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. 关于容器为NavigationControlle时,view的起始位置的问题

    在iOS 7中,苹果引入了一个新的属性“EdgesForExtendedLayout”,默认值为UIRectEdgeAll,默认的布局将从navigationbar的顶部开始,这就是为什么所有元素都往 ...

  2. css 绘制对话框三角符号

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Shell 编写准则

    Shell 中备注符号: # 或者 " 作为Shell中的备注符号. 需要注意的是,首行的#!/bin/bash 不是注释 Shell 格式 首行输入  #!/bin/bash 中间输入运行 ...

  4. BZOJ 1584 DP

    显然序列不能超过sqrt(n),因为最差情况是每个都独立答案为n #include <iostream> #include <cstring> #include <cst ...

  5. POI打印Excel报表

    1.将POI组件中的jar包放入项目中路径下的WEB-INF/lib目录中 2.在javabean中创建一个poi的java文件,中间出了创建getXXX(),setXXX()方法外,加入readRe ...

  6. mac中使用brew安装软件,下载太慢怎么办?

    mac中使用brew安装软件,下载太慢怎么办? 本文所说的软件是指较大的软件,如果软件较小,例如软件只有几M,那么使用此方法后,提升会非常小. 了解brew原理: 1: 从网络下载安装包 2: 执行一 ...

  7. iOS 宏条件编译

    在工程的 build settings 中的 preprocessor macros 下, debug: DEBUG=1 TestApi=1 #if !defined (TestApi) config ...

  8. jQuery method and examples

    一:介绍: jQuery:是DOM和js的封装.jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).现在大多数的pc端的网站都 ...

  9. android 5.0以下版本使用atof报错解决

    经过测试,如果手机系统在5.0之下,项目project.properties的target若在5.0以上(android-20), NDK 使用atof就会报错: cannot locate symb ...

  10. import pysam 出错解决办法

    安装pysam后,import之,结果,出现报错: Library not loaded: libcurl.4.dylib 尝试很多办法,最终发现应当这样解决: # 首先重装curl brew ins ...