g2001_2100.s2090_k_radius_subarray_averages.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2001_2100.s2090_k_radius_subarray_averages
// #Medium #Array #Sliding_Window #2023_06_27_Time_874_ms_(85.63%)_Space_54.7_MB_(40.94%)
class Solution {
fun getAverages(nums: IntArray, k: Int): IntArray {
// initialize result array with -1
val res = IntArray(nums.size)
res.fill(-1)
if (nums.size <= k * 2) {
// return if not enough elements
return res
}
var sum: Long = 0
val range = 2 * k + 1L
// take sum of all elements from 0 to k*2 index
for (i in 0..2 * k) {
sum += nums[i].toLong()
}
// update first valid avg
res[k] = (sum / range).toInt()
// update other valid averages using sliding window
for (i in k + 1 until nums.size - k) {
sum = sum - nums[i - k - 1] + nums[i + k]
res[i] = (sum / range).toInt()
}
return res
}
}