Skip to content

Commit dd2fd33

Browse files
author
Joseph Luce
authored
Update 494_target_sum.md
1 parent 529c024 commit dd2fd33

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

leetcode/medium/494_target_sum.md

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 494. Target Sum
22

33
## Recursive Brute Force
4-
- Runtime: 2^N
4+
- Run-time: 2^N
55
- Space: 2^N
66
- N = Number of elements in array
77

@@ -12,34 +12,42 @@ Noticed how the run-time is not big O of 2^N, its because this brute force will
1212
class Solution:
1313
def findTargetSumWays(self, nums: List[int], S: int) -> int:
1414
15-
def find_sum_ways_helper(nums, curr_sum, start_i):
16-
if curr_sum == S and start_i >= len(nums):
15+
def sum_helper(curr_sum, idx):
16+
if curr_sum == S and idx >= len(nums):
1717
return 1
18-
elif start_i >= len(nums):
18+
if idx >= len(nums):
1919
return 0
20-
n_sums = 0
21-
n_sums += find_sum_ways_helper(nums, curr_sum + nums[start_i], start_i+1)
22-
n_sums += find_sum_ways_helper(nums, curr_sum - nums[start_i], start_i+1)
23-
return n_sums
20+
return sum_helper(curr_sum+nums[idx], idx+1) + sum_helper(curr_sum-nums[idx], idx+1)
2421
25-
return find_sum_ways_helper(nums, 0, 0)
22+
return sum_helper(0, 0)
2623
```
2724

2825
## Iterative Solution with Map
29-
- Runtime: O(N^2)
30-
- Space: O(N^2)
26+
- Run-time: O(2^N)
27+
- Space: O(2^N)
3128
- N = Number of elements in array
3229

3330
We can use a dictionary to keep track of the sums and how many paths there are for each sum.
3431
We just need to maintain a rolling dictionary as we traverse across the numbers.
3532
Each traversal we will create new sums and add them into a new dictionary.
3633
We will move the values across from the old dictionary as well.
3734

38-
For the run-time, you may think that the run-time hasn't changed.
39-
Why is this an improvement?
40-
An input like [1,10,100,1000,10000...] will achieve N^2 run time.
41-
However, given any other input, since its add and subtract and not multiply or divide, its unlikely and its more likely we will have overlapping sums.
42-
So the run time is actually less than O(2^N) on most cases while the brute force solution above will always be ran at 2^N.
35+
The dictionary is used to exploit the fact that there can be overlapping sums.
36+
You can imagine the dictionary used for each height/level of the recursion tree, gathering all the sums from the previous summation and reusing it to recalcuate for the current height.
37+
38+
```
39+
Sums for each height, Key: sum, Val: n_paths
40+
1 {1: 1, -1: 1}
41+
+/ \-
42+
1 1 {2: 1, 0: 2, -2: 1}
43+
+/ \- +/ \-
44+
1 1 1 1 {3: 1, 1: 3, -1: 3, -3: 1}
45+
```
46+
47+
You may think to yourself that the run-time hasn't changed.
48+
You are correct, if given a set of numbers that would create unique sums for each height of the tree, this would end up being O(2^N).
49+
However, since this question is done with addition and subtract, it is more likely there will be overlapping sums.
50+
So the run-time is actually less than O(2^N) for the average case.
4351

4452
```
4553
from collections import defaultdict

0 commit comments

Comments
 (0)