<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)573:松鼠模擬

          共 1239字,需瀏覽 3分鐘

           ·

          2022-04-11 11:54

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

          今天和大家聊的問題叫做?松鼠模擬,我們先來看題面:
          https://leetcode-cn.com/problems/squirrel-simulation/

          There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Your goal is to find the minimal distance for the squirrel to collect all the nuts and put them under the tree one by one. The squirrel can only take at most one nut at one time and can move in four directions - up, down, left and right, to the adjacent cell. The distance is represented by the number of moves.

          現(xiàn)在有一棵樹,一只松鼠和一些堅(jiān)果。位置由二維網(wǎng)格的單元格表示。你的目標(biāo)是找到松鼠收集所有堅(jiān)果的最小路程,且堅(jiān)果是一顆接一顆地被放在樹下。松鼠一次最多只能攜帶一顆堅(jiān)果,松鼠可以向上,向下,向左和向右四個(gè)方向移動(dòng)到相鄰的單元格。移動(dòng)次數(shù)表示路程。

          示例



          解題

          https://blog.csdn.net/weixin_44171872/article/details/108985656

          主要思路:
          (1)將每個(gè)堅(jiān)果取回到樹上,則需要松鼠從樹上出發(fā),取到堅(jiān)果,再返回到樹,這個(gè)距離其實(shí)就是樹和堅(jiān)果作為對(duì)角線之間的矩形的寬高之和;
          (2)則可以先將所有的堅(jiān)果到樹的距離統(tǒng)計(jì)出來;
          (3)考慮到松鼠起始位置并不在樹上,故松鼠第一次取的堅(jiān)果可能導(dǎo)致總的路程不一致,存在最短距離,故可以針對(duì)每個(gè)堅(jiān)果作為松鼠第一個(gè)要取的堅(jiān)果,來計(jì)算此時(shí)的距離,這個(gè)過程中保存最小值即可;

          class?Solution?{
          public:
          ????int?minDistance(int?height, int?width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts)?{
          ????????int?sum_nums=0;
          ????????//先統(tǒng)計(jì)出所有堅(jiān)果的距離
          ????????for(vector<int>&nut:nuts){
          ????????????sum_nums+=abs(nut[0]-tree[0])+abs(nut[1]-tree[1]);
          ????????}
          ????????sum_nums<<=1;
          ????????int?res=INT_MAX;
          ????????//針對(duì)每個(gè)堅(jiān)果作為松鼠要取的第一個(gè)堅(jiān)果,計(jì)算各個(gè)距離,并保存最小的距離
          ????????for(vector<int>&nut:nuts){
          ????????????res=min(res,sum_nums-(abs(nut[0]-tree[0])+abs(nut[1]-tree[1]))+(abs(nut[0]-squirrel[0])+abs(nut[1]-squirrel[1])));
          ????????}
          ????????return?res;
          ????}
          };


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

          上期推文:

          LeetCode1-560題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)561:數(shù)組拆分 I
          LeetCode刷題實(shí)戰(zhàn)562:矩陣中最長(zhǎng)的連續(xù)1線段
          LeetCode刷題實(shí)戰(zhàn)563:二叉樹的坡度
          LeetCode刷題實(shí)戰(zhàn)564:尋找最近的回文數(shù)
          LeetCode刷題實(shí)戰(zhàn)565:數(shù)組嵌套
          LeetCode刷題實(shí)戰(zhàn)566:重塑矩陣
          LeetCode刷題實(shí)戰(zhàn)567:字符串的排列
          LeetCode刷題實(shí)戰(zhàn)568:最大休假天數(shù)
          LeetCode刷題實(shí)戰(zhàn)569:?jiǎn)T工薪水中位數(shù)
          LeetCode刷題實(shí)戰(zhàn)570:至少有5名直接下屬的經(jīng)理
          LeetCode刷題實(shí)戰(zhàn)571:給定數(shù)字的頻率查詢中位數(shù)
          LeetCode刷題實(shí)戰(zhàn)572:另一棵樹的子樹

          瀏覽 67
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(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>
                  久久精品视频免费看 | 下一篇日韩动态图 | 香蕉色色网站 | 久热无码一区二区三区 | 大香蕉中文电影 |