DEV Community

Cover image for Stack Patterns
Sourav
Sourav

Posted on

Stack Patterns

While approaching an algorithmic problem, we should give more attention to the similarities between problems. If we can find these similarities, then solving that kind of problems become very easy. Today we are going to discuss the patterns in the stack.

With this pattern, we can solve a lot of problems based on the stack. We can even solve the popular Stock Span and Maximum Area of Histogram problem.

Approach

Let’s go through the Next Greater Element problem. Here we have to find, the next greater element for each number in an array. We will use the stack to store the greater elements. We will traverse the array backwards.

Algorithm:

  1. Push the last element of the array to the stack

  2. For all the other elements repeat the following process

— Check if the stack is empty,if it is then put -1 at that index. ie. That number does not have any element greater than that to its right side.
— If the peek of the stack is greater than the current element then the stack peek is the next greater element.

— If not, keep popping the top of the stack until you find an element greater than the current element

— If now the stack is empty, place -1 at that index(no greater elements)

— Else peek of the stack is the greater element

— push the current element in the stack

The algorithm can be understood here.

A dry run of the algorithm for the next greater element

Now you have understood how to solve this problem, you can visualize the other related problems on your own.

If we follow this approach and with little changes we can solve Next Smaller Element, Previous Greater Element and Previous Smaller Element problem easily.

All the four problems along with the java code are given below.

Next Greater Element


Next Smaller Element

Previous Greater Element

Previous Smaller Element

Try these problems by yourself.
  1. Previous smallest element

  2. Previous greater element variation

  3. Smallest on the left of an array

    1. Next greater element
    2. Next smaller element
    3. Previous greater element

In our future tutorials, we will try to extend this problem and use this pattern to solve more problems. You can try the following problems by yourself.

  1. Stock Span Problem

  2. Maximum Area Histogram

  3. Maximum Area of Histogram in 2D

The code can be found here

LeetCode

I have been solving Leetcode problems for around a year or so. Suddenly I have developed a passion of writing tutorials for these problems. I am starting with Leetcode problem, in future I will try to make tutorials on Spring,Android,Java,Algorithms and many more.

Follow me on medium - https://sourav-saikia.medium.com
Follow me on dev.to - https://dev.to/sksaikia
Follow me on twitter - https://twitter.com/sourav__saikia
Connect on Linkedin - www.linkedin.com/in/sourav-kumar-saikia

The following table contains all the problems with their respective solution tutorials. I will try to add more articles to it soon.

March LeetCoding Challenge

Top comments (0)