Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. By starting at the top of the triangle below and moving to adjacent numbers
  2. on the row below, the maximum total from top to bottom is 23.
  3.  
  4. 3
  5. 7 4
  6. 2 4 6
  7. 8 5 9 3
  8.  
  9. That is, 3 + 7 + 4 + 9 = 23.
  10.  
  11. Find the maximum total from top to bottom of the triangle below:
  12.  
  13. 75
  14. 95 64
  15. 17 47 82
  16. 18 35 87 10
  17. 20 04 82 47 65
  18. 19 01 23 75 03 34
  19. 88 02 77 73 07 63 67
  20. 99 65 04 28 06 16 70 92
  21. 41 41 26 56 83 40 80 70 33
  22. 41 48 72 33 47 32 37 16 94 29
  23. 53 71 44 65 25 43 91 52 97 51 14
  24. 70 11 33 28 77 73 17 78 39 68 17 57
  25. 91 71 52 38 17 14 91 43 58 50 27 29 48
  26. 63 66 04 68 89 53 67 30 73 16 69 87 40 31
  27. 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
  28.  
  29. public class Euler_18{
  30. // A custom data structure used to store
  31. // the array so that I can seperate the
  32. // logic from the storage
  33. static class TriangularArray{
  34. HashMap<Integer, int[]> map;
  35. int someInt;
  36. int size;
  37. // All the elements will be stored in
  38. // HashMap according to their order
  39. public TriangularArray(int size){
  40. this.size = size;
  41. map = new HashMap<Integer, int[]>();
  42. // Initialise the array
  43. for(int i=1; i<=size; i++){
  44. int[] currArray = new int[i];
  45. map.put(i, currArray);
  46. }
  47. }
  48.  
  49.  
  50. // Accept the array
  51. public void acceptArray(Scanner in){
  52. for(int i=1; i<=size; i++){
  53. int[] currArray = map.get(i);
  54. for(int j=0; j<currArray.length; j++){
  55. currArray[j] = in.nextInt();
  56. }
  57. map.put(i, currArray);
  58. }
  59. }
  60.  
  61.  
  62. // Display the array
  63. public void displayArray(){
  64. for(int i=1; i<=size; i++){
  65. int[] currArray = map.get(i);
  66. System.out.println("Array " + i + " : " + Arrays.toString(currArray));
  67. }
  68. }
  69.  
  70.  
  71. // Finds the maximum using Memoization.
  72. public void findMaximum(){
  73. for (int i=size-1; i>0; i--) {
  74. int[] currArray = map.get(i);
  75. int[] belowArray = map.get(i+1);
  76. for (int j=0; j<currArray.length; j++) {
  77. // The value of current element will be the maximum of the 2 values
  78. // of the array directly below it
  79. currArray[j] = Math.max(belowArray[j], belowArray[j+1]) + currArray[j];
  80. }
  81. }
  82. System.out.println("The maximum route is of length : " + map.get(1)[0]);
  83. }
  84. }
  85.  
  86.  
  87. public static void main(String[] args) {
  88. Scanner in = new Scanner(System.in);
  89. int size = in.nextInt();
  90. TriangularArray theArray = new TriangularArray(size);
  91. theArray.acceptArray(in);
  92. theArray.displayArray();
  93. theArray.findMaximum();
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement