2024/10/17 灵茶题单 不定长滑动窗口
This commit is contained in:
parent
9c977b3197
commit
ff4ce5ceab
|
@ -0,0 +1,34 @@
|
||||||
|
package com.cool.ling_cha_mount.sliding_windows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/10/17/13:46
|
||||||
|
* @Description: 1493. 删掉一个元素以后全为 1 的最长子数组
|
||||||
|
* Hard 2
|
||||||
|
* Level 6
|
||||||
|
* Score 1423
|
||||||
|
*/
|
||||||
|
public class Num1493 {
|
||||||
|
public int longestSubarray(int[] nums) {
|
||||||
|
int left=0;
|
||||||
|
int num=0;
|
||||||
|
int max=0;
|
||||||
|
for(int i=0;i<nums.length;i++){
|
||||||
|
if(nums[i]!=1){
|
||||||
|
num++;
|
||||||
|
if(num>1){
|
||||||
|
max=Math.max(i-left,max);
|
||||||
|
}
|
||||||
|
while(num>1){
|
||||||
|
if(nums[left++]!=1){
|
||||||
|
num--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
max=Math.max(max,nums.length-left);
|
||||||
|
return max-1;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue