当前位置:网站首页>Interview question 17.08 Circus tower

Interview question 17.08 Circus tower

2022-06-11 07:02:00 Not coriander

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
class Solution {
    
public:
    int bestSeqAtIndex(vector<int>& height, vector<int>& weight) {
    
        int n=height.size();
        
        vector<pair<int,int> >p;
        for(int i=0;i<n;i++){
    
            p.push_back(pair<int,int>(height[i],weight[i]));
        }

        sort(p.begin(),p.end(),[](const pair<int,int>&a,const pair<int,int>&b){
    
            if(a.first==b.first){
    
                return a.second>b.second;
            }
            else{
    
                return a.first<b.first;
            }
        });

        vector<int>v;
        
        for(int i=0;i<n;i++){
    
            //cout<<p[i].first<<" "<<p[i].second<<endl;
            int len=v.size();
            if(len==0||p[i].second>v[len-1]){
    
                v.push_back(p[i].second);
            }

            int l,r;
            l=0;
            r=len-1;
            while(l<=r){
    
                int mid=(l+r)/2;
                if(v[mid]>=p[i].second){
    
                    r=mid-1;
                }
                else{
    
                    l=mid+1;
                }
            }
            if(l<len){
    
                v[l]=p[i].second;
            }
           // cout<<l<<" "<<len<<endl;
        }
       return v.size(); 


    }
};
原网站

版权声明
本文为[Not coriander]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020524018103.html