파비의 매일매일 공부기록

2023.03.20 Today's Challenge 본문

Problem Solving/LeetCode

2023.03.20 Today's Challenge

fabichoi 2023. 3. 20. 23:45

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

 

Can Place Flowers - LeetCode

Can you solve this real interview question? 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'

leetcode.com

근처에 있는 데이터 확인해서 처리

class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        cnt = 0
        for i in range(len(flowerbed)):
            if flowerbed[i] == 0:
                elp = (i == 0) or (flowerbed[i-1] == 0)
                erp = (i == len(flowerbed) - 1) or (flowerbed[i+1] == 0)

                if elp and erp:
                    flowerbed[i] = 1
                    cnt += 1
                    if cnt >= n:
                        return True
        return cnt >= n
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.03.22 Today's Challenge  (0) 2023.03.22
2023.03.21 Today's Challenge  (0) 2023.03.21
2023.03.19 Today's Challenge  (0) 2023.03.19
2023.03.18 Today's Challenge  (0) 2023.03.18
2023.03.17 Today's Challenge  (0) 2023.03.17
Comments