LeetCode 2373. 矩阵中的局部最大值

给你一个大小为 n x n 的整数矩阵 grid 。

生成一个大小为 (n - 2) x (n - 2) 的整数矩阵 maxLocal ,并满足:

maxLocal[i][j] 等于 grid 中以 i + 1 行和 j + 1 列为中心的 3 x 3 矩阵中的 最大值 。
换句话说,我们希望找出 grid 中每个 3 x 3 矩阵中的最大值。

返回生成的矩阵。

示例 1:
在这里插入图片描述
输入:grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
输出:[[9,9],[8,6]]
解释:原矩阵和生成的矩阵如上图所示。
注意,生成的矩阵中,每个值都对应 grid 中一个相接的 3 x 3 矩阵的最大值。

n == grid.length == grid[i].length
3 <= n <= 100
1 <= grid[i][j] <= 100

法一:直接遍历即可,时间复杂度O(n 2 ^2 2),空间复杂度O(1):

class Solution {
public:
    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
        vector<vector<int>> ret;
        int rowNum = grid.size();
        int colNum = grid[0].size();

        for (int i = 0; i < rowNum - 2; ++i) {
            ret.emplace_back();
            for (int j = 0; j < colNum - 2; ++j) {
                int max = 0;

                int rowStart = i;
                int rowEnd = i + 2;
                int colStart = j;
                int colEnd = j + 2;
                for (int curRow = rowStart; curRow <= rowEnd; ++curRow) {
                    for (int curCol = colStart; curCol <= colEnd; ++curCol) {
                        if (grid[curRow][curCol] > max) {
                            max = grid[curRow][curCol];
                        }
                    }
                }

                ret[i].push_back(max);
            }
        }

        return ret;
    }
};

法二:法一过程中有些数值是被重复计算的,我们可以先算出所有三行一列的单元中最大值,每次只取这三个单元的最大值的最大值即可,但这样做时间复杂度还是O(n 2 ^2 2),只有常数级优化,空间复杂度也增长到O(n 2 ^2 2),只有对时间极度敏感且空间较充足时才有意义:

class Solution {
public:
    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
        vector<vector<int>> ret;
        int rowNum = grid.size();
        int colNum = grid[0].size();

        vector<vector<int>> tmpMax;
        for (int tmpRow = 0; tmpRow < rowNum - 2; ++tmpRow) {
            tmpMax.emplace_back();
            for (int tmpCol = 0; tmpCol < colNum; ++tmpCol) {
                int unitMax = max(grid[tmpRow][tmpCol], 
                                  max(grid[tmpRow + 1][tmpCol], 
                                      grid[tmpRow + 2][tmpCol]));
                tmpMax[tmpRow].push_back(unitMax);
            }
        }

        for (int i = 0; i < rowNum - 2; ++i) {
            ret.emplace_back();
            for (int j = 0; j < colNum - 2; ++j) {
                int rowIndex = i;
                int colIndex = j;
                int maxNum = max(tmpMax[rowIndex][colIndex], 
                                 max(tmpMax[rowIndex][colIndex + 1], 
                                     tmpMax[rowIndex][colIndex + 2]));
                
                ret[i].push_back(maxNum);
            }
        }

        return ret;
    }
};

法三:法二中,tmpMax只需要1*n的大小即可,因为每下一行可以复用前一行,从而进一步把空间复杂度降低为O(n):

class Solution {
public:
    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
        vector<vector<int>> ret;
        int rowNum = grid.size();
        int colNum = rowNum;

        vector<int> tmpMax(colNum, 0);
        for (int i = 0; i < rowNum - 2; ++i) {
            ret.emplace_back();
            
            for (int curCol = 0; curCol < rowNum; ++curCol) {
                tmpMax[curCol] = max(grid[i][curCol], max(grid[i + 1][curCol], grid[i + 2][curCol]));
            }

            for (int j = 0; j < colNum - 2; ++j) {
                int colIndex = j;
                int maxNum = max(tmpMax[colIndex], max(tmpMax[colIndex + 1], tmpMax[colIndex + 2]));
                
                ret[i].push_back(maxNum);
            }
        }

        return ret;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值