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. LOJ#3104「TJOI2019」甲苯先生的字符串

    题目描述 一天小甲苯得到了一条神的指示,他要把神的指示写下来,但是又不能泄露天机,所以他要用一种方法把神的指示记下来. 神的指示是一个字符串,记为字符串 \(s_1\),\(s_1\) 仅包含小写字母 ...

  2. 项目中使用express,只是单纯项目中使用

    安装express npm install express --save-dv 建议安装到dev依赖里面 安装body-parse npm install body-parser --save-dev ...

  3. LG2664 树上游戏

    树上游戏 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 $$sum_i=\sum_{j=1}^ns(i,j)$$ 现在他想让 ...

  4. Vue中的插槽---slot

    一:什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定将所携带的内容,插入到指定的某个位置,从而使模板分块,具有模块化的特质和更大的重用性. 插槽显不显示.怎样显示是由 ...

  5. 求序列A中每个数的左边比它小的数的个数(树状数组)

    给定一个有N个正整数的序列A(N<=10^5,A[i]<=10^5),对序列中的每一个数,求出序列中它左边比它小的数的个数. 思路:树状数组的经典应用(裸题) #include <i ...

  6. javascript之大文件分段上传、断点续传

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  7. asp.net+ tinymce粘贴word

    公司做的项目需要用到粘贴Word功能.就是将word内容一键粘贴到网页编辑器(在线富文本编辑器)中.Chrome+IE默认支持粘贴剪切板中的图片,但是我要粘贴的文章存在word里面,图片多达数十张,我 ...

  8. 边框图片border-image

    一.定义: 在内容变化的容器里使用,边框自动填充,由于浏览器的兼容问题,没有广泛使用 border-image属性是速记属性用于设置 border-image-source, border-image ...

  9. nohup 后台执行

    nohup  默认是当前用户执行的,当当前用户退出会导致执行进程异常. 所以正确的 nohup 是指定 /bin/bash 进行执行. nohup /bin/bash/ /opt/script/s.s ...

  10. 洛谷P1288取数游戏2

    题目 博弈论. 考虑先手和后手的关系.然后可以通过统计数值不是0的数的个数来得出答案. \(Code\) #include <bits/stdc++.h> using namespace ...