Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. public class BalloonBurst
  2. {
  3. public static void main(String[] args)
  4. {
  5. int balloon[] = {1,2,3,4};
  6. int n = balloon.length;
  7. System.out.println("Maximum cost = " + getMaxCost(balloon, n));
  8. }
  9.  
  10. static int getMaxCost(int balloon[], int n)
  11. {
  12. int table[][] = new int[n][n];
  13.  
  14. for(int i = 1; i <= n ; i++) // this will be length of the sub array
  15. {
  16. for(int j = 0; j <= n - i; j++)
  17. {
  18. int k = i + j - 1;
  19.  
  20. for(int l = j ; l <= k; l++)
  21. {
  22. int lval = 1;
  23. int rval = 1;
  24. if(j != 0)
  25. {
  26. lval = balloon[j-1];
  27. }
  28. if(k != n-1)
  29. {
  30. rval = balloon[k+1];
  31. }
  32.  
  33. int before = 0;
  34. int after = 0;
  35. if(j != l)
  36. {
  37. before = table[j][l-1];
  38. }
  39. if(k != l)
  40. {
  41. after = table[l+1][k];
  42. }
  43. table[j][k] = Math.max(lval * balloon[l] * rval + before + after, table[j][k]);
  44. }
  45. }
  46. }
  47.  
  48. return table[0][n-1];
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement