Given a binary string composed of '0', '1', and '?' characters, and a lookup table f that maps each 3-bit binary number (from 0 to 7) too either 0 or 1, determine the number of ways to replace all '?' characters with '0' or '1' such that the resulting string can be reduced to "1" using the...
Algorithm ApproachLet dp[m][n] denote the length of the longest common subsequence between the first m characters of string str_a and the first n characters of string str_b. By utilizing an extra row and column for empty prefixes, the base cases naturally become dp[0][n] = 0 and dp[m][0] = 0.The sta...
Introduction Dynamic programming problems that rely on transitions between adjacant elements often cannot be solved using standard DP approaches. In such cases, linear segment DP proves useful. Also known as continuous segment DP, this technique focuses on maintaining the total contribution of vario...
Palindromic Substrings Problem Statement Given a string, count how many palindromic substrings it contains. Substrings that start or end at different positions are considered distinct even if they consist of the same characters. Solution using Dynamic Programming We define a two‑dimensional Boolean...
Fibonacci Number (LeetCode 509) The Fibonacci seqeunce is defined as: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) for n > 1 Example: Input: n = 4 Output: 3 (Sequence: 0, 1, 1, 2, 3) Solution using constant space: class Solution: def fib(self, n: int) -> int: if n < 2: return n # Initialize...
Greeting Code #include <iostream> int main() { std::cout << "Competition Day!" << '\n'; return 0; } Neighbor Sum Array #include <iostream> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int len; cin >>...
A knapsack problem involves selecting items with given weights and values to maximize total value without exceeding a capacity limit. It appears in resource allocation, scheduling, logistics, and investment scenarios. Problem Categories Binary Knapsack — Each item can be taken at most once. Given n...
Peanut Harvesting Problem Problem Description Traverse an R x C grid of peanut plants starting at the top-left corner and exiting at the bottom-right corner. Each cell holds a non-negative integer representing the number of peanuts available at that location. You may only move right or downward at a...
Consider processing graph updates in reverse. The state for each node represents the minimum cost required to become 'safe'. Each update modifies only a single node b_i, requiring an examination of its neighbors. A square root decomposition approach based on vertex degree allows for efficient handli...
This article covers several dynamic programming problems, including finding the maximum subarray sum, the longest increasing subsequence, the longest common subsequence, the longest palindromic substring, and finding the longest path in a Directed Acyclic Graph (DAG). Maximum Subarray Sum This probl...