Skip to content

Commit

Permalink
Update minimum-total-distance-traveled.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Nov 6, 2022
1 parent e15fe6e commit 481294d
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions Python/minimum-total-distance-traveled.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ def minimumTotalDistance(self, robot, factory):
for i in xrange(len(factory)):
prefix = 0
dq = collections.deque([(0, -1)])
new_dp = [float("inf")]*(len(robot))
for j in xrange(len(robot)):
prefix += abs(robot[j]-factory[i][0])
if j-dq[-1][1] == factory[i][1]+1:
dq.pop()
while dq and dq[0][0] >= dp[j]-prefix:
dq.popleft()
dq.appendleft((dp[j]-prefix, j))
new_dp[j] = dq[-1][0]+prefix
dp = new_dp
dp[j] = dq[-1][0]+prefix
return dp[-1]

0 comments on commit 481294d

Please sign in to comment.