-
Notifications
You must be signed in to change notification settings - Fork 1
/
1004. Max Consecutive Ones III
72 lines (67 loc) · 1.76 KB
/
1004. Max Consecutive Ones III
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
// naive approach(it will give tle)
class Solution {
public boolean helper(ArrayList<Integer> list, int k) {
for(int i = 0; i < list.size(); i++) {
if(list.get(i) == 0) {
k--;
}
}
if(k < 0) {
return false;
}
return true;
}
public int longestOnes(int[] nums, int k) {
int maxi = Integer.MIN_VALUE;
for(int i = 0; i < nums.length; i++) {
ArrayList<Integer> list = new ArrayList<>();
for(int j = i; j < nums.length; j++) {
list.add(nums[j]);
if(helper(list, k)) {
maxi = Math.max(maxi, list.size());
}
}
}
if(maxi == Integer.MIN_VALUE) return 0;
return maxi;
}
}
// binary search approach -> tc - O(nlogn) sc -> O(1)
class Solution {
public boolean helper(int[] nums, int k, int mid) {
int sum = 0;
for(int i = 0; i < mid; i++) {
sum += nums[i];
}
if((mid - sum) <= k) {
return true;
}
for(int i = mid; i < nums.length; i++) {
sum -= nums[i - mid];
sum += nums[i];
if((mid - sum) <= k) {
return true;
}
}
return false;
}
public int binarySearch(int[] nums, int k) {
int start = 0;
int end = nums.length;
int maxi = Integer.MIN_VALUE;
while (start <= end) {
int mid = (start + end) / 2;
if(helper(nums, k, mid)) {
maxi = Math.max(maxi, mid);
start = mid + 1;
}
else {
end = mid - 1;
}
}
return maxi;
}
public int longestOnes(int[] nums, int k) {
return binarySearch(nums, k);
}
}