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:
parent
970f7c1060
commit
1524366e77
1 changed files with 29 additions and 0 deletions
29
kt/find-the-power-of-k-size-subarrays-i.kt
Normal file
29
kt/find-the-power-of-k-size-subarrays-i.kt
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue