Search⌘ K
AI Features

Sort Items by Groups Respecting Dependencies

Understand how to arrange items indexed from zero to n-1 so each respects dependency orders and group continuity. Learn to apply topological sorting to ensure all items in the same group appear together while maintaining precedence constraints. This lesson helps you solve problems where items have dependencies and belong to various groups, including unassigned ones treated as unique groups.

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] represents the group of the ithi^{th} ...