Solution: Build a Matrix with Conditions
Explore how to build a k by k matrix that meets row and column precedence conditions by performing topological sorts. Understand cycle detection and mapping values to positions, enabling you to create a valid matrix or identify impossibility.
We'll cover the following...
Statement
You are given a positive integer
A 2D integer array
rowConditionsof size, where rowConditions[i] = [above[i], below[i]]. This indicates thatabove[i]must appear in a row abovebelow[i]in the final matrix.A 2D integer array
colConditionsof size, where colConditions[i] = [left[i], right[i]]. This indicates thatleft[i]must appear in a column to the left ofright[i]in the final matrix.
Both arrays contain integers ranging from
You need to construct a