在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In a group of N people (labelled For convenience, we'll call the person with label We'll say that Also, we'll say Now, return Example 1: Input: richer = [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:
在一组 N 个人(编号为 为了方便起见,我们将编号为 如果能够肯定 person 另外,如果 person 现在,返回答案 示例: 输入: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。 其他的答案也可以用类似的推理来解释。 提示:
Runtime: 488 ms
Memory Usage: 21.4 MB
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 } Runtime: 540 ms
Memory Usage: 22.2 MB
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 } Runtime: 3172 ms
Memory Usage: 34.3 MB
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 }
|
请发表评论