bowang0911/leetcode huggingface dataset:
Divide Intervals Into Minimum Number of Groups

You are given a 2D integer array `intervals` where `intervals[i] = [lefti, righti]` represents the **inclusive** interval `[lefti, righti]`.

You have to divide the intervals into one or more **groups** such that each interval is in **exactly** one group, and no two intervals that are in the same group **intersect** each other.

Return _the **minimum** number of groups you need to make_.

Two intervals **intersect** if there is at least one common number between them. For example, the intervals `[1, 5]` and `[5, 8]` intersect.

**Example 1:**

**Input:** intervals = \[\[5,10\],\[6,8\],\[1,5\],\[2,3\],\[1,10\]\]
**Output:** 3
**Explanation:** We can divide the intervals into the following groups:
- Group 1: \[1, 5\], \[6, 8\].
- Group 2: \[2, 3\], \[5, 10\].
- Group 3: \[1, 10\].
It can be proven that it is not possible to divide the intervals into fewer than 3 groups.

**Example 2:**

**Input:** intervals = \[\[1,3\],\[5,6\],\[8,10\],\[11,13\]\]
**Output:** 1
**Explanation:** None of the intervals overlap, so we can put all of them in one group.

**Constraints:**

*   `1 <= intervals.length <= 105`
*   `intervals[i].length == 2`
*   `1 <= lefti <= righti <= 106`
Longest Increasing Subsequence II

You are given an integer array `nums` and an integer `k`.

Find the longest subsequence of `nums` that meets the following requirements:

*   The subsequence is **strictly increasing** and
*   The difference between adjacent elements in the subsequence is **at most** `k`.

Return _the length of the **longest** **subsequence** that meets the requirements._

A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

**Example 1:**

**Input:** nums = \[4,2,1,4,3,4,5,8,15\], k = 3
**Output:** 5
**Explanation:**
The longest subsequence that meets the requirements is \[1,3,4,5,8\].
The subsequence has a length of 5, so we return 5.
Note that the subsequence \[1,3,4,5,8,15\] does not meet the requirements because 15 - 8 = 7 is larger than 3.

**Example 2:**

**Input:** nums = \[7,4,5,1,8,12,4,7\], k = 5
**Output:** 4
**Explanation:**
The longest subsequence that meets the requirements is \[4,5,8,12\].
The subsequence has a length of 4, so we return 4.

**Example 3:**

