Skip to content

Commit 28f356d

Browse files
committed
一刷1052
1 parent d3a8102 commit 28f356d

File tree

6 files changed

+105
-30
lines changed

6 files changed

+105
-30
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7389,14 +7389,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
73897389
//|{doc_base_url}/1051-height-checker.adoc[题解]
73907390
//|Easy
73917391
//|
7392-
//
7393-
//|{counter:codes}
7394-
//|{leetcode_base_url}/grumpy-bookstore-owner/[1052. Grumpy Bookstore Owner^]
7395-
//|{source_base_url}/_1052_GrumpyBookstoreOwner.java[Java]
7396-
//|{doc_base_url}/1052-grumpy-bookstore-owner.adoc[题解]
7397-
//|Medium
7398-
//|
7399-
//
7392+
7393+
|{counter:codes}
7394+
|{leetcode_base_url}/grumpy-bookstore-owner/[1052. Grumpy Bookstore Owner^]
7395+
|{source_base_url}/_1052_GrumpyBookstoreOwner.java[Java]
7396+
|{doc_base_url}/1052-grumpy-bookstore-owner.adoc[题解]
7397+
|Medium
7398+
|
7399+
74007400
//|{counter:codes}
74017401
//|{leetcode_base_url}/previous-permutation-with-one-swap/[1053. Previous Permutation With One Swap^]
74027402
//|{source_base_url}/_1053_PreviousPermutationWithOneSwap.java[Java]

