Skip to content

Conversation

@suhwan2004
Copy link
Collaborator

문제

Type Info
Time Complexity O(N)
Space Complexity O(1)
Algorithm for, slide window
Data Structure array

Constraints

n == nums.length
1<= k <= n <= 10^5
-10^4 <= nums[i] <= 10^4

Edge Case

  • nums 배열의 길이가 1이라면 그냥 nums[0] 반환
  • k가 1이라면 가장 큰 것 하나만 타겟하여 반환

풀이

nums 를 reduce 로 돌면서 다음과 같은 로직을 따른다.
sum이 k 갯수의 요소들이 더해지지 않았다면 현재 cur 요소를 더한다.
sum이 k 갯수의 요소들이 더해져있다면 다음 차례에서는 i-k 번재 인덱스의 값을 빼고 cur 요소를 더한다.

이후, prev(현재 가장 큰 평균값)과 sum/k (지금 회차에 발생된 평균값) 중 더 큰 것을 return한다.

return nums.reduce 시에, 가장 큰 평균값이 반환된다.

어려웠던 점

X

알게된 점

X

@suhwan2004 suhwan2004 self-assigned this Dec 30, 2024
@github-actions github-actions bot enabled auto-merge (squash) January 7, 2025 01:25
@github-actions github-actions bot merged commit 523bb61 into main Jan 7, 2025
2 checks passed
@BangDori BangDori deleted the suhwan2004/maximum-average-subarray-1 branch January 7, 2025 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants