最长上升子序列
来源:互联网 发布:国际阿里云实名认证 编辑:程序博客网 时间:2024/05/20 16:43
给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度。
说明
最长上升子序列的定义:
最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的。
https://en.wikipedia.org/wiki/Longest_increasing_subsequence
样例
给出 [5,4,1,2,3]
,LIS 是[1,2,3]
,返回3
给出 [4,2,4,5,3,7]
,LIS 是 [2,4,5,7]
,返回 4
点题:再次强调,勿以题小而不做。关键在正确率。
class Solution {public: /* * @param nums: An integer array * @return: The length of LIS (longest increasing subsequence) */ int longestIncreasingSubsequence(vector<int> &nums) { // write your code here return longestIncreasingSubsequenceDP(nums); } int longestIncreasingSubsequenceDP(vector<int> &nums) { int n = nums.size(); if (n < 2) { return n; } int max_len = 1; vector<int> dp(n, 0); dp[0] = 1; for (int i = 1; i < n; i++) { dp[i] = 1; for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = max(dp[i], dp[j] + 1); max_len = max(max_len, dp[i]); } } } return max_len; }};
阅读全文
0 0
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 输入三条边并判断是不是三角形,算出面积
- 查看虚拟机里的Centos7的IP
- shp加载并合并
- angular 1.2.29版本下 动态添加多个表单、 校验全部、 提交 、ng-form方案
- EL表达式获取URL
- 最长上升子序列
- 深度学习RNN实现股票预测实战(附数据、代码)
- 1.操作系统课程概述
- Python爬虫
- 编写一个手机类(Mobile),包括手机品牌(brand)、手机型号(type), 方法包括显示手机信息,并编写测试类进行对象的创建
- treelist 的编辑与删除
- 共享电子秤未来的方向会好吗?
- 3D游戏中的相机震动效果
- Android 封装工具类之日志