LeetCode solutions

2454. Largest Local Values in a Matrix

My accepted JavaScript solution to LeetCode problem 2454, Largest Local Values in a Matrix, running in 7ms.

  • Difficulty: Easy
  • JavaScript
  • Runtime 7ms
  • Memory 59.6MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 7ms, memory 59.6MB, accepted 2025-12-29.

javascript
var largestLocal = function(grid) {
    const n = grid.length;
    const result = [];
    for (let i = 0; i < n - 2; i++) {
        result.push([]);
        for (let j = 0; j < n - 2; j++) {
            let maxVal = 0;
            for (let di = 0; di < 3; di++) {
                for (let dj = 0; dj < 3; dj++) {
                    maxVal = Math.max(maxVal, grid[i + di][j + dj]);
                }
            }
            result[i].push(maxVal);
        }
    }
    return result;
};

Source