Skip to content

Commit 2930d48

Browse files
add 514
1 parent 66a3a39 commit 2930d48

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Your ideas/fixes/algorithms are more than welcome!
8181
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../master/src/main/java/com/fishercoder/solutions/DetectCapital.java) | O(n) |O(1) | Easy|
8282
|516|[Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_516.java) | O(n^2) |O(n^2) | Medium| DP
8383
|515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)|[Solution](../master/src/main/java/com/fishercoder/solutions/FindLargestValueinEachTreeRow.java) | O(n) |O(k) | Medium| BFS
84+
|514|[Freedom Trail](https://leetcode.com/problems/freedom-trail/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_514.java) | O(?) |O(?) | Hard | DP
8485
|513|[Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_513.java) | O(n) |O(k) | Medium| BFS
8586
|508|[Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MostFrequentSubtreeSum.java) | O(n) |O(n) | Medium| DFS, Tree
8687
|507|[Perfect Number](https://leetcode.com/problems/perfect-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/PerfectNumber.java) | O(sqrt(n)) |O(1) | Easy| Math
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 514. Freedom Trail
5+
*
6+
* In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called
7+
* the "Freedom Trail Ring", and use the dial to spell a specific keyword in order to open the door.
8+
9+
Given a string ring, which represents the code engraved on the outer ring and another string key,
10+
which represents the keyword needs to be spelled.
11+
You need to find the minimum number of steps in order to spell all the characters in the keyword.
12+
13+
Initially, the first character of the ring is aligned at 12:00 direction.
14+
You need to spell all the characters in the string key one by one by
15+
rotating the ring clockwise or anticlockwise to make each character of the string key
16+
aligned at 12:00 direction and then by pressing the center button.
17+
18+
At the stage of rotating the ring to spell the key character key[i]:
19+
You can rotate the ring clockwise or anticlockwise one place, which counts as 1 step.
20+
The final purpose of the rotation is to align one of the string ring's characters at the 12:00 direction,
21+
where this character must equal to the character key[i].
22+
If the character key[i] has been aligned at the 12:00 direction,
23+
you need to press the center button to spell, which also counts as 1 step.
24+
After the pressing, you could begin to spell the next character in the key (next stage), otherwise, you've finished all the spelling.
25+
26+
Example:
27+
Input: ring = "godding", key = "gd"
28+
Output: 4
29+
Explanation:
30+
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
31+
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
32+
Also, we need 1 more step for spelling.
33+
So the final output is 4.
34+
35+
Note:
36+
Length of both ring and key will be in range 1 to 100.
37+
There are only lowercase letters in both strings and might be some duplcate characters in both strings.
38+
It's guaranteed that string key could always be spelled by rotating the string ring.
39+
*/
40+
public class _514 {
41+
42+
public int findRotateSteps(String ring, String key) {
43+
int n = ring.length();
44+
int m = key.length();
45+
int[][] dp = new int[m+1][n];
46+
for (int i = m-1; i >= 0; i--) {
47+
for (int j = 0; j < n; j++) {
48+
dp[i][j] = Integer.MAX_VALUE;
49+
for (int k = 0; k < n; k++) {
50+
if (ring.charAt(k) == key.charAt(i)) {
51+
int diff = Math.abs(j - k);
52+
int step = Math.min(diff, n - diff);
53+
dp[i][j] = Math.min(dp[i][j], step + dp[i+1][k]);
54+
}
55+
}
56+
}
57+
}
58+
return dp[0][0]+m;
59+
}
60+
61+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy