dev-resources.site
for different kinds of informations.
Leetcode - 605. Can Place Flowers
Published at
9/10/2024
Categories
leetcode
Author
Rakesh Reddy Peddamallu
Categories
1 categories in total
leetcode
open
To solve the problem we are going to iterate through the flowerbed and check for 3 things , left , right and then current
Left
left should be empty and also it can be the first pot as left of it is not present and we can plant there
Right
Right should be empty and also it can be the last pot as right of it is not present and we can plant there
Current
Current should be empty so we can plant there
Javascript Code
/**
* @param {number[]} flowerbed
* @param {number} n
* @return {boolean}
*/
var canPlaceFlowers = function(flowerbed, n) {
for(let i=0;i<flowerbed.length;i++){
let left = i==0 || flowerbed[i-1]==0
let right = (i == (flowerbed.length) - 1) || flowerbed[i+1] == 0
if (left && right && flowerbed[i] == 0){
flowerbed[i] = 1
n --
}
}
return n <=0
};
Articles
12 articles in total
Salary breakdown in india
read article
Leetcode - 392. Is Subsequence
read article
Leetcode - 283. Move Zeroes
read article
Leetcode - 443. String Compression
read article
Leetcode - 334. Increasing Triplet Subsequence
read article
Leetcode - 238. Product of Array Except Self
read article
Leetcode - 151. Reverse Words in a String
read article
Leetcode - 345. Reverse Vowels of a String
read article
Leetcode - 605. Can Place Flowers
currently reading
Leetcode - 1431. Kids With the Greatest Number of Candies
read article
Leetcode - 1071. Greatest Common Divisor of Strings
read article
Leetcode - 1768. Merge Strings Alternately
read article
Featured ones: