【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人公众号: 每日算法题
本文关键词:力扣,LeetCode,算法题,算法,Python
题目地址:https://leetcode.com/problems/fair-candy-swap/description/
题目描述
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.
题目大意
交换A,B两个数组中的一个数字,使得两个数组的和相等。要返回的结果是个要交换的两个数字,分别来自A,B。
解题方法
题目是个 Easy 题目,看了数据范围小于等于 10000, 就不要想太多了,可以直接上 O(N) 的解法!
当两个小朋友交换完了糖果之后,两人手中的糖果大小总和就相等了。那这个数字是多少呢?很显然是两个小朋友目前手中糖果的平均数。
所以,我们可以先求出最终两者手中的糖果大小之和是多少,设为 target 。然后逐个判断 Alice 的糖果 A[i] ,计算如果把该糖果交换出去,Bob 应该给 Alice 多大的糖果呢?答案应该是 target - (Alice的糖果总和 - A[i]) ,即 Alice 期望的糖果大小之和 target,减去 Alice 剩余的糖果大小之和。
那么问题来了:如果 Alice 想要用 A[i] 跟 Bob 交换,那么 Bob 手里面有没有 Alice 此时想要的糖果大小呢?即 target - (Alice的糖果总和 - A[i]) 是否在数组 B 中?
由于遍历 Alice 的糖果需要 O(N) 时间复杂度,此时留给 Bob 查找手里是否有 Alice 想要的这个糖果的时间复杂度只剩 O(1) 了。因为如果也用 O(N) 的时间复杂度查找出来,那么总时间复杂度是 O(N ^ 2) ,数组长度是 10000, 导致总计算量到达 10 ^ 8 ,可能会超时。
在常用的数据结构中,只有 Set 能够满足在 O(1) 时间内,判断一个元素是否存在。我们使用 Set 在 Bob 手中查找是否包含目标糖果。
代码
本题的代码比较简单。
- 求数组 A、数组 B 所有糖果大小的总和;使用 set 保存 B 中的各个元素;
- 求出两个小朋友手中糖果大小总和相等时的目标 target;
- 遍历 A 中的每个糖果,求如果把该糖果交换出去,期望 Bob 交换的糖果大小 expect_b。
- 从 B 的 set 中查找是否包含 expect_b,如果包含则说明
[a, expect_b]是一种可以满足要求的交换。
时间复杂度:O(N),因为只对数组 A 遍历了一次。
空间复杂度:O(N),因为用了 set 保存数组 B 的所有元素。
使用 Python2 写的代码如下。
class Solution(object):
def fairCandySwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
sum_A, sum_B, set_B = sum(A), sum(B), set(B)
target = (sum_A + sum_B) // 2
for a in A:
expect_b = target - (sum_A - a)
if expect_b in set_B:
return [a, expect_b]
return []
刷题心得
今天这个题虽然简单,但还是有可以积累的新知识的:
- 时间复杂度的估计,要学会根据数据规模,判断应该用什么时间复杂度的算法。
- 根据时间复杂度寻找合适的算法/数据结构,因此需要牢记算法/数据结构的时间复杂度。
- set 是一种拿空间换时间的数据结构,能够在
O(1)的时间内判断某个元素是否存在其中。
关于作者
我是本文的作者是负雪明烛,毕业于北京邮电大学,目前就职于阿里巴巴。坚持刷算法题 5 年,共计刷了 800 多道算法题。做过的每个算法题都在 CSDN 上写题解博客,获得好评无数,CSDN 的累计阅读量已经 160万 次!博客地址是:https://blog.csdn.net/fuxuemingzhu
「每日算法题」公众号是我维护的一个算法题解公众号,主要讲解算法题的解法,也会分享找工作的经验。欢迎关注。

日期
2018 年 8 月 24 日 —— Keep fighting!
2018 年 11 月 9 日 —— 睡眠可以
【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)的更多相关文章
- [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 ...
- 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 ...
- Leetcode888.Fair Candy Swap公平的糖果交换
爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...
- 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 ...
- 【Leetcode_easy】888. Fair Candy Swap
problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode.888-公平的糖果交换(Fair Candy Swap)
这是悦乐书的第339次更新,第363篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第208题(顺位题号是888).Alice和Bob有不同大小的糖果棒:A[i]是Alic ...
随机推荐
- Linux—export命令查看、修改用户环境变量
Linux export 命令用于设置或显示环境变量. 在 shell 中执行程序时,shell 会提供一组环境变量. export 可新增,修改或删除环境变量,供后续执行的程序使用. export ...
- sersync+rsync进行数据同步
一:环境 操作系统环境:redhat6.6 内核版本:2.6.32-358.el6.x86_64 rsync server:192.168.2.3(部署rsync server) rsync clie ...
- .Net Core——用SignalR撸个游戏
之前开内部培训,说到实时web应用这一块讲到了SignalR,我说找时间用它做个游戏玩玩,后面时间紧张就一直没安排.这两天闲了又想起这个事,考虑后决定用2天时间写个斗D主,安排了前端同学写客户端,我写 ...
- Java项目发现==顺手改成equals之后,会发生什么?
最近发生一件很尴尬的事情,在维护一个 Java 项目的时候,发现有使用 == 来比较两个对象的属性, 于是顺手就把 == 改成了 equals.悲剧发生...... == 和 equals 的区别 = ...
- ctfshow WEB入门 信息收集 1-20
web1 题目:开发注释未及时删除 查看页面源代码即可 web2 题目:js把鼠标右键和f12屏蔽了 方法一: 禁用JavaScript 方法二: url前面加上view-source: web3 题 ...
- 大数据学习day11------hbase_day01----1. zk的监控机制,2动态感知服务上下线案例 3.HDFS-HA的高可用基本的工作原理 4. HDFS-HA的配置详解 5. HBASE(简介,安装,shell客户端,java客户端)
1. ZK的监控机制 1.1 监听数据的变化 (1)监听一次 public class ChangeDataWacher { public static void main(String[] arg ...
- linux允许直接以root身份ssh登录
1. sudo su - 2. vim /etc/ssh/sshd_config 3. let "PermitRootLogin" equal yes 4. :wq 5. serv ...
- RPC、HTTP、RESTful
RESTful RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义.RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT ...
- AD设置过孔盖油
设置所有的过孔盖油 ==> 先选中一个过孔,然后根据对象类型查找相似器件,选中所有的过孔,然后勾选过孔盖油选项即可. 上述这种方法不是太好,每次修改完PCB后都要确认下新增的过孔是否已经过孔盖油 ...
- typeScript基本概念
我一直认为学习是知识的累加,而前端技术也是进步的.所以学习的重点就是,'它有什么不同,它好在哪里'.这要求我们必须结合之前的经验和知识去学习一门新技术,而不是无情的复制粘贴机器. 首先,ts的官方定义 ...