forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-square-streak-in-an-array.cpp
47 lines (45 loc) · 1.25 KB
/
longest-square-streak-in-an-array.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
// Time: O(nlogn)
// Space: O(n)
// hash table
class Solution {
public:
int longestSquareStreak(vector<int>& nums) {
set<int64_t> sorted_nums(cbegin(nums), cend(nums));
unordered_set<int64_t> squares;
for (const auto& x : sorted_nums) {
if (x % 4 < 2) { // squared_num % 4 in [0, 1]
squares.emplace(x);
}
}
int result = 0;
for (const auto& x : sorted_nums) {
int cnt = 1;
for (int64_t square = x * x; squares.count(square); squares.erase(square), square *= square) {
++cnt;
}
result = max(result, cnt);
}
return result != 1 ? result : -1;
}
};
// Time: O(nlogn)
// Space: O(n)
// dp
class Solution2 {
public:
int longestSquareStreak(vector<int>& nums) {
sort(begin(nums), end(nums));
unordered_map<int, int> dp;
int result = 0;
for (const auto& x : nums) {
const int sqrt_x = static_cast<int>(sqrt(x));
if (sqrt_x * sqrt_x == x) {
dp[x] = dp[sqrt_x] + 1;
} else {
dp[x] = 1;
}
result = max(result, dp[x]);
}
return result != 1 ? result : -1;
}
};