id
stringlengths 13
13
| content
stringlengths 774
4.21k
| test
dict | labels
dict |
---|---|---|---|
LeetCode/3263 | # Divide an Array Into Subarrays With Minimum Cost I
You are given an array of integers `nums` of length `n`.
The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`.
You need to divide `nums` into `3` **disjoint contiguous** subarrays.
Return *the **minimum** possible **sum** of the cost of these subarrays*.
**Example 1:**
```
**Input:** nums = [1,2,3,12]
**Output:** 6
**Explanation:** The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
```
**Example 2:**
```
**Input:** nums = [5,4,3]
**Output:** 12
**Explanation:** The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
```
**Example 3:**
```
**Input:** nums = [10,3,1,1]
**Output:** 12
**Explanation:** The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
```
**Constraints:**
* `3 <= n <= 50`
* `1 <= nums[i] <= 50`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumCost(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*[[1, 2, 3, 12]]) == 6\nassert my_solution.minimumCost(*[[5, 4, 3]]) == 12\nassert my_solution.minimumCost(*[[10, 3, 1, 1]]) == 12\nassert my_solution.minimumCost(*[[1, 1, 1]]) == 3\nassert my_solution.minimumCost(*[[1, 1, 2]]) == 4\nassert my_solution.minimumCost(*[[1, 1, 3]]) == 5\nassert my_solution.minimumCost(*[[1, 1, 4]]) == 6\nassert my_solution.minimumCost(*[[1, 1, 5]]) == 7\nassert my_solution.minimumCost(*[[1, 2, 1]]) == 4\nassert my_solution.minimumCost(*[[1, 2, 2]]) == 5\nassert my_solution.minimumCost(*[[1, 2, 3]]) == 6\nassert my_solution.minimumCost(*[[1, 2, 4]]) == 7\nassert my_solution.minimumCost(*[[1, 2, 5]]) == 8\nassert my_solution.minimumCost(*[[1, 3, 1]]) == 5\nassert my_solution.minimumCost(*[[1, 3, 2]]) == 6\nassert my_solution.minimumCost(*[[1, 3, 3]]) == 7\nassert my_solution.minimumCost(*[[1, 3, 4]]) == 8\nassert my_solution.minimumCost(*[[1, 3, 5]]) == 9\nassert my_solution.minimumCost(*[[1, 4, 1]]) == 6\nassert my_solution.minimumCost(*[[1, 4, 2]]) == 7\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3263",
"questionFrontendId": "3010",
"questionTitle": "Divide an Array Into Subarrays With Minimum Cost I",
"stats": {
"totalAccepted": "2.6K",
"totalSubmission": "3.6K",
"totalAcceptedRaw": 2567,
"totalSubmissionRaw": 3605,
"acRate": "71.2%"
}
} |
LeetCode/3291 | # Find if Array Can Be Sorted
You are given a **0-indexed** array of **positive** integers `nums`.
In one **operation**, you can swap any two **adjacent** elements if they have the **same** number of set bits. You are allowed to do this operation **any** number of times (**including zero**).
Return `true` *if you can sort the array, else return* `false`.
**Example 1:**
```
**Input:** nums = [8,4,2,30,15]
**Output:** true
**Explanation:** Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
We can sort the array using 4 operations:
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
The array has become sorted, hence we return true.
Note that there may be other sequences of operations which also sort the array.
```
**Example 2:**
```
**Input:** nums = [1,2,3,4,5]
**Output:** true
**Explanation:** The array is already sorted, hence we return true.
```
**Example 3:**
```
**Input:** nums = [3,16,8,4,2]
**Output:** false
**Explanation:** It can be shown that it is not possible to sort the input array using any number of operations.
```
**Constraints:**
* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 28`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def canSortArray(self, nums: List[int]) -> bool:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canSortArray(*[[8, 4, 2, 30, 15]]) == True\nassert my_solution.canSortArray(*[[1, 2, 3, 4, 5]]) == True\nassert my_solution.canSortArray(*[[3, 16, 8, 4, 2]]) == False\nassert my_solution.canSortArray(*[[1]]) == True\nassert my_solution.canSortArray(*[[4]]) == True\nassert my_solution.canSortArray(*[[7]]) == True\nassert my_solution.canSortArray(*[[10]]) == True\nassert my_solution.canSortArray(*[[18]]) == True\nassert my_solution.canSortArray(*[[30]]) == True\nassert my_solution.canSortArray(*[[1, 2]]) == True\nassert my_solution.canSortArray(*[[2, 17]]) == True\nassert my_solution.canSortArray(*[[20, 16]]) == False\nassert my_solution.canSortArray(*[[21, 17]]) == False\nassert my_solution.canSortArray(*[[24, 12]]) == True\nassert my_solution.canSortArray(*[[26, 10]]) == False\nassert my_solution.canSortArray(*[[128, 128]]) == True\nassert my_solution.canSortArray(*[[1, 2, 3]]) == True\nassert my_solution.canSortArray(*[[1, 256, 64]]) == True\nassert my_solution.canSortArray(*[[2, 28, 9]]) == False\nassert my_solution.canSortArray(*[[6, 6, 192]]) == True\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3291",
"questionFrontendId": "3011",
"questionTitle": "Find if Array Can Be Sorted",
"stats": {
"totalAccepted": "2.3K",
"totalSubmission": "4.8K",
"totalAcceptedRaw": 2349,
"totalSubmissionRaw": 4798,
"acRate": "49.0%"
}
} |
LeetCode/3244 | # Minimize Length of Array Using Operations
You are given a **0-indexed** integer array `nums` containing **positive** integers.
Your task is to **minimize** the length of `nums` by performing the following operations **any** number of times (including zero):
* Select **two** **distinct** indices `i` and `j` from `nums`, such that `nums[i] > 0` and `nums[j] > 0`.
* Insert the result of `nums[i] % nums[j]` at the end of `nums`.
* Delete the elements at indices `i` and `j` from `nums`.
Return *an integer denoting the **minimum** **length** of* `nums` *after performing the operation any number of times.*
**Example 1:**
```
**Input:** nums = [1,4,3,1]
**Output:** 1
**Explanation:** One way to minimize the length of the array is as follows:
Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1.
nums becomes [1,1,3].
Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2.
nums becomes [1,1].
Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0.
nums becomes [0].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
```
**Example 2:**
```
**Input:** nums = [5,5,5,10,5]
**Output:** 2
**Explanation:** One way to minimize the length of the array is as follows:
Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3.
nums becomes [5,5,5,5].
Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3.
nums becomes [5,5,0].
Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1.
nums becomes [0,0].
The length of nums cannot be reduced further. Hence, the answer is 2.
It can be shown that 2 is the minimum achievable length.
```
**Example 3:**
```
**Input:** nums = [2,3,4]
**Output:** 1
**Explanation:** One way to minimize the length of the array is as follows:
Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2.
nums becomes [2,3].
Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0.
nums becomes [1].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumArrayLength(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumArrayLength(*[[1, 4, 3, 1]]) == 1\nassert my_solution.minimumArrayLength(*[[5, 5, 5, 10, 5]]) == 2\nassert my_solution.minimumArrayLength(*[[2, 3, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[1]]) == 1\nassert my_solution.minimumArrayLength(*[[3]]) == 1\nassert my_solution.minimumArrayLength(*[[6]]) == 1\nassert my_solution.minimumArrayLength(*[[1, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[1, 5]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[3, 4]]) == 1\nassert my_solution.minimumArrayLength(*[[5, 3]]) == 1\nassert my_solution.minimumArrayLength(*[[6, 9]]) == 1\nassert my_solution.minimumArrayLength(*[[8, 2]]) == 1\nassert my_solution.minimumArrayLength(*[[9, 9]]) == 1\nassert my_solution.minimumArrayLength(*[[9, 10]]) == 1\nassert my_solution.minimumArrayLength(*[[1, 2, 5]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 1, 1]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 7, 10]]) == 1\nassert my_solution.minimumArrayLength(*[[2, 9, 5]]) == 1\nassert my_solution.minimumArrayLength(*[[3, 2, 1]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3244",
"questionFrontendId": "3012",
"questionTitle": "Minimize Length of Array Using Operations",
"stats": {
"totalAccepted": "1.5K",
"totalSubmission": "5.4K",
"totalAcceptedRaw": 1523,
"totalSubmissionRaw": 5406,
"acRate": "28.2%"
}
} |
LeetCode/3260 | # Divide an Array Into Subarrays With Minimum Cost II
You are given a **0-indexed** array of integers `nums` of length `n`, and two **positive** integers `k` and `dist`.
The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`.
You need to divide `nums` into `k` **disjoint contiguous** subarrays, such that the difference between the starting index of the **second** subarray and the starting index of the `kth` subarray should be **less than or equal to** `dist`. In other words, if you divide `nums` into the subarrays `nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)]`, then `ik-1 - i1 <= dist`.
Return *the **minimum** possible sum of the cost of these* *subarrays*.
**Example 1:**
```
**Input:** nums = [1,3,2,6,4,2], k = 3, dist = 3
**Output:** 5
**Explanation:** The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
```
**Example 2:**
```
**Input:** nums = [10,1,2,2,2,1], k = 4, dist = 3
**Output:** 15
**Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
```
**Example 3:**
```
**Input:** nums = [10,8,18,9], k = 3, dist = 1
**Output:** 36
**Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
```
**Constraints:**
* `3 <= n <= 105`
* `1 <= nums[i] <= 109`
* `3 <= k <= n`
* `k - 2 <= dist <= n - 2`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumCost(self, nums: List[int], k: int, dist: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*[[1, 3, 2, 6, 4, 2], 3, 3]) == 5\nassert my_solution.minimumCost(*[[10, 1, 2, 2, 2, 1], 4, 3]) == 15\nassert my_solution.minimumCost(*[[10, 8, 18, 9], 3, 1]) == 36\nassert my_solution.minimumCost(*[[1, 1, 1], 3, 1]) == 3\nassert my_solution.minimumCost(*[[1, 1, 3], 3, 1]) == 5\nassert my_solution.minimumCost(*[[1, 2, 2], 3, 1]) == 5\nassert my_solution.minimumCost(*[[1, 2, 5], 3, 1]) == 8\nassert my_solution.minimumCost(*[[1, 4, 4], 3, 1]) == 9\nassert my_solution.minimumCost(*[[2, 2, 1], 3, 1]) == 5\nassert my_solution.minimumCost(*[[2, 3, 2], 3, 1]) == 7\nassert my_solution.minimumCost(*[[2, 5, 4], 3, 1]) == 11\nassert my_solution.minimumCost(*[[3, 1, 2], 3, 1]) == 6\nassert my_solution.minimumCost(*[[3, 1, 3], 3, 1]) == 7\nassert my_solution.minimumCost(*[[3, 2, 2], 3, 1]) == 7\nassert my_solution.minimumCost(*[[3, 3, 2], 3, 1]) == 8\nassert my_solution.minimumCost(*[[3, 4, 1], 3, 1]) == 8\nassert my_solution.minimumCost(*[[3, 5, 3], 3, 1]) == 11\nassert my_solution.minimumCost(*[[4, 1, 4], 3, 1]) == 9\nassert my_solution.minimumCost(*[[4, 1, 5], 3, 1]) == 10\nassert my_solution.minimumCost(*[[4, 2, 1], 3, 1]) == 7\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3260",
"questionFrontendId": "3013",
"questionTitle": "Divide an Array Into Subarrays With Minimum Cost II",
"stats": {
"totalAccepted": "1K",
"totalSubmission": "2.6K",
"totalAcceptedRaw": 1021,
"totalSubmissionRaw": 2604,
"acRate": "39.2%"
}
} |
LeetCode/3242 | # Count Elements With Maximum Frequency
You are given an array `nums` consisting of **positive** integers.
Return *the **total frequencies** of elements in*`nums` *such that those elements all have the **maximum** frequency*.
The **frequency** of an element is the number of occurrences of that element in the array.
**Example 1:**
```
**Input:** nums = [1,2,2,3,1,4]
**Output:** 4
**Explanation:** The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.
```
**Example 2:**
```
**Input:** nums = [1,2,3,4,5]
**Output:** 5
**Explanation:** All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.
```
**Constraints:**
* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maxFrequencyElements(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxFrequencyElements(*[[1, 2, 2, 3, 1, 4]]) == 4\nassert my_solution.maxFrequencyElements(*[[1, 2, 3, 4, 5]]) == 5\nassert my_solution.maxFrequencyElements(*[[15]]) == 1\nassert my_solution.maxFrequencyElements(*[[10, 12, 11, 9, 6, 19, 11]]) == 2\nassert my_solution.maxFrequencyElements(*[[2, 12, 17, 18, 11]]) == 5\nassert my_solution.maxFrequencyElements(*[[19, 19, 19, 20, 19, 8, 19]]) == 5\nassert my_solution.maxFrequencyElements(*[[1, 1, 1, 1]]) == 4\nassert my_solution.maxFrequencyElements(*[[10, 1, 12, 10, 10, 19, 10]]) == 4\nassert my_solution.maxFrequencyElements(*[[1, 1, 1, 20, 6, 1]]) == 4\nassert my_solution.maxFrequencyElements(*[[17, 17]]) == 2\nassert my_solution.maxFrequencyElements(*[[6, 13, 15, 15, 11, 6, 7, 12, 4, 11]]) == 6\nassert my_solution.maxFrequencyElements(*[[1, 2]]) == 2\nassert my_solution.maxFrequencyElements(*[[14, 14, 17]]) == 2\nassert my_solution.maxFrequencyElements(*[[17, 17, 2, 12, 20, 17, 12]]) == 3\nassert my_solution.maxFrequencyElements(*[[3, 9, 11, 11, 20]]) == 2\nassert my_solution.maxFrequencyElements(*[[8, 15, 8, 11, 8, 13, 12, 11, 8]]) == 4\nassert my_solution.maxFrequencyElements(*[[17, 8, 17, 19, 17, 13, 17, 17, 17, 5]]) == 6\nassert my_solution.maxFrequencyElements(*[[11]]) == 1\nassert my_solution.maxFrequencyElements(*[[5]]) == 1\nassert my_solution.maxFrequencyElements(*[[4, 4, 10]]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3242",
"questionFrontendId": "3005",
"questionTitle": "Count Elements With Maximum Frequency",
"stats": {
"totalAccepted": "5.1K",
"totalSubmission": "6.6K",
"totalAcceptedRaw": 5130,
"totalSubmissionRaw": 6572,
"acRate": "78.1%"
}
} |
LeetCode/3245 | # Find Beautiful Indices in the Given Array I
You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`.
An index `i` is **beautiful** if:
* `0 <= i <= s.length - a.length`
* `s[i..(i + a.length - 1)] == a`
* There exists an index `j` such that:
+ `0 <= j <= s.length - b.length`
+ `s[j..(j + b.length - 1)] == b`
+ `|j - i| <= k`
Return *the array that contains beautiful indices in **sorted order from smallest to largest***.
**Example 1:**
```
**Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
**Output:** [16,33]
**Explanation:** There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
```
**Example 2:**
```
**Input:** s = "abcd", a = "a", b = "a", k = 4
**Output:** [0]
**Explanation:** There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
```
**Constraints:**
* `1 <= k <= s.length <= 105`
* `1 <= a.length, b.length <= 10`
* `s`, `a`, and `b` contain only lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulIndices(*['isawsquirrelnearmysquirrelhouseohmy', 'my', 'squirrel', 15]) == [16, 33]\nassert my_solution.beautifulIndices(*['abcd', 'a', 'a', 4]) == [0]\nassert my_solution.beautifulIndices(*['sqgrt', 'rt', 'sq', 3]) == [3]\nassert my_solution.beautifulIndices(*['mquz', 'tklr', 'caz', 4]) == []\nassert my_solution.beautifulIndices(*['wl', 'xjigt', 'wl', 2]) == []\nassert my_solution.beautifulIndices(*['bavgoc', 'ba', 'c', 6]) == [0]\nassert my_solution.beautifulIndices(*['xpcp', 'yxnod', 'xpc', 4]) == []\nassert my_solution.beautifulIndices(*['lahhnlwx', 'hhnlw', 'ty', 6]) == []\nassert my_solution.beautifulIndices(*['dexgscgecd', 'gscge', 'd', 6]) == [3]\nassert my_solution.beautifulIndices(*['vjrao', 'vjr', 'yxpsw', 5]) == []\nassert my_solution.beautifulIndices(*['oo', 'swhup', 'o', 1]) == []\nassert my_solution.beautifulIndices(*['bxlzgxc', 'ducf', 'xlzgx', 3]) == []\nassert my_solution.beautifulIndices(*['wetlgztzm', 'box', 'wetl', 4]) == []\nassert my_solution.beautifulIndices(*['ocmm', 'm', 'oc', 3]) == [2, 3]\nassert my_solution.beautifulIndices(*['goxmox', 'gibs', 'ox', 6]) == []\nassert my_solution.beautifulIndices(*['kzlrqzldvy', 'zl', 'tfsr', 9]) == []\nassert my_solution.beautifulIndices(*['qhd', 'hd', 'od', 1]) == []\nassert my_solution.beautifulIndices(*['bozpeh', 'bozp', 'vrjn', 2]) == []\nassert my_solution.beautifulIndices(*['ggfsg', 'gfsg', 'g', 4]) == [1]\nassert my_solution.beautifulIndices(*['fape', 'vq', 'ap', 4]) == []\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3245",
"questionFrontendId": "3006",
"questionTitle": "Find Beautiful Indices in the Given Array I",
"stats": {
"totalAccepted": "4.1K",
"totalSubmission": "9.5K",
"totalAcceptedRaw": 4101,
"totalSubmissionRaw": 9521,
"acRate": "43.1%"
}
} |
LeetCode/3240 | # Maximum Number That Sum of the Prices Is Less Than or Equal to K
You are given an integer `k` and an integer `x`.
Consider `s` is the **1-indexed** binary representation of an integer `num`. The **price** of a number `num` is the number of `i`'s such that `i % x == 0` and `s[i]` is a **set bit**.
Return *the **greatest** integer* `num` *such that the sum of **prices** of all numbers from* `1` *to* `num` *is less than or equal to* `k`*.*
**Note**:
* In the binary representation of a number **set bit** is a bit of value `1`.
* The binary representation of a number will be indexed from right to left. For example, if `s == 11100`, `s[4] == 1` and `s[2] == 0`.
**Example 1:**
```
**Input:** k = 9, x = 1
**Output:** 6
**Explanation:** The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively.
Since x is equal to 1, the price of each number is the number of its set bits.
The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9.
So the answer is 6.
```
**Example 2:**
```
**Input:** k = 7, x = 2
**Output:** 9
**Explanation:** Since x is equal to 2, we should just check eventh bits.
The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2.
The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2.
The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2.
Numbers 1, 4, and 5 don't have set bits in their eventh bits in their binary representation. So the sum of their prices is 0.
The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2.
The sum of the prices of the first 9 numbers is 6.
Because the sum of the prices of the first 10 numbers is 8, the answer is 9.
```
**Constraints:**
* `1 <= k <= 1015`
* `1 <= x <= 8`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def findMaximumNumber(self, k: int, x: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMaximumNumber(*[9, 1]) == 6\nassert my_solution.findMaximumNumber(*[7, 2]) == 9\nassert my_solution.findMaximumNumber(*[19, 6]) == 50\nassert my_solution.findMaximumNumber(*[57, 4]) == 120\nassert my_solution.findMaximumNumber(*[58, 5]) == 121\nassert my_solution.findMaximumNumber(*[60, 8]) == 187\nassert my_solution.findMaximumNumber(*[72, 5]) == 151\nassert my_solution.findMaximumNumber(*[81, 6]) == 176\nassert my_solution.findMaximumNumber(*[83, 1]) == 33\nassert my_solution.findMaximumNumber(*[83, 7]) == 210\nassert my_solution.findMaximumNumber(*[116, 5]) == 243\nassert my_solution.findMaximumNumber(*[157, 6]) == 316\nassert my_solution.findMaximumNumber(*[201, 3]) == 212\nassert my_solution.findMaximumNumber(*[268, 6]) == 555\nassert my_solution.findMaximumNumber(*[281, 5]) == 531\nassert my_solution.findMaximumNumber(*[283, 3]) == 274\nassert my_solution.findMaximumNumber(*[309, 4]) == 364\nassert my_solution.findMaximumNumber(*[363, 7]) == 746\nassert my_solution.findMaximumNumber(*[409, 2]) == 220\nassert my_solution.findMaximumNumber(*[456, 7]) == 967\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3240",
"questionFrontendId": "3007",
"questionTitle": "Maximum Number That Sum of the Prices Is Less Than or Equal to K",
"stats": {
"totalAccepted": "2.6K",
"totalSubmission": "6.2K",
"totalAcceptedRaw": 2567,
"totalSubmissionRaw": 6194,
"acRate": "41.4%"
}
} |
LeetCode/3303 | # Find Beautiful Indices in the Given Array II
You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`.
An index `i` is **beautiful** if:
* `0 <= i <= s.length - a.length`
* `s[i..(i + a.length - 1)] == a`
* There exists an index `j` such that:
+ `0 <= j <= s.length - b.length`
+ `s[j..(j + b.length - 1)] == b`
+ `|j - i| <= k`
Return *the array that contains beautiful indices in **sorted order from smallest to largest***.
**Example 1:**
```
**Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
**Output:** [16,33]
**Explanation:** There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
```
**Example 2:**
```
**Input:** s = "abcd", a = "a", b = "a", k = 4
**Output:** [0]
**Explanation:** There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
```
**Constraints:**
* `1 <= k <= s.length <= 5 * 105`
* `1 <= a.length, b.length <= 5 * 105`
* `s`, `a`, and `b` contain only lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulIndices(*['isawsquirrelnearmysquirrelhouseohmy', 'my', 'squirrel', 15]) == [16, 33]\nassert my_solution.beautifulIndices(*['abcd', 'a', 'a', 4]) == [0]\nassert my_solution.beautifulIndices(*['a', 'a', 'a', 1]) == [0]\nassert my_solution.beautifulIndices(*['aba', 'a', 'a', 1]) == [0, 2]\nassert my_solution.beautifulIndices(*['nvnvt', 'eq', 'nv', 1]) == []\nassert my_solution.beautifulIndices(*['npearbvede', 'myqpb', 'pearb', 9]) == []\nassert my_solution.beautifulIndices(*['vatevavakz', 'va', 'lbda', 1]) == []\nassert my_solution.beautifulIndices(*['ithhi', 't', 'hhi', 1]) == [1]\nassert my_solution.beautifulIndices(*['osuv', 'osuv', 'wrn', 1]) == []\nassert my_solution.beautifulIndices(*['dc', 'dreec', 'dc', 2]) == []\nassert my_solution.beautifulIndices(*['jajrfw', 'rf', 'j', 3]) == [3]\nassert my_solution.beautifulIndices(*['zcvx', 'kfdvv', 'tru', 1]) == []\nassert my_solution.beautifulIndices(*['wltmqbxt', 'mqbxt', 'lt', 7]) == [3]\nassert my_solution.beautifulIndices(*['gggsytwgzg', 'sytwg', 'g', 4]) == [3]\nassert my_solution.beautifulIndices(*['diive', 'viw', 'lqqdn', 4]) == []\nassert my_solution.beautifulIndices(*['ss', 'omkdt', 's', 1]) == []\nassert my_solution.beautifulIndices(*['hfzoxcm', 'hfzo', 'ipelr', 1]) == []\nassert my_solution.beautifulIndices(*['xllimtmil', 'imt', 'iwqx', 5]) == []\nassert my_solution.beautifulIndices(*['vdyl', 'i', 'ir', 4]) == []\nassert my_solution.beautifulIndices(*['ouwpaz', 'mxre', 'pa', 5]) == []\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3303",
"questionFrontendId": "3008",
"questionTitle": "Find Beautiful Indices in the Given Array II",
"stats": {
"totalAccepted": "2.7K",
"totalSubmission": "9.7K",
"totalAcceptedRaw": 2698,
"totalSubmissionRaw": 9738,
"acRate": "27.7%"
}
} |
LeetCode/3251 | # Maximum Area of Longest Diagonal Rectangle
You are given a 2D **0-indexed** integer array `dimensions`.
For all indices `i`, `0 <= i < dimensions.length`, `dimensions[i][0]` represents the length and `dimensions[i][1]` represents the width of the rectangle `i`.
Return *the **area** of the rectangle having the **longest** diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the **maximum** area.*
**Example 1:**
```
**Input:** dimensions = [[9,3],[8,6]]
**Output:** 48
**Explanation:**
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
```
**Example 2:**
```
**Input:** dimensions = [[3,4],[4,3]]
**Output:** 12
**Explanation:** Length of diagonal is the same for both which is 5, so maximum area = 12.
```
**Constraints:**
* `1 <= dimensions.length <= 100`
* `dimensions[i].length == 2`
* `1 <= dimensions[i][0], dimensions[i][1] <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.areaOfMaxDiagonal(*[[[9, 3], [8, 6]]]) == 48\nassert my_solution.areaOfMaxDiagonal(*[[[3, 4], [4, 3]]]) == 12\nassert my_solution.areaOfMaxDiagonal(*[[[4, 10], [4, 9], [9, 3], [10, 8]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[2, 6], [5, 1], [3, 10], [8, 4]]]) == 30\nassert my_solution.areaOfMaxDiagonal(*[[[3, 7], [2, 10], [3, 4], [9, 9], [5, 10]]]) == 81\nassert my_solution.areaOfMaxDiagonal(*[[[10, 4]]]) == 40\nassert my_solution.areaOfMaxDiagonal(*[[[9, 9], [1, 8], [10, 5], [2, 8], [6, 3], [7, 1]]]) == 81\nassert my_solution.areaOfMaxDiagonal(*[[[10, 3], [5, 9], [8, 3]]]) == 30\nassert my_solution.areaOfMaxDiagonal(*[[[2, 7], [3, 2], [3, 3], [10, 4], [5, 3], [8, 10], [8, 8], [4, 7]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[1, 10], [3, 10], [4, 4], [2, 6], [6, 3], [6, 4], [9, 1], [6, 1], [2, 3]]]) == 30\nassert my_solution.areaOfMaxDiagonal(*[[[4, 7], [10, 10], [3, 7], [9, 1], [5, 7], [3, 9], [10, 4], [4, 8]]]) == 100\nassert my_solution.areaOfMaxDiagonal(*[[[1, 1], [6, 8], [6, 9], [7, 2], [6, 8], [1, 3], [3, 1], [1, 9]]]) == 54\nassert my_solution.areaOfMaxDiagonal(*[[[6, 6], [1, 3], [8, 10], [10, 1], [3, 10], [7, 7], [10, 8]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[6, 5], [8, 6], [2, 10], [8, 1], [9, 2], [3, 5], [3, 5]]]) == 20\nassert my_solution.areaOfMaxDiagonal(*[[[5, 1], [4, 9], [9, 1], [5, 8], [2, 9], [3, 2], [10, 10], [5, 2]]]) == 100\nassert my_solution.areaOfMaxDiagonal(*[[[8, 3], [9, 10], [7, 7], [6, 5], [6, 9], [9, 10], [5, 10]]]) == 90\nassert my_solution.areaOfMaxDiagonal(*[[[6, 10], [8, 6], [10, 1], [7, 10], [10, 10], [9, 5]]]) == 100\nassert my_solution.areaOfMaxDiagonal(*[[[9, 5], [9, 2], [2, 2], [8, 9], [5, 7], [8, 10], [3, 5]]]) == 80\nassert my_solution.areaOfMaxDiagonal(*[[[3, 9], [9, 5]]]) == 45\nassert my_solution.areaOfMaxDiagonal(*[[[10, 10], [5, 5], [3, 2], [2, 6], [3, 1], [10, 7], [4, 8], [7, 9], [9, 9], [1, 2]]]) == 100\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3251",
"questionFrontendId": "3000",
"questionTitle": "Maximum Area of Longest Diagonal Rectangle",
"stats": {
"totalAccepted": "4.7K",
"totalSubmission": "10.8K",
"totalAcceptedRaw": 4667,
"totalSubmissionRaw": 10779,
"acRate": "43.3%"
}
} |
LeetCode/3228 | # Maximum Size of a Set After Removals
You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`.
You must remove `n / 2` elements from `nums1` and `n / 2` elements from `nums2`. After the removals, you insert the remaining elements of `nums1` and `nums2` into a set `s`.
Return *the **maximum** possible size of the set* `s`.
**Example 1:**
```
**Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1]
**Output:** 2
**Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
It can be shown that 2 is the maximum possible size of the set s after the removals.
```
**Example 2:**
```
**Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
**Output:** 5
**Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
It can be shown that 5 is the maximum possible size of the set s after the removals.
```
**Example 3:**
```
**Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
**Output:** 6
**Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
It can be shown that 6 is the maximum possible size of the set s after the removals.
```
**Constraints:**
* `n == nums1.length == nums2.length`
* `1 <= n <= 2 * 104`
* `n` is even.
* `1 <= nums1[i], nums2[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumSetSize(*[[1, 2, 1, 2], [1, 1, 1, 1]]) == 2\nassert my_solution.maximumSetSize(*[[1, 2, 3, 4, 5, 6], [2, 3, 2, 3, 2, 3]]) == 5\nassert my_solution.maximumSetSize(*[[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6]]) == 6\nassert my_solution.maximumSetSize(*[[1, 2, 1, 1], [1, 2, 3, 4]]) == 4\nassert my_solution.maximumSetSize(*[[1, 1, 1, 1], [12, 23, 41, 9]]) == 3\nassert my_solution.maximumSetSize(*[[12, 23, 41, 9], [1, 1, 1, 1]]) == 3\nassert my_solution.maximumSetSize(*[[9, 8, 4, 7], [5, 5, 9, 5]]) == 4\nassert my_solution.maximumSetSize(*[[8, 9], [4, 3]]) == 2\nassert my_solution.maximumSetSize(*[[7, 1], [6, 10]]) == 2\nassert my_solution.maximumSetSize(*[[10, 3], [5, 6]]) == 2\nassert my_solution.maximumSetSize(*[[3, 6], [6, 6]]) == 2\nassert my_solution.maximumSetSize(*[[5, 1], [6, 6]]) == 2\nassert my_solution.maximumSetSize(*[[10, 7], [8, 4]]) == 2\nassert my_solution.maximumSetSize(*[[10, 8, 7, 9], [7, 9, 9, 5]]) == 4\nassert my_solution.maximumSetSize(*[[1, 10, 6, 5], [3, 7, 10, 10]]) == 4\nassert my_solution.maximumSetSize(*[[5, 2, 8, 6], [7, 4, 1, 1]]) == 4\nassert my_solution.maximumSetSize(*[[2, 4, 1, 4], [10, 2, 4, 10]]) == 4\nassert my_solution.maximumSetSize(*[[5, 7], [3, 1]]) == 2\nassert my_solution.maximumSetSize(*[[1, 10, 1, 2], [9, 5, 8, 5]]) == 4\nassert my_solution.maximumSetSize(*[[9, 4], [5, 7]]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3228",
"questionFrontendId": "3002",
"questionTitle": "Maximum Size of a Set After Removals",
"stats": {
"totalAccepted": "3.5K",
"totalSubmission": "6.9K",
"totalAcceptedRaw": 3537,
"totalSubmissionRaw": 6917,
"acRate": "51.1%"
}
} |
LeetCode/3233 | # Maximize the Number of Partitions After Operations
You are given a **0-indexed** string `s` and an integer `k`.
You are to perform the following partitioning operations until `s` is **empty**:
* Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters.
* **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order.
**Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter.
Return *an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change.*
**Example 1:**
```
**Input:** s = "accca", k = 2
**Output:** 3
**Explanation:** In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'.
s becomes "acbca".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 2 distinct characters, "acbca".
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
- Choose the longest prefix containing at most 2 distinct characters, "bca".
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
- Choose the longest prefix containing at most 2 distinct characters, "a".
- Delete the prefix, and s becomes empty. The number of partitions is now 3.
Hence, the answer is 3.
It can be shown that it is not possible to obtain more than 3 partitions.
```
**Example 2:**
```
**Input:** s = "aabaab", k = 3
**Output:** 1
**Explanation:** In this example, to maximize the number of resulting partitions we can leave s as it is.
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 3 distinct characters, "aabaab".
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.
Hence, the answer is 1.
It can be shown that it is not possible to obtain more than 1 partition.
```
**Example 3:**
```
**Input:** s = "xxyz", k = 1
**Output:** 4
**Explanation:** In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'.
s becomes "xayz".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 1 distinct character, "xayz".
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
- Choose the longest prefix containing at most 1 distinct character, "ayz".
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
- Choose the longest prefix containing at most 1 distinct character, "yz".
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
- Choose the longest prefix containing at most 1 distinct character, "z".
- Delete the prefix, and s becomes empty. The number of partitions is now 4.
Hence, the answer is 4.
It can be shown that it is not possible to obtain more than 4 partitions.
```
**Constraints:**
* `1 <= s.length <= 104`
* `s` consists only of lowercase English letters.
* `1 <= k <= 26`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maxPartitionsAfterOperations(self, s: str, k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxPartitionsAfterOperations(*['accca', 2]) == 3\nassert my_solution.maxPartitionsAfterOperations(*['aabaab', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['xxyz', 1]) == 4\nassert my_solution.maxPartitionsAfterOperations(*['c', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['c', 5]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['h', 17]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['p', 13]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['ab', 5]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['ba', 1]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['ba', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['ca', 1]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['fh', 8]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['abb', 1]) == 3\nassert my_solution.maxPartitionsAfterOperations(*['aca', 2]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['acb', 2]) == 2\nassert my_solution.maxPartitionsAfterOperations(*['acb', 4]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['bab', 3]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['cba', 1]) == 3\nassert my_solution.maxPartitionsAfterOperations(*['cbb', 5]) == 1\nassert my_solution.maxPartitionsAfterOperations(*['cca', 5]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3233",
"questionFrontendId": "3003",
"questionTitle": "Maximize the Number of Partitions After Operations",
"stats": {
"totalAccepted": "1.3K",
"totalSubmission": "4K",
"totalAcceptedRaw": 1261,
"totalSubmissionRaw": 4041,
"acRate": "31.2%"
}
} |
LeetCode/3236 | # Smallest Missing Integer Greater Than Sequential Prefix Sum
You are given a **0-indexed** array of integers `nums`.
A prefix `nums[0..i]` is **sequential** if, for all `1 <= j <= i`, `nums[j] = nums[j - 1] + 1`. In particular, the prefix consisting only of `nums[0]` is **sequential**.
Return *the **smallest** integer* `x` *missing from* `nums` *such that* `x` *is greater than or equal to the sum of the **longest** sequential prefix.*
**Example 1:**
```
**Input:** nums = [1,2,3,2,5]
**Output:** 6
**Explanation:** The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
```
**Example 2:**
```
**Input:** nums = [3,4,5,1,12,14,13]
**Output:** 15
**Explanation:** The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
```
**Constraints:**
* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 50`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def missingInteger(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.missingInteger(*[[1, 2, 3, 2, 5]]) == 6\nassert my_solution.missingInteger(*[[3, 4, 5, 1, 12, 14, 13]]) == 15\nassert my_solution.missingInteger(*[[29, 30, 31, 32, 33, 34, 35, 36, 37]]) == 297\nassert my_solution.missingInteger(*[[19, 20, 21, 22]]) == 82\nassert my_solution.missingInteger(*[[18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 9]]) == 253\nassert my_solution.missingInteger(*[[4, 5, 6, 7, 8, 8, 9, 4, 3, 2, 7]]) == 30\nassert my_solution.missingInteger(*[[38]]) == 39\nassert my_solution.missingInteger(*[[1]]) == 2\nassert my_solution.missingInteger(*[[11, 12, 13]]) == 36\nassert my_solution.missingInteger(*[[47, 48, 49, 5, 3]]) == 144\nassert my_solution.missingInteger(*[[23, 24, 25, 4, 5, 1]]) == 72\nassert my_solution.missingInteger(*[[8, 9, 10, 10, 7, 8]]) == 27\nassert my_solution.missingInteger(*[[31, 32, 33, 34, 10, 8, 7, 9, 7, 9, 9, 5, 10, 1]]) == 130\nassert my_solution.missingInteger(*[[17, 18, 19, 20, 21, 22, 3, 7, 10, 10]]) == 117\nassert my_solution.missingInteger(*[[6, 7, 8, 9, 10, 8, 6, 7, 4, 1]]) == 40\nassert my_solution.missingInteger(*[[46, 8, 2, 4, 1, 4, 10, 2, 4, 10, 2, 5, 7, 3, 1]]) == 47\nassert my_solution.missingInteger(*[[37, 1, 2, 9, 5, 8, 5, 2, 9, 4]]) == 38\nassert my_solution.missingInteger(*[[31, 32, 33, 34, 1]]) == 130\nassert my_solution.missingInteger(*[[45, 46, 47, 48, 49, 10, 8, 1, 7, 4, 10, 10, 6, 6, 2]]) == 235\nassert my_solution.missingInteger(*[[13, 10, 7, 5, 7, 10, 6, 10, 2]]) == 14\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3236",
"questionFrontendId": "2996",
"questionTitle": "Smallest Missing Integer Greater Than Sequential Prefix Sum",
"stats": {
"totalAccepted": "2.7K",
"totalSubmission": "7.5K",
"totalAcceptedRaw": 2718,
"totalSubmissionRaw": 7537,
"acRate": "36.1%"
}
} |
LeetCode/3249 | # Minimum Number of Operations to Make Array XOR Equal to K
You are given a **0-indexed** integer array `nums` and a positive integer `k`.
You can apply the following operation on the array **any** number of times:
* Choose **any** element of the array and **flip** a bit in its **binary** representation. Flipping a bit means changing a `0` to `1` or vice versa.
Return *the **minimum** number of operations required to make the bitwise* `XOR` *of **all** elements of the final array equal to* `k`.
**Note** that you can flip leading zero bits in the binary representation of elements. For example, for the number `(101)2` you can flip the fourth bit and obtain `(1101)2`.
**Example 1:**
```
**Input:** nums = [2,1,3,4], k = 1
**Output:** 2
**Explanation:** We can do the following operations:
- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
It can be shown that we cannot make the XOR equal to k in less than 2 operations.
```
**Example 2:**
```
**Input:** nums = [2,0,2,0], k = 0
**Output:** 0
**Explanation:** The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `0 <= nums[i] <= 106`
* `0 <= k <= 106`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minOperations(*[[2, 1, 3, 4], 1]) == 2\nassert my_solution.minOperations(*[[2, 0, 2, 0], 0]) == 0\nassert my_solution.minOperations(*[[4], 7]) == 2\nassert my_solution.minOperations(*[[3, 13, 9, 8, 5, 18, 11, 10], 13]) == 2\nassert my_solution.minOperations(*[[9, 7, 9, 14, 8, 6], 12]) == 3\nassert my_solution.minOperations(*[[13, 9, 10, 16, 11, 8, 1], 17]) == 3\nassert my_solution.minOperations(*[[12, 14], 1]) == 2\nassert my_solution.minOperations(*[[18, 18], 20]) == 2\nassert my_solution.minOperations(*[[3, 5, 1, 1], 19]) == 3\nassert my_solution.minOperations(*[[7, 0, 0, 0], 8]) == 4\nassert my_solution.minOperations(*[[13, 15, 19, 18, 2, 9, 18, 11, 0, 7], 6]) == 1\nassert my_solution.minOperations(*[[9, 15, 19, 15, 10, 15, 14, 0, 2, 5], 20]) == 1\nassert my_solution.minOperations(*[[19, 4, 19, 6, 3, 19, 14, 4, 16, 12], 4]) == 0\nassert my_solution.minOperations(*[[2, 10, 5, 5, 12, 3, 14, 6, 11, 14], 3]) == 2\nassert my_solution.minOperations(*[[11, 20], 10]) == 3\nassert my_solution.minOperations(*[[10, 12, 5, 3, 16, 0], 1]) == 2\nassert my_solution.minOperations(*[[0, 4, 4, 7, 14, 13], 1]) == 2\nassert my_solution.minOperations(*[[16, 2, 20, 13, 15, 20, 13], 16]) == 3\nassert my_solution.minOperations(*[[19, 11, 11, 0, 16, 2, 2, 0, 9], 4]) == 3\nassert my_solution.minOperations(*[[10, 17, 19, 8, 15], 19]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3249",
"questionFrontendId": "2997",
"questionTitle": "Minimum Number of Operations to Make Array XOR Equal to K",
"stats": {
"totalAccepted": "2.5K",
"totalSubmission": "3K",
"totalAcceptedRaw": 2485,
"totalSubmissionRaw": 2957,
"acRate": "84.0%"
}
} |
LeetCode/3239 | # Minimum Number of Operations to Make X and Y Equal
You are given two positive integers `x` and `y`.
In one operation, you can do one of the four following operations:
1. Divide `x` by `11` if `x` is a multiple of `11`.
2. Divide `x` by `5` if `x` is a multiple of `5`.
3. Decrement `x` by `1`.
4. Increment `x` by `1`.
Return *the **minimum** number of operations required to make* `x` *and* `y` equal.
**Example 1:**
```
**Input:** x = 26, y = 1
**Output:** 3
**Explanation:** We can make 26 equal to 1 by applying the following operations:
1. Decrement x by 1
2. Divide x by 5
3. Divide x by 5
It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.
```
**Example 2:**
```
**Input:** x = 54, y = 2
**Output:** 4
**Explanation:** We can make 54 equal to 2 by applying the following operations:
1. Increment x by 1
2. Divide x by 11
3. Divide x by 5
4. Increment x by 1
It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.
```
**Example 3:**
```
**Input:** x = 25, y = 30
**Output:** 5
**Explanation:** We can make 25 equal to 30 by applying the following operations:
1. Increment x by 1
2. Increment x by 1
3. Increment x by 1
4. Increment x by 1
5. Increment x by 1
It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.
```
**Constraints:**
* `1 <= x, y <= 104`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumOperationsToMakeEqual(*[26, 1]) == 3\nassert my_solution.minimumOperationsToMakeEqual(*[54, 2]) == 4\nassert my_solution.minimumOperationsToMakeEqual(*[25, 30]) == 5\nassert my_solution.minimumOperationsToMakeEqual(*[1, 1]) == 0\nassert my_solution.minimumOperationsToMakeEqual(*[1, 2]) == 1\nassert my_solution.minimumOperationsToMakeEqual(*[1, 3]) == 2\nassert my_solution.minimumOperationsToMakeEqual(*[1, 4]) == 3\nassert my_solution.minimumOperationsToMakeEqual(*[1, 5]) == 4\nassert my_solution.minimumOperationsToMakeEqual(*[1, 6]) == 5\nassert my_solution.minimumOperationsToMakeEqual(*[1, 7]) == 6\nassert my_solution.minimumOperationsToMakeEqual(*[1, 8]) == 7\nassert my_solution.minimumOperationsToMakeEqual(*[1, 9]) == 8\nassert my_solution.minimumOperationsToMakeEqual(*[1, 10]) == 9\nassert my_solution.minimumOperationsToMakeEqual(*[1, 11]) == 10\nassert my_solution.minimumOperationsToMakeEqual(*[1, 12]) == 11\nassert my_solution.minimumOperationsToMakeEqual(*[1, 13]) == 12\nassert my_solution.minimumOperationsToMakeEqual(*[1, 14]) == 13\nassert my_solution.minimumOperationsToMakeEqual(*[1, 15]) == 14\nassert my_solution.minimumOperationsToMakeEqual(*[1, 16]) == 15\nassert my_solution.minimumOperationsToMakeEqual(*[1, 17]) == 16\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3239",
"questionFrontendId": "2998",
"questionTitle": "Minimum Number of Operations to Make X and Y Equal",
"stats": {
"totalAccepted": "2.6K",
"totalSubmission": "5.7K",
"totalAcceptedRaw": 2596,
"totalSubmissionRaw": 5655,
"acRate": "45.9%"
}
} |
LeetCode/3243 | # Count the Number of Powerful Integers
You are given three integers `start`, `finish`, and `limit`. You are also given a **0-indexed** string `s` representing a **positive** integer.
A **positive** integer `x` is called **powerful** if it ends with `s` (in other words, `s` is a **suffix** of `x`) and each digit in `x` is at most `limit`.
Return *the **total** number of powerful integers in the range* `[start..finish]`.
A string `x` is a suffix of a string `y` if and only if `x` is a substring of `y` that starts from some index (**including** `0`) in `y` and extends to the index `y.length - 1`. For example, `25` is a suffix of `5125` whereas `512` is not.
**Example 1:**
```
**Input:** start = 1, finish = 6000, limit = 4, s = "124"
**Output:** 5
**Explanation:** The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
It can be shown that there are only 5 powerful integers in this range.
```
**Example 2:**
```
**Input:** start = 15, finish = 215, limit = 6, s = "10"
**Output:** 2
**Explanation:** The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
It can be shown that there are only 2 powerful integers in this range.
```
**Example 3:**
```
**Input:** start = 1000, finish = 2000, limit = 4, s = "3000"
**Output:** 0
**Explanation:** All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
```
**Constraints:**
* `1 <= start <= finish <= 1015`
* `1 <= limit <= 9`
* `1 <= s.length <= floor(log10(finish)) + 1`
* `s` only consists of numeric digits which are at most `limit`.
* `s` does not have leading zeros.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfPowerfulInt(*[1, 6000, 4, '124']) == 5\nassert my_solution.numberOfPowerfulInt(*[15, 215, 6, '10']) == 2\nassert my_solution.numberOfPowerfulInt(*[1000, 2000, 4, '3000']) == 0\nassert my_solution.numberOfPowerfulInt(*[141, 148, 9, '9']) == 0\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '17']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '27']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '41']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '47']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '61']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '66']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '71']) == 10\nassert my_solution.numberOfPowerfulInt(*[1, 971, 9, '72']) == 9\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '20']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '24']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '32']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '33']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '40']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '41']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '42']) == 8\nassert my_solution.numberOfPowerfulInt(*[20, 1159, 5, '43']) == 8\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3243",
"questionFrontendId": "2999",
"questionTitle": "Count the Number of Powerful Integers",
"stats": {
"totalAccepted": "1.7K",
"totalSubmission": "4.5K",
"totalAcceptedRaw": 1748,
"totalSubmissionRaw": 4535,
"acRate": "38.5%"
}
} |
LeetCode/3246 | # Check if Bitwise OR Has Trailing Zeros
You are given an array of **positive** integers `nums`.
You have to check if it is possible to select **two or more** elements in the array such that the bitwise `OR` of the selected elements has **at least** one trailing zero in its binary representation.
For example, the binary representation of `5`, which is `"101"`, does not have any trailing zeros, whereas the binary representation of `4`, which is `"100"`, has two trailing zeros.
Return `true` *if it is possible to select two or more elements whose bitwise* `OR` *has trailing zeros, return* `false` *otherwise*.
**Example 1:**
```
**Input:** nums = [1,2,3,4,5]
**Output:** true
**Explanation:** If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
```
**Example 2:**
```
**Input:** nums = [2,4,8,16]
**Output:** true
**Explanation:** If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).
```
**Example 3:**
```
**Input:** nums = [1,3,5,7,9]
**Output:** false
**Explanation:** There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
```
**Constraints:**
* `2 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def hasTrailingZeros(self, nums: List[int]) -> bool:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.hasTrailingZeros(*[[1, 2, 3, 4, 5]]) == True\nassert my_solution.hasTrailingZeros(*[[2, 4, 8, 16]]) == True\nassert my_solution.hasTrailingZeros(*[[1, 3, 5, 7, 9]]) == False\nassert my_solution.hasTrailingZeros(*[[1, 2]]) == False\nassert my_solution.hasTrailingZeros(*[[1, 3]]) == False\nassert my_solution.hasTrailingZeros(*[[1, 7]]) == False\nassert my_solution.hasTrailingZeros(*[[2, 2]]) == True\nassert my_solution.hasTrailingZeros(*[[3, 3]]) == False\nassert my_solution.hasTrailingZeros(*[[3, 4]]) == False\nassert my_solution.hasTrailingZeros(*[[3, 9]]) == False\nassert my_solution.hasTrailingZeros(*[[4, 3]]) == False\nassert my_solution.hasTrailingZeros(*[[4, 8]]) == True\nassert my_solution.hasTrailingZeros(*[[4, 32]]) == True\nassert my_solution.hasTrailingZeros(*[[5, 6]]) == False\nassert my_solution.hasTrailingZeros(*[[6, 2]]) == True\nassert my_solution.hasTrailingZeros(*[[6, 8]]) == True\nassert my_solution.hasTrailingZeros(*[[7, 9]]) == False\nassert my_solution.hasTrailingZeros(*[[7, 10]]) == False\nassert my_solution.hasTrailingZeros(*[[8, 2]]) == True\nassert my_solution.hasTrailingZeros(*[[8, 4]]) == True\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3246",
"questionFrontendId": "2980",
"questionTitle": "Check if Bitwise OR Has Trailing Zeros",
"stats": {
"totalAccepted": "4.3K",
"totalSubmission": "5.7K",
"totalAcceptedRaw": 4315,
"totalSubmissionRaw": 5721,
"acRate": "75.4%"
}
} |
LeetCode/3267 | # Find Longest Special Substring That Occurs Thrice I
You are given a string `s` that consists of lowercase English letters.
A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special.
Return *the length of the **longest special substring** of* `s` *which occurs **at least thrice***, *or* `-1` *if no special substring occurs at least thrice*.
A **substring** is a contiguous **non-empty** sequence of characters within a string.
**Example 1:**
```
**Input:** s = "aaaa"
**Output:** 2
**Explanation:** The longest special substring which occurs thrice is "aa": substrings "**aa**aa", "a**aa**a", and "aa**aa**".
It can be shown that the maximum length achievable is 2.
```
**Example 2:**
```
**Input:** s = "abcdef"
**Output:** -1
**Explanation:** There exists no special substring which occurs at least thrice. Hence return -1.
```
**Example 3:**
```
**Input:** s = "abcaba"
**Output:** 1
**Explanation:** The longest special substring which occurs thrice is "a": substrings "**a**bcaba", "abc**a**ba", and "abcab**a**".
It can be shown that the maximum length achievable is 1.
```
**Constraints:**
* `3 <= s.length <= 50`
* `s` consists of only lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumLength(self, s: str) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumLength(*['aaaa']) == 2\nassert my_solution.maximumLength(*['abcdef']) == -1\nassert my_solution.maximumLength(*['abcaba']) == 1\nassert my_solution.maximumLength(*['abcccccdddd']) == 3\nassert my_solution.maximumLength(*['aaa']) == 1\nassert my_solution.maximumLength(*['acc']) == -1\nassert my_solution.maximumLength(*['cab']) == -1\nassert my_solution.maximumLength(*['cad']) == -1\nassert my_solution.maximumLength(*['cbc']) == -1\nassert my_solution.maximumLength(*['ccc']) == 1\nassert my_solution.maximumLength(*['dca']) == -1\nassert my_solution.maximumLength(*['ddd']) == 1\nassert my_solution.maximumLength(*['fff']) == 1\nassert my_solution.maximumLength(*['ggg']) == 1\nassert my_solution.maximumLength(*['hhh']) == 1\nassert my_solution.maximumLength(*['kkk']) == 1\nassert my_solution.maximumLength(*['lll']) == 1\nassert my_solution.maximumLength(*['ooo']) == 1\nassert my_solution.maximumLength(*['ppp']) == 1\nassert my_solution.maximumLength(*['qqq']) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3267",
"questionFrontendId": "2981",
"questionTitle": "Find Longest Special Substring That Occurs Thrice I",
"stats": {
"totalAccepted": "3.5K",
"totalSubmission": "7.4K",
"totalAcceptedRaw": 3541,
"totalSubmissionRaw": 7356,
"acRate": "48.1%"
}
} |
LeetCode/3266 | # Find Longest Special Substring That Occurs Thrice II
You are given a string `s` that consists of lowercase English letters.
A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special.
Return *the length of the **longest special substring** of* `s` *which occurs **at least thrice***, *or* `-1` *if no special substring occurs at least thrice*.
A **substring** is a contiguous **non-empty** sequence of characters within a string.
**Example 1:**
```
**Input:** s = "aaaa"
**Output:** 2
**Explanation:** The longest special substring which occurs thrice is "aa": substrings "**aa**aa", "a**aa**a", and "aa**aa**".
It can be shown that the maximum length achievable is 2.
```
**Example 2:**
```
**Input:** s = "abcdef"
**Output:** -1
**Explanation:** There exists no special substring which occurs at least thrice. Hence return -1.
```
**Example 3:**
```
**Input:** s = "abcaba"
**Output:** 1
**Explanation:** The longest special substring which occurs thrice is "a": substrings "**a**bcaba", "abc**a**ba", and "abcab**a**".
It can be shown that the maximum length achievable is 1.
```
**Constraints:**
* `3 <= s.length <= 5 * 105`
* `s` consists of only lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumLength(self, s: str) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumLength(*['aaaa']) == 2\nassert my_solution.maximumLength(*['abcdef']) == -1\nassert my_solution.maximumLength(*['abcaba']) == 1\nassert my_solution.maximumLength(*['abcccccdddd']) == 3\nassert my_solution.maximumLength(*['acd']) == -1\nassert my_solution.maximumLength(*['bad']) == -1\nassert my_solution.maximumLength(*['bbc']) == -1\nassert my_solution.maximumLength(*['ccc']) == 1\nassert my_solution.maximumLength(*['cda']) == -1\nassert my_solution.maximumLength(*['dab']) == -1\nassert my_solution.maximumLength(*['ddd']) == 1\nassert my_solution.maximumLength(*['eee']) == 1\nassert my_solution.maximumLength(*['fff']) == 1\nassert my_solution.maximumLength(*['hhh']) == 1\nassert my_solution.maximumLength(*['jjj']) == 1\nassert my_solution.maximumLength(*['kkk']) == 1\nassert my_solution.maximumLength(*['lll']) == 1\nassert my_solution.maximumLength(*['mmm']) == 1\nassert my_solution.maximumLength(*['nnn']) == 1\nassert my_solution.maximumLength(*['ooo']) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3266",
"questionFrontendId": "2982",
"questionTitle": "Find Longest Special Substring That Occurs Thrice II",
"stats": {
"totalAccepted": "3.9K",
"totalSubmission": "10.1K",
"totalAcceptedRaw": 3897,
"totalSubmissionRaw": 10118,
"acRate": "38.5%"
}
} |
LeetCode/3203 | # Palindrome Rearrangement Queries
You are given a **0-indexed** string `s` having an **even** length `n`.
You are also given a **0-indexed** 2D integer array, `queries`, where `queries[i] = [ai, bi, ci, di]`.
For each query `i`, you are allowed to perform the following operations:
* Rearrange the characters within the **substring** `s[ai:bi]`, where `0 <= ai <= bi < n / 2`.
* Rearrange the characters within the **substring** `s[ci:di]`, where `n / 2 <= ci <= di < n`.
For each query, your task is to determine whether it is possible to make `s` a **palindrome** by performing the operations.
Each query is answered **independently** of the others.
Return *a **0-indexed** array* `answer`*, where* `answer[i] == true` *if it is possible to make* `s` *a palindrome by performing operations specified by the* `ith` *query, and* `false` *otherwise.*
* A **substring** is a contiguous sequence of characters within a string.
* `s[x:y]` represents the substring consisting of characters from the index `x` to index `y` in `s`, **both inclusive**.
**Example 1:**
```
**Input:** s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]]
**Output:** [true,true]
**Explanation:** In this example, there are two queries:
In the first query:
- a0 = 1, b0 = 1, c0 = 3, d0 = 5.
- So, you are allowed to rearrange s[1:1] => abcabc and s[3:5] => abcabc.
- To make s a palindrome, s[3:5] can be rearranged to become => abccba.
- Now, s is a palindrome. So, answer[0] = true.
In the second query:
- a1 = 0, b1 = 2, c1 = 5, d1 = 5.
- So, you are allowed to rearrange s[0:2] => abcabc and s[5:5] => abcabc.
- To make s a palindrome, s[0:2] can be rearranged to become => cbaabc.
- Now, s is a palindrome. So, answer[1] = true.
```
**Example 2:**
```
**Input:** s = "abbcdecbba", queries = [[0,2,7,9]]
**Output:** [false]
**Explanation:** In this example, there is only one query.
a0 = 0, b0 = 2, c0 = 7, d0 = 9.
So, you are allowed to rearrange s[0:2] => abbcdecbba and s[7:9] => abbcdecbba.
It is not possible to make s a palindrome by rearranging these substrings because s[3:6] is not a palindrome.
So, answer[0] = false.
```
**Example 3:**
```
**Input:** s = "acbcab", queries = [[1,2,4,5]]
**Output:** [true]
**Explanation:** In this example, there is only one query.
a0 = 1, b0 = 2, c0 = 4, d0 = 5.
So, you are allowed to rearrange s[1:2] => acbcab and s[4:5] => acbcab.
To make s a palindrome s[1:2] can be rearranged to become abccab.
Then, s[4:5] can be rearranged to become abccba.
Now, s is a palindrome. So, answer[0] = true.
```
**Constraints:**
* `2 <= n == s.length <= 105`
* `1 <= queries.length <= 105`
* `queries[i].length == 4`
* `ai == queries[i][0], bi == queries[i][1]`
* `ci == queries[i][2], di == queries[i][3]`
* `0 <= ai <= bi < n / 2`
* `n / 2 <= ci <= di < n`
* `n` is even.
* `s` consists of only lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.canMakePalindromeQueries(*['abcabc', [[1, 1, 3, 5], [0, 2, 5, 5]]]) == [True, True]\nassert my_solution.canMakePalindromeQueries(*['abbcdecbba', [[0, 2, 7, 9]]]) == [False]\nassert my_solution.canMakePalindromeQueries(*['acbcab', [[1, 2, 4, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bb', [[0, 0, 1, 1]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['dd', [[0, 0, 1, 1]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bdbd', [[0, 0, 2, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['eeee', [[0, 1, 2, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bbccbb', [[0, 1, 4, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bcdbdc', [[1, 2, 3, 3]]]) == [False]\nassert my_solution.canMakePalindromeQueries(*['cababc', [[1, 2, 3, 4]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['cbbbbc', [[1, 1, 5, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['cdbdbc', [[1, 2, 3, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ceacea', [[0, 2, 3, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['daeaed', [[0, 2, 3, 3]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ddaadd', [[0, 2, 3, 4]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ddedde', [[0, 2, 4, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['ecbbce', [[0, 1, 3, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['eczecz', [[0, 0, 3, 5]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['mpepem', [[0, 2, 3, 4]]]) == [True]\nassert my_solution.canMakePalindromeQueries(*['bccacacb', [[3, 3, 4, 7]]]) == [True]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3203",
"questionFrontendId": "2983",
"questionTitle": "Palindrome Rearrangement Queries",
"stats": {
"totalAccepted": "1.2K",
"totalSubmission": "3.9K",
"totalAcceptedRaw": 1243,
"totalSubmissionRaw": 3947,
"acRate": "31.5%"
}
} |
LeetCode/3226 | # Minimum Number Game
You are given a **0-indexed** integer array `nums` of **even** length and there is also an empty array `arr`. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:
* Every round, first Alice will remove the **minimum** element from `nums`, and then Bob does the same.
* Now, first Bob will append the removed element in the array `arr`, and then Alice does the same.
* The game continues until `nums` becomes empty.
Return *the resulting array* `arr`.
**Example 1:**
```
**Input:** nums = [5,4,2,3]
**Output:** [3,2,5,4]
**Explanation:** In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].
```
**Example 2:**
```
**Input:** nums = [2,5]
**Output:** [5,2]
**Explanation:** In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].
```
**Constraints:**
* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
* `nums.length % 2 == 0`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberGame(*[[5, 4, 2, 3]]) == [3, 2, 5, 4]\nassert my_solution.numberGame(*[[2, 5]]) == [5, 2]\nassert my_solution.numberGame(*[[4, 4, 3, 8]]) == [4, 3, 8, 4]\nassert my_solution.numberGame(*[[2, 5, 3, 8]]) == [3, 2, 8, 5]\nassert my_solution.numberGame(*[[2, 7, 9, 6, 4, 6]]) == [4, 2, 6, 6, 9, 7]\nassert my_solution.numberGame(*[[18, 26, 37, 46, 13, 33, 39, 1, 37, 16]]) == [13, 1, 18, 16, 33, 26, 37, 37, 46, 39]\nassert my_solution.numberGame(*[[17, 2, 4, 11, 14, 18]]) == [4, 2, 14, 11, 18, 17]\nassert my_solution.numberGame(*[[20, 30, 12, 3, 11, 17, 32, 12]]) == [11, 3, 12, 12, 20, 17, 32, 30]\nassert my_solution.numberGame(*[[9, 32, 6, 11, 11, 39, 18, 29, 44, 29]]) == [9, 6, 11, 11, 29, 18, 32, 29, 44, 39]\nassert my_solution.numberGame(*[[7, 2, 3, 4]]) == [3, 2, 7, 4]\nassert my_solution.numberGame(*[[8, 7, 1, 3]]) == [3, 1, 8, 7]\nassert my_solution.numberGame(*[[2, 6, 6, 6]]) == [6, 2, 6, 6]\nassert my_solution.numberGame(*[[1, 2]]) == [2, 1]\nassert my_solution.numberGame(*[[4, 1, 1, 3]]) == [1, 1, 4, 3]\nassert my_solution.numberGame(*[[13, 12, 18, 11, 15, 28, 26, 2]]) == [11, 2, 13, 12, 18, 15, 28, 26]\nassert my_solution.numberGame(*[[14, 30, 29, 3, 23, 21, 26, 23]]) == [14, 3, 23, 21, 26, 23, 30, 29]\nassert my_solution.numberGame(*[[1, 1]]) == [1, 1]\nassert my_solution.numberGame(*[[2, 1]]) == [2, 1]\nassert my_solution.numberGame(*[[12, 1, 28, 23, 2, 31, 11, 26]]) == [2, 1, 12, 11, 26, 23, 31, 28]\nassert my_solution.numberGame(*[[21, 11, 37, 1, 40, 50, 49, 45, 28, 47]]) == [11, 1, 28, 21, 40, 37, 47, 45, 50, 49]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3226",
"questionFrontendId": "2974",
"questionTitle": "Minimum Number Game",
"stats": {
"totalAccepted": "5.8K",
"totalSubmission": "6.7K",
"totalAcceptedRaw": 5847,
"totalSubmissionRaw": 6669,
"acRate": "87.7%"
}
} |
LeetCode/3235 | # Minimum Cost to Convert String I
You are given two **0-indexed** strings `source` and `target`, both of length `n` and consisting of **lowercase** English letters. You are also given two **0-indexed** character arrays `original` and `changed`, and an integer array `cost`, where `cost[i]` represents the cost of changing the character `original[i]` to the character `changed[i]`.
You start with the string `source`. In one operation, you can pick a character `x` from the string and change it to the character `y` at a cost of `z` **if** there exists **any** index `j` such that `cost[j] == z`, `original[j] == x`, and `changed[j] == y`.
Return *the **minimum** cost to convert the string* `source` *to the string* `target` *using **any** number of operations. If it is impossible to convert* `source` *to* `target`, *return* `-1`.
**Note** that there may exist indices `i`, `j` such that `original[j] == original[i]` and `changed[j] == changed[i]`.
**Example 1:**
```
**Input:** source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
**Output:** 28
**Explanation:** To convert the string "abcd" to string "acbe":
- Change value at index 1 from 'b' to 'c' at a cost of 5.
- Change value at index 2 from 'c' to 'e' at a cost of 1.
- Change value at index 2 from 'e' to 'b' at a cost of 2.
- Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
```
**Example 2:**
```
**Input:** source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
**Output:** 12
**Explanation:** To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
```
**Example 3:**
```
**Input:** source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
**Output:** -1
**Explanation:** It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
```
**Constraints:**
* `1 <= source.length == target.length <= 105`
* `source`, `target` consist of lowercase English letters.
* `1 <= cost.length == original.length == changed.length <= 2000`
* `original[i]`, `changed[i]` are lowercase English letters.
* `1 <= cost[i] <= 106`
* `original[i] != changed[i]`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*['abcd', 'acbe', ['a', 'b', 'c', 'c', 'e', 'd'], ['b', 'c', 'b', 'e', 'b', 'e'], [2, 5, 5, 1, 2, 20]]) == 28\nassert my_solution.minimumCost(*['aaaa', 'bbbb', ['a', 'c'], ['c', 'b'], [1, 2]]) == 12\nassert my_solution.minimumCost(*['abcd', 'abce', ['a'], ['e'], [10000]]) == -1\nassert my_solution.minimumCost(*['aaaabadaaa', 'dbdadddbad', ['c', 'a', 'c', 'a', 'a', 'b', 'b', 'b', 'd', 'd', 'c'], ['a', 'c', 'b', 'd', 'b', 'c', 'a', 'd', 'c', 'b', 'd'], [7, 8, 11, 9, 7, 6, 4, 6, 9, 5, 9]]) == 56\nassert my_solution.minimumCost(*['aaadbdcdac', 'cdbabaddba', ['a', 'c', 'b', 'd', 'b', 'a', 'c'], ['c', 'a', 'd', 'b', 'c', 'b', 'd'], [7, 2, 1, 3, 6, 1, 7]]) == 39\nassert my_solution.minimumCost(*['aababdaacb', 'bcdcdcbdcb', ['a', 'd', 'd', 'a', 'c', 'b', 'c', 'a', 'c', 'd', 'b', 'b'], ['b', 'c', 'b', 'd', 'a', 'a', 'b', 'c', 'd', 'a', 'c', 'd'], [11, 4, 3, 2, 7, 11, 7, 6, 9, 2, 1, 7]]) == 42\nassert my_solution.minimumCost(*['aababdbddc', 'adcbbbcdba', ['a', 'd', 'b', 'a', 'd', 'c', 'd', 'b'], ['b', 'a', 'd', 'c', 'c', 'a', 'b', 'a'], [10, 6, 8, 3, 6, 10, 8, 6]]) == 72\nassert my_solution.minimumCost(*['aabbcabbdb', 'acddbabbdd', ['c', 'd', 'c', 'a', 'd', 'c', 'a', 'd', 'b', 'a', 'b'], ['d', 'b', 'a', 'c', 'c', 'b', 'b', 'a', 'd', 'd', 'c'], [5, 3, 8, 10, 9, 7, 8, 7, 5, 1, 10]]) == 32\nassert my_solution.minimumCost(*['aabbddccbc', 'abbbaabaca', ['a', 'b', 'c', 'b', 'a', 'd'], ['d', 'c', 'b', 'd', 'b', 'b'], [3, 8, 7, 6, 7, 10]]) == -1\nassert my_solution.minimumCost(*['aabdbaabaa', 'bdaacabcab', ['b', 'd', 'd', 'a', 'c', 'c', 'a', 'd', 'a', 'b'], ['c', 'c', 'b', 'd', 'b', 'd', 'b', 'a', 'c', 'a'], [9, 1, 7, 9, 2, 1, 3, 8, 8, 2]]) == 43\nassert my_solution.minimumCost(*['aacacaaccd', 'dadaacaabd', ['c', 'c', 'a', 'a', 'd', 'b', 'd', 'd'], ['b', 'd', 'd', 'b', 'b', 'c', 'c', 'a'], [7, 8, 9, 11, 4, 6, 9, 10]]) == 77\nassert my_solution.minimumCost(*['aacbabbacc', 'adbdbcbdaa', ['c', 'b', 'a', 'b', 'a', 'c', 'd', 'c', 'd'], ['b', 'c', 'b', 'd', 'd', 'a', 'b', 'd', 'c'], [2, 6, 7, 4, 7, 4, 3, 5, 6]]) == 41\nassert my_solution.minimumCost(*['aacbbabdad', 'ddadcababd', ['d', 'b', 'c', 'a', 'b', 'c', 'd', 'c', 'b', 'a', 'a'], ['c', 'd', 'd', 'b', 'c', 'b', 'b', 'a', 'a', 'c', 'd'], [7, 10, 4, 2, 7, 4, 4, 4, 6, 2, 8]]) == 45\nassert my_solution.minimumCost(*['aacbbbbcab', 'cdacdcddac', ['b', 'd', 'c', 'c', 'b', 'a'], ['c', 'c', 'b', 'a', 'a', 'd'], [4, 7, 9, 11, 3, 4]]) == 67\nassert my_solution.minimumCost(*['aacbcabcad', 'bbcadddcdd', ['b', 'a', 'd', 'a', 'b', 'c', 'a', 'd', 'd', 'b'], ['d', 'b', 'b', 'd', 'c', 'a', 'c', 'c', 'a', 'a'], [7, 7, 9, 8, 6, 3, 8, 2, 1, 5]]) == 53\nassert my_solution.minimumCost(*['aacbdbcdca', 'bbbdbcaacd', ['a', 'c', 'b', 'd', 'd', 'a', 'c', 'd'], ['c', 'b', 'c', 'c', 'b', 'd', 'd', 'a'], [9, 5, 4, 1, 2, 4, 7, 1]]) == 47\nassert my_solution.minimumCost(*['aadbbcdbbd', 'badddbdbac', ['c', 'd', 'c', 'd', 'b', 'a'], ['b', 'b', 'a', 'a', 'a', 'd'], [11, 4, 7, 8, 5, 2]]) == -1\nassert my_solution.minimumCost(*['aadbccbddd', 'cacdbabadc', ['d', 'b', 'c', 'd', 'a', 'a', 'c', 'b'], ['c', 'c', 'b', 'b', 'b', 'd', 'a', 'a'], [5, 8, 7, 2, 4, 7, 1, 5]]) == 46\nassert my_solution.minimumCost(*['aadbddcabd', 'bdcdccbada', ['d', 'a', 'a', 'b', 'd', 'b'], ['b', 'c', 'd', 'c', 'a', 'd'], [6, 10, 5, 8, 11, 4]]) == -1\nassert my_solution.minimumCost(*['aaddadccad', 'cbaaadbcba', ['c', 'a', 'a', 'd', 'c', 'c', 'b', 'b', 'a', 'd'], ['a', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'b', 'b'], [1, 10, 2, 8, 9, 1, 9, 10, 5, 1]]) == 44\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3235",
"questionFrontendId": "2976",
"questionTitle": "Minimum Cost to Convert String I",
"stats": {
"totalAccepted": "3K",
"totalSubmission": "6.7K",
"totalAcceptedRaw": 3023,
"totalSubmissionRaw": 6678,
"acRate": "45.3%"
}
} |
LeetCode/3238 | # Minimum Cost to Convert String II
You are given two **0-indexed** strings `source` and `target`, both of length `n` and consisting of **lowercase** English characters. You are also given two **0-indexed** string arrays `original` and `changed`, and an integer array `cost`, where `cost[i]` represents the cost of converting the string `original[i]` to the string `changed[i]`.
You start with the string `source`. In one operation, you can pick a **substring** `x` from the string, and change it to `y` at a cost of `z` **if** there exists **any** index `j` such that `cost[j] == z`, `original[j] == x`, and `changed[j] == y`. You are allowed to do **any** number of operations, but any pair of operations must satisfy **either** of these two conditions:
* The substrings picked in the operations are `source[a..b]` and `source[c..d]` with either `b < c` **or** `d < a`. In other words, the indices picked in both operations are **disjoint**.
* The substrings picked in the operations are `source[a..b]` and `source[c..d]` with `a == c` **and** `b == d`. In other words, the indices picked in both operations are **identical**.
Return *the **minimum** cost to convert the string* `source` *to the string* `target` *using **any** number of operations*. *If it is impossible to convert* `source` *to* `target`, *return* `-1`.
**Note** that there may exist indices `i`, `j` such that `original[j] == original[i]` and `changed[j] == changed[i]`.
**Example 1:**
```
**Input:** source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
**Output:** 28
**Explanation:** To convert "abcd" to "acbe", do the following operations:
- Change substring source[1..1] from "b" to "c" at a cost of 5.
- Change substring source[2..2] from "c" to "e" at a cost of 1.
- Change substring source[2..2] from "e" to "b" at a cost of 2.
- Change substring source[3..3] from "d" to "e" at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
```
**Example 2:**
```
**Input:** source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5]
**Output:** 9
**Explanation:** To convert "abcdefgh" to "acdeeghh", do the following operations:
- Change substring source[1..3] from "bcd" to "cde" at a cost of 1.
- Change substring source[5..7] from "fgh" to "thh" at a cost of 3. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation.
- Change substring source[5..7] from "thh" to "ghh" at a cost of 5. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation, and identical with indices picked in the second operation.
The total cost incurred is 1 + 3 + 5 = 9.
It can be shown that this is the minimum possible cost.
```
**Example 3:**
```
**Input:** source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578]
**Output:** -1
**Explanation:** It is impossible to convert "abcdefgh" to "addddddd".
If you select substring source[1..3] as the first operation to change "abcdefgh" to "adddefgh", you cannot select substring source[3..7] as the second operation because it has a common index, 3, with the first operation.
If you select substring source[3..7] as the first operation to change "abcdefgh" to "abcddddd", you cannot select substring source[1..3] as the second operation because it has a common index, 3, with the first operation.
```
**Constraints:**
* `1 <= source.length == target.length <= 1000`
* `source`, `target` consist only of lowercase English characters.
* `1 <= cost.length == original.length == changed.length <= 100`
* `1 <= original[i].length == changed[i].length <= source.length`
* `original[i]`, `changed[i]` consist only of lowercase English characters.
* `original[i] != changed[i]`
* `1 <= cost[i] <= 106`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*['abcd', 'acbe', ['a', 'b', 'c', 'c', 'e', 'd'], ['b', 'c', 'b', 'e', 'b', 'e'], [2, 5, 5, 1, 2, 20]]) == 28\nassert my_solution.minimumCost(*['abcdefgh', 'acdeeghh', ['bcd', 'fgh', 'thh'], ['cde', 'thh', 'ghh'], [1, 3, 5]]) == 9\nassert my_solution.minimumCost(*['abcdefgh', 'addddddd', ['bcd', 'defgh'], ['ddd', 'ddddd'], [100, 1578]]) == -1\nassert my_solution.minimumCost(*['a', 'b', ['a'], ['b'], [1]]) == 1\nassert my_solution.minimumCost(*['a', 'c', ['a', 'b', 'a', 'a'], ['b', 'c', 'c', 'c'], [1, 2, 10, 1]]) == 1\nassert my_solution.minimumCost(*['a', 'd', ['a'], ['b'], [1]]) == -1\nassert my_solution.minimumCost(*['ajhpd', 'djjdc', ['hpd', 'iyk', 'qzd', 'hpi', 'aic', 'znh', 'cea', 'fug', 'wir', 'kwu', 'yjo', 'rzi', 'a', 'n', 'f', 'q', 'u', 'w', 'x', 'i', 'x', 's', 'o', 'u'], ['iyk', 'qzd', 'hpi', 'aic', 'znh', 'cea', 'fug', 'wir', 'kwu', 'yjo', 'rzi', 'jdc', 'n', 'f', 'q', 'u', 'w', 'x', 'i', 'x', 's', 'o', 'u', 'd'], [80257, 95140, 96349, 89449, 81714, 5859, 96734, 96109, 41211, 99975, 57611, 32644, 82896, 22164, 99889, 98061, 95403, 90922, 64031, 94558, 58418, 99717, 96588, 88286]]) == 1264348\nassert my_solution.minimumCost(*['bzshh', 'mlosr', ['shh', 'wbs', 'hup', 'sab', 'csp', 'tel', 'mhq', 'ezp', 'eap', 'fqb', 'iea', 'cej', 'b', 'v', 'g', 'e', 'd', 'x', 'q', 'v', 'g', 'x', 'u', 'm', 'u', 'q', 'z', 'q', 'n', 'p'], ['wbs', 'hup', 'sab', 'csp', 'tel', 'mhq', 'ezp', 'eap', 'fqb', 'iea', 'cej', 'osr', 'v', 'g', 'e', 'd', 'x', 'q', 'v', 'g', 'x', 'u', 'm', 'u', 'q', 'm', 'q', 'n', 'p', 'l'], [69565, 82190, 75322, 85502, 89675, 98424, 86521, 85852, 32285, 99465, 82356, 97775, 30173, 88276, 82158, 40971, 75361, 65284, 89814, 68219, 44777, 95082, 99781, 99072, 74513, 49667, 99719, 93132, 99203, 54171]]) == 1589277\nassert my_solution.minimumCost(*['fjybg', 'apyyt', ['bg', 'xr', 'cc', 'ip', 'vq', 'po', 'ym', 'rh', 'vw', 'lf', 'lo', 'ee', 'qv', 'yr', 'f', 'w', 'i', 'u', 'g', 'a', 'e', 'f', 's', 'r', 'p', 'j', 'o', 'g', 'i', 'u'], ['xr', 'cc', 'ip', 'vq', 'po', 'ym', 'rh', 'vw', 'lf', 'lo', 'ee', 'qv', 'yr', 'yt', 'w', 'i', 'u', 'g', 'a', 'e', 'f', 's', 'r', 'p', 'a', 'o', 'g', 'i', 'u', 'p'], [97733, 90086, 87125, 85361, 75644, 46301, 21616, 79538, 52507, 95884, 79353, 61127, 58665, 96031, 95035, 12116, 41158, 91096, 47819, 88522, 25493, 80186, 66981, 87597, 56691, 86820, 89031, 99954, 41271, 39699]]) == 1628332\nassert my_solution.minimumCost(*['htkdz', 'oaqaw', ['kdz', 'yyv', 'cde', 'oks', 'fzu', 'hkm', 'dmb', 'arh', 'lix', 'eij', 'ksv', 't', 'u', 'f', 'w', 'b', 'u', 'v', 'h', 'o', 'b', 'o', 'p', 'z', 'h', 'w', 't', 'p', 'x', 'y'], ['yyv', 'cde', 'oks', 'fzu', 'hkm', 'dmb', 'arh', 'lix', 'eij', 'ksv', 'qaw', 'u', 'f', 'w', 'b', 'u', 'v', 'h', 'o', 'b', 'o', 'p', 'z', 'a', 'w', 't', 'p', 'x', 'y', 'o'], [90243, 86765, 84893, 80924, 85915, 42672, 99995, 99429, 88069, 84925, 71184, 54929, 83245, 72750, 87238, 30151, 58657, 94445, 98330, 90683, 83980, 96513, 75536, 95212, 79301, 74556, 94836, 94781, 76273, 86147]]) == 1278928\nassert my_solution.minimumCost(*['iktgh', 'srwcg', ['h', 'e', 'y', 'g', 'q', 'y', 't', 'n', 'r', 'e', 'i', 'x', 'iktg', 'xwgv', 'ddrp', 'saxt', 'rvdq', 'moiy', 'loln', 'bkgj', 'jjgi', 'vatf'], ['e', 'y', 'g', 'q', 'y', 't', 'n', 'r', 'e', 'i', 'x', 'g', 'xwgv', 'ddrp', 'saxt', 'rvdq', 'moiy', 'loln', 'bkgj', 'jjgi', 'vatf', 'srwc'], [70839, 75691, 55903, 82637, 97906, 86576, 92197, 74464, 86638, 61531, 80041, 52732, 96361, 39766, 74988, 59857, 69068, 89990, 74293, 82838, 37650, 26885]]) == 854129\nassert my_solution.minimumCost(*['imbin', 'dmhjv', ['bin', 'pwo', 'fwt', 'xwi', 'xal', 'uqt', 'lmp', 'erq', 'kac', 'dgv', 'qgh', 'rei', 'nbx', 'i', 'u', 'b', 'v', 'c', 'q', 'p', 'f', 'q', 'v', 't', 'n', 'b'], ['pwo', 'fwt', 'xwi', 'xal', 'uqt', 'lmp', 'erq', 'kac', 'dgv', 'qgh', 'rei', 'nbx', 'hjv', 'u', 'b', 'v', 'c', 'q', 'p', 'f', 'q', 'v', 't', 'n', 'b', 'd'], [47307, 30907, 64949, 35735, 84284, 83424, 69858, 92113, 51405, 69242, 97014, 91471, 78165, 92733, 79709, 99573, 78055, 20529, 85549, 90496, 60896, 75354, 50630, 49094, 41380, 46980]]) == 1115296\nassert my_solution.minimumCost(*['jegbx', 'ezhfc', ['egbx', 'hrbf', 'twne', 'snjd', 'ysrf', 'qzqg', 'rcll', 'ekvz', 'inpr', 'frxs', 'xcww', 'unsw', 'vdug', 'ycvs', 'j', 'v', 'j', 'y', 'n', 'q', 'w', 'a', 'z', 'g', 'b', 'd'], ['hrbf', 'twne', 'snjd', 'ysrf', 'qzqg', 'rcll', 'ekvz', 'inpr', 'frxs', 'xcww', 'unsw', 'vdug', 'ycvs', 'zhfc', 'v', 'j', 'y', 'n', 'q', 'w', 'a', 'z', 'g', 'b', 'd', 'e'], [50682, 89150, 91153, 85032, 97960, 96862, 81138, 86570, 77628, 45200, 44955, 70845, 99254, 80325, 91331, 95349, 84374, 94177, 53994, 94284, 79531, 92353, 60384, 100000, 93152, 19787]]) == 1868790\nassert my_solution.minimumCost(*['jpyjj', 'jqnfp', ['j', 'i', 'q', 'u', 'y', 'w', 'd', 'a', 'h', 's', 'i', 'y', 'w', 'pyj', 'qng', 'lrn', 'nrm', 'tvn', 'fei', 'fpj', 'qlw', 'lrb', 'ufu', 'kll', 'nqp'], ['i', 'q', 'u', 'y', 'w', 'd', 'a', 'h', 's', 'i', 'y', 'w', 'p', 'qng', 'lrn', 'nrm', 'tvn', 'fei', 'fpj', 'qlw', 'lrb', 'ufu', 'kll', 'nqp', 'qnf'], [62657, 90954, 55348, 88767, 87756, 55487, 49700, 51801, 94877, 81661, 99027, 91814, 62872, 25235, 62153, 96875, 12009, 85321, 68993, 75866, 72888, 96411, 78568, 83975, 60456]]) == 1131062\nassert my_solution.minimumCost(*['nialx', 'qvqfl', ['x', 'r', 'a', 'x', 'c', 'w', 's', 'a', 'n', 'e', 'q', 'p', 'v', 'k', 'o', 'ial', 'qzu', 'owr', 'kyq', 'ukk', 'gpq', 'jdp', 'dus', 'eng', 'btu', 'cbp'], ['r', 'a', 'x', 'c', 'w', 's', 'a', 'l', 'e', 'q', 'p', 'v', 'k', 'o', 'q', 'qzu', 'owr', 'kyq', 'ukk', 'gpq', 'jdp', 'dus', 'eng', 'btu', 'cbp', 'vqf'], [64196, 95812, 96987, 40860, 41507, 99365, 99208, 53062, 44440, 65136, 95625, 86166, 61798, 84228, 92555, 97678, 97576, 19742, 92989, 98167, 68457, 82411, 39923, 81778, 87792, 7523]]) == 1096682\nassert my_solution.minimumCost(*['pagpe', 'xacng', ['gpe', 'owt', 'wyv', 'eba', 'xgp', 'uny', 'ibc', 'usb', 'mzj', 'wdo', 'lyc', 'eof', 'oci', 'p', 'e', 'p', 'u', 'h', 'w', 'i', 'l'], ['owt', 'wyv', 'eba', 'xgp', 'uny', 'ibc', 'usb', 'mzj', 'wdo', 'lyc', 'eof', 'oci', 'cng', 'e', 'p', 'u', 'h', 'w', 'i', 'l', 'x'], [56193, 92982, 90717, 67407, 91949, 77752, 88841, 43278, 51149, 43646, 99585, 41038, 84989, 57688, 64474, 96532, 77511, 37031, 90895, 62831, 87342]]) == 1381668\nassert my_solution.minimumCost(*['aaabbebbbhbbbbebaaeh', 'hhbebebbahhhehhbbhee', ['a', 'b', 'b', 'b', 'e', 'a', 'h'], ['b', 'e', 'a', 'h', 'h', 'h', 'e'], [9, 8, 5, 9, 3, 7, 9]]) == 99\nassert my_solution.minimumCost(*['abbbeebebehbbhhhbeab', 'aehebehebaeaebbaahhb', ['b', 'b', 'e', 'e', 'h', 'h', 'h', 'b', 'e', 'a'], ['e', 'h', 'b', 'a', 'e', 'b', 'a', 'a', 'h', 'h'], [10, 2, 9, 10, 7, 8, 10, 10, 6, 9]]) == 118\nassert my_solution.minimumCost(*['abebbeeeahhbahaehaab', 'eebhheeahaahbaebaaea', ['a', 'b', 'e', 'a', 'h', 'a', 'e', 'b'], ['e', 'h', 'a', 'h', 'a', 'b', 'b', 'a'], [6, 8, 5, 10, 10, 10, 10, 8]]) == 149\nassert my_solution.minimumCost(*['aeaaebhbhehbeehbehea', 'babehheaaeebeebahhba', ['a', 'e', 'a', 'e', 'b', 'h', 'b', 'h', 'h', 'e'], ['b', 'a', 'e', 'h', 'h', 'e', 'a', 'a', 'b', 'b'], [8, 6, 3, 8, 7, 9, 9, 10, 10, 5]]) == 109\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3238",
"questionFrontendId": "2977",
"questionTitle": "Minimum Cost to Convert String II",
"stats": {
"totalAccepted": "2K",
"totalSubmission": "6.9K",
"totalAcceptedRaw": 1993,
"totalSubmissionRaw": 6946,
"acRate": "28.7%"
}
} |
LeetCode/3252 | # Count the Number of Incremovable Subarrays I
You are given a **0-indexed** array of **positive** integers `nums`.
A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing.
Return *the total number of **incremovable** subarrays of* `nums`.
**Note** that an empty array is considered strictly increasing.
A **subarray** is a contiguous non-empty sequence of elements within an array.
**Example 1:**
```
**Input:** nums = [1,2,3,4]
**Output:** 10
**Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
```
**Example 2:**
```
**Input:** nums = [6,5,7,8]
**Output:** 7
**Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.
```
**Example 3:**
```
**Input:** nums = [8,7,6,6]
**Output:** 3
**Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
```
**Constraints:**
* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 50`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.incremovableSubarrayCount(*[[1, 2, 3, 4]]) == 10\nassert my_solution.incremovableSubarrayCount(*[[6, 5, 7, 8]]) == 7\nassert my_solution.incremovableSubarrayCount(*[[8, 7, 6, 6]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[2]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[3]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[4]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[5]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[6]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[7]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[8]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[9]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[10]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[1, 2]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1, 4]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1, 8]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[2, 10]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[3, 4]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[3, 8]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[3, 10]]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3252",
"questionFrontendId": "2970",
"questionTitle": "Count the Number of Incremovable Subarrays I",
"stats": {
"totalAccepted": "2.4K",
"totalSubmission": "4K",
"totalAcceptedRaw": 2420,
"totalSubmissionRaw": 4010,
"acRate": "60.3%"
}
} |
LeetCode/3262 | # Find Polygon With the Largest Perimeter
You are given an array of **positive** integers `nums` of length `n`.
A **polygon** is a closed plane figure that has at least `3` sides. The **longest side** of a polygon is **smaller** than the sum of its other sides.
Conversely, if you have `k` (`k >= 3`) **positive** real numbers `a1`, `a2`, `a3`, ..., `ak` where `a1 <= a2 <= a3 <= ... <= ak` **and** `a1 + a2 + a3 + ... + ak-1 > ak`, then there **always** exists a polygon with `k` sides whose lengths are `a1`, `a2`, `a3`, ..., `ak`.
The **perimeter** of a polygon is the sum of lengths of its sides.
Return *the **largest** possible **perimeter** of a **polygon** whose sides can be formed from* `nums`, *or* `-1` *if it is not possible to create a polygon*.
**Example 1:**
```
**Input:** nums = [5,5,5]
**Output:** 15
**Explanation:** The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
```
**Example 2:**
```
**Input:** nums = [1,12,1,2,5,50,3]
**Output:** 12
**Explanation:** The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
It can be shown that the largest possible perimeter is 12.
```
**Example 3:**
```
**Input:** nums = [5,5,50]
**Output:** -1
**Explanation:** There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
```
**Constraints:**
* `3 <= n <= 105`
* `1 <= nums[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.largestPerimeter(*[[5, 5, 5]]) == 15\nassert my_solution.largestPerimeter(*[[1, 12, 1, 2, 5, 50, 3]]) == 12\nassert my_solution.largestPerimeter(*[[5, 5, 50]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 1]]) == 3\nassert my_solution.largestPerimeter(*[[1, 1, 2]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 3]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 4]]) == -1\nassert my_solution.largestPerimeter(*[[1, 1, 5]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 1]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 2]]) == 5\nassert my_solution.largestPerimeter(*[[1, 2, 3]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 4]]) == -1\nassert my_solution.largestPerimeter(*[[1, 2, 5]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 1]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 2]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 3]]) == 7\nassert my_solution.largestPerimeter(*[[1, 3, 4]]) == -1\nassert my_solution.largestPerimeter(*[[1, 3, 5]]) == -1\nassert my_solution.largestPerimeter(*[[1, 4, 1]]) == -1\nassert my_solution.largestPerimeter(*[[1, 4, 2]]) == -1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3262",
"questionFrontendId": "2971",
"questionTitle": "Find Polygon With the Largest Perimeter",
"stats": {
"totalAccepted": "2.8K",
"totalSubmission": "4.1K",
"totalAcceptedRaw": 2813,
"totalSubmissionRaw": 4118,
"acRate": "68.3%"
}
} |
LeetCode/3248 | # Count the Number of Incremovable Subarrays II
You are given a **0-indexed** array of **positive** integers `nums`.
A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing.
Return *the total number of **incremovable** subarrays of* `nums`.
**Note** that an empty array is considered strictly increasing.
A **subarray** is a contiguous non-empty sequence of elements within an array.
**Example 1:**
```
**Input:** nums = [1,2,3,4]
**Output:** 10
**Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
```
**Example 2:**
```
**Input:** nums = [6,5,7,8]
**Output:** 7
**Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.
```
**Example 3:**
```
**Input:** nums = [8,7,6,6]
**Output:** 3
**Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.incremovableSubarrayCount(*[[1, 2, 3, 4]]) == 10\nassert my_solution.incremovableSubarrayCount(*[[6, 5, 7, 8]]) == 7\nassert my_solution.incremovableSubarrayCount(*[[8, 7, 6, 6]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[1]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[2]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[3]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[4]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[5]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[6]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[7]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[8]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[9]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[10]]) == 1\nassert my_solution.incremovableSubarrayCount(*[[4, 10]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[7, 3]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[8, 5]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[8, 10]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[9, 9]]) == 3\nassert my_solution.incremovableSubarrayCount(*[[5, 5, 6]]) == 5\nassert my_solution.incremovableSubarrayCount(*[[6, 7, 5]]) == 4\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3248",
"questionFrontendId": "2972",
"questionTitle": "Count the Number of Incremovable Subarrays II",
"stats": {
"totalAccepted": "2K",
"totalSubmission": "3.8K",
"totalAcceptedRaw": 1974,
"totalSubmissionRaw": 3774,
"acRate": "52.3%"
}
} |
LeetCode/3227 | # Find Missing and Repeated Values
You are given a **0-indexed** 2D integer matrix `grid` of size `n * n` with values in the range `[1, n2]`. Each integer appears **exactly once** except `a` which appears **twice** and `b` which is **missing**. The task is to find the repeating and missing numbers `a` and `b`.
Return *a **0-indexed** integer array* `ans` *of size* `2` *where* `ans[0]` *equals to* `a` *and* `ans[1]` *equals to* `b`*.*
**Example 1:**
```
**Input:** grid = [[1,3],[2,2]]
**Output:** [2,4]
**Explanation:** Number 2 is repeated and number 4 is missing so the answer is [2,4].
```
**Example 2:**
```
**Input:** grid = [[9,1,7],[8,9,2],[3,4,6]]
**Output:** [9,5]
**Explanation:** Number 9 is repeated and number 5 is missing so the answer is [9,5].
```
**Constraints:**
* `2 <= n == grid.length == grid[i].length <= 50`
* `1 <= grid[i][j] <= n * n`
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is not equal to any of the grid members.
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is equal to exactly two of the grid members.
* For all `x` that `1 <= x <= n * n` except two of them there is exatly one pair of `i, j` that `0 <= i, j <= n - 1` and `grid[i][j] == x`.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 3], [2, 2]]]) == [2, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[9, 1, 7], [8, 9, 2], [3, 4, 6]]]) == [9, 5]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 1], [3, 2]]]) == [1, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 1], [3, 4]]]) == [1, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [1, 3]]]) == [1, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [1, 4]]]) == [1, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [3, 3]]]) == [3, 4]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [4, 1]]]) == [1, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [4, 2]]]) == [2, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 2], [4, 4]]]) == [4, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [1, 3]]]) == [1, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [2, 1]]]) == [1, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [3, 1]]]) == [1, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [3, 4]]]) == [4, 2]\nassert my_solution.findMissingAndRepeatedValues(*[[[1, 4], [4, 2]]]) == [4, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 1], [4, 2]]]) == [2, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 1], [4, 4]]]) == [4, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 2], [3, 4]]]) == [2, 1]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 2], [4, 1]]]) == [2, 3]\nassert my_solution.findMissingAndRepeatedValues(*[[[2, 3], [2, 1]]]) == [2, 4]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3227",
"questionFrontendId": "2965",
"questionTitle": "Find Missing and Repeated Values",
"stats": {
"totalAccepted": "5.2K",
"totalSubmission": "6.6K",
"totalAcceptedRaw": 5216,
"totalSubmissionRaw": 6569,
"acRate": "79.4%"
}
} |
LeetCode/3241 | # Divide Array Into Arrays With Max Difference
You are given an integer array `nums` of size `n` and a positive integer `k`.
Divide the array into one or more arrays of size `3` satisfying the following conditions:
* **Each** element of `nums` should be in **exactly** one array.
* The difference between **any** two elements in one array is less than or equal to `k`.
Return *a* **2D** *array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return **any** of them.*
**Example 1:**
```
**Input:** nums = [1,3,4,8,7,9,3,5,1], k = 2
**Output:** [[1,1,3],[3,4,5],[7,8,9]]
**Explanation:** We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
The difference between any two elements in each array is less than or equal to 2.
Note that the order of elements is not important.
```
**Example 2:**
```
**Input:** nums = [1,3,3,2,7,3], k = 3
**Output:** []
**Explanation:** It is not possible to divide the array satisfying all the conditions.
```
**Constraints:**
* `n == nums.length`
* `1 <= n <= 105`
* `n` is a multiple of `3`.
* `1 <= nums[i] <= 105`
* `1 <= k <= 105`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.divideArray(*[[1, 3, 4, 8, 7, 9, 3, 5, 1], 2]) == [[1, 1, 3], [3, 4, 5], [7, 8, 9]]\nassert my_solution.divideArray(*[[1, 3, 3, 2, 7, 3], 3]) == []\nassert my_solution.divideArray(*[[4, 2, 9, 8, 2, 12, 7, 12, 10, 5, 8, 5, 5, 7, 9, 2, 5, 11], 14]) == [[2, 2, 2], [4, 5, 5], [5, 5, 7], [7, 8, 8], [9, 9, 10], [11, 12, 12]]\nassert my_solution.divideArray(*[[33, 26, 4, 18, 16, 24, 24, 15, 8, 18, 34, 20, 24, 16, 3], 16]) == [[3, 4, 8], [15, 16, 16], [18, 18, 20], [24, 24, 24], [26, 33, 34]]\nassert my_solution.divideArray(*[[6, 1, 8, 8, 5, 8, 5, 9, 8, 9, 5, 8, 3, 4, 6], 7]) == [[1, 3, 4], [5, 5, 5], [6, 6, 8], [8, 8, 8], [8, 9, 9]]\nassert my_solution.divideArray(*[[20, 21, 34, 3, 19, 2, 23, 32, 20, 17, 14, 13, 19, 20, 6], 15]) == [[2, 3, 6], [13, 14, 17], [19, 19, 20], [20, 20, 21], [23, 32, 34]]\nassert my_solution.divideArray(*[[6, 10, 5, 12, 7, 11, 6, 6, 12, 12, 11, 7], 2]) == [[5, 6, 6], [6, 7, 7], [10, 11, 11], [12, 12, 12]]\nassert my_solution.divideArray(*[[12, 15, 26, 7, 10, 13, 15, 5, 27, 16, 14, 15], 18]) == [[5, 7, 10], [12, 13, 14], [15, 15, 15], [16, 26, 27]]\nassert my_solution.divideArray(*[[12, 7, 13, 10, 7, 19, 11, 23, 3, 3, 7, 9], 16]) == [[3, 3, 7], [7, 7, 9], [10, 11, 12], [13, 19, 23]]\nassert my_solution.divideArray(*[[19, 3, 23, 4, 8, 1, 1, 3, 26], 7]) == [[1, 1, 3], [3, 4, 8], [19, 23, 26]]\nassert my_solution.divideArray(*[[11, 13, 24, 11, 9, 23, 16, 19, 13], 8]) == [[9, 11, 11], [13, 13, 16], [19, 23, 24]]\nassert my_solution.divideArray(*[[6, 12, 21, 12, 6, 12, 25, 20, 15, 22, 11, 19, 8, 4, 18, 26, 17, 18, 12, 5, 8], 11]) == [[4, 5, 6], [6, 8, 8], [11, 12, 12], [12, 12, 15], [17, 18, 18], [19, 20, 21], [22, 25, 26]]\nassert my_solution.divideArray(*[[15, 17, 14, 3, 25, 15, 11, 25, 15, 16, 12, 18], 10]) == [[3, 11, 12], [14, 15, 15], [15, 16, 17], [18, 25, 25]]\nassert my_solution.divideArray(*[[16, 20, 16, 19, 20, 13, 14, 20, 14], 10]) == [[13, 14, 14], [16, 16, 19], [20, 20, 20]]\nassert my_solution.divideArray(*[[2, 13, 15, 14, 18, 15, 3, 13, 2], 1]) == []\nassert my_solution.divideArray(*[[1, 14, 20, 7, 17, 2, 14, 1, 8], 11]) == [[1, 1, 2], [7, 8, 14], [14, 17, 20]]\nassert my_solution.divideArray(*[[8, 12, 19, 8, 9, 19, 9, 19, 9, 8, 6, 9, 6, 6, 12], 3]) == [[6, 6, 6], [8, 8, 8], [9, 9, 9], [9, 12, 12], [19, 19, 19]]\nassert my_solution.divideArray(*[[18, 16, 17, 19, 12, 25, 11, 27, 11, 32, 32, 17], 20]) == [[11, 11, 12], [16, 17, 17], [18, 19, 25], [27, 32, 32]]\nassert my_solution.divideArray(*[[21, 11, 24, 20, 17, 13, 7, 20, 20, 16, 24, 20, 12, 17, 16, 15, 7, 7, 18, 15, 20], 6]) == [[7, 7, 7], [11, 12, 13], [15, 15, 16], [16, 17, 17], [18, 20, 20], [20, 20, 20], [21, 24, 24]]\nassert my_solution.divideArray(*[[6, 7, 7, 6, 7, 6], 13]) == [[6, 6, 6], [7, 7, 7]]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3241",
"questionFrontendId": "2966",
"questionTitle": "Divide Array Into Arrays With Max Difference",
"stats": {
"totalAccepted": "4.7K",
"totalSubmission": "7K",
"totalAcceptedRaw": 4670,
"totalSubmissionRaw": 7035,
"acRate": "66.4%"
}
} |
LeetCode/3229 | # Minimum Cost to Make Array Equalindromic
You are given a **0-indexed** integer array `nums` having length `n`.
You are allowed to perform a special move **any** number of times (**including zero**) on `nums`. In one **special** **move** you perform the following steps **in order**:
* Choose an index `i` in the range `[0, n - 1]`, and a **positive** integer `x`.
* Add `|nums[i] - x|` to the total cost.
* Change the value of `nums[i]` to `x`.
A **palindromic number** is a positive integer that remains the same when its digits are reversed. For example, `121`, `2552` and `65756` are palindromic numbers whereas `24`, `46`, `235` are not palindromic numbers.
An array is considered **equalindromic** if all the elements in the array are equal to an integer `y`, where `y` is a **palindromic number** less than `109`.
Return *an integer denoting the **minimum** possible total cost to make* `nums` ***equalindromic** by performing any number of special moves.*
**Example 1:**
```
**Input:** nums = [1,2,3,4,5]
**Output:** 6
**Explanation:** We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
```
**Example 2:**
```
**Input:** nums = [10,12,13,14,15]
**Output:** 11
**Explanation:** We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
```
**Example 3:**
```
**Input:** nums = [22,33,22,33,22]
**Output:** 22
**Explanation:** We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
```
**Constraints:**
* `1 <= n <= 105`
* `1 <= nums[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumCost(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCost(*[[1, 2, 3, 4, 5]]) == 6\nassert my_solution.minimumCost(*[[10, 12, 13, 14, 15]]) == 11\nassert my_solution.minimumCost(*[[22, 33, 22, 33, 22]]) == 22\nassert my_solution.minimumCost(*[[1]]) == 0\nassert my_solution.minimumCost(*[[2]]) == 0\nassert my_solution.minimumCost(*[[3]]) == 0\nassert my_solution.minimumCost(*[[4]]) == 0\nassert my_solution.minimumCost(*[[5]]) == 0\nassert my_solution.minimumCost(*[[2, 1]]) == 1\nassert my_solution.minimumCost(*[[3, 1]]) == 2\nassert my_solution.minimumCost(*[[3, 2]]) == 1\nassert my_solution.minimumCost(*[[4, 1]]) == 3\nassert my_solution.minimumCost(*[[4, 2]]) == 2\nassert my_solution.minimumCost(*[[4, 3]]) == 1\nassert my_solution.minimumCost(*[[5, 1]]) == 4\nassert my_solution.minimumCost(*[[5, 2]]) == 3\nassert my_solution.minimumCost(*[[5, 3]]) == 2\nassert my_solution.minimumCost(*[[5, 4]]) == 1\nassert my_solution.minimumCost(*[[3, 2, 1]]) == 2\nassert my_solution.minimumCost(*[[4, 2, 1]]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3229",
"questionFrontendId": "2967",
"questionTitle": "Minimum Cost to Make Array Equalindromic",
"stats": {
"totalAccepted": "3.6K",
"totalSubmission": "16.1K",
"totalAcceptedRaw": 3637,
"totalSubmissionRaw": 16137,
"acRate": "22.5%"
}
} |
LeetCode/3196 | # Apply Operations to Maximize Frequency Score
You are given a **0-indexed** integer array `nums` and an integer `k`.
You can perform the following operation on the array **at most** `k` times:
* Choose any index `i` from the array and **increase** or **decrease** `nums[i]` by `1`.
The score of the final array is the **frequency** of the most frequent element in the array.
Return *the **maximum** score you can achieve*.
The frequency of an element is the number of occurences of that element in the array.
**Example 1:**
```
**Input:** nums = [1,2,6,4], k = 3
**Output:** 3
**Explanation:** We can do the following operations on the array:
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
The element 2 is the most frequent in the final array so our score is 3.
It can be shown that we cannot achieve a better score.
```
**Example 2:**
```
**Input:** nums = [1,4,4,2,4], k = 0
**Output:** 3
**Explanation:** We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`
* `0 <= k <= 1014`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maxFrequencyScore(self, nums: List[int], k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxFrequencyScore(*[[1, 2, 6, 4], 3]) == 3\nassert my_solution.maxFrequencyScore(*[[1, 4, 4, 2, 4], 0]) == 3\nassert my_solution.maxFrequencyScore(*[[3, 20, 13, 2, 3, 15, 24, 19, 8, 13, 19, 20, 21], 45]) == 10\nassert my_solution.maxFrequencyScore(*[[13, 22, 29, 21, 13, 17, 5, 2, 27, 6, 10, 4, 23, 29, 27], 117]) == 14\nassert my_solution.maxFrequencyScore(*[[27, 8, 30, 3, 13, 28, 7, 14, 21, 19, 24, 28, 29, 1, 14, 22, 6], 23]) == 8\nassert my_solution.maxFrequencyScore(*[[10, 11, 3], 1]) == 2\nassert my_solution.maxFrequencyScore(*[[10, 19, 26, 18, 27, 18], 9]) == 4\nassert my_solution.maxFrequencyScore(*[[17, 24, 10, 23, 22, 15, 25, 2, 13, 24, 22, 25, 25, 21], 52]) == 13\nassert my_solution.maxFrequencyScore(*[[28, 6, 22, 10], 12]) == 2\nassert my_solution.maxFrequencyScore(*[[17, 17, 25, 14, 29, 28, 20, 14, 16, 22, 4, 28, 2, 5, 3, 11, 6, 20, 17], 76]) == 14\nassert my_solution.maxFrequencyScore(*[[23, 10, 18, 21, 16, 23, 14], 2]) == 3\nassert my_solution.maxFrequencyScore(*[[5, 13, 7], 8]) == 3\nassert my_solution.maxFrequencyScore(*[[6, 29, 3, 19, 10, 6, 20, 26, 1, 30, 11, 25, 29, 12, 29, 14, 15, 16, 5], 64]) == 12\nassert my_solution.maxFrequencyScore(*[[10, 26, 21, 18, 30, 25, 1], 8]) == 3\nassert my_solution.maxFrequencyScore(*[[29, 10, 26, 1, 2, 2, 17, 7, 5, 16, 24, 27, 7, 7, 26, 26, 24], 3]) == 5\nassert my_solution.maxFrequencyScore(*[[11, 16, 6, 12, 3, 8, 5, 29, 9, 15, 7, 9, 14, 6, 11, 14, 12, 23, 22, 14], 79]) == 19\nassert my_solution.maxFrequencyScore(*[[5, 17, 15, 14, 27, 11, 22, 6, 4], 26]) == 6\nassert my_solution.maxFrequencyScore(*[[13, 22, 17], 4]) == 2\nassert my_solution.maxFrequencyScore(*[[24, 6, 14, 6, 30, 9, 6, 11, 21, 10, 12, 27, 1], 90]) == 13\nassert my_solution.maxFrequencyScore(*[[19, 5, 2, 23, 16, 22, 3, 2, 5, 20, 17, 3, 22, 1], 15]) == 7\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3196",
"questionFrontendId": "2968",
"questionTitle": "Apply Operations to Maximize Frequency Score",
"stats": {
"totalAccepted": "2.2K",
"totalSubmission": "4.6K",
"totalAcceptedRaw": 2167,
"totalSubmissionRaw": 4564,
"acRate": "47.5%"
}
} |
LeetCode/3220 | # Count Tested Devices After Test Operations
You are given a **0-indexed** integer array `batteryPercentages` having length `n`, denoting the battery percentages of `n` **0-indexed** devices.
Your task is to test each device `i` **in order** from `0` to `n - 1`, by performing the following test operations:
* If `batteryPercentages[i]` is **greater** than `0`:
+ **Increment** the count of tested devices.
+ **Decrease** the battery percentage of all devices with indices `j` in the range `[i + 1, n - 1]` by `1`, ensuring their battery percentage **never goes below** `0`, i.e, `batteryPercentages[j] = max(0, batteryPercentages[j] - 1)`.
+ Move to the next device.
* Otherwise, move to the next device without performing any test.
Return *an integer denoting the number of devices that will be tested after performing the test operations in order.*
**Example 1:**
```
**Input:** batteryPercentages = [1,1,2,1,3]
**Output:** 3
**Explanation:** Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
So, the answer is 3.
```
**Example 2:**
```
**Input:** batteryPercentages = [0,1,2]
**Output:** 2
**Explanation:** Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
So, the answer is 2.
```
**Constraints:**
* `1 <= n == batteryPercentages.length <= 100`
* `0 <= batteryPercentages[i] <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countTestedDevices(*[[1, 1, 2, 1, 3]]) == 3\nassert my_solution.countTestedDevices(*[[0, 1, 2]]) == 2\nassert my_solution.countTestedDevices(*[[0]]) == 0\nassert my_solution.countTestedDevices(*[[1]]) == 1\nassert my_solution.countTestedDevices(*[[0, 0]]) == 0\nassert my_solution.countTestedDevices(*[[0, 1]]) == 1\nassert my_solution.countTestedDevices(*[[0, 2]]) == 1\nassert my_solution.countTestedDevices(*[[1, 0]]) == 1\nassert my_solution.countTestedDevices(*[[1, 2]]) == 2\nassert my_solution.countTestedDevices(*[[2, 1]]) == 1\nassert my_solution.countTestedDevices(*[[2, 2]]) == 2\nassert my_solution.countTestedDevices(*[[0, 0, 1]]) == 1\nassert my_solution.countTestedDevices(*[[0, 0, 2]]) == 1\nassert my_solution.countTestedDevices(*[[1, 1, 0]]) == 1\nassert my_solution.countTestedDevices(*[[1, 2, 0]]) == 2\nassert my_solution.countTestedDevices(*[[1, 3, 1]]) == 2\nassert my_solution.countTestedDevices(*[[2, 0, 1]]) == 1\nassert my_solution.countTestedDevices(*[[2, 2, 0]]) == 2\nassert my_solution.countTestedDevices(*[[2, 2, 2]]) == 2\nassert my_solution.countTestedDevices(*[[3, 0, 3]]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3220",
"questionFrontendId": "2960",
"questionTitle": "Count Tested Devices After Test Operations",
"stats": {
"totalAccepted": "5.8K",
"totalSubmission": "6.8K",
"totalAcceptedRaw": 5796,
"totalSubmissionRaw": 6847,
"acRate": "84.7%"
}
} |
LeetCode/3234 | # Double Modular Exponentiation
You are given a **0-indexed** 2D array `variables` where `variables[i] = [ai, bi, ci, mi]`, and an integer `target`.
An index `i` is **good** if the following formula holds:
* `0 <= i < variables.length`
* `((aibi % 10)ci) % mi == target`
Return *an array consisting of **good** indices in **any order***.
**Example 1:**
```
**Input:** variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
**Output:** [0,2]
**Explanation:** For each index i in the variables array:
1) For the index 0, variables[0] = [2,3,3,10], (23 % 10)3 % 10 = 2.
2) For the index 1, variables[1] = [3,3,3,1], (33 % 10)3 % 1 = 0.
3) For the index 2, variables[2] = [6,1,1,4], (61 % 10)1 % 4 = 2.
Therefore we return [0,2] as the answer.
```
**Example 2:**
```
**Input:** variables = [[39,3,1000,1000]], target = 17
**Output:** []
**Explanation:** For each index i in the variables array:
1) For the index 0, variables[0] = [39,3,1000,1000], (393 % 10)1000 % 1000 = 1.
Therefore we return [] as the answer.
```
**Constraints:**
* `1 <= variables.length <= 100`
* `variables[i] == [ai, bi, ci, mi]`
* `1 <= ai, bi, ci, mi <= 103`
* `0 <= target <= 103`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.getGoodIndices(*[[[2, 3, 3, 10], [3, 3, 3, 1], [6, 1, 1, 4]], 2]) == [0, 2]\nassert my_solution.getGoodIndices(*[[[39, 3, 1000, 1000]], 17]) == []\nassert my_solution.getGoodIndices(*[[[3, 2, 4, 2], [3, 3, 1, 3], [2, 2, 2, 4], [4, 4, 2, 3], [2, 4, 1, 3]], 4]) == []\nassert my_solution.getGoodIndices(*[[[9, 2, 8, 5], [7, 8, 8, 8], [8, 9, 6, 1], [8, 6, 2, 2], [3, 6, 3, 1]], 9]) == []\nassert my_solution.getGoodIndices(*[[[2, 2, 3, 2], [1, 3, 3, 2], [3, 2, 2, 3], [3, 1, 2, 3], [1, 2, 3, 1], [2, 2, 2, 2], [2, 1, 3, 1], [3, 2, 2, 2], [2, 1, 3, 1], [3, 3, 1, 3]], 0]) == [0, 2, 3, 4, 5, 6, 8]\nassert my_solution.getGoodIndices(*[[[1, 3, 2, 3], [4, 2, 3, 3], [4, 1, 4, 4], [4, 2, 3, 1], [4, 2, 1, 1], [1, 2, 4, 1], [1, 1, 4, 2], [1, 4, 4, 3], [1, 2, 2, 3]], 2]) == []\nassert my_solution.getGoodIndices(*[[[5, 4, 1, 3], [2, 5, 5, 1], [5, 3, 4, 1]], 5]) == []\nassert my_solution.getGoodIndices(*[[[4, 7, 6, 7], [7, 6, 6, 4], [6, 8, 2, 3], [8, 3, 5, 8]], 4]) == []\nassert my_solution.getGoodIndices(*[[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], 0]) == [0, 1, 2, 3, 4, 5, 6]\nassert my_solution.getGoodIndices(*[[[3, 5, 1, 2], [3, 2, 5, 2], [4, 4, 3, 2], [3, 2, 5, 3], [1, 5, 1, 4]], 1]) == [0, 1, 4]\nassert my_solution.getGoodIndices(*[[[1, 2, 1, 1], [2, 2, 2, 2], [1, 1, 1, 2], [1, 2, 2, 2]], 2]) == []\nassert my_solution.getGoodIndices(*[[[3, 3, 5, 6], [8, 2, 9, 2], [1, 4, 6, 1], [6, 4, 7, 7]], 8]) == []\nassert my_solution.getGoodIndices(*[[[3, 5, 4, 3], [1, 3, 3, 1], [3, 3, 5, 5], [4, 5, 5, 5], [5, 1, 4, 3], [2, 5, 3, 4]], 7]) == []\nassert my_solution.getGoodIndices(*[[[9, 7, 2, 7], [9, 1, 8, 1], [9, 3, 5, 6], [6, 1, 8, 4], [9, 6, 2, 3]], 8]) == []\nassert my_solution.getGoodIndices(*[[[10, 6, 8, 7], [3, 6, 1, 8]], 5]) == []\nassert my_solution.getGoodIndices(*[[[4, 6, 5, 2], [2, 6, 4, 6], [4, 6, 3, 6], [2, 2, 6, 5], [6, 5, 5, 2]], 2]) == []\nassert my_solution.getGoodIndices(*[[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], 1]) == []\nassert my_solution.getGoodIndices(*[[[5, 6, 5, 1], [4, 3, 1, 6], [5, 4, 4, 2]], 4]) == [1]\nassert my_solution.getGoodIndices(*[[[5, 1, 2, 4], [4, 5, 5, 5], [5, 9, 7, 4], [7, 9, 6, 3], [1, 8, 6, 1], [1, 1, 9, 9], [3, 7, 6, 5], [2, 6, 2, 6]], 1]) == [0, 2, 3, 5]\nassert my_solution.getGoodIndices(*[[[1, 3, 2, 5], [5, 4, 1, 2], [2, 2, 3, 2], [4, 2, 5, 4], [1, 5, 4, 1], [2, 2, 5, 2], [3, 3, 2, 1], [2, 5, 4, 3], [2, 1, 5, 1]], 4]) == []\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3234",
"questionFrontendId": "2961",
"questionTitle": "Double Modular Exponentiation",
"stats": {
"totalAccepted": "4.6K",
"totalSubmission": "9.7K",
"totalAcceptedRaw": 4597,
"totalSubmissionRaw": 9672,
"acRate": "47.5%"
}
} |
LeetCode/3213 | # Count Subarrays Where Max Element Appears at Least K Times
You are given an integer array `nums` and a **positive** integer `k`.
Return *the number of subarrays where the **maximum** element of* `nums` *appears **at least*** `k` *times in that subarray.*
A **subarray** is a contiguous sequence of elements within an array.
**Example 1:**
```
**Input:** nums = [1,3,2,3,3], k = 2
**Output:** 6
**Explanation:** The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
```
**Example 2:**
```
**Input:** nums = [1,4,2,1], k = 3
**Output:** 0
**Explanation:** No subarray contains the element 4 at least 3 times.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 106`
* `1 <= k <= 105`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countSubarrays(*[[1, 3, 2, 3, 3], 2]) == 6\nassert my_solution.countSubarrays(*[[1, 4, 2, 1], 3]) == 0\nassert my_solution.countSubarrays(*[[61, 23, 38, 23, 56, 40, 82, 56, 82, 82, 82, 70, 8, 69, 8, 7, 19, 14, 58, 42, 82, 10, 82, 78, 15, 82], 2]) == 224\nassert my_solution.countSubarrays(*[[37, 20, 38, 66, 34, 38, 9, 41, 1, 14, 25, 63, 8, 12, 66, 66, 60, 12, 35, 27, 16, 38, 12, 66, 38, 36, 59, 54, 66, 54, 66, 48, 59, 66, 34, 11, 50, 66, 42, 51, 53, 66, 31, 24, 66, 44, 66, 1, 66, 66, 29, 54], 5]) == 594\nassert my_solution.countSubarrays(*[[28, 5, 58, 91, 24, 91, 53, 9, 48, 85, 16, 70, 91, 91, 47, 91, 61, 4, 54, 61, 49], 1]) == 187\nassert my_solution.countSubarrays(*[[43, 105, 105, 88, 19, 82, 95, 32, 80, 37, 49, 105, 25, 105, 46, 54, 45, 84, 105, 88, 26, 20, 49, 54, 31, 105, 8, 103, 37, 32, 105, 105, 97, 27, 105, 89, 105, 47, 25, 87, 29, 105, 105, 105, 24, 105, 105, 48, 19, 91, 96, 71], 7]) == 454\nassert my_solution.countSubarrays(*[[107, 101, 180, 137, 191, 148, 83, 15, 188, 22, 100, 124, 69, 94, 191, 181, 171, 64, 136, 96, 91, 191, 107, 191, 191, 191, 107, 191, 191, 11, 140, 33, 4, 110, 83, 5, 86, 33, 42, 186, 191, 6, 42, 61, 94, 129, 191, 119, 191, 134, 43, 182, 191, 187, 63, 116, 172, 118, 50, 141, 124, 191, 125, 145, 191, 34, 191, 191], 9]) == 548\nassert my_solution.countSubarrays(*[[41, 121, 92, 15, 24, 59, 45, 110, 97, 132, 75, 72, 31, 38, 103, 37, 132, 91, 132, 132, 105, 24], 3]) == 61\nassert my_solution.countSubarrays(*[[21, 11, 13, 15, 16, 21, 8, 9, 6, 21], 2]) == 10\nassert my_solution.countSubarrays(*[[31, 18, 36, 166, 166, 166, 135, 166, 166, 12, 102], 3]) == 31\nassert my_solution.countSubarrays(*[[2, 2, 2, 2, 1, 3, 3, 2, 2, 1, 1, 3, 1, 1, 2, 3, 2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 3], 5]) == 31\nassert my_solution.countSubarrays(*[[3, 2, 4, 4, 3, 4, 3, 1, 1, 1, 1, 3, 2, 1, 2, 1, 3, 4, 4, 1, 2, 4, 1, 1, 2, 3, 3, 3, 4, 4, 4, 1, 3, 1, 4, 1, 4, 4, 4, 2, 2, 3, 4, 3, 3, 2, 2, 2, 1, 2, 4, 2, 2, 4, 4, 1, 3, 2, 3, 2, 4, 4, 4, 2, 3, 4, 2, 4, 1, 4, 1, 4, 1, 4, 4, 3, 4, 2, 4, 3, 3, 2, 3, 3, 2, 3, 4, 2, 1, 1, 1, 2, 3], 23]) == 473\nassert my_solution.countSubarrays(*[[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 3, 3, 2, 3, 2, 1, 1, 2, 2, 1, 3, 2, 3, 1, 2, 1, 3, 1, 1, 3, 1, 2, 1, 1, 1, 1, 1, 1, 3], 8]) == 148\nassert my_solution.countSubarrays(*[[54, 161, 161, 161, 161, 31, 74, 51, 87, 19, 161, 116, 108, 149, 6, 19, 155, 101, 161, 161, 154, 161, 78, 132, 62, 156, 112, 51, 161, 42, 92, 151, 142, 17, 110, 85], 4]) == 279\nassert my_solution.countSubarrays(*[[97, 102, 144, 55, 144, 128, 16, 93, 144, 9, 144, 15, 144, 144, 32, 68, 144, 60, 94, 56, 103, 5, 41, 27, 48, 144, 12, 86, 129, 144, 144, 99, 93, 96, 144, 73, 106, 76, 107, 144, 53, 21, 144, 144, 98, 32, 85, 97, 71, 127, 144, 9, 144, 144, 133, 125, 144, 135, 52, 144, 144, 46, 134, 23, 23, 144, 79], 1]) == 2163\nassert my_solution.countSubarrays(*[[17, 17, 15, 9, 14, 11, 15, 1, 6, 2, 1, 17, 3, 17, 11, 12, 9, 11, 2, 4, 15, 17, 3, 17, 8, 6, 7, 12, 3, 16, 2, 9, 14, 17, 17, 17, 3, 7, 8, 9, 8, 17, 17, 17, 4, 2, 12, 17, 7, 17, 17, 16, 17, 17, 8, 12, 11, 3, 10, 4, 10, 17, 14, 7, 5, 17, 12, 10, 17, 13, 5, 17, 8, 14, 9, 17, 17, 17, 7, 16, 10, 13, 17, 15, 1, 14, 6, 8, 11, 3], 15]) == 1055\nassert my_solution.countSubarrays(*[[17, 12, 16, 17, 7, 1, 12, 6, 17, 5, 17, 13, 16, 16, 17, 14, 17, 6, 17, 17, 17, 17, 16, 17, 14, 8, 14, 1, 12, 13, 17, 17, 14, 8, 14, 5, 16, 17, 17], 5]) == 404\nassert my_solution.countSubarrays(*[[98, 59, 98, 32, 45, 15, 98, 98, 98, 65, 98, 10, 98, 89, 87, 51, 42, 58, 76, 23, 85, 98, 98, 35, 18, 65, 39, 88, 56, 62, 10, 32, 8, 16, 32, 98, 6, 39, 14, 24, 98, 95, 68, 98, 77, 47, 98, 23, 69, 98, 49, 98, 7, 11, 92, 98, 27, 25, 85, 98, 45, 30, 50, 62, 46, 1, 79, 58, 69, 15, 59, 57, 85, 19, 98, 95, 98, 67, 52, 98, 59, 8, 98, 98, 98, 73, 86, 20, 98, 96, 21, 98, 79, 97, 52, 22, 98, 86], 12]) == 1168\nassert my_solution.countSubarrays(*[[6, 50, 118, 27, 133, 133, 3, 121, 133, 72, 117, 133, 91, 57, 107, 93, 66, 122, 133, 6, 133, 122, 81, 20, 133, 133, 121, 133, 46, 25, 133, 133, 133, 17, 8, 49, 133, 116, 40, 133, 67, 9, 133, 133, 133, 133, 109, 41, 127, 13, 39, 133, 133, 133, 122, 58, 8, 125, 33, 62], 12]) == 538\nassert my_solution.countSubarrays(*[[94, 34, 112, 106, 112, 13, 12, 112, 112, 21, 48, 71, 112, 104, 112, 29, 99, 58, 23, 11, 49, 112, 20, 86], 4]) == 105\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3213",
"questionFrontendId": "2962",
"questionTitle": "Count Subarrays Where Max Element Appears at Least K Times",
"stats": {
"totalAccepted": "5K",
"totalSubmission": "10.6K",
"totalAcceptedRaw": 4961,
"totalSubmissionRaw": 10606,
"acRate": "46.8%"
}
} |
LeetCode/3212 | # Count the Number of Good Partitions
You are given a **0-indexed** array `nums` consisting of **positive** integers.
A partition of an array into one or more **contiguous** subarrays is called **good** if no two subarrays contain the same number.
Return *the **total number** of good partitions of* `nums`.
Since the answer may be large, return it **modulo** `109 + 7`.
**Example 1:**
```
**Input:** nums = [1,2,3,4]
**Output:** 8
**Explanation:** The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).
```
**Example 2:**
```
**Input:** nums = [1,1,1,1]
**Output:** 1
**Explanation:** The only possible good partition is: ([1,1,1,1]).
```
**Example 3:**
```
**Input:** nums = [1,2,1,3]
**Output:** 2
**Explanation:** The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
```
**Constraints:**
* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def numberOfGoodPartitions(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfGoodPartitions(*[[1, 2, 3, 4]]) == 8\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 1, 1]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[1, 2, 1, 3]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[1]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[100000]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[1000000000]]) == 1\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 1, 3, 2]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 1, 9, 7]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 1, 5, 9, 2]]) == 8\nassert my_solution.numberOfGoodPartitions(*[[1, 4, 1, 7, 5]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 5, 1, 5, 6]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[1, 5, 1, 10, 8]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 6, 8, 1, 5]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[1, 6, 9, 4, 10]]) == 16\nassert my_solution.numberOfGoodPartitions(*[[1, 7, 1, 6, 8]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[1, 9, 1, 1, 7]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[2, 1, 6, 7, 5]]) == 16\nassert my_solution.numberOfGoodPartitions(*[[2, 3, 2, 6, 9]]) == 4\nassert my_solution.numberOfGoodPartitions(*[[2, 3, 2, 8, 8]]) == 2\nassert my_solution.numberOfGoodPartitions(*[[2, 3, 9, 2, 6]]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3212",
"questionFrontendId": "2963",
"questionTitle": "Count the Number of Good Partitions",
"stats": {
"totalAccepted": "3.4K",
"totalSubmission": "6.8K",
"totalAcceptedRaw": 3378,
"totalSubmissionRaw": 6754,
"acRate": "50.0%"
}
} |
LeetCode/3206 | # Find Common Elements Between Two Arrays
You are given two **0-indexed** integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively.
Consider calculating the following values:
* The number of indices `i` such that `0 <= i < n` and `nums1[i]` occurs **at least** once in `nums2`.
* The number of indices `i` such that `0 <= i < m` and `nums2[i]` occurs **at least** once in `nums1`.
Return *an integer array* `answer` *of size* `2` *containing the two values **in the above order***.
**Example 1:**
```
**Input:** nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
**Output:** [3,4]
**Explanation:** We calculate the values as follows:
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.
```
**Example 2:**
```
**Input:** nums1 = [3,4,2,3], nums2 = [1,5]
**Output:** [0,0]
**Explanation:** There are no common elements between the two arrays, so the two values will be 0.
```
**Constraints:**
* `n == nums1.length`
* `m == nums2.length`
* `1 <= n, m <= 100`
* `1 <= nums1[i], nums2[i] <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findIntersectionValues(*[[4, 3, 2, 3, 1], [2, 2, 5, 2, 3, 6]]) == [3, 4]\nassert my_solution.findIntersectionValues(*[[3, 4, 2, 3], [1, 5]]) == [0, 0]\nassert my_solution.findIntersectionValues(*[[24, 28, 7, 27, 7, 27, 9, 24, 9, 10], [12, 29, 9, 7, 5]]) == [4, 2]\nassert my_solution.findIntersectionValues(*[[10, 30, 16, 18], [23, 30, 30, 6, 10, 26, 9, 27, 6, 16, 18, 10, 27, 2, 20, 7, 16]]) == [4, 7]\nassert my_solution.findIntersectionValues(*[[7, 23, 27, 20, 21, 29, 7, 27, 27, 18, 7, 6, 20, 10], [27, 27, 28, 24, 20, 4, 6, 17, 9, 29, 20, 14, 20]]) == [7, 7]\nassert my_solution.findIntersectionValues(*[[15, 30, 6, 6], [15, 4, 16, 10, 7, 23, 24, 3, 4, 6, 14, 8, 18, 1, 29, 27, 2, 17]]) == [3, 2]\nassert my_solution.findIntersectionValues(*[[24, 7, 8, 6, 22, 28, 22, 28, 7, 19], [3, 7, 28, 7, 3, 3]]) == [4, 3]\nassert my_solution.findIntersectionValues(*[[23, 4, 26, 17, 23, 13], [24, 17, 20, 16, 1, 13, 17, 28, 17]]) == [2, 4]\nassert my_solution.findIntersectionValues(*[[5, 8, 18, 27, 16, 29, 27, 12, 1, 29, 16, 27, 22, 19, 14, 12, 11, 25], [24, 8, 16]]) == [3, 2]\nassert my_solution.findIntersectionValues(*[[29, 17, 30, 17, 15, 30, 11, 2, 24, 28, 28, 30, 30, 27, 30, 2, 30, 9, 1, 7], [12, 12, 11, 21, 2, 28, 5, 24, 12, 17, 24, 29, 22, 19, 11, 17, 1, 23]]) == [10, 10]\nassert my_solution.findIntersectionValues(*[[4, 27, 12, 16, 16, 21, 26, 7, 19, 21, 24, 26, 12, 24, 22, 12, 16], [1, 25, 8, 27, 23, 27, 27, 24]]) == [3, 4]\nassert my_solution.findIntersectionValues(*[[27, 19, 20, 16, 24, 27, 27, 24], [30, 21, 21, 6, 17, 16]]) == [1, 1]\nassert my_solution.findIntersectionValues(*[[3, 19, 21, 5, 24, 26, 22, 22, 5], [23, 26, 20, 14, 30, 9, 10, 24, 19, 22, 19, 6, 3, 20, 22, 22, 5, 24, 24]]) == [8, 11]\nassert my_solution.findIntersectionValues(*[[13, 13, 29, 12], [29, 29, 13, 7, 30, 22]]) == [3, 3]\nassert my_solution.findIntersectionValues(*[[30, 4, 16, 14, 14, 14, 20, 15, 20, 30, 6, 10, 14], [30, 16, 20, 2, 18, 10, 5, 6, 30, 20, 22, 18, 14, 23, 15]]) == [12, 9]\nassert my_solution.findIntersectionValues(*[[22, 1, 22, 4, 11, 22, 4, 20, 11, 29, 11, 11, 4, 26, 20, 12, 20, 8, 26, 17], [4, 17, 7, 15]]) == [4, 2]\nassert my_solution.findIntersectionValues(*[[30, 15, 16, 15, 11, 16, 26, 15, 21], [22, 25, 27, 2, 26, 20, 18, 15, 26, 20, 16]]) == [6, 4]\nassert my_solution.findIntersectionValues(*[[5, 6], [13, 12, 8, 5, 19, 13, 27]]) == [1, 1]\nassert my_solution.findIntersectionValues(*[[27, 28, 15, 20, 5, 13, 28, 29, 24, 29, 20, 15, 5, 20, 20, 25, 9, 20, 24, 20], [16, 20, 13, 24, 11]]) == [9, 3]\nassert my_solution.findIntersectionValues(*[[25, 7, 18], [28, 1, 14, 22, 24, 8, 25, 17]]) == [1, 1]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3206",
"questionFrontendId": "2956",
"questionTitle": "Find Common Elements Between Two Arrays",
"stats": {
"totalAccepted": "3.6K",
"totalSubmission": "4.5K",
"totalAcceptedRaw": 3641,
"totalSubmissionRaw": 4520,
"acRate": "80.6%"
}
} |
LeetCode/3230 | # Remove Adjacent Almost-Equal Characters
You are given a **0-indexed** string `word`.
In one operation, you can pick any index `i` of `word` and change `word[i]` to any lowercase English letter.
Return *the **minimum** number of operations needed to remove all adjacent **almost-equal** characters from* `word`.
Two characters `a` and `b` are **almost-equal** if `a == b` or `a` and `b` are adjacent in the alphabet.
**Example 1:**
```
**Input:** word = "aaaaa"
**Output:** 2
**Explanation:** We can change word into "a**c**a**c**a" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
```
**Example 2:**
```
**Input:** word = "abddez"
**Output:** 2
**Explanation:** We can change word into "**y**bd**o**ez" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
```
**Example 3:**
```
**Input:** word = "zyxyxyz"
**Output:** 3
**Explanation:** We can change word into "z**a**x**a**x**a**z" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.
```
**Constraints:**
* `1 <= word.length <= 100`
* `word` consists only of lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def removeAlmostEqualCharacters(self, word: str) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.removeAlmostEqualCharacters(*['aaaaa']) == 2\nassert my_solution.removeAlmostEqualCharacters(*['abddez']) == 2\nassert my_solution.removeAlmostEqualCharacters(*['zyxyxyz']) == 3\nassert my_solution.removeAlmostEqualCharacters(*['a']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['b']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['c']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['aa']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['ab']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['ac']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['ba']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['bb']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['bc']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['ca']) == 0\nassert my_solution.removeAlmostEqualCharacters(*['cb']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['cc']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aaa']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aab']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aac']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['aba']) == 1\nassert my_solution.removeAlmostEqualCharacters(*['abb']) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3230",
"questionFrontendId": "2957",
"questionTitle": "Remove Adjacent Almost-Equal Characters",
"stats": {
"totalAccepted": "3.3K",
"totalSubmission": "5.6K",
"totalAcceptedRaw": 3259,
"totalSubmissionRaw": 5561,
"acRate": "58.6%"
}
} |
LeetCode/3225 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxSubarrayLength(*[[1, 2, 3, 1, 2, 3, 1, 2], 2]) == 6\nassert my_solution.maxSubarrayLength(*[[1, 2, 1, 2, 1, 2, 1, 2], 1]) == 2\nassert my_solution.maxSubarrayLength(*[[5, 5, 5, 5, 5, 5, 5], 4]) == 4\nassert my_solution.maxSubarrayLength(*[[1], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[2], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[3], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[4], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[5], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[6], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[7], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[8], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[9], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[10], 1]) == 1\nassert my_solution.maxSubarrayLength(*[[1, 11], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[2, 11], 1]) == 2\nassert my_solution.maxSubarrayLength(*[[3, 5], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[4, 6], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[5, 8], 2]) == 2\nassert my_solution.maxSubarrayLength(*[[6, 7], 1]) == 2\nassert my_solution.maxSubarrayLength(*[[7, 9], 2]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3225",
"questionFrontendId": "2958",
"questionTitle": "Length of Longest Subarray With at Most K Frequency",
"stats": {
"totalAccepted": "3.4K",
"totalSubmission": "7.1K",
"totalAcceptedRaw": 3372,
"totalSubmissionRaw": 7115,
"acRate": "47.4%"
}
} |
LeetCode/3221 | # 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]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findPeaks(*[[2, 4, 4]]) == []\nassert my_solution.findPeaks(*[[1, 4, 3, 8, 5]]) == [1, 3]\nassert my_solution.findPeaks(*[[1, 1, 1]]) == []\nassert my_solution.findPeaks(*[[1, 1, 3]]) == []\nassert my_solution.findPeaks(*[[1, 1, 5]]) == []\nassert my_solution.findPeaks(*[[1, 2, 5]]) == []\nassert my_solution.findPeaks(*[[1, 4, 1]]) == [1]\nassert my_solution.findPeaks(*[[1, 4, 3]]) == [1]\nassert my_solution.findPeaks(*[[1, 5, 5]]) == []\nassert my_solution.findPeaks(*[[1, 6, 4]]) == [1]\nassert my_solution.findPeaks(*[[2, 1, 1]]) == []\nassert my_solution.findPeaks(*[[2, 1, 2]]) == []\nassert my_solution.findPeaks(*[[2, 2, 3]]) == []\nassert my_solution.findPeaks(*[[2, 2, 5]]) == []\nassert my_solution.findPeaks(*[[2, 3, 2]]) == [1]\nassert my_solution.findPeaks(*[[2, 3, 6]]) == []\nassert my_solution.findPeaks(*[[2, 4, 3]]) == [1]\nassert my_solution.findPeaks(*[[2, 4, 5]]) == []\nassert my_solution.findPeaks(*[[2, 6, 4]]) == [1]\nassert my_solution.findPeaks(*[[3, 3, 3]]) == []\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3221",
"questionFrontendId": "2951",
"questionTitle": "Find the Peaks",
"stats": {
"totalAccepted": "6.3K",
"totalSubmission": "8K",
"totalAcceptedRaw": 6316,
"totalSubmissionRaw": 8009,
"acRate": "78.9%"
}
} |
LeetCode/3231 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumAddedCoins(*[[1, 4, 10], 19]) == 2\nassert my_solution.minimumAddedCoins(*[[1, 4, 10, 5, 7, 19], 19]) == 1\nassert my_solution.minimumAddedCoins(*[[1, 1, 1], 20]) == 3\nassert my_solution.minimumAddedCoins(*[[1], 100000]) == 16\nassert my_solution.minimumAddedCoins(*[[100000], 100000]) == 17\nassert my_solution.minimumAddedCoins(*[[2], 5]) == 2\nassert my_solution.minimumAddedCoins(*[[5, 6, 7], 10]) == 3\nassert my_solution.minimumAddedCoins(*[[5, 6, 7], 15]) == 3\nassert my_solution.minimumAddedCoins(*[[4, 11, 13, 15, 7, 5, 12, 11, 5, 9], 34]) == 2\nassert my_solution.minimumAddedCoins(*[[8, 12, 9], 27]) == 3\nassert my_solution.minimumAddedCoins(*[[2, 13, 7, 1, 11], 35]) == 1\nassert my_solution.minimumAddedCoins(*[[10, 3, 5, 11, 6], 27]) == 2\nassert my_solution.minimumAddedCoins(*[[6, 6, 6, 15, 4], 31]) == 2\nassert my_solution.minimumAddedCoins(*[[6, 15, 6], 22]) == 3\nassert my_solution.minimumAddedCoins(*[[8, 14, 15, 4, 14, 15, 8, 10, 8], 42]) == 2\nassert my_solution.minimumAddedCoins(*[[9, 14, 14, 9, 14, 5, 12, 10, 11], 17]) == 3\nassert my_solution.minimumAddedCoins(*[[14, 5, 13, 3, 7, 10, 10, 10], 32]) == 2\nassert my_solution.minimumAddedCoins(*[[8, 6, 7, 12], 26]) == 3\nassert my_solution.minimumAddedCoins(*[[15, 1, 12], 43]) == 4\nassert my_solution.minimumAddedCoins(*[[4, 1, 4, 10], 16]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3231",
"questionFrontendId": "2952",
"questionTitle": "Minimum Number of Coins to be Added",
"stats": {
"totalAccepted": "4.1K",
"totalSubmission": "8.2K",
"totalAcceptedRaw": 4143,
"totalSubmissionRaw": 8235,
"acRate": "50.3%"
}
} |
LeetCode/3223 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countCompleteSubstrings(*['igigee', 2]) == 3\nassert my_solution.countCompleteSubstrings(*['aaabbbccc', 3]) == 6\nassert my_solution.countCompleteSubstrings(*['a', 1]) == 1\nassert my_solution.countCompleteSubstrings(*['b', 1]) == 1\nassert my_solution.countCompleteSubstrings(*['c', 1]) == 1\nassert my_solution.countCompleteSubstrings(*['aa', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['ab', 2]) == 0\nassert my_solution.countCompleteSubstrings(*['ac', 2]) == 0\nassert my_solution.countCompleteSubstrings(*['ba', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['bb', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['bc', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['ca', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['cb', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['cc', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['aaa', 1]) == 3\nassert my_solution.countCompleteSubstrings(*['aab', 3]) == 0\nassert my_solution.countCompleteSubstrings(*['aac', 2]) == 1\nassert my_solution.countCompleteSubstrings(*['aba', 3]) == 0\nassert my_solution.countCompleteSubstrings(*['abb', 1]) == 4\nassert my_solution.countCompleteSubstrings(*['abc', 3]) == 0\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3223",
"questionFrontendId": "2953",
"questionTitle": "Count Complete Substrings",
"stats": {
"totalAccepted": "3.4K",
"totalSubmission": "10.2K",
"totalAcceptedRaw": 3433,
"totalSubmissionRaw": 10180,
"acRate": "33.7%"
}
} |
LeetCode/3224 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfSequence(*[5, [0, 4]]) == 4\nassert my_solution.numberOfSequence(*[4, [1]]) == 3\nassert my_solution.numberOfSequence(*[2, [0]]) == 1\nassert my_solution.numberOfSequence(*[5, [0]]) == 1\nassert my_solution.numberOfSequence(*[100, [0]]) == 1\nassert my_solution.numberOfSequence(*[2, [1]]) == 1\nassert my_solution.numberOfSequence(*[5, [1]]) == 4\nassert my_solution.numberOfSequence(*[5, [2]]) == 6\nassert my_solution.numberOfSequence(*[5, [3]]) == 4\nassert my_solution.numberOfSequence(*[5, [4]]) == 1\nassert my_solution.numberOfSequence(*[5, [0, 1]]) == 1\nassert my_solution.numberOfSequence(*[3, [0, 2]]) == 1\nassert my_solution.numberOfSequence(*[5, [0, 2]]) == 3\nassert my_solution.numberOfSequence(*[5, [0, 3]]) == 6\nassert my_solution.numberOfSequence(*[5, [1, 2]]) == 3\nassert my_solution.numberOfSequence(*[5, [1, 3]]) == 6\nassert my_solution.numberOfSequence(*[5, [1, 4]]) == 6\nassert my_solution.numberOfSequence(*[5, [2, 3]]) == 3\nassert my_solution.numberOfSequence(*[5, [2, 4]]) == 3\nassert my_solution.numberOfSequence(*[5, [3, 4]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3224",
"questionFrontendId": "2954",
"questionTitle": "Count the Number of Infection Sequences",
"stats": {
"totalAccepted": "1.3K",
"totalSubmission": "3.3K",
"totalAcceptedRaw": 1335,
"totalSubmissionRaw": 3325,
"acRate": "40.2%"
}
} |
LeetCode/3210 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulSubstrings(*['baeyh', 2]) == 2\nassert my_solution.beautifulSubstrings(*['abba', 1]) == 3\nassert my_solution.beautifulSubstrings(*['bcdf', 1]) == 0\nassert my_solution.beautifulSubstrings(*['ihroyeeb', 5]) == 0\nassert my_solution.beautifulSubstrings(*['uzuxpzou', 3]) == 1\nassert my_solution.beautifulSubstrings(*['ouuoeqd', 2]) == 1\nassert my_solution.beautifulSubstrings(*['eeebjoxxujuaeoqibd', 8]) == 4\nassert my_solution.beautifulSubstrings(*['ilougekqlovegioemdvu', 4]) == 21\nassert my_solution.beautifulSubstrings(*['tqaewreikaztwpfwnef', 8]) == 3\nassert my_solution.beautifulSubstrings(*['oykiuhsafgfjumnzb', 7]) == 0\nassert my_solution.beautifulSubstrings(*['ifvsa', 3]) == 0\nassert my_solution.beautifulSubstrings(*['svzauyuevujektj', 5]) == 3\nassert my_solution.beautifulSubstrings(*['urahjig', 2]) == 2\nassert my_solution.beautifulSubstrings(*['ime', 2]) == 0\nassert my_solution.beautifulSubstrings(*['oacghieut', 5]) == 0\nassert my_solution.beautifulSubstrings(*['aoluu', 3]) == 0\nassert my_solution.beautifulSubstrings(*['ioaoiciiuoziout', 1]) == 5\nassert my_solution.beautifulSubstrings(*['ouafupsuhid', 6]) == 0\nassert my_solution.beautifulSubstrings(*['ox', 2]) == 0\nassert my_solution.beautifulSubstrings(*['tlaiwoauazutusiaaui', 10]) == 0\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3210",
"questionFrontendId": "2947",
"questionTitle": "Count Beautiful Substrings I",
"stats": {
"totalAccepted": "4.2K",
"totalSubmission": "7.1K",
"totalAcceptedRaw": 4176,
"totalSubmissionRaw": 7130,
"acRate": "58.6%"
}
} |
LeetCode/3219 | # 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]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.lexicographicallySmallestArray(*[[1, 5, 3, 9, 8], 2]) == [1, 3, 5, 8, 9]\nassert my_solution.lexicographicallySmallestArray(*[[1, 7, 6, 18, 2, 1], 3]) == [1, 6, 7, 18, 1, 2]\nassert my_solution.lexicographicallySmallestArray(*[[1, 7, 28, 19, 10], 3]) == [1, 7, 28, 19, 10]\nassert my_solution.lexicographicallySmallestArray(*[[1000000000], 1]) == [1000000000]\nassert my_solution.lexicographicallySmallestArray(*[[1, 60, 34, 84, 62, 56, 39, 76, 49, 38], 4]) == [1, 56, 34, 84, 60, 62, 38, 76, 49, 39]\nassert my_solution.lexicographicallySmallestArray(*[[1, 81, 10, 79, 36, 2, 87, 12, 20, 77], 7]) == [1, 77, 10, 79, 36, 2, 81, 12, 20, 87]\nassert my_solution.lexicographicallySmallestArray(*[[2, 71, 5, 87, 11, 15, 70, 70, 14, 38], 14]) == [2, 70, 5, 87, 11, 14, 70, 71, 15, 38]\nassert my_solution.lexicographicallySmallestArray(*[[4, 3, 23, 84, 34, 88, 44, 44, 18, 15], 3]) == [3, 4, 23, 84, 34, 88, 44, 44, 15, 18]\nassert my_solution.lexicographicallySmallestArray(*[[4, 34, 29, 73, 51, 11, 8, 53, 98, 47], 10]) == [4, 29, 34, 73, 47, 8, 11, 51, 98, 53]\nassert my_solution.lexicographicallySmallestArray(*[[4, 52, 38, 59, 71, 27, 31, 83, 88, 10], 14]) == [4, 27, 31, 38, 52, 59, 71, 83, 88, 10]\nassert my_solution.lexicographicallySmallestArray(*[[4, 68, 8, 10, 70, 62, 27, 5, 42, 61], 11]) == [4, 61, 5, 8, 62, 68, 27, 10, 42, 70]\nassert my_solution.lexicographicallySmallestArray(*[[5, 9, 35, 60, 73, 91, 61, 57, 87, 76], 11]) == [5, 9, 35, 57, 73, 76, 60, 61, 87, 91]\nassert my_solution.lexicographicallySmallestArray(*[[5, 15, 68, 47, 49, 67, 9, 6, 35, 14], 4]) == [5, 14, 67, 47, 49, 68, 6, 9, 35, 15]\nassert my_solution.lexicographicallySmallestArray(*[[5, 16, 43, 15, 66, 21, 58, 74, 55, 66], 9]) == [5, 15, 43, 16, 55, 21, 58, 66, 66, 74]\nassert my_solution.lexicographicallySmallestArray(*[[5, 30, 92, 4, 31, 2, 17, 39, 15, 7], 3]) == [2, 30, 92, 4, 31, 5, 15, 39, 17, 7]\nassert my_solution.lexicographicallySmallestArray(*[[5, 38, 68, 80, 64, 79, 50, 5, 8, 95], 7]) == [5, 38, 64, 79, 68, 80, 50, 5, 8, 95]\nassert my_solution.lexicographicallySmallestArray(*[[5, 100, 44, 45, 16, 30, 14, 65, 83, 64], 15]) == [5, 100, 14, 16, 30, 44, 45, 64, 83, 65]\nassert my_solution.lexicographicallySmallestArray(*[[6, 57, 100, 67, 4, 63, 47, 59, 21, 66], 8]) == [4, 57, 100, 59, 6, 63, 47, 66, 21, 67]\nassert my_solution.lexicographicallySmallestArray(*[[6, 70, 90, 1, 33, 81, 60, 80, 68, 44], 7]) == [1, 68, 90, 6, 33, 80, 60, 81, 70, 44]\nassert my_solution.lexicographicallySmallestArray(*[[6, 74, 74, 74, 30, 70, 91, 74, 76, 41], 1]) == [6, 74, 74, 74, 30, 70, 91, 74, 76, 41]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3219",
"questionFrontendId": "2948",
"questionTitle": "Make Lexicographically Smallest Array by Swapping Elements",
"stats": {
"totalAccepted": "3.2K",
"totalSubmission": "7.4K",
"totalAcceptedRaw": 3223,
"totalSubmissionRaw": 7361,
"acRate": "43.8%"
}
} |
LeetCode/3208 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.beautifulSubstrings(*['baeyh', 2]) == 2\nassert my_solution.beautifulSubstrings(*['abba', 1]) == 3\nassert my_solution.beautifulSubstrings(*['bcdf', 1]) == 0\nassert my_solution.beautifulSubstrings(*['ihroyeeb', 5]) == 0\nassert my_solution.beautifulSubstrings(*['uzuxpzou', 3]) == 1\nassert my_solution.beautifulSubstrings(*['ouuoeqd', 2]) == 1\nassert my_solution.beautifulSubstrings(*['eeebjoxxujuaeoqibd', 8]) == 4\nassert my_solution.beautifulSubstrings(*['ilougekqlovegioemdvu', 4]) == 21\nassert my_solution.beautifulSubstrings(*['tqaewreikaztwpfwnef', 8]) == 3\nassert my_solution.beautifulSubstrings(*['oykiuhsafgfjumnzb', 7]) == 0\nassert my_solution.beautifulSubstrings(*['ifvsa', 3]) == 0\nassert my_solution.beautifulSubstrings(*['svzauyuevujektj', 5]) == 3\nassert my_solution.beautifulSubstrings(*['urahjig', 2]) == 2\nassert my_solution.beautifulSubstrings(*['ime', 2]) == 0\nassert my_solution.beautifulSubstrings(*['oacghieut', 5]) == 0\nassert my_solution.beautifulSubstrings(*['aoluu', 3]) == 0\nassert my_solution.beautifulSubstrings(*['ioaoiciiuoziout', 1]) == 5\nassert my_solution.beautifulSubstrings(*['ouafupsuhid', 6]) == 0\nassert my_solution.beautifulSubstrings(*['ox', 2]) == 0\nassert my_solution.beautifulSubstrings(*['tlaiwoauazutusiaaui', 10]) == 0\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3208",
"questionFrontendId": "2949",
"questionTitle": "Count Beautiful Substrings II",
"stats": {
"totalAccepted": "2.1K",
"totalSubmission": "6.4K",
"totalAcceptedRaw": 2066,
"totalSubmissionRaw": 6390,
"acRate": "32.3%"
}
} |
LeetCode/3194 | # 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]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findWordsContaining(*[['leet', 'code'], 'e']) == [0, 1]\nassert my_solution.findWordsContaining(*[['abc', 'bcd', 'aaaa', 'cbc'], 'a']) == [0, 2]\nassert my_solution.findWordsContaining(*[['abc', 'bcd', 'aaaa', 'cbc'], 'z']) == []\nassert my_solution.findWordsContaining(*[['sgtkshnss', 'm', 'ryvbkyvuz', 'ezittyjwgb', 'wudlwg'], 'x']) == []\nassert my_solution.findWordsContaining(*[['lkwnhpbj', 'tlohm', 'juazsb', 'f', 'rq'], 'v']) == []\nassert my_solution.findWordsContaining(*[['aaa', 'imvtfjmxr', 'wbzfoovjnf', 'hqwrwmi'], 'c']) == []\nassert my_solution.findWordsContaining(*[['utyeachht', 'bgpkcs', 'skeecqvvvw', 'nccrd'], 'i']) == []\nassert my_solution.findWordsContaining(*[['alcpxexztg', 'r'], 'h']) == []\nassert my_solution.findWordsContaining(*[['ekcpg', 'pdknua', 'fot', 'janppw', 'ofomkfvx'], 'g']) == [0]\nassert my_solution.findWordsContaining(*[['dq', 'rlvopu'], 'd']) == [0]\nassert my_solution.findWordsContaining(*[['wzppkd', 'jxvk', 'zaztizmwuv', 'hvcdtobr'], 'b']) == [3]\nassert my_solution.findWordsContaining(*[['y', 'hs', 'qznrkpi'], 'v']) == []\nassert my_solution.findWordsContaining(*[['pze', 'yojczsb', 'mjvyr', 'i', 'xsygks'], 'q']) == []\nassert my_solution.findWordsContaining(*[['qsgtjagcu', 'm'], 'e']) == []\nassert my_solution.findWordsContaining(*[['kidtwmw', 'ogh', 'trdedlh', 'wwbtlindg', 'naoylytpof', 'ujcbzwzkm', 'doamcoxdv'], 'o']) == [1, 4, 6]\nassert my_solution.findWordsContaining(*[['tsmeupctki'], 't']) == [0]\nassert my_solution.findWordsContaining(*[['dqxlbljmpf', 'uvdzfoiqg', 'jsnbnx', 'fbedae', 'nodewb', 'o', 'ivepktj'], 'g']) == [1]\nassert my_solution.findWordsContaining(*[['fjlmmecm', 'sautsoorhl', 'n', 'hsyco', 'amlukrpjpv', 'rmhdnj', 'g'], 'e']) == [0]\nassert my_solution.findWordsContaining(*[['khjchmeciv', 'vgx', 'xghr', 'bbufgegu', 'qyfxu'], 'r']) == [2]\nassert my_solution.findWordsContaining(*[['jhtcugtcpl', 'bvhlgmmla', 'ntfkwzite', 'imbtzafaj', 'sdl', 't'], 'm']) == [1, 3]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3194",
"questionFrontendId": "2942",
"questionTitle": "Find Words Containing Character",
"stats": {
"totalAccepted": "4.8K",
"totalSubmission": "5.4K",
"totalAcceptedRaw": 4826,
"totalSubmissionRaw": 5440,
"acRate": "88.7%"
}
} |
LeetCode/3209 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumCoins(*[[3, 1, 2]]) == 4\nassert my_solution.minimumCoins(*[[1, 10, 1, 1]]) == 2\nassert my_solution.minimumCoins(*[[26, 18, 6, 12, 49, 7, 45, 45]]) == 39\nassert my_solution.minimumCoins(*[[27, 17, 29, 45, 3, 39, 42, 26]]) == 47\nassert my_solution.minimumCoins(*[[14, 37, 37, 38, 24, 15, 12]]) == 63\nassert my_solution.minimumCoins(*[[1, 37, 19, 38, 11, 42, 18, 33, 6, 37, 15, 48, 23, 12, 41, 18, 27, 32]]) == 37\nassert my_solution.minimumCoins(*[[38, 23, 27, 32, 47, 45, 48, 24, 39, 26, 37, 42, 24, 45, 27, 26, 15, 16, 26, 6]]) == 132\nassert my_solution.minimumCoins(*[[45, 44, 5, 9, 22, 14, 29, 14, 21, 13, 45, 10, 2, 16, 14, 30, 26, 1, 49]]) == 66\nassert my_solution.minimumCoins(*[[37, 42, 6, 50, 50, 38, 30, 38, 1, 13, 25, 39, 18, 1, 35, 32, 12]]) == 74\nassert my_solution.minimumCoins(*[[17, 32, 11, 25, 22]]) == 28\nassert my_solution.minimumCoins(*[[18, 10, 1, 11, 6, 30, 19, 24, 1, 18, 37, 29, 28, 27, 38]]) == 26\nassert my_solution.minimumCoins(*[[3, 10, 25, 47, 49, 10, 49]]) == 38\nassert my_solution.minimumCoins(*[[46, 7, 15]]) == 53\nassert my_solution.minimumCoins(*[[16, 45, 25, 5, 18, 19, 25, 13, 33]]) == 59\nassert my_solution.minimumCoins(*[[21, 16, 7, 10, 30]]) == 28\nassert my_solution.minimumCoins(*[[21, 22, 29, 37, 23, 15, 39, 9, 19, 10, 6, 9, 33, 28, 43]]) == 71\nassert my_solution.minimumCoins(*[[37, 16, 42, 47, 16, 31, 39, 8, 26, 50, 33]]) == 77\nassert my_solution.minimumCoins(*[[32, 4]]) == 32\nassert my_solution.minimumCoins(*[[31, 9, 2, 36, 4, 45, 28, 28, 12, 22, 44, 17, 10, 48, 15, 22, 7, 14, 41]]) == 56\nassert my_solution.minimumCoins(*[[1, 31, 9, 36, 44, 2, 23]]) == 12\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3209",
"questionFrontendId": "2944",
"questionTitle": "Minimum Number of Coins for Fruits",
"stats": {
"totalAccepted": "3.5K",
"totalSubmission": "5.9K",
"totalAcceptedRaw": 3538,
"totalSubmissionRaw": 5909,
"acRate": "59.9%"
}
} |
LeetCode/3211 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMaximumLength(*[[5, 2, 2]]) == 1\nassert my_solution.findMaximumLength(*[[1, 2, 3, 4]]) == 4\nassert my_solution.findMaximumLength(*[[4, 3, 2, 6]]) == 3\nassert my_solution.findMaximumLength(*[[32]]) == 1\nassert my_solution.findMaximumLength(*[[38]]) == 1\nassert my_solution.findMaximumLength(*[[60]]) == 1\nassert my_solution.findMaximumLength(*[[79]]) == 1\nassert my_solution.findMaximumLength(*[[85]]) == 1\nassert my_solution.findMaximumLength(*[[170]]) == 1\nassert my_solution.findMaximumLength(*[[198]]) == 1\nassert my_solution.findMaximumLength(*[[220]]) == 1\nassert my_solution.findMaximumLength(*[[318]]) == 1\nassert my_solution.findMaximumLength(*[[350]]) == 1\nassert my_solution.findMaximumLength(*[[381]]) == 1\nassert my_solution.findMaximumLength(*[[413]]) == 1\nassert my_solution.findMaximumLength(*[[426]]) == 1\nassert my_solution.findMaximumLength(*[[429]]) == 1\nassert my_solution.findMaximumLength(*[[431]]) == 1\nassert my_solution.findMaximumLength(*[[445]]) == 1\nassert my_solution.findMaximumLength(*[[488]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3211",
"questionFrontendId": "2945",
"questionTitle": "Find Maximum Non-decreasing Array Length",
"stats": {
"totalAccepted": "1.1K",
"totalSubmission": "4.1K",
"totalAcceptedRaw": 1054,
"totalSubmissionRaw": 4133,
"acRate": "25.5%"
}
} |
LeetCode/3207 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findMinimumOperations(*['abc', 'abb', 'ab']) == 2\nassert my_solution.findMinimumOperations(*['dac', 'bac', 'cac']) == -1\nassert my_solution.findMinimumOperations(*['a', 'a', 'a']) == 0\nassert my_solution.findMinimumOperations(*['kui', 'm', 'v']) == -1\nassert my_solution.findMinimumOperations(*['a', 'aabc', 'a']) == 3\nassert my_solution.findMinimumOperations(*['cc', 'cccb', 'c']) == 4\nassert my_solution.findMinimumOperations(*['luso', 'lu', 'lu']) == 2\nassert my_solution.findMinimumOperations(*['xx', 'phe', 'xie']) == -1\nassert my_solution.findMinimumOperations(*['gzd', 'bcju', 'db']) == -1\nassert my_solution.findMinimumOperations(*['cbba', 'cbaa', 'c']) == 6\nassert my_solution.findMinimumOperations(*['k', 'kfb', 'krcnf']) == 6\nassert my_solution.findMinimumOperations(*['oby', 'obz', 'obf']) == 3\nassert my_solution.findMinimumOperations(*['b', 'aba', 'aaccaa']) == -1\nassert my_solution.findMinimumOperations(*['a', 'accabb', 'aaa']) == 7\nassert my_solution.findMinimumOperations(*['b', 'bccaaba', 'ba']) == 7\nassert my_solution.findMinimumOperations(*['b', 'bacccab', 'cc']) == -1\nassert my_solution.findMinimumOperations(*['ca', 'cccabb', 'cb']) == 7\nassert my_solution.findMinimumOperations(*['ccb', 'ccba', 'ccb']) == 1\nassert my_solution.findMinimumOperations(*['mbooi', 'pdq', 'br']) == -1\nassert my_solution.findMinimumOperations(*['xxfzj', 'faho', 'c']) == -1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3207",
"questionFrontendId": "2937",
"questionTitle": "Make Three Strings Equal",
"stats": {
"totalAccepted": "5.9K",
"totalSubmission": "12.7K",
"totalAcceptedRaw": 5885,
"totalSubmissionRaw": 12732,
"acRate": "46.2%"
}
} |
LeetCode/3195 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumSteps(*['101']) == 1\nassert my_solution.minimumSteps(*['100']) == 2\nassert my_solution.minimumSteps(*['0111']) == 0\nassert my_solution.minimumSteps(*['11000111']) == 6\nassert my_solution.minimumSteps(*['01010001']) == 7\nassert my_solution.minimumSteps(*['0100101']) == 4\nassert my_solution.minimumSteps(*['111111111100100010']) == 65\nassert my_solution.minimumSteps(*['10100000110010011010']) == 44\nassert my_solution.minimumSteps(*['1101110000111011110']) == 42\nassert my_solution.minimumSteps(*['01000010111010001']) == 29\nassert my_solution.minimumSteps(*['11110']) == 4\nassert my_solution.minimumSteps(*['010001001011010']) == 21\nassert my_solution.minimumSteps(*['0011011']) == 2\nassert my_solution.minimumSteps(*['001']) == 0\nassert my_solution.minimumSteps(*['000100100']) == 6\nassert my_solution.minimumSteps(*['00110']) == 2\nassert my_solution.minimumSteps(*['001110001110001']) == 27\nassert my_solution.minimumSteps(*['10000000001']) == 9\nassert my_solution.minimumSteps(*['01']) == 0\nassert my_solution.minimumSteps(*['0100011100001100100']) == 45\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3195",
"questionFrontendId": "2938",
"questionTitle": "Separate Black and White Balls",
"stats": {
"totalAccepted": "5.7K",
"totalSubmission": "10.6K",
"totalAcceptedRaw": 5664,
"totalSubmissionRaw": 10560,
"acRate": "53.6%"
}
} |
LeetCode/3192 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumXorProduct(*[12, 5, 4]) == 98\nassert my_solution.maximumXorProduct(*[6, 7, 5]) == 930\nassert my_solution.maximumXorProduct(*[1, 6, 3]) == 12\nassert my_solution.maximumXorProduct(*[0, 0, 1]) == 1\nassert my_solution.maximumXorProduct(*[0, 1, 6]) == 3906\nassert my_solution.maximumXorProduct(*[0, 2, 7]) == 15875\nassert my_solution.maximumXorProduct(*[0, 3, 1]) == 2\nassert my_solution.maximumXorProduct(*[0, 4, 0]) == 0\nassert my_solution.maximumXorProduct(*[0, 5, 6]) == 3658\nassert my_solution.maximumXorProduct(*[0, 6, 1]) == 7\nassert my_solution.maximumXorProduct(*[0, 7, 2]) == 12\nassert my_solution.maximumXorProduct(*[0, 8, 5]) == 713\nassert my_solution.maximumXorProduct(*[0, 9, 2]) == 30\nassert my_solution.maximumXorProduct(*[0, 10, 7]) == 14875\nassert my_solution.maximumXorProduct(*[0, 11, 4]) == 84\nassert my_solution.maximumXorProduct(*[0, 12, 2]) == 45\nassert my_solution.maximumXorProduct(*[0, 13, 2]) == 42\nassert my_solution.maximumXorProduct(*[0, 14, 0]) == 0\nassert my_solution.maximumXorProduct(*[0, 15, 6]) == 3080\nassert my_solution.maximumXorProduct(*[1, 0, 3]) == 42\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3192",
"questionFrontendId": "2939",
"questionTitle": "Maximum Xor Product",
"stats": {
"totalAccepted": "3.4K",
"totalSubmission": "11.2K",
"totalAcceptedRaw": 3379,
"totalSubmissionRaw": 11169,
"acRate": "30.3%"
}
} |
LeetCode/3181 | # 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]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.leftmostBuildingQueries(*[[6, 4, 8, 5, 2, 7], [[0, 1], [0, 3], [2, 4], [3, 4], [2, 2]]]) == [2, 5, -1, 5, 2]\nassert my_solution.leftmostBuildingQueries(*[[5, 3, 8, 2, 6, 1, 4, 6], [[0, 7], [3, 5], [5, 2], [3, 0], [1, 6]]]) == [7, 6, -1, 4, 6]\nassert my_solution.leftmostBuildingQueries(*[[1], [[0, 0]]]) == [0]\nassert my_solution.leftmostBuildingQueries(*[[1000000000], [[0, 0]]]) == [0]\nassert my_solution.leftmostBuildingQueries(*[[1, 2], [[0, 0], [0, 1], [1, 0], [1, 1]]]) == [0, 1, 1, 1]\nassert my_solution.leftmostBuildingQueries(*[[2, 1], [[0, 0], [0, 1], [1, 0], [1, 1]]]) == [0, -1, -1, 1]\nassert my_solution.leftmostBuildingQueries(*[[1, 2, 3], [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]]) == [0, 1, 2, 1, 1, 2, 2, 2, 2]\nassert my_solution.leftmostBuildingQueries(*[[1, 3, 2], [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]]) == [0, 1, 2, 1, 1, -1, 2, -1, 2]\nassert my_solution.leftmostBuildingQueries(*[[2, 1, 3], [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]]) == [0, 2, 2, 2, 1, 2, 2, 2, 2]\nassert my_solution.leftmostBuildingQueries(*[[2, 3, 1], [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]]) == [0, 1, -1, 1, 1, -1, -1, -1, 2]\nassert my_solution.leftmostBuildingQueries(*[[3, 1, 2], [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]]) == [0, -1, -1, -1, 1, 2, -1, 2, 2]\nassert my_solution.leftmostBuildingQueries(*[[3, 2, 1], [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]]) == [0, -1, -1, -1, 1, -1, -1, -1, 2]\nassert my_solution.leftmostBuildingQueries(*[[1, 2, 3, 4], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 1, 2, 3, 1, 1, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3]\nassert my_solution.leftmostBuildingQueries(*[[1, 2, 4, 3], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 1, 2, 3, 1, 1, 2, 3, 2, 2, 2, -1, 3, 3, -1, 3]\nassert my_solution.leftmostBuildingQueries(*[[1, 3, 2, 4], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 1, 2, 3, 1, 1, 3, 3, 2, 3, 2, 3, 3, 3, 3, 3]\nassert my_solution.leftmostBuildingQueries(*[[1, 3, 4, 2], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 1, 2, 3, 1, 1, 2, -1, 2, 2, 2, -1, 3, -1, -1, 3]\nassert my_solution.leftmostBuildingQueries(*[[1, 4, 2, 3], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 1, 2, 3, 1, 1, -1, -1, 2, -1, 2, 3, 3, -1, 3, 3]\nassert my_solution.leftmostBuildingQueries(*[[1, 4, 3, 2], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 1, 2, 3, 1, 1, -1, -1, 2, -1, 2, -1, 3, -1, -1, 3]\nassert my_solution.leftmostBuildingQueries(*[[2, 1, 3, 4], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 2, 2, 3, 2, 1, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3]\nassert my_solution.leftmostBuildingQueries(*[[2, 1, 4, 3], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]]) == [0, 2, 2, 3, 2, 1, 2, 3, 2, 2, 2, -1, 3, 3, -1, 3]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3181",
"questionFrontendId": "2940",
"questionTitle": "Find Building Where Alice and Bob Can Meet",
"stats": {
"totalAccepted": "3K",
"totalSubmission": "6.5K",
"totalAcceptedRaw": 3005,
"totalSubmissionRaw": 6491,
"acRate": "46.3%"
}
} |
LeetCode/3193 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumStrongPairXor(*[[1, 2, 3, 4, 5]]) == 7\nassert my_solution.maximumStrongPairXor(*[[10, 100]]) == 0\nassert my_solution.maximumStrongPairXor(*[[5, 6, 25, 30]]) == 7\nassert my_solution.maximumStrongPairXor(*[[1]]) == 0\nassert my_solution.maximumStrongPairXor(*[[100]]) == 0\nassert my_solution.maximumStrongPairXor(*[[1, 1, 2, 3, 5]]) == 6\nassert my_solution.maximumStrongPairXor(*[[1, 1, 3, 8, 7]]) == 15\nassert my_solution.maximumStrongPairXor(*[[1, 1, 4, 4, 3]]) == 7\nassert my_solution.maximumStrongPairXor(*[[1, 1, 6, 6, 9]]) == 15\nassert my_solution.maximumStrongPairXor(*[[1, 1, 10, 3, 9]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 2, 1, 5, 3]]) == 6\nassert my_solution.maximumStrongPairXor(*[[1, 2, 2, 1, 2]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 2, 3, 8, 1]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 2, 5, 5, 10]]) == 15\nassert my_solution.maximumStrongPairXor(*[[1, 2, 8, 3, 2]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 2, 9, 2, 8]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 3, 3, 2, 1]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 3, 8, 5, 3]]) == 13\nassert my_solution.maximumStrongPairXor(*[[1, 3, 9, 6, 5]]) == 15\nassert my_solution.maximumStrongPairXor(*[[1, 4, 1, 2, 5]]) == 6\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3193",
"questionFrontendId": "2932",
"questionTitle": "Maximum Strong Pair XOR I",
"stats": {
"totalAccepted": "5.4K",
"totalSubmission": "7.2K",
"totalAcceptedRaw": 5422,
"totalSubmissionRaw": 7215,
"acRate": "75.1%"
}
} |
LeetCode/3202 | # 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]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findHighAccessEmployees(*[[['a', '0549'], ['b', '0457'], ['a', '0532'], ['a', '0621'], ['b', '0540']]]) == ['a']\nassert my_solution.findHighAccessEmployees(*[[['d', '0002'], ['c', '0808'], ['c', '0829'], ['e', '0215'], ['d', '1508'], ['d', '1444'], ['d', '1410'], ['c', '0809']]]) == ['c', 'd']\nassert my_solution.findHighAccessEmployees(*[[['cd', '1025'], ['ab', '1025'], ['cd', '1046'], ['cd', '1055'], ['ab', '1124'], ['ab', '1120']]]) == ['ab', 'cd']\nassert my_solution.findHighAccessEmployees(*[[['baipstt', '1456']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['bouo', '1126']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['cavfbqg', '2304']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['cenjcq', '1007']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['cqotrwqcaq', '0131']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['downbuk', '1951']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['dqsoiyz', '2204']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['duzeyrov', '0243']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['erfg', '1223']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['fwhefd', '2026']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['gbefbne', '0911']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['gp', '1540']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['ht', '1319']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['inahnsjdqz', '1750']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['jwxvijxo', '0851']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['kibwwvjuez', '0716']]]) == []\nassert my_solution.findHighAccessEmployees(*[[['lvry', '0706']]]) == []\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3202",
"questionFrontendId": "2933",
"questionTitle": "High-Access Employees",
"stats": {
"totalAccepted": "4.8K",
"totalSubmission": "9.7K",
"totalAcceptedRaw": 4847,
"totalSubmissionRaw": 9734,
"acRate": "49.8%"
}
} |
LeetCode/3190 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minOperations(*[[1, 2, 7], [4, 5, 3]]) == 1\nassert my_solution.minOperations(*[[2, 3, 4, 5, 9], [8, 8, 4, 4, 4]]) == 2\nassert my_solution.minOperations(*[[1, 5, 4], [2, 5, 3]]) == -1\nassert my_solution.minOperations(*[[1], [1]]) == 0\nassert my_solution.minOperations(*[[1, 2], [2, 1]]) == 1\nassert my_solution.minOperations(*[[1, 1, 10], [1, 5, 1]]) == 1\nassert my_solution.minOperations(*[[1, 4, 16], [16, 16, 16]]) == 0\nassert my_solution.minOperations(*[[1, 5, 15], [1, 1, 1]]) == 0\nassert my_solution.minOperations(*[[2, 5, 7], [2, 2, 2]]) == 0\nassert my_solution.minOperations(*[[8, 9, 10], [10, 9, 9]]) == 1\nassert my_solution.minOperations(*[[9, 14, 14], [14, 11, 14]]) == 0\nassert my_solution.minOperations(*[[16, 16, 16], [6, 7, 16]]) == 0\nassert my_solution.minOperations(*[[19, 7, 19], [5, 19, 19]]) == 0\nassert my_solution.minOperations(*[[1, 1, 8, 9], [1, 7, 1, 1]]) == 1\nassert my_solution.minOperations(*[[1, 5, 9, 9], [9, 9, 8, 9]]) == 0\nassert my_solution.minOperations(*[[1, 7, 7, 7], [7, 3, 3, 7]]) == 0\nassert my_solution.minOperations(*[[10, 18, 12, 12], [19, 6, 5, 12]]) == -1\nassert my_solution.minOperations(*[[12, 9, 11, 12], [3, 9, 9, 9]]) == 0\nassert my_solution.minOperations(*[[15, 54, 22, 54], [54, 19, 54, 54]]) == 0\nassert my_solution.minOperations(*[[20, 20, 20, 20], [5, 8, 19, 20]]) == 0\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3190",
"questionFrontendId": "2934",
"questionTitle": "Minimum Operations to Maximize Last Elements in Arrays",
"stats": {
"totalAccepted": "4K",
"totalSubmission": "8.5K",
"totalAcceptedRaw": 3987,
"totalSubmissionRaw": 8460,
"acRate": "47.1%"
}
} |
LeetCode/3197 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumStrongPairXor(*[[1, 2, 3, 4, 5]]) == 7\nassert my_solution.maximumStrongPairXor(*[[10, 100]]) == 0\nassert my_solution.maximumStrongPairXor(*[[500, 520, 2500, 3000]]) == 1020\nassert my_solution.maximumStrongPairXor(*[[1]]) == 0\nassert my_solution.maximumStrongPairXor(*[[2, 3]]) == 1\nassert my_solution.maximumStrongPairXor(*[[3, 4]]) == 7\nassert my_solution.maximumStrongPairXor(*[[4, 5]]) == 1\nassert my_solution.maximumStrongPairXor(*[[5, 6]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 1, 1]]) == 0\nassert my_solution.maximumStrongPairXor(*[[1, 1, 2]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 1, 3]]) == 0\nassert my_solution.maximumStrongPairXor(*[[1, 1, 4]]) == 0\nassert my_solution.maximumStrongPairXor(*[[1, 1, 5]]) == 0\nassert my_solution.maximumStrongPairXor(*[[1, 2, 1]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 2, 2]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 2, 3]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 2, 4]]) == 6\nassert my_solution.maximumStrongPairXor(*[[1, 2, 5]]) == 3\nassert my_solution.maximumStrongPairXor(*[[1, 3, 1]]) == 0\nassert my_solution.maximumStrongPairXor(*[[1, 3, 2]]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3197",
"questionFrontendId": "2935",
"questionTitle": "Maximum Strong Pair XOR II",
"stats": {
"totalAccepted": "3.5K",
"totalSubmission": "9.1K",
"totalAcceptedRaw": 3499,
"totalSubmissionRaw": 9066,
"acRate": "38.6%"
}
} |
LeetCode/3199 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.distributeCandies(*[5, 2]) == 3\nassert my_solution.distributeCandies(*[3, 3]) == 10\nassert my_solution.distributeCandies(*[1, 1]) == 3\nassert my_solution.distributeCandies(*[1, 2]) == 3\nassert my_solution.distributeCandies(*[1, 3]) == 3\nassert my_solution.distributeCandies(*[1, 4]) == 3\nassert my_solution.distributeCandies(*[1, 5]) == 3\nassert my_solution.distributeCandies(*[1, 6]) == 3\nassert my_solution.distributeCandies(*[1, 7]) == 3\nassert my_solution.distributeCandies(*[1, 8]) == 3\nassert my_solution.distributeCandies(*[1, 9]) == 3\nassert my_solution.distributeCandies(*[1, 10]) == 3\nassert my_solution.distributeCandies(*[1, 11]) == 3\nassert my_solution.distributeCandies(*[1, 12]) == 3\nassert my_solution.distributeCandies(*[1, 13]) == 3\nassert my_solution.distributeCandies(*[1, 14]) == 3\nassert my_solution.distributeCandies(*[1, 15]) == 3\nassert my_solution.distributeCandies(*[1, 16]) == 3\nassert my_solution.distributeCandies(*[1, 17]) == 3\nassert my_solution.distributeCandies(*[1, 18]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3199",
"questionFrontendId": "2928",
"questionTitle": "Distribute Candies Among Children I",
"stats": {
"totalAccepted": "3.5K",
"totalSubmission": "4.7K",
"totalAcceptedRaw": 3504,
"totalSubmissionRaw": 4722,
"acRate": "74.2%"
}
} |
LeetCode/3201 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.distributeCandies(*[5, 2]) == 3\nassert my_solution.distributeCandies(*[3, 3]) == 10\nassert my_solution.distributeCandies(*[1, 1]) == 3\nassert my_solution.distributeCandies(*[1, 2]) == 3\nassert my_solution.distributeCandies(*[1, 3]) == 3\nassert my_solution.distributeCandies(*[1, 4]) == 3\nassert my_solution.distributeCandies(*[1, 5]) == 3\nassert my_solution.distributeCandies(*[1, 6]) == 3\nassert my_solution.distributeCandies(*[1, 7]) == 3\nassert my_solution.distributeCandies(*[1, 8]) == 3\nassert my_solution.distributeCandies(*[1, 9]) == 3\nassert my_solution.distributeCandies(*[1, 10]) == 3\nassert my_solution.distributeCandies(*[1, 11]) == 3\nassert my_solution.distributeCandies(*[1, 12]) == 3\nassert my_solution.distributeCandies(*[1, 13]) == 3\nassert my_solution.distributeCandies(*[1, 14]) == 3\nassert my_solution.distributeCandies(*[1, 15]) == 3\nassert my_solution.distributeCandies(*[1, 16]) == 3\nassert my_solution.distributeCandies(*[1, 17]) == 3\nassert my_solution.distributeCandies(*[1, 18]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3201",
"questionFrontendId": "2929",
"questionTitle": "Distribute Candies Among Children II",
"stats": {
"totalAccepted": "2.8K",
"totalSubmission": "7.6K",
"totalAcceptedRaw": 2777,
"totalSubmissionRaw": 7586,
"acRate": "36.6%"
}
} |
LeetCode/3200 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.stringCount(*[4]) == 12\nassert my_solution.stringCount(*[10]) == 83943898\nassert my_solution.stringCount(*[1]) == 0\nassert my_solution.stringCount(*[2]) == 0\nassert my_solution.stringCount(*[3]) == 0\nassert my_solution.stringCount(*[5]) == 1460\nassert my_solution.stringCount(*[6]) == 106620\nassert my_solution.stringCount(*[7]) == 6058192\nassert my_solution.stringCount(*[8]) == 295164156\nassert my_solution.stringCount(*[9]) == 947613240\nassert my_solution.stringCount(*[11]) == 795234177\nassert my_solution.stringCount(*[12]) == 55396773\nassert my_solution.stringCount(*[13]) == 968092561\nassert my_solution.stringCount(*[14]) == 715599898\nassert my_solution.stringCount(*[15]) == 430509685\nassert my_solution.stringCount(*[16]) == 462719236\nassert my_solution.stringCount(*[17]) == 155543310\nassert my_solution.stringCount(*[18]) == 159683962\nassert my_solution.stringCount(*[19]) == 808507313\nassert my_solution.stringCount(*[20]) == 291395991\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3200",
"questionFrontendId": "2930",
"questionTitle": "Number of Strings Which Can Be Rearranged to Contain Substring",
"stats": {
"totalAccepted": "1.9K",
"totalSubmission": "3K",
"totalAcceptedRaw": 1862,
"totalSubmissionRaw": 2990,
"acRate": "62.3%"
}
} |
LeetCode/3107 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxSpending(*[[[8, 5, 2], [6, 4, 1], [9, 7, 3]]]) == 285\nassert my_solution.maxSpending(*[[[10, 8, 6, 4, 2], [9, 7, 5, 3, 2]]]) == 386\nassert my_solution.maxSpending(*[[[1000000]]]) == 1000000\nassert my_solution.maxSpending(*[[[1]]]) == 1\nassert my_solution.maxSpending(*[[[1], [2]]]) == 5\nassert my_solution.maxSpending(*[[[2], [1]]]) == 5\nassert my_solution.maxSpending(*[[[1], [1]]]) == 3\nassert my_solution.maxSpending(*[[[5, 2]]]) == 12\nassert my_solution.maxSpending(*[[[5, 5]]]) == 15\nassert my_solution.maxSpending(*[[[7, 5]]]) == 19\nassert my_solution.maxSpending(*[[[3, 2, 1]]]) == 14\nassert my_solution.maxSpending(*[[[2, 2, 1]]]) == 11\nassert my_solution.maxSpending(*[[[3, 3, 2]]]) == 17\nassert my_solution.maxSpending(*[[[3], [2], [1]]]) == 14\nassert my_solution.maxSpending(*[[[2], [10], [1]]]) == 35\nassert my_solution.maxSpending(*[[[1000000, 1000000, 1000000]]]) == 6000000\nassert my_solution.maxSpending(*[[[1000000, 1000000, 1000000, 1000000]]]) == 10000000\nassert my_solution.maxSpending(*[[[1000000], [1000000], [1000000], [1000000]]]) == 10000000\nassert my_solution.maxSpending(*[[[1000000, 1000000], [1000000, 1000000]]]) == 10000000\nassert my_solution.maxSpending(*[[[2, 1], [4, 3]]]) == 30\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3107",
"questionFrontendId": "2931",
"questionTitle": "Maximum Spending After Buying Items",
"stats": {
"totalAccepted": "2.1K",
"totalSubmission": "3.1K",
"totalAcceptedRaw": 2144,
"totalSubmissionRaw": 3118,
"acRate": "68.8%"
}
} |
LeetCode/3188 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findChampion(*[[[0, 1], [0, 0]]]) == 0\nassert my_solution.findChampion(*[[[0, 0, 1], [1, 0, 1], [0, 0, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 0], [1, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 0, 0], [1, 0, 0], [1, 1, 0]]]) == 2\nassert my_solution.findChampion(*[[[0, 0, 0], [1, 0, 1], [1, 0, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 1, 0], [0, 0, 0], [1, 1, 0]]]) == 2\nassert my_solution.findChampion(*[[[0, 1, 1], [0, 0, 0], [0, 1, 0]]]) == 0\nassert my_solution.findChampion(*[[[0, 1, 1], [0, 0, 1], [0, 0, 0]]]) == 0\nassert my_solution.findChampion(*[[[0, 0, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]]) == 3\nassert my_solution.findChampion(*[[[0, 0, 0, 0], [1, 0, 0, 0], [1, 1, 0, 1], [1, 1, 0, 0]]]) == 2\nassert my_solution.findChampion(*[[[0, 0, 0, 0], [1, 0, 0, 1], [1, 1, 0, 1], [1, 0, 0, 0]]]) == 2\nassert my_solution.findChampion(*[[[0, 0, 0, 0], [1, 0, 1, 0], [1, 0, 0, 0], [1, 1, 1, 0]]]) == 3\nassert my_solution.findChampion(*[[[0, 0, 0, 0], [1, 0, 1, 1], [1, 0, 0, 0], [1, 0, 1, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 0, 0, 0], [1, 0, 1, 1], [1, 0, 0, 1], [1, 0, 0, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 0, 0, 1], [1, 0, 0, 1], [1, 1, 0, 1], [0, 0, 0, 0]]]) == 2\nassert my_solution.findChampion(*[[[0, 0, 0, 1], [1, 0, 1, 1], [1, 0, 0, 1], [0, 0, 0, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 0, 1, 0], [1, 0, 1, 0], [0, 0, 0, 0], [1, 1, 1, 0]]]) == 3\nassert my_solution.findChampion(*[[[0, 0, 1, 0], [1, 0, 1, 1], [0, 0, 0, 0], [1, 0, 1, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 1, 0]]]) == 1\nassert my_solution.findChampion(*[[[0, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 1], [0, 0, 0, 0]]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3188",
"questionFrontendId": "2923",
"questionTitle": "Find Champion I",
"stats": {
"totalAccepted": "6.3K",
"totalSubmission": "8.3K",
"totalAcceptedRaw": 6315,
"totalSubmissionRaw": 8300,
"acRate": "76.1%"
}
} |
LeetCode/3184 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxBalancedSubsequenceSum(*[[3, 3, 5, 6]]) == 14\nassert my_solution.maxBalancedSubsequenceSum(*[[5, -1, -3, 8]]) == 13\nassert my_solution.maxBalancedSubsequenceSum(*[[-2, -1]]) == -1\nassert my_solution.maxBalancedSubsequenceSum(*[[0]]) == 0\nassert my_solution.maxBalancedSubsequenceSum(*[[-47]]) == -47\nassert my_solution.maxBalancedSubsequenceSum(*[[-8]]) == -8\nassert my_solution.maxBalancedSubsequenceSum(*[[-7]]) == -7\nassert my_solution.maxBalancedSubsequenceSum(*[[-6]]) == -6\nassert my_solution.maxBalancedSubsequenceSum(*[[-5]]) == -5\nassert my_solution.maxBalancedSubsequenceSum(*[[-3]]) == -3\nassert my_solution.maxBalancedSubsequenceSum(*[[-2]]) == -2\nassert my_solution.maxBalancedSubsequenceSum(*[[-1]]) == -1\nassert my_solution.maxBalancedSubsequenceSum(*[[1]]) == 1\nassert my_solution.maxBalancedSubsequenceSum(*[[3]]) == 3\nassert my_solution.maxBalancedSubsequenceSum(*[[4]]) == 4\nassert my_solution.maxBalancedSubsequenceSum(*[[5]]) == 5\nassert my_solution.maxBalancedSubsequenceSum(*[[7]]) == 7\nassert my_solution.maxBalancedSubsequenceSum(*[[8]]) == 8\nassert my_solution.maxBalancedSubsequenceSum(*[[9]]) == 9\nassert my_solution.maxBalancedSubsequenceSum(*[[45]]) == 45\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3184",
"questionFrontendId": "2926",
"questionTitle": "Maximum Balanced Subsequence Sum",
"stats": {
"totalAccepted": "2.7K",
"totalSubmission": "7.2K",
"totalAcceptedRaw": 2737,
"totalSubmissionRaw": 7185,
"acRate": "38.1%"
}
} |
LeetCode/3183 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findKOr(*[[7, 12, 9, 8, 9, 15], 4]) == 9\nassert my_solution.findKOr(*[[2, 12, 1, 11, 4, 5], 6]) == 0\nassert my_solution.findKOr(*[[10, 8, 5, 9, 11, 6, 8], 1]) == 15\nassert my_solution.findKOr(*[[14, 7, 12, 9, 8, 9, 1, 15], 4]) == 13\nassert my_solution.findKOr(*[[2, 12, 1, 11, 4, 5], 3]) == 5\nassert my_solution.findKOr(*[[10, 8, 5, 10, 11, 11, 6, 8], 1]) == 15\nassert my_solution.findKOr(*[[0], 1]) == 0\nassert my_solution.findKOr(*[[1], 1]) == 1\nassert my_solution.findKOr(*[[2], 1]) == 2\nassert my_solution.findKOr(*[[3], 1]) == 3\nassert my_solution.findKOr(*[[4], 1]) == 4\nassert my_solution.findKOr(*[[5], 1]) == 5\nassert my_solution.findKOr(*[[6], 1]) == 6\nassert my_solution.findKOr(*[[7], 1]) == 7\nassert my_solution.findKOr(*[[8], 1]) == 8\nassert my_solution.findKOr(*[[9], 1]) == 9\nassert my_solution.findKOr(*[[10], 1]) == 10\nassert my_solution.findKOr(*[[11], 1]) == 11\nassert my_solution.findKOr(*[[12], 1]) == 12\nassert my_solution.findKOr(*[[13], 1]) == 13\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3183",
"questionFrontendId": "2917",
"questionTitle": "Find the K-or of an Array",
"stats": {
"totalAccepted": "5.6K",
"totalSubmission": "7.9K",
"totalAcceptedRaw": 5625,
"totalSubmissionRaw": 7924,
"acRate": "71.0%"
}
} |
LeetCode/3171 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minSum(*[[3, 2, 0, 1, 0], [6, 5, 0]]) == 12\nassert my_solution.minSum(*[[2, 0, 2, 0], [1, 4]]) == -1\nassert my_solution.minSum(*[[0, 7, 28, 17, 18], [1, 2, 6, 26, 1, 0, 27, 3, 0, 30]]) == 98\nassert my_solution.minSum(*[[8, 13, 15, 18, 0, 18, 0, 0, 5, 20, 12, 27, 3, 14, 22, 0], [29, 1, 6, 0, 10, 24, 27, 17, 14, 13, 2, 19, 2, 11]]) == 179\nassert my_solution.minSum(*[[9, 5], [15, 12, 5, 21, 4, 26, 27, 9, 6, 29, 0, 18, 16, 0, 0, 0, 20]]) == -1\nassert my_solution.minSum(*[[0, 29, 5, 22, 5, 9, 30, 11, 20, 0, 18, 16, 26, 11, 3, 0, 24, 24, 14, 24], [30, 12, 16, 3, 24, 6, 13, 0, 16]]) == 294\nassert my_solution.minSum(*[[9, 13, 0, 0, 12, 10, 0, 8, 0, 0, 5, 13, 0], [8, 14, 11, 2, 27, 0, 0]]) == 76\nassert my_solution.minSum(*[[3, 0, 20, 9, 20, 0, 20, 25, 26, 9, 0, 12, 6, 11, 0, 6], [0, 3, 8, 13, 27, 0, 0, 0, 29, 27, 0, 11, 23, 0, 19, 19, 0]]) == 186\nassert my_solution.minSum(*[[25, 28, 13, 0, 14, 23, 14, 0, 3, 3, 12], [24, 30, 0, 15, 20, 19, 18, 0, 23, 23, 0, 16, 26, 0, 29, 19, 16, 25]]) == 307\nassert my_solution.minSum(*[[0, 29, 30, 18, 5, 24, 16, 5, 17, 0, 18, 16, 26, 0, 15, 19, 14, 20, 3, 26], [0, 8, 14, 11, 13, 6, 8, 0, 13]]) == 304\nassert my_solution.minSum(*[[0, 17, 20, 17, 5, 0, 14, 19, 7, 8, 16, 18, 6], [21, 1, 27, 19, 2, 2, 24, 21, 16, 1, 13, 27, 8, 5, 3, 11, 13, 7, 29, 7]]) == 257\nassert my_solution.minSum(*[[26, 1, 25, 10, 14, 14, 4, 0, 10, 0, 23], [23, 8, 30, 18, 8, 15, 6, 9, 0, 2, 0, 0, 19, 8, 19, 4, 10]]) == 182\nassert my_solution.minSum(*[[15, 10, 7, 16], [8, 16, 2, 6, 4, 12, 6, 16, 24, 0]]) == -1\nassert my_solution.minSum(*[[0, 0, 0, 17, 0, 6, 2, 22, 12, 0, 25, 18, 1, 12, 19, 0, 0], [0, 0, 0, 30, 4, 3, 13, 25, 9, 25, 3, 0, 1, 12, 2, 10, 4, 7, 30, 16]]) == 198\nassert my_solution.minSum(*[[23, 17], [7, 3, 22, 0, 12]]) == -1\nassert my_solution.minSum(*[[15, 0, 8, 30, 6, 3, 24, 6, 0, 11, 13, 30, 6, 25, 23, 3], [12, 20, 0, 6, 0, 0, 14, 0, 0, 8, 5, 19, 16, 0, 0, 15]]) == 205\nassert my_solution.minSum(*[[3, 25, 1, 13], [19, 13, 10, 27, 10, 20, 27, 0, 3, 12, 16, 26, 0, 27]]) == -1\nassert my_solution.minSum(*[[0, 0], [29, 28]]) == 57\nassert my_solution.minSum(*[[17, 4, 11, 8, 0, 17, 0, 0, 12, 27, 20, 28, 0, 30, 21, 18, 12], [0, 2, 30, 0, 5, 17, 0, 0, 0, 15, 11, 2, 25, 18, 18]]) == 229\nassert my_solution.minSum(*[[0, 17, 0, 7, 29, 10, 22, 27, 13, 8, 19], [26, 23, 8, 14, 0, 17, 20, 4, 26, 15, 0, 9, 14, 0, 12, 10, 23, 16]]) == 240\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3171",
"questionFrontendId": "2918",
"questionTitle": "Minimum Equal Sum of Two Arrays After Replacing Zeros",
"stats": {
"totalAccepted": "5.4K",
"totalSubmission": "15.3K",
"totalAcceptedRaw": 5439,
"totalSubmissionRaw": 15335,
"acRate": "35.5%"
}
} |
LeetCode/3178 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minIncrementOperations(*[[2, 3, 0, 0, 2], 4]) == 3\nassert my_solution.minIncrementOperations(*[[0, 1, 3, 3], 5]) == 2\nassert my_solution.minIncrementOperations(*[[1, 1, 2], 1]) == 0\nassert my_solution.minIncrementOperations(*[[0, 5, 5], 8]) == 3\nassert my_solution.minIncrementOperations(*[[0, 18, 28], 93]) == 65\nassert my_solution.minIncrementOperations(*[[0, 24, 14], 7]) == 0\nassert my_solution.minIncrementOperations(*[[2, 3, 4], 3]) == 0\nassert my_solution.minIncrementOperations(*[[3, 5, 9], 6]) == 0\nassert my_solution.minIncrementOperations(*[[4, 3, 0], 2]) == 0\nassert my_solution.minIncrementOperations(*[[5, 6, 5], 9]) == 3\nassert my_solution.minIncrementOperations(*[[6, 9, 6], 3]) == 0\nassert my_solution.minIncrementOperations(*[[7, 9, 0], 6]) == 0\nassert my_solution.minIncrementOperations(*[[7, 47, 16], 39]) == 0\nassert my_solution.minIncrementOperations(*[[9, 6, 1], 6]) == 0\nassert my_solution.minIncrementOperations(*[[41, 44, 37], 55]) == 11\nassert my_solution.minIncrementOperations(*[[48, 3, 13], 1]) == 0\nassert my_solution.minIncrementOperations(*[[1, 2, 6, 9], 8]) == 2\nassert my_solution.minIncrementOperations(*[[1, 3, 1, 6], 6]) == 3\nassert my_solution.minIncrementOperations(*[[2, 35, 41, 20], 4]) == 0\nassert my_solution.minIncrementOperations(*[[3, 9, 9, 7], 6]) == 0\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3178",
"questionFrontendId": "2919",
"questionTitle": "Minimum Increment Operations to Make Array Beautiful",
"stats": {
"totalAccepted": "4.2K",
"totalSubmission": "10.8K",
"totalAcceptedRaw": 4223,
"totalSubmissionRaw": 10751,
"acRate": "39.3%"
}
} |
LeetCode/3163 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sumCounts(*[[1, 2, 1]]) == 15\nassert my_solution.sumCounts(*[[1, 1]]) == 3\nassert my_solution.sumCounts(*[[2, 2, 5, 5]]) == 22\nassert my_solution.sumCounts(*[[5, 2, 4, 2, 1, 3, 2, 4, 3, 1]]) == 578\nassert my_solution.sumCounts(*[[2, 3, 2, 1, 2, 5, 3, 4, 5, 2]]) == 629\nassert my_solution.sumCounts(*[[5, 1, 5, 2, 3, 5, 1, 5, 1]]) == 385\nassert my_solution.sumCounts(*[[4, 5, 4, 3, 4, 2]]) == 120\nassert my_solution.sumCounts(*[[2]]) == 1\nassert my_solution.sumCounts(*[[3, 4, 2, 5, 2, 4, 1, 2, 2, 5]]) == 535\nassert my_solution.sumCounts(*[[4, 4, 2, 4, 1]]) == 57\nassert my_solution.sumCounts(*[[2, 2, 5]]) == 12\nassert my_solution.sumCounts(*[[4, 5, 1, 2, 2, 1, 3, 3]]) == 266\nassert my_solution.sumCounts(*[[3, 1, 5, 5, 2, 3, 2, 2, 1]]) == 334\nassert my_solution.sumCounts(*[[2, 5, 2, 5, 3, 2, 5, 2]]) == 205\nassert my_solution.sumCounts(*[[5, 4, 1, 4, 5, 2, 4]]) == 203\nassert my_solution.sumCounts(*[[1, 3, 3, 4, 3, 1, 2, 1]]) == 253\nassert my_solution.sumCounts(*[[4]]) == 1\nassert my_solution.sumCounts(*[[1, 4, 2, 1, 5, 4, 3, 1, 4]]) == 507\nassert my_solution.sumCounts(*[[2, 4, 5, 3, 2, 5, 1, 5, 4, 4]]) == 626\nassert my_solution.sumCounts(*[[3, 4, 1, 4, 5, 2, 2]]) == 220\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3163",
"questionFrontendId": "2913",
"questionTitle": "Subarrays Distinct Element Sum of Squares I",
"stats": {
"totalAccepted": "3.5K",
"totalSubmission": "4.4K",
"totalAcceptedRaw": 3491,
"totalSubmissionRaw": 4360,
"acRate": "80.1%"
}
} |
LeetCode/3174 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minChanges(*['1001']) == 2\nassert my_solution.minChanges(*['10']) == 1\nassert my_solution.minChanges(*['0000']) == 0\nassert my_solution.minChanges(*['11000111']) == 1\nassert my_solution.minChanges(*['01010001']) == 3\nassert my_solution.minChanges(*['010010']) == 2\nassert my_solution.minChanges(*['111111111110010001']) == 3\nassert my_solution.minChanges(*['01010000011001001101']) == 6\nassert my_solution.minChanges(*['011011100001110111']) == 5\nassert my_solution.minChanges(*['1001000010111010']) == 5\nassert my_solution.minChanges(*['0011']) == 0\nassert my_solution.minChanges(*['11100100010010']) == 4\nassert my_solution.minChanges(*['110100']) == 1\nassert my_solution.minChanges(*['01']) == 1\nassert my_solution.minChanges(*['10110010']) == 2\nassert my_solution.minChanges(*['0010']) == 1\nassert my_solution.minChanges(*['01000011000111']) == 2\nassert my_solution.minChanges(*['0001110001']) == 2\nassert my_solution.minChanges(*['000000001010100011']) == 3\nassert my_solution.minChanges(*['100001']) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3174",
"questionFrontendId": "2914",
"questionTitle": "Minimum Number of Changes to Make Binary String Beautiful",
"stats": {
"totalAccepted": "3.2K",
"totalSubmission": "4.3K",
"totalAcceptedRaw": 3232,
"totalSubmissionRaw": 4297,
"acRate": "75.2%"
}
} |
LeetCode/3106 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.lengthOfLongestSubsequence(*[[1, 2, 3, 4, 5], 9]) == 3\nassert my_solution.lengthOfLongestSubsequence(*[[4, 1, 3, 2, 1, 5], 7]) == 4\nassert my_solution.lengthOfLongestSubsequence(*[[1, 1, 5, 4, 5], 3]) == -1\nassert my_solution.lengthOfLongestSubsequence(*[[1000], 12]) == -1\nassert my_solution.lengthOfLongestSubsequence(*[[1000], 1000]) == 1\nassert my_solution.lengthOfLongestSubsequence(*[[1, 2], 10]) == -1\nassert my_solution.lengthOfLongestSubsequence(*[[1, 1000], 5]) == -1\nassert my_solution.lengthOfLongestSubsequence(*[[2, 3], 3]) == 1\nassert my_solution.lengthOfLongestSubsequence(*[[2, 3], 5]) == 2\nassert my_solution.lengthOfLongestSubsequence(*[[2, 3, 5], 5]) == 2\nassert my_solution.lengthOfLongestSubsequence(*[[1, 3, 3, 7], 1000]) == -1\nassert my_solution.lengthOfLongestSubsequence(*[[1, 3, 3, 7], 2]) == -1\nassert my_solution.lengthOfLongestSubsequence(*[[1, 3, 3, 8], 7]) == 3\nassert my_solution.lengthOfLongestSubsequence(*[[1, 1, 2, 1], 2]) == 2\nassert my_solution.lengthOfLongestSubsequence(*[[1, 1, 1, 1], 5]) == -1\nassert my_solution.lengthOfLongestSubsequence(*[[1, 1, 1, 2], 3]) == 3\nassert my_solution.lengthOfLongestSubsequence(*[[9, 12, 8, 4, 11, 13, 15, 7, 5], 84]) == 9\nassert my_solution.lengthOfLongestSubsequence(*[[11, 5, 9, 11, 12, 13, 12, 5, 1, 8], 87]) == 10\nassert my_solution.lengthOfLongestSubsequence(*[[9, 11, 11, 15, 4, 14, 3, 2, 13, 7], 89]) == 10\nassert my_solution.lengthOfLongestSubsequence(*[[11, 13, 6, 13, 10], 53]) == 5\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3106",
"questionFrontendId": "2915",
"questionTitle": "Length of the Longest Subsequence That Sums to Target",
"stats": {
"totalAccepted": "4.1K",
"totalSubmission": "10.1K",
"totalAcceptedRaw": 4053,
"totalSubmissionRaw": 10146,
"acRate": "39.9%"
}
} |
LeetCode/3139 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sumCounts(*[[1, 2, 1]]) == 15\nassert my_solution.sumCounts(*[[2, 2]]) == 3\nassert my_solution.sumCounts(*[[2, 2, 5, 5]]) == 22\nassert my_solution.sumCounts(*[[5, 2, 4, 2, 1, 3, 2, 4, 3, 1]]) == 578\nassert my_solution.sumCounts(*[[2, 3, 2, 1, 2, 5, 3, 4, 5, 2]]) == 629\nassert my_solution.sumCounts(*[[5, 1, 5, 2, 3, 5, 1, 5, 1]]) == 385\nassert my_solution.sumCounts(*[[4, 5, 4, 3, 4, 2]]) == 120\nassert my_solution.sumCounts(*[[2]]) == 1\nassert my_solution.sumCounts(*[[3, 4, 2, 5, 2, 4, 1, 2, 2, 5]]) == 535\nassert my_solution.sumCounts(*[[4, 4, 2, 4, 1]]) == 57\nassert my_solution.sumCounts(*[[2, 2, 5]]) == 12\nassert my_solution.sumCounts(*[[4, 5, 1, 2, 2, 1, 3, 3]]) == 266\nassert my_solution.sumCounts(*[[3, 1, 5, 5, 2, 3, 2, 2, 1]]) == 334\nassert my_solution.sumCounts(*[[2, 5, 2, 5, 3, 2, 5, 2]]) == 205\nassert my_solution.sumCounts(*[[5, 4, 1, 4, 5, 2, 4]]) == 203\nassert my_solution.sumCounts(*[[1, 3, 3, 4, 3, 1, 2, 1]]) == 253\nassert my_solution.sumCounts(*[[4]]) == 1\nassert my_solution.sumCounts(*[[1, 4, 2, 1, 5, 4, 3, 1, 4]]) == 507\nassert my_solution.sumCounts(*[[2, 4, 5, 3, 2, 5, 1, 5, 4, 4]]) == 626\nassert my_solution.sumCounts(*[[3, 4, 1, 4, 5, 2, 2]]) == 220\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3139",
"questionFrontendId": "2916",
"questionTitle": "Subarrays Distinct Element Sum of Squares II",
"stats": {
"totalAccepted": "1.7K",
"totalSubmission": "4.4K",
"totalAcceptedRaw": 1674,
"totalSubmissionRaw": 4398,
"acRate": "38.1%"
}
} |
LeetCode/3176 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumSum(*[[8, 6, 1, 5, 3]]) == 9\nassert my_solution.minimumSum(*[[5, 4, 8, 7, 10, 2]]) == 13\nassert my_solution.minimumSum(*[[6, 5, 4, 3, 4, 5]]) == -1\nassert my_solution.minimumSum(*[[50, 50, 50]]) == -1\nassert my_solution.minimumSum(*[[49, 50, 48]]) == 147\nassert my_solution.minimumSum(*[[48, 50, 49]]) == 147\nassert my_solution.minimumSum(*[[1, 1, 1]]) == -1\nassert my_solution.minimumSum(*[[1, 1, 2]]) == -1\nassert my_solution.minimumSum(*[[1, 1, 3]]) == -1\nassert my_solution.minimumSum(*[[1, 2, 1]]) == 4\nassert my_solution.minimumSum(*[[1, 2, 2]]) == -1\nassert my_solution.minimumSum(*[[1, 2, 3]]) == -1\nassert my_solution.minimumSum(*[[1, 3, 1]]) == 5\nassert my_solution.minimumSum(*[[1, 3, 2]]) == 6\nassert my_solution.minimumSum(*[[1, 3, 3]]) == -1\nassert my_solution.minimumSum(*[[2, 1, 1]]) == -1\nassert my_solution.minimumSum(*[[2, 1, 2]]) == -1\nassert my_solution.minimumSum(*[[2, 1, 3]]) == -1\nassert my_solution.minimumSum(*[[2, 2, 1]]) == -1\nassert my_solution.minimumSum(*[[2, 2, 2]]) == -1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3176",
"questionFrontendId": "2908",
"questionTitle": "Minimum Sum of Mountain Triplets I",
"stats": {
"totalAccepted": "7K",
"totalSubmission": "10.6K",
"totalAcceptedRaw": 7038,
"totalSubmissionRaw": 10637,
"acRate": "66.2%"
}
} |
LeetCode/3186 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumSum(*[[8, 6, 1, 5, 3]]) == 9\nassert my_solution.minimumSum(*[[5, 4, 8, 7, 10, 2]]) == 13\nassert my_solution.minimumSum(*[[6, 5, 4, 3, 4, 5]]) == -1\nassert my_solution.minimumSum(*[[50, 50, 50]]) == -1\nassert my_solution.minimumSum(*[[49, 50, 48]]) == 147\nassert my_solution.minimumSum(*[[48, 50, 49]]) == 147\nassert my_solution.minimumSum(*[[99999999, 100000000, 99999999]]) == 299999998\nassert my_solution.minimumSum(*[[1, 1, 1]]) == -1\nassert my_solution.minimumSum(*[[1, 1, 2]]) == -1\nassert my_solution.minimumSum(*[[1, 1, 3]]) == -1\nassert my_solution.minimumSum(*[[1, 2, 1]]) == 4\nassert my_solution.minimumSum(*[[1, 2, 2]]) == -1\nassert my_solution.minimumSum(*[[1, 2, 3]]) == -1\nassert my_solution.minimumSum(*[[1, 3, 1]]) == 5\nassert my_solution.minimumSum(*[[1, 3, 2]]) == 6\nassert my_solution.minimumSum(*[[1, 3, 3]]) == -1\nassert my_solution.minimumSum(*[[2, 1, 1]]) == -1\nassert my_solution.minimumSum(*[[2, 1, 2]]) == -1\nassert my_solution.minimumSum(*[[2, 1, 3]]) == -1\nassert my_solution.minimumSum(*[[2, 2, 1]]) == -1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3186",
"questionFrontendId": "2909",
"questionTitle": "Minimum Sum of Mountain Triplets II",
"stats": {
"totalAccepted": "6.4K",
"totalSubmission": "12.6K",
"totalAcceptedRaw": 6428,
"totalSubmissionRaw": 12581,
"acRate": "51.1%"
}
} |
LeetCode/3166 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minGroupsForValidAssignment(*[[3, 2, 3, 2, 3]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[10, 10, 10, 3, 1, 1]]) == 4\nassert my_solution.minGroupsForValidAssignment(*[[1, 1]]) == 1\nassert my_solution.minGroupsForValidAssignment(*[[1, 2]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[1, 3]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[2, 1]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[2, 2]]) == 1\nassert my_solution.minGroupsForValidAssignment(*[[3, 1]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[3, 2]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[3, 3]]) == 1\nassert my_solution.minGroupsForValidAssignment(*[[3, 4]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[10, 4]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[10, 10]]) == 1\nassert my_solution.minGroupsForValidAssignment(*[[14, 15]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[1, 1, 1]]) == 1\nassert my_solution.minGroupsForValidAssignment(*[[1, 2, 1]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[1, 7, 10]]) == 3\nassert my_solution.minGroupsForValidAssignment(*[[2, 1, 1]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[2, 1, 2]]) == 2\nassert my_solution.minGroupsForValidAssignment(*[[2, 2, 1]]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3166",
"questionFrontendId": "2910",
"questionTitle": "Minimum Number of Groups to Create a Valid Assignment",
"stats": {
"totalAccepted": "4.9K",
"totalSubmission": "16.5K",
"totalAcceptedRaw": 4860,
"totalSubmissionRaw": 16459,
"acRate": "29.5%"
}
} |
LeetCode/2879 | # 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:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumChanges(*['abcac', 2]) == 1\nassert my_solution.minimumChanges(*['abcdef', 2]) == 2\nassert my_solution.minimumChanges(*['aabbaa', 3]) == 0\nassert my_solution.minimumChanges(*['aq', 1]) == 1\nassert my_solution.minimumChanges(*['bb', 1]) == 0\nassert my_solution.minimumChanges(*['aac', 1]) == 1\nassert my_solution.minimumChanges(*['abcc', 1]) == 2\nassert my_solution.minimumChanges(*['acba', 2]) == 2\nassert my_solution.minimumChanges(*['edaswf', 1]) == 2\nassert my_solution.minimumChanges(*['aabcbaa', 1]) == 0\nassert my_solution.minimumChanges(*['dqpldq', 3]) == 3\nassert my_solution.minimumChanges(*['eksddulf', 1]) == 3\nassert my_solution.minimumChanges(*['aaaaacabbb', 1]) == 3\nassert my_solution.minimumChanges(*['aaabacacbb', 1]) == 3\nassert my_solution.minimumChanges(*['abbbbacaaa', 1]) == 3\nassert my_solution.minimumChanges(*['abcccbaccb', 1]) == 2\nassert my_solution.minimumChanges(*['baacbbbaba', 1]) == 2\nassert my_solution.minimumChanges(*['babcbaccba', 1]) == 1\nassert my_solution.minimumChanges(*['cabbcabcbc', 1]) == 3\nassert my_solution.minimumChanges(*['ccbccaaabb', 1]) == 4\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "2879",
"questionFrontendId": "2911",
"questionTitle": "Minimum Changes to Make K Semi-palindromes",
"stats": {
"totalAccepted": "2.2K",
"totalSubmission": "4.6K",
"totalAcceptedRaw": 2209,
"totalSubmissionRaw": 4633,
"acRate": "47.7%"
}
} |
LeetCode/3165 | # Find Indices With Index and Value Difference I
You are given a **0-indexed** integer array `nums` having length `n`, an integer `indexDifference`, and an integer `valueDifference`.
Your task is to find **two** indices `i` and `j`, both in the range `[0, n - 1]`, that satisfy the following conditions:
* `abs(i - j) >= indexDifference`, and
* `abs(nums[i] - nums[j]) >= valueDifference`
Return *an integer array* `answer`, *where* `answer = [i, j]` *if there are two such indices*, *and* `answer = [-1, -1]` *otherwise*. If there are multiple choices for the two indices, return *any of them*.
**Note:** `i` and `j` may be **equal**.
**Example 1:**
```
**Input:** nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
**Output:** [0,3]
**Explanation:** In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
```
**Example 2:**
```
**Input:** nums = [2,1], indexDifference = 0, valueDifference = 0
**Output:** [0,0]
**Explanation:** In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
```
**Example 3:**
```
**Input:** nums = [1,2,3], indexDifference = 2, valueDifference = 4
**Output:** [-1,-1]
**Explanation:** In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
```
**Constraints:**
* `1 <= n == nums.length <= 100`
* `0 <= nums[i] <= 50`
* `0 <= indexDifference <= 100`
* `0 <= valueDifference <= 50`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findIndices(*[[5, 1, 4, 1], 2, 4]) == [0, 3]\nassert my_solution.findIndices(*[[2, 1], 0, 0]) == [0, 0]\nassert my_solution.findIndices(*[[1, 2, 3], 2, 4]) == [-1, -1]\nassert my_solution.findIndices(*[[0], 0, 0]) == [0, 0]\nassert my_solution.findIndices(*[[3], 0, 0]) == [0, 0]\nassert my_solution.findIndices(*[[3], 1, 1]) == [-1, -1]\nassert my_solution.findIndices(*[[4], 1, 0]) == [-1, -1]\nassert my_solution.findIndices(*[[5], 1, 3]) == [-1, -1]\nassert my_solution.findIndices(*[[7], 1, 7]) == [-1, -1]\nassert my_solution.findIndices(*[[8], 0, 2]) == [-1, -1]\nassert my_solution.findIndices(*[[8], 1, 7]) == [-1, -1]\nassert my_solution.findIndices(*[[10], 0, 9]) == [-1, -1]\nassert my_solution.findIndices(*[[11], 1, 0]) == [-1, -1]\nassert my_solution.findIndices(*[[18], 1, 4]) == [-1, -1]\nassert my_solution.findIndices(*[[38], 1, 34]) == [-1, -1]\nassert my_solution.findIndices(*[[40], 1, 2]) == [-1, -1]\nassert my_solution.findIndices(*[[5, 10], 1, 2]) == [0, 1]\nassert my_solution.findIndices(*[[5, 48], 0, 29]) == [0, 1]\nassert my_solution.findIndices(*[[6, 3], 1, 2]) == [0, 1]\nassert my_solution.findIndices(*[[7, 6], 1, 0]) == [0, 1]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3165",
"questionFrontendId": "2903",
"questionTitle": "Find Indices With Index and Value Difference I",
"stats": {
"totalAccepted": "6.7K",
"totalSubmission": "9.5K",
"totalAcceptedRaw": 6654,
"totalSubmissionRaw": 9526,
"acRate": "69.9%"
}
} |
LeetCode/3150 | # Shortest and Lexicographically Smallest Beautiful String
You are given a binary string `s` and a positive integer `k`.
A substring of `s` is **beautiful** if the number of `1`'s in it is exactly `k`.
Let `len` be the length of the **shortest** beautiful substring.
Return *the lexicographically **smallest** beautiful substring of string* `s` *with length equal to* `len`. If `s` doesn't contain a beautiful substring, return *an **empty** string*.
A string `a` is lexicographically **larger** than a string `b` (of the same length) if in the first position where `a` and `b` differ, `a` has a character strictly larger than the corresponding character in `b`.
* For example, `"abcd"` is lexicographically larger than `"abcc"` because the first position they differ is at the fourth character, and `d` is greater than `c`.
**Example 1:**
```
**Input:** s = "100011001", k = 3
**Output:** "11001"
**Explanation:** There are 7 beautiful substrings in this example:
1. The substring "100011001".
2. The substring "100011001".
3. The substring "100011001".
4. The substring "100011001".
5. The substring "100011001".
6. The substring "100011001".
7. The substring "100011001".
The length of the shortest beautiful substring is 5.
The lexicographically smallest beautiful substring with length 5 is the substring "11001".
```
**Example 2:**
```
**Input:** s = "1011", k = 2
**Output:** "11"
**Explanation:** There are 3 beautiful substrings in this example:
1. The substring "1011".
2. The substring "1011".
3. The substring "1011".
The length of the shortest beautiful substring is 2.
The lexicographically smallest beautiful substring with length 2 is the substring "11".
```
**Example 3:**
```
**Input:** s = "000", k = 1
**Output:** ""
**Explanation:** There are no beautiful substrings in this example.
```
**Constraints:**
* `1 <= s.length <= 100`
* `1 <= k <= s.length`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def shortestBeautifulSubstring(self, s: str, k: int) -> str:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\nassert my_solution.shortestBeautifulSubstring(*['100011001', 3]) == 11001\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3150",
"questionFrontendId": "2904",
"questionTitle": "Shortest and Lexicographically Smallest Beautiful String",
"stats": {
"totalAccepted": "6K",
"totalSubmission": "15.1K",
"totalAcceptedRaw": 6013,
"totalSubmissionRaw": 15061,
"acRate": "39.9%"
}
} |
LeetCode/3170 | # Find Indices With Index and Value Difference II
You are given a **0-indexed** integer array `nums` having length `n`, an integer `indexDifference`, and an integer `valueDifference`.
Your task is to find **two** indices `i` and `j`, both in the range `[0, n - 1]`, that satisfy the following conditions:
* `abs(i - j) >= indexDifference`, and
* `abs(nums[i] - nums[j]) >= valueDifference`
Return *an integer array* `answer`, *where* `answer = [i, j]` *if there are two such indices*, *and* `answer = [-1, -1]` *otherwise*. If there are multiple choices for the two indices, return *any of them*.
**Note:** `i` and `j` may be **equal**.
**Example 1:**
```
**Input:** nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
**Output:** [0,3]
**Explanation:** In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
```
**Example 2:**
```
**Input:** nums = [2,1], indexDifference = 0, valueDifference = 0
**Output:** [0,0]
**Explanation:** In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
```
**Example 3:**
```
**Input:** nums = [1,2,3], indexDifference = 2, valueDifference = 4
**Output:** [-1,-1]
**Explanation:** In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
```
**Constraints:**
* `1 <= n == nums.length <= 105`
* `0 <= nums[i] <= 109`
* `0 <= indexDifference <= 105`
* `0 <= valueDifference <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.findIndices(*[[5, 1, 4, 1], 2, 4]) == [0, 3]\nassert my_solution.findIndices(*[[2, 1], 0, 0]) == [0, 0]\nassert my_solution.findIndices(*[[1, 2, 3], 2, 4]) == [-1, -1]\nassert my_solution.findIndices(*[[1], 0, 1]) == [-1, -1]\nassert my_solution.findIndices(*[[1], 1, 0]) == [-1, -1]\nassert my_solution.findIndices(*[[2], 0, 2]) == [-1, -1]\nassert my_solution.findIndices(*[[6], 1, 4]) == [-1, -1]\nassert my_solution.findIndices(*[[7], 0, 0]) == [0, 0]\nassert my_solution.findIndices(*[[8], 1, 6]) == [-1, -1]\nassert my_solution.findIndices(*[[9], 0, 1]) == [-1, -1]\nassert my_solution.findIndices(*[[9], 1, 9]) == [-1, -1]\nassert my_solution.findIndices(*[[12], 0, 5]) == [-1, -1]\nassert my_solution.findIndices(*[[16], 0, 16]) == [-1, -1]\nassert my_solution.findIndices(*[[16], 1, 6]) == [-1, -1]\nassert my_solution.findIndices(*[[46], 0, 36]) == [-1, -1]\nassert my_solution.findIndices(*[[0, 10], 2, 4]) == [-1, -1]\nassert my_solution.findIndices(*[[2, 7], 2, 7]) == [-1, -1]\nassert my_solution.findIndices(*[[5, 1], 2, 5]) == [-1, -1]\nassert my_solution.findIndices(*[[5, 12], 0, 10]) == [-1, -1]\nassert my_solution.findIndices(*[[8, 0], 1, 7]) == [0, 1]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3170",
"questionFrontendId": "2905",
"questionTitle": "Find Indices With Index and Value Difference II",
"stats": {
"totalAccepted": "4.9K",
"totalSubmission": "12.7K",
"totalAcceptedRaw": 4873,
"totalSubmissionRaw": 12724,
"acRate": "38.3%"
}
} |
LeetCode/3031 | # Construct Product Matrix
Given a **0-indexed** 2D integer matrix `grid` of size `n * m`, we define a **0-indexed** 2D matrix `p` of size `n * m` as the **product** matrix of `grid` if the following condition is met:
* Each element `p[i][j]` is calculated as the product of all elements in `grid` except for the element `grid[i][j]`. This product is then taken modulo `12345`.
Return *the product matrix of* `grid`.
**Example 1:**
```
**Input:** grid = [[1,2],[3,4]]
**Output:** [[24,12],[8,6]]
**Explanation:** p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24
p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12
p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8
p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6
So the answer is [[24,12],[8,6]].
```
**Example 2:**
```
**Input:** grid = [[12345],[2],[1]]
**Output:** [[2],[0],[0]]
**Explanation:** p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2.
p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0.
p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0.
So the answer is [[2],[0],[0]].
```
**Constraints:**
* `1 <= n == grid.length <= 105`
* `1 <= m == grid[i].length <= 105`
* `2 <= n * m <= 105`
* `1 <= grid[i][j] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.constructProductMatrix(*[[[1, 2], [3, 4]]]) == [[24, 12], [8, 6]]\nassert my_solution.constructProductMatrix(*[[[12345], [2], [1]]]) == [[2], [0], [0]]\nassert my_solution.constructProductMatrix(*[[[1], [2]]]) == [[2], [1]]\nassert my_solution.constructProductMatrix(*[[[1, 2]]]) == [[2, 1]]\nassert my_solution.constructProductMatrix(*[[[12345, 12345]]]) == [[0, 0]]\nassert my_solution.constructProductMatrix(*[[[1], [4]]]) == [[4], [1]]\nassert my_solution.constructProductMatrix(*[[[3], [4]]]) == [[4], [3]]\nassert my_solution.constructProductMatrix(*[[[4], [3]]]) == [[3], [4]]\nassert my_solution.constructProductMatrix(*[[[1, 1, 1]]]) == [[1, 1, 1]]\nassert my_solution.constructProductMatrix(*[[[2, 1, 1]]]) == [[1, 2, 2]]\nassert my_solution.constructProductMatrix(*[[[3], [5], [2]]]) == [[10], [6], [15]]\nassert my_solution.constructProductMatrix(*[[[1, 2], [1, 1], [6, 4]]]) == [[48, 24], [48, 48], [8, 12]]\nassert my_solution.constructProductMatrix(*[[[1, 2, 2], [1, 4, 3]]]) == [[48, 24, 24], [48, 12, 16]]\nassert my_solution.constructProductMatrix(*[[[2], [7], [2], [6]]]) == [[84], [24], [84], [28]]\nassert my_solution.constructProductMatrix(*[[[3], [4], [7], [7]]]) == [[196], [147], [84], [84]]\nassert my_solution.constructProductMatrix(*[[[3, 1, 1], [1, 3, 4]]]) == [[12, 36, 36], [36, 12, 9]]\nassert my_solution.constructProductMatrix(*[[[4], [8], [3], [7]]]) == [[168], [84], [224], [96]]\nassert my_solution.constructProductMatrix(*[[[5], [8], [8], [3]]]) == [[192], [120], [120], [320]]\nassert my_solution.constructProductMatrix(*[[[6], [5], [8], [5]]]) == [[200], [240], [150], [240]]\nassert my_solution.constructProductMatrix(*[[[8], [1], [3], [8]]]) == [[24], [192], [64], [24]]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3031",
"questionFrontendId": "2906",
"questionTitle": "Construct Product Matrix",
"stats": {
"totalAccepted": "3.7K",
"totalSubmission": "10.3K",
"totalAcceptedRaw": 3663,
"totalSubmissionRaw": 10292,
"acRate": "35.6%"
}
} |
LeetCode/3164 | # Last Visited Integers
Given a **0-indexed** array of strings `words` where `words[i]` is either a positive integer represented as a string or the string `"prev"`.
Start iterating from the beginning of the array; for every `"prev"` string seen in `words`, find the **last visited integer** in `words` which is defined as follows:
* Let `k` be the number of consecutive `"prev"` strings seen so far (containing the current string). Let `nums` be the **0-indexed** array of **integers** seen so far and `nums_reverse` be the reverse of `nums`, then the integer at `(k - 1)th` index of `nums_reverse` will be the **last visited integer** for this `"prev"`.
* If `k` is **greater** than the total visited integers, then the last visited integer will be `-1`.
Return *an integer array containing the last visited integers.*
**Example 1:**
```
**Input:** words = ["1","2","prev","prev","prev"]
**Output:** [2,1,-1]
**Explanation:**
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element.
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.
```
**Example 2:**
```
**Input:** words = ["1","prev","2","prev","prev"]
**Output:** [1,2,1]
**Explanation:**
For "prev" at index = 1, last visited integer will be 1.
For "prev" at index = 3, last visited integer will be 2.
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
```
**Constraints:**
* `1 <= words.length <= 100`
* `words[i] == "prev"` or `1 <= int(words[i]) <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.lastVisitedIntegers(*[['1', '2', 'prev', 'prev', 'prev']]) == [2, 1, -1]\nassert my_solution.lastVisitedIntegers(*[['1', 'prev', '2', 'prev', 'prev']]) == [1, 2, 1]\nassert my_solution.lastVisitedIntegers(*[['prev', 'prev', 'prev', '27']]) == [-1, -1, -1]\nassert my_solution.lastVisitedIntegers(*[['17', '42']]) == []\nassert my_solution.lastVisitedIntegers(*[['prev']]) == [-1]\nassert my_solution.lastVisitedIntegers(*[['prev', 'prev', 'prev', '52', 'prev']]) == [-1, -1, -1, 52]\nassert my_solution.lastVisitedIntegers(*[['prev', 'prev', '68', 'prev', 'prev', '53', '40', '23', 'prev']]) == [-1, -1, 68, -1, 23]\nassert my_solution.lastVisitedIntegers(*[['99', '23', 'prev']]) == [23]\nassert my_solution.lastVisitedIntegers(*[['prev', 'prev', 'prev', '58', '99', 'prev', '10', 'prev']]) == [-1, -1, -1, 99, 10]\nassert my_solution.lastVisitedIntegers(*[['prev', '51', 'prev', 'prev']]) == [-1, 51, -1]\nassert my_solution.lastVisitedIntegers(*[['prev', '46', '9', 'prev']]) == [-1, 9]\nassert my_solution.lastVisitedIntegers(*[['prev', 'prev', 'prev', 'prev', 'prev', '26']]) == [-1, -1, -1, -1, -1]\nassert my_solution.lastVisitedIntegers(*[['prev', '21', 'prev', '76', '82', 'prev', '96', 'prev', '57', 'prev']]) == [-1, 21, 82, 96, 57]\nassert my_solution.lastVisitedIntegers(*[['52', '4', 'prev', 'prev', 'prev', '69']]) == [4, 52, -1]\nassert my_solution.lastVisitedIntegers(*[['24', 'prev']]) == [24]\nassert my_solution.lastVisitedIntegers(*[['46', 'prev', '78', 'prev', '83', '21', 'prev', '94', '50']]) == [46, 78, 21]\nassert my_solution.lastVisitedIntegers(*[['14', '66', 'prev', 'prev', '46', 'prev']]) == [66, 14, 46]\nassert my_solution.lastVisitedIntegers(*[['35', '90']]) == []\nassert my_solution.lastVisitedIntegers(*[['prev', '9', 'prev', '8', 'prev']]) == [-1, 9, 8]\nassert my_solution.lastVisitedIntegers(*[['prev', 'prev', '88', '71', '47', '65', '24', '39']]) == [-1, -1]\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3164",
"questionFrontendId": "2899",
"questionTitle": "Last Visited Integers",
"stats": {
"totalAccepted": "3.7K",
"totalSubmission": "5.2K",
"totalAcceptedRaw": 3698,
"totalSubmissionRaw": 5170,
"acRate": "71.5%"
}
} |
LeetCode/3143 | # Longest Unequal Adjacent Groups Subsequence I
You are given an integer `n`, a **0-indexed** string array `words`, and a **0-indexed** **binary** array `groups`, both arrays having length `n`.
You need to select the **longest** **subsequence** from an array of indices `[0, 1, ..., n - 1]`, such that for the subsequence denoted as `[i0, i1, ..., ik - 1]` having length `k`, `groups[ij] != groups[ij + 1]`, for each `j` where `0 < j + 1 < k`.
Return *a string array containing the words corresponding to the indices **(in order)** in the selected subsequence*. If there are multiple answers, return *any of them*.
A **subsequence** of an array is a new 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.
**Note:** strings in `words` may be **unequal** in length.
**Example 1:**
```
**Input:** n = 3, words = ["e","a","b"], groups = [0,0,1]
**Output:** ["e","b"]
**Explanation:** A subsequence that can be selected is [0,2] because groups[0] != groups[2].
So, a valid answer is [words[0],words[2]] = ["e","b"].
Another subsequence that can be selected is [1,2] because groups[1] != groups[2].
This results in [words[1],words[2]] = ["a","b"].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 2.
```
**Example 2:**
```
**Input:** n = 4, words = ["a","b","c","d"], groups = [1,0,1,1]
**Output:** ["a","b","c"]
**Explanation:** A subsequence that can be selected is [0,1,2] because groups[0] != groups[1] and groups[1] != groups[2].
So, a valid answer is [words[0],words[1],words[2]] = ["a","b","c"].
Another subsequence that can be selected is [0,1,3] because groups[0] != groups[1] and groups[1] != groups[3].
This results in [words[0],words[1],words[3]] = ["a","b","d"].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
```
**Constraints:**
* `1 <= n == words.length == groups.length <= 100`
* `1 <= words[i].length <= 10`
* `0 <= groups[i] < 2`
* `words` consists of **distinct** strings.
* `words[i]` consists of lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.getWordsInLongestSubsequence(*[3, ['e', 'a', 'b'], [0, 0, 1]]) == ['e', 'b']\nassert my_solution.getWordsInLongestSubsequence(*[4, ['a', 'b', 'c', 'd'], [1, 0, 1, 1]]) == ['a', 'b', 'c']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['c'], [0]]) == ['c']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['d'], [1]]) == ['d']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['e'], [0]]) == ['e']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['fe'], [0]]) == ['fe']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['frl'], [1]]) == ['frl']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['ha'], [1]]) == ['ha']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['l'], [0]]) == ['l']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['n'], [1]]) == ['n']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['s'], [1]]) == ['s']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['d', 'g'], [0, 1]]) == ['d', 'g']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['lr', 'h'], [0, 0]]) == ['lr']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['wx', 'h'], [0, 1]]) == ['wx', 'h']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['yw', 'n'], [0, 1]]) == ['yw', 'n']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['z', 'n'], [0, 0]]) == ['z']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['zr', 'a'], [0, 0]]) == ['zr']\nassert my_solution.getWordsInLongestSubsequence(*[3, ['h', 'vv', 'kp'], [0, 1, 0]]) == ['h', 'vv', 'kp']\nassert my_solution.getWordsInLongestSubsequence(*[3, ['m', 'v', 'y'], [0, 1, 0]]) == ['m', 'v', 'y']\nassert my_solution.getWordsInLongestSubsequence(*[3, ['o', 'cfy', 'en'], [1, 0, 0]]) == ['o', 'cfy']\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3143",
"questionFrontendId": "2900",
"questionTitle": "Longest Unequal Adjacent Groups Subsequence I",
"stats": {
"totalAccepted": "3.3K",
"totalSubmission": "4.4K",
"totalAcceptedRaw": 3298,
"totalSubmissionRaw": 4365,
"acRate": "75.6%"
}
} |
LeetCode/3142 | # Longest Unequal Adjacent Groups Subsequence II
You are given an integer `n`, a **0-indexed** string array `words`, and a **0-indexed** array `groups`, both arrays having length `n`.
The **hamming distance** between two strings of equal length is the number of positions at which the corresponding characters are **different**.
You need to select the **longest** **subsequence** from an array of indices `[0, 1, ..., n - 1]`, such that for the subsequence denoted as `[i0, i1, ..., ik - 1]` having length `k`, the following holds:
* For **adjacent** indices in the subsequence, their corresponding groups are **unequal**, i.e., `groups[ij] != groups[ij + 1]`, for each `j` where `0 < j + 1 < k`.
* `words[ij]` and `words[ij + 1]` are **equal** in length, and the **hamming distance** between them is `1`, where `0 < j + 1 < k`, for all indices in the subsequence.
Return *a string array containing the words corresponding to the indices **(in order)** in the selected subsequence*. If there are multiple answers, return *any of them*.
A **subsequence** of an array is a new 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.
**Note:** strings in `words` may be **unequal** in length.
**Example 1:**
```
**Input:** n = 3, words = ["bab","dab","cab"], groups = [1,2,2]
**Output:** ["bab","cab"]
**Explanation:** A subsequence that can be selected is [0,2].
- groups[0] != groups[2]
- words[0].length == words[2].length, and the hamming distance between them is 1.
So, a valid answer is [words[0],words[2]] = ["bab","cab"].
Another subsequence that can be selected is [0,1].
- groups[0] != groups[1]
- words[0].length == words[1].length, and the hamming distance between them is 1.
So, another valid answer is [words[0],words[1]] = ["bab","dab"].
It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2.
```
**Example 2:**
```
**Input:** n = 4, words = ["a","b","c","d"], groups = [1,2,3,4]
**Output:** ["a","b","c","d"]
**Explanation:** We can select the subsequence [0,1,2,3].
It satisfies both conditions.
Hence, the answer is [words[0],words[1],words[2],words[3]] = ["a","b","c","d"].
It has the longest length among all subsequences of indices that satisfy the conditions.
Hence, it is the only answer.
```
**Constraints:**
* `1 <= n == words.length == groups.length <= 1000`
* `1 <= words[i].length <= 10`
* `1 <= groups[i] <= n`
* `words` consists of **distinct** strings.
* `words[i]` consists of lowercase English letters.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.getWordsInLongestSubsequence(*[3, ['bab', 'dab', 'cab'], [1, 2, 2]]) == ['bab', 'cab']\nassert my_solution.getWordsInLongestSubsequence(*[4, ['a', 'b', 'c', 'd'], [1, 2, 3, 4]]) == ['a', 'b', 'c', 'd']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['abbbb'], [1]]) == ['abbbb']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['ad'], [1]]) == ['ad']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['baaccb'], [1]]) == ['baaccb']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['bc'], [1]]) == ['bc']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['bdb'], [1]]) == ['bdb']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['cc'], [1]]) == ['cc']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['cd'], [1]]) == ['cd']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['cdb'], [1]]) == ['cdb']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['cea'], [1]]) == ['cea']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['cebbbb'], [1]]) == ['cebbbb']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['da'], [1]]) == ['da']\nassert my_solution.getWordsInLongestSubsequence(*[1, ['daab'], [1]]) == ['daab']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['adbe', 'acace'], [2, 2]]) == ['acace']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['ba', 'dc'], [1, 1]]) == ['dc']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['baa', 'ada'], [1, 2]]) == ['ada']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['bebea', 'ddecc'], [1, 1]]) == ['ddecc']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['cedbca', 'db'], [1, 1]]) == ['db']\nassert my_solution.getWordsInLongestSubsequence(*[2, ['dbcdd', 'baba'], [2, 1]]) == ['baba']\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3142",
"questionFrontendId": "2901",
"questionTitle": "Longest Unequal Adjacent Groups Subsequence II",
"stats": {
"totalAccepted": "2.7K",
"totalSubmission": "7K",
"totalAcceptedRaw": 2678,
"totalSubmissionRaw": 7014,
"acRate": "38.2%"
}
} |
LeetCode/3091 | # Count of Sub-Multisets With Bounded Sum
You are given a **0-indexed** array `nums` of non-negative integers, and two integers `l` and `r`.
Return *the **count of sub-multisets** within* `nums` *where the sum of elements in each subset falls within the inclusive range of* `[l, r]`.
Since the answer may be large, return it modulo `109 + 7`.
A **sub-multiset** is an **unordered** collection of elements of the array in which a given value `x` can occur `0, 1, ..., occ[x]` times, where `occ[x]` is the number of occurrences of `x` in the array.
**Note** that:
* Two **sub-multisets** are the same if sorting both sub-multisets results in identical multisets.
* The sum of an **empty** multiset is `0`.
**Example 1:**
```
**Input:** nums = [1,2,2,3], l = 6, r = 6
**Output:** 1
**Explanation:** The only subset of nums that has a sum of 6 is {1, 2, 3}.
```
**Example 2:**
```
**Input:** nums = [2,1,4,2,7], l = 1, r = 5
**Output:** 7
**Explanation:** The subsets of nums that have a sum within the range [1, 5] are {1}, {2}, {4}, {2, 2}, {1, 2}, {1, 4}, and {1, 2, 2}.
```
**Example 3:**
```
**Input:** nums = [1,2,1,3,5,2], l = 3, r = 5
**Output:** 9
**Explanation:** The subsets of nums that have a sum within the range [3, 5] are {3}, {5}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {1, 1, 2}, {1, 1, 3}, and {1, 2, 2}.
```
**Constraints:**
* `1 <= nums.length <= 2 * 104`
* `0 <= nums[i] <= 2 * 104`
* Sum of `nums` does not exceed `2 * 104`.
* `0 <= l <= r <= 2 * 104`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def countSubMultisets(self, nums: List[int], l: int, r: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countSubMultisets(*[[1, 2, 2, 3], 6, 6]) == 1\nassert my_solution.countSubMultisets(*[[2, 1, 4, 2, 7], 1, 5]) == 7\nassert my_solution.countSubMultisets(*[[1, 2, 1, 3, 5, 2], 3, 5]) == 9\nassert my_solution.countSubMultisets(*[[0, 0, 1, 2, 3], 2, 3]) == 9\nassert my_solution.countSubMultisets(*[[0, 0, 0, 0, 0], 0, 0]) == 6\nassert my_solution.countSubMultisets(*[[0, 0, 0, 1, 2, 5, 2, 3], 0, 3]) == 20\nassert my_solution.countSubMultisets(*[[1, 1], 2, 2]) == 1\nassert my_solution.countSubMultisets(*[[1, 1, 1], 2, 2]) == 1\nassert my_solution.countSubMultisets(*[[1, 1, 2], 2, 4]) == 4\nassert my_solution.countSubMultisets(*[[1, 2, 1], 2, 2]) == 2\nassert my_solution.countSubMultisets(*[[1, 2, 2], 3, 5]) == 3\nassert my_solution.countSubMultisets(*[[2, 1, 1], 1, 2]) == 3\nassert my_solution.countSubMultisets(*[[2, 1, 2], 2, 2]) == 1\nassert my_solution.countSubMultisets(*[[2, 2, 1], 4, 5]) == 2\nassert my_solution.countSubMultisets(*[[2, 2, 2], 3, 6]) == 2\nassert my_solution.countSubMultisets(*[[1, 1, 1, 1], 3, 3]) == 1\nassert my_solution.countSubMultisets(*[[1, 1, 1, 2], 1, 1]) == 1\nassert my_solution.countSubMultisets(*[[1, 1, 1, 3], 4, 4]) == 1\nassert my_solution.countSubMultisets(*[[1, 1, 2, 1], 3, 4]) == 3\nassert my_solution.countSubMultisets(*[[1, 1, 2, 2], 2, 3]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3091",
"questionFrontendId": "2902",
"questionTitle": "Count of Sub-Multisets With Bounded Sum",
"stats": {
"totalAccepted": "1.7K",
"totalSubmission": "5.6K",
"totalAcceptedRaw": 1733,
"totalSubmissionRaw": 5640,
"acRate": "30.7%"
}
} |
LeetCode/3172 | # Divisible and Non-divisible Sums Difference
You are given positive integers `n` and `m`.
Define two integers, `num1` and `num2`, as follows:
* `num1`: The sum of all integers in the range `[1, n]` that are **not divisible** by `m`.
* `num2`: The sum of all integers in the range `[1, n]` that are **divisible** by `m`.
Return *the integer* `num1 - num2`.
**Example 1:**
```
**Input:** n = 10, m = 3
**Output:** 19
**Explanation:** In the given example:
- Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
We return 37 - 18 = 19 as the answer.
```
**Example 2:**
```
**Input:** n = 5, m = 6
**Output:** 15
**Explanation:** In the given example:
- Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
We return 15 - 0 = 15 as the answer.
```
**Example 3:**
```
**Input:** n = 5, m = 1
**Output:** -15
**Explanation:** In the given example:
- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
- Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
We return 0 - 15 = -15 as the answer.
```
**Constraints:**
* `1 <= n, m <= 1000`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.differenceOfSums(*[10, 3]) == 19\nassert my_solution.differenceOfSums(*[5, 6]) == 15\nassert my_solution.differenceOfSums(*[5, 1]) == -15\nassert my_solution.differenceOfSums(*[15, 9]) == 102\nassert my_solution.differenceOfSums(*[8, 10]) == 36\nassert my_solution.differenceOfSums(*[23, 36]) == 276\nassert my_solution.differenceOfSums(*[1, 32]) == 1\nassert my_solution.differenceOfSums(*[36, 7]) == 456\nassert my_solution.differenceOfSums(*[3, 8]) == 6\nassert my_solution.differenceOfSums(*[4, 2]) == -2\nassert my_solution.differenceOfSums(*[9, 7]) == 31\nassert my_solution.differenceOfSums(*[20, 9]) == 156\nassert my_solution.differenceOfSums(*[3, 19]) == 6\nassert my_solution.differenceOfSums(*[6, 16]) == 21\nassert my_solution.differenceOfSums(*[6, 1]) == -21\nassert my_solution.differenceOfSums(*[5, 25]) == 15\nassert my_solution.differenceOfSums(*[9, 3]) == 9\nassert my_solution.differenceOfSums(*[8, 23]) == 36\nassert my_solution.differenceOfSums(*[17, 1]) == -153\nassert my_solution.differenceOfSums(*[18, 9]) == 117\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3172",
"questionFrontendId": "2894",
"questionTitle": "Divisible and Non-divisible Sums Difference",
"stats": {
"totalAccepted": "7.3K",
"totalSubmission": "8.4K",
"totalAcceptedRaw": 7318,
"totalSubmissionRaw": 8400,
"acRate": "87.1%"
}
} |
LeetCode/3151 | # Minimum Processing Time
You have `n` processors each having `4` cores and `n * 4` tasks that need to be executed such that each core should perform only **one** task.
Given a **0-indexed** integer array `processorTime` representing the time at which each processor becomes available for the first time and a **0-indexed** integer array `tasks` representing the time it takes to execute each task, return *the **minimum** time when all of the tasks have been executed by the processors.*
**Note:** Each core executes the task independently of the others.
**Example 1:**
```
**Input:** processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
**Output:** 16
**Explanation:**
It's optimal to assign the tasks at indexes 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indexes 0, 1, 2, 3 to the second processor which becomes available at time = 10.
Time taken by the first processor to finish execution of all tasks = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
Time taken by the second processor to finish execution of all tasks = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
Hence, it can be shown that the minimum time taken to execute all the tasks is 16.
```
**Example 2:**
```
**Input:** processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
**Output:** 23
**Explanation:**
It's optimal to assign the tasks at indexes 1, 4, 5, 6 to the first processor which becomes available at time = 10, and the tasks at indexes 0, 2, 3, 7 to the second processor which becomes available at time = 20.
Time taken by the first processor to finish execution of all tasks = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18.
Time taken by the second processor to finish execution of all tasks = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23.
Hence, it can be shown that the minimum time taken to execute all the tasks is 23.
```
**Constraints:**
* `1 <= n == processorTime.length <= 25000`
* `1 <= tasks.length <= 105`
* `0 <= processorTime[i] <= 109`
* `1 <= tasks[i] <= 109`
* `tasks.length == 4 * n`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minProcessingTime(self, processorTime: List[int], tasks: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minProcessingTime(*[[8, 10], [2, 2, 3, 1, 8, 7, 4, 5]]) == 16\nassert my_solution.minProcessingTime(*[[10, 20], [2, 3, 1, 2, 5, 8, 4, 3]]) == 23\nassert my_solution.minProcessingTime(*[[121, 99], [287, 315, 293, 260, 333, 362, 69, 233]]) == 461\nassert my_solution.minProcessingTime(*[[33, 320], [132, 68, 232, 166, 30, 300, 112, 138]]) == 452\nassert my_solution.minProcessingTime(*[[50, 82], [288, 138, 205, 295, 367, 100, 258, 308]]) == 417\nassert my_solution.minProcessingTime(*[[291], [125, 169, 269, 32]]) == 560\nassert my_solution.minProcessingTime(*[[55, 350, 166, 210, 389], [276, 253, 157, 237, 92, 396, 331, 19, 82, 301, 136, 396, 251, 92, 280, 70, 253, 47, 81, 84]]) == 470\nassert my_solution.minProcessingTime(*[[143, 228, 349, 231, 392], [102, 365, 363, 211, 38, 96, 98, 79, 365, 289, 252, 201, 259, 346, 21, 68, 128, 56, 167, 183]]) == 517\nassert my_solution.minProcessingTime(*[[168, 32, 299, 303, 96], [382, 183, 337, 73, 115, 350, 6, 18, 93, 238, 102, 302, 96, 381, 327, 385, 387, 288, 138, 83]]) == 456\nassert my_solution.minProcessingTime(*[[324, 117, 374, 219, 303], [374, 202, 328, 11, 353, 208, 383, 287, 107, 236, 226, 387, 21, 183, 352, 164, 207, 182, 15, 65]]) == 571\nassert my_solution.minProcessingTime(*[[376], [21, 247, 274, 38]]) == 650\nassert my_solution.minProcessingTime(*[[93, 3, 281, 218], [182, 16, 241, 312, 81, 339, 207, 330, 306, 166, 82, 290, 7, 317, 396, 389]]) == 459\nassert my_solution.minProcessingTime(*[[374, 250, 197, 170], [247, 56, 330, 361, 240, 261, 67, 65, 138, 181, 308, 26, 59, 150, 137, 244]]) == 531\nassert my_solution.minProcessingTime(*[[115, 271, 137], [34, 72, 328, 312, 159, 32, 283, 6, 234, 280, 46, 349]]) == 464\nassert my_solution.minProcessingTime(*[[47, 217, 349, 233, 283], [195, 188, 181, 259, 145, 96, 298, 322, 213, 154, 278, 292, 315, 191, 177, 228, 291, 204, 310, 266]]) == 526\nassert my_solution.minProcessingTime(*[[177, 6, 326, 318, 294], [136, 215, 260, 259, 35, 248, 340, 377, 144, 248, 83, 150, 63, 48, 269, 197, 317, 135, 36, 344]]) == 542\nassert my_solution.minProcessingTime(*[[266, 372], [260, 325, 159, 316, 296, 366, 335, 146]]) == 668\nassert my_solution.minProcessingTime(*[[63, 339], [79, 316, 98, 354, 220, 267, 333, 11]]) == 559\nassert my_solution.minProcessingTime(*[[149, 60, 172, 5, 212], [230, 374, 276, 281, 55, 96, 52, 83, 56, 399, 69, 333, 145, 6, 50, 101, 216, 327, 120, 209]]) == 404\nassert my_solution.minProcessingTime(*[[220, 375, 285, 267, 150], [53, 317, 367, 258, 337, 280, 232, 322, 153, 169, 121, 211, 171, 345, 76, 370, 265, 107, 45, 320]]) == 542\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3151",
"questionFrontendId": "2895",
"questionTitle": "Minimum Processing Time",
"stats": {
"totalAccepted": "5.3K",
"totalSubmission": "6.8K",
"totalAcceptedRaw": 5263,
"totalSubmissionRaw": 6822,
"acRate": "77.1%"
}
} |
LeetCode/3033 | # Apply Operations to Make Two Strings Equal
You are given two **0-indexed** binary strings `s1` and `s2`, both of length `n`, and a positive integer `x`.
You can perform any of the following operations on the string `s1` **any** number of times:
* Choose two indices `i` and `j`, and flip both `s1[i]` and `s1[j]`. The cost of this operation is `x`.
* Choose an index `i` such that `i < n - 1` and flip both `s1[i]` and `s1[i + 1]`. The cost of this operation is `1`.
Return *the **minimum** cost needed to make the strings* `s1` *and* `s2` *equal, or return* `-1` *if it is impossible.*
**Note** that flipping a character means changing it from `0` to `1` or vice-versa.
**Example 1:**
```
**Input:** s1 = "1100011000", s2 = "0101001010", x = 2
**Output:** 4
**Explanation:** We can do the following operations:
- Choose i = 3 and apply the second operation. The resulting string is s1 = "110**11**11000".
- Choose i = 4 and apply the second operation. The resulting string is s1 = "1101**00**1000".
- Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "**0**1010010**1**0" = s2.
The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
```
**Example 2:**
```
**Input:** s1 = "10110", s2 = "00011", x = 4
**Output:** -1
**Explanation:** It is not possible to make the two strings equal.
```
**Constraints:**
* `n == s1.length == s2.length`
* `1 <= n, x <= 500`
* `s1` and `s2` consist only of the characters `'0'` and `'1'`.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minOperations(self, s1: str, s2: str, x: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minOperations(*['1100011000', '0101001010', 2]) == 4\nassert my_solution.minOperations(*['10110', '00011', 4]) == -1\nassert my_solution.minOperations(*['101101', '000000', 6]) == 4\nassert my_solution.minOperations(*['0', '1', 2]) == -1\nassert my_solution.minOperations(*['1011100100111000', '1001010001011100', 19]) == -1\nassert my_solution.minOperations(*['00101101100010', '00001010001111', 30]) == 8\nassert my_solution.minOperations(*['1011000', '0001101', 30]) == 4\nassert my_solution.minOperations(*['1111110101010110', '1000100111100101', 19]) == -1\nassert my_solution.minOperations(*['1011100000100100101', '1110001001110000011', 14]) == -1\nassert my_solution.minOperations(*['0', '1', 17]) == -1\nassert my_solution.minOperations(*['0', '1', 3]) == -1\nassert my_solution.minOperations(*['0001110010', '0110100111', 29]) == 5\nassert my_solution.minOperations(*['01111101010100110100', '10010011011001011000', 21]) == 7\nassert my_solution.minOperations(*['00000101', '01001010', 10]) == -1\nassert my_solution.minOperations(*['01', '00', 30]) == -1\nassert my_solution.minOperations(*['11111', '01011', 4]) == 2\nassert my_solution.minOperations(*['001010101011001', '110111000101110', 14]) == 7\nassert my_solution.minOperations(*['010011101', '101111000', 17]) == 4\nassert my_solution.minOperations(*['11110111', '10011111', 16]) == -1\nassert my_solution.minOperations(*['0011', '1100', 14]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3033",
"questionFrontendId": "2896",
"questionTitle": "Apply Operations to Make Two Strings Equal",
"stats": {
"totalAccepted": "3.8K",
"totalSubmission": "12.1K",
"totalAcceptedRaw": 3800,
"totalSubmissionRaw": 12132,
"acRate": "31.3%"
}
} |
LeetCode/3153 | # Apply Operations on Array to Maximize Sum of Squares
You are given a **0-indexed** integer array `nums` and a **positive** integer `k`.
You can do the following operation on the array **any** number of times:
* Choose any two distinct indices `i` and `j` and **simultaneously** update the values of `nums[i]` to `(nums[i] AND nums[j])` and `nums[j]` to `(nums[i] OR nums[j])`. Here, `OR` denotes the bitwise `OR` operation, and `AND` denotes the bitwise `AND` operation.
You have to choose `k` elements from the final array and calculate the sum of their **squares**.
Return *the **maximum** sum of squares you can achieve*.
Since the answer can be very large, return it **modulo** `109 + 7`.
**Example 1:**
```
**Input:** nums = [2,6,5,8], k = 2
**Output:** 261
**Explanation:** We can do the following operations on the array:
- Choose i = 0 and j = 3, then change nums[0] to (2 AND 8) = 0 and nums[3] to (2 OR 8) = 10. The resulting array is nums = [0,6,5,10].
- Choose i = 2 and j = 3, then change nums[2] to (5 AND 10) = 0 and nums[3] to (5 OR 10) = 15. The resulting array is nums = [0,6,0,15].
We can choose the elements 15 and 6 from the final array. The sum of squares is 152 + 62 = 261.
It can be shown that this is the maximum value we can get.
```
**Example 2:**
```
**Input:** nums = [4,5,4,7], k = 3
**Output:** 90
**Explanation:** We do not need to apply any operations.
We can choose the elements 7, 5, and 4 with a sum of squares: 72 + 52 + 42 = 90.
It can be shown that this is the maximum value we can get.
```
**Constraints:**
* `1 <= k <= nums.length <= 105`
* `1 <= nums[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maxSum(self, nums: List[int], k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxSum(*[[2, 6, 5, 8], 2]) == 261\nassert my_solution.maxSum(*[[4, 5, 4, 7], 3]) == 90\nassert my_solution.maxSum(*[[32, 85, 61], 1]) == 15625\nassert my_solution.maxSum(*[[123], 1]) == 15129\nassert my_solution.maxSum(*[[96, 66, 60, 58, 32, 17, 63, 21, 30, 44, 15, 8, 98, 93], 2]) == 32258\nassert my_solution.maxSum(*[[30, 8, 63, 69, 52, 94, 41, 28, 94, 86, 28, 13, 68, 38, 53, 11, 21, 33], 2]) == 32258\nassert my_solution.maxSum(*[[2, 38, 15, 2, 73, 100, 47, 14, 25, 58, 40, 64, 23, 9, 53, 38, 91, 75, 9, 2], 3]) == 48387\nassert my_solution.maxSum(*[[25, 52, 75, 65], 4]) == 24051\nassert my_solution.maxSum(*[[96, 36, 72, 61, 13, 25, 5, 33, 9, 51, 9, 78, 40], 13]) == 53776\nassert my_solution.maxSum(*[[38, 21, 15, 84, 65, 35, 57, 82, 94, 26, 27, 89, 73, 22, 25, 6, 97, 17], 4]) == 64516\nassert my_solution.maxSum(*[[18, 72, 52, 56, 7, 21, 55, 68, 98, 31, 35, 49, 100, 49, 64, 20], 4]) == 62548\nassert my_solution.maxSum(*[[2, 73, 75], 3]) == 11250\nassert my_solution.maxSum(*[[73, 37, 41, 84], 2]) == 27506\nassert my_solution.maxSum(*[[62, 83, 11, 3, 53], 3]) == 20459\nassert my_solution.maxSum(*[[53, 59, 71, 38, 5, 15, 98, 86, 9, 8, 35, 54, 65, 77, 3, 68, 11, 5, 41, 18], 9]) == 95273\nassert my_solution.maxSum(*[[53, 67, 91, 79, 21, 27, 63, 34, 60, 94, 51], 4]) == 64516\nassert my_solution.maxSum(*[[41, 15, 6, 31, 40, 97, 11, 45, 81, 91, 91, 62], 3]) == 48387\nassert my_solution.maxSum(*[[10, 9], 2]) == 185\nassert my_solution.maxSum(*[[9, 6, 8, 32, 92, 12, 47, 45, 62, 96, 5, 66, 82, 90, 34, 39, 49, 86, 16], 13]) == 102770\nassert my_solution.maxSum(*[[1, 19, 29, 30, 68, 13, 80, 16, 71, 32, 8, 76, 41, 24, 16, 2, 30], 14]) == 53470\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3153",
"questionFrontendId": "2897",
"questionTitle": "Apply Operations on Array to Maximize Sum of Squares",
"stats": {
"totalAccepted": "2.3K",
"totalSubmission": "4.1K",
"totalAcceptedRaw": 2345,
"totalSubmissionRaw": 4091,
"acRate": "57.3%"
}
} |
LeetCode/3154 | # Maximum Value of an Ordered Triplet I
You are given a **0-indexed** integer array `nums`.
Return ***the maximum value over all triplets of indices*** `(i, j, k)` *such that* `i < j < k`. If all such triplets have a negative value, return `0`.
The **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`.
**Example 1:**
```
**Input:** nums = [12,6,1,2,7]
**Output:** 77
**Explanation:** The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
```
**Example 2:**
```
**Input:** nums = [1,10,3,4,19]
**Output:** 133
**Explanation:** The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
```
**Example 3:**
```
**Input:** nums = [1,2,3]
**Output:** 0
**Explanation:** The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
```
**Constraints:**
* `3 <= nums.length <= 100`
* `1 <= nums[i] <= 106`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumTripletValue(*[[12, 6, 1, 2, 7]]) == 77\nassert my_solution.maximumTripletValue(*[[1, 10, 3, 4, 19]]) == 133\nassert my_solution.maximumTripletValue(*[[1, 2, 3]]) == 0\nassert my_solution.maximumTripletValue(*[[2, 3, 1]]) == 0\nassert my_solution.maximumTripletValue(*[[5, 7, 8, 4]]) == 0\nassert my_solution.maximumTripletValue(*[[1000000, 1, 1000000]]) == 999999000000\nassert my_solution.maximumTripletValue(*[[18, 15, 8, 13, 10, 9, 17, 10, 2, 16, 17]]) == 272\nassert my_solution.maximumTripletValue(*[[8, 6, 3, 13, 2, 12, 19, 5, 19, 6, 10, 11, 9]]) == 266\nassert my_solution.maximumTripletValue(*[[6, 11, 12, 12, 7, 9, 2, 11, 12, 4, 19, 14, 16, 8, 16]]) == 190\nassert my_solution.maximumTripletValue(*[[15, 14, 17, 13, 18, 17, 10, 19, 2, 20, 12, 9]]) == 340\nassert my_solution.maximumTripletValue(*[[6, 14, 20, 19, 19, 10, 3, 15, 12, 13, 8, 1, 2, 15, 3]]) == 285\nassert my_solution.maximumTripletValue(*[[2, 7, 19, 4, 8, 20]]) == 300\nassert my_solution.maximumTripletValue(*[[10, 13, 6, 2]]) == 14\nassert my_solution.maximumTripletValue(*[[1, 19, 1, 3, 18, 10, 16, 9, 3, 17, 8, 9]]) == 324\nassert my_solution.maximumTripletValue(*[[16, 2, 10, 20, 16, 2, 13, 8, 19]]) == 342\nassert my_solution.maximumTripletValue(*[[19, 11, 12, 4, 17, 1, 7, 20, 13, 10, 14, 20, 11, 19, 3]]) == 360\nassert my_solution.maximumTripletValue(*[[16, 15, 12, 5, 4, 12, 15, 17, 5, 18, 6, 16, 1, 17, 4]]) == 289\nassert my_solution.maximumTripletValue(*[[8, 10, 17, 11, 2, 8, 13]]) == 195\nassert my_solution.maximumTripletValue(*[[13, 4, 3, 19, 16, 14, 17, 6, 20, 6, 16, 4]]) == 260\nassert my_solution.maximumTripletValue(*[[1, 8, 9, 18, 4, 10, 3, 13, 9]]) == 195\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3154",
"questionFrontendId": "2873",
"questionTitle": "Maximum Value of an Ordered Triplet I",
"stats": {
"totalAccepted": "5.3K",
"totalSubmission": "9.8K",
"totalAcceptedRaw": 5340,
"totalSubmissionRaw": 9825,
"acRate": "54.4%"
}
} |
LeetCode/3152 | # Maximum Value of an Ordered Triplet II
You are given a **0-indexed** integer array `nums`.
Return ***the maximum value over all triplets of indices*** `(i, j, k)` *such that* `i < j < k`*.* If all such triplets have a negative value, return `0`.
The **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`.
**Example 1:**
```
**Input:** nums = [12,6,1,2,7]
**Output:** 77
**Explanation:** The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
```
**Example 2:**
```
**Input:** nums = [1,10,3,4,19]
**Output:** 133
**Explanation:** The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
```
**Example 3:**
```
**Input:** nums = [1,2,3]
**Output:** 0
**Explanation:** The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
```
**Constraints:**
* `3 <= nums.length <= 105`
* `1 <= nums[i] <= 106`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumTripletValue(*[[12, 6, 1, 2, 7]]) == 77\nassert my_solution.maximumTripletValue(*[[1, 10, 3, 4, 19]]) == 133\nassert my_solution.maximumTripletValue(*[[1, 2, 3]]) == 0\nassert my_solution.maximumTripletValue(*[[2, 3, 1]]) == 0\nassert my_solution.maximumTripletValue(*[[5, 7, 8, 4]]) == 0\nassert my_solution.maximumTripletValue(*[[1000000, 1, 1000000]]) == 999999000000\nassert my_solution.maximumTripletValue(*[[18, 15, 8, 13, 10, 9, 17, 10, 2, 16, 17]]) == 272\nassert my_solution.maximumTripletValue(*[[8, 6, 3, 13, 2, 12, 19, 5, 19, 6, 10, 11, 9]]) == 266\nassert my_solution.maximumTripletValue(*[[6, 11, 12, 12, 7, 9, 2, 11, 12, 4, 19, 14, 16, 8, 16]]) == 190\nassert my_solution.maximumTripletValue(*[[15, 14, 17, 13, 18, 17, 10, 19, 2, 20, 12, 9]]) == 340\nassert my_solution.maximumTripletValue(*[[6, 14, 20, 19, 19, 10, 3, 15, 12, 13, 8, 1, 2, 15, 3]]) == 285\nassert my_solution.maximumTripletValue(*[[2, 7, 19, 4, 8, 20]]) == 300\nassert my_solution.maximumTripletValue(*[[10, 13, 6, 2]]) == 14\nassert my_solution.maximumTripletValue(*[[1, 19, 1, 3, 18, 10, 16, 9, 3, 17, 8, 9]]) == 324\nassert my_solution.maximumTripletValue(*[[16, 2, 10, 20, 16, 2, 13, 8, 19]]) == 342\nassert my_solution.maximumTripletValue(*[[19, 11, 12, 4, 17, 1, 7, 20, 13, 10, 14, 20, 11, 19, 3]]) == 360\nassert my_solution.maximumTripletValue(*[[16, 15, 12, 5, 4, 12, 15, 17, 5, 18, 6, 16, 1, 17, 4]]) == 289\nassert my_solution.maximumTripletValue(*[[8, 10, 17, 11, 2, 8, 13]]) == 195\nassert my_solution.maximumTripletValue(*[[13, 4, 3, 19, 16, 14, 17, 6, 20, 6, 16, 4]]) == 260\nassert my_solution.maximumTripletValue(*[[1, 8, 9, 18, 4, 10, 3, 13, 9]]) == 195\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3152",
"questionFrontendId": "2874",
"questionTitle": "Maximum Value of an Ordered Triplet II",
"stats": {
"totalAccepted": "5.1K",
"totalSubmission": "10.6K",
"totalAcceptedRaw": 5052,
"totalSubmissionRaw": 10617,
"acRate": "47.6%"
}
} |
LeetCode/3141 | # Minimum Size Subarray in Infinite Array
You are given a **0-indexed** array `nums` and an integer `target`.
A **0-indexed** array `infinite_nums` is generated by infinitely appending the elements of `nums` to itself.
Return *the length of the **shortest** subarray of the array* `infinite_nums` *with a sum equal to* `target`*.* If there is no such subarray return `-1`.
**Example 1:**
```
**Input:** nums = [1,2,3], target = 5
**Output:** 2
**Explanation:** In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
```
**Example 2:**
```
**Input:** nums = [1,1,1,2,3], target = 4
**Output:** 2
**Explanation:** In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
```
**Example 3:**
```
**Input:** nums = [2,4,6,8], target = 3
**Output:** -1
**Explanation:** In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
It can be proven that there is no subarray with sum equal to target = 3.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 105`
* `1 <= target <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minSizeSubarray(self, nums: List[int], target: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minSizeSubarray(*[[1, 2, 3], 5]) == 2\nassert my_solution.minSizeSubarray(*[[1, 1, 1, 2, 3], 4]) == 2\nassert my_solution.minSizeSubarray(*[[2, 4, 6, 8], 3]) == -1\nassert my_solution.minSizeSubarray(*[[2, 1, 5, 7, 7, 1, 6, 3], 39]) == 9\nassert my_solution.minSizeSubarray(*[[17, 4, 3, 14, 17, 6, 15], 85]) == -1\nassert my_solution.minSizeSubarray(*[[18, 3, 11, 19, 7, 16, 6, 7, 3, 6, 18, 9, 9, 1, 14, 17, 15, 14, 12, 10], 7]) == 1\nassert my_solution.minSizeSubarray(*[[2, 3, 5, 2, 3, 4, 4, 1, 3, 5, 2, 2, 5, 1, 1, 2, 5], 19]) == 6\nassert my_solution.minSizeSubarray(*[[4, 1, 5, 7, 1, 6, 1, 7, 2, 2, 5, 5, 5, 6, 3], 20]) == 5\nassert my_solution.minSizeSubarray(*[[7, 3, 5], 36]) == -1\nassert my_solution.minSizeSubarray(*[[1, 11, 6, 4, 13], 22]) == 4\nassert my_solution.minSizeSubarray(*[[1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1], 83]) == 53\nassert my_solution.minSizeSubarray(*[[4, 3, 5, 4, 5, 4, 4, 4, 5, 7, 4, 5, 6, 3, 1, 4, 6, 3, 7], 15]) == 3\nassert my_solution.minSizeSubarray(*[[1, 2, 3, 2, 1, 5, 3, 4, 5], 53]) == 19\nassert my_solution.minSizeSubarray(*[[2, 5, 6, 4], 95]) == 22\nassert my_solution.minSizeSubarray(*[[6, 6, 4, 5, 2, 8, 1, 8, 7, 6, 6, 7, 4, 1, 9, 6, 8, 8], 55]) == 9\nassert my_solution.minSizeSubarray(*[[1, 2, 8, 19, 17, 2, 3, 11, 8, 12, 16, 18, 7], 36]) == 2\nassert my_solution.minSizeSubarray(*[[12, 14, 4, 14, 13, 16, 5], 36]) == -1\nassert my_solution.minSizeSubarray(*[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 37]) == 37\nassert my_solution.minSizeSubarray(*[[5, 7, 2, 6, 4, 1, 6, 7, 1, 4, 7, 6, 7, 7, 6, 6, 4, 6, 8], 90]) == 17\nassert my_solution.minSizeSubarray(*[[3, 5, 15, 17, 6, 17, 10, 15, 10, 4, 6], 25]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3141",
"questionFrontendId": "2875",
"questionTitle": "Minimum Size Subarray in Infinite Array",
"stats": {
"totalAccepted": "4.2K",
"totalSubmission": "12.1K",
"totalAcceptedRaw": 4156,
"totalSubmissionRaw": 12073,
"acRate": "34.4%"
}
} |
LeetCode/3044 | # Minimum Operations to Collect Elements
You are given an array `nums` of positive integers and an integer `k`.
In one operation, you can remove the last element of the array and add it to your collection.
Return *the **minimum number of operations** needed to collect elements* `1, 2, ..., k`.
**Example 1:**
```
**Input:** nums = [3,1,5,4,2], k = 2
**Output:** 4
**Explanation:** After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4.
```
**Example 2:**
```
**Input:** nums = [3,1,5,4,2], k = 5
**Output:** 5
**Explanation:** After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5.
```
**Example 3:**
```
**Input:** nums = [3,2,5,3,1], k = 3
**Output:** 4
**Explanation:** After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4.
```
**Constraints:**
* `1 <= nums.length <= 50`
* `1 <= nums[i] <= nums.length`
* `1 <= k <= nums.length`
* The input is generated such that you can collect elements `1, 2, ..., k`.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minOperations(*[[3, 1, 5, 4, 2], 2]) == 4\nassert my_solution.minOperations(*[[3, 1, 5, 4, 2], 5]) == 5\nassert my_solution.minOperations(*[[3, 2, 5, 3, 1], 3]) == 4\nassert my_solution.minOperations(*[[1], 1]) == 1\nassert my_solution.minOperations(*[[1, 1], 1]) == 1\nassert my_solution.minOperations(*[[1, 2], 1]) == 2\nassert my_solution.minOperations(*[[1, 2], 2]) == 2\nassert my_solution.minOperations(*[[2, 1], 1]) == 1\nassert my_solution.minOperations(*[[2, 1], 2]) == 2\nassert my_solution.minOperations(*[[1, 1, 1], 1]) == 1\nassert my_solution.minOperations(*[[1, 1, 2], 1]) == 2\nassert my_solution.minOperations(*[[1, 1, 2], 2]) == 2\nassert my_solution.minOperations(*[[1, 1, 3], 1]) == 2\nassert my_solution.minOperations(*[[1, 2, 1], 1]) == 1\nassert my_solution.minOperations(*[[1, 2, 1], 2]) == 2\nassert my_solution.minOperations(*[[1, 2, 2], 1]) == 3\nassert my_solution.minOperations(*[[1, 2, 2], 2]) == 3\nassert my_solution.minOperations(*[[1, 2, 3], 1]) == 3\nassert my_solution.minOperations(*[[1, 2, 3], 2]) == 3\nassert my_solution.minOperations(*[[1, 2, 3], 3]) == 3\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3044",
"questionFrontendId": "2869",
"questionTitle": "Minimum Operations to Collect Elements",
"stats": {
"totalAccepted": "4K",
"totalSubmission": "5.6K",
"totalAcceptedRaw": 3999,
"totalSubmissionRaw": 5606,
"acRate": "71.3%"
}
} |
LeetCode/3094 | # Minimum Number of Operations to Make Array Empty
You are given a **0-indexed** array `nums` consisting of positive integers.
There are two types of operations that you can apply on the array **any** number of times:
* Choose **two** elements with **equal** values and **delete** them from the array.
* Choose **three** elements with **equal** values and **delete** them from the array.
Return *the **minimum** number of operations required to make the array empty, or* `-1` *if it is not possible*.
**Example 1:**
```
**Input:** nums = [2,3,3,2,2,4,2,3,4]
**Output:** 4
**Explanation:** We can apply the following operations to make the array empty:
- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
It can be shown that we cannot make the array empty in less than 4 operations.
```
**Example 2:**
```
**Input:** nums = [2,1,2,2,3,3]
**Output:** -1
**Explanation:** It is impossible to empty the array.
```
**Constraints:**
* `2 <= nums.length <= 105`
* `1 <= nums[i] <= 106`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minOperations(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minOperations(*[[2, 3, 3, 2, 2, 4, 2, 3, 4]]) == 4\nassert my_solution.minOperations(*[[2, 1, 2, 2, 3, 3]]) == -1\nassert my_solution.minOperations(*[[3, 3]]) == 1\nassert my_solution.minOperations(*[[14, 12, 14, 14, 12, 14, 14, 12, 12, 12, 12, 14, 14, 12, 14, 14, 14, 12, 12]]) == 7\nassert my_solution.minOperations(*[[2, 2, 2, 2, 2, 2, 2, 2, 2]]) == 3\nassert my_solution.minOperations(*[[15, 3, 3, 15, 15, 13, 8, 15, 6, 15, 3, 1, 8, 8, 15]]) == -1\nassert my_solution.minOperations(*[[19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19]]) == 5\nassert my_solution.minOperations(*[[13, 7, 13, 7, 13, 7, 13, 13, 7]]) == 4\nassert my_solution.minOperations(*[[5, 5]]) == 1\nassert my_solution.minOperations(*[[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]) == 5\nassert my_solution.minOperations(*[[3, 14, 3, 14, 3, 14, 14, 3, 3, 14, 14, 14, 3, 14, 14, 3, 14, 14, 14, 3]]) == 7\nassert my_solution.minOperations(*[[16, 16, 16, 19, 16, 3, 16, 8, 16, 16, 16, 19, 3, 16, 16]]) == -1\nassert my_solution.minOperations(*[[11, 11, 11, 11, 19, 11, 11, 11, 11, 11, 19, 11, 11, 11, 11, 11, 19]]) == 6\nassert my_solution.minOperations(*[[1, 1, 1, 5, 1, 5, 1, 1, 1, 1, 1, 1, 1]]) == 5\nassert my_solution.minOperations(*[[16, 16, 16, 3, 16, 16, 3]]) == 3\nassert my_solution.minOperations(*[[14, 4, 4, 19, 19]]) == -1\nassert my_solution.minOperations(*[[1, 14, 1, 1, 1]]) == -1\nassert my_solution.minOperations(*[[3, 10, 11, 3, 3, 11, 3, 3, 3, 3, 3, 3, 3, 3, 10, 3, 3, 3]]) == 7\nassert my_solution.minOperations(*[[3, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8, 8, 8, 8]]) == 5\nassert my_solution.minOperations(*[[9, 9, 9]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3094",
"questionFrontendId": "2870",
"questionTitle": "Minimum Number of Operations to Make Array Empty",
"stats": {
"totalAccepted": "3.9K",
"totalSubmission": "6.1K",
"totalAcceptedRaw": 3912,
"totalSubmissionRaw": 6067,
"acRate": "64.5%"
}
} |
LeetCode/3080 | # Split Array Into Maximum Number of Subarrays
You are given an array `nums` consisting of **non-negative** integers.
We define the score of subarray `nums[l..r]` such that `l <= r` as `nums[l] AND nums[l + 1] AND ... AND nums[r]` where **AND** is the bitwise `AND` operation.
Consider splitting the array into one or more subarrays such that the following conditions are satisfied:
* **E****ach** element of the array belongs to **exactly** one subarray.
* The sum of scores of the subarrays is the **minimum** possible.
Return *the **maximum** number of subarrays in a split that satisfies the conditions above.*
A **subarray** is a contiguous part of an array.
**Example 1:**
```
**Input:** nums = [1,0,2,0,1,2]
**Output:** 3
**Explanation:** We can split the array into the following subarrays:
- [1,0]. The score of this subarray is 1 AND 0 = 0.
- [2,0]. The score of this subarray is 2 AND 0 = 0.
- [1,2]. The score of this subarray is 1 AND 2 = 0.
The sum of scores is 0 + 0 + 0 = 0, which is the minimum possible score that we can obtain.
It can be shown that we cannot split the array into more than 3 subarrays with a total score of 0. So we return 3.
```
**Example 2:**
```
**Input:** nums = [5,7,1,3]
**Output:** 1
**Explanation:** We can split the array into one subarray: [5,7,1,3] with a score of 1, which is the minimum possible score that we can obtain.
It can be shown that we cannot split the array into more than 1 subarray with a total score of 1. So we return 1.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `0 <= nums[i] <= 106`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maxSubarrays(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxSubarrays(*[[1, 0, 2, 0, 1, 2]]) == 3\nassert my_solution.maxSubarrays(*[[5, 7, 1, 3]]) == 1\nassert my_solution.maxSubarrays(*[[1, 0, 2, 1]]) == 2\nassert my_solution.maxSubarrays(*[[0]]) == 1\nassert my_solution.maxSubarrays(*[[0, 0]]) == 2\nassert my_solution.maxSubarrays(*[[100000]]) == 1\nassert my_solution.maxSubarrays(*[[1, 2, 2, 1]]) == 2\nassert my_solution.maxSubarrays(*[[30, 18, 19, 20, 11, 21, 12, 22, 26]]) == 2\nassert my_solution.maxSubarrays(*[[0, 8, 0, 0, 0, 23]]) == 4\nassert my_solution.maxSubarrays(*[[8, 10, 23, 26, 21, 28, 21, 14, 21, 14, 9, 16, 24, 29, 7, 26]]) == 4\nassert my_solution.maxSubarrays(*[[18, 12, 16, 28, 7, 15, 24, 7, 8, 26, 22, 6, 23, 7, 17, 1, 16]]) == 6\nassert my_solution.maxSubarrays(*[[22]]) == 1\nassert my_solution.maxSubarrays(*[[15, 24, 20, 28, 11, 16, 0, 0, 0, 22, 7, 18]]) == 5\nassert my_solution.maxSubarrays(*[[0, 0, 27]]) == 2\nassert my_solution.maxSubarrays(*[[18, 7, 20, 10, 0, 14, 0, 28, 7, 0, 0, 9, 12, 0]]) == 6\nassert my_solution.maxSubarrays(*[[0, 29, 16, 0, 6, 17]]) == 3\nassert my_solution.maxSubarrays(*[[4, 7, 13, 0, 23, 6, 4]]) == 1\nassert my_solution.maxSubarrays(*[[4, 27]]) == 1\nassert my_solution.maxSubarrays(*[[29, 5, 0, 25, 0, 15, 19, 24, 20, 0, 23]]) == 4\nassert my_solution.maxSubarrays(*[[24, 6]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3080",
"questionFrontendId": "2871",
"questionTitle": "Split Array Into Maximum Number of Subarrays",
"stats": {
"totalAccepted": "2.8K",
"totalSubmission": "5.7K",
"totalAcceptedRaw": 2829,
"totalSubmissionRaw": 5701,
"acRate": "49.6%"
}
} |
LeetCode/3055 | # Maximum Odd Binary Number
You are given a **binary** string `s` that contains at least one `'1'`.
You have to **rearrange** the bits in such a way that the resulting binary number is the **maximum odd binary number** that can be created from this combination.
Return *a string representing the maximum odd binary number that can be created from the given combination.*
**Note** that the resulting string **can** have leading zeros.
**Example 1:**
```
**Input:** s = "010"
**Output:** "001"
**Explanation:** Because there is just one '1', it must be in the last position. So the answer is "001".
```
**Example 2:**
```
**Input:** s = "0101"
**Output:** "1001"
**Explanation:** One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
```
**Constraints:**
* `1 <= s.length <= 100`
* `s` consists only of `'0'` and `'1'`.
* `s` contains at least one `'1'`.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumOddBinaryNumber(self, s: str) -> str:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumOddBinaryNumber(*['010']) == 001\nassert my_solution.maximumOddBinaryNumber(*['0101']) == 1001\nassert my_solution.maximumOddBinaryNumber(*['1']) == 1\nassert my_solution.maximumOddBinaryNumber(*['01']) == 01\nassert my_solution.maximumOddBinaryNumber(*['10']) == 01\nassert my_solution.maximumOddBinaryNumber(*['11']) == 11\nassert my_solution.maximumOddBinaryNumber(*['001']) == 001\nassert my_solution.maximumOddBinaryNumber(*['011']) == 101\nassert my_solution.maximumOddBinaryNumber(*['100']) == 001\nassert my_solution.maximumOddBinaryNumber(*['101']) == 101\nassert my_solution.maximumOddBinaryNumber(*['110']) == 101\nassert my_solution.maximumOddBinaryNumber(*['111']) == 111\nassert my_solution.maximumOddBinaryNumber(*['0010']) == 0001\nassert my_solution.maximumOddBinaryNumber(*['0011']) == 1001\nassert my_solution.maximumOddBinaryNumber(*['0100']) == 0001\nassert my_solution.maximumOddBinaryNumber(*['0111']) == 1101\nassert my_solution.maximumOddBinaryNumber(*['1000']) == 0001\nassert my_solution.maximumOddBinaryNumber(*['1001']) == 1001\nassert my_solution.maximumOddBinaryNumber(*['1011']) == 1101\nassert my_solution.maximumOddBinaryNumber(*['1100']) == 1001\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3055",
"questionFrontendId": "2864",
"questionTitle": "Maximum Odd Binary Number",
"stats": {
"totalAccepted": "7.5K",
"totalSubmission": "9.3K",
"totalAcceptedRaw": 7522,
"totalSubmissionRaw": 9297,
"acRate": "80.9%"
}
} |
LeetCode/3114 | # Beautiful Towers I
You are given a **0-indexed** array `maxHeights` of `n` integers.
You are tasked with building `n` towers in the coordinate line. The `ith` tower is built at coordinate `i` and has a height of `heights[i]`.
A configuration of towers is **beautiful** if the following conditions hold:
1. `1 <= heights[i] <= maxHeights[i]`
2. `heights` is a **mountain** array.
Array `heights` is a **mountain** if there exists an index `i` such that:
* For all `0 < j <= i`, `heights[j - 1] <= heights[j]`
* For all `i <= k < n - 1`, `heights[k + 1] <= heights[k]`
Return *the **maximum possible sum of heights** of a beautiful configuration of towers*.
**Example 1:**
```
**Input:** maxHeights = [5,3,4,1,1]
**Output:** 13
**Explanation:** One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 0.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.
```
**Example 2:**
```
**Input:** maxHeights = [6,5,3,9,2,7]
**Output:** 22
**Explanation:** One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 3.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.
```
**Example 3:**
```
**Input:** maxHeights = [3,2,5,5,2,3]
**Output:** 18
**Explanation:** One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 2.
Note that, for this configuration, i = 3 can also be considered a peak.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
```
**Constraints:**
* `1 <= n == maxHeights <= 103`
* `1 <= maxHeights[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumSumOfHeights(*[[5, 3, 4, 1, 1]]) == 13\nassert my_solution.maximumSumOfHeights(*[[6, 5, 3, 9, 2, 7]]) == 22\nassert my_solution.maximumSumOfHeights(*[[3, 2, 5, 5, 2, 3]]) == 18\nassert my_solution.maximumSumOfHeights(*[[1000000000]]) == 1000000000\nassert my_solution.maximumSumOfHeights(*[[1]]) == 1\nassert my_solution.maximumSumOfHeights(*[[933754743]]) == 933754743\nassert my_solution.maximumSumOfHeights(*[[1, 1000000000]]) == 1000000001\nassert my_solution.maximumSumOfHeights(*[[1000000000, 1000000000]]) == 2000000000\nassert my_solution.maximumSumOfHeights(*[[999999999, 1000000000]]) == 1999999999\nassert my_solution.maximumSumOfHeights(*[[1000000000, 999999999]]) == 1999999999\nassert my_solution.maximumSumOfHeights(*[[30, 1]]) == 31\nassert my_solution.maximumSumOfHeights(*[[1, 12, 19]]) == 32\nassert my_solution.maximumSumOfHeights(*[[1000000000, 1000000000, 1000000000]]) == 3000000000\nassert my_solution.maximumSumOfHeights(*[[999999999, 1000000000, 999999999]]) == 2999999998\nassert my_solution.maximumSumOfHeights(*[[1000000000, 999999999, 999999998]]) == 2999999997\nassert my_solution.maximumSumOfHeights(*[[999999998, 999999999, 1000000000]]) == 2999999997\nassert my_solution.maximumSumOfHeights(*[[1, 1, 1]]) == 3\nassert my_solution.maximumSumOfHeights(*[[1, 1, 4, 3, 3, 3, 6]]) == 20\nassert my_solution.maximumSumOfHeights(*[[2, 4, 1, 3, 5]]) == 11\nassert my_solution.maximumSumOfHeights(*[[1, 5, 2, 5, 6, 4, 6, 3, 4, 5]]) == 33\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3114",
"questionFrontendId": "2865",
"questionTitle": "Beautiful Towers I",
"stats": {
"totalAccepted": "6K",
"totalSubmission": "12.2K",
"totalAcceptedRaw": 6000,
"totalSubmissionRaw": 12159,
"acRate": "49.3%"
}
} |
LeetCode/3113 | # Beautiful Towers II
You are given a **0-indexed** array `maxHeights` of `n` integers.
You are tasked with building `n` towers in the coordinate line. The `ith` tower is built at coordinate `i` and has a height of `heights[i]`.
A configuration of towers is **beautiful** if the following conditions hold:
1. `1 <= heights[i] <= maxHeights[i]`
2. `heights` is a **mountain** array.
Array `heights` is a **mountain** if there exists an index `i` such that:
* For all `0 < j <= i`, `heights[j - 1] <= heights[j]`
* For all `i <= k < n - 1`, `heights[k + 1] <= heights[k]`
Return *the **maximum possible sum of heights** of a beautiful configuration of towers*.
**Example 1:**
```
**Input:** maxHeights = [5,3,4,1,1]
**Output:** 13
**Explanation:** One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 0.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.
```
**Example 2:**
```
**Input:** maxHeights = [6,5,3,9,2,7]
**Output:** 22
**Explanation:** One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 3.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.
```
**Example 3:**
```
**Input:** maxHeights = [3,2,5,5,2,3]
**Output:** 18
**Explanation:** One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 2.
Note that, for this configuration, i = 3 can also be considered a peak.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
```
**Constraints:**
* `1 <= n == maxHeights <= 105`
* `1 <= maxHeights[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumSumOfHeights(*[[5, 3, 4, 1, 1]]) == 13\nassert my_solution.maximumSumOfHeights(*[[6, 5, 3, 9, 2, 7]]) == 22\nassert my_solution.maximumSumOfHeights(*[[3, 2, 5, 5, 2, 3]]) == 18\nassert my_solution.maximumSumOfHeights(*[[1000000000]]) == 1000000000\nassert my_solution.maximumSumOfHeights(*[[1]]) == 1\nassert my_solution.maximumSumOfHeights(*[[352939501]]) == 352939501\nassert my_solution.maximumSumOfHeights(*[[1, 1000000000]]) == 1000000001\nassert my_solution.maximumSumOfHeights(*[[1000000000, 1000000000]]) == 2000000000\nassert my_solution.maximumSumOfHeights(*[[999999999, 1000000000]]) == 1999999999\nassert my_solution.maximumSumOfHeights(*[[1000000000, 999999999]]) == 1999999999\nassert my_solution.maximumSumOfHeights(*[[2, 1]]) == 3\nassert my_solution.maximumSumOfHeights(*[[26, 30, 30]]) == 86\nassert my_solution.maximumSumOfHeights(*[[1000000000, 1000000000, 1000000000]]) == 3000000000\nassert my_solution.maximumSumOfHeights(*[[999999999, 1000000000, 999999999]]) == 2999999998\nassert my_solution.maximumSumOfHeights(*[[1000000000, 999999999, 999999998]]) == 2999999997\nassert my_solution.maximumSumOfHeights(*[[999999998, 999999999, 1000000000]]) == 2999999997\nassert my_solution.maximumSumOfHeights(*[[1, 1, 1]]) == 3\nassert my_solution.maximumSumOfHeights(*[[1, 1, 5, 6, 2, 2, 3]]) == 19\nassert my_solution.maximumSumOfHeights(*[[3, 5, 3, 5, 1, 5, 4, 4, 4]]) == 22\nassert my_solution.maximumSumOfHeights(*[[4, 2, 4, 2, 1, 5, 4, 6]]) == 19\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3113",
"questionFrontendId": "2866",
"questionTitle": "Beautiful Towers II",
"stats": {
"totalAccepted": "16.1K",
"totalSubmission": "35K",
"totalAcceptedRaw": 16063,
"totalSubmissionRaw": 34990,
"acRate": "45.9%"
}
} |
LeetCode/3093 | # Sum of Values at Indices With K Set Bits
You are given a **0-indexed** integer array `nums` and an integer `k`.
Return *an integer that denotes the **sum** of elements in* `nums` *whose corresponding **indices** have **exactly*** `k` *set bits in their binary representation.*
The **set bits** in an integer are the `1`'s present when it is written in binary.
* For example, the binary representation of `21` is `10101`, which has `3` set bits.
**Example 1:**
```
**Input:** nums = [5,10,1,5,2], k = 1
**Output:** 13
**Explanation:** The binary representation of the indices are:
0 = 0002
1 = 0012
2 = 0102
3 = 0112
4 = 1002Indices 1, 2, and 4 have k = 1 set bits in their binary representation.
Hence, the answer is nums[1] + nums[2] + nums[4] = 13.
```
**Example 2:**
```
**Input:** nums = [4,3,2,1], k = 2
**Output:** 1
**Explanation:** The binary representation of the indices are:
0 = 002
1 = 012
2 = 102
3 = 112Only index 3 has k = 2 set bits in its binary representation.
Hence, the answer is nums[3] = 1.
```
**Constraints:**
* `1 <= nums.length <= 1000`
* `1 <= nums[i] <= 105`
* `0 <= k <= 10`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.sumIndicesWithKSetBits(*[[5, 10, 1, 5, 2], 1]) == 13\nassert my_solution.sumIndicesWithKSetBits(*[[4, 3, 2, 1], 2]) == 1\nassert my_solution.sumIndicesWithKSetBits(*[[1], 0]) == 1\nassert my_solution.sumIndicesWithKSetBits(*[[100000], 0]) == 100000\nassert my_solution.sumIndicesWithKSetBits(*[[2, 2], 1]) == 2\nassert my_solution.sumIndicesWithKSetBits(*[[2, 4], 1]) == 4\nassert my_solution.sumIndicesWithKSetBits(*[[2, 7], 1]) == 7\nassert my_solution.sumIndicesWithKSetBits(*[[3, 3], 1]) == 3\nassert my_solution.sumIndicesWithKSetBits(*[[3, 9], 1]) == 9\nassert my_solution.sumIndicesWithKSetBits(*[[4, 7], 1]) == 7\nassert my_solution.sumIndicesWithKSetBits(*[[4, 8], 1]) == 8\nassert my_solution.sumIndicesWithKSetBits(*[[6, 6], 1]) == 6\nassert my_solution.sumIndicesWithKSetBits(*[[7, 2], 1]) == 2\nassert my_solution.sumIndicesWithKSetBits(*[[7, 4], 1]) == 4\nassert my_solution.sumIndicesWithKSetBits(*[[8, 4], 1]) == 4\nassert my_solution.sumIndicesWithKSetBits(*[[8, 9], 1]) == 9\nassert my_solution.sumIndicesWithKSetBits(*[[9, 9], 1]) == 9\nassert my_solution.sumIndicesWithKSetBits(*[[15, 43], 1]) == 43\nassert my_solution.sumIndicesWithKSetBits(*[[35, 86], 1]) == 86\nassert my_solution.sumIndicesWithKSetBits(*[[36, 14], 1]) == 14\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3093",
"questionFrontendId": "2859",
"questionTitle": "Sum of Values at Indices With K Set Bits",
"stats": {
"totalAccepted": "7.9K",
"totalSubmission": "9.5K",
"totalAcceptedRaw": 7937,
"totalSubmissionRaw": 9524,
"acRate": "83.3%"
}
} |
LeetCode/3104 | # Happy Students
You are given a **0-indexed** integer array `nums` of length `n` where `n` is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy.
The `ith` student will become happy if one of these two conditions is met:
* The student is selected and the total number of selected students is **strictly greater than** `nums[i]`.
* The student is not selected and the total number of selected students is **strictly** **less than** `nums[i]`.
Return *the number of ways to select a group of students so that everyone remains happy.*
**Example 1:**
```
**Input:** nums = [1,1]
**Output:** 2
**Explanation:**
The two possible ways are:
The class teacher selects no student.
The class teacher selects both students to form the group.
If the class teacher selects just one student to form a group then the both students will not be happy. Therefore, there are only two possible ways.
```
**Example 2:**
```
**Input:** nums = [6,0,3,3,6,7,2,7]
**Output:** 3
**Explanation:**
The three possible ways are:
The class teacher selects the student with index = 1 to form the group.
The class teacher selects the students with index = 1, 2, 3, 6 to form the group.
The class teacher selects all the students to form the group.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `0 <= nums[i] < nums.length`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def countWays(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countWays(*[[1, 1]]) == 2\nassert my_solution.countWays(*[[6, 0, 3, 3, 6, 7, 2, 7]]) == 3\nassert my_solution.countWays(*[[1, 1, 0, 1]]) == 1\nassert my_solution.countWays(*[[5, 0, 3, 4, 2, 1, 2, 4]]) == 1\nassert my_solution.countWays(*[[0, 4, 4, 4, 4, 4, 2]]) == 2\nassert my_solution.countWays(*[[0, 1, 5, 6, 8, 7, 4, 7, 2]]) == 2\nassert my_solution.countWays(*[[0]]) == 1\nassert my_solution.countWays(*[[2, 2, 7, 1, 2, 2, 4, 7]]) == 3\nassert my_solution.countWays(*[[0, 2, 2, 2, 3, 3, 3, 3]]) == 2\nassert my_solution.countWays(*[[7, 7, 7, 7, 7, 7, 7, 7, 2]]) == 2\nassert my_solution.countWays(*[[4, 2, 3, 6, 6, 0, 6, 8, 3]]) == 3\nassert my_solution.countWays(*[[0, 0, 1, 7, 2, 0, 6, 5]]) == 1\nassert my_solution.countWays(*[[6, 6, 6, 6, 6, 6, 6, 7, 1, 7]]) == 2\nassert my_solution.countWays(*[[2, 2, 4, 5, 0, 1, 4, 4, 7]]) == 1\nassert my_solution.countWays(*[[4, 4, 4, 4, 4]]) == 2\nassert my_solution.countWays(*[[0, 4, 0, 3, 4]]) == 2\nassert my_solution.countWays(*[[6, 5, 5, 8, 4, 2, 6, 4, 8]]) == 3\nassert my_solution.countWays(*[[0, 9, 4, 6, 8, 8, 1, 7, 4, 7]]) == 2\nassert my_solution.countWays(*[[1, 0, 0]]) == 1\nassert my_solution.countWays(*[[1, 1, 2, 2, 2, 3, 3, 3, 3]]) == 2\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3104",
"questionFrontendId": "2860",
"questionTitle": "Happy Students",
"stats": {
"totalAccepted": "5.8K",
"totalSubmission": "10.4K",
"totalAcceptedRaw": 5817,
"totalSubmissionRaw": 10411,
"acRate": "55.9%"
}
} |
LeetCode/3095 | # Maximum Number of Alloys
You are the owner of a company that creates alloys using various types of metals. There are `n` different types of metals available, and you have access to `k` machines that can be used to create alloys. Each machine requires a specific amount of each metal type to create an alloy.
For the `ith` machine to create an alloy, it needs `composition[i][j]` units of metal of type `j`. Initially, you have `stock[i]` units of metal type `i`, and purchasing one unit of metal type `i` costs `cost[i]` coins.
Given integers `n`, `k`, `budget`, a **1-indexed** 2D array `composition`, and **1-indexed** arrays `stock` and `cost`, your goal is to **maximize** the number of alloys the company can create while staying within the budget of `budget` coins.
**All alloys must be created with the same machine.**
Return *the maximum number of alloys that the company can create*.
**Example 1:**
```
**Input:** n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,0], cost = [1,2,3]
**Output:** 2
**Explanation:** It is optimal to use the 1st machine to create alloys.
To create 2 alloys we need to buy the:
- 2 units of metal of the 1st type.
- 2 units of metal of the 2nd type.
- 2 units of metal of the 3rd type.
In total, we need 2 * 1 + 2 * 2 + 2 * 3 = 12 coins, which is smaller than or equal to budget = 15.
Notice that we have 0 units of metal of each type and we have to buy all the required units of metal.
It can be proven that we can create at most 2 alloys.
```
**Example 2:**
```
**Input:** n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,100], cost = [1,2,3]
**Output:** 5
**Explanation:** It is optimal to use the 2nd machine to create alloys.
To create 5 alloys we need to buy:
- 5 units of metal of the 1st type.
- 5 units of metal of the 2nd type.
- 0 units of metal of the 3rd type.
In total, we need 5 * 1 + 5 * 2 + 0 * 3 = 15 coins, which is smaller than or equal to budget = 15.
It can be proven that we can create at most 5 alloys.
```
**Example 3:**
```
**Input:** n = 2, k = 3, budget = 10, composition = [[2,1],[1,2],[1,1]], stock = [1,1], cost = [5,5]
**Output:** 2
**Explanation:** It is optimal to use the 3rd machine to create alloys.
To create 2 alloys we need to buy the:
- 1 unit of metal of the 1st type.
- 1 unit of metal of the 2nd type.
In total, we need 1 * 5 + 1 * 5 = 10 coins, which is smaller than or equal to budget = 10.
It can be proven that we can create at most 2 alloys.
```
**Constraints:**
* `1 <= n, k <= 100`
* `0 <= budget <= 108`
* `composition.length == k`
* `composition[i].length == n`
* `1 <= composition[i][j] <= 100`
* `stock.length == cost.length == n`
* `0 <= stock[i] <= 108`
* `1 <= cost[i] <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maxNumberOfAlloys(self, n: int, k: int, budget: int, composition: List[List[int]], stock: List[int], cost: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maxNumberOfAlloys(*[3, 2, 15, [[1, 1, 1], [1, 1, 10]], [0, 0, 0], [1, 2, 3]]) == 2\nassert my_solution.maxNumberOfAlloys(*[3, 2, 15, [[1, 1, 1], [1, 1, 10]], [0, 0, 100], [1, 2, 3]]) == 5\nassert my_solution.maxNumberOfAlloys(*[2, 3, 10, [[2, 1], [1, 2], [1, 1]], [1, 1], [5, 5]]) == 2\nassert my_solution.maxNumberOfAlloys(*[4, 4, 17, [[10, 10, 1, 5], [9, 7, 7, 1], [6, 3, 5, 9], [2, 10, 2, 7]], [9, 8, 2, 7], [9, 2, 6, 10]]) == 1\nassert my_solution.maxNumberOfAlloys(*[4, 9, 55, [[8, 3, 4, 2], [3, 9, 5, 5], [1, 7, 9, 8], [7, 6, 5, 1], [4, 6, 9, 4], [6, 8, 7, 1], [5, 10, 3, 4], [10, 1, 2, 4], [10, 3, 7, 2]], [9, 1, 10, 0], [3, 4, 9, 9]]) == 1\nassert my_solution.maxNumberOfAlloys(*[10, 10, 142, [[5, 3, 7, 3, 5, 5, 1, 6, 4, 3], [4, 8, 10, 8, 8, 3, 10, 6, 3, 8], [10, 2, 5, 10, 9, 2, 8, 5, 10, 7], [10, 8, 8, 8, 10, 8, 9, 6, 1, 8], [6, 2, 2, 3, 6, 3, 1, 10, 5, 8], [10, 7, 3, 10, 7, 6, 6, 10, 4, 5], [10, 2, 8, 10, 1, 8, 7, 6, 6, 7], [4, 1, 9, 6, 8, 8, 7, 1, 1, 4], [10, 9, 1, 2, 6, 4, 6, 8, 9, 4], [5, 6, 7, 2, 7, 10, 7, 8, 3, 5]], [0, 6, 3, 0, 0, 8, 1, 2, 8, 6], [2, 2, 2, 7, 4, 2, 10, 8, 9, 8]]) == 1\nassert my_solution.maxNumberOfAlloys(*[9, 3, 90, [[10, 9, 1, 3, 3, 5, 5, 10, 7], [2, 6, 4, 9, 9, 1, 9, 6, 7], [1, 4, 7, 6, 7, 7, 10, 6, 6]], [3, 10, 10, 8, 10, 5, 7, 1, 2], [9, 8, 10, 9, 9, 3, 9, 5, 8]]) == 1\nassert my_solution.maxNumberOfAlloys(*[8, 4, 196, [[5, 2, 3, 4, 7, 3, 3, 1], [1, 5, 9, 9, 6, 1, 9, 7], [5, 8, 3, 10, 2, 4, 8, 7], [9, 9, 5, 9, 6, 8, 4, 3]], [3, 5, 3, 6, 1, 5, 8, 1], [4, 5, 4, 9, 4, 8, 7, 5]]) == 2\nassert my_solution.maxNumberOfAlloys(*[2, 5, 48, [[6, 3], [9, 5], [1, 9], [1, 8], [3, 3]], [4, 8], [10, 1]]) == 5\nassert my_solution.maxNumberOfAlloys(*[3, 8, 50, [[10, 8, 5], [9, 8, 8], [2, 3, 1], [6, 2, 7], [5, 5, 3], [3, 5, 6], [8, 2, 9], [10, 2, 1]], [3, 9, 5], [1, 10, 6]]) == 4\nassert my_solution.maxNumberOfAlloys(*[6, 1, 195, [[4, 7, 7, 9, 6, 9]], [7, 4, 1, 4, 4, 0], [6, 6, 9, 10, 7, 9]]) == 0\nassert my_solution.maxNumberOfAlloys(*[10, 4, 149, [[9, 10, 1, 7, 6, 4, 9, 5, 7, 8], [9, 7, 2, 10, 7, 9, 10, 10, 1, 8], [1, 10, 9, 3, 5, 6, 6, 1, 8, 4], [9, 6, 2, 3, 9, 10, 6, 8, 7, 3]], [5, 0, 7, 5, 7, 8, 2, 2, 6, 10], [7, 5, 3, 3, 10, 9, 9, 3, 6, 8]]) == 1\nassert my_solution.maxNumberOfAlloys(*[5, 3, 110, [[5, 8, 9, 3, 10], [10, 10, 2, 1, 9], [7, 8, 2, 3, 4]], [7, 3, 4, 8, 4], [2, 2, 6, 5, 7]]) == 2\nassert my_solution.maxNumberOfAlloys(*[2, 3, 12, [[5, 9], [7, 8], [1, 1]], [0, 9], [8, 5]]) == 1\nassert my_solution.maxNumberOfAlloys(*[9, 5, 172, [[8, 8, 7, 6, 5, 3, 6, 10, 8], [9, 5, 4, 5, 9, 9, 2, 8, 5], [1, 9, 7, 8, 4, 10, 5, 1, 2], [10, 10, 4, 4, 5, 5, 5, 5, 9], [7, 10, 4, 7, 9, 6, 3, 1, 8]], [5, 0, 10, 0, 0, 8, 10, 9, 8], [3, 7, 6, 10, 10, 5, 2, 10, 6]]) == 0\nassert my_solution.maxNumberOfAlloys(*[7, 10, 31, [[10, 6, 2, 1, 6, 3, 9], [9, 7, 1, 4, 3, 3, 6], [4, 8, 3, 10, 7, 2, 10], [8, 1, 3, 3, 9, 3, 6], [6, 3, 2, 4, 9, 7, 5], [4, 2, 10, 2, 9, 8, 2], [9, 3, 6, 1, 3, 8, 1], [9, 5, 6, 9, 4, 10, 3], [1, 8, 8, 2, 5, 4, 10], [1, 6, 6, 6, 10, 6, 4]], [3, 9, 10, 4, 4, 8, 9], [6, 6, 9, 2, 1, 9, 6]]) == 1\nassert my_solution.maxNumberOfAlloys(*[4, 9, 103, [[5, 9, 6, 3], [1, 5, 7, 5], [5, 4, 10, 6], [2, 2, 4, 6], [1, 1, 2, 2], [10, 6, 5, 4], [9, 7, 8, 9], [3, 7, 8, 2], [8, 2, 4, 4]], [7, 7, 7, 3], [4, 7, 6, 10]]) == 5\nassert my_solution.maxNumberOfAlloys(*[10, 1, 197, [[7, 6, 6, 1, 2, 4, 8, 6, 4, 10]], [1, 3, 2, 1, 3, 4, 2, 6, 1, 1], [10, 6, 2, 1, 6, 2, 6, 5, 9, 8]]) == 0\nassert my_solution.maxNumberOfAlloys(*[10, 4, 152, [[1, 7, 1, 3, 9, 6, 8, 9, 10, 4], [8, 8, 9, 3, 10, 10, 4, 3, 2, 2], [3, 6, 4, 6, 1, 9, 4, 1, 4, 5], [2, 5, 1, 8, 3, 10, 6, 3, 8, 4]], [7, 2, 9, 6, 9, 4, 6, 6, 3, 6], [8, 2, 3, 9, 1, 10, 1, 9, 5, 4]]) == 1\nassert my_solution.maxNumberOfAlloys(*[6, 9, 72, [[1, 10, 8, 5, 4, 3], [6, 7, 3, 6, 10, 3], [10, 9, 8, 6, 2, 10], [8, 9, 10, 7, 9, 10], [2, 7, 2, 7, 6, 9], [4, 2, 8, 2, 7, 9], [2, 1, 1, 8, 8, 9], [5, 7, 1, 7, 3, 5], [4, 4, 4, 3, 10, 4]], [3, 3, 1, 6, 10, 8], [1, 8, 9, 8, 3, 3]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3095",
"questionFrontendId": "2861",
"questionTitle": "Maximum Number of Alloys",
"stats": {
"totalAccepted": "4.8K",
"totalSubmission": "13.8K",
"totalAcceptedRaw": 4828,
"totalSubmissionRaw": 13788,
"acRate": "35.0%"
}
} |
LeetCode/3047 | # Maximum Element-Sum of a Complete Subset of Indices
You are given a **1****-indexed** array `nums` of `n` integers.
A set of numbers is **complete** if the product of every pair of its elements is a perfect square.
For a subset of the indices set `{1, 2, ..., n}` represented as `{i1, i2, ..., ik}`, we define its **element-sum** as: `nums[i1] + nums[i2] + ... + nums[ik]`.
Return *the **maximum element-sum** of a **complete** subset of the indices set* `{1, 2, ..., n}`.
A perfect square is a number that can be expressed as the product of an integer by itself.
**Example 1:**
```
**Input:** nums = [8,7,3,5,7,2,4,9]
**Output:** 16
**Explanation:** Apart from the subsets consisting of a single index, there are two other complete subsets of indices: {1,4} and {2,8}.
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 8 + 5 = 13.
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 7 + 9 = 16.
Hence, the maximum element-sum of a complete subset of indices is 16.
```
**Example 2:**
```
**Input:** nums = [5,10,3,10,1,13,7,9,4]
**Output:** 19
**Explanation:** Apart from the subsets consisting of a single index, there are four other complete subsets of indices: {1,4}, {1,9}, {2,8}, {4,9}, and {1,4,9}.
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 5 + 10 = 15.
The sum of the elements corresponding to indices 1 and 9 is equal to nums[1] + nums[9] = 5 + 4 = 9.
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 10 + 9 = 19.
The sum of the elements corresponding to indices 4 and 9 is equal to nums[4] + nums[9] = 10 + 4 = 14.
The sum of the elements corresponding to indices 1, 4, and 9 is equal to nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19.
Hence, the maximum element-sum of a complete subset of indices is 19.
```
**Constraints:**
* `1 <= n == nums.length <= 104`
* `1 <= nums[i] <= 109`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def maximumSum(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.maximumSum(*[[8, 7, 3, 5, 7, 2, 4, 9]]) == 16\nassert my_solution.maximumSum(*[[5, 10, 3, 10, 1, 13, 7, 9, 4]]) == 19\nassert my_solution.maximumSum(*[[1, 1, 1, 1]]) == 2\nassert my_solution.maximumSum(*[[1, 100, 100, 1]]) == 100\nassert my_solution.maximumSum(*[[998244353, 998244353, 998244353, 998244353]]) == 1996488706\nassert my_solution.maximumSum(*[[1000000000, 1, 1, 1000000000]]) == 2000000000\nassert my_solution.maximumSum(*[[1, 1, 1000000000, 1]]) == 1000000000\nassert my_solution.maximumSum(*[[5, 10, 3, 10, 1, 13, 7, 20, 4]]) == 30\nassert my_solution.maximumSum(*[[5, 3, 3, 10, 1, 13, 7, 67, 4]]) == 70\nassert my_solution.maximumSum(*[[35, 45, 29, 16, 42, 49, 25, 19, 46]]) == 97\nassert my_solution.maximumSum(*[[34, 43, 47, 50, 45]]) == 84\nassert my_solution.maximumSum(*[[4, 31, 45, 34, 44]]) == 45\nassert my_solution.maximumSum(*[[12, 5, 49, 26]]) == 49\nassert my_solution.maximumSum(*[[41]]) == 41\nassert my_solution.maximumSum(*[[38, 9, 37, 11, 20, 43]]) == 49\nassert my_solution.maximumSum(*[[47, 50, 17, 49, 12, 22]]) == 96\nassert my_solution.maximumSum(*[[23, 13, 17, 3, 21, 23]]) == 26\nassert my_solution.maximumSum(*[[38, 28]]) == 38\nassert my_solution.maximumSum(*[[16, 31, 37, 29, 28, 34, 25, 36]]) == 67\nassert my_solution.maximumSum(*[[19, 46, 37, 44, 4, 40, 24, 17, 49]]) == 112\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3047",
"questionFrontendId": "2862",
"questionTitle": "Maximum Element-Sum of a Complete Subset of Indices",
"stats": {
"totalAccepted": "3.2K",
"totalSubmission": "6.7K",
"totalAcceptedRaw": 3221,
"totalSubmissionRaw": 6704,
"acRate": "48.0%"
}
} |
LeetCode/3045 | # Minimum Right Shifts to Sort the Array
You are given a **0-indexed** array `nums` of length `n` containing **distinct** positive integers. Return *the **minimum** number of **right shifts** required to sort* `nums` *and* `-1` *if this is not possible.*
A **right shift** is defined as shifting the element at index `i` to index `(i + 1) % n`, for all indices.
**Example 1:**
```
**Input:** nums = [3,4,5,1,2]
**Output:** 2
**Explanation:**
After the first right shift, nums = [2,3,4,5,1].
After the second right shift, nums = [1,2,3,4,5].
Now nums is sorted; therefore the answer is 2.
```
**Example 2:**
```
**Input:** nums = [1,3,5]
**Output:** 0
**Explanation:** nums is already sorted therefore, the answer is 0.
```
**Example 3:**
```
**Input:** nums = [2,1,4]
**Output:** -1
**Explanation:** It's impossible to sort the array using right shifts.
```
**Constraints:**
* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
* `nums` contains distinct integers.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minimumRightShifts(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minimumRightShifts(*[[3, 4, 5, 1, 2]]) == 2\nassert my_solution.minimumRightShifts(*[[1, 3, 5]]) == 0\nassert my_solution.minimumRightShifts(*[[2, 1, 4]]) == -1\nassert my_solution.minimumRightShifts(*[[31, 72, 79, 25]]) == 1\nassert my_solution.minimumRightShifts(*[[27, 33, 42, 58, 81, 8, 9, 17]]) == 3\nassert my_solution.minimumRightShifts(*[[72, 13, 21, 35, 52]]) == 4\nassert my_solution.minimumRightShifts(*[[65, 73, 77, 1]]) == 1\nassert my_solution.minimumRightShifts(*[[100, 8, 14, 68, 80, 84]]) == 5\nassert my_solution.minimumRightShifts(*[[53, 60, 64, 69, 98, 40]]) == 1\nassert my_solution.minimumRightShifts(*[[21]]) == 0\nassert my_solution.minimumRightShifts(*[[78, 12, 18, 21, 23, 36, 64, 70]]) == 7\nassert my_solution.minimumRightShifts(*[[25, 26, 53, 91, 92, 99, 10, 24]]) == 2\nassert my_solution.minimumRightShifts(*[[63, 51, 65, 87, 6, 17, 32, 14, 42, 46]]) == -1\nassert my_solution.minimumRightShifts(*[[43, 46, 75, 76, 85, 96, 9, 19, 25]]) == 3\nassert my_solution.minimumRightShifts(*[[5]]) == 0\nassert my_solution.minimumRightShifts(*[[35, 72, 76, 82, 96, 97, 24, 26]]) == 2\nassert my_solution.minimumRightShifts(*[[82, 30, 94, 55, 76, 51, 3, 89, 52, 96]]) == -1\nassert my_solution.minimumRightShifts(*[[57, 59, 88, 97, 6, 27, 41, 46, 52]]) == 5\nassert my_solution.minimumRightShifts(*[[17]]) == 0\nassert my_solution.minimumRightShifts(*[[62]]) == 0\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3045",
"questionFrontendId": "2855",
"questionTitle": "Minimum Right Shifts to Sort the Array",
"stats": {
"totalAccepted": "4.4K",
"totalSubmission": "7.4K",
"totalAcceptedRaw": 4373,
"totalSubmissionRaw": 7420,
"acRate": "58.9%"
}
} |
LeetCode/3081 | # Minimum Array Length After Pair Removals
You are given a **0-indexed** **sorted** array of integers `nums`.
You can perform the following operation any number of times:
* Choose **two** indices, `i` and `j`, where `i < j`, such that `nums[i] < nums[j]`.
* Then, remove the elements at indices `i` and `j` from `nums`. The remaining elements retain their original order, and the array is re-indexed.
Return *an integer that denotes the **minimum** length of* `nums` *after performing the operation any number of times (**including zero**).*
Note that `nums` is sorted in **non-decreasing** order.
**Example 1:**
```
**Input:** nums = [1,3,4,9]
**Output:** 0
**Explanation:** Initially, nums = [1, 3, 4, 9].
In the first operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 1 < 3.
Remove indices 0 and 1, and nums becomes [4, 9].
For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 4 < 9.
Remove indices 0 and 1, and nums becomes an empty array [].
Hence, the minimum length achievable is 0.
```
**Example 2:**
```
**Input:** nums = [2,3,6,9]
**Output:** 0
**Explanation:** Initially, nums = [2, 3, 6, 9].
In the first operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 2 < 6.
Remove indices 0 and 2, and nums becomes [3, 9].
For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 3 < 9.
Remove indices 0 and 1, and nums becomes an empty array [].
Hence, the minimum length achievable is 0.
```
**Example 3:**
```
**Input:** nums = [1,1,2]
**Output:** 1
**Explanation:** Initially, nums = [1, 1, 2].
In an operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 1 < 2.
Remove indices 0 and 2, and nums becomes [1].
It is no longer possible to perform an operation on the array.
Hence, the minimum achievable length is 1.
```
**Constraints:**
* `1 <= nums.length <= 105`
* `1 <= nums[i] <= 109`
* `nums` is sorted in **non-decreasing** order.
Please make sure your answer follows the type signature below:
```python3
class Solution:
def minLengthAfterRemovals(self, nums: List[int]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.minLengthAfterRemovals(*[[1, 3, 4, 9]]) == 0\nassert my_solution.minLengthAfterRemovals(*[[2, 3, 6, 9]]) == 0\nassert my_solution.minLengthAfterRemovals(*[[1, 1, 2]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[1]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[2]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[3]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[5]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[1, 1]]) == 2\nassert my_solution.minLengthAfterRemovals(*[[1, 2]]) == 0\nassert my_solution.minLengthAfterRemovals(*[[1, 4]]) == 0\nassert my_solution.minLengthAfterRemovals(*[[2, 3]]) == 0\nassert my_solution.minLengthAfterRemovals(*[[3, 3]]) == 2\nassert my_solution.minLengthAfterRemovals(*[[4, 5]]) == 0\nassert my_solution.minLengthAfterRemovals(*[[1, 1, 1]]) == 3\nassert my_solution.minLengthAfterRemovals(*[[1, 2, 2]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[1, 3, 3]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[2, 2, 2]]) == 3\nassert my_solution.minLengthAfterRemovals(*[[2, 3, 3]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[2, 3, 4]]) == 1\nassert my_solution.minLengthAfterRemovals(*[[3, 4, 5]]) == 1\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3081",
"questionFrontendId": "2856",
"questionTitle": "Minimum Array Length After Pair Removals",
"stats": {
"totalAccepted": "3.4K",
"totalSubmission": "13K",
"totalAcceptedRaw": 3447,
"totalSubmissionRaw": 12981,
"acRate": "26.6%"
}
} |
LeetCode/2953 | # Count Pairs of Points With Distance k
You are given a **2D** integer array `coordinates` and an integer `k`, where `coordinates[i] = [xi, yi]` are the coordinates of the `ith` point in a 2D plane.
We define the **distance** between two points `(x1, y1)` and `(x2, y2)` as `(x1 XOR x2) + (y1 XOR y2)` where `XOR` is the bitwise `XOR` operation.
Return *the number of pairs* `(i, j)` *such that* `i < j` *and the distance between points* `i` *and* `j` *is equal to* `k`.
**Example 1:**
```
**Input:** coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5
**Output:** 2
**Explanation:** We can choose the following pairs:
- (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5.
- (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5.
```
**Example 2:**
```
**Input:** coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0
**Output:** 10
**Explanation:** Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs.
```
**Constraints:**
* `2 <= coordinates.length <= 50000`
* `0 <= xi, yi <= 106`
* `0 <= k <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def countPairs(self, coordinates: List[List[int]], k: int) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.countPairs(*[[[1, 2], [4, 2], [1, 3], [5, 2]], 5]) == 2\nassert my_solution.countPairs(*[[[1, 3], [1, 3], [1, 3], [1, 3], [1, 3]], 0]) == 10\nassert my_solution.countPairs(*[[[27, 94], [61, 68], [47, 0], [100, 4], [127, 89], [61, 103], [26, 4], [51, 54], [91, 26], [98, 23], [80, 74], [19, 93]], 95]) == 5\nassert my_solution.countPairs(*[[[39, 29], [98, 59], [65, 77], [41, 26], [95, 12], [71, 66], [41, 93], [28, 33], [96, 40], [39, 8], [106, 54], [8, 49], [68, 59], [21, 15], [3, 66], [77, 85], [111, 51]], 21]) == 6\nassert my_solution.countPairs(*[[[100, 32], [69, 8], [85, 31], [69, 47], [62, 34], [102, 43], [81, 39], [90, 0], [123, 6], [79, 18], [21, 94], [13, 36], [49, 97], [76, 59], [42, 74], [60, 68], [21, 11], [71, 21], [64, 13], [64, 95], [5, 85], [118, 53], [70, 44], [38, 57], [32, 119], [80, 61], [13, 68], [43, 108], [86, 49]], 39]) == 20\nassert my_solution.countPairs(*[[[60, 55], [35, 32], [99, 2], [58, 57], [16, 2], [43, 28], [30, 35], [35, 83], [104, 41], [20, 69], [58, 14], [12, 92], [71, 49], [7, 82], [65, 68], [9, 40], [15, 56], [57, 46], [21, 8], [37, 64], [42, 94], [73, 91], [12, 121], [10, 21], [41, 89]], 54]) == 10\nassert my_solution.countPairs(*[[[94, 23], [86, 58], [126, 55], [107, 23], [121, 60], [89, 28], [123, 15], [127, 3], [100, 49], [5, 3], [81, 49], [93, 0], [95, 37], [92, 25]], 53]) == 18\nassert my_solution.countPairs(*[[[40, 54], [8, 68], [33, 11], [51, 93], [95, 95], [17, 53], [35, 39], [59, 42], [28, 63], [41, 63], [54, 0], [88, 31], [5, 107], [32, 124], [74, 64], [15, 27], [61, 92], [16, 47], [62, 22], [2, 28], [27, 14], [53, 39], [21, 91], [7, 11]], 60]) == 11\nassert my_solution.countPairs(*[[[28, 14], [2, 13], [28, 14], [4, 7], [23, 1], [54, 0], [43, 22], [98, 16]], 33]) == 5\nassert my_solution.countPairs(*[[[84, 92], [84, 92], [84, 92], [84, 92], [84, 92], [54, 59], [84, 92], [93, 44]], 0]) == 15\nassert my_solution.countPairs(*[[[10, 57], [12, 62], [92, 44], [7, 60], [8, 55], [13, 50], [5, 55], [71, 82], [64, 26], [68, 43], [61, 88], [9, 44], [95, 16], [17, 16], [12, 53], [9, 59], [81, 44], [3, 56], [70, 94], [0, 58], [84, 29], [13, 63], [79, 87], [19, 39], [74, 35], [92, 7], [31, 6], [2, 50]], 13]) == 19\nassert my_solution.countPairs(*[[[56, 47], [26, 50], [51, 2], [40, 7], [24, 34], [55, 2], [13, 92], [57, 50], [47, 35], [32, 96], [14, 0], [4, 84], [86, 95]], 56]) == 4\nassert my_solution.countPairs(*[[[34, 60], [17, 93], [87, 90], [32, 125], [71, 27], [27, 26], [127, 115], [91, 27], [63, 68], [97, 48], [69, 73], [120, 78], [43, 55], [101, 125], [86, 87], [12, 35], [5, 20], [46, 12], [17, 24], [107, 62], [86, 88], [26, 80], [30, 41], [110, 114]], 81]) == 17\nassert my_solution.countPairs(*[[[65, 19], [12, 80], [90, 64], [38, 68], [17, 25], [49, 36], [91, 47], [20, 31], [81, 54], [83, 20], [90, 100], [0, 6], [93, 121]], 36]) == 3\nassert my_solution.countPairs(*[[[24, 75], [22, 67]], 23]) == 0\nassert my_solution.countPairs(*[[[42, 32], [62, 60], [57, 61], [100, 56], [91, 62], [57, 21], [100, 56], [63, 63], [45, 52], [59, 75], [32, 61], [57, 43], [61, 57], [64, 52], [24, 54], [92, 15], [53, 25], [84, 63], [1, 18], [21, 57], [29, 9], [68, 91], [22, 43], [105, 27]], 48]) == 18\nassert my_solution.countPairs(*[[[70, 98], [79, 66], [71, 63], [111, 94], [3, 50], [64, 111], [98, 67], [23, 41], [66, 14], [40, 19], [15, 13], [32, 86], [59, 58], [73, 94], [18, 10], [77, 50], [20, 60], [66, 8], [15, 30], [71, 2], [55, 9]], 60]) == 7\nassert my_solution.countPairs(*[[[5, 100], [60, 9], [84, 65], [38, 66], [83, 35], [17, 80], [88, 76], [80, 101], [55, 74], [46, 62], [28, 73], [54, 40], [119, 71], [10, 94], [45, 82], [20, 90], [47, 27], [41, 97], [66, 5], [33, 0], [101, 5], [89, 125], [6, 58], [61, 107], [25, 17], [104, 0], [29, 2]], 73]) == 15\nassert my_solution.countPairs(*[[[29, 23], [8, 19], [26, 5], [12, 25], [37, 2], [37, 27], [18, 68], [3, 53], [81, 85], [27, 94], [29, 39], [41, 64], [26, 28], [23, 80], [13, 46], [5, 68], [16, 18], [21, 77]], 25]) == 8\nassert my_solution.countPairs(*[[[90, 31], [113, 54], [92, 36], [67, 49], [123, 124], [127, 112], [16, 24], [85, 50], [58, 94], [115, 48], [83, 30], [51, 112], [39, 23], [0, 21], [27, 44], [99, 100], [122, 63], [34, 39], [25, 48], [44, 49], [84, 97], [31, 61]], 84]) == 10\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "2953",
"questionFrontendId": "2857",
"questionTitle": "Count Pairs of Points With Distance k",
"stats": {
"totalAccepted": "2.8K",
"totalSubmission": "7K",
"totalAcceptedRaw": 2822,
"totalSubmissionRaw": 7003,
"acRate": "40.3%"
}
} |
LeetCode/3034 | # Points That Intersect With Cars
You are given a **0-indexed** 2D integer array `nums` representing the coordinates of the cars parking on a number line. For any index `i`, `nums[i] = [starti, endi]` where `starti` is the starting point of the `ith` car and `endi` is the ending point of the `ith` car.
Return *the number of integer points on the line that are covered with **any part** of a car.*
**Example 1:**
```
**Input:** nums = [[3,6],[1,5],[4,7]]
**Output:** 7
**Explanation:** All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.
```
**Example 2:**
```
**Input:** nums = [[1,3],[5,8]]
**Output:** 7
**Explanation:** Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7.
```
**Constraints:**
* `1 <= nums.length <= 100`
* `nums[i].length == 2`
* `1 <= starti <= endi <= 100`
Please make sure your answer follows the type signature below:
```python3
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
```
| {
"code": "\nfrom typing import *\n\n#<INSERT>\n\nmy_solution = Solution()\n\nassert my_solution.numberOfPoints(*[[[3, 6], [1, 5], [4, 7]]]) == 7\nassert my_solution.numberOfPoints(*[[[1, 3], [5, 8]]]) == 7\nassert my_solution.numberOfPoints(*[[[4, 4], [9, 10], [9, 10], [3, 8]]]) == 8\nassert my_solution.numberOfPoints(*[[[2, 5], [3, 8], [1, 6], [4, 10]]]) == 10\nassert my_solution.numberOfPoints(*[[[2, 3], [3, 9], [5, 7], [4, 10], [9, 10]]]) == 9\nassert my_solution.numberOfPoints(*[[[4, 10]]]) == 7\nassert my_solution.numberOfPoints(*[[[1, 9], [2, 10], [6, 7], [8, 9], [5, 8], [1, 3]]]) == 10\nassert my_solution.numberOfPoints(*[[[5, 10], [3, 8], [3, 9]]]) == 8\nassert my_solution.numberOfPoints(*[[[2, 3], [3, 10], [5, 8], [4, 8], [2, 7], [3, 4], [3, 10], [7, 8]]]) == 9\nassert my_solution.numberOfPoints(*[[[1, 3], [2, 4], [6, 6], [6, 9], [2, 10], [4, 10], [3, 6], [1, 4], [1, 3]]]) == 10\nassert my_solution.numberOfPoints(*[[[4, 10], [3, 9], [3, 5], [4, 10], [7, 10], [1, 7], [7, 9], [4, 8]]]) == 10\nassert my_solution.numberOfPoints(*[[[1, 6], [6, 7], [1, 6], [1, 3], [1, 8], [2, 9], [3, 8], [1, 9]]]) == 9\nassert my_solution.numberOfPoints(*[[[1, 6], [8, 10], [3, 7], [6, 10], [3, 10], [1, 10], [7, 8]]]) == 10\nassert my_solution.numberOfPoints(*[[[6, 8], [2, 8], [3, 9], [3, 5], [6, 10], [1, 2], [5, 5]]]) == 10\nassert my_solution.numberOfPoints(*[[[4, 5], [5, 9], [2, 3], [5, 10], [1, 9], [1, 8], [2, 9], [2, 10]]]) == 10\nassert my_solution.numberOfPoints(*[[[8, 9], [6, 7], [6, 9], [3, 5], [7, 10], [5, 9], [10, 10]]]) == 8\nassert my_solution.numberOfPoints(*[[[6, 8], [7, 10], [9, 10], [6, 10], [1, 10], [5, 10]]]) == 10\nassert my_solution.numberOfPoints(*[[[9, 9], [2, 8], [5, 8], [3, 5], [2, 2], [7, 9], [5, 10]]]) == 9\nassert my_solution.numberOfPoints(*[[[3, 9], [5, 9]]]) == 7\nassert my_solution.numberOfPoints(*[[[5, 10], [2, 3], [3, 10], [4, 7], [1, 9], [5, 10], [2, 6], [1, 7], [8, 9], [2, 9]]]) == 10\n"
} | {
"programming_language": "python",
"execution_language": "python",
"questionId": "3034",
"questionFrontendId": "2848",
"questionTitle": "Points That Intersect With Cars",
"stats": {
"totalAccepted": "8K",
"totalSubmission": "10.8K",
"totalAcceptedRaw": 7978,
"totalSubmissionRaw": 10798,
"acRate": "73.9%"
}
} |