File tree Expand file tree Collapse file tree 3 files changed +103
-0
lines changed
solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden Expand file tree Collapse file tree 3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -162,6 +162,42 @@ public:
162
162
};
163
163
```
164
164
165
+ ### **Rust**
166
+
167
+ ```rust
168
+ impl Solution {
169
+ #[allow(dead_code)]
170
+ pub fn min_taps(n: i32, ranges: Vec<i32>) -> i32 {
171
+ let mut last = vec![0; (n + 1) as usize];
172
+ let mut ans = 0;
173
+ let mut mx = 0;
174
+ let mut pre = 0;
175
+
176
+ // Initialize the last vector
177
+ for (i, &r) in ranges.iter().enumerate() {
178
+ if i as i32 - r >= 0 {
179
+ last[(i as i32 - r) as usize] = std::cmp::max(last[(i as i32 - r) as usize], i as i32 + r);
180
+ } else {
181
+ last[0] = std::cmp::max(last[0], i as i32 + r);
182
+ }
183
+ }
184
+
185
+ for i in 0..n as usize {
186
+ mx = std::cmp::max(mx, last[i]);
187
+ if mx <= i as i32 {
188
+ return -1;
189
+ }
190
+ if pre == i as i32 {
191
+ ans += 1;
192
+ pre = mx;
193
+ }
194
+ }
195
+
196
+ ans
197
+ }
198
+ }
199
+ ```
200
+
165
201
### ** Go**
166
202
167
203
``` go
Original file line number Diff line number Diff line change @@ -122,6 +122,42 @@ public:
122
122
};
123
123
```
124
124
125
+ ### **Rust**
126
+
127
+ ```rust
128
+ impl Solution {
129
+ #[allow(dead_code)]
130
+ pub fn min_taps(n: i32, ranges: Vec<i32>) -> i32 {
131
+ let mut last = vec![0; (n + 1) as usize];
132
+ let mut ans = 0;
133
+ let mut mx = 0;
134
+ let mut pre = 0;
135
+
136
+ // Initialize the last vector
137
+ for (i, &r) in ranges.iter().enumerate() {
138
+ if i as i32 - r >= 0 {
139
+ last[(i as i32 - r) as usize] = std::cmp::max(last[(i as i32 - r) as usize], i as i32 + r);
140
+ } else {
141
+ last[0] = std::cmp::max(last[0], i as i32 + r);
142
+ }
143
+ }
144
+
145
+ for i in 0..n as usize {
146
+ mx = std::cmp::max(mx, last[i]);
147
+ if mx <= i as i32 {
148
+ return -1;
149
+ }
150
+ if pre == i as i32 {
151
+ ans += 1;
152
+ pre = mx;
153
+ }
154
+ }
155
+
156
+ ans
157
+ }
158
+ }
159
+ ```
160
+
125
161
### ** Go**
126
162
127
163
``` go
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ #[ allow( dead_code) ]
3
+ pub fn min_taps ( n : i32 , ranges : Vec < i32 > ) -> i32 {
4
+ let mut last = vec ! [ 0 ; ( n + 1 ) as usize ] ;
5
+ let mut ans = 0 ;
6
+ let mut mx = 0 ;
7
+ let mut pre = 0 ;
8
+
9
+ // Initialize the last vector
10
+ for ( i, & r) in ranges. iter ( ) . enumerate ( ) {
11
+ if i as i32 - r >= 0 {
12
+ last[ ( i as i32 - r) as usize ] = std:: cmp:: max ( last[ ( i as i32 - r) as usize ] , i as i32 + r) ;
13
+ } else {
14
+ last[ 0 ] = std:: cmp:: max ( last[ 0 ] , i as i32 + r) ;
15
+ }
16
+ }
17
+
18
+ for i in 0 ..n as usize {
19
+ mx = std:: cmp:: max ( mx, last[ i] ) ;
20
+ if mx <= i as i32 {
21
+ return -1 ;
22
+ }
23
+ if pre == i as i32 {
24
+ ans += 1 ;
25
+ pre = mx;
26
+ }
27
+ }
28
+
29
+ ans
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments