目标值子矩阵的数量

目标值子矩阵的数量

问题描述

小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;
}
相关推荐
ShineWinsu4 小时前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
数模竞赛Paid answer5 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆5 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
Lzg_na5 小时前
constexpr的优势
c++
想吃火锅10057 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
王维同学7 小时前
进程模块枚举、映像身份与线程启动地址关联
c++·windows·安全
hold?fish:palm7 小时前
24 回文链表
数据结构·c++·链表
举手7 小时前
Dispatcher模块剖析
linux·c++
Nil2087 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣8 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论