坐标型动态规划总结,再附两个算法

Posted 超级码厉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了坐标型动态规划总结,再附两个算法相关的知识,希望对你有一定的参考价值。

reference: nine chapter algrithm; leetcode.com

前面给出了三个坐标型动态规划题解: Minimum Sum Path, Longest Increasing Sequence, Unique Path.

(1)对于二维矩阵,自底向上与自顶向下两种方法都可以解决;

(2)另外,最优子结构的空间有很多优化空间,而且上述三个代码也还有很多优化空间,从O(n^2)优化到线性空间;

(3)条条大路通罗马。动态规划只是一种思路,朋友们能想到其他解题思路吗?


下面对动态规划做一个总结:

(1) 从递归到动态规划


多重循环

记忆化搜索

优点

比较常规,容易第一眼想到,在面试时也容易被接受

很容易从搜索算法直接转化过来,节省时间

缺点

思考有难度

递归

(2)什么样的题适合用动态规划?动态规划为什么快?

满足以下三个条件之一,则极有可能用动态规划求解:

  • 求最大值/最小值

  • 判断是否可行

  • 统计方案个数

(3)记忆化搜索本质:动态规划

(4)动态规划与分治的区别:分治重复计算


第一题:jump game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

 Notice

This problem have two method which is Greedy and Dynamic Programming.

The time complexity of Greedy method is O(n).

The time complexity of Dynamic Programming method is O(n^2).


贪心思路:  用farthest 指针纪录 从开始节点(第0个元素)到当前节点能到达的最远距离;

                初始值:  farthest = A[0]; 很显然,若A[0] = 0, farthest = 0, 在数组元素大于1的情况下,是不能跳到最后一个元素的。

                通过一次遍历,算出结果。

                贪心思路的原则是, 假定当前指针在第i个, 若farthest 能够跳到最后一个元素,直接结束。  

                一次遍历的过程就是不停更新farthest与比较的过程 。

                a[0] + 0 = farthest(0) 说明不能跳到最后一个元素 能否推出若 a[i] + i = farthest , 则不能跳到最后一个元素

public boolean canJump(int[] A) { if(A == null || A.length <= 0) { return true; } 
int farthest = A[0];
for(int i = 0 ; i < A.length - 2; i++) { if(A[i] + i > farthest ) { farthest = A[i] + i; } if(farthest >= A.length - 1) { return true; } if(farthest == i) { return false; } } return farthest >= A.length - 1;}


第二题,Leetcode: triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[

     [2],

    [3,4],

   [6,5,7],

  [4,1,8,3]

]

 

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:

Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

public class Solution { public int minimumTotal(List<List<Integer>> triangle) { if(triangle == null || triangle.size() == 0) { return Integer.MAX_VALUE; }   int size = triangle.size(); int[][] f = new int[size][size];  f[0][0] = triangle.get(0).get(0); //initialize the diagonal for(int i = 1; i < size; i++ ) { f[i][i] = f[i - 1][i - 1] + triangle.get(i).get(i); }  for(int i = 1 ; i < size; i++) { f[i][0] = f[i - 1][0] + triangle.get(i).get(0); }  for(int i = 1; i < size; i++){ for(int j = 1; j < i;j++) { f[i][j] = triangle.get(i).get(j) + Math.min(f[i - 1][j - 1], f[i - 1][j]); } }  int min = f[size - 1][0]; for(int k = 1; k < size; k++) { min = Math.min(min, f[size - 1][k]); } return min;  }}





以上是关于坐标型动态规划总结,再附两个算法的主要内容,如果未能解决你的问题,请参考以下文章

算法动态规划 ④ ( 动态规划分类 | 坐标型动态规划 | 前缀划分型动态规划 | 前缀匹配型动态规划 | 区间型动态规划 | 背包型动态规划 )

坐标型动态规划之:Longest Increasing Subsequence

坐标型动态规划序曲之DFS剪枝

noip(提高组难度)动态规划有哪几种类型?(如:坐标DP、背包DP等) 各都有哪些经典题目?

数据结构与算法题解|动态规划|——矩阵型动态规划

动态规划