|
| 1 | +# 1743. Restore the Array From Adjacent Pairs |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Hash Table. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +There is an integer array `nums` that consists of `n` **unique **elements, but you have forgotten it. However, you do remember every pair of adjacent elements in `nums`. |
| 10 | + |
| 11 | +You are given a 2D integer array `adjacentPairs` of size `n - 1` where each `adjacentPairs[i] = [ui, vi]` indicates that the elements `ui` and `vi` are adjacent in `nums`. |
| 12 | + |
| 13 | +It is guaranteed that every adjacent pair of elements `nums[i]` and `nums[i+1]` will exist in `adjacentPairs`, either as `[nums[i], nums[i+1]]` or `[nums[i+1], nums[i]]`. The pairs can appear **in any order**. |
| 14 | + |
| 15 | +Return **the original array **`nums`**. If there are multiple solutions, return **any of them****. |
| 16 | + |
| 17 | + |
| 18 | +Example 1: |
| 19 | + |
| 20 | +``` |
| 21 | +Input: adjacentPairs = [[2,1],[3,4],[3,2]] |
| 22 | +Output: [1,2,3,4] |
| 23 | +Explanation: This array has all its adjacent pairs in adjacentPairs. |
| 24 | +Notice that adjacentPairs[i] may not be in left-to-right order. |
| 25 | +``` |
| 26 | + |
| 27 | +Example 2: |
| 28 | + |
| 29 | +``` |
| 30 | +Input: adjacentPairs = [[4,-2],[1,4],[-3,1]] |
| 31 | +Output: [-2,4,1,-3] |
| 32 | +Explanation: There can be negative numbers. |
| 33 | +Another solution is [-3,1,4,-2], which would also be accepted. |
| 34 | +``` |
| 35 | + |
| 36 | +Example 3: |
| 37 | + |
| 38 | +``` |
| 39 | +Input: adjacentPairs = [[100000,-100000]] |
| 40 | +Output: [100000,-100000] |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +**Constraints:** |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +- `nums.length == n` |
| 49 | + |
| 50 | +- `adjacentPairs.length == n - 1` |
| 51 | + |
| 52 | +- `adjacentPairs[i].length == 2` |
| 53 | + |
| 54 | +- `2 <= n <= 105` |
| 55 | + |
| 56 | +- `-105 <= nums[i], ui, vi <= 105` |
| 57 | + |
| 58 | +- There exists some `nums` that has `adjacentPairs` as its pairs. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## Solution |
| 63 | + |
| 64 | +```javascript |
| 65 | +/** |
| 66 | + * @param {number[][]} adjacentPairs |
| 67 | + * @return {number[]} |
| 68 | + */ |
| 69 | +var restoreArray = function(adjacentPairs) { |
| 70 | + var map = {}; |
| 71 | + for (var i = 0 ; i < adjacentPairs.length; i++) { |
| 72 | + map[adjacentPairs[i][0]] = map[adjacentPairs[i][0]] || []; |
| 73 | + map[adjacentPairs[i][1]] = map[adjacentPairs[i][1]] || []; |
| 74 | + map[adjacentPairs[i][0]].push(adjacentPairs[i][1]); |
| 75 | + map[adjacentPairs[i][1]].push(adjacentPairs[i][0]); |
| 76 | + } |
| 77 | + var root = Number(Object.keys(map).find(num => map[num].length === 1)); |
| 78 | + var res = [root]; |
| 79 | + var last = root; |
| 80 | + var now = map[root][0]; |
| 81 | + while (now !== undefined) { |
| 82 | + var next = map[now][0] === last ? map[now][1] : map[now][0]; |
| 83 | + res.push(now); |
| 84 | + last = now; |
| 85 | + now = next; |
| 86 | + } |
| 87 | + return res; |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +**Explain:** |
| 92 | + |
| 93 | +nope. |
| 94 | + |
| 95 | +**Complexity:** |
| 96 | + |
| 97 | +* Time complexity : O(n). |
| 98 | +* Space complexity : O(n). |
0 commit comments