All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0813_largest_sum_of_averages.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0801_0900.s0813_largest_sum_of_averages;

// #Medium #Array #Dynamic_Programming #2022_03_23_Time_4_ms_(97.01%)_Space_42.5_MB_(18.51%)

/**
 * 813 - Largest Sum of Averages\.
 *
 * Medium
 *
 * 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.
 *
 * **Example 1:**
 *
 * **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. 
 *
 * We could have also partitioned nums into [9, 1], [2], [3, 9], for example. 
 *
 * That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
 *
 * **Example 2:**
 *
 * **Input:** nums = [1,2,3,4,5,6,7], k = 4
 *
 * **Output:** 20.50000
 *
 * **Constraints:**
 *
 * *   `1 <= nums.length <= 100`
 * *   1 <= nums[i] <= 104
 * *   `1 <= k <= nums.length`
**/
public class Solution {
    public double largestSumOfAverages(int[] nums, int k) {
        return helper(nums, k, 0, new Double[k + 1][nums.length]);
    }

    private double helper(int[] nums, int k, int idx, Double[][] memo) {
        if (memo[k][idx] != null) {
            return memo[k][idx];
        }
        double maxAvg = 0;
        double sum = 0;
        for (int i = idx; i <= nums.length - k; i++) {
            sum += nums[i];
            if (k - 1 > 0) {
                maxAvg = Math.max(maxAvg, (sum / (i - idx + 1)) + helper(nums, k - 1, i + 1, memo));
            } else if (i == nums.length - 1) {
                maxAvg = Math.max(maxAvg, sum / (i - idx + 1));
            }
        }
        memo[k][idx] = maxAvg;
        return maxAvg;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy