作者: 负雪明烛
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. 1 <= A.length <= 10000
  2. 1 <= B.length <= 10000
  3. 1 <= A[i] <= 100000
  4. 1 <= B[i] <= 100000
  5. It is guaranteed that Alice and Bob have different total amounts of candy.
  6. 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 手中查找是否包含目标糖果。

代码

本题的代码比较简单。

  1. 求数组 A、数组 B 所有糖果大小的总和;使用 set 保存 B 中的各个元素;
  2. 求出两个小朋友手中糖果大小总和相等时的目标 target;
  3. 遍历 A 中的每个糖果,求如果把该糖果交换出去,期望 Bob 交换的糖果大小 expect_b。
  4. 从 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 []

刷题心得

今天这个题虽然简单,但还是有可以积累的新知识的:

  1. 时间复杂度的估计,要学会根据数据规模,判断应该用什么时间复杂度的算法。
  2. 根据时间复杂度寻找合适的算法/数据结构,因此需要牢记算法/数据结构的时间复杂度。
  3. 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)的更多相关文章

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

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

  3. Leetcode888.Fair Candy Swap公平的糖果交换

    爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...

  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_easy】888. Fair Candy Swap

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

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

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

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

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

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

随机推荐

  1. 日常Java 2021/10/28

    Java lterator Java lterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayList和HashSet等集合.lterator是Java迭代器最简单的 ...

  2. vim中搜索指定单词(不加前后缀)

    \< : 搜索内容作为单词开头 \> : 搜索内容作为单词结尾 一起用即为将搜索内容指定为whole word e.g. : word_suffix word 如果用/word来搜索则两个 ...

  3. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  4. java foreach循环抛出异常java.util.ConcurrentModificationException

    代码如下: for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { if (Integer.parseInt(i ...

  5. entfrm开发平台,一个免费开源可视化的无代码开发平台

    简介 entfrm开发平台,是一个以模块化为核心的无代码开发平台,是一个集PC和APP快速开发.系统管理.运维监控.开发工具.OAuth2授权.可视化数据源管理与数据构建.API动态生成与统计.工作流 ...

  6. 【编程思想】【设计模式】【行为模式Behavioral】中介者模式Mediator

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/mediator.py #!/usr/bin/env py ...

  7. shell awk命令字符串拼接

    本节内容:awk命令实现字符串的拼接 输入文件的内容: TMALL_INVENTORY_30_GROUP my163149.cm6 3506 5683506 mysql-bin.000013 3273 ...

  8. SQL注入 (1) SQL注入类型介绍

    SQL注入 SQL注入介绍与分类 1. 什么是sql注入 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 2. sql注入类型 按照注入 ...

  9. 《手把手教你》系列技巧篇(五十一)-java+ selenium自动化测试-字符串操作-下篇(详解教程)

    1.简介 自动化测试中进行断言的时候,我们可能经常遇到的场景.从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言.这个时候就需要我们对字符串进行操作,宏哥这里介绍两种 ...

  10. Tableau如何绘制多边形地图

    一.把省\自治区拖拽至标记生成地图二.把销售额拖拽至标记 三.地图-地图层-冲蚀100% 四.最终结果如图所示