File tree 6 files changed +167
-0
lines changed
1870.Minimum Speed to Arrive on Time
1894.Find the Student that Will Replace the Chalk
6 files changed +167
-0
lines changed Original file line number Diff line number Diff line change @@ -270,6 +270,44 @@ func minSpeedOnTime(dist []int, hour float64) int {
270
270
}
271
271
```
272
272
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 = 1e 7 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
+
273
311
### ** ...**
274
312
275
313
```
Original file line number Diff line number Diff line change @@ -247,6 +247,44 @@ func minSpeedOnTime(dist []int, hour float64) int {
247
247
}
248
248
```
249
249
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 = 1e 7 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
+
250
288
### ** ...**
251
289
252
290
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -189,6 +189,27 @@ func chalkReplacer(chalk []int, k int) int {
189
189
}
190
190
```
191
191
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
+
192
213
### ** ...**
193
214
194
215
```
Original file line number Diff line number Diff line change @@ -132,6 +132,27 @@ func chalkReplacer(chalk []int, k int) int {
132
132
}
133
133
```
134
134
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
+
135
156
### ** ...**
136
157
137
158
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments