1152. Analyze User Website Visit Pattern
Posted 北叶青藤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1152. 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.
1 class Pair { 2 int time; 3 String web; 4 5 public Pair(int time, String web) { 6 this.time = time; 7 this.web = web; 8 } 9 } 10 11 class Solution { 12 public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) { 13 Map<String, List<Pair>> map = new HashMap<>(); 14 int n = username.length; 15 for (int i = 0; i < n; i++) { 16 map.putIfAbsent(username[i], new ArrayList<>()); 17 map.get(username[i]).add(new Pair(timestamp[i], website[i])); 18 } 19 // count map to record every 3 combination occuring time for the different user. 20 Map<String, Integer> count = new HashMap<>(); 21 String res = ""; 22 for (String key : map.keySet()) { 23 Set<String> set = new HashSet<>(); 24 List<Pair> list = map.get(key); 25 if (list.size() < 3) { 26 continue; 27 } 28 Collections.sort(list, (a, b) -> (a.time - b.time)); // sort by time 29 // brutal force O(N ^ 3) 30 for (int i = 0; i < list.size(); i++) { 31 for (int j = i + 1; j < list.size(); j++) { 32 for (int k = j + 1; k < list.size(); k++) { 33 String str = list.get(i).web + " " + list.get(j).web + " " + list.get(k).web; 34 if (!set.contains(str)) { 35 count.put(str, count.getOrDefault(str, 0) + 1); 36 set.add(str); 37 } 38 if (res.equals("") || count.get(res) < count.get(str) || (count.get(res) == count.get(str) && res.compareTo(str) > 0)) { 39 // make sure the right lexi order 40 res = str; 41 } 42 } 43 } 44 } 45 } 46 return Arrays.asList(res.split(" ")); 47 } 48 }
以上是关于1152. Analyze User Website Visit Pattern的主要内容,如果未能解决你的问题,请参考以下文章
1152 Google Recruitment (20 分)