Leetcode - 排序(一)

目录

164. 最大间距

274. H 指数


 

164. 最大间距

https://leetcode-cn.com/problems/maximum-gap/

给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。如果数组元素个数小于 2,则返回 0。

示例 1:输入: [3,6,9,1],输出: 3。解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。
示例 2:输入: [10],输出: 0。解释: 数组元素个数小于 2,因此返回 0。
说明:你可以假设数组中所有元素都是非负整数,且数值在 32 位有符号整数范围内。请尝试在线性时间复杂度和空间复杂度的条件下解决此问题。

题解

一:快速排序,相邻元素比较,时间复杂度O(nlgn)

二:计数排序,时间复杂度O(n + m)。n是数组的长度,m是数组最大元素与最小元素的差值+1,未通过,超出时间限制了

三:转自https://leetcode-cn.com/problems/maximum-gap/solution/tong-pai-xu-by-powcai/https://leetcode-cn.com/problems/maximum-gap/solution/zui-da-jian-ju-by-leetcode/

这边的bucketNum = max(1, (maxNum - minNum) // bucketSize) + 1是因为方便边界值的处理,每个区间都是左闭右开。最大间隔必定不会存在在桶内,因为最大间隔必定大于等于,等间隔的时候取等于,等间隔,任意两个元素的差值都是答案,无所谓桶内筒外。我们对数组内全是相同数值的元素要特殊处理一下。直接返回0即可,即最大值等于最小值返回0。因为相等,都在一个桶内,没有桶间的概念。

from math import ceil
class Bucket:
    def __init__(self):
        self.usedId = False 
        self.maxVal = float("-inf")
        self.minVal = float("inf")

class Solution(object):
    def maximumGap(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        N = len(nums)
        if N < 2:
            return 0       
        
        maxNum = max(nums)
        minNum = min(nums)

        if maxNum == minNum:
            return 0
        
        bucketSize = max(1, (maxNum - minNum) // (N - 1))
        bucketNum = max(1, (maxNum - minNum) // bucketSize) + 1

        bucket = [Bucket() for _ in range(bucketNum)]

        for num in nums:
            bucketId = (num - minNum) // bucketSize 
            bucket[bucketId].usedId = True
            bucket[bucketId].maxVal = max(bucket[bucketId].maxVal, num)
            bucket[bucketId].minVal = min(bucket[bucketId].minVal, num)
        
        # 第一个箱子肯定有值
        prevMax, ans = bucket[0].maxVal, 0
        for i in range(1, bucketNum):
            if bucket[i].usedId:
                ans = max(ans, bucket[i].minVal - prevMax)
                prevMax = bucket[i].maxVal

        return ans

274. H 指数

https://leetcode-cn.com/problems/h-index/

给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。

h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数 不超过 h 次。)

例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。

示例:输入:citations = [3,0,6,1,5],输出:3 
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。

题解

一:快速排序

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        n = len(citations)
        citations = sorted(citations, reverse=True)
        if n == 0 or citations[0] == 0:
            return 0
                    
        res = 0
        for i in range(n):
            if citations[i] >= i + 1:
                res = i + 1
    
        return res

二:计数排序,转自https://leetcode-cn.com/problems/h-index/solution/hzhi-shu-by-leetcode/

class Solution(object):
    def hIndex(self, citations):
        n = len(citations)
        if n == 0:
            return 0
        rec = [0] * (n + 1)

        for citation in citations:
            # h指数不会超过n,将引用数降到n不会产生影响
            if citation >= n:
                rec[n] += 1
            else:
                rec[citation] += 1

        cnt = 0
        for i in range(n, -1, -1):
            cnt += rec[i]
            if cnt >= i:
                return i
        return cnt

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值