-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2501.cpp
37 lines (34 loc) · 849 Bytes
/
2501.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
#include "leetcode.hpp"
class Solution {
public:
int longestSquareStreak(vector<int>& nums) {
map<unsigned long long, int> hash;
sort(nums.begin(), nums.end());
auto ut = unique(nums.begin(), nums.end());
nums.resize(ut - nums.begin());
int streak = -1;
for (auto nn : nums) {
unsigned long long n = nn;
auto it = hash.find(n);
if (it == hash.end()) {
hash[n * n]++;
} else {
hash[n * n] = it->second + 1;
if (it->second + 1 >= 2) {
streak = max(streak, it->second + 1);
}
hash.erase(it);
}
}
return streak;
}
};
int main(int argc, char const* argv[]) {
auto tc =
vector<vector<int>>{{2, 4, 4, 2}, {4, 3, 6, 16, 8, 2}, {2, 3, 5, 6, 7}};
Solution s;
for (auto c : tc) {
s.longestSquareStreak(c);
}
return 0;
}