Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc3a764

Browse files
committed
feat: add maximum average subarray i
1 parent 1ef1db7 commit cc3a764

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

‎src/sliding_window/__init__.py

Whitespace-only changes.

‎src/sliding_window/maximum_average_subarray/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findMaxAverage(self, nums: List[int], k: int) -> float:
6+
total = sum(nums[:k])
7+
max_total = total
8+
9+
for index in range(k, len(nums)):
10+
total -= nums[index - k]
11+
total += nums[index]
12+
max_total = max(total, max_total)
13+
14+
return max_total / k

‎tests/test_max_average_subarray.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
from src.sliding_window.maximum_average_subarray.solution import Solution
3+
4+
5+
@pytest.mark.parametrize(
6+
"nums, k, expected",
7+
[([1, 12, -5, -6, 50, 3], 4, 12.75), ([5], 1, 5)],
8+
)
9+
def test_max_average_subarray(nums, k, expected):
10+
solution = Solution()
11+
assert solution.findMaxAverage(nums, k) == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /