Java - Neat Greedy - Beats 100% too


  • 0
    W
    public class Solution {
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            for (int i = 0; i < flowerbed.length; i++) {
                if (flowerbed[i] == 0 && 
                     (i == 0 || flowerbed[i-1] == 0) &&
                     (i == flowerbed.length-1 || flowerbed[i+1] == 0)) {
                    n--;
                    i++;
                } 
            }
            return n <= 0;
        }
    }
    

Log in to reply
 

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