Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.

Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy.  (The total amount of candy a person has is the sum of the sizes of candy bars they have.)

Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange.

If there are multiple answers, you may return any one of them.  It is guaranteed an answer exists.

Example 1:

Input: A = [1,1], B = [2,2]
Output: [1,2]

Example 2:

Input: A = [1,2], B = [2,3]
Output: [1,2]

Example 3:

Input: A = [2], B = [1,3]
Output: [2,3]

Example 4:

Input: A = [1,2,5], B = [2,4]
Output: [5,4]

Note:

  • 1 <= A.length <= 10000
  • 1 <= B.length <= 10000
  • 1 <= A[i] <= 100000
  • 1 <= B[i] <= 100000
  • It is guaranteed that Alice and Bob have different total amounts of candy.
  • It is guaranteed there exists an answer.
class Solution(object):
def fairCandySwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
diff=(sum(A)-sum(B))/2
A=set(A)
for b in set(B):
if b+diff in A:
return b+diff, b

  

[LeetCode&Python] Problem 888. Fair Candy Swap的更多相关文章

  1. 【Leetcode_easy】888. Fair Candy Swap

    problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...

  2. 888. Fair Candy Swap@python

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  3. 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...

  4. [LeetCode] 888. Fair Candy Swap 公平糖果交换

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  5. LeetCode 888 Fair Candy Swap 解题报告

    题目要求 Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy tha ...

  6. LeetCode 888. Fair Candy Swap(C++)

    题目: Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that ...

  7. LeetCode.888-公平的糖果交换(Fair Candy Swap)

    这是悦乐书的第339次更新,第363篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第208题(顺位题号是888).Alice和Bob有不同大小的糖果棒:A[i]是Alic ...

  8. C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...

  9. [Swift]LeetCode888. 公平的糖果交换 | Fair Candy Swap

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

随机推荐

  1. git 从远程拉取代码、推代码的步骤

    (注:如果是几个人共同管理项目,并且你的队友在你之前推过代码,那你就需要 git pull 一下,把代码拉到本地,解决一下冲突,再执行以下步骤,将本地代码推到远程仓库.) 第一步:查看当前的git仓库 ...

  2. thinkphp 3.2 加载第三方库 第三方命名空间库

    tp 自动加载的介绍: http://document.thinkphp.cn/manual_3_2.html#autoload 第三方库不规范库 不适用命名空间的库 可以使用import函数导入,其 ...

  3. memory prefix mini mono multi out _m 5

      1● mini 小 迷你   2● mono 单一 ,单   3● multi 多

  4. Mysql 用户ip访问根据省份查询

    表名:shop_interview_customer 表结构:customerId空为游客模式 interviedId customerId interviewIP iPdetail 1 1001 1 ...

  5. linux网络连接--桥接bridge,NAT,host-only的区别

    linux网络连接主要分为三种:桥接,net,host_only 桥接使用的是真实网卡,电脑里面有两种真实网卡,有线网卡,无线网卡,当你使用的是无线连接, 则选择无线网卡,使用网线连接,则选择有线网卡 ...

  6. learning rndis protocol

    reference:  https://docs.microsoft.com/en-us/windows-hardware/drivers/network/overview-of-remote-ndi ...

  7. Spring MVC之DispatcherServlet初始化详解

    Spring作为一个优秀的web框架,其运行是基于Tomcat的.在我们前面的讲解中,Spring的驱动都是使用的ClassPathXmlApplicationContext,并且都是直接在main方 ...

  8. Pamulinawen--IPA--菲律宾伊洛卡诺语

    这是一首菲律宾的民谣(不是他加禄语/Tagalog, 而是伊洛卡诺语/Ilokano), 我们国家的著名歌手朱明瑛也翻唱过, 歌曲中文名为<<田野之歌>>.

  9. Centos7初始配置

    配置 centos7 ip地址: vi /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO=static ONBOOT=yes NM_CONTRO ...

  10. python中变量命名的基本规则,标识符和关键字

    变量的命名 目标 标识符和关键字 变量的命名规则 0.1 标识符和关键字 1.1 标识符 标示符就是程序员定义的 变量名.函数名 名字 需要有 见名知义 的效果,见下图:  标示符可以由 字母.下划 ...