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的更多相关文章

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

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

  3. LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

    783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...

  4. 【Leetcode_easy】783. Minimum Distance Between BST Nodes

    problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...

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

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

  7. 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...

  8. [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  9. [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

随机推荐

  1. 码海拾遗:strstr()、strcmp()和strcpy()实现

    1.strstr()实现 原型:char * strstr(const char * str1, const char * str2) 说明:判断str2是否为str1的子串,如果是则返回str2第一 ...

  2. [css-animation-101] 8 multiple transitions

    原文地址:css animation 101 #multiple-transitions 原文作者:Donovan Hutchinson 译者:JobbyM 到目前为止,我们已经讨论了一个过渡如何在一 ...

  3. Golang: chan定义问题(7)

    通常都是定义读写双向的 chan,定义单向 chan 问题. 专栏的介绍可以参考 <GotchaGolang专栏>,代码可以看<宝库-Gotcha>. 通过 只写 chan 传 ...

  4. C#连接Informix数据库

    最近在工作中遇到了需要连接Informix数据库的问题,在通过研究后发现了可以通过多种方式实现,我选择的是通过IBM Informix .NET Provider.该方式需要引用IBM.Data.In ...

  5. 从0到1,本地到远程git程序过程

    从0到1,本地到远程git程序过程 切记一定要在需要提交代码的文件夹下git init,既是你使用了什么 tortoisegit什么工具,或者你在idea环境下已经add了,但是仍然需要你在当前文件夹 ...

  6. WebAPI-处理架构

    带着问题去思考,大家好! 问题1:HTTP请求和返回相应的HTTP响应信息之间发生了什么? 1:首先是最底层,托管层,位于WebAPI和底层HTTP栈之间 2:其次是 消息处理程序管道层,这里比如日志 ...

  7. SpringCloud - 全家桶

    先导篇:SpringCloud介绍篇 第一篇:注册中心Eureka 第二篇:服务提供与Rest+Ribbon调用 第三篇:服务提供与Feign调用 第四篇:熔断器Hystrix(断路器) 第五篇:熔断 ...

  8. Chrome 63 - What"s New in DevTools(中文字幕)

    大家好,这是代码之声(codefm)第一期,今天给大家带来的是 What's New In DevTools (Chrome 63). Chrome 一般会每隔 6 周发布一次主版本.​目前 Chro ...

  9. springboot 整合logback

    日志包使用的是springboot内置的日志包,所以我们不许要再专门导入日志包 1.logback-spring.xml配置 <?xml version="1.0" enco ...

  10. 五分钟学Java:如何才能学好Java Web里这么多的技术

    原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 系列文章介绍 本文是<五分钟学Java>系列文章的一篇 本系列文章主要围绕Java程序员必须掌握的核心技能,结合我个人三年 ...