Given an n x n matrix where each of the rows and columns are sorted in ascending order, return the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int n = matrix.length;
List<Integer> tmp = new ArrayList();
for(int i = 0; i < n; i++){
for(int j = 0;j < n; j++){
tmp.add(matrix[i][j]);
}
}
Collections.sort(tmp);
return tmp.get(k-1);
}
}