brain

tamnd's digital brain — notes, problems, research

43815 notes

LeetCode 1520 - Maximum Number of Non-Overlapping Substrings

This problem asks us to select as many non-overlapping substrings as possible from a given lowercase string s, while sat

leetcodehardhash-tablestringgreedysorting
CF 131B - Opposites Attract

We are asked to count all pairs of clients whose assigned numbers are exact opposites. Each client has a number between -10 and 10, and a pair is valid if one client has number $x$ and the other has number $-x$.

codeforcescompetitive-programmingimplementationmath
CF 120D - Three Sons

We have a rectangular cornfield represented as a grid of size n × m, where each cell contains a certain number of tons of corn. The father wants to divide this field among three sons in such a way that each son receives exactly a predetermined amount of corn: A, B, or C tons.

codeforcescompetitive-programmingbrute-force
LeetCode 503 - Next Greater Element II

The problem gives us a circular array nums, and for every element, we must find the next greater element. The phrase next greater element means the first value encountered while moving forward in the array that is strictly larger than the current number.

leetcodemediumarraystackmonotonic-stack
CF 39A - C*++ Calculations

We are given an arithmetic expression built from terms involving a single variable a. Every term is one of two forms:

codeforcescompetitive-programmingexpression-parsinggreedy
LeetCode 102 - Binary Tree Level Order Traversal

The problem asks us to perform a level order traversal on a binary tree. A binary tree is a hierarchical structure where each node can have at most two children, commonly referred to as the left child and the right child.

leetcodemediumtreebreadth-first-searchbinary-tree
LeetCode 1253 - Reconstruct a 2-Row Binary Matrix

The problem asks us to reconstruct a binary matrix with exactly two rows and n columns given three constraints: the sum

leetcodemediumarraygreedymatrix
LeetCode 16 - 3Sum Closest

The problem gives us an integer array nums and a target integer target. We must select exactly three elements from the array, using distinct indices, and compute their sum. Among all possible three-number sums, we want the one whose value is closest to the target.

leetcodemediumarraytwo-pointerssorting
CF 41B - Martian Dollar

We are asked to maximize the amount of money Vasya can have at the end of _n_ days if he starts with a given sum of bourles and can buy and later sell Martian dollars.

codeforcescompetitive-programmingbrute-force
LeetCode 1423 - Maximum Points You Can Obtain from Cards

The problem asks us to maximize the total points we can collect from a row of cards, where each card has a point value.

leetcodemediumarraysliding-windowprefix-sum
CF 50E - Square Equation Roots

We are asked to count all distinct real roots of quadratic equations of the form , where ranges from 1 to and ranges from 1 to . Each pair defines one quadratic. The output is the total number of distinct real roots across all these quadratics.

codeforcescompetitive-programmingmath
LeetCode 91 - Decode Ways

This problem asks us to determine how many different valid ways a numeric string can be decoded into letters using the mapping: - "1" → 'A' - "2" → 'B' - ... - "26" → 'Z' The input is a string s containing only digits.

leetcodemediumstringdynamic-programming
LeetCode 1349 - Maximum Students Taking Exam

LeetCode 1349 is long and detailed enough that a complete high quality guide with all required sections, full prose expl

leetcodehardarraydynamic-programmingbit-manipulationmatrixbitmask
LeetCode 1838 - Frequency of the Most Frequent Element

This problem asks us to determine the maximum frequency of an element in an integer array nums after performing at most k increment operations. Each operation allows you to choose an element and increase it by 1.

leetcodemediumarraybinary-searchgreedysliding-windowsortingprefix-sum
LeetCode 1692 - Count Ways to Distribute Candies

The problem asks us to compute the number of ways to distribute n distinct candies into k bags such that every bag has a

leetcodeharddynamic-programming
CF 30E - Tricky and Clever Password

We are given a single lowercase string. Somewhere inside this string, there exists a hidden palindrome of odd length, but the palindrome was split into three consecutive parts:

codeforcescompetitive-programmingbinary-searchconstructive-algorithmsdata-structuresgreedyhashingstrings
LeetCode 430 - Flatten a Multilevel Doubly Linked List

The problem gives us a special type of doubly linked list where every node contains four fields: The next and prev pointers behave exactly like a normal doubly linked list. The extra child pointer introduces another linked list that branches downward from the current node.

leetcodemediumlinked-listdepth-first-searchdoubly-linked-list
LeetCode 964 - Least Operators to Express Number

This problem asks us to construct a mathematical expression using only the number x repeatedly, combined with the operators +, -, , and /, such that the final expression evaluates exactly to target.

