Heaters Solutions in Go
Number 475
Difficulty Easy
Acceptance 33.2%
Link LeetCode
Other languages —
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("math""sort")func findRadius(houses []int, heaters []int) int {minRad := 0sort.Ints(heaters)for _, house := range houses {// 遍历房子的坐标,维护 heaters 的最小半径heater := findClosestHeater(house, heaters)rad := heater - houseif rad < 0 {rad = -rad}if rad > minRad {minRad = rad}}return minRad}// 二分搜索func findClosestHeater(pos int, heaters []int) int {low, high := 0, len(heaters)-1if pos < heaters[low] {return heaters[low]}if pos > heaters[high] {return heaters[high]}for low <= high {mid := low + (high-low)>>1if pos == heaters[mid] {return heaters[mid]} else if pos < heaters[mid] {high = mid - 1} else {low = mid + 1}}// 判断距离两边的 heaters 哪个更近if pos-heaters[high] < heaters[low]-pos {return heaters[high]}return heaters[low]}// 解法二 暴力搜索func findRadius1(houses []int, heaters []int) int {res := 0for i := 0; i < len(houses); i++ {dis := math.MaxInt64for j := 0; j < len(heaters); j++ {dis = min(dis, abs(houses[i]-heaters[j]))}res = max(res, dis)}return res}func max(a int, b int) int {if a > b {return a}return b}func min(a int, b int) int {if a > b {return b}return a}func abs(a int) int {if a > 0 {return a}return -a}