topological-sort:

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.

by lek tin in "algorithm" access_time 2-min read

Course Schedule

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? Example 1 Input: 2, [[1,0]] Output: true Explanation: There are a total of 2 courses to take.

by lek tin in "algorithm" access_time 3-min read

Alien Dictionary

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 non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language. Example 1 Input: [ "wrt", "wrf", "er", "ett", "rftt" ] Output: "wertf" Example 2 Input: [ "z", "x" ] Output: "zx" Example 3 Input: [ "z", "x", "z" ] Output: "" Explanation The order is invalid, so return "".

by lek tin in "algorithm" access_time 3-min read