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.

思路:就是每个user visite 网页的sebusequence,频率加起来最大的那个subsequence,注意是每一个user的subsequence,不能够有重复,不同user还是可以重复的;所以visited是每个user内部的去重复;

class Solution {
    private class Node {
        public int time;
        public String website;
        public Node(int time, String website) {
            this.time = time;
            this.website = website;
        }
    }
    
    public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) {
        List<String> list = new ArrayList<String>();
        HashMap<String, List<Node>> hashmap = new HashMap<>();
        for(int i = 0; i < username.length; i++) {
            hashmap.putIfAbsent(username[i], new ArrayList<Node>());
            hashmap.get(username[i]).add(new Node(timestamp[i], website[i]));
        }
        
        HashMap<String, Integer> countmap = new HashMap<>();
        String res = null;
        for(String user: hashmap.keySet()) {
            // 注意是每一个user的subsequence,不能够有重复,不同user还是可以重复的;
            HashSet<String> visited = new HashSet<String>();
            List<Node> nodes = hashmap.get(user);
            Collections.sort(nodes, (a, b) -> (a.time - b.time));
            
            for(int i = 0; i < nodes.size(); i++) {
                for(int j = i + 1; j < nodes.size(); j++) {
                    for(int k = j + 1; k < nodes.size(); k++) {
                        String path = nodes.get(i).website + " "
                                        + nodes.get(j).website + " " 
                                        + nodes.get(k).website;
                        if(!visited.contains(path)) {
                            visited.add(path);
                            countmap.put(path, countmap.getOrDefault(path, 0) + 1);
                        }
                        if(res == null || countmap.get(res) < countmap.get(path) || 
                            countmap.get(res) == countmap.get(path) && res.compareTo(path) > 0) {
                            res = path;
                        }
                    }
                }
            }
        }
        
        String[] strs = res.split(" ");
        for(String str: strs) {
            list.add(str);
        }
        return list;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值