<Code author="Calf" />
1092. Shortest Common Supersequence
#Problem Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them. A string s is a subsequence of string t if deleting some number of …
2353. Design a Food Rating System
#Problem Design a food rating system that can do the following: Modify the rating of a food item listed in the system. Return the highest-rated food item for a type of cuisine in the system. Implement the FoodRatings class: …
873. Length of Longest Fibonacci Subsequence
#Problem A sequence x₁, x₂, ..., xₙ is Fibonacci-like if: n >= 3 xᵢ + xᵢ₊₁ == xᵢ₊₂ for all i + 2 <= n Given a strictly increasing array arr of positive integers forming a sequence, return the length of the longest Fibonacci-like subsequence …
2296. Design a Text Editor
#Problem Design a text editor with a cursor that can do the following: Add text to where the cursor is. Delete text from where the cursor is (simulating the backspace key). Move the cursor either left or right. When deleting text, only …
1524. Number of Sub-arrays With Odd Sum
#Problem Given an array of integers arr, return the number of subarrays with an odd sum. Since the answer can be very large, return it modulo 10⁹ + 7. https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/ Example 1: Input: arr = …
1749. Maximum Absolute Sum of Any Subarray
#Problem You are given an integer array nums. The absolute sum of a subarray [numsₗ, numsₗ₊₁, ..., numsᵣ₋₁, numsᵣ] is abs(numsₗ + numsₗ₊₁ + ... + numsᵣ₋₁ + numsᵣ). Return the maximum absolute sum of any (possibly empty) subarray of nums. …
1472. Design Browser History
#Problem You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: …
2502. Design Memory Allocator
#Problem You are given an integer n representing the size of a 0-indexed memory array. All memory units are initially free. You have a memory allocator with the following functionalities: Allocate a block of size consecutive free memory …
2467. Most Profitable Path in a Tree
#Problem There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [aᵢ, bᵢ] indicates that there is an edge between nodes aᵢ and bᵢ in the …
1656. Design an Ordered Stream
#Problem There is a stream of n (idKey, value) pairs arriving in an arbitrary order, where idKey is an integer between 1 and n and value is a string. No two pairs have the same id. Design a stream that returns the values in increasing order …
889. Construct Binary Tree from Preorder and Postorder Traversal
#Problem Given two integer arrays, preorder and postorder where preorder is the preorder traversal of a binary tree of distinct values and postorder is the postorder traversal of the same tree, reconstruct and return the binary tree. If …
1028. Recover a Tree From Preorder Traversal
#Problem We run a preorder depth-first search (DFS) on the root of a binary tree. At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node. If the depth of a node is D, …
2506. Count Pairs Of Similar Strings
#Problem You are given a 0-indexed string array words. Two strings are similar if they consist of the same characters. For example, "abca" and "cba" are similar since both consist of characters 'a', 'b', and 'c'. However, "abacba" and …
1261. Find Elements in a Contaminated Binary Tree
#Problem Given a binary tree with the following rules: root.val == 0 For any treeNode: If treeNode.val has a value x and treeNode.left != null, then treeNode.left.val == 2 * x + 1 If treeNode.val has a value x and treeNode.right != null, …
2209. Minimum White Tiles After Covering With Carpets
#Problem You are given a 0-indexed binary string floor, which represents the colors of tiles on a floor: floor[i] = '0' denotes that the iᵗʰ tile of the floor is colored black. On the other hand, floor[i] = '1' denotes that the iᵗʰ tile of …
1980. Find Unique Binary String
#Problem Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them. …
2595. Number of Even and Odd Bits
#Problem You are given a positive integer n. Let even denote the number of even indices in the binary representation of n with value 1. Let odd denote the number of odd indices in the binary representation of n with value 1. Note that bits …
2944. Minimum Number of Coins for Fruits
#Problem You are given an 0-indexed integer array prices where prices[i] denotes the number of coins needed to purchase the (i + 1)ᵗʰ fruit. The fruit market has the following reward for each fruit: If you purchase the (i + 1)ᵗʰ fruit at …
1415. The k-th Lexicographical String of All Happy Strings of Length n
#Problem A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings “abc”, “ac”, “b” and “abcbabcbcb” are all …
624. Maximum Distance in Arrays
#Problem You are given m arrays, where each array is sorted in ascending order. You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to …
2375. Construct Smallest Number From DI String
#Problem You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing. A 0-indexed string num of length n + 1 is created using the following conditions: num consists of …
2080. Range Frequency Queries
#Problem Design a data structure to find the frequency of a given value in a given subarray. The frequency of a value in a subarray is the number of occurrences of that value in the subarray. Implement the RangeFreqQuery class: …
1079. Letter Tile Possibilities
#Problem You have n tiles, where each tile has one letter tiles[i] printed on it. Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles. …
1287. Element Appearing More Than 25% In Sorted Array
#Problem Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer. …
1718. Construct the Lexicographically Largest Valid Sequence
#Problem Given an integer n, find a sequence that satisfies all of the following: The integer 1 occurs once in the sequence. Each integer between 2 and n occurs twice in the sequence. For every integer i between 2 and n, the distance …
1299. Replace Elements with Greatest Element on Right Side
#Problem Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. After doing so, return the array. …
2698. Find the Punishment Number of an Integer
#Problem Given a positive integer n, return the punishment number of n. The punishment number of n is defined as the sum of the squares of all integers i such that: 1 <= i <= n The decimal representation of i * i can be partitioned into …
1706. Where Will the Ball Fall
#Problem You have a 2-D grid of size m x n representing a box, and you have n balls. The box is open on the top and bottom sides. Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the …
1352. Product of the Last K Numbers
#Problem Design an algorithm that accepts a stream of integers and retrieves the product of the last k integers of the stream. Implement the ProductOfNumbers class: ProductOfNumbers() Initializes the object with an empty stream. void …
1552. Magnetic Force Between Two Balls
#Problem In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the iᵗʰ basket is at position[i], Morty has m balls and needs to …
1742. Maximum Number of Balls in a Box
#Problem You are working in a ball factory where you have n balls numbered from lowLimit up to highLimit inclusive (i.e., n == highLimit - lowLimit + 1), and an infinite number of boxes numbered from 1 to infinity. Your job at this factory …
2342. Max Sum of a Pair With Equal Sum of Digits
#Problem You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j]. Return the maximum value of …