Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2dfe2f9

Browse files
committed
Create 2463-MinimumTotalDistanceTraveled.py
1 parent c3b6864 commit 2dfe2f9

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python3
2+
"""
3+
CREATED AT: 2022εΉ΄11月07ζ—₯
4+
5+
URL: https://leetcode.com/problems/minimum-total-distance-traveled/
6+
7+
GITHUB: https://github.com/Jiezhi/myleetcode
8+
9+
FileName: 2463-MinimumTotalDistanceTraveled
10+
11+
Difficulty: Hard
12+
13+
Desc:
14+
15+
Tag:
16+
17+
See:
18+
19+
"""
20+
from tool import *
21+
22+
23+
class Solution:
24+
def minimumTotalDistance(self, robot: List[int], factory: List[List[int]]) -> int:
25+
"""
26+
Ref: https://leetcode.cn/problems/minimum-total-distance-traveled/discuss/2783305/Python-DP-Solution
27+
Runtime: 2908 ms, faster than 50.00%
28+
Memory Usage: 445.2 MB, less than 100.00%
29+
1 <= robot.length, factory.length <= 100
30+
factory[j].length == 2
31+
-10^9 <= robot[i], positionj <= 10^9
32+
0 <= limitj <= robot.length
33+
The input will be generated such that it is always possible to repair every robot.
34+
"""
35+
36+
robot.sort()
37+
factory.sort()
38+
39+
@cache
40+
def dp(i, j, k) -> int:
41+
if i == len(robot):
42+
return 0
43+
if j == len(factory):
44+
return math.inf
45+
return min(dp(i, j + 1, 0),
46+
dp(i + 1, j, k + 1) + abs(robot[i] - factory[j][0]) if factory[j][1] > k else math.inf)
47+
48+
return dp(0, 0, 0)
49+
50+
def minimumTotalDistance2(self, robot: List[int], factory: List[List[int]]) -> int:
51+
"""
52+
1 <= robot.length, factory.length <= 100
53+
factory[j].length == 2
54+
-10^9 <= robot[i], positionj <= 10^9
55+
0 <= limitj <= robot.length
56+
The input will be generated such that it is always possible to repair every robot.
57+
"""
58+
cnt = Counter()
59+
for p, l in factory:
60+
if l > 0:
61+
cnt[p] = l
62+
rob = []
63+
for r in robot:
64+
if cnt[r] > 0:
65+
cnt[r] -= 1
66+
else:
67+
rob.append(r)
68+
69+
dq = collections.deque([(rob, cnt, 0)])
70+
ret = math.inf
71+
while dq:
72+
rob, cnt, step = dq.popleft()
73+
if not rob:
74+
ret = min(ret, step)
75+
continue
76+
rob_copy = rob.copy()
77+
r = rob_copy.pop()
78+
for fp in cnt.keys():
79+
cnt_copy = cnt.copy()
80+
if cnt_copy[fp] == 1:
81+
del cnt_copy[fp]
82+
else:
83+
cnt_copy[fp] -= 1
84+
dq.append((rob_copy, cnt_copy, step + abs(fp - r)))
85+
return ret
86+
87+
88+
def test():
89+
assert Solution().minimumTotalDistance(
90+
[670355988, 403625544, 886437985, 224430896, 126139936, -477101480, -868159607, -293937930],
91+
[[333473422, 7], [912209329, 7], [468372740, 7], [-765827269, 4], [155827122, 4], [635462096, 2],
92+
[-300275936, 2], [-115627659, 0]]) == 509199280
93+
assert Solution().minimumTotalDistance(robot=[0, 4, 6], factory=[[2, 2], [6, 2]]) == 4
94+
assert Solution().minimumTotalDistance(robot=[1, -1], factory=[[-2, 1], [2, 1]]) == 2
95+
96+
97+
if __name__ == '__main__':
98+
test()

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /