Back to course home
0% completed
Can Place Flowers (easy)
Problem Statement
You have a long, narrow flowerbed divided into sections, each of which can either be empty or filled with a flower. Due to gardening restrictions, you cannot plant flowers in adjacent sections
— they must be at least one section apart to prevent overcrowding.
Given the current state
of the flowerbed (represented as an array, where 0
indicates an empty section and 1
signifies a section with a flower) and a number representing how many more flowers you wish to plant, determine if you can plant all the desired flowers without breaking
the gardening restrictions.
Examples
-
Example 1:
- Input: flowerbed =
[0,0,1,0,1,0,0]
, n =2
- Expected Output: true
- Justification: You can plant flowers in the 0th and 6th index, satisfying the non-adjacent rule.
- Input: flowerbed =
-
Example 2:
- Input: flowerbed =
[1,0,0,0,0,1]
, n =2
- Expected Output: false
- Justification: Despite the four consecutive empty sections, you can only plant one flower (either in the 2nd or 3rd index) without violating the rule of not planting in adjacent sections.
- Input: flowerbed =
-
Example 3:
- Input: flowerbed =
[0,0,1,0,0]
, n =1
- Expected Output: true
- Justification: You can plant one flower either in the 0th or 4th index, adhering to the non-adjacent rule.
- Input: flowerbed =
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
Mark as Completed
Table of Contents
Problem Statement
Examples
Try it yourself