728x90
문제: https://leetcode.com/problems/can-place-flowers/
난이도: Easy
풀이
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
flowerbed.insert(0, 0)
flowerbed.append(0)
emptyCnt = 0 # 0이 연달아 3개면 심기 가능
flower = 0 # 심은 꽃 갯수
for fb in flowerbed:
if fb == 0:
emptyCnt += 1
else:
emptyCnt = 0 # 리셋
if emptyCnt == 3:
flower += 1
emptyCnt = 1
if flower == n:
return True
return False
728x90