每日一道Leetcode算法——Fair Candy Swap——2019.02.03

题干:

中文:

Alice和Bob有不同大小的糖果棒:A [i]是Alice拥有的第i个糖果棒的大小,B [j]是Bob拥有的第j个糖果棒的大小。

由于他们是朋友,他们想交换一个糖果,以便交换后,他们都有相同的糖果总量。 (一个人拥有的糖果总量是他们拥有的糖果大小的总和。)

返回一个整数数组ans,其中ans [0]是Alice必须交换的直板的大小,ans [1]是Bob必须交换的直板的大小。

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

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
  • 保证爱丽丝和鲍勃的糖果总量不同。
  • 保证有答案。

英文:

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.

解题思路:

先算出目前Alice和Bob糖果的总数,

然后双重循环,依次交换Alice和Bob手里的糖果,直到双方糖果总数相同,返回交换的糖果。

package cn.leetcode.easy;

import java.util.Arrays;

/**
 * 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.
 *
 * @author kimtian
 * @date 2019.02.03
 * @num 888
 */
public class FairCandySwap {
    /**
     * 先算出目前Alice和Bob糖果的总数,
     * 然后双重循环,依次交换Alice和Bob手里的糖果,
     * 直到双方糖果总数相同,返回交换的糖果
     *
     * @param A Alice手里的糖果
     * @param B Bob手里的糖果
     * @return 可以交换的糖果
     */
    public static int[] fairCandySwap(int[] A, int[] B) {
        //定义一个返回交换结果的数组
        int[] a = new int[2];
        //计算Alice最初拥有的糖果的总数
        int sumA = 0;
        for (int i : A) {
            sumA += A[i];
        }
        //计算Bob最初拥有的糖果的总数
        int sumB = 0;
        for (int i : B) {
            sumB += B[i];
        }
        //循环Alice手里的糖果
        for (int i = 0; i < A.length; i++) {
            //循环Bob手里的糖果
            int j = 0;
            while (j < B.length) {
                //如果将Alice和Bob手里的糖果交换,两人手里的糖果数相同,则记录为true
                boolean flag = sumA - A[i] + B[j] == sumB - B[j] + A[i];
                //并将当前结果返回,终止循环
                if (flag) {
                    a[0] = A[i];
                    a[1] = B[j];
                    return a;
                }
                //否则继续尝试交换糖果
                else {
                    j++;
                }
            }
        }
        //如果没有合适的则返回空数组
        return a;
    }

    /**
     * 测试类
     *
     * @param args
     */
    public static void main(String[] args) {
        int a[] = new int[]{2};
        int b[] = new int[]{1, 3};
        System.out.println(Arrays.toString(fairCandySwap(a, b)));
    }
}

猜你喜欢

转载自blog.csdn.net/third_/article/details/87275692