-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCandySwap.go
120 lines (90 loc) · 2.8 KB
/
CandySwap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
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.
*/
package main
import (
"fmt"
)
func main() {
testCases := [][][]int{{{1,1},{2,2}}, {{1,2},{2,3}}, {{2},{1,3}}, {{1,2,5},{2,4}}}
for _, testCase := range testCases {
// fmt.Println("fairCandySwap(", testCase[0], ", ", testCase[1], ") => ", fairCandySwap(testCase[0], testCase[1]))
fmt.Println("fairCandySwapLinear(", testCase[0], ", ", testCase[1], ") => ", fairCandySwapLinear(testCase[0], testCase[1]))
}
}
// per https://leetcode.com/articles/fair-candy-swap/
func fairCandySwapLinear(A []int, B []int) []int {
// original total of Alice
totalAlice := 0
for i := 0; i < len(A); i++ {
totalAlice += A[i]
}
// original total of Bob
totalBob := 0
for i := 0; i < len(B); i++ {
totalBob += B[i]
}
result := []int{}
delta := (totalBob - totalAlice) / 2 // If Alice gives x, she expects to receive x + delta
bMap := map[int]int{}
for _, val := range B {
bMap[val] = val
}
for _, valA := range A {
_, contains := bMap[valA + delta]
if contains {
result = []int{valA, valA + delta}
}
}
return result
}
// quadratic performance
func fairCandySwap(A []int, B []int) []int {
// original total of Alice
totalAlice := 0
for i := 0; i < len(A); i++ {
totalAlice += A[i]
}
// original total of Bob
totalBob := 0
for i := 0; i < len(B); i++ {
totalBob += B[i]
}
// result placeholder
result := []int{}
swapA := 0 // candy value that Alice gives Bob
swapB := 0 // candy value that Bob gives Alice
for i := 0; i < len(A); i++ {
swapA = A[i]
for j := 0; j < len(B); j++ {
swapB = B[j]
// if Alice gives Bob swapA, and Bob gives Alice swapB, would their candy totals equal?
if (totalAlice - swapA + swapB) == (totalBob - swapB + swapA) {
// found a solution
result = []int{swapA, swapB}
}
}
}
return result
}