You are given an integer array nums of length n.
For each index i (0 <= i < n), define ans[i] as the maximum possible length of a contiguous subarray nums[l..r] such that:
l <= i <= r
(the subarray contains index
i
), and
nums[l..r]
is
exactly
nums[i]
.
In other words, starting from position i, you can expand to the left and right as long as you do not include any element strictly greater than nums[i]. Among all such subarrays that still contain index i, ans[i] is the maximum possible length.
Return the array ans of length n.
You may assume n can be large (e.g., up to around 2 * 10^5), so you should design an efficient algorithm.
Example 1
Example 2
Follow-up (circular array):
Now suppose nums is circular: index 0 comes after index n - 1. A contiguous subarray on this circle may wrap around the end of the array (e.g., [nums[3], nums[4], nums[0], nums[1]] when n = 5).
For each index i, compute the maximum possible length of a circular contiguous segment such that:
i
, and
nums[i]
.
Return the resulting array for the circular case as well, or describe how to modify your algorithm to handle the circular version.