Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two integers w and m, and an integer array arrivals, where arrivals[i] is the type of item arriving on day i (days are 1-indexed).
Items are managed according to the following rules:
i, consider the window of days [max(1, i - w + 1), i] (the w most recent days up to day i):
m times among kept arrivals whose arrival day lies in that window.i would cause its type to appear more than m times in the window, that arrival must be discarded.Return the minimum number of arrivals to be discarded so that every w-day window contains at most m occurrences of each type.
Example 1:
Input: arrivals = [1,2,1,3,1], w = 4, m = 2
Output: 0
Explanation:
m occurrences of this type, so we keep it.[1, 2, 1] has item 1 twice, within limit.[1, 2, 1, 3] has item 1 twice, allowed.[2, 1, 3, 1] has item 1 twice, still valid.There are no discarded items, so return 0.
Example 2:
Input: arrivals = [1,2,3,3,3,4], w = 3, m = 2
Output: 1
Explanation:
[1, 2] is fine.[1, 2, 3] has item 3 once.[2, 3, 3] has item 3 twice, allowed.[3, 3, 3] has item 3 three times, exceeds limit, so the arrival must be discarded.[3, 4] is fine.Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.
Constraints:
1 <= arrivals.length <= 1051 <= arrivals[i] <= 1051 <= w <= arrivals.length1 <= m <= wProblem summary: You are given two integers w and m, and an integer array arrivals, where arrivals[i] is the type of item arriving on day i (days are 1-indexed). Items are managed according to the following rules: Each arrival may be kept or discarded; an item may only be discarded on its arrival day. For each day i, consider the window of days [max(1, i - w + 1), i] (the w most recent days up to day i): For any such window, each item type may appear at most m times among kept arrivals whose arrival day lies in that window. If keeping the arrival on day i would cause its type to appear more than m times in the window, that arrival must be discarded. Return the minimum number of arrivals to be discarded so that every w-day window contains at most m occurrences of each type.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Sliding Window
[1,2,1,3,1] 4 2
[1,2,3,3,3,4] 3 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3679: Minimum Discards to Balance Inventory
class Solution {
public int minArrivalsToDiscard(int[] arrivals, int w, int m) {
Map<Integer, Integer> cnt = new HashMap<>();
int n = arrivals.length;
int[] marked = new int[n];
int ans = 0;
for (int i = 0; i < n; i++) {
int x = arrivals[i];
if (i >= w) {
int prev = arrivals[i - w];
cnt.merge(prev, -marked[i - w], Integer::sum);
}
if (cnt.getOrDefault(x, 0) >= m) {
ans++;
} else {
marked[i] = 1;
cnt.merge(x, 1, Integer::sum);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3679: Minimum Discards to Balance Inventory
func minArrivalsToDiscard(arrivals []int, w int, m int) (ans int) {
cnt := make(map[int]int)
n := len(arrivals)
marked := make([]int, n)
for i, x := range arrivals {
if i >= w {
cnt[arrivals[i-w]] -= marked[i-w]
}
if cnt[x] >= m {
ans++
} else {
marked[i] = 1
cnt[x]++
}
}
return
}
# Accepted solution for LeetCode #3679: Minimum Discards to Balance Inventory
class Solution:
def minArrivalsToDiscard(self, arrivals: List[int], w: int, m: int) -> int:
cnt = Counter()
n = len(arrivals)
marked = [0] * n
ans = 0
for i, x in enumerate(arrivals):
if i >= w:
cnt[arrivals[i - w]] -= marked[i - w]
if cnt[x] >= m:
ans += 1
else:
marked[i] = 1
cnt[x] += 1
return ans
// Accepted solution for LeetCode #3679: Minimum Discards to Balance Inventory
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3679: Minimum Discards to Balance Inventory
// class Solution {
// public int minArrivalsToDiscard(int[] arrivals, int w, int m) {
// Map<Integer, Integer> cnt = new HashMap<>();
// int n = arrivals.length;
// int[] marked = new int[n];
// int ans = 0;
// for (int i = 0; i < n; i++) {
// int x = arrivals[i];
// if (i >= w) {
// int prev = arrivals[i - w];
// cnt.merge(prev, -marked[i - w], Integer::sum);
// }
// if (cnt.getOrDefault(x, 0) >= m) {
// ans++;
// } else {
// marked[i] = 1;
// cnt.merge(x, 1, Integer::sum);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3679: Minimum Discards to Balance Inventory
function minArrivalsToDiscard(arrivals: number[], w: number, m: number): number {
const cnt = new Map<number, number>();
const n = arrivals.length;
const marked = Array<number>(n).fill(0);
let ans = 0;
for (let i = 0; i < n; i++) {
const x = arrivals[i];
if (i >= w) {
cnt.set(arrivals[i - w], (cnt.get(arrivals[i - w]) || 0) - marked[i - w]);
}
if ((cnt.get(x) || 0) >= m) {
ans++;
} else {
marked[i] = 1;
cnt.set(x, (cnt.get(x) || 0) + 1);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.