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
相关推荐
用户120391129472611 小时前
打破信息壁垒:手把手教你实现DeepSeek大模型的天气查询功能
python·openai
鱼骨不是鱼翅12 小时前
力扣hot100----1day
python·算法·leetcode·职场和发展
2501_9412362112 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
程序猿_极客12 小时前
【2025 最新】 Python 安装教程 以及 Pycharm 安装教程(超详细图文指南,附常见问题解决)
开发语言·python·pycharm·python安装以及配置
小欣加油12 小时前
leetcode 429 N叉树的层序遍历
数据结构·c++·算法·leetcode·职场和发展
b***666112 小时前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
chushiyunen12 小时前
django使用笔记
笔记·python·django
Kuo-Teng12 小时前
LeetCode 142: Linked List Cycle II
java·算法·leetcode·链表·职场和发展
2501_9411113412 小时前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python
ada7_12 小时前
LeetCode(python)——73.矩阵置零
python·算法·leetcode·矩阵