Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Kth Smallest in a Sorted Matrix

LeetCode #378Medium
Binary search on values · count ≤ x probes

Given an n x n matrix whose rows and columns are each sorted in ascending order, find the kth smallest element in the matrix considering its sorted order, counting duplicate values separately.

Asked atAmazonGoogle
step 1 / 10
row 0 →
row 1 →
row 2 →
1
5
9
10
11
13
12
13
15
[0][1][2][3][4][5][6][7][8]
Brute force · flatten and sort
1given matrix, k
2flatten into a list; sort it
3return list[k − 1]
state
  • k8

line 1A 3×3 matrix where every ROW and every COLUMN is sorted (shown flattened, one bracket per row). Find the k = 8th smallest element. Brute: ignore all that structure — dump and sort.