Skip to content

Commit bf21f11

Browse files
committed
feat: add solutions to lc problem: No.0605
No.0605.Can Place Flowers
1 parent bae4f45 commit bf21f11

File tree

6 files changed

+170
-54
lines changed

6 files changed

+170
-54
lines changed

solution/0600-0699/0605.Can Place Flowers/README.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,88 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
**方法一:贪心**
45+
4446
<!-- tabs:start -->
4547

4648
### **Python3**
4749

4850
<!-- 这里可写当前语言的特殊实现逻辑 -->
4951

5052
```python
51-
53+
class Solution:
54+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
55+
flowerbed = [0] + flowerbed + [0]
56+
for i in range(1, len(flowerbed) - 1):
57+
if sum(flowerbed[i - 1: i + 2]) == 0:
58+
flowerbed[i] = 1
59+
n -= 1
60+
return n <= 0
5261
```
5362

5463
### **Java**
5564

5665
<!-- 这里可写当前语言的特殊实现逻辑 -->
5766

5867
```java
68+
class Solution {
69+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
70+
int m = flowerbed.length;
71+
for (int i = 0; i < m; ++i) {
72+
int l = i == 0 ? 0 : flowerbed[i - 1];
73+
int r = i == m - 1 ? 0 : flowerbed[i + 1];
74+
if (l + flowerbed[i] + r == 0) {
75+
flowerbed[i] = 1;
76+
--n;
77+
}
78+
}
79+
return n <= 0;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
90+
int m = flowerbed.size();
91+
for (int i = 0; i < m; ++i)
92+
{
93+
int l = i == 0 ? 0 : flowerbed[i - 1];
94+
int r = i == m - 1 ? 0 : flowerbed[i + 1];
95+
if (l + flowerbed[i] + r == 0)
96+
{
97+
flowerbed[i] = 1;
98+
--n;
99+
}
100+
}
101+
return n <= 0;
102+
}
103+
};
104+
```
59105
106+
### **Go**
107+
108+
```go
109+
func canPlaceFlowers(flowerbed []int, n int) bool {
110+
m := len(flowerbed)
111+
for i, v := range flowerbed {
112+
l, r := 0, 0
113+
if i > 0 {
114+
l = flowerbed[i-1]
115+
}
116+
if i < m-1 {
117+
r = flowerbed[i+1]
118+
}
119+
if l+v+r == 0 {
120+
flowerbed[i] = 1
121+
n--
122+
}
123+
}
124+
return n <= 0
125+
}
60126
```
61127

62128
### **...**

solution/0600-0699/0605.Can Place Flowers/README_EN.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,77 @@
3333
### **Python3**
3434

3535
```python
36-
36+
class Solution:
37+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
38+
flowerbed = [0] + flowerbed + [0]
39+
for i in range(1, len(flowerbed) - 1):
40+
if sum(flowerbed[i - 1: i + 2]) == 0:
41+
flowerbed[i] = 1
42+
n -= 1
43+
return n <= 0
3744
```
3845

3946
### **Java**
4047

4148
```java
49+
class Solution {
50+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
51+
int m = flowerbed.length;
52+
for (int i = 0; i < m; ++i) {
53+
int l = i == 0 ? 0 : flowerbed[i - 1];
54+
int r = i == m - 1 ? 0 : flowerbed[i + 1];
55+
if (l + flowerbed[i] + r == 0) {
56+
flowerbed[i] = 1;
57+
--n;
58+
}
59+
}
60+
return n <= 0;
61+
}
62+
}
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
class Solution {
69+
public:
70+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
71+
int m = flowerbed.size();
72+
for (int i = 0; i < m; ++i)
73+
{
74+
int l = i == 0 ? 0 : flowerbed[i - 1];
75+
int r = i == m - 1 ? 0 : flowerbed[i + 1];
76+
if (l + flowerbed[i] + r == 0)
77+
{
78+
flowerbed[i] = 1;
79+
--n;
80+
}
81+
}
82+
return n <= 0;
83+
}
84+
};
85+
```
4286
87+
### **Go**
88+
89+
```go
90+
func canPlaceFlowers(flowerbed []int, n int) bool {
91+
m := len(flowerbed)
92+
for i, v := range flowerbed {
93+
l, r := 0, 0
94+
if i > 0 {
95+
l = flowerbed[i-1]
96+
}
97+
if i < m-1 {
98+
r = flowerbed[i+1]
99+
}
100+
if l+v+r == 0 {
101+
flowerbed[i] = 1
102+
n--
103+
}
104+
}
105+
return n <= 0
106+
}
43107
```
44108

