↓Skip to main content
  1. LeetCode/

LeetCode 3679: Minimum Discards to Balance Inventory

·3 mins· ·
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-12-24

  • Submissions: Wrong Answer ×3 → Accepted ×2

Approach
#

The problem says an arrival that would exceed m must be discarded, so there is no choice to make: simulating the rule gives the minimum.

A fixed window of length w with a count map that tracks only kept arrivals:

  1. On day i, first move day i - w out of the window, subtracting only if that arrival was kept.
  2. If this type already has m in the window, discard it: answer +1.
  3. Otherwise keep it and increment its count.

My Attempts
#

VersionProblem
Attempts 1–2Added the arrival to the counts first and incremented the answer when it exceeded m, but never removed the discarded item from the counts. For [2, 2, 2], w = 2, m = 1: day 2 is discarded, day 3’s window is days 2–3 with day 2 already discarded, so day 3 is kept and the answer is 1; these versions still counted day 2 and returned 2
Attempt 3Took the discarded item back out of the counts, but when day i - w left the window it subtracted again whether or not it was discarded, so counts went too low. For [3, 3, 1, 3, 3], w = 2, m = 1, the answer is 2; this returned 1
Attempt 4, ACOn a discard, set arrivals[i] to 0 (a type that does not exist), so leaving the window subtracts from type 0 and leaves real counts alone

Solution
#

class Solution:
    def minArrivalsToDiscard(self, arrivals: List[int], w: int, m: int) -> int:
        n = len(arrivals)
        kept = [False] * n
        cnt = Counter()
        discards = 0
        for i, t in enumerate(arrivals):
            if i >= w and kept[i - w]:
                cnt[arrivals[i - w]] -= 1         # only kept arrivals leave the counts
            if cnt[t] >= m:
                discards += 1                     # the rule forces a discard
            else:
                cnt[t] += 1
                kept[i] = True
        return discards

A kept array is more direct than overwriting arrivals[i] with 0, and it leaves the input unchanged.

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Leaving discarded arrivals in the counts.
  • Subtracting discarded arrivals again when they leave the window.

Interview Explanation
#

The rule already decides which arrivals must be discarded, so simulation gives the minimum. I keep a length-w window whose counts include only kept arrivals: each day I first remove the arrival from w days ago (only if it was kept), then discard today’s arrival if its type already has m, otherwise keep it. Time O(n).

Related

LeetCode 2841: Maximum Sum of Almost Unique Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2841 note: a fixed window of length k keeps a sum and a count map; the number of distinct values is the map’s size, so delete keys that drop to zero
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 1814: Count Nice Pairs in an Array
·1 min
LeetCode Medium Array Hash-Table Math Counting
LeetCode 1814 note: rearrange the condition into nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]), count pairs per value, and remember the modulus
LeetCode 3071: Minimum Operations to Write the Letter Y on a Grid
·2 mins
LeetCode Medium Array Hash-Table Matrix Counting
LeetCode 3071 note: count each value on and off the Y, enumerate the Y value a and background value b (a != b), and take the fewest cells to change
LeetCode 229: Majority Element II
·2 mins
LeetCode Medium Array Hash-Table Sorting Counting
LeetCode 229 note: at most two values can exceed n / 3; Boyer-Moore keeps two candidates and a second pass verifies their counts
LeetCode 2461: Maximum Sum of Distinct Subarrays With Length K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2461 note: in a fixed window of length k, all values are distinct exactly when the number of distinct values equals k