bowang0911/leetcode huggingface dataset:
Count Collisions on a Road

There are `n` cars on an infinitely long road. The cars are numbered from `0` to `n - 1` from left to right and each car is present at a **unique** point.

You are given a **0-indexed** string `directions` of length `n`. `directions[i]` can be either `'L'`, `'R'`, or `'S'` denoting whether the `ith` car is moving towards the **left**, towards the **right**, or **staying** at its current point respectively. Each moving car has the **same speed**.

The number of collisions can be calculated as follows:

*   When two cars moving in **opposite** directions collide with each other, the number of collisions increases by `2`.
*   When a moving car collides with a stationary car, the number of collisions increases by `1`.

After a collision, the cars involved can no longer move and will stay at the point where they collided. Other than that, cars cannot change their state or direction of motion.

Return _the **total number of collisions** that will happen on the road_.

**Example 1:**

**Input:** directions =  "RLRSLL "
**Output:** 5
**Explanation:**
The collisions that will happen on the road are:
- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.
- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
- Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.
Thus, the total number of collisions that will happen on the road is 5. 

**Example 2:**

**Input:** directions =  "LLRR "
**Output:** 0
**Explanation:**
No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.

**Constraints:**

*   `1 <= directions.length <= 105`
*   `directions[i]` is either `'L'`, `'R'`, or `'S'`.
Maximum Points in an Archery Competition

Alice and Bob are opponents in an archery competition. The competition has set the following rules:

1.  Alice first shoots `numArrows` arrows and then Bob shoots `numArrows` arrows.
2.  The points are then calculated as follows:
    1.  The target has integer scoring sections ranging from `0` to `11` **inclusive**.
    2.  For **each** section of the target with score `k` (in between `0` to `11`), say Alice and Bob have shot `ak` and `bk` arrows on that section respectively. If `ak >= bk`, then Alice takes `k` points. If `ak < bk`, then Bob takes `k` points.
    3.  However, if `ak == bk == 0`, then **nobody** takes `k` points.

*   For example, if Alice and Bob both shot `2` arrows on the section with score `11`, then Alice takes `11` points. On the other hand, if Alice shot `0` arrows on the section with score `11` and Bob shot `2` arrows on that same section, then Bob takes `11` points.
    

You are given the integer `numArrows` and an integer array `aliceArrows` of size `12`, which represents the number of arrows Alice shot on each scoring section from `0` to `11`. Now, Bob wants to **maximize** the total number of points he can obtain.

Return _the array_ `bobArrows` _which represents the number of arrows Bob shot on **each** scoring section from_ `0` _to_ `11`. The sum of the values in `bobArrows` should equal `numArrows`.

If there are multiple ways for Bob to earn the maximum total points, return **any** one of them.

**Example 1:**

**Input:** numArrows = 9, aliceArrows = \[1,1,0,1,0,0,2,1,0,1,2,0\]
**Output:** \[0,0,0,0,1,1,0,0,1,2,3,1\]
**Explanation:** The table above shows how the competition is scored. 
Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 = 47.
It can be shown that Bob cannot obtain a score higher than 47 points.

**Example 2:**

**Input:** numArrows = 3, aliceArrows = \[0,0,1,0,0,0,0,0,0,0,0,2\]
**Output:** \[0,0,0,0,0,0,0,0,1,1,1,0\]
**Explanation:** The table above shows how the competition is scored.
Bob earns a total point of 8 + 9 + 10 = 27.
It can be shown that Bob cannot obtain a score higher than 27 points.

**Constraints:**

*   `1 <= numArrows <= 105`
*   `aliceArrows.length == bobArrows.length == 12`
*   `0 <= aliceArrows[i], bobArrows[i] <= numArrows`
*   `sum(aliceArrows[i]) == numArrows`
Longest Substring of One Repeating Character

You are given a **0-indexed** string `s`. You are also given a **0-indexed** string `queryCharacters` of length `k` and a **0-indexed** array of integer **indices** `queryIndices` of length `k`, both of which are used to describe `k` queries.

