-
Notifications
You must be signed in to change notification settings - Fork 112
/
1152-AnalyzeUserWebsiteVisitPattern.cs
64 lines (57 loc) · 2.47 KB
/
1152-AnalyzeUserWebsiteVisitPattern.cs
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
//-----------------------------------------------------------------------------
// Runtime: 304ms
// Memory Usage: 35 MB
// Link: https://leetcode.com/submissions/detail/365336197/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _1152_AnalyzeUserWebsiteVisitPattern
{
public IList<string> MostVisitedPattern(string[] username, int[] timestamp, string[] website)
{
var tuples = new List<(string n, int t, string w)>();
for (int i = 0; i < username.Length; i++)
tuples.Add((username[i], timestamp[i], website[i]));
var userVisit = new Dictionary<string, IList<string>>();
foreach (var tuple in tuples.OrderBy(t => t.t))
{
if (!userVisit.ContainsKey(tuple.n))
userVisit[tuple.n] = new List<string>();
userVisit[tuple.n].Add(tuple.w);
}
var counts = new Dictionary<string, int>();
var maxCount = int.MinValue;
var resultStr = string.Empty;
foreach (var visit in userVisit.Values)
{
if (visit.Count < 3) continue;
var visitSet = new HashSet<string>();
for (int i = 0; i < visit.Count; i++)
for (int j = i + 1; j < visit.Count; j++)
for (int k = j + 1; k < visit.Count; k++)
{
var str = $"{visit[i]} {visit[j]} {visit[k]}";
if (!visitSet.Contains(str))
{
visitSet.Add(str);
if (!counts.ContainsKey(str))
counts[str] = 1;
else
counts[str]++;
}
if (maxCount < counts[str])
{
maxCount = counts[str];
resultStr = str;
}
else if (maxCount == counts[str] && str.CompareTo(resultStr) < 0)
resultStr = str;
}
}
var result = resultStr.Split(' ');
return result.Length < 3 ? new List<string>() : result.ToList();
}
}
}