📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 16
跳转到指定楼层
上一主题 下一主题
收起左侧

野的 店面

全局:

2017(7-9月) 码农类General 硕士 全职@uber - 内推 - 技术电面  | | Other | 在职跳槽

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

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

x
上周刚面完店面, 给一个json input,列了一个list的intersection。每个intersection有一个list的road。每条road有通向的intersection,cost和名字。求给一个起始intersection和一个终点intersection,找到最短的路径并且打印出来。
ex.

您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
ection class和一个road class,然后再用一个HashMap建一个adjacent list。算法是用dfs去搜索。但是楼主最后始终输出的路径不是最短的一条,不知道最后能不能过。。。



上一篇:两个西格玛 OA
下一篇:阅后即焚 新鲜 店面面经
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

推荐
endofunctor 2017-7-31 15:51:58 | 只看该作者
全局:
试着写了下。。。这题应该是对于每对intersection,取距离最短的road作为edge,然后dijkstra,dijkstra没用堆优化。
假设输入已经parse好了
请面过的老司机指点下用C++面Uber怎么做这种题。。。
  1. vector<string> getPath(unordered_map<string, unordered_map<string, pair<string, int> > >& mp, const string& start, const string& end) {
  2.   unordered_map<string, int> dist;
  3.   unordered_set<string> q;
  4.   for (const auto& p: mp) {
  5.     dist[p.first] = INT_MAX;
  6.     q.insert(p.first);
  7.   }
  8.   dist[start] = 0;
  9.   unordered_map<string, string> previous;
  10.   if (mp.count(start) == 0 || start == end) {
  11.     return {};
  12.   }
  13.   while (not q.empty()) {
  14.     string cur;
  15.     int curDist = INT_MAX;
  16.     for (const string& s: q) {
  17.       if (dist[s] < curDist) {
  18.         curDist = dist[s];
  19.         cur = s;
  20.       }
  21.     }
  22.     for (auto p: mp[cur]) {
  23.       if (dist[p.first]> dist[cur] + p.second.second) {
  24.         dist[p.first] = dist[cur] + p.second.second;
  25.         previous[p.first] = cur;
  26.       }
  27.     }
  28.     q.erase(cur);
  29.   }
  30.   vector<string> res;
  31.   string tmp = end;
  32.   while (tmp != start) {
  33.     res.push_back(mp[previous[tmp]][tmp].first);
  34.     tmp = previous[tmp];
  35.   }
  36.   reverse(res.begin(), res.end());
  37.   return res;
  38. }
复制代码
回复

使用道具 举报

推荐
endofunctor 2017-7-31 16:06:56 | 只看该作者
全局:
咋不能发帖了。。。
思路大概是,对于每对intersection,选择最短的一条road构造图,然后Dijkstra。(Dijkstra没用堆优化)
假设input已经parse好:
  1. vector<string> getPath(unordered_map<string, unordered_map<string, pair<string, int> > >& mp, const string& start, const string& end) {
  2.   unordered_map<string, int> dist;
  3.   unordered_set<string> q;
  4.   for (const auto& p: mp) {
  5.     dist[p.first] = INT_MAX;
  6.     q.insert(p.first);
  7.   }
  8.   dist[start] = 0;
  9.   unordered_map<string, string> previous;
  10.   if (mp.count(start) == 0 || start == end) {
  11.     return {};
  12.   }
  13.   while (not q.empty()) {
  14.     string cur;
  15.     int curDist = INT_MAX;
  16.     for (const string& s: q) {
  17.       if (dist[s] < curDist) {
  18.         curDist = dist[s];
  19.         cur = s;
  20.       }
  21.     }
  22.     for (auto p: mp[cur]) {
  23.       if (dist[p.first]> dist[cur] + p.second.second) {
  24.         dist[p.first] = dist[cur] + p.second.second;
  25.         previous[p.first] = cur;
  26.       }
  27.     }
  28.     q.erase(cur);
  29.   }
  30.   vector<string> res;
  31.   string tmp = end;
  32.   while (tmp != start) {
  33.     res.push_back(mp[previous[tmp]][tmp].first);
  34.     tmp = previous[tmp];
  35.   }
  36.   reverse(res.begin(), res.end());
  37.   return res;
  38. }
复制代码

顺便求老司机解答如何用C++写这种题。。。
回复

使用道具 举报

本楼:
全局:
野的是啥。。。
回复

使用道具 举报

🔗
endofunctor 2017-7-31 13:23:04 | 只看该作者
全局:
用C++的表示哭晕在厕所
回复

使用道具 举报

🔗
steveumn 2017-8-1 03:55:08 | 只看该作者
全局:
弱问一句: "野的"是指哪一家?
回复

使用道具 举报

🔗
endofunctor 2017-8-1 10:51:13 | 只看该作者
全局:
endofunctor 发表于 2017-7-31 16:06
咋不能发帖了。。。
思路大概是,对于每对intersection,选择最短的一条road构造图,然后Dijkstra。(Dijk ...

发重了。。。求老爷把这一层砍了。。。
回复

使用道具 举报

🔗
endofunctor 2017-8-1 10:51:26 | 只看该作者
全局:
steveumn 发表于 2017-8-1 03:55
弱问一句: "野的"是指哪一家?

Uber,凑个字数
回复

使用道具 举报

🔗
juw037 2017-8-1 14:23:51 | 只看该作者
全局:
yě dī 字数字数
回复

使用道具 举报

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

本版积分规则

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