Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d6e8362

Browse files
committed
feat: add solutions to lc problem: No.2079
No.2079.Watering Plants
1 parent 14dc6f3 commit d6e8362

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,67 @@ func wateringPlants(plants []int, capacity int) int {
154154
}
155155
```
156156

157+
### **TypeScript**
158+
159+
```ts
160+
function wateringPlants(plants: number[], capacity: number): number {
161+
const n = plants.length;
162+
let ans = 0;
163+
let water = capacity;
164+
for (let i = 0; i < n; i++) {
165+
if (water < plants[i]) {
166+
ans += i * 2 + 1;
167+
water = capacity - plants[i];
168+
} else {
169+
ans++;
170+
water -= plants[i];
171+
}
172+
}
173+
return ans;
174+
}
175+
```
176+
177+
### **Rust**
178+
179+
```rust
180+
impl Solution {
181+
pub fn watering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
182+
let n = plants.len();
183+
let mut ans = 0;
184+
let mut water = capacity;
185+
for i in 0..n {
186+
if water < plants[i] {
187+
ans += 2 * i + 1;
188+
water = capacity - plants[i];
189+
} else {
190+
ans += 1;
191+
water -= plants[i];
192+
}
193+
}
194+
ans as i32
195+
}
196+
}
197+
```
198+
199+
### **C**
200+
201+
```c
202+
int wateringPlants(int *plants, int plantsSize, int capacity) {
203+
int ans = 0;
204+
int water = capacity;
205+
for (int i = 0; i < plantsSize; i++) {
206+
if (water < plants[i]) {
207+
ans += i * 2 + 1;
208+
water = capacity - plants[i];
209+
} else {
210+
ans++;
211+
water -= plants[i];
212+
}
213+
}
214+
return ans;
215+
}
216+
```
217+
157218
### **...**
158219
159220
```

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,67 @@ func wateringPlants(plants []int, capacity int) int {
145145
}
146146
```
147147

148+
### **TypeScript**
149+
150+
```ts
151+
function wateringPlants(plants: number[], capacity: number): number {
152+
const n = plants.length;
153+
let ans = 0;
154+
let water = capacity;
155+
for (let i = 0; i < n; i++) {
156+
if (water < plants[i]) {
157+
ans += i * 2 + 1;
158+
water = capacity - plants[i];
159+
} else {
160+
ans++;
161+
water -= plants[i];
162+
}
163+
}
164+
return ans;
165+
}
166+
```
167+
168+
### **Rust**
169+
170+
```rust
171+
impl Solution {
172+
pub fn watering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
173+
let n = plants.len();
174+
let mut ans = 0;
175+
let mut water = capacity;
176+
for i in 0..n {
177+
if water < plants[i] {
178+
ans += 2 * i + 1;
179+
water = capacity - plants[i];
180+
} else {
181+
ans += 1;
182+
water -= plants[i];
183+
}
184+
}
185+
ans as i32
186+
}
187+
}
188+
```
189+
190+
### **C**
191+
192+
```c
193+
int wateringPlants(int *plants, int plantsSize, int capacity) {
194+
int ans = 0;
195+
int water = capacity;
196+
for (int i = 0; i < plantsSize; i++) {
197+
if (water < plants[i]) {
198+
ans += i * 2 + 1;
199+
water = capacity - plants[i];
200+
} else {
201+
ans++;
202+
water -= plants[i];
203+
}
204+
}
205+
return ans;
206+
}
207+
```
208+
148209
### **...**
149210
150211
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
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++;
10+
water -= plants[i];
11+
}
12+
}
13+
return ans;
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn watering_plants(plants: Vec<i32>, capacity: i32) -> i32 {
3+
let n = plants.len();
4+
let mut ans = 0;
5+
let mut water = capacity;
6+
for i in 0..n {
7+
if water < plants[i] {
8+
ans += 2 * i + 1;
9+
water = capacity - plants[i];
10+
} else {
11+
ans += 1;
12+
water -= plants[i];
13+
}
14+
}
15+
ans as i32
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function wateringPlants(plants: number[], capacity: number): number {
2+
const n = plants.length;
3+
let ans = 0;
4+
let water = capacity;
5+
for (let i = 0; i < n; i++) {
6+
if (water < plants[i]) {
7+
ans += i * 2 + 1;
8+
water = capacity - plants[i];
9+
} else {
10+
ans++;
11+
water -= plants[i];
12+
}
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /