Skip to content

Commit 8b2c308

Browse files
committed
feat: add solutions to leetcode problem: No.1052
1 parent cd73d5f commit 8b2c308

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

solution/1000-1099/1052.Grumpy Bookstore Owner/README.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,62 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
-`s` 累计不使用秘密技巧时,满意的顾客数;
41+
-`t` 计算大小为 `X` 的滑动窗口最多增加的满意的顾客数;
42+
- 结果即为 `s+t`
43+
4044
<!-- tabs:start -->
4145

4246
### **Python3**
4347

4448
<!-- 这里可写当前语言的特殊实现逻辑 -->
4549

4650
```python
47-
51+
class Solution:
52+
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
53+
# 用s累计不使用秘密技巧时,满意的顾客数
54+
# 用t计算大小为X的滑动窗口最多增加的满意的顾客数
55+
# 结果即为s+t
56+
s = t = 0
57+
win, n = 0, len(customers)
58+
for i in range(n):
59+
if grumpy[i] == 0:
60+
s += customers[i]
61+
else:
62+
win += customers[i]
63+
if i >= X and grumpy[i - X] == 1:
64+
win -= customers[i - X]
65+
# 求滑动窗口的最大值
66+
t = max(t, win)
67+
return s + t
4868
```
4969

5070
### **Java**
5171

5272
<!-- 这里可写当前语言的特殊实现逻辑 -->
5373

5474
```java
55-
75+
class Solution {
76+
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
77+
// 用s累计不使用秘密技巧时,满意的顾客数
78+
// 用t计算大小为X的滑动窗口最多增加的满意的顾客数
79+
// 结果即为s+t
80+
int s = 0, t = 0;
81+
for (int i = 0, win = 0, n = customers.length; i < n; ++i) {
82+
if (grumpy[i] == 0) {
83+
s += customers[i];
84+
} else {
85+
win += customers[i];
86+
}
87+
if (i >= X && grumpy[i - X] == 1) {
88+
win -= customers[i - X];
89+
}
90+
// 求滑动窗口的最大值
91+
t = Math.max(t, win);
92+
}
93+
return s + t;
94+
}
95+
}
5696
```
5797

5898
### **...**

solution/1000-1099/1052.Grumpy Bookstore Owner/README_EN.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,41 @@ The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 =
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
50+
s = t = 0
51+
win, n = 0, len(customers)
52+
for i in range(n):
53+
if grumpy[i] == 0:
54+
s += customers[i]
55+
else:
56+
win += customers[i]
57+
if i >= X and grumpy[i - X] == 1:
58+
win -= customers[i - X]
59+
t = max(t, win)
60+
return s + t
4961
```
5062

5163
### **Java**
5264

5365
```java
54-
66+
class Solution {
67+
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
68+
int s = 0, t = 0;
69+
for (int i = 0, win = 0, n = customers.length; i < n; ++i) {
70+
if (grumpy[i] == 0) {
71+
s += customers[i];
72+
} else {
73+
win += customers[i];
74+
}
75+
if (i >= X && grumpy[i - X] == 1) {
76+
win -= customers[i - X];
77+
}
78+
t = Math.max(t, win);
79+
}
80+
return s + t;
81+
}
82+
}
5583
```
5684

5785
### **...**

solution/1000-1099/1052.Grumpy Bookstore Owner/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
33
int s = 0, t = 0;
4-
for (int i = 0, win = 0; i < customers.length; ++i) {
4+
for (int i = 0, win = 0, n = customers.length; i < n; ++i) {
55
if (grumpy[i] == 0) {
66
s += customers[i];
77
} else {
@@ -14,4 +14,4 @@ public int maxSatisfied(int[] customers, int[] grumpy, int X) {
1414
}
1515
return s + t;
1616
}
17-
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
3+
s = t = 0
4+
win, n = 0, len(customers)
5+
for i in range(n):
6+
if grumpy[i] == 0:
7+
s += customers[i]
8+
else:
9+
win += customers[i]
10+
if i >= X and grumpy[i - X] == 1:
11+
win -= customers[i - X]
12+
t = max(t, win)
13+
return s + t

0 commit comments

Comments
 (0)