Simple javascript solution and easy to understand.


  • 0
    L

    when find a 0 just check it's left and right
    (that not 1)

    var canPlaceFlowers = function(flowerbed, n) {
        var count=0;
        for(let i=0;i<flowerbed.length;i++){
            if(count>=n)return true;
            if(flowerbed[i]===0&&flowerbed[i+1]!==1&&flowerbed[i-1]!==1){
               count++;
               i++;
            }
        }
        //console.log(count);
        return count>=n;
    };
    

Log in to reply
 

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