Skip to content

Commit 2601c72

Browse files
committed
[LeetCode Sync] Runtime - 1144 ms (21.46%), Memory - 21.9 MB (49.43%)
1 parent 9eafe24 commit 2601c72

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>You are given an integer array <code>ranks</code> representing the <strong>ranks</strong> of some mechanics. <font face="monospace">ranks<sub>i</sub></font> is the rank of the <font face="monospace">i<sup>th</sup></font> mechanic<font face="monospace">.</font> A mechanic with a rank <code>r</code> can repair <font face="monospace">n</font> cars in <code>r * n<sup>2</sup></code> minutes.</p>
2+
3+
<p>You are also given an integer <code>cars</code> representing the total number of cars waiting in the garage to be repaired.</p>
4+
5+
<p>Return <em>the <strong>minimum</strong> time taken to repair all the cars.</em></p>
6+
7+
<p><strong>Note:</strong> All the mechanics can repair the cars simultaneously.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> ranks = [4,2,3,1], cars = 10
14+
<strong>Output:</strong> 16
15+
<strong>Explanation:</strong>
16+
- The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
17+
- The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
18+
- The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
19+
- The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
20+
It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> ranks = [5,1,8], cars = 6
27+
<strong>Output:</strong> 16
28+
<strong>Explanation:</strong>
29+
- The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
30+
- The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
31+
- The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
32+
It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>1 &lt;= ranks.length &lt;= 10<sup>5</sup></code></li>
40+
<li><code>1 &lt;= ranks[i] &lt;= 100</code></li>
41+
<li><code>1 &lt;= cars &lt;= 10<sup>6</sup></code></li>
42+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def repairCars(self, ranks: List[int], cars: int) -> int:
3+
# The minimum possible time is 1,
4+
# and the maximum possible time is when the slowest mechanic (highest rank) repairs all cars.
5+
low, high = 1, cars * cars * ranks[0]
6+
7+
# Perform binary search to find the minimum time required.
8+
while low < high:
9+
mid = (low + high) // 2
10+
cars_repaired = sum(int((mid / rank) ** 0.5) for rank in ranks)
11+
12+
# If the total cars repaired is less than required, increase the time.
13+
if cars_repaired < cars:
14+
low = mid + 1
15+
else:
16+
high = mid # Otherwise, try a smaller time.
17+
18+
return low

0 commit comments

Comments
 (0)