Skip to content

Commit e8331dc

Browse files
authored
Merge pull request #852 from aaradhyasinghgaur/adding-LC-solution
Adding LC Solution for 605. Can Place Flowers
2 parents 771d6ef + f2a9efd commit e8331dc

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
id: can-place-flowers
3+
title: Can Place Flowers (Leetcode)
4+
sidebar_label: 0605-CanPlaceFlowers
5+
description: 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.
6+
---
7+
8+
## Problem Description
9+
10+
| Problem Statement | Solution Link | LeetCode Profile |
11+
| :---------------- | :------------ | :--------------- |
12+
| [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/description/) | [Can Place Flowers Solution on LeetCode](https://leetcode.com/problems/can-place-flowers/solutions) | [Aaradhya Singh ](https://leetcode.com/u/keira_09/) |
13+
14+
15+
## Problem Description
16+
17+
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.
18+
19+
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.
20+
21+
### Examples
22+
23+
#### Example 1
24+
25+
- **Input:** $flowerbed = [1,0,0,0,1], n = 1$
26+
- **Output:** $true$
27+
28+
29+
#### Example 2
30+
31+
- **Input:** $flowerbed = [1,0,0,0,1], n = 2$
32+
- **Output:** $false$
33+
34+
### Constraints
35+
36+
- $1 <= flowerbed.length <= 2 * 104$
37+
- $flowerbed[i]$ is 0 or 1.
38+
- There are no two adjacent flowers in $flowerbed$.
39+
- $1 <= k <= 109$
40+
41+
42+
### Intuition
43+
44+
The code aims to determine if it's possible to plant n flowers in a flowerbed without violating the no-adjacent-flowers rule. It iterates through the flowerbed array and checks if a flower can be planted at each position by ensuring the current position and its adjacent positions are empty (or out of bounds). If a flower can be planted, it updates the position and decrements the count of flowers left to plant. The process continues until either all flowers are planted or all positions are checked. If the count of flowers to be planted reaches zero, it returns true, indicating success; otherwise, it returns false.
45+
46+
47+
### Approach
48+
49+
1. **Initialize Variables:**
50+
51+
- Determine the size of the flowerbed array.
52+
53+
2. **Iterate Through the Flowerbed:**
54+
55+
- Use a for loop to traverse each position in the flowerbed array.
56+
- Continue the loop as long as there are positions to check and flowers to plant (n > 0).
57+
58+
3. **Check Conditions for Planting a Flower:**
59+
60+
- At each position i, check if the current position is empty (flowerbed[i] == 0).
61+
- Also, check if the previous position (if it exists) is empty or out of bounds (i == 0 || flowerbed[i - 1] == 0).
62+
- Similarly, check if the next position (if it exists) is empty or out of bounds (i == size - 1 || flowerbed[i + 1] == 0).
63+
64+
4. **Plant the Flower:**
65+
66+
- If all conditions are met, plant a flower at position i by setting flowerbed[i] = 1.
67+
- Decrease the count of flowers left to plant (n--).
68+
69+
5. **Check if All Flowers Are Planted:**
70+
71+
- After the loop, check if the count of flowers left to plant is zero.
72+
- If n is zero, return true indicating that all flowers have been successfully planted.
73+
- If n is not zero, return false indicating that it's not possible to plant all n flowers.
74+
75+
### Solution Code
76+
77+
#### Python
78+
79+
```py
80+
class Solution:
81+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
82+
size = len(flowerbed)
83+
84+
for i in range(size):
85+
if n == 0:
86+
return True
87+
if flowerbed[i] == 0 and (i == 0 or flowerbed[i - 1] == 0) and (i == size - 1 or flowerbed[i + 1] == 0):
88+
flowerbed[i] = 1 # Place a flower
89+
n -= 1 # Decrease the remaining flowers to place
90+
91+
return n == 0
92+
```
93+
94+
#### Java
95+
96+
```java
97+
class Solution {
98+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
99+
int size = flowerbed.length;
100+
101+
for (int i = 0; i < size && n > 0; i++) {
102+
if (flowerbed[i] == 0 &&
103+
(i == 0 || flowerbed[i - 1] == 0) &&
104+
(i == size - 1 || flowerbed[i + 1] == 0)) {
105+
flowerbed[i] = 1; // Place a flower
106+
n--; // Decrease the remaining flowers to place
107+
}
108+
}
109+
110+
return n == 0;
111+
}
112+
}
113+
```
114+
115+
#### C++
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
121+
int size = flowerbed.size();
122+
for (int i = 0; i < size && n > 0; i++) {
123+
if (flowerbed[i] == 0 &&
124+
(i == 0 || flowerbed[i - 1] == 0) &&
125+
(i == size - 1 || flowerbed[i + 1] == 0)) {
126+
flowerbed[i] = 1; // Place a flower
127+
n--; // Decrease the remaining flowers to place
128+
}
129+
}
130+
return n == 0;
131+
}
132+
};
133+
```
134+
135+
### Conclusion
136+
137+
The provided code effectively determines whether it is possible to plant n flowers in a given flowerbed array without violating the no-adjacent-flowers rule. By iterating through the array and checking each position for the possibility of planting a flower based on the state of the adjacent positions, the code ensures that flowers are planted only where allowed. The solution modifies the array in place and uses a greedy approach to place as many flowers as possible. The time complexity of this approach is $O(n)$, where nn is the length of the flowerbed array, as each position is checked once. The space complexity is $O(1)$ since the solution only uses a constant amount of extra space.

0 commit comments

Comments
 (0)