阿里巴巴算法工程师编程题

第一题:现在城市有N个路口,每个路口有自己的编号,从0到N-1,每个路口还有自己的交通控制信号,例如0,3表示0号路口的交通信号每3个时刻变化一次,即0到3时刻0号路口允许通过,3到6时刻不允许通过,而6到9时刻又允许通过;以此类推,所有路口的允许通行都从时刻0开始。同时城市中存在M条道路将这N个路口相连接起来,确保从一个路口到另一个路口都可达,每条路由两个端点加上通行所需要的时间表示。现在给定起始路口和目的路口,从0时刻出发,请问最快能在什么时刻到达?

第二题:菜鸟仓库是一个很大很神奇的地方,各种琳琅满目的商品整整齐齐地摆放在一排排货架上,通常一种品类(sku)的商品会放置在货架的某一个格子中,格子设有统一的编号,方便工人们拣选。 有一天沐哲去菜鸟仓库参观,无意中发现第1个货架格子编码为1,第2-3个分别为1,2,第4-6个格子分别是1,2,3,第7-10个格子编号分别是1,2,3,4,每个格子编号都是0-9中的一个整数,且相邻格子的编号连在一起有如下规律 1|12|123|1234|...|123456789101112131415|...|123456789101112131415……n 这个仓库存放的商品品类非常丰富,共有1千万多个货架格子。沐哲很好奇,他想快速知道第k个格子编号是多少?
#阿里巴巴#
全部评论
# 第一题应该是dijkstra's algorithm的变种?没来得及仔细debug,通过率0%。第二题看起来简单,到交卷了才发现编号只能0-9,通过率0% (此处应该微笑 def minTravelTime(intersections, roads, start, goal): frontier = [] frontier.append([start, 0]) came_from = {} cost_so_far = {} came_from[start] = None cost_so_far[start] = 0 t = 0 while len(frontier) > 0: current = min(frontier, key=lambda x: x[1]) frontier.remove(current) current = current[0] if current == goal: print(current) return cost_so_far(current) for next in get_neighbors(current, roads): wait_time = get_wait_time(next[0], intersections, t) new_cost = cost_so_far[current] + wait_time + next[2] t = t + wait_time + next[2] if next[1] not in cost_so_far or new_cost < cost_so_far[next[1]]: cost_so_far[next[1]] = new_cost frontier.append([next[1], new_cost]) came_from[next] = current def get_neighbors(node, roads): results = [] for road in roads: if node == roads[0]: results.append(road) return results def get_wait_time(node, intersection, t): change = intersection[node] if (t/change)%2 == 0: return 0 else: return ((t/change)+1)*change - t
点赞
送花
回复 分享
发布于 2017-08-25 21:30
想问一下,没点运行,直接交卷,如果程序正确,有分么。。。
点赞
送花
回复 分享
发布于 2017-08-26 11:12
秋招专场
校招火热招聘中
官网直投
第二题纯模拟可过- -。
点赞
送花
回复 分享
发布于 2017-08-25 21:23
void dfs(vector<int>& signal,vector<bool> & mask, vector<vector<int> > &path, vector<vector<int> >& matrix, int nowNode, int T,int nowTime, int & minValue){ if(nowTime >= minValue) return; if(nowNode == T){ minValue = min(minValue, nowTime); return; } int waitTime = 0; if(nowTime / signal[nowNode] % 2 == 1) { waitTime = signal[nowNode] - nowTime % signal[nowNode]; } nowTime += waitTime; mask[nowNode] = true; for(int i=0;i<path[nowNode].size();i++){ if(!mask[path[nowNode][i]]){ nowTime += matrix[nowNode][path[nowNode][i]]; dfs(signal,mask,path,matrix,path[nowNode][i],T,nowTime,minValue); nowTime -= matrix[nowNode][path[nowNode][i]]; } } mask[nowNode] = false; } int minTravelTime(int N, vector < vector < int > > intersections, int M, vector < vector < int > > roads, int s, int t) { vector<bool> mask(N,false); vector<int> signal(N,0); for(int i=0;i<N;i++){ signal[intersections[i][0]] = intersections[i][1]; } vector<vector<int> > path(N); vector<vector<int> > matrix(N,vector<int>(N,0)); for(int i=0;i<M;i++){ path[roads[i][0]].push_back(roads[i][1]); path[roads[i][1]].push_back(roads[i][0]); matrix[roads[i][0]][roads[i][1]] = roads[i][2]; matrix[roads[i][1]][roads[i][0]] = roads[i][2]; } int result = 0x7fffffff; dfs(signal,mask,path,matrix,s,t,0,result); return result; }
点赞
送花
回复 分享
发布于 2017-08-25 21:27
第二题很奇怪啊,本地明明都是对的,但是系统就是0.0%。。。
点赞
送花
回复 分享
发布于 2017-08-25 21:27
上面两个题目的思路和Python源码,http://blog.csdn.net/u014606206/article/details/77596819
点赞
送花
回复 分享
发布于 2017-08-31 21:37
来来,有大神分享下思路或者代码吗?
点赞
送花
回复 分享
发布于 2017-08-25 21:22
0%,0%
点赞
送花
回复 分享
发布于 2017-08-25 21:22
Mark 求大佬解答。。
点赞
送花
回复 分享
发布于 2017-08-25 21:23
第二题10,4的不是测试案例么?通过了也是0%?
点赞
送花
回复 分享
发布于 2017-08-25 21:23
0%,0%
点赞
送花
回复 分享
发布于 2017-08-25 21:23
第二题没想明白,感觉没问题,但是通过率0%
点赞
送花
回复 分享
发布于 2017-08-25 21:24
AK
点赞
送花
回复 分享
发布于 2017-08-25 21:25
第一题标准dfs吧,只是每次通过路口的时候处理下时间
点赞
送花
回复 分享
发布于 2017-08-25 21:30
第二题的测试用例有20%是k在45~9000中间,40%在9000~1386450中间。别问我为什么知道的。。。
点赞
送花
回复 分享
发布于 2017-08-25 21:30
*** 我Java岗跟你的编程题一样..  
点赞
送花
回复 分享
发布于 2017-08-25 21:31
为什么我觉得我投的测试岗的题目比算法还难。。
点赞
送花
回复 分享
发布于 2017-08-25 21:34
m
点赞
送花
回复 分享
发布于 2017-08-25 21:34
//第二题 #include <stdio.h>   #include <math.h>   #include <stdlib.h>   int Get(int n){     int x;     // do something     int zuhao,kaishi;     zuhao=int(sqrt(2*n));     if ((zuhao*(zuhao+1)/2>=n)&&(zuhao*(zuhao-1)/2+1<=n)){     zuhao=zuhao-1; }     kaishi=zuhao*(zuhao+1)/2+1;     x=n-kaishi+1;     return x; } int main()   {       int n;     scanf("%d",&n);     int r = Get(n);              printf("%d\n",r);   }
点赞
送花
回复 分享
发布于 2017-08-25 21:34
//第一题完全没有思路,感觉好难,选择题也没做完
点赞
送花
回复 分享
发布于 2017-08-25 21:35

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务