File tree 5 files changed +168
-0
lines changed
solution/2000-2099/2079.Watering Plants
5 files changed +168
-0
lines changed Original file line number Diff line number Diff line change @@ -154,6 +154,67 @@ func wateringPlants(plants []int, capacity int) int {
154
154
}
155
155
```
156
156
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
+
157
218
### **...**
158
219
159
220
```
Original file line number Diff line number Diff line change @@ -145,6 +145,67 @@ func wateringPlants(plants []int, capacity int) int {
145
145
}
146
146
```
147
147
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
+
148
209
### **...**
149
210
150
211
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments