task_id
stringlengths 33
83
| url
stringlengths 44
94
| title
stringlengths 14
64
| meta
dict | prompt
stringlengths 399
2.26k
| prompt_sft
stringlengths 424
2.29k
| test
stringlengths 8.25k
44.9k
| start_time
int64 1.69B
1.71B
|
---|---|---|---|---|---|---|---|
biweekly-contest-114-minimum-operations-to-collect-elements | https://leetcode.com/problems/minimum-operations-to-collect-elements | minimum-operations-to-collect-elements | {
"questionId": "3044",
"questionFrontendId": "2869",
"title": "Minimum Operations to Collect Elements",
"titleSlug": "minimum-operations-to-collect-elements",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 130,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
给你一个正整数数组 nums 和一个整数 k 。
一次操作中,你可以将数组的最后一个元素删除,将该元素添加到一个集合中。
请你返回收集元素 1, 2, ..., k 需要的 最少操作次数 。
示例 1:
输入:nums = [3,1,5,4,2], k = 2
输出:4
解释:4 次操作后,集合中的元素依次添加了 2 ,4 ,5 和 1 。此时集合中包含元素 1 和 2 ,所以答案为 4 。
示例 2:
输入:nums = [3,1,5,4,2], k = 5
输出:5
解释:5 次操作后,集合中的元素依次添加了 2 ,4 ,5 ,1 和 3 。此时集合中包含元素 1 到 5 ,所以答案为 5 。
示例 3:
输入:nums = [3,2,5,3,1], k = 3
输出:4
解释:4 次操作后,集合中的元素依次添加了 1 ,3 ,5 和 2 。此时集合中包含元素 1 到 3 ,所以答案为 4 。
提示:
* 1 <= nums.length <= 50
* 1 <= nums[i] <= nums.length
* 1 <= k <= nums.length
* 输入保证你可以收集到元素 1, 2, ..., k 。
"""
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
| 给你一个正整数数组 nums 和一个整数 k 。
一次操作中,你可以将数组的最后一个元素删除,将该元素添加到一个集合中。
请你返回收集元素 1, 2, ..., k 需要的 最少操作次数 。
示例 1:
输入:nums = [3,1,5,4,2], k = 2
输出:4
解释:4 次操作后,集合中的元素依次添加了 2 ,4 ,5 和 1 。此时集合中包含元素 1 和 2 ,所以答案为 4 。
示例 2:
输入:nums = [3,1,5,4,2], k = 5
输出:5
解释:5 次操作后,集合中的元素依次添加了 2 ,4 ,5 ,1 和 3 。此时集合中包含元素 1 到 5 ,所以答案为 5 。
示例 3:
输入:nums = [3,2,5,3,1], k = 3
输出:4
解释:4 次操作后,集合中的元素依次添加了 1 ,3 ,5 和 2 。此时集合中包含元素 1 到 3 ,所以答案为 4 。
提示:
* 1 <= nums.length <= 50
* 1 <= nums[i] <= nums.length
* 1 <= k <= nums.length
* 输入保证你可以收集到元素 1, 2, ..., k 。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [3,1,5,4,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [3,1,5,4,2], "k": 5 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [3,2,5,3,1], "k": 3 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,3], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,3], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,3,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,3,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,3,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,3,2], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,3,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,1,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,1,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,1,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,1,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,1,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,1,3], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,1,3], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,2,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,2,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,3,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,3,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,3,1], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [3,1,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [3,1,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [3,1,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [3,1,2], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [3,1,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [3,2,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [3,2,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [3,2,1], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [3,3,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,1,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,1,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,2,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,2,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,2,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,2,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,2,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,2,3], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,2,3], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,2,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,2,4], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,3,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,3,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,3,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,3,2], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,3,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,3,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,4,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,4,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,4,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,4,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,4,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,1,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2,1,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,1,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2,1,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2,1,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2,1,3], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,1,3], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,1,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2,1,4], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,2,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2,2,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,2,2,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,2,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,2,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,2,3], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,2,3], "k": 3 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,2,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,2,4], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,1], "k": 1 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2,3,1], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,3,1], "k": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,3,2], "k": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,2], "k": 3 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,3], "k": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,3], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,3], "k": 3 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,2,3,4], "k": 2 }
assert my_solution.minOperations(**test_input) == 4 | 1,696,084,200 |
biweekly-contest-114-minimum-number-of-operations-to-make-array-empty | https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty | minimum-number-of-operations-to-make-array-empty | {
"questionId": "3094",
"questionFrontendId": "2870",
"title": "Minimum Number of Operations to Make Array Empty",
"titleSlug": "minimum-number-of-operations-to-make-array-empty",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 147,
"dislikes": 5,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的正整数数组 nums 。
你可以对数组执行以下两种操作 任意次 :
* 从数组中选择 两个 值 相等 的元素,并将它们从数组中 删除 。
* 从数组中选择 三个 值 相等 的元素,并将它们从数组中 删除 。
请你返回使数组为空的 最少 操作次数,如果无法达成,请返回 -1 。
示例 1:
输入:nums = [2,3,3,2,2,4,2,3,4]
输出:4
解释:我们可以执行以下操作使数组为空:
- 对下标为 0 和 3 的元素执行第一种操作,得到 nums = [3,3,2,4,2,3,4] 。
- 对下标为 2 和 4 的元素执行第一种操作,得到 nums = [3,3,4,3,4] 。
- 对下标为 0 ,1 和 3 的元素执行第二种操作,得到 nums = [4,4] 。
- 对下标为 0 和 1 的元素执行第一种操作,得到 nums = [] 。
至少需要 4 步操作使数组为空。
示例 2:
输入:nums = [2,1,2,2,3,3]
输出:-1
解释:无法使数组为空。
提示:
* 2 <= nums.length <= 105
* 1 <= nums[i] <= 106
"""
class Solution:
def minOperations(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始的正整数数组 nums 。
你可以对数组执行以下两种操作 任意次 :
* 从数组中选择 两个 值 相等 的元素,并将它们从数组中 删除 。
* 从数组中选择 三个 值 相等 的元素,并将它们从数组中 删除 。
请你返回使数组为空的 最少 操作次数,如果无法达成,请返回 -1 。
示例 1:
输入:nums = [2,3,3,2,2,4,2,3,4]
输出:4
解释:我们可以执行以下操作使数组为空:
- 对下标为 0 和 3 的元素执行第一种操作,得到 nums = [3,3,2,4,2,3,4] 。
- 对下标为 2 和 4 的元素执行第一种操作,得到 nums = [3,3,4,3,4] 。
- 对下标为 0 ,1 和 3 的元素执行第二种操作,得到 nums = [4,4] 。
- 对下标为 0 和 1 的元素执行第一种操作,得到 nums = [] 。
至少需要 4 步操作使数组为空。
示例 2:
输入:nums = [2,1,2,2,3,3]
输出:-1
解释:无法使数组为空。
提示:
* 2 <= nums.length <= 105
* 1 <= nums[i] <= 106
请完成下面的代码来解决上述问题:
```python
class Solution:
def minOperations(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,3,3,2,2,4,2,3,4] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [2,1,2,2,3,3] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [3,3] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [14,12,14,14,12,14,14,12,12,12,12,14,14,12,14,14,14,12,12] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [2,2,2,2,2,2,2,2,2] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [15,3,3,15,15,13,8,15,6,15,3,1,8,8,15] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [19,19,19,19,19,19,19,19,19,19,19,19,19] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [13,7,13,7,13,7,13,13,7] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [5,5] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [3,14,3,14,3,14,14,3,3,14,14,14,3,14,14,3,14,14,14,3] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [16,16,16,19,16,3,16,8,16,16,16,19,3,16,16] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [11,11,11,11,19,11,11,11,11,11,19,11,11,11,11,11,19] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [1,1,1,5,1,5,1,1,1,1,1,1,1] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [16,16,16,3,16,16,3] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [14,4,4,19,19] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [1,14,1,1,1] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [3,10,11,3,3,11,3,3,3,3,3,3,3,3,10,3,3,3] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [3,8,8,8,8,3,8,8,8,8,8,8,8,8] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [9,9,9] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [6,6,6,6,6,8,8,8,8,6,8,6,15,15,6,15,6,6] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [9,19,19,19,9,9,19,19,19,9,9,19,9,19,19,19] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [9,4,9,20,20,4,20] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [6,9,18,16,18,1,9,1,1,1,1,16,1,6,1,1,9,6] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [11,18,11,18,11,18,11] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [20,20,20,20,20] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [12,7,7] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [10,7,9,9,10,9,9,10,10,9,10,9,10,10] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [9,9,9,8,9,9,9,9,2,9,9,9,9,9] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [5,5,18,1,5,5] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [13,13,16,4,16,13,2,16,16,16,2,16,6,16,13,18,9] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [8,8,8,8,8,8,8,8,8,7,8,8,8,8,8,7,8] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [20,20,19,19,20,19,20,20,20,19,20,20,20,20,20,20,20,20,20] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [4,4,20,20,4,20,1,4,4,4,4,4,4,4,20,4,4] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [16,17,17,8,17,17,16,8,17,16,17] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [10,10,10,9,10,10,10,9,10,18,10,4,20,2,10,10] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [11,20,20,11,11,20,14,20,11,11,20,1] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [14,14,14,14,15,20,15] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [17,7,17,5,17,17,17,7,17,17,17,17,5,17,17,7,5] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [4,4,4,4,4,4,4,4,4] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [17,17] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [15,2,15,2,8,15,15,15,15,15,15,8,2] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [1,12,12,1,1,1,1,12,1] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [12,4,9,10,17,12,5,17,4,12,12,12,4,10,4] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [7,7,7,7,7,7,7,16,7,7,7,16,7,16,7,16,16,16,16,7] }
assert my_solution.minOperations(**test_input) == 8
test_input = { "nums": [20,20,20,20,19] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [13,13,13,13,13,13,13,13] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [15,12,18,18,15,15,15,12,12,12,12,12,12,15,18] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [14,14,14,1] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [2,2,2,2,2,2,8,2,8,2,2,2,2] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [10,16,6,6,10,6] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [18,17,3,18,6,13,3,6,14,6,15,3] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [15,15] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [9,9,9,9,9,9,9,9,9,9,9] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [11,4,4,18,11,12,18,18,12,4,4,12,4] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [2,5,20,20,5,20,20,16,20,20,20,20,20,20,3,20,20,20] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [12,13,13,13,12,13,13,13,13,13,11,13,13,13] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [1,1,1,1] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [10,10,10,10,3,10,10,3,10] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [7,14,7,7,2,2,7] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [1,10,1,10,1] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [11,11,11,11,11,11,11,11,11,11] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [13,13,13,13,13,13,13] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [19,19,19,19,18,19,15,7,19,19,15,5] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [2,12,12,12,17,12,12,12,12,12,12,12,12,12,12] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [19,16,19,19,16,16,16,16] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [15,15,15,15,15,15,11,13,15,15,11,15,13,15,11,13] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [15,16,16,15,16] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [9,7,14,9,14,7,7,9,9,9,9,9,9,14,14] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [12,16,5,5,7,10,2,16,12,7,2,12,5,16,2,11] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [18,13,13,18,18,13,13,18,13,13] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [4,4,8,10,8,10,19,19,19,19,8,8,19,4] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [1,18,14,16,14] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [7,7,7,7,3,7,7,3,7,7] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [13,13] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [6,11,6,8,6,13,17,14] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [10,2,2,10] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [19,17,17,17,17,17,17,17,19,19,19,17,19,17] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [4,16,12,7,16,16,16] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [11,11,11,11,11,11,11,11,11,11,11,11,11,11,11] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [6,6,6,13,6,18,13,18,5,18,12,3,12,12,18,6,18,3,18,6] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [4,4,3,3,4,4] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [11,11,11,11,9,11,9,9,11,11,9,9,11,11,9] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [16,16] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [4,4,4,4,4,4,4,4,4,4,4,4,4,4] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [17,16,16,17,16,16,16] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [10,18,10,10] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [8,8] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [8,6,6,6,8,8,6,8,8,6,8,6,8,8,6,6,6,8] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [15,14,20,15,20,14,14,14,20,14,20,20] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [19,3,3,3,3,3,3,15,17,3,3,18,10,17,17,15,17,3,3] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [1,1,16,2,16,1,2,2,2,2,2,1,2,2,2,16] }
assert my_solution.minOperations(**test_input) == 6
test_input = { "nums": [1,1,4,4,4] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] }
assert my_solution.minOperations(**test_input) == 7
test_input = { "nums": [16,18,18,20] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [2,2,2,20,15,2,20,15,2,15] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [16,16,16,16,16] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,14,14,14,14,1,14,14,1,14,14,14,14,1,14,14,1,14] }
assert my_solution.minOperations(**test_input) == 7 | 1,696,084,200 |
biweekly-contest-114-split-array-into-maximum-number-of-subarrays | https://leetcode.com/problems/split-array-into-maximum-number-of-subarrays | split-array-into-maximum-number-of-subarrays | {
"questionId": "3080",
"questionFrontendId": "2871",
"title": "Split Array Into Maximum Number of Subarrays",
"titleSlug": "split-array-into-maximum-number-of-subarrays",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 188,
"dislikes": 24,
"categoryTitle": "Algorithms"
} | """
给你一个只包含 非负 整数的数组 nums 。
我们定义满足 l <= r 的子数组 nums[l..r] 的分数为 nums[l] AND nums[l + 1] AND ... AND nums[r] ,其中 AND 是按位与运算。
请你将数组分割成一个或者更多子数组,满足:
* 每个 元素都 只 属于一个子数组。
* 子数组分数之和尽可能 小 。
请你在满足以上要求的条件下,返回 最多 可以得到多少个子数组。
一个 子数组 是一个数组中一段连续的元素。
示例 1:
输入:nums = [1,0,2,0,1,2]
输出:3
解释:我们可以将数组分割成以下子数组:
- [1,0] 。子数组分数为 1 AND 0 = 0 。
- [2,0] 。子数组分数为 2 AND 0 = 0 。
- [1,2] 。子数组分数为 1 AND 2 = 0 。
分数之和为 0 + 0 + 0 = 0 ,是我们可以得到的最小分数之和。
在分数之和为 0 的前提下,最多可以将数组分割成 3 个子数组。所以返回 3 。
示例 2:
输入:nums = [5,7,1,3]
输出:1
解释:我们可以将数组分割成一个子数组:[5,7,1,3] ,分数为 1 ,这是可以得到的最小总分数。
在总分数为 1 的前提下,最多可以将数组分割成 1 个子数组。所以返回 1 。
提示:
* 1 <= nums.length <= 105
* 0 <= nums[i] <= 106
"""
class Solution:
def maxSubarrays(self, nums: List[int]) -> int:
| 给你一个只包含 非负 整数的数组 nums 。
我们定义满足 l <= r 的子数组 nums[l..r] 的分数为 nums[l] AND nums[l + 1] AND ... AND nums[r] ,其中 AND 是按位与运算。
请你将数组分割成一个或者更多子数组,满足:
* 每个 元素都 只 属于一个子数组。
* 子数组分数之和尽可能 小 。
请你在满足以上要求的条件下,返回 最多 可以得到多少个子数组。
一个 子数组 是一个数组中一段连续的元素。
示例 1:
输入:nums = [1,0,2,0,1,2]
输出:3
解释:我们可以将数组分割成以下子数组:
- [1,0] 。子数组分数为 1 AND 0 = 0 。
- [2,0] 。子数组分数为 2 AND 0 = 0 。
- [1,2] 。子数组分数为 1 AND 2 = 0 。
分数之和为 0 + 0 + 0 = 0 ,是我们可以得到的最小分数之和。
在分数之和为 0 的前提下,最多可以将数组分割成 3 个子数组。所以返回 3 。
示例 2:
输入:nums = [5,7,1,3]
输出:1
解释:我们可以将数组分割成一个子数组:[5,7,1,3] ,分数为 1 ,这是可以得到的最小总分数。
在总分数为 1 的前提下,最多可以将数组分割成 1 个子数组。所以返回 1 。
提示:
* 1 <= nums.length <= 105
* 0 <= nums[i] <= 106
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxSubarrays(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,0,2,0,1,2] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [5,7,1,3] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [1,0,2,1] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [0] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,0] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [100000] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [1,2,2,1] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [30,18,19,20,11,21,12,22,26] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [0,8,0,0,0,23] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [8,10,23,26,21,28,21,14,21,14,9,16,24,29,7,26] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [18,12,16,28,7,15,24,7,8,26,22,6,23,7,17,1,16] }
assert my_solution.maxSubarrays(**test_input) == 6
test_input = { "nums": [22] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [15,24,20,28,11,16,0,0,0,22,7,18] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [0,0,27] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [18,7,20,10,0,14,0,28,7,0,0,9,12,0] }
assert my_solution.maxSubarrays(**test_input) == 6
test_input = { "nums": [0,29,16,0,6,17] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [4,7,13,0,23,6,4] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [4,27] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [29,5,0,25,0,15,19,24,20,0,23] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [24,6] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [13,20,16,16,27,0,7,18,3,0,23,16,25,0,5,1,4] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [0,0,30,20,6,13] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [21,24,8,8,20,12,24,28,17,9,17] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [0,0,18,0,0,4,5,25,0,0,0,30,0,18,0,0,12,21,21,18] }
assert my_solution.maxSubarrays(**test_input) == 12
test_input = { "nums": [12,3,0,27,23,0,29,18,0,0,0,20,29,0,2,0,17,10,0,0] }
assert my_solution.maxSubarrays(**test_input) == 11
test_input = { "nums": [26,28,7,14,24,15,1,16,5,24,4,10,24] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [30,11,0,9,15,0,0,0] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [16,18,14,6,25,30,7,0,22,0,15,0] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [16,12,6,21,26,25,2,0,6,0,13] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [13,1,13,18,2,15,15,27,3,3,14,12,23,8,29,10,29,15,10] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [0,29,0,28,20,0,21,8,0,0,26,8,0,0,8,12] }
assert my_solution.maxSubarrays(**test_input) == 8
test_input = { "nums": [0,18,0,0,0,22,0,15] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [0,30,26] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,1,3,29,16,0,0,0,11] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [0,10,11,14,0,19,1,0,28,10,27,27,25,17,0,25,19] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [18,4,0,6,0,10,23,3,26,0] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [11,22,22,22,18,15,8,8,19,12,20,11] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [0,23,0,0,0,17,0,0] }
assert my_solution.maxSubarrays(**test_input) == 6
test_input = { "nums": [0,23,0,14,4,5,23,23,8,8,15,0,0,13,6,26,0] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [0,3,0,16,15,0,1,0,24,16,27,0,23,15,0,13,0,0] }
assert my_solution.maxSubarrays(**test_input) == 9
test_input = { "nums": [12,0,16,0,0,0,29,18] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [0,27,15,13] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [15,10,0,2,0,3,8,0,0,26,25,27,0,0,0,28,0,10,27,3] }
assert my_solution.maxSubarrays(**test_input) == 9
test_input = { "nums": [11,10,12,6,15,25,17,21,22] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [2,19,13] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [27,4] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,0,12,0,7,0,18,26,9,29,0,4,30,21,0,1] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [6,8,0,0,25,0,30,18,18,0] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [21,5,18,18,18,0,0,17,0] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [25,29,23,27] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [28,28,0,16,21,27,12,3,10,0,19,0,0] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [9,2,22,14,17,3,21,1,29,3,30,13,16,17,25,26,17] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [0,9,4,25,21,0,25,11,13,9,0,0,19,0,16,8,17,26,0,0] }
assert my_solution.maxSubarrays(**test_input) == 9
test_input = { "nums": [11,0,0,2,3,0,9,26,0,0,25,7,1,12,16,14] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [7,11,27,13,23,16,13,5,27,5,16] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [18] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [20,19,2,20,26,1,17,6,23,25,7,14,16,8] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [28,0,29,9,0,11,13,22,10,16,21,30,18,19,0,0,0] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [20,26,0,0,0,11,30] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [16,12,0,2,15,30,0,16,7,25] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [0,7,16,17,29,0,18,0,14,21,17,2,28,7,26,0,14] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [29,0,19,23,13] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,13,1,12,18,24,11,26,7] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [7,26,28,12,24,10,11,26,19,29,1,26] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [28,15,24,9,0] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [22,6,21,16,24,7,18,25,25,26,15,10,26,22,23,15] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [0,23,0,1,26,5,18,12,25,23,5,19] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [16,19,0,0] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [13] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [8,9] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,0,0,11,0,24,8,0,0,17,21,7,0,25,25] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [0,0,10,0,0,30,5,0] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [29,22,30,22,19,26] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [17,15,14,24,30,14,25,28,10,11] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,0,13,2,12,2,27] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [0,18,9,0,20,0,30,22,0] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [2,6,23,0,0,6,12,22,0,19,18,0,0,14,19,3,11,28,0,17] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [16,12,7] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,22,0,0,0,26,7] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [0,19,5,25,0,0,4,0,0,1,0,10,25,2,3] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [7,8,18,14,4,6,22,22,7] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [8,9,13,0,0,20] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [29,20,13,11,19,27,20,4,24,20,10,21,18,26,3,23,15,18,23,25] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [25,0,18,13,22,5,0,0] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [9,3,0,2,18,11,15,1,10,0,0,16,0,11] }
assert my_solution.maxSubarrays(**test_input) == 5
test_input = { "nums": [9,0,0,11,14,0,23,0] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [6,3,22,28,0,0,25,2,23,0,0,23,15,0] }
assert my_solution.maxSubarrays(**test_input) == 7
test_input = { "nums": [0,10,11,25,14] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [26,9,27,25,30,25,10,3,14,29,2,6,5,7,8,18] }
assert my_solution.maxSubarrays(**test_input) == 3
test_input = { "nums": [5,9,0,28,23,11,1,3] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [0,22,3,0,15,0,11,3,20,23,0,7,13,20,11,6,19] }
assert my_solution.maxSubarrays(**test_input) == 6
test_input = { "nums": [0,16,6] }
assert my_solution.maxSubarrays(**test_input) == 2
test_input = { "nums": [26,9,20,26,22,12,18,21,23,30,3,9,12,19,16] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [14,10,8] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [0,0,0,19,0] }
assert my_solution.maxSubarrays(**test_input) == 4
test_input = { "nums": [3,27] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [7,14,27] }
assert my_solution.maxSubarrays(**test_input) == 1
test_input = { "nums": [16,4,29,17,0,0,13,22,1,6,0,29,1,3,0,7] }
assert my_solution.maxSubarrays(**test_input) == 6
test_input = { "nums": [5,0,0,9,0,0,19,0,0,0,0,7,19,14,3,0,10,29] }
assert my_solution.maxSubarrays(**test_input) == 9
test_input = { "nums": [16,22,0,0,0,0,27,0,0,0,24,22,0,0] }
assert my_solution.maxSubarrays(**test_input) == 9 | 1,696,084,200 |
biweekly-contest-114-maximum-number-of-k-divisible-components | https://leetcode.com/problems/maximum-number-of-k-divisible-components | maximum-number-of-k-divisible-components | {
"questionId": "3058",
"questionFrontendId": "2872",
"title": "Maximum Number of K-Divisible Components",
"titleSlug": "maximum-number-of-k-divisible-components",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 170,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
给你一棵 n 个节点的无向树,节点编号为 0 到 n - 1 。给你整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。
同时给你一个下标从 0 开始长度为 n 的整数数组 values ,其中 values[i] 是第 i 个节点的 值 。再给你一个整数 k 。
你可以从树中删除一些边,也可以一条边也不删,得到若干连通块。一个 连通块的值 定义为连通块中所有节点值之和。如果所有连通块的值都可以被 k 整除,那么我们说这是一个 合法分割 。
请你返回所有合法分割中,连通块数目的最大值 。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg]
输入:n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
输出:2
解释:我们删除节点 1 和 2 之间的边。这是一个合法分割,因为:
- 节点 1 和 3 所在连通块的值为 values[1] + values[3] = 12 。
- 节点 0 ,2 和 4 所在连通块的值为 values[0] + values[2] + values[4] = 6 。
最多可以得到 2 个连通块的合法分割。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg]
输入:n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3
输出:3
解释:我们删除节点 0 和 2 ,以及节点 0 和 1 之间的边。这是一个合法分割,因为:
- 节点 0 的连通块的值为 values[0] = 3 。
- 节点 2 ,5 和 6 所在连通块的值为 values[2] + values[5] + values[6] = 9 。
- 节点 1 ,3 和 4 的连通块的值为 values[1] + values[3] + values[4] = 6 。
最多可以得到 3 个连通块的合法分割。
提示:
* 1 <= n <= 3 * 104
* edges.length == n - 1
* edges[i].length == 2
* 0 <= ai, bi < n
* values.length == n
* 0 <= values[i] <= 109
* 1 <= k <= 109
* values 之和可以被 k 整除。
* 输入保证 edges 是一棵无向树。
"""
class Solution:
def maxKDivisibleComponents(self, n: int, edges: List[List[int]], values: List[int], k: int) -> int:
| 给你一棵 n 个节点的无向树,节点编号为 0 到 n - 1 。给你整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。
同时给你一个下标从 0 开始长度为 n 的整数数组 values ,其中 values[i] 是第 i 个节点的 值 。再给你一个整数 k 。
你可以从树中删除一些边,也可以一条边也不删,得到若干连通块。一个 连通块的值 定义为连通块中所有节点值之和。如果所有连通块的值都可以被 k 整除,那么我们说这是一个 合法分割 。
请你返回所有合法分割中,连通块数目的最大值 。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg]
输入:n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
输出:2
解释:我们删除节点 1 和 2 之间的边。这是一个合法分割,因为:
- 节点 1 和 3 所在连通块的值为 values[1] + values[3] = 12 。
- 节点 0 ,2 和 4 所在连通块的值为 values[0] + values[2] + values[4] = 6 。
最多可以得到 2 个连通块的合法分割。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg]
输入:n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3
输出:3
解释:我们删除节点 0 和 2 ,以及节点 0 和 1 之间的边。这是一个合法分割,因为:
- 节点 0 的连通块的值为 values[0] = 3 。
- 节点 2 ,5 和 6 所在连通块的值为 values[2] + values[5] + values[6] = 9 。
- 节点 1 ,3 和 4 的连通块的值为 values[1] + values[3] + values[4] = 6 。
最多可以得到 3 个连通块的合法分割。
提示:
* 1 <= n <= 3 * 104
* edges.length == n - 1
* edges[i].length == 2
* 0 <= ai, bi < n
* values.length == n
* 0 <= values[i] <= 109
* 1 <= k <= 109
* values 之和可以被 k 整除。
* 输入保证 edges 是一棵无向树。
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxKDivisibleComponents(self, n: int, edges: List[List[int]], values: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 5, "edges": [[0,2],[1,2],[1,3],[2,4]], "values": [1,8,1,4,4], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 7, "edges": [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], "values": [3,0,6,1,5,2,1], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 1, "edges": [], "values": [0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 1
test_input = { "n": 1, "edges": [], "values": [10000], "k": 100 }
assert my_solution.maxKDivisibleComponents(**test_input) == 1
test_input = { "n": 2, "edges": [[1,0]], "values": [0,0], "k": 100000000 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 2, "edges": [[0,1]], "values": [1,2], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 2, "edges": [[1,0]], "values": [10000,10000], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 3, "edges": [[0,2],[2,1]], "values": [0,0,0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 3, "edges": [[1,0],[2,0]], "values": [1,1,2], "k": 2 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 3, "edges": [[1,2],[2,0]], "values": [0,2,2], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 3, "edges": [[1,0],[0,2]], "values": [0,1,2], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 1
test_input = { "n": 4, "edges": [[0,1],[1,2],[2,3]], "values": [0,0,0,0], "k": 9999999 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 9, "edges": [[1,2],[1,7],[0,6],[0,8],[0,3],[3,4],[0,5],[2,5]], "values": [1,4,4,0,2,1,1,6,2], "k": 7 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 9, "edges": [[5,0],[5,1],[1,6],[1,7],[5,8],[0,3],[2,4],[5,2]], "values": [3,0,10,0,6,1,1,3,0], "k": 8 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 6, "edges": [[5,0],[1,4],[4,3],[4,2],[5,4]], "values": [1,2,2,2,0,2], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 4, "edges": [[0,3],[1,2],[0,2]], "values": [12,6,0,18], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 10, "edges": [[8,7],[8,3],[7,6],[6,2],[6,4],[3,9],[4,1],[6,0],[2,5]], "values": [2,2,2,0,1,3,1,0,3,1], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 10
test_input = { "n": 8, "edges": [[0,4],[4,1],[0,3],[1,2],[0,5],[5,7],[1,6]], "values": [2,6,2,2,2,0,0,0], "k": 7 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 9, "edges": [[1,5],[5,2],[1,8],[2,0],[2,6],[1,7],[6,4],[7,3]], "values": [8,8,12,12,8,8,8,8,4], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 9
test_input = { "n": 7, "edges": [[0,3],[3,2],[3,5],[0,6],[0,1],[6,4]], "values": [12,6,6,12,18,18,12], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 7
test_input = { "n": 4, "edges": [[1,3],[0,2],[1,0]], "values": [2,6,1,9], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 5, "edges": [[2,0],[0,1],[2,3],[2,4]], "values": [0,2,10,0,18], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 6, "edges": [[3,5],[3,0],[5,2],[5,4],[3,1]], "values": [3,3,0,18,0,0], "k": 8 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 10, "edges": [[9,5],[4,9],[5,8],[3,6],[8,6],[0,1],[9,0],[6,2],[3,7]], "values": [10,14,12,4,12,1,8,36,12,11], "k": 12 }
assert my_solution.maxKDivisibleComponents(**test_input) == 7
test_input = { "n": 7, "edges": [[3,0],[0,4],[2,6],[3,6],[2,1],[1,5]], "values": [5,36,21,7,36,36,15], "k": 12 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 5, "edges": [[2,3],[2,0],[2,4],[3,1]], "values": [3,0,3,15,3], "k": 8 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 10, "edges": [[9,1],[1,7],[7,3],[3,6],[1,8],[9,4],[3,0],[3,5],[9,2]], "values": [9,9,18,9,9,18,9,9,18,27], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 10
test_input = { "n": 4, "edges": [[2,1],[2,0],[2,3]], "values": [2,0,8,10], "k": 5 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 5, "edges": [[2,4],[4,0],[0,1],[0,3]], "values": [10,20,10,30,30], "k": 10 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 10, "edges": [[5,6],[5,4],[5,1],[5,0],[1,7],[0,8],[0,2],[8,9],[3,8]], "values": [4,0,2,9,2,8,0,2,0,0], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 9, "edges": [[5,6],[5,1],[6,0],[6,8],[8,2],[7,3],[8,3],[8,4]], "values": [33,11,33,6,11,11,33,16,33], "k": 11 }
assert my_solution.maxKDivisibleComponents(**test_input) == 8
test_input = { "n": 4, "edges": [[3,0],[0,2],[3,1]], "values": [8,8,12,4], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 8, "edges": [[1,6],[5,7],[6,7],[3,2],[3,4],[4,0],[1,2]], "values": [6,6,6,6,3,8,15,6], "k": 7 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 9, "edges": [[7,8],[4,6],[8,6],[3,0],[3,5],[5,1],[1,2],[7,0]], "values": [5,0,1,1,1,3,9,30,10], "k": 10 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 4, "edges": [[2,1],[1,0],[2,3]], "values": [1,0,1,2], "k": 2 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 4, "edges": [[1,2],[1,0],[2,3]], "values": [2,1,0,1], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 4, "edges": [[2,1],[2,3],[2,0]], "values": [3,6,9,6], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 7, "edges": [[0,4],[0,1],[4,6],[4,3],[6,5],[2,4]], "values": [2,0,2,0,0,0,0], "k": 2 }
assert my_solution.maxKDivisibleComponents(**test_input) == 7
test_input = { "n": 9, "edges": [[6,0],[1,2],[6,2],[2,3],[3,7],[3,8],[0,5],[3,4]], "values": [1,1,0,3,3,3,1,1,3], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 9
test_input = { "n": 5, "edges": [[0,4],[0,1],[1,3],[0,2]], "values": [2,12,14,0,0], "k": 7 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 6, "edges": [[1,2],[0,5],[5,4],[0,3],[2,0]], "values": [2,2,2,0,0,2], "k": 2 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 10, "edges": [[3,1],[1,4],[4,5],[3,0],[4,7],[7,2],[0,9],[0,8],[1,6]], "values": [0,0,0,1,0,0,0,0,0,0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 10
test_input = { "n": 5, "edges": [[3,1],[0,3],[4,2],[1,2]], "values": [3,1,3,2,6], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 10, "edges": [[6,3],[3,9],[3,7],[3,4],[7,8],[8,0],[8,5],[3,2],[0,1]], "values": [0,0,0,0,0,0,2,0,0,0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 10
test_input = { "n": 5, "edges": [[4,3],[3,1],[1,2],[1,0]], "values": [8,6,0,8,2], "k": 8 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 8, "edges": [[1,3],[0,3],[7,4],[4,6],[0,7],[0,2],[4,5]], "values": [12,10,6,2,6,12,3,9], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 9, "edges": [[5,4],[2,1],[2,0],[0,7],[7,8],[5,2],[6,3],[2,3]], "values": [1,2,3,2,2,4,4,0,0], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 7, "edges": [[4,6],[4,2],[2,1],[2,5],[4,3],[0,5]], "values": [27,0,1,1,4,2,1], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 7, "edges": [[4,6],[0,2],[0,5],[2,3],[3,1],[4,2]], "values": [1,0,0,1,2,0,0], "k": 2 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 9, "edges": [[3,2],[2,1],[1,4],[4,6],[4,8],[6,0],[0,5],[5,7]], "values": [10,10,15,5,5,15,15,10,15], "k": 5 }
assert my_solution.maxKDivisibleComponents(**test_input) == 9
test_input = { "n": 10, "edges": [[9,6],[9,0],[9,3],[3,8],[6,5],[0,7],[8,2],[6,4],[4,1]], "values": [0,0,3,0,0,0,3,0,3,3], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 8, "edges": [[4,3],[3,6],[6,1],[6,7],[1,2],[7,0],[4,5]], "values": [30,10,10,20,10,10,30,10], "k": 10 }
assert my_solution.maxKDivisibleComponents(**test_input) == 8
test_input = { "n": 4, "edges": [[2,1],[1,0],[0,3]], "values": [9,6,6,9], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 5, "edges": [[3,4],[3,2],[4,0],[2,1]], "values": [3,2,2,3,1], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 5, "edges": [[1,3],[3,4],[4,2],[2,0]], "values": [27,14,0,0,4], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 4, "edges": [[1,2],[2,0],[0,3]], "values": [3,1,2,3], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 7, "edges": [[6,1],[6,4],[1,3],[3,0],[2,5],[4,2]], "values": [33,18,7,22,11,4,4], "k": 11 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 10, "edges": [[6,2],[5,2],[0,3],[0,7],[0,1],[6,7],[2,4],[8,9],[5,9]], "values": [3,0,1,0,3,3,2,0,6,3], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 9
test_input = { "n": 8, "edges": [[6,1],[0,5],[5,7],[5,2],[5,4],[1,4],[0,3]], "values": [15,24,9,12,0,3,24,9], "k": 12 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 6, "edges": [[3,1],[3,5],[1,4],[1,0],[3,2]], "values": [12,12,36,12,24,36], "k": 12 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 7, "edges": [[6,4],[6,5],[3,1],[1,0],[0,2],[4,2]], "values": [0,0,0,1,0,0,1], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 7
test_input = { "n": 8, "edges": [[6,1],[2,0],[0,7],[1,2],[4,5],[7,5],[1,3]], "values": [1,14,5,21,18,3,7,1], "k": 7 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 4, "edges": [[1,3],[1,0],[1,2]], "values": [18,18,9,27], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 5, "edges": [[1,2],[1,3],[3,4],[1,0]], "values": [2,3,0,0,0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 5, "edges": [[2,3],[2,4],[4,1],[2,0]], "values": [0,0,1,0,0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 6, "edges": [[1,4],[1,3],[1,2],[4,0],[2,5]], "values": [0,3,0,0,0,0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 6, "edges": [[4,1],[0,2],[4,2],[3,5],[1,5]], "values": [3,9,6,3,9,6], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 4, "edges": [[1,3],[0,2],[1,2]], "values": [4,1,0,3], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 7, "edges": [[4,5],[5,2],[2,0],[0,6],[5,3],[2,1]], "values": [15,15,10,5,2,8,5], "k": 5 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 5, "edges": [[2,4],[2,0],[0,3],[2,1]], "values": [36,24,10,24,2], "k": 12 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 7, "edges": [[2,4],[2,1],[2,5],[1,3],[4,0],[1,6]], "values": [2,0,2,2,0,2,0], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 4, "edges": [[3,2],[2,1],[2,0]], "values": [6,2,2,6], "k": 8 }
assert my_solution.maxKDivisibleComponents(**test_input) == 1
test_input = { "n": 10, "edges": [[7,4],[7,2],[4,1],[6,5],[6,0],[7,6],[1,8],[9,3],[7,9]], "values": [12,4,3,2,1,18,3,3,22,20], "k": 11 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 5, "edges": [[0,3],[3,2],[3,1],[0,4]], "values": [12,36,36,36,12], "k": 12 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 5, "edges": [[0,1],[1,2],[2,3],[3,4]], "values": [3,0,0,18,3], "k": 8 }
assert my_solution.maxKDivisibleComponents(**test_input) == 1
test_input = { "n": 4, "edges": [[3,2],[3,0],[3,1]], "values": [9,0,15,3], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 5, "edges": [[0,2],[0,3],[3,4],[1,3]], "values": [3,3,0,0,0], "k": 1 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 5, "edges": [[1,4],[1,3],[0,3],[1,2]], "values": [10,2,15,2,1], "k": 5 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 6, "edges": [[1,4],[4,5],[0,3],[1,0],[0,2]], "values": [6,9,6,3,6,3], "k": 3 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 6, "edges": [[5,1],[0,1],[2,4],[2,3],[1,4]], "values": [21,4,3,1,3,3], "k": 7 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 6, "edges": [[5,1],[1,2],[5,3],[1,4],[2,0]], "values": [0,4,0,2,0,2], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 10, "edges": [[3,6],[3,4],[6,0],[3,9],[5,7],[6,5],[0,2],[6,1],[9,8]], "values": [0,12,12,3,9,8,3,4,12,3], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 7, "edges": [[0,2],[2,5],[2,3],[5,4],[2,1],[3,6]], "values": [9,9,3,6,3,0,0], "k": 10 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 6, "edges": [[4,1],[4,5],[4,2],[4,0],[3,1]], "values": [0,6,6,22,2,8], "k": 11 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 8, "edges": [[2,7],[1,4],[2,4],[4,3],[2,5],[7,0],[7,6]], "values": [12,3,12,4,1,8,12,12], "k": 4 }
assert my_solution.maxKDivisibleComponents(**test_input) == 7
test_input = { "n": 5, "edges": [[0,3],[0,1],[0,2],[0,4]], "values": [3,3,0,18,0], "k": 8 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 4, "edges": [[2,0],[2,1],[0,3]], "values": [10,15,5,15], "k": 5 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 7, "edges": [[2,5],[5,1],[5,4],[1,6],[4,3],[3,0]], "values": [0,0,6,2,0,2,2], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 8, "edges": [[5,2],[2,4],[5,1],[5,3],[1,0],[2,6],[2,7]], "values": [18,9,9,9,27,9,9,18], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 8
test_input = { "n": 7, "edges": [[5,6],[6,4],[5,2],[6,3],[1,0],[5,1]], "values": [9,21,6,20,2,8,4], "k": 10 }
assert my_solution.maxKDivisibleComponents(**test_input) == 3
test_input = { "n": 8, "edges": [[3,1],[3,0],[0,4],[3,5],[7,6],[1,6],[6,2]], "values": [10,10,5,15,5,15,3,2], "k": 5 }
assert my_solution.maxKDivisibleComponents(**test_input) == 7
test_input = { "n": 5, "edges": [[2,4],[2,1],[2,0],[0,3]], "values": [1,2,1,0,2], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 8, "edges": [[1,0],[0,2],[0,6],[2,5],[1,3],[2,4],[2,7]], "values": [0,3,18,9,0,0,0,3], "k": 11 }
assert my_solution.maxKDivisibleComponents(**test_input) == 4
test_input = { "n": 9, "edges": [[5,0],[2,4],[0,2],[2,3],[4,1],[2,7],[8,6],[0,8]], "values": [10,10,2,20,18,20,6,10,24], "k": 10 }
assert my_solution.maxKDivisibleComponents(**test_input) == 7
test_input = { "n": 10, "edges": [[0,9],[9,7],[1,4],[0,4],[8,2],[4,2],[2,3],[3,5],[0,6]], "values": [6,4,6,18,8,18,12,18,6,12], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 9
test_input = { "n": 7, "edges": [[4,2],[6,4],[5,3],[3,1],[4,5],[2,0]], "values": [9,3,0,9,9,15,18], "k": 9 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 5, "edges": [[2,3],[3,1],[3,0],[1,4]], "values": [0,0,3,0,3], "k": 2 }
assert my_solution.maxKDivisibleComponents(**test_input) == 2
test_input = { "n": 10, "edges": [[3,6],[4,3],[0,8],[0,5],[8,9],[0,1],[8,7],[5,2],[3,8]], "values": [6,6,0,4,7,0,3,0,3,6], "k": 7 }
assert my_solution.maxKDivisibleComponents(**test_input) == 6
test_input = { "n": 6, "edges": [[2,3],[4,5],[4,1],[2,4],[2,0]], "values": [20,0,30,30,12,8], "k": 10 }
assert my_solution.maxKDivisibleComponents(**test_input) == 5
test_input = { "n": 9, "edges": [[5,7],[5,4],[5,6],[7,2],[7,1],[4,3],[2,0],[3,8]], "values": [6,6,6,18,6,12,18,6,6], "k": 6 }
assert my_solution.maxKDivisibleComponents(**test_input) == 9 | 1,696,084,200 |
weekly-contest-364-maximum-odd-binary-number | https://leetcode.com/problems/maximum-odd-binary-number | maximum-odd-binary-number | {
"questionId": "3055",
"questionFrontendId": "2864",
"title": "Maximum Odd Binary Number",
"titleSlug": "maximum-odd-binary-number",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 149,
"dislikes": 1,
"categoryTitle": "Algorithms"
} | """
给你一个 二进制 字符串 s ,其中至少包含一个 '1' 。
你必须按某种方式 重新排列 字符串中的位,使得到的二进制数字是可以由该组合生成的 最大二进制奇数 。
以字符串形式,表示并返回可以由给定组合生成的最大二进制奇数。
注意 返回的结果字符串 可以 含前导零。
示例 1:
输入:s = "010"
输出:"001"
解释:因为字符串 s 中仅有一个 '1' ,其必须出现在最后一位上。所以答案是 "001" 。
示例 2:
输入:s = "0101"
输出:"1001"
解释:其中一个 '1' 必须出现在最后一位上。而由剩下的数字可以生产的最大数字是 "100" 。所以答案是 "1001" 。
提示:
* 1 <= s.length <= 100
* s 仅由 '0' 和 '1' 组成
* s 中至少包含一个 '1'
"""
class Solution:
def maximumOddBinaryNumber(self, s: str) -> str:
| 给你一个 二进制 字符串 s ,其中至少包含一个 '1' 。
你必须按某种方式 重新排列 字符串中的位,使得到的二进制数字是可以由该组合生成的 最大二进制奇数 。
以字符串形式,表示并返回可以由给定组合生成的最大二进制奇数。
注意 返回的结果字符串 可以 含前导零。
示例 1:
输入:s = "010"
输出:"001"
解释:因为字符串 s 中仅有一个 '1' ,其必须出现在最后一位上。所以答案是 "001" 。
示例 2:
输入:s = "0101"
输出:"1001"
解释:其中一个 '1' 必须出现在最后一位上。而由剩下的数字可以生产的最大数字是 "100" 。所以答案是 "1001" 。
提示:
* 1 <= s.length <= 100
* s 仅由 '0' 和 '1' 组成
* s 中至少包含一个 '1'
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumOddBinaryNumber(self, s: str) -> str:
``` |
my_solution = Solution()
test_input = { "s": "010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "001"
test_input = { "s": "0101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1001"
test_input = { "s": "1" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1"
test_input = { "s": "01" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "01"
test_input = { "s": "10" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "01"
test_input = { "s": "11" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11"
test_input = { "s": "001" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "001"
test_input = { "s": "011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "101"
test_input = { "s": "100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "001"
test_input = { "s": "101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "101"
test_input = { "s": "110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "101"
test_input = { "s": "111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111"
test_input = { "s": "0010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "0001"
test_input = { "s": "0011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1001"
test_input = { "s": "0100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "0001"
test_input = { "s": "0111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1101"
test_input = { "s": "1000" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "0001"
test_input = { "s": "1001" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1001"
test_input = { "s": "1011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1101"
test_input = { "s": "1100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1001"
test_input = { "s": "1101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1101"
test_input = { "s": "1111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111"
test_input = { "s": "00001" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "00001"
test_input = { "s": "00011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "10001"
test_input = { "s": "00100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "00001"
test_input = { "s": "00101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "10001"
test_input = { "s": "00111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11001"
test_input = { "s": "01011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11001"
test_input = { "s": "01100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "10001"
test_input = { "s": "01110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11001"
test_input = { "s": "01111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11101"
test_input = { "s": "10000" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "00001"
test_input = { "s": "10100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "10001"
test_input = { "s": "11010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11001"
test_input = { "s": "11011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11101"
test_input = { "s": "11110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "11101"
test_input = { "s": "000001" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "000001"
test_input = { "s": "000100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "000001"
test_input = { "s": "000101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "100001"
test_input = { "s": "000110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "100001"
test_input = { "s": "000111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "001100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "100001"
test_input = { "s": "001110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "001111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "010001" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "100001"
test_input = { "s": "010101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "010111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "011010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "011100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "011111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111101"
test_input = { "s": "100100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "100001"
test_input = { "s": "100101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "100111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "101010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "101100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "101101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "110000" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "100001"
test_input = { "s": "110010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "110011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "110100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "110101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "110110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "111000" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "110001"
test_input = { "s": "111010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111001"
test_input = { "s": "111101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111101"
test_input = { "s": "111111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "111111"
test_input = { "s": "0000010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "0000001"
test_input = { "s": "0000100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "0000001"
test_input = { "s": "0001001" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1000001"
test_input = { "s": "0001110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "0010000" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "0000001"
test_input = { "s": "0010101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "0010110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "0010111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "0011101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "0011111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001"
test_input = { "s": "0100100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1000001"
test_input = { "s": "0101001" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "0110010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "0110110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "0110111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001"
test_input = { "s": "0111011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001"
test_input = { "s": "0111101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001"
test_input = { "s": "1000011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "1000100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1000001"
test_input = { "s": "1000101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "1000111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "1001100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "1001101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "1010000" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1000001"
test_input = { "s": "1010100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "1010111" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001"
test_input = { "s": "1011000" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "1011010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "1011011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001"
test_input = { "s": "1100100" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1100001"
test_input = { "s": "1100101" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "1101010" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1110001"
test_input = { "s": "1101011" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001"
test_input = { "s": "1101110" }
assert my_solution.maximumOddBinaryNumber(**test_input) == "1111001" | 1,695,522,600 |
weekly-contest-364-beautiful-towers-i | https://leetcode.com/problems/beautiful-towers-i | beautiful-towers-i | {
"questionId": "3114",
"questionFrontendId": "2865",
"title": "Beautiful Towers I",
"titleSlug": "beautiful-towers-i",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 160,
"dislikes": 26,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 下标从 0 开始的整数数组 maxHeights 。
你的任务是在坐标轴上建 n 座塔。第 i 座塔的下标为 i ,高度为 heights[i] 。
如果以下条件满足,我们称这些塔是 美丽 的:
1. 1 <= heights[i] <= maxHeights[i]
2. heights 是一个 山状 数组。
如果存在下标 i 满足以下条件,那么我们称数组 heights 是一个 山状 数组:
* 对于所有 0 < j <= i ,都有 heights[j - 1] <= heights[j]
* 对于所有 i <= k < n - 1 ,都有 heights[k + 1] <= heights[k]
请你返回满足 美丽塔 要求的方案中,高度和的最大值 。
示例 1:
输入:maxHeights = [5,3,4,1,1]
输出:13
解释:和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 0 处。
13 是所有美丽塔方案中的最大高度和。
示例 2:
输入:maxHeights = [6,5,3,9,2,7]
输出:22
解释: 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 3 处。
22 是所有美丽塔方案中的最大高度和。
示例 3:
输入:maxHeights = [3,2,5,5,2,3]
输出:18
解释:和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,最大值在 i = 2 处。
注意,在这个方案中,i = 3 也是一个峰值。
18 是所有美丽塔方案中的最大高度和。
提示:
* 1 <= n == maxHeights <= 103
* 1 <= maxHeights[i] <= 109
"""
class Solution:
def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
| 给你一个长度为 n 下标从 0 开始的整数数组 maxHeights 。
你的任务是在坐标轴上建 n 座塔。第 i 座塔的下标为 i ,高度为 heights[i] 。
如果以下条件满足,我们称这些塔是 美丽 的:
1. 1 <= heights[i] <= maxHeights[i]
2. heights 是一个 山状 数组。
如果存在下标 i 满足以下条件,那么我们称数组 heights 是一个 山状 数组:
* 对于所有 0 < j <= i ,都有 heights[j - 1] <= heights[j]
* 对于所有 i <= k < n - 1 ,都有 heights[k + 1] <= heights[k]
请你返回满足 美丽塔 要求的方案中,高度和的最大值 。
示例 1:
输入:maxHeights = [5,3,4,1,1]
输出:13
解释:和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 0 处。
13 是所有美丽塔方案中的最大高度和。
示例 2:
输入:maxHeights = [6,5,3,9,2,7]
输出:22
解释: 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 3 处。
22 是所有美丽塔方案中的最大高度和。
示例 3:
输入:maxHeights = [3,2,5,5,2,3]
输出:18
解释:和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,最大值在 i = 2 处。
注意,在这个方案中,i = 3 也是一个峰值。
18 是所有美丽塔方案中的最大高度和。
提示:
* 1 <= n == maxHeights <= 103
* 1 <= maxHeights[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "maxHeights": [5,3,4,1,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [6,5,3,9,2,7] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [3,2,5,5,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 1000000000
test_input = { "maxHeights": [1] }
assert my_solution.maximumSumOfHeights(**test_input) == 1
test_input = { "maxHeights": [933754743] }
assert my_solution.maximumSumOfHeights(**test_input) == 933754743
test_input = { "maxHeights": [1,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 1000000001
test_input = { "maxHeights": [1000000000,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 2000000000
test_input = { "maxHeights": [999999999,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 1999999999
test_input = { "maxHeights": [1000000000,999999999] }
assert my_solution.maximumSumOfHeights(**test_input) == 1999999999
test_input = { "maxHeights": [30,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 31
test_input = { "maxHeights": [1,12,19] }
assert my_solution.maximumSumOfHeights(**test_input) == 32
test_input = { "maxHeights": [1000000000,1000000000,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 3000000000
test_input = { "maxHeights": [999999999,1000000000,999999999] }
assert my_solution.maximumSumOfHeights(**test_input) == 2999999998
test_input = { "maxHeights": [1000000000,999999999,999999998] }
assert my_solution.maximumSumOfHeights(**test_input) == 2999999997
test_input = { "maxHeights": [999999998,999999999,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 2999999997
test_input = { "maxHeights": [1,1,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 3
test_input = { "maxHeights": [1,1,4,3,3,3,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [2,4,1,3,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 11
test_input = { "maxHeights": [1,5,2,5,6,4,6,3,4,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 33
test_input = { "maxHeights": [3,6,3,5,5,1,2,5,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 24
test_input = { "maxHeights": [1,6,5,6,2,4,1,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 23
test_input = { "maxHeights": [5,1,6,5,4,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 23
test_input = { "maxHeights": [3,4,3,1,1,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [4,1,6,5,3,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [3,5,5,6,4,6,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 35
test_input = { "maxHeights": [6,4,3,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [6,4,3,6,1,2,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [6,5,1,4,6,1,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [2,3,4,4,3,2,3,5,5,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 30
test_input = { "maxHeights": [5,4,6,1,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [1,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 7
test_input = { "maxHeights": [5,2,4,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 12
test_input = { "maxHeights": [1,5,5,3,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 17
test_input = { "maxHeights": [3,1,1,4,5,5,4,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [1,4,3,4,5,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 17
test_input = { "maxHeights": [5,5,3,1,1,2,5,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [3,1,3,2,6,1,4,4,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [5,3,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 11
test_input = { "maxHeights": [5,2,1,4,3,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [1,3,2,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 7
test_input = { "maxHeights": [1,3,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 10
test_input = { "maxHeights": [5,5,5,3,3,3,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 27
test_input = { "maxHeights": [1,3,3,2,1,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 11
test_input = { "maxHeights": [5,5,4,1,4,4,5,6,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 27
test_input = { "maxHeights": [3,5,5,6,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 21
test_input = { "maxHeights": [4,6,6,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 17
test_input = { "maxHeights": [4,2,6,1,4,1,5,3,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [4,1,6,3,6,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [5,2,1,4,1,6,1,5,3,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [1,4,6,3,5,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [6,1,2,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 9
test_input = { "maxHeights": [6,1,5,1,6,2,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [6,1,2,3,4,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 15
test_input = { "maxHeights": [6,1,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 8
test_input = { "maxHeights": [1,6,6,3,5,6,1,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 24
test_input = { "maxHeights": [2,6,1,5,1,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [3,5,1,6,3,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 15
test_input = { "maxHeights": [4,4,5,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [3,5,4,4,3,1,1,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [5,6,4,4,5,1,2,3,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 28
test_input = { "maxHeights": [2,5,1,5,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [1,2,6,2,6,5,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 23
test_input = { "maxHeights": [1,1,6,4,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [3,4,1,6,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 11
test_input = { "maxHeights": [1,3,3,5,6,4,6,5,2,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 34
test_input = { "maxHeights": [2,4,6,4,6,3,1,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 26
test_input = { "maxHeights": [1,6,1,6,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [4,2,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 7
test_input = { "maxHeights": [1,2,4,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 8
test_input = { "maxHeights": [1,3,3,2,5,1,4,3,1,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 17
test_input = { "maxHeights": [4,3,1,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 9
test_input = { "maxHeights": [5,1,3,4,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [1,5,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 12
test_input = { "maxHeights": [2,6,4,3,2,2,2,5,6,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 30
test_input = { "maxHeights": [2,6,1,2,1,1,2,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [1,2,5,3,3,3,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [2,5,2,2,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 12
test_input = { "maxHeights": [3,5,3,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [4,6,6,3,4,1,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 24
test_input = { "maxHeights": [2,3,5,3,4,1,1,1,3,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 21
test_input = { "maxHeights": [6,6,5,3,5,4,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 29
test_input = { "maxHeights": [5,5,2,2,4,2,3,2,4,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 26
test_input = { "maxHeights": [1,2,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 8
test_input = { "maxHeights": [3,6,2,4,5,2,2,5,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [6,2,6,3,4,6,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [6,2,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 10
test_input = { "maxHeights": [5,4,2,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [2,6,5,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [4,5,4,1,6,5,1,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [4,1,2,4,6,2,6,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [6,4,4,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [5,5,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [5,3,3,4,2,2,1,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [6,5,6,6,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 28
test_input = { "maxHeights": [6,3,5,6,2,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 23
test_input = { "maxHeights": [4,6,4,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [3,5,4,2,1,1,5,6,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [2,3,5,6,2,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [4,5,2,1,4,6,6,1,6,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 23 | 1,695,522,600 |
weekly-contest-364-beautiful-towers-ii | https://leetcode.com/problems/beautiful-towers-ii | beautiful-towers-ii | {
"questionId": "3113",
"questionFrontendId": "2866",
"title": "Beautiful Towers II",
"titleSlug": "beautiful-towers-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 348,
"dislikes": 22,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 下标从 0 开始的整数数组 maxHeights 。
你的任务是在坐标轴上建 n 座塔。第 i 座塔的下标为 i ,高度为 heights[i] 。
如果以下条件满足,我们称这些塔是 美丽 的:
1. 1 <= heights[i] <= maxHeights[i]
2. heights 是一个 山状 数组。
如果存在下标 i 满足以下条件,那么我们称数组 heights 是一个 山状 数组:
* 对于所有 0 < j <= i ,都有 heights[j - 1] <= heights[j]
* 对于所有 i <= k < n - 1 ,都有 heights[k + 1] <= heights[k]
请你返回满足 美丽塔 要求的方案中,高度和的最大值 。
示例 1:
输入:maxHeights = [5,3,4,1,1]
输出:13
解释:和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 0 处。
13 是所有美丽塔方案中的最大高度和。
示例 2:
输入:maxHeights = [6,5,3,9,2,7]
输出:22
解释: 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 3 处。
22 是所有美丽塔方案中的最大高度和。
示例 3:
输入:maxHeights = [3,2,5,5,2,3]
输出:18
解释:和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,最大值在 i = 2 处。
注意,在这个方案中,i = 3 也是一个峰值。
18 是所有美丽塔方案中的最大高度和。
提示:
* 1 <= n == maxHeights <= 105
* 1 <= maxHeights[i] <= 109
"""
class Solution:
def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
| 给你一个长度为 n 下标从 0 开始的整数数组 maxHeights 。
你的任务是在坐标轴上建 n 座塔。第 i 座塔的下标为 i ,高度为 heights[i] 。
如果以下条件满足,我们称这些塔是 美丽 的:
1. 1 <= heights[i] <= maxHeights[i]
2. heights 是一个 山状 数组。
如果存在下标 i 满足以下条件,那么我们称数组 heights 是一个 山状 数组:
* 对于所有 0 < j <= i ,都有 heights[j - 1] <= heights[j]
* 对于所有 i <= k < n - 1 ,都有 heights[k + 1] <= heights[k]
请你返回满足 美丽塔 要求的方案中,高度和的最大值 。
示例 1:
输入:maxHeights = [5,3,4,1,1]
输出:13
解释:和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 0 处。
13 是所有美丽塔方案中的最大高度和。
示例 2:
输入:maxHeights = [6,5,3,9,2,7]
输出:22
解释: 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,峰值在 i = 3 处。
22 是所有美丽塔方案中的最大高度和。
示例 3:
输入:maxHeights = [3,2,5,5,2,3]
输出:18
解释:和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
- 1 <= heights[i] <= maxHeights[i]
- heights 是个山状数组,最大值在 i = 2 处。
注意,在这个方案中,i = 3 也是一个峰值。
18 是所有美丽塔方案中的最大高度和。
提示:
* 1 <= n == maxHeights <= 105
* 1 <= maxHeights[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "maxHeights": [5,3,4,1,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [6,5,3,9,2,7] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [3,2,5,5,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 1000000000
test_input = { "maxHeights": [1] }
assert my_solution.maximumSumOfHeights(**test_input) == 1
test_input = { "maxHeights": [352939501] }
assert my_solution.maximumSumOfHeights(**test_input) == 352939501
test_input = { "maxHeights": [1,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 1000000001
test_input = { "maxHeights": [1000000000,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 2000000000
test_input = { "maxHeights": [999999999,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 1999999999
test_input = { "maxHeights": [1000000000,999999999] }
assert my_solution.maximumSumOfHeights(**test_input) == 1999999999
test_input = { "maxHeights": [2,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 3
test_input = { "maxHeights": [26,30,30] }
assert my_solution.maximumSumOfHeights(**test_input) == 86
test_input = { "maxHeights": [1000000000,1000000000,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 3000000000
test_input = { "maxHeights": [999999999,1000000000,999999999] }
assert my_solution.maximumSumOfHeights(**test_input) == 2999999998
test_input = { "maxHeights": [1000000000,999999999,999999998] }
assert my_solution.maximumSumOfHeights(**test_input) == 2999999997
test_input = { "maxHeights": [999999998,999999999,1000000000] }
assert my_solution.maximumSumOfHeights(**test_input) == 2999999997
test_input = { "maxHeights": [1,1,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 3
test_input = { "maxHeights": [1,1,5,6,2,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [3,5,3,5,1,5,4,4,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [4,2,4,2,1,5,4,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [5,3,2,4,6,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 21
test_input = { "maxHeights": [6,5,2,1,5,4,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [4,3,2,4,6,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [2,6,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [6,5,3,4,6,1,2,3,2,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [2,1,6,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 12
test_input = { "maxHeights": [6,5,1,5,4,3,4,2,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [1,1,6,4,2,2,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [2,2,2,5,6,1,4,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [3,1,5,3,6,5,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [1,4,6,1,4,6,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 15
test_input = { "maxHeights": [4,4,2,3,3,2,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [4,4,1,6,2,1,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [6,3,3,5,4,1,5,2,4,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 23
test_input = { "maxHeights": [3,2,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 8
test_input = { "maxHeights": [3,1,4,3,2,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [3,1,4,5,4,4,6,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 28
test_input = { "maxHeights": [1,2,5,5,5,6,5,6,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 37
test_input = { "maxHeights": [6,1,1,6,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [1,6,6,6,2,6,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [4,1,4,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 11
test_input = { "maxHeights": [1,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 12
test_input = { "maxHeights": [1,4,4,5,6,6,2,4,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 31
test_input = { "maxHeights": [2,6,6,4,6,1,6,2,1,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 27
test_input = { "maxHeights": [2,4,6,4,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [5,2,6,1,6,6,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 21
test_input = { "maxHeights": [3,6,1,4,5,5,2,5,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [3,2,6,5,6,6,2,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 29
test_input = { "maxHeights": [1,4,2,1,6,3,2,1,2,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [3,3,4,3,5,6,1,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [4,4,2,4,1,4,5,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [6,5,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [2,2,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 8
test_input = { "maxHeights": [6,2,6,5,3,6,2,5,1,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 27
test_input = { "maxHeights": [4,6,3,4,6,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [6,4,6,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 17
test_input = { "maxHeights": [4,3,5,1,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [3,5,6,6,3,6,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 27
test_input = { "maxHeights": [6,1,6,5,5,1,1,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 21
test_input = { "maxHeights": [4,3,3,1,5,3,3,6,3,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [1,4,6,1,6,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [6,2,1,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 11
test_input = { "maxHeights": [1,4,6,4,2,6,6,5,5,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 36
test_input = { "maxHeights": [6,1,2,1,3,3,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 12
test_input = { "maxHeights": [6,4,3,3,6,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [3,1,4,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 10
test_input = { "maxHeights": [2,4,1,6,5,3,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [2,5,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [5,2,1,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 9
test_input = { "maxHeights": [5,5,3,3,3,3,1,3,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [5,6,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 16
test_input = { "maxHeights": [1,1,6,2,3,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [2,4,2,2,2,6,1,4,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 19
test_input = { "maxHeights": [6,2,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 10
test_input = { "maxHeights": [5,6,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 12
test_input = { "maxHeights": [6,6,4,3,6,3,6,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 31
test_input = { "maxHeights": [3,2,4,2,3,2,3,6,1,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 23
test_input = { "maxHeights": [6,3,3,3,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [5,5,2,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [3,5,4,5,5,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 25
test_input = { "maxHeights": [3,4,6,3,1,5,5,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 20
test_input = { "maxHeights": [4,3,6,6,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 28
test_input = { "maxHeights": [3,5,5,2,2,3,4,1,1,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 24
test_input = { "maxHeights": [5,6,1,2,5,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 15
test_input = { "maxHeights": [6,3,3,6,4,4,2,1,6,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 28
test_input = { "maxHeights": [1,5,3,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [5,2,3,5,3,2,5,5,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 28
test_input = { "maxHeights": [5,4,2,6,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [5,5,4,4,1,4,3,1,4,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 24
test_input = { "maxHeights": [4,4,4,6,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 22
test_input = { "maxHeights": [3,3,3,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [2,2,2] }
assert my_solution.maximumSumOfHeights(**test_input) == 6
test_input = { "maxHeights": [4,4,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [1,1,6,5,4,6,6,2,5] }
assert my_solution.maximumSumOfHeights(**test_input) == 30
test_input = { "maxHeights": [6,6,3,3,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 21
test_input = { "maxHeights": [1,3,2,5,2,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 13
test_input = { "maxHeights": [1,5,3,3,1,6] }
assert my_solution.maximumSumOfHeights(**test_input) == 14
test_input = { "maxHeights": [4,2,4,3] }
assert my_solution.maximumSumOfHeights(**test_input) == 11
test_input = { "maxHeights": [6,4,5,1,2,3,1] }
assert my_solution.maximumSumOfHeights(**test_input) == 18
test_input = { "maxHeights": [4,4,1,6,1,4] }
assert my_solution.maximumSumOfHeights(**test_input) == 12 | 1,695,522,600 |
weekly-contest-364-count-valid-paths-in-a-tree | https://leetcode.com/problems/count-valid-paths-in-a-tree | count-valid-paths-in-a-tree | {
"questionId": "3112",
"questionFrontendId": "2867",
"title": "Count Valid Paths in a Tree",
"titleSlug": "count-valid-paths-in-a-tree",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 212,
"dislikes": 5,
"categoryTitle": "Algorithms"
} | """
给你一棵 n 个节点的无向树,节点编号为 1 到 n 。给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ui, vi] 表示节点 ui 和 vi 在树中有一条边。
请你返回树中的 合法路径数目 。
如果在节点 a 到节点 b 之间 恰好有一个 节点的编号是质数,那么我们称路径 (a, b) 是 合法的 。
注意:
* 路径 (a, b) 指的是一条从节点 a 开始到节点 b 结束的一个节点序列,序列中的节点 互不相同 ,且相邻节点之间在树上有一条边。
* 路径 (a, b) 和路径 (b, a) 视为 同一条 路径,且只计入答案 一次 。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/27/example1.png]
输入:n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
输出:4
解释:恰好有一个质数编号的节点路径有:
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
只有 4 条合法路径。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/27/example2.png]
输入:n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
输出:6
解释:恰好有一个质数编号的节点路径有:
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
- (1, 6) 因为路径 1 到 6 只包含一个质数 3 。
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
- (3, 6) 因为路径 3 到 6 只包含一个质数 3 。
只有 6 条合法路径。
提示:
* 1 <= n <= 105
* edges.length == n - 1
* edges[i].length == 2
* 1 <= ui, vi <= n
* 输入保证 edges 形成一棵合法的树。
"""
class Solution:
def countPaths(self, n: int, edges: List[List[int]]) -> int:
| 给你一棵 n 个节点的无向树,节点编号为 1 到 n 。给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ui, vi] 表示节点 ui 和 vi 在树中有一条边。
请你返回树中的 合法路径数目 。
如果在节点 a 到节点 b 之间 恰好有一个 节点的编号是质数,那么我们称路径 (a, b) 是 合法的 。
注意:
* 路径 (a, b) 指的是一条从节点 a 开始到节点 b 结束的一个节点序列,序列中的节点 互不相同 ,且相邻节点之间在树上有一条边。
* 路径 (a, b) 和路径 (b, a) 视为 同一条 路径,且只计入答案 一次 。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/27/example1.png]
输入:n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
输出:4
解释:恰好有一个质数编号的节点路径有:
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
只有 4 条合法路径。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/27/example2.png]
输入:n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
输出:6
解释:恰好有一个质数编号的节点路径有:
- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
- (1, 6) 因为路径 1 到 6 只包含一个质数 3 。
- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
- (3, 6) 因为路径 3 到 6 只包含一个质数 3 。
只有 6 条合法路径。
提示:
* 1 <= n <= 105
* edges.length == n - 1
* edges[i].length == 2
* 1 <= ui, vi <= n
* 输入保证 edges 形成一棵合法的树。
请完成下面的代码来解决上述问题:
```python
class Solution:
def countPaths(self, n: int, edges: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "n": 5, "edges": [[1,2],[1,3],[2,4],[2,5]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 6, "edges": [[1,2],[1,3],[2,4],[3,5],[3,6]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 1, "edges": [] }
assert my_solution.countPaths(**test_input) == 0
test_input = { "n": 2, "edges": [[2,1]] }
assert my_solution.countPaths(**test_input) == 1
test_input = { "n": 4, "edges": [[1,2],[4,1],[3,4]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 5, "edges": [[1,3],[4,3],[2,3],[5,2]] }
assert my_solution.countPaths(**test_input) == 3
test_input = { "n": 5, "edges": [[1,5],[2,1],[4,5],[3,2]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 5, "edges": [[2,3],[4,2],[1,3],[5,1]] }
assert my_solution.countPaths(**test_input) == 3
test_input = { "n": 5, "edges": [[4,1],[5,4],[2,1],[3,4]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 5, "edges": [[3,5],[1,5],[4,3],[2,5]] }
assert my_solution.countPaths(**test_input) == 2
test_input = { "n": 4, "edges": [[2,1],[4,2],[3,2]] }
assert my_solution.countPaths(**test_input) == 3
test_input = { "n": 5, "edges": [[1,4],[2,4],[3,2],[5,4]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 5, "edges": [[5,4],[3,4],[1,4],[2,4]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 4, "edges": [[3,4],[1,3],[2,4]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 10, "edges": [[10,9],[2,10],[1,10],[3,2],[6,10],[4,3],[8,6],[5,8],[7,6]] }
assert my_solution.countPaths(**test_input) == 16
test_input = { "n": 8, "edges": [[7,2],[6,2],[5,2],[1,2],[4,7],[8,1],[3,6]] }
assert my_solution.countPaths(**test_input) == 7
test_input = { "n": 5, "edges": [[3,2],[4,3],[5,4],[1,4]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 9, "edges": [[7,4],[3,4],[5,4],[1,5],[6,4],[9,5],[8,7],[2,8]] }
assert my_solution.countPaths(**test_input) == 17
test_input = { "n": 9, "edges": [[1,8],[5,8],[4,8],[6,5],[3,1],[9,1],[7,4],[2,8]] }
assert my_solution.countPaths(**test_input) == 21
test_input = { "n": 10, "edges": [[2,9],[7,2],[10,9],[5,7],[4,5],[6,7],[8,2],[1,5],[3,10]] }
assert my_solution.countPaths(**test_input) == 11
test_input = { "n": 8, "edges": [[6,1],[8,1],[3,6],[4,1],[7,3],[2,8],[5,1]] }
assert my_solution.countPaths(**test_input) == 12
test_input = { "n": 10, "edges": [[9,1],[8,9],[5,9],[10,8],[7,5],[2,8],[3,8],[6,7],[4,1]] }
assert my_solution.countPaths(**test_input) == 16
test_input = { "n": 5, "edges": [[5,3],[4,5],[2,5],[1,4]] }
assert my_solution.countPaths(**test_input) == 2
test_input = { "n": 7, "edges": [[7,6],[2,6],[5,2],[3,5],[4,5],[1,6]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 8, "edges": [[4,5],[8,4],[6,4],[2,5],[1,6],[3,1],[7,8]] }
assert my_solution.countPaths(**test_input) == 12
test_input = { "n": 10, "edges": [[9,6],[5,6],[10,6],[8,5],[4,8],[3,9],[1,3],[2,6],[7,1]] }
assert my_solution.countPaths(**test_input) == 22
test_input = { "n": 5, "edges": [[1,4],[2,4],[5,4],[3,4]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 10, "edges": [[9,4],[7,9],[10,9],[6,7],[8,7],[2,7],[3,2],[1,9],[5,10]] }
assert my_solution.countPaths(**test_input) == 19
test_input = { "n": 7, "edges": [[4,7],[6,7],[1,7],[3,7],[2,6],[5,6]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 5, "edges": [[1,4],[2,1],[5,2],[3,1]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 5, "edges": [[4,5],[2,5],[1,4],[3,1]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 10, "edges": [[4,3],[5,3],[1,4],[7,5],[6,3],[8,3],[9,1],[10,8],[2,10]] }
assert my_solution.countPaths(**test_input) == 19
test_input = { "n": 5, "edges": [[1,3],[4,1],[2,1],[5,1]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 7, "edges": [[5,2],[3,5],[4,5],[1,2],[6,2],[7,2]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 5, "edges": [[3,4],[1,4],[5,3],[2,5]] }
assert my_solution.countPaths(**test_input) == 2
test_input = { "n": 8, "edges": [[1,8],[2,1],[6,8],[3,6],[4,3],[5,8],[7,2]] }
assert my_solution.countPaths(**test_input) == 13
test_input = { "n": 9, "edges": [[2,1],[6,1],[8,2],[9,8],[4,9],[7,9],[3,1],[5,2]] }
assert my_solution.countPaths(**test_input) == 16
test_input = { "n": 5, "edges": [[4,3],[5,4],[1,5],[2,3]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 8, "edges": [[2,4],[3,4],[6,4],[7,3],[8,7],[5,6],[1,5]] }
assert my_solution.countPaths(**test_input) == 10
test_input = { "n": 6, "edges": [[3,6],[1,3],[2,3],[5,2],[4,5]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 10, "edges": [[7,5],[4,7],[10,5],[6,5],[8,6],[2,8],[9,4],[3,10],[1,9]] }
assert my_solution.countPaths(**test_input) == 11
test_input = { "n": 8, "edges": [[3,2],[8,3],[7,3],[6,2],[4,7],[5,6],[1,8]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 8, "edges": [[2,6],[3,6],[5,2],[1,6],[7,2],[8,1],[4,1]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 9, "edges": [[3,7],[6,7],[4,7],[1,6],[8,4],[9,1],[2,7],[5,2]] }
assert my_solution.countPaths(**test_input) == 11
test_input = { "n": 6, "edges": [[6,2],[5,6],[1,2],[3,2],[4,3]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 6, "edges": [[6,2],[1,6],[3,1],[5,3],[4,3]] }
assert my_solution.countPaths(**test_input) == 7
test_input = { "n": 8, "edges": [[2,3],[8,3],[5,3],[6,3],[4,3],[7,8],[1,2]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 7, "edges": [[4,6],[5,6],[7,6],[3,4],[2,6],[1,5]] }
assert my_solution.countPaths(**test_input) == 11
test_input = { "n": 6, "edges": [[4,2],[5,4],[1,5],[6,1],[3,1]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 6, "edges": [[4,1],[6,1],[5,4],[3,5],[2,1]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 7, "edges": [[6,7],[1,6],[4,6],[2,7],[3,2],[5,7]] }
assert my_solution.countPaths(**test_input) == 3
test_input = { "n": 6, "edges": [[4,3],[5,3],[6,5],[2,4],[1,5]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 10, "edges": [[2,5],[3,5],[10,2],[8,3],[7,5],[4,3],[1,8],[9,5],[6,8]] }
assert my_solution.countPaths(**test_input) == 9
test_input = { "n": 8, "edges": [[2,8],[1,8],[5,2],[4,5],[6,5],[7,2],[3,5]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 8, "edges": [[7,2],[6,7],[5,7],[8,6],[1,7],[3,5],[4,2]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 7, "edges": [[5,7],[3,5],[2,7],[4,3],[6,3],[1,4]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 7, "edges": [[2,3],[1,3],[7,3],[4,1],[6,3],[5,1]] }
assert my_solution.countPaths(**test_input) == 7
test_input = { "n": 7, "edges": [[4,2],[6,4],[1,6],[7,6],[5,2],[3,7]] }
assert my_solution.countPaths(**test_input) == 6
test_input = { "n": 6, "edges": [[3,4],[6,3],[1,4],[5,4],[2,6]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 10, "edges": [[2,7],[1,2],[8,7],[4,2],[6,8],[10,7],[5,4],[9,8],[3,6]] }
assert my_solution.countPaths(**test_input) == 14
test_input = { "n": 8, "edges": [[6,2],[7,2],[5,6],[8,2],[3,6],[1,8],[4,2]] }
assert my_solution.countPaths(**test_input) == 11
test_input = { "n": 10, "edges": [[9,6],[1,9],[2,1],[8,1],[10,6],[7,6],[3,8],[4,3],[5,3]] }
assert my_solution.countPaths(**test_input) == 21
test_input = { "n": 8, "edges": [[4,6],[1,6],[7,6],[2,1],[8,7],[5,6],[3,5]] }
assert my_solution.countPaths(**test_input) == 13
test_input = { "n": 7, "edges": [[3,7],[6,7],[4,6],[1,3],[5,3],[2,3]] }
assert my_solution.countPaths(**test_input) == 3
test_input = { "n": 10, "edges": [[2,9],[3,9],[8,9],[10,3],[1,9],[4,8],[7,10],[6,1],[5,10]] }
assert my_solution.countPaths(**test_input) == 18
test_input = { "n": 8, "edges": [[5,6],[3,6],[7,5],[4,6],[8,6],[2,3],[1,3]] }
assert my_solution.countPaths(**test_input) == 10
test_input = { "n": 9, "edges": [[9,3],[4,3],[2,9],[7,3],[8,7],[1,7],[5,4],[6,2]] }
assert my_solution.countPaths(**test_input) == 10
test_input = { "n": 6, "edges": [[5,1],[2,1],[4,2],[6,5],[3,4]] }
assert my_solution.countPaths(**test_input) == 7
test_input = { "n": 7, "edges": [[3,6],[2,6],[5,6],[7,2],[1,6],[4,2]] }
assert my_solution.countPaths(**test_input) == 9
test_input = { "n": 5, "edges": [[1,5],[3,5],[4,3],[2,5]] }
assert my_solution.countPaths(**test_input) == 2
test_input = { "n": 8, "edges": [[2,8],[6,2],[1,2],[4,1],[7,2],[3,2],[5,7]] }
assert my_solution.countPaths(**test_input) == 9
test_input = { "n": 9, "edges": [[5,3],[1,3],[7,3],[4,5],[6,7],[8,7],[9,5],[2,5]] }
assert my_solution.countPaths(**test_input) == 7
test_input = { "n": 10, "edges": [[8,10],[3,10],[1,8],[7,8],[6,10],[2,3],[5,8],[9,5],[4,6]] }
assert my_solution.countPaths(**test_input) == 21
test_input = { "n": 10, "edges": [[4,8],[6,8],[9,4],[1,6],[5,8],[2,8],[10,9],[7,6],[3,5]] }
assert my_solution.countPaths(**test_input) == 18
test_input = { "n": 9, "edges": [[4,6],[9,4],[3,6],[7,6],[1,4],[5,9],[2,7],[8,4]] }
assert my_solution.countPaths(**test_input) == 15
test_input = { "n": 7, "edges": [[7,6],[2,6],[1,7],[3,2],[5,2],[4,5]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 9, "edges": [[1,8],[5,8],[3,8],[2,1],[9,2],[7,1],[6,5],[4,9]] }
assert my_solution.countPaths(**test_input) == 17
test_input = { "n": 7, "edges": [[3,5],[4,3],[6,5],[2,4],[7,3],[1,4]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 10, "edges": [[5,2],[8,5],[10,5],[3,8],[4,2],[1,4],[9,10],[7,5],[6,8]] }
assert my_solution.countPaths(**test_input) == 12
test_input = { "n": 8, "edges": [[7,4],[1,4],[5,7],[6,1],[8,5],[3,8],[2,6]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 10, "edges": [[8,2],[7,8],[9,2],[4,2],[6,4],[3,9],[5,4],[1,9],[10,8]] }
assert my_solution.countPaths(**test_input) == 24
test_input = { "n": 8, "edges": [[3,8],[2,8],[1,8],[6,8],[4,3],[7,6],[5,2]] }
assert my_solution.countPaths(**test_input) == 13
test_input = { "n": 9, "edges": [[5,4],[2,4],[9,5],[3,2],[7,3],[1,4],[8,2],[6,4]] }
assert my_solution.countPaths(**test_input) == 14
test_input = { "n": 10, "edges": [[10,8],[5,10],[1,10],[3,10],[7,3],[2,10],[6,8],[9,6],[4,6]] }
assert my_solution.countPaths(**test_input) == 18
test_input = { "n": 9, "edges": [[6,7],[5,7],[8,7],[9,6],[2,5],[4,5],[1,8],[3,6]] }
assert my_solution.countPaths(**test_input) == 11
test_input = { "n": 10, "edges": [[1,6],[9,1],[5,9],[10,6],[4,9],[8,9],[7,8],[2,1],[3,1]] }
assert my_solution.countPaths(**test_input) == 24
test_input = { "n": 8, "edges": [[4,5],[3,4],[6,5],[1,4],[2,3],[8,4],[7,3]] }
assert my_solution.countPaths(**test_input) == 10
test_input = { "n": 7, "edges": [[7,4],[6,4],[2,6],[5,6],[3,2],[1,5]] }
assert my_solution.countPaths(**test_input) == 9
test_input = { "n": 10, "edges": [[3,2],[5,3],[1,2],[9,5],[7,5],[6,3],[4,3],[8,6],[10,1]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 8, "edges": [[2,8],[5,2],[4,2],[7,4],[3,2],[1,3],[6,3]] }
assert my_solution.countPaths(**test_input) == 7
test_input = { "n": 6, "edges": [[6,3],[2,3],[4,2],[5,2],[1,6]] }
assert my_solution.countPaths(**test_input) == 3
test_input = { "n": 9, "edges": [[7,3],[5,3],[4,3],[9,7],[6,5],[8,3],[2,4],[1,5]] }
assert my_solution.countPaths(**test_input) == 8
test_input = { "n": 5, "edges": [[1,2],[5,1],[4,5],[3,4]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 9, "edges": [[8,3],[6,8],[2,8],[4,3],[5,4],[7,5],[9,4],[1,5]] }
assert my_solution.countPaths(**test_input) == 15
test_input = { "n": 5, "edges": [[2,1],[4,2],[5,1],[3,2]] }
assert my_solution.countPaths(**test_input) == 4
test_input = { "n": 6, "edges": [[4,2],[6,2],[3,2],[1,6],[5,2]] }
assert my_solution.countPaths(**test_input) == 5
test_input = { "n": 10, "edges": [[10,5],[3,10],[4,3],[8,3],[7,4],[1,7],[6,5],[9,1],[2,5]] }
assert my_solution.countPaths(**test_input) == 14
test_input = { "n": 9, "edges": [[4,7],[6,4],[8,4],[2,8],[5,2],[9,7],[1,6],[3,7]] }
assert my_solution.countPaths(**test_input) == 13
test_input = { "n": 10, "edges": [[7,3],[1,3],[2,3],[10,2],[4,2],[8,1],[9,4],[5,3],[6,3]] }
assert my_solution.countPaths(**test_input) == 10
test_input = { "n": 7, "edges": [[4,7],[2,7],[3,4],[1,7],[6,4],[5,4]] }
assert my_solution.countPaths(**test_input) == 9 | 1,695,522,600 |
weekly-contest-363-sum-of-values-at-indices-with-k-set-bits | https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits | sum-of-values-at-indices-with-k-set-bits | {
"questionId": "3093",
"questionFrontendId": "2859",
"title": "Sum of Values at Indices With K Set Bits",
"titleSlug": "sum-of-values-at-indices-with-k-set-bits",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 154,
"dislikes": 16,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。
请你用整数形式返回 nums 中的特定元素之 和 ,这些特定元素满足:其对应下标的二进制表示中恰存在 k 个置位。
整数的二进制表示中的 1 就是这个整数的 置位 。
例如,21 的二进制表示为 10101 ,其中有 3 个置位。
示例 1:
输入:nums = [5,10,1,5,2], k = 1
输出:13
解释:下标的二进制表示是:
0 = 0002
1 = 0012
2 = 0102
3 = 0112
4 = 1002
下标 1、2 和 4 在其二进制表示中都存在 k = 1 个置位。
因此,答案为 nums[1] + nums[2] + nums[4] = 13 。
示例 2:
输入:nums = [4,3,2,1], k = 2
输出:1
解释:下标的二进制表示是:
0 = 002
1 = 012
2 = 102
3 = 112
只有下标 3 的二进制表示中存在 k = 2 个置位。
因此,答案为 nums[3] = 1 。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 105
* 0 <= k <= 10
"""
class Solution:
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
| 给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。
请你用整数形式返回 nums 中的特定元素之 和 ,这些特定元素满足:其对应下标的二进制表示中恰存在 k 个置位。
整数的二进制表示中的 1 就是这个整数的 置位 。
例如,21 的二进制表示为 10101 ,其中有 3 个置位。
示例 1:
输入:nums = [5,10,1,5,2], k = 1
输出:13
解释:下标的二进制表示是:
0 = 0002
1 = 0012
2 = 0102
3 = 0112
4 = 1002
下标 1、2 和 4 在其二进制表示中都存在 k = 1 个置位。
因此,答案为 nums[1] + nums[2] + nums[4] = 13 。
示例 2:
输入:nums = [4,3,2,1], k = 2
输出:1
解释:下标的二进制表示是:
0 = 002
1 = 012
2 = 102
3 = 112
只有下标 3 的二进制表示中存在 k = 2 个置位。
因此,答案为 nums[3] = 1 。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 105
* 0 <= k <= 10
请完成下面的代码来解决上述问题:
```python
class Solution:
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [5,10,1,5,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13
test_input = { "nums": [4,3,2,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1
test_input = { "nums": [1], "k": 0 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1
test_input = { "nums": [100000], "k": 0 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 100000
test_input = { "nums": [2,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2
test_input = { "nums": [2,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4
test_input = { "nums": [2,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [3,3], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3
test_input = { "nums": [3,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9
test_input = { "nums": [4,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [4,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8
test_input = { "nums": [6,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 6
test_input = { "nums": [7,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2
test_input = { "nums": [7,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4
test_input = { "nums": [8,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4
test_input = { "nums": [8,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9
test_input = { "nums": [9,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9
test_input = { "nums": [15,43], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 43
test_input = { "nums": [35,86], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 86
test_input = { "nums": [36,14], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14
test_input = { "nums": [47,61], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 61
test_input = { "nums": [60,46], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 46
test_input = { "nums": [70,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [1,51,55], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 106
test_input = { "nums": [2,2,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [2,3,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 5
test_input = { "nums": [3,2,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [3,7,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 12
test_input = { "nums": [4,5,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14
test_input = { "nums": [5,5,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10
test_input = { "nums": [5,7,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14
test_input = { "nums": [6,2,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3
test_input = { "nums": [6,9,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17
test_input = { "nums": [7,1,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3
test_input = { "nums": [7,9,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10
test_input = { "nums": [8,5,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9
test_input = { "nums": [9,1,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [9,3,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8
test_input = { "nums": [57,48,69], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 117
test_input = { "nums": [78,37,59], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 96
test_input = { "nums": [96,71,53], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 124
test_input = { "nums": [900,914,367], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1281
test_input = { "nums": [1,4,9,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2
test_input = { "nums": [1,5,9,5], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 5
test_input = { "nums": [1,8,5,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13
test_input = { "nums": [2,2,1,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3
test_input = { "nums": [2,4,5,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2
test_input = { "nums": [2,5,8,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13
test_input = { "nums": [2,7,3,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10
test_input = { "nums": [3,5,4,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2
test_input = { "nums": [4,1,6,3], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3
test_input = { "nums": [6,3,8,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8
test_input = { "nums": [6,6,1,4], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4
test_input = { "nums": [7,1,9,6], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 6
test_input = { "nums": [7,5,2,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [7,5,3,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8
test_input = { "nums": [7,8,6,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14
test_input = { "nums": [8,3,9,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8
test_input = { "nums": [8,7,3,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2
test_input = { "nums": [9,4,2,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 6
test_input = { "nums": [9,6,8,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14
test_input = { "nums": [9,7,8,9], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9
test_input = { "nums": [9,40,73,19], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 19
test_input = { "nums": [41,51,58,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 109
test_input = { "nums": [44,96,36,56], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 56
test_input = { "nums": [1,1,3,1,6], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1
test_input = { "nums": [2,1,1,3,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7
test_input = { "nums": [2,5,4,3,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3
test_input = { "nums": [3,2,7,1,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14
test_input = { "nums": [4,2,8,8,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8
test_input = { "nums": [4,3,7,8,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8
test_input = { "nums": [4,6,2,2,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15
test_input = { "nums": [4,7,5,1,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1
test_input = { "nums": [5,6,6,6,3], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15
test_input = { "nums": [6,4,8,4,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4
test_input = { "nums": [7,7,9,5,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 24
test_input = { "nums": [7,9,1,3,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3
test_input = { "nums": [8,5,6,9,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9
test_input = { "nums": [8,6,9,4,4], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4
test_input = { "nums": [8,8,2,9,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 12
test_input = { "nums": [9,3,7,9,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 16
test_input = { "nums": [9,5,5,5,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15
test_input = { "nums": [27,73,37,82,78], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 188
test_input = { "nums": [36,28,94,49,79], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 49
test_input = { "nums": [48,54,75,72,77], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 206
test_input = { "nums": [574,419,838,216,442], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1699
test_input = { "nums": [1,1,1,2,5,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9
test_input = { "nums": [2,6,6,8,6,4], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 12
test_input = { "nums": [2,8,2,9,2,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17
test_input = { "nums": [2,9,1,6,5,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13
test_input = { "nums": [2,9,1,6,6,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13
test_input = { "nums": [3,5,4,5,8,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17
test_input = { "nums": [5,5,3,7,9,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17
test_input = { "nums": [5,9,4,8,7,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10
test_input = { "nums": [5,9,6,6,4,5], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 11
test_input = { "nums": [6,4,7,8,4,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15
test_input = { "nums": [6,8,6,2,7,3], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 5
test_input = { "nums": [7,2,5,4,4,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 11
test_input = { "nums": [7,2,9,7,8,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14
test_input = { "nums": [7,6,6,7,6,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8 | 1,694,917,800 |
weekly-contest-363-happy-students | https://leetcode.com/problems/happy-students | happy-students | {
"questionId": "3104",
"questionFrontendId": "2860",
"title": "Happy Students",
"titleSlug": "happy-students",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 144,
"dislikes": 272,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,其中 n 是班级中学生的总数。班主任希望能够在让所有学生保持开心的情况下选出一组学生:
如果能够满足下述两个条件之一,则认为第 i 位学生将会保持开心:
* 这位学生被选中,并且被选中的学生人数 严格大于 nums[i] 。
* 这位学生没有被选中,并且被选中的学生人数 严格小于 nums[i] 。
返回能够满足让所有学生保持开心的分组方法的数目。
示例 1:
输入:nums = [1,1]
输出:2
解释:
有两种可行的方法:
班主任没有选中学生。
班主任选中所有学生形成一组。
如果班主任仅选中一个学生来完成分组,那么两个学生都无法保持开心。因此,仅存在两种可行的方法。
示例 2:
输入:nums = [6,0,3,3,6,7,2,7]
输出:3
解释:
存在三种可行的方法:
班主任选中下标为 1 的学生形成一组。
班主任选中下标为 1、2、3、6 的学生形成一组。
班主任选中所有学生形成一组。
提示:
* 1 <= nums.length <= 105
* 0 <= nums[i] < nums.length
"""
class Solution:
def countWays(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始、长度为 n 的整数数组 nums ,其中 n 是班级中学生的总数。班主任希望能够在让所有学生保持开心的情况下选出一组学生:
如果能够满足下述两个条件之一,则认为第 i 位学生将会保持开心:
* 这位学生被选中,并且被选中的学生人数 严格大于 nums[i] 。
* 这位学生没有被选中,并且被选中的学生人数 严格小于 nums[i] 。
返回能够满足让所有学生保持开心的分组方法的数目。
示例 1:
输入:nums = [1,1]
输出:2
解释:
有两种可行的方法:
班主任没有选中学生。
班主任选中所有学生形成一组。
如果班主任仅选中一个学生来完成分组,那么两个学生都无法保持开心。因此,仅存在两种可行的方法。
示例 2:
输入:nums = [6,0,3,3,6,7,2,7]
输出:3
解释:
存在三种可行的方法:
班主任选中下标为 1 的学生形成一组。
班主任选中下标为 1、2、3、6 的学生形成一组。
班主任选中所有学生形成一组。
提示:
* 1 <= nums.length <= 105
* 0 <= nums[i] < nums.length
请完成下面的代码来解决上述问题:
```python
class Solution:
def countWays(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [6,0,3,3,6,7,2,7] }
assert my_solution.countWays(**test_input) == 3
test_input = { "nums": [1,1,0,1] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [5,0,3,4,2,1,2,4] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [0,4,4,4,4,4,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,1,5,6,8,7,4,7,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [2,2,7,1,2,2,4,7] }
assert my_solution.countWays(**test_input) == 3
test_input = { "nums": [0,2,2,2,3,3,3,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [7,7,7,7,7,7,7,7,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [4,2,3,6,6,0,6,8,3] }
assert my_solution.countWays(**test_input) == 3
test_input = { "nums": [0,0,1,7,2,0,6,5] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [6,6,6,6,6,6,6,7,1,7] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,4,5,0,1,4,4,7] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [4,4,4,4,4] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,4,0,3,4] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [6,5,5,8,4,2,6,4,8] }
assert my_solution.countWays(**test_input) == 3
test_input = { "nums": [0,9,4,6,8,8,1,7,4,7] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,0,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [1,1,2,2,2,3,3,3,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [4,5,0,4,2,0,2] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [8,1,2,1,2,1,2,4,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [0,1] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [8,4,1,8,8,7,4,5,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,1,0,2,5,5,4] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [6,3,1,5,5,4,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,1,1,5,5,5,5,5,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,3,5,3,0,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,0,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,1,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [7,7,7,7,7,7,7,7,4,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,2,4] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [3,3,3,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [6,6,6,6,6,6,6,0,4,9] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [4,4,4,4,4,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,3,2,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [3,3,3,3,2,2,2,2,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,5,3,0,3,6,2] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [1,1,1,1,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [4,4,4,4,4,0,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [4,3,1,5,1,4,2,1,6] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [6,4,0,7,5,7,5,6,0] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [5,5,5,5,5,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,3,4,3,4,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,1,1,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,1,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [1,1,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [1,7,6,4,1,2,1,6,4] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,4,4,4,4,4] }
assert my_solution.countWays(**test_input) == 3
test_input = { "nums": [1,3,5,2,4,6,7,5,6] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,1,1,1,2,2,2,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [3,5,3,2,0,3,3,7] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [6,6,6,6,6,6,6] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [7,9,0,3,6,9,4,0,8,7] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,0,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [2,2,2,2,1,3,3,6] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,2,2,2,6] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [5,3,9,2,4,2,1,2,8,6] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,0,0,6,6,2,5,4] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [2,0,1] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [2,2,0] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,2,1,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [2,2,2,2,4,5,0] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,1,2] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [1,1,3,3,3,3,7,8,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,2,0,4,4] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,1,0,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [1,7,8,7,1,0,7,3,8] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,1] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,8,6,3,3,4,3,2,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [8,1,6,4,1,2,2,3,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,2,1,1,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,3,3,3,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,3,2,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,1,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,3,6,3,0,2,3] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [7,7,7,7,7,7,7,7,2,0] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [5,4,4,4,4,7,1,4] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [3,3,3,3,0] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [7,7,4,0,2,1,5,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,2,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [7,7,7,7,7,7,7,7] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,2,0,3] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [3,3,3,3,0,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [4,4,4,2,5,0] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,1,3,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,3,0,2,4] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [0,3,3,3,3,1,1] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [0,4,1,2,0,6,6] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [4,4,4,4,4,5,3,6,5,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,0,1,0] }
assert my_solution.countWays(**test_input) == 1
test_input = { "nums": [0,5,5,5,5,5,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [0,5,4,4,3,5] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [6,6,6,6,6,6,6,2,7] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [1,5,3,7,2,3,1,2,8] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [3,3,3,3,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [3,3,3,3,0,2] }
assert my_solution.countWays(**test_input) == 2
test_input = { "nums": [2,3,9,0,1,3,9,2,6,1] }
assert my_solution.countWays(**test_input) == 2 | 1,694,917,800 |
weekly-contest-363-maximum-number-of-alloys | https://leetcode.com/problems/maximum-number-of-alloys | maximum-number-of-alloys | {
"questionId": "3095",
"questionFrontendId": "2861",
"title": "Maximum Number of Alloys",
"titleSlug": "maximum-number-of-alloys",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 227,
"dislikes": 36,
"categoryTitle": "Algorithms"
} | """
假设你是一家合金制造公司的老板,你的公司使用多种金属来制造合金。现在共有 n 种不同类型的金属可以使用,并且你可以使用 k 台机器来制造合金。每台机器都需要特定数量的每种金属来创建合金。
对于第 i 台机器而言,创建合金需要 composition[i][j] 份 j 类型金属。最初,你拥有 stock[i] 份 i 类型金属,而每购入一份 i 类型金属需要花费 cost[i] 的金钱。
给你整数 n、k、budget,下标从 1 开始的二维数组 composition,两个下标从 1 开始的数组 stock 和 cost,请你在预算不超过 budget 金钱的前提下,最大化 公司制造合金的数量。
所有合金都需要由同一台机器制造。
返回公司可以制造的最大合金数。
示例 1:
输入:n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,0], cost = [1,2,3]
输出:2
解释:最优的方法是使用第 1 台机器来制造合金。
要想制造 2 份合金,我们需要购买:
- 2 份第 1 类金属。
- 2 份第 2 类金属。
- 2 份第 3 类金属。
总共需要 2 * 1 + 2 * 2 + 2 * 3 = 12 的金钱,小于等于预算 15 。
注意,我们最开始时候没有任何一类金属,所以必须买齐所有需要的金属。
可以证明在示例条件下最多可以制造 2 份合金。
示例 2:
输入:n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,100], cost = [1,2,3]
输出:5
解释:最优的方法是使用第 2 台机器来制造合金。
要想制造 5 份合金,我们需要购买:
- 5 份第 1 类金属。
- 5 份第 2 类金属。
- 0 份第 3 类金属。
总共需要 5 * 1 + 5 * 2 + 0 * 3 = 15 的金钱,小于等于预算 15 。
可以证明在示例条件下最多可以制造 5 份合金。
示例 3:
输入:n = 2, k = 3, budget = 10, composition = [[2,1],[1,2],[1,1]], stock = [1,1], cost = [5,5]
输出:2
解释:最优的方法是使用第 3 台机器来制造合金。
要想制造 2 份合金,我们需要购买:
- 1 份第 1 类金属。
- 1 份第 2 类金属。
总共需要 1 * 5 + 1 * 5 = 10 的金钱,小于等于预算 10 。
可以证明在示例条件下最多可以制造 2 份合金。
提示:
* 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
"""
class Solution:
def maxNumberOfAlloys(self, n: int, k: int, budget: int, composition: List[List[int]], stock: List[int], cost: List[int]) -> int:
| 假设你是一家合金制造公司的老板,你的公司使用多种金属来制造合金。现在共有 n 种不同类型的金属可以使用,并且你可以使用 k 台机器来制造合金。每台机器都需要特定数量的每种金属来创建合金。
对于第 i 台机器而言,创建合金需要 composition[i][j] 份 j 类型金属。最初,你拥有 stock[i] 份 i 类型金属,而每购入一份 i 类型金属需要花费 cost[i] 的金钱。
给你整数 n、k、budget,下标从 1 开始的二维数组 composition,两个下标从 1 开始的数组 stock 和 cost,请你在预算不超过 budget 金钱的前提下,最大化 公司制造合金的数量。
所有合金都需要由同一台机器制造。
返回公司可以制造的最大合金数。
示例 1:
输入:n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,0], cost = [1,2,3]
输出:2
解释:最优的方法是使用第 1 台机器来制造合金。
要想制造 2 份合金,我们需要购买:
- 2 份第 1 类金属。
- 2 份第 2 类金属。
- 2 份第 3 类金属。
总共需要 2 * 1 + 2 * 2 + 2 * 3 = 12 的金钱,小于等于预算 15 。
注意,我们最开始时候没有任何一类金属,所以必须买齐所有需要的金属。
可以证明在示例条件下最多可以制造 2 份合金。
示例 2:
输入:n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,100], cost = [1,2,3]
输出:5
解释:最优的方法是使用第 2 台机器来制造合金。
要想制造 5 份合金,我们需要购买:
- 5 份第 1 类金属。
- 5 份第 2 类金属。
- 0 份第 3 类金属。
总共需要 5 * 1 + 5 * 2 + 0 * 3 = 15 的金钱,小于等于预算 15 。
可以证明在示例条件下最多可以制造 5 份合金。
示例 3:
输入:n = 2, k = 3, budget = 10, composition = [[2,1],[1,2],[1,1]], stock = [1,1], cost = [5,5]
输出:2
解释:最优的方法是使用第 3 台机器来制造合金。
要想制造 2 份合金,我们需要购买:
- 1 份第 1 类金属。
- 1 份第 2 类金属。
总共需要 1 * 5 + 1 * 5 = 10 的金钱,小于等于预算 10 。
可以证明在示例条件下最多可以制造 2 份合金。
提示:
* 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
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxNumberOfAlloys(self, n: int, k: int, budget: int, composition: List[List[int]], stock: List[int], cost: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "n": 3, "k": 2, "budget": 15, "composition": [[1,1,1],[1,1,10]], "stock": [0,0,0], "cost": [1,2,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 3, "k": 2, "budget": 15, "composition": [[1,1,1],[1,1,10]], "stock": [0,0,100], "cost": [1,2,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 5
test_input = { "n": 2, "k": 3, "budget": 10, "composition": [[2,1],[1,2],[1,1]], "stock": [1,1], "cost": [5,5] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 4, "k": 4, "budget": 17, "composition": [[10,10,1,5],[9,7,7,1],[6,3,5,9],[2,10,2,7]], "stock": [9,8,2,7], "cost": [9,2,6,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 4, "k": 9, "budget": 55, "composition": [[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]], "stock": [9,1,10,0], "cost": [3,4,9,9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 10, "k": 10, "budget": 142, "composition": [[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]], "stock": [0,6,3,0,0,8,1,2,8,6], "cost": [2,2,2,7,4,2,10,8,9,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 9, "k": 3, "budget": 90, "composition": [[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]], "stock": [3,10,10,8,10,5,7,1,2], "cost": [9,8,10,9,9,3,9,5,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 8, "k": 4, "budget": 196, "composition": [[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]], "stock": [3,5,3,6,1,5,8,1], "cost": [4,5,4,9,4,8,7,5] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 2, "k": 5, "budget": 48, "composition": [[6,3],[9,5],[1,9],[1,8],[3,3]], "stock": [4,8], "cost": [10,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 5
test_input = { "n": 3, "k": 8, "budget": 50, "composition": [[10,8,5],[9,8,8],[2,3,1],[6,2,7],[5,5,3],[3,5,6],[8,2,9],[10,2,1]], "stock": [3,9,5], "cost": [1,10,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 4
test_input = { "n": 6, "k": 1, "budget": 195, "composition": [[4,7,7,9,6,9]], "stock": [7,4,1,4,4,0], "cost": [6,6,9,10,7,9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 10, "k": 4, "budget": 149, "composition": [[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]], "stock": [5,0,7,5,7,8,2,2,6,10], "cost": [7,5,3,3,10,9,9,3,6,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 5, "k": 3, "budget": 110, "composition": [[5,8,9,3,10],[10,10,2,1,9],[7,8,2,3,4]], "stock": [7,3,4,8,4], "cost": [2,2,6,5,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 2, "k": 3, "budget": 12, "composition": [[5,9],[7,8],[1,1]], "stock": [0,9], "cost": [8,5] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 9, "k": 5, "budget": 172, "composition": [[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]], "stock": [5,0,10,0,0,8,10,9,8], "cost": [3,7,6,10,10,5,2,10,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 7, "k": 10, "budget": 31, "composition": [[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]], "stock": [3,9,10,4,4,8,9], "cost": [6,6,9,2,1,9,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 4, "k": 9, "budget": 103, "composition": [[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]], "stock": [7,7,7,3], "cost": [4,7,6,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 5
test_input = { "n": 10, "k": 1, "budget": 197, "composition": [[7,6,6,1,2,4,8,6,4,10]], "stock": [1,3,2,1,3,4,2,6,1,1], "cost": [10,6,2,1,6,2,6,5,9,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 10, "k": 4, "budget": 152, "composition": [[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]], "stock": [7,2,9,6,9,4,6,6,3,6], "cost": [8,2,3,9,1,10,1,9,5,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 6, "k": 9, "budget": 72, "composition": [[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]], "stock": [3,3,1,6,10,8], "cost": [1,8,9,8,3,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 1, "k": 10, "budget": 177, "composition": [[6],[3],[8],[8],[7],[7],[4],[5],[10],[1]], "stock": [2], "cost": [7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 27
test_input = { "n": 2, "k": 6, "budget": 196, "composition": [[6,5],[7,10],[3,10],[5,8],[5,7],[5,6]], "stock": [6,3], "cost": [3,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 5
test_input = { "n": 7, "k": 9, "budget": 148, "composition": [[5,8,7,7,5,8,4],[8,6,2,6,3,3,2],[5,6,9,6,6,2,5],[8,2,10,5,4,5,10],[2,8,10,4,9,6,1],[4,1,2,2,5,5,5],[9,9,1,4,1,4,4],[3,8,4,4,10,4,6],[8,2,8,4,5,5,10]], "stock": [7,8,7,9,3,8,2], "cost": [7,5,4,5,1,3,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 8, "k": 5, "budget": 151, "composition": [[5,9,10,2,8,10,2,8],[1,5,8,9,3,4,6,6],[10,10,10,6,1,7,9,4],[6,7,6,2,10,8,6,10],[5,2,6,2,8,1,6,2]], "stock": [0,6,2,2,9,8,0,3], "cost": [6,7,4,6,10,3,5,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 8, "k": 3, "budget": 187, "composition": [[1,4,8,6,8,5,1,4],[10,9,4,3,1,2,5,9],[4,10,7,8,7,7,1,9]], "stock": [2,6,4,0,2,8,2,3], "cost": [9,2,5,7,6,10,2,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 1, "k": 3, "budget": 90, "composition": [[5],[3],[9]], "stock": [5], "cost": [10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 4
test_input = { "n": 10, "k": 5, "budget": 91, "composition": [[7,8,3,2,9,3,4,4,2,3],[3,2,4,1,4,5,10,9,10,7],[1,4,3,4,9,5,2,2,9,9],[6,9,9,6,2,7,1,10,5,3],[10,7,8,2,2,2,9,6,1,4]], "stock": [9,5,5,0,0,8,1,4,5,3], "cost": [7,3,6,4,10,10,5,4,2,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 8, "k": 3, "budget": 97, "composition": [[3,3,7,1,5,5,8,2],[10,5,1,3,1,5,1,5],[7,2,2,10,7,10,6,8]], "stock": [1,1,8,3,0,1,0,6], "cost": [4,1,4,5,5,3,5,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 9, "k": 3, "budget": 19, "composition": [[5,9,4,6,6,1,4,5,3],[6,9,2,3,5,4,1,4,5],[6,10,5,4,7,5,3,4,3]], "stock": [8,7,6,3,4,7,7,0,4], "cost": [10,8,1,6,9,7,3,7,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 8, "k": 2, "budget": 168, "composition": [[5,7,8,6,7,4,10,8],[3,7,7,4,8,9,9,9]], "stock": [6,4,5,10,2,5,3,8], "cost": [5,1,10,3,4,4,7,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 3, "k": 3, "budget": 108, "composition": [[6,1,10],[5,3,6],[2,8,7]], "stock": [3,9,7], "cost": [4,2,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 3
test_input = { "n": 10, "k": 5, "budget": 197, "composition": [[7,2,9,6,2,3,8,9,10,10],[2,1,7,7,3,1,3,8,1,2],[4,5,1,3,6,3,2,4,4,6],[8,5,9,10,8,3,7,10,1,7],[8,3,2,4,1,5,3,6,9,6]], "stock": [5,2,9,8,1,3,6,4,2,3], "cost": [6,6,10,4,9,5,2,6,4,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 8, "k": 8, "budget": 90, "composition": [[6,6,9,7,6,7,7,5],[5,10,4,2,8,5,6,6],[7,7,1,10,3,3,2,2],[7,9,8,10,7,10,8,2],[7,1,2,2,1,2,3,6],[2,8,10,10,6,2,6,3],[3,2,2,2,4,7,4,3],[2,5,3,2,3,7,6,4]], "stock": [1,1,6,10,3,0,8,6], "cost": [3,2,1,3,2,3,8,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 4, "k": 7, "budget": 87, "composition": [[8,8,5,3],[7,8,8,9],[1,7,3,10],[4,3,9,8],[4,7,2,2],[5,8,2,2],[6,1,2,7]], "stock": [3,7,9,8], "cost": [6,3,1,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 2, "k": 3, "budget": 184, "composition": [[7,1],[6,7],[4,6]], "stock": [1,6], "cost": [8,2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 4
test_input = { "n": 4, "k": 3, "budget": 25, "composition": [[7,4,5,3],[10,8,1,2],[6,4,3,4]], "stock": [1,3,0,5], "cost": [1,2,6,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 10, "k": 8, "budget": 33, "composition": [[3,2,9,8,3,7,10,2,6,7],[6,6,5,6,3,3,4,6,5,7],[6,8,5,10,8,4,1,8,4,2],[7,10,7,10,4,4,10,7,5,3],[2,6,3,3,8,8,2,6,4,2],[2,2,2,4,8,2,7,3,7,4],[10,9,7,9,9,2,3,9,2,1],[8,9,10,7,10,9,7,2,3,8]], "stock": [0,2,5,5,8,2,5,9,1,1], "cost": [3,4,10,5,8,8,8,9,8,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 7, "k": 9, "budget": 8, "composition": [[5,4,8,9,2,2,2],[2,8,7,6,8,10,3],[6,8,4,4,5,4,10],[5,3,7,8,2,2,9],[8,4,3,2,6,4,3],[5,2,8,5,4,5,10],[9,5,4,9,6,5,7],[10,1,6,7,2,7,5],[3,6,9,9,3,7,6]], "stock": [3,9,1,5,1,7,9], "cost": [5,7,1,6,8,3,9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 1, "k": 3, "budget": 96, "composition": [[4],[8],[3]], "stock": [0], "cost": [6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 5
test_input = { "n": 4, "k": 2, "budget": 113, "composition": [[6,9,5,7],[4,9,7,1]], "stock": [4,1,0,4], "cost": [9,2,3,5] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 6, "k": 6, "budget": 97, "composition": [[9,3,10,2,6,3],[9,4,3,7,1,7],[10,10,9,2,1,6],[4,5,2,3,3,10],[2,6,8,3,6,1],[4,9,6,10,3,10]], "stock": [2,8,10,8,9,0], "cost": [4,5,6,3,10,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 9, "k": 6, "budget": 18, "composition": [[5,10,2,4,3,3,2,10,3],[2,7,1,7,10,7,8,8,7],[6,2,10,2,4,3,4,8,9],[5,7,2,10,6,10,4,10,3],[1,9,4,4,9,9,4,2,6],[7,5,1,4,10,9,2,2,3]], "stock": [5,4,0,1,1,6,1,8,0], "cost": [8,1,6,5,10,4,10,9,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 2, "k": 10, "budget": 197, "composition": [[8,1],[7,4],[2,3],[10,3],[6,3],[9,8],[8,7],[3,4],[2,6],[4,5]], "stock": [5,9], "cost": [10,2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 10
test_input = { "n": 10, "k": 4, "budget": 115, "composition": [[4,6,5,10,9,5,2,2,10,1],[6,7,2,2,4,10,3,8,3,7],[1,9,10,5,4,6,2,1,8,4],[7,10,9,5,10,6,9,5,8,4]], "stock": [7,8,8,8,6,10,7,8,2,3], "cost": [3,5,1,8,7,7,10,4,7,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 6, "k": 3, "budget": 168, "composition": [[1,2,10,5,5,8],[1,3,6,1,3,6],[8,5,6,6,5,10]], "stock": [7,0,3,1,6,8], "cost": [5,6,2,5,3,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 4
test_input = { "n": 4, "k": 1, "budget": 13, "composition": [[6,10,1,10]], "stock": [2,9,7,3], "cost": [7,8,5,5] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 1, "k": 3, "budget": 144, "composition": [[4],[10],[9]], "stock": [10], "cost": [1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 38
test_input = { "n": 8, "k": 4, "budget": 34, "composition": [[9,1,1,9,1,10,6,4],[10,8,6,5,7,5,2,9],[7,4,5,10,7,2,6,2],[3,8,3,6,9,9,10,5]], "stock": [9,9,6,5,5,7,5,4], "cost": [7,4,2,2,8,10,10,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 10, "k": 3, "budget": 64, "composition": [[7,2,7,4,4,6,8,3,5,6],[10,10,6,5,4,7,5,1,3,2],[10,10,8,4,6,8,9,1,8,10]], "stock": [8,9,7,3,10,6,6,0,6,10], "cost": [7,8,4,6,9,7,7,8,2,9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 9, "k": 5, "budget": 37, "composition": [[7,5,8,5,3,1,4,10,6],[4,5,5,5,7,4,2,8,1],[3,8,3,6,7,9,10,2,7],[5,3,5,1,10,3,4,10,6],[6,2,9,3,10,6,3,9,7]], "stock": [1,4,1,7,10,8,8,3,6], "cost": [7,4,2,7,3,10,9,8,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 3, "k": 10, "budget": 67, "composition": [[5,3,10],[7,5,4],[3,9,9],[10,2,9],[9,4,8],[8,5,7],[5,2,3],[1,7,2],[3,9,1],[7,1,4]], "stock": [2,9,4], "cost": [4,9,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 3
test_input = { "n": 4, "k": 7, "budget": 113, "composition": [[6,10,4,10],[7,8,1,1],[1,5,4,1],[4,7,8,9],[7,9,2,4],[5,1,10,4],[3,3,9,4]], "stock": [0,5,5,10], "cost": [1,10,8,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 10, "k": 7, "budget": 128, "composition": [[5,1,1,4,5,9,2,9,2,2],[6,10,4,8,3,10,8,4,5,10],[3,3,8,5,2,6,4,5,4,8],[5,5,4,1,3,2,10,5,3,10],[9,4,2,4,2,4,7,7,1,4],[9,2,10,5,1,5,5,9,5,6],[10,7,9,1,4,7,6,7,5,7]], "stock": [3,8,4,5,3,5,4,10,4,9], "cost": [4,1,8,4,2,9,1,2,1,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 1, "k": 7, "budget": 48, "composition": [[1],[5],[9],[6],[4],[2],[4]], "stock": [6], "cost": [1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 54
test_input = { "n": 9, "k": 4, "budget": 21, "composition": [[7,2,7,7,8,1,6,7,3],[8,10,4,3,7,2,3,2,5],[6,9,3,2,7,6,10,6,5],[10,2,4,10,7,9,5,8,6]], "stock": [9,10,5,2,10,9,8,10,10], "cost": [6,5,2,8,10,1,2,7,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 8, "k": 8, "budget": 164, "composition": [[8,8,7,7,4,8,8,3],[8,1,5,9,4,5,10,8],[6,3,7,5,5,5,8,7],[7,1,6,2,6,10,5,6],[9,10,1,10,3,8,9,9],[1,5,5,4,5,10,5,9],[8,3,5,3,5,4,7,1],[10,2,3,6,2,4,8,6]], "stock": [10,3,10,2,1,4,8,8], "cost": [2,9,7,7,4,3,2,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 1, "k": 6, "budget": 149, "composition": [[4],[8],[1],[9],[1],[9]], "stock": [6], "cost": [7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 27
test_input = { "n": 6, "k": 7, "budget": 136, "composition": [[8,9,8,5,9,8],[4,2,1,9,3,8],[6,8,3,1,9,9],[8,1,4,5,2,7],[4,5,6,3,4,9],[5,9,8,2,1,10],[10,10,9,9,2,8]], "stock": [4,1,2,9,9,2], "cost": [8,1,7,8,1,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 6, "k": 1, "budget": 55, "composition": [[3,5,3,8,9,8]], "stock": [9,7,0,1,9,4], "cost": [3,3,1,1,1,2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 3, "k": 1, "budget": 195, "composition": [[7,3,7]], "stock": [0,10,7], "cost": [7,6,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 1, "k": 8, "budget": 69, "composition": [[8],[9],[10],[10],[4],[4],[7],[6]], "stock": [10], "cost": [9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 4
test_input = { "n": 9, "k": 2, "budget": 176, "composition": [[8,10,1,2,6,3,5,7,7],[4,6,4,8,4,5,3,6,6]], "stock": [10,9,1,3,10,1,10,5,2], "cost": [3,8,8,2,5,6,5,8,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 8, "k": 3, "budget": 17, "composition": [[3,1,4,8,7,8,5,5],[7,10,6,6,3,10,10,9],[5,6,7,1,4,7,5,1]], "stock": [0,4,0,4,4,9,2,1], "cost": [10,1,3,9,9,3,1,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 1, "k": 9, "budget": 109, "composition": [[8],[10],[4],[3],[9],[7],[9],[8],[7]], "stock": [10], "cost": [9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 7
test_input = { "n": 7, "k": 6, "budget": 130, "composition": [[4,2,10,2,2,9,4],[9,4,8,8,4,9,9],[9,10,7,8,7,1,4],[8,2,5,5,6,4,7],[9,8,4,3,8,6,2],[1,2,3,9,4,10,1]], "stock": [10,1,7,10,1,10,5], "cost": [3,7,6,5,1,5,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 1, "k": 8, "budget": 48, "composition": [[5],[6],[10],[9],[2],[8],[9],[8]], "stock": [9], "cost": [5] }
assert my_solution.maxNumberOfAlloys(**test_input) == 9
test_input = { "n": 10, "k": 3, "budget": 124, "composition": [[5,8,7,1,7,7,5,4,5,2],[9,1,2,8,2,8,3,5,2,4],[7,4,6,4,4,2,4,10,4,8]], "stock": [9,8,7,0,9,1,4,3,1,1], "cost": [9,3,5,4,3,5,2,10,7,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 7, "k": 10, "budget": 177, "composition": [[1,8,4,1,9,7,4],[8,1,3,3,9,4,5],[8,5,4,2,9,9,10],[2,10,3,3,3,10,8],[6,3,1,3,7,1,7],[3,5,7,6,8,10,10],[2,10,10,2,2,7,7],[3,2,10,9,4,1,2],[2,7,1,8,2,7,10],[10,9,2,8,10,1,4]], "stock": [0,3,5,10,0,9,9], "cost": [3,5,4,8,10,1,2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 6, "k": 3, "budget": 135, "composition": [[7,8,3,4,10,5],[6,5,10,3,1,1],[5,2,9,9,8,1]], "stock": [10,0,5,1,0,7], "cost": [3,10,3,3,3,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 10, "k": 1, "budget": 131, "composition": [[10,2,8,4,3,6,10,8,8,6]], "stock": [1,7,10,1,4,8,4,6,7,0], "cost": [1,10,9,3,9,8,3,2,9,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 10, "k": 7, "budget": 79, "composition": [[3,8,9,10,7,3,8,4,2,2],[7,9,1,1,2,1,8,7,5,7],[5,9,6,2,9,4,10,1,8,5],[4,3,7,2,4,7,4,6,2,10],[4,5,2,5,5,2,7,7,1,8],[9,2,9,4,9,4,7,3,4,4],[8,4,10,8,2,8,7,5,6,10]], "stock": [0,4,5,5,7,9,9,7,3,0], "cost": [10,8,1,2,7,7,4,7,6,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 8, "k": 7, "budget": 86, "composition": [[10,5,6,8,9,10,6,5],[7,8,9,9,3,3,9,4],[5,4,4,4,10,2,6,3],[9,7,1,10,10,4,4,6],[10,9,10,3,4,2,9,4],[2,1,4,8,3,6,4,1],[1,8,2,2,3,3,10,8]], "stock": [0,6,0,5,6,0,10,1], "cost": [7,9,10,10,5,5,7,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 6, "k": 8, "budget": 48, "composition": [[10,2,1,4,9,1],[5,10,6,3,3,9],[10,10,10,9,2,9],[1,5,2,2,10,9],[7,9,10,5,10,3],[3,3,10,5,6,2],[6,6,6,8,9,9],[2,4,2,7,3,3]], "stock": [5,6,5,1,3,5], "cost": [4,3,8,6,1,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 2, "k": 8, "budget": 44, "composition": [[8,5],[1,6],[3,10],[4,6],[5,8],[10,5],[7,5],[5,1]], "stock": [8,0], "cost": [8,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 9, "k": 3, "budget": 107, "composition": [[8,1,9,10,10,5,4,9,1],[8,10,6,8,10,2,10,9,4],[9,6,4,7,10,2,7,4,2]], "stock": [9,2,4,1,1,3,8,9,0], "cost": [6,2,8,3,1,3,5,9,9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 3, "k": 5, "budget": 125, "composition": [[10,8,9],[10,7,8],[7,7,6],[9,2,5],[8,4,6]], "stock": [8,3,1], "cost": [4,10,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 6, "k": 10, "budget": 118, "composition": [[10,7,8,10,5,9],[2,4,4,5,4,2],[6,7,2,6,3,10],[3,8,10,8,1,6],[2,9,3,8,5,5],[1,6,8,1,7,7],[4,9,1,8,9,5],[9,4,10,4,1,4],[1,5,10,2,5,3],[2,1,3,3,2,9]], "stock": [9,6,2,2,6,5], "cost": [2,1,7,5,10,9] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 1, "k": 2, "budget": 148, "composition": [[1],[8]], "stock": [4], "cost": [2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 78
test_input = { "n": 5, "k": 5, "budget": 91, "composition": [[2,4,10,5,5],[4,3,8,8,10],[9,6,2,7,3],[7,7,3,6,6],[6,4,5,3,4]], "stock": [6,4,7,3,3], "cost": [4,10,9,7,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 8, "k": 5, "budget": 143, "composition": [[8,3,3,6,2,5,8,9],[6,8,3,6,4,10,10,6],[9,6,10,9,6,5,1,1],[1,1,10,3,4,10,2,2],[10,6,4,9,9,3,9,2]], "stock": [2,1,4,5,6,8,2,8], "cost": [1,8,3,9,9,7,1,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 4, "k": 9, "budget": 172, "composition": [[9,2,2,2],[4,5,3,2],[4,6,1,9],[5,3,3,5],[2,4,3,9],[7,4,4,3],[1,3,2,6],[7,2,5,4],[4,4,2,2]], "stock": [6,7,5,2], "cost": [2,8,5,2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 5
test_input = { "n": 5, "k": 2, "budget": 110, "composition": [[2,8,10,7,4],[5,3,5,5,5]], "stock": [6,8,8,1,6], "cost": [8,5,8,6,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 3, "k": 10, "budget": 21, "composition": [[9,8,5],[2,9,9],[2,6,8],[7,4,10],[10,8,6],[2,6,5],[6,6,8],[4,7,7],[8,9,10],[6,1,7]], "stock": [6,4,10], "cost": [7,9,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 5, "k": 1, "budget": 176, "composition": [[5,6,2,5,4]], "stock": [1,4,3,10,7], "cost": [10,9,9,8,10] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 9, "k": 1, "budget": 112, "composition": [[10,4,3,5,2,10,10,8,9]], "stock": [4,4,3,0,5,0,7,6,2], "cost": [7,9,8,9,2,6,10,5,5] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 8, "k": 9, "budget": 41, "composition": [[10,6,7,3,6,9,9,8],[5,9,5,2,4,10,2,5],[4,10,9,3,5,10,7,1],[8,3,1,4,2,5,3,1],[1,10,10,10,7,1,10,4],[6,9,7,10,8,7,6,9],[4,9,10,4,10,7,1,7],[3,5,9,5,2,8,3,10],[8,7,1,9,3,8,6,3]], "stock": [5,4,10,10,7,8,7,6], "cost": [5,1,10,4,9,9,4,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 2, "k": 10, "budget": 2, "composition": [[9,6],[10,1],[7,3],[5,5],[7,6],[10,2],[7,3],[7,6],[7,3],[2,7]], "stock": [6,10], "cost": [1,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 2, "k": 7, "budget": 168, "composition": [[8,2],[4,7],[8,3],[1,6],[3,3],[9,7],[8,4]], "stock": [1,7], "cost": [5,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 8
test_input = { "n": 8, "k": 9, "budget": 195, "composition": [[2,5,4,2,2,4,4,1],[5,4,5,8,9,1,2,5],[1,10,3,9,6,7,1,3],[4,10,3,3,6,4,7,6],[4,3,10,2,7,8,4,9],[4,1,6,2,8,7,3,3],[10,7,5,2,1,5,4,5],[7,4,3,10,4,10,1,2],[9,7,9,8,9,2,3,10]], "stock": [1,1,7,10,1,6,3,9], "cost": [1,4,7,6,4,5,5,2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 3
test_input = { "n": 7, "k": 6, "budget": 167, "composition": [[2,2,3,4,6,2,4],[5,7,3,7,4,6,7],[5,7,4,10,5,1,1],[2,3,10,6,9,5,6],[3,9,7,8,5,10,2],[6,7,8,8,1,6,6]], "stock": [10,10,8,9,7,8,4], "cost": [2,6,1,9,9,9,8] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 6, "k": 9, "budget": 73, "composition": [[1,1,9,4,1,7],[6,8,7,7,6,2],[2,1,5,10,2,5],[3,10,7,7,5,10],[6,1,6,8,4,6],[3,10,10,9,8,2],[10,8,7,7,4,2],[10,2,3,8,7,4],[7,5,9,10,4,3]], "stock": [1,9,8,6,10,6], "cost": [6,3,9,7,1,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 5, "k": 3, "budget": 128, "composition": [[4,6,5,5,1],[1,4,8,8,7],[3,3,5,4,5]], "stock": [6,8,8,0,2], "cost": [2,4,9,7,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 10, "k": 7, "budget": 96, "composition": [[9,3,10,2,8,6,1,7,2,4],[2,10,4,5,5,3,7,5,2,10],[8,7,7,10,6,6,3,2,3,8],[8,1,5,4,7,8,1,9,2,10],[6,7,10,9,8,8,8,3,1,2],[1,6,9,1,8,4,9,4,9,7],[5,6,5,2,1,8,9,4,3,6]], "stock": [4,6,3,0,7,0,7,10,8,10], "cost": [4,3,6,5,6,2,7,3,1,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 5, "k": 9, "budget": 81, "composition": [[4,4,9,5,5],[7,9,10,6,8],[4,7,4,2,10],[2,9,6,9,8],[2,5,7,3,4],[4,9,9,2,5],[5,6,1,2,9],[5,3,8,7,8],[8,6,9,9,3]], "stock": [2,9,6,5,3], "cost": [3,3,8,7,6] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 10, "k": 10, "budget": 102, "composition": [[9,4,10,4,9,4,3,2,2,4],[10,8,3,4,5,6,4,10,2,7],[4,1,10,9,4,5,7,9,6,8],[1,5,9,7,8,5,10,3,8,7],[6,2,10,2,8,10,7,5,10,7],[7,2,2,6,8,7,9,10,6,8],[9,4,8,3,10,3,2,5,6,6],[3,1,3,5,10,5,8,8,1,10],[6,4,3,9,3,8,8,6,3,5],[8,9,1,2,7,8,1,10,8,1]], "stock": [3,6,1,3,3,7,7,2,2,2], "cost": [3,10,9,7,1,3,2,9,7,3] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 6, "k": 6, "budget": 115, "composition": [[2,5,8,6,2,3],[7,10,7,2,10,5],[2,7,10,10,3,8],[3,3,8,4,3,10],[10,10,4,10,4,3],[8,9,9,5,7,5]], "stock": [7,1,10,7,6,2], "cost": [3,8,9,7,8,4] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1
test_input = { "n": 8, "k": 10, "budget": 19, "composition": [[7,6,3,10,2,9,7,2],[5,10,3,3,10,6,6,10],[10,4,9,10,10,7,7,9],[10,6,9,3,4,9,9,5],[7,1,3,4,8,2,8,8],[9,2,3,1,1,2,2,5],[2,6,9,3,7,9,5,8],[3,10,5,2,8,5,8,10],[8,10,1,1,2,1,7,8],[10,8,4,8,5,5,10,2]], "stock": [6,0,9,2,5,0,10,8], "cost": [2,4,9,8,4,10,3,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 0
test_input = { "n": 7, "k": 10, "budget": 179, "composition": [[10,5,2,6,5,2,7],[1,6,8,2,4,8,3],[8,6,1,2,7,7,4],[4,1,9,6,3,8,10],[7,6,3,5,3,4,2],[8,10,9,3,8,1,5],[5,4,1,7,7,6,3],[10,9,8,1,10,4,8],[9,4,6,2,3,3,9],[6,5,2,3,10,6,8]], "stock": [9,0,2,10,3,7,6], "cost": [6,2,7,10,1,2,7] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 9, "k": 10, "budget": 123, "composition": [[4,9,5,9,9,4,8,10,10],[3,1,5,8,4,4,8,6,3],[5,7,2,8,2,7,3,9,5],[9,4,6,7,2,3,4,3,3],[3,6,2,1,3,7,10,7,3],[2,6,9,7,5,7,3,10,1],[1,7,10,6,9,6,6,9,8],[10,8,2,1,6,8,3,8,6],[6,5,1,3,5,1,2,3,1],[8,2,6,1,8,8,3,2,1]], "stock": [2,6,3,4,6,1,7,2,6], "cost": [5,5,9,1,5,10,4,1,2] }
assert my_solution.maxNumberOfAlloys(**test_input) == 2
test_input = { "n": 6, "k": 1, "budget": 161, "composition": [[4,6,9,8,5,5]], "stock": [7,4,5,1,9,4], "cost": [6,5,5,6,3,1] }
assert my_solution.maxNumberOfAlloys(**test_input) == 1 | 1,694,917,800 |
weekly-contest-363-maximum-element-sum-of-a-complete-subset-of-indices | https://leetcode.com/problems/maximum-element-sum-of-a-complete-subset-of-indices | maximum-element-sum-of-a-complete-subset-of-indices | {
"questionId": "3047",
"questionFrontendId": "2862",
"title": "Maximum Element-Sum of a Complete Subset of Indices",
"titleSlug": "maximum-element-sum-of-a-complete-subset-of-indices",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 157,
"dislikes": 27,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 1 开始、由 n 个整数组成的数组。
如果一组数字中每对元素的乘积都是一个完全平方数,则称这组数字是一个 完全集 。
下标集 {1, 2, ..., n} 的子集可以表示为 {i1, i2, ..., ik},我们定义对应该子集的 元素和 为 nums[i1] + nums[i2] + ... + nums[ik] 。
返回下标集 {1, 2, ..., n} 的 完全子集 所能取到的 最大元素和 。
完全平方数是指可以表示为一个整数和其自身相乘的数。
示例 1:
输入:nums = [8,7,3,5,7,2,4,9]
输出:16
解释:除了由单个下标组成的子集之外,还有两个下标集的完全子集:{1,4} 和 {2,8} 。
与下标 1 和 4 对应的元素和等于 nums[1] + nums[4] = 8 + 5 = 13 。
与下标 2 和 8 对应的元素和等于 nums[2] + nums[8] = 7 + 9 = 16 。
因此,下标集的完全子集可以取到的最大元素和为 16 。
示例 2:
输入:nums = [5,10,3,10,1,13,7,9,4]
输出:19
解释:除了由单个下标组成的子集之外,还有四个下标集的完全子集:{1,4}、{1,9}、{2,8}、{4,9} 和 {1,4,9} 。
与下标 1 和 4 对应的元素和等于 nums[1] + nums[4] = 5 + 10 = 15 。
与下标 1 和 9 对应的元素和等于 nums[1] + nums[9] = 5 + 4 = 9 。
与下标 2 和 8 对应的元素和等于 nums[2] + nums[8] = 10 + 9 = 19 。
与下标 4 和 9 对应的元素和等于 nums[4] + nums[9] = 10 + 4 = 14 。
与下标 1、4 和 9 对应的元素和等于 nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19 。
因此,下标集的完全子集可以取到的最大元素和为 19 。
提示:
* 1 <= n == nums.length <= 104
* 1 <= nums[i] <= 109
"""
class Solution:
def maximumSum(self, nums: List[int]) -> int:
| 给你一个下标从 1 开始、由 n 个整数组成的数组。
如果一组数字中每对元素的乘积都是一个完全平方数,则称这组数字是一个 完全集 。
下标集 {1, 2, ..., n} 的子集可以表示为 {i1, i2, ..., ik},我们定义对应该子集的 元素和 为 nums[i1] + nums[i2] + ... + nums[ik] 。
返回下标集 {1, 2, ..., n} 的 完全子集 所能取到的 最大元素和 。
完全平方数是指可以表示为一个整数和其自身相乘的数。
示例 1:
输入:nums = [8,7,3,5,7,2,4,9]
输出:16
解释:除了由单个下标组成的子集之外,还有两个下标集的完全子集:{1,4} 和 {2,8} 。
与下标 1 和 4 对应的元素和等于 nums[1] + nums[4] = 8 + 5 = 13 。
与下标 2 和 8 对应的元素和等于 nums[2] + nums[8] = 7 + 9 = 16 。
因此,下标集的完全子集可以取到的最大元素和为 16 。
示例 2:
输入:nums = [5,10,3,10,1,13,7,9,4]
输出:19
解释:除了由单个下标组成的子集之外,还有四个下标集的完全子集:{1,4}、{1,9}、{2,8}、{4,9} 和 {1,4,9} 。
与下标 1 和 4 对应的元素和等于 nums[1] + nums[4] = 5 + 10 = 15 。
与下标 1 和 9 对应的元素和等于 nums[1] + nums[9] = 5 + 4 = 9 。
与下标 2 和 8 对应的元素和等于 nums[2] + nums[8] = 10 + 9 = 19 。
与下标 4 和 9 对应的元素和等于 nums[4] + nums[9] = 10 + 4 = 14 。
与下标 1、4 和 9 对应的元素和等于 nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19 。
因此,下标集的完全子集可以取到的最大元素和为 19 。
提示:
* 1 <= n == nums.length <= 104
* 1 <= nums[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumSum(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [8,7,3,5,7,2,4,9] }
assert my_solution.maximumSum(**test_input) == 16
test_input = { "nums": [5,10,3,10,1,13,7,9,4] }
assert my_solution.maximumSum(**test_input) == 19
test_input = { "nums": [1,1,1,1] }
assert my_solution.maximumSum(**test_input) == 2
test_input = { "nums": [1,100,100,1] }
assert my_solution.maximumSum(**test_input) == 100
test_input = { "nums": [998244353,998244353,998244353,998244353] }
assert my_solution.maximumSum(**test_input) == 1996488706
test_input = { "nums": [1000000000,1,1,1000000000] }
assert my_solution.maximumSum(**test_input) == 2000000000
test_input = { "nums": [1,1,1000000000,1] }
assert my_solution.maximumSum(**test_input) == 1000000000
test_input = { "nums": [5,10,3,10,1,13,7,20,4] }
assert my_solution.maximumSum(**test_input) == 30
test_input = { "nums": [5,3,3,10,1,13,7,67,4] }
assert my_solution.maximumSum(**test_input) == 70
test_input = { "nums": [35,45,29,16,42,49,25,19,46] }
assert my_solution.maximumSum(**test_input) == 97
test_input = { "nums": [34,43,47,50,45] }
assert my_solution.maximumSum(**test_input) == 84
test_input = { "nums": [4,31,45,34,44] }
assert my_solution.maximumSum(**test_input) == 45
test_input = { "nums": [12,5,49,26] }
assert my_solution.maximumSum(**test_input) == 49
test_input = { "nums": [41] }
assert my_solution.maximumSum(**test_input) == 41
test_input = { "nums": [38,9,37,11,20,43] }
assert my_solution.maximumSum(**test_input) == 49
test_input = { "nums": [47,50,17,49,12,22] }
assert my_solution.maximumSum(**test_input) == 96
test_input = { "nums": [23,13,17,3,21,23] }
assert my_solution.maximumSum(**test_input) == 26
test_input = { "nums": [38,28] }
assert my_solution.maximumSum(**test_input) == 38
test_input = { "nums": [16,31,37,29,28,34,25,36] }
assert my_solution.maximumSum(**test_input) == 67
test_input = { "nums": [19,46,37,44,4,40,24,17,49] }
assert my_solution.maximumSum(**test_input) == 112
test_input = { "nums": [28,40,37] }
assert my_solution.maximumSum(**test_input) == 40
test_input = { "nums": [19,6,30,23,25,45,15,2,3,46] }
assert my_solution.maximumSum(**test_input) == 46
test_input = { "nums": [5,16,4,13,37,44,49,7] }
assert my_solution.maximumSum(**test_input) == 49
test_input = { "nums": [40,8,19,26] }
assert my_solution.maximumSum(**test_input) == 66
test_input = { "nums": [3,37,2] }
assert my_solution.maximumSum(**test_input) == 37
test_input = { "nums": [1,5,35,20,32,18,6,49,34,15] }
assert my_solution.maximumSum(**test_input) == 55
test_input = { "nums": [25,31,4,20,45] }
assert my_solution.maximumSum(**test_input) == 45
test_input = { "nums": [32,3,25,15,37,37,21,24,8,33] }
assert my_solution.maximumSum(**test_input) == 55
test_input = { "nums": [13] }
assert my_solution.maximumSum(**test_input) == 13
test_input = { "nums": [41,25,20,28,40,22,37,43,6,32] }
assert my_solution.maximumSum(**test_input) == 75
test_input = { "nums": [23,9,7,24,30,34,10,47] }
assert my_solution.maximumSum(**test_input) == 56
test_input = { "nums": [11,31,1,34,8,20,15,20,49] }
assert my_solution.maximumSum(**test_input) == 94
test_input = { "nums": [41,21,4,15,25,38,26,7,6] }
assert my_solution.maximumSum(**test_input) == 62
test_input = { "nums": [46,31,28,34,12,40,11,31,8,25] }
assert my_solution.maximumSum(**test_input) == 88
test_input = { "nums": [50] }
assert my_solution.maximumSum(**test_input) == 50
test_input = { "nums": [41,43,17,35] }
assert my_solution.maximumSum(**test_input) == 76
test_input = { "nums": [20,6] }
assert my_solution.maximumSum(**test_input) == 20
test_input = { "nums": [43,18,10,19,20,9,49] }
assert my_solution.maximumSum(**test_input) == 62
test_input = { "nums": [3,39,29,5,6,36,38,26,14,10] }
assert my_solution.maximumSum(**test_input) == 65
test_input = { "nums": [47,2,48,16,10,3,45,20,39] }
assert my_solution.maximumSum(**test_input) == 102
test_input = { "nums": [11,47,27,17,7] }
assert my_solution.maximumSum(**test_input) == 47
test_input = { "nums": [48,17,41,23,40,9,3,26,44,34] }
assert my_solution.maximumSum(**test_input) == 115
test_input = { "nums": [47,2,42,10,15,44,35,50] }
assert my_solution.maximumSum(**test_input) == 57
test_input = { "nums": [45,48,50,24,23,14,2,33] }
assert my_solution.maximumSum(**test_input) == 81
test_input = { "nums": [7,39,27,39,43,42,31,37,15] }
assert my_solution.maximumSum(**test_input) == 76
test_input = { "nums": [10,36,23,31] }
assert my_solution.maximumSum(**test_input) == 41
test_input = { "nums": [27,27,13,37] }
assert my_solution.maximumSum(**test_input) == 64
test_input = { "nums": [34,17,26,13] }
assert my_solution.maximumSum(**test_input) == 47
test_input = { "nums": [9,42] }
assert my_solution.maximumSum(**test_input) == 42
test_input = { "nums": [23,42,4,28,30,36,30,39] }
assert my_solution.maximumSum(**test_input) == 81
test_input = { "nums": [10,28,28,21,25,14,38] }
assert my_solution.maximumSum(**test_input) == 38
test_input = { "nums": [40,34,26,9,23,15,23,27,49] }
assert my_solution.maximumSum(**test_input) == 98
test_input = { "nums": [27,42,40,2,1] }
assert my_solution.maximumSum(**test_input) == 42
test_input = { "nums": [48,19,21,21,32,20,50,41,49,30] }
assert my_solution.maximumSum(**test_input) == 118
test_input = { "nums": [10,13,6,39,9] }
assert my_solution.maximumSum(**test_input) == 49
test_input = { "nums": [50,37,24,4,10,43,35] }
assert my_solution.maximumSum(**test_input) == 54
test_input = { "nums": [39,24] }
assert my_solution.maximumSum(**test_input) == 39
test_input = { "nums": [21] }
assert my_solution.maximumSum(**test_input) == 21
test_input = { "nums": [20,15] }
assert my_solution.maximumSum(**test_input) == 20
test_input = { "nums": [23,27,42,3,33,36,43,32,27,48,40,22,5,36,48] }
assert my_solution.maximumSum(**test_input) == 64
test_input = { "nums": [39,46,12,14,25,37,24,44,6,38,4] }
assert my_solution.maximumSum(**test_input) == 90
test_input = { "nums": [36,5,23,17,32,47,23,41,18,44,21,4,22,6,21] }
assert my_solution.maximumSum(**test_input) == 71
test_input = { "nums": [46,26,37,17,15,26,45,45,17,42,16,14,36,40] }
assert my_solution.maximumSum(**test_input) == 80
test_input = { "nums": [46,40,16,48,24,1,13,15,6,5,12,15,4] }
assert my_solution.maximumSum(**test_input) == 100
test_input = { "nums": [11,27,28,26,4,22,10,49,4,23,30,6,5] }
assert my_solution.maximumSum(**test_input) == 76
test_input = { "nums": [37,17,17,18,10,28,47,38,43,20,10] }
assert my_solution.maximumSum(**test_input) == 98
test_input = { "nums": [12,17,9,30,38,20,28,36,34,15,4,15,48] }
assert my_solution.maximumSum(**test_input) == 76
test_input = { "nums": [28,32,22,9,33,26,10,15,15,37,33,48,2,14,35] }
assert my_solution.maximumSum(**test_input) == 70
test_input = { "nums": [35,28,45,34,49,45,38,15,36,33,15,16] }
assert my_solution.maximumSum(**test_input) == 105
test_input = { "nums": [50,18,24,30,6,49,3,20,22,19,25,35,30,33] }
assert my_solution.maximumSum(**test_input) == 102
test_input = { "nums": [10,19,37,1,31,6,2,37,10,1,36,48,7,40] }
assert my_solution.maximumSum(**test_input) == 85
test_input = { "nums": [45,49,32,44,12,39,8,7,3,48,37,27,41,20,18] }
assert my_solution.maximumSum(**test_input) == 92
test_input = { "nums": [40,9,16,40,9,28,29,36,4,17,29] }
assert my_solution.maximumSum(**test_input) == 84
test_input = { "nums": [21,32,24,39,2,13,37,33,50,43,9,43,14] }
assert my_solution.maximumSum(**test_input) == 110
test_input = { "nums": [36,25,21,29,42,40,16,41,22,24,45,7,33] }
assert my_solution.maximumSum(**test_input) == 87
test_input = { "nums": [32,33,7,45,23,13,45,4,15,12] }
assert my_solution.maximumSum(**test_input) == 92
test_input = { "nums": [10,7,1,15,12,22,34,3,36,44,10,12] }
assert my_solution.maximumSum(**test_input) == 61
test_input = { "nums": [26,4,44,3,37,50,27,22,48,14,12,3,39,31] }
assert my_solution.maximumSum(**test_input) == 77
test_input = { "nums": [47,48,8,24,1,17,32,13,19,25,15,30,12] }
assert my_solution.maximumSum(**test_input) == 90
test_input = { "nums": [31,18,11,28,7,34,32,38,47,44,1,13,46] }
assert my_solution.maximumSum(**test_input) == 106
test_input = { "nums": [47,12,35,10,37,36,44,38,19,31,28,21,3,34] }
assert my_solution.maximumSum(**test_input) == 76
test_input = { "nums": [19,47,46,5,7,42,35,3,39,2,1,31] }
assert my_solution.maximumSum(**test_input) == 77
test_input = { "nums": [15,50,14,27,37,44,11,38,23,39,27,36,22] }
assert my_solution.maximumSum(**test_input) == 88
test_input = { "nums": [31,31,18,24,9,27,33,10,23,38,44,4,17,11,14] }
assert my_solution.maximumSum(**test_input) == 78
test_input = { "nums": [38,27,13,20,36,23,6,7,37,20,49,31,25,12] }
assert my_solution.maximumSum(**test_input) == 95
test_input = { "nums": [6,21,11,15,17,47,50,14,24,18,38,19,48,43] }
assert my_solution.maximumSum(**test_input) == 50
test_input = { "nums": [31,4,1,5,50,46,14,17,13,14,17,39,46,18] }
assert my_solution.maximumSum(**test_input) == 50
test_input = { "nums": [39,13,6,45,44,14,44,37,24,20,21,47,6,6,5] }
assert my_solution.maximumSum(**test_input) == 108
test_input = { "nums": [17,16,48,41,7,39,50,29,2,33] }
assert my_solution.maximumSum(**test_input) == 60
test_input = { "nums": [3,8,19,47,29,4,16,31,11,30] }
assert my_solution.maximumSum(**test_input) == 61
test_input = { "nums": [20,1,46,36,35,32,49,14,48,25,17,50,22] }
assert my_solution.maximumSum(**test_input) == 104
test_input = { "nums": [49,20,12,42,33,21,29,30,35,4,5,50] }
assert my_solution.maximumSum(**test_input) == 126
test_input = { "nums": [36,1,8,14,39,2,31,23,10,46,42] }
assert my_solution.maximumSum(**test_input) == 60
test_input = { "nums": [33,29,23,4,48,31,31,26,11,39] }
assert my_solution.maximumSum(**test_input) == 55
test_input = { "nums": [33,36,31,21,2,41,14,36,6,1,22,13,34] }
assert my_solution.maximumSum(**test_input) == 72
test_input = { "nums": [35,32,43,20,4,13,6,19,36,20] }
assert my_solution.maximumSum(**test_input) == 91
test_input = { "nums": [16,41,16,41,11,39,40,7,24,28,13] }
assert my_solution.maximumSum(**test_input) == 81
test_input = { "nums": [32,24,29,24,29,45,10,37,22,35,37,28,15] }
assert my_solution.maximumSum(**test_input) == 78
test_input = { "nums": [45,8,19,1,33,2,32,40,16,33,44,27] }
assert my_solution.maximumSum(**test_input) == 62
test_input = { "nums": [3,42,14,18,1,20,19,7,37,3,2,3,48] }
assert my_solution.maximumSum(**test_input) == 58 | 1,694,917,800 |
biweekly-contest-113-minimum-right-shifts-to-sort-the-array | https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array | minimum-right-shifts-to-sort-the-array | {
"questionId": "3045",
"questionFrontendId": "2855",
"title": "Minimum Right Shifts to Sort the Array",
"titleSlug": "minimum-right-shifts-to-sort-the-array",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 169,
"dislikes": 6,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 下标从 0 开始的数组 nums ,数组中的元素为 互不相同 的正整数。请你返回让 nums 成为递增数组的 最少右移 次数,如果无法得到递增数组,返回 -1 。
一次 右移 指的是同时对所有下标进行操作,将下标为 i 的元素移动到下标 (i + 1) % n 处。
示例 1:
输入:nums = [3,4,5,1,2]
输出:2
解释:
第一次右移后,nums = [2,3,4,5,1] 。
第二次右移后,nums = [1,2,3,4,5] 。
现在 nums 是递增数组了,所以答案为 2 。
示例 2:
输入:nums = [1,3,5]
输出:0
解释:nums 已经是递增数组了,所以答案为 0 。
示例 3:
输入:nums = [2,1,4]
输出:-1
解释:无法将数组变为递增数组。
提示:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 100
* nums 中的整数互不相同。
"""
class Solution:
def minimumRightShifts(self, nums: List[int]) -> int:
| 给你一个长度为 n 下标从 0 开始的数组 nums ,数组中的元素为 互不相同 的正整数。请你返回让 nums 成为递增数组的 最少右移 次数,如果无法得到递增数组,返回 -1 。
一次 右移 指的是同时对所有下标进行操作,将下标为 i 的元素移动到下标 (i + 1) % n 处。
示例 1:
输入:nums = [3,4,5,1,2]
输出:2
解释:
第一次右移后,nums = [2,3,4,5,1] 。
第二次右移后,nums = [1,2,3,4,5] 。
现在 nums 是递增数组了,所以答案为 2 。
示例 2:
输入:nums = [1,3,5]
输出:0
解释:nums 已经是递增数组了,所以答案为 0 。
示例 3:
输入:nums = [2,1,4]
输出:-1
解释:无法将数组变为递增数组。
提示:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 100
* nums 中的整数互不相同。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumRightShifts(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [3,4,5,1,2] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [1,3,5] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [2,1,4] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [31,72,79,25] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [27,33,42,58,81,8,9,17] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [72,13,21,35,52] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [65,73,77,1] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [100,8,14,68,80,84] }
assert my_solution.minimumRightShifts(**test_input) == 5
test_input = { "nums": [53,60,64,69,98,40] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [21] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [78,12,18,21,23,36,64,70] }
assert my_solution.minimumRightShifts(**test_input) == 7
test_input = { "nums": [25,26,53,91,92,99,10,24] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [63,51,65,87,6,17,32,14,42,46] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [43,46,75,76,85,96,9,19,25] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [5] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [35,72,76,82,96,97,24,26] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [82,30,94,55,76,51,3,89,52,96] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [57,59,88,97,6,27,41,46,52] }
assert my_solution.minimumRightShifts(**test_input) == 5
test_input = { "nums": [17] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [62] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [24,46,55,61,71,78,1,4] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [83,2,21,42,73,77,80] }
assert my_solution.minimumRightShifts(**test_input) == 6
test_input = { "nums": [83,94,14,43,50,62,63] }
assert my_solution.minimumRightShifts(**test_input) == 5
test_input = { "nums": [38,46,66,77,7,15,17,35] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [35,68,82,90,9,18,29,34] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [71] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [71,73,88,12,49,55,59,70] }
assert my_solution.minimumRightShifts(**test_input) == 5
test_input = { "nums": [54,65,75,81,24,37] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [57,67,73,78,79,2,45,48,51] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [36,62,65,85,95,9,21] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [68,12] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [34,9,86,20,67,94,65,82,40,79] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [92,84,37,19,16,85,20,79,25,89] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [3,16,38,44,67,79,84] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [14,24,58,69,71,94,13] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [100,18] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [13] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [94,30,53,56,67,72,82] }
assert my_solution.minimumRightShifts(**test_input) == 6
test_input = { "nums": [92,14,65,80,85] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [43,53,81,87,93,19,31,39] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [80,38] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [52,72,78,83,85,99,20] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [3,6,89] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [3] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [55,56,63,91,3,46] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [58,10,31,37,41] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [17,33,53,58,78] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [82,44] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [89,96,35,48,57,71] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [43,69,4,29,37] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [65,88] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [42,44,59,76,86] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [29,56,78,96,1,10,27] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [48,100] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [4,33,17,3,8,91,28,13,72,42] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [5,35,53,56] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [65,67,70,27,41,50,52,57,60] }
assert my_solution.minimumRightShifts(**test_input) == 6
test_input = { "nums": [94,32,45,62] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [23,25,34,47,61,65,6,21] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [99,11,12,21,22,55,62,83] }
assert my_solution.minimumRightShifts(**test_input) == 7
test_input = { "nums": [92,13,33,58,61,85] }
assert my_solution.minimumRightShifts(**test_input) == 5
test_input = { "nums": [46] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [12,27,30,36] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [33,44,57,16,22,26,30] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [67,24] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [12,44,83,87] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [19,52,3,8,12] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [82,86,88,6,35,47,52,58,62] }
assert my_solution.minimumRightShifts(**test_input) == 6
test_input = { "nums": [48] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [60,11] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [69,60] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [22,28,36,16,82,77,41,85,44,97] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [63,94,2,14] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [41,45,74,84,90,93,100,18,31] }
assert my_solution.minimumRightShifts(**test_input) == 2
test_input = { "nums": [21,38,57,64,12] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [99,2,9,17,33,58,59,72] }
assert my_solution.minimumRightShifts(**test_input) == 7
test_input = { "nums": [36,89,90,98,11,14,23] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [84,90,5,57,78] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [48,73,76,30] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [74] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [21,84,35,65,12,74,30,95,46,23] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [64,76,46,53,54] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [77,84,89,47,52,74] }
assert my_solution.minimumRightShifts(**test_input) == 3
test_input = { "nums": [12,29,31,52,88,89,10] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [20,25,28,41,57,89] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [1,28,51,59] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [59,76,2,26,49,78,36,70,64,24] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [35,43,49,63,21] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [1,35,38,47,54,56,58,74] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [49,94,97] }
assert my_solution.minimumRightShifts(**test_input) == 0
test_input = { "nums": [32,30] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [37,36] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [31,41,65,14] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [45,57,73,77,17,30,42,43] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [17,65,11] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [32,84,93,31,61,78,15,52,100,65] }
assert my_solution.minimumRightShifts(**test_input) == -1
test_input = { "nums": [61,72,90,3,8,17,23,55] }
assert my_solution.minimumRightShifts(**test_input) == 5
test_input = { "nums": [19,30,44,95,13] }
assert my_solution.minimumRightShifts(**test_input) == 1
test_input = { "nums": [42,46,66,71,87,3,4,5,14] }
assert my_solution.minimumRightShifts(**test_input) == 4
test_input = { "nums": [13,57,7] }
assert my_solution.minimumRightShifts(**test_input) == 1 | 1,694,874,600 |
biweekly-contest-113-minimum-array-length-after-pair-removals | https://leetcode.com/problems/minimum-array-length-after-pair-removals | minimum-array-length-after-pair-removals | {
"questionId": "3081",
"questionFrontendId": "2856",
"title": "Minimum Array Length After Pair Removals",
"titleSlug": "minimum-array-length-after-pair-removals",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 259,
"dislikes": 88,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的 非递减 整数数组 nums 。
你可以执行以下操作任意次:
* 选择 两个 下标 i 和 j ,满足 i < j 且 nums[i] < nums[j] 。
* 将 nums 中下标在 i 和 j 处的元素删除。剩余元素按照原来的顺序组成新的数组,下标也重新从 0 开始编号。
请你返回一个整数,表示执行以上操作任意次后(可以执行 0 次),nums 数组的 最小 数组长度。
请注意,nums 数组是按 非降序 排序的。
示例 1:
输入:nums = [1,3,4,9]
输出:0
解释:一开始,nums = [1, 3, 4, 9] 。
第一次操作,我们选择下标 0 和 1 ,满足 nums[0] < nums[1] <=> 1 < 3 。
删除下标 0 和 1 处的元素,nums 变成 [4, 9] 。
下一次操作,我们选择下标 0 和 1 ,满足 nums[0] < nums[1] <=> 4 < 9 。
删除下标 0 和 1 处的元素,nums 变成空数组 [] 。
所以,可以得到的最小数组长度为 0 。
示例 2:
输入:nums = [2,3,6,9]
输出:0
解释:一开始,nums = [2, 3, 6, 9] 。
第一次操作,我们选择下标 0 和 2 ,满足 nums[0] < nums[2] <=> 2 < 6 。
删除下标 0 和 2 处的元素,nums 变成 [3, 9] 。
下一次操作,我们选择下标 0 和 1 ,满足 nums[0] < nums[1] <=> 3 < 9 。
删除下标 0 和 1 处的元素,nums 变成空数组 [] 。
所以,可以得到的最小数组长度为 0 。
示例 3:
输入:nums = [1,1,2]
输出:1
解释:一开始,nums = [1, 1, 2] 。
第一次操作,我们选择下标 0 和 2 ,满足 nums[0] < nums[2] <=> 1 < 2 。
删除下标 0 和 2 处的元素,nums 变成 [1] 。
无法对数组再执行操作。
所以,可以得到的最小数组长度为 1 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* nums 是 非递减 数组。
"""
class Solution:
def minLengthAfterRemovals(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始的 非递减 整数数组 nums 。
你可以执行以下操作任意次:
* 选择 两个 下标 i 和 j ,满足 i < j 且 nums[i] < nums[j] 。
* 将 nums 中下标在 i 和 j 处的元素删除。剩余元素按照原来的顺序组成新的数组,下标也重新从 0 开始编号。
请你返回一个整数,表示执行以上操作任意次后(可以执行 0 次),nums 数组的 最小 数组长度。
请注意,nums 数组是按 非降序 排序的。
示例 1:
输入:nums = [1,3,4,9]
输出:0
解释:一开始,nums = [1, 3, 4, 9] 。
第一次操作,我们选择下标 0 和 1 ,满足 nums[0] < nums[1] <=> 1 < 3 。
删除下标 0 和 1 处的元素,nums 变成 [4, 9] 。
下一次操作,我们选择下标 0 和 1 ,满足 nums[0] < nums[1] <=> 4 < 9 。
删除下标 0 和 1 处的元素,nums 变成空数组 [] 。
所以,可以得到的最小数组长度为 0 。
示例 2:
输入:nums = [2,3,6,9]
输出:0
解释:一开始,nums = [2, 3, 6, 9] 。
第一次操作,我们选择下标 0 和 2 ,满足 nums[0] < nums[2] <=> 2 < 6 。
删除下标 0 和 2 处的元素,nums 变成 [3, 9] 。
下一次操作,我们选择下标 0 和 1 ,满足 nums[0] < nums[1] <=> 3 < 9 。
删除下标 0 和 1 处的元素,nums 变成空数组 [] 。
所以,可以得到的最小数组长度为 0 。
示例 3:
输入:nums = [1,1,2]
输出:1
解释:一开始,nums = [1, 1, 2] 。
第一次操作,我们选择下标 0 和 2 ,满足 nums[0] < nums[2] <=> 1 < 2 。
删除下标 0 和 2 处的元素,nums 变成 [1] 。
无法对数组再执行操作。
所以,可以得到的最小数组长度为 1 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* nums 是 非递减 数组。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minLengthAfterRemovals(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,4,9] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [2,3,6,9] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 2
test_input = { "nums": [1,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [2,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 2
test_input = { "nums": [4,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 3
test_input = { "nums": [1,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 3
test_input = { "nums": [2,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [2,3,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [3,4,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 4
test_input = { "nums": [1,1,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [2,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 2
test_input = { "nums": [1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 5
test_input = { "nums": [1,1,2,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,5,6] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,3,3,3,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [2,3,4,4,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 6
test_input = { "nums": [1,1,2,3,4,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,4,4,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,2,2,2,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,2,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 2
test_input = { "nums": [3,4,7,8,9,9] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 7
test_input = { "nums": [1,1,1,1,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,2,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,2,2,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,2,3,3,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,3,4,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,2,3,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 3
test_input = { "nums": [2,2,2,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 8
test_input = { "nums": [1,1,1,1,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,2,2,3,3,4,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,3,4,4,4,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,2,2,2,4,4,4,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [2,3,3,3,4,4,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 9
test_input = { "nums": [1,1,1,1,1,1,1,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 5
test_input = { "nums": [1,1,1,1,2,2,2,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,2,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 3
test_input = { "nums": [1,1,1,5,5,5,5,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 3
test_input = { "nums": [1,1,2,2,2,2,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,2,2,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,2,4,4,5,5,5,6,6] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 10
test_input = { "nums": [1,1,1,1,1,2,2,2,3,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,1,2,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 2
test_input = { "nums": [1,1,2,2,2,3,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,2,2,2,3,3,3,3,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,2,2,4,5,6,6,7,7,8] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 11
test_input = { "nums": [1,1,1,1,1,1,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,2,2,2,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 5
test_input = { "nums": [1,1,1,2,2,2,3,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,2,2,3,4,6,6,8,8] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,2,2,2,2,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,2,2,2,3,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,2,2,2,3,3,3,4,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,2,2,2,3,3,4,5,5,6,6] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 12
test_input = { "nums": [1,1,1,1,1,1,1,1,1,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 6
test_input = { "nums": [1,1,1,1,1,2,2,3,3,4,4,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,2,2,2,2,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 6
test_input = { "nums": [1,1,1,2,2,3,3,3,4,5,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,2,4,4,4,4,4,5,6,6] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,2,2,2,2,2,2,2,2,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 4
test_input = { "nums": [1,1,2,2,2,2,2,3,3,3,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,2,2,2,3,3,4,4,4,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 13
test_input = { "nums": [1,1,1,1,1,1,1,2,2,4,4,4,4] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,1,2,3,3,3,3,3,3,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,2,2,2,2,2,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,2,2,3,3,3,3,4,4,4,6,7] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,2,2,2,2,2,2,3,3,4,6,7,7] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1] }
assert my_solution.minLengthAfterRemovals(**test_input) == 14
test_input = { "nums": [1,1,1,1,1,1,2,2,2,2,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,1,1,2,2,2,2,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 4
test_input = { "nums": [1,1,1,2,2,2,3,3,3,4,4,4,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,2,2,3,3,3,3,4,4,5,5,6] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,2,2,2,3,3,3,5,6,7,9,9,10] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,3,3,3,3,3,3,3,4,4,4,4,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [2,2,2,2,2,2,2,3,3,3,3,3,3,3] }
assert my_solution.minLengthAfterRemovals(**test_input) == 0
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 5
test_input = { "nums": [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2] }
assert my_solution.minLengthAfterRemovals(**test_input) == 3
test_input = { "nums": [1,1,1,1,1,1,2,2,2,4,4,4,5,5,5] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1
test_input = { "nums": [1,1,1,3,5,6,6,7,7,8,8,8,8,9,9] }
assert my_solution.minLengthAfterRemovals(**test_input) == 1 | 1,694,874,600 |
biweekly-contest-113-count-pairs-of-points-with-distance-k | https://leetcode.com/problems/count-pairs-of-points-with-distance-k | count-pairs-of-points-with-distance-k | {
"questionId": "2953",
"questionFrontendId": "2857",
"title": "Count Pairs of Points With Distance k",
"titleSlug": "count-pairs-of-points-with-distance-k",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 238,
"dislikes": 34,
"categoryTitle": "Algorithms"
} | """
给你一个 二维 整数数组 coordinates 和一个整数 k ,其中 coordinates[i] = [xi, yi] 是第 i 个点在二维平面里的坐标。
我们定义两个点 (x1, y1) 和 (x2, y2) 的 距离 为 (x1 XOR x2) + (y1 XOR y2) ,XOR 指的是按位异或运算。
请你返回满足 i < j 且点 i 和点 j之间距离为 k 的点对数目。
示例 1:
输入:coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5
输出:2
解释:以下点对距离为 k :
- (0, 1):(1 XOR 4) + (2 XOR 2) = 5 。
- (2, 3):(1 XOR 5) + (3 XOR 2) = 5 。
示例 2:
输入:coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0
输出:10
解释:任何两个点之间的距离都为 0 ,所以总共有 10 组点对。
提示:
* 2 <= coordinates.length <= 50000
* 0 <= xi, yi <= 106
* 0 <= k <= 100
"""
class Solution:
def countPairs(self, coordinates: List[List[int]], k: int) -> int:
| 给你一个 二维 整数数组 coordinates 和一个整数 k ,其中 coordinates[i] = [xi, yi] 是第 i 个点在二维平面里的坐标。
我们定义两个点 (x1, y1) 和 (x2, y2) 的 距离 为 (x1 XOR x2) + (y1 XOR y2) ,XOR 指的是按位异或运算。
请你返回满足 i < j 且点 i 和点 j之间距离为 k 的点对数目。
示例 1:
输入:coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5
输出:2
解释:以下点对距离为 k :
- (0, 1):(1 XOR 4) + (2 XOR 2) = 5 。
- (2, 3):(1 XOR 5) + (3 XOR 2) = 5 。
示例 2:
输入:coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0
输出:10
解释:任何两个点之间的距离都为 0 ,所以总共有 10 组点对。
提示:
* 2 <= coordinates.length <= 50000
* 0 <= xi, yi <= 106
* 0 <= k <= 100
请完成下面的代码来解决上述问题:
```python
class Solution:
def countPairs(self, coordinates: List[List[int]], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "coordinates": [[1,2],[4,2],[1,3],[5,2]], "k": 5 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "coordinates": [[1,3],[1,3],[1,3],[1,3],[1,3]], "k": 0 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[27,94],[61,68],[47,0],[100,4],[127,89],[61,103],[26,4],[51,54],[91,26],[98,23],[80,74],[19,93]], "k": 95 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "coordinates": [[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]], "k": 21 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "coordinates": [[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]], "k": 39 }
assert my_solution.countPairs(**test_input) == 20
test_input = { "coordinates": [[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]], "k": 54 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[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]], "k": 53 }
assert my_solution.countPairs(**test_input) == 18
test_input = { "coordinates": [[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]], "k": 60 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "coordinates": [[28,14],[2,13],[28,14],[4,7],[23,1],[54,0],[43,22],[98,16]], "k": 33 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "coordinates": [[84,92],[84,92],[84,92],[84,92],[84,92],[54,59],[84,92],[93,44]], "k": 0 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "coordinates": [[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]], "k": 13 }
assert my_solution.countPairs(**test_input) == 19
test_input = { "coordinates": [[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]], "k": 56 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "coordinates": [[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]], "k": 81 }
assert my_solution.countPairs(**test_input) == 17
test_input = { "coordinates": [[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]], "k": 36 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "coordinates": [[24,75],[22,67]], "k": 23 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "coordinates": [[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]], "k": 48 }
assert my_solution.countPairs(**test_input) == 18
test_input = { "coordinates": [[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]], "k": 60 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "coordinates": [[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]], "k": 73 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "coordinates": [[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]], "k": 25 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[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]], "k": 84 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[51,47],[51,47],[8,14],[82,68],[55,85],[8,14],[51,47],[87,97],[75,65],[78,10],[51,47],[87,97],[74,19],[51,47],[56,66],[8,14],[78,10],[74,66],[65,92],[51,47],[3,31]], "k": 0 }
assert my_solution.countPairs(**test_input) == 20
test_input = { "coordinates": [[25,82],[86,89],[25,82],[47,118],[14,58],[22,51],[0,93],[26,9],[67,27],[43,22],[78,49],[82,15],[93,22],[67,34],[54,43],[61,55],[74,77],[115,108],[54,55],[9,30],[31,3],[26,5],[60,49]], "k": 90 }
assert my_solution.countPairs(**test_input) == 22
test_input = { "coordinates": [[29,23],[48,3],[58,62],[16,19],[0,30],[59,5],[96,50],[7,46],[5,18],[42,32],[78,55]], "k": 17 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "coordinates": [[47,68],[55,68],[36,73],[33,70],[36,81],[60,81],[32,18],[38,95],[34,75],[33,5],[33,78],[32,10],[36,93],[56,77],[43,17],[99,70],[15,77],[42,87],[30,18],[36,56],[47,68],[45,70],[48,77],[53,94],[0,86],[53,9],[68,35],[32,77],[95,90]], "k": 24 }
assert my_solution.countPairs(**test_input) == 31
test_input = { "coordinates": [[5,100],[19,21],[83,36],[24,59],[92,49],[6,73],[57,78],[69,33],[3,81],[53,59],[23,40],[6,21],[57,55],[98,43],[33,15],[8,83],[29,29],[85,41],[47,64],[10,32],[82,94],[14,29],[13,99],[19,20],[85,108],[41,9]], "k": 78 }
assert my_solution.countPairs(**test_input) == 12
test_input = { "coordinates": [[8,94],[19,13],[72,75],[17,8],[57,45],[17,15],[14,95],[74,78],[17,15],[9,95],[79,76],[13,91],[28,76],[94,12],[11,90],[94,11],[94,11],[15,89],[20,13],[23,14],[22,8],[21,71]], "k": 7 }
assert my_solution.countPairs(**test_input) == 24
test_input = { "coordinates": [[37,76],[109,71],[66,1],[55,6],[90,22],[71,24],[3,19],[46,24],[74,74],[85,94],[2,96],[1,48],[31,86],[22,78],[93,80],[3,112],[11,11],[98,18],[81,86],[55,54],[82,18],[127,23]], "k": 83 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "coordinates": [[9,25],[56,25],[7,58],[9,48],[77,55],[6,10],[33,98],[22,26],[41,57],[18,4],[40,74]], "k": 49 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[91,12],[86,8],[74,12],[85,58],[65,10],[49,51],[43,83],[34,91],[89,63],[26,44],[68,6],[71,8],[92,12],[49,79],[64,26],[0,87],[22,85],[15,72],[17,54],[33,37],[70,9],[88,95],[85,67],[32,85],[94,69],[87,77]], "k": 17 }
assert my_solution.countPairs(**test_input) == 16
test_input = { "coordinates": [[54,60],[31,62],[76,56],[79,44]], "k": 52 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "coordinates": [[41,13],[15,74],[43,51],[44,10],[49,72],[63,48],[50,40],[90,86],[105,13],[11,118],[55,8],[3,39],[27,3],[55,72],[33,98],[10,59],[40,45],[10,59],[40,30],[97,43],[96,55],[47,32],[43,86],[57,61],[1,64]], "k": 64 }
assert my_solution.countPairs(**test_input) == 23
test_input = { "coordinates": [[29,96],[82,101],[1,88],[9,100],[55,42],[37,77],[89,95],[40,10],[111,114],[89,53],[91,33],[93,18],[90,14],[50,49],[27,91],[99,92],[26,15],[69,17],[61,64]], "k": 84 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "coordinates": [[57,88],[83,2],[82,23],[19,7],[43,84],[54,87],[51,38],[61,68],[68,31],[74,49],[64,80],[2,19],[18,73],[52,73],[75,26],[32,71],[91,83],[84,15],[49,76]], "k": 30 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[34,96],[53,25],[97,70],[48,31],[48,20],[54,26],[42,99],[52,24],[56,100],[35,106],[16,71],[34,69],[42,72],[28,8],[35,97],[103,67],[12,81],[8,86]], "k": 11 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[60,56],[48,34],[21,82],[63,26],[97,51],[35,63],[39,29],[5,46],[16,115],[19,71],[34,54],[6,65],[11,21],[54,66],[2,103],[13,64],[30,73],[23,58],[31,75],[6,63],[16,66],[21,100]], "k": 38 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[5,28],[16,39],[38,16],[21,34],[5,22],[73,52],[3,24],[24,37],[11,26]], "k": 10 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "coordinates": [[34,76],[50,71],[55,74],[36,6],[56,77],[56,86],[9,25],[7,38],[34,76],[96,85],[29,32]], "k": 27 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[69,99],[60,80],[59,72],[74,67],[34,78],[73,95],[65,72],[86,64],[42,89],[90,25],[84,48]], "k": 31 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[50,75],[84,10],[3,1],[8,12],[41,82],[68,39],[55,31],[4,103],[50,19],[15,85],[20,50],[118,81],[47,14],[1,40],[1,58],[8,58],[18,110],[62,10],[98,69],[25,31],[99,10],[74,29],[124,73]], "k": 98 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "coordinates": [[65,100],[43,13],[80,116],[40,82],[50,5],[53,14],[62,16],[38,8],[83,107],[56,11],[82,92],[62,16],[59,21],[38,8],[55,50],[67,76],[36,65]], "k": 33 }
assert my_solution.countPairs(**test_input) == 14
test_input = { "coordinates": [[52,32],[42,21],[1,56],[93,52],[85,87],[14,58],[39,21],[3,105],[18,13],[5,119],[108,77],[91,81],[22,71],[76,39],[2,59],[23,54],[83,26],[28,23],[33,69],[27,91],[92,19],[53,5],[39,32],[14,124]], "k": 83 }
assert my_solution.countPairs(**test_input) == 21
test_input = { "coordinates": [[84,63],[92,55],[56,94],[89,27],[53,93],[85,80],[65,91],[77,16],[28,99],[48,86],[54,44],[33,47],[47,10],[11,62],[2,17]], "k": 16 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "coordinates": [[78,84],[91,79],[1,35],[73,76],[89,92],[69,94],[78,1],[27,71],[17,58],[18,33],[82,67],[24,59],[23,53],[82,86]], "k": 21 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[29,53],[40,74],[42,73],[24,53],[79,50],[13,7],[43,72],[26,54],[41,75],[66,27],[43,72],[81,75],[47,73],[74,43],[97,60],[42,76],[46,77],[21,69],[88,77]], "k": 5 }
assert my_solution.countPairs(**test_input) == 16
test_input = { "coordinates": [[21,95],[53,15],[71,7],[22,40],[8,89],[66,62]], "k": 74 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "coordinates": [[93,3],[89,13],[70,48],[75,6],[43,82],[121,49],[80,1],[122,45],[57,45],[96,96],[86,82],[46,62],[63,79],[10,6],[55,36],[63,61],[79,99]], "k": 92 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[0,36],[77,49],[25,41]], "k": 98 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "coordinates": [[42,18],[48,0],[64,62],[61,7],[33,51],[50,26],[1,91],[24,92]], "k": 44 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "coordinates": [[69,94],[83,39],[2,37],[117,117],[82,54],[20,84],[91,88],[67,63],[43,69],[109,42],[9,69],[46,42],[60,99],[69,74],[81,80],[12,19]], "k": 91 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "coordinates": [[75,44],[90,42],[62,96],[80,91],[82,78],[77,42]], "k": 23 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "coordinates": [[81,20],[74,53],[70,49],[99,66],[11,88]], "k": 60 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "coordinates": [[33,37],[35,52],[49,38],[47,32],[98,98],[84,83],[50,54],[45,34],[105,106],[54,44],[80,57],[96,80],[83,81],[36,22]], "k": 19 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "coordinates": [[45,38],[47,5],[13,69],[88,65],[123,11],[15,30],[91,45],[66,100],[25,50],[63,10],[46,70],[36,77],[27,9],[78,91]], "k": 98 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "coordinates": [[71,58],[60,37],[27,97],[7,56],[56,126],[24,59],[46,76],[15,79],[18,3],[98,8],[110,62],[76,30],[38,63]], "k": 66 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[21,80],[17,111],[0,126],[20,81],[50,76],[80,32],[7,97],[21,19],[50,91],[58,68],[55,4],[37,56],[20,42],[6,35],[38,72],[96,6],[11,70],[10,91],[11,94],[46,88],[81,64],[37,78],[15,75],[90,79],[13,103],[46,66],[2,95]], "k": 67 }
assert my_solution.countPairs(**test_input) == 26
test_input = { "coordinates": [[65,15],[73,72],[60,97],[101,107],[3,2],[4,20],[90,74],[71,7],[113,95],[39,17],[87,56],[2,76],[27,122],[48,41]], "k": 79 }
assert my_solution.countPairs(**test_input) == 9
test_input = { "coordinates": [[82,41],[27,65],[94,92],[15,82],[56,69],[30,57],[28,28],[5,53],[100,2],[112,44],[23,6],[92,29],[18,69],[124,26],[125,88],[97,54],[7,31],[50,80]], "k": 39 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "coordinates": [[72,31],[86,19],[63,97],[11,118],[8,67],[14,6],[6,69],[51,1],[70,34],[98,68],[84,29],[47,37],[94,75],[73,15],[34,59],[71,42],[45,98],[22,52],[70,94],[67,78],[64,110],[104,5],[65,28],[87,100],[93,10]], "k": 75 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[90,16],[30,5],[16,71],[21,75],[33,55],[76,76],[16,50],[19,42],[18,59],[30,46],[6,21],[19,73],[35,78],[36,98],[30,77],[6,65],[87,31],[69,46],[62,42],[14,50],[44,29],[86,56]], "k": 17 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "coordinates": [[27,30],[15,52],[26,30],[26,30],[15,53],[75,57],[27,30],[95,67],[26,31],[27,31],[15,53],[90,84],[27,30],[90,85],[10,3],[48,59]], "k": 1 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "coordinates": [[6,12],[53,6],[16,65],[22,42],[66,85]], "k": 54 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "coordinates": [[45,11],[43,19],[35,27],[43,13],[38,28],[41,59],[68,39],[29,47]], "k": 30 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "coordinates": [[39,98],[1,97],[41,90],[1,83],[65,2],[7,27],[79,51],[124,88],[32,97]], "k": 87 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "coordinates": [[54,49],[98,5],[98,25],[75,53],[117,42],[111,6],[31,85],[124,49],[120,115]], "k": 70 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "coordinates": [[33,9],[59,5],[71,12],[36,2],[6,92],[32,81],[45,72],[54,67],[17,83],[64,19],[24,68],[58,56],[69,87],[76,23],[86,14],[40,25],[50,38],[50,71]], "k": 38 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[7,7],[44,51],[93,41],[43,37],[31,2],[39,52],[12,68],[92,78],[59,78],[95,70],[62,45],[30,79],[7,17],[3,89],[60,35]], "k": 29 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "coordinates": [[77,91],[3,84],[91,18],[83,18],[56,94],[92,19],[69,83],[88,0],[73,95],[65,87],[95,89],[90,90],[19,36],[94,1],[20,18],[14,62],[77,62],[76,92],[14,55],[22,39],[75,95],[94,17],[21,38]], "k": 8 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[27,49],[44,38],[99,7],[32,33],[60,98],[98,84],[93,89],[85,80]], "k": 95 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "coordinates": [[86,74],[117,67],[106,78],[66,82],[15,75],[76,72],[116,64],[85,51],[109,87],[75,69],[103,89],[80,20],[101,95],[124,76],[91,53],[100,84],[112,108],[45,94],[14,96]], "k": 44 }
assert my_solution.countPairs(**test_input) == 19
test_input = { "coordinates": [[43,81],[53,103],[106,66],[75,67],[88,96],[112,90],[23,87],[26,70],[75,78],[102,100],[82,15],[69,5],[32,106],[38,116],[10,32],[48,46],[7,93],[61,43],[11,38],[4,99],[58,4],[29,10],[28,6],[40,80],[7,110],[95,91],[24,56],[92,53]], "k": 84 }
assert my_solution.countPairs(**test_input) == 19
test_input = { "coordinates": [[28,78],[90,77],[51,40],[67,125],[31,62],[19,116],[3,79],[61,5],[39,7],[27,9],[56,33],[100,69],[30,72],[0,66],[17,54],[123,6],[87,72],[11,25],[24,49],[103,81],[37,58],[26,53],[23,45],[120,1],[39,96],[58,84],[97,5]], "k": 73 }
assert my_solution.countPairs(**test_input) == 17
test_input = { "coordinates": [[63,22],[10,98],[61,3],[7,4],[0,111],[56,17],[50,11],[30,97],[16,2],[59,77],[4,48],[42,94],[63,1],[42,3],[13,9],[27,100],[60,30],[1,34],[54,43],[3,32],[15,60],[39,9],[52,82],[19,7],[42,82],[88,96]], "k": 23 }
assert my_solution.countPairs(**test_input) == 18
test_input = { "coordinates": [[76,84],[58,43],[15,66],[83,35],[38,10],[12,44],[70,34],[20,36],[13,29],[17,24],[53,100]], "k": 61 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "coordinates": [[5,32],[28,98],[26,96],[30,100],[29,101],[32,50],[0,73],[29,101],[65,92],[54,15],[1,36],[68,46],[98,62],[67,90],[28,98],[12,81],[16,83],[55,77],[49,14],[0,12],[25,101],[27,99],[4,47],[19,99],[63,62],[56,92]], "k": 8 }
assert my_solution.countPairs(**test_input) == 18
test_input = { "coordinates": [[95,54],[53,94],[90,47],[89,90],[90,47],[73,36],[73,84],[72,49],[63,91],[39,66],[57,80],[80,59]], "k": 30 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[66,53],[64,2],[94,55],[85,23],[74,7],[18,83],[32,95],[55,13],[81,34],[25,125],[73,75],[49,32],[57,19],[0,19],[72,79],[65,8],[118,38],[44,44],[68,16],[62,62],[0,116],[60,21]], "k": 57 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "coordinates": [[38,73],[37,117],[95,92],[28,22],[16,64],[53,0],[65,85],[91,16],[82,28],[57,9],[53,75],[47,45],[30,43],[91,47],[56,94],[53,39]], "k": 63 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "coordinates": [[11,11],[96,86],[86,64],[94,11],[121,100],[68,1],[84,54],[21,40],[8,3],[96,44],[96,127],[42,25],[43,119],[94,10],[71,0],[84,96],[79,73],[37,11],[74,15],[4,53],[27,59],[0,67]], "k": 83 }
assert my_solution.countPairs(**test_input) == 13
test_input = { "coordinates": [[0,8],[45,94],[87,72],[12,98],[4,16],[91,88],[26,100],[8,31],[56,89],[13,54],[22,26],[2,18],[7,36],[19,13],[61,72],[44,10],[44,87],[1,38],[25,23],[24,36],[21,50],[27,13],[95,68],[15,13],[54,68],[5,62]], "k": 28 }
assert my_solution.countPairs(**test_input) == 17
test_input = { "coordinates": [[97,95],[100,90],[99,87],[100,80],[102,82],[4,7],[0,69],[99,89]], "k": 10 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "coordinates": [[22,68],[75,70],[67,78]], "k": 95 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "coordinates": [[36,33],[73,78],[41,27],[58,34],[10,67]], "k": 80 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "coordinates": [[2,37],[39,2],[12,57],[33,38],[73,36],[85,22],[9,95],[31,64],[22,3]], "k": 76 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "coordinates": [[44,0],[95,53],[37,6],[40,4],[5,73],[33,2],[16,71],[36,8],[87,50],[31,71],[83,57],[4,31],[35,79],[12,70],[93,55],[21,77],[97,9],[95,53],[10,73],[78,100],[22,48],[87,50],[74,64]], "k": 15 }
assert my_solution.countPairs(**test_input) == 17
test_input = { "coordinates": [[16,39],[17,57],[14,38],[22,62],[69,40],[2,53],[23,63],[20,35],[25,49]], "k": 31 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "coordinates": [[0,46],[13,69],[38,80],[60,17],[72,83],[27,78],[21,9],[9,29],[84,39],[59,117],[79,65],[1,116],[90,71],[53,91],[46,3],[100,73],[105,23],[12,81],[113,84],[111,25],[27,1],[48,49],[51,53],[93,83],[48,29],[27,21],[9,71]], "k": 91 }
assert my_solution.countPairs(**test_input) == 19
test_input = { "coordinates": [[50,93],[12,98],[26,22],[50,19],[20,70],[53,119],[1,127],[38,100],[52,116],[89,71],[9,98],[34,94],[12,98],[29,119],[60,29],[97,81],[102,84],[13,15],[10,28],[40,26],[16,87],[45,83],[55,83],[62,35],[30,94],[7,75],[14,86],[16,12],[73,88],[60,124]], "k": 78 }
assert my_solution.countPairs(**test_input) == 26
test_input = { "coordinates": [[19,26],[2,28],[3,10],[42,61],[56,56]], "k": 23 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "coordinates": [[56,55],[42,83],[35,97],[28,32],[52,76],[34,20],[68,88],[90,38],[99,76],[32,20],[22,85],[50,34],[4,11],[17,92],[59,80],[66,65],[47,60]], "k": 59 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "coordinates": [[87,78],[72,88],[82,69],[88,79],[36,24],[42,15],[66,94],[32,10],[92,71],[46,89],[74,86],[37,23],[61,44],[66,87],[35,17],[91,78],[43,15],[61,75],[62,70],[61,70],[34,7],[85,64],[35,20],[42,22],[41,27],[82,85],[90,89],[41,13]], "k": 16 }
assert my_solution.countPairs(**test_input) == 20
test_input = { "coordinates": [[48,86],[98,33],[46,68],[91,21],[39,73]], "k": 22 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "coordinates": [[71,47],[68,44],[65,45],[97,43],[97,45],[97,45],[71,41],[103,43],[96,20],[99,41],[57,4],[17,77],[68,44],[16,72],[17,75],[64,69],[19,75],[99,41],[2,21],[71,47],[91,4],[57,2]], "k": 6 }
assert my_solution.countPairs(**test_input) == 21
test_input = { "coordinates": [[5,11],[16,87],[48,55],[26,15],[41,58],[12,14],[81,66],[30,5]], "k": 14 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "coordinates": [[85,89],[119,89],[34,16],[54,41],[55,29],[33,34],[54,30],[80,74],[12,92],[42,49],[69,7],[47,13],[26,38],[39,96],[61,58],[24,48],[46,47]], "k": 34 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "coordinates": [[35,45],[58,17],[64,60],[117,23],[18,63],[26,55],[65,54]], "k": 85 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "coordinates": [[60,1],[57,6],[39,3],[58,7],[61,14],[19,80],[46,0],[84,35],[43,3],[46,4],[48,71],[48,75],[85,40],[46,45],[6,20],[35,7],[57,6],[51,78],[68,25],[17,0]], "k": 12 }
assert my_solution.countPairs(**test_input) == 12
test_input = { "coordinates": [[95,0],[36,24],[68,27],[80,14],[39,2],[93,52],[107,52],[86,63],[82,13],[55,14],[8,52],[99,20],[101,36],[50,70],[26,98],[95,41]], "k": 54 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "coordinates": [[43,14],[55,83],[33,89],[44,74],[46,84],[51,87],[61,69],[1,89]], "k": 32 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "coordinates": [[88,15],[93,65],[52,39],[20,24],[100,36],[39,17],[26,77],[52,39],[47,83],[98,99],[43,28],[72,29],[21,48],[43,32],[60,108],[44,47],[45,125],[84,94]], "k": 83 }
assert my_solution.countPairs(**test_input) == 13
test_input = { "coordinates": [[12,2],[43,87],[21,100],[79,63],[5,6],[70,75],[20,55],[23,55],[17,31],[121,89],[27,71],[27,22],[42,34],[15,14],[16,40],[49,68],[30,48],[45,43],[88,23],[47,15],[16,41],[8,5]], "k": 81 }
assert my_solution.countPairs(**test_input) == 8 | 1,694,874,600 |
biweekly-contest-113-minimum-edge-reversals-so-every-node-is-reachable | https://leetcode.com/problems/minimum-edge-reversals-so-every-node-is-reachable | minimum-edge-reversals-so-every-node-is-reachable | {
"questionId": "3105",
"questionFrontendId": "2858",
"title": "Minimum Edge Reversals So Every Node Is Reachable",
"titleSlug": "minimum-edge-reversals-so-every-node-is-reachable",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 227,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
给你一个 n 个点的 简单有向图 (没有重复边的有向图),节点编号为 0 到 n - 1 。如果这些边是双向边,那么这个图形成一棵 树 。
给你一个整数 n 和一个 二维 整数数组 edges ,其中 edges[i] = [ui, vi] 表示从节点 ui 到节点 vi 有一条 有向边 。
边反转 指的是将一条边的方向反转,也就是说一条从节点 ui 到节点 vi 的边会变为一条从节点 vi 到节点 ui 的边。
对于范围 [0, n - 1] 中的每一个节点 i ,你的任务是分别 独立 计算 最少 需要多少次 边反转 ,从节点 i 出发经过 一系列有向边 ,可以到达所有的节点。
请你返回一个长度为 n 的整数数组 answer ,其中 answer[i]表示从节点 i 出发,可以到达所有节点的 最少边反转 次数。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/26/image-20230826221104-3.png]
输入:n = 4, edges = [[2,0],[2,1],[1,3]]
输出:[1,1,0,2]
解释:上图表示了与输入对应的简单有向图。
对于节点 0 :反转 [2,0] ,从节点 0 出发可以到达所有节点。
所以 answer[0] = 1 。
对于节点 1 :反转 [2,1] ,从节点 1 出发可以到达所有节点。
所以 answer[1] = 1 。
对于节点 2 :不需要反转就可以从节点 2 出发到达所有节点。
所以 answer[2] = 0 。
对于节点 3 :反转 [1,3] 和 [2,1] ,从节点 3 出发可以到达所有节点。
所以 answer[3] = 2 。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/26/image-20230826225541-2.png]
输入:n = 3, edges = [[1,2],[2,0]]
输出:[2,0,1]
解释:上图表示了与输入对应的简单有向图。
对于节点 0 :反转 [2,0] 和 [1,2] ,从节点 0 出发可以到达所有节点。
所以 answer[0] = 2 。
对于节点 1 :不需要反转就可以从节点 2 出发到达所有节点。
所以 answer[1] = 0 。
对于节点 2 :反转 [1,2] ,从节点 2 出发可以到达所有节点。
所以 answer[2] = 1 。
提示:
* 2 <= n <= 105
* edges.length == n - 1
* edges[i].length == 2
* 0 <= ui == edges[i][0] < n
* 0 <= vi == edges[i][1] < n
* ui != vi
* 输入保证如果边是双向边,可以得到一棵树。
"""
class Solution:
def minEdgeReversals(self, n: int, edges: List[List[int]]) -> List[int]:
| 给你一个 n 个点的 简单有向图 (没有重复边的有向图),节点编号为 0 到 n - 1 。如果这些边是双向边,那么这个图形成一棵 树 。
给你一个整数 n 和一个 二维 整数数组 edges ,其中 edges[i] = [ui, vi] 表示从节点 ui 到节点 vi 有一条 有向边 。
边反转 指的是将一条边的方向反转,也就是说一条从节点 ui 到节点 vi 的边会变为一条从节点 vi 到节点 ui 的边。
对于范围 [0, n - 1] 中的每一个节点 i ,你的任务是分别 独立 计算 最少 需要多少次 边反转 ,从节点 i 出发经过 一系列有向边 ,可以到达所有的节点。
请你返回一个长度为 n 的整数数组 answer ,其中 answer[i]表示从节点 i 出发,可以到达所有节点的 最少边反转 次数。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/26/image-20230826221104-3.png]
输入:n = 4, edges = [[2,0],[2,1],[1,3]]
输出:[1,1,0,2]
解释:上图表示了与输入对应的简单有向图。
对于节点 0 :反转 [2,0] ,从节点 0 出发可以到达所有节点。
所以 answer[0] = 1 。
对于节点 1 :反转 [2,1] ,从节点 1 出发可以到达所有节点。
所以 answer[1] = 1 。
对于节点 2 :不需要反转就可以从节点 2 出发到达所有节点。
所以 answer[2] = 0 。
对于节点 3 :反转 [1,3] 和 [2,1] ,从节点 3 出发可以到达所有节点。
所以 answer[3] = 2 。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/26/image-20230826225541-2.png]
输入:n = 3, edges = [[1,2],[2,0]]
输出:[2,0,1]
解释:上图表示了与输入对应的简单有向图。
对于节点 0 :反转 [2,0] 和 [1,2] ,从节点 0 出发可以到达所有节点。
所以 answer[0] = 2 。
对于节点 1 :不需要反转就可以从节点 2 出发到达所有节点。
所以 answer[1] = 0 。
对于节点 2 :反转 [1,2] ,从节点 2 出发可以到达所有节点。
所以 answer[2] = 1 。
提示:
* 2 <= n <= 105
* edges.length == n - 1
* edges[i].length == 2
* 0 <= ui == edges[i][0] < n
* 0 <= vi == edges[i][1] < n
* ui != vi
* 输入保证如果边是双向边,可以得到一棵树。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minEdgeReversals(self, n: int, edges: List[List[int]]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "n": 4, "edges": [[2,0],[2,1],[1,3]] }
assert my_solution.minEdgeReversals(**test_input) == [1,1,0,2]
test_input = { "n": 3, "edges": [[1,2],[2,0]] }
assert my_solution.minEdgeReversals(**test_input) == [2,0,1]
test_input = { "n": 2, "edges": [[0,1]] }
assert my_solution.minEdgeReversals(**test_input) == [0,1]
test_input = { "n": 3, "edges": [[2,0],[2,1]] }
assert my_solution.minEdgeReversals(**test_input) == [1,1,0]
test_input = { "n": 4, "edges": [[0,1],[3,0],[2,3]] }
assert my_solution.minEdgeReversals(**test_input) == [2,3,0,1]
test_input = { "n": 4, "edges": [[0,2],[0,3],[3,1]] }
assert my_solution.minEdgeReversals(**test_input) == [0,2,1,1]
test_input = { "n": 4, "edges": [[0,3],[1,2],[2,3]] }
assert my_solution.minEdgeReversals(**test_input) == [2,1,2,3]
test_input = { "n": 4, "edges": [[0,3],[1,2],[3,1]] }
assert my_solution.minEdgeReversals(**test_input) == [0,2,3,1]
test_input = { "n": 4, "edges": [[0,3],[2,1],[3,1]] }
assert my_solution.minEdgeReversals(**test_input) == [1,3,2,2]
test_input = { "n": 4, "edges": [[1,0],[2,0],[3,2]] }
assert my_solution.minEdgeReversals(**test_input) == [3,2,2,1]
test_input = { "n": 5, "edges": [[0,1],[0,4],[2,3],[4,2]] }
assert my_solution.minEdgeReversals(**test_input) == [0,1,2,3,1]
test_input = { "n": 5, "edges": [[0,1],[2,0],[0,4],[3,4]] }
assert my_solution.minEdgeReversals(**test_input) == [2,3,1,2,3]
test_input = { "n": 5, "edges": [[0,2],[0,1],[3,1],[4,1]] }
assert my_solution.minEdgeReversals(**test_input) == [2,3,3,2,2]
test_input = { "n": 5, "edges": [[0,2],[0,1],[3,1],[4,3]] }
assert my_solution.minEdgeReversals(**test_input) == [2,3,3,2,1]
test_input = { "n": 5, "edges": [[0,2],[1,3],[1,4],[2,4]] }
assert my_solution.minEdgeReversals(**test_input) == [1,2,2,3,3]
test_input = { "n": 5, "edges": [[0,2],[1,3],[2,3],[4,2]] }
assert my_solution.minEdgeReversals(**test_input) == [2,3,3,4,2]
test_input = { "n": 5, "edges": [[0,2],[2,1],[2,3],[4,3]] }
assert my_solution.minEdgeReversals(**test_input) == [1,3,2,3,2]
test_input = { "n": 5, "edges": [[0,3],[0,4],[1,2],[4,1]] }
assert my_solution.minEdgeReversals(**test_input) == [0,2,3,1,1]
test_input = { "n": 5, "edges": [[1,0],[3,0],[0,4],[2,3]] }
assert my_solution.minEdgeReversals(**test_input) == [3,2,1,2,4]
test_input = { "n": 5, "edges": [[3,0],[0,4],[1,3],[2,3]] }
assert my_solution.minEdgeReversals(**test_input) == [3,1,1,2,4]
test_input = { "n": 6, "edges": [[0,1],[0,5],[1,2],[3,2],[2,4]] }
assert my_solution.minEdgeReversals(**test_input) == [1,2,3,2,4,2]
test_input = { "n": 6, "edges": [[0,1],[2,1],[1,5],[2,3],[4,3]] }
assert my_solution.minEdgeReversals(**test_input) == [2,3,2,3,2,4]
test_input = { "n": 6, "edges": [[0,2],[0,3],[0,1],[0,5],[1,4]] }
assert my_solution.minEdgeReversals(**test_input) == [0,1,1,1,2,1]
test_input = { "n": 6, "edges": [[0,2],[1,3],[1,2],[2,4],[2,5]] }
assert my_solution.minEdgeReversals(**test_input) == [1,1,2,2,3,3]
test_input = { "n": 6, "edges": [[0,4],[1,2],[3,1],[5,1],[4,5]] }
assert my_solution.minEdgeReversals(**test_input) == [1,4,5,3,2,3]
test_input = { "n": 6, "edges": [[1,0],[0,4],[2,4],[2,3],[3,5]] }
assert my_solution.minEdgeReversals(**test_input) == [2,1,2,3,3,4]
test_input = { "n": 6, "edges": [[1,0],[3,1],[1,4],[2,5],[4,5]] }
assert my_solution.minEdgeReversals(**test_input) == [3,2,3,1,3,4]
test_input = { "n": 7, "edges": [[0,5],[2,0],[1,3],[6,2],[4,3],[3,5]] }
assert my_solution.minEdgeReversals(**test_input) == [5,4,4,5,4,6,3]
test_input = { "n": 7, "edges": [[0,6],[2,1],[6,1],[2,5],[5,3],[6,4]] }
assert my_solution.minEdgeReversals(**test_input) == [1,3,2,4,3,3,2]
test_input = { "n": 7, "edges": [[5,0],[2,0],[6,1],[2,4],[2,3],[3,6]] }
assert my_solution.minEdgeReversals(**test_input) == [2,4,1,2,2,1,3]
test_input = { "n": 8, "edges": [[0,4],[0,3],[7,0],[1,4],[2,5],[2,3],[6,5]] }
assert my_solution.minEdgeReversals(**test_input) == [4,4,4,5,5,5,4,3]
test_input = { "n": 8, "edges": [[0,5],[1,0],[1,6],[1,2],[2,3],[2,7],[4,7]] }
assert my_solution.minEdgeReversals(**test_input) == [2,1,2,3,2,3,2,3]
test_input = { "n": 8, "edges": [[1,0],[7,1],[2,6],[5,2],[3,6],[5,4],[5,7]] }
assert my_solution.minEdgeReversals(**test_input) == [4,3,2,2,2,1,3,2]
test_input = { "n": 8, "edges": [[2,0],[5,0],[3,1],[1,4],[6,2],[4,5],[7,5]] }
assert my_solution.minEdgeReversals(**test_input) == [7,4,6,3,5,6,5,5]
test_input = { "n": 8, "edges": [[4,0],[0,1],[1,3],[3,2],[6,2],[2,7],[5,7]] }
assert my_solution.minEdgeReversals(**test_input) == [3,4,6,5,2,6,5,7]
test_input = { "n": 8, "edges": [[4,0],[7,1],[2,3],[7,2],[5,3],[4,6],[7,6]] }
assert my_solution.minEdgeReversals(**test_input) == [3,3,3,4,2,3,3,2]
test_input = { "n": 9, "edges": [[0,5],[0,2],[0,7],[0,4],[7,1],[3,2],[6,3],[8,4]] }
assert my_solution.minEdgeReversals(**test_input) == [3,5,4,3,4,4,2,4,3]
test_input = { "n": 9, "edges": [[0,5],[0,6],[0,2],[5,1],[7,2],[4,2],[3,6],[8,4]] }
assert my_solution.minEdgeReversals(**test_input) == [4,6,5,4,4,5,5,4,3]
test_input = { "n": 9, "edges": [[0,5],[7,0],[1,6],[1,3],[2,8],[3,4],[3,7],[7,8]] }
assert my_solution.minEdgeReversals(**test_input) == [4,1,3,2,3,5,2,3,4]
test_input = { "n": 9, "edges": [[2,0],[1,6],[1,5],[2,3],[2,5],[4,5],[5,7],[8,7]] }
assert my_solution.minEdgeReversals(**test_input) == [4,3,3,4,3,4,4,5,4]
test_input = { "n": 9, "edges": [[7,0],[0,6],[1,2],[2,6],[3,6],[4,5],[5,8],[8,6]] }
assert my_solution.minEdgeReversals(**test_input) == [7,6,7,7,5,6,8,6,7]
test_input = { "n": 10, "edges": [[0,1],[0,2],[2,4],[6,2],[3,9],[5,4],[4,8],[5,7],[9,6]] }
assert my_solution.minEdgeReversals(**test_input) == [4,5,5,2,6,5,4,6,7,3]
test_input = { "n": 10, "edges": [[0,3],[0,5],[0,1],[8,1],[6,1],[2,3],[4,6],[9,4],[5,7]] }
assert my_solution.minEdgeReversals(**test_input) == [5,6,5,6,4,6,5,7,5,3]
test_input = { "n": 10, "edges": [[0,5],[1,3],[1,9],[2,7],[4,2],[6,3],[3,4],[5,4],[7,8]] }
assert my_solution.minEdgeReversals(**test_input) == [3,3,6,4,5,4,3,7,8,4]
test_input = { "n": 10, "edges": [[1,0],[0,5],[6,0],[2,3],[4,2],[2,6],[8,2],[2,9],[8,7]] }
assert my_solution.minEdgeReversals(**test_input) == [5,4,3,4,2,6,4,3,2,4]
test_input = { "n": 10, "edges": [[6,0],[7,0],[4,1],[1,9],[2,7],[9,2],[3,7],[5,7],[8,9]] }
assert my_solution.minEdgeReversals(**test_input) == [9,5,7,7,4,7,8,8,5,6]
test_input = { "n": 10, "edges": [[7,0],[1,2],[1,9],[2,3],[2,8],[5,3],[4,6],[6,7],[7,9]] }
assert my_solution.minEdgeReversals(**test_input) == [5,4,5,6,2,5,3,4,6,5]
test_input = { "n": 10, "edges": [[7,0],[1,5],[2,6],[8,2],[7,3],[4,5],[5,6],[9,7],[8,9]] }
assert my_solution.minEdgeReversals(**test_input) == [6,3,4,6,3,4,5,5,3,4]
test_input = { "n": 11, "edges": [[0,1],[1,2],[8,1],[2,4],[3,9],[3,7],[6,4],[5,7],[5,6],[10,8]] }
assert my_solution.minEdgeReversals(**test_input) == [5,6,7,6,8,6,7,7,5,7,4]
test_input = { "n": 11, "edges": [[0,3],[0,2],[1,4],[1,5],[2,9],[3,5],[7,6],[7,9],[7,8],[8,10]] }
assert my_solution.minEdgeReversals(**test_input) == [2,3,3,3,4,4,4,3,4,4,5]
test_input = { "n": 11, "edges": [[0,3],[0,7],[0,9],[0,10],[1,4],[4,2],[2,6],[6,3],[5,6],[7,8]] }
assert my_solution.minEdgeReversals(**test_input) == [5,2,4,6,3,4,5,6,7,6,6]
test_input = { "n": 11, "edges": [[2,0],[0,9],[1,5],[2,8],[3,5],[3,8],[9,4],[9,6],[6,10],[7,10]] }
assert my_solution.minEdgeReversals(**test_input) == [4,3,3,3,6,4,6,6,4,5,7]
test_input = { "n": 11, "edges": [[6,0],[0,1],[1,4],[2,8],[9,3],[4,10],[5,8],[5,6],[6,9],[10,7]] }
assert my_solution.minEdgeReversals(**test_input) == [3,4,1,4,5,1,2,7,2,3,6]
test_input = { "n": 11, "edges": [[7,0],[1,0],[1,6],[5,1],[4,2],[8,2],[9,2],[7,3],[5,9],[10,5]] }
assert my_solution.minEdgeReversals(**test_input) == [6,5,6,6,5,4,6,5,5,5,3]
test_input = { "n": 11, "edges": [[8,0],[6,1],[7,1],[1,10],[3,2],[4,3],[3,8],[5,8],[8,9],[9,10]] }
assert my_solution.minEdgeReversals(**test_input) == [7,7,6,5,4,5,6,6,6,7,8]
test_input = { "n": 11, "edges": [[10,0],[1,2],[1,10],[2,8],[3,9],[3,5],[4,6],[7,4],[4,8],[5,8]] }
assert my_solution.minEdgeReversals(**test_input) == [6,4,5,4,5,5,6,4,6,5,5]
test_input = { "n": 12, "edges": [[0,10],[1,3],[1,10],[2,7],[2,11],[3,4],[5,9],[5,11],[6,9],[9,8],[9,10]] }
assert my_solution.minEdgeReversals(**test_input) == [5,5,4,6,7,4,4,5,6,5,6,5]
test_input = { "n": 12, "edges": [[3,0],[10,0],[6,0],[1,9],[3,2],[4,6],[4,7],[5,11],[11,7],[8,10],[9,11]] }
assert my_solution.minEdgeReversals(**test_input) == [9,5,9,8,7,6,8,8,7,6,8,7]
test_input = { "n": 12, "edges": [[4,0],[11,0],[1,9],[1,3],[6,2],[2,7],[2,3],[3,10],[4,10],[5,6],[10,8]] }
assert my_solution.minEdgeReversals(**test_input) == [7,5,5,6,6,3,4,6,8,6,7,6]
test_input = { "n": 12, "edges": [[10,0],[0,3],[1,6],[1,8],[1,5],[2,3],[2,4],[3,5],[4,7],[4,11],[10,9]] }
assert my_solution.minEdgeReversals(**test_input) == [3,4,3,4,4,5,5,5,5,3,2,5]
test_input = { "n": 13, "edges": [[0,5],[1,10],[2,4],[2,7],[10,2],[3,5],[5,12],[11,6],[6,12],[8,10],[8,11],[10,9]] }
assert my_solution.minEdgeReversals(**test_input) == [5,4,6,5,7,6,6,7,4,6,5,5,7]
test_input = { "n": 13, "edges": [[0,11],[1,4],[1,8],[1,3],[3,2],[10,3],[12,3],[12,5],[6,11],[10,7],[9,10],[11,12]] }
assert my_solution.minEdgeReversals(**test_input) == [4,6,8,7,7,7,4,7,7,5,6,5,6]
test_input = { "n": 13, "edges": [[5,0],[0,2],[0,9],[10,1],[6,1],[7,2],[2,6],[3,7],[8,4],[4,11],[4,12],[9,12]] }
assert my_solution.minEdgeReversals(**test_input) == [6,9,7,5,7,5,8,6,6,7,8,8,8]
test_input = { "n": 13, "edges": [[7,0],[3,1],[2,4],[2,8],[10,2],[3,10],[9,5],[5,7],[6,11],[6,12],[12,7],[10,12]] }
assert my_solution.minEdgeReversals(**test_input) == [7,4,5,3,6,5,4,6,6,4,4,5,5]
test_input = { "n": 14, "edges": [[0,1],[0,7],[2,6],[2,8],[11,3],[4,12],[4,11],[5,10],[7,11],[13,7],[8,10],[10,9],[9,12]] }
assert my_solution.minEdgeReversals(**test_input) == [7,8,5,10,8,6,6,8,6,8,7,9,9,7]
test_input = { "n": 14, "edges": [[0,1],[1,2],[2,8],[9,2],[6,2],[13,2],[3,4],[5,4],[7,5],[6,10],[12,6],[11,7],[11,10]] }
assert my_solution.minEdgeReversals(**test_input) == [6,7,8,9,10,9,7,8,9,7,8,7,6,7]
test_input = { "n": 14, "edges": [[0,2],[0,3],[0,13],[1,3],[2,4],[8,2],[11,3],[12,3],[4,6],[5,7],[7,9],[7,11],[10,12]] }
assert my_solution.minEdgeReversals(**test_input) == [7,7,8,8,9,5,10,6,7,7,6,7,7,8]
test_input = { "n": 14, "edges": [[0,4],[0,8],[1,3],[1,13],[2,9],[4,11],[4,12],[5,11],[8,6],[13,6],[7,9],[7,8],[10,8]] }
assert my_solution.minEdgeReversals(**test_input) == [6,6,6,7,7,7,8,6,7,7,6,8,8,7]
test_input = { "n": 14, "edges": [[0,6],[5,0],[2,0],[12,1],[13,2],[12,3],[4,7],[5,7],[7,12],[8,13],[9,11],[9,10],[10,13]] }
assert my_solution.minEdgeReversals(**test_input) == [7,9,6,9,6,6,8,7,4,3,4,4,8,5]
test_input = { "n": 14, "edges": [[0,9],[0,11],[0,5],[1,6],[1,11],[7,2],[3,5],[3,12],[3,13],[11,4],[6,10],[9,7],[11,8]] }
assert my_solution.minEdgeReversals(**test_input) == [2,2,5,2,4,3,3,4,4,3,4,3,3,3]
test_input = { "n": 14, "edges": [[0,11],[1,2],[1,10],[1,6],[3,6],[4,11],[10,4],[5,12],[13,6],[8,7],[8,9],[8,12],[8,13]] }
assert my_solution.minEdgeReversals(**test_input) == [7,5,6,5,7,4,6,5,4,5,6,8,5,5]
test_input = { "n": 14, "edges": [[1,0],[1,8],[7,2],[2,11],[2,13],[3,9],[8,3],[3,11],[4,12],[4,8],[5,11],[6,10],[10,12]] }
assert my_solution.minEdgeReversals(**test_input) == [7,6,8,8,6,8,5,7,7,9,6,9,7,9]
test_input = { "n": 14, "edges": [[1,0],[11,0],[6,0],[7,2],[2,8],[10,3],[4,10],[5,12],[12,6],[13,6],[7,10],[9,8],[8,12]] }
assert my_solution.minEdgeReversals(**test_input) == [11,10,7,8,6,8,10,6,8,7,7,10,9,9]
test_input = { "n": 14, "edges": [[3,0],[5,0],[0,7],[1,8],[1,4],[12,2],[4,3],[10,3],[12,5],[13,6],[7,13],[9,10],[10,11]] }
assert my_solution.minEdgeReversals(**test_input) == [7,4,6,6,5,6,10,8,5,4,5,6,5,9]
test_input = { "n": 15, "edges": [[0,5],[1,9],[4,1],[1,5],[14,1],[2,5],[4,3],[4,6],[5,11],[5,7],[10,6],[7,8],[13,7],[14,12]] }
assert my_solution.minEdgeReversals(**test_input) == [6,6,6,6,5,7,6,8,9,7,5,8,6,7,5]
test_input = { "n": 15, "edges": [[0,6],[13,0],[1,2],[10,1],[3,12],[4,11],[5,12],[10,7],[9,7],[7,13],[11,8],[8,9],[12,11],[13,14]] }
assert my_solution.minEdgeReversals(**test_input) == [10,8,9,3,4,3,11,8,6,7,7,5,4,9,10]
test_input = { "n": 15, "edges": [[0,7],[12,1],[5,2],[4,2],[3,8],[3,14],[12,4],[4,7],[5,6],[10,5],[11,5],[6,9],[7,14],[13,12]] }
assert my_solution.minEdgeReversals(**test_input) == [7,7,8,8,7,7,8,8,9,9,6,6,6,5,9]
test_input = { "n": 15, "edges": [[1,0],[10,1],[2,8],[2,3],[5,3],[4,8],[9,5],[6,12],[7,12],[8,7],[9,11],[11,10],[10,13],[10,14]] }
assert my_solution.minEdgeReversals(**test_input) == [7,6,4,5,4,4,6,6,5,3,5,4,7,6,6]
test_input = { "n": 15, "edges": [[2,0],[0,7],[0,5],[1,6],[1,7],[3,4],[8,4],[10,4],[5,12],[5,10],[11,7],[9,13],[10,14],[13,14]] }
assert my_solution.minEdgeReversals(**test_input) == [7,7,6,9,10,8,8,8,9,8,9,7,9,9,10]
test_input = { "n": 15, "edges": [[8,0],[1,0],[1,13],[1,14],[9,2],[3,13],[4,11],[4,14],[5,8],[12,6],[9,6],[7,8],[14,9],[10,13]] }
assert my_solution.minEdgeReversals(**test_input) == [8,7,10,7,7,6,10,6,7,9,7,8,9,8,8]
test_input = { "n": 15, "edges": [[12,0],[0,8],[1,3],[2,1],[2,7],[12,2],[9,3],[4,11],[11,5],[5,7],[6,13],[6,9],[8,14],[10,12]] }
assert my_solution.minEdgeReversals(**test_input) == [7,8,7,9,5,7,7,8,8,8,5,6,6,8,9]
test_input = { "n": 15, "edges": [[12,0],[1,12],[1,7],[2,12],[3,7],[3,14],[4,6],[5,6],[5,7],[6,8],[6,10],[11,6],[7,13],[9,14]] }
assert my_solution.minEdgeReversals(**test_input) == [8,6,6,6,6,6,7,7,8,6,8,6,7,8,7]
test_input = { "n": 16, "edges": [[0,1],[5,0],[0,2],[0,4],[2,12],[10,3],[3,15],[4,7],[8,6],[6,9],[7,14],[7,13],[11,9],[13,9],[10,13]] }
assert my_solution.minEdgeReversals(**test_input) == [5,6,6,8,6,4,8,7,7,9,7,8,7,8,8,9]
test_input = { "n": 16, "edges": [[2,0],[0,4],[4,1],[1,11],[5,2],[8,3],[4,3],[4,13],[14,4],[7,5],[5,12],[6,9],[12,9],[10,14],[11,15]] }
assert my_solution.minEdgeReversals(**test_input) == [7,9,6,9,8,5,6,4,8,7,6,10,6,9,7,11]
test_input = { "n": 16, "edges": [[8,0],[1,6],[10,1],[8,2],[3,15],[14,4],[12,5],[13,5],[8,6],[6,9],[6,13],[11,7],[7,13],[15,10],[11,14]] }
assert my_solution.minEdgeReversals(**test_input) == [8,7,8,4,9,10,8,8,7,9,6,7,9,9,8,5]
test_input = { "n": 17, "edges": [[0,4],[0,9],[13,1],[5,1],[6,2],[13,2],[3,8],[3,15],[3,9],[4,5],[5,10],[11,6],[7,16],[12,9],[16,9],[12,14]] }
assert my_solution.minEdgeReversals(**test_input) == [7,10,10,7,8,9,9,6,8,8,10,8,7,9,8,8,7]
test_input = { "n": 17, "edges": [[0,7],[15,1],[2,3],[2,7],[3,8],[15,3],[5,4],[4,16],[6,14],[16,7],[16,9],[10,14],[10,16],[16,11],[12,14],[15,13]] }
assert my_solution.minEdgeReversals(**test_input) == [8,9,8,9,7,6,7,9,10,9,7,9,7,9,8,8,8]
test_input = { "n": 17, "edges": [[0,10],[12,0],[0,14],[0,7],[0,16],[1,8],[1,7],[2,8],[3,5],[4,15],[5,9],[11,5],[14,6],[11,7],[15,8],[13,16]] }
assert my_solution.minEdgeReversals(**test_input) == [8,8,8,8,7,9,10,9,9,10,9,8,7,8,9,8,9]
test_input = { "n": 17, "edges": [[0,12],[1,2],[1,13],[1,14],[4,3],[3,15],[5,4],[10,6],[14,6],[6,15],[7,11],[8,11],[13,9],[10,11],[15,12],[16,15]] }
assert my_solution.minEdgeReversals(**test_input) == [11,8,9,10,9,8,10,9,9,10,9,10,12,9,9,11,10]
test_input = { "n": 17, "edges": [[0,13],[15,0],[1,5],[1,6],[11,1],[1,13],[8,2],[3,16],[4,16],[7,14],[11,8],[9,10],[9,16],[14,12],[14,15],[15,16]] }
assert my_solution.minEdgeReversals(**test_input) == [8,8,9,7,7,9,9,5,8,7,8,7,7,9,6,7,8]
test_input = { "n": 17, "edges": [[8,0],[0,10],[6,1],[1,15],[15,2],[3,2],[12,3],[9,4],[5,7],[13,7],[9,10],[10,11],[10,14],[13,12],[16,12],[14,15]] }
assert my_solution.minEdgeReversals(**test_input) == [9,11,13,12,10,10,10,11,8,9,10,11,11,10,11,12,10]
test_input = { "n": 17, "edges": [[10,0],[0,5],[1,8],[1,16],[7,2],[2,8],[3,5],[8,3],[4,11],[13,4],[16,6],[16,9],[14,12],[14,13],[14,15],[15,16]] }
assert my_solution.minEdgeReversals(**test_input) == [8,6,6,8,7,9,8,5,7,8,7,8,6,6,5,6,7]
test_input = { "n": 18, "edges": [[0,3],[16,1],[1,10],[2,13],[2,8],[14,3],[4,12],[5,10],[13,6],[7,11],[10,7],[9,7],[14,8],[9,8],[9,17],[12,14],[13,15]] }
assert my_solution.minEdgeReversals(**test_input) == [9,8,9,10,7,8,11,10,10,9,9,11,8,10,9,11,7,10]
test_input = { "n": 18, "edges": [[0,10],[0,17],[1,3],[1,2],[2,9],[2,17],[16,3],[4,7],[4,13],[4,6],[5,12],[5,17],[6,17],[8,15],[11,8],[16,11],[13,14]] }
assert my_solution.minEdgeReversals(**test_input) == [6,5,6,6,5,6,6,6,7,7,7,6,7,6,7,8,5,7]
test_input = { "n": 18, "edges": [[11,0],[5,1],[1,10],[1,3],[1,17],[14,2],[2,13],[3,13],[4,7],[4,12],[4,17],[6,17],[7,8],[15,9],[16,11],[11,14],[14,15]] }
assert my_solution.minEdgeReversals(**test_input) == [7,7,8,8,7,6,7,8,9,9,8,6,8,9,7,8,5,8]
test_input = { "n": 19, "edges": [[0,3],[0,5],[4,0],[12,1],[1,5],[2,17],[2,4],[3,13],[4,8],[5,7],[18,6],[18,8],[14,9],[9,15],[16,9],[10,12],[11,17],[12,16]] }
assert my_solution.minEdgeReversals(**test_input) == [8,8,6,9,7,9,8,10,8,9,6,6,7,10,8,10,8,7,7]
test_input = { "n": 19, "edges": [[0,5],[0,9],[14,1],[2,11],[3,12],[3,8],[10,3],[4,13],[4,12],[6,17],[10,7],[7,14],[8,11],[16,8],[9,14],[9,17],[15,17],[15,18]] }
assert my_solution.minEdgeReversals(**test_input) == [7,10,9,8,8,8,8,8,9,8,7,10,9,9,9,8,8,9,9]
test_input = { "n": 19, "edges": [[0,12],[13,1],[12,1],[14,2],[2,11],[3,9],[11,3],[15,4],[4,11],[5,12],[17,6],[8,7],[8,10],[16,8],[9,12],[11,18],[16,18],[17,18]] }
assert my_solution.minEdgeReversals(**test_input) == [11,13,8,10,8,11,10,11,10,11,11,9,12,12,7,7,9,9,10]
test_input = { "n": 19, "edges": [[0,18],[8,1],[5,1],[9,2],[6,2],[3,11],[17,3],[4,12],[4,6],[18,4],[14,5],[5,16],[6,7],[16,7],[17,7],[15,8],[10,14],[14,13]] }
assert my_solution.minEdgeReversals(**test_input) == [8,11,12,12,10,10,11,12,10,11,8,13,11,10,9,9,11,11,9]
test_input = { "n": 19, "edges": [[1,0],[0,13],[15,0],[1,9],[5,2],[2,13],[3,12],[3,16],[4,12],[5,7],[8,6],[12,8],[16,10],[13,10],[17,11],[13,11],[14,13],[15,18]] }
assert my_solution.minEdgeReversals(**test_input) == [9,8,9,9,9,8,12,9,11,9,11,11,10,10,9,8,10,10,9] | 1,694,874,600 |
weekly-contest-362-points-that-intersect-with-cars | https://leetcode.com/problems/points-that-intersect-with-cars | points-that-intersect-with-cars | {
"questionId": "3034",
"questionFrontendId": "2848",
"title": "Points That Intersect With Cars",
"titleSlug": "points-that-intersect-with-cars",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 211,
"dislikes": 13,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的二维整数数组 nums 表示汽车停放在数轴上的坐标。对于任意下标 i,nums[i] = [starti, endi] ,其中 starti 是第 i 辆车的起点,endi 是第 i 辆车的终点。
返回数轴上被车 任意部分 覆盖的整数点的数目。
示例 1:
输入:nums = [[3,6],[1,5],[4,7]]
输出:7
解释:从 1 到 7 的所有点都至少与一辆车相交,因此答案为 7 。
示例 2:
输入:nums = [[1,3],[5,8]]
输出:7
解释:1、2、3、5、6、7、8 共计 7 个点满足至少与一辆车相交,因此答案为 7 。
提示:
* 1 <= nums.length <= 100
* nums[i].length == 2
* 1 <= starti <= endi <= 100
"""
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
| 给你一个下标从 0 开始的二维整数数组 nums 表示汽车停放在数轴上的坐标。对于任意下标 i,nums[i] = [starti, endi] ,其中 starti 是第 i 辆车的起点,endi 是第 i 辆车的终点。
返回数轴上被车 任意部分 覆盖的整数点的数目。
示例 1:
输入:nums = [[3,6],[1,5],[4,7]]
输出:7
解释:从 1 到 7 的所有点都至少与一辆车相交,因此答案为 7 。
示例 2:
输入:nums = [[1,3],[5,8]]
输出:7
解释:1、2、3、5、6、7、8 共计 7 个点满足至少与一辆车相交,因此答案为 7 。
提示:
* 1 <= nums.length <= 100
* nums[i].length == 2
* 1 <= starti <= endi <= 100
请完成下面的代码来解决上述问题:
```python
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [[3,6],[1,5],[4,7]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[1,3],[5,8]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[4,4],[9,10],[9,10],[3,8]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[2,5],[3,8],[1,6],[4,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[2,3],[3,9],[5,7],[4,10],[9,10]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[4,10]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[1,9],[2,10],[6,7],[8,9],[5,8],[1,3]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[5,10],[3,8],[3,9]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[2,3],[3,10],[5,8],[4,8],[2,7],[3,4],[3,10],[7,8]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[1,3],[2,4],[6,6],[6,9],[2,10],[4,10],[3,6],[1,4],[1,3]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[4,10],[3,9],[3,5],[4,10],[7,10],[1,7],[7,9],[4,8]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,6],[6,7],[1,6],[1,3],[1,8],[2,9],[3,8],[1,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[1,6],[8,10],[3,7],[6,10],[3,10],[1,10],[7,8]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[6,8],[2,8],[3,9],[3,5],[6,10],[1,2],[5,5]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[4,5],[5,9],[2,3],[5,10],[1,9],[1,8],[2,9],[2,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[8,9],[6,7],[6,9],[3,5],[7,10],[5,9],[10,10]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[6,8],[7,10],[9,10],[6,10],[1,10],[5,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[9,9],[2,8],[5,8],[3,5],[2,2],[7,9],[5,10]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[3,9],[5,9]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[5,10],[2,3],[3,10],[4,7],[1,9],[5,10],[2,6],[1,7],[8,9],[2,9]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[2,3],[2,3],[1,5]] }
assert my_solution.numberOfPoints(**test_input) == 5
test_input = { "nums": [[4,7],[4,7]] }
assert my_solution.numberOfPoints(**test_input) == 4
test_input = { "nums": [[7,9],[5,9],[2,10],[9,9],[5,8],[4,6],[6,7],[3,9],[2,4]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[5,9],[7,7],[3,10],[7,9],[3,4],[1,1],[1,1],[1,7],[1,2],[6,6]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[7,8],[1,7],[5,5],[4,4],[5,8],[2,6]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[3,5],[8,8],[5,10],[1,7],[2,6],[7,10],[6,6],[5,9],[8,9],[5,6]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[4,9]] }
assert my_solution.numberOfPoints(**test_input) == 6
test_input = { "nums": [[2,7],[1,9],[5,6],[6,8],[1,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,4],[2,4],[7,10],[2,8],[1,6],[1,10],[3,5]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,4]] }
assert my_solution.numberOfPoints(**test_input) == 4
test_input = { "nums": [[6,9],[4,7]] }
assert my_solution.numberOfPoints(**test_input) == 6
test_input = { "nums": [[5,7]] }
assert my_solution.numberOfPoints(**test_input) == 3
test_input = { "nums": [[1,9],[6,8],[4,7],[7,9],[8,9],[7,9],[4,6],[6,8],[4,9],[8,8]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[3,6],[3,5],[1,9],[3,4],[3,8],[2,7],[3,8],[2,8]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[2,5],[8,8],[1,6],[4,4],[4,5],[2,4]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[4,7],[2,6]] }
assert my_solution.numberOfPoints(**test_input) == 6
test_input = { "nums": [[5,8],[4,10],[2,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[5,9],[2,4],[2,6]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[2,3],[1,7],[1,8],[7,9],[1,5]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[6,8],[6,7],[1,6],[2,10],[2,2],[6,8],[2,8],[8,9]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[3,4],[2,5],[4,10],[3,6],[4,6],[1,8],[2,6],[6,9],[4,10],[3,6]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[3,5],[2,5],[8,8]] }
assert my_solution.numberOfPoints(**test_input) == 5
test_input = { "nums": [[5,8],[1,3],[8,8]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[2,8],[5,7],[2,3],[2,7],[5,8],[1,10],[4,7],[10,10],[6,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,3],[5,10],[3,10],[5,9]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[4,10],[3,6]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[7,8],[6,10],[7,8],[6,10],[7,10]] }
assert my_solution.numberOfPoints(**test_input) == 5
test_input = { "nums": [[7,7],[4,4],[2,7],[2,3],[4,6],[4,8]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[3,4],[1,4],[4,8],[1,7],[2,10],[8,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,4],[7,10],[1,5],[8,9],[3,5],[3,8],[6,7],[3,5],[1,3],[2,8]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,6],[5,10],[7,8],[7,10],[1,3]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[2,3],[4,4],[2,7],[5,5],[4,7],[6,9],[2,4]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[6,8],[6,8],[6,10]] }
assert my_solution.numberOfPoints(**test_input) == 5
test_input = { "nums": [[3,10],[3,5],[2,3],[7,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[4,4],[8,10],[2,7],[8,9],[1,8],[1,3],[1,9],[7,7],[3,6],[3,5]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[2,6],[1,4],[3,8],[1,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[1,2],[1,9],[2,9],[6,10],[3,5],[1,2]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[6,7],[1,10],[4,4],[5,5],[5,10],[2,3],[2,8],[9,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,1],[2,9],[3,3],[2,2],[2,4],[8,9],[3,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[4,6],[1,10],[4,10],[1,10],[5,7]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[2,3],[9,10],[2,9],[2,8],[8,9],[1,2]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[4,9],[4,6],[2,7],[1,9],[6,10],[7,10],[3,9],[2,9]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[7,10],[4,10],[4,10],[4,5],[3,10],[2,4],[8,9],[3,9],[4,5],[6,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[2,7],[2,5],[3,3],[4,4],[5,6],[3,4],[4,10],[5,5],[4,5]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[3,7],[7,8],[2,6],[10,10],[1,4]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[3,4],[3,8],[5,8]] }
assert my_solution.numberOfPoints(**test_input) == 6
test_input = { "nums": [[6,9],[1,8],[7,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[7,8],[1,5]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[5,10],[5,9],[5,6],[6,8],[1,5],[7,8],[3,5]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[6,8]] }
assert my_solution.numberOfPoints(**test_input) == 3
test_input = { "nums": [[5,5],[5,9],[2,8],[5,9],[5,6]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[7,9],[3,8],[1,8],[8,8],[5,9],[1,3],[2,6]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[3,6],[4,8],[7,9],[3,3],[9,10],[5,8],[1,2],[7,8],[3,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,8],[4,5],[1,5],[6,7],[2,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[6,8],[2,8],[6,9],[10,10],[2,5],[4,6],[1,10],[8,8],[9,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[9,10],[4,8],[9,10],[5,7],[2,5],[2,7],[6,10],[5,7],[9,10]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[1,7],[2,7],[2,4],[6,7]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[2,10],[4,5],[4,10]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[2,10],[3,6],[2,10],[4,10],[4,9],[10,10],[1,1]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[3,5],[6,9],[4,7],[6,6],[4,5],[2,4],[2,7]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[1,1],[1,7]] }
assert my_solution.numberOfPoints(**test_input) == 7
test_input = { "nums": [[1,8],[2,8]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[3,7]] }
assert my_solution.numberOfPoints(**test_input) == 5
test_input = { "nums": [[1,6],[10,10],[5,7],[2,9]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[7,8]] }
assert my_solution.numberOfPoints(**test_input) == 2
test_input = { "nums": [[2,10],[1,10],[5,9],[7,7],[1,6],[3,5],[2,9],[2,10],[7,10]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[3,8],[2,9],[6,10],[4,8],[3,4],[2,3],[5,9],[1,5],[7,9]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[6,7],[1,5],[4,6],[4,9],[6,8],[1,7],[5,10],[3,4]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,2],[4,10],[3,7],[2,10],[1,2],[3,4],[9,9],[5,9],[3,7],[3,5]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[1,6],[3,4],[4,8],[8,10],[3,8]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[3,6],[8,10],[2,5],[9,10],[2,8],[5,10],[7,10],[8,8],[8,10],[8,9]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[1,8],[2,6],[2,3],[3,6],[1,10],[5,8]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[3,7],[7,10],[6,6],[4,10],[5,10],[2,8],[1,10],[7,8],[6,6],[4,7]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[6,9]] }
assert my_solution.numberOfPoints(**test_input) == 4
test_input = { "nums": [[7,8],[1,1],[4,10],[1,9],[2,6],[4,6],[8,9],[4,5]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[2,7],[7,10],[7,8],[3,5],[1,7],[1,4]] }
assert my_solution.numberOfPoints(**test_input) == 10
test_input = { "nums": [[2,9]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[7,9],[2,2],[2,7]] }
assert my_solution.numberOfPoints(**test_input) == 8
test_input = { "nums": [[2,10],[8,9],[6,8],[9,10]] }
assert my_solution.numberOfPoints(**test_input) == 9
test_input = { "nums": [[3,3]] }
assert my_solution.numberOfPoints(**test_input) == 1 | 1,694,313,000 |
weekly-contest-362-determine-if-a-cell-is-reachable-at-a-given-time | https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time | determine-if-a-cell-is-reachable-at-a-given-time | {
"questionId": "3056",
"questionFrontendId": "2849",
"title": "Determine if a Cell Is Reachable at a Given Time",
"titleSlug": "determine-if-a-cell-is-reachable-at-a-given-time",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 782,
"dislikes": 730,
"categoryTitle": "Algorithms"
} | """
给你四个整数 sx、sy、fx、fy 以及一个 非负整数 t 。
在一个无限的二维网格中,你从单元格 (sx, sy) 开始出发。每一秒,你 必须 移动到任一与之前所处单元格相邻的单元格中。
如果你能在 恰好 t 秒 后到达单元格 (fx, fy) ,返回 true ;否则,返回 false 。
单元格的 相邻单元格 是指该单元格周围与其至少共享一个角的 8 个单元格。你可以多次访问同一个单元格。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/05/example2.svg]
输入:sx = 2, sy = 4, fx = 7, fy = 7, t = 6
输出:true
解释:从单元格 (2, 4) 开始出发,穿过上图标注的单元格,可以在恰好 6 秒后到达单元格 (7, 7) 。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/05/example1.svg]
输入:sx = 3, sy = 1, fx = 7, fy = 3, t = 3
输出:false
解释:从单元格 (3, 1) 开始出发,穿过上图标注的单元格,至少需要 4 秒后到达单元格 (7, 3) 。 因此,无法在 3 秒后到达单元格 (7, 3) 。
提示:
* 1 <= sx, sy, fx, fy <= 109
* 0 <= t <= 109
"""
class Solution:
def isReachableAtTime(self, sx: int, sy: int, fx: int, fy: int, t: int) -> bool:
| 给你四个整数 sx、sy、fx、fy 以及一个 非负整数 t 。
在一个无限的二维网格中,你从单元格 (sx, sy) 开始出发。每一秒,你 必须 移动到任一与之前所处单元格相邻的单元格中。
如果你能在 恰好 t 秒 后到达单元格 (fx, fy) ,返回 true ;否则,返回 false 。
单元格的 相邻单元格 是指该单元格周围与其至少共享一个角的 8 个单元格。你可以多次访问同一个单元格。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/05/example2.svg]
输入:sx = 2, sy = 4, fx = 7, fy = 7, t = 6
输出:true
解释:从单元格 (2, 4) 开始出发,穿过上图标注的单元格,可以在恰好 6 秒后到达单元格 (7, 7) 。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/05/example1.svg]
输入:sx = 3, sy = 1, fx = 7, fy = 3, t = 3
输出:false
解释:从单元格 (3, 1) 开始出发,穿过上图标注的单元格,至少需要 4 秒后到达单元格 (7, 3) 。 因此,无法在 3 秒后到达单元格 (7, 3) 。
提示:
* 1 <= sx, sy, fx, fy <= 109
* 0 <= t <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def isReachableAtTime(self, sx: int, sy: int, fx: int, fy: int, t: int) -> bool:
``` |
my_solution = Solution()
test_input = { "sx": 3, "sy": 1, "fx": 7, "fy": 3, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 2, "sy": 4, "fx": 7, "fy": 7, "t": 6 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 1, "fy": 2, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 2, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 3, "fy": 4, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 3, "fy": 5, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 1, "fy": 1, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 1, "fy": 3, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 4, "fy": 1, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 4, "fy": 2, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 1, "fy": 4, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 1, "fy": 5, "t": 8 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 2, "fy": 1, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 4, "fy": 3, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 2, "fy": 2, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 4, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 5, "fy": 1, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 2, "fy": 3, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 5, "fy": 2, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 1, "fx": 2, "fy": 5, "t": 6 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 3, "fy": 1, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 3, "fy": 2, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 3, "fy": 3, "t": 9 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 4, "fy": 5, "t": 9 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 5, "fy": 3, "t": 9 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 5, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 1, "fy": 1, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 1, "fx": 5, "fy": 5, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 1, "fy": 2, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 1, "fy": 3, "t": 6 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 1, "fy": 4, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 2, "fy": 5, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 3, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 1, "fy": 5, "t": 5 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 3, "fy": 5, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 4, "fy": 1, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 2, "fy": 1, "t": 10 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 2, "fy": 2, "t": 9 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 2, "fy": 3, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 4, "fy": 2, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 2, "fy": 4, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 4, "fy": 3, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 4, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 3, "fy": 1, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 3, "fy": 2, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 4, "fy": 5, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 3, "fy": 3, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 5, "fy": 1, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 5, "fy": 3, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 2, "fx": 5, "fy": 2, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 5, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 1, "fy": 1, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 1, "fy": 2, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 2, "fx": 5, "fy": 5, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 2, "fy": 1, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 2, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 1, "fy": 3, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 1, "fy": 4, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 1, "fy": 5, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 2, "fy": 2, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 2, "fy": 3, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 3, "fy": 1, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 2, "fy": 5, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 4, "fy": 1, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 3, "fy": 2, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 3, "fy": 3, "t": 8 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 4, "fy": 5, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 5, "fy": 1, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 5, "fy": 2, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 5, "fy": 3, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 5, "fy": 4, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 5, "fy": 5, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 3, "fy": 4, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 3, "fy": 5, "t": 2 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 1, "fy": 2, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 3, "fx": 4, "fy": 2, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 4, "fy": 3, "t": 10 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 3, "fx": 4, "fy": 4, "t": 10 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 1, "fy": 1, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 1, "fy": 3, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 1, "fy": 4, "t": 6 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 2, "fy": 2, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 1, "fy": 5, "t": 6 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 2, "fy": 4, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 2, "fy": 1, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 3, "fy": 3, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 2, "fy": 3, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 2, "fy": 5, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 3, "fy": 1, "t": 4 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 3, "fy": 2, "t": 5 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 4, "fy": 1, "t": 7 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 3, "fy": 4, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 4, "fy": 4, "t": 10 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 4, "fy": 5, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == True
test_input = { "sx": 1, "sy": 4, "fx": 3, "fy": 5, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 4, "fy": 2, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 4, "fy": 3, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 5, "fy": 2, "t": 1 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 5, "fy": 4, "t": 3 }
assert my_solution.isReachableAtTime(**test_input) == False
test_input = { "sx": 1, "sy": 4, "fx": 5, "fy": 5, "t": 0 }
assert my_solution.isReachableAtTime(**test_input) == False | 1,694,313,000 |
weekly-contest-362-minimum-moves-to-spread-stones-over-grid | https://leetcode.com/problems/minimum-moves-to-spread-stones-over-grid | minimum-moves-to-spread-stones-over-grid | {
"questionId": "3092",
"questionFrontendId": "2850",
"title": "Minimum Moves to Spread Stones Over Grid",
"titleSlug": "minimum-moves-to-spread-stones-over-grid",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 419,
"dislikes": 43,
"categoryTitle": "Algorithms"
} | """
给你一个大小为 3 * 3 ,下标从 0 开始的二维整数矩阵 grid ,分别表示每一个格子里石头的数目。网格图中总共恰好有 9 个石头,一个格子里可能会有 多个 石头。
每一次操作中,你可以将一个石头从它当前所在格子移动到一个至少有一条公共边的相邻格子。
请你返回每个格子恰好有一个石头的 最少移动次数 。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg]
输入:grid = [[1,1,0],[1,1,1],[1,2,1]]
输出:3
解释:让每个格子都有一个石头的一个操作序列为:
1 - 将一个石头从格子 (2,1) 移动到 (2,2) 。
2 - 将一个石头从格子 (2,2) 移动到 (1,2) 。
3 - 将一个石头从格子 (1,2) 移动到 (0,2) 。
总共需要 3 次操作让每个格子都有一个石头。
让每个格子都有一个石头的最少操作次数为 3 。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg]
输入:grid = [[1,3,0],[1,0,0],[1,0,3]]
输出:4
解释:让每个格子都有一个石头的一个操作序列为:
1 - 将一个石头从格子 (0,1) 移动到 (0,2) 。
2 - 将一个石头从格子 (0,1) 移动到 (1,1) 。
3 - 将一个石头从格子 (2,2) 移动到 (1,2) 。
4 - 将一个石头从格子 (2,2) 移动到 (2,1) 。
总共需要 4 次操作让每个格子都有一个石头。
让每个格子都有一个石头的最少操作次数为 4 。
提示:
* grid.length == grid[i].length == 3
* 0 <= grid[i][j] <= 9
* grid 中元素之和为 9 。
"""
class Solution:
def minimumMoves(self, grid: List[List[int]]) -> int:
| 给你一个大小为 3 * 3 ,下标从 0 开始的二维整数矩阵 grid ,分别表示每一个格子里石头的数目。网格图中总共恰好有 9 个石头,一个格子里可能会有 多个 石头。
每一次操作中,你可以将一个石头从它当前所在格子移动到一个至少有一条公共边的相邻格子。
请你返回每个格子恰好有一个石头的 最少移动次数 。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg]
输入:grid = [[1,1,0],[1,1,1],[1,2,1]]
输出:3
解释:让每个格子都有一个石头的一个操作序列为:
1 - 将一个石头从格子 (2,1) 移动到 (2,2) 。
2 - 将一个石头从格子 (2,2) 移动到 (1,2) 。
3 - 将一个石头从格子 (1,2) 移动到 (0,2) 。
总共需要 3 次操作让每个格子都有一个石头。
让每个格子都有一个石头的最少操作次数为 3 。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg]
输入:grid = [[1,3,0],[1,0,0],[1,0,3]]
输出:4
解释:让每个格子都有一个石头的一个操作序列为:
1 - 将一个石头从格子 (0,1) 移动到 (0,2) 。
2 - 将一个石头从格子 (0,1) 移动到 (1,1) 。
3 - 将一个石头从格子 (2,2) 移动到 (1,2) 。
4 - 将一个石头从格子 (2,2) 移动到 (2,1) 。
总共需要 4 次操作让每个格子都有一个石头。
让每个格子都有一个石头的最少操作次数为 4 。
提示:
* grid.length == grid[i].length == 3
* 0 <= grid[i][j] <= 9
* grid 中元素之和为 9 。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumMoves(self, grid: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "grid": [[1,1,0],[1,1,1],[1,2,1]] }
assert my_solution.minimumMoves(**test_input) == 3
test_input = { "grid": [[1,3,0],[1,0,0],[1,0,3]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[1,2,2],[1,1,0],[0,1,1]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[1,3,3],[1,0,0],[0,1,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[3,2,0],[0,1,0],[0,3,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[1,0,4],[2,0,0],[2,0,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,1,3],[3,1,0],[0,1,0]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[3,0,0],[0,2,1],[1,0,2]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[1,0,0],[4,1,1],[0,2,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,2,3],[2,0,1],[0,1,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[4,0,0],[0,0,2],[3,0,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[3,0,0],[4,1,0],[1,0,0]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[0,2,1],[1,2,0],[3,0,0]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[1,3,0],[0,0,1],[2,1,1]] }
assert my_solution.minimumMoves(**test_input) == 3
test_input = { "grid": [[3,0,0],[1,0,1],[0,2,2]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[4,2,2],[0,1,0],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[3,2,1],[1,1,0],[1,0,0]] }
assert my_solution.minimumMoves(**test_input) == 9
test_input = { "grid": [[0,3,0],[2,0,0],[1,3,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,0,0],[3,0,0],[4,2,0]] }
assert my_solution.minimumMoves(**test_input) == 13
test_input = { "grid": [[1,1,2],[0,0,0],[0,4,1]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[4,0,0],[0,0,0],[1,2,2]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[1,3,1],[0,0,0],[1,2,1]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[3,1,0],[1,2,2],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[2,3,2],[0,1,0],[0,0,1]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,1,4],[0,3,0],[0,1,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,1,0],[1,4,0],[0,3,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,0,1],[0,0,3],[2,2,1]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[1,0,1],[1,2,0],[1,0,3]] }
assert my_solution.minimumMoves(**test_input) == 3
test_input = { "grid": [[0,0,0],[4,1,2],[1,1,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[2,2,1],[0,2,1],[0,1,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[1,0,0],[0,2,0],[4,2,0]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[2,0,1],[4,0,0],[0,2,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,0,3],[0,2,0],[1,3,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[2,0,1],[1,0,0],[4,0,1]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,1,0],[1,0,3],[0,3,1]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,3,1],[0,0,0],[3,1,1]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[3,0,1],[0,3,1],[0,0,1]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[0,4,0],[1,0,0],[0,2,2]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[4,2,0],[0,0,0],[1,1,1]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,2,0],[2,1,2],[0,2,0]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[0,1,0],[2,1,1],[4,0,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,4,1],[1,0,1],[0,0,2]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[1,0,0],[0,0,0],[1,3,4]] }
assert my_solution.minimumMoves(**test_input) == 9
test_input = { "grid": [[0,7,1],[0,1,0],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 13
test_input = { "grid": [[0,1,1],[0,2,1],[2,0,2]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[0,2,0],[3,0,0],[3,1,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,0,0],[2,0,2],[0,2,3]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,3,4],[0,1,0],[1,0,0]] }
assert my_solution.minimumMoves(**test_input) == 9
test_input = { "grid": [[1,0,0],[0,0,1],[7,0,0]] }
assert my_solution.minimumMoves(**test_input) == 13
test_input = { "grid": [[0,0,2],[2,0,0],[1,4,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[2,0,1],[1,3,0],[0,2,0]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[1,0,2],[2,3,0],[1,0,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[1,1,2],[0,0,0],[3,1,1]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[0,2,0],[0,1,0],[0,0,6]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[1,1,1],[3,0,0],[2,1,0]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[2,0,0],[2,0,1],[3,1,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[1,1,0],[0,2,2],[0,3,0]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[1,0,3],[1,1,0],[1,0,2]] }
assert my_solution.minimumMoves(**test_input) == 3
test_input = { "grid": [[1,3,0],[2,0,0],[3,0,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[1,0,1],[0,0,1],[0,1,5]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[2,0,0],[0,2,1],[1,1,2]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[1,2,3],[1,0,1],[0,0,1]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,2,3],[1,0,0],[0,1,2]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[4,1,0],[0,1,1],[2,0,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[2,0,1],[1,0,1],[1,2,1]] }
assert my_solution.minimumMoves(**test_input) == 2
test_input = { "grid": [[3,0,3],[0,0,0],[1,1,1]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[0,0,0],[3,4,2],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[2,1,2],[0,0,1],[3,0,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[1,0,0],[0,3,3],[0,0,2]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,2,0],[1,0,1],[1,3,1]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[0,1,0],[2,0,4],[1,0,1]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[1,0,3],[0,0,2],[0,1,2]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[2,1,1],[0,0,0],[0,1,4]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[2,0,1],[0,2,0],[1,3,0]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[2,0,2],[0,0,2],[0,0,3]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,2,2],[2,0,1],[1,1,0]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[0,0,1],[2,3,2],[1,0,0]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[1,1,0],[3,0,4],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[1,0,0],[0,0,0],[5,1,2]] }
assert my_solution.minimumMoves(**test_input) == 11
test_input = { "grid": [[1,0,0],[3,5,0],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 9
test_input = { "grid": [[3,1,1],[1,1,0],[2,0,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,1,3],[0,0,1],[1,2,1]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,0,0],[1,2,0],[3,0,3]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,0,0],[1,2,2],[2,0,2]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[4,0,0],[2,3,0],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[1,1,0],[1,0,1],[4,1,0]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,0,1],[0,1,2],[1,0,4]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[0,1,3],[2,0,0],[0,3,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,0,0],[5,0,1],[1,1,1]] }
assert my_solution.minimumMoves(**test_input) == 7
test_input = { "grid": [[0,1,1],[0,0,3],[2,1,1]] }
assert my_solution.minimumMoves(**test_input) == 5
test_input = { "grid": [[2,0,1],[0,0,1],[2,2,1]] }
assert my_solution.minimumMoves(**test_input) == 3
test_input = { "grid": [[3,0,2],[2,1,0],[0,0,1]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,0,2],[0,0,2],[4,0,1]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[3,0,0],[0,2,0],[0,0,4]] }
assert my_solution.minimumMoves(**test_input) == 8
test_input = { "grid": [[0,1,3],[1,0,0],[0,4,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[2,1,2],[0,2,1],[1,0,0]] }
assert my_solution.minimumMoves(**test_input) == 4
test_input = { "grid": [[0,0,2],[1,0,3],[1,0,2]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[0,2,2],[0,1,4],[0,0,0]] }
assert my_solution.minimumMoves(**test_input) == 10
test_input = { "grid": [[1,0,1],[0,0,5],[1,1,0]] }
assert my_solution.minimumMoves(**test_input) == 6
test_input = { "grid": [[3,1,0],[0,0,0],[0,1,4]] }
assert my_solution.minimumMoves(**test_input) == 8 | 1,694,313,000 |
weekly-contest-362-string-transformation | https://leetcode.com/problems/string-transformation | string-transformation | {
"questionId": "3024",
"questionFrontendId": "2851",
"title": "String Transformation",
"titleSlug": "string-transformation",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 140,
"dislikes": 19,
"categoryTitle": "Algorithms"
} | """
给你两个长度都为 n 的字符串 s 和 t 。你可以对字符串 s 执行以下操作:
* 将 s 长度为 l (0 < l < n)的 后缀字符串 删除,并将它添加在 s 的开头。
比方说,s = 'abcd' ,那么一次操作中,你可以删除后缀 'cd' ,并将它添加到 s 的开头,得到 s = 'cdab' 。
给你一个整数 k ,请你返回 恰好 k 次操作将 s 变为 t 的方案数。
由于答案可能很大,返回答案对 109 + 7 取余 后的结果。
示例 1:
输入:s = "abcd", t = "cdab", k = 2
输出:2
解释:
第一种方案:
第一次操作,选择 index = 3 开始的后缀,得到 s = "dabc" 。
第二次操作,选择 index = 3 开始的后缀,得到 s = "cdab" 。
第二种方案:
第一次操作,选择 index = 1 开始的后缀,得到 s = "bcda" 。
第二次操作,选择 index = 1 开始的后缀,得到 s = "cdab" 。
示例 2:
输入:s = "ababab", t = "ababab", k = 1
输出:2
解释:
第一种方案:
选择 index = 2 开始的后缀,得到 s = "ababab" 。
第二种方案:
选择 index = 4 开始的后缀,得到 s = "ababab" 。
提示:
* 2 <= s.length <= 5 * 105
* 1 <= k <= 1015
* s.length == t.length
* s 和 t 都只包含小写英文字母。
"""
class Solution:
def numberOfWays(self, s: str, t: str, k: int) -> int:
| 给你两个长度都为 n 的字符串 s 和 t 。你可以对字符串 s 执行以下操作:
* 将 s 长度为 l (0 < l < n)的 后缀字符串 删除,并将它添加在 s 的开头。
比方说,s = 'abcd' ,那么一次操作中,你可以删除后缀 'cd' ,并将它添加到 s 的开头,得到 s = 'cdab' 。
给你一个整数 k ,请你返回 恰好 k 次操作将 s 变为 t 的方案数。
由于答案可能很大,返回答案对 109 + 7 取余 后的结果。
示例 1:
输入:s = "abcd", t = "cdab", k = 2
输出:2
解释:
第一种方案:
第一次操作,选择 index = 3 开始的后缀,得到 s = "dabc" 。
第二次操作,选择 index = 3 开始的后缀,得到 s = "cdab" 。
第二种方案:
第一次操作,选择 index = 1 开始的后缀,得到 s = "bcda" 。
第二次操作,选择 index = 1 开始的后缀,得到 s = "cdab" 。
示例 2:
输入:s = "ababab", t = "ababab", k = 1
输出:2
解释:
第一种方案:
选择 index = 2 开始的后缀,得到 s = "ababab" 。
第二种方案:
选择 index = 4 开始的后缀,得到 s = "ababab" 。
提示:
* 2 <= s.length <= 5 * 105
* 1 <= k <= 1015
* s.length == t.length
* s 和 t 都只包含小写英文字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def numberOfWays(self, s: str, t: str, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "s": "abcd", "t": "cdab", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 2
test_input = { "s": "ababab", "t": "ababab", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 2
test_input = { "s": "goxoq", "t": "dfqgl", "k": 244326024901249 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "ceoceo", "t": "eoceoc", "k": 4 }
assert my_solution.numberOfWays(**test_input) == 208
test_input = { "s": "ib", "t": "ib", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "ttttttt", "t": "ttttttt", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 7776
test_input = { "s": "aaaa", "t": "aaaa", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 6561
test_input = { "s": "meplrmeplr", "t": "eplrmeplrm", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 956594
test_input = { "s": "dsmn", "t": "smnd", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 7
test_input = { "s": "jjj", "t": "jjj", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 1024
test_input = { "s": "rrrrr", "t": "rrrrr", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 4
test_input = { "s": "fefe", "t": "fefe", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 9841
test_input = { "s": "pfly", "t": "wvqr", "k": 840550364246523 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "ltjwwltjww", "t": "jwwltjwwlt", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 2
test_input = { "s": "mb", "t": "mb", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "jjjjjjjjjj", "t": "jjjjjjjjjj", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 729
test_input = { "s": "oqytlmi", "t": "lmioqyt", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 239945
test_input = { "s": "hpcg", "t": "pcgh", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 61
test_input = { "s": "bqbqbqbqbq", "t": "bqbqbqbqbq", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 193710244
test_input = { "s": "ccccccccc", "t": "ccccccccc", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 2097152
test_input = { "s": "jjjjjjjjjj", "t": "jjjjjjjjjj", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 387420489
test_input = { "s": "qqqq", "t": "qqqq", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 19683
test_input = { "s": "loppaqg", "t": "nvbxtmh", "k": 104865546226045 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "qqqqqqqqqq", "t": "qqqqqqqqqq", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 729
test_input = { "s": "qsqsqsqsqs", "t": "qsqsqsqsqs", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 41
test_input = { "s": "nnnnn", "t": "nnnnn", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 1024
test_input = { "s": "klncccd", "t": "klncccd", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "qqqqq", "t": "qqqqq", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 262144
test_input = { "s": "qvxrlh", "t": "hqvxrl", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 2604
test_input = { "s": "uuuu", "t": "uuuu", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 19683
test_input = { "s": "sss", "t": "sss", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 128
test_input = { "s": "gggggggggg", "t": "gggggggggg", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 9
test_input = { "s": "ks", "t": "cj", "k": 400700574233583 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "lllllllll", "t": "lllllllll", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 32768
test_input = { "s": "uhixx", "t": "xxuhi", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 13
test_input = { "s": "vkrvkrvkr", "t": "rvkrvkrvk", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 21
test_input = { "s": "xtxtxtxt", "t": "xtxtxtxt", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 2882401
test_input = { "s": "nzybrhi", "t": "rhinzyb", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 6665
test_input = { "s": "ff", "t": "ff", "k": 4 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "ubagdasws", "t": "aswsubagd", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 14913081
test_input = { "s": "aaaaa", "t": "aaaaa", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 1048576
test_input = { "s": "iiiiiiiiii", "t": "iiiiiiiiii", "k": 4 }
assert my_solution.numberOfWays(**test_input) == 6561
test_input = { "s": "nnjqjmgome", "t": "gbfuecwlqc", "k": 359221508193514 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "slmzyj", "t": "slmzyj", "k": 4 }
assert my_solution.numberOfWays(**test_input) == 105
test_input = { "s": "vfyxl", "t": "vfyxl", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 209716
test_input = { "s": "sxzfvsxzfv", "t": "vsxzfvsxzf", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 16
test_input = { "s": "kalt", "t": "ltka", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 547
test_input = { "s": "jj", "t": "jj", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "bcriunp", "t": "criunpb", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 5
test_input = { "s": "rutmzyj", "t": "zyjrutm", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 6665
test_input = { "s": "vvvvv", "t": "vvvvv", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 64
test_input = { "s": "hlld", "t": "hlld", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 4920
test_input = { "s": "kctcsgswa", "t": "qfyyjeohe", "k": 966836940319300 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "otwqxmpktt", "t": "totwqxmpkt", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 478297
test_input = { "s": "kkkkkkk", "t": "kkkkkkk", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 279936
test_input = { "s": "iyl", "t": "iyl", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 22
test_input = { "s": "glao", "t": "ogla", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 14762
test_input = { "s": "jp", "t": "jp", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "uuuuuu", "t": "uuuuuu", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 78125
test_input = { "s": "achach", "t": "achach", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 3255209
test_input = { "s": "uuuuuuuu", "t": "uuuuuuuu", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 823543
test_input = { "s": "gjh", "t": "jhg", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 171
test_input = { "s": "cliuw", "t": "fphcn", "k": 647756904366432 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "zmcum", "t": "mzmcu", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "ll", "t": "ll", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "ccccc", "t": "ccccc", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 4
test_input = { "s": "rrrr", "t": "rrrr", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 3
test_input = { "s": "ih", "t": "hi", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "qfgihqrw", "t": "rwqfgihq", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 720600
test_input = { "s": "cd", "t": "cd", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "oooooooooo", "t": "oooooooooo", "k": 4 }
assert my_solution.numberOfWays(**test_input) == 6561
test_input = { "s": "wp", "t": "wp", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "rqq", "t": "nln", "k": 776508964349618 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "rr", "t": "rr", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "knwppsd", "t": "psdknwp", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 5
test_input = { "s": "epfeepfe", "t": "feepfeep", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 10088402
test_input = { "s": "wwwww", "t": "wwwww", "k": 9 }
assert my_solution.numberOfWays(**test_input) == 262144
test_input = { "s": "cdcdcdcd", "t": "cdcdcdcd", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 58825
test_input = { "s": "uphfr", "t": "fruph", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 3277
test_input = { "s": "cocococo", "t": "cocococo", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 171
test_input = { "s": "vhzjo", "t": "jovhz", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "bbbbbbbbbb", "t": "bbbbbbbbbb", "k": 1 }
assert my_solution.numberOfWays(**test_input) == 9
test_input = { "s": "pgnrstuh", "t": "yjzhldlg", "k": 618648276258027 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "cccccc", "t": "cccccc", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 3125
test_input = { "s": "kkkkkkk", "t": "kkkkkkk", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 216
test_input = { "s": "lxqqzsvej", "t": "svejlxqqz", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 57
test_input = { "s": "lllll", "t": "lllll", "k": 3 }
assert my_solution.numberOfWays(**test_input) == 64
test_input = { "s": "hhhhhhhhhh", "t": "hhhhhhhhhh", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 43046721
test_input = { "s": "gggg", "t": "gggg", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 243
test_input = { "s": "jj", "t": "jj", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "uuuuuuuuu", "t": "uuuuuuuuu", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 73741817
test_input = { "s": "qvx", "t": "vxq", "k": 8 }
assert my_solution.numberOfWays(**test_input) == 85
test_input = { "s": "nolnqlgqcs", "t": "jkguybcfcu", "k": 179216079747558 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "xpk", "t": "xpk", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 22
test_input = { "s": "xzoyb", "t": "bxzoy", "k": 5 }
assert my_solution.numberOfWays(**test_input) == 205
test_input = { "s": "krxjvvg", "t": "krxjvvg", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 6
test_input = { "s": "ks", "t": "sk", "k": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "s": "ty", "t": "ty", "k": 6 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "s": "otototot", "t": "totototo", "k": 7 }
assert my_solution.numberOfWays(**test_input) == 411772
test_input = { "s": "uoaowbdznp", "t": "npuoaowbdz", "k": 10 }
assert my_solution.numberOfWays(**test_input) == 348678440 | 1,694,313,000 |
weekly-contest-361-count-symmetric-integers | https://leetcode.com/problems/count-symmetric-integers | count-symmetric-integers | {
"questionId": "2998",
"questionFrontendId": "2843",
"title": " Count Symmetric Integers",
"titleSlug": "count-symmetric-integers",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 210,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
给你两个正整数 low 和 high 。
对于一个由 2 * n 位数字组成的整数 x ,如果其前 n 位数字之和与后 n 位数字之和相等,则认为这个数字是一个对称整数。
返回在 [low, high] 范围内的 对称整数的数目 。
示例 1:
输入:low = 1, high = 100
输出:9
解释:在 1 到 100 范围内共有 9 个对称整数:11、22、33、44、55、66、77、88 和 99 。
示例 2:
输入:low = 1200, high = 1230
输出:4
解释:在 1200 到 1230 范围内共有 4 个对称整数:1203、1212、1221 和 1230 。
提示:
* 1 <= low <= high <= 104
"""
class Solution:
def countSymmetricIntegers(self, low: int, high: int) -> int:
| 给你两个正整数 low 和 high 。
对于一个由 2 * n 位数字组成的整数 x ,如果其前 n 位数字之和与后 n 位数字之和相等,则认为这个数字是一个对称整数。
返回在 [low, high] 范围内的 对称整数的数目 。
示例 1:
输入:low = 1, high = 100
输出:9
解释:在 1 到 100 范围内共有 9 个对称整数:11、22、33、44、55、66、77、88 和 99 。
示例 2:
输入:low = 1200, high = 1230
输出:4
解释:在 1200 到 1230 范围内共有 4 个对称整数:1203、1212、1221 和 1230 。
提示:
* 1 <= low <= high <= 104
请完成下面的代码来解决上述问题:
```python
class Solution:
def countSymmetricIntegers(self, low: int, high: int) -> int:
``` |
my_solution = Solution()
test_input = { "low": 1, "high": 100 }
assert my_solution.countSymmetricIntegers(**test_input) == 9
test_input = { "low": 1200, "high": 1230 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 1 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 2 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 3 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 4 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 5 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 6 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 7 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 8 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 9 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 10 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 1, "high": 11 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 12 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 13 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 14 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 15 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 16 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 17 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 18 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 19 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 20 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 21 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 1, "high": 22 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 23 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 24 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 25 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 26 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 27 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 28 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 29 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 30 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 31 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 32 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 1, "high": 33 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 34 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 35 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 36 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 37 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 38 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 39 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 40 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 41 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 42 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 43 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 1, "high": 44 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 45 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 46 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 47 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 48 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 100, "high": 1782 }
assert my_solution.countSymmetricIntegers(**test_input) == 44
test_input = { "low": 1, "high": 49 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 50 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 51 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 52 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 53 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 54 }
assert my_solution.countSymmetricIntegers(**test_input) == 4
test_input = { "low": 1, "high": 55 }
assert my_solution.countSymmetricIntegers(**test_input) == 5
test_input = { "low": 1, "high": 56 }
assert my_solution.countSymmetricIntegers(**test_input) == 5
test_input = { "low": 1, "high": 57 }
assert my_solution.countSymmetricIntegers(**test_input) == 5
test_input = { "low": 1, "high": 58 }
assert my_solution.countSymmetricIntegers(**test_input) == 5
test_input = { "low": 1, "high": 59 }
assert my_solution.countSymmetricIntegers(**test_input) == 5
test_input = { "low": 1, "high": 60 }
assert my_solution.countSymmetricIntegers(**test_input) == 5
test_input = { "low": 2, "high": 2 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 3 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 4 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 5 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 6 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 7 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 8 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 9 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 10 }
assert my_solution.countSymmetricIntegers(**test_input) == 0
test_input = { "low": 2, "high": 11 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 12 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 13 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 14 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 15 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 16 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 17 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 18 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 19 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 20 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 21 }
assert my_solution.countSymmetricIntegers(**test_input) == 1
test_input = { "low": 2, "high": 22 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 23 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 24 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 25 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 26 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 27 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 28 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 29 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 30 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 31 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 32 }
assert my_solution.countSymmetricIntegers(**test_input) == 2
test_input = { "low": 2, "high": 33 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 2, "high": 34 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 2, "high": 35 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 2, "high": 36 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 2, "high": 37 }
assert my_solution.countSymmetricIntegers(**test_input) == 3
test_input = { "low": 2, "high": 38 }
assert my_solution.countSymmetricIntegers(**test_input) == 3 | 1,693,708,200 |
weekly-contest-361-minimum-operations-to-make-a-special-number | https://leetcode.com/problems/minimum-operations-to-make-a-special-number | minimum-operations-to-make-a-special-number | {
"questionId": "3046",
"questionFrontendId": "2844",
"title": "Minimum Operations to Make a Special Number",
"titleSlug": "minimum-operations-to-make-a-special-number",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 317,
"dislikes": 48,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的字符串 num ,表示一个非负整数。
在一次操作中,您可以选择 num 的任意一位数字并将其删除。请注意,如果你删除 num 中的所有数字,则 num 变为 0。
返回最少需要多少次操作可以使 num 变成特殊数字。
如果整数 x 能被 25 整除,则该整数 x 被认为是特殊数字。
示例 1:
输入:num = "2245047"
输出:2
解释:删除数字 num[5] 和 num[6] ,得到数字 "22450" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 2 位数字。
示例 2:
输入:num = "2908305"
输出:3
解释:删除 num[3]、num[4] 和 num[6] ,得到数字 "2900" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 3 位数字。
示例 3:
输入:num = "10"
输出:1
解释:删除 num[0] ,得到数字 "0" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 1 位数字。
提示
* 1 <= num.length <= 100
* num 仅由数字 '0' 到 '9' 组成
* num 不含任何前导零
"""
class Solution:
def minimumOperations(self, num: str) -> int:
| 给你一个下标从 0 开始的字符串 num ,表示一个非负整数。
在一次操作中,您可以选择 num 的任意一位数字并将其删除。请注意,如果你删除 num 中的所有数字,则 num 变为 0。
返回最少需要多少次操作可以使 num 变成特殊数字。
如果整数 x 能被 25 整除,则该整数 x 被认为是特殊数字。
示例 1:
输入:num = "2245047"
输出:2
解释:删除数字 num[5] 和 num[6] ,得到数字 "22450" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 2 位数字。
示例 2:
输入:num = "2908305"
输出:3
解释:删除 num[3]、num[4] 和 num[6] ,得到数字 "2900" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 3 位数字。
示例 3:
输入:num = "10"
输出:1
解释:删除 num[0] ,得到数字 "0" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 1 位数字。
提示
* 1 <= num.length <= 100
* num 仅由数字 '0' 到 '9' 组成
* num 不含任何前导零
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumOperations(self, num: str) -> int:
``` |
my_solution = Solution()
test_input = { "num": "2245047" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "2908305" }
assert my_solution.minimumOperations(**test_input) == 3
test_input = { "num": "10" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "1" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "2" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "3" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "4" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "5" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "6" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "7" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "8" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "9" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "11" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "12" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "13" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "14" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "15" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "16" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "17" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "18" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "19" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "20" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "21" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "22" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "23" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "24" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "25" }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "num": "26" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "27" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "28" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "29" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "30" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "31" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "32" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "33" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "34" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "35" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "36" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "37" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "38" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "39" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "40" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "41" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "42" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "43" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "44" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "45" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "46" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "47" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "48" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "49" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "50" }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "num": "51" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "52" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "53" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "54" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "55" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "56" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "57" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "58" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "59" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "60" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "61" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "62" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "63" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "64" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "65" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "66" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "67" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "68" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "69" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "70" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "71" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "72" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "73" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "74" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "75" }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "num": "76" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "77" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "78" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "79" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "80" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "81" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "82" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "83" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "84" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "85" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "86" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "87" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "88" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "89" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "90" }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "num": "91" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "92" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "93" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "94" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "95" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "96" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "97" }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "num": "98" }
assert my_solution.minimumOperations(**test_input) == 2 | 1,693,708,200 |
weekly-contest-361-count-of-interesting-subarrays | https://leetcode.com/problems/count-of-interesting-subarrays | count-of-interesting-subarrays | {
"questionId": "2915",
"questionFrontendId": "2845",
"title": "Count of Interesting Subarrays",
"titleSlug": "count-of-interesting-subarrays",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 449,
"dislikes": 62,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums ,以及整数 modulo 和整数 k 。
请你找出并统计数组中 趣味子数组 的数目。
如果 子数组 nums[l..r] 满足下述条件,则称其为 趣味子数组 :
* 在范围 [l, r] 内,设 cnt 为满足 nums[i] % modulo == k 的索引 i 的数量。并且 cnt % modulo == k 。
以整数形式表示并返回趣味子数组的数目。
注意:子数组是数组中的一个连续非空的元素序列。
示例 1:
输入:nums = [3,2,4], modulo = 2, k = 1
输出:3
解释:在这个示例中,趣味子数组分别是:
子数组 nums[0..0] ,也就是 [3] 。
- 在范围 [0, 0] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
子数组 nums[0..1] ,也就是 [3,2] 。
- 在范围 [0, 1] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
子数组 nums[0..2] ,也就是 [3,2,4] 。
- 在范围 [0, 2] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
可以证明不存在其他趣味子数组。因此,答案为 3 。
示例 2:
输入:nums = [3,1,9,6], modulo = 3, k = 0
输出:2
解释:在这个示例中,趣味子数组分别是:
子数组 nums[0..3] ,也就是 [3,1,9,6] 。
- 在范围 [0, 3] 内,只存在 3 个下标 i = 0, 2, 3 满足 nums[i] % modulo == k 。
- 因此 cnt = 3 ,且 cnt % modulo == k 。
子数组 nums[1..1] ,也就是 [1] 。
- 在范围 [1, 1] 内,不存在下标满足 nums[i] % modulo == k 。
- 因此 cnt = 0 ,且 cnt % modulo == k 。
可以证明不存在其他趣味子数组,因此答案为 2 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* 1 <= modulo <= 109
* 0 <= k < modulo
"""
class Solution:
def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
| 给你一个下标从 0 开始的整数数组 nums ,以及整数 modulo 和整数 k 。
请你找出并统计数组中 趣味子数组 的数目。
如果 子数组 nums[l..r] 满足下述条件,则称其为 趣味子数组 :
* 在范围 [l, r] 内,设 cnt 为满足 nums[i] % modulo == k 的索引 i 的数量。并且 cnt % modulo == k 。
以整数形式表示并返回趣味子数组的数目。
注意:子数组是数组中的一个连续非空的元素序列。
示例 1:
输入:nums = [3,2,4], modulo = 2, k = 1
输出:3
解释:在这个示例中,趣味子数组分别是:
子数组 nums[0..0] ,也就是 [3] 。
- 在范围 [0, 0] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
子数组 nums[0..1] ,也就是 [3,2] 。
- 在范围 [0, 1] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
子数组 nums[0..2] ,也就是 [3,2,4] 。
- 在范围 [0, 2] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
可以证明不存在其他趣味子数组。因此,答案为 3 。
示例 2:
输入:nums = [3,1,9,6], modulo = 3, k = 0
输出:2
解释:在这个示例中,趣味子数组分别是:
子数组 nums[0..3] ,也就是 [3,1,9,6] 。
- 在范围 [0, 3] 内,只存在 3 个下标 i = 0, 2, 3 满足 nums[i] % modulo == k 。
- 因此 cnt = 3 ,且 cnt % modulo == k 。
子数组 nums[1..1] ,也就是 [1] 。
- 在范围 [1, 1] 内,不存在下标满足 nums[i] % modulo == k 。
- 因此 cnt = 0 ,且 cnt % modulo == k 。
可以证明不存在其他趣味子数组,因此答案为 2 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* 1 <= modulo <= 109
* 0 <= k < modulo
请完成下面的代码来解决上述问题:
```python
class Solution:
def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [3,2,4], "modulo": 2, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [3,1,9,6], "modulo": 3, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 2
test_input = { "nums": [11,12,21,31], "modulo": 10, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 5
test_input = { "nums": [2,4], "modulo": 7, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [2,7], "modulo": 7, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [2,45], "modulo": 13, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [3,3], "modulo": 5, "k": 3 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [3,4], "modulo": 8, "k": 3 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [4,5], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [5,1], "modulo": 6, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 2
test_input = { "nums": [7,2], "modulo": 7, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [7,4], "modulo": 7, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [8,8], "modulo": 4, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [9,2], "modulo": 2, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [18,43], "modulo": 3, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [19,67], "modulo": 47, "k": 19 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [20,8], "modulo": 41, "k": 8 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [26,5], "modulo": 21, "k": 5 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [81,36], "modulo": 4, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [2,1,5], "modulo": 9, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [2,2,5], "modulo": 3, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 2
test_input = { "nums": [2,2,5], "modulo": 4, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 2
test_input = { "nums": [2,3,2], "modulo": 6, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [3,2,5], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [5,1,6], "modulo": 2, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [5,2,8], "modulo": 2, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [6,5,6], "modulo": 6, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [7,1,2], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [7,2,9], "modulo": 4, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [7,5,6], "modulo": 4, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [9,1,6], "modulo": 7, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [40,1,24], "modulo": 41, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [48,36,27], "modulo": 9, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [1,9,6,1], "modulo": 2, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [2,2,1,2], "modulo": 3, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [3,5,4,2], "modulo": 5, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [4,18,38,15], "modulo": 21, "k": 4 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [6,6,1,4], "modulo": 7, "k": 6 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [7,5,2,1], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 10
test_input = { "nums": [7,5,3,4], "modulo": 3, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [8,6,5,6], "modulo": 3, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 2
test_input = { "nums": [8,7,3,2], "modulo": 6, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 1
test_input = { "nums": [9,2,2,6], "modulo": 7, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [9,7,4,1], "modulo": 2, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [9,7,8,9], "modulo": 5, "k": 4 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [9,48,32,11], "modulo": 2, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [53,44,40,37], "modulo": 2, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [91,5,60,93], "modulo": 59, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [2,1,1,3,5], "modulo": 4, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [2,2,5,4,3], "modulo": 5, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [2,2,5,6,1], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 15
test_input = { "nums": [2,6,2,3,1], "modulo": 9, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [2,7,4,8,5], "modulo": 2, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 7
test_input = { "nums": [4,2,1,8,8], "modulo": 3, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [4,2,8,8,2], "modulo": 9, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 2
test_input = { "nums": [5,6,3,9,3], "modulo": 9, "k": 3 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [6,7,1,9,2], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 15
test_input = { "nums": [6,9,5,1,6], "modulo": 5, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 7
test_input = { "nums": [7,3,6,2,6], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 15
test_input = { "nums": [7,7,9,5,8], "modulo": 4, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 5
test_input = { "nums": [7,9,1,3,2], "modulo": 8, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 5
test_input = { "nums": [8,6,9,4,4], "modulo": 9, "k": 4 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [8,8,6,8,9], "modulo": 9, "k": 8 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [9,7,8,7,8], "modulo": 7, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 3
test_input = { "nums": [26,9,14,4,24], "modulo": 26, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 10
test_input = { "nums": [31,30,24,34,20], "modulo": 22, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [39,41,6,30,38], "modulo": 43, "k": 6 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [1,2,7,1,6,6], "modulo": 5, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 8
test_input = { "nums": [1,6,2,1,9,7], "modulo": 3, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 11
test_input = { "nums": [1,7,8,2,5,9], "modulo": 7, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 10
test_input = { "nums": [2,4,6,6,5,1], "modulo": 8, "k": 6 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [2,8,2,9,2,8], "modulo": 5, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [2,9,1,6,5,7], "modulo": 7, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 5
test_input = { "nums": [2,9,1,6,6,7], "modulo": 9, "k": 6 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [2,9,6,8,8,3], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 21
test_input = { "nums": [4,8,4,3,7,5], "modulo": 4, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [4,9,4,9,7,7], "modulo": 9, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 5
test_input = { "nums": [5,3,7,9,8,7], "modulo": 3, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 5
test_input = { "nums": [7,1,6,1,7,2], "modulo": 6, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 7
test_input = { "nums": [7,3,1,9,1,3], "modulo": 5, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 10
test_input = { "nums": [7,4,9,8,3,4], "modulo": 1, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 21
test_input = { "nums": [7,5,1,7,7,7], "modulo": 8, "k": 7 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [8,4,5,6,7,4], "modulo": 4, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [8,7,3,8,4,8], "modulo": 8, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [8,7,5,5,2,1], "modulo": 5, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 6
test_input = { "nums": [8,18,36,50,12,37], "modulo": 18, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 7
test_input = { "nums": [9,9,6,7,2,3], "modulo": 5, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 8
test_input = { "nums": [16,1,33,39,15,1], "modulo": 30, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 12
test_input = { "nums": [17,25,9,20,41,26], "modulo": 38, "k": 3 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [21,26,39,21,31,49], "modulo": 22, "k": 21 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [37,44,17,22,50,15], "modulo": 6, "k": 2 }
assert my_solution.countInterestingSubarrays(**test_input) == 4
test_input = { "nums": [40,10,31,40,30,32], "modulo": 50, "k": 40 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [40,22,22,35,2,16], "modulo": 24, "k": 16 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [98,23,66,13,70,34], "modulo": 74, "k": 13 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [1,1,4,8,3,2,7], "modulo": 9, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 7
test_input = { "nums": [1,8,6,3,2,8,8], "modulo": 2, "k": 0 }
assert my_solution.countInterestingSubarrays(**test_input) == 13
test_input = { "nums": [1,9,2,1,5,4,8], "modulo": 4, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 8
test_input = { "nums": [4,6,8,3,4,3,4], "modulo": 7, "k": 4 }
assert my_solution.countInterestingSubarrays(**test_input) == 0
test_input = { "nums": [5,4,5,8,9,1,9], "modulo": 4, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 11
test_input = { "nums": [5,4,6,1,3,2,7], "modulo": 9, "k": 1 }
assert my_solution.countInterestingSubarrays(**test_input) == 16 | 1,693,708,200 |
weekly-contest-361-minimum-edge-weight-equilibrium-queries-in-a-tree | https://leetcode.com/problems/minimum-edge-weight-equilibrium-queries-in-a-tree | minimum-edge-weight-equilibrium-queries-in-a-tree | {
"questionId": "3079",
"questionFrontendId": "2846",
"title": "Minimum Edge Weight Equilibrium Queries in a Tree",
"titleSlug": "minimum-edge-weight-equilibrium-queries-in-a-tree",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 260,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
现有一棵由 n 个节点组成的无向树,节点按从 0 到 n - 1 编号。给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ui, vi, wi] 表示树中存在一条位于节点 ui 和节点 vi 之间、权重为 wi 的边。
另给你一个长度为 m 的二维整数数组 queries ,其中 queries[i] = [ai, bi] 。对于每条查询,请你找出使从 ai 到 bi 路径上每条边的权重相等所需的 最小操作次数 。在一次操作中,你可以选择树上的任意一条边,并将其权重更改为任意值。
注意:
* 查询之间 相互独立 的,这意味着每条新的查询时,树都会回到 初始状态 。
* 从 ai 到 bi的路径是一个由 不同 节点组成的序列,从节点 ai 开始,到节点 bi 结束,且序列中相邻的两个节点在树中共享一条边。
返回一个长度为 m 的数组 answer ,其中 answer[i] 是第 i 条查询的答案。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png]
输入:n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
输出:[0,0,1,3]
解释:第 1 条查询,从节点 0 到节点 3 的路径中的所有边的权重都是 1 。因此,答案为 0 。
第 2 条查询,从节点 3 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 0 。
第 3 条查询,将边 [2,3] 的权重变更为 2 。在这次操作之后,从节点 2 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 1 。
第 4 条查询,将边 [0,1]、[1,2]、[2,3] 的权重变更为 2 。在这次操作之后,从节点 0 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 3 。
对于每条查询 queries[i] ,可以证明 answer[i] 是使从 ai 到 bi 的路径中的所有边的权重相等的最小操作次数。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png]
输入:n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
输出:[1,2,2,3]
解释:第 1 条查询,将边 [1,3] 的权重变更为 6 。在这次操作之后,从节点 4 到节点 6 的路径中的所有边的权重都是 6 。因此,答案为 1 。
第 2 条查询,将边 [0,3]、[3,1] 的权重变更为 6 。在这次操作之后,从节点 0 到节点 4 的路径中的所有边的权重都是 6 。因此,答案为 2 。
第 3 条查询,将边 [1,3]、[5,2] 的权重变更为 6 。在这次操作之后,从节点 6 到节点 5 的路径中的所有边的权重都是 6 。因此,答案为 2 。
第 4 条查询,将边 [0,7]、[0,3]、[1,3] 的权重变更为 6 。在这次操作之后,从节点 7 到节点 4 的路径中的所有边的权重都是 6 。因此,答案为 3 。
对于每条查询 queries[i] ,可以证明 answer[i] 是使从 ai 到 bi 的路径中的所有边的权重相等的最小操作次数。
提示:
* 1 <= n <= 104
* edges.length == n - 1
* edges[i].length == 3
* 0 <= ui, vi < n
* 1 <= wi <= 26
* 生成的输入满足 edges 表示一棵有效的树
* 1 <= queries.length == m <= 2 * 104
* queries[i].length == 2
* 0 <= ai, bi < n
"""
class Solution:
def minOperationsQueries(self, n: int, edges: List[List[int]], queries: List[List[int]]) -> List[int]:
| 现有一棵由 n 个节点组成的无向树,节点按从 0 到 n - 1 编号。给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges ,其中 edges[i] = [ui, vi, wi] 表示树中存在一条位于节点 ui 和节点 vi 之间、权重为 wi 的边。
另给你一个长度为 m 的二维整数数组 queries ,其中 queries[i] = [ai, bi] 。对于每条查询,请你找出使从 ai 到 bi 路径上每条边的权重相等所需的 最小操作次数 。在一次操作中,你可以选择树上的任意一条边,并将其权重更改为任意值。
注意:
* 查询之间 相互独立 的,这意味着每条新的查询时,树都会回到 初始状态 。
* 从 ai 到 bi的路径是一个由 不同 节点组成的序列,从节点 ai 开始,到节点 bi 结束,且序列中相邻的两个节点在树中共享一条边。
返回一个长度为 m 的数组 answer ,其中 answer[i] 是第 i 条查询的答案。
示例 1:
[https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png]
输入:n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
输出:[0,0,1,3]
解释:第 1 条查询,从节点 0 到节点 3 的路径中的所有边的权重都是 1 。因此,答案为 0 。
第 2 条查询,从节点 3 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 0 。
第 3 条查询,将边 [2,3] 的权重变更为 2 。在这次操作之后,从节点 2 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 1 。
第 4 条查询,将边 [0,1]、[1,2]、[2,3] 的权重变更为 2 。在这次操作之后,从节点 0 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 3 。
对于每条查询 queries[i] ,可以证明 answer[i] 是使从 ai 到 bi 的路径中的所有边的权重相等的最小操作次数。
示例 2:
[https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png]
输入:n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
输出:[1,2,2,3]
解释:第 1 条查询,将边 [1,3] 的权重变更为 6 。在这次操作之后,从节点 4 到节点 6 的路径中的所有边的权重都是 6 。因此,答案为 1 。
第 2 条查询,将边 [0,3]、[3,1] 的权重变更为 6 。在这次操作之后,从节点 0 到节点 4 的路径中的所有边的权重都是 6 。因此,答案为 2 。
第 3 条查询,将边 [1,3]、[5,2] 的权重变更为 6 。在这次操作之后,从节点 6 到节点 5 的路径中的所有边的权重都是 6 。因此,答案为 2 。
第 4 条查询,将边 [0,7]、[0,3]、[1,3] 的权重变更为 6 。在这次操作之后,从节点 7 到节点 4 的路径中的所有边的权重都是 6 。因此,答案为 3 。
对于每条查询 queries[i] ,可以证明 answer[i] 是使从 ai 到 bi 的路径中的所有边的权重相等的最小操作次数。
提示:
* 1 <= n <= 104
* edges.length == n - 1
* edges[i].length == 3
* 0 <= ui, vi < n
* 1 <= wi <= 26
* 生成的输入满足 edges 表示一棵有效的树
* 1 <= queries.length == m <= 2 * 104
* queries[i].length == 2
* 0 <= ai, bi < n
请完成下面的代码来解决上述问题:
```python
class Solution:
def minOperationsQueries(self, n: int, edges: List[List[int]], queries: List[List[int]]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "n": 7, "edges": [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], "queries": [[0,3],[3,6],[2,6],[0,6]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,1,3]
test_input = { "n": 8, "edges": [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], "queries": [[4,6],[0,4],[6,5],[7,4]] }
assert my_solution.minOperationsQueries(**test_input) == [1,2,2,3]
test_input = { "n": 1, "edges": [], "queries": [[0,0]] }
assert my_solution.minOperationsQueries(**test_input) == [0]
test_input = { "n": 2, "edges": [[0,1,26]], "queries": [[0,1],[0,0],[1,1]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0]
test_input = { "n": 3, "edges": [[2,1,1],[2,0,2]], "queries": [[0,1],[0,2],[1,2],[0,0],[1,1],[2,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,0,0,0]
test_input = { "n": 6, "edges": [[1,3,3],[4,1,3],[0,3,5],[5,4,2],[2,5,1]], "queries": [[2,1],[2,0],[3,0],[2,2],[2,5],[4,1],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [2,3,0,0,0,0,0]
test_input = { "n": 7, "edges": [[2,1,2],[4,2,4],[5,2,4],[3,4,5],[6,3,5],[0,6,5]], "queries": [[4,4],[6,2],[3,4],[6,1],[2,0],[4,2],[5,0],[3,2],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,0,2,1,0,2,1,1]
test_input = { "n": 5, "edges": [[1,2,1],[4,1,5],[3,2,3],[0,1,2]], "queries": [[1,2],[0,4],[0,0],[4,3],[4,2],[0,2],[3,3],[3,2]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,0,2,1,1,0,0]
test_input = { "n": 10, "edges": [[9,7,1],[5,9,2],[0,9,4],[3,9,5],[1,9,5],[4,0,4],[2,0,2],[8,7,4],[6,3,2]], "queries": [[4,3],[8,1],[9,6],[7,0],[1,1],[5,0],[4,8],[3,6],[8,2],[9,7]] }
assert my_solution.minOperationsQueries(**test_input) == [1,2,1,1,0,1,1,0,2,0]
test_input = { "n": 10, "edges": [[7,1,2],[9,7,3],[8,7,1],[3,7,4],[4,8,2],[5,7,2],[6,4,5],[0,1,4],[2,1,5]], "queries": [[0,4],[9,6],[8,0],[2,6],[5,9],[3,2],[4,1],[9,4]] }
assert my_solution.minOperationsQueries(**test_input) == [2,3,2,3,1,2,1,2]
test_input = { "n": 5, "edges": [[4,2,4],[3,4,3],[0,4,1],[1,3,1]], "queries": [[4,3],[3,1],[1,1],[4,2],[1,4],[2,3],[3,3],[4,1]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,0,1,1,0,1]
test_input = { "n": 6, "edges": [[4,0,4],[1,0,5],[3,4,4],[5,0,4],[2,4,4]], "queries": [[4,4],[1,2],[4,0],[0,4],[4,3],[1,4],[3,2],[3,5],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,0,0,0,1,0,0,0]
test_input = { "n": 6, "edges": [[4,3,2],[5,3,2],[1,4,5],[0,4,2],[2,1,1]], "queries": [[0,4],[2,1],[5,4],[2,0],[4,2],[4,5],[3,3],[5,0],[3,5],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,2,1,0,0,0,0,2]
test_input = { "n": 5, "edges": [[0,1,5],[2,0,1],[3,0,1],[4,2,1]], "queries": [[2,1],[4,3],[4,2],[1,4],[0,2],[2,2],[3,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,1,0,0,0,1]
test_input = { "n": 9, "edges": [[6,2,1],[5,2,1],[4,2,2],[7,4,4],[1,7,4],[0,1,4],[3,2,2],[8,3,1]], "queries": [[4,4],[7,4],[2,4],[6,2],[1,1],[6,8],[5,7],[4,2],[2,6]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,0,0,1,2,0,0]
test_input = { "n": 8, "edges": [[6,1,2],[2,1,5],[3,6,1],[7,1,3],[5,7,4],[4,7,5],[0,5,5]], "queries": [[4,0],[7,1],[3,4],[4,6],[5,7],[4,5],[3,3],[0,2],[7,2],[1,0]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,3,2,0,1,0,2,1,2]
test_input = { "n": 10, "edges": [[5,3,5],[6,3,1],[1,6,4],[2,6,3],[9,1,5],[4,3,5],[7,3,4],[0,1,1],[8,6,4]], "queries": [[6,5],[6,8],[0,3],[5,1],[5,7],[2,3],[7,9],[9,8],[5,9],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,1,2,1,1,2,1,2,0]
test_input = { "n": 5, "edges": [[0,3,2],[4,3,2],[2,3,3],[1,4,5]], "queries": [[2,1],[0,0],[4,3],[1,1],[2,0],[3,0],[2,3],[1,0]] }
assert my_solution.minOperationsQueries(**test_input) == [2,0,0,0,1,0,0,1]
test_input = { "n": 7, "edges": [[1,2,4],[3,2,4],[6,2,4],[0,6,5],[4,6,1],[5,1,5]], "queries": [[4,4],[5,5],[0,4],[0,0],[5,4],[5,1],[2,3],[2,2],[3,2],[4,1]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,1,0,2,0,0,0,0,1]
test_input = { "n": 10, "edges": [[7,0,2],[4,0,2],[6,7,3],[1,6,4],[9,1,3],[8,1,4],[3,6,3],[2,7,5],[5,7,5]], "queries": [[6,2],[0,4],[5,1],[2,3],[4,7],[8,9],[4,8],[9,1],[6,3],[1,9]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,2,1,0,1,3,0,0,0]
test_input = { "n": 9, "edges": [[1,5,4],[4,5,1],[0,4,2],[8,0,2],[6,1,5],[3,8,5],[2,3,2],[7,5,2]], "queries": [[0,1],[0,4],[3,1],[1,1],[5,4],[7,0],[5,1],[8,0],[0,3],[4,7]] }
assert my_solution.minOperationsQueries(**test_input) == [2,0,3,0,0,1,0,0,1,1]
test_input = { "n": 10, "edges": [[4,8,4],[5,4,3],[6,5,3],[7,4,3],[2,8,5],[9,4,4],[1,6,1],[3,9,5],[0,8,2]], "queries": [[8,8],[9,0],[6,2],[2,1],[7,7],[8,7],[9,6],[5,0],[7,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,2,3,0,1,1,2,0]
test_input = { "n": 10, "edges": [[4,3,1],[9,3,1],[2,3,5],[1,4,2],[5,4,2],[0,2,5],[7,1,3],[6,2,2],[8,0,4]], "queries": [[0,7],[9,3],[5,8],[9,6],[5,7],[1,4],[2,9],[0,8],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [3,0,3,2,1,0,1,0,1]
test_input = { "n": 10, "edges": [[4,0,1],[7,4,1],[3,0,3],[1,7,4],[6,7,1],[9,7,5],[5,4,5],[2,9,1],[8,9,2]], "queries": [[6,2],[8,1],[1,1],[6,4],[2,9],[5,0],[2,5],[9,7],[1,9],[2,8]] }
assert my_solution.minOperationsQueries(**test_input) == [1,2,0,0,0,1,2,0,1,1]
test_input = { "n": 7, "edges": [[6,5,4],[0,5,5],[4,0,1],[1,6,4],[3,1,5],[2,3,1]], "queries": [[4,1],[0,2],[3,3],[2,6],[5,6],[0,5],[5,3],[1,3],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [2,3,0,2,0,0,1,0,2]
test_input = { "n": 7, "edges": [[2,3,2],[4,2,2],[0,2,4],[5,3,5],[6,3,2],[1,3,4]], "queries": [[6,2],[4,6],[2,0],[3,0],[0,2],[0,5],[5,3],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,1,0,2,0,0]
test_input = { "n": 9, "edges": [[7,6,2],[3,7,5],[4,3,4],[2,6,5],[8,4,5],[5,2,3],[1,2,4],[0,6,2]], "queries": [[4,4],[3,8],[0,4],[8,1],[3,1],[7,0],[4,5],[3,6],[4,7]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,2,3,2,0,3,1,1]
test_input = { "n": 6, "edges": [[4,1,3],[2,4,4],[5,2,5],[3,1,5],[0,4,2]], "queries": [[3,4],[4,3],[0,3],[4,2],[3,0],[3,3],[5,3],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,2,0,2,0,2,0]
test_input = { "n": 8, "edges": [[0,3,3],[4,0,5],[5,4,2],[6,3,1],[1,3,3],[2,3,2],[7,3,4]], "queries": [[6,2],[4,0],[3,4],[2,7],[4,3],[7,0],[7,3],[7,6],[6,3],[4,1]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,1,1,1,1,0,1,0,1]
test_input = { "n": 5, "edges": [[0,3,2],[2,0,2],[4,3,3],[1,2,3]], "queries": [[0,1],[2,1],[4,3],[1,4],[2,3],[0,2],[3,3],[3,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,2,0,0,0,0,1]
test_input = { "n": 10, "edges": [[8,0,5],[6,0,4],[7,6,3],[3,7,5],[5,3,5],[2,8,5],[1,8,4],[4,6,3],[9,4,4]], "queries": [[9,0],[3,4],[6,5],[0,3],[2,3],[1,7],[7,6],[5,0],[2,5]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,1,2,2,2,0,2,2]
test_input = { "n": 9, "edges": [[2,3,1],[4,2,1],[1,3,3],[0,4,3],[5,2,3],[7,2,1],[8,5,3],[6,4,2]], "queries": [[7,7],[4,3],[3,1],[5,4],[1,8],[1,4],[7,3],[7,6],[8,2],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,1,1,1,0,1,0,1]
test_input = { "n": 5, "edges": [[4,1,4],[3,1,5],[0,4,3],[2,1,4]], "queries": [[0,1],[2,4],[1,2],[0,4],[3,4],[0,0],[1,1]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,0,1,0,0]
test_input = { "n": 4, "edges": [[1,2,4],[3,2,5],[0,1,5]], "queries": [[1,2],[3,1],[1,1],[2,0],[3,0],[3,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,0,1,1,0]
test_input = { "n": 4, "edges": [[1,2,3],[0,2,1],[3,2,5]], "queries": [[3,1],[1,1],[3,0],[2,3],[3,3],[2,2],[1,0],[3,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,1,0,0,0,1,0]
test_input = { "n": 8, "edges": [[4,6,3],[7,4,1],[3,7,4],[1,4,1],[0,6,4],[5,7,3],[2,1,3]], "queries": [[2,4],[6,2],[7,1],[5,1],[4,2],[1,7],[1,3],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,0,1,1,0,1,1]
test_input = { "n": 9, "edges": [[1,7,4],[5,1,2],[6,1,4],[2,5,2],[3,2,5],[8,3,2],[0,3,2],[4,5,2]], "queries": [[7,4],[4,0],[3,4],[6,1],[0,3],[3,3],[6,0],[5,3],[3,2],[6,3]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,1,0,0,0,2,1,0,2]
test_input = { "n": 5, "edges": [[0,3,4],[2,0,5],[4,2,1],[1,4,4]], "queries": [[0,1],[0,4],[4,1],[2,0],[4,2],[0,2],[3,3],[1,0],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [2,1,0,0,0,0,0,2,2]
test_input = { "n": 8, "edges": [[5,4,1],[0,5,3],[2,0,5],[3,5,4],[7,2,4],[1,7,5],[6,5,2]], "queries": [[2,4],[4,0],[6,5],[5,4],[5,1],[0,6],[6,0],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [2,1,0,0,2,1,1,0]
test_input = { "n": 8, "edges": [[4,2,3],[3,2,3],[6,3,3],[7,4,5],[5,3,4],[0,6,5],[1,5,3]], "queries": [[0,1],[2,4],[3,4],[4,2],[7,3],[4,5],[3,6],[6,6],[6,3]] }
assert my_solution.minOperationsQueries(**test_input) == [2,0,0,0,1,1,0,0,0]
test_input = { "n": 10, "edges": [[2,4,2],[1,2,4],[5,2,1],[0,1,1],[9,4,4],[7,9,1],[3,9,5],[8,5,1],[6,2,5]], "queries": [[1,2],[4,0],[8,4],[0,3],[6,7],[3,3],[1,6],[3,2],[9,1],[7,8]] }
assert my_solution.minOperationsQueries(**test_input) == [0,2,1,3,3,0,1,2,1,2]
test_input = { "n": 5, "edges": [[0,1,5],[4,1,5],[3,4,4],[2,0,2]], "queries": [[2,4],[1,2],[0,4],[3,4],[0,0],[4,3],[1,1],[1,4],[3,0],[0,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,0,0,0,0,0,0,1,0]
test_input = { "n": 8, "edges": [[0,2,5],[7,2,2],[1,7,4],[5,0,1],[4,2,2],[3,2,3],[6,4,3]], "queries": [[7,4],[0,4],[6,5],[0,0],[6,1],[2,0],[5,7],[7,2],[2,2],[1,0]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,3,0,2,0,2,0,0,2]
test_input = { "n": 8, "edges": [[7,5,4],[0,5,3],[6,7,5],[3,0,3],[2,6,3],[4,2,4],[1,4,3]], "queries": [[5,5],[7,1],[3,4],[2,7],[4,3],[6,1],[1,4],[3,0],[6,0]] }
assert my_solution.minOperationsQueries(**test_input) == [0,2,3,1,3,1,0,0,2]
test_input = { "n": 5, "edges": [[4,0,4],[2,4,4],[3,2,1],[1,4,4]], "queries": [[0,1],[0,4],[3,4],[4,3],[1,1],[1,4],[0,2],[3,3],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,1,1,0,0,0,0,1]
test_input = { "n": 4, "edges": [[0,2,1],[3,2,2],[1,2,1]], "queries": [[0,3],[2,3],[0,2],[3,3],[2,2],[1,0]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,0,0,0]
test_input = { "n": 5, "edges": [[4,2,5],[1,4,5],[0,4,4],[3,4,4]], "queries": [[0,1],[0,4],[3,1],[0,3],[4,2],[3,0],[1,4],[1,0]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,1,0,0,0,0,1]
test_input = { "n": 4, "edges": [[1,3,4],[2,3,5],[0,3,2]], "queries": [[1,2],[0,3],[2,0],[3,0],[0,2],[2,2],[3,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,1,0,1,0,0,0]
test_input = { "n": 9, "edges": [[5,8,2],[3,8,3],[6,5,3],[7,5,3],[1,6,1],[0,8,1],[4,5,2],[2,1,4]], "queries": [[8,8],[3,4],[6,5],[2,7],[8,1],[6,7],[2,5],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,0,2,2,0,2,1]
test_input = { "n": 10, "edges": [[4,5,5],[1,4,3],[8,4,5],[2,1,3],[0,1,2],[6,0,1],[7,0,1],[3,7,4],[9,2,1]], "queries": [[3,8],[1,5],[0,3],[4,6],[4,2],[2,3],[6,3],[2,5],[9,1],[1,9]] }
assert my_solution.minOperationsQueries(**test_input) == [4,1,1,2,0,3,1,1,1,1]
test_input = { "n": 8, "edges": [[0,5,5],[2,0,2],[3,5,2],[1,5,1],[7,1,5],[4,0,4],[6,2,4]], "queries": [[7,4],[0,4],[7,1],[6,4],[5,0],[5,6],[2,2],[2,5]] }
assert my_solution.minOperationsQueries(**test_input) == [2,0,0,1,0,2,0,1]
test_input = { "n": 9, "edges": [[0,3,1],[4,0,5],[8,4,5],[5,8,4],[1,4,3],[6,4,4],[7,0,1],[2,5,5]], "queries": [[1,2],[8,4],[2,1],[6,5],[3,4],[4,0],[2,0],[6,0],[7,8],[2,8]] }
assert my_solution.minOperationsQueries(**test_input) == [2,0,2,1,1,0,1,1,1,1]
test_input = { "n": 5, "edges": [[1,2,5],[4,2,5],[0,4,2],[3,1,1]], "queries": [[0,4],[0,0],[4,3],[1,4],[3,0],[2,3],[2,2],[1,0],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,1,0,2,1,0,1,0]
test_input = { "n": 7, "edges": [[4,1,3],[5,4,5],[0,1,2],[2,4,3],[3,4,2],[6,1,5]], "queries": [[2,4],[4,0],[1,2],[3,4],[1,5],[6,1],[2,0],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,0,0,1,0,1,1]
test_input = { "n": 9, "edges": [[8,5,5],[0,5,3],[4,5,3],[6,5,2],[1,0,2],[3,6,4],[2,4,2],[7,3,3]], "queries": [[0,4],[7,7],[1,5],[8,7],[1,1],[5,1],[2,3],[8,3],[3,6],[3,2]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,1,3,0,1,2,2,0,2]
test_input = { "n": 9, "edges": [[6,3,1],[1,6,1],[8,3,2],[7,1,1],[0,8,2],[5,3,2],[4,3,5],[2,6,4]], "queries": [[8,1],[8,7],[6,1],[7,3],[1,0],[1,6],[0,8],[1,3],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,0,0,2,0,0,0,2]
test_input = { "n": 9, "edges": [[2,0,1],[5,0,1],[3,2,4],[4,5,2],[1,4,3],[7,3,5],[6,1,1],[8,5,5]], "queries": [[4,0],[7,1],[6,5],[4,1],[8,7],[5,7],[2,3],[1,7],[1,3],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,4,2,0,3,2,0,4,3,0]
test_input = { "n": 5, "edges": [[3,4,2],[2,3,2],[0,4,3],[1,0,1]], "queries": [[4,4],[0,1],[4,0],[1,2],[0,0],[1,4],[2,3],[0,2]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,2,0,1,0,1]
test_input = { "n": 4, "edges": [[3,2,1],[0,3,2],[1,3,4]], "queries": [[0,1],[1,2],[3,1],[1,1],[2,0],[0,2],[2,2],[1,0]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,0,0,1,1,0,1]
test_input = { "n": 5, "edges": [[0,2,2],[4,0,5],[3,2,2],[1,3,3]], "queries": [[2,4],[1,2],[0,0],[0,3],[4,2],[3,0],[1,0],[3,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,0,0,1,0,1,0,0]
test_input = { "n": 7, "edges": [[5,2,2],[3,5,2],[1,5,2],[0,3,4],[6,2,2],[4,0,4]], "queries": [[1,5],[2,0],[6,4],[0,5],[2,2],[5,3],[1,3],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,2,1,0,0,0,0]
test_input = { "n": 4, "edges": [[0,3,3],[1,3,1],[2,1,2]], "queries": [[0,1],[1,2],[2,1],[0,0],[3,1],[1,1],[0,2],[2,2],[1,0],[3,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,0,0,0,2,0,1,1]
test_input = { "n": 6, "edges": [[0,5,3],[2,5,4],[4,5,1],[3,4,4],[1,3,5]], "queries": [[4,4],[2,1],[4,1],[3,1],[0,3],[2,2],[1,0],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,2,1,0,2,0,3,0]
test_input = { "n": 8, "edges": [[0,7,1],[4,7,4],[3,7,5],[1,3,2],[5,4,3],[2,3,3],[6,0,4]], "queries": [[4,4],[7,4],[1,2],[7,7],[0,0],[6,1],[0,6],[2,6],[4,1],[4,7]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,1,0,0,3,0,3,2,0]
test_input = { "n": 5, "edges": [[0,3,3],[2,0,1],[1,2,5],[4,0,3]], "queries": [[0,4],[2,1],[3,4],[0,0],[4,3],[3,1],[1,1],[0,2],[4,1]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,0,0,2,0,0,2]
test_input = { "n": 7, "edges": [[4,1,3],[2,1,2],[0,4,3],[6,0,3],[3,4,2],[5,0,5]], "queries": [[0,1],[6,2],[5,4],[1,4],[3,0],[0,2],[5,6],[3,6],[1,0],[6,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,1,0,1,1,1,1,0,1]
test_input = { "n": 9, "edges": [[8,0,4],[4,8,5],[1,8,1],[7,0,2],[2,7,4],[3,0,1],[6,0,1],[5,2,1]], "queries": [[4,4],[0,4],[3,4],[2,7],[0,0],[8,7],[8,6],[0,5],[6,6],[8,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,2,0,0,1,1,2,0,2]
test_input = { "n": 9, "edges": [[0,4,2],[5,4,3],[8,5,1],[2,5,5],[7,2,5],[1,0,5],[3,4,1],[6,2,5]], "queries": [[6,5],[8,1],[0,0],[1,4],[7,3],[8,6],[4,8],[1,0],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,3,0,1,2,1,1,0,1]
test_input = { "n": 7, "edges": [[0,5,2],[2,5,5],[4,2,3],[3,5,2],[6,3,5],[1,3,4]], "queries": [[0,1],[3,4],[4,1],[4,2],[3,0],[4,5],[0,5],[3,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [1,2,3,0,0,1,0,1,0]
test_input = { "n": 9, "edges": [[0,6,3],[1,0,1],[4,1,5],[3,1,4],[2,3,4],[5,6,2],[8,0,2],[7,5,4]], "queries": [[7,4],[3,8],[8,4],[2,1],[6,8],[1,4],[7,6],[4,1],[7,8],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [4,2,2,0,1,0,1,0,2,3]
test_input = { "n": 6, "edges": [[0,4,5],[1,0,3],[5,0,5],[2,1,3],[3,4,2]], "queries": [[1,2],[5,5],[4,3],[2,0],[5,1],[0,5],[5,3],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,0,1,0,1,1]
test_input = { "n": 10, "edges": [[6,7,1],[5,7,1],[0,5,1],[8,7,2],[3,6,2],[4,8,2],[1,3,1],[2,6,4],[9,6,5]], "queries": [[0,7],[7,7],[4,3],[4,9],[9,8],[0,5],[9,1],[4,7],[9,4]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,1,2,2,0,2,0,2]
test_input = { "n": 4, "edges": [[3,0,5],[1,0,5],[2,0,5]], "queries": [[0,1],[1,2],[2,1],[3,1],[1,1],[3,0],[2,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,0,0,0,0]
test_input = { "n": 9, "edges": [[6,8,4],[7,6,2],[1,7,1],[4,6,4],[0,7,2],[5,4,5],[3,5,2],[2,0,5]], "queries": [[4,3],[6,4],[1,4],[2,2],[7,5],[1,3],[7,8],[0,8]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,2,0,2,3,1,1]
test_input = { "n": 8, "edges": [[5,3,1],[0,5,3],[1,5,2],[7,3,5],[4,0,2],[6,4,4],[2,0,1]], "queries": [[0,7],[2,7],[5,4],[4,6],[5,1],[0,2],[5,0],[5,6],[2,2],[1,6]] }
assert my_solution.minOperationsQueries(**test_input) == [2,2,1,0,0,0,0,2,0,2]
test_input = { "n": 7, "edges": [[2,5,2],[6,2,1],[3,6,2],[1,5,1],[4,3,3],[0,2,2]], "queries": [[0,4],[2,1],[6,1],[5,4],[0,3],[6,4],[0,5],[3,2],[6,3]] }
assert my_solution.minOperationsQueries(**test_input) == [2,1,1,2,1,1,0,1,0]
test_input = { "n": 7, "edges": [[2,3,5],[6,2,1],[0,3,4],[1,0,4],[5,1,1],[4,2,1]], "queries": [[4,4],[0,4],[3,4],[0,0],[1,1],[0,3],[0,6],[0,2],[3,3],[6,0]] }
assert my_solution.minOperationsQueries(**test_input) == [0,2,1,0,0,0,2,1,0,2]
test_input = { "n": 6, "edges": [[0,2,2],[4,2,1],[5,4,5],[1,0,4],[3,4,5]], "queries": [[0,4],[3,4],[3,1],[1,1],[5,4],[2,0],[2,2],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,3,0,0,0,0,1]
test_input = { "n": 9, "edges": [[3,7,2],[1,7,3],[4,3,5],[6,4,3],[8,1,2],[5,1,2],[2,6,1],[0,5,2]], "queries": [[3,8],[2,4],[8,1],[6,8],[7,3],[3,0],[1,0],[8,2],[7,5],[8,5]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,0,3,0,1,0,4,1,0]
test_input = { "n": 9, "edges": [[1,4,3],[3,4,3],[8,4,4],[7,1,5],[2,7,3],[5,8,4],[6,7,4],[0,2,4]], "queries": [[4,4],[0,7],[1,8],[8,3],[3,3],[5,0],[7,6],[6,6],[2,5],[4,1]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,1,1,0,3,0,0,3,0]
test_input = { "n": 9, "edges": [[6,5,3],[3,5,3],[2,5,5],[1,5,2],[8,2,3],[7,6,3],[4,7,2],[0,7,4]], "queries": [[2,4],[8,1],[8,7],[6,1],[4,6],[0,3],[3,2],[3,0],[0,8],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [2,2,1,1,1,1,1,1,2,0]
test_input = { "n": 6, "edges": [[3,5,4],[4,3,5],[0,5,2],[1,4,3],[2,5,4]], "queries": [[1,2],[1,5],[5,4],[5,1],[1,4],[0,2],[0,5],[2,5],[1,3],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [2,2,1,2,0,1,0,0,1,0]
test_input = { "n": 5, "edges": [[3,0,1],[2,3,1],[4,2,1],[1,0,3]], "queries": [[4,0],[3,4],[4,3],[3,1],[2,0],[3,3],[3,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,1,0,0,0,1]
test_input = { "n": 6, "edges": [[4,2,1],[3,4,4],[1,2,5],[5,2,3],[0,3,5]], "queries": [[5,5],[0,4],[3,1],[5,4],[0,3],[1,4],[0,5],[3,2],[2,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,2,1,0,1,3,1,0]
test_input = { "n": 6, "edges": [[3,5,2],[1,3,5],[2,3,4],[0,1,5],[4,1,3]], "queries": [[0,1],[1,1],[5,3],[5,1],[4,5],[0,2],[3,3],[0,5],[1,0],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,1,2,1,0,1,0,0]
test_input = { "n": 7, "edges": [[2,3,3],[1,2,1],[0,3,5],[5,1,2],[6,5,4],[4,6,2]], "queries": [[6,2],[4,0],[0,4],[2,3],[0,2],[3,3],[5,0],[0,5],[1,3],[3,5]] }
assert my_solution.minOperationsQueries(**test_input) == [2,4,4,0,1,0,3,3,1,2]
test_input = { "n": 7, "edges": [[2,5,1],[1,2,2],[3,2,4],[0,5,1],[4,5,1],[6,3,2]], "queries": [[2,4],[3,1],[0,3],[5,1],[4,2],[5,0],[2,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,1,1,0,0,0,1]
test_input = { "n": 8, "edges": [[5,6,2],[7,6,1],[3,5,2],[1,6,3],[0,1,1],[4,6,5],[2,7,3]], "queries": [[6,2],[7,1],[7,7],[4,2],[3,0],[2,3],[7,6],[5,3],[7,5]] }
assert my_solution.minOperationsQueries(**test_input) == [1,1,0,2,2,2,0,0,1]
test_input = { "n": 5, "edges": [[3,1,5],[0,1,3],[2,3,5],[4,3,3]], "queries": [[0,1],[4,0],[0,4],[4,3],[0,3],[1,4],[0,2],[3,3],[2,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,1,0,1,1,1,0,0,0]
test_input = { "n": 6, "edges": [[2,0,1],[1,0,1],[3,2,5],[5,2,4],[4,2,4]], "queries": [[2,4],[3,4],[0,0],[4,1],[0,3],[5,1],[0,2],[4,5],[0,5],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,0,1,1,1,0,0,1,1]
test_input = { "n": 10, "edges": [[2,9,2],[5,2,3],[7,5,1],[0,5,1],[8,2,3],[4,9,3],[3,5,2],[1,0,4],[6,4,3]], "queries": [[9,3],[0,0],[9,9],[4,1],[9,6],[3,7],[4,2],[9,7],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,3,0,1,1,2,0]
test_input = { "n": 5, "edges": [[4,0,4],[2,0,1],[3,0,4],[1,3,5]], "queries": [[3,4],[0,0],[3,1],[2,0],[2,3],[3,3],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,0,0,0,1,0,0]
test_input = { "n": 8, "edges": [[5,3,2],[2,3,2],[0,5,4],[1,5,1],[4,0,3],[6,1,1],[7,3,3]], "queries": [[0,1],[5,5],[7,7],[2,1],[3,1],[4,6],[2,0],[7,6],[5,3],[7,5]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,0,1,1,2,1,2,0,1]
test_input = { "n": 5, "edges": [[0,3,1],[2,0,2],[1,3,2],[4,1,4]], "queries": [[0,4],[3,4],[0,0],[1,1],[2,0],[3,0],[2,3],[4,1]] }
assert my_solution.minOperationsQueries(**test_input) == [2,1,0,0,0,0,1,0]
test_input = { "n": 5, "edges": [[3,4,3],[0,3,1],[1,4,3],[2,0,1]], "queries": [[0,1],[4,4],[4,0],[2,1],[4,3],[3,1],[0,2],[3,3],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [1,0,1,2,0,0,0,0,0]
test_input = { "n": 7, "edges": [[1,0,2],[4,1,5],[5,0,2],[6,5,3],[2,5,5],[3,4,3]], "queries": [[4,4],[2,4],[6,4],[1,4],[5,0],[5,6],[2,2],[1,3]] }
assert my_solution.minOperationsQueries(**test_input) == [0,2,2,0,0,0,0,1]
test_input = { "n": 9, "edges": [[6,8,1],[1,6,2],[0,8,2],[4,8,5],[3,4,4],[5,8,4],[2,5,2],[7,1,2]], "queries": [[0,0],[8,1],[8,3],[1,7],[2,6],[3,3],[0,5],[2,2],[7,5],[2,5]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,1,0,2,0,1,0,2,0]
test_input = { "n": 9, "edges": [[0,1,5],[8,1,4],[2,8,1],[3,2,1],[6,3,2],[4,2,3],[7,8,2],[5,2,3]], "queries": [[8,8],[6,2],[3,7],[1,8],[2,0],[1,4],[6,3],[7,8],[5,2]] }
assert my_solution.minOperationsQueries(**test_input) == [0,1,1,0,2,2,0,0,0]
test_input = { "n": 8, "edges": [[4,5,2],[7,4,2],[0,5,2],[6,0,1],[2,6,1],[3,2,1],[1,7,5]], "queries": [[4,4],[3,7],[4,6],[5,7],[3,0],[0,2],[1,7],[3,6],[4,7]] }
assert my_solution.minOperationsQueries(**test_input) == [0,3,1,0,0,0,0,0,0]
test_input = { "n": 9, "edges": [[5,8,1],[4,8,2],[1,5,1],[7,8,5],[0,8,2],[6,1,5],[2,8,3],[3,1,2]], "queries": [[6,2],[2,7],[5,4],[1,8],[5,7],[3,0],[6,6],[0,8]] }
assert my_solution.minOperationsQueries(**test_input) == [2,1,1,0,1,2,0,0] | 1,693,708,200 |
biweekly-contest-112-check-if-strings-can-be-made-equal-with-operations-i | https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i | check-if-strings-can-be-made-equal-with-operations-i | {
"questionId": "2999",
"questionFrontendId": "2839",
"title": "Check if Strings Can be Made Equal With Operations I",
"titleSlug": "check-if-strings-can-be-made-equal-with-operations-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 164,
"dislikes": 20,
"categoryTitle": "Algorithms"
} | """
给你两个字符串 s1 和 s2 ,两个字符串的长度都为 4 ,且只包含 小写 英文字母。
你可以对两个字符串中的 任意一个 执行以下操作 任意 次:
* 选择两个下标 i 和 j 且满足 j - i = 2 ,然后 交换 这个字符串中两个下标对应的字符。
如果你可以让字符串 s1 和 s2 相等,那么返回 true ,否则返回 false 。
示例 1:
输入:s1 = "abcd", s2 = "cdab"
输出:true
解释: 我们可以对 s1 执行以下操作:
- 选择下标 i = 0 ,j = 2 ,得到字符串 s1 = "cbad" 。
- 选择下标 i = 1 ,j = 3 ,得到字符串 s1 = "cdab" = s2 。
示例 2:
输入:s1 = "abcd", s2 = "dacb"
输出:false
解释:无法让两个字符串相等。
提示:
* s1.length == s2.length == 4
* s1 和 s2 只包含小写英文字母。
"""
class Solution:
def canBeEqual(self, s1: str, s2: str) -> bool:
| 给你两个字符串 s1 和 s2 ,两个字符串的长度都为 4 ,且只包含 小写 英文字母。
你可以对两个字符串中的 任意一个 执行以下操作 任意 次:
* 选择两个下标 i 和 j 且满足 j - i = 2 ,然后 交换 这个字符串中两个下标对应的字符。
如果你可以让字符串 s1 和 s2 相等,那么返回 true ,否则返回 false 。
示例 1:
输入:s1 = "abcd", s2 = "cdab"
输出:true
解释: 我们可以对 s1 执行以下操作:
- 选择下标 i = 0 ,j = 2 ,得到字符串 s1 = "cbad" 。
- 选择下标 i = 1 ,j = 3 ,得到字符串 s1 = "cdab" = s2 。
示例 2:
输入:s1 = "abcd", s2 = "dacb"
输出:false
解释:无法让两个字符串相等。
提示:
* s1.length == s2.length == 4
* s1 和 s2 只包含小写英文字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def canBeEqual(self, s1: str, s2: str) -> bool:
``` |
my_solution = Solution()
test_input = { "s1": "abcd", "s2": "cdab" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "abcd", "s2": "dacb" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "gudo", "s2": "ogdu" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "bnxw", "s2": "bwxn" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "zzon", "s2": "zozn" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "cmpr", "s2": "rmcp" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "qnde", "s2": "flsi" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "vofo", "s2": "oofv" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "xsvc", "s2": "vcxs" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "hvsz", "s2": "hzsv" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "ifjz", "s2": "jzfi" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "zrmq", "s2": "mrzq" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "hazw", "s2": "pfmp" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "kina", "s2": "kina" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "fymg", "s2": "famj" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "riti", "s2": "riti" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "goze", "s2": "gezo" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "seeo", "s2": "vfvm" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "ybyd", "s2": "himj" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "gcdm", "s2": "dmgc" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "kvne", "s2": "nekv" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "cbyo", "s2": "cbyo" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "fezu", "s2": "zufe" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "homs", "s2": "fhdu" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "zlek", "s2": "zlek" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "bxqt", "s2": "xbtq" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "waso", "s2": "wyjd" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "nibi", "s2": "seua" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "oynw", "s2": "sgxl" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "ehui", "s2": "uhei" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "vchn", "s2": "jfwr" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "zgmt", "s2": "zgmt" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "eobz", "s2": "boez" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "zpzg", "s2": "zzpg" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "bbfp", "s2": "fbbp" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "vxqp", "s2": "xpvq" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "ihtv", "s2": "ixji" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "ahsk", "s2": "aksh" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "zexw", "s2": "miva" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "iicq", "s2": "ihda" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "kunv", "s2": "ziac" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "gqzd", "s2": "gqzd" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "ppeb", "s2": "ebpp" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "uouc", "s2": "ucuo" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "laxa", "s2": "xala" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "rbwe", "s2": "wbre" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "mswt", "s2": "wsmt" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "yfyz", "s2": "deyv" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "jlai", "s2": "alji" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "uliu", "s2": "bsmu" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "bhag", "s2": "kuws" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "bvwr", "s2": "wrbv" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "safs", "s2": "safs" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "hzfp", "s2": "hpfz" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "xide", "s2": "dixe" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "qpye", "s2": "qpye" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "zaus", "s2": "zsua" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "lpsc", "s2": "cslp" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "taxc", "s2": "taxc" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "kkjc", "s2": "kcjk" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "pshr", "s2": "prhs" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "kpdr", "s2": "djoe" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "vzla", "s2": "lzva" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "tcar", "s2": "tacr" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "zkyt", "s2": "yfzr" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "puwg", "s2": "pgwu" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "ownv", "s2": "ovnw" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "rayz", "s2": "bpnf" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "zbwg", "s2": "wbzg" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "rypk", "s2": "pyrk" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "qchw", "s2": "bcqn" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "qtpf", "s2": "qfpt" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "apnl", "s2": "nlap" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "pkmh", "s2": "mkph" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "ouxw", "s2": "xuow" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "dlgd", "s2": "gdld" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "xbcx", "s2": "cxxb" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "eaba", "s2": "uaul" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "fyro", "s2": "rofy" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "bzqb", "s2": "bzqb" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "zyjv", "s2": "xjzr" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "jdvv", "s2": "djvv" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "nyxb", "s2": "ocry" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "gxlx", "s2": "lxgx" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "kgkr", "s2": "krkg" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "edfw", "s2": "fdew" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "vxkq", "s2": "kqxv" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "qnjc", "s2": "jivc" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "zzaf", "s2": "azzf" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "esgr", "s2": "gres" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "meuu", "s2": "yqlh" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "gjda", "s2": "djga" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "qaqz", "s2": "qaqz" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "legy", "s2": "lyge" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "eeum", "s2": "emue" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "vsvs", "s2": "vsvs" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "mxlk", "s2": "mxlk" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "nbre", "s2": "renb" }
assert my_solution.canBeEqual(**test_input) == True
test_input = { "s1": "erfk", "s2": "gmfy" }
assert my_solution.canBeEqual(**test_input) == False
test_input = { "s1": "gsic", "s2": "snvs" }
assert my_solution.canBeEqual(**test_input) == False | 1,693,665,000 |
biweekly-contest-112-check-if-strings-can-be-made-equal-with-operations-ii | https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-ii | check-if-strings-can-be-made-equal-with-operations-ii | {
"questionId": "2978",
"questionFrontendId": "2840",
"title": "Check if Strings Can be Made Equal With Operations II",
"titleSlug": "check-if-strings-can-be-made-equal-with-operations-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 231,
"dislikes": 2,
"categoryTitle": "Algorithms"
} | """
给你两个字符串 s1 和 s2 ,两个字符串长度都为 n ,且只包含 小写 英文字母。
你可以对两个字符串中的 任意一个 执行以下操作 任意 次:
* 选择两个下标 i 和 j ,满足 i < j 且 j - i 是 偶数,然后 交换 这个字符串中两个下标对应的字符。
如果你可以让字符串 s1 和 s2 相等,那么返回 true ,否则返回 false 。
示例 1:
输入:s1 = "abcdba", s2 = "cabdab"
输出:true
解释:我们可以对 s1 执行以下操作:
- 选择下标 i = 0 ,j = 2 ,得到字符串 s1 = "cbadba" 。
- 选择下标 i = 2 ,j = 4 ,得到字符串 s1 = "cbbdaa" 。
- 选择下标 i = 1 ,j = 5 ,得到字符串 s1 = "cabdab" = s2 。
示例 2:
输入:s1 = "abe", s2 = "bea"
输出:false
解释:无法让两个字符串相等。
提示:
* n == s1.length == s2.length
* 1 <= n <= 105
* s1 和 s2 只包含小写英文字母。
"""
class Solution:
def checkStrings(self, s1: str, s2: str) -> bool:
| 给你两个字符串 s1 和 s2 ,两个字符串长度都为 n ,且只包含 小写 英文字母。
你可以对两个字符串中的 任意一个 执行以下操作 任意 次:
* 选择两个下标 i 和 j ,满足 i < j 且 j - i 是 偶数,然后 交换 这个字符串中两个下标对应的字符。
如果你可以让字符串 s1 和 s2 相等,那么返回 true ,否则返回 false 。
示例 1:
输入:s1 = "abcdba", s2 = "cabdab"
输出:true
解释:我们可以对 s1 执行以下操作:
- 选择下标 i = 0 ,j = 2 ,得到字符串 s1 = "cbadba" 。
- 选择下标 i = 2 ,j = 4 ,得到字符串 s1 = "cbbdaa" 。
- 选择下标 i = 1 ,j = 5 ,得到字符串 s1 = "cabdab" = s2 。
示例 2:
输入:s1 = "abe", s2 = "bea"
输出:false
解释:无法让两个字符串相等。
提示:
* n == s1.length == s2.length
* 1 <= n <= 105
* s1 和 s2 只包含小写英文字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def checkStrings(self, s1: str, s2: str) -> bool:
``` |
my_solution = Solution()
test_input = { "s1": "abe", "s2": "bea" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "abcdba", "s2": "cabdab" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ublnlasppynwgx", "s2": "ganplbuylnswpx" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "jghn", "s2": "jghn" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "pqtsprqmvi", "s2": "qrvqpitmps" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "aavizsxpqhxztrwi", "s2": "zvisqatzpaxhixwr" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "slmqqdbrwyvm", "s2": "qyldmmwsrqvb" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "shvqocguj", "s2": "vqsghujco" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "ktjpralqanofuuqsyal", "s2": "qjlornpasktfuyluaqa" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "mgflranpdjdkrh", "s2": "fpcgobmkdxbzyl" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "usvpwcehhvlg", "s2": "ehuvvshcwplg" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "jh", "s2": "fy" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "kfqofkvsoiiirznw", "s2": "hosthwbinxrsikkf" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "ppmfd", "s2": "pfdpm" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ntuuwwh", "s2": "jyjwmdf" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "lcgcm", "s2": "brdxe" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "hjswnccgwjcapko", "s2": "acwjnjoscchgwpk" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "epjtzubboiallzd", "s2": "dboilpzzjteualb" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "c", "s2": "c" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "oziiqbotydegrm", "s2": "ytizriobogqmed" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ztmuzn", "s2": "muztzn" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "jkzcqlh", "s2": "hkqczlj" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "qnh", "s2": "cmq" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "azagmtvhske", "s2": "mkazstvhage" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "jitb", "s2": "zqbg" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "tgznpamgczyvqjzvp", "s2": "mpyzvzzjvaqntgpgc" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "tcl", "s2": "lct" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "datuhdkoqe", "s2": "hetddokuqa" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ztnpdilyrxz", "s2": "yxzitnrdlzp" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "vx", "s2": "zv" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "tpfcyceq", "s2": "fceqtpyc" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "rcugebinbszkhy", "s2": "zkebryhcbginus" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ryykp", "s2": "rkpyy" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "lpyfmysemgoxgawwr", "s2": "wfoyspygralemxgwm" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "bfylavpyffcnj", "s2": "cljfbfyvpnayf" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "chhbmikpp", "s2": "hpcimbkhp" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "nateujd", "s2": "jeutnad" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "sxrjofoedghtplnex", "s2": "sgdeolnepthfojrxx" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ajvfujrczdciilihi", "s2": "jcriaviuiflicdhzj" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "gajpuahzcexdunltldh", "s2": "xxatubgvqzmxjvzcxah" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "hnisnfvikgrhkfoe", "s2": "hgkivsifrekfonnh" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "fvfsxftpuemgpnkzz", "s2": "mgknxpuztffvzepsf" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "zrwzpibwp", "s2": "pwpiwzbrz" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "zoqpcssnhugxffcptsj", "s2": "chgospjssfxnfpcuqzt" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "ldpjhj", "s2": "jdlpjh" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "zodbh", "s2": "pqdpy" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "mdcvitezgqur", "s2": "mvigcqrdeztu" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "rdyau", "s2": "dyrau" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "pyymcw", "s2": "ptlqxp" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "ebicv", "s2": "vibce" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "xkzknvrbvajwbj", "s2": "rnkwzbvvxkjbja" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "zoxpiqxvnk", "s2": "xvkpxinqoz" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "obhmqltgmoszljeyh", "s2": "ogmmlbhoeysjtlhzq" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "wfsdirrheuglhfkbjk", "s2": "wfsuhhglifkrebrdjk" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "shn", "s2": "hsn" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "xdicugry", "s2": "igucxdry" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "bkfmkrbybim", "s2": "brbimkkmbyf" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "dwowyfgreakdvfi", "s2": "yfiddfvwerkaowg" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "bdywnvbgmum", "s2": "guymbwdbnmv" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "tmujoqerfvupnyy", "s2": "uvortyfmuqypnje" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "hggckwudke", "s2": "ylkgulkehd" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "zrppcm", "s2": "okurkg" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "ennl", "s2": "ennl" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "spznlxodciidngyvvkl", "s2": "lnvnzdixivoglydscpk" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "nxezcoklmdbekyjp", "s2": "cdkenlkyeomxjzbp" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "yui", "s2": "iyu" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "zphe", "s2": "hpze" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "oyjngnej", "s2": "oynjjeng" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "kvwdssgl", "s2": "wskxsdgv" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "ozzfbhzkowpcv", "s2": "vzpwzkbhzfoco" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "h", "s2": "h" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "mqkdv", "s2": "kqvdm" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "hphfqesitgprud", "s2": "tpisuhqhfgdrpe" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "yrbto", "s2": "orytb" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "lvahby", "s2": "ilbyaz" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "bbaiabphflgyfo", "s2": "bhglybaoaipbff" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "uielps", "s2": "uselpi" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ftyhgkgwg", "s2": "gtfhgwgky" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "mzrfxrwlliuoi", "s2": "llrouzirwimfx" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "drofbzocwolcznzfi", "s2": "wzocdnirzfbclozfo" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "popefwbkepdxy", "s2": "pxbwpeekfoypd" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "swgzqluzcvhskegvdry", "s2": "seuwkvclqzyvdsgrgzh" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "hik", "s2": "ihk" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "zq", "s2": "zq" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "igdzd", "s2": "phcyi" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "cazutcxkhcklpuogt", "s2": "ockghuukctcztxapl" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "bx", "s2": "bx" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "bkujvjwgjzxdzuw", "s2": "zgjubjwkwzxdujv" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ujpudt", "s2": "dtujpu" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "hkalpd", "s2": "hlpkad" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "kbcpjzadbv", "s2": "dsgzcapzao" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "rubbbf", "s2": "rbbubf" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "ybudbyrlmiddsxks", "s2": "nuyyabyisyptvdnb" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "wiijtcgqez", "s2": "ctqzwjgiei" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "bgalfvefwmhodexazkd", "s2": "fbgmdfholzakvxaweed" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "oxlphorosradotpshnt", "s2": "hdotpaoorosrlhtsxpn" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "mnvnzakjaymsxhojq", "s2": "oaxjoeanksmyqmglm" }
assert my_solution.checkStrings(**test_input) == False
test_input = { "s1": "hsm", "s2": "hsm" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "qgloiyhewunm", "s2": "qehuwonmiylg" }
assert my_solution.checkStrings(**test_input) == True
test_input = { "s1": "drdu", "s2": "dudr" }
assert my_solution.checkStrings(**test_input) == True | 1,693,665,000 |
biweekly-contest-112-maximum-sum-of-almost-unique-subarray | https://leetcode.com/problems/maximum-sum-of-almost-unique-subarray | maximum-sum-of-almost-unique-subarray | {
"questionId": "2954",
"questionFrontendId": "2841",
"title": "Maximum Sum of Almost Unique Subarray",
"titleSlug": "maximum-sum-of-almost-unique-subarray",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 230,
"dislikes": 133,
"categoryTitle": "Algorithms"
} | """
给你一个整数数组 nums 和两个正整数 m 和 k 。
请你返回 nums 中长度为 k 的 几乎唯一 子数组的 最大和 ,如果不存在几乎唯一子数组,请你返回 0 。
如果 nums 的一个子数组有至少 m 个互不相同的元素,我们称它是 几乎唯一 子数组。
子数组指的是一个数组中一段连续 非空 的元素序列。
示例 1:
输入:nums = [2,6,7,3,1,7], m = 3, k = 4
输出:18
解释:总共有 3 个长度为 k = 4 的几乎唯一子数组。分别为 [2, 6, 7, 3] ,[6, 7, 3, 1] 和 [7, 3, 1, 7] 。这些子数组中,和最大的是 [2, 6, 7, 3] ,和为 18 。
示例 2:
输入:nums = [5,9,9,2,4,5,4], m = 1, k = 3
输出:23
解释:总共有 5 个长度为 k = 3 的几乎唯一子数组。分别为 [5, 9, 9] ,[9, 9, 2] ,[9, 2, 4] ,[2, 4, 5] 和 [4, 5, 4] 。这些子数组中,和最大的是 [5, 9, 9] ,和为 23 。
示例 3:
输入:nums = [1,2,1,2,1,2,1], m = 3, k = 3
输出:0
解释:输入数组中不存在长度为 k = 3 的子数组含有至少 m = 3 个互不相同元素的子数组。所以不存在几乎唯一子数组,最大和为 0 。
提示:
* 1 <= nums.length <= 2 * 104
* 1 <= m <= k <= nums.length
* 1 <= nums[i] <= 109
"""
class Solution:
def maxSum(self, nums: List[int], m: int, k: int) -> int:
| 给你一个整数数组 nums 和两个正整数 m 和 k 。
请你返回 nums 中长度为 k 的 几乎唯一 子数组的 最大和 ,如果不存在几乎唯一子数组,请你返回 0 。
如果 nums 的一个子数组有至少 m 个互不相同的元素,我们称它是 几乎唯一 子数组。
子数组指的是一个数组中一段连续 非空 的元素序列。
示例 1:
输入:nums = [2,6,7,3,1,7], m = 3, k = 4
输出:18
解释:总共有 3 个长度为 k = 4 的几乎唯一子数组。分别为 [2, 6, 7, 3] ,[6, 7, 3, 1] 和 [7, 3, 1, 7] 。这些子数组中,和最大的是 [2, 6, 7, 3] ,和为 18 。
示例 2:
输入:nums = [5,9,9,2,4,5,4], m = 1, k = 3
输出:23
解释:总共有 5 个长度为 k = 3 的几乎唯一子数组。分别为 [5, 9, 9] ,[9, 9, 2] ,[9, 2, 4] ,[2, 4, 5] 和 [4, 5, 4] 。这些子数组中,和最大的是 [5, 9, 9] ,和为 23 。
示例 3:
输入:nums = [1,2,1,2,1,2,1], m = 3, k = 3
输出:0
解释:输入数组中不存在长度为 k = 3 的子数组含有至少 m = 3 个互不相同元素的子数组。所以不存在几乎唯一子数组,最大和为 0 。
提示:
* 1 <= nums.length <= 2 * 104
* 1 <= m <= k <= nums.length
* 1 <= nums[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxSum(self, nums: List[int], m: int, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,6,7,3,1,7], "m": 3, "k": 4 }
assert my_solution.maxSum(**test_input) == 18
test_input = { "nums": [5,9,9,2,4,5,4], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 23
test_input = { "nums": [1,2,1,2,1,2,1], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 1
test_input = { "nums": [1,1], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [1,1,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 1
test_input = { "nums": [1,1,1,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 1
test_input = { "nums": [1,1,1,2], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,1,1,3], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,1,1,4], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 7
test_input = { "nums": [1,1,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 2
test_input = { "nums": [1,1,2,1], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,1,2,2], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,1,2,3], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,1,2,4], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,1,3], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,1,3,1], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,1,3,2], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,1,3,3], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,1,3,4], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,1,4], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,1,4,1], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,1,4,2], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,1,4,3], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,1,4,4], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [1,2], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,2,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 2
test_input = { "nums": [1,2,1,1], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,2,1,2], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,2,1,3], "m": 1, "k": 4 }
assert my_solution.maxSum(**test_input) == 7
test_input = { "nums": [1,2,1,4], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,2,2], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,2,2,1], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,2,2,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 2
test_input = { "nums": [1,2,2,3], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [1,2,2,4], "m": 1, "k": 4 }
assert my_solution.maxSum(**test_input) == 9
test_input = { "nums": [1,2,3], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,2,3,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,2,3,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,2,3,3], "m": 2, "k": 3 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,2,3,4], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 7
test_input = { "nums": [1,2,4], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,2,4,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,2,4,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,2,4,3], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,2,4,4], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,3], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,3,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [1,3,1,1], "m": 2, "k": 3 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,3,1,2], "m": 4, "k": 4 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [1,3,1,3], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,3,1,4], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,3,2], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,3,2,1], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,3,2,2], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [1,3,2,3], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,3,2,4], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 9
test_input = { "nums": [1,3,3], "m": 2, "k": 3 }
assert my_solution.maxSum(**test_input) == 7
test_input = { "nums": [1,3,3,1], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,3,3,2], "m": 2, "k": 3 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,3,3,3], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,3,3,4], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 11
test_input = { "nums": [1,3,4], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,3,4,1], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 9
test_input = { "nums": [1,3,4,2], "m": 3, "k": 4 }
assert my_solution.maxSum(**test_input) == 10
test_input = { "nums": [1,3,4,3], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,3,4,4], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,4], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,4,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,4,1,1], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,4,1,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,4,1,3], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,4,1,4], "m": 2, "k": 3 }
assert my_solution.maxSum(**test_input) == 9
test_input = { "nums": [1,4,2], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 7
test_input = { "nums": [1,4,2,1], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [1,4,2,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,4,2,3], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 9
test_input = { "nums": [1,4,2,4], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 10
test_input = { "nums": [1,4,3], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,4,3,1], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 7
test_input = { "nums": [1,4,3,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,4,3,3], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 7
test_input = { "nums": [1,4,3,4], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,4,4], "m": 2, "k": 3 }
assert my_solution.maxSum(**test_input) == 9
test_input = { "nums": [1,4,4,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 4
test_input = { "nums": [1,4,4,2], "m": 1, "k": 2 }
assert my_solution.maxSum(**test_input) == 8
test_input = { "nums": [1,4,4,3], "m": 3, "k": 4 }
assert my_solution.maxSum(**test_input) == 12
test_input = { "nums": [1,4,4,4], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 2
test_input = { "nums": [2,1], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [2,1,1], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 2
test_input = { "nums": [2,1,1,1], "m": 4, "k": 4 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [2,1,1,2], "m": 2, "k": 4 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [2,1,1,3], "m": 3, "k": 3 }
assert my_solution.maxSum(**test_input) == 0
test_input = { "nums": [2,1,1,4], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [2,1,2], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 5
test_input = { "nums": [2,1,2,1], "m": 2, "k": 2 }
assert my_solution.maxSum(**test_input) == 3
test_input = { "nums": [2,1,2,2], "m": 1, "k": 1 }
assert my_solution.maxSum(**test_input) == 2
test_input = { "nums": [2,1,2,3], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 6
test_input = { "nums": [2,1,2,4], "m": 1, "k": 3 }
assert my_solution.maxSum(**test_input) == 7 | 1,693,665,000 |
biweekly-contest-112-count-k-subsequences-of-a-string-with-maximum-beauty | https://leetcode.com/problems/count-k-subsequences-of-a-string-with-maximum-beauty | count-k-subsequences-of-a-string-with-maximum-beauty | {
"questionId": "3057",
"questionFrontendId": "2842",
"title": "Count K-Subsequences of a String With Maximum Beauty",
"titleSlug": "count-k-subsequences-of-a-string-with-maximum-beauty",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 286,
"dislikes": 21,
"categoryTitle": "Algorithms"
} | """
给你一个字符串 s 和一个整数 k 。
k 子序列指的是 s 的一个长度为 k 的 子序列 ,且所有字符都是 唯一 的,也就是说每个字符在子序列里只出现过一次。
定义 f(c) 为字符 c 在 s 中出现的次数。
k 子序列的 美丽值 定义为这个子序列中每一个字符 c 的 f(c) 之 和 。
比方说,s = "abbbdd" 和 k = 2 ,我们有:
* f('a') = 1, f('b') = 3, f('d') = 2
* s 的部分 k 子序列为:
* "abbbdd" -> "ab" ,美丽值为 f('a') + f('b') = 4
* "abbbdd" -> "ad" ,美丽值为 f('a') + f('d') = 3
* "abbbdd" -> "bd" ,美丽值为 f('b') + f('d') = 5
请你返回一个整数,表示所有 k 子序列 里面 美丽值 是 最大值 的子序列数目。由于答案可能很大,将结果对 109 + 7 取余后返回。
一个字符串的子序列指的是从原字符串里面删除一些字符(也可能一个字符也不删除),不改变剩下字符顺序连接得到的新字符串。
注意:
* f(c) 指的是字符 c 在字符串 s 的出现次数,不是在 k 子序列里的出现次数。
* 两个 k 子序列如果有任何一个字符在原字符串中的下标不同,则它们是两个不同的子序列。所以两个不同的 k 子序列可能产生相同的字符串。
示例 1:
输入:s = "bcca", k = 2
输出:4
解释:s 中我们有 f('a') = 1 ,f('b') = 1 和 f('c') = 2 。
s 的 k 子序列为:
bcca ,美丽值为 f('b') + f('c') = 3
bcca ,美丽值为 f('b') + f('c') = 3
bcca ,美丽值为 f('b') + f('a') = 2
bcca ,美丽值为 f('c') + f('a') = 3
bcca ,美丽值为 f('c') + f('a') = 3
总共有 4 个 k 子序列美丽值为最大值 3 。
所以答案为 4 。
示例 2:
输入:s = "abbcd", k = 4
输出:2
解释:s 中我们有 f('a') = 1 ,f('b') = 2 ,f('c') = 1 和 f('d') = 1 。
s 的 k 子序列为:
abbcd ,美丽值为 f('a') + f('b') + f('c') + f('d') = 5
abbcd ,美丽值为 f('a') + f('b') + f('c') + f('d') = 5
总共有 2 个 k 子序列美丽值为最大值 5 。
所以答案为 2 。
提示:
* 1 <= s.length <= 2 * 105
* 1 <= k <= s.length
* s 只包含小写英文字母。
"""
class Solution:
def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:
| 给你一个字符串 s 和一个整数 k 。
k 子序列指的是 s 的一个长度为 k 的 子序列 ,且所有字符都是 唯一 的,也就是说每个字符在子序列里只出现过一次。
定义 f(c) 为字符 c 在 s 中出现的次数。
k 子序列的 美丽值 定义为这个子序列中每一个字符 c 的 f(c) 之 和 。
比方说,s = "abbbdd" 和 k = 2 ,我们有:
* f('a') = 1, f('b') = 3, f('d') = 2
* s 的部分 k 子序列为:
* "abbbdd" -> "ab" ,美丽值为 f('a') + f('b') = 4
* "abbbdd" -> "ad" ,美丽值为 f('a') + f('d') = 3
* "abbbdd" -> "bd" ,美丽值为 f('b') + f('d') = 5
请你返回一个整数,表示所有 k 子序列 里面 美丽值 是 最大值 的子序列数目。由于答案可能很大,将结果对 109 + 7 取余后返回。
一个字符串的子序列指的是从原字符串里面删除一些字符(也可能一个字符也不删除),不改变剩下字符顺序连接得到的新字符串。
注意:
* f(c) 指的是字符 c 在字符串 s 的出现次数,不是在 k 子序列里的出现次数。
* 两个 k 子序列如果有任何一个字符在原字符串中的下标不同,则它们是两个不同的子序列。所以两个不同的 k 子序列可能产生相同的字符串。
示例 1:
输入:s = "bcca", k = 2
输出:4
解释:s 中我们有 f('a') = 1 ,f('b') = 1 和 f('c') = 2 。
s 的 k 子序列为:
bcca ,美丽值为 f('b') + f('c') = 3
bcca ,美丽值为 f('b') + f('c') = 3
bcca ,美丽值为 f('b') + f('a') = 2
bcca ,美丽值为 f('c') + f('a') = 3
bcca ,美丽值为 f('c') + f('a') = 3
总共有 4 个 k 子序列美丽值为最大值 3 。
所以答案为 4 。
示例 2:
输入:s = "abbcd", k = 4
输出:2
解释:s 中我们有 f('a') = 1 ,f('b') = 2 ,f('c') = 1 和 f('d') = 1 。
s 的 k 子序列为:
abbcd ,美丽值为 f('a') + f('b') + f('c') + f('d') = 5
abbcd ,美丽值为 f('a') + f('b') + f('c') + f('d') = 5
总共有 2 个 k 子序列美丽值为最大值 5 。
所以答案为 2 。
提示:
* 1 <= s.length <= 2 * 105
* 1 <= k <= s.length
* s 只包含小写英文字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "s": "bcca", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4
test_input = { "s": "abbcd", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "am", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "az", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "ci", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "dd", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 0
test_input = { "s": "di", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "dw", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "ef", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "gq", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "hj", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "hx", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "ii", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 0
test_input = { "s": "il", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "jb", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "kx", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "qh", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "qk", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "qr", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "rg", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "rn", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "st", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "tb", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "tl", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "xc", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "auy", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "axm", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "dqc", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "fkp", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "fmk", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "fvl", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "hcx", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "iua", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "kzb", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "mhb", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "nzo", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "oof", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 0
test_input = { "s": "rfh", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "sty", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "sue", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "tba", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "tmc", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "wes", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "wvl", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "xho", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "xke", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "ysu", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "yxn", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "zco", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "zpq", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "axgn", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "dfyq", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "dogq", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "drbs", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "elex", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 0
test_input = { "s": "fsaj", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4
test_input = { "s": "fxau", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "glbq", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "hzcj", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "minc", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4
test_input = { "s": "nkim", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4
test_input = { "s": "otpl", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "pvrz", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "qwmy", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "rliu", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "tpig", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "ucvh", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "vevt", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4
test_input = { "s": "xstt", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "ypmv", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4
test_input = { "s": "znoq", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4
test_input = { "s": "bicnt", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "bnhom", "k": 5 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "culhr", "k": 5 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 1
test_input = { "s": "dpfki", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "dscbu", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 10
test_input = { "s": "edwlo", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 10
test_input = { "s": "ggsgo", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "guzzf", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "gzzzl", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 3
test_input = { "s": "kjojr", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "kvsds", "k": 5 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 0
test_input = { "s": "ljdvp", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "mdccc", "k": 5 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 0
test_input = { "s": "mmqny", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "mrbrj", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "pwtbx", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "qcxkr", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 10
test_input = { "s": "qvauy", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "tzwoq", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 10
test_input = { "s": "ufxge", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "unzxd", "k": 4 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "vhqqj", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 6
test_input = { "s": "vnkbt", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "wpbkz", "k": 2 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 10
test_input = { "s": "xdgvy", "k": 1 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 5
test_input = { "s": "yelem", "k": 5 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 0
test_input = { "s": "zwkhq", "k": 3 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 10
test_input = { "s": "anxnfi", "k": 5 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2
test_input = { "s": "cfbyuf", "k": 5 }
assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2 | 1,693,665,000 |
weekly-contest-360-furthest-point-from-origin | https://leetcode.com/problems/furthest-point-from-origin | furthest-point-from-origin | {
"questionId": "3019",
"questionFrontendId": "2833",
"title": "Furthest Point From Origin",
"titleSlug": "furthest-point-from-origin",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 203,
"dislikes": 29,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 的字符串 moves ,该字符串仅由字符 'L'、'R' 和 '_' 组成。字符串表示你在一条原点为 0 的数轴上的若干次移动。
你的初始位置就在原点(0),第 i 次移动过程中,你可以根据对应字符选择移动方向:
* 如果 moves[i] = 'L' 或 moves[i] = '_' ,可以选择向左移动一个单位距离
* 如果 moves[i] = 'R' 或 moves[i] = '_' ,可以选择向右移动一个单位距离
移动 n 次之后,请你找出可以到达的距离原点 最远 的点,并返回 从原点到这一点的距离 。
示例 1:
输入:moves = "L_RL__R"
输出:3
解释:可以到达的距离原点 0 最远的点是 -3 ,移动的序列为 "LLRLLLR" 。
示例 2:
输入:moves = "_R__LL_"
输出:5
解释:可以到达的距离原点 0 最远的点是 -5 ,移动的序列为 "LRLLLLL" 。
示例 3:
输入:moves = "_______"
输出:7
解释:可以到达的距离原点 0 最远的点是 7 ,移动的序列为 "RRRRRRR" 。
提示:
* 1 <= moves.length == n <= 50
* moves 仅由字符 'L'、'R' 和 '_' 组成
"""
class Solution:
def furthestDistanceFromOrigin(self, moves: str) -> int:
| 给你一个长度为 n 的字符串 moves ,该字符串仅由字符 'L'、'R' 和 '_' 组成。字符串表示你在一条原点为 0 的数轴上的若干次移动。
你的初始位置就在原点(0),第 i 次移动过程中,你可以根据对应字符选择移动方向:
* 如果 moves[i] = 'L' 或 moves[i] = '_' ,可以选择向左移动一个单位距离
* 如果 moves[i] = 'R' 或 moves[i] = '_' ,可以选择向右移动一个单位距离
移动 n 次之后,请你找出可以到达的距离原点 最远 的点,并返回 从原点到这一点的距离 。
示例 1:
输入:moves = "L_RL__R"
输出:3
解释:可以到达的距离原点 0 最远的点是 -3 ,移动的序列为 "LLRLLLR" 。
示例 2:
输入:moves = "_R__LL_"
输出:5
解释:可以到达的距离原点 0 最远的点是 -5 ,移动的序列为 "LRLLLLL" 。
示例 3:
输入:moves = "_______"
输出:7
解释:可以到达的距离原点 0 最远的点是 7 ,移动的序列为 "RRRRRRR" 。
提示:
* 1 <= moves.length == n <= 50
* moves 仅由字符 'L'、'R' 和 '_' 组成
请完成下面的代码来解决上述问题:
```python
class Solution:
def furthestDistanceFromOrigin(self, moves: str) -> int:
``` |
my_solution = Solution()
test_input = { "moves": "L_RL__R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "_R__LL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 5
test_input = { "moves": "_______" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 7
test_input = { "moves": "L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "LL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "L_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "RR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "R_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "_L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "_R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "__" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LLL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "LLR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "LL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "LRL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "LRR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "LR_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "L_L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "L_R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "L__" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "RLL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "RLR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "RL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "RRL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "RRR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "RR_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "R_L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "R_R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "R__" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "_LL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "_LR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "_L_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "_RL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 1
test_input = { "moves": "_RR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "_R_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "__L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "__R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "___" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 3
test_input = { "moves": "LLLL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "LLLR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LLL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "LLRL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LLRR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "LLR_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LL_L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "LL_R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LL__" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "LRLL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LRLR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "LRL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LRRL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "LRRR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LRR_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LR_L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LR_R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "LR__" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "L_LL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "L_LR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "L_L_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "L_RL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "L_RR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "L_R_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "L__L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "L__R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "L___" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "RLLL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RLLR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "RLL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RLRL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "RLRR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RLR_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RL_L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RL_R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RL__" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RRLL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 0
test_input = { "moves": "RRLR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RRL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RRRL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RRRR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "RRR_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "RR_L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "RR_R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "RR__" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "R_LL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "R_LR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "R_L_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "R_RL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "R_RR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "R_R_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "R__L" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "R__R" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "R___" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "_LLL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "_LLR" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2
test_input = { "moves": "_LL_" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 4
test_input = { "moves": "_LRL" }
assert my_solution.furthestDistanceFromOrigin(**test_input) == 2 | 1,693,103,400 |
weekly-contest-360-find-the-minimum-possible-sum-of-a-beautiful-array | https://leetcode.com/problems/find-the-minimum-possible-sum-of-a-beautiful-array | find-the-minimum-possible-sum-of-a-beautiful-array | {
"questionId": "3026",
"questionFrontendId": "2834",
"title": "Find the Minimum Possible Sum of a Beautiful Array",
"titleSlug": "find-the-minimum-possible-sum-of-a-beautiful-array",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 276,
"dislikes": 37,
"categoryTitle": "Algorithms"
} | """
给你两个正整数:n 和 target 。
如果数组 nums 满足下述条件,则称其为 美丽数组 。
* nums.length == n.
* nums 由两两互不相同的正整数组成。
* 在范围 [0, n-1] 内,不存在 两个 不同 下标 i 和 j ,使得 nums[i] + nums[j] == target 。
返回符合条件的美丽数组所可能具备的 最小 和,并对结果进行取模 109 + 7。
示例 1:
输入:n = 2, target = 3
输出:4
解释:nums = [1,3] 是美丽数组。
- nums 的长度为 n = 2 。
- nums 由两两互不相同的正整数组成。
- 不存在两个不同下标 i 和 j ,使得 nums[i] + nums[j] == 3 。
可以证明 4 是符合条件的美丽数组所可能具备的最小和。
示例 2:
输入:n = 3, target = 3
输出:8
解释:
nums = [1,3,4] 是美丽数组。
- nums 的长度为 n = 3 。
- nums 由两两互不相同的正整数组成。
- 不存在两个不同下标 i 和 j ,使得 nums[i] + nums[j] == 3 。
可以证明 8 是符合条件的美丽数组所可能具备的最小和。
示例 3:
输入:n = 1, target = 1
输出:1
解释:nums = [1] 是美丽数组。
提示:
* 1 <= n <= 109
* 1 <= target <= 109
"""
class Solution:
def minimumPossibleSum(self, n: int, target: int) -> int:
| 给你两个正整数:n 和 target 。
如果数组 nums 满足下述条件,则称其为 美丽数组 。
* nums.length == n.
* nums 由两两互不相同的正整数组成。
* 在范围 [0, n-1] 内,不存在 两个 不同 下标 i 和 j ,使得 nums[i] + nums[j] == target 。
返回符合条件的美丽数组所可能具备的 最小 和,并对结果进行取模 109 + 7。
示例 1:
输入:n = 2, target = 3
输出:4
解释:nums = [1,3] 是美丽数组。
- nums 的长度为 n = 2 。
- nums 由两两互不相同的正整数组成。
- 不存在两个不同下标 i 和 j ,使得 nums[i] + nums[j] == 3 。
可以证明 4 是符合条件的美丽数组所可能具备的最小和。
示例 2:
输入:n = 3, target = 3
输出:8
解释:
nums = [1,3,4] 是美丽数组。
- nums 的长度为 n = 3 。
- nums 由两两互不相同的正整数组成。
- 不存在两个不同下标 i 和 j ,使得 nums[i] + nums[j] == 3 。
可以证明 8 是符合条件的美丽数组所可能具备的最小和。
示例 3:
输入:n = 1, target = 1
输出:1
解释:nums = [1] 是美丽数组。
提示:
* 1 <= n <= 109
* 1 <= target <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumPossibleSum(self, n: int, target: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 2, "target": 3 }
assert my_solution.minimumPossibleSum(**test_input) == 4
test_input = { "n": 3, "target": 3 }
assert my_solution.minimumPossibleSum(**test_input) == 8
test_input = { "n": 1, "target": 1 }
assert my_solution.minimumPossibleSum(**test_input) == 1
test_input = { "n": 16, "target": 6 }
assert my_solution.minimumPossibleSum(**test_input) == 162
test_input = { "n": 16, "target": 32 }
assert my_solution.minimumPossibleSum(**test_input) == 136
test_input = { "n": 13, "target": 50 }
assert my_solution.minimumPossibleSum(**test_input) == 91
test_input = { "n": 36, "target": 21 }
assert my_solution.minimumPossibleSum(**test_input) == 926
test_input = { "n": 40, "target": 17 }
assert my_solution.minimumPossibleSum(**test_input) == 1076
test_input = { "n": 37, "target": 46 }
assert my_solution.minimumPossibleSum(**test_input) == 1011
test_input = { "n": 33, "target": 7 }
assert my_solution.minimumPossibleSum(**test_input) == 651
test_input = { "n": 42, "target": 46 }
assert my_solution.minimumPossibleSum(**test_input) == 1321
test_input = { "n": 46, "target": 29 }
assert my_solution.minimumPossibleSum(**test_input) == 1529
test_input = { "n": 9, "target": 43 }
assert my_solution.minimumPossibleSum(**test_input) == 45
test_input = { "n": 30, "target": 31 }
assert my_solution.minimumPossibleSum(**test_input) == 690
test_input = { "n": 14, "target": 47 }
assert my_solution.minimumPossibleSum(**test_input) == 105
test_input = { "n": 5, "target": 3 }
assert my_solution.minimumPossibleSum(**test_input) == 19
test_input = { "n": 41, "target": 23 }
assert my_solution.minimumPossibleSum(**test_input) == 1191
test_input = { "n": 17, "target": 13 }
assert my_solution.minimumPossibleSum(**test_input) == 219
test_input = { "n": 9, "target": 13 }
assert my_solution.minimumPossibleSum(**test_input) == 63
test_input = { "n": 29, "target": 18 }
assert my_solution.minimumPossibleSum(**test_input) == 595
test_input = { "n": 21, "target": 14 }
assert my_solution.minimumPossibleSum(**test_input) == 315
test_input = { "n": 4, "target": 6 }
assert my_solution.minimumPossibleSum(**test_input) == 12
test_input = { "n": 38, "target": 15 }
assert my_solution.minimumPossibleSum(**test_input) == 958
test_input = { "n": 14, "target": 7 }
assert my_solution.minimumPossibleSum(**test_input) == 138
test_input = { "n": 18, "target": 26 }
assert my_solution.minimumPossibleSum(**test_input) == 231
test_input = { "n": 11, "target": 15 }
assert my_solution.minimumPossibleSum(**test_input) == 94
test_input = { "n": 7, "target": 8 }
assert my_solution.minimumPossibleSum(**test_input) == 37
test_input = { "n": 11, "target": 22 }
assert my_solution.minimumPossibleSum(**test_input) == 66
test_input = { "n": 36, "target": 11 }
assert my_solution.minimumPossibleSum(**test_input) == 821
test_input = { "n": 18, "target": 29 }
assert my_solution.minimumPossibleSum(**test_input) == 227
test_input = { "n": 26, "target": 17 }
assert my_solution.minimumPossibleSum(**test_input) == 495
test_input = { "n": 37, "target": 13 }
assert my_solution.minimumPossibleSum(**test_input) == 889
test_input = { "n": 46, "target": 38 }
assert my_solution.minimumPossibleSum(**test_input) == 1567
test_input = { "n": 13, "target": 7 }
assert my_solution.minimumPossibleSum(**test_input) == 121
test_input = { "n": 33, "target": 34 }
assert my_solution.minimumPossibleSum(**test_input) == 817
test_input = { "n": 39, "target": 12 }
assert my_solution.minimumPossibleSum(**test_input) == 945
test_input = { "n": 1, "target": 45 }
assert my_solution.minimumPossibleSum(**test_input) == 1
test_input = { "n": 37, "target": 36 }
assert my_solution.minimumPossibleSum(**test_input) == 1026
test_input = { "n": 16, "target": 19 }
assert my_solution.minimumPossibleSum(**test_input) == 199
test_input = { "n": 22, "target": 15 }
assert my_solution.minimumPossibleSum(**test_input) == 358
test_input = { "n": 34, "target": 42 }
assert my_solution.minimumPossibleSum(**test_input) == 855
test_input = { "n": 50, "target": 22 }
assert my_solution.minimumPossibleSum(**test_input) == 1665
test_input = { "n": 42, "target": 44 }
assert my_solution.minimumPossibleSum(**test_input) == 1323
test_input = { "n": 40, "target": 8 }
assert my_solution.minimumPossibleSum(**test_input) == 928
test_input = { "n": 7, "target": 19 }
assert my_solution.minimumPossibleSum(**test_input) == 28
test_input = { "n": 44, "target": 10 }
assert my_solution.minimumPossibleSum(**test_input) == 1146
test_input = { "n": 21, "target": 6 }
assert my_solution.minimumPossibleSum(**test_input) == 267
test_input = { "n": 27, "target": 26 }
assert my_solution.minimumPossibleSum(**test_input) == 546
test_input = { "n": 49, "target": 4 }
assert my_solution.minimumPossibleSum(**test_input) == 1272
test_input = { "n": 35, "target": 2 }
assert my_solution.minimumPossibleSum(**test_input) == 630
test_input = { "n": 32, "target": 29 }
assert my_solution.minimumPossibleSum(**test_input) == 780
test_input = { "n": 20, "target": 41 }
assert my_solution.minimumPossibleSum(**test_input) == 210
test_input = { "n": 30, "target": 48 }
assert my_solution.minimumPossibleSum(**test_input) == 603
test_input = { "n": 12, "target": 34 }
assert my_solution.minimumPossibleSum(**test_input) == 78
test_input = { "n": 50, "target": 44 }
assert my_solution.minimumPossibleSum(**test_input) == 1863
test_input = { "n": 42, "target": 26 }
assert my_solution.minimumPossibleSum(**test_input) == 1251
test_input = { "n": 3, "target": 18 }
assert my_solution.minimumPossibleSum(**test_input) == 6
test_input = { "n": 11, "target": 3 }
assert my_solution.minimumPossibleSum(**test_input) == 76
test_input = { "n": 38, "target": 29 }
assert my_solution.minimumPossibleSum(**test_input) == 1077
test_input = { "n": 17, "target": 24 }
assert my_solution.minimumPossibleSum(**test_input) == 208
test_input = { "n": 50, "target": 31 }
assert my_solution.minimumPossibleSum(**test_input) == 1800
test_input = { "n": 32, "target": 41 }
assert my_solution.minimumPossibleSum(**test_input) == 768
test_input = { "n": 12, "target": 24 }
assert my_solution.minimumPossibleSum(**test_input) == 78
test_input = { "n": 35, "target": 43 }
assert my_solution.minimumPossibleSum(**test_input) == 924
test_input = { "n": 9, "target": 47 }
assert my_solution.minimumPossibleSum(**test_input) == 45
test_input = { "n": 32, "target": 26 }
assert my_solution.minimumPossibleSum(**test_input) == 756
test_input = { "n": 6, "target": 42 }
assert my_solution.minimumPossibleSum(**test_input) == 21
test_input = { "n": 11, "target": 1 }
assert my_solution.minimumPossibleSum(**test_input) == 66
test_input = { "n": 11, "target": 24 }
assert my_solution.minimumPossibleSum(**test_input) == 66
test_input = { "n": 39, "target": 38 }
assert my_solution.minimumPossibleSum(**test_input) == 1140
test_input = { "n": 18, "target": 8 }
assert my_solution.minimumPossibleSum(**test_input) == 213
test_input = { "n": 29, "target": 5 }
assert my_solution.minimumPossibleSum(**test_input) == 489
test_input = { "n": 44, "target": 6 }
assert my_solution.minimumPossibleSum(**test_input) == 1072
test_input = { "n": 29, "target": 30 }
assert my_solution.minimumPossibleSum(**test_input) == 631
test_input = { "n": 13, "target": 47 }
assert my_solution.minimumPossibleSum(**test_input) == 91
test_input = { "n": 46, "target": 21 }
assert my_solution.minimumPossibleSum(**test_input) == 1441
test_input = { "n": 46, "target": 6 }
assert my_solution.minimumPossibleSum(**test_input) == 1167
test_input = { "n": 27, "target": 30 }
assert my_solution.minimumPossibleSum(**test_input) == 546
test_input = { "n": 5, "target": 35 }
assert my_solution.minimumPossibleSum(**test_input) == 15
test_input = { "n": 12, "target": 32 }
assert my_solution.minimumPossibleSum(**test_input) == 78
test_input = { "n": 13, "target": 39 }
assert my_solution.minimumPossibleSum(**test_input) == 91
test_input = { "n": 10, "target": 11 }
assert my_solution.minimumPossibleSum(**test_input) == 80
test_input = { "n": 46, "target": 14 }
assert my_solution.minimumPossibleSum(**test_input) == 1315
test_input = { "n": 37, "target": 18 }
assert my_solution.minimumPossibleSum(**test_input) == 927
test_input = { "n": 32, "target": 8 }
assert my_solution.minimumPossibleSum(**test_input) == 612
test_input = { "n": 26, "target": 14 }
assert my_solution.minimumPossibleSum(**test_input) == 465
test_input = { "n": 33, "target": 41 }
assert my_solution.minimumPossibleSum(**test_input) == 821
test_input = { "n": 44, "target": 39 }
assert my_solution.minimumPossibleSum(**test_input) == 1465
test_input = { "n": 3, "target": 21 }
assert my_solution.minimumPossibleSum(**test_input) == 6
test_input = { "n": 9, "target": 11 }
assert my_solution.minimumPossibleSum(**test_input) == 65
test_input = { "n": 16, "target": 43 }
assert my_solution.minimumPossibleSum(**test_input) == 136
test_input = { "n": 7, "target": 22 }
assert my_solution.minimumPossibleSum(**test_input) == 28
test_input = { "n": 21, "target": 49 }
assert my_solution.minimumPossibleSum(**test_input) == 231
test_input = { "n": 23, "target": 16 }
assert my_solution.minimumPossibleSum(**test_input) == 381
test_input = { "n": 33, "target": 32 }
assert my_solution.minimumPossibleSum(**test_input) == 816
test_input = { "n": 22, "target": 1 }
assert my_solution.minimumPossibleSum(**test_input) == 253
test_input = { "n": 5, "target": 47 }
assert my_solution.minimumPossibleSum(**test_input) == 15
test_input = { "n": 38, "target": 7 }
assert my_solution.minimumPossibleSum(**test_input) == 846
test_input = { "n": 38, "target": 24 }
assert my_solution.minimumPossibleSum(**test_input) == 1027
test_input = { "n": 13, "target": 36 }
assert my_solution.minimumPossibleSum(**test_input) == 91 | 1,693,103,400 |
weekly-contest-360-minimum-operations-to-form-subsequence-with-target-sum | https://leetcode.com/problems/minimum-operations-to-form-subsequence-with-target-sum | minimum-operations-to-form-subsequence-with-target-sum | {
"questionId": "3025",
"questionFrontendId": "2835",
"title": "Minimum Operations to Form Subsequence With Target Sum",
"titleSlug": "minimum-operations-to-form-subsequence-with-target-sum",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 503,
"dislikes": 125,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的数组 nums ,它包含 非负 整数,且全部为 2 的幂,同时给你一个整数 target 。
一次操作中,你必须对数组做以下修改:
* 选择数组中一个元素 nums[i] ,满足 nums[i] > 1 。
* 将 nums[i] 从数组中删除。
* 在 nums 的 末尾 添加 两个 数,值都为 nums[i] / 2 。
你的目标是让 nums 的一个 子序列 的元素和等于 target ,请你返回达成这一目标的 最少操作次数 。如果无法得到这样的子序列,请你返回 -1 。
数组中一个 子序列 是通过删除原数组中一些元素,并且不改变剩余元素顺序得到的剩余数组。
示例 1:
输入:nums = [1,2,8], target = 7
输出:1
解释:第一次操作中,我们选择元素 nums[2] 。数组变为 nums = [1,2,4,4] 。
这时候,nums 包含子序列 [1,2,4] ,和为 7 。
无法通过更少的操作得到和为 7 的子序列。
示例 2:
输入:nums = [1,32,1,2], target = 12
输出:2
解释:第一次操作中,我们选择元素 nums[1] 。数组变为 nums = [1,1,2,16,16] 。
第二次操作中,我们选择元素 nums[3] 。数组变为 nums = [1,1,2,16,8,8] 。
这时候,nums 包含子序列 [1,1,2,8] ,和为 12 。
无法通过更少的操作得到和为 12 的子序列。
示例 3:
输入:nums = [1,32,1], target = 35
输出:-1
解释:无法得到和为 35 的子序列。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 230
* nums 只包含非负整数,且均为 2 的幂。
* 1 <= target < 231
"""
class Solution:
def minOperations(self, nums: List[int], target: int) -> int:
| 给你一个下标从 0 开始的数组 nums ,它包含 非负 整数,且全部为 2 的幂,同时给你一个整数 target 。
一次操作中,你必须对数组做以下修改:
* 选择数组中一个元素 nums[i] ,满足 nums[i] > 1 。
* 将 nums[i] 从数组中删除。
* 在 nums 的 末尾 添加 两个 数,值都为 nums[i] / 2 。
你的目标是让 nums 的一个 子序列 的元素和等于 target ,请你返回达成这一目标的 最少操作次数 。如果无法得到这样的子序列,请你返回 -1 。
数组中一个 子序列 是通过删除原数组中一些元素,并且不改变剩余元素顺序得到的剩余数组。
示例 1:
输入:nums = [1,2,8], target = 7
输出:1
解释:第一次操作中,我们选择元素 nums[2] 。数组变为 nums = [1,2,4,4] 。
这时候,nums 包含子序列 [1,2,4] ,和为 7 。
无法通过更少的操作得到和为 7 的子序列。
示例 2:
输入:nums = [1,32,1,2], target = 12
输出:2
解释:第一次操作中,我们选择元素 nums[1] 。数组变为 nums = [1,1,2,16,16] 。
第二次操作中,我们选择元素 nums[3] 。数组变为 nums = [1,1,2,16,8,8] 。
这时候,nums 包含子序列 [1,1,2,8] ,和为 12 。
无法通过更少的操作得到和为 12 的子序列。
示例 3:
输入:nums = [1,32,1], target = 35
输出:-1
解释:无法得到和为 35 的子序列。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 230
* nums 只包含非负整数,且均为 2 的幂。
* 1 <= target < 231
请完成下面的代码来解决上述问题:
```python
class Solution:
def minOperations(self, nums: List[int], target: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,8], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,32,1,2], "target": 12 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,32,1], "target": 35 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums": [1], "target": 1 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [16,128,32], "target": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,1], "target": 2 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,128,128], "target": 2 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [2], "target": 2 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [32,256,4], "target": 2 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,1], "target": 3 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,256,16,128], "target": 3 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2], "target": 3 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [16,16,4], "target": 3 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,1], "target": 4 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [128,1,128,1,64], "target": 4 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [2,1,1], "target": 4 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [8,2,64,32], "target": 4 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [16,128,8,1,1], "target": 4 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2,1], "target": 4 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [128,8,8,2], "target": 4 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,2], "target": 4 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [128,32,16], "target": 4 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [4], "target": 4 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [128,32,256], "target": 4 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,1,1,1], "target": 5 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,1,128,256,1,16], "target": 5 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,1,1,1], "target": 5 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [32,1,8,1,64], "target": 5 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [128,256,32,1,1,1], "target": 5 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,2,1], "target": 5 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [16,16,1,1,128], "target": 5 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,2,1], "target": 5 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,32,2,8], "target": 5 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,4], "target": 5 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [8,64,128], "target": 5 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,1,1,1,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [128,1,256,1,1,1,32], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,1,1,2], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,256,1,1,8,64], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,128,1,128,1,8,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,1,2,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,1,16,1,1,64], "target": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,1,2,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [2,1,8,64,64], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [4,1,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [16,64,4,128], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,1,8,128,1,8], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2,1,1,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,128,1,32,1,2], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,1,256,1,8,64], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,128,1,256,32,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,1,2], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [256,2,32,32,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,16,1,1,1,16,256], "target": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,2,1,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,1,64,8,16,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,16,128,1,1,32,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [128,1,1,1,64,32], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [64,16,16,1,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [32,1,1,32,1,64,1], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [8,64,2,1,1,8], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,256,1,64,1,128], "target": 6 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,1,64,64,32,2], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,32,128,1,64], "target": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,2,2], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,8,2,128], "target": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [4,2], "target": 6 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [8,8,16], "target": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,1,1,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,1,1,1,128,1,64,8], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,1,1,1,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,2,64,32,16,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,16,128,256,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,2,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [2,8,8,1,128,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,2,1,1,2], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [32,1,2,1,256,128], "target": 7 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,1,4], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,8,256,32,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [64,1,1,1,16,1,64,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,1,1,1,2,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,256,1,8,1,2,16], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [32,1,1,1,32,32,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,1,1,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,1,32,256,1,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [2,1,2,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [2,32,32,32,2,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,1,8,1,1,64,1,128], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,1,1,1,2], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,128,2,128,1,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,1,1,128,8,32,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [64,256,1,1,1,32,2], "target": 7 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [1,2,2,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [1,256,64,2,1,128], "target": 7 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [256,1,1,1,64,1,1,64], "target": 7 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [2,1,8,256,1,128,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,1,16,1,1,256,1,128], "target": 7 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,8,128,1,1,1,256], "target": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,2,1,1,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [64,2,64,1,32,1], "target": 7 }
assert my_solution.minOperations(**test_input) == 3 | 1,693,103,400 |
weekly-contest-360-maximize-value-of-function-in-a-ball-passing-game | https://leetcode.com/problems/maximize-value-of-function-in-a-ball-passing-game | maximize-value-of-function-in-a-ball-passing-game | {
"questionId": "3032",
"questionFrontendId": "2836",
"title": "Maximize Value of Function in a Ball Passing Game",
"titleSlug": "maximize-value-of-function-in-a-ball-passing-game",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 202,
"dislikes": 85,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 下标从 0 开始的整数数组 receiver 和一个整数 k 。
总共有 n 名玩家,玩家 编号 互不相同,且为 [0, n - 1] 中的整数。这些玩家玩一个传球游戏,receiver[i] 表示编号为 i 的玩家会传球给编号为 receiver[i] 的玩家。玩家可以传球给自己,也就是说 receiver[i] 可能等于 i 。
你需要从 n 名玩家中选择一名玩家作为游戏开始时唯一手中有球的玩家,球会被传 恰好 k 次。
如果选择编号为 x 的玩家作为开始玩家,定义函数 f(x) 表示从编号为 x 的玩家开始,k 次传球内所有接触过球玩家的编号之 和 ,如果有玩家多次触球,则 累加多次 。换句话说, f(x) = x + receiver[x] + receiver[receiver[x]] + ... + receiver(k)[x] 。
你的任务时选择开始玩家 x ,目的是 最大化 f(x) 。
请你返回函数的 最大值 。
注意:receiver 可能含有重复元素。
示例 1:
传递次数 传球者编号 接球者编号 x + 所有接球者编号 2 1 2 1 3 2 1 0 3 3 0 2 5 4 2 1 6
输入:receiver = [2,0,1], k = 4
输出:6
解释:上表展示了从编号为 x = 2 开始的游戏过程。
从表中可知,f(2) 等于 6 。
6 是能得到最大的函数值。
所以输出为 6 。
示例 2:
传递次数 传球者编号 接球者编号 x + 所有接球者编号 4 1 4 3 7 2 3 2 9 3 2 1 10
输入:receiver = [1,1,1,2,3], k = 3
输出:10
解释:上表展示了从编号为 x = 4 开始的游戏过程。
从表中可知,f(4) 等于 10 。
10 是能得到最大的函数值。
所以输出为 10 。
提示:
* 1 <= receiver.length == n <= 105
* 0 <= receiver[i] <= n - 1
* 1 <= k <= 1010
"""
class Solution:
def getMaxFunctionValue(self, receiver: List[int], k: int) -> int:
| 给你一个长度为 n 下标从 0 开始的整数数组 receiver 和一个整数 k 。
总共有 n 名玩家,玩家 编号 互不相同,且为 [0, n - 1] 中的整数。这些玩家玩一个传球游戏,receiver[i] 表示编号为 i 的玩家会传球给编号为 receiver[i] 的玩家。玩家可以传球给自己,也就是说 receiver[i] 可能等于 i 。
你需要从 n 名玩家中选择一名玩家作为游戏开始时唯一手中有球的玩家,球会被传 恰好 k 次。
如果选择编号为 x 的玩家作为开始玩家,定义函数 f(x) 表示从编号为 x 的玩家开始,k 次传球内所有接触过球玩家的编号之 和 ,如果有玩家多次触球,则 累加多次 。换句话说, f(x) = x + receiver[x] + receiver[receiver[x]] + ... + receiver(k)[x] 。
你的任务时选择开始玩家 x ,目的是 最大化 f(x) 。
请你返回函数的 最大值 。
注意:receiver 可能含有重复元素。
示例 1:
传递次数 传球者编号 接球者编号 x + 所有接球者编号 2 1 2 1 3 2 1 0 3 3 0 2 5 4 2 1 6
输入:receiver = [2,0,1], k = 4
输出:6
解释:上表展示了从编号为 x = 2 开始的游戏过程。
从表中可知,f(2) 等于 6 。
6 是能得到最大的函数值。
所以输出为 6 。
示例 2:
传递次数 传球者编号 接球者编号 x + 所有接球者编号 4 1 4 3 7 2 3 2 9 3 2 1 10
输入:receiver = [1,1,1,2,3], k = 3
输出:10
解释:上表展示了从编号为 x = 4 开始的游戏过程。
从表中可知,f(4) 等于 10 。
10 是能得到最大的函数值。
所以输出为 10 。
提示:
* 1 <= receiver.length == n <= 105
* 0 <= receiver[i] <= n - 1
* 1 <= k <= 1010
请完成下面的代码来解决上述问题:
```python
class Solution:
def getMaxFunctionValue(self, receiver: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "receiver": [2,0,1], "k": 4 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [1,1,1,2,3], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 10
test_input = { "receiver": [0], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 0
test_input = { "receiver": [0], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 0
test_input = { "receiver": [0], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 0
test_input = { "receiver": [0], "k": 100 }
assert my_solution.getMaxFunctionValue(**test_input) == 0
test_input = { "receiver": [0,0], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 1
test_input = { "receiver": [0,0], "k": 7 }
assert my_solution.getMaxFunctionValue(**test_input) == 1
test_input = { "receiver": [0,0], "k": 10 }
assert my_solution.getMaxFunctionValue(**test_input) == 1
test_input = { "receiver": [0,0], "k": 13 }
assert my_solution.getMaxFunctionValue(**test_input) == 1
test_input = { "receiver": [0,0], "k": 16 }
assert my_solution.getMaxFunctionValue(**test_input) == 1
test_input = { "receiver": [0,1], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 2
test_input = { "receiver": [0,1], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 4
test_input = { "receiver": [0,1], "k": 5 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [0,1], "k": 8 }
assert my_solution.getMaxFunctionValue(**test_input) == 9
test_input = { "receiver": [0,1], "k": 13 }
assert my_solution.getMaxFunctionValue(**test_input) == 14
test_input = { "receiver": [0,1], "k": 14 }
assert my_solution.getMaxFunctionValue(**test_input) == 15
test_input = { "receiver": [0,1], "k": 15 }
assert my_solution.getMaxFunctionValue(**test_input) == 16
test_input = { "receiver": [1,0], "k": 5 }
assert my_solution.getMaxFunctionValue(**test_input) == 3
test_input = { "receiver": [1,0], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 4
test_input = { "receiver": [1,0], "k": 7 }
assert my_solution.getMaxFunctionValue(**test_input) == 4
test_input = { "receiver": [1,0], "k": 10 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [1,0], "k": 12 }
assert my_solution.getMaxFunctionValue(**test_input) == 7
test_input = { "receiver": [1,0], "k": 14 }
assert my_solution.getMaxFunctionValue(**test_input) == 8
test_input = { "receiver": [1,0], "k": 57 }
assert my_solution.getMaxFunctionValue(**test_input) == 29
test_input = { "receiver": [1,1], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 2
test_input = { "receiver": [1,1], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 3
test_input = { "receiver": [1,1], "k": 7 }
assert my_solution.getMaxFunctionValue(**test_input) == 8
test_input = { "receiver": [0,0,0], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 2
test_input = { "receiver": [0,0,0], "k": 4 }
assert my_solution.getMaxFunctionValue(**test_input) == 2
test_input = { "receiver": [0,0,0], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 2
test_input = { "receiver": [0,0,0], "k": 10 }
assert my_solution.getMaxFunctionValue(**test_input) == 2
test_input = { "receiver": [0,0,1], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 3
test_input = { "receiver": [0,0,1], "k": 9 }
assert my_solution.getMaxFunctionValue(**test_input) == 3
test_input = { "receiver": [0,0,2], "k": 11 }
assert my_solution.getMaxFunctionValue(**test_input) == 24
test_input = { "receiver": [0,0,2], "k": 14 }
assert my_solution.getMaxFunctionValue(**test_input) == 30
test_input = { "receiver": [0,0,2], "k": 82 }
assert my_solution.getMaxFunctionValue(**test_input) == 166
test_input = { "receiver": [0,1,0], "k": 5 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [0,1,1], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 4
test_input = { "receiver": [0,1,2], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 8
test_input = { "receiver": [0,1,2], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 14
test_input = { "receiver": [1,0,0], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 5
test_input = { "receiver": [1,0,1], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 3
test_input = { "receiver": [1,0,2], "k": 10 }
assert my_solution.getMaxFunctionValue(**test_input) == 22
test_input = { "receiver": [1,1,1], "k": 4 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [1,1,1], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 8
test_input = { "receiver": [1,1,2], "k": 8 }
assert my_solution.getMaxFunctionValue(**test_input) == 18
test_input = { "receiver": [1,2,0], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 5
test_input = { "receiver": [1,2,0], "k": 8 }
assert my_solution.getMaxFunctionValue(**test_input) == 9
test_input = { "receiver": [1,2,1], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 11
test_input = { "receiver": [1,2,2], "k": 7 }
assert my_solution.getMaxFunctionValue(**test_input) == 16
test_input = { "receiver": [2,0,2], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 14
test_input = { "receiver": [2,1,0], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 4
test_input = { "receiver": [2,1,0], "k": 8 }
assert my_solution.getMaxFunctionValue(**test_input) == 10
test_input = { "receiver": [2,1,0], "k": 10 }
assert my_solution.getMaxFunctionValue(**test_input) == 12
test_input = { "receiver": [2,1,1], "k": 4 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [2,1,2], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [2,1,2], "k": 15 }
assert my_solution.getMaxFunctionValue(**test_input) == 32
test_input = { "receiver": [2,2,0], "k": 4 }
assert my_solution.getMaxFunctionValue(**test_input) == 6
test_input = { "receiver": [2,2,0], "k": 9 }
assert my_solution.getMaxFunctionValue(**test_input) == 11
test_input = { "receiver": [2,2,1], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 3
test_input = { "receiver": [2,2,1], "k": 10 }
assert my_solution.getMaxFunctionValue(**test_input) == 17
test_input = { "receiver": [2,2,2], "k": 15 }
assert my_solution.getMaxFunctionValue(**test_input) == 32
test_input = { "receiver": [0,0,3,0], "k": 4 }
assert my_solution.getMaxFunctionValue(**test_input) == 5
test_input = { "receiver": [0,1,0,1], "k": 11 }
assert my_solution.getMaxFunctionValue(**test_input) == 14
test_input = { "receiver": [0,1,1,3], "k": 5 }
assert my_solution.getMaxFunctionValue(**test_input) == 18
test_input = { "receiver": [0,2,1,3], "k": 5 }
assert my_solution.getMaxFunctionValue(**test_input) == 18
test_input = { "receiver": [0,2,3,1], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 15
test_input = { "receiver": [0,2,3,3], "k": 8 }
assert my_solution.getMaxFunctionValue(**test_input) == 27
test_input = { "receiver": [0,2,3,3], "k": 15 }
assert my_solution.getMaxFunctionValue(**test_input) == 48
test_input = { "receiver": [0,3,3,0], "k": 10 }
assert my_solution.getMaxFunctionValue(**test_input) == 5
test_input = { "receiver": [1,0,0,2], "k": 9 }
assert my_solution.getMaxFunctionValue(**test_input) == 9
test_input = { "receiver": [1,0,1,2], "k": 15 }
assert my_solution.getMaxFunctionValue(**test_input) == 12
test_input = { "receiver": [1,0,3,1], "k": 6 }
assert my_solution.getMaxFunctionValue(**test_input) == 8
test_input = { "receiver": [1,0,3,2], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 8
test_input = { "receiver": [1,1,0,0], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 4
test_input = { "receiver": [1,1,0,3], "k": 3 }
assert my_solution.getMaxFunctionValue(**test_input) == 12
test_input = { "receiver": [1,1,1,3], "k": 7 }
assert my_solution.getMaxFunctionValue(**test_input) == 24
test_input = { "receiver": [1,2,0,0], "k": 14 }
assert my_solution.getMaxFunctionValue(**test_input) == 16
test_input = { "receiver": [1,2,0,1], "k": 5 }
assert my_solution.getMaxFunctionValue(**test_input) == 9
test_input = { "receiver": [1,2,3,1], "k": 47 }
assert my_solution.getMaxFunctionValue(**test_input) == 96
test_input = { "receiver": [1,3,0,1], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 7
test_input = { "receiver": [1,3,3,0], "k": 7 }
assert my_solution.getMaxFunctionValue(**test_input) == 13
test_input = { "receiver": [2,0,0,1], "k": 9 }
assert my_solution.getMaxFunctionValue(**test_input) == 12
test_input = { "receiver": [2,0,0,2], "k": 12 }
assert my_solution.getMaxFunctionValue(**test_input) == 15
test_input = { "receiver": [2,0,2,0], "k": 5 }
assert my_solution.getMaxFunctionValue(**test_input) == 12
test_input = { "receiver": [2,1,0,0], "k": 8 }
assert my_solution.getMaxFunctionValue(**test_input) == 11
test_input = { "receiver": [2,1,0,1], "k": 97 }
assert my_solution.getMaxFunctionValue(**test_input) == 100
test_input = { "receiver": [2,2,2,0], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 4
test_input = { "receiver": [2,2,3,2], "k": 8 }
assert my_solution.getMaxFunctionValue(**test_input) == 23
test_input = { "receiver": [2,3,2,1], "k": 56 }
assert my_solution.getMaxFunctionValue(**test_input) == 115
test_input = { "receiver": [2,3,3,1], "k": 15 }
assert my_solution.getMaxFunctionValue(**test_input) == 33
test_input = { "receiver": [2,3,3,3], "k": 2 }
assert my_solution.getMaxFunctionValue(**test_input) == 9
test_input = { "receiver": [3,0,0,1], "k": 4 }
assert my_solution.getMaxFunctionValue(**test_input) == 8
test_input = { "receiver": [3,0,0,2], "k": 85 }
assert my_solution.getMaxFunctionValue(**test_input) == 145
test_input = { "receiver": [3,0,1,3], "k": 9 }
assert my_solution.getMaxFunctionValue(**test_input) == 30
test_input = { "receiver": [3,1,1,2], "k": 7 }
assert my_solution.getMaxFunctionValue(**test_input) == 11
test_input = { "receiver": [3,1,2,0], "k": 60 }
assert my_solution.getMaxFunctionValue(**test_input) == 122
test_input = { "receiver": [3,2,0,3], "k": 12 }
assert my_solution.getMaxFunctionValue(**test_input) == 39
test_input = { "receiver": [3,3,0,1], "k": 1 }
assert my_solution.getMaxFunctionValue(**test_input) == 4 | 1,693,103,400 |
weekly-contest-359-check-if-a-string-is-an-acronym-of-words | https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words | check-if-a-string-is-an-acronym-of-words | {
"questionId": "2977",
"questionFrontendId": "2828",
"title": "Check if a String Is an Acronym of Words",
"titleSlug": "check-if-a-string-is-an-acronym-of-words",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 257,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
给你一个字符串数组 words 和一个字符串 s ,请你判断 s 是不是 words 的 首字母缩略词 。
如果可以按顺序串联 words 中每个字符串的第一个字符形成字符串 s ,则认为 s 是 words 的首字母缩略词。例如,"ab" 可以由 ["apple", "banana"] 形成,但是无法从 ["bear", "aardvark"] 形成。
如果 s 是 words 的首字母缩略词,返回 true ;否则,返回 false 。
示例 1:
输入:words = ["alice","bob","charlie"], s = "abc"
输出:true
解释:words 中 "alice"、"bob" 和 "charlie" 的第一个字符分别是 'a'、'b' 和 'c'。因此,s = "abc" 是首字母缩略词。
示例 2:
输入:words = ["an","apple"], s = "a"
输出:false
解释:words 中 "an" 和 "apple" 的第一个字符分别是 'a' 和 'a'。
串联这些字符形成的首字母缩略词是 "aa" 。
因此,s = "a" 不是首字母缩略词。
示例 3:
输入:words = ["never","gonna","give","up","on","you"], s = "ngguoy"
输出:true
解释:串联数组 words 中每个字符串的第一个字符,得到字符串 "ngguoy" 。
因此,s = "ngguoy" 是首字母缩略词。
提示:
* 1 <= words.length <= 100
* 1 <= words[i].length <= 10
* 1 <= s.length <= 100
* words[i] 和 s 由小写英文字母组成
"""
class Solution:
def isAcronym(self, words: List[str], s: str) -> bool:
| 给你一个字符串数组 words 和一个字符串 s ,请你判断 s 是不是 words 的 首字母缩略词 。
如果可以按顺序串联 words 中每个字符串的第一个字符形成字符串 s ,则认为 s 是 words 的首字母缩略词。例如,"ab" 可以由 ["apple", "banana"] 形成,但是无法从 ["bear", "aardvark"] 形成。
如果 s 是 words 的首字母缩略词,返回 true ;否则,返回 false 。
示例 1:
输入:words = ["alice","bob","charlie"], s = "abc"
输出:true
解释:words 中 "alice"、"bob" 和 "charlie" 的第一个字符分别是 'a'、'b' 和 'c'。因此,s = "abc" 是首字母缩略词。
示例 2:
输入:words = ["an","apple"], s = "a"
输出:false
解释:words 中 "an" 和 "apple" 的第一个字符分别是 'a' 和 'a'。
串联这些字符形成的首字母缩略词是 "aa" 。
因此,s = "a" 不是首字母缩略词。
示例 3:
输入:words = ["never","gonna","give","up","on","you"], s = "ngguoy"
输出:true
解释:串联数组 words 中每个字符串的第一个字符,得到字符串 "ngguoy" 。
因此,s = "ngguoy" 是首字母缩略词。
提示:
* 1 <= words.length <= 100
* 1 <= words[i].length <= 10
* 1 <= s.length <= 100
* words[i] 和 s 由小写英文字母组成
请完成下面的代码来解决上述问题:
```python
class Solution:
def isAcronym(self, words: List[str], s: str) -> bool:
``` |
my_solution = Solution()
test_input = { "words": ["an","apple"], "s": "a" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["alice","bob","charlie"], "s": "abc" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["never","gonna","give","up","on","you"], "s": "ngguoy" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ad","uadhrwxki"], "s": "au" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["afqcpzsx","icenu"], "s": "yi" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["afkc","icxufam"], "s": "ai" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ahbibag","aoximesw"], "s": "aa" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["auqoc","koioxa"], "s": "ak" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["b","x"], "s": "bx" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["c","df"], "s": "bd" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["dv","g"], "s": "sg" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["dvn","acafe"], "s": "dp" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["bpctc","kaqquqbpmw"], "s": "bk" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["c","evlvvhrsqa"], "s": "ce" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["dwrvgkxdtm","wy"], "s": "hw" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["eceekigel","gmgdfvsrkw"], "s": "wg" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["cfsrsyt","md"], "s": "cm" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["envpklvi","jpymde"], "s": "en" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["espleklys","dg"], "s": "ea" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["euptjhixnu","fwci"], "s": "kf" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["ddnlfpvy","exs"], "s": "de" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["deacf","hldiauk"], "s": "dh" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["dllcn","tnzrnzypg"], "s": "dt" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["dmekslxlpo","wqdgxqwdk"], "s": "dw" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["eyzywjsxza","jxeimcc"], "s": "ex" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["f","oylvtltvo"], "s": "ho" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["eo","e"], "s": "ee" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["eucvcqdgg","qtdwhygerb"], "s": "eq" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["fnpow","ysqwqli"], "s": "jy" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["gpqyvv","kihi"], "s": "ik" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["exrgiw","irexgmrl"], "s": "ei" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ez","acnmits"], "s": "ea" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["fvkekkv","jfbv"], "s": "fj" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["kncge","nje"], "s": "kw" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["fyocwzlz","lz"], "s": "fl" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["mnh","clep"], "s": "pc" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["mnpdwq","hziusbxr"], "s": "mg" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["g","r"], "s": "gr" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["n","fddigeie"], "s": "hf" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["gle","irt"], "s": "gi" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["h","xhtkcj"], "s": "hx" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["n","ityua"], "s": "ei" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["nmxysdim","xnpqsauh"], "s": "ne" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["ovdhflcck","ndd"], "s": "oi" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["piiyodecdf","wdwfxsjfou"], "s": "pp" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["hdmwkr","jfrqh"], "s": "hj" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["hflf","fvnotmdcpw"], "s": "hf" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["hnwphhozqw","cfhsjlqj"], "s": "hc" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["pxcsaaa","lrvxsc"], "s": "pz" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["htlsq","y"], "s": "hy" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["iakfeop","pd"], "s": "ip" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["qir","qyyzmntl"], "s": "qa" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["iakfmr","gzggxzwor"], "s": "ig" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["jna","rjdbu"], "s": "jr" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["qunqyc","ouzjotitvn"], "s": "co" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["rdednrsn","yfrgdeapme"], "s": "ny" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["rtnbfaemv","kgpcwaoik"], "s": "rf" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["s","n"], "s": "sx" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["siiyqtkyis","mogzgabcgk"], "s": "fm" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["tit","pmuqzrs"], "s": "tz" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["uip","hhstwupgg"], "s": "eh" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["uyj","jlfnksqlt"], "s": "ur" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["kabfejv","g"], "s": "kg" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["w","eshensjifo"], "s": "ez" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["khhhdsaevp","dnod"], "s": "kd" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["wefmc","tmunsmg"], "s": "jt" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["wo","jhaabx"], "s": "wx" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["kltil","mubemf"], "s": "km" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["kxkvhylsh","gyshntskq"], "s": "kg" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["wseopbedw","iihrgujev"], "s": "wq" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["kzxp","fy"], "s": "kf" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["wvdx","jerzn"], "s": "cj" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["y","qppnclhhbd"], "s": "mq" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["yegnsnddq","kusrkz"], "s": "bk" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["couqsa","sncuru","jhgxpxipg"], "s": "csa" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["csm","hexhvojfj","l"], "s": "chh" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["lbor","zx"], "s": "lz" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["losinu","ptsjoihvj"], "s": "lp" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["maczdfm","ywj"], "s": "my" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["d","geviina","tyljs"], "s": "dvt" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["mammhva","igyzbwpj"], "s": "mi" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ecmlkida","vrjwdpe","vocff"], "s": "hvv" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["emqlklvrw","das","bzuq"], "s": "edm" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["hawikjbs","qi","ssvgttkzd"], "s": "rornirdphvargyce" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["hd","f","meivn"], "s": "hpm" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["mqhptbyzzv","mfubjraiqz"], "s": "mm" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["mvftnzftb","ga"], "s": "mg" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ncgutvi","rsh"], "s": "nr" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ntf","txayzc"], "s": "nt" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["nubibbe","wzwdrjcck"], "s": "nw" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["hphfek","kezkkq","oh"], "s": "hmo" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["odtclcvcj","ufhrhk"], "s": "ou" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ojcn","naujlz"], "s": "on" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["iho","ignyipken","q"], "s": "wiq" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["olynt","xf"], "s": "ox" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["ir","wzhmxee","kjfwful"], "s": "iwn" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["ixo","wigba","raaphui"], "s": "dwr" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["opup","eme"], "s": "oe" }
assert my_solution.isAcronym(**test_input) == True
test_input = { "words": ["kmmoq","aoh","hznkpurdh"], "s": "kar" }
assert my_solution.isAcronym(**test_input) == False
test_input = { "words": ["ottxwi","akpixfvbel"], "s": "oa" }
assert my_solution.isAcronym(**test_input) == True | 1,692,498,600 |
weekly-contest-359-determine-the-minimum-sum-of-a-k-avoiding-array | https://leetcode.com/problems/determine-the-minimum-sum-of-a-k-avoiding-array | determine-the-minimum-sum-of-a-k-avoiding-array | {
"questionId": "2811",
"questionFrontendId": "2829",
"title": "Determine the Minimum Sum of a k-avoiding Array",
"titleSlug": "determine-the-minimum-sum-of-a-k-avoiding-array",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 305,
"dislikes": 8,
"categoryTitle": "Algorithms"
} | """
给你两个整数 n 和 k 。
对于一个由 不同 正整数组成的数组,如果其中不存在任何求和等于 k 的不同元素对,则称其为 k-avoiding 数组。
返回长度为 n 的 k-avoiding 数组的可能的最小总和。
示例 1:
输入:n = 5, k = 4
输出:18
解释:设若 k-avoiding 数组为 [1,2,4,5,6] ,其元素总和为 18 。
可以证明不存在总和小于 18 的 k-avoiding 数组。
示例 2:
输入:n = 2, k = 6
输出:3
解释:可以构造数组 [1,2] ,其元素总和为 3 。
可以证明不存在总和小于 3 的 k-avoiding 数组。
提示:
* 1 <= n, k <= 50
"""
class Solution:
def minimumSum(self, n: int, k: int) -> int:
| 给你两个整数 n 和 k 。
对于一个由 不同 正整数组成的数组,如果其中不存在任何求和等于 k 的不同元素对,则称其为 k-avoiding 数组。
返回长度为 n 的 k-avoiding 数组的可能的最小总和。
示例 1:
输入:n = 5, k = 4
输出:18
解释:设若 k-avoiding 数组为 [1,2,4,5,6] ,其元素总和为 18 。
可以证明不存在总和小于 18 的 k-avoiding 数组。
示例 2:
输入:n = 2, k = 6
输出:3
解释:可以构造数组 [1,2] ,其元素总和为 3 。
可以证明不存在总和小于 3 的 k-avoiding 数组。
提示:
* 1 <= n, k <= 50
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumSum(self, n: int, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 5, "k": 4 }
assert my_solution.minimumSum(**test_input) == 18
test_input = { "n": 2, "k": 6 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 1, "k": 1 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 2 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 3 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 4 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 5 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 6 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 7 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 8 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 9 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 10 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 11 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 12 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 13 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 14 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 15 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 16 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 17 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 18 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 19 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 20 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 21 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 22 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 23 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 24 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 25 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 26 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 27 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 28 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 29 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 30 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 31 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 32 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 33 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 34 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 35 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 36 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 37 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 38 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 39 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 40 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 41 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 42 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 43 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 44 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 45 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 46 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 47 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 48 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 49 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 1, "k": 50 }
assert my_solution.minimumSum(**test_input) == 1
test_input = { "n": 2, "k": 1 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 2 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 3 }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "n": 2, "k": 4 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 5 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 7 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 8 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 9 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 10 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 11 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 12 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 13 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 14 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 15 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 16 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 17 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 18 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 19 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 20 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 21 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 22 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 23 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 24 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 25 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 26 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 27 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 28 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 29 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 30 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 31 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 32 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 33 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 34 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 35 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 36 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 37 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 38 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 39 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 40 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 41 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 42 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 43 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 44 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 45 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 46 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 47 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 48 }
assert my_solution.minimumSum(**test_input) == 3
test_input = { "n": 2, "k": 49 }
assert my_solution.minimumSum(**test_input) == 3 | 1,692,498,600 |
weekly-contest-359-maximize-the-profit-as-the-salesman | https://leetcode.com/problems/maximize-the-profit-as-the-salesman | maximize-the-profit-as-the-salesman | {
"questionId": "2979",
"questionFrontendId": "2830",
"title": "Maximize the Profit as the Salesman",
"titleSlug": "maximize-the-profit-as-the-salesman",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 603,
"dislikes": 19,
"categoryTitle": "Algorithms"
} | """
给你一个整数 n 表示数轴上的房屋数量,编号从 0 到 n - 1 。
另给你一个二维整数数组 offers ,其中 offers[i] = [starti, endi, goldi] 表示第 i 个买家想要以 goldi 枚金币的价格购买从 starti 到 endi 的所有房屋。
作为一名销售,你需要有策略地选择并销售房屋使自己的收入最大化。
返回你可以赚取的金币的最大数目。
注意 同一所房屋不能卖给不同的买家,并且允许保留一些房屋不进行出售。
示例 1:
输入:n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]
输出:3
解释:
有 5 所房屋,编号从 0 到 4 ,共有 3 个购买要约。
将位于 [0,0] 范围内的房屋以 1 金币的价格出售给第 1 位买家,并将位于 [1,3] 范围内的房屋以 2 金币的价格出售给第 3 位买家。
可以证明我们最多只能获得 3 枚金币。
示例 2:
输入:n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]
输出:10
解释:有 5 所房屋,编号从 0 到 4 ,共有 3 个购买要约。
将位于 [0,2] 范围内的房屋以 10 金币的价格出售给第 2 位买家。
可以证明我们最多只能获得 10 枚金币。
提示:
* 1 <= n <= 105
* 1 <= offers.length <= 105
* offers[i].length == 3
* 0 <= starti <= endi <= n - 1
* 1 <= goldi <= 103
"""
class Solution:
def maximizeTheProfit(self, n: int, offers: List[List[int]]) -> int:
| 给你一个整数 n 表示数轴上的房屋数量,编号从 0 到 n - 1 。
另给你一个二维整数数组 offers ,其中 offers[i] = [starti, endi, goldi] 表示第 i 个买家想要以 goldi 枚金币的价格购买从 starti 到 endi 的所有房屋。
作为一名销售,你需要有策略地选择并销售房屋使自己的收入最大化。
返回你可以赚取的金币的最大数目。
注意 同一所房屋不能卖给不同的买家,并且允许保留一些房屋不进行出售。
示例 1:
输入:n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]
输出:3
解释:
有 5 所房屋,编号从 0 到 4 ,共有 3 个购买要约。
将位于 [0,0] 范围内的房屋以 1 金币的价格出售给第 1 位买家,并将位于 [1,3] 范围内的房屋以 2 金币的价格出售给第 3 位买家。
可以证明我们最多只能获得 3 枚金币。
示例 2:
输入:n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]
输出:10
解释:有 5 所房屋,编号从 0 到 4 ,共有 3 个购买要约。
将位于 [0,2] 范围内的房屋以 10 金币的价格出售给第 2 位买家。
可以证明我们最多只能获得 10 枚金币。
提示:
* 1 <= n <= 105
* 1 <= offers.length <= 105
* offers[i].length == 3
* 0 <= starti <= endi <= n - 1
* 1 <= goldi <= 103
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximizeTheProfit(self, n: int, offers: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "n": 5, "offers": [[0,0,1],[0,2,2],[1,3,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 3
test_input = { "n": 5, "offers": [[0,0,1],[0,2,10],[1,3,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 4, "offers": [[1,3,10],[1,3,3],[0,0,1],[0,0,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 17
test_input = { "n": 4, "offers": [[0,0,6],[1,2,8],[0,3,7],[2,2,5],[0,1,5],[2,3,2],[0,2,8],[2,3,10],[0,3,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 16
test_input = { "n": 15, "offers": [[5,5,10],[2,6,6],[8,11,5],[7,11,9],[2,4,1],[3,8,5],[0,6,9],[0,10,5],[5,10,8],[4,5,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 20
test_input = { "n": 10, "offers": [[1,6,1],[0,1,10],[3,6,2],[0,5,10],[0,0,3],[0,0,4],[1,1,4],[0,6,7],[4,4,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 12
test_input = { "n": 11, "offers": [[7,8,6],[6,6,4],[4,6,9],[6,7,4],[5,5,8],[1,5,9],[7,7,8],[1,2,5],[0,2,9],[1,3,8],[0,2,7],[2,2,8]] }
assert my_solution.maximizeTheProfit(**test_input) == 29
test_input = { "n": 3, "offers": [[0,0,6],[0,1,8],[1,2,1],[0,1,4],[0,1,2],[0,0,7],[0,0,6],[0,0,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 8
test_input = { "n": 4, "offers": [[0,1,9],[1,1,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 9
test_input = { "n": 11, "offers": [[1,10,6],[1,10,5],[0,2,7],[0,0,8],[8,10,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 15
test_input = { "n": 3, "offers": [[0,1,8],[1,1,6],[2,2,7],[0,2,6],[0,2,2],[0,0,6],[0,0,9],[0,1,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 22
test_input = { "n": 6, "offers": [[0,2,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 4
test_input = { "n": 10, "offers": [[5,9,3],[1,5,8],[0,0,6],[5,8,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 16
test_input = { "n": 5, "offers": [[1,1,3],[1,1,3],[0,0,8],[1,3,8],[0,2,1],[3,3,9],[0,0,7],[0,2,3],[0,0,5],[0,3,10],[1,3,10],[4,4,6],[0,1,1],[2,4,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 26
test_input = { "n": 13, "offers": [[2,2,5],[1,8,10],[2,3,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 2, "offers": [[1,1,8],[1,1,8],[1,1,10],[1,1,7],[0,0,7],[0,0,3],[0,1,8],[0,0,4],[0,0,4],[0,0,7],[0,0,10],[0,1,4],[1,1,1],[0,1,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 20
test_input = { "n": 3, "offers": [[0,1,7],[1,1,3],[0,0,2],[1,1,6],[0,0,10],[1,1,7],[0,2,3],[0,1,2],[0,0,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 17
test_input = { "n": 5, "offers": [[0,0,5],[1,3,9],[0,2,2],[1,1,6],[1,2,10],[0,2,10],[1,1,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 15
test_input = { "n": 10, "offers": [[0,1,9],[5,6,10],[1,3,8],[1,9,7],[7,8,1],[2,7,1],[0,8,7],[1,6,6],[1,4,4],[0,5,4],[0,0,3],[0,8,6]] }
assert my_solution.maximizeTheProfit(**test_input) == 22
test_input = { "n": 4, "offers": [[0,0,1],[0,0,10],[0,2,1],[0,0,6],[0,3,10],[0,1,5],[1,2,10],[0,0,2],[3,3,1],[0,0,9],[0,1,2],[0,0,4],[1,3,5],[1,1,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 21
test_input = { "n": 9, "offers": [[0,3,10],[5,6,5],[1,5,2],[1,8,9],[1,1,9],[1,7,1],[3,7,9],[2,3,2],[4,6,1],[4,5,7],[2,2,2],[6,8,10],[1,3,10],[1,4,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 28
test_input = { "n": 10, "offers": [[0,2,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 2
test_input = { "n": 10, "offers": [[2,7,4],[2,4,9],[1,8,7],[0,4,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 9
test_input = { "n": 6, "offers": [[0,1,4],[1,2,4],[0,1,10],[1,2,4],[2,2,5],[1,1,8],[2,3,2],[4,4,4],[0,0,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 20
test_input = { "n": 1, "offers": [[0,0,8],[0,0,3],[0,0,8],[0,0,8],[0,0,5],[0,0,9],[0,0,6],[0,0,1],[0,0,8],[0,0,1],[0,0,5],[0,0,9],[0,0,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 9
test_input = { "n": 15, "offers": [[8,10,5],[4,12,6],[6,11,7],[8,11,3],[7,13,1],[7,7,8],[8,10,5],[0,11,3],[1,1,9],[2,11,6],[3,11,8]] }
assert my_solution.maximizeTheProfit(**test_input) == 22
test_input = { "n": 10, "offers": [[5,6,9],[0,2,9]] }
assert my_solution.maximizeTheProfit(**test_input) == 18
test_input = { "n": 11, "offers": [[7,9,5],[0,0,8],[6,6,3],[4,9,1],[3,7,5],[0,4,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 16
test_input = { "n": 7, "offers": [[0,2,9],[2,4,8],[0,3,6],[4,4,10],[2,2,2],[1,1,10],[0,0,8],[4,4,9],[4,4,4],[3,3,5],[2,5,2],[0,3,6],[3,4,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 35
test_input = { "n": 9, "offers": [[3,8,1],[0,6,7],[0,3,6],[1,6,2],[2,3,10],[3,3,2],[1,2,2],[1,3,9],[0,0,7],[1,2,9],[5,5,4],[5,6,6],[1,5,5],[0,1,2],[0,6,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 24
test_input = { "n": 8, "offers": [[0,0,7],[0,1,8],[1,1,1],[2,2,7],[2,3,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 15
test_input = { "n": 8, "offers": [[6,6,5],[0,1,7],[1,7,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 12
test_input = { "n": 13, "offers": [[0,9,5],[6,8,7],[0,0,3],[4,4,2],[1,9,7],[9,12,9],[1,2,9],[1,1,10],[3,3,3],[0,3,3],[4,8,5],[0,0,9],[7,10,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 40
test_input = { "n": 11, "offers": [[2,5,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 1
test_input = { "n": 3, "offers": [[0,0,9],[0,2,6],[1,1,1],[1,2,10],[0,0,10],[0,0,4],[0,2,7],[0,0,1],[0,0,9],[2,2,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 20
test_input = { "n": 5, "offers": [[1,1,3],[1,2,1],[0,2,3],[1,1,10],[3,3,3],[2,4,3],[0,3,5],[4,4,2],[2,3,10],[3,3,8],[3,3,9],[0,2,8],[0,2,2],[1,1,3],[0,0,8]] }
assert my_solution.maximizeTheProfit(**test_input) == 30
test_input = { "n": 13, "offers": [[6,9,3],[6,9,6],[5,12,10],[11,12,4],[4,4,2],[0,7,8],[2,6,6],[6,6,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 12
test_input = { "n": 3, "offers": [[0,2,9],[1,1,8],[0,1,1],[2,2,4],[2,2,1],[0,0,4],[1,1,9],[0,0,6],[0,1,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 19
test_input = { "n": 3, "offers": [[1,2,8],[0,0,1],[0,1,1],[0,0,3],[1,2,2],[0,0,7],[0,0,10],[1,1,6]] }
assert my_solution.maximizeTheProfit(**test_input) == 18
test_input = { "n": 2, "offers": [[0,0,3],[1,1,10],[0,1,6]] }
assert my_solution.maximizeTheProfit(**test_input) == 13
test_input = { "n": 3, "offers": [[0,0,9],[1,1,1],[0,2,7],[1,1,7],[1,2,6],[0,0,8],[0,2,3],[1,2,10],[2,2,3],[2,2,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 21
test_input = { "n": 5, "offers": [[2,3,2],[0,1,7],[0,1,1],[0,0,9],[2,4,1],[3,4,5],[1,3,10],[0,0,8]] }
assert my_solution.maximizeTheProfit(**test_input) == 19
test_input = { "n": 15, "offers": [[4,6,9],[4,10,9],[3,5,4],[0,2,6],[3,13,7],[1,11,6],[1,8,4],[4,12,4],[3,8,8],[13,13,7],[4,12,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 22
test_input = { "n": 8, "offers": [[1,5,9],[0,4,9],[0,0,3],[1,2,9],[0,0,10],[4,7,9],[7,7,2],[0,2,6],[1,1,5],[1,4,3],[2,4,8],[0,1,1],[2,3,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 28
test_input = { "n": 4, "offers": [[0,2,7],[2,3,9],[2,3,2],[1,2,1],[1,2,9],[0,3,7],[0,2,9],[1,2,8],[0,3,10],[0,3,8],[0,0,5],[2,2,6]] }
assert my_solution.maximizeTheProfit(**test_input) == 14
test_input = { "n": 12, "offers": [[0,0,4],[5,8,2],[2,2,10],[3,5,7],[1,2,1],[5,7,8],[8,11,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 25
test_input = { "n": 2, "offers": [[0,0,7],[0,1,3],[0,0,8]] }
assert my_solution.maximizeTheProfit(**test_input) == 8
test_input = { "n": 4, "offers": [[2,3,8],[0,1,1],[3,3,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 9
test_input = { "n": 14, "offers": [[2,12,4],[7,11,4],[4,4,5],[0,1,6],[3,4,1],[4,11,9],[10,12,7],[7,12,1],[11,11,1],[0,0,5],[12,12,8],[6,7,6]] }
assert my_solution.maximizeTheProfit(**test_input) == 26
test_input = { "n": 10, "offers": [[1,4,6],[7,9,9],[1,4,5],[8,8,2],[4,7,1],[6,8,8],[2,3,1],[0,1,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 15
test_input = { "n": 7, "offers": [[2,5,5],[1,2,9],[1,3,7],[2,4,3],[0,0,6],[0,0,1],[4,4,9],[1,5,7],[2,2,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 25
test_input = { "n": 11, "offers": [[0,4,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 3, "offers": [[0,1,10],[1,2,2],[0,2,6],[0,0,1],[0,0,3],[0,1,8],[0,0,2],[2,2,8],[0,0,3],[2,2,3],[1,2,6],[0,0,4],[1,2,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 18
test_input = { "n": 14, "offers": [[11,11,4],[1,11,10],[11,12,2],[7,8,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 2, "offers": [[0,0,1],[0,0,1],[1,1,9],[0,0,1],[1,1,2],[0,1,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 6, "offers": [[0,5,6],[1,2,10],[0,2,4],[2,4,5],[4,4,6],[2,2,2],[0,0,7],[2,5,9],[2,2,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 23
test_input = { "n": 6, "offers": [[0,0,7],[2,5,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 12
test_input = { "n": 10, "offers": [[2,3,2],[0,1,6],[0,0,2],[1,1,5],[3,3,8],[2,8,7],[1,7,8],[0,1,4],[7,7,8],[1,3,7],[5,5,10],[2,6,6],[0,0,4],[5,7,4],[1,9,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 35
test_input = { "n": 10, "offers": [[0,2,4],[1,4,7],[0,1,10],[0,5,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 12, "offers": [[0,5,6],[4,10,9],[7,11,10],[10,11,1],[6,10,1],[2,2,6]] }
assert my_solution.maximizeTheProfit(**test_input) == 16
test_input = { "n": 11, "offers": [[3,7,8],[2,7,10],[3,9,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 4, "offers": [[0,0,3],[0,2,6],[0,0,1],[1,1,2],[0,2,8],[1,1,3],[1,3,8],[1,1,10],[1,2,7],[1,1,8],[0,0,9]] }
assert my_solution.maximizeTheProfit(**test_input) == 19
test_input = { "n": 1, "offers": [[0,0,9]] }
assert my_solution.maximizeTheProfit(**test_input) == 9
test_input = { "n": 3, "offers": [[0,1,5],[0,0,5],[0,0,6],[0,1,6],[0,2,10],[1,2,6],[0,0,9],[1,2,9]] }
assert my_solution.maximizeTheProfit(**test_input) == 18
test_input = { "n": 4, "offers": [[0,0,2],[2,3,9],[0,1,8],[0,0,9],[0,0,1],[3,3,9],[1,2,1],[1,3,5],[0,1,4],[0,1,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 19
test_input = { "n": 3, "offers": [[0,0,7],[2,2,1],[1,1,3],[0,0,3],[1,1,7],[0,1,5],[0,2,3],[1,1,5],[0,1,10],[1,1,5],[1,1,6],[0,1,3],[0,0,8],[1,2,7],[1,1,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 16
test_input = { "n": 14, "offers": [[5,7,2],[1,5,3],[11,13,2],[12,12,5],[4,5,6],[5,10,2],[4,10,8],[1,1,4],[4,4,2],[3,7,9],[5,10,1],[0,3,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 18
test_input = { "n": 11, "offers": [[1,1,5],[4,4,9],[0,0,1],[1,3,3],[3,7,4],[3,9,6],[7,10,2],[3,7,5],[4,4,8],[7,8,10],[1,3,7],[1,4,5],[0,0,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 36
test_input = { "n": 13, "offers": [[4,9,9],[1,9,8],[1,9,8],[0,0,8],[8,11,3],[2,3,6],[9,9,10],[5,12,1],[4,6,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 28
test_input = { "n": 5, "offers": [[2,2,7],[0,2,10],[2,3,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 10, "offers": [[0,4,6],[1,1,1],[0,5,1],[1,6,3],[8,9,1],[2,3,7],[2,3,10],[1,2,1],[0,0,8],[3,5,5],[0,0,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 22
test_input = { "n": 4, "offers": [[0,1,1],[0,0,9],[1,1,8],[3,3,1],[1,1,5],[0,0,9],[0,1,9],[0,0,7],[2,2,2],[2,3,5],[1,1,10],[1,2,8]] }
assert my_solution.maximizeTheProfit(**test_input) == 24
test_input = { "n": 7, "offers": [[0,1,9],[0,1,4],[0,0,3],[0,0,1],[1,6,5],[4,6,9],[4,5,7],[0,0,3],[1,5,9],[0,2,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 18
test_input = { "n": 12, "offers": [[8,8,6],[8,8,6],[1,10,7],[0,0,3],[9,10,7],[1,7,2],[1,1,1],[2,3,6],[0,11,1],[1,8,5],[1,5,7],[1,2,4],[9,9,5],[0,3,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 23
test_input = { "n": 15, "offers": [[5,6,3],[2,2,7],[0,0,5],[1,7,10],[11,14,5],[13,14,1],[2,12,1],[0,4,5],[0,6,2],[6,9,10],[3,5,2],[0,1,1],[1,14,1],[1,6,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 29
test_input = { "n": 7, "offers": [[1,1,5],[1,1,4],[0,0,9],[1,1,6],[0,6,4],[2,6,3],[2,5,9],[0,6,3],[0,2,1],[1,1,6],[4,5,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 24
test_input = { "n": 1, "offers": [[0,0,5],[0,0,3],[0,0,4],[0,0,8],[0,0,10],[0,0,6],[0,0,7],[0,0,7],[0,0,7],[0,0,3],[0,0,4],[0,0,5]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 7, "offers": [[2,2,3],[2,6,4],[4,6,5],[0,0,4],[1,1,4],[2,3,1],[2,4,3],[0,2,8],[1,3,10],[1,3,2],[1,6,7],[0,6,9],[2,2,2],[1,1,9],[4,4,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 21
test_input = { "n": 12, "offers": [[0,0,7],[0,2,3],[0,7,2],[2,3,1],[2,11,6],[2,10,2],[1,3,6],[4,7,9],[7,9,3],[4,6,1],[5,6,8],[0,2,4],[0,0,3],[5,5,9],[2,5,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 25
test_input = { "n": 9, "offers": [[1,8,4],[5,6,5],[0,2,6],[4,5,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 11
test_input = { "n": 8, "offers": [[0,4,6],[2,3,6],[2,5,9],[2,6,7],[6,6,5],[4,4,4],[1,1,5],[2,5,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 20
test_input = { "n": 13, "offers": [[0,6,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 6, "offers": [[0,1,2],[0,0,9],[3,3,10],[0,3,7],[0,0,2],[0,0,3],[2,2,2],[2,3,2],[5,5,6],[0,1,2],[0,5,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 27
test_input = { "n": 14, "offers": [[3,12,7],[1,3,2],[4,11,3],[0,1,7],[1,5,2],[1,1,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 14
test_input = { "n": 14, "offers": [[0,0,3],[0,1,3],[1,11,3],[6,7,6],[7,7,5],[1,2,8],[7,10,9]] }
assert my_solution.maximizeTheProfit(**test_input) == 20
test_input = { "n": 13, "offers": [[0,12,7],[2,2,4],[2,2,8],[3,3,2],[1,11,5],[1,7,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 1, "offers": [[0,0,2],[0,0,8],[0,0,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 8
test_input = { "n": 1, "offers": [[0,0,1],[0,0,4],[0,0,7],[0,0,2],[0,0,5],[0,0,1],[0,0,4],[0,0,2],[0,0,6],[0,0,6],[0,0,3],[0,0,3]] }
assert my_solution.maximizeTheProfit(**test_input) == 7
test_input = { "n": 1, "offers": [[0,0,6],[0,0,6],[0,0,3],[0,0,6],[0,0,6],[0,0,10],[0,0,1],[0,0,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 9, "offers": [[4,6,7],[1,3,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 17
test_input = { "n": 13, "offers": [[2,6,3],[1,12,6],[2,11,3],[7,7,2],[5,12,4],[0,1,2],[0,1,8],[1,1,3],[6,6,4],[8,9,7],[8,8,2],[2,2,2],[0,0,9],[9,11,7],[8,9,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 29
test_input = { "n": 8, "offers": [[0,1,8],[0,0,6],[5,5,9]] }
assert my_solution.maximizeTheProfit(**test_input) == 17
test_input = { "n": 1, "offers": [[0,0,10],[0,0,3],[0,0,8],[0,0,9],[0,0,1],[0,0,8],[0,0,2],[0,0,7],[0,0,10],[0,0,8],[0,0,5],[0,0,3],[0,0,2],[0,0,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 10
test_input = { "n": 9, "offers": [[0,2,6],[1,3,5],[1,1,5],[2,3,10],[4,8,4],[5,8,5],[6,6,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 25
test_input = { "n": 6, "offers": [[0,0,7]] }
assert my_solution.maximizeTheProfit(**test_input) == 7
test_input = { "n": 8, "offers": [[1,1,5],[1,2,9],[1,2,6],[0,3,6],[1,1,10],[3,4,1],[3,5,3],[1,5,8],[0,2,6],[5,7,9]] }
assert my_solution.maximizeTheProfit(**test_input) == 20
test_input = { "n": 14, "offers": [[3,4,4],[6,8,1],[0,4,1]] }
assert my_solution.maximizeTheProfit(**test_input) == 5
test_input = { "n": 11, "offers": [[4,4,2],[1,2,7],[2,8,10],[1,1,3],[8,10,4],[1,2,1],[4,6,10]] }
assert my_solution.maximizeTheProfit(**test_input) == 21
test_input = { "n": 11, "offers": [[1,8,1],[1,5,5],[0,1,3],[10,10,10],[1,1,8],[1,2,1],[2,3,10],[2,10,10],[2,2,9],[0,9,4]] }
assert my_solution.maximizeTheProfit(**test_input) == 28
test_input = { "n": 6, "offers": [[2,2,6],[0,1,2],[2,2,2]] }
assert my_solution.maximizeTheProfit(**test_input) == 8 | 1,692,498,600 |
weekly-contest-359-find-the-longest-equal-subarray | https://leetcode.com/problems/find-the-longest-equal-subarray | find-the-longest-equal-subarray | {
"questionId": "2832",
"questionFrontendId": "2831",
"title": "Find the Longest Equal Subarray",
"titleSlug": "find-the-longest-equal-subarray",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 579,
"dislikes": 14,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。
如果子数组中所有元素都相等,则认为子数组是一个 等值子数组 。注意,空数组是 等值子数组 。
从 nums 中删除最多 k 个元素后,返回可能的最长等值子数组的长度。
子数组 是数组中一个连续且可能为空的元素序列。
示例 1:
输入:nums = [1,3,2,3,1,3], k = 3
输出:3
解释:最优的方案是删除下标 2 和下标 4 的元素。
删除后,nums 等于 [1, 3, 3, 3] 。
最长等值子数组从 i = 1 开始到 j = 3 结束,长度等于 3 。
可以证明无法创建更长的等值子数组。
示例 2:
输入:nums = [1,1,2,2,1,1], k = 2
输出:4
解释:最优的方案是删除下标 2 和下标 3 的元素。
删除后,nums 等于 [1, 1, 1, 1] 。
数组自身就是等值子数组,长度等于 4 。
可以证明无法创建更长的等值子数组。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= nums.length
* 0 <= k <= nums.length
"""
class Solution:
def longestEqualSubarray(self, nums: List[int], k: int) -> int:
| 给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。
如果子数组中所有元素都相等,则认为子数组是一个 等值子数组 。注意,空数组是 等值子数组 。
从 nums 中删除最多 k 个元素后,返回可能的最长等值子数组的长度。
子数组 是数组中一个连续且可能为空的元素序列。
示例 1:
输入:nums = [1,3,2,3,1,3], k = 3
输出:3
解释:最优的方案是删除下标 2 和下标 4 的元素。
删除后,nums 等于 [1, 3, 3, 3] 。
最长等值子数组从 i = 1 开始到 j = 3 结束,长度等于 3 。
可以证明无法创建更长的等值子数组。
示例 2:
输入:nums = [1,1,2,2,1,1], k = 2
输出:4
解释:最优的方案是删除下标 2 和下标 3 的元素。
删除后,nums 等于 [1, 1, 1, 1] 。
数组自身就是等值子数组,长度等于 4 。
可以证明无法创建更长的等值子数组。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= nums.length
* 0 <= k <= nums.length
请完成下面的代码来解决上述问题:
```python
class Solution:
def longestEqualSubarray(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,2,3,1,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [1,1,2,2,1,1], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 4
test_input = { "nums": [1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [1], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [2,1], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [2,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [1,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [2,2], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,3,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,2,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,1,1], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,2,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [1,2,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [2,3,1], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [2,2,1], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,3,2], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [2,3,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,2,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,2,3], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,2,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [1,2,1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [3,3,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [1,2,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,1,3], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,2,3], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [1,2,2], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,2,4,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,4,2,1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [1,3,4,2], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [2,4,2,2], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [4,2,2,3], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [4,1,3,2], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [1,1,2,1], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [2,4,1,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [1,1,1,2], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [4,2,1,4], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,1,4,1], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [2,1,4,1], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,4,3,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,2,2,4], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,2,2,4], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [2,3,3,1], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,4,1,4], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,4,1,4], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,2,3,1], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,1,1,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,3,2,5,1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [5,3,3,1,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [4,4,2,2,4], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [5,4,2,3,3], "k": 5 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [4,4,4,3,4], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 4
test_input = { "nums": [1,5,5,5,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [2,1,4,5,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [5,1,5,2,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [5,3,2,3,4], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,2,1,4,1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [3,2,2,5,3], "k": 5 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,2,4,4,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [4,3,3,4,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [1,5,4,3,4], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [4,4,2,5,3], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,3,5,2,3], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [1,1,1,1,3], "k": 5 }
assert my_solution.longestEqualSubarray(**test_input) == 4
test_input = { "nums": [1,3,2,5,1], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 1
test_input = { "nums": [3,3,1,4,5], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,2,5,5,4], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,1,5,3,1,1], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [6,4,1,5,5,3], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,2,2,1,2,4], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 4
test_input = { "nums": [1,1,2,2,6,2], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [1,2,3,6,6,2], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,4,5,1,4,1], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,5,6,4,6,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [5,4,2,4,1,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,1,1,3,1,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [2,4,6,6,6,4], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [3,6,2,5,4,5], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,1,5,6,1,4], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [4,6,6,6,2,4], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [2,5,5,5,6,4], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [2,3,3,2,3,3], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 4
test_input = { "nums": [5,2,5,3,3,2], "k": 6 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,5,3,2,3,6], "k": 6 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [4,3,2,5,4,2], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,2,4,2,1,3], "k": 3 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,1,3,4,1,6], "k": 6 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,3,7,7,3,2,2], "k": 7 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [5,6,7,7,4,4,2], "k": 5 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [7,2,4,1,3,3,4], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [5,3,1,7,5,5,7], "k": 6 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [4,2,6,2,3,4,6], "k": 6 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,7,6,7,3,7,4], "k": 5 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [3,2,5,1,4,3,4], "k": 4 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [2,3,3,7,2,5,1], "k": 2 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [4,1,6,7,5,3,5], "k": 1 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [3,1,4,1,5,5,6], "k": 6 }
assert my_solution.longestEqualSubarray(**test_input) == 2
test_input = { "nums": [1,5,5,7,7,7,4], "k": 0 }
assert my_solution.longestEqualSubarray(**test_input) == 3
test_input = { "nums": [5,7,4,4,1,6,7], "k": 5 }
assert my_solution.longestEqualSubarray(**test_input) == 2 | 1,692,498,600 |
biweekly-contest-111-count-pairs-whose-sum-is-less-than-target | https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target | count-pairs-whose-sum-is-less-than-target | {
"questionId": "2917",
"questionFrontendId": "2824",
"title": "Count Pairs Whose Sum is Less than Target",
"titleSlug": "count-pairs-whose-sum-is-less-than-target",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 358,
"dislikes": 23,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始长度为 n 的整数数组 nums 和一个整数 target ,请你返回满足 0 <= i < j < n 且 nums[i] + nums[j] < target 的下标对 (i, j) 的数目。
示例 1:
输入:nums = [-1,1,2,3,1], target = 2
输出:3
解释:总共有 3 个下标对满足题目描述:
- (0, 1) ,0 < 1 且 nums[0] + nums[1] = 0 < target
- (0, 2) ,0 < 2 且 nums[0] + nums[2] = 1 < target
- (0, 4) ,0 < 4 且 nums[0] + nums[4] = 0 < target
注意 (0, 3) 不计入答案因为 nums[0] + nums[3] 不是严格小于 target 。
示例 2:
输入:nums = [-6,2,5,-2,-7,-1,3], target = -2
输出:10
解释:总共有 10 个下标对满足题目描述:
- (0, 1) ,0 < 1 且 nums[0] + nums[1] = -4 < target
- (0, 3) ,0 < 3 且 nums[0] + nums[3] = -8 < target
- (0, 4) ,0 < 4 且 nums[0] + nums[4] = -13 < target
- (0, 5) ,0 < 5 且 nums[0] + nums[5] = -7 < target
- (0, 6) ,0 < 6 且 nums[0] + nums[6] = -3 < target
- (1, 4) ,1 < 4 且 nums[1] + nums[4] = -5 < target
- (3, 4) ,3 < 4 且 nums[3] + nums[4] = -9 < target
- (3, 5) ,3 < 5 且 nums[3] + nums[5] = -3 < target
- (4, 5) ,4 < 5 且 nums[4] + nums[5] = -8 < target
- (4, 6) ,4 < 6 且 nums[4] + nums[6] = -4 < target
提示:
* 1 <= nums.length == n <= 50
* -50 <= nums[i], target <= 50
"""
class Solution:
def countPairs(self, nums: List[int], target: int) -> int:
| 给你一个下标从 0 开始长度为 n 的整数数组 nums 和一个整数 target ,请你返回满足 0 <= i < j < n 且 nums[i] + nums[j] < target 的下标对 (i, j) 的数目。
示例 1:
输入:nums = [-1,1,2,3,1], target = 2
输出:3
解释:总共有 3 个下标对满足题目描述:
- (0, 1) ,0 < 1 且 nums[0] + nums[1] = 0 < target
- (0, 2) ,0 < 2 且 nums[0] + nums[2] = 1 < target
- (0, 4) ,0 < 4 且 nums[0] + nums[4] = 0 < target
注意 (0, 3) 不计入答案因为 nums[0] + nums[3] 不是严格小于 target 。
示例 2:
输入:nums = [-6,2,5,-2,-7,-1,3], target = -2
输出:10
解释:总共有 10 个下标对满足题目描述:
- (0, 1) ,0 < 1 且 nums[0] + nums[1] = -4 < target
- (0, 3) ,0 < 3 且 nums[0] + nums[3] = -8 < target
- (0, 4) ,0 < 4 且 nums[0] + nums[4] = -13 < target
- (0, 5) ,0 < 5 且 nums[0] + nums[5] = -7 < target
- (0, 6) ,0 < 6 且 nums[0] + nums[6] = -3 < target
- (1, 4) ,1 < 4 且 nums[1] + nums[4] = -5 < target
- (3, 4) ,3 < 4 且 nums[3] + nums[4] = -9 < target
- (3, 5) ,3 < 5 且 nums[3] + nums[5] = -3 < target
- (4, 5) ,4 < 5 且 nums[4] + nums[5] = -8 < target
- (4, 6) ,4 < 6 且 nums[4] + nums[6] = -4 < target
提示:
* 1 <= nums.length == n <= 50
* -50 <= nums[i], target <= 50
请完成下面的代码来解决上述问题:
```python
class Solution:
def countPairs(self, nums: List[int], target: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [-1,1,2,3,1], "target": 2 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-6,2,5,-2,-7,-1,3], "target": -2 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "nums": [9,-5,-5,5,-5,-4,-6,6,-6], "target": 3 }
assert my_solution.countPairs(**test_input) == 27
test_input = { "nums": [-8,-5,5,-4,10], "target": 2 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "nums": [-5,0,-7,-1,9,8,-9,9], "target": -14 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [6,-1,7,4,2,3], "target": 8 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "nums": [2,8,2,8,7], "target": 10 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-6,1,1,-1,-10,-7,1,-5,-4,0], "target": -15 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [10,-2,-1,7,8,5,3,-4,-9], "target": -10 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [3,8,-3,4,10,-6], "target": 1 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "nums": [-4,-6,-7,8], "target": -13 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "nums": [-4,0,10,8,-2], "target": 0 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-8,-5,-3,1,-7], "target": -6 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "nums": [4,-8,-2,5,2,-9,6,5,-4], "target": -4 }
assert my_solution.countPairs(**test_input) == 9
test_input = { "nums": [-1,-5,4,4,-10], "target": -6 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [-5,4,-6,-5,-10,-1,10,3], "target": 6 }
assert my_solution.countPairs(**test_input) == 24
test_input = { "nums": [-9,6,-4,10,1,8], "target": 11 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "nums": [-10,-6,-8,-9,6,6,-6,-6,-3], "target": -2 }
assert my_solution.countPairs(**test_input) == 25
test_input = { "nums": [-7,7,6,-9,-4,10,8,-8,2,2], "target": -1 }
assert my_solution.countPairs(**test_input) == 17
test_input = { "nums": [0,-1,0,-6,-9], "target": -9 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [8,-10,-9,6,-3,5,-2,-2,-7], "target": -4 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "nums": [-1,3,8,3], "target": 2 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "nums": [7,2,9,-10,-4,4,-3,0], "target": -20 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "nums": [6,4,1,-7], "target": 7 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "nums": [3,8,6,-2,6,1,7], "target": 7 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "nums": [1,3,-10,5,-8,0,-5,-9], "target": -7 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "nums": [-6,9,2,4,-9,2,4,-6,6,-9], "target": 11 }
assert my_solution.countPairs(**test_input) == 40
test_input = { "nums": [-10,-7,-5,-1,2,4,-6,6], "target": -3 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "nums": [-1,0,1,9,-2,-8,-8,7], "target": 1 }
assert my_solution.countPairs(**test_input) == 16
test_input = { "nums": [1,9,3,2,9,-5,6,0,-6,6], "target": 9 }
assert my_solution.countPairs(**test_input) == 29
test_input = { "nums": [-3,-3,-4,1,4,9], "target": 6 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "nums": [-10,3,-5,2,-10,7,9], "target": 4 }
assert my_solution.countPairs(**test_input) == 14
test_input = { "nums": [7,10,9,8,-9,1,-7,10,-4,2], "target": 4 }
assert my_solution.countPairs(**test_input) == 21
test_input = { "nums": [9,-9,0,5,4], "target": 14 }
assert my_solution.countPairs(**test_input) == 9
test_input = { "nums": [7,9,7,-10,-6,-8,-5], "target": 2 }
assert my_solution.countPairs(**test_input) == 14
test_input = { "nums": [8,-5,0,4], "target": 0 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [5,2,-1,9,-1,-1], "target": 4 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "nums": [-7,-4,3,9,10,5,-1,1,-7], "target": -4 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "nums": [0,8,9,-9,8,-2,-1,2,5], "target": -1 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "nums": [10,4,-9,8,-10,3], "target": -7 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [-6,-6,6,-4,-5,-1,10,-8,1], "target": -13 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [7,3,-4,1,-9,-8,10,4,-1], "target": -2 }
assert my_solution.countPairs(**test_input) == 13
test_input = { "nums": [4,3,-3,1,-3,-1], "target": -2 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [10,-8,-9,-7,2,-10,4,7,6,6], "target": 14 }
assert my_solution.countPairs(**test_input) == 41
test_input = { "nums": [9,-5,-4,-2,9], "target": 4 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-1,2,-3,-4,-10,-8,2], "target": -1 }
assert my_solution.countPairs(**test_input) == 16
test_input = { "nums": [-8,-9,-10,0,-5,-5], "target": -15 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-8,9,-10,2,-10,-6,-1,-8], "target": -4 }
assert my_solution.countPairs(**test_input) == 19
test_input = { "nums": [4,-7,8,7,-4,3,7,7,-2,-10], "target": 4 }
assert my_solution.countPairs(**test_input) == 25
test_input = { "nums": [6,3,4,5,-4], "target": 0 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [2,-4,5,3,7,10,9,-1,9,0], "target": 9 }
assert my_solution.countPairs(**test_input) == 23
test_input = { "nums": [-2,-5,9,-3,-8,5,-1,3,-9], "target": 1 }
assert my_solution.countPairs(**test_input) == 23
test_input = { "nums": [0,-2,-3,-1,-6,-7,3], "target": -10 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [10,4,-3,9,-8,6], "target": 14 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "nums": [-10,-6,6,-7,1,-7,9,3,1], "target": 15 }
assert my_solution.countPairs(**test_input) == 35
test_input = { "nums": [2,-3,-6,-2,5], "target": -4 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-10,-8,8,-2], "target": 0 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "nums": [-2,2,-7,-5,1,6,8], "target": 0 }
assert my_solution.countPairs(**test_input) == 9
test_input = { "nums": [4,-4,-5,-8,9], "target": -10 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [-5,-4,-6,-7,9,-10,0,4,9,-1], "target": -7 }
assert my_solution.countPairs(**test_input) == 13
test_input = { "nums": [-10,-6,6,-3,10,-6,4,-8], "target": -9 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "nums": [-8,-1,-9,1], "target": -17 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "nums": [6,-4,2,1,10,-1], "target": 1 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "nums": [9,4,-8,8,9,-4], "target": -16 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "nums": [9,-4,8,-9,-2,-2], "target": -11 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [-7,1,3,7,6,3], "target": 10 }
assert my_solution.countPairs(**test_input) == 12
test_input = { "nums": [10,10,-2,-4], "target": 8 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-3,2,6,-6,9], "target": 3 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "nums": [5,0,2,4,2,-7], "target": 2 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "nums": [4,-5,-4,-2,-9,-6,-10,-10,2,-8], "target": -19 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [-10,10,-9,-2,3,-2,-7,-1,-6,7], "target": -17 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [-9,-9,3,7,-9,-10,2,3,-4], "target": -13 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "nums": [-5,-4,-10,7], "target": 14 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "nums": [-3,4,-6,-6,1,-10,-1,-8], "target": -11 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "nums": [-7,1,-5,8,-7,-3,2,-2,-2,7], "target": -5 }
assert my_solution.countPairs(**test_input) == 14
test_input = { "nums": [8,0,-8,-8,-1,5], "target": 0 }
assert my_solution.countPairs(**test_input) == 8
test_input = { "nums": [9,7,2,4,3], "target": 7 }
assert my_solution.countPairs(**test_input) == 2
test_input = { "nums": [8,-1,-5,7,7,5,-6,2], "target": -2 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "nums": [-1,3,3,3,9], "target": 8 }
assert my_solution.countPairs(**test_input) == 6
test_input = { "nums": [-8,0,-1,-6,-9,2,3,1], "target": -9 }
assert my_solution.countPairs(**test_input) == 4
test_input = { "nums": [3,-3,-7,-6,-5,-2], "target": -2 }
assert my_solution.countPairs(**test_input) == 12
test_input = { "nums": [3,6,0,-4,-2,5], "target": -6 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "nums": [-8,9,2,5,9,-4,3], "target": 6 }
assert my_solution.countPairs(**test_input) == 12
test_input = { "nums": [0,-6,-5,-8,-4,0,7], "target": 7 }
assert my_solution.countPairs(**test_input) == 19
test_input = { "nums": [0,9,2,-4], "target": -8 }
assert my_solution.countPairs(**test_input) == 0
test_input = { "nums": [-7,9,-3,-5,-9,-3,-8,-2,1,2], "target": -8 }
assert my_solution.countPairs(**test_input) == 15
test_input = { "nums": [4,10,-7,0,-3,5,9,6,8,-4], "target": 13 }
assert my_solution.countPairs(**test_input) == 34
test_input = { "nums": [2,-6,0,5,-9,-8,6,5], "target": -3 }
assert my_solution.countPairs(**test_input) == 11
test_input = { "nums": [-4,6,-2,10,-5,-7,-8,-1], "target": -5 }
assert my_solution.countPairs(**test_input) == 13
test_input = { "nums": [10,4,8,-1,9,-5,-1,-7,-9], "target": 19 }
assert my_solution.countPairs(**test_input) == 35
test_input = { "nums": [-6,5,3,-2,0,3,-7,-7], "target": -8 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "nums": [-3,-1,7,4,-10,-6,2], "target": -4 }
assert my_solution.countPairs(**test_input) == 7
test_input = { "nums": [-7,8,3,-1,2,1,-10], "target": -7 }
assert my_solution.countPairs(**test_input) == 5
test_input = { "nums": [7,-3,-5,9,-10,-1,-3,-3,-3,1], "target": 6 }
assert my_solution.countPairs(**test_input) == 36
test_input = { "nums": [-5,-10,-7,-3,-2,-2], "target": -15 }
assert my_solution.countPairs(**test_input) == 1
test_input = { "nums": [-4,6,-9,-8,-9,-9], "target": -17 }
assert my_solution.countPairs(**test_input) == 3
test_input = { "nums": [-3,-8,-6,-4,-8,-10,-2,5,-2], "target": 1 }
assert my_solution.countPairs(**test_input) == 32
test_input = { "nums": [1,1,-6,8,2,10,-7,-9,-9], "target": -7 }
assert my_solution.countPairs(**test_input) == 10
test_input = { "nums": [-9,-6,-3,5,-4], "target": 2 }
assert my_solution.countPairs(**test_input) == 9
test_input = { "nums": [-5,10,-5,1,7,-8,8,-6,-6], "target": 2 }
assert my_solution.countPairs(**test_input) == 19 | 1,692,455,400 |
biweekly-contest-111-make-string-a-subsequence-using-cyclic-increments | https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments | make-string-a-subsequence-using-cyclic-increments | {
"questionId": "3018",
"questionFrontendId": "2825",
"title": "Make String a Subsequence Using Cyclic Increments",
"titleSlug": "make-string-a-subsequence-using-cyclic-increments",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 273,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的字符串 str1 和 str2 。
一次操作中,你选择 str1 中的若干下标。对于选中的每一个下标 i ,你将 str1[i] 循环 递增,变成下一个字符。也就是说 'a' 变成 'b' ,'b' 变成 'c' ,以此类推,'z' 变成 'a' 。
如果执行以上操作 至多一次 ,可以让 str2 成为 str1 的子序列,请你返回 true ,否则返回 false 。
注意:一个字符串的子序列指的是从原字符串中删除一些(可以一个字符也不删)字符后,剩下字符按照原本先后顺序组成的新字符串。
示例 1:
输入:str1 = "abc", str2 = "ad"
输出:true
解释:选择 str1 中的下标 2 。
将 str1[2] 循环递增,得到 'd' 。
因此,str1 变成 "abd" 且 str2 现在是一个子序列。所以返回 true 。
示例 2:
输入:str1 = "zc", str2 = "ad"
输出:true
解释:选择 str1 中的下标 0 和 1 。
将 str1[0] 循环递增得到 'a' 。
将 str1[1] 循环递增得到 'd' 。
因此,str1 变成 "ad" 且 str2 现在是一个子序列。所以返回 true 。
示例 3:
输入:str1 = "ab", str2 = "d"
输出:false
解释:这个例子中,没法在执行一次操作的前提下,将 str2 变为 str1 的子序列。
所以返回 false 。
提示:
* 1 <= str1.length <= 105
* 1 <= str2.length <= 105
* str1 和 str2 只包含小写英文字母。
"""
class Solution:
def canMakeSubsequence(self, str1: str, str2: str) -> bool:
| 给你一个下标从 0 开始的字符串 str1 和 str2 。
一次操作中,你选择 str1 中的若干下标。对于选中的每一个下标 i ,你将 str1[i] 循环 递增,变成下一个字符。也就是说 'a' 变成 'b' ,'b' 变成 'c' ,以此类推,'z' 变成 'a' 。
如果执行以上操作 至多一次 ,可以让 str2 成为 str1 的子序列,请你返回 true ,否则返回 false 。
注意:一个字符串的子序列指的是从原字符串中删除一些(可以一个字符也不删)字符后,剩下字符按照原本先后顺序组成的新字符串。
示例 1:
输入:str1 = "abc", str2 = "ad"
输出:true
解释:选择 str1 中的下标 2 。
将 str1[2] 循环递增,得到 'd' 。
因此,str1 变成 "abd" 且 str2 现在是一个子序列。所以返回 true 。
示例 2:
输入:str1 = "zc", str2 = "ad"
输出:true
解释:选择 str1 中的下标 0 和 1 。
将 str1[0] 循环递增得到 'a' 。
将 str1[1] 循环递增得到 'd' 。
因此,str1 变成 "ad" 且 str2 现在是一个子序列。所以返回 true 。
示例 3:
输入:str1 = "ab", str2 = "d"
输出:false
解释:这个例子中,没法在执行一次操作的前提下,将 str2 变为 str1 的子序列。
所以返回 false 。
提示:
* 1 <= str1.length <= 105
* 1 <= str2.length <= 105
* str1 和 str2 只包含小写英文字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def canMakeSubsequence(self, str1: str, str2: str) -> bool:
``` |
my_solution = Solution()
test_input = { "str1": "ab", "str2": "d" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "a", "str2": "d" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "abc", "str2": "ad" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "b", "str2": "v" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "c", "str2": "b" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "c", "str2": "k" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "zc", "str2": "ad" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "b", "str2": "c" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "c", "str2": "m" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "d", "str2": "h" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "f", "str2": "f" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "f", "str2": "g" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "d", "str2": "m" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "g", "str2": "g" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "h", "str2": "i" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "d", "str2": "x" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "i", "str2": "j" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "j", "str2": "j" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "j", "str2": "k" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "f", "str2": "s" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "i", "str2": "e" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "k", "str2": "k" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "l", "str2": "d" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "k", "str2": "l" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "m", "str2": "e" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "l", "str2": "l" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "m", "str2": "n" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "o", "str2": "p" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "o", "str2": "c" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "o", "str2": "n" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "p", "str2": "a" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "p", "str2": "s" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "q", "str2": "q" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "q", "str2": "j" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "s", "str2": "f" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "s", "str2": "u" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "r", "str2": "s" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "t", "str2": "h" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "t", "str2": "k" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "t", "str2": "u" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "u", "str2": "w" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "u", "str2": "u" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "w", "str2": "y" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "u", "str2": "v" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "at", "str2": "se" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "cl", "str2": "qs" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "v", "str2": "v" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "w", "str2": "w" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "cq", "str2": "xz" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "y", "str2": "y" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "ct", "str2": "xb" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "z", "str2": "a" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "fs", "str2": "rd" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "z", "str2": "z" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "gq", "str2": "kn" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "hq", "str2": "dk" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "dm", "str2": "e" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "eh", "str2": "e" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "ir", "str2": "ka" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "is", "str2": "af" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "ja", "str2": "zz" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "jq", "str2": "zi" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "ke", "str2": "qw" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "fp", "str2": "p" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "hz", "str2": "z" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "ia", "str2": "a" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "lo", "str2": "fa" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "km", "str2": "l" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "mu", "str2": "za" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "np", "str2": "q" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "nh", "str2": "pa" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "oj", "str2": "j" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "oh", "str2": "hu" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "pg", "str2": "xb" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "uq", "str2": "v" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "pl", "str2": "yi" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "ww", "str2": "x" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "xh", "str2": "x" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "yg", "str2": "y" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "px", "str2": "dh" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "rm", "str2": "au" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "se", "str2": "vs" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "yw", "str2": "w" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "st", "str2": "zf" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "dby", "str2": "z" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "sx", "str2": "fx" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "ei", "str2": "ei" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "ff", "str2": "fg" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "to", "str2": "yv" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "tv", "str2": "ei" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "va", "str2": "ow" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "jc", "str2": "jd" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "jrg", "str2": "h" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "lf", "str2": "lg" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "agb", "str2": "vgz" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "amk", "str2": "nto" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "atd", "str2": "xrr" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "pc", "str2": "qc" }
assert my_solution.canMakeSubsequence(**test_input) == True
test_input = { "str1": "ayt", "str2": "nov" }
assert my_solution.canMakeSubsequence(**test_input) == False
test_input = { "str1": "qv", "str2": "rv" }
assert my_solution.canMakeSubsequence(**test_input) == True | 1,692,455,400 |
biweekly-contest-111-sorting-three-groups | https://leetcode.com/problems/sorting-three-groups | sorting-three-groups | {
"questionId": "2904",
"questionFrontendId": "2826",
"title": "Sorting Three Groups",
"titleSlug": "sorting-three-groups",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 347,
"dislikes": 72,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始长度为 n 的整数数组 nums 。
从 0 到 n - 1 的数字被分为编号从 1 到 3 的三个组,数字 i 属于组 nums[i] 。注意,有的组可能是 空的 。
你可以执行以下操作任意次:
* 选择数字 x 并改变它的组。更正式的,你可以将 nums[x] 改为数字 1 到 3 中的任意一个。
你将按照以下过程构建一个新的数组 res :
1. 将每个组中的数字分别排序。
2. 将组 1 ,2 和 3 中的元素 依次 连接以得到 res 。
如果得到的 res 是 非递减顺序的,那么我们称数组 nums 是 美丽数组 。
请你返回将 nums 变为 美丽数组 需要的最少步数。
示例 1:
输入:nums = [2,1,3,2,1]
输出:3
解释:以下三步操作是最优方案:
1. 将 nums[0] 变为 1 。
2. 将 nums[2] 变为 1 。
3. 将 nums[3] 变为 1 。
执行以上操作后,将每组中的数字排序,组 1 为 [0,1,2,3,4] ,组 2 和组 3 都为空。所以 res 等于 [0,1,2,3,4] ,它是非递减顺序的。
三步操作是最少需要的步数。
示例 2:
输入:nums = [1,3,2,1,3,3]
输出:2
解释:以下两步操作是最优方案:
1. 将 nums[1] 变为 1 。
2. 将 nums[2] 变为 1 。
执行以上操作后,将每组中的数字排序,组 1 为 [0,1,2,3] ,组 2 为空,组 3 为 [4,5] 。所以 res 等于 [0,1,2,3,4,5] ,它是非递减顺序的。
两步操作是最少需要的步数。
示例 3:
输入:nums = [2,2,2,2,3,3]
输出:0
解释:不需要执行任何操作。
组 1 为空,组 2 为 [0,1,2,3] ,组 3 为 [4,5] 。所以 res 等于 [0,1,2,3,4,5] ,它是非递减顺序的。
提示:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 3
"""
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始长度为 n 的整数数组 nums 。
从 0 到 n - 1 的数字被分为编号从 1 到 3 的三个组,数字 i 属于组 nums[i] 。注意,有的组可能是 空的 。
你可以执行以下操作任意次:
* 选择数字 x 并改变它的组。更正式的,你可以将 nums[x] 改为数字 1 到 3 中的任意一个。
你将按照以下过程构建一个新的数组 res :
1. 将每个组中的数字分别排序。
2. 将组 1 ,2 和 3 中的元素 依次 连接以得到 res 。
如果得到的 res 是 非递减顺序的,那么我们称数组 nums 是 美丽数组 。
请你返回将 nums 变为 美丽数组 需要的最少步数。
示例 1:
输入:nums = [2,1,3,2,1]
输出:3
解释:以下三步操作是最优方案:
1. 将 nums[0] 变为 1 。
2. 将 nums[2] 变为 1 。
3. 将 nums[3] 变为 1 。
执行以上操作后,将每组中的数字排序,组 1 为 [0,1,2,3,4] ,组 2 和组 3 都为空。所以 res 等于 [0,1,2,3,4] ,它是非递减顺序的。
三步操作是最少需要的步数。
示例 2:
输入:nums = [1,3,2,1,3,3]
输出:2
解释:以下两步操作是最优方案:
1. 将 nums[1] 变为 1 。
2. 将 nums[2] 变为 1 。
执行以上操作后,将每组中的数字排序,组 1 为 [0,1,2,3] ,组 2 为空,组 3 为 [4,5] 。所以 res 等于 [0,1,2,3,4,5] ,它是非递减顺序的。
两步操作是最少需要的步数。
示例 3:
输入:nums = [2,2,2,2,3,3]
输出:0
解释:不需要执行任何操作。
组 1 为空,组 2 为 [0,1,2,3] ,组 3 为 [4,5] 。所以 res 等于 [0,1,2,3,4,5] ,它是非递减顺序的。
提示:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 3
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,1,3,2,1] }
assert my_solution.minimumOperations(**test_input) == 3
test_input = { "nums": [1,3,2,1,3,3] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [2,2,2,2,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [1] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [1,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [1,1,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,2,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,2,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,1,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,2,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,2,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [1,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,3,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,3,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [3,3,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,1,2,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,2,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,2,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,2,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,2,2,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,2,2,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,3,2,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,3,2,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,3,2,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,1,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,1,3,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [3,1,3,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,2,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,2,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,2,3,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,3,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,3,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,3,3,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,1,1,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,2,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,2,1,3] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,3,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,3,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,3,1,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,1,2,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,2,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,2,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,2,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,2,2,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,2,2,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,3,2,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,3,2,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,3,2,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,1,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,3,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,3,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,2,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,2,3,3] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,3,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,3,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [3,3,3,3] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [1,1,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 0
test_input = { "nums": [2,1,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,1,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [1,2,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,2,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [3,2,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,3,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,3,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [3,3,1,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,1,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,1,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [3,1,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,2,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [2,2,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 1
test_input = { "nums": [3,2,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 2
test_input = { "nums": [1,3,2,1,2] }
assert my_solution.minimumOperations(**test_input) == 2 | 1,692,455,400 |
biweekly-contest-111-number-of-beautiful-integers-in-the-range | https://leetcode.com/problems/number-of-beautiful-integers-in-the-range | number-of-beautiful-integers-in-the-range | {
"questionId": "3017",
"questionFrontendId": "2827",
"title": "Number of Beautiful Integers in the Range",
"titleSlug": "number-of-beautiful-integers-in-the-range",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 313,
"dislikes": 24,
"categoryTitle": "Algorithms"
} | """
给你正整数 low ,high 和 k 。
如果一个数满足以下两个条件,那么它是 美丽的 :
* 偶数数位的数目与奇数数位的数目相同。
* 这个整数可以被 k 整除。
请你返回范围 [low, high] 中美丽整数的数目。
示例 1:
输入:low = 10, high = 20, k = 3
输出:2
解释:给定范围中有 2 个美丽数字:[12,18]
- 12 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 3 整除。
- 18 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 3 整除。
以下是一些不是美丽整数的例子:
- 16 不是美丽整数,因为它不能被 k = 3 整除。
- 15 不是美丽整数,因为它的奇数数位和偶数数位的数目不相等。
给定范围内总共有 2 个美丽整数。
示例 2:
输入:low = 1, high = 10, k = 1
输出:1
解释:给定范围中有 1 个美丽数字:[10]
- 10 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 1 整除。
给定范围内总共有 1 个美丽整数。
示例 3:
输入:low = 5, high = 5, k = 2
输出:0
解释:给定范围中有 0 个美丽数字。
- 5 不是美丽整数,因为它的奇数数位和偶数数位的数目不相等。
提示:
* 0 < low <= high <= 109
* 0 < k <= 20
"""
class Solution:
def numberOfBeautifulIntegers(self, low: int, high: int, k: int) -> int:
| 给你正整数 low ,high 和 k 。
如果一个数满足以下两个条件,那么它是 美丽的 :
* 偶数数位的数目与奇数数位的数目相同。
* 这个整数可以被 k 整除。
请你返回范围 [low, high] 中美丽整数的数目。
示例 1:
输入:low = 10, high = 20, k = 3
输出:2
解释:给定范围中有 2 个美丽数字:[12,18]
- 12 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 3 整除。
- 18 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 3 整除。
以下是一些不是美丽整数的例子:
- 16 不是美丽整数,因为它不能被 k = 3 整除。
- 15 不是美丽整数,因为它的奇数数位和偶数数位的数目不相等。
给定范围内总共有 2 个美丽整数。
示例 2:
输入:low = 1, high = 10, k = 1
输出:1
解释:给定范围中有 1 个美丽数字:[10]
- 10 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 1 整除。
给定范围内总共有 1 个美丽整数。
示例 3:
输入:low = 5, high = 5, k = 2
输出:0
解释:给定范围中有 0 个美丽数字。
- 5 不是美丽整数,因为它的奇数数位和偶数数位的数目不相等。
提示:
* 0 < low <= high <= 109
* 0 < k <= 20
请完成下面的代码来解决上述问题:
```python
class Solution:
def numberOfBeautifulIntegers(self, low: int, high: int, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "low": 10, "high": 20, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 1, "high": 10, "k": 1 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 5, "high": 5, "k": 2 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 3, "high": 31, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 25, "high": 31, "k": 11 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 9, "high": 25, "k": 4 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 58, "high": 72, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 5, "high": 79, "k": 12 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 26, "high": 74, "k": 7 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 4
test_input = { "low": 36, "high": 65, "k": 7 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 12, "high": 84, "k": 8 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 4
test_input = { "low": 13, "high": 91, "k": 13 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 8, "high": 18, "k": 4 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 22, "high": 59, "k": 6 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 15, "high": 27, "k": 9 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 4, "high": 9, "k": 19 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 14, "high": 81, "k": 17 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 12, "high": 33, "k": 18 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 10, "high": 17, "k": 8 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 42, "high": 58, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 22, "high": 42, "k": 4 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 5, "high": 8, "k": 5 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 12, "high": 75, "k": 13 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 2, "high": 28, "k": 1 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 9
test_input = { "low": 29, "high": 35, "k": 17 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 13, "high": 21, "k": 9 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 1, "high": 13, "k": 15 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 12, "high": 21, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 47, "high": 72, "k": 13 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 1, "high": 35, "k": 12 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 38, "high": 52, "k": 4 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 10, "high": 74, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 11
test_input = { "low": 60, "high": 92, "k": 11 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 3, "high": 25, "k": 15 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 63, "high": 65, "k": 20 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 22, "high": 77, "k": 7 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 4
test_input = { "low": 1, "high": 1, "k": 4 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 28, "high": 73, "k": 20 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 21, "high": 32, "k": 6 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 43, "high": 43, "k": 11 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 31, "high": 68, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 1, "high": 8, "k": 4 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 47, "high": 100, "k": 18 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 45, "high": 84, "k": 19 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 31, "high": 80, "k": 11 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 14, "high": 14, "k": 5 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 21, "high": 88, "k": 10 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 11, "high": 42, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 6
test_input = { "low": 19, "high": 53, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 57, "high": 98, "k": 6 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 4
test_input = { "low": 57, "high": 69, "k": 9 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 17, "high": 64, "k": 1 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 23
test_input = { "low": 29, "high": 40, "k": 15 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 36, "high": 60, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 16, "high": 23, "k": 13 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 36, "high": 99, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 11
test_input = { "low": 23, "high": 83, "k": 4 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 6
test_input = { "low": 4, "high": 5, "k": 9 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 15, "high": 21, "k": 2 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 51, "high": 76, "k": 7 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 24, "high": 34, "k": 8 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 24, "high": 99, "k": 1 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 38
test_input = { "low": 37, "high": 63, "k": 10 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 19, "high": 23, "k": 6 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 35, "high": 70, "k": 17 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 3, "high": 18, "k": 19 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 30, "high": 64, "k": 12 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 3, "high": 12, "k": 9 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 14, "high": 21, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 19, "high": 21, "k": 12 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 54, "high": 78, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 24, "high": 36, "k": 20 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 54, "high": 58, "k": 13 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 74, "high": 88, "k": 12 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 45, "high": 58, "k": 14 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 51, "high": 99, "k": 8 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 8, "high": 26, "k": 13 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 12, "high": 92, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 15
test_input = { "low": 18, "high": 91, "k": 11 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 14, "high": 53, "k": 15 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 2
test_input = { "low": 4, "high": 10, "k": 11 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 7, "high": 24, "k": 5 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 1, "high": 25, "k": 8 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 7, "high": 20, "k": 15 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 51, "high": 92, "k": 1 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 21
test_input = { "low": 73, "high": 73, "k": 3 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 50, "high": 63, "k": 19 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 30, "high": 51, "k": 19 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 2, "high": 65, "k": 7 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 5
test_input = { "low": 49, "high": 87, "k": 20 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 3, "high": 6, "k": 1 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 16, "high": 17, "k": 6 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 17, "high": 32, "k": 16 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 13, "high": 14, "k": 6 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 25, "high": 42, "k": 11 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 18, "high": 46, "k": 5 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 3
test_input = { "low": 1, "high": 65, "k": 6 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 5
test_input = { "low": 6, "high": 43, "k": 15 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 1
test_input = { "low": 5, "high": 9, "k": 12 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 0
test_input = { "low": 1, "high": 75, "k": 9 }
assert my_solution.numberOfBeautifulIntegers(**test_input) == 7 | 1,692,455,400 |
weekly-contest-358-max-pair-sum-in-an-array | https://leetcode.com/problems/max-pair-sum-in-an-array | max-pair-sum-in-an-array | {
"questionId": "2902",
"questionFrontendId": "2815",
"title": "Max Pair Sum in an Array",
"titleSlug": "max-pair-sum-in-an-array",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 274,
"dislikes": 91,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 。请你从 nums 中找出和 最大 的一对数,且这两个数数位上最大的数字相等。
返回最大和,如果不存在满足题意的数字对,返回 -1 。
示例 1:
输入:nums = [51,71,17,24,42]
输出:88
解释:
i = 1 和 j = 2 ,nums[i] 和 nums[j] 数位上最大的数字相等,且这一对的总和 71 + 17 = 88 。
i = 3 和 j = 4 ,nums[i] 和 nums[j] 数位上最大的数字相等,且这一对的总和 24 + 42 = 66 。
可以证明不存在其他数对满足数位上最大的数字相等,所以答案是 88 。
示例 2:
输入:nums = [1,2,3,4]
输出:-1
解释:不存在数对满足数位上最大的数字相等。
提示:
* 2 <= nums.length <= 100
* 1 <= nums[i] <= 104
"""
class Solution:
def maxSum(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始的整数数组 nums 。请你从 nums 中找出和 最大 的一对数,且这两个数数位上最大的数字相等。
返回最大和,如果不存在满足题意的数字对,返回 -1 。
示例 1:
输入:nums = [51,71,17,24,42]
输出:88
解释:
i = 1 和 j = 2 ,nums[i] 和 nums[j] 数位上最大的数字相等,且这一对的总和 71 + 17 = 88 。
i = 3 和 j = 4 ,nums[i] 和 nums[j] 数位上最大的数字相等,且这一对的总和 24 + 42 = 66 。
可以证明不存在其他数对满足数位上最大的数字相等,所以答案是 88 。
示例 2:
输入:nums = [1,2,3,4]
输出:-1
解释:不存在数对满足数位上最大的数字相等。
提示:
* 2 <= nums.length <= 100
* 1 <= nums[i] <= 104
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxSum(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [51,71,17,24,42] }
assert my_solution.maxSum(**test_input) == 88
test_input = { "nums": [1,2,3,4] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [31,25,72,79,74] }
assert my_solution.maxSum(**test_input) == 146
test_input = { "nums": [84,91,18,59,27,9,81,33,17,58] }
assert my_solution.maxSum(**test_input) == 165
test_input = { "nums": [8,75,28,35,21,13,21] }
assert my_solution.maxSum(**test_input) == 42
test_input = { "nums": [35,52,74,92,25,65,77,1,73,32] }
assert my_solution.maxSum(**test_input) == 151
test_input = { "nums": [68,8,100,84,80,14,88] }
assert my_solution.maxSum(**test_input) == 172
test_input = { "nums": [53,98,69,64,40,60,23] }
assert my_solution.maxSum(**test_input) == 167
test_input = { "nums": [21,76] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [99,63,23,70,18,64] }
assert my_solution.maxSum(**test_input) == 127
test_input = { "nums": [21,21,78] }
assert my_solution.maxSum(**test_input) == 42
test_input = { "nums": [58,88,58,99,26,92] }
assert my_solution.maxSum(**test_input) == 191
test_input = { "nums": [10,24,25,20,92,73,63,51] }
assert my_solution.maxSum(**test_input) == 76
test_input = { "nums": [87,6,17,32,14,42,46,65,43,9] }
assert my_solution.maxSum(**test_input) == 111
test_input = { "nums": [96,46,85,19,29] }
assert my_solution.maxSum(**test_input) == 125
test_input = { "nums": [5,24] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [26,76,24,96,82,97,97,72,35] }
assert my_solution.maxSum(**test_input) == 194
test_input = { "nums": [77,82,30,94] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [76,94,51,82,3,89,52,96] }
assert my_solution.maxSum(**test_input) == 190
test_input = { "nums": [27,59,57,97,6,46,88,41,52,46] }
assert my_solution.maxSum(**test_input) == 156
test_input = { "nums": [17,2] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [62,69] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [63,24,1] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [55,46,4,61,78,21,85,52,83,77] }
assert my_solution.maxSum(**test_input) == 168
test_input = { "nums": [21,73,2,80,99,98,89] }
assert my_solution.maxSum(**test_input) == 197
test_input = { "nums": [94,63,50,43,62,14,83,91] }
assert my_solution.maxSum(**test_input) == 185
test_input = { "nums": [66,17,17,35,46,77,7,15,38] }
assert my_solution.maxSum(**test_input) == 112
test_input = { "nums": [61,90,34,29,68,35] }
assert my_solution.maxSum(**test_input) == 119
test_input = { "nums": [18,82,78] }
assert my_solution.maxSum(**test_input) == 160
test_input = { "nums": [8,71,2,59,70,12] }
assert my_solution.maxSum(**test_input) == 141
test_input = { "nums": [55,88,59] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [49,47,46,65,37,24,75,81,54,39] }
assert my_solution.maxSum(**test_input) == 122
test_input = { "nums": [73,79,48,45,57,73,51,78,67,78] }
assert my_solution.maxSum(**test_input) == 156
test_input = { "nums": [2,82,80,74,34,54,65] }
assert my_solution.maxSum(**test_input) == 162
test_input = { "nums": [9,62,85,95,36,62,21,38,16,12] }
assert my_solution.maxSum(**test_input) == 124
test_input = { "nums": [50,80,34,9,86,20,67,94,65,82] }
assert my_solution.maxSum(**test_input) == 168
test_input = { "nums": [79,74,92,84,37,19] }
assert my_solution.maxSum(**test_input) == 171
test_input = { "nums": [85,20,79] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [89,55,67,84,3] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [16,44,2,54,58,94] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [71,14,24,13,21,14,100,18,84,37] }
assert my_solution.maxSum(**test_input) == 108
test_input = { "nums": [13,26] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [82,30,53,72,56,94,72,67] }
assert my_solution.maxSum(**test_input) == 144
test_input = { "nums": [14,80,92,65,85,70] }
assert my_solution.maxSum(**test_input) == 165
test_input = { "nums": [81,39,43,31,53,43,87,19,93] }
assert my_solution.maxSum(**test_input) == 168
test_input = { "nums": [27,12,80,38,94,92,67,54,56,20] }
assert my_solution.maxSum(**test_input) == 186
test_input = { "nums": [52,32,24,6,3,89,100,3,5,3] }
assert my_solution.maxSum(**test_input) == 57
test_input = { "nums": [93,1,13,88,47,48,46,63] }
assert my_solution.maxSum(**test_input) == 136
test_input = { "nums": [3,55,40,93,97,37,31,31] }
assert my_solution.maxSum(**test_input) == 190
test_input = { "nums": [58,41,10,74,40,17] }
assert my_solution.maxSum(**test_input) == 91
test_input = { "nums": [58,33,78,53,88,1,15,44,82] }
assert my_solution.maxSum(**test_input) == 170
test_input = { "nums": [41,48,96,71,35,89,57,71] }
assert my_solution.maxSum(**test_input) == 185
test_input = { "nums": [43,4,69,29,37,50] }
assert my_solution.maxSum(**test_input) == 98
test_input = { "nums": [65,88,2] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [86,42,59,44,76,6] }
assert my_solution.maxSum(**test_input) == 86
test_input = { "nums": [29,96,1,10,27,78,56,62] }
assert my_solution.maxSum(**test_input) == 125
test_input = { "nums": [100,48,6] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [33,17] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [8,91] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [91,13,72,42,28] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [5,53,35,88,77,1,66,57] }
assert my_solution.maxSum(**test_input) == 134
test_input = { "nums": [50,27,52,70,67,60,65] }
assert my_solution.maxSum(**test_input) == 137
test_input = { "nums": [84,82,31,45,94,62,45,32] }
assert my_solution.maxSum(**test_input) == 166
test_input = { "nums": [61,61,61,23,47,34,21,6,65,25] }
assert my_solution.maxSum(**test_input) == 126
test_input = { "nums": [60,21,11,99] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [22,83,62,12,63,100,41,33] }
assert my_solution.maxSum(**test_input) == 125
test_input = { "nums": [92,58,85] }
assert my_solution.maxSum(**test_input) == 143
test_input = { "nums": [93,5,46,26,25,36,27,12,30] }
assert my_solution.maxSum(**test_input) == 82
test_input = { "nums": [52,30,16] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [22,57,33,26,76,14,67] }
assert my_solution.maxSum(**test_input) == 143
test_input = { "nums": [90,72,37,30] }
assert my_solution.maxSum(**test_input) == 109
test_input = { "nums": [44,87,16] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [19,12,52,8,3,58] }
assert my_solution.maxSum(**test_input) == 66
test_input = { "nums": [88,52,35,6,58,47,62,82,47,86] }
assert my_solution.maxSum(**test_input) == 174
test_input = { "nums": [84,1,48,76,16,10,11,60] }
assert my_solution.maxSum(**test_input) == 132
test_input = { "nums": [12,60,69,63,78,22,28] }
assert my_solution.maxSum(**test_input) == 123
test_input = { "nums": [16,28,82,77,41,22] }
assert my_solution.maxSum(**test_input) == 110
test_input = { "nums": [97,31,63,2,94,14,47] }
assert my_solution.maxSum(**test_input) == 191
test_input = { "nums": [93,100,45,74,31,41,84,90,18,21] }
assert my_solution.maxSum(**test_input) == 183
test_input = { "nums": [21,12,38,64,57,24] }
assert my_solution.maxSum(**test_input) == 33
test_input = { "nums": [33,17,99,2,58,59,72,9,62] }
assert my_solution.maxSum(**test_input) == 158
test_input = { "nums": [36,11,23,98,14,89,90,53] }
assert my_solution.maxSum(**test_input) == 188
test_input = { "nums": [57,90,5,78,84,51] }
assert my_solution.maxSum(**test_input) == 162
test_input = { "nums": [73,73,76,48,30] }
assert my_solution.maxSum(**test_input) == 149
test_input = { "nums": [2,74,37,75] }
assert my_solution.maxSum(**test_input) == 149
test_input = { "nums": [84,35,65,12] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [95,46,23,81,35] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [64,76,46,54,64,94,90,95] }
assert my_solution.maxSum(**test_input) == 189
test_input = { "nums": [77,52,74,84,47,89,53] }
assert my_solution.maxSum(**test_input) == 151
test_input = { "nums": [29,31,52,12,89,88,10,18] }
assert my_solution.maxSum(**test_input) == 118
test_input = { "nums": [28,57,28,41,25,89,20] }
assert my_solution.maxSum(**test_input) == 56
test_input = { "nums": [31,28] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [51,1,98,73,84,11,100,100,75] }
assert my_solution.maxSum(**test_input) == 200
test_input = { "nums": [76,2,26,49,78,36,2,70,64] }
assert my_solution.maxSum(**test_input) == 146
test_input = { "nums": [34,63,21,49] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [35,19,1,21,11,59,38] }
assert my_solution.maxSum(**test_input) == 78
test_input = { "nums": [1,35,74,58,56,54,75] }
assert my_solution.maxSum(**test_input) == 149
test_input = { "nums": [20,49] }
assert my_solution.maxSum(**test_input) == -1
test_input = { "nums": [97,92,13,30] }
assert my_solution.maxSum(**test_input) == 189
test_input = { "nums": [89,49,10,36,37] }
assert my_solution.maxSum(**test_input) == 138 | 1,691,893,800 |
weekly-contest-358-double-a-number-represented-as-a-linked-list | https://leetcode.com/problems/double-a-number-represented-as-a-linked-list | double-a-number-represented-as-a-linked-list | {
"questionId": "2871",
"questionFrontendId": "2816",
"title": "Double a Number Represented as a Linked List",
"titleSlug": "double-a-number-represented-as-a-linked-list",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 397,
"dislikes": 5,
"categoryTitle": "Algorithms"
} | """
给你一个 非空 链表的头节点 head ,表示一个不含前导零的非负数整数。
将链表 翻倍 后,返回头节点 head 。
示例 1:
[https://assets.leetcode.com/uploads/2023/05/28/example.png]
输入:head = [1,8,9]
输出:[3,7,8]
解释:上图中给出的链表,表示数字 189 。返回的链表表示数字 189 * 2 = 378 。
示例 2:
[https://assets.leetcode.com/uploads/2023/05/28/example2.png]
输入:head = [9,9,9]
输出:[1,9,9,8]
解释:上图中给出的链表,表示数字 999 。返回的链表表示数字 999 * 2 = 1998 。
提示:
* 链表中节点的数目在范围 [1, 104] 内
* 0 <= Node.val <= 9
* 生成的输入满足:链表表示一个不含前导零的数字,除了数字 0 本身。
"""
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
| 给你一个 非空 链表的头节点 head ,表示一个不含前导零的非负数整数。
将链表 翻倍 后,返回头节点 head 。
示例 1:
[https://assets.leetcode.com/uploads/2023/05/28/example.png]
输入:head = [1,8,9]
输出:[3,7,8]
解释:上图中给出的链表,表示数字 189 。返回的链表表示数字 189 * 2 = 378 。
示例 2:
[https://assets.leetcode.com/uploads/2023/05/28/example2.png]
输入:head = [9,9,9]
输出:[1,9,9,8]
解释:上图中给出的链表,表示数字 999 。返回的链表表示数字 999 * 2 = 1998 。
提示:
* 链表中节点的数目在范围 [1, 104] 内
* 0 <= Node.val <= 9
* 生成的输入满足:链表表示一个不含前导零的数字,除了数字 0 本身。
请完成下面的代码来解决上述问题:
```python
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
``` |
my_solution = Solution()
_f1 = lambda lst: ListNode(lst[0], _f1(lst[1:])) if lst else None
_f2 = lambda node: [node.val] + _f2(node.next) if node else []
test_input = { "head": _f1([1,8,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [3,7,8]
test_input = { "head": _f1([9,9,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,9,9,8]
test_input = { "head": _f1([0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [0]
test_input = { "head": _f1([1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [2]
test_input = { "head": _f1([2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [4]
test_input = { "head": _f1([3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [6]
test_input = { "head": _f1([4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [8]
test_input = { "head": _f1([5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,0]
test_input = { "head": _f1([6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,2]
test_input = { "head": _f1([7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,4]
test_input = { "head": _f1([8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,6]
test_input = { "head": _f1([9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,8]
test_input = { "head": _f1([1,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [2,0]
test_input = { "head": _f1([1,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [2,2]
test_input = { "head": _f1([1,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [2,4]
test_input = { "head": _f1([1,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [2,6]
test_input = { "head": _f1([1,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [2,8]
test_input = { "head": _f1([1,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [3,0]
test_input = { "head": _f1([1,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [3,2]
test_input = { "head": _f1([1,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [3,4]
test_input = { "head": _f1([1,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [3,6]
test_input = { "head": _f1([1,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [3,8]
test_input = { "head": _f1([2,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [4,0]
test_input = { "head": _f1([2,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [4,2]
test_input = { "head": _f1([2,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [4,4]
test_input = { "head": _f1([2,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [4,6]
test_input = { "head": _f1([2,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [4,8]
test_input = { "head": _f1([2,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [5,0]
test_input = { "head": _f1([2,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [5,2]
test_input = { "head": _f1([2,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [5,4]
test_input = { "head": _f1([2,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [5,6]
test_input = { "head": _f1([2,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [5,8]
test_input = { "head": _f1([3,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [6,0]
test_input = { "head": _f1([3,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [6,2]
test_input = { "head": _f1([3,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [6,4]
test_input = { "head": _f1([3,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [6,6]
test_input = { "head": _f1([3,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [6,8]
test_input = { "head": _f1([3,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [7,0]
test_input = { "head": _f1([3,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [7,2]
test_input = { "head": _f1([3,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [7,4]
test_input = { "head": _f1([3,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [7,6]
test_input = { "head": _f1([3,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [7,8]
test_input = { "head": _f1([4,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [8,0]
test_input = { "head": _f1([4,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [8,2]
test_input = { "head": _f1([4,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [8,4]
test_input = { "head": _f1([4,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [8,6]
test_input = { "head": _f1([4,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [8,8]
test_input = { "head": _f1([4,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [9,0]
test_input = { "head": _f1([4,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [9,2]
test_input = { "head": _f1([4,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [9,4]
test_input = { "head": _f1([4,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [9,6]
test_input = { "head": _f1([4,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [9,8]
test_input = { "head": _f1([5,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,0,0]
test_input = { "head": _f1([5,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,0,2]
test_input = { "head": _f1([5,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,0,4]
test_input = { "head": _f1([5,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,0,6]
test_input = { "head": _f1([5,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,0,8]
test_input = { "head": _f1([5,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,1,0]
test_input = { "head": _f1([5,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,1,2]
test_input = { "head": _f1([5,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,1,4]
test_input = { "head": _f1([5,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,1,6]
test_input = { "head": _f1([5,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,1,8]
test_input = { "head": _f1([6,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,2,0]
test_input = { "head": _f1([6,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,2,2]
test_input = { "head": _f1([6,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,2,4]
test_input = { "head": _f1([6,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,2,6]
test_input = { "head": _f1([6,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,2,8]
test_input = { "head": _f1([6,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,3,0]
test_input = { "head": _f1([6,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,3,2]
test_input = { "head": _f1([6,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,3,4]
test_input = { "head": _f1([6,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,3,6]
test_input = { "head": _f1([6,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,3,8]
test_input = { "head": _f1([7,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,4,0]
test_input = { "head": _f1([7,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,4,2]
test_input = { "head": _f1([7,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,4,4]
test_input = { "head": _f1([7,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,4,6]
test_input = { "head": _f1([7,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,4,8]
test_input = { "head": _f1([7,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,5,0]
test_input = { "head": _f1([7,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,5,2]
test_input = { "head": _f1([7,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,5,4]
test_input = { "head": _f1([7,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,5,6]
test_input = { "head": _f1([7,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,5,8]
test_input = { "head": _f1([8,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,6,0]
test_input = { "head": _f1([8,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,6,2]
test_input = { "head": _f1([8,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,6,4]
test_input = { "head": _f1([8,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,6,6]
test_input = { "head": _f1([8,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,6,8]
test_input = { "head": _f1([8,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,7,0]
test_input = { "head": _f1([8,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,7,2]
test_input = { "head": _f1([8,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,7,4]
test_input = { "head": _f1([8,8]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,7,6]
test_input = { "head": _f1([8,9]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,7,8]
test_input = { "head": _f1([9,0]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,8,0]
test_input = { "head": _f1([9,1]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,8,2]
test_input = { "head": _f1([9,2]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,8,4]
test_input = { "head": _f1([9,3]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,8,6]
test_input = { "head": _f1([9,4]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,8,8]
test_input = { "head": _f1([9,5]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,9,0]
test_input = { "head": _f1([9,6]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,9,2]
test_input = { "head": _f1([9,7]) }
assert _f2(my_solution.doubleIt(**test_input)) == [1,9,4] | 1,691,893,800 |
weekly-contest-358-minimum-absolute-difference-between-elements-with-constraint | https://leetcode.com/problems/minimum-absolute-difference-between-elements-with-constraint | minimum-absolute-difference-between-elements-with-constraint | {
"questionId": "3000",
"questionFrontendId": "2817",
"title": "Minimum Absolute Difference Between Elements With Constraint",
"titleSlug": "minimum-absolute-difference-between-elements-with-constraint",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 618,
"dislikes": 64,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 和一个整数 x 。
请你找到数组中下标距离至少为 x 的两个元素的 差值绝对值 的 最小值 。
换言之,请你找到两个下标 i 和 j ,满足 abs(i - j) >= x 且 abs(nums[i] - nums[j]) 的值最小。
请你返回一个整数,表示下标距离至少为 x 的两个元素之间的差值绝对值的 最小值 。
示例 1:
输入:nums = [4,3,2,4], x = 2
输出:0
解释:我们选择 nums[0] = 4 和 nums[3] = 4 。
它们下标距离满足至少为 2 ,差值绝对值为最小值 0 。
0 是最优解。
示例 2:
输入:nums = [5,3,2,10,15], x = 1
输出:1
解释:我们选择 nums[1] = 3 和 nums[2] = 2 。
它们下标距离满足至少为 1 ,差值绝对值为最小值 1 。
1 是最优解。
示例 3:
输入:nums = [1,2,3,4], x = 3
输出:3
解释:我们选择 nums[0] = 1 和 nums[3] = 4 。
它们下标距离满足至少为 3 ,差值绝对值为最小值 3 。
3 是最优解。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* 0 <= x < nums.length
"""
class Solution:
def minAbsoluteDifference(self, nums: List[int], x: int) -> int:
| 给你一个下标从 0 开始的整数数组 nums 和一个整数 x 。
请你找到数组中下标距离至少为 x 的两个元素的 差值绝对值 的 最小值 。
换言之,请你找到两个下标 i 和 j ,满足 abs(i - j) >= x 且 abs(nums[i] - nums[j]) 的值最小。
请你返回一个整数,表示下标距离至少为 x 的两个元素之间的差值绝对值的 最小值 。
示例 1:
输入:nums = [4,3,2,4], x = 2
输出:0
解释:我们选择 nums[0] = 4 和 nums[3] = 4 。
它们下标距离满足至少为 2 ,差值绝对值为最小值 0 。
0 是最优解。
示例 2:
输入:nums = [5,3,2,10,15], x = 1
输出:1
解释:我们选择 nums[1] = 3 和 nums[2] = 2 。
它们下标距离满足至少为 1 ,差值绝对值为最小值 1 。
1 是最优解。
示例 3:
输入:nums = [1,2,3,4], x = 3
输出:3
解释:我们选择 nums[0] = 1 和 nums[3] = 4 。
它们下标距离满足至少为 3 ,差值绝对值为最小值 3 。
3 是最优解。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* 0 <= x < nums.length
请完成下面的代码来解决上述问题:
```python
class Solution:
def minAbsoluteDifference(self, nums: List[int], x: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [4,3,2,4], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 0
test_input = { "nums": [5,3,2,10,15], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 1
test_input = { "nums": [1,2,3,4], "x": 3 }
assert my_solution.minAbsoluteDifference(**test_input) == 3
test_input = { "nums": [1,67], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 66
test_input = { "nums": [7,398], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 391
test_input = { "nums": [12,141], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 129
test_input = { "nums": [21,75], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 54
test_input = { "nums": [22,147], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 125
test_input = { "nums": [25,197], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 172
test_input = { "nums": [27,275], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 248
test_input = { "nums": [37,192], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 155
test_input = { "nums": [41,163], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 122
test_input = { "nums": [45,49], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 4
test_input = { "nums": [48,195], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 147
test_input = { "nums": [68,68], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 0
test_input = { "nums": [71,4], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 67
test_input = { "nums": [72,169], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 97
test_input = { "nums": [74,62], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 12
test_input = { "nums": [75,1], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 74
test_input = { "nums": [76,49], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 27
test_input = { "nums": [88,72], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 16
test_input = { "nums": [99,370], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 271
test_input = { "nums": [103,39], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 64
test_input = { "nums": [109,99], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 10
test_input = { "nums": [111,161], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 50
test_input = { "nums": [113,117], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 4
test_input = { "nums": [119,184], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 65
test_input = { "nums": [122,118], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 4
test_input = { "nums": [123,13], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 110
test_input = { "nums": [123,162], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 39
test_input = { "nums": [126,69], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 57
test_input = { "nums": [127,18], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 109
test_input = { "nums": [127,346], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 219
test_input = { "nums": [132,110], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 22
test_input = { "nums": [134,23], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 111
test_input = { "nums": [136,150], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 14
test_input = { "nums": [139,215], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 76
test_input = { "nums": [153,3], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 150
test_input = { "nums": [156,67], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 89
test_input = { "nums": [160,168], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 8
test_input = { "nums": [161,93], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 68
test_input = { "nums": [164,81], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 83
test_input = { "nums": [167,83], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 84
test_input = { "nums": [174,58], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 116
test_input = { "nums": [174,102], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 72
test_input = { "nums": [175,137], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 38
test_input = { "nums": [176,99], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 77
test_input = { "nums": [178,179], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 1
test_input = { "nums": [228,359], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 131
test_input = { "nums": [243,280], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 37
test_input = { "nums": [283,62], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 221
test_input = { "nums": [288,149], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 139
test_input = { "nums": [293,278], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 15
test_input = { "nums": [327,425], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 98
test_input = { "nums": [337,187], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 150
test_input = { "nums": [346,160], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 186
test_input = { "nums": [347,369], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 22
test_input = { "nums": [355,199], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 156
test_input = { "nums": [413,311], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 102
test_input = { "nums": [417,320], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 97
test_input = { "nums": [418,131], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 287
test_input = { "nums": [3274,71], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 3203
test_input = { "nums": [5,14,81], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 76
test_input = { "nums": [9,25,15], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 6
test_input = { "nums": [9,113,136], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 23
test_input = { "nums": [13,19,12], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 1
test_input = { "nums": [13,94,59], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 46
test_input = { "nums": [14,111,16], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 2
test_input = { "nums": [17,173,69], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 52
test_input = { "nums": [24,39,28], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 4
test_input = { "nums": [32,129,93], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 36
test_input = { "nums": [33,18,131], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 15
test_input = { "nums": [36,19,27], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 9
test_input = { "nums": [40,18,17], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 1
test_input = { "nums": [43,49,20], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 23
test_input = { "nums": [44,186,163], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 119
test_input = { "nums": [56,23,158], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 33
test_input = { "nums": [62,37,182], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 120
test_input = { "nums": [63,116,12], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 51
test_input = { "nums": [66,345,278], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 212
test_input = { "nums": [67,81,165], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 98
test_input = { "nums": [70,184,70], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 0
test_input = { "nums": [73,106,172], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 33
test_input = { "nums": [74,199,57], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 17
test_input = { "nums": [83,14,14], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 0
test_input = { "nums": [86,1,129], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 43
test_input = { "nums": [87,194,107], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 20
test_input = { "nums": [88,75,122], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 34
test_input = { "nums": [93,96,28], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 3
test_input = { "nums": [95,86,132], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 37
test_input = { "nums": [96,41,24], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 17
test_input = { "nums": [116,6,3], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 3
test_input = { "nums": [120,102,184], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 18
test_input = { "nums": [123,113,20], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 10
test_input = { "nums": [125,14,141], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 16
test_input = { "nums": [126,2,180], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 54
test_input = { "nums": [136,24,114], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 22
test_input = { "nums": [136,177,98], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 38
test_input = { "nums": [136,177,123], "x": 1 }
assert my_solution.minAbsoluteDifference(**test_input) == 13
test_input = { "nums": [136,178,18], "x": 2 }
assert my_solution.minAbsoluteDifference(**test_input) == 118 | 1,691,893,800 |
weekly-contest-358-apply-operations-to-maximize-score | https://leetcode.com/problems/apply-operations-to-maximize-score | apply-operations-to-maximize-score | {
"questionId": "3001",
"questionFrontendId": "2818",
"title": "Apply Operations to Maximize Score",
"titleSlug": "apply-operations-to-maximize-score",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 301,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 的正整数数组 nums 和一个整数 k 。
一开始,你的分数为 1 。你可以进行以下操作至多 k 次,目标是使你的分数最大:
* 选择一个之前没有选过的 非空 子数组 nums[l, ..., r] 。
* 从 nums[l, ..., r] 里面选择一个 质数分数 最高的元素 x 。如果多个元素质数分数相同且最高,选择下标最小的一个。
* 将你的分数乘以 x 。
nums[l, ..., r] 表示 nums 中起始下标为 l ,结束下标为 r 的子数组,两个端点都包含。
一个整数的 质数分数 等于 x 不同质因子的数目。比方说, 300 的质数分数为 3 ,因为 300 = 2 * 2 * 3 * 5 * 5 。
请你返回进行至多 k 次操作后,可以得到的 最大分数 。
由于答案可能很大,请你将结果对 109 + 7 取余后返回。
示例 1:
输入:nums = [8,3,9,3,8], k = 2
输出:81
解释:进行以下操作可以得到分数 81 :
- 选择子数组 nums[2, ..., 2] 。nums[2] 是子数组中唯一的元素。所以我们将分数乘以 nums[2] ,分数变为 1 * 9 = 9 。
- 选择子数组 nums[2, ..., 3] 。nums[2] 和 nums[3] 质数分数都为 1 ,但是 nums[2] 下标更小。所以我们将分数乘以 nums[2] ,分数变为 9 * 9 = 81 。
81 是可以得到的最高得分。
示例 2:
输入:nums = [19,12,14,6,10,18], k = 3
输出:4788
解释:进行以下操作可以得到分数 4788 :
- 选择子数组 nums[0, ..., 0] 。nums[0] 是子数组中唯一的元素。所以我们将分数乘以 nums[0] ,分数变为 1 * 19 = 19 。
- 选择子数组 nums[5, ..., 5] 。nums[5] 是子数组中唯一的元素。所以我们将分数乘以 nums[5] ,分数变为 19 * 18 = 342 。
- 选择子数组 nums[2, ..., 3] 。nums[2] 和 nums[3] 质数分数都为 2,但是 nums[2] 下标更小。所以我们将分数乘以 nums[2] ,分数变为 342 * 14 = 4788 。
4788 是可以得到的最高的分。
提示:
* 1 <= nums.length == n <= 105
* 1 <= nums[i] <= 105
* 1 <= k <= min(n * (n + 1) / 2, 109)
"""
class Solution:
def maximumScore(self, nums: List[int], k: int) -> int:
| 给你一个长度为 n 的正整数数组 nums 和一个整数 k 。
一开始,你的分数为 1 。你可以进行以下操作至多 k 次,目标是使你的分数最大:
* 选择一个之前没有选过的 非空 子数组 nums[l, ..., r] 。
* 从 nums[l, ..., r] 里面选择一个 质数分数 最高的元素 x 。如果多个元素质数分数相同且最高,选择下标最小的一个。
* 将你的分数乘以 x 。
nums[l, ..., r] 表示 nums 中起始下标为 l ,结束下标为 r 的子数组,两个端点都包含。
一个整数的 质数分数 等于 x 不同质因子的数目。比方说, 300 的质数分数为 3 ,因为 300 = 2 * 2 * 3 * 5 * 5 。
请你返回进行至多 k 次操作后,可以得到的 最大分数 。
由于答案可能很大,请你将结果对 109 + 7 取余后返回。
示例 1:
输入:nums = [8,3,9,3,8], k = 2
输出:81
解释:进行以下操作可以得到分数 81 :
- 选择子数组 nums[2, ..., 2] 。nums[2] 是子数组中唯一的元素。所以我们将分数乘以 nums[2] ,分数变为 1 * 9 = 9 。
- 选择子数组 nums[2, ..., 3] 。nums[2] 和 nums[3] 质数分数都为 1 ,但是 nums[2] 下标更小。所以我们将分数乘以 nums[2] ,分数变为 9 * 9 = 81 。
81 是可以得到的最高得分。
示例 2:
输入:nums = [19,12,14,6,10,18], k = 3
输出:4788
解释:进行以下操作可以得到分数 4788 :
- 选择子数组 nums[0, ..., 0] 。nums[0] 是子数组中唯一的元素。所以我们将分数乘以 nums[0] ,分数变为 1 * 19 = 19 。
- 选择子数组 nums[5, ..., 5] 。nums[5] 是子数组中唯一的元素。所以我们将分数乘以 nums[5] ,分数变为 19 * 18 = 342 。
- 选择子数组 nums[2, ..., 3] 。nums[2] 和 nums[3] 质数分数都为 2,但是 nums[2] 下标更小。所以我们将分数乘以 nums[2] ,分数变为 342 * 14 = 4788 。
4788 是可以得到的最高的分。
提示:
* 1 <= nums.length == n <= 105
* 1 <= nums[i] <= 105
* 1 <= k <= min(n * (n + 1) / 2, 109)
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumScore(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [8,3,9,3,8], "k": 2 }
assert my_solution.maximumScore(**test_input) == 81
test_input = { "nums": [19,12,14,6,10,18], "k": 3 }
assert my_solution.maximumScore(**test_input) == 4788
test_input = { "nums": [3289,2832,14858,22011], "k": 6 }
assert my_solution.maximumScore(**test_input) == 256720975
test_input = { "nums": [1,7,11,1,5], "k": 14 }
assert my_solution.maximumScore(**test_input) == 823751938
test_input = { "nums": [1,1,2,18,1,9,3,1], "k": 32 }
assert my_solution.maximumScore(**test_input) == 346264255
test_input = { "nums": [1,1,1], "k": 2 }
assert my_solution.maximumScore(**test_input) == 1
test_input = { "nums": [1,2,1,12,1,3], "k": 3 }
assert my_solution.maximumScore(**test_input) == 1728
test_input = { "nums": [12,5,1,6,9,1,17,14], "k": 12 }
assert my_solution.maximumScore(**test_input) == 62996359
test_input = { "nums": [1], "k": 1 }
assert my_solution.maximumScore(**test_input) == 1
test_input = { "nums": [1,10,15,1,3], "k": 13 }
assert my_solution.maximumScore(**test_input) == 499978741
test_input = { "nums": [6,1,13,10,1,17,6], "k": 27 }
assert my_solution.maximumScore(**test_input) == 630596200
test_input = { "nums": [1,14], "k": 1 }
assert my_solution.maximumScore(**test_input) == 14
test_input = { "nums": [2,1,14,5,18,1,8,5], "k": 34 }
assert my_solution.maximumScore(**test_input) == 799392504
test_input = { "nums": [5,12,11,15,10,18], "k": 18 }
assert my_solution.maximumScore(**test_input) == 557423913
test_input = { "nums": [4,1], "k": 3 }
assert my_solution.maximumScore(**test_input) == 16
test_input = { "nums": [1,2,5,1,10,1,1], "k": 20 }
assert my_solution.maximumScore(**test_input) == 600000014
test_input = { "nums": [13,16,12,15,12,1,13,1,18,1], "k": 46 }
assert my_solution.maximumScore(**test_input) == 912532739
test_input = { "nums": [10,11], "k": 3 }
assert my_solution.maximumScore(**test_input) == 1100
test_input = { "nums": [15,16,12,1,10,14], "k": 19 }
assert my_solution.maximumScore(**test_input) == 311972352
test_input = { "nums": [14,12,5,2,14], "k": 6 }
assert my_solution.maximumScore(**test_input) == 7529536
test_input = { "nums": [1,13,12,1,9,12,1,18], "k": 31 }
assert my_solution.maximumScore(**test_input) == 846374420
test_input = { "nums": [1,6,14,10,16], "k": 4 }
assert my_solution.maximumScore(**test_input) == 43904
test_input = { "nums": [14,1,9,1,10], "k": 3 }
assert my_solution.maximumScore(**test_input) == 2744
test_input = { "nums": [13,1,1,15,9,1,1], "k": 22 }
assert my_solution.maximumScore(**test_input) == 925331761
test_input = { "nums": [1,17,3,9,10,17,1,1,1,11], "k": 22 }
assert my_solution.maximumScore(**test_input) == 407065837
test_input = { "nums": [1,1,1], "k": 5 }
assert my_solution.maximumScore(**test_input) == 1
test_input = { "nums": [1,13,4,1,1], "k": 14 }
assert my_solution.maximumScore(**test_input) == 206765780
test_input = { "nums": [3,1,3,10,2,16], "k": 16 }
assert my_solution.maximumScore(**test_input) == 996976007
test_input = { "nums": [1,2,1,1,16,8,11,6], "k": 27 }
assert my_solution.maximumScore(**test_input) == 33977400
test_input = { "nums": [1,10,13,1,9,15,1], "k": 2 }
assert my_solution.maximumScore(**test_input) == 225
test_input = { "nums": [18,1], "k": 3 }
assert my_solution.maximumScore(**test_input) == 324
test_input = { "nums": [16], "k": 1 }
assert my_solution.maximumScore(**test_input) == 16
test_input = { "nums": [14,6,18,10,1,8,1,17], "k": 25 }
assert my_solution.maximumScore(**test_input) == 677968714
test_input = { "nums": [15,16,12], "k": 6 }
assert my_solution.maximumScore(**test_input) == 7776000
test_input = { "nums": [1,10,1,2], "k": 7 }
assert my_solution.maximumScore(**test_input) == 2000000
test_input = { "nums": [13,1,10,1,15,6,1,12,6], "k": 10 }
assert my_solution.maximumScore(**test_input) == 650386593
test_input = { "nums": [1,15,5,1,1,15,1,1], "k": 17 }
assert my_solution.maximumScore(**test_input) == 10486853
test_input = { "nums": [1,9], "k": 2 }
assert my_solution.maximumScore(**test_input) == 81
test_input = { "nums": [13,8,14,6,3,14,13,10,1], "k": 21 }
assert my_solution.maximumScore(**test_input) == 247566578
test_input = { "nums": [4], "k": 1 }
assert my_solution.maximumScore(**test_input) == 4
test_input = { "nums": [1,12,5,1,12], "k": 15 }
assert my_solution.maximumScore(**test_input) == 209137175
test_input = { "nums": [1,5,1,11], "k": 4 }
assert my_solution.maximumScore(**test_input) == 3025
test_input = { "nums": [3,6,17,1,1,17,18,12,16,5], "k": 9 }
assert my_solution.maximumScore(**test_input) == 359288982
test_input = { "nums": [2,2,15,2,15,15], "k": 10 }
assert my_solution.maximumScore(**test_input) == 650386593
test_input = { "nums": [1,12], "k": 1 }
assert my_solution.maximumScore(**test_input) == 12
test_input = { "nums": [2,11,1,1], "k": 2 }
assert my_solution.maximumScore(**test_input) == 121
test_input = { "nums": [11,15,18,4,11,7,1,1], "k": 32 }
assert my_solution.maximumScore(**test_input) == 179964426
test_input = { "nums": [10,1,15], "k": 1 }
assert my_solution.maximumScore(**test_input) == 15
test_input = { "nums": [10,1,1,9,14,6,4,18,8], "k": 10 }
assert my_solution.maximumScore(**test_input) == 420565606
test_input = { "nums": [6,8], "k": 3 }
assert my_solution.maximumScore(**test_input) == 288
test_input = { "nums": [13,1,10,6], "k": 4 }
assert my_solution.maximumScore(**test_input) == 16900
test_input = { "nums": [8,10,16,1,1,1], "k": 2 }
assert my_solution.maximumScore(**test_input) == 256
test_input = { "nums": [11,2,1], "k": 4 }
assert my_solution.maximumScore(**test_input) == 2662
test_input = { "nums": [15], "k": 1 }
assert my_solution.maximumScore(**test_input) == 15
test_input = { "nums": [5,5,16,12,8,1,1,7,12], "k": 34 }
assert my_solution.maximumScore(**test_input) == 15762264
test_input = { "nums": [1,1], "k": 1 }
assert my_solution.maximumScore(**test_input) == 1
test_input = { "nums": [3,18,12,8,1,3,6], "k": 23 }
assert my_solution.maximumScore(**test_input) == 18966086
test_input = { "nums": [13,1,5,6], "k": 8 }
assert my_solution.maximumScore(**test_input) == 14236560
test_input = { "nums": [1,1,15,1,9,1,1], "k": 23 }
assert my_solution.maximumScore(**test_input) == 929527145
test_input = { "nums": [6,2,1,17,9,14,1], "k": 25 }
assert my_solution.maximumScore(**test_input) == 808455901
test_input = { "nums": [1,18], "k": 3 }
assert my_solution.maximumScore(**test_input) == 324
test_input = { "nums": [1,10], "k": 2 }
assert my_solution.maximumScore(**test_input) == 100
test_input = { "nums": [17], "k": 1 }
assert my_solution.maximumScore(**test_input) == 17
test_input = { "nums": [18,13,12,1,11], "k": 8 }
assert my_solution.maximumScore(**test_input) == 537271275
test_input = { "nums": [10,14,18,6,12,14,3,13], "k": 23 }
assert my_solution.maximumScore(**test_input) == 795923147
test_input = { "nums": [16,1,14,13,3,1], "k": 6 }
assert my_solution.maximumScore(**test_input) == 9834496
test_input = { "nums": [10,4,4,3,10,1], "k": 15 }
assert my_solution.maximumScore(**test_input) == 997200007
test_input = { "nums": [2,4,1,5,14,13,13,17], "k": 19 }
assert my_solution.maximumScore(**test_input) == 241329678
test_input = { "nums": [6,1,15,1,13], "k": 12 }
assert my_solution.maximumScore(**test_input) == 820232542
test_input = { "nums": [14,13,1,14], "k": 5 }
assert my_solution.maximumScore(**test_input) == 537824
test_input = { "nums": [10,4,12,12,16,9,1,18], "k": 36 }
assert my_solution.maximumScore(**test_input) == 892891506
test_input = { "nums": [6], "k": 1 }
assert my_solution.maximumScore(**test_input) == 6
test_input = { "nums": [12], "k": 1 }
assert my_solution.maximumScore(**test_input) == 12
test_input = { "nums": [10,17,17,15,1,15,1], "k": 19 }
assert my_solution.maximumScore(**test_input) == 301460564
test_input = { "nums": [10,3,1,1,4,2,14], "k": 10 }
assert my_solution.maximumScore(**test_input) == 295359475
test_input = { "nums": [10,12], "k": 3 }
assert my_solution.maximumScore(**test_input) == 1200
test_input = { "nums": [9,10,2,3,2,1,11,1,1,13], "k": 49 }
assert my_solution.maximumScore(**test_input) == 900432644
test_input = { "nums": [1,10,18,18,3,1,8,1,15,15], "k": 18 }
assert my_solution.maximumScore(**test_input) == 38759446
test_input = { "nums": [15,1,11,13,16], "k": 14 }
assert my_solution.maximumScore(**test_input) == 753886562
test_input = { "nums": [12,1,6,1,1,4,8,17,18,1], "k": 2 }
assert my_solution.maximumScore(**test_input) == 324
test_input = { "nums": [1,1,1,17,8,18,8,1], "k": 14 }
assert my_solution.maximumScore(**test_input) == 958387476
test_input = { "nums": [7,1,6,9,11,7,13,12,1], "k": 39 }
assert my_solution.maximumScore(**test_input) == 21295572
test_input = { "nums": [12,1,1,15,2,1,1,16,1,1], "k": 8 }
assert my_solution.maximumScore(**test_input) == 294967268
test_input = { "nums": [3,14,13,1,11,1,1,1], "k": 22 }
assert my_solution.maximumScore(**test_input) == 810815174
test_input = { "nums": [14,3,6,6,1,7,13,16], "k": 28 }
assert my_solution.maximumScore(**test_input) == 312142986
test_input = { "nums": [3,4,4,15,18,12,6], "k": 8 }
assert my_solution.maximumScore(**test_input) == 428674972
test_input = { "nums": [6,15,17,9,9,17], "k": 5 }
assert my_solution.maximumScore(**test_input) == 1419857
test_input = { "nums": [2,12,1,11,12], "k": 5 }
assert my_solution.maximumScore(**test_input) == 248832
test_input = { "nums": [1,15,15], "k": 4 }
assert my_solution.maximumScore(**test_input) == 50625
test_input = { "nums": [7,11,3,1,12,10], "k": 15 }
assert my_solution.maximumScore(**test_input) == 784368200
test_input = { "nums": [18,1,11,15,16,18,18,13,10,10], "k": 41 }
assert my_solution.maximumScore(**test_input) == 911212578
test_input = { "nums": [11,15,2,14,6,15], "k": 2 }
assert my_solution.maximumScore(**test_input) == 225
test_input = { "nums": [11,2,15,1,1,11,7,1], "k": 16 }
assert my_solution.maximumScore(**test_input) == 734032462
test_input = { "nums": [1,6,12,4,10,13,7,6,17,1], "k": 50 }
assert my_solution.maximumScore(**test_input) == 377786273
test_input = { "nums": [1,5,4,18,12,1,1,1,7,12], "k": 3 }
assert my_solution.maximumScore(**test_input) == 5832
test_input = { "nums": [5,1,17,10,12], "k": 9 }
assert my_solution.maximumScore(**test_input) == 467999979
test_input = { "nums": [6,1,1,4,10,15,16,8], "k": 12 }
assert my_solution.maximumScore(**test_input) == 999939527
test_input = { "nums": [1,1], "k": 3 }
assert my_solution.maximumScore(**test_input) == 1
test_input = { "nums": [8], "k": 1 }
assert my_solution.maximumScore(**test_input) == 8
test_input = { "nums": [9,8,14,1,14,14,5,1,6], "k": 44 }
assert my_solution.maximumScore(**test_input) == 439903801 | 1,691,893,800 |
weekly-contest-357-faulty-keyboard | https://leetcode.com/problems/faulty-keyboard | faulty-keyboard | {
"questionId": "2886",
"questionFrontendId": "2810",
"title": "Faulty Keyboard",
"titleSlug": "faulty-keyboard",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 343,
"dislikes": 5,
"categoryTitle": "Algorithms"
} | """
你的笔记本键盘存在故障,每当你在上面输入字符 'i' 时,它会反转你所写的字符串。而输入其他字符则可以正常工作。
给你一个下标从 0 开始的字符串 s ,请你用故障键盘依次输入每个字符。
返回最终笔记本屏幕上输出的字符串。
示例 1:
输入:s = "string"
输出:"rtsng"
解释:
输入第 1 个字符后,屏幕上的文本是:"s" 。
输入第 2 个字符后,屏幕上的文本是:"st" 。
输入第 3 个字符后,屏幕上的文本是:"str" 。
因为第 4 个字符是 'i' ,屏幕上的文本被反转,变成 "rts" 。
输入第 5 个字符后,屏幕上的文本是:"rtsn" 。
输入第 6 个字符后,屏幕上的文本是: "rtsng" 。
因此,返回 "rtsng" 。
示例 2:
输入:s = "poiinter"
输出:"ponter"
解释:
输入第 1 个字符后,屏幕上的文本是:"p" 。
输入第 2 个字符后,屏幕上的文本是:"po" 。
因为第 3 个字符是 'i' ,屏幕上的文本被反转,变成 "op" 。
因为第 4 个字符是 'i' ,屏幕上的文本被反转,变成 "po" 。
输入第 5 个字符后,屏幕上的文本是:"pon" 。
输入第 6 个字符后,屏幕上的文本是:"pont" 。
输入第 7 个字符后,屏幕上的文本是:"ponte" 。
输入第 8 个字符后,屏幕上的文本是:"ponter" 。
因此,返回 "ponter" 。
提示:
* 1 <= s.length <= 100
* s 由小写英文字母组成
* s[0] != 'i'
"""
class Solution:
def finalString(self, s: str) -> str:
| 你的笔记本键盘存在故障,每当你在上面输入字符 'i' 时,它会反转你所写的字符串。而输入其他字符则可以正常工作。
给你一个下标从 0 开始的字符串 s ,请你用故障键盘依次输入每个字符。
返回最终笔记本屏幕上输出的字符串。
示例 1:
输入:s = "string"
输出:"rtsng"
解释:
输入第 1 个字符后,屏幕上的文本是:"s" 。
输入第 2 个字符后,屏幕上的文本是:"st" 。
输入第 3 个字符后,屏幕上的文本是:"str" 。
因为第 4 个字符是 'i' ,屏幕上的文本被反转,变成 "rts" 。
输入第 5 个字符后,屏幕上的文本是:"rtsn" 。
输入第 6 个字符后,屏幕上的文本是: "rtsng" 。
因此,返回 "rtsng" 。
示例 2:
输入:s = "poiinter"
输出:"ponter"
解释:
输入第 1 个字符后,屏幕上的文本是:"p" 。
输入第 2 个字符后,屏幕上的文本是:"po" 。
因为第 3 个字符是 'i' ,屏幕上的文本被反转,变成 "op" 。
因为第 4 个字符是 'i' ,屏幕上的文本被反转,变成 "po" 。
输入第 5 个字符后,屏幕上的文本是:"pon" 。
输入第 6 个字符后,屏幕上的文本是:"pont" 。
输入第 7 个字符后,屏幕上的文本是:"ponte" 。
输入第 8 个字符后,屏幕上的文本是:"ponter" 。
因此,返回 "ponter" 。
提示:
* 1 <= s.length <= 100
* s 由小写英文字母组成
* s[0] != 'i'
请完成下面的代码来解决上述问题:
```python
class Solution:
def finalString(self, s: str) -> str:
``` |
my_solution = Solution()
test_input = { "s": "string" }
assert my_solution.finalString(**test_input) == "rtsng"
test_input = { "s": "poiinter" }
assert my_solution.finalString(**test_input) == "ponter"
test_input = { "s": "goci" }
assert my_solution.finalString(**test_input) == "cog"
test_input = { "s": "ksi" }
assert my_solution.finalString(**test_input) == "sk"
test_input = { "s": "fii" }
assert my_solution.finalString(**test_input) == "f"
test_input = { "s": "qskyviiiii" }
assert my_solution.finalString(**test_input) == "vyksq"
test_input = { "s": "pft" }
assert my_solution.finalString(**test_input) == "pft"
test_input = { "s": "viwif" }
assert my_solution.finalString(**test_input) == "wvf"
test_input = { "s": "wiie" }
assert my_solution.finalString(**test_input) == "we"
test_input = { "s": "kiis" }
assert my_solution.finalString(**test_input) == "ks"
test_input = { "s": "xihbosxitx" }
assert my_solution.finalString(**test_input) == "xsobhxtx"
test_input = { "s": "uwioili" }
assert my_solution.finalString(**test_input) == "lwuo"
test_input = { "s": "aapziai" }
assert my_solution.finalString(**test_input) == "aaapz"
test_input = { "s": "pviist" }
assert my_solution.finalString(**test_input) == "pvst"
test_input = { "s": "miiuiei" }
assert my_solution.finalString(**test_input) == "emu"
test_input = { "s": "diiiiq" }
assert my_solution.finalString(**test_input) == "dq"
test_input = { "s": "eirov" }
assert my_solution.finalString(**test_input) == "erov"
test_input = { "s": "niiiiisiii" }
assert my_solution.finalString(**test_input) == "sn"
test_input = { "s": "siiuii" }
assert my_solution.finalString(**test_input) == "su"
test_input = { "s": "piijciivq" }
assert my_solution.finalString(**test_input) == "pjcvq"
test_input = { "s": "tidtwitik" }
assert my_solution.finalString(**test_input) == "ttdtwk"
test_input = { "s": "z" }
assert my_solution.finalString(**test_input) == "z"
test_input = { "s": "ffyuidnn" }
assert my_solution.finalString(**test_input) == "uyffdnn"
test_input = { "s": "xitiiinix" }
assert my_solution.finalString(**test_input) == "nxtx"
test_input = { "s": "ciiiuifab" }
assert my_solution.finalString(**test_input) == "ucfab"
test_input = { "s": "x" }
assert my_solution.finalString(**test_input) == "x"
test_input = { "s": "v" }
assert my_solution.finalString(**test_input) == "v"
test_input = { "s": "liinii" }
assert my_solution.finalString(**test_input) == "ln"
test_input = { "s": "ziii" }
assert my_solution.finalString(**test_input) == "z"
test_input = { "s": "ei" }
assert my_solution.finalString(**test_input) == "e"
test_input = { "s": "tidiiiii" }
assert my_solution.finalString(**test_input) == "dt"
test_input = { "s": "krjiqjii" }
assert my_solution.finalString(**test_input) == "jrkqj"
test_input = { "s": "mxczii" }
assert my_solution.finalString(**test_input) == "mxcz"
test_input = { "s": "bz" }
assert my_solution.finalString(**test_input) == "bz"
test_input = { "s": "zbwri" }
assert my_solution.finalString(**test_input) == "rwbz"
test_input = { "s": "biiq" }
assert my_solution.finalString(**test_input) == "bq"
test_input = { "s": "mmiiliir" }
assert my_solution.finalString(**test_input) == "mmlr"
test_input = { "s": "plibeici" }
assert my_solution.finalString(**test_input) == "clpbe"
test_input = { "s": "cii" }
assert my_solution.finalString(**test_input) == "c"
test_input = { "s": "wiilg" }
assert my_solution.finalString(**test_input) == "wlg"
test_input = { "s": "cdidi" }
assert my_solution.finalString(**test_input) == "dcd"
test_input = { "s": "fsq" }
assert my_solution.finalString(**test_input) == "fsq"
test_input = { "s": "hkjciaiii" }
assert my_solution.finalString(**test_input) == "ahkjc"
test_input = { "s": "l" }
assert my_solution.finalString(**test_input) == "l"
test_input = { "s": "vilcoizi" }
assert my_solution.finalString(**test_input) == "zvlco"
test_input = { "s": "tgigivipx" }
assert my_solution.finalString(**test_input) == "vgtgpx"
test_input = { "s": "ri" }
assert my_solution.finalString(**test_input) == "r"
test_input = { "s": "kficiiioiy" }
assert my_solution.finalString(**test_input) == "ofkcy"
test_input = { "s": "o" }
assert my_solution.finalString(**test_input) == "o"
test_input = { "s": "piifwiiit" }
assert my_solution.finalString(**test_input) == "wfpt"
test_input = { "s": "sifsiui" }
assert my_solution.finalString(**test_input) == "usfs"
test_input = { "s": "sxiuiiiii" }
assert my_solution.finalString(**test_input) == "usx"
test_input = { "s": "tiiiihiw" }
assert my_solution.finalString(**test_input) == "htw"
test_input = { "s": "ko" }
assert my_solution.finalString(**test_input) == "ko"
test_input = { "s": "gagi" }
assert my_solution.finalString(**test_input) == "gag"
test_input = { "s": "yyigiir" }
assert my_solution.finalString(**test_input) == "yygr"
test_input = { "s": "jimiiaci" }
assert my_solution.finalString(**test_input) == "camj"
test_input = { "s": "xiiiei" }
assert my_solution.finalString(**test_input) == "ex"
test_input = { "s": "hwi" }
assert my_solution.finalString(**test_input) == "wh"
test_input = { "s": "ji" }
assert my_solution.finalString(**test_input) == "j"
test_input = { "s": "heii" }
assert my_solution.finalString(**test_input) == "he"
test_input = { "s": "zitjcq" }
assert my_solution.finalString(**test_input) == "ztjcq"
test_input = { "s": "upmipaw" }
assert my_solution.finalString(**test_input) == "mpupaw"
test_input = { "s": "fiixkgp" }
assert my_solution.finalString(**test_input) == "fxkgp"
test_input = { "s": "ldr" }
assert my_solution.finalString(**test_input) == "ldr"
test_input = { "s": "kiboiithi" }
assert my_solution.finalString(**test_input) == "htobk"
test_input = { "s": "svcii" }
assert my_solution.finalString(**test_input) == "svc"
test_input = { "s": "d" }
assert my_solution.finalString(**test_input) == "d"
test_input = { "s": "edgijwiua" }
assert my_solution.finalString(**test_input) == "wjedgua"
test_input = { "s": "wiidqoiwov" }
assert my_solution.finalString(**test_input) == "oqdwwov"
test_input = { "s": "zimxiiqqi" }
assert my_solution.finalString(**test_input) == "qqxmz"
test_input = { "s": "githpgiini" }
assert my_solution.finalString(**test_input) == "ngphtg"
test_input = { "s": "fy" }
assert my_solution.finalString(**test_input) == "fy"
test_input = { "s": "hesi" }
assert my_solution.finalString(**test_input) == "seh"
test_input = { "s": "eiiii" }
assert my_solution.finalString(**test_input) == "e"
test_input = { "s": "be" }
assert my_solution.finalString(**test_input) == "be"
test_input = { "s": "rpi" }
assert my_solution.finalString(**test_input) == "pr"
test_input = { "s": "mi" }
assert my_solution.finalString(**test_input) == "m"
test_input = { "s": "wiiiiii" }
assert my_solution.finalString(**test_input) == "w"
test_input = { "s": "rbiiiii" }
assert my_solution.finalString(**test_input) == "br"
test_input = { "s": "diiii" }
assert my_solution.finalString(**test_input) == "d"
test_input = { "s": "poiiifl" }
assert my_solution.finalString(**test_input) == "opfl"
test_input = { "s": "loifiicii" }
assert my_solution.finalString(**test_input) == "olfc"
test_input = { "s": "bii" }
assert my_solution.finalString(**test_input) == "b"
test_input = { "s": "nirii" }
assert my_solution.finalString(**test_input) == "nr"
test_input = { "s": "wiigipio" }
assert my_solution.finalString(**test_input) == "pwgo"
test_input = { "s": "gimliibin" }
assert my_solution.finalString(**test_input) == "blmgn"
test_input = { "s": "zi" }
assert my_solution.finalString(**test_input) == "z"
test_input = { "s": "tjn" }
assert my_solution.finalString(**test_input) == "tjn"
test_input = { "s": "ly" }
assert my_solution.finalString(**test_input) == "ly"
test_input = { "s": "sqzviyiimi" }
assert my_solution.finalString(**test_input) == "mysqzv"
test_input = { "s": "jhmaxm" }
assert my_solution.finalString(**test_input) == "jhmaxm"
test_input = { "s": "py" }
assert my_solution.finalString(**test_input) == "py"
test_input = { "s": "yyilwiib" }
assert my_solution.finalString(**test_input) == "yylwb"
test_input = { "s": "ryjiilj" }
assert my_solution.finalString(**test_input) == "ryjlj"
test_input = { "s": "tnokpgfii" }
assert my_solution.finalString(**test_input) == "tnokpgf"
test_input = { "s": "niihiliiv" }
assert my_solution.finalString(**test_input) == "hnlv"
test_input = { "s": "gvhms" }
assert my_solution.finalString(**test_input) == "gvhms"
test_input = { "s": "yg" }
assert my_solution.finalString(**test_input) == "yg"
test_input = { "s": "eiiiuizgi" }
assert my_solution.finalString(**test_input) == "gzeu" | 1,691,289,000 |
weekly-contest-357-check-if-it-is-possible-to-split-array | https://leetcode.com/problems/check-if-it-is-possible-to-split-array | check-if-it-is-possible-to-split-array | {
"questionId": "2916",
"questionFrontendId": "2811",
"title": "Check if it is Possible to Split Array",
"titleSlug": "check-if-it-is-possible-to-split-array",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 425,
"dislikes": 84,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 的数组 nums 和一个整数 m 。请你判断能否执行一系列操作,将数组拆分成 n 个 非空 数组。
在每一步操作中,你可以选择一个 长度至少为 2 的现有数组(之前步骤的结果) 并将其拆分成 2 个子数组,而得到的 每个 子数组,至少 需要满足以下条件之一:
* 子数组的长度为 1 ,或者
* 子数组元素之和 大于或等于 m 。
如果你可以将给定数组拆分成 n 个满足要求的数组,返回 true ;否则,返回 false 。
注意:子数组是数组中的一个连续非空元素序列。
示例 1:
输入:nums = [2, 2, 1], m = 4
输出:true
解释:
第 1 步,将数组 nums 拆分成 [2, 2] 和 [1] 。
第 2 步,将数组 [2, 2] 拆分成 [2] 和 [2] 。
因此,答案为 true 。
示例 2:
输入:nums = [2, 1, 3], m = 5
输出:false
解释:
存在两种不同的拆分方法:
第 1 种,将数组 nums 拆分成 [2, 1] 和 [3] 。
第 2 种,将数组 nums 拆分成 [2] 和 [1, 3] 。
然而,这两种方法都不满足题意。因此,答案为 false 。
示例 3:
输入:nums = [2, 3, 3, 2, 3], m = 6
输出:true
解释:
第 1 步,将数组 nums 拆分成 [2, 3, 3, 2] 和 [3] 。
第 2 步,将数组 [2, 3, 3, 2] 拆分成 [2, 3, 3] 和 [2] 。
第 3 步,将数组 [2, 3, 3] 拆分成 [2] 和 [3, 3] 。
第 4 步,将数组 [3, 3] 拆分成 [3] 和 [3] 。
因此,答案为 true 。
提示:
* 1 <= n == nums.length <= 100
* 1 <= nums[i] <= 100
* 1 <= m <= 200
"""
class Solution:
def canSplitArray(self, nums: List[int], m: int) -> bool:
| 给你一个长度为 n 的数组 nums 和一个整数 m 。请你判断能否执行一系列操作,将数组拆分成 n 个 非空 数组。
在每一步操作中,你可以选择一个 长度至少为 2 的现有数组(之前步骤的结果) 并将其拆分成 2 个子数组,而得到的 每个 子数组,至少 需要满足以下条件之一:
* 子数组的长度为 1 ,或者
* 子数组元素之和 大于或等于 m 。
如果你可以将给定数组拆分成 n 个满足要求的数组,返回 true ;否则,返回 false 。
注意:子数组是数组中的一个连续非空元素序列。
示例 1:
输入:nums = [2, 2, 1], m = 4
输出:true
解释:
第 1 步,将数组 nums 拆分成 [2, 2] 和 [1] 。
第 2 步,将数组 [2, 2] 拆分成 [2] 和 [2] 。
因此,答案为 true 。
示例 2:
输入:nums = [2, 1, 3], m = 5
输出:false
解释:
存在两种不同的拆分方法:
第 1 种,将数组 nums 拆分成 [2, 1] 和 [3] 。
第 2 种,将数组 nums 拆分成 [2] 和 [1, 3] 。
然而,这两种方法都不满足题意。因此,答案为 false 。
示例 3:
输入:nums = [2, 3, 3, 2, 3], m = 6
输出:true
解释:
第 1 步,将数组 nums 拆分成 [2, 3, 3, 2] 和 [3] 。
第 2 步,将数组 [2, 3, 3, 2] 拆分成 [2, 3, 3] 和 [2] 。
第 3 步,将数组 [2, 3, 3] 拆分成 [2] 和 [3, 3] 。
第 4 步,将数组 [3, 3] 拆分成 [3] 和 [3] 。
因此,答案为 true 。
提示:
* 1 <= n == nums.length <= 100
* 1 <= nums[i] <= 100
* 1 <= m <= 200
请完成下面的代码来解决上述问题:
```python
class Solution:
def canSplitArray(self, nums: List[int], m: int) -> bool:
``` |
my_solution = Solution()
test_input = { "nums": [2, 2, 1], "m": 4 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2, 3, 3, 2, 3], "m": 6 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1], "m": 1 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2, 1, 3], "m": 5 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 1, 1], "m": 3 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2], "m": 1 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2, 1, 2], "m": 4 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 2, 1], "m": 5 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 2, 2], "m": 5 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3, 1, 2], "m": 5 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3], "m": 1 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 3, 1], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 3, 2], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 1, 1], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 2, 1], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 2, 3], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 1, 1], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3, 1, 3], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [7], "m": 5 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [4], "m": 7 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [9], "m": 7 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2], "m": 8 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 2, 2], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [4], "m": 8 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [10], "m": 11 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [6], "m": 12 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 2, 3], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3, 3, 6], "m": 10 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2], "m": 14 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [5, 3, 6], "m": 10 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3], "m": 18 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [4, 6, 5], "m": 12 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [9, 7], "m": 1 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 2], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [9, 1, 7], "m": 14 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [8, 2, 4], "m": 16 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 3], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2, 1], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [9, 5, 7], "m": 20 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 3], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 2, 1, 1], "m": 4 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 2, 2, 5], "m": 8 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [4, 4, 4, 2], "m": 9 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3, 2], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [8, 1, 2, 5], "m": 10 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [10, 2, 9, 3], "m": 14 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3, 3], "m": 2 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [10, 4, 8, 6], "m": 16 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [14, 1, 1, 15], "m": 17 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [6, 11, 2, 12], "m": 18 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [14, 3, 12, 3], "m": 18 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 1], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 2], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 1, 2, 2, 1], "m": 5 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 2, 1, 3, 1], "m": 5 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 3], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 1, 1, 3, 1], "m": 5 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 2], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2, 3], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2, 10], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 1], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 3], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [7, 9], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 3, 1, 5, 1], "m": 7 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 4, 2, 4, 2], "m": 8 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 9, 2, 3, 2], "m": 12 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 3], "m": 4 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [2, 2], "m": 4 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [4, 4, 4, 7, 5], "m": 13 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [5, 2, 6, 5, 4], "m": 13 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 3], "m": 4 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 1], "m": 4 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [13, 2, 13, 4, 11], "m": 18 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [5, 6, 10, 7, 4], "m": 19 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [16, 1, 12, 6, 7], "m": 19 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3, 3], "m": 4 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [9, 9, 8, 10, 8], "m": 20 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [9, 4], "m": 5 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [10, 2], "m": 5 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [16, 2, 2, 16, 2], "m": 20 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [6, 2], "m": 7 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 3, 1, 2, 1, 4], "m": 6 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [5, 1], "m": 8 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 4, 4, 2, 1, 5], "m": 9 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [2, 5], "m": 12 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [10, 9], "m": 15 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [8, 4], "m": 18 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [3, 4, 3, 5, 2, 1], "m": 9 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [3, 7, 2, 8, 2, 4], "m": 11 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [8, 1], "m": 19 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 1, 2], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [5, 1, 1, 9, 1, 5], "m": 11 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 1, 3], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [6, 4, 7, 2, 5, 4], "m": 12 }
assert my_solution.canSplitArray(**test_input) == False
test_input = { "nums": [1, 2, 1], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True
test_input = { "nums": [1, 3, 1], "m": 3 }
assert my_solution.canSplitArray(**test_input) == True | 1,691,289,000 |
weekly-contest-357-find-the-safest-path-in-a-grid | https://leetcode.com/problems/find-the-safest-path-in-a-grid | find-the-safest-path-in-a-grid | {
"questionId": "2914",
"questionFrontendId": "2812",
"title": "Find the Safest Path in a Grid",
"titleSlug": "find-the-safest-path-in-a-grid",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 706,
"dislikes": 68,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始、大小为 n x n 的二维矩阵 grid ,其中 (r, c) 表示:
* 如果 grid[r][c] = 1 ,则表示一个存在小偷的单元格
* 如果 grid[r][c] = 0 ,则表示一个空单元格
你最开始位于单元格 (0, 0) 。在一步移动中,你可以移动到矩阵中的任一相邻单元格,包括存在小偷的单元格。
矩阵中路径的 安全系数 定义为:从路径中任一单元格到矩阵中任一小偷所在单元格的 最小 曼哈顿距离。
返回所有通向单元格 (n - 1, n - 1) 的路径中的 最大安全系数 。
单元格 (r, c) 的某个 相邻 单元格,是指在矩阵中存在的 (r, c + 1)、(r, c - 1)、(r + 1, c) 和 (r - 1, c) 之一。
两个单元格 (a, b) 和 (x, y) 之间的 曼哈顿距离 等于 | a - x | + | b - y | ,其中 |val| 表示 val 的绝对值。
示例 1:
[https://assets.leetcode.com/uploads/2023/07/02/example1.png]
输入:grid = [[1,0,0],[0,0,0],[0,0,1]]
输出:0
解释:从 (0, 0) 到 (n - 1, n - 1) 的每条路径都经过存在小偷的单元格 (0, 0) 和 (n - 1, n - 1) 。
示例 2:
[https://assets.leetcode.com/uploads/2023/07/02/example2.png]
输入:grid = [[0,0,1],[0,0,0],[0,0,0]]
输出:2
解释:
上图所示路径的安全系数为 2:
- 该路径上距离小偷所在单元格(0,2)最近的单元格是(0,0)。它们之间的曼哈顿距离为 | 0 - 0 | + | 0 - 2 | = 2 。
可以证明,不存在安全系数更高的其他路径。
示例 3:
[https://assets.leetcode.com/uploads/2023/07/02/example3.png]
输入:grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
输出:2
解释:
上图所示路径的安全系数为 2:
- 该路径上距离小偷所在单元格(0,3)最近的单元格是(1,2)。它们之间的曼哈顿距离为 | 0 - 1 | + | 3 - 2 | = 2 。
- 该路径上距离小偷所在单元格(3,0)最近的单元格是(3,2)。它们之间的曼哈顿距离为 | 3 - 3 | + | 0 - 2 | = 2 。
可以证明,不存在安全系数更高的其他路径。
提示:
* 1 <= grid.length == n <= 400
* grid[i].length == n
* grid[i][j] 为 0 或 1
* grid 至少存在一个小偷
"""
class Solution:
def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
| 给你一个下标从 0 开始、大小为 n x n 的二维矩阵 grid ,其中 (r, c) 表示:
* 如果 grid[r][c] = 1 ,则表示一个存在小偷的单元格
* 如果 grid[r][c] = 0 ,则表示一个空单元格
你最开始位于单元格 (0, 0) 。在一步移动中,你可以移动到矩阵中的任一相邻单元格,包括存在小偷的单元格。
矩阵中路径的 安全系数 定义为:从路径中任一单元格到矩阵中任一小偷所在单元格的 最小 曼哈顿距离。
返回所有通向单元格 (n - 1, n - 1) 的路径中的 最大安全系数 。
单元格 (r, c) 的某个 相邻 单元格,是指在矩阵中存在的 (r, c + 1)、(r, c - 1)、(r + 1, c) 和 (r - 1, c) 之一。
两个单元格 (a, b) 和 (x, y) 之间的 曼哈顿距离 等于 | a - x | + | b - y | ,其中 |val| 表示 val 的绝对值。
示例 1:
[https://assets.leetcode.com/uploads/2023/07/02/example1.png]
输入:grid = [[1,0,0],[0,0,0],[0,0,1]]
输出:0
解释:从 (0, 0) 到 (n - 1, n - 1) 的每条路径都经过存在小偷的单元格 (0, 0) 和 (n - 1, n - 1) 。
示例 2:
[https://assets.leetcode.com/uploads/2023/07/02/example2.png]
输入:grid = [[0,0,1],[0,0,0],[0,0,0]]
输出:2
解释:
上图所示路径的安全系数为 2:
- 该路径上距离小偷所在单元格(0,2)最近的单元格是(0,0)。它们之间的曼哈顿距离为 | 0 - 0 | + | 0 - 2 | = 2 。
可以证明,不存在安全系数更高的其他路径。
示例 3:
[https://assets.leetcode.com/uploads/2023/07/02/example3.png]
输入:grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
输出:2
解释:
上图所示路径的安全系数为 2:
- 该路径上距离小偷所在单元格(0,3)最近的单元格是(1,2)。它们之间的曼哈顿距离为 | 0 - 1 | + | 3 - 2 | = 2 。
- 该路径上距离小偷所在单元格(3,0)最近的单元格是(3,2)。它们之间的曼哈顿距离为 | 3 - 3 | + | 0 - 2 | = 2 。
可以证明,不存在安全系数更高的其他路径。
提示:
* 1 <= grid.length == n <= 400
* grid[i].length == n
* grid[i][j] 为 0 或 1
* grid 至少存在一个小偷
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "grid": [[1,0,0],[0,0,0],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[0,0,1],[0,0,0],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 2
test_input = { "grid": [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 2
test_input = { "grid": [[1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1],[1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,1],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,1,0],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,1],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[1,0,0],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1],[1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,1],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,1,0],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,1],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,1],[0,0,0],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1],[0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,1],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,1,0],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[0,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[0,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,1],[0,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,0],[1,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,0],[1,1,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,0],[1,0,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,0],[1,0,0]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0
test_input = { "grid": [[1,1,0],[1,0,0],[0,1,1]] }
assert my_solution.maximumSafenessFactor(**test_input) == 0 | 1,691,289,000 |
weekly-contest-357-maximum-elegance-of-a-k-length-subsequence | https://leetcode.com/problems/maximum-elegance-of-a-k-length-subsequence | maximum-elegance-of-a-k-length-subsequence | {
"questionId": "2894",
"questionFrontendId": "2813",
"title": "Maximum Elegance of a K-Length Subsequence",
"titleSlug": "maximum-elegance-of-a-k-length-subsequence",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 270,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
给你一个长度为 n 的二维整数数组 items 和一个整数 k 。
items[i] = [profiti, categoryi],其中 profiti 和 categoryi 分别表示第 i 个项目的利润和类别。
现定义 items 的 子序列 的 优雅度 可以用 total_profit + distinct_categories2 计算,其中 total_profit 是子序列中所有项目的利润总和,distinct_categories 是所选子序列所含的所有类别中不同类别的数量。
你的任务是从 items 所有长度为 k 的子序列中,找出 最大优雅度 。
用整数形式表示并返回 items 中所有长度恰好为 k 的子序列的最大优雅度。
注意:数组的子序列是经由原数组删除一些元素(可能不删除)而产生的新数组,且删除不改变其余元素相对顺序。
示例 1:
输入:items = [[3,2],[5,1],[10,1]], k = 2
输出:17
解释:
在这个例子中,我们需要选出长度为 2 的子序列。
其中一种方案是 items[0] = [3,2] 和 items[2] = [10,1] 。
子序列的总利润为 3 + 10 = 13 ,子序列包含 2 种不同类别 [2,1] 。
因此,优雅度为 13 + 22 = 17 ,可以证明 17 是可以获得的最大优雅度。
示例 2:
输入:items = [[3,1],[3,1],[2,2],[5,3]], k = 3
输出:19
解释:
在这个例子中,我们需要选出长度为 3 的子序列。
其中一种方案是 items[0] = [3,1] ,items[2] = [2,2] 和 items[3] = [5,3] 。
子序列的总利润为 3 + 2 + 5 = 10 ,子序列包含 3 种不同类别 [1, 2, 3] 。
因此,优雅度为 10 + 32 = 19 ,可以证明 19 是可以获得的最大优雅度。
示例 3:
输入:items = [[1,1],[2,1],[3,1]], k = 3
输出:7
解释:
在这个例子中,我们需要选出长度为 3 的子序列。
我们需要选中所有项目。
子序列的总利润为 1 + 2 + 3 = 6,子序列包含 1 种不同类别 [1] 。
因此,最大优雅度为 6 + 12 = 7 。
提示:
* 1 <= items.length == n <= 105
* items[i].length == 2
* items[i][0] == profiti
* items[i][1] == categoryi
* 1 <= profiti <= 109
* 1 <= categoryi <= n
* 1 <= k <= n
"""
class Solution:
def findMaximumElegance(self, items: List[List[int]], k: int) -> int:
| 给你一个长度为 n 的二维整数数组 items 和一个整数 k 。
items[i] = [profiti, categoryi],其中 profiti 和 categoryi 分别表示第 i 个项目的利润和类别。
现定义 items 的 子序列 的 优雅度 可以用 total_profit + distinct_categories2 计算,其中 total_profit 是子序列中所有项目的利润总和,distinct_categories 是所选子序列所含的所有类别中不同类别的数量。
你的任务是从 items 所有长度为 k 的子序列中,找出 最大优雅度 。
用整数形式表示并返回 items 中所有长度恰好为 k 的子序列的最大优雅度。
注意:数组的子序列是经由原数组删除一些元素(可能不删除)而产生的新数组,且删除不改变其余元素相对顺序。
示例 1:
输入:items = [[3,2],[5,1],[10,1]], k = 2
输出:17
解释:
在这个例子中,我们需要选出长度为 2 的子序列。
其中一种方案是 items[0] = [3,2] 和 items[2] = [10,1] 。
子序列的总利润为 3 + 10 = 13 ,子序列包含 2 种不同类别 [2,1] 。
因此,优雅度为 13 + 22 = 17 ,可以证明 17 是可以获得的最大优雅度。
示例 2:
输入:items = [[3,1],[3,1],[2,2],[5,3]], k = 3
输出:19
解释:
在这个例子中,我们需要选出长度为 3 的子序列。
其中一种方案是 items[0] = [3,1] ,items[2] = [2,2] 和 items[3] = [5,3] 。
子序列的总利润为 3 + 2 + 5 = 10 ,子序列包含 3 种不同类别 [1, 2, 3] 。
因此,优雅度为 10 + 32 = 19 ,可以证明 19 是可以获得的最大优雅度。
示例 3:
输入:items = [[1,1],[2,1],[3,1]], k = 3
输出:7
解释:
在这个例子中,我们需要选出长度为 3 的子序列。
我们需要选中所有项目。
子序列的总利润为 1 + 2 + 3 = 6,子序列包含 1 种不同类别 [1] 。
因此,最大优雅度为 6 + 12 = 7 。
提示:
* 1 <= items.length == n <= 105
* items[i].length == 2
* items[i][0] == profiti
* items[i][1] == categoryi
* 1 <= profiti <= 109
* 1 <= categoryi <= n
* 1 <= k <= n
请完成下面的代码来解决上述问题:
```python
class Solution:
def findMaximumElegance(self, items: List[List[int]], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "items": [[3,2],[5,1],[10,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[3,1],[3,1],[2,2],[5,3]], "k": 3 }
assert my_solution.findMaximumElegance(**test_input) == 19
test_input = { "items": [[1,1],[2,1],[3,1]], "k": 3 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[1,1],[1,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 6
test_input = { "items": [[1,1],[4,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 5
test_input = { "items": [[1,1],[4,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 6
test_input = { "items": [[1,1],[6,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[1,1],[6,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[1,1],[8,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 13
test_input = { "items": [[1,2],[6,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[1,2],[10,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[2,1],[6,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[2,1],[7,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[2,1],[9,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[2,2],[2,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 3
test_input = { "items": [[2,2],[2,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 5
test_input = { "items": [[2,2],[3,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[2,2],[6,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 12
test_input = { "items": [[3,1],[1,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[3,1],[9,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 13
test_input = { "items": [[3,1],[9,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[3,1],[10,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[3,2],[3,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 4
test_input = { "items": [[3,2],[5,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 6
test_input = { "items": [[3,2],[10,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[3,2],[10,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 14
test_input = { "items": [[4,1],[7,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 15
test_input = { "items": [[4,1],[9,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[4,2],[2,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[4,2],[3,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[4,2],[5,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 6
test_input = { "items": [[4,2],[7,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[4,2],[8,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 16
test_input = { "items": [[4,2],[10,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 18
test_input = { "items": [[5,1],[4,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 6
test_input = { "items": [[5,1],[8,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 14
test_input = { "items": [[5,1],[8,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[5,1],[9,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[5,2],[2,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 6
test_input = { "items": [[5,2],[4,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[5,2],[5,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 14
test_input = { "items": [[6,1],[1,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[6,1],[4,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[6,1],[7,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[6,1],[7,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[6,1],[8,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 15
test_input = { "items": [[6,1],[8,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 18
test_input = { "items": [[6,1],[9,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[6,2],[2,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[6,2],[4,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[6,2],[5,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[6,2],[6,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 7
test_input = { "items": [[6,2],[6,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 13
test_input = { "items": [[6,2],[7,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[6,2],[7,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[6,2],[8,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 15
test_input = { "items": [[6,2],[10,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[7,1],[1,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[7,1],[3,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[7,1],[6,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 14
test_input = { "items": [[7,2],[5,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 16
test_input = { "items": [[7,2],[5,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 8
test_input = { "items": [[7,2],[7,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 15
test_input = { "items": [[7,2],[10,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 18
test_input = { "items": [[8,1],[2,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,1],[2,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,1],[4,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,1],[5,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,1],[6,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,1],[8,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[8,1],[9,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[8,1],[9,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 21
test_input = { "items": [[8,2],[1,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,2],[1,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,2],[2,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,2],[8,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 9
test_input = { "items": [[8,2],[9,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 21
test_input = { "items": [[8,2],[9,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 18
test_input = { "items": [[8,2],[10,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 22
test_input = { "items": [[9,1],[1,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[9,1],[3,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 16
test_input = { "items": [[9,1],[4,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 17
test_input = { "items": [[9,1],[6,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[9,1],[9,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[9,1],[10,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[9,2],[1,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[9,2],[2,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 15
test_input = { "items": [[9,2],[6,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 19
test_input = { "items": [[9,2],[6,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 16
test_input = { "items": [[9,2],[8,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 10
test_input = { "items": [[10,1],[2,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[10,1],[4,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[10,1],[5,1]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 16
test_input = { "items": [[10,1],[5,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 19
test_input = { "items": [[10,1],[7,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[10,2],[1,2]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[10,2],[4,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[10,2],[5,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[10,2],[7,1]], "k": 1 }
assert my_solution.findMaximumElegance(**test_input) == 11
test_input = { "items": [[10,2],[8,2]], "k": 2 }
assert my_solution.findMaximumElegance(**test_input) == 19 | 1,691,289,000 |
biweekly-contest-110-account-balance-after-rounded-purchase | https://leetcode.com/problems/account-balance-after-rounded-purchase | account-balance-after-rounded-purchase | {
"questionId": "2955",
"questionFrontendId": "2806",
"title": "Account Balance After Rounded Purchase",
"titleSlug": "account-balance-after-rounded-purchase",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 178,
"dislikes": 37,
"categoryTitle": "Algorithms"
} | """
一开始,你的银行账户里有 100 块钱。
给你一个整数purchaseAmount ,它表示你在一次购买中愿意支出的金额。
在一个商店里,你进行一次购买,实际支出的金额会向 最近 的 10 的 倍数 取整。换句话说,你实际会支付一个 非负 金额 roundedAmount ,满足 roundedAmount 是 10 的倍数且 abs(roundedAmount - purchaseAmount) 的值 最小 。
如果存在多于一个最接近的 10 的倍数,较大的倍数 是你的实际支出金额。
请你返回一个整数,表示你在愿意支出金额为 purchaseAmount 块钱的前提下,购买之后剩下的余额。
注意: 0 也是 10 的倍数。
示例 1:
输入:purchaseAmount = 9
输出:90
解释:这个例子中,最接近 9 的 10 的倍数是 10 。所以你的账户余额为 100 - 10 = 90 。
示例 2:
输入:purchaseAmount = 15
输出:80
解释:这个例子中,有 2 个最接近 15 的 10 的倍数:10 和 20,较大的数 20 是你的实际开销。
所以你的账户余额为 100 - 20 = 80 。
提示:
* 0 <= purchaseAmount <= 100
"""
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
| 一开始,你的银行账户里有 100 块钱。
给你一个整数purchaseAmount ,它表示你在一次购买中愿意支出的金额。
在一个商店里,你进行一次购买,实际支出的金额会向 最近 的 10 的 倍数 取整。换句话说,你实际会支付一个 非负 金额 roundedAmount ,满足 roundedAmount 是 10 的倍数且 abs(roundedAmount - purchaseAmount) 的值 最小 。
如果存在多于一个最接近的 10 的倍数,较大的倍数 是你的实际支出金额。
请你返回一个整数,表示你在愿意支出金额为 purchaseAmount 块钱的前提下,购买之后剩下的余额。
注意: 0 也是 10 的倍数。
示例 1:
输入:purchaseAmount = 9
输出:90
解释:这个例子中,最接近 9 的 10 的倍数是 10 。所以你的账户余额为 100 - 10 = 90 。
示例 2:
输入:purchaseAmount = 15
输出:80
解释:这个例子中,有 2 个最接近 15 的 10 的倍数:10 和 20,较大的数 20 是你的实际开销。
所以你的账户余额为 100 - 20 = 80 。
提示:
* 0 <= purchaseAmount <= 100
请完成下面的代码来解决上述问题:
```python
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
``` |
my_solution = Solution()
test_input = { "purchaseAmount": 9 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 15 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 10 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 11 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 12 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 13 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 14 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 16 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 17 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 18 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 19 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 1 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 100
test_input = { "purchaseAmount": 2 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 100
test_input = { "purchaseAmount": 3 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 100
test_input = { "purchaseAmount": 4 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 100
test_input = { "purchaseAmount": 5 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 6 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 7 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 8 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 90
test_input = { "purchaseAmount": 20 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 21 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 22 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 23 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 24 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 80
test_input = { "purchaseAmount": 25 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 26 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 27 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 28 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 29 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 30 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 31 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 32 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 33 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 34 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 70
test_input = { "purchaseAmount": 35 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 36 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 37 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 38 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 39 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 40 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 41 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 42 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 43 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 44 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 60
test_input = { "purchaseAmount": 45 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 46 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 47 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 48 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 49 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 50 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 51 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 52 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 53 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 54 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 50
test_input = { "purchaseAmount": 55 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 56 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 57 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 58 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 59 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 60 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 61 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 62 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 63 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 64 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 40
test_input = { "purchaseAmount": 65 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 66 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 67 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 68 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 69 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 70 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 71 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 72 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 73 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 74 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 30
test_input = { "purchaseAmount": 75 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 76 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 77 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 78 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 79 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 80 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 81 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 82 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 83 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 84 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 20
test_input = { "purchaseAmount": 85 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 86 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 87 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 88 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 89 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 90 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 91 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 92 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 93 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 94 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 10
test_input = { "purchaseAmount": 95 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 0
test_input = { "purchaseAmount": 96 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 0
test_input = { "purchaseAmount": 97 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 0
test_input = { "purchaseAmount": 98 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 0
test_input = { "purchaseAmount": 99 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 0
test_input = { "purchaseAmount": 100 }
assert my_solution.accountBalanceAfterPurchase(**test_input) == 0 | 1,691,245,800 |
biweekly-contest-110-insert-greatest-common-divisors-in-linked-list | https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list | insert-greatest-common-divisors-in-linked-list | {
"questionId": "2903",
"questionFrontendId": "2807",
"title": "Insert Greatest Common Divisors in Linked List",
"titleSlug": "insert-greatest-common-divisors-in-linked-list",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 390,
"dislikes": 12,
"categoryTitle": "Algorithms"
} | """
给你一个链表的头 head ,每个结点包含一个整数值。
在相邻结点之间,请你插入一个新的结点,结点值为这两个相邻结点值的 最大公约数 。
请你返回插入之后的链表。
两个数的 最大公约数 是可以被两个数字整除的最大正整数。
示例 1:
[https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png]
输入:head = [18,6,10,3]
输出:[18,6,6,2,10,1,3]
解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
- 18 和 6 的最大公约数为 6 ,插入第一和第二个结点之间。
- 6 和 10 的最大公约数为 2 ,插入第二和第三个结点之间。
- 10 和 3 的最大公约数为 1 ,插入第三和第四个结点之间。
所有相邻结点之间都插入完毕,返回链表。
示例 2:
[https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png]
输入:head = [7]
输出:[7]
解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
没有相邻结点,所以返回初始链表。
提示:
* 链表中结点数目在 [1, 5000] 之间。
* 1 <= Node.val <= 1000
"""
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]:
| 给你一个链表的头 head ,每个结点包含一个整数值。
在相邻结点之间,请你插入一个新的结点,结点值为这两个相邻结点值的 最大公约数 。
请你返回插入之后的链表。
两个数的 最大公约数 是可以被两个数字整除的最大正整数。
示例 1:
[https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png]
输入:head = [18,6,10,3]
输出:[18,6,6,2,10,1,3]
解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
- 18 和 6 的最大公约数为 6 ,插入第一和第二个结点之间。
- 6 和 10 的最大公约数为 2 ,插入第二和第三个结点之间。
- 10 和 3 的最大公约数为 1 ,插入第三和第四个结点之间。
所有相邻结点之间都插入完毕,返回链表。
示例 2:
[https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png]
输入:head = [7]
输出:[7]
解释:第一幅图是一开始的链表,第二幅图是插入新结点后的图(蓝色结点为新插入结点)。
没有相邻结点,所以返回初始链表。
提示:
* 链表中结点数目在 [1, 5000] 之间。
* 1 <= Node.val <= 1000
请完成下面的代码来解决上述问题:
```python
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]:
``` |
my_solution = Solution()
_f1 = lambda lst: ListNode(lst[0], _f1(lst[1:])) if lst else None
_f2 = lambda node: [node.val] + _f2(node.next) if node else []
test_input = { "head": _f1([18,6,10,3]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [18,6,6,2,10,1,3]
test_input = { "head": _f1([7]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [7]
test_input = { "head": _f1([19]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [19]
test_input = { "head": _f1([2]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [2]
test_input = { "head": _f1([13]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [13]
test_input = { "head": _f1([8]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [8]
test_input = { "head": _f1([1]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [1]
test_input = { "head": _f1([3]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [3]
test_input = { "head": _f1([20]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [20]
test_input = { "head": _f1([14]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [14]
test_input = { "head": _f1([16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [16]
test_input = { "head": _f1([12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [12]
test_input = { "head": _f1([9]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [9]
test_input = { "head": _f1([5]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [5]
test_input = { "head": _f1([4]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [4]
test_input = { "head": _f1([18]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [18]
test_input = { "head": _f1([11]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [11]
test_input = { "head": _f1([17]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [17]
test_input = { "head": _f1([15]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15]
test_input = { "head": _f1([11,13]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [11,1,13]
test_input = { "head": _f1([7,13]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [7,1,13]
test_input = { "head": _f1([18,17]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [18,1,17]
test_input = { "head": _f1([15,17]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,1,17]
test_input = { "head": _f1([13,8]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [13,1,8]
test_input = { "head": _f1([12,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [12,4,16]
test_input = { "head": _f1([12,8]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [12,4,8]
test_input = { "head": _f1([18,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [18,2,16]
test_input = { "head": _f1([16,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [16,16,16]
test_input = { "head": _f1([6,12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [6,6,12]
test_input = { "head": _f1([9,6]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [9,3,6]
test_input = { "head": _f1([2,17]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [2,1,17]
test_input = { "head": _f1([7,5]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [7,1,5]
test_input = { "head": _f1([15,6]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,3,6]
test_input = { "head": _f1([3,14]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [3,1,14]
test_input = { "head": _f1([6,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [6,2,16]
test_input = { "head": _f1([3,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [3,1,16]
test_input = { "head": _f1([11,9]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [11,1,9]
test_input = { "head": _f1([4,15]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [4,1,15]
test_input = { "head": _f1([16,2]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [16,2,2]
test_input = { "head": _f1([12,7]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [12,1,7]
test_input = { "head": _f1([7,9]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [7,1,9]
test_input = { "head": _f1([7,3]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [7,1,3]
test_input = { "head": _f1([8,4]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [8,4,4]
test_input = { "head": _f1([4,11]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [4,1,11]
test_input = { "head": _f1([6,15]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [6,3,15]
test_input = { "head": _f1([9,7]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [9,1,7]
test_input = { "head": _f1([19,4]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [19,1,4]
test_input = { "head": _f1([17,6,18]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [17,1,6,6,18]
test_input = { "head": _f1([10,8,3]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [10,2,8,1,3]
test_input = { "head": _f1([11,4,10]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [11,1,4,2,10]
test_input = { "head": _f1([5,3,13]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [5,1,3,1,13]
test_input = { "head": _f1([2,1,15]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [2,1,1,1,15]
test_input = { "head": _f1([17,13,9]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [17,1,13,1,9]
test_input = { "head": _f1([2,15,12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [2,1,15,3,12]
test_input = { "head": _f1([16,12,13]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [16,4,12,1,13]
test_input = { "head": _f1([1,12,19]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [1,1,12,1,19]
test_input = { "head": _f1([4,3,3]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [4,1,3,3,3]
test_input = { "head": _f1([15,11,3]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,1,11,1,3]
test_input = { "head": _f1([15,18,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,3,18,2,16]
test_input = { "head": _f1([20,6,7]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [20,2,6,1,7]
test_input = { "head": _f1([9,4,7]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [9,1,4,1,7]
test_input = { "head": _f1([20,11,6]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [20,1,11,1,6]
test_input = { "head": _f1([11,8,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [11,1,8,8,16]
test_input = { "head": _f1([1,4,12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [1,1,4,4,12]
test_input = { "head": _f1([18,12,19]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [18,6,12,1,19]
test_input = { "head": _f1([8,11,5]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [8,1,11,1,5]
test_input = { "head": _f1([6,10,6]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [6,2,10,2,6]
test_input = { "head": _f1([3,10,16]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [3,1,10,2,16]
test_input = { "head": _f1([15,6,15]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,3,6,3,15]
test_input = { "head": _f1([9,5,1]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [9,1,5,1,1]
test_input = { "head": _f1([15,15,18]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,15,15,3,18]
test_input = { "head": _f1([3,16,13]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [3,1,16,1,13]
test_input = { "head": _f1([9,3,6]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [9,3,3,3,6]
test_input = { "head": _f1([4,14,9]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [4,2,14,1,9]
test_input = { "head": _f1([15,2,20]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,1,2,2,20]
test_input = { "head": _f1([13,7,19]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [13,1,7,1,19]
test_input = { "head": _f1([19,19,12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [19,19,19,1,12]
test_input = { "head": _f1([8,6,1]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [8,2,6,1,1]
test_input = { "head": _f1([19,10,19]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [19,1,10,1,19]
test_input = { "head": _f1([6,9,17]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [6,3,9,1,17]
test_input = { "head": _f1([3,19,8]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [3,1,19,1,8]
test_input = { "head": _f1([12,6,9]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [12,6,6,3,9]
test_input = { "head": _f1([14,16,19]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [14,2,16,1,19]
test_input = { "head": _f1([12,14,16,12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [12,2,14,2,16,4,12]
test_input = { "head": _f1([20,13,19,12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [20,1,13,1,19,1,12]
test_input = { "head": _f1([14,13,8,8]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [14,1,13,1,8,8,8]
test_input = { "head": _f1([13,3,3,5]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [13,1,3,3,3,1,5]
test_input = { "head": _f1([11,7,15,7]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [11,1,7,1,15,1,7]
test_input = { "head": _f1([11,7,5,1]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [11,1,7,1,5,1,1]
test_input = { "head": _f1([8,7,15,13]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [8,1,7,1,15,1,13]
test_input = { "head": _f1([1,19,3,19]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [1,1,19,1,3,1,19]
test_input = { "head": _f1([15,19,1,7]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [15,1,19,1,1,1,7]
test_input = { "head": _f1([13,20,9,1]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [13,1,20,1,9,1,1]
test_input = { "head": _f1([18,16,6,9]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [18,2,16,2,6,3,9]
test_input = { "head": _f1([16,6,13,11]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [16,2,6,1,13,1,11]
test_input = { "head": _f1([9,5,1,6]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [9,1,5,1,1,1,6]
test_input = { "head": _f1([3,15,10,12]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [3,3,15,5,10,2,12]
test_input = { "head": _f1([1,9,19,15]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [1,1,9,1,19,1,15]
test_input = { "head": _f1([14,14,19,2]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [14,14,14,1,19,1,2]
test_input = { "head": _f1([5,13,5,4]) }
assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [5,1,13,1,5,1,4] | 1,691,245,800 |
biweekly-contest-110-minimum-seconds-to-equalize-a-circular-array | https://leetcode.com/problems/minimum-seconds-to-equalize-a-circular-array | minimum-seconds-to-equalize-a-circular-array | {
"questionId": "2920",
"questionFrontendId": "2808",
"title": "Minimum Seconds to Equalize a Circular Array",
"titleSlug": "minimum-seconds-to-equalize-a-circular-array",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 481,
"dislikes": 25,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始长度为 n 的数组 nums 。
每一秒,你可以对数组执行以下操作:
* 对于范围在 [0, n - 1] 内的每一个下标 i ,将 nums[i] 替换成 nums[i] ,nums[(i - 1 + n) % n] 或者 nums[(i + 1) % n] 三者之一。
注意,所有元素会被同时替换。
请你返回将数组 nums 中所有元素变成相等元素所需要的 最少 秒数。
示例 1:
输入:nums = [1,2,1,2]
输出:1
解释:我们可以在 1 秒内将数组变成相等元素:
- 第 1 秒,将每个位置的元素分别变为 [nums[3],nums[1],nums[3],nums[3]] 。变化后,nums = [2,2,2,2] 。
1 秒是将数组变成相等元素所需要的最少秒数。
示例 2:
输入:nums = [2,1,3,3,2]
输出:2
解释:我们可以在 2 秒内将数组变成相等元素:
- 第 1 秒,将每个位置的元素分别变为 [nums[0],nums[2],nums[2],nums[2],nums[3]] 。变化后,nums = [2,3,3,3,3] 。
- 第 2 秒,将每个位置的元素分别变为 [nums[1],nums[1],nums[2],nums[3],nums[4]] 。变化后,nums = [3,3,3,3,3] 。
2 秒是将数组变成相等元素所需要的最少秒数。
示例 3:
输入:nums = [5,5,5,5]
输出:0
解释:不需要执行任何操作,因为一开始数组中的元素已经全部相等。
提示:
* 1 <= n == nums.length <= 105
* 1 <= nums[i] <= 109
"""
class Solution:
def minimumSeconds(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始长度为 n 的数组 nums 。
每一秒,你可以对数组执行以下操作:
* 对于范围在 [0, n - 1] 内的每一个下标 i ,将 nums[i] 替换成 nums[i] ,nums[(i - 1 + n) % n] 或者 nums[(i + 1) % n] 三者之一。
注意,所有元素会被同时替换。
请你返回将数组 nums 中所有元素变成相等元素所需要的 最少 秒数。
示例 1:
输入:nums = [1,2,1,2]
输出:1
解释:我们可以在 1 秒内将数组变成相等元素:
- 第 1 秒,将每个位置的元素分别变为 [nums[3],nums[1],nums[3],nums[3]] 。变化后,nums = [2,2,2,2] 。
1 秒是将数组变成相等元素所需要的最少秒数。
示例 2:
输入:nums = [2,1,3,3,2]
输出:2
解释:我们可以在 2 秒内将数组变成相等元素:
- 第 1 秒,将每个位置的元素分别变为 [nums[0],nums[2],nums[2],nums[2],nums[3]] 。变化后,nums = [2,3,3,3,3] 。
- 第 2 秒,将每个位置的元素分别变为 [nums[1],nums[1],nums[2],nums[3],nums[4]] 。变化后,nums = [3,3,3,3,3] 。
2 秒是将数组变成相等元素所需要的最少秒数。
示例 3:
输入:nums = [5,5,5,5]
输出:0
解释:不需要执行任何操作,因为一开始数组中的元素已经全部相等。
提示:
* 1 <= n == nums.length <= 105
* 1 <= nums[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumSeconds(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,1,2] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [2,1,3,3,2] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [5,5,5,5] }
assert my_solution.minimumSeconds(**test_input) == 0
test_input = { "nums": [4,18] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [11,7] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [14,2] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [14,9] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [20,1] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [17,15] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [11,13] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [7,13] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [18,17] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [15,17] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [13,8] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [12,16] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [12,8] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [18,16] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [16,16] }
assert my_solution.minimumSeconds(**test_input) == 0
test_input = { "nums": [6,12] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [9,6] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [2,17] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [7,5] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [15,6] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [3,14] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [6,16] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [3,16] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [11,9] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [4,15] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [16,2] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [12,7] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [7,9] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [7,3] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [8,4] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [4,11] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [6,15] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [9,7] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [19,4] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [17,6] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [18,10] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [8,3] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [11,4,10] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [5,3,13] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [2,1,15] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [17,13,9] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [2,15,12] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [16,12,13] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [1,12,19] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [4,3,3] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [15,11,3] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [15,18,16] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [20,6,7] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [9,4,7] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [20,11,6] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [11,8,16] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [1,4,12] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [18,12,19] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [8,11,5] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [6,10,6] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [3,10,16] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [15,6,15] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [9,5,1] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [15,15,18] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [3,16,13] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [9,3,6] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [4,14,9] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [15,2,20] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [13,7,19] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [19,19,12] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [8,6,1] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [19,10,19] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [6,9,17] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [3,19,8,12] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [6,9,14,16] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [19,12,14,16] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [12,20,13,19] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [12,14,13,8] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [8,13,3,3] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [5,11,7,15] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [7,11,7,5] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [1,8,7,15] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [13,1,19,3] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [19,15,19,1] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [7,13,20,9] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [1,18,16,6] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [9,16,6,13] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [11,9,5,1] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [6,3,15,10] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [12,1,9,19] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [15,14,14,19] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [2,5,13,5] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [4,8,8,13] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [3,9,10,13] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [7,17,11,8] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [4,5,15,11] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [11,15,19,12] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [8,20,5,10] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [5,3,5,17] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [14,19,8,8] }
assert my_solution.minimumSeconds(**test_input) == 1
test_input = { "nums": [16,20,4,13] }
assert my_solution.minimumSeconds(**test_input) == 2
test_input = { "nums": [17,16,2,16] }
assert my_solution.minimumSeconds(**test_input) == 1 | 1,691,245,800 |
biweekly-contest-110-minimum-time-to-make-array-sum-at-most-x | https://leetcode.com/problems/minimum-time-to-make-array-sum-at-most-x | minimum-time-to-make-array-sum-at-most-x | {
"questionId": "2952",
"questionFrontendId": "2809",
"title": "Minimum Time to Make Array Sum At Most x",
"titleSlug": "minimum-time-to-make-array-sum-at-most-x",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 207,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
给你两个长度相等下标从 0 开始的整数数组 nums1 和 nums2 。每一秒,对于所有下标 0 <= i < nums1.length ,nums1[i] 的值都增加 nums2[i] 。操作 完成后 ,你可以进行如下操作:
* 选择任一满足 0 <= i < nums1.length 的下标 i ,并使 nums1[i] = 0 。
同时给你一个整数 x 。
请你返回使 nums1 中所有元素之和 小于等于 x 所需要的 最少 时间,如果无法实现,那么返回 -1 。
示例 1:
输入:nums1 = [1,2,3], nums2 = [1,2,3], x = 4
输出:3
解释:
第 1 秒,我们对 i = 0 进行操作,得到 nums1 = [0,2+2,3+3] = [0,4,6] 。
第 2 秒,我们对 i = 1 进行操作,得到 nums1 = [0+1,0,6+3] = [1,0,9] 。
第 3 秒,我们对 i = 2 进行操作,得到 nums1 = [1+1,0+2,0] = [2,2,0] 。
现在 nums1 的和为 4 。不存在更少次数的操作,所以我们返回 3 。
示例 2:
输入:nums1 = [1,2,3], nums2 = [3,3,3], x = 4
输出:-1
解释:不管如何操作,nums1 的和总是会超过 x 。
提示:
* 1 <= nums1.length <= 103
* 1 <= nums1[i] <= 103
* 0 <= nums2[i] <= 103
* nums1.length == nums2.length
* 0 <= x <= 106
"""
class Solution:
def minimumTime(self, nums1: List[int], nums2: List[int], x: int) -> int:
| 给你两个长度相等下标从 0 开始的整数数组 nums1 和 nums2 。每一秒,对于所有下标 0 <= i < nums1.length ,nums1[i] 的值都增加 nums2[i] 。操作 完成后 ,你可以进行如下操作:
* 选择任一满足 0 <= i < nums1.length 的下标 i ,并使 nums1[i] = 0 。
同时给你一个整数 x 。
请你返回使 nums1 中所有元素之和 小于等于 x 所需要的 最少 时间,如果无法实现,那么返回 -1 。
示例 1:
输入:nums1 = [1,2,3], nums2 = [1,2,3], x = 4
输出:3
解释:
第 1 秒,我们对 i = 0 进行操作,得到 nums1 = [0,2+2,3+3] = [0,4,6] 。
第 2 秒,我们对 i = 1 进行操作,得到 nums1 = [0+1,0,6+3] = [1,0,9] 。
第 3 秒,我们对 i = 2 进行操作,得到 nums1 = [1+1,0+2,0] = [2,2,0] 。
现在 nums1 的和为 4 。不存在更少次数的操作,所以我们返回 3 。
示例 2:
输入:nums1 = [1,2,3], nums2 = [3,3,3], x = 4
输出:-1
解释:不管如何操作,nums1 的和总是会超过 x 。
提示:
* 1 <= nums1.length <= 103
* 1 <= nums1[i] <= 103
* 0 <= nums2[i] <= 103
* nums1.length == nums2.length
* 0 <= x <= 106
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumTime(self, nums1: List[int], nums2: List[int], x: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums1": [1,2,3], "nums2": [1,2,3], "x": 4 }
assert my_solution.minimumTime(**test_input) == 3
test_input = { "nums1": [1,2,3], "nums2": [3,3,3], "x": 4 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [4,4,9,10], "nums2": [4,4,1,3], "x": 16 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [5,3], "nums2": [3,2], "x": 4 }
assert my_solution.minimumTime(**test_input) == 2
test_input = { "nums1": [4,5,3,2,3,9,5,7,10,4], "nums2": [4,4,0,4,1,2,4,0,4,0], "x": 47 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [7,9,8,5,8,3], "nums2": [0,1,4,2,3,1], "x": 37 }
assert my_solution.minimumTime(**test_input) == 2
test_input = { "nums1": [8,2,3], "nums2": [1,4,2], "x": 13 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [4,7,2,3,4,3,10,8], "nums2": [3,4,0,1,1,0,2,2], "x": 36 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [2,10,10,4,6,3], "nums2": [1,0,0,1,3,1], "x": 35 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [9,5,3], "nums2": [4,1,3], "x": 17 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [1,7,9,4,8,8,1], "nums2": [2,2,3,2,0,1,0], "x": 20 }
assert my_solution.minimumTime(**test_input) == 6
test_input = { "nums1": [9,2,8,3,1,9,7,6], "nums2": [0,3,4,1,3,4,2,1], "x": 40 }
assert my_solution.minimumTime(**test_input) == 8
test_input = { "nums1": [10], "nums2": [3], "x": 10 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [7,6,8,2,8,9,3,3], "nums2": [2,2,4,0,0,2,2,3], "x": 45 }
assert my_solution.minimumTime(**test_input) == 5
test_input = { "nums1": [4,9,5,2,3], "nums2": [4,2,0,4,0], "x": 18 }
assert my_solution.minimumTime(**test_input) == 3
test_input = { "nums1": [2,10,2,7,8,9,7,6,6], "nums2": [4,2,1,4,3,2,4,4,4], "x": 55 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [6,8,10,7,10,9], "nums2": [4,2,0,4,4,2], "x": 38 }
assert my_solution.minimumTime(**test_input) == 5
test_input = { "nums1": [9,2,8,5,8,3,5,2,2], "nums2": [4,3,4,2,0,1,4,4,2], "x": 41 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [5,3,2,3,10,4,7,9,1,10], "nums2": [2,0,2,0,3,3,4,4,0,1], "x": 30 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [2,3,5], "nums2": [0,0,1], "x": 8 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [7,9,7,9], "nums2": [4,2,0,4], "x": 32 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [8,5,6,4,7,6,3,9,4], "nums2": [0,4,2,4,3,3,1,4,4], "x": 38 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [3,1,1,1], "nums2": [0,0,3,0], "x": 3 }
assert my_solution.minimumTime(**test_input) == 2
test_input = { "nums1": [6,6,8,7,1,7], "nums2": [2,2,1,1,2,3], "x": 27 }
assert my_solution.minimumTime(**test_input) == 5
test_input = { "nums1": [10,5], "nums2": [1,3], "x": 14 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [10,7,1,2,6], "nums2": [4,3,2,2,4], "x": 17 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [9,5,6,1,9,4,5,7], "nums2": [0,4,0,2,2,3,2,4], "x": 24 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [4,1,2,4,10,7,8], "nums2": [0,2,0,4,0,2,1], "x": 18 }
assert my_solution.minimumTime(**test_input) == 5
test_input = { "nums1": [4], "nums2": [0], "x": 4 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [4,7,1,7,5,10], "nums2": [0,4,3,2,3,1], "x": 29 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [9,8,9,7,4,6,8,6,4], "nums2": [4,3,3,3,1,2,2,1,0], "x": 42 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [8,3,2], "nums2": [3,1,3], "x": 7 }
assert my_solution.minimumTime(**test_input) == 3
test_input = { "nums1": [6,5,2,8,8,1,6,4], "nums2": [1,2,1,0,1,0,3,1], "x": 23 }
assert my_solution.minimumTime(**test_input) == 6
test_input = { "nums1": [3,8,5,4,10,2], "nums2": [4,1,4,2,1,0], "x": 26 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [5,3], "nums2": [0,3], "x": 4 }
assert my_solution.minimumTime(**test_input) == 2
test_input = { "nums1": [8], "nums2": [4], "x": 7 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [1,8,6,8,6], "nums2": [3,0,2,4,0], "x": 16 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [8,6], "nums2": [0,3], "x": 14 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [3,4,5,2,4,10,6,3,6,4], "nums2": [3,0,0,2,4,2,4,1,2,1], "x": 28 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [3,2,5,8,8], "nums2": [1,3,2,1,0], "x": 20 }
assert my_solution.minimumTime(**test_input) == 3
test_input = { "nums1": [9,2,8,7,5,2,3,2], "nums2": [3,2,3,0,4,3,1,4], "x": 37 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [6,4,3,1,10,5,10,3,5,9], "nums2": [0,4,1,2,1,2,3,3,4,2], "x": 41 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [6,10,7,10,6,7,7,4], "nums2": [1,3,0,0,1,2,1,3], "x": 55 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [6,4,3,1], "nums2": [1,1,3,3], "x": 7 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [2,10,8,10,1,4,7,10,5,1], "nums2": [4,3,1,2,3,1,3,2,2,1], "x": 29 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [2,8,5], "nums2": [2,0,2], "x": 14 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [7,10,1,3,7,3,2], "nums2": [1,1,3,0,2,2,3], "x": 22 }
assert my_solution.minimumTime(**test_input) == 7
test_input = { "nums1": [6,4,2,3,8,6,6,8,10], "nums2": [2,1,4,1,2,1,0,1,4], "x": 39 }
assert my_solution.minimumTime(**test_input) == 9
test_input = { "nums1": [4,4,8,10,2,7,9,8,1,8], "nums2": [1,0,4,0,3,3,1,2,2,1], "x": 44 }
assert my_solution.minimumTime(**test_input) == 10
test_input = { "nums1": [2,4,1,8,3,9], "nums2": [0,2,0,0,0,4], "x": 21 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [6,10], "nums2": [2,1], "x": 8 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [8,6], "nums2": [3,0], "x": 10 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [5,5,5,10], "nums2": [0,1,0,3], "x": 21 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [7,1,1,2,9,3,3,2,2], "nums2": [0,1,4,3,4,1,2,1,2], "x": 15 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [10,4,1,10,7,5,6,3,2,10], "nums2": [4,0,4,0,3,4,3,0,0,3], "x": 50 }
assert my_solution.minimumTime(**test_input) == 9
test_input = { "nums1": [9,4,6,2], "nums2": [3,4,0,4], "x": 15 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [7,3,9,2,9,10,7,10,10,4], "nums2": [1,4,2,1,4,1,1,0,3,4], "x": 69 }
assert my_solution.minimumTime(**test_input) == 8
test_input = { "nums1": [4,5,6], "nums2": [4,4,0], "x": 13 }
assert my_solution.minimumTime(**test_input) == 2
test_input = { "nums1": [2,3,3,4,4], "nums2": [2,2,1,1,1], "x": 12 }
assert my_solution.minimumTime(**test_input) == 5
test_input = { "nums1": [4,5,5,3,7], "nums2": [3,3,2,0,4], "x": 21 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [1,3,3,4], "nums2": [1,3,2,3], "x": 6 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [9,1,8,9,7,2], "nums2": [3,3,0,2,3,4], "x": 26 }
assert my_solution.minimumTime(**test_input) == 6
test_input = { "nums1": [5,5,6,8,6,1,5,7,8], "nums2": [2,1,0,3,2,2,2,2,4], "x": 33 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [2,9,5,5,6,7,7,9], "nums2": [1,3,0,3,3,3,4,2], "x": 47 }
assert my_solution.minimumTime(**test_input) == 8
test_input = { "nums1": [3], "nums2": [0], "x": 2 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [3,6,4,8,7,9,3,3,9], "nums2": [4,3,2,0,0,3,3,1,4], "x": 34 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [8], "nums2": [1], "x": 6 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [1,7,6,2,9], "nums2": [4,2,3,3,0], "x": 23 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [9,10,10,5,2,4], "nums2": [2,4,0,3,3,4], "x": 40 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [9,10,9,4,8,9,10,7,5], "nums2": [2,0,3,0,2,4,3,2,4], "x": 69 }
assert my_solution.minimumTime(**test_input) == 7
test_input = { "nums1": [1,7,2,7], "nums2": [1,0,2,3], "x": 10 }
assert my_solution.minimumTime(**test_input) == 2
test_input = { "nums1": [10,4], "nums2": [2,4], "x": 10 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [2,10,3,6,2,10,4], "nums2": [4,1,4,4,4,0,0], "x": 30 }
assert my_solution.minimumTime(**test_input) == 7
test_input = { "nums1": [5,9,6], "nums2": [1,3,2], "x": 20 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [4,5,2,4,2,7], "nums2": [0,0,0,0,3,0], "x": 23 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [8], "nums2": [0], "x": 7 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [3], "nums2": [3], "x": 3 }
assert my_solution.minimumTime(**test_input) == 0
test_input = { "nums1": [6,1,10,10], "nums2": [3,2,4,0], "x": 13 }
assert my_solution.minimumTime(**test_input) == 3
test_input = { "nums1": [8,9,2,10,10,1,5], "nums2": [4,3,3,0,2,1,2], "x": 38 }
assert my_solution.minimumTime(**test_input) == 5
test_input = { "nums1": [10,2], "nums2": [3,4], "x": 10 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [8,9,2], "nums2": [2,4,1], "x": 16 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [4,2,3], "nums2": [4,2,2], "x": 4 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [9,8,7,6,5,1,4], "nums2": [2,4,1,2,3,3,0], "x": 28 }
assert my_solution.minimumTime(**test_input) == 7
test_input = { "nums1": [3,4,10,1,2,4,10,3,7,2], "nums2": [4,0,0,1,1,4,4,4,2,1], "x": 44 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [3,5,5,1,6,4,3], "nums2": [1,3,4,3,3,1,4], "x": 23 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [3,8,10,2,5,10], "nums2": [4,0,3,2,4,3], "x": 37 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [8,8,10,8,9,6,1,8], "nums2": [2,0,0,1,2,1,0,4], "x": 38 }
assert my_solution.minimumTime(**test_input) == 4
test_input = { "nums1": [10,7,3,10,7,6,6,10], "nums2": [1,2,4,0,3,4,0,3], "x": 41 }
assert my_solution.minimumTime(**test_input) == 7
test_input = { "nums1": [6,7,4,1,9,6], "nums2": [3,3,3,0,0,1], "x": 32 }
assert my_solution.minimumTime(**test_input) == 2
test_input = { "nums1": [2], "nums2": [2], "x": 1 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [8,9,4,5,6,7], "nums2": [0,3,4,3,3,1], "x": 27 }
assert my_solution.minimumTime(**test_input) == 6
test_input = { "nums1": [7], "nums2": [1], "x": 3 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [9], "nums2": [0], "x": 5 }
assert my_solution.minimumTime(**test_input) == 1
test_input = { "nums1": [7,2,2,2,7,4,2,10,8], "nums2": [4,3,2,4,4,0,1,1,2], "x": 31 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [7,2,6,4,9,9,1,9,6,7], "nums2": [0,1,3,2,3,3,4,2,2,1], "x": 58 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [6,8,2,3,9,8,10,9,9], "nums2": [1,4,2,3,2,0,1,1,3], "x": 40 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [1,1,5], "nums2": [4,4,2], "x": 3 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [7,5,8,3,10,2,4,8,7], "nums2": [4,4,2,4,2,3,1,1,1], "x": 49 }
assert my_solution.minimumTime(**test_input) == -1
test_input = { "nums1": [4,7,2,6,9,2], "nums2": [1,2,1,4,1,3], "x": 28 }
assert my_solution.minimumTime(**test_input) == 3
test_input = { "nums1": [3,6,3,9,5], "nums2": [0,4,0,3,1], "x": 23 }
assert my_solution.minimumTime(**test_input) == 1 | 1,691,245,800 |
weekly-contest-356-number-of-employees-who-met-the-target | https://leetcode.com/problems/number-of-employees-who-met-the-target | number-of-employees-who-met-the-target | {
"questionId": "2876",
"questionFrontendId": "2798",
"title": "Number of Employees Who Met the Target",
"titleSlug": "number-of-employees-who-met-the-target",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 362,
"dislikes": 46,
"categoryTitle": "Algorithms"
} | """
公司里共有 n 名员工,按从 0 到 n - 1 编号。每个员工 i 已经在公司工作了 hours[i] 小时。
公司要求每位员工工作 至少 target 小时。
给你一个下标从 0 开始、长度为 n 的非负整数数组 hours 和一个非负整数 target 。
请你用整数表示并返回工作至少 target 小时的员工数。
示例 1:
输入:hours = [0,1,2,3,4], target = 2
输出:3
解释:公司要求每位员工工作至少 2 小时。
- 员工 0 工作 0 小时,不满足要求。
- 员工 1 工作 1 小时,不满足要求。
- 员工 2 工作 2 小时,满足要求。
- 员工 3 工作 3 小时,满足要求。
- 员工 4 工作 4 小时,满足要求。
共有 3 位满足要求的员工。
示例 2:
输入:hours = [5,1,4,2,2], target = 6
输出:0
解释:公司要求每位员工工作至少 6 小时。
共有 0 位满足要求的员工。
提示:
* 1 <= n == hours.length <= 50
* 0 <= hours[i], target <= 105
"""
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
| 公司里共有 n 名员工,按从 0 到 n - 1 编号。每个员工 i 已经在公司工作了 hours[i] 小时。
公司要求每位员工工作 至少 target 小时。
给你一个下标从 0 开始、长度为 n 的非负整数数组 hours 和一个非负整数 target 。
请你用整数表示并返回工作至少 target 小时的员工数。
示例 1:
输入:hours = [0,1,2,3,4], target = 2
输出:3
解释:公司要求每位员工工作至少 2 小时。
- 员工 0 工作 0 小时,不满足要求。
- 员工 1 工作 1 小时,不满足要求。
- 员工 2 工作 2 小时,满足要求。
- 员工 3 工作 3 小时,满足要求。
- 员工 4 工作 4 小时,满足要求。
共有 3 位满足要求的员工。
示例 2:
输入:hours = [5,1,4,2,2], target = 6
输出:0
解释:公司要求每位员工工作至少 6 小时。
共有 0 位满足要求的员工。
提示:
* 1 <= n == hours.length <= 50
* 0 <= hours[i], target <= 105
请完成下面的代码来解决上述问题:
```python
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
``` |
my_solution = Solution()
test_input = { "hours": [0,1,2,3,4], "target": 2 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [5,1,4,2,2], "target": 6 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [98], "target": 5 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [19], "target": 13 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [70], "target": 13 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [26], "target": 14 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [2], "target": 16 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [77], "target": 19 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [6], "target": 21 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [27], "target": 21 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [36], "target": 22 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [42], "target": 25 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [70], "target": 27 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [2], "target": 28 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [14], "target": 31 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [45], "target": 34 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [44], "target": 35 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [11], "target": 39 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [71], "target": 39 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [91], "target": 45 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [81], "target": 51 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [15], "target": 52 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [90], "target": 59 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [40], "target": 64 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [12], "target": 69 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [83], "target": 70 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [38], "target": 74 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [18], "target": 78 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [60], "target": 83 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [50], "target": 87 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [75], "target": 92 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [91], "target": 96 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [11], "target": 97 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [48,28], "target": 2 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [38,46], "target": 3 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [30,79], "target": 6 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [45,78], "target": 6 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [20,69], "target": 10 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [82,67], "target": 11 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [29,75], "target": 12 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [97,37], "target": 17 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [42,100], "target": 20 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [11,58], "target": 21 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [12,46], "target": 21 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [70,84], "target": 37 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [7,100], "target": 38 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [47,94], "target": 40 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [18,34], "target": 50 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [47,79], "target": 55 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [74,99], "target": 56 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [53,81], "target": 67 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [36,61], "target": 68 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [48,98], "target": 71 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [71,94], "target": 72 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [60,99], "target": 73 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [12,12], "target": 74 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [100,87], "target": 75 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [12,56], "target": 77 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [15,36], "target": 86 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [53,45], "target": 86 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [4,77], "target": 89 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [23,29], "target": 93 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [76,62,96], "target": 5 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [82,67,33], "target": 5 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [28,96,39], "target": 10 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [42,93,58], "target": 13 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [53,22,48], "target": 13 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [68,81,61], "target": 13 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [68,32,33], "target": 22 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [59,65,70], "target": 26 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [15,43,21], "target": 29 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [40,80,75], "target": 33 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [64,11,73], "target": 34 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [1,74,34], "target": 44 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [96,79,91], "target": 44 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3
test_input = { "hours": [59,9,9], "target": 48 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [79,48,62], "target": 53 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [58,83,2], "target": 54 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [51,40,12], "target": 57 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [54,2,80], "target": 60 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [92,45,91], "target": 65 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 2
test_input = { "hours": [93,23,46], "target": 67 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [17,60,1], "target": 70 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [9,63,77], "target": 73 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [44,86,37], "target": 73 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [75,37,68], "target": 73 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [33,26,77], "target": 78 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [11,88,27], "target": 79 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [12,48,44], "target": 80 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [19,88,13], "target": 82 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [61,56,67], "target": 82 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [46,24,38], "target": 84 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [93,51,83], "target": 85 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 1
test_input = { "hours": [58,14,83], "target": 87 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [52,33,56], "target": 89 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [34,73,46], "target": 91 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [26,59,55], "target": 94 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [41,89,34], "target": 100 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0
test_input = { "hours": [9,26,77,55], "target": 0 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 4
test_input = { "hours": [24,79,63,37], "target": 1 }
assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 4 | 1,690,684,200 |
weekly-contest-356-count-complete-subarrays-in-an-array | https://leetcode.com/problems/count-complete-subarrays-in-an-array | count-complete-subarrays-in-an-array | {
"questionId": "2856",
"questionFrontendId": "2799",
"title": "Count Complete Subarrays in an Array",
"titleSlug": "count-complete-subarrays-in-an-array",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 464,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
给你一个由 正 整数组成的数组 nums 。
如果数组中的某个子数组满足下述条件,则称之为 完全子数组 :
* 子数组中 不同 元素的数目等于整个数组不同元素的数目。
返回数组中 完全子数组 的数目。
子数组 是数组中的一个连续非空序列。
示例 1:
输入:nums = [1,3,1,2,2]
输出:4
解释:完全子数组有:[1,3,1,2]、[1,3,1,2,2]、[3,1,2] 和 [3,1,2,2] 。
示例 2:
输入:nums = [5,5,5,5]
输出:10
解释:数组仅由整数 5 组成,所以任意子数组都满足完全子数组的条件。子数组的总数为 10 。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 2000
"""
class Solution:
def countCompleteSubarrays(self, nums: List[int]) -> int:
| 给你一个由 正 整数组成的数组 nums 。
如果数组中的某个子数组满足下述条件,则称之为 完全子数组 :
* 子数组中 不同 元素的数目等于整个数组不同元素的数目。
返回数组中 完全子数组 的数目。
子数组 是数组中的一个连续非空序列。
示例 1:
输入:nums = [1,3,1,2,2]
输出:4
解释:完全子数组有:[1,3,1,2]、[1,3,1,2,2]、[3,1,2] 和 [3,1,2,2] 。
示例 2:
输入:nums = [5,5,5,5]
输出:10
解释:数组仅由整数 5 组成,所以任意子数组都满足完全子数组的条件。子数组的总数为 10 。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 2000
请完成下面的代码来解决上述问题:
```python
class Solution:
def countCompleteSubarrays(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,1,2,2] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [5,5,5,5] }
assert my_solution.countCompleteSubarrays(**test_input) == 10
test_input = { "nums": [459,459,962,1579,1435,756,1872,1597] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1786,1786,1786,114] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1632,1632,528,359,1671,1632,511,1087,424,1684] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1430,12,1430,1075,1722] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1917,1917,608,608,1313,751,558,1561,608] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [254,1690,1690,1068,1779] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1116] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1677,1677,1352,1219,1666,1677,1892,1892,319] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1386,1997,1997,574,574,1360,989] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [50,48,1118,540,1248,1984,1698,41,1984,186] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [273,524,40,1323,1323] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [246,376,828,191,1942,210] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [463,1497,1676,127,1379,17,1075,190] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [765,1370] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1110,804,1110,839,728,839] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [1001] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [665,867,954,1411,728,1006,372,1411] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1213,1203,1277,369,1277] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1898,370,822,1659,1360,128,370,360,261,1898] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [1881,1446] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [474,315,155,155,1986] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1389,1817,401,1067,1356,1997] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1586,1332,1055,1586,1586,1861,892,1445] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1601,1601] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1417,1417,1160,387,928,1572,1832] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1497,1237,1237,946,682,331,742] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [377,377] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [356,356,356,356,356,315] }
assert my_solution.countCompleteSubarrays(**test_input) == 5
test_input = { "nums": [285] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [211,211,731,226] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1253,188,188,5,1393,1696,1062] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [90,1297,482,482,90,1836,1045,1497,482] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [1857,273,609,609,1803,1491,223,609,1857,1052] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [617,1014,679,934,955] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [577,577] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1793,997,1082,1411,997,546,224,336,307,336] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1150,1150] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [634] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1454,1789,1454] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1657,1090,1682,1376,547,547,407,755,1124,1376] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [379] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1673,1584,1584,1055,1971,1122,1086,1692,75] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [722,1427] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1641,448,1641,1437,448,1406,1437] }
assert my_solution.countCompleteSubarrays(**test_input) == 6
test_input = { "nums": [1440,704,1440,1440,749] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [832,832] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1635,1759,1759,1976,700] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1577,1674,1745,156,596,1973,1390,156,1497,415] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1646,1991] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1613,881,1660,1270,1783,881,773,1783,1229,111] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [431] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [113] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [151] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [999,701,389,999,409,488,993,999,517,1860] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [236,596,1263,1563,860,596,1184,575] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [278,338] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [939] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1293,564,614,694,1386,564] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [681,448] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1563,558,1778,1404,1973] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1508,1508,649] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [1077,445,1947,445,789,789,789,956,1988,189] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1984,526,30,1205,1691,1984,1241,280,280,1984] }
assert my_solution.countCompleteSubarrays(**test_input) == 6
test_input = { "nums": [1802,1876,1143,1802,1012,1876,1802,1821] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1338,901,613,575,613] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [406,406,242,242,770,1063,1436,1063,1063] }
assert my_solution.countCompleteSubarrays(**test_input) == 6
test_input = { "nums": [1235,1235] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1337,1088,1088,892,1209,1269] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1941,1941] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [319] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1891,1891,1748,1748,923,1748,923,763,1062,1748] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [1111,503,1980] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [213,1666,469,1675] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [769,1774,1654,928,1204] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [294,294,294,294,1351,294,1351,62,585] }
assert my_solution.countCompleteSubarrays(**test_input) == 6
test_input = { "nums": [1197] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [21,1549,21,1549,1998,1219,1549,1021] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1124,1124,556,1322,556] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [908,908,863,1977,908,8,427,1322] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [770] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [517,1497,334,334,996,1497,1394,534] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [564,750,750,750,1965,1965,1402] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [403,1080,365,1962,1589,1740,1335,1335,1589] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [1712,1621,1295,522,1734,522,1371,1935,684] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [270,1443,807,1704,1487] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1880,1880,1880,604,1634,1412,1880,67,1759,1488] }
assert my_solution.countCompleteSubarrays(**test_input) == 4
test_input = { "nums": [540,1799,1784,1799,972,1786,1578,1480,178,532] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1235,471,367] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1887,1373,190,1764,1764,959,959,1373,17] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1313,910,1172,1541,1758,140,1380,492,240,1664] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [381,1304,381,758,1304,381,758] }
assert my_solution.countCompleteSubarrays(**test_input) == 14
test_input = { "nums": [1517,665] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1555,223,379,223,379,1982] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1268,1268,1268,1268] }
assert my_solution.countCompleteSubarrays(**test_input) == 10
test_input = { "nums": [1051,266,266,94,761,1051,255] }
assert my_solution.countCompleteSubarrays(**test_input) == 3
test_input = { "nums": [420,945,3,172] }
assert my_solution.countCompleteSubarrays(**test_input) == 1
test_input = { "nums": [1045,1120,1045,511,1045,1777,1224,336,560,153] }
assert my_solution.countCompleteSubarrays(**test_input) == 2
test_input = { "nums": [627,592,592,1416,370,229,526,633] }
assert my_solution.countCompleteSubarrays(**test_input) == 1 | 1,690,684,200 |
weekly-contest-356-shortest-string-that-contains-three-strings | https://leetcode.com/problems/shortest-string-that-contains-three-strings | shortest-string-that-contains-three-strings | {
"questionId": "2877",
"questionFrontendId": "2800",
"title": "Shortest String That Contains Three Strings",
"titleSlug": "shortest-string-that-contains-three-strings",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 301,
"dislikes": 244,
"categoryTitle": "Algorithms"
} | """
给你三个字符串 a ,b 和 c , 你的任务是找到长度 最短 的字符串,且这三个字符串都是它的 子字符串 。
如果有多个这样的字符串,请你返回 字典序最小 的一个。
请你返回满足题目要求的字符串。
注意:
* 两个长度相同的字符串 a 和 b ,如果在第一个不相同的字符处,a 的字母在字母表中比 b 的字母 靠前 ,那么字符串 a 比字符串 b 字典序小 。
* 子字符串 是一个字符串中一段连续的字符序列。
示例 1:
输入:a = "abc", b = "bca", c = "aaa"
输出:"aaabca"
解释:字符串 "aaabca" 包含所有三个字符串:a = ans[2...4] ,b = ans[3..5] ,c = ans[0..2] 。结果字符串的长度至少为 6 ,且"aaabca" 是字典序最小的一个。
示例 2:
输入:a = "ab", b = "ba", c = "aba"
输出:"aba"
解释:字符串 "aba" 包含所有三个字符串:a = ans[0..1] ,b = ans[1..2] ,c = ans[0..2] 。由于 c 的长度为 3 ,结果字符串的长度至少为 3 。"aba" 是字典序最小的一个。
提示:
* 1 <= a.length, b.length, c.length <= 100
* a ,b ,c 只包含小写英文字母。
"""
class Solution:
def minimumString(self, a: str, b: str, c: str) -> str:
| 给你三个字符串 a ,b 和 c , 你的任务是找到长度 最短 的字符串,且这三个字符串都是它的 子字符串 。
如果有多个这样的字符串,请你返回 字典序最小 的一个。
请你返回满足题目要求的字符串。
注意:
* 两个长度相同的字符串 a 和 b ,如果在第一个不相同的字符处,a 的字母在字母表中比 b 的字母 靠前 ,那么字符串 a 比字符串 b 字典序小 。
* 子字符串 是一个字符串中一段连续的字符序列。
示例 1:
输入:a = "abc", b = "bca", c = "aaa"
输出:"aaabca"
解释:字符串 "aaabca" 包含所有三个字符串:a = ans[2...4] ,b = ans[3..5] ,c = ans[0..2] 。结果字符串的长度至少为 6 ,且"aaabca" 是字典序最小的一个。
示例 2:
输入:a = "ab", b = "ba", c = "aba"
输出:"aba"
解释:字符串 "aba" 包含所有三个字符串:a = ans[0..1] ,b = ans[1..2] ,c = ans[0..2] 。由于 c 的长度为 3 ,结果字符串的长度至少为 3 。"aba" 是字典序最小的一个。
提示:
* 1 <= a.length, b.length, c.length <= 100
* a ,b ,c 只包含小写英文字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumString(self, a: str, b: str, c: str) -> str:
``` |
my_solution = Solution()
test_input = { "a": "abc", "b": "bca", "c": "aaa" }
assert my_solution.minimumString(**test_input) == "aaabca"
test_input = { "a": "ab", "b": "ba", "c": "aba" }
assert my_solution.minimumString(**test_input) == "aba"
test_input = { "a": "xyyyz", "b": "xzyz", "c": "zzz" }
assert my_solution.minimumString(**test_input) == "xyyyzxzyzzz"
test_input = { "a": "a", "b": "a", "c": "a" }
assert my_solution.minimumString(**test_input) == "a"
test_input = { "a": "a", "b": "a", "c": "b" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "a", "b": "c", "c": "a" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "a", "b": "b", "c": "b" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "a", "b": "a", "c": "c" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "a", "b": "c", "c": "b" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "c", "b": "c", "c": "a" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "k", "b": "e", "c": "a" }
assert my_solution.minimumString(**test_input) == "aek"
test_input = { "a": "a", "b": "b", "c": "a" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "b", "b": "b", "c": "a" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "b", "b": "b", "c": "b" }
assert my_solution.minimumString(**test_input) == "b"
test_input = { "a": "b", "b": "b", "c": "c" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "c", "b": "b", "c": "b" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "b", "b": "c", "c": "c" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "b", "b": "c", "c": "a" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "c", "b": "a", "c": "c" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "c", "b": "b", "c": "c" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "c", "b": "c", "c": "c" }
assert my_solution.minimumString(**test_input) == "c"
test_input = { "a": "e", "b": "k", "c": "y" }
assert my_solution.minimumString(**test_input) == "eky"
test_input = { "a": "z", "b": "p", "c": "m" }
assert my_solution.minimumString(**test_input) == "mpz"
test_input = { "a": "a", "b": "aa", "c": "a" }
assert my_solution.minimumString(**test_input) == "aa"
test_input = { "a": "ac", "b": "a", "c": "a" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "ca", "b": "a", "c": "a" }
assert my_solution.minimumString(**test_input) == "ca"
test_input = { "a": "a", "b": "cc", "c": "a" }
assert my_solution.minimumString(**test_input) == "acc"
test_input = { "a": "a", "b": "a", "c": "aa" }
assert my_solution.minimumString(**test_input) == "aa"
test_input = { "a": "c", "b": "a", "c": "aa" }
assert my_solution.minimumString(**test_input) == "aac"
test_input = { "a": "a", "b": "ab", "c": "a" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "ab", "b": "b", "c": "a" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "ab", "b": "a", "c": "c" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "c", "b": "ac", "c": "a" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "ab", "b": "a", "c": "b" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "b", "b": "a", "c": "ba" }
assert my_solution.minimumString(**test_input) == "ba"
test_input = { "a": "ba", "b": "a", "c": "a" }
assert my_solution.minimumString(**test_input) == "ba"
test_input = { "a": "b", "b": "ba", "c": "a" }
assert my_solution.minimumString(**test_input) == "ba"
test_input = { "a": "a", "b": "bc", "c": "a" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "c", "b": "a", "c": "bc" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "a", "b": "c", "c": "ab" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "c", "b": "a", "c": "ac" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "a", "b": "c", "c": "ca" }
assert my_solution.minimumString(**test_input) == "ca"
test_input = { "a": "c", "b": "a", "c": "cc" }
assert my_solution.minimumString(**test_input) == "acc"
test_input = { "a": "a", "b": "ca", "c": "a" }
assert my_solution.minimumString(**test_input) == "ca"
test_input = { "a": "a", "b": "cc", "c": "c" }
assert my_solution.minimumString(**test_input) == "acc"
test_input = { "a": "aa", "b": "a", "c": "a" }
assert my_solution.minimumString(**test_input) == "aa"
test_input = { "a": "aa", "b": "b", "c": "a" }
assert my_solution.minimumString(**test_input) == "aab"
test_input = { "a": "a", "b": "aa", "c": "c" }
assert my_solution.minimumString(**test_input) == "aac"
test_input = { "a": "b", "b": "c", "c": "aa" }
assert my_solution.minimumString(**test_input) == "aabc"
test_input = { "a": "b", "b": "b", "c": "ab" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "ab", "b": "b", "c": "c" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "ac", "b": "c", "c": "b" }
assert my_solution.minimumString(**test_input) == "acb"
test_input = { "a": "c", "b": "c", "c": "ac" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "ba", "b": "b", "c": "a" }
assert my_solution.minimumString(**test_input) == "ba"
test_input = { "a": "a", "b": "b", "c": "ca" }
assert my_solution.minimumString(**test_input) == "bca"
test_input = { "a": "b", "b": "a", "c": "aa" }
assert my_solution.minimumString(**test_input) == "aab"
test_input = { "a": "b", "b": "b", "c": "aa" }
assert my_solution.minimumString(**test_input) == "aab"
test_input = { "a": "b", "b": "ab", "c": "a" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "b", "b": "ab", "c": "b" }
assert my_solution.minimumString(**test_input) == "ab"
test_input = { "a": "ac", "b": "b", "c": "a" }
assert my_solution.minimumString(**test_input) == "acb"
test_input = { "a": "b", "b": "b", "c": "ac" }
assert my_solution.minimumString(**test_input) == "acb"
test_input = { "a": "bb", "b": "b", "c": "b" }
assert my_solution.minimumString(**test_input) == "bb"
test_input = { "a": "b", "b": "bc", "c": "b" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "b", "b": "b", "c": "ca" }
assert my_solution.minimumString(**test_input) == "bca"
test_input = { "a": "cb", "b": "b", "c": "b" }
assert my_solution.minimumString(**test_input) == "cb"
test_input = { "a": "b", "b": "b", "c": "bb" }
assert my_solution.minimumString(**test_input) == "bb"
test_input = { "a": "b", "b": "a", "c": "bc" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "b", "b": "bc", "c": "c" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "b", "b": "ab", "c": "c" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "b", "b": "bb", "c": "c" }
assert my_solution.minimumString(**test_input) == "bbc"
test_input = { "a": "c", "b": "b", "c": "bc" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "b", "b": "cb", "c": "c" }
assert my_solution.minimumString(**test_input) == "cb"
test_input = { "a": "b", "b": "cc", "c": "c" }
assert my_solution.minimumString(**test_input) == "bcc"
test_input = { "a": "b", "b": "cb", "c": "a" }
assert my_solution.minimumString(**test_input) == "acb"
test_input = { "a": "b", "b": "b", "c": "cb" }
assert my_solution.minimumString(**test_input) == "cb"
test_input = { "a": "c", "b": "b", "c": "cb" }
assert my_solution.minimumString(**test_input) == "cb"
test_input = { "a": "a", "b": "ba", "c": "b" }
assert my_solution.minimumString(**test_input) == "ba"
test_input = { "a": "ba", "b": "a", "c": "b" }
assert my_solution.minimumString(**test_input) == "ba"
test_input = { "a": "ba", "b": "b", "c": "b" }
assert my_solution.minimumString(**test_input) == "ba"
test_input = { "a": "b", "b": "ba", "c": "c" }
assert my_solution.minimumString(**test_input) == "bac"
test_input = { "a": "c", "b": "ba", "c": "b" }
assert my_solution.minimumString(**test_input) == "bac"
test_input = { "a": "c", "b": "c", "c": "ba" }
assert my_solution.minimumString(**test_input) == "bac"
test_input = { "a": "bb", "b": "a", "c": "b" }
assert my_solution.minimumString(**test_input) == "abb"
test_input = { "a": "b", "b": "bb", "c": "b" }
assert my_solution.minimumString(**test_input) == "bb"
test_input = { "a": "c", "b": "bb", "c": "b" }
assert my_solution.minimumString(**test_input) == "bbc"
test_input = { "a": "a", "b": "bc", "c": "b" }
assert my_solution.minimumString(**test_input) == "abc"
test_input = { "a": "bc", "b": "b", "c": "c" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "bc", "b": "c", "c": "b" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "bc", "b": "c", "c": "c" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "ac", "b": "a", "c": "c" }
assert my_solution.minimumString(**test_input) == "ac"
test_input = { "a": "c", "b": "c", "c": "aa" }
assert my_solution.minimumString(**test_input) == "aac"
test_input = { "a": "cb", "b": "b", "c": "c" }
assert my_solution.minimumString(**test_input) == "cb"
test_input = { "a": "c", "b": "b", "c": "cc" }
assert my_solution.minimumString(**test_input) == "bcc"
test_input = { "a": "ba", "b": "b", "c": "c" }
assert my_solution.minimumString(**test_input) == "bac"
test_input = { "a": "aa", "b": "c", "c": "c" }
assert my_solution.minimumString(**test_input) == "aac"
test_input = { "a": "c", "b": "bc", "c": "c" }
assert my_solution.minimumString(**test_input) == "bc"
test_input = { "a": "c", "b": "ca", "c": "c" }
assert my_solution.minimumString(**test_input) == "ca"
test_input = { "a": "c", "b": "cb", "c": "c" }
assert my_solution.minimumString(**test_input) == "cb"
test_input = { "a": "c", "b": "c", "c": "cc" }
assert my_solution.minimumString(**test_input) == "cc"
test_input = { "a": "ca", "b": "c", "c": "a" }
assert my_solution.minimumString(**test_input) == "ca" | 1,690,684,200 |
weekly-contest-356-count-stepping-numbers-in-range | https://leetcode.com/problems/count-stepping-numbers-in-range | count-stepping-numbers-in-range | {
"questionId": "2921",
"questionFrontendId": "2801",
"title": "Count Stepping Numbers in Range",
"titleSlug": "count-stepping-numbers-in-range",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 312,
"dislikes": 8,
"categoryTitle": "Algorithms"
} | """
给你两个正整数 low 和 high ,都用字符串表示,请你统计闭区间 [low, high] 内的 步进数字 数目。
如果一个整数相邻数位之间差的绝对值都 恰好 是 1 ,那么这个数字被称为 步进数字 。
请你返回一个整数,表示闭区间 [low, high] 之间步进数字的数目。
由于答案可能很大,请你将它对 109 + 7 取余 后返回。
注意:步进数字不能有前导 0 。
示例 1:
输入:low = "1", high = "11"
输出:10
解释:区间 [1,11] 内的步进数字为 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 和 10 。总共有 10 个步进数字。所以输出为 10 。
示例 2:
输入:low = "90", high = "101"
输出:2
解释:区间 [90,101] 内的步进数字为 98 和 101 。总共有 2 个步进数字。所以输出为 2 。
提示:
* 1 <= int(low) <= int(high) < 10100
* 1 <= low.length, high.length <= 100
* low 和 high 只包含数字。
* low 和 high 都不含前导 0 。
"""
class Solution:
def countSteppingNumbers(self, low: str, high: str) -> int:
| 给你两个正整数 low 和 high ,都用字符串表示,请你统计闭区间 [low, high] 内的 步进数字 数目。
如果一个整数相邻数位之间差的绝对值都 恰好 是 1 ,那么这个数字被称为 步进数字 。
请你返回一个整数,表示闭区间 [low, high] 之间步进数字的数目。
由于答案可能很大,请你将它对 109 + 7 取余 后返回。
注意:步进数字不能有前导 0 。
示例 1:
输入:low = "1", high = "11"
输出:10
解释:区间 [1,11] 内的步进数字为 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 和 10 。总共有 10 个步进数字。所以输出为 10 。
示例 2:
输入:low = "90", high = "101"
输出:2
解释:区间 [90,101] 内的步进数字为 98 和 101 。总共有 2 个步进数字。所以输出为 2 。
提示:
* 1 <= int(low) <= int(high) < 10100
* 1 <= low.length, high.length <= 100
* low 和 high 只包含数字。
* low 和 high 都不含前导 0 。
请完成下面的代码来解决上述问题:
```python
class Solution:
def countSteppingNumbers(self, low: str, high: str) -> int:
``` |
my_solution = Solution()
test_input = { "low": "1", "high": "11" }
assert my_solution.countSteppingNumbers(**test_input) == 10
test_input = { "low": "90", "high": "101" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "2", "high": "40" }
assert my_solution.countSteppingNumbers(**test_input) == 14
test_input = { "low": "26", "high": "60" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "40", "high": "70" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "46", "high": "66" }
assert my_solution.countSteppingNumbers(**test_input) == 3
test_input = { "low": "58", "high": "58" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "23", "high": "99" }
assert my_solution.countSteppingNumbers(**test_input) == 14
test_input = { "low": "44", "high": "86" }
assert my_solution.countSteppingNumbers(**test_input) == 7
test_input = { "low": "20", "high": "111" }
assert my_solution.countSteppingNumbers(**test_input) == 16
test_input = { "low": "70", "high": "75" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "37", "high": "111" }
assert my_solution.countSteppingNumbers(**test_input) == 12
test_input = { "low": "17", "high": "149" }
assert my_solution.countSteppingNumbers(**test_input) == 18
test_input = { "low": "21", "high": "145" }
assert my_solution.countSteppingNumbers(**test_input) == 18
test_input = { "low": "47", "high": "124" }
assert my_solution.countSteppingNumbers(**test_input) == 12
test_input = { "low": "81", "high": "91" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "18", "high": "159" }
assert my_solution.countSteppingNumbers(**test_input) == 18
test_input = { "low": "85", "high": "92" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "66", "high": "112" }
assert my_solution.countSteppingNumbers(**test_input) == 7
test_input = { "low": "84", "high": "102" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "41", "high": "156" }
assert my_solution.countSteppingNumbers(**test_input) == 14
test_input = { "low": "64", "high": "135" }
assert my_solution.countSteppingNumbers(**test_input) == 10
test_input = { "low": "57", "high": "143" }
assert my_solution.countSteppingNumbers(**test_input) == 10
test_input = { "low": "85", "high": "116" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "103", "high": "104" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "98", "high": "118" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "28", "high": "197" }
assert my_solution.countSteppingNumbers(**test_input) == 16
test_input = { "low": "6", "high": "220" }
assert my_solution.countSteppingNumbers(**test_input) == 26
test_input = { "low": "106", "high": "121" }
assert my_solution.countSteppingNumbers(**test_input) == 1
test_input = { "low": "7", "high": "226" }
assert my_solution.countSteppingNumbers(**test_input) == 25
test_input = { "low": "105", "high": "136" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "30", "high": "221" }
assert my_solution.countSteppingNumbers(**test_input) == 18
test_input = { "low": "113", "high": "139" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "44", "high": "210" }
assert my_solution.countSteppingNumbers(**test_input) == 14
test_input = { "low": "13", "high": "242" }
assert my_solution.countSteppingNumbers(**test_input) == 22
test_input = { "low": "12", "high": "257" }
assert my_solution.countSteppingNumbers(**test_input) == 23
test_input = { "low": "70", "high": "205" }
assert my_solution.countSteppingNumbers(**test_input) == 8
test_input = { "low": "55", "high": "229" }
assert my_solution.countSteppingNumbers(**test_input) == 13
test_input = { "low": "16", "high": "276" }
assert my_solution.countSteppingNumbers(**test_input) == 22
test_input = { "low": "140", "high": "153" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "79", "high": "218" }
assert my_solution.countSteppingNumbers(**test_input) == 8
test_input = { "low": "99", "high": "200" }
assert my_solution.countSteppingNumbers(**test_input) == 3
test_input = { "low": "90", "high": "210" }
assert my_solution.countSteppingNumbers(**test_input) == 5
test_input = { "low": "123", "high": "186" }
assert my_solution.countSteppingNumbers(**test_input) == 1
test_input = { "low": "149", "high": "160" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "138", "high": "180" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "160", "high": "163" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "79", "high": "246" }
assert my_solution.countSteppingNumbers(**test_input) == 10
test_input = { "low": "137", "high": "189" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "163", "high": "163" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "37", "high": "289" }
assert my_solution.countSteppingNumbers(**test_input) == 18
test_input = { "low": "79", "high": "255" }
assert my_solution.countSteppingNumbers(**test_input) == 10
test_input = { "low": "140", "high": "197" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "22", "high": "317" }
assert my_solution.countSteppingNumbers(**test_input) == 21
test_input = { "low": "146", "high": "199" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "57", "high": "288" }
assert my_solution.countSteppingNumbers(**test_input) == 14
test_input = { "low": "109", "high": "237" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "48", "high": "299" }
assert my_solution.countSteppingNumbers(**test_input) == 16
test_input = { "low": "158", "high": "194" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "29", "high": "326" }
assert my_solution.countSteppingNumbers(**test_input) == 22
test_input = { "low": "133", "high": "223" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "109", "high": "249" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "20", "high": "341" }
assert my_solution.countSteppingNumbers(**test_input) == 24
test_input = { "low": "9", "high": "352" }
assert my_solution.countSteppingNumbers(**test_input) == 29
test_input = { "low": "115", "high": "253" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "181", "high": "188" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "120", "high": "250" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "100", "high": "273" }
assert my_solution.countSteppingNumbers(**test_input) == 7
test_input = { "low": "105", "high": "269" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "189", "high": "190" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "148", "high": "237" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "126", "high": "267" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "141", "high": "252" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "185", "high": "209" }
assert my_solution.countSteppingNumbers(**test_input) == 0
test_input = { "low": "14", "high": "381" }
assert my_solution.countSteppingNumbers(**test_input) == 26
test_input = { "low": "7", "high": "388" }
assert my_solution.countSteppingNumbers(**test_input) == 31
test_input = { "low": "15", "high": "383" }
assert my_solution.countSteppingNumbers(**test_input) == 26
test_input = { "low": "78", "high": "325" }
assert my_solution.countSteppingNumbers(**test_input) == 13
test_input = { "low": "131", "high": "274" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "177", "high": "230" }
assert my_solution.countSteppingNumbers(**test_input) == 2
test_input = { "low": "66", "high": "346" }
assert my_solution.countSteppingNumbers(**test_input) == 17
test_input = { "low": "144", "high": "271" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "96", "high": "322" }
assert my_solution.countSteppingNumbers(**test_input) == 9
test_input = { "low": "112", "high": "307" }
assert my_solution.countSteppingNumbers(**test_input) == 6
test_input = { "low": "73", "high": "349" }
assert my_solution.countSteppingNumbers(**test_input) == 16
test_input = { "low": "128", "high": "296" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "189", "high": "237" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "141", "high": "286" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "47", "high": "382" }
assert my_solution.countSteppingNumbers(**test_input) == 20
test_input = { "low": "27", "high": "411" }
assert my_solution.countSteppingNumbers(**test_input) == 24
test_input = { "low": "16", "high": "423" }
assert my_solution.countSteppingNumbers(**test_input) == 26
test_input = { "low": "22", "high": "417" }
assert my_solution.countSteppingNumbers(**test_input) == 25
test_input = { "low": "174", "high": "266" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "101", "high": "342" }
assert my_solution.countSteppingNumbers(**test_input) == 9
test_input = { "low": "76", "high": "370" }
assert my_solution.countSteppingNumbers(**test_input) == 16
test_input = { "low": "147", "high": "301" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "72", "high": "376" }
assert my_solution.countSteppingNumbers(**test_input) == 16
test_input = { "low": "154", "high": "297" }
assert my_solution.countSteppingNumbers(**test_input) == 4
test_input = { "low": "15", "high": "439" }
assert my_solution.countSteppingNumbers(**test_input) == 28
test_input = { "low": "73", "high": "381" }
assert my_solution.countSteppingNumbers(**test_input) == 16 | 1,690,684,200 |
weekly-contest-355-split-strings-by-separator | https://leetcode.com/problems/split-strings-by-separator | split-strings-by-separator | {
"questionId": "2881",
"questionFrontendId": "2788",
"title": "Split Strings by Separator",
"titleSlug": "split-strings-by-separator",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 262,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
给你一个字符串数组 words 和一个字符 separator ,请你按 separator 拆分 words 中的每个字符串。
返回一个由拆分后的新字符串组成的字符串数组,不包括空字符串 。
注意
* separator 用于决定拆分发生的位置,但它不包含在结果字符串中。
* 拆分可能形成两个以上的字符串。
* 结果字符串必须保持初始相同的先后顺序。
示例 1:
输入:words = ["one.two.three","four.five","six"], separator = "."
输出:["one","two","three","four","five","six"]
解释:在本示例中,我们进行下述拆分:
"one.two.three" 拆分为 "one", "two", "three"
"four.five" 拆分为 "four", "five"
"six" 拆分为 "six"
因此,结果数组为 ["one","two","three","four","five","six"] 。
示例 2:
输入:words = ["$easy$","$problem$"], separator = "$"
输出:["easy","problem"]
解释:在本示例中,我们进行下述拆分:
"$easy$" 拆分为 "easy"(不包括空字符串)
"$problem$" 拆分为 "problem"(不包括空字符串)
因此,结果数组为 ["easy","problem"] 。
示例 3:
输入:words = ["|||"], separator = "|"
输出:[]
解释:在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。
提示:
* 1 <= words.length <= 100
* 1 <= words[i].length <= 20
* words[i] 中的字符要么是小写英文字母,要么就是字符串 ".,|$#@" 中的字符(不包括引号)
* separator 是字符串 ".,|$#@" 中的某个字符(不包括引号)
"""
class Solution:
def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
| 给你一个字符串数组 words 和一个字符 separator ,请你按 separator 拆分 words 中的每个字符串。
返回一个由拆分后的新字符串组成的字符串数组,不包括空字符串 。
注意
* separator 用于决定拆分发生的位置,但它不包含在结果字符串中。
* 拆分可能形成两个以上的字符串。
* 结果字符串必须保持初始相同的先后顺序。
示例 1:
输入:words = ["one.two.three","four.five","six"], separator = "."
输出:["one","two","three","four","five","six"]
解释:在本示例中,我们进行下述拆分:
"one.two.three" 拆分为 "one", "two", "three"
"four.five" 拆分为 "four", "five"
"six" 拆分为 "six"
因此,结果数组为 ["one","two","three","four","five","six"] 。
示例 2:
输入:words = ["$easy$","$problem$"], separator = "$"
输出:["easy","problem"]
解释:在本示例中,我们进行下述拆分:
"$easy$" 拆分为 "easy"(不包括空字符串)
"$problem$" 拆分为 "problem"(不包括空字符串)
因此,结果数组为 ["easy","problem"] 。
示例 3:
输入:words = ["|||"], separator = "|"
输出:[]
解释:在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。
提示:
* 1 <= words.length <= 100
* 1 <= words[i].length <= 20
* words[i] 中的字符要么是小写英文字母,要么就是字符串 ".,|$#@" 中的字符(不包括引号)
* separator 是字符串 ".,|$#@" 中的某个字符(不包括引号)
请完成下面的代码来解决上述问题:
```python
class Solution:
def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
``` |
my_solution = Solution()
test_input = { "words": ["one.two.three","four.five","six"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["one","two","three","four","five","six"]
test_input = { "words": ["$easy$","$problem$"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["easy","problem"]
test_input = { "words": ["|||"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == []
test_input = { "words": ["stars.bars.$"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["stars","bars","$"]
test_input = { "words": ["###x#i@f"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == ["x","i@f"]
test_input = { "words": ["##q@t#"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == ["q@t"]
test_input = { "words": ["#,"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == [","]
test_input = { "words": ["#@"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["#"]
test_input = { "words": ["#a$f$nwgq#vw"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["#a","f","nwgq#vw"]
test_input = { "words": ["#uyddd,trxiwfv"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["#uyddd","trxiwfv"]
test_input = { "words": ["#x"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["#x"]
test_input = { "words": ["#x#,"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["#x#"]
test_input = { "words": ["#|"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == ["|"]
test_input = { "words": ["#|a|b|##|#|#g#u|"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["#","a","b","##","#","#g#u"]
test_input = { "words": ["$$.o.$$."], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == [".o.","."]
test_input = { "words": ["$,,"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["$"]
test_input = { "words": ["$j@@@"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["$j"]
test_input = { "words": [",$$"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == [","]
test_input = { "words": [",,"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == [",,"]
test_input = { "words": [",,,@@n"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["@@n"]
test_input = { "words": [",,,@o,"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["@o"]
test_input = { "words": [",,,p,#r#"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["p","#r#"]
test_input = { "words": [",,."], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["."]
test_input = { "words": [",,ulxh|,u|ustg||ulo"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == [",,ulxh",",u","ustg","ulo"]
test_input = { "words": [",esplco"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == [",esplco"]
test_input = { "words": [",g#,,,#"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["g#","#"]
test_input = { "words": [",gko"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == [",gko"]
test_input = { "words": [",h,u.f"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["h","u.f"]
test_input = { "words": [",y|z|bg,"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == [",y","z","bg,"]
test_input = { "words": [".#z.###b"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == [".","z.","b"]
test_input = { "words": [".."], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == []
test_input = { "words": [".@"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["@"]
test_input = { "words": [".n#.#.."], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == [".n",".",".."]
test_input = { "words": [".trs.t..cvvw.,..p"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["trs","t","cvvw",",","p"]
test_input = { "words": [".vy.$$.qw"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["vy","$$","qw"]
test_input = { "words": ["@,,"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == [",,"]
test_input = { "words": ["@..@@.s.@u@.@"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["@","@@","s","@u@","@"]
test_input = { "words": ["@@..@@@.@vn@"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["@@","@@@","@vn@"]
test_input = { "words": ["@@@@@@@...@@@"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["@@@@@@@","@@@"]
test_input = { "words": ["@@@@@@g@@v"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["g","v"]
test_input = { "words": ["@@||@"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["@@","@"]
test_input = { "words": ["@acp.@"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["@acp","@"]
test_input = { "words": ["@iw"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["@iw"]
test_input = { "words": ["@z#y@@ni"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == ["@z","y@@ni"]
test_input = { "words": ["aovx"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["aovx"]
test_input = { "words": ["b"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["b"]
test_input = { "words": ["b,,"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["b,,"]
test_input = { "words": ["b,s"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["b","s"]
test_input = { "words": ["bqukl,bv"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["bqukl","bv"]
test_input = { "words": ["cmypa#"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == ["cmypa"]
test_input = { "words": ["cq,,,,y"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["cq,,,,y"]
test_input = { "words": ["d,,,@d"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["d","@d"]
test_input = { "words": ["dgjtc"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["dgjtc"]
test_input = { "words": ["e$,$,w$,,z,,$$,"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["e$","$","w$","z","$$"]
test_input = { "words": ["e,,$,$,,,"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["e","$","$"]
test_input = { "words": ["edz."], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["edz."]
test_input = { "words": ["f"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["f"]
test_input = { "words": ["f."], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["f."]
test_input = { "words": ["g"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["g"]
test_input = { "words": ["g###"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["g###"]
test_input = { "words": ["g|@@@@@|"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["g|","|"]
test_input = { "words": ["h"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["h"]
test_input = { "words": ["h#"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == ["h"]
test_input = { "words": ["hpo"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["hpo"]
test_input = { "words": ["imq#zect"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["imq#zect"]
test_input = { "words": ["io@"], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["io@"]
test_input = { "words": ["j@..@f.v@."], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["j","..","f.v","."]
test_input = { "words": ["jxe|xfim"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["jxe","xfim"]
test_input = { "words": ["lyegjxsg"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["lyegjxsg"]
test_input = { "words": ["l|"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["l|"]
test_input = { "words": ["mm..,.j.."], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["mm",",","j"]
test_input = { "words": ["n."], "separator": "." }
assert my_solution.splitWordsBySeparator(**test_input) == ["n"]
test_input = { "words": ["no#"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["no#"]
test_input = { "words": ["nyy|"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["nyy"]
test_input = { "words": ["pfx|ons"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["pfx|ons"]
test_input = { "words": ["pl"], "separator": "#" }
assert my_solution.splitWordsBySeparator(**test_input) == ["pl"]
test_input = { "words": ["q"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["q"]
test_input = { "words": ["q"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["q"]
test_input = { "words": ["qv"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["qv"]
test_input = { "words": ["r"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["r"]
test_input = { "words": ["rxs|$mg"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["rxs|","mg"]
test_input = { "words": ["sls,l,xn"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["sls","l","xn"]
test_input = { "words": ["t@"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["t"]
test_input = { "words": ["tsklm|dhmsr"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["tsklm","dhmsr"]
test_input = { "words": ["t|@@w||"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["t","@@w"]
test_input = { "words": ["u"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["u"]
test_input = { "words": ["ufjvddie"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["ufjvddie"]
test_input = { "words": ["unvywqeuqvdq"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["unvywqeuqvdq"]
test_input = { "words": ["vt@"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["vt"]
test_input = { "words": ["w,xu#v,m,jh,"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["w","xu#v","m","jh"]
test_input = { "words": ["w|vip|"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["w","vip"]
test_input = { "words": ["xjmjh$@"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["xjmjh$"]
test_input = { "words": ["xm,tjd$"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["xm,tjd"]
test_input = { "words": ["yww"], "separator": "@" }
assert my_solution.splitWordsBySeparator(**test_input) == ["yww"]
test_input = { "words": ["z"], "separator": "$" }
assert my_solution.splitWordsBySeparator(**test_input) == ["z"]
test_input = { "words": ["z#|||###"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["z#","###"]
test_input = { "words": ["zb@"], "separator": "," }
assert my_solution.splitWordsBySeparator(**test_input) == ["zb@"]
test_input = { "words": ["|,,|,|||"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == [",,",","]
test_input = { "words": ["|,r,fg"], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == [",r,fg"]
test_input = { "words": ["|."], "separator": "|" }
assert my_solution.splitWordsBySeparator(**test_input) == ["."] | 1,690,079,400 |
weekly-contest-355-largest-element-in-an-array-after-merge-operations | https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations | largest-element-in-an-array-after-merge-operations | {
"questionId": "2872",
"questionFrontendId": "2789",
"title": "Largest Element in an Array after Merge Operations",
"titleSlug": "largest-element-in-an-array-after-merge-operations",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 413,
"dislikes": 26,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始、由正整数组成的数组 nums 。
你可以在数组上执行下述操作 任意 次:
* 选中一个同时满足 0 <= i < nums.length - 1 和 nums[i] <= nums[i + 1] 的整数 i 。将元素 nums[i + 1] 替换为 nums[i] + nums[i + 1] ,并从数组中删除元素 nums[i] 。
返回你可以从最终数组中获得的 最大 元素的值。
示例 1:
输入:nums = [2,3,7,9,3]
输出:21
解释:我们可以在数组上执行下述操作:
- 选中 i = 0 ,得到数组 nums = [5,7,9,3] 。
- 选中 i = 1 ,得到数组 nums = [5,16,3] 。
- 选中 i = 0 ,得到数组 nums = [21,3] 。
最终数组中的最大元素是 21 。可以证明我们无法获得更大的元素。
示例 2:
输入:nums = [5,3,3]
输出:11
解释:我们可以在数组上执行下述操作:
- 选中 i = 1 ,得到数组 nums = [5,6] 。
- 选中 i = 0 ,得到数组 nums = [11] 。
最终数组中只有一个元素,即 11 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 106
"""
class Solution:
def maxArrayValue(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始、由正整数组成的数组 nums 。
你可以在数组上执行下述操作 任意 次:
* 选中一个同时满足 0 <= i < nums.length - 1 和 nums[i] <= nums[i + 1] 的整数 i 。将元素 nums[i + 1] 替换为 nums[i] + nums[i + 1] ,并从数组中删除元素 nums[i] 。
返回你可以从最终数组中获得的 最大 元素的值。
示例 1:
输入:nums = [2,3,7,9,3]
输出:21
解释:我们可以在数组上执行下述操作:
- 选中 i = 0 ,得到数组 nums = [5,7,9,3] 。
- 选中 i = 1 ,得到数组 nums = [5,16,3] 。
- 选中 i = 0 ,得到数组 nums = [21,3] 。
最终数组中的最大元素是 21 。可以证明我们无法获得更大的元素。
示例 2:
输入:nums = [5,3,3]
输出:11
解释:我们可以在数组上执行下述操作:
- 选中 i = 1 ,得到数组 nums = [5,6] 。
- 选中 i = 0 ,得到数组 nums = [11] 。
最终数组中只有一个元素,即 11 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 106
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxArrayValue(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,3,7,9,3] }
assert my_solution.maxArrayValue(**test_input) == 21
test_input = { "nums": [5,3,3] }
assert my_solution.maxArrayValue(**test_input) == 11
test_input = { "nums": [77] }
assert my_solution.maxArrayValue(**test_input) == 77
test_input = { "nums": [34,95,50,12,25,100,21,3,25,16,76,73,93,46,18] }
assert my_solution.maxArrayValue(**test_input) == 623
test_input = { "nums": [40,15,35,98,77,79,24,62,53,84,97,16,30,22,49] }
assert my_solution.maxArrayValue(**test_input) == 781
test_input = { "nums": [64,35,42,19,95,8,83,89,33,21,97,11,51,93,36,34,67,53] }
assert my_solution.maxArrayValue(**test_input) == 878
test_input = { "nums": [65,68,55,6,79,30,81,25,61,2,28,59,63,15,35,8,10,83] }
assert my_solution.maxArrayValue(**test_input) == 773
test_input = { "nums": [56] }
assert my_solution.maxArrayValue(**test_input) == 56
test_input = { "nums": [100] }
assert my_solution.maxArrayValue(**test_input) == 100
test_input = { "nums": [35,23,71,38] }
assert my_solution.maxArrayValue(**test_input) == 129
test_input = { "nums": [56,67,18,81,95,41,39,56,63,70,56,31,84,46,28,38,27,56,13,10,58,16,85,21,63,8] }
assert my_solution.maxArrayValue(**test_input) == 1134
test_input = { "nums": [72,72] }
assert my_solution.maxArrayValue(**test_input) == 144
test_input = { "nums": [16,31,55] }
assert my_solution.maxArrayValue(**test_input) == 102
test_input = { "nums": [6,65,68,7,35,19,28] }
assert my_solution.maxArrayValue(**test_input) == 228
test_input = { "nums": [38,37,88,60,93,4,5,65,74,25,59,28,86,33,28,33,93] }
assert my_solution.maxArrayValue(**test_input) == 849
test_input = { "nums": [29,9,3,55,25,38,88,39,38,73,47,57,40,56,4,52,1,44,88,20,18,8] }
assert my_solution.maxArrayValue(**test_input) == 786
test_input = { "nums": [34,92,42,24,98,87,40,82,51,67,70,75,45,57,67] }
assert my_solution.maxArrayValue(**test_input) == 931
test_input = { "nums": [31,75,44,92,13,10,3,41,47,89,5,92,17,62,65,40,43,68,30,45,85,24,40,77,80,65] }
assert my_solution.maxArrayValue(**test_input) == 1218
test_input = { "nums": [63,58,61,58,82,48,83,24,24,61,31,16,26,50] }
assert my_solution.maxArrayValue(**test_input) == 685
test_input = { "nums": [10,82,74,54,20,43,74,95,17,28,44,74,25,19,75,2,84,99] }
assert my_solution.maxArrayValue(**test_input) == 919
test_input = { "nums": [91,32,21,55,44,29,82,75,66,29,77,62,55,94,49,80,12,46,80,64,88,51,2,24,11,10,86,39,16] }
assert my_solution.maxArrayValue(**test_input) == 1415
test_input = { "nums": [18,16,56,64,82,25,16,2,19] }
assert my_solution.maxArrayValue(**test_input) == 236
test_input = { "nums": [29,79,47,55,13,47,48,91,29,28,34,85,98,44,93,56,24,77,61] }
assert my_solution.maxArrayValue(**test_input) == 977
test_input = { "nums": [74,8,1,57,25,62] }
assert my_solution.maxArrayValue(**test_input) == 227
test_input = { "nums": [25,36,55,32,15,16,73,67,82,23,17,29,78,34,4,91,1,1,55,65] }
assert my_solution.maxArrayValue(**test_input) == 799
test_input = { "nums": [6,36,69,97,86,44,27,46] }
assert my_solution.maxArrayValue(**test_input) == 411
test_input = { "nums": [18,36,100,34,31,89,96,6,73,10,82,20,26,13,24,51,87,70,63,36] }
assert my_solution.maxArrayValue(**test_input) == 796
test_input = { "nums": [86,57,57,56,38,97,82,48,33,55,19,21,57,85,11,11,71,94,61,41,1,78,39,45] }
assert my_solution.maxArrayValue(**test_input) == 1243
test_input = { "nums": [33,9,11,6,68,43,76,40,91] }
assert my_solution.maxArrayValue(**test_input) == 377
test_input = { "nums": [89,45,50,98,23,79,10,98,69,65,47,46,95] }
assert my_solution.maxArrayValue(**test_input) == 814
test_input = { "nums": [58,95] }
assert my_solution.maxArrayValue(**test_input) == 153
test_input = { "nums": [91,50] }
assert my_solution.maxArrayValue(**test_input) == 91
test_input = { "nums": [99,82,49,52,5,69,65,94,94,57,46,26,28,84,42,61,19,87,71,66,1,72] }
assert my_solution.maxArrayValue(**test_input) == 1269
test_input = { "nums": [75,75,93,44,16,27,43,71,65,90,100,97,39,100,55,15,10,7,25,23,47] }
assert my_solution.maxArrayValue(**test_input) == 1117
test_input = { "nums": [65] }
assert my_solution.maxArrayValue(**test_input) == 65
test_input = { "nums": [50] }
assert my_solution.maxArrayValue(**test_input) == 50
test_input = { "nums": [56,8,10,87,83,79,33,72,32,59,75,2,46,9] }
assert my_solution.maxArrayValue(**test_input) == 594
test_input = { "nums": [26,77,78,94,90,90,57,100,60,1,98,85,78,77,63,30,88,60,41,55] }
assert my_solution.maxArrayValue(**test_input) == 1348
test_input = { "nums": [65,53,93,76,75,18,32,88,4] }
assert my_solution.maxArrayValue(**test_input) == 500
test_input = { "nums": [24,89,92,48,81,49,83,4,53,39,48,10,53,51,41,23,83,8,53,91,43,58,82] }
assert my_solution.maxArrayValue(**test_input) == 1206
test_input = { "nums": [59,17,33] }
assert my_solution.maxArrayValue(**test_input) == 59
test_input = { "nums": [15,35,97,93,34,34,90,2,21] }
assert my_solution.maxArrayValue(**test_input) == 398
test_input = { "nums": [87,64,21,27,41,63,28,75,64,22,30,76,77,91,84,81,99,86,1,74,46,4,7] }
assert my_solution.maxArrayValue(**test_input) == 1030
test_input = { "nums": [89,49,59,59,2,77,55,44,51,47,100,77,30,71,47,100,13,17,12,38,26,55,89,41] }
assert my_solution.maxArrayValue(**test_input) == 1207
test_input = { "nums": [71,4,53,51,9,92,91,86,84,58,31,39,38,49,56,27,91,17,10,56,52,78,35,76,39] }
assert my_solution.maxArrayValue(**test_input) == 1254
test_input = { "nums": [9,46,6,42,81,7,61,88,37,15,20,67] }
assert my_solution.maxArrayValue(**test_input) == 479
test_input = { "nums": [50,64,31,70,46,30,41,69,80,45,73,4,100,88,7,3,59] }
assert my_solution.maxArrayValue(**test_input) == 703
test_input = { "nums": [20,41,58,61,79,7,58,42,89,39] }
assert my_solution.maxArrayValue(**test_input) == 455
test_input = { "nums": [68,86,34,99,4,6,24,88,26,83,2,33,37,79,30,60,56,44,53,4,86,60,13,81,95,28,83,24] }
assert my_solution.maxArrayValue(**test_input) == 1362
test_input = { "nums": [5,61,59,13,21,90,32,93,84,16,71,78,53,90,5,50,47,85,83,72,88,20,97,28,73,75,59,34,21] }
assert my_solution.maxArrayValue(**test_input) == 1489
test_input = { "nums": [99,57,14,77,78,88,47,12,45,72,70,73,75,35,50,88,26,38,77,23,86,27,9,16] }
assert my_solution.maxArrayValue(**test_input) == 1230
test_input = { "nums": [68,65,95,53,51,26,2,3,17,26,15,37,50,79,20,71,99,72,82,37,29,34,74,93] }
assert my_solution.maxArrayValue(**test_input) == 1198
test_input = { "nums": [68,21,61,74,38,91,99,32,98,12,52] }
assert my_solution.maxArrayValue(**test_input) == 582
test_input = { "nums": [98,95,15,53,31,15,9,24,59] }
assert my_solution.maxArrayValue(**test_input) == 399
test_input = { "nums": [51,18,21,99,6,55,41,20,74,43,98,41,58,29,75,16,8,83,23,79,73,68,95,10,67] }
assert my_solution.maxArrayValue(**test_input) == 1174
test_input = { "nums": [78,91,52,92,42,53,77,88,40,33,86,70,85,50,65,43,75,60,28,97,95,95] }
assert my_solution.maxArrayValue(**test_input) == 1495
test_input = { "nums": [99,6] }
assert my_solution.maxArrayValue(**test_input) == 99
test_input = { "nums": [59,50,38,100,42,42,99,7] }
assert my_solution.maxArrayValue(**test_input) == 430
test_input = { "nums": [35,23,73,45,29,94,1,18,46,7,52,6,47,47,19,93,48,70,85,98,50,89,23] }
assert my_solution.maxArrayValue(**test_input) == 1075
test_input = { "nums": [2,55,19,10,28,45,86,31,45,32,38,95,65,23,50,39,51,24,40,15,16] }
assert my_solution.maxArrayValue(**test_input) == 778
test_input = { "nums": [31,59,12,90,39] }
assert my_solution.maxArrayValue(**test_input) == 192
test_input = { "nums": [53,87,11,58,79,42,44,24,68,61] }
assert my_solution.maxArrayValue(**test_input) == 466
test_input = { "nums": [20,45] }
assert my_solution.maxArrayValue(**test_input) == 65
test_input = { "nums": [85,36,99,11,91,88,55,25,68,88,27,98,7,14,40,27,18,51,90,21,77,12,87,11,37,80,70] }
assert my_solution.maxArrayValue(**test_input) == 1343
test_input = { "nums": [43,50,40,92,31,2,92] }
assert my_solution.maxArrayValue(**test_input) == 350
test_input = { "nums": [79,90,32,30,33,18,55,96] }
assert my_solution.maxArrayValue(**test_input) == 433
test_input = { "nums": [65,15,18,94,96,22,37,19,23,11,27,94,5,99] }
assert my_solution.maxArrayValue(**test_input) == 625
test_input = { "nums": [74,26,11,96,49,19,25,77,47,31,87,96,19,40,95,91,48,79,33,96] }
assert my_solution.maxArrayValue(**test_input) == 1139
test_input = { "nums": [17,90,66] }
assert my_solution.maxArrayValue(**test_input) == 107
test_input = { "nums": [60,11,95,75,10,64,62,20] }
assert my_solution.maxArrayValue(**test_input) == 166
test_input = { "nums": [99,6,67,44,84,29,87,13,44,12,92,53,26,47,88,44,75,33,19] }
assert my_solution.maxArrayValue(**test_input) == 910
test_input = { "nums": [69,21,11,10,42,3,38,36,50,28,25,93,37,45,73,37,97] }
assert my_solution.maxArrayValue(**test_input) == 715
test_input = { "nums": [32,76,65,61,84,11,94,96,17,14,79,15,62] }
assert my_solution.maxArrayValue(**test_input) == 629
test_input = { "nums": [59,91,27,74,57,30,51,67,88,26,89,10,70,31,32,26,42] }
assert my_solution.maxArrayValue(**test_input) == 870
test_input = { "nums": [8,73,22,37,39,1,66,59,5,20,16,68,55,50,48,6,8,46,93,76,48,14,92] }
assert my_solution.maxArrayValue(**test_input) == 950
test_input = { "nums": [58,34,72,5,33,34,68,96,63,85,84,74,87,33,75,43,36,28,62,44,95,39,2] }
assert my_solution.maxArrayValue(**test_input) == 1209
test_input = { "nums": [49,88,44,17,36,65,94,92,75,23,67,55,68,80,95,11,68,66,77,66,3,32,16,81,34,20,56,87,87,29] }
assert my_solution.maxArrayValue(**test_input) == 1652
test_input = { "nums": [94,27,5,47] }
assert my_solution.maxArrayValue(**test_input) == 94
test_input = { "nums": [83,85,59,55] }
assert my_solution.maxArrayValue(**test_input) == 168
test_input = { "nums": [72,56,30,65,94,91,12,99,9] }
assert my_solution.maxArrayValue(**test_input) == 519
test_input = { "nums": [9,99,20,61,57,88,50,36,21,100,62,98,94,81,96,3,98,37,88] }
assert my_solution.maxArrayValue(**test_input) == 1198
test_input = { "nums": [45,18,13,66,54,45,64,70,94,67,26,48,84,57,81,85,35,17,20,84,78,24,63,9] }
assert my_solution.maxArrayValue(**test_input) == 1238
test_input = { "nums": [5,38,82,83,92,97] }
assert my_solution.maxArrayValue(**test_input) == 397
test_input = { "nums": [95,15,19,26,59,58] }
assert my_solution.maxArrayValue(**test_input) == 214
test_input = { "nums": [94,75,16,33,2,70,56,4,64] }
assert my_solution.maxArrayValue(**test_input) == 414
test_input = { "nums": [64,50,26,66] }
assert my_solution.maxArrayValue(**test_input) == 206
test_input = { "nums": [3,48,11,71,57,72,83,61,59,25,36,29,11,69,75,48,44,44] }
assert my_solution.maxArrayValue(**test_input) == 846
test_input = { "nums": [88,7,38,15,43,8,87,7,25,2,51,29,74,34,84,87,83,34,74,22,45,96,71,4,23,28,27,68,61] }
assert my_solution.maxArrayValue(**test_input) == 1254
test_input = { "nums": [30,58,2,20,54] }
assert my_solution.maxArrayValue(**test_input) == 164
test_input = { "nums": [17,34,71,23,88,84,35,49,89,39,33,13,87,49,48,97] }
assert my_solution.maxArrayValue(**test_input) == 856
test_input = { "nums": [50,67,98,47,18,91,80,3,19,74,40,89,85,99,95,81,72,96,56,15,48,93,64] }
assert my_solution.maxArrayValue(**test_input) == 1416
test_input = { "nums": [58,10,99,6,16,94,45,47,4,30,58] }
assert my_solution.maxArrayValue(**test_input) == 467
test_input = { "nums": [92,58,90,38,37,95,47,82,6,86,99,9,91,80,73,54,45] }
assert my_solution.maxArrayValue(**test_input) == 830
test_input = { "nums": [31,100,59,88,81,74,49,21,31,53,9,89,67,4,84,46,41] }
assert my_solution.maxArrayValue(**test_input) == 840
test_input = { "nums": [25,3,94,55,70,23,43,8,65,34,83,60,53,62,97,55,3,10] }
assert my_solution.maxArrayValue(**test_input) == 775
test_input = { "nums": [27,53,99,55,15,59,85,40,46,45,45,71,42,67] }
assert my_solution.maxArrayValue(**test_input) == 749
test_input = { "nums": [8,62,12,10,79,36,59,73,76,24,45,98,72,83,61,6,19,49] }
assert my_solution.maxArrayValue(**test_input) == 872
test_input = { "nums": [2,24,30,18,94,26,22,60,50,3,27,31] }
assert my_solution.maxArrayValue(**test_input) == 387
test_input = { "nums": [64,17,57,72,24,88,29,2,23,82,15,69,80,93,38,47,9,10,68,89,65,16] }
assert my_solution.maxArrayValue(**test_input) == 976
test_input = { "nums": [99,58,59,5,67,15,6,91,71,75,79,59,40,1,18,49,48,75,92,72,81,43,31,31,29,94,39] }
assert my_solution.maxArrayValue(**test_input) == 1388 | 1,690,079,400 |
weekly-contest-355-maximum-number-of-groups-with-increasing-length | https://leetcode.com/problems/maximum-number-of-groups-with-increasing-length | maximum-number-of-groups-with-increasing-length | {
"questionId": "2919",
"questionFrontendId": "2790",
"title": "Maximum Number of Groups With Increasing Length",
"titleSlug": "maximum-number-of-groups-with-increasing-length",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 383,
"dislikes": 38,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始、长度为 n 的数组 usageLimits 。
你的任务是使用从 0 到 n - 1 的数字创建若干组,并确保每个数字 i 在 所有组 中使用的次数总共不超过 usageLimits[i] 次。此外,还必须满足以下条件:
* 每个组必须由 不同 的数字组成,也就是说,单个组内不能存在重复的数字。
* 每个组(除了第一个)的长度必须 严格大于 前一个组。
在满足所有条件的情况下,以整数形式返回可以创建的最大组数。
示例 1:
输入:usageLimits = [1,2,5]
输出:3
解释:在这个示例中,我们可以使用 0 至多一次,使用 1 至多 2 次,使用 2 至多 5 次。
一种既能满足所有条件,又能创建最多组的方式是:
组 1 包含数字 [2] 。
组 2 包含数字 [1,2] 。
组 3 包含数字 [0,1,2] 。
可以证明能够创建的最大组数是 3 。
所以,输出是 3 。
示例 2:
输入:usageLimits = [2,1,2]
输出:2
解释:在这个示例中,我们可以使用 0 至多 2 次,使用 1 至多 1 次,使用 2 至多 2 次。
一种既能满足所有条件,又能创建最多组的方式是:
组 1 包含数字 [0] 。
组 2 包含数字 [1,2] 。
可以证明能够创建的最大组数是 2 。
所以,输出是 2 。
示例 3:
输入:usageLimits = [1,1]
输出:1
解释:在这个示例中,我们可以使用 0 和 1 至多 1 次。
一种既能满足所有条件,又能创建最多组的方式是:
组 1 包含数字 [0] 。
可以证明能够创建的最大组数是 1 。
所以,输出是 1 。
提示:
* 1 <= usageLimits.length <= 105
* 1 <= usageLimits[i] <= 109
"""
class Solution:
def maxIncreasingGroups(self, usageLimits: List[int]) -> int:
| 给你一个下标从 0 开始、长度为 n 的数组 usageLimits 。
你的任务是使用从 0 到 n - 1 的数字创建若干组,并确保每个数字 i 在 所有组 中使用的次数总共不超过 usageLimits[i] 次。此外,还必须满足以下条件:
* 每个组必须由 不同 的数字组成,也就是说,单个组内不能存在重复的数字。
* 每个组(除了第一个)的长度必须 严格大于 前一个组。
在满足所有条件的情况下,以整数形式返回可以创建的最大组数。
示例 1:
输入:usageLimits = [1,2,5]
输出:3
解释:在这个示例中,我们可以使用 0 至多一次,使用 1 至多 2 次,使用 2 至多 5 次。
一种既能满足所有条件,又能创建最多组的方式是:
组 1 包含数字 [2] 。
组 2 包含数字 [1,2] 。
组 3 包含数字 [0,1,2] 。
可以证明能够创建的最大组数是 3 。
所以,输出是 3 。
示例 2:
输入:usageLimits = [2,1,2]
输出:2
解释:在这个示例中,我们可以使用 0 至多 2 次,使用 1 至多 1 次,使用 2 至多 2 次。
一种既能满足所有条件,又能创建最多组的方式是:
组 1 包含数字 [0] 。
组 2 包含数字 [1,2] 。
可以证明能够创建的最大组数是 2 。
所以,输出是 2 。
示例 3:
输入:usageLimits = [1,1]
输出:1
解释:在这个示例中,我们可以使用 0 和 1 至多 1 次。
一种既能满足所有条件,又能创建最多组的方式是:
组 1 包含数字 [0] 。
可以证明能够创建的最大组数是 1 。
所以,输出是 1 。
提示:
* 1 <= usageLimits.length <= 105
* 1 <= usageLimits[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxIncreasingGroups(self, usageLimits: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "usageLimits": [1,2,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,1,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [1,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 1
test_input = { "usageLimits": [1,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [1,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [1,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [1,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [2,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [3,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [3,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [3,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [3,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [4,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [4,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [4,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [4,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [4,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [4,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [5,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [5,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,6] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [6,19] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [7,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [7,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [7,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [7,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [7,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [7,13] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [8,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [8,6] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [9,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [9,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [9,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [9,6] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [9,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [9,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [10,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [10,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [10,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [10,11] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [13,11] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [13,13] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [16,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [18,6] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [32,42] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [1,1,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [1,1,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [1,4,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,4,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,6,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,6,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,7,19] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,8,6] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,9,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,9,6] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [1,10,6] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,2,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,3,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,6,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,7,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,7,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,8,7] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [2,9,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,1,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 2
test_input = { "usageLimits": [3,5,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,5,8] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,6,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,7,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,7,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,8,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,9,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [3,10,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,2,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,2,15] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,5,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,7,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,8,2] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,8,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,10,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [4,10,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [5,1,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [5,2,9] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [5,2,10] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [5,6,1] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [5,6,5] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [5,7,4] }
assert my_solution.maxIncreasingGroups(**test_input) == 3
test_input = { "usageLimits": [5,10,3] }
assert my_solution.maxIncreasingGroups(**test_input) == 3 | 1,690,079,400 |
weekly-contest-355-count-paths-that-can-form-a-palindrome-in-a-tree | https://leetcode.com/problems/count-paths-that-can-form-a-palindrome-in-a-tree | count-paths-that-can-form-a-palindrome-in-a-tree | {
"questionId": "2905",
"questionFrontendId": "2791",
"title": "Count Paths That Can Form a Palindrome in a Tree",
"titleSlug": "count-paths-that-can-form-a-palindrome-in-a-tree",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 343,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
给你一棵 树(即,一个连通、无向且无环的图),根 节点为 0 ,由编号从 0 到 n - 1 的 n 个节点组成。这棵树用一个长度为 n 、下标从 0 开始的数组 parent 表示,其中 parent[i] 为节点 i 的父节点,由于节点 0 为根节点,所以 parent[0] == -1 。
另给你一个长度为 n 的字符串 s ,其中 s[i] 是分配给 i 和 parent[i] 之间的边的字符。s[0] 可以忽略。
找出满足 u < v ,且从 u 到 v 的路径上分配的字符可以 重新排列 形成 回文 的所有节点对 (u, v) ,并返回节点对的数目。
如果一个字符串正着读和反着读都相同,那么这个字符串就是一个 回文 。
示例 1:
[https://assets.leetcode.com/uploads/2023/07/15/treedrawio-8drawio.png]
输入:parent = [-1,0,0,1,1,2], s = "acaabc"
输出:8
解释:符合题目要求的节点对分别是:
- (0,1)、(0,2)、(1,3)、(1,4) 和 (2,5) ,路径上只有一个字符,满足回文定义。
- (2,3),路径上字符形成的字符串是 "aca" ,满足回文定义。
- (1,5),路径上字符形成的字符串是 "cac" ,满足回文定义。
- (3,5),路径上字符形成的字符串是 "acac" ,可以重排形成回文 "acca" 。
示例 2:
输入:parent = [-1,0,0,0,0], s = "aaaaa"
输出:10
解释:任何满足 u < v 的节点对 (u,v) 都符合题目要求。
提示:
* n == parent.length == s.length
* 1 <= n <= 105
* 对于所有 i >= 1 ,0 <= parent[i] <= n - 1 均成立
* parent[0] == -1
* parent 表示一棵有效的树
* s 仅由小写英文字母组成
"""
class Solution:
def countPalindromePaths(self, parent: List[int], s: str) -> int:
| 给你一棵 树(即,一个连通、无向且无环的图),根 节点为 0 ,由编号从 0 到 n - 1 的 n 个节点组成。这棵树用一个长度为 n 、下标从 0 开始的数组 parent 表示,其中 parent[i] 为节点 i 的父节点,由于节点 0 为根节点,所以 parent[0] == -1 。
另给你一个长度为 n 的字符串 s ,其中 s[i] 是分配给 i 和 parent[i] 之间的边的字符。s[0] 可以忽略。
找出满足 u < v ,且从 u 到 v 的路径上分配的字符可以 重新排列 形成 回文 的所有节点对 (u, v) ,并返回节点对的数目。
如果一个字符串正着读和反着读都相同,那么这个字符串就是一个 回文 。
示例 1:
[https://assets.leetcode.com/uploads/2023/07/15/treedrawio-8drawio.png]
输入:parent = [-1,0,0,1,1,2], s = "acaabc"
输出:8
解释:符合题目要求的节点对分别是:
- (0,1)、(0,2)、(1,3)、(1,4) 和 (2,5) ,路径上只有一个字符,满足回文定义。
- (2,3),路径上字符形成的字符串是 "aca" ,满足回文定义。
- (1,5),路径上字符形成的字符串是 "cac" ,满足回文定义。
- (3,5),路径上字符形成的字符串是 "acac" ,可以重排形成回文 "acca" 。
示例 2:
输入:parent = [-1,0,0,0,0], s = "aaaaa"
输出:10
解释:任何满足 u < v 的节点对 (u,v) 都符合题目要求。
提示:
* n == parent.length == s.length
* 1 <= n <= 105
* 对于所有 i >= 1 ,0 <= parent[i] <= n - 1 均成立
* parent[0] == -1
* parent 表示一棵有效的树
* s 仅由小写英文字母组成
请完成下面的代码来解决上述问题:
```python
class Solution:
def countPalindromePaths(self, parent: List[int], s: str) -> int:
``` |
my_solution = Solution()
test_input = { "parent": [-1,0,0,1,1,2], "s": "acaabc" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,0,0,0,0], "s": "aaaaa" }
assert my_solution.countPalindromePaths(**test_input) == 10
test_input = { "parent": [-1,0], "s": "pi" }
assert my_solution.countPalindromePaths(**test_input) == 1
test_input = { "parent": [-1,5,0,5,5,2], "s": "xsbcqq" }
assert my_solution.countPalindromePaths(**test_input) == 7
test_input = { "parent": [-1,6,8,5,0,4,2,0,4], "s": "tiaiaivea" }
assert my_solution.countPalindromePaths(**test_input) == 20
test_input = { "parent": [-1,0,0,0,1,3,7,2], "s": "pxxgtgpp" }
assert my_solution.countPalindromePaths(**test_input) == 18
test_input = { "parent": [-1,0,1,4,1,0], "s": "hfhmmf" }
assert my_solution.countPalindromePaths(**test_input) == 12
test_input = { "parent": [-1,0,1], "s": "cri" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,0,5,7,0,0,1,2,0], "s": "snlzlzngna" }
assert my_solution.countPalindromePaths(**test_input) == 19
test_input = { "parent": [-1,0,0], "s": "sat" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0], "s": "ko" }
assert my_solution.countPalindromePaths(**test_input) == 1
test_input = { "parent": [-1,0,6,4,1,6,3,1], "s": "jibwrfoi" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,4,0,4,6,0,5,5], "s": "bhrlorou" }
assert my_solution.countPalindromePaths(**test_input) == 18
test_input = { "parent": [-1,4,4,6,0,7,1,1], "s": "boqvndoo" }
assert my_solution.countPalindromePaths(**test_input) == 18
test_input = { "parent": [-1,2,0,1,0], "s": "eixnx" }
assert my_solution.countPalindromePaths(**test_input) == 6
test_input = { "parent": [-1,5,0,2,2,3], "s": "jwlllw" }
assert my_solution.countPalindromePaths(**test_input) == 14
test_input = { "parent": [-1,2,6,2,5,2,7,0], "s": "pipfippl" }
assert my_solution.countPalindromePaths(**test_input) == 15
test_input = { "parent": [-1,6,0,6,5,6,2,6,5,6], "s": "zwrdhnhwtf" }
assert my_solution.countPalindromePaths(**test_input) == 11
test_input = { "parent": [-1,4,1,1,5,0], "s": "gndfnj" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,0,0,0,1], "s": "crwkr" }
assert my_solution.countPalindromePaths(**test_input) == 7
test_input = { "parent": [-1,4,4,4,0,2,0], "s": "wwwwewd" }
assert my_solution.countPalindromePaths(**test_input) == 13
test_input = { "parent": [-1,2,0], "s": "mwz" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,1,6,5,0,1,1,1,0], "s": "mqgmjzrwuq" }
assert my_solution.countPalindromePaths(**test_input) == 14
test_input = { "parent": [-1,2,3,0,3,3,3,3], "s": "bnievjov" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,0,0], "s": "kyr" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,5,1,2,3,2,3,9,4], "s": "xukzwzsnww" }
assert my_solution.countPalindromePaths(**test_input) == 18
test_input = { "parent": [-1,6,0,1,5,0,2,2,6,0], "s": "snlpzocqpt" }
assert my_solution.countPalindromePaths(**test_input) == 10
test_input = { "parent": [-1,3,0,5,5,2], "s": "pxlxpl" }
assert my_solution.countPalindromePaths(**test_input) == 12
test_input = { "parent": [-1,5,5,5,5,0], "s": "ketewj" }
assert my_solution.countPalindromePaths(**test_input) == 6
test_input = { "parent": [-1,0,4,0,0], "s": "zrrqq" }
assert my_solution.countPalindromePaths(**test_input) == 7
test_input = { "parent": [-1,0,0], "s": "qlw" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,0], "s": "bds" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,2,6,6,0,4,4,2], "s": "odggsrsp" }
assert my_solution.countPalindromePaths(**test_input) == 13
test_input = { "parent": [-1,0,1], "s": "ldk" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,7,0,6,2,3,3,3], "s": "elllffflv" }
assert my_solution.countPalindromePaths(**test_input) == 28
test_input = { "parent": [-1,4,4,0,0], "s": "ntzhc" }
assert my_solution.countPalindromePaths(**test_input) == 4
test_input = { "parent": [-1,5,4,1,1,0,4], "s": "gmcmavf" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,0,0,4,0], "s": "aogkg" }
assert my_solution.countPalindromePaths(**test_input) == 6
test_input = { "parent": [-1,2,0], "s": "xmt" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,2,0], "s": "dff" }
assert my_solution.countPalindromePaths(**test_input) == 3
test_input = { "parent": [-1,0,1,1], "s": "lsvw" }
assert my_solution.countPalindromePaths(**test_input) == 3
test_input = { "parent": [-1,0,0], "s": "ovi" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,6,0,5,2,2,0,3], "s": "lpnfznpf" }
assert my_solution.countPalindromePaths(**test_input) == 19
test_input = { "parent": [-1,7,5,5,0,0,2,2], "s": "hqitxxwi" }
assert my_solution.countPalindromePaths(**test_input) == 16
test_input = { "parent": [-1,2,0], "s": "pyw" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,0,0], "s": "ybfa" }
assert my_solution.countPalindromePaths(**test_input) == 3
test_input = { "parent": [-1,3,6,6,6,0,0,4], "s": "ulicllkc" }
assert my_solution.countPalindromePaths(**test_input) == 11
test_input = { "parent": [-1,3,1,0], "s": "ukne" }
assert my_solution.countPalindromePaths(**test_input) == 3
test_input = { "parent": [-1,2,0,1,0,0], "s": "rhlxdd" }
assert my_solution.countPalindromePaths(**test_input) == 6
test_input = { "parent": [-1,4,5,0,2,0], "s": "zenbnb" }
assert my_solution.countPalindromePaths(**test_input) == 12
test_input = { "parent": [-1,0,5,4,5,1,3,9,7,4], "s": "jigognjnlb" }
assert my_solution.countPalindromePaths(**test_input) == 12
test_input = { "parent": [-1,4,4,6,3,0,5,3,6], "s": "imrcmdkew" }
assert my_solution.countPalindromePaths(**test_input) == 11
test_input = { "parent": [-1,4,4,0,6,4,0,0], "s": "dqpipiyz" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,0,0], "s": "mgm" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,4,0,1], "s": "pzlob" }
assert my_solution.countPalindromePaths(**test_input) == 4
test_input = { "parent": [-1,8,0,8,8,3,4,4,0,6], "s": "vvbvyovyvy" }
assert my_solution.countPalindromePaths(**test_input) == 30
test_input = { "parent": [-1,0,4,0,5,1,1], "s": "dvrvpea" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,0,1], "s": "jnx" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0,1,2], "s": "wxjj" }
assert my_solution.countPalindromePaths(**test_input) == 5
test_input = { "parent": [-1,3,3,0,1,2,1], "s": "erorchx" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,0,4,1,1,4,3], "s": "ywzwzcw" }
assert my_solution.countPalindromePaths(**test_input) == 14
test_input = { "parent": [-1,4,6,2,3,3,0], "s": "bititzq" }
assert my_solution.countPalindromePaths(**test_input) == 10
test_input = { "parent": [-1,2,0], "s": "uup" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,2,3,0,0], "s": "siiou" }
assert my_solution.countPalindromePaths(**test_input) == 6
test_input = { "parent": [-1,2,0,2,0], "s": "dilfs" }
assert my_solution.countPalindromePaths(**test_input) == 4
test_input = { "parent": [-1,3,7,0,1,0,2,3,0], "s": "wqojvjoqq" }
assert my_solution.countPalindromePaths(**test_input) == 22
test_input = { "parent": [-1,0], "s": "hi" }
assert my_solution.countPalindromePaths(**test_input) == 1
test_input = { "parent": [-1,3,1,4,0,6,3,1], "s": "fwvwwqqw" }
assert my_solution.countPalindromePaths(**test_input) == 21
test_input = { "parent": [-1,0,4,2,1,1,1], "s": "hkmmkmk" }
assert my_solution.countPalindromePaths(**test_input) == 16
test_input = { "parent": [-1,4,3,1,0], "s": "bzeez" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,3,3,0], "s": "zyuj" }
assert my_solution.countPalindromePaths(**test_input) == 3
test_input = { "parent": [-1,0,1,4,6,2,2], "s": "tlcpcll" }
assert my_solution.countPalindromePaths(**test_input) == 13
test_input = { "parent": [-1,2,0], "s": "vjw" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,0], "s": "nz" }
assert my_solution.countPalindromePaths(**test_input) == 1
test_input = { "parent": [-1,0,0], "s": "iot" }
assert my_solution.countPalindromePaths(**test_input) == 2
test_input = { "parent": [-1,2,8,8,3,1,2,0,0], "s": "rlurrluxm" }
assert my_solution.countPalindromePaths(**test_input) == 19
test_input = { "parent": [-1,5,0,0,0,4], "s": "zxrddx" }
assert my_solution.countPalindromePaths(**test_input) == 10
test_input = { "parent": [-1,0,1,2], "s": "tffc" }
assert my_solution.countPalindromePaths(**test_input) == 5
test_input = { "parent": [-1,5,3,0,3,0], "s": "ltdewr" }
assert my_solution.countPalindromePaths(**test_input) == 5
test_input = { "parent": [-1,0,4,5,0,1], "s": "gjwwjv" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,7,7,0,8,2,2,4,3,8], "s": "cnssgsoogy" }
assert my_solution.countPalindromePaths(**test_input) == 20
test_input = { "parent": [-1,7,1,6,0,0,7,0], "s": "zaceoxax" }
assert my_solution.countPalindromePaths(**test_input) == 13
test_input = { "parent": [-1,0,1,0,3,0], "s": "zrvyyc" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,4,9,0,3,3,7,4,2,0], "s": "qxixxxxxix" }
assert my_solution.countPalindromePaths(**test_input) == 41
test_input = { "parent": [-1,2,0,5,5,2,5], "s": "hoptvuu" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,7,3,0,3,1,0,3], "s": "kopflpao" }
assert my_solution.countPalindromePaths(**test_input) == 13
test_input = { "parent": [-1,0,1,6,1,1,1], "s": "bhhslhl" }
assert my_solution.countPalindromePaths(**test_input) == 11
test_input = { "parent": [-1,0,0,1,2], "s": "khhch" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,0,3,0], "s": "koss" }
assert my_solution.countPalindromePaths(**test_input) == 5
test_input = { "parent": [-1,3,4,4,0], "s": "owzoq" }
assert my_solution.countPalindromePaths(**test_input) == 4
test_input = { "parent": [-1,2,0,1,1], "s": "zqqww" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,0,1,1,1,2,2], "s": "lacccdb" }
assert my_solution.countPalindromePaths(**test_input) == 13
test_input = { "parent": [-1,4,3,4,6,6,7,0,6], "s": "sjoooorrm" }
assert my_solution.countPalindromePaths(**test_input) == 24
test_input = { "parent": [-1,7,4,2,0,6,2,3,7], "s": "guqyxxtau" }
assert my_solution.countPalindromePaths(**test_input) == 9
test_input = { "parent": [-1,4,4,0,0], "s": "scicc" }
assert my_solution.countPalindromePaths(**test_input) == 8
test_input = { "parent": [-1,5,8,6,3,0,2,3,1], "s": "naphhahhp" }
assert my_solution.countPalindromePaths(**test_input) == 29
test_input = { "parent": [-1,3,8,8,5,0,4,0,0,2], "s": "ciiyggofij" }
assert my_solution.countPalindromePaths(**test_input) == 23
test_input = { "parent": [-1,2,0,2,2,3,7,8,4,2], "s": "stthtthddt" }
assert my_solution.countPalindromePaths(**test_input) == 34
test_input = { "parent": [-1,2,0,7,3,7,4,0], "s": "mjjsjdsj" }
assert my_solution.countPalindromePaths(**test_input) == 18
test_input = { "parent": [-1,0], "s": "ey" }
assert my_solution.countPalindromePaths(**test_input) == 1 | 1,690,079,400 |
biweekly-contest-109-check-if-array-is-good | https://leetcode.com/problems/check-if-array-is-good | check-if-array-is-good | {
"questionId": "2892",
"questionFrontendId": "2784",
"title": "Check if Array is Good",
"titleSlug": "check-if-array-is-good",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 233,
"dislikes": 43,
"categoryTitle": "Algorithms"
} | """
给你一个整数数组 nums ,如果它是数组 base[n] 的一个排列,我们称它是个 好 数组。
base[n] = [1, 2, ..., n - 1, n, n] (换句话说,它是一个长度为 n + 1 且包含 1 到 n - 1 恰好各一次,包含 n 两次的一个数组)。比方说,base[1] = [1, 1] ,base[3] = [1, 2, 3, 3] 。
如果数组是一个好数组,请你返回 true ,否则返回 false 。
注意:数组的排列是这些数字按任意顺序排布后重新得到的数组。
示例 1:
输入:nums = [2, 1, 3]
输出:false
解释:因为数组的最大元素是 3 ,唯一可以构成这个数组的 base[n] 对应的 n = 3 。但是 base[3] 有 4 个元素,但数组 nums 只有 3 个元素,所以无法得到 base[3] = [1, 2, 3, 3] 的排列,所以答案为 false 。
示例 2:
输入:nums = [1, 3, 3, 2]
输出:true
解释:因为数组的最大元素是 3 ,唯一可以构成这个数组的 base[n] 对应的 n = 3 ,可以看出数组是 base[3] = [1, 2, 3, 3] 的一个排列(交换 nums 中第二个和第四个元素)。所以答案为 true 。
示例 3:
输入:nums = [1, 1]
输出:true
解释:因为数组的最大元素是 1 ,唯一可以构成这个数组的 base[n] 对应的 n = 1,可以看出数组是 base[1] = [1, 1] 的一个排列。所以答案为 true 。
示例 4:
输入:nums = [3, 4, 4, 1, 2, 1]
输出:false
解释:因为数组的最大元素是 4 ,唯一可以构成这个数组的 base[n] 对应的 n = 4 。但是 base[n] 有 5 个元素而 nums 有 6 个元素。所以答案为 false 。
提示:
* 1 <= nums.length <= 100
* 1 <= num[i] <= 200
"""
class Solution:
def isGood(self, nums: List[int]) -> bool:
| 给你一个整数数组 nums ,如果它是数组 base[n] 的一个排列,我们称它是个 好 数组。
base[n] = [1, 2, ..., n - 1, n, n] (换句话说,它是一个长度为 n + 1 且包含 1 到 n - 1 恰好各一次,包含 n 两次的一个数组)。比方说,base[1] = [1, 1] ,base[3] = [1, 2, 3, 3] 。
如果数组是一个好数组,请你返回 true ,否则返回 false 。
注意:数组的排列是这些数字按任意顺序排布后重新得到的数组。
示例 1:
输入:nums = [2, 1, 3]
输出:false
解释:因为数组的最大元素是 3 ,唯一可以构成这个数组的 base[n] 对应的 n = 3 。但是 base[3] 有 4 个元素,但数组 nums 只有 3 个元素,所以无法得到 base[3] = [1, 2, 3, 3] 的排列,所以答案为 false 。
示例 2:
输入:nums = [1, 3, 3, 2]
输出:true
解释:因为数组的最大元素是 3 ,唯一可以构成这个数组的 base[n] 对应的 n = 3 ,可以看出数组是 base[3] = [1, 2, 3, 3] 的一个排列(交换 nums 中第二个和第四个元素)。所以答案为 true 。
示例 3:
输入:nums = [1, 1]
输出:true
解释:因为数组的最大元素是 1 ,唯一可以构成这个数组的 base[n] 对应的 n = 1,可以看出数组是 base[1] = [1, 1] 的一个排列。所以答案为 true 。
示例 4:
输入:nums = [3, 4, 4, 1, 2, 1]
输出:false
解释:因为数组的最大元素是 4 ,唯一可以构成这个数组的 base[n] 对应的 n = 4 。但是 base[n] 有 5 个元素而 nums 有 6 个元素。所以答案为 false 。
提示:
* 1 <= nums.length <= 100
* 1 <= num[i] <= 200
请完成下面的代码来解决上述问题:
```python
class Solution:
def isGood(self, nums: List[int]) -> bool:
``` |
my_solution = Solution()
test_input = { "nums": [1, 3, 3, 2] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [2, 1, 3] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [1, 1] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 2, 2] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [3, 4, 4, 1, 2, 1] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [2, 2, 1] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [2] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [3] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [3, 3, 1, 2] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 3, 4, 4, 2] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [4] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [1, 4, 2, 4, 3] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [5] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [4, 4, 1, 2, 3] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [6] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [4, 4, 2, 3, 1] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [8] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [9] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [5, 5, 1, 3, 2, 4] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 5, 3, 6, 6, 4, 2] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [5, 4, 6, 6, 3, 2, 1] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [10] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [6, 3, 6, 4, 2, 1, 5] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [6, 3, 6, 5, 1, 2, 4] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [12] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [7, 2, 5, 7, 3, 6, 1, 4] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 2, 8, 8, 6, 5, 4, 3, 7] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 8, 4, 8, 2, 7, 6, 3, 5] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [13] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [14] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [2, 8, 6, 1, 7, 5, 3, 4, 8] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [4, 1, 3, 2, 8, 5, 8, 6, 7] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [15] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [7, 2, 1, 5, 6, 8, 8, 4, 3] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [82] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [1, 8] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [8, 6, 2, 5, 7, 4, 1, 8, 3] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [4, 6, 1, 9, 8, 9, 5, 7, 3, 2] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 13] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [2, 3] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [9, 5, 8, 9, 6, 1, 2, 7, 4, 3] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 2, 10, 9, 10, 5, 6, 4, 8, 7, 3] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 9, 2, 6, 5, 4, 7, 10, 3, 10, 8] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [1, 10, 7, 8, 10, 4, 6, 3, 5, 2, 9] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [8, 2, 1, 4, 3, 10, 9, 5, 10, 7, 6] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [10, 9, 5, 3, 6, 4, 2, 10, 8, 7, 1] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [2, 9] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [1, 4, 5, 10, 11, 2, 9, 7, 6, 11, 3, 8] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [2, 11] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [11, 5, 9, 10, 3, 11, 1, 2, 8, 4, 7, 6] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [11, 9, 8, 1, 12, 4, 2, 15, 16, 10, 13, 6, 3, 16, 7, 5, 14] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [11, 16, 9, 5, 14, 13, 4, 1, 3, 16, 15, 8, 10, 7, 12, 2, 6] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [2, 12] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [17, 1, 18, 11, 9, 4, 7, 6, 3, 21, 16, 14, 10, 8, 20, 21, 5, 2, 12, 19, 15, 13] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [3, 1] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [20, 13, 7, 10, 16, 12, 19, 2, 21, 17, 3, 11, 5, 15, 21, 1, 18, 6, 8, 14, 9, 4] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [8, 19, 16, 17, 20, 15, 11, 4, 22, 3, 13, 10, 1, 18, 9, 12, 22, 7, 6, 2, 5, 21, 14] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [22, 1, 17, 13, 8, 22, 9, 5, 21, 6, 14, 12, 10, 11, 2, 18, 4, 7, 19, 20, 3, 15, 16] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [21, 15, 16, 13, 3, 4, 11, 22, 7, 14, 20, 10, 18, 17, 6, 8, 9, 1, 19, 5, 2, 12, 23, 24, 24] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [22, 24, 24, 12, 17, 15, 14, 16, 8, 11, 23, 5, 2, 10, 6, 21, 9, 13, 3, 20, 19, 7, 4, 18, 1] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [3, 2] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [6, 16, 26, 9, 4, 24, 12, 26, 22, 3, 11, 23, 15, 2, 17, 5, 1, 21, 14, 19, 18, 20, 13, 25, 8, 7, 10] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [29, 24, 5, 6, 4, 25, 9, 8, 21, 13, 27, 7, 20, 18, 3, 15, 23, 28, 29, 19, 17, 10, 22, 26, 1, 11, 12, 14, 16, 2] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [25, 21, 6, 10, 20, 15, 16, 26, 7, 3, 30, 1, 12, 29, 11, 30, 14, 19, 2, 28, 23, 9, 8, 24, 17, 5, 4, 27, 22, 18, 13] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [3, 11] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [2, 25, 20, 30, 4, 6, 1, 29, 15, 11, 10, 19, 14, 12, 32, 3, 21, 27, 16, 17, 28, 13, 5, 32, 8, 24, 22, 7, 31, 23, 18, 26, 9] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [4, 10] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [4, 12] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [5, 9] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [5, 12] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [5, 13] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [4, 29, 5, 24, 19, 1, 8, 31, 18, 21, 9, 28, 32, 27, 10, 17, 22, 2, 11, 15, 13, 23, 6, 16, 7, 30, 12, 33, 14, 25, 33, 26, 3, 20] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [6, 1] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [30, 32, 1, 35, 19, 34, 4, 23, 18, 6, 11, 8, 22, 33, 31, 16, 17, 20, 24, 10, 21, 7, 14, 15, 29, 9, 12, 2, 36, 3, 26, 36, 5, 13, 25, 27, 28] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [5, 34, 42, 17, 9, 44, 40, 24, 38, 21, 30, 14, 39, 11, 18, 36, 4, 43, 12, 32, 2, 6, 45, 46, 37, 47, 8, 3, 26, 1, 31, 28, 16, 20, 22, 35, 25, 15, 10, 29, 7, 27, 19, 33, 41, 47, 23, 13] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [6, 7] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [5, 41, 8, 2, 32, 24, 9, 44, 27, 6, 22, 36, 14, 21, 43, 28, 45, 37, 17, 18, 20, 26, 3, 12, 10, 33, 30, 13, 29, 38, 4, 47, 46, 15, 25, 11, 1, 19, 47, 16, 39, 31, 40, 34, 23, 7, 42, 35] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [6, 10] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [6, 12] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [6, 15] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [7, 9] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [8, 4] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [8, 6] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [26, 35, 9, 3, 46, 33, 13, 8, 47, 27, 17, 40, 15, 20, 37, 12, 16, 44, 34, 2, 14, 30, 1, 29, 10, 11, 25, 18, 43, 42, 6, 5, 47, 38, 41, 32, 24, 31, 7, 4, 45, 19, 39, 22, 28, 36, 21, 23] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [8, 9] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [8, 10] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [8, 13] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [32, 39, 25, 49, 6, 48, 9, 7, 34, 3, 8, 26, 14, 27, 43, 30, 1, 21, 36, 10, 31, 38, 40, 12, 2, 46, 20, 15, 11, 24, 22, 28, 33, 4, 19, 18, 44, 41, 35, 29, 16, 37, 45, 47, 49, 23, 42, 13, 5, 17] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [9, 7] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [18, 35, 44, 41, 8, 33, 28, 9, 3, 14, 43, 56, 6, 10, 25, 53, 61, 22, 17, 23, 32, 50, 31, 13, 1, 29, 45, 34, 30, 48, 36, 58, 46, 15, 4, 7, 52, 60, 16, 12, 54, 19, 24, 40, 26, 55, 49, 42, 21, 38, 2, 20, 57, 61, 5, 37, 47, 27, 39, 11, 51, 59] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [9, 9] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [10, 4] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [10, 11] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [11, 1] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [60, 62, 16, 59, 8, 49, 51, 41, 42, 40, 46, 3, 10, 13, 53, 2, 63, 54, 32, 33, 31, 14, 12, 15, 1, 66, 61, 18, 52, 4, 55, 11, 26, 28, 47, 21, 25, 43, 65, 58, 45, 50, 17, 64, 23, 22, 30, 9, 38, 19, 24, 44, 37, 39, 48, 20, 35, 36, 27, 34, 56, 57, 29, 5, 7, 6, 66] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [46, 9, 21, 14, 24, 15, 6, 58, 22, 40, 63, 39, 49, 65, 30, 5, 43, 36, 29, 55, 67, 45, 61, 35, 67, 56, 16, 23, 50, 17, 19, 13, 26, 66, 47, 59, 2, 51, 27, 28, 31, 1, 44, 42, 53, 57, 11, 25, 4, 54, 37, 20, 48, 52, 41, 32, 10, 7, 64, 34, 60, 12, 33, 38, 3, 18, 62, 8] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [11, 5] }
assert my_solution.isGood(**test_input) == False
test_input = { "nums": [57, 40, 35, 55, 42, 24, 43, 29, 30, 59, 21, 52, 67, 72, 32, 78, 13, 51, 36, 48, 74, 64, 69, 65, 9, 4, 37, 31, 6, 27, 7, 2, 38, 61, 15, 19, 71, 49, 44, 47, 46, 54, 76, 26, 63, 17, 22, 3, 16, 12, 18, 41, 25, 62, 8, 10, 23, 50, 56, 11, 20, 5, 28, 77, 66, 53, 14, 33, 68, 34, 73, 45, 1, 78, 39, 70, 75, 60, 58] }
assert my_solution.isGood(**test_input) == True
test_input = { "nums": [14, 38, 18, 10, 21, 9, 67, 68, 23, 19, 80, 13, 74, 75, 32, 55, 65, 45, 5, 28, 43, 8, 17, 42, 40, 44, 31, 1, 7, 25, 6, 47, 27, 50, 4, 76, 63, 52, 71, 34, 83, 56, 77, 81, 16, 12, 60, 79, 46, 41, 24, 35, 33, 2, 49, 70, 78, 36, 48, 69, 26, 66, 37, 72, 61, 30, 22, 58, 20, 39, 82, 64, 73, 59, 57, 3, 51, 29, 83, 53, 15, 54, 11, 62] }
assert my_solution.isGood(**test_input) == True | 1,690,036,200 |
biweekly-contest-109-sort-vowels-in-a-string | https://leetcode.com/problems/sort-vowels-in-a-string | sort-vowels-in-a-string | {
"questionId": "2887",
"questionFrontendId": "2785",
"title": "Sort Vowels in a String",
"titleSlug": "sort-vowels-in-a-string",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 883,
"dislikes": 50,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的字符串 s ,将 s 中的元素重新 排列 得到新的字符串 t ,它满足:
* 所有辅音字母都在原来的位置上。更正式的,如果满足 0 <= i < s.length 的下标 i 处的 s[i] 是个辅音字母,那么 t[i] = s[i] 。
* 元音字母都必须以他们的 ASCII 值按 非递减 顺序排列。更正式的,对于满足 0 <= i < j < s.length 的下标 i 和 j ,如果 s[i] 和 s[j] 都是元音字母,那么 t[i] 的 ASCII 值不能大于 t[j] 的 ASCII 值。
请你返回结果字母串。
元音字母为 'a' ,'e' ,'i' ,'o' 和 'u' ,它们可能是小写字母也可能是大写字母,辅音字母是除了这 5 个字母以外的所有字母。
示例 1:
输入:s = "lEetcOde"
输出:"lEOtcede"
解释:'E' ,'O' 和 'e' 是 s 中的元音字母,'l' ,'t' ,'c' 和 'd' 是所有的辅音。将元音字母按照 ASCII 值排序,辅音字母留在原地。
示例 2:
输入:s = "lYmpH"
输出:"lYmpH"
解释:s 中没有元音字母(s 中都为辅音字母),所以我们返回 "lYmpH" 。
提示:
* 1 <= s.length <= 105
* s 只包含英语字母表中的 大写 和 小写 字母。
"""
class Solution:
def sortVowels(self, s: str) -> str:
| 给你一个下标从 0 开始的字符串 s ,将 s 中的元素重新 排列 得到新的字符串 t ,它满足:
* 所有辅音字母都在原来的位置上。更正式的,如果满足 0 <= i < s.length 的下标 i 处的 s[i] 是个辅音字母,那么 t[i] = s[i] 。
* 元音字母都必须以他们的 ASCII 值按 非递减 顺序排列。更正式的,对于满足 0 <= i < j < s.length 的下标 i 和 j ,如果 s[i] 和 s[j] 都是元音字母,那么 t[i] 的 ASCII 值不能大于 t[j] 的 ASCII 值。
请你返回结果字母串。
元音字母为 'a' ,'e' ,'i' ,'o' 和 'u' ,它们可能是小写字母也可能是大写字母,辅音字母是除了这 5 个字母以外的所有字母。
示例 1:
输入:s = "lEetcOde"
输出:"lEOtcede"
解释:'E' ,'O' 和 'e' 是 s 中的元音字母,'l' ,'t' ,'c' 和 'd' 是所有的辅音。将元音字母按照 ASCII 值排序,辅音字母留在原地。
示例 2:
输入:s = "lYmpH"
输出:"lYmpH"
解释:s 中没有元音字母(s 中都为辅音字母),所以我们返回 "lYmpH" 。
提示:
* 1 <= s.length <= 105
* s 只包含英语字母表中的 大写 和 小写 字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def sortVowels(self, s: str) -> str:
``` |
my_solution = Solution()
test_input = { "s": "lEetcOde" }
assert my_solution.sortVowels(**test_input) == "lEOtcede"
test_input = { "s": "lYmpH" }
assert my_solution.sortVowels(**test_input) == "lYmpH"
test_input = { "s": "mDVD" }
assert my_solution.sortVowels(**test_input) == "mDVD"
test_input = { "s": "xdX" }
assert my_solution.sortVowels(**test_input) == "xdX"
test_input = { "s": "xdE" }
assert my_solution.sortVowels(**test_input) == "xdE"
test_input = { "s": "RiQYo" }
assert my_solution.sortVowels(**test_input) == "RiQYo"
test_input = { "s": "LQRamBOHfq" }
assert my_solution.sortVowels(**test_input) == "LQROmBaHfq"
test_input = { "s": "UpjPbEnOj" }
assert my_solution.sortVowels(**test_input) == "EpjPbOnUj"
test_input = { "s": "ziF" }
assert my_solution.sortVowels(**test_input) == "ziF"
test_input = { "s": "WxkKdjhL" }
assert my_solution.sortVowels(**test_input) == "WxkKdjhL"
test_input = { "s": "uZcPmqAd" }
assert my_solution.sortVowels(**test_input) == "AZcPmqud"
test_input = { "s": "UjshJXjkjS" }
assert my_solution.sortVowels(**test_input) == "UjshJXjkjS"
test_input = { "s": "nElwWTQHJ" }
assert my_solution.sortVowels(**test_input) == "nElwWTQHJ"
test_input = { "s": "kwcJqvsgM" }
assert my_solution.sortVowels(**test_input) == "kwcJqvsgM"
test_input = { "s": "z" }
assert my_solution.sortVowels(**test_input) == "z"
test_input = { "s": "Pz" }
assert my_solution.sortVowels(**test_input) == "Pz"
test_input = { "s": "T" }
assert my_solution.sortVowels(**test_input) == "T"
test_input = { "s": "syRWvFi" }
assert my_solution.sortVowels(**test_input) == "syRWvFi"
test_input = { "s": "G" }
assert my_solution.sortVowels(**test_input) == "G"
test_input = { "s": "MuQYHVy" }
assert my_solution.sortVowels(**test_input) == "MuQYHVy"
test_input = { "s": "gsc" }
assert my_solution.sortVowels(**test_input) == "gsc"
test_input = { "s": "nynBd" }
assert my_solution.sortVowels(**test_input) == "nynBd"
test_input = { "s": "qUSUCJeJZt" }
assert my_solution.sortVowels(**test_input) == "qUSUCJeJZt"
test_input = { "s": "PoEvPD" }
assert my_solution.sortVowels(**test_input) == "PEovPD"
test_input = { "s": "SrSuArHDvA" }
assert my_solution.sortVowels(**test_input) == "SrSAArHDvu"
test_input = { "s": "zI" }
assert my_solution.sortVowels(**test_input) == "zI"
test_input = { "s": "zpVZt" }
assert my_solution.sortVowels(**test_input) == "zpVZt"
test_input = { "s": "dZVLG" }
assert my_solution.sortVowels(**test_input) == "dZVLG"
test_input = { "s": "EHhQZGJBbp" }
assert my_solution.sortVowels(**test_input) == "EHhQZGJBbp"
test_input = { "s": "aPLCji" }
assert my_solution.sortVowels(**test_input) == "aPLCji"
test_input = { "s": "HSe" }
assert my_solution.sortVowels(**test_input) == "HSe"
test_input = { "s": "HvDMPPU" }
assert my_solution.sortVowels(**test_input) == "HvDMPPU"
test_input = { "s": "LYACGrvJLZ" }
assert my_solution.sortVowels(**test_input) == "LYACGrvJLZ"
test_input = { "s": "RepLvwHFI" }
assert my_solution.sortVowels(**test_input) == "RIpLvwHFe"
test_input = { "s": "vjbObvLfs" }
assert my_solution.sortVowels(**test_input) == "vjbObvLfs"
test_input = { "s": "sKQwLo" }
assert my_solution.sortVowels(**test_input) == "sKQwLo"
test_input = { "s": "PoqU" }
assert my_solution.sortVowels(**test_input) == "PUqo"
test_input = { "s": "QgUxRvJTfH" }
assert my_solution.sortVowels(**test_input) == "QgUxRvJTfH"
test_input = { "s": "wUMnwnblpu" }
assert my_solution.sortVowels(**test_input) == "wUMnwnblpu"
test_input = { "s": "JpqXrPuMd" }
assert my_solution.sortVowels(**test_input) == "JpqXrPuMd"
test_input = { "s": "wdtDPSQdKl" }
assert my_solution.sortVowels(**test_input) == "wdtDPSQdKl"
test_input = { "s": "Dl" }
assert my_solution.sortVowels(**test_input) == "Dl"
test_input = { "s": "v" }
assert my_solution.sortVowels(**test_input) == "v"
test_input = { "s": "axRukCyOHm" }
assert my_solution.sortVowels(**test_input) == "OxRakCyuHm"
test_input = { "s": "sQyytiAh" }
assert my_solution.sortVowels(**test_input) == "sQyytAih"
test_input = { "s": "ieTwHeOR" }
assert my_solution.sortVowels(**test_input) == "OeTwHeiR"
test_input = { "s": "LLxyZ" }
assert my_solution.sortVowels(**test_input) == "LLxyZ"
test_input = { "s": "s" }
assert my_solution.sortVowels(**test_input) == "s"
test_input = { "s": "oefu" }
assert my_solution.sortVowels(**test_input) == "eofu"
test_input = { "s": "XV" }
assert my_solution.sortVowels(**test_input) == "XV"
test_input = { "s": "VkfjDpSH" }
assert my_solution.sortVowels(**test_input) == "VkfjDpSH"
test_input = { "s": "rg" }
assert my_solution.sortVowels(**test_input) == "rg"
test_input = { "s": "ecV" }
assert my_solution.sortVowels(**test_input) == "ecV"
test_input = { "s": "RUnxytMua" }
assert my_solution.sortVowels(**test_input) == "RUnxytMau"
test_input = { "s": "gUyMeyzOZo" }
assert my_solution.sortVowels(**test_input) == "gOyMUyzeZo"
test_input = { "s": "WEir" }
assert my_solution.sortVowels(**test_input) == "WEir"
test_input = { "s": "zZWs" }
assert my_solution.sortVowels(**test_input) == "zZWs"
test_input = { "s": "WULsDqIhp" }
assert my_solution.sortVowels(**test_input) == "WILsDqUhp"
test_input = { "s": "pw" }
assert my_solution.sortVowels(**test_input) == "pw"
test_input = { "s": "nOWxdSzo" }
assert my_solution.sortVowels(**test_input) == "nOWxdSzo"
test_input = { "s": "NfK" }
assert my_solution.sortVowels(**test_input) == "NfK"
test_input = { "s": "wXRFu" }
assert my_solution.sortVowels(**test_input) == "wXRFu"
test_input = { "s": "XXtjDoinAD" }
assert my_solution.sortVowels(**test_input) == "XXtjDAinoD"
test_input = { "s": "SGUzEv" }
assert my_solution.sortVowels(**test_input) == "SGEzUv"
test_input = { "s": "RFOvEt" }
assert my_solution.sortVowels(**test_input) == "RFEvOt"
test_input = { "s": "umQePdr" }
assert my_solution.sortVowels(**test_input) == "emQuPdr"
test_input = { "s": "wRqZ" }
assert my_solution.sortVowels(**test_input) == "wRqZ"
test_input = { "s": "blu" }
assert my_solution.sortVowels(**test_input) == "blu"
test_input = { "s": "QeOQEatFaW" }
assert my_solution.sortVowels(**test_input) == "QEOQaatFeW"
test_input = { "s": "jzWiXrYa" }
assert my_solution.sortVowels(**test_input) == "jzWaXrYi"
test_input = { "s": "xs" }
assert my_solution.sortVowels(**test_input) == "xs"
test_input = { "s": "DwROc" }
assert my_solution.sortVowels(**test_input) == "DwROc"
test_input = { "s": "XMhLlJd" }
assert my_solution.sortVowels(**test_input) == "XMhLlJd"
test_input = { "s": "uAmir" }
assert my_solution.sortVowels(**test_input) == "Aimur"
test_input = { "s": "PTlFpeAI" }
assert my_solution.sortVowels(**test_input) == "PTlFpAIe"
test_input = { "s": "XLYy" }
assert my_solution.sortVowels(**test_input) == "XLYy"
test_input = { "s": "vA" }
assert my_solution.sortVowels(**test_input) == "vA"
test_input = { "s": "y" }
assert my_solution.sortVowels(**test_input) == "y"
test_input = { "s": "C" }
assert my_solution.sortVowels(**test_input) == "C"
test_input = { "s": "wrnMlek" }
assert my_solution.sortVowels(**test_input) == "wrnMlek"
test_input = { "s": "JWbfCfGgf" }
assert my_solution.sortVowels(**test_input) == "JWbfCfGgf"
test_input = { "s": "OPGlnq" }
assert my_solution.sortVowels(**test_input) == "OPGlnq"
test_input = { "s": "DeOMW" }
assert my_solution.sortVowels(**test_input) == "DOeMW"
test_input = { "s": "xG" }
assert my_solution.sortVowels(**test_input) == "xG"
test_input = { "s": "ZcaBhfkWC" }
assert my_solution.sortVowels(**test_input) == "ZcaBhfkWC"
test_input = { "s": "pKa" }
assert my_solution.sortVowels(**test_input) == "pKa"
test_input = { "s": "DXSEKrfJCe" }
assert my_solution.sortVowels(**test_input) == "DXSEKrfJCe"
test_input = { "s": "xA" }
assert my_solution.sortVowels(**test_input) == "xA"
test_input = { "s": "Jb" }
assert my_solution.sortVowels(**test_input) == "Jb"
test_input = { "s": "SBQT" }
assert my_solution.sortVowels(**test_input) == "SBQT"
test_input = { "s": "LWRfYb" }
assert my_solution.sortVowels(**test_input) == "LWRfYb"
test_input = { "s": "tvLWAeGDFK" }
assert my_solution.sortVowels(**test_input) == "tvLWAeGDFK"
test_input = { "s": "jFkj" }
assert my_solution.sortVowels(**test_input) == "jFkj"
test_input = { "s": "zC" }
assert my_solution.sortVowels(**test_input) == "zC"
test_input = { "s": "ikYSsAveh" }
assert my_solution.sortVowels(**test_input) == "AkYSsevih"
test_input = { "s": "YXkS" }
assert my_solution.sortVowels(**test_input) == "YXkS"
test_input = { "s": "SOEo" }
assert my_solution.sortVowels(**test_input) == "SEOo"
test_input = { "s": "qoJx" }
assert my_solution.sortVowels(**test_input) == "qoJx"
test_input = { "s": "qGJbgTQ" }
assert my_solution.sortVowels(**test_input) == "qGJbgTQ"
test_input = { "s": "yiYYO" }
assert my_solution.sortVowels(**test_input) == "yOYYi" | 1,690,036,200 |
biweekly-contest-109-visit-array-positions-to-maximize-score | https://leetcode.com/problems/visit-array-positions-to-maximize-score | visit-array-positions-to-maximize-score | {
"questionId": "2893",
"questionFrontendId": "2786",
"title": "Visit Array Positions to Maximize Score",
"titleSlug": "visit-array-positions-to-maximize-score",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 438,
"dislikes": 21,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 和一个正整数 x 。
你 一开始 在数组的位置 0 处,你可以按照下述规则访问数组中的其他位置:
* 如果你当前在位置 i ,那么你可以移动到满足 i < j 的 任意 位置 j 。
* 对于你访问的位置 i ,你可以获得分数 nums[i] 。
* 如果你从位置 i 移动到位置 j 且 nums[i] 和 nums[j] 的 奇偶性 不同,那么你将失去分数 x 。
请你返回你能得到的 最大 得分之和。
注意 ,你一开始的分数为 nums[0] 。
示例 1:
输入:nums = [2,3,6,1,9,2], x = 5
输出:13
解释:我们可以按顺序访问数组中的位置:0 -> 2 -> 3 -> 4 。
对应位置的值为 2 ,6 ,1 和 9 。因为 6 和 1 的奇偶性不同,所以下标从 2 -> 3 让你失去 x = 5 分。
总得分为:2 + 6 + 1 + 9 - 5 = 13 。
示例 2:
输入:nums = [2,4,6,8], x = 3
输出:20
解释:数组中的所有元素奇偶性都一样,所以我们可以将每个元素都访问一次,而且不会失去任何分数。
总得分为:2 + 4 + 6 + 8 = 20 。
提示:
* 2 <= nums.length <= 105
* 1 <= nums[i], x <= 106
"""
class Solution:
def maxScore(self, nums: List[int], x: int) -> int:
| 给你一个下标从 0 开始的整数数组 nums 和一个正整数 x 。
你 一开始 在数组的位置 0 处,你可以按照下述规则访问数组中的其他位置:
* 如果你当前在位置 i ,那么你可以移动到满足 i < j 的 任意 位置 j 。
* 对于你访问的位置 i ,你可以获得分数 nums[i] 。
* 如果你从位置 i 移动到位置 j 且 nums[i] 和 nums[j] 的 奇偶性 不同,那么你将失去分数 x 。
请你返回你能得到的 最大 得分之和。
注意 ,你一开始的分数为 nums[0] 。
示例 1:
输入:nums = [2,3,6,1,9,2], x = 5
输出:13
解释:我们可以按顺序访问数组中的位置:0 -> 2 -> 3 -> 4 。
对应位置的值为 2 ,6 ,1 和 9 。因为 6 和 1 的奇偶性不同,所以下标从 2 -> 3 让你失去 x = 5 分。
总得分为:2 + 6 + 1 + 9 - 5 = 13 。
示例 2:
输入:nums = [2,4,6,8], x = 3
输出:20
解释:数组中的所有元素奇偶性都一样,所以我们可以将每个元素都访问一次,而且不会失去任何分数。
总得分为:2 + 4 + 6 + 8 = 20 。
提示:
* 2 <= nums.length <= 105
* 1 <= nums[i], x <= 106
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxScore(self, nums: List[int], x: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,3,6,1,9,2], "x": 5 }
assert my_solution.maxScore(**test_input) == 13
test_input = { "nums": [2,4,6,8], "x": 3 }
assert my_solution.maxScore(**test_input) == 20
test_input = { "nums": [38,92,23,30,25,96,6,71,78,77,33,23,71,48,87,77,53,28,6,20,90,83,42,21,64,95,84,29,22,21,33,36,53,51,85,25,80,56,71,69,5,21,4,84,28,16,65,7], "x": 52 }
assert my_solution.maxScore(**test_input) == 1545
test_input = { "nums": [18,13,60,61,57,21,10,98,51,3,13,36,72,70,68,62,52,83,63,63,53,42,59,98,95,48,22,64,94,80,14,14], "x": 2 }
assert my_solution.maxScore(**test_input) == 1633
test_input = { "nums": [90,87,79,59,91,19,96], "x": 51 }
assert my_solution.maxScore(**test_input) == 419
test_input = { "nums": [96,81,48,3,60,78,74,82,14,7,87,72,42,41,80,4,92,82,59,16,19,94,70,45,83,58,2,91,11,96,17,62,79,34,44,47,89,76,85,21,5,57,35,51], "x": 24 }
assert my_solution.maxScore(**test_input) == 1952
test_input = { "nums": [99,88,98,15,34,40,29,81,2,6,12,9,82,93,5,81,84,71,83,31,12,22,9,65,56,9,68,79,39,84,50,7,25,3,49], "x": 19 }
assert my_solution.maxScore(**test_input) == 1363
test_input = { "nums": [8,50,65,85,8,73,55,50,29,95,5,68,52,79], "x": 74 }
assert my_solution.maxScore(**test_input) == 470
test_input = { "nums": [45,9,20,89,18,94,12,51,38,77,100,95,46,1,76,41,8,90,82,33,92,32,76,43,6,61,85,40,63,10,74,18,44,43,17,100,17,33,100,77,97,8,99,85,88,9,63,31,32], "x": 68 }
assert my_solution.maxScore(**test_input) == 1694
test_input = { "nums": [87,23,53,57,21,60,68,84,66,49,48,61,32,95,71,11,15,61,10,86,50,53,38,20,63], "x": 92 }
assert my_solution.maxScore(**test_input) == 814
test_input = { "nums": [39,47,76,64,90,17,30,57,19,40,9,76,68,33,36,61,19,93,8,1,31,2,55,70,24,85,97,40,35,93,56,67,64,67,52,2,75,13,89,97], "x": 77 }
assert my_solution.maxScore(**test_input) == 1390
test_input = { "nums": [92,87,85,27,27,10,24,94,26,78,24,61,4,46,3,76,29,65,52,61,34,67,74,61,90,40,81,60,10,98,87,57,28,77,55,33,10,91,57,72,3,72,4,39,99], "x": 70 }
assert my_solution.maxScore(**test_input) == 1551
test_input = { "nums": [20,90,68], "x": 39 }
assert my_solution.maxScore(**test_input) == 178
test_input = { "nums": [43,100,72,33,45,9,51,10,22,42,7,74,41,68,100,24,20,20,79,30,99,82], "x": 1 }
assert my_solution.maxScore(**test_input) == 1060
test_input = { "nums": [100,87,29,94,56,41,53,98,34,17,52,3,54,51,22,39,37,9,76], "x": 40 }
assert my_solution.maxScore(**test_input) == 670
test_input = { "nums": [55,37,87,45,96,7,66,62,91,51,33,92,65,99], "x": 81 }
assert my_solution.maxScore(**test_input) == 625
test_input = { "nums": [2,75,65,43], "x": 39 }
assert my_solution.maxScore(**test_input) == 146
test_input = { "nums": [74,82,80,95,72,23,49,43,76,28,87,27,58,39,7,77,26,63,56,96,77,75,82,60,90,69,83,20,13,82,16,90,40,23,36,17,77,15,18,10], "x": 40 }
assert my_solution.maxScore(**test_input) == 1571
test_input = { "nums": [75,99,45,34,63,19,71,48,73,66,2,14,76,41,92,23,6,31,49,6,70,40,69,25,97,58,20,84,21,37,100,75,73,10,59,87,30], "x": 96 }
assert my_solution.maxScore(**test_input) == 1181
test_input = { "nums": [9,58,17,54,91,90,32,6,13,67,24,80,8,56,29,66,85,38,45,13,20,73,16,98,28,56,23,2,47,85,11,97,72,2,28,52,33], "x": 90 }
assert my_solution.maxScore(**test_input) == 886
test_input = { "nums": [96,89,30,38,58,26,47,52,27,77,87,92,6], "x": 83 }
assert my_solution.maxScore(**test_input) == 423
test_input = { "nums": [35,11,46,34,57,2,21,98,85,7,65,26,22,14,48,14,38,72,56,63,73,11,70,92,62,3,9,72,32,99,8,71,85,66,73,29,74,88,41,24,21,40,41,19,49,90], "x": 11 }
assert my_solution.maxScore(**test_input) == 1948
test_input = { "nums": [31,17,68,37,56,25,43,71,46,59,6,30,98,69,91], "x": 78 }
assert my_solution.maxScore(**test_input) == 443
test_input = { "nums": [53,49,57,84,69,39,97,78,19,42,10,16,16,62,79,74,49,59,21,29,76,6,14,64,76,29,8,27,26], "x": 80 }
assert my_solution.maxScore(**test_input) == 855
test_input = { "nums": [42,60,23,29,66,46,82,83,97,56,71,39,19,31,23,60,34,63,14,73,4,92,37,65,50,49,100,72,63], "x": 88 }
assert my_solution.maxScore(**test_input) == 943
test_input = { "nums": [79,60,100,62,25,2,86,9,66,67,20,14,92,27,93,52,12,67,9,8,69,21,31,77,71,52,60], "x": 84 }
assert my_solution.maxScore(**test_input) == 906
test_input = { "nums": [40,54,14,66,95,97,3,10,34,100,68,35,54,35,48,3,79,69,71,2,44,82,85,67,47,5,37,61,68,64,61,49,36,87,77,57,69,31,40,45,50,17,2,50,71], "x": 69 }
assert my_solution.maxScore(**test_input) == 1564
test_input = { "nums": [53,15,2,58,28,93,55,41,88,69,93,67,67,40,37,99,17,30,10,7,94,50,73,53,37,84,50,37,81,91,72,28,22,22,67], "x": 82 }
assert my_solution.maxScore(**test_input) == 1165
test_input = { "nums": [41,35,43,93,79,62,66,16,92,29,74,67,93,100,56,73], "x": 69 }
assert my_solution.maxScore(**test_input) == 714
test_input = { "nums": [67,9,2,39,28,92,99,62,37,75,3,53,26,32,76,14,88,16,68,56,60], "x": 91 }
assert my_solution.maxScore(**test_input) == 727
test_input = { "nums": [95,29,91,86,23,30,46,95,6,84,62,23,71,6,13,19,25,65,29,6,65,92], "x": 28 }
assert my_solution.maxScore(**test_input) == 886
test_input = { "nums": [74,47,86,24,44,91,88,64,37], "x": 24 }
assert my_solution.maxScore(**test_input) == 436
test_input = { "nums": [36,62,82,46,40], "x": 54 }
assert my_solution.maxScore(**test_input) == 266
test_input = { "nums": [66,28,100,33,15,47,80,43,61,16,10], "x": 3 }
assert my_solution.maxScore(**test_input) == 487
test_input = { "nums": [62,1,66,47,85,69,35,42,42,7,20,91], "x": 41 }
assert my_solution.maxScore(**test_input) == 436
test_input = { "nums": [8,23,19,37,12,78,25,62,99,88,86,27,1,78,40,57,5,62,12,93,10,42], "x": 60 }
assert my_solution.maxScore(**test_input) == 578
test_input = { "nums": [35,49,85,37,74,50,77,21,68,49,86,92,36,31,70,66,10,75,6,70,55,72,40,99,24,74,55,46,4,46,22,36,58,36,68,68,54,9,36,76,57,83,86,92,6,47,44,31], "x": 55 }
assert my_solution.maxScore(**test_input) == 1797
test_input = { "nums": [75,96,53,79,89,57,75,94,24,75,71,8,44,70,12,92,38,24,3,38,88,10], "x": 31 }
assert my_solution.maxScore(**test_input) == 1057
test_input = { "nums": [93,68,62,23,56,95,7,38,43,87,76,60,34,32,40,4,49,15,41,18,76,50], "x": 46 }
assert my_solution.maxScore(**test_input) == 776
test_input = { "nums": [53,78,79,81,75,36,35,37], "x": 61 }
assert my_solution.maxScore(**test_input) == 360
test_input = { "nums": [60,55,100,61,23,45,43,31], "x": 62 }
assert my_solution.maxScore(**test_input) == 301
test_input = { "nums": [98,48,29,44,96,57], "x": 40 }
assert my_solution.maxScore(**test_input) == 303
test_input = { "nums": [96,18,77,100,88,19,41,32,15,41,14], "x": 30 }
assert my_solution.maxScore(**test_input) == 405
test_input = { "nums": [46,69,20,84,52,23,13,52,68,49,99,23,14,60,56,71,68,43,44,66,96,58,94], "x": 6 }
assert my_solution.maxScore(**test_input) == 1208
test_input = { "nums": [85,12], "x": 79 }
assert my_solution.maxScore(**test_input) == 85
test_input = { "nums": [63,95,35,79,39,14,55,5,44,57,31,23,67,61,75,61,40,51,55,27,53,100,15,100,23,89,76,99,31,47,49,52,47], "x": 49 }
assert my_solution.maxScore(**test_input) == 1419
test_input = { "nums": [60,94,97,97,57,16,45,84,10,44,16], "x": 10 }
assert my_solution.maxScore(**test_input) == 584
test_input = { "nums": [24,28,63,5,13,83,2,15,81,34,9,10,54,88,12,36,81,87,81,42,56,82,85,85,31,47,29,59,21,55,73,31,80,75,61,70,82,90,23,44,71,94], "x": 57 }
assert my_solution.maxScore(**test_input) == 1665
test_input = { "nums": [92,7,95,1,79,49,58,77,54,12,38,18,9,23,75,98,76,86,40,33,22,14,62,67,60,36,67,51,85,100,75,30,55,63,28,100,94,4], "x": 89 }
assert my_solution.maxScore(**test_input) == 1282
test_input = { "nums": [76,24,85,30,37,86,3,50,94,19,48,95,31], "x": 93 }
assert my_solution.maxScore(**test_input) == 441
test_input = { "nums": [50,19,12,63,20,33,21,77,25,24,46,22,46,57,86,65,13,99,36,23,85,99,10], "x": 7 }
assert my_solution.maxScore(**test_input) == 949
test_input = { "nums": [68,26,50,20,54,30,12,66,30,75,31,89,78,30,17,38,97,15,43,39,82,25,3,78,66,6,68,86,29,20,99], "x": 97 }
assert my_solution.maxScore(**test_input) == 985
test_input = { "nums": [62,1,18,37,87,73,16], "x": 44 }
assert my_solution.maxScore(**test_input) == 233
test_input = { "nums": [35,60,95,31,19,87,19,37,78,82,81,96,23,58,93,96,92,41,48,67,90,70,6,97,6,2,77,47,34,17,51,15,13,93,12,46], "x": 1 }
assert my_solution.maxScore(**test_input) == 1895
test_input = { "nums": [21,29], "x": 68 }
assert my_solution.maxScore(**test_input) == 50
test_input = { "nums": [37,98,2,60,89,82,99,80,28,54,12,15,16,88,82,72,63,8,45,56,99,19,29,38,26,35], "x": 94 }
assert my_solution.maxScore(**test_input) == 813
test_input = { "nums": [84,8,44,48,85,77,18,34,17,46,53,84,52,77,12,94,18,67,46,45], "x": 44 }
assert my_solution.maxScore(**test_input) == 684
test_input = { "nums": [50,1,21,95,20,65,80,75,47,74,95,23,89,61,48,25,84,76,81,51,52,37,84,24,15,32,11,88], "x": 65 }
assert my_solution.maxScore(**test_input) == 982
test_input = { "nums": [54,2,22,17,41,23,51,16,5,42,12,77,9,71,92,87,78,50,14,74,72,42,90], "x": 52 }
assert my_solution.maxScore(**test_input) == 780
test_input = { "nums": [3,34,22,49,66,40,13,7,71,35,1,96,36,83,31,55,60,20,90,76,51,95,21,47,82,91,75,99,72,48,53,2,56,64], "x": 70 }
assert my_solution.maxScore(**test_input) == 1105
test_input = { "nums": [75,34,33,97,3,25,4,71,8,73], "x": 27 }
assert my_solution.maxScore(**test_input) == 377
test_input = { "nums": [27,91,78,7,48,79,23,34,17,42,94,85,48,36,26,57,53,10,38,32,45,89,74,5,35,39,9,59,71,39,1,60,39,50,47,47,48,74,71,91,85,86,22], "x": 74 }
assert my_solution.maxScore(**test_input) == 1292
test_input = { "nums": [26,97,16,94,49,98,78,84,76,21,75,88,22,49,34,98,7,94,100,98,72,70,47,6,56,8,50,9,37,37,34,36,48,95,8,63,35,81,26,57,91,4,83,38,64,45,98,51], "x": 86 }
assert my_solution.maxScore(**test_input) == 1919
test_input = { "nums": [14,64,4,14,94,58,67,15,79,26,66,34,47,42,20,67,5,21,63,73,44,96,29,72,26,20,84,84,62,39,93,53,13,35,32,82,22,58], "x": 87 }
assert my_solution.maxScore(**test_input) == 1252
test_input = { "nums": [42,98,75,46,10,21,10,35,4,59,100,78,62,51,84,99,92,2,4,12,59,8,42,85,86,81,20,1,43,41,56,2,30,25,21,56,43,82,38,45,89,54,15,63,69,20,64], "x": 45 }
assert my_solution.maxScore(**test_input) == 1442
test_input = { "nums": [99,80,22,56,93,18,65,63,8,16,80], "x": 39 }
assert my_solution.maxScore(**test_input) == 465
test_input = { "nums": [2,17,64,100,23,2,8,93,31,6,16,28,32,98,18,33,22,54,73,35,47,16,76,74,17,5,6,1,7,19,100,17,70,98,94,5,78,38,10,80], "x": 59 }
assert my_solution.maxScore(**test_input) == 1246
test_input = { "nums": [15,10,55,18,55,54,63,79,97,9,98,10,95,3,88,43,75,17,19,36,64,44,85,10,45,42,58,75,79,7,55,75,50,89,8,89,58,87,30,36,59,59,25], "x": 4 }
assert my_solution.maxScore(**test_input) == 2072
test_input = { "nums": [97,60,79,8,79,39,37,66,78,58,32,59,83,23,36,82,34,70,17,17,33,91,1,55,54,45,30,11,30,19,8,8,98,36,39,30,87,34,99,83,6,90], "x": 91 }
assert my_solution.maxScore(**test_input) == 1214
test_input = { "nums": [52,83,17,67,51,47,8,86,59,56,96,74,36,38,73,96,95,50,25,45,5,48,16,3,65,22,92,11,80,46,15], "x": 4 }
assert my_solution.maxScore(**test_input) == 1497
test_input = { "nums": [38,97,36,48,88,68,66,39,40,36,39,53,96,21,3,28,86,94,31,53,76,24,54,45,10,99,92,21,52,25,15,42,12,17,89,51], "x": 14 }
assert my_solution.maxScore(**test_input) == 1547
test_input = { "nums": [6,13,9], "x": 98 }
assert my_solution.maxScore(**test_input) == 6
test_input = { "nums": [52,66,40,14,6,26,37,93,23,2,40,10,42,1,85,22,45,46,16,14,70,76,48,100,68,85,72,31,15,56,65,61,83,90,31,31,2,27,55,91,50,32], "x": 18 }
assert my_solution.maxScore(**test_input) == 1679
test_input = { "nums": [8,60,58], "x": 18 }
assert my_solution.maxScore(**test_input) == 126
test_input = { "nums": [74,24,7,80,13,46,52,19,20,6,70,95,20,82,97,32,28,16,4,21,19,56,9,56,30,99,64,94,61,5,28,51,58,49,49,92,68,66,17,84,54], "x": 51 }
assert my_solution.maxScore(**test_input) == 1331
test_input = { "nums": [99,30,17,54,77,71,48,19,80,43,20,59,95,76,64,32,29,84,80,33,90,11,76,65,76,51,50,36,99], "x": 6 }
assert my_solution.maxScore(**test_input) == 1533
test_input = { "nums": [21,99,29,76,1,25,62,67,82,90,99,12,51,53,62,78,41,14,55,66,90,73,30,76,97], "x": 60 }
assert my_solution.maxScore(**test_input) == 935
test_input = { "nums": [59,78,89,17,43,89,21,43,73,76,68,94,69,76,26,3,86,65,45,29,68,53,41,87,79,37,11,55,82,97,9,48,64,13,56,56,60,22,22,50,23,51,14,36,2], "x": 81 }
assert my_solution.maxScore(**test_input) == 1655
test_input = { "nums": [5,2,24,57,9,5,71,90,20,80,9,99,45,27,60,7,65,23,55,46,49,57,7,22,28,35], "x": 77 }
assert my_solution.maxScore(**test_input) == 661
test_input = { "nums": [76,60,37,9,31,30,86,64,83,71,70,18,32,74,38,11,6,4,9,62,52,14,20,41,60,54,40,15,90,52,27,46,47,1,7,79,22,49,99,82], "x": 100 }
assert my_solution.maxScore(**test_input) == 1230
test_input = { "nums": [20,89,67,20,1,84,36,92,41,79,35,85,58,76,42,12,96,38,44,93,54,80,44,49,55,6,34,84,3,74,13], "x": 23 }
assert my_solution.maxScore(**test_input) == 1403
test_input = { "nums": [69,86,56,72,35,8,57,10,42,90,92,46,7,22,69,16,62,9,57,74,52,49,14,23,13,43,73,63,88,18,31,89,94,3,23,14,39,82,70,78], "x": 95 }
assert my_solution.maxScore(**test_input) == 1234
test_input = { "nums": [28,66,78,21,47,6,18,60,8,82,34,19,62,26,34,56,59,56,7,75,35,42,19,23,92,88,83,65,74,24,69,83,12,63,4,71,78,40,64,98,15,17,81,19], "x": 84 }
assert my_solution.maxScore(**test_input) == 1430
test_input = { "nums": [77,48,31,26], "x": 53 }
assert my_solution.maxScore(**test_input) == 108
test_input = { "nums": [33,69,10,90,86,82,66,19,28,33,9,98,87,7,7,17,69,79,85,65,31,38,75], "x": 21 }
assert my_solution.maxScore(**test_input) == 1042
test_input = { "nums": [16,99,70,71,62,42], "x": 83 }
assert my_solution.maxScore(**test_input) == 190
test_input = { "nums": [55,64,59,68,50,32,56,75,84,53,97,7,40,62,56,80,36,52,43,77,82,47,7,96,94,43,77,71,36,92], "x": 48 }
assert my_solution.maxScore(**test_input) == 1267
test_input = { "nums": [61,12,92,54,88,10,49,19,83,24,82,29,64,96,67,12,27,97,15,96,35,43,92,96,28,84,49,72,16,92,29,41], "x": 73 }
assert my_solution.maxScore(**test_input) == 1151
test_input = { "nums": [90,84,13,56,24,54,29,20], "x": 31 }
assert my_solution.maxScore(**test_input) == 328
test_input = { "nums": [94,12,26,83,92,8,64,21,80,32,47,71,30], "x": 66 }
assert my_solution.maxScore(**test_input) == 460
test_input = { "nums": [42,11,1], "x": 16 }
assert my_solution.maxScore(**test_input) == 42
test_input = { "nums": [22,56,65,84,34,80,56,63,22,52,94,29,99,45,20,66,50,62,44,10,3,70,13,23,99,99,71,61,11,28,48,66,41,4,5,18,22,44,36,92,10,90,20], "x": 36 }
assert my_solution.maxScore(**test_input) == 1706
test_input = { "nums": [6,45,84,69,49,47,49,13,6,25,82,38,1,4,99,68,89,78,53,29,73,96,71,58,88,18,97,61,37,80,20,93,77,38,84], "x": 40 }
assert my_solution.maxScore(**test_input) == 1336
test_input = { "nums": [85,71,5,91,31,75,36,4,42,81,92,42,40,14,57,72,33,66,4,1,26,81,45,56,64,76,43,39,53,9,37,38,53,26,2,55,6,70,9,45,35,60,73,38,62,58,3], "x": 86 }
assert my_solution.maxScore(**test_input) == 1343
test_input = { "nums": [32,53,27,49,7,40,22,85,53,46,28,95,59,85,78,16,15,63,24,64,90,9,84,9,66,41,75,8,22,53,72,29,15,32,49,29,37,66,82,63,59,58], "x": 70 }
assert my_solution.maxScore(**test_input) == 1221
test_input = { "nums": [76,100,47,98,31,46,73,18,40,46,4,70,33,43,58,21,72,24,97,17,18,61,86,9,8,96,54,55], "x": 43 }
assert my_solution.maxScore(**test_input) == 997
test_input = { "nums": [78,66,10,19,59,87,27,40,49,80,25,3,33,54,29,97,9,36,73,80,59,68], "x": 97 }
assert my_solution.maxScore(**test_input) == 626
test_input = { "nums": [13,26,3,19,21,43,33,62,32,61,40,22,56,69,15,21,10,87,84,66,26,35,54,64,7,53,32,14,7], "x": 76 }
assert my_solution.maxScore(**test_input) == 649
test_input = { "nums": [73,93,27,67,11,40,18,88,78,77,79,80,15,100,83,33,36,63,90,44,89,23,25,79,56,41,8,62,32,98,58], "x": 10 }
assert my_solution.maxScore(**test_input) == 1641
test_input = { "nums": [38,97,76,72,85,23,70,90,89,1,65,50,1,93,41,33,94,43,45,39,98,52,85,18,70,79,79,33,22,93,72,25,20,42,19,66,64,64,95,29,3,75,54,40,17,86,71,23,26,23], "x": 66 }
assert my_solution.maxScore(**test_input) == 1683 | 1,690,036,200 |
biweekly-contest-109-ways-to-express-an-integer-as-sum-of-powers | https://leetcode.com/problems/ways-to-express-an-integer-as-sum-of-powers | ways-to-express-an-integer-as-sum-of-powers | {
"questionId": "2882",
"questionFrontendId": "2787",
"title": "Ways to Express an Integer as Sum of Powers",
"titleSlug": "ways-to-express-an-integer-as-sum-of-powers",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 346,
"dislikes": 11,
"categoryTitle": "Algorithms"
} | """
给你两个 正 整数 n 和 x 。
请你返回将 n 表示成一些 互不相同 正整数的 x 次幂之和的方案数。换句话说,你需要返回互不相同整数 [n1, n2, ..., nk] 的集合数目,满足 n = n1x + n2x + ... + nkx 。
由于答案可能非常大,请你将它对 109 + 7 取余后返回。
比方说,n = 160 且 x = 3 ,一个表示 n 的方法是 n = 23 + 33 + 53 。
示例 1:
输入:n = 10, x = 2
输出:1
解释:我们可以将 n 表示为:n = 32 + 12 = 10 。
这是唯一将 10 表达成不同整数 2 次方之和的方案。
示例 2:
输入:n = 4, x = 1
输出:2
解释:我们可以将 n 按以下方案表示:
- n = 41 = 4 。
- n = 31 + 11 = 4 。
提示:
* 1 <= n <= 300
* 1 <= x <= 5
"""
class Solution:
def numberOfWays(self, n: int, x: int) -> int:
| 给你两个 正 整数 n 和 x 。
请你返回将 n 表示成一些 互不相同 正整数的 x 次幂之和的方案数。换句话说,你需要返回互不相同整数 [n1, n2, ..., nk] 的集合数目,满足 n = n1x + n2x + ... + nkx 。
由于答案可能非常大,请你将它对 109 + 7 取余后返回。
比方说,n = 160 且 x = 3 ,一个表示 n 的方法是 n = 23 + 33 + 53 。
示例 1:
输入:n = 10, x = 2
输出:1
解释:我们可以将 n 表示为:n = 32 + 12 = 10 。
这是唯一将 10 表达成不同整数 2 次方之和的方案。
示例 2:
输入:n = 4, x = 1
输出:2
解释:我们可以将 n 按以下方案表示:
- n = 41 = 4 。
- n = 31 + 11 = 4 。
提示:
* 1 <= n <= 300
* 1 <= x <= 5
请完成下面的代码来解决上述问题:
```python
class Solution:
def numberOfWays(self, n: int, x: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 10, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 4, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 2
test_input = { "n": 1, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 1, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 1, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 1, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 1, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 2, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 2, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 2, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 2, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 2, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 3, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 2
test_input = { "n": 3, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 3, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 3, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 3, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 4, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 4, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 4, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 4, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 5, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 3
test_input = { "n": 5, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 5, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 5, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 5, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 6, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 4
test_input = { "n": 6, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 6, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 6, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 6, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 7, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 5
test_input = { "n": 7, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 7, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 7, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 7, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 8, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 6
test_input = { "n": 8, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 8, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 8, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 8, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 9, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 8
test_input = { "n": 9, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 9, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 9, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 9, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 10, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 10
test_input = { "n": 10, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 10, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 10, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 11, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 12
test_input = { "n": 11, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 11, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 11, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 11, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 12, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 15
test_input = { "n": 12, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 12, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 12, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 12, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 13, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 18
test_input = { "n": 13, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 13, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 13, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 13, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 14, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 22
test_input = { "n": 14, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 14, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 14, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 14, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 15, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 27
test_input = { "n": 15, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 15, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 15, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 15, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 16, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 32
test_input = { "n": 16, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 16, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 16, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 16, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 17, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 38
test_input = { "n": 17, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 17, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 17, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 17, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 18, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 46
test_input = { "n": 18, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 18, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 18, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 18, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 19, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 54
test_input = { "n": 19, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 19, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 19, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 19, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 20, "x": 1 }
assert my_solution.numberOfWays(**test_input) == 64
test_input = { "n": 20, "x": 2 }
assert my_solution.numberOfWays(**test_input) == 1
test_input = { "n": 20, "x": 3 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 20, "x": 4 }
assert my_solution.numberOfWays(**test_input) == 0
test_input = { "n": 20, "x": 5 }
assert my_solution.numberOfWays(**test_input) == 0 | 1,690,036,200 |
weekly-contest-354-sum-of-squares-of-special-elements | https://leetcode.com/problems/sum-of-squares-of-special-elements | sum-of-squares-of-special-elements | {
"questionId": "2844",
"questionFrontendId": "2778",
"title": "Sum of Squares of Special Elements ",
"titleSlug": "sum-of-squares-of-special-elements",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 218,
"dislikes": 65,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 1 开始、长度为 n 的整数数组 nums 。
对 nums 中的元素 nums[i] 而言,如果 n 能够被 i 整除,即 n % i == 0 ,则认为 num[i] 是一个 特殊元素 。
返回 nums 中所有 特殊元素 的 平方和 。
示例 1:
输入:nums = [1,2,3,4]
输出:21
解释:nums 中共有 3 个特殊元素:nums[1],因为 4 被 1 整除;nums[2],因为 4 被 2 整除;以及 nums[4],因为 4 被 4 整除。
因此,nums 中所有特殊元素的平方和等于 nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21 。
示例 2:
输入:nums = [2,7,1,19,18,3]
输出:63
解释:nums 中共有 4 个特殊元素:nums[1],因为 6 被 1 整除;nums[2] ,因为 6 被 2 整除;nums[3],因为 6 被 3 整除;以及 nums[6],因为 6 被 6 整除。
因此,nums 中所有特殊元素的平方和等于 nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 。
提示:
* 1 <= nums.length == n <= 50
* 1 <= nums[i] <= 50
"""
class Solution:
def sumOfSquares(self, nums: List[int]) -> int:
| 给你一个下标从 1 开始、长度为 n 的整数数组 nums 。
对 nums 中的元素 nums[i] 而言,如果 n 能够被 i 整除,即 n % i == 0 ,则认为 num[i] 是一个 特殊元素 。
返回 nums 中所有 特殊元素 的 平方和 。
示例 1:
输入:nums = [1,2,3,4]
输出:21
解释:nums 中共有 3 个特殊元素:nums[1],因为 4 被 1 整除;nums[2],因为 4 被 2 整除;以及 nums[4],因为 4 被 4 整除。
因此,nums 中所有特殊元素的平方和等于 nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21 。
示例 2:
输入:nums = [2,7,1,19,18,3]
输出:63
解释:nums 中共有 4 个特殊元素:nums[1],因为 6 被 1 整除;nums[2] ,因为 6 被 2 整除;nums[3],因为 6 被 3 整除;以及 nums[6],因为 6 被 6 整除。
因此,nums 中所有特殊元素的平方和等于 nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 。
提示:
* 1 <= nums.length == n <= 50
* 1 <= nums[i] <= 50
请完成下面的代码来解决上述问题:
```python
class Solution:
def sumOfSquares(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4] }
assert my_solution.sumOfSquares(**test_input) == 21
test_input = { "nums": [2,7,1,19,18,3] }
assert my_solution.sumOfSquares(**test_input) == 63
test_input = { "nums": [1] }
assert my_solution.sumOfSquares(**test_input) == 1
test_input = { "nums": [2] }
assert my_solution.sumOfSquares(**test_input) == 4
test_input = { "nums": [3] }
assert my_solution.sumOfSquares(**test_input) == 9
test_input = { "nums": [4] }
assert my_solution.sumOfSquares(**test_input) == 16
test_input = { "nums": [5] }
assert my_solution.sumOfSquares(**test_input) == 25
test_input = { "nums": [6] }
assert my_solution.sumOfSquares(**test_input) == 36
test_input = { "nums": [7] }
assert my_solution.sumOfSquares(**test_input) == 49
test_input = { "nums": [8] }
assert my_solution.sumOfSquares(**test_input) == 64
test_input = { "nums": [9] }
assert my_solution.sumOfSquares(**test_input) == 81
test_input = { "nums": [10] }
assert my_solution.sumOfSquares(**test_input) == 100
test_input = { "nums": [11] }
assert my_solution.sumOfSquares(**test_input) == 121
test_input = { "nums": [12] }
assert my_solution.sumOfSquares(**test_input) == 144
test_input = { "nums": [13] }
assert my_solution.sumOfSquares(**test_input) == 169
test_input = { "nums": [14] }
assert my_solution.sumOfSquares(**test_input) == 196
test_input = { "nums": [15] }
assert my_solution.sumOfSquares(**test_input) == 225
test_input = { "nums": [16] }
assert my_solution.sumOfSquares(**test_input) == 256
test_input = { "nums": [17] }
assert my_solution.sumOfSquares(**test_input) == 289
test_input = { "nums": [18] }
assert my_solution.sumOfSquares(**test_input) == 324
test_input = { "nums": [19] }
assert my_solution.sumOfSquares(**test_input) == 361
test_input = { "nums": [20] }
assert my_solution.sumOfSquares(**test_input) == 400
test_input = { "nums": [21] }
assert my_solution.sumOfSquares(**test_input) == 441
test_input = { "nums": [22] }
assert my_solution.sumOfSquares(**test_input) == 484
test_input = { "nums": [23] }
assert my_solution.sumOfSquares(**test_input) == 529
test_input = { "nums": [24] }
assert my_solution.sumOfSquares(**test_input) == 576
test_input = { "nums": [25] }
assert my_solution.sumOfSquares(**test_input) == 625
test_input = { "nums": [26] }
assert my_solution.sumOfSquares(**test_input) == 676
test_input = { "nums": [27] }
assert my_solution.sumOfSquares(**test_input) == 729
test_input = { "nums": [28] }
assert my_solution.sumOfSquares(**test_input) == 784
test_input = { "nums": [29] }
assert my_solution.sumOfSquares(**test_input) == 841
test_input = { "nums": [30] }
assert my_solution.sumOfSquares(**test_input) == 900
test_input = { "nums": [31] }
assert my_solution.sumOfSquares(**test_input) == 961
test_input = { "nums": [32] }
assert my_solution.sumOfSquares(**test_input) == 1024
test_input = { "nums": [33] }
assert my_solution.sumOfSquares(**test_input) == 1089
test_input = { "nums": [34] }
assert my_solution.sumOfSquares(**test_input) == 1156
test_input = { "nums": [35] }
assert my_solution.sumOfSquares(**test_input) == 1225
test_input = { "nums": [36] }
assert my_solution.sumOfSquares(**test_input) == 1296
test_input = { "nums": [37] }
assert my_solution.sumOfSquares(**test_input) == 1369
test_input = { "nums": [38] }
assert my_solution.sumOfSquares(**test_input) == 1444
test_input = { "nums": [39] }
assert my_solution.sumOfSquares(**test_input) == 1521
test_input = { "nums": [40] }
assert my_solution.sumOfSquares(**test_input) == 1600
test_input = { "nums": [41] }
assert my_solution.sumOfSquares(**test_input) == 1681
test_input = { "nums": [42] }
assert my_solution.sumOfSquares(**test_input) == 1764
test_input = { "nums": [43] }
assert my_solution.sumOfSquares(**test_input) == 1849
test_input = { "nums": [44] }
assert my_solution.sumOfSquares(**test_input) == 1936
test_input = { "nums": [45] }
assert my_solution.sumOfSquares(**test_input) == 2025
test_input = { "nums": [46] }
assert my_solution.sumOfSquares(**test_input) == 2116
test_input = { "nums": [47] }
assert my_solution.sumOfSquares(**test_input) == 2209
test_input = { "nums": [48] }
assert my_solution.sumOfSquares(**test_input) == 2304
test_input = { "nums": [49] }
assert my_solution.sumOfSquares(**test_input) == 2401
test_input = { "nums": [50] }
assert my_solution.sumOfSquares(**test_input) == 2500
test_input = { "nums": [16,16] }
assert my_solution.sumOfSquares(**test_input) == 512
test_input = { "nums": [13,36] }
assert my_solution.sumOfSquares(**test_input) == 1465
test_input = { "nums": [40,37] }
assert my_solution.sumOfSquares(**test_input) == 2969
test_input = { "nums": [33,42] }
assert my_solution.sumOfSquares(**test_input) == 2853
test_input = { "nums": [46,9] }
assert my_solution.sumOfSquares(**test_input) == 2197
test_input = { "nums": [30,14] }
assert my_solution.sumOfSquares(**test_input) == 1096
test_input = { "nums": [5,41] }
assert my_solution.sumOfSquares(**test_input) == 1706
test_input = { "nums": [17,9] }
assert my_solution.sumOfSquares(**test_input) == 370
test_input = { "nums": [29,21] }
assert my_solution.sumOfSquares(**test_input) == 1282
test_input = { "nums": [4,38] }
assert my_solution.sumOfSquares(**test_input) == 1460
test_input = { "nums": [14,18] }
assert my_solution.sumOfSquares(**test_input) == 520
test_input = { "nums": [11,7] }
assert my_solution.sumOfSquares(**test_input) == 170
test_input = { "nums": [11,36] }
assert my_solution.sumOfSquares(**test_input) == 1417
test_input = { "nums": [18,26] }
assert my_solution.sumOfSquares(**test_input) == 1000
test_input = { "nums": [37,46] }
assert my_solution.sumOfSquares(**test_input) == 3485
test_input = { "nums": [13,33] }
assert my_solution.sumOfSquares(**test_input) == 1258
test_input = { "nums": [39,1] }
assert my_solution.sumOfSquares(**test_input) == 1522
test_input = { "nums": [37,16] }
assert my_solution.sumOfSquares(**test_input) == 1625
test_input = { "nums": [22,34] }
assert my_solution.sumOfSquares(**test_input) == 1640
test_input = { "nums": [4,50] }
assert my_solution.sumOfSquares(**test_input) == 2516
test_input = { "nums": [42,40] }
assert my_solution.sumOfSquares(**test_input) == 3364
test_input = { "nums": [7,44] }
assert my_solution.sumOfSquares(**test_input) == 1985
test_input = { "nums": [21,27] }
assert my_solution.sumOfSquares(**test_input) == 1170
test_input = { "nums": [49,35] }
assert my_solution.sumOfSquares(**test_input) == 3626
test_input = { "nums": [32,20] }
assert my_solution.sumOfSquares(**test_input) == 1424
test_input = { "nums": [30,12] }
assert my_solution.sumOfSquares(**test_input) == 1044
test_input = { "nums": [50,42] }
assert my_solution.sumOfSquares(**test_input) == 4264
test_input = { "nums": [3,11] }
assert my_solution.sumOfSquares(**test_input) == 130
test_input = { "nums": [38,17] }
assert my_solution.sumOfSquares(**test_input) == 1733
test_input = { "nums": [50,32] }
assert my_solution.sumOfSquares(**test_input) == 3524
test_input = { "nums": [12,35] }
assert my_solution.sumOfSquares(**test_input) == 1369
test_input = { "nums": [9,32] }
assert my_solution.sumOfSquares(**test_input) == 1105
test_input = { "nums": [6,11] }
assert my_solution.sumOfSquares(**test_input) == 157
test_input = { "nums": [11,39] }
assert my_solution.sumOfSquares(**test_input) == 1642
test_input = { "nums": [18,29] }
assert my_solution.sumOfSquares(**test_input) == 1165
test_input = { "nums": [44,29] }
assert my_solution.sumOfSquares(**test_input) == 2777
test_input = { "nums": [50,13] }
assert my_solution.sumOfSquares(**test_input) == 2669
test_input = { "nums": [46,46] }
assert my_solution.sumOfSquares(**test_input) == 4232
test_input = { "nums": [27,5] }
assert my_solution.sumOfSquares(**test_input) == 754
test_input = { "nums": [12,13] }
assert my_solution.sumOfSquares(**test_input) == 313
test_input = { "nums": [10,46] }
assert my_solution.sumOfSquares(**test_input) == 2216
test_input = { "nums": [37,32] }
assert my_solution.sumOfSquares(**test_input) == 2393
test_input = { "nums": [26,33] }
assert my_solution.sumOfSquares(**test_input) == 1765
test_input = { "nums": [44,3] }
assert my_solution.sumOfSquares(**test_input) == 1945
test_input = { "nums": [9,16] }
assert my_solution.sumOfSquares(**test_input) == 337
test_input = { "nums": [7,21] }
assert my_solution.sumOfSquares(**test_input) == 490
test_input = { "nums": [23,33] }
assert my_solution.sumOfSquares(**test_input) == 1618
test_input = { "nums": [22,5] }
assert my_solution.sumOfSquares(**test_input) == 509 | 1,689,474,600 |
weekly-contest-354-maximum-beauty-of-an-array-after-applying-operation | https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation | maximum-beauty-of-an-array-after-applying-operation | {
"questionId": "2891",
"questionFrontendId": "2779",
"title": "Maximum Beauty of an Array After Applying Operation",
"titleSlug": "maximum-beauty-of-an-array-after-applying-operation",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 559,
"dislikes": 12,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 和一个 非负 整数 k 。
在一步操作中,你可以执行下述指令:
* 在范围 [0, nums.length - 1] 中选择一个 此前没有选过 的下标 i 。
* 将 nums[i] 替换为范围 [nums[i] - k, nums[i] + k] 内的任一整数。
数组的 美丽值 定义为数组中由相等元素组成的最长子序列的长度。
对数组 nums 执行上述操作任意次后,返回数组可能取得的 最大 美丽值。
注意:你 只 能对每个下标执行 一次 此操作。
数组的 子序列 定义是:经由原数组删除一些元素(也可能不删除)得到的一个新数组,且在此过程中剩余元素的顺序不发生改变。
示例 1:
输入:nums = [4,6,1,2], k = 2
输出:3
解释:在这个示例中,我们执行下述操作:
- 选择下标 1 ,将其替换为 4(从范围 [4,8] 中选出),此时 nums = [4,4,1,2] 。
- 选择下标 3 ,将其替换为 4(从范围 [0,4] 中选出),此时 nums = [4,4,1,4] 。
执行上述操作后,数组的美丽值是 3(子序列由下标 0 、1 、3 对应的元素组成)。
可以证明 3 是我们可以得到的由相等元素组成的最长子序列长度。
示例 2:
输入:nums = [1,1,1,1], k = 10
输出:4
解释:在这个示例中,我们无需执行任何操作。
数组 nums 的美丽值是 4(整个数组)。
提示:
* 1 <= nums.length <= 105
* 0 <= nums[i], k <= 105
"""
class Solution:
def maximumBeauty(self, nums: List[int], k: int) -> int:
| 给你一个下标从 0 开始的整数数组 nums 和一个 非负 整数 k 。
在一步操作中,你可以执行下述指令:
* 在范围 [0, nums.length - 1] 中选择一个 此前没有选过 的下标 i 。
* 将 nums[i] 替换为范围 [nums[i] - k, nums[i] + k] 内的任一整数。
数组的 美丽值 定义为数组中由相等元素组成的最长子序列的长度。
对数组 nums 执行上述操作任意次后,返回数组可能取得的 最大 美丽值。
注意:你 只 能对每个下标执行 一次 此操作。
数组的 子序列 定义是:经由原数组删除一些元素(也可能不删除)得到的一个新数组,且在此过程中剩余元素的顺序不发生改变。
示例 1:
输入:nums = [4,6,1,2], k = 2
输出:3
解释:在这个示例中,我们执行下述操作:
- 选择下标 1 ,将其替换为 4(从范围 [4,8] 中选出),此时 nums = [4,4,1,2] 。
- 选择下标 3 ,将其替换为 4(从范围 [0,4] 中选出),此时 nums = [4,4,1,4] 。
执行上述操作后,数组的美丽值是 3(子序列由下标 0 、1 、3 对应的元素组成)。
可以证明 3 是我们可以得到的由相等元素组成的最长子序列长度。
示例 2:
输入:nums = [1,1,1,1], k = 10
输出:4
解释:在这个示例中,我们无需执行任何操作。
数组 nums 的美丽值是 4(整个数组)。
提示:
* 1 <= nums.length <= 105
* 0 <= nums[i], k <= 105
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumBeauty(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [4,6,1,2], "k": 2 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [1,1,1,1], "k": 10 }
assert my_solution.maximumBeauty(**test_input) == 4
test_input = { "nums": [12,71], "k": 10 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [27,55], "k": 1 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [52,34], "k": 21 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [76,0], "k": 16 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [56,40], "k": 26 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [49,26], "k": 12 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [69,66], "k": 14 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [64,98], "k": 12 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [83,81], "k": 7 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [44,93], "k": 15 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [44,31], "k": 26 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [70,60], "k": 15 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [60,22], "k": 11 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [33,20], "k": 1 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [64,24], "k": 4 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [59,20], "k": 28 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [10,98], "k": 27 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [54,21], "k": 20 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [61,11], "k": 15 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [99,40], "k": 27 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [32,91], "k": 3 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [91,57], "k": 21 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [60,92], "k": 26 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [4,45], "k": 6 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [24,35], "k": 6 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [11,29], "k": 3 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [51,29], "k": 3 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [43,21], "k": 14 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [32,25], "k": 18 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [13,66], "k": 5 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [89,71], "k": 28 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [36,29], "k": 20 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [11,43], "k": 21 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [15,36], "k": 4 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [11,51], "k": 1 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [2,57], "k": 20 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [94,66], "k": 26 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [87,51], "k": 8 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [5,57,46], "k": 15 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [81,46,85], "k": 23 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [51,83,0], "k": 11 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [75,15,9], "k": 28 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [10,59,86], "k": 23 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [41,11,59], "k": 17 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [62,77,100], "k": 5 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [27,35,15], "k": 6 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [81,76,40], "k": 5 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [84,43,96], "k": 7 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [62,1,93], "k": 30 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [13,46,71], "k": 29 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [92,99,44], "k": 28 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [73,30,40], "k": 26 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [83,89,17], "k": 5 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [38,20,11], "k": 9 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [63,56,23], "k": 14 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [32,16,98], "k": 0 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [57,58,71], "k": 2 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [61,50,35], "k": 2 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [22,97,13], "k": 22 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [89,52,33], "k": 14 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [89,4,77], "k": 20 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [50,26,72], "k": 30 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [72,75,47], "k": 7 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [23,1,73], "k": 25 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [36,74,20], "k": 20 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [34,64,11], "k": 18 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [29,94,45], "k": 27 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [22,80,34], "k": 28 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [52,63,75], "k": 11 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [53,63,93,89], "k": 23 }
assert my_solution.maximumBeauty(**test_input) == 4
test_input = { "nums": [47,76,100,51], "k": 27 }
assert my_solution.maximumBeauty(**test_input) == 4
test_input = { "nums": [73,83,46,88], "k": 13 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [50,28,30,51], "k": 2 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [88,87,9,17], "k": 10 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [27,56,27,40], "k": 6 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [88,19,2,30], "k": 6 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [58,50,0,97], "k": 18 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [83,10,99,99], "k": 18 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [58,75,1,25], "k": 12 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [77,35,1,69], "k": 15 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [23,33,62,20], "k": 12 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [42,34,18,0], "k": 5 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [10,58,37,46], "k": 0 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [34,73,57,55], "k": 27 }
assert my_solution.maximumBeauty(**test_input) == 4
test_input = { "nums": [53,100,74,5], "k": 4 }
assert my_solution.maximumBeauty(**test_input) == 1
test_input = { "nums": [48,93,96,19], "k": 24 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [91,12,29,31], "k": 22 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [48,9,35,36], "k": 12 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [24,64,40,30], "k": 3 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [19,58,41,42], "k": 14 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [72,44,29,76], "k": 4 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [37,19,10,16], "k": 16 }
assert my_solution.maximumBeauty(**test_input) == 4
test_input = { "nums": [54,84,73,31], "k": 30 }
assert my_solution.maximumBeauty(**test_input) == 4
test_input = { "nums": [83,92,30,60], "k": 19 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [14,51,99,64], "k": 15 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [7,60,16,2], "k": 17 }
assert my_solution.maximumBeauty(**test_input) == 3
test_input = { "nums": [7,89,54,54], "k": 5 }
assert my_solution.maximumBeauty(**test_input) == 2
test_input = { "nums": [43,86,33,18], "k": 23 }
assert my_solution.maximumBeauty(**test_input) == 3 | 1,689,474,600 |
weekly-contest-354-minimum-index-of-a-valid-split | https://leetcode.com/problems/minimum-index-of-a-valid-split | minimum-index-of-a-valid-split | {
"questionId": "2888",
"questionFrontendId": "2780",
"title": "Minimum Index of a Valid Split",
"titleSlug": "minimum-index-of-a-valid-split",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 282,
"dislikes": 11,
"categoryTitle": "Algorithms"
} | """
如果元素 x 在长度为 m 的整数数组 arr 中满足 freq(x) * 2 > m ,那么我们称 x 是 支配元素 。其中 freq(x) 是 x 在数组 arr 中出现的次数。注意,根据这个定义,数组 arr 最多 只会有 一个 支配元素。
给你一个下标从 0 开始长度为 n 的整数数组 nums ,数据保证它含有一个支配元素。
你需要在下标 i 处将 nums 分割成两个数组 nums[0, ..., i] 和 nums[i + 1, ..., n - 1] ,如果一个分割满足以下条件,我们称它是 合法 的:
* 0 <= i < n - 1
* nums[0, ..., i] 和 nums[i + 1, ..., n - 1] 的支配元素相同。
这里, nums[i, ..., j] 表示 nums 的一个子数组,它开始于下标 i ,结束于下标 j ,两个端点都包含在子数组内。特别地,如果 j < i ,那么 nums[i, ..., j] 表示一个空数组。
请你返回一个 合法分割 的 最小 下标。如果合法分割不存在,返回 -1 。
示例 1:
输入:nums = [1,2,2,2]
输出:2
解释:我们将数组在下标 2 处分割,得到 [1,2,2] 和 [2] 。
数组 [1,2,2] 中,元素 2 是支配元素,因为它在数组中出现了 2 次,且 2 * 2 > 3 。
数组 [2] 中,元素 2 是支配元素,因为它在数组中出现了 1 次,且 1 * 2 > 1 。
两个数组 [1,2,2] 和 [2] 都有与 nums 一样的支配元素,所以这是一个合法分割。
下标 2 是合法分割中的最小下标。
示例 2:
输入:nums = [2,1,3,1,1,1,7,1,2,1]
输出:4
解释:我们将数组在下标 4 处分割,得到 [2,1,3,1,1] 和 [1,7,1,2,1] 。
数组 [2,1,3,1,1] 中,元素 1 是支配元素,因为它在数组中出现了 3 次,且 3 * 2 > 5 。
数组 [1,7,1,2,1] 中,元素 1 是支配元素,因为它在数组中出现了 3 次,且 3 * 2 > 5 。
两个数组 [2,1,3,1,1] 和 [1,7,1,2,1] 都有与 nums 一样的支配元素,所以这是一个合法分割。
下标 4 是所有合法分割中的最小下标。
示例 3:
输入:nums = [3,3,3,3,7,2,2]
输出:-1
解释:没有合法分割。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* nums 有且只有一个支配元素。
"""
class Solution:
def minimumIndex(self, nums: List[int]) -> int:
| 如果元素 x 在长度为 m 的整数数组 arr 中满足 freq(x) * 2 > m ,那么我们称 x 是 支配元素 。其中 freq(x) 是 x 在数组 arr 中出现的次数。注意,根据这个定义,数组 arr 最多 只会有 一个 支配元素。
给你一个下标从 0 开始长度为 n 的整数数组 nums ,数据保证它含有一个支配元素。
你需要在下标 i 处将 nums 分割成两个数组 nums[0, ..., i] 和 nums[i + 1, ..., n - 1] ,如果一个分割满足以下条件,我们称它是 合法 的:
* 0 <= i < n - 1
* nums[0, ..., i] 和 nums[i + 1, ..., n - 1] 的支配元素相同。
这里, nums[i, ..., j] 表示 nums 的一个子数组,它开始于下标 i ,结束于下标 j ,两个端点都包含在子数组内。特别地,如果 j < i ,那么 nums[i, ..., j] 表示一个空数组。
请你返回一个 合法分割 的 最小 下标。如果合法分割不存在,返回 -1 。
示例 1:
输入:nums = [1,2,2,2]
输出:2
解释:我们将数组在下标 2 处分割,得到 [1,2,2] 和 [2] 。
数组 [1,2,2] 中,元素 2 是支配元素,因为它在数组中出现了 2 次,且 2 * 2 > 3 。
数组 [2] 中,元素 2 是支配元素,因为它在数组中出现了 1 次,且 1 * 2 > 1 。
两个数组 [1,2,2] 和 [2] 都有与 nums 一样的支配元素,所以这是一个合法分割。
下标 2 是合法分割中的最小下标。
示例 2:
输入:nums = [2,1,3,1,1,1,7,1,2,1]
输出:4
解释:我们将数组在下标 4 处分割,得到 [2,1,3,1,1] 和 [1,7,1,2,1] 。
数组 [2,1,3,1,1] 中,元素 1 是支配元素,因为它在数组中出现了 3 次,且 3 * 2 > 5 。
数组 [1,7,1,2,1] 中,元素 1 是支配元素,因为它在数组中出现了 3 次,且 3 * 2 > 5 。
两个数组 [2,1,3,1,1] 和 [1,7,1,2,1] 都有与 nums 一样的支配元素,所以这是一个合法分割。
下标 4 是所有合法分割中的最小下标。
示例 3:
输入:nums = [3,3,3,3,7,2,2]
输出:-1
解释:没有合法分割。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
* nums 有且只有一个支配元素。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumIndex(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,2,2] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [2,1,3,1,1,1,7,1,2,1] }
assert my_solution.minimumIndex(**test_input) == 4
test_input = { "nums": [3,3,3,3,7,2,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,1,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,1,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,1,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,1,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,1,2,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,1,3,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,1,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,1,4,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,2,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,2,1,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,2,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,3,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,3,1,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,3,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,3,3,3] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [1,4,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,4,1,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [1,4,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [1,4,4,4] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,1,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,1,1,1] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [2,1,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,1,2,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,2,1,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,2,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,2,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,2,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,2,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,2,3,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,2,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,2,4,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,3,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,3,2,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,3,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,3,3,3] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [2,4,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,4,2,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [2,4,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [2,4,4,4] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,1,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,1,1,1] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [3,1,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,1,3,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,2,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,2,2,2] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [3,2,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,2,3,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,3,1,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,3,2,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,3,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,3,2] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,3,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,3,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,3,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,3,4,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,4,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,4,3,3] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [3,4,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [3,4,4,4] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,1,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,1,1,1] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [4,1,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,1,4,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,2,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,2,2,2] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [4,2,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,2,4,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,3,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,3,3,3] }
assert my_solution.minimumIndex(**test_input) == 2
test_input = { "nums": [4,3,4] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,3,4,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,4,1] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,4,1,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,4,2] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,4,2,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,4,3] }
assert my_solution.minimumIndex(**test_input) == -1
test_input = { "nums": [4,4,3,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,4,4] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,4,4,1] }
assert my_solution.minimumIndex(**test_input) == 0
test_input = { "nums": [4,4,4,2] }
assert my_solution.minimumIndex(**test_input) == 0 | 1,689,474,600 |
weekly-contest-354-length-of-the-longest-valid-substring | https://leetcode.com/problems/length-of-the-longest-valid-substring | length-of-the-longest-valid-substring | {
"questionId": "2884",
"questionFrontendId": "2781",
"title": "Length of the Longest Valid Substring",
"titleSlug": "length-of-the-longest-valid-substring",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 447,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
给你一个字符串 word 和一个字符串数组 forbidden 。
如果一个字符串不包含 forbidden 中的任何字符串,我们称这个字符串是 合法 的。
请你返回字符串 word 的一个 最长合法子字符串 的长度。
子字符串 指的是一个字符串中一段连续的字符,它可以为空。
示例 1:
输入:word = "cbaaaabc", forbidden = ["aaa","cb"]
输出:4
解释:总共有 11 个合法子字符串:"c", "b", "a", "ba", "aa", "bc", "baa", "aab", "ab", "abc" 和 "aabc"。最长合法子字符串的长度为 4 。
其他子字符串都要么包含 "aaa" ,要么包含 "cb" 。
示例 2:
输入:word = "leetcode", forbidden = ["de","le","e"]
输出:4
解释:总共有 11 个合法子字符串:"l" ,"t" ,"c" ,"o" ,"d" ,"tc" ,"co" ,"od" ,"tco" ,"cod" 和 "tcod" 。最长合法子字符串的长度为 4 。
所有其他子字符串都至少包含 "de" ,"le" 和 "e" 之一。
提示:
* 1 <= word.length <= 105
* word 只包含小写英文字母。
* 1 <= forbidden.length <= 105
* 1 <= forbidden[i].length <= 10
* forbidden[i] 只包含小写英文字母。
"""
class Solution:
def longestValidSubstring(self, word: str, forbidden: List[str]) -> int:
| 给你一个字符串 word 和一个字符串数组 forbidden 。
如果一个字符串不包含 forbidden 中的任何字符串,我们称这个字符串是 合法 的。
请你返回字符串 word 的一个 最长合法子字符串 的长度。
子字符串 指的是一个字符串中一段连续的字符,它可以为空。
示例 1:
输入:word = "cbaaaabc", forbidden = ["aaa","cb"]
输出:4
解释:总共有 11 个合法子字符串:"c", "b", "a", "ba", "aa", "bc", "baa", "aab", "ab", "abc" 和 "aabc"。最长合法子字符串的长度为 4 。
其他子字符串都要么包含 "aaa" ,要么包含 "cb" 。
示例 2:
输入:word = "leetcode", forbidden = ["de","le","e"]
输出:4
解释:总共有 11 个合法子字符串:"l" ,"t" ,"c" ,"o" ,"d" ,"tc" ,"co" ,"od" ,"tco" ,"cod" 和 "tcod" 。最长合法子字符串的长度为 4 。
所有其他子字符串都至少包含 "de" ,"le" 和 "e" 之一。
提示:
* 1 <= word.length <= 105
* word 只包含小写英文字母。
* 1 <= forbidden.length <= 105
* 1 <= forbidden[i].length <= 10
* forbidden[i] 只包含小写英文字母。
请完成下面的代码来解决上述问题:
```python
class Solution:
def longestValidSubstring(self, word: str, forbidden: List[str]) -> int:
``` |
my_solution = Solution()
test_input = { "word": "cbaaaabc", "forbidden": ["aaa","cb"] }
assert my_solution.longestValidSubstring(**test_input) == 4
test_input = { "word": "leetcode", "forbidden": ["de","le","e"] }
assert my_solution.longestValidSubstring(**test_input) == 4
test_input = { "word": "a", "forbidden": ["n"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "a", "forbidden": ["s"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "a", "forbidden": ["a"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "b", "forbidden": ["g"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "b", "forbidden": ["t"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "b", "forbidden": ["b"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "c", "forbidden": ["k"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "c", "forbidden": ["s"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "c", "forbidden": ["c"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "d", "forbidden": ["h"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "d", "forbidden": ["n"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "d", "forbidden": ["d"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "e", "forbidden": ["s"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "e", "forbidden": ["e"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "f", "forbidden": ["b"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "f", "forbidden": ["s"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "f", "forbidden": ["f"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "g", "forbidden": ["r"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "g", "forbidden": ["y"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "g", "forbidden": ["g"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "h", "forbidden": ["v"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "h", "forbidden": ["b"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "h", "forbidden": ["h"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "i", "forbidden": ["k"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "i", "forbidden": ["y"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "i", "forbidden": ["i"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "j", "forbidden": ["v"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "j", "forbidden": ["u"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "j", "forbidden": ["j"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "k", "forbidden": ["z"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "k", "forbidden": ["o"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "k", "forbidden": ["k"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "l", "forbidden": ["i"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "l", "forbidden": ["r"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "l", "forbidden": ["l"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "m", "forbidden": ["s"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "m", "forbidden": ["g"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "m", "forbidden": ["m"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "n", "forbidden": ["e"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "n", "forbidden": ["i"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "n", "forbidden": ["n"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "o", "forbidden": ["j"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "o", "forbidden": ["f"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "o", "forbidden": ["o"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "p", "forbidden": ["z"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "p", "forbidden": ["i"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "p", "forbidden": ["p"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "q", "forbidden": ["j"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "q", "forbidden": ["z"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "q", "forbidden": ["q"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "r", "forbidden": ["v"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "r", "forbidden": ["p"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "r", "forbidden": ["r"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "s", "forbidden": ["m"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "s", "forbidden": ["x"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "s", "forbidden": ["s"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "t", "forbidden": ["v"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "t", "forbidden": ["m"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "t", "forbidden": ["t"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "u", "forbidden": ["l"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "u", "forbidden": ["n"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "u", "forbidden": ["u"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "v", "forbidden": ["o"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "v", "forbidden": ["v"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "w", "forbidden": ["w"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "w", "forbidden": ["s"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "x", "forbidden": ["r"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "x", "forbidden": ["q"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "x", "forbidden": ["x"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "y", "forbidden": ["w"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "y", "forbidden": ["t"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "y", "forbidden": ["y"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "z", "forbidden": ["l"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "z", "forbidden": ["o"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "z", "forbidden": ["z"] }
assert my_solution.longestValidSubstring(**test_input) == 0
test_input = { "word": "acbc", "forbidden": ["cbc","acb","acb","acbc"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "cabba", "forbidden": ["aaba","abba","acabb","cabb"] }
assert my_solution.longestValidSubstring(**test_input) == 3
test_input = { "word": "bbc", "forbidden": ["baba","babc","bbc","bbc"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "acb", "forbidden": ["acb","caccc","baaab","baa"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "aaac", "forbidden": ["aaac","aac","aaa","aaac"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "ca", "forbidden": ["ababa","ca","caac","babb"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "babbb", "forbidden": ["bbb","aacb","babbb","bcab"] }
assert my_solution.longestValidSubstring(**test_input) == 4
test_input = { "word": "cbbba", "forbidden": ["bca","cbbba","acbcc","aabb"] }
assert my_solution.longestValidSubstring(**test_input) == 4
test_input = { "word": "abab", "forbidden": ["aab","abab","cacb","bab"] }
assert my_solution.longestValidSubstring(**test_input) == 3
test_input = { "word": "cbab", "forbidden": ["bbcc","aaccc","cbab","babca"] }
assert my_solution.longestValidSubstring(**test_input) == 3
test_input = { "word": "caaa", "forbidden": ["aaa","cbb","aaa","caaa"] }
assert my_solution.longestValidSubstring(**test_input) == 3
test_input = { "word": "baa", "forbidden": ["aaab","bbaa","babac","baa"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "cbcc", "forbidden": ["cbcc","baa","bbba","cac"] }
assert my_solution.longestValidSubstring(**test_input) == 3
test_input = { "word": "cac", "forbidden": ["cccaa","baaca","cac","cac"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "cabab", "forbidden": ["cabab","abab","cabab","abab"] }
assert my_solution.longestValidSubstring(**test_input) == 4
test_input = { "word": "caa", "forbidden": ["caa","bba","acc","bcabb"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "ba", "forbidden": ["ba","ba","cab","cbcac"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "bbc", "forbidden": ["baca","bbc","bbc","caa"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "bbb", "forbidden": ["cbaab","bbb","bbb","bab"] }
assert my_solution.longestValidSubstring(**test_input) == 2
test_input = { "word": "bbccc", "forbidden": ["ccc","bcba","bcc","bcc"] }
assert my_solution.longestValidSubstring(**test_input) == 3
test_input = { "word": "bcac", "forbidden": ["bcac","caca","bcac","bca"] }
assert my_solution.longestValidSubstring(**test_input) == 3
test_input = { "word": "ab", "forbidden": ["aca","cabcc","caba","ab"] }
assert my_solution.longestValidSubstring(**test_input) == 1
test_input = { "word": "caa", "forbidden": ["bab","babbb","abbaa","caa"] }
assert my_solution.longestValidSubstring(**test_input) == 2 | 1,689,474,600 |
weekly-contest-353-find-the-maximum-achievable-number | https://leetcode.com/problems/find-the-maximum-achievable-number | find-the-maximum-achievable-number | {
"questionId": "2812",
"questionFrontendId": "2769",
"title": "Find the Maximum Achievable Number",
"titleSlug": "find-the-maximum-achievable-number",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 230,
"dislikes": 286,
"categoryTitle": "Algorithms"
} | """
给你两个整数 num 和 t 。
如果整数 x 可以在执行下述操作不超过 t 次的情况下变为与 num 相等,则称其为 可达成数字 :
* 每次操作将 x 的值增加或减少 1 ,同时可以选择将 num 的值增加或减少 1 。
返回所有可达成数字中的最大值。可以证明至少存在一个可达成数字。
示例 1:
输入:num = 4, t = 1
输出:6
解释:最大可达成数字是 x = 6 ,执行下述操作可以使其等于 num :
- x 减少 1 ,同时 num 增加 1 。此时,x = 5 且 num = 5 。
可以证明不存在大于 6 的可达成数字。
示例 2:
输入:num = 3, t = 2
输出:7
解释:最大的可达成数字是 x = 7 ,执行下述操作可以使其等于 num :
- x 减少 1 ,同时 num 增加 1 。此时,x = 6 且 num = 4 。
- x 减少 1 ,同时 num 增加 1 。此时,x = 5 且 num = 5 。
可以证明不存在大于 7 的可达成数字。
提示:
* 1 <= num, t <= 50
"""
class Solution:
def theMaximumAchievableX(self, num: int, t: int) -> int:
| 给你两个整数 num 和 t 。
如果整数 x 可以在执行下述操作不超过 t 次的情况下变为与 num 相等,则称其为 可达成数字 :
* 每次操作将 x 的值增加或减少 1 ,同时可以选择将 num 的值增加或减少 1 。
返回所有可达成数字中的最大值。可以证明至少存在一个可达成数字。
示例 1:
输入:num = 4, t = 1
输出:6
解释:最大可达成数字是 x = 6 ,执行下述操作可以使其等于 num :
- x 减少 1 ,同时 num 增加 1 。此时,x = 5 且 num = 5 。
可以证明不存在大于 6 的可达成数字。
示例 2:
输入:num = 3, t = 2
输出:7
解释:最大的可达成数字是 x = 7 ,执行下述操作可以使其等于 num :
- x 减少 1 ,同时 num 增加 1 。此时,x = 6 且 num = 4 。
- x 减少 1 ,同时 num 增加 1 。此时,x = 5 且 num = 5 。
可以证明不存在大于 7 的可达成数字。
提示:
* 1 <= num, t <= 50
请完成下面的代码来解决上述问题:
```python
class Solution:
def theMaximumAchievableX(self, num: int, t: int) -> int:
``` |
my_solution = Solution()
test_input = { "num": 4, "t": 1 }
assert my_solution.theMaximumAchievableX(**test_input) == 6
test_input = { "num": 3, "t": 2 }
assert my_solution.theMaximumAchievableX(**test_input) == 7
test_input = { "num": 1, "t": 1 }
assert my_solution.theMaximumAchievableX(**test_input) == 3
test_input = { "num": 1, "t": 2 }
assert my_solution.theMaximumAchievableX(**test_input) == 5
test_input = { "num": 1, "t": 3 }
assert my_solution.theMaximumAchievableX(**test_input) == 7
test_input = { "num": 1, "t": 4 }
assert my_solution.theMaximumAchievableX(**test_input) == 9
test_input = { "num": 1, "t": 5 }
assert my_solution.theMaximumAchievableX(**test_input) == 11
test_input = { "num": 1, "t": 6 }
assert my_solution.theMaximumAchievableX(**test_input) == 13
test_input = { "num": 1, "t": 7 }
assert my_solution.theMaximumAchievableX(**test_input) == 15
test_input = { "num": 1, "t": 8 }
assert my_solution.theMaximumAchievableX(**test_input) == 17
test_input = { "num": 1, "t": 9 }
assert my_solution.theMaximumAchievableX(**test_input) == 19
test_input = { "num": 1, "t": 10 }
assert my_solution.theMaximumAchievableX(**test_input) == 21
test_input = { "num": 1, "t": 11 }
assert my_solution.theMaximumAchievableX(**test_input) == 23
test_input = { "num": 1, "t": 12 }
assert my_solution.theMaximumAchievableX(**test_input) == 25
test_input = { "num": 1, "t": 13 }
assert my_solution.theMaximumAchievableX(**test_input) == 27
test_input = { "num": 1, "t": 14 }
assert my_solution.theMaximumAchievableX(**test_input) == 29
test_input = { "num": 1, "t": 15 }
assert my_solution.theMaximumAchievableX(**test_input) == 31
test_input = { "num": 1, "t": 16 }
assert my_solution.theMaximumAchievableX(**test_input) == 33
test_input = { "num": 1, "t": 17 }
assert my_solution.theMaximumAchievableX(**test_input) == 35
test_input = { "num": 1, "t": 18 }
assert my_solution.theMaximumAchievableX(**test_input) == 37
test_input = { "num": 1, "t": 19 }
assert my_solution.theMaximumAchievableX(**test_input) == 39
test_input = { "num": 1, "t": 20 }
assert my_solution.theMaximumAchievableX(**test_input) == 41
test_input = { "num": 1, "t": 21 }
assert my_solution.theMaximumAchievableX(**test_input) == 43
test_input = { "num": 1, "t": 22 }
assert my_solution.theMaximumAchievableX(**test_input) == 45
test_input = { "num": 1, "t": 23 }
assert my_solution.theMaximumAchievableX(**test_input) == 47
test_input = { "num": 1, "t": 24 }
assert my_solution.theMaximumAchievableX(**test_input) == 49
test_input = { "num": 1, "t": 25 }
assert my_solution.theMaximumAchievableX(**test_input) == 51
test_input = { "num": 1, "t": 26 }
assert my_solution.theMaximumAchievableX(**test_input) == 53
test_input = { "num": 1, "t": 27 }
assert my_solution.theMaximumAchievableX(**test_input) == 55
test_input = { "num": 1, "t": 28 }
assert my_solution.theMaximumAchievableX(**test_input) == 57
test_input = { "num": 1, "t": 29 }
assert my_solution.theMaximumAchievableX(**test_input) == 59
test_input = { "num": 1, "t": 30 }
assert my_solution.theMaximumAchievableX(**test_input) == 61
test_input = { "num": 1, "t": 31 }
assert my_solution.theMaximumAchievableX(**test_input) == 63
test_input = { "num": 1, "t": 32 }
assert my_solution.theMaximumAchievableX(**test_input) == 65
test_input = { "num": 1, "t": 33 }
assert my_solution.theMaximumAchievableX(**test_input) == 67
test_input = { "num": 1, "t": 34 }
assert my_solution.theMaximumAchievableX(**test_input) == 69
test_input = { "num": 1, "t": 35 }
assert my_solution.theMaximumAchievableX(**test_input) == 71
test_input = { "num": 1, "t": 36 }
assert my_solution.theMaximumAchievableX(**test_input) == 73
test_input = { "num": 1, "t": 37 }
assert my_solution.theMaximumAchievableX(**test_input) == 75
test_input = { "num": 1, "t": 38 }
assert my_solution.theMaximumAchievableX(**test_input) == 77
test_input = { "num": 1, "t": 39 }
assert my_solution.theMaximumAchievableX(**test_input) == 79
test_input = { "num": 1, "t": 40 }
assert my_solution.theMaximumAchievableX(**test_input) == 81
test_input = { "num": 1, "t": 41 }
assert my_solution.theMaximumAchievableX(**test_input) == 83
test_input = { "num": 1, "t": 42 }
assert my_solution.theMaximumAchievableX(**test_input) == 85
test_input = { "num": 1, "t": 43 }
assert my_solution.theMaximumAchievableX(**test_input) == 87
test_input = { "num": 1, "t": 44 }
assert my_solution.theMaximumAchievableX(**test_input) == 89
test_input = { "num": 1, "t": 45 }
assert my_solution.theMaximumAchievableX(**test_input) == 91
test_input = { "num": 1, "t": 46 }
assert my_solution.theMaximumAchievableX(**test_input) == 93
test_input = { "num": 1, "t": 47 }
assert my_solution.theMaximumAchievableX(**test_input) == 95
test_input = { "num": 1, "t": 48 }
assert my_solution.theMaximumAchievableX(**test_input) == 97
test_input = { "num": 1, "t": 49 }
assert my_solution.theMaximumAchievableX(**test_input) == 99
test_input = { "num": 1, "t": 50 }
assert my_solution.theMaximumAchievableX(**test_input) == 101
test_input = { "num": 2, "t": 1 }
assert my_solution.theMaximumAchievableX(**test_input) == 4
test_input = { "num": 2, "t": 2 }
assert my_solution.theMaximumAchievableX(**test_input) == 6
test_input = { "num": 2, "t": 3 }
assert my_solution.theMaximumAchievableX(**test_input) == 8
test_input = { "num": 2, "t": 4 }
assert my_solution.theMaximumAchievableX(**test_input) == 10
test_input = { "num": 2, "t": 5 }
assert my_solution.theMaximumAchievableX(**test_input) == 12
test_input = { "num": 2, "t": 6 }
assert my_solution.theMaximumAchievableX(**test_input) == 14
test_input = { "num": 2, "t": 7 }
assert my_solution.theMaximumAchievableX(**test_input) == 16
test_input = { "num": 2, "t": 8 }
assert my_solution.theMaximumAchievableX(**test_input) == 18
test_input = { "num": 2, "t": 9 }
assert my_solution.theMaximumAchievableX(**test_input) == 20
test_input = { "num": 2, "t": 10 }
assert my_solution.theMaximumAchievableX(**test_input) == 22
test_input = { "num": 2, "t": 11 }
assert my_solution.theMaximumAchievableX(**test_input) == 24
test_input = { "num": 2, "t": 12 }
assert my_solution.theMaximumAchievableX(**test_input) == 26
test_input = { "num": 2, "t": 13 }
assert my_solution.theMaximumAchievableX(**test_input) == 28
test_input = { "num": 2, "t": 14 }
assert my_solution.theMaximumAchievableX(**test_input) == 30
test_input = { "num": 2, "t": 15 }
assert my_solution.theMaximumAchievableX(**test_input) == 32
test_input = { "num": 2, "t": 16 }
assert my_solution.theMaximumAchievableX(**test_input) == 34
test_input = { "num": 2, "t": 17 }
assert my_solution.theMaximumAchievableX(**test_input) == 36
test_input = { "num": 2, "t": 18 }
assert my_solution.theMaximumAchievableX(**test_input) == 38
test_input = { "num": 2, "t": 19 }
assert my_solution.theMaximumAchievableX(**test_input) == 40
test_input = { "num": 2, "t": 20 }
assert my_solution.theMaximumAchievableX(**test_input) == 42
test_input = { "num": 2, "t": 21 }
assert my_solution.theMaximumAchievableX(**test_input) == 44
test_input = { "num": 2, "t": 22 }
assert my_solution.theMaximumAchievableX(**test_input) == 46
test_input = { "num": 2, "t": 23 }
assert my_solution.theMaximumAchievableX(**test_input) == 48
test_input = { "num": 2, "t": 24 }
assert my_solution.theMaximumAchievableX(**test_input) == 50
test_input = { "num": 2, "t": 25 }
assert my_solution.theMaximumAchievableX(**test_input) == 52
test_input = { "num": 2, "t": 26 }
assert my_solution.theMaximumAchievableX(**test_input) == 54
test_input = { "num": 2, "t": 27 }
assert my_solution.theMaximumAchievableX(**test_input) == 56
test_input = { "num": 2, "t": 28 }
assert my_solution.theMaximumAchievableX(**test_input) == 58
test_input = { "num": 2, "t": 29 }
assert my_solution.theMaximumAchievableX(**test_input) == 60
test_input = { "num": 2, "t": 30 }
assert my_solution.theMaximumAchievableX(**test_input) == 62
test_input = { "num": 2, "t": 31 }
assert my_solution.theMaximumAchievableX(**test_input) == 64
test_input = { "num": 2, "t": 32 }
assert my_solution.theMaximumAchievableX(**test_input) == 66
test_input = { "num": 2, "t": 33 }
assert my_solution.theMaximumAchievableX(**test_input) == 68
test_input = { "num": 2, "t": 34 }
assert my_solution.theMaximumAchievableX(**test_input) == 70
test_input = { "num": 2, "t": 35 }
assert my_solution.theMaximumAchievableX(**test_input) == 72
test_input = { "num": 2, "t": 36 }
assert my_solution.theMaximumAchievableX(**test_input) == 74
test_input = { "num": 2, "t": 37 }
assert my_solution.theMaximumAchievableX(**test_input) == 76
test_input = { "num": 2, "t": 38 }
assert my_solution.theMaximumAchievableX(**test_input) == 78
test_input = { "num": 2, "t": 39 }
assert my_solution.theMaximumAchievableX(**test_input) == 80
test_input = { "num": 2, "t": 40 }
assert my_solution.theMaximumAchievableX(**test_input) == 82
test_input = { "num": 2, "t": 41 }
assert my_solution.theMaximumAchievableX(**test_input) == 84
test_input = { "num": 2, "t": 42 }
assert my_solution.theMaximumAchievableX(**test_input) == 86
test_input = { "num": 2, "t": 43 }
assert my_solution.theMaximumAchievableX(**test_input) == 88
test_input = { "num": 2, "t": 44 }
assert my_solution.theMaximumAchievableX(**test_input) == 90
test_input = { "num": 2, "t": 45 }
assert my_solution.theMaximumAchievableX(**test_input) == 92
test_input = { "num": 2, "t": 46 }
assert my_solution.theMaximumAchievableX(**test_input) == 94
test_input = { "num": 2, "t": 47 }
assert my_solution.theMaximumAchievableX(**test_input) == 96
test_input = { "num": 2, "t": 48 }
assert my_solution.theMaximumAchievableX(**test_input) == 98 | 1,688,869,800 |
weekly-contest-353-maximum-number-of-jumps-to-reach-the-last-index | https://leetcode.com/problems/maximum-number-of-jumps-to-reach-the-last-index | maximum-number-of-jumps-to-reach-the-last-index | {
"questionId": "2855",
"questionFrontendId": "2770",
"title": "Maximum Number of Jumps to Reach the Last Index",
"titleSlug": "maximum-number-of-jumps-to-reach-the-last-index",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 356,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始、由 n 个整数组成的数组 nums 和一个整数 target 。
你的初始位置在下标 0 。在一步操作中,你可以从下标 i 跳跃到任意满足下述条件的下标 j :
* 0 <= i < j < n
* -target <= nums[j] - nums[i] <= target
返回到达下标 n - 1 处所需的 最大跳跃次数 。
如果无法到达下标 n - 1 ,返回 -1 。
示例 1:
输入:nums = [1,3,6,4,1,2], target = 2
输出:3
解释:要想以最大跳跃次数从下标 0 到下标 n - 1 ,可以按下述跳跃序列执行操作:
- 从下标 0 跳跃到下标 1 。
- 从下标 1 跳跃到下标 3 。
- 从下标 3 跳跃到下标 5 。
可以证明,从 0 到 n - 1 的所有方案中,不存在比 3 步更长的跳跃序列。因此,答案是 3 。
示例 2:
输入:nums = [1,3,6,4,1,2], target = 3
输出:5
解释:要想以最大跳跃次数从下标 0 到下标 n - 1 ,可以按下述跳跃序列执行操作:
- 从下标 0 跳跃到下标 1 。
- 从下标 1 跳跃到下标 2 。
- 从下标 2 跳跃到下标 3 。
- 从下标 3 跳跃到下标 4 。
- 从下标 4 跳跃到下标 5 。
可以证明,从 0 到 n - 1 的所有方案中,不存在比 5 步更长的跳跃序列。因此,答案是 5 。
示例 3:
输入:nums = [1,3,6,4,1,2], target = 0
输出:-1
解释:可以证明不存在从 0 到 n - 1 的跳跃序列。因此,答案是 -1 。
提示:
* 2 <= nums.length == n <= 1000
* -109 <= nums[i] <= 109
* 0 <= target <= 2 * 109
"""
class Solution:
def maximumJumps(self, nums: List[int], target: int) -> int:
| 给你一个下标从 0 开始、由 n 个整数组成的数组 nums 和一个整数 target 。
你的初始位置在下标 0 。在一步操作中,你可以从下标 i 跳跃到任意满足下述条件的下标 j :
* 0 <= i < j < n
* -target <= nums[j] - nums[i] <= target
返回到达下标 n - 1 处所需的 最大跳跃次数 。
如果无法到达下标 n - 1 ,返回 -1 。
示例 1:
输入:nums = [1,3,6,4,1,2], target = 2
输出:3
解释:要想以最大跳跃次数从下标 0 到下标 n - 1 ,可以按下述跳跃序列执行操作:
- 从下标 0 跳跃到下标 1 。
- 从下标 1 跳跃到下标 3 。
- 从下标 3 跳跃到下标 5 。
可以证明,从 0 到 n - 1 的所有方案中,不存在比 3 步更长的跳跃序列。因此,答案是 3 。
示例 2:
输入:nums = [1,3,6,4,1,2], target = 3
输出:5
解释:要想以最大跳跃次数从下标 0 到下标 n - 1 ,可以按下述跳跃序列执行操作:
- 从下标 0 跳跃到下标 1 。
- 从下标 1 跳跃到下标 2 。
- 从下标 2 跳跃到下标 3 。
- 从下标 3 跳跃到下标 4 。
- 从下标 4 跳跃到下标 5 。
可以证明,从 0 到 n - 1 的所有方案中,不存在比 5 步更长的跳跃序列。因此,答案是 5 。
示例 3:
输入:nums = [1,3,6,4,1,2], target = 0
输出:-1
解释:可以证明不存在从 0 到 n - 1 的跳跃序列。因此,答案是 -1 。
提示:
* 2 <= nums.length == n <= 1000
* -109 <= nums[i] <= 109
* 0 <= target <= 2 * 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def maximumJumps(self, nums: List[int], target: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,6,4,1,2], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,3,6,4,1,2], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 5
test_input = { "nums": [1,3,6,4,1,2], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,1], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,1], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [0,1], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,0], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,0], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,0], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [0,1,2], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,1,2], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,1,2], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,1,2], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,2,1], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,2,1], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [0,2,1], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,2,1], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,0,2], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,0,2], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,0,2], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,0,2], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,2,0], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,2,0], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,2,0], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,2,0], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [2,0,1], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [2,0,1], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [2,0,1], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [2,0,1], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [2,1,0], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [2,1,0], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [2,1,0], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [2,1,0], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,1,2,3], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,1,2,3], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,1,2,3], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,1,2,3], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,1,2,3], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,1,3,2], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,1,3,2], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,1,3,2], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,1,3,2], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,1,3,2], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,2,1,3], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,2,1,3], "target": 1 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,2,1,3], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,2,1,3], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,2,1,3], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,2,3,1], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,2,3,1], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [0,2,3,1], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,2,3,1], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,2,3,1], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,3,1,2], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,3,1,2], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,3,1,2], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,3,1,2], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,3,1,2], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,3,2,1], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [0,3,2,1], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [0,3,2,1], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [0,3,2,1], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [0,3,2,1], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,0,2,3], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,0,2,3], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,0,2,3], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,0,2,3], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,0,2,3], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,0,3,2], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,0,3,2], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,0,3,2], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,0,3,2], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,0,3,2], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,2,0,3], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,2,0,3], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,2,0,3], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,2,0,3], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,2,0,3], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,2,3,0], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,2,3,0], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,2,3,0], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,2,3,0], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,2,3,0], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,3,0,2], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,3,0,2], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,3,0,2], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 2
test_input = { "nums": [1,3,0,2], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,3,0,2], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,3,2,0], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [1,3,2,0], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [1,3,2,0], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,3,2,0], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [1,3,2,0], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [2,0,1,3], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [2,0,1,3], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1
test_input = { "nums": [2,0,1,3], "target": 2 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [2,0,1,3], "target": 3 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [2,0,1,3], "target": 4 }
assert my_solution.maximumJumps(**test_input) == 3
test_input = { "nums": [2,0,3,1], "target": 0 }
assert my_solution.maximumJumps(**test_input) == -1
test_input = { "nums": [2,0,3,1], "target": 1 }
assert my_solution.maximumJumps(**test_input) == 1 | 1,688,869,800 |
weekly-contest-353-longest-non-decreasing-subarray-from-two-arrays | https://leetcode.com/problems/longest-non-decreasing-subarray-from-two-arrays | longest-non-decreasing-subarray-from-two-arrays | {
"questionId": "2869",
"questionFrontendId": "2771",
"title": "Longest Non-decreasing Subarray From Two Arrays",
"titleSlug": "longest-non-decreasing-subarray-from-two-arrays",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 505,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,长度均为 n 。
让我们定义另一个下标从 0 开始、长度为 n 的整数数组,nums3 。对于范围 [0, n - 1] 的每个下标 i ,你可以将 nums1[i] 或 nums2[i] 的值赋给 nums3[i] 。
你的任务是使用最优策略为 nums3 赋值,以最大化 nums3 中 最长非递减子数组 的长度。
以整数形式表示并返回 nums3 中 最长非递减 子数组的长度。
注意:子数组 是数组中的一个连续非空元素序列。
示例 1:
输入:nums1 = [2,3,1], nums2 = [1,2,1]
输出:2
解释:构造 nums3 的方法之一是:
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]
从下标 0 开始到下标 1 结束,形成了一个长度为 2 的非递减子数组 [2,2] 。
可以证明 2 是可达到的最大长度。
示例 2:
输入:nums1 = [1,3,2,1], nums2 = [2,2,3,4]
输出:4
解释:构造 nums3 的方法之一是:
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]
整个数组形成了一个长度为 4 的非递减子数组,并且是可达到的最大长度。
示例 3:
输入:nums1 = [1,1], nums2 = [2,2]
输出:2
解释:构造 nums3 的方法之一是:
nums3 = [nums1[0], nums1[1]] => [1,1]
整个数组形成了一个长度为 2 的非递减子数组,并且是可达到的最大长度。
提示:
* 1 <= nums1.length == nums2.length == n <= 105
* 1 <= nums1[i], nums2[i] <= 109
"""
class Solution:
def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:
| 给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,长度均为 n 。
让我们定义另一个下标从 0 开始、长度为 n 的整数数组,nums3 。对于范围 [0, n - 1] 的每个下标 i ,你可以将 nums1[i] 或 nums2[i] 的值赋给 nums3[i] 。
你的任务是使用最优策略为 nums3 赋值,以最大化 nums3 中 最长非递减子数组 的长度。
以整数形式表示并返回 nums3 中 最长非递减 子数组的长度。
注意:子数组 是数组中的一个连续非空元素序列。
示例 1:
输入:nums1 = [2,3,1], nums2 = [1,2,1]
输出:2
解释:构造 nums3 的方法之一是:
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]
从下标 0 开始到下标 1 结束,形成了一个长度为 2 的非递减子数组 [2,2] 。
可以证明 2 是可达到的最大长度。
示例 2:
输入:nums1 = [1,3,2,1], nums2 = [2,2,3,4]
输出:4
解释:构造 nums3 的方法之一是:
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]
整个数组形成了一个长度为 4 的非递减子数组,并且是可达到的最大长度。
示例 3:
输入:nums1 = [1,1], nums2 = [2,2]
输出:2
解释:构造 nums3 的方法之一是:
nums3 = [nums1[0], nums1[1]] => [1,1]
整个数组形成了一个长度为 2 的非递减子数组,并且是可达到的最大长度。
提示:
* 1 <= nums1.length == nums2.length == n <= 105
* 1 <= nums1[i], nums2[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums1": [2,3,1], "nums2": [1,2,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [1,3,2,1], "nums2": [2,2,3,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 4
test_input = { "nums1": [1,1], "nums2": [2,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [1], "nums2": [1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [1], "nums2": [2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [1,4], "nums2": [4,19] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [1,8], "nums2": [10,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [1,11], "nums2": [9,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [1,13], "nums2": [18,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [1,19], "nums2": [12,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [1,19], "nums2": [18,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [2,20], "nums2": [1,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,5], "nums2": [13,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,6], "nums2": [10,12] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,7], "nums2": [8,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,8], "nums2": [15,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,9], "nums2": [11,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,12], "nums2": [7,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,12], "nums2": [20,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [3,20], "nums2": [5,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [4,2], "nums2": [10,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [4,12], "nums2": [6,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [4,15], "nums2": [3,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [5,5], "nums2": [19,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [5,7], "nums2": [19,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [5,11], "nums2": [2,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [5,14], "nums2": [8,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [5,15], "nums2": [16,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [5,20], "nums2": [4,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [5,20], "nums2": [10,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [6,7], "nums2": [3,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [6,14], "nums2": [5,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [6,14], "nums2": [18,6] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [6,16], "nums2": [16,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [6,17], "nums2": [4,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [7,3], "nums2": [16,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [7,4], "nums2": [15,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [7,9], "nums2": [3,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [7,10], "nums2": [10,14] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [7,11], "nums2": [5,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [7,12], "nums2": [20,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [7,20], "nums2": [12,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [8,5], "nums2": [13,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [8,11], "nums2": [9,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [8,16], "nums2": [9,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [8,17], "nums2": [2,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [8,18], "nums2": [16,12] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [4,2], "nums2": [10,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [9,1], "nums2": [11,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [9,6], "nums2": [8,14] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [9,9], "nums2": [11,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [9,12], "nums2": [20,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [9,15], "nums2": [20,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [9,16], "nums2": [11,15] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [9,19], "nums2": [17,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [10,19], "nums2": [17,10] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [11,1], "nums2": [3,11] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [11,3], "nums2": [9,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [11,6], "nums2": [9,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [11,19], "nums2": [17,11] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [11,69], "nums2": [26,62] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [12,1], "nums2": [10,12] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [12,10], "nums2": [16,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [13,6], "nums2": [20,13] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [13,16], "nums2": [5,13] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [14,2], "nums2": [2,14] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [14,4], "nums2": [2,13] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [15,10], "nums2": [17,15] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [15,11], "nums2": [19,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [16,9], "nums2": [5,16] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [16,17], "nums2": [9,16] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [17,8], "nums2": [11,10] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [17,10], "nums2": [9,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [17,10], "nums2": [18,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [17,11], "nums2": [19,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [17,14], "nums2": [17,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [17,17], "nums2": [15,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [17,17], "nums2": [16,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [18,4], "nums2": [1,6] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [18,9], "nums2": [10,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [18,9], "nums2": [17,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [18,10], "nums2": [1,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [18,104], "nums2": [117,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [19,2], "nums2": [1,19] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [19,5], "nums2": [15,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [19,5], "nums2": [52,10] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [19,15], "nums2": [12,19] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [19,15], "nums2": [18,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [20,1], "nums2": [1,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [20,5], "nums2": [2,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [20,5], "nums2": [14,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1
test_input = { "nums1": [20,7], "nums2": [12,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [20,12], "nums2": [2,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [20,16], "nums2": [8,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [20,18], "nums2": [18,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [25,83], "nums2": [28,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [26,47], "nums2": [87,26] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [28,41], "nums2": [87,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [36,53], "nums2": [66,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2
test_input = { "nums1": [44,34], "nums2": [53,44] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2 | 1,688,869,800 |
weekly-contest-353-apply-operations-to-make-all-array-elements-equal-to-zero | https://leetcode.com/problems/apply-operations-to-make-all-array-elements-equal-to-zero | apply-operations-to-make-all-array-elements-equal-to-zero | {
"questionId": "2878",
"questionFrontendId": "2772",
"title": "Apply Operations to Make All Array Elements Equal to Zero",
"titleSlug": "apply-operations-to-make-all-array-elements-equal-to-zero",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 339,
"dislikes": 19,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 和一个正整数 k 。
你可以对数组执行下述操作 任意次 :
* 从数组中选出长度为 k 的 任一 子数组,并将子数组中每个元素都 减去 1 。
如果你可以使数组中的所有元素都等于 0 ,返回 true ;否则,返回 false 。
子数组 是数组中的一个非空连续元素序列。
示例 1:
输入:nums = [2,2,3,1,1,0], k = 3
输出:true
解释:可以执行下述操作:
- 选出子数组 [2,2,3] ,执行操作后,数组变为 nums = [1,1,2,1,1,0] 。
- 选出子数组 [2,1,1] ,执行操作后,数组变为 nums = [1,1,1,0,0,0] 。
- 选出子数组 [1,1,1] ,执行操作后,数组变为 nums = [0,0,0,0,0,0] 。
示例 2:
输入:nums = [1,3,1,1], k = 2
输出:false
解释:无法使数组中的所有元素等于 0 。
提示:
* 1 <= k <= nums.length <= 105
* 0 <= nums[i] <= 106
"""
class Solution:
def checkArray(self, nums: List[int], k: int) -> bool:
| 给你一个下标从 0 开始的整数数组 nums 和一个正整数 k 。
你可以对数组执行下述操作 任意次 :
* 从数组中选出长度为 k 的 任一 子数组,并将子数组中每个元素都 减去 1 。
如果你可以使数组中的所有元素都等于 0 ,返回 true ;否则,返回 false 。
子数组 是数组中的一个非空连续元素序列。
示例 1:
输入:nums = [2,2,3,1,1,0], k = 3
输出:true
解释:可以执行下述操作:
- 选出子数组 [2,2,3] ,执行操作后,数组变为 nums = [1,1,2,1,1,0] 。
- 选出子数组 [2,1,1] ,执行操作后,数组变为 nums = [1,1,1,0,0,0] 。
- 选出子数组 [1,1,1] ,执行操作后,数组变为 nums = [0,0,0,0,0,0] 。
示例 2:
输入:nums = [1,3,1,1], k = 2
输出:false
解释:无法使数组中的所有元素等于 0 。
提示:
* 1 <= k <= nums.length <= 105
* 0 <= nums[i] <= 106
请完成下面的代码来解决上述问题:
```python
class Solution:
def checkArray(self, nums: List[int], k: int) -> bool:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,1,1], "k": 2 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [2,2,3,1,1,0], "k": 3 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [24,24,14,37,31,88,94,38,94,0,100,100,4,46,5,50,0,33,22,25,0], "k": 10 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [60,72,87,89,63,52,64,62,31,37,57,83,98,94,92,77,94,91,87,100,91,91,50,26], "k": 4 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [22,4,1,25,68,30,97,99,100,22,20,39,85,68,3,1,1,74], "k": 4 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [63,40,30,0,72,53], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [27,99,7,1,94,63,84,46,76,35,97,77,19,72,3], "k": 2 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [60,78,96,97,97,97,49,7,97,97,97,99,97,97,97,97,85,97,97,97,37,5,1], "k": 20 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [34,34,99,93,93,26,99,100,94,94,82,86,100,100,87,100,100,100,100,100,63,100,100,66,17,10,8,7,3,1], "k": 23 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [67,98,97,99,98,97,97,96,99,99,99,42,68,18,99,44,95,79,1,16,49,1,2,2,0], "k": 16 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [59,60,99,99,99,99,99,99,99,40,39,0], "k": 9 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [12,87,91,18], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [0,0,51,67,80,98,88,75,89,83,100,70,77,82,57,100,80,69,19,17], "k": 3 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [22], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [52,92,51,24,23,79,100,94,78,96,38,14,72,27,99,94,32,67,43,31,88,8], "k": 4 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [8,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,43,0], "k": 25 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [0,0,39,84,86,94,55,10,8,0], "k": 4 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [12,79,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,95,83,16,0], "k": 24 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [15,11,99,86,58,23,82,100,100,80,58,58,84,57,0,25,6], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [99,0,82,66,3,25,92,41,3,0,46], "k": 7 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [18,52,56,96,98,82,76,87,2,61,88,100], "k": 2 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [0,6,100,74,4,50,100,92,18,70,15,88,0,24], "k": 9 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [0,0,33,72,86,53,14], "k": 3 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [67,0,68,97,94], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [0,0,8,64,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,89,81,25], "k": 25 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [84,0,68,95,95,0,25,0,7,71,4,68,23,97,80,0], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [95,92,99,99,2,100,100,100,100,100,100,100,8,57,65,69,69,100,100,100,100,100,100,100,100,0,79,72,32], "k": 12 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [7,33,97,98,100,100,74,98,95,13,39,31,82,51,28,68,37,59,21,5,66,77,89,6,0], "k": 6 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [48,48,48,48,48], "k": 5 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [42,60,56,99,72,2,100,51,65,14,13,51,1,55,56,61,99,49,96,2], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [8,82,98,99,66,39,71,100,81,85,100,19,96,0,2,85,40,0,19,0], "k": 11 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [92,96,96,96,97,99,99,99,99,99,99,99,99,22,18,18,18,17,15,15,15,15,15,15,15,15], "k": 13 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [89,8,8,8,8,8,8,8,8,8,8,8], "k": 12 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [4,19,0,74,0,26,94,25,99,35,0], "k": 10 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [79,4,27,35,16,27,85,92,75,99,7,98,86,92,33,8,96,44,21,52,34], "k": 4 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [75,18,0,81,18,16,51,0], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [74,74,74,74,74,74,74,74,74], "k": 9 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [5,5,5,9,37,81,95,95,95,98,98,99,100,100,97,99,100,96,68,24,10,10,10,7,7,6,5,5,3,1], "k": 14 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [81,100,99,99,68,66,56,100,74,63,2,84,23,67,93,92,56,90,18,57,100,33,88,26,100,72,93,57,28,17], "k": 4 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [62,76,96,12,0,20,63,29,96,97,8,18,56], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [31,60,97,71,53,46,63,50,91,82,40,79,96,100,55,55,57,39,50,98,72,37,27,55], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [100,65,78,59,17,17], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [57,10,53,20,40,42,64,94,82,67,100,3,22,67,95,28,61,74,67,99,100,46,67,67,76,31,99,26,85], "k": 24 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [34,76,66,48,13,89,22,24,70,17,17,42,100,2,96,8], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [92,94,2,6,6], "k": 2 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [100,17,95], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [20,27,75,96,97,84,90,77,65,64,57,44,9,0], "k": 5 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [41,39,98,99,100,100,100,100,100,100,100,100,100,100,100,37,5,2,1,0,0,92,0,0,0], "k": 15 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [45,90,90,99,99,99,99,99,54,9,9,0], "k": 8 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [41,18,59,3,52,59,59,55,1,34,67,1,2,13,60,40,5,0,4,47,73,96,33,59,98], "k": 24 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [53,88,96,97,97,97,97,97,97,97,44,9,1,0], "k": 10 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [90,74,13,81,34,10,29,18,61,94,43,99,86,0], "k": 11 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [44,47,72,0,0,68,97,67], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [77,78,19,97,79], "k": 2 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [36,61,86,61,0,61,61,61,61,61,61,61,61,65,62,61,95,61,61,61,80,66,61,61,25], "k": 24 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [0,44,92,94,94,50,95,98,98,99,6,1,54,58,94,94,41,36], "k": 4 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [60,71,76,93,98,98,98,98,38,27,22,5], "k": 8 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [94,94,99,94,86,100,32,96,59,69,99,95,75], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [6,55,95,95,95,96,95,95,90,5,0], "k": 8 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [62,90,90,90,99,57,61,96,96,97,99,78,84,0], "k": 5 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [57,77,91,66,46,32], "k": 3 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [86,87,92,20,42,99,100,97,17,18,48,11,60,98,96,28,59,5,18,56,62,35,100,87,51,54,77,98,61], "k": 7 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [21,26,20,2,38,22,0,96,79,93,9,67,34], "k": 12 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [92,14,95,100,53,99,100,100,50,99,99,99,93,99,100,15,71,100,7,5,48,65,6], "k": 18 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [64,14,64,8,0,83,17,68,5,98,36], "k": 7 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [50,50,50,50,93,92,84,96,96,96,96,96,96,96,96,46,46,46,46,33,4,1,0], "k": 15 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [79,90,100,96,22,2,100,10,100,100,78,85,54,7,35,97,98,98,98,98,33,38,4,14,63,23], "k": 10 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [49,74,96,93,93,99,100,95,73,100,41,95,99,22,13,52,19,13,11,80], "k": 7 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [6,100,100,100,100,100,98,4,74,89,89,89,85,85,15], "k": 6 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [69,48,8,3,82,10,88,76,32,95,68,30,97,64,32,62,86], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [87,87,87], "k": 3 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [88,98,99,99,100,100,58,82,85,87,86,86,40,6,2], "k": 6 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [26,26,46,46,70,70,95,97,98,98,98,72,72,52,52,28,28,3,1], "k": 11 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [25], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [71,87,87,96,99,36,61,87,98,97,93,96,71,59,75,71,27,26,18], "k": 5 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [39,90,15,100,52,27,100,67,99,79,4,78,95,84,2], "k": 4 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [39,39,39,39,39,39,39,39,39,39], "k": 10 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [58,72,93,99,99,64,50,29,23,23,0,0,0,0,0], "k": 5 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [30,86,23], "k": 3 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [65,69,74,79,90,95,98,99,99,99,100,100,100,100,100,100,35,31,26,21,10,5,2,1,1,1,0,0], "k": 16 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [69,2,71,4,97,97,100,26,100,100,100,100,100,77,13,8,13,3,3,72,0,0,0], "k": 13 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [77,90,94,94,98,98,28,29,36,91,91,94,87,73,80,81,77,74,74,74,56], "k": 6 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [93], "k": 1 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [70,91,100,17,80,94,35,83,33,0], "k": 4 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [79,87,87,99,50,77,97,85,92,91,71,71,34], "k": 4 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [75,9,96,100,100,77,7,99,100,99,94,71,5,78,3,8,8,7,59,8,6], "k": 10 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [36,37,69,88,88,96,96,96,96,96,96,96,96,96,96,96,96,96,96,96,96,60,59,27,8,8,0], "k": 21 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [94,94,96,97,98,11,64,85,84,87,98,45,65,83,79,63,74,79,61,61,59,48], "k": 5 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [80,80,98,98,98,18,18,0,0], "k": 5 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [14,100,17,100,13,85,100,100,14,100,100,1,83,0], "k": 11 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [98,74,91,70], "k": 2 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [0,29,29,93,93,93,93,93,93,93,93,93,64,64], "k": 11 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,0], "k": 15 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [34,89,92,96,96,98,99,98,79,4,100,100,12,99,100,44,100,66,25,8,4,98,2,1,97,70,1,15,0,88], "k": 17 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [95,95,98,99,99,99,100,100,100,100,100,100,100,5,5,2,1,1,1], "k": 13 }
assert my_solution.checkArray(**test_input) == True
test_input = { "nums": [15,58,78,10,68,49,100,94,30,14,72], "k": 5 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [9,44,70,75,28,23,11,37,69,34,61], "k": 10 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [70,51,47,100,59,66,17,98,60], "k": 4 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [92,33,58,88], "k": 2 }
assert my_solution.checkArray(**test_input) == False
test_input = { "nums": [99,15,91,32,7,98], "k": 3 }
assert my_solution.checkArray(**test_input) == False | 1,688,869,800 |
biweekly-contest-108-longest-alternating-subarray | https://leetcode.com/problems/longest-alternating-subarray | longest-alternating-subarray | {
"questionId": "2870",
"questionFrontendId": "2765",
"title": "Longest Alternating Subarray",
"titleSlug": "longest-alternating-subarray",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 181,
"dislikes": 153,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 。如果 nums 中长度为 m 的子数组 s 满足以下条件,我们称它是一个 交替子序列 :
* m 大于 1 。
* s1 = s0 + 1 。
* 下标从 0 开始的子数组 s 与数组 [s0, s1, s0, s1,...,s(m-1) % 2] 一样。也就是说,s1 - s0 = 1 ,s2 - s1 = -1 ,s3 - s2 = 1 ,s4 - s3 = -1 ,以此类推,直到 s[m - 1] - s[m - 2] = (-1)m 。
请你返回 nums 中所有 交替 子数组中,最长的长度,如果不存在交替子数组,请你返回 -1 。
子数组是一个数组中一段连续 非空 的元素序列。
示例 1:
输入:nums = [2,3,4,3,4]
输出:4
解释:交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ,长度为4 。
示例 2:
输入:nums = [4,5,6]
输出:2
解释:[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。
提示:
* 2 <= nums.length <= 100
* 1 <= nums[i] <= 104
"""
class Solution:
def alternatingSubarray(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始的整数数组 nums 。如果 nums 中长度为 m 的子数组 s 满足以下条件,我们称它是一个 交替子序列 :
* m 大于 1 。
* s1 = s0 + 1 。
* 下标从 0 开始的子数组 s 与数组 [s0, s1, s0, s1,...,s(m-1) % 2] 一样。也就是说,s1 - s0 = 1 ,s2 - s1 = -1 ,s3 - s2 = 1 ,s4 - s3 = -1 ,以此类推,直到 s[m - 1] - s[m - 2] = (-1)m 。
请你返回 nums 中所有 交替 子数组中,最长的长度,如果不存在交替子数组,请你返回 -1 。
子数组是一个数组中一段连续 非空 的元素序列。
示例 1:
输入:nums = [2,3,4,3,4]
输出:4
解释:交替子数组有 [3,4] ,[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ,长度为4 。
示例 2:
输入:nums = [4,5,6]
输出:2
解释:[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。
提示:
* 2 <= nums.length <= 100
* 1 <= nums[i] <= 104
请完成下面的代码来解决上述问题:
```python
class Solution:
def alternatingSubarray(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,3,4,3,4] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [4,5,6] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [31,32,31,32,33] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [21,9,5] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [42,43,44,43,44,43,44,45,46] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [13,14,15,14] }
assert my_solution.alternatingSubarray(**test_input) == 3
test_input = { "nums": [74,75,74,75,74,75,74,75] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [77,78,79,78,79,78,79,78,79,80] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [88,42,53] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [64,65,64,65,64,65,66,65,66,65] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [99,100,99,100] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [21,22] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [23,24,23,24,25,24,25,24,25] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [20,9,15,15] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [92,93,92,93,92] }
assert my_solution.alternatingSubarray(**test_input) == 5
test_input = { "nums": [24,25,26] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [51,52,53,52,53,52,53,54,53] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [65,66,65,66,67,68,69] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [29,2,5,24] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [26,27,26,27,28,27,28,27,28] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [21,22,21,22,21,22] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [94,95,94,95,94] }
assert my_solution.alternatingSubarray(**test_input) == 5
test_input = { "nums": [82,83,84,83,84,83,84,83] }
assert my_solution.alternatingSubarray(**test_input) == 7
test_input = { "nums": [14,30,29,49,3,23,44,21,26,52] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [4,5,4,5,6,5,6] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [62,63] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [24,25,26,25,26,25,26,25,26] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [55,56,55,56,55,56,55,56,57,56] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [52,77,42,21] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [80,81] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [50,51,50,51,50,51,50,51,50] }
assert my_solution.alternatingSubarray(**test_input) == 9
test_input = { "nums": [83,84,83] }
assert my_solution.alternatingSubarray(**test_input) == 3
test_input = { "nums": [17,18,17,18,19,18,19,20,19,20] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [5,14,8,12,5,4] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [35,36,35,36,35,36,35,36,35,36] }
assert my_solution.alternatingSubarray(**test_input) == 10
test_input = { "nums": [8,9,8,9,8,9] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [59,60] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [55,56,55] }
assert my_solution.alternatingSubarray(**test_input) == 3
test_input = { "nums": [47,46,65,37,24,54,39,70] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [45,46,45,46,45,46,45] }
assert my_solution.alternatingSubarray(**test_input) == 7
test_input = { "nums": [78,79,78,79,78,79,78,79,80,79] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [65,66,65,66,65,66,67,68] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [62,63,62,63,62,63] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [7,10,5,2,11,3,9,12,9,11] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [79,80,79,80,79,80] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [19,20,21,20,21,22] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [84,85,86,85,86,85,86,87,88,87] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [54,55] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [71,14,24,13,21,14,18,84,37,2] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [26,27,26] }
assert my_solution.alternatingSubarray(**test_input) == 3
test_input = { "nums": [53,54,53,54,53] }
assert my_solution.alternatingSubarray(**test_input) == 5
test_input = { "nums": [67,68,67,68,67,68,69,70,69,70] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [85,86,85,86,85,86,85,86,85,86] }
assert my_solution.alternatingSubarray(**test_input) == 10
test_input = { "nums": [22,16,27,22,44,10] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [27,28,27,28,27,28,27,28,29,28] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [54,55,54,55,54,55,54,55,56,57] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [24,25,26,27,28] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [55,56] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [13,6,6,8,12,7,1] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [40,41,40,41,40,41,40,41] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [10,11,10,11,10,11,12] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [58,59,58,59] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [1,15,44,74,56,41,48,71] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [89,90,89,90,89,90] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [4,5,4,5,4,5,6] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [50,51,52,53,52,53] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [44,45,46,45,46,45,46,47,48] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [16,3,25,12,2,19,1,26] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [17,18,19,20,19,20] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [91,92,93,92,93] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [28,29,28,29,28,29,30] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [88,89,88,89,88,89] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [32,33,32,33,32,33,34] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [61,62,61,62,63,62,63,64,63] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [6,7,6,7] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [20,21,20,21,20] }
assert my_solution.alternatingSubarray(**test_input) == 5
test_input = { "nums": [14,6,21] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [12,13,12,13,12,13,12,13,12] }
assert my_solution.alternatingSubarray(**test_input) == 9
test_input = { "nums": [33,34,33,34,33,34,33] }
assert my_solution.alternatingSubarray(**test_input) == 7
test_input = { "nums": [92,93,92] }
assert my_solution.alternatingSubarray(**test_input) == 3
test_input = { "nums": [93,94,95,94,95,96,95,96,97] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [8,4,27] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [30,31,32,31,32,33,34,33] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [26,27,26,27,26,27] }
assert my_solution.alternatingSubarray(**test_input) == 6
test_input = { "nums": [67,68,69] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [83,84,85,84,85] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [6,26,4,2] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [81,82,81,82,81,82,81,82,83] }
assert my_solution.alternatingSubarray(**test_input) == 8
test_input = { "nums": [58,59] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [82,83,82,83,82,83,82,83,82] }
assert my_solution.alternatingSubarray(**test_input) == 9
test_input = { "nums": [48,49] }
assert my_solution.alternatingSubarray(**test_input) == 2
test_input = { "nums": [8,6,2] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [69,70,69,70,69,70,69,70,69] }
assert my_solution.alternatingSubarray(**test_input) == 9
test_input = { "nums": [28,29,28,29] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [82,83,82,83,84] }
assert my_solution.alternatingSubarray(**test_input) == 4
test_input = { "nums": [97,98,97,98,97,98,97] }
assert my_solution.alternatingSubarray(**test_input) == 7
test_input = { "nums": [2,2,1] }
assert my_solution.alternatingSubarray(**test_input) == -1
test_input = { "nums": [84,85,84,85,84,85,84] }
assert my_solution.alternatingSubarray(**test_input) == 7
test_input = { "nums": [21,22,21,22] }
assert my_solution.alternatingSubarray(**test_input) == 4 | 1,688,826,600 |
biweekly-contest-108-relocate-marbles | https://leetcode.com/problems/relocate-marbles | relocate-marbles | {
"questionId": "2834",
"questionFrontendId": "2766",
"title": "Relocate Marbles",
"titleSlug": "relocate-marbles",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 170,
"dislikes": 13,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums ,表示一些石块的初始位置。再给你两个长度 相等 下标从 0 开始的整数数组 moveFrom 和 moveTo 。
在 moveFrom.length 次操作内,你可以改变石块的位置。在第 i 次操作中,你将位置在 moveFrom[i] 的所有石块移到位置 moveTo[i] 。
完成这些操作后,请你按升序返回所有 有 石块的位置。
注意:
* 如果一个位置至少有一个石块,我们称这个位置 有 石块。
* 一个位置可能会有多个石块。
示例 1:
输入:nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5]
输出:[5,6,8,9]
解释:一开始,石块在位置 1,6,7,8 。
第 i = 0 步操作中,我们将位置 1 处的石块移到位置 2 处,位置 2,6,7,8 有石块。
第 i = 1 步操作中,我们将位置 7 处的石块移到位置 9 处,位置 2,6,8,9 有石块。
第 i = 2 步操作中,我们将位置 2 处的石块移到位置 5 处,位置 5,6,8,9 有石块。
最后,至少有一个石块的位置为 [5,6,8,9] 。
示例 2:
输入:nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2]
输出:[2]
解释:一开始,石块在位置 [1,1,3,3] 。
第 i = 0 步操作中,我们将位置 1 处的石块移到位置 2 处,有石块的位置为 [2,2,3,3] 。
第 i = 1 步操作中,我们将位置 3 处的石块移到位置 2 处,有石块的位置为 [2,2,2,2] 。
由于 2 是唯一有石块的位置,我们返回 [2] 。
提示:
* 1 <= nums.length <= 105
* 1 <= moveFrom.length <= 105
* moveFrom.length == moveTo.length
* 1 <= nums[i], moveFrom[i], moveTo[i] <= 109
* 测试数据保证在进行第 i 步操作时,moveFrom[i] 处至少有一个石块。
"""
class Solution:
def relocateMarbles(self, nums: List[int], moveFrom: List[int], moveTo: List[int]) -> List[int]:
| 给你一个下标从 0 开始的整数数组 nums ,表示一些石块的初始位置。再给你两个长度 相等 下标从 0 开始的整数数组 moveFrom 和 moveTo 。
在 moveFrom.length 次操作内,你可以改变石块的位置。在第 i 次操作中,你将位置在 moveFrom[i] 的所有石块移到位置 moveTo[i] 。
完成这些操作后,请你按升序返回所有 有 石块的位置。
注意:
* 如果一个位置至少有一个石块,我们称这个位置 有 石块。
* 一个位置可能会有多个石块。
示例 1:
输入:nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5]
输出:[5,6,8,9]
解释:一开始,石块在位置 1,6,7,8 。
第 i = 0 步操作中,我们将位置 1 处的石块移到位置 2 处,位置 2,6,7,8 有石块。
第 i = 1 步操作中,我们将位置 7 处的石块移到位置 9 处,位置 2,6,8,9 有石块。
第 i = 2 步操作中,我们将位置 2 处的石块移到位置 5 处,位置 5,6,8,9 有石块。
最后,至少有一个石块的位置为 [5,6,8,9] 。
示例 2:
输入:nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2]
输出:[2]
解释:一开始,石块在位置 [1,1,3,3] 。
第 i = 0 步操作中,我们将位置 1 处的石块移到位置 2 处,有石块的位置为 [2,2,3,3] 。
第 i = 1 步操作中,我们将位置 3 处的石块移到位置 2 处,有石块的位置为 [2,2,2,2] 。
由于 2 是唯一有石块的位置,我们返回 [2] 。
提示:
* 1 <= nums.length <= 105
* 1 <= moveFrom.length <= 105
* moveFrom.length == moveTo.length
* 1 <= nums[i], moveFrom[i], moveTo[i] <= 109
* 测试数据保证在进行第 i 步操作时,moveFrom[i] 处至少有一个石块。
请完成下面的代码来解决上述问题:
```python
class Solution:
def relocateMarbles(self, nums: List[int], moveFrom: List[int], moveTo: List[int]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "nums": [1,6,7,8], "moveFrom": [1,7,2], "moveTo": [2,9,5] }
assert my_solution.relocateMarbles(**test_input) == [5,6,8,9]
test_input = { "nums": [1,1,3,3], "moveFrom": [1,3], "moveTo": [2,2] }
assert my_solution.relocateMarbles(**test_input) == [2]
test_input = { "nums": [5,7,8,15], "moveFrom": [5,7,8,9], "moveTo": [9,15,2,7] }
assert my_solution.relocateMarbles(**test_input) == [2,7,15]
test_input = { "nums": [4,6,6,9,18], "moveFrom": [18,6,17,4,9,19,2], "moveTo": [23,17,20,19,11,2,20] }
assert my_solution.relocateMarbles(**test_input) == [11,20,23]
test_input = { "nums": [3,4], "moveFrom": [4,3,1,2,2,3,2,4,1], "moveTo": [3,1,2,2,3,2,4,1,1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [5,13,22,23,23,33], "moveFrom": [13,5,12], "moveTo": [1,12,13] }
assert my_solution.relocateMarbles(**test_input) == [1,13,22,23,33]
test_input = { "nums": [21,24,35,72,77,82,82,96,97,97], "moveFrom": [82,76,3,97], "moveTo": [76,3,52,27] }
assert my_solution.relocateMarbles(**test_input) == [21,24,27,35,52,72,77,96]
test_input = { "nums": [4,6,17,41,46,46,52,57], "moveFrom": [4], "moveTo": [62] }
assert my_solution.relocateMarbles(**test_input) == [6,17,41,46,52,57,62]
test_input = { "nums": [1,4,10,24,46,55,61,63,71], "moveFrom": [10,52,1,80,63,55,4,46,71,24], "moveTo": [52,42,80,55,50,62,60,17,46,38] }
assert my_solution.relocateMarbles(**test_input) == [17,38,42,46,50,60,61,62]
test_input = { "nums": [8,9,16,17,23], "moveFrom": [8,5,16,2,9], "moveTo": [5,20,2,18,22] }
assert my_solution.relocateMarbles(**test_input) == [17,18,20,22,23]
test_input = { "nums": [12,37,46,47,49,55,59,65,71,88], "moveFrom": [88,59,71], "moveTo": [81,39,73] }
assert my_solution.relocateMarbles(**test_input) == [12,37,39,46,47,49,55,65,73,81]
test_input = { "nums": [2,45,45,48,51,57,67,73,78,78], "moveFrom": [78,67,45,34,51,62,48,95,2,67], "moveTo": [34,65,62,95,62,12,85,67,79,71] }
assert my_solution.relocateMarbles(**test_input) == [12,57,65,71,73,79,85]
test_input = { "nums": [1,2], "moveFrom": [1,2,3], "moveTo": [2,3,2] }
assert my_solution.relocateMarbles(**test_input) == [2]
test_input = { "nums": [7,19,28,34,36,36,47], "moveFrom": [36,33,34,28,41,19,14,47,28,40], "moveTo": [33,41,27,47,14,40,46,28,42,16] }
assert my_solution.relocateMarbles(**test_input) == [7,16,27,42,46]
test_input = { "nums": [1,1,1], "moveFrom": [1], "moveTo": [7] }
assert my_solution.relocateMarbles(**test_input) == [7]
test_input = { "nums": [1], "moveFrom": [1,1,1,1,1,1], "moveTo": [1,1,1,1,1,1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [5,9,17,20,29,29], "moveFrom": [20,5,1,29,22,21,9,36,33,1], "moveTo": [1,22,21,36,36,15,33,1,3,15] }
assert my_solution.relocateMarbles(**test_input) == [3,15,17]
test_input = { "nums": [1], "moveFrom": [1,1,1,1], "moveTo": [1,1,1,1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [27,41,50,52,57,60,65,67,70], "moveFrom": [52,67,70,50,57,27,47], "moveTo": [45,45,61,47,21,65,60] }
assert my_solution.relocateMarbles(**test_input) == [21,41,45,60,61,65]
test_input = { "nums": [2,3,7], "moveFrom": [2,7,8,8,3,5,1,4], "moveTo": [8,5,8,1,4,4,4,5] }
assert my_solution.relocateMarbles(**test_input) == [5]
test_input = { "nums": [4,6,8,10], "moveFrom": [8,6], "moveTo": [4,3] }
assert my_solution.relocateMarbles(**test_input) == [3,4,10]
test_input = { "nums": [2,4,29,34,41,44,48], "moveFrom": [29,3,44,48,2,43,4], "moveTo": [3,24,43,42,24,8,6] }
assert my_solution.relocateMarbles(**test_input) == [6,8,24,34,41,42]
test_input = { "nums": [12,16,22,28,36,42,60,63], "moveFrom": [42,28,12,22], "moveTo": [22,63,14,45] }
assert my_solution.relocateMarbles(**test_input) == [14,16,36,45,60,63]
test_input = { "nums": [12,18,21,21,31,38,39,41,84,90], "moveFrom": [41,31,12,84,9,39,21,62], "moveTo": [24,17,58,9,62,36,23,90] }
assert my_solution.relocateMarbles(**test_input) == [17,18,23,24,36,38,58,90]
test_input = { "nums": [12,23,30,35,46,53,64,74,81], "moveFrom": [53,74,54,48,52,64,35,30,46,29], "moveTo": [54,48,52,47,53,29,52,10,44,28] }
assert my_solution.relocateMarbles(**test_input) == [10,12,23,28,44,47,52,53,81]
test_input = { "nums": [2,10,13,14,16,30], "moveFrom": [2,6,14,16,25,13,30], "moveTo": [6,30,25,1,32,17,11] }
assert my_solution.relocateMarbles(**test_input) == [1,10,11,17,32]
test_input = { "nums": [1,6,10,11,18,22,30], "moveFrom": [10,18,1,30,6], "moveTo": [1,37,28,38,15] }
assert my_solution.relocateMarbles(**test_input) == [11,15,22,28,37,38]
test_input = { "nums": [3,9,10,13], "moveFrom": [9,3,13,10,5,11,8], "moveTo": [11,5,11,15,8,5,14] }
assert my_solution.relocateMarbles(**test_input) == [5,14,15]
test_input = { "nums": [15,31,32,52,61,65,78,84,93,100], "moveFrom": [15,32,93,3,78,65,61,84], "moveTo": [61,3,8,55,23,87,95,44] }
assert my_solution.relocateMarbles(**test_input) == [8,23,31,44,52,55,87,95,100]
test_input = { "nums": [1,2], "moveFrom": [1,2,1,1,1,3,4,4], "moveTo": [1,1,1,1,3,4,4,2] }
assert my_solution.relocateMarbles(**test_input) == [2]
test_input = { "nums": [2,2,2], "moveFrom": [2,8,5,9], "moveTo": [8,5,9,2] }
assert my_solution.relocateMarbles(**test_input) == [2]
test_input = { "nums": [3,10,11,27,58,59,61,66,68], "moveFrom": [59,61,3,15], "moveTo": [15,68,77,52] }
assert my_solution.relocateMarbles(**test_input) == [10,11,27,52,58,66,68,77]
test_input = { "nums": [2,9,9], "moveFrom": [9,2,8,9,4,6,7,1,5,5], "moveTo": [4,8,9,4,6,7,1,5,5,5] }
assert my_solution.relocateMarbles(**test_input) == [5]
test_input = { "nums": [1,18,24,25,29,31], "moveFrom": [18,25,29,18,23], "moveTo": [3,23,18,18,8] }
assert my_solution.relocateMarbles(**test_input) == [1,3,8,18,24,31]
test_input = { "nums": [2,18,38,38,48,50,51,61,71], "moveFrom": [61,71,2,18,47,22,24,51], "moveTo": [58,38,22,47,68,24,47,60] }
assert my_solution.relocateMarbles(**test_input) == [38,47,48,50,58,60,68]
test_input = { "nums": [11,11,35,35,38,43,45], "moveFrom": [35,11,2,27,38,45,47,17], "moveTo": [2,47,27,17,47,24,35,21] }
assert my_solution.relocateMarbles(**test_input) == [21,24,35,43]
test_input = { "nums": [4,11,15,28,36,42,45,57], "moveFrom": [57,32,36,11,52,42,55,4], "moveTo": [32,55,39,52,11,54,31,56] }
assert my_solution.relocateMarbles(**test_input) == [11,15,28,31,39,45,54,56]
test_input = { "nums": [2,4], "moveFrom": [4,2], "moveTo": [4,1] }
assert my_solution.relocateMarbles(**test_input) == [1,4]
test_input = { "nums": [9,14,24,31,32,40,47,54,75,76], "moveFrom": [31,75,76,3,47,32,24,9,14,18], "moveTo": [76,76,3,8,18,66,32,2,62,82] }
assert my_solution.relocateMarbles(**test_input) == [2,8,32,40,54,62,66,82]
test_input = { "nums": [1], "moveFrom": [1,1,1,1,1,1,1], "moveTo": [1,1,1,1,1,1,1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [2,12,13,14,18], "moveFrom": [13,2,14], "moveTo": [17,20,19] }
assert my_solution.relocateMarbles(**test_input) == [12,17,18,19,20]
test_input = { "nums": [12,21,24,28,41,60,62,70,76], "moveFrom": [21,76,41,3], "moveTo": [23,33,3,53] }
assert my_solution.relocateMarbles(**test_input) == [12,23,24,28,33,53,60,62,70]
test_input = { "nums": [7,29,40,43,48,56,60,72,81], "moveFrom": [56,60,6,62], "moveTo": [62,6,34,17] }
assert my_solution.relocateMarbles(**test_input) == [7,17,29,34,40,43,48,72,81]
test_input = { "nums": [2,4,5,8], "moveFrom": [2,4,3,7,5,8,14], "moveTo": [3,7,9,14,9,6,16] }
assert my_solution.relocateMarbles(**test_input) == [6,9,16]
test_input = { "nums": [9,15,18,24,39,48,59,64], "moveFrom": [59,64,63,60,9], "moveTo": [60,63,61,45,57] }
assert my_solution.relocateMarbles(**test_input) == [15,18,24,39,45,48,57,61]
test_input = { "nums": [2,8,8,9,11,21], "moveFrom": [8,11,30,21,14,27,9,22,2,7], "moveTo": [30,27,22,14,7,3,1,21,4,16] }
assert my_solution.relocateMarbles(**test_input) == [1,3,4,16,21]
test_input = { "nums": [5,11,30,43,47,63,65,82,86,93], "moveFrom": [43,63,11,93,82,47,54,5,30], "moveTo": [36,53,54,49,18,3,29,66,22] }
assert my_solution.relocateMarbles(**test_input) == [3,18,22,29,36,49,53,65,66,86]
test_input = { "nums": [9,14,14,16,26,51,53,64,76], "moveFrom": [64,45,9,14,26,53,51,67,80], "moveTo": [45,67,23,37,80,16,27,51,44] }
assert my_solution.relocateMarbles(**test_input) == [16,23,27,37,44,51,76]
test_input = { "nums": [16,21,21,25,39,41,44], "moveFrom": [41,21,45,29,35,39,29,25,28], "moveTo": [21,45,29,35,33,29,20,28,45] }
assert my_solution.relocateMarbles(**test_input) == [16,20,33,44,45]
test_input = { "nums": [1,1,3], "moveFrom": [3,1,9,5,5], "moveTo": [9,9,5,5,9] }
assert my_solution.relocateMarbles(**test_input) == [9]
test_input = { "nums": [12,14,16,16,21,32], "moveFrom": [32,5,14,21,15,22,16], "moveTo": [5,22,15,16,15,27,22] }
assert my_solution.relocateMarbles(**test_input) == [12,15,22,27]
test_input = { "nums": [1,5,9], "moveFrom": [1,5,9,3,8,5,9,1,5], "moveTo": [8,3,9,1,5,8,1,5,2] }
assert my_solution.relocateMarbles(**test_input) == [2,8]
test_input = { "nums": [1,2,14,30,43,44,76,76,77], "moveFrom": [76,77,43,1], "moveTo": [56,44,11,45] }
assert my_solution.relocateMarbles(**test_input) == [2,11,14,30,44,45,56]
test_input = { "nums": [4,25,27,33,33,35], "moveFrom": [25,27,27,34,7,36], "moveTo": [34,27,36,7,25,1] }
assert my_solution.relocateMarbles(**test_input) == [1,4,25,33,35]
test_input = { "nums": [3,7,18,25,37,48,48,62], "moveFrom": [48,18,62,48,2,18,56,53,37], "moveTo": [15,48,56,2,18,55,53,40,22] }
assert my_solution.relocateMarbles(**test_input) == [3,7,15,22,25,40,55]
test_input = { "nums": [19,35,46,55,59,59,68,72,93,100], "moveFrom": [46,100,35,19,68,87,21,93,27], "moveTo": [76,94,87,66,57,21,27,89,40] }
assert my_solution.relocateMarbles(**test_input) == [40,55,57,59,66,72,76,89,94]
test_input = { "nums": [1,3], "moveFrom": [3,1,3,4,4], "moveTo": [1,3,4,4,4] }
assert my_solution.relocateMarbles(**test_input) == [4]
test_input = { "nums": [22,30,36,40,44,48,50,59], "moveFrom": [30,44,64,59,4], "moveTo": [64,4,50,25,36] }
assert my_solution.relocateMarbles(**test_input) == [22,25,36,40,48,50]
test_input = { "nums": [1], "moveFrom": [1,1,1,1,1,1,1,1,1,1], "moveTo": [1,1,1,1,1,1,1,1,1,1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [8,18,23,37,37,39,48], "moveFrom": [8,39,23,7,37,36], "moveTo": [39,7,36,25,10,28] }
assert my_solution.relocateMarbles(**test_input) == [10,18,25,28,48]
test_input = { "nums": [1,4], "moveFrom": [1,2,4], "moveTo": [2,3,1] }
assert my_solution.relocateMarbles(**test_input) == [1,3]
test_input = { "nums": [3,7,9,13], "moveFrom": [3,16,9,7,6,15,16,7,13,7], "moveTo": [16,6,2,15,8,16,7,1,7,9] }
assert my_solution.relocateMarbles(**test_input) == [1,2,8,9]
test_input = { "nums": [11,12,17,18,20], "moveFrom": [11,17], "moveTo": [18,13] }
assert my_solution.relocateMarbles(**test_input) == [12,13,18,20]
test_input = { "nums": [5,11,17,21,25], "moveFrom": [17], "moveTo": [14] }
assert my_solution.relocateMarbles(**test_input) == [5,11,14,21,25]
test_input = { "nums": [7,9,12,20,23], "moveFrom": [7,8], "moveTo": [8,12] }
assert my_solution.relocateMarbles(**test_input) == [9,12,20,23]
test_input = { "nums": [1], "moveFrom": [1,1], "moveTo": [1,1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [10,13,17,49,56,57,59,62,68], "moveFrom": [49,62,59,45], "moveTo": [58,64,45,77] }
assert my_solution.relocateMarbles(**test_input) == [10,13,17,56,57,58,64,68,77]
test_input = { "nums": [1,8,15,23,24,29,47], "moveFrom": [8,23,24,47,10,1], "moveTo": [38,10,10,48,22,24] }
assert my_solution.relocateMarbles(**test_input) == [15,22,24,29,38,48]
test_input = { "nums": [3,3], "moveFrom": [3,1,4,2,2,2,2,2,1], "moveTo": [1,4,2,2,2,2,2,1,3] }
assert my_solution.relocateMarbles(**test_input) == [3]
test_input = { "nums": [4,8,8], "moveFrom": [8,7,7], "moveTo": [7,7,8] }
assert my_solution.relocateMarbles(**test_input) == [4,8]
test_input = { "nums": [1,2], "moveFrom": [1,2,1,3,4,2,3,4,1,4], "moveTo": [3,1,3,4,2,3,4,1,4,4] }
assert my_solution.relocateMarbles(**test_input) == [4]
test_input = { "nums": [12,13,16,31,48,52,56,72,79], "moveFrom": [13,79,12,72,14,48,56,52], "moveTo": [56,14,8,63,70,54,19,73] }
assert my_solution.relocateMarbles(**test_input) == [8,16,19,31,54,63,70,73]
test_input = { "nums": [3,7,7,14], "moveFrom": [3,16,7,15,12,5,14,16,13], "moveTo": [16,15,5,12,16,14,9,13,5] }
assert my_solution.relocateMarbles(**test_input) == [5,9]
test_input = { "nums": [9,13,14,15], "moveFrom": [15,14,13,5,8], "moveTo": [8,3,5,3,12] }
assert my_solution.relocateMarbles(**test_input) == [3,9,12]
test_input = { "nums": [11,16,28,33,37,45,45,58,79], "moveFrom": [16,57,81,79,11], "moveTo": [57,81,29,45,31] }
assert my_solution.relocateMarbles(**test_input) == [28,29,31,33,37,45,58]
test_input = { "nums": [1,6,7,7], "moveFrom": [6,8,8,12,15,8,9,15], "moveTo": [8,8,12,15,8,9,15,8] }
assert my_solution.relocateMarbles(**test_input) == [1,7,8]
test_input = { "nums": [7,20,23,25,33,39,51,74,76], "moveFrom": [76,20,74,7,15], "moveTo": [74,64,15,40,71] }
assert my_solution.relocateMarbles(**test_input) == [23,25,33,39,40,51,64,71]
test_input = { "nums": [4,6,6], "moveFrom": [6,4,7,6,4,7,8,1,2,4], "moveTo": [6,7,4,7,8,2,1,9,4,2] }
assert my_solution.relocateMarbles(**test_input) == [2,9]
test_input = { "nums": [8,14,17,19,21], "moveFrom": [19,8,14,21,17,1,14,18], "moveTo": [14,18,14,1,18,23,10,12] }
assert my_solution.relocateMarbles(**test_input) == [10,12,23]
test_input = { "nums": [13,18,39,44,45,49,72,81,95,100], "moveFrom": [49,81,18,39,44,22,100,66,45,5], "moveTo": [54,22,66,32,13,4,76,5,92,33] }
assert my_solution.relocateMarbles(**test_input) == [4,13,32,33,54,72,76,92,95]
test_input = { "nums": [1,3], "moveFrom": [3,1,3,4,3,3,3,3,2], "moveTo": [1,3,4,3,3,3,3,2,3] }
assert my_solution.relocateMarbles(**test_input) == [3]
test_input = { "nums": [2,4,5,13], "moveFrom": [13,5,2], "moveTo": [1,9,10] }
assert my_solution.relocateMarbles(**test_input) == [1,4,9,10]
test_input = { "nums": [1], "moveFrom": [1], "moveTo": [1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [3,4,13,14,19], "moveFrom": [13,3,19,3,3,14,13,4,15,11], "moveTo": [15,3,8,3,13,1,10,22,11,2] }
assert my_solution.relocateMarbles(**test_input) == [1,2,8,10,22]
test_input = { "nums": [3,6,9,11], "moveFrom": [9,3,8], "moveTo": [7,8,9] }
assert my_solution.relocateMarbles(**test_input) == [6,7,9,11]
test_input = { "nums": [9,15,16,18,20,26], "moveFrom": [20,18,13], "moveTo": [31,13,32] }
assert my_solution.relocateMarbles(**test_input) == [9,15,16,26,31,32]
test_input = { "nums": [6,11,21,25,25], "moveFrom": [11,21,2,25,17,1], "moveTo": [17,2,19,8,1,9] }
assert my_solution.relocateMarbles(**test_input) == [6,8,9,19]
test_input = { "nums": [2,6,8], "moveFrom": [2,6,8,2,9,2,7], "moveTo": [8,2,2,9,2,7,8] }
assert my_solution.relocateMarbles(**test_input) == [8]
test_input = { "nums": [1,7,7], "moveFrom": [1,7,1,6,2,9,5,6], "moveTo": [2,1,6,9,5,6,5,6] }
assert my_solution.relocateMarbles(**test_input) == [5,6]
test_input = { "nums": [4,6,7,12,12,25,37], "moveFrom": [37,41,37,6,4,7,25], "moveTo": [41,37,37,26,7,45,45] }
assert my_solution.relocateMarbles(**test_input) == [12,26,37,45]
test_input = { "nums": [3,4], "moveFrom": [4,3], "moveTo": [2,1] }
assert my_solution.relocateMarbles(**test_input) == [1,2]
test_input = { "nums": [1,2], "moveFrom": [1,2,3,3,2,1,2], "moveTo": [2,3,3,2,1,2,2] }
assert my_solution.relocateMarbles(**test_input) == [2]
test_input = { "nums": [20,37,44,53,55,59,60,62], "moveFrom": [20,44,53,60,11,55,37,59], "moveTo": [53,28,62,11,16,63,9,6] }
assert my_solution.relocateMarbles(**test_input) == [6,9,16,28,62,63]
test_input = { "nums": [2,4], "moveFrom": [4,2,2,1,4,2,2,3], "moveTo": [2,2,1,4,2,2,3,4] }
assert my_solution.relocateMarbles(**test_input) == [4]
test_input = { "nums": [2,6,7], "moveFrom": [6,7,6,3,6,5,2], "moveTo": [6,6,3,6,5,4,8] }
assert my_solution.relocateMarbles(**test_input) == [4,8]
test_input = { "nums": [3,7,13,13], "moveFrom": [13,7,4,6], "moveTo": [9,4,6,15] }
assert my_solution.relocateMarbles(**test_input) == [3,9,15]
test_input = { "nums": [2,3], "moveFrom": [2,3], "moveTo": [1,1] }
assert my_solution.relocateMarbles(**test_input) == [1]
test_input = { "nums": [12,13,25,27,34,34,38], "moveFrom": [12,13,11,33,2,25,34,27,38], "moveTo": [11,2,33,26,25,30,24,38,47] }
assert my_solution.relocateMarbles(**test_input) == [24,26,30,47]
test_input = { "nums": [2,13,20,29,34,48,48], "moveFrom": [2,13], "moveTo": [5,42] }
assert my_solution.relocateMarbles(**test_input) == [5,20,29,34,42,48]
test_input = { "nums": [19,30,31,41,47,54,57,62], "moveFrom": [41,31,62,30,54], "moveTo": [38,49,10,60,31] }
assert my_solution.relocateMarbles(**test_input) == [10,19,31,38,47,49,57,60] | 1,688,826,600 |
biweekly-contest-108-partition-string-into-minimum-beautiful-substrings | https://leetcode.com/problems/partition-string-into-minimum-beautiful-substrings | partition-string-into-minimum-beautiful-substrings | {
"questionId": "2883",
"questionFrontendId": "2767",
"title": "Partition String Into Minimum Beautiful Substrings",
"titleSlug": "partition-string-into-minimum-beautiful-substrings",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 289,
"dislikes": 8,
"categoryTitle": "Algorithms"
} | """
给你一个二进制字符串 s ,你需要将字符串分割成一个或者多个 子字符串 ,使每个子字符串都是 美丽 的。
如果一个字符串满足以下条件,我们称它是 美丽 的:
* 它不包含前导 0 。
* 它是 5 的幂的 二进制 表示。
请你返回分割后的子字符串的 最少 数目。如果无法将字符串 s 分割成美丽子字符串,请你返回 -1 。
子字符串是一个字符串中一段连续的字符序列。
示例 1:
输入:s = "1011"
输出:2
解释:我们可以将输入字符串分成 ["101", "1"] 。
- 字符串 "101" 不包含前导 0 ,且它是整数 51 = 5 的二进制表示。
- 字符串 "1" 不包含前导 0 ,且它是整数 50 = 1 的二进制表示。
最少可以将 s 分成 2 个美丽子字符串。
示例 2:
输入:s = "111"
输出:3
解释:我们可以将输入字符串分成 ["1", "1", "1"] 。
- 字符串 "1" 不包含前导 0 ,且它是整数 50 = 1 的二进制表示。
最少可以将 s 分成 3 个美丽子字符串。
示例 3:
输入:s = "0"
输出:-1
解释:无法将给定字符串分成任何美丽子字符串。
提示:
* 1 <= s.length <= 15
* s[i] 要么是 '0' 要么是 '1' 。
"""
class Solution:
def minimumBeautifulSubstrings(self, s: str) -> int:
| 给你一个二进制字符串 s ,你需要将字符串分割成一个或者多个 子字符串 ,使每个子字符串都是 美丽 的。
如果一个字符串满足以下条件,我们称它是 美丽 的:
* 它不包含前导 0 。
* 它是 5 的幂的 二进制 表示。
请你返回分割后的子字符串的 最少 数目。如果无法将字符串 s 分割成美丽子字符串,请你返回 -1 。
子字符串是一个字符串中一段连续的字符序列。
示例 1:
输入:s = "1011"
输出:2
解释:我们可以将输入字符串分成 ["101", "1"] 。
- 字符串 "101" 不包含前导 0 ,且它是整数 51 = 5 的二进制表示。
- 字符串 "1" 不包含前导 0 ,且它是整数 50 = 1 的二进制表示。
最少可以将 s 分成 2 个美丽子字符串。
示例 2:
输入:s = "111"
输出:3
解释:我们可以将输入字符串分成 ["1", "1", "1"] 。
- 字符串 "1" 不包含前导 0 ,且它是整数 50 = 1 的二进制表示。
最少可以将 s 分成 3 个美丽子字符串。
示例 3:
输入:s = "0"
输出:-1
解释:无法将给定字符串分成任何美丽子字符串。
提示:
* 1 <= s.length <= 15
* s[i] 要么是 '0' 要么是 '1' 。
请完成下面的代码来解决上述问题:
```python
class Solution:
def minimumBeautifulSubstrings(self, s: str) -> int:
``` |
my_solution = Solution()
test_input = { "s": "1011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 2
test_input = { "s": "111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 3
test_input = { "s": "0" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == -1
test_input = { "s": "100111000110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "100111000111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 3
test_input = { "s": "1001110001111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "100111000111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "10110011100011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 3
test_input = { "s": "101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 2
test_input = { "s": "101101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 3
test_input = { "s": "101101101101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "1011011011101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10110110111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101101101111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "1011011011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101101110011101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10110111001111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "1011011101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101101110110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101101110111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "1011011101111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101101110111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10110111011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "101101111001101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10110111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10110111101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101101111011011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101101111011101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10110111101111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "101101111011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 9
test_input = { "s": "1011011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "1011011111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101101111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "1011011111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10110111110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "10110111111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101101111110011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10110111111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "101101111110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101101111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "101101111111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101110011011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "10111001101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "101110011011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "1011100111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "10111001110001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 3
test_input = { "s": "10111001110011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "1011100111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111001110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "10111001111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111001111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "1011100111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101110011110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101110011111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111001111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101110011111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "1011100111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10111001111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "1011101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "10111011011011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101110110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101110110111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "1011101101111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101110111001101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101110111001111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10111011101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111011101111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "101110111011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 9
test_input = { "s": "1011101111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "1011101111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111011110011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101110111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "1011101111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10111011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10111011111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "10111011111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "101110111110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 9
test_input = { "s": "101110111111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101110111111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101110111111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111011111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 10
test_input = { "s": "101110111111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 11
test_input = { "s": "101111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "101111001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 3
test_input = { "s": "1011110011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "10111100110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "101111001110001" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 4
test_input = { "s": "101111001111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "10111100111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "101111011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "101111011011" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "10111101101101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 6
test_input = { "s": "1011110110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "1011110111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 7
test_input = { "s": "101111011110111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 9
test_input = { "s": "101111011111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 8
test_input = { "s": "1011110111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 9
test_input = { "s": "101111011111101" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 5
test_input = { "s": "10111101111111" }
assert my_solution.minimumBeautifulSubstrings(**test_input) == 10 | 1,688,826,600 |
biweekly-contest-108-number-of-black-blocks | https://leetcode.com/problems/number-of-black-blocks | number-of-black-blocks | {
"questionId": "2889",
"questionFrontendId": "2768",
"title": "Number of Black Blocks",
"titleSlug": "number-of-black-blocks",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 204,
"dislikes": 21,
"categoryTitle": "Algorithms"
} | """
给你两个整数 m 和 n ,表示一个下标从 0 开始的 m x n 的网格图。
给你一个下标从 0 开始的二维整数矩阵 coordinates ,其中 coordinates[i] = [x, y] 表示坐标为 [x, y] 的格子是 黑色的 ,所有没出现在 coordinates 中的格子都是 白色的。
一个块定义为网格图中 2 x 2 的一个子矩阵。更正式的,对于左上角格子为 [x, y] 的块,其中 0 <= x < m - 1 且 0 <= y < n - 1 ,包含坐标为 [x, y] ,[x + 1, y] ,[x, y + 1] 和 [x + 1, y + 1] 的格子。
请你返回一个下标从 0 开始长度为 5 的整数数组 arr ,arr[i] 表示恰好包含 i 个 黑色 格子的块的数目。
示例 1:
输入:m = 3, n = 3, coordinates = [[0,0]]
输出:[3,1,0,0,0]
解释:网格图如下:
[https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png]
只有 1 个块有一个黑色格子,这个块是左上角为 [0,0] 的块。
其他 3 个左上角分别为 [0,1] ,[1,0] 和 [1,1] 的块都有 0 个黑格子。
所以我们返回 [3,1,0,0,0] 。
示例 2:
输入:m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]]
输出:[0,2,2,0,0]
解释:网格图如下:
[https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png]
有 2 个块有 2 个黑色格子(左上角格子分别为 [0,0] 和 [0,1])。
左上角为 [1,0] 和 [1,1] 的两个块,都有 1 个黑格子。
所以我们返回 [0,2,2,0,0] 。
提示:
* 2 <= m <= 105
* 2 <= n <= 105
* 0 <= coordinates.length <= 104
* coordinates[i].length == 2
* 0 <= coordinates[i][0] < m
* 0 <= coordinates[i][1] < n
* coordinates 中的坐标对两两互不相同。
"""
class Solution:
def countBlackBlocks(self, m: int, n: int, coordinates: List[List[int]]) -> List[int]:
| 给你两个整数 m 和 n ,表示一个下标从 0 开始的 m x n 的网格图。
给你一个下标从 0 开始的二维整数矩阵 coordinates ,其中 coordinates[i] = [x, y] 表示坐标为 [x, y] 的格子是 黑色的 ,所有没出现在 coordinates 中的格子都是 白色的。
一个块定义为网格图中 2 x 2 的一个子矩阵。更正式的,对于左上角格子为 [x, y] 的块,其中 0 <= x < m - 1 且 0 <= y < n - 1 ,包含坐标为 [x, y] ,[x + 1, y] ,[x, y + 1] 和 [x + 1, y + 1] 的格子。
请你返回一个下标从 0 开始长度为 5 的整数数组 arr ,arr[i] 表示恰好包含 i 个 黑色 格子的块的数目。
示例 1:
输入:m = 3, n = 3, coordinates = [[0,0]]
输出:[3,1,0,0,0]
解释:网格图如下:
[https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png]
只有 1 个块有一个黑色格子,这个块是左上角为 [0,0] 的块。
其他 3 个左上角分别为 [0,1] ,[1,0] 和 [1,1] 的块都有 0 个黑格子。
所以我们返回 [3,1,0,0,0] 。
示例 2:
输入:m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]]
输出:[0,2,2,0,0]
解释:网格图如下:
[https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png]
有 2 个块有 2 个黑色格子(左上角格子分别为 [0,0] 和 [0,1])。
左上角为 [1,0] 和 [1,1] 的两个块,都有 1 个黑格子。
所以我们返回 [0,2,2,0,0] 。
提示:
* 2 <= m <= 105
* 2 <= n <= 105
* 0 <= coordinates.length <= 104
* coordinates[i].length == 2
* 0 <= coordinates[i][0] < m
* 0 <= coordinates[i][1] < n
* coordinates 中的坐标对两两互不相同。
请完成下面的代码来解决上述问题:
```python
class Solution:
def countBlackBlocks(self, m: int, n: int, coordinates: List[List[int]]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "m": 3, "n": 3, "coordinates": [[0,0]] }
assert my_solution.countBlackBlocks(**test_input) == [3,1,0,0,0]
test_input = { "m": 3, "n": 3, "coordinates": [[0,0],[1,1],[0,2]] }
assert my_solution.countBlackBlocks(**test_input) == [0,2,2,0,0]
test_input = { "m": 32, "n": 32, "coordinates": [[17,29],[29,16],[19,20],[18,9],[16,7],[20,25],[22,19],[4,9],[14,17],[6,23],[2,2],[20,1],[8,7],[4,7],[14,14],[10,10],[1,27],[18,23],[6,30],[8,18],[26,23],[25,8],[5,6],[3,4]] }
assert my_solution.countBlackBlocks(**test_input) == [866,94,1,0,0]
test_input = { "m": 22, "n": 73, "coordinates": [[11,14],[16,11],[20,5],[5,33],[14,7],[16,60],[0,15],[15,72],[6,60],[9,16],[14,51],[1,52],[18,24],[17,30],[3,4],[19,13],[9,10],[11,40],[15,7],[13,62],[8,41],[12,71],[4,72],[18,7],[1,0],[4,35],[16,33],[7,30],[13,52],[5,1],[15,21],[3,59],[2,41],[4,28]] }
assert my_solution.countBlackBlocks(**test_input) == [1387,122,3,0,0]
test_input = { "m": 22, "n": 79, "coordinates": [[11,43],[17,56],[11,58],[19,68],[5,10],[18,35],[18,27],[10,53],[1,72],[4,64],[4,72],[3,76],[18,36],[14,47],[12,57],[10,11],[13,5],[17,39],[1,4],[3,32],[6,34],[2,62],[8,35],[9,18],[12,77],[8,43],[1,49],[15,14],[15,27],[4,68],[19,24],[9,19],[17,3],[3,51],[5,61]] }
assert my_solution.countBlackBlocks(**test_input) == [1503,130,5,0,0]
test_input = { "m": 3, "n": 89, "coordinates": [[0,46],[1,29],[0,50],[1,61]] }
assert my_solution.countBlackBlocks(**test_input) == [164,12,0,0,0]
test_input = { "m": 25, "n": 97, "coordinates": [[23,65],[11,33],[6,76],[9,65],[18,13],[16,59],[0,86],[17,14],[23,61],[8,78],[19,40],[18,57],[1,66],[15,1],[13,44],[17,6],[14,90],[1,22],[5,34],[0,53],[1,65],[20,32],[22,54],[3,11],[20,53],[7,70],[0,30],[23,17],[18,2],[14,93],[1,28],[19,82],[6,61],[13,64],[6,73],[0,52],[17,93],[20,63],[16,24],[14,6],[20,28],[4,57],[19,76],[21,50],[5,44],[5,29],[11,36],[14,87],[2,10],[4,78],[12,35],[11,27],[20,7],[11,20],[9,51],[22,37],[11,30],[2,77],[5,87],[13,5],[2,56],[23,25],[0,44],[19,23],[23,1],[3,70],[3,86],[17,92],[19,24],[23,39],[16,2],[15,45],[14,82],[21,30],[11,90],[7,82],[17,50],[12,66],[22,84],[15,71],[14,54]] }
assert my_solution.countBlackBlocks(**test_input) == [2002,290,12,0,0]
test_input = { "m": 58, "n": 74, "coordinates": [[38,21],[1,34],[29,15],[46,33],[56,7],[55,63],[7,32],[53,38],[30,73],[17,68],[32,26],[13,39],[10,22],[42,38],[29,9],[7,53],[17,2],[56,13],[34,26],[31,30],[16,21],[13,56],[16,72],[21,8],[47,28],[2,24],[32,23],[13,4],[37,29],[1,11],[45,70],[49,40],[11,61],[20,49],[11,4],[17,61],[11,68],[14,64],[31,10],[1,14],[10,47],[21,48],[46,51],[25,59],[45,12],[47,43],[32,39],[11,28],[55,46],[50,7]] }
assert my_solution.countBlackBlocks(**test_input) == [3964,196,1,0,0]
test_input = { "m": 40, "n": 82, "coordinates": [[11,63],[20,43],[16,53],[33,52],[7,30],[36,2],[25,65],[10,31],[4,29],[31,17],[14,52],[36,32],[35,68],[26,8],[20,68],[21,45],[7,46],[32,15],[33,13],[30,4],[32,9],[32,78],[32,66],[27,58],[20,14],[12,29],[8,80],[2,1],[1,0],[34,41],[1,3],[1,74],[0,69],[21,7],[36,16],[0,21],[4,80],[33,78],[18,11],[18,50],[17,63],[24,29]] }
assert my_solution.countBlackBlocks(**test_input) == [3000,156,3,0,0]
test_input = { "m": 57, "n": 92, "coordinates": [[38,14],[27,72]] }
assert my_solution.countBlackBlocks(**test_input) == [5088,8,0,0,0]
test_input = { "m": 94, "n": 98, "coordinates": [[39,62],[40,20],[47,78],[75,10],[52,82],[11,77],[52,21],[22,12],[75,42],[75,68],[42,39],[68,75],[1,29],[18,79],[56,82],[72,41],[52,28],[61,83],[44,55],[73,81],[43,71],[55,23],[4,13],[89,68],[36,57],[48,22],[64,49],[10,72],[84,80],[1,77],[50,7],[54,0],[76,9],[57,6],[7,81],[66,80]] }
assert my_solution.countBlackBlocks(**test_input) == [8880,140,1,0,0]
test_input = { "m": 30, "n": 97, "coordinates": [] }
assert my_solution.countBlackBlocks(**test_input) == [2784,0,0,0,0]
test_input = { "m": 11, "n": 28, "coordinates": [[7,24],[8,22],[1,5],[6,21],[0,22],[0,15],[4,16],[2,11],[0,11],[1,3],[3,26],[1,20],[5,27],[9,8],[7,25],[0,19],[3,21],[2,13],[6,7],[5,13],[0,1],[4,18],[4,1],[2,24],[7,17],[3,15],[9,18],[1,25],[3,18],[4,27],[4,20],[4,7],[9,12],[7,1],[3,12],[5,26],[5,23],[2,4],[3,5],[6,18],[4,10],[8,8],[2,7],[7,15],[5,0],[4,9],[8,1],[8,0],[1,17],[3,9],[2,10],[1,13],[0,10],[8,14],[1,21],[1,10],[7,8],[4,6],[7,23],[0,20],[8,13],[3,24],[1,15],[5,24],[4,11],[9,14],[4,12],[7,2],[2,25],[2,9],[6,14],[0,25],[7,4],[4,2],[6,1],[4,8],[6,17]] }
assert my_solution.countBlackBlocks(**test_input) == [83,107,63,17,0]
test_input = { "m": 6, "n": 47, "coordinates": [[1,1],[1,23],[1,5],[0,22],[1,12],[0,24],[4,24],[2,8],[1,11],[0,31],[1,39],[0,43],[2,18],[1,17],[1,3],[3,10],[0,26],[2,38],[1,0],[3,37],[3,2],[1,26],[4,34],[3,24],[0,23]] }
assert my_solution.countBlackBlocks(**test_input) == [159,58,11,2,0]
test_input = { "m": 45, "n": 88, "coordinates": [[13,14],[6,48],[4,20],[18,57],[2,48],[0,70],[42,71],[20,70],[29,11],[34,24],[24,28],[38,45],[31,73],[41,85],[18,69]] }
assert my_solution.countBlackBlocks(**test_input) == [3770,58,0,0,0]
test_input = { "m": 7, "n": 36, "coordinates": [[2,21],[3,14],[4,18],[5,30],[4,26],[5,7],[2,31],[4,22],[0,0],[2,22],[4,6],[0,30],[0,19],[0,21],[3,11],[4,28],[5,25],[2,11],[0,23],[5,16],[3,29],[3,17],[4,11],[1,7],[1,19],[1,34],[5,19],[5,6],[5,28],[4,8],[2,9],[1,6],[4,24],[2,14],[1,24],[3,16],[0,2],[0,26],[5,27],[3,35],[2,16],[4,2],[4,32],[5,0],[0,34],[1,5],[2,4],[1,4],[0,22],[2,2],[3,19],[3,4],[1,10],[3,5],[1,29],[0,33],[0,3]] }
assert my_solution.countBlackBlocks(**test_input) == [63,99,42,6,0]
test_input = { "m": 59, "n": 60, "coordinates": [[4,37],[56,0],[32,53],[27,8],[18,42],[5,25],[11,46],[51,55],[55,16],[7,17],[47,1],[47,38],[28,7],[17,39],[29,59],[47,39],[56,42],[2,31],[41,16],[44,32],[27,1],[14,8],[38,52],[38,48],[40,12],[25,32],[15,55],[12,22],[0,38],[38,58],[54,52],[19,28],[39,45],[10,43],[44,26],[18,14],[34,30],[6,23],[39,22],[15,29],[50,31],[24,11],[12,5],[42,45],[18,38],[27,56],[33,58],[54,24],[40,24],[24,2],[28,44],[33,53],[49,57],[47,56],[50,18],[25,14],[56,54],[40,55],[53,44],[27,42],[39,23],[44,38],[25,2],[47,14],[27,50],[26,46],[15,1],[16,15],[55,56],[27,21],[6,7]] }
assert my_solution.countBlackBlocks(**test_input) == [3155,256,11,0,0]
test_input = { "m": 89, "n": 90, "coordinates": [[12,15],[31,16],[75,79],[86,48],[19,77],[39,82],[77,62],[75,35],[19,35]] }
assert my_solution.countBlackBlocks(**test_input) == [7796,36,0,0,0]
test_input = { "m": 26, "n": 42, "coordinates": [[7,13],[1,3],[11,32],[5,8],[11,4],[9,24],[0,0],[18,20],[13,35],[19,31],[15,35],[1,41],[18,40],[18,41],[14,11],[20,0],[11,6],[14,16],[0,11],[4,36],[9,12],[20,36],[14,33],[6,34],[0,12],[22,6],[22,34],[13,6],[12,1],[4,23],[6,18],[11,38],[19,17],[22,27],[21,6],[3,35],[9,11],[23,6],[22,29],[8,5],[6,26],[3,18],[0,2],[3,41],[24,6],[24,1],[7,9],[8,35],[0,6],[6,23],[13,40],[10,38],[10,20],[21,7],[20,11],[10,11],[19,3],[14,9],[1,5],[3,30],[9,9],[17,34],[18,16],[3,26],[18,19],[17,16],[2,17],[5,22],[5,41],[16,32],[9,7],[1,36],[6,30],[6,38],[9,20],[4,28],[12,8],[7,26],[5,30],[2,27],[3,32],[11,7],[7,41],[8,4],[10,34],[19,19],[13,32],[23,25]] }
assert my_solution.countBlackBlocks(**test_input) == [739,246,37,3,0]
test_input = { "m": 31, "n": 46, "coordinates": [[5,31],[13,5],[6,32],[3,24],[5,41],[22,22],[18,38],[29,11],[25,28],[10,43],[29,5],[10,28],[21,2],[27,1],[4,42],[18,2],[22,26],[22,4],[2,26],[21,4],[5,32],[0,43],[24,32],[2,30],[18,44],[18,43],[7,36],[15,13],[11,29],[6,17],[10,7],[15,8],[4,11],[9,7],[28,1],[0,15],[21,19],[6,0],[24,19],[1,0],[15,30],[1,18],[4,24],[1,9],[24,16],[22,28],[4,35],[14,15],[27,23],[13,1],[2,8],[4,20],[19,30],[15,11],[0,34],[27,21],[10,1],[10,33],[11,26],[13,40],[11,45],[25,4],[28,28],[4,7],[7,29],[3,45],[20,17],[25,38],[27,41],[23,35],[19,10],[2,7],[0,25],[4,18],[23,37],[6,38]] }
assert my_solution.countBlackBlocks(**test_input) == [1081,251,17,1,0]
test_input = { "m": 11, "n": 21, "coordinates": [[0,19],[2,8],[6,9],[5,15],[6,15],[2,5],[3,14],[2,10],[8,13],[4,2],[0,0],[7,19],[9,10],[8,19]] }
assert my_solution.countBlackBlocks(**test_input) == [153,43,4,0,0]
test_input = { "m": 76, "n": 95, "coordinates": [[16,61],[1,23],[51,8],[36,87],[60,92],[23,44],[28,93],[15,35],[8,68],[58,57],[58,80],[53,7],[47,25],[55,4],[6,61],[43,56],[6,40],[39,44],[67,76],[44,16],[40,93],[7,6],[59,44],[74,6],[1,72],[38,93],[58,51],[17,79],[73,79],[58,65],[45,28],[17,36],[69,94],[66,73],[56,1],[31,62],[62,68],[54,64],[72,46],[67,59],[9,47],[33,86],[45,49],[3,6],[63,59],[51,47],[31,74],[52,42],[34,80],[61,30],[62,4],[11,9],[44,21],[73,69],[7,77],[43,17],[53,22],[37,10],[49,5],[64,82],[51,77],[74,2],[60,2],[17,53],[16,51],[52,80],[22,23],[17,80],[52,26],[15,1],[19,59],[7,39]] }
assert my_solution.countBlackBlocks(**test_input) == [6768,278,4,0,0]
test_input = { "m": 65, "n": 71, "coordinates": [[62,8],[18,63],[23,43],[30,3],[40,48],[60,62],[58,9],[13,20],[47,46],[34,0],[11,6],[17,28],[20,34],[24,48],[24,18],[43,18],[31,59],[25,60],[15,68],[24,35],[33,47],[32,4],[26,42],[35,63],[31,22],[16,0],[45,45],[52,19],[46,49],[36,37],[11,10],[23,5],[4,13],[17,20],[14,41],[26,4],[21,7],[52,40],[31,18],[55,26],[17,57],[41,31],[1,47],[56,61],[46,38],[7,16],[53,13],[45,4],[1,69],[29,15],[46,12],[29,65],[61,10],[35,54],[33,26],[41,34],[55,48],[42,48],[0,8],[44,44],[52,10],[30,37],[14,54],[4,29],[50,58],[41,10],[30,19],[31,1],[31,62],[31,44],[53,7],[12,56],[57,69],[34,2],[34,16],[42,68],[45,15],[44,61],[15,60],[54,69],[1,39],[50,59],[51,47],[52,15],[33,69],[5,51],[19,38],[10,61],[42,17],[60,30]] }
assert my_solution.countBlackBlocks(**test_input) == [4131,344,5,0,0]
test_input = { "m": 25, "n": 95, "coordinates": [[8,74],[20,10],[18,56],[23,20],[7,16],[7,5],[23,52],[19,31],[11,93],[0,68],[4,3],[21,52],[17,61],[7,65],[11,20],[1,61],[20,63],[9,71],[11,63],[11,61],[0,74],[17,60],[16,12],[3,54],[23,66],[23,37],[14,0],[19,64],[18,82],[4,87],[14,82],[23,3],[11,86],[3,64],[9,32],[14,8],[1,24],[21,20],[21,38],[22,27],[21,82],[10,58],[19,5],[22,57],[18,44],[10,83]] }
assert my_solution.countBlackBlocks(**test_input) == [2081,172,3,0,0]
test_input = { "m": 40, "n": 84, "coordinates": [[19,36],[35,7],[3,78],[17,4],[28,8],[20,38],[12,38],[30,6],[37,45],[26,53],[11,46]] }
assert my_solution.countBlackBlocks(**test_input) == [3193,44,0,0,0]
test_input = { "m": 8, "n": 57, "coordinates": [[3,38],[1,41],[6,23],[0,54],[3,11],[3,44],[1,24],[0,38],[5,28]] }
assert my_solution.countBlackBlocks(**test_input) == [360,32,0,0,0]
test_input = { "m": 32, "n": 41, "coordinates": [[12,13],[20,33],[3,12],[28,40],[9,10],[18,10],[5,18],[9,24],[29,24],[9,33],[25,38],[29,17],[28,25],[0,35],[2,30],[18,37],[7,0],[14,21],[25,27],[17,33],[9,38],[19,22],[17,5],[18,4],[9,18],[15,6],[4,21],[12,4],[5,35],[19,26],[6,6],[0,2],[0,28],[1,13],[19,10],[5,16],[2,31],[24,2],[3,22],[23,12],[0,17],[6,30],[20,31],[10,32],[17,15],[1,34],[28,6],[21,15],[14,11],[27,23],[4,16],[2,11],[21,26],[23,19],[27,15],[3,5],[28,10],[1,2],[27,18],[19,36],[2,2],[17,13],[7,19],[25,0],[5,37],[30,6],[3,39],[28,30],[26,0],[9,5],[23,5],[27,6],[4,26],[15,39],[10,26]] }
assert my_solution.countBlackBlocks(**test_input) == [976,244,20,0,0]
test_input = { "m": 63, "n": 99, "coordinates": [[17,28],[7,39],[14,81],[44,4],[21,7],[21,95],[1,89],[57,74],[34,2],[55,56],[43,50],[52,28],[38,61],[35,62],[57,46],[30,72],[25,46],[4,32],[18,25],[53,98]] }
assert my_solution.countBlackBlocks(**test_input) == [5998,78,0,0,0]
test_input = { "m": 44, "n": 97, "coordinates": [[17,3],[29,4],[40,71],[32,45],[3,59],[22,34],[11,17],[17,43],[4,32],[8,8],[19,43],[12,19],[32,57],[9,15],[7,46],[4,39],[11,26],[17,87],[21,70],[12,58],[35,88],[12,72],[23,61],[23,43],[3,86],[31,30],[17,26],[14,22],[16,77],[41,73],[39,91],[41,74],[15,78],[31,36],[11,45],[9,57],[25,68],[42,28],[5,60],[38,72],[26,14],[33,48],[39,50],[38,33],[41,21],[39,86],[29,64],[4,69],[37,25],[28,85],[9,32],[9,76],[13,25],[26,43],[10,79],[2,2],[23,85],[39,29],[34,47],[13,17],[34,59],[27,84],[29,93],[17,89],[23,27],[9,16],[39,64],[3,37],[41,75],[32,26],[27,11]] }
assert my_solution.countBlackBlocks(**test_input) == [3853,266,9,0,0]
test_input = { "m": 45, "n": 53, "coordinates": [[15,41],[0,27],[24,15],[34,31],[36,27],[32,46],[1,39],[4,8],[18,8],[39,3],[2,50],[6,33],[25,46],[17,41],[27,46],[37,30],[41,23],[16,14],[21,17],[26,47],[31,47],[9,23],[32,21],[29,28],[37,41],[10,37],[21,31],[1,25],[31,19],[16,49],[2,22],[1,43],[39,33],[6,12],[12,39],[40,15],[31,50],[8,7],[5,21],[8,4],[30,10],[15,20],[9,21],[38,28],[42,14],[36,8],[27,5],[2,2],[13,32],[13,50],[22,8],[23,25],[33,23],[9,22],[28,12],[15,37],[40,10],[42,45],[1,11],[26,2],[30,18],[0,19],[15,38],[32,2],[26,19],[29,29],[24,21],[24,10],[19,8],[24,31],[37,34],[5,20],[11,30],[41,19],[34,43],[41,7],[38,36],[13,10],[39,14],[22,4],[34,27],[23,21],[9,24],[13,29],[14,30],[32,48],[9,47],[13,37],[3,4],[1,6],[19,48],[41,47],[40,33],[26,23],[1,4],[40,28],[31,37]] }
assert my_solution.countBlackBlocks(**test_input) == [1929,334,25,0,0]
test_input = { "m": 36, "n": 62, "coordinates": [[5,42],[14,2],[32,11],[28,38],[18,49],[23,52],[32,52],[15,39],[11,38],[15,54],[21,27],[14,0],[26,38],[4,43],[22,26],[5,17]] }
assert my_solution.countBlackBlocks(**test_input) == [2075,58,2,0,0]
test_input = { "m": 30, "n": 98, "coordinates": [[10,13],[23,8],[9,69],[20,70],[17,12],[12,39],[7,72],[14,0],[5,45],[21,24],[10,88],[2,8],[22,86],[2,28],[20,62],[6,82],[27,10],[7,28],[12,79],[21,38],[24,92],[0,47],[8,8],[26,3],[20,57],[12,36],[21,47],[19,12],[20,35],[28,26],[4,61],[5,90],[0,48],[14,50],[3,63],[17,93],[12,5],[12,94],[7,25]] }
assert my_solution.countBlackBlocks(**test_input) == [2664,148,1,0,0]
test_input = { "m": 20, "n": 41, "coordinates": [[11,3],[1,36],[17,23],[13,1],[14,33],[2,23],[0,5],[5,32],[14,36],[8,16],[0,9],[14,26],[8,9],[6,5],[10,12],[17,20],[10,33],[16,23],[12,40],[8,17],[12,35],[11,23],[6,34],[2,4],[7,0],[7,7],[8,27],[7,39],[13,19],[14,2],[9,23],[12,33],[14,20],[12,27],[15,22],[1,19],[10,17],[7,36],[4,29],[12,37],[7,18],[10,20]] }
assert my_solution.countBlackBlocks(**test_input) == [608,144,8,0,0]
test_input = { "m": 60, "n": 73, "coordinates": [[45,35],[12,1],[43,11],[58,9],[0,3],[51,34],[1,65],[15,55],[32,29],[41,36],[41,61],[5,39],[54,2],[21,35],[41,69],[12,71],[17,5],[56,59],[41,40],[49,17],[48,56],[18,71],[39,57],[1,46],[53,44],[40,46],[52,14],[57,68],[14,66],[20,26],[48,8],[46,22],[34,41],[15,47],[18,45],[16,29],[40,6],[51,51],[47,70],[29,64],[51,32],[18,40],[10,62],[5,28],[57,3],[43,69],[49,58],[8,7],[18,9],[24,0],[56,0],[23,39],[31,19],[56,12],[34,17],[13,42],[8,23],[28,20],[42,38],[5,16],[29,36],[56,47],[45,23],[51,58],[2,69],[2,44],[5,6],[53,6],[50,48],[51,64],[43,15],[37,70],[18,44],[41,23],[51,31],[31,10],[25,53],[28,46],[56,42],[7,49],[55,50],[31,26],[3,26],[43,52],[54,68],[21,37]] }
assert my_solution.countBlackBlocks(**test_input) == [3915,328,5,0,0]
test_input = { "m": 37, "n": 80, "coordinates": [[5,3],[29,79],[35,32],[29,8],[10,57]] }
assert my_solution.countBlackBlocks(**test_input) == [2826,18,0,0,0]
test_input = { "m": 31, "n": 97, "coordinates": [[29,40],[29,6],[11,73],[10,81],[12,92],[23,62],[16,86],[26,0],[9,95],[17,17],[22,60],[27,15],[15,70],[7,18],[1,27],[18,51],[14,38],[2,42],[26,43],[0,52],[1,69],[23,50],[26,68],[24,53],[23,31],[7,78],[18,23],[14,96],[25,49],[23,74],[11,35],[5,14],[24,35]] }
assert my_solution.countBlackBlocks(**test_input) == [2754,126,0,0,0]
test_input = { "m": 17, "n": 43, "coordinates": [[8,38],[2,23],[15,18],[1,3],[7,40],[3,30],[13,1],[12,40],[9,4],[0,10],[5,36],[3,15],[3,5],[7,23],[5,13],[11,26],[3,28],[14,23],[10,16],[2,11],[14,5],[11,32],[1,20],[4,0],[15,13],[3,12],[12,9],[3,31],[8,35],[8,17],[3,42],[1,36],[5,2],[13,14],[9,2],[6,28],[5,4],[2,1],[13,36],[2,24],[12,29],[15,3],[11,20],[2,25],[13,8],[4,17],[8,29],[0,33],[11,3],[0,29],[12,30],[6,16],[7,33],[13,7],[5,26],[14,24],[3,16],[0,11],[11,28],[11,34],[6,8],[4,25],[14,20],[8,0],[7,32],[7,27],[8,23],[5,14],[4,30],[14,3],[6,21],[6,41],[12,18],[14,25],[8,3],[8,14]] }
assert my_solution.countBlackBlocks(**test_input) == [418,219,34,1,0]
test_input = { "m": 12, "n": 73, "coordinates": [[7,64],[5,14],[5,46]] }
assert my_solution.countBlackBlocks(**test_input) == [780,12,0,0,0]
test_input = { "m": 36, "n": 91, "coordinates": [[14,10],[28,69],[34,15],[6,62],[2,44],[12,81],[19,47],[32,89],[13,59],[12,25],[8,62],[26,87],[31,29],[16,49],[4,46],[3,46],[10,47],[31,87],[15,44],[1,75],[0,61],[3,35],[3,58],[25,88],[15,56],[30,30],[13,26],[9,49],[24,56],[17,17],[19,85],[23,80],[5,68],[30,79],[34,34],[32,69],[19,58],[20,43],[4,40],[33,44],[21,71],[3,37],[34,54],[10,28],[9,62],[20,19],[21,84],[22,32],[9,90],[10,82],[19,38],[15,51],[32,11],[26,72],[34,46],[17,89],[16,28],[15,81],[0,39],[5,28],[10,5],[20,0],[32,40],[14,76],[8,72]] }
assert my_solution.countBlackBlocks(**test_input) == [2905,238,7,0,0]
test_input = { "m": 11, "n": 76, "coordinates": [[2,6],[1,36],[2,60],[3,57],[1,72],[5,15],[1,30],[1,28],[8,15],[4,39],[1,2],[0,56],[5,2],[4,27],[9,2],[3,67],[7,19],[6,54],[7,73],[9,51],[8,63],[0,27],[8,44],[5,31],[0,11]] }
assert my_solution.countBlackBlocks(**test_input) == [657,92,1,0,0]
test_input = { "m": 24, "n": 70, "coordinates": [[9,49],[13,17],[14,52],[17,13],[20,40],[6,62],[18,62],[14,9],[13,0],[15,34],[5,33],[18,1],[17,33],[6,63],[4,26],[3,28],[18,68],[2,15],[5,10],[21,13],[10,53],[5,35],[21,60],[9,59],[1,0],[15,53],[5,45],[0,42],[6,24],[9,9],[2,44],[8,12],[1,16],[13,29],[21,38],[20,39],[4,13],[17,56],[10,45],[3,65],[14,15],[5,62],[13,18],[4,35],[18,11],[12,31],[18,18],[7,50],[12,52],[11,47],[7,14],[11,61],[13,1],[2,1],[4,19],[15,8],[0,7],[18,15],[20,31],[1,17],[15,31],[7,39],[5,52],[8,9],[1,50],[16,34],[21,15],[8,66],[5,53],[18,43],[5,19],[9,61],[11,52],[18,36],[4,9],[21,28],[10,30],[17,68],[4,52],[16,38],[15,57],[13,35],[9,32],[2,65],[17,34],[14,1],[16,3]] }
assert my_solution.countBlackBlocks(**test_input) == [1290,258,35,4,0]
test_input = { "m": 55, "n": 74, "coordinates": [[33,73],[38,37],[4,69],[23,58],[47,17],[48,36],[46,42],[6,63],[22,23],[36,57],[20,38],[42,66],[31,6],[13,3],[5,40],[25,28],[50,7],[6,7],[23,66],[29,17],[5,67],[41,63],[2,53],[3,24],[25,49],[43,18],[27,11],[37,72],[30,14],[16,70],[22,71],[0,48],[21,4],[8,9],[38,57],[14,12],[13,54],[11,40],[10,28],[9,49],[31,24],[11,28],[11,51],[10,62],[46,39],[3,35],[23,15],[52,25],[37,55],[28,63],[1,2],[26,12],[29,30],[44,62],[35,34],[25,31],[8,63],[8,6],[38,14],[19,14],[8,28],[26,14],[27,36],[20,61],[23,12],[15,20]] }
assert my_solution.countBlackBlocks(**test_input) == [3685,254,3,0,0]
test_input = { "m": 6, "n": 79, "coordinates": [[2,64],[4,74],[2,3],[1,63],[2,34],[3,57],[1,1],[3,52],[1,56],[3,1],[1,61],[2,49],[1,66],[2,25],[1,20],[4,51],[0,78],[1,30]] }
assert my_solution.countBlackBlocks(**test_input) == [323,65,2,0,0]
test_input = { "m": 36, "n": 75, "coordinates": [[23,28],[21,10],[16,48],[26,45],[13,45],[24,66],[34,15],[20,13],[4,42],[17,14],[7,19],[8,5],[4,27],[16,32],[19,0],[27,69],[18,62],[18,63],[4,24],[15,51],[24,53],[6,46],[33,51],[13,65],[11,39],[14,37],[33,1],[5,16],[24,15],[30,63],[34,23],[12,22],[29,44],[19,8],[3,4],[12,67],[0,44],[4,65],[5,44],[16,13],[16,71],[16,10],[27,71],[28,56],[1,23],[11,42],[1,1],[10,19],[10,18],[8,55],[23,68],[12,23],[12,61],[31,3],[13,14],[20,30],[25,1],[4,37],[25,32]] }
assert my_solution.countBlackBlocks(**test_input) == [2365,218,7,0,0]
test_input = { "m": 31, "n": 87, "coordinates": [[12,40],[28,11],[20,65],[24,38],[21,65],[29,11],[11,8],[26,16],[8,25],[18,63],[11,69],[9,16],[14,25],[2,34],[7,14],[26,74],[27,51],[13,53],[25,2],[27,17],[3,45],[23,22],[28,38],[5,26],[23,33],[2,16],[14,41],[22,79],[25,75],[7,79],[9,6],[14,73],[15,61],[27,16],[29,21],[27,72],[16,17],[1,37]] }
assert my_solution.countBlackBlocks(**test_input) == [2437,135,7,1,0]
test_input = { "m": 55, "n": 67, "coordinates": [[44,3],[12,10],[20,17],[41,24],[25,40],[21,46],[5,9],[34,64],[29,40],[49,32],[19,37],[42,61],[35,46],[49,52],[16,65],[4,32],[20,48],[5,59],[28,26],[2,3],[40,28],[20,35],[4,29],[47,20],[3,8],[26,39],[7,54],[8,10],[37,5],[33,13],[3,44],[14,63],[21,24],[26,17],[11,60],[19,44],[46,23],[37,6],[9,64],[15,36],[1,36],[44,31],[3,24],[13,3],[49,39],[28,11],[21,32],[27,34],[44,52],[51,51],[17,50],[49,47],[0,5],[11,51],[37,23],[45,45],[50,19],[33,51],[14,36],[7,44],[3,36],[7,27],[18,54],[31,4],[15,12],[38,38],[52,33],[36,16],[25,42],[28,37],[24,50],[26,24],[48,26],[2,24],[33,10],[10,9],[25,26],[15,64],[52,58],[0,44],[7,48],[32,43],[10,6],[43,11],[50,53],[43,26],[13,45],[50,28],[40,0],[31,50],[42,51],[4,59],[18,62],[26,45],[22,44],[37,58],[38,43],[5,53],[23,1],[21,21]] }
assert my_solution.countBlackBlocks(**test_input) == [3182,370,12,0,0]
test_input = { "m": 19, "n": 40, "coordinates": [[8,25],[14,19],[9,0],[10,26],[11,8],[4,8],[2,0],[13,6],[4,24],[6,11],[5,19],[2,21],[4,11],[4,29],[1,25],[6,14],[0,28],[5,39],[7,33],[15,7],[13,25],[15,5],[9,34],[0,33],[6,10],[3,20],[6,13],[14,12],[6,17],[3,1],[2,30],[13,16],[14,1],[9,9],[14,10],[17,4],[13,39],[8,7],[16,19],[2,5],[10,19],[1,6],[15,4],[7,0],[3,18],[16,3],[14,21],[10,31],[11,31],[1,33],[2,23],[17,20],[8,9],[0,16],[4,32],[9,4],[9,8],[0,8],[1,0],[13,1],[1,34],[7,9],[8,34],[4,39],[10,7],[4,5],[9,12],[5,36],[15,0],[15,36],[6,22]] }
assert my_solution.countBlackBlocks(**test_input) == [478,190,32,2,0]
test_input = { "m": 16, "n": 27, "coordinates": [[7,4],[11,26],[2,21],[3,3],[13,11],[7,14],[0,8]] }
assert my_solution.countBlackBlocks(**test_input) == [366,24,0,0,0]
test_input = { "m": 34, "n": 39, "coordinates": [[4,12],[29,24],[15,28],[3,1],[7,0],[0,27],[15,5],[2,0],[16,7],[19,33],[3,11],[5,26],[29,32],[21,32],[31,10],[17,4],[23,32],[5,10],[3,12],[27,11],[12,26],[2,5],[1,0],[20,2],[11,26],[0,11],[12,4],[14,7],[13,18],[9,7],[16,9],[10,3],[3,33],[13,27],[27,18],[11,19],[18,13],[16,21],[2,15],[11,27],[13,33],[8,21],[16,19],[26,27],[3,36],[9,3],[13,38],[10,22],[1,38],[12,31],[13,13],[17,19],[30,1],[15,37],[21,5],[14,15],[6,37],[8,23],[26,22],[24,38],[21,23]] }
assert my_solution.countBlackBlocks(**test_input) == [1041,200,11,2,0]
test_input = { "m": 26, "n": 41, "coordinates": [[12,9],[1,18],[7,10],[22,0],[15,10],[12,3],[20,15],[0,27],[16,35],[20,40],[7,38],[13,27],[9,32],[4,26],[9,40],[4,1],[21,33],[16,12],[6,38],[22,40],[10,24],[20,21],[17,31],[12,6],[8,27],[9,15],[5,13],[14,31],[11,17],[20,31],[22,35],[23,19],[23,16],[3,15],[16,11],[23,27],[9,33],[16,3],[5,12],[21,20],[12,1],[13,30],[13,17],[13,22],[9,28],[7,8],[8,5],[19,15],[16,1],[23,36],[4,6],[18,40],[8,19],[5,2],[21,2],[23,7]] }
assert my_solution.countBlackBlocks(**test_input) == [804,180,16,0,0]
test_input = { "m": 39, "n": 68, "coordinates": [[35,51],[9,25],[13,28],[14,62],[24,6]] }
assert my_solution.countBlackBlocks(**test_input) == [2526,20,0,0,0]
test_input = { "m": 33, "n": 94, "coordinates": [[15,21],[0,63],[25,30],[11,77],[6,65],[14,5],[3,18],[30,61],[21,52],[17,8],[10,69],[24,84],[19,18],[10,75],[4,47],[8,9],[17,22],[3,70],[30,90],[22,58],[15,84],[4,78],[8,15],[20,18],[23,60],[26,83],[27,14],[5,31],[2,48],[18,48],[16,33],[6,31],[1,82],[5,46],[19,13],[0,86],[3,44],[9,49],[13,47],[0,4],[31,52],[6,68],[15,79],[29,47],[6,4],[5,33],[10,9],[30,50],[9,77],[13,79],[20,87],[11,27],[23,86]] }
assert my_solution.countBlackBlocks(**test_input) == [2775,196,5,0,0]
test_input = { "m": 65, "n": 78, "coordinates": [[43,38],[0,36],[40,68],[52,19],[16,59],[10,35],[23,1],[1,58],[29,38],[19,49],[34,40],[57,37],[32,66],[35,37],[16,10],[53,51],[9,53],[23,40],[42,74],[31,51],[17,51],[21,32],[33,72]] }
assert my_solution.countBlackBlocks(**test_input) == [4838,90,0,0,0]
test_input = { "m": 47, "n": 80, "coordinates": [[26,59],[14,64],[4,67],[17,51],[10,66],[18,78],[17,69],[33,57],[18,44],[16,78],[27,39],[22,18],[16,69],[21,5],[19,52],[39,40],[13,17],[15,10],[30,33],[9,67],[34,10],[3,39],[32,52],[33,71],[24,65],[15,57],[43,53]] }
assert my_solution.countBlackBlocks(**test_input) == [3529,102,3,0,0]
test_input = { "m": 42, "n": 99, "coordinates": [[27,29],[39,18],[40,17],[36,14],[17,76],[15,67],[2,41],[37,70],[34,67],[36,15],[23,57],[7,46],[7,42],[3,91],[3,32],[17,30],[35,37],[25,91],[32,83],[20,81],[20,19],[23,51],[17,10],[23,61],[18,24],[3,13],[16,48],[0,88],[31,81],[4,20],[34,95],[30,22],[28,58],[4,50],[1,77],[4,13],[7,37],[13,1],[28,75],[28,77],[4,4],[31,20],[40,6],[18,65]] }
assert my_solution.countBlackBlocks(**test_input) == [3849,164,5,0,0]
test_input = { "m": 77, "n": 80, "coordinates": [[34,8],[7,40],[67,29],[64,70],[32,71],[17,29],[19,15],[6,77],[68,39],[11,9],[9,59],[19,63],[44,11],[24,54],[13,15],[10,23],[27,35],[14,68],[9,61],[17,74],[44,56],[37,78],[20,68],[7,58],[9,38],[72,22],[72,33],[41,33],[64,63],[22,69],[2,12],[51,73],[43,32],[16,57],[11,16],[3,38],[14,45],[73,28],[8,51],[72,42],[18,65],[45,42],[42,18],[34,38],[47,50],[47,0],[29,32],[15,66],[43,61],[49,36],[69,71],[47,45],[55,64],[1,62],[42,0],[18,49],[57,48],[22,16],[14,7],[56,10],[49,42],[19,46],[40,68],[65,49],[8,20],[12,0],[31,17],[50,6],[8,14],[6,38],[32,67],[4,3],[10,13],[44,8],[21,43],[70,34],[49,22],[72,47],[59,59],[56,72],[33,30],[18,20],[2,76],[22,33]] }
assert my_solution.countBlackBlocks(**test_input) == [5674,330,0,0,0]
test_input = { "m": 53, "n": 93, "coordinates": [] }
assert my_solution.countBlackBlocks(**test_input) == [4784,0,0,0,0]
test_input = { "m": 48, "n": 91, "coordinates": [[19,79],[30,11],[6,54],[35,3],[41,20],[8,3],[8,46],[37,67],[21,61],[27,70],[15,32],[44,61],[1,70],[9,68],[28,16],[19,43],[12,68],[21,12],[7,77],[18,32],[1,25],[31,11],[1,82],[10,18],[31,86],[21,24],[11,43],[15,4],[8,12],[39,68],[11,65],[37,53],[10,39],[30,61],[34,46],[33,9],[30,64],[27,59],[31,60],[14,64],[11,70],[16,83],[28,15],[26,21],[33,71],[31,53],[39,36],[27,77],[41,82],[21,45],[3,59],[42,14],[19,59],[5,57]] }
assert my_solution.countBlackBlocks(**test_input) == [4019,206,5,0,0]
test_input = { "m": 53, "n": 77, "coordinates": [[29,74],[44,76],[10,11],[11,14],[45,0],[41,19],[32,49],[21,50],[14,41],[7,74],[42,47],[50,19],[6,34],[47,0],[19,64],[39,25],[49,46],[45,1],[9,67],[51,19],[6,59],[24,52],[40,61],[48,23],[23,49],[4,64],[40,27],[49,42],[17,30],[13,14],[3,49],[41,14],[36,0],[5,32],[6,1],[4,27],[11,22],[26,37],[5,61],[22,15],[42,1],[9,33],[20,25],[44,38],[46,16],[10,20],[45,13],[31,27],[27,70],[22,11],[28,17]] }
assert my_solution.countBlackBlocks(**test_input) == [3760,188,4,0,0]
test_input = { "m": 12, "n": 69, "coordinates": [[8,6],[1,19],[0,23],[7,8],[7,31],[10,16],[9,2],[6,1],[8,63],[7,45],[5,40],[1,27],[2,14],[6,54],[0,4],[1,62],[4,45],[5,31],[7,33],[2,38]] }
assert my_solution.countBlackBlocks(**test_input) == [672,76,0,0,0]
test_input = { "m": 49, "n": 78, "coordinates": [[4,26],[9,25],[9,77],[42,45],[47,12],[30,68],[15,63],[23,11],[24,24],[23,49],[26,77]] }
assert my_solution.countBlackBlocks(**test_input) == [3656,40,0,0,0]
test_input = { "m": 63, "n": 95, "coordinates": [[36,71],[2,38],[25,72],[16,54],[38,59],[44,82],[7,57],[8,65],[5,90],[8,82],[56,26],[39,15],[36,30],[31,53],[59,80],[38,25],[50,47],[12,72],[27,26],[41,23],[20,25],[19,74],[22,18],[19,75],[53,61],[25,17],[12,22],[32,40],[40,1],[6,7],[6,24],[46,43],[39,84],[17,92],[12,91],[32,25],[20,68],[11,12],[23,20],[7,36],[42,22],[21,69],[19,9],[25,8],[57,82],[52,83],[43,81],[60,19],[26,83],[18,73],[44,12],[30,45],[25,84],[55,77],[35,41],[53,8],[0,44],[29,36],[36,69],[9,53],[37,15],[24,0],[13,60],[31,62],[56,84],[45,59],[45,82],[58,35],[12,15],[34,60],[28,51],[18,40],[53,73],[10,48],[28,6],[8,16],[41,34],[50,15],[23,30],[33,21],[2,34]] }
assert my_solution.countBlackBlocks(**test_input) == [5517,302,9,0,0]
test_input = { "m": 42, "n": 58, "coordinates": [[35,34],[26,21],[17,14],[34,44],[12,51],[1,33],[23,17],[30,54],[25,46],[39,12],[21,13],[26,37],[13,38],[37,10],[27,57],[3,14],[19,31]] }
assert my_solution.countBlackBlocks(**test_input) == [2271,66,0,0,0]
test_input = { "m": 6, "n": 12, "coordinates": [[4,7],[2,11],[0,7],[0,10],[1,4],[2,0],[2,10],[0,8],[3,8],[3,2],[0,5],[1,10],[0,9],[3,4],[2,9],[1,1],[3,7],[2,3],[0,0]] }
assert my_solution.countBlackBlocks(**test_input) == [18,19,14,4,0]
test_input = { "m": 51, "n": 77, "coordinates": [[41,39],[28,5],[6,8],[36,7],[2,46],[18,75],[12,63],[42,26],[16,42],[6,9],[9,55],[37,61],[8,69],[35,10],[34,51],[33,2],[37,64],[5,32],[17,53],[38,51],[42,18],[24,35],[8,25],[17,58],[41,53],[2,9],[48,37],[13,7],[47,20],[28,59],[17,8],[32,27],[39,16],[43,74],[38,61],[40,14],[35,28],[9,46],[30,44],[46,12],[7,45],[26,76],[34,10],[28,51],[47,1],[46,65],[29,71],[2,24],[11,43],[16,28],[33,40],[18,29],[8,73],[30,55],[0,47],[45,34],[3,45],[16,5],[33,9],[4,68],[36,47],[11,39],[8,37],[6,63],[42,51],[4,16],[37,5],[32,60],[40,17],[9,14],[5,29],[22,34],[47,21],[44,37],[2,75],[21,76],[30,46],[20,60]] }
assert my_solution.countBlackBlocks(**test_input) == [3505,284,11,0,0]
test_input = { "m": 58, "n": 65, "coordinates": [[20,24],[4,13],[38,14],[41,21],[6,36],[12,3],[51,52],[10,7],[14,50],[47,60],[30,59],[46,58],[55,49],[35,25],[15,53],[24,39],[55,40],[4,43],[36,28],[21,38],[36,1],[44,42],[12,26],[50,30],[36,14],[53,0],[11,48],[0,55],[42,17],[34,48],[15,43],[13,6],[33,29],[32,20],[12,13],[7,1],[22,41],[26,64],[16,16],[3,5],[14,0],[39,19],[32,47],[47,14],[51,64],[51,12],[7,45],[43,46],[47,28],[50,60],[45,8],[6,30],[9,35],[21,47],[17,39]] }
assert my_solution.countBlackBlocks(**test_input) == [3438,210,0,0,0]
test_input = { "m": 6, "n": 10, "coordinates": [[2,7],[0,4],[3,3],[0,2],[3,0],[4,2],[2,1]] }
assert my_solution.countBlackBlocks(**test_input) == [25,18,2,0,0]
test_input = { "m": 37, "n": 75, "coordinates": [[34,9],[30,27],[20,54],[35,35],[24,8],[0,68],[22,23],[10,20],[4,32],[8,20],[33,6],[7,63],[17,72],[8,37],[28,48],[25,24],[25,74],[20,38],[9,51],[10,74],[3,37],[20,17],[11,31],[14,67],[19,26],[34,7],[31,51],[5,47],[24,28]] }
assert my_solution.countBlackBlocks(**test_input) == [2555,108,1,0,0]
test_input = { "m": 32, "n": 90, "coordinates": [[14,54],[13,33],[27,23],[28,68],[23,46],[12,22],[4,85],[4,41],[26,32],[0,42],[16,24],[17,83],[28,75],[4,39],[5,46],[10,51],[27,88],[6,69],[22,11],[7,9],[19,37],[0,74],[24,85],[28,74],[28,3],[13,80],[9,30],[16,86],[3,22],[2,3],[21,68],[11,32],[19,68],[25,83],[25,31],[12,10],[26,3],[16,57],[24,47],[12,73],[21,87],[9,28],[21,16],[23,29],[25,13],[23,82],[19,7],[29,11],[21,78],[24,23],[7,66],[9,57],[6,78],[13,22],[0,39],[20,48],[18,66],[17,77],[25,66],[11,25],[19,26],[20,75],[29,63],[3,52],[28,15],[13,84],[12,21],[0,4],[7,60],[16,74],[1,3],[7,5],[14,27],[22,4],[12,6],[16,0],[16,14],[12,17],[27,75],[20,16],[0,16],[9,26],[4,43],[19,0],[22,81],[12,71],[15,54],[3,61],[13,72],[7,71],[2,30],[2,18],[16,15],[28,5],[2,7],[25,89]] }
assert my_solution.countBlackBlocks(**test_input) == [2413,326,18,2,0]
test_input = { "m": 21, "n": 81, "coordinates": [[5,71],[9,65],[12,36],[19,38],[7,66],[7,2],[14,13],[4,71],[19,32],[14,30],[13,21],[16,79],[0,2],[3,27],[13,72],[18,49],[0,72],[0,52],[18,24],[18,41],[13,7],[1,29],[15,24],[16,2],[8,25],[5,51],[19,15],[12,18],[16,61],[9,6],[7,61],[18,37],[10,79],[6,35],[17,12],[2,36],[13,64],[9,59],[5,42],[19,30],[7,51],[15,53],[10,75],[2,34],[8,0],[2,70],[3,36],[12,38],[4,35],[9,25],[13,68],[13,13],[14,25],[6,48],[15,33],[15,29]] }
assert my_solution.countBlackBlocks(**test_input) == [1396,192,12,0,0]
test_input = { "m": 44, "n": 54, "coordinates": [[37,23],[27,31],[19,28],[30,2],[15,29],[4,40],[7,47],[36,18],[17,49],[18,51],[29,42],[8,34],[28,51],[17,27],[18,49],[26,16],[34,39],[11,19],[20,43],[23,47],[25,0],[20,32],[16,5],[29,30],[22,3],[4,12],[13,8],[5,6],[35,40],[15,49],[27,14],[37,8],[41,15],[0,16],[37,37],[38,31],[36,52],[18,22],[30,3],[31,18]] }
assert my_solution.countBlackBlocks(**test_input) == [2128,146,5,0,0]
test_input = { "m": 2, "n": 52, "coordinates": [[0,24],[0,29],[0,14],[0,23],[0,33],[0,51],[0,27],[0,50],[0,7],[0,32],[0,9],[0,26],[0,17],[0,31],[0,19],[0,11],[0,3],[0,15],[0,20],[0,12],[0,18],[0,45],[0,48],[0,41],[0,8],[0,42],[0,47],[0,43],[0,2],[0,34],[0,0],[0,5],[0,49],[0,4],[0,36],[0,40],[0,44],[0,6],[0,38],[0,22],[0,13]] }
assert my_solution.countBlackBlocks(**test_input) == [0,22,29,0,0]
test_input = { "m": 3, "n": 75, "coordinates": [[0,49],[1,17],[1,18],[0,20],[1,65],[0,35],[1,70],[1,20],[0,43],[1,58],[1,36],[0,28],[0,74],[0,30],[1,51],[1,63],[1,66]] }
assert my_solution.countBlackBlocks(**test_input) == [102,39,7,0,0]
test_input = { "m": 33, "n": 93, "coordinates": [[18,53],[29,30],[29,23],[10,74],[5,25],[17,26],[5,87],[27,11],[29,16],[22,34],[19,86],[24,80],[11,46],[13,41],[7,24],[24,86],[30,89],[17,92],[11,21],[8,71],[7,8],[24,21],[22,32],[27,21],[29,17],[4,25],[22,91],[23,81],[10,59],[19,87],[20,16],[30,56],[13,70],[18,78],[13,27],[3,48],[8,3],[1,27],[16,62],[3,46],[6,23],[22,12],[0,26],[18,91],[0,1],[8,36],[8,16],[27,17],[12,1],[9,88]] }
assert my_solution.countBlackBlocks(**test_input) == [2760,174,10,0,0]
test_input = { "m": 70, "n": 88, "coordinates": [[40,71],[29,33],[59,67],[55,61],[17,41]] }
assert my_solution.countBlackBlocks(**test_input) == [5983,20,0,0,0]
test_input = { "m": 76, "n": 88, "coordinates": [[39,76],[42,86],[4,51],[60,0],[5,28],[1,46],[28,25],[21,12],[5,39],[19,20],[71,58],[70,25],[38,32],[42,51],[49,62],[39,20],[48,24],[10,14],[17,85],[27,2],[5,40],[56,23],[21,67],[15,28],[15,84],[73,34],[50,81],[20,68],[0,30],[30,53],[33,82],[58,65],[4,7],[73,50],[55,25],[45,59],[69,78],[63,47],[19,83],[46,41],[23,66],[0,76],[63,18],[1,3],[18,71],[27,79],[51,21],[9,46],[48,30],[11,30],[67,74],[74,9],[8,8],[29,9],[66,4],[52,42],[21,56],[40,74],[16,57],[54,64],[41,4],[20,64],[28,9],[55,85],[13,35],[15,33],[43,43],[24,61],[11,52],[34,70],[19,66],[23,57],[68,86],[58,53],[23,19],[46,10],[59,49],[66,9],[24,64],[23,36],[20,10],[1,68],[12,20],[8,73],[59,41]] }
assert my_solution.countBlackBlocks(**test_input) == [6196,324,5,0,0]
test_input = { "m": 77, "n": 92, "coordinates": [[57,46],[24,2],[13,78],[50,91],[50,3],[17,80],[62,17],[5,88],[17,53],[73,42],[10,62],[21,60],[36,45],[36,53],[57,29],[68,43],[4,80],[74,26],[18,23],[44,69],[9,16],[52,20],[25,13],[0,60],[23,80],[26,23],[49,34],[58,71],[24,53],[56,82],[30,63],[53,19],[34,14],[47,5],[2,49],[20,2],[38,87],[30,46],[56,48],[34,1],[24,14],[70,40],[33,74],[65,86],[17,39],[48,80],[24,19],[30,90],[17,61],[31,83],[66,79],[25,30],[57,51],[25,43],[54,47],[60,12],[17,67],[2,57],[40,32],[58,90],[19,62],[9,47],[45,26],[0,37],[10,65],[64,1],[64,38],[2,34],[53,4],[16,37],[2,22],[69,91],[27,64],[50,4],[27,88],[17,47],[9,28],[19,57],[35,88],[4,55],[7,21],[37,68],[8,83],[70,37],[38,0],[40,89],[5,36],[74,37],[59,64],[28,89],[34,75],[42,21],[51,26],[21,57],[66,63]] }
assert my_solution.countBlackBlocks(**test_input) == [6552,358,6,0,0]
test_input = { "m": 25, "n": 52, "coordinates": [[7,8],[3,21],[10,39],[12,35],[20,21],[5,37],[4,28],[15,49],[14,26],[12,47],[10,28]] }
assert my_solution.countBlackBlocks(**test_input) == [1180,44,0,0,0]
test_input = { "m": 78, "n": 84, "coordinates": [[70,29],[22,36],[60,16],[39,28],[67,15],[50,11],[69,28],[24,32],[48,52]] }
assert my_solution.countBlackBlocks(**test_input) == [6356,34,1,0,0]
test_input = { "m": 46, "n": 76, "coordinates": [[18,2],[41,13],[38,28],[9,54],[35,67],[31,72],[29,47],[44,16],[35,5],[36,14],[16,69],[34,74],[15,32],[37,29],[41,37],[5,9],[26,68],[40,38],[30,38],[42,57],[2,2],[42,54],[4,23],[40,15],[22,18],[0,28],[34,33],[37,17],[24,49],[25,73],[35,63],[20,60],[20,16],[10,50],[24,46],[26,8],[43,56],[9,37],[24,56],[42,73],[2,9],[14,72],[35,0],[40,20],[30,8],[12,30],[15,37],[42,75],[31,2],[43,34],[24,18],[19,27],[42,23],[6,70],[22,21],[15,63],[13,39],[36,12],[13,46],[23,71],[21,44],[21,26],[3,15],[41,41],[17,43],[34,47],[4,65],[36,18],[19,1],[31,55],[44,21],[15,9],[18,46],[29,32],[32,19],[1,48],[41,55],[40,66],[43,3],[32,25],[36,49],[7,5],[26,4],[27,21],[27,44]] }
assert my_solution.countBlackBlocks(**test_input) == [3047,322,6,0,0]
test_input = { "m": 6, "n": 35, "coordinates": [[1,26],[4,24],[2,2],[4,17],[2,5],[3,32],[3,21],[1,0],[2,8],[4,20],[1,4],[2,9],[1,17],[3,1],[0,2],[4,6],[0,18],[4,23],[2,24],[0,16],[1,15],[3,22],[2,12],[1,18],[4,21],[0,26],[4,19],[4,2],[3,34],[0,24],[3,31],[2,34],[3,2],[1,24],[0,33],[0,31]] }
assert my_solution.countBlackBlocks(**test_input) == [81,59,25,5,0]
test_input = { "m": 95, "n": 99, "coordinates": [[40,34],[77,26],[5,19],[84,36],[6,42],[92,59],[67,70],[62,38],[87,44],[79,79],[92,44],[75,85],[43,2],[91,34],[78,69],[19,1],[10,15],[82,79],[28,14],[79,72],[57,84],[75,93],[29,93],[91,66],[84,38],[76,81],[5,11],[32,7],[48,25],[10,70],[67,37],[31,91],[45,19],[59,19],[29,98],[37,57],[83,18],[44,80],[91,27],[74,69],[13,62],[5,23],[91,70],[89,71],[11,32],[82,30],[45,61],[10,50],[25,65],[22,32],[77,13],[9,79],[69,37],[77,63],[6,48],[7,77],[35,8],[27,94],[45,37],[30,78],[81,21],[89,84],[75,73],[74,18],[4,28],[10,66],[43,54],[28,7],[26,41],[2,74],[6,70],[8,30],[18,98],[63,10],[22,98]] }
assert my_solution.countBlackBlocks(**test_input) == [8918,294,0,0,0]
test_input = { "m": 57, "n": 91, "coordinates": [[42,12],[38,56],[28,82],[54,52],[24,5]] }
assert my_solution.countBlackBlocks(**test_input) == [5020,20,0,0,0]
test_input = { "m": 60, "n": 73, "coordinates": [[22,24],[22,4],[45,1],[38,57],[54,41],[6,58],[8,44],[40,42],[19,47],[31,47],[48,33],[43,37],[22,54],[36,23],[23,9],[51,15],[24,39],[32,0],[17,7],[41,27],[39,12],[11,10],[22,58],[13,69],[37,37],[17,48],[18,19],[52,40],[52,32],[11,2],[32,21],[39,33],[32,1],[54,17],[0,71],[7,21],[18,24],[0,13],[22,29],[49,62],[44,63],[53,53],[31,44],[28,5],[46,65],[37,27],[39,55],[16,56],[33,10],[40,19],[43,58],[6,65],[24,41],[48,61],[28,46],[0,15],[0,55],[29,49],[5,72],[34,21],[33,40],[14,34],[9,70],[3,48],[16,15],[56,68],[18,68],[2,13],[47,38],[55,55],[22,13],[19,59],[48,60],[53,2],[46,67],[58,9],[56,67],[23,50]] }
assert my_solution.countBlackBlocks(**test_input) == [3955,286,7,0,0]
test_input = { "m": 14, "n": 76, "coordinates": [[1,20],[9,9],[6,57],[3,8],[6,71],[2,67],[12,53],[6,44],[6,16],[3,26],[10,39],[7,4]] }
assert my_solution.countBlackBlocks(**test_input) == [927,48,0,0,0]
test_input = { "m": 69, "n": 72, "coordinates": [[45,12],[54,37],[43,47],[4,45],[28,24],[56,5],[10,67],[30,0],[34,48],[40,45],[27,47],[47,58],[63,59],[53,53],[24,31],[16,51],[55,40],[42,27],[7,38],[3,0],[9,43],[17,15],[63,52],[27,24],[6,32],[13,50],[55,68],[11,34],[42,18],[20,42],[48,20],[36,8],[29,50],[4,55],[33,24],[43,32],[20,67],[29,37],[36,7],[66,60],[61,6],[8,28],[55,31],[61,17],[55,49],[64,33],[32,9],[54,67],[34,40],[52,39],[45,64],[4,38],[44,8],[48,34],[35,14],[25,43],[35,31],[10,21],[63,13],[7,0],[13,54],[54,68],[13,44],[51,62],[17,28],[33,31],[8,70],[25,32],[44,49],[25,16],[35,44],[23,37],[23,48],[8,49],[23,42]] }
assert my_solution.countBlackBlocks(**test_input) == [4543,277,7,1,0]
test_input = { "m": 29, "n": 79, "coordinates": [[26,78],[24,55],[3,8],[3,55],[12,76],[11,23],[4,52],[5,73],[27,21],[8,19],[14,54],[10,4],[8,23],[13,16],[4,1],[21,47],[11,15],[2,26],[24,59],[12,22],[2,69],[20,1],[10,74],[25,53],[0,13],[9,28],[7,4],[10,76],[13,75],[24,25],[14,19],[12,7],[5,53],[5,59],[7,41],[8,25],[7,65],[23,48],[18,37],[7,67],[2,76],[8,64],[22,46],[2,16],[20,31],[7,38],[13,66],[22,30],[17,23],[22,60],[4,10],[6,37],[5,33],[19,58],[22,53],[9,53],[4,22],[12,4],[2,51],[3,63],[4,18],[22,43]] }
assert my_solution.countBlackBlocks(**test_input) == [1946,232,6,0,0]
test_input = { "m": 6, "n": 58, "coordinates": [[0,33],[3,43],[4,25],[0,42],[4,2],[0,31],[2,10],[0,18],[1,46],[2,55],[0,13],[1,8],[2,24],[1,54],[3,54],[0,6],[0,37],[3,34],[3,53],[1,33],[2,1],[2,26],[3,11],[2,37],[1,24],[0,2],[0,14],[2,33],[0,41],[4,7],[4,28],[3,48],[3,0],[4,38],[1,34],[0,26],[4,43],[3,35],[2,7],[4,37],[4,26],[4,51],[4,21],[3,1],[4,53],[0,34],[4,3],[3,50],[3,24],[2,5],[3,40],[2,14],[4,39],[2,43],[2,50],[0,21],[2,8],[0,51],[0,47],[1,22],[3,17],[4,13],[2,2],[3,55],[4,29],[0,16],[1,55],[2,49],[0,24],[1,16],[2,17],[1,19],[1,42],[4,40],[0,5],[0,9],[2,32],[2,25],[0,40],[2,18],[3,31],[3,26],[1,57],[0,45],[0,17],[2,22],[3,3],[1,27],[1,20],[0,44],[2,0],[3,30],[2,40],[3,39],[4,50]] }
assert my_solution.countBlackBlocks(**test_input) == [80,107,76,19,3]
test_input = { "m": 28, "n": 64, "coordinates": [[3,3],[13,53],[17,60],[1,30],[21,59],[1,51],[2,49],[1,16],[14,22],[23,17],[13,55],[8,38],[13,31],[8,34],[17,6]] }
assert my_solution.countBlackBlocks(**test_input) == [1641,60,0,0,0]
test_input = { "m": 15, "n": 45, "coordinates": [[4,29],[0,10],[10,21],[6,5],[4,27],[3,11],[8,36],[1,38],[5,19],[9,1],[5,14],[8,18],[2,8],[13,5],[5,11],[6,37],[2,17],[5,39],[7,3],[2,3],[9,20],[9,41],[0,40],[10,20],[12,30],[2,4],[12,0],[5,16],[11,35],[4,38],[9,11],[3,34],[4,39],[5,8],[10,27],[5,13],[2,36],[9,44],[5,42],[7,43],[13,16],[0,30],[6,35],[8,12],[7,33],[11,12],[13,0],[10,0],[4,17],[0,34],[10,4],[0,22],[2,25],[8,39],[9,40],[4,35],[9,10],[7,18],[7,28],[13,12],[7,8],[10,37],[11,27],[12,40],[8,0],[7,4]] }
assert my_solution.countBlackBlocks(**test_input) == [402,186,26,2,0]
test_input = { "m": 50, "n": 60, "coordinates": [[37,49],[8,45],[11,21],[38,10],[47,35],[15,33],[18,8],[15,22],[16,24],[14,50],[38,24],[8,23],[24,58],[11,35],[37,39],[29,18],[1,8],[40,47],[40,59],[47,48],[15,54],[16,33],[22,29],[39,16],[35,43],[40,54],[11,8],[16,14],[8,7],[38,1],[17,3],[8,3],[0,38],[43,22],[14,10],[31,15],[11,46],[21,45],[13,6],[0,34],[2,42],[47,57],[30,2],[6,38],[8,31],[11,44],[44,36],[10,45],[41,29],[31,30],[47,54],[3,51],[26,5],[3,43],[22,42],[27,28],[48,4],[0,49],[45,22],[7,58],[26,27],[35,31],[40,15],[45,44],[42,34],[17,54]] }
assert my_solution.countBlackBlocks(**test_input) == [2642,242,7,0,0]
test_input = { "m": 36, "n": 54, "coordinates": [[7,0],[22,50],[2,41],[33,48],[1,26]] }
assert my_solution.countBlackBlocks(**test_input) == [1837,18,0,0,0]
test_input = { "m": 26, "n": 68, "coordinates": [[8,51],[18,5],[18,64]] }
assert my_solution.countBlackBlocks(**test_input) == [1663,12,0,0,0]
test_input = { "m": 21, "n": 46, "coordinates": [[12,7],[12,35],[13,14],[11,22],[2,1],[11,28],[1,7],[6,36],[2,15],[15,18],[1,39],[12,38],[11,35],[9,9],[17,6],[12,10]] }
assert my_solution.countBlackBlocks(**test_input) == [838,60,2,0,0]
test_input = { "m": 35, "n": 74, "coordinates": [[9,54],[9,23],[8,48],[14,29],[17,37],[6,4],[17,39],[18,41],[18,50],[24,20],[29,65],[30,3],[5,19],[11,58],[6,67],[21,44],[8,3],[7,11],[18,29],[22,46],[19,51],[4,49],[13,15],[19,48],[12,33],[8,6],[25,48],[17,70],[2,68],[21,53],[9,37],[27,8],[14,69],[2,16],[20,54],[8,67],[31,44],[10,38],[19,21],[16,43],[25,65],[33,20],[17,5],[23,6],[14,23],[14,26],[1,61],[28,21],[1,68],[24,59],[19,17],[23,4],[25,52],[2,43],[30,13],[31,34],[32,43],[14,20],[11,4],[30,20],[0,67],[27,20],[32,64],[20,17],[24,61],[5,35],[31,18],[28,40],[13,11],[29,59],[17,50],[13,54],[20,67],[32,2],[4,18]] }
assert my_solution.countBlackBlocks(**test_input) == [2197,272,13,0,0]
test_input = { "m": 28, "n": 52, "coordinates": [[3,23],[24,28],[7,19],[11,12],[4,27],[3,17],[8,41],[20,30],[23,24],[1,41],[12,30],[2,50],[18,36],[5,7],[1,42],[7,0],[10,51],[20,46],[20,24],[19,7],[23,28],[26,8],[17,31],[22,7],[22,26],[15,8],[19,27],[16,33],[15,39],[13,51],[20,47],[3,39],[25,36],[25,37],[10,0],[19,26],[8,9],[12,31],[19,44],[8,1],[3,12],[8,5],[23,46],[22,15],[18,2],[22,10],[24,43],[16,9],[18,22],[22,8],[20,6],[3,1],[8,17],[25,1],[9,32],[11,34],[7,44],[2,38],[13,37],[26,1],[2,26],[14,38],[6,36],[22,24],[6,27],[18,38],[25,41],[22,39],[18,51],[2,18],[12,24],[4,14],[21,38],[6,44],[18,25],[12,1],[12,4],[1,22],[12,40],[18,11]] }
assert my_solution.countBlackBlocks(**test_input) == [1096,252,29,0,0]
test_input = { "m": 5, "n": 56, "coordinates": [[1,2],[3,7],[2,24],[0,5],[2,3],[2,25],[3,20],[1,8],[2,26],[0,53],[2,44],[0,49],[3,30],[0,11],[1,6],[0,14],[1,9],[2,34],[2,54],[0,31],[1,10],[0,39],[0,26],[0,12],[0,37],[0,42],[0,28],[2,29],[2,46],[1,44],[0,47],[0,35],[1,53],[1,38],[0,24],[0,2],[3,23],[3,36],[3,24],[2,17],[2,35],[3,37],[2,0],[3,45],[0,16],[1,34],[0,29],[1,5],[2,38]] }
assert my_solution.countBlackBlocks(**test_input) == [105,76,35,4,0]
test_input = { "m": 36, "n": 38, "coordinates": [[7,13],[24,30],[23,29],[31,14],[28,33],[16,37],[3,27],[21,36],[9,30],[22,12],[22,21],[6,37],[18,31],[19,4],[18,20],[11,8],[16,14],[7,1],[11,30],[23,16],[15,15],[23,26],[13,12],[11,19]] }
assert my_solution.countBlackBlocks(**test_input) == [1205,88,2,0,0]
test_input = { "m": 70, "n": 75, "coordinates": [[17,67],[60,71],[53,71],[14,36],[19,57],[35,22],[8,11],[27,36],[47,71],[58,54],[35,63],[54,18],[17,22],[34,60],[26,35],[61,39],[65,20],[38,37],[58,34],[34,23],[63,32],[55,32],[35,53],[29,52],[6,4],[5,66],[5,6],[6,60],[61,50],[16,24],[24,51],[65,45],[11,14],[7,27],[67,24],[34,53],[61,16],[60,50],[19,41],[56,19],[10,15],[17,0],[8,59],[32,24],[35,27],[41,34],[59,6],[17,30]] }
assert my_solution.countBlackBlocks(**test_input) == [4923,176,7,0,0]
test_input = { "m": 8, "n": 68, "coordinates": [[5,36],[4,65],[1,9],[5,49],[5,34],[6,59],[5,27],[4,2],[6,53],[6,22],[4,60],[3,65],[6,16],[2,56],[0,27],[6,25],[3,32],[5,2],[3,63],[0,58],[0,37],[6,44],[0,61],[6,37],[1,36],[2,1],[1,4],[6,24],[5,12],[5,23],[0,57],[4,59],[1,44],[2,51],[5,1],[0,8],[5,22],[4,3],[5,0],[5,45],[2,8],[1,12],[1,8],[2,10],[0,48],[1,55],[1,14],[3,12],[5,39],[1,31],[1,27],[0,31],[4,61],[0,54],[2,46],[4,66],[6,32],[5,53],[3,11],[5,4],[2,21],[1,49]] }
assert my_solution.countBlackBlocks(**test_input) == [288,140,35,6,0] | 1,688,826,600 |
weekly-contest-352-longest-even-odd-subarray-with-threshold | https://leetcode.com/problems/longest-even-odd-subarray-with-threshold | longest-even-odd-subarray-with-threshold | {
"questionId": "2866",
"questionFrontendId": "2760",
"title": "Longest Even Odd Subarray With Threshold",
"titleSlug": "longest-even-odd-subarray-with-threshold",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 248,
"dislikes": 243,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 和一个整数 threshold 。
请你从 nums 的子数组中找出以下标 l 开头、下标 r 结尾 (0 <= l <= r < nums.length) 且满足以下条件的 最长子数组 :
* nums[l] % 2 == 0
* 对于范围 [l, r - 1] 内的所有下标 i ,nums[i] % 2 != nums[i + 1] % 2
* 对于范围 [l, r] 内的所有下标 i ,nums[i] <= threshold
以整数形式返回满足题目要求的最长子数组的长度。
注意:子数组 是数组中的一个连续非空元素序列。
示例 1:
输入:nums = [3,2,5,4], threshold = 5
输出:3
解释:在这个示例中,我们选择从 l = 1 开始、到 r = 3 结束的子数组 => [2,5,4] ,满足上述条件。
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。
示例 2:
输入:nums = [1,2], threshold = 2
输出:1
解释:
在这个示例中,我们选择从 l = 1 开始、到 r = 1 结束的子数组 => [2] 。
该子数组满足上述全部条件。可以证明 1 是满足题目要求的最大长度。
示例 3:
输入:nums = [2,3,4,5], threshold = 4
输出:3
解释:
在这个示例中,我们选择从 l = 0 开始、到 r = 2 结束的子数组 => [2,3,4] 。
该子数组满足上述全部条件。
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。
提示:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 100
* 1 <= threshold <= 100
"""
class Solution:
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
| 给你一个下标从 0 开始的整数数组 nums 和一个整数 threshold 。
请你从 nums 的子数组中找出以下标 l 开头、下标 r 结尾 (0 <= l <= r < nums.length) 且满足以下条件的 最长子数组 :
* nums[l] % 2 == 0
* 对于范围 [l, r - 1] 内的所有下标 i ,nums[i] % 2 != nums[i + 1] % 2
* 对于范围 [l, r] 内的所有下标 i ,nums[i] <= threshold
以整数形式返回满足题目要求的最长子数组的长度。
注意:子数组 是数组中的一个连续非空元素序列。
示例 1:
输入:nums = [3,2,5,4], threshold = 5
输出:3
解释:在这个示例中,我们选择从 l = 1 开始、到 r = 3 结束的子数组 => [2,5,4] ,满足上述条件。
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。
示例 2:
输入:nums = [1,2], threshold = 2
输出:1
解释:
在这个示例中,我们选择从 l = 1 开始、到 r = 1 结束的子数组 => [2] 。
该子数组满足上述全部条件。可以证明 1 是满足题目要求的最大长度。
示例 3:
输入:nums = [2,3,4,5], threshold = 4
输出:3
解释:
在这个示例中,我们选择从 l = 0 开始、到 r = 2 结束的子数组 => [2,3,4] 。
该子数组满足上述全部条件。
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。
提示:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 100
* 1 <= threshold <= 100
请完成下面的代码来解决上述问题:
```python
class Solution:
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [3,2,5,4], "threshold": 5 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3
test_input = { "nums": [1,2], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,3,4,5], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3
test_input = { "nums": [1], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [2], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [3], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [4], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [5], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [6], "threshold": 5 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [7], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [8], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [9], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [10], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,3], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,6], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,10], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,10], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [2,2], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,4], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,5], "threshold": 2 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,6], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,7], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [2,8], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,8], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,8], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [2,9], "threshold": 14 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [3,1], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [3,4], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [3,10], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [4,2], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [4,2], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [4,4], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [4,7], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [4,9], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [5,8], "threshold": 5 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [5,8], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [6,2], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [6,4], "threshold": 14 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [6,4], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [6,5], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [7,3], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [7,4], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [7,5], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [7,10], "threshold": 52 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [7,17], "threshold": 31 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [8,4], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [8,8], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [9,2], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [9,4], "threshold": 15 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [10,3], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [10,4], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [10,5], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [10,7], "threshold": 11 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [10,8], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [10,18], "threshold": 43 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [12,34], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [12,35], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [13,9], "threshold": 53 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [15,13], "threshold": 23 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [15,15], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [17,2], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [23,37], "threshold": 35 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [24,11], "threshold": 54 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [27,9], "threshold": 55 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [27,17], "threshold": 40 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [33,4], "threshold": 43 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [41,16], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [47,44], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [49,39], "threshold": 52 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [50,8], "threshold": 19 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [76,46], "threshold": 91 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [1,1,7], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,3,1], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,4,3], "threshold": 1 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,5,3], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [1,10,5], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [2,1,8], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [2,10,5], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [3,2,8], "threshold": 18 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [3,3,10], "threshold": 20 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [3,4,2], "threshold": 19 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [3,6,10], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [3,8,9], "threshold": 19 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [4,3,1], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [4,4,4], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [4,5,10], "threshold": 3 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [4,10,2], "threshold": 4 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [4,10,3], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [4,10,3], "threshold": 10 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [4,40,8], "threshold": 45 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [5,3,9], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [5,5,6], "threshold": 7 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [6,2,2], "threshold": 16 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [6,2,4], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1
test_input = { "nums": [6,3,4], "threshold": 6 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3
test_input = { "nums": [6,5,2], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3
test_input = { "nums": [6,5,3], "threshold": 17 }
assert my_solution.longestAlternatingSubarray(**test_input) == 2
test_input = { "nums": [6,7,2], "threshold": 14 }
assert my_solution.longestAlternatingSubarray(**test_input) == 3
test_input = { "nums": [7,1,10], "threshold": 9 }
assert my_solution.longestAlternatingSubarray(**test_input) == 0
test_input = { "nums": [7,5,6], "threshold": 8 }
assert my_solution.longestAlternatingSubarray(**test_input) == 1 | 1,688,265,000 |
weekly-contest-352-prime-pairs-with-target-sum | https://leetcode.com/problems/prime-pairs-with-target-sum | prime-pairs-with-target-sum | {
"questionId": "2873",
"questionFrontendId": "2761",
"title": "Prime Pairs With Target Sum",
"titleSlug": "prime-pairs-with-target-sum",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 311,
"dislikes": 30,
"categoryTitle": "Algorithms"
} | """
给你一个整数 n 。如果两个整数 x 和 y 满足下述条件,则认为二者形成一个质数对:
* 1 <= x <= y <= n
* x + y == n
* x 和 y 都是质数
请你以二维有序列表的形式返回符合题目要求的所有 [xi, yi] ,列表需要按 xi 的 非递减顺序 排序。如果不存在符合要求的质数对,则返回一个空数组。
注意:质数是大于 1 的自然数,并且只有两个因子,即它本身和 1 。
示例 1:
输入:n = 10
输出:[[3,7],[5,5]]
解释:在这个例子中,存在满足条件的两个质数对。
这两个质数对分别是 [3,7] 和 [5,5],按照题面描述中的方式排序后返回。
示例 2:
输入:n = 2
输出:[]
解释:可以证明不存在和为 2 的质数对,所以返回一个空数组。
提示:
* 1 <= n <= 106
"""
class Solution:
def findPrimePairs(self, n: int) -> List[List[int]]:
| 给你一个整数 n 。如果两个整数 x 和 y 满足下述条件,则认为二者形成一个质数对:
* 1 <= x <= y <= n
* x + y == n
* x 和 y 都是质数
请你以二维有序列表的形式返回符合题目要求的所有 [xi, yi] ,列表需要按 xi 的 非递减顺序 排序。如果不存在符合要求的质数对,则返回一个空数组。
注意:质数是大于 1 的自然数,并且只有两个因子,即它本身和 1 。
示例 1:
输入:n = 10
输出:[[3,7],[5,5]]
解释:在这个例子中,存在满足条件的两个质数对。
这两个质数对分别是 [3,7] 和 [5,5],按照题面描述中的方式排序后返回。
示例 2:
输入:n = 2
输出:[]
解释:可以证明不存在和为 2 的质数对,所以返回一个空数组。
提示:
* 1 <= n <= 106
请完成下面的代码来解决上述问题:
```python
class Solution:
def findPrimePairs(self, n: int) -> List[List[int]]:
``` |
my_solution = Solution()
test_input = { "n": 10 }
assert my_solution.findPrimePairs(**test_input) == [[3,7],[5,5]]
test_input = { "n": 2 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 1 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 3 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 4 }
assert my_solution.findPrimePairs(**test_input) == [[2,2]]
test_input = { "n": 5 }
assert my_solution.findPrimePairs(**test_input) == [[2,3]]
test_input = { "n": 6 }
assert my_solution.findPrimePairs(**test_input) == [[3,3]]
test_input = { "n": 7 }
assert my_solution.findPrimePairs(**test_input) == [[2,5]]
test_input = { "n": 8 }
assert my_solution.findPrimePairs(**test_input) == [[3,5]]
test_input = { "n": 9 }
assert my_solution.findPrimePairs(**test_input) == [[2,7]]
test_input = { "n": 11 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 12 }
assert my_solution.findPrimePairs(**test_input) == [[5,7]]
test_input = { "n": 13 }
assert my_solution.findPrimePairs(**test_input) == [[2,11]]
test_input = { "n": 14 }
assert my_solution.findPrimePairs(**test_input) == [[3,11],[7,7]]
test_input = { "n": 15 }
assert my_solution.findPrimePairs(**test_input) == [[2,13]]
test_input = { "n": 16 }
assert my_solution.findPrimePairs(**test_input) == [[3,13],[5,11]]
test_input = { "n": 17 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 18 }
assert my_solution.findPrimePairs(**test_input) == [[5,13],[7,11]]
test_input = { "n": 19 }
assert my_solution.findPrimePairs(**test_input) == [[2,17]]
test_input = { "n": 20 }
assert my_solution.findPrimePairs(**test_input) == [[3,17],[7,13]]
test_input = { "n": 21 }
assert my_solution.findPrimePairs(**test_input) == [[2,19]]
test_input = { "n": 22 }
assert my_solution.findPrimePairs(**test_input) == [[3,19],[5,17],[11,11]]
test_input = { "n": 23 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 24 }
assert my_solution.findPrimePairs(**test_input) == [[5,19],[7,17],[11,13]]
test_input = { "n": 25 }
assert my_solution.findPrimePairs(**test_input) == [[2,23]]
test_input = { "n": 26 }
assert my_solution.findPrimePairs(**test_input) == [[3,23],[7,19],[13,13]]
test_input = { "n": 27 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 28 }
assert my_solution.findPrimePairs(**test_input) == [[5,23],[11,17]]
test_input = { "n": 29 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 30 }
assert my_solution.findPrimePairs(**test_input) == [[7,23],[11,19],[13,17]]
test_input = { "n": 31 }
assert my_solution.findPrimePairs(**test_input) == [[2,29]]
test_input = { "n": 32 }
assert my_solution.findPrimePairs(**test_input) == [[3,29],[13,19]]
test_input = { "n": 33 }
assert my_solution.findPrimePairs(**test_input) == [[2,31]]
test_input = { "n": 34 }
assert my_solution.findPrimePairs(**test_input) == [[3,31],[5,29],[11,23],[17,17]]
test_input = { "n": 35 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 36 }
assert my_solution.findPrimePairs(**test_input) == [[5,31],[7,29],[13,23],[17,19]]
test_input = { "n": 37 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 38 }
assert my_solution.findPrimePairs(**test_input) == [[7,31],[19,19]]
test_input = { "n": 39 }
assert my_solution.findPrimePairs(**test_input) == [[2,37]]
test_input = { "n": 40 }
assert my_solution.findPrimePairs(**test_input) == [[3,37],[11,29],[17,23]]
test_input = { "n": 41 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 42 }
assert my_solution.findPrimePairs(**test_input) == [[5,37],[11,31],[13,29],[19,23]]
test_input = { "n": 43 }
assert my_solution.findPrimePairs(**test_input) == [[2,41]]
test_input = { "n": 44 }
assert my_solution.findPrimePairs(**test_input) == [[3,41],[7,37],[13,31]]
test_input = { "n": 45 }
assert my_solution.findPrimePairs(**test_input) == [[2,43]]
test_input = { "n": 46 }
assert my_solution.findPrimePairs(**test_input) == [[3,43],[5,41],[17,29],[23,23]]
test_input = { "n": 47 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 48 }
assert my_solution.findPrimePairs(**test_input) == [[5,43],[7,41],[11,37],[17,31],[19,29]]
test_input = { "n": 49 }
assert my_solution.findPrimePairs(**test_input) == [[2,47]]
test_input = { "n": 50 }
assert my_solution.findPrimePairs(**test_input) == [[3,47],[7,43],[13,37],[19,31]]
test_input = { "n": 51 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 52 }
assert my_solution.findPrimePairs(**test_input) == [[5,47],[11,41],[23,29]]
test_input = { "n": 53 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 54 }
assert my_solution.findPrimePairs(**test_input) == [[7,47],[11,43],[13,41],[17,37],[23,31]]
test_input = { "n": 55 }
assert my_solution.findPrimePairs(**test_input) == [[2,53]]
test_input = { "n": 56 }
assert my_solution.findPrimePairs(**test_input) == [[3,53],[13,43],[19,37]]
test_input = { "n": 57 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 58 }
assert my_solution.findPrimePairs(**test_input) == [[5,53],[11,47],[17,41],[29,29]]
test_input = { "n": 59 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 60 }
assert my_solution.findPrimePairs(**test_input) == [[7,53],[13,47],[17,43],[19,41],[23,37],[29,31]]
test_input = { "n": 61 }
assert my_solution.findPrimePairs(**test_input) == [[2,59]]
test_input = { "n": 62 }
assert my_solution.findPrimePairs(**test_input) == [[3,59],[19,43],[31,31]]
test_input = { "n": 63 }
assert my_solution.findPrimePairs(**test_input) == [[2,61]]
test_input = { "n": 64 }
assert my_solution.findPrimePairs(**test_input) == [[3,61],[5,59],[11,53],[17,47],[23,41]]
test_input = { "n": 65 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 66 }
assert my_solution.findPrimePairs(**test_input) == [[5,61],[7,59],[13,53],[19,47],[23,43],[29,37]]
test_input = { "n": 67 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 68 }
assert my_solution.findPrimePairs(**test_input) == [[7,61],[31,37]]
test_input = { "n": 69 }
assert my_solution.findPrimePairs(**test_input) == [[2,67]]
test_input = { "n": 70 }
assert my_solution.findPrimePairs(**test_input) == [[3,67],[11,59],[17,53],[23,47],[29,41]]
test_input = { "n": 71 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 72 }
assert my_solution.findPrimePairs(**test_input) == [[5,67],[11,61],[13,59],[19,53],[29,43],[31,41]]
test_input = { "n": 73 }
assert my_solution.findPrimePairs(**test_input) == [[2,71]]
test_input = { "n": 74 }
assert my_solution.findPrimePairs(**test_input) == [[3,71],[7,67],[13,61],[31,43],[37,37]]
test_input = { "n": 75 }
assert my_solution.findPrimePairs(**test_input) == [[2,73]]
test_input = { "n": 76 }
assert my_solution.findPrimePairs(**test_input) == [[3,73],[5,71],[17,59],[23,53],[29,47]]
test_input = { "n": 77 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 78 }
assert my_solution.findPrimePairs(**test_input) == [[5,73],[7,71],[11,67],[17,61],[19,59],[31,47],[37,41]]
test_input = { "n": 79 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 80 }
assert my_solution.findPrimePairs(**test_input) == [[7,73],[13,67],[19,61],[37,43]]
test_input = { "n": 81 }
assert my_solution.findPrimePairs(**test_input) == [[2,79]]
test_input = { "n": 82 }
assert my_solution.findPrimePairs(**test_input) == [[3,79],[11,71],[23,59],[29,53],[41,41]]
test_input = { "n": 83 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 84 }
assert my_solution.findPrimePairs(**test_input) == [[5,79],[11,73],[13,71],[17,67],[23,61],[31,53],[37,47],[41,43]]
test_input = { "n": 85 }
assert my_solution.findPrimePairs(**test_input) == [[2,83]]
test_input = { "n": 86 }
assert my_solution.findPrimePairs(**test_input) == [[3,83],[7,79],[13,73],[19,67],[43,43]]
test_input = { "n": 87 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 88 }
assert my_solution.findPrimePairs(**test_input) == [[5,83],[17,71],[29,59],[41,47]]
test_input = { "n": 89 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 90 }
assert my_solution.findPrimePairs(**test_input) == [[7,83],[11,79],[17,73],[19,71],[23,67],[29,61],[31,59],[37,53],[43,47]]
test_input = { "n": 91 }
assert my_solution.findPrimePairs(**test_input) == [[2,89]]
test_input = { "n": 92 }
assert my_solution.findPrimePairs(**test_input) == [[3,89],[13,79],[19,73],[31,61]]
test_input = { "n": 93 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 94 }
assert my_solution.findPrimePairs(**test_input) == [[5,89],[11,83],[23,71],[41,53],[47,47]]
test_input = { "n": 95 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 96 }
assert my_solution.findPrimePairs(**test_input) == [[7,89],[13,83],[17,79],[23,73],[29,67],[37,59],[43,53]]
test_input = { "n": 97 }
assert my_solution.findPrimePairs(**test_input) == []
test_input = { "n": 98 }
assert my_solution.findPrimePairs(**test_input) == [[19,79],[31,67],[37,61]]
test_input = { "n": 99 }
assert my_solution.findPrimePairs(**test_input) == [[2,97]]
test_input = { "n": 100 }
assert my_solution.findPrimePairs(**test_input) == [[3,97],[11,89],[17,83],[29,71],[41,59],[47,53]] | 1,688,265,000 |
weekly-contest-352-continuous-subarrays | https://leetcode.com/problems/continuous-subarrays | continuous-subarrays | {
"questionId": "2868",
"questionFrontendId": "2762",
"title": "Continuous Subarrays",
"titleSlug": "continuous-subarrays",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 604,
"dislikes": 17,
"categoryTitle": "Algorithms"
} | """
给你一个下标从 0 开始的整数数组 nums 。nums 的一个子数组如果满足以下条件,那么它是 不间断 的:
* i,i + 1 ,...,j 表示子数组中的下标。对于所有满足 i <= i1, i2 <= j 的下标对,都有 0 <= |nums[i1] - nums[i2]| <= 2 。
请你返回 不间断 子数组的总数目。
子数组是一个数组中一段连续 非空 的元素序列。
示例 1:
输入:nums = [5,4,2,4]
输出:8
解释:
大小为 1 的不间断子数组:[5], [4], [2], [4] 。
大小为 2 的不间断子数组:[5,4], [4,2], [2,4] 。
大小为 3 的不间断子数组:[4,2,4] 。
没有大小为 4 的不间断子数组。
不间断子数组的总数目为 4 + 3 + 1 = 8 。
除了这些以外,没有别的不间断子数组。
示例 2:
输入:nums = [1,2,3]
输出:6
解释:
大小为 1 的不间断子数组:[1], [2], [3] 。
大小为 2 的不间断子数组:[1,2], [2,3] 。
大小为 3 的不间断子数组:[1,2,3] 。
不间断子数组的总数目为 3 + 2 + 1 = 6 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
"""
class Solution:
def continuousSubarrays(self, nums: List[int]) -> int:
| 给你一个下标从 0 开始的整数数组 nums 。nums 的一个子数组如果满足以下条件,那么它是 不间断 的:
* i,i + 1 ,...,j 表示子数组中的下标。对于所有满足 i <= i1, i2 <= j 的下标对,都有 0 <= |nums[i1] - nums[i2]| <= 2 。
请你返回 不间断 子数组的总数目。
子数组是一个数组中一段连续 非空 的元素序列。
示例 1:
输入:nums = [5,4,2,4]
输出:8
解释:
大小为 1 的不间断子数组:[5], [4], [2], [4] 。
大小为 2 的不间断子数组:[5,4], [4,2], [2,4] 。
大小为 3 的不间断子数组:[4,2,4] 。
没有大小为 4 的不间断子数组。
不间断子数组的总数目为 4 + 3 + 1 = 8 。
除了这些以外,没有别的不间断子数组。
示例 2:
输入:nums = [1,2,3]
输出:6
解释:
大小为 1 的不间断子数组:[1], [2], [3] 。
大小为 2 的不间断子数组:[1,2], [2,3] 。
大小为 3 的不间断子数组:[1,2,3] 。
不间断子数组的总数目为 3 + 2 + 1 = 6 。
提示:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
请完成下面的代码来解决上述问题:
```python
class Solution:
def continuousSubarrays(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [5,4,2,4] }
assert my_solution.continuousSubarrays(**test_input) == 8
test_input = { "nums": [1,2,3] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [31,30,31,32] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [65,66,67,66,66,65,64,65,65,64] }
assert my_solution.continuousSubarrays(**test_input) == 43
test_input = { "nums": [42,41,42,41,41,40,39,38] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [35,35,36,37,36,37,38,37,38] }
assert my_solution.continuousSubarrays(**test_input) == 39
test_input = { "nums": [43,44,43,44] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [14,15,15,15,16,16,16,16,15,16] }
assert my_solution.continuousSubarrays(**test_input) == 55
test_input = { "nums": [21] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [34,34,33,34,33,33,32,31,30,31] }
assert my_solution.continuousSubarrays(**test_input) == 39
test_input = { "nums": [58,59,59,58,59] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [10,9,8,7,8,9,9] }
assert my_solution.continuousSubarrays(**test_input) == 24
test_input = { "nums": [65,66,65,64,63,62,62] }
assert my_solution.continuousSubarrays(**test_input) == 20
test_input = { "nums": [65,65,64,65,66,65] }
assert my_solution.continuousSubarrays(**test_input) == 21
test_input = { "nums": [85,84,83,84,83,82] }
assert my_solution.continuousSubarrays(**test_input) == 20
test_input = { "nums": [60,59,60] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [96,97,98] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [21,22,23,22,23] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [76,77,77,78,77,78,78] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [27,27,27,26,26,27,27,27,27] }
assert my_solution.continuousSubarrays(**test_input) == 45
test_input = { "nums": [17] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [95] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [62] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [10,10,9,8,9,9,9,8,8] }
assert my_solution.continuousSubarrays(**test_input) == 45
test_input = { "nums": [21,22,22,23,24,24,23,24,23,24] }
assert my_solution.continuousSubarrays(**test_input) == 49
test_input = { "nums": [94,94,94,94,94,93,94] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [66,65,64,64,64,65,64,63] }
assert my_solution.continuousSubarrays(**test_input) == 35
test_input = { "nums": [35,35,36,36,35] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [35,34,33,34,35,35,34,35,34] }
assert my_solution.continuousSubarrays(**test_input) == 45
test_input = { "nums": [70,69,70,71,70,70,71,71] }
assert my_solution.continuousSubarrays(**test_input) == 36
test_input = { "nums": [49,49,49,50,50,49,50,51,51] }
assert my_solution.continuousSubarrays(**test_input) == 45
test_input = { "nums": [70,71,72,72,72] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [73,73,74,75,76,76,75,76] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [74,74,74,75,76,75,75,76,77,77] }
assert my_solution.continuousSubarrays(**test_input) == 49
test_input = { "nums": [21,21,20,19,20,20,21,21] }
assert my_solution.continuousSubarrays(**test_input) == 36
test_input = { "nums": [86,85] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [94,95,96,96,97,98,99,100,100] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [16,17,16] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [25,26,26,27,28,27,28,28,27,27] }
assert my_solution.continuousSubarrays(**test_input) == 49
test_input = { "nums": [54] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [94,95,96,95,94,93,92,91] }
assert my_solution.continuousSubarrays(**test_input) == 24
test_input = { "nums": [84,84,83] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [26,26] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [53,54,54,55] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [67,67,66,67,68,69,70,71,71] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [43,42,42,42,43] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [93,94,93] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [80,80] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [54,54,53,54,55,56,57,58,59] }
assert my_solution.continuousSubarrays(**test_input) == 27
test_input = { "nums": [52,51,50,49,48,49,48,47,46] }
assert my_solution.continuousSubarrays(**test_input) == 29
test_input = { "nums": [93,92,91,92,92,92,92] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [91,91,90,90,90,91,91,90] }
assert my_solution.continuousSubarrays(**test_input) == 36
test_input = { "nums": [37,37,37,36] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [40,39,39,39,39,40,40,41,40,39] }
assert my_solution.continuousSubarrays(**test_input) == 55
test_input = { "nums": [82,83,83,83,83,84] }
assert my_solution.continuousSubarrays(**test_input) == 21
test_input = { "nums": [35,36,36,37,37,37,36,37,36] }
assert my_solution.continuousSubarrays(**test_input) == 45
test_input = { "nums": [50,49,50,51,50] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [86,86,86,86,87] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [52] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [96,95,94,93] }
assert my_solution.continuousSubarrays(**test_input) == 9
test_input = { "nums": [56,56,55,55,54,55,54,54,53,52] }
assert my_solution.continuousSubarrays(**test_input) == 47
test_input = { "nums": [91] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [91,90,91,91] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [56,55,55,55] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [1,2,2,2,1,2,3,4,4,5] }
assert my_solution.continuousSubarrays(**test_input) == 39
test_input = { "nums": [84,85,84,84,85,85,85] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [71,71,71,71] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [47,47,46] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [65] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [20,20,19,18] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [22,23,23,22,22,22,22] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [92,92] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [93,92,92,91,90,90,89,88] }
assert my_solution.continuousSubarrays(**test_input) == 27
test_input = { "nums": [13,13,12,11] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [22,22,22,21,22,21] }
assert my_solution.continuousSubarrays(**test_input) == 21
test_input = { "nums": [24,25,26,26,25,26,25,25,26] }
assert my_solution.continuousSubarrays(**test_input) == 45
test_input = { "nums": [37,36] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [52,51] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [58] }
assert my_solution.continuousSubarrays(**test_input) == 1
test_input = { "nums": [88,88,88,87,87,87,87,88,88] }
assert my_solution.continuousSubarrays(**test_input) == 45
test_input = { "nums": [84,83,83,84,83,82,81] }
assert my_solution.continuousSubarrays(**test_input) == 24
test_input = { "nums": [87,88,88,87,87,88,88,89] }
assert my_solution.continuousSubarrays(**test_input) == 36
test_input = { "nums": [28,28,27] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [82,83,83,82] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [97,96,96,95,96,95] }
assert my_solution.continuousSubarrays(**test_input) == 21
test_input = { "nums": [72,73,73,74,73,73] }
assert my_solution.continuousSubarrays(**test_input) == 21
test_input = { "nums": [21,21,20] }
assert my_solution.continuousSubarrays(**test_input) == 6
test_input = { "nums": [38,38] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [24,24,24,23,22,22,22,23] }
assert my_solution.continuousSubarrays(**test_input) == 36
test_input = { "nums": [62,62] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [11,10,9,10,11] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [34,34,35,34,35,36,36] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [73,74,75,75] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [24,23,24,24] }
assert my_solution.continuousSubarrays(**test_input) == 10
test_input = { "nums": [21,22,22,23,22,23,22,23,23,22] }
assert my_solution.continuousSubarrays(**test_input) == 55
test_input = { "nums": [53,53,54,54,54] }
assert my_solution.continuousSubarrays(**test_input) == 15
test_input = { "nums": [94,95,96,96,97,97,98,99] }
assert my_solution.continuousSubarrays(**test_input) == 28
test_input = { "nums": [89,89,89,88,87,87] }
assert my_solution.continuousSubarrays(**test_input) == 21
test_input = { "nums": [89,90] }
assert my_solution.continuousSubarrays(**test_input) == 3
test_input = { "nums": [18,18] }
assert my_solution.continuousSubarrays(**test_input) == 3 | 1,688,265,000 |
weekly-contest-352-sum-of-imbalance-numbers-of-all-subarrays | https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays | sum-of-imbalance-numbers-of-all-subarrays | {
"questionId": "2849",
"questionFrontendId": "2763",
"title": "Sum of Imbalance Numbers of All Subarrays",
"titleSlug": "sum-of-imbalance-numbers-of-all-subarrays",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 278,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
一个长度为 n 下标从 0 开始的整数数组 arr 的 不平衡数字 定义为,在 sarr = sorted(arr) 数组中,满足以下条件的下标数目:
* 0 <= i < n - 1 ,和
* sarr[i+1] - sarr[i] > 1
这里,sorted(arr) 表示将数组 arr 排序后得到的数组。
给你一个下标从 0 开始的整数数组 nums ,请你返回它所有 子数组 的 不平衡数字 之和。
子数组指的是一个数组中连续一段 非空 的元素序列。
示例 1:
输入:nums = [2,3,1,4]
输出:3
解释:总共有 3 个子数组有非 0 不平衡数字:
- 子数组 [3, 1] ,不平衡数字为 1 。
- 子数组 [3, 1, 4] ,不平衡数字为 1 。
- 子数组 [1, 4] ,不平衡数字为 1 。
其他所有子数组的不平衡数字都是 0 ,所以所有子数组的不平衡数字之和为 3 。
示例 2:
输入:nums = [1,3,3,3,5]
输出:8
解释:总共有 7 个子数组有非 0 不平衡数字:
- 子数组 [1, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3, 3, 5] ,不平衡数字为 2 。
- 子数组 [3, 3, 3, 5] ,不平衡数字为 1 。
- 子数组 [3, 3, 5] ,不平衡数字为 1 。
- 子数组 [3, 5] ,不平衡数字为 1 。
其他所有子数组的不平衡数字都是 0 ,所以所有子数组的不平衡数字之和为 8 。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= nums.length
"""
class Solution:
def sumImbalanceNumbers(self, nums: List[int]) -> int:
| 一个长度为 n 下标从 0 开始的整数数组 arr 的 不平衡数字 定义为,在 sarr = sorted(arr) 数组中,满足以下条件的下标数目:
* 0 <= i < n - 1 ,和
* sarr[i+1] - sarr[i] > 1
这里,sorted(arr) 表示将数组 arr 排序后得到的数组。
给你一个下标从 0 开始的整数数组 nums ,请你返回它所有 子数组 的 不平衡数字 之和。
子数组指的是一个数组中连续一段 非空 的元素序列。
示例 1:
输入:nums = [2,3,1,4]
输出:3
解释:总共有 3 个子数组有非 0 不平衡数字:
- 子数组 [3, 1] ,不平衡数字为 1 。
- 子数组 [3, 1, 4] ,不平衡数字为 1 。
- 子数组 [1, 4] ,不平衡数字为 1 。
其他所有子数组的不平衡数字都是 0 ,所以所有子数组的不平衡数字之和为 3 。
示例 2:
输入:nums = [1,3,3,3,5]
输出:8
解释:总共有 7 个子数组有非 0 不平衡数字:
- 子数组 [1, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3, 3, 5] ,不平衡数字为 2 。
- 子数组 [3, 3, 3, 5] ,不平衡数字为 1 。
- 子数组 [3, 3, 5] ,不平衡数字为 1 。
- 子数组 [3, 5] ,不平衡数字为 1 。
其他所有子数组的不平衡数字都是 0 ,所以所有子数组的不平衡数字之和为 8 。
提示:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= nums.length
请完成下面的代码来解决上述问题:
```python
class Solution:
def sumImbalanceNumbers(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,3,1,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,3,3,3,5] }
assert my_solution.sumImbalanceNumbers(**test_input) == 8
test_input = { "nums": [1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [1,2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,3,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,3,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [1,3,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [2,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,1,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [2,2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,2,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,3,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [2,3,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,3,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [3,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [3,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [3,1,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [3,2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [3,2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [3,2,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [3,3,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [3,3,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [3,3,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,1,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,1,1,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,1,2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,2,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,1,2,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,1,3,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,1,3,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [1,1,3,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 4
test_input = { "nums": [1,1,3,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 4
test_input = { "nums": [1,1,4,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,1,4,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,1,4,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 4
test_input = { "nums": [1,1,4,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 4
test_input = { "nums": [1,2,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,1,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [1,2,1,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,2,2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,2,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,2,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,2,3,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [1,2,3,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,3,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,3,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [1,2,4,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,2,4,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,2,4,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [1,2,4,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 4
test_input = { "nums": [1,3,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,3,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,3,1,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 6
test_input = { "nums": [1,3,1,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 6
test_input = { "nums": [1,3,2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [1,3,2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [1,3,2,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 1
test_input = { "nums": [1,3,2,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [1,3,3,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,3,3,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [1,3,3,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,3,3,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,3,4,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,3,4,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,3,4,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,3,4,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,4,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,4,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,4,1,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 6
test_input = { "nums": [1,4,1,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 6
test_input = { "nums": [1,4,2,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,4,2,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,4,2,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,4,2,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 6
test_input = { "nums": [1,4,3,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,4,3,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 2
test_input = { "nums": [1,4,3,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,4,3,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,4,4,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,4,4,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 5
test_input = { "nums": [1,4,4,3] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [1,4,4,4] }
assert my_solution.sumImbalanceNumbers(**test_input) == 3
test_input = { "nums": [2,1,1,1] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0
test_input = { "nums": [2,1,1,2] }
assert my_solution.sumImbalanceNumbers(**test_input) == 0 | 1,688,265,000 |