[LeetCode] 643. Maximum Average Subarray I(Kotlin)

0

LeetCode

목록 보기
29/58
post-thumbnail
post-custom-banner

[LeetCode] 643. Maximum Average Subarray I(Kotlin)

풀이

class Solution {
    fun findMaxAverage(nums: IntArray, k: Int): Double {
        var sum:Long = 0L
        for(i in 0 until k) sum += nums[i].toLong()

        var maxAvg:Double = sum.toDouble() / k.toDouble()
        for(i in k until nums.size){
            //Sliding Window
            sum -= nums[i-k].toLong()
            sum += nums[i].toLong()

            maxAvg = max(maxAvg, sum.toDouble() / k.toDouble())
        }
        return maxAvg
    }
}
profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글