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 (记忆化搜索)的更多相关文章

  1. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  2. LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...

  3. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  4. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  5. [leetcode] 300. Longest Increasing Subsequence (Medium)

    题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...

  6. LeetCode——300. Longest Increasing Subsequence

    一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...

  7. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. 【刷题-LeetCode】300. Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

随机推荐

  1. C++11新特性:自动类型推断和类型获取

    声明:本文是在Alex Allain的文章http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-functi ...

  2. Topo图

    http://blog.csdn.net/youfangyuan/article/details/8367398 http://joshuaxiao.iteye.com/blog/2224120 ht ...

  3. Android中如何取消调转界面后EditText默认获取聚焦问题

    最近在做一个项目,当我点击跳转至一个带有EditText的界面后,模拟器中的软键盘会自动弹出,严重影响了用户体验.在网上找了资料,现总结如下. 我们知道,EditText有一个 android:foc ...

  4. hdu 4794 FIb求循环节

    很容易看出来这道题是求模n意义下fib数列的最小循环节 对于fib数列的最小循环节的求法,我们可以这样: 1.令n=p1^m1 * p2^m2 * p3^m3…… 2.分别计算fib数列在模p1^m1 ...

  5. xcode 预编译头文件

    xcode 预编译头文件 cocos2d-prefix.pch  #import <Foundation/Foundation.h>

  6. 【转】notepad++ 应用学习 -- 列模式,十六进制模式

      Notepad++ 顾名思义,是一个比notepad(Windows下叫记事本)的功能更强的编辑器. 总以为notepad++小巧轻盈,而且开源,要比UE(UltraEdit)好用.因为她支持的视 ...

  7. CentOS笔记——配置DNS服务器

    前话 咳咳,这次Linux系统的DNS服务器搭建我不得不记下来.,这错误真的太蛋疼了,我整整弄了两天才解决问题(抱歉我很蠢). 也许有人会和我犯同样的错误,给大家分享一下经验. 首先总结一下知识点: ...

  8. C#.Net 如何动态加载与卸载程序集(.dll或者.exe)6-----在不卸载程序域的前提下替换程序集文件。

    当某个程序集文件被载入AppDomain,该文件在AppDomain.Unload之前是不能被替换和删除的.使用AppDomainSetup的影像复制功能可以实现在不卸载程序的情况下替换或者删除程序集 ...

  9. [HIS] HIT行业常用名词及缩写定义

    [HIS] HIT行业常用名词及缩写定义 1.   EHR 居民个人电子健康记录 2.   MPI 居民个人主索引 3.   HIS 医院管理信息系统 4.   CIS 医院临床信息系统 5.   P ...

  10. IOS,Object C学习过程中遇到的attributes

    @property 定义一个属性 @synthesize 告诉编译器自动为属性自动生成 getter 和setter方法 在定义属性的时候会用到如下@attributes nonatomic,告诉编译 ...