Easy to understand Java Solution


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

Log in to reply
 

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