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.

原题地址: Fair Candy Swap

题意: 给两个数组A B,各选取一个数进行交换,使得数组A的和等于数组B的和,返回交换的两个数

例子:

Input: A = [1,1], B = [2,2]
Output: [1,2] # 交换之后 A = [1,2] B = [1, 2]

(1)求出两个数组的平均值,也就是最后两个数组的和

(2)遍历数组A,假设当前值是要交换的数A[i],那么数组B中要交换的数是 平均值 - (sum(A) - A[i]) , 并判断此数是否在数组B中.如果存在,返回这两个数

注意: 可能存在重复值,是否存在重复值对结果没有影响,为了减少遍历次数,可以用 set() 去重,并且获取元素的时间复杂度是O(1)

代码:

class Solution(object):
def fairCandySwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
a, b , set_b = sum(A), sum(B), set(B)
mean = (a + b) / 2
for num in A:
target = mean - (a - num)
if 1<= target <= 100000 and target in set_b:
return [num, target]

时间复杂度:O(n)

遍历数组A为n次,判断一个数是否在set(B)中,需要1次,

空间复杂度:list转化为set,为O(n)

888. Fair Candy Swap@python的更多相关文章

  1. 【Leetcode_easy】888. Fair Candy Swap

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

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

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

  3. [LeetCode&Python] Problem 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 ...

  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 tha ...

  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 that Ali ...

  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. [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 ...

  8. Fair Candy Swap LT888

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

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

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

随机推荐

  1. bzoj 5499: [2019省队联测]春节十二响【堆】

    首先看两条链怎么合并,贪心可得是从大到小取max,多条链同理 所以dfs合并子树的大根堆即可,注意为了保证复杂度,合并的时候要合并到最长链上,证明见长链剖分 #include<iostream& ...

  2. P5165 xtq的棋盘

    传送门 设\(f[i]\)为\(i\)位置向左走一步的期望时间,那么答案就是\(\sum_{i=1}^mf[i]\) 首先\(f[n]=1\),设\(p\)为向左的概率,对于\(i<n\)的位置 ...

  3. Luogu P3393 逃离僵尸岛【最短路】By cellur925

    题目传送门 题目大意:(其实概括出来也就基本做完了hh)在一张有$n$个点,$m$条边的无向图上,有$k$个点是不能经过的,而与之距离不超过$s$的点,到他们会花费$Q$元,到其他点会花费$p$元,求 ...

  4. 组合数学1.4&3.10 By cellur925

    本文引用于清华大学出版社卢开澄.卢华明<组合数学第五版>. 今天我们稍微讨论下圆排列以及$n$对夫妻的问题. 1.4圆周排列 这个问题是:从$n$个人中取$r$个在圆周上,我们用$Q(n, ...

  5. hdu1068 Girls and Boys 匈牙利算法(邻接表)

    #include <cstdio> #include <algorithm> #include <cstring> #include <vector> ...

  6. 跟我一起玩Win32开发(25):监视剪贴板

    自从郭大侠和蓉儿离开桃花岛后,最近岛比较寂静,有一种“门前冷落鞍马稀”的感觉.于是,老邪就拿出<九阴真经>认真阅读,同时用迅雷下载经典大剧<汉武大帝>晚上睡觉前看上几集,老邪一 ...

  7. iOS中数据类型转换--遇到则记录

    1.NSString转NSNumber 使用情景:CoreData存储数据,其中一个为价格,CoreData里面定义为float 用文本输入框得到的数据类型是NSString,将NSString转换成 ...

  8. JAVA常用知识总结(五)——Linux

    简单介绍一下 Linux 文件系统? 在Linux操作系统中,所有被操作系统管理的资源,例如网络接口卡.磁盘驱动器.打印机.输入输出设备.普通文件或是目录都被看作是一个文件. 也就是说在LINUX系统 ...

  9. sass+compass起步

    前言:Sass is an extension of CSS that adds power and elegance to the basic language. It allows you to ...

  10. SpringBoot项目不占用端口启动

    @EnableScheduling @SpringBootApplication public class Application { public static void main(String[] ...