Search⌘ K
AI Features

Solution: Sort Items by Groups Respecting Dependencies

Understand how to solve the problem of sorting items grouped with dependency constraints. Learn to use hierarchical topological sorting on both group-level and item-level dependencies to produce a valid order respecting all rules or determine if no valid order 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] ...