Stacks

Balanced Parentheses

December 31, 2023
medium
Stacks, String Parsing

Problem # Given a string containing three types of parentheses ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, write an algorithm to check if the input string is valid. A string is valid if open brackets are closed by the same type of brackets, and open brackets must be closed in the correct order. Solution # Here’s the Python code for checking if a string containing different types of parentheses is valid: ...

Largest Rectangle in Histogram

December 30, 2023
medium
Stacks

Problem # Given an array of integers heights representing the histogram’s bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. Solution # Here’s the Python code for solving the problem of finding the area of the largest rectangle in a histogram: def largestRectangleArea(heights): """ Calculate the area of the largest rectangle in the histogram. Args: heights (List[int]): List of integers representing the heights of the bars in the histogram. ...