Search⌘ K
AI Features

Solution: Sort Items by Groups Respecting Dependencies

Explore how to arrange items into a valid order that respects both inter-item dependencies and group contiguity. This lesson teaches hierarchical topological sorting with dual-level DAGs to solve complex ordering problems while handling cycles and ensuring group continuity.

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