当前位置:网站首页>leetcode:1314. 矩阵区域和【二维前缀和模板】
leetcode:1314. 矩阵区域和【二维前缀和模板】
2022-07-04 03:53:00 【白速龙王的回眸】

分析
二维前缀和模板
dp的时候多一行一列记录0方便统一式子
ac code
class Solution:
def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]:
# 二维前缀和
m, n = len(mat), len(mat[0])
# row and col 补充一行全0
dp = [[0] * (n + 1) for _ in range(m + 1)]
dp[1][1] = mat[0][0]
for j in range(2, n + 1):
dp[1][j] = mat[0][j - 1] + dp[1][j - 1]
for i in range(2, m + 1):
dp[i][1] = mat[i - 1][0] + dp[i - 1][1]
for i in range(2, m + 1):
for j in range(2, n + 1):
#print(i, j)
dp[i][j] = mat[i - 1][j - 1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
#print(dp)
res = [[0] * n for _ in range(m)]
for i in range(1, m + 1):
for j in range(1, n + 1):
left, right = max(i - k, 1), min(m, i + k)
up, down = max(j - k, 1), min(n, j + k)
res[i - 1][j - 1] = dp[right][down] + dp[left - 1][up - 1] - dp[left - 1][down] - dp[right][up - 1]
return res
总结
二维前缀和板子
边栏推荐
- 程序员远程办公喜忧参半| 社区征文
- dried food! Generation of rare samples based on GaN
- Boutique website navigation theme whole station source code WordPress template adaptive mobile terminal
- The difference between bagging and boosting in machine learning
- Idea modify body color
- 软件测试是干什么的 发现缺陷错误,提高软件的质量
- (指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
- 毕业设计:设计秒杀电商系统
- JS realizes the effect of text scrolling marquee
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
猜你喜欢

Apple CMS imitation watermelon video atmospheric response video template source code

ModStartBlog 现代化个人博客系统 v5.2.0 源码下载

2021 RSC | Drug–target affinity prediction using graph neural network and contact maps

软件测试是干什么的 发现缺陷错误,提高软件的质量

R语言中如何查看已安装的R包

微信公众号无限回调授权系统源码

96% of the collected traffic is prevented by bubble mart of cloud hosting

普源DS1000Z系列数字示波器在通信原理实验中的应用方案
Tcp- simple understanding of three handshakes and four waves

2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
随机推荐
JS实现文字滚动 跑马灯效果
Leetcode brush question: binary tree 06 (symmetric binary tree)
Programmers' telecommuting is mixed | community essay solicitation
Emlog用户注册插件 价值80元
线程常用的方法
What does software testing do? Find defects and improve the quality of software
沃博联结束战略评估,决定保留表现优异的博姿业务
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
RHCSA 03 - 文件的基础权限
Apple CMS imitation watermelon video atmospheric response video template source code
y55.第三章 Kubernetes从入门到精通 -- HPA控制器及metrics-server(二八)
Virtual commodity account trading platform source code_ Support personal QR code collection
R语言中如何查看已安装的R包
【微服务|openfeign】@FeignClient详解
RHCSA 06 - suid, sgid, sticky bit(待补充)
Pointer array and array pointer
Modstartblog modern personal blog system v5.2.0 source code download
leetcode刷题:二叉树06(对称二叉树)
hbuildx中夜神模拟器的配置以及热更新
Interpretation of leveldb source code skiplist