zoukankan      html  css  js  c++  java
  • 1152. Analyze User Website Visit Pattern 分析用户访问过的网站

    You are given two string arrays username and website and an integer array timestamp. All the given arrays are of the same length and the tuple [username[i], website[i], timestamp[i]] indicates that the user username[i] visited the website website[i] at time timestamp[i].

    A pattern is a list of three websites (not necessarily distinct).

    • For example, ["home", "away", "love"]["leetcode", "love", "leetcode"], and ["luffy", "luffy", "luffy"] are all patterns.

    The score of a pattern is the number of users that visited all the websites in the pattern in the same order they appeared in the pattern.

    • For example, if the pattern is ["home", "away", "love"], the score is the number of users x such that x visited "home" then visited "away" and visited "love" after that.
    • Similarly, if the pattern is ["leetcode", "love", "leetcode"], the score is the number of users x such that x visited "leetcode" then visited "love" and visited "leetcode" one more time after that.
    • Also, if the pattern is ["luffy", "luffy", "luffy"], the score is the number of users x such that x visited "luffy" three different times at different timestamps.

    Return the pattern with the largest score. If there is more than one pattern with the same largest score, return the lexicographically smallest such pattern.

     

    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","home",1],["joe","about",2],["joe","career",3],["james","home",4],["james","cart",5],["james","maps",6],["james","home",7],["mary","home",8],["mary","about",9], and ["mary","career",10].
    The pattern ("home", "about", "career") has score 2 (joe and mary).
    The pattern ("home", "cart", "maps") has score 1 (james).
    The pattern ("home", "cart", "home") has score 1 (james).
    The pattern ("home", "maps", "home") has score 1 (james).
    The pattern ("cart", "maps", "home") has score 1 (james).
    The pattern ("home", "home", "home") has score 0 (no user visited home 3 times).
    

    Example 2:

    Input: username = ["ua","ua","ua","ub","ub","ub"], timestamp = [1,2,3,4,5,6], website = ["a","b","a","a","b","c"]
    Output: ["a","b","a"]

    参考:https://blog.csdn.net/qq_46105170/article/details/112001460

    三元数组,当作map的key。真是有创意啊。第一次碰到list可以当作key的情况。哪怕很麻烦,有个想法也是好的啊。

    import java.util.*;
    
    //三元数组,当作map的key。真是有创意啊。第一次碰到list可以当作key的情况。
    public class Solution {
        
        class Visit {
            String username, website;
            int timestamp;
            
            public Visit(String username, String website, int timestamp) {
                this.username = username;
                this.website = website;
                this.timestamp = timestamp;
            }
        }
        
        public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) {
            int n = username.length;
            // 先存一下Visit,然后对其按照时间戳排序
            List<Visit> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(new Visit(username[i], website[i], timestamp[i]));
            }
            list.sort((v1, v2) -> Integer.compare(v1.timestamp, v2.timestamp));
            
            // 存一下每个用户访问网站的序列
            Map<String, List<String>> map = new HashMap<>();
            for (int i = 0; i < n; i++) {
                Visit visit = list.get(i);
                map.putIfAbsent(visit.username, new ArrayList<>());
                map.get(visit.username).add(visit.website);
            }
            
            // 对每个用户枚举三元组
            Map<List<String>, Integer> count = new HashMap<>();
            for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                List<String> webList = entry.getValue();
                if (webList.size() >= 3) {
                    // 这里对三元组要去重,因为他们的计数只能加1
                    Set<List<String>> set = new HashSet<>();
                    for (int i = 0; i < webList.size() - 2; i++) {
                        for (int j = i + 1; j < webList.size() - 1; j++) {
                            for (int k = j + 1; k < webList.size(); k++) {
                                List<String> tuple = Arrays.asList(webList.get(i), webList.get(j), webList.get(k));
                                set.add(tuple);
                            }
                        }
                    }
                    
                    // 在哈希表里的计数加1
                    for (List<String> tuple : set) {
                        count.put(tuple, count.getOrDefault(tuple, 0) + 1);
                    }
                }
            }
            
            // 最后找一下出现次数最多、字典序最小的三元组
            List<String> res = new ArrayList<>();
            int maxCount = 0;
            for (Map.Entry<List<String>, Integer> entry : count.entrySet()) {
                if (entry.getValue() > maxCount) {
                    maxCount = entry.getValue();
                    res = entry.getKey();
                } else if (entry.getValue() == maxCount && compare(entry.getKey(), res) < 0) {
                    res = entry.getKey();
                }
            }
            
            return res;
        }
        
        private int compare(List<String> l1, List<String> l2) {
            for (int i = 0; i < 3; i++) {
                int comp = l1.get(i).compareTo(l2.get(i));
                if (comp != 0) {
                    return comp;
                }
            }
            
            return 0;
        }
    }
    
    

     

  • 相关阅读:
    html实现文件夹的上传和下载
    JQuery & Javascript
    JSP Scripting Element
    JSP Filter
    Oct22 实例测试
    JSP HTML error code
    Implicit Object in JSP
    JSP action elements
    JSP lifecycle
    Eclipse 配置Tomcat
  • 原文地址:https://www.cnblogs.com/immiao0319/p/15201771.html
Copyright © 2011-2022 走看看