hey, i tried to solve this_problem of Atcoder using top_down approach,but it shows TLE for two sub-tasks>
am i missing any edge case or it just because of recursion .
one thing to mention that i know to solve the problem iteratively (bottom up) but curious to know why my recursive method failed.
submission_link
code for quick review.
import sys
sys.setrecursionlimit(10**6)
def dp(n,k,cache,height):
#print(cache)
if n==0:
return 0
elif n in cache :
return cache[n]
else:
lcl_min=float('inf')
i=1
while n-i>=0 and i<=k:
lcl_min=min(lcl_min,dp(n-i,k,cache,height)+abs(height[n]-height[n-i]))
i+=1
cache[n]=lcl_min
return cache[n]
n,k=map(int,input().split())
h=list(map(int,input().split()))
print(dp(n-1,k,dict(),h))
thanks a ton in advance…