Skip to content

Instantly share code, notes, and snippets.

@oscarzhou
Last active December 12, 2019 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarzhou/423e58acda59c722c1809658b896028b to your computer and use it in GitHub Desktop.
Save oscarzhou/423e58acda59c722c1809658b896028b to your computer and use it in GitHub Desktop.
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