📘
Can Place Flowers
EasyStrings & Arrays
Given a flowerbed (array of 0s and 1s) and n new flowers, return True if n new flowers can be planted without violating the no-adjacent rule.
Example:
Input:
1,0,0,0,1 1
Output:
True
Reasoning:
- The given flowerbed is represented as an array:
[1,0,0,0,1], where0indicates an empty plot and1indicates a plot with a flower. - We need to plant
1new flower, so we look for a plot where we can plant it without violating the no-adjacent rule. - We find that we can plant the new flower at the third plot:
[1,0,0,1,1]or at other positions, but since we only need to plant1flower, we can choose any of these valid positions. - The final output is
Truebecause we can plant the new flower without violating the rule.
Constraints:
- 1 <= len(flowerbed) <= 2 * 10^4
- flowerbed[i] is 0 or 1
- No two adjacent flowers exist in initial state
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.