forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
96 lines (78 loc) · 3.04 KB
/
Solution.java
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
//Problem: https://www.hackerrank.com/challenges/flatland-space-stations
//Java 8
/*
5 2
0 4
[1 0 0 0 1]
update indices with space stations
leftNearest
rightNearest
maxDistance
move through the array keeping track of left and
right distance at each point
while updating our max
I expect this to take O(n)
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int n = input.nextInt(); //Number of cities
int m = input.nextInt(); //Number of spacestations
int[] map = new int[n]; //Map of all the cities
int[] distanceMap = new int[n]; //Map containing distances to the nearest spacestation
int leftNearest = n+1; //The spacestation nearest to a city from the left side
int rightNearest = -1; //The spacestation nearest to a city from the right side
int maxDistance = 0; //The max distance you have to travel from any city to a spacestation
//Mark cities who have a spacestation
for(int i = 0; i < m; i++)
{
int spacestation = input.nextInt();
map[spacestation] = 1;
rightNearest = (spacestation > rightNearest) ? spacestation : rightNearest;
leftNearest = (spacestation < leftNearest) ? spacestation : leftNearest;
}
//Scan left right calculating value based on left nearest
for(int i = 0; i < n; i++)
{
if(map[i] == 1)//If there is a spacestation in a given city then distance is 0 and it is the new leftNearest
{
distanceMap[i] = 0;
leftNearest = i;
}
else
{
if(i > leftNearest)
{
//Update the min distance from the leftNearest on the distance map
distanceMap[i] = i - leftNearest;
}
else //Occurs when there is not leftNearest spaceStation yet
{
distanceMap[i] = n+1;
}
}
}
//Scan right left calculating value base on right nearest
for(int i = n-1; i>= 0; i--)
{
if(map[i] == 1)
{
rightNearest = i;
}
else
{
if(rightNearest > i)
{
//Update the min distance for each point on the distance map
distanceMap[i] = (distanceMap[i] > rightNearest - i) ? rightNearest - i : distanceMap[i];
}
}
//Keep track of the maxDistance
maxDistance = (distanceMap[i] > maxDistance) ? distanceMap[i] : maxDistance;
}
System.out.println(maxDistance);
}
}