[Python] 리트코드 605. Can Place Flowers (그리디)


[Python] 리트코드 605. Can Place Flowers (그리디)

문제 해석 일렬로 된 화분(flowerbed)이 있고, 꽃은 인접해서 심을 수 없다. 0: 빈 화분, 1: 꽃이 심어진 화분, n : 심어야 할 꽃의 수 모든 꽃을 심을 수 있는지 구해라 풀이 1 내 풀이 class Solution: def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: if len(flowerbed)<=2: # 화분이 2개 이하일 때 (양 끝을 따로 확인해서 예외가 생김..) if 1 not in flowerbed: # 0으로만 이루어져 있어야 꽃을 심을 수 있음 n-=1 else: # 화분이 3개 이상일 때 if flowerbed[1]==0 and flowerbed[0]==0: # 왼쪽 끝 확인 n-=1 flowerbed[0]=1 if flowerbed[-2]==0 and flowerbed[-1]==0: # 오른쪽 끝 확인 n-=1 flowerbed[-1]=1 for i in range(1, len(f...



원문링크 : [Python] 리트코드 605. Can Place Flowers (그리디)