File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
src/com/blankj/medium/_0064 Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .blankj .medium ._064 ;
2
+
3
+ public class Solution {
4
+
5
+ public static void main (String [] args ) {
6
+ Solution s = new Solution ();
7
+ int [][] matrix = new int [][]{
8
+ {1 ,3 ,1 },
9
+ {1 ,5 ,1 },
10
+ {4 ,2 ,1 }
11
+ };
12
+ System .out .println (s .minimumPath (matrix ));
13
+ matrix = new int [][]{
14
+ {1 ,3 ,1 },
15
+ {1 ,5 ,4 },
16
+ {4 ,2 ,1 }
17
+ };
18
+ System .out .println (s .minimumPath (matrix ));
19
+ }
20
+
21
+ public int minimumPath (int [][] matrix ) {
22
+ if (matrix == null || matrix .length == 0 ) {
23
+ return -1 ;
24
+ }
25
+
26
+ int m = matrix .length ;
27
+ int n = matrix [0 ].length ;
28
+
29
+ int [][] f = new int [m ][n ];
30
+ f [0 ][0 ] = matrix [0 ][0 ];
31
+ for (int i = 1 ; i < m ; i ++) {
32
+ f [i ][0 ] = f [i - 1 ][0 ] + matrix [i ][0 ];
33
+ }
34
+ for (int j = 1 ; j < n ; j ++) {
35
+ f [0 ][j ] = f [0 ][j - 1 ] + matrix [0 ][j ];
36
+ }
37
+ for (int i = 1 ; i < m ; i ++) {
38
+ for (int j = 1 ; j < n ; j ++) {
39
+ if (f [i - 1 ][j ] > f [i ][j - 1 ]) {
40
+ f [i ][j ] = f [i ][j - 1 ] + matrix [i ][j ];
41
+ } else {
42
+ f [i ][j ] = f [i - 1 ][j ] + matrix [i ][j ];
43
+ }
44
+ }
45
+ }
46
+ return f [m - 1 ][n - 1 ];
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments