forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (35 loc) · 1.01 KB
/
Solution.java
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
class Solution {
private long[][] f;
private List<Integer> robot;
private int[][] factory;
public long minimumTotalDistance(List<Integer> robot, int[][] factory) {
Collections.sort(robot);
Arrays.sort(factory, (a, b) -> a[0] - b[0]);
this.robot = robot;
this.factory = factory;
f = new long[robot.size()][factory.length];
return dfs(0, 0);
}
private long dfs(int i, int j) {
if (i == robot.size()) {
return 0;
}
if (j == factory.length) {
return Long.MAX_VALUE / 1000;
}
if (f[i][j] != 0) {
return f[i][j];
}
long ans = dfs(i, j + 1);
long t = 0;
for (int k = 0; k < factory[j][1]; ++k) {
if (i + k == robot.size()) {
break;
}
t += Math.abs(robot.get(i + k) - factory[j][0]);
ans = Math.min(ans, t + dfs(i + k + 1, j + 1));
}
f[i][j] = ans;
return ans;
}
}