12345678910111213141516171819202122232425262728293031323334353637 |
- func findRadius(houses []int, heaters []int) (radius int) {
- sort.Ints(heaters)
- n := len(heaters)
- for _, i := range houses {
- beg, end := 0, n-1
- for beg <= end {
- mid := beg + (end-beg)/2
- if heaters[mid] < i {
- beg = mid + 1
- } else {
- end = mid - 1
- }
- }
- if beg == 0 {
- radius = maxInt(radius, heaters[beg]-i)
- } else if beg == n {
- radius = maxInt(radius, i-heaters[n-1])
- } else {
- radius = maxInt(radius, minInt(i-heaters[beg-1], heaters[beg]-i))
- }
- }
- return
- }
- func minInt(x, y int) int {
- if x < y {
- return x
- }
- return y
- }
- func maxInt(x, y int) int {
- if x < y {
- return y
- }
- return x
- }
|