Time limit exceeding

Hi! Please check my code…I believe it’s right but i am still getting TLE for 2 test cases

hello @vaani787

u need to use dynamic programming to optimise it furthur and pass all test case

def countWays(self,n, m):

    # table to store values
    # of subproblems
    count =[]
    for i in range(n + 1):
        count.append(0)
    count[0] = 0
 
    # Fill the table upto value n
    for i in range(1, n + 1):
 
        # recurrence relation
        if (i > m):
            count[i] = (count[i-1] + count[i-m])%(10**9+7)
     
        # base cases
        elif (i < m or m == 1):
            count[i] = 1

        # i = = m
        else:
            count[i] = 2
 
 
    # required number of ways
    return count[n]

@vaani787

this is correct… and should pass all test cases

But it is giving TLE for 2 cases

…

code the same in c++

I don’t know C++…will such errors always arise in python?

…

python is generally slower than c++ thats why i was telling u to code it c++.

most of the platforms has given higher time limit for python so u might not get this issue on most of the plaform if ur code is efficient.

btw which course u r taking?

Coding Interview prep with C++. I do understand c++ code and concepts…as there are no options for python specifically, i chose that course.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.