-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution1052.java
36 lines (29 loc) · 939 Bytes
/
Solution1052.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package leetcode.slidingwindow;
public class Solution1052 {
public static void main(String[] args) {
System.out.println(new Solution1052().maxSatisfied(
new int[]{1, 0, 1, 2, 1, 1, 7, 5},
new int[]{0, 1, 0, 1, 0, 1, 0, 1}, 3));
}
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
int already = 0;
for (int i = 0; i < customers.length; i++) {
if (grumpy[i] == 0) {
already += customers[i];
customers[i] = 0;
}
}
int max = 0;
int sum = 0;
int left = 0, right = 0;
while (right < customers.length) {
sum += customers[right];
if (right - left + 1 == minutes) {
max = Math.max(max, sum);
sum -= customers[left++];
}
right++;
}
return already + max;
}
}