help me analyze recursion time complexity

Revision en2, by ducanh203, 2024-10-31 14:41:33

Here's my code for problem https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-ii/description/ , I'm confused to find Time Complexity of that , Please help me !!!

class Solution:
    def maxOperations(self, nums: List[int]) -> int:
        def f(i,j,pre=0):
            if i >= j:
                return 0

            ans = 0
            if pre == 0:
                return 1+max(f(i+2,j,nums[i] + nums[i+1]),f(i+1,j-1,nums[i]+ nums[j]), f(i,j-2,nums[j]+ nums[j-1]))
            else: 
                if nums[i] + nums[i+1] == pre:
                    ans = max(ans,1 + f(i+2,j,nums[i] + nums[i+1]))
                if nums[i]+ nums[j] == pre:
                    ans = max(ans,1 + f(i+1,j-1,nums[i] + nums[j]))
                if nums[j]+ nums[j-1] == pre:
                    ans = max(ans,1 + f(i,j-2,nums[j]+ nums[j-1]))
                return ans

        return f(0,len(nums)-1,0)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ducanh203 2024-10-31 14:41:33 12
en1 English ducanh203 2024-10-31 14:40:45 977 Initial revision (published)