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, k2flatten into a list; sort it3return list[k − 1]
state
- k8
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 problemGiven 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.
▸1given matrix, k2flatten into a list; sort it3return list[k − 1]
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.