动态规划——Freedom Trail
题目:https://leetcode.com/problems/freedom-trail/
额。。。不解释大意了,题目我也不想写过程了有点繁琐,直接给出代码:
public int findRotateSteps(String ring, String key) {
int rlen = ring.length();
int klen = key.length();
int[][]dp = new int[klen+1][rlen+1];
int temp = 0,step = 0,diff = 0;
for(int i = 0;i<=klen;i++) {
for(int j = 1;j<=rlen;j++)
if(i==0)dp[i][j] = 0;
else dp[i][j] = Integer.MAX_VALUE;
}
for(int i = 1;i<=klen;i++) {
for(int j = 1;j<=rlen;j++) {
temp = Integer.MAX_VALUE;
if(ring.charAt(j-1)==key.charAt(i-1)) {
if(i==1)temp = Math.min(j-1,rlen+1-j);
else {
for(int k = 1;k<=rlen;k++)
if(dp[i-1][k]!=Integer.MAX_VALUE) {
diff = Math.abs(j-k);
step = Math.min(diff,rlen-diff);
temp = Math.min(temp,step+dp[i-1][k]);
}
}
}
dp[i][j] = temp;
}
}
/*
for(int i = 1;i<=klen;i++) {
for(int j = 1;j<=rlen;j++)
System.out.print(dp[i][j]+" ");
System.out.println();
}*/
int ans = dp[klen][1];
for(int i = 2;i<=rlen;i++) {
ans = ans < dp[klen][i]?ans:dp[klen][i];
}
return ans+klen;
}
动态规划——Freedom Trail的更多相关文章
- [LeetCode] Freedom Trail 自由之路
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- [Swift]LeetCode514. 自由之路 | Freedom Trail
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- 514. Freedom Trail
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- 514 Freedom Trail 自由之路
详见:https://leetcode.com/problems/freedom-trail/description/ C++: class Solution { public: int findRo ...
- LeetCode 514----Freedom Trail
问题描述 In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- Leetcode 514.自由之路
自由之路 视频游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写特定关键词 ...
- leetcode 学习心得 (2) (301~516)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Web_0002:关于MongoDB的操作
1,启动moggdb服务端 打开cmd命令窗口进入到MongoDB的安装目录bin文件下: 如: cd /d F:\Program Files\mongodb\bin 执行如下命令(该命令窗口为 ...
- Java使用POI解析Excel表格
概述 Excel表格是常用的数据存储工具,项目中经常会遇到导入Excel和导出Excel的功能. 常见的Excel格式有xls和xlsx.07版本以后主要以基于XML的压缩格式作为默认文件格式xlsx ...
- 技术栈(technology stack)
technology stack 技术栈: 产品实现上依赖的软件基础组件, 包括 1. 系统 2. 中间件 3. 数据库 4. 应用软件 5. 开发语言 6. 框架 https://en.wikipe ...
- 树莓派3B+ 安装系统
安装概要步骤: 官网下载系统->刷入TF卡->设置开启显示器和SSH->通电->进入系统 1. 进入官方网站下载系统镜像 下载页面:https://www.raspberryp ...
- react native 中时间选择插件
npm install react-native-datepicker --save import DatePicker from 'react-native-datepicker'; <Vie ...
- asp.net core NLog将日志写到文件
1.安装Nlog包 Install-Package NLog.Extensions.Logging -Pre 2.在项目添加nlog.config文件 2.1.nlog.config <?xml ...
- 线程——自定义多线程task
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- QButtonGroup按钮组
继承 QObject 提供 一个抽象的按钮容器, 可以将多个按钮划分为一组,不具备可视化的效果,一般放的都是可以被检查的按钮 import sys from PyQt5.QtWidgets impo ...
- 感受野RF的计算
参考博客:https://blog.csdn.net/wgx571859177/article/details/80983043 设第N层的感受野为N_RF,卷积核尺寸为kernel_size,步长为 ...
- 如何用java实现一个p2p种子搜索(2)-路由表实现
路由表实现 回顾一下上一篇讲的内容,上一篇提到从dht网络中获取infohash,那么加入dht网络后的最重要的第一步就是怎么去建立路由表. 路由表里面保存的是dht中其他node的信息,所以node ...