Skip to main content

All Questions

0 votes
1 answer
71 views

Why is this DFS + Memoization solution for selecting indices in two arrays too slow while a similar approach is more efficient?

I was solving LeetCode problem 3290. Maximum Multiplication Score: You are given an integer array a of size 4 and another integer array b of size at least 4. You need to choose 4 indices i0, i1, i2, ...
souparno majumder's user avatar
0 votes
1 answer
84 views

How is it possible to memoize longest divisible subset using only the length (and end position)?

The goal is to find the largest divisible subset. A divisible subset is a subset s.t. for every pair of elements i, j, either i is divisible by j or j is divisible by i. The general approach to solve ...
Alec's user avatar
  • 9,643
2 votes
1 answer
69 views

Issue with Memoization in Recursive Function for Finding Combinations Summing to Target

I need to write the following function: Write a function that takes in a target (int) and a list of ints. The function should return a list of any combination of elements that add up to the target, ...
CStudent's user avatar
  • 158
0 votes
1 answer
274 views

LeetCode - Minimum Falling Path Sum - question on memoization

I am trying to solve this leetcode problem: https://leetcode.com/problems/minimum-falling-path-sum/description Given an n x n array of integers matrix, return the minimum sum of any falling path ...
Hemanth Annavarapu's user avatar
0 votes
0 answers
510 views

Longest Stable Subsequence - Recover solution from memo table

I am working on a DP problem to find longest stable subsequence, and I'm currently stuck with recovering the solution. Here is the problem statement I have tried the below solution, def computeLSS(a):...
stack underflow's user avatar
1 vote
1 answer
269 views

DP array 0th element is initialized as 1 instead of 0

I encountered the LeetCode climbStairs problem: You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb ...
Hari Prasad's user avatar
1 vote
0 answers
22 views

Inconsistent output on leetcode and vs code [duplicate]

class Solution: def combinationSum4(self, nums: List[int], target: int,memo={}) -> int: if target in memo: return memo[target] if target==0: return 1 ...
Kaustubh's user avatar
0 votes
1 answer
51 views

Python acting in a strange way (Dynamic Programing) [duplicate]

I have written this following code that takes an integer N and and array of integers: arr. The task is to find the shortest combination of the integers in arr that sum up to N . Each elements of arr ...
Non_CS_Dude's user avatar
0 votes
1 answer
245 views

Problem in Memoization part! Leetcode problem 121. Best Time to Buy and Sell Stock

I was trying to solve this problem, I know it can be done by one simple for loop but I wanna try doing it using dp. So can anyone help in the memoization part? Problem: You are given an array prices ...
Muhammad Umer Farooq's user avatar
-1 votes
1 answer
91 views

Why does timeit results in almost constant time for all memoization runs?

We compute the Fibonacci number: def fibo_memo(i, memo={}): if i <= 0: return 0 elif i == 1: return 1 elif i in memo: return memo[i] else: memo[i] = ...
THN's user avatar
  • 3,631
-1 votes
1 answer
60 views

Why there is no difference between passing in the memo dict and not passing it? [duplicate]

So I have this memoized algorithm for the Fibonacci sequence and I faced something a little strange, there is no impact on the time complexity if I pass the memo dict in line 4, or if I don't, I want ...
Haidara's user avatar
  • 33
0 votes
0 answers
64 views

Memoization in DP problem to create all possible expressions by using symbol + / -

I was solving target sum problem on leetcode which asks us to use either +/- symbol on all integers and then check sum of array is equal to target. I'm trying to return all possible combinations using ...
Amey-K27's user avatar
1 vote
1 answer
89 views

Why two seemingly similar implementations of the same DP algorithm differ?

I'm implementing a dynamic programming (DP) solution to an "unbounded knapsack"-family problem. To be more precise, the problem is to find all possible change for the given amount having a ...
RomanGirin's user avatar
3 votes
1 answer
154 views

Returning value in a nested function when using memoization

I am trying to implement a count variable in the function below using dynamic programming specifically memoization. The method calculates the value in a Fibonacci sequence at a given index. I cannot ...
Faisal Khan's user avatar
0 votes
3 answers
324 views

Dynamic Programming: Smallest cost path through matrix -- memoization?

Is there a simple way to memoize results? my dynamic programming solution is potentially calling the same function, with the same parameters, multiple times. I think memoizing would add speed. However,...
jbuddy_13's user avatar
  • 1,248

15 30 50 per page
1
2 3 4 5