# Length of Longest Subarray With at Most K Frequency

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

The **frequency** of an element `x` is the number of times it occurs in an array.

An array is called **good** if the frequency of each element in this array is **less than or equal** to `k`.

Return *the length of the **longest** **good** subarray of* `nums`*.*

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

**Example 1:**

```

**Input:** nums = [1,2,3,1,2,3,1,2], k = 2
**Output:** 6
**Explanation:** The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
It can be shown that there are no good subarrays with length more than 6.

```

**Example 2:**

```

**Input:** nums = [1,2,1,2,1,2,1,2], k = 1
**Output:** 2
**Explanation:** The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
It can be shown that there are no good subarrays with length more than 2.

```

**Example 3:**

```

**Input:** nums = [5,5,5,5,5,5,5], k = 4
**Output:** 4
**Explanation:** The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
It can be shown that there are no good subarrays with length more than 4.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maxSubarrayLength(self, nums: List[int], k: int) -> int:
```

my_solution = Solution()

assert my_solution.maxSubarrayLength(**[[1, 2, 3, 1, 2, 3, 1, 2], 2]) == 6
assert my_solution.maxSubarrayLength(**[[1, 2, 1, 2, 1, 2, 1, 2], 1]) == 2
assert my_solution.maxSubarrayLength(**[[5, 5, 5, 5, 5, 5, 5], 4]) == 4
assert my_solution.maxSubarrayLength(**[[1], 1]) == 1
assert my_solution.maxSubarrayLength(**[[2], 1]) == 1
assert my_solution.maxSubarrayLength(**[[3], 1]) == 1
assert my_solution.maxSubarrayLength(**[[4], 1]) == 1
assert my_solution.maxSubarrayLength(**[[5], 1]) == 1
assert my_solution.maxSubarrayLength(**[[6], 1]) == 1
assert my_solution.maxSubarrayLength(**[[7], 1]) == 1
assert my_solution.maxSubarrayLength(**[[8], 1]) == 1
assert my_solution.maxSubarrayLength(**[[9], 1]) == 1
assert my_solution.maxSubarrayLength(**[[10], 1]) == 1
assert my_solution.maxSubarrayLength(**[[1, 11], 2]) == 2
assert my_solution.maxSubarrayLength(**[[2, 11], 1]) == 2
assert my_solution.maxSubarrayLength(**[[3, 5], 2]) == 2
assert my_solution.maxSubarrayLength(**[[4, 6], 2]) == 2
assert my_solution.maxSubarrayLength(**[[5, 8], 2]) == 2
assert my_solution.maxSubarrayLength(**[[6, 7], 1]) == 2
assert my_solution.maxSubarrayLength(**[[7, 9], 2]) == 2


# Find the Peaks

You are given a **0-indexed** array `mountain`. Your task is to find all the **peaks** in the `mountain` array.

Return *an array that consists of* indices *of **peaks** in the given array in **any order**.*

**Notes:**

* A **peak** is defined as an element that is **strictly greater** than its neighboring elements.
* The first and last elements of the array are **not** a peak.

**Example 1:**

```

**Input:** mountain = [2,4,4]
**Output:** []
**Explanation:** mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array.
mountain[1] also can not be a peak because it is not strictly greater than mountain[2].
So the answer is [].

```

**Example 2:**

```

**Input:** mountain = [1,4,3,8,5]
**Output:** [1,3]
**Explanation:** mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array.
mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1].
But mountain [1] and mountain[3] are strictly greater than their neighboring elements.
So the answer is [1,3].

```

**Constraints:**

* `3 <= mountain.length <= 100`
* `1 <= mountain[i] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findPeaks(self, mountain: List[int]) -> List[int]:
```

my_solution = Solution()

assert my_solution.findPeaks(**[[2, 4, 4]]) == []
assert my_solution.findPeaks(**[[1, 4, 3, 8, 5]]) == [1, 3]
assert my_solution.findPeaks(**[[1, 1, 1]]) == []
assert my_solution.findPeaks(**[[1, 1, 3]]) == []
assert my_solution.findPeaks(**[[1, 1, 5]]) == []
assert my_solution.findPeaks(**[[1, 2, 5]]) == []
assert my_solution.findPeaks(**[[1, 4, 1]]) == [1]
assert my_solution.findPeaks(**[[1, 4, 3]]) == [1]
assert my_solution.findPeaks(**[[1, 5, 5]]) == []
assert my_solution.findPeaks(**[[1, 6, 4]]) == [1]
assert my_solution.findPeaks(**[[2, 1, 1]]) == []
assert my_solution.findPeaks(**[[2, 1, 2]]) == []
assert my_solution.findPeaks(**[[2, 2, 3]]) == []
assert my_solution.findPeaks(**[[2, 2, 5]]) == []
assert my_solution.findPeaks(**[[2, 3, 2]]) == [1]
assert my_solution.findPeaks(**[[2, 3, 6]]) == []
assert my_solution.findPeaks(**[[2, 4, 3]]) == [1]
assert my_solution.findPeaks(**[[2, 4, 5]]) == []
assert my_solution.findPeaks(**[[2, 6, 4]]) == [1]
assert my_solution.findPeaks(**[[3, 3, 3]]) == []


# Minimum Number of Coins to be Added

You are given a **0-indexed** integer array `coins`, representing the values of the coins available, and an integer `target`.

An integer `x` is **obtainable** if there exists a subsequence of `coins` that sums to `x`.

Return *the **minimum** number of coins **of any value** that need to be added to the array so that every integer in the range* `[1, target]` *is **obtainable***.

A **subsequence** of an array is a new **non-empty** array that is formed from the original array by deleting some (**possibly none**) of the elements without disturbing the relative positions of the remaining elements.

**Example 1:**

```

**Input:** coins = [1,4,10], target = 19
**Output:** 2
**Explanation:** We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array.

```

**Example 2:**

```

**Input:** coins = [1,4,10,5,7,19], target = 19
**Output:** 1
**Explanation:** We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array.

```

**Example 3:**

```

**Input:** coins = [1,1,1], target = 20
**Output:** 3
**Explanation:** We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16].
It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.

```

**Constraints:**

* `1 <= target <= 105`
* `1 <= coins.length <= 105`
* `1 <= coins[i] <= target`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumAddedCoins(self, coins: List[int], target: int) -> int:
```

my_solution = Solution()

assert my_solution.minimumAddedCoins(**[[1, 4, 10], 19]) == 2
assert my_solution.minimumAddedCoins(**[[1, 4, 10, 5, 7, 19], 19]) == 1
assert my_solution.minimumAddedCoins(**[[1, 1, 1], 20]) == 3
assert my_solution.minimumAddedCoins(**[[1], 100000]) == 16
assert my_solution.minimumAddedCoins(**[[100000], 100000]) == 17
assert my_solution.minimumAddedCoins(**[[2], 5]) == 2
assert my_solution.minimumAddedCoins(**[[5, 6, 7], 10]) == 3
assert my_solution.minimumAddedCoins(**[[5, 6, 7], 15]) == 3
assert my_solution.minimumAddedCoins(**[[4, 11, 13, 15, 7, 5, 12, 11, 5, 9], 34]) == 2
assert my_solution.minimumAddedCoins(**[[8, 12, 9], 27]) == 3
assert my_solution.minimumAddedCoins(**[[2, 13, 7, 1, 11], 35]) == 1
assert my_solution.minimumAddedCoins(**[[10, 3, 5, 11, 6], 27]) == 2
assert my_solution.minimumAddedCoins(**[[6, 6, 6, 15, 4], 31]) == 2
assert my_solution.minimumAddedCoins(**[[6, 15, 6], 22]) == 3
assert my_solution.minimumAddedCoins(**[[8, 14, 15, 4, 14, 15, 8, 10, 8], 42]) == 2
assert my_solution.minimumAddedCoins(**[[9, 14, 14, 9, 14, 5, 12, 10, 11], 17]) == 3
assert my_solution.minimumAddedCoins(**[[14, 5, 13, 3, 7, 10, 10, 10], 32]) == 2
assert my_solution.minimumAddedCoins(**[[8, 6, 7, 12], 26]) == 3
assert my_solution.minimumAddedCoins(**[[15, 1, 12], 43]) == 4
assert my_solution.minimumAddedCoins(**[[4, 1, 4, 10], 16]) == 1


# Count Complete Substrings

You are given a string `word` and an integer `k`.

A substring `s` of `word` is **complete** if:

* Each character in `s` occurs **exactly** `k` times.
* The difference between two adjacent characters is **at most** `2`. That is, for any two adjacent characters `c1` and `c2` in `s`, the absolute difference in their positions in the alphabet is **at most** `2`.

Return *the number of **complete** substrings of* `word`.

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

**Example 1:**

```

**Input:** word = "igigee", k = 2
**Output:** 3
**Explanation:** The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: **igig**ee, igig**ee**, **igigee**.

```

**Example 2:**

```

**Input:** word = "aaabbbccc", k = 3
**Output:** 6
**Explanation:** The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: **aaa**bbbccc, aaa**bbb**ccc, aaabbb**ccc**, **aaabbb**ccc, aaa**bbbccc**, **aaabbbccc**.

```

**Constraints:**

* `1 <= word.length <= 105`
* `word` consists only of lowercase English letters.
* `1 <= k <= word.length`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def countCompleteSubstrings(self, word: str, k: int) -> int:
```

my_solution = Solution()

assert my_solution.countCompleteSubstrings(**['igigee', 2]) == 3
assert my_solution.countCompleteSubstrings(**['aaabbbccc', 3]) == 6
assert my_solution.countCompleteSubstrings(**['a', 1]) == 1
assert my_solution.countCompleteSubstrings(**['b', 1]) == 1
assert my_solution.countCompleteSubstrings(**['c', 1]) == 1
assert my_solution.countCompleteSubstrings(**['aa', 2]) == 1
assert my_solution.countCompleteSubstrings(**['ab', 2]) == 0
assert my_solution.countCompleteSubstrings(**['ac', 2]) == 0
assert my_solution.countCompleteSubstrings(**['ba', 1]) == 3
assert my_solution.countCompleteSubstrings(**['bb', 2]) == 1
assert my_solution.countCompleteSubstrings(**['bc', 1]) == 3
assert my_solution.countCompleteSubstrings(**['ca', 1]) == 3
assert my_solution.countCompleteSubstrings(**['cb', 1]) == 3
assert my_solution.countCompleteSubstrings(**['cc', 2]) == 1
assert my_solution.countCompleteSubstrings(**['aaa', 1]) == 3
assert my_solution.countCompleteSubstrings(**['aab', 3]) == 0
assert my_solution.countCompleteSubstrings(**['aac', 2]) == 1
assert my_solution.countCompleteSubstrings(**['aba', 3]) == 0
assert my_solution.countCompleteSubstrings(**['abb', 1]) == 4
assert my_solution.countCompleteSubstrings(**['abc', 3]) == 0


