最長の増加シーケンス

インタビューの質問17.08。サーカスの人々はタワー
オリジナルタイトルリンクを
ピラミッド性能を設計されているサーカスがあり、人は別の肩の上に立つ必要があります。実用的かつ審美的な考慮事項については、少し以下の人よりも短くて軽い上の人インチ 一人一人の身長と体重サーカスを知られ、少数の人々にピラミッドスタックを計算するためのコードを記述してください。

例:

入力:高さ= [65,70,56,75,60,68]重量= [100,150,90,190,95,110]
出力:6
説明:数トップダウンから、6層まで積層ピラミッド(56,90)、 (60,95)、(65100)、(68110)、(70150)、(75190)

height.length == weight.length <= 10000

class Solution {
public:
    int bestSeqAtIndex(vector<int>& height, vector<int>& weight) {
        static auto speedup = [](){ios::sync_with_stdio(false);cin.tie(nullptr);return nullptr;}();
        vector<pair<int, int> > arr;
        vector<int> dp(height.size()+1, 0);
        for (int i = 0; i < height.size(); ++i){
            arr.push_back(make_pair(height[i], weight[i]));
        }
        sort(arr.begin(), arr.end(), [](pair<int,int> &a,pair<int,int> &b){
            if (a.first == b.first) return a.second > b.second;
            return a.first < b.first;
        });

        int res = 1;
        dp[1] = arr.front().second;
        for (int i = 1; i < arr.size(); ++i){
            if (arr[i].second > dp[res]){
                dp[++res] = arr[i].second;
            }
            else{
                auto pos = lower_bound(dp.begin()+1,dp.begin()+res+1,arr[i].second);
                *pos = arr[i].second;
            }
        }
        return res;
    }
};
公開された48元の記事 ウォン称賛13 ビュー20000 +

おすすめ

転載: blog.csdn.net/seanbill/article/details/104630249