SoFunction
Updated on 2025-03-05

Detailed explanation of Go language problem solution LeetCode888 fair candy exchange example

1 Description

888. Fair Candy Exchange - LeetCode ()

Alice and Bob have different total amounts of candy. Give you two arraysaliceSizesandbobSizesaliceSizes[i]Alice ownediThe amount of candy in the box,bobSizes[j]Bob owns the firstjThe amount of candy in the box.

The two want to exchange a box of candies with each other so that after the exchange they can have the same total amount of candies. The total amount of candy a person has is the sum of the amount of candy they can be per box.

Return an array of integersanswer,inanswer[0]It is the number of candies in the candy box that Alice must exchange,answer[1]It is the number of candy in the candy box that Bob must exchange. If there are multiple answers, you can return itAny. The test cases of the question ensure that there are answers corresponding to the input.

Example 1:

enter:aliceSizes = [1,1], bobSizes = [2,2]
Output:[1,2]

Example 2:

enter:aliceSizes = [1,2], bobSizes = [2,3]
Output:[1,2]

Example 3:

enter:aliceSizes = [2], bobSizes = [1,3]
Output:[2,3]

Example 4:

enter:aliceSizes = [1,2,5], bobSizes = [2,4]
Output:[5,4]

hint:

1 <= , <= 10^4

1 <= aliceSizes[i], bobSizes[j] <= 10^5

Alice and Bob have different total candies.

The question data ensures that there is at least one valid answer for a given input.

Two Analysis

Calculate the size of candy required by A when A is the same as B, traverse B, and find the candy

  • First calculate the sum of candies between A and B
  • The required candy difference for equality of A and B is (sum_B-sum_a)/2
  • Build a hash table, with each element of A, plus the difference of 2
  • Iterate through B and find out if there is a candy in B equal to a certain value in the hash table.
  • Output value

Three Answers

class Solution {
public:
    vector&lt;int&gt; fairCandySwap(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
        int sum_A=0;//1 First calculate the sum of candies between A and B        int sum_B=0;
        for(auto a:A)
        {
            sum_A=sum_A+a;
        }
        for(auto b:B)
        {
            sum_B=sum_B+b;
        }
        unordered_map&lt;int,int&gt; map;//2 The required candy difference for equality of A and B is (sum_B-sum_a)/2        for(auto a:A)//3 Build a hash table, the hash table is each element of A, plus the difference of 2        {
            map[a+(sum_B-sum_A)/2];
        }
        for(auto b:B)//4 traverse B and find out if there is a candy in B equal to a certain value in the hash table        {
            if((b)!=())
            {
                return{b+(sum_A-sum_B)/2,b};//5 Output value            }
        }
        return {};
    }
};

The above is the detailed explanation of the Go language problem solution LeetCode888 fair candy exchange example. For more information about Go problems solution Fair candy exchange, please pay attention to my other related articles!