3795. Minimum Subarray Length With Distinct Sum At Least K Medium
1/**
2 * [3795] Minimum Subarray Length With Distinct Sum At Least K
3 *
4 * You are given an integer array nums and an integer k.
5 * Return the minimum length of a <span data-keyword="subarray-nonempty">subarray</span> whose sum of the distinct values present in that subarray (each value counted once) is at least k. If no such subarray exists, return -1.
6 *
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">nums = [2,2,3,1], k = 4</span>
10 * Output: <span class="example-io">2</span>
11 * Explanation:
12 * The subarray [2, 3] has distinct elements {2, 3} whose sum is 2 + 3 = 5, which is at least k = 4. Thus, the answer is 2.
13 * </div>
14 * <strong class="example">Example 2:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [3,2,3,4], k = 5</span>
17 * Output: <span class="example-io">2</span>
18 * Explanation:
19 * The subarray [3, 2] has distinct elements {3, 2} whose sum is 3 + 2 = 5, which is at least k = 5. Thus, the answer is 2.
20 * </div>
21 * <strong class="example">Example 3:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [5,5,4], k = 5</span>
24 * Output: <span class="example-io">1</span>
25 * Explanation:
26 * The subarray [5] has distinct elements {5} whose sum is 5, which is at least k = 5. Thus, the answer is 1.
27 * </div>
28 *
29 * Constraints:
30 *
31 * 1 <= nums.length <= 10^5
32 * 1 <= nums[i] <= 10^5
33 * 1 <= k <= 10^9
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/minimum-subarray-length-with-distinct-sum-at-least-k/
39// discuss: https://leetcode.com/problems/minimum-subarray-length-with-distinct-sum-at-least-k/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn min_length(nums: Vec<i32>, k: i32) -> i32 {
45 0
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_3795() {
57 }
58}
59Back
© 2026 bowen.ge All Rights Reserved.