zoukankan      html  css  js  c++  java
  • Triangle -- C3

    Given a triangle, find the minimum path sum from top to bottom. 
    Each step you may move to adjacent numbers on the row below. Notice 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. Have you met this question in a real interview? Yes 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). Tags Dynamic Programming Related Problems Easy Minimum Path Sum

    矩阵的题常构造新的矩阵转化为题意, 简单的记忆化存储, 常用dfs的分治策略自底向下回溯, 找一条最小的最合适的路径

    递归出口(出范围, 遍历过, 满足题意(到节点) ) , 递归条件(对邻居的判断, 遍历过, 出范围, 邻居), 返回值常用分治法(后序) 尾递归,递的时候是用的上面的值, 回的时候是用的下面的值.

    自底向上

    记忆化搜索: 分治法加入了数组变量
       
    public int minimumTotal(List<List<Integer>> triangle) {
            if (triangle == null || triangle.size() == 0) {
                return 0;
            }
          
            long[][] dp = new long[triangle.size()][triangle.size()];
           for (long[] d : dp) {
            Arrays.fill(d, Long.MAX_VALUE);
               
           }
           
            for (int i = 0 ; i < dp[0].length; i++) {
                dp[dp.length - 1][i] = triangle.get(triangle.size() - 1).get(i);
            }
            dfs(triangle, dp, 0, 0);
            return (int)dp[0][0];
    
        }
        private int dfs(List<List<Integer>> triangle, long[][] dp, int i, int j) {
            if (dp[i][j] != Long.MAX_VALUE) {
                return (int)dp[i][j];
            }
            
            int ans = triangle.get(i).get(j);
    
            int next = Math.min(dfs(triangle, dp, i + 1, j),dfs(triangle, dp, i+1, j + 1)) + ans;
            dp[i][j] = next;
            return next;
        }
    

      

    动归, 动归矩阵, 自顶向下

    public class Solution {
        /**
         * @param triangle: a list of lists of integers.
         * @return: An integer, minimum path sum.
         */
        public int minimumTotal(int[][] triangle) {
            // write your code here
            if (triangle == null || triangle.length == 0) {
                return -1;
            }
            
            // state:
            int n = triangle.length;
            int[][] f = new int [n][n];
            
            //initialize
            f[0][0] = triangle[0][0];
            for (int i = 1; i < n; i++) {
                f[i][0] = f[i - 1][0] + triangle[i][0];
                f[i][i] = f[i - 1][i - 1] + triangle[i][i];
            }
            
            // top down
            
            for (int i = 1; i < n; i++) {
                for (int j = 1; j < i; j++) {
                    f[i][j] = Math.min(f[i - 1][j], f[i - 1][j - 1]) + triangle[i][j];
                    
                }
            }
            
            //answer
            int ans = f[n - 1][0];
            for (int i = 1; i < n; i++) {
                if (ans > f[n - 1][i]) {
                    ans = f[n - 1][i];
                }
            }
            
            return ans;
    }
    

      

  • 相关阅读:
    【笔记】进化型开发方法
    错误注入学习笔记
    【C/C++】关于编译错误 "error C2146: syntax error : missing ';' before identifier 'xxx'"
    查找进程加载到内存中的EntryPoint
    devepxress qtp 点击子菜单
    RijndaelManaged 自定义key和iv
    sql server transaction
    使用gzip压缩字符串
    tsql 与时间(周)相关的一些操作
    excel 合并单元格
  • 原文地址:https://www.cnblogs.com/apanda009/p/7290161.html
Copyright © 2011-2022 走看看