zoukankan      html  css  js  c++  java
  • [LC] 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].

    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:

    1. 3 <= N = username.length = timestamp.length = website.length <= 50
    2. 1 <= username[i].length <= 10
    3. 0 <= timestamp[i] <= 10^9
    4. 1 <= website[i].length <= 10
    5. Both username[i] and website[i] contain only lowercase characters.
    6. It is guaranteed that there is at least one user who visited at least 3 websites.
    7. No user visits two websites at the same time.
    class Solution {
        public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) {
            Map<String, List<Pair>> map = new HashMap<>();
            int len = username.length;
            for (int i = 0; i < len; i++) {
                if (map.get(username[i]) == null) {
                    map.put(username[i], new ArrayList<>());
                }
                map.get(username[i]).add(new Pair(timestamp[i], website[i]));
            }
            
            Map<String, Integer> count = new HashMap<>();
            String res = "";
            for (String str: map.keySet()) {
                List<Pair> list = map.get(str);
                // avoid the same user visit 3-seq
                Set<String> set = new HashSet<>();
                Collections.sort(list, (a, b) -> a.time - b.time);
                for (int i = 0; i < list.size(); i++) {
                    for (int j = i + 1; j < list.size(); j++) {
                        for (int k = j + 1; k < list.size(); k++) {
                            String cur = list.get(i).web + " " + list.get(j).web + " " + list.get(k).web;
                            if (!set.contains(cur)) {
                                count.put(cur, count.getOrDefault(cur, 0) + 1);
                                set.add(cur);
                            }
                            if (res.equals("") || count.get(cur) > count.get(res) || count.get(cur) == count.get(res) && cur.compareTo(res) < 0) {
                                res = cur;
                            }
                        }
                    }
                }
            }
            
            String[] strArr = res.split(" ");
            List<String> resList = new ArrayList<>();
            for (String str: strArr) {
                resList.add(str);
            }
            return resList;
        }
    }
    
    class Pair {
        String web;
        int time;
        public Pair(Integer time, String web) {
            this.web = web;
            this.time = time;
        }
    }
  • 相关阅读:
    Spring JPA使用CriteriaBuilder动态构造查询
    vscode 将本地项目上传到github、从github克隆项目以及删除github上的某个文件夹
    CDN 加速配置
    dos常用命令
    使用Github作为博客的图床
    一个简单mock-server 解决方案
    postman(三):详解postman动态变量使用
    postman(一):详解在postman中使用环境变量
    postman(二):详解在Pre-request Script中如何执行请求
    MySql中4种批量更新的方法
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12784192.html
Copyright © 2011-2022 走看看