Skip to content

Commit 437d293

Browse files
Added Max Consecutive Ones
1 parent 13cd84f commit 437d293

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Max Consecutive Ones III
3+
https://leetcode.com/problems/max-consecutive-ones-iii
4+
5+
Given a binary array nums and an integer k, return the maximum number of consecutive 1's
6+
in the array if you can flip at most k 0's.
7+
8+
Example 1:
9+
10+
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
11+
Output: 6
12+
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
13+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
14+
Example 2:
15+
16+
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
17+
Output: 10
18+
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
19+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
20+
*/
21+
22+
var longestOnes = function(nums, k) {
23+
let start = 0;
24+
let end = 0;
25+
let maxWindow = 0;
26+
while(start < nums.length && end < nums.length) {
27+
if(k > 0 || nums[end] == 1) {
28+
if(nums[end] == 0) { k--; }
29+
maxWindow = Math.max(maxWindow, end - start + 1);
30+
end++;
31+
} else { // k = 0 and nums[end] == 0
32+
while(k == 0 && start < nums.length) {
33+
if(nums[start] == 0) {
34+
k++;
35+
}
36+
start++;
37+
}
38+
}
39+
}
40+
41+
return maxWindow;
42+
};
43+
44+
module.exports.longestOnes = longestOnes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const assert = require('assert');
2+
const longestOnes = require('../../LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III').longestOnes;
3+
4+
function test() {
5+
assert.equal(1, longestOnes([1], 1));
6+
assert.equal(1, longestOnes([0], 1));
7+
assert.equal(0, longestOnes([0], 0));
8+
assert.equal(6, longestOnes([1,1,1,0,0,0,1,1,1,1,0], 2));
9+
assert.equal(10, longestOnes([0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], 3));
10+
}
11+
12+
module.exports.test = test;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const assert = require('assert');
2+
const minSubArrayLength = require('../../LeetcodeProblems/Algorithms/Minimum_Size_Subarray').minSubArrayLength;
3+
4+
function test() {
5+
assert.equal(0, minSubArrayLength(10, []));
6+
assert.equal(0, minSubArrayLength(10, [4,1,2]));
7+
assert.equal(2, minSubArrayLength(4, [1,1,1,1, 3, 1]));
8+
assert.equal(1, minSubArrayLength(11, [5, 6, 12, 3, 1]));
9+
assert.equal(5, minSubArrayLength(5, [1,1,1,1,1]));
10+
assert.equal(2, minSubArrayLength(7, [2,3,1,2,4,3]));
11+
assert.equal(1, minSubArrayLength(4, [1,4,4]));
12+
assert.equal(0, minSubArrayLength(11, [1,1,1,1,1,1,1,1]));
13+
}
14+
15+
module.exports.test = test;

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
3838
| [Longest Palindromic Substring ](/LeetcodeProblems/Algorithms/Longest_Palindromic_Substring.js) | Medium | https://leetcode.com/problems/longest-palindromic-substring/|
3939
| [Longest Substring Without Reapeating Characters](/LeetcodeProblems/Algorithms/Longest_Substring.js) | Medium | https://leetcode.com/problems/longest-substring-without-repeating-characters|
4040
| [Max Area Of Island ](/LeetcodeProblems/Algorithms/Max_Area_Of_Island.js) | Medium | https://leetcode.com/problems/max-area-of-island/ |
41+
| [Max Consecutive Ones III ](/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js) | Medium | https://leetcode.com/problems/max-consecutive-ones-iii |
4142
| [Maximal Square ](/LeetcodeProblems/Algorithms/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/ |
43+
| [Minimum Size Subarray](/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js) | Medium | https://leetcode.com/problems/minimum-size-subarray-sum |
4244
| [Permutations ](/LeetcodeProblems/Algorithms/Permutations.js) | Medium | https://leetcode.com/problems/permutations/ |
4345
| [Permutations II ](/LeetcodeProblems/Algorithms/Permutations_II.js) | Medium | https://leetcode.com/problems/permutations-ii/ |
4446
| [Permutations Without Duplicates ](/LeetcodeProblems/Algorithms/Permutations_Without_Duplicates.js) | Medium | https://leetcode.com/problems/permutations/ |

0 commit comments

Comments
 (0)