[LeetCode]164. Maximum Gap

挑战第七天:Llamaindex RAG 实践 安装python 依赖包,并安装 Llamaindex和相关包以及Sentence Transformer 模型。进入开发机后,创建新的conda环境,命名为。镜像,然后在资源配置中,使用。的选项,然后立即创建开发机器。后,点击 选择镜像 使用。然后安装相关基础依赖。 阅读详情

原题链接:https://leetcode.com/problems/maximum-gap/

这道题如果是O(nlgn)方法肯定很简单,排序后找最大gap就行了。但如果要求O(n)时间完成的话,难度就大大加大了。所以我们用桶排序的方法来做这道题。 有关桶排序的简介,大家可以参考维基-桶排序。我们来介绍一下桶排序的应用,桶排序就是将一定范围的值全部丢到同一个桶里。所以我们只要合理安排桶的个数,让最大的差异出现的桶与桶之间即可。假设输入的数组中的个数是n,范围从a~b。则我们会发先最大的gap一定会大于等于ceiling(b-a)/(n-1),因为如果数组是等差数列,数与数之间的差异是ceiling(b-a)/(n-1),所以最大gap不可能比这个还小。所以我们假设让桶的容量变成ceiling(b-a)/(n-1),那最大差异一定发生的桶之间。所以我们只要设置(b-a)/[ceiling(b-a)/(n-1)]+1个桶即可。然后就是桶之间的比较我们只需比较上一个桶的最大值和当前桶的最小值即可。所以桶里只需维护一个最大值和一个最小值。时间复杂度是O(n),空间复杂度是O(n)

代码如下:

public class Solution {
    public class bucket
    {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
    }
    
    public int maximumGap(int[] nums) {
        if(nums.length <= 1)
            return 0;
        int len = nums.length;
        bucket[] buckets = new bucket[len];
        int min = nums[0];
        int max = nums[0];
        for(int i = 0;i<len;i++)
        {
            if(nums[i]<min)
                min = nums[i];
            if(nums[i]>max)
                max = nums[i];
        }
        int seg = (int)Math.ceil((double)(max-min)/(len-1));
        if(seg == 0)
            return 0;
        for(int i = 0;i<len;i++)
        {
            int t = nums[i];
            int k = (t-min)/seg;
            if(buckets[k]==null)
            	buckets[k] = new bucket();
            if(buckets[k].max<t)
                buckets[k].max = t;
            if(buckets[k].min>t)
                buckets[k].min = t;
        }
        int gap = 0;
        int preMax = max;
        for(int i = 0;i<buckets.length;i++)
        {
            if(buckets[i]!=null)
            {
                gap = Math.max(gap,buckets[i].min-preMax);
            	preMax = buckets[i].max; 
            }
        }
        return gap;
    }
}
这道题方法很巧妙,思路也就是想着如何把数据分组开始。如果遇到类似的变种题没思路,面试官提示用到桶排序,大家一定要知道该怎么往下想。

C语言使用开源库libusb进行USB设备操作 https://libusb.info/libusb is a C library that provides generic access to USB devices. It is intended to be used by developers to facilitate the production of applications that communicate with USB hardware. It is portable: Using a single cross-platform AP 阅读详情

相关推荐

BSW:MemMap模块配置及使用

