DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

1

Leetcode - 605. Can Place Flowers

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

Image description

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
};
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay