1052. Grumpy Bookstore Owner
Solution
Find the original number if not use clam down technique
Contemplate about when to use the technique and move the region to find the best case
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
int orig = 0;
for(int i = 0; i < customers.length; i++){
orig += (grumpy[i] == 0) ? customers[i] : 0;
}
if(X == 0) return orig;
int ret = orig;
int j = 0, value = 0;
for(int i = 0; i < customers.length - X + 1; i++){
while(j < i+X){
value += (grumpy[j] == 1) ? customers[j] : 0;
j++;
}
if((j - X -1) >= 0 && grumpy[j-X-1] == 1){
value -= customers[j-X-1];
}
ret = Math.max(ret, value + orig);
}
return ret;
}
Last updated
Was this helpful?