Skip to content

Commit a816d0d

Browse files
add 1743
1 parent bdf3754 commit a816d0d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1743|[Restore the Array From Adjacent Pairs](https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1743.java) ||Medium|Greedy|
1112
|1742|[Maximum Number of Balls in a Box](https://leetcode.com/problems/maximum-number-of-balls-in-a-box/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1742.java) ||Easy|Array|
1213
|1736|[Latest Time by Replacing Hidden Digits](https://leetcode.com/problems/latest-time-by-replacing-hidden-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1736.java) ||Easy|String, Greedy|
1314
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1733.java) ||Medium|Array, Greedy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
public class _1743 {
6+
public static class Solution1 {
7+
public int[] restoreArray(int[][] adjacentPairs) {
8+
Map<Integer, Integer> map = new HashMap<>();
9+
Map<Integer, List<Integer>> map2 = new HashMap<>();
10+
for (int[] pair : adjacentPairs) {
11+
int num1 = pair[0];
12+
int num2 = pair[1];
13+
map.put(num1, map.getOrDefault(num1, 0) + 1);
14+
map.put(num2, map.getOrDefault(num2, 0) + 1);
15+
if (!map2.containsKey(num1)) {
16+
map2.put(num1, new ArrayList<>());
17+
}
18+
map2.get(num1).add(num2);
19+
if (!map2.containsKey(num2)) {
20+
map2.put(num2, new ArrayList<>());
21+
}
22+
map2.get(num2).add(num1);
23+
}
24+
int first = Integer.MAX_VALUE;
25+
for (int key : map.keySet()) {
26+
if (map.get(key) == 1) {
27+
first = key;
28+
break;
29+
}
30+
}
31+
int[] original = new int[map.size()];
32+
Set<Integer> set = new HashSet<>();
33+
original[0] = first;
34+
set.add(first);
35+
for (int i = 1; i < map.size(); i++) {
36+
int prev = original[i - 1];
37+
List<Integer> connected = map2.get(prev);
38+
for (int conn : connected) {
39+
if (set.add(conn)) {
40+
original[i] = conn;
41+
break;
42+
}
43+
}
44+
}
45+
return original;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)