[Swift]LeetCode851. 喧闹和富有 | Loud and Rich
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10592389.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
In a group of N people (labelled 0, 1, 2, ..., N-1
), each person has different amounts of money, and different levels of quietness.
For convenience, we'll call the person with label x
, simply "person x
".
We'll say that richer[i] = [x, y]
if person x
definitely has more money than person y
. Note that richer
may only be a subset of valid observations.
Also, we'll say quiet[x] = q
if person x has quietness q
.
Now, return answer
, where answer[x] = y
if y
is the least quiet person (that is, the person y
with the smallest value of quiet[y]
), among all people who definitely have equal to or more money than person x
.
Example 1:
Input: richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
Output: [5,5,2,5,4,5,6,7]
Explanation:
answer[0] = 5.
Person 5 has more money than 3, which has more money than 1, which has more money than 0.
The only person who is quieter (has lower quiet[x]) is person 7, but
it isn't clear if they have more money than person 0.
answer[7] = 7.
Among all people that definitely have equal to or more money than person 7
(which could be persons 3, 4, 5, 6, or 7), the person who is the quietest (has lower quiet[x])
is person 7.
The other answers can be filled out with similar reasoning.
Note:
1 <= quiet.length = N <= 500
0 <= quiet[i] < N
, allquiet[i]
are different.0 <= richer.length <= N * (N-1) / 2
0 <= richer[i][j] < N
richer[i][0] != richer[i][1]
richer[i]
's are all different.- The observations in
richer
are all logically consistent.
在一组 N 个人(编号为 0, 1, 2, ..., N-1
)中,每个人都有不同数目的钱,以及不同程度的安静(quietness)。
为了方便起见,我们将编号为 x
的人简称为 "person x
"。
如果能够肯定 person x
比 person y
更有钱的话,我们会说 richer[i] = [x, y]
。注意 richer
可能只是有效观察的一个子集。
另外,如果 person x
的安静程度为 q
,我们会说 quiet[x] = q
。
现在,返回答案 answer
,其中 answer[x] = y
的前提是,在所有拥有的钱不少于 person x
的人中,person y
是最安静的人(也就是安静值 quiet[y]
最小的人)。
示例:
输入:richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0] 输出:[5,5,2,5,4,5,6,7] 解释: answer[0] = 5, person 5 比 person 3 有更多的钱,person 3 比 person 1 有更多的钱,person 1 比 person 0 有更多的钱。 唯一较为安静(有较低的安静值 quiet[x])的人是 person 7, 但是目前还不清楚他是否比 person 0 更有钱。 answer[7] = 7, 在所有拥有的钱肯定不少于 person 7 的人中(这可能包括 person 3,4,5,6 以及 7), 最安静(有较低安静值 quiet[x])的人是 person 7。 其他的答案也可以用类似的推理来解释。
提示:
1 <= quiet.length = N <= 500
0 <= quiet[i] < N
,所有quiet[i]
都不相同。0 <= richer.length <= N * (N-1) / 2
0 <= richer[i][j] < N
richer[i][0] != richer[i][1]
richer[i]
都是不同的。- 对
richer
的观察在逻辑上是一致的。
1 class Solution { 2 var richer2:[Int:[Int]] = [Int:[Int]]() 3 var res:[Int] = [Int]() 4 func loudAndRich(_ richer: [[Int]], _ quiet: [Int]) -> [Int] { 5 var n:Int = quiet.count 6 for i in 0..<n 7 { 8 richer2[i] = [Int]() 9 } 10 for v in richer 11 { 12 richer2[v[1],default:[Int]()].append(v[0]) 13 } 14 res = [Int](repeating:-1,count:n) 15 for i in 0..<n 16 { 17 dfs(i, quiet) 18 } 19 return res 20 } 21 22 func dfs(_ i:Int,_ quiet:[Int]) -> Int 23 { 24 if res[i] >= 0 {return res[i]} 25 res[i] = i 26 for j in richer2[i,default:[Int]()] 27 { 28 if quiet[res[i]] > quiet[dfs(j, quiet)] 29 { 30 res[i] = res[j] 31 } 32 } 33 return res[i] 34 } 35 }
1 class Solution { 2 func loudAndRich(_ richer: [[Int]], _ quiet: [Int]) -> [Int] { 3 //这是个列表, 4 var mans:[Man] = (0 ..< quiet.count).map{Man.init(index: $0, quiet: quiet[$0],quiets: quiet)} 5 //做成单向链表 叫这个名字吧? 6 for paired in richer { mans[paired.last!].richs.insert(mans[paired.first!]) 7 } 8 //存 结果答案 9 var result:[Int] = [] 10 //遍历答案 11 for e in mans { 12 result.append( 13 e.mostQuietMan.index 14 ) 15 } 16 return result 17 } 18 } 19 20 /// 抽象的人 21 class Man { 22 var index:Int //自己的位置 23 var quiet:Int //安静度 24 var quiets:[Int] 25 var richs:Set<Man> = [] //比自己富的人,用set可去重,有层数 26 init(index:Int,quiet:Int, quiets: [Int]){ 27 self.index = index 28 self.quiet = quiet 29 self.quiets = quiets 30 } 31 lazy var mostQuietMan = getmostQuietMan(quiet: self.quiets) 32 func getmostQuietMan(quiet: [Int]) -> Man { 33 var mostQuietMan = self 34 for man in self.richs{ 35 if quiet[man.index] < quiet[mostQuietMan.index]{ 36 mostQuietMan = man 37 } 38 if quiet[man.mostQuietMan.index] < quiet[mostQuietMan.index] { 39 mostQuietMan = man.mostQuietMan 40 } 41 } 42 return mostQuietMan 43 } 44 } 45 46 //Set需要遵循hashable 47 extension Man:Hashable { 48 static func == (lhs: Man, rhs: Man) -> Bool { 49 return lhs.index == rhs.index 50 } 51 52 var hashValue:Int { 53 return self.index 54 } 55 }
1 class Solution { 2 func loudAndRich(_ richer: [[Int]], _ quiet: [Int]) -> [Int] { 3 var mans:[Man] = (0 ..< quiet.count).map{ Man.init(index: $0, quiet: quiet[$0])} 4 //做成单向链表 叫这个名字吧? 5 for paired in richer { 6 mans[paired.last!].richs.insert(mans[paired.first!]) 7 } 8 var result:[Int] = [] 9 10 for e in mans { 11 result.append( 12 e.richers.min { (a, b) -> Bool in 13 return a.quiet < b.quiet 14 }!.index 15 ) 16 } 17 return result 18 } 19 } 20 21 class Man { 22 var index:Int 23 var quiet:Int 24 var richs:Set<Man> = [] 25 init(index:Int,quiet:Int){ 26 self.index = index 27 self.quiet = quiet 28 } 29 lazy var richers:Set<Man> = self.getRichers() 30 func getRichers() -> Set<Man> { 31 var rs:Set<Man> = richs 32 for e in richs { 33 rs = rs.union(e.richers) 34 } 35 rs.insert(self) 36 return rs 37 } 38 } 39 40 extension Man:Hashable { 41 static func == (lhs: Man, rhs: Man) -> Bool { 42 return lhs.index == rhs.index 43 } 44 45 var hashValue:Int { 46 return self.index 47 } 48 }