/**
* Problem: LeetCode 2097 - Valid Arrangement of Pairs
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
class Solution {
public int[][] validArrangement(int[][] pairs) {
// Graph: node -> edges (adjacency list), values 10^9 tak ja sakti hain isliye HashMap
Map<Integer, Deque<Integer>> graph = new HashMap<>();
Map<Integer, Integer> outDeg = new HashMap<>();
Map<Integer, Integer> inDeg = new HashMap<>();
for (int[] p : pairs) {
graph.computeIfAbsent(p[0], k -> new ArrayDeque<>()).add(p[1]);
outDeg.merge(p[0], 1, Integer::sum);
inDeg.merge(p[1], 1, Integer::sum);
}
// Start node: jiska out - in == 1. Agar aisa koi nahi, toh Euler circuit hai, koi bhi node chalega.
int start = pairs[0][0];
for (int node : outDeg.keySet()) {
if (outDeg.get(node) - inDeg.getOrDefault(node, 0) == 1) {
start = node;
break;
}
}
// Hierholzer's algorithm (iterative, recursion se stack overflow na ho)
List<Integer> path = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>();
stack.push(start);
while (!stack.isEmpty()) {
int u = stack.peek();
Deque<Integer> edges = graph.get(u);
if (edges != null && !edges.isEmpty()) {
stack.push(edges.poll());
} else {
path.add(stack.pop());
}
}
// path ulta bana hai (end se start), isliye reverse karo
Collections.reverse(path);
int[][] res = new int[pairs.length][2];
for (int i = 0; i < pairs.length; i++) {
res[i][0] = path.get(i);
res[i][1] = path.get(i + 1);
}
return res;
}
}
"""
Problem: LeetCode 2097 - Valid Arrangement of Pairs
Difficulty: Hard
Language: Python 3
Author: CodeByTushu
"""
from collections import defaultdict
class Solution:
def validArrangement(self, pairs: list[list[int]]) -> list[list[int]]:
graph = defaultdict(list)
out_deg = defaultdict(int)
in_deg = defaultdict(int)
for u, v in pairs:
graph[u].append(v)
out_deg[u] += 1
in_deg[v] += 1
start = pairs[0][0]
for node, od in out_deg.items():
if od - in_deg[node] == 1:
start = node
break
path = []
stack = [start]
while stack:
u = stack[-1]
if graph[u]:
stack.append(graph[u].pop())
else:
path.append(stack.pop())
path.reverse()
return [[path[i], path[i + 1]] for i in range(len(pairs))]
/**
* Problem: LeetCode 2097 - Valid Arrangement of Pairs
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> validArrangement(vector<vector<int>>& pairs) {
unordered_map<int, vector<int>> graph;
unordered_map<int, int> outDeg, inDeg;
graph.reserve(pairs.size());
for (const auto& p : pairs) {
graph[p[0]].push_back(p[1]);
outDeg[p[0]]++;
inDeg[p[1]]++;
}
int start = pairs[0][0];
for (const auto& kv : outDeg) {
if (kv.second - inDeg[kv.first] == 1) {
start = kv.first;
break;
}
}
vector<int> path;
path.reserve(pairs.size() + 1);
vector<int> st;
st.push_back(start);
while (!st.empty()) {
int u = st.back();
auto it = graph.find(u);
if (it != graph.end() && !it->second.empty()) {
int v = it->second.back();
it->second.pop_back();
st.push_back(v);
} else {
path.push_back(u);
st.pop_back();
}
}
reverse(path.begin(), path.end());
vector<vector<int>> res(pairs.size(), vector<int>(2));
for (size_t i = 0; i < pairs.size(); i++) {
res[i][0] = path[i];
res[i][1] = path[i + 1];
}
return res;
}
};
/**
* Problem: LeetCode 2097 - Valid Arrangement of Pairs
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[][]} pairs
* @return {number[][]}
*/
var validArrangement = function(pairs) {
const graph = new Map();
const outDeg = new Map();
const inDeg = new Map();
for (const [u, v] of pairs) {
if (!graph.has(u)) graph.set(u, []);
graph.get(u).push(v);
outDeg.set(u, (outDeg.get(u) || 0) + 1);
inDeg.set(v, (inDeg.get(v) || 0) + 1);
}
let start = pairs[0][0];
for (const [node, od] of outDeg.entries()) {
if (od - (inDeg.get(node) || 0) === 1) {
start = node;
break;
}
}
const path = [];
const stack = [start];
while (stack.length > 0) {
const u = stack[stack.length - 1];
const edges = graph.get(u);
if (edges && edges.length > 0) {
stack.push(edges.pop());
} else {
path.push(stack.pop());
}
}
path.reverse();
const res = new Array(pairs.length);
for (let i = 0; i < pairs.length; i++) {
res[i] = [path[i], path[i + 1]];
}
return res;
};