Skip to content

Commit d6f5afe

Browse files
authored
Create maximum-equal-frequency.py
1 parent 98365ad commit d6f5afe

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Python/maximum-equal-frequency.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def maxEqualFreq(self, nums):
9+
"""
10+
:type nums: List[int]
11+
:rtype: int
12+
"""
13+
result = 0
14+
count = collections.Counter()
15+
freq = [0 for _ in xrange(len(nums)+1)]
16+
for i, n in enumerate(nums, 1):
17+
freq[count[n]] -= 1
18+
freq[count[n]+1] += 1
19+
count[n] += 1
20+
c = count[n]
21+
if freq[c]*c == i and i < len(nums):
22+
result = i+1
23+
remain = i-freq[c]*c
24+
if freq[remain] == 1 and remain in [1, c+1]:
25+
result = i
26+
return result

0 commit comments

Comments
 (0)