Javascript Solution


  • 1
    B
    var str = nums.join("").split("0").map(function(value){
        return value.length;
    });
    return Math.max.apply(null, str);

  • 0
    E

    Somehow this solution turns out very slow


  • 0
    T

    I did the same solution, without the apply, and making sure we return 0 if there is no 1s in the binary array, and I'm faster than 93% of the solutions:

    var findMaxConsecutiveOnes = function(nums) {
        const lengths = nums.join('').split('0').map(group => group.length)
        return Math.max(0, ...lengths)
    };
    

  • 0
    S
    var findMaxConsecutiveOnes = function(nums) {
        let count = 0
        let maxCountTillNow = 0
        nums.map((num) => {
            if(num == 1) {
                count++
            } else {
                maxCountTillNow = Math.max(count, maxCountTillNow)
                count = 0
            }
        })
        return Math.max(count, maxCountTillNow)
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.