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 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.
原题地址: Fair Candy Swap
题意: 给两个数组A B,各选取一个数进行交换,使得数组A的和等于数组B的和,返回交换的两个数
例子:
Input: A = [1,1], B = [2,2]
Output: [1,2] # 交换之后 A = [1,2] B = [1, 2]
(1)求出两个数组的平均值,也就是最后两个数组的和
(2)遍历数组A,假设当前值是要交换的数A[i],那么数组B中要交换的数是 平均值 - (sum(A) - A[i]) , 并判断此数是否在数组B中.如果存在,返回这两个数
注意: 可能存在重复值,是否存在重复值对结果没有影响,为了减少遍历次数,可以用 set() 去重,并且获取元素的时间复杂度是O(1)
代码:
class Solution(object):
def fairCandySwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
a, b , set_b = sum(A), sum(B), set(B)
mean = (a + b) / 2
for num in A:
target = mean - (a - num)
if 1<= target <= 100000 and target in set_b:
return [num, target]
时间复杂度:O(n)
遍历数组A为n次,判断一个数是否在set(B)中,需要1次,
空间复杂度:list转化为set,为O(n)
888. Fair Candy Swap@python的更多相关文章
- 【Leetcode_easy】888. Fair Candy Swap
problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...
- 【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 解题报告
题目要求 Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy tha ...
- [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(C++)
题目: Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that ...
- [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 ...
- 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 Ali ...
- LeetCode.888-公平的糖果交换(Fair Candy Swap)
这是悦乐书的第339次更新,第363篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第208题(顺位题号是888).Alice和Bob有不同大小的糖果棒:A[i]是Alic ...
随机推荐
- 所读STL文章摘要集结
在网上看了一些STL相关文章,这里将它们的摘要进行集结,方便以后查找. 1.黄常标,林俊义,江开勇.快速成形中STL文件拓扑信息的快速建立.2004 摘要:在分析现有建立拓扑信息方法的基础上,提出基于 ...
- IOS高级开发~Runtime(一)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...
- LuoguP2700逐个击破【并查集/生成树/正难则反】By cellur925
题目传送门 题目大意:给你一棵树,求把其中k个点相互隔离(不连通)所需要的边权代价. 这题我开始是想要求出把k个点联通的最小代价的,但后来发现还是实现起来比较困难,题解里貌似也没有这种做法,于是就鸽了 ...
- 跟我一起玩Win32开发(22):抓取屏幕
关于如何拷贝屏幕并保存,这里已经有现成的例子,我也不必去Copy人家了,我一向不喜欢Copy.这里有一个完整的例子,可以看看. http://msdn.microsoft.com/EN-US/libr ...
- 使用c++的一些建议
1: 不要使用宏,用const或enum定义常量 用inline避免函数的额外调用(使用inline的函数,块里面尽量不要使用循环和递归) 用template去荷花一些函数或者类型 用namespac ...
- 【Helvetic Coding Contest 2018】B2. Maximum Control (medium)
Description 传送门(翻译就别想了,本人英语太垃圾) Solution 设ans[i]为设置i个船时能控制的最多星球数(看到这你可能因为是dp,然而我可以很负责地告诉你是假的) 首先一个显然 ...
- A Dangerous Maze LightOJ - 1027
这题意真是... 题意:你在一个迷宫里,有一些门,每个门有一个参数x,如果为正表明你进入门后可以花x的时间出去,如果为负表明你进入门后可以花-x的时间回到出发的地方.每次回到出发的地方之后,不能记得之 ...
- 题解报告:poj 2480 Longge's problem(欧拉函数)
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...
- Android的代码适配方案
public class DensityUtil { private DensityUtil(){ throw new AssertionError(); } /** * dp转px * @param ...
- RHEL 6.5----apr-util1.6执行make时报错
报错信息 ]: Entering directory `/usr/local/src/apr-util-' /bin//build-/libtool --silent --mode=compile / ...