bowang0911/leetcode huggingface dataset:
Defuse the Bomb

You have a bomb to defuse, and your time is running out! Your informer will provide you with a **circular** array `code` of length of `n` and a key `k`.

To decrypt the code, you must replace every number. All the numbers are replaced **simultaneously**.

*   If `k > 0`, replace the `ith` number with the sum of the **next** `k` numbers.
*   If `k < 0`, replace the `ith` number with the sum of the **previous** `k` numbers.
*   If `k == 0`, replace the `ith` number with `0`.

As `code` is circular, the next element of `code[n-1]` is `code[0]`, and the previous element of `code[0]` is `code[n-1]`.

Given the **circular** array `code` and an integer key `k`, return _the decrypted code to defuse the bomb_!

**Example 1:**

**Input:** code = \[5,7,1,4\], k = 3
**Output:** \[12,10,16,13\]
**Explanation:** Each number is replaced by the sum of the next 3 numbers. The decrypted code is \[7+1+4, 1+4+5, 4+5+7, 5+7+1\]. Notice that the numbers wrap around.

**Example 2:**

**Input:** code = \[1,2,3,4\], k = 0
**Output:** \[0,0,0,0\]
**Explanation:** When k is zero, the numbers are replaced by 0. 

**Example 3:**

**Input:** code = \[2,4,9,3\], k = -2
**Output:** \[12,5,6,13\]
**Explanation:** The decrypted code is \[3+9, 2+3, 4+2, 9+4\]. Notice that the numbers wrap around again. If k is negative, the sum is of the **previous** numbers.

**Constraints:**

*   `n == code.length`
*   `1 <= n <= 100`
*   `1 <= code[i] <= 100`
*   `-(n - 1) <= k <= n - 1`
Minimum Deletions to Make String Balanced

You are given a string `s` consisting only of characters `'a'` and `'b'`​​​​.

You can delete any number of characters in `s` to make `s` **balanced**. `s` is **balanced** if there is no pair of indices `(i,j)` such that `i < j` and `s[i] = 'b'` and `s[j]= 'a'`.

Return _the **minimum** number of deletions needed to make_ `s` _**balanced**_.

**Example 1:**

**Input:** s =  "aababbab "
**Output:** 2
**Explanation:** You can either:
Delete the characters at 0-indexed positions 2 and 6 ( "aababbab " ->  "aaabbb "), or
Delete the characters at 0-indexed positions 3 and 6 ( "aababbab " ->  "aabbbb ").

**Example 2:**

**Input:** s =  "bbaaaaabb "
**Output:** 2
**Explanation:** The only solution is to delete the first two characters.

**Constraints:**

*   `1 <= s.length <= 105`
*   `s[i]` is `'a'` or `'b'`​​.
Minimum Jumps to Reach Home

A certain bug's home is on the x-axis at position `x`. Help them get there from position `0`.

The bug jumps according to the following rules:

*   It can jump exactly `a` positions **forward** (to the right).
*   It can jump exactly `b` positions **backward** (to the left).
*   It cannot jump backward twice in a row.
*   It cannot jump to any `forbidden` positions.

The bug may jump forward **beyond** its home, but it **cannot jump** to positions numbered with **negative** integers.

Given an array of integers `forbidden`, where `forbidden[i]` means that the bug cannot jump to the position `forbidden[i]`, and integers `a`, `b`, and `x`, return _the minimum number of jumps needed for the bug to reach its home_. If there is no possible sequence of jumps that lands the bug on position `x`, return `-1.`

**Example 1:**

**Input:** forbidden = \[14,4,18,1,15\], a = 3, b = 15, x = 9
**Output:** 3
**Explanation:** 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.

**Example 2:**

**Input:** forbidden = \[8,3,16,6,12,20\], a = 15, b = 13, x = 11
**Output:** -1

**Example 3:**

**Input:** forbidden = \[1,6,2,14,5,17,4\], a = 16, b = 9, x = 7
**Output:** 2
**Explanation:** One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.

**Constraints:**

