Java - o(n)


  • 0
    O
    public class Solution {
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
    
            int currZeros = 1;
            int maxN = 0;
            for (int i = 0; i < flowerbed.length; i++) {
                if (flowerbed[i] == 0) currZeros++;
                else {
                    maxN += (currZeros-1)/2;
                    currZeros = 0;
                }
            }
            currZeros++;
            maxN += (currZeros-1)/2;
            return maxN >= n;
        }
    }
    

Log in to reply
 

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