给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述。

返回 A 的任意排列,使其相对于 B 的优势最大化。

示例 1:

输入:A = [2,7,11,15], B = [1,10,4,11]
输出:[2,11,7,15]

示例 2:

输入:A = [12,24,8,32], B = [13,25,32,11]
输出:[24,32,8,12]

提示:

  1. 1 <= A.length = B.length <= 10000
  2. 0 <= A[i] <= 10^9
  3. 0 <= B[i] <= 10^9

思路:A排序之后,给B每个元素分配最小大于的元素,再删除A中该元素。若A中元素都比B中元素小,则分配A[0]

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; vector<int> advantageCount(vector<int>& A, vector<int>& B) {
vector<int> temp;
sort(A.begin(), A.end());
for (size_t i = ; i < B.size(); ++i) {
size_t j = ;
bool flag = false;
while (j < A.size()) {
if (A[j] > B[i]) {
temp.push_back(A[j]);
A.erase(A.begin() + j);
flag = true;
break;
}
++j;
}
if (!flag) {
temp.push_back(A[]);
A.erase(A.begin());
}
}
return temp;
} int main()
{
vector<int> A = { ,,, }, B = { ,,, }, temp;
temp = advantageCount(A, B);
for (auto& i : temp) {
cout << i << " ";
} system("PAUSE");
return ;
}

LeetCode 870.优势洗牌(C++)的更多相关文章

  1. Leetcode 870. 优势洗牌

    870. 优势洗牌  显示英文描述 我的提交返回竞赛   用户通过次数49 用户尝试次数92 通过次数49 提交次数192 题目难度Medium 给定两个大小相等的数组 A 和 B,A 相对于 B 的 ...

  2. LeetCode 中级 - 优势洗牌(870)

    给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述. 返回 A 的任意排列,使其相对于 B 的优势最大化. 示例 2: 输入: ...

  3. Leetcode(870)-优势洗牌

    给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述. 返回 A 的任意排列,使其相对于 B 的优势最大化. 示例 1: 输入: ...

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

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

  6. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  7. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  8. 洗牌算法Fisher-Yates以及C语言随机数的产生

    前些天在蘑菇街的面试中碰到一道洗牌的算法题,拿出来和大家分享一下! 原题是:54张有序的牌,如何无序的发给3个人? 这个题是运用经典的洗牌算法完成.首先介绍一种经典的洗牌算法--Fisher-Yate ...

  9. 洗牌算法及 random 中 shuffle 方法和 sample 方法浅析

    对于算法书买了一本又一本却没一本读完超过 10%,Leetcode 刷题从来没坚持超过 3 天的我来说,算法能力真的是渣渣.但是,今天决定写一篇跟算法有关的文章.起因是读了吴师兄的文章<扫雷与算 ...

随机推荐

  1. C# Socket 接受数据不全的处理

    由于Socket 一次传输数据有限,因此需要多次接受数据传输. 解决办法一:     int numberOfBytesRead = 0;     int totalNumberOfBytes = 0 ...

  2. 控制某个panel的display样式

    "我想在onload方法里把panel的 style 里的 display 属性变成 none.我的页面由于有一些脚本,触发某些事件之后还想显示这个panel,不想用Panel3.Visib ...

  3. windows下启动Apache报443错误!

    windows下启动apache报make_sock: could not bind to address [::]:443错误! 查看指定端口的占用情况 netstat -aon|findstr & ...

  4. 如何在Linux上使用x2go设置远程桌面

    Until ACS supports Spice, if ever,you're better off with "on-VM" softare such RDP for Wind ...

  5. binder学习笔记(十)—— 穿越到驱动层

    Binder驱动层的代码在kernel/goldfish/drivers/staging/android下的binder.c和binder.h.Android源码是不带Linux内核的,驱动正是在这个 ...

  6. 【bzoj2818】: Gcd 数论-欧拉函数

    [bzoj2818]: Gcd 考虑素数p<=n gcd(xp,yp)=p 当 gcd(x,y)=1 xp,yp<=n满足条件 p对答案的贡献: 预处理前缀和就好了 /* http://w ...

  7. findControl 可以获取前台页面的控件

    findControl 可以获取前台页面的控件

  8. ASP SQLDATASOURCE

    原文链接:http://blog.csdn.net/dodream/article/details/4887076

  9. js中的DOM对象 和 jQuery对象 比较

    一,二者的区别 通过 jQuery 获取的元素是一个数组,数组中包含着原生JS中的DOM对象. 总结:jQuery 就是把 DOM 对象重新包装了一下,让其具有了 jQuery 方法. 二,二者的相互 ...

  10. 微信Dat文件解码

    最近在整理磁盘文件,因为经过一段时间的蹂躏后,磁盘实在是太多东西了,不整理一下,简直对不住我的SSD好嘛.偶然发现磁盘中某公司的文件夹占用空间简直不能再大,那可是我的C盘啊,合计才119GB的SSD空 ...