All Questions
Tagged with dynamic-programming python-3.x
137
questions
-2
votes
0
answers
36
views
There a DP code for question edit distance from CSES problem set and i solved using python in most optimized way but its giving TLE [closed]
import sys
input = sys.stdin.read
def edit_distance(s1, s2):
n1, n2 = len(s1), len(s2)
prev_row = list(range(n2 + 1))
curr_row = [0] * (n2 + 1)
for i in range(1, n1 + 1):
...
0
votes
2
answers
55
views
dynamic programming: (minimum days of not eating icecreams)
Minimum Days Not Eating Ice-cream
Ram decides to eat ice-cream for N days. On each day the ice-cream shop can have chocolate flavour or mango flavour or both or none. The availability of ice-cream ...
0
votes
1
answer
26
views
Number of hits in fibonacci using lru_cache
I am using functools.lru_cache to implement Fibonacci using memoization. But I do not understand the number of hits that my output generates. My code is as follows:
@lru_cache(maxsize=8)
def fib(n):
...
1
vote
3
answers
80
views
Update every nth element of given a range
I have N=12
and between 1 to 12 (in N) I want every 3 elements to be updated.
For example:
I am calculating the current hour(say 6am) and add 4 hours to start with "6-10" for the 1st 3 rows ...
0
votes
1
answer
150
views
Trouble Solving Maximum Product Subarray
I am trying to solve LeetCode problem 152. Maximum Product Subarray:
Given an integer array nums, find a subarray* that has the largest product, and return the product.
The test cases are generated ...
0
votes
0
answers
54
views
Dynamic Programming code to return the shortest way to sum numbers from an array to a target integer does not return the correct answer
I am following a yt video on Dynamic Programming. The problem statement is: "Given an array nums, find the shortest combination of numbers (repetition allowed) from it that sums upto a given ...
0
votes
0
answers
32
views
GIven a list of locations of residents, need to find K locations such that it minimises the sum of abs diff b/w the selected location and A[i]
following Question is assigmnet for my course in college
Problem statement
Following is the code i tried for the question.
from math import ceil, floor
n = 0
def cost(i, j, p,c):
if (i + j) % ...
1
vote
1
answer
93
views
Tabulation (Dynamic Programming) using Decorator
Find the Factorial of a number by tabulation using decorators
def factorial(n):
if n<1:
return 1
else:
f=[0]*(n+1) #creation of the array/list#
f[0]=1 ...
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
...
1
vote
3
answers
80
views
Every Potential Combination of a Number from a List
I'm attempting to generate all conceivable combinations of a given number using elements from a list. For instance, when the number is 4 and the list contains [1, 2], there are 3 possible combinations:...
5
votes
1
answer
271
views
Is there any true general pattern to solve any dynamic programming problems?
I understand there is a general pattern to solve dynamic programming problems. Consists of:
Identify Overlapping Subproblem by Breaking down the problem into smaller subproblems that are solved ...
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 ...
0
votes
0
answers
102
views
How to find out phonenumbers based on (chess) knight moves
So basically in this problem we have a knight (from chess) and a keypad from a phone. Using the possible moves for knight (i.e. 'L' shape) we have to find the total count of phone numbers that can ...
0
votes
1
answer
29
views
How the local instance is not reset during an recursive call in python
I was practicing a problem to find the nth number of a Fibonacci series using dynamic programming.
Here is my python code:
def fib(n,memo=[1,1]):
if n<=len(memo):
return memo[n-1]
...
0
votes
1
answer
322
views
How to edit the parameter in the decorator dynamically in the runtime in Python
can anyone help me to make the "FROM" variable dynamic in the decorator in the runtime?
The script is a message forwarder, in the beginning it is working with the Chat IDs I provide in FROM ...