生活随笔
收集整理的這篇文章主要介紹了
根据身高重现队列
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目:
假設有打亂順序的一群人站成一個隊列。 每個人由一個整數(shù)對(h, k)表示,其中h是這個人的身高,k是排在這個人前面且身高大于或等于h的人數(shù)。 編寫一個算法來重建這個隊列。
注意:
總人數(shù)少于1100人。
示例
輸入:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
輸出:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
class Solution {
public:static bool cmp(const vector<int>& a, const vector<int>& b){return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]);}vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {sort(people.begin(), people.end(), cmp);//{{7,0},{7,1}, {6,1},{5,0},{5,2},{4,4}}//{7,0}//{7,0},{7,1}//{7,0},{6,1},{7,1}//{5,0},{7,0},{6,1},{7,1}//{5,0},{7,0},{5,2},{6,1},{7,1}//{5,0},{7,0},{5,2},{6,1},{4,4},{7,1}vector<vector<int>> res;for (auto a : people){cout << a[1] << endl;res.insert(res.begin() + a[1], a);}return res;}
};
貪心算法:優(yōu)先安排個子高的人
總結
以上是生活随笔為你收集整理的根据身高重现队列的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。