[刷题] 300 Longest Increasing Subsequence
要求
- 给定一个整数序列,求其中的最长上升子序列长度
- 子序列元素可不相邻
- 元素相等不算上升
- 一个序列可能有多个最长上升子序列,但最长的长度只有一个
思路
- 暴力解法:选择所有子序列进行判断((2^n)*n)
- 动态规划(n^2)
- LIS(i):[0...i]范围内,选择数字nums[i]可以获得的最长上升子序列长度
- LIS(i) = max( 1 + LIS(j) if nums[i] > nums[j] ) (j<i)


实现
1 class Solution {
2 public:
3 int lengthOfLIS(vector<int>& nums) {
4
5 if( nums.size() == 0 )
6 return 0;
7
8 // memo[i] 表示以 nums[i] 为结尾的最长上升子序列的长度
9 vector<int> memo(nums.size(),1);
10 for( int i = 1 ; i < nums.size() ; i ++ )
11 for( int j = 0 ; j < i ; j ++ )
12 if( nums[j] < nums[i] )
13 memo[i] = max( memo[i] , 1 + memo[j] );
14
15 int res = 1;
16 for( int i = 0 ; i < nums.size() ; i ++ )
17 res = max( res, memo[i] );
18
19 return res;
20 }
21 };
相关
- 376 Wiggle Subsequence
[刷题] 300 Longest Increasing Subsequence的更多相关文章
- LintCode刷题笔记--Longest Increasing Subsequence
标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
- [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 解题报告(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 (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
随机推荐
- java面试-JVM常用的基本配置参数有哪些?
1.-Xms 初始大小内存,默认为物理内存 1/64,等价于 -XX:InitialHeapSize 2.-Xmx 最大分配内存,默认为物理内存的 1/4,等价于 -XX:MaxHeapSize 3. ...
- 201871010129-郑文潇 实验二 个人项目—《D{0-1}背包问题 》项目报告
项目 内容 课程班级博客链接 课程链接 这个作业要求链接 [作业要求](https://www.cnblogs.com/nwnu-daizh/p/14552393.html) 我的课程学习目标 1.掌 ...
- SQL Server 实用语句
创建临时表 #Test CREATE TABLE #Test( ID INT, Name VARCHAR(50) ) INSERT INTO #Test( ID, Name ) VALUES ( 1, ...
- 如何在CSS中映射的鼠标位置,并实现通过鼠标移动控制页面元素效果
映射鼠标位置或实现拖拽效果,我们可以在 JavaScript 中做到这一点.但实际上,在CSS中有更加简洁的方法,我们可以在不使用JavaScript 的情况下,仍然可以实现相同的功能! 只使用CSS ...
- day12.函数其它与模块1
一.函数递归 函数的递归调用:是函数嵌套调用的一种特殊形式 具体指的是在调用一个函数的过程中又直接或者间接地调用自己,称之为函数的递归调用 函数的递归调用其实就是用函数实现的循环 # def f1() ...
- Searching the Web UVA - 1597
The word "search engine" may not be strange to you. Generally speaking, a search engine ...
- Day12_60_多线程的创建和启动(一)
多线程的创建和启动 * 在java语言中实现多线程的第一种方式, 继承 java.lang.Thread; 之后重写run()方法. * 使用多线程之后,主线程和其他线程是不在同一个栈中的,一个线程对 ...
- Ansible(1)- 简单介绍
什么是 Ansible 开源部署工具,也是一个自动化运维工具 开发语言:Python Ansible 的特性 模块化部署管理:调用特定的模块,完成特定任务 三个关键模块:Paramiko(python ...
- 10.for循环
for循环 语法: for(初始化; 布尔表达式; 更新) { //代码语句 } 初始化最先执行,可以声明一种类型,可初始化一个或多个循环控制变量,也可以是一个空语句. 布尔值判断,为 true 执行 ...
- hdu3714 水三分
题意: 给你一些函数 y = ax^2 + bx + c,的a >= 0 的二次函数,x的范围是0--1000, 问你在这个范围内函数值最大的最小是多少,最大指的是对于某一个x最大的 ...