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.

一个字符串ring,首字符在12点方向,它可以顺时针或逆时针转,要转出字符串key中的所有字符,每转出一个字符,按一下中间按钮。
转盘转动时,每经过一个字符算一个step,按中间按钮也算一个step。
问最终转出key至少要经过多少step

思路:
参考方法

转盘的转动要靠想象了,假设字符串ring的长度是R,刚开始在12点处的应该是index=0的位置,假设现在想转到key的第一个字符G,可以就保持不动直接就是G,也可以逆时针转一位。就是说有两个G可以转,那么需要先标记每个G在什么位置,比如说在位置 i,那么顺时针转到i就需要i step,逆时针就需要R-i step。这是G转到key的index0的一步,记为dp[G][0], 因为G有两个位置,要遍历这两个位置,选出较小的一个,因此dp[G][0] = min(i, R-i) + 1, 加1是要按中间那个按钮

上面是第一步,后面的就需要知道上一步在哪个位置了,假设上一步在pre_i, 这一步就是cur_i, 遍历上一步所有可能的位置,求dp[pre_i][cur_i],
从pre_i顺时针到cur_i的step数是abs(pre_i - cur_i), 逆时针是R - abs(pre_i-cur_i) ,这两者选出较小的,当然这选出的只是遍历到的一个位置的较小值,最终的 dp[pre_i][cur_i] 要从遍历的所有里面选出最小值

当cur_i是key的最后一个字符的时候,就把从遍历pre_i中选出的最小值作为结果。

因为字母只有26个,可以给每个字母出现在ring中的位置提前保存起来,便于后面使用。

    public int findRotateSteps(String ring, String key) {if(ring == null || key == null) {return 0;}int R = ring.length();int K = key.length();int result = Integer.MAX_VALUE;int[][] dp = new int[R][K];List<List<Integer>> char_pos = new ArrayList<>();for(int i = 0; i < 26; i++) {char_pos.add(new ArrayList<Integer>());}for(int i = 0; i < R; i++) {Arrays.fill(dp[i], Integer.MAX_VALUE);}for(int i = 0; i < R; i++) {//key中每个char对应的位置char_pos.get(ring.charAt(i)-'a').add(i);}for(int i = 0; i < K; i++) {int cur = key.charAt(i) - 'a';for(Integer cur_pos : char_pos.get(cur)) {if(i == 0) {dp[cur_pos][0] = Math.min(cur_pos, R - cur_pos) + 1;} else {int pre = key.charAt(i-1) - 'a';for(Integer pre_pos : char_pos.get(pre)) {int diff = Math.min(Math.abs(pre_pos - cur_pos), R - Math.abs(pre_pos-cur_pos));dp[cur_pos][i] = Math.min(dp[cur_pos][i], dp[pre_pos][i-1]+diff+1);}}if(i == K-1) {result = Math.min(result, dp[cur_pos][i]);}}}return result;}

leetcode 514. Freedom Trail(自由之路)相关推荐

  1. Leetcode每日一题:514.freedom-trail(自由之路)

    思路:首先想到的就是深度优先搜索,从ring[i]顺时针到达key[j]的路径和逆时针的路径都尝试一遍,最终肯定能得到最短路径,但肯定会超时,因为这个复杂度是幂级的,即每个点都有两种可能: 所以,需要 ...

  2. LeetCode 514. 自由之路(记忆化递归 / DP)

    文章目录 1. 题目 2. 解题 1. 题目 电子游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring" ...

  3. 【Leetcode 每日一题】514. 自由之路(BFS+优先队列)

    Leetcode 每日一题 题目链接:514. 自由之路 难度: 困难 解题思路: 这道题乍一看,可以选择用动态规划或者BFS来求解.本文使用BFS来进行解答.注意到题中有一个最小的到路径.所以我们可 ...

  4. leetcode 动态规划 514. 自由之路

    电子游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写特定关键词才能开门. ...

  5. 2021-08-08:自由之路。电子游戏“辐射4”中,任务“通向自由”要求玩家到达名为“Freedom Trail Ring”的金属表盘,并使用表盘拼写特定关键词才能开门。给定一个字符串 ring,表

    2021-08-08:自由之路.电子游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并 ...

  6. leetcode:514. 自由之路

    题目来源 leetcode:514. 自由之路 题目描述 class Solution {public:int findRotateSteps(string ring, string key) {} ...

  7. leetcode【每日一题】514. 自由之路 Java【待完成

    题干 视频游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写特定关键词才能 ...

  8. 【LeetCode】自由之路 [H](记忆化搜索)

    514. 自由之路 - 力扣(LeetCode) 一.题目 电子游戏"辐射4"中,任务 "通向自由" 要求玩家到达名为 "Freedom Trail ...

  9. leecode 514. 自由之路

    题目描述 电子游戏"辐射4"中,任务 "通向自由" 要求玩家到达名为 "Freedom Trail Ring" 的金属表盘,并使用表盘拼写特 ...

最新文章

  1. 城市仿真为何成为大势所趋?
  2. 一个aov网用邻接矩阵表示_关注讲述我和朱婷7年7个故事,见证一个明星代表的诞生...
  3. 用PHP获取土豆网视频FLV地址
  4. yyblog2.0 数据库开发规范
  5. 合并两个数组并去重(ES5和ES6两种方式实现)
  6. Linux软件管理之yum
  7. 访问不了html的内容,index.html文件内容与实际访问结果不同
  8. 《深入浅出通信原理》读书笔记系列1-第2章 信号与频谱
  9. 小米路由器4a开发版固件_小米路由器 4A 刷入lean 的 openwrt/lede
  10. C# AForge视频录像
  11. 迈普交换机中断计算机网络,迈普3100交换机配置命令大全
  12. excel如何快速录入身份证号码?
  13. 复杂网络分析——networkx的使用
  14. 沉浸其境,共赴云栖数智硬核美学
  15. 内网主机通过公网域名解析访问内网服务器,存在什么问题,如何解决?
  16. 金士顿固态硬盘不认盘问题的开盘修复完整过程
  17. python round_Python round() 函数
  18. 如何做一个靠谱的产品经理
  19. 推荐5款Windows桌面效率工具
  20. Ubuntu 18.04安装openJDK7编译安卓6.0.0_r1

热门文章

  1. Pandas Dataframe 每隔n行取1行
  2. MG323所有命令使用
  3. POJ 1984 Navigation Nightmare 多权值并查集
  4. php 生成斜体字,JavaScript italics方法入门实例(把字符串显示为斜体)
  5. keras的简单介绍
  6. 《嵌入式linux内存使用与性能优化》读书笔记
  7. iOS-关于M1芯片可以下载APP使用问题
  8. 科学家称五年内将3D打印人类心脏
  9. 关于斐波那契数列那些事儿~
  10. 苹果邮箱怎么登录qq邮箱_gmail邮箱登录官网方法