目标值子矩阵的数量

目标值子矩阵的数量

问题描述

小M最近在研究矩阵,他对矩阵中的子矩阵很感兴趣。给定一个矩阵 matrix 和一个目标值 target,他的任务是找到所有总和等于目标值的非空子矩阵的数量。子矩阵通过选择矩阵的某个矩形区域定义,形式为 (x1, y1, x2, y2),其中 (x1, y1) 表示左上角的坐标,(x2, y2) 表示右下角的坐标。一个子矩阵包含矩阵中所有位于这个矩形区域内的单元格。如果两个子矩阵的坐标不同(如 x1 != x1' 或 y1 != y1'),则这两个子矩阵被认为是不同的。

你需要返回满足条件的子矩阵数量。

测试样例

样例1:

输入:matrix = [[-1,1,0], [1,1,1], [0,1,0]] ,target = 0

输出:7

样例2:

输入:matrix = [[-1,-1], [-1,1]] ,target = 0

输出:2

样例3:

输入:matrix = [[-1,2,3], [4,5,6], [7,8,9]] ,target = 10

输出:2

题解

二维前缀和,枚举左上角的点和右下角的点。时间复杂度O(n^2 * m^2)

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;

int solution(vector<vector<int>>& matrix, int target) {
    // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
    // write code here
    int n = matrix.size();
    int m = matrix[0].size();
    vector<vector<int>> ans(n+1, vector<int>(m+1, 0));
    int res = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ans[i+1][j+1] = matrix[i][j] + ans[i][j+1] + ans[i+1][j] - ans[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int r = i ; r < n; r++) {
                for (int c = j ; c < m; c++) {
                    int tmp = ans[r+1][c+1] - ans[r+1][j] - ans[i][c+1] + ans[i][j];
                    if (target == tmp) {
                        res++;
                    }
                }
            }
        }
    }
 
    return res;
}
cpp 复制代码
优化枚举左上点和右下点的枚举。使用前缀和+ 哈希表进行优化。
c++ 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

int solution(vector<vector<int>>& matrix, int target) {
    // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
    // write code here
    int n = matrix.size();
    int m = matrix[0].size();
    vector<vector<int>> ans(n+1, vector<int>(m+1, 0));
    int res = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ans[i+1][j+1] = matrix[i][j] + ans[i][j+1] + ans[i+1][j] - ans[i][j];
        }
    } 
    // top代表左上点的行号
    for (int top = 0; top< n; top++) {
        // bottom 代表右下点的行号
        for (int bottom = top; bottom < n; bottom ++) {
            // 存储前缀和的数量
            unordered_map<int, int> sumCount;
            sumCount[0] = 1;
            for (int col = 0; col < m; col++) {
                // 这里计算出来的就是[top][col] - [bottom][col]的值
                int currentSum = ans[bottom+1][col+1] - ans[top][col+1];
                // prefix[i−1]=prefix[j]−target 这个公式的提现
                if (sumCount.find(currentSum-target) != sumCount.end()) {
                    res += sumCount[currentSum-target];
                }
                sumCount[currentSum]++;
            }
        }
    }
    return res;
}
相关推荐
EPSDA4 小时前
Linux命名管道与共享内存
linux·运维·服务器·开发语言·c++
小仇学长5 小时前
嵌入式八股文面试题(二)C语言算法
c语言·算法·八股文
因兹菜6 小时前
[LeetCode]day21 15.三数之和
数据结构·算法·leetcode
编程就是如此7 小时前
LeetCode Hot100(持续更新中)
算法·leetcode
萌の鱼7 小时前
leetcode 2466. 统计构造好字符串的方案数
数据结构·c++·算法·leetcode
Yolowuwu8 小时前
算法跟练第十一弹——二叉树
java·算法·leetcode
TwilightLemon8 小时前
C++ 使用MIDI库演奏《晴天》
c++
云边有个稻草人8 小时前
AI语言模型的技术之争:DeepSeek与ChatGPT的架构与训练揭秘
人工智能·算法·语言模型·chatgpt·deepseek
bbqz0079 小时前
浅说 c++20 cppcoro (三)
c++·c++20·协程·coroutine·co_await·co_yield·cppcoro·co_return
生信宝典9 小时前
机器学习算法 - 随机森林之决策树初探(1)
算法·随机森林·机器学习