当前位置:网站首页>【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.参考代码
边栏推荐
- 微信小程序:星际旅行飞船乘坐票制作生成
- 三层架构实验
- vmware网络连接出错Unit network.service not found
- 图片降噪DeNoise AI
- 轮播图的实现
- 大环境不好难找工作?三面阿里,幸好做足了准备,已拿offer
- ② Cocoapods principle and podspec file uploading operation
- "Wechat applet - Basics" takes you to understand the routing system of the applet (2)
- [OFDM communication] simulation of OFDM multi-user resource allocation based on MATLAB [including Matlab source code 1902]
- 【Laravel系列7.8】广播系统
猜你喜欢
![[graph theory] - bipartite graph](/img/2d/999820edafe7294440cf1873a26084.png)
[graph theory] - bipartite graph

LeetCode 797:所有可能的路径

TabControl style of WPF basic control

dolphinscheduler 1.2.1 数据迁移到 dolphinscheduler 2.0.5方法及迁移后数据测试记录

百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划

【图像融合】基于非凸罚分的稀疏正则化实现图像融合附matlab代码

微信小程序:凑单满减计算神器

微信小程序:爱情保证书制作生成

prometheus、influxdb2.2安装及flume_export下载编译使用

go学习记录二(Window)
随机推荐
dolphinscheduler海豚调度升级代码改造-UpgradeDolphinScheduler
Beyond chips and AI, why is hard technology capital becoming more and more "hard core"?
微信小程序;AI智能配音助手
【图像融合】基于非凸罚分的稀疏正则化实现图像融合附matlab代码
JSP入门级笔记
Official download and installation of QT and QT vs tools plug-ins
[MAC] there is no source option in security and privacy
HCIP 重发布实验
入行软件测试5年,跳槽3次,我摸透了软件测试这一行
Introduction to s file generated by TEQC for GNSS data quality analysis
Direct insertion sort - [common sort method (1/8)]
Getting started with the shutter AppBar
笔者认为所谓的产业互联网,就是一个产业与互联网深度融合的过程
dolphinscheduler 2.0.5 spark 任务测试总结(源码优化)
Raspberry pie network remote access
Less than a year after development, I dared to ask for 20k in the interview, but I didn't even want to give 8K after the interview~
395. redundant path
SwiftUI 2.0 课程笔记 Chapter 4
The technological advance of new employees? Journey
Three methods of GNSS velocity calculation