Using Min Heap Java
Storing pairs of distance and an array containing row and col index in priority queue. Now min heap will automatically sort the pairs according to distance\n\ni
Storing pairs of distance and an array containing row and col index in priority queue. Now min heap will automatically sort the pairs according to distance\n```\nimport java.util.PriorityQueue;\n\nclass Solution {\n    public int[][] allCellsDistOrder(int rows, int cols, int rCentre, int cCentre) {\n\n        PriorityQueue<Pair<Integer, int[]>> pq = new PriorityQueue<>(rows*cols,(a,b) -> a.getKey() - b.getKey());\n        int[][] ans = new int[rows*cols][];\n\n        Integer dist;\n\n        for(int i=0;i<rows;i++){\n            for(int j=0;j<cols;j++){\n                dist = Math.abs(rCentre-i) + Math.abs(cCentre-j);\n                pq.add(new Pair<Integer, int[]>(dist, new int[]{i,j}));\n            }\n        }\n\n        int i=0;\n        while(!pq.isEmpty()){\n            ans[i] = pq.poll().getValue();\n            i++;\n        }\n\n        return ans;\n    }\n}\n```
['Heap (Priority Queue)', 'Java']