docs/1052-grumpy-bookstore-owner.adoc

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,73 @@
11
[#1052-grumpy-bookstore-owner]
2-
= 1052. Grumpy Bookstore Owner
2+
= 1052. 爱生气的书店老板
33

4-
{leetcode}/problems/grumpy-bookstore-owner/[LeetCode - Grumpy Bookstore Owner^]
4+
https://leetcode.cn/problems/grumpy-bookstore-owner/[LeetCode - 1052. 爱生气的书店老板 ^]
55

6-
Today, the bookstore owner has a store open for `customers.length` minutes. Every minute, some number of customers (`customers[i]`) enter the store, and all those customers leave after the end of that minute.
6+
有一个书店老板,他的书店开了 `n` 分钟。每分钟都有一些顾客进入这家商店。给定一个长度为 `n` 的整数数组 `customers` ,其中 `customers[i]` 是在第 `i` 分钟开始时进入商店的顾客数量,所有这些顾客在第 `i` 分钟结束后离开。
77

8-
On some minutes, the bookstore owner is grumpy. If the bookstore owner is grumpy on the i-th minute, `grumpy[i] = 1`, otherwise `grumpy[i] = 0`. When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.
8+
在某些分钟内,书店老板会生气。 如果书店老板在第 `i` 分钟生气,那么 `grumpy[i] = 1`,否则 `grumpy[i] = 0`
99

10-
The bookstore owner knows a secret technique to keep themselves not grumpy for `X` minutes straight, but can only use it once.
10+
当书店老板生气时,那一分钟的顾客就会不满意,若老板不生气则顾客是满意的。
1111

12-
Return the maximum number of customers that can be satisfied throughout the day.
12+
书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 `minutes` 分钟不生气,但却只能使用一次。
1313

14-
14+
请你返回 _这一天营业下来,最多有多少客户能够感到满意_
1515

16-
*Example 1:*
16+
*示例 1:*
1717

18-
[subs="verbatim,quotes,macros"]
19-
----
20-
*Input:* customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
21-
*Output:* 16
22-
*Explanation:* The bookstore owner keeps themselves not grumpy for the last 3 minutes.
23-
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
24-
----
18+
....
19+
输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
20+
输出:16
21+
解释:书店老板在最后 3 分钟保持冷静。
22+
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.
23+
....
24+
25+
*示例 2:*
26+
27+
....
28+
输入:customers = [1], grumpy = [0], minutes = 1
29+
输出:1
30+
....
2531

26-
2732

28-
*Note:*
33+
*提示:*
2934

35+
* `n == customers.length == grumpy.length`
36+
* `1 \<= minutes \<= n \<= 2 * 10^4^`
37+
* `0 \<= customers[i] \<= 1000`
38+
* `grumpy[i] == 0 or 1`
3039
31-
* `1 <= X <= customers.length == grumpy.length <= 20000`
32-
* `0 <= customers[i] <= 1000`
33-
* `0 <= grumpy[i] <= 1`
3440
41+
== 思路分析
3542

43+
先计算出老板不生气的情况下,会有多少顾客满意。然后使用滑动窗口,计算在窗口内老板原本因为生气而不满意,但因为老板不生气而满意的顾客顾客数量,去窗口满意数量最大值,再加原本已经满意的顾客数量即可得到结果值。
44+
45+
image::images/1052-10.gif[{image_attr}]
3646

3747
[[src-1052]]
48+
[tabs]
49+
====
50+
一刷::
51+
+
52+
--
3853
[{java_src_attr}]
3954
----
4055
include::{sourcedir}/_1052_GrumpyBookstoreOwner.java[tag=answer]
4156
----
57+
--
58+
59+
// 二刷::
60+
// +
61+
// --
62+
// [{java_src_attr}]
63+
// ----
64+
// include::{sourcedir}/_1052_GrumpyBookstoreOwner_2.java[tag=answer]
65+
// ----
66+
// --
67+
====
68+
69+
70+
== 参考资料
4271

72+
. https://leetcode.cn/problems/grumpy-bookstore-owner/solutions/615133/ai-sheng-qi-de-shu-dian-lao-ban-by-leetc-dloq/[1052. 爱生气的书店老板 - 官方题解^]
73+
. https://leetcode.cn/problems/grumpy-bookstore-owner/solutions/616238/yong-mi-mi-ji-qiao-wan-liu-zhu-zui-duo-d-py41/[1052. 爱生气的书店老板 - 用「秘密技巧」挽留住最多的原本因为生气而被赶走的顾客^]

docs/images/1052-10.gif

8.72 MB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,7 @@ include::1049-last-stone-weight-ii.adoc[leveloffset=+1]
21872187

21882188
// include::1051-height-checker.adoc[leveloffset=+1]
21892189

2190-
// include::1052-grumpy-bookstore-owner.adoc[leveloffset=+1]
2190+
include::1052-grumpy-bookstore-owner.adoc[leveloffset=+1]
21912191

21922192
// include::1053-previous-permutation-with-one-swap.adoc[leveloffset=+1]
21932193

logbook/202503.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,10 @@ endif::[]
813813
|{doc_base_url}/0890-find-and-replace-pattern.adoc[题解]
814814
|✅ 映射。建立一个字母映射,并且每个字母只能有一种映射关系。否则就不匹配。
815815

816-
816+
|{counter:codes2503}
817+
|{leetcode_base_url}/grumpy-bookstore-owner/[1052. 爱生气的书店老板^]
818+
|{doc_base_url}/1052-grumpy-bookstore-owner.adoc[题解]
819+
|✅ 先计算没有生气而满意的总数,再计算因为老板憋气而满意的增量用户。
817820

818821
|===
819822

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1052_GrumpyBookstoreOwner {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-25 20:02:35
9+
*/
10+
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
11+
int sum = 0;
12+
for (int i = 0; i < customers.length; i++) {
13+
if (grumpy[i] == 0) {
14+
sum += customers[i];
15+
}
16+
}
17+
int slow = 0, fast = 0;
18+
int incre = 0;
19+
int winSum = 0;
20+
while (fast < customers.length) {
21+
if (grumpy[fast] == 1) {
22+
winSum += customers[fast];
23+
}
24+
while (fast - slow + 1 > minutes) {
25+
if (grumpy[slow] == 1) {
26+
winSum -= customers[slow];
27+
}
28+
slow++;
29+
}
30+
incre = Math.max(incre, winSum);
31+
fast++;
32+
}
33+
return incre + sum;
34+
}
35+
// end::answer[]
36+
public static void main(String[] args) {
37+
new _1052_GrumpyBookstoreOwner()
38+
.maxSatisfied(new int[]{1, 0, 1, 2, 1, 1, 7, 5},
39+
new int[]{0, 1, 0, 1, 0, 1, 0, 1}, 3);
40+
}
41+
}

0 commit comments

Comments
 (0)