bowang0911/leetcode huggingface dataset:
Solving Questions With Brainpower

You are given a **0-indexed** 2D integer array `questions` where `questions[i] = [pointsi, brainpoweri]`.

The array describes the questions of an exam, where you have to process the questions **in order** (i.e., starting from question `0`) and make a decision whether to **solve** or **skip** each question. Solving question `i` will **earn** you `pointsi` points but you will be **unable** to solve each of the next `brainpoweri` questions. If you skip question `i`, you get to make the decision on the next question.

*   For example, given `questions = [[3, 2], [4, 3], [4, 4], [2, 5]]`:
    *   If question `0` is solved, you will earn `3` points but you will be unable to solve questions `1` and `2`.
    *   If instead, question `0` is skipped and question `1` is solved, you will earn `4` points but you will be unable to solve questions `2` and `3`.

Return _the **maximum** points you can earn for the exam_.

**Example 1:**

**Input:** questions = \[\[3,2\],\[4,3\],\[4,4\],\[2,5\]\]
**Output:** 5
**Explanation:** The maximum points can be earned by solving questions 0 and 3.
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
- Unable to solve questions 1 and 2
- Solve question 3: Earn 2 points
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.

**Example 2:**

**Input:** questions = \[\[1,1\],\[2,2\],\[3,3\],\[4,4\],\[5,5\]\]
**Output:** 7
**Explanation:** The maximum points can be earned by solving questions 1 and 4.
- Skip question 0
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
- Unable to solve questions 2 and 3
- Solve question 4: Earn 5 points
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.

**Constraints:**

*   `1 <= questions.length <= 105`
*   `questions[i].length == 2`
*   `1 <= pointsi, brainpoweri <= 105`
Maximum Running Time of N Computers

You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.

Initially, you can insert **at most one battery** into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery **any number of times**. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.

Note that the batteries cannot be recharged.

Return _the **maximum** number of minutes you can run all the_ `n` _computers simultaneously._

**Example 1:**

**Input:** n = 2, batteries = \[3,3,3\]
**Output:** 4
**Explanation:** 
Initially, insert battery 0 into the first computer and battery 1 into the second computer.
After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute.
At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead.
By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running.
We can run the two computers simultaneously for at most 4 minutes, so we return 4.

**Example 2:**

**Input:** n = 2, batteries = \[1,1,1,1\]
**Output:** 2
**Explanation:** 
Initially, insert battery 0 into the first computer and battery 2 into the second computer. 
After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer. 
After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.
We can run the two computers simultaneously for at most 2 minutes, so we return 2.

**Constraints:**

*   `1 <= n <= batteries.length <= 105`
*   `1 <= batteries[i] <= 109`
Choose Numbers From Two Arrays in Range

You are given two **0-indexed** integer arrays `nums1` and `nums2` of length `n`.

A range `[l, r]` (**inclusive**) where `0 <= l <= r < n` is **balanced** if:

*   For every `i` in the range `[l, r]`, you pick either `nums1[i]` or `nums2[i]`.
*   The sum of the numbers you pick from `nums1` equals to the sum of the numbers you pick from `nums2` (the sum is considered to be `0` if you pick no numbers from an array).

Two **balanced** ranges from `[l1, r1]` and `[l2, r2]` are considered to be **different** if at least one of the following is true:

*   `l1 != l2`
*   `r1 != r2`
*   `nums1[i]` is picked in the first range, and `nums2[i]` is picked in the second range or **vice versa** for at least one `i`.

Return _the number of **different** ranges that are balanced._ Since the answer may be very large, return it **modulo** `109 + 7`_._

**Example 1:**

**Input:** nums1 = \[1,2,5\], nums2 = \[2,6,3\]
**Output:** 3
**Explanation:** The balanced ranges are:
- \[0, 1\] where we choose nums2\[0\], and nums1\[1\].
  The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 2 = 2.
- \[0, 2\] where we choose nums1\[0\], nums2\[1\], and nums1\[2\].
  The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 1 + 5 = 6.
- \[0, 2\] where we choose nums1\[0\], nums1\[1\], and nums2\[2\].
  The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 1 + 2 = 3.
Note that the second and third balanced ranges are different.
In the second balanced range, we choose nums2\[1\] and in the third balanced range, we choose nums1\[1\].

**Example 2:**

**Input:** nums1 = \[0,1\], nums2 = \[1,0\]
**Output:** 4
**Explanation:** The balanced ranges are:
- \[0, 0\] where we choose nums1\[0\].
  The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 0 = 0.
- \[1, 1\] where we choose nums2\[1\].
  The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 0 = 0.
- \[0, 1\] where we choose nums1\[0\] and nums2\[1\].
  The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 0 = 0.
