★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-cflsuycw-me.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.git
Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.github
So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.微信
Note:app
Example 1:spa
Input: [1,2,3],[2] Output: 1 Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
Example 2:设计
Input: [1,2,3,4],[1,4] Output: 1 Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.
冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向全部房屋供暖。code
如今,给出位于一条水平线上的房屋和供暖器的位置,找到能够覆盖全部房屋的最小加热半径。htm
因此,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。blog
说明:
示例 1:
输入: [1,2,3],[2] 输出: 1 解释: 仅在位置2上有一个供暖器。若是咱们将加热半径设为1,那么全部房屋就都能获得供暖。
示例 2:
输入: [1,2,3,4],[1,4] 输出: 1 解释: 在位置1, 4上有两个供暖器。咱们须要将加热半径设为1,这样全部房屋就都能获得供暖。
112ms:二分法
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var heaters = heaters.sorted { $0 < $1 } 4 var minRadius = Int.min 5 for housePos in houses { 6 var left = 0 7 var right = heaters.count - 1 8 while left + 1 < right { 9 let mid = left + (right - left) / 2 10 if housePos < heaters[mid] { 11 right = mid 12 } else if housePos > heaters[mid] { 13 left = mid 14 } else { 15 left = mid 16 right = mid 17 } 18 } 19 minRadius = max(minRadius, min(abs(housePos - heaters[left]) , abs(housePos - heaters[right]) )) 20 } 21 return minRadius 22 } 23 }
116ms
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var heaters = heaters.sorted { $0 < $1 } 4 var minRadius = Int.min 5 for housePos in houses { 6 var left = 0 7 var right = heaters.count - 1 8 while left + 1 < right { 9 let mid = left + (right - left) / 2 10 if housePos < heaters[mid] { 11 right = mid 12 } else if housePos > heaters[mid] { 13 left = mid 14 } else { 15 left = mid 16 right = mid 17 } 18 } 19 minRadius = max(minRadius, min(abs(housePos - heaters[left]) , abs(housePos - heaters[right]) )) 20 } 21 return minRadius 22 } 23 }
120ms
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var heaters = heaters.sorted { $0 < $1 } 4 heaters.append(Int.max) 5 var minRadius = 0 6 for housePos in houses { 7 var left = 0 8 var right = heaters.count - 1 9 10 while left + 1 < right { 11 let mid = left + (right - left) / 2 12 if housePos <= heaters[mid] { 13 right = mid 14 } else if housePos > heaters[mid] { 15 left = mid 16 } 17 } 18 minRadius = max(minRadius, min(abs(housePos - heaters[left]),abs(housePos - heaters[right]))) 19 } 20 21 return minRadius 22 } 23 }
144ms
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var heaters = heaters.sorted() 4 var result = Int.min 5 6 for house in houses { 7 var i = binarySearch(heaters, house) 8 9 let rightDistance = i - 1 >= 0 ? (house - heaters[i - 1]) : Int.max 10 let leftDistance = i < heaters.count ? (heaters[i] - house) : Int.max 11 12 result = max(result, min(rightDistance, leftDistance)) 13 } 14 15 return result 16 } 17 private func binarySearch(_ nums: [Int], _ num: Int) -> Int { 18 var left = 0 19 var right = nums.count 20 var middle = 0 21 22 while left < right { 23 middle = (left + right) / 2 24 if nums[middle] < num { 25 left = middle + 1 26 } else { 27 right = middle 28 } 29 } 30 31 return left 32 } 33 }
152ms
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var heaters = heaters.sorted { $0 < $1 } 4 print(heaters) 5 var minRadius = Int.min 6 for housePos in houses { 7 var left = 0 8 var right = heaters.count - 1 9 while left + 1 < right { 10 let mid = left + (right - left) / 2 11 if housePos < heaters[mid] { 12 right = mid 13 } else if housePos > heaters[mid] { 14 left = mid 15 } else { 16 left = mid 17 right = mid 18 } 19 } 20 minRadius = max(minRadius, min(abs(housePos - heaters[left]) , abs(housePos - heaters[right]) )) 21 } 22 return minRadius 23 } 24 }
180ms
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var index = 0, maxvalue = 0 4 let sortHouses = houses.sorted() 5 let sortHeaters = heaters.sorted() 6 for house in sortHouses{ 7 while index < sortHeaters.count - 1 && house * 2 >= sortHeaters[index] + sortHeaters[index + 1]{ 8 index += 1 9 } 10 maxvalue = max(maxvalue,abs(sortHeaters[index] - house)) 11 } 12 return maxvalue 13 } 14 }
184ms
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var index = 0, maxvalue = 0 4 let sortHouses = houses.sorted() 5 let sortHeaters = heaters.sorted() 6 for house in sortHouses{ 7 while index < sortHeaters.count - 1 && house * 2 >= sortHeaters[index] + sortHeaters[index + 1]{ 8 index += 1 9 } 10 maxvalue = max(maxvalue,abs(sortHeaters[index] - house)) 11 } 12 return maxvalue 13 } 14 }
188ms
1 class Solution { 2 func findRadius(_ houses: [Int], _ heaters: [Int]) -> Int { 3 var i = 0, radius = 0 4 let houses = houses.sorted(), heaters = heaters.sorted() 5 6 for house in houses { 7 while i < heaters.count - 1 && 2 * house >= heaters[i] + heaters[i + 1] { 8 i += 1 9 } 10 11 radius = max(radius, abs(house - heaters[i])) 12 } 13 14 return radius 15 } 16 }