• [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.

    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] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。

    给定一个整数序列,返回作为摆动序列的最长子序列的长度。 通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/wiggle-subsequence
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路是动态规划。这道题的思路很像300题最长上升子序列,我们创建两个数组up和down,表示以nums[i]为结尾的,最后是上升/下降的最长子序列的长度。

    如果nums[i] > nums[i - 1],说明最后是上升的,最长子序列的长度是down[i - 1] + 1,因为最后是上升的话,再往前应该是下降的

    如果nums[i] < nums[i - 1],说明最后是下降的,最长子序列的长度是up[i - 1] + 1,因为最后是下降的话,再往前应该是上升的

    如果nums[i] == nums[i - 1],说明已经破坏了上升/下降的规则,则最长子序列的长度跟前一个位置相同

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int wiggleMaxLength(int[] nums) {
     3         // corner case
     4         if (nums.length <= 1) {
     5             return nums.length;
     6         }
     7 
     8         // normal case
     9         int len = nums.length;
    10         int[] down = new int[len];
    11         int[] up = new int[len];
    12         down[0] = 1;
    13         up[0] = 1;
    14         for (int i = 1; i < len; i++) {
    15             if (nums[i] > nums[i - 1]) {
    16                 down[i] = down[i - 1];
    17                 up[i] = down[i - 1] + 1;
    18             } else if (nums[i] < nums[i - 1]) {
    19                 down[i] = up[i - 1] + 1;
    20                 up[i] = up[i - 1];
    21             } else {
    22                 down[i] = down[i - 1];
    23                 up[i] = up[i - 1];
    24             }
    25         }
    26         return Math.max(down[len - 1], up[len - 1]);
    27     }
    28 }

    另外提供一个不使用额外空间的做法。

     1 class Solution {
     2     public int wiggleMaxLength(int[] nums) {
     3         // corner case
     4         if (nums.length <= 1) {
     5             return nums.length;
     6         }
     7 
     8         // normal case
     9         int up = 1;
    10         int down = 1;
    11         for (int i = 1; i < nums.length; i++) {
    12             if (nums[i] > nums[i - 1]) {
    13                 up = down + 1;
    14             } else if (nums[i] < nums[i - 1]) {
    15                 down = up + 1;
    16             }
    17         }
    18         return Math.max(up, down);
    19     }
    20 }

    LIS类相关题目

    LeetCode 题目总结

  • 相关阅读:
    CentOS7安装Oracle 11gR2 安装
    CentOS7 FTP服务搭建(虚拟用户访问FTP服务)
    .NET中RabbitMQ的使用
    ElasticSearch(站内搜索)
    SignalR 2.1 简单入门项目
    Oracl基础知识(一)
    CentOS6—HAProxy安装与配置
    Redis C#缓存的使用
    CentOS6— Redis安装(转和延续)
    Linux(CentOS)常用操作指令(二)
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14127971.html
Copyright © 2020-2023  润新知