LeetCode "Wiggle Subsequence" !
Another interesting DP. Lesson learnt: how you define state is crucial..
1. if DP[i] is defined as, longest wiggle(up\down) subseq AT number i, you will have O(n^2) solution
class Solution
{
struct Rec
{
Rec(): mlen_dw(), mlen_up(){}
Rec(int ldw, int lup): mlen_dw(ldw), mlen_up(lup){}
int mlen_dw;
int mlen_up;
};
public:
int wiggleMaxLength(vector<int>& nums)
{
int n = nums.size();
if(n < ) return n; vector<Rec> dp(n);
dp[].mlen_up = dp[].mlen_dw = ; int ret = ;
for(int i = ; i < n; i ++)
{
int cv = nums[i];
for(int j = i - ; j >= max(, ret - ); j --)
{
if(cv > nums[j])
{
dp[i].mlen_up = max(dp[i].mlen_up, dp[j].mlen_dw + );
}
else if(cv < nums[j])
{
dp[i].mlen_dw = max(dp[i].mlen_dw, dp[j].mlen_up + );
}
ret = max(ret, max(dp[i].mlen_dw, dp[i].mlen_up));
}
} return ret;
}
};
2. if DP[i] is defined as, longest wiggle(up\down) subseq SO FAR UNTIL number i, you will have O(n) solution
class Solution
{
struct Rec
{
Rec(): mlen_dw(), mlen_up(){}
Rec(int ldw, int lup): mlen_dw(ldw), mlen_up(lup){}
int mlen_dw;
int mlen_up;
};
public:
int wiggleMaxLength(vector<int>& nums)
{
int n = nums.size();
if(n < ) return n; vector<Rec> dp(n);
dp[].mlen_up = dp[].mlen_dw = ; int ret = ;
for(int i = ; i < n; i ++)
{
int cv = nums[i];
dp[i] = dp[i - ];
if(cv > nums[i - ])
{
dp[i].mlen_up = max(dp[i].mlen_up, dp[i - ].mlen_dw + );
}
else if(cv < nums[i - ])
{
dp[i].mlen_dw = max(dp[i].mlen_dw, dp[i - ].mlen_up + );
}
ret = max(ret, max(dp[i].mlen_dw, dp[i].mlen_up));
} return ret;
}
};
3. And, there's always smarter solution - GREEDY!
https://discuss.leetcode.com/topic/52074/concise-10-lines-code-0ms-acepted
LeetCode "Wiggle Subsequence" !的更多相关文章
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- Week13 - 376. Wiggle Subsequence
Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...
- Leetcode 376. Wiggle Subsequence
本题要求在O(n)时间内求解.用delta储存相邻两个数的差,如果相邻的两个delta不同负号,那么说明子序列摇摆了一次.参看下图的nums的plot.这个例子的答案是7.平的线段部分我们支取最左边的 ...
- LeetCode 376. Wiggle Subsequence 摆动子序列
原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers s ...
- [Leetcode 376]摇摆序列 Wiggle Subsequence
[题目] A sequence of numbers is called a wiggle sequence if the differences between successive numbers ...
- 【Leetcode】376. Wiggle Subsequence
Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...
- 376 Wiggle Subsequence 摆动序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
随机推荐
- 用VC调用EXCEL简单代码(转载自越长大越孤单,觉得很好)
首先在stdafx.h里加入对IDispatch接口提供支持的头文件: #include <afxDisp.h> 再在应用程序类的InitInstance()函数里加入: AfxOleIn ...
- JAVA学习遇到的问题:接口实现
引用知乎看到对接口的总结: 接口就是个招牌比如说你饿了,看到前面有个挂着KFC的店,然后你想到可以进去买汉堡了.KFC就是接口,我们看到了这个接口,就知道这个店会卖汉堡(实现接口).那么为什么我们要去 ...
- ubuntu下avd创建和文件传输
进入安卓官网下载安卓sdk并解压到指定路径 在命令行模式下进入安卓文件的toos文件夹下 android avd #在弹出的对话框中新建虚拟机 adb install ~/targetPath/hel ...
- 用Unity代码通过Xml配置生成GameObject之——前两天掉的坑
1. Resources.Load(path),path不是绝对路径,而是相对"Resources/"的相对路径!如: 要想Instantiate则代码应该如下: string m ...
- SQL函数创建错误
[Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration a ...
- 带head的gridview
这是github上的一个项目,根据谷歌的那个HeadGridView改的,因为谷歌的那个addHeadView后宽度不能填充屏幕,下面是代码. 来源:https://github.com/liaohu ...
- Django project structure: how does static folder, STATIC_URL, STATIC_ROOT work
So I've been messing up with Django(1.6+) project setting for quite sometime, this is what i finally ...
- 国内如何使用gem?
答案很简单,使用淘宝镜像! https://ruby.taobao.org/ 造福人类啊! 设置方法: $ gem sources --add https://ruby.taobao.org/ --r ...
- 添加jre/jdk A java Exception occurred
- stunnel-client
#!/bin/bash # giving user passwordless sudo privileges USER=`whoami` MYPATH=$(cat /etc/passwd|grep $ ...