kt: add «2501. Longest Square Streak in an Array»

URL:	https://leetcode.com/problems/longest-square-streak-in-an-array/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-28 22:53:48 +01:00
parent d1d9ad0c74
commit 2e5cb675cd
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,15 @@
class Solution {
fun longestSquareStreak(nums: IntArray): Int {
nums.sort()
val foundBest = mutableMapOf<Int, Int>()
for (num in nums) {
val squared = num * num
foundBest.put(squared, 1 + foundBest.getOrDefault(num, 0))
}
return foundBest.values.filter {
it >= 2
}.maxOrNull() ?: -1
}
}