514-Freedom Trail

Description:
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.

问题描述:

给定一串密码key和一串环状的铭文ring,你可以顺时针或逆时针旋转铭文,12点方向的字符为当前可以选择的字符,计数方式为每旋转一次计1步,每确定一次计一步。需要返回通过这种方式获得密码的最小步数。

例子:
输入为ring = “godding”, key = “gd”
输出为4
解释:
十二点钟方向为g,确认+1,顺时针旋转2步,+2,点击确定,+1,,总计4步。

解法1(dfs + memorization)

/*
*假设ring的起始位置为ringIndex, key已经破解到第targetIndex位,那么我们可以把这种状态
*进行分解,令c = key[targetIndex],那么c即为我们需要破解的当前字符,于是在ring
*中可能存在多个字符可以匹配,那么就有多个下标,令其为i,于是可以往下递归,这次递归
*ring的起始位置为i,key破解到targetIndex + 1位。既然我们要找的是最小步数,那么存在
*选择问题。多个下标有多种步数,我们需要从中选择最小的那个。
*
*/
class Solution {
    public int findRotateSteps(String ring, String key) {
        char[] ringArr = ring.toCharArray();
        char[] keyArr = key.toCharArray();

        return dfs(ringArr, keyArr, 0, 0, new int[ringArr.length][keyArr.length]);
    }
    private  int dfs(char[] ring, char[] target, int targetIndex, int ringIndex, int[][] memo){
        if(targetIndex == target.length)    return 0;
        //memo保存了环的位置为ringIndex,密码位置为targetIndex的状态
        if(memo[ringIndex][targetIndex] != 0)   return memo[ringIndex][targetIndex];

        int min = Integer.MAX_VALUE;
        for(int i = 0;i < ring.length;i++){
            if(ring[i] != target[targetIndex])  continue;

            int dif = Math.abs(i - ringIndex);
            //注意这里
            int distance =  1 + Math.min(dif, ring.length - dif) + dfs(ring, target, targetIndex + 1, i, memo);
            min = Math.min(min, distance);            
        }

        memo[ringIndex][targetIndex] = min;
        return min;
    }
}

dfs+memorization的解法实际比dp还要快,28ms,beats 100%。

解法2(动态规划):

/*
*递归是top-down。动规是bottom-up
*dp[i][j],i代表密码的下标,j代表ring的下标
*/
public class Solution {
    public int findRotateSteps(String ring, String key) {
        int n = ring.length();
        int m = key.length();
        int[][] dp = new int[m + 1][n];

        for (int i = m - 1; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = Integer.MAX_VALUE;
                for (int k = 0; k < n; k++) {
                    if (ring.charAt(k) == key.charAt(i)) {
                        int diff = Math.abs(j - k);
                        int step = Math.min(diff, n - diff);
                        dp[i][j] = Math.min(dp[i][j], step + dp[i + 1][k]);
                    }
                }
            }
        }
        //注意这里,还需要m次确认
        return dp[0][0] + m;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值