The `ith` query updates the character in `s` at index `queryIndices[i]` to the character `queryCharacters[i]`.

Return _an array_ `lengths` _of length_ `k` _where_ `lengths[i]` _is the **length** of the **longest substring** of_ `s` _consisting of **only one repeating** character **after** the_ `ith` _query_ _is performed._

**Example 1:**

**Input:** s =  "babacc ", queryCharacters =  "bcb ", queryIndices = \[1,3,3\]
**Output:** \[3,3,4\]
**Explanation:** 
- 1st query updates s =  "b**b**bacc ". The longest substring consisting of one repeating character is  "bbb " with length 3.
- 2nd query updates s =  "bbb**c**cc ". 
  The longest substring consisting of one repeating character can be  "bbb " or  "ccc " with length 3.
- 3rd query updates s =  "bbb**b**cc ". The longest substring consisting of one repeating character is  "bbbb " with length 4.
Thus, we return \[3,3,4\].

**Example 2:**

**Input:** s =  "abyzz ", queryCharacters =  "aa ", queryIndices = \[2,1\]
**Output:** \[2,3\]
**Explanation:**
- 1st query updates s =  "ab**a**zz ". The longest substring consisting of one repeating character is  "zz " with length 2.
- 2nd query updates s =  "a**a**azz ". The longest substring consisting of one repeating character is  "aaa " with length 3.
Thus, we return \[2,3\].

**Constraints:**

*   `1 <= s.length <= 105`
*   `s` consists of lowercase English letters.
*   `k == queryCharacters.length == queryIndices.length`
*   `1 <= k <= 105`
*   `queryCharacters` consists of lowercase English letters.
*   `0 <= queryIndices[i] < s.length`
Minimum Health to Beat Game

You are playing a game that has `n` levels numbered from `0` to `n - 1`. You are given a **0-indexed** integer array `damage` where `damage[i]` is the amount of health you will lose to complete the `ith` level.

You are also given an integer `armor`. You may use your armor ability **at most once** during the game on **any** level which will protect you from **at most** `armor` damage.

You must complete the levels in order and your health must be **greater than** `0` at all times to beat the game.

Return _the **minimum** health you need to start with to beat the game._

**Example 1:**

**Input:** damage = \[2,7,4,3\], armor = 4
**Output:** 13
**Explanation:** One optimal way to beat the game starting at 13 health is:
On round 1, take 2 damage. You have 13 - 2 = 11 health.
On round 2, take 7 damage. You have 11 - 7 = 4 health.
On round 3, use your armor to protect you from 4 damage. You have 4 - 0 = 4 health.
On round 4, take 3 damage. You have 4 - 3 = 1 health.
Note that 13 is the minimum health you need to start with to beat the game.

**Example 2:**

**Input:** damage = \[2,5,3,4\], armor = 7
**Output:** 10
**Explanation:** One optimal way to beat the game starting at 10 health is:
On round 1, take 2 damage. You have 10 - 2 = 8 health.
On round 2, use your armor to protect you from 5 damage. You have 8 - 0 = 8 health.
On round 3, take 3 damage. You have 8 - 3 = 5 health.
On round 4, take 4 damage. You have 5 - 4 = 1 health.
Note that 10 is the minimum health you need to start with to beat the game.

**Example 3:**

**Input:** damage = \[3,3,3\], armor = 0
**Output:** 10
**Explanation:** One optimal way to beat the game starting at 10 health is:
On round 1, take 3 damage. You have 10 - 3 = 7 health.
On round 2, take 3 damage. You have 7 - 3 = 4 health.
On round 3, take 3 damage. You have 4 - 3 = 1 health.
Note that you did not use your armor ability.

**Constraints:**

*   `n == damage.length`
*   `1 <= n <= 105`
*   `0 <= damage[i] <= 105`
*   `0 <= armor <= 105`
Find the Difference of Two Arrays

