Java Easy Solution


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

Log in to reply
 

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