package com.fishercoder.solutions;

/**
 * 1043. Partition Array for Maximum Sum
 *
 * Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.
 * Return the largest sum of the given array after partitioning.
 *
 * Example 1:
 * Input: A = [1,15,7,9,2,5,10], K = 3
 * Output: 84
 * Explanation: A becomes [15,15,15,9,10,10,10]
 *
 * Note:
 * 1 <= K <= A.length <= 500
 * 0 <= A[i] <= 10^6
 * */
public class _1043 {
    public static class Solution1 {
        /**credit: https://leetcode.com/problems/partition-array-for-maximum-sum/discuss/290863/JavaC%2B%2BPython-DP*/
        public int maxSumAfterPartitioning(int[] A, int K) {
            int N = A.length;
            int[] dp = new int[N];
            for (int i = 0; i < N; i++) {
                int curMax = 0;
                for (int k = 1; k <= K && i - k + 1 >= 0; k++) {
                    curMax = Math.max(curMax, A[i - k + 1]);
                    dp[i] = Math.max(dp[i], (i >= k ? dp[i - k] : 0) + curMax * k);
                }
            }
            return dp[N - 1];
        }
    }
}

@@ -33,515 +33,8 @@
 s;%0A%0A
-/**%0A * 1043. Partition Array for Maximum Sum%0A *%0A * Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.%0A * Return the largest sum of the given array after partitioning.%0A *%0A * Example 1:%0A * Input: A = %5B1,15,7,9,2,5,10%5D, K = 3%0A * Output: 84%0A * Explanation: A becomes %5B15,15,15,9,10,10,10%5D%0A *%0A * Note:%0A * 1 %3C= K %3C= A.length %3C= 500%0A * 0 %3C= A%5Bi%5D %3C= 10%5E6%0A * */%0A
 publ
@@ -97,16 +97,28 @@
      /**
+%0A         * 
 credit: 
@@ -214,16 +214,26 @@
 ython-DP
+%0A         
 */%0A     

refactor 1043