870. Advantage Shuffle
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indices i for which A[i] > B[i].
Return any permutation of A that maximizes its advantage with respect to B.
Example 1:
Input: A = [2,7,11,15], B = [1,10,4,11]
Output: [2,11,7,15]
Example 2:
Input: A = [12,24,8,32], B = [13,25,32,11]
Output: [24,32,8,12]
Note:
1 <= A.length = B.length <= 100000 <= A[i] <= 10^90 <= B[i] <= 10^9
Approach #1: C++.
class Solution {
public:
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
map<int, int> m;
for (int i : A) m[i]++;
map<int, int>::iterator it;
vector<int> res;
for (int i : B) {
it = m.upper_bound(i);
int x = it != m.end() ? it->first : m.begin()->first;
if (--m[x] == 0) m.erase(x);
res.push_back(x);
}
return res;
}
};
870. Advantage Shuffle的更多相关文章
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【leetcode】870. Advantage Shuffle
题目如下: 解题思路:几千年前,一个古人曾经解过这个题目,他的名字叫做田忌,后人称他的解题思想叫做“田忌赛马”.言归正传,本题就是一个田忌赛马的问题,先将A与B进行排序,然后判断A[0]与B[0]的大 ...
- [LeetCode] Advantage Shuffle 优势洗牌
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...
- [Swift]LeetCode870. 优势洗牌 | Advantage Shuffle
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
随机推荐
- Spark之 Spark Streaming整合kafka(并演示reduceByKeyAndWindow、updateStateByKey算子使用)
Kafka0.8版本基于receiver接受器去接受kafka topic中的数据(并演示reduceByKeyAndWindow的使用) 依赖 <dependency> <grou ...
- Elasticsearch前沿:ES 5.x改进详解与ES6展望
转:http://www.dataguru.cn/article-11094-1.html 曾勇(Medcl),Elastic 工程师与布道师,2015 年加入 Elastic 公司.加入 Elast ...
- 对加密的了解(DES/3DES/AES区别 )
DES 1977年1月,美国政府颁布:采纳IBM公司设计的方案作为非机密数据的正式. 目前在国内,随着三金工程尤其是金卡工程的启动,DES算法在POS.ATM.磁卡及智能卡(IC卡).加油站.高速公路 ...
- iOS调试程序的方法
IOS各种调试技巧豪华套餐 普通操作 如图3 基本的断点操作如下 图4 点击那个黑列列就创建了一个断点,再次点击就临时取消这个断点(但是不删除),长按那个断点拖出去就删除了(mac os的系统工程师就 ...
- SpringBoot与Shiro的整合(单体式项目)
1.包结构 2.jar包,配置文件,类 2.1pom.xml文件配置 <?xml version="1.0" encoding="UTF-8"?> ...
- c# 类中使用ResolveUrl
类中使用ResolveUrl 1>获取当前page然后调用ResolveUrl System.Web.UI.Page page = HttpContext.Current.CurrentHand ...
- PropertyPlaceholderConfigurer使用及@Value使用注意事项
思考 PropertyPlaceholderConfigurer和<context:property-placeholder/>有何区别? @Value在Controller层和Servi ...
- UEFI下win10+Ubuntu双启动后完全纯净卸载Ubuntu,重建BCD
以下内容操作具有风险,操作前请提前备份数据.建议由有丰富经验的人使用,需要掌握diskpart. 背景 使用ubuntu+win10 dual boot后,需要重置回纯净win10系统. BCD是Bo ...
- springmvc乱码配置
web.xml配置 <!-- springmvc乱码 --> <filter> <filter-name>encodingFilter</filter-nam ...
- lnmp一键安装包 配置多站点
在/usr/local/nginx/conf/vhost下配置多站点的文件,一个站点对应一个文件,配置如下信息: vim ./vhost/test.conf server { listen ; ser ...