Search⌘ K
AI Features

Solution: Sort Items by Groups Respecting Dependencies

Explore how to arrange items so that dependencies are respected and groups stay contiguous by applying hierarchical topological sorting. This lesson guides you through managing both item-level and group-level dependencies effectively to find a valid order or determine if none exists.

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