Home >Backend Development >PHP Tutorial >Valid Arrangement of Pairs
2097. Valid Arrangement of Pairs
Difficulty: Hard
Topics: Depth-First Search, Graph, Eulerian Circuit
You are given a 0-indexed 2D integer array pairs where pairs[i] = [starti, endi]. An arrangement of pairs is valid if for every index i where 1 <= i < pairs.length, we have endi-1 == starti.
Return any valid arrangement of pairs.
Note: The inputs will be generated such that there exists a valid arrangement of pairs.
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
Solution:
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 and end) 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.
Let's implement this solution in PHP: 2097. Valid Arrangement of Pairs
<?php /** * @param Integer[][] $pairs * @return Integer[][] */ function validArrangement($pairs) { ... ... ... /** * go to ./solution.php */ } // 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]] ?> </p> <h3> Explanation: </h3> <ol> <li> <p><strong>Graph Construction</strong>:</p> <ul> <li>We build the graph using an adjacency list where each key is a start node, and the value is a list of end nodes.</li> <li>We also maintain the out-degree and in-degree for each node, which will help us find the start node for the Eulerian path.</li> </ul> </li> <li> <p><strong>Finding the Start Node</strong>:</p> <ul> <li>An Eulerian path starts at a node where the out-degree is greater than the in-degree by 1 (if such a node exists).</li> <li>If no such node exists, the graph is balanced, and we can start at any node.</li> </ul> </li> <li> <p><strong>Hierholzer's Algorithm</strong>:</p> <ul> <li>We start from the startNode and repeatedly follow edges, marking them as visited by removing them from the adjacency list.</li> <li>Once we reach a node with no more outgoing edges, we backtrack and build the result.</li> </ul> </li> <li> <p><strong>Return the Result</strong>:</p> <ul> <li>The result is constructed in reverse order because of the way we backtrack, so we reverse it at the end.</li> </ul> </li> </ol> <h3> Example Output: </h3> <pre class="brush:php;toolbar:false"><?php /** * @param Integer[][] $pairs * @return Integer[][] */ function validArrangement($pairs) { ... ... ... /** * go to ./solution.php */ } // 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]] ?>
This approach efficiently finds a valid arrangement of pairs by treating the problem as an Eulerian path problem in a directed graph.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Valid Arrangement of Pairs. For more information, please follow other related articles on the PHP Chinese website!