package leetcode;

/**
 * https://leetcode.com/problems/can-place-flowers/
 */
public class Problem605 {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int idx = 0;
        int flowers = 0;
        boolean foundOne = false;
        for (int i = 0; i < flowerbed.length; i++) {
            if (flowerbed[i] == 1) {
                foundOne = true;
                int zeros = i - idx;
                if (flowerbed[idx] == 1) {
                    zeros--;
                }
                if (zeros >= 2) {
                    if (flowerbed[idx] == 1) {
                        flowers += (zeros - 1) / 2;
                    } else {
                        flowers += zeros / 2;
                    }
                }
                idx = i;
            }
        }
        if (!foundOne) {
            flowers += (flowerbed.length + 1) / 2;
        } else {
            int zeros = flowerbed.length - idx;
            if (flowerbed[idx] == 1) {
                zeros--;
            }
            if (zeros >= 2) {
                flowers += zeros / 2;
            }
        }
        return flowers >= n;
    }

    public static void main(String[] args) {
        Problem605 prob = new Problem605();
        System.out.println(prob.canPlaceFlowers(new int[]{1, 0, 0, 0, 1}, 1)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{1, 0, 0, 0, 1}, 2)); // false
        System.out.println(prob.canPlaceFlowers(new int[]{1, 0, 1, 0, 1}, 2)); // false
        System.out.println(prob.canPlaceFlowers(new int[]{1, 0, 1, 0, 1}, 3)); // false
        System.out.println(prob.canPlaceFlowers(new int[]{0, 0, 0, 0, 0, 0, 1}, 3)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{0, 0, 0, 1, 0, 0, 1}, 1)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{0, 0, 0, 1, 0, 0, 1}, 2)); // false
        System.out.println(prob.canPlaceFlowers(new int[]{1, 0, 0, 0, 1, 0, 0}, 2)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{0}, 1)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{0, 0}, 1)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{1}, 1)); // false
        System.out.println(prob.canPlaceFlowers(new int[]{1}, 0)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{1, 0}, 1)); // false
        System.out.println(prob.canPlaceFlowers(new int[]{0, 0, 0}, 2)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{0, 0, 0, 0}, 2)); // true
        System.out.println(prob.canPlaceFlowers(new int[]{0, 0, 0, 0}, 3)); // false
    }
}

@@ -1135,1460 +1135,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem605 prob = new Problem605();%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1, 0, 0, 0, 1%7D, 1)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1, 0, 0, 0, 1%7D, 2)); // false%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1, 0, 1, 0, 1%7D, 2)); // false%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1, 0, 1, 0, 1%7D, 3)); // false%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0, 0, 0, 0, 0, 0, 1%7D, 3)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0, 0, 0, 1, 0, 0, 1%7D, 1)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0, 0, 0, 1, 0, 0, 1%7D, 2)); // false%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1, 0, 0, 0, 1, 0, 0%7D, 2)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0%7D, 1)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0, 0%7D, 1)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1%7D, 1)); // false%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1%7D, 0)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B1, 0%7D, 1)); // false%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0, 0, 0%7D, 2)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0, 0, 0, 0%7D, 2)); // true%0A        System.out.println(prob.canPlaceFlowers(new int%5B%5D%7B0, 0, 0, 0%7D, 3)); // false%0A    %7D
 %0A%7D%0A

Solve problem 605