1870. Minimum Speed to Arrive on Time Medium

@problem@discussion
#Array#Binary Search



1/**
2 * [1870] Minimum Speed to Arrive on Time
3 *
4 * You are given a floating-point number hour, representing the amount of time you have to reach the office. To commute to the office, you must take n trains in sequential order. You are also given an integer array dist of length n, where dist[i] describes the distance (in kilometers) of the i^th train ride.
5 * Each train can only depart at an integer hour, so you may need to wait in between each train ride.
6 * 
7 * 	For example, if the 1^st train ride takes 1.5 hours, you must wait for an additional 0.5 hours before you can depart on the 2^nd train ride at the 2 hour mark.
8 * 
9 * Return the minimum positive integer speed (in kilometers per hour) that all the trains must travel at for you to reach the office on time, or -1 if it is impossible to be on time.
10 * Tests are generated such that the answer will not exceed 10^7 and hour will have at most two digits after the decimal point.
11 *  
12 * Example 1:
13 * 
14 * Input: dist = [1,3,2], hour = 6
15 * Output: 1
16 * Explanation: At speed 1:
17 * - The first train ride takes 1/1 = 1 hour.
18 * - Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours.
19 * - Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours.
20 * - You will arrive at exactly the 6 hour mark.
21 * 
22 * Example 2:
23 * 
24 * Input: dist = [1,3,2], hour = 2.7
25 * Output: 3
26 * Explanation: At speed 3:
27 * - The first train ride takes 1/3 = 0.33333 hours.
28 * - Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train ride takes 3/3 = 1 hour.
29 * - Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third train takes 2/3 = 0.66667 hours.
30 * - You will arrive at the 2.66667 hour mark.
31 * 
32 * Example 3:
33 * 
34 * Input: dist = [1,3,2], hour = 1.9
35 * Output: -1
36 * Explanation: It is impossible because the earliest the third train can depart is at the 2 hour mark.
37 * 
38 *  
39 * Constraints:
40 * 
41 * 	n == dist.length
42 * 	1 <= n <= 10^5
43 * 	1 <= dist[i] <= 10^5
44 * 	1 <= hour <= 10^9
45 * 	There will be at most two digits after the decimal point in hour.
46 * 
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/minimum-speed-to-arrive-on-time/
51// discuss: https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56    pub fn min_speed_on_time(dist: Vec<i32>, hour: f64) -> i32 {
57        0
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_1870() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.