5,714 questions
-3
votes
0
answers
58
views
Can we solve the following optimization problem by dynamic programming [closed]
Suppose we have an 𝑚*𝑛 grid, let 𝑖=0,...,𝑚−1 and 𝑗=0,...,𝑛−1. For each grid point we have an array 𝑆𝑖𝑗 of size 𝑁 that serves as local score function. We want to to find a way that assigns ...
-3
votes
0
answers
59
views
Cross-Platform Quantum Bitmask Pathfinding Challenge [closed]
You are given a N×NN×N 2D grid representing possible quantum states, where each cell contains a 32-bit unsigned integer. Your task is to find a continuous path from the top-left (0,0) to bottom-right (...
6
votes
2
answers
303
views
Efficient algorithm to count contiguous subarrays that can form arithmetic progressions
I'm working on a problem where I need to count, for each possible common difference D, the number of contiguous subarrays whose elements can be rearranged to form an arithmetic progression with common ...
0
votes
0
answers
74
views
Why this code for 0/1 knapsack problem works for some examples?
Problem Statement
There are N items, numbered 1, 2, …, N.
For each item i (1 ≤ i ≤ N):
It has a weight w[i]
It has a value v[i]
Taro wants to choose some of these N items and carry them home in a ...
2
votes
1
answer
194
views
how to decide forward or backward computation in dynamic programming tabulation
I do not know when to use backward or forward computation for tabulation in solving a dynamic programming problem.
I would like to know the thinking process to decide which one to use or which one ...
-4
votes
1
answer
280
views
How to count the number of subset partitions of {1..n} into two equal-sum subsets (modulo 10⁹+7) [closed]
I'm working on a combinatorics problem where I need to compute how many ways the set {1, 2, ..., n} can be divided into two subsets with equal sum, such that every number from 1 to n is in exactly one ...
0
votes
1
answer
113
views
Time complexity of dynamic programming
I'm looking at one of the solutions to the programming problem: Partition a Set into Two Subsets of Equal Sum. The programming problem: Given an array arr[], the task is to check if it can be ...
1
vote
1
answer
106
views
Anyone know the right algorithm? [closed]
Does anyone know any LeetCode, Codeforces or anything like the following programming problem?:
Imagine you have "n" slots, from 1 to "n" with "s" being your starting ...
9
votes
2
answers
264
views
Given Input strings A, B and C, determing if C is a valid shuffle of A and B
A valid shuffle of two strings A and B is when their characters are mixed together to form a bigger string and all characters are used and the string is such that the order of characters in A and B is ...
-1
votes
1
answer
69
views
What is correct path tracking in Floyd-Warshall algorithm?
There are several descriptions of the Floyd-Warshall algorithm, including the one in Wikipedia where path tracking logic described like in the pseudo-code below (copy from Wikipedia https://en....
0
votes
1
answer
89
views
Why does int in my DP array cause overflow in Coin Change II problem?
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<unsigned int > dp(amount+1, 0);
dp[0]=1;
for (int value: coins){
...
5
votes
2
answers
133
views
Getting all distinct subsets from subset sum problem with target T using Dynamic Programming
In the classic subset sum problem, there are a set S and a target t, and the goal is to find a subset of S whose sum is t. This variant has a pseudo-polynomial time solution.
In a variant of the ...
6
votes
1
answer
178
views
Struggling with bottom up approach for This Kattis Problem: The mailbox manufacturers
Problem description:
A mailbox manufacturer wants to test a new mailbox prototype's durability based on how many fire crackers it can withstand. Given k identical mailboxes (each holding up to m ...
0
votes
0
answers
44
views
how to return dynamic columns with multiple records with out passing a columns to functions returning "record" in PostgreSQL [duplicate]
I have a table like below
create table public.test_dynamic_data_tbl(col1 int,col2 character varying,col3 double precision);
insert into public.test_dynamic_data_tbl values(1,'test1',12)
insert into ...
0
votes
0
answers
55
views
Valid Parenthesis String, Recursion with memoization to DP
How can the following recursion + memoization code be converted into a tabulation format dynamic programming solution?
The code is working but I want to improve it.
The challenge I am facing is ...
4
votes
2
answers
118
views
Why does my A* algorithm expand nodes differently when using heapq vs. a set for the open set?
I'm implementing an A* search algorithm for a maze solver in Python. Initially, I maintained the open set as a plain set and selected the node with the lowest f-score using:
current = min(open_set, ...
3
votes
2
answers
128
views
Match indexes from 1 to n with n given arrays so that sum of elements is minimal
Originally I faced a problem: I had three arrays of the same length (say, a, b, c), and I needed to choose indexes i, j, k distinct from each other that the sum a_i + b_j + c_k would be minimal. I ...
1
vote
2
answers
110
views
Number of ways to reach A to B by climbing one step, two steps or three steps at a time
I was solving a test on an online platform and the problem statement was similar to this.
Stuart has to go from one place to other (A-> B) and he can jump either 1 step, 2 step or 3 steps at a time....
2
votes
1
answer
158
views
Maximum the sum of lengths of two nondecreasing subsequence of given array
It is a famous question which asked about finding longest nondecreasing subsequence from a given array. The question is well studied and have O(n log n) time complexity solutions.
I have encountered a ...
4
votes
2
answers
129
views
How to optimize the performance of nested loops with dynamic data structure updates in Python?
How can I optimize this code for better performance while maintaining the correctness of dynamic updates?
Is there a way to restructure the nested loop or use a different approach to reduce the time ...
2
votes
4
answers
236
views
How to align two string's offset given a list of substrings offsets?
Given a and b relating to a list of substrings in c:
a = "how are you ?"
b = "wie gehst's es dir?"
c = [
("how", "wie"),
("are", "gehst's&...
0
votes
1
answer
107
views
solution to factorial problem using dynamic programming resulting in more time than simple recursion
I was just starting out with dynamic programming and tried solving factorial problem using the same, i used binary tree for underlying data structure, but when i thought of comparing it with normal ...
-1
votes
1
answer
335
views
CSES Elevator Rides why does the solution work?
everyone! While I was solving the problems from cses.fi I came across the one that I totally couldn't understand. I searched the Internet and found the solution with DP along subsets, but still wasn't ...
-1
votes
3
answers
174
views
Why isn't my method working for finding value of Fibonacci sequence for nth term?
I am trying to find the value of a Fibonacci sequence given an integer N, representing the nth term using an ArrayList (my logic is that because ArrayList are dynamic and can resize no matter what ...
3
votes
0
answers
58
views
Minimum dominating subgraph
A graph G(V,E) is defined by a set of vertices V and a set of edges E. Given W⊆V, find a connected subgraph G'(V',E') of G that satisfies the following conditions:
i) ∀p∈V,∃p'∈V' such that p′ is ...
-5
votes
2
answers
114
views
how do i fix this dynamic programming code? [closed]
Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:
After the cutting each ribbon piece should have length a, b or c.
After the ...
0
votes
1
answer
105
views
Storing the recursive call result in a variable leads to incorrect calculation in a DP memoized solution
Just by using the commented code instead of making the recursive calls inside the max function, leads to incorrect result particularly with the following test case
int main() {
Solution obj = ...
0
votes
1
answer
127
views
Knapsack 0/1 variation maximize the number of objects with independent weight restrictions
I'm trying to use dynamic programming to solve a knapsack variation, where you are given an array of containers, each of them having a weight, a resistance and an id, I need to find the tallest pile ...
1
vote
2
answers
164
views
What is the time complexity of the following algorithm and is there any optimization I can do?
Problem: Given an array that sums to 0, find the maximum number of partitions of it such that all of them sum up to 0 individually.
Example:
input: [-2, 4, -3, 5, -4]
output: 2 (which are [[5, -2, -3]...
3
votes
4
answers
187
views
How to efficiently solve the Maximum Product Subarray problem in Python?
I’m trying to solve the Maximum Product Subarray problem, which is described as follows:
Given an array of integers (positive, negative, or zero), find the maximum product of any contiguous subarray.
...
1
vote
1
answer
117
views
Max product non-intersection paths in tree
Question:
Given an undirected tree with N nodes ( 2 <= N <= 100,000 ) and N-1 edges, how can I find two non-intersecting paths with maximum product?
Example:
6
1-2
2-3
2-4
5-4
6-4
Answer: 4
...
1
vote
4
answers
426
views
3366. Minimum Array Sum leetcode
https://leetcode.com/problems/minimum-array-sum/
You are given an integer array nums and three integers k, op1, and op2.
You can perform the following operations on nums:
Operation 1: Choose an ...
0
votes
1
answer
155
views
Why is this DFS + Memoization solution for selecting indices in two arrays too slow while a similar approach is more efficient?
I was solving LeetCode problem 3290. Maximum Multiplication Score:
You are given an integer array a of size 4 and another integer array b of size at least 4.
You need to choose 4 indices i0, i1, i2, ...
1
vote
1
answer
67
views
Print supersequence
I'm struggling with understanding a concept of minimum supersequence of two given strings.
I understand how to print this supersequence from 2D dp table that we get after solving LCS problem for two ...
2
votes
1
answer
56
views
No solution found in GEKKO when modelling a tank level
I'm trying to simulate the level of a tank that has two inlet flows and one outlet. The idea is that after this will be used in a control problem. However, I can't get it to work with gekko but it did ...
5
votes
2
answers
152
views
Algorithm to Minimize Time to Complete All Endings in a Multi-Branch Game with Limited Save Points
In a multi-branch game, there are 𝑛 possible endings. You can think of it as a directed tree with 𝑛 leaf nodes, where each edge has a weight of 1. Instead of playing through the entire game to reach ...
-2
votes
1
answer
49
views
Maximize sum of last three remaining elements [closed]
You are given an array containing n elements, where n is odd. In one operation, you
can select an index i, such that 1 ≤ i < n and remove ai and ai+1 from the array
and concatenate the remaining ...
1
vote
2
answers
124
views
Computing the nth Fibonacci number using forward recursion with memoization
Question: Is it possible to compute the nth Fibonacci number using forward recursion with memoization? If so, how? If not, why not?
Context: Most learning resources and books (e.g., CLRS, DPV, etc.) ...
1
vote
1
answer
179
views
Placing 1x2 or 2x1 tiles on r x c matrix to achieve maximum sum of covered cells
How can you find the maximum possible sum of values by optimally placing tiles in a matrix rxc where each cell has an integer value?
The goal is to maximize the sum by covering cells with 1x2 or 2x1 ...
5
votes
1
answer
212
views
Algorithm to count permutations with specific fixed points and relative value constraints
I'm trying to optimize an algorithm that counts permutations with specific constraints. Given integers n, t, a, b where:
n is the length of permutation (1 to n)
t is the required number of fixed ...
4
votes
1
answer
286
views
Give the result string provided minimum number of parenthesis addition done to make string balanced
A good follow up question asked in one of the interview post 'valid parentheses' problem.
Given an imbalanced parentheses string, return the result string(any one of multiple solutions) with balanced ...
0
votes
4
answers
364
views
Max sum when in each step selecting one addend, then removing last and first element
I have an array. Each iteration, I take an element from the array and add it to a running sum. Then, the first and last element are discarded. I cannot pick an element in the same initial index twice.
...
0
votes
1
answer
58
views
3-D DP VS 2-D DP, can't seem to figure out why my code can't be memoized
class Solution {
public:
int countNeighborhoods(const vector<int>& houses) {
int neighborhoods = 0;
int m = houses.size();
for (int i = 0; i < m; i++) {
...
1
vote
0
answers
145
views
How can I find the length of the longest common substring between two strings efficiently?
I have two strings, s1 and s2, and I need to find the length of the longest substring that appears in both strings. The substring must be contiguous in both s1 and s2.
For example:
If s1 = "...
0
votes
0
answers
76
views
Select top K arrays in an array of arrays, that will consist of the most unique elements
Given an array of arrays, with each sub-array contains unique elements, but among different sub-arrays, there might be the same element. Select the top K sub-arrays, that the union of all elements ...
0
votes
0
answers
246
views
How to dynamically calculate ZigZag Indicator?
I try to implement ZigZag Indicator in TypeScript.
My ZigZagCalculation function accepts as a parameters: Bar[] bar, deviation: number
And returns array with all the zig-zag points.
My bar array has a ...
1
vote
1
answer
269
views
Branch and bound problem, how to enumerate all of the possibilities?
Here is a problem that was in a past exam for Advanced algorithms in my studies,it's on 5 points over 3 hours so should take approximately 1H but I am struggling to find the solution for the first ...
0
votes
1
answer
157
views
Optimizing dynamic programming solution for Abbreviation problem- Hackerrank
So I personally favor coding dynamic programming solutions using a top-down approach. Especially in python because it lends itself to a rather easy recursive implementation using the cache decorator, ...
0
votes
0
answers
19
views
Dynamic Programming Table Extension Causes Incorrect Results for Subset Sum Partition Problem
I'm working on a dynamic programming problem related to counting the number of ways to partition an array into two subsets such that the absolute difference between their sums is equal to a given ...
-1
votes
2
answers
150
views
Minimum number of coins needed for amount with infinite coins available. Understanding the optimization
I am looking at a particular solution that was given for LeetCode problem 322. Coin Change :
You are given an integer array coins representing coins of different denominations and an integer amount ...