#2975

Maximum Square Area by Removing Fences From a Field

specialist · 910 · lc medium +31 · verified · 49.4% accepted · 452 likes · top 36%

Description

There is a large (m - 1) x (n - 1) rectangular field with corners at (1, 1) and (m, n). It contains horizontal fences in hFences and vertical fences in vFences.

Horizontal fences run from (hFences[i], 1) to (hFences[i], n); vertical fences from (1, vFences[i]) to (m, vFences[i]).

You can remove any subset of the removable fences. Return the maximum area of a square hole that can be formed, modulo 109 + 7, or -1 if impossible.

Note: The four boundary 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.

Code

1
2
3