LeetCode Climbing Stairs

Question:

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

Asked By: Terwin

||

Answers:

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
Answered By: sahasrara62
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.