LeetCode 1152: Analyze User Website Visit Pattern Solution
Master LeetCode problem 1152 (Analyze User Website Visit Pattern), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1152. Analyze User Website Visit Pattern
Problem Explanation
Explanation:
To solve this problem, we need to find the 3-sequence user website visit pattern that appears most frequently among all users. We can use a HashMap to store each user's website visit history and then generate all possible 3-sequence combinations from their visits. Finally, we count the frequency of each 3-sequence and return the one with the highest frequency.
Algorithm:
- Create a HashMap to store user's website visit history.
- Generate all possible 3-sequence combinations for each user's visit history.
- Count the frequency of each 3-sequence combination using a HashMap.
- Find the 3-sequence combination with the highest frequency.
Time Complexity: O(n^3 * m) where n is the number of users and m is the average number of websites visited by each user. Space Complexity: O(n^3)
: :
Solution Code
class Solution {
public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) {
Map<String, List<Visit>> userVisits = new HashMap<>();
for (int i = 0; i < username.length; i++) {
userVisits.putIfAbsent(username[i], new ArrayList<>());
userVisits.get(username[i]).add(new Visit(timestamp[i], website[i]));
}
Map<String, Integer> seqCount = new HashMap<>();
String result = "";
for (String user : userVisits.keySet()) {
List<Visit> visits = userVisits.get(user);
Collections.sort(visits, (a, b) -> a.time - b.time);
Set<String> sequences = new HashSet<>();
for (int i = 0; i < visits.size() - 2; i++) {
for (int j = i + 1; j < visits.size() - 1; j++) {
for (int k = j + 1; k < visits.size(); k++) {
String sequence = visits.get(i).website + " " + visits.get(j).website + " " + visits.get(k).website;
sequences.add(sequence);
}
}
}
for (String seq : sequences) {
seqCount.put(seq, seqCount.getOrDefault(seq, 0) + 1);
if (result.equals("") || seqCount.get(seq) > seqCount.get(result) || (seqCount.get(seq) == seqCount.get(result) && seq.compareTo(result) < 0)) {
result = seq;
}
}
}
return Arrays.asList(result.split(" "));
}
class Visit {
int time;
String website;
public Visit(int time, String website) {
this.time = time;
this.website = website;
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1152 (Analyze User Website Visit Pattern)?
This page provides optimized solutions for LeetCode problem 1152 (Analyze User Website Visit Pattern) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1152 (Analyze User Website Visit Pattern)?
The time complexity for LeetCode 1152 (Analyze User Website Visit Pattern) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1152 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1152 in Java, C++, or Python.