动态规划——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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 前端面试题整理—React篇
1.说一下React React是Facebook 开发的前端JavaScript库 V层:react并不是完整的MVC框架,而是MVC中的C层 虚拟DOM:react引入虚拟DOM,每当数据变化通过 ...
- C++编译/运行过程中产生的各种文件
参考:https://blog.csdn.net/weixin_39609623/article/details/82348576 Windows中的obj,lib,dll,exe文件 lib是和dl ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop(dp)
题目链接 大意:给你n个物品和m种优惠方式,让你买k种,问最少多少钱. 思路:考虑dpdpdp,dp[x]dp[x]dp[x]表示买xxx种物品的最少花费,然后遍历mmm种优惠方式就行转移就好了. # ...
- C#解压文件,Excel操作
/// <summary> /// 获取目录下文件路径 /// </summary> /// <param name="path"></p ...
- 记一次解决netty半包问题的经历
最近学习了netty,想写一个简单的rpc,结果发现发送消息时遇到难题了,网上搜了一下,这种情况是半包问题和粘包问题,主要是出现在并发高一些的时候. talk is cheap 客户端编码: prot ...
- 本地Git与Github建立关联
准备 本地与Github建立连接,需要用到SSH公钥.一般安装完Git,会在用户目录中生成一个 .ssh的文件夹 如果没有此文件夹,可以通过命令创建 $ ssh-keygen -t rsa -C &q ...
- CNN的反向传播
在一般的全联接神经网络中,我们通过反向传播算法计算参数的导数.BP 算法本质上可以认为是链式法则在矩阵求导上的运用.但 CNN 中的卷积操作则不再是全联接的形式,因此 CNN 的 BP 算法需要在原始 ...
- k8s部署etcd数据库集群
⒈下载 https://github.com/etcd-io/etcd/releases ⒉解压 tar -zxvf etcd-v3.3.12-linux-amd64.tar.gz ⒊移动可执行文件及 ...
- RNN和LSTM
一.RNN 全称为Recurrent Neural Network,意为循环神经网络,用于处理序列数据. 序列数据是指在不同时间点上收集到的数据,反映了某一事物.现象等随时间的变化状态或程度.即数据之 ...
- docker中的镜像中运行Django项目
首先要在镜像中 安装python3 以及 django2.0.4 目前我用的是这两个版本. 进入镜像 创建项目 进入项目中修改setting文件 将引号和星号添加进括号中 ALLOWED_HOSTS ...