Dashboard
LC #378

Kth Smallest Element in a Sorted Matrix

Pattern · ?GoogleAmazonAppleBloomberg

Problem

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

Constraints

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

Example

Input
matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
Output
13
Why
Sorted: [1,5,9,10,11,12,13,13,15]. 8th element = 13.

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