↓Skip to main content
  1. LeetCode/

LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K

·2 mins· ·
LeetCode Medium Sliding-Window Hash-Table 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: 2026-01-03

  • Submissions: Wrong Answer → Accepted

Approach
#

All values are positive, so the sum of distinct values never decreases as the window grows, which allows the “shortest” variable window: update the answer while valid, then try shrinking the left.

The catch is that a value appearing several times in the window counts once:

  • last[x] records the last position of value x in the window. When adding x, add it to the sum only if x is not in last.
  • When nums[left] leaves, subtract and delete it only if last[nums[left]] == left (it was the last copy in the window); otherwise another copy remains on the right and the sum is unchanged.

My Attempts
#

Attempt 1 used a plain sum without handling duplicates. For nums = [4, 4], k = 7, the distinct sum is at most 4, so the answer is -1; adding both 4s gave 8 and it returned 2.

Solution
#

class Solution:
    def minLength(self, nums: List[int], k: int) -> int:
        last = {}                                  # value -> last position in the window
        total = left = 0
        best = float("inf")
        for right, x in enumerate(nums):
            if x not in last:
                total += x
            last[x] = right
            while total >= k:
                best = min(best, right - left + 1)
                if last[nums[left]] == left:       # no other copy of nums[left] in the window
                    total -= nums[left]
                    del last[nums[left]]
                left += 1
        return best if best < float("inf") else -1

Complexity
#

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

Mistakes to Avoid
#

  • A plain sum counts duplicates many times.
  • When the left value leaves, another copy may remain on the right, so it cannot always be subtracted.

Interview Explanation
#

Values are positive, so a larger window has a larger distinct sum, which fits a shortest-window slide. Duplicates count once: I track each value’s last position in the window, adding a value to the sum only on its first appearance, and when the left value leaves I subtract it only if it was the last copy in the window. Time O(n).

Related

LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave
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 2653: Sliding Subarray Beauty
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 note: values are only -50 to 50, so the fixed window keeps a count array, and each window accumulates upward from -50 to find the x-th smallest negative
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
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set