📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
查看: 1640| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

[动态规划] 请教大家一道题:wiggle subsequence

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
376. Wiggle Subsequence 这道题是dynamic programming的题。我看了官方的解法,一和二都懂了,但是对于第三种线性时间的解法存在疑惑。
这个解法核心是这样的:
Any element in the array could correspond to only one of the three possible states:

1. up position, it means nums[i] > nums[i-1]
2. down position, it means nums[i] < nums[i-1]
3. equals to position, nums[i] == nums[i-1]
The updates are done as:

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] remains the same as down[i-1]. 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] remains the same as up[i-1]. If nums[i] == nums[i-1], that means it will not change anything becaue it didn't wiggle at all. So both down[i] and up[i] remain the same as down[i-1] and up[i−1].
我不明白的为什么1只比较了nums[i] > nums[i-1], 难道说nums[i]一定要连接上nums[i-1]咯?可是不一定呀,比方说有可能一个数组中有连续递增的几个数,nums[i]和nums[i-2]连起来也可以呀。我看了网上其他解释也没有讲这一点。


上一篇:Insert In Binary Search Tree我写个helper function
下一篇:请教大家一道关于邻接链表的算法题
推荐
QizhenLaker 2020-1-27 04:01:56 | 只看该作者
全局:
不知道有木有理解对楼主的疑问~ 这题是算subsequence的长度,所以nums[i]的状态是从nums[i-1]的状态转移过来的,不能直接从nums[i-2]跳到nums[i].

评分

参与人数 1大米 +1 收起 理由
yangzhilin + 1 嗯,有道理!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表