-2

I have something like this in my code for a Dynamic Programming problem.

class Solution 
{
    
    static int dp[][] = new int[1000][1000];
    static int someMethod(int a, int b) {
        // use dp[][] somewhere here
        // some recursion statements
        someMethod(a-x,b-y);
    }
}

I want to initialize all values in dp with -1 instead of 0. Standard initialization inside method isn't an option because it uses recursion to make multiple calls.

Solution goes into an online judging tool so can't really config anything too. Any plain code solution will be appreciated.

Arth
  • 17
  • 4
  • What's wrong with cycling through each element and initializing it? – Federico klez Culloca May 29 '21 at 15:34
  • @FedericoklezCulloca Can't really do it because method is using recursion. Sorry should have mentioned that. – Arth May 29 '21 at 15:35
  • Ok, but you can do it in a [static block](https://stackoverflow.com/questions/2943556/static-block-in-java). Or am I misunderstanding your question? – Federico klez Culloca May 29 '21 at 15:36
  • Just call a `static` method that creates, populates, and returns the array. Or is your question asking something else? What does recursion have to do with this? Not clear, I suggest your edit the question for clarity. – Basil Bourque May 29 '21 at 15:37
  • @BasilBourque That might do if I was calling for array just once in the method, the issue is since its a recursive method it will keep calling the initialization and that would reset it values. – Arth May 29 '21 at 15:47
  • Manually call the initialization method, once, before you start the recursion. – NomadMaker May 29 '21 at 19:20

3 Answers3

3

You said:

I want to initialize all values in dp with -1 instead of 0.

Call a static method that creates, populates, and returns the needed array.

class Solution 
{
    static int[][] dp = Solution.makeArray() ;
    static int[][] makeArray() {
        int limit = 1_000 ;
        int[][] a = new int[ limit ][ limit ] ;
        for ( row = 0; row < limit ; row ++) {
            for ( column = 0; column < limit ; column ++) {
                a[ row ][ column ] = -1 ;
            }
        }
        return a ;
    }
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2
class Solution 
{
    
    static int dp[][];
    static {
       dp = new int[1000][1000];
       for (int[] row: dp) {
          Arrays.fill(row, -1);
       }
    }

    static int someMethod(int a, int b) {
        // use dp[][] somewhere here
        // some recursion statements
        someMethod(a-x,b-y);
    }
}
vincendep
  • 528
  • 4
  • 10
1

If you are required to use recursion to do this, you can do it as follows: I reduced the size of the array for demo purposes. But for a 1000 x 1000 array you may get a StackOverflow unless you modify your JVM parameters. Using Eclipse, I had to set stack size to 28 megabytes via the
java -Xss28m command.

import java.util.Arrays;

class Solution {
    
    static int dp[][] = new int[10][10];

    public static void main(String[] arg) {
        someMethod(0, 0);
        for (int[] row : dp) {
            System.out.println(Arrays.toString(row));
        }
    }

prints

[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]

The method

    static void someMethod(int a, int b) {
        if (b < dp[0].length - 1) {
            someMethod(a, b + 1);
        } else if (a < dp.length - 1) {
            someMethod(a + 1, 0);
        }
        dp[a][b] = -1;
    }
}
WJS
  • 36,363
  • 4
  • 24
  • 39