I like to read this solution as a small machine: keep the useful information, throw away the noise. For 888. Fair Candy Swap, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are fairCandySwap.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(A.length+B.length).
- Space: O(B.length), the space used by setB.
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Alice 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.
03
04Since 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.)
05
06Return 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.
07
08If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.
09
10
11
12Example 1:
13
14Input: A = [1,1], B = [2,2]
15Output: [1,2]
16Example 2:
17
18Input: A = [1,2], B = [2,3]
19Output: [1,2]
20Example 3:
21
22Input: A = [2], B = [1,3]
23Output: [2,3]
24Example 4:
25
26Input: A = [1,2,5], B = [2,4]
27Output: [5,4]
28
29
30Note:
31
321 <= A.length <= 10000
331 <= B.length <= 10000
341 <= A[i] <= 100000
351 <= B[i] <= 100000
36It is guaranteed that Alice and Bob have different total amounts of candy.
37It is guaranteed there exists an answer.
38**/
39
40/**
41Complexity Analysis
42
43Time Complexity: O(A.length+B.length).
44
45Space Complexity: O(B.length), the space used by setB.
46(We can improve this to min(A.length,B.length) by using an if statement.)
47**/
48
49//Runtime: 552 ms, faster than 30.71% of C++ online submissions for Fair Candy Swap.
50//Memory Usage: 12.6 MB, less than 91.78% of C++ online submissions for Fair Candy Swap.
51class Solution {
52public:
53 vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
54 int bSum = accumulate(B.begin(), B.end(), 0);
55 int half = (accumulate(A.begin(), A.end(), 0) + \
56 accumulate(B.begin(), B.end(), 0))/2;
57
58 for(int i = 0; i < A.size(); i++){
59 int toFind = bSum + A[i] - half;
60 vector<int>::iterator it = find(B.begin(), B.end(), toFind);
61 if(it!=B.end()){
62 return vector<int> {A[i], *it};
63 }
64 }
65
66 return vector<int> {0,0};
67 }
68};
Cost