一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 1647|回复: 2
收起左侧

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

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

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

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

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
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

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

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-11 03:35

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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