forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1620.java
33 lines (32 loc) · 1.35 KB
/
_1620.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
package com.fishercoder.solutions;
public class _1620 {
public static class Solution1 {
public static int[] bestCoordinate(int[][] towers, int radius) {
int maxSignal = 0;
int[] best = new int[2];
for (int i = 0; i < towers.length; i++) {
int thisQuality = 0;
for (int j = 0; j < towers.length; j++) {
double distance = Math.sqrt((towers[i][0] - towers[j][0]) * (towers[i][0] - towers[j][0]) + (towers[i][1] - towers[j][1]) * (towers[i][1] - towers[j][1]));
if (distance <= radius) {
thisQuality += Math.floor(towers[j][2] / (1 + distance));
}
}
if (thisQuality > maxSignal) {
maxSignal = thisQuality;
best[0] = towers[i][0];
best[1] = towers[i][1];
} else if (thisQuality == maxSignal) {
if (towers[i][0] < best[0]) {
best[0] = towers[i][0];
best[1] = towers[i][1];
} else if (towers[i][0] == best[0] && towers[i][1] < best[1]) {
best[0] = towers[i][0];
best[1] = towers[i][1];
}
}
}
return best;
}
}
}