-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode2463.cpp
More file actions
60 lines (29 loc) · 1015 Bytes
/
leetcode2463.cpp
File metadata and controls
60 lines (29 loc) · 1015 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public:
using ll = long long ;
const ll INF = 4e14;
ll solve(int i ,int j , vector<int>& robot, vector<vector<int>>& factory,vector<vector<ll>>& dp){
int n = robot.size();
int m = factory.size();
if(i == n) return 0;
if(j == m ) return INF;
if(dp[i][j] != -1) return dp[i][j];
ll ans = solve(i,j+1,robot,factory,dp);
ll cost =0 ;
int pos = factory[j][0];
int limit = factory[j][1];
for(int k = 1; k<= limit && i+k-1 <n ; k++){
cost+= abs((ll)robot[i+k-1]-pos) ;
ans = min(ans ,cost+solve(i+k,j+1,robot,factory,dp));
}
return dp[i][j] = ans ;
}
long long minimumTotalDistance(vector<int>& robot, vector<vector<int>>& factory) {
sort(robot.begin() , robot.end());
sort(factory.begin() , factory.end());
int n = robot.size();
int m = factory.size();
vector<vector<ll>> dp(n+1,vector<ll>(m+1,-1));
return solve(0,0,robot,factory,dp);
}
};