#1052
Medium Algorithms Grumpy bookstore owner
Array Sliding Window
64.0% acceptance
Feb 25, 2026
2696
262
There is a bookstore owner that has a store open for n minutes. You are given an integer array customers of length n where customers[i] is the number of the customers that enter the store at the start of the ith minute and all those customers leave after the end of that minute.
During certain minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise.
When the bookstore owner is grumpy, the customers entering during that minute are not satisfied. Otherwise, they are satisfied.
The bookstore owner knows a secret technique to remain not grumpy for minutes consecutive minutes, but this technique can only be used once.
Return the maximum number of customers that can be satisfied throughout the day.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn max_satisfied(customers: Vec<i32>, grumpy: Vec<i32>, minutes: i32) -> i32 {
let n = customers.len();
let m = minutes as usize;
let base: i32 = customers.iter().zip(grumpy.iter()).filter(|(_, g)| **g == 0).map(|(c, _)| c).sum();
let win: i32 = customers[..m].iter().zip(grumpy[..m].iter()).filter(|(_, g)| **g == 1).map(|(c, _)| c).sum();
let mut max_win = win;
let mut cur_win = win;
for i in m..n {
if grumpy[i] == 1 { cur_win += customers[i]; }
if grumpy[i - m] == 1 { cur_win -= customers[i - m]; }
max_win = max_win.max(cur_win);
}
base + max_win
}
}