-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathRestore the Array From Adjacent Pairs.java
36 lines (32 loc) · 1.19 KB
/
Restore the Array From Adjacent Pairs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Runtime: 122 ms (Top 96.49%) | Memory: 87.3 MB (Top 95.99%)
class Solution {
public int[] restoreArray(int[][] adjacentPairs) {
// Build an adjacency list graph.
Map<Integer, Queue<Integer>> iToPairs = new HashMap<>();
for (int[] pair : adjacentPairs) {
iToPairs.computeIfAbsent(pair[0], k -> new ArrayDeque<>()).add(pair[1]);
iToPairs.computeIfAbsent(pair[1], k -> new ArrayDeque<>()).add(pair[0]);
}
// Find an item that has only one neighbour.
int start = -1;
for (int i : iToPairs.keySet()) {
if (iToPairs.get(i).size() == 1) {
start = i;
break;
}
}
// Traverse the graph in a linked-list fashion.
int n = iToPairs.size();
int writeIdx = 0;
int[] restored = new int[n];
restored[writeIdx++] = start;
while (writeIdx < n) {
int next = iToPairs.get(start).remove();
iToPairs.remove(start);
iToPairs.get(next).remove(start); // Remove the loop back to the current start.
restored[writeIdx++] = next;
start = next;
}
return restored;
}
}