brain
tamnd's digital brain — notes, problems, research
43815 notes
The problem asks us to find the diameter of a tree, which is defined as the number of edges in the longest path between any two nodes. We are given an undirected tree represented as a list of edges. Each edge connects two nodes, labeled from 0 to n - 1.
The problem gives two strings, s and p, both containing only lowercase English letters. We need to find every starting index in s where a substring is an anagram of p.
The problem is asking us to determine whether it is possible to use exactly four numbers, each between 1 and 9 inclusive, to form a mathematical expression that evaluates to exactly 24. The numbers are given in an array called cards of length 4.
The problem asks us to build a basketball team with the maximum possible total score, while ensuring that the team does not contain any conflicts.
We start with a vector A = (x1, y1) and want to transform it into another vector B = (x2, y2). Two operations are allowed. We may rotate the current vector by 90 degrees clockwise, and we may add vector C = (x3, y3) any number of times. The operations may be mixed in any order.
The problem asks us to find pairs of songs whose total duration is divisible by 60. Specifically, we are given an array time where each element represents the length of a song in seconds. We need to count all unique pairs (i, j) where i < j and (time[i] + time[j]) % 60 == 0.
We are asked to determine whether a positive integer $x$ is extraordinarily nice. By the problem's definition, a number is extraordinarily nice if it has exactly the same number of even divisors as odd divisors. The input is a single integer $x$ between 1 and 1000.
We are given a sequence of integers representing the yearly income of a company starting from 2001. The first number is the income in 2001, the second in 2002, and so on. These values may be negative if the company incurred a loss that year.
We have a rectangular board where some cells are already blocked by and the remaining cells . must be covered by polyominoes. The allowed pieces are extremely flexible: any connected shape consisting of 2, 3, 4, or 5 cells may be used, with arbitrary rotations and reflections.
The problem asks us to design an efficient cache that follows the Least Recently Used, or LRU, eviction policy. A cache stores a limited number of key-value pairs.
The problem asks us to split a string into substrings such that every substring is a palindrome. Among all valid palindrome partitions, we must return the minimum number of cuts required. A cut divides the string into two parts.
The problem asks us to compute the Hamming distance between two integers. The Hamming distance is defined as the number of bit positions where the two numbers differ in their binary representation. For example, consider x = 1 and y = 4.
This problem asks us to compute the boundary traversal of a binary tree in a very specific order. The boundary is formed by combining four parts: 1. The root node 2. The left boundary, excluding leaves 3. All leaf nodes from left to right 4.
The problem asks us to determine the minimum number of deletion steps required to make two strings identical. We are given two input strings, word1 and word2, and in each step, we can delete exactly one character from either string.
You are given a list of events, where each event is represented as: - startDay - endDay - value If you attend an event, you must attend the entire interval from startDay to endDay, inclusive.
The problem is asking us to compute the time at which each car in a line of cars collides with the car immediately in front of it, or return -1 if it never collides. Each car is represented by its position and speed.
The problem gives us a string s that contains English letters and spaces. Inside this string, words are separated by spaces, and there may also be extra spaces at the beginning or end of the string. Our task is to return the length of the last word in the string.
This problem asks us to maximize profit from stock trading over a sequence of days. We are given an array prices, where prices[i] represents the stock price on the ith day.
This problem asks us to convert an Excel-style column title into its corresponding numerical index. In Excel spreadsheets, columns are labeled alphabetically: The labeling system works similarly to a positional number system, except instead of digits 0-9, it uses letters A-Z…
Lavrenty has a fixed amount of dough and several types of stuffing. Each stuffing type has a limited quantity and requires a certain amount of dough to make a bun, and each bun yields a profit.
That is a long, comprehensive reference document with multiple sections and full code in two languages. To keep quality
We are given a string representing an email address, but all the . symbols have been spelled out as dot and all the @ symbols as at. Our goal is to reconstruct the original, valid email address in its shortest form.
The problem requires transforming a rope of balloons into a "colorful" rope, which means no two consecutive balloons sha
The problem gives us two parallel arrays, keyName and keyTime. Each index represents a single key-card usage event. For example: Together, they describe when a specific employee used their key-card during a single day.
The problem gives us a binary matrix called grid, where: - 0 represents water - 1 represents land We can move only in four directions, up, down, left, and right.
The problem asks us to count the number of strings of length n composed only of vowels a, e, i, o, u such that each stri
We are given the maximum length of a single SMS message and one complete text consisting of sentences separated by spaces. A sentence always ends with one of ., ?, or !. Words contain only letters.
We have a tank that wants to move from point A to point B along the straight segment connecting them. The tank moves with constant speed $v$, which we must choose as small as possible. There are $n$ enemy tanks placed on the plane.
The problem asks whether it is possible to split a binary tree into two separate trees such that both resulting trees have the same sum of node values. The split must happen by removing exactly one edge from the original tree. A binary tree is given through its root node.
We are asked to construct a calendar from a list of city names. Each line of the calendar must contain exactly two city names separated by a given symbol, and all lines must have identical length. The calendar must use each city name exactly once.
This problem asks us to simulate a data stream where each element arrives with a unique ID in arbitrary order. Each element consists of an integer idKey (between 1 and n) and a string value.
Alice gives Bob a decimal number and asks him to rearrange its digits so that the resulting number is as small as possible, while still being a valid decimal number without leading zeroes.
The problem asks us to find the optimal location to build a house on a grid so that the total travel distance to all existing buildings is minimized. The input is an m x n grid of integers where each cell is either empty land (0), a building (1), or an obstacle (2).
The problem gives an array of intervals, where each interval is represented as [start, end]. Each interval describes a continuous range of values from start to end, inclusive. The goal is to combine all intervals that overlap into a single larger interval.
This problem gives us the root of a binary tree where every node contains either 0 or 1. Each path from the root node to a leaf node represents a binary number, where the root contributes the most significant bit and each child adds another bit to the right.
Here is the complete, detailed technical solution guide for LeetCode 1648 - Sell Diminishing-Valued Colored Balls follow
The problem gives us a two dimensional grid of size m x n, where each cell contains a non-negative integer. We begin at the top-left corner of the grid, specifically at position (0, 0), and want to reach the bottom-right corner at position (m - 1, n - 1).
We are asked to simulate a turn-based game involving two players, Ciel and Hanako, who alternate taking coins from a common pile. The pile initially contains x 100-yen coins and y 10-yen coins. On each turn, the active player must remove exactly 220 yen.
The problem asks us to compute the total Hamming distance between every possible pair of integers in the array. The Hamming distance between two integers is defined as the number of bit positions where the two numbers differ.
We need to construct a binary prefix code.
We are given an encrypted password represented as a binary string of length 80. The original password had exactly 8 decimal digits, and each digit was encoded into a block of 10 binary characters.
The problem gives us the root node of a binary tree and asks whether the tree is "uni-valued". A binary tree is considered uni-valued if every node in the tree contains exactly the same integer value.
This problem asks us to distribute candies to children standing in a line, based on their rating values. Each child must receive at least one candy, and any child with a higher rating than an adjacent neighbor must receive strictly more candies than that neighbor.
This problem asks us to find the Lowest Common Ancestor, or LCA, of multiple nodes in a binary tree. Unlike the classic
In this problem, we are given a binary array called bits. The array represents a sequence of encoded characters using the following rules: - A one-bit character is represented by a single 0 - A two-bit character is represented by either 10 or 11 The array is guaranteed to end…
The problem is asking us to find two disjoint palindromic subsequences from a given string s such that the product of their lengths is maximized. A subsequence is derived by deleting zero or more characters from the original string while keeping the relative order intact.
The problem gives two strings, secret and guess, representing two numbers of equal length. We need to compare them and return a hint in the format "xAyB". A "bull" is a digit that matches in both value and position.
The problem gives us a sentence where words are separated by single spaces, the very first character of the sentence is
The problem requires creating a masked version of a personal information string s that can either be an email address or a phone number. The goal is to obscure sensitive information while keeping enough data to identify the user minimally.
This problem gives us a rectangular cake with height h and width w. We are also given two arrays: - horizontalCuts, whic
We are given an undirected graph and need to decide whether it matches a very specific structure. The graph should contain exactly one simple cycle, and every other vertex must belong to a tree attached to some vertex on that cycle.
We are given a string written in the Borze encoding system. Every digit of a ternary number is represented by one of three patterns:
The problem asks us to find the lexicographically largest permutation that is still smaller than the given array, using exactly one swap operation.
We are asked to examine permutations of numbers from 1 to n and focus on "lucky numbers" - integers that contain only the digits 4 and 7.
The problem requires finding the kth smallest sum obtainable by selecting exactly one element from each row of a matrix mat with sorted rows. Each row is sorted in non-decreasing order, and we must explore combinations of elements across rows to form sums.
Here’s a full, detailed technical solution guide for LeetCode 585 following your exact formatting rules: The problem presents an Insurance table containing policyholder information, including a unique policy ID (pid), investment values for 2015 (tiv2015) and 2016 (tiv2016)…
This problem asks us to determine which day of the week corresponds to a given calendar date. The input consists of three integers: - day, representing the day within the month - month, representing the month number from 1 to 12 - year, representing the year We must return the…
This problem asks us to simulate repeated shift operations on a 2D matrix. We are given an m x n grid and an integer k,
The problem asks us to design a dictionary-like data structure that supports two operations. First, we load a collection of distinct words into the structure.
This problem is asking us to calculate the probability that, when tossing a set of coins, exactly a specific number of them come up heads. Each coin has its own individual probability of landing heads, which is given in the input array prob.
The problem asks us to compute the maximum alternating subarray sum for a given integer array. A subarray is any contiguous sequence of elements from the array.
We start with an array of positive integers. We must change exactly one element to a different value, still between 1 and 10^9. After that single replacement, we sort the array in non-decreasing order.
In this problem, we are given the root node of a binary tree. Every node belongs to a specific level in the tree. The root is at level 1, its direct children are at level 2, their children are at level 3, and so on.
The problem gives us an m x n multiplication table where each cell contains the product of its row index and column index, using 1-based indexing. That means the value at position (i, j) is simply i j.
This problem asks us to identify the project or projects that have the largest number of employees assigned to them. We are given two database tables. The Project table stores relationships between projects and employees.
The problem asks us to populate the next pointer for every node in a binary tree so that it points to the node immediately to its right on the same level. If no such node exists, the next pointer should remain NULL.
We are given a set of wooden bars, each with a positive integer length. Vasya wants to build towers by stacking bars of the same length. Each tower must consist of bars that are identical in length, but different towers can have different lengths.
Valera has a shopping list containing a number of fruits, possibly with duplicates if he wants more than one of the same type. At the market, the stall has _n_ types of fruits and _n_ price tags, but the tags are not yet attached to the fruits.
We are given three lines representing the three phrases of a poem. A valid haiku must contain exactly 5 vowel letters in the first phrase, 7 in the second, and 5 in the third. For this problem, syllables are simplified into vowel counts.
We have n towers on a 2D plane. Every tower must belong to exactly one of two generals. For each general, the cost he demands is the maximum Manhattan distance between any two towers assigned to him. The king only pays the larger of the two costs.
The problem asks us to identify products that were sold only during the first quarter of 2019, meaning between 2019-01-01 and 2019-03-31, inclusive. We are provided with two tables: Product and Sales.
We are filling an n × m grid with brackets. Every cell contains either "(" or ")". The grid is called valid if every monotone path from the top-left corner to the bottom-right corner forms a correct bracket sequence. A monotone path only moves right or down.
The problem asks us to construct a special integer sequence using numbers from 1 to n. The resulting sequence has length 2 n - 1 because: - The number 1 appears exactly once. - Every number from 2 to n appears exactly twice.
The problem gives an integer array nums and asks whether any number appears more than once. If at least one value is repeated, we return true. If every value appears exactly once, we return false. The input is a list of integers.
The problem gives us a database table named MyNumbers that contains a single column, num. The table may contain duplicate values because there is no primary key restriction. Our task is to find the largest number that appears exactly once in the table.
The problem asks us to identify all distinct integers that appear in at least two out of three given integer arrays. The input consists of three arrays nums1, nums2, and nums3, each containing integers in the range [1, 100].
The problem asks us to calculate the sum of widths for all non-empty subsequences of a given integer array nums. A subsequence is any sequence derived by removing zero or more elements from the original array while maintaining the order.
The office is represented as a rectangular grid. Every uppercase letter represents part of a desk, and all cells with the same letter belong to the same rectangular desk. The president’s desk color is given as c.
We are given two pieces of information about a person riding an elevator. The first input tells us which door the person used to enter, either the front door or the back door. The second input tells us which rail the person was holding, rail 1 or rail 2.
We are given a rectangular slab divided into an grid of squares. Each square is either black or white in the final design, which is provided as input. The slab initially starts entirely white.
The office floor is represented as an n × m grid. Each cell is either free, written as 0, or blocked by furniture, written as 1. We want to place one rectangular table whose sides stay aligned with the grid. Every cell covered by the rectangle must be free.
The problem requires rearranging spaces in a given string text so that all words are separated by the maximum possible e
The problem describes a scenario where we have a limited number of opportunities to invest in projects before an IPO.
The problem gives a collection of words on the left side of an equation and a single result word on the right side. Each distinct uppercase letter must be assigned a unique digit from 0 to 9.
The problem asks us to count the number of good triplets in an integer array arr based on three difference constraints a, b, and c.
The problem is asking for the minimum number of operations required to generate exactly n characters 'A' on a notepad starting with a single 'A'.
We start with the array [1, 2, 3, ..., n]. A recursive transformation rearranges it by repeatedly taking all elements at odd positions, transforming that subarray, then taking all elements at even positions and transforming that subarray.
We are given a party scenario where Petya invited n friends, each of whom gave exactly one gift to another friend. The input lists, for each friend in order, the friend they gave a gift to.
This problem asks us to implement a thread-safe bounded blocking queue, which is a data structure that stores elements in a first-in-first-out (FIFO) order, but with the added constraint that multiple threads may be concurrently accessing it.
We are given a line of people, each identified as a boy or a girl, and each with a numeric dancing skill. The line evolves over time as couples consisting of one boy and one girl who are adjacent and have the smallest difference in skill leave to dance.
This problem asks us to find all root-to-leaf paths in a binary tree where the sum of the node values equals a given targetSum. A binary tree is provided through the root node, and every node contains an integer value. We are also given an integer targetSum.
That is a long, structured technical document with multiple required sections, complete Python and Go implementations, w
We are asked to construct a fully connected graph of _N_ tourist attractions, where each road has a distinct positive integer cost not exceeding 1000.
The problem gives us a singly linked list where every node contains a unique integer value. We are also given an array nums, and every value in nums is guaranteed to appear somewhere in the linked list.
The problem gives an array nums containing n distinct integers. Every number is supposed to come from the range [0, n], which means there are actually n + 1 possible values in total. Since the array only contains n numbers, exactly one value from that range is missing.
The problem asks us to find the shortest superstring that contains all the given strings in the array words as substrings.
The problem gives us a sorted integer array arr, along with two integers, k and x. We need to return exactly k elements from the array that are closest to x.
The problem asks us to traverse a jagged 2D integer array nums diagonally. Specifically, elements are accessed by their
Five people stand in a queue in a fixed order: Sheldon, Leonard, Penny, Rajesh, Howard. Whenever the person at the front buys a cola, that person immediately creates a copy of themselves, and both copies go to the back of the queue. The queue keeps growing forever.
We are asked to help King Copa maximize his expected number of hits in a shooting gallery. The gallery is represented as a 2D plane, and each target appears at a specific point exactly at a specific time and disappears immediately afterward.