当前位置:网站首页>[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;
}
}
边栏推荐
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- P1339 [USACO09OCT]Heat Wave G
- [Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
- Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
- Generic tips
- 2022.02.13
- Cgb2201 preparatory class evening self-study and lecture content
- How to prevent malicious crawling of information by one-to-one live broadcast source server
- D24:divisor and multiple (divisor and multiple, translation + solution)
- 炒股開戶傭金優惠怎麼才能獲得,網上開戶安全嗎
猜你喜欢

Gorilla/mux framework (RK boot): add tracing Middleware

Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place

Current detection circuit - including op amp current scheme

leetcode-43. String multiplication

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Alibaba cloud container service differentiation SLO hybrid technology practice

Iclr2022: how does AI recognize "things I haven't seen"?

Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation

Common mode interference of EMC
![[note] IPC traditional interprocess communication and binder interprocess communication principle](/img/f6/36c28df02198539e27352e3cdf4ba6.jpg)
[note] IPC traditional interprocess communication and binder interprocess communication principle
随机推荐
SQL data update
P1656 bombing Railway
I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
[MySQL] sql99 syntax to realize multi table query
"Learning notes" recursive & recursive
股票開戶傭金最低的券商有哪些大家推薦一下,手機上開戶安全嗎
Learning methods of zynq
Ramble 72 of redis source code
How to quickly build high availability of service discovery
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
Idea set class header comments
2020.2.14
Investment demand and income forecast report of China's building ceramics industry, 2022-2028
想请教一下,十大劵商如何开户?在线开户是安全么?
What are the securities companies with the lowest Commission for stock account opening? Would you recommend it? Is it safe to open an account on your mobile phone
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
Maxwell equation and Euler formula - link
How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii