每周完成一个ARTS:

1.Algorithm:每周至少做一个 leetcode 的算法题 2.Review:阅读并点评至少一篇英文技术文章 3.Tip:学习至少一个技术技巧 4.Share:分享一篇有观点和思考的技术文章

Algorithm

leetcode-210

210. Course Schedule II

Desc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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.

Example 1:

Input: 2, [[1,0]] 
Output: [0,1]
Explanation: 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] .
Example 2:

Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: 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] .

Analyze

依赖关系转化为有向图之间节点之间关系,问题可以转化图的问题:

根据图的连接关系找出遍历图中所有节点(不能重复) 题目中只需要找出一种可行路径即可

Solution

 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
class Solution {
public:
    vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<int> res;
        vector<vector<int> > graph(numCourses, vector<int>(0));
        vector<int> in(numCourses, 0);
        // build graph
        for (auto &a : prerequisites) {
            graph[a.second].push_back(a.first);
            // depend count 
            ++in[a.first];
        }
        queue<int> q;
        // no depend  on any  prepare bfs condition
        for (int i = 0; i < numCourses; ++i) {
            if (in[i] == 0) q.push(i);
        }
        // bfs 
        while (!q.empty()) {
            int t = q.front();
            res.push_back(t);
            q.pop();
            // get next  node 
            for (auto &a : graph[t]) {
                // count -- 
                --in[a];
                if (in[a] == 0) q.push(a);
            }
        }
        if (res.size() != numCourses) res.clear();
        return res; 
    }
};

Deep

  1. 如何输出所有的路径

Relation

  1. 207. Course Schedule
  2. 630. Course Schedule III

Review

引入设计调度器追求三个目标:

  1. 降低Context-switch开销
  2. 减少调度延迟
  3. 避免cache miss概率

来自Scheduling In Go : Part I - OS Scheduler

Tip

聪明并且努力却不能成功的七个原因(自查与共勉):

  1. You don’t reach out to new people 没有主动接触新的朋友
  2. You are averse to change 逃避改变与挑战
  3. You’re not willing to take risks 不愿意承担风险
  4. You believe you deserve success based on credentials 认为自己理应成功
  5. You constantly go after whatever’s exciting at the moment 总是追求热点
  6. You can’t commit to a decision 不能做出决定
  7. You don’t believe in yourself 不相信自己

注:内容来自7 Reasons Why Smart, Hardworking People Don’t Become Successful

Share

  1. 分享一种分析信息技术发展的框架

(End)

欢迎关注

欢迎关注微信公众帐号:沉风网事(savewind)

沉风网事