Skip to main content
The 2024 Developer Survey results are live! See the results

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
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 ...
-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): ...
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 ...
0 votes
0 answers
21 views

Efficiently store a dynamic increasing dataset and make computation on it

I want to write a python program that will compute the price of different groceries baskets as their constituants change. I have a csv file with the constituents of each basket. PS: a basket can ...
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 ...
0 votes
3 answers
125 views

Python lambda function in a list gives unexpected result

So I'm trying to make a list where a lambda functions are the elements of the list. The lambda function calls another function which I pass an argument to. The problem is that lambda function only '...
-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 ...
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
2 answers
2k views

Maximum difference of sum of odd and even index elements of a subarray

Given an array of N integers (can be positive/negative), calculate the maximum difference of the sum of odd and even indexed elements of any subarray, assuming the array follows 0 based indexing. For ...
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 ...
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
109 views

Dynamic Programming - Minimum cost to fill given weight in a bag

We are given an array of positive weights cost where cost[i] represents the cost of filling i + 1 kg's of oranges in a bag (0-based indexing). We need to find the minimum cost to buy exactly w kg's of ...

15 30 50 per page
1
2 3 4 5
380