动态规划-Minimum Distance to Type a Word Using Two Fingers
2020-01-12 18:28:13
问题描述:
问题求解:
本题还是非常困难的,至少我在看到这个题目的时候是没有想到怎么解决的。我当时联想到的题目是那条grid走两遍的题目,那条题目也很麻烦,使用的是dp。
本题最难的地方在于如何定义状态,其实本题可以看作是个路径规划问题,所以状态就是左指在的位置和右指在位置以及当前的字符位置。
之后递归去遍历解空间即可。
int[][][] dp = new int[27][27][301]; public int minimumDistance(String word) {
return dfs(word, 0, 0, 0);
} private int dfs(String word, int start, int l, int r) {
if (start >= word.length()) return 0;
if (dp[l][r][start] != 0) return dp[l][r][start];
int idx = word.charAt(start) - 'A' + 1;
dp[l][r][start] = Math.min(dist(l, idx) + dfs(word, start + 1, idx, r), dist(r, idx) + dfs(word, start + 1, l, idx));
return dp[l][r][start];
} private int dist(int from, int to) {
if (from == 0) return 0;
int x1 = (from - 1) / 6;
int y1 = (from - 1) % 6;
int x2 = (to - 1) / 6;
int y2 = (to - 1) % 6;
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
动态规划-Minimum Distance to Type a Word Using Two Fingers的更多相关文章
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
- [LeetCode&Python] Problem 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- leetcode 783. Minimum Distance Between BST Nodes 以及同样的题目 530. Minimum Absolute Difference in BST
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...
- [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
随机推荐
- 码海拾遗:基于MySQL Connector/C++的MySQL操作(连接池)
1.MySQL安装及简单设置 (1)安装:在OSX系统下,可以使用万能的“brew install”命令来进行安装:brew isntall mysql(默认安装最新版的MySQL) (2)启动:br ...
- 配置github——每次提交后使contributions有记录(有小绿格子)
# 配置github--每次提交后使contributions有记录(有小绿格子) 这几天都有将自己的代码提交到github上,但是在profile里的contributions的表格中没有我提交的记 ...
- Java对接微信登录
今天我们来对接微信开放平台的网站应用登录 首先上文档链接:https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login ...
- web前端 关于浏览器兼容的一些知识和问题解决
浏览器兼容 为什么产生浏览器兼容,浏览器兼容问题什么是浏览器兼容: 所谓的浏览器兼容性问题,是指因为不同的浏览器对同一段代码有不同的解析,造成页面显示效果不统一的情况. 浏览器兼容产生的原因: 因为不 ...
- 【echarts3】 折线图我踩过的那些坑 (tooltip 设置,line 单个点/散点不显示问题)
echarts 折线图小技巧 echarts 折线图功能丰富且官方文档详细易懂,上手比较容易,这篇文章将分享一些项目中踩过的坑,示例主要以多条曲线为主,对大家完成一些 曲线.tooltip 和 mar ...
- JZOJ 5258. 友好数对 (Standard IO)
5258. 友好数对 (Standard IO) Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description I ...
- python数据分析工具 | numpy
Python中没有提供数组功能,虽然列表可以完成基本的数组功能,但并不是真正的数组,而且在数据量较大时,使用列表的速度回非常慢.因此,Numpy提供了真正的数组功能,以及对数据进行快速处理的函数.Nu ...
- 安装docker,docker-compose,Harbor
一.docker安装 1.删除旧版本和相关依赖 yum remove docker \ docker-client \ docker-client-latest \ docker-common \ d ...
- 检测js对象是不是数组类型?
面试时候被人问如何检测一个未知变量是不是数组类型,丢脸啊,老祖宗的脸都丢没了,这都不会,回家啃书本去吧!!! var a = [];方法一:Array.isArray([]) //true type ...
- scrapy mid中间件一般处理方法
import user_agent import requests class UA_midd(object): def process_request(self,request,spider): r ...