Imagine you're a landscape architect working with a large rectangular field that's divided by various fences. Your goal is to create the largest possible square area by strategically removing some of these fences!
You have a field with dimensions (m-1) × (n-1) that spans from coordinate (1,1) to (m,n). The field contains:
- Horizontal fences: Each fence at position
hFences[i]runs from(hFences[i], 1)to(hFences[i], n) - Vertical fences: Each fence at position
vFences[i]runs from(1, vFences[i])to(m, vFences[i])
Important: The field has a permanent border (four outer fences) that cannot be removed.
Your task is to find the maximum area of a square that can be formed by removing some fences (or keeping all of them). Return the area modulo 10^9 + 7, or -1 if no square can be formed.
Input & Output
Visualization
Time & Space Complexity
O(h²) to generate horizontal distances and O(v²) for vertical distances, where h and v are number of fences
Space needed to store all possible horizontal and vertical distances in hash sets
Constraints
- 3 ≤ m, n ≤ 109
- 1 ≤ hFences.length, vFences.length ≤ 1000
- 1 < hFences[i] < m
- 1 < vFences[i] < n
- All fence positions are unique within their respective arrays