# Count the Number of Infection Sequences

You are given an integer `n` and a **0-indexed**integer array `sick` which is **sorted** in **increasing** order.

There are `n` children standing in a queue with positions `0` to `n - 1` assigned to them. The array `sick` contains the positions of the children who are infected with an infectious disease. An infected child at position `i` can spread the disease to either of its immediate neighboring children at positions `i - 1` and `i + 1` **if** they exist and are currently not infected. **At most one** child who was previously not infected can get infected with the disease in one second.

It can be shown that after a finite number of seconds, all the children in the queue will get infected with the disease. An **infection sequence** is the sequential order of positions in which **all** of the non-infected children get infected with the disease. Return *the total number of possible infection sequences*.

Since the answer may be large, return it modulo `109 + 7`.

**Note** that an infection sequence **does not** contain positions of children who were already infected with the disease in the beginning.

**Example 1:**

```

**Input:** n = 5, sick = [0,4]
**Output:** 4
**Explanation:** Children at positions 1, 2, and 3 are not infected in the beginning. There are 4 possible infection sequences:
- The children at positions 1 and 3 can get infected since their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 2 gets infected.
Finally, the child at position 3 gets infected because it is adjacent to children at positions 2 and 4 who are infected. The infection sequence is [1,2,3].
- The children at positions 1 and 3 can get infected because their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 3 gets infected.
Finally, the child at position 2 gets infected because it is adjacent to children at positions 1 and 3 who are infected. The infection sequence is [1,3,2].
- The infection sequence is [3,1,2]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].
- The infection sequence is [3,2,1]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].

```

**Example 2:**

```

**Input:** n = 4, sick = [1]
**Output:** 3
**Explanation:** Children at positions 0, 2, and 3 are not infected in the beginning. There are 3 possible infection sequences:
- The infection sequence is [0,2,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,0,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,3,0]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].

```

**Constraints:**

