Accounts Merge LeetCode solution in Python

1
class Solution:
2
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
3
def merge(accounts):
4
res = []
5
ls = []
6
for i in range(len(accounts)):
7
temp = list(set(accounts[i][1:]))
8
temp.sort()
9
temp = [accounts[i][0]] + temp
10
if i in ls:
11
continue
12
for j in range(len(accounts[i:])):
13
s = i + j
14
if i == s:
15
continue
16
if accounts[i][0] != accounts[s][0]:
17
continue
18
temp3 = list(set(accounts[s][1:]))
19
uni = list(set(temp[1:]) | set(temp3))
20
if len(uni) < len(temp[1:]) + len(temp3):
21
temp1 = list(set(temp[1:]) | set(temp3))
22
temp = [temp[0]] + temp1
23
ls.append(s)
24
temp0 = temp[1:]
25
temp0.sort()
26
temp = [temp[0]] + temp0
27
res.append(temp)
28
return res
29

30
merged = merge(accounts)
31
while merge(merged) != merged:
32
merged = merge(merged)
33

34
return merged

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0