algorythms
Modified Binary Search
LC #378Medium

Kth Smallest Element in a Sorted Matrix

Modified Binary Search
GoogleAmazonAppleBloomberg

Problem

Find the kth smallest element in an n×n matrix where each row and column is sorted.

binary-searchmatrixheap

Constraints

  • n == matrix.length == matrix[i].length
  • 1 ≤ n ≤ 300
  • -2³¹ ≤ matrix[i][j] ≤ 2³¹-1
  • 1 ≤ k ≤ n²

Example

Inputmatrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
Output13
Why

Sorted: [1,5,9,10,11,12,13,13,15]. 8th element = 13.

Hints — reveal one at a time