Analyze User Website Visit Pattern — LeetCode 1152 Python Solution
- Problem
- #1152
- Pattern
- Sorting
- Reading time
- 4 min
- Source
- leetcode.com
The problem
You are given two string arrays username and website and an integer array timestamp. All the given arrays are of the same length and the tuple [username[i], website[i], timestamp[i]] indicates that the user username[i] visited the website website[i] at time timestamp[i].
Example
- Input
- username = ["joe","joe","joe","james","james","james","james","mary","mary","mary"], timestamp = [1,2,3,4,5,6,7,8,9,10], website = ["home","about","career","home","cart","maps","home","home","about","career"]
- Output
- ["home","about","career"]
- Explanation
- The tuples in this example are:
Python solution
class Solution:
def mostVisitedPattern(
self, username: List[str], timestamp: List[int], website: List[str]
) -> List[str]:
d = defaultdict(list)
for user, _, site in sorted(
zip(username, timestamp, website), key=lambda x: x[1]
):
d[user].append(site)
cnt = Counter()
for sites in d.values():
m = len(sites)
s = set()
if m > 2:
for i in range(m - 2):
for j in range(i + 1, m - 1):
for k in range(j + 1, m):
s.add((sites[i], sites[j], sites[k]))
for t in s:
cnt[t] += 1
return sorted(cnt.items(), key=lambda x: (-x[1], x[0]))[0][0]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n^3) |
| Space | O(n^3) auxiliary |
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 1152. Analyze User Website Visit Pattern is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1152. Analyze User Website Visit Pattern?
- LeetCode 1152. Analyze User Website Visit Pattern is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1152. Analyze User Website Visit Pattern?
- The Python solution on this page runs in O(n^3).
- What is the space complexity of LeetCode 1152. Analyze User Website Visit Pattern?
- The Python solution on this page uses O(n^3) auxiliary space.
- What topics does LeetCode 1152. Analyze User Website Visit Pattern cover?
- LeetCode 1152. Analyze User Website Visit Pattern is tagged Array, Hash Table, String and Sorting on LeetCode.
- Is LeetCode 1152. Analyze User Website Visit Pattern a premium problem?
- Yes. LeetCode 1152. Analyze User Website Visit Pattern is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.