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

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

示例 2:

输入:A = [12,24,8,32], B = [13,25,32,11]
输出:[24,32,8,12] 思路: 类似田忌赛马,先将A排序。
class Solution {
public:
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
vector<int> c;//存入符合要求的序列
sort(A.begin(),A.end());//先将A按递增排序
bool flag;
int index=;
while(!A.empty()&&index<B.size())
{
flag=false;
for(int i=;i<A.size();i++)
{
//找到第一个大于B[index]的位置
if(A[i]>B[index])
{
flag = true;
index++;
c.push_back(A[i]);//将其放入c中
A.erase(A.begin()+i);//移除该位置的值
break;//跳出循环
}
}
if(!flag)//若都比B[index]小,将A中最小的压入c
{
c.push_back(A[]);
A.erase(A.begin());
index++;
}
}
return c;
}
};

LeetCode 中级 - 优势洗牌(870)的更多相关文章

  1. Leetcode 870. 优势洗牌

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

  2. LeetCode 870.优势洗牌(C++)

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

  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. Python基础-进程和线程

    一.进程和线程的概念 首先,引出“多任务”的概念:多任务处理是指用户可以在同一时间内运行多个应用程序,每个应用程序被称作一个任务.Linux.windows就是支持多任务的操作系统,比起单任务系统它的 ...

  2. webgis开发-开始向JS转向

    官方的一个blog Final Release and Support Plan for the ArcGIS APIs / Viewers for Flex and Silverlight 网址: ...

  3. JavaScript中filter()方法

    方法概述 用于把数组(Array)的某些元素过滤掉,然后返回剩下的元素组成的数组. 语法: var filteredArray = array.filter(callback[, thisObject ...

  4. radio中最佳解决方案

    radio中最佳解决方案 1.html中 <td> <input id="status" name="status" type="r ...

  5. element ui下拉框如何实现默认选择?

    <template> <el-select v-model="value4" clearable placeholder="请选择"> ...

  6. SVNKit学习——使用低级别的API(ISVNEditor接口)直接操作Repository的目录和文件(五)

      本文是参考官方文档的实现,官方wiki:https://wiki.svnkit.com/Committing_To_A_Repository 本文核心使用的是ISVNEditor这个接口直接对Re ...

  7. 重新认识KCP

    什么是KCP KCP是一种网络传输协议(ARQ,自动重传请求),可以视它为TCP的代替品,但是它运行于用户空间,它不管底层的发送与接收,只是个纯算法实现可靠传输,它的特点是牺牲带宽来降低延迟.因为TC ...

  8. cmd:相关命令和笔记

    (1)查看git版本:git --version (2)

  9. June 20th 2017 Week 25th Tuesday

    Care and diligence bring luck. 谨慎和勤奋,带来好运气. In my opinion, care and diligence may just gurantee us a ...

  10. 关于数据库插入sql操作速度的影响

    大概看了以下,适当多线程数据库连接操作比单线程效率高 多个sql语句组合后调用数据库连接执行比单个sql循环执行效率高的多 下面是几个参考资料,有空的时候详细整理一下 https://blog.csd ...