File tree 1 file changed +31
-0
lines changed
others/dp/grid traveler memoization
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ // "static void main" must be defined in a public class.
2
+ public class Main {
3
+
4
+ public final static Map <Map .Entry <Integer ,Integer >,Integer > map = new HashMap ();
5
+ public static void main (String [] args ) {
6
+ System .out .println (gridTraveler (1 ,1 ));
7
+ System .out .println (gridTraveler (3 ,1 ));
8
+ System .out .println (gridTraveler (3 ,3 ));
9
+ System .out .println (gridTraveler (2 ,2 ));
10
+ System .out .println (gridTraveler (5 ,5 ));
11
+
12
+ }
13
+
14
+ public static int gridTraveler (int x ,int y ){
15
+ if (x == 0 || y == 0 ){
16
+ return 0 ;
17
+ }
18
+
19
+ if (x == 1 && y == 1 ){
20
+ return 1 ;
21
+ }
22
+
23
+ if (map .containsKey (Map .entry (x ,y ))){
24
+ return map .get (Map .entry (x ,y ));
25
+ }
26
+
27
+ int ans = gridTraveler (x -1 ,y )+gridTraveler (x ,y -1 );
28
+ map .put (Map .entry (x ,y ),ans );
29
+ return ans ;
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments