Posted on

Description

Submission

class Solution {
    bool canReach(int speed, vector<int>& dist, double hour) {
        double hoursNeeded = 0;
        for(int i = 0; i < dist.size() - 1; ++i) {
            hoursNeeded += (dist[i] - 1) / speed + 1;
        }
        hoursNeeded += dist.back() * 1.0 / speed;
        return hoursNeeded <= hour;
    }
public:
    int minSpeedOnTime(vector<int>& dist, double hour) {
        int left = 1, right = INT_MAX;

        while(left < right) {
            int mid = left + (right - left) / 2;

            if(canReach(mid, dist, hour)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }

        return left == INT_MAX ? -1 : left;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *