public int kthSmallest(int[][] matrix, int k) {
ArrayList <Integer> result = new ArrayList<Integer>();
for(int z=0;z<matrix.length;z++){
for(int l=0;l<matrix[0].length;l++){
result.add(matrix[z][l]);
}
}
Integer[] bar = result.toArray(new Integer[result.size()]);
Arrays.sort(bar);
return bar[k1];
}
}
48ms Easy to understand Java solution



@gayathri3 I like this solution, but the interviewer doesn't like... hehe I cannot see any use case for this situation.... why don't they prepare the data in a monotonic sorted manner. :)


@chenw2000 bar got n^2 elements so time complexity is n^2log(n^2) which is n^2log(n), worse than n^2.