Skip to content

Commit f1374d2

Browse files
committed
+ problem 1870
1 parent d483435 commit f1374d2

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 1870. Minimum Speed to Arrive on Time
2+
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 <code>i<sup>th</sup></code> train ride.
3+
4+
Each train can only depart at an integer hour, so you may need to wait in between each train ride.
5+
6+
* For example, if the <code>1<sup>st</sup></code> train ride takes `1.5` hours, you must wait for an additional `0.5` hours before you can depart on the <code>2<sup>nd</sup></code> train ride at the 2 hour mark.
7+
8+
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*.
9+
10+
Tests are generated such that the answer will not exceed <code>10<sup>7</sup></code> and `hour` will have at most two digits after the decimal point.
11+
12+
#### Example 1:
13+
<pre>
14+
<strong>Input:</strong> dist = [1,3,2], hour = 6
15+
<strong>Output:</strong> 1
16+
<strong>Explanation:</strong> 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+
</pre>
22+
23+
#### Example 2:
24+
<pre>
25+
<strong>Input:</strong> dist = [1,3,2], hour = 2.7
26+
<strong>Output:</strong> 3
27+
<strong>Explanation:</strong> At speed 3:
28+
- The first train ride takes 1/3 = 0.33333 hours.
29+
- 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.
30+
- 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.
31+
- You will arrive at the 2.66667 hour mark.
32+
</pre>
33+
34+
#### Example 3:
35+
<pre>
36+
<strong>Input:</strong> dist = [1,3,2], hour = 1.9
37+
<strong>Output:</strong> -1
38+
<strong>Explanation:</strong> It is impossible because the earliest the third train can depart is at the 2 hour mark.
39+
</pre>
40+
41+
#### Constraints:
42+
* `n == dist.length`
43+
* <code>1 <= n <= 10<sup>5</sup></code>
44+
* <code>1 <= dist[i] <= 10<sup>5</sup></code>
45+
* <code>1 <= hour <= 10<sup>9</sup></code>
46+
* There will be at most two digits after the decimal point in `hour`.
47+
48+
## Solutions (Python)
49+
50+
### 1. Solution
51+
```Python
52+
from math import ceil
53+
54+
55+
class Solution:
56+
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
57+
if len(dist) - 0.999 > hour:
58+
return -1
59+
60+
lo = 1
61+
hi = 10000000
62+
63+
while lo < hi:
64+
v = (lo + hi) // 2
65+
t = sum(ceil(d / v) for d in dist[:-1]) + dist[-1] / v
66+
67+
if t > hour:
68+
lo = v + 1
69+
else:
70+
hi = v
71+
72+
return hi
73+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 1870. 准时到达的列车最小时速
2+
给你一个浮点数 `hour` ,表示你到达办公室可用的总通勤时间。要到达办公室,你必须按给定次序乘坐 `n` 趟列车。另给你一个长度为 `n` 的整数数组 `dist` ,其中 `dist[i]` 表示第 `i` 趟列车的行驶距离(单位是千米)。
3+
4+
每趟列车均只能在整点发车,所以你可能需要在两趟列车之间等待一段时间。
5+
6+
* 例如,第 `1` 趟列车需要 `1.5` 小时,那你必须再等待 `0.5` 小时,搭乘在第 `2` 小时发车的第 2 趟列车。
7+
8+
返回能满足你准时到达办公室所要求全部列车的 **最小正整数** 时速(单位:千米每小时),如果无法准时到达,则返回 `-1`
9+
10+
生成的测试用例保证答案不超过 <code>10<sup>7</sup></code> ,且 `hour`**小数点后最多存在两位数字**
11+
12+
#### 示例 1:
13+
<pre>
14+
<strong>输入:</strong> dist = [1,3,2], hour = 6
15+
<strong>输出:</strong> 1
16+
<strong>解释:</strong> 速度为 1 时:
17+
- 第 1 趟列车运行需要 1/1 = 1 小时。
18+
- 由于是在整数时间到达,可以立即换乘在第 1 小时发车的列车。第 2 趟列车运行需要 3/1 = 3 小时。
19+
- 由于是在整数时间到达,可以立即换乘在第 4 小时发车的列车。第 3 趟列车运行需要 2/1 = 2 小时。
20+
- 你将会恰好在第 6 小时到达。
21+
</pre>
22+
23+
#### 示例 2:
24+
<pre>
25+
<strong>输入:</strong> dist = [1,3,2], hour = 2.7
26+
<strong>输出:</strong> 3
27+
<strong>解释:</strong> 速度为 3 时:
28+
- 第 1 趟列车运行需要 1/3 = 0.33333 小时。
29+
- 由于不是在整数时间到达,故需要等待至第 1 小时才能搭乘列车。第 2 趟列车运行需要 3/3 = 1 小时。
30+
- 由于是在整数时间到达,可以立即换乘在第 2 小时发车的列车。第 3 趟列车运行需要 2/3 = 0.66667 小时。
31+
- 你将会在第 2.66667 小时到达。
32+
</pre>
33+
34+
#### 示例 3:
35+
<pre>
36+
<strong>输入:</strong> dist = [1,3,2], hour = 1.9
37+
<strong>输出:</strong> -1
38+
<strong>解释:</strong> 不可能准时到达,因为第 3 趟列车最早是在第 2 小时发车。
39+
</pre>
40+
41+
#### 提示:
42+
* `n == dist.length`
43+
* <code>1 <= n <= 10<sup>5</sup></code>
44+
* <code>1 <= dist[i] <= 10<sup>5</sup></code>
45+
* <code>1 <= hour <= 10<sup>9</sup></code>
46+
* `hours` 中,小数点后最多存在两位数字
47+
48+
## 题解 (Python)
49+
50+
### 1. 题解
51+
```Python
52+
from math import ceil
53+
54+
55+
class Solution:
56+
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
57+
if len(dist) - 0.999 > hour:
58+
return -1
59+
60+
lo = 1
61+
hi = 10000000
62+
63+
while lo < hi:
64+
v = (lo + hi) // 2
65+
t = sum(ceil(d / v) for d in dist[:-1]) + dist[-1] / v
66+
67+
if t > hour:
68+
lo = v + 1
69+
else:
70+
hi = v
71+
72+
return hi
73+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from math import ceil
2+
3+
4+
class Solution:
5+
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
6+
if len(dist) - 0.999 > hour:
7+
return -1
8+
9+
lo = 1
10+
hi = 10000000
11+
12+
while lo < hi:
13+
v = (lo + hi) // 2
14+
t = sum(ceil(d / v) for d in dist[:-1]) + dist[-1] / v
15+
16+
if t > hour:
17+
lo = v + 1
18+
else:
19+
hi = v
20+
21+
return hi

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,7 @@
966966
[1863][1863l]|[Sum of All Subset XOR Totals][1863] |![py]
967967
[1866][1866l]|[Number of Ways to Rearrange Sticks With K Sticks Visible][1866] |![rs]
968968
[1869][1869l]|[Longer Contiguous Segments of Ones than Zeros][1869] |![rs]
969+
[1870][1870l]|[Minimum Speed to Arrive on Time][1870] |![py]
969970
[1876][1876l]|[Substrings of Size Three with Distinct Characters][1876] |![rs]
970971
[1877][1877l]|[Minimize Maximum Pair Sum in Array][1877] |![rs]
971972
[1880][1880l]|[Check if Word Equals Summation of Two Words][1880] |![rs]
@@ -2260,6 +2261,7 @@
22602261
[1863]:Problemset/1863-Sum%20of%20All%20Subset%20XOR%20Totals/README.md#1863-sum-of-all-subset-xor-totals
22612262
[1866]:Problemset/1866-Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README.md#1866-number-of-ways-to-rearrange-sticks-with-k-sticks-visible
22622263
[1869]:Problemset/1869-Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README.md#1869-longer-contiguous-segments-of-ones-than-zeros
2264+
[1870]:Problemset/1870-Minimum%20Speed%20to%20Arrive%20on%20Time/README.md#1870-minimum-speed-to-arrive-on-time
22632265
[1876]:Problemset/1876-Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README.md#1876-substrings-of-size-three-with-distinct-characters
22642266
[1877]:Problemset/1877-Minimize%20Maximum%20Pair%20Sum%20in%20Array/README.md#1877-minimize-maximum-pair-sum-in-array
22652267
[1880]:Problemset/1880-Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README.md#1880-check-if-word-equals-summation-of-two-words
@@ -3557,6 +3559,7 @@
35573559
[1863l]:https://leetcode.com/problems/sum-of-all-subset-xor-totals/
35583560
[1866l]:https://leetcode.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/
35593561
[1869l]:https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/
3562+
[1870l]:https://leetcode.com/problems/minimum-speed-to-arrive-on-time/
35603563
[1876l]:https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/
35613564
[1877l]:https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/
35623565
[1880l]:https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,7 @@
966966
[1863][1863l]|[找出所有子集的异或总和再求和][1863] |![py]
967967
[1866][1866l]|[恰有 K 根木棍可以看到的排列数目][1866] |![rs]
968968
[1869][1869l]|[哪种连续子字符串更长][1869] |![rs]
969+
[1870][1870l]|[准时到达的列车最小时速][1870] |![py]
969970
[1876][1876l]|[长度为三且各字符不同的子字符串][1876] |![rs]
970971
[1877][1877l]|[数组中最大数对和的最小值][1877] |![rs]
971972
[1880][1880l]|[检查某单词是否等于两单词之和][1880] |![rs]
@@ -2260,6 +2261,7 @@
22602261
[1863]:Problemset/1863-Sum%20of%20All%20Subset%20XOR%20Totals/README_CN.md#1863-找出所有子集的异或总和再求和
22612262
[1866]:Problemset/1866-Number%20of%20Ways%20to%20Rearrange%20Sticks%20With%20K%20Sticks%20Visible/README_CN.md#1866-恰有-k-根木棍可以看到的排列数目
22622263
[1869]:Problemset/1869-Longer%20Contiguous%20Segments%20of%20Ones%20than%20Zeros/README_CN.md#1869-哪种连续子字符串更长
2264+
[1870]:Problemset/1870-Minimum%20Speed%20to%20Arrive%20on%20Time/README_CN.md#1870-准时到达的列车最小时速
22632265
[1876]:Problemset/1876-Substrings%20of%20Size%20Three%20with%20Distinct%20Characters/README_CN.md#1876-长度为三且各字符不同的子字符串
22642266
[1877]:Problemset/1877-Minimize%20Maximum%20Pair%20Sum%20in%20Array/README_CN.md#1877-数组中最大数对和的最小值
22652267
[1880]:Problemset/1880-Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README_CN.md#1880-检查某单词是否等于两单词之和
@@ -3557,6 +3559,7 @@
35573559
[1863l]:https://leetcode.cn/problems/sum-of-all-subset-xor-totals/
35583560
[1866l]:https://leetcode.cn/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/
35593561
[1869l]:https://leetcode.cn/problems/longer-contiguous-segments-of-ones-than-zeros/
3562+
[1870l]:https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/
35603563
[1876l]:https://leetcode.cn/problems/substrings-of-size-three-with-distinct-characters/
35613564
[1877l]:https://leetcode.cn/problems/minimize-maximum-pair-sum-in-array/
35623565
[1880l]:https://leetcode.cn/problems/check-if-word-equals-summation-of-two-words/

0 commit comments

Comments
 (0)