Given two **0-indexed** integer arrays `nums1` and `nums2`, return _a list_ `answer` _of size_ `2` _where:_

*   `answer[0]` _is a list of all **distinct** integers in_ `nums1` _which are **not** present in_ `nums2`_._
*   `answer[1]` _is a list of all **distinct** integers in_ `nums2` _which are **not** present in_ `nums1`.

**Note** that the integers in the lists may be returned in **any** order.

**Example 1:**

**Input:** nums1 = \[1,2,3\], nums2 = \[2,4,6\]
**Output:** \[\[1,3\],\[4,6\]\]
**Explanation:**
For nums1, nums1\[1\] = 2 is present at index 0 of nums2, whereas nums1\[0\] = 1 and nums1\[2\] = 3 are not present in nums2. Therefore, answer\[0\] = \[1,3\].
For nums2, nums2\[0\] = 2 is present at index 1 of nums1, whereas nums2\[1\] = 4 and nums2\[2\] = 6 are not present in nums2. Therefore, answer\[1\] = \[4,6\].

**Example 2:**

**Input:** nums1 = \[1,2,3,3\], nums2 = \[1,1,2,2\]
**Output:** \[\[3\],\[\]\]
**Explanation:**
For nums1, nums1\[2\] and nums1\[3\] are not present in nums2. Since nums1\[2\] == nums1\[3\], their value is only included once and answer\[0\] = \[3\].
Every integer in nums2 is present in nums1. Therefore, answer\[1\] = \[\].

**Constraints:**

*   `1 <= nums1.length, nums2.length <= 1000`
*   `-1000 <= nums1[i], nums2[i] <= 1000`
Minimum Deletions to Make Array Beautiful

You are given a **0-indexed** integer array `nums`. The array `nums` is **beautiful** if:

*   `nums.length` is even.
*   `nums[i] != nums[i + 1]` for all `i % 2 == 0`.

Note that an empty array is considered beautiful.

You can delete any number of elements from `nums`. When you delete an element, all the elements to the right of the deleted element will be **shifted one unit to the left** to fill the gap created and all the elements to the left of the deleted element will remain **unchanged**.

Return _the **minimum** number of elements to delete from_ `nums` _to make it_ _beautiful._

**Example 1:**

**Input:** nums = \[1,1,2,3,5\]
**Output:** 1
**Explanation:** You can delete either `nums[0]` or `nums[1]` to make `nums` = \[1,2,3,5\] which is beautiful. It can be proven you need at least 1 deletion to make `nums` beautiful.

**Example 2:**

**Input:** nums = \[1,1,2,2,3,3\]
**Output:** 2
**Explanation:** You can delete `nums[0]` and `nums[5]` to make nums = \[1,2,2,3\] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `0 <= nums[i] <= 105`
Find Palindrome With Fixed Length

Given an integer array `queries` and a **positive** integer `intLength`, return _an array_ `answer` _where_ `answer[i]` _is either the_ `queries[i]th` _smallest **positive palindrome** of length_ `intLength` _or_ `-1` _if no such palindrome exists_.

A **palindrome** is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.

**Example 1:**

**Input:** queries = \[1,2,3,4,5,90\], intLength = 3
**Output:** \[101,111,121,131,141,999\]
**Explanation:**
The first few palindromes of length 3 are:
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...
The 90th palindrome of length 3 is 999.

**Example 2:**

**Input:** queries = \[2,4,6\], intLength = 4
**Output:** \[1111,1331,1551\]
**Explanation:**
The first six palindromes of length 4 are:
1001, 1111, 1221, 1331, 1441, and 1551.

**Constraints:**

*   `1 <= queries.length <= 5 * 104`
*   `1 <= queries[i] <= 109`
*   `1 <= intLength <= 15`
Maximum Value of K Coins From Piles

