-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path888.py
43 lines (39 loc) · 1.32 KB
/
888.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
__________________________________________________________________________________________________
sample 396 ms submission
class Solution:
def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
x, y = sum(A), sum(B)
z = (x + y) // 2
B = set(B)
for a in A:
b = a + z - x
if b in B:
return [a, b]
__________________________________________________________________________________________________
sample 14536 kb submission
class Solution:
def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
sumA = sum(A)
sumB = sum(B)
d = abs(sumA-sumB)
lenA = len(A)
lenB = len(B)
i = j = 0
A.sort()
B.sort()
while i < lenA and j < lenB:
if sumA < sumB:
if B[j]-A[i]==d/2:
return [A[i], B[j]]
elif B[j]-A[i] > d/2:
i+=1
else:
j+=1
else:
if A[i]-B[j]==d/2:
return [A[i], B[j]]
elif A[i]-B[j] > d/2:
j+=1
else:
i+=1
__________________________________________________________________________________________________