forked from gauriiighadge/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSA.java
29 lines (24 loc) · 756 Bytes
/
DSA.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
// Problem Statement : 70. Climbing Stairs
// You are climbing a staircase. It takes n steps to reach the top.
// Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
class Solution {
public int climbStairs(int n) {
//Based on fibonacci series
//so seried=s will be 1,2,3,5,8,13.......
//therefor the first elemnts there is no need of adding previous elements
int[] a= new int[n];
{
if(n<=3)
{
return n;
}
for(int i=2;i<n;i++)
{
a[0]=1;
a[1]=2;
a[i]=a[i-1]+a[i-2];
}
return a[n-1];
}
}
}