-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2463_Minimum_Total_Distance_Traveled.java
More file actions
38 lines (30 loc) 路 1.29 KB
/
Copy path2463_Minimum_Total_Distance_Traveled.java
File metadata and controls
38 lines (30 loc) 路 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public long minimumTotalDistance(List<Integer> robot, int[][] factory) {
Collections.sort(robot);
Arrays.sort(factory,(x,y)->Integer.compare(x[0],y[0]));
long[][]memo = new long[robot.size()][factory.length];
return dfs(0, 0,robot,factory,memo);
}
public long dfs(int robotIndex, int factoryIndex,List<Integer> robots, int[][] factories,long[][]memo) {
if (robotIndex == robots.size()) {
return 0;
}
if (factoryIndex == factories.length) {
return Long.MAX_VALUE / 1000;
}
if (memo[robotIndex][factoryIndex] != 0) {
return memo[robotIndex][factoryIndex];
}
long ans = dfs(robotIndex, factoryIndex + 1,robots,factories,memo);
long distanceSum = 0;
for (int k = 0; k < factories[factoryIndex][1]; ++k) {
if (robotIndex + k == robots.size()) {
break;
}
distanceSum += Math.abs(robots.get(robotIndex + k) - factories[factoryIndex][0]);
ans = Math.min(ans, distanceSum + dfs(robotIndex + k + 1, factoryIndex + 1,robots,factories,memo));
}
memo[robotIndex][factoryIndex] = ans;
return ans;
}
}