LeetCode 888 Fair Candy Swap 解题报告

时间:2022-09-20 17:17:46

题目要求

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.

题目分析及思路

给定两组整数数组,分别代表两个人所拥有的糖果长度。要求经过一次交换后,两人所拥有的糖果长度相等。可以先将两人要交换的糖果长度分别设为x和y,然后求解。根据得到的结果遍历数组即可。

python代码

class Solution:

def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:

s_a, s_b = sum(A), sum(B)

set_b = set(B)

for x in A:

if x + (s_b-s_a) // 2 in set_b:

return [x, x+(s_b-s_a) // 2]

LeetCode 888 Fair Candy Swap 解题报告的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 【Leetcode_easy】888. Fair Candy Swap

    problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...

  4. 888&period; Fair Candy Swap&commat;python

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  5. 【LeetCode】888&period; Fair Candy Swap 公平的糖果棒交换(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...

  6. &lbrack;LeetCode&amp&semi;Python&rsqb; Problem 888&period; 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 ...

  7. 【LeetCode】723&period; Candy Crush 解题报告 &lpar;C&plus;&plus;&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  8. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  9. 【LeetCode】376&period; Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

随机推荐

  1. 【java】:读取文件

    PS:转 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制 ...

  2. Let the Balloon Rise 分类: HDU 2015-06-19 19&colon;11 7人阅读 评论&lpar;0&rpar; 收藏

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. 慕课网-安卓工程师初养成-3-6 Java中的逻辑运算符

    来源: http://www.imooc.com/code/1301 逻辑运算符主要用于进行逻辑运算.Java 中常用的逻辑运算符如下表所示: 我们可以从“投票选举”的角度理解逻辑运算符: 1. 与: ...

  4. &lbrack;wikioi&rsqb;线段覆盖

    http://wikioi.com/problem/1214/ 这道题也归为贪心了.我也不是很能分辨,但想法确实是:1.有阶段最优化性:2.前一状态和后一状态有关系. 想法:1.排个序是很自然的想法, ...

  5. BZOJ 1008 &lbrack;HNOI2008&rsqb;越狱

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5166  Solved: 2242[Submit][Status] ...

  6. BZOJ 1648&colon; &lbrack;Usaco2006 Dec&rsqb;Cow Picnic 奶牛野餐&lpar; dfs &rpar;

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

  7. hao947 &colon; Mybatis resultMap配置插入和主键自增返回 &colon; 好947

    映射配置文件  好947  <!-- type:映射实体类的数据类型 id:resultMap的唯一标识 -->  <resultMap type="person&quot ...

  8. activiti 5&period;13 使用activiti设置用户组任务的 工作流的角色

    1.设置activiti 流程引擎的用户,组别,关系/**在部署流程定义和启动流程实例的中间,设置组任务的办理人,向Activiti表中存放组和用户的信息*/ IdentityService iden ...

  9. c&num; Exchange 收件箱获取。

    public List<Email> GetInbox() { try { List<Email> lstEmails = new List<Email>(); F ...

  10. 基于上三角变换或基于DFS的行(列)展开的n阶行列式求值算法分析及性能评估

    进入大一新学期,看完<线性代数>前几节后,笔者有了用计算机实现行列式运算的想法.这样做的目的,一是巩固自己对相关概念的理解,二是通过独立设计算法练手,三是希望通过图表直观地展现涉及的两种算 ...