bowang0911/leetcode huggingface dataset:
Destroying Asteroids

You are given an integer `mass`, which represents the original mass of a planet. You are further given an integer array `asteroids`, where `asteroids[i]` is the mass of the `ith` asteroid.

You can arrange for the planet to collide with the asteroids in **any arbitrary order**. If the mass of the planet is **greater than or equal to** the mass of the asteroid, the asteroid is **destroyed** and the planet **gains** the mass of the asteroid. Otherwise, the planet is destroyed.

Return `true` _if **all** asteroids can be destroyed. Otherwise, return_ `false`_._

**Example 1:**

**Input:** mass = 10, asteroids = \[3,9,19,5,21\]
**Output:** true
**Explanation:** One way to order the asteroids is \[9,19,5,3,21\]:
- The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19
- The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38
- The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43
- The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46
- The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67
All asteroids are destroyed.

**Example 2:**

**Input:** mass = 5, asteroids = \[4,9,23,4\]
**Output:** false
**Explanation:** 
The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.
After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.
This is less than 23, so a collision would not destroy the last asteroid.

**Constraints:**

*   `1 <= mass <= 105`
*   `1 <= asteroids.length <= 105`
*   `1 <= asteroids[i] <= 105`
Maximum Employees to Be Invited to a Meeting

A company is organizing a meeting and has a list of `n` employees, waiting to be invited. They have arranged for a large **circular** table, capable of seating **any number** of employees.

The employees are numbered from `0` to `n - 1`. Each employee has a **favorite** person and they will attend the meeting **only if** they can sit next to their favorite person at the table. The favorite person of an employee is **not** themself.

Given a **0-indexed** integer array `favorite`, where `favorite[i]` denotes the favorite person of the `ith` employee, return _the **maximum number of employees** that can be invited to the meeting_.

**Example 1:**

**Input:** favorite = \[2,2,1,2\]
**Output:** 3
**Explanation:**
The above figure shows how the company can invite employees 0, 1, and 2, and seat them at the round table.
All employees cannot be invited because employee 2 cannot sit beside employees 0, 1, and 3, simultaneously.
Note that the company can also invite employees 1, 2, and 3, and give them their desired seats.
The maximum number of employees that can be invited to the meeting is 3. 

**Example 2:**

**Input:** favorite = \[1,2,0\]
**Output:** 3
**Explanation:** 
Each employee is the favorite person of at least one other employee, and the only way the company can invite them is if they invite every employee.
The seating arrangement will be the same as that in the figure given in example 1:
- Employee 0 will sit between employees 2 and 1.
- Employee 1 will sit between employees 0 and 2.
- Employee 2 will sit between employees 1 and 0.
The maximum number of employees that can be invited to the meeting is 3.

**Example 3:**

**Input:** favorite = \[3,0,1,4,1\]
**Output:** 4
**Explanation:**
The above figure shows how the company will invite employees 0, 1, 3, and 4, and seat them at the round table.
Employee 2 cannot be invited because the two spots next to their favorite employee 1 are taken.
So the company leaves them out of the meeting.
The maximum number of employees that can be invited to the meeting is 4.

**Constraints:**

*   `n == favorite.length`
*   `2 <= n <= 105`
*   `0 <= favorite[i] <= n - 1`
*   `favorite[i] != i`
Remove All Ones With Row and Column Flips

You are given an `m x n` binary matrix `grid`.

In one operation, you can choose **any** row or column and flip each value in that row or column (i.e., changing all `0`'s to `1`'s, and all `1`'s to `0`'s).

Return `true` _if it is possible to remove all_ `1`_'s from_ `grid` using **any** number of operations or `false` otherwise.

**Example 1:**

**Input:** grid = \[\[0,1,0\],\[1,0,1\],\[0,1,0\]\]
**Output:** true
**Explanation:** One possible way to remove all 1's from grid is to:
- Flip the middle row
- Flip the middle column

**Example 2:**

**Input:** grid = \[\[1,1,0\],\[0,0,0\],\[0,0,0\]\]
**Output:** false
**Explanation:** It is impossible to remove all 1's from grid.

**Example 3:**

**Input:** grid = \[\[0\]\]
**Output:** true
**Explanation:** There are no 1's in grid.

**Constraints:**

*   `m == grid.length`
*   `n == grid[i].length`
*   `1 <= m, n <= 300`
*   `grid[i][j]` is either `0` or `1`.
Capitalize the Title