* `2 <= n <= 105`
* `1 <= sick.length <= n - 1`
* `0 <= sick[i] <= n - 1`
* `sick` is sorted in increasing order.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def numberOfSequence(self, n: int, sick: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.numberOfSequence(**[5, [0, 4]]) == 4
assert my_solution.numberOfSequence(**[4, [1]]) == 3
assert my_solution.numberOfSequence(**[2, [0]]) == 1
assert my_solution.numberOfSequence(**[5, [0]]) == 1
assert my_solution.numberOfSequence(**[100, [0]]) == 1
assert my_solution.numberOfSequence(**[2, [1]]) == 1
assert my_solution.numberOfSequence(**[5, [1]]) == 4
assert my_solution.numberOfSequence(**[5, [2]]) == 6
assert my_solution.numberOfSequence(**[5, [3]]) == 4
assert my_solution.numberOfSequence(**[5, [4]]) == 1
assert my_solution.numberOfSequence(**[5, [0, 1]]) == 1
assert my_solution.numberOfSequence(**[3, [0, 2]]) == 1
assert my_solution.numberOfSequence(**[5, [0, 2]]) == 3
assert my_solution.numberOfSequence(**[5, [0, 3]]) == 6
assert my_solution.numberOfSequence(**[5, [1, 2]]) == 3
assert my_solution.numberOfSequence(**[5, [1, 3]]) == 6
assert my_solution.numberOfSequence(**[5, [1, 4]]) == 6
assert my_solution.numberOfSequence(**[5, [2, 3]]) == 3
assert my_solution.numberOfSequence(**[5, [2, 4]]) == 3
assert my_solution.numberOfSequence(**[5, [3, 4]]) == 1


# Count Beautiful Substrings I

You are given a string `s` and a positive integer `k`.

Let `vowels` and `consonants` be the number of vowels and consonants in a string.

A string is **beautiful** if:

* `vowels == consonants`.
* `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowels` and `consonants` is divisible by `k`.

Return *the number of **non-empty beautiful substrings** in the given string* `s`.

A **substring** is a contiguous sequence of characters in a string.

**Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

**Consonant letters** in English are every letter except vowels.

**Example 1:**

```

**Input:** s = "baeyh", k = 2
**Output:** 2
**Explanation:** There are 2 beautiful substrings in the given string.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
It can be shown that there are only 2 beautiful substrings in the given string.

```

**Example 2:**

```

**Input:** s = "abba", k = 1
**Output:** 3
**Explanation:** There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
It can be shown that there are only 3 beautiful substrings in the given string.

```

**Example 3:**

```

**Input:** s = "bcdf", k = 1
**Output:** 0
**Explanation:** There are no beautiful substrings in the given string.

```

**Constraints:**

* `1 <= s.length <= 1000`
* `1 <= k <= 1000`
* `s` consists of only English lowercase letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def beautifulSubstrings(self, s: str, k: int) -> int:
```

my_solution = Solution()

assert my_solution.beautifulSubstrings(**['baeyh', 2]) == 2
assert my_solution.beautifulSubstrings(**['abba', 1]) == 3
assert my_solution.beautifulSubstrings(**['bcdf', 1]) == 0
assert my_solution.beautifulSubstrings(**['ihroyeeb', 5]) == 0
assert my_solution.beautifulSubstrings(**['uzuxpzou', 3]) == 1
assert my_solution.beautifulSubstrings(**['ouuoeqd', 2]) == 1
assert my_solution.beautifulSubstrings(**['eeebjoxxujuaeoqibd', 8]) == 4
assert my_solution.beautifulSubstrings(**['ilougekqlovegioemdvu', 4]) == 21
assert my_solution.beautifulSubstrings(**['tqaewreikaztwpfwnef', 8]) == 3
assert my_solution.beautifulSubstrings(**['oykiuhsafgfjumnzb', 7]) == 0
assert my_solution.beautifulSubstrings(**['ifvsa', 3]) == 0
assert my_solution.beautifulSubstrings(**['svzauyuevujektj', 5]) == 3
assert my_solution.beautifulSubstrings(**['urahjig', 2]) == 2
assert my_solution.beautifulSubstrings(**['ime', 2]) == 0
assert my_solution.beautifulSubstrings(**['oacghieut', 5]) == 0
assert my_solution.beautifulSubstrings(**['aoluu', 3]) == 0
assert my_solution.beautifulSubstrings(**['ioaoiciiuoziout', 1]) == 5
assert my_solution.beautifulSubstrings(**['ouafupsuhid', 6]) == 0
assert my_solution.beautifulSubstrings(**['ox', 2]) == 0
assert my_solution.beautifulSubstrings(**['tlaiwoauazutusiaaui', 10]) == 0


# Make Lexicographically Smallest Array by Swapping Elements

You are given a **0-indexed** array of **positive** integers `nums` and a **positive** integer `limit`.

In one operation, you can choose any two indices `i` and `j` and swap `nums[i]` and `nums[j]` **if** `|nums[i] - nums[j]| <= limit`.

Return *the **lexicographically smallest array** that can be obtained by performing the operation any number of times*.

An array `a` is lexicographically smaller than an array `b` if in the first position where `a` and `b` differ, array `a` has an element that is less than the corresponding element in `b`. For example, the array `[2,10,3]` is lexicographically smaller than the array `[10,2,3]` because they differ at index `0` and `2 < 10`.

**Example 1:**

```

**Input:** nums = [1,5,3,9,8], limit = 2
**Output:** [1,3,5,8,9]
**Explanation:** Apply the operation 2 times:
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
We cannot obtain a lexicographically smaller array by applying any more operations.
Note that it may be possible to get the same result by doing different operations.

```

**Example 2:**

```

**Input:** nums = [1,7,6,18,2,1], limit = 3
**Output:** [1,6,7,18,1,2]
**Explanation:** Apply the operation 3 times:
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
We cannot obtain a lexicographically smaller array by applying any more operations.

```

**Example 3:**

```

**Input:** nums = [1,7,28,19,10], limit = 3
**Output:** [1,7,28,19,10]
**Explanation:** [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]:
```


# Count Beautiful Substrings II

You are given a string `s` and a positive integer `k`.

Let `vowels` and `consonants` be the number of vowels and consonants in a string.

A string is **beautiful** if:

* `vowels == consonants`.
* `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowels` and `consonants` is divisible by `k`.

Return *the number of **non-empty beautiful substrings** in the given string* `s`.

A **substring** is a contiguous sequence of characters in a string.

**Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

**Consonant letters** in English are every letter except vowels.

**Example 1:**

```

**Input:** s = "baeyh", k = 2
**Output:** 2
**Explanation:** There are 2 beautiful substrings in the given string.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
It can be shown that there are only 2 beautiful substrings in the given string.

```

**Example 2:**

```

**Input:** s = "abba", k = 1
**Output:** 3
**Explanation:** There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
It can be shown that there are only 3 beautiful substrings in the given string.

```

**Example 3:**

```

**Input:** s = "bcdf", k = 1
**Output:** 0
**Explanation:** There are no beautiful substrings in the given string.

```

**Constraints:**

* `1 <= s.length <= 5 * 104`
* `1 <= k <= 1000`
* `s` consists of only English lowercase letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def beautifulSubstrings(self, s: str, k: int) -> int:
```


# Find Words Containing Character

You are given a **0-indexed** array of strings `words` and a character `x`.

Return *an **array of indices** representing the words that contain the character* `x`.

**Note** that the returned array may be in **any** order.

**Example 1:**

```

**Input:** words = ["leet","code"], x = "e"
**Output:** [0,1]
**Explanation:** "e" occurs in both words: "l**ee**t", and "cod**e**". Hence, we return indices 0 and 1.

```

**Example 2:**

```

**Input:** words = ["abc","bcd","aaaa","cbc"], x = "a"
**Output:** [0,2]
**Explanation:** "a" occurs in "**a**bc", and "**aaaa**". Hence, we return indices 0 and 2.

```

**Example 3:**

```

**Input:** words = ["abc","bcd","aaaa","cbc"], x = "z"
**Output:** []
**Explanation:** "z" does not occur in any of the words. Hence, we return an empty array.

```

**Constraints:**

* `1 <= words.length <= 50`
* `1 <= words[i].length <= 50`
* `x` is a lowercase English letter.
* `words[i]` consists only of lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findWordsContaining(self, words: List[str], x: str) -> List[int]:
```

my_solution = Solution()

assert my_solution.findWordsContaining(**[['leet', 'code'], 'e']) == [0, 1]
assert my_solution.findWordsContaining(**[['abc', 'bcd', 'aaaa', 'cbc'], 'a']) == [0, 2]
assert my_solution.findWordsContaining(**[['abc', 'bcd', 'aaaa', 'cbc'], 'z']) == []
assert my_solution.findWordsContaining(**[['sgtkshnss', 'm', 'ryvbkyvuz', 'ezittyjwgb', 'wudlwg'], 'x']) == []
assert my_solution.findWordsContaining(**[['lkwnhpbj', 'tlohm', 'juazsb', 'f', 'rq'], 'v']) == []
assert my_solution.findWordsContaining(**[['aaa', 'imvtfjmxr', 'wbzfoovjnf', 'hqwrwmi'], 'c']) == []
assert my_solution.findWordsContaining(**[['utyeachht', 'bgpkcs', 'skeecqvvvw', 'nccrd'], 'i']) == []
assert my_solution.findWordsContaining(**[['alcpxexztg', 'r'], 'h']) == []
assert my_solution.findWordsContaining(**[['ekcpg', 'pdknua', 'fot', 'janppw', 'ofomkfvx'], 'g']) == [0]
assert my_solution.findWordsContaining(**[['dq', 'rlvopu'], 'd']) == [0]
assert my_solution.findWordsContaining(**[['wzppkd', 'jxvk', 'zaztizmwuv', 'hvcdtobr'], 'b']) == [3]
assert my_solution.findWordsContaining(**[['y', 'hs', 'qznrkpi'], 'v']) == []
assert my_solution.findWordsContaining(**[['pze', 'yojczsb', 'mjvyr', 'i', 'xsygks'], 'q']) == []
assert my_solution.findWordsContaining(**[['qsgtjagcu', 'm'], 'e']) == []
assert my_solution.findWordsContaining(**[['kidtwmw', 'ogh', 'trdedlh', 'wwbtlindg', 'naoylytpof', 'ujcbzwzkm', 'doamcoxdv'], 'o']) == [1, 4, 6]
assert my_solution.findWordsContaining(**[['tsmeupctki'], 't']) == [0]
assert my_solution.findWordsContaining(**[['dqxlbljmpf', 'uvdzfoiqg', 'jsnbnx', 'fbedae', 'nodewb', 'o', 'ivepktj'], 'g']) == [1]
assert my_solution.findWordsContaining(**[['fjlmmecm', 'sautsoorhl', 'n', 'hsyco', 'amlukrpjpv', 'rmhdnj', 'g'], 'e']) == [0]
assert my_solution.findWordsContaining(**[['khjchmeciv', 'vgx', 'xghr', 'bbufgegu', 'qyfxu'], 'r']) == [2]
assert my_solution.findWordsContaining(**[['jhtcugtcpl', 'bvhlgmmla', 'ntfkwzite', 'imbtzafaj', 'sdl', 't'], 'm']) == [1, 3]


# Minimum Number of Coins for Fruits

You are at a fruit market with different types of exotic fruits on display.

You are given a **1-indexed** array `prices`, where `prices[i]` denotes the number of coins needed to purchase the `ith` fruit.

The fruit market has the following offer:

* If you purchase the `ith` fruit at `prices[i]` coins, you can get the next `i` fruits for free.

**Note** that even if you **can** take fruit `j` for free, you can still purchase it for `prices[j]` coins to receive a new offer.

Return *the **minimum** number of coins needed to acquire all the fruits*.

**Example 1:**

```

**Input:** prices = [3,1,2]
**Output:** 4
**Explanation:** You can acquire the fruits as follows:
- Purchase the 1st fruit with 3 coins, you are allowed to take the 2nd fruit for free.
- Purchase the 2nd fruit with 1 coin, you are allowed to take the 3rd fruit for free.
- Take the 3rd fruit for free.
Note that even though you were allowed to take the 2nd fruit for free, you purchased it because it is more optimal.
It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.

```

**Example 2:**

```

**Input:** prices = [1,10,1,1]
**Output:** 2
**Explanation:** You can acquire the fruits as follows:
- Purchase the 1st fruit with 1 coin, you are allowed to take the 2nd fruit for free.
- Take the 2nd fruit for free.
- Purchase the 3rd fruit for 1 coin, you are allowed to take the 4th fruit for free.
- Take the 4th fruit for free.
It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.

```

**Constraints:**

* `1 <= prices.length <= 1000`
* `1 <= prices[i] <= 105`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumCoins(self, prices: List[int]) -> int:
```


# Find Maximum Non-decreasing Array Length

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

You can perform any number of operations, where each operation involves selecting a **subarray** of the array and replacing it with the **sum** of its elements. For example, if the given array is `[1,3,5,6]` and you select subarray `[3,5]` the array will convert to `[1,8,6]`.

Return *the* ***maximum*** *length of a* ***non-decreasing*** *array that can be made after applying operations.*

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

**Example 1:**

```

**Input:** nums = [5,2,2]
**Output:** 1
**Explanation:** This array with length 3 is not non-decreasing.
We have two ways to make the array length two.
First, choosing subarray [2,2] converts the array to [5,4].
Second, choosing subarray [5,2] converts the array to [7,2].
In these two ways the array is not non-decreasing.
And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing.
So the answer is 1.

```

**Example 2:**

```

**Input:** nums = [1,2,3,4]
**Output:** 4
**Explanation:** The array is non-decreasing. So the answer is 4.

```

**Example 3:**

```

**Input:** nums = [4,3,2,6]
**Output:** 3
**Explanation:** Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing.
Because the given array is not non-decreasing, the maximum possible answer is 3.
```

**Constraints:**

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

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findMaximumLength(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.findMaximumLength(**[[5, 2, 2]]) == 1
assert my_solution.findMaximumLength(**[[1, 2, 3, 4]]) == 4
assert my_solution.findMaximumLength(**[[4, 3, 2, 6]]) == 3
assert my_solution.findMaximumLength(**[[32]]) == 1
assert my_solution.findMaximumLength(**[[38]]) == 1
assert my_solution.findMaximumLength(**[[60]]) == 1
assert my_solution.findMaximumLength(**[[79]]) == 1
assert my_solution.findMaximumLength(**[[85]]) == 1
assert my_solution.findMaximumLength(**[[170]]) == 1
assert my_solution.findMaximumLength(**[[198]]) == 1
assert my_solution.findMaximumLength(**[[220]]) == 1
assert my_solution.findMaximumLength(**[[318]]) == 1
assert my_solution.findMaximumLength(**[[350]]) == 1
assert my_solution.findMaximumLength(**[[381]]) == 1
assert my_solution.findMaximumLength(**[[413]]) == 1
assert my_solution.findMaximumLength(**[[426]]) == 1
assert my_solution.findMaximumLength(**[[429]]) == 1
assert my_solution.findMaximumLength(**[[431]]) == 1
assert my_solution.findMaximumLength(**[[445]]) == 1
assert my_solution.findMaximumLength(**[[488]]) == 1


# Make Three Strings Equal

You are given three strings `s1`, `s2`, and `s3`. You have to perform the following operation on these three strings **as many times** as you want.

In one operation you can choose one of these three strings such that its length is at least `2` and delete the **rightmost** character of it.

Return *the **minimum** number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return* `-1`*.*

**Example 1:**

```

**Input:** s1 = "abc", s2 = "abb", s3 = "ab"
**Output:** 2
**Explanation:** Performing operations on s1 and s2 once will lead to three equal strings.
It can be shown that there is no way to make them equal with less than two operations.
```

**Example 2:**

```

**Input:** s1 = "dac", s2 = "bac", s3 = "cac"
**Output:** -1
**Explanation:** Because the leftmost letters of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1.

```

**Constraints:**

* `1 <= s1.length, s2.length, s3.length <= 100`
* `s1`, `s2` and `s3` consist only of lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
```

my_solution = Solution()

assert my_solution.findMinimumOperations(**['abc', 'abb', 'ab']) == 2
assert my_solution.findMinimumOperations(**['dac', 'bac', 'cac']) == -1
assert my_solution.findMinimumOperations(**['a', 'a', 'a']) == 0
assert my_solution.findMinimumOperations(**['kui', 'm', 'v']) == -1
assert my_solution.findMinimumOperations(**['a', 'aabc', 'a']) == 3
assert my_solution.findMinimumOperations(**['cc', 'cccb', 'c']) == 4
assert my_solution.findMinimumOperations(**['luso', 'lu', 'lu']) == 2
assert my_solution.findMinimumOperations(**['xx', 'phe', 'xie']) == -1
assert my_solution.findMinimumOperations(**['gzd', 'bcju', 'db']) == -1
assert my_solution.findMinimumOperations(**['cbba', 'cbaa', 'c']) == 6
assert my_solution.findMinimumOperations(**['k', 'kfb', 'krcnf']) == 6
assert my_solution.findMinimumOperations(**['oby', 'obz', 'obf']) == 3
assert my_solution.findMinimumOperations(**['b', 'aba', 'aaccaa']) == -1
assert my_solution.findMinimumOperations(**['a', 'accabb', 'aaa']) == 7
assert my_solution.findMinimumOperations(**['b', 'bccaaba', 'ba']) == 7
assert my_solution.findMinimumOperations(**['b', 'bacccab', 'cc']) == -1
assert my_solution.findMinimumOperations(**['ca', 'cccabb', 'cb']) == 7
assert my_solution.findMinimumOperations(**['ccb', 'ccba', 'ccb']) == 1
assert my_solution.findMinimumOperations(**['mbooi', 'pdq', 'br']) == -1
assert my_solution.findMinimumOperations(**['xxfzj', 'faho', 'c']) == -1


# Separate Black and White Balls

There are `n` balls on a table, each ball has a color black or white.

You are given a **0-indexed** binary string `s` of length `n`, where `1` and `0` represent black and white balls, respectively.

In each step, you can choose two adjacent balls and swap them.

Return *the **minimum** number of steps to group all the black balls to the right and all the white balls to the left*.

**Example 1:**

```

**Input:** s = "101"
**Output:** 1
**Explanation:** We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.
```

**Example 2:**

```

**Input:** s = "100"
**Output:** 2
**Explanation:** We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.

```

**Example 3:**

```

**Input:** s = "0111"
**Output:** 0
**Explanation:** All the black balls are already grouped to the right.

```

**Constraints:**

* `1 <= n == s.length <= 105`
* `s[i]` is either `'0'` or `'1'`.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumSteps(self, s: str) -> int:
```

my_solution = Solution()

assert my_solution.minimumSteps(**['101']) == 1
assert my_solution.minimumSteps(**['100']) == 2
assert my_solution.minimumSteps(**['0111']) == 0
assert my_solution.minimumSteps(**['11000111']) == 6
assert my_solution.minimumSteps(**['01010001']) == 7
assert my_solution.minimumSteps(**['0100101']) == 4
assert my_solution.minimumSteps(**['111111111100100010']) == 65
assert my_solution.minimumSteps(**['10100000110010011010']) == 44
assert my_solution.minimumSteps(**['1101110000111011110']) == 42
assert my_solution.minimumSteps(**['01000010111010001']) == 29
assert my_solution.minimumSteps(**['11110']) == 4
assert my_solution.minimumSteps(**['010001001011010']) == 21
assert my_solution.minimumSteps(**['0011011']) == 2
assert my_solution.minimumSteps(**['001']) == 0
assert my_solution.minimumSteps(**['000100100']) == 6
assert my_solution.minimumSteps(**['00110']) == 2
assert my_solution.minimumSteps(**['001110001110001']) == 27
assert my_solution.minimumSteps(**['10000000001']) == 9
assert my_solution.minimumSteps(**['01']) == 0
assert my_solution.minimumSteps(**['0100011100001100100']) == 45


# Maximum Xor Product

Given three integers `a`, `b`, and `n`, return *the **maximum value** of* `(a XOR x) * (b XOR x)` *where* `0 <= x < 2n`.

Since the answer may be too large, return it **modulo** `109 + 7`.

**Note** that `XOR` is the bitwise XOR operation.

**Example 1:**

```

**Input:** a = 12, b = 5, n = 4
**Output:** 98
**Explanation:** For x = 2, (a XOR x) = 14 and (b XOR x) = 7. Hence, (a XOR x) * (b XOR x) = 98.
It can be shown that 98 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.

```

**Example 2:**

```

**Input:** a = 6, b = 7 , n = 5
**Output:** 930
**Explanation:** For x = 25, (a XOR x) = 31 and (b XOR x) = 30. Hence, (a XOR x) * (b XOR x) = 930.
It can be shown that 930 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
```

**Example 3:**

```

**Input:** a = 1, b = 6, n = 3
**Output:** 12
**Explanation:** For x = 5, (a XOR x) = 4 and (b XOR x) = 3. Hence, (a XOR x) * (b XOR x) = 12.
It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.

```

**Constraints:**

* `0 <= a, b < 250`
* `0 <= n <= 50`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maximumXorProduct(self, a: int, b: int, n: int) -> int:
```

my_solution = Solution()

assert my_solution.maximumXorProduct(**[12, 5, 4]) == 98
assert my_solution.maximumXorProduct(**[6, 7, 5]) == 930
assert my_solution.maximumXorProduct(**[1, 6, 3]) == 12
assert my_solution.maximumXorProduct(**[0, 0, 1]) == 1
assert my_solution.maximumXorProduct(**[0, 1, 6]) == 3906
assert my_solution.maximumXorProduct(**[0, 2, 7]) == 15875
assert my_solution.maximumXorProduct(**[0, 3, 1]) == 2
assert my_solution.maximumXorProduct(**[0, 4, 0]) == 0
assert my_solution.maximumXorProduct(**[0, 5, 6]) == 3658
assert my_solution.maximumXorProduct(**[0, 6, 1]) == 7
assert my_solution.maximumXorProduct(**[0, 7, 2]) == 12
assert my_solution.maximumXorProduct(**[0, 8, 5]) == 713
assert my_solution.maximumXorProduct(**[0, 9, 2]) == 30
assert my_solution.maximumXorProduct(**[0, 10, 7]) == 14875
assert my_solution.maximumXorProduct(**[0, 11, 4]) == 84
assert my_solution.maximumXorProduct(**[0, 12, 2]) == 45
assert my_solution.maximumXorProduct(**[0, 13, 2]) == 42
assert my_solution.maximumXorProduct(**[0, 14, 0]) == 0
assert my_solution.maximumXorProduct(**[0, 15, 6]) == 3080
assert my_solution.maximumXorProduct(**[1, 0, 3]) == 42


# Find Building Where Alice and Bob Can Meet

You are given a **0-indexed** array `heights` of positive integers, where `heights[i]` represents the height of the `ith` building.

If a person is in building `i`, they can move to any other building `j` if and only if `i < j` and `heights[i] < heights[j]`.

You are also given another array `queries` where `queries[i] = [ai, bi]`. On the `ith` query, Alice is in building `ai` while Bob is in building `bi`.

Return *an array* `ans` *where* `ans[i]` *is **the index of the leftmost building** where Alice and Bob can meet on the* `ith` *query*. *If Alice and Bob cannot move to a common building on query* `i`, *set* `ans[i]` *to* `-1`.

**Example 1:**

```

**Input:** heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
**Output:** [2,5,-1,5,2]
**Explanation:** In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2].
In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5].
In the third query, Alice cannot meet Bob since Alice cannot move to any other building.
In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5].
In the fifth query, Alice and Bob are already in the same building.
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.

```

**Example 2:**

```

**Input:** heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]
**Output:** [7,6,-1,4,6]
**Explanation:** In the first query, Alice can directly move to Bob's building since heights[0] < heights[7].
In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6].
In the third query, Alice cannot meet Bob since Bob cannot move to any other building.
In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4].
In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6].
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.

```

**Constraints:**

* `1 <= heights.length <= 5 * 104`
* `1 <= heights[i] <= 109`
* `1 <= queries.length <= 5 * 104`
* `queries[i] = [ai, bi]`
* `0 <= ai, bi <= heights.length - 1`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]:
```


# Maximum Strong Pair XOR I

You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition:

* `|x - y| <= min(x, y)`

You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array.

Return *the **maximum*** `XOR` *value out of all possible strong pairs in the array* `nums`.

**Note** that you can pick the same integer twice to form a pair.

**Example 1:**

```

**Input:** nums = [1,2,3,4,5]
**Output:** 7
**Explanation:** There are 11 strong pairs in the array nums: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.

```

**Example 2:**

```

**Input:** nums = [10,100]
**Output:** 0
**Explanation:** There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.

```

**Example 3:**

```

**Input:** nums = [5,6,25,30]
**Output:** 7
**Explanation:** There are 6 strong pairs in the array nums: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.

```

**Constraints:**

* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maximumStrongPairXor(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.maximumStrongPairXor(**[[1, 2, 3, 4, 5]]) == 7
assert my_solution.maximumStrongPairXor(**[[10, 100]]) == 0
assert my_solution.maximumStrongPairXor(**[[5, 6, 25, 30]]) == 7
assert my_solution.maximumStrongPairXor(**[[1]]) == 0
assert my_solution.maximumStrongPairXor(**[[100]]) == 0
assert my_solution.maximumStrongPairXor(**[[1, 1, 2, 3, 5]]) == 6
assert my_solution.maximumStrongPairXor(**[[1, 1, 3, 8, 7]]) == 15
assert my_solution.maximumStrongPairXor(**[[1, 1, 4, 4, 3]]) == 7
assert my_solution.maximumStrongPairXor(**[[1, 1, 6, 6, 9]]) == 15
assert my_solution.maximumStrongPairXor(**[[1, 1, 10, 3, 9]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 2, 1, 5, 3]]) == 6
assert my_solution.maximumStrongPairXor(**[[1, 2, 2, 1, 2]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 2, 3, 8, 1]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 2, 5, 5, 10]]) == 15
assert my_solution.maximumStrongPairXor(**[[1, 2, 8, 3, 2]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 2, 9, 2, 8]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 3, 3, 2, 1]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 3, 8, 5, 3]]) == 13
assert my_solution.maximumStrongPairXor(**[[1, 3, 9, 6, 5]]) == 15
assert my_solution.maximumStrongPairXor(**[[1, 4, 1, 2, 5]]) == 6


# High-Access Employees

You are given a 2D **0-indexed** array of strings, `access_times`, with size `n`. For each `i` where `0 <= i <= n - 1`, `access_times[i][0]` represents the name of an employee, and `access_times[i][1]` represents the access time of that employee. All entries in `access_times` are within the same day.

The access time is represented as **four digits** using a **24-hour** time format, for example, `"0800"` or `"2250"`.

An employee is said to be **high-access** if he has accessed the system **three or more** times within a **one-hour period**.

Times with exactly one hour of difference are **not** considered part of the same one-hour period. For example, `"0815"` and `"0915"` are not part of the same one-hour period.

Access times at the start and end of the day are **not** counted within the same one-hour period. For example, `"0005"` and `"2350"` are not part of the same one-hour period.

Return *a list that contains the names of **high-access** employees with any order you want.*

**Example 1:**

```

**Input:** access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
**Output:** ["a"]
**Explanation:** "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
But "b" does not have more than two access times at all.
So the answer is ["a"].
```

**Example 2:**

```

**Input:** access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
**Output:** ["c","d"]
**Explanation:** "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
"d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].
```

**Example 3:**

```

**Input:** access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
**Output:** ["ab","cd"]
**Explanation:** "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
"cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
So the answer is ["ab","cd"].
```

**Constraints:**

* `1 <= access_times.length <= 100`
* `access_times[i].length == 2`
* `1 <= access_times[i][0].length <= 10`
* `access_times[i][0]` consists only of English small letters.
* `access_times[i][1].length == 4`
* `access_times[i][1]` is in 24-hour time format.
* `access_times[i][1]` consists only of `'0'` to `'9'`.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
```

my_solution = Solution()

assert my_solution.findHighAccessEmployees(**[[['a', '0549'], ['b', '0457'], ['a', '0532'], ['a', '0621'], ['b', '0540']]]) == ['a']
assert my_solution.findHighAccessEmployees(**[[['d', '0002'], ['c', '0808'], ['c', '0829'], ['e', '0215'], ['d', '1508'], ['d', '1444'], ['d', '1410'], ['c', '0809']]]) == ['c', 'd']
assert my_solution.findHighAccessEmployees(**[[['cd', '1025'], ['ab', '1025'], ['cd', '1046'], ['cd', '1055'], ['ab', '1124'], ['ab', '1120']]]) == ['ab', 'cd']
assert my_solution.findHighAccessEmployees(**[[['baipstt', '1456']]]) == []
assert my_solution.findHighAccessEmployees(**[[['bouo', '1126']]]) == []
assert my_solution.findHighAccessEmployees(**[[['cavfbqg', '2304']]]) == []
assert my_solution.findHighAccessEmployees(**[[['cenjcq', '1007']]]) == []
assert my_solution.findHighAccessEmployees(**[[['cqotrwqcaq', '0131']]]) == []
assert my_solution.findHighAccessEmployees(**[[['downbuk', '1951']]]) == []
assert my_solution.findHighAccessEmployees(**[[['dqsoiyz', '2204']]]) == []
assert my_solution.findHighAccessEmployees(**[[['duzeyrov', '0243']]]) == []
assert my_solution.findHighAccessEmployees(**[[['erfg', '1223']]]) == []
assert my_solution.findHighAccessEmployees(**[[['fwhefd', '2026']]]) == []
assert my_solution.findHighAccessEmployees(**[[['gbefbne', '0911']]]) == []
assert my_solution.findHighAccessEmployees(**[[['gp', '1540']]]) == []
assert my_solution.findHighAccessEmployees(**[[['ht', '1319']]]) == []
assert my_solution.findHighAccessEmployees(**[[['inahnsjdqz', '1750']]]) == []
assert my_solution.findHighAccessEmployees(**[[['jwxvijxo', '0851']]]) == []
assert my_solution.findHighAccessEmployees(**[[['kibwwvjuez', '0716']]]) == []
assert my_solution.findHighAccessEmployees(**[[['lvry', '0706']]]) == []


# Minimum Operations to Maximize Last Elements in Arrays

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

You are allowed to perform a series of **operations** (**possibly none**).

In an operation, you select an index `i` in the range `[0, n - 1]` and **swap** the values of `nums1[i]` and `nums2[i]`.

Your task is to find the **minimum** number of operations required to satisfy the following conditions:

* `nums1[n - 1]` is equal to the **maximum value** among all elements of `nums1`, i.e., `nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])`.
* `nums2[n - 1]` is equal to the **maximum** **value** among all elements of `nums2`, i.e., `nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])`.

Return *an integer denoting the **minimum** number of operations needed to meet **both** conditions*, *or* `-1` *if it is **impossible** to satisfy both conditions.*

**Example 1:**

```

