All Questions
Tagged with dynamic-programming java
566
questions
-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 ...
-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 ...
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....
1
vote
1
answer
62
views
Why backtracking is giving wrong results in case of 1st question and correct results for 2nd. Leetcode #322 #78 respectively
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make ...
1
vote
2
answers
129
views
Dynamic programming partition array to find minimum difference fails for negative numbers
I am trying to solve the problem it works for positive integer arrays but fails for negative test cases. How to handle the below test case?
You are given an integer array nums of 2 * n integers. You ...
-4
votes
1
answer
64
views
Why is my code giving wrong answer when using 2d ArrayList and passing when using 2d arrays?
Here is the DP question,
Given a ‘N’ * ’M’ maze with obstacles, count and return the number of unique paths to reach the right-bottom cell from the top-left cell. A cell in the given maze has a value '...
2
votes
3
answers
208
views
Maximum Sum Without Skipping Two Contiguous Elements
The task is to find the maximum sum of a subsequence of integers from a given list. The subsequence must follow two conditions:
It must be contiguous, meaning the selected elements are in consecutive ...
0
votes
0
answers
19
views
dynamic programming min path grid sum
"I recently found a frustrating error in my code. I have two solutions for finding the minimum path sum using dynamic programming (DP). The first solution, which I wrote, doesn't seem to work, ...
0
votes
2
answers
202
views
Increasing Tripplet Subsequence
I was attempting the Increasing Triplet Subsequence problem on leetcode.com
I started off with a brute force approach but it ran into time out issues but passing almost all testcases. The following is ...
0
votes
0
answers
57
views
Algorithm to split basket optimally
I have my own personal project and I got stuck while writing BasketSplitter class.
My input data is in json format and looks as follows
[
"Steak (300g)",
"Carrots (1kg)",
"...
0
votes
1
answer
44
views
Leetcode 1255-recursion and backtracking
I dont quite understand why have we used .clone() method when we have a second for loop to restore the letterCount.And when i'm runnig this code without .clone() method its giving me the wrong answer? ...
0
votes
0
answers
11
views
Discrepancy in Recursive and Memoized Knapsack Solution
I'm currently working on a knapsack problem and have implemented both a recursive solution without memoization and a memoized version. Surprisingly, the recursive solution is giving me the correct ...
0
votes
0
answers
40
views
Dynamic Programming [Longest Palindromic Substring]
I solved this problem using recursion and memoization but struggled to come up with an iterative solution. Can someone help me to convert the below recursive solution into iterative solution with step-...
0
votes
0
answers
90
views
Minimum Cost Path Recursion - Memoization
Given a square grid of size N, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which ...
0
votes
0
answers
57
views
How to optimize recursive dp solution to Buy and Sell stocks 3 problem?
I am trying to solve this problem - https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii
I have the following code-
class Solution {
Map<State, int[][]> dp;
enum State {
...