- \[0, 1\] where we choose nums2\[0\] and nums1\[1\].
  The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 1 = 1.

**Constraints:**

*   `n == nums1.length == nums2.length`
*   `1 <= n <= 100`
*   `0 <= nums1[i], nums2[i] <= 100`
Minimum Cost of Buying Candies With Discount

A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.

The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.

*   For example, if there are `4` candies with costs `1`, `2`, `3`, and `4`, and the customer buys candies with costs `2` and `3`, they can take the candy with cost `1` for free, but not the candy with cost `4`.

Given a **0-indexed** integer array `cost`, where `cost[i]` denotes the cost of the `ith` candy, return _the **minimum cost** of buying **all** the candies_.

**Example 1:**

**Input:** cost = \[1,2,3\]
**Output:** 5
**Explanation:** We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.
The total cost of buying all candies is 2 + 3 = 5. This is the **only** way we can buy the candies.
Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.
The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.

**Example 2:**

**Input:** cost = \[6,5,7,9,2,2\]
**Output:** 23
**Explanation:** The way in which we can get the minimum cost is described below:
- Buy candies with costs 9 and 7
- Take the candy with cost 6 for free
- We buy candies with costs 5 and 2
- Take the last remaining candy with cost 2 for free
Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.

**Example 3:**

**Input:** cost = \[5,5\]
**Output:** 10
**Explanation:** Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.
Hence, the minimum cost to buy all candies is 5 + 5 = 10.

**Constraints:**

*   `1 <= cost.length <= 100`
*   `1 <= cost[i] <= 100`
Count the Hidden Sequences

You are given a **0-indexed** array of `n` integers `differences`, which describes the **differences** between each pair of **consecutive** integers of a **hidden** sequence of length `(n + 1)`. More formally, call the hidden sequence `hidden`, then we have that `differences[i] = hidden[i + 1] - hidden[i]`.

You are further given two integers `lower` and `upper` that describe the **inclusive** range of values `[lower, upper]` that the hidden sequence can contain.

*   For example, given `differences = [1, -3, 4]`, `lower = 1`, `upper = 6`, the hidden sequence is a sequence of length `4` whose elements are in between `1` and `6` (**inclusive**).
    *   `[3, 4, 1, 5]` and `[4, 5, 2, 6]` are possible hidden sequences.
    *   `[5, 6, 3, 7]` is not possible since it contains an element greater than `6`.
    *   `[1, 2, 3, 4]` is not possible since the differences are not correct.

Return _the number of **possible** hidden sequences there are._ If there are no possible sequences, return `0`.

**Example 1:**

**Input:** differences = \[1,-3,4\], lower = 1, upper = 6
**Output:** 2
**Explanation:** The possible hidden sequences are:
- \[3, 4, 1, 5\]
- \[4, 5, 2, 6\]
Thus, we return 2.

**Example 2:**

**Input:** differences = \[3,-4,5,1,-2\], lower = -4, upper = 5
**Output:** 4
**Explanation:** The possible hidden sequences are:
- \[-3, 0, -4, 1, 2, 0\]
- \[-2, 1, -3, 2, 3, 1\]
- \[-1, 2, -2, 3, 4, 2\]
- \[0, 3, -1, 4, 5, 3\]
Thus, we return 4.

**Example 3:**

**Input:** differences = \[4,-7,2\], lower = 3, upper = 6
**Output:** 0
**Explanation:** There are no possible hidden sequences. Thus, we return 0.

**Constraints:**

*   `n == differences.length`
*   `1 <= n <= 105`
*   `-105 <= differences[i] <= 105`
*   `-105 <= lower <= upper <= 105`
Number of Ways to Divide a Long Corridor

Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.

One room divider has **already** been installed to the left of index `0`, and **another** to the right of index `n - 1`. Additional room dividers can be installed. For each position between indices `i - 1` and `i` (`1 <= i <= n - 1`), at most one divider can be installed.

Divide the corridor into non-overlapping sections, where each section has **exactly two seats** with any number of plants. There may be multiple ways to perform the division. Two ways are **different** if there is a position with a room divider installed in the first way but not in the second way.

Return _the number of ways to divide the corridor_. Since the answer may be very large, return it **modulo** `109 + 7`. If there is no way, return `0`.

**Example 1:**

**Input:** corridor =  "SSPPSPS "
**Output:** 3
**Explanation:** There are 3 different ways to divide the corridor.
The black bars in the above image indicate the two room dividers already installed.
Note that in each of the ways, **each** section has exactly **two** seats.

**Example 2:**

**Input:** corridor =  "PPSPSP "
**Output:** 1
**Explanation:** There is only 1 way to divide the corridor, by not installing any additional dividers.
Installing any would create some section that does not have exactly two seats.

