Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

LeetCode Climbing Stairs

I tried to separate the climbing scenarios into scenarios of how many 2 steps can be taken and to solve the question according to the sum of the combinations of the moments where 2 steps can be taken in these scenarios.

import math
class Solution:

    def climbStairs(self, n: int) -> int:
        def faktoriyel(a) -> int:
            sonuc=1
            for i in range(1,a+1):
                sonuc=sonuc*i
            return sonuc
        def permutasyon(a,b) -> int:  
          return faktoriyel(a)/faktoriyel(a-b)
        t=math.trunc(n/2) # how many two steps can be taken
        sonuc=0
        for i in range(1,t+1):
            sonuc=sonuc+int(permutasyon(n-i,i)/faktoriyel(i))
        return sonuc+1 # +1 is for scenario that only using one steps

Constraints:

1 <= n <= 45

It says wrong answer only for n=43 and n=45. The rest of inputs are correct. I don’t understand why its wrong for 43 and 45. Can somebody explain this ?
result for n=43
result for n=45

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you just need to do floor division (use //) instead of floating point division (not /)

ie case 1: 5/2 - > 2.5, case 2: 5//2 -> 2
since stairs or steps cant be in decimal and should be integer do floor division ie case 2
so your code should be

import math
class Solution:

    def climbStairs(self, n: int) -> int:
        def faktoriyel(a) -> int:
            sonuc=1
            for i in range(1,a+1):
                sonuc=sonuc*i
            return sonuc
        def permutasyon(a,b) -> int:
          return faktoriyel(a)//faktoriyel(a-b)
        t=math.trunc(n//2) # how many two steps can be taken
        sonuc=0
        for i in range(1,t+1):
            sonuc=sonuc+int(permutasyon(n-i,i)//faktoriyel(i))
        return sonuc+1 # +1 is for scenario that only using one steps
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading