Advertisement
unknown_0711

Untitled

May 10th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. public class Solution {
  2. public int minPathSum(int[][] A) {
  3. int M = A[0].length;
  4.  
  5. int N = A.length;
  6.  
  7. int Mem[][] = new int[N][M];
  8.  
  9. for (int i = 0; i < N; i++) {
  10.  
  11. for (int j = 0; j < M; j++) {
  12.  
  13. if (i == 0 && j == 0) {
  14.  
  15. Mem[i][j] = A[i][j];
  16.  
  17. }
  18. else if (i == 0) {
  19.  
  20. Mem[i][j] = Mem[i][j - 1] + A[i][j];
  21.  
  22. } else if (j == 0) {
  23.  
  24. Mem[i][j] = Mem[i - 1][j] + A[i][j];
  25.  
  26. } else {
  27.  
  28. Mem[i][j] = Math.min(Mem[i - 1][j], Mem[i][j - 1]) + A[i][j];
  29.  
  30. }
  31.  
  32. }
  33.  
  34. }
  35.  
  36. return Mem[N - 1][M - 1];
  37. }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement