kt: add «2924. Find Champion II»

URL:	https://leetcode.com/problems/find-champion-ii/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-26 15:16:27 +01:00
parent 7dee18a1ca
commit 97f7ee5480
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

21
kt/find-champion-ii.kt Normal file
View file

@ -0,0 +1,21 @@
class Solution {
fun findChampion(
n: Int,
edges: Array<IntArray>,
): Int {
val degrees = IntArray(n)
for (edge in edges) {
degrees[edge[1]] += 1
}
val candidates =
degrees.withIndex().filter {
it.value == 0
}.take(2)
return when {
candidates.size == 1 -> candidates.first().index
else -> -1
}
}
}