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 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...
随机推荐
- 73.纯 CSS 创作一只卡通狐狸
原文地址:https://segmentfault.com/a/1190000015566332 学习效果地址:https://scrimba.com/c/cz6EzdSd 感想:过渡效果,圆角,定位 ...
- mybatis 映射生成mapper和pojo ---逆向工程的使用过程
使用逆向工程生成mapper和pojo 2. 新建一个项目,随便叫什么 3.导入mybatis-generator-core .mybatis.mybatis-spring.log4j等jar 4.在 ...
- HTTPS 基本流程 转载 https://zhuanlan.zhihu.com/p/27395037
协议 1.HTTP 协议(HyperText Transfer Protocol,超文本传输协议):是客户端浏览器或其他程序与Web服务器之间的应用层通信协议 . 2.HTTPS 协议(HyperTe ...
- oracle 的tnsnames.ora,listener.ora
x:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN listener.ora: # listener.ora Network Conf ...
- nginx问题集锦
1.配置访问指定路径的文件 以访问/mnt/data/logs下文件为例,修改nginx.conf配置,执行命令重新加载/usr/local/nginx/sbin/nginx -s reload lo ...
- POI--各种样式的XSSFCellStyle的生成
//背景色.フォント色.枠線より各種XSSFCellStyleの作成して.cellStyleMapに保存する private HashMap<String, XSSFCellStyle> ...
- 把一个syn报文给rst掉
下面展示一个极其简单的例子,看如何使用netfilter来将一个指定端口的syn报文给rst掉. //************************************************* ...
- 图解BERT(NLP中的迁移学习)
目录 一.例子:句子分类 二.模型架构 模型的输入 模型的输出 三.与卷积网络并行 四.嵌入表示的新时代 回顾一下词嵌入 ELMo: 语境的重要性 五.ULM-FiT:搞懂NLP中的迁移学习 六.Tr ...
- ARTS打卡计划第二周-Tips-mysql-binlog-connector-java的使用
最近发现一个挺不错的框架mysql-binlog-connector-java,可以实时监控binlog的变化. 首先检查mysql的binlog是否开启,在开启的情况下: 引入依赖 <depe ...
- ARTS打卡计划第二周-Review
本周review的文章是:https://medium.com/@hakibenita/optimizing-django-admin-paginator-53c4eb6bfca3 改篇文章的题目是: ...