Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c63e39d

Browse files
committed
Code clean - Hackerrank FlatlandSpaceStations
1 parent 7c2af61 commit c63e39d

File tree

2 files changed

+184
-121
lines changed

2 files changed

+184
-121
lines changed
Lines changed: 118 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package hackerrank.FlatlandSpaceStations;
22

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;
76
import java.util.Arrays;
7+
import java.util.Scanner;
88

99
/**
1010
* Flatland Space Stations
@@ -46,156 +46,159 @@
4646
*
4747
* Problem: https://www.hackerrank.com/challenges/flatland-space-stations/problem
4848
*
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.
9449
*/
9550
public class FlatlandSpaceStations {
51+
9652
public static int flatlandSpaceStations(
97-
int noOfCities,
53+
int numOfCities,
9854
int[] spaceStations) {
99-
int noOfSpaceStations = spaceStations.length;
55+
int numOfSpaceStations = spaceStations.length;
10056

101-
if (noOfCities == noOfSpaceStations)
57+
if ( isCitySpaceStation(numOfCities,
58+
numOfSpaceStations))
10259
return 0;
10360

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)) {
11162

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);
12265
}
12366

12467
Arrays.sort(spaceStations);
12568

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(
13270
spaceStations,
133-
noOfSpaceStations,
134-
noOfCities))
71+
numOfSpaceStations,
72+
numOfCities))
13573
return spaceStations[0];
13674

13775
int maxDistance = 0;
13876

139-
// If first city is not a space station
140-
if (spaceStations[0] != 0) {
141-
maxDistance = spaceStations[0];
77+
maxDistance = setMaxDistanceBasedOnFirstSpaceStation(
78+
spaceStations);
14279

143-
} else if ( spaceStations[0] == 0 ) {
144-
maxDistance = (spaceStations[1]-spaceStations[0])/2;
145-
}
80+
maxDistance = getMaxDistance(spaceStations,
81+
numOfSpaceStations, maxDistance);
14682

147-
for (inti = 1; i < noOfSpaceStations-1; i++) {
148-
intdistance = (spaceStations[i+1] - spaceStations[i])/2;
83+
if ( lastCityIsNotLastSpaceStation(numOfCities,
84+
spaceStations, numOfSpaceStations)) {
14985

150-
if ( distance > maxDistance )
151-
maxDistance = distance;
86+
return distanceFromLastCityToLastStation(
87+
numOfCities, spaceStations,
88+
numOfSpaceStations, maxDistance);
15289
}
15390

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+
}
158110

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;
161143
}
162144

163145
return maxDistance;
164146
}
165147

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+
183155
public static boolean areSpaceStationsConsecutiveLast(
184156
int[] spaceStations,
185157
int numberOfSpaceStations,
186158
int numberOfCities) {
187159

188160
int lastSpaceStation = spaceStations[numberOfSpaceStations - 1];
161+
int lastCity = numberOfCities-1;
189162

190-
if ( lastSpaceStation != numberOfCities-1)
163+
if ( lastSpaceStation != lastCity)
191164
return false;
192165

193166
int firstSpaceStation = spaceStations[0];
194-
int consecutiveLast = (lastSpaceStation - firstSpaceStation) -
195-
(numberOfSpaceStations - 1);
196167

197-
return ( consecutiveLast == 0 ) ? true : false;
168+
return numberOfSpaceStations >
169+
lastSpaceStation - firstSpaceStation;
170+
198171
}
199-
}
200172

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])?");
201189

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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /