【LeetCode】 514. Freedom Trail 自由之路(Hard)(JAVA)
题目地址: https://leetcode.com/problems/freedom-trail/
题目描述:
In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial to spell a specific keyword in order to open the door.
Given a string ring, which represents the code engraved on the outer ring and another string key, which represents the keyword needs to be spelled. You need to find the minimum number of steps in order to spell all the characters in the keyword.
Initially, the first character of the ring is aligned at 12:00 direction. You need to spell all the characters in the string key one by one by rotating the ring clockwise or anticlockwise to make each character of the string key aligned at 12:00 direction and then by pressing the center button.
At the stage of rotating the ring to spell the key character key[i]:
- You can rotate the ring clockwise or anticlockwise one place, which counts as 1 step. The final purpose of the rotation is to align one of the string ring’s characters at the 12:00 direction, where this character must equal to the character key[i].
- If the character key[i] has been aligned at the 12:00 direction, you need to press the center button to spell, which also counts as 1 step. After the pressing, you could begin to spell the next character in the key (next stage), otherwise, you’ve finished all the spelling.
Example:
Input: ring = "godding", key = "gd"
Output: 4
Explanation:
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
Note:
- Length of both ring and key will be in range 1 to 100.
- There are only lowercase letters in both strings and might be some duplcate characters in both strings.
- It’s guaranteed that string key could always be spelled by rotating the string ring.
题目大意
视频游戏“辐射4”中,任务“通向自由”要求玩家到达名为“Freedom Trail Ring”的金属表盘,并使用表盘拼写特定关键词才能开门。
给定一个字符串 ring,表示刻在外环上的编码;给定另一个字符串 key,表示需要拼写的关键词。您需要算出能够拼写关键词中所有字符的最少步数。
最初,ring 的第一个字符与12:00方向对齐。您需要顺时针或逆时针旋转 ring 以使 key 的一个字符在 12:00 方向对齐,然后按下中心按钮,以此逐个拼写完 key 中的所有字符。
旋转 ring 拼出 key 字符 key[i] 的阶段中:
- 您可以将 ring 顺时针或逆时针旋转一个位置,计为1步。旋转的最终目的是将字符串 ring 的一个字符与 12:00 方向对齐,并且这个字符必须等于字符 key[i] 。
- 如果字符 key[i] 已经对齐到12:00方向,您需要按下中心按钮进行拼写,这也将算作 1 步。按完之后,您可以开始拼写 key 的下一个字符(下一阶段), 直至完成所有拼写。
解题方法
- 记录下所有字母对应的位置,这样在找字母相对位置的时候就不需要循环搜索了
- 采用递归的方法,找出当前字母对应的位置最小的步数:只需要把当前字母对应的所有位置找出来,然后计算最小值即可
- 下一个位置再次迭代计算即可
class Solution {
public int findRotateSteps(String ring, String key) {
Map<Character, List<Integer>> map = new HashMap<>();
int[][] dp = new int[ring.length()][key.length()];
for (int i = 0; i < ring.length(); i++) {
char temp = ring.charAt(i);
List<Integer> cur = map.get(temp);
if (cur == null) {
cur = new ArrayList<>();
map.put(temp, cur);
}
cur.add(i);
}
return fH(map, dp, ring, 0, key, 0);
}
public int fH(Map<Character, List<Integer>> map, int[][] dp, String ring, int rIndex, String key, int kIndex) {
if (kIndex >= key.length()) {
return 0;
}
if (dp[rIndex][kIndex] > 0) return dp[rIndex][kIndex];
List<Integer> nextList = map.get(key.charAt(kIndex));
int min = Integer.MAX_VALUE;
for (int i = 0; i < nextList.size(); i++) {
min = Math.min(min, fH(map, dp, ring, nextList.get(i), key, kIndex + 1) + minLen(ring.length(), nextList.get(i), rIndex) + 1);
}
dp[rIndex][kIndex] = min;
return dp[rIndex][kIndex];
}
public int minLen(int len, int i, int j) {
int min = Math.min(i, j);
int max = Math.max(i, j);
return Math.min(Math.abs(i - j), Math.abs(len + min - max));
}
}
执行耗时:9 ms,击败了91.78% 的Java用户
内存消耗:39.2 MB,击败了45.26% 的Java用户