[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 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) 时间复杂度完成此题?


12ms

 1 class Solution {
 2     func wiggleMaxLength(_ nums: [Int]) -> Int {
 3         if nums.isEmpty {return 0}
 4         var p:Int = 1
 5         var q:Int = 1
 6         var n:Int = nums.count
 7         for i in 1..<n
 8         {
 9             if nums[i] > nums[i - 1]
10             {
11                 p = q + 1
12             }
13             else if nums[i] < nums[i - 1]
14             {
15                 q = p + 1
16             }
17         }
18         return min(n, max(p, q))
19     }
20 }


40ms

 1 class Solution {
 2     func wiggleMaxLength(_ nums: [Int]) -> Int {
 3
 4         guard nums.count > 0 else { return 0 }
 5         var n = nums.count
 6         var p = Array(repeating: 1, count: n)
 7         var q = Array(repeating: 1, count: n)
 8         for i in 1..<n {
 9             for j in 0..<i {
10                 if nums[i] < nums[j] { q[i] = max(q[i], p[j]+1)}
11                 else if nums[i] > nums[j] { p[i] = max(p[i], q[j]+1)}
12             }
13         }
14         return max(q.last!, p.last!)
15     }
16 }  

原文地址:https://www.cnblogs.com/strengthen/p/10278265.html

时间: 2024-08-29 09:11:06

[Swift]LeetCode376. 摆动序列 | Wiggle Subsequence的相关文章

算法训练 摆动序列

时间限制:1.0s   内存限制:512.0MB 问题描述 如果一个序列满足下面的性质,我们就将它称为摆动序列: 1. 序列中的所有数都是不大于k的正整数: 2. 序列中至少有两个数. 3. 序列中的数两两不相等: 4. 如果第i – 1个数比第i – 2个数大,则第i个数比第i – 2个数小:如果第i – 1个数比第i – 2个数小,则第i个数比第i – 2个数大. 比如,当k = 3时,有下面几个这样的序列: 1 2 1 3 2 1 2 1 3 2 3 2 3 1 3 1 3 2 一共有8种

leetcode 376. 摆动序列 java

题目: 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的.相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零. 给定一个整数序列,返回作为摆动序列的最长子序列的长度. 通过从原始序列中删除一些(

算法训练——摆动序列

//摆动序列 #include<stdio.h> int k,num; int data[22],book[22]; void dfs(int t){ if(t>1){ if(t==2) num++; else{ int flag = 1; for(int i=t-1;i>=2;i--){ //条件4 if((data[i-1]-data[i-2])*(data[i] - data[i-2]) >=0 ){ flag = 0; break; } if(flag == 1) n

376. 摆动序列

如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的.相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零. 给定一个整数序列,返回作为摆动序列的最长子序列的长度. 通过从原始序列中删除一些(也可以不

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]LeetCode324. 摆动排序 II | Wiggle Sort II

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, 5, 1, 6]. Example 2: Input: nums = [1, 3, 2, 2, 3, 1] Output: One po

【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

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