-
Notifications
You must be signed in to change notification settings - Fork 0
/
RankTeamsByVotes.py
58 lines (45 loc) · 1.63 KB
/
RankTeamsByVotes.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
class Solution:
def compare_rank(self, a, b, a_char, b_char):
for i, j in zip(a, b):
if i > j:
return True
elif i < j:
return False
if a_char > b_char:
return False
elif a_char < b_char:
return True
return True
def quickSort(self, arr, rank, begin, end):
if begin >= end:
return
left = begin
right = end
pivot = arr[left]
pivot_rank = rank[left]
while left < right:
while left < right and self.compare_rank(rank[right], pivot_rank, arr[right], pivot):
right -= 1
arr[left] = arr[right]
rank[left] = rank[right]
while left < right and self.compare_rank(pivot_rank, rank[left], pivot, arr[left]):
left += 1
arr[right] = arr[left]
rank[right] = rank[left]
arr[left] = pivot
rank[left] = pivot_rank
self.quickSort(arr, rank, begin, left-1)
self.quickSort(arr, rank, left+1, end)
return
def rankTeams(self, votes: List[str]) -> str:
num_char = len(votes[0])
rank = [[0] * num_char for i in range(num_char)]
for i in votes:
for j in range(num_char):
idx = votes[0].index(i[j])
rank[idx][j] += 1
ans = list(votes[0])
self.quickSort(ans, rank, 0, num_char-1)
ans.reverse()
ans_str = ''.join(ans)
return ans_str