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
// @Title: 公平的糖果棒交换 (Fair Candy Swap)
// @Author: 15816537946@163.com
// @Date: 2021-02-01 22:59:42
// @Runtime: 76 ms
// @Memory: 7.4 MB
func fairCandySwap(A []int, B []int) []int {
	var sumA, sumB int
	setA := make(map[int]struct{})
	for _, v := range A {
		sumA += v
		setA[v] = struct{}{}
	}
	for _, v := range B {
		sumB += v
	}

	delta := (sumA - sumB) / 2
	for i := 0; ; i++ {
		y := B[i]
		x := y + delta
		if _, has := setA[x]; has {
			return []int{x, y}
		}
	}
	return []int{}

}