**Input:** nums = \[1,5\], k = 1
**Output:** 1
**Explanation:**
The longest subsequence that meets the requirements is \[1\].
The subsequence has a length of 1, so we return 1.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i], k <= 105`
Design SQL

You are given `n` tables represented with two arrays `names` and `columns`, where `names[i]` is the name of the `ith` table and `columns[i]` is the number of columns of the `ith` table.

You should be able to perform the following **operations**:

*   **Insert** a row in a specific table. Each row you insert has an id. The id is assigned using an auto-increment method where the id of the first inserted row is 1, and the id of each other row inserted into the same table is the id of the last inserted row (even if it was deleted) plus one.
*   **Delete** a row from a specific table. **Note** that deleting a row does not affect the id of the next inserted row.
*   **Select** a specific cell from any table and return its value.

Implement the `SQL` class:

*   `SQL(String[] names, int[] columns)` Creates the `n` tables.
*   `void insertRow(String name, String[] row)` Adds a row to the table `name`. It is **guaranteed** that the table will exist, and the size of the array `row` is equal to the number of columns in the table.
*   `void deleteRow(String name, int rowId)` Removes the row `rowId` from the table `name`. It is **guaranteed** that the table and row will **exist**.
*   `String selectCell(String name, int rowId, int columnId)` Returns the value of the cell in the row `rowId` and the column `columnId` from the table `name`.

**Example 1:**

**Input**
\[ "SQL ",  "insertRow ",  "selectCell ",  "insertRow ",  "deleteRow ",  "selectCell "\]
\[\[\[ "one ",  "two ",  "three "\], \[2, 3, 1\]\], \[ "two ", \[ "first ",  "second ",  "third "\]\], \[ "two ", 1, 3\], \[ "two ", \[ "fourth ",  "fifth ",  "sixth "\]\], \[ "two ", 1\], \[ "two ", 2, 2\]\]
**Output**
\[null, null,  "third ", null, null,  "fifth "\]

**Explanation**
SQL sql = new SQL(\[ "one ",  "two ",  "three "\], \[2, 3, 1\]); // creates three tables.
sql.insertRow( "two ", \[ "first ",  "second ",  "third "\]); // adds a row to the table  "two ". Its id is 1.
sql.selectCell( "two ", 1, 3); // return  "third ", finds the value of the third column in the row with id 1 of the table  "two ".
sql.insertRow( "two ", \[ "fourth ",  "fifth ",  "sixth "\]); // adds another row to the table  "two ". Its id is 2.
sql.deleteRow( "two ", 1); // deletes the first row of the table  "two ". Note that the second row will still have the id 2.
sql.selectCell( "two ", 2, 2); // return  "fifth ", finds the value of the second column in the row with id 2 of the table  "two ".

**Constraints:**

*   `n == names.length == columns.length`
*   `1 <= n <= 104`
*   `1 <= names[i].length, row[i].length, name.length <= 20`
*   `names[i]`, `row[i]`, and `name` consist of lowercase English letters.
*   `1 <= columns[i] <= 100`
*   All the strings of `names` are **distinct**.
*   `name` exists in the array `names`.
*   `row.length` equals the number of columns in the chosen table.
*   `rowId` and `columnId` will be valid.
*   At most `250` calls will be made to `insertRow` and `deleteRow`.
*   At most `104` calls will be made to `selectCell`.
Count Days Spent Together

Alice and Bob are traveling to Rome for separate business meetings.

You are given 4 strings `arriveAlice`, `leaveAlice`, `arriveBob`, and `leaveBob`. Alice will be in the city from the dates `arriveAlice` to `leaveAlice` (**inclusive**), while Bob will be in the city from the dates `arriveBob` to `leaveBob` (**inclusive**). Each will be a 5-character string in the format `"MM-DD "`, corresponding to the month and day of the date.

Return _the total number of days that Alice and Bob are in Rome together._

You can assume that all dates occur in the **same** calendar year, which is **not** a leap year. Note that the number of days per month can be represented as: `[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]`.

**Example 1:**

**Input:** arriveAlice =  "08-15 ", leaveAlice =  "08-18 ", arriveBob =  "08-16 ", leaveBob =  "08-19 "
**Output:** 3
**Explanation:** Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.

**Example 2:**

**Input:** arriveAlice =  "10-01 ", leaveAlice =  "10-31 ", arriveBob =  "11-01 ", leaveBob =  "12-31 "
**Output:** 0
**Explanation:** There is no day when Alice and Bob are in Rome together, so we return 0.

**Constraints:**

*   All dates are provided in the format `"MM-DD "`.
*   Alice and Bob's arrival dates are **earlier than or equal to** their leaving dates.
*   The given dates are valid dates of a **non-leap** year.
Maximum Matching of Players With Trainers

You are given a **0-indexed** integer array `players`, where `players[i]` represents the **ability** of the `ith` player. You are also given a **0-indexed** integer array `trainers`, where `trainers[j]` represents the **training capacity** of the `jth` trainer.

The `ith` player can **match** with the `jth` trainer if the player's ability is **less than or equal to** the trainer's training capacity. Additionally, the `ith` player can be matched with at most one trainer, and the `jth` trainer can be matched with at most one player.

Return _the **maximum** number of matchings between_ `players` _and_ `trainers` _that satisfy these conditions._

**Example 1:**

**Input:** players = \[4,7,9\], trainers = \[8,2,5,8\]
**Output:** 2
**Explanation:**
One of the ways we can form two matchings is as follows:
- players\[0\] can be matched with trainers\[0\] since 4 <= 8.
- players\[1\] can be matched with trainers\[3\] since 7 <= 8.
It can be proven that 2 is the maximum number of matchings that can be formed.

**Example 2:**

**Input:** players = \[1,1,1\], trainers = \[10\]
**Output:** 1
**Explanation:**
The trainer can be matched with any of the 3 players.
Each player can only be matched with one trainer, so the maximum answer is 1.

**Constraints:**

*   `1 <= players.length, trainers.length <= 105`
*   `1 <= players[i], trainers[j] <= 109`
Smallest Subarrays With Maximum Bitwise OR

You are given a **0-indexed** array `nums` of length `n`, consisting of non-negative integers. For each index `i` from `0` to `n - 1`, you must determine the size of the **minimum sized** non-empty subarray of `nums` starting at `i` (**inclusive**) that has the **maximum** possible **bitwise OR**.

*   In other words, let `Bij` be the bitwise OR of the subarray `nums[i...j]`. You need to find the smallest subarray starting at `i`, such that bitwise OR of this subarray is equal to `max(Bik)` where `i <= k <= n - 1`.

The bitwise OR of an array is the bitwise OR of all the numbers in it.

Return _an integer array_ `answer` _of size_ `n` _where_ `answer[i]` _is the length of the **minimum** sized subarray starting at_ `i` _with **maximum** bitwise OR._

A **subarray** is a contiguous non-empty sequence of elements within an array.

**Example 1:**

**Input:** nums = \[1,0,2,1,3\]
**Output:** \[3,3,2,2,1\]
**Explanation:**
The maximum possible bitwise OR starting at any index is 3. 
- Starting at index 0, the shortest subarray that yields it is \[1,0,2\].
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is \[0,2,1\].
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is \[2,1\].
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is \[1,3\].
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is \[3\].
Therefore, we return \[3,3,2,2,1\]. 

**Example 2:**

**Input:** nums = \[1,2\]
**Output:** \[2,1\]
**Explanation:**
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
Therefore, we return \[2,1\].

**Constraints:**

*   `n == nums.length`
*   `1 <= n <= 105`
*   `0 <= nums[i] <= 109`
Minimum Money Required Before Transactions

You are given a **0-indexed** 2D integer array `transactions`, where `transactions[i] = [costi, cashbacki]`.

The array describes transactions, where each transaction must be completed exactly once in **some order**. At any given moment, you have a certain amount of `money`. In order to complete transaction `i`, `money >= costi` must hold true. After performing a transaction, `money` becomes `money - costi + cashbacki`.

Return _the minimum amount of_ `money` _required before any transaction so that all of the transactions can be completed **regardless of the order** of the transactions._

**Example 1:**

**Input:** transactions = \[\[2,1\],\[5,0\],\[4,2\]\]
**Output:** 10
**Explanation:**
Starting with money = 10, the transactions can be performed in any order.
It can be shown that starting with money < 10 will fail to complete all transactions in some order.

**Example 2:**

**Input:** transactions = \[\[3,0\],\[0,3\]\]
**Output:** 3
**Explanation:**
- If transactions are in the order \[\[3,0\],\[0,3\]\], the minimum money required to complete the transactions is 3.
- If transactions are in the order \[\[0,3\],\[3,0\]\], the minimum money required to complete the transactions is 0.
Thus, starting with money = 3, the transactions can be performed in any order.

**Constraints:**

*   `1 <= transactions.length <= 105`
*   `transactions[i].length == 2`
*   `0 <= costi, cashbacki <= 109`
Smallest Even Multiple

Given a **positive** integer `n`, return _the smallest positive integer that is a multiple of **both**_ `2` _and_ `n`.

**Example 1:**

**Input:** n = 5
**Output:** 10
**Explanation:** The smallest multiple of both 5 and 2 is 10.

**Example 2:**

**Input:** n = 6
**Output:** 6
**Explanation:** The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.

**Constraints:**

*   `1 <= n <= 150`
Length of the Longest Alphabetical Continuous Substring

An **alphabetical continuous string** is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string `"abcdefghijklmnopqrstuvwxyz "`.

*   For example, `"abc "` is an alphabetical continuous string, while `"acb "` and `"za "` are not.

Given a string `s` consisting of lowercase letters only, return the _length of the **longest** alphabetical continuous substring._

**Example 1:**

**Input:** s =  "abacaba "
**Output:** 2
**Explanation:** There are 4 distinct continuous substrings:  "a ",  "b ",  "c " and  "ab ".
 "ab " is the longest continuous substring.

**Example 2:**

**Input:** s =  "abcde "
**Output:** 5
**Explanation:**  "abcde " is the longest continuous substring.

**Constraints:**

*   `1 <= s.length <= 105`
*   `s` consists of only English lowercase letters.
Reverse Odd Levels of Binary Tree

Given the `root` of a **perfect** binary tree, reverse the node values at each **odd** level of the tree.

*   For example, suppose the node values at level 3 are `[2,1,3,4,7,11,29,18]`, then it should become `[18,29,11,7,4,3,1,2]`.

Return _the root of the reversed tree_.

A binary tree is **perfect** if all parent nodes have two children and all leaves are on the same level.

The **level** of a node is the number of edges along the path between it and the root node.

**Example 1:**

**Input:** root = \[2,3,5,8,13,21,34\]
**Output:** \[2,5,3,8,13,21,34\]
**Explanation:** 
The tree has only one odd level.
The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.

**Example 2:**

**Input:** root = \[7,13,11\]
**Output:** \[7,11,13\]
**Explanation:** 
The nodes at level 1 are 13, 11, which are reversed and become 11, 13.

**Example 3:**

**Input:** root = \[0,1,2,0,0,0,0,1,1,1,1,2,2,2,2\]
**Output:** \[0,2,1,0,0,0,0,2,2,2,2,1,1,1,1\]
**Explanation:** 
The odd levels have non-zero values.
The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.
The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.

**Constraints:**

*   The number of nodes in the tree is in the range `[1, 214]`.
*   `0 <= Node.val <= 105`
*   `root` is a **perfect** binary tree.
Sum of Prefix Scores of Strings

You are given an array `words` of size `n` consisting of **non-empty** strings.

We define the **score** of a string `word` as the **number** of strings `words[i]` such that `word` is a **prefix** of `words[i]`.

*   For example, if `words = [ "a ", "ab ", "abc ", "cab "]`, then the score of `"ab "` is `2`, since `"ab "` is a prefix of both `"ab "` and `"abc "`.

Return _an array_ `answer` _of size_ `n` _where_ `answer[i]` _is the **sum** of scores of every **non-empty** prefix of_ `words[i]`.

**Note** that a string is considered as a prefix of itself.

**Example 1:**

**Input:** words = \[ "abc ", "ab ", "bc ", "b "\]
**Output:** \[5,4,3,2\]
**Explanation:** The answer for each string is the following:
-  "abc " has 3 prefixes:  "a ",  "ab ", and  "abc ".
- There are 2 strings with the prefix  "a ", 2 strings with the prefix  "ab ", and 1 string with the prefix  "abc ".
The total is answer\[0\] = 2 + 2 + 1 = 5.
-  "ab " has 2 prefixes:  "a " and  "ab ".
- There are 2 strings with the prefix  "a ", and 2 strings with the prefix  "ab ".
The total is answer\[1\] = 2 + 2 = 4.
-  "bc " has 2 prefixes:  "b " and  "bc ".
- There are 2 strings with the prefix  "b ", and 1 string with the prefix  "bc ".
The total is answer\[2\] = 2 + 1 = 3.
-  "b " has 1 prefix:  "b ".
- There are 2 strings with the prefix  "b ".
The total is answer\[3\] = 2.

**Example 2:**

**Input:** words = \[ "abcd "\]
**Output:** \[4\]
**Explanation:**
 "abcd " has 4 prefixes:  "a ",  "ab ",  "abc ", and  "abcd ".
Each prefix has a score of one, so the total is answer\[0\] = 1 + 1 + 1 + 1 = 4.

**Constraints:**

*   `1 <= words.length <= 1000`
*   `1 <= words[i].length <= 1000`
*   `words[i]` consists of lowercase English letters.
Closest Fair Integer

You are given a **positive** integer `n`.

We call an integer `k` fair if the number of **even** digits in `k` is equal to the number of **odd** digits in it.

Return _the **smallest** fair integer that is **greater than or equal** to_ `n`.

**Example 1:**

**Input:** n = 2
**Output:** 10
**Explanation:** The smallest fair integer that is greater than or equal to 2 is 10.
10 is fair because it has an equal number of even and odd digits (one odd digit and one even digit).

**Example 2:**

**Input:** n = 403
**Output:** 1001
**Explanation:** The smallest fair integer that is greater than or equal to 403 is 1001.
1001 is fair because it has an equal number of even and odd digits (two odd digits and two even digits).

**Constraints:**

*   `1 <= n <= 109`
Sort the People

You are given an array of strings `names`, and an array `heights` that consists of **distinct** positive integers. Both arrays are of length `n`.

For each index `i`, `names[i]` and `heights[i]` denote the name and height of the `ith` person.

Return `names` _sorted in **descending** order by the people's heights_.

**Example 1:**

**Input:** names = \[ "Mary ", "John ", "Emma "\], heights = \[180,165,170\]
**Output:** \[ "Mary ", "Emma ", "John "\]
**Explanation:** Mary is the tallest, followed by Emma and John.

**Example 2:**

**Input:** names = \[ "Alice ", "Bob ", "Bob "\], heights = \[155,185,150\]
**Output:** \[ "Bob ", "Alice ", "Bob "\]
**Explanation:** The first Bob is the tallest, followed by Alice and the second Bob.

**Constraints:**

*   `n == names.length == heights.length`
*   `1 <= n <= 103`
*   `1 <= names[i].length <= 20`
*   `1 <= heights[i] <= 105`
*   `names[i]` consists of lower and upper case English letters.
*   All the values of `heights` are distinct.
Longest Subarray With Maximum Bitwise AND

You are given an integer array `nums` of size `n`.

Consider a **non-empty** subarray from `nums` that has the **maximum** possible **bitwise AND**.

*   In other words, let `k` be the maximum value of the bitwise AND of **any** subarray of `nums`. Then, only subarrays with a bitwise AND equal to `k` should be considered.

Return _the length of the **longest** such subarray_.

The bitwise AND of an array is the bitwise AND of all the numbers in it.

A **subarray** is a contiguous sequence of elements within an array.

**Example 1:**

**Input:** nums = \[1,2,3,3,2,2\]
**Output:** 2
**Explanation:**
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is \[3,3\], so we return 2.

**Example 2:**

**Input:** nums = \[1,2,3,4\]
**Output:** 1
**Explanation:**
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is \[4\], so we return 1.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 106`
Find All Good Indices

