left
and right
, to store the indices of the nearest smaller elements to the left and right of each element in nums
.left
and right
arrays.res
with the same length as nums
.nums
and calculate the maximum length of a subarray where the current element is the minimum. Update res
for that length.res
in reverse to fill in any gaps, ensuring that each element in res
is at least as large as the one after it.res
.