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
相关推荐
门框研究员31 分钟前
解锁Python的强大能力:深入理解描述符
python
leoufung1 小时前
LeetCode 92 反转链表 II 全流程详解
算法·leetcode·链表
子不语1802 小时前
Python——函数
开发语言·python
daidaidaiyu2 小时前
一文入门 LangChain 开发
python·ai
im_AMBER2 小时前
Leetcode 59 二分搜索
数据结构·笔记·学习·算法·leetcode
JJ1M83 小时前
用 Python 快速搭建一个支持 HTTPS、CORS 和断点续传的文件服务器
服务器·python·https
leoufung3 小时前
LeetCode 61. 旋转链表(Rotate List)题解与思路详解
leetcode·链表·list
汤姆yu3 小时前
基于python大数据的小说数据可视化及预测系统
大数据·python·信息可视化
x***J3483 小时前
Python多线程爬虫
开发语言·爬虫·python
m***D2864 小时前
Python网络爬虫实战案例
开发语言·爬虫·python