LeetCode 솔루션 분류
[3/20] 605. Can Place Flowers
본문
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1 Output: true
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2 Output: false
Constraints:
1 <= flowerbed.length <= 2 * 104flowerbed[i]is0or1There are no two adjacent flowers inflowerbed.
0 <= n <= flowerbed.lengthAccepted355.2KSubmissions1.1MAcceptance Rate32.6% 관련자료
-
링크
댓글 2
학부유학생님의 댓글
- 익명
- 작성일
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
for idx in range(len(flowerbed)):
if flowerbed[idx] == 0 and
(idx == 0 or flowerbed[idx-1] == 0) and
(idx == len(flowerbed)-1 or flowerbed[idx+1] == 0) and
n > 0:
flowerbed[idx] = 1
n -= 1
return True if n == 0 else FalseJJJJJJJJJJ님의 댓글
- 익명
- 작성일
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if n == 0: return True
count = 0
ln = len(flowerbed)
for idx,val in enumerate(flowerbed):
if val == 0 and (idx == 0 or flowerbed[idx-1] == 0) and (idx == ln-1 or flowerbed[idx+1 ]==0):
count += 1
flowerbed[idx] = 1
if count == n:
return True
return False
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if n == 0: return True
count = 0
ln = len(flowerbed)
for idx,val in enumerate(flowerbed):
if val == 0 and (idx == 0 or flowerbed[idx-1] == 0) and (idx == ln-1 or flowerbed[idx+1 ]==0):
count += 1
flowerbed[idx] = 1
if count == n:
return True
return False







