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         """
 3         :type nums: List[int]
 4         :rtype: int
 5         """
 6         n = len(nums)
 7         if n <= 1:
 8             return n
 9
10         delta = nums[1] - nums[0]
11         cnt = 1 + (delta != 0)
12
13         for i in range(1, n-1):
14             newDelta = nums[i+1] - nums[i]
15             if newDelta != 0 and newDelta*delta <= 0:
16                 cnt += 1
17                 delta = newDelta
18         return cnt

还可以用inc 和 dec来维护最后小段是升序列,或者是降序列的子序列长度。所以如果nums[x] > nums[x - 1],说明最后一小段是升序列,由于我们需要的摇摆子序列,所以inc = dec + 1。反之同理。最后比较一下inc和dec哪个更长。这么做的好处是避免了分情况处理边界值。

 1 def wiggleMaxLength(self, nums):
 2         """
 3         :type nums: List[int]
 4         :rtype: int
 5         """
 6         size = len(nums)
 7         inc, dec = 1, 1
 8         for x in range(1, size):
 9             if nums[x] > nums[x - 1]:
10                 inc = dec + 1
11             elif nums[x] < nums[x - 1]:
12                 dec = inc + 1
13
14         return max(inc, dec) if size else 0
时间: 2024-10-13 23:00:53

Leetcode 376. Wiggle Subsequence的相关文章

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 t

【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. 采用贪心算法先获取两相邻数的差,再进行判

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 | 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 &quot;Wiggle Subsequence&quot; !

Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, longest wiggle(up\down) subseq AT number i, you will have O(n^2) solution class Solution { struct Rec { Rec(): mlen_dw(0), mlen_up(0){} Rec(int ldw, i

LeetCode 280. Wiggle Sort (摆动排序)$

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 题目标签:Array, Sort 题目给了我们一个nums array, 让我们wiggle sort.

[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

LeetCode Longest Uncommon Subsequence II

原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of o