title: Restaurant Maximum Profit using Dynamic Programming
body: <p>Its an assignment task,I have spend 2 days to come up with a solution but still having lots of confusion,however here I need to make few points clear. Following is the problem:</p>
  
  <p><em>Yuckdonald’s is considering opening a series of restaurant along QVH. n possible locations are along a straight line and the distances of these locations from the start of QVH are in miles and in increasing order <strong>m1, m2, ...., mn</strong>. The constraints are as follows:<br>
  1. At each location, Yuckdonald may open one restaurant and expected profit from opening a restaurant at location <strong>i</strong> is given as <strong>pi</strong><br>
  2. Any two restaurants should be at least <strong>k</strong> miles apart, where <strong>k</strong> is a positive integer</em></p>
  
  <p>My solution:</p>
  
  <pre><code>public class RestaurantProblem {
  
      int[] Profit;
      int[] P;
      int[] L;
      int k;
  
  
  
      public RestaurantProblem(int[] L , int[] P, int k) {
          this.L = L;
          this.P = P;
          this.k = k;
          Profit = new int[L.length];
      }
  
  
  
      public int compute(int i){
          if(i==0)
              return 0;
  
  
          Profit[i]= P[i]+(L[i]-L[i-1]&lt; k ? 0:compute(i-1));//if condition satisfies then adding previous otherwise zero
          if (Profit[i]&lt;compute(i-1)){
                  Profit[i] = compute(i-1);
              }
          return Profit[i];
      }
  
      public static void main(String args[]){
          int[] m = {0,5,10,15,19,25,28,29};
          int[] p = {0,10,4,61,21,13,19,15};
          int k = 5;
  
          RestaurantProblem rp = new RestaurantProblem(m, p ,k);
          rp.compute(m.length-1);
          for(int n : rp.Profit)
              System.out.println(n);
  
      }
  
  }
  </code></pre>
  
  <p>This solution giving me 88 however if I exclude <strong>(Restaurant at 25 with Profit 13)</strong> and include <strong>(Restaurant 28 with profit 19)</strong> I can have 94 max...</p>
  
  <p>point me if I am wrong or how can I achieve this if its true.</p>