class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int ans = 0;
for (int house : houses) {
int l = 0;
int r = heaters.length-1;
int tmp = (int) 1e9+1;
while (l<=r){
int mid = (l + r)/2;
//怕遗漏边缘情况不好处理就每次二分的结果都比较下
tmp = Math.min(tmp, Math.abs(heaters[mid] - house));
if (heaters[mid] > house){
r = mid-1;
}else if (heaters[mid] < house){
l = mid+1;
}else {
break;
}
}
ans = Math.max(ans,tmp);
}
return ans;
}
}
class Solution {
public int largestSumAfterKNegations(int[] nums, int k) {
int sum = 0;
PriorityQueue<Integer> queue = new PriorityQueue<>();
int absMin = 101;
for (int num : nums) {
absMin = Math.min(absMin,Math.abs(num));
if (num >= 0){
sum += num;
}else{
queue.offer(num);
}
}
if (queue.size()>0){
//把绝对值较大的负数尽量翻转
while (!queue.isEmpty() && k > 0){
sum -= queue.poll();
k--;
}
if (k==0){
//翻转次数用完了,还有数字没翻转
while (!queue.isEmpty()){
sum += queue.poll();
}
return sum;
}
}
if (k%2 == 0){
return sum;
}else{
return sum - 2*absMin;
}
}
}