DEV Community

Karthika Movva
Karthika Movva

Posted on

Importance of Data Structures

Hi, Folks! Today, I solved three problem on LeetCode : Valid Parentheses, Asteroid collision, and Trapping rain water. All these problems can be solved efficiently using data structures and well thought logics. attempting to solve these problems without data structures implementing any other logic can make them more challenging.

We can assign bracket pairs in the object and use stack data structure to pop the element that no longer required. if required, push the element into stack. In this way we can solve valid parentheses problem.

Similarly, To solve the asteroid collision problem, we can use stack. Depending on positive and negative values, we can pop elements from the stack. In this way we can solve asteroid collision problem.

To solve trapping rain water problem, we need to traverse array from left to right while keeping track of left maximum and right maximum values on left side and right side. Create one variable to keep track of trapped water. As we traverse, we add the trapped water based on left and right values. In this way we can solve trapping rain water problem.

I hope my experience will be helpful to you.

Top comments (0)