1

Given a 2D array with size N, I want to maximize array[0][k1] + array[k1 + 1][k2] + array[k2 + 1][k3] + ... + array[kx + 1][N - 1] for a given x. All k values are strictly increasing.

With small values of x (x = 2, 3, 4), a dynamic programming solution appears feasible.

However, the bound is 1 <= x, N <= 100, so I am not sure how to attempt this.

6
  • What is x, and what is the size of the inner N arrays? Commented Feb 26, 2018 at 2:29
  • what is 1<=x means?it can be 0?if its 0,then become a[0][k1] + a[1][N-1] Commented Feb 26, 2018 at 2:29
  • Maybe x is in kx means number of k? Can you describe more about your dp approach? I think using the dp[x][n][n] should work? O(n^4) with n = 100 is not bad. Commented Feb 26, 2018 at 3:03
  • I'm sorry, I was unclear. Pham Trung is correct, x is the number of different of k. I am also mistaken. Also, obgnaw, 1 <= x, N <= 100 is the bounds for the problem. x and N are given as input, but they are within the mentioned bounds. Commented Feb 26, 2018 at 3:15
  • How would dp[x][n][n] work? I was under the impression that you would need dp[n][n][n][n]...[n][n] for something like 100. dp[x][n][n] would be sufficient, I believe. Commented Feb 26, 2018 at 3:17

1 Answer 1

2

Assuming that we are at a particular state (ki, xi) with ki is the current k index, x is the last k in order to give the answer for the problem, we need to try to find the maximum value we can create from this state.

We make one observation that the problem can be divided into subproblem (ki, xi). As for each (ki, xi) state, the result is independent of previous state, we can have our formula:

  • For ki == x, answer = 0

  • Otherwise, (ki, xi) = array[xi][z] + max(ki + 1, z) with z > xi

Here is a simple pseudocode

int[][]dp;
boolean[][]check;
boolean maxAmount(int k, int x){
    if(k == X){
       return true;
    }
    if this state is visited {
       return check[k][x];
    } 
    boolean result = false;

    for(int i = x + 1; i < n; i++){
        if(maxAmount(k + 1, i)){
            result = true;
            dp[k][x] = max(dp[k][x], array[x][i] + dp[k + 1][i]);
        }
    }
    return check[k][x] = result;

}

Note: For the special case when we cannot find enough k, you need to handle it depending on the requirement, but it should be trivial.

Sign up to request clarification or add additional context in comments.

9 Comments

I'm not quite sure if I understand completely. In order to find the solution to x=2, 3, or even 100, i would just change the size of int[][][] dp and then set k = x, correct?
@j1119 yup, the key idea is for the maximum value for a state (ki, xi, yi) is independent of previous state, thus we can use it as our dp state
What is the special case when we cannot find enough k?
@j1119 I don't know, whether you need to return invalid if it is not acceptable, or just return whatever you have if it is ok to form an answer with less than x k, it depends on your specific problem.
Appreciate your help, I needed that key observation!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.