LeetCode 376. Wiggle Subsequence 摆动子序列

原题

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.

题目讲解

  题目叫做摆动子序列,顾名思义就是不断摆动的的点,不能连续上升也不能连续下降,只能一上一下不断摆动,接着我们要知道子序列对于原序列来说虽然不要求连续但是要求保序(子串、子数组要求连续),所以这里也就没必要排序了。

示例

Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.

Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].

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

思路及代码

"""
这题的思路其实很简单:
    当三个点事连续上升(下降)时,去掉中间的那个点,因为去掉第一个点会影响之前的结果,去掉最后一个点遇到下降的可能性相对较低。

    如果相乘为零,则代表是折线点,那么结果加一,判断点向后推一位

    下面的代码最费时的地方是相乘的时候,可以优化

"""
class Solution(object):
    def wiggleMaxLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 边界条件
        if len(nums) <= 1:
            return len(nums)
        ans = 1
        # 这里判断有上下折线的方法是求导,相乘为负即为不同
        diff = nums[0] - nums[1]
        if diff != 0:
            ans += 1
        for i in range(2,len(nums)):
            if nums[i] != nums[i - 1]:  # 这里主要是处理相等值的问题
                if diff * (nums[i - 1] - nums[i]) <= 0: # 这里的=0主要是为了处理前面都是相等项到起伏点的情况
                    ans += 1
                    diff = nums[i - 1] - nums[i]

        return ans

  

时间: 2024-10-13 23:22:30

LeetCode 376. Wiggle Subsequence 摆动子序列的相关文章

Leetcode 376. Wiggle Subsequence

本题要求在O(n)时间内求解.用delta储存相邻两个数的差,如果相邻的两个delta不同负号,那么说明子序列摇摆了一次.参看下图的nums的plot.这个例子的答案是7.平的线段部分我们支取最左边的一个点.除了最左边的边界点,我们要求delta != 0, 并且newDelta * delta <= 0.(这里不能只取<号),否则dot 5和7就会被忽略,因为他们的newDelta*delta = 0. 1 def wiggleMaxLength(self, nums): 2 "&

LeetCode | 0392. Is Subsequence判断子序列【Python】

LeetCode 0392. Is Subsequence判断子序列[Easy][Python][双指针] Problem LeetCode Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length

【Leetcode】376. Wiggle Subsequence

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 fe

376. Wiggle Subsequence

不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 题意为给定数组,获取满足要求的最长子串的长度.要求为前后两个数字差为绝对的正负关系(差为0不满足要求). 例如,[1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) . 7-1=6>0, 4-7=-3<0, 9-4=5>0, 2-9=-7<0, 5-2=3>0. 采用贪心算法先获取两相邻数的差,再进行判

(Java) LeetCode 392. Is Subsequence —— 判断子序列

Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and sis a short string (<=100). A subsequence of a

LeetCode OJ - Distinct Subsequence

这道题采用动态规划,可是我一开始没有想到.后来参考了discuss中前辈的代码和思路,才想通的. 方法二是因为每一步只和上一步的内容相关,所以可以只用O(n)的空间复杂度. 下面是AC代码: 1 /** 2 * Solution DP 3 * we keep a m*n matrix and scanning through string T, 4 * p[i][j] means the number of distinct subsequence of S(0...j) equal to T(

LeetCode:递增的三元子序列【334】

LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) . 示例 1: 输入: [1,2,3,4,5] 输出: true 示例 2: 输入:

[Swift]LeetCode376. 摆动序列 | Wiggle Subsequence

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

[LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subseq