r/learnpython • • 27m ago

leetcode 2926, TLE with both top down and bottom up approaches, what now

leetcode 2926

```py

class Solution:

def maxBalancedSubsequenceSum(self, nums: List[int]) -> int:

t = [nums[i] for i in range(len(nums))]

max_val = 0

for i in range(len(nums)):

for k in range(i):

if nums[k] - k <= nums[i] - i:

t[i] = max(t[i], t[k] + nums[i])

max_val = max(max_val, t[i])

return max_val if max(nums) > 0 else max(nums)

```

time limit exceeded how to fix? same error for top down as well

1 Upvotes

Duplicates