当前位置:网站首页>【Leetcode】最长递增子序列问题及应用
【Leetcode】最长递增子序列问题及应用
2022-06-23 03:54:00 【小朱小朱绝不服输】
文章目录
最长递增子序列问题及应用
300. 最长递增子序列
请参考 【Leetcode】计算最长系列(动态规划) 中对 300. 最长递增子序列 的讲解。解题方法主要有两种:动态规划、贪心+二分查找。
面试题 17.08. 马戏团人塔
1.题目描述
leetcode链接:面试题 17.08. 马戏团人塔
2.思路分析
题目要求在2个维度上(即身高 + 体重)同时保持严格递增。
那么我们可以先将其中一个维度排好序,以保证在一个维度上保持递增(此时并非严格递增);之后就可以专注于处理另一个维度。
先对身高体重排序,身高升序排列,身高相同,体重降序排列,这里可以使用二维数组的lambda表达式写法。可以参考:Java数组、ArrayList、HashMap排序总结。
之后就是计算最长递增子序列。身高已经按升序了,只需要判断体重。处理体重的问题就是处理最长递增子序列的问题。
参考最长递增子序列的解法。
3.参考代码
方法一:动态规划(超时)
class Solution {
public int bestSeqAtIndex(int[] height, int[] weight) {
int n = height.length;
int[][] person = new int[n][2];
for (int i = 0; i < n; i++) {
person[i] = new int[]{
height[i], weight[i]};
}
// 身高相同,体重降序
Arrays.sort(person, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
int[] dp = new int[n];
Arrays.fill(dp, 1);
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (person[i][1] > person[j][1]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
res = Math.max(dp[i], res);
}
return res;
}
}
方法二:贪心 + 二分查找
class Solution {
public int bestSeqAtIndex(int[] height, int[] weight) {
int n = height.length;
int[][] person = new int[n][2];
for (int i = 0; i < n; i++) {
person[i] = new int[]{
height[i], weight[i]};
}
// 身高相同,体重降序
Arrays.sort(person, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
int[] dp = new int[n];
Arrays.fill(dp, 1);
int res = 0;
for (int[] per : person) {
int left = 0, right = res;
while (left < right) {
int mid = (right - left) / 2 + left;
if (dp[mid] < per[1]) {
// 要找的是dp数组中第一个小于当前h的位置
left = mid + 1;
} else {
right = mid;
}
}
dp[left] = per[1];
if (res == left) res++;
}
return res;
}
}
二分查找可以直接调API:
通过二分法在已经排好序的数组中查找指定的元素,并返回该元素的下标。
- 如果数组中存在该元素,则会返回该元素在数组中的下标
- 如果数组中不存在该元素,则会返回 -(插入点 + 1)
int res1 = Arrays.binarySearch(int[] arr, int key); // 默认搜索整个数组
int res2 = Arrays.binarySearch(int[] arr, int fromIndex, int toIndex, int key); // 搜索数组指定索引内
class Solution {
public int bestSeqAtIndex(int[] height, int[] weight) {
int n = height.length;
int[][] person = new int[n][2];
for (int i = 0; i < n; i++) {
person[i] = new int[]{
height[i], weight[i]};
}
// 身高相同,体重降序
Arrays.sort(person, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
int[] dp = new int[n];
Arrays.fill(dp, 1);
int res = 0;
for (int[] per : person) {
int i = Arrays.binarySearch(dp, 0, res, per[1]);
if (i < 0) i = -(i + 1); // 如果没找到
dp[i] = per[1];
if (i == res) res++;
}
return res;
}
}
354. 俄罗斯套娃信封问题
1.题目描述
leetcode链接:354. 俄罗斯套娃信封问题
2.思路分析
与上一题相同,只是身高体重的区间换成了信封的长宽,方法完全一样。
直接动态规划会超时,所以可以还是使用二分查找来优化。
3.参考代码
class Solution {
public int maxEnvelopes(int[][] envelopes) {
int n = envelopes.length;
Arrays.sort(envelopes, (a, b) -> (a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]));
int[] dp = new int[n];
Arrays.fill(dp, 1); // 初始化为1,表示当前信封
int max = 0;
// 二分查找
for (int[] env : envelopes) {
int left = 0, right = max;
while (left < right) {
int mid = (right - left) / 2 + left;
if (dp[mid] < env[1]) {
// 要找的是dp数组中第一个小于当前h的位置
left = mid + 1;
} else {
right = mid;
}
}
dp[left] = env[1];
if (left == max) {
max++;
}
}
return max;
}
}
面试题 08.13. 堆箱子
1.题目描述
leetcode链接:面试题 08.13. 堆箱子
2.思路分析
3.参考代码
1691. 堆叠长方体的最大高度
1.题目描述
leetcode链接:1691. 堆叠长方体的最大高度

2.思路分析
3.参考代码
406. 根据身高重建队列
1.题目描述
leetcode链接:406. 根据身高重建队列
2.思路分析
3.参考代码
边栏推荐
- Do280openshift command and troubleshooting -- common troubleshooting and chapter experiments
- Course design C for freshmen -- clothing management system
- Go learning record II (window)
- DO280OpenShift命令及故障排查--常见故障排除和章节实验
- JDBC call stored procedure, MySQL trigger
- Decompile
- 轮播图的实现
- Dolphin scheduler 2.0.5 spark task test summary (source code optimization)
- UI automation positioning edge -xpath actual combat
- C'est dur de trouver un emploi? Ali des trois côtés, heureusement qu'il s'est bien préparé et qu'il a pris un produit.
猜你喜欢

Strong push, quick start to software testing

直接插入排序——【常见排序法(1/8)】

Emergency response HW review

Mini Homer - can you get a remote map data transmission link for hundreds of yuan?

Brief ideas and simple cases of JVM tuning - why do you need JVM tuning?

Visual display of TEQC quality analysis results using teqcplot

Parameter passing of 18 generator function
![Direct insertion sort - [common sort method (1/8)]](/img/c3/f08d789c9ff32dda4f20c58e7c114d.png)
Direct insertion sort - [common sort method (1/8)]

大环境不好难找工作?三面阿里,幸好做足了准备,已拿offer

PRCS-1016 : Failed to resolve Single Client Access Name
随机推荐
硬核,成为高素质测试人员:学会和产品沟通需求
The technological advance of new employees? Journey
HCIP 重发布实验
入行软件测试5年,跳槽3次,我摸透了软件测试这一行
微信小程序:未来老婆查询生成器
How to use data to tell a wonderful story?
Separate use of alertmanager alarms and use of Prometheus configuration alarm rules
《微信小程序-基础篇》带你了解小程序的路由系统(二)
BGP第二次试验
微信小程序:星际旅行飞船乘坐票制作生成
Apache atlas quick start
Function declaration and call of 17 generator
Do280openshift command and troubleshooting -- common troubleshooting and chapter experiments
Strong push, quick start to software testing
【Laravel系列7.8】广播系统
Is data scientist a promising profession?
Meituan's good article: understand swift, Objective-C and the mixing mechanism from the perspective of precompiling
Banner banner
直接插入排序——【常见排序法(1/8)】
Seven year manong Road