zoukankan      html  css  js  c++  java
  • Leetcode: 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?

    DP solution is O(N^2), better way is greedy

    Now for explanation, we take example series:
    2,1,4,5,6,3,3,4,8,4

    First we check if the series is starting as (big, small) or (small, big). So as 2,1 is big, small. So we will start the loop as we need small number first that is 1 as 2 is already there.

    Step 1: First we check our requirement is to get small number. As 1<2 so the series will be
     2,1
    
    Step 2: Now we need big number that is  greater than 1. As 4>1 so series  will be
    2,1,4
    
    Step 3: Now we need small number. But 5>4 so 4 will be replaced by 5. So the series will become
    2,1,5
    
    Step 4:  We need small number. But 6>5. Series will be
    2,1,6
    
    Step 5: Require small number. 3<6. Series will be
    2,1,6,3
    
    Step 6: Require big number. 3=3. No change in series
    2,1,6,3
    
    Step 7: Require big number. 4>3. Series will become
    2,1,6,3,4
    
    Step 8:  Require small number. 8>4. 8 will  replace 4 and series will become
    2,1,6,3,8
    
    Step 9: Require small number. 4<8. So final series will  be
    2,1,6,3,8,4
    

    Answer is 6.

     1 public class Solution {
     2     public int wiggleMaxLength(int[] nums) {
     3         if (nums.length <= 1) return nums.length;
     4         int i = 1;
     5         while (i<nums.length && nums[i]==nums[i-1]) {
     6             i++;
     7         }
     8         if (i == nums.length) return 1;
     9         int res = 2;
    10         boolean isSmaller = nums[i] < nums[i-1];
    11         i++;
    12         while (i < nums.length) {
    13             if (isSmaller && nums[i]>nums[i-1]) {
    14                 res++;
    15                 isSmaller = !isSmaller;
    16             }
    17             else if (!isSmaller && nums[i]<nums[i-1]) {
    18                 res++;
    19                 isSmaller = !isSmaller;
    20             }
    21             i++;
    22         }
    23         return res;
    24     }
    25 }

    DP solution refer to https://discuss.leetcode.com/topic/52076/easy-understanding-dp-solution-with-o-n-java-version

    This DP consider status.

    For every position in the array, there are only three possible statuses for it.

    • up position, it means nums[i] > nums[i-1]
    • down position, it means nums[i] < nums[i-1]
    • equals to position, nums[i] == nums[i-1]

    So we can use two arrays up[] and down[] to record the max wiggle sequence length so far at index i.

    up[i]表示到i为止,最后是up的max sequence长度; down[i]表示到i为止,最后是down的max sequence长度
    If nums[i] > nums[i-1], that means it wiggles up. the element before it must be a down position. so up[i] = down[i-1] + 1; down[i] keeps the same with before.
    If nums[i] < nums[i-1], that means it wiggles down. the element before it must be a up position. so down[i] = up[i-1] + 1; up[i] keeps the same with before.
    If nums[i] == nums[i-1], that means it will not change anything becasue it didn't wiggle at all. so both down[i] and up[i] keep the same.

    In fact, we can reduce the space complexity to O(1), but current way is more easy to understanding.

     1 public class Solution {
     2     public int wiggleMaxLength(int[] nums) {
     3         
     4         if( nums.length == 0 ) return 0;
     5         
     6         int[] up = new int[nums.length];
     7         int[] down = new int[nums.length];
     8         
     9         up[0] = 1;
    10         down[0] = 1;
    11         
    12         for(int i = 1 ; i < nums.length; i++){
    13             if( nums[i] > nums[i-1] ){
    14                 up[i] = down[i-1]+1;
    15                 down[i] = down[i-1];
    16             }else if( nums[i] < nums[i-1]){
    17                 down[i] = up[i-1]+1;
    18                 up[i] = up[i-1];
    19             }else{
    20                 down[i] = down[i-1];
    21                 up[i] = up[i-1];
    22             }
    23         }
    24         
    25         return Math.max(down[nums.length-1],up[nums.length-1]);
    26     }
    27 }
  • 相关阅读:
    将 Web 项目从 Visual Studio .Net 2002/2003 转换到 Visual Studio 2005 的分步指南
    用 ASP.NET 2.0 改进的 ViewState 加快网站速度
    SQL行列转换实战
    分页存储过程
    分布式系统设计套件
    ASP.NET 2.0 本地化功能:本地化 Web 应用程序的新方法
    在 ASP.NET 页面中使用 TreeView 控件
    SQL Server中的几个方法和Transact SQL 常用语句以及函数[个人推荐]
    ASP.NET 常见问题 和 网页上加上百度搜索
    两台SQL Server数据同步解决方案
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6108453.html
Copyright © 2011-2022 走看看