矩阵中的局部最大值
| Category | Difficulty | Likes | Dislikes |
|---|---|---|---|
| algorithms | Easy (84.79%) | 11 | - |
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>
给你一个大小为 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 矩阵的最大值。示例 2:

输入: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]]
输出:[[2,2,2],[2,2,2],[2,2,2]]
解释:注意,2 包含在 grid 中每个 3 x 3 的矩阵中。提示:
n == grid.length == grid[i].length3 <= n <= 1001 <= grid[i][j] <= 100
/*
* @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Date: 2022-11-10 13:59:00
* @LastEditTime: 2022-11-10 14:12:19
* @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Description:
* @FilePath: /imooc-jira/Users/izhaong/izhaong/Project_me/leetcode/2373.矩阵中的局部最大值.js
*/
/*
* @lc app=leetcode.cn id=2373 lang=javascript
*
* [2373] 矩阵中的局部最大值
*
* https://leetcode.cn/problems/largest-local-values-in-a-matrix/description/
*
* algorithms
* Easy (84.79%)
* Likes: 11
* Dislikes: 0
* Total Accepted: 10.3K
* Total Submissions: 12.1K
* Testcase Example: '[[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]'
*
* 给你一个大小为 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 矩阵的最大值。
*
* 示例 2:
*
*
*
*
* 输入: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]]
* 输出:[[2,2,2],[2,2,2],[2,2,2]]
* 解释:注意,2 包含在 grid 中每个 3 x 3 的矩阵中。
*
*
*
*
* 提示:
*
*
* n == grid.length == grid[i].length
* 3 <= n <= 100
* 1 <= grid[i][j] <= 100
*
*
*/
// @lc code=start
/**
* @param {number[][]} grid
* @return {number[][]}
*/
function largestLocal(grid) {
const n = grid.length - 2;
const resArr = new Array(n);
for (let i = 0; i < n; i++) {
resArr[i] = new Array(n);
for (let j = 0; j < n; j++) {
resArr[i][j] = Math.max(
grid[i][j],
grid[i][j + 1],
grid[i][j + 2],
grid[i + 2][j],
grid[i + 2][j + 1],
grid[i + 2][j + 2],
grid[i + 1][j],
grid[i + 1][j + 1],
grid[i + 1][j + 2]
);
}
}
return resArr;
}
// @lc code=end