Sort Items by Groups Respecting Dependencies

There are n items labelled 0 to n - 1, each in one of m groups 0 to m - 1 or in no group -1. beforeItems[i] lists the items that must appear before item i. Return an ordering of all items such that every item comes after all items in its beforeItems list, and items belonging to the same group are printed consecutively. Items with no group are treated as their own groups. Return an empty array when no such ordering exists. Any valid ordering is accepted.

Examples
Input: [4,4,[0,1,2,3],[[],[2],[3],[0]]]
Output: [0,3,2,1]
Hints

Sort Items by Groups Respecting Dependencies

There are `n` items labelled `0` to `n - 1`, each in one of `m` groups `0` to `m - 1` or in no group `-1`. `beforeItems[i]` lists the items that must appear before item `i`. Return an ordering of all items such that every item comes after all items in its `beforeItems` list, and items belonging to the same group are printed consecutively. Items with no group are treated as their own groups. Return an empty array when no such ordering exists. Any valid ordering is accepted.