You are given a string `title` consisting of one or more words separated by a single space, where each word consists of English letters. **Capitalize** the string by changing the capitalization of each word such that:

*   If the length of the word is `1` or `2` letters, change all letters to lowercase.
*   Otherwise, change the first letter to uppercase and the remaining letters to lowercase.

Return _the **capitalized**_ `title`.

**Example 1:**

**Input:** title =  "capiTalIze tHe titLe "
**Output:**  "Capitalize The Title "
**Explanation:**
Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.

**Example 2:**

**Input:** title =  "First leTTeR of EACH Word "
**Output:**  "First Letter of Each Word "
**Explanation:**
The word  "of " has length 2, so it is all lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

**Example 3:**

**Input:** title =  "i lOve leetcode "
**Output:**  "i Love Leetcode "
**Explanation:**
The word  "i " has length 1, so it is lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

**Constraints:**

*   `1 <= title.length <= 100`
*   `title` consists of words separated by a single space without any leading or trailing spaces.
*   Each word consists of uppercase and lowercase English letters and is **non-empty**.
Maximum Twin Sum of a Linked List

In a linked list of size `n`, where `n` is **even**, the `ith` node (**0-indexed**) of the linked list is known as the **twin** of the `(n-1-i)th` node, if `0 <= i <= (n / 2) - 1`.

*   For example, if `n = 4`, then node `0` is the twin of node `3`, and node `1` is the twin of node `2`. These are the only nodes with twins for `n = 4`.

The **twin sum** is defined as the sum of a node and its twin.

Given the `head` of a linked list with even length, return _the **maximum twin sum** of the linked list_.

**Example 1:**

**Input:** head = \[5,4,2,1\]
**Output:** 6
**Explanation:**
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
There are no other nodes with twins in the linked list.
Thus, the maximum twin sum of the linked list is 6. 

**Example 2:**

**Input:** head = \[4,2,2,3\]
**Output:** 7
**Explanation:**
The nodes with twins present in this linked list are:
- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
Thus, the maximum twin sum of the linked list is max(7, 4) = 7. 

**Example 3:**

**Input:** head = \[1,100000\]
**Output:** 100001
**Explanation:**
There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.

**Constraints:**

*   The number of nodes in the list is an **even** integer in the range `[2, 105]`.
*   `1 <= Node.val <= 105`
Longest Palindrome by Concatenating Two Letter Words

You are given an array of strings `words`. Each element of `words` consists of **two** lowercase English letters.

Create the **longest possible palindrome** by selecting some elements from `words` and concatenating them in **any order**. Each element can be selected **at most once**.

Return _the **length** of the longest palindrome that you can create_. If it is impossible to create any palindrome, return `0`.

A **palindrome** is a string that reads the same forward and backward.

**Example 1:**

**Input:** words = \[ "lc ", "cl ", "gg "\]
**Output:** 6
**Explanation:** One longest palindrome is  "lc " +  "gg " +  "cl " =  "lcggcl ", of length 6.
Note that  "clgglc " is another longest palindrome that can be created.

**Example 2:**

**Input:** words = \[ "ab ", "ty ", "yt ", "lc ", "cl ", "ab "\]
**Output:** 8
**Explanation:** One longest palindrome is  "ty " +  "lc " +  "cl " +  "yt " =  "tylcclyt ", of length 8.
Note that  "lcyttycl " is another longest palindrome that can be created.

**Example 3:**

**Input:** words = \[ "cc ", "ll ", "xx "\]
**Output:** 2
**Explanation:** One longest palindrome is  "cc ", of length 2.
Note that  "ll " is another longest palindrome that can be created, and so is  "xx ".

**Constraints:**

*   `1 <= words.length <= 105`
*   `words[i].length == 2`
*   `words[i]` consists of lowercase English letters.
Stamping the Grid

You are given an `m x n` binary matrix `grid` where each cell is either `0` (empty) or `1` (occupied).

You are then given stamps of size `stampHeight x stampWidth`. We want to fit the stamps such that they follow the given **restrictions** and **requirements**:

1.  Cover all the **empty** cells.
2.  Do not cover any of the **occupied** cells.
3.  We can put as **many** stamps as we want.
4.  Stamps can **overlap** with each other.
5.  Stamps are not allowed to be **rotated**.
6.  Stamps must stay completely **inside** the grid.

