摘要:題目要求扭動(dòng)序列是指數(shù)組中的相鄰兩個(gè)元素的差保證嚴(yán)格的正負(fù)交替,如數(shù)組中相鄰兩個(gè)元素的差為,滿足扭動(dòng)序列的要求。現(xiàn)在要求從一個(gè)數(shù)組中,找到長(zhǎng)度最長(zhǎng)的扭動(dòng)子序列,并返回其長(zhǎng)度。即前一個(gè)元素和當(dāng)前元素構(gòu)成下降序列,因此代碼如下
題目要求
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?
扭動(dòng)序列是指數(shù)組中的相鄰兩個(gè)元素的差保證嚴(yán)格的正負(fù)交替,如[1,7,4,9,2,5]數(shù)組中相鄰兩個(gè)元素的差為6,-3,5,-7,3,滿足扭動(dòng)序列的要求。現(xiàn)在要求從一個(gè)數(shù)組中,找到長(zhǎng)度最長(zhǎng)的扭動(dòng)子序列,并返回其長(zhǎng)度。
思路和代碼這是一個(gè)可以通過(guò)動(dòng)態(tài)規(guī)劃來(lái)解決的問(wèn)題。動(dòng)態(tài)規(guī)劃的特點(diǎn)就是,加入我知道第i個(gè)元素的結(jié)果,那么第i+1個(gè)元素的結(jié)果可以由其推到出來(lái)。這里假設(shè)我們知道,以第i個(gè)元素為止的最長(zhǎng)子序列長(zhǎng)度,包括上升序列up和下降序列down,則第i+1個(gè)元素的可能情況如下:
nums[i+1]>nums[i]: 即前一個(gè)元素和當(dāng)前元素構(gòu)成上升序列,因此up[i+1]=down[i]+1, down[i+1]=down[i],這是指以第i個(gè)元素為結(jié)尾的上升序列應(yīng)當(dāng)基于第i-1個(gè)元素為結(jié)尾的下降序列,而以第i個(gè)元素為結(jié)尾的下降序列,等同于基于第i-1個(gè)元素為結(jié)尾的下降序列。
nums[i+1]>nums[i]: 即前一個(gè)元素和當(dāng)前元素構(gòu)成下降序列,因此down[i+1]=up[i]+1, up[i+1]=up[i]
nums[i+1]=nums[i]: down[i+1]=down[i], up[i+1]=up[i]
代碼如下:
public int wiggleMaxLength(int[] nums) { if( nums.length == 0 ) return 0; int[] up = new int[nums.length]; int[] down = new int[nums.length]; up[0] = 1; down[0] = 1; for(int i = 1 ; inums[i-1]) { up[i] = down[i-1] + 1; down[i] = down[i-1]; }else if(nums[i] < nums[i-1]) { down[i] = up[i-1] + 1; up[i] = up[i-1]; }else { down[i] = down[i-1]; up[i] = up[i-1]; } } return Math.max(up[nums.length-1], down[nums.length-1]); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/74428.html
摘要:每隔兩位交換一次,如,處理為。難點(diǎn)是會(huì)有相等的元素,而要求相鄰元素除了外,不能相等。那么就不能取排序后相鄰的元素交換,而要和后面的元素交換。例如犧牲空間的做法是,建立一個(gè)新數(shù)組,按照我們想要的規(guī)律放入元素,最后回原數(shù)組。 Wiggle Sort Problem Given an unsorted array nums, reorder it in-place such that num...
摘要:就能滿足題目要求。代碼先將數(shù)組排序?qū)?shù)組中一對(duì)一對(duì)交換交換法復(fù)雜度時(shí)間空間思路題目對(duì)搖擺排序的定義有兩部分如果是奇數(shù),如果是偶數(shù),所以我們只要遍歷一遍數(shù)組,把不符合的情況交換一下就行了。 Wiggle Sort Given an unsorted array nums, reorder it in-place such that nums[0] = nums[2] = nums[i ...
Problem Given an unsorted array nums, reorder it in-place such that nums[0] = nums[2] nums[i-1]) swap(nums, i, i-1); } } } private void swap(int[] nums, int i, int j) { ...
摘要:如果沒(méi)復(fù)雜度的要求,先也可以,再交叉放入數(shù)字也可以。交叉的時(shí)候注意是按照,降序的。 Wiggle Sort 題目鏈接:https://leetcode.com/problems... 這道題允許等號(hào),相對(duì)簡(jiǎn)單,有兩種方法:1. sort然后交換奇數(shù)位和它下一位的元素,2. 不滿足條件的時(shí)候直接交換 可以用遞推來(lái)說(shuō)明一下這么做的正確性: 假設(shè)到第i位之前都滿足題目要求的關(guān)系 現(xiàn)在比較...
摘要:再用二分法找當(dāng)前值應(yīng)該在排好序的數(shù)組中的插入位置。因?yàn)橐业氖亲铋L(zhǎng)的序列,所以每次將排好序的數(shù)組中替換成已經(jīng)排好序的,會(huì)能保證得到的結(jié)果是最長(zhǎng)的。保證升序相等也要替換這個(gè)值 LeetCode[300] Longest Increasing Subsequence Given an unsorted array of integers, find the length of longe...
閱讀 1884·2021-11-17 09:33
閱讀 6470·2021-10-12 10:20
閱讀 2299·2021-09-22 15:50
閱讀 1783·2021-09-22 15:10
閱讀 615·2021-09-10 10:51
閱讀 618·2021-09-10 10:50
閱讀 3020·2021-08-11 11:19
閱讀 1776·2019-08-30 15:55