leetcode 719. Find K-th Smallest Pair Distance(找到第k小的距离)

避坑指南:在Ubuntu 22.04上配置UR5e与ROS2 Humble真机通信的完整流程 本文详细介绍了在Ubuntu 22.04上配置UR5e机械臂与ROS2 Humble真机通信的完整流程,包括环境准备、网络配置、URCap安装、ROS2驱动部署及控制器状态验证等关键步骤。特别针对常见问题如网络连接失败、驱动启动错误等提供了实用解决方案,帮助工程师高效完成UR5e与ROS2的集成工作。 阅读详情

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.

Example 1:
Input:
nums = [1,3,1]
k = 1
Output: 0
Explanation:
Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.

Note:
2 <= len(nums) <= 10000.
0 <= nums[i] < 1000000.
1 <= k <= len(nums) * (len(nums) - 1) / 2.

给一个数组,找到数组中所有元素对之间第k小的距离

思路:
1) brute force
计算所有pair的距离,然后用桶排序统计每个距离出现的次数,找到第k个距离。
桶排序用的数组size到数组中的最大值,作为最大值-0的距离。

//593ms
    public int smallestDistancePair(int[] nums, int k) {
        int maxNum = 0;
        int n = nums.length;
        int result = 0;
        int count = 0;
        for(int i = 0; i < n; i++) {
            if(nums[i] > maxNum) maxNum = nums[i];
        }
        int[] dis = new int[maxNum+1];
        
        for(int i = 0; i < n-1; i ++) {
            for(int j = i+1; j < n; j++) {
                dis[Math.abs(nums[j] - nums[i])] ++;
            }
        }
        //dis.length:maxNum
        for(int i = 0; i <= maxNum; i++) {
            count += dis[i];
            if(count >= k) {
                result = i;
                break;
            }
        }
        return result;
    }

2)binary search + DP
把问题转化一下
问题是要找到第k小的距离,转化为<=某距离的pair有k个
那么只需找到这个“某距离”即可

定义某距离为m,遍历数组,找到每个元素到其他元素的距离<=m的个数,定义dp[i]为:到index=i为止的元素,也就是nums[0] ~ nums[i]中每个元素到其它元素pair distance<=m的累加和。即dp[i] = dp[i-1] + nums[i]到其它元素pair distance<=m的个数。

下面就是如何计算nums[i]到其它元素pair distance<=m的个数。
先把数组升序排序,可以保证只计算nums[i]右边元素到nums[i]的距离。
找到第一个nums[j]使nums[j] - nums[i] > m,这样( nums[i] , nums[j] )区间的数字到nums[i]的距离就会<=m, 区间中数字的个数是(j - i - 1)
所以dp[i] = dp[i-1] + (j - i - 1)

当遍历完数组,累加了所有pair distance <= m的个数后,如果个数>k,说明m偏大,满足条件的个数过多,要把右边界调整到m-1。反之左边界调整到m+1。做binary search,直到找到合适的m使得个数==k。

因为dp每次只用到前面一个元素的信息,所以dp可以不用数组,降维到变量。

