Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. // . O 〇 o #=# <- space station
  2. // → ← → ←
  3. //
  4. // mass 1 5 7 3
  5. // direction 1 -1 1 -1
  6. //
  7. // answer: 1
  8.  
  9. // . o O 〇 #=#
  10. // → → → ←
  11. //
  12. // mass 1 3 5 7
  13. // direction 1 1 1 -1
  14. //
  15. // answer: 0
  16.  
  17. // O . o #=#
  18. // → → ←
  19. //
  20. // mass 5 1 4
  21. // direction 1 1 -1
  22. //
  23. // answer: 1
  24.  
  25. // . O o #=#
  26. // → → ←
  27. //
  28. // mass 1 5 4
  29. // direction 1 1 -1
  30. //
  31. // answer: 2
  32. // "static void main" must be defined in a public class.
  33. public class Main {
  34. static class Asteroid{
  35. private int mass;
  36. private int direction;
  37.  
  38. Asteroid(int mass, int direction) {
  39. this.mass = mass;
  40. this.direction = direction;
  41. }
  42.  
  43. private int getMass() {
  44. return mass;
  45. }
  46.  
  47. private int getDirection() {
  48. return direction;
  49. }
  50. }
  51.  
  52. public static void main(String[] args) {
  53. Asteroid[] case1 = new Asteroid[]{
  54. new Asteroid(1, 1),
  55. new Asteroid(5, -1),
  56. new Asteroid(7, 1),
  57. new Asteroid(3, -1),
  58. };
  59.  
  60. Asteroid[] case2 = new Asteroid[]{
  61. new Asteroid(1, 1),
  62. new Asteroid(3, 1),
  63. new Asteroid(5, 1),
  64. new Asteroid(7, -1),
  65. };
  66.  
  67. Asteroid[] case3 = new Asteroid[]{
  68. new Asteroid(5, 1),
  69. new Asteroid(1, 1),
  70. new Asteroid(4, -1),
  71. };
  72.  
  73. Asteroid[] case4 = new Asteroid[]{
  74. new Asteroid(1, 1),
  75. new Asteroid(5, 1),
  76. new Asteroid(4, -1),
  77. };
  78.  
  79. testCase(1, case1, 1);
  80. testCase(2, case2, 0);
  81. testCase(3, case3, 1);
  82. testCase(4, case4, 2);
  83. }
  84.  
  85. private static void testCase(int testCase, Asteroid[] asteroids, int expected) {
  86. int actual = countHits(asteroids);
  87. if (actual == expected) {
  88. System.out.println("Case " + testCase + ": PASSED");
  89. } else {
  90. System.out.println("Case " + testCase + ": FAILED; got " + actual + " expected " + expected);
  91. }
  92. }
  93.  
  94. /*
  95. iterate over asteroids O(n)
  96. if asteroid is moving away AWAY
  97. iterate backwards FORWARDS O(n)
  98. if AWAY.mass > FORWARDS.mass
  99. remove forwards from array O(n)
  100. else
  101. remove away from array O(n)
  102. and stop iterating
  103. return size of remaining array
  104.  
  105. O(n^3) O(1) space
  106.  
  107. optimize
  108.  
  109. // o . 0 #=#
  110. // ← → →
  111. // 4 1 5
  112. answer : 2
  113. no sorting!
  114.  
  115. dump into linked list to resize array
  116. O(n^2) O(n) space
  117. O(n^2) time O(1) space
  118.  
  119. */
  120. private static int countHits(Asteroid[] asteroids) {
  121. return 0;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement