Skip to content

Commit 2f01e7b

Browse files
committed
feat: add solutions to lc problem: No.1052
No.1052.Grumpy Bookstore Owner
1 parent c2c2b52 commit 2f01e7b

File tree

6 files changed

+240
-81
lines changed

6 files changed

+240
-81
lines changed

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

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50-
-`s` 累计不使用秘密技巧时,满意的顾客数;
51-
-`t` 计算大小为 `X` 的滑动窗口最多增加的满意的顾客数;
52-
- 结果即为 `s+t`
50+
**方法一:滑动窗口**
51+
52+
定义 $s$ 表示所有不满意的顾客总数,$cs$ 表示顾客总数
5353

5454
<!-- tabs:start -->
5555

@@ -59,22 +59,16 @@
5959

6060
```python
6161
class Solution:
62-
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
63-
# 用s累计不使用秘密技巧时,满意的顾客数
64-
# 用t计算大小为X的滑动窗口最多增加的满意的顾客数
65-
# 结果即为s+t
66-
s = t = 0
67-
win, n = 0, len(customers)
68-
for i in range(n):
69-
if grumpy[i] == 0:
70-
s += customers[i]
71-
else:
72-
win += customers[i]
73-
if i >= X and grumpy[i - X] == 1:
74-
win -= customers[i - X]
75-
# 求滑动窗口的最大值
76-
t = max(t, win)
77-
return s + t
62+
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
63+
s = sum(a * b for a, b in zip(customers, grumpy))
64+
cs = sum(customers)
65+
t = ans = 0
66+
for i, (a, b) in enumerate(zip(customers, grumpy), 1):
67+
t += a * b
68+
if (j := i - minutes) >= 0:
69+
ans = max(ans, cs - (s - t))
70+
t -= customers[j] * grumpy[j]
71+
return ans
7872
```
7973

8074
### **Java**
@@ -83,25 +77,82 @@ class Solution:
8377

8478
```java
8579
class Solution {
86-
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
87-
// 用s累计不使用秘密技巧时,满意的顾客数
88-
// 用t计算大小为X的滑动窗口最多增加的满意的顾客数
89-
// 结果即为s+t
90-
int s = 0, t = 0;
91-
for (int i = 0, win = 0, n = customers.length; i < n; ++i) {
92-
if (grumpy[i] == 0) {
93-
s += customers[i];
94-
} else {
95-
win += customers[i];
80+
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
81+
int s = 0, cs = 0;
82+
int n = customers.length;
83+
for (int i = 0; i < n; ++i) {
84+
s += customers[i] * grumpy[i];
85+
cs += customers[i];
86+
}
87+
int t = 0, ans = 0;
88+
for (int i = 0; i < n; ++i) {
89+
t += customers[i] * grumpy[i];
90+
int j = i - minutes + 1;
91+
if (j >= 0) {
92+
ans = Math.max(ans, cs - (s - t));
93+
t -= customers[j] * grumpy[j];
9694
}
97-
if (i >= X && grumpy[i - X] == 1) {
98-
win -= customers[i - X];
95+
}
96+
return ans;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) {
107+
int s = 0, cs = 0;
108+
int n = customers.size();
109+
for (int i = 0; i < n; ++i)
110+
{
111+
s += customers[i] * grumpy[i];
112+
cs += customers[i];
113+
}
114+
int t = 0, ans = 0;
115+
for (int i = 0; i < n; ++i)
116+
{
117+
t += customers[i] * grumpy[i];
118+
int j = i - minutes + 1;
119+
if (j >= 0)
120+
{
121+
ans = max(ans, cs - (s - t));
122+
t -= customers[j] * grumpy[j];
99123
}
100-
// 求滑动窗口的最大值
101-
t = Math.max(t, win);
102124
}
103-
return s + t;
125+
return ans;
104126
}
127+
};
128+
```
129+
130+
### **Go**
131+
132+
```go
133+
func maxSatisfied(customers []int, grumpy []int, minutes int) int {
134+
s, cs := 0, 0
135+
for i, c := range customers {
136+
s += c * grumpy[i]
137+
cs += c
138+
}
139+
t, ans := 0, 0
140+
for i, c := range customers {
141+
t += c * grumpy[i]
142+
j := i - minutes + 1
143+
if j >= 0 {
144+
ans = max(ans, cs-(s-t))
145+
t -= customers[j] * grumpy[j]
146+
}
147+
}
148+
return ans
149+
}
150+
151+
func max(a, b int) int {
152+
if a > b {
153+
return a
154+
}
155+
return b
105156
}
106157
```
107158

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

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,98 @@ The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 =
4949

5050
```python
5151
class Solution:
52-
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
53-
s = t = 0
54-
win, n = 0, len(customers)
55-
for i in range(n):
56-
if grumpy[i] == 0:
57-
s += customers[i]
58-
else:
59-
win += customers[i]
60-
if i >= X and grumpy[i - X] == 1:
61-
win -= customers[i - X]
62-
t = max(t, win)
63-
return s + t
52+
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
53+
s = sum(a * b for a, b in zip(customers, grumpy))
54+
cs = sum(customers)
55+
t = ans = 0
56+
for i, (a, b) in enumerate(zip(customers, grumpy), 1):
57+
t += a * b
58+
if (j := i - minutes) >= 0:
59+
ans = max(ans, cs - (s - t))
60+
t -= customers[j] * grumpy[j]
61+
return ans
6462
```
6563

