LeetCode 605. Can Place Flowers
https://leetcode.com/problems/can-place-flowers/
class Solution { public boolean canPlaceFlowers(int[] flowerbed, int n) { //max: 0 adjacent 1 //dont create like this: [1, 0, 0, 1, 0, 0, 1] and do like this [1,0,1,0,0,0,1], then we can put one more flower in the flower bed if (flowerbed.length == 1){ //n >= 1; if (n >= 1 && flowerbed[0] == 1) return false; return true; } //find if maximum is >= n; int max = 0; int size = flowerbed.length; //size is bigger than 1 for (int i = 0; i < size; i++){ if (flowerbed[i] == 1){ continue; } if (i == 0){ max += flowerbed[i + 1] == 0? 1: 0; flowerbed[i] = 1; } else if (i == size - 1){ max += flowerbed[i - 1] == 0? 1:0; flowerbed[i] = 1; } else if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0){ max += 1; flowerbed[i] = 1; } } System.out.print(max); return max >= n; } }