Search⌘ K
AI Features

Solution: Sort Items by Groups Respecting Dependencies

Understand how to arrange items within groups by managing two-level dependencies through topological sorting. This lesson teaches you to handle item and group dependency graphs and produce a valid sequence or detect cycles that invalidate ordering.

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] ...