Problem Description
Given three arrays—username, timestamp, and website—each index represents a user visiting a website at a particular time. Our goal is to determine the 3-sequence of websites (a pattern) that is visited by the largest number of unique users in the order they visited them. If there is a tie, return the lexicographically smallest pattern.
Key Insights
- Combine the inputs into a single list of events and sort them by timestamp.
- Group the visits by each user, preserving the sorted order.
- Generate all unique combinations of 3-sequences for each user (order matters and non-contiguous visits are allowed).
- Use a dictionary (or hash map) to map each 3-sequence pattern to the set of users who visited that pattern.
- The problem constraints are small, so generating combinations per user (even using triple nested loops) is acceptable.
- When multiple patterns have the same frequency, choose the lexicographically smallest one.
Space and Time Complexity
Time Complexity: O(U * L^3) where U is the number of users and L is the maximum length of visits per user (since we generate combinations of 3 visits per user). Sorting all events costs O(N log N) for N total events. Space Complexity: O(P) where P is the number of unique 3-sequence patterns stored in the dictionary, plus O(N) for grouping the events.
Solution
- Aggregate the events by user while sorting them based on timestamp.
- For each user, generate all sets of 3-website patterns (to avoid duplicate patterns per user).
- Use a hash map to count the number of unique users for each pattern.
- Traverse the map to find the pattern with the highest count. In case of a tie, compare patterns lexicographically.
- Return the pattern that meets the criteria.