当前位置: 首页 > news >正文

LeetCode -- 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 may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.


本题直接使用.net内置(或其他编程语言)的排序算法就可以,然后计算间隔找出最大的就行了。






以下是一种最简单的实现方式,调用 LINQ的sort(内部实现为stable的快排)然后计算间隔。 

public class Solution {
    public int MaximumGap(int[] nums) 
    {
        if(nums == null || nums.Length < 2){
    		return 0;
    	}
    	
    	var lst = nums.OrderBy(n=>n).ToList();
    	var max = 0;
    	for(var i = 0;i < lst.Count - 1; i++){
    		max = Math.Max(max, lst[i+1] - lst[i]);
    	}
    	
    	return max;
    }




}







官方的答案是使用桶排:
1. 求出最大最小值,一共需要nums.Length / (max - min)个桶
2. 遍历nums的过程中判断nums[i]属于哪个桶,然后将元素放入指定的桶中
3. 维护每个桶的最大最小值
4. 遍历桶的最值,它们之间的间隔(bucket[i-1]的最小值与bucket[i]的最大值)




以下实现参考了:
http://www.programcreek.com/2014/03/leetcode-maximum-gap-java/


public class Solution {
    public int MaximumGap(int[] nums) 
    {
        if(nums == null || nums.Length < 2){
    		return 0;
    	}
    	
    	// find the max and min
    	int max = nums[0];
        int min = nums[0];
        for(int i = 1; i < nums.Length; i++){
            max = Math.Max(max, nums[i]);
            min = Math.Min(min, nums[i]);
        }
     
        // init bucket
        var buckets = new Bucket[nums.Length + 1];
        for(int i=0; i < buckets.Length; i++){
            buckets[i] = new Bucket();
        }
     
        double height = (double) nums.Length / (max - min);// bucket height
        
        // loop each element in nums, find the bucket for it.
        for(int i=0; i<nums.Length; i++){
            int index = (int) ((nums[i] - min) * height);
     
            if(buckets[index].low == null){
                buckets[index].low = nums[i];
                buckets[index].high = nums[i];
            }else{
                buckets[index].low = Math.Min(buckets[index].low.Value, nums[i]);
                buckets[index].high = Math.Max(buckets[index].high.Value, nums[i]);
            }
        }


        int result = 0;
        int prev = buckets[0].high.Value;
        for(int i=1; i<buckets.Length; i++){
            if(buckets[i].low != null){
                result = Math.Max(result, buckets[i].low.Value-prev);
                prev = buckets[i].high.Value;
            }
     
        }
     
        return result;
    }


public class Bucket{
   public int? low;
   public int? high;
    public Bucket(){


    }
}


}


相关文章:

  • OO系统分析员之路--用例分析系列(8)--如何编写一份完整的UML需求规格说明书[整理重发]...
  • LeetCode -- Maximum Subarray
  • ArcGIS Server Java ADF 案例教程 25
  • LeetCode -- Minimum Depth of Binary Tree
  • ArcGIS Server Java ADF 案例教程 26
  • LeetCode -- Minimum Path Sum
  • LeetCode -- Multiply Strings
  • ArcGIS Server Java ADF 案例教程 27
  • LeetCode -- Permutations II
  • SQL语句性能调整之ORACLE的执行计划
  • LeetCode -- Product of Array Except Self
  • 不知道为什么我的一oracle的sql调优文章笔记无法发表,提示“文章中出现禁止的词语,系统不予接受。”...
  • LeetCode -- Remove Duplicates From Sorted Array 2
  • 好人陈虻
  • LeetCode -- Reverse Bits
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • Apache的基本使用
  • const let
  • Lsb图片隐写
  • mockjs让前端开发独立于后端
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • Spring Boot MyBatis配置多种数据库
  • Web标准制定过程
  • 安卓应用性能调试和优化经验分享
  • 从0实现一个tiny react(三)生命周期
  • 猴子数据域名防封接口降低小说被封的风险
  • 机器学习学习笔记一
  • 每天10道Java面试题,跟我走,offer有!
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 深入浅出webpack学习(1)--核心概念
  • 阿里云移动端播放器高级功能介绍
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (C++17) optional的使用
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (JS基础)String 类型
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (八)c52学习之旅-中断实验
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (九)c52学习之旅-定时器
  • .Net Memory Profiler的使用举例
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .Net8 Blazor 尝鲜
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .NET项目中存在多个web.config文件时的加载顺序
  • /etc/shadow字段详解
  • @DataRedisTest测试redis从未如此丝滑
  • @entity 不限字节长度的类型_一文读懂Redis常见对象类型的底层数据结构
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现
  • [android] 切换界面的通用处理
  • [autojs]autojs开关按钮的简单使用