[Swift]LeetCode164. 最大間距 | Maximum Gap

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-hcgradzg-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.git

Return 0 if the array contains less than 2 elements.github

Example 1:數組

Input: [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either
             (3,6) or (6,9) has the maximum difference 3.

Example 2:微信

Input: [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.

Note:less

  • You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
  • Try to solve it in linear time/space.

給定一個無序的數組,找出數組在排序以後,相鄰元素之間最大的差值。spa

若是數組元素個數小於 2,則返回 0。code

示例 1:orm

輸入: [3,6,9,1]
輸出: 3
解釋: 排序後的數組是 [1,3,6,9], 其中相鄰元素 (3,6) 和 (6,9) 之間都存在最大差值 3。

示例 2:htm

輸入: [10]
輸出: 0
解釋: 數組元素個數小於 2,所以返回 0。

說明:

  • 你能夠假設數組中全部元素都是非負整數,且數值在 32 位有符號整數範圍內。
  • 請嘗試在線性時間複雜度和空間複雜度的條件下解決此問題。

48ms

 1 class Solution {
 2     func maximumGap(_ nums: [Int]) -> Int {
 3 if nums.count < 2 {
 4         return 0
 5     }
 6     var minNums = INTPTR_MAX
 7     var maxNums = 0
 8     for i in nums {
 9         if i < minNums {
10             minNums = i
11         }
12         if i > maxNums {
13             maxNums = i
14         }
15     }
16     let gap = max(1,(maxNums - minNums) / (nums.count - 1))
17     let bucketNum = (maxNums - minNums) / gap + 1
18     var bucketMax = [Int](repeating: 0, count: bucketNum)
19     var bucketMin = [Int](repeating: INTPTR_MAX, count: bucketNum)
20     var bucketUsed = [Bool](repeating: false, count: bucketNum)
21     for i in nums {
22         let n = (i - minNums) / gap
23         bucketUsed[n] = true
24         if i > bucketMax[n] {
25             bucketMax[n] = i
26         }
27         if i < bucketMin[n] {
28             bucketMin[n] = i
29         }
30     }
31     var maxGap = 0
32     var previousMax = minNums
33     for n in (0..<bucketNum) {
34         if bucketUsed[n] {
35             maxGap = max(maxGap, bucketMin[n] - previousMax)
36             previousMax = bucketMax[n]
37         }
38     }
39     return maxGap
40     }
41 }

52ms

 1 class Solution {
 2     func maximumGap(_ nums: [Int]) -> Int {
 3         var maxGap = 0
 4         if nums.count < 2 { return maxGap }
 5         
 6         var start = 0
 7         let sortedArray = nums.sorted()
 8         while (start < sortedArray.count - 1) {
 9             let diff = sortedArray[start + 1] - sortedArray[start]
10             maxGap = max(diff, maxGap)
11             start += 1
12         }
13         
14         return maxGap
15     }
16 }

56ms

 1 class Solution {
 2     func maximumGap(_ nums: [Int]) -> Int {
 3         if nums.isEmpty || nums.count == 1 {return 0}
 4         var nums: [Int] = nums.sorted(by:<)
 5         var ret:Int = 0
 6         for i in 1..<nums.count
 7         {
 8             ret = max(ret, nums[i]-nums[i-1])
 9         }
10         return ret
11     }
12 }

56ms

 1 class Solution {
 2     func maximumGap(_ nums: [Int]) -> Int {
 3         if nums.count < 2 {
 4             return 0
 5         }
 6         var res = 0
 7         var maxn = 0
 8         var minn = Int.max
 9         for i in 0..<nums.count {
10             maxn = max(maxn, nums[i])
11             minn = min(minn, nums[i])
12         }
13         
14         let n = (maxn - minn) / nums.count + 1
15         let buckets = (maxn - minn) / n + 1
16         var maxs = Array(repeating: minn-1, count: buckets)
17         var mins = Array(repeating: maxn+1, count: buckets)
18         for i in 0..<nums.count {
19             let index = (nums[i] - minn) / n
20             mins[index] = min(mins[index], nums[i])
21             maxs[index] = max(maxs[index], nums[i])
22         }
23         
24         var i = 0
25         while i < buckets  {
26             while maxs[i] == minn - 1 {
27                 i+=1
28             }
29             var j = i+1
30             if j>=buckets {
31                 break
32             }
33             while mins[j] == maxn + 1 {
34                 j += 1
35             }
36             res = max(res,mins[j] - maxs[i])
37             
38             i = j
39         }
40         
41         return res
42     }
43 }
相關文章
相關標籤/搜索