leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence is [2, 3, 7, 101]
, therefore the length is 4
. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
class Solution {
public:
int dfs(vector<int>& nums, vector<int> &dp, int v) {
if(dp[v]) return dp[v];
if(v == ) return ; int res = -;
for(int nv=v-;nv>=;--nv) {
if(nums[nv] >= nums[v]) continue;
res = max(res, dfs(nums, dp, nv));
}
dp[v] = res + ;
return dp[v];
}
int lengthOfLIS(vector<int>& nums) {
if(nums.size() == || nums.size() == ) return nums.size(); vector<int> dp(nums.size(), ); int res = -;
for(int i=nums.size()-;i>=;--i) {
dp[i] = dfs(nums, dp, i);
res = max(res, dp[i] + );
} return res;
}
};
leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)的更多相关文章
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode] 300. Longest Increasing Subsequence (Medium)
题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...
- LeetCode——300. Longest Increasing Subsequence
一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
随机推荐
- 关于PYTHON的反射,装饰的练习
从基本概念,简单例子才能慢慢走到高级一点的地方. 另外,PYTHON的函数式编程也是我很感兴趣的一点. 总体而言,我觉得OOP可以作大的框架和思路,FP能作细节实现时的优雅牛X. ~~~~~~~~~~ ...
- 多线程 (三)iOS中的锁
锁的类别:互斥锁,递归锁,条件锁,自旋锁等 锁的实现方式:NSLock,NSRecursiveLock, NSConditionLock,@synchronized,GCD的信号量等 下面说一下常用的 ...
- mac 用 brew
mac 下用 brew 安装插件, 有重复的不会再次安装,比xmap模式好
- linux2.6中的工作队列接口 workqueue_struct
http://blog.csdn.net/sfrysh/article/details/5801786 工作队列接口 工作队列接口是在2.5的开发过程中引入的,用于取代任务队列接口(用于调 度内核任务 ...
- Razor视图引擎布局
不需要像过去aspx一样,使用.Master文件,而是统一使用.cshtml 或 .vbhtml文件.但文件名一般以 _开头,这样做文件不会当做View显示出来 使用@RenderBody() 表示替 ...
- 再分析 返回值加引用&,const
本文主要分析,返回&,和返回值加const的作用. 返回& 定义一个数组模板: template<class T>class Array{ enum{size = 100} ...
- Android帧缓冲区(Frame Buffer)硬件抽象层(HAL)模块Gralloc的实现原理分析[转]
前面在介绍Android系统的开机画面时提到,Android设备的显示屏被抽象为一个帧缓冲区,而Android系统中的SurfaceFlinger服务就是通过向这个帧缓冲区写入内容来绘制应用程序的用户 ...
- Java数据库增删改查
数据库为MySQL数据库,Oracle数据库类似: create database db_test;--创建数据库 ';--创建用户 grant all privileges on db_test.* ...
- bzoj1857
三分的入门题,如果从AB上一点走到D是一个单峰函数从AB开始向CD传送带走的那个点也是一个单峰函数显然三分套三分 const eps=1e-5; var ax,ay,bx,by,cx,cy,dx,dy ...
- BZOJ2140: 稳定婚姻
题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...