要求

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

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

思路

  • 暴力解法:选择所有子序列进行判断((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. pwn题命令行解题脚本

    目录 脚本说明 脚本内容 使用 使用示例 参考与引用 脚本说明 这是专门为本地调试与远程答题准备的脚本,依靠命令行参数进行控制. 本脚本支持的功能有: 本地调试 开启tmux调试 设置gdb断点,支持 ...

  2. 阳明-K8S训练营全部文档-2020年08月11日14:59:02更新

    阳明-K8S训练营全部文档 Docker 基础 简介 安装 基本操作 Dockerfile Dockerfile最佳实践 Kubernetes 基础 简介 安装 资源清单 Pod 原理 Pod 生命周 ...

  3. Mysql之案例分析(一)

    可见性分析 CREATE TABLE `t` ( `id` int(11) NOT NULL, `k` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGIN ...

  4. sql注入之超详细sqlmap使用攻略

    0x00 前言 干过sql注入的小伙伴们一定听说过sqlmap的大名,但是面对一些特殊情况,sqlmap也不一定"好使",这里的"好使"并不是真正不好使的意思, ...

  5. 201871030140-朱婷婷 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告

    项目 内容 课程班级博客链接 2018级卓越班 这个作业要求链接 实验三 结对项目 我的课程学习目标 1.体验软件项目开发中的两人合作,练习结对编程:2.掌握GitHub协作开发程序的操作方法. 这个 ...

  6. 如何用 Electron + WebRTC 开发一个跨平台的视频会议应用

    在搭建在线教育.医疗.视频会议等场景时,很多中小型公司常常面临 PC 客户端和 Web 端二选一的抉择.Electron 技术的出现解决了这一难题,只需前端开发就能完成一个跨平台的 PC 端应用.本文 ...

  7. 解决SQLPLUS无法使用上下箭头

    1 问题描述 SQLPLUS中使用上下箭头无法获取历史命令,如下图所示: 按上下箭头会显示^[[A/^[[B. 2 解决方案 需要安装rlwrap,可以的话可以用包管理器安装,笔者环境CentOS,这 ...

  8. Mariadb3—多表查询

    1.内关联 select 字段名 from 表名1 inner join 表名2 on 表名1.字段名=表名2.字段名 where 条件 2.左关联 select 字段名 from 表名1 left ...

  9. IDEA xml 注解快捷键

    注释:CTRL + SHIFT + / 撤销注释:CTRL + SHIFT + \

  10. Updating a Dictionary UVA - 12504

    In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...