LeetCode Weekly Contest 169 Solutions
Problem 1: Find N Unique Integers Sum up to Zero
Given an integer n, any array of n unique integers that sum to 0 can be returned.
Example 1:
Input: n = 5
Output: [-7,-1,1,3,4]
Explanation: These arrays are also correct [-5,-1,1,2,3], [-3,-1,2,-2,4].
Example 2:
Input: n = 3
Output: [-1,0,1]
Example 3:
Input: n = 1
Output: [0]
Constraints:
1 &l ...
Posted on Tue, 03 Nov 2026 16:11:59 +0000 by Zoud
Essential Binary Tree Operations: Finding Bottom Left Value, Path Sum Variations, and Inorder-Postorder Construction
Finding the Bottom Left Tree ValueLocating the leftmost value in the last row of a binary tree can be efficiently achieved using Breadth-First Search (BFS). By processing the tree level by level, the first node encountered at each depth represents the leftmost node for that specific row. Upon completing the traversal, the value recorded for the ...
Posted on Sun, 18 Oct 2026 16:30:55 +0000 by batman
Algorithmic Approaches to Subset Sum and N-Queens via Recursion
Magic Pocket Subset Sum
The Magic Pocket problem requires determining the number of ways to select items such that their total volume equals exactly 40. This can be approached using a Depth-First Search (DFS) strategy where we explore every possible combination of including or excluding an item. We maintain a running sum of the selected volumes ...
Posted on Sat, 17 Oct 2026 16:41:07 +0000 by zzman
Implementing a Word Search Algorithm in a 2D Grid
This problem can be efficiently solved using a depth-first search (DFS) algorithm with backtracking. The core idea is to traverse each cell of the grid and check if it can start a valid path that forms the target word.
Key Concepts
The grid trvaersal here is a variation of binary tree DFS, where each cell (node) has four possible adjacent direc ...
Posted on Sun, 11 Oct 2026 16:19:13 +0000 by sebajom
Analysis of Two Number Theory Problems: Set Construction and GCD Subsequence
Problem 1: Counting Valid Sets
In this problem, the floor operation does not affect the correcntess, so a brute-force DFS approach is applicable.
A critical observation: if the array contains the element \(1\), it must be skipped immediately. Otherwise, \(1\) could be selected infinitely many time, leading to incorrect results.
Solution Code
in ...
Posted on Mon, 05 Oct 2026 16:14:09 +0000 by marcel
Insect Population Growth: Recursive and Dynamic Programming Solutions
Insect Population Growth Problem
Time Limit: C/C++ 1000ms, Other Languages 2000ms
Memory Limit: C/C++ 256MB, Other Languages 512MB
Problem Description
Scientists in a tropical forest have discovered a special insect species with remarkable reproductive capabilities. Each pair of adult insects produces y pairs of eggs after x months. Each pair o ...
Posted on Wed, 23 Sep 2026 16:24:21 +0000 by silverphpd
Essential Binary-Tree Algorithms and Their Implementations
In-Order Traversal
Recursive
List<Integer> inorder(TreeNode node) {
List<Integer> out = new ArrayList<>();
walk(node, out);
return out;
}
void walk(TreeNode cur, List<Integer> acc) {
if (cur == null) return;
walk(cur.left, acc);
acc.add(cur.val);
walk(cur.right, acc);
}
Iterative (Single Sta ...
Posted on Mon, 21 Sep 2026 16:25:53 +0000 by otterbield
The Confounding Switches - Solution
The Confounding Switches
Time Limit: C/C++ 1000MS, Other Languages 2000MS
Memory Limit: C/C++ 256MB, Other Languages 512MB
Description
Have you ever played the "Light Switch" game?
There are \(25\) lights arranged in a \(5×5\) grid.
Each light has a switch that can toggle its state.
In each move, a player can toggle one light's sta ...
Posted on Fri, 18 Sep 2026 16:14:26 +0000 by zachatk1
Minimal Coprime Groups Partitioning via Depth-First Search
Suppose you are given an integer array arr. The goal is to split it into the fewest possible subsets such that every pair of elements inside the same subset is coprime (their greatest common divisor equals 1).
We can solve this problem using a DFS backtracking approach. Below are two distinct strategies, each corresponding to a different way of ...
Posted on Tue, 08 Sep 2026 16:30:37 +0000 by PhilVaz
Recursive Techniques for Generating Subsets and Permutations
Recursion can be categorized into path-aware and path-unaware forms. Most tree-related recursions are path-aware. Fundamentally, recursion implements depth-first search (DFS). To solve problems recursively, treat the recursive function as a black box that handles a subproblem, then reuse it. Beginners often try to fully expand the recursion, wh ...
Posted on Sat, 05 Sep 2026 16:48:55 +0000 by denoteone