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. cal of easter egg

    在Linux中用cal命令查看1752年9月,发现3到13日全都消失了……

  2. mount.cifs Windows共享目录权限755问题

    umount -l /usr/local/tomcat7/webapps/dsideal_yy/html/down mount -t cifs -o rw,dir_mode=,file_mode=,s ...

  3. jmeter多机联合负载

    操作步骤如下: 1.在负载机上部署Jmeter,确保Jmeter的bin目录下存在ApacheJMeter.jar与jmeter-server.bat两个文件. 2.双击启动负载机的jmeter-se ...

  4. 安装k8s,使用root帐号的初始化脚本

    现在稳定性差不多了.可以总结一下了. 真正使用时,有几个地方,还是确认一下,再正式运行吧. #!/bin/bash # Version V0. ---: ;fi K8S_VERSION="1 ...

  5. vetur 和 npm run lint 格式化不一致

    vetur 的 template(html) 默认使用的格式化插件是 prettyhtml,虽然可以选 prettier,和 npm run lint 的格式化保持一致,但是有时候会影响到 scrip ...

  6. 动态加载 ShellCode绕过杀软

    反病毒解决方案用于检测恶意文件,并且通常使用静态分析技术来区分二进制文件的好坏.如果是恶意文件本身包含恶意内容(ShellCode),那么依靠静态分析技术会非常有效,但如果攻击者使用轻量级的stage ...

  7. Ubuntu 下面手动安装 Redis

    1.下载 wget http://download.redis.io/releases/redis-2.8.17.tar.gz .tar.gz cd redis- make 2.复制文件到bin目录 ...

  8. Processing中和值域相关的函数

    今天在群里有人问了个问题:请教下啊,群里能有高手讲讲norm(), lerp(), map()么,英文的实在是没看懂呀?鉴于很多人初学Processing都没弄明白这3个函数的用法,我这里简单介绍一下 ...

  9. 查看windows操作系统的默认编码【转】

    在Windows平台下,进入DOS窗口,输入:chcp可以得到操作系统的代码页信息,你可以从控制面板的语言选项中查看代码页对应的详细的字符集信息. 例如: 我的活动代码页为:936,它对于的编码格式为 ...

  10. 利用Unicode RTLO方法构建恶意文件名

    一:简介 RTLO"字符全名为"RIGHT-TO-LEFTOVERRIDE",是一个不可显示的控制类字符,其本质是unicode 字符."RTLO"字 ...