Share Your Experience: Take the 2024 Developer Survey

# Questions tagged [dynamic-programming]

Dynamic programming is an algorithmic technique for efficiently solving problems with a recursive structure containing many overlapping subproblems.

298 questions
Filter by
Sorted by
Tagged with
311 views

### Codeforces: D2. Counting Is Fun (Hard Version)

The code works okay for the following problem. Problem An array ๐ of ๐ non-negative integers is said to be good if all the elements of ๐ can be made equal to 0 using the following operation some (...
• 235
1 vote
32 views

### Collecting inputs from a list of file names or stdin if there is no input files

In my program, I want to either read from a file or stdin depending on the arguments. I did this function to collect input: ...
• 111
924 views

### C++ - Secretary Problem using dynamic programming

I tried using dynamic programming to get a solution to the Secretary Problem. It seems to me it's working with with expected result. I really didn't want to use ...
• 153
1 vote
69 views

### 3ph 2l inverter simulation with SPWM

Here is a code for a simple model of three phase two level inverter with constant DC voltage source and three phase RL load. ...
• 21
1 vote
112 views

### Another ATMs cash-out (denomination) algorithm in Java

Related to this question and this answer, I would like to have a second review from you on a modified version. The problem I tried to solve Some kind of "Minimum count of numbers required from ...
280 views

### Sum of bitwise XOR of each subarray weighted by length

here is the problem statement You are given an array a of length n consisting of non-negative integers. You have to calculate the value of $\sum_{l=1}^n \sum_{r=l}^n f(l,r)\cdot (r - l + 1)$ where \...
1 vote
164 views

### Wrapping an ExpandoObject within a SafeExpandoObject

I've written code to deserialize a JSON into an ExpandoObject. The only problem with the ExpandoObject is that if you try to access a member which doesn't exist, it ...
• 111
1 vote
577 views

### Leetcode: 2327. Number of People Aware of a Secret

On day 1, one person discovers a secret. You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the ...
• 21
115 views

### Find largest sum not involving consecutive values

There is a question to basically find the largest sum in an array, such that no two elements are chosen adjacent to each other. The concept is to recursively calculate the sum, while considering and ...
183 views

### k-dice Ways to get a target value

I'm trying to solve the following problem: You have a k-dice. A k-dice is a dice which have k-faces and each face have value written from 1 to k. Eg. A 6-dice is the normal dice we use while playing ...
• 212
154 views

### Longest (more or less) ideal subsequence

An ideal subsequence is a subsequence of a string, where the distance between adjacent characters in the subsequence is bounded by a maximum, i.e. $\lvert c[i] - c[i\pm 1] \rvert \leq k$ Example: ...
1 vote
156 views

### Making my DP algorithm faster - longest palindromic substring

The following code is my solution to a LeetCode question - find the longest palindromic substring. My code is 100% correct, it passed once but it took too long, and in most of the reruns I hit a "...
• 113
1 vote
1k views

### Leetcode 55. Jump Game solution

I was working on Jump Game problem on leetcode Question You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your ...
• 189
1 vote
1k views

### Re-order table columns in HTML dynamically with Javascript

I've a table in HTML looks like this: Subjects n1 n2 n3 subject1 10 0 0 subject2 0 5 20 ...
• 523
105 views

### Segmentation of list to minimize the largest sum

I have written the following code for diving a list 'seq' into 'k' segments such that the maximum sum of each segment is minimized. I have used dynamic programming to solve this problem. But my class &...
84 views

### Find all non-crossing tuples which has the same content with a given tableau

Let T be a semistandard Young tableau of rectangular shape. For example, [[1,2,4],[3,5,6]] is such a tableau, where [1,2,4] and [3,5,6] are columns. All non-...
• 183
464 views

### make html table in sync with javascript array

I have an array of users containing user name with their rules on my practice project I have generated a table in HTML from that array in JS, So I can change the ...
• 523
262 views

### Longest Palindromic Substring | Python Code Giving TLE

Problem Statement Given a string s , return the longest palindromic substring in s. Constraints ...
• 138
118 views

### Coin partitions

I am looking to obtain the partitions of coins that sum to a target amount So I tried search online, but every single website loves to use this problem to show the benefits of dynamic programming. ...
• 5,725
1 vote
75 views

### Longest Increasing Subsequence of Rectangles

Background I was looking at some programming Olympiads the other day, and I found this ACM-ICPC Ukraine 2013 pdf. The part I am working on is Problem D, which consist of finding out how many rectangle ...
63 views

### RodCutting and learning Dynamic Programming

I tried to learn dynamic programming going from recursion to memoized version with the RodCutting problem: Returns the best obtainable price for a rod of length n and price[] as prices of different ...
• 313
136 views

### Bellman-Ford algorithm implementation

I have used an adjacency list to represent the graph. This implementation is based on the procedure given in the book 'Introduction to Algorithms'. ...
2k views

### HackerRank: Sam and substrings | How can dynamic programming be used in my code?

Given a number as a string, no leading zeros, determine the sum of all integer values of substrings of the string. Given an integer as a string, sum all of its substrings cast as integers. As the ...
• 147
1 vote
138 views

...
797 views

### Find maximum value of recursively-defined "fusc" function

I've been trying this question from SPOJ, which asks for the user to enter a number n, and they will receive a maximum fusc value that lies in between 0 to ...
831 views

### Divide array into three disjoint sets with equal sum

Problem definition : Array partition problem Given an array of positive integers, divide it into three disjoint subsets having equal sum. These disjoint sets cover the complete array. Example Input: [...
• 389
1 vote
235 views

### Target Sum array using Dynamic Programming

I'm learning Dynamic Programming and trying to solve this Target Sum array problem. I've to find an array of integers that sums to a given target sum using the integers of given input array. I've used ...
• 429
3k views

### Recursive solution of ordered Coin Combinations II (CSES)

Question Link Consider a money system consisting of n coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ordered ways you can produce a money sum x using ...
• 131
122 views

### Find ALL duplicate subtrees. using recursion

This is just a practice exercise, I'm trying to understand dynamic programming as deeply as I can. I solved this using recursion, though I am not sure if it will always work. If anyone could tell me a ...
• 121
1 vote
342 views

### Leetcode 377. Combination Sum IV Javascript Memoization attempt

Problem Statement ...
1 vote
6k views

### Patch a JSON object using dynamic / ExpandoObject with System.Text.Json

Background Recently, I was making some updates to an "older" library that would handle PATCH-style modifications to an object that is persisted in a JSON format on our document-storage ...
• 1,023
466 views

### Calculate sum of a substring

I try to solve some old Codeforces questions. The problem is; Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up q ...
• 177
221 views

### Killing a Hydra - Overengineered

Background This question is inspired by the question: Killing a hydra, and my response therein. I will restate the problem at hand in full so that this question is fully self contained You can only ...
• 5,725
220 views

### K Inverse Pairs Array - Brute Force

For an integer array nums, an inverse pair is a pair of integers [i, j] where ...
• 195
113 views

### Haskell implementation of Needleman-Wunsch

The code below is a Haskell implementation of Needleman-Wunsch algorithm for sequence alignment (and string edit distance). It's an experiment in trying to closely imitate the dynamic programming ...
• 33
1 vote
121 views

### I want reduce the time complexity for this ladder problem to find the minimum ladders required

I was trying to attempt below given challenge. I tried multiple ways to solve the problem but none of them were good enough to pass the time limit. Please advise if you have any idea to improve on ...
99 views

### Unique Paths II, dynamic programming problem, O(n^2) time, O(n^2) space

This is based on this leetcode question. I get the correct answer, but I know it could be much, much cleaner. I also know there is supposed to be an O(n) space solution, but I'm not sure how to ...
4k views

### Count the number of distinct subarrays

I want to determine the number of distinct subarrays that can form having at most a given number of odd elements. Two subarrays are distinct if they differ at even one position. The subarray is a ...
• 31
128 views

### Haskell Dynamic Programming on a Tree

Here is my shot at Homework 8 of CIS-194 (from Spring '13). The problem is about a hierarchy of employees at a company (a tree), each node being of type Employee, ...
• 125
313 views

### Efficiently calculate value of Pascal's Triangle using memoization and recursion

So reading about functional programming, applications to dynamic programming, and learning Scala. I figured I would try it out with a popular example, Pascal's Triangle, tests against known values ...
• 133
1 vote
64 views

### LeetCode: range sum query mutable c#

https://leetcode.com/problems/range-sum-query-mutable/ I am trying to solve this question using dynamic programming. I guess the guys in leetcode want to limit us to use segment tree or one of the ...
• 5,311
355 views

### Grid Dynamic Programming

atcoder.jp Problem Statement: There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the ...
163 views

### Searching for an idiomatic Rust implementation of Minimum Edit Distance (LeetCode #72)

I am solving LeetCode Dynamic Programming challenges (this one is #72, at https://leetcode.com/problems/edit-distance). Here below, I've implemented a Rust solution for the minimum edit distance ...
• 143
72 views

### Find a sum using just two different digits

The problem statement was pretty easy and I was able to run half of the test cases, but some of the test cases in the end gave me the error of memory limit reached. What can I do here to improve the ...
• 13
1 vote
284 views

### Divide a set into subset with some constraints

Note - I am not sure whether this question belongs to stackoverflow or CSE stackexchange or here Problem: Given a list Lconsisting of not necessarily unique non-...
• 161
134 views

### C++ - Longest Common Subsequence

Link to the Problem Here is my code to compute the length of the longest common subsequence of two integer arrays arr[] and brr[]...
309 views

### LeetCode: Stone Game C#

https://leetcode.com/problems/stone-game/ Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones ...
• 5,311
1 vote
1k views

### Code A Primitive Calculator ( x3,x2,+1) Using Dynamic programming [closed]

Question -You are given a primitive calculator that can perform the following three operations with the current number ๐ฅ: multiply ๐ฅ by 2, multiply ๐ฅ by 3, or add 1 to ๐ฅ. Your goal is given ...
• 21