2024/10/13 灵茶题单 滑动窗口

This commit is contained in:
Cool 2024-10-13 20:00:07 +08:00
parent 2b77624088
commit 266e67f8be
1 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.cool.ling_cha_mount.sliding_windows;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/10/13/19:59
* @Description: 2653. 滑动子数组的美丽值
* Hard 2
* Level 5
* Score 1786
*/
public class Num2653 {
public int[] getSubarrayBeauty(int[] nums, int k, int x) {
int[] count = new int[101];
int[] res = new int[nums.length - k + 1];
for (int i = 0; i < k - 1; i++) {
count[nums[i] + 50]++;
}
for (int i = k - 1; i < nums.length; i++) {
count[nums[i] + 50]++;
int val = x;
for (int j = 0; j < 50; j++) {
val -= count[j];
if (val <= 0) {
res[i - k + 1] = j - 50;
break;
}
}
count[nums[i - k + 1] + 50]--;
}
return res;
}
}