From 97f7ee548000db0c0aede715787cf147680a4633 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 26 Nov 2024 15:16:27 +0100 Subject: [PATCH] =?UTF-8?q?kt:=20add=20=C2=AB2924.=20Find=20Champion=20II?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/find-champion-ii/ Signed-off-by: Matej Focko --- kt/find-champion-ii.kt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 kt/find-champion-ii.kt diff --git a/kt/find-champion-ii.kt b/kt/find-champion-ii.kt new file mode 100644 index 0000000..43065e8 --- /dev/null +++ b/kt/find-champion-ii.kt @@ -0,0 +1,21 @@ +class Solution { + fun findChampion( + n: Int, + edges: Array, + ): 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 + } + } +}