Monotonic Stack

Monotonic Stack

Thirty questions on the monotonic stack pattern, for next-greater/next-smaller and histogram-style problems.

Final Prices With a Special Discount in a Shop

easy
You are given an integer array prices where prices[i] is the price of the ith item in a shop.

Next Greater Element I

easy
The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.

132 Pattern

medium
Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].

Beautiful Towers I

medium
You are given an array heights of n integers representing the number of bricks in n consecutive towers.

Beautiful Towers II

medium
You are given a 0-indexed array maxHeights of n integers.

Car Fleet

medium
There are n cars at given miles away from the starting mile 0, traveling to reach the mile target.

Count Submatrices With All Ones

medium
Given an m x n binary matrix mat, return the number of submatrices that have all ones.

Daily Temperatures

medium
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to...

Find the Most Competitive Subsequence

medium
Given an integer array nums and a positive integer k, return the most competitive subsequence of nums of size k.

Longest Well-Performing Interval

medium
We are given hours, a list of the number of hours worked per day for a given employee.

Make Array Non-decreasing

medium
You are given an integer array nums.

Max Chunks To Make Sorted

medium
You are given an integer array arr of length n that represents a permutation of the integers in the range [0, n - 1].

Maximum Subarray Min-Product

medium
The min-product of an array is equal to the minimum value in the array multiplied by the array's sum.

Maximum Width Ramp

medium
A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j].

Minimum Cost Tree From Leaf Values

medium
Given an array arr of positive integers, consider all binary trees such that: Among all possible binary trees considered, return the smallest possible sum of the values of each...

Minimum Operations to Convert All Elements to Zero

medium
You are given an array nums of size n, consisting of non-negative integers.

Next Greater Element II

medium
Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.

Remove Duplicate Letters

medium
Given a string s, remove duplicate letters so that every letter appears once and only once.

Remove K Digits

medium
Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.

Shortest Subarray to be Removed to Make Array Sorted

medium
Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing.

Shortest Unsorted Continuous Subarray

medium
Given an integer array nums, you need to find one continuous subarray such that if you only sort this subarray in non-decreasing order, then the whole array will be sorted in...

Smallest Subsequence of Distinct Characters

medium
Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.

Steps to Make Array Non-decreasing

medium
You are given a 0-indexed integer array nums.

Sum of Subarray Minimums

medium
Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr.

Sum of Subarray Ranges

medium
You are given an integer array nums.

The Number of Weak Characters in the Game

medium
You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense.

Apply Operations to Maximize Score

hard
You are given an array nums of n positive integers and an integer k.

Car Fleet II

hard
There are n cars traveling at different speeds in the same direction along a one-lane road.

Count Non-Decreasing Subarrays After K Operations

hard
You are given an array nums of n integers and an integer k.

Create Maximum Number

hard
You are given two integer arrays nums1 and nums2 of lengths m and n respectively.