15% off all plans & roadmaps Use code
See plansLeetCode 494 · Solution & editorial
You are given an integer array nums and an integer target.
Target Sum is a Dynamic Programming (DP) Patterns problem, in the 1D Array (0/1 Knapsack Subset Sum Style) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
Solve 0/1 knapsack, partition equal subset sum, and target sum problems.
The reference solution runs in O(n * S) time and O(S) space.
Target Sum has been reported in interviews at Adobe, Amazon, Bloomberg, Facebook, Google, Microsoft.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Target Sum is a Dynamic Programming (DP) Patterns problem, in the 1D Array (0/1 Knapsack Subset Sum Style) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.