日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

475. Heaters

發(fā)布時(shí)間:2025/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 475. Heaters 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:

Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.

Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.

So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.

Note:

  • Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
  • Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
  • As long as a house is in the heaters' warm radius range, it can be warmed.
  • All the heaters follow your radius standard and the warm radius will the same.
  • ?

    Example 1:

    Input: [1,2,3],[2] Output: 1 Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.

    ?

    Example 2:

    Input: [1,2,3,4],[1,4] Output: 1 Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.

    鏈接:https://leetcode.com/problems/heaters/#/description

    3/28/2017

    18ms, beat 96%

    每個(gè)房屋只遍歷一遍,heater卻可能需要在update house之后重新來(lái)算。改正的次數(shù)太多。最主要的改變是:

    1. radis的初始值,如果第一個(gè)house在第一個(gè)heater左邊則至少大于這個(gè)距離;但是如果在heater右邊,就跟其他房間/heater計(jì)算方法一樣

    2. 29行還是需要更新radis的,否則一旦heater前進(jìn)之后,我們不會(huì)根據(jù)左邊的house來(lái)更新radis的。如果左邊在下一個(gè)heater范圍內(nèi),一定要在更新heater index之前計(jì)算

    3. 26, 29行不要逼著眼更新,一定要選跟當(dāng)前radis中較大的那個(gè)

    4. 注意for循環(huán)中沒(méi)有步進(jìn),或者可以改成while loop

    1 public class Solution { 2 public int findRadius(int[] houses, int[] heaters) { 3 Arrays.sort(houses); 4 Arrays.sort(heaters); 5 int index = 0; 6 int radis = 0; 7 if (houses[0] < heaters[0]) radis = heaters[0] - houses[0]; 8 for (int i = 0; i < heaters.length; ) { 9 // all unvisited houses left to heater[i] should be less than current radis 10 // some unvisited houses right to heater[i] may also less than current radis 11 // break when 1. invalid index, 2. houses right to heater[i] larger than current radis 12 while (index < houses.length && (houses[index] <= heaters[i] || houses[index] - heaters[i] <= radis)) index++; 13 if (index == houses.length) break; 14 // have unvisited heaters 15 // here we meet first house out of range on the right 16 if (i < heaters.length - 1) { 17 // house is right to next heater, we could update radis when processing next heater 18 // because this house will still break in while loop 19 if (heaters[i + 1] <= houses[index]) { 20 i++; 21 } else { 22 // next heater is right to current house, the house has to be in the range of current heater, 23 // need to update radis 24 // do not update heater index, we may meet more houses have to be covered by this heater 25 if (heaters[i + 1] - houses[index] > houses[index] - heaters[i]) { 26 radis = Math.max(houses[index] - heaters[i], radis); 27 } else { 28 // next heater will cover current house 29 radis = Math.max(heaters[i + 1] - houses[index], radis); 30 i++; 31 } 32 // radis determined by current house is done 33 index++; 34 } 35 } else { 36 // no unvisited heaters, this last house out of range of current radis, update radis 37 radis = houses[houses.length - 1] - heaters[i]; 38 break; 39 } 40 } 41 return radis; 42 } 43 }

    還需要看別人的算法

    別人的算法,巧妙的運(yùn)用了Arrays.binarySearch()方法的返回值,其中第8行運(yùn)用了負(fù)數(shù)的反碼:(-(insertion point) - 1)。看來(lái)Java也是有意用反碼,返回的是在數(shù)組中可以插入的第幾個(gè)值,返回的是下標(biāo)的相反數(shù)?

    還有一點(diǎn),如果本身返回的是正數(shù),那么沒(méi)有必要來(lái)計(jì)算,因?yàn)閞esult = 0已經(jīng)足夠了。

    第9,10行就是邊界問(wèn)題的考慮。時(shí)間復(fù)雜度O((m+n)lgn)

    1 public int findRadius(int[] houses, int[] heaters) { 2 Arrays.sort(heaters); 3 int result = 0; 4 5 for (int house : houses) { 6 int index = Arrays.binarySearch(heaters, house); 7 if (index < 0) { 8 index = ~index; 9 int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE; 10 int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE; 11 12 result = Math.max(result, Math.min(dist1, dist2)); 13 } 14 } 15 16 return result; 17 }

    另外一種算法,還是不理解:https://discuss.leetcode.com/topic/71450/simple-java-solution-with-2-pointers

    我猜測(cè)的原因是,目的是每個(gè)house都被取暖,所以按照house來(lái)遍歷肯定可以保證都被cover了,而且它只跟左右的來(lái)比較

    1 public class Solution { 2 public int findRadius(int[] houses, int[] heaters) { 3 Arrays.sort(houses); 4 Arrays.sort(heaters); 5 6 int i = 0, res = 0; 7 for (int house : houses) { 8 while (i < heaters.length - 1 && heaters[i] + heaters[i + 1] <= house * 2) { 9 i++; 10 } 11 res = Math.max(res, Math.abs(heaters[i] - house)); 12 } 13 14 return res; 15 } 16 }

    更多討論:

    https://discuss.leetcode.com/category/606/heaters

    轉(zhuǎn)載于:https://www.cnblogs.com/panini/p/6637810.html

    總結(jié)

    以上是生活随笔為你收集整理的475. Heaters的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: julia一区二区三区在线观看 | 成av人在线观看 | 久久精品天堂 | 天天做天天爱夜夜爽 | 成年人激情网 | 波多野结衣久久 | 一本久久精品一区二区 | 中文字幕在线观看视频一区二区 | 中文字幕在线一 | 午夜在线视频免费观看 | 艳妇乳肉豪妇荡乳 | 久久久久久久国产精品毛片 | 告诉我真相俄剧在线观看 | 精品爱爱 | 色综合久久综合 | 亚洲成a人片77777kkkk | 偷操 | 麻豆三级视频 | 婷婷五月花 | 欧美绿帽合集xxxxx | 69av在线| 国产欧美在线看 | 青娱乐超碰 | 色午夜婷婷 | 国产福利不卡视频 | 成年人免费看的视频 | 草逼视频免费看 | 九九久久国产视频 | av大片在线免费观看 | 国精品无码人妻一区二区三区 | 青青草在线免费 | 亚洲1区 | 97在线影院 | 丰满少妇高潮在线观看 | 少妇一区二区三区四区 | 想要视频在线 | 日本在线观看a | 欧美一区二区三区四区视频 | 亚洲精品动漫在线观看 | 蜜臀久久99精品久久久久久 | 精品久久久久久久久久久久久久久久 | av免费观看网| 成人拍拍| 成人福利在线免费观看 | 日韩av影音先锋 | 久久午夜剧场 | 国产51自产区 | 自拍偷自拍亚洲精品播放 | 网友自拍咪咪爱 | 四虎久久久久 | 久久久蜜桃一区二区 | 欧美日韩国产在线一区 | 午夜极品 | 日本道在线观看 | 蜜桃视频日韩 | 日本一区二区高清视频 | 日韩欧美电影一区二区三区 | 色综合免费视频 | 婷婷亚洲综合五月天小说 | 日批视频免费观看 | 日本精品区 | 国产最新地址 | 久久一二 | 亚洲婷婷网 | 成人免费一区 | 美女成人在线 | 一区三区在线观看 | 韩国av一区二区三区 | 九九资源网 | 九九热久久免费视频 | 国产精品亚洲精品 | 四虎成人在线视频 | 亚洲精品综合 | 欧美日韩精品二区 | 丰满少妇被猛烈进入一区二区 | www天天操| 岛国大片在线观看 | 黄色777| 天天碰视频 | 亚洲污片 | 国产三级精品视频 | 国产第一页在线播放 | 免费在线观看av | 99国产精品欲 | 国产精品国产一区二区三区四区 | 杨幂国产精品一区二区 | 巨乳xxx| 欧美又粗又深又猛又爽啪啪九色 | 亚洲精品一区三区三区在线观看 | 色综合一区二区三区 | 狠色综合7777夜色撩人 | www.com在线观看| 97xxx| 欧美日韩精品中文字幕 | 青草91 | www国产com| 在线青草 | 成人片在线看 | 蜜臀av性久久久久蜜臀aⅴ涩爱 |