Skip to content

Commit ec3fca7

Browse files
Merge pull request #27 from Rajkushwaha0/Max-Ones
Max Consecutive Ones III
2 parents 6985806 + 5455c2d commit ec3fca7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

1004. Max Consecutive Ones III

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Link: https://leetcode.com/problems/max-consecutive-ones-iii/
2+
3+
//JAVA Solution
4+
//O(n) Optimize
5+
class Solution {
6+
public int longestOnes(int[] nums, int k) {
7+
int ws = 0 , max = 0 , count = 0;
8+
HashMap<Integer,Integer> hm = new HashMap<>();
9+
for(int we = 0 ; we < nums.length ; we++){
10+
// if(nums[we] == 0) count++;
11+
if(nums[we]==0) hm.put(nums[we],hm.getOrDefault(nums[we],0)+1);
12+
while(nums[we]==0 && hm.get(nums[we]) > k){
13+
if(nums[ws] == 0){
14+
hm.put(nums[ws],hm.get(nums[ws])-1);
15+
}
16+
ws++;
17+
}
18+
max=Math.max(max,we-ws+1);
19+
}
20+
return max;
21+
}
22+
}

0 commit comments

Comments
 (0)