Created
July 31, 2020 19:08
-
-
Save javiermendozain/e581e65ef9118c8062e716097a2369fc to your computer and use it in GitHub Desktop.
Solution of LeetCode, Max Consecutive Ones Solution: given a binary array, find the maximum number of consecutive 1s in this array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var findMaxConsecutiveOnes = function(nums) { | |
let count = 0 ; | |
let accum = 0; | |
for(const num of nums) { | |
if (num === 1) { | |
count+=1 | |
} else if ( count > accum) { | |
accum = count; | |
count = 0; | |
} else { | |
count = 0 | |
} | |
} | |
return count > accum ? count : accum | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment