bowang0911/leetcode huggingface dataset:
The Score of Students Solving Math Expression

You are given a string `s` that contains digits `0-9`, addition symbols `'+'`, and multiplication symbols `'*'` **only**, representing a **valid** math expression of **single digit numbers** (e.g., `3+5*2`). This expression was given to `n` elementary school students. The students were instructed to get the answer of the expression by following this **order of operations**:

1.  Compute **multiplication**, reading from **left to right**; Then,
2.  Compute **addition**, reading from **left to right**.

You are given an integer array `answers` of length `n`, which are the submitted answers of the students in no particular order. You are asked to grade the `answers`, by following these **rules**:

*   If an answer **equals** the correct answer of the expression, this student will be rewarded `5` points;
*   Otherwise, if the answer **could be interpreted** as if the student applied the operators **in the wrong order** but had **correct arithmetic**, this student will be rewarded `2` points;
*   Otherwise, this student will be rewarded `0` points.

Return _the sum of the points of the students_.

**Example 1:**

**Input:** s =  "7+3\*1\*2 ", answers = \[20,13,42\]
**Output:** 7
**Explanation:** As illustrated above, the correct answer of the expression is 13, therefore one student is rewarded 5 points: \[20,**13**,42\]
A student might have applied the operators in this wrong order: ((7+3)\*1)\*2 = 20. Therefore one student is rewarded 2 points: \[**20**,13,42\]
The points for the students are: \[2,5,0\]. The sum of the points is 2+5+0=7.

**Example 2:**