There are `n` **piles** of coins on a table. Each pile consists of a **positive number** of coins of assorted denominations.

In one move, you can choose any coin on **top** of any pile, remove it, and add it to your wallet.

Given a list `piles`, where `piles[i]` is a list of integers denoting the composition of the `ith` pile from **top to bottom**, and a positive integer `k`, return _the **maximum total value** of coins you can have in your wallet if you choose **exactly**_ `k` _coins optimally_.

**Example 1:**

**Input:** piles = \[\[1,100,3\],\[7,8,9\]\], k = 2
**Output:** 101
**Explanation:**
The above diagram shows the different ways we can choose k coins.
The maximum total we can obtain is 101.

**Example 2:**

**Input:** piles = \[\[100\],\[100\],\[100\],\[100\],\[100\],\[100\],\[1,1,1,1,1,1,700\]\], k = 7
**Output:** 706
**Explanation:**
The maximum total can be obtained if we choose all coins from the last pile.

**Constraints:**

*   `n == piles.length`
*   `1 <= n <= 1000`
*   `1 <= piles[i][j] <= 105`
*   `1 <= k <= sum(piles[i].length) <= 2000`
Maximum Sum Score of Array

You are given a **0-indexed** integer array `nums` of length `n`.

The **sum** **score** of `nums` at an index `i` where `0 <= i < n` is the **maximum** of:

*   The sum of the **first** `i + 1` elements of `nums`.
*   The sum of the **last** `n - i` elements of `nums`.

Return _the **maximum** **sum** **score** of_ `nums` _at any index._

**Example 1:**

**Input:** nums = \[4,3,-2,5\]
**Output:** 10
**Explanation:**
The sum score at index 0 is max(4, 4 + 3 + -2 + 5) = max(4, 10) = 10.
The sum score at index 1 is max(4 + 3, 3 + -2 + 5) = max(7, 6) = 7.
The sum score at index 2 is max(4 + 3 + -2, -2 + 5) = max(5, 3) = 5.
The sum score at index 3 is max(4 + 3 + -2 + 5, 5) = max(10, 5) = 10.
The maximum sum score of nums is 10.

**Example 2:**

**Input:** nums = \[-3,-5\]
**Output:** -3
**Explanation:**
The sum score at index 0 is max(-3, -3 + -5) = max(-3, -8) = -3.
The sum score at index 1 is max(-3 + -5, -5) = max(-8, -5) = -5.
The maximum sum score of nums is -3.

**Constraints:**

*   `n == nums.length`
*   `1 <= n <= 105`
*   `-105 <= nums[i] <= 105`
Minimum Bit Flips to Convert Number

A **bit flip** of a number `x` is choosing a bit in the binary representation of `x` and **flipping** it from either `0` to `1` or `1` to `0`.

*   For example, for `x = 7`, the binary representation is `111` and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get `110`, flip the second bit from the right to get `101`, flip the fifth bit from the right (a leading zero) to get `10111`, etc.

Given two integers `start` and `goal`, return _the **minimum** number of **bit flips** to convert_ `start` _to_ `goal`.

**Example 1:**

**Input:** start = 10, goal = 7
**Output:** 3
**Explanation:** The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
- Flip the first bit from the right: 1010 -> 1011.
- Flip the third bit from the right: 1011 -> 1111.
- Flip the fourth bit from the right: 1111 -> 0111.
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.

**Example 2:**

**Input:** start = 3, goal = 4
**Output:** 3
**Explanation:** The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
- Flip the first bit from the right: 011 -> 010.
- Flip the second bit from the right: 010 -> 000.
- Flip the third bit from the right: 000 -> 100.
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.

**Constraints:**

*   `0 <= start, goal <= 109`
Find Triangular Sum of an Array

You are given a **0-indexed** integer array `nums`, where `nums[i]` is a digit between `0` and `9` (**inclusive**).

The **triangular sum** of `nums` is the value of the only element present in `nums` after the following process terminates:

