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

相关推荐
午彦琳8 小时前
2026.9.17
数据结构·算法·leetcode
木井巳8 小时前
【记忆化搜索】不同路径
java·算法·leetcode·深度优先·剪枝·推荐算法
All for pursuit.11 小时前
【链表-9】146.LRU缓存
数据结构·c++·算法·leetcode
圣保罗的大教堂12 小时前
leetcode 1477. 找两个和为目标值且不重叠的子数组 中等
leetcode
hanlin0312 小时前
刷题笔记:力扣第144题-二叉树的前序遍历
笔记·算法·leetcode
圣保罗的大教堂14 小时前
leetcode 3629. 通过质数传送到达终点的最少跳跃次数 中等
leetcode
圣保罗的大教堂14 小时前
leetcode 1914. 循环轮转矩阵 中等
leetcode
wabs66616 小时前
关于二叉树【力扣100.相同的树的思考】
数据结构·c++·算法·leetcode·二叉树·递归法
alphaTao16 小时前
LeetCode 每日一题 2026/9/14-2026/9/20
算法·leetcode
hanlin0316 小时前
刷题笔记:力扣第560题-和为k的子数组
笔记·算法·leetcode