[Swift]LeetCode475. 供暖器 | Heaters

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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

  1. Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
  2. Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
  3. As long as a house is in the heaters' warm radius range, it can be warmed.
  4. All the heaters follow your radius standard and the warm radius will the same. 

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. 給出的房屋和供暖器的數目是非負數且不會超過 25000。
  2. 給出的房屋和供暖器的位置均是非負數且不會超過10^9。
  3. 只要房屋位於供暖器的半徑內(包括在邊緣上),它就能夠獲得供暖。
  4. 全部供暖器都遵循你的半徑標準,加熱的半徑也同樣。

示例 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 }
相關文章
相關標籤/搜索