-
Notifications
You must be signed in to change notification settings - Fork 56
/
EquiLeader.py
57 lines (40 loc) · 1.42 KB
/
EquiLeader.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
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
# write your code in Python 3.6
# key point:
# the equi-leader 'must' be leader in A
# step 1: find the leader in A
my_dictionary = {}
for item in A:
my_dictionary[item] = 0
for item in A:
my_dictionary[item] += 1
have_leader = False
leader_value = -1
for item in A:
if my_dictionary[item] > float( len(A) / 2):
have_leader = True
leader_value = item
if have_leader == False:
return 0
# step 2: number of equi-leaders
leader_count = []
num_leader = 0
for item in A:
if item == leader_value:
num_leader += 1
leader_count.append(num_leader)
else:
leader_count.append(num_leader)
# print(leader_count)
num_equi_leader = 0
for index in range( len(A) ):
total_num_leader = leader_count[len(A)-1]
left_num_leader = leader_count[index]
right_num_leader = total_num_leader - left_num_leader
left_leader_threshold = float( (index+1) / 2)
right_leader_threshold = float( (len(A)-index-1) / 2)
if left_num_leader > left_leader_threshold and right_num_leader > right_leader_threshold:
num_equi_leader += 1
return num_equi_leader