You are given a 0-indexed array nums of length n.
For each index i from 0 to n - 1, find the largest range [lefti, righti] such that the following conditions hold:
nums[i] is the maximum element in the subarray nums[lefti...righti].lefti <= i <= righti.Return an array answer of length n where answer[i] = righti - lefti + 1.
Example 1:
Input: nums = [1,3,2,1,2] Output: [1,3,1,1,1] Explanation: For i = 0, the largest range that satisfies the condition is [0, 0], so answer[0] = 0 - 0 + 1 = 1. For i = 1, the largest range that satisfies the condition is [0, 2], so answer[1] = 2 - 0 + 1 = 3. For i = 2, the largest range that satisfies the condition is [2, 2], so answer[2] = 2 - 2 + 1 = 1. For i = 3, the largest range that satisfies the condition is [3, 3], so answer[3] = 3 - 3 + 1 = 1. For i = 4, the largest range that satisfies the condition is [4, 4], so answer[4] = 4 - 4 + 1 = 1.
Example 2:
Input: nums = [1,5,4,3,5] Output: [1,4,2,1,3] Explanation: For i = 0, the largest range that satisfies the condition is [0, 0], so answer[0] = 0 - 0 + 1 = 1. For i = 1, the largest range that satisfies the condition is [0, 3], so answer[1] = 3 - 0 + 1 = 4. For i = 2, the largest range that satisfies the condition is [2, 3], so answer[2] = 3 - 2 + 1 = 2. For i = 3, the largest range that satisfies the condition is [3, 3], so answer[3] = 3 - 3 + 1 = 1. For i = 4, the largest range that satisfies the condition is [3, 5], so answer[4] = 5 - 3 + 1 = 3.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 106When 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:
The brute force approach for finding the maximal range where each number is the biggest involves checking all possible ranges for each number in a collection. We test every range to see if the current number is truly the largest within that specific range. We then keep track of the longest range we've found where the number is the biggest.
Here's how the algorithm would work step-by-step:
def find_maximal_range(collection):
maximal_range_size = 0
for current_index in range(len(collection)):
# Consider each element as the maximum for a potential range
current_range_size = 0
left_index = current_index
right_index = current_index
while left_index >= 0 and right_index < len(collection):
is_maximum = True
for index in range(left_index, right_index + 1):
# Verify that the current element is the maximum in the range
if collection[index] > collection[current_index]:
is_maximum = False
break
if is_maximum:
current_range_size = right_index - left_index + 1
maximal_range_size = max(maximal_range_size, current_range_size)
# Expand range to right, then left
right_index += 1
if right_index >= len(collection):
break
is_maximum = True
for index in range(left_index, right_index + 1):
if collection[index] > collection[current_index]:
is_maximum = False
break
if is_maximum:
current_range_size = right_index - left_index + 1
maximal_range_size = max(maximal_range_size, current_range_size)
else:
break
left_index -= 1
if left_index < 0:
break
is_maximum = True
for index in range(left_index, right_index + 1):
if collection[index] > collection[current_index]:
is_maximum = False
break
if is_maximum:
current_range_size = right_index - left_index + 1
maximal_range_size = max(maximal_range_size, current_range_size)
else:
break
else:
break
return maximal_range_sizeThe goal is to find, for each number in a list, the largest continuous section where that number is the highest. We can solve this efficiently by scanning the list from both the left and the right to determine the boundaries of each section, avoiding redundant comparisons.
Here's how the algorithm would work step-by-step:
def find_maximal_range(number_list):
number_list_length = len(number_list)
left_ranges = [0] * number_list_length
right_ranges = [0] * number_list_length
for index in range(number_list_length):
left_ranges[index] = index
# Extend left range as far as possible
while left_ranges[index] > 0 and number_list[index] >= number_list[left_ranges[index] - 1]:
left_ranges[index] -= 1
right_ranges[index] = index
# Extend right range as far as possible
while right_ranges[index] < number_list_length - 1 and number_list[index] > number_list[right_ranges[index] + 1]:
right_ranges[index] += 1
result = []
# Combine left and right ranges to form the final result.
for index in range(number_list_length):
result.append((left_ranges[index], right_ranges[index]))
return result| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty list or throw an IllegalArgumentException, depending on requirements. |
| Array with a single element | The element's maximal range is itself, so return a list containing a single-element range [index, index]. |
| Array with all identical elements | Each element's maximal range is the entire array, from index 0 to index n-1. |
| Array sorted in ascending order | Each element's maximal range extends from its index to the end of the array. |
| Array sorted in descending order | Each element's maximal range is only itself. |
| Array with negative numbers | The algorithm should handle negative numbers correctly as it compares values directly without assumptions about positivity. |
| Array with very large numbers (potential integer overflow) | Use long or appropriate large number data types to avoid integer overflow during comparisons. |
| Input array is extremely large | Ensure the solution is efficient (e.g., using a stack or optimized algorithm) to avoid exceeding time or memory limits. |