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
相关推荐
且去填词25 分钟前
DeepSeek API 深度解析:从流式输出、Function Calling 到构建拥有“手脚”的 AI 应用
人工智能·python·语言模型·llm·agent·deepseek
rgeshfgreh1 小时前
Python条件与循环实战指南
python
rgeshfgreh1 小时前
通达信LC1文件结构解析指南
python
七夜zippoe1 小时前
事件驱动架构:构建高并发松耦合系统的Python实战
开发语言·python·架构·eda·事件驱动
Kratzdisteln1 小时前
【MVCD】PPT提纲汇总
经验分享·python
圣保罗的大教堂1 小时前
leetcode 1161. 最大层内元素和 中等
leetcode
闲看云起1 小时前
LeetCode-day6:接雨水
算法·leetcode·职场和发展
一个无名的炼丹师2 小时前
GraphRAG深度解析:从原理到实战,重塑RAG检索增强生成的未来
人工智能·python·rag
黛色正浓2 小时前
leetCode-热题100-贪心合集(JavaScript)
javascript·算法·leetcode
用户8356290780512 小时前
用Python轻松管理Word页脚:批量处理与多节文档技巧
后端·python