Simplest using HashMap | Accepted


  • 0
    U
    class Solution {
        public int kthSmallest(int[][] matrix, int k) {
            List myList=new ArrayList();
            for(int i=0;i<matrix.length;i++){
                for(int j=0;j<matrix[0].length;j++){
                    myList.add(matrix[i][j]);
                }
            }
            Collections.sort(myList);
            System.out.println(myList);
            int value =Integer.parseInt(myList.get(k-1).toString());
            return value;
        }
    }
    

  • 0
    X

    I guess using O(mn) extra space and O(mn log (mn)) sorting time complexity is remotely the optimal solution.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.