• [Swift]LeetCode1152. 用户网站访问行为分析 | Analyze User Website Visit Pattern


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(www.zengqiang.org
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11333855.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    You are given three arrays usernametimestamp and website of the same length N where the ithtuple means that the user with name username[i] visited the website website[i] at time timestamp[i].

    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 X is lexicographically smaller than a 3-sequence Y if X[0] < Y[0] or X[0] == Y[0] and (X[1] < Y[1] or X[1] == Y[1] and X[2] < Y[2])

    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 = [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.

    为了评估某网站的用户转化率,我们需要对用户的访问行为进行分析,并建立用户行为模型。日志文件中已经记录了用户名、访问时间 以及 页面路径。

    为了方便分析,日志文件中的 N 条记录已经被解析成三个长度相同且长度都为 N 的数组,分别是:用户名 username,访问时间 timestamp 和 页面路径 website。第 i 条记录意味着用户名是 username[i] 的用户在 timestamp[i] 的时候访问了路径为 website[i] 的页面。

    我们需要找到用户访问网站时的 『共性行为路径』,也就是有最多的用户都 至少按某种次序访问过一次 的三个页面路径。需要注意的是,用户 可能不是连续访问 这三个路径的。

    『共性行为路径』是一个 长度为 3 的页面路径列表,列表中的路径 不必不同,并且按照访问时间的先后升序排列。

    如果有多个满足要求的答案,那么就请返回按字典序排列最小的那个。(页面路径列表 X 按字典序小于 Y 的前提条件是:X[0] < Y[0] 或 X[0] == Y[0] 且 (X[1] < Y[1] 或 X[1] == Y[1] 且 X[2] < Y[2])

    题目保证一个用户会至少访问 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. 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. username[i] 和 website[i] 都只含小写字符

     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 }
  • 相关阅读:
    Centos7 安装rabbitmq详细教程
    Spring Boot中的@Configuration和@Bean
    springboot+redis项目实战完整篇
    rabbitmq、kafka、activemq、rocketmq之间对比,区别
    MySQL基础总结
    MySQL视图
    MySQL左外链接与右外连接
    MySQL自连接
    MySQL关联查询
    MySQLhaving子句
  • 原文地址:https://www.cnblogs.com/strengthen/p/11333855.html
Copyright © 2020-2023  润新知