-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_169198.java
More file actions
29 lines (27 loc) · 1.07 KB
/
_169198.java
File metadata and controls
29 lines (27 loc) · 1.07 KB
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
import java.util.*;
class Solution {
public int[] solution(int m, int n, int startX, int startY, int[][] balls) {
int[] answer = new int[balls.length];
for(int i=0; i<balls.length; i++){
answer[i] = getMinDist(m, n, startX, startY, balls[i][0], balls[i][1]);
}
return answer;
}
public int getMinDist(int m, int n, int r1, int c1, int r2, int c2){
Queue<Integer> dist = new ArrayDeque<>();
if(c1!=c2 || r1<r2) dist.add(getDist(r1, c1, -r2, c2));
if(r1!=r2 || c1<c2) dist.add(getDist(r1, c1, r2, -c2));
if(c1!=c2 || r1>r2) dist.add(getDist(r1, c1, m+m-r2, c2));
if(r1!=r2 || c1>c2) dist.add(getDist(r1, c1, r2, n+n-c2));
// System.out.println(dist.peek());
int minDist = dist.poll();
while(!dist.isEmpty()){
// System.out.println(dist.peek());
minDist = Math.min(minDist, dist.poll());
}
return minDist;
}
public int getDist(int r1, int c1, int r2, int c2){
return (r1-r2)*(r1-r2) + (c1-c2)*(c1-c2);
}
}