leetcodehardmathdynamic-programmingmemoization
Project Euler Problem 774

Let '' denote the bitwise AND operation.

eulermathematicscompetitive-programming
Project Euler Problem 901

A driller drills for water.

eulermathematicscompetitive-programming
Project Euler Problem 959

A frog is placed on the number line.

eulermathematicscompetitive-programming
LeetCode 191 - Number of 1 Bits

The problem asks us to count how many bits are set to 1 in the binary representation of a positive integer n. A set bit is simply a bit whose value is 1. For example, the number 11 in binary is: This binary representation contains three 1 bits, so the answer is 3.

leetcodeeasydivide-and-conquerbit-manipulation
Project Euler Problem 979

Solution to Project Euler Problem 979.

eulermathematicscompetitive-programming
Project Euler Problem 960

nThere are n distinct piles of stones, each of size n-1.

eulermathematicscompetitive-programming
Project Euler Problem 934

We define the unlucky prime of a number n, denoted u(n), as the smallest prime number p such that the remainder of n div

eulermathematicscompetitive-programming
Project Euler Problem 897

Let G(n) denote the largest possible area of an n-gona polygon with n sides contained in the region (x, y) in Bbb R^2: x

eulermathematicscompetitive-programming
Project Euler Problem 915

The function s(n) is defined recursively for positive integers by s(1) = 1 and s(n+1) = big(s(n) - 1big)^3 +2 for ngeq 1

eulermathematicscompetitive-programming
Project Euler Problem 973

Solution to Project Euler Problem 973.

eulermathematicscompetitive-programming
Project Euler Problem 447

For every integer n 1, the family of functions f{n,a,b} is defined by for integers a, b, x with 0 < a < n, 0 le b < n, 0

eulermathematicscompetitive-programming
Project Euler Problem 896

A contiguous range of positive integers is called a divisible range if all the integers in the range can be arranged in

eulermathematicscompetitive-programming
CF 1941A - Rudolf and the Ticket

The problem gives us two arrays of coin values. The first array represents coins in Rudolf's left pocket, and the second array represents coins in his right pocket.

codeforcescompetitive-programmingbrute-forcemath
Project Euler Problem 375

Let Sn be an integer sequence produced with the following pseudo-random number generator: Let A(i, j) be the minimum of

eulermathematicscompetitive-programming
Project Euler Problem 829

Given any integer n gt 1 a binary factor tree T(n) is defined to be: - A tree with the single node n when n is prime.

eulermathematicscompetitive-programming
LeetCode 201 - Bitwise AND of Numbers Range

The problem asks us to compute the bitwise AND of every integer in the inclusive range [left, right]. For example, if left = 5 and right = 7, the numbers in the range are: Applying bitwise AND across all values: So the answer is 4.

leetcodemediumbit-manipulation
Project Euler Problem 913

The numbers from 1 to 12 can be arranged into a 3 times 4 matrix in either row-major or column-major order: By swapping

eulermathematicscompetitive-programming
Project Euler Problem 905

Three epistemologists, known as A, B, and C, are in a room, each wearing a hat with a number on it.

eulermathematicscompetitive-programming
Project Euler Problem 971

Let p be a prime of the form 5k-4 and define fp(x) = left(x^k+xright) bmod p.nnLet C(p) be the number of values 0 le x l

eulermathematicscompetitive-programming
Project Euler Problem 256

Tatami are rectangular mats, used to completely cover the floor of a room, without overlap.

eulermathematicscompetitive-programming
Project Euler Problem 975

Solution to Project Euler Problem 975.

eulermathematicscompetitive-programming
Project Euler Problem 928

This problem is based on (but not identical to) the scoring for the card game Cribbage.

eulermathematicscompetitive-programming
Project Euler Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

eulermathematicscompetitive-programming
Project Euler Problem 565

Let sigma(n) be the sum of the divisors of n.

eulermathematicscompetitive-programming
Project Euler Problem 374

An integer partition of a number n is a way of writing n as a sum of positive integers.

eulermathematicscompetitive-programming
Project Euler Problem 922

A Young diagram is a finite collection of (equally-sized) squares in a grid-like arrangement of rows and columns, such t

eulermathematicscompetitive-programming
Project Euler Problem 977

Solution to Project Euler Problem 977.

eulermathematicscompetitive-programming
LeetCode 4 - Median of Two Sorted Arrays

The problem gives two individually sorted integer arrays, nums1 and nums2, with lengths m and n. The task is to compute the median of the combined sorted sequence formed by merging both arrays. The median is the middle value of a sorted sequence.

leetcodehardarraybinary-searchdivide-and-conquer
LeetCode 199 - Binary Tree Right Side View

