Hey there! Just wanted to let you know that some of the links in this comment are affiliate links, which means that if you make a purchase through them, I may earn a small commission. Don't worry though, it doesn't cost you anything extra and it helps support this channel so I can continue to make more videos for you. Thank you so much for your support, and as always, all opinions are my own! Start getting great at system design: bytebytego.com?fpr=nideesh (affiliate link) Handpicked Algorithms and Data Structures for Interview To Save Time: interviewpen.com/?via=nideesh (affiliate link) Fast track to becoming a knowledgeable SWE www.educative.io/unlimited?aff=K1z6 (affiliate link)
Very nice explanation and combining of boundary conditions. Since we place a flower at i only when right (i+1) is empty, we can incr i while doing a[i] = 1 (to skip the right empty slot) ==> change to a[i++] = 1. Also, acc to convention, we could have used n for array len and k for num of flowers.
Python solution: class Solution(object): def canPlaceFlowers(self, flowerbed, n): """ :type flowerbed: List[int] :type n: int :rtype: bool """ count = 0 for i in range(0,len(flowerbed)): if flowerbed[i] == 0 and ( i == 0 or flowerbed[i-1] == 0) and ( i == len(flowerbed) - 1 or flowerbed[i+1] == 0): flowerbed[i] = 1 count += 1
return count >= n Runtime: 168 ms, faster than 19.37% of Python online submissions for Can Place Flowers. Memory Usage: 14.2 MB, less than 29.55% of Python online submissions for Can Place Flowers.
Nice explanation.
Hey there! Just wanted to let you know that some of the links in this comment are affiliate links, which means that if you make a purchase through them, I may earn a small commission. Don't worry though, it doesn't cost you anything extra and it helps support this channel so I can continue to make more videos for you. Thank you so much for your support, and as always, all opinions are my own!
Start getting great at system design: bytebytego.com?fpr=nideesh (affiliate link)
Handpicked Algorithms and Data Structures for Interview To Save Time: interviewpen.com/?via=nideesh (affiliate link)
Fast track to becoming a knowledgeable SWE www.educative.io/unlimited?aff=K1z6 (affiliate link)
In the if block we should check for last i.e i==k-1 instead i ==n-1
Dude, I saw you on gfg video explaining palindromic string subset and here!. Please make more videos!. Your explanation is soo good.
There should be "k" in the if statement , third part i.e i == k-1 || arr[i +1] ==0 , otherwise it will give wrong answer for boundary condition
Nice and simple explanation. Thanks
Very nice explanation and combining of boundary conditions. Since we place a flower at i only when right (i+1) is empty, we can incr i while doing a[i] = 1 (to skip the right empty slot) ==> change to a[i++] = 1.
Also, acc to convention, we could have used n for array len and k for num of flowers.
Are you sure we want to mutate the input array?
Python solution:
class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
count = 0
for i in range(0,len(flowerbed)):
if flowerbed[i] == 0 and ( i == 0 or flowerbed[i-1] == 0) and ( i == len(flowerbed) - 1 or flowerbed[i+1] == 0):
flowerbed[i] = 1
count += 1
return count >= n
Runtime: 168 ms, faster than 19.37% of Python online submissions for Can Place Flowers.
Memory Usage: 14.2 MB, less than 29.55% of Python online submissions for Can Place Flowers.
we don't need to use a new count variable, we can decrement the N each time when we plot a new flower.
and return n
thanks!
Bro, are u from Telugu states?
Avunu bro