LeetCode 2097: Valid Arrangement of Pairs Solution

Master LeetCode problem 2097 (Valid Arrangement of Pairs), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2097. Valid Arrangement of Pairs

Problem Explanation

Explanation

To solve this problem, we need to rearrange the pairs in a way that the end of one pair matches the start of the next pair. This can be achieved by constructing a directed graph where each pair represents a directed edge from the start to the end. Then we can perform a topological sort on the graph to find a valid arrangement.

  1. Create a directed graph where the nodes represent the start and end values of the pairs, and the edges represent the pairs.
  2. Perform a topological sort on the graph to find a valid arrangement of pairs.
  3. Return the pairs in the order obtained from the topological sort.

Time Complexity: O(n), where n is the number of pairs Space Complexity: O(n)

Solution Code

class Solution {
    public int[][] findValidArrangement(int[][] pairs) {
        Map<Integer, List<Integer>> graph = new HashMap<>();
        Map<Integer, Integer> indegree = new HashMap<>();
        
        for (int[] pair : pairs) {
            int start = pair[0];
            int end = pair[1];
            graph.putIfAbsent(start, new ArrayList<>());
            graph.get(start).add(end);
            indegree.put(end, indegree.getOrDefault(end, 0) + 1);
        }
        
        Queue<Integer> queue = new LinkedList<>();
        for (int key : graph.keySet()) {
            if (!indegree.containsKey(key)) {
                queue.offer(key);
            }
        }
        
        List<Integer> result = new ArrayList<>();
        while (!queue.isEmpty()) {
            int curr = queue.poll();
            result.add(curr);
            List<Integer> neighbors = graph.getOrDefault(curr, new ArrayList<>());
            for (int neighbor : neighbors) {
                indegree.put(neighbor, indegree.get(neighbor) - 1);
                if (indegree.get(neighbor) == 0) {
                    queue.offer(neighbor);
                }
            }
        }
        
        int[][] validArrangement = new int[pairs.length][2];
        for (int i = 0; i < result.size() - 1; i++) {
            validArrangement[i] = new int[]{result.get(i), result.get(i + 1)};
        }
        
        return validArrangement;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2097 (Valid Arrangement of Pairs)?

This page provides optimized solutions for LeetCode problem 2097 (Valid Arrangement of Pairs) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2097 (Valid Arrangement of Pairs)?

The time complexity for LeetCode 2097 (Valid Arrangement of Pairs) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2097 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2097 in Java, C++, or Python.

Back to LeetCode Solutions