6664
### **Java**
6765

6866
```java
6967
class Solution {
70-
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
71-
int s = 0, t = 0;
72-
for (int i = 0, win = 0, n = customers.length; i < n; ++i) {
73-
if (grumpy[i] == 0) {
74-
s += customers[i];
75-
} else {
76-
win += customers[i];
68+
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
69+
int s = 0, cs = 0;
70+
int n = customers.length;
71+
for (int i = 0; i < n; ++i) {
72+
s += customers[i] * grumpy[i];
73+
cs += customers[i];
74+
}
75+
int t = 0, ans = 0;
76+
for (int i = 0; i < n; ++i) {
77+
t += customers[i] * grumpy[i];
78+
int j = i - minutes + 1;
79+
if (j >= 0) {
80+
ans = Math.max(ans, cs - (s - t));
81+
t -= customers[j] * grumpy[j];
7782
}
78-
if (i >= X && grumpy[i - X] == 1) {
79-
win -= customers[i - X];
83+
}
84+
return ans;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) {
95+
int s = 0, cs = 0;
96+
int n = customers.size();
97+
for (int i = 0; i < n; ++i)
98+
{
99+
s += customers[i] * grumpy[i];
100+
cs += customers[i];
101+
}
102+
int t = 0, ans = 0;
103+
for (int i = 0; i < n; ++i)
104+
{
105+
t += customers[i] * grumpy[i];
106+
int j = i - minutes + 1;
107+
if (j >= 0)
108+
{
109+
ans = max(ans, cs - (s - t));
110+
t -= customers[j] * grumpy[j];
80111
}
81-
t = Math.max(t, win);
82112
}
83-
return s + t;
113+
return ans;
84114
}
115+
};
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
func maxSatisfied(customers []int, grumpy []int, minutes int) int {
122+
s, cs := 0, 0
123+
for i, c := range customers {
124+
s += c * grumpy[i]
125+
cs += c
126+
}
127+
t, ans := 0, 0
128+
for i, c := range customers {
129+
t += c * grumpy[i]
130+
j := i - minutes + 1
131+
if j >= 0 {
132+
ans = max(ans, cs-(s-t))
133+
t -= customers[j] * grumpy[j]
134+
}
135+
}
136+
return ans
137+
}
138+
139+
func max(a, b int) int {
140+
if a > b {
141+
return a
142+
}
143+
return b
85144
}
86145
```
87146

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) {
4+
int s = 0, cs = 0;
5+
int n = customers.size();
6+
for (int i = 0; i < n; ++i)
7+
{
8+
s += customers[i] * grumpy[i];
9+
cs += customers[i];
10+
}
11+
int t = 0, ans = 0;
12+
for (int i = 0; i < n; ++i)
13+
{
14+
t += customers[i] * grumpy[i];
15+
int j = i - minutes + 1;
16+
if (j >= 0)
17+
{
18+
ans = max(ans, cs - (s - t));
19+
t -= customers[j] * grumpy[j];
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func maxSatisfied(customers []int, grumpy []int, minutes int) int {
2+
s, cs := 0, 0
3+
for i, c := range customers {
4+
s += c * grumpy[i]
5+
cs += c
6+
}
7+
t, ans := 0, 0
8+
for i, c := range customers {
9+
t += c * grumpy[i]
10+
j := i - minutes + 1
11+
if j >= 0 {
12+
ans = max(ans, cs-(s-t))
13+
t -= customers[j] * grumpy[j]
14+
}
15+
}
16+
return ans
17+
}
18+
19+
func max(a, b int) int {
20+
if a > b {
21+
return a
22+
}
23+
return b
24+
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
class Solution {
2-
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
3-
int s = 0, t = 0;
4-
for (int i = 0, win = 0, n = customers.length; i < n; ++i) {
5-
if (grumpy[i] == 0) {
6-
s += customers[i];
7-
} else {
8-
win += customers[i];
9-
}
10-
if (i >= X && grumpy[i - X] == 1) {
11-
win -= customers[i - X];
2+
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
3+
int s = 0, cs = 0;
4+
int n = customers.length;
5+
for (int i = 0; i < n; ++i) {
6+
s += customers[i] * grumpy[i];
7+
cs += customers[i];
8+
}
9+
int t = 0, ans = 0;
10+
for (int i = 0; i < n; ++i) {
11+
t += customers[i] * grumpy[i];
12+
int j = i - minutes + 1;
13+
if (j >= 0) {
14+
ans = Math.max(ans, cs - (s - t));
15+
t -= customers[j] * grumpy[j];
1216
}
13-
t = Math.max(t, win);
1417
}
15-
return s + t;
18+
return ans;
1619
}
1720
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
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
2+
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
3+
s = sum(a * b for a, b in zip(customers, grumpy))
4+
cs = sum(customers)
5+
t = ans = 0
6+
for i, (a, b) in enumerate(zip(customers, grumpy), 1):
7+
t += a * b
8+
if (j := i - minutes) >= 0:
9+
ans = max(ans, cs - (s - t))
10+
t -= customers[j] * grumpy[j]
11+
return ans

0 commit comments

Comments
 (0)