File tree 1 file changed +60
-0
lines changed
src/main/java/com/leetcode/dynamicprogramming
1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ public static int LeetCode_70_Climbing_Stairs_Memoization (int n , int [] dp )
2
+ {
3
+ if (n <= 1 )
4
+ {
5
+ return dp [n ] = 1 ;
6
+ }
7
+
8
+ if (dp [n ] != 0 )
9
+ {
10
+ return dp [n ];
11
+ }
12
+
13
+ int ans = LeetCode_70_Climbing_Stairs_Memoization (n - 1 , dp )
14
+ + LeetCode_70_Climbing_Stairs_Memoization (n - 2 , dp );
15
+
16
+ return dp [n ] = ans ;
17
+ }
18
+ public static int LeetCode_70_Climbing_Stairs_Tabulation (int n , int [] dp )
19
+ {
20
+ int N = n ;
21
+ for (n = 0 ; n <= N ; n ++)
22
+ {
23
+ if (n <= 1 )
24
+ {
25
+ dp [n ] = 1 ;
26
+ continue ;
27
+ }
28
+
29
+ int ans = dp [n - 1 ] + dp [n - 2 ];
30
+
31
+ dp [n ] = ans ;
32
+ }
33
+
34
+ return dp [N ];
35
+ }
36
+ public static int LeetCode_70_Climbing_Stairs_Better (int n )
37
+ {
38
+ int a = 1 ;
39
+ int b = 1 ;
40
+ int sum = 0 ;
41
+
42
+ for (int i = 2 ; i <= n ; i ++)
43
+ {
44
+ sum = a + b ;
45
+ a = b ;
46
+ b = sum ;
47
+ }
48
+
49
+ return sum ;
50
+ }
51
+ public static void LeetCode_70_Climbing_Stairs ()
52
+ {
53
+ int n =7 ;
54
+ int [] dp = new int [n +1 ];
55
+ System .out .println ("Memoization :- " + LeetCode_70_Climbing_Stairs_Memoization (n , dp ));
56
+ display_1D (dp );
57
+ System .out .println ("Tabulation :- " + LeetCode_70_Climbing_Stairs_Tabulation (n , dp ));
58
+ display_1D (dp );
59
+ System .out .println ("Better :- " + LeetCode_70_Climbing_Stairs_Better (n ));
60
+ }
You can’t perform that action at this time.
0 commit comments