Fading Coder

One Final Commit for the Last Sprint

Dynamic Programming in C: Counting Palindromic Substrings and Longest Palindromic Subsequence

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...

Dynamic Programming Practice: Fibonacci, Climbing Stairs, and Minimum Cost

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...

Solutions for Common Programming Competition Problems

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 >>...

Combinatorial Optimization with C++: Solving Knapsack Variants via Dynamic Programming

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...

Grid Path Dynamic Programming Problems Based on Digital Triangle Model

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...

Inverted Dynamic Programming with Time Blocking for Graph Updates

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...

Dynamic Programming Techniques for Algorithmic Problems

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...

Solving Luogu P1509: Finding a Girlfriend with Multi-dimensional Knapsack

Problem Overview Luogu P1509 "Finding a Girlfriend" presents a multi-constraint 0/1 knapsack problem. Given n items, each with three attributes rmb_i (money), rp_i (reputation), and time_i (time spent), the goal is to maximize the number of selected items such that the sum of rmb_i does no...

Advanced Dynamic Programming and Algorithm Challenges

Simulation Contest 2 Solutions Current Progress: Linear DP, Knapsack Problems, Intervals Challenging Problems 1038 [NOIP2008] Paper Passing Tags High-Dimensional DP Approach The challenge lies in ensuring that the path from (1,1) to (n,m) and back from (n,m) to (1,1) do not overlap. It can be treate...

Competitive Programming Solutions: Mathematical Analysis, Matrix Operations, Bracket Sequences, and DAG Construction

Problem A: Minimum Function Summation Given the function f(x) = min_{i∈ℕ⁺}, compute the sum Σ_{i=1}^{n} f(i) modulo 10^9+7 across multiple test cases. Constraints: n ≤ 10^16, T ≤ 10^4. Solution Approach: Analyze the frequency of each value in the sequence f(i). For a particular value x, we need to c...