Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. class Solution {
  2.     public int minimumTotal(List<List<Integer>> triangle) {
  3.         if (triangle.size() == 0)
  4.             return 0;
  5.        
  6.         int[] dp = new int[triangle.size()];
  7.         Arrays.fill(dp, Integer.MAX_VALUE);
  8.         dp[0] = triangle.get(0).get(0);
  9.        
  10.         int minDst = triangle.get(0).get(0);
  11.         for(int i = 1; i < triangle.size(); i++) {
  12.             int curMinDst = Integer.MAX_VALUE;
  13.             for (int j = triangle.get(i).size() - 1; j >= 0 ; j--) {
  14.                 int straightStep = dp[j];
  15.                 int sideStep = Integer.MAX_VALUE;
  16.                 int curItm = triangle.get(i).get(j);
  17.                 if(j > 0)
  18.                     sideStep = dp[j - 1];
  19.                
  20.                 int min = Math.min(straightStep, sideStep);
  21.                 dp[j] = min + curItm;
  22.                 curMinDst = Math.min(curMinDst, dp[j]);
  23.                
  24.             }
  25.             minDst = curMinDst;
  26.         }
  27.        
  28.         return minDst;
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement