-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
148 lines (126 loc) · 5.9 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Problem: https://www.hackerrank.com/challenges/flatland-space-stations/problem
C# Language Version: 7.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
1. First set up bool array arr of all cities and mark all the space station cities with true.
2. Consider two indexes namely prev and next which represent indexes of previous and next space stations for any given
city. Initialize both to -1.
3. Initialize maxOfMin (which will keep track of the maximum distance found for any city from a space station) to zero.
4. Iterate through each city:
- If the city is a space station then set prev index of a space station to current city's index.
- If the city is not a space station then work out the prev and next space station indexes if they are not appropriately set.
Then find its minimum distance from either of prev or next space station indexes.
If the minimum distance found is greater than maxOfMin then set maxOfMin to new value.
A point of caution is that the next space station index can become stale once in a while when we have crossed it during
iteration so it will have to be searched again whenever the case happens.
5. Print maxOfMin on the screen.
Time Complexity: O(n) //there are no nested loops. There will be some extra traversals here and there to get nearest
//previous or next space station.
Space Complexity: O(n) //We have taken an array to store the presence/absence of a space station at each city of Flatland.
*/
using System;
class Solution
{
static int FlatlandSpaceStations(int n, int[] spaceStationIndexes)
{
var maxOfMin = 0;
//we can use bit array instead to save space but bool[] is way more faster.
//when I tried bit array then test case # 18 and 19 were resulting in time out.
var cities = new bool[n];
for (var i = 0; i < spaceStationIndexes.Length; i++)
cities[spaceStationIndexes[i]] = true;
var previousSpaceStation = -1;
var nextSpaceStation = -1;
for (var i = 0; i < n; i++)
{
var isSpaceStation = cities[i];
var currentMin = 0;
if (isSpaceStation)
previousSpaceStation = i;
else
{
if (previousSpaceStation == -1 && nextSpaceStation == -1)
{
//find next space station. You are guaranteed to find a space station in forward direction
//as there is a minimum of one space station in cities array. So traverse forward without bounds check.
var j = i;
while (!cities[j])
j++;
nextSpaceStation = j;
currentMin = nextSpaceStation - i;
}
else if (previousSpaceStation > -1 && nextSpaceStation == -1)
{
//find next space station. Then set currentMin
if (IsNextSpaceStationPresent(cities, i, out nextSpaceStation))
{
if (nextSpaceStation - i < i - previousSpaceStation)
currentMin = nextSpaceStation - i;
else
currentMin = i - previousSpaceStation;
}
else
currentMin = i - previousSpaceStation;
}
else if (previousSpaceStation == -1 && nextSpaceStation > -1)
{
//no calculation required. Just set currentMin
currentMin = nextSpaceStation - i;
}
else if (previousSpaceStation > -1 && nextSpaceStation > -1)
{
if (nextSpaceStation > i)
{
//we're in middle of two space stations. Find which is nearest and set
if (nextSpaceStation - i < i - previousSpaceStation)
currentMin = nextSpaceStation - i;
else
currentMin = i - previousSpaceStation;
}
else
{
//find next space station. Then set currentMin
if (IsNextSpaceStationPresent(cities, i, out nextSpaceStation))
{
if (nextSpaceStation - i < i - previousSpaceStation)
currentMin = nextSpaceStation - i;
else
currentMin = i - previousSpaceStation;
}
else
currentMin = i - previousSpaceStation;
}
}
}
if (currentMin > maxOfMin)
maxOfMin = currentMin;
}
return maxOfMin;
}
private static bool IsNextSpaceStationPresent(bool[] cities, int startIndex, out int nextSpaceStationIndex)
{
var spaceStationfound = false;
nextSpaceStationIndex = -1;
for (; startIndex < cities.Length; startIndex++)
{
if (cities[startIndex])
{
spaceStationfound = true;
nextSpaceStationIndex = startIndex;
break;
}
}
return spaceStationfound;
}
static void Main(String[] args)
{
var tokens_n = Console.ReadLine().Split(' ');
var n = int.Parse(tokens_n[0]);
var c_temp = Console.ReadLine().Split(' ');
var c = Array.ConvertAll(c_temp, int.Parse);
int result = FlatlandSpaceStations(n, c);
Console.WriteLine(result);
}
}