Largest Sum of Averages
Description
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.
Note:
1 <= A.length <= 100
.1 <= A[i] <= 10000
.1 <= K <= A.length
.- Answers within
10^-6
of the correct answer will be accepted as correct.
Solution(javascript)
/*
* @lc app=leetcode id=813 lang=javascript
*
* [813] Largest Sum of Averages
*/
// @lc code=start
// /** Top-down
// * @param {number[]} A
// * @param {number} K
// * @return {number}
// */
// const largestSumOfAverages = function (A, K) {
// const sum = (xs = []) => xs.reduce((acc, num) => acc + num, 0)
// const memo = {}
// const aux = (index, count, currentK) => {
// const key = `${index}-${count}-${currentK}`
// if (memo[key] !== undefined) {
// return memo[key]
// }
// if (index >= A.length && currentK === K) {
// return 0
// }
// if (index < A.length && currentK < K) {
// memo[key] = Math.max(
// aux(index + 1, count + 1, currentK),
// aux(index + 1, 0, currentK + 1) + sum(A.slice(index - count, index + 1)) / (count + 1),
// )
// return memo[key]
// }
// return -Infinity
// }
// return aux(0, 0, 0)
// }
// /** Bottom-up
// * @param {number[]} A
// * @param {number} K
// * @return {number}
// */
// const largestSumOfAverages = function (A, K) {
// const sum = (xs = []) => xs.reduce((acc, num) => acc + num, 0)
// // const memo = {}
// const memo = []
// for (let index = A.length; index >= 0; index--) {
// memo[index] = memo[index] || []
// for (let currentK = K; currentK >= 0; currentK--) {
// memo[index][currentK] = memo[index][currentK] || []
// for (let count = A.length - 1; count >= 0; count--) {
// if (index === A.length && currentK === K) {
// memo[index][currentK][count] = 0
// } else if (index < A.length && currentK < K) {
// memo[index][currentK][count] = Math.max(
// memo[index + 1][currentK][count + 1] || -Infinity,
// memo[index + 1][currentK + 1][0] + sum(A.slice(index - count, index + 1)) / (count + 1),
// )
// } else {
// memo[index][currentK][count] = -Infinity
// }
// }
// }
// }
// return memo[0][0][0]
// }
/** Bottom-up O(kn) space
* @param {number[]} A
* @param {number} K
* @return {number}
*/
const largestSumOfAverages = function (A, K) {
const sum = (xs = []) => xs.reduce((acc, num) => acc + num, 0)
let memo = new Array(K + 1).fill(0).map(() => new Array(A.length + 1).fill(0))
for (let index = A.length; index >= 0; index--) {
const current = new Array(K + 1).fill(0).map(() => new Array(A.length + 1).fill(0))
for (let currentK = K; currentK >= 0; currentK--) {
for (let count = A.length - 1; count >= 0; count--) {
if (index === A.length && currentK === K) {
current[currentK][count] = 0
} else if (index < A.length && currentK < K) {
current[currentK][count] = Math.max(
memo[currentK][count + 1],
memo[currentK + 1][0] + sum(A.slice(index - count, index + 1)) / (count + 1),
)
} else {
current[currentK][count] = -Infinity
}
}
}
memo = current
}
return memo[0][0]
}
// @lc code=end