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. HAProxy此例简单介绍基于docker的HAProxy反代

    HAProxy拓展连接 此例简单介绍基于Docker的HAProxy反代 反代: 1.获取haproxy镜像  docker pull haproxy 2.写配置文件haproxy.cfg 1 glo ...

  2. 在线做RAID命令

    # 安装raid卡管理工具 wget http://10.12.30.102:10800/other/MegaCli-8.07.14-1.noarch.rpm -O /tmp/MegaCli-8.07 ...

  3. python设置检查点简单实现

    说检查点,其实就是对过去历史的记录,可以认为是log.不过这里进行了简化.举例来说,我现在又一段文本.文本里放有一堆堆的链接地址.我现在的任务是下载那些地址中的内容.另外因为网络的问题或者网站的问题, ...

  4. Oops 的栈信息分析

    MTK MT55 F3600 平台 现象:播放MP4文件不断快退或者快进系统重启. 关键log: Kernel panic - not syncing: x_msg_q_receive(): not ...

  5. HINOC2.0标准介绍(1):概述

    本文首发于'瀚诺观察'微信公众号 摘要: 2016年3月18日,国家新闻出版广电总局批准发布了行业标准GY/T 297-2016<NGB宽带接入系统HINOC2.0物理层和媒体接入控制层技术规范 ...

  6. C++扬帆远航——8(张三李四,等差数列)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:qiudengcha数列.cpp * 作者:常轩 * 完成日期: ...

  7. python爬虫所遇问题列举

    1.通过python socket库来构造请求报文,向服务器发送图片请求时 (1)图片在浏览器请求头中的remote address信息跟通过python socket输出远程连接地址和端口号不一致 ...

  8. Zookeeper的核心概念以及java客户端使用

    一.Zookeeper的核心概念 分布式配置中心(存储):disconf(zk).diamond(mysql+http) 1)znode ZooKeeper操作和维护的是一个个数据节点,称为 znod ...

  9. SQL基本操作总结

    1.SQL简介 结构化查询语言 (层次模型,网状模型,关系模型) 关系模型是目前的主流 (Oralce,mysql mssql ) SQL标准:ANSI (1992 1997 2002 ISO) 方言 ...

  10. html5插件完成滚屏幕效果

    首先想要完成这样的效果要用到jquery-fullpage插件我们需要他的js文件和css样式文件如图  因为是jquery的插件所以我们还要导入jquery-min.js 在页面引入这些样式和插件 ...