Search⌘ K
AI Features

Solution: Sort Items by Groups Respecting Dependencies

Understand how to apply hierarchical topological sorting to arrange items so that dependencies are respected within and across groups. Learn to create dependency graphs for items and groups, detect cycles, and produce valid linear orders. This lesson helps you implement a comprehensive solution for sorting with two-level dependencies, a common pattern 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] ...