NeetCode #224LC-1052MediumSliding Window
← Back to All Problems#224 · #1052 · Grumpy Bookstore Owner(爱生气的书店老板)
📌 Problem Statement & Constraints
A bookstore owner has
customers[i] customers at minute i and a grumpy[i] flag that is 1 when they are grumpy (in which case those customers do not buy). The owner can use a secret technique for minutes consecutive minutes to make themselves not grumpy. Return the maximum number of satisfied customers. Constraints: 1 <= customers.length == grumpy.length <= 2 * 10^4, 0 <= customers[i] <= 1000.💡 Core Algorithmic Approaches
- Customers are always satisfied when the owner is not grumpy; call that the baseline.
- The technique can additionally recover the customers in one window of length
minuteswhere the owner is grumpy. - So slide a window of that length, summing only the grumpy-minute customers, and take the maximum extra gain.
- The answer is the baseline plus that maximum gain.
💻 Benchmark Python3 Implementation
class Solution:
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
n = len(customers)
# baseline: customers served when the owner is not grumpy
base = sum(c for c, g in zip(customers, grumpy) if g == 0)
# extra: the best window of `minutes` of grumpy customers
cur = 0
best = 0
for i in range(n):
if grumpy[i] == 1:
cur += customers[i]
if i >= minutes and grumpy[i - minutes] == 1:
cur -= customers[i - minutes] # left the window
best = max(best, cur)
return base + best⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): one pass for the baseline and one sliding pass.
💾 Space Complexity
O(1): two accumulators.
⚠️ Interview Pitfalls & Follow-ups
- Including non-grumpy customers in the window sum: they are already counted in the baseline, so only grumpy minutes should contribute to the extra gain.
- Evicting based on
customers[i-minutes]instead ofgrumpy[i-minutes]: only grumpy minutes were added, so only they should be removed. - Using a
whileloop to shrink the window: the window has a fixed length, so a simpleifeviction is correct. - Forgetting the baseline: the technique only affects
minutesminutes; all other non-grumpy minutes still contribute.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Sliding Window Problems
#219 · #219Contains Duplicate II#220 · #121Best Time to Buy and Sell Stock#221 · #2379Minimum Recolors to Get K Consecutive Black Blocks#222 · #1984Minimum Difference Between Highest and Lowest of K Scores#223 · #1343Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold#225 · #487Max Consecutive Ones II
中文速览与面试要点
爱生气的书店老板
有一个书店老板,他的书店开了 n 分钟。第 i 分钟有一些顾客 customers[i] 会进入书店,所有这些顾客都会在那一分钟结束后离开。在某些时候,书店老板会生气。如果书店老板在第 i 分钟生气,那么他会在第 i 分钟不卖书。 grumpy[i] 表示老板是否生气:0 表示不生气,1 表示生气。当书店老板生气时,那一分钟的顾客就会不满意。书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 minutes 分钟不生气,但却只能使用一次。请你返回这一天营业下来,最多有多少客户能够感到满意。约束:n == customers.length == grumpy.length;1 ≤ minutes ≤ n ≤ 2 × 10^4;0 ≤ customers[i] ≤ 1000;grumpy[i] 为 0 或 1。
核心要点
- 分两部分计算(标准解) :① 老板原本不生气时( grumpy[i] == 0 )的顾客一定满意,先累加为 base 。
- ② 用长度为 minutes 的窗口找「能额外挽回的顾客数」 :窗口内 grumpy[i] == 1 的顾客会被技巧覆盖而变得满意,其总和即额外收益。
- 滚动更新窗口收益 :窗口右移时,若新进入位置老板生气则加上该顾客数;若滑出位置老板生气则减去。
- 答案 = base + 最大额外收益 。
常见陷阱
- 把 base 也算进窗口收益 :窗口收益只应包含「老板生气时段的顾客」(即原本不满意的),否则会重复计算。
- 忘记处理滑出窗口的减法 :窗口会一直增长,收益被高估。
- 误以为技巧必须从某一分钟开始 :题目只说「连续 minutes 分钟」,起始位置任意,因此需要枚举所有窗口。
- minutes == n 时 :窗口覆盖全部,收益为所有生气时段的顾客数,本解正确。
参考实现
class Solution:
def maxSatisfied(self, customers: list[int], grumpy: list[int], minutes: int) -> int:
# ① 老板原本不生气时的满意顾客数(必定满意)
base = sum(c for c, g in zip(customers, grumpy) if g == 0)
# ② 滑动窗口求「使用技巧后额外挽回的顾客数」
extra = sum(c for c, g in zip(customers[:minutes], grumpy[:minutes]) if g == 1)
best_extra = extra
for i in range(minutes, len(customers)):
if grumpy[i] == 1:
extra += customers[i] # 新进入窗口
if grumpy[i - minutes] == 1:
extra -= customers[i - minutes] # 滑出窗口
if extra > best_extra:
best_extra = extra
return base + best_extra