> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Largest Sum of Averages Python Solution

> Tested Python solution for LeetCode 813 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 813, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/largest-sum-of-averages/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 813   # by problem number
lcpy gen -s largest_sum_of_averages   # by problem name
```

## Problem

You are given an integer array `nums` and an integer `k`. You can partition the array into at most `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.

Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.

Return the maximum **score** you can achieve of all the possible partitions. Answers within `10^-6` of the actual answer will be accepted.

### Examples

```
Input: nums = [9,1,2,3,9], k = 3
Output: 20.00000
Explanation: The best choice is to partition nums into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
```

```
Input: nums = [1,2,3,4,5,6,7], k = 4
Output: 20.50000
```

### Constraints

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 10^4`
* `1 <= k <= nums.length`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_sum_of_averages/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_sum_of_averages/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2 * k)
    # Space: O(n)
    def largest_sum_of_averages(self, nums: list[int], k: int) -> float:
        n = len(nums)
        prefix = [0.0] * (n + 1)
        for i, value in enumerate(nums):
            prefix[i + 1] = prefix[i] + value

        def average(i: int, j: int) -> float:
            return (prefix[j] - prefix[i]) / (j - i)

        # best[i] = best score achievable for nums[i:] with the parts still available;
        # index n is the empty suffix, worth 0
        best = [average(i, n) for i in range(n)] + [0.0]
        for _ in range(2, k + 1):
            # ascending so best[end] still holds the (parts - 1) values
            for i in range(n):
                best[i] = max(average(i, end) + best[end] for end in range(i + 1, n + 1))
        return best[0]
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 \* k) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.