*   `1 <= forbidden.length <= 1000`
*   `1 <= a, b, forbidden[i] <= 2000`
*   `0 <= x <= 2000`
*   All the elements in `forbidden` are distinct.
*   Position `x` is not forbidden.
Distribute Repeating Integers

You are given an array of `n` integers, `nums`, where there are at most `50` unique values in the array. You are also given an array of `m` customer order quantities, `quantity`, where `quantity[i]` is the amount of integers the `ith` customer ordered. Determine if it is possible to distribute `nums` such that:

*   The `ith` customer gets **exactly** `quantity[i]` integers,
*   The integers the `ith` customer gets are **all equal**, and
*   Every customer is satisfied.

Return `true` _if it is possible to distribute_ `nums` _according to the above conditions_.

**Example 1:**

**Input:** nums = \[1,2,3,4\], quantity = \[2\]
**Output:** false
**Explanation:** The 0th customer cannot be given two different integers.

**Example 2:**

**Input:** nums = \[1,2,3,3\], quantity = \[2\]
**Output:** true
**Explanation:** The 0th customer is given \[3,3\]. The integers \[1,2\] are not used.

**Example 3:**

**Input:** nums = \[1,1,2,2\], quantity = \[2,2\]
**Output:** true
**Explanation:** The 0th customer is given \[1,1\], and the 1st customer is given \[2,2\].

**Constraints:**

*   `n == nums.length`
*   `1 <= n <= 105`
*   `1 <= nums[i] <= 1000`
*   `m == quantity.length`
*   `1 <= m <= 10`
*   `1 <= quantity[i] <= 105`
*   There are at most `50` unique values in `nums`.
Design an Ordered Stream

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 of their IDs** by returning a **chunk** (list) of values after each insertion. The concatenation of all the **chunks** should result in a list of the sorted values.

Implement the `OrderedStream` class:

*   `OrderedStream(int n)` Constructs the stream to take `n` values.
*   `String[] insert(int idKey, String value)` Inserts the pair `(idKey, value)` into the stream, then returns the **largest possible chunk** of currently inserted values that appear next in the order.

**Example:**

**Input**
\[ "OrderedStream ",  "insert ",  "insert ",  "insert ",  "insert ",  "insert "\]
\[\[5\], \[3,  "ccccc "\], \[1,  "aaaaa "\], \[2,  "bbbbb "\], \[5,  "eeeee "\], \[4,  "ddddd "\]\]
**Output**
\[null, \[\], \[ "aaaaa "\], \[ "bbbbb ",  "ccccc "\], \[\], \[ "ddddd ",  "eeeee "\]\]

**Explanation**
// Note that the values ordered by ID is \[ "aaaaa ",  "bbbbb ",  "ccccc ",  "ddddd ",  "eeeee "\].
OrderedStream os = new OrderedStream(5);
os.insert(3,  "ccccc "); // Inserts (3,  "ccccc "), returns \[\].
os.insert(1,  "aaaaa "); // Inserts (1,  "aaaaa "), returns \[ "aaaaa "\].
os.insert(2,  "bbbbb "); // Inserts (2,  "bbbbb "), returns \[ "bbbbb ",  "ccccc "\].
os.insert(5,  "eeeee "); // Inserts (5,  "eeeee "), returns \[\].
os.insert(4,  "ddddd "); // Inserts (4,  "ddddd "), returns \[ "ddddd ",  "eeeee "\].
// Concatentating all the chunks returned:
// \[\] + \[ "aaaaa "\] + \[ "bbbbb ",  "ccccc "\] + \[\] + \[ "ddddd ",  "eeeee "\] = \[ "aaaaa ",  "bbbbb ",  "ccccc ",  "ddddd ",  "eeeee "\]
// The resulting order is the same as the order above.

**Constraints:**

*   `1 <= n <= 1000`
*   `1 <= id <= n`
*   `value.length == 5`
*   `value` consists only of lowercase letters.
*   Each call to `insert` will have a unique `id.`
*   Exactly `n` calls will be made to `insert`.
Determine if Two Strings Are Close

Two strings are considered **close** if you can attain one from the other using the following operations:

