-
Notifications
You must be signed in to change notification settings - Fork 10
/
leader.rb
102 lines (92 loc) · 1.61 KB
/
leader.rb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
A = [4, 6, 6, 6, 6, 8, 8]
n = 7
size =
value = 6
k = 5
A[k] = 8
# def hash_leader(n)
# freq = Hash.new(0)
# n.each do |num|
# freq[num] += 1
# end
# max_freq = freq.values.max
# if max_freq > n.size / 2
# return freq.key(max_freq)
# end
# end
# O(n*n)
def slow_leader(n)
length = n.size
leader = -1
n.each do |candidate|
count = 0
n.each do |num|
count += 1 if num == candidate
end
leader = candidate if count > length/2
end
leader
end
# O(n log n) because sort method is O(n log n)
def fast_leader(n)
n.sort!
length = n.size
leader = -1
freq = 0
candidate = n[length / 2]
n.each do |num|
freq += 1 if num == candidate
leader = candidate if freq > length / 2
end
leader
end
# O(n)
def golden_leader(n)
stack = []
leader = -1
stack_i = 0
n.each do |num|
stack << num
if stack_i == 0
stack_i += 1
next
end
if stack[stack_i] != stack[stack_i - 1]
stack.pop(2)
stack_i -= 2
end
stack_i += 1
end
candidate = stack.last
freq = 0
n.each do |num|
freq += 1 if num == candidate
leader = candidate if freq > n.size / 2
end
leader
end
p golden_leader(n)
Python solution from PDF:
def goldenLeader(A):
n = len(A)
size = 0
for k in xrange(n):
if (size == 0):
size += 1
value = A[k]
else:
if (value != A[k]):
size -= 1
else:
size += 1
candidate = -1
if (size > 0):
candidate = value
leader = -1
count = 0
for k in xrange(n):
if (A[k] == candidate):
count += 1
if (count > n // 2):
leader = candidate
return leader