Analysis of Selected Competitive Programming Problems
[CTS2024] The Gate of All Beings
This is a constructive problem on tree traversal. Observation of large test cases shows the answer does not exceed 3. It is posssible to traverse the entire tree with paths of length at most 3.
The answer is typically 0 or 1, except for small trees or star-shaped graphs. For small n (≤ 8), a brute-force search o ...
Posted on Mon, 06 Jul 2026 16:00:40 +0000 by rilana
Monotonic Stack Techniques for Maximum Subrectangle Problems
Monotonic Stack Fundamentals
Monotonic stacks enable linear preprocessing to find:
Prefix/suffix maximum/minimum positions in sequences
Next greater/smaller element positions for each index
Problem B3666: Suffix Maximum Positions
Given a dynamically growing array, after each insertion, find all suffix maximum indices and output their XOR sum. ...
Posted on Sun, 05 Jul 2026 17:15:02 +0000 by Hayce
Selected Solutions from 2024 Nowcoder Winter Algorithm Camp
A. Cosmic End
Find a number within a given range that is the product of three distinct primes.
Given the small constraitn (upper bound ≤ 100), precompute small primes and check all combinations of three distinct ones. The maximum third prime needed is around 100/(2×3) ≈ 16, so checking primes up to 19 suffices.
#include <bits/stdc++.h>
us ...
Posted on Sun, 05 Jul 2026 16:45:51 +0000 by heimskr
Add and Search Word Data Structure
Trie (Prefix Tree) Fundamentals
Binary trees consist of nodes where each node holds a value and pointers to left and right children:
struct Node {
int value;
Node* left;
Node* right;
};
A binary tree node has at most two children. When a tree node can have multiple children, it becomes a multi-way tree. Since the number of children ...
Posted on Sun, 05 Jul 2026 16:36:08 +0000 by Joeddox
LeetCode Problem 160: Intersection of Linked Lists
Intersection of Linked Lists
Problem Link
LeetCode 160
Problem Statement
Given the heads of two singly linked lists, headA and headB, return the node at which the two lists intesrect. If there is no intersection, return nullptr.
The linked lists must retain their original structure after the function returns. You are not allowed to modify th ...
Posted on Sun, 05 Jul 2026 16:14:54 +0000 by bobthebullet990
Adding Two Numbers Represented as Linked Lists
Problem Description
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Examples
Example ...
Posted on Fri, 03 Jul 2026 16:12:18 +0000 by persepha
Implementing Deep Copy for Linked Lists with Random Pointers
The algorithm works in three phases:
Duplicate each node and insert it immediately after its original
Copy the random pointers from original nodes to their duplicates
Separate the interleaved lists into original and copy
C++ Implementation
class LinkedListCloner {
public:
Node* cloneList(Node* head) {
if (!head) return nullptr;
...
Posted on Mon, 29 Jun 2026 17:41:23 +0000 by bmdsherman
Dynamic Programming Techniques for Knapsack Problems
0/1 Knapsack
Given N items and a knapsack with capacity V, each item can only be selected once. Item i has volume v[i] and value w[i]. Determine which items to select to maximize total value without exceeding the knapsack's volume.
Constraints:
0 < N, V ≤ 1000
0 < v[i], w[i] ≤ 1000
Time Complexity: O(N × V)
int n, m;
int f[100010], w[10 ...
Posted on Mon, 29 Jun 2026 17:26:05 +0000 by Duxie
Three LeetCode Problems: Binary Tree Split, Array Reduction, and Jump Game
Maximum Product of Splitted Binary Tree
Given a binary tree with root node, remove exactly one edge to split the tree into two separate subtrees. The goal is to maximize the product of the sums of both resulting subtrees. Return the result modulo 10^9 + 7.
Approach
The key insight is that during a depth-first search that calculates subtree sums ...
Posted on Mon, 29 Jun 2026 16:28:43 +0000 by sapoxgn
Efficient Submatrix Sum Queries Using Prefix Sums
Problem Statement
Given an n×m integer matrix and q queries, each query specifies the coordinates of the top-left and bottom-right corners of a submatrix. For each query, compute the sum of all elements within the specified submatrix.
Solution Approach
The problem can be efficiently solved using 2D prefix sums. By precomputing the cumulative su ...
Posted on Sun, 28 Jun 2026 17:36:37 +0000 by trufla