-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_1004.java
More file actions
35 lines (34 loc) · 1.09 KB
/
Copy pathproblem_1004.java
File metadata and controls
35 lines (34 loc) · 1.09 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
/*
1004. Max Consecutive Ones III
Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's. */
class Solution {
public int longestOnes(int[] nums, int k) {
int count_zero = 0, max = 0, count_max = 0, remaining_length = nums.length;
for(int i = 0;i<nums.length;i++,remaining_length--){
for(int j = i;j<nums.length;j++){
if(nums[j] == 0 && count_zero<k){
count_max++;
count_zero++;
}
else if(nums[j] == 1){
count_max++;
}
else{
if(max < count_max){
max = count_max;
}
count_zero = 0;
count_max = 0;
break;
}
if(max < count_max){
max = count_max;
}
}
if(max >= remaining_length){
break;
}
}
return max;
}
}