LeetCode.888- candy fair exchange (Fair Candy Swap)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/codeMas/article/details/90710087

This is the first book of pleasure and delight 339 update, the first 363 Pian original

01 questions and look ready

LeetCode algorithm introduced today is the first title in the Easy level 208 title (overall title number is 888). Alice and Bob have different sized candy bars: A [i] is the size of the i-th Alice has a candy bar, B [j] is the size of the j-th Bob has a candy bar.

Because they are friends, they want to exchange a candy for the exchange, they all have the same amount of candy. (Total amount of candy a person has is the sum of the size of the candy they have.)

It returns an array of integers ans, wherein ans [0] is the size of the candy must be exchanged Alice, ans [1] is the size of candy Bob must be exchanged.

If there are multiple answers, you can return to any of them. The answer guarantee exists. E.g:

Input: A = [1,1], B = [2,2]
Output: [1,2]

Input: A = [1,2], B = [2,3]
Output: [1,2] [2,3]

Input: A = [2], B = [1,3]
Output: [2,3]

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

  • To ensure that different amount of candy Alice and Bob.

  • Ensure that there is an answer.

The use of problem-solving tools that eclipse, jdk version used is 1.8, the environment is win7 64-bit systems, and test using the Java language.

02 The first solution

The total amount of candy Alice and Bob are not the same, in order to answer certainly exist, the difference between the total amount of candy is even sure both of them, only the difference is even, the difference divided equally get an average, more than the total amount of candy subtracting the average of the party, one of the few plus average amount of candy this time the two are equal.

So we simply requires an average difference of the total candy two, plus either take this average size of a candy, candy in addition to matching the size of the one in the can matched, indicating that these two the two sides exchanged candy is a need, in this scenario, this question is somewhat similar to two Sum.

public int[] fairCandySwap(int[] A, int[] B) {
    int[] result = new int[2];
    // 存储A中的元素(换成B也行)
    Set<Integer> set = new HashSet<Integer>();
    int sumA = 0;
    for (int a : A) {
        sumA += a;
        set.add(a);
    }
    int sumB = 0;
    for (int b : B) {
        sumB += b;
    }
    // 两人糖果总量之差的平均数
    int num = (sumA-sumB)/2;
    // 遍历B中元素,加上差值平均数去另外一个数组里匹配,
    // 能匹配上,表明遇到了需要交换的元素。
    for (int b : B) {
        if (set.contains(b+num)) {
            return new int[]{b+num, b};
        }
    }
    return result;
}

03 The second solution

HashSet first solution for use, we can also into another data structure instead of using a boolean array to store the value of A, the other ideas are the same.

public int[] fairCandySwap2(int[] A, int[] B) {
    int[] result = new int[2];
    boolean[] set = new boolean[100001];
    int sumA = 0;
    for (int a : A) {
        sumA += a;
        set[a] = true;
    }
    int sumB = 0;
    for (int b : B) {
        sumB += b;
    }
    int num = (sumA-sumB)/2;
    for (int b : B) {
        if (b+num > 0 && b+num < 100001 && set[b+num]) {
            return new int[]{b+num, b};
        }
    }
    return result;
}

04 Summary

Thematic algorithm has been continuous days more than six months , the algorithm of feature articles 208 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures ] either a keyword to obtain a series of articles Collection .

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!

Guess you like

Origin blog.csdn.net/codeMas/article/details/90710087