2024/10/13 灵茶题单 滑动窗口
This commit is contained in:
parent
2b77624088
commit
266e67f8be
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue