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…


This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu

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


This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu


Print Share Comment Cite Upload Translate Updates
APA

Rakesh Reddy Peddamallu | Sciencx (2024-09-10T04:48:10+00:00) Leetcode – 605. Can Place Flowers. Retrieved from https://www.scien.cx/2024/09/10/leetcode-605-can-place-flowers/

MLA
" » Leetcode – 605. Can Place Flowers." Rakesh Reddy Peddamallu | Sciencx - Tuesday September 10, 2024, https://www.scien.cx/2024/09/10/leetcode-605-can-place-flowers/
HARVARD
Rakesh Reddy Peddamallu | Sciencx Tuesday September 10, 2024 » Leetcode – 605. Can Place Flowers., viewed ,<https://www.scien.cx/2024/09/10/leetcode-605-can-place-flowers/>
VANCOUVER
Rakesh Reddy Peddamallu | Sciencx - » Leetcode – 605. Can Place Flowers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/10/leetcode-605-can-place-flowers/
CHICAGO
" » Leetcode – 605. Can Place Flowers." Rakesh Reddy Peddamallu | Sciencx - Accessed . https://www.scien.cx/2024/09/10/leetcode-605-can-place-flowers/
IEEE
" » Leetcode – 605. Can Place Flowers." Rakesh Reddy Peddamallu | Sciencx [Online]. Available: https://www.scien.cx/2024/09/10/leetcode-605-can-place-flowers/. [Accessed: ]
rf:citation
» Leetcode – 605. Can Place Flowers | Rakesh Reddy Peddamallu | Sciencx | https://www.scien.cx/2024/09/10/leetcode-605-can-place-flowers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.