1,530 questions
529 votes
15 answers
318k views
What is memoization and how can I use it in Python?
I just started Python and I've got no idea what memoization is and how to use it. Also, may I have a simplified example?
3 votes
2 answers
165 views
Use of !! in memoization
When looking up memoization in Haskell, I found this bit of code: memoized_fib :: Int -> Integer memoized_fib = (map fib [0 ..] !!) where fib 0 = 0 fib 1 = 1 fib n = ...
369 votes
12 answers
153k views
What is the difference between memoization and dynamic programming?
What is the difference between memoization and dynamic programming? I think dynamic programming is a subset of memoization. Is it right?
0 votes
1 answer
154 views
Memoize function repeatedly called with same arguments?
I have a mathematical function called in render loop (240Hz). It's a function of some (atomic) state that very rarely changes. It calls 3 times to std::log, twice to std::sqrt, once to std::sin. When ...
298 votes
20 answers
282k views
Is there a decorator to simply cache function return values?
Consider the following: @property def name(self): if not hasattr(self, '_name'): # expensive calculation self._name = 1 + 1 return self._name I'm new, but I think the ...
234 votes
21 answers
206k views
How to determine the longest increasing subsequence using dynamic programming?
I have a set of integers. I want to find the longest increasing subsequence of that set using dynamic programming.
279 votes
10 answers
236k views
What is the difference between bottom-up and top-down?
The bottom-up approach (to dynamic programming) consists in first looking at the "smaller" subproblems, and then solve the larger subproblems using the solution to the smaller problems. The top-down ...
146 votes
11 answers
99k views
Caching class attributes in Python
I'm writing a class in python and I have an attribute that will take a relatively long time to compute, so I only want to do it once. Also, it will not be needed by every instance of the class, so I ...
80 votes
15 answers
42k views
memoize to disk - Python - persistent memoization
Is there a way to memoize the output of a function to disk? I have a function def getHtmlOfUrl(url): ... # expensive computation and would like to do something like: def getHtmlMemoized(url) = ...
2 votes
2 answers
102 views
How to avoid re-render in Redux based on the outputs of .filter()?
I have a simple scenario in which I want to access my store.entities, but only those which are active. I understand the following is bad because a new reference is given each time: const ...
0 votes
2 answers
90 views
Flattening an array causes UI freeze
I have an app with cards. Cards are fetched with useInfiniteQuery from React Query. I fetch new card each time I reach the end of the list. Backend sends offset and limit based responses of this ...
1 vote
2 answers
164 views
What is the time complexity of the following algorithm and is there any optimization I can do?
Problem: Given an array that sums to 0, find the maximum number of partitions of it such that all of them sum up to 0 individually. Example: input: [-2, 4, -3, 5, -4] output: 2 (which are [[5, -2, -3]...
0 votes
1 answer
105 views
Storing the recursive call result in a variable leads to incorrect calculation in a DP memoized solution
Just by using the commented code instead of making the recursive calls inside the max function, leads to incorrect result particularly with the following test case int main() { Solution obj = ...
0 votes
0 answers
55 views
Valid Parenthesis String, Recursion with memoization to DP
How can the following recursion + memoization code be converted into a tabulation format dynamic programming solution? The code is working but I want to improve it. The challenge I am facing is ...
151 votes
8 answers
31k views
Memoization in Haskell?
Any pointers on how to solve efficiently the following function in Haskell, for large numbers (n > 108) f(n) = max(n, f(n/2) + f(n/3) + f(n/4)) I've seen examples of memoization in Haskell to ...