This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"math" | |
"os" | |
"strconv" | |
"strings" | |
) | |
// Complete the flatlandSpaceStations function below. | |
func flatlandSpaceStations(n int32, c []int32) int32 { | |
var ( | |
m int32 = int32(len(c)) | |
i int32 = 0 | |
max int32 = 0 | |
) | |
if n == m { | |
return 0 | |
} | |
for i = 0; i < n; i++ { | |
var min int32 = n // max distance between any of two cities won't be longer than n | |
// step 1: Find the minimum distance of all cities to their nearest space station | |
for _, station := range c { | |
d := math.Abs(float64(i - station)) | |
if min > int32(d) { | |
min = int32(d) | |
} | |
} | |
// step 2: Find the maximum distance from the step 1 result | |
if max < min { | |
max = min | |
} | |
} | |
return max | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment