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
相关推荐
小王子10242 小时前
设计模式Python版 组合模式
python·设计模式·组合模式
Mason Lin4 小时前
2025年1月22日(网络编程 udp)
网络·python·udp
清弦墨客4 小时前
【蓝桥杯】43697.机器人塔
python·蓝桥杯·程序算法
Joyner20185 小时前
python-leetcode-从中序与后序遍历序列构造二叉树
算法·leetcode·职场和发展
因兹菜5 小时前
[LeetCode]day9 203.移除链表元素
算法·leetcode·链表
LNsupermali5 小时前
力扣257. 二叉树的所有路径(遍历思想解决)
算法·leetcode·职场和发展
雾月555 小时前
LeetCode LCR180文件组合
算法·leetcode·职场和发展
萌の鱼5 小时前
leetcode 2080. 区间内查询数字的频率
数据结构·c++·算法·leetcode
Tisfy5 小时前
LeetCode 0541.反转字符串 II:模拟
算法·leetcode·字符串·题解
RZer6 小时前
Hypium+python鸿蒙原生自动化安装配置
python·自动化·harmonyos