You are given a **0-indexed** integer array `nums` of size `n` and a positive integer `k`.

We call an index `i` in the range `k <= i < n - k` **good** if the following conditions are satisfied:

*   The `k` elements that are just **before** the index `i` are in **non-increasing** order.
*   The `k` elements that are just **after** the index `i` are in **non-decreasing** order.

Return _an array of all good indices sorted in **increasing** order_.

**Example 1:**

**Input:** nums = \[2,1,1,1,3,4,1\], k = 2
**Output:** \[2,3\]
**Explanation:** There are two good indices in the array:
- Index 2. The subarray \[2,1\] is in non-increasing order, and the subarray \[1,3\] is in non-decreasing order.
- Index 3. The subarray \[1,1\] is in non-increasing order, and the subarray \[3,4\] is in non-decreasing order.
Note that the index 4 is not good because \[4,1\] is not non-decreasing.

**Example 2:**

**Input:** nums = \[2,1,1,2\], k = 2
**Output:** \[\]
**Explanation:** There are no good indices in this array.

**Constraints:**

*   `n == nums.length`
*   `3 <= n <= 105`
*   `1 <= nums[i] <= 106`
*   `1 <= k <= n / 2`
Number of Good Paths

There is a tree (i.e. a connected, undirected graph with no cycles) consisting of `n` nodes numbered from `0` to `n - 1` and exactly `n - 1` edges.