The problem gives us the root node of a binary tree and asks us to determine which nodes are visible when looking at the tree from the right side.

leetcodemediumtreedepth-first-searchbreadth-first-searchbinary-tree
Project Euler Problem 367

Bozo sort, not to be confused with the slightly less efficient bogo sort, consists out of checking if the input sequence

eulermathematicscompetitive-programming
Project Euler Problem 920

For a positive integer n we define tau(n) to be the count of the divisors of n.

eulermathematicscompetitive-programming
Project Euler Problem 925

Let B(n) be the smallest number larger than n that can be formed by rearranging digits of n, or 0 if no such number exis

eulermathematicscompetitive-programming
Project Euler Problem 995

For each prime p and each positive integer n define two polynomials: Let S(p) be the smallest positive integer s such th

eulermathematicscompetitive-programming
Project Euler Problem 902

A permutation pi of 1, dots, n can be represented in one-line notation as pi(1),ldots,pi(n) .

eulermathematicscompetitive-programming
Project Euler Problem 898

Claire Voyant is a teacher playing a game with a class of students.

eulermathematicscompetitive-programming
Project Euler Problem 940

The Fibonacci sequence (fi) is the unique sequence such that - f0=0 - f1=1 - f{i+1}=fi+f{i-1} Similarly, there is a uniq

eulermathematicscompetitive-programming
Project Euler Problem 917

The sequence sn is defined by s1 = 102022661 and sn = s{n-1}^2 bmod {998388889} for n 1.

eulermathematicscompetitive-programming
Project Euler Problem 982

Solution to Project Euler Problem 982.

eulermathematicscompetitive-programming
Project Euler Problem 887

Consider the problem of determining a secret number from a set 1, ..., N by repeatedly choosing a number y and asking "I

eulermathematicscompetitive-programming
LeetCode 198 - House Robber

The problem describes a row of houses, where each house contains some amount of money. You are acting as a robber who wants to maximize the total amount stolen, but there is one important restriction, you cannot rob two adjacent houses.

leetcodemediumarraydynamic-programming
Project Euler Problem 903

A permutation pi of 1, dots, n can be represented in one-line notation as pi(1),ldots,pi(n) .

eulermathematicscompetitive-programming
Project Euler Problem 926

A round number is a number that ends with one or more zeros in a given base.

eulermathematicscompetitive-programming
Project Euler Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

eulermathematicscompetitive-programming
Project Euler Problem 923

A Young diagram is a finite collection of (equally-sized) squares in a grid-like arrangement of rows and columns, such t

eulermathematicscompetitive-programming
Project Euler Problem 907

An infant's toy consists of n cups, labelled C1,dots,Cn in increasing order of size.

eulermathematicscompetitive-programming
Project Euler Problem 918

The sequence an is defined by a1=1, and then recursively for ngeq1: The first ten terms are 1, 2, -5, 4, 17, -10, -17, 8

eulermathematicscompetitive-programming
Project Euler Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.

eulermathematicscompetitive-programming
Project Euler Problem 559

An ascent of a column j in a matrix occurs if the value of column j is smaller than the value of column j + 1 in all row

eulermathematicscompetitive-programming
Project Euler Problem 978

Solution to Project Euler Problem 978.

eulermathematicscompetitive-programming
Project Euler Problem 965

Let {x} denote the fractional part of a real number x.nnDefine fN(x) to be the minimal value of {nx} for integer n satis

eulermathematicscompetitive-programming
Project Euler Problem 639

A multiplicative function f(x) is a function over positive integers satisfying f(1)=1 and f(a b)=f(a) f(b) for any two c

eulermathematicscompetitive-programming
Project Euler Problem 987

Solution to Project Euler Problem 987.

eulermathematicscompetitive-programming
LeetCode 11 - Container With Most Water

The problem gives an array called height, where each element represents the height of a vertical line drawn on a coordinate plane. The line at index i starts at (i, 0) and ends at (i, height[i]).

leetcodemediumarraytwo-pointersgreedy
Project Euler Problem 924

Let B(n) be the smallest number larger than n that can be formed by rearranging digits of n, or 0 if no such number exis

eulermathematicscompetitive-programming
Project Euler Problem 983

Solution to Project Euler Problem 983.

eulermathematicscompetitive-programming
LeetCode 7 - Reverse Integer

The problem asks us to reverse the digits of a signed 32-bit integer. Given an integer x, we must return a new integer whose digits appear in reverse order while preserving the sign. For example, if the input is 123, reversing the digits produces 321.

leetcodemediummath
Project Euler Problem 981

