Grouped Knapsack Problem: 2D and 1D Dynamic Programming Approaches
Problem Statement
Given n items, each with weight weight[i], value value[i], and group index group[i], pack them in to a knapsack with maximum capacity W. Each group can contain at most one item. Find the maximum total value.
Solution 1: Two-Dimensional DP Array
This is a classic grouped knapsack problem. First, we need to process all items sim ...
Posted on Wed, 20 May 2026 05:18:47 +0000 by jeffshead
Advanced Stair Climbing and Coin Change Solutions Using Dynamic Programming
Advanced Stair Climbing
Problem Statement: Given a staircase with n steps, determine the number of distinct ways to reach the top if you can climb between 1 and m steps at a time. Each step increment can be reused any number of times.
Solution Approach
This problem is modeled as a complete knapsack problem where step increments represent items ...
Posted on Tue, 19 May 2026 21:41:40 +0000 by peytonrm
Dynamic Programming on Increasing and Decreasing Sequences
Consider a sequence $A = (a_1, a_2, \dots, a_n)$. We want to partition $A$ into contiguous subsequences, and then arrange these subsequences to form a new sequence $f_1, f_2, \dots, f_k$. Each contiguous subsequence $f_1, \dots, f_p$ must satisfy either $f_1 \le f_2 \le \dots \le f_p$ or $f_1 \ge f_2 \ge \dots \ge f_p$. The cost associated with ...
Posted on Tue, 19 May 2026 12:41:33 +0000 by thedream
Solving the 0/1 Knapsack: From Brute-Force Recursion to Memoization and Dynamic Programming
Problem Overview: The 0/1 Knapsack
Given a maximum capacity (or time limit) W and N distinct items, where each item has a weight (or time cost) and a value, the objective is to maximize the total value of selected items without exceeding the given capacity. Each item can be chosen at most once.
Constraints
Maximum Capacity W: 1 ≤ W ≤ 1000
Numb ...
Posted on Mon, 18 May 2026 13:23:16 +0000 by VDarkAzN
Unbounded Knapsack Dynamic Programming: Combinations vs Permutations
Unbounded Knapsack ProblemIn the classic 0/1 Knapsack problem, each item can be selected at most once. The Unbounded Knapsack problem modifies this constraint: each item can be chosen an unlimited number of times. Consider a knapsack with a maximum capacity of 4, and the following items:ItemWeightValueA115B320C430The core difference in implemen ...
Posted on Sun, 17 May 2026 17:18:17 +0000 by bbristow
Fundamentals of the 0-1 Knapsack Problem and Partition Equal Subset Sum Solution
Core Principles of the 0-1 Knapsack Problem
The 0-1 Knapsack Problem serves as the foundation for various packing challenges. Given a set of items with specific weights and values, the objective is to maximize value while not exceeding a given capacity constraint. Each item can be included at most once.
Dynamic Programming Solution
We use a DP ...
Posted on Sun, 17 May 2026 06:53:50 +0000 by stickynote427
Algorithms for Finding the Maximum Subarray Sum
Given a sequence of integers of length n, the objective is to identify a contiguous subarray that yields the maximum possible sum. This is a fundamental problem in computer science, solvable through several distinct algorithmic approaches.
Dynamic Programming
The optimal substructure for this problem can be defined by letting f(i) represent the ...
Posted on Sun, 17 May 2026 06:36:02 +0000 by Jiin
LeetCode 62: Unique Paths (Dynamic Programming, Combinatorics)
Problem Description
A robot is located at the top - left corner of an m x n grid (marked 'Start'). The robot can only move either down or right at any point. The goal is to reach the bottom - right corner (marked 'Finish'). We need to determine the number of unique paths possible.
Examples
Example 1:
Input: rows = 3, cols = 7
Output: 28
Exampl ...
Posted on Sat, 16 May 2026 05:47:43 +0000 by ansarka
All-Pairs Shortest Path Computation Using the Floyd-Warshall Method
The Floyd-Warshall algorithm solves the all-pairs shortest path problem in a weighted graph, handling both positive and negative edge weights (with no negative cycles). It uses dynamic programming to iteratively improve shortest path estimates between every pair of vertices.
Core Principal
Define dist[i][j][k] as the shortest distance from node ...
Posted on Fri, 15 May 2026 09:39:48 +0000 by Rovas
AtCoder Regular Contest 104 Problem Solutions
Problem A: Plus Minus
Given the sum and difference of two numbers, we can easily retrieve the original values. The first number is the average of the sum and difference, while the second is half of the difference subtracted from the sum.
s, d = map(int, input().split())
print((s + d) // 2, (s - d) // 2)
Problem B: DNA Sequence
A substring is c ...
Posted on Fri, 15 May 2026 00:15:36 +0000 by ganich