leetcode 406- Queue Reconstruction by Height 贪心算法的应用
来源:互联网 发布:mysql的好处 编辑:程序博客网 时间:2024/04/30 15:19
406- Queue Reconstruction by Height
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]]
思路一:由题不难看出,将数组排序后,题目会简单很多。我们可以自定义一个排序准则,将按h递增排序,h相同则按k递减排序。此时我们可以利用贪心算法,寻找结构的贪心性质。我们从排序后的数组来看,利用一个指针遍历数组,假设指针之前的数组都是已经排序完成的,则剩下的第一个pair在未排序中h最小,
因此,我们pair的位置应该是第k个空位置,因为后续空位置都将由比当前pair 的h大的pair 填充,而非空位置都比当前pair的 h小,第k个空位置为安全地选择。 代码如下:
class Solution {public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { vector<pair<int, int>> res(people.size(),pair<int,int>(INT_MIN,INT_MIN)); auto cmp = [](const pair<int,int>&lhs,const pair<int,int>& rhs){ return (lhs.first < rhs.first)?1:(lhs.first == rhs.first)?(lhs.second > rhs.second):0; }; sort(people.begin(),people.end(),cmp); int size = people.size(); for(auto &p:people){ for(int i = 0,j = -1;i <size;++i){ if(res[i] == pair<int,int>(INT_MIN,INT_MIN))++j; if(j == p.second){res[i] = p; break;} } } return res; }
思路二:由题不难看出,将数组排序后,题目会简单很多。我们可以自定义一个排序准则,将按h递减排序,h相同则按k递增排序。排序完成后,遍历整个数组,由于元素按h递减排序,因此每个pair的h都比前面所有元素的h小,因此pair的序号便代表前面h比自己大的pair数目。如果序号大于k,则表示该元素位置不对,应该向前移动,插入到对的位置。由于前面所有pair的h都比当前pair的h 要大,因此应该将当前pair移动到第k个位置。由于前面所有pair的h都比当前pair的h 要大,移动pair不会对前面的pair产生影响,前面所有的pair的位置依然满足队列要求。根据这一思路,可以写出如下代码。
bool cmp(const pair<int,int>& lhs,const pair<int,int>& rhs){ return (lhs.first > rhs.first)?1:((lhs.first==rhs.first)?(lhs.second < rhs.second):0); }class Solution {public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { sort(people.begin(),people.end(),cmp); for(int i = 0;i <people.size();++i){ if(people[i].second <= i){ pair<int,int> tmp = people[i]; people.erase(people.begin() + i); people.insert(people.begin()+tmp.second,tmp); } } return people; }};
代码很简洁,但是运用了erase 和insert 函数,因此每次插入交换都需要O(n)的时间复杂度,算法整体需要
for(int i = 0;i <people.size();++i){ if(people[i].second < i){ pair<int,int> tmp = people[i]; int j; for(j = i - 1;j >= tmp.second;--j){ people[j + 1] = people[j]; } people[j + 1] = tmp; }
测试运行时间从206ms 提升为 59ms,虽然最终代码最坏情况下依然是
- 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 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 Queue Reconstruction by Height
- leetcode---Queue Reconstruction by Height
- LeetCode-Queue Reconstruction by Height
- #python练习#leetcode 406. Queue Reconstruction by Height【贪心】
- leetcode 406. Queue Reconstruction by Height (贪心)
- 【406】 Queue Reconstruction by Height
- LeetCode 406 Queue Reconstruction by Height (排序 思维)
- LeetCode#406 Queue Reconstruction by Height题解(C++版)
- LeetCode—406. Queue Reconstruction by Height
- MySQL_Shell_清空数据库表中的数据
- Web基础之Maven包冲突
- NTP服务及时间同步(CentOS6.x)
- android opengl es入门示例
- LeaveCriticalSection问题的解决方案
- leetcode 406- Queue Reconstruction by Height 贪心算法的应用
- ICPC 2016 DALIAN H To begin or not to begin
- 使用VisualStudio2013编译ffmpeg3.1全过程
- 欢迎使用CSDN-markdown编辑器
- 机器学习之决策树(Decision Tree)&随机森林(Random forest)
- Java NIO框架--Netty4的简单示例
- 知识补充:在本地和gitlab上创建新分支以及字符串strcpy和直接等于
- 分布式缓存--系列2 -- Redis Cluster
- ACM日志 字符串统计 (java)