【leetcode】870. Advantage Shuffle
题目如下:

解题思路:几千年前,一个古人曾经解过这个题目,他的名字叫做田忌,后人称他的解题思想叫做“田忌赛马”。言归正传,本题就是一个田忌赛马的问题,先将A与B进行排序,然后判断A[0]与B[0]的大小,如果A[0] > B[0],那么A[0]和B[0]就是一对,将A[0]和B[0]同时从数组删除;如果A[0] <= B[0],把A[0]存入一个holdspace,同时删除A[0],一直循环,直到len(A) = 0 为止,这些都是Advantage。如果B还有元素的话,那么就用holdspace中的元素和B配对,这些就属于非Advantage了。
代码如下:
class Solution(object):
def advantageCount(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
a = A
b = B
sb = B[:]
sb.sort()
a.sort()
dic = {}
queue = []
while len(sb) > 0 and len(A) > 0:
if sb[0] < a[0]:
if sb[0] not in dic:
dic[sb[0]] = [a[0]]
else:
dic[sb[0]].append(a[0])
del sb[0]
else:
queue.append(a[0])
del a[0]
res = []
for i in B:
if i in dic:
res.append(dic[i][0])
del dic[i][0]
if len(dic[i]) == 0:
del dic[i]
else:
res.append(queue[0])
del queue[0]
return res
【leetcode】870. Advantage Shuffle的更多相关文章
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 贪心: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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- Linux中退出循环命令
[root@a ~]#cat break.sh #!/bin/bash while : #其中“:”表示while循环的条件永远为真的意思 do read -p "Enter a numbe ...
- First-order logic
w https://en.wikipedia.org/wiki/First-order_logic
- 测开之路一百一十:bootstrap图片
bootstrap图片 引入bootstrap 原版的图片 bootstrap处理后的: 圆角.圆形.缩略图 自适应窗口
- sklearn版本
10.19.0以前的sklearn版本才有cross_validation包,这个时候不要用model_selection导入StratifiedKFold,要用cross_validation,0. ...
- Krypton Suite of .NET WinForms Controls
The Krypton Suite of .NET WinForms controls are now freely available for use in personal or commeric ...
- Java 基础-类与面向对象
类 Object 类(java.lang.Object)是所有 Java 类的直接或间接父类. 成员方法及变长参数 方法定义: [public | protected | private] [stat ...
- Python解决ModuleNotFoundError: No module named 'Queue'的问题
我们知道Python2和Python3两个版本之间,有些不兼容的地方,Python3中引入Queue会报出这个问题. Python3中要这样引入: import queue Python2中要这样引入 ...
- C# DropDownList绑定添加新数据的三种方法
一.在前台手动绑定 <asp:DropDownList ID="DropDownList1" runat="server"> <asp: ...
- ElasticSearch 基础 1
ElasticSearch 基础=============================== 索引创建 ========================== 1. RESTFUL APIAPI 基本 ...
- Node.js实战11:fs模块初探。
fs模块封装了对文件操作的各种方法,比如同步和异步读写.批量操作.流.监听. 我们还是通常例程学习, 获取目录下的文件清单: var fs =require("fs"); fs.r ...