5,712 questions
3
votes
1
answer
76
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 ...
2
votes
1
answer
188
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 ...
0
votes
1
answer
106
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 ...
9
votes
2
answers
263
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 ...
0
votes
1
answer
86
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){
...
6
votes
1
answer
174
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
52
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 ...
3
votes
2
answers
121
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 ...
2
votes
1
answer
156
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 ...
2
votes
4
answers
230
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&...
-1
votes
1
answer
333
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 ...
3
votes
0
answers
53
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 ...
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 = ...
1
vote
2
answers
163
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]...
1
vote
1
answer
110
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
...
0
votes
1
answer
149
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, ...
2
votes
1
answer
49
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 ...
-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
1
answer
175
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 ...
4
votes
1
answer
279
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
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++) {
...
0
votes
0
answers
73
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 ...
1
vote
1
answer
258
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
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 ...
3
votes
3
answers
167
views
Maximize distance while pushing crates
I'm struggling to write an algorithm for this problem. What approach can be used to solve it?
Problem
There's a company called CratePushers, whose employees are willing to push crates for money. Their ...