leetcode--python--605

605. Flower Problem

Suppose you have a very long flower bed, and one part of the plot is planted with flowers but another part is not. However, flowers cannot be planted on adjacent plots. They will compete for water and both will die.

Given a flower bed (represented as an array containing 0 and 1, where 0 means no flowers are planted, and 1 means flowers are planted), and a number n. Can n flowers be planted without breaking the planting rules? It returns True if it can, and False if it can't.

class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        tem_list = [0] + flowerbed + [0]
        num = len(tem_list)
        k = 0
        for i in range(1, num-1):
            if tem_list[i] == 0 and tem_list[i-1] == 0 and tem_list[i+1] == 0 :
                tem_list[i] = 1
                k+=1
        return(k >= n)		##注意这里是大于等于而不是等于,看清题目

Guess you like

Origin blog.csdn.net/AWhiteDongDong/article/details/111035398