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

相关推荐
阿豪学编程2 小时前
LeetCode724.:寻找数组的中心下标
算法·leetcode
禹中一只鱼3 小时前
【力扣热题100学习笔记】 - 哈希
java·学习·leetcode·哈希算法
凌波粒3 小时前
LeetCode--349.两个数组的交集(哈希表)
java·算法·leetcode·散列表
_日拱一卒8 小时前
LeetCode:滑动窗口的最大值
数据结构·算法·leetcode
圣保罗的大教堂9 小时前
leetcode 3548. 等和矩阵分割 II 困难
leetcode
mifengxing9 小时前
力扣HOT100——(1)两数之和
java·数据结构·算法·leetcode·hot100
Z.风止9 小时前
Large Model-learning(2)
开发语言·笔记·python·leetcode
AlenTech10 小时前
139. 单词拆分 - 力扣(LeetCode)
算法·leetcode·职场和发展
穿条秋裤到处跑12 小时前
每日一道leetcode(2026.03.30):判断通过操作能否让字符串相等 II
算法·leetcode
Q741_14712 小时前
每日一题 力扣 2840. 判断通过操作能否让字符串相等 II 力扣 2839. 判断通过操作能否让字符串相等 I 找规律 字符串 C++ 题解
c++·算法·leetcode·力扣·数组·找规律