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 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]
分析:
两个人拥有一些糖果,交换一个后,两人糖果总和相等。
我们计sumA为A拥有的和,sumB为B拥有的和,a是A交出去的糖果,b是B交换出去的糖果,根据题意有如下等式。
sumA-a+b=sumB-b+a => sumB-sumA=2b-2a => b-a=(sumB-sumA)/2
我们只需要找到一组满足b-a=(sumB-sumA)/2的b和a,交换后两人的糖果总和便相等了。
可以使用map存储A糖果的个数,然后遍历B的时候,查找map[(sumB-sumA)/2-b]是否等于1就可以了,实现方法有很多,这里就不一一赘述了。
程序:
class Solution {
public:
vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
vector<int> res;
int sumA = ;
int sumB = ;
for(auto a:A){
sumA += a;
}
for(auto b:B){
sumB += b;
}
int cha = (sumB - sumA)/;
unordered_map<int, int> m;
for(auto a:A){
m[a] = ;
}
for(auto b:B){
if(m[b-cha] == ){
res.push_back(b-cha);
res.push_back(b);
return res;
}
}
return res;
}
};
LeetCode 888. Fair Candy Swap(C++)的更多相关文章
- [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 解题报告
题目要求 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】888. Fair Candy Swap 公平的糖果棒交换(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...
- [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 ...
- LeetCode.888-公平的糖果交换(Fair Candy Swap)
这是悦乐书的第339次更新,第363篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第208题(顺位题号是888).Alice和Bob有不同大小的糖果棒:A[i]是Alic ...
- C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...
- [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 ...
随机推荐
- PhpStorm中实现代码自动换行
方法一: 随便打开一个页面,在显示行号(最左边)这里鼠标右击,勾选"Use Soft Wraps". 方法二: 选择"File-->>Settings--&g ...
- Hadoop分布式集群搭建_1
Hadoop是一个开源的分布式系统框架 一.集群准备 1. 三台虚拟机,操作系统Centos7,三台主机名分别为k1,k2,k3,NAT模式 2.节点分布 k1: NameNode DataNode ...
- axios的post请求后台(ThinkPHP5)接收不到数据
最近做vue项目,做分页的功能,使用post给后台发送数据,使用接口还是工具(postman)都可获取数据,唯独axios获取不到:经过排除,发现这与axios的post传参格式有关系: this.$ ...
- Hibernate学习笔记四
1 整合log4j(了解) l slf4j 核心jar : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日志第三方进行整合. l 整合导入jar包 log4j 核心包 ...
- Python之路(三)
今天这篇文章是对上一篇文章的补充,主要说一下与int和str有关的几个函数. ------------------------------------------------------------( ...
- 【leetcode 简单】 第五十二题 有效电话号码
给定一个文本文件 file.txt,请只打印这个文件中的第十行. 示例: 假设 file.txt 有如下内容: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Li ...
- 【LG4185】[USACO18JAN]MooTube
[LG4185][USACO18JAN]MooTube 题面 洛谷 题解 先将所有操作和询问离线 然后按照边权从大到小将操作和询问排序 利用\(two\;pointers\),每次扫到一个询问,将边权 ...
- 1245 最小的N个和
1245 最小的N个和 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 有两个长度为 N 的序列 A 和 B, ...
- Apache入门篇(一)之安装部署apache
一.HTTPD特性 (1)高度模块化:core(核心) + modules(模块) = apache(2)动态模块加载DSO机制: Dynamic Shared Object(动态共享对象)(3)MP ...
- Redis可视化客户端管理Web UI工具收集
https://github.com/uglide/RedisDesktopManager(推荐,全平台支持的桌面UI工具) Web方案: https://github.com/ErikDubbelb ...