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的更多相关文章

  1. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  2. [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 ...

  3. 【leetcode】452. Minimum Number of Arrows to Burst Balloons

    题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...

  4. [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 ...

  5. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. [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 ...

  7. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  8. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] 316. Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

随机推荐

  1. Java精通并发-wait与notify及线程同步系统总结

    notifyAll(): 在上两次中对于Object的wait()和notify()方法的官方doc进行了通读,上一次https://www.cnblogs.com/webor2006/p/11407 ...

  2. linux系统编程之管道(二)

    今天继续研究管道,话不多说,言归正传: 对于管道,有一定的读写规则,所以这里主要是对它的规则进行探讨,具体规则如下: 规则一: 下面用程序来验证下,还是用上节学的子进程写数据,父进程读取数据的例子,只 ...

  3. IOT设备通讯,MQTT物联网协议,MQTTnet

    一.IOT设备的特性 硬件能力差(存储能力基本只有几MB,CPU频率低连使用HTTP请求都很奢侈) 系统千差万别(Brillo,mbedOS,RIOT等) 如使用电池供电,电量消耗敏感 如果是小设备, ...

  4. Exec Maven插件

    1.为什么使用exec? 现在的工程往往依赖 众多的jar包,不像war包工程,对于那些打包成jar包形式的本地java应用来说,通过java命令启动将会是一件极为繁琐的事情,原因很简单,太 多的依赖 ...

  5. GITHUB添加SSH内容

    首先,你需要注册一个 github账号,最好取一个有意义的名字,比如姓名全拼,昵称全拼,如果被占用,可以加上有意义的数字. 本文中假设用户名为 chuaaqiCSDN(我的博客名的全拼) 一.gihu ...

  6. mysql跨表删除多条记录

    Mysql可以在一个sql语句中同时删除多表记录,也可以根据多个表之间的关系来删除某一个表中的记录. 假定我们有两张表:Product表和ProductPrice表.前者存在Product的基本信息, ...

  7. Kafka、ActiveMQ、RabbitMQ、RocketMQ区别

    1.区别: Kafka和RocketMQ的区别: 1.两者对于消息的单机吞吐量.时效性.可用性.消息可靠性都差不多,其中时效性就是消息延迟都在ms级,kafka吞吐量会更大. 2.功能支持方面:Kaf ...

  8. web 字体 font-family

    body { font-family: -apple-system, //针对 Web 页面 BlinkMacSystemFont, //针对 Mac Chrome 页面 SFProDisplay, ...

  9. 1.7volatile关键字

    volatile volatile关键字的主要作用是使变量在多个线程间可见 使用方法: private volatile int number=0; 图示: 两个线程t1和t2共享一份数据,int a ...

  10. asp.net使用WebUploader做大文件的分块和断点续传

    HTML部分 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.a ...