leetcode 376. 摆动序列 java

题目:

如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。

例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的。相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。

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

示例 1:

输入: [1,7,4,9,2,5]
输出: 6
解释: 整个序列均为摆动序列。
示例 2:

输入: [1,17,5,10,13,15,10,5,16,8]
输出: 7
解释: 这个序列包含几个长度为 7 摆动序列,其中一个可为[1,17,10,13,10,16,8]。
示例 3:

输入: [1,2,3,4,5,6,7,8,9]
输出: 2
进阶:
你能否用 O(n) 时间复杂度完成此题?

解题:

方法 3:线性动态规划
算法

数组中的任何元素都对应下面三种可能状态中的一种:

上升的位置,意味着 nums[i] > nums[i - 1]nums[i]>nums[i−1]
下降的位置,意味着 nums[i] < nums[i - 1]nums[i]<nums[i−1]
相同的位置,意味着 nums[i] == nums[i - 1]nums[i]==nums[i−1]
更新的过程如下:

如果 nums[i] > nums[i-1]nums[i]>nums[i−1] ,意味着这里在摆动上升,前一个数字肯定处于下降的位置。所以 up[i] = down[i-1] + 1up[i]=down[i−1]+1 , down[i]down[i] 与 down[i-1]down[i−1] 保持相同。

如果 nums[i] < nums[i-1]nums[i]<nums[i−1] ,意味着这里在摆动下降,前一个数字肯定处于下降的位置。所以 down[i] = up[i-1] + 1down[i]=up[i−1]+1 , up[i]up[i] 与 up[i-1]up[i−1] 保持不变。

如果 nums[i] == nums[i-1]nums[i]==nums[i−1] ,意味着这个元素不会改变任何东西因为它没有摆动。所以 down[i]down[i] 与 up[i]up[i] 与 down[i-1]down[i−1] 和 up[i-1]up[i−1] 都分别保持不变。

最后,我们可以将 up[length-1]up[length−1] 和 down[length-1]down[length−1] 中的较大值作为问题的答案,其中 lengthlength 是给定数组中的元素数目。

public class Solution {
    public int wiggleMaxLength(int[] nums) {
        if (nums.length < 2)
            return nums.length;
        int[] up = new int[nums.length];
        int[] down = new int[nums.length];
        up[0] = down[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[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(down[nums.length - 1], up[nums.length - 1]);
    }
}

链接:https://leetcode-cn.com/problems/wiggle-subsequence/solution/bai-dong-xu-lie-by-leetcode/

原文地址:https://www.cnblogs.com/yanhowever/p/11846278.html

时间: 2024-08-28 05:28:24

leetcode 376. 摆动序列 java的相关文章

376. 摆动序列

如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的.相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零. 给定一个整数序列,返回作为摆动序列的最长子序列的长度. 通过从原始序列中删除一些(也可以不

算法训练 摆动序列

时间限制:1.0s   内存限制:512.0MB 问题描述 如果一个序列满足下面的性质,我们就将它称为摆动序列: 1. 序列中的所有数都是不大于k的正整数: 2. 序列中至少有两个数. 3. 序列中的数两两不相等: 4. 如果第i – 1个数比第i – 2个数大,则第i个数比第i – 2个数小:如果第i – 1个数比第i – 2个数小,则第i个数比第i – 2个数大. 比如,当k = 3时,有下面几个这样的序列: 1 2 1 3 2 1 2 1 3 2 3 2 3 1 3 1 3 2 一共有8种

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

POJ 3982 序列(JAVA,简单,大数)

题目 //在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, //可以先建立对象,然后通过对象调用方法: import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger a99(BigInteger a, BigInteger b, BigInteger c) { f

Leetcode: Longest Palindromic Substring. java

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划public class Solution { public String longestPalindrome(String s) { if

[Swift]LeetCode376. 摆动序列 | 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

算法训练——摆动序列

//摆动序列 #include<stdio.h> int k,num; int data[22],book[22]; void dfs(int t){ if(t>1){ if(t==2) num++; else{ int flag = 1; for(int i=t-1;i>=2;i--){ //条件4 if((data[i-1]-data[i-2])*(data[i] - data[i-2]) >=0 ){ flag = 0; break; } if(flag == 1) n

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 t

leetcode 61 Rotate List ----- java

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.题目意思不难,就是说给一个数k,然后从右向左数第k个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len