回复: 24
跳转到指定楼层
上一主题 下一主题
收起左侧

骨骼蠡口分类整理追加单调队列

 
全局:

2020(7-9月) 码农类General 硕士 全职@google - 网上海投 - 技术电面  | | Other | 在职跳槽

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

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

x
骨骼面试有一类比较Tricky的题目,即单调队列 Monotonic Queue / 单调栈 Monotonic Stack
通过单调队列,可以把复杂度降到O(n),主要覆盖Stack, Sliding Window, Two Pointers, Linked List/Queue, HashTable/HashMap等算法和数据结构,分类参考主贴


G家6个月内
LC42. Trapping Rain Water
LC84. Largest Rectangle in Histogram (单调递增
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
e OOD + 单调递增栈)
LC907. Sum of Subarray Minimums, 微软
LC1019. Next Greater Node In Linked List, FB 亚麻

后面会把做完的题跟贴。抛砖引玉,希望各路高手能把好理解、好记的解法贴出来。

评分

参与人数 13大米 +17 收起 理由
showton + 2 给你点个赞!
wyu51 + 1 很有用的信息!
zywu89 + 1 给你点个赞!
lc331 + 3 很有用的信息!
louise + 2 很有用的信息!

查看全部评分


上一篇:亚麻有声书VO
下一篇:Expedia OA
推荐
weii 2020-7-15 14:33:51 | 只看该作者
全局:
1499应该也归属于这一类吧

评分

参与人数 1大米 +1 收起 理由
yangzhit + 1 谢谢分享!

查看全部评分

回复

使用道具 举报

推荐
 楼主| yangzhit 2020-7-14 18:31:04 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies

本帖子中包含更多资源

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

x
回复

使用道具 举报

推荐
__jind 2020-7-13 07:40:50 | 只看该作者
全局:
yangzhit 发表于 2020-7-12 16:55
谢谢,不确定。如果有相应解法或链接,麻烦贴出来
  1. class Solution:
  2.     def numSubmat(self, mat) -> int:
  3.         '''
  4.             1 0 1
  5.             1 1 0
  6.             1 1 0

  7.               ↓ use prefix sum by column

  8.             1 0 1
  9.             2 1 0
  10.             3 2 0

  11.         calculate the prefix sum, like:
  12.         
  13.                 h: [2 2 4 3 1]
  14.             index:  0 1 2 3 4
  15.         
  16.         
  17.         stack = [(-1, 0)]
  18.         stack.append (i, h[i]) when it is increasing.
  19.         Once you find h[3] = 3 which is not increasing,
  20.             you pop items from stack (i, h[i]),
  21.             until the item on the top of the stack has a lower height
  22.         
  23.         this property means that:
  24.             
  25.             
  26.         # of rectangles created by the popped column =
  27.             [cur(i) - popped(i)]
  28.             * [popped(i) - stack[-1][0]]   
  29.             # 左边包含 popped column 的 能组成正方形的长度 * 右边... (左边和右边,包含 popped column 的 所有组合)
  30.             * h[popped(i)]
  31.             
  32.         '''
  33.         for row in mat:
  34.             row.append(0)  
  35.         
  36.         col = len(mat[0])
  37.         res = 0
  38.         h = [0 for _ in range(col)]
  39.         
  40.         for row in mat:
  41.             stack = [(-1, 0)]
  42.             
  43.             for i in range(col):
  44.                
  45.                 h[i] = h[i] + 1 if row[i] == 1 else 0 # calculate the height  
  46.                
  47.                 while stack and len(stack) > 1 and h[stack[-1][0]] > h[i]: #  and len(stack) > 1 是因为有可能 h[stack[-1][0]] 是 h[-1] 就错了
  48.                     popped_i, popped_h = stack.pop()
  49.                     res += (i - popped_i) * (popped_i - stack[-1][0]) * popped_h
  50.                
  51.                 stack.append((i, h[i]))
  52.             
  53.         return res         
  54.         
  55.         
  56.         
  57.         
  58.     def numSubmat___(self, mat) -> int:
  59.         '''
  60.             1 0 1
  61.             1 1 0
  62.             1 1 0

  63.               ↓ use prefix sum by column

  64.             1 0 1
  65.             2 1 0
  66.             3 2 0

  67.         calculate the prefix sum, like:
  68.         
  69.                 h: [2 2 4 3 1]
  70.             index:  0 1 2 3 4
  71.             
  72.         # of rectangles created by the cur column =
  73.             (the index of the cur column - the index of the previous column with lower height)
  74.             * the height of cur column
  75.             + the submatries owing to the previous column with lower height
  76.         
  77.         we store the # of rectangle with the index, like: (index, # of rectangle of the cur column)
  78.             
  79.         Once you find h[3] = 3 which is not increasing,
  80.             you pop items from stack (i, previous rectangle),
  81.             until the item on the top of the stack has a lower height
  82.             


  83.         '''
  84.         col = len(mat[0])
  85.         res = 0
  86.         h = [0 for _ in range(col)]
  87.         
  88.         for row in mat:
  89.             stack = [(-1, 0)]
  90.             
  91.             for i in range(col):
  92.                
  93.                 h[i] = h[i] + 1 if row[i] == 1 else 0 # calculate the height  
  94.                
  95.                 while stack and len(stack) > 1 and h[stack[-1][0]] > h[i]:
  96.                     stack.pop()
  97.                
  98.                 # the rectangles created by cur column
  99.                 width = i - stack[-1][0]
  100.                 plus = stack[-1][1]
  101.                 cur = width*h[i] + plus
  102.                
  103.                 stack.append((i, cur))
  104.                 res += cur
  105.             print(h)
  106.         return res
复制代码

评分

参与人数 1大米 +1 收起 理由
yangzhit + 1 赞一个!

查看全部评分

回复

使用道具 举报

🔗
__jind 2020-7-12 04:47:43 来自APP | 只看该作者
全局:
加个1504
回复

使用道具 举报

🔗
 楼主| yangzhit 2020-7-12 16:55:04 | 只看该作者
全局:

谢谢,不确定。如果有相应解法或链接,麻烦贴出来
回复

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

🔗
 楼主| yangzhit 2020-7-13 04:38:57 | 只看该作者
全局:
46, 84, 862三道Hard先跳过,有好的解法,麻烦贴出来,解释一下
回复

使用道具 举报

🔗
 楼主| yangzhit 2020-7-13 05:10:22 | 只看该作者
全局:
更正 901. Online Stock Span 是G家的题,还是7月新题
回复

使用道具 举报

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

使用道具 举报

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

本版积分规则

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