Skip to content

Commit 8e079a7

Browse files
committed
Add solution #1004
1 parent b3b0983 commit 8e079a7

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
994|[Rotting Oranges](./0994-rotting-oranges.js)|Medium|
271271
997|[Find the Town Judge](./0997-find-the-town-judge.js)|Easy|
272272
1002|[Find Common Characters](./1002-find-common-characters.js)|Easy|
273+
1004|[Max Consecutive Ones III](./1004-max-consecutive-ones-iii.js)|Medium|
273274
1009|[Complement of Base 10 Integer](./1009-complement-of-base-10-integer.js)|Easy|
274275
1010|[Pairs of Songs With Total Durations Divisible by 60](./1010-pairs-of-songs-with-total-durations-divisible-by-60.js)|Medium|
275276
1022|[Sum of Root To Leaf Binary Numbers](./1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 1004. Max Consecutive Ones III
3+
* https://leetcode.com/problems/max-consecutive-ones-iii/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary array nums and an integer k, return the maximum number of consecutive 1's
7+
* in the array if you can flip at most k 0's.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @param {number} k
13+
* @return {number}
14+
*/
15+
var longestOnes = function(nums, k) {
16+
let left = 0;
17+
let right = 0;
18+
19+
while (right < nums.length) {
20+
if (!nums[right]) k--;
21+
if (k < 0) {
22+
if (!nums[left]) k++;
23+
left++;
24+
}
25+
right++;
26+
}
27+
28+
return right - left;
29+
};

0 commit comments

Comments
 (0)