Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created January 15, 2026 17:38
Show Gist options
  • Select an option

  • Save tatsuyax25/7437cf51e48c1c0afd39b1feaf3c3c27 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/7437cf51e48c1c0afd39b1feaf3c3c27 to your computer and use it in GitHub Desktop.
You are given the two integers, n and m and two integer arrays, hBars and vBars. The grid has n + 2 horizontal and m + 2 vertical bars, creating 1 x 1 unit cells. The bars are indexed starting from 1. You can remove some of the bars in hBars from ho
/**
* @param {number} n
* @param {number} m
* @param {number[]} hBars
* @param {number[]} vBars
* @return {number}
*/
var maximizeSquareHoleArea = function(n, m, hBars, vBars) {
/**
* Finds the longest run of consecutive integers in an array.
* Example: [2,3,4,7,8] → longest run = 3 (2→3→4)
*/
function longestRun(arr) {
// No removable bars → no run
if (arr.length === 0) return 0;
// Sort so we can detect consecutive sequences
arr.sort((a, b) => a - b);
let maxRun = 1; // longest run found so far
let currentRun = 1; // current consecutive streak
for (let i = 1; i < arr.length; i++) {
// If the current bar continues the streak
if (arr[i] === arr[i - 1] + 1) {
currentRun++;
} else {
// Streak broken → reset
currentRun = 1;
}
// Track the best streak seen
maxRun = Math.max(maxRun, currentRun);
}
return maxRun;
}
// Longest consecutive removable horizontal bars
const hRun = longestRun(hBars);
// Longest consecutive removable vertical bars
const vRun = longestRun(vBars);
/**
* IMPORTANT:
* Removing k consecutive bars opens a gap of size (k + 1).
* Example: remove bars 3 and 4 → you open 3 cells.
*/
const hGap = hRun + 1;
const vGap = vRun + 1;
// The largest square must fit in BOTH directions
const side = Math.min(hGap, vGap);
// Area of the largest square hole
return side * side;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment