当前位置:网站首页>[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;
}
}
边栏推荐
- Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
- Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
- P1339 [USACO09OCT]Heat Wave G
- C # basic knowledge (3)
- Unity shader visualizer shader graph
- China standard gas market prospect investment and development feasibility study report 2022-2028
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- The difference between single power amplifier and dual power amplifier
- After the Lunar New Year and a half
- Fashion cloud interview questions series - JS high-frequency handwritten code questions
猜你喜欢
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
Correlation analysis summary
Schematic diagram of crystal oscillator clock and PCB Design Guide
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
Alibaba cloud container service differentiation SLO hybrid technology practice
2/14 (regular expression, sed streaming editor)
[note] IPC traditional interprocess communication and binder interprocess communication principle
How to prevent malicious crawling of information by one-to-one live broadcast source server
Pyqt5 sensitive word detection tool production, operator's Gospel
Design of logic level conversion in high speed circuit
随机推荐
FPGA tutorial and Allegro tutorial - link
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Similarities and differences of text similarity between Jaccard and cosine
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Solve the problem that the kaggle account registration does not display the verification code
What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
Gossip about redis source code 82
Fluent learning (5) GridView
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Op amp related - link
QT creator source code learning note 05, how does the menu bar realize plug-in?
How to make icons easily
How about opening an account at Hengtai securities? Is it safe?
D30:color tunnels (color tunnels, translation)
Selenium library 4.5.0 keyword explanation (III)
Double efficiency. Six easy-to-use pychar plug-ins are recommended
A method to solve Bert long text matching
"Learning notes" recursive & recursive
C # basic knowledge (1)
It is forbidden to splice SQL in code