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 自由之路的更多相关文章

  1. Leetcode 514.自由之路

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

  2. [LeetCode] Freedom Trail 自由之路

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

  3. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. 第一章 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/ ...

  5. 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 ...

  6. 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. ...

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. hex文件格式总结

    hex文件格式总结 文章目录 hex文件格式总结 什么是hex文件? 文件格式 指令类型(Record type) 校验和 :04 02B0 00 92020008 AE :04 0000 05 08 ...

  2. 详解 Spark 中的 Bucketing

    什么是 Bucketing Bucketing 就是利用 buckets(按列进行分桶)来决定数据分区(partition)的一种优化技术,它可以帮助在计算中避免数据交换(avoid data shu ...

  3. 手写一个简易的多周期 MIPS CPU

    一点前言 多周期 CPU 相比单周期 CPU 以及流水线 CPU 实现来说其实写起来要麻烦那么一些,但是相对于流水线 CPU 和单周期 CPU 而言,多周期 CPU 除了能提升主频之外似乎并没有什么卵 ...

  4. CSS:必须要掌握的重要基础知识点

    目录 1. 盒子 2. 常用选择器 3. 优先级 4. CSS继承 5. 伪元素(pseudo-element)和伪类(pseudo-class) 6. CSS:元素定位机制(positioning ...

  5. spring mvc --自定义converse

    在MVC中我们可以很轻松的根据项目需求进行必要的信息转换,如设置默认的日期格式,自定义String类型的格式等等... 配置中我们需要自定义converseService: <bean id=& ...

  6. 读懂这几个关键词,你就能了解 Docker 啦

    基于高度虚拟化所诞生的容器技术,如今已经走向大规模应用.那么容器.虚拟机.Docker.Openstack.Kubernetes 之间又有什么关系,对现在的选择有什么影响呢? 上世纪 60 年代,计算 ...

  7. java触发器的学习

         public class OpenVirtualService {             public void open(){ //虚机开通                //业务逻辑 ...

  8. 数独c++

    #include <bits/stdc++.h> using namespace std; const int maxn = 10; bool maps[maxn][maxn], row[ ...

  9. Django之ORM多表增删改操作

    关系表的操作语句: 以上一节中创建的书籍.出版社.作者.作者信息表为例进行: 增: # 一对一 # (1)类属性外键关联,使用外键约束属性直接进行对象关联插入 author_detail_obj=mo ...

  10. 在 n 道题目中挑选一些使得所有人对题目的掌握情况不超过一半。

    Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...