Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ae644af

Browse files
committed
Add solution #1743
1 parent 1676eee commit ae644af

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,514 LeetCode solutions in JavaScript
1+
# 1,515 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1342,6 +1342,7 @@
13421342
1738|[Find Kth Largest XOR Coordinate Value](./solutions/1738-find-kth-largest-xor-coordinate-value.js)|Medium|
13431343
1739|[Building Boxes](./solutions/1739-building-boxes.js)|Hard|
13441344
1742|[Maximum Number of Balls in a Box](./solutions/1742-maximum-number-of-balls-in-a-box.js)|Easy|
1345+
1743|[Restore the Array From Adjacent Pairs](./solutions/1743-restore-the-array-from-adjacent-pairs.js)|Medium|
13451346
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13461347
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13471348
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1743. Restore the Array From Adjacent Pairs
3+
* https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/
4+
* Difficulty: Medium
5+
*
6+
* There is an integer array nums that consists of n unique elements, but you have
7+
* forgotten it. However, you do remember every pair of adjacent elements in nums.
8+
*
9+
* You are given a 2D integer array adjacentPairs of size n - 1 where each
10+
* adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.
11+
*
12+
* It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in
13+
* adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear
14+
* in any order.
15+
*
16+
* Return the original array nums. If there are multiple solutions, return any of them.
17+
*/
18+
19+
/**
20+
* @param {number[][]} adjacentPairs
21+
* @return {number[]}
22+
*/
23+
var restoreArray = function(adjacentPairs) {
24+
const graph = new Map();
25+
for (const [u, v] of adjacentPairs) {
26+
graph.set(u, (graph.get(u) || []).concat(v));
27+
graph.set(v, (graph.get(v) || []).concat(u));
28+
}
29+
30+
let start;
31+
for (const [node, neighbors] of graph) {
32+
if (neighbors.length === 1) {
33+
start = node;
34+
break;
35+
}
36+
}
37+
38+
const result = [start];
39+
let prev = start;
40+
let curr = graph.get(start)[0];
41+
42+
while (graph.get(curr).length > 1) {
43+
result.push(curr);
44+
const next = graph.get(curr).find(n => n !== prev);
45+
prev = curr;
46+
curr = next;
47+
}
48+
result.push(curr);
49+
50+
return result;
51+
};

0 commit comments

Comments
 (0)