|
1 | 1 | package hackerrank.FlatlandSpaceStations;
|
2 | 2 |
|
3 | | -import org.junit.jupiter.api.Assertions; |
4 | | -import org.junit.jupiter.api.Disabled; |
5 | | -import org.junit.jupiter.api.Test; |
6 | | - |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.FileWriter; |
| 5 | +import java.io.IOException; |
7 | 6 | import java.util.Arrays;
|
| 7 | +import java.util.Scanner; |
8 | 8 |
|
9 | 9 | /**
|
10 | 10 | * Flatland Space Stations
|
|
46 | 46 | *
|
47 | 47 | * Problem: https://www.hackerrank.com/challenges/flatland-space-stations/problem
|
48 | 48 | *
|
49 | | - * # Solution |
50 | | - * The maximum distance any city is from a space station. |
51 | | - * |
52 | | - * Example 1 |
53 | | - * 5 2 n = 5, c[] size m = 2 |
54 | | - * 0 4 space stations |
55 | | - * |
56 | | - * There are five cities, c0,c1,c2,c3,c4. |
57 | | - * c0 and c4 has space stations. |
58 | | - * c2 has the maximum distance either to c0 or c4. |
59 | | - * So maximum distance is 2. |
60 | | - * |
61 | | - * We only need to find the distances from those |
62 | | - * cities which do not have the space station. |
63 | | - * |
64 | | - * Example 2 |
65 | | - * 6 6 n=6, m=6 |
66 | | - * 0 1 2 4 3 5 |
67 | | - * As all the cities have stations, maximum |
68 | | - * distance from any city to a space station |
69 | | - * is 0. |
70 | | - * |
71 | | - * |
72 | | - * |
73 | | - * Pseudocode |
74 | | - * maxDistance = 0 |
75 | | - * We have to calculate the distance for each |
76 | | - * city to another, so we need two loops. |
77 | | - * |
78 | | - * for i=0;i<cities.length;i++ |
79 | | - * for j=i+1;j< |
80 | | - * |
81 | | - * I think we just need to find maximum distance |
82 | | - * between two stations, i.e. from a city to a |
83 | | - * station between the stations. |
84 | | - * |
85 | | - * The distance from one (minimum) end point to |
86 | | - * a space station. |
87 | | - * |
88 | | - * If there is only space station, calculate the |
89 | | - * max distance from both ends. |
90 | | - * |
91 | | - * If there is only space station which is at |
92 | | - * the either ends, get the distance from the |
93 | | - * number of cities-1. |
94 | 49 | */
|
95 | 50 | public class FlatlandSpaceStations {
|
| 51 | + |
96 | 52 | public static int flatlandSpaceStations(
|
97 | | - int noOfCities, |
| 53 | + int numOfCities, |
98 | 54 | int[] spaceStations) {
|
99 | | - int noOfSpaceStations = spaceStations.length; |
| 55 | + int numOfSpaceStations = spaceStations.length; |
100 | 56 |
|
101 | | - if (noOfCities == noOfSpaceStations) |
| 57 | + if ( isCitySpaceStation(numOfCities, |
| 58 | + numOfSpaceStations)) |
102 | 59 | return 0;
|
103 | 60 |
|
104 | | - else if (noOfCities == 2 && noOfSpaceStations == 1) |
105 | | - return 1; |
106 | | - |
107 | | - else if (noOfCities == 3 && noOfSpaceStations == 2) |
108 | | - return 1; |
109 | | - |
110 | | - else if (noOfSpaceStations == 1) { |
| 61 | + if (isCitySpaceStation(numOfSpaceStations, 1)) { |
111 | 62 |
|
112 | | - // if the space station is at first or last |
113 | | - if (spaceStations[0] == 0 || |
114 | | - spaceStations[0] == noOfCities - 1) |
115 | | - return noOfCities - 1; |
116 | | - |
117 | | - else { |
118 | | - return Math.max(spaceStations[0], |
119 | | - noOfCities - 1 - spaceStations[0]); |
120 | | - |
121 | | - } |
| 63 | + return getMaxDistanceWithOneSpaceStation( |
| 64 | + numOfCities, spaceStations); |
122 | 65 | }
|
123 | 66 |
|
124 | 67 | Arrays.sort(spaceStations);
|
125 | 68 |
|
126 | | - // First and last cities are the space station. |
127 | | - if (spaceStations[0] == 0 && |
128 | | - spaceStations[1] == noOfCities - 1) |
129 | | - return (spaceStations[1] - spaceStations[0]) / 2; |
130 | | - |
131 | | - if (areSpaceStationsConsecutiveLast( |
| 69 | + if ( areSpaceStationsConsecutiveLast( |
132 | 70 | spaceStations,
|
133 | | - noOfSpaceStations, |
134 | | - noOfCities)) |
| 71 | + numOfSpaceStations, |
| 72 | + numOfCities)) |
135 | 73 | return spaceStations[0];
|
136 | 74 |
|
137 | 75 | int maxDistance = 0;
|
138 | 76 |
|
139 | | - // If first city is not a space station |
140 | | - if (spaceStations[0] != 0) { |
141 | | - maxDistance = spaceStations[0]; |
| 77 | + maxDistance = setMaxDistanceBasedOnFirstSpaceStation( |
| 78 | + spaceStations); |
142 | 79 |
|
143 | | - } else if ( spaceStations[0] == 0 ) { |
144 | | - maxDistance = (spaceStations[1]-spaceStations[0])/2; |
145 | | - } |
| 80 | + maxDistance = getMaxDistance(spaceStations, |
| 81 | + numOfSpaceStations, maxDistance); |
146 | 82 |
|
147 | | - for (inti = 1; i < noOfSpaceStations-1; i++) { |
148 | | - intdistance = (spaceStations[i+1] - spaceStations[i])/2; |
| 83 | + if ( lastCityIsNotLastSpaceStation(numOfCities, |
| 84 | + spaceStations, numOfSpaceStations)) { |
149 | 85 |
|
150 | | - if ( distance > maxDistance ) |
151 | | - maxDistance = distance; |
| 86 | + return distanceFromLastCityToLastStation( |
| 87 | + numOfCities, spaceStations, |
| 88 | + numOfSpaceStations, maxDistance); |
152 | 89 | }
|
153 | 90 |
|
154 | | - // If last space station is not the last city |
155 | | - if ( spaceStations[noOfSpaceStations-1] != noOfCities -1 ) { |
156 | | - int lastSpaceStationDistance = noOfCities - 1 - |
157 | | - spaceStations[noOfSpaceStations-1] ; |
| 91 | + return maxDistance; |
| 92 | + } |
| 93 | + |
| 94 | + private static boolean lastCityIsNotLastSpaceStation( |
| 95 | + int numOfCities, int[] spaceStations, |
| 96 | + int numOfSpaceStations) { |
| 97 | + |
| 98 | + return spaceStations[numOfSpaceStations - 1] |
| 99 | + != numOfCities - 1; |
| 100 | + } |
| 101 | + |
| 102 | + private static int setMaxDistanceBasedOnFirstSpaceStation( |
| 103 | + int[] spaceStations) { |
| 104 | + |
| 105 | + if (isCitySpaceStation(spaceStations[0], 0)) |
| 106 | + return (spaceStations[1] - spaceStations[0])/2; |
| 107 | + |
| 108 | + return spaceStations[0]; |
| 109 | + } |
158 | 110 |
|
159 | | - return maxDistance > lastSpaceStationDistance ? |
160 | | - maxDistance : lastSpaceStationDistance; |
| 111 | + private static boolean isCitySpaceStation( |
| 112 | + int spaceStation, int city) { |
| 113 | + |
| 114 | + return city == spaceStation; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + private static int getMaxDistanceWithOneSpaceStation( |
| 119 | + int numOfCities, int[] spaceStations) { |
| 120 | + |
| 121 | + // The space station is at first or last |
| 122 | + if ( isCitySpaceStation(spaceStations[0], 0) |
| 123 | + || isCitySpaceStation( spaceStations[0], |
| 124 | + numOfCities - 1)) |
| 125 | + return numOfCities - 1; |
| 126 | + |
| 127 | + // The space station is in the middle |
| 128 | + return Math.max(spaceStations[0], |
| 129 | + numOfCities - 1 - spaceStations[0]); |
| 130 | + } |
| 131 | + |
| 132 | + private static int getMaxDistance( |
| 133 | + int[] spaceStations, |
| 134 | + int numOfSpaceStations, |
| 135 | + int maxDistance) { |
| 136 | + |
| 137 | + for ( int i = 1; i < numOfSpaceStations -1; i++) { |
| 138 | + int distance = ( spaceStations[i+1] - |
| 139 | + spaceStations[i] ) / 2; |
| 140 | + |
| 141 | + if ( distance > maxDistance) |
| 142 | + maxDistance = distance; |
161 | 143 | }
|
162 | 144 |
|
163 | 145 | return maxDistance;
|
164 | 146 | }
|
165 | 147 |
|
166 | | - /** |
167 | | - * The space stations are consecutive last, |
168 | | - * i.e. They are the last ones. |
169 | | - * |
170 | | - * Given 5 cities with 2 space stations that |
171 | | - * are in the end. i.e. Cities 3 and 4 are |
172 | | - * the space stations. |
173 | | - * |
174 | | - * Get the last station |
175 | | - * Check if the last city is the last station, |
176 | | - * spaceStations[noOfSpaceStations-1] == noOfCities-1 |
177 | | - * |
178 | | - * Get the first station spaceStations[0] |
179 | | - * |
180 | | - * Confirm that space stations are consecutive last |
181 | | - * |
182 | | - */ |
| 148 | + private static int distanceFromLastCityToLastStation(int numOfCities, int[] spaceStations, int numOfSpaceStations, int maxDistance) { |
| 149 | + int lastSpaceStationDistance = numOfCities - 1 - |
| 150 | + spaceStations[numOfSpaceStations -1] ; |
| 151 | + |
| 152 | + return Math.max(maxDistance, lastSpaceStationDistance); |
| 153 | + } |
| 154 | + |
183 | 155 | public static boolean areSpaceStationsConsecutiveLast(
|
184 | 156 | int[] spaceStations,
|
185 | 157 | int numberOfSpaceStations,
|
186 | 158 | int numberOfCities) {
|
187 | 159 |
|
188 | 160 | int lastSpaceStation = spaceStations[numberOfSpaceStations - 1];
|
| 161 | + int lastCity = numberOfCities-1; |
189 | 162 |
|
190 | | - if ( lastSpaceStation != numberOfCities-1) |
| 163 | + if ( lastSpaceStation != lastCity) |
191 | 164 | return false;
|
192 | 165 |
|
193 | 166 | int firstSpaceStation = spaceStations[0];
|
194 | | - int consecutiveLast = (lastSpaceStation - firstSpaceStation) - |
195 | | - (numberOfSpaceStations - 1); |
196 | 167 |
|
197 | | - return ( consecutiveLast == 0 ) ? true : false; |
| 168 | + return numberOfSpaceStations > |
| 169 | + lastSpaceStation - firstSpaceStation; |
| 170 | + |
198 | 171 | }
|
199 | | -} |
200 | 172 |
|
| 173 | + private static final Scanner scanner = new Scanner(System.in); |
| 174 | + |
| 175 | + public static void main(String[] args) throws IOException { |
| 176 | + BufferedWriter bufferedWriter = new BufferedWriter( |
| 177 | + new FileWriter(System.getenv("OUTPUT_PATH"))); |
| 178 | + |
| 179 | + String[] nm = scanner.nextLine().split(" "); |
| 180 | + |
| 181 | + int n = Integer.parseInt(nm[0]); |
| 182 | + |
| 183 | + int m = Integer.parseInt(nm[1]); |
| 184 | + |
| 185 | + int[] c = new int[m]; |
| 186 | + |
| 187 | + String[] cItems = scanner.nextLine().split(" "); |
| 188 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
201 | 189 |
|
| 190 | + for (int i = 0; i < m; i++) { |
| 191 | + int cItem = Integer.parseInt(cItems[i]); |
| 192 | + c[i] = cItem; |
| 193 | + } |
| 194 | + |
| 195 | + int result = flatlandSpaceStations(n, c); |
| 196 | + |
| 197 | + bufferedWriter.write(String.valueOf(result)); |
| 198 | + bufferedWriter.newLine(); |
| 199 | + |
| 200 | + bufferedWriter.close(); |
| 201 | + |
| 202 | + scanner.close(); |
| 203 | + } |
| 204 | +} |
0 commit comments