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

[找工就业] 请问一道谷歌的onsite面经

全局:

2020(1-3月)-CS硕士+1-3年 | 内推| 码农类General全职@google

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

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

x
给一个0和1的矩阵, 求出由1组成的rectangle的数量。.--
0,1,1. .и
0,1,1. 1point3acres.com
1,0,0
这个例子的话答案是:10个
因为1x1 矩阵有 5
1x2 矩阵有2
2x1 矩阵有2
2x2 矩阵有1. check 1point3acres for more.

之前有道简单的小题,让你求出square的数量,这个答案就是6. 我用dp做的
除了brute force 应该有一些优化的做法。. From 1point 3acres bbs
注:把他看做histogram来解应该不是最优解,因为我是这么做的结果被挂了

求大神解释

上一篇:ux intern被亚麻waitlist,但是linkedin上职位还一直开着
下一篇:跨领域实习offer比较:AI@G家大药厂 VS Device test engineer@WDC
推荐
Actuant 2020-3-28 16:19:41 | 只看该作者
全局:
Code在下面,Comment里写了思路,跟LC85是挺像的,就是Hist处理部分计算不一样。时间O(n^2),空间O(n).

  1. from typing import List

  2. class Solution:
  3.     def calc_matrix_rectangles(self, matrix: List[List[str]]) -> int:
  4.         if not matrix or not matrix[0]:
  5.             return 0
  6.         ans = 0
  7.         m, n = len(matrix), len(matrix[0])
  8.         heights = [0] * n
  9.         for i in range(m):
  10.             for j in range(n):
  11.                 if matrix[i][j] == 0:
  12.                     heights[j] = 0
  13.                 else:
  14.                     heights[j] += 1. From 1point 3acres bbs
  15.             ans += self.calc_hist_rectangles(heights)
  16.         return ans


  17.     def calc_hist_rectangles(self, heights: List[int]) -> int:
  18.         # calculates the number of rectangles with base at bottom
  19.         # e.g. heights = [2,0,3,2,1,2,4]
  20.         #
  21.         # the visualization below may be distorted when posted (but you get the idea).google  и
  22.         #             x
  23.         #     x       x
  24.         # x   x x   x x
  25.         # x   x x x x x
  26.         #
  27.         # ans = 2 + 0 + 3 + (2 + 2) + (1 + 1 + 1) + (1 + 1 + 1 + 2) + (1 + 1 + 1 + 2 + 4)
  28.         #       = 26

  29.         ans = curr_sum = heights[0]
  30.         l = 0

  31.         for r in range(1, len(heights)):
  32.             # starting a new round
  33.             if heights[r] == 0:
  34.                 curr_sum = 0
  35.                 l = r + 1
  36.                 continue ..

  37.             # the rectangles extending to the left now have a lower height limit
  38.             if heights[r] < heights[r-1]:. ----
  39.                 curr_sum = (r - l) * heights[r]

  40.             # calculates the number of rectangles added when we reach at index r
  41.             curr_sum += heights[r]. check 1point3acres for more.
  42.             ans += curr_sum

  43.         return ans

  44. if __name__ == '__main__':
  45.     s = Solution()
  46.     matrix = [[0,1,1],[0,1,1],[1,0,0]].1point3acres
  47.     print(s.calc_matrix_rectangles(matrix))
复制代码
回复

使用道具 举报

推荐
clark.li86 2020-3-28 17:56:40 | 只看该作者
全局:
Leetcode 1277 DP.
回复

使用道具 举报

推荐
Jimmy123 2020-3-28 13:48:28 | 只看该作者
全局:
yuw485 发表于 2020-3-28 09:30
leetcode 85题原题。如果你的Histogram 算法是1维那就是最优算法。

补充内容 (2020-3-27 18:31):

更像是 1074. Number of Submatrices That Sum to Target
. Waral dи,
Histogram 算法具体指的是什么
回复

使用道具 举报

🔗
yuw485 2020-3-28 09:30:13 来自APP | 只看该作者
全局:
leetcode 85题原题。如果你的Histogram 算法是1维那就是最优算法。
.--
补充内容 (2020-3-27 18:31):
哦看错了不好意思不是一道题嘻嘻
回复

使用道具 举报

全局:
感觉用Histogram的想法可以解,时间O(n^2), 空间O(n)
回复

使用道具 举报

🔗
telegramatic 2020-3-28 16:05:00 | 只看该作者
全局:
应该是histogram的做法,lz可能是面试时的算法计算错了,有重复统计
回复

使用道具 举报

🔗
mchzh 2020-3-29 00:51:55 | 只看该作者
全局:
只要是不给最优解就挂吗
回复

使用道具 举报

🔗
hugefacecat 2020-3-29 01:03:44 | 只看该作者
全局:
本帖最后由 hugefacecat 于 2020-3-29 01:12 编辑

能说说你的DP具体怎么implement的吗?这题也不可能比O(n^2) time更优了吧? space 可能可以reduce成O(1).
回复

使用道具 举报

🔗
hugefacecat 2020-3-29 01:57:16 | 只看该作者
全局:

好像可以把这道square题里的dp[i][i,j] = (width, top_continue, left_continue)拓展成 (width, height, top_continue, left_continue)然后用长宽的min做乘法?[/i]
回复

使用道具 举报

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

本版积分规则

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