动态规划-Minimum Distance to Type a Word Using Two Fingers

2020-01-12 18:28:13

问题描述

问题求解

本题还是非常困难的,至少我在看到这个题目的时候是没有想到怎么解决的。我当时联想到的题目是那条grid走两遍的题目,那条题目也很麻烦,使用的是dp。

本题最终的解决方式其实是和那条题目是类似的,也是使用dp的方式去做。

最大的类似在于,这两题都可以使用dfs + me来进行解决,dfs和dp任何一个都是解决问题的大杀器,他们的结合就更是非常的厉害。很多的题目直接使用自底向上的dp是不容易写的,但是结合dfs使用me的dp则非常的直观并且效率依然很高。

这两条题目都是采用的这个算法,结构清晰且逻辑严明,本质上都是解空间的遍历,但是利用了dp的思路将其中的一些解保存了下来方便了后续的计算。

    int[][][] dp = new int[27][27][301];

    public int minimumDistance(String word) {
        return helper(word, 0, 0, 0);
    }

    private int helper(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) + helper(word, start + 1, idx, r), dist(r, idx) + helper(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);
    }

  

原文地址:https://www.cnblogs.com/hyserendipity/p/12183423.html

时间: 2024-08-30 15:12:40

动态规划-Minimum Distance to Type a Word Using Two Fingers的相关文章

【leetcode】1320. Minimum Distance to Type a Word Using Two Fingers

题目如下: You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for example, the letter A is located at coordinate (0,0), the letter B is located at coordinate (0,1), the letter P is

leetcode 783. Minimum Distance Between BST Nodes ---中序遍历

过年晚上无聊,233333333 题解: BST树的中序遍历是有序的,遍历过程中,记录前一个值,然后和当前值比较,来更新最小的minimum distance 注意python参数传递时候,像list这些object是传引用,单独int的数是传值的 void getans(TreeNode* root,int &pre,int &ans) { if(root==NULL) return; getans(root->left,pre,ans); if(pre!=INT_MAX) ans

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

783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意: root 是树结点对象 (TreeNode object),而不是数组. 给定的树 [4,2,6,1,3,null,null] 可表示为下图: 4 / 2 6 / \ 1 3 最小的差

783. Minimum Distance Between BST Nodes BST节点之间的最小距离

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

783. Minimum Distance Between BST Nodes

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

[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 values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

Leetcode 动态规划 Minimum Path Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Minimum Path Sum Total Accepted: 15789 Total Submissions: 50645My Submissions Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

783. Minimum Distance Between BST Nodes - Easy

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

2017南宁网络赛 Problem J Minimum Distance in a Star Graph ( 模拟 )

题意 : 乱七八糟说了一大堆,实际上就是问你从一个序列到另个序列最少经过多少步的变化,每一次变化只能取序列的任意一个元素去和首元素互换 分析 : 由于只能和第一个元素去互换这种操作,所以没啥最优的特别方法,只要乖乖模拟即可,如果第一个元素不在正确位置则将它和正确位置的元素交换使其回到正确位置,如果第一个元素的位置就是正确的,那么就往后找不在正确位置的元素将它互换到第一个去,然后再对第一个元素进行判断即可,直到序列变成最终满足条件的序列........ #include<bits/stdc++.h