You are given an integer array planks, where planks[i] represents the height of the ith wooden plank. Each plank has a width of 1 unit.
You want to build a fence consisting of planks that all have the same height.
You may either use a plank as is, or combine exactly two distinct original planks into a single plank whose height equals the sum of their heights. Each original plank can be used at most once, and not all original planks need to be used.
Return the maximum possible width of the fence that can be built.
Example 1:
Input: planks = [1,3,2,5,7,5,4,2,1]
Output: 4
Explanation:
We can have four planks of height 5.
planks[3] = 5planks[5] = 5planks[0] + planks[6] = 1 + 4 = 5planks[1] + planks[2] = 3 + 2 = 5Hence, the maximum width is 4.
Example 2:
Input: planks = [2,3,7]
Output: 1
Explanation:
Constraints:
1 <= planks.length <= 10001 <= planks[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a grid with several fence posts placed around, and you want to find the widest possible gap you can create between two opposite sides by removing some posts. The brute force approach works by systematically testing every single combination of removing posts to see what the resulting gap would be.
Here's how the algorithm would work step-by-step:
def widest_possible_fence(grid_width, grid_height, fence_posts_x, fence_posts_y):
# Sort boundaries to test every sequential pair of fence posts as a gap
sorted_x_coordinates = sorted([0] + fence_posts_x + [grid_width])
sorted_y_coordinates = sorted([0] + fence_posts_y + [grid_height])
maximum_found_width = 0
maximum_found_height = 0
for left_boundary_index in range(len(sorted_x_coordinates)):
for right_boundary_index in range(left_boundary_index + 1, len(sorted_x_coordinates)):
current_width_gap = sorted_x_coordinates[right_boundary_index] - sorted_x_coordinates[left_boundary_index]
for bottom_boundary_index in range(len(sorted_y_coordinates)):
for top_boundary_index in range(bottom_boundary_index + 1, len(sorted_y_coordinates)):
current_height_gap = sorted_y_coordinates[top_boundary_index] - sorted_y_coordinates[bottom_boundary_index]
# Check every fence post to ensure no obstructions exist within this specific gap
is_gap_obstructed = False
for post_x, post_y in zip(fence_posts_x, fence_posts_y):
if sorted_x_coordinates[left_boundary_index] < post_x < sorted_x_coordinates[right_boundary_index] and sorted_y_coordinates[bottom_boundary_index] < post_y < sorted_y_coordinates[top_boundary_index]:
is_gap_obstructed = True
break
# Track the largest unobstructed rectangular area found across all tested combinations
if not is_gap_obstructed:
current_area = current_width_gap * current_height_gap
maximum_possible_area = maximum_found_width * maximum_found_height
if current_area > maximum_possible_area:
maximum_found_width = current_width_gap
maximum_found_height = current_height_gap
return maximum_found_width * maximum_found_heightInstead of testing every single combination of gaps, the optimal approach focuses on finding the single largest gap in both dimensions independently. By looking at where the vertical and horizontal bars are placed, we can find the biggest empty space by simply sorting their positions and measuring the distances between neighboring pairs.
Here's how the algorithm would work step-by-step:
def widest_possible_fence(fence_height, fence_width, horizontal_bars, vertical_bars):
# Outer boundaries must be included because they limit the maximum possible empty area.
all_horizontal_bars = sorted([0, fence_height] + horizontal_bars)
max_vertical_gap = max(all_horizontal_bars[index] - all_horizontal_bars[index - 1] for index in range(1, len(all_horizontal_bars)))
# Outer boundaries are added here as well to correctly measure edge gaps.
all_vertical_bars = sorted([0, fence_width] + vertical_bars)
max_horizontal_gap = max(all_vertical_bars[index] - all_vertical_bars[index - 1] for index in range(1, len(all_vertical_bars)))
# Independence of dimensions allows us to multiply max gaps directly for total area.
return max_vertical_gap * max_horizontal_gap| Case | How to Handle |
|---|---|
| Fences arrays do not include the grid boundaries m and n | Explicitly append 1 and m to hFences, and 1 and n to vFences before processing to include the outer walls. |
| No common spacing can be formed between any horizontal and vertical fence pairs | Return -1 if the intersection of possible horizontal and vertical differences is empty. |
| Duplicate fence coordinates provided in the input arrays | Sort and deduplicate the fence arrays before generating differences to avoid redundant zero-width or zero-height intervals. |
| Maximum possible area exceeds standard integer limits before applying modulo | Perform area multiplication using 64-bit integers and apply modulo 10^9 + 7 at the final return step. |
| Fences arrays contain only a single element | Appending the grid boundaries ensures we always have at least two coordinates to compute valid fence gaps. |
| Grid dimensions m and n are at their maximum constraints | O(N^2 + M^2) difference generation scales efficiently within time limits given typical constraint sizes. |