376. Wiggle Subsequence

Question

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


Code

public class Solution {
    public class Data {
        public int pre;
        public int count;
        
        public Data(int index) {
            this.pre = index;
            this.count = 1;
        }
    }
    
    public int wiggleMaxLength(int[] nums) {
        if (nums.length <= 1) return nums.length;
        Data[] dp = new Data[nums.length];
        dp[0] = new Data(0);
        
        int max = 1;
        for (int i = 1; i < dp.length; i++) {
            dp[i] = new Data(i);
            for (int j = 0; j < i; j++) {
                if (nums[j] != nums[i]) {
                    if (dp[j].pre == j) {
                        dp[i].count = Math.max(dp[i].count, 1 + dp[j].count);
                        dp[i].pre = j;
                        continue;
                    }
                    
                    int first = nums[dp[j].pre], second = nums[j], third = nums[i];
                    if ((second - first < 0 && third - second > 0) || (second - first > 0 && third - second < 0)) {
                        dp[i].count = Math.max(dp[i].count, 1 + dp[j].count);
                        dp[i].pre = j;
                    }
                }
            }
            max = Math.max(max, dp[i].count);
        }
        
        return max;
    }
}

Solution

動(dòng)態(tài)規(guī)劃 + 貪心。

自定義一個(gè)內(nèi)部類(lèi),包含2個(gè)元素:pre指序列中當(dāng)前元素的前一個(gè)元素在nums中的下標(biāo),count指以當(dāng)前元素結(jié)尾的序列的長(zhǎng)度。

dp[i].count表示以nums[i]結(jié)尾的序列的最長(zhǎng)長(zhǎng)度。
初始化時(shí),dp[0].pre = 0, count = 1;

狀態(tài)轉(zhuǎn)移方程:dp[i].count = Math.max(dp[i].count, 1 + dp[j].count);
注意可以轉(zhuǎn)移的條件即可。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,047評(píng)論 0 23
  • 【Day 15】和孩子一起學(xué)習(xí)一起成長(zhǎng)感到很快樂(lè)。我雖說(shuō)讀了英語(yǔ)專(zhuān)業(yè)但英文歌一首都不會(huì)唱。最近陪貝貝一起學(xué)兒歌,我...
    幸福滿屋Emily閱讀 211評(píng)論 0 0
  • 江水流兮浪依舊 聲勢(shì)滔天 大雨落兮傾漂泊 接天蓮葉 朱雀降兮現(xiàn)神光 涅槃重生 白虎嘯兮風(fēng)縹緲 震懾禽獸 青龍騰兮云...
    山河莽莽閱讀 550評(píng)論 0 0
  • 早上醒來(lái)睜開(kāi)眼睛,習(xí)慣性的看看陽(yáng)臺(tái)外面,沒(méi)有象往常一樣,清晨的陽(yáng)光映照在蔚藍(lán)的天空,把對(duì)面像城堡一樣的建筑物染成金...
    隨性自在的杜姐閱讀 551評(píng)論 14 13
  • 極簡(jiǎn)主義生活方式,是對(duì)自身的再認(rèn)識(shí),對(duì)自由的再定義。 ?深入分析自己,首先了解什么對(duì)自己最重要,然后用有限的時(shí)間和...
    木頭鞋閱讀 907評(píng)論 1 17

友情鏈接更多精彩內(nèi)容