870. 优势洗牌

 显示英文描述

 
  • 用户通过次数49
  • 用户尝试次数92
  • 通过次数49
  • 提交次数192
  • 题目难度Medium

给定两个大小相等的数组 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
class Solution {
public:
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
vector<pair<int,int>> mp;
vector<int> ans(A.size()); for(int i=;i < B.size();i++){
mp.push_back(make_pair(B[i],i)); // make_pair 类似map(key->value)
} sort(A.begin(),A.end());
sort(mp.begin(),mp.end()); // 按照key的大小排序 for(int i=;i < B.size();i++){
vector<int>::iterator it = upper_bound(A.begin(),A.end(),mp[i].first); //在A中找到B[i]上界,返回的是A的指针,所以用vector迭代器
if(it != A.end()){ // 找到了
ans[mp[i].second] = *it;
*it = -; //遍历过的A中数组都置为-1
}
else{ //超出A上界后,只要没有访问过的A[i]就随便插入到ans中
int pos = ;
for(pos=;pos<A.size();pos++){if(A[pos] != -)break;}
ans[mp[i].second] = A[pos];
A[pos] = -;
}
}
return ans;
}
};

_思路很好发现,但代码能力有限,看别人代码实现了

 vector<int>::iterator it = upper_bound(A.begin(),A.end(),mp[i].first); 重点

lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

Leetcode 870. 优势洗牌的更多相关文章

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

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

  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. mybatis配置文件resultMap标签的使用

    本文为博主原创,未经允许不得转载: resultMap标签是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与 数据库表中的字段进行关联映射. 注意:当实体类中的字段与数据库表中的 ...

  2. Latex: 减少图与文字之间的空白间隙

    参考: Remove space after figure and before text Latex: 减少图与文字之间的空白间隙 论文中图与文字之间的空白间隙过大,导致排版不大美观.解决方法是在\ ...

  3. multiple definition of `qMain(int, char**)'

    QT C++ 我上一分钟运行地好好的,下一分钟就无法通过编译了.查了半天发现在IDE自动生成的项目文件.pro中 main竟然包含了两遍.我对这表示很无语,我完全是通过IDE来操作,却产生一些我不易察 ...

  4. springmvc通过ajax异步请求返回json格式数据

    jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...

  5. Spring-json依赖

    <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jacks ...

  6. Spring依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. Spring boot 整合JSP开发步骤

    1. 新建Springboot项目,war <dependency> <groupId>org.springframework.boot</groupId> < ...

  8. java 四种线程池的异同

    四种线程池的区别仅仅在于executors让threadpoolexecutor的构造器的参数不同,即核心线程池数,最大线程池数等不同.但是其他的,例如终止线程池等都是一样的

  9. 堆排序 java实现

    import java.util.Arrays; /* * 思路: * 1.方法adjustDown:对于一个数组a[],针对第i个数进行向下(直到len-1)调整,使得该位置成为大顶堆 * 2.方法 ...

  10. hybrid cordova+vue开发APP(一) 环境搭建

    没有选择react-navite,而选择cordova+vue2.x,是因为react-navite有学习成本,并且cordova+vue2.x程序员 可以直接上手,性能上可以满足需求,成本低,开发速 ...