2024/10/31 灵茶题单 滑动窗口 求子数组个数
This commit is contained in:
parent
127d23b479
commit
c6efbd9789
|
@ -0,0 +1,40 @@
|
||||||
|
package com.cool.ling_cha_mount.sliding_windows;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/10/31/16:42
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
public class Num2962 {
|
||||||
|
public long countSubarrays(int[] nums, int k) {
|
||||||
|
int left = 0;
|
||||||
|
long res = 0;
|
||||||
|
int max=nums[0];
|
||||||
|
int maxNum=0;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
if(nums[i]>max){
|
||||||
|
maxNum=1;
|
||||||
|
res=0;
|
||||||
|
left=0;
|
||||||
|
max=nums[i];
|
||||||
|
}else if(nums[i]==max){
|
||||||
|
maxNum++;
|
||||||
|
}
|
||||||
|
while(maxNum>=k){
|
||||||
|
res+=nums.length-i;
|
||||||
|
if(nums[left++]==max){
|
||||||
|
maxNum--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
System.out.println(countSubarrays(new int[]{61, 23, 38, 23, 56, 40, 82, 56, 82, 82, 82, 70, 8, 69, 8, 7, 19, 14, 58, 42, 82, 10, 82, 78, 15, 82}, 2));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue