回复: 25
跳转到指定楼层
上一主题 下一主题
收起左侧

狗家店面跪经

全局:

2019(1-3月) 码农类General 硕士 全职@google - 内推 - 技术电面  | | Fail | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
您好!
本帖隐藏的内容需要积分高于 120 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 120 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies



没怎么好好刷题就随便说了个brute force。。应该是跪了

评分

参与人数 8大米 +34 收起 理由
孙行者 + 2 很有用的信息!
mengmeng4263 + 1 给你点个赞!
StrongerMe + 1 很有用的信息!
阿满 + 3
kaipeng21 + 3 谢谢分享!

查看全部评分


上一篇:条纹 onsite 详细挂经
下一篇:Facebook E5 三月面经

本帖被以下淘专辑推荐:

推荐
孙行者 2019-6-23 11:53:33 | 只看该作者
全局:
这题我想了几天,发现不复杂。根本就不需要拓扑排序。解法有点像BFS。

您好!
本帖隐藏的内容需要积分高于 200 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 200 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies


补充内容 (2019-6-23 12:00):
我这里用的方法是:
先做完的server取最长的task.
回复

使用道具 举报

推荐
chenchen628 2019-3-29 18:22:02 | 只看该作者
全局:
chenchen628 发表于 2019-3-29 16:44
1. 拓扑排序,确定每个任务现在开始最少还要多久。同时记录每个任务的前置数量,以及可能unlock哪些。 2.  ...
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>

  4. using namespace std;

  5. class Solution {
  6. public:
  7.   int fastedTaskSchedule(int N, vector<int>& tasks, vector<vector<int>>& prerequisites, int machines) {
  8.     vector<int> pre_nums(N, 0);
  9.     vector<vector<int>> unlocks(N, vector<int>());
  10.     for (auto pre : prerequisites) {
  11.       pre_nums[pre[1]] ++;
  12.       unlocks[pre[0]].push_back(pre[1]);
  13.     }
  14.         
  15.     vector<int> time_left(N, -1);
  16.     for (int i = 0; i < N; ++i) {
  17.       if (pre_nums[i] == 0) getTimeLeft(i, tasks, unlocks, time_left);
  18.     }
  19.         
  20.     auto cmp_min = [](const pair<int, int>& t1, const pair<int, int>& t2) {
  21.       return t1.second > t2.second;
  22.     };
  23.     auto cmp_max = [](const pair<int, int>& t1, const pair<int, int>& t2) {
  24.       return t1.second < t2.second;
  25.     };
  26.         
  27.     priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp_min)> task_finish(cmp_min);
  28.     priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp_max)> task_ready(cmp_max);
  29.         
  30.     for (int i = 0; i < N; ++i) {
  31.       if (pre_nums[i] == 0) task_ready.push({i, time_left[i]});
  32.     }
  33.         
  34.     int cur_time = 0;
  35.     while (!task_ready.empty() || !task_finish.empty()) {
  36.       while (task_finish.size() < machines && !task_ready.empty()) {
  37.         auto task = task_ready.top();
  38.         task_ready.pop();
  39.         task_finish.push({task.first, cur_time + tasks[task.first]});
  40.       }
  41.             
  42.       auto cur_finish = task_finish.top();
  43.       task_finish.pop();
  44.       cur_time = cur_finish.second;
  45.       int cur = cur_finish.first;
  46.       for (int next : unlocks[cur]) {
  47.         pre_nums[next] --;
  48.         if (pre_nums[next] == 0) task_ready.push({next, time_left[next]});
  49.       }
  50.     }
  51.     return cur_time;
  52.   }
  53.    
  54.   void getTimeLeft(int i, const vector<int>& tasks, const vector<vector<int>>& unlocks, vector<int>& time_left) {
  55.     if (time_left[i] != -1) return;
  56.     time_left[i] = tasks[i];
  57.     int max_next = 0;
  58.     for (int next : unlocks[i]) {
  59.       getTimeLeft(next, tasks, unlocks, time_left);
  60.       max_next = max(max_next, time_left[next]);
  61.     }
  62.     time_left[i] += max_next;
  63.   }
  64. };

  65. int main()
  66. {
  67.   Solution s;
  68.    
  69.   int N = 6;
  70.   vector<int> tasks = {10, 10, 2, 3, 100, 200};
  71.   vector<vector<int>> prerequisites = {{0, 1}, {2, 3}, {4, 5}};
  72.   int machines = 2;
  73.    
  74.   cout << s.fastedTaskSchedule(N, tasks, prerequisites, machines) << endl;

  75.   return 0;
  76. }
