Questions tagged [memoization]

In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs.

In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

This tag should be used for programs that are using memoization.

Source

Wikipedia

1491 questions
-2
votes
1 answer

implement a memoization function in JavaScript

The function that I need to implement should take a function to memoize, then return a new function that remembers the inputs to the supplied function. This is an exercise, for such is not required for the memoize function to remember all previous…
-2
votes
2 answers

I am getting 'list' object is not callable error for this code. I need to add zero before each element

a = [1,2,3,4] newarray = list(map(a,[0,*a])) print(newarray) output: 'list' object is not callable error expected: Add zero to each element in array
-2
votes
3 answers

Fibonacci Memoization return list from 0 to N

def fib(n, memo: Dict = {}): if n == 0 or n == 1: return n if n not in memo: memo[n] = fib(n-2, memo)+fib(n-1, memo) return memo[n] I have this function that uses memoization which returns the nth digit of the fibonacci…
-2
votes
2 answers

How can i reduce the time of execution of this code

var yourself = { fibonacci : function(n) { return n === 0 ? 0 : n === 1 ? 1 : this.fibonacci(n -1) + this.fibonacci (n-2) } }; This function is constantly setting the value of its 'fibonacci' property based on the arguement…
-2
votes
1 answer

Why does this solution works in JavaScript but takes too long in C++? (Dynamic Programming)

This is what my canSum function needs to do: Given a target sum x, return true iff it is possible to get that sum by adding elements from a given array, assuming array elements can be used any number of times. Examples: canSum(7, {2,3}) ->…
-2
votes
2 answers

Memoization and return statements

I am fairly new to python, stumbled upon a dynamic programming course on youtube that I thought would help me better understand the concept @ here. Now, I have been struggling to understand it... Would anyone have the time to explain the way this…
-2
votes
4 answers

Naive Recursion faster then Memoized recursion

I am a beginner to python, and was following this video for learning dynamic programming. For the case of finding a Fibonacci series, the tutor introduced memoization to improve the performance of the recursive algorithm. The complete code is as…
-2
votes
1 answer

How can I solve this problem using dynamic programming?

Given a list of numbers, say [4 5 2 3], I need to maximize the sum obtained according to the following set of rules: I need to select a number from the list and that number will be removed. Eg. selecting 2 will have the list as [4 5 3]. If the…
-2
votes
1 answer

function attribute lifespan

I'm writing a memoization of a function and I want to store an attribute in the function object. Will the function attribute be available for the lifespan of the process? if not how can I achieve such a thing? Thank you
kambi
  • 3,291
  • 10
  • 37
  • 58
-2
votes
1 answer

matrix max sum finding by removing the almost one row or almost one column

Given a matrix with m-rows and n-columns, finding the maximum sum of elements in the matrix by removing almost one row or one column Example: m=2, n=3 matrix : **[[1,2,-3] [4,5,-6 ] ]** output: 12 , by removing the third column then sum…
-2
votes
1 answer

Memoizing fibonacci sequence in python

I have to write a fib_memoize(n) function that should be recursive but it should memoize its answer so that it's running time is O(n). This is what I have: def fib_memoize(n): memo = {} if n in memo: return memo[n] else: …
user7732694
-2
votes
2 answers

recursion and multi dimension matrix in python

this is famous path counting problem , i am trying to solve it using memoization. Enlighten me! def pathCounter(a,b): matrix = [[0 for i in xrange(a)] for i in xrange(b)] if a==0 or b==0: return 1 if matrix[a][b]: …
user8530486
-2
votes
2 answers

Initializing a big array of a certain structure -- getting seg faults

The following code is giving me a seg fault. gdb says its coming from the line memos[j][k] -> cost = -1;, but I can't tell exactly what is wrong. I'm guessing something with how I am allocating memory and that the array index is mistakenly out of…
Craig
  • 835
  • 1
  • 7
  • 21
-3
votes
1 answer

How to make a static local variable in golang

/* Description: Write a function canSum(targetSum, numbers) that takes in a targetSum and an array of numbers as arguments. The function should return a boolean indicating whether or not it is possible to generate the targetSum using numbers from…
-3
votes
2 answers

Why I'm getting TLE even if I'm using memoization?

Here is the question statement Given two strings s1 and s2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without…
1 2 3
99
100