*   Operation 1: Swap any two **existing** characters.
    *   For example, `abcde -> aecdb`
*   Operation 2: Transform **every** occurrence of one **existing** character into another **existing** character, and do the same with the other character.
    *   For example, `aacabb -> bbcbaa` (all `a`'s turn into `b`'s, and all `b`'s turn into `a`'s)

You can use the operations on either string as many times as necessary.

Given two strings, `word1` and `word2`, return `true` _if_ `word1` _and_ `word2` _are **close**, and_ `false` _otherwise._

**Example 1:**

**Input:** word1 =  "abc ", word2 =  "bca "
**Output:** true
**Explanation:** You can attain word2 from word1 in 2 operations.
Apply Operation 1:  "abc " ->  "acb "
Apply Operation 1:  "acb " ->  "bca "

**Example 2:**

**Input:** word1 =  "a ", word2 =  "aa "
**Output:** false
**Explanation:** It is impossible to attain word2 from word1, or vice versa, in any number of operations.

**Example 3:**

**Input:** word1 =  "cabbba ", word2 =  "abbccc "
**Output:** true
**Explanation:** You can attain word2 from word1 in 3 operations.
Apply Operation 1:  "cabbba " ->  "caabbb "
`Apply Operation 2:  "`caabbb " ->  "baaccc "
Apply Operation 2:  "baaccc " ->  "abbccc "

**Constraints:**

*   `1 <= word1.length, word2.length <= 105`
*   `word1` and `word2` contain only lowercase English letters.
Minimum Operations to Reduce X to Zero

You are given an integer array `nums` and an integer `x`. In one operation, you can either remove the leftmost or the rightmost element from the array `nums` and subtract its value from `x`. Note that this **modifies** the array for future operations.

Return _the **minimum number** of operations to reduce_ `x` _to **exactly**_ `0` _if it is possible__, otherwise, return_ `-1`.

**Example 1:**

**Input:** nums = \[1,1,4,2,3\], x = 5
**Output:** 2
**Explanation:** The optimal solution is to remove the last two elements to reduce x to zero.

**Example 2:**

**Input:** nums = \[5,6,7,8,9\], x = 4
**Output:** -1

**Example 3:**

