brain
tamnd's digital brain — notes, problems, research
43815 notes
In this problem, we are given an n x n grid where each cell contains a unique integer representing its elevation. Rain begins falling, and the water level rises over time.
The problem asks us to determine how many complete rows of a coin staircase can be formed using exactly n coins. A staircase arrangement follows a very specific structure: - The first row contains 1 coin - The second row contains 2 coins - The third row contains 3 coins -…
We are given two integer arrays. We need to build the longest sequence that satisfies two conditions at the same time.
We are given an array of positive integers and many interval queries. For each query [l, r], we look only at the subarray between those indices and compute a value called its power. If a number x appears k times inside the subarray, then x contributes k² x to the answer.
This is a SQL database problem where we need to calculate the total sales amount per product for each year, even when a
The problem asks us to count how many permutations of the numbers 1 through n are valid derangements. A derangement is a permutation where no element remains in its original position. For example, when n = 3, the original array is [1, 2, 3].
This problem asks us to count how many different ways a ball can leave the boundaries of a grid within a limited number of moves.
Each camel stands at a unique coordinate on a number line. A camel at position x spits exactly toward position x + d. If another camel stands there, it gets hit. We need to determine whether there exists a pair of camels such that each one hits the other.
The problem asks us to design a miniature spreadsheet system similar to Microsoft Excel. The spreadsheet contains cells arranged in rows and columns, where rows are numbered starting from 1 and columns are labeled using uppercase letters such as A, B, C, and so on.
The problem asks us to calculate the maximum absolute sum of any contiguous subarray in a given integer array nums. A subarray is a sequence of consecutive elements, and its absolute sum is defined as the absolute value of the sum of all elements in that subarray.
We are given a string s consisting of uppercase and lowercase Latin letters, and we are asked to analyze its substrings according to a specific property: a substring is "good" if the number of vowels it contains is at most twice the number of consonants.
This problem asks us to determine whether a given binary tree satisfies the rules of a Binary Search Tree, commonly abbreviated as BST. A binary tree consists of nodes where each node contains a value and pointers to a left child and a right child.
The problem is asking us to rearrange a list of barcodes so that no two adjacent barcodes are the same. The input is an array barcodes of integers where each integer represents a type of barcode. The output should be a rearranged array that satisfies the adjacency constraint.
We are given a three-dimensional grid representing the inside of a plate. The grid has k layers, each layer has n rows and m columns. Every cell is either empty . or blocked . Water starts entering from one specific cell on the top layer.
This problem asks us to determine whether a given binary tree is height-balanced. A binary tree is considered height-balanced if, for every node in the tree, the height difference between its left and right subtrees is at most 1.
The problem gives us a connected, weighted, undirected graph with n vertices and a list of edges. Each edge is represented as [u, v, weight], meaning there is a bidirectional connection between vertices u and v with the given cost.
The problem asks us to compute the minimum absolute difference in subarrays of a given integer array nums for multiple q
The problem gives us the root of a binary tree where every node stores an integer between 0 and 25. Each integer corresponds to a lowercase English letter: - 0 - 'a' - 1 - 'b' - ... - 25 - 'z' We need to construct strings that begin at a leaf node and move upward toward the root.
This problem gives us the root of a Binary Search Tree, abbreviated as BST, along with an integer k. We need to return the kth smallest value in the tree. A Binary Search Tree has a very important property: - Every value in the left subtree is smaller than the current node.
The problem requires determining how many items can be stored in a warehouse with a limited square footage of 500,000. T
This problem works with three relational database tables: Movies, Users, and MovieRating. The goal is to produce a result containing exactly two rows. The first row should contain the name of the user who rated the greatest number of movies.
The problem asks us to round an array of decimal prices to integers such that the sum of the rounded numbers equals a given target, while minimizing the total rounding error. Each price can be rounded either down (Floor) or up (Ceil).
We are given two decimal integers, a and c. The computer in this problem does not use binary xor. Instead, it uses a ternary operation called tor. To apply tor, both numbers are written in base 3.
The problem is asking us to convert a given integer n from base 10 into another base k, then compute the sum of its digits in that base. For example, if n is 34 and k is 6, first we convert 34 into base 6, which yields 54, and then sum the digits: 5 + 4 = 9.
We are asked to route two people, Bob and Alex, across a town represented as an undirected graph with n crossroads and m roads. Bob starts at node 1 and wants to reach node n, while Alex starts at node n and wants to reach node 1.
The problem asks us to build an index mapping from nums1 to nums2, where nums2 is guaranteed to be an anagram of nums1. Since an anagram means the same elements appear in both arrays, but possibly in a different order, every value in nums1 must appear somewhere in nums2.
This problem asks whether we can satisfy a set of customer requests using repeated integers from the array nums. Each customer wants a certain quantity of numbers, given by quantity[i]. The important restriction is that every number given to a single customer must be identical.
We are given a decimal string and an operation count k. Each operation looks for the leftmost occurrence of the substring "47". Suppose the substring "47" starts at position x using 1-based indexing. If x is odd, we replace both digits with '4', so "47" becomes "44".
The problem asks us to modify an integer array so that it becomes the next lexicographically greater permutation of its current arrangement. A permutation is simply an ordering of the elements. Lexicographical order works the same way dictionary order works for words.
LeetCode 428, LeetCode Serialize and Deserialize N-ary Tree, asks us to design a reversible encoding system for an N-ary tree. The problem is not asking for a specific serialization format.
The problem asks us to count how many palindromic substrings exist inside a given string s. A substring is any contiguous segment of the string. This means we cannot rearrange characters or skip positions.
This problem gives us the root node of a Binary Search Tree, commonly abbreviated as a BST, along with an integer value val. Our goal is to locate the node whose value is exactly equal to val and return that node.
We are given a string representing the genome of an organism, where each character is one of the first K capital letters. Adjacent genes contribute to the total “risk of disease” according to a given K × K matrix of non-negative integers.
This problem models a very large chessboard-like grid where certain cells contain active lamps. A lamp illuminates four directions simultaneously: - Its entire row - Its entire column - Its main diagonal, identified by row - col - Its anti-diagonal, identified by row + col For…
This problem asks us to count how many contiguous subarrays of length k have an average value greater than or equal to a
The problem gives us a directed graph of cities connected by roads. There are n cities numbered from 0 to n - 1, and exactly n - 1 roads. Since the graph contains n - 1 edges and there is exactly one path between any pair of cities, the graph forms a tree.
The problem asks us to generate all integers from 1 to n, but not in normal numerical order. Instead, the numbers must appear in lexicographical order, also called dictionary order. Lexicographical order compares numbers as strings rather than as numeric values.
This problem asks us to create a completely independent copy of a linked list where each node contains two pointers: - next, which points to the next node in the list - random, which can point to any node in the list or null The key requirement is that the copied list must be…
The problem asks us to split a given integer n into two positive integers a and b such that: - a + b = n - Neither a nor b contains the digit 0 anywhere in their decimal representation Such integers are called No-Zero integers.
The problem gives us a sorted integer array nums and a quadratic transformation function: We must apply this function to every element in the array and return the transformed values in sorted order.
The problem asks us to find the maximum sum of elements from an integer array nums such that the sum is divisible by thr
Every student independently chooses one of the bathroom rooms uniformly at random. A room may contain several wash basins, so students entering that room are split into several queues.
This problem gives us two database tables, Users and Rides. The Users table contains information about each user. Every user has a unique id and a corresponding name. The Rides table contains ride records.
We have exactly five people. Some pairs of people know each other, and the input lists all such acquaintance relations. The task is to determine whether there exists either: 1. Three people where every pair knows each other. 2. Three people where no pair knows each other.
The problem gives us a binary matrix, meaning every cell contains either 0 or 1. We are allowed to rearrange the columns of the matrix in any order we want.
The problem gives us a sorted array of distinct integers and a target value. Our task is to determine where the target belongs in the array. If the target already exists, we return its index.
The problem gives two dates in the format YYYY-MM-DD and asks us to compute the absolute number of days between them. Each input string represents a valid calendar date. The year, month, and day are separated by hyphens.
The problem asks us to generate all integers of length n such that the absolute difference between every two consecutive digits is exactly k.
The problem is asking us to partition an integer array nums into exactly k subsets of equal size, such that no subset co
The problem asks us to compute the XOR coordinate value for each element in a given 2D matrix and then find the kth largest among them.
This problem extends the mechanics introduced in earlier Maze problems, but adds two important complications. First, we are no longer looking for a simple reachable or unreachable answer. Instead, we must find the shortest path by travel distance.
Yesterday the Gauls defeated n Roman soldiers, and n is guaranteed to be a prime number. Today they defeated m soldiers, where m n. We need to decide whether m is exactly the next prime number that comes immediately after n. The key detail is the phrase "next prime".
This problem asks us to determine whether a given word can be legally placed into a crossword board while respecting crossword placement rules.
We are given a sequence of books by Berlbury, each with a known height, arranged chronologically. The library wants to organize an exposition by selecting consecutive books such that the difference between the tallest and shortest book in the selection does not exceed a given…
The problem is asking us to swap the sex values of all employees in a Salary table. Each row in the table represents an employee with four columns: id, name, sex, and salary. The sex column is an ENUM with values 'm' for male and 'f' for female.
The problem provides a Customer table with three columns: id, name, and refereeid. Each row represents a customer, their unique identifier (id), their name, and optionally the id of the customer who referred them.
This problem asks us to identify every (actorid, directorid) pair where an actor and a director have collaborated at least three times. The input is a database table named ActorDirector. Each row represents one collaboration event between an actor and a director.
We start from the sorted permutation 1 2 3 ... n. Someone chooses exactly one contiguous segment and reverses it. We are given the final permutation and must determine whether it could have been produced by exactly one such reversal. The task is not to sort the array.
In this problem, a biker starts at altitude 0 and travels through a sequence of roads. The input array gain describes how the biker's altitude changes between consecutive points on the trip. If gain[i] is positive, the biker climbs upward between point i and point i + 1.
The problem requires simulating the transformation of an array over consecutive days according to a simple local rule. Each day, every element of the array (except the first and last) is compared with its immediate neighbors.
We are given a sequence of integers, and the task is to maximize its sum by performing two operations: first, we may choose any prefix of the sequence and multiply every element in it by -1; second, we may choose any suffix of the sequence and multiply every element in it by -1.
The problem gives us an integer array arr and asks whether every distinct number appears a unique number of times. In other words, we first count how many times each value occurs in the array.
The problem asks us to identify active users from a database containing two tables: Accounts and Logins. The Accounts ta
We are given exactly four triangles. Each triangle represents the shape of one spaceship. A landing platform is just a set of points in the plane, called columns. A ship can land if we can choose three columns that form a triangle congruent to the ship.
We are asked to represent a positive integer given in binary as a sum of powers of two, with the option of using negative powers, such that the total number of terms is minimized. Formally, we want to write the number as a sum of expressions of the form +2^x or -2^x.
The problem asks us to find the length of the longest contiguous substring in a given string s such that the substring contains at most k distinct characters. A substring is a continuous portion of the string.
The problem asks us to compute a "beauty" score for each element in an array nums, excluding the first and last elements. Specifically, for each index i in the range 1 <= i <= nums.length - 2, the beauty of nums[i] is determined by two conditions: 1.
We are given an array of integers and a fixed window size k. For each contiguous subarray (segment) of length k, we need to find the largest element that appears exactly once within that segment. If no element appears exactly once, we output "Nothing".
This problem asks us to count the number of valid sequences for performing pickups and deliveries for n orders. Each ord
The problem gives a string of digits where each digit is between 2 and 9. Each digit corresponds to a set of letters on a traditional phone keypad: - 2 - "abc" - 3 - "def" - 4 - "ghi" - 5 - "jkl" - 6 - "mno" - 7 - "pqrs" - 8 - "tuv" - 9 - "wxyz" We must generate every possible…
The problem gives a single integer w, the weight of a watermelon. We need to decide whether it can be split into two positive parts such that both parts are even numbers.
Each item has two properties. If Bob pays for that item, the cashier spends t[i] seconds processing it and Bob also spends c[i] money. During those t[i] seconds, Bob can steal other items, one item per second.
We start with a strictly increasing array of problem complexities. The imbalance of the set is defined as the largest difference between two neighboring elements after sorting.
This problem asks us to count how many root to leaf paths in a binary tree are "pseudo-palindromic". A palindrome is a s
We are given two integers, k and l. The task is to determine whether l can be written as an exact power of k. In other words, we need to check whether there exists a non-negative integer n such that: $l = k^n$ If such an n exists, we print "YES" and also print the importance…
This problem asks us to find the titles of movies that satisfy three separate conditions at the same time. First, the content must be marked as kid-friendly. In the Content table, this is represented by the column Kidscontent = 'Y'. Second, the content must actually be a movie.
The problem asks us to calculate the minimum number of operations to convert one 24-hour time string, current, into anot
We are asked to verify a watering schedule for flowers over a set of consecutive holiday days. Each day must be watered exactly once. The schedule specifies, for each of several people, the range of days they are assigned to water the flowers.
The problem gives us a matrix called picture, where each cell contains either 'B' for a black pixel or 'W' for a white pixel. We are also given an integer target. We need to count how many black pixels qualify as "lonely pixels" under two strict conditions.
The problem asks us to replace words in a sentence using a set of predefined root words. A root is a shorter word that can serve as a prefix for a longer derivative word.
The problem asks whether Vasya, who typed a string of lowercase letters, effectively managed to say "hello". The goal is not to check if the typed string is exactly "hello", but whether we can remove some letters (possibly zero) to produce the sequence h, e, l, l, o in order.
The problem describes a variation of the classic Josephus problem. We have n friends sitting in a circle, numbered 1 through n clockwise. Starting from the first friend, we count k friends clockwise (inclusive of the starting friend).
In this problem, we are given the root node of a binary tree, and we need to compute the average value of all nodes at each depth level of the tree. A binary tree is organized into levels.
We are asked to count the number of ways to line up Caesar’s soldiers, consisting of a given number of footmen and horsemen, so that no more than a fixed number of the same type stand consecutively.
Each student points to exactly one other student, the person they call whenever they hear news. This creates a directed functional graph, every node has out-degree exactly one.
We have a grid of size where each cell is 1 centimeter square. A flea starts at some cell and can jump exactly centimeters either vertically or horizontally, staying inside the board.
The problem gives us a line of washing machines, where each machine contains some number of dresses. In one move, we are allowed to choose any subset of machines, and every chosen machine may pass exactly one dress to one of its adjacent machines simultaneously.
The Spending table records purchases made by users on an e-commerce platform. Every row represents one user's spending on a specific date using either the mobile platform or the desktop platform.
This problem simulates a lemonade stand where each lemonade costs 5, 20 bill. The challenge is to provide exact change to every customer as they pay. The input is a list of integers bills, where each element represents the bill a customer gives.
This problem is asking us to assign a set of jobs, each with a specific time requirement, to k workers such that every job is assigned to exactly one worker, and we minimize the maximum total working time among all workers.
The problem gives us an integer array nums of length n + 1, where every value is guaranteed to be in the range [1, n]. Since there are n + 1 numbers but only n possible distinct values, at least one number must appear more than once. The task is to return that duplicate value.
The problem asks us to design a data structure that supports two operations efficiently: 1. Adding words into the structure. 2. Searching for words, where the search pattern may contain the wildcard character '.'. The wildcard '.' represents any single lowercase English letter.
This problem asks us to count how many distinct non-empty palindromic subsequences exist in a string. A subsequence is formed by deleting characters while preserving the relative order of the remaining characters. Unlike substrings, subsequences do not need to be contiguous.
The problem asks us to count how many pairs of indices (i, j) satisfy two conditions: 1. i < j 2. nums[i] 2 nums[j] These pairs are called reverse pairs.
The task is to take a positive integer represented in binary and determine how many steps it takes to reduce it to 1 using a simple iterative process. In each step, if the number is odd, we increment it by 1, and if it is even, we divide it by 2.
The problem gives us a binary array named nums, where every element is either 0 or 1. Our task is to determine the maximum number of consecutive 1s that appear anywhere in the array.
The problem gives us the head of a singly linked list and asks us to reorder the nodes in a very specific alternating pattern. A normal linked list looks like this: We must transform it into: The important detail is that we are not allowed to change node values.
The problem asks us to design a class that processes stock prices one day at a time and, for every new price, returns the stock span for that day.
This problem revolves around identifying which subtasks of a task were not executed. We are given two tables: Tasks and Executed.
We are asked to count the number of arrays of length n containing integers from 1 to n such that the array is either entirely non-decreasing or entirely non-increasing.