215. Kth Largest Element in an Array

215. Kth Largest Element in an Array

python 复制代码
class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        
        m,n=len(nums1),len(nums2)

        l,r=0,m
        N=m+n

        while l<=r:
            A=(l+r)//2
            B=((N+1)//2)-A

            x1=-float("inf") if A-1<0 else nums1[A-1]
            y1=float("inf") if A==m else nums1[A]
            x2=-float("inf") if B-1<0 else nums2[B-1]
            y2=float("inf") if B==n else nums2[B]

            if x1<=y2 and x2<=y1:
                if N%2==0:
                    return (max(x1,x2)+min(y1,y2))/2
                else:
                    return max(x1,x2)
            elif x1>y2:
                r=A-1
            else:
                l=A+1                

heapq

相关推荐
smj2302_796826521 天前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
leoufung1 天前
LeetCode 92 反转链表 II 全流程详解
算法·leetcode·链表
im_AMBER1 天前
Leetcode 59 二分搜索
数据结构·笔记·学习·算法·leetcode
leoufung1 天前
LeetCode 61. 旋转链表(Rotate List)题解与思路详解
leetcode·链表·list
leoufung2 天前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
Aspect of twilight2 天前
LeetCode华为大模型岗刷题
python·leetcode·华为·力扣·算法题
2301_807997382 天前
代码随想录-day47
数据结构·c++·算法·leetcode
Elias不吃糖2 天前
LeetCode每日一练(3)
c++·算法·leetcode
小年糕是糕手2 天前
【C++】类和对象(二) -- 构造函数、析构函数
java·c语言·开发语言·数据结构·c++·算法·leetcode
sheeta19982 天前
LeetCode 每日一题笔记 日期:2025.11.24 题目:1018. 可被5整除的二进制前缀
笔记·算法·leetcode