-
Notifications
You must be signed in to change notification settings - Fork 0
/
514. Freedom Trail.cpp
38 lines (30 loc) · 1.04 KB
/
514. Freedom Trail.cpp
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:
int t[101][101];
int countSteps(int ringIndex, int i, int n) {
int dist = abs(i - ringIndex);
int wrapAround = n - dist;
return min(dist, wrapAround);
}
int solve(int ringIndex, int keyIndex, string& ring, string& key) {
if (keyIndex == key.length()) {
return 0;
}
if(t[ringIndex][keyIndex] != -1) {
return t[ringIndex][keyIndex];
}
int result = INT_MAX;
for (int i = 0; i < ring.length(); i++) {
if (ring[i] == key[keyIndex]) {
int totalSteps = countSteps(ringIndex, i, ring.length()) + 1 +
solve(i, keyIndex + 1, ring, key);
result = min(result, totalSteps);
}
}
return t[ringIndex][keyIndex] = result;
}
int findRotateSteps(string ring, string key) {
memset(t, -1, sizeof(t));
return solve(0, 0, ring, key);
}
};