-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1054_GrumpyBookstoreOwner.java
More file actions
67 lines (62 loc) · 2.67 KB
/
P1054_GrumpyBookstoreOwner.java
File metadata and controls
67 lines (62 loc) · 2.67 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package yyl.leetcode.p10;
import yyl.leetcode.util.Assert;
/**
* <h3>爱生气的书店老板</h3><br>
* 今天,书店老板有一家店打算试营业 customers.length 分钟。每分钟都有一些顾客(customers[i])会进入书店,所有这些顾客都会在那一分钟结束后离开。<br>
* 在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。 当书店老板生气时,那一分钟的顾客就会不满意,不生气则他们是满意的。 <br>
* 书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 X 分钟不生气,但却只能使用一次。 <br>
* 请你返回这一天营业下来,最多有多少客户能够感到满意的数量。 <br>
*
* <pre>
* 示例:
* 输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
* 输出:16
* 解释:
* 书店老板在最后 3 分钟保持冷静。
* 感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.
* </pre>
*
* 提示:<br>
* ├ 1 <= X <= customers.length == grumpy.length <= 20000<br>
* ├ 0 <= customers[i] <= 1000<br>
* └ 0 <= grumpy[i] <= 1 <br>
*/
public class P1054_GrumpyBookstoreOwner {
public static void main(String[] args) {
Solution solution = new Solution();
Assert.assertEquals(16, solution.maxSatisfied(new int[] { 1, 0, 1, 2, 1, 1, 7, 5 }, new int[] { 0, 1, 0, 1, 0, 1, 0, 1 }, 3));
}
// 滑动窗口
// 先求出老板不生气时候的满意顾客总数
// 然后使用滑动窗口求出,不生气所能增加的最大满意顾客总数
// 时间复杂度:O(n),其中 n 是数组 customers 和 grumpy 的长度(customers.length==grumpy.length)。
// 空间复杂度:O(1)。
static class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
int n = customers.length;
int total = 0;
for (int i = 0; i < n; i++) {
if (grumpy[i] == 0) {
total += customers[i];
}
}
int increase = 0;
for (int i = 0; i < X; i++) {
if (grumpy[i] == 1) {
increase += customers[i];
}
}
int maxIncrease = increase;
for (int i = X; i < n; i++) {
if (grumpy[i - X] == 1) {
increase -= customers[i - X];
}
if (grumpy[i] == 1) {
increase += customers[i];
}
maxIncrease = Math.max(maxIncrease, increase);
}
return total + maxIncrease;
}
}
}