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 60f44d7

Browse files
committed
Hackerrank Challenges - Flatland Space Station - WIP
Algorithm build for three cities with space stations
1 parent 47f27d2 commit 60f44d7

File tree

1 file changed

+261
-0
lines changed

1 file changed

+261
-0
lines changed
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
package hackerrank;
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(int n, int[] c){
97+
int spaceStations = c.length;
98+
99+
if ( n == 1 && spaceStations == 1 ) return 0;
100+
else if ( n == 2 && spaceStations == 1 ) return 1;
101+
else if ( n == 2 && spaceStations == 2 ) return 0;
102+
else if ( n == 3 && spaceStations == 2 ) return 1;
103+
104+
if ( spaceStations == 1 ) {
105+
int firstSpaceStation = c[0];
106+
107+
if ( n == 3 )
108+
return Math.max(firstSpaceStation, n-firstSpaceStation-1);
109+
110+
else if ( firstSpaceStation == c.length-1 || c.length == 3 )
111+
return Math.max(firstSpaceStation-1, n-firstSpaceStation-1);
112+
113+
else
114+
return Math.max(Math.abs(firstSpaceStation-1), n-firstSpaceStation);
115+
}
116+
117+
Arrays.sort(c);
118+
int maxDistance = 0;
119+
120+
int[] withLastCity = new int[spaceStations+1];
121+
withLastCity[spaceStations] = n-1;
122+
123+
System.arraycopy(c, 0, withLastCity, 0, spaceStations);
124+
// maximum distance between space stations
125+
126+
for(int i=0; i<spaceStations-1; i++){
127+
int distanceDiff = Math.abs(withLastCity[i]-withLastCity[i+1]);
128+
129+
if ( maxDistance < distanceDiff)
130+
maxDistance = distanceDiff;
131+
}
132+
133+
return maxDistance;
134+
}
135+
}
136+
137+
138+
class FlatlandSpaceStationsTest {
139+
140+
@Test
141+
@Disabled
142+
void hasFourCitiesThreeStationsAtFirstSecondLast() {
143+
int n=4, expected=1; // Distance from city four to city 2
144+
int[] c={0,1,3};
145+
Assertions.assertEquals(expected,
146+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
147+
148+
/**
149+
* The current implementation fails.
150+
* Expected :1
151+
* Actual :2
152+
*
153+
* The maximum distance being calculated
154+
*/
155+
}
156+
157+
@Test
158+
@Disabled
159+
void hasFourCitiesTwoStationsAtFirstAndSecond() {
160+
int n=4, expected=2; // Distance from city four to city 2
161+
int[] c={0,1};
162+
Assertions.assertEquals(expected,
163+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
164+
165+
/**
166+
* Get the maximum distance in space stations.
167+
* Here, 0 and 1 has 0 distance but there
168+
* 0 is the starting so no other cities and
169+
* there are cities after one as total cities
170+
* is 4.
171+
*/
172+
}
173+
174+
@Test
175+
void hasThreeCityTwoStationsAtFirstAndThird() {
176+
int n=3, expected=1;
177+
int[] c={0,2};
178+
Assertions.assertEquals(expected,
179+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
180+
}
181+
182+
@Test
183+
void hasThreeCityTwoStations() {
184+
int n=3, expected=1;
185+
int[] c={0,1};
186+
Assertions.assertEquals(expected,
187+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
188+
}
189+
190+
@Test
191+
void hasTwoCityTwoStations() {
192+
int n=2, expected=0;
193+
int[] c={0,1};
194+
Assertions.assertEquals(expected,
195+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
196+
}
197+
198+
@Test
199+
void hasFourCitiesAndOneStationAtFirst() {
200+
int n=4;
201+
// Distance from first to fourth city 4-1 =3
202+
int expectedMaxDistance=3;
203+
int[] c={0};
204+
Assertions.assertEquals(expectedMaxDistance,
205+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
206+
}
207+
208+
@Test
209+
void hasFourCitiesAndOneStationAtThird() {
210+
int n=4, expectedMaxDistance=2;
211+
int[] c={2};
212+
Assertions.assertEquals(expectedMaxDistance,
213+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
214+
}
215+
216+
@Test
217+
void hasThreeCitiesAndOneStationAtThird() {
218+
int n=3, expectedMaxDistance=2;
219+
int[] c={2};
220+
Assertions.assertEquals(expectedMaxDistance,
221+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
222+
}
223+
224+
@Test
225+
void hasThreeCitiesAndOneStationAtSecond() {
226+
int n=3, expectedMaxDistance=1;
227+
int[] c={1};
228+
Assertions.assertEquals(expectedMaxDistance,
229+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
230+
}
231+
232+
@Test
233+
void hasThreeCitiesAndOneStationAtFirst() {
234+
/**
235+
* Case1: c0 has a station
236+
*
237+
* Get max distance from either end.
238+
*/
239+
int n=3, expectedMaxDistance=2;
240+
int[] c={0};
241+
Assertions.assertEquals(expectedMaxDistance,
242+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
243+
}
244+
245+
@Test
246+
void hasTwoCity() {
247+
int n=2, expected=1;
248+
int[] c={0};
249+
Assertions.assertEquals(expected,
250+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
251+
}
252+
253+
@Test
254+
void hasOneCity() {
255+
int n=1, expected=0;
256+
int[] c={0};
257+
Assertions.assertEquals(expected,
258+
FlatlandSpaceStations.flatlandSpaceStations(n, c));
259+
}
260+
261+
}

0 commit comments

Comments
(0)

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