Fading Coder

One Final Commit for the Last Sprint

Implementing Dynamic Programming Solutions in Java: Matrix Chain Multiplication and Longest Common Subsequence

Matrix Chain Mulitplication Matrix chain multiplication finds the optimal parenthesization to minimize scalar multiplications. The recurrence relation is: m[i,j] = 0 if i = j min(m[i,k] + m[k+1,j] + p[i-1]*p[k]*p[j]) for i ≤ k < j if i < j Three nested loops are required: Outer loop: chain len...

Dynamic Programming for Stock Trading with at Most K Transactions

We tackle two classic stock trading problems: at most two transactions (LeetCode 123) and at most k transactions (LeetCode 188). Both problems prohibit holding more than one share at a time: you must sell before buying again. Problem 123: Best Time to Buy and Sell Stock III Given an array prices, fi...

Competitive Programming Solutions: Matrix DP, Tree Operations, SCC Analysis, and Combinatorial Counting

Problem A Given an n×m matrix of 0s and 1s, you can flip any submatrix. Find the minimum number of operations to create a path from the top-left corner to the bottom-right corner that only moves down or right and traverses only 0s. (n,m\le 1000). This is a straightforward dynamic programming problem...

Longest Increasing Subsequence, Continuous Increasing Subsequence, and Longest Repeating Subarray

300. Longest Increasnig Subsequence Today we officially begin the subsequence series. This problem is relatively straightforward and serves as a good introduction to the thought process behind subsequence dynamic programming problesm. class Solution { public: int lengthOfLIS(vector<int>& s...

House Robber Problems: Dynamic Programming Solutions

198. House Robber - LeetCode Approach: Consider two states—robbing or skipping the current house—and choose the one yielding the maximum value. Use dynamic programming with the recurrence: dp[i] = max(dp[i-1], dp[i-2] + nums[i]). class Solution { public: int rob(vector<int>& nums) { int n...

Optimizing 0-1 Knapsack with 1D Dynamic Programming Using Rolling Arrays

0-1 Knapsack Problem with Rolling Array Optimization Understanding the Problem The 0-1 knapsack problem is a classic optimization challenge where we aim to maximize the value of items placed in a knapsack with a fixed capacity. Each item can either be included (1) or not included (0), hence the name...

Longest Increasing Subsequence and Related Dynamic Programming Problems

LeetCode 300: Longest Increasing Subsequence Given an integer array nums, find the length of the longest strictly increasing subsequence. A subsequence is a sequence derived from an array by deleting some or no elements without changing the order of remaining elements. Dynamic Programming Approach D...

Counting Valid Binary Strings Under Reduction Rules

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

Longest Common Subsequence via Two-Dimensional Dynamic Programming

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

Linear Segment Dynamic Programming Techniques

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