This problem is solvable by DP but that will not be its optimal solution.

Its optimal solution can be obtained using STACK. Hence this problem should ne in stack section

# This problem should be in stack section

**geekfreak97**#1

**aliencoder22**#2

If judging on the basis of time complexity and space complexity then both the approaches have the same time complexity i.e O(n) and worst case space complexity of stack approach is also O(n) which is similar to the space complexity of dp approach.