/**
* Problem: LeetCode 2943 - Maximize Area of Square Hole in Grid
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
class Solution {
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
int maxHGap = getMaxGap(hBars);
int maxVGap = getMaxGap(vBars);
int side = Math.min(maxHGap, maxVGap);
return side * side;
}
private int getMaxGap(int[] bars) {
Arrays.sort(bars);
int maxConsecutive = 1;
int currentConsecutive = 1;
for (int i = 1; i < bars.length; i++) {
if (bars[i] == bars[i - 1] + 1) {
currentConsecutive++;
} else {
currentConsecutive = 1;
}
maxConsecutive = Math.max(maxConsecutive, currentConsecutive);
}
return maxConsecutive + 1;
}
}
"""
Problem: LeetCode 2943 - Maximize Area of Square Hole in Grid
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def maximizeSquareHoleArea(
self, n: int, m: int, hBars: List[int], vBars: List[int]
) -> int:
def get_max_gap(bars: List[int]) -> int:
bars.sort()
max_consecutive = 1
current_consecutive = 1
for i in range(1, len(bars)):
if bars[i] == bars[i - 1] + 1:
current_consecutive += 1
else:
current_consecutive = 1
max_consecutive = max(max_consecutive, current_consecutive)
return max_consecutive + 1
side = min(get_max_gap(hBars), get_max_gap(vBars))
return side * side
/**
* Problem: LeetCode 2943 - Maximize Area of Square Hole in Grid
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
auto getMaxGap = [](vector<int>& bars) -> int {
sort(bars.begin(), bars.end());
int maxConsecutive = 1;
int currentConsecutive = 1;
for (size_t i = 1; i < bars.size(); i++) {
if (bars[i] == bars[i - 1] + 1) {
currentConsecutive++;
} else {
currentConsecutive = 1;
}
maxConsecutive = max(maxConsecutive, currentConsecutive);
}
return maxConsecutive + 1;
};
int maxHGap = getMaxGap(hBars);
int maxVGap = getMaxGap(vBars);
int side = min(maxHGap, maxVGap);
return side * side;
}
};
/**
* Problem: LeetCode 2943 - Maximize Area of Square Hole in Grid
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number} n
* @param {number} m
* @param {number[]} hBars
* @param {number[]} vBars
* @return {number}
*/
var maximizeSquareHoleArea = function(n, m, hBars, vBars) {
function getMaxGap(bars) {
bars.sort((a, b) => a - b);
let maxConsecutive = 1;
let currentConsecutive = 1;
for (let i = 1; i < bars.length; i++) {
if (bars[i] === bars[i - 1] + 1) {
currentConsecutive++;
} else {
currentConsecutive = 1;
}
maxConsecutive = Math.max(maxConsecutive, currentConsecutive);
}
return maxConsecutive + 1;
}
const side = Math.min(getMaxGap(hBars), getMaxGap(vBars));
return side * side;
};