-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxConsecutiveOnesIII.java
More file actions
113 lines (92 loc) · 2.69 KB
/
MaxConsecutiveOnesIII.java
File metadata and controls
113 lines (92 loc) · 2.69 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package Algorithms.SlidingWindow;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 12 April 2025
*/
public class MaxConsecutiveOnesIII {
public static void main(String[] args) {
int[] nums = {1,1,0,0,1,1,1,0,1,1};
int k = 2;
System.out.println("maxConsecutiveOnes : " + longestOnes(nums, k));
}
public static int longestOnes(int[] nums, int k) {
int left = 0, right = 0, max = 0, count = 0;
while (right < nums.length) {
if (nums[right] == 0) count++;
right++;
while (count > k) {
if (nums[left] == 0) count--;
left++;
}
max = Math.max(max, right - left);
}
return max;
}
/**
* NOT WORKING AS EXPECTED. NEED TO RESEARCH MORE ON THIS.
*
* Here I calculated the window that has max 1s
* And then expand the window to left and right until k becomes 0
*
* but
*
* [1,1,1,0,1,1,1,0,0,1,1,1,1] and k=1
*
* [1,1,1,0,1,1,1,0,0,1,1,1,1] ---> 5
* ^ ^
* this method will give wrong above output
*
* But the right ans is
* [1,1,1,0,1,1,1,0,0,1,1,1,1] ---> 7
* ^ ^
*
*
*
* And similarly,
* [1,1,0,0,1,1,1,0,1,1] and k=1
*
* if you fill left sides first then you'll get 4
* but if you fill right side first then you'll get 6
*
* We need to check which side is better to fill first
*/
public int longestOnesMyApproachOld(int[] nums, int k) {
int n = nums.length;
if(n==1) return (nums[0]==1)? 1 : (k>0? 1: 0);
int maxL=0, maxR=0;
int l=0, r=1;
while(r<n) {
System.out.printf("l:%s, r:%s, maxL:%s, maxR:%s\n", l, r, maxL, maxR);
if(nums[r]==1){
if(maxR-maxL < r-l) {
maxR=r;
maxL=l;
}
r++;
}
else {
r++;
l = r;
}
}
while(maxL-1>0 && nums[maxL-1]==0 && k>0) {
maxL--;
k--;
System.out.printf("maxL:%s, maxR:%s\n", maxL, maxR);
}
while(maxL-1>0 && nums[maxL-1]==1){
maxL--;
System.out.printf("maxL:%s, maxR:%s\n", maxL, maxR);
}
while(maxR+1<n && nums[maxR+1]==0 && k>0) {
maxR++;
k--;
System.out.printf("maxL:%s, maxR:%s\n", maxL, maxR);
}
while(maxR+1<n && nums[maxR+1]==1) {
maxR++;
System.out.printf("maxL:%s, maxR:%s\n", maxL, maxR);
}
return maxR-maxL+1;
}
}