forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_373.java
60 lines (47 loc) · 1.74 KB
/
_373.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
public class _373 {
public static class Solution1 {
int[][] dirs = {{0, 1}, {1, 0}, {1, 1}};
public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
List<List<Integer>> result = new ArrayList<>();
// EDGE CASE
if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) {
return result;
}
// visited array
boolean[][] visited = new boolean[nums1.length][nums2.length];
// Min Heap
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
return (a[0] + a[1]) - (b[0] + b[1]);
});
int[] temp = new int[]{nums1[0], nums2[0], 0, 0};
pq.add(temp);
visited[0][0] = true;
while (!pq.isEmpty()) {
int[] arr = pq.poll();
List<Integer> ls = new ArrayList<>();
ls.add(arr[0]);
ls.add(arr[1]);
result.add(ls);
if (result.size() == k) {
break;
}
int i = arr[2];
int j = arr[3];
for (int[] dir : dirs) {
int dx = i + dir[0];
int dy = j + dir[1];
if (dx >= 0 && dx < nums1.length && dy >= 0 && dy < nums2.length && !visited[dx][dy]) {
pq.add(new int[]{nums1[dx], nums2[dy], dx, dy});
visited[dx][dy] = true;
}
}
}
return result;
}
}
}