当前位置:网站首页>[leetcode] interview question 17.08 Circus tower
[leetcode] interview question 17.08 Circus tower
2022-07-03 23:50:00 【Chinese fir sauce_】
subject :
Interview questions 17.08. Circus tower
There's a circus that's designing a lot of shows , One should stand on the shoulder of another . For practical and aesthetic reasons , The person above is shorter and lighter than the person below . We know the height and weight of everyone in the Circus , Please write code to calculate how many people can be stacked at most .
Example :
Input :height = [65,70,56,75,60,68] weight = [100,150,90,190,95,110]
Output :6
explain : Count from top to bottom , At most, a man can fold 6 layer :(56,90), (60,95), (65,100), (68,110), (70,150), (75,190)
Tips :
height.length == weight.length <= 10000
Ideas :
In ascending order of height , Those of the same height are sorted in descending order of weight , It becomes Longest ascending subsequence (LIS) problem .
class Solution {
public int bestSeqAtIndex(int[] height, int[] weight) {
int len = height.length;
int[][]person = new int[len][2];
for(int i = 0; i < len;i++){
person[i] = new int[]{
height[i],weight[i]};
}
// Sort height in ascending order , If the height is the same , Weight in descending order
Arrays.sort(person,(a , b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int[] dp = new int[len]; //dp Storing elements that may form the longest subsequence
int res = 0; // Store the longest subsequence length
for(int[] pair : person){
// ( Dichotomy : Get the value of the longest substring )
// In subscript 0~res Search between pair[1] The subscript ( Two points search pair[1] In the sorted dp Index in array )
int i = Arrays.binarySearch(dp,0,res,pair[1]);
if(i < 0) {
// If the search is not successful , Return a negative number :- The insertion point -1) The insertion point Defined as the point where the key is inserted into the array : That is, the first element index greater than this key
i = -(i + 1); // Find the insertion point
}
dp[i] = pair[1]; // If i<res, The description replaces the original dp The greater of , Smaller values enter the array , It is convenient for latecomers to expand the actual number of elements of the array , Increase the sequence length
if(i == res) {
// If pair[1] Greater than dp All elements in , Extend the length of the longest subsequence
++res;
}
}
return res;
}
}
边栏推荐
- Social network analysis -social network analysis
- How to prevent malicious crawling of information by one-to-one live broadcast source server
- 网上的低佣金链接安全吗?招商证券怎么开户?
- ADB command to get XML
- 2022.02.13
- Investment demand and income forecast report of China's building ceramics industry, 2022-2028
- 2020.2.14
- Fluent learning (5) GridView
- How to make icons easily
- Distributed transaction -- middleware of TCC -- selection / comparison
猜你喜欢
How to make icons easily
Enter MySQL in docker container by command under Linux
A method to solve Bert long text matching
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
QT creator source code learning note 05, how does the menu bar realize plug-in?
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Docking Alipay process [pay in person, QR code Payment]
2/14 (regular expression, sed streaming editor)
Amway by head has this project management tool to improve productivity in a straight line
随机推荐
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
[note] IPC traditional interprocess communication and binder interprocess communication principle
AI Challenger 2018 text mining competition related solutions and code summary
Pyqt5 sensitive word detection tool production, operator's Gospel
在恒泰证券开户怎么样?安全吗?
P1656 bombing Railway
Advanced C language - pointer 2 - knowledge points sorting
2/14 (regular expression, sed streaming editor)
Distributed transaction -- middleware of TCC -- selection / comparison
URLEncoder. Encode and urldecoder Decode processing URL
Open 2022 efficient office, starting from project management
Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
I wrote a chat software with timeout connect function
Double efficiency. Six easy-to-use pychar plug-ins are recommended
Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
C # basic knowledge (1)
NLP Chinese corpus project: large scale Chinese natural language processing corpus
Current detection circuit - including op amp current scheme
Learning methods of zynq
D29:post Office (post office, translation)