Java - Greed Solution - Simple to understand - beats 100%


  • 0
    L
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        for(int i = 0; i < flowerbed.length; ++i){
        	if(flowerbed[i] == 1) continue;
        	if(i - 1 >= 0 && flowerbed[i - 1] == 1) continue;
        	if(i + 1 < flowerbed.length && flowerbed[i + 1] == 1) continue;
        	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.