res
to keep track of the maximum number of steps.dp_i
to 0.
b. While the stack is not empty and the current element is greater than the element at the top of the stack:
i. Update dp_i
to be the maximum of dp_i + 1
and the steps of the element at the top of the stack.
ii. Pop the element from the stack.
c. Update res
to be the maximum of res
and dp_i
.
d. Push the current element and its steps onto the stack.res
as the final result.