★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-alactdio-kp.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You are given three arrays username
, timestamp
and website
of the same length N
where the ith
tuple means that the user with name username[i]
visited the website website[i]
at time timestamp[i]
.git
A 3-sequence is a list of not necessarily different websites of length 3 sorted in ascending order by the time of their visits.github
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.web
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:this
3 <= N = username.length = timestamp.length = website.length <= 50
1 <= username[i].length <= 10
0 <= timestamp[i] <= 10^9
1 <= website[i].length <= 10
username[i]
and website[i]
contain only lowercase characters.爲了評估某網站的用戶轉化率,咱們須要對用戶的訪問行爲進行分析,並創建用戶行爲模型。日誌文件中已經記錄了用戶名、訪問時間 以及 頁面路徑。spa
爲了方便分析,日誌文件中的 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")。
提示:
3 <= N = username.length = timestamp.length = website.length <= 50
1 <= username[i].length <= 10
0 <= timestamp[i] <= 10^9
1 <= website[i].length <= 10
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 }