Dynamic Programming Approaches to Knapsack Problems for Coding Competitions

01 Knapsack Problem Problem Statement Given item_count items and a knapsack with capacity capacity, each item has a weight w and value val. Calculate the maximum total value achievable without exceeding the knapsack capacity. Input Example: 5 20 1 6 2 5 3 8 5 15 3 3 Output Example: 37 Solution Idea The 01 knapsack problem restricts each item ...

Posted on Tue, 21 Jul 2026 17:11:09 +0000 by chrisdarby

Dynamic Programming: Knapsack Problems and Combination Counting

Both knapsack problems and combination counting problems follow a similar pattern in dynamic programming. Each element in a sequence has two states: selected or not selected. The current state can be derived from the previous state based on these two choices. DP Array Definition The definition of the dp array depends on the problem requirements ...

Posted on Sat, 18 Jul 2026 16:50:19 +0000 by drax

Dynamic Programming Solutions for Three Algorithmic Problems

Ehab and the Expected GCD Problem The key insight is that the first element should have the maximum number of prime factors, and subsequent elements should remove at most one prime factor per step for optimality. The smallest prime factors are 2 and 3, and using 3 more than once is suboptimal (e.g., 5 can be replaced by 2² for better results). ...

Posted on Fri, 17 Jul 2026 17:02:09 +0000 by RossC0

State Compression Dynamic Programming: Cannon Positioning and Non-attacking Kings Problems

In an N×M (N<100, M<10) grid, we need to place cannons on plains (P) while avoiding mountains (H). Cannons attack in a cross pattern: 2 cells left and right horizontally, and 2 cells up and down vertically. Cannons cannot attack each other. The goal is to maximize the number of cannons placed. Example input: 5 4 PHPP PPHH PPPP PHPP PHHP ...

Posted on Fri, 17 Jul 2026 16:18:25 +0000 by andymike07

Computing Subset Sums with SOS DP and Fast Walsh-Hadamard Transform

Define ∩ as bitwise AND, ∪ as bitwise OR, and ⊕ as bitwise XOR. The notation i ⊆ x means the set of bits in binary representation i is a subset of x, i.e., i ∩ x = i. SOS DP (Sum Over Subsets) Given an array a, define F(x) = Σ a_i for all i ⊆ x. The goal is to compute F(x) for all x. Define dp[x][k] as the sum of a_i for all i that differ from ...

Posted on Thu, 16 Jul 2026 17:07:00 +0000 by mentor

Memoization Recursion and Dynamic Programming: Solving Optimization Problems Efficiently

Guess Number Higher or Lower II We need to solve a game where we guess a number between 1 and n. Each wrong guess costs the amount equal to the guessed number. The goal is to find the minimum amount of money needed to guarantee a win regardless of which number is selected. Brute-Force Recursion class Solution { public: int calculateMinCost( ...

Posted on Wed, 15 Jul 2026 17:20:52 +0000 by djBuilder

Advanced Algorithmic Patterns: Interval Games, State-Space Routing, and Lazy Segment Trees

Interval Game Theory via Dynamic Programming Two participants alternately extract characters from either end of a string. Assuming optimal play from both sides, the objective is to predict the final match outcome. The input guarantees an even-length string, with cumulative lengths capped at 2000 across all test cases. The problem resolves effic ...

Posted on Tue, 14 Jul 2026 16:35:22 +0000 by ozzysworld

Dynamic Programming Approaches for Palindrome String Problems

Counting Palindromic Substrings This section addresses the problem of counting all palindromic substrings within a given string, similar to LeetCode problem 647. Problem Description Given a string s, determine and return the total count of palindromic substrings. A substring is a contiguous sequence of charcaters. A palindromic string reads the ...

Posted on Tue, 14 Jul 2026 16:19:33 +0000 by mdgalib

AtCoder ABC 044 Problem Solutions

A Problem: You need to stay for (n) consecutive days. The pricing is (x) yuan per night for the first (k) days, and (y) yuan per night thereafter. What is the total cost? Solution: (min(k, n) \cdot x + max(n - k, 0) \cdot y) B Problem: Given a string (s), determine if it is "beautiful". A string is beautiful if every lowercase charac ...

Posted on Mon, 13 Jul 2026 16:37:11 +0000 by justsomeone

Expected Key Presses in the Klavir Problem

Notation Let \(dp_i\) denote the expected number of attempts to correctly play the prefix \(1 \to i\), with \(dp_1 = n\). The target melody is given by the array \(target_i\). Analysis When playing a melody, the current partially correct sequence falls into one of two cases: The current suffix matches some prefix of the melody. No suffix of ...

Posted on Mon, 13 Jul 2026 16:09:08 +0000 by rslnerd