<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)475:供暖器

          共 1804字,需瀏覽 4分鐘

           ·

          2021-12-24 01:12

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?供暖器,我們先來看題面:
          https://leetcode-cn.com/problems/heaters/

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


          Every house can be warmed, as long as the house is within the heater's warm radius range.?


          Given the positions of houses and heaters on a horizontal line, return the minimum radius standard of heaters so that those heaters could cover all houses.


          Notice that all the heaters follow your radius standard, and the warm radius will the same.

          冬季已經(jīng)來臨。你的任務(wù)是設(shè)計(jì)一個有固定加熱半徑的供暖器向所有房屋供暖。
          在加熱器的加熱半徑范圍內(nèi)的每個房屋都可以獲得供暖。
          現(xiàn)在,給出位于一條水平線上的房屋 houses 和供暖器 heaters 的位置,請你找出并返回可以覆蓋所有房屋的最小加熱半徑。
          說明:所有供暖器都遵循你的半徑標(biāo)準(zhǔn),加熱的半徑也一樣。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?

          示例 1:
          輸入: houses = [1,2,3], heaters = [2]
          輸出: 1
          解釋: 僅在位置2上有一個供暖器。如果我們將加熱半徑設(shè)為1,那么所有房屋就都能得到供暖。

          示例 2:
          輸入: houses = [1,2,3,4], heaters = [1,4]
          輸出: 1
          解釋: 在位置1, 4上有兩個供暖器。我們需要將加熱半徑設(shè)為1,這樣所有房屋就都能得到供暖。

          示例 3:
          輸入:houses = [1,5], heaters = [2]
          輸出:3


          解題

          用二分查找法:前提是有序,所以我們先排序。

          class?Solution?{
          ????public?int?findRadius(int[] houses, int[] heaters)?{
          ????????Arrays.sort(heaters);
          ????????int?ans=-1;
          ????????for(int?house:houses){
          ????????????ans=Math.max(ans,findMinDest(house,heaters));
          ????????}
          ????????return?ans;
          ????}
          ????private?int?findMinDest(int?house,int[] heaters){
          ????????int?len=heaters.length-1;
          ????????if(house>=heaters[len]) return?(house-heaters[len]);
          ????????if(house<=heaters[0]) return?(heaters[0]-house);
          ????????int?des=Integer.MAX_VALUE;
          ????????int?left=0;
          ????????int?right=heaters.length-1;
          ????????while(left<=right){
          ????????????int?mid=left+((right-left)>>1);//這個地方當(dāng)時括號錯了,調(diào)了半天。
          ????????????if(heaters[mid]==house) return?0;
          ????????????if(heaters[mid]??????????????//如果當(dāng)前house>heater[mid],那我們向右縮小范圍
          ????????????????des=Math.min(des,Math.abs(house-heaters[mid]));
          ????????????????left=mid+1;
          ????????????}else{
          ????????????//當(dāng)前house
          ????????????????des=Math.min(des,Math.abs(house-heaters[mid]));
          ????????????????right=mid-1;
          ????????????}
          ????????}
          ????????return?des;
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-460題匯總,希望對你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)461:漢明距離

          LeetCode刷題實(shí)戰(zhàn)462:最少移動次數(shù)使數(shù)組元素相等 II

          LeetCode刷題實(shí)戰(zhàn)463:島嶼的周長

          LeetCode刷題實(shí)戰(zhàn)464:我能贏嗎

          LeetCode刷題實(shí)戰(zhàn)465:最優(yōu)賬單平衡

          LeetCode刷題實(shí)戰(zhàn)466:統(tǒng)計(jì)重復(fù)個數(shù)

          LeetCode刷題實(shí)戰(zhàn)467:環(huán)繞字符串中唯一的子字符串

          LeetCode刷題實(shí)戰(zhàn)468:驗(yàn)證IP地址

          LeetCode刷題實(shí)戰(zhàn)469:凸多邊形

          LeetCode刷題實(shí)戰(zhàn)470:用 Rand7() 實(shí)現(xiàn) Rand10()

          LeetCode刷題實(shí)戰(zhàn)471:編碼最短長度的字符串

          LeetCode刷題實(shí)戰(zhàn)472:連接詞

          LeetCode刷題實(shí)戰(zhàn)473:火柴拼正方形

          LeetCode刷題實(shí)戰(zhàn)474:一和零


          瀏覽 47
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  北条麻美在线无码 | 一级内射视频 | 国产精品日韩欧美大师 | 波多野结衣成人在线 | 亚洲中文大香蕉 |