Skip to content

Instantly share code, notes, and snippets.

@javiermendozain
Created July 31, 2020 19:08
Show Gist options
  • Save javiermendozain/e581e65ef9118c8062e716097a2369fc to your computer and use it in GitHub Desktop.
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.
/**
* @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