-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_Consecutive_Ones_III.java
More file actions
55 lines (51 loc) · 1.36 KB
/
Max_Consecutive_Ones_III.java
File metadata and controls
55 lines (51 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.leet_code;
import java.util.PriorityQueue;
import java.util.Queue;
public class Max_Consecutive_Ones_III {
public static void main(String[] args) {
int[] arr={0,0,0,1};
int k=4;
System.out.println(longestOnes(arr,k));
}
public static int longestOnes(int[] nums, int k) {
Queue<Integer> queue=new PriorityQueue<>();
int st=0;
int en=0;
int total=0;
int count=0;
while (st<nums.length) {// [1,1,1,0,0,0,1,1,1,1,0] passed 40 test cases
if (nums[st]==0) {
total++;
queue.add(st);
count=Math.max(count,(st-en)-1);
if (total > k) {
en=queue.remove()+1;
total--;
}
}
st++;
}
if (count==0){
return 0;
}
return count+1;
//--------------------------------------------------------
// int start=0;
// int end=0;
// int zeros=0;
//
// while(end<nums.length){
// if(nums[end] == 0){
// zeros++;
// }
// end++;
// if(zeros>k){
// if(nums[start] == 0){
// zeros--;
// }
// start++;
// }
// }
// return end-start;
}
}