1+ package  com .concept .scala .leetcode_30days_challenge_July2020 
2+ 
3+ import  com .concept .scala .RunTimeCalculation 
4+ 
5+ /**  *
6+  * Day 31 
7+  * 
8+  * @todo  You are climbing a stair case. It takes n steps to reach to the top. 
9+  * Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 
10+  * @example  Example 1: 
11+  * 
12+  * Input: 2 
13+  * Output: 2 
14+  * Explanation: There are two ways to climb to the top. 
15+  * 1. 1 step + 1 step 
16+  * 2. 2 steps 
17+  * Example 2: 
18+  * 
19+  * Input: 3 
20+  * Output: 3 
21+  * Explanation: There are three ways to climb to the top. 
22+  * 1. 1 step + 1 step + 1 step 
23+  * 2. 1 step + 2 steps 
24+  * 3. 2 steps + 1 step 
25+  * @note  1 <= n <= 45 
26+  * @see  https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/548/week-5-july-29th-july-31st/3407/ 
27+  * OR 
28+  * https://leetcode.com/problems/climbing-stairs/ 
29+  * 
30+  * 
31+  */  
32+ object  ClimbingStairs  {
33+  def  main (args : Array [String ]):  Unit  =  {
34+  val  runtime  =  RunTimeCalculation .calculateRunTime(println(climbStairs(45 )))
35+  val  runtime2  =  RunTimeCalculation .calculateRunTime(println(climbStairsHashMap(45 )))
36+  val  runtime3  =  RunTimeCalculation .calculateRunTime(println(climbingStairsDP(45 )))
37+  val  runtime4  =  RunTimeCalculation .calculateRunTime(println(climbingStairsDP2(45 )))
38+  println(s "  climbStairs total run time =  $runtime seconds OR  ${runtime *  1000 } milliseonds " )
39+  println(s "  climbStairsHashMap total run time =  $runtime2 seconds OR  ${runtime2 *  1000 } milliseonds " )
40+  println(s "  climbingStairsDP total run time =  $runtime3 seconds OR  ${runtime3 *  1000 } milliseonds " )
41+  println(s "  climbingStairsDP2 total run time =  $runtime4 seconds OR  ${runtime4 *  1000 } milliseonds " )
42+  /* 
43+  1836311903 
44+  1836311903 
45+  1836311903 
46+  1836311903 
47+  climbStairs total run time = 8.529 seconds OR 8529.0 milliseonds 
48+  climbStairsHashMap total run time = 7.363 seconds OR 7363.0 milliseonds 
49+  climbingStairsDP total run time = 0.004 seconds OR 4.0 milliseonds 
50+  climbingStairsDP2 total run time = 0.001 seconds OR 1.0 milliseonds 
51+  */  
52+  }
53+ 
54+  def  climbStairs (n : Int ):  Int  =  if  (n <=  1 ) 1  else  climbStairs(n -  1 ) +  climbStairs(n -  2 )
55+ 
56+  def  climbStairsHashMap (n : Int ):  Int  =  {
57+  val  map  =  scala.collection.mutable.HashMap [Int , Int ]()
58+  if  (n <  2 ) return  1 
59+  if  (map contains n) return  map(n)
60+  map.put(n, climbStairs(n -  1 ) +  climbStairs(n -  2 ))
61+  map(n)
62+ 
63+  }
64+ 
65+  /**  * 
66+  * Runtime: 376 ms, faster than 97.44% of Scala online submissions for Climbing Stairs. 
67+  * Memory Usage: 48.3 MB, less than 100.00% of Scala online submissions for Climbing Stairs. 
68+  * 
69+  * 45 / 45 test cases passed. 
70+  * Status: Accepted 
71+  * Runtime: 376 ms 
72+  * Memory Usage: 48.3 MB 
73+  * 
74+  */  
75+  def  climbingStairsDP (n : Int ):  Int  =  {
76+  if  (n <  2 ) return  1 
77+  val  dp  =  new  Array [Int ](n +  1 )
78+  dp(0 ) =  1 
79+  dp(1 ) =  1 
80+  for  (i <-  2  to n) dp(i) =  dp(i -  1 ) +  dp(i -  2 )
81+  dp(n)
82+  }
83+ 
84+  def  climbingStairsDP2 (n : Int ):  Int  =  {
85+  if  (n <  2 ) return  1 
86+  val  dp  =  new  Array [Int ](n +  1 )
87+  dp(0 ) =  1 
88+  dp(1 ) =  1 
89+  var  i  =  2 
90+  while  (i <=  n) {
91+  dp(i) =  dp(i -  1 ) +  dp(i -  2 )
92+  i +=  1 
93+  }
94+  dp(n)
95+  }
96+ }
0 commit comments