LeetCode 376. Wiggle Subsequence -动态规划解法

在AutoDL的A100云服务器上,用Docker搞定Sapien+RobotWin环境(附完整conda list) 本文详细介绍了在AutoDL的A100云服务器上使用Docker构建Sapien+RobotWin环境的完整指南。通过配置Vulkan渲染管线、精确复现Conda环境以及解决常见问题,帮助研究人员高效搭建稳定的机器人仿真开发环境,特别适合需要高性能GPU加速的复杂仿真任务。 阅读详情

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

这个题目要注意的就是那个元素如果相等需要跳过,其他思路类似于之前我写的那篇文章的思路,只是判断条件变了,注意点细节就好,传送门:LeetCode 300 最长上升子序列

public class WiggleMaxLength {
    public static int max(int a, int b) {
        if (a > b)
            return a;
        else
            return b;
    }

    public static int wiggleMaxLength(int[] nums) {
        int N = nums.length;
        if (N <= 1)
            return N;
        int flag=-1;
        int Max; //记录最大值
        int keynums[] = new int[nums.length];
        if (nums[0] != nums[1]) { //初始化如果前两个元素相等那么初始值是1和2,否则初始值都是1
            Max = 2;
            keynums[0] = 1;
            keynums[1] = 2;
        } else {
            Max = 1;
            keynums[0] = 1;
            keynums[1] = 1;
        }
        for (int i = 2; i < N; i++)
            keynums[i] = 1;

        for (int i = 2; i < N; i++) {
            int temmax = 1;
            for (int j = i - 1; j >= 1; j--) {
                if (nums[i] > nums[j] && nums[j] < nums[j - 1]) { //当前元素与之前元素比较
                    temmax = max(temmax, keynums[j] + keynums[i]);


                } else if(nums[i] < nums[j] && nums[j] > nums[j - 1]){

                    temmax = max(temmax, keynums[j] + keynums[i]);

                } else if(nums[i] != nums[j]){
                    temmax = max(temmax, 2);

                } else if(nums[i]==nums[j]) {//如果元素相等,赋值给前面那个相等元素相同的值便可跳出循环
                    flag=2;
                    keynums[i]=max(temmax,keynums[j]);
                    break;
                    }

            }
            if(flag!=2)
            keynums[i] = temmax;
            flag=-1;
            if (temmax > Max)
                Max = temmax;
        }

        return Max;
    }



}
一文读懂 CST 时/频域求解器 本文系统介绍了CST Studio Suite中的关键网格技术——六面体网格、四面体网格以及TLM求解器专用的八叉树非均匀网格技术。重点阐述了六面体网格的三项核心技术:理想边界拟合(PBA)技术可精确处理曲面边界,薄片技术(TST)能高效模拟薄层结构,自适应八叉树网格实现计算资源最优分配。同时详细对比了六面体网格与四面体网格的特点及适用场景,前者适用于系统级EMC/EMI分析等时域问题,后者擅长处理复杂曲面和异质材料的频域问题。 阅读详情

相关推荐

高通410随身WiFi刷OpenWRT与Debian双系统一键集成方案

本文详细介绍高通410随身WiFi设备刷入OpenWRT与Debian双系统的一键集成方案。通过虎王工作室提供的一键刷机包,用户可轻松实现双系统部署,兼顾OpenWRT的网络功能与Debian的完整应用服务,适用于网络加速、服务器部署等多种场景,并提供了硬件资源分配与系统切换的实用指南。

docker7nomad的博客 1135

376. 摆动序列(动态规划)

在本文章中,我们将要详细介绍一下Leetcode中376. 摆动序列,在本道题目中,我们将会用动态规划的方式解决。以上就是我们对Leetcode摆动序列详细介绍,希望对大家的学习有所帮助,仅供参考 如有错误请大佬指点我会尽快去改正 欢迎大家来评论~~

lim6ere的博客 1126

QGIS:安装Deepness插件+应用

简单的描述了deepness的安装应用历程,重点是记录博主本次遇到的坑

fengyi157的博客 2331

(LeetCode 376)摆动序列 [动态规划 O(n) 和 O(n^2) 解答 详细思路 ]

376. 摆动序列 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。 例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的。相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列...

STILLxjy 851

SDR实战(四)-AD9361使用手册(三)

本节定义了并行数据端口和串行外设接口(SPI),它们使得AD9361与基带处理器(BBP)之间能够传输数据和控制/状态信息。图63展示了这些接口,并提供了AD9361和BBP在宽带无线系统中使用方式的高级概述。数据接口以两种模式之一运行:兼容标准CMOS模式或兼容低压差分信号(LVDS)模式。每个接口都具有以下部分描述的独特特性。使用CMOS模式时:保持了单端-CMOS逻辑兼容性。可以使用一个或两个数据端口。使用两个端口可以提高数据吞吐量。

hutx0924的博客 6243

用 PERFORM ... ON COMMIT / ON ROLLBACK 把收尾工作做干净:从 SAP LUW 到 Update Task 的事务钩子实践

ABAP事务钩子机制解析 本文深入探讨SAP ABAP开发中的事务边界管理问题,重点分析PERFORM ... ON COMMIT/ROLLBACK机制: 事务分层:区分Database LUW(数据库提交)和SAP LUW(业务逻辑单元),后者通过COMMIT WORK触发多种收尾动作 钩子机制:PERFORM...ON COMMIT将子程序注册为事务结束回调,在提交/回滚时执行清理工作而非业务更新 执行顺序:钩子程序先于Update Task执行,仍保留当前会话内存上下文 现代实践:建议将FORM仅作为

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3993

