当前位置:网站首页>[leetcode] longest increasing subsequence problem and its application
[leetcode] longest increasing subsequence problem and its application
2022-06-23 05:20:00 【Xiaozhu Xiaozhu will never admit defeat】
List of articles
The longest increasing subsequence problem and its application
300. The longest increasing subsequence
Please refer to 【Leetcode】 Calculate longest series ( Dynamic programming ) Chinese vs 300. The longest increasing subsequence Explanation . There are two main ways to solve problems : Dynamic programming 、 greedy + Two points search .
Interview questions 17.08. Circus tower
1. Title Description
leetcode link : Interview questions 17.08. Circus tower 
2. Thought analysis
The topic should be in 2 On dimensions ( Height + weight ) At the same time, keep strictly increasing .
Then we can arrange one of the dimensions in order , To ensure that it keeps increasing in one dimension ( This is not strictly incremental ); Then you can focus on another dimension .
Rank the height and weight first , In ascending order of height , Same height , Weight in descending order , Here you can use a two-dimensional array lambda Expression writing . You can refer to :Java Array 、ArrayList、HashMap Sort the summary .
Then we calculate the longest increasing subsequence . The height is in ascending order , Just judge your weight . To deal with the problem of weight is to deal with the problem of the longest increasing subsequence .
Refer to the solution of the longest increasing subsequence .
3. Reference code
Method 1 : Dynamic programming ( Overtime )
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]};
}
// Same height , Weight descending
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;
}
}
Method 2 : greedy + Two points search
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]};
}
// Same height , Weight descending
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]) {
// What I'm looking for is dp The first in the array is smaller than the current h The location of
left = mid + 1;
} else {
right = mid;
}
}
dp[left] = per[1];
if (res == left) res++;
}
return res;
}
}
Binary search can be adjusted directly API:
Find the specified elements in the ordered array by dichotomy , And returns the subscript of the element .
- If the element exists in the array , The subscript of the element in the array will be returned
- If the element does not exist in the array , Will return -( The insertion point + 1)
int res1 = Arrays.binarySearch(int[] arr, int key); // By default, the entire array is searched
int res2 = Arrays.binarySearch(int[] arr, int fromIndex, int toIndex, int key); // Search the array within the specified index
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]};
}
// Same height , Weight descending
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); // If not
dp[i] = per[1];
if (i == res) res++;
}
return res;
}
}
354. The envelope problem of Russian Dolls
1. Title Description
leetcode link :354. The envelope problem of Russian Dolls 
2. Thought analysis
Same as the previous question , Only the height and weight range is changed into the length and width of the envelope , The method is exactly the same .
Direct dynamic planning will time out , So you can still use binary search to optimize .
3. Reference code
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); // Initialize to 1, Represents the current envelope
int max = 0;
// Two points search
for (int[] env : envelopes) {
int left = 0, right = max;
while (left < right) {
int mid = (right - left) / 2 + left;
if (dp[mid] < env[1]) {
// What I'm looking for is dp The first in the array is smaller than the current h The location of
left = mid + 1;
} else {
right = mid;
}
}
dp[left] = env[1];
if (left == max) {
max++;
}
}
return max;
}
}
Interview questions 08.13. Stacking boxes
1. Title Description
leetcode link : Interview questions 08.13. Stacking boxes 
2. Thought analysis
3. Reference code
1691. The maximum height of the stacked box
1. Title Description
leetcode link :1691. The maximum height of the stacked box 

2. Thought analysis
3. Reference code
406. Rebuild the queue according to height
1. Title Description
leetcode link :406. Rebuild the queue according to height 
2. Thought analysis
3. Reference code
边栏推荐
- Open source ecology 𞓜 super practical open source license basic knowledge literacy post (Part 2)
- The propeller framework v2.3 releases the highly reusable operator library Phi! Restructure development paradigm to reduce cost and increase efficiency
- SwiftUI 2.0 课程笔记 Chapter 4
- Introduction to s file generated by TEQC for GNSS data quality analysis
- 关于信息泄露和防御
- I have been engaged in software testing for 5 years and have changed jobs for 3 times. I have understood the field of software testing
- 计算欧式距离和余弦相似度
- BGP summary of hcip operation
- When SBAS encounters rtklib
- Snippet Manager snippetslab
猜你喜欢

Snippet Manager snippetslab

The propeller framework v2.3 releases the highly reusable operator library Phi! Restructure development paradigm to reduce cost and increase efficiency

三层架构实验

A bug in rtklib2.4.3 B34 single point positioning

掌握 Shell,一篇就够了!

onnxoptimizer、onnxsim使用记录

IDEA 代码开发完毕后,提交代码,提交后发现分支不对,怎么撤回

UnityShader入门精要——Unity中的渲染优化技术(四)

Sift特征点提取

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.
随机推荐
UnityShader入门精要——Unity中的渲染优化技术(四)
IDEA 代码开发完毕后,提交代码,提交后发现分支不对,怎么撤回
Get bat command results in bat
小时候 觉得爸爸就是天 无所不能~
985 test engineer is hanged. Who is more important in terms of education and experience?
Mongodb sharding principle
HCIP 重发布实验
JDBC入门学习(一)之DML操作
Missing essential plugin
STP summary
Calculate Euclidean distance and cosine similarity
小时候 觉得爸爸就是天 无所不能~
Getting started with the shutter AppBar
OSPF shunt test
BGP实验
Strong push, quick start to software testing
STM32cube CMSIS_V2 freeRTOS Queue 队列使用
[laravel series 7.8] broadcasting system
What is the average annual salary of an outsourced tester who has worked for 5-8 years?
Zygote进程