Skip to content

Latest commit

 

History

History
21 lines (16 loc) · 337 Bytes

kth-smallest-element-in-a-sorted-matrix.md

File metadata and controls

21 lines (16 loc) · 337 Bytes

Code

func kthSmallest(matrix [][]int, k int) int {
	singleRow := []int{}
	for _, row := range matrix {
		singleRow = append(singleRow, row...)
	}

	sort.Ints(singleRow)
	return singleRow[k-1]
}

Solution in mind

  • Flatten the matrix into a single row.

  • Sort the single row and return the kth element.