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.

Examples:

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

Follow up:
Can you do it in O(n) time?

Runtime: 0ms.

 1 class Solution {
 2 public:
 3     int wiggleMaxLength(vector<int>& nums) {
 4         int positive = 1, negative = 1, n = nums.size();
 5         for(int i = 1; i < nums.size(); i++) {
 6             if(nums[i] > nums[i - 1]) positive = negative + 1;
 7             else if(nums[i] < nums[i - 1]) negative = positive + 1;
 8         }
 9         return min(n, max(positive, negative));
10     }
11 };
时间: 2024-11-08 15:01:39

Wiggle Subsequence的相关文章

【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

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

[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 &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

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 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 "&

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

算法题解之贪心法

Wiggle Subsequence 最长扭动子序列 思路1:动态规划.状态dp[i]表示以nums[i]为末尾的最长wiggle子序列的长度.时间是O(n^2). 1 public class Solution { 2 public int wiggleMaxLength(int[] nums) { 3 if (nums == null || nums.length == 0) { 4 return 0; 5 } 6 int[] pos_dp = new int[nums.length]; 7

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.