Java实现 LeetCode 514 自由之路
514. 自由之路
视频游戏“辐射4”中,任务“通向自由”要求玩家到达名为“Freedom Trail Ring”的金属表盘,并使用表盘拼写特定关键词才能开门。
给定一个字符串 ring,表示刻在外环上的编码;给定另一个字符串 key,表示需要拼写的关键词。您需要算出能够拼写关键词中所有字符的最少步数。
最初,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。
提示:
ring 和 key 的字符串长度取值范围均为 1 至 100;
两个字符串中都只有小写字符,并且均可能存在重复字符;
字符串 key 一定可以由字符串 ring 旋转拼出。
Ps:
DP【i】【j】指的是key第i个字符匹配的轮盘的第j个数字
因为我是两边转,在dp基础上加一个判断正反最小的
class Solution {
public int findRotateSteps(String ring, String key) {
char[] sring = ring.toCharArray();
char[] skey = key.toCharArray();
int[][] dp = new int[key.length()][ring.length()];
for(int i = 0 ; i < dp.length ; i ++){
Arrays.fill(dp[i], Integer.MAX_VALUE);
}
int n = ring.length();
int count = Integer.MAX_VALUE;
for(int i = 0 ; i < skey.length ; i ++) {
for(int j = 0 ; j < n; j ++){
if(skey[i] == sring[j]){
if(i == 0)
dp[i][j] = Math.min(j, n - j);
else{
for(int k = 0 ; k < n ; k ++){
if(dp[i - 1][k] != Integer.MAX_VALUE){
dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + Math.min(Math.abs(j - k), n - Math.abs(j - k)));
}
}
}
if(i == skey.length - 1)
count = Math.min(count, dp[i][j]);
}
}
}
return count + skey.length;
}
Java实现 LeetCode 514 自由之路的更多相关文章
- Leetcode 514.自由之路
自由之路 视频游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写特定关键词 ...
- [LeetCode] Freedom Trail 自由之路
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 第一章 Java的I/O演进之路
I/O基础入门 Java的I/O演进 第一章 Java的I/O演进之路 1.1 I/O基础入门 1.1.1 Linux网络I/O模型简介 根据UNIX网络编程对I/O模型的分类,UNIX提供了5中I/ ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- 《学习笔记》.NET Core API搭建
1.创建 ASP.NET Core Web程序,记住取消HTTPS配置 2.此时一个简单的.NET Core API 架子搭建好了,细心的人可以发现Properties下面不是CS文件,确是launc ...
- Docker之镜像地址
转载自https://www.cnblogs.com/doraman/p/9570891.html 官方docker hub 官方:https://hub.docker.com/explore/ 常用 ...
- mybatis中的动态SQL(IF Chooes When Where Set ForEach SQL片段)
mapper: public interface BlogMapper { List<Blog> getBlogByIF(Map map); } IF <select id=&quo ...
- gentoo 下安装lamp
更新系统 emerge --sync emerge --update world 安装apache emerge www-servers/apache rc-update add apache2 de ...
- 【雕爷学编程】MicroPython动手做(04)——零基础学MaixPy之尝试运行
1.hello micropython #MicroPython动手做(04)——零基础学MaixPy之基本示例 #程序之一:hello micropython #MicroPython动手做(04) ...
- python机器学习(四)分类算法-决策树
一.决策树的原理 决策树思想的来源非常朴素,程序设计中的条件分支结构就是if-then结构,最早的决策树就是利用这类结构分割数据的一种分类学习方法 . 二.决策树的现实案例 相亲 相亲决策树 ...
- 3.3 Go浮点型
1.Go浮点型 Go 语言提供了两种精度的浮点数,float32 和 float64,编译器默认声明为float64 小数类型就是存放小数的,如1.2 0.005 -2.32 package main ...
- ftp服务器搭建(一)
先安装vsftpd 发现安装yum不行 root命令下也不行 那么可以去 /var/lib/dpkg 把lock文件rm掉 然后在yum install vsftpd 发现可以了 注意:如果发现yum ...
- Hyperledger Fabric——balance transfer(五)执行交易
链码安装和实例化之后就可以调用chaincode执行交易,下面分析简单的账户转账操作是如何完成的. 源码分析 1.首先看app.js的路由函数 app.post('/channels/:channel ...
- BZOJ1059 二分匹配
1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4810 Solved: 2297[Submit][Stat ...