题目如下:

解题思路:几千年前,一个古人曾经解过这个题目,他的名字叫做田忌,后人称他的解题思想叫做“田忌赛马”。言归正传,本题就是一个田忌赛马的问题,先将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的更多相关文章

  1. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  2. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. leetcode-mid-Linked list- 103. Binary Tree Zigzag Level Order Traversal

    mycode 99.24% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x) ...

  2. Using Groovy To Import XML Into MongoDB

    w https://trishagee.github.io/post/groovy_import_to_mongodb/

  3. chales抓包,模拟异常情况

    抓包能做什么? 1 .可以抓取客户端和server的请求和返回,可以借助判断是客户端问题是server问题 2.可以模拟各种异常情况用来测试异常情况 3.无接口文档情况下测试接口 怎么修改你抓到的请求 ...

  4. oracle alter index rebuild offline与online

    oracle index build online与offline测试环境为oracle 11.2.0.4 --sql test SQL> conn test/test )); begin .. ...

  5. linux环境,centos7,安装docker

    https://www.jianshu.com/p/2dae7b13ce2f 1.安装依赖包 yum install -y yum-utils device-mapper-persistent-dat ...

  6. js数组,运算符

  7. xmake v2.1.9版本发布,增加可视化图形菜单配置

    此版本主要增加xmake f --menu实现用户自定义图形菜单配置,界面风格类似linux的make menuconfig: [图片上传失败-(image-505bc0-1517795319124) ...

  8. Billboard 题解 hdu2795

    Billboard 题解 hdu2795 题意 有个广告牌,上面需要依次贴广告,广告的高度均为1,但是宽度不同,每次都想贴在最靠左上的位置,按照顺序进行广告的话,输出每个广告位于广告牌的高度. 解题思 ...

  9. stl应用

    http://codeforces.com/problemset/problem/1154/E E. Two Teams time limit per test 2 seconds memory li ...

  10. select,poll 和 epoll ??

    其实所有的 I/O 都是轮询的方法,只不过实现的层面不同罢了. 其中 tornado 使用的就是 epoll 的. selec,poll 和 epoll 区别总结 基本上 select 有 3 个缺点 ...