Skip to content

Commit 093867e

Browse files
authored
Create find-the-maximum-number-of-elements-in-subset.py
1 parent ee8c5de commit 093867e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Time: O(n * log(logr)) = O(9 * n)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
# freq table
8+
class Solution(object):
9+
def maximumLength(self, nums):
10+
"""
11+
:type nums: List[int]
12+
:rtype: int
13+
"""
14+
cnt = collections.Counter(nums)
15+
result = 0
16+
for x in cnt.iterkeys():
17+
if x == 1:
18+
result = max(result, cnt[x]-(1 if cnt[x]%2 == 0 else 0))
19+
continue
20+
l = 0
21+
while x in cnt and cnt[x] >= 2:
22+
l += 2
23+
x *= x
24+
l += 1 if x in cnt else -1
25+
result = max(result, l)
26+
return result

0 commit comments

Comments
 (0)