kt: add «2097. Valid Arrangement of Pairs»

URL:	https://leetcode.com/problems/valid-arrangement-of-pairs/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-30 11:25:07 +01:00
parent f878952bbc
commit 03db4fc1c3
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,56 @@
class Solution {
private data class Graph(
val neighbors: Map<Int, ArrayDeque<Int>>,
val ingoing: Map<Int, Int>,
val outgoing: Map<Int, Int>,
)
private fun constructGraph(pairs: Array<IntArray>): Graph {
val neighbors = mutableMapOf<Int, ArrayDeque<Int>>()
val ingoing = mutableMapOf<Int, Int>()
val outgoing = mutableMapOf<Int, Int>()
pairs.forEach { pair ->
val (left, right) = pair[0] to pair[1]
neighbors.getOrPut(left) { ArrayDeque<Int>() }.add(right)
outgoing[left] = 1 + outgoing.getOrDefault(left, 0)
ingoing[right] = 1 + ingoing.getOrDefault(right, 0)
}
return Graph(neighbors, ingoing, outgoing)
}
private fun findStart(g: Graph): Int =
g.outgoing.keys.firstOrNull {
g.outgoing[it] == 1 + g.ingoing.getOrDefault(it, 0)
} ?: g.outgoing.keys.first()
private fun visit(
g: Graph,
path: MutableList<Int>,
u: Int,
) {
val neighbors = g.neighbors.getOrDefault(u, null)
while (neighbors != null && neighbors.isNotEmpty()) {
val v = neighbors.removeFirst()
visit(g, path, v)
}
path.add(u)
}
fun validArrangement(pairs: Array<IntArray>): Array<IntArray> {
val g = constructGraph(pairs)
val start = findStart(g)
val path = mutableListOf<Int>()
visit(g, path, start)
path.reverse()
return Array(path.size - 1) { i ->
intArrayOf(path[i], path[i + 1])
}
}
}