Skip to content

Commit d7be5f9

Browse files
authored
feat: add solutions to lc problem: No.2086 (doocs#867)
No.2086.Minimum Number of Buckets Required to Collect Rainwater from Houses
1 parent 1593f6c commit d7be5f9

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed

solution/2000-2099/2086.Minimum Number of Buckets Required to Collect Rainwater from Houses/README.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,52 @@
8383
<!-- 这里可写当前语言的特殊实现逻辑 -->
8484

8585
```python
86-
86+
class Solution:
87+
def minimumBuckets(self, street: str) -> int:
88+
n, last = len(street), -2
89+
ans = 0
90+
for i, v in enumerate(street):
91+
if v == 'H':
92+
if last == i - 1:
93+
continue
94+
if i + 1 < n and street[i + 1] == '.':
95+
last = i + 1
96+
elif i - 1 >= 0 and street[i - 1] == '.':
97+
last = i - 1
98+
else:
99+
return -1
100+
ans += 1
101+
return ans
87102
```
88103

89104
### **Java**
90105

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

93108
```java
94-
109+
class Solution {
110+
public int minimumBuckets(String street) {
111+
int last = -2, n = street.length();
112+
int ans = 0;
113+
for (int i = 0; i < n; i++) {
114+
char c = street.charAt(i);
115+
if (c == 'H') {
116+
if (last == i - 1) {
117+
continue;
118+
}
119+
if (i + 1 < n && street.charAt(i + 1) == '.') {
120+
last = i + 1;
121+
} else if (i - 1 >= 0 && street.charAt(i - 1) == '.') {
122+
last = i - 1;
123+
} else {
124+
return -1;
125+
}
126+
ans++;
127+
}
128+
}
129+
return ans;
130+
}
131+
}
95132
```
96133

97134
### **...**

solution/2000-2099/2086.Minimum Number of Buckets Required to Collect Rainwater from Houses/README_EN.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,50 @@ Thus, it is impossible to collect the rainwater from all the houses.
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def minimumBuckets(self, street: str) -> int:
65+
n, last = len(street), -2
66+
ans = 0
67+
for i, v in enumerate(street):
68+
if v == 'H':
69+
if last == i - 1:
70+
continue
71+
if i + 1 < n and street[i + 1] == '.':
72+
last = i + 1
73+
elif i - 1 >= 0 and street[i - 1] == '.':
74+
last = i - 1
75+
else:
76+
return -1
77+
ans += 1
78+
return ans
6479
```
6580

6681
### **Java**
6782

6883
```java
69-
84+
class Solution {
85+
public int minimumBuckets(String street) {
86+
int last = -2, n = street.length();
87+
int ans = 0;
88+
for (int i = 0; i < n; i++) {
89+
char c = street.charAt(i);
90+
if (c == 'H') {
91+
if (last == i - 1) {
92+
continue;
93+
}
94+
if (i + 1 < n && street.charAt(i + 1) == '.') {
95+
last = i + 1;
96+
} else if (i - 1 >= 0 && street.charAt(i - 1) == '.') {
97+
last = i - 1;
98+
} else {
99+
return -1;
100+
}
101+
ans++;
102+
}
103+
}
104+
return ans;
105+
}
106+
}
70107
```
71108

72109
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int minimumBuckets(String street) {
3+
int last = -2, n = street.length();
4+
int ans = 0;
5+
for (int i = 0; i < n; i++) {
6+
char c = street.charAt(i);
7+
if (c == 'H') {
8+
if (last == i - 1) {
9+
continue;
10+
}
11+
if (i + 1 < n && street.charAt(i + 1) == '.') {
12+
last = i + 1;
13+
} else if (i - 1 >= 0 && street.charAt(i - 1) == '.') {
14+
last = i - 1;
15+
} else {
16+
return -1;
17+
}
18+
ans++;
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def minimumBuckets(self, street: str) -> int:
3+
n, last = len(street), -2
4+
ans = 0
5+
for i, v in enumerate(street):
6+
if v == 'H':
7+
if last == i - 1:
8+
continue
9+
if i + 1 < n and street[i + 1] == '.':
10+
last = i + 1
11+
elif i - 1 >= 0 and street[i - 1] == '.':
12+
last = i - 1
13+
else:
14+
return -1
15+
ans += 1
16+
return ans

0 commit comments

Comments
 (0)