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.

这道题说爱丽丝和鲍勃两人有不同大小的糖果,现在要让两人交换一个糖果,使得交换后两人的糖果总重量相同,而且限定了两人初始时的糖果总量不相同,并且一定会有解。若我们仔细观察题目中给的例子,可以发现所有例子中起始时 Alice 和 Bob 两人的糖果总重量的差值一定时偶数,这是 make sense 的,因为最终两人的糖果总量时要相同的,那么起始时的重量差就应该能平均分为两部分,一部分来弥补轻的一方,一部分来抵消重的一方。那么有了这个 diff,我们只需要在两个数组中查找差值为 diff 的两个数字了,其实就是 [Two Sum](http://www.cnblogs.com/grandyang/p/4130379.html) 的变种,使用一个 HashSet 先来保存数组 A 中所有的数字,然后遍历数组B中的每个数字 num,查找 HashSet 中否存在 num+diff 即可,参见代码如下:

class Solution {
public:
vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
int diff = (accumulate(A.begin(), A.end(), 0) - accumulate(B.begin(), B.end(), 0)) / 2;
unordered_set<int> st(A.begin(), A.end());
for (int num : B) {
if (st.count(num + diff)) return {num + diff, num};
}
return {};
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/888

类似题目:

Two Sum

参考资料:

https://leetcode.com/problems/fair-candy-swap/

https://leetcode.com/problems/fair-candy-swap/discuss/161269/C%2B%2BJavaPython-Straight-Forward

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 888. Fair Candy Swap 公平糖果交换的更多相关文章

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

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

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

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

  4. 【Leetcode_easy】888. Fair Candy Swap

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

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

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

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

  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. 海边拾贝-B-优秀博客/网站

    记下若干优秀博客,方便后期检索.会不定期更新: 优秀的程序员,从使用Github开始:https://help.github.com/en/github/managing-your-work-on-g ...

  2. Spring @CrossOrigin 通配符 解决跨域问题

    @CrossOrigin 通配符 解决跨域问题 痛点: 对很多api接口需要 开放H5 Ajax跨域请求支持 由于环境多套域名不同,而CrossOrigin 原生只支持* 或者具体域名的跨域支持 所以 ...

  3. 一篇文章,带你玩转MVVM,Dapper,AutoMapper

    一.背景 由于现在做的项目都是采用WPF来进行UI设计,开发过程中都是基于MVVM来进行开发,但是项目中的MVVM并不是真正的把实体和视图进行解耦,而是将实体和视图完全融合起来,ViewModel只是 ...

  4. 最近在折腾在线编辑,研究了下Wopi,下面粘贴出自己Office Online Server2016搭建与部署

    至少需要两台服务器,一台域控制器,一台部署Office Online Server https://docs.microsoft.com/zh-cn/officeonlineserver/office ...

  5. 理解类、对象、实例、原型链以及继承 - WPF特工队内部资料

    理解类.对象.实例.原型链以及继承 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  6. asp.net实现页面跳转后不可以返回

    window.history.go(0); Response.Write("<script> window.history.go(0);alert('恭喜user注册成功!!!\ ...

  7. oracle 主键生成策略-sequence序列+trigger触发器

    oracle中设置表的主键字段为自增序列(实例)1.首先创建一个表(如日志表) //删除库表中存在的日志表drop table S_LOG_INFO cascade constraints;//新建日 ...

  8. 运算符 &(与运算)、|(或运算)、^(异或运算)

    按位与运算符(&) 参加运算的两个数据,按二进制位进行“与”运算. 运算规则:0&0=0;  0&1=0;   1&0=0;    1&1=1; 按位或运算符( ...

  9. Django 练习班级管理系统五 -- 查看老师列表

    models.py 对应的配置 class Classes(models.Model): caption = models.CharField(max_length=32) class Teacher ...

  10. 简单使用:SpringBoot使用freemarker

    使用步骤: a : 添加依赖 b: 创建模板文件 保存位置resources/templates 目录下 文件后缀名.ftl c 编写controller 把结果传递给模板 在resources.te ...