Leetcode 376. Wiggle Subsequence
来源:互联网 发布:软考网络规划师论文 编辑:程序博客网 时间:2024/06/01 08:58
问题描述
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: 6The entire sequence is a wiggle sequence.Input: [1,17,5,10,13,15,10,5,16,8]Output: 7There 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?
Credits:
Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases.
保存两个数组,一行表示当前数作为最后一个波谷时序列长度,一行表示当前数作为最后一个波峰时的序列长度,所以有,如果当前数比之前的数大,则当前数波峰序列数目为之前数波谷序列数目加1,如果当前数比之前的数小,则当前数波谷序列数目为之前数波峰序列数目加1。
O(n2)代码:
public int wiggleMaxLength(int[] nums) { int n=nums.length; if(n==0) return 0; int[][]dp=new int[2][n]; dp[0][0]=1; dp[1][0]=1; int result=1; for(int i=1;i<n;i++){ dp[0][i]=1; dp[1][i]=1; for(int j=0;j<i;j++){ if(nums[i]>nums[j]){ dp[0][i]=Math.max(dp[0][i],dp[1][j]+1); } if(nums[i]<nums[j]){ dp[1][i]=Math.max(dp[1][i],dp[0][j]+1); } } if(result<dp[0][i]) result=dp[0][i]; if(result<dp[1][i]) result=dp[1][i]; } return result; }
由于每次更新当前数的波峰波谷时必须向前遍历,找到最大的那个可以更新的前一个数,导致算法的复杂度上升达到O(n2)。如果可以将“最大的那个可以更新的前一个数的波峰波谷”向后实时传递,则可以降低复杂度。此时,dp[0][i]表示前(i+)个数最长波动序列的最后一个数是波峰时其序列长度,同理dp[1][i]表示波谷序列长度。
public int wiggleMaxLength(int[] nums) { int n=nums.length; if(n==0) return 0; int[][]dp=new int[2][n]; dp[0][0]=1; dp[1][0]=1; for(int i=1;i<n;i++){ if(nums[i]>nums[i-1]){ dp[0][i]=dp[1][i-1]+1; dp[1][i]=dp[1][i-1]; } else if(nums[i]<nums[i-1]){ dp[0][i]=dp[0][i-1]; dp[1][i]=dp[0][i-1]+1; } else{ dp[0][i]=dp[0][i-1]; dp[1][i]=dp[1][i-1]; } } return dp[0][n-1]>dp[1][n-1]?dp[0][n-1]:dp[1][n-1]; }
dp[i]只与dp[i-1]有关,所以可以只用2个变量来降低空间复杂度。代码忽略
参考链接:http://bookshadow.com/weblog/2016/07/21/leetcode-wiggle-subsequence/
- leetcode 376. Wiggle Subsequence
- leetcode 376. Wiggle Subsequence
- [leetcode] 376. Wiggle Subsequence
- [LEETCODE] 376. Wiggle Subsequence
- [leetcode] 376. Wiggle Subsequence
- [leetcode] 376. Wiggle Subsequence
- LeetCode 376. Wiggle Subsequence
- leetcode 376. Wiggle Subsequence
- 376.[LeetCode]Wiggle Subsequence
- [Leetcode]376. Wiggle Subsequence
- leetcode-376. Wiggle Subsequence
- Leetcode 376. Wiggle Subsequence
- leetcode 376. Wiggle Subsequence
- LeetCode 376. Wiggle Subsequence
- [LeetCode]376. Wiggle Subsequence
- 【LeetCode】 376. Wiggle Subsequence
- leetcode 376. Wiggle Subsequence
- [leetcode]376. Wiggle Subsequence
- javaWeb实战教程2-servlet内置对象和错误处理
- 大牛博客
- 文章标题
- HDU3790 最短路径问题
- javaWeb实战教程3-过滤器filter
- Leetcode 376. Wiggle Subsequence
- javaWeb实战教程4-jdbc连接数据库和junit单元测试
- 记录MySQL遇到的错误
- javaWeb实战教程5-fileupload文件上传
- PAT 1054 求数组中数量超过一半的数字
- dubbo assembly打包
- 《数据结构学习与实验指导》4-7:修理牧场
- 数据库索引
- javaWeb实战教程6-jsp和jstl