-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze_user_web_pattern.py
49 lines (33 loc) · 1.3 KB
/
analyze_user_web_pattern.py
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
"""
URL - https://leetcode.com/problems/analyze-user-website-visit-pattern/
Status - REDO - short-term
Could not come up with a solution within 15 mins. Looked at other submissions to get an idea.
Need to practice more on Array + Hash Table + Graph problems.
Time Complexity = O(N log(N))
Space Complexity = O(N)
where N is the number of records
Time taken - 40 mins
"""
import collections
import itertools
from typing import List
class Solution:
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
graph = collections.defaultdict(list)
for t, u, w in sorted(zip(timestamp, username, website)):
graph[u].append(w)
seen_combs = collections.Counter()
for u, route in graph.items():
for combination in set(itertools.combinations(route, 3)):
seen_combs[combination] += 1
pattern = None
max_count = 0
for comb, count in seen_combs.items():
# highest count combination is picked
if count > max_count:
pattern = comb
max_count = count
# if tie then pick lexicographically smaller combination
elif count == max_count and pattern > comb:
pattern = comb
return pattern