Kth Smallest Element in a Sorted Matrix LeetCode solution in Python
1class Solution:2def kthSmallest(self, matrix, k):3n, beg, end = len(matrix), matrix[0][0], matrix[-1][-1]45def check(m):6i, j, cnt = 0, n - 1, 07for i in range(n):8while j >= 0 and matrix[i][j] > m:9j -= 110cnt += j + 111return cnt1213while beg < end:14mid = (beg + end) // 215if check(mid) < k:16beg = mid + 117else:18end = mid1920return beg