**Input:** nums = \[3,2,20,1,1,3\], x = 10
**Output:** 5
**Explanation:** The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 104`
*   `1 <= x <= 109`
Maximize Grid Happiness

You are given four integers, `m`, `n`, `introvertsCount`, and `extrovertsCount`. You have an `m x n` grid, and there are two types of people: introverts and extroverts. There are `introvertsCount` introverts and `extrovertsCount` extroverts.

You should decide how many people you want to live in the grid and assign each of them one grid cell. Note that you **do not** have to have all the people living in the grid.

The **happiness** of each person is calculated as follows:

*   Introverts **start** with `120` happiness and **lose** `30` happiness for each neighbor (introvert or extrovert).
*   Extroverts **start** with `40` happiness and **gain** `20` happiness for each neighbor (introvert or extrovert).

Neighbors live in the directly adjacent cells north, east, south, and west of a person's cell.

The **grid happiness** is the **sum** of each person's happiness. Return _the **maximum possible grid happiness**._

**Example 1:**

**Input:** m = 2, n = 3, introvertsCount = 1, extrovertsCount = 2
**Output:** 240
**Explanation:** Assume the grid is 1-indexed with coordinates (row, column).
We can put the introvert in cell (1,1) and put the extroverts in cells (1,3) and (2,3).
- Introvert at (1,1) happiness: 120 (starting happiness) - (0 \* 30) (0 neighbors) = 120
- Extrovert at (1,3) happiness: 40 (starting happiness) + (1 \* 20) (1 neighbor) = 60
- Extrovert at (2,3) happiness: 40 (starting happiness) + (1 \* 20) (1 neighbor) = 60
The grid happiness is 120 + 60 + 60 = 240.
The above figure shows the grid in this example with each person's happiness. The introvert stays in the light green cell while the extroverts live on the light purple cells.

**Example 2:**

**Input:** m = 3, n = 1, introvertsCount = 2, extrovertsCount = 1
**Output:** 260
**Explanation:** Place the two introverts in (1,1) and (3,1) and the extrovert at (2,1).
- Introvert at (1,1) happiness: 120 (starting happiness) - (1 \* 30) (1 neighbor) = 90
- Extrovert at (2,1) happiness: 40 (starting happiness) + (2 \* 20) (2 neighbors) = 80
- Introvert at (3,1) happiness: 120 (starting happiness) - (1 \* 30) (1 neighbor) = 90
The grid happiness is 90 + 80 + 90 = 260.

**Example 3:**

**Input:** m = 2, n = 2, introvertsCount = 4, extrovertsCount = 0
**Output:** 240

**Constraints:**

*   `1 <= m, n <= 5`
*   `0 <= introvertsCount, extrovertsCount <= min(m * n, 6)`
Correct a Binary Tree

You have a binary tree with a small defect. There is **exactly one** invalid node where its right child incorrectly points to another node at the **same depth** but to the **invalid node's right**.

Given the root of the binary tree with this defect, `root`, return _the root of the binary tree after **removing** this invalid node **and every node underneath it** (minus the node it incorrectly points to)._

**Custom testing:**

The test input is read as 3 lines:

*   `TreeNode root`
*   `int fromNode` (**not available to** `correctBinaryTree`)
*   `int toNode` (**not available to** `correctBinaryTree`)

After the binary tree rooted at `root` is parsed, the `TreeNode` with value of `fromNode` will have its right child pointer pointing to the `TreeNode` with a value of `toNode`. Then, `root` is passed to `correctBinaryTree`.

**Example 1:**

**Input:** root = \[1,2,3\], fromNode = 2, toNode = 3
**Output:** \[1,null,3\]
**Explanation:** The node with value 2 is invalid, so remove it.

**Example 2:**

**Input:** root = \[8,3,1,7,null,9,4,2,null,null,null,5,6\], fromNode = 7, toNode = 4
**Output:** \[8,3,1,null,null,9,4,null,null,5,6\]
**Explanation:** The node with value 7 is invalid, so remove it and the node underneath it, node 2.

**Constraints:**

*   The number of nodes in the tree is in the range `[3, 104]`.
*   `-109 <= Node.val <= 109`
*   All `Node.val` are **unique**.
*   `fromNode != toNode`
*   `fromNode` and `toNode` will exist in the tree and will be on the same depth.
*   `toNode` is to the **right** of `fromNode`.
*   `fromNode.right` is `null` in the initial tree from the test data.
Check If Two String Arrays are Equivalent

Given two string arrays `word1` and `word2`, return `true` _if the two arrays **represent** the same string, and_ `false` _otherwise._

A string is **represented** by an array if the array elements concatenated **in order** forms the string.

**Example 1:**

**Input:** word1 = \[ "ab ",  "c "\], word2 = \[ "a ",  "bc "\]
**Output:** true
**Explanation:**
word1 represents string  "ab " +  "c " ->  "abc "
word2 represents string  "a " +  "bc " ->  "abc "
The strings are the same, so return true.

**Example 2:**

**Input:** word1 = \[ "a ",  "cb "\], word2 = \[ "ab ",  "c "\]
**Output:** false

**Example 3:**

**Input:** word1  = \[ "abc ",  "d ",  "defg "\], word2 = \[ "abcddefg "\]
**Output:** true

**Constraints:**

*   `1 <= word1.length, word2.length <= 103`
*   `1 <= word1[i].length, word2[i].length <= 103`
*   `1 <= sum(word1[i].length), sum(word2[i].length) <= 103`
*   `word1[i]` and `word2[i]` consist of lowercase letters.
Smallest String With A Given Numeric Value

The **numeric value** of a **lowercase character** is defined as its position `(1-indexed)` in the alphabet, so the numeric value of `a` is `1`, the numeric value of `b` is `2`, the numeric value of `c` is `3`, and so on.

The **numeric value** of a **string** consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string `"abe "` is equal to `1 + 2 + 5 = 8`.

You are given two integers `n` and `k`. Return _the **lexicographically smallest string** with **length** equal to `n` and **numeric value** equal to `k`._

Note that a string `x` is lexicographically smaller than string `y` if `x` comes before `y` in dictionary order, that is, either `x` is a prefix of `y`, or if `i` is the first position such that `x[i] != y[i]`, then `x[i]` comes before `y[i]` in alphabetic order.

**Example 1:**

**Input:** n = 3, k = 27
**Output:**  "aay "
**Explanation:** The numeric value of the string is 1 + 1 + 25 = 27, and it is the smallest string with such a value and length equal to 3.

**Example 2:**

**Input:** n = 5, k = 73
**Output:**  "aaszz "

**Constraints:**

*   `1 <= n <= 105`
*   `n <= k <= 26 * n`
Ways to Make a Fair Array

You are given an integer array `nums`. You can choose **exactly one** index (**0-indexed**) and remove the element. Notice that the index of the elements may change after the removal.

For example, if `nums = [6,1,7,4,1]`:

*   Choosing to remove index `1` results in `nums = [6,7,4,1]`.
*   Choosing to remove index `2` results in `nums = [6,1,4,1]`.
*   Choosing to remove index `4` results in `nums = [6,1,7,4]`.

An array is **fair** if the sum of the odd-indexed values equals the sum of the even-indexed values.

Return the _**number** of indices that you could choose such that after the removal,_ `nums` _is **fair**._

**Example 1:**

**Input:** nums = \[2,1,6,4\]
**Output:** 1
**Explanation:**
Remove index 0: \[1,6,4\] -> Even sum: 1 + 4 = 5. Odd sum: 6. Not fair.
Remove index 1: \[2,6,4\] -> Even sum: 2 + 4 = 6. Odd sum: 6. Fair.
Remove index 2: \[2,1,4\] -> Even sum: 2 + 4 = 6. Odd sum: 1. Not fair.
Remove index 3: \[2,1,6\] -> Even sum: 2 + 6 = 8. Odd sum: 1. Not fair.
There is 1 index that you can remove to make nums fair.

**Example 2:**

**Input:** nums = \[1,1,1\]
**Output:** 3
**Explanation:** You can remove any index and the remaining array is fair.

**Example 3:**

**Input:** nums = \[1,2,3\]
**Output:** 0
**Explanation:** You cannot make a fair array after removing any index.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 104`
Minimum Initial Energy to Finish Tasks

