Many Hard Leetcode Problems are Easy Constraint Problems Use the right tool for the job. --- Introduction The author reflects on a common interview problem—the change counter problem—and explains that although dynamic programming is often required for an optimal solution, using a constraint solver like MiniZinc can simplify these problems drastically. --- Example: Change Counter Problem Problem: Given coin denominations, find the minimum number of coins required to make change. The greedy algorithm may fail for some denominations (e.g., [10, 9, 1]). Instead of coding dynamic programming, one can use a constraint solver: MiniZinc can be tried online and quickly finds the optimal number of coins. --- General Insight Many tricky algorithmic interview problems are essentially mathematical optimization problems—maximizing or minimizing a function subject to constraints. These are often challenging to solve by hand but well-suited for constraint solvers like MiniZinc, Z3, or OR-Tools. Hard Leetcode problems = easy constraint problems --- More Examples Stock Profit Maximization Problem: Maximize profit by buying and selling a stock once. Sum/Subtraction to Zero with Three Numbers Problem: Check if three numbers in a list can be added or subtracted to equal zero. Largest Rectangle in Histogram Problem: Given bar heights, find the largest rectangle area. --- Why Use Constraint Solvers? They avoid complex bespoke algorithm design. Although runtimes are less predictable and often slower than optimal algorithms, they outperform naive approaches. They easily adapt to new or more complex constraints. Example: > Maximize profit by buying/selling up to maxsales stocks, only one trade at a time, and holding up to maxhold stocks simultaneously. This problem is much more complicated to solve algorithmically but only slightly more complex to state in a constraint solver. --- Conclusion Online examples mostly focus on puzzles like Sudoku or alphametic problems. Using constraint solvers to tackle leetcode-style algorithmic problems is a compelling approach. -