leetcode 376. Wiggle Subsequence 最长摆动序列 + 动态规划DP + 这道题很棒

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 pos

JackZhangNJU的专栏 1706

U盘插入电脑时发出声音但不显示——编程解决方案

以上是一些常见的编程解决方案,帮助您解决U盘插入电脑时发出声音但不显示的问题。请注意,这些解决方案可能因不同的情况而略有不同,因此请根据您的具体情况进行调整和尝试。当您将U盘插入电脑时,如果您听到了连接声音,但是U盘却没有在文件资源管理器或桌面上显示出来,这可能是由于一些常见问题导致的。U盘的文件系统格式可能与您的计算机不兼容,导致无法正确识别。如果输出显示U盘的文件系统格式与您的计算机不兼容,您可以尝试重新格式化U盘或使用其他兼容的文件系统格式。其中,X是U盘的磁盘编号,Y是您想要分配给U盘的盘符。

ByteEchoX的博客 2461

动态规划-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 eit

格物致知 356

ThingsBoard 场景联动需求

当初有个兄弟请我帮他实现一个需求,需求如上,很简单,实现完成后我怕他操作不来,又写了这份文档给他,今天把这个文档的场景发出来,大家也可以学习一下思路我把需要模拟设备的指令都总结在下面,连接参数截图讲解如下:温度传感器风扇光照传感器灯。

AldrichEugene 1232

FineReport实战-- 国内外各地区销售数据分析 多个图表联动

进行国内外各地区业务员销售数据分析是一项复杂但极具价值的任务,它可以帮助企业了解不同市场的表现,并据此调整销售策略以提高整体绩效。实现功能:同一个报表里面多个图表联动问题。

Davina_yu的博客 1198

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 pos

不积跬步无以至千里 435

RISC-V CPU设计(三)---多周期CPU的设计与实现

基本情况 本多周期CPU支持RV32I指令集(fence,ecall,ebreak除外),已通过部分指令的测试,如代码有bug,欢迎指出。 模块划分 代码将CPU划分为以下几个模块: 1.控制单元:ControlUnit 用于产生控制信号,是CPU的大脑,指挥中枢。 2.分支决策单元:BranchUnit 用于决定B型指令是否发生跳转 3.立即数单元:ImmUnit 用于从指令中解析出立即数. 4.寄存器堆:RegisterFile RISC-V的32个寄存器,其中x0寄存器恒为0,不可更改。 5.指令存储

qq_40268672的博客 5542

Leetcode 376. Wiggle Subsequence (python+cpp)

Leetcode 376. Wiggle Subsequence题目解析:动态规划 题目 解析:动态规划 这道题目时间复杂度最低的解法反而是最容易理解的解法,至于解法的话LC官方的解释我觉得这次非常的清楚,所以直接引用吧: python代码如下: class Solution: def wiggleMaxLength(self, nums: List[int]) -> int: n = len(nums) if n<2: return n

qq_37821701的博客 260

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Leetcode学习日志-376 Wiggle Subsequence

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 dif

Zarlove的博客 532

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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 elemen

weixin_45771864的博客 179

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【LeetCode】376. Wiggle Subsequence 解题报告(Python)

【LeetCode】376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/wiggle-subsequence/description/ 题目描述: A sequence of num...

负雪明烛 2197

Java描述 LeetCode,376. Wiggle Subsequence 摇摆序列

大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢。 1-1:题目 A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (.

人在江湖 779

Leetcode 376. Wiggle Subsequence 解题报告

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 firs

rjx博客 283

[LeetCode]376. Wiggle Subsequence

https://discuss.leetcode.com/topic/51946/very-simple-java-solution-with-detail-explanation/2 本题两解,均为Time:O(N);Space:O(1)。关键在于当nums[i] > nums[i - 1]时,上摆数组长度为下摆数组长度加一,反之亦然。 解法一: public clas

gqk289的专栏 290

LeetCode 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 e...

ConnieWu的博客 540

LeetCode C++ 376. Wiggle Subsequence【Dynamic Programming】中等

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 elemen

memcpy0的博客 430

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 pos...

weixin_30883777的博客 70

[解题报告]376. Wiggle Subsequence

Problem Background: 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

Mark__Zeng的专栏 1056

老晨Leetcode 376. Wiggle Subsequence 摆动序列

如果连续数字之间的差严格地在正和负之间交替,则这样的数字序列称为摆动序列。 第一个差值(如果存在)可以是正的也可以是负的。 少于两个元素的序列通常是摆动序列。 例如,[1,7,4,9,2,5]是一个摆动序列,因为连续数字的差(6,-3,5,-7,3)交替为正和负。 相反,[1,4,7,2,5]和[1,7,4,5,5]不是摆动序列,第一个是因为它的前两个连续数字的差是正的,而第二个是因为它的最后一...

weixin_45588823的博客 178

LeetCode.376 - Wiggle Subsequence

  求最长摆动子序列。   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 tha...

ding7530的博客 123
上一篇: LeetCode 300. Longest Increasing Subsequence -动态规划的使用
下一篇: LeetCode 416. Partition Equal Subset Sum-0-1背包变型
HJsir
博客等级 码龄10年 19粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值