-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-the-maximum-number-of-elements-in-subset.cpp
67 lines (65 loc) · 1.75 KB
/
find-the-maximum-number-of-elements-in-subset.cpp
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
// Time: O(n)
// Space: O(n)
// freq table, dp
class Solution {
public:
int maximumLength(vector<int>& nums) {
unordered_map<int64_t, int> cnt;
for (const auto& x : nums) {
++cnt[x];
}
unordered_map<int64_t, int> dp;
int result = 0;
for (const auto& k_v : cnt) {
int64_t x = k_v.first;
if (x == 1) {
result = max(result, cnt[x] - (cnt[x] % 2 == 0 ? 1 : 0));
continue;
}
vector<int> stk;
for (; !dp.count(x) && cnt.count(x) && cnt[x] >= 2; x *= x) {
stk.emplace_back(x);
}
if (!dp.count(x)) {
if (!cnt.count(x)) {
x = stk.back(); stk.pop_back();
}
dp[x] = 1;
}
int l = dp[x];
while (!empty(stk)) {
l += 2;
dp[stk.back()] = l; stk.pop_back();
}
result = max(result, l);
}
return result;
}
};
// Time: O(n * log(logr)) = O(9 * n)
// Space: O(n)
// freq table
class Solution2 {
public:
int maximumLength(vector<int>& nums) {
unordered_map<int64_t, int> cnt;
for (const auto& x : nums) {
++cnt[x];
}
int result = 0;
for (const auto& k_v : cnt) {
int64_t x = k_v.first;
if (x == 1) {
result = max(result, cnt[x] - (cnt[x] % 2 == 0 ? 1 : 0));
continue;
}
int l = 0;
for (; cnt.count(x) && cnt[x] >= 2; x *= x) {
l += 2;
}
l += cnt.count(x) ? 1 : -1;
result = max(result, l);
}
return result;
}
};