File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 270
270
994|[ Rotting Oranges] ( ./0994-rotting-oranges.js ) |Medium|
271
271
997|[ Find the Town Judge] ( ./0997-find-the-town-judge.js ) |Easy|
272
272
1002|[ Find Common Characters] ( ./1002-find-common-characters.js ) |Easy|
273
+ 1004|[ Max Consecutive Ones III] ( ./1004-max-consecutive-ones-iii.js ) |Medium|
273
274
1009|[ Complement of Base 10 Integer] ( ./1009-complement-of-base-10-integer.js ) |Easy|
274
275
1010|[ Pairs of Songs With Total Durations Divisible by 60] ( ./1010-pairs-of-songs-with-total-durations-divisible-by-60.js ) |Medium|
275
276
1022|[ Sum of Root To Leaf Binary Numbers] ( ./1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments