当前位置:网站首页>leetcode 1074. Number of Submatrices That Sum to Target(和为target的子矩阵个数)
leetcode 1074. Number of Submatrices That Sum to Target(和为target的子矩阵个数)
2022-07-23 02:53:00 【蓝羽飞鸟】

题目中让找出有多少个子矩阵,其元素的和为target.
子矩阵的范围:x1 <= x <= x2, y1 <= y <= y2
4个边界有任一个不同,都算不同的子矩阵。
思路:
有一种简单粗暴的方法,就是遍历所有的x1, x2, y1, y2,
用积分矩阵求和。
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
// aux[i][j] is the sum of sub-matrix which start at [0][0] and end in [i][j]
int[][] aux = new int[m + 1][n + 1]; // padding on top and left
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
aux[i][j] = matrix[i - 1][j - 1] + aux[i - 1][j] + aux[i][j - 1] - aux[i - 1][j - 1];
}
}
int res = 0;
// try each sub-matrix
for (int x1 = 1; x1 < m + 1; x1++) {
for (int y1 = 1; y1 < n + 1; y1++) {
for (int x2 = x1; x2 < m + 1; x2++) {
for (int y2 = y1; y2 < n + 1; y2++) {
if (target == aux[x2][y2] - aux[x2][y1 - 1] - aux[x1 - 1][y2] + aux[x1 - 1][y1 - 1])
res++;
}
}
}
}
return res;
}
上面的方法能通过,但是比较慢。时间上排名比较靠后。
找到了下面这种方法,
可以想象一下行的上边界是top, 下边界是bottom
然后在top~bottom这些行的范围内,一条一条(一列一列)地求和,
和保存在sum数组里,sum[col] 表示 col这一列top~bottom范围的和。
这样子矩阵的上下边界确定好了,再确定左右边界,
只需要求left ~ right范围内sum[col]的和就行了,每个sum[col]是一条,多条组合起来就是一个子矩阵。
一旦和满足==target,就算找到一个子矩阵。
遍历top : 1~m-1, bottom : top~m-1, left : 0 ~n-1, right: left ~ n-1
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int res = 0;
// traverse upper boundary
for (int top = 0; top < m; top++) {
// for each upper boundary, we have a prefix sum array
int[] sum = new int[n];
// traverse lower boundary
for (int bottom = top; bottom < m; bottom++) {
// count the prefix sum for each column
for (int col = 0; col < n; col++) {
sum[col] += matrix[bottom][col];
}
// traverse left and right boundary
for (int left = 0; left < n; left++) {
int cnt = 0;
for (int right = left; right < n; right++) {
cnt += sum[right];
if (cnt == target) res++;
}
}
}
}
return res;
}
边栏推荐
- 亿级流量下的分布式锁优化方案!太好用了~
- 剑指 Offer II 031. 最近最少使用缓存
- 南京银行提前批金融科技岗
- Is it safe for CITIC futures to open an account online and will it be cheated?
- 中信期货网上开户是否安全,网上开户会被骗吗?
- PHP script paging TXT content case
- This is how the permission system is designed, yyds
- Interviewer: explain the core principle of ThreadLocal
- 凌晨两点,你们都在卷什么?
- Multi-UA V Cooperative Exploringfor the Unknown Indoor EnvironmentBased on Dynamic Target Tracking翻译
猜你喜欢

系统安全测试要怎么做,详细来说说

百度地图鹰眼轨迹服务

I changed my career as a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings

用现代化的开发方法和思维,打跑遗留系统“拦路虎”

BGP反射器,联邦,选路规则

检测Windows安全缺陷工具wesng的学习与使用

CANopen通信----PDO与SDO

ACM SIGIR 2022 | 美团技术团队精选论文解读

实现方法pathListToMap,能够将输入的pathList转化为具有层级结构的map类型

This is how the permission system is designed, yyds
随机推荐
解密 Redis 助力双 11 背后电商秒杀系统
Use recursive string inversion and Full Permutation
I changed my career as a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings
亿级融资事件占比超30%,超自动化的下一站是何处?丨曼孚科技
hdu-7141 Ball (bitset)
Visual full link log tracking
Want to give up software testing, 4 years of experience to interview 10 minutes, the test is now so difficult?
笔记——记录@RefreshScope动态刷新配置失效的解决方式
es6相关面试题3
.split(“,“, -1) 和 .split(“,“) 的区别
逆向理论知识1
实现多层级条件查询(类似京东多层级添加查询)
剑指 Offer II 031. 最近最少使用缓存
海通证券场内基金开户怎么样安全吗
Comprehensive summary of software quality management practice
Liunx上杀死一进程
十年磨一剑,云原生分布式数据库PolarDB-X的核心技术演化
平台型企业如何解决分账核算业务呢?
What level is GF futures? Is the account opening safe and reliable?
[notes on the practice of node middle tier (I)] -- building the project framework