LeetCode 中级 - 优势洗牌(870)
给定两个大小相等的数组 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)的更多相关文章
- Leetcode 870. 优势洗牌
870. 优势洗牌 显示英文描述 我的提交返回竞赛 用户通过次数49 用户尝试次数92 通过次数49 提交次数192 题目难度Medium 给定两个大小相等的数组 A 和 B,A 相对于 B 的 ...
- LeetCode 870.优势洗牌(C++)
给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述. 返回 A 的任意排列,使其相对于 B 的优势最大化. 示例 1: 输入: ...
- Leetcode(870)-优势洗牌
给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述. 返回 A 的任意排列,使其相对于 B 的优势最大化. 示例 1: 输入: ...
- [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 ...
- [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 ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 洗牌算法Fisher-Yates以及C语言随机数的产生
前些天在蘑菇街的面试中碰到一道洗牌的算法题,拿出来和大家分享一下! 原题是:54张有序的牌,如何无序的发给3个人? 这个题是运用经典的洗牌算法完成.首先介绍一种经典的洗牌算法--Fisher-Yate ...
- 洗牌算法及 random 中 shuffle 方法和 sample 方法浅析
对于算法书买了一本又一本却没一本读完超过 10%,Leetcode 刷题从来没坚持超过 3 天的我来说,算法能力真的是渣渣.但是,今天决定写一篇跟算法有关的文章.起因是读了吴师兄的文章<扫雷与算 ...
随机推荐
- 应该是很简单的事,关于SQL2005的视图浏览不排序问题
http://bbs.csdn.net/topics/390667337?page=1
- switch case 忘记 break
昨天纠结了很久的问题,一直找不到原因,早上发现是一个低级错误: private void selectButton(int id) { switch (id) { case 0: recommend_ ...
- spring-springmvc code-based
idea设置maven在下载依赖的同时把对应的源码下载过来.图0: 1 主要实现零配置来完成springMVC环境搭建,当然现在有了springBoot也是零配置,但是很多同仁都是从spring3.x ...
- Dapper 批量操作sql
static void Main(string[] args) { private static readonly string sqlconnection =""; //遍历循环 ...
- SQL点点滴滴_非聚集索引设计指南-转载
非聚集索引包含索引键值和指向表数据存储位置的行定位器. 有关非聚集索引体系结构的详细信息, 请参阅 非聚集索引结构. 可以对表或索引视图创建多个非聚集索引. 通常, 设计非聚集索引是为改善经常使用的没 ...
- 【Leetcode】【Medium】3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 配置docker容器上ssh无密登录
配置docker容器上ssh无密登录 1.修改所有容器中root账户密码 ssh到远程主机时,首次需要密码访问,因此需要修改root账号密码. 密码必须要8位以上字母数字混合. $>passwd ...
- python数据结构(整理)
http://www.cnblogs.com/yupeng/p/3413763.html 1. 单链表 链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点 ...
- 使用redux开发的简单步骤
一.安装redux包 npm install redux --save 二.根据APP数据结构或者后台请求的数据结构拟定state的大致结构. 可以把state写成一个对象字面量,放在reducer文 ...
- bzoj2331 [SCOI2011]地板
Description lxhgww的小名叫“小L”,这是因为他总是很喜欢L型的东西.小L家的客厅是一个的矩形,现在他想用L型的地板来铺满整个客厅,客厅里有些位置有柱子,不能铺地板.现在小L想知道,用 ...