Practice/Meta/Leetcode 605. Can Place Flowers
CodingMust
You are managing a garden plot represented as an array where each position either contains a planted flower (1) or is empty (0). The garden has a strict rule: no two flowers can be planted in adjacent positions.
Given the current state of the garden and a number of new flowers you want to plant, determine whether it's possible to plant all the new flowers while respecting the adjacency rule.
true if you can plant n new flowers without violating the adjacency constraintfalse if it's impossible to plant all n flowersExample 1:
Input: flowerbed = [1, 0, 0, 0, 1], n = 1 Output: true Explanation: We can plant one flower at index 2. The neighbors at indices 1 and 3 are both empty (0).
Example 2:
Input: flowerbed = [1, 0, 0, 0, 1], n = 2 Output: false Explanation: Only one flower can be planted (at index 2). We cannot plant 2 flowers without creating adjacent plants.
Example 3:
Input: flowerbed = [0, 0, 0], n = 2 Output: true Explanation: We can plant flowers at index 0 and index 2, leaving index 1 empty as a buffer.
Example 4:
Input: flowerbed = [0], n = 1 Output: true Explanation: A single empty plot can accommodate one flower since it has no neighbors.