**Input:** s =  "3+5\*2 ", answers = \[13,0,10,13,13,16,16\]
**Output:** 19
**Explanation:** The correct answer of the expression is 13, therefore three students are rewarded 5 points each: \[**13**,0,10,**13**,**13**,16,16\]
A student might have applied the operators in this wrong order: ((3+5)\*2 = 16. Therefore two students are rewarded 2 points: \[13,0,10,13,13,**16**,**16**\]
The points for the students are: \[5,0,0,5,5,2,2\]. The sum of the points is 5+0+0+5+5+2+2=19.

**Example 3:**

**Input:** s =  "6+0\*1 ", answers = \[12,9,6,4,8,6\]
**Output:** 10
**Explanation:** The correct answer of the expression is 6.
If a student had incorrectly done (6+0)\*1, the answer would also be 6.
By the rules of grading, the students will still be rewarded 5 points (as they got the correct answer), not 2 points.
The points for the students are: \[0,0,5,0,0,5\]. The sum of the points is 10.

**Constraints:**

*   `3 <= s.length <= 31`
*   `s` represents a valid expression that contains only digits `0-9`, `'+'`, and `'*'` only.
*   All the integer operands in the expression are in the **inclusive** range `[0, 9]`.
*   `1 <=` The count of all operators (`'+'` and `'*'`) in the math expression `<= 15`
*   Test data are generated such that the correct answer of the expression is in the range of `[0, 1000]`.
*   `n == answers.length`
*   `1 <= n <= 104`
*   `0 <= answers[i] <= 1000`
Brightest Position on Street

A perfectly straight street is represented by a number line. The street has street lamp(s) on it and is represented by a 2D integer array `lights`. Each `lights[i] = [positioni, rangei]` indicates that there is a street lamp at position `positioni` that lights up the area from `[positioni - rangei, positioni + rangei]` (**inclusive**).

The **brightness** of a position `p` is defined as the number of street lamp that light up the position `p`.

Given `lights`, return _the **brightest** position on the_ _street. If there are multiple brightest positions, return the **smallest** one._

**Example 1:**

**Input:** lights = \[\[-3,2\],\[1,2\],\[3,3\]\]
**Output:** -1
**Explanation:**
The first street lamp lights up the area from \[(-3) - 2, (-3) + 2\] = \[-5, -1\].
The second street lamp lights up the area from \[1 - 2, 1 + 2\] = \[-1, 3\].
The third street lamp lights up the area from \[3 - 3, 3 + 3\] = \[0, 6\].

Position -1 has a brightness of 2, illuminated by the first and second street light.
Positions 0, 1, 2, and 3 have a brightness of 2, illuminated by the second and third street light.
Out of all these positions, -1 is the smallest, so return it.

**Example 2:**

**Input:** lights = \[\[1,0\],\[0,1\]\]
**Output:** 1
**Explanation:**
The first street lamp lights up the area from \[1 - 0, 1 + 0\] = \[1, 1\].
The second street lamp lights up the area from \[0 - 1, 0 + 1\] = \[-1, 1\].

Position 1 has a brightness of 2, illuminated by the first and second street light.
Return 1 because it is the brightest position on the street.

**Example 3:**

**Input:** lights = \[\[1,2\]\]
**Output:** -1
**Explanation:**
The first street lamp lights up the area from \[1 - 2, 1 + 2\] = \[-1, 3\].

Positions -1, 0, 1, 2, and 3 have a brightness of 1, illuminated by the first street light.
Out of all these positions, -1 is the smallest, so return it.

**Constraints:**

*   `1 <= lights.length <= 105`
*   `lights[i].length == 2`
*   `-108 <= positioni <= 108`
*   `0 <= rangei <= 108`
Convert 1D Array Into 2D Array

You are given a **0-indexed** 1-dimensional (1D) integer array `original`, and two integers, `m` and `n`. You are tasked with creating a 2-dimensional (2D) array with `m` rows and `n` columns using **all** the elements from `original`.

The elements from indices `0` to `n - 1` (**inclusive**) of `original` should form the first row of the constructed 2D array, the elements from indices `n` to `2 * n - 1` (**inclusive**) should form the second row of the constructed 2D array, and so on.

Return _an_ `m x n` _2D array constructed according to the above procedure, or an empty 2D array if it is impossible_.

**Example 1:**

**Input:** original = \[1,2,3,4\], m = 2, n = 2
**Output:** \[\[1,2\],\[3,4\]\]
**Explanation:** The constructed 2D array should contain 2 rows and 2 columns.
The first group of n=2 elements in original, \[1,2\], becomes the first row in the constructed 2D array.
The second group of n=2 elements in original, \[3,4\], becomes the second row in the constructed 2D array.

**Example 2:**

**Input:** original = \[1,2,3\], m = 1, n = 3
**Output:** \[\[1,2,3\]\]
**Explanation:** The constructed 2D array should contain 1 row and 3 columns.
Put all three elements in original into the first row of the constructed 2D array.

**Example 3:**

**Input:** original = \[1,2\], m = 1, n = 1
**Output:** \[\]
**Explanation:** There are 2 elements in original.
It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.

**Constraints:**

*   `1 <= original.length <= 5 * 104`
*   `1 <= original[i] <= 105`
*   `1 <= m, n <= 4 * 104`
Number of Pairs of Strings With Concatenation Equal to Target

Given an array of **digit** strings `nums` and a **digit** string `target`, return _the number of pairs of indices_ `(i, j)` _(where_ `i != j`_) such that the **concatenation** of_ `nums[i] + nums[j]` _equals_ `target`.

**Example 1:**

**Input:** nums = \[ "777 ", "7 ", "77 ", "77 "\], target =  "7777 "
**Output:** 4
**Explanation:** Valid pairs are:
- (0, 1):  "777 " +  "7 "
- (1, 0):  "7 " +  "777 "
- (2, 3):  "77 " +  "77 "
- (3, 2):  "77 " +  "77 "

**Example 2:**

**Input:** nums = \[ "123 ", "4 ", "12 ", "34 "\], target =  "1234 "
**Output:** 2
**Explanation:** Valid pairs are:
- (0, 1):  "123 " +  "4 "
- (2, 3):  "12 " +  "34 "

**Example 3:**

**Input:** nums = \[ "1 ", "1 ", "1 "\], target =  "11 "
**Output:** 6
**Explanation:** Valid pairs are:
- (0, 1):  "1 " +  "1 "
- (1, 0):  "1 " +  "1 "
- (0, 2):  "1 " +  "1 "
- (2, 0):  "1 " +  "1 "
- (1, 2):  "1 " +  "1 "
- (2, 1):  "1 " +  "1 "

**Constraints:**

*   `2 <= nums.length <= 100`
*   `1 <= nums[i].length <= 100`
*   `2 <= target.length <= 100`
*   `nums[i]` and `target` consist of digits.
*   `nums[i]` and `target` do not have leading zeros.
Maximize the Confusion of an Exam

A teacher is writing a test with `n` true/false questions, with `'T'` denoting true and `'F'` denoting false. He wants to confuse the students by **maximizing** the number of **consecutive** questions with the **same** answer (multiple trues or multiple falses in a row).

You are given a string `answerKey`, where `answerKey[i]` is the original answer to the `ith` question. In addition, you are given an integer `k`, the maximum number of times you may perform the following operation:

*   Change the answer key for any question to `'T'` or `'F'` (i.e., set `answerKey[i]` to `'T'` or `'F'`).

Return _the **maximum** number of consecutive_ `'T'`s or `'F'`s _in the answer key after performing the operation at most_ `k` _times_.

**Example 1:**

**Input:** answerKey =  "TTFF ", k = 2
**Output:** 4
**Explanation:** We can replace both the 'F's with 'T's to make answerKey =  "TTTT ".
There are four consecutive 'T's.

**Example 2:**

**Input:** answerKey =  "TFFT ", k = 1
**Output:** 3
**Explanation:** We can replace the first 'T' with an 'F' to make answerKey =  "FFFT ".
Alternatively, we can replace the second 'T' with an 'F' to make answerKey =  "TFFF ".
In both cases, there are three consecutive 'F's.

**Example 3:**

**Input:** answerKey =  "TTFTTFTT ", k = 1
**Output:** 5
**Explanation:** We can replace the first 'F' to make answerKey =  "TTTTTFTT "
Alternatively, we can replace the second 'F' to make answerKey =  "TTFTTTTT ". 
In both cases, there are five consecutive 'T's.

**Constraints:**

*   `n == answerKey.length`
*   `1 <= n <= 5 * 104`
*   `answerKey[i]` is either `'T'` or `'F'`
*   `1 <= k <= n`
Maximum Number of Ways to Partition an Array

You are given a **0-indexed** integer array `nums` of length `n`. The number of ways to **partition** `nums` is the number of `pivot` indices that satisfy both conditions:

*   `1 <= pivot < n`
*   `nums[0] + nums[1] + ... + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + ... + nums[n - 1]`

You are also given an integer `k`. You can choose to change the value of **one** element of `nums` to `k`, or to leave the array **unchanged**.

Return _the **maximum** possible number of ways to **partition**_ `nums` _to satisfy both conditions after changing **at most** one element_.

**Example 1:**

**Input:** nums = \[2,-1,2\], k = 3
**Output:** 1
**Explanation:** One optimal approach is to change nums\[0\] to k. The array becomes \[**3**,-1,2\].
There is one way to partition the array:
- For pivot = 2, we have the partition \[3,-1 | 2\]: 3 + -1 == 2.

**Example 2:**

**Input:** nums = \[0,0,0\], k = 1
**Output:** 2
**Explanation:** The optimal approach is to leave the array unchanged.
There are two ways to partition the array:
- For pivot = 1, we have the partition \[0 | 0,0\]: 0 == 0 + 0.
- For pivot = 2, we have the partition \[0,0 | 0\]: 0 + 0 == 0.

**Example 3:**

**Input:** nums = \[22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14\], k = -33
**Output:** 4
**Explanation:** One optimal approach is to change nums\[2\] to k. The array becomes \[22,4,**\-33**,-20,-15,15,-16,7,19,-10,0,-13,-14\].
There are four ways to partition the array.

**Constraints:**

*   `n == nums.length`
*   `2 <= n <= 105`
*   `-105 <= k, nums[i] <= 105`
Minimum Moves to Convert String

You are given a string `s` consisting of `n` characters which are either `'X'` or `'O'`.

A **move** is defined as selecting **three** **consecutive characters** of `s` and converting them to `'O'`. Note that if a move is applied to the character `'O'`, it will stay the **same**.

Return _the **minimum** number of moves required so that all the characters of_ `s` _are converted to_ `'O'`.

**Example 1:**

**Input:** s =  "XXX "
**Output:** 1
**Explanation:** XXX -> OOO
We select all the 3 characters and convert them in one move.

**Example 2:**

**Input:** s =  "XXOX "
**Output:** 2
**Explanation:** XXOX -> OOOX -> OOOO
We select the first 3 characters in the first move, and convert them to `'O'`.
Then we select the last 3 characters and convert them so that the final string contains all `'O'`s.

**Example 3:**

**Input:** s =  "OOOO "
**Output:** 0
**Explanation:** There are no `'X's` in `s` to convert.

**Constraints:**

*   `3 <= s.length <= 1000`
*   `s[i]` is either `'X'` or `'O'`.
Find Missing Observations

You have observations of `n + m` **6-sided** dice rolls with each face numbered from `1` to `6`. `n` of the observations went missing, and you only have the observations of `m` rolls. Fortunately, you have also calculated the **average value** of the `n + m` rolls.

You are given an integer array `rolls` of length `m` where `rolls[i]` is the value of the `ith` observation. You are also given the two integers `mean` and `n`.

Return _an array of length_ `n` _containing the missing observations such that the **average value** of the_ `n + m` _rolls is **exactly**_ `mean`. If there are multiple valid answers, return _any of them_. If no such array exists, return _an empty array_.

The **average value** of a set of `k` numbers is the sum of the numbers divided by `k`.

Note that `mean` is an integer, so the sum of the `n + m` rolls should be divisible by `n + m`.

**Example 1:**

**Input:** rolls = \[3,2,4,3\], mean = 4, n = 2
**Output:** \[6,6\]
**Explanation:** The mean of all n + m rolls is (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4.

**Example 2:**

**Input:** rolls = \[1,5,6\], mean = 3, n = 4
**Output:** \[2,3,2,2\]
**Explanation:** The mean of all n + m rolls is (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3.

**Example 3:**

**Input:** rolls = \[1,2,3,4\], mean = 6, n = 4
**Output:** \[\]
**Explanation:** It is impossible for the mean to be 6 no matter what the 4 missing rolls are.

**Constraints:**

*   `m == rolls.length`
*   `1 <= n, m <= 105`
*   `1 <= rolls[i], mean <= 6`
Stone Game IX

Alice and Bob continue their games with stones. There is a row of n stones, and each stone has an associated value. You are given an integer array `stones`, where `stones[i]` is the **value** of the `ith` stone.

Alice and Bob take turns, with **Alice** starting first. On each turn, the player may remove any stone from `stones`. The player who removes a stone **loses** if the **sum** of the values of **all removed stones** is divisible by `3`. Bob will win automatically if there are no remaining stones (even if it is Alice's turn).

Assuming both players play **optimally**, return `true` _if Alice wins and_ `false` _if Bob wins_.

**Example 1:**

**Input:** stones = \[2,1\]
**Output:** true
**Explanation:** The game will be played as follows:
- Turn 1: Alice can remove either stone.
- Turn 2: Bob removes the remaining stone. 
The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.

**Example 2:**

**Input:** stones = \[2\]
**Output:** false
**Explanation:** Alice will remove the only stone, and the sum of the values on the removed stones is 2. 
Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.

**Example 3:**

**Input:** stones = \[5,1,2,4,3\]
**Output:** false
**Explanation:** Bob will always win. One possible way for Bob to win is shown below:
- Turn 1: Alice can remove the second stone with value 1. Sum of removed stones = 1.
- Turn 2: Bob removes the fifth stone with value 3. Sum of removed stones = 1 + 3 = 4.
- Turn 3: Alices removes the fourth stone with value 4. Sum of removed stones = 1 + 3 + 4 = 8.
- Turn 4: Bob removes the third stone with value 2. Sum of removed stones = 1 + 3 + 4 + 2 = 10.
- Turn 5: Alice removes the first stone with value 5. Sum of removed stones = 1 + 3 + 4 + 2 + 5 = 15.
Alice loses the game because the sum of the removed stones (15) is divisible by 3. Bob wins the game.

**Constraints:**

*   `1 <= stones.length <= 105`
*   `1 <= stones[i] <= 104`
Smallest K-Length Subsequence With Occurrences of a Letter

You are given a string `s`, an integer `k`, a letter `letter`, and an integer `repetition`.

Return _the **lexicographically smallest** subsequence of_ `s` _of length_ `k` _that has the letter_ `letter` _appear **at least**_ `repetition` _times_. The test cases are generated so that the `letter` appears in `s` **at least** `repetition` times.

A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

A string `a` is **lexicographically smaller** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`.

**Example 1:**

**Input:** s =  "leet ", k = 3, letter =  "e ", repetition = 1
**Output:**  "eet "
**Explanation:** There are four subsequences of length 3 that have the letter 'e' appear at least 1 time:
-  "lee " (from  "**lee**t ")
-  "let " (from  "**le**e**t** ")
-  "let " (from  "**l**e**et** ")
-  "eet " (from  "l**eet** ")
The lexicographically smallest subsequence among them is  "eet ".

**Example 2:**

**Input:** s =  "leetcode ", k = 4, letter =  "e ", repetition = 2
**Output:**  "ecde "
**Explanation:**  "ecde " is the lexicographically smallest subsequence of length 4 that has the letter  "e " appear at least 2 times.

**Example 3:**

**Input:** s =  "bb ", k = 2, letter =  "b ", repetition = 2
**Output:**  "bb "
**Explanation:**  "bb " is the only subsequence of length 2 that has the letter  "b " appear at least 2 times.

**Constraints:**

*   `1 <= repetition <= k <= s.length <= 5 * 104`
*   `s` consists of lowercase English letters.
*   `letter` is a lowercase English letter, and appears in `s` at least `repetition` times.
Count Subarrays With More Ones Than Zeros

You are given a binary array `nums` containing only the integers `0` and `1`. Return _the number of **subarrays** in nums that have **more**_ `1`'_s than_ `0`_'s. Since the answer may be very large, return it **modulo**_ `109 + 7`.

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

**Example 1:**

**Input:** nums = \[0,1,1,0,1\]
**Output:** 9
**Explanation:**
The subarrays of size 1 that have more ones than zeros are: \[1\], \[1\], \[1\]
The subarrays of size 2 that have more ones than zeros are: \[1,1\]
The subarrays of size 3 that have more ones than zeros are: \[0,1,1\], \[1,1,0\], \[1,0,1\]
The subarrays of size 4 that have more ones than zeros are: \[1,1,0,1\]
The subarrays of size 5 that have more ones than zeros are: \[0,1,1,0,1\]

**Example 2:**

**Input:** nums = \[0\]
**Output:** 0
**Explanation:**
No subarrays have more ones than zeros.

**Example 3:**

**Input:** nums = \[1\]
**Output:** 1
**Explanation:**
The subarrays of size 1 that have more ones than zeros are: \[1\]

**Constraints:**

*   `1 <= nums.length <= 105`
*   `0 <= nums[i] <= 1`
Two Out of Three

Given three integer arrays `nums1`, `nums2`, and `nums3`, return _a **distinct** array containing all the values that are present in **at least two** out of the three arrays. You may return the values in **any** order_.

**Example 1:**

**Input:** nums1 = \[1,1,3,2\], nums2 = \[2,3\], nums3 = \[3\]
**Output:** \[3,2\]
**Explanation:** The values that are present in at least two arrays are:
- 3, in all three arrays.
- 2, in nums1 and nums2.

**Example 2:**

**Input:** nums1 = \[3,1\], nums2 = \[2,3\], nums3 = \[1,2\]
**Output:** \[2,3,1\]
**Explanation:** The values that are present in at least two arrays are:
- 2, in nums2 and nums3.
- 3, in nums1 and nums2.
- 1, in nums1 and nums3.

**Example 3:**

**Input:** nums1 = \[1,2,2\], nums2 = \[4,3,3\], nums3 = \[5\]
**Output:** \[\]
**Explanation:** No value is present in at least two arrays.

**Constraints:**

*   `1 <= nums1.length, nums2.length, nums3.length <= 100`
*   `1 <= nums1[i], nums2[j], nums3[k] <= 100`
Minimum Operations to Make a Uni-Value Grid

You are given a 2D integer `grid` of size `m x n` and an integer `x`. In one operation, you can **add** `x` to or **subtract** `x` from any element in the `grid`.

A **uni-value grid** is a grid where all the elements of it are equal.

Return _the **minimum** number of operations to make the grid **uni-value**_. If it is not possible, return `-1`.

**Example 1:**

**Input:** grid = \[\[2,4\],\[6,8\]\], x = 2
**Output:** 4
**Explanation:** We can make every element equal to 4 by doing the following: 
- Add x to 2 once.
- Subtract x from 6 once.
- Subtract x from 8 twice.
A total of 4 operations were used.

**Example 2:**

**Input:** grid = \[\[1,5\],\[2,3\]\], x = 1
**Output:** 5
**Explanation:** We can make every element equal to 3.

**Example 3:**

**Input:** grid = \[\[1,2\],\[3,4\]\], x = 2
**Output:** -1
**Explanation:** It is impossible to make every element equal.

**Constraints:**

*   `m == grid.length`
*   `n == grid[i].length`
*   `1 <= m, n <= 105`
*   `1 <= m * n <= 105`
*   `1 <= x, grid[i][j] <= 104`
Stock Price Fluctuation

You are given a stream of **records** about a particular stock. Each record contains a **timestamp** and the corresponding **price** of the stock at that timestamp.

Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream **correcting** the price of the previous wrong record.

Design an algorithm that:

*   **Updates** the price of the stock at a particular timestamp, **correcting** the price from any previous records at the timestamp.
*   Finds the **latest price** of the stock based on the current records. The **latest price** is the price at the latest timestamp recorded.
*   Finds the **maximum price** the stock has been based on the current records.
*   Finds the **minimum price** the stock has been based on the current records.

Implement the `StockPrice` class:

*   `StockPrice()` Initializes the object with no price records.
*   `void update(int timestamp, int price)` Updates the `price` of the stock at the given `timestamp`.
*   `int current()` Returns the **latest price** of the stock.
*   `int maximum()` Returns the **maximum price** of the stock.
*   `int minimum()` Returns the **minimum price** of the stock.

**Example 1:**

**Input**
\[ "StockPrice ",  "update ",  "update ",  "current ",  "maximum ",  "update ",  "maximum ",  "update ",  "minimum "\]
\[\[\], \[1, 10\], \[2, 5\], \[\], \[\], \[1, 3\], \[\], \[4, 2\], \[\]\]
**Output**
\[null, null, null, 5, 10, null, 5, null, 2\]

**Explanation**
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are \[1\] with corresponding prices \[10\].
stockPrice.update(2, 5);  // Timestamps are \[1,2\] with corresponding prices \[10,5\].
stockPrice.current();     // return 5, the latest timestamp is 2 with the price being 5.
stockPrice.maximum();     // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3);  // The previous timestamp 1 had the wrong price, so it is updated to 3.
                          // Timestamps are \[1,2\] with corresponding prices \[3,5\].
stockPrice.maximum();     // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2);  // Timestamps are \[1,2,4\] with corresponding prices \[3,5,2\].
stockPrice.minimum();     // return 2, the minimum price is 2 at timestamp 4.

**Constraints:**

*   `1 <= timestamp, price <= 109`
*   At most `105` calls will be made **in total** to `update`, `current`, `maximum`, and `minimum`.
*   `current`, `maximum`, and `minimum` will be called **only after** `update` has been called **at least once**.