1.  Let `nums` comprise of `n` elements. If `n == 1`, **end** the process. Otherwise, **create** a new **0-indexed** integer array `newNums` of length `n - 1`.
2.  For each index `i`, where `0 <= i < n - 1`, **assign** the value of `newNums[i]` as `(nums[i] + nums[i+1]) % 10`, where `%` denotes modulo operator.
3.  **Replace** the array `nums` with `newNums`.
4.  **Repeat** the entire process starting from step 1.

Return _the triangular sum of_ `nums`.

**Example 1:**

**Input:** nums = \[1,2,3,4,5\]
**Output:** 8
**Explanation:**
The above diagram depicts the process from which we obtain the triangular sum of the array.

**Example 2:**

**Input:** nums = \[5\]
**Output:** 5
**Explanation:**
Since there is only one element in nums, the triangular sum is the value of that element itself.

**Constraints:**

*   `1 <= nums.length <= 1000`
*   `0 <= nums[i] <= 9`
Number of Ways to Select Buildings

You are given a **0-indexed** binary string `s` which represents the types of buildings along a street where:

*   `s[i] = '0'` denotes that the `ith` building is an office and
*   `s[i] = '1'` denotes that the `ith` building is a restaurant.

As a city official, you would like to **select** 3 buildings for random inspection. However, to ensure variety, **no two consecutive** buildings out of the **selected** buildings can be of the same type.

*   For example, given `s = "0**0**1**1**0**1** "`, we cannot select the `1st`, `3rd`, and `5th` buildings as that would form `"0**11** "` which is **not** allowed due to having two consecutive buildings of the same type.

Return _the **number of valid ways** to select 3 buildings._

**Example 1:**

**Input:** s =  "001101 "
**Output:** 6
**Explanation:** 
The following sets of indices selected are valid:
- \[0,2,4\] from  "**0**0**1**1**0**1 " forms  "010 "
- \[0,3,4\] from  "**0**01**10**1 " forms  "010 "
- \[1,2,4\] from  "0**01**1**0**1 " forms  "010 "
- \[1,3,4\] from  "0**0**1**10**1 " forms  "010 "
- \[2,4,5\] from  "00**1**1**01** " forms  "101 "
- \[3,4,5\] from  "001**101** " forms  "101 "
No other selection is valid. Thus, there are 6 total ways.

**Example 2:**

**Input:** s =  "11100 "
**Output:** 0
**Explanation:** It can be shown that there are no valid selections.

**Constraints:**

*   `3 <= s.length <= 105`
*   `s[i]` is either `'0'` or `'1'`.
Sum of Scores of Built Strings

You are **building** a string `s` of length `n` **one** character at a time, **prepending** each new character to the **front** of the string. The strings are labeled from `1` to `n`, where the string with length `i` is labeled `si`.

*   For example, for `s = "abaca "`, `s1 == "a "`, `s2 == "ca "`, `s3 == "aca "`, etc.

The **score** of `si` is the length of the **longest common prefix** between `si` and `sn` (Note that `s == sn`).

Given the final string `s`, return _the **sum** of the **score** of every_ `si`.

**Example 1:**

**Input:** s =  "babab "
**Output:** 9
**Explanation:**
For s1 ==  "b ", the longest common prefix is  "b " which has a score of 1.
For s2 ==  "ab ", there is no common prefix so the score is 0.
For s3 ==  "bab ", the longest common prefix is  "bab " which has a score of 3.
For s4 ==  "abab ", there is no common prefix so the score is 0.
For s5 ==  "babab ", the longest common prefix is  "babab " which has a score of 5.
The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.

**Example 2:**

**Input:** s =  "azbazbzaz "
**Output:** 14
**Explanation:** 
For s2 ==  "az ", the longest common prefix is  "az " which has a score of 2.
For s6 ==  "azbzaz ", the longest common prefix is  "azb " which has a score of 3.
For s9 ==  "azbazbzaz ", the longest common prefix is  "azbazbzaz " which has a score of 9.
For all other si, the score is 0.
The sum of the scores is 2 + 3 + 9 = 14, so we return 14.

