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
相关推荐
a11177621 小时前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
0思必得021 小时前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
一匹电信狗21 小时前
【LeetCode_547_990】并查集的应用——省份数量 + 等式方程的可满足性
c++·算法·leetcode·职场和发展·stl
鱼跃鹰飞1 天前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
摘星编程1 天前
OpenHarmony + RN:Calendar日期选择功能
python
Yvonne爱编码1 天前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python
一方_self1 天前
了解和使用python的click命令行cli工具
开发语言·python
小芳矶1 天前
Dify本地docker部署踩坑记录
python·docker·容器
We་ct1 天前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
2301_822366351 天前
使用Scikit-learn构建你的第一个机器学习模型
jvm·数据库·python