-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path164_MaximumGap.py
39 lines (33 loc) · 975 Bytes
/
164_MaximumGap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
"""
Radix sort, you can see the explanation follow the links.
https://www.cs.usfca.edu/~galles/visualization/RadixSort.html
And here is a java solution in leetcode's discuss:
https://leetcode.com/discuss/53636/radix-sort-solution-in-java-with-explanation
"""
def maximumGap(self, nums):
if not nums:
return 0
max_num = max(nums)
bucket = [[] for i in range(10)]
exp = 1
while max_num / exp > 0:
for num in nums:
bucket[(num / exp) % 10].append(num)
nums = []
for each in bucket:
nums.extend(each)
bucket = [[] for i in range(10)]
exp *= 10
max_gap = 0
for i in range(1, len(nums)):
max_gap = max(max_gap, nums[i] - nums[i - 1])
return max_gap
"""
[]
[1]
[9,12,4,8,6,16,23]
[2,99999999]
"""