Python DP beats 81%


  • 0
    A
    class Solution(object):
        def wiggleMaxLength(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if not nums:
                return 0
            return max(self.count(1, nums), self.count(-1,nums))
            
        def count(self, flag, nums):
            _tmp=nums[0]
            cnt=1
            for i in range(1,len(nums)):
                if flag==1:
                    if nums[i]<=_tmp:
                        _tmp=nums[i]
                    else:
                        flag=-1
                        cnt+=1
                        _tmp=nums[i]
                else:
                    if nums[i]>=_tmp:
                        _tmp=nums[i]
                    else:
                        flag=1
                        cnt+=1
                        _tmp=nums[i]
            return cnt
                    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.