-
LeetCode 739: Daily Temperatures
link Brute force For each day we can scan into the future and pick the first day with a higher temperature. Time: . Stack If the temperatures are sorted in non-increasing order, no days will have a warmer day in the future. So, if any day has a future warmer day, there must be two…
-
LeetCode 232: Implement Queue using Stacks
link Queue preserves the write order. Stack reverses the write order. If we reverse write order twice, we get back the original order. So, we can pass the push()‘s through the two stacks (left and right) in sequence: . Here would be the push()and would be peek() or pop(). Amortized, each push(), peek(), pop() and…
-
LeetCode 341: Flatten Nested List Iterator
link We want to flatten the list lazily, so we use a stack with the top as the next element to return. If hasNext() returns true, the next() must return an integer. So, hasNext() must guarantee that there is an integer to return. We could not simply use len() as the signal for hasNext() because,…
-
LeetCode 636: Exclusive Time of Functions
link From pairs of consecutive logs we find exclusive time piecemeal and attribute to the correct function. Exclusive cpu time A log represents two timestamps: (1) end timestamp of the previous function and (2) start timestamp of the next function. If log timestamp is , then: From a pair of consecutive logs: and , we…
-
LeetCode 1249: Minimum Remove to Make Valid Parentheses
link Brute force Try all possibilities. For every opening parenthesis we can take it or remove it. For every closing parenthesis, we can remove it or if close count is less than open count, we can take it. Time: , space: . Greedy With a stack we try to match (or pop) an open as…
-
LeetCode 1047: Remove All Adjacent Duplicates In String
link Stack We can consider an adjacent duplicate pair like “aa” or “cc” as “()”. Removal of such a pair may produce more duplicate pairs only if there is nesting like: “(())”. For example: “zaaz” or “poop”. So, we use a stack to remove the nested duplicate pairs. Time: , space: same. Two pointers If…
-
LeetCode 224: Basic Calculator
link We want to process the string as a sequence of tokens where each token is an operator or an opening (or closing) parenthesis or a multi-digit number. So, we use a stack to store tokens as we process the string from left-to-right. All binary operators, no parentheses If we only had binary operators and…
-
LeetCode 59: Spiral Matrix II
link We simulate continuous spiral motion defined by directions and their boundaries. To get the matrix filled up, we piggyback on this motion by hopping in and hopping out at the right times. We hop in at while the motion has just moved from Up -> Right, with Up’s boundary changed from row_min = 0…
-
LeetCode 2022: Convert 1D Array Into 2D Array
link If we cannot fit all elements of original in the grid, we return empty. Otherwise, say we have already copied elements from the original. Then the next element would be at index of the original. If we translate into grid coordinates, . In other words, original[k] corresponds to grid[count-copied-rows * row-width][count-copied-columns]. We can copy…
-
LeetCode 463: Island Perimeter
link A 1-cell has four sides and it can contribute at most 4 to the perimeter. If a side is shared with another 1-cell, that side does not contribute to perimeter. So, for each 1 cell in the grid, we count how many of its four sides are shared and then add (4 – shared_count)…