Return `true` _if it is possible to fit the stamps while following the given restrictions and requirements. Otherwise, return_ `false`.

**Example 1:**

**Input:** grid = \[\[1,0,0,0\],\[1,0,0,0\],\[1,0,0,0\],\[1,0,0,0\],\[1,0,0,0\]\], stampHeight = 4, stampWidth = 3
**Output:** true
**Explanation:** We have two overlapping stamps (labeled 1 and 2 in the image) that are able to cover all the empty cells.

**Example 2:**

**Input:** grid = \[\[1,0,0,0\],\[0,1,0,0\],\[0,0,1,0\],\[0,0,0,1\]\], stampHeight = 2, stampWidth = 2 
**Output:** false 
**Explanation:** There is no way to fit the stamps onto all the empty cells without the stamps going outside the grid.

**Constraints:**

*   `m == grid.length`
*   `n == grid[r].length`
*   `1 <= m, n <= 105`
*   `1 <= m * n <= 2 * 105`
*   `grid[r][c]` is either `0` or `1`.
*   `1 <= stampHeight, stampWidth <= 105`
Check if Every Row and Column Contains All Numbers

An `n x n` matrix is **valid** if every row and every column contains **all** the integers from `1` to `n` (**inclusive**).

Given an `n x n` integer matrix `matrix`, return `true` _if the matrix is **valid**._ Otherwise, return `false`.

**Example 1:**

**Input:** matrix = \[\[1,2,3\],\[3,1,2\],\[2,3,1\]\]
**Output:** true
**Explanation:** In this case, n = 3, and every row and column contains the numbers 1, 2, and 3.
Hence, we return true.

**Example 2:**

**Input:** matrix = \[\[1,1,1\],\[1,2,3\],\[1,2,3\]\]
**Output:** false
**Explanation:** In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3.
Hence, we return false.

**Constraints:**

*   `n == matrix.length == matrix[i].length`
*   `1 <= n <= 100`
*   `1 <= matrix[i][j] <= n`
Minimum Swaps to Group All 1's Together II

A **swap** is defined as taking two **distinct** positions in an array and swapping the values in them.

A **circular** array is defined as an array where we consider the **first** element and the **last** element to be **adjacent**.

Given a **binary** **circular** array `nums`, return _the minimum number of swaps required to group all_ `1`_'s present in the array together at **any location**_.

**Example 1:**

**Input:** nums = \[0,1,0,1,1,0,0\]
**Output:** 1
**Explanation:** Here are a few of the ways to group all the 1's together:
\[0,0,1,1,1,0,0\] using 1 swap.
\[0,1,1,1,0,0,0\] using 1 swap.
\[1,1,0,0,0,0,1\] using 2 swaps (using the circular property of the array).
There is no way to group all 1's together with 0 swaps.
Thus, the minimum number of swaps required is 1.

**Example 2:**

**Input:** nums = \[0,1,1,1,0,0,1,1,0\]
**Output:** 2
**Explanation:** Here are a few of the ways to group all the 1's together:
\[1,1,1,0,0,0,0,1,1\] using 2 swaps (using the circular property of the array).
\[1,1,1,1,1,0,0,0,0\] using 2 swaps.
There is no way to group all 1's together with 0 or 1 swaps.
Thus, the minimum number of swaps required is 2.

**Example 3:**

**Input:** nums = \[1,1,0,0,1\]
**Output:** 0
**Explanation:** All the 1's are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `nums[i]` is either `0` or `1`.
Count Words Obtained After Adding a Letter

You are given two **0-indexed** arrays of strings `startWords` and `targetWords`. Each string consists of **lowercase English letters** only.

For each string in `targetWords`, check if it is possible to choose a string from `startWords` and perform a **conversion operation** on it to be equal to that from `targetWords`.

The **conversion operation** is described in the following two steps:

1.  **Append** any lowercase letter that is **not present** in the string to its end.
    *   For example, if the string is `"abc "`, the letters `'d'`, `'e'`, or `'y'` can be added to it, but not `'a'`. If `'d'` is added, the resulting string will be `"abcd "`.
2.  **Rearrange** the letters of the new string in **any** arbitrary order.
    *   For example, `"abcd "` can be rearranged to `"acbd "`, `"bacd "`, `"cbda "`, and so on. Note that it can also be rearranged to `"abcd "` itself.

Return _the **number of strings** in_ `targetWords` _that can be obtained by performing the operations on **any** string of_ `startWords`.

