LeetCode Notes 021

Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.

Example:
For num = 5 you should return [0,1,1,2,1,2].

Follow up:

  • It is very easy to come up with a solution with run time $O(n*sizeof(integer))$. But can you do it in linear time $O(n)$ /possibly in a single pass?
  • Space complexity should be $O(n)$.
  • Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @param {number} num
* @return {number[]}
*/
var countBits = function(num) {
var ans = Array(num + 1);
for (var i = 0; i <= num; i++)
ans[i] = hammingWeight(i);

return ans;
};

var hammingWeight = function(n) {
n = ((n & 0xAAAAAAAA) >>> 1) + (n & 0x55555555);
n = ((n & 0xCCCCCCCC) >>> 2) + (n & 0x33333333);
n = ((n & 0xF0F0F0F0) >>> 4) + (n & 0x0F0F0F0F);
n = ((n & 0xFF00FF00) >>> 8) + (n & 0x00FF00FF);
n = ((n & 0xFFFF0000) >>> 16) + (n & 0x0000FFFF);
return n;
};

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?

For example:

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

1
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:

  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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function(numCourses, prerequisites) {
var map = []; // 邻接表
var indegree = []; // 入度

for (var i = 0; i < numCourses; i++)
map[i] = [], indegree[i] = 0;

prerequisites.forEach(function(item) {
var from = item[1];
var to = item[0];

map[from].push(to);
indegree[to]++;
});

var q = [];
var finishNum = 0;
for (var i = 0; i < numCourses; i++) {
// 入度为 0,没有依赖
if (!indegree[i]) {
q.push(i);
finishNum++;
}
}

while (q.length) {
var from = q.shift();

map[from].forEach(function(to) {
if (--indegree[to] === 0) {
q.push(to);
finishNum++;
}
});
}

return finishNum === numCourses;
};

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:

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

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.

Hints:

  1. This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
  2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  3. Topological sort could also be done via BFS.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {number[]}
*/
var findOrder = function(numCourses, prerequisites) {
var map = []; // 邻接表
var indegree = []; // 入度

for (var i = 0; i < numCourses; i++)
map[i] = [], indegree[i] = 0;

prerequisites.forEach(function(item) {
var from = item[1];
var to = item[0];

map[from].push(to);
indegree[to]++;
});

var q = [];
var finishNum = 0;
var ans = [];

for (var i = 0; i < numCourses; i++) {
// 入度为 0,没有依赖
if (!indegree[i]) {
q.push(i);
finishNum++;
}
}

while (q.length) {
var from = q.shift();
ans.push(from);

map[from].forEach(function(to) {
if (--indegree[to] === 0) {
q.push(to);
finishNum++;
}
});
}

return finishNum === numCourses ? ans : [];
};
文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/03/30/LeetCode-Notes-021/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