**Example 3:**

**Input:** corridor =  "S "
**Output:** 0
**Explanation:** There is no way to divide the corridor because there will always be a section that does not have exactly two seats.

**Constraints:**

*   `n == corridor.length`
*   `1 <= n <= 105`
*   `corridor[i]` is either `'S'` or `'P'`.
Count Elements With Strictly Smaller and Greater Elements

Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.

**Example 1:**

**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.
Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it.
In total there are 2 elements having both a strictly smaller and a strictly greater element appear in `nums`.

**Example 2:**

**Input:** nums = \[-3,3,3,90\]
**Output:** 2
**Explanation:** The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.
Since there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in `nums`.

**Constraints:**

*   `1 <= nums.length <= 100`
*   `-105 <= nums[i] <= 105`
Rearrange Array Elements by Sign

You are given a **0-indexed** integer array `nums` of **even** length consisting of an **equal** number of positive and negative integers.

You should **rearrange** the elements of `nums` such that the modified array follows the given conditions:

1.  Every **consecutive pair** of integers have **opposite signs**.
2.  For all integers with the same sign, the **order** in which they were present in `nums` is **preserved**.
3.  The rearranged array begins with a positive integer.

Return _the modified array after rearranging the elements to satisfy the aforementioned conditions_.

**Example 1:**

**Input:** nums = \[3,1,-2,-5,2,-4\]
**Output:** \[3,-2,1,-5,2,-4\]
**Explanation:**
The positive integers in nums are \[3,1,2\]. The negative integers are \[-2,-5,-4\].
The only possible way to rearrange them such that they satisfy all conditions is \[3,-2,1,-5,2,-4\].
Other ways such as \[1,-2,2,-5,3,-4\], \[3,1,2,-2,-5,-4\], \[-2,3,-5,1,-4,2\] are incorrect because they do not satisfy one or more conditions.  

**Example 2:**

**Input:** nums = \[-1,1\]
**Output:** \[1,-1\]
**Explanation:**
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to \[1,-1\].

**Constraints:**

*   `2 <= nums.length <= 2 * 105`
*   `nums.length` is **even**
*   `1 <= |nums[i]| <= 105`
*   `nums` consists of **equal** number of positive and negative integers.
Find All Lonely Numbers in the Array

You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.

Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.

**Example 1:**

**Input:** nums = \[10,6,5,8\]
**Output:** \[10,8\]
**Explanation:** 
- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
- 5 is not a lonely number since 6 appears in nums and vice versa.
Hence, the lonely numbers in nums are \[10, 8\].
Note that \[8, 10\] may also be returned.

**Example 2:**

**Input:** nums = \[1,3,5,3\]
**Output:** \[1,5\]
**Explanation:** 
- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
- 3 is not a lonely number since it appears twice.
Hence, the lonely numbers in nums are \[1, 5\].
Note that \[5, 1\] may also be returned.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `0 <= nums[i] <= 106`
Minimum Number of Lines to Cover Points

You are given an array `points` where `points[i] = [xi, yi]` represents a point on an **X-Y** plane.

**Straight lines** are going to be added to the **X-Y** plane, such that every point is covered by at **least** one line.

Return _the **minimum** number of **straight lines** needed to cover all the points_.

**Example 1:**

**Input:** points = \[\[0,1\],\[2,3\],\[4,5\],\[4,3\]\]
**Output:** 2
**Explanation:** The minimum number of straight lines needed is two. One possible solution is to add:
- One line connecting the point at (0, 1) to the point at (4, 5).
- Another line connecting the point at (2, 3) to the point at (4, 3).

**Example 2:**

**Input:** points = \[\[0,2\],\[-2,-2\],\[1,4\]\]
**Output:** 1
**Explanation:** The minimum number of straight lines needed is one. The only solution is to add:
- One line connecting the point at (-2, -2) to the point at (1, 4).

**Constraints:**

*   `1 <= points.length <= 10`
*   `points[i].length == 2`
*   `-100 <= xi, yi <= 100`
*   All the `points` are **unique**.
Keep Multiplying Found Values by Two

You are given an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`.

You then do the following steps:

1.  If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`).
2.  Otherwise, **stop** the process.
3.  **Repeat** this process with the new number as long as you keep finding the number.

Return _the **final** value of_ `original`.

**Example 1:**

**Input:** nums = \[5,3,6,1,12\], original = 3
**Output:** 24
**Explanation:** 
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
- 24 is not found in nums. Thus, 24 is returned.

**Example 2:**

**Input:** nums = \[2,7,9\], original = 4
**Output:** 4
**Explanation:**
- 4 is not found in nums. Thus, 4 is returned.

