在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ There are Return a sorted list of the items such that:
Return any solution if there is more than one solution and return an empty list if there is no solution.
Example 1: Input: n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] Output: [6,3,4,1,5,2,0,7] Example 2: Input: n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] Output: [] Explanation: This is the same as example 1 except that 4 needs to be before 6 in the sorted list.
Constraints:
公司共有 我们用 请你帮忙按要求安排这些项目的进度,并返回排序后的项目列表:
结果要求: 如果存在多个解决方案,只需要返回其中任意一个即可。 如果没有合适的解决方案,就请返回一个 空列表。
示例 1: 输入:n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] 输出:[6,3,4,1,5,2,0,7] 示例 2: 输入:n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] 输出:[] 解释:与示例 1 大致相同,但是在排序后的列表中,4 必须放在 6 的前面。
提示:
Runtime: 556 ms
Memory Usage: 28.7 MB
1 class Solution { 2 func sortItems(_ n: Int, _ m: Int, _ group: [Int], _ beforeItems: [[Int]]) -> [Int] { 3 var group = group 4 var ret:[Int] = [Int]() 5 //the vector stores the followers of the key which is the group id 6 var rules_between_groups:[Int:[Int]] = [Int:[Int]]() 7 //the key is the the group id, the value has similar structure as rules_between_groups 8 var rules_in_group:[Int:[Int:[Int]]] = [Int:[Int:[Int]]]() 9 var mm:Int = m 10 for i in 0..<group.count 11 { 12 //asign group ids for the no-group items 13 if group[i] == -1 {group[i] = mm++} 14 15 } 16 //parse the following/followed (beforeItems) rules 17 for i in 0..<beforeItems.count 18 { 19 for j in 0..<beforeItems[i].count 20 { 21 if group[beforeItems[i][j]] == group[i] 22 { 23 let gg:Int = group[i] 24 rules_in_group[gg,default:[Int:[Int]]()][beforeItems[i][j],default:[Int]()].append(i) 25 } 26 else 27 { 28 rules_between_groups[group[beforeItems[i][j]],default:[Int]()].append(group[i]) 29 } 30 } 31 } 32 //to remove the duplicated group id 33 var groups:Set<Int> = Set<Int>() 34 //key is the group id, vector stores the items 35 var items_by_group:[Int:[Int]] = [Int:[Int]]() 36 for i in 0..<group.count 37 { 38 groups.insert(group[i]) 39 items_by_group[group[i],default:[Int]()].append(i) 40 } 41 //vector of unique group ids 42 var vgroup:[Int] = Array(groups) 43 if !applyRules(&vgroup,&rules_between_groups) {return ret} 44 for i in 0..<m 45 { 46 if !applyRules(&items_by_group[i,default:[Int]()], &rules_in_group[i,default:[Int:[Int]]()]) 47 { 48 return ret 49 } 50 } 51 for gg in vgroup 52 { 53 for ii in items_by_group[gg,default:[Int]()] 54 { 55 ret.append(ii) 56 } 57 } 58 return ret 59 } 60 61 func applyRules(_ eles:inout [Int],_ rules:inout [Int:[Int]]) -> Bool 62 { 63 //toplogical sort 64 //id -> indegree number 65 var indegree:[Int:Int] = [Int:Int]() 66 for ele in eles 67 { 68 indegree[ele] = 0 69 } 70 eles.removeAll() 71 //calculate the indegrees 72 for valu in rules.values 73 { 74 for i in 0..<valu.count 75 { 76 indegree[valu[i],default:0] += 1 77 } 78 } 79 while(indegree.count > 0) 80 { 81 let start:Int = eles.count 82 for (key,val) in indegree 83 { 84 if val == 0 85 { 86 eles.append(key) 87 for aa in rules[key,default:[Int]()] 88 { 89 indegree[aa,default:0] -= 1 90 } 91 } 92 } 93 let stop:Int = eles.count 94 for i in start..<stop 95 { 96 indegree[eles[i]] = nil 97 } 98 if start == stop 99 { 100 return false 101 } 102 } 103 return true 104 } 105 } 106 107 /*扩展Int类,实现自增++、自减--运算符*/ 108 extension Int{ 109 //后缀++:先执行表达式后再自增 110 static postfix func ++(num:inout Int) -> Int { 111 //输入输出参数num 112 let temp = num 113 //num加1 114 num += 1 115 //返回加1前的数值 116 return temp 117 } 118 }
|
请发表评论