[Leetcode - Easy] Maximum Average Subarray I #37
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
문제
Constraints
n == nums.length
1<= k <= n <= 10^5
-10^4 <= nums[i] <= 10^4
Edge Case
풀이
nums 를 reduce 로 돌면서 다음과 같은 로직을 따른다.
sum이 k 갯수의 요소들이 더해지지 않았다면 현재 cur 요소를 더한다.
sum이 k 갯수의 요소들이 더해져있다면 다음 차례에서는 i-k 번재 인덱스의 값을 빼고 cur 요소를 더한다.
이후, prev(현재 가장 큰 평균값)과 sum/k (지금 회차에 발생된 평균값) 중 더 큰 것을 return한다.
return nums.reduce 시에, 가장 큰 평균값이 반환된다.
어려웠던 점
X
알게된 점
X