Comprehensive Guide to Dynamic Programming Patterns and Implementations

Minimum Path Sum in a Grid Finding the minimum path sum from the top-left to the bottom-right of a grid is a classic dynamic programming problem. To optimize space complexity, we can use a 1D array instead of a 2D matrix to store the DP states, updating the array iteratively as we traverse each row. #include <vector> #include <algorith ...

Posted on Thu, 04 Jun 2026 17:28:26 +0000 by m4tt

SMU Summer 2024 Contest Round 4 - Problem Editorials

Made Up Problem Statement Given three sequences A, B, C, count the number of pairs (i, j) such that A[i] = B[C[j]]. Solution Approach Since all values are bounded between 1 and N, we can use frequency counting. For each value v, count how many times it appears in array A (stored in cntA) and how many times it appears as B[C[j]] (stored in cntB) ...

Posted on Thu, 04 Jun 2026 16:57:11 +0000 by reyes99

Sliding Window and Spiral Matrix Algorithms

Problem Description Given an array of n positive integers and a positive integer target, find the length of the shortest continuous subarray whose sum is at least target. Return the length of this subarray. If no such subarray exists, return 0. Example 1: Input: target = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: The subarray [4,3] has the ...

Posted on Thu, 04 Jun 2026 16:09:14 +0000 by 8mycsh

Graph Theory: Multi-source and Single-source Shortest Path Algorithms

Shortest path problems in graph theory often rely on the concept of relaxation. Relaxation occurs when a path from node u to v can be shoretned by routing through an intermediate node k, i.e., if dist[u][v] > dist[u][k] + dist[k][v], we update dist[u][v] accordingly. Floyd-Warshall Algorithm (All-Pairs Shortest Paths) To compute shortest pat ...

Posted on Wed, 03 Jun 2026 18:20:34 +0000 by little_tris

Algorithm Solutions: Path Search, String Ranking, and Knapsack Problems

D - Path Traversal A straightforward depth-first search approach can solve this traversal problem. int nodes, edges, max_steps, min_cost, max_cost; vector<int> valid_endpoints; vector<pair<int, int>> graph[MAX_NODES]; void traverse(int current_node, int current_cost, int steps_taken) { if (current_cost > max_cost) retu ...

Posted on Fri, 29 May 2026 19:58:58 +0000 by volant

2012 NOIP Popularization Group Finals Walkthrough

Task 1 – Factor Splitting Given a positive integer n that is the product of two distinct primes, output the larger one. Input: One integer n (≤ 2·109). Output: The larger prime factor. Observation: The first divisor (other than 1) must be the smaller prime, so the answer is n / i once the smallest i > 1 with n % i == 0 is found. #include &lt ...

Posted on Thu, 28 May 2026 22:28:35 +0000 by dannyd

Dynamic Programming Fundamentals and Applications

Linear DP Core Concepts Dynamic Programming (DP) solves complex problems by breaking them in to overlapping subproblems. The solution to the main problem is derived from solutions to these subproblems. State Representation State are typically represented as dp[i][j] = value, where i and j are indices or variables describing the state, and value ...

Posted on Thu, 28 May 2026 20:03:56 +0000 by d3ad1ysp0rk

Solving Sequential Placement Problems with Overlapping Constraints via Linear Dynamic Programming

The problem models a sequential arrangement where each position $i$ offers two distinct categories of elements. Category X occupies exactly one slot, while Category Y spans two consecutive slots $(i-1, i)$. The objective is to compute the total number of valid configurations modulo $10^9+7$. A two-state dynamic programming approach efficiently ...

Posted on Thu, 28 May 2026 18:09:44 +0000 by scorphus

Solving Key Problems from Codeforces Round 1057 (Div. 2)

A. Apple Tree Ring Given a sequence of integers representing apple counts on trees arranged in a circle, determine the maximum number of distinct values that can be consumed under infinite rotations. Since rotations allow arbitrary reordering over time, the optimal strategy is to consume one unique value per round. Hence, the answer equals the ...

Posted on Tue, 26 May 2026 23:07:05 +0000 by interpim

Dynamic Programming: Core Concepts and Algorithmic Implementations

Understanding Dynamic Programming Dynamic programming (DP) is an optimization technique used to solve complex problems by breaking them into simpler subproblems. It stores the results of subproblems to avoid redundant computations, thereby improving efficiency. This article explores fundamental DP concepts and implementations through various al ...

Posted on Wed, 20 May 2026 00:48:10 +0000 by saint959