45109
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
11
class Solution {
22
public:
33
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
4-
int i = 0, j = flowerbed.size()-1 ;
5-
int cnt=0 ;
6-
int num ;
7-
while (i <= j && !flowerbed[i])
8-
++i ;
9-
if (i > j)
10-
return n <= 1 + (j>>1) ;
11-
num = i>>1 ;
12-
while (!flowerbed[j])
13-
--j ;
14-
num += (flowerbed.size()-1-j)>>1 ;
15-
//cout << i << ' ' << j << endl ;
16-
while (i <= j)
4+
int m = flowerbed.size();
5+
for (int i = 0; i < m; ++i)
176
{
18-
19-
//cout << "num = " << num << ", cnt = " << cnt << endl ;
20-
if (flowerbed[i])
7+
int l = i == 0 ? 0 : flowerbed[i - 1];
8+
int r = i == m - 1 ? 0 : flowerbed[i + 1];
9+
if (l + flowerbed[i] + r == 0)
2110
{
22-
if (cnt > 0)
23-
num += (cnt-1) >> 1 ;
24-
cnt = 0 ;
11+
flowerbed[i] = 1;
12+
--n;
2513
}
26-
else
27-
cnt++ ;
28-
++i ;
2914
}
30-
//cout << num << endl ;
31-
return num >= n ;
15+
return n <= 0;
3216
}
3317
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func canPlaceFlowers(flowerbed []int, n int) bool {
2+
m := len(flowerbed)
3+
for i, v := range flowerbed {
4+
l, r := 0, 0
5+
if i > 0 {
6+
l = flowerbed[i-1]
7+
}
8+
if i < m-1 {
9+
r = flowerbed[i+1]
10+
}
11+
if l+v+r == 0 {
12+
flowerbed[i] = 1
13+
n--
14+
}
15+
}
16+
return n <= 0
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
public boolean canPlaceFlowers(int[] flowerbed, int n) {
3-
int len = flowerbed.length;
4-
int cnt = 0;
5-
for (int i = 0; i < len; ++i) {
6-
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == len - 1 || flowerbed[i + 1] == 0)) {
7-
++cnt;
3+
int m = flowerbed.length;
4+
for (int i = 0; i < m; ++i) {
5+
int l = i == 0 ? 0 : flowerbed[i - 1];
6+
int r = i == m - 1 ? 0 : flowerbed[i + 1];
7+
if (l + flowerbed[i] + r == 0) {
88
flowerbed[i] = 1;
9+
--n;
910
}
1011
}
11-
return cnt >= n;
12+
return n <= 0;
1213
}
1314
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
class Solution:
2-
def canPlaceFlowers(self, flowerBed, n):
3-
"""
4-
type flowerBed : List[int], n : int
5-
rtype : bool
6-
"""
7-
8-
i = 0
9-
while n > 0 and i < len(flowerBed):
10-
if i == 0 and flowerBed[0] == 0: # for 1st Element
11-
if len(flowerBed) == 1 or (len(flowerBed) > 1 and flowerBed[1] == 0):
12-
n -= 1
13-
flowerBed[0] = 1
14-
elif (
15-
i == len(flowerBed) - 1 and flowerBed[i] == 0 and flowerBed[i - 1] == 0
16-
): # for last element
2+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
3+
flowerbed = [0] + flowerbed + [0]
4+
for i in range(1, len(flowerbed) - 1):
5+
if sum(flowerbed[i - 1: i + 2]) == 0:
6+
flowerbed[i] = 1
177
n -= 1
18-
flowerBed[i] = 1
19-
elif flowerBed[i] == 0 and flowerBed[i - 1] == 0 and flowerBed[i + 1] == 0:
20-
n -= 1
21-
flowerBed[i] = 1
22-
i += 1
23-
24-
return n == 0
8+
return n <= 0

0 commit comments

Comments
 (0)