Largest Sum of Averages

原创 2018年04月17日 10:52:51

We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?

Note that our partition must use every number in A, and that scores are not necessarily integers.

Example:
Input: 
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation: 
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.

动态规划的思想:

    想要知道n个数分成k组,各组平均数之和的最大值。

    就要看前T个数分成k-1组+剩余n-T个数分成一组,然后平均数之和的最大值。

    即

 for (int i = n - 1; i > 0; --i) {
     cur += A[i];
     memo[n][k] = max(memo[n][k], memo[i][k-1] + cur / (n - i));
 }

   如果以n为横坐标,k为纵坐标绘制表格:

 n=1n=2n=3n=4n=5
k=1     
k=2     
k=3     
k=4     
k=5     

    则框格(5,5)(n=5,k=5)由框格(4,4)计算得到

    (4,4)由(3,3)计算得到

    (3,3)由(2,2)计算得到


    递归实现。


  double memo[200][200];
    double largestSumOfAverages(vector<int>& A, int K) {
        memset(memo, 0, sizeof(memo));
        int N = A.size();
        double cur = 0;
        for (int i = 0; i < N; ++i) {
            cur += A[i];
            memo[i + 1][1] = cur / (i + 1);
        }
        return search(N, K, A);
    }

    double search(int n, int k, vector<int>& A) {
        if (memo[n][k] > 0) return memo[n][k];
        double cur = 0;
        for (int i = n - 1; i > 0; --i) {
            cur += A[i];
            memo[n][k] = max(memo[n][k], search(i, k - 1, A) + cur / (n - i));
        }
        return memo[n][k];
    }

leetcode 813. Largest Sum of Averages

leetcode 813. Largest Sum of Averages 原题地址:https://leetcode.com/problems/largest-sum-of-averages/...
  • birdreamer
  • birdreamer
  • 2018-04-08 12:01:19
  • 67

813-Largest Sum of Averages

Description: We partition a row of numbers A into at most K adjacent (non-empty) groups, then our s...
  • LaputaFallen
  • LaputaFallen
  • 2018-04-08 22:28:42
  • 63

Leetcode 813 Largest Sum of Averages (区间dp)

Description We partition a row of numbers A into at most K adjacent (non-empty) groups, then our...
  • qq_28954601
  • qq_28954601
  • 2018-04-08 15:14:53
  • 61

815. Bus Routes

We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e...
  • zjucor
  • zjucor
  • 2018-04-08 12:07:55
  • 46

813. Largest Sum of Averages

We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su...
  • zjucor
  • zjucor
  • 2018-04-08 12:06:23
  • 27

Leetcode 410. Split Array Largest Sum 划分数组 解题报告

1 解题思想首先声明下,本文这些内容,基本是dicuss的来的题目意思是,给一个数组,需要划分成m份,让你找出一种划分方式,使得划分成m份后,最大的那一个区间的数值最小这道题discuss上说的是2分...
  • MebiuW
  • MebiuW
  • 2016-10-02 17:04:16
  • 4203

算法细节系列(6):410. Split Array Largest Sum

410. Split Array Largest Sum这是一道颠覆我算法大厦的革命性题,它的求解思路对我来说非常独特,是一道值得深入研究探讨的题目,单独拿出来分析下,若有帮助甚好,来源于leetco...
  • u014688145
  • u014688145
  • 2017-04-07 15:33:40
  • 855

leetcode-410. Split Array Largest Sum

题目: Given an array which consists of non-negative integers and an integer m, you can split the arr...
  • caoyan_12727
  • caoyan_12727
  • 2016-10-22 11:58:41
  • 595

【Leetcode】410. Split Array Largest Sum

题目链接:https://leetcode.com/contest/7/problems/split-array-largest-sum/] 题目: Given an array which c...
  • yeqiuzs
  • yeqiuzs
  • 2016-10-02 13:19:52
  • 3865

最大和子数组与最大积子数组(Largest Sum Contiguous Subarray & Maximum Product Subarray)

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》...
  • bdss58
  • bdss58
  • 2014-10-16 13:25:41
  • 486
收藏助手
不良信息举报
您举报文章:Largest Sum of Averages
举报原因:
原因补充:

(最多只允许输入30个字)