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/Ruby/Go/Node 之四国大战
Python Flask vs Ruby Sinatra vs Go Martini vs Node Express 本文授权转载自 zybuluo 博客. 题外话一: 最近一段时间,Cloud In ...
- ajax返回正个页面
- net.sf.json.JSONException: Object is null
出现这个错误的原因是net.sf.json.JSONArray或JSONObject转换时,对象内包含另一个对象,而该被包含的对象为NULL,所以抛出异常. 补充: 最可恨的是,明明转换的时候已经成功 ...
- 【转】notepad++ 应用学习 -- 列模式,十六进制模式
Notepad++ 顾名思义,是一个比notepad(Windows下叫记事本)的功能更强的编辑器. 总以为notepad++小巧轻盈,而且开源,要比UE(UltraEdit)好用.因为她支持的视 ...
- Java web 项目搭建
Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring+Hibernate的架构搭建一个 ...
- SwapEffect 枚举(定义交换效果)
由于创建设备时要用到这个值,所以在这里总结一下,以免以后再找. 首先引自msdn: Copy 只能为构成单个后台缓冲区的交换链指定此交换效果. 无论交换链是有窗口的还是全屏的,运行库都保证 Devic ...
- CVE爬虫抓取漏洞URL
String url1="http://www.cnnvd.org.cn/vulnerability/index/vulcode2/tomcat/vulcode/tomcat/cnnvdid ...
- 如何定制Sink扩展.Net Remoting功能
http://www.cnblogs.com/rickie/archive/2004/10/21/54891.html
- Struts2国际化文件乱码解决
方法1:使用native2ascii进行编码转换 代码如下: native2ascii -encoding UTF-8 GlobalMessages.properties NewGlobalMessa ...
- Codeforces 374B - Inna and Nine
原题地址:http://codeforces.com/problemset/problem/374/B 这道题没什么难度,但是考场上就是没写对.Round #220彰显了它的逗比性质——这道题的“标算 ...