Advertisement
Guest User

lab10

a guest
Mar 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. /**
  2. * lab10: starter code
  3. *
  4. * A Rod is constructed from an array of non-negative prices, where
  5. * price[i] represents the selling price of a rod of length i. We
  6. * assume here that price[0] is 0.
  7. *
  8. * We wish to determine the maximum profit obtainable by cutting up a
  9. * rod of length n and selling the pieces. We also wish to know the
  10. * lengths of each cut segment that obtains the maximum profit. Thus,
  11. * a Result consists of two pieces of information: the total profit
  12. * and a list of lengths.
  13. *
  14. * TODO:
  15. *
  16. * (1) Create a lab10 project in IntelliJ and copy your code for Map,
  17. * HashMap, AList, Assoc, and DoublyLinkedList from p4 into the src
  18. * directory.
  19. *
  20. * (2) Implement the naive brute-force solution, as described in
  21. * lecture, to generate all configurations of different pieces and
  22. * find the highest priced configuration. Do this in two stages:
  23. *
  24. * -- Implement the basicCut() method to return an int
  25. * representing the maximal profit.
  26. *
  27. * -- Implement the fancyCut() method to return a Result
  28. * object that includes the maximal profit and a list
  29. * of corresponding rod lengths
  30. *
  31. * (3) Memoize your fancyCut() method by using a cache (of type
  32. * HashMap) as an instance variable.
  33. *
  34. * @author Colin Curry & Adam Hilinski
  35. *
  36. */
  37.  
  38. public class Rod {
  39.  
  40. class Result {
  41. int profit;
  42. List<Integer> lengths;
  43.  
  44. Result(int profit) {
  45. this.profit = profit;
  46. lengths = new DoublyLinkedList<>();
  47. }
  48.  
  49. public String toString() {
  50. return String.format("[numCuts=%d, profit=%d, lengths=%s]",
  51. lengths.size() - 1, profit, lengths);
  52. }
  53. }
  54.  
  55. private int[] price;
  56.  
  57. public Rod(int[] price) {
  58. this.price = price;
  59. }
  60.  
  61. /**
  62. * TODO
  63. *
  64. * Returns the maximal profit obtainable by cutting a rod
  65. * of length n. Implemented as a top-down recursive method.
  66. * Runs in O(2^n) time.
  67. */
  68.  
  69. public int basicCut(int n) {
  70. if(n == 0)
  71. return 0;
  72. int profit = Integer.MIN_VALUE;
  73. for(int i = 1; i <= n; i++) {
  74. profit = Math.max(profit, (price[i]) + basicCut(n - i));
  75. }
  76. return profit;
  77. }
  78.  
  79. /**
  80. * TODO
  81. *
  82. * Returns a Result containing the maximal profit obtainable by
  83. * cutting a rod of length n and a list of corresponding rod
  84. * lengths. Implemented as a memoized top-down recursive method.
  85. * Runs in O(n^2) time.
  86. */
  87.  
  88. public Result fancyCut(int n) {
  89. if(n == 0)
  90. return new Result(0);
  91. Result[] cache = new Result[n];
  92.  
  93. int profit = 0;
  94. for(int i = 1; i <= n; i++) {
  95. if(profit < price[i]) {
  96. profit = ;
  97. cache[i] =
  98. }
  99. }
  100.  
  101. return null;
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement