Kth Smallest Element in a Sorted Matrix

Problem Given an n x n matrix with each row and each column sorted ascending, return the kth smallest element.

Input / Output

  • Input: the matrix and int k (1 <= k <= n^2).
  • Output: the kth smallest value.

Constraints

  • n up to 300; better than O(n^2 log n) (flatten + sort) is expected.

Example

  • matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8 -> 13.
asked …
LeaderboardSalaryAccount