[LeetCode]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'scharacters 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.

老生常谈的做法 ,普通dfs会超时,记忆化dfs或则dp解题

普通dfs超时代码:

public class Solution {
	  //普通dfs  会超时
	  public int findRotateSteps(String ring, String key) {
		  int len=ring.length();
		  HashMap<Character,HashSet<Integer>> map=new HashMap<Character, HashSet<Integer>>();
		  for(int i=0;i<ring.length();i++){
			  if(!map.containsKey(ring.charAt(i))){
				  map.put(ring.charAt(i),new HashSet<Integer>());
			  }
			  map.get(ring.charAt(i)).add(i);
		  }
		  dfs(0,key,map,len,0);
		  return re==Integer.MAX_VALUE?-1:re+key.length();
	  }
	  int trace=0;
	  int re=Integer.MAX_VALUE;
	  public void dfs(int k,String key,HashMap<Character,HashSet<Integer>> map,int len,int pos){
		  if(k==key.length()){
			  re=Math.min(trace, re);
			  return;
		  }
		  HashSet<Integer> level=map.get(key.charAt(k));
		  for(Integer num:level){
			  int dis=Math.min(Math.abs(num-pos), len-Math.abs(num-pos));//这里不是Math.abs(len-num+pos)
			  trace+=dis;
			  dfs(k+1,key,map,len,num);
			  trace-=dis;
			  
		  }
	  }
	  public static void main(String[] args) {
		  String ring="caotmcaataijjx";
		  String key="oatjiioicitatajtijciocjcaaxaaatmctxamacaamjjx";
		  Solution s=new Solution();
		  System.out.println(s.findRotateSteps(ring, key));
	}
}

记忆化dfs代码:
public class Solution2 {
	//一些分析:
	// 对于dfs而言  它永远是对图的搜索(决策树也是图) 图的每一个顶点也就是每一个状态 
	//本题所用的dp写法是自上而下的  其实自下而上是和dfs的逻辑更相符合的  即从ring.length-1向前dp 
	//但是由于本题  从上而下和从下而上是一模一样的 所以没有必要纠结于dp的写法 
	//在记忆化dfs的代码中 dp[][] 表示从(i,j)位置到最终路径的最小距离
	//而在dp写法中  dp[][] 则表示从(0,0)到(i,j)的距离
	  public int findRotateSteps(String ring, String key) {
		  int len=ring.length();
		  HashMap<Character,HashSet<Integer>> map=new HashMap<Character, HashSet<Integer>>();
		  dp=new int[key.length()][ring.length()];
		  for(int i=0;i<dp.length;i++) Arrays.fill(dp[i],Integer.MAX_VALUE);
		  for(int i=0;i<ring.length();i++){
			  if(!map.containsKey(ring.charAt(i))){
				  map.put(ring.charAt(i),new HashSet<Integer>());
			  }
			  map.get(ring.charAt(i)).add(i);
		  }
	  	  int re=dfs(0,key,map,len,0);
		  return re==Integer.MAX_VALUE?-1:re+key.length();
	  }
	  int[][] dp;
	  public int dfs(int k,String key,HashMap<Character,HashSet<Integer>> map,int len,int pos){
		  if(k==key.length()){
			  return 0;
		  }
		  if(dp[k][pos]!=Integer.MAX_VALUE) return dp[k][pos];
		  HashSet<Integer> level=map.get(key.charAt(k));
		  for(Integer num:level){
			  int dis=Math.min(Math.abs(num-pos), len-Math.abs(num-pos));//这里不是Math.abs(len-num+pos)
			  int re=dfs(k+1,key,map,len,num);
			  dp[k][pos]=Math.min(re+dis, dp[k][pos]);
		  }
		  return dp[k][pos];
	  }
}
dp代码:
public class Solution3 {
	public int findRotateSteps(String ring, String key) {
		int[][] dp=new int[key.length()][ring.length()];
		for(int i=0;i<dp.length;i++){
			Arrays.fill(dp[i],Integer.MAX_VALUE);
		}
		for(int i=0;i<dp.length;i++){
			for(int j=0;j<dp[0].length;j++){
				if(ring.charAt(j)==key.charAt(i)){
					for(int k=0;k<dp[0].length;k++){
						if(i==0)
							dp[i][j]=Math.min(j, ring.length()-j);
						else{
							if(ring.charAt(k)==key.charAt(i-1))
								dp[i][j]=Math.min(dp[i][j],dp[i-1][k]+Math.min(Math.abs(j-k),ring.length()-Math.abs(j-k)));
						}
					}
				}
			}
		}
		int re=Integer.MAX_VALUE;
		for(int i=0;i<dp[0].length;i++){
			re=Math.min(re,dp[dp.length-1][i]);
		}
		return re==Integer.MAX_VALUE?-1:re+key.length();
	}
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值