-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path475.java
62 lines (51 loc) · 1.56 KB
/
475.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
61
62
import java.util.*;
class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters);
int right = 0;
if(houses[houses.length - 1] >= heaters[heaters.length - 1]){
right = houses[houses.length - 1];
}else{
right = heaters[heaters.length - 1];
}
int left = 0;
int fm = 0;
while(left <= right){
int meio = (left + right) / 2;
int gasolina = meio;
int cont = 0;
boolean n = false;
for(int i = 0; i < houses.length; i++){
if(cont == heaters.length){
n = true;
break;
}
if(houses[i] > heaters[cont]){
if(houses[i] <= heaters[cont] + gasolina){
continue;
}else{
cont++;
i--;
}
}else if(houses[i] == heaters[cont]){
continue;
}else{
if(houses[i] + gasolina >= heaters[cont]){
continue;
}else{
cont++;
i--;
}
}
}
if(n){
left = meio + 1;
}else{
right = meio - 1;
fm = meio;
}
}
return fm;
}
}