Skip to content

Latest commit

 

History

History
113 lines (97 loc) · 4.86 KB

README.md

File metadata and controls

113 lines (97 loc) · 4.86 KB

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]:

  1. 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].
  2. 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:

  1. Length of both ring and key will be in range 1 to 100.
  2. There are only lowercase letters in both strings and might be some duplcate characters in both strings.
  3. It's guaranteed that string key could always be spelled by rotating the string ring.

Related Topics:
Divide and Conquer, Dynamic Programming, Depth-first Search

Solution 1. DP

// OJ: https://leetcode.com/problems/freedom-trail/
// Author: github.com/lzl124631x
// Time: O(N * M^2)
// Space: O(MN)
// Ref: https://leetcode.com/problems/freedom-trail/discuss/98902/Concise-Java-DP-Solution
class Solution {
public:
    int findRotateSteps(string ring, string key) {
        int M = ring.size(), N = key.size();
        vector<vector<int>> dp(N + 1, vector<int>(M));
        for (int i = N - 1; i >= 0; --i) {
            for (int j = 0; j < M; ++j) {
                dp[i][j] = INT_MAX;
                for (int k = 0; k < M; ++k) {
                    if (ring[k] != key[i]) continue;
                    int diff = abs(j - k);
                    int step = min(diff, M - diff);
                    dp[i][j] = min(dp[i][j], step + dp[i + 1][k]);
                }
            }
        }
        return dp[0][0] + N;
    }
};

Solution 2. DP

// OJ: https://leetcode.com/problems/freedom-trail/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class Solution {
    // d[0] clockwise, d[1] anticlockwise
    int d[100][26][2], dp[100][100] = {0};
    void buildDist(string &ring) {
        int idx[26] = {-1}, N = ring.size();
        for (int i = 0; i < N; ++i) idx[ring[i] - 'a'] = i;
        for (int i = 0; i < N; ++i) {
            idx[ring[i] - 'a'] = i;
            for (int j = 0; j < 26; ++j) d[i][j][1] = idx[j];
        }
        for (int i = N - 1; i >= 0; --i) idx[ring[i] - 'a'] = i;
        for (int i = N - 1; i >= 0; --i) {
            idx[ring[i] - 'a'] = i;
            for (int j = 0; j < 26; ++j) d[i][j][0] = idx[j];
        }
    }
    int lookUp(string &ring, string &key, int i, int j) {
        if (j == key.size()) return 0;
        if (dp[i][j] != 0) return dp[i][j];
        if (ring[i] == key[j]) return dp[i][j] = 1 + lookUp(ring, key, i, j + 1);
        int N = ring.size(), ans = INT_MAX;
        for (int t = 0; t < 2; ++t) {
            int to = d[i][key[j] - 'a'][t];
            int dst = (to - i) * (t == 0 ? 1 : -1) % N;
            if (dst < 0) dst += N;
            ans = min(ans, dst + 1 + lookUp(ring, key, to, j + 1));
        }
        return dp[i][j] = ans;
    }
public:
    int findRotateSteps(string ring, string key) {
        buildDist(ring);
        return lookUp(ring, key, 0, 0);
    }
};