当前位置:网站首页>[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;
}
}
边栏推荐
- Actual combat | use composite material 3 in application
- Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
- [Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
- Learning methods of zynq
- D25:sequence search (sequence search, translation + problem solving)
- Tencent interview: can you find the number of 1 in binary?
- 2.14 summary
- 2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- [MySQL] classification of multi table queries
猜你喜欢
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Tencent interview: can you pour water?
Zipper table in data warehouse (compressed storage)
It is forbidden to splice SQL in code
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
Schematic diagram of crystal oscillator clock and PCB Design Guide
Fluent learning (5) GridView
Selenium check box
"Learning notes" recursive & recursive
随机推荐
Selenium library 4.5.0 keyword explanation (4)
EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
Pandaoxi's video
Les sociétés de valeurs mobilières dont la Commission d'ouverture d'un compte d'actions est la plus faible ont ce que tout le monde recommande.
D30:color tunnels (color tunnels, translation)
Tencent interview: can you find the number of 1 in binary?
Fluent learning (5) GridView
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
P1656 bombing Railway
Gossip about redis source code 76
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
How to prevent malicious crawling of information by one-to-one live broadcast source server
Vscode regular match replace console log(.*)
2022 system integration project management engineer examination knowledge points: software development model
2022.02.14
Pytorch learning notes 5: model creation
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
Is user authentication really simple
Schematic diagram of crystal oscillator clock and PCB Design Guide
Pyqt5 sensitive word detection tool production, operator's Gospel