Advertisement

Restore the Array From Adjacent Pairs - LeetCode 1743 Solution

Restore the Array From Adjacent Pairs - Complete Solution Guide

Restore the Array From Adjacent Pairs is LeetCode problem 1743, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 . You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [u i , v i ] indicates that the elements u i and v i are adjacent in nums . 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

Detailed Explanation

The problem presents a scenario where we've forgotten an array of unique integers (`nums`), but we remember all pairs of adjacent elements in the array. We are given these pairs in the `adjacentPairs` array. The goal is to reconstruct the original `nums` array given the `adjacentPairs`. The pairs can be in any order (e.g., `[a, b]` or `[b, a]`). We are guaranteed that a solution exists, and if multiple solutions exist, we can return any of them.

Solution Approach

The provided solution uses a graph-based approach. First, it builds an adjacency list representation of the graph from the `adjacentPairs`. Then, it finds a starting node (a node with only one neighbor, representing one end of the array). Finally, it traverses the graph, reconstructing the array by following the neighbors, while avoiding revisiting the previous node.

Step-by-Step Algorithm

  1. Step 1: Create an adjacency list (graph) from the `adjacentPairs`. For each pair `[u, v]`, add `v` to the list of neighbors of `u`, and `u` to the list of neighbors of `v`.
  2. Step 2: Find a starting node. Iterate through the graph and identify a node that has only one neighbor (degree 1). This node will be one of the ends of the original array.
  3. Step 3: Initialize an array `result` of the correct size to store the reconstructed array. Also, initialize `curr` to the starting node and `prev` to a value that is guaranteed to be different from any node value (e.g., infinity or a very large number) to avoid going back to the starting node in the first iteration.
  4. Step 4: Iterate through the `result` array. In each iteration, set `result[i]` to `curr`. Then, find the neighbor of `curr` that is not equal to `prev`. Update `prev` to `curr` and `curr` to the found neighbor.
  5. Step 5: Return the `result` array.

Key Insights

  • Insight 1: The `adjacentPairs` effectively form a graph where each number is a node and each pair represents an edge. Since each number in `nums` is unique, there are exactly two nodes (the start and end) which have only one neighbor, corresponding to the first and last elements of the original array.
  • Insight 2: We can use a hash map (or dictionary) to represent the adjacency list of the graph. This allows us to quickly find the neighbors of a given number.
  • Insight 3: Since there are two possible start/end points in the array (each having degree 1), and each adjacent pair could be stored as `[u, v]` or `[v, u]`, there could be multiple solutions to the problem. The algorithm only needs to return any one of the valid solutions.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Depth-First Search.

Companies

Asked at: Robinhood.