Given n items belonging to m groups, and dependencies between items, sort items so that:
- All dependencies are satisfied (if item
adepends onb,bcomes beforea). - Items in the same group are adjacent.
Items with group -1 can go anywhere (treat each as its own group).
With n=8, m=2, group=[-1,-1,1,0,0,1,0,-1], beforeItems=[[],[6],[5],[6],[3,6],[],[],[]:
- Item 2 depends on 5, item 3 depends on 6, item 4 depends on 3 and 6.
- Valid order: [6,3,4,1,5,2,0,7] or similar.
Return empty array if impossible.
Constraints: . .