思路:
- 读完题目一定要搞清楚要求的东西是需要怎么组成,最大的满意度 = 老板不生气的时间点的满意度总和(也就是
grumpy[i] == 0
的所有位置的满意度) + 在window时间内生气的老板会导致流失的最多满意度。 - 统计不生气时间点满意度总和很容易
- 再利用滑动窗口计算老板生气时刻
grumpy[i] == 1
,窗口内最大的满意度是多少,稍微比滑动窗口最大值更简单一点。
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
// max satisfied = original satisfied + suppress emotion brings satisfied.
int len = customers.length;
int originalSatisfied = 0;
// find max satified within X.
// find max within window
int right = 0;
int max = 0;
int satifiedInWindow = 0;
while (right < len) {
if (grumpy[right] == 1) {
satifiedInWindow += customers[right];
} else { // == 0
originalSatisfied += customers[right];
}
if (right >= X - 1) {
max = Math.max(max, satifiedInWindow);
// left++
if (grumpy[right - X + 1] == 1) {
satifiedInWindow -= customers[right - X + 1];
}
}
right++;
}
return originalSatisfied + max;
}
}