title: Not able to Memoize this Recursive Code into Top Down DP
body: <p>I got this problem on one of my tests. I wrote a Recursive solution for it (I tried it on a few test cases, it seems to be working fine). I am trying to memoize this code, but am not able to do that.</p>
  <p>The problem statement goes like this,</p>
  <p>You are playing Fantasy Cricket on IPL11 App. The game is a bit different from other Fantasy Cricket Apps.
  Here you are given two lists of players. Both lists contain <code>n</code> players each.
  The list is denoted based on the value they provide.</p>
  <p>You need to select exactly <code>n</code> players out of these <code>2*n</code> players.</p>
  <p>The selection needs to be done based on the following rules.</p>
  <ul>
  <li>You can select either from the end of <code>list1</code> or end of <code>list2</code></li>
  <li>Once you select a player, he is removed from that list.</li>
  <li>To get access to better players, you also have an option to discard a player and remove them
  from the end of the list. You can do the discard operation at most k times.</li>
  </ul>
  <p>Naturally, you would want to get the maximum value possible in your team. Find the maximum value that you can get after selecting <code>n</code> players from the 2 lists based on the above rules.</p>
  <p>Example</p>
  <pre><code>n: 6
  list1: [10, 13, 31, 12, 12, 18]
  list2: [34, 12, 3, 23, 11, 13]
  k: 2
  </code></pre>
  <pre><code>O/P: 112
  </code></pre>
  <pre><code>Explanation:
  
  * Select 18 (list1)
  * Select 13 (list2)
  * Skip 11 (list2)
  * Select 23 (list2)
  * Select 12 (list1)
  * Skip 3 (list2)
  * Select 12 (list2)
  * Select 34 (list2)
  </code></pre>
  <p>My Recursive Solution,</p>
  <pre><code>public static int helper(int[] list1, int[] list2, int n1, int n2, int k, int count) {
          if(count == n) return 0;
          int chooseFromList1 = Integer.MIN_VALUE;
          int chooseFromList2 = Integer.MIN_VALUE;
          if(n1 &gt; 0) {
              chooseFromList1 = list1[n1-1]+helper(list1, list2, n1-1, n2, k, count+1);
          }
          if(n2 &gt; 0) {
              chooseFromList2 = list2[n2-1]+helper(list1, list2, n1, n2-1, k, count+1);
          }
          int temp = Integer.MIN_VALUE;
          if(k &gt; 0) {
              int skipList1 = helper(list1, list2, n1-1, n2, k-1, count);
              int skipList2 = helper(list1, list2, n1, n2-1, k-1, count);
              temp = Math.max(skipList1, skipList2);
          }
          return Math.max(Math.max(chooseFromList1, chooseFromList2), temp);
      }
  </code></pre>
  <p>What I did is, I took 4 choices in this recursive code,</p>
  <ul>
  <li>Take last element from <code>list1</code> if n1 &gt; 0</li>
  <li>Take last element from <code>list2</code> if n2 &gt; 0</li>
  <li>Skip last element from <code>list1</code> if k &gt; 0</li>
  <li>Skip last element from <code>list2</code> if k &gt; 0</li>
  </ul>
  <p>I want to convert it to Top Down DP, but not able to write the code for it.</p>