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" !的更多相关文章

  1. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

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

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. Week13 - 376. Wiggle Subsequence

    Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...

  4. Leetcode 376. Wiggle Subsequence

    本题要求在O(n)时间内求解.用delta储存相邻两个数的差,如果相邻的两个delta不同负号,那么说明子序列摇摆了一次.参看下图的nums的plot.这个例子的答案是7.平的线段部分我们支取最左边的 ...

  5. LeetCode 376. Wiggle Subsequence 摆动子序列

    原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers s ...

  6. [Leetcode 376]摇摆序列 Wiggle Subsequence

    [题目] A sequence of numbers is called a wiggle sequence if the differences between successive numbers ...

  7. 【Leetcode】376. Wiggle Subsequence

    Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...

  8. 376 Wiggle Subsequence 摆动序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  9. [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 ...

随机推荐

  1. ios网络请求特殊字符&处理

    原文地址:http://www.xuebuyuan.com/2039420.html CFURLCreateStringByAddingPercentEscapes 在作项目的的时候,一般都要用到网络 ...

  2. [抓紧小长假的尾巴] 分析一个KeyFileMe

    系统 : Windows xp 程序 : keyfileme 程序下载地址 :http://pan.baidu.com/s/1qYVfvu0 要求 : 编写KeyFile 使用工具 : OD 可在看雪 ...

  3. C# 属性控件2

    PropertyGrid,.net框架下的一个控件,这是一个软件升级的项目,原来的软件用的是C++,控件用的还是第三方,这次升级到visual studio .net4.0版本,原以为.net的东西用 ...

  4. (原创)MongoDB之NoSQL简介

    Nosql简介1.1系统对数据的需求        Nosql[Nosql主要用途大数据处理]的全称为”not only sql”,为非关系型数据库[非关系型数据库就是关系型数据库的所有特点都没有了, ...

  5. JDBC连接数据库演示

    今天重新学习了JDBC连接数据库,使用的数据库是Oracle,在运行前已经手动建立了一张t_user表,建表信息如下: create table t_user( card_id ) primary k ...

  6. Java设计模式(三) 抽象工厂模式

    原创文章,同步发自作者个人博客,转载请注明出处 http://www.jasongj.com/design_pattern/abstract_factory/ 抽象工厂模式解决的问题 上文<工厂 ...

  7. css常用公共样式

    /*style reset*/ body,ul,p,h1,h2,h3,h4,h5,h6,dl,dd,form,input,textarea,select{padding:0; margin:0;fon ...

  8. Java笔记8-抽象接口

    高级特性部分: 抽象(abstract) 接口(interface) 提纲: 抽象类的定义和使用 模板设计模式的使用 接口的定义和使用 回调函数 区别抽象类和接口的异同 软件设计原则--------- ...

  9. Spring可以将简单的组件配置

    这次听了老师的课程,觉得还是需要更加集中的去把各种题进行一个分类吧,然后有针对的去准备,虽然据说这一块在面试中也不容易考到,但是毕竟是难点,还是需要好好准备一下的.因为在dp这个方面,我算是一个比较新 ...

  10. Tomcat编码配置解疑

    环境:tomcat6.0.23 jdk 1.6 相关参考: http://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html http://wiki.ap ...