Skip to content

Commit 19a5c82

Browse files
authored
Create 514. Freedom Trail (#467)
2 parents ee439d4 + a4fed2a commit 19a5c82

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

514. Freedom Trail

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution
2+
{
3+
public:
4+
int method(int i1,int i2, string ring, string key,int m,int n, vector<vector<int>>&dp)
5+
{
6+
if(i2==n)
7+
return 0;
8+
if(dp[i1][i2]!=-1)
9+
return dp[i1][i2];
10+
int ans = INT_MAX;
11+
for(int i=0;i<m;i++)
12+
{
13+
if(ring[i]==key[i2])
14+
{
15+
int count = min(abs(i1-i),m-abs(i1-i));
16+
ans = min(ans, count + method(i, i2+1, ring, key, m, n,dp));
17+
}
18+
}
19+
return dp[i1][i2]=ans;
20+
}
21+
int findRotateSteps(string ring, string key)
22+
{
23+
int i1=0,i2=0;
24+
int n = key.size();
25+
int m = ring.size();
26+
vector<vector<int>>dp(m, vector<int>(n,-1));
27+
return method(i1,i2,ring, key,m,n,dp)+n;
28+
}
29+
};

0 commit comments

Comments
 (0)