Grokking Dynamic Programming Patterns for Coding Interviews
Vote

0% completed

​

Solution: Count of Subset Sum

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

class Solution: def countSubsets(self, num, s): def dp(index, acc, memo): if (index, acc) in memo: return memo[(index, acc)] if acc == s: return 1 if index == len(num): return 0 result = dp(index + 1, acc, memo) + dp(index + 1, acc + num[index], memo) memo[(index, acc)] = result return result return dp(0, 0, {})
A

Anonymous

· 4 years ago

For "Top-down Dynamic Programming with Memoization", is there a good explanation on using the currentIndex as key first, then sum as key? Why currentIndex should be first?

The alternative would be to use sum as first key and then currentIndex as key.

Reading Progress

0%


Vote for new content