**Input:** nums1 = [1,2,7], nums2 = [4,5,3]
**Output:** 1
**Explanation:** In this example, an operation can be performed using index i = 2.
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
Both conditions are now satisfied.
It can be shown that the minimum number of operations needed to be performed is 1.
So, the answer is 1.

```

**Example 2:**

```

**Input:** nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]
**Output:** 2
**Explanation:** In this example, the following operations can be performed:
First operation using index i = 4.
When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9].
Another operation using index i = 3.
When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9].
Both conditions are now satisfied.
It can be shown that the minimum number of operations needed to be performed is 2.
So, the answer is 2.

```

**Example 3:**

```

**Input:** nums1 = [1,5,4], nums2 = [2,5,3]
**Output:** -1
**Explanation:** In this example, it is not possible to satisfy both conditions.
So, the answer is -1.

```

**Constraints:**

* `1 <= n == nums1.length == nums2.length <= 1000`
* `1 <= nums1[i] <= 109`
* `1 <= nums2[i] <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.minOperations(**[[1, 2, 7], [4, 5, 3]]) == 1
assert my_solution.minOperations(**[[2, 3, 4, 5, 9], [8, 8, 4, 4, 4]]) == 2
assert my_solution.minOperations(**[[1, 5, 4], [2, 5, 3]]) == -1
assert my_solution.minOperations(**[[1], [1]]) == 0
assert my_solution.minOperations(**[[1, 2], [2, 1]]) == 1
assert my_solution.minOperations(**[[1, 1, 10], [1, 5, 1]]) == 1
assert my_solution.minOperations(**[[1, 4, 16], [16, 16, 16]]) == 0
assert my_solution.minOperations(**[[1, 5, 15], [1, 1, 1]]) == 0
assert my_solution.minOperations(**[[2, 5, 7], [2, 2, 2]]) == 0
assert my_solution.minOperations(**[[8, 9, 10], [10, 9, 9]]) == 1
assert my_solution.minOperations(**[[9, 14, 14], [14, 11, 14]]) == 0
assert my_solution.minOperations(**[[16, 16, 16], [6, 7, 16]]) == 0
assert my_solution.minOperations(**[[19, 7, 19], [5, 19, 19]]) == 0
assert my_solution.minOperations(**[[1, 1, 8, 9], [1, 7, 1, 1]]) == 1
assert my_solution.minOperations(**[[1, 5, 9, 9], [9, 9, 8, 9]]) == 0
assert my_solution.minOperations(**[[1, 7, 7, 7], [7, 3, 3, 7]]) == 0
assert my_solution.minOperations(**[[10, 18, 12, 12], [19, 6, 5, 12]]) == -1
assert my_solution.minOperations(**[[12, 9, 11, 12], [3, 9, 9, 9]]) == 0
assert my_solution.minOperations(**[[15, 54, 22, 54], [54, 19, 54, 54]]) == 0
assert my_solution.minOperations(**[[20, 20, 20, 20], [5, 8, 19, 20]]) == 0


# Maximum Strong Pair XOR II

You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition:

* `|x - y| <= min(x, y)`

You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array.

Return *the **maximum*** `XOR` *value out of all possible strong pairs in the array* `nums`.

**Note** that you can pick the same integer twice to form a pair.

**Example 1:**

```

**Input:** nums = [1,2,3,4,5]
**Output:** 7
**Explanation:** There are 11 strong pairs in the array nums: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.

```

**Example 2:**

```

**Input:** nums = [10,100]
**Output:** 0
**Explanation:** There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.

```

**Example 3:**

```

**Input:** nums = [500,520,2500,3000]
**Output:** 1020
**Explanation:** There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.

```

**Constraints:**

* `1 <= nums.length <= 5 * 104`
* `1 <= nums[i] <= 220 - 1`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maximumStrongPairXor(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.maximumStrongPairXor(**[[1, 2, 3, 4, 5]]) == 7
assert my_solution.maximumStrongPairXor(**[[10, 100]]) == 0
assert my_solution.maximumStrongPairXor(**[[500, 520, 2500, 3000]]) == 1020
assert my_solution.maximumStrongPairXor(**[[1]]) == 0
assert my_solution.maximumStrongPairXor(**[[2, 3]]) == 1
assert my_solution.maximumStrongPairXor(**[[3, 4]]) == 7
assert my_solution.maximumStrongPairXor(**[[4, 5]]) == 1
assert my_solution.maximumStrongPairXor(**[[5, 6]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 1, 1]]) == 0
assert my_solution.maximumStrongPairXor(**[[1, 1, 2]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 1, 3]]) == 0
assert my_solution.maximumStrongPairXor(**[[1, 1, 4]]) == 0
assert my_solution.maximumStrongPairXor(**[[1, 1, 5]]) == 0
assert my_solution.maximumStrongPairXor(**[[1, 2, 1]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 2, 2]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 2, 3]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 2, 4]]) == 6
assert my_solution.maximumStrongPairXor(**[[1, 2, 5]]) == 3
assert my_solution.maximumStrongPairXor(**[[1, 3, 1]]) == 0
assert my_solution.maximumStrongPairXor(**[[1, 3, 2]]) == 3


# Distribute Candies Among Children I

You are given two positive integers `n` and `limit`.

Return *the **total number** of ways to distribute* `n` *candies among* `3` *children such that no child gets more than* `limit` *candies.*

**Example 1:**

```

**Input:** n = 5, limit = 2
**Output:** 3
**Explanation:** There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).

```

**Example 2:**

```

**Input:** n = 3, limit = 3
**Output:** 10
**Explanation:** There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).

```

**Constraints:**

* `1 <= n <= 50`
* `1 <= limit <= 50`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def distributeCandies(self, n: int, limit: int) -> int:
```

my_solution = Solution()

assert my_solution.distributeCandies(**[5, 2]) == 3
assert my_solution.distributeCandies(**[3, 3]) == 10
assert my_solution.distributeCandies(**[1, 1]) == 3
assert my_solution.distributeCandies(**[1, 2]) == 3
assert my_solution.distributeCandies(**[1, 3]) == 3
assert my_solution.distributeCandies(**[1, 4]) == 3
assert my_solution.distributeCandies(**[1, 5]) == 3
assert my_solution.distributeCandies(**[1, 6]) == 3
assert my_solution.distributeCandies(**[1, 7]) == 3
assert my_solution.distributeCandies(**[1, 8]) == 3
assert my_solution.distributeCandies(**[1, 9]) == 3
assert my_solution.distributeCandies(**[1, 10]) == 3
assert my_solution.distributeCandies(**[1, 11]) == 3
assert my_solution.distributeCandies(**[1, 12]) == 3
assert my_solution.distributeCandies(**[1, 13]) == 3
assert my_solution.distributeCandies(**[1, 14]) == 3
assert my_solution.distributeCandies(**[1, 15]) == 3
assert my_solution.distributeCandies(**[1, 16]) == 3
assert my_solution.distributeCandies(**[1, 17]) == 3
assert my_solution.distributeCandies(**[1, 18]) == 3


# Distribute Candies Among Children II

You are given two positive integers `n` and `limit`.

Return *the **total number** of ways to distribute* `n` *candies among* `3` *children such that no child gets more than* `limit` *candies.*

**Example 1:**

```

**Input:** n = 5, limit = 2
**Output:** 3
**Explanation:** There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).

```

**Example 2:**

```

**Input:** n = 3, limit = 3
**Output:** 10
**Explanation:** There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).

```

**Constraints:**

* `1 <= n <= 106`
* `1 <= limit <= 106`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def distributeCandies(self, n: int, limit: int) -> int:
```


# Number of Strings Which Can Be Rearranged to Contain Substring

You are given an integer `n`.

A string `s` is called **good** if it contains only lowercase English characters **and** it is possible to rearrange the characters of `s` such that the new string contains `"leet"` as a **substring**.

For example:

* The string `"lteer"` is good because we can rearrange it to form `"leetr"` .
* `"letl"` is not good because we cannot rearrange it to contain `"leet"` as a substring.

Return *the **total** number of good strings of length* `n`.

Since the answer may be large, return it **modulo** `109 + 7`.

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

**Example 1:**

```

**Input:** n = 4
**Output:** 12
**Explanation:** The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".

```

**Example 2:**

```

**Input:** n = 10
**Output:** 83943898
**Explanation:** The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (109 + 7) = 83943898.

```

**Constraints:**

* `1 <= n <= 105`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def stringCount(self, n: int) -> int:
```

my_solution = Solution()

assert my_solution.stringCount(**[4]) == 12
assert my_solution.stringCount(**[10]) == 83943898
assert my_solution.stringCount(**[1]) == 0
assert my_solution.stringCount(**[2]) == 0
assert my_solution.stringCount(**[3]) == 0
assert my_solution.stringCount(**[5]) == 1460
assert my_solution.stringCount(**[6]) == 106620
assert my_solution.stringCount(**[7]) == 6058192
assert my_solution.stringCount(**[8]) == 295164156
assert my_solution.stringCount(**[9]) == 947613240
assert my_solution.stringCount(**[11]) == 795234177
assert my_solution.stringCount(**[12]) == 55396773
assert my_solution.stringCount(**[13]) == 968092561
assert my_solution.stringCount(**[14]) == 715599898
assert my_solution.stringCount(**[15]) == 430509685
assert my_solution.stringCount(**[16]) == 462719236
assert my_solution.stringCount(**[17]) == 155543310
assert my_solution.stringCount(**[18]) == 159683962
assert my_solution.stringCount(**[19]) == 808507313
assert my_solution.stringCount(**[20]) == 291395991


# Maximum Spending After Buying Items

You are given a **0-indexed** `m * n` integer matrix `values`, representing the values of `m * n` different items in `m` different shops. Each shop has `n` items where the `jth` item in the `ith` shop has a value of `values[i][j]`. Additionally, the items in the `ith` shop are sorted in non-increasing order of value. That is, `values[i][j] >= values[i][j + 1]` for all `0 <= j < n - 1`.

On each day, you would like to buy a single item from one of the shops. Specifically, On the `dth` day you can:

* Pick any shop `i`.
* Buy the rightmost available item `j` for the price of `values[i][j] * d`. That is, find the greatest index `j` such that item `j` was never bought before, and buy it for the price of `values[i][j] * d`.

**Note** that all items are pairwise different. For example, if you have bought item `0` from shop `1`, you can still buy item `0` from any other shop.

Return *the **maximum amount of money that can be spent** on buying all*  `m * n` *products*.

**Example 1:**

```

**Input:** values = [[8,5,2],[6,4,1],[9,7,3]]
**Output:** 285
**Explanation:** On the first day, we buy product 2 from shop 1 for a price of values[1][2] * 1 = 1.
On the second day, we buy product 2 from shop 0 for a price of values[0][2] * 2 = 4.
On the third day, we buy product 2 from shop 2 for a price of values[2][2] * 3 = 9.
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] * 4 = 16.
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] * 5 = 25.
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] * 6 = 36.
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] * 7 = 49.
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] * 8 = 64.
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] * 9 = 81.
Hence, our total spending is equal to 285.
It can be shown that 285 is the maximum amount of money that can be spent buying all m * n products.

```

**Example 2:**

```

**Input:** values = [[10,8,6,4,2],[9,7,5,3,2]]
**Output:** 386
**Explanation:** On the first day, we buy product 4 from shop 0 for a price of values[0][4] * 1 = 2.
On the second day, we buy product 4 from shop 1 for a price of values[1][4] * 2 = 4.
On the third day, we buy product 3 from shop 1 for a price of values[1][3] * 3 = 9.
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] * 4 = 16.
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] * 5 = 25.
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] * 6 = 36.
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] * 7 = 49.
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] * 8 = 64
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] * 9 = 81.
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] * 10 = 100.
Hence, our total spending is equal to 386.
It can be shown that 386 is the maximum amount of money that can be spent buying all m * n products.

```

**Constraints:**

* `1 <= m == values.length <= 10`
* `1 <= n == values[i].length <= 104`
* `1 <= values[i][j] <= 106`
* `values[i]` are sorted in non-increasing order.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maxSpending(self, values: List[List[int]]) -> int:
```


# Find Champion I

There are `n` teams numbered from `0` to `n - 1` in a tournament.

Given a **0-indexed** 2D boolean matrix `grid` of size `n * n`. For all `i, j` that `0 <= i, j <= n - 1` and `i != j` team `i` is **stronger** than team `j` if `grid[i][j] == 1`, otherwise, team `j` is **stronger** than team `i`.

Team `a` will be the **champion** of the tournament if there is no team `b` that is stronger than team `a`.

Return *the team that will be the champion of the tournament.*

**Example 1:**

```

**Input:** grid = [[0,1],[0,0]]
**Output:** 0
**Explanation:** There are two teams in this tournament.
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.

```

**Example 2:**

```

**Input:** grid = [[0,0,1],[1,0,1],[0,0,0]]
**Output:** 1
**Explanation:** There are three teams in this tournament.
grid[1][0] == 1 means that team 1 is stronger than team 0.
grid[1][2] == 1 means that team 1 is stronger than team 2.
So team 1 will be the champion.

```

**Constraints:**

* `n == grid.length`
* `n == grid[i].length`
* `2 <= n <= 100`
* `grid[i][j]` is either `0` or `1`.
* For all `i grid[i][i]` is `0.`
* For all `i, j` that `i != j`, `grid[i][j] != grid[j][i]`.
* The input is generated such that if team `a` is stronger than team `b` and team `b` is stronger than team `c`, then team `a` is stronger than team `c`.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findChampion(self, grid: List[List[int]]) -> int:
```


# Maximum Balanced Subsequence Sum

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

A **subsequence** of `nums` having length `k` and consisting of **indices** `i0 < i1 < ... < ik-1` is **balanced** if the following holds:

* `nums[ij] - nums[ij-1] >= ij - ij-1`, for every `j` in the range `[1, k - 1]`.

A **subsequence** of `nums` having length `1` is considered balanced.

Return *an integer denoting the **maximum** possible **sum of elements** in a **balanced** subsequence of* `nums`.

A **subsequence** of an array is a new **non-empty** array that is formed from the original array by deleting some (**possibly none**) of the elements without disturbing the relative positions of the remaining elements.

**Example 1:**

```

**Input:** nums = [3,3,5,6]
**Output:** 14
**Explanation:** In this example, the subsequence [3,5,6] consisting of indices 0, 2, and 3 can be selected.
nums[2] - nums[0] >= 2 - 0.
nums[3] - nums[2] >= 3 - 2.
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
The subsequence consisting of indices 1, 2, and 3 is also valid.
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 14.
```

**Example 2:**

```

**Input:** nums = [5,-1,-3,8]
**Output:** 13
**Explanation:** In this example, the subsequence [5,8] consisting of indices 0 and 3 can be selected.
nums[3] - nums[0] >= 3 - 0.
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 13.

```

**Example 3:**

```

