-
Notifications
You must be signed in to change notification settings - Fork 13
/
DP_CountWaysToReachNthStairUsing123Steps.java
42 lines (34 loc) · 1.49 KB
/
DP_CountWaysToReachNthStairUsing123Steps.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// https://www.geeksforgeeks.org/count-ways-reach-nth-stair-using-step-1-2-3/
// https://practice.geeksforgeeks.org/problems/count-ways-to-reach-the-nth-stair/0 (Similar problem)
// https://www.youtube.com/watch?v=CFQk7OQO_xM (BEST Video)
// https://www.youtube.com/watch?v=NFJ3m9a1oJQ
// https://www.youtube.com/watch?v=5o-kdjv7FD0
public class DP_CountWaysToReachNthStairUsing123Steps {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
int numOfStairs = Integer.parseInt(br.readLine().trim());
System.out.println(getNumOfWaysToReachNthStair(numOfStairs));
}
}
private static int getNumOfWaysToReachNthStair(int n) {
if(n == 0 || n == 1 || n == 2) return n;
if(n == 3) return 4;
int modulo = 1000000007;
int thirdLastNumber = 1;
int secondLastNumber = 2;
int lastNumber = 4;
int currentNumber = 0;
for(int i = 4; i <= n; i++) {
currentNumber = ( (lastNumber + secondLastNumber)% modulo + thirdLastNumber) % modulo;
thirdLastNumber = secondLastNumber % modulo;
secondLastNumber = lastNumber % modulo;
lastNumber = currentNumber % modulo;
}
return currentNumber % modulo;
}
}