Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. class Solution{
  2.  
  3. public static int price[] = { 0 , 1 , 5 , 8 , 9 , 10 , 17 , 17, 20 };
  4.  
  5. public static int bestPrice[] = new int[price.length] ;
  6.  
  7. public static void main(String args[]){
  8.  
  9. if( price.length >= 2 ){
  10. calculateprice(price.length-1) ;
  11. System.out.println( "Best price that can be attained for rod of length " + (price.length - 1 ) + " is " + bestPrice[price.length - 1] );
  12. }
  13. }
  14.  
  15. public static void calculateprice(int len){
  16.  
  17. System.out.println( len );
  18.  
  19. if( len == 1 ){
  20. bestPrice[1] = price[len] ;
  21. return;
  22. }
  23.  
  24. int currentBestPrice = price[len] ;
  25. for( int i = 1 ; i <= len/2; i++ ){
  26.  
  27. if( bestPrice[i] == 0 )
  28. calculateprice(i) ;
  29. if( bestPrice[len-i] == 0 )
  30. calculateprice(len-i) ;
  31.  
  32. currentBestPrice = Math.max( currentBestPrice , bestPrice[i] + bestPrice[len-i] ) ;
  33. }
  34. bestPrice[len] = currentBestPrice ;
  35. return ;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement