-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbingStairs.java
More file actions
32 lines (28 loc) · 828 Bytes
/
ClimbingStairs.java
File metadata and controls
32 lines (28 loc) · 828 Bytes
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
/*
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
*/
import java.io.*;
import java.util.*;
public class ClimbingStairs {
public static int climbStairs(int n) {
if(n <= 3) return n;
int prepre = 2;
int pre = 3;
int cur = 0;
for(int i = 4; i <= n; i++){
cur = prepre + pre;
prepre = pre;
pre = cur;
}
return cur;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt();
int res = climbStairs(n);
System.out.println(res + " distinct ways to climp to the top");
return;
}
}