Python | Leetcode Python题解之第378题有序矩阵中第K小的元素

题目:

题解:

python 复制代码
class Solution:
    def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
        n = len(matrix)

        def check(mid):
            i, j = n - 1, 0
            num = 0
            while i >= 0 and j < n:
                if matrix[i][j] <= mid:
                    num += i + 1
                    j += 1
                else:
                    i -= 1
            return num >= k

        left, right = matrix[0][0], matrix[-1][-1]
        while left < right:
            mid = (left + right) // 2
            if check(mid):
                right = mid
            else:
                left = mid + 1
        
        return left
相关推荐
web3.08889991 小时前
微店商品详情API实用
python·json·时序数据库
踩坑记录1 小时前
leetcode hot100 189.轮转数组 medium
leetcode
知乎的哥廷根数学学派1 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
sunfove1 小时前
将 Python 仿真工具部署并嵌入个人博客
开发语言·数据库·python
Learner1 小时前
Python类
开发语言·python
2501_941329722 小时前
门及其组件定位识别_YOLO13-C3k2-PoolingFormer改进模型研究
python
Ancelin安心2 小时前
kali-dirsearch的使用
linux·运维·服务器·python·计算机网络·web安全·网络安全
努力学习的小洋2 小时前
Python训练打卡Day5离散特征的处理-独热编码
人工智能·python·机器学习
Dream it possible!2 小时前
LeetCode 面试经典 150_二分查找_在排序数组中查找元素的第一个和最后一个位置(115_34_C++_中等)
c++·leetcode·面试
Sherry Wangs2 小时前
【ML】机器学习进阶
人工智能·python·机器学习