Find the k-th Smallest Element in a Sorted Matrix

Advanced

Welcome to the magical world of matrices where rows and columns organize numbers like soldiers in ranks! Your challenge, should you choose to accept it, is to find the k-th smallest number in a sorted matrix.

Imagine a 2D matrix where each row and each column is sorted in ascending order. We want you to find the k-th smallest element in this matrix. For instance, here's how such a matrix might look:

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

If we want the 8th smallest number, the answer would be 13!

Here's the plan:

  • You will receive a sorted matrix and a number k.
  • Your task is to determine the k-th smallest element.

Examples:

matrix = [ [1, 2], [1, 3] ] k = 2 # find_kth_smallest(matrix, k) should return 1 matrix = [ [1, 5, 9], [10, 11, 13], [12, 13, 15] ] k = 8 # find_kth_smallest(matrix, k) should return 13

Get ready, grab your coding wand, and let's uncover the secrets hidden within these numbers!

Geek
Inputs

Arg 1:

[[1,2],[1,3]]

Arg 2:

2
Expected Output
1
Actual Output