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:
parent
d1d9ad0c74
commit
2e5cb675cd
1 changed files with 15 additions and 0 deletions
15
kt/longest-square-streak-in-an-array.kt
Normal file
15
kt/longest-square-streak-in-an-array.kt
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue