[刷题] 300 Longest Increasing Subsequence
要求
- 给定一个整数序列,求其中的最长上升子序列长度
- 子序列元素可不相邻
- 元素相等不算上升
- 一个序列可能有多个最长上升子序列,但最长的长度只有一个
思路
- 暴力解法:选择所有子序列进行判断((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的更多相关文章
- LintCode刷题笔记--Longest Increasing Subsequence
标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
随机推荐
- ubuntu系统编译安装OpenCV 4.4
内容转载自我的博客 目录 前言 1. 下载源码 2. 安装各种依赖 3. 开始编译安装 4. 配置C++开发环境 5. 程序执行时加载动态库*.so 6. 测试cpp文件 7. 配置python3的o ...
- Node.js核心入门
前言: 因为以前学习Node.js并没有真正意义上的去学习它,而是粗略的学习了npm的常用命令和Node.js一些模块化的语法,因此昨天花了一天的时间看了<Node.js开发指南>一书.通 ...
- pandas(1):Pandas文件读取——read_excel()
目录 一.函数原型 二.功能说明 三.常用参数说明 四.总结 一.函数原型 pd.read_excel(io, sheet_name=0, header=0, names=None, index_co ...
- hibernate的三种查询方式
hibernate的三种查询方式 目录 hibernate的三种查询方式 1.HQL查询 1.1.SQL概述 1.2.实体查询 1.3.带where的查询 1.3.属性查询 1.4.实体的更新和删除 ...
- 浅入Kubernetes(11):了解 Service 和 Endpoint
目录 Srevice Service 的创建及现象 Service 定义 Endpoint slices 创建 Endpoint.Service Service 创建应用 创建 Endpoint 浅入 ...
- Day13_67_interrupt() 方法
interrupt() 方法 中断线程 * interrupt()方法的简单理解 - interrupt() 方法只是改变线程的阻塞状态而已,让一个正在阻塞状态的线程,恢复执行.但是它不会中断一个正在 ...
- k8s deployment 金丝雀发布 更新的 暂停 恢复 回滚
假设现在有业务需求,计划将所有的nginx 从镜像版本1.14更新到1.15,这一次发布不紧需要平滑发布,还需要 金丝雀发布,及确认其中一个Pod没有问题后在进行剩余的更新. 暂停与恢复也可以使用ym ...
- 数据结构(5):Java实现二叉树
二叉树图: package com.test.Sort; import java.util.ArrayList; import java.util.LinkedList; public class B ...
- 基于vite2+electron12后台管理模板|Electron后台框架系统
前一溜时间有给大家分享一个 electron+vite跨端短视频 项目.这次分享的是vite2.x和electron实现跨平台后台框架,支持国际化多语言配置.导航菜单+树形菜单两种路由菜单模式.展开/ ...
- Python 极速入门指南
前言 转载于本人博客. 面向有编程经验者的极速入门指南. 大部分内容简化于 W3School,翻译不一定准确,因此标注了英文. 包括代码一共两万字符左右,预计阅读时间一小时. 目前我的博客长文显示效果 ...