Minimum Penalty for a Shop
You are given the customer visit log of a shop represented by a 0-indexed string customers'N''Y'
-
if the
character isith , it means that customers come at the'Y' hourith -
whereas
indicates that no customers come at the'N' hour.ith
If the shop closes at the jth0 <= j <= n
-
For every hour when the shop is open and no customers come, the penalty increases by
.1 -
For every hour when the shop is closed and customers come, the penalty increases by
.1
Return the earliest hour at which the shop must be closed to incur a minimum penalty.
Note:
Note that if a shop closes at the jthj
Example 1
Output: 2
Explanation:
- Closing the shop at the 0th hour incurs in 1+1+0+1 = 3 penalty.
- Closing the shop at the 1st hour incurs in 0+1+0+1 = 2 penalty.
- Closing the shop at the 2nd hour incurs in 0+0+0+1 = 1 penalty.
- Closing the shop at the 3rd hour incurs in 0+0+1+1 = 2 penalty.
- Closing the shop at the 4th hour incurs in 0+0+1+0 = 1 penalty.
Closing the shop at 2nd or 4th hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
Example 2
Output: 0
Explanation: It is best to close the shop at the 0th hour as no customers arrive.
Example 3
Output: 4
Explanation: It is best to close the shop at the 4th hour as customers arrive at each hour.
Constraints:
1 <= customers.length <= 105 consists only of characterscustomers and'Y' .'N'
Contents
Solution 1 - Create prefix sum array scanning through N's and postfix sum array scanning Y's
In this approach, we will compute a prefix sum, by looking at 'N'jth
We will also compute a postfix sum, by looking at 'Y'jth
Note:
The reason why we chose for exclusive when calculating prefix sum, and inclusive when calculating postfix sum is because, when we look at these two arrays
to get the penalty at jth
public class MinimumPenaltyForAShop {
static int bestClosingTime(String customers) {
int n = customers.length();
int[] prefix = new int[n+1];
int[] postfix = new int[n+1];
// customers = "YYNY" , prefix[0] = 0 since there will not be any N's before this
for(int i=1; i<prefix.length; i++) {
prefix[i] = prefix[i-1];
if(customers.charAt(i-1) == 'N') {
prefix[i] += 1;
}
}
//postfix[n] = 0 since there will not be any Y's after this
// in postfix, we are counting Y's until now + current one
for(int i=postfix.length-2; i>=0; i--) {
postfix[i] = postfix[i+1];
if(customers.charAt(i) == 'Y') {
postfix[i] += 1;
}
}
int minPenalty = Integer.MAX_VALUE;
int index = 0;
for(int i=0;i<prefix.length; i++) {
int penalty = prefix[i] + postfix[i];
if(penalty < minPenalty) {
minPenalty = penalty;
index = i;
}
}
return index;
}
public static void main(String[] args) {
System.out.println(bestClosingTime("YYNY"));
System.out.println(bestClosingTime("NNNNN"));
System.out.println(bestClosingTime("YYYY"));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n) time where ncustomers
Space complexity: O(n), since we are creating prefix and postfix sum arrays with size n
Solution 2 - Using prefix sum, memory optimized approach
In this approach, we will first compute the maximum penalty possible, that is what if the shop is closed at 0th hour, loop through input string customersYpenalty
Next, we will loop through customers'Y'penalty1'N'penaltypenaltyindex +1
public class MinimumPenaltyForAShop {
static int bestClosingTimeMemoryOptimized(String customers) {
int penalty = 0;
int n = customers.length();
for(int i=0; i<n; i++) {
if(customers.charAt(i) == 'Y') {
penalty++;
}
}
int minPenalty = penalty;
int index = 0;
for(int i=0; i<n; i++) {
if(customers.charAt(i) == 'Y') {
penalty--;
} else {
penalty++;
}
if(penalty < minPenalty) {
minPenalty = penalty;
index = i+1;
}
}
return index;
}
public static void main(String[] args) {
System.out.println("############### Memory optimized ######################");
System.out.println(bestClosingTimeMemoryOptimized("YYNY"));
System.out.println(bestClosingTimeMemoryOptimized("NNNNN"));
System.out.println(bestClosingTimeMemoryOptimized("YYYY"));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n) time where ncustomers
Space complexity: O(1).
Above implementations source code can be found at