leetcode 406. Queue Reconstruction by Height (贪心)
来源:互联网 发布:新业汽修软件注册码 编辑:程序博客网 时间:2024/05/21 09:14
题意:
给你一组数据(n,k),n代表价值,k代表当前数据 之前有k组价值大于等于n的数据,让你排序,使得这组数据合理。
思路:
关键在于维护价值大于等于n的组数,所以应该先确当价值大的数据的位置。 只要确定了 当前数据 与 价值大于等于n的数据 的位置关系,后面价值小的数据不管插在哪都与当前数据无关了。
代码:
bool cmp(pair<int,int> a,pair<int,int> b){ if(a.first==b.first) return a.second<b.second; return a.first>b.first;}class Solution {public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { int n = people.size(); vector<pair<int,int> > ans(n); if(n==0) return ans; sort(people.begin(),people.end(),cmp); for(int i = 0;i<n;i++) { int pos = people[i].second; for(int j = i;j>pos;j--) { ans[j] = ans[j-1]; } ans[pos] = people[i]; } return ans; }};
阅读全文
1 0
- leetcode 406. Queue Reconstruction by Height (贪心)
- #python练习#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
- 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
- Leetcode 406. Queue Reconstruction by Height
- [LeetCode]406. Queue Reconstruction by Height
- Leetcode-406. Queue Reconstruction by Height
- LeetCode OJ 406. Queue Reconstruction by Height
- 【LeetCode】406. Queue Reconstruction by Height
- arcgis android开发之添加地图控件并缩放至当前位置
- Linux vmstat命令实战详解
- Android--------------百度地图
- Eclipse编辑环境设置
- Java过滤器流之BufferedInputStream与BufferedOutputStream
- leetcode 406. Queue Reconstruction by Height (贪心)
- 爬虫实践---今日头条<街拍>图片
- MapReduce编程模型概述
- Android Studio编译报错“java.lang.OutOfMemoryError: GC overhead limit exceeded”
- Block循环引用的例子
- xml cdata的作用
- 分类与预测
- MFC编程中遇到的问题记录
- Google Chrome浏览器点击 input file上传按钮时延迟3-5秒