When I am changing the order of isPossible function then it’s giving wrong answer otherwise present code gives correct answer. I wanted to know why this is happening. Here is the problem statement: https://leetcode.com/problems/partition-equal-subset-sum/submissions/ Here is my accepted solution class Solution { unordered_map<int, bool> um; public: bool isPossible(vector<int>&nums, int currentIndex, int sum){ if(sum == 0){ ..

#### Category : knapsack-problem

Hello i need to adapt some problems with dynamic programming to work with more than the conventional number of objects that they usually works like adapt the knapsack problem to work with N sacks instead of 1 and Longest Common Subsequence to work with N strings instead the usual 2. I found that one way ..

Codeforces Round 683 Knapsack Problem I tried this knapsack problem by this method but i got wrong answer . In my code below i calculated my till it was the least less than W . In one solution the sum was taken till the least more than W/2 and it was correct. So what is ..

Problem: I have a list number of candies I would receive for n stalls. I could only visit w stalls. The stall number which I visit would be saved in x[w] (sorted in ascending order). I could receive bonus candies A*max(0, B-(x[i+1] – x[i])). I have to plan out which stalls I have to visit ..

I’m learning algorithms for solving the 0/1 knapsack problem, and I found this nice solution with brute-force. How about its performance if we try to contrast it with a recursive way? About this solution(just my opinion): we use a bit to describe the object is included or not(0 means excluded, 1 means included), so we ..

I have written a brute force algorithm in R and it does work fine. I want to implement using Rcpp so that my code runs faster however I can’t get it to work. for example for a simple example w = [4 2 3] , v = [10 7 4] with maximum weight W=5 it ..

#include <fstream> #include <iostream> #include <vector> using namespace std; int max(int a, int b) { return (a > b) ? a : b; } int knapsack(int W, vector<int> wt, vector<int> val, int n, vector<bool>& include) { unsigned int i; if (n == 0 || W == 0) { return 0; } if (wt.size() > W) ..

my question is about modifying the knapsack 0-1 problem. I want to modify my knapsack code into a C++ code that: takes a filename as an input (so that the filename will be like a variable, the program will read the numbers of any written filename as the filename is given as a command-line parameter ..

I have this code for finding the best value possible depending on the value and weight (Knapsack problem). I want to track only the values and weights there were added to the knapsack. What is the most efficient way to do that? #include <iostream> #include <vector> using namespace std; int solution = 0, n = ..

I have written a Bottom-up DP of knapsack but it is giving me the wrong answer. for input: n=4, maxWeight = 8, weight[]: 1 4 5 7, value[]: 1 3 4 5, the output should be 11 instead, it is giving 8. int unboundedKnapsackDp(int *weight, int *value, int n, int maxWeight) { // creating 2d ..

## Recent Comments