-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnalyzeUsagePattern.cpp
More file actions
64 lines (64 loc) · 1.92 KB
/
AnalyzeUsagePattern.cpp
File metadata and controls
64 lines (64 loc) · 1.92 KB
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
class Solution {
public:
vector<string> mostVisitedPattern(vector<string>& username, vector<int>& timestamp, vector<string>& website)
{
unordered_map<string, vector<pair<int,string>> >mp;
vector<string>ans;
int i,sz=username.size(),j,k,maxsz = 0;
if(sz==0)
return ans;
string res = "",sol;
for(i=0;i<sz;i++)
mp[username[i]].push_back(make_pair(timestamp[i],website[i]));
unordered_map<string,int>freq;
for(auto it=mp.begin();it!=mp.end();it++)
{
vector<pair<int,string>>temp = it->second;
sort(temp.begin(),temp.end());
sz = temp.size();
unordered_set<string>visited;
for(i=0;i<sz-2;i++)
{
for(j=i+1;j<sz-1;j++)
{
for(k=j+1;k<sz;k++)
{
sol = temp[i].second+" "+temp[j].second+" "+temp[k].second;
if(visited.count(sol) == 0)
{
if(freq.find(sol) == freq.end())
freq[sol] = 1;
else
freq[sol]++;
visited.insert(sol);
}
maxsz = max(maxsz,freq[sol]);
}
}
}
}
for(auto it=freq.begin();it!=freq.end();it++)
{
if(it->second==maxsz)
{
if((res == "") || (res>it->first))
res = it->first;
}
}
sol = "";
sz = res.length();
for(i=0;i<sz;i++)
{
if(res[i]==' ')
{
ans.push_back(sol);
sol="";
}
else
sol=sol+res[i];
}
if(sol!="")
ans.push_back(sol);
return ans;
}
};