首页 > 其他分享 >LeetCode 658. Find K Closest Elements 二分+双指针

LeetCode 658. Find K Closest Elements 二分+双指针

时间:2023-07-15 22:55:52浏览次数:34  
标签:二分 arr Elements Closest int right integer Find left

Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.

An integer a is closer to x than an integer b if:

  • |a - x| < |b - x|, or
  • |a - x| == |b - x| and a < b

Solution

先用二分确定 x 的位置,然后用双指针来确定左右范围

点击查看代码
class Solution:
    def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
        def left_bound(arr, x):
            left = 0
            right = len(arr)

            while left<right:
                mid = left+int((right-left)/2)
                if arr[mid]==x:
                    right=mid
                elif arr[mid]<x:
                    left=mid+1
                elif arr[mid]>x:
                    right=mid
            return left
        
        right = left_bound(arr,x)
        left = right-1
        print(right, left)

        for _ in range(k):
            if left<0: 
                right+=1
            elif right>=len(arr):
                left-=1
            else:
                if x-arr[left]<=arr[right]-x:
                    left-=1
                else:
                    right+=1
        return arr[left+1:right]
            

标签:二分,arr,Elements,Closest,int,right,integer,Find,left
From: https://www.cnblogs.com/xinyu04/p/17557163.html

相关文章

  • maven打包repackage failed: Unable to find main class
    maven打包提示这个问题。原因:主项目pomxml文件中,不需要<build>打包的配置,只需要在有入口类的模块pom.xml配置好<build><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.......
  • vim E447: cannot find file iostream in path
    查看c/c++文件中的头文件,可以使用gf跳转,但是有时会出现Error447:notfoundinpath1,命名模式中输入,临时修改:setpath=.,/usr/include,,/usr/include/c++/*/2,修改vimrc增加setpath+=.,/usr/include,,/usr/include/c++/*/......
  • Find命令的7种用法
    可以很肯定地说,find命令是Linux后台开发人员必须熟知的操作之一,除非您使用的是WindowsServer。对于技术面试,它也是一个热门话题。让我们看一道真题:如果你的Linux服务器上有一个名为logs的目录,如何删除该目录下最后一次访问时间超过一年的日志文件呢?这种情况很常见,但令......
  • Union-Find
    Union-Find算法详解今天讲讲Union-Find算法,也就是常说的并查集算法,主要是解决图论中「动态连通性」问题的。名词很高端,其实特别好理解,等会解释,另外这个算法的应用都非常有趣。说起这个Union-Find,应该算是我的「启蒙算法」了,因为《算法4》的开头就介绍了这款算法,可是把我秀翻......
  • destoon列表性能优化,关于IN()与FIND_IN_SET
    destoon数据达到一定之后列表打开速度就很慢,于是为了解决这个问题,进行以下办法处理。找到/include/tag.func.php文件,找到这段代码: 原因:区别:1、in后面只能跟常量,find_in_set()函数可以使用常量或字段。2、in是完全匹配,find_in_set()函数是精确匹配,字段值以英文”,”分隔。......
  • JMeter脚本报错:Cannot find engine named: 'javascript'的解决方法
    本文将介绍如何解决在JMeter版本5.4.1下执行脚本时出现的错误信息“javax.script.ScriptException:Cannotfindenginenamed:'javascript'”。通过将本地JDK版本从18.0.1.1更改为1.8.0_151来解决此问题。当使用JMeter进行脚本执行时,有时可能会遇到以下错误信息:javax.script......
  • ansible find模块简单使用
    ansiblefind模块简单使用参数参数说明paths要查找的目录列表别名:name、path类型:listrecurse是否递归遍历子目录选项:true或false,默认falsehidden是否包含隐藏文件选项:true或false,默认falsefile_type要查找的文件类型默认只查找的是文件选项:any(所......
  • rust-bindgen报错 ‘Unable to find libclang的解决办法
    Windows下面可能会遇到这个问题的解决方案:1)把LLVM安装到没有空格的路径。2)LIBCLANG_PATH的值不要加双引号。thread'main'panickedat'Unabletofindlibclang:"couldn'tfindanyvalidsharedlibrariesmatching:['clang.dll','libclang.dll'],setth......
  • Closest Cow Wins S 最近的奶牛获胜
    ClosestCowWinsS最近的奶牛获胜题目传送门目录ClosestCowWinsS最近的奶牛获胜题目描述输入格式输出格式样例#1样例输入#1样例输出#1提示思路code题目描述FarmerJohn沿着一条高速公路拥有一个很长的农场,可以被看作类似于一维数轴。沿着农场有\(K\)块草地(\(1\le......
  • 使用input标签的时候报错,提示Form elements must have labels: Element has no title
    使用input标签的时候报错,提示Formelementsmusthavelabels:ElementhasnotitleattributeElementhasnoplaceholderattribute大概就是下面这样其实规范化一下,加个label就可以了......