From 2e5cb675cd80cd00ba7f744d22778f29c435339f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 28 Oct 2024 22:53:48 +0100 Subject: [PATCH] =?UTF-8?q?kt:=20add=20=C2=AB2501.=20Longest=20Square=20St?= =?UTF-8?q?reak=20in=20an=20Array=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/longest-square-streak-in-an-array/ Signed-off-by: Matej Focko --- kt/longest-square-streak-in-an-array.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 kt/longest-square-streak-in-an-array.kt diff --git a/kt/longest-square-streak-in-an-array.kt b/kt/longest-square-streak-in-an-array.kt new file mode 100644 index 0000000..dd0fbbf --- /dev/null +++ b/kt/longest-square-streak-in-an-array.kt @@ -0,0 +1,15 @@ +class Solution { + fun longestSquareStreak(nums: IntArray): Int { + nums.sort() + + val foundBest = mutableMapOf() + for (num in nums) { + val squared = num * num + foundBest.put(squared, 1 + foundBest.getOrDefault(num, 0)) + } + + return foundBest.values.filter { + it >= 2 + }.maxOrNull() ?: -1 + } +}