Arena / Problems /

605. Can Place Flowers

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #605

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.

Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.

Example 1
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
Example 2
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Constraints
  • 1 <= flowerbed.length <= 2 * 10^4
  • flowerbed[i] is 0 or 1.
  • There are no two adjacent flowers in flowerbed.
  • 0 <= n <= flowerbed.length
Topics & Algorithms
⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:

Check each empty plot (0). If both adjacent plots are empty (or boundaries), plant a flower (set to 1) and count. Return true if the final count ≥ n, otherwise false.

class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        count = 0

        for i in range(len(flowerbed)):
            if flowerbed[i] == 1:
                continue

            if (i == 0 or flowerbed[i - 1] == 0) and \
               (i == len(flowerbed) - 1 or flowerbed[i + 1] == 0):
                flowerbed[i] = 1
                count += 1

        return count >= n
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:

Check each empty plot (0). If both adjacent plots are empty (or boundaries), plant a flower (set to 1) and count. Return true if the final count ≥ n, otherwise false.

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int count = 0;

        for (int i = 0; i < flowerbed.length; i++) {
            if (flowerbed[i] == 1) {
                continue;
            }

            if ((i == 0 || flowerbed[i - 1] == 0) && 
                (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
                flowerbed[i] = 1;
                count++;
            }
        }

        return count >= n;
    }
}
⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C++ Solution by Anonymous Developer
0
Approach Intuition:

Check each empty plot (0). If both adjacent plots are empty (or boundaries), plant a flower (set to 1) and count. Return true if the final count ≥ n, otherwise false.

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        int count = 0;

        for (int i = 0; i < flowerbed.size(); i++) {
            if (flowerbed[i] == 1) {
                continue;
            }

            if ((i == 0 || flowerbed[i - 1] == 0) && 
                (i == flowerbed.size() - 1 || flowerbed[i + 1] == 0)) {
                flowerbed[i] = 1;
                count++;
            }
        }

        return count >= n;
    }
};
⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Javascript Solution by Anonymous Developer
0
Approach Intuition:

Check each empty plot (0). If both adjacent plots are empty (or boundaries), plant a flower (set to 1) and count. Return true if the final count ≥ n, otherwise false.

var canPlaceFlowers = function(flowerbed, n) {
  let count = 0;

  for (let i = 0; i < flowerbed.length; i++) {
    if (flowerbed[i] === 1) {
      continue;
    }

    if ((i === 0 || flowerbed[i - 1] === 0) && 
      (i === flowerbed.length - 1 || flowerbed[i + 1] === 0)) {
      flowerbed[i] = 1;
      count++;
    }
  }

  return count >= n;  
};

⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Go Solution by Anonymous Developer
0
Approach Intuition:

Check each empty plot (0). If both adjacent plots are empty (or boundaries), plant a flower (set to 1) and count. Return true if the final count ≥ n, otherwise false.

func canPlaceFlowers(flowerbed []int, n int) bool {
    count := 0

    for i := 0; i < len(flowerbed); i++ {
        if flowerbed[i] == 1 {
            continue
        }

        if (i == 0 || flowerbed[i - 1] == 0) && 
           (i == len(flowerbed) - 1 || flowerbed[i + 1] == 0) {
            flowerbed[i] = 1
            count++
        }
    }

    return count >= n
}

⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C# Solution by Anonymous Developer
0
Approach Intuition:

Check each empty plot (0). If both adjacent plots are empty (or boundaries), plant a flower (set to 1) and count. Return true if the final count ≥ n, otherwise false.

public class Solution
{
    public bool CanPlaceFlowers(int[] flowerbed, int n)
    {
        int count = 0;

        for (int i = 0; i < flowerbed.Length; i++)
        {
            if (flowerbed[i] == 1)
            {
                continue;
            }

            if ((i == 0 || flowerbed[i - 1] == 0) && 
                (i == flowerbed.Length - 1 || flowerbed[i + 1] == 0))
                {
                flowerbed[i] = 1;
                count++;
            }
        }

        return count >= n;
    }
}
⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:

Check each empty plot (0). If both adjacent plots are empty (or boundaries), plant a flower (set to 1) and count. Return true if the final count ≥ n, otherwise false.

def can_place_flowers(flowerbed, n)
  count = 0

  flowerbed.each_with_index do |plot, i|
    next if plot == 1

    if (i == 0 || flowerbed[i - 1] == 0) && 
       (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)
      flowerbed[i] = 1
      count += 1
    end
  end

  count >= n
end
⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #605 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.