<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)164:最大間距

          共 2554字,需瀏覽 6分鐘

           ·

          2021-01-25 14:13

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?最大間距??,我們先來看題面:
          https://leetcode-cn.com/problems/maximum-gap/

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


          Return 0 if the array contains less than 2 elements.

          題意


          給定一個無序的數(shù)組,找出數(shù)組在排序之后,相鄰元素之間最大的差值。
          如果數(shù)組元素個數(shù)小于 2,則返回 0。

          樣例

          示例?1:

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

          示例?2:

          輸入: [10]
          輸出: 0
          解釋: 數(shù)組元素個數(shù)小于 2,因此返回 0。

          說明:
          你可以假設(shè)數(shù)組中所有元素都是非負整數(shù),且數(shù)值在 32 位有符號整數(shù)范圍內(nèi)。
          請嘗試在線性時間復(fù)雜度和空間復(fù)雜度的條件下解決此問題。



          解題

          https://www.cnblogs.com/powercai/p/11299825.html

          思路:

          不用線性時間復(fù)雜度,其實就是排序的題!代碼如下:
          class Solution:
          ????def maximumGap(self, nums:?List[int]) -> int:
          ????????n = len(nums)
          ????????if?n < 2: return?0
          ????????res?= 0
          ????????nums.sort()
          ????????for?i in range(1, n):
          ????????????res?= max(res, nums[i] - nums[i - 1])
          ????????return?res

          下面介紹一下用桶排序的方法:
          首先,要知道很關(guān)鍵一點:相鄰的最大差值一定不小于該數(shù)組的最大值減去最小值除以間隔個數(shù),取上界。即?maxnumminnum(n1)" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; overflow-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border-width: 0px; border-style: initial; border-color: initial; ">?maxnum?minnum(n?1)??maxnum?minnum(n?1)??,這里的?n?是數(shù)組的個數(shù)。
          我們可以用反證法證明,如果小于,會怎么呢?比如?[1, 2, 5], 利用上式得?gap=512=2" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; overflow-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border-width: 0px; border-style: initial; border-color: initial; ">gap=5?12=2gap=5?12=2。如果假設(shè)相鄰間隔最大為?1,那必然不能組成以上數(shù)組!
          接下來,我們開始放桶,一個桶里放多少個數(shù)呢?我們可以把相鄰間隔小于?gap放在一個桶里,那么最大間隔一定在相互桶之間產(chǎn)生!
          如何判斷這個數(shù)在哪個桶里呢?我們用?numminnumgap" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; overflow-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border-width: 0px; border-style: initial; border-color: initial; ">?num?minnumgap??num?minnumgap?表示?num?在哪個桶里(換句話說,離min_num有幾個gap)。我們比如數(shù)組為?[1, 2, 3, 4,7],我們排除最大值和最小值, 把其他數(shù)組放入桶中,如下圖:
          | | | |
          |2,3 | | 4 |
          |_ _ _| | _ _ _|
          同一個桶里,絕對不會出現(xiàn)最大的相鄰的差值!
          所以,我們只需要比較桶之間的差值,這樣我們只需要保持同一桶里的最大值,和最小值即可!
          代碼如下:

          class Solution:
          ????def maximumGap(self, nums:?List[int]) -> int:
          ????????n = len(nums)
          ????????if?n < 2: return?0
          ????????max_num = max(nums)
          ????????min_num = min(nums)
          ????????gap = math.ceil((max_num - min_num)/(n - 1))
          ????????bucket = [[float("inf"), float("-inf")] for?_ in range(n - 1)]
          ????????#print(bucket)
          ????????# 求出每個桶的最大值,和最小值
          ????????for?num in nums:
          ????????????if?num == max_num or?num == min_num:
          ????????????????continue
          ????????????loc?= (num - min_num) // gap
          ????????????bucket[loc][0] = min(num, bucket[loc][0])
          ????????????bucket[loc][1] = max(num, bucket[loc][1])
          ????????##print(bucket)
          ????????# 遍歷整個桶
          ????????preMin = min_num
          ????????res?= float("-inf")
          ????????for?x, y?in bucket:
          ????????????if?x?== float("inf") :
          ????????????????continue
          ????????????res?= max(res, x?- preMin)
          ????????????preMin = y
          ????????res?= max(res, max_num - preMin)
          ????????return?res


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-160題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)161:相隔為1的編輯距離
          LeetCode刷題實戰(zhàn)162:尋找峰值
          LeetCode刷題實戰(zhàn)163:缺失的區(qū)間


          瀏覽 17
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲插逼视频 | www黄片| 免费无码人成视在线观看不卡 | 亚洲国产精品毛片一区二区三区 | 一级学生妹在线无码 |