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

  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:

leetcode 514 Freedom Trail_第1张图片

 

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.

最初,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 的下一个字符(下一阶段), 直至完成所有拼写。
示例: 
输入: ring = "godding", key = "gd"
输出: 4
解释:
 对于 key 的第一个字符 'g',已经在正确的位置, 我们只需要1步来拼写这个字符。 
 对于 key 的第二个字符 'd',我们需要逆时针旋转 ring "godding" 2步使它变成 "ddinggo"。
 当然, 我们还需要1步进行拼写。
 因此最终的输出是 4。

 

解题思路:

      看了很多大神的解法,普遍的做法是动态规划,我目前理解的动态规划的本质就是用空间换时间,将一些可能会重复遇到的子问题结果保存,避免重复计算。可是这道题并不会遇到重复的子问题 , 总的来说应该和递归是一样的。可是用动态规划就不会TLE,用递归(深度优先搜索)就会TLE使我很是迷惑。。。

     对于key[i], ring中可能会有多个元素ring[k]==key[i]  ;

    对于任一个k,假设此时在ring的12点位置的是ring的 j 元素:

            dp[i][j] == min(dp[i][j] , step + dp[i+1][k] ;

   step: 因为可以顺时针和逆时针的旋转 , 所以从 j 元素转到 k 元素 有两种步数 abs(k - j) 和 ring.size() - abs(k - j) , 取较小的步数 ;    

for(int k = 0 ; k < ring.size() ; k++)
{
    if(ring[k] == key[i])
    {
        int step = min(abs(k - j) , ring.size() - abs(k - j));
        dp[i][j] = min(dp[i][j] , step + dp[i+1][k]) ;
    }
}

因为拼写也算一步,所以最后需要加上key的大小;

 

class Solution {
public:
    int findRotateSteps(string ring, string key) 
    {
        int len_r = ring.size() , len_k = key.size() ;
        vector> dp(len_k + 1 , vector(len_r , 0)) ;
        
        for(int i = len_k - 1 ; i >= 0 ; i--)
        {
            for(int j = 0 ; j < len_r ; j++)
            {
                dp[i][j] = INT_MAX ; // 
                for(int k = 0 ; k < len_r ; k++)
                {
                    if(ring[k] == key[i])
                    {
                        int step = min(abs(k - j) , len_r - abs(k - j)) ;
                        dp[i][j] = min(dp[i][j] , step + dp[i + 1][k]) ; //我这里经常忘记dp[i][j]需要初始化为INT_MAX ;
                    }
                }
            }
        }
        
        return dp[0][0] + len_k ;
    }
    
    
};

 

你可能感兴趣的:(leetcode)