-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrange_II.cpp
More file actions
37 lines (31 loc) · 845 Bytes
/
Arrange_II.cpp
File metadata and controls
37 lines (31 loc) · 845 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
vector<vector<int>> dp;
int rec(int horse, int stable, string A, int K){
int n = A.size();
if (horse == n) {
if (stable == K)
return 0;
return INT_MAX;
}
if (stable == K)
return INT_MAX;
if (dp[horse][stable] != -1)
return dp[horse][stable];
int W = 0, B = 0, res = INT_MAX;
for (int i = horse; i < n; ++i){
W += (A[i] == 'W');
B += (A[i] == 'B');
if (W * B > res)
break;
int tmp = rec(i + 1, stable + 1, A, K);
if (tmp != INT_MAX)
res = min(res, tmp + (W * B));
}
return dp[horse][stable] = res;
}
int Solution::arrange(string A, int B) {
int n =A.size();
dp.clear();
dp.resize(n,vector<int>(B,-1));
int res = rec(0,0,A,B);
return res==INT_MAX ? -1 : res;
}