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.

Approach #1: DP. [C++]

class Solution {
public:
int findRotateSteps(string ring, string key) {
int N = ring.length();
int M = key.length();
int ans = INT_MAX;
// dp[i][j] denote means the minimum of steps to spell the key[0:i] which key[i] at the index of j in ring.
vector<vector<int>> dp(M, vector<int>(N, INT_MAX));
vector<vector<int>> char_idx(26, vector<int>());
for (int i = 0; i < N; ++i) {
int c = ring[i] - 'a';
char_idx[c].push_back(i);
}
for (int i = 0; i < M; ++i) {
int cur_char = key[i] - 'a';
for (int cur_pos : char_idx[cur_char]) {
if (i == 0) {
dp[i][cur_pos] = min(cur_pos, N - cur_pos) + 1;
} else {
int pre_char = key[i-1] - 'a';
for (int pre_pos : char_idx[pre_char]) {
int diff = min(abs(cur_pos-pre_pos), N - abs(cur_pos-pre_pos));
dp[i][cur_pos] = min(dp[i][cur_pos], dp[i-1][pre_pos]+diff+1);
//cout << dp[i][cur_pos] << endl;
}
}
if (i == M-1)
ans = min(ans, dp[M-1][cur_pos]);
}
}
return ans;
}
};

  

Analysis:

https://leetcode.com/problems/freedom-trail/discuss/194601/DP-solution-with-detailed-text-and-video-explanation

514. Freedom Trail的更多相关文章

  1. 514 Freedom Trail 自由之路

    详见:https://leetcode.com/problems/freedom-trail/description/ C++: class Solution { public: int findRo ...

  2. [LeetCode] Freedom Trail 自由之路

    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...

  3. [Swift]LeetCode514. 自由之路 | Freedom Trail

    In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...

  4. 动态规划——Freedom Trail

    题目:https://leetcode.com/problems/freedom-trail/ 额...不解释大意了,题目我也不想写过程了有点繁琐,直接给出代码: public int findRot ...

  5. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  6. LeetCode Weekly Contest 22

    1. 532. K-diff Pairs in an Array 分析:由于要唯一,所以要去重,考虑k=0,时候,相同的数字需要个数大于1,所以,先用map统计个数,对于k=0,特判,对于其他的,遍历 ...

  7. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

随机推荐

  1. Linux实战教学笔记53:开源虚拟化KVM(一)搭建部署与概述

    一,KVM概述 1.1 虚拟化概述 在计算机技术中,虚拟化意味着创建设备或资源的虚拟版本,如服务器,存储设备,网络或者操作系统等等 [x] 虚拟化技术分类: 系统虚拟化(我们主要讨论的反向) 存储虚拟 ...

  2. go_变量定义

    package main import "fmt" var( aa =3 bb ="kkk" cc =true )//go语言中,变量可以定义在函数外面,并不是 ...

  3. sql解决主键冲突

    在数据插入的时候,假设主键对应的值已经存在,则插入失败!这就是主键冲突.当主键存在冲突(duplicate key)的时候,可以选择性的进行处理,即忽略.更新或者替换. 1.忽略 insert ign ...

  4. 基于mapper插件编写的可定制代码生成基本框架(springboot)

    先看一下,基本结构图: 特征,提供 最佳实践的项目结构.配置文件.精简的POM 统一响应结果封装 统一异常处理 统一接口登录认证 常用基础方法抽象封装 Controller.service.dao层基 ...

  5. Apache CloudStack Features

    As a mature and turnkey Infrastructure-as-a-Service (IaaS) platform, Apache CloudStack has a compreh ...

  6. 访问tomcat出现java.lang.IllegalStateException No output folder错误解决方法

    访问tomcat出现java.lang.IllegalStateException: No output folder错误解决方法 问题:tomcat分为安装版和解压缩版,解压缩版如果解压到安装盘,在 ...

  7. 冲刺NOIP2015提高组复赛模拟试题(五) 3.破坏基地

    3.破坏基地 描述 Description 在Z国和W国之间一直战火不断. 好不容易,W国的间谍把完整的Z国的军事基地的地图到手了. 于是W国决定再次出击,一举击破Z国的防线. W国认真研究了Z国的地 ...

  8. python2中的__new__与__init__,新式类和经典类-乾颐堂

    在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...

  9. 实践作业4---DAY2阶段一。

    由于CSDN博客没有班级博客栏目,所以在该项功能上无法与博客园进行对比,我们将就CSDN和博客园的博文发布功能进行对比.我们就CSDN和博客园的博文发布页面.后台管理界面.发布新博客及界面进行了全面的 ...

  10. Jrebel 独立部署tomcat 远程同步项目

    一直在用 jrebel 感觉热部署的 功能,修改xml配置文件等,省去了很多的重新启动的时间. 由于偶然间发现 jrebel 还有remote 路由功能.这样,在服务器端用jrebel部署的项目和本地 ...