要求

  • 给定一个整数序列,求其中的最长上升子序列长度

    • 子序列元素可不相邻
    • 元素相等不算上升
    • 一个序列可能有多个最长上升子序列,但最长的长度只有一个

思路

  • 暴力解法:选择所有子序列进行判断((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的更多相关文章

  1. LintCode刷题笔记--Longest Increasing Subsequence

    标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...

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

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

  3. 300. Longest Increasing Subsequence

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

  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最长上升子序列 (C++/Java)

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

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

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

  7. Leetcode 300 Longest Increasing Subsequence

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

  8. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

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

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

随机推荐

  1. 前端vue性能优化

    一:代码层次优化 1.1.v-if 和 v-show 区分使用场景 v-if 是 真正 的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建:也是惰性的:如果在初始渲染时 ...

  2. PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...

  3. 复制文件--cp

    cp file1 file2    将文件拷贝到指定路径下 cp -r dir1 dir2  将文件夹拷贝到指定路径下

  4. VIM 编辑器操作详解

    1 vim 使用介绍 1.1 vim 安装 # CentOS 安装: yum install -y vim # Ubuntu 安装: sudu apt-get install vim 安装完成后,可使 ...

  5. 开源组件编排引擎LiteFlow发布里程碑版本2.5.0

    介绍 LiteFlow作为一款轻量级组件编排框架,自开源来,获得了挺多人的关注.社区群也扩展到了接近200人. 早期版本因为疏忽打理,有一些BUG,迭代也不及时.距离上一个稳定版本2.3.3,已经有超 ...

  6. Go Protobuf(比xml小3-10倍, 快20-100倍)

    简介 Protocol Buffers是什么? protocol buffers 是一种灵活,高效,自动化机制的结构数据序列化方法-可类比 XML,但是比 XML 更小.更快.更为简单.你可以定义数据 ...

  7. PMP考位抢夺攻略(二)

    为什么会有第二篇文章呢,因为北京周边的考点太难抢了,都不是页面样式能不能展示的问题了!!! 如何在网页完全打不开的情况下报考PMP? 首先,自动登录. 打开浏览器,输入网址http://exam.ch ...

  8. 如何在IDEA中进行时序图分析

    方法一: 使用插件 SequenceDiagram (系统自动生成) 使用方法: 下载插件,我们可以在 Plugins 中找到 选中线程方法名,然后右键就可以创建此方法的时序图了 参数设置 生成效果以 ...

  9. 死磕Spring之AOP篇 - Spring AOP总览

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  10. Spring Boot+MySQL+Spring Data JPA一个Web的Demo

    2020.06.23 更新 1 概述 一个简单的web项目配合MySQL+Hibernate+Tomcat的简单示例demo,很容易在此基础上扩展成自己的项目. 2 创建工程 笔者IDE为Intell ...