{"id":9783,"date":"2022-08-14T21:18:53","date_gmt":"2022-08-15T04:18:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9783"},"modified":"2022-08-14T21:20:26","modified_gmt":"2022-08-15T04:20:26","slug":"leetcode-2373-largest-local-values-in-a-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2373-largest-local-values-in-a-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2373. Largest Local Values in a Matrix"},"content":{"rendered":"\n<p>You are given an&nbsp;<code>n x n<\/code>&nbsp;integer matrix&nbsp;<code>grid<\/code>.<\/p>\n\n\n\n<p>Generate an integer matrix&nbsp;<code>maxLocal<\/code>&nbsp;of size&nbsp;<code>(n - 2) x (n - 2)<\/code>&nbsp;such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>maxLocal[i][j]<\/code>&nbsp;is equal to the&nbsp;<strong>largest<\/strong>&nbsp;value of the&nbsp;<code>3 x 3<\/code>&nbsp;matrix in&nbsp;<code>grid<\/code>&nbsp;centered around row&nbsp;<code>i + 1<\/code>&nbsp;and column&nbsp;<code>j + 1<\/code>.<\/li><\/ul>\n\n\n\n<p>In other words, we want to find the largest value in every contiguous&nbsp;<code>3 x 3<\/code>&nbsp;matrix in&nbsp;<code>grid<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the generated matrix<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/06\/21\/ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]\n<strong>Output:<\/strong> [[9,9],[8,6]]\n<strong>Explanation:<\/strong> The diagram above shows the original matrix and the generated matrix.\nNotice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/07\/02\/ex2new2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]\n<strong>Output:<\/strong> [[2,2,2],[2,2,2],[2,2,2]]\n<strong>Explanation:<\/strong> Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == grid.length == grid[i].length<\/code><\/li><li><code>3 &lt;= n &lt;= 100<\/code><\/li><li><code>1 &lt;= grid[i][j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*n*9)<br>Space complexity: O(n*n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> largestLocal(vector<vector<int>>& grid) {    \n    const int m = grid.size() - 2;\n    vector<vector<int>> ans(m, vector<int>(m));\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < m; ++j)        \n        for (int dy = 0; dy <= 2; ++dy)\n          for (int dx = 0; dx <= 2; ++dx)\n            ans[i][j] = max(ans[i][j], grid[i + dy][j + dx]);        \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an&nbsp;n x n&nbsp;integer matrix&nbsp;grid. Generate an integer matrix&nbsp;maxLocal&nbsp;of size&nbsp;(n &#8211; 2) x (n &#8211; 2)&nbsp;such that: maxLocal[i][j]&nbsp;is equal to the&nbsp;largest&nbsp;value of the&nbsp;3&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,216],"class_list":["post-9783","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-matrix","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9783"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9783\/revisions"}],"predecessor-version":[{"id":9786,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9783\/revisions\/9786"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}