Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like dasblinkenlight said, the most efficient way to do this is using a memoization or dynamic programming technique. I tend to prefer dynamic programming, but I'll use pure recursion here.</p> <p>The answer centers around the answer to one fundamental question: "If I'm in the square in row r and column c on my field, how can I evaluate the path from the top left to here such that the number of strawberries is maximized?"</p> <p>The key to realize is that there's only two ways to get in the plot in row r and column c: either I can get there from above, using the plot in row r-1 and column c, or I can get there from the side, using the plot in row r and column c-1. After that, you just need to make sure you know your base cases...which means, fundamentally, my purely recursive version would be something like:</p> <pre><code>int[][] field; int max(int r, int c) { //Base case if (r == 0 &amp;&amp; c == 0) { return field[r][c]; } //Assuming a positive number of strawberries in each plot, otherwise this needs //to be negative infinity int maxTop = -1, maxLeft = -1; //We can't come from the top if we're in the top row if (r != 0) { maxTop = field[r-1][c]; } //Similarly, we can't come from the left if we're in the left column if (c != 0) { maxLeft = field[r][c-1]; } //Take whichever gives you more and return.. return Math.max(maxTop, maxLeft) + field[r][c]; } </code></pre> <p>Call max(r-1, c-1) to get your answer. Notice there's a lot of inefficiency here; you'll do much better by using dynamic programming (which I'll provide below) or memoization (which has already been defined). The thing to remember, though, is that both the DP and memoization techniques are simply more efficient ways that come from the recursive principles used here.</p> <p>DP:</p> <pre><code>int maxValue(int[][] field) { int r = field.length; int c = field[0].length; int[][] maxValues = new int[r][c]; for (int i = 0; i &lt; r; i++) { for (int j = 0; j &lt; c; j++) { if (i == 0 &amp;&amp; j == 0) { maxValues[i][j] = field[i][j]; } else if (i == 0) { maxValues[i][j] = maxValues[i][j-1] + field[i][j]; } else if (j == 0) { maxValues[i][j] = maxValues[i-1][j] + field[i][j]; } else { maxValues[i][j] = Math.max(maxValues[i][j-1], maxValues[i-1][j]) + field[i][j]; } } } return maxValues[r-1][c-1]; } </code></pre> <p>In both cases, if you want to recreate the actual path, just keep a 2D table of booleans that corresponds with "Did I come from above or to the left"? If the most strawberry path comes from above, put true, otherwise put false. That can allow you to retrace the patch after the calculation.</p> <p>Notice that this is still recursive in principal: at each step, we're looking back at our previous results. We just happen to be caching our previous results so we don't waste a bunch of work, and we're attacking the subproblems in an intelligent order so that we can always solve them. For more on dynamic programming, see <a href="http://en.wikipedia.org/wiki/Dynamic_programming">Wikipedia</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload