Largest 1-Bordered Square || JAVASCRIPT || Solution by Bharadwaj
Approach\nDynamic Programming and Prefix Sum \n\n# Complexity\n- Time complexity:\nO(h^2 * w^2)\n\n- Space complexity:\nO(h * w)\n\n# Code\n\nfunction largest1B
# Approach\nDynamic Programming and Prefix Sum \n\n# Complexity\n- Time complexity:\nO(h^2 * w^2)\n\n- Space complexity:\nO(h * w)\n\n# Code\n```\nfunction largest1BorderedSquare(grid) {\n // Initialize dimensions and result\n let h = grid.length, w = grid[0].length, res = 0;\n // Create DP arrays to store prefix sums of 1s in rows and columns\n let dp1 = new Array(h).fill(0).map(() => new Array(w).fill(0));\n let dp2 = new Array(h).fill(0).map(() => new Array(w).fill(0));\n // Calculate prefix sums for rows and columns\n for (let y = 0; y < h; y++) {\n   for (let x = 0; x < w; x++) {\n     dp1[y][x] = x > 0 ? (dp1[y][x - 1] + 1) * grid[y][x] : grid[y][0];\n     dp2[y][x] = y > 0 ? (dp2[y - 1][x] + 1) * grid[y][x] : grid[0][x];\n   }\n }\n // Iterate through potential square corners\n for (let y = 0; y < h; y++) {\n   for (let x = 0; x < w; x++) {\n     if (grid[y][x] === 1) {\n       // Expand potential squares from the current corner\n       let x2 = x, y2 = y;\n       while (y2 < h && x2 < w) {\n         // Check if current square is 1-bordered using prefix sums\n         if (check(x, y, x2, y2)) {\n           res = Math.max(res, (x2 - x + 1) * (y2 - y + 1));\n         }\n         x2++;\n         y2++;\n       }\n     }\n   }\n }\n return res;\n}\n```
['JavaScript']