Problem 1: Counting Isosceles Triangles (Easy Variant) Description Given a grid containing characters, determine the total number of isosceles triangles that can be formed by connected asterisk (*) symbols. Solution Strategy Iterate through every cell that contains an asterisk, treating it as the po...
Fibonacci Number (LeetCode 509) Problem Statement The Fibonacci sequence, denoted as F(n), starts with base values F(0) = 0 and F(1) = 1. For all integers n greater than 1, each term is the sum of the two preceding terms, following the formula F(n) = F(n-1) + F(n-2). Given an integer n, return the v...
Jury Compromise Define dp[i][j][k] as whether it's feasible to consider the i-th candidate with a total defense score of j and prosecution score of k. Transition: dp[i][j][k] = dp[i][j][k] OR dp[i-1][j-defense[i]][k-prosecution[i]]. After DP, enumerate absolute differences to check feasibility. Spac...
0/1 Knapsack Model Given a knapsack with capacity V and n items, each item has a value v and weight w. Each item can be taken at most once. Determine the maximum total value that can be placed in the knapsack. There are two states for each item: take or not take, leading too 2^n possibilities. Defin...
Linear Programming Formulation Let (\cdot) denote the dot product, with (b,c,x,y) as row vectors. For matrix (A), vectors (u,v) satisfy (u \leq v) if (\forall i, u_i \leq v_i), and similar for (\geq). The standard linear programming (LP) problem is: [\max c \cdot x \quad \text{s.t.} \quad \begin{cas...
Dynamic programming is a technique that decomposes complex problems in to smaller subproblems, avoiding redundant computations and storing data efficiently to solve multi-stage mathematical problems, thereby improving algorithmic efficiency. In algorithm design, dynamic programming includes methods...
Given a set of vertical pillars of integer heights and unit width, we consider arranging them in a line. Rainwater can be trapped between the pillars. The goal is to determine all possible total volumes of trapped rainwater (in unit volumes) achievable across all possible arrangements of the pillars...
The problem requires selecting names of lengths from 1 to N, where each name in a given layer must be formed by adding a single character to the beginning or end of a name from the previous layer. This can be efficient solved using string hashing and dynamic programming. Approach We can compute thre...
B. Determining the Number of Unique Lines in a Grid Given a grid of points defined by coordinates (x, y) where x ranges from 0 to 19 and y ranges from 0 too 20, the objective is to calculate the total number of distinct straight lines that can be formed by connecting any two points. Implementation S...