要求

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

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

思路

  • 暴力解法:选择所有子序列进行判断((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. 1 class Solution {
  2. 2 public:
  3. 3 int lengthOfLIS(vector<int>& nums) {
  4. 4
  5. 5 if( nums.size() == 0 )
  6. 6 return 0;
  7. 7
  8. 8 // memo[i] 表示以 nums[i] 为结尾的最长上升子序列的长度
  9. 9 vector<int> memo(nums.size(),1);
  10. 10 for( int i = 1 ; i < nums.size() ; i ++ )
  11. 11 for( int j = 0 ; j < i ; j ++ )
  12. 12 if( nums[j] < nums[i] )
  13. 13 memo[i] = max( memo[i] , 1 + memo[j] );
  14. 14
  15. 15 int res = 1;
  16. 16 for( int i = 0 ; i < nums.size() ; i ++ )
  17. 17 res = max( res, memo[i] );
  18. 18
  19. 19 return res;
  20. 20 }
  21. 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. Asp Net Core 5 REST API 使用 RefreshToken 刷新 JWT - Step by Step

    翻译自 Mohamad Lawand 2021年1月25日的文章 <Refresh JWT with Refresh Tokens in Asp Net Core 5 Rest API Step ...

  2. Spring MVC(七篇)

    (一)Spring MVC简介 (二)SpringMVC核心控制器 (三)Spring MVC Controller接口控制器详解(一) (三)Spring MVC Controller接口控制器详解 ...

  3. MySQL数据库高级三:查询截取分析(了解)

  4. (十一)Docker-DinD

    1. Docker in Docker Step 1. Start a daemon instance $ docker run --privileged --name some-docker -d ...

  5. 我与Git的那些破事(上)--代码管理

    1. Git是什么? 作为一名程序猿,我相信大家都或多或少接触过git--分布式版本控制软件. 有人说,它是目前世界上最先进的分布式版本控制系统,我想说,是否最先进不知道,但确实好用,实用. 作为一款 ...

  6. 善用k8s explain

    使用kubectl explain来快速了解用法 例如 kubectl explain deployment.spec 或 kubectl explain deployment.spec.strate ...

  7. 进击中的Vue 3——“电动车电池范围计算器”开源项目

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文参考:https://dzone.com/articles/build-a-tesla-battery- ...

  8. hdu 2841 Visible Trees(容斥)

    原文链接 There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is stand ...

  9. docker日志设置

    最近查看docker日志的时候,使用命令docker log -f 会出现日志无限翻滚的情况,这些日志都是打印到控制台的,但是都被docker收集了起来,放到了/var/lib/docker/cont ...

  10. flex 的 三个参数 flex:1 0 auto

    flex :flex-group  flex-shirk  flex-basis ①.flex-group 剩余空间索取 默认值为0,不索取 eg:父元素400,子元素A为100px,B为200px. ...