herrDeng網內搜尋

自訂搜尋

Ads

2024年5月12日 星期日

python C++速解MaxPooling Leetcode 2373 Largest Local Values in a Matrix


python CPP速解MaxPooling Leetcode 2373  Largest Local Values in a Matrix
算maxPooling stride=1就一般矩陣計算而已,找出子矩陣的極大,可以只使用額外空間O(1)就得解。C++解請進。
-----
Calculating maxPooling stride=1 is just a general matrix calculation. To find the maximum of the submatrix, you can solve it using only extra space O(1).
#pragma GCC optimize("O3", "unroll-loops")
class Solution {
public:
    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
        const int n=grid.size();
        for(int i=1; i<n-1; i++){           
            for(int j=1; j< n-1; j++){
                for(int r=i-1; r<=i+1; r++)
                    for( int c=j-1; c<=j+1; c++)
                        grid[i-1][j-1]=max(grid[i-1][j-1], grid[r][c]);
            }
            grid[i-1].resize(n-2);
        }
        grid.resize(n-2);
        return grid;
    }
};

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章