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).
  1. #pragma GCC optimize("O3", "unroll-loops")
  2. class Solution {
  3. public:
  4. vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
  5. const int n=grid.size();
  6. for(int i=1; i<n-1; i++){
  7. for(int j=1; j< n-1; j++){
  8. for(int r=i-1; r<=i+1; r++)
  9. for( int c=j-1; c<=j+1; c++)
  10. grid[i-1][j-1]=max(grid[i-1][j-1], grid[r][c]);
  11. }
  12. grid[i-1].resize(n-2);
  13. }
  14. grid.resize(n-2);
  15. return grid;
  16. }
  17. };

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章