3679. Minimum Discards to Balance Inventory Medium
1/**
2 * [3679] Minimum Discards to Balance Inventory
3 *
4 * 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).
5 * Items are managed according to the following rules:
6 *
7 * Each arrival may be kept or discarded; an item may only be discarded on its arrival day.
8 * For each day i, consider the window of days [max(1, i - w + 1), i] (the w most recent days up to day i):
9 *
10 * For any such window, each item type may appear at most m times among kept arrivals whose arrival day lies in that window.
11 * 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.
12 *
13 *
14 *
15 * Return the minimum number of arrivals to be discarded so that every w-day window contains at most m occurrences of each type.
16 *
17 * <strong class="example">Example 1:
18 * <div class="example-block">
19 * Input: <span class="example-io">arrivals = [1,2,1,3,1], w = 4, m = 2</span>
20 * Output: <span class="example-io">0</span>
21 * Explanation:
22 *
23 * On day 1, Item 1 arrives; the window contains no more than m occurrences of this type, so we keep it.
24 * On day 2, Item 2 arrives; the window of days 1 - 2 is fine.
25 * On day 3, Item 1 arrives, window [1, 2, 1] has item 1 twice, within limit.
26 * On day 4, Item 3 arrives, window [1, 2, 1, 3] has item 1 twice, allowed.
27 * On day 5, Item 1 arrives, window [2, 1, 3, 1] has item 1 twice, still valid.
28 *
29 * There are no discarded items, so return 0.
30 * </div>
31 * <strong class="example">Example 2:
32 * <div class="example-block">
33 * Input: <span class="example-io">arrivals = [1,2,3,3,3,4], w = 3, m = 2</span>
34 * Output: <span class="example-io">1</span>
35 * Explanation:
36 *
37 * On day 1, Item 1 arrives. We keep it.
38 * On day 2, Item 2 arrives, window [1, 2] is fine.
39 * On day 3, Item 3 arrives, window [1, 2, 3] has item 3 once.
40 * On day 4, Item 3 arrives, window [2, 3, 3] has item 3 twice, allowed.
41 * On day 5, Item 3 arrives, window [3, 3, 3] has item 3 three times, exceeds limit, so the arrival must be discarded.
42 * On day 6, Item 4 arrives, window [3, 4] is fine.
43 *
44 * Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.
45 * </div>
46 *
47 * Constraints:
48 *
49 * 1 <= arrivals.length <= 10^5
50 * 1 <= arrivals[i] <= 10^5
51 * 1 <= w <= arrivals.length
52 * 1 <= m <= w
53 *
54 */
55pub struct Solution {}
56
57// problem: https://leetcode.com/problems/minimum-discards-to-balance-inventory/
58// discuss: https://leetcode.com/problems/minimum-discards-to-balance-inventory/discuss/?currentPage=1&orderBy=most_votes&query=
59
60// submission codes start here
61
62impl Solution {
63 pub fn min_arrivals_to_discard(arrivals: Vec<i32>, w: i32, m: i32) -> i32 {
64 0
65 }
66}
67
68// submission codes end
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_3679() {
76 }
77}
78Back
© 2026 bowen.ge All Rights Reserved.