查看: 4636| 回复: 36
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] [第二轮] 2/18-2/24 CareerCup 1.7

全局:

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

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

x
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.

发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

评分

参与人数 1大米 +10 收起 理由
Kimurate + 10

查看全部评分


上一篇:[第二轮] 2/18-2/24 CareerCup 1.6
下一篇:Programming Interviews Exposed 下载地址
🔗
yang_cs 2013-2-17 19:33:42 | 只看该作者
全局:
Can you explain this problem in detail? I mean, it just requires that an algorithm can achieve this operation? Does this operation have the transitivity?  
回复

使用道具 举报

🔗
lvchaoshuai 2013-2-17 21:07:16 | 只看该作者
全局:
yang_cs 发表于 2013-2-17 19:33
Can you explain this problem in detail? I mean, it just requires that an algorithm can achieve this  ...

If this operation have the transitivity, all elements will be zero= =...

So I think you just implement an algorithm to simulate this operation.
回复

使用道具 举报

🔗
jimwallet 2013-2-18 03:31:06 | 只看该作者
全局:
check which rows and columns are affected. Then set all elements are at those rows or columns.

https://gist.github.com/njcongtou/4972963
回复

使用道具 举报

🔗
grassgigi 2013-2-18 07:07:05 | 只看该作者
全局:
use bitmap to record which positions should be set zero

https://gist.github.com/chrislukkk/4973969
回复

使用道具 举报

🔗
ryancooper 2013-2-18 07:17:48 | 只看该作者
全局:
jimwallet 发表于 2013-2-18 03:31
check which rows and columns are affected. Then set all elements are at those rows or columns.

ht ...

Nice. Your idea is similar with ls's, both of you want to record which rows and columns are affected, but your algorithm will run much faster than his.
回复

使用道具 举报

🔗
ryancooper 2013-2-18 07:26:03 | 只看该作者
全局:
本帖最后由 ryancooper 于 2013-2-18 07:28 编辑
grassgigi 发表于 2013-2-18 07:07
use bitmap to record which positions should be set zero

https://gist.github.com/chrislukkk/497396 ...

Your idea is nice, but you do a lot of redundant works. In your code, you unnecessarily record too much information. For example, in your "traverse all elements in matrix" part, when you find out that mat[j] is 0, you don't have to set your whole ith row and jth column of your bitmap into zero, because all we need to do is to remember that ith row and jth column of the original matrix are need to be set to 0. So you only need to two 1-dimension array, one is for row information, the other is for column's. That is OK, keep in mind that by using bitmap we should increase efficiency dramatically, usually results in having an algorithm runs in lieanr time. Otherwise, there is no need to use it, right.
For the detail implementation, you can refer to jimwallet's code.
回复

使用道具 举报

🔗
grassgigi 2013-2-18 07:26:06 | 只看该作者
全局:
ryancooper 发表于 2013-2-18 07:17
Nice. Your idea is similar with ls's, both of you want to record which rows and columns are affect ...

ye, one dimensional arrays for col and row are neat and elegant, i didn't come up with this thought. Nice!
回复

使用道具 举报

🔗
cqx83 2013-2-18 08:26:49 | 只看该作者
全局:
https://gist.github.com/cqx83/4974349
O(mn)
Need to traverse the whole matrix twice.  Record which rows and columns should be set to 0 in the first time. We need two extra arrays.
And then set all the elements which should be zero to 0.
回复

使用道具 举报

🔗
mazl123321 2013-2-18 09:31:59 | 只看该作者
全局:
1. have row and col flag to mark the zeros
2. the 2nd pass will set zero for flaged rows and cols
https://gist.github.com/mazl123321/4974587
回复

使用道具 举报

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

本版积分规则

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