ducanh203's blog

By ducanh203, history, 3 weeks ago, In English

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)

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it