smj007

Untitled

Apr 7th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. class Solution:
  2.     def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
  3.        
  4.         counter = 0
  5.  
  6.         for i in range(len(flowerbed)):
  7.             if flowerbed[i]==0:
  8.                 # check on the left
  9.                 empty_left = i==0 or flowerbed[i-1]!=1
  10.                 # check on the right
  11.                 empty_right = i==len(flowerbed)-1 or flowerbed[i+1]!=1
  12.                 # if both are empty, place the flower
  13.                 if empty_left and empty_right:
  14.                     # plan the godamn flower
  15.                     flowerbed[i] = 1
  16.                     n=n-1
  17.                     if n==0:
  18.                         return True
  19.  
  20.         return False
  21.  
  22.  
Add Comment
Please, Sign In to add comment