Best Time to Buy and Sell Stock:Track the Floor, Not Every Pair of Days
One buy then one later sell is a running minimum and a running best profit. Trying every pair of days is correct and too slow; you cannot sell before you buy.
Read More45 questions
One buy then one later sell is a running minimum and a running best profit. Trying every pair of days is correct and too slow; you cannot sell before you buy.
Read MoreA target in a sorted array is a shrinking lo/hi window. A linear scan is honest and too slow; mid must not overflow, and the loop invariant has to say which side still holds the answer.
Read MoreThe ways to reach step n are the ways to n-1 plus the ways to n-2. Recursing the full tree is exponential; two rolling integers are enough.
Read MoreOne pass into a HashSet answers whether any value repeats. Nested comparison is honest and quadratic; sorting works but permutes an array you were not asked to reorder.
Read MoreAn element that appears more than n/2 times can be found by cancelling other votes. A frequency map is honest extra space; sorting finds the middle slot and still permutes the array.
Read MoreTwo sorted tails merge into nums1 if you write from the far end. Copying then sorting throws away the order; writing from the front overwrites values you still need.
Read MoreOne missing value in 0..n is the XOR of every index with every value — or a closed-form sum. Sorting is honest and slower; a set is honest extra space.
Read MoreZeros belong at the end without reordering the live values. A write pointer compacting non-zeros is one pass; swapping blindly can scramble order if you are not careful.
Read MoreFind two indices that add to a target in one pass with a HashMap. Nested search is honest and too slow; sorting loses the indices you were asked for.
Read MoreTriplets that sum to zero are a sorted array, a fixed first index, and two pointers on the rest. A nested map forgets that this prompt wants unique value triples, not indices.
Read MoreThe fewest coins for an amount is unbounded DP on remaining value. Greedy largest-first fails on some coin sets; the algorithms post owns the combination count, this writeup owns the min-count interview.
Read MoreCombinations that sum to a target reuse a coin by recursing at the same index. Starting every coin from 0 without an order dumps permutations of the same multiset.
Read MoreMax area between two vertical lines is two pointers from the ends. Nested pairs are correct and quadratic; the width only shrinks, so the short wall is the one that has to move.
Read MoreThe first and last index of a target in a sorted array are two binary searches, not a scan from a single hit. Finding any match and walking outward is linear in the worst run of duplicates.
Read MoreA rotated sorted array has one drop. Binary search asks which side of mid still contains that drop; scanning for the min throws the remaining order away.
Read MoreAny local peak is enough, and a binary search on the slope finds one in log n. A linear scan is honest; you do not need the array to be globally sorted.
Read MoreA value in 1..n that appears twice is the entrance of a cycle if you treat nums[i] as the next index. Sorting or a set finds it and breaks the extra-space / in-place rules this prompt usually adds.
Read MoreA unique viable start on a circular gas loop is the station after the most negative tank prefix — if total gas covers total cost. Restarting a full lap from every index is n circuits.
Read MoreThe best take through house i is skip it or take it plus the best through i-2. Adjacent houses are forbidden; a 2^n subset search is the honest brute.
Read MoreA new range in an already-sorted disjoint cover is a linear splice: copy the left, merge the overlap, copy the right. Sorting from scratch throws away the invariant you were given.
Read MoreThe fewest jumps to the last index is the number of reachability layers. Jump Game only asked if you can arrive; here each layer is one jump, and the farthest in the layer is the next wall.
Read MoreWhether you can reach the last index is a running farthest landing. Exploring every jump tree is correct and exponential; you only need to know if the next index still sits inside the best reach.
Read MoreThe slowest speed that still finishes by hour h is a yes/no check over a monotonic range. Trying every speed from 1 is linear in the max pile; the piles do not need to be sorted.
Read MoreThe longest streak of consecutive values is a set membership test, not a sort. You only grow a run from a number whose predecessor is missing — otherwise you recount the same streak.
Read MoreThe length of a strictly increasing subsequence is patience sorting on tails, or n² DP. It is not a subarray; contiguous max is a different question.
Read MoreThe best contiguous product needs the worst product ending here as well — a negative times a negative can become the answer. Kadane on sums does not survive a sign flip.
Read MoreThe best contiguous sum is Kadane: extend the run or start over at this index. Trying every slice is correct and quadratic; this is not a subsequence.
Read MoreOverlapping ranges collapse after one sort by start. Pairwise merging until the list quiets is correct and slow; two intervals overlap when the next start is not after the current end.
Read MoreThe fewest removals so ranges do not overlap is interval scheduling: sort by end, keep a range only if it starts at or after the last keep. Merging overlaps is a different job.
Read MoreEvery ordering is a backtracking swap (or a used-bit) and an undo. Nested loops only work for a fixed k; n! does not fit in three fors.
Read MoreEach index wants the product of everyone else. Two prefix/suffix passes fill the output in linear time; division collapses the first time a zero shows up.
Read MoreA right rotate by k is three reverses after k mod n. Copying into a second array is honest extra space; the reverse trick stays in place.
Read MoreA 90-degree clockwise turn is a transpose plus a row reverse. Allocating a second matrix is honest extra space; in place you cycle four cells or layer the two reflections.
Read MoreA matrix whose rows continue the sorted order is one binary search on a virtual 1D index. Scanning row by row throws away the global order; a per-row search is log n times too many starts.
Read MoreA rotated sorted array still has a sorted half at every mid. Binary search goes into the half that can legally hold the target; a linear scan throws the remaining order away.
Read MoreA zero infects its row and column. Extra boolean arrays are honest; the first row and first column can hold those marks so you do not allocate.
Read MoreAn array of 0, 1, and 2 partitions in one pass with a low, a mid, and a high pointer. Counting then rewriting is two passes; a full sort is the wrong primitive.
Read MoreSpiral order is four walking bounds that close in. A visited matrix works and wastes space; the walls already know which cells remain.
Read MoreThe power set is a take-or-skip walk (or a start-index loop) that snapshots the path. Generating by bit masks is the same 2^n work with worse interview talk.
Read MoreA sorted pair-sum is two pointers from the ends. The HashMap from unordered Two Sum still works and wastes space; nested search ignores the order you were given.
Read MorePaths to a grid cell are paths from the cell above plus paths from the cell to the left. Recursing every route is exponential; a rolling row is enough.
Read MoreA board is valid when no filled digit repeats in a row, a column, or a 3x3 box. Solving the puzzle is a different question; one scan with three membership tests is enough.
Read MoreThe median of two sorted arrays is a cut: every value on the left is <= every value on the right. Merging is linear; binary search the cut on the shorter array.
Read MoreThe max of each window of length k is the front of a deque of useful indices. Rescanning k cells per start is correct and too slow; anything smaller than the incoming value can never be the answer.
Read MoreEach index holds water up to the min of the tallest bar on its left and on its right, minus its own height. Nested scans of those peaks are quadratic; two pointers carry both skylines in one pass.
Read MoreRepresentation and operations — not the problem set.