**Constraints:**

*   `1 <= nums.length <= 1000`
*   `1 <= nums[i], original <= 1000`
All Divisions With the Highest Score of a Binary Array

You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:

*   `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of nums between index `i` and `n - 1` **(inclusive)**.
*   If `i == 0`, `numsleft` is **empty**, while `numsright` has all the elements of `nums`.
*   If `i == n`, `numsleft` has all the elements of nums, while `numsright` is **empty**.

The **division score** of an index `i` is the **sum** of the number of `0`'s in `numsleft` and the number of `1`'s in `numsright`.

Return _**all distinct indices** that have the **highest** possible **division score**_. You may return the answer in **any order**.

**Example 1:**

**Input:** nums = \[0,0,1,0\]
**Output:** \[2,4\]
**Explanation:** Division at index
- 0: numsleft is \[\]. numsright is \[0,0,**1**,0\]. The score is 0 + 1 = 1.
- 1: numsleft is \[**0**\]. numsright is \[0,**1**,0\]. The score is 1 + 1 = 2.
- 2: numsleft is \[**0**,**0**\]. numsright is \[**1**,0\]. The score is 2 + 1 = 3.
- 3: numsleft is \[**0**,**0**,1\]. numsright is \[0\]. The score is 2 + 0 = 2.
- 4: numsleft is \[**0**,**0**,1,**0**\]. numsright is \[\]. The score is 3 + 0 = 3.
Indices 2 and 4 both have the highest possible division score 3.
Note the answer \[4,2\] would also be accepted.

**Example 2:**

**Input:** nums = \[0,0,0\]
**Output:** \[3\]
**Explanation:** Division at index
- 0: numsleft is \[\]. numsright is \[0,0,0\]. The score is 0 + 0 = 0.
- 1: numsleft is \[**0**\]. numsright is \[0,0\]. The score is 1 + 0 = 1.
- 2: numsleft is \[**0**,**0**\]. numsright is \[0\]. The score is 2 + 0 = 2.
- 3: numsleft is \[**0**,**0**,**0**\]. numsright is \[\]. The score is 3 + 0 = 3.
Only index 3 has the highest possible division score 3.

**Example 3:**

**Input:** nums = \[1,1\]
**Output:** \[0\]
**Explanation:** Division at index
- 0: numsleft is \[\]. numsright is \[**1**,**1**\]. The score is 0 + 2 = 2.
- 1: numsleft is \[1\]. numsright is \[**1**\]. The score is 0 + 1 = 1.
- 2: numsleft is \[1,1\]. numsright is \[\]. The score is 0 + 0 = 0.
Only index 0 has the highest possible division score 2.

**Constraints:**

*   `n == nums.length`
*   `1 <= n <= 105`
*   `nums[i]` is either `0` or `1`.
Find Substring With Given Hash Value

The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function:

*   `hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m`.

Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`.

You are given a string `s` and the integers `power`, `modulo`, `k`, and `hashValue.` Return `sub`, _the **first** **substring** of_ `s` _of length_ `k` _such that_ `hash(sub, power, modulo) == hashValue`.

The test cases will be generated such that an answer always **exists**.

A **substring** is a contiguous non-empty sequence of characters within a string.

**Example 1:**

**Input:** s =  "leetcode ", power = 7, modulo = 20, k = 2, hashValue = 0
**Output:**  "ee "
**Explanation:** The hash of  "ee " can be computed to be hash( "ee ", 7, 20) = (5 \* 1 + 5 \* 7) mod 20 = 40 mod 20 = 0. 
 "ee " is the first substring of length 2 with hashValue 0. Hence, we return  "ee ".

**Example 2:**

**Input:** s =  "fbxzaad ", power = 31, modulo = 100, k = 3, hashValue = 32
**Output:**  "fbx "
**Explanation:** The hash of  "fbx " can be computed to be hash( "fbx ", 31, 100) = (6 \* 1 + 2 \* 31 + 24 \* 312) mod 100 = 23132 mod 100 = 32. 
The hash of  "bxz " can be computed to be hash( "bxz ", 31, 100) = (2 \* 1 + 24 \* 31 + 26 \* 312) mod 100 = 25732 mod 100 = 32. 
 "fbx " is the first substring of length 3 with hashValue 32. Hence, we return  "fbx ".
Note that  "bxz " also has a hash of 32 but it appears later than  "fbx ".

**Constraints:**

*   `1 <= k <= s.length <= 2 * 104`
*   `1 <= power, modulo <= 109`
*   `0 <= hashValue < modulo`
*   `s` consists of lowercase English letters only.
*   The test cases are generated such that an answer always **exists**.