You are given an array `tasks` where `tasks[i] = [actuali, minimumi]`:

*   `actuali` is the actual amount of energy you **spend to finish** the `ith` task.
*   `minimumi` is the minimum amount of energy you **require to begin** the `ith` task.

For example, if the task is `[10, 12]` and your current energy is `11`, you cannot start this task. However, if your current energy is `13`, you can complete this task, and your energy will be `3` after finishing it.

You can finish the tasks in **any order** you like.

Return _the **minimum** initial amount of energy you will need_ _to finish all the tasks_.

**Example 1:**

**Input:** tasks = \[\[1,2\],\[2,4\],\[4,8\]\]
**Output:** 8
**Explanation:**
Starting with 8 energy, we finish the tasks in the following order:
    - 3rd task. Now energy = 8 - 4 = 4.
    - 2nd task. Now energy = 4 - 2 = 2.
    - 1st task. Now energy = 2 - 1 = 1.
Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.

**Example 2:**

**Input:** tasks = \[\[1,3\],\[2,4\],\[10,11\],\[10,12\],\[8,9\]\]
**Output:** 32
**Explanation:**
Starting with 32 energy, we finish the tasks in the following order:
    - 1st task. Now energy = 32 - 1 = 31.
    - 2nd task. Now energy = 31 - 2 = 29.
    - 3rd task. Now energy = 29 - 10 = 19.
    - 4th task. Now energy = 19 - 10 = 9.
    - 5th task. Now energy = 9 - 8 = 1.

**Example 3:**

