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 <= 10000
0 <= A[i] <= 10^9
0 <= 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分钟 ...
随机推荐
- C#获取类里面的所有的方法名称
Type trypInfo = typeof(Program);// Program为类名//获得方法名string str = "包括的方法名列表:\r\n"; MethodIn ...
- Android判断Service是否运行
/** * 用来判断服务是否运行. * @param context * @param className 判断的服务名字 * @ret ...
- 使用OpenSsl自己CA根证书,二级根证书和颁发证书(亲测步骤)
---恢复内容开始--- 一.介绍 企业自用, 到证书机构签发证书的费用和时间等都可以省下..... SSl证书的背景功用.......(省略万字,不废话) 可以参考: SSL证书_百度百科 X509 ...
- Using Mono DLLs in a Unity Project
[Using Mono DLLs in a Unity Project] The path to the Unity DLLs will typically be: 一个生成dll的例子如下: mcs ...
- Hadoop2.2.0安装配置手册
第一部分 Hadoop 2.2 下载 Hadoop我们从Apache官方网站直接下载最新版本Hadoop2.2.官方目前是提供了linux32位系统可执行文件,所以如果需要在64位系统上部署则需要单独 ...
- 原生JS的使用,包括jquery和原生JS获取节点、jquery和原生JS修改属性的比较
一.前言 相比于JS这条直达终点.满是荆棘的小路,jquery无疑是康庄大道了,足够的简洁.易用给了它辉煌的地位.然而,毕竟是绕着道的插件,当小路走着走着变成大路的时候,曾经的大路也就失去了他自身的优 ...
- 75-扩展GCD-时间复杂度
扩展gcd-时间复杂性 题目内容: 计算循环语句的执行频次 for(i=A; i!=B ; i+=C) x+=1; 其中A,B,C,i都是k位无符号整数. 输入描述 A B C k, 其中0<k ...
- Red Hat 6.5 本地yum源的配置
在没有网络的情况下,想要使用yum源进行软件的安装就显得非常困难了.所以有时候配置本地的yum源也是非常必要的. 准备工作: rad hat 的ISO镜像文件. 1.创建一个文件夹,用于挂载ISO镜像 ...
- wsl命令行
参考: https://docs.microsoft.com/en-us/windows/wsl/about 查看已安装 wslconfig /l /all 重装 wslconfig /u debia ...
- 一些命令可以帮您了解Linux 操作系统用户信息
1 显示上次登录的用户信息列表,包括(登录时间.退出时间.登录IP): [sywu@wusuyuan ~]$ last root pts/1 192.168.1.3 Wed Aug 27 22:08 ...