I have this problem:
Given an array
Aand an integerK, partitionAintoKcontiguous subarrays in the way that maximizes the sum of the number of inversions in each subarray. (An "inversion" is a pair of indicesi,jwherei < jandA[i] > A[j].)Return the sum.
For example, if
A = 9 1 7 2 3andK = 2, then the answer is4, because you can split the array into9 1 7 2(which has four inversions) and3(which has none), and 4 + 0 = 4.Constaints
1 <= A.size <= 500 1 <= k <= A.size 1 <= A[i] <= 100000
My thoughts:
This looks like a dynamic programming problem but I can't work out how to integrate
Kgroups into the solution.This problems roughly translates to finding
Kgroups with each group having the highest number of increasing elements within that group.
Any ideas on how to solve this would be helpful.
1, 7, 2, 3. And don't you mean "highest number of decreasing elements"?length(A)andKcan be?