|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# 1870. Minimum Speed to Arrive on Time |
| 4 | +# Medium |
| 5 | +# https://leetcode.com/problems/minimum-speed-to-arrive-on-time |
| 6 | + |
| 7 | +=begin |
| 8 | +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 ith train ride. |
| 9 | +Each train can only depart at an integer hour, so you may need to wait in between each train ride. |
| 10 | +For example, if the 1st train ride takes 1.5 hours, you must wait for an additional 0.5 hours before you can depart on the 2nd train ride at the 2 hour mark. |
| 11 | +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. |
| 12 | +Tests are generated such that the answer will not exceed 107 and hour will have at most two digits after the decimal point. |
| 13 | +
|
| 14 | +Example 1: |
| 15 | +Input: dist = [1,3,2], hour = 6 |
| 16 | +Output: 1 |
| 17 | +Explanation: At speed 1: |
| 18 | +- The first train ride takes 1/1 = 1 hour. |
| 19 | +- Since we are already at an integer hour, we depart immediately at the 1 hour mark. The second train takes 3/1 = 3 hours. |
| 20 | +- Since we are already at an integer hour, we depart immediately at the 4 hour mark. The third train takes 2/1 = 2 hours. |
| 21 | +- You will arrive at exactly the 6 hour mark. |
| 22 | +
|
| 23 | +Example 2: |
| 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 | +Input: dist = [1,3,2], hour = 1.9 |
| 34 | +Output: -1 |
| 35 | +Explanation: It is impossible because the earliest the third train can depart is at the 2 hour mark. |
| 36 | +
|
| 37 | +Constraints: |
| 38 | +* n == dist.length |
| 39 | +* 1 <= n <= 105 |
| 40 | +* 1 <= dist[i] <= 105 |
| 41 | +* 1 <= hour <= 109 |
| 42 | +* There will be at most two digits after the decimal point in hour. |
| 43 | +=end |
| 44 | + |
| 45 | +# @param {Integer[]} dist |
| 46 | +# @param {Float} hour |
| 47 | +# @return {Integer} |
| 48 | +def min_speed_on_time(dist, hour) |
| 49 | + return -1 if hour.ceil <= dist.size - 1 |
| 50 | + |
| 51 | + i, j = 1, 10**7 + 1 |
| 52 | + |
| 53 | + while i < j |
| 54 | + mid = (i + j) / 2 |
| 55 | + time = dist[0..-2].sum { |d| (d + mid - 1) / mid } + dist[-1] / mid.to_f |
| 56 | + if time > hour |
| 57 | + i = mid + 1 |
| 58 | + else |
| 59 | + j = mid |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + i == 10**7 + 1 ? -1 : i |
| 64 | +end |
| 65 | + |
| 66 | +# **************** # |
| 67 | +# TEST # |
| 68 | +# **************** # |
| 69 | + |
| 70 | +require "test/unit" |
| 71 | +class Test_min_speed_on_time < Test::Unit::TestCase |
| 72 | + def test_ |
| 73 | + assert_equal 1, min_speed_on_time([1, 3, 2], 6) |
| 74 | + assert_equal 3, min_speed_on_time([1, 3, 2], 2.7) |
| 75 | + assert_equal(-1, min_speed_on_time([1, 3, 2], 1.9)) |
| 76 | + assert_equal(-1, min_speed_on_time([1, 1], 1.0)) |
| 77 | + end |
| 78 | +end |
0 commit comments