-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
38 lines (33 loc) · 1.13 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.company;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
// write your code here
}
public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
PriorityQueue<int[]> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> (Math.abs(a[0] - r0) + Math.abs(a[1] - c0))));
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
minHeap.offer(new int[]{r, c});
}
}
int[][] res = new int[R * C][2];
int index = 0;
while (!minHeap.isEmpty())
res[index++] = minHeap.poll();
return res;
}
public int[][] allCellsDistOrder_sort(int R, int C, int r0, int c0) {
int[][] res = new int[R * C][2];
int index = 0;
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
res[index++] = new int[]{r, c};
}
}
Arrays.sort(res, Comparator.comparingInt(a -> Math.abs(a[0] - r0) + Math.abs(a[1] - c0)));
return res;
}
}