LeetCode 2653. 滑动子数组的美丽值
原题链接
中等
作者:
autumn_0
,
2024-10-08 09:01:34
,
所有人可见
,
阅读 5
class Solution {
public int[] getSubarrayBeauty(int[] nums, int k, int x) {
final int BIAS = 50;
var cnt = new int[BIAS * 2 + 1];
int n = nums.length;
for(int i = 0; i < k - 1; i ++ ){
cnt[nums[i] + BIAS] ++ ;
}
var ans = new int[n - k + 1];
for(int i = k - 1; i < n; i ++ ){
cnt[nums[i] + BIAS] ++ ;
int left = x;
for(int j = 0; j < BIAS; j ++ ){
left -= cnt[j];
if(left <= 0){
ans[i - k + 1] = j - BIAS;
break;
}
}
cnt[nums[i - k + 1] + BIAS] -- ;
}
return ans;
}
}