---
# System prepended metadata

title: 1870. Minimum Speed to Arrive on Time
tags: [Leetcode, Binary Search, Medium]

---

# 1870. Minimum Speed to Arrive on Time
###### tags: `Leetcode` `Medium` `Binary Search`
Link: https://leetcode.com/problems/minimum-speed-to-arrive-on-time/
## 思路 $O(NlogN)$ $O(1)$
## Code
```java=
class Solution {
    public int minSpeedOnTime(int[] dist, double hour) {
        int minHour = dist.length-1;
        if(hour<=minHour) return -1;
        int maxDist = 0;
        int start = 1, end = (int)1e7;
        while(start<end){
            int mid = start+(end-start)/2;
            double time = computeTime(dist, mid);
            if(time>hour){
                start = mid+1;
            }
            else end = mid;
        }
        return start;
    }
    private double computeTime(int[] dist, int speed){
        double time = 0;
        for(int i=0; i<dist.length-1; i++){
            time += Math.ceil((double)dist[i]/(double)speed);
        }
        time += (double)dist[dist.length-1]/(double)speed;
        return time;
    }
}
```