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 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...
随机推荐
- Windows下android模拟器环境搭建
一.搭建java环境 1.下载jdk1.6.0_45 下载地址:http://www.oracle.com/technetwork/java/archive-139210.html ----> ...
- jQuery中event.target和this的区别
http://www.cnblogs.com/hhsy/p/5647930.html 该链接有详细讲解
- python 网站 监控返回值
import requests try: code = requests.get("https://api.sudaizhijia.cm/").status_code print( ...
- POI导入具有合并了单元格的Excel
POI进行单行单行地导入的数据在网上有许多的文章,但是要导入一个具有合并单元格的excel貌似比较难找.刚好最近完成了这样的一个需求,要求导入具有合并单元格的excel: /** * 读取excel数 ...
- asp.net ajax get 调用(和post不一样,直接返回json才行,否则报错;post不能返回json)
<script type="text/javascript" > $(document).ready(function () { $('#Label1').click( ...
- Git综合使用命令行和gui工具小结
使用Git的时候,综合使用命令行和gui工具,可以把Git用的最舒服,因此这里总结下使用gui和命令行的一些对应操作, gui中拉取:git pull origin dev_branch gui中推送 ...
- Tomcat 启动时 SecureRandom 非常慢解决办法,亲测有效
1.找到jre—>lib—>security 2.找到 securerandom.source=file:/dev/random 替换成:securerandom.source= ...
- (转)Thinkpad X1 Carbon 扩展硬盘
http://tieba.baidu.com/p/5837920925 网上看到有人成功利用X1C空闲的4G模块来实现了扩充用的是东芝RC100或者建兴的T11 其实难点应该是2242这种尺寸的SSD ...
- Intel 82599网卡异常挂死原因
前提背景: 生产环境上,服务器网络突然断链,ssh连接失败. 问题初步定位: 查找内核日志,得到网卡异常信息 Jan 24 11:52:43 localhost kernel: ixgbe 0000: ...
- Window上编译最新版libCef(Branch 2704)(转载)
转自http://blog.csdn.net/mfcing/article/details/52066579 1.开发环境搭建 VS2010及以上版本,CMake 2.8.12.1及以上版本.我安 ...