kt: add «3254. Find the Power of K-Size Subarrays I»

URL:	https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-16 21:20:02 +01:00
parent 970f7c1060
commit 1524366e77
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,29 @@
class Solution {
fun resultsArray(
nums: IntArray,
k: Int,
): IntArray {
if (k == 1) {
return nums
}
val result = IntArray(nums.size - k + 1) { -1 }
(0..<nums.size - 1)
.scan(1) { consecutive, i ->
if (nums[i] == nums[i + 1] - 1) {
consecutive + 1
} else {
1
}
}
.withIndex()
.drop(1)
.filter {
it.value >= k
}
.forEach {
result[it.index - k + 1] = nums[it.index]
}
return result
}
}