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 an integer array nums and an integer k.
Return the minimum length of a subarray 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.
Example 1:
Input: nums = [2,2,3,1], k = 4
Output: 2
Explanation:
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.
Example 2:
Input: nums = [3,2,3,4], k = 5
Output: 2
Explanation:
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.
Example 3:
Input: nums = [5,5,4], k = 5
Output: 1
Explanation:
The subarray [5] has distinct elements {5} whose sum is 5, which is at least k = 5. Thus, the answer is 1.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1051 <= k <= 109Problem summary: You are given an integer array nums and an integer k. Return the minimum length of a subarray 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Sliding Window
[2,2,3,1] 4
[3,2,3,4] 5
[5,5,4] 5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minLength(nums []int, k int) int {
// cnt := map[int]int{}
// sum := 0
// left := 0
// ans := math.MaxInt
//
// for i, x := range nums {
// // 1. 入
// cnt[x]++
// if cnt[x] == 1 {
// sum += x
// }
//
// for sum >= k {
// // 2. 更新答案
// ans = min(ans, i-left+1)
//
// // 3. 出
// out := nums[left]
// cnt[out]--
// if cnt[out] == 0 {
// sum -= out
// }
// left++
// }
// }
//
// if ans == math.MaxInt {
// return -1
// }
// return ans
// }
// Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
package main
import "math"
// https://space.bilibili.com/206214
func minLength(nums []int, k int) int {
cnt := map[int]int{}
sum := 0
left := 0
ans := math.MaxInt
for i, x := range nums {
// 1. 入
cnt[x]++
if cnt[x] == 1 {
sum += x
}
for sum >= k {
// 2. 更新答案
ans = min(ans, i-left+1)
// 3. 出
out := nums[left]
cnt[out]--
if cnt[out] == 0 {
sum -= out
}
left++
}
}
if ans == math.MaxInt {
return -1
}
return ans
}
# Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
# Time: O(n)
# Space: O(n)
import collections
# freq table, two pointers
class Solution(object):
def minLength(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
INF = float("inf")
cnt = collections.defaultdict(int)
result = INF
left = curr = 0
for right in xrange(len(nums)):
cnt[nums[right]] += 1
if cnt[nums[right]] == 1:
curr += nums[right]
while curr >= k:
result = min(result, right-left+1)
if cnt[nums[left]] == 1:
curr -= nums[left]
cnt[nums[left]] -= 1
left += 1
return result if result is not INF else -1
// Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
// Rust example auto-generated from go 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 (go):
// // Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minLength(nums []int, k int) int {
// cnt := map[int]int{}
// sum := 0
// left := 0
// ans := math.MaxInt
//
// for i, x := range nums {
// // 1. 入
// cnt[x]++
// if cnt[x] == 1 {
// sum += x
// }
//
// for sum >= k {
// // 2. 更新答案
// ans = min(ans, i-left+1)
//
// // 3. 出
// out := nums[left]
// cnt[out]--
// if cnt[out] == 0 {
// sum -= out
// }
// left++
// }
// }
//
// if ans == math.MaxInt {
// return -1
// }
// return ans
// }
// Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3795: Minimum Subarray Length With Distinct Sum At Least K
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minLength(nums []int, k int) int {
// cnt := map[int]int{}
// sum := 0
// left := 0
// ans := math.MaxInt
//
// for i, x := range nums {
// // 1. 入
// cnt[x]++
// if cnt[x] == 1 {
// sum += x
// }
//
// for sum >= k {
// // 2. 更新答案
// ans = min(ans, i-left+1)
//
// // 3. 出
// out := nums[left]
// cnt[out]--
// if cnt[out] == 0 {
// sum -= out
// }
// left++
// }
// }
//
// if ans == math.MaxInt {
// return -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.