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. NYOJ7——街区最短路径问题

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4  描述:一个街区有很多住户,街区的街道只能为东西.南北两种方向.住户只可以沿着街道行走.各个街道之间的间隔相等 ...

  2. angular源码剖析之Provider系列--QProvider

    QProvider 简介 源码里是这么描述的: A service that helps you run functions asynchronously, and use their return ...

  3. (4)ASP.NET Core 中间件

    1.前言 整个HTTP Request请求跟HTTP Response返回结果之间的处理流程是一个请求管道(request pipeline).而中间件(middleware)则是一种装配到请求管道以 ...

  4. (水题)洛谷 - P1579 - 哥德巴赫猜想(升级版)

    https://www.luogu.org/problemnew/show/P1579 先预处理出素数看看有多少个,大概才2500个不到(事实上素数的个数大约是 $\frac{n}{ln(n)}$ ) ...

  5. DFS系列 POJ(自认为的讲解)

    C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...

  6. Adore

    (非公共题目) 问题描述 小 w 偶然间⻅到了一个 DAG.这个 DAG 有 m 层,第一层只有一个源点,最后一层只有一个汇点,剩下的每一层都有 k 个节点. 现在小 w 每次可以取反第 i(1 &l ...

  7. poj2893 M*N puzzle 【n*m数码问题小结】By cellur925

    题目传送门 这个问题是来源于lydrainbowcat老师书上讲排序的一个扩展.当时讲的是奇数码问题,其实这种问题有两种问法:一种局面能否到另一种局面.到达目标局面的最小步数. 本文部分内容引用于ly ...

  8. scrapy 用法总结

    待更新: 建立python开发虚拟环境 virtualenv  mkvirtualenv --python=the-path-to-the-python-you-want-to use 安装: 使用p ...

  9. 同一台服务器上部署多个Tomcat的配置修改方法

    同一服务器部署多个tomcat时,存在端口号冲突的问题,所以需要修改tomcat配置文件server.xml,以tomcat7为例. 首先了解下tomcat的几个主要端口: <Server po ...

  10. 设置Linux环境变量的方法和区别_Ubuntu/CentOS

    设置 Linux 环境变量可以通过 export 实现,也可以通过修改几个文件来实现,有必要弄清楚这两种方法以及这几个文件的区别. 通过文件设置 Linux 环境变量 首先是设置全局环境变量,对所有用 ...