**Note** that you will only be verifying if the string in `targetWords` can be obtained from a string in `startWords` by performing the operations. The strings in `startWords` **do not** actually change during this process.

**Example 1:**

**Input:** startWords = \[ "ant ", "act ", "tack "\], targetWords = \[ "tack ", "act ", "acti "\]
**Output:** 2
**Explanation:**
- In order to form targetWords\[0\] =  "tack ", we use startWords\[1\] =  "act ", append 'k' to it, and rearrange  "actk " to  "tack ".
- There is no string in startWords that can be used to obtain targetWords\[1\] =  "act ".
  Note that  "act " does exist in startWords, but we **must** append one letter to the string before rearranging it.
- In order to form targetWords\[2\] =  "acti ", we use startWords\[1\] =  "act ", append 'i' to it, and rearrange  "acti " to  "acti " itself.

**Example 2:**

**Input:** startWords = \[ "ab ", "a "\], targetWords = \[ "abc ", "abcd "\]
**Output:** 1
**Explanation:**
- In order to form targetWords\[0\] =  "abc ", we use startWords\[0\] =  "ab ", add 'c' to it, and rearrange it to  "abc ".
- There is no string in startWords that can be used to obtain targetWords\[1\] =  "abcd ".

**Constraints:**

*   `1 <= startWords.length, targetWords.length <= 5 * 104`
*   `1 <= startWords[i].length, targetWords[j].length <= 26`
*   Each string of `startWords` and `targetWords` consists of lowercase English letters only.
*   No letter occurs more than once in any string of `startWords` or `targetWords`.
Earliest Possible Day of Full Bloom

You have `n` flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two **0-indexed** integer arrays `plantTime` and `growTime`, of length `n` each:

*   `plantTime[i]` is the number of **full days** it takes you to **plant** the `ith` seed. Every day, you can work on planting exactly one seed. You **do not** have to work on planting the same seed on consecutive days, but the planting of a seed is not complete **until** you have worked `plantTime[i]` days on planting it in total.
*   `growTime[i]` is the number of **full days** it takes the `ith` seed to grow after being completely planted. **After** the last day of its growth, the flower **blooms** and stays bloomed forever.

From the beginning of day `0`, you can plant the seeds in **any** order.

Return _the **earliest** possible day where **all** seeds are blooming_.

**Example 1:**

**Input:** plantTime = \[1,4,3\], growTime = \[2,3,1\]
**Output:** 9
**Explanation:** The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
One optimal way is:
On day 0, plant the 0th seed. The seed grows for 2 full days and blooms on day 3.
On days 1, 2, 3, and 4, plant the 1st seed. The seed grows for 3 full days and blooms on day 8.
On days 5, 6, and 7, plant the 2nd seed. The seed grows for 1 full day and blooms on day 9.
Thus, on day 9, all the seeds are blooming.

**Example 2:**

**Input:** plantTime = \[1,2,3,2\], growTime = \[2,1,2,1\]
**Output:** 9
**Explanation:** The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
One optimal way is:
On day 1, plant the 0th seed. The seed grows for 2 full days and blooms on day 4.
On days 0 and 3, plant the 1st seed. The seed grows for 1 full day and blooms on day 5.
On days 2, 4, and 5, plant the 2nd seed. The seed grows for 2 full days and blooms on day 8.
On days 6 and 7, plant the 3rd seed. The seed grows for 1 full day and blooms on day 9.
Thus, on day 9, all the seeds are blooming.

**Example 3:**

**Input:** plantTime = \[1\], growTime = \[1\]
**Output:** 2
**Explanation:** On day 0, plant the 0th seed. The seed grows for 1 full day and blooms on day 2.
Thus, on day 2, all the seeds are blooming.

**Constraints:**

*   `n == plantTime.length == growTime.length`
*   `1 <= n <= 105`
*   `1 <= plantTime[i], growTime[i] <= 104`
Pour Water Between Buckets to Make Water Levels Equal

You have `n` buckets each containing some gallons of water in it, represented by a **0-indexed** integer array `buckets`, where the `ith` bucket contains `buckets[i]` gallons of water. You are also given an integer `loss`.

You want to make the amount of water in each bucket equal. You can pour any amount of water from one bucket to another bucket (not necessarily an integer). However, every time you pour `k` gallons of water, you spill `loss` **percent** of `k`.

