luoheng
12/24/2019 - 7:25 AM

canPlaceFlowers

func canPlaceFlowers(flowerbed []int, n int) bool {
    can_fit := true
    if n == 0 {
        return true
    }
    for i := 0; i < len(flowerbed); i++ {
        v := flowerbed[i]
        if v == 1 {
            can_fit = false
        } else {
            if !can_fit {
                can_fit = true
            } else {
                if i + 1 >= len(flowerbed) || flowerbed[i+1] != 1{
                    n--
                    if n == 0 {
                        break
                    }
                }
                can_fit = false
            }
        }
    }
    return n == 0
}