【LeetCode】870. Advantage Shuffle 解题报告(Python)
【LeetCode】870. Advantage Shuffle 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/advantage-shuffle/description/
题目描述:
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indices i for which A[i] > B[i].
Return any permutation of A that maximizes its advantage with respect to B.
Example 1:
Input: A = [2,7,11,15], B = [1,10,4,11]
Output: [2,11,7,15]
Example 2:
Input: A = [12,24,8,32], B = [13,25,32,11]
Output: [24,32,8,12]
Note:
- 1 <= A.length = B.length <= 10000
- 0 <= A[i] <= 10^9
- 0 <= B[i] <= 10^9
题目大意
如何安排A的各个数字,使得对于每个位置A[i]>B[i]的情况最多。
解题方法
中国人小时候学过的一个故事,叫做田忌赛马,大家应该都知道的,用自己比对方强一点点的马去战胜对方的马,如果对方的马太强了,那么就用自己的最弱的马去参加比赛。这样做的好处就是我们用自己的弱鸡消耗掉对方的精锐,自己获胜的概率就最大。
使用了双向队列解决这个问题。同样是对A,B进行从小到大的排序,排序时需要保存B中的数字原来在数组中的哪个位置。这样我们对A进行一次遍历,每次出动自己的最弱的马,如果这个马能战胜B中最弱的马,那么就这么使用;如果战胜不了的话,那么就用这个最弱的马去和B中最强的马比赛,这样就干掉了对方的精锐。
时间复杂度是O(nlogn),空间复杂度是O(n).
代码如下:
class Solution(object):
def advantageCount(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
res = [-1] * len(A)
A = collections.deque(sorted(A))
B = collections.deque(sorted((b, i) for i, b in enumerate(B)))
for i in range(len(A)):
a = A.popleft()
b = B[0]
if a > b[0]:
B.popleft()
else:
b = B.pop()
res[b[1]] = a
return res
参考资料:
日期
2018 年 9 月 21 日 —— 转眼这个月又快过去了
【LeetCode】870. Advantage Shuffle 解题报告(Python)的更多相关文章
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- 变量、内存区域、MDK文件(map、htm)
变量分为:局部变量和全局变量 局部变量:函数体内部定义的变量,作用域为函数内部,static声明(静态局部变量)该变量则函数调用结束后不消失而保留值,分配的存储空间不释放. 全局变量:函数体外部定义的 ...
- LearnPython_week4
1.装饰器2.生成器3.迭代器4.内置方法5.可序列化6.项目规范化 1.装饰器 # -*- coding:utf-8 -*- # Author:Wong Du ### 原代码 def home(): ...
- A Child's History of England.13
Then came the boy-king, Edgar, called the Peaceful, fifteen years old. Dunstan, being still the real ...
- Leetcode中的SQL题目练习(一)
595. Big Countries https://leetcode.com/problems/big-countries/description/ Description name contine ...
- STL学习笔记1
STL六大部件 容器.分配器.算法.迭代器.适配器.仿函数 他们的关系如下
- C++ 写出这个数
题目如下: 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1. 输出格式: 在一行内 ...
- Linux学习 - 条件判断
一.判断格式 test -e /root/install.log 或 [ -e /root/install.log ] 使用echo $?查看是否正确,当返回0时表示返回正确 1 按照文件类型进行判断 ...
- spring定时任务执行两次
最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...
- BigDecimal 计算注意事项
BigDecimal 在进行除法运算(divide)时一定要注意:如果被除数为变量,一定要指定精度 和 舍入模式,否则会报:Non-terminating decimal expansion; no ...
- 记ByteCTF中的Node题
记ByteCTF中的Node题 我总觉得字节是跟Node过不去了,初赛和决赛都整了个Node题目,当然PHP.Java都是必不可少的,只是我觉得Node类型的比较少见,所以感觉挺新鲜的. Nothin ...