在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You are given three arrays A 3-sequence is a list of not necessarily different websites of length 3 sorted in ascending order by the time of their visits. Find the 3-sequence visited at least once by the largest number of users. If there is more than one solution, return the lexicographically minimum solution. A 3-sequence It is guaranteed that there is at least one user who visited at least 3 websites. No user visits two websites at the same time. Example 1: Input: username = ["joe","joe","joe","james","james","james","james","mary","mary","mary"], timestamp = Note:
为了评估某网站的用户转化率,我们需要对用户的访问行为进行分析,并建立用户行为模型。日志文件中已经记录了用户名、访问时间 以及 页面路径。 为了方便分析,日志文件中的 我们需要找到用户访问网站时的 『共性行为路径』,也就是有最多的用户都 至少按某种次序访问过一次 的三个页面路径。需要注意的是,用户 可能不是连续访问 这三个路径的。 『共性行为路径』是一个 长度为 3 的页面路径列表,列表中的路径 不必不同,并且按照访问时间的先后升序排列。 如果有多个满足要求的答案,那么就请返回按字典序排列最小的那个。(页面路径列表 题目保证一个用户会至少访问 3 个路径一致的页面,并且一个用户不会在同一时间访问两个路径不同的页面。 示例: 输入: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"] 输出:["home","about","career"] 解释: 由示例输入得到的记录如下: ["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"] 有 2 个用户至少访问过一次 ("home", "about", "career")。 有 1 个用户至少访问过一次 ("home", "cart", "maps")。 有 1 个用户至少访问过一次 ("home", "cart", "home")。 有 1 个用户至少访问过一次 ("home", "maps", "home")。 有 1 个用户至少访问过一次 ("cart", "maps", "home")。 提示:
1 class Solution { 2 func mostVisitedPattern(_ username: [String], _ timestamp: [Int], _ website: [String]) -> [String] { 3 let N:Int = username.count 4 var webs:Set<String> = Set<String>(website) 5 var v:[String:[Int:String]] = [String:[Int:String]]() 6 for i in 0..<N 7 { 8 v[username[i],default:[Int:String]()][timestamp[i]] = website[i] 9 } 10 var ans:Int = 0 11 var ret:[String] = [String]() 12 for a in website 13 { 14 for b in website 15 { 16 for c in website 17 { 18 var num:Int = 0 19 for (user, history) in v 20 { 21 var cnt:Int = 0 22 for (time, web) in history 23 { 24 if cnt == 0 && web == a {cnt += 1} 25 else if cnt == 1 && web == b {cnt += 1} 26 else if cnt == 2 && web == c 27 { 28 cnt += 1 29 break 30 } 31 } 32 if cnt == 3 {num += 1} 33 } 34 if num > ans 35 { 36 ans = num 37 ret = [a, b, c] 38 } 39 } 40 } 41 } 42 return ret 43 } 44 }
|
请发表评论