605. Can Place Flowers

class Solution {

    /**
     * @param Integer[] $flowerbed
     * @param Integer $n
     * @return Boolean
     */
    function canPlaceFlowers($flowerbed, $n) {
        foreach($flowerbed as $index => $place) {
            if ($n == 0) {
                return true;
            } elseif ($place == 0 && empty($flowerbed[$index - 1]) && empty($flowerbed[$index + 1])) {
                $flowerbed[$index] = 1;
                $n--;
            }
        }

        return $n == 0;
    }
}

Last updated