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 ...
随机推荐
- 洛谷 - P2444 - 病毒 - AC自动机
https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using na ...
- (水题)洛谷 - P1003 - 铺地毯
https://www.luogu.org/problemnew/show/P1003 一开始觉得是用树套树来区间修改单点查询,但是发现空间不够开. 看了题解发现这个是静态的问题,而且只问一个点的结果 ...
- 用动态链表high-poj 1528
//2333333 题目超级水,但是!刚学了链表拿来high一high也不错啊. #include <iostream> #include <stdio.h> #include ...
- sql server通过脚本进行数据库压缩全备份的方法
问题:生产环境的数据库可能比较大,如果直接进行全备而不压缩的话,备份集就会占用了大量磁盘空间.给备份文件的存放管理带来不便. 解决方案:通过with compression显式启用备份压缩,指定对此备 ...
- python 高阶函数一 概念
一.2个概念: 1.变量可以指向函数本身 >>> abs <built-in function abs> >>> f = abs >>> ...
- 第三章 Python运算符
1.算数运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 31 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 * 乘 - 两个数相乘或是返回一个被重复若干 ...
- bzoj 5015 [Snoi2017]礼物
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=5015 题解 首先把k=1,k=2,k=3的手推一遍 然后发现一些规律 就是数列可以表示成$a ...
- _bzoj1029 [JSOI2007]建筑抢修【贪心 堆】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1029 经典的贪心问题,不解释. #include <cstdio> #inclu ...
- python_9(模块补充)
第1章 re模块补充 1.1 贪婪匹配:回溯算法 1.2 .*?的用法 1.3 例:分组<name>取值 1.4 匹配整数删除小数 1.5 正则测试地址 第2章 重点模块 2.1 hash ...
- [转]Android专家级别的面试总结
Android专家级别的面试总结 2017年02月15日 16:56:28 阅读数:1225 1.. 自定义View流程 onMeasure, onLayout, onDraw, 采用深度优先,因为必 ...