LeetCode 376. Wiggle Subsequence

简介: 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。

v2-a3b04115a1367c4a6884e6169230d130_1440w.jpg

Description



A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.


For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.


Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.


Example 1:

Input: [1,7,4,9,2,5]

Output: 6

Explanation: The entire sequence is a wiggle sequence.


Example 2:

Input: [1,17,5,10,13,15,10,5,16,8]

Output: 7

Explanation: There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].


Example 3:

Input: [1,2,3,4,5,6,7,8,9]

Output: 2

Follow up:

Can you do it in O(n) time?


描述



如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。


例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的。相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。


给定一个整数序列,返回作为摆动序列的最长子序列的长度。 通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。


示例 1:

输入: [1,7,4,9,2,5]

输出: 6

解释: 整个序列均为摆动序列。


示例 2:

输入: [1,17,5,10,13,15,10,5,16,8]

输出: 7

解释: 这个序列包含几个长度为 7 摆动序列,其中一个可为[1,17,10,13,10,16,8]。


示例 3:

输入: [1,2,3,4,5,6,7,8,9]

输出: 2


进阶:

你能否用 O(n) 时间复杂度完成此题?

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/wiggle-subsequence

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路


  • 使用动态规划求解。动态规划适用于求解「是否」,「有几种」这类型的问题,而如果一个问题要问为什么可以,可以的路径是什么;具体解决问题的各种方案是什么时,动态规划就不适用了。
  • 动态规划类的问题求解有几个要素:1.状态,明确状态的含义是什么;2. 转移方程,当前状态转移到下一个状态的条件是什么;3.结果,求解的结果和状态之间的关系是什么。
  • 关于这道题目,状态是:使用一个一维数组 dp,dp 的每一个元素都是一个有两个元素的数组,dp[i][0] 表示以给定的数组 nums 中的 nums[i] 作为最后一个数来形成的最长摆动序列;此时最后一个数与前一个数之间的差值符号(用 1,-1,0 表示,1 表示 nums[i] - nums[j] >0;-1 表示 nums[i] - nums[j] <0; 0 此时序列只有一个数);dp[i][1] 表示此时形成的最长摆动序列的长度;
  • 转移方程:假设我们已经知道了 dp 的前 i-1 个结果,也就是说以 nums[i-1] 作为摆动序列的最后一个元素,此时的摆动序列长度,与上一个数的差值符号已经知道;以 nums[i-1] 作为摆动序列的最后一个元素,此时的摆动序列长度,与上一个数的差值符号已经知道 ... nums[0] ... 已经知道;为了求解 dp[i],我们从 nums[i-1] 开始向前遍历,找到一个数 nums[j] 使得,(nums[i] - nums[j]) 与 dp[j][0] 的乘积小于 0,说明 nums[i] 可以追加到以 nums[j] 作为结尾的摆动序列后面,于是我们将 dp[i][1] 置为 dp[j][1] + 1;要注意的是,如果乘积等于 0,但是 nums[i] - nums[j] 不为 0,说明以 nums[j] 作为结尾的只有一个数,此时也应当更新 dp[i][1] 置 dp[j][1] + 1;
  • 结果:dp[-1][0]


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-07-13 07:52:43
# @Last Modified by:   何睿
# @Last Modified time: 2019-07-13 08:15:12
from typing import List
class Solution:
    def wiggleMaxLength(self, nums: List[int]) -> int:
        if not nums: return 0
        dp = [[0, 1] for _ in range(len(nums))]
        for i in range(1, len(nums)):
            for j in range(i, -1, -1):
                diff = nums[i] - nums[j]
                if diff * dp[j][0] <= 0 and diff != 0 and (dp[j][1] + 1) > dp[i][1]:
                    dp[i][0] = diff // abs(diff)
                    dp[i][1] = dp[j][1] + 1
                    break
        return dp[-1][1]

源代码文件在 这里


目录
相关文章
|
8月前
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
29 0
|
索引
LeetCode 392. Is Subsequence
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。 你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。 字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
76 0
LeetCode 392. Is Subsequence
|
算法
LeetCode 334. Increasing Triplet Subsequence
给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, 使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。
72 0
LeetCode 334. Increasing Triplet Subsequence
|
算法
LeetCode 300. Longest Increasing Subsequence
给定一个无序的整数数组,找到其中最长上升子序列的长度。
37 0
LeetCode 300. Longest Increasing Subsequence
LeetCode contest 183 5376. 非递增顺序的最小子序列 Minimum Subsequence in Non-Increasing Order
LeetCode contest 183 5376. 非递增顺序的最小子序列 Minimum Subsequence in Non-Increasing Order
【LeetCode】Increasing Triplet Subsequence(334)
  Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
84 0
|
Java
[LeetCode]Longest Continuous Increasing Subsequence 最长连续增长序列
链接:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/难度:Easy题目:674.
1075 0
|
16天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题