#1043. Partition Array for Maximum Sum

ยท

1 min read

https://leetcode.com/problems/partition-array-for-maximum-sum/description/?envType=daily-question&envId=2024-02-03

function maxSumAfterPartitioning(arr, k) {
    const n = arr.length;
    const store = new Array(n + 1).fill(0);

    for (let i = 1; i <= n; i++) {
        let maxVal = 0;
        for (let j = 1; j <= Math.min(i, k); j++) {
            maxVal = Math.max(maxVal, arr[i - j]);
            store[i] = Math.max(store[i], store[i - j] + maxVal * j);
        }
    }

    return store[n];
}

Still learning these kind of questions solution pattern.

ย