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 334bdaa

Browse files
authored
feat: add rust solution to lc problem: No.1326 (doocs#1567)
1 parent 99fdb7b commit 334bdaa

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

‎solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,42 @@ public:
162162
};
163163
```
164164
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+
165201
### **Go**
166202

167203
```go

‎solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README_EN.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,42 @@ public:
122122
};
123123
```
124124
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+
125161
### **Go**
126162

127163
```go
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
(0)

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