|
| 1 | +<h2><a href="https://leetcode.com/problems/valid-arrangement-of-pairs">Valid Arrangement of Pairs</a></h2> <img src='https://img.shields.io/badge/Difficulty-Hard-red' alt='Difficulty: Hard' /><hr><p>You are given a <strong>0-indexed</strong> 2D integer array <code>pairs</code> where <code>pairs[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>. An arrangement of <code>pairs</code> is <strong>valid</strong> if for every index <code>i</code> where <code>1 <= i < pairs.length</code>, we have <code>end<sub>i-1</sub> == start<sub>i</sub></code>.</p> |
| 2 | + |
| 3 | +<p>Return <em><strong>any</strong> valid arrangement of </em><code>pairs</code>.</p> |
| 4 | + |
| 5 | +<p><strong>Note:</strong> The inputs will be generated such that there exists a valid arrangement of <code>pairs</code>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> pairs = [[5,1],[4,5],[11,9],[9,4]] |
| 12 | +<strong>Output:</strong> [[11,9],[9,4],[4,5],[5,1]] |
| 13 | +<strong>Explanation: |
| 14 | +</strong>This is a valid arrangement since end<sub>i-1</sub> always equals start<sub>i</sub>. |
| 15 | +end<sub>0</sub> = 9 == 9 = start<sub>1</sub> |
| 16 | +end<sub>1</sub> = 4 == 4 = start<sub>2</sub> |
| 17 | +end<sub>2</sub> = 5 == 5 = start<sub>3</sub> |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> pairs = [[1,3],[3,2],[2,1]] |
| 24 | +<strong>Output:</strong> [[1,3],[3,2],[2,1]] |
| 25 | +<strong>Explanation:</strong> |
| 26 | +This is a valid arrangement since end<sub>i-1</sub> always equals start<sub>i</sub>. |
| 27 | +end<sub>0</sub> = 3 == 3 = start<sub>1</sub> |
| 28 | +end<sub>1</sub> = 2 == 2 = start<sub>2</sub> |
| 29 | +The arrangements [[2,1],[1,3],[3,2]] and [[3,2],[2,1],[1,3]] are also valid. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong class="example">Example 3:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> pairs = [[1,2],[1,3],[2,1]] |
| 36 | +<strong>Output:</strong> [[1,2],[2,1],[1,3]] |
| 37 | +<strong>Explanation:</strong> |
| 38 | +This is a valid arrangement since end<sub>i-1</sub> always equals start<sub>i</sub>. |
| 39 | +end<sub>0</sub> = 2 == 2 = start<sub>1</sub> |
| 40 | +end<sub>1</sub> = 1 == 1 = start<sub>2</sub> |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= pairs.length <= 10<sup>5</sup></code></li> |
| 48 | + <li><code>pairs[i].length == 2</code></li> |
| 49 | + <li><code>0 <= start<sub>i</sub>, end<sub>i</sub> <= 10<sup>9</sup></code></li> |
| 50 | + <li><code>start<sub>i</sub> != end<sub>i</sub></code></li> |
| 51 | + <li>No two pairs are exactly the same.</li> |
| 52 | + <li>There <strong>exists</strong> a valid arrangement of <code>pairs</code>.</li> |
| 53 | +</ul> |
0 commit comments