Skip to content

Commit 720047a

Browse files
committedJul 15, 2022
Hackerrank FaltlandSpaceStations WIP
After implementing new logic, assertion fails when the space station is at the beginning - hasFourCitiesTwoStationsAtFirstAndThird
1 parent 68aea3b commit 720047a

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed
 

‎src/hackerrank/FlatlandSpaceStations.java

+56-15
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,22 @@
9494
*/
9595
public class FlatlandSpaceStations {
9696
public static int flatlandSpaceStations(int noOfCities, int[] spaceStations){
97-
int noSpaceStations = spaceStations.length;
97+
int noOfSpaceStations = spaceStations.length;
9898

99-
if (noOfCities == noSpaceStations) return 0;
99+
if (noOfCities == noOfSpaceStations) return 0;
100100
// if ( noOfCities == 1 && noSpaceStations == 1 ) return 0;
101-
else if ( noOfCities == 2 && noSpaceStations == 1 ) return 1;
101+
else if ( noOfCities == 2 && noOfSpaceStations == 1 ) return 1;
102102
// else if ( noOfCities == 2 && noSpaceStations == 2 ) return 0;
103-
else if ( noOfCities == 3 && noSpaceStations == 2 ) return 1;
103+
else if ( noOfCities == 3 && noOfSpaceStations == 2 ) return 1;
104104

105105

106-
if ( noSpaceStations == 1 ) {
106+
if ( noOfSpaceStations == 1 ) {
107107
int firstSpaceStation = spaceStations[0];
108108

109109
if ( noOfCities == 3 )
110110
return Math.max(firstSpaceStation, noOfCities-firstSpaceStation-1);
111111

112-
else if ( firstSpaceStation == noSpaceStations-1 || noSpaceStations == 3 )
112+
else if ( firstSpaceStation == noOfSpaceStations-1 || noOfSpaceStations == 3 )
113113
return Math.max(firstSpaceStation-1, noOfCities-firstSpaceStation-1);
114114

115115
else
@@ -122,18 +122,47 @@ else if ( firstSpaceStation == noSpaceStations-1 || noSpaceStations == 3 )
122122

123123
int maxDistance = 0;
124124
int i=0;
125-
for(; i<noSpaceStations-1; i++){
126-
int distanceDiff = spaceStations[i+1]-spaceStations[i];
127125

128-
if ( maxDistance < distanceDiff)
129-
maxDistance = distanceDiff;
130-
}
126+
// for(; i<noSpaceStations-1; i++) {
131127

132-
if (spaceStations[i] < noSpaceStations) {
133-
int distanceFromLast = noOfCities-1-spaceStations[i];
128+
//check if the space station are consecutive last
129+
// if (spaceStations[i] == noSpaceStations-2)
130+
//
131+
//
132+
// int distanceDiff = spaceStations[i+1]-spaceStations[i];
133+
//
134+
// if ( maxDistance < distanceDiff)
135+
// maxDistance = distanceDiff;
136+
// }
134137

135-
if (maxDistance < distanceFromLast)
136-
return distanceFromLast;
138+
// if (spaceStations[i] < noSpaceStations) {
139+
// int distanceFromLast = noOfCities-1-spaceStations[i];
140+
//
141+
// if (maxDistance < distanceFromLast)
142+
// return distanceFromLast;
143+
// }
144+
145+
// lets get the distance from the first city
146+
// to first space station
147+
int j=0;
148+
while( j < noOfCities-1 ) {
149+
// if space station is ahead of city
150+
if ( spaceStations[i] > j ) {
151+
maxDistance = spaceStations[i] - j;
152+
153+
// suppose next city is next to space station
154+
// it should not be a consecutive space station
155+
// if it is, increment the index until next
156+
// city is found.
157+
j = spaceStations[i]+1;
158+
for ( int k=j; k < noOfSpaceStations-1; k++ ){
159+
160+
if( k != spaceStations[k]){
161+
j = k;
162+
break;
163+
}
164+
}
165+
} else j++;
137166
}
138167

139168
return maxDistance;
@@ -168,6 +197,18 @@ void hasFourCitiesTwoStationsAtThirdAndFourth() {
168197

169198
Assertions.assertEquals(expectedMaxDistance,
170199
FlatlandSpaceStations.flatlandSpaceStations(noOfCities, spaceStaions));
200+
201+
/**
202+
* Assertion Fail.
203+
* Expected :2
204+
* Actual :1
205+
*
206+
* The logic to get the maximum distance fails.
207+
* As the stations are in the end, we have to
208+
* get the distance from the first city to that
209+
* station.
210+
*
211+
*/
171212
}
172213

173214
@Test

0 commit comments

Comments
 (0)
Failed to load comments.