//4ms
    public int smallestDistancePair(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;
        int left = 0;
        int right = nums[n-1] - nums[0];  //最大distance
        
        while(left <= right) {
            int dp = 0;
            int mid = left + (right - left) / 2;   
            int j = 0;
            //找到nums[j] - nums[i] > mid, j是递增的,不用从0开始
            for(int i = 0; i < n-1; i++) {
                while(j < n && nums[j] - nums[i] <= mid) j++;
                dp += (j-i-1); //dp[i] = dp[i-1]+(j-i-1)
            }
            
            //distance太大,满足条件的很多,缩小right
            if(dp >= k) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

参考资料

在MacBook M1/M2上,手把手教你打包包含嵌入模型的RagFlow Docker镜像(避坑指南) 本文详细指导如何在MacBook M1/M2上构建ARM64架构的RagFlow Docker镜像,涵盖环境准备、依赖处理、镜像构建及性能优化全流程。特别针对Apple Silicon芯片的兼容性问题提供解决方案,帮助开发者高效完成本地开发环境搭建。 阅读详情

相关推荐

Gromacs文件格式全解析:xtc vs trr vs cpt,哪种更适合你的模拟需求?

本文深入解析Gromacs分子动力学模拟中的三种核心文件格式xtc、trr和cpt,详细对比其技术特性和适用场景。xtc适合存储效率优先的场景,trr保留完整动力学数据,cpt确保模拟精确重启。通过实际配置示例和存储优化技巧,帮助用户根据模拟需求选择最佳格式组合,平衡分析灵活性与存储成本。

weixin_29281941的博客 106

LeetCode(719):找出第 k 小的距离对 Find K-th Smallest Pair Distance(Java)

2019.11.13 从零单刷个人笔记整理(持续更新)](https://blog.csdn.net/qq_20304723/article/details/89401203) github:https://github.com/ChopinXBP/LeetCode-Babel 这题可以有一个直接的思路是用最大堆+归并,对于排序后的数组中的每一个元素nums[i],左侧最小的距离必定为nums[...

NJU_ChopinXBP的博客 301

WSL2 Ubuntu复现FoundationPose项目(docker)

复现一个基于深度学习的物体姿态估计框架FoundationPose项目,使用docker,WSL2

woaidawangwang的博客 2010

机器学习算法(二十二):局部离群因子检测方法 (Local Outlier Factor, LOF)

1 离群点挖掘方法 2LOF 算法背景 基于密度的离群点检测方法的关键步骤在于给每个数据点都分配一个离散度,其主要思想是:针对给定的数据集,对其中的任意一个数据点,如果在其局部邻域内的点都很密集,那么认为此数据点为正常数据点;而离群点则是距离正常数据点最近邻的点都比较远的数据点。通常有阈值进行界定距离的远近。在基于密度的离群点检测方法中,最具有代表性的方法是局部离群因子检测方法 (Local Outlier Factor, LOF)。 3LOF算法简介 在众多...

weixin_39910711的博客 1万+

同花顺CM函数深度解析:如何精准设置历史换手衰减系数以优化筹码峰

本文深度解析同花顺CM函数中的历史换手衰减系数,揭示其如何影响筹码峰准确性。通过拆解CM函数参数,并提供基于前十大流通股东锁定比例的计算公式,指导用户精准设置该系数,从而优化筹码峰模型,使其更真实反映个股持仓成本分布,提升技术分析的有效性。

mango的博客 1942

基于STM32与4G模组的远程OTA升级实战:从Bootloader设计到HTTP固件下载

本文详细介绍了基于STM32与4G模组的远程OTA升级实战,从Bootloader设计到HTTP固件下载的全流程。通过双备份Bootloader设计、AT指令优化、固件分包处理及安全校验等关键技术,实现高效稳定的远程升级方案,特别适用于物联网设备的批量维护与更新。

weixin_28716769的博客 157

离群点检测——局部离群因子(Local Outlier Factor,LOF)算法

1 离群点检测概述 离群点是观察的数据集中明显异常的数据点,或者说,离群点的数据分布与数据集的整体分布不同。离群点检测的目的是检测出那些与正常数据差别较大的数据点,然后根据具体的问题作进一步处理。 离群点检测算法主要有基于统计、聚类、分类、信息论、距离、密度等相关的方法,列表如下 检测方法 描述 优缺点 基于统计 根据数据的分布特点,选择一个概率分布模型对数据进行匹配,将不能匹...

归去来兮的博客 1万+

WINDOWS下SIP拨号的实现

在WIN10上拨打SIP电话  由于我的交换原理实验有一个是关于SIP的抓包的。而之前从未接触过SIP,所以在如何完成一次SIP拨号上遇到了不少问题,查了好多资料都没有完整的或者写的很清楚的,这里就对那些资料做一个总结,给出一个比较完善的解决方案,一来是记录此次实验的收获,而来也可能为其他有同样困惑的人解惑。  废话不多说直接开始。首先是下载一个可以拨打SIP电话的客户终端,这里我选择的是MicroSIP, 下载红圈内的文件,下载好之后直接一路安装即可,没有什么特别注意的地方。  安装好客户端之后,就需要注

weixin_43918519的博客 3828

[leetcode]Find K-th Smallest Pair Distance:查找数组元素中差值第K大的两个元素的差值

链接:https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/Given an integer array, return the k-th smallestdistanceamong all the pairs. The distance of a pair (A, B) is defined as...

xiaocong1990的博客 353

10分钟学会SkiaSharp:.NET跨平台绘图的秘密武器

SkiaSharp是一个基于Google的Skia图形库的跨平台2D图形API,专为.NET开发者打造。它提供了一个功能强大的API,可以广泛应用于各种设备和环境,包括移动、服务器和桌面系统,实现图像渲染的无缝对接。

java专栏 1473

MIKE 21 教程 2.2 Domain, Time, Module Selection设置教学

上一节讲解了水动力模型的基本概念与文件创建MIKE 21 教程 2.1水动力模型介绍​本节讲解水动力模型前三个模块的设置此模块需导入之前绘制好的网格文件,导入后可预览网格文件点击上方的Domain Specification可查看网格的数据统计与高程属性,快速检查网格文件有无明显错误 点击上方的Boundary names,可查看之前定义的出入水边界。在网格文件中,我们对边界只能以数字属性进行区分,这里可以对数字属性进行命名,在name中直接修改即可。本实例中我们的2代表入水边界,3代表出水边界。因为博主日

Hello Word 3879

719. Find K-th Smallest Pair Distance

每次取中间距离mid,计算每个元素距离小于等于mid值的元素值总和,如果比k大,取左半部分。比k小,则距离需增大取右半部分。注意在取左半部分的时候为使得最后求出的距离肯定是数组中真实存在的距离,需要end=mid,不能end=mid-1。先将数组排序,然后每相邻两个的距离进priority_queue,queue中保存三个信息{nums[end]和nums[start]距离,end,start},按从小到大排序。看,明明也是NlogN的时间复杂度,为啥这个不超时,二分法的logN了不起啊?...

qing2019的博客 209

学Simulink--基于多物理场耦合与系统集成的储能系统场景实例:压缩空气储能(CAES)系统的热力学建模与效率分析

本文提供了一个基于Simulink的压缩空气储能(CAES)系统热力学建模教程。通过多物理场耦合方法,详细演示了从系统架构设计到仿真测试的全过程,包括多级压缩机/膨胀机系统搭建、显热蓄热系统设计以及效率分析。文章重点强调了热管理对提升CAES效率的关键作用,并给出了68%电-电效率的优化案例。最后提出了变工况建模、不同蓄热介质对比等高级优化方向,为开发零碳大容量储能系统提供了实用工具和方法。

amy_mhd的博客 142

[Swift]LeetCode719. 找出第 k 小的距离对 | Find K-th Smallest Pair Distance

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30858241的博客 109

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Statistics and Machine Learning Toolbox

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

LeetCode //C - 719. Find K-th Smallest Pair Distance

This problem requires finding the kth smallest pair distance in an array. First, sort the array to simplify distance calculations. Then use binary search to find the target distance between the minimum distance 0 and the maximum distance (the last element

Made in Code 763

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

leetcode 719. 困难题 Find K-th Smallest Pair Distance 找出第 K 小的数对距离

最后看了官方题解,只需要计数<mid的个数就可以了,>mid的可以跳过大大节省了时间,二分搜索真好用,也不需要计算所有的差值了

邹九的个人博客 293

LeetCode - 719. Find K-th Smallest Pair Distance(暴力 | 二分)

LeetCode - 719. Find K-th Smallest Pair Distance(暴力 | 二分) 题目链接 题目 解析 import java.io.*; import java.util.*; class Solution { // find K-th Smallest Pair Distance public int smallestDistanceP...

zxzxin的博客~ 388

leetcode 719. Find K-th Smallest Pair Distance 第k小的绝对距离 + 暴力计算真棒

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1] ...

JackZhangNJU的专栏 1050

LWC 56:719. Find K-th Smallest Pair Distance

LWC 56:719. Find K-th Smallest Pair Distance传送门:719. Find K-th Smallest Pair DistanceProblem: Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A,

Demon-初来驾到 2098

LeetCode——719 找出第k小的距离对

问题描述: 给定一个整数数组,返回所有数对之间的第 k 个最小距离。一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值。 示例 1: 输入: nums = [1,3,1] k = 1 输出:0 解释: 所有数对如下: (1,3) -> 2 (1,1) -> 0 (3,1) -> 2 因此第 1 个最小距离的数对是 (1,1),它们之间的距离为 0。 提示:(...

CSDN的博客 288

[Leetcode] 719. Find K-th Smallest Pair Distance 解题报告

题目: Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: num

魔豆(Magicbean)的博客 1195

【LeetCode 719】 Find K-th Smallest Pair Distance

题目描述 Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1...

iCode_girl的博客 292

LEETCODE: 719 Find K-th Smallest Pair Distance

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.Example 1:Input: nums = [1,3,1] k = 1

博客已退休 912

LeetCode | 719. Find K-th Smallest Pair Distance

The distance of a pair of integers a and b is defined as the absolute difference between a and b.Given an integer array nums and an integer k, return the kth smallest distance among all the pairs nums[i] and nums[j] where 0

iLOVEJohnny的博客 1540

LeetCode 719.Find K-th Smallest Pair Distance

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1] ...

hidentity的博客 367

leetcode - 719. Find K-th Smallest Pair Distance

leetcode - 719

sinat_41679123的博客 755

leetcode 719. Find K-th Smallest Pair Distance c++

Given an integer array, return the k-th smallestdistanceamong all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1] ...

cmy203的博客 233
上一篇: leetcode 769. Max Chunks To Make Sorted(可排序的最大块数)
下一篇: leetcode 768. Max Chunks To Make Sorted II(可排序的最大块数II)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址