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.

Idea 1. Set + 由结果推这个pair的关系

Sa -a + b = Sb -b + a -> a - b = (Sa - Sb)/2, for each b find the a satisfies the diff, like to look for a pair with target diff

Time complexity: O(Na + Nb)

Space complexity: O(Na)

 class Solution {
public int[] fairCandySwap(int[] A, int[] B) {
int sumA = Arrays.stream(A).sum();
int sumB = Arrays.stream(B).sum(); Set<Integer> setA = IntStream.of(A).boxed().collect(Collectors.toSet()); int diff = (sumA - sumB)/2;
for(int b: B) {
if(setA.contains(diff + b)) {
return new int[] {diff+b, b};
}
} return new int[0];
}
}

Idea 1.a or alternatively binary search the target on sorted list, trade off between time and space

Time complexity: O(nlogn + Na + Nb)

Space complexity: O(1) if sort in place and allowed to modify the array

Fair Candy Swap LT888的更多相关文章

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

  2. 【Leetcode_easy】888. Fair Candy Swap

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

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

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

  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 公平糖果交换

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

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

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

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

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

随机推荐

  1. iOS字符串自动计算文本的宽和高

    根据字符串如何自动计算出这些字符所占的宽和高: 首先,需要知道要显示的字体的样式,因为不同大小的字体所占据的空间大小不一样. 其次,要设置限制范围,例如一串字符可以显示成一行(较宽),也可以显示成多行 ...

  2. 爬虫之scrapy

    一.项目简单流程 1.创建项目 scrapy startproject 项目名 2.创建Spider cd 项目名 scrapy genspider 爬虫名 域名 class YokaSpider(s ...

  3. 设计在canal中的运用,看到随手记下

    观察者模式,定义添加修改删除对应的操作 系统很多Monitor/Listener都是类似 Monitor内含listener,调用再触发 public synchronized void start( ...

  4. qt学习001之运行对话框

    使用QT实现Window下运行对话框 1.摆放控件 首先设置并摆放相应的对话框控件,并更改相应名称:        2.实现功能 1)在文本框中输入信息后,点击确定或回车可以运行系统中相应的程序: 点 ...

  5. Hadoop Java API操作HDFS文件系统(Mac)

    1.下载Hadoop的压缩包 tar.gz   https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/stable/ 2.关联jar包 在 ...

  6. PHP使用RabbitMQ实例

    相关博文:CentOS6.9安装RabbitMQ和源码编译安装php的RabbitMQ扩展RabbitMQ入门基础CentOS7源码编译安装nginx+php7.2+mysql5.7并使用system ...

  7. wireshark抓包新手使用教程

    wireshark是非常流行的网络封包分析软件,可以截取各种网络数据包,并显示数据包详细信息.常用于开发测试过程各种问题定位. Wireshark软件安装 软件下载路径:wireshark官网.按照系 ...

  8. java mybatis后台判断表是否存在mysql

    <select id="isTableExist" parameterType="string" resultType="int"&g ...

  9. 将ipad作为电脑拓展屏或分屏的简单方法

    用Ipad实现电脑分屏的方法是挺简单的,但鉴于部分小白找不到合适的门路,在此重新分享一下. 需要的装备:  ipad   电脑   数据连接线 方法:某宝上搜索 duet display ,只需1元左 ...

  10. 字符IO流

    输入 FileReader的用法: 1. 找到目标文件 2. 建立数据的输入通道 3. 读取数据 4. 关闭资源 具体实例:从硬盘中读取文件 输出 FileWriter的使用步骤: 1. 找到目标文件 ...