★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-mksiwpvj-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git
For convenience, we'll call the person with label x
, simply "person x
".github
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
.app
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
.ui
Example 1:spa
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:code
1 <= quiet.length = N <= 500
0 <= quiet[i] < N
, all quiet[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.richer
are all logically consistent.在一組 N 我的(編號爲 0, 1, 2, ..., N-1
)中,每一個人都有不一樣數目的錢,以及不一樣程度的安靜(quietness)。htm
爲了方便起見,咱們將編號爲 x
的人簡稱爲 "person x
"。blog
若是可以確定 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 }