|
| 1 | +/** |
| 2 | + * [1743] Restore the Array From Adjacent Pairs |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * 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. |
| 6 | + * 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. |
| 7 | + * Return the original array nums. If there are multiple solutions, return any of them. |
| 8 | + * |
| 9 | + * Example 1: |
| 10 | + * |
| 11 | + * Input: adjacentPairs = [[2,1],[3,4],[3,2]] |
| 12 | + * Output: [1,2,3,4] |
| 13 | + * Explanation: This array has all its adjacent pairs in adjacentPairs. |
| 14 | + * Notice that adjacentPairs[i] may not be in left-to-right order. |
| 15 | + * |
| 16 | + * Example 2: |
| 17 | + * |
| 18 | + * Input: adjacentPairs = [[4,-2],[1,4],[-3,1]] |
| 19 | + * Output: [-2,4,1,-3] |
| 20 | + * Explanation: There can be negative numbers. |
| 21 | + * Another solution is [-3,1,4,-2], which would also be accepted. |
| 22 | + * |
| 23 | + * Example 3: |
| 24 | + * |
| 25 | + * Input: adjacentPairs = [[100000,-100000]] |
| 26 | + * Output: [100000,-100000] |
| 27 | + * |
| 28 | + * |
| 29 | + * Constraints: |
| 30 | + * |
| 31 | + * nums.length == n |
| 32 | + * adjacentPairs.length == n - 1 |
| 33 | + * adjacentPairs[i].length == 2 |
| 34 | + * 2 <= n <= 10^5 |
| 35 | + * -10^5 <= nums[i], ui, vi <= 10^5 |
| 36 | + * There exists some nums that has adjacentPairs as its pairs. |
| 37 | + * |
| 38 | + */ |
| 39 | +pub struct Solution {} |
| 40 | + |
| 41 | +// problem: https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/ |
| 42 | +// discuss: https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/discuss/?currentPage=1&orderBy=most_votes&query= |
| 43 | + |
| 44 | +// submission codes start here |
| 45 | + |
| 46 | +impl Solution { |
| 47 | + pub fn restore_array(adjacent_pairs: Vec<Vec<i32>>) -> Vec<i32> { |
| 48 | + let mut mp = std::collections::HashMap::<i32, Vec<i32>>::new(); |
| 49 | + |
| 50 | + for p in adjacent_pairs { |
| 51 | + mp.entry(p[0]).or_insert(vec![]).push(p[1]); |
| 52 | + mp.entry(p[1]).or_insert(vec![]).push(p[0]); |
| 53 | + } |
| 54 | + |
| 55 | + let mut start = 0; |
| 56 | + for (k, v) in &mp { |
| 57 | + if v.len() == 2 { |
| 58 | + continue; |
| 59 | + } |
| 60 | + start = *k; |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + let mut s = std::collections::HashSet::new(); |
| 65 | + let mut result = vec![start]; |
| 66 | + s.insert(start); |
| 67 | + |
| 68 | + while mp.contains_key(&start) { |
| 69 | + let mut v = mp.get(&start).unwrap(); |
| 70 | + let mut done = true; |
| 71 | + for u in mp.get(&start).unwrap() { |
| 72 | + if s.contains(&u) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + start = *u; |
| 76 | + result.push(*u); |
| 77 | + s.insert(*u); |
| 78 | + done = false; |
| 79 | + break; |
| 80 | + } |
| 81 | + if done { |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + result |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// submission codes end |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::*; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_1743_example_1() { |
| 98 | + let adjacent_pairs = vec![vec![2, 1], vec![3, 4], vec![3, 2]]; |
| 99 | + |
| 100 | + let result = vec![1, 2, 3, 4]; |
| 101 | + |
| 102 | + assert_eq_sorted!(Solution::restore_array(adjacent_pairs), result); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_1743_example_2() { |
| 107 | + let adjacent_pairs = vec![vec![4, -2], vec![1, 4], vec![-3, 1]]; |
| 108 | + |
| 109 | + let result = vec![-2, 4, 1, -3]; |
| 110 | + |
| 111 | + assert_eq_sorted!(Solution::restore_array(adjacent_pairs), result); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_1743_example_3() { |
| 116 | + let adjacent_pairs = vec![vec![100000, -100000]]; |
| 117 | + |
| 118 | + let result = vec![100000, -100000]; |
| 119 | + |
| 120 | + assert_eq_sorted!(Solution::restore_array(adjacent_pairs), result); |
| 121 | + } |
| 122 | +} |
0 commit comments