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

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

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

使用道具 举报

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

评分

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

查看全部评分

回复

使用道具 举报

🔗
wanli 2020-7-13 06:37:22 来自APP | 只看该作者
全局:
这才是值得加分的精华帖子
回复

使用道具 举报

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

评分

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

查看全部评分

回复

使用道具 举报

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

使用道具 举报

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

评分

参与人数 1大米 +1 收起 理由
yangzhit + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
__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 赞一个!

查看全部评分

回复

使用道具 举报

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

使用道具 举报

🔗
 楼主| 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
回复

使用道具 举报

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

使用道具 举报

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

本版积分规则

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