要求

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

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

思路

  • 暴力解法:选择所有子序列进行判断((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. CVPR2021| 行人搜索中的第一个anchor-free模型:AlignPS

    论文地址:https://arxiv.org/abs/2103.11617 代码地址:https://github.com/daodaofr/AlignPS 前言: 本文针对anchor-free模型 ...

  2. 第7 章 : 应用编排与管理:Job & DaemonSet

    应用编排与管理:Job & DaemonSet 本节课程要点 Job & CronJobs 基础操作与概念解析: DaemonSet 基础操作与概念解析. Job 需求来源 Job 背 ...

  3. elasticsearch之Java调用本地代码

    虽然Java虚拟机为开发人员屏蔽了底层的实现细节,使得开发人员不用考虑底层操作系统的差异性.不过在某些应用程序中,还是免不了要直接与底层操作系统上的原生代码进行交互.今天我们就来看一下Java对本地调 ...

  4. SpringMVC时间格式和时区解决办法

    问题默认情况下在使用spring的@ResponseBody返回json串时,如果有日期类型的数据,会发现在日期会莫名其妙的差8小时比如:2017-12-20 10:16:23.0结果是:2017-1 ...

  5. gitee 学习笔记

    这个流程只能是在自己的测试仓库中联系哟 首先创建一个自己的仓库 接下来安装git客户端,通过gitee官网给了一个例子创建ssh密钥 然后输入命令 get clone 你仓库的https 或者ssh地 ...

  6. SpringCloud LoadBalancer灰度策略实现

    如何使用 Spring Cloud 2020 中重磅推荐的负载均衡器 Spring Cloud LoadBalancer (下文简称 SCL),如何扩展负载均衡策略? 你将从本文中获取到答案 快速上手 ...

  7. day14.面向对象编程

    一 对象的概念 "面向对象"的核心是"对象"二字,而对象的精髓在于"整合" 所有的程序都是由"数据"与"功能& ...

  8. Symmetry UVA - 1595

      The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper ...

  9. EasyCode Entity 实体类模板 IDEA

    自己修改了一份EasyCode的实体类模板,防止日后找不到在这里存一下 修改了如下内容: 取消生成GetSet方法,改用Lombok 修改默认命名规则,改为[表名Entity.java] 取消了实现序 ...

  10. 100多个很有用的JavaScript函数以及基础写法大集合

    100多个很有用的JavaScript函数以及基础写法大集合 1.document.write("");为 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:docume ...