Tap here to switch tabs
Problem
Ask
Submissions

Problem: Sort Items by Groups Respecting Dependencies

hard
40 min
Explore how to sort items based on group assignments and dependency constraints using topological sort. Understand how to handle ungrouped items, maintain dependency order, and ensure items of the same group appear consecutively. This lesson guides you through solving complex ordering problems efficiently.

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} item.

  • If group[i] ==1==-1, the item isn’t assigned to any existing group and should be treated as belonging to its own unique group.

You’re also given a list, beforeItems, where beforeItems[i] contains all items that must precede item ii in the final ordering.

Your goal is to arrange all nn items in a list that satisfies both of the following rules:

  1. Dependency order: Every item must appear after all the items listed in beforeItems[i].

  2. Group continuity: All items that belong to the same group must appear next to each other in the final order.

If there are multiple valid orderings, return any of them. If there’s no possible ordering, return an empty list.

Constraints:

  • 11 \leq m \leq n \leq 3×1043 \times 10^4

  • group.length ==== beforeItems.length ==== n

  • 1-1 \leq group[i] \leq m - 11

  • 00 \leq beforeItems[i].length \leq n - 11

  • 00 \leq beforeItems[i][j] \leq n - 11

  • i !=!= beforeItems[i][j]

  • beforeItems[i] does not contain duplicate elements.

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Sort Items by Groups Respecting Dependencies

hard
40 min
Explore how to sort items based on group assignments and dependency constraints using topological sort. Understand how to handle ungrouped items, maintain dependency order, and ensure items of the same group appear consecutively. This lesson guides you through solving complex ordering problems efficiently.

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} item.

  • If group[i] ==1==-1, the item isn’t assigned to any existing group and should be treated as belonging to its own unique group.

You’re also given a list, beforeItems, where beforeItems[i] contains all items that must precede item ii in the final ordering.

Your goal is to arrange all nn items in a list that satisfies both of the following rules:

  1. Dependency order: Every item must appear after all the items listed in beforeItems[i].

  2. Group continuity: All items that belong to the same group must appear next to each other in the final order.

If there are multiple valid orderings, return any of them. If there’s no possible ordering, return an empty list.

Constraints:

  • 11 \leq m \leq n \leq 3×1043 \times 10^4

  • group.length ==== beforeItems.length ==== n

  • 1-1 \leq group[i] \leq m - 11

  • 00 \leq beforeItems[i].length \leq n - 11

  • 00 \leq beforeItems[i][j] \leq n - 11

  • i !=!= beforeItems[i][j]

  • beforeItems[i] does not contain duplicate elements.