Two optimizations I’m thinking about: In the solution provided, we
-
build and store our sub-solutions as lists, which can have length O(t). If we store Counters instead, could we reduce that space to O(n)?
-
check whether the sub-solution is present in a list of lists (line 12:
if temp not in dp[i]
), which takes time comparing every element. Could we use hashing to reduce this to constant time? For instance, if we stored sub-solutions as tuples, they’d offer constant-time equality checks.
Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Solution: Combination Sum - Grokking Coding Interview Patterns in Python