-
Notifications
You must be signed in to change notification settings - Fork 43
/
fair-candy-swap.py
63 lines (60 loc) · 1.53 KB
/
fair-candy-swap.py
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
# V0
# V1
# https://www.jiuzhang.com/solution/fair-candy-swap/#tag-highlight-lang-python
class Solution:
"""
@param A: an array
@param B: an array
@return: an integer array
"""
def fairCandySwap(self, A, B):
# Write your code here.
ans = []
sumA = sum(A)
sumB = sum(B)
A.sort()
B.sort()
tmp = sumA - (sumA + sumB) / 2
i = 0
j = 0
while i < len(A) and j < len(B):
if A[i] - B[j] == tmp:
ans.append(A[i])
ans.append(B[j])
break
elif A[i] - B[j] > tmp:
j += 1
elif A[i] - B[j] < tmp:
i += 1
return ans
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/82013077
class Solution(object):
def fairCandySwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
sum_A, sum_B, set_B = sum(A), sum(B), set(B)
target = (sum_A + sum_B) / 2
for a in A:
b = target - (sum_A - a)
if b >= 1 and b <= 100000 and b in set_B:
return [a, b]
# V2
# Time: O(m + n)
# Space: O(m + n)
class Solution(object):
def fairCandySwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: List[int]
"""
diff = (sum(A)-sum(B))//2
setA = set(A)
for b in set(B):
if diff+b in setA:
return [diff+b, b]
return []