210. Course Schedule II

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

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

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

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].
Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

一刷
思路同207

public class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        List<Integer> seq = new ArrayList<>();
        int[][] topo = new int[numCourses][numCourses];
        int[] indegree = new int[numCourses];
        for(int i=0; i<prerequisites.length; i++){
            int pre = prerequisites[i][1];
            int ready = prerequisites[i][0];
            if(topo[pre][ready] == 0){
                indegree[ready]++;
            }
            topo[pre][ready] = 1;
        }
        
        
        Queue<Integer> wl = new LinkedList();
        for(int i=0; i<numCourses; i++){
            if(indegree[i] == 0) wl.offer(i);
        }
        
        while(!wl.isEmpty()){
            int course = wl.poll();
            seq.add(course);
            for(int i=0; i<numCourses; i++){
                if(topo[course][i]!=0){
                    indegree[i]--;
                    if(indegree[i] == 0) wl.offer(i);
                }
            }
        }
        
        if(seq.size()!=numCourses) return new int[0];
        
        int[] res = new int[seq.size()];
        for(int i=0; i<seq.size(); i++){
            res[i] = seq.get(i);
        }
        return res;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子探颈,更是在濱河造成了極大的恐慌褐耳,老刑警劉巖洛勉,帶你破解...
    沈念sama閱讀 221,888評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件聊疲,死亡現(xiàn)場(chǎng)離奇詭異竣况,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)悴灵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)骂蓖,“玉大人积瞒,你說(shuō)我怎么就攤上這事〉窍拢” “怎么了茫孔?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,386評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)被芳。 經(jīng)常有香客問(wèn)我缰贝,道長(zhǎng),這世上最難降的妖魔是什么畔濒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,726評(píng)論 1 297
  • 正文 為了忘掉前任剩晴,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘赞弥。我一直安慰自己毅整,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布绽左。 她就那樣靜靜地躺著悼嫉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪妇菱。 梳的紋絲不亂的頭發(fā)上承粤,一...
    開(kāi)封第一講書(shū)人閱讀 52,337評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音闯团,去河邊找鬼辛臊。 笑死,一個(gè)胖子當(dāng)著我的面吹牛房交,可吹牛的內(nèi)容都是我干的彻舰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,902評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼候味,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼刃唤!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起白群,我...
    開(kāi)封第一講書(shū)人閱讀 39,807評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤尚胞,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后帜慢,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體笼裳,經(jīng)...
    沈念sama閱讀 46,349評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評(píng)論 3 340
  • 正文 我和宋清朗相戀三年粱玲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了躬柬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,567評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抽减,死狀恐怖允青,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情卵沉,我是刑警寧澤颠锉,帶...
    沈念sama閱讀 36,242評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站偎箫,受9級(jí)特大地震影響木柬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜淹办,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評(píng)論 3 334
  • 文/蒙蒙 一眉枕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦速挑、人聲如沸谤牡。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,420評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)翅萤。三九已至,卻和暖如春腊满,著一層夾襖步出監(jiān)牢的瞬間套么,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,531評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工碳蛋, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留胚泌,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,995評(píng)論 3 377
  • 正文 我出身青樓肃弟,卻偏偏與公主長(zhǎng)得像玷室,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子笤受,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容