Determining Graph Connectivity using DFS and BFS

Algorithmic Approach When handling graph problems, especially those involving large datasets, an adjacency list is often preferred over an adjacency matrix to optimize memory usage, particularly when the vertex count may exceed standard limits. To determine if an undirected graph is connected, one can traverse the structure using either Depth-F ...

Posted on Sun, 19 Jul 2026 16:48:56 +0000 by Jay87

LeetCode Daily Problems: Binary Tree Traversals and Construction

590. N-ary Tree Postorder Traversal Approach: Right-to-left, then root-to-left. Use a stack with a visited set to track processed nodes. class Node: def __init__(self, val=None, children=None): self.val = val self.children = children def postorder(root): if not root: return [] stack = [root] result = [] ...

Posted on Thu, 09 Jul 2026 16:38:30 +0000 by balacay

Search Algorithms in Problem Solving

Definition Search algorithms systematically explore state spaces to find optimal solutions or count valid configurations through exhaustive enumeration. This approach leverages understanding of state transitions to navigate possible states. Search Algorithm Applications When explicit enumeration becomes infeasible (e.g., permutations for n=100 ...

Posted on Thu, 02 Jul 2026 17:04:03 +0000 by littlejones

Graph Traversal: Searching References

Problem Description Little K enjoys browsing Luogu blog articles for knowledge. Each article may have several (or none) reference links pointing to other blog articels. Little K is very curious: if he reads an article, he will certainly read its references (unless he has already read that reference). Assume there are n (n ≤ 10^5) articles on Lu ...

Posted on Wed, 01 Jul 2026 16:36:22 +0000 by coder4Ever

Understanding Simulation, DFS/BFS, Dynamic Programming, and Block Decomposition for Competitive Programming

Simulation problems, often labeled as "warm-up" or "signature" tasks in contests, require translating problem statements directly into code without relying on predefined algorithms. While they appear simple, their difficulty lies in accurately interpreting edge cases and constraints. A single oversight in boundary checks or ...

Posted on Sun, 14 Jun 2026 16:51:10 +0000 by ldougherty

Binary Tree Algorithms: Bottom-Left Value, Path Sum Variants, and Tree Construction from Traversals

Finding the Bottom-Left Node Value Given the root of a binary tree, return the value of the leftmost node at the deepest level. Breadth-First Search (Iterative) A level-order traversal naturally visits nodes layer by layer. The first node encountered in the final level is the answer. #include <queue> int findBottomLeftValue(TreeNode* roo ...

Posted on Sat, 13 Jun 2026 17:35:17 +0000 by eideticmnemonic

Computing Minimum Knight Moves on a Chessboard Using BFS and DFS

Given an n × m chessboard (with 1 < n, m ≤ 400) and the starting position of a knight, determine the minimum number of moves required for the knight to reach every other square. If a square is unreachable, output -1. Input Format A single line containing four integers: n, m, start_x, and start_y. Output Format Print an n × m matrix. Each val ...

Posted on Tue, 09 Jun 2026 17:50:23 +0000 by fourteen00

Solutions for the SXJ202507250900 Simulation Contest

Problem 1: Dumpling Purchase Optimization The problem reduces to a daily deciison: buy dumplings at the current price or rely on an earlier purchase plus storage cost. The total expense for day i if we buy on day j ≤ i is price[j] + c*(i - j). This can be rewritten as (price[j] - c*j) + c*i. Thus we can maintain the minimum value of price[j] - ...

Posted on Sat, 23 May 2026 19:20:05 +0000 by d_barszczak

Minimum Button Presses for a Strange Elevator

A building has an unusual elevator system. Each floor i (where 1 ≤ i ≤ N) has a fixed value K[i], which determines how many floors the elevator moves when the "up" or "down" button is pressed. The elveator can only move up by K[i] floors or down by K[i] floors from floor i. If the target floor would be below 1 or above N, th ...

Posted on Mon, 18 May 2026 01:53:57 +0000 by bobdabuilder

BFS on Parity-Based Reachability for a Single 1 in a Binary String

Spinning Around Given a binary string (S) of length (n) with exactly one 1. In each operation, you can reverse a substring of length (k). For each position (i), find the minimum number of operations to move the 1 to position (i). Some positions are forbidden and cannot hold the 1 during the process. If no such number exists, output (-1). (n \le ...

Posted on Fri, 15 May 2026 16:47:23 +0000 by Matt Kindig