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 a0dfd72

Browse files
feat: add rust solution lc problems: No.1870,1894
* No.1870.Minimum Speed to Arrive on Time * No.1894.Find the Student that Will Replace the Chalk
1 parent 45367e5 commit a0dfd72

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

‎solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,44 @@ func minSpeedOnTime(dist []int, hour float64) int {
270270
}
271271
```
272272

273+
### **Rust**
274+
275+
```rust
276+
impl Solution {
277+
pub fn min_speed_on_time(dist: Vec<i32>, hour: f64) -> i32 {
278+
let n = dist.len();
279+
280+
let check = |speed| {
281+
let mut cur = 0.;
282+
for (i, &d) in dist.iter().enumerate() {
283+
if i == n - 1 {
284+
cur += d as f64 / speed as f64;
285+
} else {
286+
cur += (d as f64 / speed as f64).ceil();
287+
}
288+
}
289+
cur <= hour
290+
};
291+
292+
let mut left = 1;
293+
let mut right = 1e7 as i32;
294+
while left < right {
295+
let mid = left + (right - left) / 2;
296+
if !check(mid) {
297+
left = mid + 1;
298+
} else {
299+
right = mid;
300+
}
301+
}
302+
303+
if check(left) {
304+
return left;
305+
}
306+
-1
307+
}
308+
}
309+
```
310+
273311
### **...**
274312

275313
```

‎solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,44 @@ func minSpeedOnTime(dist []int, hour float64) int {
247247
}
248248
```
249249

250+
### **Rust**
251+
252+
```rust
253+
impl Solution {
254+
pub fn min_speed_on_time(dist: Vec<i32>, hour: f64) -> i32 {
255+
let n = dist.len();
256+
257+
let check = |speed| {
258+
let mut cur = 0.;
259+
for (i, &d) in dist.iter().enumerate() {
260+
if i == n - 1 {
261+
cur += d as f64 / speed as f64;
262+
} else {
263+
cur += (d as f64 / speed as f64).ceil();
264+
}
265+
}
266+
cur <= hour
267+
};
268+
269+
let mut left = 1;
270+
let mut right = 1e7 as i32;
271+
while left < right {
272+
let mid = left + (right - left) / 2;
273+
if !check(mid) {
274+
left = mid + 1;
275+
} else {
276+
right = mid;
277+
}
278+
}
279+
280+
if check(left) {
281+
return left;
282+
}
283+
-1
284+
}
285+
}
286+
```
287+
250288
### **...**
251289

252290
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn min_speed_on_time(dist: Vec<i32>, hour: f64) -> i32 {
3+
let n = dist.len();
4+
5+
let check = |speed| {
6+
let mut cur = 0.;
7+
for (i, &d) in dist.iter().enumerate() {
8+
if i == n - 1 {
9+
cur += d as f64 / speed as f64;
10+
} else {
11+
cur += (d as f64 / speed as f64).ceil();
12+
}
13+
}
14+
cur <= hour
15+
};
16+
17+
let mut left = 1;
18+
let mut right = 1e7 as i32;
19+
while left < right {
20+
let mid = left + (right - left) / 2;
21+
if !check(mid) {
22+
left = mid + 1;
23+
} else {
24+
right = mid;
25+
}
26+
}
27+
28+
if check(left) {
29+
return left;
30+
}
31+
-1
32+
}
33+
}

‎solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@ func chalkReplacer(chalk []int, k int) int {
189189
}
190190
```
191191

192+
### **Rust**
193+
194+
```rust
195+
impl Solution {
196+
pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
197+
let pre_sum: Vec<i64> = chalk
198+
.into_iter()
199+
.map(|x| x as i64)
200+
.scan(0, |state, x| {
201+
*state += x;
202+
Some(*state)
203+
})
204+
.collect();
205+
206+
pre_sum
207+
.binary_search(&(k as i64 % pre_sum.last().unwrap()))
208+
.map_or_else(|e| e, |v| v + 1) as i32
209+
}
210+
}
211+
```
212+
192213
### **...**
193214

194215
```

‎solution/1800-1899/1894.Find the Student that Will Replace the Chalk/README_EN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,27 @@ func chalkReplacer(chalk []int, k int) int {
132132
}
133133
```
134134

135+
### **Rust**
136+
137+
```rust
138+
impl Solution {
139+
pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
140+
let pre_sum: Vec<i64> = chalk
141+
.into_iter()
142+
.map(|x| x as i64)
143+
.scan(0, |state, x| {
144+
*state += x;
145+
Some(*state)
146+
})
147+
.collect();
148+
149+
pre_sum
150+
.binary_search(&(k as i64 % pre_sum.last().unwrap()))
151+
.map_or_else(|e| e, |v| v + 1) as i32
152+
}
153+
}
154+
```
155+
135156
### **...**
136157

137158
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
3+
let pre_sum: Vec<i64> = chalk
4+
.into_iter()
5+
.map(|x| x as i64)
6+
.scan(0, |state, x| {
7+
*state += x;
8+
Some(*state)
9+
})
10+
.collect();
11+
12+
pre_sum
13+
.binary_search(&(k as i64 % pre_sum.last().unwrap()))
14+
.map_or_else(|e| e, |v| v + 1) as i32
15+
}
16+
}

0 commit comments

Comments
(0)

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