Skip to content

Latest commit

 

History

History
225 lines (195 loc) · 5.26 KB

_605. Can Place Flowers.md

File metadata and controls

225 lines (195 loc) · 5.26 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 23, 2024

Last updated : July 23, 2024


Related Topics : Array, Greedy

Acceptance Rate : 28.82 %


Solutions

Python

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

C

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;
}

C++

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;
    }
};

Java

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;
    }
}

JavaScript

/**
 * @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;
};

Rust

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;
    }
}

TypeScript

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;
};