国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

【LC總結(jié)】圖、拓?fù)渑判?(Course Schedule I, II/Alien Dictiona

gaara / 696人閱讀

Course Schedule Problem

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, is it possible for you to finish all courses?

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 it is possible.

2, [[1,0],[0,1]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

One simple way to represent a graph is just a list, or array, of |E| edges, which we call an edge list. To represent an edge, we just have an array of two vertex numbers, or an array of objects containing the vertex numbers of the vertices that the edges are incident on. If edges have weights, add either a third element to the array or more information to the object, giving the edge"s weight. Since each edge contains just two or three numbers, the total space for an edge list is Θ(E).

Solution Graph using HashMap+Queue
public class Solution {
    public boolean canFinish(int n, int[][] p) {
        Map> graph = new HashMap<>();
        int[] in = new int[n];
        for (int i = 0; i < p.length; i++) {
            if (!graph.containsKey(p[i][1])) graph.put(p[i][1], new ArrayList<>());
            graph.get(p[i][1]).add(p[i][0]);
            in[p[i][0]]++;
        }
        Queue q = new LinkedList<>();
        for (int i = 0; i < in.length; i++) {
            if (in[i] == 0) q.offer(i);
        }
        int count = 0;
        while (!q.isEmpty()) {
            Integer from = q.poll();
            count++;
            List to = graph.get(from);
            if (to == null) continue;
            for (Integer i: to) {
                in[i]--;
                if (in[i] == 0) q.offer(i);
            }
        }
        return count == n;
    }
}
Graph using Queue
public class Solution {
    public boolean canFinish(int n, int[][] p) {
        int[] in = new int[n];
        for (int i = 0; i < p.length; i++) {
            in[p[i][0]]++;
        }
        Queue q = new LinkedList<>();
        for (int i = 0; i < in.length; i++) {
            if (in[i] == 0) q.offer(i);
        }
        while (!q.isEmpty()) {
            int cur = q.poll();
            for (int i = 0; i < p.length; i++) {
                if (cur == p[i][1]) {in[p[i][0]]--;
                if (in[p[i][0]] == 0) q.offer(p[i][0]);}
            }
        }
        for (int i = 0; i < in.length; i++) {
            if (in[i] != 0) return false;
        }
        return true;
    }
}
Course Schedule II Problem

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].

Solution
public class Solution {
    public int[] findOrder(int n, int[][] p) {
        int[] res = new int[n];
        int[] in = new int[n];
        Queue q = new LinkedList<>();
        for (int i = 0; i < p.length; i++) {
            in[p[i][0]]++;
        }
        for (int i = 0; i < in.length; i++) {
            if (in[i] == 0) q.offer(i);
        }
        int index = 0;
        while (!q.isEmpty()) {
            Integer cur = q.poll();
            res[index++] = cur;
            for (int i = 0; i < p.length; i++) {
                if (p[i][1] == cur) {
                    in[p[i][0]]--;
                    if (in[p[i][0]] == 0) q.offer(p[i][0]);
                }
            }
        }
        return index == n ? res: new int[0];
    }
}
Alien Dictionary Problem

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

For example,
Given the following words in dictionary,

[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

The correct order is: "wertf".

Note:
You may assume all letters are in lowercase.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return any one of them is fine.

Note Soluton

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/66100.html

相關(guān)文章

  • [Leetcode] Course Schedule 課程計(jì)劃

    Course Schedule I 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 e...

    Amio 評(píng)論0 收藏0
  • [Algo] Install Dependencies 安裝依賴

    摘要:拓?fù)渑判驈?fù)雜度時(shí)間空間思路本題和的解法一樣,不會(huì)拓?fù)渑判虻目梢詤⒖寄瞧恼隆^(qū)別在于我們拓?fù)渑判蚝蟮脑L問順序,本來我們是用一個(gè)來進(jìn)行,這里為了讓依賴少的先安裝,我們將換成,并以依賴數(shù)排序。 Install Dependencies 給定軟件之間安裝的依賴關(guān)系,用一個(gè)二維數(shù)組表示,第一維表示依賴的序號(hào),第二維表示依賴關(guān)系,比如要先裝deps[0][0],才能裝deps[0][1]。安裝時(shí)...

    li21 評(píng)論0 收藏0
  • 210. Course Schedule II

    摘要:建立入度組成,把原來輸入的無規(guī)律,轉(zhuǎn)換成另一種表示圖的方法。找到為零的點(diǎn),放到里,也就是我們圖的入口。對(duì)于它的也就是指向的。如果這些的入度也變成,也就變成了新的入口,加入到里,重復(fù)返回結(jié)果。這里題目有可能沒有預(yù)修課,可以直接上任意課程。 Some courses may have prerequisites, for example to take course 0 you have ...

    lbool 評(píng)論0 收藏0
  • [LeetCode] 210. Course Schedule II

    Problem 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...

    zhkai 評(píng)論0 收藏0
  • [LeetCode/LintCode] Course Schedule II

    Problem 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 a...

    Lavender 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

gaara

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<