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

相关推荐
Pniubi7 小时前
力扣42接雨水(动态规划法)
算法·leetcode·职场和发展
Pniubi7 小时前
力扣46全排列(回溯(递归))
算法·leetcode·职场和发展
文丑颜不良啊8 小时前
LeetCode1461~1480题目解析
java·leetcode
Pniubi18 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
旖旎夜光1 天前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
文丑颜不良啊1 天前
LeetCode1401~1420题目解析
java·leetcode
Nuanyt1 天前
LeetCode Hot 100 链表专题2 3 题 (Java)
java·笔记·算法·leetcode·链表
文丑颜不良啊1 天前
LeetCode1341~1360题目解析
java·leetcode
海绵宝宝转agent1 天前
LeetCodehot100题解
java·笔记·学习·算法·leetcode·深度优先
Pniubi1 天前
力扣543二叉树的直径(深度优先搜索法(递归))
算法·leetcode·职场和发展