Solution to Project Euler Problem 981.

eulermathematicscompetitive-programming
Project Euler Problem 8

The four adjacent digits in the 1000-digit number that have the greatest product are 9 times 9 times 8 times 9 = 5832.

eulermathematicscompetitive-programming
Project Euler Problem 909

An L-expression is defined as any one of the following: - a natural number; - the symbol A; - the symbol Z; - the symbol

eulermathematicscompetitive-programming
Project Euler Problem 306

The following game is a classic example of Combinatorial Game Theory: Two players start with a strip of n white squares

eulermathematicscompetitive-programming
LeetCode 208 - Implement Trie (Prefix Tree)

The problem asks us to design and implement a Trie, also called a Prefix Tree. A Trie is a specialized tree structure for storing strings in a way that makes prefix-based operations very efficient.

leetcodemediumhash-tablestringdesigntrie
LeetCode 206 - Reverse Linked List

The problem asks us to reverse a singly linked list. A singly linked list is a sequence of nodes where each node stores a value and a pointer to the next node in the sequence. The input, head, represents the first node of the linked list.

leetcodeeasylinked-listrecursion
Project Euler Problem 997

There are xyz dice arranged in an x times y times z box such that touching faces have the same value.

eulermathematicscompetitive-programming
Project Euler Problem 985

Solution to Project Euler Problem 985.

eulermathematicscompetitive-programming
LeetCode 197 - Rising Temperature

The problem gives us a database table named Weather that stores daily temperature records. Each row contains three fields: a unique id, a recordDate, and the temperature recorded on that date.

leetcodeeasydatabase
Project Euler Problem 993

Solution to Project Euler Problem 993.

eulermathematicscompetitive-programming
LeetCode 81 - Search in Rotated Sorted Array II

This problem asks us to determine whether a target value exists inside a rotated sorted array that may contain duplicate values. The original array was sorted in non-decreasing order, meaning values are arranged from smallest to largest, and duplicates are allowed.

leetcodemediumarraybinary-search
Project Euler Problem 904

Given a right-angled triangle with integer sides, the smaller angle formed by the two medians drawn on the the two perpe

eulermathematicscompetitive-programming
Project Euler Problem 6

The sum of the squares of the first ten natural numbers is, The square of the sum of the first ten natural numbers is, H

eulermathematicscompetitive-programming
Project Euler Problem 991

Solution to Project Euler Problem 991.

eulermathematicscompetitive-programming
Project Euler Problem 961

nThis game starts with a positive integer.

eulermathematicscompetitive-programming
Project Euler Problem 970

nStarting at zero, a kangaroo hops along the real number line in the positive direction.

eulermathematicscompetitive-programming
Project Euler Problem 958

The Euclidean algorithm can be used to find the greatest common divisor of two positive integers.

eulermathematicscompetitive-programming
Project Euler Problem 939

Two players A and B are playing a variant of Nim.

eulermathematicscompetitive-programming
Project Euler Problem 911

An irrational number x can be uniquely expressed as a continued fraction [a0; a1,a2,a3,dots]: where a0 is an integer and

eulermathematicscompetitive-programming
Project Euler Problem 777

For coprime positive integers a and b, let C{a,b} be the curve defined by: where t varies between 0 and 2pi.

eulermathematicscompetitive-programming
Project Euler Problem 989

Write Fn for the n-th Fibonacci number, with F1 = F2 = 1 and F{n+1} = Fn + F{n-1}.

eulermathematicscompetitive-programming
LeetCode 190 - Reverse Bits

The problem asks us to reverse the binary representation of a 32-bit integer. Instead of reversing the decimal digits of a number, we reverse the order of its individual bits.

leetcodeeasydivide-and-conquerbit-manipulation
LeetCode 5 - Longest Palindromic Substring

The problem asks us to find the longest substring of a given string that is also a palindrome. A palindrome is a sequence that reads the same forward and backward. The substring must be contiguous, which means the characters must appear next to each other in the original string.

leetcodemediumtwo-pointersstringdynamic-programming
LeetCode 196 - Delete Duplicate Emails

This problem provides a database table named Person with two columns: The id column is unique because it is the primary key. The email column may contain duplicate values, meaning multiple rows can share the same email address.

leetcodeeasydatabase
LeetCode 2 - Add Two Numbers

The problem gives us two non empty singly linked lists. Each linked list represents a non negative integer, but the digits are stored in reverse order. That means the head node contains the least significant digit.

leetcodemediumlinked-listmathrecursion
Project Euler Problem 992

Solution to Project Euler Problem 992.

eulermathematicscompetitive-programming