from collections import defaultdict
class Solution:
def restoreArray(self, adjacentPairs):
# Build adjacency list
adj = defaultdict(list)
for a, b in adjacentPairs:
adj[a].append(b)
adj[b].append(a)
# Find the start (degree 1)
for k in adj:
if len(adj[k]) == 1:
start = k
break
res = [start]
prev = None
curr = start
while len(res) < len(adj):
for neighbor in adj[curr]:
if neighbor != prev:
res.append(neighbor)
prev, curr = curr, neighbor
break
return res
class Solution {
public:
vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {
unordered_map<int, vector<int>> adj;
for (auto& p : adjacentPairs) {
adj[p[0]].push_back(p[1]);
adj[p[1]].push_back(p[0]);
}
int start;
for (auto& kv : adj) {
if (kv.second.size() == 1) {
start = kv.first;
break;
}
}
vector<int> res = {start};
int prev = INT_MAX, curr = start;
while (res.size() < adj.size()) {
for (int neighbor : adj[curr]) {
if (neighbor != prev) {
res.push_back(neighbor);
prev = curr;
curr = neighbor;
break;
}
}
}
return res;
}
};
class Solution {
public int[] restoreArray(int[][] adjacentPairs) {
Map<Integer, List<Integer>> adj = new HashMap<>();
for (int[] pair : adjacentPairs) {
adj.computeIfAbsent(pair[0], k -> new ArrayList<>()).add(pair[1]);
adj.computeIfAbsent(pair[1], k -> new ArrayList<>()).add(pair[0]);
}
int start = 0;
for (int key : adj.keySet()) {
if (adj.get(key).size() == 1) {
start = key;
break;
}
}
int n = adj.size();
int[] res = new int[n];
res[0] = start;
int prev = Integer.MAX_VALUE, curr = start;
for (int i = 1; i < n; ++i) {
for (int neighbor : adj.get(curr)) {
if (neighbor != prev) {
res[i] = neighbor;
prev = curr;
curr = neighbor;
break;
}
}
}
return res;
}
}
var restoreArray = function(adjacentPairs) {
const adj = new Map();
for (const [a, b] of adjacentPairs) {
if (!adj.has(a)) adj.set(a, []);
if (!adj.has(b)) adj.set(b, []);
adj.get(a).push(b);
adj.get(b).push(a);
}
let start;
for (const [k, v] of adj.entries()) {
if (v.length === 1) {
start = k;
break;
}
}
const res = [start];
let prev = null, curr = start;
while (res.length < adj.size) {
for (const neighbor of adj.get(curr)) {
if (neighbor !== prev) {
res.push(neighbor);
prev = curr;
curr = neighbor;
break;
}
}
}
return res;
};
You are given a list of pairs called adjacentPairs
, where each pair [u, v]
means that the elements u
and v
are adjacent in some original array (but the order is unknown). Your task is to restore and return the original array.
Key details:
adjacentPairs
(either as [u, v]
or [v, u]
).adjacentPairs
.
At first glance, the problem seems to require checking all possible orderings of the elements to see which one fits the adjacency requirements. However, this would be highly inefficient, especially for larger inputs.
Instead of brute-forcing all permutations, we can reason about the structure: each number is connected to its neighbors. If we think of the pairs as edges in a graph, the original array is like a path through the graph where each node (number) is connected to its neighbors.
The ends of the array (first and last elements) must have only one neighbor, while all other elements have two neighbors. This insight allows us to start from one end and reconstruct the array step by step, using the adjacency information.
We can solve the problem efficiently by following these steps:
We use a hash map for the adjacency list because it allows O(1) lookups when finding neighbors, making the overall process efficient.
Let's work through an example with adjacentPairs = [[2,1],[3,4],[3,2]]
.
At each step, we avoid going backward by skipping the element we just came from.
Brute-force approach:
- Trying all permutations would be O(N!), which is infeasible for large N.
Optimized approach (using adjacency list):
The key insight for this problem is recognizing that the array's structure can be represented as a path in a graph, with the ends having only one neighbor. By building an adjacency list and starting from an endpoint, we can efficiently reconstruct the array in linear time. This approach avoids brute-force permutations and leverages the properties of adjacency to find the unique solution elegantly and efficiently.