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

相关推荐
Pluchon5 小时前
硅基计划4.0 算法 二叉树深搜(DFS)
java·数据结构·算法·leetcode·深度优先·剪枝
sprintzer6 小时前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展
一匹电信狗8 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
学学学无无止境8 小时前
力扣-上升的温度
leetcode
Emilia486.10 小时前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
Dream it possible!11 小时前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
程序员阿鹏12 小时前
49.字母异位词分组
java·开发语言·leetcode
Espresso Macchiato12 小时前
Leetcode 3715. Sum of Perfect Square Ancestors
算法·leetcode·职场和发展·leetcode hard·树的遍历·leetcode 3715·leetcode周赛471
Miraitowa_cheems19 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号19 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode