2097. Valid Arrangement of Pairs #895
-
Topics: You are given a 0-indexed 2D integer array Return any valid arrangement of Note: The inputs will be generated such that there exists a valid arrangement of Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We can approach it as an Eulerian Path problem in graph theory. In this case, the pairs can be treated as edges, and the values within the pairs (the Key Steps:
Plan:
Let's implement this solution in PHP: 2097. Valid Arrangement of Pairs <?php
/**
* @param Integer[][] $pairs
* @return Integer[][]
*/
function validArrangement($pairs) {
// Graph representation: adjacency list
$graph = [];
// In-degree and Out-degree for each node
$outDegree = [];
$inDegree = [];
// Build the graph and calculate in-degree and out-degree
foreach ($pairs as $pair) {
list($start, $end) = $pair;
$graph[$start][] = $end;
$outDegree[$start] = isset($outDegree[$start]) ? $outDegree[$start] + 1 : 1;
$inDegree[$end] = isset($inDegree[$end]) ? $inDegree[$end] + 1 : 1;
}
// Find the start node for Eulerian path (a node with outDegree - inDegree = 1)
$startNode = null;
foreach ($outDegree as $node => $outCount) {
$inCount = isset($inDegree[$node]) ? $inDegree[$node] : 0;
if ($outCount - $inCount == 1) {
$startNode = $node;
break;
}
}
// If no start node found, use any node from the graph as a start
if ($startNode === null) {
$startNode = key($graph); // Just get the first key
}
// Hierholzer's algorithm to find the Eulerian path
$stack = [];
$result = [];
$currentNode = $startNode;
while (count($stack) > 0 || isset($graph[$currentNode]) && count($graph[$currentNode]) > 0) {
if (isset($graph[$currentNode]) && count($graph[$currentNode]) > 0) {
// There are still edges to visit from current node
$stack[] = $currentNode;
$nextNode = array_pop($graph[$currentNode]);
$currentNode = $nextNode;
} else {
// No more edges, add current node to result
$result[] = [$stack[count($stack) - 1], $currentNode];
$currentNode = array_pop($stack);
}
}
// Since we built the path backwards, reverse it
return array_reverse($result);
}
// Example usage:
$pairs1 = [[5, 1], [4, 5], [11, 9], [9, 4]];
$pairs2 = [[1, 3], [3, 2], [2, 1]];
$pairs3 = [[1, 2], [1, 3], [2, 1]];
print_r(validArrangement($pairs1)); // Output: [[11, 9], [9, 4], [4, 5], [5, 1]]
print_r(validArrangement($pairs2)); // Output: [[1, 3], [3, 2], [2, 1]]
print_r(validArrangement($pairs3)); // Output: [[1, 2], [2, 1], [1, 3]]
?> Explanation:
Example Output:Array
(
[0] => Array
(
[0] => 11
[1] => 9
)
[1] => Array
(
[0] => 9
[1] => 4
)
[2] => Array
(
[0] => 4
[1] => 5
)
[3] => Array
(
[0] => 5
[1] => 1
)
) Time Complexity:
This approach efficiently finds a valid arrangement of pairs by treating the problem as an Eulerian path problem in a directed graph. |
Beta Was this translation helpful? Give feedback.
We can approach it as an Eulerian Path problem in graph theory. In this case, the pairs can be treated as edges, and the values within the pairs (the
start
andend
) can be treated as nodes. We need to find an Eulerian path, which is a path that uses every edge exactly once, and the end of one edge must match the start of the next edge.Key Steps:
start[i]
toend[i]
.