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 8ddc8de

Browse files
committed
Solved Hackerrank FlatlandSpaceStations
1 parent 720047a commit 8ddc8de

File tree

2 files changed

+425
-203
lines changed

2 files changed

+425
-203
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package hackerrank.FlatlandSpaceStations;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Disabled;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Flatland Space Stations
11+
* Flatland is a country with a number of cities,
12+
* some of which have space stations. Cities are
13+
* numbered consecutively and each has a road of
14+
* length 1km connecting it to the next city.
15+
*
16+
* It is not a circular route, so the first city
17+
* doesn't connect with the last city.
18+
*
19+
* Determine the maximum distance from any city
20+
* to its nearest space station.
21+
*
22+
* Function Description
23+
* Complete the flatlandSpaceStations function in the editor below.
24+
*
25+
* flatlandSpaceStations has the following parameter(s):
26+
*
27+
* int n: the number of cities
28+
* int c[m]: the indices of cities with a space station
29+
*
30+
* Returns
31+
* - int: the maximum distance any city is from a space station
32+
*
33+
* Input Format
34+
* The first line consists of two space-separated integers n and m.
35+
*
36+
* The second line contains space-separated integers,
37+
* the indices of each city that has a space-station.
38+
* These values are unordered and distinct.
39+
*
40+
* Constraints
41+
* 1 <= n <= 10^5
42+
* 1 <= m <= n
43+
*
44+
* There will be at least 1 city with a space station.
45+
* No city has more than one space station.
46+
*
47+
* Problem: https://www.hackerrank.com/challenges/flatland-space-stations/problem
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+
*/
95+
public class FlatlandSpaceStations {
96+
public static int flatlandSpaceStations(
97+
int noOfCities,
98+
int[] spaceStations) {
99+
int noOfSpaceStations = spaceStations.length;
100+
101+
if (noOfCities == noOfSpaceStations)
102+
return 0;
103+
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) {
111+
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+
}
122+
}
123+
124+
Arrays.sort(spaceStations);
125+
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(
132+
spaceStations,
133+
noOfSpaceStations,
134+
noOfCities))
135+
return spaceStations[0];
136+
137+
int maxDistance = 0;
138+
139+
// If first city is not a space station
140+
if (spaceStations[0] != 0) {
141+
maxDistance = spaceStations[0];
142+
} else if ( spaceStations[0] == 0 ) {
143+
maxDistance = (spaceStations[1]-spaceStations[0])/2;
144+
}
145+
146+
for (int i = 1; i < noOfSpaceStations-1; i++) {
147+
int distance = (spaceStations[i+1] - spaceStations[i])/2;
148+
149+
if ( distance > maxDistance )
150+
maxDistance = distance;
151+
}
152+
153+
// If last space station is not the last city
154+
if ( spaceStations[noOfSpaceStations-1] != noOfCities -1 ) {
155+
int lastSpaceStationDistance = noOfCities - 1 -
156+
spaceStations[noOfSpaceStations-1] ;
157+
158+
return maxDistance > lastSpaceStationDistance ?
159+
maxDistance : lastSpaceStationDistance;
160+
}
161+
162+
return maxDistance;
163+
}
164+
165+
/**
166+
* The space stations are consecutive last,
167+
* i.e. They are the last ones.
168+
* Given 5 cities with 2 space stations that
169+
* are in the end. i.e. Cities 3 and 4 are
170+
* the space stations.
171+
*
172+
* Get the last station
173+
* Check if the last city is the last station,
174+
* spaceStations[noOfSpaceStations-1] == noOfCities-1
175+
*
176+
* Get the first station spaceStations[0]
177+
*
178+
* How to confirm that space stations are
179+
* consecutive last?
180+
*
181+
*/
182+
public static boolean areSpaceStationsConsecutiveLast(
183+
int[] spaceStations,
184+
int numberOfSpaceStations,
185+
int numberOfCities) {
186+
187+
int lastSpaceStation = spaceStations[numberOfSpaceStations - 1];
188+
189+
if ( lastSpaceStation != numberOfCities-1 )
190+
return false;
191+
192+
int firstSpaceStation = spaceStations[0];
193+
int isLast = (lastSpaceStation - firstSpaceStation) -
194+
(numberOfSpaceStations - 1);
195+
196+
return ( isLast == 0 ) ? true : false;
197+
}
198+
}
199+
200+

0 commit comments

Comments
(0)

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