Open robert-min opened 3 weeks ago
⏰ 소요 시간 : 10분 🗂️ 유형 : 완전탐색
🖌️ 문제 풀이
flowerbed == 1
인 경우와 n==0
인 경우를 처리하지 않아 정답이 되지 않음class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
0 : adjacent check
"""
if n == 0:
return True
if len(flowerbed) == 1:
if flowerbed[0] == 0 and n == 1:
return True
for idx in range(len(flowerbed)):
if flowerbed[idx] == 0:
if idx == 0:
if flowerbed[idx+1] == 0:
flowerbed[idx] = 1
n -= 1
elif idx == len(flowerbed) - 1:
if flowerbed[idx-1] == 0:
flowerbed[idx] = 1
n -= 1
else:
if flowerbed[idx-1] == 0 and flowerbed[idx+1] == 0:
flowerbed[idx] = 1
n -= 1
if n > 0:
return False
return True
문제 추천 이유!!
문제 링크
https://leetcode.com/problems/can-place-flowers/description/?envType=study-plan-v2&envId=leetcode-75
*작성가이드 입니다.
아래는 comment 템플릿입니다.(복사해서 사용)