Post

leetcode(리트코드)-605 Can Place Flowers

leetcode 605 - Can Place Flowers문제입니다.

1. 문제

https://leetcode.com/problems/can-place-flowers/

"None"


2. Input , Output

"None"


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • n개의 꽃을 심을겁니다. 인접하지 않게 심을건데 1은 이미 심어져 있는 꽃, 0은 심어지지 않은 곳 입니다.
  • 인접하지 않게 꽃을 전부 심을 수 있으면 True, 아니면 False를 리턴하세요.

5. code

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        if n == 0 : 
            return True
        if len(flowerbed) == 1 :
            if flowerbed[0]== 1 and n==1 :
                return False
            else : 
                return True
        for i in range(len(flowerbed)):
            if i == 0 and flowerbed[i] == 0:
                if flowerbed[i+1] == 0 :
                    n-=1
                    flowerbed[i]=1
            elif i == (len(flowerbed)-1)  : 
                if(flowerbed[i-1] == 0) and flowerbed[i] == 0: 
                    n-=1
                    flowerbed[i]=1
            else:
                if flowerbed[i-1] == 0 and flowerbed[i+1] == 0 and flowerbed[i]==0 : 
                    n-=1
                    flowerbed[i]=1
            if n<1 : 
                return True
        return False
                    
                
            

6. 결과 및 후기, 개선점

"None"

필요시 c++로 짜드립니다.

설명이 필요하다면 댓글을 달아주세요.

This post is licensed under CC BY 4.0 by the author.