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

相关推荐
爱上语文3 小时前
Java LeetCode每日一题
java·开发语言·leetcode
大二转专业8 小时前
408算法题leetcode--第24天
考研·算法·leetcode
__AtYou__15 小时前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解
转调15 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
huanxiangcoco16 小时前
152. 乘积最大子数组
python·leetcode
希望有朝一日能如愿以偿18 小时前
力扣题解(飞机座位分配概率)
算法·leetcode·职场和发展
Espresso Macchiato18 小时前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
数据分析螺丝钉19 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
￴ㅤ￴￴ㅤ9527超级帅19 小时前
LeetCode hot100---数组及矩阵专题(C++语言)
c++·leetcode·矩阵
鱼跃鹰飞20 小时前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试