-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-no6254.py
71 lines (66 loc) · 1.95 KB
/
leetcode-no6254.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
64
65
66
67
68
69
70
71
from configparser import ConfigParser
class Solution2(object):
def dividePlayers(self, skill):
"""
:type skill: List[int]
:rtype: int
"""
average = sum(skill)/len(skill)*2
print(average)
team = []
copy = skill[:]
for i in skill:
if average-i in copy:
team += [[i,average-i]]
copy.remove(average - i)
if i in copy:
copy.remove(i)
else:
return -1
print(team)
if not len(team) == len(skill)//2:
return -1
return int(sum(map(lambda x:x[0]*x[1],team)))
class Solution3:
def dividePlayers(self, skill: list[int]) -> int:
average = sum(skill) / len(skill) * 2
result = 0
copy = skill[:]
copy2 = skill[:]
times = 0
for i in skill:
if not average-i in skill:
return -1
for i in skill:
mid = average-i
if mid in copy and i in copy:
result += i*(mid)
copy.remove(mid)
copy.remove(i)
times += 1
if not times == len(skill) // 2:
return -1
return int(result)
class Solution:
def dividePlayers(self, skill: list[int]) -> int:
skill.sort()
length = len(skill)
half = length//2
average = sum(skill) / half
for i in range(half):
if not skill[i] + skill[length-i-1] == average:
return -1
result = 0
for i in range(half):
result += skill[i] * skill[length-i-1]
return result
CONFIGFILE = "tests.txt"
config = ConfigParser()
config.read(CONFIGFILE)
h = eval(config.get("threesum",'test3'))
t = Solution()
print(t.dividePlayers([3,2,5,1,3,4]))
print(t.dividePlayers([3,4]))
print(t.dividePlayers([1,1,2,3]))
print(t.dividePlayers([3,2,5,1,3,4]))
print(t.dividePlayers(h))