Search⌘ K
AI Features

Solution: Sort Items by Groups Respecting Dependencies

Explore the hierarchical topological sort method to solve the problem of sorting items by groups while respecting both intra- and inter-group dependencies. This lesson shows you how to assign groups, construct dependency graphs, and perform multi-level sorting to ensure valid item sequences or detect impossible orderings.

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