车版热帖:大家对买豪车怎么看

一亩三分地论坛

 找回密码
 Sign Up 注册获取更多干货
码农求职神器Triplebyte:
不用海投,内推多家公司面试
[Google级团队]:实时大数据分析领域践行者
北京/深圳-大数据/搜索/机器学习职位
日志易机器大数据行业践行者Web/大数据/机器学习等职位-北京or深圳
把贵司招聘信息放这里
查看: 3386|回复: 2
收起左侧

[算法题] 分享一个codility的题

[复制链接] |试试Instant~ |关注本帖
zenw 发表于 2014-12-11 09:49:14 | 显示全部楼层 |阅读模式

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

您需要 登录 才可以下载或查看,没有帐号?Sign Up 注册获取更多干货

x
现在很多公司用codility 面试

A rectangular map consisting of N rows and M columns of square areas
is given. Each area is painted with a certain color.
Two areas on the map belong to the same country if the following
conditions are met:
they have the same color;
it is possible to travel from one area to the other by moving only
north, south, west or east without moving over areas of a different
color.
The map can be described by a zero-indexed matrix consisting of N rows
and M columns of integers. The color of each area is described by the
corresponding element of the matrix. Two areas have the same color if
and only if their corresponding matrix elements have the same value.
For example, consider the following matrix A consisting of seven rows
and three columns:
A[0][0] = 5    A[0][1] = 4    A[0][2] = 4
A[1][0] = 4    A[1][1] = 3    A[1][2] = 4
A[2][0] = 3    A[2][1] = 2    A[2][2] = 4
A[3][0] = 2    A[3][1] = 2    A[3][2] = 2
A[4][0] = 3    A[4][1] = 3    A[4][2] = 4
A[5][0] = 1    A[5][1] = 4    A[5][2] = 4
A[6][0] = 4    A[6][1] = 1    A[6][2] = 1
Matrix A describes a map that is colored with five colors. Areas on
the map belong to eleven different countries:
area A[0][0] forms a one-area country;
areas A[0][1], A[0][2], A[1][2], A[2][2] belong to the same country;
area A[1][0] forms a one-area country;
area A[1][1] forms a one-area country;
area A[2][0] forms a one-area country;
areas A[2][1], A[3][0], A[3][1], A[3][2] belong to the same country;
areas A[4][0], A[4][1] belong to the same country;
areas A[4][2], A[5][1], A[5][2] belong to the same country;
area A[5][0] forms a one-area country;
area A[6][0] forms a one-area country;
areas A[6][1], A[6][2] belong to the same country.
Write a function
int countries_count(const vector< vector<int> > &A);
that, given a zero-indexed matrix A consisting of N rows and M columns
of integers, returns the number of different countries that the areas
of the map described by matrix A belong to.
Assume that:
N is an integer within the range [1..1,000,000];
M is an integer within the range [1..1,000,000];
the number of elements in matrix A is within the range [1..1,000,000];
each element of matrix A is an integer within the range
[-1,000,000,000..1,000,000,000].
For example, given matrix A consisting of seven rows and three columns
corresponding to the example above, the function should return 11.
Complexity:
expected worst-case time complexity is O(N*M);
expected worst-case space complexity is O(N*M).


评分

1

查看全部评分

hans3 发表于 2015-5-23 04:30:45 | 显示全部楼层
codility 的题目描述都折磨老长吗? 属于 leetcode 上什么等级的难度?
有大神很推荐它的 training list:
http://codesays.com/
回复 支持 反对

使用道具 举报

dwl1222 发表于 2015-5-30 04:06:41 | 显示全部楼层
看起来麻烦。其实不难。dfs就好了。
没浏览一个国家 count++; 然后标记这个国家的数字 Integer.min_value.最后返回count
回复 支持 反对

使用道具 举报

本版积分规则

提醒:发帖可以选择内容隐藏,部分板块支持匿名发帖。请认真读完以下全部说明:

■隐藏内容方法: [hide=200]你想要隐藏的内容比如面经[/hide]
■意思是:用户积分低于200则看不到被隐藏的内容
■可以自行设置积分值,不建议太高(200以上太多人看不到),也不建议太低(那就没必要隐藏了)
■建议只隐藏关键内容,比如具体的面试题目、涉及隐私的信息,大部分内容没必要隐藏。
■微信/QQ/电子邮件等,为防止将来被骚扰甚至人肉,以论坛私信方式发给对方最安全。
■匿名发帖的板块和方法:http://www.1point3acres.com/bbs/thread-405991-1-1.html

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2018-4-23 21:47

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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