贪心: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 ...
随机推荐
- HashMap简介以及hashCode写法的建议
映射表Map Map也叫映射表或者字典,Map中存储的元素是一个键值对key-value,他们共同包装在Entry<K,V>对象中.我们能通过key直接获取value,就像查字典一样. J ...
- Celebrate it, this is my first time on this blog.
After hovered around the technology edge many years(so ashamed), I want to collected all the points ...
- rocketmq那些事儿之入门基础
分布式消息队列中间件作为高并发系统的核心组件之一,能够帮助业务系统解构提升开发效率和系统稳定性,其复杂性可见一斑,作为核心组件,有必要去深入了解学习 前言 分布式消息队列中间件主要具有以下优势: 削峰 ...
- C# 4.0 新特性(.NET Framework 4.0 与 Visual Studio 2010 )
一.dynamic binding:动态绑定 在通过 dynamic 类型实现的操作中,该类型的作用是不在编译时类型检查,而是在运行时解析这些操作.dynamic 类型简化了对 COM API(例如 ...
- Vue中的插槽---slot
一:什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定将所携带的内容,插入到指定的某个位置,从而使模板分块,具有模块化的特质和更大的重用性. 插槽显不显示.怎样显示是由 ...
- service worker(一)之离线应用
serviceWork.html <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- LINQPad 应用
https://www.linqpad.net/ 使用 LINQPad 调试linq以及lambda表达式 http://www.studyofnet.com/news/1168.html linq ...
- 【EF】vs2017中没有EF模型
在添加->新建项目 中找不到实体模型? 或者 在vs中打开edmx文件时,显示的只有文本,没有图形模式 原因:是因为没有安装实体模型插件 解决方法: 1.打开网址 https://marketp ...
- saltstack 发布 iis 站点
Saltstack 发布 iis 站点 saltstack 主服务器配置:切换到 salt 的主目录下 : 主目录示例:/home/salt 程序集放置目录: web/web1 sls 目录: web ...
- 布鲁克斯法则 (Brooks's Law)
软件开发后期,添加人力只会使项目开发得更慢. 这个定律表明,在许多情况下,试图通过增加人力来加速延期项目的交付,将会使项目交付得更晚.布鲁克斯也明白,这是一种过度简化.但一般的推理是,新资源的增加时间 ...