动态规划算法 —— LIS (最长上升子序列)

详情可见动态规划算法3——最长上升子序列

 

1.力扣上的题目
300. 最长上升子序列

给定一个无序的整数数组,找到其中最长上升子序列的长度。

示例:

输入: [10,9,2,5,3,7,101,18]
输出: 4 
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
说明:

可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。
你算法的时间复杂度应该为 O(n2) 。
进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-increasing-subsequence

解题代码

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
		vector<int> dp(nums.size());
		int cnt = 0;
		for(int i = 0; i < nums.size(); i++) {
			if (cnt == 0 || nums[i] > dp[cnt-1]) {
				dp[cnt++] = nums[i];
			} else {
				int l = 0, r = cnt-1;
				while (l < r) {
					int mid = l + (r-l) / 2;
					if (dp[mid] >= nums[i]) {
						r = mid;
					} else {
						l = mid + 1;
					}
				}
				dp[l] = nums[i];
			}
		}
		
		return cnt;	
	}
};

 

2.

7-4 列车调度 (25分)

火车站的列车调度铁轨的结构如下图所示。

两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道。每趟列车从入口可以选择任意一条轨道进入,最后从出口离开。在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入。如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度?

输入格式:

输入第一行给出一个整数N (2 ≤ N ≤10​5​​),下一行给出从1到N的整数序号的一个重排列。数字间以空格分隔。

输出格式:

在一行中输出可以将输入的列车按序号递减的顺序调离所需要的最少的铁轨条数。

输入样例:

9
8 4 2 5 3 9 1 6 7

输出样例:

4

 

解题代码1 —— 暴力解题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;
//#define endl '\n'

int nums[1000010];
int save[1000010];

int main()
{
	int n;
	int cnt=0;
	cin>>n;
	for(int i=0; i<n; i++)
		cin>>nums[i];
	
	for(int i=0; i<n;i++) {
		save[i]=1;
		for(int j=0; j<i; j++) {
			if (nums[j]<nums[i] && save[j]+1 > save[i]) {
				save[i]=save[j]+1;
			}
		}
	}
	
	int maxn = 0;
	for(int i=0; i<n; i++)
		maxn = max(maxn, save[i]);
	
	cout<<maxn<<endl;
	
	return 0;
}

结果超时

 

 

解题代码2 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;
//#define endl '\n'

int exits[100010];

int main()
{
	int n;
	int cnt=0;
	cin>>n;
	int temp;
	for(int i=0; i<n; i++) {
		cin>>temp;
		if (cnt == 0 || temp > exits[cnt-1]) {
			exits[cnt++] = temp;
		} else {
			int l = 0, r = cnt - 1;
			while (l < r) {
				int mid = l + (r - l) / 2;
				if (exits[mid] > temp) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			exits[l] = temp;
		}
	}
	
	cout<<cnt<<endl;
	
	return 0;
}

 

 

解题代码3

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;
//#define endl '\n'

int exits[100010];

int main()
{
	int n;
	int cnt=0;
	cin>>n;
	int temp;
	for(int i=0; i<n; i++) {
		cin>>temp;
		if (cnt == 0 || temp > exits[cnt-1]) {
			exits[cnt++] = temp;
		} else {
			int pos = lower_bound(exits, exits + cnt, temp) - exits;
			exits[pos] = temp;
		}
	}
	
	cout<<cnt<<endl;
	
	return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重剑DS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值