diff --git a/solution3.c b/solution3.c new file mode 100644 index 0000000..3431921 --- /dev/null +++ b/solution3.c @@ -0,0 +1,12 @@ +int maxSumAfterPartitioning(int* arr, int arrSize, int k){ + int n = arrSize; + int dp[n + 1]; + for (int i = 1; i <= n; ++i) { + int curMax = 0; + for (int j = 1; j <= k && i - j >= 0; ++j) { + curMax = max(curMax, arr[i - j]); + dp[i] = max(dp[i], dp[i - j] + curMax * j); + } + } + return dp[n]; +}