Dashboard
LC #494

Target Sum

Pattern · ?AmazonGoogleMeta

Problem

Assign + or - to each number to reach a target sum. Count the number of ways.

Constraints

  • 1 ≤ nums.length ≤ 20
  • 0 ≤ nums[i] ≤ 1000
  • 0 ≤ sum(nums[i]) ≤ 1000
  • -1000 ≤ target ≤ 1000

Example

Input
nums = [1, 1, 1, 1, 1], target = 3
Output
5
Why
-1+1+1+1+1, 1-1+1+1+1, 1+1-1+1+1, 1+1+1-1+1, 1+1+1+1-1 = 5 ways

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