This problem should be in stack section

Tags: #<Tag:0x00007f242d957c90>


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


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.