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
easyYou are given an integer array prices where prices[i] is the price of the ith item in a shop.
Next Greater Element I
easyThe 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
mediumGiven 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
mediumYou are given an array heights of n integers representing the number of bricks in n consecutive towers.
Beautiful Towers II
mediumYou are given a 0-indexed array maxHeights of n integers.
Car Fleet
mediumThere are n cars at given miles away from the starting mile 0, traveling to reach the mile target.
Count Submatrices With All Ones
mediumGiven an m x n binary matrix mat, return the number of submatrices that have all ones.
Daily Temperatures
mediumGiven 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
mediumGiven an integer array nums and a positive integer k, return the most competitive subsequence of nums of size k.
Longest Well-Performing Interval
mediumWe are given hours, a list of the number of hours worked per day for a given employee.
Make Array Non-decreasing
mediumYou are given an integer array nums.
Max Chunks To Make Sorted
mediumYou 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
mediumThe min-product of an array is equal to the minimum value in the array multiplied by the array's sum.
Maximum Width Ramp
mediumA 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
mediumGiven 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
mediumYou are given an array nums of size n, consisting of non-negative integers.
Next Greater Element II
mediumGiven 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
mediumGiven a string s, remove duplicate letters so that every letter appears once and only once.
Remove K Digits
mediumGiven 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
mediumGiven 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
mediumGiven 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
mediumGiven 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
mediumYou are given a 0-indexed integer array nums.
Sum of Subarray Minimums
mediumGiven an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr.
Sum of Subarray Ranges
mediumYou are given an integer array nums.
The Number of Weak Characters in the Game
mediumYou 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
hardYou are given an array nums of n positive integers and an integer k.
Car Fleet II
hardThere are n cars traveling at different speeds in the same direction along a one-lane road.
Count Non-Decreasing Subarrays After K Operations
hardYou are given an array nums of n integers and an integer k.
Create Maximum Number
hardYou are given two integer arrays nums1 and nums2 of lengths m and n respectively.