Skip to content

Commit 12b78bb

Browse files
committed
Time: 15 ms (100%), Space: 17.8 MB (0%) - LeetHub
1 parent a235912 commit 12b78bb

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# time complexity: O(logn)
2+
# space complexity: O(1)
3+
from math import gcd
4+
from typing import List
5+
6+
7+
class Solution:
8+
def minimumTime(self, d: List[int], r: List[int]) -> int:
9+
d1, d2 = d
10+
r1, r2 = r
11+
12+
def lcm(a, b):
13+
return a * b // gcd(a, b)
14+
15+
LCM = lcm(r1, r2)
16+
17+
left, right = 0, 10**18
18+
result = right
19+
20+
while left <= right:
21+
mid = (left + right) // 2
22+
a1 = mid - mid // r1
23+
a2 = mid - mid // r2
24+
25+
total = mid - mid // LCM
26+
27+
if a1 >= d1 and a2 >= d2 and total >= d1 + d2:
28+
result = mid
29+
right = mid - 1
30+
else:
31+
left = mid + 1
32+
33+
return result
34+
35+
36+
d = [3, 1]
37+
r = [2, 3]
38+
print(Solution().minimumTime(d, r))
39+
d = [1, 3]
40+
r = [2, 2]
41+
print(Solution().minimumTime(d, r))
42+
d = [2, 1]
43+
r = [3, 4]
44+
print(Solution().minimumTime(d, r))

0 commit comments

Comments
 (0)