Fast and Easy Python Solution


  • -1
    J
    class Solution(object):
        def maximumGap(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            nums.sort()
            l = len(nums)
            if l < 2:
                return 0
            m = 0
            for i in range(l-1):
                m = max(m,abs(nums[i]-nums[i+1]))
            return m
    

    Sort the array, check two neighbors and keep track of the best difference


  • 0
    O

    However, sorting the array has NlogN time complexity, which makes the solution trivial. The hard part is how to get it in linear time and space


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.