Faaez Razeen

Analyze User Website Visit Pattern

  • 1 min read
  • Array
  • LC-Medium

3 years ago

Solution

TimeSpaceExplanation
O()O()
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]: visited = defaultdict(list) for t, u, w in sorted(zip(timestamp, username, website)): visited[u].append(w) scores = defaultdict(lambda: 0) for u, w in visited.items(): for pattern in set(combinations(w, 3)): scores[pattern] += 1 max_score, best_pattern = 0, None for p, s in scores.items(): if s > max_score or (s == max_score and p < best_pattern): max_score, best_pattern = s, p return best_pattern