高级农民
- 积分
- 4020
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-22
- 最后登录
- 1970-1-1
|
本帖最后由 Myron2017 于 2026-2-1 10:34 编辑
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.
Note that the websites in a pattern do not need to be visited contiguously, they only need to be visited in the order they appeared in the 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"]
Constraints:
3 <= username.length <= 50
1 <= username[i].length <= 10
timestamp.length == username.length
1 <= timestamp[i] <= 109
website.length == username.length
1 <= website[i].length <= 10
username[i] and website[i] consist of lowercase English letters.
It is guaranteed that there is at least one user who visited at least three websites.
All the tuples [username[i], timestamp[i], website[i]] are unique.- class Solution:
- def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
- data = [(user, ts, site) for user, ts, site in zip(username, timestamp, website)]
- data = sorted(data, key = lambda x: x[1])
-
- user_path = collections.defaultdict(list)
- for d in data:
- user_path[d[0]].append(d[2])
- freq_count = collections.defaultdict(int)
- for u, u_path in user_path.items():
- distinct_path = set()
- for i in range(len(u_path)):
- for j in range(i+1, len(u_path)):
- for k in range(j+1, len(u_path)):
- distinct_path.add((u_path[i], u_path[j], u_path[k]))
- for k in distinct_path:
- freq_count[k] += 1
- maxCount = -1
- res = ()
- for k, v in freq_count.items():
- if v > maxCount or (v == maxCount and k < res):
- maxCount = v
- res = k
-
- return list(res)
-
-
复制代码- class Node:
- def __init__(self, name, timestamp, website):
- self.name = name
- self.timestamp = timestamp
- self.website = website
- class Solution:
- def mostVisitedPattern(
- self, username: List[str], timestamp: List[int], website: List[str]
- ) -> List[str]:
- nodes = [
- Node(name, ts, site)
- for name, ts, site in zip(username, timestamp, website)
- ]
- nodes.sort(key=lambda x: x.timestamp)
- user_visits = defaultdict(list)
- for node in nodes:
- user_visits[node.name].append(node)
- route = defaultdict(int)
- for visits in user_visits.values():
- tmp = set()
- for i, j, k in combinations(range(len(visits)), 3):
- path = (visits[i].website, visits[j].website, visits[k].website)
- tmp.add(path)
- for path in tmp:
- route[path] += 1
- max_count = -1
- result = ()
- for path, count in route.items():
- if count > max_count or (count == max_count and path < result):
- max_count = count
- result = path
- return list(result)
复制代码 |
|