|
| 1 | +// Source : https://leetcode.com/problems/minimum-speed-to-arrive-on-time/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-05-30 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given a floating-point number hour, representing the amount of time you have to reach the |
| 8 | + * office. To commute to the office, you must take n trains in sequential order. You are also given an |
| 9 | + * integer array dist of length n, where dist[i] describes the distance (in kilometers) of the i^th |
| 10 | + * train ride. |
| 11 | + * |
| 12 | + * Each train can only depart at an integer hour, so you may need to wait in between each train ride. |
| 13 | + * |
| 14 | + * For example, if the 1^st train ride takes 1.5 hours, you must wait for an additional 0.5 |
| 15 | + * hours before you can depart on the 2^nd train ride at the 2 hour mark. |
| 16 | + * |
| 17 | + * Return the minimum positive integer speed (in kilometers per hour) that all the trains must travel |
| 18 | + * at for you to reach the office on time, or -1 if it is impossible to be on time. |
| 19 | + * |
| 20 | + * Tests are generated such that the answer will not exceed 10^7 and hour will have at most two digits |
| 21 | + * after the decimal point. |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Input: dist = [1,3,2], hour = 6 |
| 26 | + * Output: 1 |
| 27 | + * Explanation: At speed 1: |
| 28 | + * - The first train ride takes 1/1 = 1 hour. |
| 29 | + * - Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second |
| 30 | + * train takes 3/1 = 3 hours. |
| 31 | + * - Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third |
| 32 | + * train takes 2/1 = 2 hours. |
| 33 | + * - You will arrive at exactly the 6 hour mark. |
| 34 | + * |
| 35 | + * Example 2: |
| 36 | + * |
| 37 | + * Input: dist = [1,3,2], hour = 2.7 |
| 38 | + * Output: 3 |
| 39 | + * Explanation: At speed 3: |
| 40 | + * - The first train ride takes 1/3 = 0.33333 hours. |
| 41 | + * - Since we are not at an integer hour, we wait until the 1 hour mark to depart. The second train |
| 42 | + * ride takes 3/3 = 1 hour. |
| 43 | + * - Since we are already at an integer hour, we depart immediately at the 2 hour mark. The third |
| 44 | + * train takes 2/3 = 0.66667 hours. |
| 45 | + * - You will arrive at the 2.66667 hour mark. |
| 46 | + * |
| 47 | + * Example 3: |
| 48 | + * |
| 49 | + * Input: dist = [1,3,2], hour = 1.9 |
| 50 | + * Output: -1 |
| 51 | + * Explanation: It is impossible because the earliest the third train can depart is at the 2 hour mark. |
| 52 | + * |
| 53 | + * Constraints: |
| 54 | + * |
| 55 | + * n == dist.length |
| 56 | + * 1 <= n <= 10^5 |
| 57 | + * 1 <= dist[i] <= 10^5 |
| 58 | + * 1 <= hour <= 10^9 |
| 59 | + * There will be at most two digits after the decimal point in hour. |
| 60 | + ******************************************************************************************************/ |
| 61 | + |
| 62 | +class Solution { |
| 63 | +public: |
| 64 | + bool verify(vector<int>& dist, double hour, int speed) { |
| 65 | + double t = 0; |
| 66 | + int n = dist.size(); |
| 67 | + for (int i=0; i<n-1; i++){ |
| 68 | + t += (dist[i] + speed -1) / speed; |
| 69 | + } |
| 70 | + t += (double)dist[n-1]/speed; |
| 71 | + return t <= hour; |
| 72 | + } |
| 73 | + |
| 74 | + int minSpeedOnTime(vector<int>& dist, double hour) { |
| 75 | + int n = dist.size(); |
| 76 | + if (hour <= n-1) return -1; |
| 77 | + |
| 78 | + int low = 1, high = 1e7; |
| 79 | + while (low < high) { |
| 80 | + int mid = low + (high - low) / 2; |
| 81 | + if (verify(dist, hour, mid)) { |
| 82 | + high = mid; |
| 83 | + } else { |
| 84 | + low = mid + 1; |
| 85 | + } |
| 86 | + } |
| 87 | + return high; |
| 88 | + } |
| 89 | +}; |
0 commit comments