From 11f960491962f0dba34d92f7e7bfa1bf7f8d721c Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Fri, 16 Jan 2026 11:24:29 +0600 Subject: [PATCH 1/2] LeetCode-2975: Maximum Square Area by Removing Fences From a Field --- LeetCode/medium.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 LeetCode/medium.py diff --git a/LeetCode/medium.py b/LeetCode/medium.py new file mode 100644 index 0000000..fc84059 --- /dev/null +++ b/LeetCode/medium.py @@ -0,0 +1,37 @@ +from typing import List + + +class Solution: + # https://leetcode.com/problems/maximum-square-area-by-removing-fences-from-a-field + # 2975. Maximum Square Area by Removing Fences From a Field + def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[int]) -> int: + # Add boundaries + """ + This function calculates the maximum square area that can be formed by removing fences from a field of size m x n. + + Parameters: + m (int): The width of the field + n (int): The height of the field + hFences (List[int]): A list of horizontal fence positions + vFences (List[int]): A list of vertical fence positions + + Returns: + int: The maximum square area that can be formed, or -1 if it is not possible + """ + h = sorted([1] + hFences + [m]) + v = sorted([1] + vFences + [n]) + + # possible horizontal distances + h_dist = set() + for i in range(len(h)): + for j in range(i + 1, len(h)): + h_dist.add(h[j] - h[i]) + max_dist = 0 + # possible vertical distance + for i in range(len(v)): + for j in range(i + 1, len(v)): + d = v[j] - v[i] + if d in h_dist: + max_dist = max(d, max_dist) + + return -1 if max_dist == 0 else (max_dist * max_dist) % 10**9 + 7 From 1bc0ff20ea67ab54093e2747f186c1945a105f98 Mon Sep 17 00:00:00 2001 From: Abdul Wajed Khan <51821594+WazedKhan@users.noreply.github.com> Date: Fri, 16 Jan 2026 11:46:32 +0600 Subject: [PATCH 2/2] Update LeetCode/medium.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- LeetCode/medium.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LeetCode/medium.py b/LeetCode/medium.py index fc84059..2bba78c 100644 --- a/LeetCode/medium.py +++ b/LeetCode/medium.py @@ -34,4 +34,5 @@ def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[i if d in h_dist: max_dist = max(d, max_dist) - return -1 if max_dist == 0 else (max_dist * max_dist) % 10**9 + 7 + MOD = 10**9 + 7 + return -1 if max_dist == 0 else (max_dist * max_dist) % MOD