Questions tagged [dynamic-programming]
Dynamic programming is an algorithmic technique for efficiently solving problems which have recursive structure with many overlapping subproblems. Do NOT use this tag for general "dynamic" behavior in code.
dynamic-programming
5,690
questions
1
vote
1
answer
24
views
Maximizing sum of signed numbers by not skipping more than one consecutive value: DP goes wrong
I am trying to solve this code challenge:
A user maintains a list of negative and positive ratings for movies in a collection. The user wants to choose some subsequence of movies from the collection ...
0
votes
0
answers
12
views
Finding Longest Palindromic Substring, solution is too inefficient but I am not convinced dp is more efficient in this case
I have a solution which is O(n^2).
My idea loop through each character of the given string and find longest possible odd and even substring which results in a palindrome.
Why does my solution take so ...
3
votes
2
answers
106
views
A more efficient algorithm in finding the number of distinct decompositions a number has using only repdigits
This is a problem from a previous semester that I am trying to upsolve. I am trying to solve a problem involving the total number of ways of decomposing a number using only repdigits. A repdigit is a ...
-2
votes
0
answers
32
views
Dynamic Programming for Minimum Operations to Equalize Arrays - Incorrect Results
I'm trying to solve a problem where we need to find the minimum number of operations required to make two integer arrays nums and target (same length) equal. In one operation, we can select a subarray ...
-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
1
answer
34
views
Performance Comparison Between Tabulation and Memoization for Dynamic Programming [closed]
I'm delving into dynamic programming and trying to understand the general performance differences between tabulation and memoization. Both techniques aim to optimize recursive algorithms by storing ...
-1
votes
2
answers
96
views
Flip consecutive zeroes to ones in k operations to have maximum number of ones, find the maximum number of ones
You are given a binary string made of 0 and 1, and a value k which represents number of operations. You can flip consecutive 0s in each operation to 1s. Find the maximum number of 1s after k ...
1
vote
4
answers
81
views
The minimum size of substrings for the presence of at least one character
i have a question that want the minimum length of k that in each consecutive substring of a string with length k, There must be at least one common character.
for example if s="abcaca"
for k ...
1
vote
1
answer
94
views
Single Source - Single Destination Shortest Path (DFS + DP) - Time complexity?
Context:
I want to write an algorithm for finding the shortest(least cost) path from a fixed source to a fixed destination node in a weighted directed graph with non negative weights (can have cycles)....
0
votes
2
answers
62
views
Understanding dynamic programming timeouts with 2 different solutions
The problem is from leetcode (link - https://leetcode.com/problems/the-number-of-ways-to-make-the-sum/description/ )
You have an infinite number of coins with values 1, 2, and 6, and only 2 coins with ...
2
votes
0
answers
48
views
Coin Change Recursive, memoization gone wrong
I'm trying to understand why first one fails and second works, all I'm doing is passing along coinTrack, call it redundant but why does it not work?
It works without memoization, but on using ...
-1
votes
1
answer
48
views
Coin Change II : To take a value or not to
The problem goes like this:
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the number of ...
2
votes
4
answers
101
views
Is there a way initialize a matrix(2D array) with some default values(-1)
I was going through a dp problem, there I have to initialize the matrix with default(-1) values for memoized solution. And felt the need.
We can initialize the 1D array in java like this => Arrays....
0
votes
1
answer
20
views
Recusion: does the order of safety checks vs base cases matter?
I am learning dynamic programming, and I have observed when creating recursive code, sometimes we check out-of-bounds conditions first then check other base cases, and sometimes we check the other ...
1
vote
1
answer
140
views
Asteroid Mining - Dynamic Programming problem
I was given the following assignment:
There is n gram of mineral to be dug up on a certain asteroid. At the beginning there is only one robot at our disposal. Each robot can do one of two activities ...