Advertisement
rishu110067

Untitled

Feb 25th, 2022
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. class Solution {
  2.     public int minimumTotal(List<List<Integer>> grid) {
  3.        
  4.         int numRows = grid.size();
  5.         for(int row = 1; row < numRows; row++) {
  6.             int leftWall = grid.get(row-1).get(0) + grid.get(row).get(0);
  7.             grid.get(row).set(0, leftWall);
  8.            
  9.             int rightWall = grid.get(row-1).get(row-1) + grid.get(row).get(row);
  10.             grid.get(row).set(row, rightWall);
  11.         }
  12.        
  13.         for(int row = 2; row < numRows; row++) {
  14.             for(int col = 1; col < row; col++) {
  15.                 int min = Math.min(grid.get(row-1).get(col), grid.get(row-1).get(col-1));
  16.                 int curr = min + grid.get(row).get(col);
  17.                 grid.get(row).set(col, curr);
  18.             }
  19.         }
  20.        
  21.         List<Integer> lastRow = grid.get(grid.size()-1);
  22.         return Collections.min(lastRow);
  23.        
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement