Leetcode 164 数组最大间隔(线性复杂度实现)//Python

LeetCode 地址:https://leetcode.com/problems/maximum-gap/description/

题目描述

给定一个未排序数组,找出排序后的元素之间的最大间隔。

要求用线性时间复杂度实现。

例如:输入为【3,6,9,1,10】,输出应为最大间隔3。

题目分析

线性时间复杂度实现,就说明不能先对元素进行排序,因为采用此法复杂度为O(n?log(n))

思路是采用桶排序的方法。

首先,求随机数组中的最大元素 , 最小元素,为线性时间复杂度;

然后,在最大元素和最小元素之间,分配(N-1)个间隔,即为N个桶(bucket)。每个桶为一个有序实数对(None,None),将每个数依次往目标桶里塞。

其中,桶的上下界若已有元素(存在一个桶对应多个元素),则只需计算新元素与下界的较小值,同时计算其与上界的较大值,并将该最值作为新下界/上界即可;

最后,计算每个桶的下界与下一个桶的上节的差值。差值最大值即为最大间隔。

时间复杂度为O(n+b)≈O(n)

完整代码(Python)

def maximumGap(num):
    if len(num) < 2 or min(num) == max(num):
        return 0
    a, b = min(num), max(num)
    size = (b-a)//(len(num)-1) or 1
    bucket = [[None, None] for _ in range((b-a)//size+1)]
    for n in num:
        b = bucket[(n-a)//size]
        b[0] = n if b[0] is None else min(b[0], n)
        b[1] = n if b[1] is None else max(b[1], n)
    bucket = [b for b in bucket if b[0] is not None]
    return max(bucket[i][0]-bucket[i-1][1] for i in range(1, len(bucket)))

原文地址:https://www.cnblogs.com/Sanshibayuan/p/9105794.html

时间: 2024-10-08 06:46:36

Leetcode 164 数组最大间隔(线性复杂度实现)//Python的相关文章

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

[leetcode]Binary Tree Zigzag Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between

[leetcode]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4-&g

leetcode旋转数组查找 二分查找的变形

http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return it

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if

[leetcode]Flatten Binary Tree to Linked List @ Python

原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6Hints: If you notice carefully

LeetCode:数组中的第K个最大元素【215】

LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4 说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度. 题目分析 我们主要来学习一个新的集合类型--优先队列.优先队列作用是保证每次取

python 参议院文本预处理的一维数组的间隔空间

#!/usr/bin/python import re def pre_process_msg ( msgIn ): if msgIn=="": return "msgIn_Input_Error,should'nt Null, it is Strings" else: #1 trim msg = msgIn msg = msg.strip() #2 process msg internal special char replace with " &quo