给定两个大小相等的数组 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#中控制线程池的执行顺序

    在使用线程池时,当用线程池执行多个任务时,由于执行的任务时间过长,会导制两个任务互相执行,如果两个任务具有一定的操作顺序,可能会导制不同的操作结果,这时,就要将线程池按顺序操作.下面先给一段代码,该代 ...

  2. EF中的MySql返回 DataTable公共类库

    public static class SqlHelper { /// <summary> /// EF SQL 语句返回 dataTable /// </summary> / ...

  3. forEach,for in,for of循环的用法

    一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...

  4. Linux配置国内的Yum源

    因为Linux默认的yum源是国外的源,所以会有卡顿,缓慢的情况.而国内的Yum源相对速度较快,现在也比较成熟,所以给Linux更换国内Yum源是一个很好的选择. 1.  备份(备份之前需要yum i ...

  5. P2156 [SDOI2009]细胞探索

    $ \color{#0066ff}{ 题目描述 }$ 生物课上,老师开始为同学们介绍细胞.为了加深同学们的印象,老师在一张N×M的矩阵中定义了一种细胞,矩阵中仅有井号"#"和点&q ...

  6. P2488 [SDOI2011]工作安排 费用流

    \(\color{#0066ff}{ 题目描述 }\) 你的任务是制定出一个产品的分配方案,使得订单条件被满足,并且所有员工的愤怒值之和最小.由于我们并不想使用Special Judge,也为了使选手 ...

  7. 完美解决:"library not found for - "

    分析原因,解决问题 在Xcode编译的时候,可能会遇到报这个错误"library not found for - ",这是为什么呢? 由于我们在项目中使用了一些第三方的库,如百度的静态库.当Xcode ...

  8. CF581D Three Logos 暴力

    Three companies decided to order a billboard with pictures of their logos. A billboard is a big squa ...

  9. [转载]np.where()使用说明

    转载自https://www.cnblogs.com/massquantity/p/8908859.html#4072620 numpy.where() 有两种用法: 1. np.where(cond ...

  10. js 遍历tree的一个例子(全遍历),更复杂的功能

    更复杂的功能 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...