---
# System prepended metadata

title: 2653. Sliding Subarray Beauty
tags: [Leetcode, Sliding Window, Medium]

---

# 2653. Sliding Subarray Beauty
###### tags: `Leetcode` `Medium` `Sliding Window`
Link: https://leetcode.com/problems/sliding-subarray-beauty/description/
## 思路 $O(50N)$
做题记得看完constraints
由于nums里面所有数字都在-50和50之间 因此只需要一个array记录window里面每个数字出现的频率即可
## Code
```python=
class Solution:
    def getSubarrayBeauty(self, nums: List[int], k: int, x: int) -> List[int]:
        counter = [0]*50
        n = len(nums)
        ans = [0]*(n-k+1)
        for i in range(n):
            if nums[i]<0: counter[nums[i]+50] += 1
            if i-k>=0 and nums[i-k]<0: counter[nums[i-k]+50] -= 1
            if i-k+1<0: continue
            count = 0
            for j in range(50):
                count += counter[j]
                if count>=x:
                    ans[i-k+1] = j-50
                    break
        return ans
```