复制代码

评分

参与人数 1大米 +2 收起 理由
孙行者 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

推荐
ziwei1992 2019-4-10 08:10:36 | 只看该作者
全局:
附上一个python版本,还有楼上讨论的两个test case
  1.     def task_scheduler(dependencies, k, times)

  2. [code]    def task_scheduler(dependencies, k, times):
  3.         def makeGraph(dependencies):
  4.             graph = collections.defaultdict(list)
  5.             for s, e in dependencies:
  6.                 graph[s].append(e)
  7.             return graph
  8.         
  9.         def computeIndegree(graph, n):
  10.             indegree = {}
  11.             for i in xrange(n):
  12.                 indegree[i] = 0
  13.             for node in graph:
  14.                 for nei in graph[node]:
  15.                     indegree[nei] += 1
  16.             return indegree
  17.         
  18.         n = len(times)
  19.         graph = makeGraph(dependencies)
  20.         indegree = computeIndegree(graph, n)
  21.         waiting = []
  22.         process = []
  23.         for node in indegree:
  24.             if indegree[node] == 0:
  25.                 heappush(waiting, (-times[node], node))
  26.             
  27.         while len(process) < k and waiting:
  28.             t, task = heappop(waiting)
  29.             heappush(process, (-t, task))
  30.         
  31.         while process:
  32.             #print process
  33.             time, task = heappop(process)
  34.             for nei in graph[task]:
  35.                 indegree[nei] -= 1
  36.                 if indegree[nei] == 0:
  37.                     heappush(waiting, (-times[nei], nei))
  38.                     
  39.             #print waiting
  40.             while len(process) < k and waiting:
  41.                 dt, task = heappop(waiting)
  42.                 heappush(process, (time - dt, task))
  43.         
  44.         return time
  45.    
  46.     dependencies = [[3,1],[3,2],[1,0]]
  47.     times = [1,2,3,4]
  48.     k = 2
  49.     print task_scheduler(dependencies, k, times)
  50.    
  51.     dependencies = [[0,1],[2,3],[4,5]]
  52.     times = [10, 10, 5, 4, 100, 200]
  53.     k = 2
  54.     print task_scheduler(dependencies, k, times)
复制代码




补充内容 (2019-4-10 08:11):
大体就是topological sort,外加两个priority queue维护

评分

参与人数 1大米 +2 收起 理由
孙行者 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
Akira明明 2019-3-25 06:04:01 | 只看该作者
全局:
楼主面的是美国的全职吗?现在还在发面试吗?
回复

使用道具 举报

🔗
 楼主| toranosuke 2019-3-25 06:05:42 | 只看该作者
全局:
Akira明明 发表于 2019-3-25 06:04
楼主面的是美国的全职吗?现在还在发面试吗?

因为有事拖到现在才面的,二月底发的面试通知
回复

使用道具 举报

🔗
Hannah_sy 2019-3-25 06:32:46 | 只看该作者
全局:
这个感觉有点麻烦,lz最后怎么解的啊
回复

使用道具 举报

全局:
店面遇到这个题应该算是比较背的,这题还挺麻烦,我觉得应该是用拓扑排序,先找indegree为0的点放进线程池子,等结束在找下一波indegree为0的点,直到最后算出时间。
回复

使用道具 举报

🔗
lalxyy 2019-3-25 09:53:00 | 只看该作者
全局:
yangyuzhiguang 发表于 2019-3-25 06:39
店面遇到这个题应该算是比较背的,这题还挺麻烦,我觉得应该是用拓扑排序,先找indegree为0的点放进线程池 ...

我也是这样的想法。但是隐约觉得得到的不一定是最优解
回复

使用道具 举报

🔗
jevi 2019-3-25 10:46:13 | 只看该作者
全局:
先找critical path , schedule 第一段 并且设置成0, 再重新计算critial path, schedule 下一个。
这样貌似能最优,复杂度有点高
回复

使用道具 举报

🔗
mhsasd 2019-3-25 12:06:45 来自APP | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
黓龙君 2019-3-25 15:52:27 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

全局:
店面这难度也太过分了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表