[LeetCode&Python] Problem 888. Fair Candy Swap
2024-08-30 08:01:41
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 <= 10000
1 <= B.length <= 10000
1 <= A[i] <= 100000
1 <= B[i] <= 100000
- It is guaranteed that Alice and Bob have different total amounts of candy.
- It is guaranteed there exists an answer.
class Solution(object):
def fairCandySwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
diff=(sum(A)-sum(B))/2
A=set(A)
for b in set(B):
if b+diff in A:
return b+diff, b
最新文章
- Spark概述
- SQLSERVER中的假脱机spool
- C++线程中packaged_tack
- JavaScript中对象的比较
- HTML5适合移动应用开发的几大特性
- 【转】maven导出项目依赖的jar包
- Java静态代码分析工具——FindBugs插件的安装与使用
- HttpContext.Current.Cache在控制台下不工作
- winform 发邮件
- 虚幻4以及DX12将允许开发者利用Xbox One的更多性能(转)
- cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient
- error: dst ref refs/heads/zhCN_v0.13.1 receives from more than one src.
- Hibernate从入门到精通(六)一对一双向关联映射
- webApp添加到iOS桌面
- 细聊 Cocoapods 与 Xcode 工程配置
- 语音频谱语音信号处理之(四)梅尔频率倒谱系数(MFCC)
- 使用grunt-init自动创建gruntfile.js和package.json文件
- centos 7安装mysql 执行./scripts/mysql_install_db --user=mysql 报错 FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db: Data::Dumper
- 架构(四)Git简介,安装以及相关命令SourceTree
- Latex(表格|图片(一丢丢))