엔지니어 게시판
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 * 104
  • flowerbed[i] is 0 or 1
  • There are no two adjacent flowers in flowerbed.
0 <= n <= flowerbed.length
Accepted
355.2K
Submissions
1.1M
Acceptance Rate
32.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 False

JJJJJJJJJJ님의 댓글

  • 익명
  • 작성일
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
전체 410 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 574 명
  • 오늘 방문자 1,634 명
  • 어제 방문자 9,517 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,597 명
알림 0