LeetCode Weekly Contest 109 Solutions

The solution involves verifying that the array length equals the maximum value plus one, then sorting the array and confirmnig that elements form a sequence from 0 to max - 1. class Solution { public: bool isGood(vector<int>& nums) { int maxVal = 0; for(auto val : nums){ maxVal = max(maxVal, val); ...

Posted on Fri, 06 Nov 2026 16:14:39 +0000 by BigJohn

Algorithm Study | Day 40/60: Word Break, Multiple Knapsack, and Knapsack Problem Summary

一、Problem Practice 1.1 Word Break Problem Link: . - LeetCode class Solution { public: bool existsInDict(const vector<string>& dictionary, const string& target) { for (const auto& word : dictionary) { if (word == target) return true; } return false; } bool wordBreak(string ...

Posted on Thu, 05 Nov 2026 16:07:17 +0000 by Fatboy

Efficient Connectivity DP for Grid Path Counting

Given the constraints where the grid height N is small (typically N ≤ 6) and the width M is large, a column-wise dynamic programming approach with state compression is the optimal strategy. The core challenge lies in efficiently tracking connectivity between cells across columns. Initial DP Formulation and Limitations A naive dynamic programmin ...

Posted on Wed, 04 Nov 2026 16:48:31 +0000 by tkm

Optimizing Maximum Subrectangle Problems with the Hanging Line Method

The "Hanging Line" (or "Suspension Line") method is an efficient technique for identifying the largest rectangular area within a histogram or a grid of binary values. It optimizes the naive brute-force approach from O(N²) to O(N) complexity. Core Concept Consider a histogram of bars with varying heights. For any bar at index ...

Posted on Tue, 03 Nov 2026 16:43:14 +0000 by lordzardeck

Dynamic Programming Solution for Coin Change Problem

Dynamic programming solves problems by combining optimal substructures. For the coin change problem, we need to find the minimum number of coins to make up a given amount. Approach 1: Coin Selection as Subproblem Create a 2D table where rows reprseent available coins and columns represent target amounts. Each cell dp[i][j] stores the minimum co ...

Posted on Mon, 02 Nov 2026 16:34:30 +0000 by k3Bobos

Algorithms for Palindrome Partitioning: Minimum Cuts and Exhaustive Generation

Palindrome partitioning is a common algorithmic challenge that involves splitting a string into several segments, where each segment reads the same forwards and backwards. This article explores two distinct variations of this problem: finding the minimum number of cuts required and generating all possible valid partitions. 1. Calculating the Mi ...

Posted on Fri, 30 Oct 2026 16:07:14 +0000 by graziano

Solving a Competitive Programming Contest: Problem Analyses and Solutions

Problem T1: Geometry and Distances The first problem involves calculating two distances. The first is the distance from a given point to a line segment, and the second is the distance from the same point to a circle. The distance to the circle is straightforward: it's the distance from the point to the circle's center minus the radius. For the ...

Posted on Sat, 24 Oct 2026 16:14:38 +0000 by sv4rog

Greedy Algorithms: Principles and Classic Problem Solutions

Understanding Greedy Algorithms A greedy algorithm makes the locally optimal choice at each step with the hope of finding a global optimum. It does not guarantee a globally optimal solution for all problems, but it works well when the problem exhibits optimal substructure and the greedy choice property. When Greedy Works—and When It Doesn’t App ...

Posted on Thu, 22 Oct 2026 16:31:07 +0000 by howard-moore

Solutions for AtCoder Beginner Contest 369 Problems

The sample cases cover all possible scenarios for this problem. int main() { int x, y; cin >> x >> y; int result = 0; if(x > y) swap(x, y); if(x == y) result = 1; else if((y - x) % 2) result = 2; else result = 3; cout << result << endl; return 0; } </details>### Problem B - Piano Keys Track positions for le ...

Posted on Wed, 21 Oct 2026 16:54:36 +0000 by peyups

Solving the Longest Common Subsequence Problem Using Dynamic Programming

Given two character sequences, str_a and str_b, the objective is to determine the maximum length of a subsequence that appears in both strings in the same relative order, though not necessarily contiguously. If no shared characters exist, the result defaults to zero. Input constraints typically specify lengths between 1 and 1000, containing onl ...

Posted on Mon, 19 Oct 2026 16:11:05 +0000 by elthomo