DEV Community

Karleb
Karleb

Posted on

2373. Largest Local Values in a Matrix

https://leetcode.com/problems/largest-local-values-in-a-matrix/?envType=daily-question&envId=2024-05-14

May 12 solution

/**
 * @param {number[][]} grid
 * @return {number[][]}
 */
/**
 * @param {number[][]} grid
 * @return {number[][]}
 */
var largestLocal = function (grid) {
    const nRows = grid.length;
    const nCols = grid[0].length;

    let result = new Array(nRows - 2).fill().map(() => new Array(nCols - 2).fill(0));

    for (let row = 0; row < nRows - 2; row++) {
        for (let col = 0; col < nCols - 2; col++) {
            result[row][col] = findLargest(grid, row, col);
        }
    }

    return result;
};

function findLargest(grid, row, col) {
    let best = grid[row][col];
    for (let i = row; i < row + 3; i++) {
        for (let j = col; j < col + 3; j++) {
            best = Math.max(best, grid[i][j]);
        }
    }
    return best;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)