LeetCode 1152. Analyze User Website Visit Pattern
原题链接在这里:https://leetcode.com/problems/analyze-user-website-visit-pattern/
题目:
We are given some website visits: the user with name username[i]
visited the website website[i]
at time timestamp[i]
.
A 3-sequence is a list of websites of length 3 sorted in ascending order by the time of their visits. (The websites in a 3-sequence are not necessarily distinct.)
Find the 3-sequence visited by the largest number of users. If there is more than one solution, return the lexicographically smallest such 3-sequence.
Example 1:
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:
["joe", 1, "home"]
["joe", 2, "about"]
["joe", 3, "career"]
["james", 4, "home"]
["james", 5, "cart"]
["james", 6, "maps"]
["james", 7, "home"]
["mary", 8, "home"]
["mary", 9, "about"]
["mary", 10, "career"]
The 3-sequence ("home", "about", "career") was visited at least once by 2 users.
The 3-sequence ("home", "cart", "maps") was visited at least once by 1 user.
The 3-sequence ("home", "cart", "home") was visited at least once by 1 user.
The 3-sequence ("home", "maps", "home") was visited at least once by 1 user.
The 3-sequence ("cart", "maps", "home") was visited at least once by 1 user.
Note:
3 <= N = username.length = timestamp.length = website.length <= 50
1 <= username[i].length <= 10
0 <= timestamp[i] <= 10^9
1 <= website[i].length <= 10
- Both
username[i]
andwebsite[i]
contain only lowercase characters. - It is guaranteed that there is at least one user who visited at least 3 websites.
- No user visits two websites at the same time.
题解:
Try to get the website sequence visted by most different users. sequence here means chronological order.
First sort the data by timestamp.
And put user and corresponding websites into HashMap<String, List<String>>.
For each user, find all its combination of 3 websites.
And each sequence within this combination, check its count of different user. If count is higher or lexicographically smaller, update the max sequence.
The max sequence is the result.
Time Complexity: O(n ^ 3). n = username.length. sort takes O(nlogn). getCom takes O(n ^ 3).
Space: O(n ^ 3).
AC Java:
1 class Solution { 2 public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) { 3 int n = username.length; 4 List<Pair> datas = new ArrayList<>(); 5 for(int i = 0; i < n; i++){ 6 datas.add(new Pair(username[i], timestamp[i], website[i])); 7 } 8 9 Collections.sort(datas, (a, b) -> a.time - b.time); 10 HashMap<String, List<String>> userToWebs = new HashMap<>(); 11 for(Pair data : datas){ 12 userToWebs.putIfAbsent(data.user, new ArrayList<String>()); 13 userToWebs.get(data.user).add(data.web); 14 } 15 16 HashMap<String, Integer> seqToCount = new HashMap<>(); 17 18 int maxCount = 0; 19 String maxSeq = ""; 20 for(Map.Entry<String, List<String>> entry : userToWebs.entrySet()){ 21 Set<String> seqCom = getCom(entry.getValue()); 22 for(String seq : seqCom){ 23 seqToCount.put(seq, seqToCount.getOrDefault(seq, 0) + 1); 24 if(seqToCount.get(seq) > maxCount){ 25 maxCount = seqToCount.get(seq); 26 maxSeq = seq; 27 }else if(seqToCount.get(seq) == maxCount && seq.compareTo(maxSeq) < 0){ 28 maxSeq = seq; 29 } 30 } 31 } 32 33 List<String> res = new ArrayList<>(); 34 String [] webs = maxSeq.split(","); 35 for(String w : webs){ 36 res.add(w); 37 } 38 39 return res; 40 } 41 42 private HashSet<String> getCom(List<String> webs){ 43 HashSet<String> res = new HashSet<>(); 44 int n = webs.size(); 45 for(int i = 0; i < n - 2; i++){ 46 for(int j = i + 1; j < n - 1; j++){ 47 for(int k = j + 1; k < n; k++){ 48 res.add(webs.get(i) + "," + webs.get(j) + "," + webs.get(k)); 49 } 50 } 51 } 52 53 return res; 54 } 55 } 56 57 class Pair{ 58 String user; 59 int time; 60 String web; 61 public Pair(String user, int time, String web){ 62 this.user = user; 63 this.time = time; 64 this.web = web; 65 } 66 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2017-02-24 LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed
2017-02-24 LeetCode 380. Insert Delete GetRandom O(1)
2016-02-24 LeetCode 249. Group Shifted Strings