Skip to content

Commit 8f9e6fb

Browse files
authored
feat: add solutions to lc problem: No.2079 (#2756)
No.2079.Watering Plants
1 parent c5d742b commit 8f9e6fb

File tree

9 files changed

+156
-146
lines changed

9 files changed

+156
-146
lines changed

solution/2000-2099/2079.Watering Plants/README.md

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,46 @@
7373

7474
## 解法
7575

76-
### 方法一
76+
### 方法一:模拟
77+
78+
我们可以模拟给植物浇水的过程,用一个变量 $\text{water}$ 表示当前水罐中的水量,初始时 $\text{water} = \text{capacity}$。
79+
80+
我们遍历植物,对于每一株植物:
81+
82+
- 如果当前水罐中的水量足够浇灌这株植物,我们就向前移动一步,浇灌这株植物,同时更新 $\text{water} = \text{water} - \text{plants}[i]$。
83+
- 否则我们就需要返回河边重新装满水罐,再次走到当前位置,然后向前移动一步,此时我们需要的步数为 $i \times 2 + 1$,然后我们浇灌这株植物,更新 $\text{water} = \text{capacity} - \text{plants}[i]$。
84+
85+
最后返回总的步数即可。
86+
87+
时间复杂度 $O(n)$,其中 $n$ 为植物的数量。空间复杂度 $O(1)$。
7788

7889
<!-- tabs:start -->
7990

8091
```python
8192
class Solution:
8293
def wateringPlants(self, plants: List[int], capacity: int) -> int:
83-
ans, cap = 0, capacity
84-
for i, x in enumerate(plants):
85-
if cap >= x:
86-
cap -= x
94+
ans, water = 0, capacity
95+
for i, p in enumerate(plants):
96+
if water >= p:
97+
water -= p
8798
ans += 1
8899
else:
89-
cap = capacity - x
100+
water = capacity - p
90101
ans += i * 2 + 1
91102
return ans
92103
```
93104

94105
```java
95106
class Solution {
96107
public int wateringPlants(int[] plants, int capacity) {
97-
int ans = 0, cap = capacity;
108+
int ans = 0, water = capacity;
98109
for (int i = 0; i < plants.length; ++i) {
99-
if (cap >= plants[i]) {
100-
cap -= plants[i];
101-
++ans;
110+
if (water >= plants[i]) {
111+
water -= plants[i];
112+
ans += 1;
102113
} else {
103-
ans += (i * 2 + 1);
104-
cap = capacity - plants[i];
114+
water = capacity - plants[i];
115+
ans += i * 2 + 1;
105116
}
106117
}
107118
return ans;
@@ -113,13 +124,13 @@ class Solution {
113124
class Solution {
114125
public:
115126
int wateringPlants(vector<int>& plants, int capacity) {
116-
int ans = 0, cap = capacity;
127+
int ans = 0, water = capacity;
117128
for (int i = 0; i < plants.size(); ++i) {
118-
if (cap >= plants[i]) {
119-
cap -= plants[i];
120-
++ans;
129+
if (water >= plants[i]) {
130+
water -= plants[i];
131+
ans += 1;
121132
} else {
122-
cap = capacity - plants[i];
133+
water = capacity - plants[i];
123134
ans += i * 2 + 1;
124135
}
125136
}
@@ -129,33 +140,31 @@ public:
129140
```
130141

131142
```go
132-
func wateringPlants(plants []int, capacity int) int {
133-
ans, cap := 0, capacity
134-
for i, x := range plants {
135-
if cap >= x {
136-
cap -= x
143+
func wateringPlants(plants []int, capacity int) (ans int) {
144+
water := capacity
145+
for i, p := range plants {
146+
if water >= p {
147+
water -= p
137148
ans++
138149
} else {
139-
cap = capacity - x
150+
water = capacity - p
140151
ans += i*2 + 1
141152
}
142153
}
143-
return ans
154+
return
144155
}
145156
```
146157

147158
```ts
148159
function wateringPlants(plants: number[], capacity: number): number {
149-
const n = plants.length;
150-
let ans = 0;
151-
let water = capacity;
152-
for (let i = 0; i < n; i++) {
153-
if (water < plants[i]) {
154-
ans += i * 2 + 1;
155-
water = capacity - plants[i];
156-
} else {
157-
ans++;
160+
let [ans, water] = [0, capacity];
161+
for (let i = 0; i < plants.length; ++i) {
162+
if (water >= plants[i]) {
158163
water -= plants[i];
164+
++ans;
165+
} else {
166+
water = capacity - plants[i];
167+
ans += i * 2 + 1;
159168
}
160169
}
161170
return ans;
@@ -165,34 +174,32 @@ function wateringPlants(plants: number[], capacity: number): number {
165174
```rust
166175
impl Solution {
167176
pub fn watering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
168-
let n = plants.len();
169177
let mut ans = 0;
170178
let mut water = capacity;
171-
for i in 0..n {
172-
if water < plants[i] {
173-
ans += 2 * i + 1;
174-
water = capacity - plants[i];
175-
} else {
179+
for (i, &p) in plants.iter().enumerate() {
180+
if water >= p {
181+
water -= p;
176182
ans += 1;
177-
water -= plants[i];
183+
} else {
184+
water = capacity - p;
185+
ans += (i as i32) * 2 + 1;
178186
}
179187
}
180-
ans as i32
188+
ans
181189
}
182190
}
183191
```
184192

185193
```c
186194
int wateringPlants(int* plants, int plantsSize, int capacity) {
187-
int ans = 0;
188-
int water = capacity;
189-
for (int i = 0; i < plantsSize; i++) {
190-
if (water < plants[i]) {
191-
ans += i * 2 + 1;
192-
water = capacity - plants[i];
193-
} else {
194-
ans++;
195+
int ans = 0, water = capacity;
196+
for (int i = 0; i < plantsSize; ++i) {
197+
if (water >= plants[i]) {
195198
water -= plants[i];
199+
ans += 1;
200+
} else {
201+
water = capacity - plants[i];
202+
ans += i * 2 + 1;
196203
}
197204
}
198205
return ans;

solution/2000-2099/2079.Watering Plants/README_EN.md

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -70,35 +70,46 @@ Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.
7070

7171
## Solutions
7272

73-
### Solution 1
73+
### Solution 1: Simulation
74+
75+
We can simulate the process of watering the plants. We use a variable $\text{water}$ to represent the current amount of water in the watering can, initially $\text{water} = \text{capacity}$.
76+
77+
We traverse the plants. For each plant:
78+
79+
- If the current amount of water in the watering can is enough to water this plant, we move forward one step, water this plant, and update $\text{water} = \text{water} - \text{plants}[i]$.
80+
- Otherwise, we need to return to the river to refill the watering can, walk back to the current position, and then move forward one step. The number of steps we need is $i \times 2 + 1$. Then we water this plant and update $\text{water} = \text{capacity} - \text{plants}[i]$.
81+
82+
Finally, return the total number of steps.
83+
84+
The time complexity is $O(n)$, where $n$ is the number of plants. The space complexity is $O(1)$.
7485

7586
<!-- tabs:start -->
7687

7788
```python
7889
class Solution:
7990
def wateringPlants(self, plants: List[int], capacity: int) -> int:
80-
ans, cap = 0, capacity
81-
for i, x in enumerate(plants):
82-
if cap >= x:
83-
cap -= x
91+
ans, water = 0, capacity
92+
for i, p in enumerate(plants):
93+
if water >= p:
94+
water -= p
8495
ans += 1
8596
else:
86-
cap = capacity - x
97+
water = capacity - p
8798
ans += i * 2 + 1
8899
return ans
89100
```
90101

91102
```java
92103
class Solution {
93104
public int wateringPlants(int[] plants, int capacity) {
94-
int ans = 0, cap = capacity;
105+
int ans = 0, water = capacity;
95106
for (int i = 0; i < plants.length; ++i) {
96-
if (cap >= plants[i]) {
97-
cap -= plants[i];
98-
++ans;
107+
if (water >= plants[i]) {
108+
water -= plants[i];
109+
ans += 1;
99110
} else {
100-
ans += (i * 2 + 1);
101-
cap = capacity - plants[i];
111+
water = capacity - plants[i];
112+
ans += i * 2 + 1;
102113
}
103114
}
104115
return ans;
@@ -110,13 +121,13 @@ class Solution {
110121
class Solution {
111122
public:
112123
int wateringPlants(vector<int>& plants, int capacity) {
113-
int ans = 0, cap = capacity;
124+
int ans = 0, water = capacity;
114125
for (int i = 0; i < plants.size(); ++i) {
115-
if (cap >= plants[i]) {
116-
cap -= plants[i];
117-
++ans;
126+
if (water >= plants[i]) {
127+
water -= plants[i];
128+
ans += 1;
118129
} else {
119-
cap = capacity - plants[i];
130+
water = capacity - plants[i];
120131
ans += i * 2 + 1;
121132
}
122133
}
@@ -126,33 +137,31 @@ public:
126137
```
127138

128139
```go
129-
func wateringPlants(plants []int, capacity int) int {
130-
ans, cap := 0, capacity
131-
for i, x := range plants {
132-
if cap >= x {
133-
cap -= x
140+
func wateringPlants(plants []int, capacity int) (ans int) {
141+
water := capacity
142+
for i, p := range plants {
143+
if water >= p {
144+
water -= p
134145
ans++
135146
} else {
136-
cap = capacity - x
147+
water = capacity - p
137148
ans += i*2 + 1
138149
}
139150
}
140-
return ans
151+
return
141152
}
142153
```
143154

144155
```ts
145156
function wateringPlants(plants: number[], capacity: number): number {
146-
const n = plants.length;
147-
let ans = 0;
148-
let water = capacity;
149-
for (let i = 0; i < n; i++) {
150-
if (water < plants[i]) {
151-
ans += i * 2 + 1;
152-
water = capacity - plants[i];
153-
} else {
154-
ans++;
157+
let [ans, water] = [0, capacity];
158+
for (let i = 0; i < plants.length; ++i) {
159+
if (water >= plants[i]) {
155160
water -= plants[i];
161+
++ans;
162+
} else {
163+
water = capacity - plants[i];
164+
ans += i * 2 + 1;
156165
}
157166
}
158167
return ans;
@@ -162,34 +171,32 @@ function wateringPlants(plants: number[], capacity: number): number {
162171
```rust
163172
impl Solution {
164173
pub fn watering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
165-
let n = plants.len();
166174
let mut ans = 0;
167175
let mut water = capacity;
168-
for i in 0..n {
169-
if water < plants[i] {
170-
ans += 2 * i + 1;
171-
water = capacity - plants[i];
172-
} else {
176+
for (i, &p) in plants.iter().enumerate() {
177+
if water >= p {
178+
water -= p;
173179
ans += 1;
174-
water -= plants[i];
180+
} else {
181+
water = capacity - p;
182+
ans += (i as i32) * 2 + 1;
175183
}
176184
}
177-
ans as i32
185+
ans
178186
}
179187
}
180188
```
181189

182190
```c
183191
int wateringPlants(int* plants, int plantsSize, int capacity) {
184-
int ans = 0;
185-
int water = capacity;
186-
for (int i = 0; i < plantsSize; i++) {
187-
if (water < plants[i]) {
188-
ans += i * 2 + 1;
189-
water = capacity - plants[i];
190-
} else {
191-
ans++;
192+
int ans = 0, water = capacity;
193+
for (int i = 0; i < plantsSize; ++i) {
194+
if (water >= plants[i]) {
192195
water -= plants[i];
196+
ans += 1;
197+
} else {
198+
water = capacity - plants[i];
199+
ans += i * 2 + 1;
193200
}
194201
}
195202
return ans;

solution/2000-2099/2079.Watering Plants/Solution.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
int wateringPlants(int* plants, int plantsSize, int capacity) {
2-
int ans = 0;
3-
int water = capacity;
4-
for (int i = 0; i < plantsSize; i++) {
5-
if (water < plants[i]) {
6-
ans += i * 2 + 1;
7-
water = capacity - plants[i];
8-
} else {
9-
ans++;
2+
int ans = 0, water = capacity;
3+
for (int i = 0; i < plantsSize; ++i) {
4+
if (water >= plants[i]) {
105
water -= plants[i];
6+
ans += 1;
7+
} else {
8+
water = capacity - plants[i];
9+
ans += i * 2 + 1;
1110
}
1211
}
1312
return ans;

solution/2000-2099/2079.Watering Plants/Solution.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
int wateringPlants(vector<int>& plants, int capacity) {
4-
int ans = 0, cap = capacity;
4+
int ans = 0, water = capacity;
55
for (int i = 0; i < plants.size(); ++i) {
6-
if (cap >= plants[i]) {
7-
cap -= plants[i];
8-
++ans;
6+
if (water >= plants[i]) {
7+
water -= plants[i];
8+
ans += 1;
99
} else {
10-
cap = capacity - plants[i];
10+
water = capacity - plants[i];
1111
ans += i * 2 + 1;
1212
}
1313
}

0 commit comments

Comments
 (0)