**Input:** nums = [-2,-1]
**Output:** -1
**Explanation:** In this example, the subsequence [-1] can be selected.
It is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def maxBalancedSubsequenceSum(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.maxBalancedSubsequenceSum(**[[3, 3, 5, 6]]) == 14
assert my_solution.maxBalancedSubsequenceSum(**[[5, -1, -3, 8]]) == 13
assert my_solution.maxBalancedSubsequenceSum(**[[-2, -1]]) == -1
assert my_solution.maxBalancedSubsequenceSum(**[[0]]) == 0
assert my_solution.maxBalancedSubsequenceSum(**[[-47]]) == -47
assert my_solution.maxBalancedSubsequenceSum(**[[-8]]) == -8
assert my_solution.maxBalancedSubsequenceSum(**[[-7]]) == -7
assert my_solution.maxBalancedSubsequenceSum(**[[-6]]) == -6
assert my_solution.maxBalancedSubsequenceSum(**[[-5]]) == -5
assert my_solution.maxBalancedSubsequenceSum(**[[-3]]) == -3
assert my_solution.maxBalancedSubsequenceSum(**[[-2]]) == -2
assert my_solution.maxBalancedSubsequenceSum(**[[-1]]) == -1
assert my_solution.maxBalancedSubsequenceSum(**[[1]]) == 1
assert my_solution.maxBalancedSubsequenceSum(**[[3]]) == 3
assert my_solution.maxBalancedSubsequenceSum(**[[4]]) == 4
assert my_solution.maxBalancedSubsequenceSum(**[[5]]) == 5
assert my_solution.maxBalancedSubsequenceSum(**[[7]]) == 7
assert my_solution.maxBalancedSubsequenceSum(**[[8]]) == 8
assert my_solution.maxBalancedSubsequenceSum(**[[9]]) == 9
assert my_solution.maxBalancedSubsequenceSum(**[[45]]) == 45


# Find the K-or of an Array

You are given a **0-indexed** integer array `nums`, and an integer `k`.

The **K-or** of `nums` is a non-negative integer that satisfies the following:

* The `ith` bit is set in the K-or **if and only if** there are at least `k` elements of nums in which bit `i` is set.

Return *the  **K-or** of* `nums`.

**Note** that a bit `i` is set in `x` if `(2i AND x) == 2i`, where `AND` is the bitwise `AND` operator.

**Example 1:**

```

**Input:** nums = [7,12,9,8,9,15], k = 4
**Output:** 9
**Explanation:** Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].
Bit 1 is set at nums[0], and nums[5].
Bit 2 is set at nums[0], nums[1], and nums[5].
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].
Only bits 0 and 3 are set in at least k elements of the array, and bits i >= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9.

```

**Example 2:**

```

**Input:** nums = [2,12,1,11,4,5], k = 6
**Output:** 0
**Explanation:** Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0.

```

**Example 3:**

```

**Input:** nums = [10,8,5,9,11,6,8], k = 1
**Output:** 15
**Explanation:** Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.

```

**Constraints:**

* `1 <= nums.length <= 50`
* `0 <= nums[i] < 231`
* `1 <= k <= nums.length`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def findKOr(self, nums: List[int], k: int) -> int:
```

my_solution = Solution()

assert my_solution.findKOr(**[[7, 12, 9, 8, 9, 15], 4]) == 9
assert my_solution.findKOr(**[[2, 12, 1, 11, 4, 5], 6]) == 0
assert my_solution.findKOr(**[[10, 8, 5, 9, 11, 6, 8], 1]) == 15
assert my_solution.findKOr(**[[14, 7, 12, 9, 8, 9, 1, 15], 4]) == 13
assert my_solution.findKOr(**[[2, 12, 1, 11, 4, 5], 3]) == 5
assert my_solution.findKOr(**[[10, 8, 5, 10, 11, 11, 6, 8], 1]) == 15
assert my_solution.findKOr(**[[0], 1]) == 0
assert my_solution.findKOr(**[[1], 1]) == 1
assert my_solution.findKOr(**[[2], 1]) == 2
assert my_solution.findKOr(**[[3], 1]) == 3
assert my_solution.findKOr(**[[4], 1]) == 4
assert my_solution.findKOr(**[[5], 1]) == 5
assert my_solution.findKOr(**[[6], 1]) == 6
assert my_solution.findKOr(**[[7], 1]) == 7
assert my_solution.findKOr(**[[8], 1]) == 8
assert my_solution.findKOr(**[[9], 1]) == 9
assert my_solution.findKOr(**[[10], 1]) == 10
assert my_solution.findKOr(**[[11], 1]) == 11
assert my_solution.findKOr(**[[12], 1]) == 12
assert my_solution.findKOr(**[[13], 1]) == 13


# Minimum Equal Sum of Two Arrays After Replacing Zeros

You are given two arrays `nums1` and `nums2` consisting of positive integers.

You have to replace **all** the `0`'s in both arrays with **strictly** positive integers such that the sum of elements of both arrays becomes **equal**.

Return *the **minimum** equal sum you can obtain, or* `-1` *if it is impossible*.

**Example 1:**

```

**Input:** nums1 = [3,2,0,1,0], nums2 = [6,5,0]
**Output:** 12
**Explanation:** We can replace 0's in the following way:
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.

```

**Example 2:**

```

**Input:** nums1 = [2,0,2,0], nums2 = [1,4]
**Output:** -1
**Explanation:** It is impossible to make the sum of both arrays equal.

```

**Constraints:**

* `1 <= nums1.length, nums2.length <= 105`
* `0 <= nums1[i], nums2[i] <= 106`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minSum(self, nums1: List[int], nums2: List[int]) -> int:
```


# Minimum Increment Operations to Make Array Beautiful

You are given a **0-indexed** integer array `nums` having length `n`, and an integer `k`.

You can perform the following **increment** operation **any** number of times (**including zero**):

* Choose an index `i` in the range `[0, n - 1]`, and increase `nums[i]` by `1`.

An array is considered **beautiful** if, for any **subarray** with a size of `3` or **more**, its **maximum** element is **greater than or equal** to `k`.

Return *an integer denoting the **minimum** number of increment operations needed to make* `nums` ***beautiful**.*

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

**Example 1:**

```

**Input:** nums = [2,3,0,0,2], k = 4
**Output:** 3
**Explanation:** We can perform the following increment operations to make nums beautiful:
Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
Hence, the answer is 3.

```

**Example 2:**

```

**Input:** nums = [0,1,3,3], k = 5
**Output:** 2
**Explanation:** We can perform the following increment operations to make nums beautiful:
Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
Hence, the answer is 2.

```

**Example 3:**

```

**Input:** nums = [1,1,2], k = 1
**Output:** 0
**Explanation:** The only subarray with a size of 3 or more in this example is [1,1,2].
The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
Hence, the answer is 0.

```

**Constraints:**

* `3 <= n == nums.length <= 105`
* `0 <= nums[i] <= 109`
* `0 <= k <= 109`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minIncrementOperations(self, nums: List[int], k: int) -> int:
```

my_solution = Solution()

assert my_solution.minIncrementOperations(**[[2, 3, 0, 0, 2], 4]) == 3
assert my_solution.minIncrementOperations(**[[0, 1, 3, 3], 5]) == 2
assert my_solution.minIncrementOperations(**[[1, 1, 2], 1]) == 0
assert my_solution.minIncrementOperations(**[[0, 5, 5], 8]) == 3
assert my_solution.minIncrementOperations(**[[0, 18, 28], 93]) == 65
assert my_solution.minIncrementOperations(**[[0, 24, 14], 7]) == 0
assert my_solution.minIncrementOperations(**[[2, 3, 4], 3]) == 0
assert my_solution.minIncrementOperations(**[[3, 5, 9], 6]) == 0
assert my_solution.minIncrementOperations(**[[4, 3, 0], 2]) == 0
assert my_solution.minIncrementOperations(**[[5, 6, 5], 9]) == 3
assert my_solution.minIncrementOperations(**[[6, 9, 6], 3]) == 0
assert my_solution.minIncrementOperations(**[[7, 9, 0], 6]) == 0
assert my_solution.minIncrementOperations(**[[7, 47, 16], 39]) == 0
assert my_solution.minIncrementOperations(**[[9, 6, 1], 6]) == 0
assert my_solution.minIncrementOperations(**[[41, 44, 37], 55]) == 11
assert my_solution.minIncrementOperations(**[[48, 3, 13], 1]) == 0
assert my_solution.minIncrementOperations(**[[1, 2, 6, 9], 8]) == 2
assert my_solution.minIncrementOperations(**[[1, 3, 1, 6], 6]) == 3
assert my_solution.minIncrementOperations(**[[2, 35, 41, 20], 4]) == 0
assert my_solution.minIncrementOperations(**[[3, 9, 9, 7], 6]) == 0


# Subarrays Distinct Element Sum of Squares I

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

The **distinct count** of a subarray of `nums` is defined as:

* Let `nums[i..j]` be a subarray of `nums` consisting of all the indices from `i` to `j` such that `0 <= i <= j < nums.length`. Then the number of distinct values in `nums[i..j]` is called the distinct count of `nums[i..j]`.

Return *the sum of the **squares** of **distinct counts** of all subarrays of* `nums`.

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

**Example 1:**

```

**Input:** nums = [1,2,1]
**Output:** 15
**Explanation:** Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.

```

**Example 2:**

```

**Input:** nums = [1,1]
**Output:** 3
**Explanation:** Three possible subarrays are:
[1]: 1 distinct value
[1]: 1 distinct value
[1,1]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
```

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def sumCounts(self, nums: List[int]) -> int:
```


# Minimum Number of Changes to Make Binary String Beautiful

You are given a **0-indexed** binary string `s` having an even length.

A string is **beautiful** if it's possible to partition it into one or more substrings such that:

* Each substring has an **even length**.
* Each substring contains **only** `1`'s or **only** `0`'s.

You can change any character in `s` to `0` or `1`.

Return *the **minimum** number of changes required to make the string* `s` *beautiful*.

**Example 1:**

```

**Input:** s = "1001"
**Output:** 2
**Explanation:** We change s[1] to 1 and s[3] to 0 to get string "1100".
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.

```

**Example 2:**

```

**Input:** s = "10"
**Output:** 1
**Explanation:** We change s[1] to 1 to get string "11".
It can be seen that the string "11" is beautiful because we can partition it into "11".
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.

```

**Example 3:**

```

**Input:** s = "0000"
**Output:** 0
**Explanation:** We don't need to make any changes as the string "0000" is beautiful already.

```

**Constraints:**

* `2 <= s.length <= 105`
* `s` has an even length.
* `s[i]` is either `'0'` or `'1'`.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minChanges(self, s: str) -> int:
```

my_solution = Solution()

assert my_solution.minChanges(**['1001']) == 2
assert my_solution.minChanges(**['10']) == 1
assert my_solution.minChanges(**['0000']) == 0
assert my_solution.minChanges(**['11000111']) == 1
assert my_solution.minChanges(**['01010001']) == 3
assert my_solution.minChanges(**['010010']) == 2
assert my_solution.minChanges(**['111111111110010001']) == 3
assert my_solution.minChanges(**['01010000011001001101']) == 6
assert my_solution.minChanges(**['011011100001110111']) == 5
assert my_solution.minChanges(**['1001000010111010']) == 5
assert my_solution.minChanges(**['0011']) == 0
assert my_solution.minChanges(**['11100100010010']) == 4
assert my_solution.minChanges(**['110100']) == 1
assert my_solution.minChanges(**['01']) == 1
assert my_solution.minChanges(**['10110010']) == 2
assert my_solution.minChanges(**['0010']) == 1
assert my_solution.minChanges(**['01000011000111']) == 2
assert my_solution.minChanges(**['0001110001']) == 2
assert my_solution.minChanges(**['000000001010100011']) == 3
assert my_solution.minChanges(**['100001']) == 2


# Length of the Longest Subsequence That Sums to Target

You are given a **0-indexed** array of integers `nums`, and an integer `target`.

Return *the **length of the longest subsequence** of* `nums` *that sums up to* `target`. *If no such subsequence exists, return* `-1`.

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

**Example 1:**

```

**Input:** nums = [1,2,3,4,5], target = 9
**Output:** 3
**Explanation:** There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3.

```

**Example 2:**

```

**Input:** nums = [4,1,3,2,1,5], target = 7
**Output:** 4
**Explanation:** There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4.

```

**Example 3:**

```

**Input:** nums = [1,1,5,4,5], target = 3
**Output:** -1
**Explanation:** It can be shown that nums has no subsequence that sums up to 3.

```

**Constraints:**

* `1 <= nums.length <= 1000`
* `1 <= nums[i] <= 1000`
* `1 <= target <= 1000`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
```

my_solution = Solution()

assert my_solution.lengthOfLongestSubsequence(**[[1, 2, 3, 4, 5], 9]) == 3
assert my_solution.lengthOfLongestSubsequence(**[[4, 1, 3, 2, 1, 5], 7]) == 4
assert my_solution.lengthOfLongestSubsequence(**[[1, 1, 5, 4, 5], 3]) == -1
assert my_solution.lengthOfLongestSubsequence(**[[1000], 12]) == -1
assert my_solution.lengthOfLongestSubsequence(**[[1000], 1000]) == 1
assert my_solution.lengthOfLongestSubsequence(**[[1, 2], 10]) == -1
assert my_solution.lengthOfLongestSubsequence(**[[1, 1000], 5]) == -1
assert my_solution.lengthOfLongestSubsequence(**[[2, 3], 3]) == 1
assert my_solution.lengthOfLongestSubsequence(**[[2, 3], 5]) == 2
assert my_solution.lengthOfLongestSubsequence(**[[2, 3, 5], 5]) == 2
assert my_solution.lengthOfLongestSubsequence(**[[1, 3, 3, 7], 1000]) == -1
assert my_solution.lengthOfLongestSubsequence(**[[1, 3, 3, 7], 2]) == -1
assert my_solution.lengthOfLongestSubsequence(**[[1, 3, 3, 8], 7]) == 3
assert my_solution.lengthOfLongestSubsequence(**[[1, 1, 2, 1], 2]) == 2
assert my_solution.lengthOfLongestSubsequence(**[[1, 1, 1, 1], 5]) == -1
assert my_solution.lengthOfLongestSubsequence(**[[1, 1, 1, 2], 3]) == 3
assert my_solution.lengthOfLongestSubsequence(**[[9, 12, 8, 4, 11, 13, 15, 7, 5], 84]) == 9
assert my_solution.lengthOfLongestSubsequence(**[[11, 5, 9, 11, 12, 13, 12, 5, 1, 8], 87]) == 10
assert my_solution.lengthOfLongestSubsequence(**[[9, 11, 11, 15, 4, 14, 3, 2, 13, 7], 89]) == 10
assert my_solution.lengthOfLongestSubsequence(**[[11, 13, 6, 13, 10], 53]) == 5


# Subarrays Distinct Element Sum of Squares II

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

The **distinct count** of a subarray of `nums` is defined as:

* Let `nums[i..j]` be a subarray of `nums` consisting of all the indices from `i` to `j` such that `0 <= i <= j < nums.length`. Then the number of distinct values in `nums[i..j]` is called the distinct count of `nums[i..j]`.

Return *the sum of the **squares** of **distinct counts** of all subarrays of* `nums`.

Since the answer may be very large, return it **modulo** `109 + 7`.

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

**Example 1:**

```

**Input:** nums = [1,2,1]
**Output:** 15
**Explanation:** Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.

```

**Example 2:**

```

**Input:** nums = [2,2]
**Output:** 3
**Explanation:** Three possible subarrays are:
[2]: 1 distinct value
[2]: 1 distinct value
[2,2]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
```

**Constraints:**

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

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def sumCounts(self, nums: List[int]) -> int:
```


# Minimum Sum of Mountain Triplets I

You are given a **0-indexed** array `nums` of integers.

A triplet of indices `(i, j, k)` is a **mountain** if:

* `i < j < k`
* `nums[i] < nums[j]` and `nums[k] < nums[j]`

Return *the **minimum possible sum** of a mountain triplet of* `nums`. *If no such triplet exists, return* `-1`.

**Example 1:**

```

**Input:** nums = [8,6,1,5,3]
**Output:** 9
**Explanation:** Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
- 2 < 3 < 4
- nums[2] < nums[3] and nums[4] < nums[3]
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.

```

**Example 2:**

```

**Input:** nums = [5,4,8,7,10,2]
**Output:** 13
**Explanation:** Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
- 1 < 3 < 5
- nums[1] < nums[3] and nums[5] < nums[3]
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.

```

**Example 3:**

```

**Input:** nums = [6,5,4,3,4,5]
**Output:** -1
**Explanation:** It can be shown that there are no mountain triplets in nums.

```

**Constraints:**

* `3 <= nums.length <= 50`
* `1 <= nums[i] <= 50`

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumSum(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.minimumSum(**[[8, 6, 1, 5, 3]]) == 9
assert my_solution.minimumSum(**[[5, 4, 8, 7, 10, 2]]) == 13
assert my_solution.minimumSum(**[[6, 5, 4, 3, 4, 5]]) == -1
assert my_solution.minimumSum(**[[50, 50, 50]]) == -1
assert my_solution.minimumSum(**[[49, 50, 48]]) == 147
assert my_solution.minimumSum(**[[48, 50, 49]]) == 147
assert my_solution.minimumSum(**[[1, 1, 1]]) == -1
assert my_solution.minimumSum(**[[1, 1, 2]]) == -1
assert my_solution.minimumSum(**[[1, 1, 3]]) == -1
assert my_solution.minimumSum(**[[1, 2, 1]]) == 4
assert my_solution.minimumSum(**[[1, 2, 2]]) == -1
assert my_solution.minimumSum(**[[1, 2, 3]]) == -1
assert my_solution.minimumSum(**[[1, 3, 1]]) == 5
assert my_solution.minimumSum(**[[1, 3, 2]]) == 6
assert my_solution.minimumSum(**[[1, 3, 3]]) == -1
assert my_solution.minimumSum(**[[2, 1, 1]]) == -1
assert my_solution.minimumSum(**[[2, 1, 2]]) == -1
assert my_solution.minimumSum(**[[2, 1, 3]]) == -1
assert my_solution.minimumSum(**[[2, 2, 1]]) == -1
assert my_solution.minimumSum(**[[2, 2, 2]]) == -1


# Minimum Sum of Mountain Triplets II

You are given a **0-indexed** array `nums` of integers.

A triplet of indices `(i, j, k)` is a **mountain** if:

* `i < j < k`
* `nums[i] < nums[j]` and `nums[k] < nums[j]`

Return *the **minimum possible sum** of a mountain triplet of* `nums`. *If no such triplet exists, return* `-1`.

**Example 1:**

```

**Input:** nums = [8,6,1,5,3]
**Output:** 9
**Explanation:** Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
- 2 < 3 < 4
- nums[2] < nums[3] and nums[4] < nums[3]
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.

```

**Example 2:**

```

**Input:** nums = [5,4,8,7,10,2]
**Output:** 13
**Explanation:** Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
- 1 < 3 < 5
- nums[1] < nums[3] and nums[5] < nums[3]
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.

```

**Example 3:**

```

**Input:** nums = [6,5,4,3,4,5]
**Output:** -1
**Explanation:** It can be shown that there are no mountain triplets in nums.

```

**Constraints:**

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

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumSum(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.minimumSum(**[[8, 6, 1, 5, 3]]) == 9
assert my_solution.minimumSum(**[[5, 4, 8, 7, 10, 2]]) == 13
assert my_solution.minimumSum(**[[6, 5, 4, 3, 4, 5]]) == -1
assert my_solution.minimumSum(**[[50, 50, 50]]) == -1
assert my_solution.minimumSum(**[[49, 50, 48]]) == 147
assert my_solution.minimumSum(**[[48, 50, 49]]) == 147
assert my_solution.minimumSum(**[[99999999, 100000000, 99999999]]) == 299999998
assert my_solution.minimumSum(**[[1, 1, 1]]) == -1
assert my_solution.minimumSum(**[[1, 1, 2]]) == -1
assert my_solution.minimumSum(**[[1, 1, 3]]) == -1
assert my_solution.minimumSum(**[[1, 2, 1]]) == 4
assert my_solution.minimumSum(**[[1, 2, 2]]) == -1
assert my_solution.minimumSum(**[[1, 2, 3]]) == -1
assert my_solution.minimumSum(**[[1, 3, 1]]) == 5
assert my_solution.minimumSum(**[[1, 3, 2]]) == 6
assert my_solution.minimumSum(**[[1, 3, 3]]) == -1
assert my_solution.minimumSum(**[[2, 1, 1]]) == -1
assert my_solution.minimumSum(**[[2, 1, 2]]) == -1
assert my_solution.minimumSum(**[[2, 1, 3]]) == -1
assert my_solution.minimumSum(**[[2, 2, 1]]) == -1


# Minimum Number of Groups to Create a Valid Assignment

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

We want to group the indices so for each index `i` in the range `[0, n - 1]`, it is assigned to **exactly one** group.

A groupassignment is **valid** if the following conditions hold:

* For every group `g`, all indices `i` assigned to group `g` have the same value in `nums`.
* For any two groups `g1` and `g2`, the **difference** between the **number of indices** assigned to `g1` and `g2` should **not exceed** `1`.

Return *an integer denoting* *the **minimum** number of groups needed to create a valid group assignment.*

**Example 1:**

```

**Input:** nums = [3,2,3,2,3]
**Output:** 2
**Explanation:** One way the indices can be assigned to 2 groups is as follows, where the values in square brackets are indices:
group 1 -> [0,2,4]
group 2 -> [1,3]
All indices are assigned to one group.
In group 1, nums[0] == nums[2] == nums[4], so all indices have the same value.
In group 2, nums[1] == nums[3], so all indices have the same value.
The number of indices assigned to group 1 is 3, and the number of indices assigned to group 2 is 2.
Their difference doesn't exceed 1.
It is not possible to use fewer than 2 groups because, in order to use just 1 group, all indices assigned to that group must have the same value.
Hence, the answer is 2.
```

**Example 2:**

```

**Input:** nums = [10,10,10,3,1,1]
**Output:** 4
**Explanation:** One way the indices can be assigned to 4 groups is as follows, where the values in square brackets are indices:
group 1 -> [0]
group 2 -> [1,2]
group 3 -> [3]
group 4 -> [4,5]
The group assignment above satisfies both conditions.
It can be shown that it is not possible to create a valid assignment using fewer than 4 groups.
Hence, the answer is 4.
```

**Constraints:**

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

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minGroupsForValidAssignment(self, nums: List[int]) -> int:
```

my_solution = Solution()

assert my_solution.minGroupsForValidAssignment(**[[3, 2, 3, 2, 3]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[10, 10, 10, 3, 1, 1]]) == 4
assert my_solution.minGroupsForValidAssignment(**[[1, 1]]) == 1
assert my_solution.minGroupsForValidAssignment(**[[1, 2]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[1, 3]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[2, 1]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[2, 2]]) == 1
assert my_solution.minGroupsForValidAssignment(**[[3, 1]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[3, 2]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[3, 3]]) == 1
assert my_solution.minGroupsForValidAssignment(**[[3, 4]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[10, 4]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[10, 10]]) == 1
assert my_solution.minGroupsForValidAssignment(**[[14, 15]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[1, 1, 1]]) == 1
assert my_solution.minGroupsForValidAssignment(**[[1, 2, 1]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[1, 7, 10]]) == 3
assert my_solution.minGroupsForValidAssignment(**[[2, 1, 1]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[2, 1, 2]]) == 2
assert my_solution.minGroupsForValidAssignment(**[[2, 2, 1]]) == 2


# Minimum Changes to Make K Semi-palindromes

Given a string `s` and an integer `k`, partition `s` into `k` **substrings** such that the sum of the number of letter changes required to turn each **substring** into a **semi-palindrome** is minimized.

Return *an integer denoting the **minimum** number of letter changes required.*

**Notes**

* A string is a **palindrome** if it can be read the same way from left to right and right to left.
* A string with a length of `len` is considered a **semi-palindrome** if there exists a positive integer `d` such that `1 <= d < len` and `len % d == 0`, and if we take indices that have the same modulo by `d`, they form a **palindrome**. For example, `"aa"`, `"aba"`, `"adbgad"`, and, `"abab"` are **semi-palindrome** and `"a"`, `"ab"`, and, `"abca"` are not.
* A **substring** is a contiguous sequence of characters within a string.

**Example 1:**

```

**Input:** s = "abcac", k = 2
**Output:** 1
**Explanation:** We can divide s into substrings "ab" and "cac". The string "cac" is already a semi-palindrome. If we change "ab" to "aa", it becomes a semi-palindrome with d = 1.
It can be shown that there is no way to divide the string "abcac" into two semi-palindrome substrings. Therefore, the answer would be at least 1.
```

**Example 2:**

```

**Input:** s = "abcdef", k = 2
**Output:** 2
**Explanation:** We can divide it into substrings "abc" and "def". Each of the substrings "abc" and "def" requires one change to become a semi-palindrome, so we need 2 changes in total to make all substrings semi-palindrome.
It can be shown that we cannot divide the given string into two substrings in a way that it would require less than 2 changes.
```

**Example 3:**

```

**Input:** s = "aabbaa", k = 3
**Output:** 0
**Explanation:** We can divide it into substrings "aa", "bb" and "aa".
The strings "aa" and "bb" are already semi-palindromes. Thus, the answer is zero.

```

**Constraints:**

* `2 <= s.length <= 200`
* `1 <= k <= s.length / 2`
* `s` consists only of lowercase English letters.

Please make sure your answer follows the type signature below:

```python3
class Solution:
    def minimumChanges(self, s: str, k: int) -> int:
```

my_solution = Solution()

assert my_solution.minimumChanges(**['abcac', 2]) == 1
assert my_solution.minimumChanges(**['abcdef', 2]) == 2
assert my_solution.minimumChanges(**['aabbaa', 3]) == 0
assert my_solution.minimumChanges(**['aq', 1]) == 1
assert my_solution.minimumChanges(**['bb', 1]) == 0
assert my_solution.minimumChanges(**['aac', 1]) == 1
assert my_solution.minimumChanges(**['abcc', 1]) == 2
assert my_solution.minimumChanges(**['acba', 2]) == 2
assert my_solution.minimumChanges(**['edaswf', 1]) == 2
assert my_solution.minimumChanges(**['aabcbaa', 1]) == 0
assert my_solution.minimumChanges(**['dqpldq', 3]) == 3
assert my_solution.minimumChanges(**['eksddulf', 1]) == 3
assert my_solution.minimumChanges(**['aaaaacabbb', 1]) == 3
assert my_solution.minimumChanges(**['aaabacacbb', 1]) == 3
assert my_solution.minimumChanges(**['abbbbacaaa', 1]) == 3
assert my_solution.minimumChanges(**['abcccbaccb', 1]) == 2
assert my_solution.minimumChanges(**['baacbbbaba', 1]) == 2
assert my_solution.minimumChanges(**['babcbaccba', 1]) == 1
assert my_solution.minimumChanges(**['cabbcabcbc', 1]) == 3
assert my_solution.minimumChanges(**['ccbccaaabb', 1]) == 4