(内存映射(MemMap)模块用于通过内存映射文件将代码和数据映射到特定的内存部分。MemMap生成特定的头文件,其中包含为MemorySection元素和编译器特定指令定义的内存映射预处理器。上面是比较官方的回答,说人话就是把数据和代码放在ECU内指定的地址。初级应用:软件中的bss,data,rodata,code,text,段需要在memory layout中划分具体的物理地址,对于多核系统,要将执行的数据和代码放在目标核的RAM和ROM。

Forever_Oct的博客 3693

164. Maximum Gap [leetcode]

这道题可以有两种刚解法: 解法一: 基于bucket sort的方法: 有n个未排序的数字。 1、先求出数组内最大数max和最小数min。 2、求出每个桶内的数字范围:gap = (max - min) / (n - 1)。 3、算出桶的数量: N = (max - min)/ gap + 1  (可知 N >= n) 每个桶对应的数字范围为:  第一个:[min, min

angie的博客 328

压测:Locust & WebSocket 二次开发

压测:Locust & WebSocket 二次开发 一、需求: 压测 WebSocket 接口性能问题。 二、问题: Locust 仅封装了Http Client;压测使用 WebSocket,需二次开发 三、解决方案:学习Http Client DrawScript.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ ------------------------------------------------- File Nam

Allan_shore_ma的博客 2344

【LeetCode解题报告】164. Maximum Gap

题目来源:https://leetcode.com/problems/maximum-gap/description/题目:Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Return 0 if the array contains les...

Angry_Bird16的博客 335

手把手教你用QEMU模拟AST2600-EVB开发板:OpenBMC环境搭建全流程

本文提供了一份详尽的实战指南,手把手教你使用QEMU模拟器搭建AST2600-EVB开发板的OpenBMC环境。内容涵盖从工具链准备、系统镜像获取(官方与自编译),到QEMU启动命令解析、网络配置(用户与桥接模式),以及通过SSH、WebUI和Redfish API访问服务的全流程,帮助开发者在个人电脑上快速构建安全、可复现的嵌入式开发与测试沙盒。

play7的博客 1139

FreeCAD MCP实战:用AI自然语言驱动CAD建模与自动化

在计算机辅助设计(CAD)领域,参数化建模和自动化脚本是提升设计效率的核心技术。其原理在于通过程序化指令控制三维模型的创建与修改,从而将设计意图转化为精确的几何实体。这种技术价值在于显著降低重复性操作成本,实现快速原型迭代和设计探索。在应用场景上,它尤其适用于需要频繁调整尺寸、生成系列化零件或执行规则化建模任务的工程与产品设计。本文聚焦的FreeCAD MCP项目,正是这一理念的先进实践。它通过集成Model Context Protocol(MCP),构建了一个连接Claude AI与FreeCAD的智能

weixin_26913055的博客 749

164. Maximum Gap

题目 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements...

分享人工智能学习心得与实践经验,探讨应用场景,见证变革与进步 769

miniDP与DP管脚定义差异详解及设计避坑指南

本文详细解析了Mini DisplayPort与标准DisplayPort接口在管脚定义上的核心差异,通过对比表格直观展示了两者20针脚的不同排列方式,特别是高速差分信号线的分布。文章结合真实设计案例,为硬件工程师提供了从原理图设计、PCB布局到信号完整性考量及故障排查的完整避坑指南,强调绝不能将两者视为简单的物理缩小版。

weixin_29248653的博客 371

164. Maximum Gap(最大间距)———附带思路和完整代码

文章目录0 效果1 题目2 思路3 代码 0 效果 1 题目 2 思路 朴素算法,先处理特殊情况,然后排序序列,依次比较两个相邻元素的差值并记录最大值。 3 代码 int cmp(const void* p1, const void* p2){ return *(int*)p2 - *(int*)p1; } int maximumGap(int* nums, int numsSize){ if(numsSize < 2) return 0; if(numsSize ==

韩江雪de 小屋 563

Vben Admin权限管理全解析:从动态菜单到按钮级控制(附代码示例)

本文全面解析Vben Admin框架的权限管理系统,从动态菜单生成到按钮级细粒度控制,提供完整的实现方案和代码示例。深入探讨基于角色-权限模型的架构设计,分享前后端协作的最佳实践,帮助开发者快速构建安全高效的企业级后台系统。

weixin_30919429的博客 491

YOLOv8改进 | 主干网络 | 将主干网络替换为轻量化的ShuffleNetv2【原理 + 完整代码】

yolov8,yolov8创新,yolov8改进

kay_545 2572

LeetCode: 164. Maximum Gap

LeetCode: 164. 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. Exampl...

杨领well的专栏 225

【keil5和STC-ISP】保姆级下载安装教程

点击下载中的产品下载。然后就会看到如下界面:可以看到,提供了针对不同芯片架构的四套独立开发工具。:你使用的51单片机,其核心就是。C51套件里的编译器是专门为这个架构优化的。:如果安装了MDK-Arm,你在创建新项目时将像“AT89C51”这样的51系列芯片型号,无法进行开发。进入如下界面:双击下载 → 双击运行安装 → 同意协议并下一步 → 使用默认安装路径 → 完成安装。

深耕CV 1714

leetcode-164 Maximum Gap

1.leetcode链接(hard) 2. 题目描述 给定未排序的数组,找到其排序形式中的连续元素之间的最大差异。 如果数组包含少于2个元素,则返回0。 Case example: Input: [3,6,9,1] Output: 3 Explanation: 排序后是[1,3,6,9], (3,6)或(6,9) 有最大差3. 3.思路分析 复杂度分析 ...

公众号/知乎: AI算法札记,欢迎关注交流 338

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Maximum Gap Leetcode #164 题解[Python]

题目来源 https://leetcode.com/problems/maximum-gap/description/ 题意分析 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array ...

Wayne_Mai的博客 1053

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode(164) Maximum Gap

题目如下: Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 eleme

BUILD 4811

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Leetcode 164 Maximum Gap 桶排序好题

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements

无名山丘,崛起成峰 4243

LeetCode164—Maximum Gap

LeetCode164—Maximum Gap

上善若水 763

[LeetCode164] Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements

coder 进阶的专栏 1414

LeetCode 164. Maximum Gap

LeetCode 164. Maximum Gap 问题来源LeetCode 164. Maximum Gap 问题描述 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear

yanqinghe123的专栏 501

Leetcode164 Maximum Gap

leetcode164题

YinRJ的博客 136

LeetCode(164)Maximum Gap

题目Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements.You ma

指尖飞舞 1263

leetcode[164] Maximum Gap

梅西刚梅开二度,我也记一题。 在一个没排序的数组里,找出排序后的相邻数字的最大差值。 要求用线性时间和空间。 如果用nlgn的话,直接排序然后判断就可以了。so easy class Solution { public: int maximumGap(vector<int> &num) { if (num.size() &...

132

Leetcode 164. Maximum Gap

这道题先排序再遍历一遍能够直接AC,但是在讨论区有人提出了使用桶排序的方式,理论上比直接调库排序更加快,但实际上直接调库速度更快。 首先,定义max, min, N分别为数组最大值、最小值、以及数组长度,根据抽屉原理,显然maximum gap ≥ gap = round_up[(max - min) / (N-1)]。反证法如果maximum gap < gap,则会出现 min +...

I_ren的博客 195

Leetcode 164 Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements

夜色之浓,莫过于黎明前的黑暗。 243

[leetcode] 164. Maximum Gap

题目链接:https://leetcode.com/problems/maximum-gap/ 题意:给定一个无序数组,求有序情况下相邻元素差最大为多少? 要求:时间复杂度和空间复杂度均为O(n)法一:我们很容易能够想到基数排序,它的时间复杂度和空间复杂度都是O(n),排好序之后扫一遍就行了。这样当然是没有问题的。代码如下:class Solution { public: void rad

曲径通幽处 1091
上一篇: [LeetCode]163. Missing Ranges
foolnote
博客等级 码龄11年 0粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值