LeetCode-Queue Reconstruction by Height
来源:互联网 发布:java中多态的理解 编辑:程序博客网 时间:2024/05/18 03:11
1. Queue Reconstruction by Height (Medium)
Description
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.
Note:
The number of people is less than 1,100.
Example
Input:[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]Output:[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
Analysis
首先将people
按照第一个数字的降序,第二个数字的升序排序。例如题目给定的 [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
排序之后是 [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]
然后依次将排序后的pair
按照第二个数字作为index
插入result
中。这样每一步插入都是满足题意要求的,最后返回结果。
代码:
class Solution {public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { vector<pair<int, int>> result; sort(people.begin(), people.end(), [](pair<int, int> a, pair<int, int> b){ return a.first > b.first || (a.first == b.first && a.second < b.second); }); for (auto p: people) { result.insert(result.begin() + p.second, p); } return result; }};
阅读全文
0 0
- Leetcode Queue Reconstruction by Height
- leetcode---Queue Reconstruction by Height
- LeetCode-Queue Reconstruction by Height
- LeetCode—406. Queue Reconstruction by Height
- LeetCode[406] Queue Reconstruction by Height
- [LeetCode]406.Queue Reconstruction by Height
- LeetCode 406. Queue Reconstruction by Height
- LeetCode #406: Queue Reconstruction by Height
- leetcode——Queue Reconstruction by Height
- [LeetCode] 406. Queue Reconstruction by Height
- Leetcode 406. Queue Reconstruction by Height
- leetcode 406 Queue Reconstruction by Height C++
- LeetCode 406. Queue Reconstruction by Height
- Leetcode 406 Queue Reconstruction by Height
- LeetCode笔记:406. Queue Reconstruction by Height
- 【LeetCode】 406. Queue Reconstruction by Height
- [leetcode]406. Queue Reconstruction by Height
- [Leetcode] 406. Queue Reconstruction by Height
- EF的查询与性能优化
- MySQL开启慢查询
- java关键字final、static使用总结
- hbase系列-Hbase热点问题、数据倾斜和rowkey的散列设计
- Error Domain=com.alamofire.error.serialization.response Code=-1016
- LeetCode-Queue Reconstruction by Height
- js 闭包
- View 事件体系(三)
- Java反射(思维导图)
- C++中的inline函数
- LeetCode 37. Sudoku Solver
- poj3278--Catch That Cow(BFS+裁剪记录)
- elasticsearch6.0 中文分词
- Java 定时任务的实现