Reputation: 23
TLDR; It seems to be a top k problem with dependency. Or a Knapsack Problem but the order matters.
If I have a 2D array with (N, M) shape. I need to select L rows from it to be a (L, M) shape array SUB_M. And for each column in SUB_M. I pick the smallest value in it and become a 1D (M) shape array ANS_M My goal is to minimize the sum of ANS_M.
So for array like this.
[[ 8 7 7 7 7 7 7 5 6]
[ 5 7 6 5 6 6 7 7 5]
[ 7 8 8 8 5 5 5 6 7]
[ 7 8 7 7 7 6 7 8 5]
[ 0 0 100 100 100 100 100 100 100]
[100 100 100 0 0 100 100 100 100]
[100 100 100 100 100 100 0 0 100]]
I'll select row 1, 4, 5, 6. The SUB_M would looks like this:
[[ 5 7 6 5 6 6 7 7 5]
[ 0 0 100 100 100 100 100 100 100]
[100 100 100 0 0 100 100 100 100]
[100 100 100 100 100 100 0 0 100]]
And the ANS_M is this:
[ 0 0 6 0 0 6 0 0 5]
So the sum of ANS_M is 17.
I think this problem is a little bit like a k sum problem. But unlike k-sum problem. This problem can't be solved using max/min heap. Because in k-sum problem, two number can be compare directly. But in my problem, even if arr A is smaller than B and C in sum. Maybe sum of the combination of B and C can be smaller than A. For example.
A = [ 5 5 5 5 5]
B = [ 0 0 0 99 99]
C = [99 99 99 0 0]
I was banging my head on wall on the problem. And I can't make ChatGPT to solve it.
I have a brute force solution is find all the combination C(N, C). And for each combination arrays, I find the minimum along N and iterating through all the combination.
Is there any better way? Because the N and M could be pretty large.
Upvotes: 1
Views: 72