首页 > 其他 > 详细

Maximum Sum Of K Consecutive Elements In Array

时间:2020-09-04 08:33:55      阅读:37      评论:0      收藏:0      [点我收藏+]
package _interview_question

/**
 * Given an array of integers size ‘n‘
Our aim is to calculate the maximum sum of ‘k‘
consecutive elements in the array
//input
const arr = [100,200,300,400]
k = 2
//Output
700
 * */
class Solution12 {
    /*
    * Solution: Sliding Window, Time complexity:O(n), Space complexity:O(1)
    * */
    fun maximumSumOfKConsecutive(nums: IntArray, k: Int): Int {
        if (nums.isEmpty()) {
            return 0
        }
        var max = 0
        var windowSum = 0
        for (i in 0 until k) {
            windowSum += nums[i]
        }
        for (i in k until nums.size) {
            /*
            * set up the sum of range, for example: 1,2,3,4; k=1
            * windowSum = 1+2 after below, then add 3 and minus 1 at first loop and keep looping
            * */
            windowSum += nums[i]
            windowSum -= nums[i - k]
            max = Math.max(windowSum, max)
        }
        return max
    }
}

 

Maximum Sum Of K Consecutive Elements In Array

原文:https://www.cnblogs.com/johnnyzhao/p/13611112.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!