+ 147
[ASSIGNMENT] ๐๐๐ A Man On Stairs of n Steps . ๐คโ๐
A man can jump 1 or 2 steps at a time , find number of ways in which the man can climb n number of steps ? /* example ::: input :: 3 output :: 3 // because 3 ways 1st way) 3 times 1 step (1,1,1) 2nd way)1 time 2 steps together & then 1 step more(2,1) 3rd way) 1 step & then 1 time 2 steps together(1,2) Remember man can take 1 or 2 steps at a time */ //level :: easy & interesting //simple approach is welcome , with some explanation HAPPY CODING โบ๐
113 Answers
+ 82
+ 43
After some observations,
if steps = 1: ways = 1
if steps = 2: ways = 2
if steps = 3: ways = 3
if steps = 4: ways = 5.....
And finally, I ended up with Fibonacci sequence
1, 2, 3, 5, 8, 13, 21.....
https://code.sololearn.com/cX917vOg9Mrl/?ref=app
+ 38
thnx ๐๐ @river,bool1
+ 33
Thank you for the challenge ๐
Here's my try :
https://code.sololearn.com/clieRlKMyz21/?ref=app
+ 30
@tai
welcome to Sl
https://www.sololearn.com/Discuss/595802/?ref=app
u can start with language u want to learn
//happy codingโบ
+ 29
i will give a simple approach to this challenge later โบ
//just give it a try ๐
+ 29
thnx ๐๐ swapnil,kartikey
+ 27
My try -
https://code.sololearn.com/W4h1B7NEBnk0/?ref=app
By the way, @Gaurav congrats for cotd.
+ 15
I tried to approach this problem by finding a mathematical pattern for the terms.
1 stair:
1
= 1 way
2 stairs:
11
2
= 2 ways
3 stairs:
111
12
21
= 3 ways
4 stairs:
1111
112
121
211
22
= 5 ways
5 stairs:
11111
1112
1121
1211
2111
122
212
221
= 8 ways
6 stairs:
111111
11112
11121
11211
12111
21111
1122
1212
1221
2112
2121
2211
222
= 13 ways
7 stairs:
1111111
111112
111121
111211
112111
121111
211111
11122
11212
11221
12112
12121
12211
21112
21121
21211
22111
1222
2122
2212
2221
= 21 ways
...
{1, 2, 3, 5, 8, 13, 21, ...} is the Fibonacci sequence starting from the third term (F2).
The Fibonacci sequence:
F0 = 0
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
...
This means that to solve the initial problem one can use this equation:
<number of ways to climb the stairs> = F(<number of stairs>-1)
To simplify the task I rephrased the problem to "Given n, find the Fibonacci number of term F(n-1)"
Here are my codes (JavaScript and C++):
https://code.sololearn.com/Wc7Hle99U3JS/?ref=app
https://code.sololearn.com/cqYg2kTfY0wx/?ref=app
+ 14
gaurav we can use a whole set of permutations & combinations here, i think ๐ฐ๐ฐ
+ 14
i have done it!!!!!
https://code.sololearn.com/cbDIAsPOrQHp/?ref=app
+ 12
Gaurav,great challenge,im lost!๐ค๐ค
+ 12
Congrats for COTD
+ 11
COT DAY
CONGRATS
+ 10
not in that kind of state but i will try after a month
+ 10
@Charan here is my Solotion
https://code.sololearn.com/cvNgxxjbX7sg
+ 10
My try..
Doesn't work for more than 19 steps, any help very appreciated!
https://code.sololearn.com/cM1XQdLu5zm6/?ref=app
+ 9
Since there's no answer in python yet, I'll post this clumsy one I made before any research (it works only up to 4, though hahaha)
https://code.sololearn.com/cno0PYFgy4tO/?ref=app
+ 9
Congrats @ COTD Gaurav๐ป๐บ๐and cheeeeeers