贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle
思路:A数组的最大值大于B的最大值,就拿这个A跟B比较;如果不大于,就拿最小值跟B比较
A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺序,并且必须存储相应的index,因为最终需要将选择的A的数值存入与这个B相对应的index下
class Solution {
public:
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
vector<int> result(A.size());
sort(A.begin(),A.end());
priority_queue<pair<int,int>> q;
for(int i = ;i < B.size();i++)
q.push({B[i],i});
int left = ,right = A.size() - ;
while(left <= right){
int num = q.top().first;
int index = q.top().second;
q.pop();
if(A[right] > num){
result[index] = A[right];
right--;
}
else{
result[index] = A[left];
left++;
}
}
return result;
}
};
134. Gas Station
https://www.cnblogs.com/grandyang/p/4266812.html
当到达某一站点时,若这个值小于0了,则说明从起点到这个点中间的任何一个点都不能作为起点,则把起点设为下一个点,继续遍历
注意:这里int初始化的时候都要写=0,自己电脑上不初始化,初始化的结果为-1,oj上则是随机在初始化。至于这个的原因,我可能还要继续学习一下。
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int sum = ,cur_sum = ,index = ;
for(int i = ;i < gas.size();i++){
sum += gas[i] - cost[i];
cur_sum += gas[i] - cost[i];
if(cur_sum < ){
cur_sum = ;
index = i+;
}
}
return sum < ? - : index;
}
};
452. Minimum Number of Arrows to Burst Balloons
在x轴上,给你一串气球在轴上开始和结束的位置索引,从轴上某一点发射一根箭,能刺破气球。问最少需要多少根箭刺破气球。
或者说:这些气球沿X轴方向摆放,每个气球大小可能不同,一个气球占据的区间可以表示为[Xstart,Xend],气球可以重叠摆放。一支坐标为x的箭,可以扎破所有满足 Xstart <= x <= Xend 的气球,求出最少射几支箭可以将所有气球扎破。
这个题和区间相交有点像,按顺序将气球排列,如果有交集,同一根箭就能同时刺破两个气球。所以按照位置索引的开始位置进行排序,然后贪心地以结束位置为箭发射的位置,只要相交,就能刺破,但必须更新这个位置,有可能下一个气球的second比前一个小。如果不相交,就更新index,并增加箭的个数。
sort对pair的排序默认是以first的升序排列的,所以sort不用改tmp函数
注意:以第一个气球为出发,所以res为1;同时如果发射箭的位置等于了坐标,也应该能刺破,所以index >= points[i][0]
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
if(points.empty() || points[].empty())
return ;
int res = ;
sort(points.begin(),points.end());
int index = points[][];
for(int i = ;i < points.size();i++){
if(index >= points[i][])
index = min(index,points[i][]);
else{
index = points[i][];
res++;
}
}
return res;
}
};
316. Remove Duplicate Letters
https://www.cnblogs.com/grandyang/p/5085379.html
删除重复的,让最后的字符串没有重复的单词,并且要保证大小写的顺序和原本字符串中的相对位置。
class Solution {
public:
string removeDuplicateLetters(string s) {
vector<int> container(,);
vector<bool> visited(,false);
string res = "";
for(int i = ;i < s.size();i++)
container[s[i] - 'a']++;
for(int i = ;i < s.size();i++){
container[s[i] - 'a']--;
if(visited[s[i] - 'a'])
continue;
while(s[i] < res.back() && container[res.back() - 'a'] > ){
visited[res.back() - 'a'] = false;
res.pop_back();
}
res += s[i];
visited[s[i] - 'a'] = true;
}
return res.substr();
}
};
贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters的更多相关文章
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- [LeetCode] 452. Minimum Number of Arrows to Burst Balloons 最少箭数爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 【leetcode】452. Minimum Number of Arrows to Burst Balloons
题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...
- [LeetCode] 452 Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [Leetcode 452] 最少需要射出多少支箭Minimum Number of Arrows to Burst Balloons 贪心 重载
[题目] There are a number of spherical balloons spread in two-dimensional space. For each balloon, pro ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Leetcode: Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] 316. Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
随机推荐
- Rabbitmq异常排查
[RabbitMQ] beam.smp high cpu load https://blog.csdn.net/beer_do/article/details/52777445 Erlang 打开和关 ...
- DNS子域授权,区域传送
dig 命令 +recurse 递归查询 默认 +norecurse 不递归查询 dig +recurse -t A www.baidu.com @127.0.0.1 dig -t a ...
- istio-1.1.6镜像列表
istio-1.1.6镜像列表 istio-1.1.6/install/kubernetes/istio-demo.yaml文件里提取出来的镜像,方便作harbor部署. ============== ...
- P1983 车站分级[拓扑]
题目描述 一条单向的铁路线上,依次有编号为 1, 2, -, n1,2,-,n的 nn个火车站.每个火车站都有一个级别,最低为 11 级.现有若干趟车次在这条线路上行驶,每一趟都满足如下要求:如果这趟 ...
- Eclipse的tab键为4个空格完整方法 附:阿里代码开发规范书
开发规范书:http://pan.baidu.com/s/1dESdyox 1.点击 window->preference-,依次选择 General->Editors->Text ...
- NOI.ac模拟赛20181021 ball sequence color
T1 ball 可以发现每次推动球时,是将每个球的位置 −1-1−1 ,然后把最左边的球放到 P−1P-1P−1 处. 记个 −1-1−1 次数,再用set维护就好了. #include <bi ...
- 猴猴的比赛 dfs序
猴猴的比赛 dfs序 两颗\(n\)节点的树,不相同,问多少点对\((u,v)\)在两棵树上均满足路径\(v\)在\(u\)子树中 \(n\le 10^5\) 暴力: \(n^2\)暴力枚举点对用\( ...
- H - Almost Union-Find
//带删除操作的并查集 //题意:给你一个1~n的集合,有三种操作 // 1: 把p和q所在的集合合并 //2:把p移到q所在的集合中 //3:返回p所在集合中的元素个数和元素的和 //第二种操作不能 ...
- (26)打鸡儿教你Vue.js
weex框架的使用 1.weex开发入门 2.weex开发环境搭建 3.掌握部分weex组件模块 4.了解一些vue基本常见语法 5.制作一个接近原生应用体验的app weex介绍 安装开发环境 We ...
- Ceilometer和Gnocchi的监控架构解析
1 采集模块整体架构 采集模块主要分为三大块. Ceilometer:用于采集数据并处理数据后发送到gnocchi服务去存储 Gnocchi:用于将采集数据进行计算合并和存储并提供rest api方 ...