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. Kubernetes之Controllers三

    StatefulSets StatefulSet is the workload API object used to manage stateful applications. Note: Stat ...

  2. Git误操作 git reset强制回滚 恢复commit方法

    参考: 找回Git中丢失的Commit Git误操作 git reset强制回滚 恢复commit方法 使用Git时,常有误操作,在Commit之后又执行了git reset --hard HEAD强 ...

  3. CART决策树

     CART(Classification and Regression tree)分类回归树由L.Breiman,J.Friedman,R.Olshen和C.Stone于1984年提出.ID3中根据属 ...

  4. <aop:aspect>与<aop:advisor>的区别

    在开发过程中,不少有Spring Aop的使用,在面向切面编程时,我们会使用< aop:aspect>:在进行事务管理时,我们会使用< aop:advisor>.那么,对于&l ...

  5. C# 获取程序运行目录

    string a = "BaseDirectory:" + AppDomain.CurrentDomain.BaseDirectory + "\r\n" + & ...

  6. L2-019. 悄悄关注

    说真的,看了一些L2题明白多学点库函数是多么重要!!!! 会map这题直接水过,map用法不解释自己学,代码如下: #include<bits/stdc++.h> using namesp ...

  7. django使用MySQL数据库

    在实际生产环境,Django是不可能使用SQLite这种轻量级的基于文件的数据库作为生产数据库.一般较多的会选择MySQL. 下面介绍一下如何在Django中使用MySQL数据库. 一.安装MySQL ...

  8. Excel 常用设置

    首行固定 视图->冻结窗口

  9. 写给前端的Python依赖管理指北

    概述 在Python的项目中,我们可以通过pip来安装依赖包,但是不像npm install,pip默认安装的依赖包会挂在全局上,不利于项目工程协作. 这时候需要一款类似npm的工具记录我们的项目依赖 ...

  10. 《剑指offer》第六十七题(把字符串转换成整数)

    // 面试题67:把字符串转换成整数 // 题目:请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不 // 能使用atoi或者其他类似的库函数. #include <ios ...