Fading Coder

One Final Commit for the Last Sprint

Dynamic Programming Solutions for Integer Partition and Binary Search Trees

Integer Partition Problem Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Approach We use dynamic programming where dp[i] represents the maximum product for integer i. The key insight is that for each integer i, we can b...

Simulated Contest Summary: DP, Graph Construction, and Matrix Optimization

Simulated Contest Summary: DP, Graph Construction, and Matrix Optimization
Summary Time Allocation Nothing much to say; I sat through the entire contest, with some random submissions in between, essentially playing the IOI format. Exam Reflection Although the problems in this contest were quite challenging and had low discrimination, there were still some points I failed t...

Solving Programming Contest Problems: Selection, Review, and Time Management

Problem A: Minimal Operations through Recursive Decomposition In this problem, we are tasked with finding the minimum number of operations to process a sequence. By analyzing small cases, a recursive pattern emerges. For a sequence of length $n$, the optimal number of steps $f(n)$ can be defined by...

Dynamic Programming for Maximum Stock Profit: Single and Multiple Transactions

Valid for a single buy-sell cycle The problem asks for the maximum possible profit from one purchase and one sale. A dynamic programming approach tracks two states for each day: cash_with_stock[i]: the largest amount of cash achievable on day i while holding the stock. cash_without_stock[i]: the lar...

Dynamic Programming for Grid Path Counting with and without Obstacles

The classic problem of counting distinct paths in a rectangular grid where movement is restricted to right and down steps can be modeled as a binary tree structure, but such an approach leads to exponential time complexity. A more efficient solution uses dynamic programming with a two-dimensional st...

Minimizing Total Cost to Cut a Rectangular Cake to Unit Squares

The objective is to partition an m by n rectangular cake into 1x1 unit squares with the least total cost. Each horizontal cut between rows i and i+1 incurs a cost of horizontalCut[i]. Each vertical cut between columns j and j+1 incurs a cost of verticalCut[j]. The total cost is the sum of all cut co...

Solving Buying Hay: Minimum Cost to Meet a Weight Requirement with Unbounded Items

We need to determine the minimum cost to obtain at least (H) pounds of hay given (n) suppliers. Each supplier offers a bundle weighing (p_i) pounds at cost (c_i), and bundles can be purchased unlimitedly. Approach 1: State Transition with Conditional Bounds Standard knapsack formulations maximize va...

Optimizing Color Coverage and Chain Selection on Trees

Canvas Painting The problem involves finding the maximum number of effective operations to reduce distinct colors. With n initial colors, each operation can reduce one color by making two positions share the same color. The answer is n minus the maximum effective operations. Algorithm: Group paintin...

Solving Word Break with Dynamic Programming and Understanding Multiple Knapsack

139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. Note: The same word in the dictionary may be reused multiple times in the segmentation. You may...

Selected Algorithmic Problem Solutions and Techniques

A. [POI2004] SZP Description Given a directed graph with cycles, where each node has exactly one outgoing edge (non-self-loop), select the maximum number of nodes such that every selected node has at least one immediate predecessor that is not selected. Solution Observe that each weakly connected co...