**Input:** tasks = \[\[1,7\],\[2,8\],\[3,9\],\[4,10\],\[5,11\],\[6,12\]\]
**Output:** 27
**Explanation:**
Starting with 27 energy, we finish the tasks in the following order:
    - 5th task. Now energy = 27 - 5 = 22.
    - 2nd task. Now energy = 22 - 2 = 20.
    - 3rd task. Now energy = 20 - 3 = 17.
    - 1st task. Now energy = 17 - 1 = 16.
    - 4th task. Now energy = 16 - 4 = 12.
    - 6th task. Now energy = 12 - 6 = 6.

**Constraints:**

*   `1 <= tasks.length <= 105`
*   `1 <= actual​i <= minimumi <= 104`
Change the Root of a Binary Tree

Given the `root` of a binary tree and a `leaf` node, reroot the tree so that the `leaf` is the new root.

You can reroot the tree with the following steps for each node `cur` on the path **starting from the** `leaf` up to the `root`​​​ **excluding the root**:

1.  If `cur` has a left child, then that child becomes `cur`'s right child.
2.  `cur`'s original parent becomes `cur`'s left child. Note that in this process the original parent's pointer to `cur` becomes `null`, making it have at most one child.

Return _the new root_ _of the rerooted tree._

**Note:** Ensure that your solution sets the `Node.parent` pointers correctly after rerooting or you will receive "Wrong Answer ".

**Example 1:**

**Input:** root = \[3,5,1,6,2,0,8,null,null,7,4\], leaf = 7
**Output:** \[7,2,null,5,4,3,6,null,null,null,1,null,null,0,8\]

**Example 2:**

**Input:** root = \[3,5,1,6,2,0,8,null,null,7,4\], leaf = 0
**Output:** \[0,1,null,3,8,5,null,null,null,6,2,null,null,7,4\]

**Constraints:**

*   The number of nodes in the tree is in the range `[2, 100]`.
*   `-109 <= Node.val <= 109`
*   All `Node.val` are **unique**.
*   `leaf` exist in the tree.
Maximum Repeating Substring

For a string `sequence`, a string `word` is **`k`\-repeating** if `word` concatenated `k` times is a substring of `sequence`. The `word`'s **maximum `k`\-repeating value** is the highest value `k` where `word` is `k`\-repeating in `sequence`. If `word` is not a substring of `sequence`, `word`'s maximum `k`\-repeating value is `0`.

Given strings `sequence` and `word`, return _the **maximum `k`\-repeating value** of `word` in `sequence`_.

**Example 1:**

**Input:** sequence =  "ababc ", word =  "ab "
**Output:** 2
**Explanation: ** "abab " is a substring in  "ababc ".

**Example 2:**

**Input:** sequence =  "ababc ", word =  "ba "
**Output:** 1
**Explanation: ** "ba " is a substring in  "ababc ".  "baba " is not a substring in  "ababc ".

**Example 3:**

**Input:** sequence =  "ababc ", word =  "ac "
**Output:** 0
**Explanation: ** "ac " is not a substring in  "ababc ". 

**Constraints:**

*   `1 <= sequence.length <= 100`
*   `1 <= word.length <= 100`
*   `sequence` and `word` contains only lowercase English letters.
Merge In Between Linked Lists

You are given two linked lists: `list1` and `list2` of sizes `n` and `m` respectively.

Remove `list1`'s nodes from the `ath` node to the `bth` node, and put `list2` in their place.

The blue edges and nodes in the following figure indicate the result:

_Build the result list and return its head._

**Example 1:**

**Input:** list1 = \[0,1,2,3,4,5\], a = 3, b = 4, list2 = \[1000000,1000001,1000002\]
**Output:** \[0,1,2,1000000,1000001,1000002,5\]
**Explanation:** We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.

**Example 2:**

**Input:** list1 = \[0,1,2,3,4,5,6\], a = 2, b = 5, list2 = \[1000000,1000001,1000002,1000003,1000004\]
**Output:** \[0,1,1000000,1000001,1000002,1000003,1000004,6\]
**Explanation:** The blue edges and nodes in the above figure indicate the result.

**Constraints:**

*   `3 <= list1.length <= 104`
*   `1 <= a <= b < list1.length - 1`
*   `1 <= list2.length <= 104`