Matej Focko
2e5cb675cd
URL: https://leetcode.com/problems/longest-square-streak-in-an-array/ Signed-off-by: Matej Focko <me@mfocko.xyz>
15 lines
381 B
Kotlin
15 lines
381 B
Kotlin
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
|
|
}
|
|
}
|