605. Can Place Flowers
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 23, 2024
Last updated : July 23, 2024
Related Topics : Array, Greedy
Acceptance Rate : 28.82 %
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
indx = 0
counter = 0
while indx < len(flowerbed) :
if flowerbed[indx] :
indx += 2
elif indx and flowerbed[indx - 1] :
indx += 1
elif indx < len(flowerbed) - 1 and flowerbed[indx + 1] :
indx += 3
else :
counter += 1
indx += 2
if counter >= n :
return True
return False
bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n) {
int indx = 0;
int counter = 0;
while (indx < flowerbedSize) {
if (flowerbed[indx] == 1)
indx += 2;
else if (indx > 0 && flowerbed[indx - 1] == 1)
indx += 1;
else if (indx < flowerbedSize - 1 && flowerbed[indx + 1] == 1)
indx += 3;
else {
counter++;
indx += 2;
}
if (counter >= n)
return true;
}
return false;
}
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int indx = 0;
int counter = 0;
while (indx < flowerbed.size()) {
if (flowerbed[indx] == 1)
indx += 2;
else if (indx > 0 && flowerbed[indx - 1] == 1)
indx += 1;
else if (indx < flowerbed.size() - 1 && flowerbed[indx + 1] == 1)
indx += 3;
else {
counter++;
indx += 2;
}
if (counter >= n)
return true;
}
return false;
}
};
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int indx = 0;
int counter = 0;
while (indx < flowerbed.length) {
if (flowerbed[indx] == 1)
indx += 2;
else if (indx > 0 && flowerbed[indx - 1] == 1)
indx += 1;
else if (indx < flowerbed.length - 1 && flowerbed[indx + 1] == 1)
indx += 3;
else {
counter++;
indx += 2;
}
if (counter >= n)
return true;
}
return false;
}
}
/**
* @param {number[]} flowerbed
* @param {number} n
* @return {boolean}
*/
var canPlaceFlowers = function(flowerbed, n) {
let indx = 0;
let counter = 0;
while (indx < flowerbed.length) {
if (flowerbed[indx] == 1)
indx += 2;
else if (indx > 0 && flowerbed[indx - 1] == 1)
indx += 1;
else if (indx < flowerbed.length - 1 && flowerbed[indx + 1] == 1)
indx += 3;
else {
counter++;
indx += 2;
}
if (counter >= n)
return true;
}
return false;
};
impl Solution {
pub fn can_place_flowers(flowerbed: Vec<i32>, n: i32) -> bool {
let mut indx = 0;
let mut counter = 0;
while (indx < flowerbed.len()) {
if (flowerbed[indx] == 1) {
indx += 2;
}
else if (indx > 0 && flowerbed[indx - 1] == 1) {
indx += 1;
}
else if (indx < flowerbed.len() - 1 && flowerbed[indx + 1] == 1) {
indx += 3;
}
else {
counter += 1;
indx += 2;
}
if (counter >= n) {
return true;
}
}
return false;
}
}
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
let indx = 0;
let counter = 0;
while (indx < flowerbed.length) {
if (flowerbed[indx] == 1)
indx += 2;
else if (indx > 0 && flowerbed[indx - 1] == 1)
indx += 1;
else if (indx < flowerbed.length - 1 && flowerbed[indx + 1] == 1)
indx += 3;
else {
counter++;
indx += 2;
}
if (counter >= n)
return true;
}
return false;
};