要求

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

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

思路

  • 暴力解法:选择所有子序列进行判断((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. 数据仓库系列之ETL中常见的增量抽取方式

    为了实现数据仓库中的更加高效的数据处理,今天和小黎子一起来探讨ETL系统中的增量抽取方式.增量抽取是数据仓库ETL(数据的抽取(extraction).转换(transformation)和装载(lo ...

  2. Recoil 中默认值的正确处理

    继续使用 Recoil 默认值及数据级联的使用 的地域可用区级联的例子. 地域变更后可用区随之联动,两个下拉框皆默认选中第一个可选项. 从 URL 获取默认值 考虑这种情况,当 URL 中带了 que ...

  3. String类的使用2

    /*String:字符串,使用一对""引起来表示.1.String声明为final的,不可被继承2.String实现了Serializable接口:表示字符串是支持序列化的. 实现 ...

  4. Python 高级特性(4)- 生成器

    列表生成式 通过上一篇介绍 列表生成式文章可以知道,它可以快速创建我们需要的列表 局限性 受内存限制,列表生成式创建的列表的容量肯定有限的 不仅占用很大的存储空间,如果我们仅仅需要访问前几个元素,那后 ...

  5. c# 定时启动一个操作、任务

    // 定时启动一个操作.任务 using System; using System.Collections.Generic; using System.Collections.ObjectModel; ...

  6. 6.1vector用法

    目录 一.用法介绍 二.基本用法 三.PAT A1039 一.用法介绍 vector<typename>name; 按照这样的格式进行定义与书写. 注意定义成双数组的情况要加上空格. ve ...

  7. Day09_47_Map

    Map 集合 Map集合** 继承关系** <interface>: Map(接口)<---HashMap(class)/HashTable(class)/SortedMap(子接口 ...

  8. Day09_46_Set集合_SortedSet03

    SortedSet03 让SortedSet集合完成比较,还有另外一种方法,那就是单独编写一个比较器. java.util.comparator 在TreeSet集合创建的时候可以在集合中传入一个比较 ...

  9. TortoiseGit2.12.0-64下载和安装【Windows10】

    TortoiseGit2.12.0-64下载和安装[Windows10] 下载 下载地址:https://tortoisegit.org/download/ 找到合适自己版本的点击后会自动下载 安装 ...

  10. 全图文分析:如何利用Google的protobuf,来思考、设计、实现自己的RPC框架

    目录 一.前言 二.RPC 基础概念 1. RPC 是什么? 2. 需要解决什么问题? 3. 有哪些开源实现? 三.protobuf 基本使用 1. 基本知识 2. 使用步骤 四.libevent 1 ...