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

相关推荐
Pniubi18 小时前
力扣2 . 两数相加
算法·leetcode·职场和发展
文丑颜不良啊1 天前
LeetCode3401~3420题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode3421~3440题目解析
java·leetcode
LK_072 天前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
Pniubi2 天前
力扣21合并两个有序链表
算法·leetcode·职场和发展
文丑颜不良啊2 天前
LeetCode3341~3360题目解析
java·leetcode
Rain的Java大神之路2 天前
把数组本身变成哈希表:原地哈希搞定 LeetCode 41「缺失的第一个正数」
java·后端·leetcode
Pniubi2 天前
力扣239.滑动窗口最大值(优先队列)
算法·leetcode·职场和发展
文丑颜不良啊3 天前
LeetCode3281~3300题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode3261~3280题目解析
java·leetcode