leetcode 406 根据身高重建列队

简介: leetcode 406 根据身高重建列队

根据身高重建列队


89bc5b4bab564b5197b9326ae1a23988.png

eb3002d8d65c40889096575b9475447e.png


整个插入过程如下:


排序完的people: [[7,0], [7,1], [6,1], [5,0], [5,2],[4,4]]


插入的过程:


插入[7,0]:[[7,0]]

插入[7,1]:[[7,0],[7,1]]

插入[6,1]:[[7,0],[6,1],[7,1]]

插入[5,0]:[[5,0],[7,0],[6,1],[7,1]]

插入[5,2]:[[5,0],[7,0],[5,2],[6,1],[7,1]]

插入[4,4]:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]


提高速度可以用链表,vector插入慢

class Solution {
public:
    static bool compare(vector<int> a, vector<int> b)
    {
        if(a[0] == b[0]) return a[1] < b[1];
        else return a[0] > b[0];
    }
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        vector<vector<int>> result;
        sort(people.begin() , people.end(),compare);
        for(int i=0 ; i < people.size() ;i++)
        {
            int pos = people[i][1];
            result.insert(result.begin()+pos , 1 , people[i]);
        }
        return result;
    }
};

二刷

vector

class Solution {
public:
    static bool cmp(vector<int> &a , vector<int> &b)
    {
        if(a[0] == b[0]) return a[1] < b[1];
        else return a[0] > b[0];
    }
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        vector<vector<int>> result;
        sort(people.begin(),people.end(),cmp);
        for(int i=0 ; i<people.size() ;i++)
        {
            result.insert(result.begin()+people[i][1] , people[i]);
        }
        return result;
    }
};

list

class Solution {
public:
    static bool cmp(vector<int> &a , vector<int> &b)
    {
        if(a[0] == b[0]) return a[1] < b[1];
        else return a[0] > b[0];
    }
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        list<vector<int>> my_list;
        sort(people.begin(),people.end(),cmp);
        for(int i=0 ; i<people.size() ;i++)
        {
            int point = people[i][1];
            auto it = my_list.begin();
            while(point--)
                it++;
            my_list.insert(it , people[i]);
        }
        vector<vector<int>> result(my_list.begin(),my_list.end());
        return result;
    }
};
相关文章
|
19天前
|
Go
golang力扣leetcode 406.根据身高重建队列
golang力扣leetcode 406.根据身高重建队列
49 0
|
19天前
代码随想录Day29 贪心04 LeetCode T860 柠檬水找零 T406 根据身高重建队列 T452 用最少得箭引爆气球
代码随想录Day29 贪心04 LeetCode T860 柠檬水找零 T406 根据身高重建队列 T452 用最少得箭引爆气球
26 0
|
7月前
|
算法 Java
代码随想录算法训练营第三十四天 | LeetCode 860. 柠檬水找零、406. 根据身高重建队列、452. 用最少数量的箭引爆气球
代码随想录算法训练营第三十四天 | LeetCode 860. 柠檬水找零、406. 根据身高重建队列、452. 用最少数量的箭引爆气球
42 0
LeetCode 406. 根据身高重建队列
假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。
66 0
代码随想录刷题|LeetCode 860.柠檬水找零 406.根据身高重建队列 452. 用最少数量的箭引爆气球
代码随想录刷题|LeetCode 860.柠檬水找零 406.根据身高重建队列 452. 用最少数量的箭引爆气球
代码随想录刷题|LeetCode 860.柠檬水找零 406.根据身高重建队列 452. 用最少数量的箭引爆气球
|
算法
LeetCode 406. 根据身高重建队列
LeetCode 406. 根据身高重建队列
29 0
LeetCode每日一题——剑指 Offer II 115. 重建序列
给定一个长度为 n 的整数数组 nums ,其中 nums 是范围为 [1,n] 的整数的排列。还提供了一个 2D 整数数组 sequences ,其中 sequences[i] 是 nums 的子序列。
66 0
LeetCode:406. 根据身高重建队列
LeetCode:406. 根据身高重建队列
68 0
LeetCode:406. 根据身高重建队列
|
Java C++
LeetCode(剑指 Offer)- 07. 重建二叉树
LeetCode(剑指 Offer)- 07. 重建二叉树
126 0
|
7天前
|
索引
【力扣刷题】两数求和、移动零、相交链表、反转链表
【力扣刷题】两数求和、移动零、相交链表、反转链表
15 2
【力扣刷题】两数求和、移动零、相交链表、反转链表