Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.


Note that it is the kth smallest element in the sorted order, not the kth distinct element.


Example:

matrix = [
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.



Note: 
You may assume k is always valid, 1 ≤ k ≤ n2.

Difficulty: MEDIUM_HARD
Starter code: class Solution:
    def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
        

Solutions:
class Solution:

	def kthSmallest(self, matrix, k):
		arr = []
		for i in matrix:
			for j in i:
				arr.append(j)
		arr.sort()
		print(arr)
		return arr[k - 1]


class Solution:

	def kthSmallest(self, matrix, k):
		from heapq import heappush, heappop
		n = len(matrix[0])
		heap = []
		for i in range(n):
			heappush(heap, (matrix[0][i], 0, i))
		for _ in range(k - 1):
			(v, x, y) = heappop(heap)
			if x == n - 1:
				continue
			heappush(heap, (matrix[x + 1][y], x + 1, y))
		return heappop(heap)[0]


class Solution:

	def kthSmallest(self, matrix, k):
		list = matrix[0]
		for i in range(1, len(matrix)):
			list.extend(matrix[i])
		list.sort()
		return list[k - 1]