Fair Candy Swap LT888
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.
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的更多相关文章
- 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_easy】888. Fair Candy Swap
problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...
- [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 解题报告
题目要求 Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy tha ...
- [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(C++)
题目: Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that ...
- [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 ...
- C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...
- 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...
随机推荐
- 用系统默认mail服务实现邮件发送
用系统默认mail服务实现邮件发送 1.操作步骤 第一步:设备服务器发送邮件要用的,邮箱地址,账号密码 编辑/etc/mail.rc vim /etc/mail.rc 在文件的结尾追加,账号信息配置 ...
- 深入理解Java虚拟机读书笔记8----Java内存模型与线程
八 Java内存模型与线程 1 Java内存模型 ---主要目标:定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样的底层细节. ---此处的变量和J ...
- idea Tomcat部署时没有update classes and resources
idea 没有update classes and resources 发现为了方便调试页面,想用idea的update classes and resources找不到了,发现需要把 ...
- Office免费激活方法!亲测有效!2019年4月3日测试
内容转载自:https://blog.csdn.net/qq_41785863/article/details/83619401 防止原博主删除,我先放上自己下载好了的工具分享链接. 链接:https ...
- 虚拟机安装及ubuntu-16.04.3-desktop-amd64.iso映像文件的安装
虚拟机安装及ubuntu-16.04.3-desktop-amd64.iso映像文件 搞了大半天才搞清楚装linux的前提是要先安装虚拟机的 先下载虚拟机,在然后创建虚拟机,在虚拟机里面再安装linu ...
- jQuery 效果 - 动画 animate() 方法
我们先看一个demo <!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11. ...
- jquery中checkbox的选中,反选,全不选 注意1.6版本以上将attr改成prop
<script type="text/javascript"> $(function () { // 全选 $("#btnCheckAll").bi ...
- nagios监控mysql及邮件报警
1.使用默认监控命令check_http命令+相关的参数来实现,如下: 在command.cfg添加如下关键词监控命令:check_http_word,参数解析:-I指定IP或者主机名,-u指定URL ...
- uniapp 标题后面紧跟一个标签的布局
使用uni-app的时候,因为想用flex布局,所以一开始就设置了全部view display为flex. 之后遇到了如下这种样式: 开始想了半天没想出来,后来想到div span有这个效果. 然后就 ...
- spreed&rest
ES6变化-spreed&rest … 展开&收集运算符: 此运算符在不同地方使用有不同的功效,可以从写和读两个角度考虑. 写:function test (…arg){}; test ...