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. 浅析Spring中bean的作用域

    一.前言   刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...

  2. 《机器学习_02_线性模型_Logistic回归》

    import numpy as np import os os.chdir('../') from ml_models import utils import matplotlib.pyplot as ...

  3. mysql5.6 thread pool

    从percona 的压测来看,确实很牛笔啊.提升很大. http://www.mysqlperformanceblog.com/2014/01/29/percona-server-thread-poo ...

  4. Python格式化字符串(格式化输出)

    熟悉C语言 printf() 函数的读者能够轻而易举学会 Python print() 函数,它们是非常类似的. print() 函数使用以%开头的转换说明符对各种类型的数据进行格式化输出,具体请看下 ...

  5. .NET 合并程序集(将 dll 合并到 exe 中)

    ------------恢复内容开始------------ ------------恢复内容开始------------ 背景:我们的应用程序通常都是由多个程序集组成,例如一个 exe 程序依赖于多 ...

  6. 01.drf文档及外键字段反序列化

    一 安装drf 1.1 安装库 pip install djangorestframework pip install markdown # Markdown support for the brow ...

  7. 王艳 201771010127《面向对象程序设计(java)》第七周学习总结

    1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: (2)掌握Object类的常用API用法: (3)掌握ArrayList类用法与常用API: (4)掌握枚举类使用方法: (5)结合 ...

  8. 王艳 201771010127《面向对象程序设计(Java)》第四周学习总结

    第一部分:理论知识. 第四章:对象与类 4.1:类与对象的概念. 类:是构造对象的模板或蓝图.由类构造对象的过程称为创建类的实例. 对象:想要使用oop,一定要清楚对象的三个特性: 1)对象的行为:对 ...

  9. python 格式化输出(% VS format)

    提到Python中的格式化输出方法,一般来说有以下两种方式: 1)% 格式说明由%和格式字符组成,如%f,%s,%d,它的作用是将数据按照指定的格式输出.格式说明是由“%”字符开始的. #1.输出字符 ...

  10. java颜色对照表