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 ...
随机推荐
- UE4 VR GUI实现 参考(UMG AND VR)
Note:4.13以后版本VR UI采用 WidgetComponent + WidgetInteractionComponent可轻松实现交互 Blueprint Demo: https://for ...
- 初识 Burp Suite
Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程. 所有的工具都共享一个能处理并显示HTTP 消息, ...
- 第三个Sprint总结
工作进展:项目基本已经完成 工作状况:
- Pixel VS Point, FrameBuffer VS RenderBuffer
// How iOS app MVC works View, Window, AppDelegate ViewController, RootViewController // On Pixel VS ...
- 获取select标签的值
1.设置value为pxx的项选中 $(".selector").val("pxx"); 2.设置text为pxx的项选中 $(".selector& ...
- Oracle数据类型隐式转换小析
测试使用环境:oracle 11g r1 平常写sql语句时,大大咧咧,不太注意和数字有关的数据类型,有时例如 where c1=111 和 where c1='111'这样混用,却不曾想这里面另有蹊 ...
- Codeforces Round #381 (Div. 2) A B C 水 构造
A. Alyona and copybooks time limit per test 1 second memory limit per test 256 megabytes input stand ...
- iOS纯代码适配masonry中mas_的问题
//equalto 和 mas_equalto 是有区别的.但是我们不打算去了解,可以通过添加以下代码来统一. //注意!! 宏定义必须要放在 import 引入头文件之前! //define thi ...
- c语言数据结构之 快速排序
编译器:VS2013 #include "stdafx.h"#include<stdlib.h> //函数声明 void QuickSort(int a[],int n ...
- IOS 线程处理 子线程
IOS 线程处理 子线程的启动与结束 技术交流新QQ群:414971585 IOS中,如果要在主线程中启动一个子线程,可以又两种方法: [NSThread detachNewThreadSelec ...