**Constraints:**

*   `1 <= s.length <= 105`
*   `s` consists of lowercase English letters.
Minimum Number of Operations to Convert Time

You are given two strings `current` and `correct` representing two **24-hour times**.

24-hour times are formatted as `"HH:MM "`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`.

In one operation you can increase the time `current` by `1`, `5`, `15`, or `60` minutes. You can perform this operation **any** number of times.

Return _the **minimum number of operations** needed to convert_ `current` _to_ `correct`.

**Example 1:**

**Input:** current =  "02:30 ", correct =  "04:35 "
**Output:** 3
**Explanation:**
We can convert current to correct in 3 operations as follows:
- Add 60 minutes to current. current becomes  "03:30 ".
- Add 60 minutes to current. current becomes  "04:30 ".
- Add 5 minutes to current. current becomes  "04:35 ".
It can be proven that it is not possible to convert current to correct in fewer than 3 operations.

**Example 2:**

**Input:** current =  "11:00 ", correct =  "11:01 "
**Output:** 1
**Explanation:** We only have to add one minute to current, so the minimum number of operations needed is 1.

**Constraints:**

*   `current` and `correct` are in the format `"HH:MM "`
*   `current <= correct`
Find Players With Zero or One Losses

You are given an integer array `matches` where `matches[i] = [winneri, loseri]` indicates that the player `winneri` defeated player `loseri` in a match.

Return _a list_ `answer` _of size_ `2` _where:_

*   `answer[0]` is a list of all players that have **not** lost any matches.
*   `answer[1]` is a list of all players that have lost exactly **one** match.

The values in the two lists should be returned in **increasing** order.

**Note:**

*   You should only consider the players that have played **at least one** match.
*   The testcases will be generated such that **no** two matches will have the **same** outcome.

**Example 1:**

**Input:** matches = \[\[1,3\],\[2,3\],\[3,6\],\[5,6\],\[5,7\],\[4,5\],\[4,8\],\[4,9\],\[10,4\],\[10,9\]\]
**Output:** \[\[1,2,10\],\[4,5,7,8\]\]
**Explanation:**
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer\[0\] = \[1,2,10\] and answer\[1\] = \[4,5,7,8\].

**Example 2:**

**Input:** matches = \[\[2,3\],\[1,3\],\[5,4\],\[6,4\]\]
**Output:** \[\[1,2,5,6\],\[\]\]
**Explanation:**
Players 1, 2, 5, and 6 have not lost any matches.
Players 3 and 4 each have lost two matches.
Thus, answer\[0\] = \[1,2,5,6\] and answer\[1\] = \[\].

**Constraints:**

*   `1 <= matches.length <= 105`
*   `matches[i].length == 2`
*   `1 <= winneri, loseri <= 105`
*   `winneri != loseri`
*   All `matches[i]` are **unique**.
Maximum Candies Allocated to K Children

You are given a **0-indexed** integer array `candies`. Each element in the array denotes a pile of candies of size `candies[i]`. You can divide each pile into any number of **sub piles**, but you **cannot** merge two piles together.

You are also given an integer `k`. You should allocate piles of candies to `k` children such that each child gets the **same** number of candies. Each child can take **at most one** pile of candies and some piles of candies may go unused.

Return _the **maximum number of candies** each child can get._

**Example 1:**

**Input:** candies = \[5,8,6\], k = 3
**Output:** 5
**Explanation:** We can divide candies\[1\] into 2 piles of size 5 and 3, and candies\[2\] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.

**Example 2:**

**Input:** candies = \[2,5\], k = 11
**Output:** 0
**Explanation:** There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.

**Constraints:**

*   `1 <= candies.length <= 105`
*   `1 <= candies[i] <= 107`
*   `1 <= k <= 1012`