Return _the **maximum** amount of water in each bucket after making the amount of water equal._ Answers within `10-5` of the actual answer will be accepted.

**Example 1:**

**Input:** buckets = \[1,2,7\], loss = 80
**Output:** 2.00000
**Explanation:** Pour 5 gallons of water from buckets\[2\] to buckets\[0\].
5 \* 80% = 4 gallons are spilled and buckets\[0\] only receives 5 - 4 = 1 gallon of water.
All buckets have 2 gallons of water in them so return 2.

**Example 2:**

**Input:** buckets = \[2,4,6\], loss = 50
**Output:** 3.50000
**Explanation:** Pour 0.5 gallons of water from buckets\[1\] to buckets\[0\].
0.5 \* 50% = 0.25 gallons are spilled and buckets\[0\] only receives 0.5 - 0.25 = 0.25 gallons of water.
Now, buckets = \[2.25, 3.5, 6\].
Pour 2.5 gallons of water from buckets\[2\] to buckets\[0\].
2.5 \* 50% = 1.25 gallons are spilled and buckets\[0\] only receives 2.5 - 1.25 = 1.25 gallons of water.
All buckets have 3.5 gallons of water in them so return 3.5.

**Example 3:**

**Input:** buckets = \[3,3,3,3\], loss = 40
**Output:** 3.00000
**Explanation:** All buckets already have the same amount of water in them.

**Constraints:**

*   `1 <= buckets.length <= 105`
*   `0 <= buckets[i] <= 105`
*   `0 <= loss <= 99`
Divide a String Into Groups of Size k

A string `s` can be partitioned into groups of size `k` using the following procedure:

*   The first group consists of the first `k` characters of the string, the second group consists of the next `k` characters of the string, and so on. Each character can be a part of **exactly one** group.
*   For the last group, if the string **does not** have `k` characters remaining, a character `fill` is used to complete the group.

Note that the partition is done so that after removing the `fill` character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be `s`.

Given the string `s`, the size of each group `k` and the character `fill`, return _a string array denoting the **composition of every group**_ `s` _has been divided into, using the above procedure_.

**Example 1:**

**Input:** s =  "abcdefghi ", k = 3, fill =  "x "
**Output:** \[ "abc ", "def ", "ghi "\]
**Explanation:**
The first 3 characters  "abc " form the first group.
The next 3 characters  "def " form the second group.
The last 3 characters  "ghi " form the third group.
Since all groups can be completely filled by characters from the string, we do not need to use fill.
Thus, the groups formed are  "abc ",  "def ", and  "ghi ".

**Example 2:**

**Input:** s =  "abcdefghij ", k = 3, fill =  "x "
**Output:** \[ "abc ", "def ", "ghi ", "jxx "\]
**Explanation:**
Similar to the previous example, we are forming the first three groups  "abc ",  "def ", and  "ghi ".
For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.
Thus, the 4 groups formed are  "abc ",  "def ",  "ghi ", and  "jxx ".

**Constraints:**

*   `1 <= s.length <= 100`
*   `s` consists of lowercase English letters only.
*   `1 <= k <= 100`
*   `fill` is a lowercase English letter.
Minimum Moves to Reach Target Score

You are playing a game with integers. You start with the integer `1` and you want to reach the integer `target`.

In one move, you can either:

*   **Increment** the current integer by one (i.e., `x = x + 1`).
*   **Double** the current integer (i.e., `x = 2 * x`).

You can use the **increment** operation **any** number of times, however, you can only use the **double** operation **at most** `maxDoubles` times.

Given the two integers `target` and `maxDoubles`, return _the minimum number of moves needed to reach_ `target` _starting with_ `1`.

**Example 1:**

**Input:** target = 5, maxDoubles = 0
**Output:** 4
**Explanation:** Keep incrementing by 1 until you reach target.

**Example 2:**

**Input:** target = 19, maxDoubles = 2
**Output:** 7
**Explanation:** Initially, x = 1
Increment 3 times so x = 4
Double once so x = 8
Increment once so x = 9
Double again so x = 18
Increment once so x = 19

**Example 3:**

**Input:** target = 10, maxDoubles = 4
**Output:** 4
**Explanation:** Initially, x = 1
Increment once so x = 2
Double once so x = 4
Increment once so x = 5
Double again so x = 10

**Constraints:**

*   `1 <= target <= 109`
*   `0 <= maxDoubles <= 100`