My Java DP Solution


  • 0
    public class Solution {
        public int minimumTotal(List<List<Integer>> triangle) {
            int[] dp = new int[triangle.get(triangle.size() - 1).size()];
            for (int i = 0; i < dp.length; ++i) {
                 dp[i] = triangle.get(triangle.size() - 1).get(i);
            }
            for (int i = triangle.size() - 2; i >= 0; --i) {
                List<Integer> row = triangle.get(i);
                for (int j = 0; j < row.size(); ++j) {
                    dp[j] = Math.min(dp[j], dp[j + 1]) + row.get(j);
                }
            }
            return dp[0];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.