Skip to content

Instantly share code, notes, and snippets.

@liketheflower
Created January 10, 2020 06:38
Show Gist options
  • Save liketheflower/39c132c12fd2b0a0d8fc9b1ee91ad3b5 to your computer and use it in GitHub Desktop.
Save liketheflower/39c132c12fd2b0a0d8fc9b1ee91ad3b5 to your computer and use it in GitHub Desktop.
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