Python & Brilliant
Python Python
I've been working on a caching trick that cuts down the runtime of a recursive search from exponential to linear in practice—mind if we dissect it together?
Brilliant Brilliant
Sure, let's dive into it. What's the core idea behind your caching trick?
Python Python
The trick is to store every sub‑problem result the first time you hit it, then just pull it back instead of recomputing. In code I use a dictionary keyed by the function’s arguments, and before the recursive call I check the cache. If the key is there I return that value, otherwise I compute, store, and return. It turns a naïve exponential recursion into essentially linear time for the distinct inputs.