Created
January 10, 2020 06:38
-
-
Save liketheflower/39c132c12fd2b0a0d8fc9b1ee91ad3b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import lru_cache | |
class Solution: | |
def largestSumOfAverages(self, a: List[int], k: int) -> float: | |
cusum = list(itertools.accumulate([0]+a)) | |
@lru_cache(None) | |
def dp(i, k): | |
#if i>=len(a):return 0 | |
if k == 1:return (cusum[-1]-cusum[i])/(len(a)-i) | |
return max((cusum[j+1]-cusum[i])/(j-i+1) + dp(j+1, k-1) for j in range(i, len(a)-k+1)) | |
return dp(0, k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment