forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze-user-website-visit-pattern.cpp
53 lines (50 loc) · 1.85 KB
/
analyze-user-website-visit-pattern.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
// Time: O(n^3)
// Space: O(n^3)
class Solution {
public:
vector<string> mostVisitedPattern(vector<string>& username, vector<int>& timestamp, vector<string>& website) {
vector<tuple<int, string, string>> A;
for (int i = 0; i < username.size(); ++i) {
A.emplace_back(timestamp[i], username[i], website[i]);
}
sort(A.begin(), A.end());
unordered_map<string, vector<string>> users;
for (const auto& [t, u, w] : A) {
users[u].emplace_back(w);
}
vector<string> result;
unordered_map<vector<string>, int, VectorHash<string>> count;
for (const auto& [u, webs] : users) {
unordered_set<vector<string>, VectorHash<string>> lookup;
for (int i = 0; i + 2 < webs.size(); ++i) {
for (int j = i + 1; j + 1 < webs.size(); ++j) {
for (int k = j + 1; k < webs.size(); ++k) {
vector<string> pattern = {webs[i], webs[j], webs[k]};
if (lookup.count(pattern)) {
continue;
}
lookup.emplace(pattern);
++count[pattern];
if (result.empty() ||
count[pattern] > count[result] ||
(count[pattern] == count[result] && pattern < result)) {
result = pattern;
}
}
}
}
}
return result;
}
private:
template<typename T>
struct VectorHash {
size_t operator()(const std::vector<T>& v) const {
size_t seed = 0;
for (const auto& i : v) {
seed ^= std::hash<T>{}(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
return seed;
}
};
};