<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)210:課程表 II

          共 6426字,需瀏覽 13分鐘

           ·

          2021-03-14 14:02

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

          今天和大家聊的問題叫做 課程表 II,我們先來看題面:
          https://leetcode-cn.com/problems/course-schedule-ii/


          There are a total of n courses you have to take labelled from 0 to n - 1.

          Some courses may have prerequisites, for example, if prerequisites[i] = [ai, bi] this means you must take the course bi before the course ai.

          Given the total number of courses numCourses and a list of the prerequisite pairs, return the ordering of courses you should take to finish all courses.

          If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.


          題意


          現(xiàn)在你總共有 n 門課需要選,記為 0 到 n-1。

          在選修某些課程之前需要一些先修課程。例如,想要學(xué)習(xí)課程 0 ,你需要先完成課程 1 ,我們用一個(gè)匹配來表示他們: [0,1]

          給定課程總量以及它們的先決條件,返回你為了學(xué)完所有課程所安排的學(xué)習(xí)順序。

          可能會(huì)有多個(gè)正確的順序,你只要返回一種就可以了。如果不可能完成所有課程,返回一個(gè)空數(shù)組。

          示例


          示例 1:

          輸入: 2, [[1,0]]
          輸出: [0,1]
          解釋: 總共有 2 門課程。要學(xué)習(xí)課程 1,你需要先完成課程 0。因此,正確的課程順序?yàn)?[0,1] 。

          示例 2:

          輸入: 4, [[1,0],[2,0],[3,1],[3,2]]
          輸出: [0,1,2,3] or [0,2,1,3]
          解釋: 總共有 4 門課程。要學(xué)習(xí)課程 3,你應(yīng)該先完成課程 1 和課程 2。并且課程 1 和課程 2 都應(yīng)該排在課程 0 之后。
            因此,一個(gè)正確的課程順序是 [0,1,2,3] 。另一個(gè)正確的排序是 [0,2,1,3] 。

          說明:

          輸入的先決條件是由邊緣列表表示的圖形,而不是鄰接矩陣。詳情請(qǐng)參見圖的表示法。
          你可以假定輸入的先決條件中沒有重復(fù)的邊。

          提示:

          這個(gè)問題相當(dāng)于查找一個(gè)循環(huán)是否存在于有向圖中。如果存在循環(huán),則不存在拓?fù)渑判?,因此不可能選取所有課程進(jìn)行學(xué)習(xí)。
          通過 DFS 進(jìn)行拓?fù)渑判?- 一個(gè)關(guān)于Coursera的精彩視頻教程(21分鐘),介紹拓?fù)渑判虻幕靖拍睢?br mpa-from-tpl="t">拓?fù)渑判蛞部梢酝ㄟ^ BFS 完成。


          解題


          本題跟 207 題完全一致,只是增加了路徑輸出。207可以點(diǎn)擊看, LeetCode刷題實(shí)戰(zhàn)207:課程表

          廣度優(yōu)先

          class Solution {
          public:
              vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
                unordered_map<int, unordered_set<int>> m;
                vector<int> indegree(numCourses, 0);
                for(auto& pre : prerequisites)
                {
                  m[pre[1]].insert(pre[0]);
                  indegree[pre[0]]++;
                }
                queue<int> q;
                int tp, finish = 0, i;
                for(i = 0; i < numCourses; ++i)
                  if(indegree[i] == 0)
                    q.push(i);
                vector<int> ans(numCourses);
                i = 0;
              while(!q.empty())
              {
                tp = q.front();
                finish++;
                ans[i++] = tp;
                q.pop();
                for(auto id : m[tp])
                {
                  if(--indegree[id] == 0)
                    q.push(id);
                }
              }
              if(i != numCourses)
                return {};
              return ans;
              }
          };


          深度優(yōu)先

          class Solution {
              unordered_map<int, unordered_set<int>> m;
              vector<int> ans;
          public:
              vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
                  vector<int> indegree(numCourses, 0);
                for(auto& pre : prerequisites)
                {
                      m[pre[1]].insert(pre[0]);
                      indegree[pre[0]]++;
                  }
                  bool can = true;
                  vector<int> visited(numCourses,0);
                for(int i = 0; i < numCourses; ++i)
                  {
                      if(indegree[i]==0)//從入度0的開始
                          dfs(i, visited, can);
                      if(!can)
                          return {};
                  }
                  if(ans.size() < numCourses)//不夠個(gè)數(shù),說明有的存在環(huán)
                      return {};
                  reverse(ans.begin(),ans.end());//反序
                  return ans;
              }

              void dfs(int i, vector<int> & visited, bool &can)
              
          {
                  if(!can) return;
                  if(visited[i]==2) return;
                  if(visited[i]==1)
                  {
                      can = false;
                      return;
                  }
                  visited[i] = 1;//進(jìn)入的時(shí)候?yàn)?
                  for(auto id : m[i])
                      dfs(id, visited, can);
                  visited[i] = 2;//出去的時(shí)候?yàn)?
                  ans.push_back(i);//出去的時(shí)候push進(jìn)去,(這是最后做的)最終答案反序即可
              }
          };


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

          上期推文:

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

          LeetCode刷題實(shí)戰(zhàn)201:數(shù)字范圍按位與

          LeetCode刷題實(shí)戰(zhàn)202:快樂數(shù)

          LeetCode刷題實(shí)戰(zhàn)203:移除鏈表元素

          LeetCode刷題實(shí)戰(zhàn)204:計(jì)數(shù)質(zhì)數(shù)

          LeetCode刷題實(shí)戰(zhàn)205:同構(gòu)字符串

          LeetCode刷題實(shí)戰(zhàn)206:反轉(zhuǎn)鏈表

          LeetCode刷題實(shí)戰(zhàn)207:課程表

          LeetCode刷題實(shí)戰(zhàn)208:實(shí)現(xiàn) Trie (前綴樹)

          LeetCode刷題實(shí)戰(zhàn)209:長(zhǎng)度最小的子數(shù)組


          瀏覽 32
          點(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>
                  免费视频在线观看久 | 中国熟妇XXX | 欧美亚洲视频 | 日本无码中文字幕乱码aⅴ的特色 | 京东热av|