Fading Coder

One Final Commit for the Last Sprint

Maximizing Energy Release in a Circular Necklace Using Dynamic Programming

Energy beads on a circcular necklace each have a head and tail marker, both positive integers. Adjacent beads satisfy that the tail of one equals the head of the next. Merging two adjacent beads with head a, tail b and head b, tail c releases energy a * b * c, producing a new bead with head a and ta...

Perfect Squares on LeetCode

Given a integer n, detremine the minimum number of perfect squares that sum to n. A perfect square is a integer that equals another integer multiplied by itself. For example: 1, 4, 9, and 16 are perfect squares. 3 and 11 are not. Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4 Example...

Dynamic Programming Solutions for String Deletion and Edit Distance Problems

Problem 1: Minimum Operations to Make Two Strings Equal Given two strings s and t, determine the minimum number of operations required to make them identical by deleting characters from either string. The approach involves identifying the longest common subsequence (LCS) between the two strings. Onc...

Tree Node State Transition Optimization with Dynamic Programming

Problem Statement A tree with (n) nodes has two weights (a) and (b) per node, initailly set to (0). At each step, you can toggle the (b) value of at most one node. For nodes with (b = 1), their (a) values are toggled, followed by toggling thier (b) values and their parent's (b) values (if the parent...

Using a Modified Sieve to Count Composite Numbers Built from Exactly 12 Prime Factors

A specialized variant of the Sieve of Eratosthenes can simultaneously tag integer primality and record the count of prime factors for each composite. This technique is particularly effective when searching for numbers within a range whose total number of prime factors (with multiplicity) equals a gi...

Integer Break and Unique Binary Search Trees

Integer Break and Unique Binary Search Trees
343. Integer Break Given a positive integer n, break it into the sum of k positive integers (k >= 2) and maximzie the product of those integers. Return the maximum product you can get. Example 1: Input: n = 2 Output: 1 Explanation: 2 = 1 + 1, 1 × 1 = 1. Example 2: Input: n = 10 Output: 36 Explana...

Dynamic Programming Solutions for Longest Increasing Subsequence, Continuous Increasing Subsequence, and Repeated Subarray

Longest Increasing Subsequence (LeetCode 300) To find the length of the longest strictly increasing subsequence, define dp[i] as the length of the longest increasing subsequence ending at index i. Initialize all entries to 1. For each element, compare it with all previous elements; if the current el...

Counting Unique Paths in a 2D Grid Matrix

Consider a scenario where an automated agent is situated at the top-left corner of a grid defined by m rows and n columns. The agent is strictly limited to movements either towards the right or downwards. The task is to calculate the total number of distinct routes the agent can take to arrive at th...

Dynamic Programming and Algorithm Problems Collection

Dynamic Programing Coin Change Problem def coin_change(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for coin in coins: for x in range(coin, amount + 1): dp[x] = min(dp[x], dp[x - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1 Predict the Winner def predict_winner(...

Efficient Bitmask-Based String Matching with Subset Constraints

The problem involves determining the lexicographically largest binary string that can be constructed under constraints derived from a set of pattern strings containing '0', '1', and '-' (wildcard) characters. Each query provides a target string, and the solution must find the maximal answer consiste...