2975. Maximum Square Area by Removing Fences From a Field LeetCode Solution
In this guide, you will get 2975. Maximum Square Area by Removing Fences From a Field LeetCode Solution with the best time and space complexity. The solution to Maximum Square Area by Removing Fences From a Field problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Square Area by Removing Fences From a Field solution in C++
- Maximum Square Area by Removing Fences From a Field solution in Java
- Maximum Square Area by Removing Fences From a Field solution in Python
- Additional Resources
Problem Statement of Maximum Square Area by Removing Fences From a Field
There is a large (m – 1) x (n – 1) rectangular field with corners at (1, 1) and (m, n) containing some horizontal and vertical fences given in arrays hFences and vFences respectively.
Horizontal fences are from the coordinates (hFences[i], 1) to (hFences[i], n) and vertical fences are from the coordinates (1, vFences[i]) to (m, vFences[i]).
Return the maximum area of a square field that can be formed by removing some fences (possibly none) or -1 if it is impossible to make a square field.
Since the answer may be large, return it modulo 109 + 7.
Note: The field is surrounded by two horizontal fences from the coordinates (1, 1) to (1, n) and (m, 1) to (m, n) and two vertical fences from the coordinates (1, 1) to (m, 1) and (1, n) to (m, n). These fences cannot be removed.
Example 1:
Input: m = 4, n = 3, hFences = [2,3], vFences = [2]
Output: 4
Explanation: Removing the horizontal fence at 2 and the vertical fence at 2 will give a square field of area 4.
Example 2:
Input: m = 6, n = 7, hFences = [2], vFences = [4]
Output: -1
Explanation: It can be proved that there is no way to create a square field by removing fences.
Constraints:
3 <= m, n <= 109
1 <= hFences.length, vFences.length <= 600
1 < hFences[i] < m
1 < vFences[i] < n
hFences and vFences are unique.
Complexity Analysis
- Time Complexity: O(|\texttt{hFences}|^2 + |\texttt{vFences}|^2)
- Space Complexity: O(|\texttt{hFences}|^2 + |\texttt{vFences}|^2)
2975. Maximum Square Area by Removing Fences From a Field LeetCode Solution in C++
class Solution {
public:
int maximizeSquareArea(int m, int n, vector<int>& hFences,
vector<int>& vFences) {
constexpr int kMod = 1'000'000'007;
hFences.push_back(1);
hFences.push_back(m);
vFences.push_back(1);
vFences.push_back(n);
ranges::sort(hFences);
ranges::sort(vFences);
const unordered_set<int> hGaps = getGaps(hFences);
const unordered_set<int> vGaps = getGaps(vFences);
int maxGap = -1;
for (const int hGap : hGaps)
if (vGaps.contains(hGap))
maxGap = max(maxGap, hGap);
return maxGap == -1 ? -1 : static_cast<long>(maxGap) * maxGap % kMod;
}
private:
unordered_set<int> getGaps(const vector<int>& fences) {
unordered_set<int> gaps;
for (int i = 0; i < fences.size(); ++i)
for (int j = 0; j < i; ++j)
gaps.insert(fences[i] - fences[j]);
return gaps;
}
};
/* code provided by PROGIEZ */
2975. Maximum Square Area by Removing Fences From a Field LeetCode Solution in Java
class Solution {
public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) {
final int kMod = 1_000_000_007;
hFences = Arrays.copyOf(hFences, hFences.length + 2);
vFences = Arrays.copyOf(vFences, vFences.length + 2);
hFences[hFences.length - 2] = 1;
hFences[hFences.length - 1] = m;
vFences[vFences.length - 2] = 1;
vFences[vFences.length - 1] = n;
Arrays.sort(hFences);
Arrays.sort(vFences);
Set<Integer> hGaps = getGaps(hFences);
Set<Integer> vGaps = getGaps(vFences);
int maxGap = -1;
for (final int hGap : hGaps)
if (vGaps.contains(hGap))
maxGap = Math.max(maxGap, hGap);
return maxGap == -1 ? -1 : (int) ((long) maxGap * maxGap % kMod);
}
private Set<Integer> getGaps(int[] fences) {
Set<Integer> gaps = new HashSet<>();
for (int i = 0; i < fences.length; ++i)
for (int j = 0; j < i; ++j)
gaps.add(fences[i] - fences[j]);
return gaps;
}
}
// code provided by PROGIEZ
2975. Maximum Square Area by Removing Fences From a Field LeetCode Solution in Python
class Solution:
def maximizeSquareArea(
self,
m: int,
n: int,
hFences: list[int],
vFences: list[int],
) -> int:
hFences = sorted(hFences + [1, m])
vFences = sorted(vFences + [1, n])
hGaps = {hFences[i] - hFences[j]
for i in range(len(hFences))
for j in range(i)}
vGaps = {vFences[i] - vFences[j]
for i in range(len(vFences))
for j in range(i)}
maxGap = next((hGap
for hGap in sorted(hGaps, reverse=True)
if hGap in vGaps), -1)
return -1 if maxGap == -1 else maxGap**2 % (10**9 + 7)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.