← 返回 roblox 的题目列表List of Dependencies with Tie Handling
类型:online_judge
Given a list of dependencies, where each dependency is represented by a tuple (A, B) indicating B depends on A. Write a function to return a topological order. If there are ties (multiple possible orders), sort them according to the first element of the 2D array.
Input
A 2D array dependencies representing the dependencies, where each element is a tuple (A, B).
Output
Return a list representing the topological sorting order.
Examples
Example 1
Input: dependencies = [[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]]
Output: [5, 4, 2, 3, 1, 0]
Example 2
Input: dependencies = [[1, 0], [2, 0], [3, 1], [3, 2]]
Output: [3, 1, 2, 0]
Constraints
All numbers are non-negative integers.
Dependencies may contain cycles.
Note: You need to handle ties by the order of the first element.
Example
Input
[[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]]
Output
[5, 4, 2, 3, 1, 0]