zoukankan      html  css  js  c++  java
  • [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 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.

    公平的糖果棒交换。

    爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 根糖果棒的大小,B[j] 是鲍勃拥有的第 j 根糖果棒的大小。

    因为他们是朋友,所以他们想交换一根糖果棒,这样交换后,他们都有相同的糖果总量。(一个人拥有的糖果总量是他们拥有的糖果棒大小的总和。)

    返回一个整数数组 ans,其中 ans[0] 是爱丽丝必须交换的糖果棒的大小,ans[1] 是 Bob 必须交换的糖果棒的大小。

    如果有多个答案,你可以返回其中任何一个。保证答案存在。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/fair-candy-swap
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道数组题,不涉及算法,我介绍两种思路。

    首先是暴力解。首先我们分别计算两个人的糖果数量的总和,然后得到差值diff = aliceSum - bobSum。因为交换之后需要满足

    aliceSum - A[i] + B[j] = bobSum - B[j] + A[i]

    这个等式转化一下得到 aliceSum - bobSum = 2 * (A[i] - B[j])

    两边除以2得到 diff / 2 = A[i] - B[j]

    所以暴力解两重for循环找的就是看哪两个元素满足这个条件。

    时间O(mn)

    空间O(1)

    Java实现

     1 class Solution {
     2     public int[] fairCandySwap(int[] A, int[] B) {
     3         int diff = 0;
     4         int aliceSum = 0;
     5         int bobSum = 0;
     6         for (int i : A) {
     7             aliceSum += i;
     8         }
     9         for (int j : B) {
    10             bobSum += j;
    11         }
    12         diff = (aliceSum - bobSum) / 2;
    13         for (int i : A) {
    14             for (int j : B) {
    15                 if (i - j == diff) {
    16                     return new int[] { i, j };
    17                 }
    18             }
    19         }
    20         return null;
    21     }
    22 }

    其次是用hashset。其实思路还是需要去找到某两个数字满足 diff / 2 = A[i] - B[j]。这里我们用hashset去记录Alice手上所有的糖果数。这样当我们遍历Bob的糖果的时候,我们找hashset中是否存在某个糖果数 = diff + B[j]。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int[] fairCandySwap(int[] A, int[] B) {
     3         int[] res = new int[2];
     4         HashSet<Integer> set = new HashSet<>();
     5         int aliceSum = 0;
     6         int bobSum = 0;
     7         for (int i : A) {
     8             aliceSum += i;
     9             set.add(i);
    10         }
    11         for (int j : B) {
    12             bobSum += j;
    13         }
    14         int diff = (aliceSum - bobSum) / 2;
    15         for (int j : B) {
    16             if (set.contains(j + diff)) {
    17                 res[0] = j + diff;
    18                 res[1] = j;
    19                 break;
    20             }
    21         }
    22         return res;
    23     }
    24 }

    LeetCode 题目总结

  • 相关阅读:
    数据结构 数据结构分类
    数据结构 基本概念(数据项--数据元素--数据对象)
    C++ STL标准模板库(list)
    C++ STL标准模板库(queue)
    python文件操作--字符串替换
    Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
    pyCharm使用
    Python中变量的作用域(variable scope)
    对于python,一切事物都是对象,对象基于类创建
    python主文件判断
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14354955.html
Copyright © 2011-2022 走看看