Succinct Java Solution - Beat 100%


  • 0
    G
    public class Solution {
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            
            boolean left, right;
            
            for (int i = 0; i < flowerbed.length; i++) {
                if (flowerbed[i] == 1) continue;
                
                left = (i == 0 || flowerbed[i-1] == 0)? true : false;
                right = (i == flowerbed.length - 1 || flowerbed[i+1] == 0)? true : false;
                
                if (left && right) {
                    n--;
                    flowerbed[i] = 1;
                }
            }
            
            return n <= 0;
        }
    }
    

Log in to reply
 

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