Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

475. Heaters #161

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

思路:第二while循环找出了距离第i个房子最近的heater,第二个循环找的是最大的半径,这样才能选的heater数量最少 public class Solution { public int findRadius(int[] houses, int[] heaters) { Arrays.sort(houses); Arrays.sort(heaters); int i = 0, j = 0, res = 0;//i is pointer of houses, j is pointer pf heaters while(i < houses.length) { while(j < heaters.length - 1 && Math.abs(heaters[j+1]-houses[i]) <= Math.abs(heaters[j]-houses[i])) { j++; } res = Math.max(res, Math.abs(heaters[j]-houses[i])); i++; } return res; } }

image

Shawngbk commented 7 years ago

Google