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

狗家电面

🔗
ysboss123 2019-9-27 11:51:59 | 只看该作者
全局:
虎蝇孑 发表于 2019-9-27 11:37
我觉得那个代码应该停的下来的 base check的时候出边界或者等于0就停了,但是我感觉时间复杂度有点高 是 ...

嗯 复杂度应该是这个。 太高了,可以讨论下怎么优化。。。
回复

使用道具 举报

🔗
ysboss123 2019-9-27 11:53:55 | 只看该作者
全局:
jjames 发表于 2019-9-27 11:31
谢谢你提供的代码,感觉还是有些问题,没有visited status,感觉dfs会无线进行下去。
加上visited statu ...

可以停下的,每走一步都把这步改位0,backtrack的时候再赋值回原值。 就是复杂度太高了
回复

使用道具 举报

🔗
reliveinfire 2019-9-27 15:36:04 | 只看该作者
全局:
关建在于没有环, 没有环 可以视为一个tree, 所以就是max tree path sum即可.
回复

使用道具 举报

🔗
onewaymyway 2019-9-27 16:44:19 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
细雨微张 2019-9-27 23:17:37 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
细雨微张 2019-9-28 00:05:32 | 只看该作者
全局:
参考LC124写了一个O(m*n)的解,用楼主给的两个例子似乎都是对的

  1. int helper(vector<vector<int>>& matrix, vector<vector<bool>>& visited, int row, int col, int& res) {
  2.   int m = matrix.size(), n = matrix[0].size();
  3.   if (row < 0 || row >= m || col < 0 || col >= n || matrix[row][col] == 0 || visited[row][col]) return 0;
  4.   visited[row][col] = true;
  5.   int left = helper(matrix, visited, row, col - 1, res);
  6.   int right = helper(matrix, visited, row, col + 1, res);
  7.   int up = helper(matrix, visited, row - 1, col, res);
  8.   int down = helper(matrix, visited, row + 1, col, res);
  9.   vector<int> tmp = {left, right, up, down};
  10.   sort(tmp.begin(), tmp.end());
  11.   res = max(res, matrix[row][col] + tmp[3] + tmp[2]); // get the largest two directions
  12.   return matrix[row][col] + tmp[3];
  13. }

  14. int maxPath(vector<vector<int>>& matrix) {
  15.   int m = matrix.size(), n = matrix[0].size();
  16.   vector<vector<int>> connectedRegion(m, vector<int>(n, 0));
  17.   vector<vector<bool>> visited(m, vector<bool>(n, false));
  18.   int res = 0; // does not need to consider negative value
  19.   for (int i = 0; i < m; ++i) {
  20.     for (int j = 0; j < n; ++j) {
  21.       if (matrix[i][j] != 0 && !visited[i][j]) {
  22.         helper(matrix, visited, i, j, res);
  23.       }
  24.     }
  25.   }

  26.   return res;
  27. }

  28. int main(){
  29.   vector<vector<int>> test1 = { {5,4,1,3}, {1,0,1,0}, {3,0,0,4}, {0,1,2,0} };
  30.   assert(maxPath(test1) == 17);
  31.   vector<vector<int>> test2 = { {5,4,1,0}, {1,0,1,2}, {3,0,0,4}, {0,1,2,0} };
  32.   assert(maxPath(test2) == 21);
  33.   return 0;
  34. }
复制代码
回复

使用道具 举报

🔗
虎蝇孑 2019-9-28 00:31:04 | 只看该作者
全局:
细雨微张 发表于 2019-9-27 23:17
复杂度应该是远远高于O(m^2n^2)的。O(m2n2)的意思是以矩阵中的任意一个元素作为出发点,然后遍历所有的矩 ...

啊 好像是 谢谢解释
回复

使用道具 举报

🔗
laura9 2019-9-28 00:50:01 | 只看该作者
全局:
dfs找岛,每个岛构造一个三叉树来求max path sum
回复

使用道具 举报

🔗
Hanna097 2019-9-28 04:23:38 | 只看该作者
全局:
怎么感觉有些电面很简单有些很难呢emm
回复

使用道具 举报

🔗
savage811 2019-9-28 04:37:06 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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