【LEETCODE】376- Wiggle Subsequence [Python]

题目:

https://leetcode.com/problems/wiggle-subsequence/

题意:

Given a sequence, return the length of the longest wiggle subsequence.

Wiggle sequence means that the difference between successive numbers strictly alternate between positive and negative. Notice that the difference should not be 0, the first difference can be either positive or negative, and a sequence is also a wiggle one if the length is less than or equal to 2.

思路:

We can either apply ‘greedy’ or ‘DP’ to solve this problem.


For DP
Step 1: The structure

Let Si denote the longest subsequence when the index is i of given sequence.

So whether Si is a wiggle sequence, it depends on Si1 is already a wiggle sequence, at the same time Si1 and numi combine a wiggle sequence.

That means, at the point of i there can be two choices:
one is, when num[i]<num[i1] , the last difference of Si1 should be positive, then the length of Si=Si1+1 .
Another one is, when num[i]>num[i1] , the last difference of Si1 should be negative, then the length of Si=Si1+1 .

So, we got the structure of this DP problem, if we want to solve the problem to find longest length of wiggle subsequence at Si , we can firstly solve subproblem of finding the longest length of wiggle subsequence at Si1 .

Step 2: A recursive solution

According to the above structure, we should define 2 sequences p and q.
p stores the longest length of wiggle subsequence at index i satisfying the last difference is positive.
similarly, q stores the longest length of wiggle subsequence at index i satisfying the last difference is negative.

Then, if num[i]>num[i1] , at least the last difference of Si1 should be negative, so p[i]=q[i1]+1 .
Similarly, if numi<numi1 , at least the last difference of Si1 should be positive, so q[i]=p[i1]+1 .

If num is null, the answer should be 0.

If length of num is larger than 0, at least the length of longest wiggle subsequence is larger or equal to 1, so we can initialize p and q with 1s.

At each step, we should choose the max length, as well as the final, we will choose between p[n1] and q[n1] .

Step 3: Computing the optimal costs
class Solution(object):
    def wiggleMaxLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """

        if nums==[]:
            return 0

        size = len(nums)
        p = [1]*size
        q = [1]*size
        for i in range(1, size):
            for j in range(i):
                if nums[i]>nums[j]:
                    p[i] = max(p[i], q[j]+1)
                elif nums[i]<nums[j]:
                    q[i] = max(q[i], p[j]+1)

        return max(p[size-1], q[size-1])

notice:

class
def
max
return

nums

IndentationError: unexpected indent: 缩进不标准


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值