Problem:
Given array of integers, find the maximal possible sum of some of its k consecutive elements.
Example
For inputArray = [2, 3, 5, 1, 6] and k = 2, the output should be
arrayMaxConsecutiveSum(inputArray, k) = 8.
All possible sums of 2 consecutive elements are:
- 2 + 3 = 5;
- 3 + 5 = 8;
- 5 + 1 = 6;
- 1 + 6 = 7.
Thus, the answer is 8.
-Summary-
1. Set the max_sum as first k consecutive sum from the inputArray.
2. In the for loop, keep check and compare the previous sum and current sum. (Then save the bigger value in the max_sum)
3. Return max_sum
처음엔 이중 for loop을 써서 풀었는데, test case 중 한개가 time limit 에 걸려 자꾸 통과를 못하여 한참을 헤맸다. 그러나 discussion에서의 도움으로 아이디어를 얻고 다시 새로 풀어 통과하게 되었다.
모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]
'CodeSignal > Arcade' 카테고리의 다른 글
CodeSignal [39/60] knapsackLight (0) | 2020.05.26 |
---|---|
CodeSignal [38/60] growingPlant (0) | 2020.05.25 |
CodeSignal [36/60] differentSymbolsNaive (0) | 2020.05.25 |
CodeSignal [35/60] firstDigit (0) | 2020.05.25 |
CodeSignal [34/60] extractEachKth (0) | 2020.05.24 |
댓글