0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

day4 at leetcode

0
Posted at
  1. Can Place Flowers
    最初こう考えたが、
class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        sum_num = sum(flowerbed) + n
        return len(flowerbed)-sum_num > len(flowerbed)/2-1 

[0,1,0]などの時うまくいかない。
実直に一つずつ見て判別してみる。

class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        for i in range(len(flowerbed)):
            left  = (i == 0) or (flowerbed[i-1] == 0)#最初or左が0
            right = (i == len(flowerbed)-1) or (flowerbed[i+1] == 0)#最後or右が0
            
            if flowerbed[i] == 0 and left and right:
                flowerbed[i] = 1
                n -= 1
        
        return n <= 0 #0ばかりの時nはマイナスになる

思いつきたかった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?