Dynamic Programming Patterns for Knapsack Problems
01 Knapsack
Problem Statement
Given N items and a knapsack with capacity m, each item has a volume v[i] and value w[i]. Each item can be selected at most once. Determine which items to select so that the total volume does not exceed the knapsack's capacity and the total value is maximized.
Approach
Let dp[i][j] denote the maximum value achievab ...
Posted on Sun, 10 May 2026 13:24:38 +0000 by basdog22
Combination Sum II - Handling Duplicate Elements in Backtracking
Given an array of integers candidates and a target value target, find all unique combinations in candidates where the numbers sum to target.
Key constraints:
Each number can only be used once in each combination.
All numbers (including the target) are positive integers.
The result set must not contain duplicate combinations.
Examples
Example ...
Posted on Sun, 10 May 2026 11:27:21 +0000 by smilley654
Merging Sorted Linked Lists Efficiently
Given two singly-linked lists that are already sorted in ascending order, produce a single sorted linked list that interleaves all node from both input lists.
Examples
Input: list1 = [1, 2, 4], list2 = [1, 3, 4]
Output: [1, 1, 2, 3, 4, 4]
Input: list1 = [], list2 = []
Output: []
Input: list1 = [], list2 = [0]
Output: [0]
Implementation strate ...
Posted on Sat, 09 May 2026 16:57:09 +0000 by quercus
Finding Perfect Numbers in C: A Complete Guide
Finding Perfect Numbers in C: A Complete Guide
Understanding Perfect Numbers
A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. For example, 6 is a perfect number because its divisors (1, 2, 3) sum to 6 (1+2+3=6). Other perfect numbers include 28, 496, and 8128.
Perfect numbers have inte ...
Posted on Sat, 09 May 2026 12:31:04 +0000 by alwoodman
Sandglass Pattern Generation Algorithm
This problem requires implementing a program that prints a sandglass pattern using a specified character. For instance, given 17 asterisks, the output should follow this format:
*****
***
*
***
*****
A sandglass shape has these characteristics: each row contains an odd number of characters, all rows are center-aligned, adjacent rows diffe ...
Posted on Sat, 09 May 2026 07:15:06 +0000 by Jalz
Strange Elevator Problem (P1135) - BFS Solution
Problem Description
A dream once led to an unusual elevator that operates differently from typical ones. Each floor has a number K_i (0 ≤ K_i ≤ N), and the elevator can move up or down by exactly K_i floors when the corresponding button is pressed. The elevator has four buttons: open, close, go up, and go down.
Given a building with N floors, e ...
Posted on Sat, 09 May 2026 02:44:26 +0000 by cmp241
Dynamic Programming Solutions for LeetCode 343 and 96
LeetCode 343: Integer Break
Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers. Return the maximum product you can get.
Dynamic Programmign Approach
The problem can be solved using dynamic programming. The key insight is that the maximum product for a number i depends on ...
Posted on Sat, 09 May 2026 02:17:40 +0000 by Svoboda
Solving the Longest Valid Parentheses Problem Using Stack and Dynamic Programming
Stack-Based Index Tracking
Calculating the maximum length of well-formed parenthesis substrings requires maintaining a dynamic baseline for distance measurements. A stack storing character indices provides an efficient mechanism for this. Initialize the data structure with -1 to act as a virtual boundary before the string begins. Process the in ...
Posted on Sat, 09 May 2026 00:27:25 +0000 by Kold
Dynamic Programming Approaches for Integer Partitioning and Unique BST Generation
Integer Partitioning for Maximum Product
To maximize the product of integers summing up to a target value n, dynamic programming tracks optimal sub-solutions. Define an array maxProduct where maxProduct[val] represents the highest achievable product from partitioning the integer val.
Since partitioning 0 or 1 yields no valid product, the base c ...
Posted on Fri, 08 May 2026 19:11:46 +0000 by thiscatis
Suffix Automaton: Definition, Construction, and Applications
Definition
A suffix automaton (SAM) for a string (s) is the minimal deterministic finite automaton (DFA) that accepts all suffixes of (s). Formally:
A SAM is a directed acyclic graph (DAG) where nodes represent states and edges represent transitions.
The source node (t_0) serves as the initial state. All states are reachable from (t_0).
Each t ...
Posted on Fri, 08 May 2026 15:39:54 +0000 by james13009