brain
tamnd's digital brain — notes, problems, research
43815 notes
This problem asks us to sort an array that contains only three distinct values: 0, 1, and 2. These values represent colors: - 0 represents red - 1 represents white - 2 represents blue The goal is to rearrange the array so that all 0s come first, followed by all 1s, followed by…
This problem asks us to compute the minimum number of moves a knight needs to travel from the origin [0, 0] to a target position [x, y] on an infinite chessboard.
The keyboard is arranged in a fixed grid layout containing the 26 uppercase English letters. Each letter has a coordinat
We start with a set of distinct lattice points on the plane. We may add more points, and the final set must satisfy a geometric condition for every pair of points. Take any two points.
This problem asks us to convert a non-negative integer n into its representation using base -2, instead of the usual positive bases such as base 2, base 10, or base 16. In standard binary representation, numbers are written as powers of 2. For example: which becomes "1101".
We are given a house with a certain number of rooms connected by doors, and each door has a unique key. There are several residents in the house, each initially in some room with some keys. We also know the positions and key holdings of every resident at a later time.
We are given a rectangular plaza, the Theatre Square, with dimensions _n_ meters in length and _m_ meters in width. The city wants to pave the entire area with square flagstones of side _a_.
The problem gives a list of online courses, where each course is represented as [duration, lastDay]. The duration tells us how many consecutive days are required to complete the course. The lastDay tells us the latest possible day by which the course must be finished.
In this problem, we are given two strings: - word, the original full word - abbr, a supposed abbreviation of that word We must determine whether abbr is a valid abbreviation of word. An abbreviation works by replacing one or more non-empty substrings with their lengths.
This problem asks us to find the cheapest valid path through an array while respecting jump constraints and blocked positions. The array coins is 1-indexed in the problem statement, although programming languages will use 0-indexed arrays internally.
This problem asks us to retrieve the second highest distinct salary from the Employee table. The key word here is distinct. We are not looking for the second row after sorting salaries, we are looking for the second unique salary value.
The problem asks us to calculate the total time each employee spends in the office per day based on a table of their in and out times.
We have an L-shaped corridor with widths a and b on the two branches. A rectangular coffin of fixed length l must be moved through the corner.
The problem gives us a list of tasks, where each task is represented as [actual, minimum]. For every task: - minimum is the amount of energy we must currently have before we are allowed to start the task.
The problem asks us to implement a multi-threaded web crawler that starts from a given URL startUrl and visits all URLs reachable from it, but only those that share the same hostname.
We are given two positive integers, and then many range queries. For each query [low, high], we must find the largest integer inside that interval that divides both numbers. The first observation is that we are not really working with a and b independently.
The problem gives us two water jugs with capacities x and y. We can perform only three types of operations: - Fill a jug completely - Empty a jug completely - Pour water from one jug into the other until either the source jug becomes empty or the destination jug becomes full…
This problem asks us to generate the shortest possible abbreviation for a given target word such that the abbreviation cannot also represent any word in the dictionary. An abbreviation replaces one or more non-adjacent substrings with their lengths.
This problem asks us to design a miniature peer-to-peer file sharing system. The file is divided into m chunks, where every chunk has an ID from 1 to m. Users can join the system while already owning some chunks, leave the system entirely, and request chunks from other users.
We are given a decimal number as a string. The number contains an integer part, then a dot, then a fractional part. The task is to simulate the kingdom's strange rounding rules. The rules are intentionally incomplete.
The problem asks us to find the intersection of two integer arrays, but with an important detail: duplicates matter. If a number appears multiple times in both arrays, it must appear in the result as many times as it appears in both.
This problem asks us to identify students who are "quiet" across all exams they participated in. A student is considered
The problem asks us to count all substrings in a string where every character inside the substring is identical. These are called homogenous substrings. A substring must be contiguous, meaning the characters must appear next to each other in the original string.
We are given two lowercase strings of the same length. We may repeatedly transform characters using directed conversion rules. A rule like a -> b with cost 5 means we can change one occurrence of a into b by paying 5.
The problem asks us to generate every possible valid combination of n pairs of parentheses. A valid parentheses string is one where every opening parenthesis ( has a matching closing parenthesis ), and the parentheses are properly nested.
We are given a list of integers where every number is either 1, 2, or 3. The goal is to transform the sequence so that all numbers are the same, and we want to do this using the smallest number of replacements possible.
The problem gives an integer array nums, and asks us to choose two different indices i and j. For those two elements, we compute the expression: Our goal is to return the maximum possible value of this expression.
The problem asks us to generate every possible permutation of the given array nums, while ensuring that duplicate permutations are not included in the final result. A permutation is an arrangement of all elements in a particular order.
This problem asks us to determine whether an entire input string s matches a wildcard pattern p. The pattern supports two special wildcard characters: - '?' matches exactly one character. - '' matches any sequence of characters, including an empty sequence.
This problem involves a scenario with n passengers and n seats on an airplane. Each passenger has a designated seat, but the first passenger has lost their ticket and picks a seat randomly.
This problem asks us to build a new array called target by following a sequence of insertion operations. We are given tw
Edit This problem gives us a weighted, undirected graph where each node represents a city and each edge represents a bid
We are asked to determine whether a given number is almost lucky. A number is almost lucky if it is divisible by at least one lucky number. Lucky numbers are positive integers composed entirely of the digits 4 and 7, like 4, 7, 44, 47, 74, and so on.
The problem provides a singly linked list and an integer k. The task is to swap the values of the kth node from the start with the kth node from the end of the list. The list is 1-indexed, meaning that the first node is position 1, not 0.
This problem asks us to construct a new string from the input string s such that every distinct character appears exactly once, while also ensuring that the final result is the smallest possible in lexicographical order.
The UserVisits table stores records of when users visited a retailer. Each row contains a userid and a visitdate. A user may appear multiple times because they can visit on many different days.
The problem describes a country, Berland, with n cities arranged in a perfect ring. Originally, each city had two-way roads connecting it to its two neighbors, so it was trivial to travel from any city to any other.
The problem asks us to analyze sales data stored in a Sales table and extract the records corresponding to the first year each product was sold.
This problem gives us an array of unique strings called dict, where every string has the same length. We must determine whether there exists at least one pair of strings that differ by exactly one character at the same position.
This problem asks us to determine whether a given chessboard square is white or black based on its coordinate. The input is a string of length two, where the first character is a lowercase letter from 'a' to 'h', representing the column, and the second character is a digit…
The problem gives us an array called groupSizes, where each index represents a person and each value represents the size
Here’s a full technical solution guide following your formatting rules for LeetCode 629: The problem asks us to determine the number of arrays of length n containing numbers from 1 to n such that there are exactly k inverse pairs.
We are given a list of integers of length _N_, which may contain negative numbers, zeros, or large positive numbers. The task is to transform this list into a non-decreasing sequence, where each element is at least as large as the previous one.
We are given a connected undirected graph representing a road network. Every junction is a vertex, every road is an edge, and there is at most one edge between any pair of vertices.
The problem requires implementing a BSTIterator class that allows forward and backward traversal over the in-order sequence of a Binary Search Tree (BST). In-order traversal of a BST visits nodes in ascending order.
In this problem, we are given a list of travel days during a single year and the costs of three different train passes. Each pass covers a consecutive range of days: - A 1-day pass covers exactly one day. - A 7-day pass covers seven consecutive days.
The problem gives us a sorted array of lowercase English letters and a target character. Our task is to find the smallest character in the array that is strictly greater than the target character in lexicographical order.
We are asked to tile an rectangular floor with three types of 2x2 square tiles: black, white, and mixed tiles that have a black and a white section in a diagonal pattern.
We have two independent lists, one containing names and one containing surnames. Every name must be paired with exactly one surname, and every surname must be used exactly once. After choosing the matching, we print all pairs in one comma-separated line.
We are given a rectangular grid representing a country’s flag. Each cell in the grid has a color, encoded as a digit from 0 to 9. The dimensions of the flag are rows by columns. The goal is to determine whether the flag is “striped” according to the new ISO standard.
The problem asks us to generate strings of length n using only the characters 'a', 'b', and 'c', with one important rest
We have a grid of digits. The pawn starts somewhere on the bottom row and moves upward one row at a time. At each step it may go diagonally left or diagonally right. Every visited cell contributes its digit to the total collected peas.
This problem asks us to compute the total area covered by a set of axis-aligned rectangles on a 2D plane. Each rectangle is defined by its bottom-left and top-right coordinates [xi1, yi1, xi2, yi2].
The problem asks us to determine the maximum depth of a binary tree. A binary tree consists of nodes where each node can have up to two children, a left child and a right child.
We are given a tree with n vertices where each edge has a positive weight. Some edges are considered "lucky" if their weight consists only of the digits 4 and 7.
The graph has exactly n vertices and n edges. A connected graph with n vertices and n edges contains exactly one simple cycle. Every other edge belongs to a tree attached to that cycle. Initially every edge is turned off.
In this problem, we are given an integer array called satisfaction, where each element represents the satisfaction level
We are given three musical notes, and the task is to classify the chord they form as either major, minor, or "strange". Notes are represented in the twelve-tone chromatic scale: C, C, D, D, E, F, F, G, G, A, B, H, and the scale is cyclic, so after H comes C again.
The problem asks us to find the optimal location for a service center on a 2D map such that the total Euclidean distance
The problem asks us to compute the maximum depth of an n-ary tree. An n-ary tree is a tree where each node can have zero or more children, unlike a binary tree where each node has at most two children.
We are given a one-dimensional collider with n particles, each with a starting position x_i and a velocity v_i. Positive velocity means a particle moves right, negative velocity means it moves left.
The problem asks us to count how many non-empty contiguous subarrays have a sum that is divisible by a given integer k. A subarray is any continuous segment of the array. For example, in the array [1,2,3], the subarrays include [1], [2], [3], [1,2], [2,3], and [1,2,3].
The problem asks us to determine whether certain substrings of a given string s can be rearranged and partially modified to form a palindrome. A palindrome is a string that reads the same forward and backward.
We are asked to count how many numbers from 1 to n consist only of the digits 0 and 1 in their decimal representation. In other words, Hexadecimal's memory only stores numbers that, when written in base 10, contain no digits other than 0 or 1.
This problem asks us to control a car moving on an infinite one dimensional number line. The car starts at position 0 with speed +1.
The problem gives us a circular array called code and an integer k. We must produce a new array where each element is replaced according to the value of k. If k 0, each element becomes the sum of the next k elements in the circular array.
We are given a positive integer and need to decide whether it is a nearly lucky number. A number is considered nearly lucky if the count of its lucky digits-digits equal to 4 or 7-is itself a lucky number. A lucky number contains only 4s and 7s in its decimal representation.
The problem asks us to compute the minimum number of edit operations needed to transform one string into another. The allowed operations are insertion, deletion, and replacement of a single character. Each operation has a cost of exactly one.
This problem asks us to count how many subarrays have sums that fall within a given inclusive range [lower, upper]. A range sum S(i, j) represents the sum of all elements from index i through index j in the array.
The problem gives us an integer array nums and an integer k. For every element in the array, we are allowed to modify it once by adding any integer value in the range [-k, k].
This problem asks us to find the smallest element in a sorted array that has been rotated some number of times. A sorted array in ascending order might originally look like this: After rotation, it could become: or remain unchanged: The important observation is that the array…
We have n seats and n professors. Professor 1 is the most senior, professor n is the least senior. Some pairs of seats impose ordering constraints. If (a, b) is given, then the professor sitting in seat a must be more senior than the professor sitting in seat b.
We are given two arrays of positive integers, arr1 and arr2. The task is to determine the maximum length of a common dig
The problem provides a collection of employees where each employee contains three pieces of information: - A unique employee ID - An integer importance value - A list of IDs representing their direct subordinates We are also given the ID of one employee, and we must calculate…
In this problem, we are given a list of log strings. Each log contains an identifier followed by one or more words separated by spaces. The first token is always the identifier, and everything after it represents the content of the log.
Here is the complete, detailed technical solution guide for LeetCode 1044 - Longest Duplicate Substring, following your formatting rules exactly. The problem asks us to find the longest duplicated substring in a given string s.
This problem asks us to find the k-th smallest distance among every possible pair of numbers in the array. For any pair (a, b), the distance is defined as: We are given an array nums, and we must consider every pair (nums[i], nums[j]) where i < j.
The problem gives us a tree with n nodes numbered from 0 to n - 1. The tree is represented by an array of edges, where each edge connects two nodes ai and bi. Each node also has a label, represented as a string labels, where labels[i] is the label of node i.
The problem describes a robot moving on an infinite two dimensional grid. The robot begins at coordinate (0, 0) and initially faces north, which corresponds to the positive Y direction.
We are given a convex polygon defined by a list of points in clockwise order. For each query point in the plane, we need to count how many triangles formed by the polygon’s vertices contain that point strictly inside.
The problem asks us to sort every diagonal of a matrix independently in ascending order. A diagonal is defined as a sequence of cells that begins either from the first row or the first column, then continues by repeatedly moving one row down and one column to the right.
The problem gives us an array candiesCount where candiesCount[i] represents how many candies exist for candy type i.
The problem asks us to partition an array into the maximum possible number of contiguous chunks such that, after sorting each chunk independently and concatenating the results, the entire array becomes globally sorted. The key detail is that each chunk must remain contiguous.
Here is a comprehensive, detailed solution guide for LeetCode 1337 following your exact instructions. The problem provid
This problem provides two database tables, Candidate and Vote, and asks us to determine which candidate won the election. The Candidate table stores information about each candidate. Every candidate has a unique integer id and a corresponding name.
This problem asks us to determine the minimum number of boats required to rescue all people, given two constraints. Each boat can carry at most two people, and the combined weight of the people in a boat cannot exceed the given weight limit.
The problem asks us to transform a binary tree into a formatted 2D string matrix representation. Each node must appear in a specific row and column based on its position in the tree. The output is not simply a traversal order, it is a visual layout of the tree structure.
The problem provides access to a single API, rand7(), which returns a uniformly random integer from 1 to 7. The task is to implement another function, rand10(), which must return a uniformly random integer from 1 to 10.
This problem provides a 2D grid representing a map of land and water. Cells with 0 represent land, and cells with 1 repr
The problem describes a frog trying to cross a river by jumping across stones placed at specific positions. The input array stones contains the positions of all stones in sorted ascending order. The frog starts on the first stone, which is always at position 0.
The problem describes a car traveling from position 0 to a destination located target miles away. The car starts with startFuel liters of fuel, and every mile driven consumes exactly one liter of fuel.
The problem gives us the head of a singly linked list whose values are already sorted in ascending order. We must convert this linked list into a height balanced Binary Search Tree, usually abbreviated as BST.
This problem describes a group of people where some people are known to be richer than others. The richer array represents directed relationships between people. If richer[i] = [a, b], it means person a definitely has more money than person b.
This problem gives us two strings, s and t. The string t is created by taking all characters from s, shuffling them into a different order, and then inserting exactly one additional character somewhere in the string. Our task is to identify and return that extra character.
The problem provides an m x n binary matrix, grid, where each element is either 0 or 1. You are allowed to perform a move, which consists of selecting any row or column and flipping all its values - turning every 0 into a 1 and every 1 into a 0.
The problem gives us a string s and a target character c. For every index in the string, we must compute the distance to the nearest occurrence of c. The distance between two indices is defined as: where i is the current index and j is the index of some occurrence of c.
We start with a monitor whose dimensions are a × b. Both values are integers. We want to shrink this monitor so that the new dimensions keep the exact aspect ratio x : y.
Solomon stands on the fortress wall at position 0. To his right there may exist a chain of ice blocks occupying positions 1, 2, .... Initially there are no blocks at all. The battlefield is a line of length n. At battlefield position i, there may be a demon with strength a[i].
We are asked to find the maximum number of "full lucky numbers" that can appear after adjusting segments of numbers. A lucky number is any positive integer whose digits consist only of 4 and 7. Each segment is a range [li, ri] on the number line.
The problem gives us two soups, A and B, each starting with exactly n milliliters. At every turn, one of four serving operations is chosen uniformly at random. Each operation removes different amounts from A and B simultaneously.