↓Skip to main content
  1. LeetCode/

LeetCode 2653: Sliding Subarray Beauty

·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: Accepted (first try)

Approach
#

Each length-k window needs its x-th smallest value if it is negative, otherwise 0.

A general “x-th smallest in a window” needs an ordered structure, but here values are only -50..50, so a count array of 101 slots is enough:

  • As the window moves, the incoming value +1 and the outgoing value -1.
  • For each window, accumulate negative counts upward from -50; the value where the total reaches x is the answer, or 0 if there are fewer than x negatives.

Each window scans at most 50 slots: O(50n) overall.

My Solution
#

class Solution:
    def getSubarrayBeauty(self, nums: List[int], k: int, x: int) -> List[int]:
        cnt = [0] * 101                            # value v stored at cnt[v + 50]
        res = []
        for right, v in enumerate(nums):
            cnt[v + 50] += 1
            if right >= k:
                cnt[nums[right - k] + 50] -= 1
            if right >= k - 1:
                seen = 0
                for idx in range(50):              # negatives only, -50..-1
                    seen += cnt[idx]
                    if seen >= x:
                        res.append(idx - 50)
                        break
                else:
                    res.append(0)
        return res

My original used a 50-slot array indexed directly by the negative v (neg_array[v]), relying on Python’s negative indexing from the end so that -50 maps to index 0 and index i means value i - 50. It works but is hard to read, so this version uses an explicit + 50 offset.

Complexity
#

Time O(n · 50), space O(1) (101 slots).

Mistakes to Avoid
#

  • Sorting every window: O(n · k log k).
  • Return 0 when there are fewer than x negatives.

Interview Explanation
#

Values range only from -50 to 50, so a count array maintains the fixed window, +1 in and -1 out. For each window I accumulate negative counts from -50 upward; reaching x gives the x-th smallest negative, otherwise 0. Each window checks at most 50 slots: O(50n).

Related

LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 note: a shortest-window slide where each value counts once; track each value’s last position, and subtract the left value only if it was the last occurrence
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 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 2134: Minimum Swaps to Group All 1's Together II
·2 mins
LeetCode Medium Sliding-Window Array
LeetCode 2134 note: the 1s end up in a circular window as long as their count, and the swaps needed equal the 0s in that window; slide around the circle for the minimum