[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 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 <= 100001 <= B.length <= 100001 <= A[i] <= 1000001 <= 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
类似题目:
参考资料:
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 公平糖果交换的更多相关文章
- 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,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 tha ...
- 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 ...
- 【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 ...
- Leetcode888.Fair Candy Swap公平的糖果交换
爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...
- [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 ...
随机推荐
- 大话设计模式Python实现-原型模式
原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 一个原型模式的简单demo: #!/usr/bin/env python # -*- c ...
- SourceTree 版本跳过bitbucket注册方法
1.安装sourcetree时 需要选择 bitbucket账号,这个令人头疼 当然肯定有办法来跳过这一步 2.关闭当前安装界面 进入 C:\Users\Administrator\AppData ...
- 云原生生态周报 Vol. 13 | Forrester 发布企业级容器平台报告
业界要闻 近日,全球知名市场调研机构 Forrester 发布首个企业级公共云容器平台报告.其中,阿里云容器服务的市场表现全球前三.中国第一,同时创造中国企业最好成绩,进入强劲表现者象限.报告显示,阿 ...
- C# MySQL,Dapper Trans,list
static async Task MySQLTransDemo() { try { using(dbConnection) { dbConnection.Open(); //The object m ...
- 接口interface实现与显示实现
接口实现: interface IMath { void Add(int x, int y); } public class MathClass : IMath { public void Add(i ...
- C#与vb.net源码代码互转网站
该转换器是印度开发团队推出的,推出时间也挺长,仅支持C#和VB.net代码转换.代码转换地址: C# -> VB.NET http://www.dotnetspider.com/convert ...
- 从VisualStudio资源文件看.NET资源处理
c# 工程里面,经常会添加资源文件. 作用: 一处文本多个地方的UI使用,最好把文本抽成资源,多处调用使用一处资源. 多语言版本支持,一份代码支持多国语言.配置多国语言的资源文件,调用处引用资源. 例 ...
- 面试官:来谈谈限流-RateLimiter源码分析
RateLimiter有两个实现类:SmoothBursty和SmoothWarmingUp,其都是令牌桶算法的变种实现,区别在于SmoothBursty加令牌的速度是恒定的,而SmoothWarmi ...
- Linux链接文件——管理链接文件的命令
Linux链接文件——管理链接文件的命令 摘要:本文主要学习了在Linux系统中创建链接文件的命令. ln命令 ln命令用于给文件创建链接,是Link的缩写. 基本语法 ln [选项] 源文件 目标文 ...
- mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法
mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法 #分开两个sql执行正常的语句,只保留最新1000条数据,删掉1000条 ...