[Swift]LeetCode1101. 彼此熟識的最先時間 | The Earliest Moment When Everyone Become Friends

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-dugcglna-kz.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

In a social group, there are N people, with unique integer ids from 0 to N-1.git

We have a list of logs, where each logs[i] = [timestamp, id_A, id_B] contains a non-negative integer timestamp, and the ids of two different people.github

Each log represents the time in which two different people became friends.  Friendship is symmetric: if A is friends with B, then B is friends with A.微信

Let's say that person A is acquainted with person B if A is friends with B, or A is a friend of someone acquainted with B.app

Return the earliest time for which every person became acquainted with every other person. Return -1 if there is no such earliest time.spa

Example 1:日誌

Input: logs = [[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]], N = 6 Output: 20190301 Explanation: The first event occurs at timestamp = 20190101 and after 0 and 1 become friends we have the following friendship groups [0,1], [2], [3], [4], [5]. The second event occurs at timestamp = 20190104 and after 3 and 4 become friends we have the following friendship groups [0,1], [2], [3,4], [5]. The third event occurs at timestamp = 20190107 and after 2 and 3 become friends we have the following friendship groups [0,1], [2,3,4], [5]. The fourth event occurs at timestamp = 20190211 and after 1 and 5 become friends we have the following friendship groups [0,1,5], [2,3,4]. The fifth event occurs at timestamp = 20190224 and as 2 and 4 are already friend anything happens. The sixth event occurs at timestamp = 20190301 and after 0 and 3 become friends we have that all become friends.

Note:code

  1. 1 <= N <= 100
  2. 1 <= logs.length <= 10^4
  3. 0 <= logs[i][0] <= 10^9
  4. 0 <= logs[i][1], logs[i][2] <= N - 1
  5. It's guaranteed that all timestamps in logs[i][0] are different.
  6. Logs are not necessarily ordered by some criteria.
  7. logs[i][1] != logs[i][2]

在一個社交圈子當中,有 N 我的。每一個人都有一個從 0 到 N-1 惟一的 id 編號。htm

咱們有一份日誌列表 logs,其中每條記錄都包含一個非負整數的時間戳,以及分屬兩我的的不一樣 id,logs[i] = [timestamp, id_A, id_B]blog

每條日誌標識出兩我的成爲好友的時間,友誼是相互的:若是 A 和 B 是好友,那麼 B 和 A 也是好友。

若是 A 是 B 的好友,或者 A 是 B 的好友的好友,那麼就能夠認爲 A 也與 B 熟識。

返回圈子裏全部人之間都熟識的最先時間。若是找不到最先時間,就返回 -1 。

示例:

輸入:logs = [[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]], N = 6
輸出:20190301
解釋:
第一次結交發生在 timestamp = 20190101,0 和 1 成爲好友,社交朋友圈以下 [0,1], [2], [3], [4], [5]。
第二次結交發生在 timestamp = 20190104,3 和 4 成爲好友,社交朋友圈以下 [0,1], [2], [3,4], [5].
第三次結交發生在 timestamp = 20190107,2 和 3 成爲好友,社交朋友圈以下 [0,1], [2,3,4], [5].
第四次結交發生在 timestamp = 20190211,1 和 5 成爲好友,社交朋友圈以下 [0,1,5], [2,3,4].
第五次結交發生在 timestamp = 20190224,2 和 4 已是好友了。
第六次結交發生在 timestamp = 20190301,0 和 3 成爲好友,你們都互相熟識了。

提示:

  1. 1 <= N <= 100
  2. 1 <= logs.length <= 10^4
  3. 0 <= logs[i][0] <= 10^9
  4. 0 <= logs[i][1], logs[i][2] <= N - 1
  5. 保證 logs[i][0] 中的全部時間戳都不一樣
  6. Logs 不必定按某一標準排序
  7. logs[i][1] != logs[i][2]

 368ms

 1 class Solution {
 2     func earliestAcq(_ logs: [[Int]], _ N: Int) -> Int {
 3         var logs = logs
 4         logs.sort(by:{return $0[0] < $1[0]})
 5         let dsu:DisjointSetUnion = DisjointSetUnion(N)
 6         for i in logs
 7         {
 8             dsu.AddEdge(i[1] + 1, i[2] + 1)
 9             if dsu.GetSize(1) == N
10             {
11                 return i[0]
12             }
13         }
14         return -1
15     }
16 }
17 
18 class DisjointSetUnion
19 {
20     var N_:Int
21     var par_:[Int]
22     var size_:[Int]
23     
24     init(_ N:Int)
25     {
26         self.N_ = N
27         self.par_ = [Int](repeating:0,count:N_ + 1)
28         self.size_ = [Int](repeating:1,count:N_ + 1)
29         for i in 0..<N
30         {
31             par_[i] = i
32         }
33     }
34     
35     // Returns the parent of |x|'s set.
36     func GetParent(_ x:Int) -> Int
37     {
38         if par_[x] != x
39         {
40             par_[x] = GetParent(par_[x])
41         }
42         return par_[x]
43     }
44     
45     // Returns the size of set in which |x| belongs.
46     func GetSize(_ x:Int) -> Int
47     {
48         return size_[GetParent(x)]
49     }
50     
51     // Add an edge between |u| and |v|. Creates union of both sets.
52     func AddEdge(_ u:Int,_ v:Int)
53     {
54         var parent_u:Int = GetParent(u)
55         var parent_v:Int = GetParent(v)
56         if parent_u != parent_v
57         {
58             if size_[parent_v] > size_[parent_u]
59             {
60                 let temp:Int = parent_u
61                 parent_u = parent_v
62                 parent_v = temp
63             }
64             par_[parent_v] = parent_u
65             size_[parent_u] += size_[parent_v]
66             size_[parent_v] = 0
67         }
68     }
69 }
相關文章
相關標籤/搜索