dynamic-programming

Return all possible placements of buildings in a city grid using backtracking

Return all possible placements of buildings in a city grid using backtracking Question: I have two inputs An NxM city grid where empty spaces represent vacant lots, and X’s represent filled lots e.g X XXX X X XXXX XX X This is in the format List[List[str]] And an integer that represents the number of buildings …

Total answers: 2

Find the maximum cost path from the bottom-left corner to the top-right corner

Find the maximum cost path from the bottom-left corner to the top-right corner Question: Hi, everybody! I try to find best solution with assignment in title. But I don’t understand how I can output way of calculation. I write python program, it can output random massive and max sum, but I need way too. from …

Total answers: 2

Python Solution for 392. Is Subsequence

Python Solution for 392. Is Subsequence Question: I have solved solution 392 on LeetCode and one of the topics listed for it is Dynamic Programming. Looking at my code and other solutions online, I wonder what part of the solution is categorized as pertaining to Dynamic Programming. I would appreciate it if someone could enlighten …

Total answers: 1

Find good days to rob the bank – some test cases failing

Find good days to rob the bank – some test cases failing Question: I came across the LeetCode problem 2100. Find Good Days to Rob the Bank: You and a gang of thieves are planning on robbing a bank. You are given a 0-indexed integer array security, where security[i] is the number of guards on …

Total answers: 1

Finding minimum number of steps to reach (x,y) from (1,1) : we can increment number by using condition (x,y+x)or(x+y,x)

Finding minimum number of steps to reach (x,y) from (1,1) : we can increment number by using condition (x,y+x)or(x+y,x) Question: a = 1 b = 1 x=int(input()) y=int(input()) def minsteps(x,y): if x==a and y==b: print(1) return 1 if x<a and y<b: print(2) return 20 count = 1 + min(minsteps(x,x+y),minsteps(x+y,y)) return count print(minsteps(x,y)) Test case: (3,2) …

Total answers: 3

Dynamic programming solution inappropriate for change-making problem?

Dynamic programming solution inappropriate for change-making problem? Question: I’m curious why my approach to the change-making problem isn’t succeeding. The logic makes sense to me, so I’m not sure where the failure is. def count_change(denoms, denoms_length, amount): """ Finds the number of ways that the given number of cents can be represented. :param denoms: Values …

Total answers: 2

Returning value in a nested function when using memoization

Returning value in a nested function when using memoization Question: 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 figure out why the count (in this case the number of times this …

Total answers: 1

How to find subsets from a set that product equals the target?

How to find subsets from a set that product equals the target? Question: Let us say we have a list and target of: list: [1,2,3,4,5] and target: 20 and we want to find total combinations to reach this, with multiplication, which is: [1,4,5], [4,5], [2,5,2], [1,2,2,5] I did this code, but I can’t seem to …

Total answers: 3

Dynamic Programming: Smallest cost path through matrix — memoization?

Dynamic Programming: Smallest cost path through matrix — memoization? Question: 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, I’m not sure what the best approach is. Here’s the original function, which works, …

Total answers: 3