Search⌘ K
AI Features

Solution: Sort Items by Groups Respecting Dependencies

Explore how to organize items into groups while respecting dependencies using a two-level topological sorting approach. Understand how to manage both item-level and group-level constraints to produce a valid ordering. Learn to implement the solution with graphs and Kahn’s algorithm, enabling you to tackle complex dependency problems often seen in coding interviews.

Statement

You are given nn items indexed from 00 to n1n − 1. Each item belongs to 00 or one of m groups, described by the array group, where:

  • group[i] ...