You are given a **0-indexed** integer array `vals` of length `n` where `vals[i]` denotes the value of the `ith` node. You are also given a 2D integer array `edges` where `edges[i] = [ai, bi]` denotes that there exists an **undirected** edge connecting nodes `ai` and `bi`.

A **good path** is a simple path that satisfies the following conditions:

1.  The starting node and the ending node have the **same** value.
2.  All nodes between the starting node and the ending node have values **less than or equal to** the starting node (i.e. the starting node's value should be the maximum value along the path).

Return _the number of distinct good paths_.

Note that a path and its reverse are counted as the **same** path. For example, `0 -> 1` is considered to be the same as `1 -> 0`. A single node is also considered as a valid path.

**Example 1:**

**Input:** vals = \[1,3,2,1,3\], edges = \[\[0,1\],\[0,2\],\[2,3\],\[2,4\]\]
**Output:** 6
**Explanation:** There are 5 good paths consisting of a single node.
There is 1 additional good path: 1 -> 0 -> 2 -> 4.
(The reverse path 4 -> 2 -> 0 -> 1 is treated as the same as 1 -> 0 -> 2 -> 4.)
Note that 0 -> 2 -> 3 is not a good path because vals\[2\] > vals\[0\].

**Example 2:**

**Input:** vals = \[1,1,2,2,3\], edges = \[\[0,1\],\[1,2\],\[2,3\],\[2,4\]\]
**Output:** 7
**Explanation:** There are 5 good paths consisting of a single node.
There are 2 additional good paths: 0 -> 1 and 2 -> 3.

**Example 3:**

**Input:** vals = \[1\], edges = \[\]
**Output:** 1
**Explanation:** The tree consists of only one node, so there is one good path.

**Constraints:**

*   `n == vals.length`
*   `1 <= n <= 3 * 104`
*   `0 <= vals[i] <= 105`
*   `edges.length == n - 1`
*   `edges[i].length == 2`
*   `0 <= ai, bi < n`
*   `ai != bi`
*   `edges` represents a valid tree.
Merge Operations to Turn Array Into a Palindrome

You are given an array `nums` consisting of **positive** integers.

You can perform the following operation on the array **any** number of times:

*   Choose any two **adjacent** elements and **replace** them with their **sum**.
    *   For example, if `nums = [1,2,3,1]`, you can apply one operation to make it `[1,5,1]`.

Return _the **minimum** number of operations needed to turn the array into a **palindrome**_.

**Example 1:**

**Input:** nums = \[4,3,2,1,2,3,1\]
**Output:** 2
**Explanation:** We can turn the array into a palindrome in 2 operations as follows:
- Apply the operation on the fourth and fifth element of the array, nums becomes equal to \[4,3,2,**3**,3,1\].
- Apply the operation on the fifth and sixth element of the array, nums becomes equal to \[4,3,2,3,**4**\].
The array \[4,3,2,3,4\] is a palindrome.
It can be shown that 2 is the minimum number of operations needed.

**Example 2:**

**Input:** nums = \[1,2,3,4\]
**Output:** 3
**Explanation:** We do the operation 3 times in any position, we obtain the array \[10\] at the end which is a palindrome.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 106`