[LeetCode] Analyze User Website Visit Pattern

1152. Analyze User Website Visit Pattern

We are given some website visits: the user with name username[i] visited the website website[i] at time timestamp[i].

A 3-sequence is a list of websites of length 3 sorted in ascending order by the time of their visits. (The websites in a 3-sequence are not necessarily distinct.)

Find the 3-sequence visited by the largest number of users. If there is more than one solution, return the lexicographically smallest such 3-sequence.

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
class Solution {
public:
vector<string> mostVisitedPattern(vector<string>& u, vector<int>& t, vector<string>& w) {
int sz = u.size();
unordered_map<string, map<int, string>> m;
map<vector<string>, int> mm;
for(int i = 0; i < sz; i++) {
m[u[i]][t[i]] = w[i];
}
for(auto& user : m) {
set<vector<string>> s;
for(auto i = begin(user.second); i != end(user.second); i++) {
for(auto j = next(i); j != end(user.second); j++) {
for(auto k = next(j); k != end(user.second); k++) {
s.insert({i->second, j->second, k->second});
}
}
}
for(auto vec : s) {
mm[vec]++;
}
}

return max_element(begin(mm), end(mm), [](const auto& a, const auto& b) { return a.second == b.second ? a.first > b.first : a.second < b.second; })->first;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/11/PS/LeetCode/analyze-user-website-visit-pattern/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.