• 문제 링크
Maximum Average Subarray I - LeetCode
Can you solve this real interview question? Maximum Average Subarray I - You are given an integer array nums consisting of n elements, and an integer k. Find a contiguous subarray whose length is equal to k that has the maximum average value and return thi
leetcode.com
• 풀이 코드
public class Solution {
public double findMaxAverage(int[] nums, int k) {
int sum = 0;
for (int i = 0; i < k; i++) sum += nums[i];
int max = sum;
for (int i = k; i < nums.length; i++) max = Math.max(max, sum += nums[i] - nums[i - k]);
return (double) max / k;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 653. Two Sum IV - Input is a BST - Java (0) | 2026.09.17 |
|---|---|
| [LeetCode] 645. Set Mismatch - Java (0) | 2026.09.16 |
| [LeetCode] 637. Average of Levels in Binary Tree - Java (0) | 2026.09.14 |
| [LeetCode] 628. Maximum Product of Three Numbers - Java (0) | 2026.09.13 |
| [LeetCode] 617. Merge Two Binary Trees - Java (0) | 2026.09.12 |
댓글