task_id
stringlengths
33
83
url
stringlengths
44
94
title
stringlengths
14
64
meta
dict
prompt
stringlengths
718
3.82k
prompt_sft
stringlengths
780
3.89k
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" }
""" You are given an array nums of positive integers and an integer k. In one operation, you can remove the last element of the array and add it to your collection. Return the minimum number of operations needed to collect elements 1, 2, ..., k. Example 1: Input: nums = [3,1,5,4,2], k = 2 Output: 4 Explanation: After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4. Example 2: Input: nums = [3,1,5,4,2], k = 5 Output: 5 Explanation: After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5. Example 3: Input: nums = [3,2,5,3,1], k = 3 Output: 4 Explanation: After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4. Constraints: * 1 <= nums.length <= 50 * 1 <= nums[i] <= nums.length * 1 <= k <= nums.length * The input is generated such that you can collect elements 1, 2, ..., k. """ class Solution: def minOperations(self, nums: List[int], k: int) -> int:
You are given an array nums of positive integers and an integer k. In one operation, you can remove the last element of the array and add it to your collection. Return the minimum number of operations needed to collect elements 1, 2, ..., k. Example 1: Input: nums = [3,1,5,4,2], k = 2 Output: 4 Explanation: After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4. Example 2: Input: nums = [3,1,5,4,2], k = 5 Output: 5 Explanation: After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5. Example 3: Input: nums = [3,2,5,3,1], k = 3 Output: 4 Explanation: After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4. Constraints: * 1 <= nums.length <= 50 * 1 <= nums[i] <= nums.length * 1 <= k <= nums.length * The input is generated such that you can collect elements 1, 2, ..., k. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array nums consisting of positive integers. There are two types of operations that you can apply on the array any number of times: * Choose two elements with equal values and delete them from the array. * Choose three elements with equal values and delete them from the array. Return the minimum number of operations required to make the array empty, or -1 if it is not possible. Example 1: Input: nums = [2,3,3,2,2,4,2,3,4] Output: 4 Explanation: We can apply the following operations to make the array empty: - Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4]. - Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4]. - Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4]. - Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = []. It can be shown that we cannot make the array empty in less than 4 operations. Example 2: Input: nums = [2,1,2,2,3,3] Output: -1 Explanation: It is impossible to empty the array. Constraints: * 2 <= nums.length <= 105 * 1 <= nums[i] <= 106 """ class Solution: def minOperations(self, nums: List[int]) -> int:
You are given a 0-indexed array nums consisting of positive integers. There are two types of operations that you can apply on the array any number of times: * Choose two elements with equal values and delete them from the array. * Choose three elements with equal values and delete them from the array. Return the minimum number of operations required to make the array empty, or -1 if it is not possible. Example 1: Input: nums = [2,3,3,2,2,4,2,3,4] Output: 4 Explanation: We can apply the following operations to make the array empty: - Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4]. - Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4]. - Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4]. - Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = []. It can be shown that we cannot make the array empty in less than 4 operations. Example 2: Input: nums = [2,1,2,2,3,3] Output: -1 Explanation: It is impossible to empty the array. Constraints: * 2 <= nums.length <= 105 * 1 <= nums[i] <= 106 Please complete the code below to solve above prblem: ```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" }
""" You are given an array nums consisting of non-negative integers. We define the score of subarray nums[l..r] such that l <= r as nums[l] AND nums[l + 1] AND ... AND nums[r] where AND is the bitwise AND operation. Consider splitting the array into one or more subarrays such that the following conditions are satisfied: * Each element of the array belongs to exactly one subarray. * The sum of scores of the subarrays is the minimum possible. Return the maximum number of subarrays in a split that satisfies the conditions above. A subarray is a contiguous part of an array. Example 1: Input: nums = [1,0,2,0,1,2] Output: 3 Explanation: We can split the array into the following subarrays: - [1,0]. The score of this subarray is 1 AND 0 = 0. - [2,0]. The score of this subarray is 2 AND 0 = 0. - [1,2]. The score of this subarray is 1 AND 2 = 0. The sum of scores is 0 + 0 + 0 = 0, which is the minimum possible score that we can obtain. It can be shown that we cannot split the array into more than 3 subarrays with a total score of 0. So we return 3. Example 2: Input: nums = [5,7,1,3] Output: 1 Explanation: We can split the array into one subarray: [5,7,1,3] with a score of 1, which is the minimum possible score that we can obtain. It can be shown that we cannot split the array into more than 1 subarray with a total score of 1. So we return 1. Constraints: * 1 <= nums.length <= 105 * 0 <= nums[i] <= 106 """ class Solution: def maxSubarrays(self, nums: List[int]) -> int:
You are given an array nums consisting of non-negative integers. We define the score of subarray nums[l..r] such that l <= r as nums[l] AND nums[l + 1] AND ... AND nums[r] where AND is the bitwise AND operation. Consider splitting the array into one or more subarrays such that the following conditions are satisfied: * Each element of the array belongs to exactly one subarray. * The sum of scores of the subarrays is the minimum possible. Return the maximum number of subarrays in a split that satisfies the conditions above. A subarray is a contiguous part of an array. Example 1: Input: nums = [1,0,2,0,1,2] Output: 3 Explanation: We can split the array into the following subarrays: - [1,0]. The score of this subarray is 1 AND 0 = 0. - [2,0]. The score of this subarray is 2 AND 0 = 0. - [1,2]. The score of this subarray is 1 AND 2 = 0. The sum of scores is 0 + 0 + 0 = 0, which is the minimum possible score that we can obtain. It can be shown that we cannot split the array into more than 3 subarrays with a total score of 0. So we return 3. Example 2: Input: nums = [5,7,1,3] Output: 1 Explanation: We can split the array into one subarray: [5,7,1,3] with a score of 1, which is the minimum possible score that we can obtain. It can be shown that we cannot split the array into more than 1 subarray with a total score of 1. So we return 1. Constraints: * 1 <= nums.length <= 105 * 0 <= nums[i] <= 106 Please complete the code below to solve above prblem: ```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" }
""" There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given a 0-indexed integer array values of length n, where values[i] is the value associated with the ith node, and an integer k. A valid split of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by k, where the value of a connected component is the sum of the values of its nodes. Return the maximum number of components in any valid split. Example 1: [https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg] Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6 Output: 2 Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because: - The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12. - The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6. It can be shown that no other valid split has more than 2 connected components. Example 2: [https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg] 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 Output: 3 Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because: - The value of the component containing node 0 is values[0] = 3. - The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9. - The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6. It can be shown that no other valid split has more than 3 connected components. Constraints: * 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 * Sum of values is divisible by k. * The input is generated such that edges represents a valid tree. """ class Solution: def maxKDivisibleComponents(self, n: int, edges: List[List[int]], values: List[int], k: int) -> int:
There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given a 0-indexed integer array values of length n, where values[i] is the value associated with the ith node, and an integer k. A valid split of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by k, where the value of a connected component is the sum of the values of its nodes. Return the maximum number of components in any valid split. Example 1: [https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg] Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6 Output: 2 Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because: - The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12. - The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6. It can be shown that no other valid split has more than 2 connected components. Example 2: [https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg] 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 Output: 3 Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because: - The value of the component containing node 0 is values[0] = 3. - The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9. - The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6. It can be shown that no other valid split has more than 3 connected components. Constraints: * 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 * Sum of values is divisible by k. * The input is generated such that edges represents a valid tree. Please complete the code below to solve above prblem: ```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" }
""" You are given a binary string s that contains at least one '1'. You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination. Return a string representing the maximum odd binary number that can be created from the given combination. Note that the resulting string can have leading zeros. Example 1: Input: s = "010" Output: "001" Explanation: Because there is just one '1', it must be in the last position. So the answer is "001". Example 2: Input: s = "0101" Output: "1001" Explanation: One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001". Constraints: * 1 <= s.length <= 100 * s consists only of '0' and '1'. * s contains at least one '1'. """ class Solution: def maximumOddBinaryNumber(self, s: str) -> str:
You are given a binary string s that contains at least one '1'. You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination. Return a string representing the maximum odd binary number that can be created from the given combination. Note that the resulting string can have leading zeros. Example 1: Input: s = "010" Output: "001" Explanation: Because there is just one '1', it must be in the last position. So the answer is "001". Example 2: Input: s = "0101" Output: "1001" Explanation: One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001". Constraints: * 1 <= s.length <= 100 * s consists only of '0' and '1'. * s contains at least one '1'. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights is a mountain array. Array heights is a mountain if there exists an index i such that: * For all 0 < j <= i, heights[j - 1] <= heights[j] * For all i <= k < n - 1, heights[k + 1] <= heights[k] Return the maximum possible sum of heights of a beautiful configuration of towers. Example 1: Input: maxHeights = [5,3,4,1,1] Output: 13 Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 0. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13. Example 2: Input: maxHeights = [6,5,3,9,2,7] Output: 22 Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 3. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22. Example 3: Input: maxHeights = [3,2,5,5,2,3] Output: 18 Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 2. Note that, for this configuration, i = 3 can also be considered a peak. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18. Constraints: * 1 <= n == maxHeights <= 103 * 1 <= maxHeights[i] <= 109 """ class Solution: def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights is a mountain array. Array heights is a mountain if there exists an index i such that: * For all 0 < j <= i, heights[j - 1] <= heights[j] * For all i <= k < n - 1, heights[k + 1] <= heights[k] Return the maximum possible sum of heights of a beautiful configuration of towers. Example 1: Input: maxHeights = [5,3,4,1,1] Output: 13 Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 0. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13. Example 2: Input: maxHeights = [6,5,3,9,2,7] Output: 22 Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 3. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22. Example 3: Input: maxHeights = [3,2,5,5,2,3] Output: 18 Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 2. Note that, for this configuration, i = 3 can also be considered a peak. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18. Constraints: * 1 <= n == maxHeights <= 103 * 1 <= maxHeights[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights is a mountain array. Array heights is a mountain if there exists an index i such that: * For all 0 < j <= i, heights[j - 1] <= heights[j] * For all i <= k < n - 1, heights[k + 1] <= heights[k] Return the maximum possible sum of heights of a beautiful configuration of towers. Example 1: Input: maxHeights = [5,3,4,1,1] Output: 13 Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 0. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13. Example 2: Input: maxHeights = [6,5,3,9,2,7] Output: 22 Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 3. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22. Example 3: Input: maxHeights = [3,2,5,5,2,3] Output: 18 Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 2. Note that, for this configuration, i = 3 can also be considered a peak. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18. Constraints: * 1 <= n == maxHeights <= 105 * 1 <= maxHeights[i] <= 109 """ class Solution: def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights is a mountain array. Array heights is a mountain if there exists an index i such that: * For all 0 < j <= i, heights[j - 1] <= heights[j] * For all i <= k < n - 1, heights[k + 1] <= heights[k] Return the maximum possible sum of heights of a beautiful configuration of towers. Example 1: Input: maxHeights = [5,3,4,1,1] Output: 13 Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 0. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13. Example 2: Input: maxHeights = [6,5,3,9,2,7] Output: 22 Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 3. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22. Example 3: Input: maxHeights = [3,2,5,5,2,3] Output: 18 Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 2. Note that, for this configuration, i = 3 can also be considered a peak. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18. Constraints: * 1 <= n == maxHeights <= 105 * 1 <= maxHeights[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" There is an undirected tree with n nodes labeled from 1 to n. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. Return the number of valid paths in the tree. A path (a, b) is valid if there exists exactly one prime number among the node labels in the path from a to b. Note that: * The path (a, b) is a sequence of distinct nodes starting with node a and ending with node b such that every two adjacent nodes in the sequence share an edge in the tree. * Path (a, b) and path (b, a) are considered the same and counted only once. Example 1: [https://assets.leetcode.com/uploads/2023/08/27/example1.png] Input: n = 5, edges = [[1,2],[1,3],[2,4],[2,5]] Output: 4 Explanation: The pairs with exactly one prime number on the path between them are: - (1, 2) since the path from 1 to 2 contains prime number 2. - (1, 3) since the path from 1 to 3 contains prime number 3. - (1, 4) since the path from 1 to 4 contains prime number 2. - (2, 4) since the path from 2 to 4 contains prime number 2. It can be shown that there are only 4 valid paths. Example 2: [https://assets.leetcode.com/uploads/2023/08/27/example2.png] Input: n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]] Output: 6 Explanation: The pairs with exactly one prime number on the path between them are: - (1, 2) since the path from 1 to 2 contains prime number 2. - (1, 3) since the path from 1 to 3 contains prime number 3. - (1, 4) since the path from 1 to 4 contains prime number 2. - (1, 6) since the path from 1 to 6 contains prime number 3. - (2, 4) since the path from 2 to 4 contains prime number 2. - (3, 6) since the path from 3 to 6 contains prime number 3. It can be shown that there are only 6 valid paths. Constraints: * 1 <= n <= 105 * edges.length == n - 1 * edges[i].length == 2 * 1 <= ui, vi <= n * The input is generated such that edges represent a valid tree. """ class Solution: def countPaths(self, n: int, edges: List[List[int]]) -> int:
There is an undirected tree with n nodes labeled from 1 to n. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. Return the number of valid paths in the tree. A path (a, b) is valid if there exists exactly one prime number among the node labels in the path from a to b. Note that: * The path (a, b) is a sequence of distinct nodes starting with node a and ending with node b such that every two adjacent nodes in the sequence share an edge in the tree. * Path (a, b) and path (b, a) are considered the same and counted only once. Example 1: [https://assets.leetcode.com/uploads/2023/08/27/example1.png] Input: n = 5, edges = [[1,2],[1,3],[2,4],[2,5]] Output: 4 Explanation: The pairs with exactly one prime number on the path between them are: - (1, 2) since the path from 1 to 2 contains prime number 2. - (1, 3) since the path from 1 to 3 contains prime number 3. - (1, 4) since the path from 1 to 4 contains prime number 2. - (2, 4) since the path from 2 to 4 contains prime number 2. It can be shown that there are only 4 valid paths. Example 2: [https://assets.leetcode.com/uploads/2023/08/27/example2.png] Input: n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]] Output: 6 Explanation: The pairs with exactly one prime number on the path between them are: - (1, 2) since the path from 1 to 2 contains prime number 2. - (1, 3) since the path from 1 to 3 contains prime number 3. - (1, 4) since the path from 1 to 4 contains prime number 2. - (1, 6) since the path from 1 to 6 contains prime number 3. - (2, 4) since the path from 2 to 4 contains prime number 2. - (3, 6) since the path from 3 to 6 contains prime number 3. It can be shown that there are only 6 valid paths. Constraints: * 1 <= n <= 105 * edges.length == n - 1 * edges[i].length == 2 * 1 <= ui, vi <= n * The input is generated such that edges represent a valid tree. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums and an integer k. Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set bits in their binary representation. The set bits in an integer are the 1's present when it is written in binary. * For example, the binary representation of 21 is 10101, which has 3 set bits. Example 1: Input: nums = [5,10,1,5,2], k = 1 Output: 13 Explanation: The binary representation of the indices are: 0 = 0002 1 = 0012 2 = 0102 3 = 0112 4 = 1002 Indices 1, 2, and 4 have k = 1 set bits in their binary representation. Hence, the answer is nums[1] + nums[2] + nums[4] = 13. Example 2: Input: nums = [4,3,2,1], k = 2 Output: 1 Explanation: The binary representation of the indices are: 0 = 002 1 = 012 2 = 102 3 = 112 Only index 3 has k = 2 set bits in its binary representation. Hence, the answer is nums[3] = 1. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 105 * 0 <= k <= 10 """ class Solution: def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
You are given a 0-indexed integer array nums and an integer k. Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set bits in their binary representation. The set bits in an integer are the 1's present when it is written in binary. * For example, the binary representation of 21 is 10101, which has 3 set bits. Example 1: Input: nums = [5,10,1,5,2], k = 1 Output: 13 Explanation: The binary representation of the indices are: 0 = 0002 1 = 0012 2 = 0102 3 = 0112 4 = 1002 Indices 1, 2, and 4 have k = 1 set bits in their binary representation. Hence, the answer is nums[1] + nums[2] + nums[4] = 13. Example 2: Input: nums = [4,3,2,1], k = 2 Output: 1 Explanation: The binary representation of the indices are: 0 = 002 1 = 012 2 = 102 3 = 112 Only index 3 has k = 2 set bits in its binary representation. Hence, the answer is nums[3] = 1. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 105 * 0 <= k <= 10 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums of length n where n is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy. The ith student will become happy if one of these two conditions is met: * The student is selected and the total number of selected students is strictly greater than nums[i]. * The student is not selected and the total number of selected students is strictly less than nums[i]. Return the number of ways to select a group of students so that everyone remains happy. Example 1: Input: nums = [1,1] Output: 2 Explanation: The two possible ways are: The class teacher selects no student. The class teacher selects both students to form the group. If the class teacher selects just one student to form a group then the both students will not be happy. Therefore, there are only two possible ways. Example 2: Input: nums = [6,0,3,3,6,7,2,7] Output: 3 Explanation: The three possible ways are: The class teacher selects the student with index = 1 to form the group. The class teacher selects the students with index = 1, 2, 3, 6 to form the group. The class teacher selects all the students to form the group. Constraints: * 1 <= nums.length <= 105 * 0 <= nums[i] < nums.length """ class Solution: def countWays(self, nums: List[int]) -> int:
You are given a 0-indexed integer array nums of length n where n is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy. The ith student will become happy if one of these two conditions is met: * The student is selected and the total number of selected students is strictly greater than nums[i]. * The student is not selected and the total number of selected students is strictly less than nums[i]. Return the number of ways to select a group of students so that everyone remains happy. Example 1: Input: nums = [1,1] Output: 2 Explanation: The two possible ways are: The class teacher selects no student. The class teacher selects both students to form the group. If the class teacher selects just one student to form a group then the both students will not be happy. Therefore, there are only two possible ways. Example 2: Input: nums = [6,0,3,3,6,7,2,7] Output: 3 Explanation: The three possible ways are: The class teacher selects the student with index = 1 to form the group. The class teacher selects the students with index = 1, 2, 3, 6 to form the group. The class teacher selects all the students to form the group. Constraints: * 1 <= nums.length <= 105 * 0 <= nums[i] < nums.length Please complete the code below to solve above prblem: ```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" }
""" You are the owner of a company that creates alloys using various types of metals. There are n different types of metals available, and you have access to k machines that can be used to create alloys. Each machine requires a specific amount of each metal type to create an alloy. For the ith machine to create an alloy, it needs composition[i][j] units of metal of type j. Initially, you have stock[i] units of metal type i, and purchasing one unit of metal type i costs cost[i] coins. Given integers n, k, budget, a 1-indexed 2D array composition, and 1-indexed arrays stock and cost, your goal is to maximize the number of alloys the company can create while staying within the budget of budget coins. All alloys must be created with the same machine. Return the maximum number of alloys that the company can create. Example 1: Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,0], cost = [1,2,3] Output: 2 Explanation: It is optimal to use the 1st machine to create alloys. To create 2 alloys we need to buy the: - 2 units of metal of the 1st type. - 2 units of metal of the 2nd type. - 2 units of metal of the 3rd type. In total, we need 2 * 1 + 2 * 2 + 2 * 3 = 12 coins, which is smaller than or equal to budget = 15. Notice that we have 0 units of metal of each type and we have to buy all the required units of metal. It can be proven that we can create at most 2 alloys. Example 2: Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,100], cost = [1,2,3] Output: 5 Explanation: It is optimal to use the 2nd machine to create alloys. To create 5 alloys we need to buy: - 5 units of metal of the 1st type. - 5 units of metal of the 2nd type. - 0 units of metal of the 3rd type. In total, we need 5 * 1 + 5 * 2 + 0 * 3 = 15 coins, which is smaller than or equal to budget = 15. It can be proven that we can create at most 5 alloys. Example 3: Input: n = 2, k = 3, budget = 10, composition = [[2,1],[1,2],[1,1]], stock = [1,1], cost = [5,5] Output: 2 Explanation: It is optimal to use the 3rd machine to create alloys. To create 2 alloys we need to buy the: - 1 unit of metal of the 1st type. - 1 unit of metal of the 2nd type. In total, we need 1 * 5 + 1 * 5 = 10 coins, which is smaller than or equal to budget = 10. It can be proven that we can create at most 2 alloys. Constraints: * 1 <= n, k <= 100 * 0 <= budget <= 108 * composition.length == k * composition[i].length == n * 1 <= composition[i][j] <= 100 * stock.length == cost.length == n * 0 <= stock[i] <= 108 * 1 <= cost[i] <= 100 """ class Solution: def maxNumberOfAlloys(self, n: int, k: int, budget: int, composition: List[List[int]], stock: List[int], cost: List[int]) -> int:
You are the owner of a company that creates alloys using various types of metals. There are n different types of metals available, and you have access to k machines that can be used to create alloys. Each machine requires a specific amount of each metal type to create an alloy. For the ith machine to create an alloy, it needs composition[i][j] units of metal of type j. Initially, you have stock[i] units of metal type i, and purchasing one unit of metal type i costs cost[i] coins. Given integers n, k, budget, a 1-indexed 2D array composition, and 1-indexed arrays stock and cost, your goal is to maximize the number of alloys the company can create while staying within the budget of budget coins. All alloys must be created with the same machine. Return the maximum number of alloys that the company can create. Example 1: Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,0], cost = [1,2,3] Output: 2 Explanation: It is optimal to use the 1st machine to create alloys. To create 2 alloys we need to buy the: - 2 units of metal of the 1st type. - 2 units of metal of the 2nd type. - 2 units of metal of the 3rd type. In total, we need 2 * 1 + 2 * 2 + 2 * 3 = 12 coins, which is smaller than or equal to budget = 15. Notice that we have 0 units of metal of each type and we have to buy all the required units of metal. It can be proven that we can create at most 2 alloys. Example 2: Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,100], cost = [1,2,3] Output: 5 Explanation: It is optimal to use the 2nd machine to create alloys. To create 5 alloys we need to buy: - 5 units of metal of the 1st type. - 5 units of metal of the 2nd type. - 0 units of metal of the 3rd type. In total, we need 5 * 1 + 5 * 2 + 0 * 3 = 15 coins, which is smaller than or equal to budget = 15. It can be proven that we can create at most 5 alloys. Example 3: Input: n = 2, k = 3, budget = 10, composition = [[2,1],[1,2],[1,1]], stock = [1,1], cost = [5,5] Output: 2 Explanation: It is optimal to use the 3rd machine to create alloys. To create 2 alloys we need to buy the: - 1 unit of metal of the 1st type. - 1 unit of metal of the 2nd type. In total, we need 1 * 5 + 1 * 5 = 10 coins, which is smaller than or equal to budget = 10. It can be proven that we can create at most 2 alloys. Constraints: * 1 <= n, k <= 100 * 0 <= budget <= 108 * composition.length == k * composition[i].length == n * 1 <= composition[i][j] <= 100 * stock.length == cost.length == n * 0 <= stock[i] <= 108 * 1 <= cost[i] <= 100 Please complete the code below to solve above prblem: ```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" }
""" You are given a 1-indexed array nums of n integers. A set of numbers is complete if the product of every pair of its elements is a perfect square. For a subset of the indices set {1, 2, ..., n} represented as {i1, i2, ..., ik}, we define its element-sum as: nums[i1] + nums[i2] + ... + nums[ik]. Return the maximum element-sum of a complete subset of the indices set {1, 2, ..., n}. A perfect square is a number that can be expressed as the product of an integer by itself. Example 1: Input: nums = [8,7,3,5,7,2,4,9] Output: 16 Explanation: Apart from the subsets consisting of a single index, there are two other complete subsets of indices: {1,4} and {2,8}. The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 8 + 5 = 13. The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 7 + 9 = 16. Hence, the maximum element-sum of a complete subset of indices is 16. Example 2: Input: nums = [5,10,3,10,1,13,7,9,4] Output: 19 Explanation: Apart from the subsets consisting of a single index, there are four other complete subsets of indices: {1,4}, {1,9}, {2,8}, {4,9}, and {1,4,9}. The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 5 + 10 = 15. The sum of the elements corresponding to indices 1 and 9 is equal to nums[1] + nums[9] = 5 + 4 = 9. The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 10 + 9 = 19. The sum of the elements corresponding to indices 4 and 9 is equal to nums[4] + nums[9] = 10 + 4 = 14. The sum of the elements corresponding to indices 1, 4, and 9 is equal to nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19. Hence, the maximum element-sum of a complete subset of indices is 19. Constraints: * 1 <= n == nums.length <= 104 * 1 <= nums[i] <= 109 """ class Solution: def maximumSum(self, nums: List[int]) -> int:
You are given a 1-indexed array nums of n integers. A set of numbers is complete if the product of every pair of its elements is a perfect square. For a subset of the indices set {1, 2, ..., n} represented as {i1, i2, ..., ik}, we define its element-sum as: nums[i1] + nums[i2] + ... + nums[ik]. Return the maximum element-sum of a complete subset of the indices set {1, 2, ..., n}. A perfect square is a number that can be expressed as the product of an integer by itself. Example 1: Input: nums = [8,7,3,5,7,2,4,9] Output: 16 Explanation: Apart from the subsets consisting of a single index, there are two other complete subsets of indices: {1,4} and {2,8}. The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 8 + 5 = 13. The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 7 + 9 = 16. Hence, the maximum element-sum of a complete subset of indices is 16. Example 2: Input: nums = [5,10,3,10,1,13,7,9,4] Output: 19 Explanation: Apart from the subsets consisting of a single index, there are four other complete subsets of indices: {1,4}, {1,9}, {2,8}, {4,9}, and {1,4,9}. The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 5 + 10 = 15. The sum of the elements corresponding to indices 1 and 9 is equal to nums[1] + nums[9] = 5 + 4 = 9. The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 10 + 9 = 19. The sum of the elements corresponding to indices 4 and 9 is equal to nums[4] + nums[9] = 10 + 4 = 14. The sum of the elements corresponding to indices 1, 4, and 9 is equal to nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19. Hence, the maximum element-sum of a complete subset of indices is 19. Constraints: * 1 <= n == nums.length <= 104 * 1 <= nums[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array nums of length n containing distinct positive integers. Return the minimum number of right shifts required to sort nums and -1 if this is not possible. A right shift is defined as shifting the element at index i to index (i + 1) % n, for all indices. Example 1: Input: nums = [3,4,5,1,2] Output: 2 Explanation: After the first right shift, nums = [2,3,4,5,1]. After the second right shift, nums = [1,2,3,4,5]. Now nums is sorted; therefore the answer is 2. Example 2: Input: nums = [1,3,5] Output: 0 Explanation: nums is already sorted therefore, the answer is 0. Example 3: Input: nums = [2,1,4] Output: -1 Explanation: It's impossible to sort the array using right shifts. Constraints: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 100 * nums contains distinct integers. """ class Solution: def minimumRightShifts(self, nums: List[int]) -> int:
You are given a 0-indexed array nums of length n containing distinct positive integers. Return the minimum number of right shifts required to sort nums and -1 if this is not possible. A right shift is defined as shifting the element at index i to index (i + 1) % n, for all indices. Example 1: Input: nums = [3,4,5,1,2] Output: 2 Explanation: After the first right shift, nums = [2,3,4,5,1]. After the second right shift, nums = [1,2,3,4,5]. Now nums is sorted; therefore the answer is 2. Example 2: Input: nums = [1,3,5] Output: 0 Explanation: nums is already sorted therefore, the answer is 0. Example 3: Input: nums = [2,1,4] Output: -1 Explanation: It's impossible to sort the array using right shifts. Constraints: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 100 * nums contains distinct integers. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed sorted array of integers nums. You can perform the following operation any number of times: * Choose two indices, i and j, where i < j, such that nums[i] < nums[j]. * Then, remove the elements at indices i and j from nums. The remaining elements retain their original order, and the array is re-indexed. Return an integer that denotes the minimum length of nums after performing the operation any number of times (including zero). Note that nums is sorted in non-decreasing order. Example 1: Input: nums = [1,3,4,9] Output: 0 Explanation: Initially, nums = [1, 3, 4, 9]. In the first operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 1 < 3. Remove indices 0 and 1, and nums becomes [4, 9]. For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 4 < 9. Remove indices 0 and 1, and nums becomes an empty array []. Hence, the minimum length achievable is 0. Example 2: Input: nums = [2,3,6,9] Output: 0 Explanation: Initially, nums = [2, 3, 6, 9]. In the first operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 2 < 6. Remove indices 0 and 2, and nums becomes [3, 9]. For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 3 < 9. Remove indices 0 and 1, and nums becomes an empty array []. Hence, the minimum length achievable is 0. Example 3: Input: nums = [1,1,2] Output: 1 Explanation: Initially, nums = [1, 1, 2]. In an operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 1 < 2. Remove indices 0 and 2, and nums becomes [1]. It is no longer possible to perform an operation on the array. Hence, the minimum achievable length is 1. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 * nums is sorted in non-decreasing order. """ class Solution: def minLengthAfterRemovals(self, nums: List[int]) -> int:
You are given a 0-indexed sorted array of integers nums. You can perform the following operation any number of times: * Choose two indices, i and j, where i < j, such that nums[i] < nums[j]. * Then, remove the elements at indices i and j from nums. The remaining elements retain their original order, and the array is re-indexed. Return an integer that denotes the minimum length of nums after performing the operation any number of times (including zero). Note that nums is sorted in non-decreasing order. Example 1: Input: nums = [1,3,4,9] Output: 0 Explanation: Initially, nums = [1, 3, 4, 9]. In the first operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 1 < 3. Remove indices 0 and 1, and nums becomes [4, 9]. For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 4 < 9. Remove indices 0 and 1, and nums becomes an empty array []. Hence, the minimum length achievable is 0. Example 2: Input: nums = [2,3,6,9] Output: 0 Explanation: Initially, nums = [2, 3, 6, 9]. In the first operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 2 < 6. Remove indices 0 and 2, and nums becomes [3, 9]. For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 3 < 9. Remove indices 0 and 1, and nums becomes an empty array []. Hence, the minimum length achievable is 0. Example 3: Input: nums = [1,1,2] Output: 1 Explanation: Initially, nums = [1, 1, 2]. In an operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 1 < 2. Remove indices 0 and 2, and nums becomes [1]. It is no longer possible to perform an operation on the array. Hence, the minimum achievable length is 1. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 * nums is sorted in non-decreasing order. Please complete the code below to solve above prblem: ```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" }
""" You are given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the ith point in a 2D plane. We define the distance between two points (x1, y1) and (x2, y2) as (x1 XOR x2) + (y1 XOR y2) where XOR is the bitwise XOR operation. Return the number of pairs (i, j) such that i < j and the distance between points i and j is equal to k. Example 1: Input: coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5 Output: 2 Explanation: We can choose the following pairs: - (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5. - (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5. Example 2: Input: coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0 Output: 10 Explanation: Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs. Constraints: * 2 <= coordinates.length <= 50000 * 0 <= xi, yi <= 106 * 0 <= k <= 100 """ class Solution: def countPairs(self, coordinates: List[List[int]], k: int) -> int:
You are given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the ith point in a 2D plane. We define the distance between two points (x1, y1) and (x2, y2) as (x1 XOR x2) + (y1 XOR y2) where XOR is the bitwise XOR operation. Return the number of pairs (i, j) such that i < j and the distance between points i and j is equal to k. Example 1: Input: coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5 Output: 2 Explanation: We can choose the following pairs: - (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5. - (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5. Example 2: Input: coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0 Output: 10 Explanation: Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs. Constraints: * 2 <= coordinates.length <= 50000 * 0 <= xi, yi <= 106 * 0 <= k <= 100 Please complete the code below to solve above prblem: ```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" }
""" There is a simple directed graph with n nodes labeled from 0 to n - 1. The graph would form a tree if its edges were bi-directional. You are given an integer n and a 2D integer array edges, where edges[i] = [ui, vi] represents a directed edge going from node ui to node vi. An edge reversal changes the direction of an edge, i.e., a directed edge going from node ui to node vi becomes a directed edge going from node vi to node ui. For every node i in the range [0, n - 1], your task is to independently calculate the minimum number of edge reversals required so it is possible to reach any other node starting from node i through a sequence of directed edges. Return an integer array answer, where answer[i] is the minimum number of edge reversals required so it is possible to reach any other node starting from node i through a sequence of directed edges. Example 1: [https://assets.leetcode.com/uploads/2023/08/26/image-20230826221104-3.png] Input: n = 4, edges = [[2,0],[2,1],[1,3]] Output: [1,1,0,2] Explanation: The image above shows the graph formed by the edges. For node 0: after reversing the edge [2,0], it is possible to reach any other node starting from node 0. So, answer[0] = 1. For node 1: after reversing the edge [2,1], it is possible to reach any other node starting from node 1. So, answer[1] = 1. For node 2: it is already possible to reach any other node starting from node 2. So, answer[2] = 0. For node 3: after reversing the edges [1,3] and [2,1], it is possible to reach any other node starting from node 3. So, answer[3] = 2. Example 2: [https://assets.leetcode.com/uploads/2023/08/26/image-20230826225541-2.png] Input: n = 3, edges = [[1,2],[2,0]] Output: [2,0,1] Explanation: The image above shows the graph formed by the edges. For node 0: after reversing the edges [2,0] and [1,2], it is possible to reach any other node starting from node 0. So, answer[0] = 2. For node 1: it is already possible to reach any other node starting from node 1. So, answer[1] = 0. For node 2: after reversing the edge [1, 2], it is possible to reach any other node starting from node 2. So, answer[2] = 1. Constraints: * 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 * The input is generated such that if the edges were bi-directional, the graph would be a tree. """ class Solution: def minEdgeReversals(self, n: int, edges: List[List[int]]) -> List[int]:
There is a simple directed graph with n nodes labeled from 0 to n - 1. The graph would form a tree if its edges were bi-directional. You are given an integer n and a 2D integer array edges, where edges[i] = [ui, vi] represents a directed edge going from node ui to node vi. An edge reversal changes the direction of an edge, i.e., a directed edge going from node ui to node vi becomes a directed edge going from node vi to node ui. For every node i in the range [0, n - 1], your task is to independently calculate the minimum number of edge reversals required so it is possible to reach any other node starting from node i through a sequence of directed edges. Return an integer array answer, where answer[i] is the minimum number of edge reversals required so it is possible to reach any other node starting from node i through a sequence of directed edges. Example 1: [https://assets.leetcode.com/uploads/2023/08/26/image-20230826221104-3.png] Input: n = 4, edges = [[2,0],[2,1],[1,3]] Output: [1,1,0,2] Explanation: The image above shows the graph formed by the edges. For node 0: after reversing the edge [2,0], it is possible to reach any other node starting from node 0. So, answer[0] = 1. For node 1: after reversing the edge [2,1], it is possible to reach any other node starting from node 1. So, answer[1] = 1. For node 2: it is already possible to reach any other node starting from node 2. So, answer[2] = 0. For node 3: after reversing the edges [1,3] and [2,1], it is possible to reach any other node starting from node 3. So, answer[3] = 2. Example 2: [https://assets.leetcode.com/uploads/2023/08/26/image-20230826225541-2.png] Input: n = 3, edges = [[1,2],[2,0]] Output: [2,0,1] Explanation: The image above shows the graph formed by the edges. For node 0: after reversing the edges [2,0] and [1,2], it is possible to reach any other node starting from node 0. So, answer[0] = 2. For node 1: it is already possible to reach any other node starting from node 1. So, answer[1] = 0. For node 2: after reversing the edge [1, 2], it is possible to reach any other node starting from node 2. So, answer[2] = 1. Constraints: * 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 * The input is generated such that if the edges were bi-directional, the graph would be a tree. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed 2D integer array nums representing the coordinates of the cars parking on a number line. For any index i, nums[i] = [starti, endi] where starti is the starting point of the ith car and endi is the ending point of the ith car. Return the number of integer points on the line that are covered with any part of a car. Example 1: Input: nums = [[3,6],[1,5],[4,7]] Output: 7 Explanation: All the points from 1 to 7 intersect at least one car, therefore the answer would be 7. Example 2: Input: nums = [[1,3],[5,8]] Output: 7 Explanation: Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7. Constraints: * 1 <= nums.length <= 100 * nums[i].length == 2 * 1 <= starti <= endi <= 100 """ class Solution: def numberOfPoints(self, nums: List[List[int]]) -> int:
You are given a 0-indexed 2D integer array nums representing the coordinates of the cars parking on a number line. For any index i, nums[i] = [starti, endi] where starti is the starting point of the ith car and endi is the ending point of the ith car. Return the number of integer points on the line that are covered with any part of a car. Example 1: Input: nums = [[3,6],[1,5],[4,7]] Output: 7 Explanation: All the points from 1 to 7 intersect at least one car, therefore the answer would be 7. Example 2: Input: nums = [[1,3],[5,8]] Output: 7 Explanation: Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7. Constraints: * 1 <= nums.length <= 100 * nums[i].length == 2 * 1 <= starti <= endi <= 100 Please complete the code below to solve above prblem: ```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" }
""" You are given four integers sx, sy, fx, fy, and a non-negative integer t. In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells. Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise. A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times. Example 1: [https://assets.leetcode.com/uploads/2023/08/05/example2.svg] Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6 Output: true Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. Example 2: [https://assets.leetcode.com/uploads/2023/08/05/example1.svg] Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3 Output: false Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second. Constraints: * 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:
You are given four integers sx, sy, fx, fy, and a non-negative integer t. In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells. Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise. A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times. Example 1: [https://assets.leetcode.com/uploads/2023/08/05/example2.svg] Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6 Output: true Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. Example 2: [https://assets.leetcode.com/uploads/2023/08/05/example1.svg] Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3 Output: false Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second. Constraints: * 1 <= sx, sy, fx, fy <= 109 * 0 <= t <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed 2D integer matrix grid of size 3 * 3, representing the number of stones in each cell. The grid contains exactly 9 stones, and there can be multiple stones in a single cell. In one move, you can move a single stone from its current cell to any other cell if the two cells share a side. Return the minimum number of moves required to place one stone in each cell. Example 1: [https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg] Input: grid = [[1,1,0],[1,1,1],[1,2,1]] Output: 3 Explanation: One possible sequence of moves to place one stone in each cell is: 1- Move one stone from cell (2,1) to cell (2,2). 2- Move one stone from cell (2,2) to cell (1,2). 3- Move one stone from cell (1,2) to cell (0,2). In total, it takes 3 moves to place one stone in each cell of the grid. It can be shown that 3 is the minimum number of moves required to place one stone in each cell. Example 2: [https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg] Input: grid = [[1,3,0],[1,0,0],[1,0,3]] Output: 4 Explanation: One possible sequence of moves to place one stone in each cell is: 1- Move one stone from cell (0,1) to cell (0,2). 2- Move one stone from cell (0,1) to cell (1,1). 3- Move one stone from cell (2,2) to cell (1,2). 4- Move one stone from cell (2,2) to cell (2,1). In total, it takes 4 moves to place one stone in each cell of the grid. It can be shown that 4 is the minimum number of moves required to place one stone in each cell. Constraints: * grid.length == grid[i].length == 3 * 0 <= grid[i][j] <= 9 * Sum of grid is equal to 9. """ class Solution: def minimumMoves(self, grid: List[List[int]]) -> int:
You are given a 0-indexed 2D integer matrix grid of size 3 * 3, representing the number of stones in each cell. The grid contains exactly 9 stones, and there can be multiple stones in a single cell. In one move, you can move a single stone from its current cell to any other cell if the two cells share a side. Return the minimum number of moves required to place one stone in each cell. Example 1: [https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg] Input: grid = [[1,1,0],[1,1,1],[1,2,1]] Output: 3 Explanation: One possible sequence of moves to place one stone in each cell is: 1- Move one stone from cell (2,1) to cell (2,2). 2- Move one stone from cell (2,2) to cell (1,2). 3- Move one stone from cell (1,2) to cell (0,2). In total, it takes 3 moves to place one stone in each cell of the grid. It can be shown that 3 is the minimum number of moves required to place one stone in each cell. Example 2: [https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg] Input: grid = [[1,3,0],[1,0,0],[1,0,3]] Output: 4 Explanation: One possible sequence of moves to place one stone in each cell is: 1- Move one stone from cell (0,1) to cell (0,2). 2- Move one stone from cell (0,1) to cell (1,1). 3- Move one stone from cell (2,2) to cell (1,2). 4- Move one stone from cell (2,2) to cell (2,1). In total, it takes 4 moves to place one stone in each cell of the grid. It can be shown that 4 is the minimum number of moves required to place one stone in each cell. Constraints: * grid.length == grid[i].length == 3 * 0 <= grid[i][j] <= 9 * Sum of grid is equal to 9. Please complete the code below to solve above prblem: ```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" }
""" You are given two strings s and t of equal length n. You can perform the following operation on the string s: * Remove a suffix of s of length l where 0 < l < n and append it at the start of s. For example, let s = 'abcd' then in one operation you can remove the suffix 'cd' and append it in front of s making s = 'cdab'. You are also given an integer k. Return the number of ways in which s can be transformed into t in exactly k operations. Since the answer can be large, return it modulo 109 + 7. Example 1: Input: s = "abcd", t = "cdab", k = 2 Output: 2 Explanation: First way: In first operation, choose suffix from index = 3, so resulting s = "dabc". In second operation, choose suffix from index = 3, so resulting s = "cdab". Second way: In first operation, choose suffix from index = 1, so resulting s = "bcda". In second operation, choose suffix from index = 1, so resulting s = "cdab". Example 2: Input: s = "ababab", t = "ababab", k = 1 Output: 2 Explanation: First way: Choose suffix from index = 2, so resulting s = "ababab". Second way: Choose suffix from index = 4, so resulting s = "ababab". Constraints: * 2 <= s.length <= 5 * 105 * 1 <= k <= 1015 * s.length == t.length * s and t consist of only lowercase English alphabets. """ class Solution: def numberOfWays(self, s: str, t: str, k: int) -> int:
You are given two strings s and t of equal length n. You can perform the following operation on the string s: * Remove a suffix of s of length l where 0 < l < n and append it at the start of s. For example, let s = 'abcd' then in one operation you can remove the suffix 'cd' and append it in front of s making s = 'cdab'. You are also given an integer k. Return the number of ways in which s can be transformed into t in exactly k operations. Since the answer can be large, return it modulo 109 + 7. Example 1: Input: s = "abcd", t = "cdab", k = 2 Output: 2 Explanation: First way: In first operation, choose suffix from index = 3, so resulting s = "dabc". In second operation, choose suffix from index = 3, so resulting s = "cdab". Second way: In first operation, choose suffix from index = 1, so resulting s = "bcda". In second operation, choose suffix from index = 1, so resulting s = "cdab". Example 2: Input: s = "ababab", t = "ababab", k = 1 Output: 2 Explanation: First way: Choose suffix from index = 2, so resulting s = "ababab". Second way: Choose suffix from index = 4, so resulting s = "ababab". Constraints: * 2 <= s.length <= 5 * 105 * 1 <= k <= 1015 * s.length == t.length * s and t consist of only lowercase English alphabets. Please complete the code below to solve above prblem: ```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" }
""" You are given two positive integers low and high. An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric. Return the number of symmetric integers in the range [low, high]. Example 1: Input: low = 1, high = 100 Output: 9 Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99. Example 2: Input: low = 1200, high = 1230 Output: 4 Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230. Constraints: * 1 <= low <= high <= 104 """ class Solution: def countSymmetricIntegers(self, low: int, high: int) -> int:
You are given two positive integers low and high. An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric. Return the number of symmetric integers in the range [low, high]. Example 1: Input: low = 1, high = 100 Output: 9 Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99. Example 2: Input: low = 1200, high = 1230 Output: 4 Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230. Constraints: * 1 <= low <= high <= 104 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed string num representing a non-negative integer. In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0. Return the minimum number of operations required to make num special. An integer x is considered special if it is divisible by 25. Example 1: Input: num = "2245047" Output: 2 Explanation: Delete digits num[5] and num[6]. The resulting number is "22450" which is special since it is divisible by 25. It can be shown that 2 is the minimum number of operations required to get a special number. Example 2: Input: num = "2908305" Output: 3 Explanation: Delete digits num[3], num[4], and num[6]. The resulting number is "2900" which is special since it is divisible by 25. It can be shown that 3 is the minimum number of operations required to get a special number. Example 3: Input: num = "10" Output: 1 Explanation: Delete digit num[0]. The resulting number is "0" which is special since it is divisible by 25. It can be shown that 1 is the minimum number of operations required to get a special number. Constraints: * 1 <= num.length <= 100 * num only consists of digits '0' through '9'. * num does not contain any leading zeros. """ class Solution: def minimumOperations(self, num: str) -> int:
You are given a 0-indexed string num representing a non-negative integer. In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0. Return the minimum number of operations required to make num special. An integer x is considered special if it is divisible by 25. Example 1: Input: num = "2245047" Output: 2 Explanation: Delete digits num[5] and num[6]. The resulting number is "22450" which is special since it is divisible by 25. It can be shown that 2 is the minimum number of operations required to get a special number. Example 2: Input: num = "2908305" Output: 3 Explanation: Delete digits num[3], num[4], and num[6]. The resulting number is "2900" which is special since it is divisible by 25. It can be shown that 3 is the minimum number of operations required to get a special number. Example 3: Input: num = "10" Output: 1 Explanation: Delete digit num[0]. The resulting number is "0" which is special since it is divisible by 25. It can be shown that 1 is the minimum number of operations required to get a special number. Constraints: * 1 <= num.length <= 100 * num only consists of digits '0' through '9'. * num does not contain any leading zeros. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums, an integer modulo, and an integer k. Your task is to find the count of subarrays that are interesting. A subarray nums[l..r] is interesting if the following condition holds: * Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k. Return an integer denoting the count of interesting subarrays. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [3,2,4], modulo = 2, k = 1 Output: 3 Explanation: In this example the interesting subarrays are: The subarray nums[0..0] which is [3]. - There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..1] which is [3,2]. - There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..2] which is [3,2,4]. - There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. It can be shown that there are no other interesting subarrays. So, the answer is 3. Example 2: Input: nums = [3,1,9,6], modulo = 3, k = 0 Output: 2 Explanation: In this example the interesting subarrays are: The subarray nums[0..3] which is [3,1,9,6]. - There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k. - Hence, cnt = 3 and cnt % modulo == k. The subarray nums[1..1] which is [1]. - There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k. - Hence, cnt = 0 and cnt % modulo == k. It can be shown that there are no other interesting subarrays. So, the answer is 2. Constraints: * 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:
You are given a 0-indexed integer array nums, an integer modulo, and an integer k. Your task is to find the count of subarrays that are interesting. A subarray nums[l..r] is interesting if the following condition holds: * Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k. Return an integer denoting the count of interesting subarrays. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [3,2,4], modulo = 2, k = 1 Output: 3 Explanation: In this example the interesting subarrays are: The subarray nums[0..0] which is [3]. - There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..1] which is [3,2]. - There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..2] which is [3,2,4]. - There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k. - Hence, cnt = 1 and cnt % modulo == k. It can be shown that there are no other interesting subarrays. So, the answer is 3. Example 2: Input: nums = [3,1,9,6], modulo = 3, k = 0 Output: 2 Explanation: In this example the interesting subarrays are: The subarray nums[0..3] which is [3,1,9,6]. - There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k. - Hence, cnt = 3 and cnt % modulo == k. The subarray nums[1..1] which is [1]. - There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k. - Hence, cnt = 0 and cnt % modulo == k. It can be shown that there are no other interesting subarrays. So, the answer is 2. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 * 1 <= modulo <= 109 * 0 <= k < modulo Please complete the code below to solve above prblem: ```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" }
""" There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree. You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, find the minimum number of operations required to make the weight of every edge on the path from ai to bi equal. In one operation, you can choose any edge of the tree and change its weight to any value. Note that: * Queries are independent of each other, meaning that the tree returns to its initial state on each new query. * The path from ai to bi is a sequence of distinct nodes starting with node ai and ending with node bi such that every two adjacent nodes in the sequence share an edge in the tree. Return an array answer of length m where answer[i] is the answer to the ith query. Example 1: [https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png] 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]] Output: [0,0,1,3] Explanation: In the first query, all the edges in the path from 0 to 3 have a weight of 1. Hence, the answer is 0. In the second query, all the edges in the path from 3 to 6 have a weight of 2. Hence, the answer is 0. In the third query, we change the weight of edge [2,3] to 2. After this operation, all the edges in the path from 2 to 6 have a weight of 2. Hence, the answer is 1. In the fourth query, we change the weights of edges [0,1], [1,2] and [2,3] to 2. After these operations, all the edges in the path from 0 to 6 have a weight of 2. Hence, the answer is 3. For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from ai to bi. Example 2: [https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png] 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]] Output: [1,2,2,3] Explanation: In the first query, we change the weight of edge [1,3] to 6. After this operation, all the edges in the path from 4 to 6 have a weight of 6. Hence, the answer is 1. In the second query, we change the weight of edges [0,3] and [3,1] to 6. After these operations, all the edges in the path from 0 to 4 have a weight of 6. Hence, the answer is 2. In the third query, we change the weight of edges [1,3] and [5,2] to 6. After these operations, all the edges in the path from 6 to 5 have a weight of 6. Hence, the answer is 2. In the fourth query, we change the weights of edges [0,7], [0,3] and [1,3] to 6. After these operations, all the edges in the path from 7 to 4 have a weight of 6. Hence, the answer is 3. For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from ai to bi. Constraints: * 1 <= n <= 104 * edges.length == n - 1 * edges[i].length == 3 * 0 <= ui, vi < n * 1 <= wi <= 26 * The input is generated such that edges represents a valid tree. * 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]:
There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree. You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, find the minimum number of operations required to make the weight of every edge on the path from ai to bi equal. In one operation, you can choose any edge of the tree and change its weight to any value. Note that: * Queries are independent of each other, meaning that the tree returns to its initial state on each new query. * The path from ai to bi is a sequence of distinct nodes starting with node ai and ending with node bi such that every two adjacent nodes in the sequence share an edge in the tree. Return an array answer of length m where answer[i] is the answer to the ith query. Example 1: [https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png] 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]] Output: [0,0,1,3] Explanation: In the first query, all the edges in the path from 0 to 3 have a weight of 1. Hence, the answer is 0. In the second query, all the edges in the path from 3 to 6 have a weight of 2. Hence, the answer is 0. In the third query, we change the weight of edge [2,3] to 2. After this operation, all the edges in the path from 2 to 6 have a weight of 2. Hence, the answer is 1. In the fourth query, we change the weights of edges [0,1], [1,2] and [2,3] to 2. After these operations, all the edges in the path from 0 to 6 have a weight of 2. Hence, the answer is 3. For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from ai to bi. Example 2: [https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png] 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]] Output: [1,2,2,3] Explanation: In the first query, we change the weight of edge [1,3] to 6. After this operation, all the edges in the path from 4 to 6 have a weight of 6. Hence, the answer is 1. In the second query, we change the weight of edges [0,3] and [3,1] to 6. After these operations, all the edges in the path from 0 to 4 have a weight of 6. Hence, the answer is 2. In the third query, we change the weight of edges [1,3] and [5,2] to 6. After these operations, all the edges in the path from 6 to 5 have a weight of 6. Hence, the answer is 2. In the fourth query, we change the weights of edges [0,7], [0,3] and [1,3] to 6. After these operations, all the edges in the path from 7 to 4 have a weight of 6. Hence, the answer is 3. For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from ai to bi. Constraints: * 1 <= n <= 104 * edges.length == n - 1 * edges[i].length == 3 * 0 <= ui, vi < n * 1 <= wi <= 26 * The input is generated such that edges represents a valid tree. * 1 <= queries.length == m <= 2 * 104 * queries[i].length == 2 * 0 <= ai, bi < n Please complete the code below to solve above prblem: ```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" }
""" You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that j - i = 2, then swap the two characters at those indices in the string. Return true if you can make the strings s1 and s2 equal, and false otherwise. Example 1: Input: s1 = "abcd", s2 = "cdab" Output: true Explanation: We can do the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad". - Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2. Example 2: Input: s1 = "abcd", s2 = "dacb" Output: false Explanation: It is not possible to make the two strings equal. Constraints: * s1.length == s2.length == 4 * s1 and s2 consist only of lowercase English letters. """ class Solution: def canBeEqual(self, s1: str, s2: str) -> bool:
You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that j - i = 2, then swap the two characters at those indices in the string. Return true if you can make the strings s1 and s2 equal, and false otherwise. Example 1: Input: s1 = "abcd", s2 = "cdab" Output: true Explanation: We can do the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad". - Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2. Example 2: Input: s1 = "abcd", s2 = "dacb" Output: false Explanation: It is not possible to make the two strings equal. Constraints: * s1.length == s2.length == 4 * s1 and s2 consist only of lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" You are given two strings s1 and s2, both of length n, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in the string. Return true if you can make the strings s1 and s2 equal, and false otherwise. Example 1: Input: s1 = "abcdba", s2 = "cabdab" Output: true Explanation: We can apply the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbadba". - Choose the indices i = 2, j = 4. The resulting string is s1 = "cbbdaa". - Choose the indices i = 1, j = 5. The resulting string is s1 = "cabdab" = s2. Example 2: Input: s1 = "abe", s2 = "bea" Output: false Explanation: It is not possible to make the two strings equal. Constraints: * n == s1.length == s2.length * 1 <= n <= 105 * s1 and s2 consist only of lowercase English letters. """ class Solution: def checkStrings(self, s1: str, s2: str) -> bool:
You are given two strings s1 and s2, both of length n, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in the string. Return true if you can make the strings s1 and s2 equal, and false otherwise. Example 1: Input: s1 = "abcdba", s2 = "cabdab" Output: true Explanation: We can apply the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbadba". - Choose the indices i = 2, j = 4. The resulting string is s1 = "cbbdaa". - Choose the indices i = 1, j = 5. The resulting string is s1 = "cabdab" = s2. Example 2: Input: s1 = "abe", s2 = "bea" Output: false Explanation: It is not possible to make the two strings equal. Constraints: * n == s1.length == s2.length * 1 <= n <= 105 * s1 and s2 consist only of lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" You are given an integer array nums and two positive integers m and k. Return the maximum sum out of all almost unique subarrays of length k of nums. If no such subarray exists, return 0. A subarray of nums is almost unique if it contains at least m distinct elements. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2,6,7,3,1,7], m = 3, k = 4 Output: 18 Explanation: There are 3 almost unique subarrays of size k = 4. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18. Example 2: Input: nums = [5,9,9,2,4,5,4], m = 1, k = 3 Output: 23 Explanation: There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23. Example 3: Input: nums = [1,2,1,2,1,2,1], m = 3, k = 3 Output: 0 Explanation: There are no subarrays of size k = 3 that contain at least m = 3 distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0. Constraints: * 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:
You are given an integer array nums and two positive integers m and k. Return the maximum sum out of all almost unique subarrays of length k of nums. If no such subarray exists, return 0. A subarray of nums is almost unique if it contains at least m distinct elements. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2,6,7,3,1,7], m = 3, k = 4 Output: 18 Explanation: There are 3 almost unique subarrays of size k = 4. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18. Example 2: Input: nums = [5,9,9,2,4,5,4], m = 1, k = 3 Output: 23 Explanation: There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23. Example 3: Input: nums = [1,2,1,2,1,2,1], m = 3, k = 3 Output: 0 Explanation: There are no subarrays of size k = 3 that contain at least m = 3 distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0. Constraints: * 1 <= nums.length <= 2 * 104 * 1 <= m <= k <= nums.length * 1 <= nums[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given a string s and an integer k. A k-subsequence is a subsequence of s, having length k, and all its characters are unique, i.e., every character occurs once. Let f(c) denote the number of times the character c occurs in s. The beauty of a k-subsequence is the sum of f(c) for every character c in the k-subsequence. For example, consider s = "abbbdd" and k = 2: * f('a') = 1, f('b') = 3, f('d') = 2 * Some k-subsequences of s are: * "abbbdd" -> "ab" having a beauty of f('a') + f('b') = 4 * "abbbdd" -> "ad" having a beauty of f('a') + f('d') = 3 * "abbbdd" -> "bd" having a beauty of f('b') + f('d') = 5 Return an integer denoting the number of k-subsequences whose beauty is the maximum among all k-subsequences. Since the answer may be too large, return it modulo 109 + 7. A subsequence of a string is a new string formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters. Notes * f(c) is the number of times a character c occurs in s, not a k-subsequence. * Two k-subsequences are considered different if one is formed by an index that is not present in the other. So, two k-subsequences may form the same string. Example 1: Input: s = "bcca", k = 2 Output: 4 Explanation: From s we have f('a') = 1, f('b') = 1, and f('c') = 2. The k-subsequences of s are: bcca having a beauty of f('b') + f('c') = 3 bcca having a beauty of f('b') + f('c') = 3 bcca having a beauty of f('b') + f('a') = 2 bcca having a beauty of f('c') + f('a') = 3 bcca having a beauty of f('c') + f('a') = 3 There are 4 k-subsequences that have the maximum beauty, 3. Hence, the answer is 4. Example 2: Input: s = "abbcd", k = 4 Output: 2 Explanation: From s we have f('a') = 1, f('b') = 2, f('c') = 1, and f('d') = 1. The k-subsequences of s are: abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5 abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5 There are 2 k-subsequences that have the maximum beauty, 5. Hence, the answer is 2. Constraints: * 1 <= s.length <= 2 * 105 * 1 <= k <= s.length * s consists only of lowercase English letters. """ class Solution: def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:
You are given a string s and an integer k. A k-subsequence is a subsequence of s, having length k, and all its characters are unique, i.e., every character occurs once. Let f(c) denote the number of times the character c occurs in s. The beauty of a k-subsequence is the sum of f(c) for every character c in the k-subsequence. For example, consider s = "abbbdd" and k = 2: * f('a') = 1, f('b') = 3, f('d') = 2 * Some k-subsequences of s are: * "abbbdd" -> "ab" having a beauty of f('a') + f('b') = 4 * "abbbdd" -> "ad" having a beauty of f('a') + f('d') = 3 * "abbbdd" -> "bd" having a beauty of f('b') + f('d') = 5 Return an integer denoting the number of k-subsequences whose beauty is the maximum among all k-subsequences. Since the answer may be too large, return it modulo 109 + 7. A subsequence of a string is a new string formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters. Notes * f(c) is the number of times a character c occurs in s, not a k-subsequence. * Two k-subsequences are considered different if one is formed by an index that is not present in the other. So, two k-subsequences may form the same string. Example 1: Input: s = "bcca", k = 2 Output: 4 Explanation: From s we have f('a') = 1, f('b') = 1, and f('c') = 2. The k-subsequences of s are: bcca having a beauty of f('b') + f('c') = 3 bcca having a beauty of f('b') + f('c') = 3 bcca having a beauty of f('b') + f('a') = 2 bcca having a beauty of f('c') + f('a') = 3 bcca having a beauty of f('c') + f('a') = 3 There are 4 k-subsequences that have the maximum beauty, 3. Hence, the answer is 4. Example 2: Input: s = "abbcd", k = 4 Output: 2 Explanation: From s we have f('a') = 1, f('b') = 2, f('c') = 1, and f('d') = 1. The k-subsequences of s are: abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5 abbcd having a beauty of f('a') + f('b') + f('c') + f('d') = 5 There are 2 k-subsequences that have the maximum beauty, 5. Hence, the answer is 2. Constraints: * 1 <= s.length <= 2 * 105 * 1 <= k <= s.length * s consists only of lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0. In the ith move, you can choose one of the following directions: * move to the left if moves[i] = 'L' or moves[i] = '_' * move to the right if moves[i] = 'R' or moves[i] = '_' Return the distance from the origin of the furthest point you can get to after n moves. Example 1: Input: moves = "L_RL__R" Output: 3 Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR". Example 2: Input: moves = "_R__LL_" Output: 5 Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL". Example 3: Input: moves = "_______" Output: 7 Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR". Constraints: * 1 <= moves.length == n <= 50 * moves consists only of characters 'L', 'R' and '_'. """ class Solution: def furthestDistanceFromOrigin(self, moves: str) -> int:
You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0. In the ith move, you can choose one of the following directions: * move to the left if moves[i] = 'L' or moves[i] = '_' * move to the right if moves[i] = 'R' or moves[i] = '_' Return the distance from the origin of the furthest point you can get to after n moves. Example 1: Input: moves = "L_RL__R" Output: 3 Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR". Example 2: Input: moves = "_R__LL_" Output: 5 Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL". Example 3: Input: moves = "_______" Output: 7 Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR". Constraints: * 1 <= moves.length == n <= 50 * moves consists only of characters 'L', 'R' and '_'. Please complete the code below to solve above prblem: ```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" }
""" You are given positive integers n and target. An array nums is beautiful if it meets the following conditions: * nums.length == n. * nums consists of pairwise distinct positive integers. * There doesn't exist two distinct indices, i and j, in the range [0, n - 1], such that nums[i] + nums[j] == target. Return the minimum possible sum that a beautiful array could have modulo 109 + 7. Example 1: Input: n = 2, target = 3 Output: 4 Explanation: We can see that nums = [1,3] is beautiful. - The array nums has length n = 2. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 4 is the minimum possible sum that a beautiful array could have. Example 2: Input: n = 3, target = 3 Output: 8 Explanation: We can see that nums = [1,3,4] is beautiful. - The array nums has length n = 3. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 8 is the minimum possible sum that a beautiful array could have. Example 3: Input: n = 1, target = 1 Output: 1 Explanation: We can see, that nums = [1] is beautiful. Constraints: * 1 <= n <= 109 * 1 <= target <= 109 """ class Solution: def minimumPossibleSum(self, n: int, target: int) -> int:
You are given positive integers n and target. An array nums is beautiful if it meets the following conditions: * nums.length == n. * nums consists of pairwise distinct positive integers. * There doesn't exist two distinct indices, i and j, in the range [0, n - 1], such that nums[i] + nums[j] == target. Return the minimum possible sum that a beautiful array could have modulo 109 + 7. Example 1: Input: n = 2, target = 3 Output: 4 Explanation: We can see that nums = [1,3] is beautiful. - The array nums has length n = 2. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 4 is the minimum possible sum that a beautiful array could have. Example 2: Input: n = 3, target = 3 Output: 8 Explanation: We can see that nums = [1,3,4] is beautiful. - The array nums has length n = 3. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 8 is the minimum possible sum that a beautiful array could have. Example 3: Input: n = 1, target = 1 Output: 1 Explanation: We can see, that nums = [1] is beautiful. Constraints: * 1 <= n <= 109 * 1 <= target <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array nums consisting of non-negative powers of 2, and an integer target. In one operation, you must apply the following changes to the array: * Choose any element of the array nums[i] such that nums[i] > 1. * Remove nums[i] from the array. * Add two occurrences of nums[i] / 2 to the end of nums. Return the minimum number of operations you need to perform so that nums contains a subsequence whose elements sum to target. If it is impossible to obtain such a subsequence, return -1. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1: Input: nums = [1,2,8], target = 7 Output: 1 Explanation: In the first operation, we choose element nums[2]. The array becomes equal to nums = [1,2,4,4]. At this stage, nums contains the subsequence [1,2,4] which sums up to 7. It can be shown that there is no shorter sequence of operations that results in a subsequnce that sums up to 7. Example 2: Input: nums = [1,32,1,2], target = 12 Output: 2 Explanation: In the first operation, we choose element nums[1]. The array becomes equal to nums = [1,1,2,16,16]. In the second operation, we choose element nums[3]. The array becomes equal to nums = [1,1,2,16,8,8] At this stage, nums contains the subsequence [1,1,2,8] which sums up to 12. It can be shown that there is no shorter sequence of operations that results in a subsequence that sums up to 12. Example 3: Input: nums = [1,32,1], target = 35 Output: -1 Explanation: It can be shown that no sequence of operations results in a subsequence that sums up to 35. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 230 * nums consists only of non-negative powers of two. * 1 <= target < 231 """ class Solution: def minOperations(self, nums: List[int], target: int) -> int:
You are given a 0-indexed array nums consisting of non-negative powers of 2, and an integer target. In one operation, you must apply the following changes to the array: * Choose any element of the array nums[i] such that nums[i] > 1. * Remove nums[i] from the array. * Add two occurrences of nums[i] / 2 to the end of nums. Return the minimum number of operations you need to perform so that nums contains a subsequence whose elements sum to target. If it is impossible to obtain such a subsequence, return -1. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1: Input: nums = [1,2,8], target = 7 Output: 1 Explanation: In the first operation, we choose element nums[2]. The array becomes equal to nums = [1,2,4,4]. At this stage, nums contains the subsequence [1,2,4] which sums up to 7. It can be shown that there is no shorter sequence of operations that results in a subsequnce that sums up to 7. Example 2: Input: nums = [1,32,1,2], target = 12 Output: 2 Explanation: In the first operation, we choose element nums[1]. The array becomes equal to nums = [1,1,2,16,16]. In the second operation, we choose element nums[3]. The array becomes equal to nums = [1,1,2,16,8,8] At this stage, nums contains the subsequence [1,1,2,8] which sums up to 12. It can be shown that there is no shorter sequence of operations that results in a subsequence that sums up to 12. Example 3: Input: nums = [1,32,1], target = 35 Output: -1 Explanation: It can be shown that no sequence of operations results in a subsequence that sums up to 35. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 230 * nums consists only of non-negative powers of two. * 1 <= target < 231 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array receiver of length n and an integer k. There are n players having a unique id in the range [0, n - 1] who will play a ball passing game, and receiver[i] is the id of the player who receives passes from the player with id i. Players can pass to themselves, i.e. receiver[i] may be equal to i. You must choose one of the n players as the starting player for the game, and the ball will be passed exactly k times starting from the chosen player. For a chosen starting player having id x, we define a function f(x) that denotes the sum of x and the ids of all players who receive the ball during the k passes, including repetitions. In other words, f(x) = x + receiver[x] + receiver[receiver[x]] + ... + receiver(k)[x]. Your task is to choose a starting player having id x that maximizes the value of f(x). Return an integer denoting the maximum value of the function. Note: receiver may contain duplicates. Example 1: Pass Number Sender ID Receiver ID x + Receiver IDs       2 1 2 1 3 2 1 0 3 3 0 2 5 4 2 1 6 Input: receiver = [2,0,1], k = 4 Output: 6 Explanation: The table above shows a simulation of the game starting with the player having id x = 2. From the table, f(2) is equal to 6. It can be shown that 6 is the maximum achievable value of the function. Hence, the output is 6. Example 2: Pass Number Sender ID Receiver ID x + Receiver IDs       4 1 4 3 7 2 3 2 9 3 2 1 10 Input: receiver = [1,1,1,2,3], k = 3 Output: 10 Explanation: The table above shows a simulation of the game starting with the player having id x = 4. From the table, f(4) is equal to 10. It can be shown that 10 is the maximum achievable value of the function. Hence, the output is 10. Constraints: * 1 <= receiver.length == n <= 105 * 0 <= receiver[i] <= n - 1 * 1 <= k <= 1010 """ class Solution: def getMaxFunctionValue(self, receiver: List[int], k: int) -> int:
You are given a 0-indexed integer array receiver of length n and an integer k. There are n players having a unique id in the range [0, n - 1] who will play a ball passing game, and receiver[i] is the id of the player who receives passes from the player with id i. Players can pass to themselves, i.e. receiver[i] may be equal to i. You must choose one of the n players as the starting player for the game, and the ball will be passed exactly k times starting from the chosen player. For a chosen starting player having id x, we define a function f(x) that denotes the sum of x and the ids of all players who receive the ball during the k passes, including repetitions. In other words, f(x) = x + receiver[x] + receiver[receiver[x]] + ... + receiver(k)[x]. Your task is to choose a starting player having id x that maximizes the value of f(x). Return an integer denoting the maximum value of the function. Note: receiver may contain duplicates. Example 1: Pass Number Sender ID Receiver ID x + Receiver IDs       2 1 2 1 3 2 1 0 3 3 0 2 5 4 2 1 6 Input: receiver = [2,0,1], k = 4 Output: 6 Explanation: The table above shows a simulation of the game starting with the player having id x = 2. From the table, f(2) is equal to 6. It can be shown that 6 is the maximum achievable value of the function. Hence, the output is 6. Example 2: Pass Number Sender ID Receiver ID x + Receiver IDs       4 1 4 3 7 2 3 2 9 3 2 1 10 Input: receiver = [1,1,1,2,3], k = 3 Output: 10 Explanation: The table above shows a simulation of the game starting with the player having id x = 4. From the table, f(4) is equal to 10. It can be shown that 10 is the maximum achievable value of the function. Hence, the output is 10. Constraints: * 1 <= receiver.length == n <= 105 * 0 <= receiver[i] <= n - 1 * 1 <= k <= 1010 Please complete the code below to solve above prblem: ```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" }
""" Given an array of strings words and a string s, determine if s is an acronym of words. The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["bear", "aardvark"]. Return true if s is an acronym of words, and false otherwise. Example 1: Input: words = ["alice","bob","charlie"], s = "abc" Output: true Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym. Example 2: Input: words = ["an","apple"], s = "a" Output: false Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively. The acronym formed by concatenating these characters is "aa". Hence, s = "a" is not the acronym. Example 3: Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy" Output: true Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy". Hence, s = "ngguoy" is the acronym. Constraints: * 1 <= words.length <= 100 * 1 <= words[i].length <= 10 * 1 <= s.length <= 100 * words[i] and s consist of lowercase English letters. """ class Solution: def isAcronym(self, words: List[str], s: str) -> bool:
Given an array of strings words and a string s, determine if s is an acronym of words. The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["bear", "aardvark"]. Return true if s is an acronym of words, and false otherwise. Example 1: Input: words = ["alice","bob","charlie"], s = "abc" Output: true Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym. Example 2: Input: words = ["an","apple"], s = "a" Output: false Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively. The acronym formed by concatenating these characters is "aa". Hence, s = "a" is not the acronym. Example 3: Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy" Output: true Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy". Hence, s = "ngguoy" is the acronym. Constraints: * 1 <= words.length <= 100 * 1 <= words[i].length <= 10 * 1 <= s.length <= 100 * words[i] and s consist of lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" You are given two integers, n and k. An array of distinct positive integers is called a k-avoiding array if there does not exist any pair of distinct elements that sum to k. Return the minimum possible sum of a k-avoiding array of length n. Example 1: Input: n = 5, k = 4 Output: 18 Explanation: Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18. It can be proven that there is no k-avoiding array with a sum less than 18. Example 2: Input: n = 2, k = 6 Output: 3 Explanation: We can construct the array [1,2], which has a sum of 3. It can be proven that there is no k-avoiding array with a sum less than 3. Constraints: * 1 <= n, k <= 50 """ class Solution: def minimumSum(self, n: int, k: int) -> int:
You are given two integers, n and k. An array of distinct positive integers is called a k-avoiding array if there does not exist any pair of distinct elements that sum to k. Return the minimum possible sum of a k-avoiding array of length n. Example 1: Input: n = 5, k = 4 Output: 18 Explanation: Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18. It can be proven that there is no k-avoiding array with a sum less than 18. Example 2: Input: n = 2, k = 6 Output: 3 Explanation: We can construct the array [1,2], which has a sum of 3. It can be proven that there is no k-avoiding array with a sum less than 3. Constraints: * 1 <= n, k <= 50 Please complete the code below to solve above prblem: ```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" }
""" You are given an integer n representing the number of houses on a number line, numbered from 0 to n - 1. Additionally, you are given a 2D integer array offers where offers[i] = [starti, endi, goldi], indicating that ith buyer wants to buy all the houses from starti to endi for goldi amount of gold. As a salesman, your goal is to maximize your earnings by strategically selecting and selling houses to buyers. Return the maximum amount of gold you can earn. Note that different buyers can't buy the same house, and some houses may remain unsold. Example 1: Input: n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]] Output: 3 Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,0] to 1st buyer for 1 gold and houses in the range [1,3] to 3rd buyer for 2 golds. It can be proven that 3 is the maximum amount of gold we can achieve. Example 2: Input: n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]] Output: 10 Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,2] to 2nd buyer for 10 golds. It can be proven that 10 is the maximum amount of gold we can achieve. Constraints: * 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:
You are given an integer n representing the number of houses on a number line, numbered from 0 to n - 1. Additionally, you are given a 2D integer array offers where offers[i] = [starti, endi, goldi], indicating that ith buyer wants to buy all the houses from starti to endi for goldi amount of gold. As a salesman, your goal is to maximize your earnings by strategically selecting and selling houses to buyers. Return the maximum amount of gold you can earn. Note that different buyers can't buy the same house, and some houses may remain unsold. Example 1: Input: n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]] Output: 3 Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,0] to 1st buyer for 1 gold and houses in the range [1,3] to 3rd buyer for 2 golds. It can be proven that 3 is the maximum amount of gold we can achieve. Example 2: Input: n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]] Output: 10 Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,2] to 2nd buyer for 10 golds. It can be proven that 10 is the maximum amount of gold we can achieve. Constraints: * 1 <= n <= 105 * 1 <= offers.length <= 105 * offers[i].length == 3 * 0 <= starti <= endi <= n - 1 * 1 <= goldi <= 103 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums and an integer k. A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray. Return the length of the longest possible equal subarray after deleting at most k elements from nums. A subarray is a contiguous, possibly empty sequence of elements within an array. Example 1: Input: nums = [1,3,2,3,1,3], k = 3 Output: 3 Explanation: It's optimal to delete the elements at index 2 and index 4. After deleting them, nums becomes equal to [1, 3, 3, 3]. The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3. It can be proven that no longer equal subarrays can be created. Example 2: Input: nums = [1,1,2,2,1,1], k = 2 Output: 4 Explanation: It's optimal to delete the elements at index 2 and index 3. After deleting them, nums becomes equal to [1, 1, 1, 1]. The array itself is an equal subarray, so the answer is 4. It can be proven that no longer equal subarrays can be created. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= nums.length * 0 <= k <= nums.length """ class Solution: def longestEqualSubarray(self, nums: List[int], k: int) -> int:
You are given a 0-indexed integer array nums and an integer k. A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray. Return the length of the longest possible equal subarray after deleting at most k elements from nums. A subarray is a contiguous, possibly empty sequence of elements within an array. Example 1: Input: nums = [1,3,2,3,1,3], k = 3 Output: 3 Explanation: It's optimal to delete the elements at index 2 and index 4. After deleting them, nums becomes equal to [1, 3, 3, 3]. The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3. It can be proven that no longer equal subarrays can be created. Example 2: Input: nums = [1,1,2,2,1,1], k = 2 Output: 4 Explanation: It's optimal to delete the elements at index 2 and index 3. After deleting them, nums becomes equal to [1, 1, 1, 1]. The array itself is an equal subarray, so the answer is 4. It can be proven that no longer equal subarrays can be created. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= nums.length * 0 <= k <= nums.length Please complete the code below to solve above prblem: ```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" }
""" Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target. Example 1: Input: nums = [-1,1,2,3,1], target = 2 Output: 3 Explanation: There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target - (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target. Example 2: Input: nums = [-6,2,5,-2,-7,-1,3], target = -2 Output: 10 Explanation: There are 10 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target - (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target - (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target - (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target - (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target - (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target - (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target - (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target - (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target Constraints: * 1 <= nums.length == n <= 50 * -50 <= nums[i], target <= 50 """ class Solution: def countPairs(self, nums: List[int], target: int) -> int:
Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target. Example 1: Input: nums = [-1,1,2,3,1], target = 2 Output: 3 Explanation: There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target - (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target. Example 2: Input: nums = [-6,2,5,-2,-7,-1,3], target = -2 Output: 10 Explanation: There are 10 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target - (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target - (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target - (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target - (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target - (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target - (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target - (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target - (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target Constraints: * 1 <= nums.length == n <= 50 * -50 <= nums[i], target <= 50 Please complete the code below to solve above prblem: ```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" }
""" You are given two 0-indexed strings str1 and str2. In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'. Return true if it is possible to make str2 a subsequence of str1 by performing the operation at most once, and false otherwise. Note: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters. Example 1: Input: str1 = "abc", str2 = "ad" Output: true Explanation: Select index 2 in str1. Increment str1[2] to become 'd'. Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned. Example 2: Input: str1 = "zc", str2 = "ad" Output: true Explanation: Select indices 0 and 1 in str1. Increment str1[0] to become 'a'. Increment str1[1] to become 'd'. Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned. Example 3: Input: str1 = "ab", str2 = "d" Output: false Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. Therefore, false is returned. Constraints: * 1 <= str1.length <= 105 * 1 <= str2.length <= 105 * str1 and str2 consist of only lowercase English letters. """ class Solution: def canMakeSubsequence(self, str1: str, str2: str) -> bool:
You are given two 0-indexed strings str1 and str2. In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'. Return true if it is possible to make str2 a subsequence of str1 by performing the operation at most once, and false otherwise. Note: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters. Example 1: Input: str1 = "abc", str2 = "ad" Output: true Explanation: Select index 2 in str1. Increment str1[2] to become 'd'. Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned. Example 2: Input: str1 = "zc", str2 = "ad" Output: true Explanation: Select indices 0 and 1 in str1. Increment str1[0] to become 'a'. Increment str1[1] to become 'd'. Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned. Example 3: Input: str1 = "ab", str2 = "d" Output: false Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. Therefore, false is returned. Constraints: * 1 <= str1.length <= 105 * 1 <= str2.length <= 105 * str1 and str2 consist of only lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums of length n. The numbers from 0 to n - 1 are divided into three groups numbered from 1 to 3, where number i belongs to group nums[i]. Notice that some groups may be empty. You are allowed to perform this operation any number of times: * Pick number x and change its group. More formally, change nums[x] to any number from 1 to 3. A new array res is constructed using the following procedure: 1. Sort the numbers in each group independently. 2. Append the elements of groups 1, 2, and 3 to res in this order. Array nums is called a beautiful array if the constructed array res is sorted in non-decreasing order. Return the minimum number of operations to make nums a beautiful array. Example 1: Input: nums = [2,1,3,2,1] Output: 3 Explanation: It's optimal to perform three operations: 1. change nums[0] to 1. 2. change nums[2] to 1. 3. change nums[3] to 1. After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3,4] and group 2 and group 3 become empty. Hence, res is equal to [0,1,2,3,4] which is sorted in non-decreasing order. It can be proven that there is no valid sequence of less than three operations. Example 2: Input: nums = [1,3,2,1,3,3] Output: 2 Explanation: It's optimal to perform two operations: 1. change nums[1] to 1. 2. change nums[2] to 1. After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3], group 2 becomes empty, and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order. It can be proven that there is no valid sequence of less than two operations. Example 3: Input: nums = [2,2,2,2,3,3] Output: 0 Explanation: It's optimal to not perform operations. After sorting the numbers in each group, group 1 becomes empty, group 2 becomes equal to [0,1,2,3] and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order. Constraints: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 3 """ class Solution: def minimumOperations(self, nums: List[int]) -> int:
You are given a 0-indexed integer array nums of length n. The numbers from 0 to n - 1 are divided into three groups numbered from 1 to 3, where number i belongs to group nums[i]. Notice that some groups may be empty. You are allowed to perform this operation any number of times: * Pick number x and change its group. More formally, change nums[x] to any number from 1 to 3. A new array res is constructed using the following procedure: 1. Sort the numbers in each group independently. 2. Append the elements of groups 1, 2, and 3 to res in this order. Array nums is called a beautiful array if the constructed array res is sorted in non-decreasing order. Return the minimum number of operations to make nums a beautiful array. Example 1: Input: nums = [2,1,3,2,1] Output: 3 Explanation: It's optimal to perform three operations: 1. change nums[0] to 1. 2. change nums[2] to 1. 3. change nums[3] to 1. After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3,4] and group 2 and group 3 become empty. Hence, res is equal to [0,1,2,3,4] which is sorted in non-decreasing order. It can be proven that there is no valid sequence of less than three operations. Example 2: Input: nums = [1,3,2,1,3,3] Output: 2 Explanation: It's optimal to perform two operations: 1. change nums[1] to 1. 2. change nums[2] to 1. After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3], group 2 becomes empty, and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order. It can be proven that there is no valid sequence of less than two operations. Example 3: Input: nums = [2,2,2,2,3,3] Output: 0 Explanation: It's optimal to not perform operations. After sorting the numbers in each group, group 1 becomes empty, group 2 becomes equal to [0,1,2,3] and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order. Constraints: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 3 Please complete the code below to solve above prblem: ```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" }
""" You are given positive integers low, high, and k. A number is beautiful if it meets both of the following conditions: * The count of even digits in the number is equal to the count of odd digits. * The number is divisible by k. Return the number of beautiful integers in the range [low, high]. Example 1: Input: low = 10, high = 20, k = 3 Output: 2 Explanation: There are 2 beautiful integers in the given range: [12,18]. - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that: - 16 is not beautiful because it is not divisible by k = 3. - 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range. Example 2: Input: low = 1, high = 10, k = 1 Output: 1 Explanation: There is 1 beautiful integer in the given range: [10]. - 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1. It can be shown that there is only 1 beautiful integer in the given range. Example 3: Input: low = 5, high = 5, k = 2 Output: 0 Explanation: There are 0 beautiful integers in the given range. - 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits. Constraints: * 0 < low <= high <= 109 * 0 < k <= 20 """ class Solution: def numberOfBeautifulIntegers(self, low: int, high: int, k: int) -> int:
You are given positive integers low, high, and k. A number is beautiful if it meets both of the following conditions: * The count of even digits in the number is equal to the count of odd digits. * The number is divisible by k. Return the number of beautiful integers in the range [low, high]. Example 1: Input: low = 10, high = 20, k = 3 Output: 2 Explanation: There are 2 beautiful integers in the given range: [12,18]. - 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. - 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that: - 16 is not beautiful because it is not divisible by k = 3. - 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range. Example 2: Input: low = 1, high = 10, k = 1 Output: 1 Explanation: There is 1 beautiful integer in the given range: [10]. - 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1. It can be shown that there is only 1 beautiful integer in the given range. Example 3: Input: low = 5, high = 5, k = 2 Output: 0 Explanation: There are 0 beautiful integers in the given range. - 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits. Constraints: * 0 < low <= high <= 109 * 0 < k <= 20 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the maximum digit in both numbers are equal. Return the maximum sum or -1 if no such pair exists. Example 1: Input: nums = [51,71,17,24,42] Output: 88 Explanation: For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66. It can be shown that there are no other pairs with equal maximum digits, so the answer is 88. Example 2: Input: nums = [1,2,3,4] Output: -1 Explanation: No pair exists in nums with equal maximum digits. Constraints: * 2 <= nums.length <= 100 * 1 <= nums[i] <= 104 """ class Solution: def maxSum(self, nums: List[int]) -> int:
You are given a 0-indexed integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the maximum digit in both numbers are equal. Return the maximum sum or -1 if no such pair exists. Example 1: Input: nums = [51,71,17,24,42] Output: 88 Explanation: For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66. It can be shown that there are no other pairs with equal maximum digits, so the answer is 88. Example 2: Input: nums = [1,2,3,4] Output: -1 Explanation: No pair exists in nums with equal maximum digits. Constraints: * 2 <= nums.length <= 100 * 1 <= nums[i] <= 104 Please complete the code below to solve above prblem: ```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" }
""" You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Return the head of the linked list after doubling it. Example 1: [https://assets.leetcode.com/uploads/2023/05/28/example.png] Input: head = [1,8,9] Output: [3,7,8] Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. Example 2: [https://assets.leetcode.com/uploads/2023/05/28/example2.png] Input: head = [9,9,9] Output: [1,9,9,8] Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. Constraints: * The number of nodes in the list is in the range [1, 104] * 0 <= Node.val <= 9 * The input is generated such that the list represents a number that does not have leading zeros, except the number 0 itself. """ # 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]:
You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Return the head of the linked list after doubling it. Example 1: [https://assets.leetcode.com/uploads/2023/05/28/example.png] Input: head = [1,8,9] Output: [3,7,8] Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. Example 2: [https://assets.leetcode.com/uploads/2023/05/28/example2.png] Input: head = [9,9,9] Output: [1,9,9,8] Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. Constraints: * The number of nodes in the list is in the range [1, 104] * 0 <= Node.val <= 9 * The input is generated such that the list represents a number that does not have leading zeros, except the number 0 itself. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums and an integer x. Find the minimum absolute difference between two elements in the array that are at least x indices apart. In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized. Return an integer denoting the minimum absolute difference between two elements that are at least x indices apart. Example 1: Input: nums = [4,3,2,4], x = 2 Output: 0 Explanation: We can select nums[0] = 4 and nums[3] = 4. They are at least 2 indices apart, and their absolute difference is the minimum, 0. It can be shown that 0 is the optimal answer. Example 2: Input: nums = [5,3,2,10,15], x = 1 Output: 1 Explanation: We can select nums[1] = 3 and nums[2] = 2. They are at least 1 index apart, and their absolute difference is the minimum, 1. It can be shown that 1 is the optimal answer. Example 3: Input: nums = [1,2,3,4], x = 3 Output: 3 Explanation: We can select nums[0] = 1 and nums[3] = 4. They are at least 3 indices apart, and their absolute difference is the minimum, 3. It can be shown that 3 is the optimal answer. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 * 0 <= x < nums.length """ class Solution: def minAbsoluteDifference(self, nums: List[int], x: int) -> int:
You are given a 0-indexed integer array nums and an integer x. Find the minimum absolute difference between two elements in the array that are at least x indices apart. In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized. Return an integer denoting the minimum absolute difference between two elements that are at least x indices apart. Example 1: Input: nums = [4,3,2,4], x = 2 Output: 0 Explanation: We can select nums[0] = 4 and nums[3] = 4. They are at least 2 indices apart, and their absolute difference is the minimum, 0. It can be shown that 0 is the optimal answer. Example 2: Input: nums = [5,3,2,10,15], x = 1 Output: 1 Explanation: We can select nums[1] = 3 and nums[2] = 2. They are at least 1 index apart, and their absolute difference is the minimum, 1. It can be shown that 1 is the optimal answer. Example 3: Input: nums = [1,2,3,4], x = 3 Output: 3 Explanation: We can select nums[0] = 1 and nums[3] = 4. They are at least 3 indices apart, and their absolute difference is the minimum, 3. It can be shown that 3 is the optimal answer. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 * 0 <= x < nums.length Please complete the code below to solve above prblem: ```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" }
""" You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1. You have to maximize your score by applying the following operation at most k times: * Choose any non-empty subarray nums[l, ..., r] that you haven't chosen previously. * Choose an element x of nums[l, ..., r] with the highest prime score. If multiple such elements exist, choose the one with the smallest index. * Multiply your score by x. Here, nums[l, ..., r] denotes the subarray of nums starting at index l and ending at the index r, both ends being inclusive. The prime score of an integer x is equal to the number of distinct prime factors of x. For example, the prime score of 300 is 3 since 300 = 2 * 2 * 3 * 5 * 5. Return the maximum possible score after applying at most k operations. Since the answer may be large, return it modulo 109 + 7. Example 1: Input: nums = [8,3,9,3,8], k = 2 Output: 81 Explanation: To get a score of 81, we can apply the following operations: - Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81. It can be proven that 81 is the highest score one can obtain. Example 2: Input: nums = [19,12,14,6,10,18], k = 3 Output: 4788 Explanation: To get a score of 4788, we can apply the following operations: - Choose subarray nums[0, ..., 0]. nums[0] is the only element in this subarray. Hence, we multiply the score by nums[0]. The score becomes 1 * 19 = 19. - Choose subarray nums[5, ..., 5]. nums[5] is the only element in this subarray. Hence, we multiply the score by nums[5]. The score becomes 19 * 18 = 342. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 2, but nums[2] has the smaller index. Hence, we multipy the score by nums[2]. The score becomes 342 * 14 = 4788. It can be proven that 4788 is the highest score one can obtain. Constraints: * 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:
You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1. You have to maximize your score by applying the following operation at most k times: * Choose any non-empty subarray nums[l, ..., r] that you haven't chosen previously. * Choose an element x of nums[l, ..., r] with the highest prime score. If multiple such elements exist, choose the one with the smallest index. * Multiply your score by x. Here, nums[l, ..., r] denotes the subarray of nums starting at index l and ending at the index r, both ends being inclusive. The prime score of an integer x is equal to the number of distinct prime factors of x. For example, the prime score of 300 is 3 since 300 = 2 * 2 * 3 * 5 * 5. Return the maximum possible score after applying at most k operations. Since the answer may be large, return it modulo 109 + 7. Example 1: Input: nums = [8,3,9,3,8], k = 2 Output: 81 Explanation: To get a score of 81, we can apply the following operations: - Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81. It can be proven that 81 is the highest score one can obtain. Example 2: Input: nums = [19,12,14,6,10,18], k = 3 Output: 4788 Explanation: To get a score of 4788, we can apply the following operations: - Choose subarray nums[0, ..., 0]. nums[0] is the only element in this subarray. Hence, we multiply the score by nums[0]. The score becomes 1 * 19 = 19. - Choose subarray nums[5, ..., 5]. nums[5] is the only element in this subarray. Hence, we multiply the score by nums[5]. The score becomes 19 * 18 = 342. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 2, but nums[2] has the smaller index. Hence, we multipy the score by nums[2]. The score becomes 342 * 14 = 4788. It can be proven that 4788 is the highest score one can obtain. Constraints: * 1 <= nums.length == n <= 105 * 1 <= nums[i] <= 105 * 1 <= k <= min(n * (n + 1) / 2, 109) Please complete the code below to solve above prblem: ```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" }
""" Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected. You are given a 0-indexed string s, and you type each character of s using your faulty keyboard. Return the final string that will be present on your laptop screen. Example 1: Input: s = "string" Output: "rtsng" Explanation: After typing first character, the text on the screen is "s". After the second character, the text is "st". After the third character, the text is "str". Since the fourth character is an 'i', the text gets reversed and becomes "rts". After the fifth character, the text is "rtsn". After the sixth character, the text is "rtsng". Therefore, we return "rtsng". Example 2: Input: s = "poiinter" Output: "ponter" Explanation: After the first character, the text on the screen is "p". After the second character, the text is "po". Since the third character you type is an 'i', the text gets reversed and becomes "op". Since the fourth character you type is an 'i', the text gets reversed and becomes "po". After the fifth character, the text is "pon". After the sixth character, the text is "pont". After the seventh character, the text is "ponte". After the eighth character, the text is "ponter". Therefore, we return "ponter". Constraints: * 1 <= s.length <= 100 * s consists of lowercase English letters. * s[0] != 'i' """ class Solution: def finalString(self, s: str) -> str:
Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected. You are given a 0-indexed string s, and you type each character of s using your faulty keyboard. Return the final string that will be present on your laptop screen. Example 1: Input: s = "string" Output: "rtsng" Explanation: After typing first character, the text on the screen is "s". After the second character, the text is "st". After the third character, the text is "str". Since the fourth character is an 'i', the text gets reversed and becomes "rts". After the fifth character, the text is "rtsn". After the sixth character, the text is "rtsng". Therefore, we return "rtsng". Example 2: Input: s = "poiinter" Output: "ponter" Explanation: After the first character, the text on the screen is "p". After the second character, the text is "po". Since the third character you type is an 'i', the text gets reversed and becomes "op". Since the fourth character you type is an 'i', the text gets reversed and becomes "po". After the fifth character, the text is "pon". After the sixth character, the text is "pont". After the seventh character, the text is "ponte". After the eighth character, the text is "ponter". Therefore, we return "ponter". Constraints: * 1 <= s.length <= 100 * s consists of lowercase English letters. * s[0] != 'i' Please complete the code below to solve above prblem: ```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" }
""" You are given an array nums of length n and an integer m. You need to determine if it is possible to split the array into n non-empty arrays by performing a series of steps. In each step, you can select an existing array (which may be the result of previous steps) with a length of at least two and split it into two subarrays, if, for each resulting subarray, at least one of the following holds: * The length of the subarray is one, or * The sum of elements of the subarray is greater than or equal to m. Return true if you can split the given array into n arrays, otherwise return false. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2, 2, 1], m = 4 Output: true Explanation: We can split the array into [2, 2] and [1] in the first step. Then, in the second step, we can split [2, 2] into [2] and [2]. As a result, the answer is true. Example 2: Input: nums = [2, 1, 3], m = 5 Output: false Explanation: We can try splitting the array in two different ways: the first way is to have [2, 1] and [3], and the second way is to have [2] and [1, 3]. However, both of these ways are not valid. So, the answer is false. Example 3: Input: nums = [2, 3, 3, 2, 3], m = 6 Output: true Explanation: We can split the array into [2, 3, 3, 2] and [3] in the first step. Then, in the second step, we can split [2, 3, 3, 2] into [2, 3, 3] and [2]. Then, in the third step, we can split [2, 3, 3] into [2] and [3, 3]. And in the last step we can split [3, 3] into [3] and [3]. As a result, the answer is true. Constraints: * 1 <= n == nums.length <= 100 * 1 <= nums[i] <= 100 * 1 <= m <= 200 """ class Solution: def canSplitArray(self, nums: List[int], m: int) -> bool:
You are given an array nums of length n and an integer m. You need to determine if it is possible to split the array into n non-empty arrays by performing a series of steps. In each step, you can select an existing array (which may be the result of previous steps) with a length of at least two and split it into two subarrays, if, for each resulting subarray, at least one of the following holds: * The length of the subarray is one, or * The sum of elements of the subarray is greater than or equal to m. Return true if you can split the given array into n arrays, otherwise return false. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2, 2, 1], m = 4 Output: true Explanation: We can split the array into [2, 2] and [1] in the first step. Then, in the second step, we can split [2, 2] into [2] and [2]. As a result, the answer is true. Example 2: Input: nums = [2, 1, 3], m = 5 Output: false Explanation: We can try splitting the array in two different ways: the first way is to have [2, 1] and [3], and the second way is to have [2] and [1, 3]. However, both of these ways are not valid. So, the answer is false. Example 3: Input: nums = [2, 3, 3, 2, 3], m = 6 Output: true Explanation: We can split the array into [2, 3, 3, 2] and [3] in the first step. Then, in the second step, we can split [2, 3, 3, 2] into [2, 3, 3] and [2]. Then, in the third step, we can split [2, 3, 3] into [2] and [3, 3]. And in the last step we can split [3, 3] into [3] and [3]. As a result, the answer is true. Constraints: * 1 <= n == nums.length <= 100 * 1 <= nums[i] <= 100 * 1 <= m <= 200 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: * A cell containing a thief if grid[r][c] = 1 * An empty cell if grid[r][c] = 0 You are initially positioned at cell (0, 0). In one move, you can move to any adjacent cell in the grid, including cells containing thieves. The safeness factor of a path on the grid is defined as the minimum manhattan distance from any cell in the path to any thief in the grid. Return the maximum safeness factor of all paths leading to cell (n - 1, n - 1). An adjacent cell of cell (r, c), is one of the cells (r, c + 1), (r, c - 1), (r + 1, c) and (r - 1, c) if it exists. The Manhattan distance between two cells (a, b) and (x, y) is equal to |a - x| + |b - y|, where |val| denotes the absolute value of val. Example 1: [https://assets.leetcode.com/uploads/2023/07/02/example1.png] Input: grid = [[1,0,0],[0,0,0],[0,0,1]] Output: 0 Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). Example 2: [https://assets.leetcode.com/uploads/2023/07/02/example2.png] Input: grid = [[0,0,1],[0,0,0],[0,0,0]] Output: 2 Explanation: The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. Example 3: [https://assets.leetcode.com/uploads/2023/07/02/example3.png] Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] Output: 2 Explanation: The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. Constraints: * 1 <= grid.length == n <= 400 * grid[i].length == n * grid[i][j] is either 0 or 1. * There is at least one thief in the grid. """ class Solution: def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: * A cell containing a thief if grid[r][c] = 1 * An empty cell if grid[r][c] = 0 You are initially positioned at cell (0, 0). In one move, you can move to any adjacent cell in the grid, including cells containing thieves. The safeness factor of a path on the grid is defined as the minimum manhattan distance from any cell in the path to any thief in the grid. Return the maximum safeness factor of all paths leading to cell (n - 1, n - 1). An adjacent cell of cell (r, c), is one of the cells (r, c + 1), (r, c - 1), (r + 1, c) and (r - 1, c) if it exists. The Manhattan distance between two cells (a, b) and (x, y) is equal to |a - x| + |b - y|, where |val| denotes the absolute value of val. Example 1: [https://assets.leetcode.com/uploads/2023/07/02/example1.png] Input: grid = [[1,0,0],[0,0,0],[0,0,1]] Output: 0 Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). Example 2: [https://assets.leetcode.com/uploads/2023/07/02/example2.png] Input: grid = [[0,0,1],[0,0,0],[0,0,0]] Output: 2 Explanation: The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. Example 3: [https://assets.leetcode.com/uploads/2023/07/02/example3.png] Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] Output: 2 Explanation: The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. Constraints: * 1 <= grid.length == n <= 400 * grid[i].length == n * grid[i][j] is either 0 or 1. * There is at least one thief in the grid. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed 2D integer array items of length n and an integer k. items[i] = [profiti, categoryi], where profiti and categoryi denote the profit and category of the ith item respectively. Let's define the elegance of a subsequence of items as total_profit + distinct_categories2, where total_profit is the sum of all profits in the subsequence, and distinct_categories is the number of distinct categories from all the categories in the selected subsequence. Your task is to find the maximum elegance from all subsequences of size k in items. Return an integer denoting the maximum elegance of a subsequence of items with size exactly k. Note: A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. Example 1: Input: items = [[3,2],[5,1],[10,1]], k = 2 Output: 17 Explanation: In this example, we have to select a subsequence of size 2. We can select items[0] = [3,2] and items[2] = [10,1]. The total profit in this subsequence is 3 + 10 = 13, and the subsequence contains 2 distinct categories [2,1]. Hence, the elegance is 13 + 22 = 17, and we can show that it is the maximum achievable elegance. Example 2: Input: items = [[3,1],[3,1],[2,2],[5,3]], k = 3 Output: 19 Explanation: In this example, we have to select a subsequence of size 3. We can select items[0] = [3,1], items[2] = [2,2], and items[3] = [5,3]. The total profit in this subsequence is 3 + 2 + 5 = 10, and the subsequence contains 3 distinct categories [1,2,3]. Hence, the elegance is 10 + 32 = 19, and we can show that it is the maximum achievable elegance. Example 3: Input: items = [[1,1],[2,1],[3,1]], k = 3 Output: 7 Explanation: In this example, we have to select a subsequence of size 3. We should select all the items. The total profit will be 1 + 2 + 3 = 6, and the subsequence contains 1 distinct category [1]. Hence, the maximum elegance is 6 + 12 = 7. Constraints: * 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:
You are given a 0-indexed 2D integer array items of length n and an integer k. items[i] = [profiti, categoryi], where profiti and categoryi denote the profit and category of the ith item respectively. Let's define the elegance of a subsequence of items as total_profit + distinct_categories2, where total_profit is the sum of all profits in the subsequence, and distinct_categories is the number of distinct categories from all the categories in the selected subsequence. Your task is to find the maximum elegance from all subsequences of size k in items. Return an integer denoting the maximum elegance of a subsequence of items with size exactly k. Note: A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. Example 1: Input: items = [[3,2],[5,1],[10,1]], k = 2 Output: 17 Explanation: In this example, we have to select a subsequence of size 2. We can select items[0] = [3,2] and items[2] = [10,1]. The total profit in this subsequence is 3 + 10 = 13, and the subsequence contains 2 distinct categories [2,1]. Hence, the elegance is 13 + 22 = 17, and we can show that it is the maximum achievable elegance. Example 2: Input: items = [[3,1],[3,1],[2,2],[5,3]], k = 3 Output: 19 Explanation: In this example, we have to select a subsequence of size 3. We can select items[0] = [3,1], items[2] = [2,2], and items[3] = [5,3]. The total profit in this subsequence is 3 + 2 + 5 = 10, and the subsequence contains 3 distinct categories [1,2,3]. Hence, the elegance is 10 + 32 = 19, and we can show that it is the maximum achievable elegance. Example 3: Input: items = [[1,1],[2,1],[3,1]], k = 3 Output: 7 Explanation: In this example, we have to select a subsequence of size 3. We should select all the items. The total profit will be 1 + 2 + 3 = 6, and the subsequence contains 1 distinct category [1]. Hence, the maximum elegance is 6 + 12 = 7. Constraints: * 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 Please complete the code below to solve above prblem: ```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" }
""" Initially, you have a bank account balance of 100 dollars. You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars. At the store where you will make the purchase, the purchase amount is rounded to the nearest multiple of 10. In other words, you pay a non-negative amount, roundedAmount, such that roundedAmount is a multiple of 10 and abs(roundedAmount - purchaseAmount) is minimized. If there is more than one nearest multiple of 10, the largest multiple is chosen. Return an integer denoting your account balance after making a purchase worth purchaseAmount dollars from the store. Note: 0 is considered to be a multiple of 10 in this problem. Example 1: Input: purchaseAmount = 9 Output: 90 Explanation: In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90. Example 2: Input: purchaseAmount = 15 Output: 80 Explanation: In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen. Hence, your account balance becomes 100 - 20 = 80. Constraints: * 0 <= purchaseAmount <= 100 """ class Solution: def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
Initially, you have a bank account balance of 100 dollars. You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars. At the store where you will make the purchase, the purchase amount is rounded to the nearest multiple of 10. In other words, you pay a non-negative amount, roundedAmount, such that roundedAmount is a multiple of 10 and abs(roundedAmount - purchaseAmount) is minimized. If there is more than one nearest multiple of 10, the largest multiple is chosen. Return an integer denoting your account balance after making a purchase worth purchaseAmount dollars from the store. Note: 0 is considered to be a multiple of 10 in this problem. Example 1: Input: purchaseAmount = 9 Output: 90 Explanation: In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90. Example 2: Input: purchaseAmount = 15 Output: 80 Explanation: In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen. Hence, your account balance becomes 100 - 20 = 80. Constraints: * 0 <= purchaseAmount <= 100 Please complete the code below to solve above prblem: ```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" }
""" Given the head of a linked list head, in which each node contains an integer value. Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them. Return the linked list after insertion. The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers. Example 1: [https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png] Input: head = [18,6,10,3] Output: [18,6,6,2,10,1,3] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes). - We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes. - We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes. - We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes. There are no more adjacent nodes, so we return the linked list. Example 2: [https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png] Input: head = [7] Output: [7] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes. There are no pairs of adjacent nodes, so we return the initial linked list. Constraints: * The number of nodes in the list is in the range [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]:
Given the head of a linked list head, in which each node contains an integer value. Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them. Return the linked list after insertion. The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers. Example 1: [https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png] Input: head = [18,6,10,3] Output: [18,6,6,2,10,1,3] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes). - We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes. - We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes. - We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes. There are no more adjacent nodes, so we return the linked list. Example 2: [https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png] Input: head = [7] Output: [7] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes. There are no pairs of adjacent nodes, so we return the initial linked list. Constraints: * The number of nodes in the list is in the range [1, 5000]. * 1 <= Node.val <= 1000 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array nums containing n integers. At each second, you perform the following operation on the array: * For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n]. Note that all the elements get replaced simultaneously. Return the minimum number of seconds needed to make all elements in the array nums equal. Example 1: Input: nums = [1,2,1,2] Output: 1 Explanation: We can equalize the array in 1 second in the following way: - At 1st second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2]. It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array. Example 2: Input: nums = [2,1,3,3,2] Output: 2 Explanation: We can equalize the array in 2 seconds in the following way: - At 1st second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3]. - At 2nd second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3]. It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array. Example 3: Input: nums = [5,5,5,5] Output: 0 Explanation: We don't need to perform any operations as all elements in the initial array are the same. Constraints: * 1 <= n == nums.length <= 105 * 1 <= nums[i] <= 109 """ class Solution: def minimumSeconds(self, nums: List[int]) -> int:
You are given a 0-indexed array nums containing n integers. At each second, you perform the following operation on the array: * For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n]. Note that all the elements get replaced simultaneously. Return the minimum number of seconds needed to make all elements in the array nums equal. Example 1: Input: nums = [1,2,1,2] Output: 1 Explanation: We can equalize the array in 1 second in the following way: - At 1st second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2]. It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array. Example 2: Input: nums = [2,1,3,3,2] Output: 2 Explanation: We can equalize the array in 2 seconds in the following way: - At 1st second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3]. - At 2nd second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3]. It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array. Example 3: Input: nums = [5,5,5,5] Output: 0 Explanation: We don't need to perform any operations as all elements in the initial array are the same. Constraints: * 1 <= n == nums.length <= 105 * 1 <= nums[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given two 0-indexed integer arrays nums1 and nums2 of equal length. Every second, for all indices 0 <= i < nums1.length, value of nums1[i] is incremented by nums2[i]. After this is done, you can do the following operation: * Choose an index 0 <= i < nums1.length and make nums1[i] = 0. You are also given an integer x. Return the minimum time in which you can make the sum of all elements of nums1 to be less than or equal to x, or -1 if this is not possible. Example 1: Input: nums1 = [1,2,3], nums2 = [1,2,3], x = 4 Output: 3 Explanation: For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6]. For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9]. For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0]. Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3. Example 2: Input: nums1 = [1,2,3], nums2 = [3,3,3], x = 4 Output: -1 Explanation: It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed. Constraints: * 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:
You are given two 0-indexed integer arrays nums1 and nums2 of equal length. Every second, for all indices 0 <= i < nums1.length, value of nums1[i] is incremented by nums2[i]. After this is done, you can do the following operation: * Choose an index 0 <= i < nums1.length and make nums1[i] = 0. You are also given an integer x. Return the minimum time in which you can make the sum of all elements of nums1 to be less than or equal to x, or -1 if this is not possible. Example 1: Input: nums1 = [1,2,3], nums2 = [1,2,3], x = 4 Output: 3 Explanation: For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6]. For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9]. For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0]. Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3. Example 2: Input: nums1 = [1,2,3], nums2 = [3,3,3], x = 4 Output: -1 Explanation: It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed. Constraints: * 1 <= nums1.length <= 103 * 1 <= nums1[i] <= 103 * 0 <= nums2[i] <= 103 * nums1.length == nums2.length * 0 <= x <= 106 Please complete the code below to solve above prblem: ```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" }
""" There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company. The company requires each employee to work for at least target hours. You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target. Return the integer denoting the number of employees who worked at least target hours. Example 1: Input: hours = [0,1,2,3,4], target = 2 Output: 3 Explanation: The company wants each employee to work for at least 2 hours. - Employee 0 worked for 0 hours and didn't meet the target. - Employee 1 worked for 1 hours and didn't meet the target. - Employee 2 worked for 2 hours and met the target. - Employee 3 worked for 3 hours and met the target. - Employee 4 worked for 4 hours and met the target. There are 3 employees who met the target. Example 2: Input: hours = [5,1,4,2,2], target = 6 Output: 0 Explanation: The company wants each employee to work for at least 6 hours. There are 0 employees who met the target. Constraints: * 1 <= n == hours.length <= 50 * 0 <= hours[i], target <= 105 """ class Solution: def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company. The company requires each employee to work for at least target hours. You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target. Return the integer denoting the number of employees who worked at least target hours. Example 1: Input: hours = [0,1,2,3,4], target = 2 Output: 3 Explanation: The company wants each employee to work for at least 2 hours. - Employee 0 worked for 0 hours and didn't meet the target. - Employee 1 worked for 1 hours and didn't meet the target. - Employee 2 worked for 2 hours and met the target. - Employee 3 worked for 3 hours and met the target. - Employee 4 worked for 4 hours and met the target. There are 3 employees who met the target. Example 2: Input: hours = [5,1,4,2,2], target = 6 Output: 0 Explanation: The company wants each employee to work for at least 6 hours. There are 0 employees who met the target. Constraints: * 1 <= n == hours.length <= 50 * 0 <= hours[i], target <= 105 Please complete the code below to solve above prblem: ```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" }
""" You are given an array nums consisting of positive integers. We call a subarray of an array complete if the following condition is satisfied: * The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array. Return the number of complete subarrays. A subarray is a contiguous non-empty part of an array. Example 1: Input: nums = [1,3,1,2,2] Output: 4 Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2]. Example 2: Input: nums = [5,5,5,5] Output: 10 Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 2000 """ class Solution: def countCompleteSubarrays(self, nums: List[int]) -> int:
You are given an array nums consisting of positive integers. We call a subarray of an array complete if the following condition is satisfied: * The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array. Return the number of complete subarrays. A subarray is a contiguous non-empty part of an array. Example 1: Input: nums = [1,3,1,2,2] Output: 4 Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2]. Example 2: Input: nums = [5,5,5,5] Output: 10 Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 2000 Please complete the code below to solve above prblem: ```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" }
""" Given three strings a, b, and c, your task is to find a string that has the minimum length and contains all three strings as substrings. If there are multiple such strings, return the lexicographically smallest one. Return a string denoting the answer to the problem. Notes * A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. * A substring is a contiguous sequence of characters within a string. Example 1: Input: a = "abc", b = "bca", c = "aaa" Output: "aaabca" Explanation: We show that "aaabca" contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and "aaabca" is the lexicographically smallest one. Example 2: Input: a = "ab", b = "ba", c = "aba" Output: "aba" Explanation: We show that the string "aba" contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that "aba" is the lexicographically smallest one. Constraints: * 1 <= a.length, b.length, c.length <= 100 * a, b, c consist only of lowercase English letters. """ class Solution: def minimumString(self, a: str, b: str, c: str) -> str:
Given three strings a, b, and c, your task is to find a string that has the minimum length and contains all three strings as substrings. If there are multiple such strings, return the lexicographically smallest one. Return a string denoting the answer to the problem. Notes * A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. * A substring is a contiguous sequence of characters within a string. Example 1: Input: a = "abc", b = "bca", c = "aaa" Output: "aaabca" Explanation: We show that "aaabca" contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and "aaabca" is the lexicographically smallest one. Example 2: Input: a = "ab", b = "ba", c = "aba" Output: "aba" Explanation: We show that the string "aba" contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that "aba" is the lexicographically smallest one. Constraints: * 1 <= a.length, b.length, c.length <= 100 * a, b, c consist only of lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" Given two positive integers low and high represented as strings, find the count of stepping numbers in the inclusive range [low, high]. A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1. Return an integer denoting the count of stepping numbers in the inclusive range [low, high]. Since the answer may be very large, return it modulo 109 + 7. Note: A stepping number should not have a leading zero. Example 1: Input: low = "1", high = "11" Output: 10 Explanation: The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10. Example 2: Input: low = "90", high = "101" Output: 2 Explanation: The stepping numbers in the range [90,101] are 98 and 101. There are a total of 2 stepping numbers in the range. Hence, the output is 2. Constraints: * 1 <= int(low) <= int(high) < 10100 * 1 <= low.length, high.length <= 100 * low and high consist of only digits. * low and high don't have any leading zeros. """ class Solution: def countSteppingNumbers(self, low: str, high: str) -> int:
Given two positive integers low and high represented as strings, find the count of stepping numbers in the inclusive range [low, high]. A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1. Return an integer denoting the count of stepping numbers in the inclusive range [low, high]. Since the answer may be very large, return it modulo 109 + 7. Note: A stepping number should not have a leading zero. Example 1: Input: low = "1", high = "11" Output: 10 Explanation: The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10. Example 2: Input: low = "90", high = "101" Output: 2 Explanation: The stepping numbers in the range [90,101] are 98 and 101. There are a total of 2 stepping numbers in the range. Hence, the output is 2. Constraints: * 1 <= int(low) <= int(high) < 10100 * 1 <= low.length, high.length <= 100 * low and high consist of only digits. * low and high don't have any leading zeros. Please complete the code below to solve above prblem: ```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" }
""" Given an array of strings words and a character separator, split each string in words by separator. Return an array of strings containing the new strings formed after the splits, excluding empty strings. Notes * separator is used to determine where the split should occur, but it is not included as part of the resulting strings. * A split may result in more than two strings. * The resulting strings must maintain the same order as they were initially given. Example 1: Input: words = ["one.two.three","four.five","six"], separator = "." Output: ["one","two","three","four","five","six"] Explanation: In this example we split as follows: "one.two.three" splits into "one", "two", "three" "four.five" splits into "four", "five" "six" splits into "six" Hence, the resulting array is ["one","two","three","four","five","six"]. Example 2: Input: words = ["$easy$","$problem$"], separator = "$" Output: ["easy","problem"] Explanation: In this example we split as follows: "$easy$" splits into "easy" (excluding empty strings) "$problem$" splits into "problem" (excluding empty strings) Hence, the resulting array is ["easy","problem"]. Example 3: Input: words = ["|||"], separator = "|" Output: [] Explanation: In this example the resulting split of "|||" will contain only empty strings, so we return an empty array []. Constraints: * 1 <= words.length <= 100 * 1 <= words[i].length <= 20 * characters in words[i] are either lowercase English letters or characters from the string ".,|$#@" (excluding the quotes) * separator is a character from the string ".,|$#@" (excluding the quotes) """ class Solution: def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
Given an array of strings words and a character separator, split each string in words by separator. Return an array of strings containing the new strings formed after the splits, excluding empty strings. Notes * separator is used to determine where the split should occur, but it is not included as part of the resulting strings. * A split may result in more than two strings. * The resulting strings must maintain the same order as they were initially given. Example 1: Input: words = ["one.two.three","four.five","six"], separator = "." Output: ["one","two","three","four","five","six"] Explanation: In this example we split as follows: "one.two.three" splits into "one", "two", "three" "four.five" splits into "four", "five" "six" splits into "six" Hence, the resulting array is ["one","two","three","four","five","six"]. Example 2: Input: words = ["$easy$","$problem$"], separator = "$" Output: ["easy","problem"] Explanation: In this example we split as follows: "$easy$" splits into "easy" (excluding empty strings) "$problem$" splits into "problem" (excluding empty strings) Hence, the resulting array is ["easy","problem"]. Example 3: Input: words = ["|||"], separator = "|" Output: [] Explanation: In this example the resulting split of "|||" will contain only empty strings, so we return an empty array []. Constraints: * 1 <= words.length <= 100 * 1 <= words[i].length <= 20 * characters in words[i] are either lowercase English letters or characters from the string ".,|$#@" (excluding the quotes) * separator is a character from the string ".,|$#@" (excluding the quotes) Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array nums consisting of positive integers. You can do the following operation on the array any number of times: * Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array. Return the value of the largest element that you can possibly obtain in the final array. Example 1: Input: nums = [2,3,7,9,3] Output: 21 Explanation: We can apply the following operations on the array: - Choose i = 0. The resulting array will be nums = [5,7,9,3]. - Choose i = 1. The resulting array will be nums = [5,16,3]. - Choose i = 0. The resulting array will be nums = [21,3]. The largest element in the final array is 21. It can be shown that we cannot obtain a larger element. Example 2: Input: nums = [5,3,3] Output: 11 Explanation: We can do the following operations on the array: - Choose i = 1. The resulting array will be nums = [5,6]. - Choose i = 0. The resulting array will be nums = [11]. There is only one element in the final array, which is 11. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 106 """ class Solution: def maxArrayValue(self, nums: List[int]) -> int:
You are given a 0-indexed array nums consisting of positive integers. You can do the following operation on the array any number of times: * Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array. Return the value of the largest element that you can possibly obtain in the final array. Example 1: Input: nums = [2,3,7,9,3] Output: 21 Explanation: We can apply the following operations on the array: - Choose i = 0. The resulting array will be nums = [5,7,9,3]. - Choose i = 1. The resulting array will be nums = [5,16,3]. - Choose i = 0. The resulting array will be nums = [21,3]. The largest element in the final array is 21. It can be shown that we cannot obtain a larger element. Example 2: Input: nums = [5,3,3] Output: 11 Explanation: We can do the following operations on the array: - Choose i = 1. The resulting array will be nums = [5,6]. - Choose i = 0. The resulting array will be nums = [11]. There is only one element in the final array, which is 11. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 106 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array usageLimits of length n. Your task is to create groups using numbers from 0 to n - 1, ensuring that each number, i, is used no more than usageLimits[i] times in total across all groups. You must also satisfy the following conditions: * Each group must consist of distinct numbers, meaning that no duplicate numbers are allowed within a single group. * Each group (except the first one) must have a length strictly greater than the previous group. Return an integer denoting the maximum number of groups you can create while satisfying these conditions. Example 1: Input: usageLimits = [1,2,5] Output: 3 Explanation: In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [2]. Group 2 contains the numbers [1,2]. Group 3 contains the numbers [0,1,2]. It can be shown that the maximum number of groups is 3. So, the output is 3. Example 2: Input: usageLimits = [2,1,2] Output: 2 Explanation: In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. Group 2 contains the numbers [1,2]. It can be shown that the maximum number of groups is 2. So, the output is 2. Example 3: Input: usageLimits = [1,1] Output: 1 Explanation: In this example, we can use both 0 and 1 at most once. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. It can be shown that the maximum number of groups is 1. So, the output is 1. Constraints: * 1 <= usageLimits.length <= 105 * 1 <= usageLimits[i] <= 109 """ class Solution: def maxIncreasingGroups(self, usageLimits: List[int]) -> int:
You are given a 0-indexed array usageLimits of length n. Your task is to create groups using numbers from 0 to n - 1, ensuring that each number, i, is used no more than usageLimits[i] times in total across all groups. You must also satisfy the following conditions: * Each group must consist of distinct numbers, meaning that no duplicate numbers are allowed within a single group. * Each group (except the first one) must have a length strictly greater than the previous group. Return an integer denoting the maximum number of groups you can create while satisfying these conditions. Example 1: Input: usageLimits = [1,2,5] Output: 3 Explanation: In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [2]. Group 2 contains the numbers [1,2]. Group 3 contains the numbers [0,1,2]. It can be shown that the maximum number of groups is 3. So, the output is 3. Example 2: Input: usageLimits = [2,1,2] Output: 2 Explanation: In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. Group 2 contains the numbers [1,2]. It can be shown that the maximum number of groups is 2. So, the output is 2. Example 3: Input: usageLimits = [1,1] Output: 1 Explanation: In this example, we can use both 0 and 1 at most once. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. It can be shown that the maximum number of groups is 1. So, the output is 1. Constraints: * 1 <= usageLimits.length <= 105 * 1 <= usageLimits[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1. You are also given a string s of length n, where s[i] is the character assigned to the edge between i and parent[i]. s[0] can be ignored. Return the number of pairs of nodes (u, v) such that u < v and the characters assigned to edges on the path from u to v can be rearranged to form a palindrome. A string is a palindrome when it reads the same backwards as forwards. Example 1: [https://assets.leetcode.com/uploads/2023/07/15/treedrawio-8drawio.png] Input: parent = [-1,0,0,1,1,2], s = "acaabc" Output: 8 Explanation: The valid pairs are: - All the pairs (0,1), (0,2), (1,3), (1,4) and (2,5) result in one character which is always a palindrome. - The pair (2,3) result in the string "aca" which is a palindrome. - The pair (1,5) result in the string "cac" which is a palindrome. - The pair (3,5) result in the string "acac" which can be rearranged into the palindrome "acca". Example 2: Input: parent = [-1,0,0,0,0], s = "aaaaa" Output: 10 Explanation: Any pair of nodes (u,v) where u < v is valid. Constraints: * n == parent.length == s.length * 1 <= n <= 105 * 0 <= parent[i] <= n - 1 for all i >= 1 * parent[0] == -1 * parent represents a valid tree. * s consists of only lowercase English letters. """ class Solution: def countPalindromePaths(self, parent: List[int], s: str) -> int:
You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1. You are also given a string s of length n, where s[i] is the character assigned to the edge between i and parent[i]. s[0] can be ignored. Return the number of pairs of nodes (u, v) such that u < v and the characters assigned to edges on the path from u to v can be rearranged to form a palindrome. A string is a palindrome when it reads the same backwards as forwards. Example 1: [https://assets.leetcode.com/uploads/2023/07/15/treedrawio-8drawio.png] Input: parent = [-1,0,0,1,1,2], s = "acaabc" Output: 8 Explanation: The valid pairs are: - All the pairs (0,1), (0,2), (1,3), (1,4) and (2,5) result in one character which is always a palindrome. - The pair (2,3) result in the string "aca" which is a palindrome. - The pair (1,5) result in the string "cac" which is a palindrome. - The pair (3,5) result in the string "acac" which can be rearranged into the palindrome "acca". Example 2: Input: parent = [-1,0,0,0,0], s = "aaaaa" Output: 10 Explanation: Any pair of nodes (u,v) where u < v is valid. Constraints: * n == parent.length == s.length * 1 <= n <= 105 * 0 <= parent[i] <= n - 1 for all i >= 1 * parent[0] == -1 * parent represents a valid tree. * s consists of only lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3]. Return true if the given array is good, otherwise return false. Note: A permutation of integers represents an arrangement of these numbers. Example 1: Input: nums = [2, 1, 3] Output: false Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false. Example 2: Input: nums = [1, 3, 3, 2] Output: true Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true. Example 3: Input: nums = [1, 1] Output: true Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true. Example 4: Input: nums = [3, 4, 4, 1, 2, 1] Output: false Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false. Constraints: * 1 <= nums.length <= 100 * 1 <= num[i] <= 200 """ class Solution: def isGood(self, nums: List[int]) -> bool:
You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3]. Return true if the given array is good, otherwise return false. Note: A permutation of integers represents an arrangement of these numbers. Example 1: Input: nums = [2, 1, 3] Output: false Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false. Example 2: Input: nums = [1, 3, 3, 2] Output: true Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true. Example 3: Input: nums = [1, 1] Output: true Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true. Example 4: Input: nums = [3, 4, 4, 1, 2, 1] Output: false Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false. Constraints: * 1 <= nums.length <= 100 * 1 <= num[i] <= 200 Please complete the code below to solve above prblem: ```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" }
""" Given a 0-indexed string s, permute s to get a new string t such that: * All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i]. * The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j]. Return the resulting string. The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels. Example 1: Input: s = "lEetcOde" Output: "lEOtcede" Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. Example 2: Input: s = "lYmpH" Output: "lYmpH" Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH". Constraints: * 1 <= s.length <= 105 * s consists only of letters of the English alphabet in uppercase and lowercase. """ class Solution: def sortVowels(self, s: str) -> str:
Given a 0-indexed string s, permute s to get a new string t such that: * All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i]. * The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j]. Return the resulting string. The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels. Example 1: Input: s = "lEetcOde" Output: "lEOtcede" Explanation: 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. Example 2: Input: s = "lYmpH" Output: "lYmpH" Explanation: There are no vowels in s (all characters in s are consonants), so we return "lYmpH". Constraints: * 1 <= s.length <= 105 * s consists only of letters of the English alphabet in uppercase and lowercase. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums and a positive integer x. You are initially at position 0 in the array and you can visit other positions according to the following rules: * If you are currently in position i, then you can move to any position j such that i < j. * For each position i that you visit, you get a score of nums[i]. * If you move from a position i to a position j and the parities of nums[i] and nums[j] differ, then you lose a score of x. Return the maximum total score you can get. Note that initially you have nums[0] points. Example 1: Input: nums = [2,3,6,1,9,2], x = 5 Output: 13 Explanation: We can visit the following positions in the array: 0 -> 2 -> 3 -> 4. The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5. The total score will be: 2 + 6 + 1 + 9 - 5 = 13. Example 2: Input: nums = [2,4,6,8], x = 3 Output: 20 Explanation: All the integers in the array have the same parities, so we can visit all of them without losing any score. The total score is: 2 + 4 + 6 + 8 = 20. Constraints: * 2 <= nums.length <= 105 * 1 <= nums[i], x <= 106 """ class Solution: def maxScore(self, nums: List[int], x: int) -> int:
You are given a 0-indexed integer array nums and a positive integer x. You are initially at position 0 in the array and you can visit other positions according to the following rules: * If you are currently in position i, then you can move to any position j such that i < j. * For each position i that you visit, you get a score of nums[i]. * If you move from a position i to a position j and the parities of nums[i] and nums[j] differ, then you lose a score of x. Return the maximum total score you can get. Note that initially you have nums[0] points. Example 1: Input: nums = [2,3,6,1,9,2], x = 5 Output: 13 Explanation: We can visit the following positions in the array: 0 -> 2 -> 3 -> 4. The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5. The total score will be: 2 + 6 + 1 + 9 - 5 = 13. Example 2: Input: nums = [2,4,6,8], x = 3 Output: 20 Explanation: All the integers in the array have the same parities, so we can visit all of them without losing any score. The total score is: 2 + 4 + 6 + 8 = 20. Constraints: * 2 <= nums.length <= 105 * 1 <= nums[i], x <= 106 Please complete the code below to solve above prblem: ```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" }
""" Given two positive integers n and x. Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx. Since the result can be very large, return it modulo 109 + 7. For example, if n = 160 and x = 3, one way to express n is n = 23 + 33 + 53. Example 1: Input: n = 10, x = 2 Output: 1 Explanation: We can express n as the following: n = 32 + 12 = 10. It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers. Example 2: Input: n = 4, x = 1 Output: 2 Explanation: We can express n in the following ways: - n = 41 = 4. - n = 31 + 11 = 4. Constraints: * 1 <= n <= 300 * 1 <= x <= 5 """ class Solution: def numberOfWays(self, n: int, x: int) -> int:
Given two positive integers n and x. Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx. Since the result can be very large, return it modulo 109 + 7. For example, if n = 160 and x = 3, one way to express n is n = 23 + 33 + 53. Example 1: Input: n = 10, x = 2 Output: 1 Explanation: We can express n as the following: n = 32 + 12 = 10. It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers. Example 2: Input: n = 4, x = 1 Output: 2 Explanation: We can express n in the following ways: - n = 41 = 4. - n = 31 + 11 = 4. Constraints: * 1 <= n <= 300 * 1 <= x <= 5 Please complete the code below to solve above prblem: ```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" }
""" You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums. Example 1: Input: nums = [1,2,3,4] Output: 21 Explanation: There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21. Example 2: Input: nums = [2,7,1,19,18,3] Output: 63 Explanation: There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6. Hence, the sum of the squares of all special elements of nums is 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. Constraints: * 1 <= nums.length == n <= 50 * 1 <= nums[i] <= 50 """ class Solution: def sumOfSquares(self, nums: List[int]) -> int:
You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums. Example 1: Input: nums = [1,2,3,4] Output: 21 Explanation: There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21. Example 2: Input: nums = [2,7,1,19,18,3] Output: 63 Explanation: There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6. Hence, the sum of the squares of all special elements of nums is 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. Constraints: * 1 <= nums.length == n <= 50 * 1 <= nums[i] <= 50 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array nums and a non-negative integer k. In one operation, you can do the following: * Choose an index i that hasn't been chosen before from the range [0, nums.length - 1]. * Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k]. The beauty of the array is the length of the longest subsequence consisting of equal elements. Return the maximum possible beauty of the array nums after applying the operation any number of times. Note that you can apply the operation to each index only once. A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements. Example 1: Input: nums = [4,6,1,2], k = 2 Output: 3 Explanation: In this example, we apply the following operations: - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2]. - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4]. After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3). It can be proven that 3 is the maximum possible length we can achieve. Example 2: Input: nums = [1,1,1,1], k = 10 Output: 4 Explanation: In this example we don't have to apply any operations. The beauty of the array nums is 4 (whole array). Constraints: * 1 <= nums.length <= 105 * 0 <= nums[i], k <= 105 """ class Solution: def maximumBeauty(self, nums: List[int], k: int) -> int:
You are given a 0-indexed array nums and a non-negative integer k. In one operation, you can do the following: * Choose an index i that hasn't been chosen before from the range [0, nums.length - 1]. * Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k]. The beauty of the array is the length of the longest subsequence consisting of equal elements. Return the maximum possible beauty of the array nums after applying the operation any number of times. Note that you can apply the operation to each index only once. A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements. Example 1: Input: nums = [4,6,1,2], k = 2 Output: 3 Explanation: In this example, we apply the following operations: - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2]. - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4]. After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3). It can be proven that 3 is the maximum possible length we can achieve. Example 2: Input: nums = [1,1,1,1], k = 10 Output: 4 Explanation: In this example we don't have to apply any operations. The beauty of the array nums is 4 (whole array). Constraints: * 1 <= nums.length <= 105 * 0 <= nums[i], k <= 105 Please complete the code below to solve above prblem: ```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" }
""" An element x of an integer array arr of length m is dominant if freq(x) * 2 > m, where freq(x) is the number of occurrences of x in arr. Note that this definition implies that arr can have at most one dominant element. You are given a 0-indexed integer array nums of length n with one dominant element. You can split nums at an index i into two arrays nums[0, ..., i] and nums[i + 1, ..., n - 1], but the split is only valid if: * 0 <= i < n - 1 * nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element. Here, nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j, both ends being inclusive. Particularly, if j < i then nums[i, ..., j] denotes an empty subarray. Return the minimum index of a valid split. If no valid split exists, return -1. Example 1: Input: nums = [1,2,2,2] Output: 2 Explanation: We can split the array at index 2 to obtain arrays [1,2,2] and [2]. In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3. In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1. Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split. It can be shown that index 2 is the minimum index of a valid split. Example 2: Input: nums = [2,1,3,1,1,1,7,1,2,1] Output: 4 Explanation: We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1]. In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split. It can be shown that index 4 is the minimum index of a valid split. Example 3: Input: nums = [3,3,3,3,7,2,2] Output: -1 Explanation: It can be shown that there is no valid split. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 * nums has exactly one dominant element. """ class Solution: def minimumIndex(self, nums: List[int]) -> int:
An element x of an integer array arr of length m is dominant if freq(x) * 2 > m, where freq(x) is the number of occurrences of x in arr. Note that this definition implies that arr can have at most one dominant element. You are given a 0-indexed integer array nums of length n with one dominant element. You can split nums at an index i into two arrays nums[0, ..., i] and nums[i + 1, ..., n - 1], but the split is only valid if: * 0 <= i < n - 1 * nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element. Here, nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j, both ends being inclusive. Particularly, if j < i then nums[i, ..., j] denotes an empty subarray. Return the minimum index of a valid split. If no valid split exists, return -1. Example 1: Input: nums = [1,2,2,2] Output: 2 Explanation: We can split the array at index 2 to obtain arrays [1,2,2] and [2]. In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3. In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1. Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split. It can be shown that index 2 is the minimum index of a valid split. Example 2: Input: nums = [2,1,3,1,1,1,7,1,2,1] Output: 4 Explanation: We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1]. In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split. It can be shown that index 4 is the minimum index of a valid split. Example 3: Input: nums = [3,3,3,3,7,2,2] Output: -1 Explanation: It can be shown that there is no valid split. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 * nums has exactly one dominant element. Please complete the code below to solve above prblem: ```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" }
""" You are given a string word and an array of strings forbidden. A string is called valid if none of its substrings are present in forbidden. Return the length of the longest valid substring of the string word. A substring is a contiguous sequence of characters in a string, possibly empty. Example 1: Input: word = "cbaaaabc", forbidden = ["aaa","cb"] Output: 4 Explanation: There are 11 valid substrings in word: "c", "b", "a", "ba", "aa", "bc", "baa", "aab", "ab", "abc" and "aabc". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "aaa" or "cb" as a substring. Example 2: Input: word = "leetcode", forbidden = ["de","le","e"] Output: 4 Explanation: There are 11 valid substrings in word: "l", "t", "c", "o", "d", "tc", "co", "od", "tco", "cod", and "tcod". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "de", "le", or "e" as a substring. Constraints: * 1 <= word.length <= 105 * word consists only of lowercase English letters. * 1 <= forbidden.length <= 105 * 1 <= forbidden[i].length <= 10 * forbidden[i] consists only of lowercase English letters. """ class Solution: def longestValidSubstring(self, word: str, forbidden: List[str]) -> int:
You are given a string word and an array of strings forbidden. A string is called valid if none of its substrings are present in forbidden. Return the length of the longest valid substring of the string word. A substring is a contiguous sequence of characters in a string, possibly empty. Example 1: Input: word = "cbaaaabc", forbidden = ["aaa","cb"] Output: 4 Explanation: There are 11 valid substrings in word: "c", "b", "a", "ba", "aa", "bc", "baa", "aab", "ab", "abc" and "aabc". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "aaa" or "cb" as a substring. Example 2: Input: word = "leetcode", forbidden = ["de","le","e"] Output: 4 Explanation: There are 11 valid substrings in word: "l", "t", "c", "o", "d", "tc", "co", "od", "tco", "cod", and "tcod". The length of the longest valid substring is 4. It can be shown that all other substrings contain either "de", "le", or "e" as a substring. Constraints: * 1 <= word.length <= 105 * word consists only of lowercase English letters. * 1 <= forbidden.length <= 105 * 1 <= forbidden[i].length <= 10 * forbidden[i] consists only of lowercase English letters. Please complete the code below to solve above prblem: ```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" }
""" You are given two integers, num and t. An integer x is called achievable if it can become equal to num after applying the following operation no more than t times: * Increase or decrease x by 1, and simultaneously increase or decrease num by 1. Return the maximum possible achievable number. It can be proven that there exists at least one achievable number. Example 1: Input: num = 4, t = 1 Output: 6 Explanation: The maximum achievable number is x = 6; it can become equal to num after performing this operation: 1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. It can be proven that there is no achievable number larger than 6. Example 2: Input: num = 3, t = 2 Output: 7 Explanation: The maximum achievable number is x = 7; after performing these operations, x will equal num: 1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4. 2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. It can be proven that there is no achievable number larger than 7. Constraints: * 1 <= num, t <= 50 """ class Solution: def theMaximumAchievableX(self, num: int, t: int) -> int:
You are given two integers, num and t. An integer x is called achievable if it can become equal to num after applying the following operation no more than t times: * Increase or decrease x by 1, and simultaneously increase or decrease num by 1. Return the maximum possible achievable number. It can be proven that there exists at least one achievable number. Example 1: Input: num = 4, t = 1 Output: 6 Explanation: The maximum achievable number is x = 6; it can become equal to num after performing this operation: 1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. It can be proven that there is no achievable number larger than 6. Example 2: Input: num = 3, t = 2 Output: 7 Explanation: The maximum achievable number is x = 7; after performing these operations, x will equal num: 1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4. 2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. It can be proven that there is no achievable number larger than 7. Constraints: * 1 <= num, t <= 50 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0. In one step, you can jump from index i to any index j such that: * 0 <= i < j < n * -target <= nums[j] - nums[i] <= target Return the maximum number of jumps you can make to reach index n - 1. If there is no way to reach index n - 1, return -1. Example 1: Input: nums = [1,3,6,4,1,2], target = 2 Output: 3 Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 3. - Jump from index 3 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3. Example 2: Input: nums = [1,3,6,4,1,2], target = 3 Output: 5 Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 2. - Jump from index 2 to index 3. - Jump from index 3 to index 4. - Jump from index 4 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5. Example 3: Input: nums = [1,3,6,4,1,2], target = 0 Output: -1 Explanation: It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1. Constraints: * 2 <= nums.length == n <= 1000 * -109 <= nums[i] <= 109 * 0 <= target <= 2 * 109 """ class Solution: def maximumJumps(self, nums: List[int], target: int) -> int:
You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0. In one step, you can jump from index i to any index j such that: * 0 <= i < j < n * -target <= nums[j] - nums[i] <= target Return the maximum number of jumps you can make to reach index n - 1. If there is no way to reach index n - 1, return -1. Example 1: Input: nums = [1,3,6,4,1,2], target = 2 Output: 3 Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 3. - Jump from index 3 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3. Example 2: Input: nums = [1,3,6,4,1,2], target = 3 Output: 5 Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 2. - Jump from index 2 to index 3. - Jump from index 3 to index 4. - Jump from index 4 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5. Example 3: Input: nums = [1,3,6,4,1,2], target = 0 Output: -1 Explanation: It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1. Constraints: * 2 <= nums.length == n <= 1000 * -109 <= nums[i] <= 109 * 0 <= target <= 2 * 109 Please complete the code below to solve above prblem: ```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" }
""" You are given two 0-indexed integer arrays nums1 and nums2 of length n. Let's define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i]. Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally. Return an integer representing the length of the longest non-decreasing subarray in nums3. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums1 = [2,3,1], nums2 = [1,2,1] Output: 2 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]. The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2. We can show that 2 is the maximum achievable length. Example 2: Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4] Output: 4 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]. The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length. Example 3: Input: nums1 = [1,1], nums2 = [2,2] Output: 2 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums1[1]] => [1,1]. The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length. Constraints: * 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:
You are given two 0-indexed integer arrays nums1 and nums2 of length n. Let's define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i]. Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally. Return an integer representing the length of the longest non-decreasing subarray in nums3. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums1 = [2,3,1], nums2 = [1,2,1] Output: 2 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]. The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2. We can show that 2 is the maximum achievable length. Example 2: Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4] Output: 4 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]. The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length. Example 3: Input: nums1 = [1,1], nums2 = [2,2] Output: 2 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums1[1]] => [1,1]. The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length. Constraints: * 1 <= nums1.length == nums2.length == n <= 105 * 1 <= nums1[i], nums2[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums and a positive integer k. You can apply the following operation on the array any number of times: * Choose any subarray of size k from the array and decrease all its elements by 1. Return true if you can make all the array elements equal to 0, or false otherwise. A subarray is a contiguous non-empty part of an array. Example 1: Input: nums = [2,2,3,1,1,0], k = 3 Output: true Explanation: We can do the following operations: - Choose the subarray [2,2,3]. The resulting array will be nums = [1,1,2,1,1,0]. - Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,1,0,0,0]. - Choose the subarray [1,1,1]. The resulting array will be nums = [0,0,0,0,0,0]. Example 2: Input: nums = [1,3,1,1], k = 2 Output: false Explanation: It is not possible to make all the array elements equal to 0. Constraints: * 1 <= k <= nums.length <= 105 * 0 <= nums[i] <= 106 """ class Solution: def checkArray(self, nums: List[int], k: int) -> bool:
You are given a 0-indexed integer array nums and a positive integer k. You can apply the following operation on the array any number of times: * Choose any subarray of size k from the array and decrease all its elements by 1. Return true if you can make all the array elements equal to 0, or false otherwise. A subarray is a contiguous non-empty part of an array. Example 1: Input: nums = [2,2,3,1,1,0], k = 3 Output: true Explanation: We can do the following operations: - Choose the subarray [2,2,3]. The resulting array will be nums = [1,1,2,1,1,0]. - Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,1,0,0,0]. - Choose the subarray [1,1,1]. The resulting array will be nums = [0,0,0,0,0,0]. Example 2: Input: nums = [1,3,1,1], k = 2 Output: false Explanation: It is not possible to make all the array elements equal to 0. Constraints: * 1 <= k <= nums.length <= 105 * 0 <= nums[i] <= 106 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if: * m is greater than 1. * s1 = s0 + 1. * The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m. Return the maximum length of all alternating subarrays present in nums or -1 if no such subarray exists. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2,3,4,3,4] Output: 4 Explanation: The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4. Example 2: Input: nums = [4,5,6] Output: 2 Explanation: [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2. Constraints: * 2 <= nums.length <= 100 * 1 <= nums[i] <= 104 """ class Solution: def alternatingSubarray(self, nums: List[int]) -> int:
You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if: * m is greater than 1. * s1 = s0 + 1. * The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m. Return the maximum length of all alternating subarrays present in nums or -1 if no such subarray exists. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2,3,4,3,4] Output: 4 Explanation: The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4. Example 2: Input: nums = [4,5,6] Output: 2 Explanation: [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2. Constraints: * 2 <= nums.length <= 100 * 1 <= nums[i] <= 104 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums representing the initial positions of some marbles. You are also given two 0-indexed integer arrays moveFrom and moveTo of equal length. Throughout moveFrom.length steps, you will change the positions of the marbles. On the ith step, you will move all marbles at position moveFrom[i] to position moveTo[i]. After completing all the steps, return the sorted list of occupied positions. Notes: * We call a position occupied if there is at least one marble in that position. * There may be multiple marbles in a single position. Example 1: Input: nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5] Output: [5,6,8,9] Explanation: Initially, the marbles are at positions 1,6,7,8. At the i = 0th step, we move the marbles at position 1 to position 2. Then, positions 2,6,7,8 are occupied. At the i = 1st step, we move the marbles at position 7 to position 9. Then, positions 2,6,8,9 are occupied. At the i = 2nd step, we move the marbles at position 2 to position 5. Then, positions 5,6,8,9 are occupied. At the end, the final positions containing at least one marbles are [5,6,8,9]. Example 2: Input: nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2] Output: [2] Explanation: Initially, the marbles are at positions [1,1,3,3]. At the i = 0th step, we move all the marbles at position 1 to position 2. Then, the marbles are at positions [2,2,3,3]. At the i = 1st step, we move all the marbles at position 3 to position 2. Then, the marbles are at positions [2,2,2,2]. Since 2 is the only occupied position, we return [2]. Constraints: * 1 <= nums.length <= 105 * 1 <= moveFrom.length <= 105 * moveFrom.length == moveTo.length * 1 <= nums[i], moveFrom[i], moveTo[i] <= 109 * The test cases are generated such that there is at least a marble in moveFrom[i] at the moment we want to apply the ith move. """ class Solution: def relocateMarbles(self, nums: List[int], moveFrom: List[int], moveTo: List[int]) -> List[int]:
You are given a 0-indexed integer array nums representing the initial positions of some marbles. You are also given two 0-indexed integer arrays moveFrom and moveTo of equal length. Throughout moveFrom.length steps, you will change the positions of the marbles. On the ith step, you will move all marbles at position moveFrom[i] to position moveTo[i]. After completing all the steps, return the sorted list of occupied positions. Notes: * We call a position occupied if there is at least one marble in that position. * There may be multiple marbles in a single position. Example 1: Input: nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5] Output: [5,6,8,9] Explanation: Initially, the marbles are at positions 1,6,7,8. At the i = 0th step, we move the marbles at position 1 to position 2. Then, positions 2,6,7,8 are occupied. At the i = 1st step, we move the marbles at position 7 to position 9. Then, positions 2,6,8,9 are occupied. At the i = 2nd step, we move the marbles at position 2 to position 5. Then, positions 5,6,8,9 are occupied. At the end, the final positions containing at least one marbles are [5,6,8,9]. Example 2: Input: nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2] Output: [2] Explanation: Initially, the marbles are at positions [1,1,3,3]. At the i = 0th step, we move all the marbles at position 1 to position 2. Then, the marbles are at positions [2,2,3,3]. At the i = 1st step, we move all the marbles at position 3 to position 2. Then, the marbles are at positions [2,2,2,2]. Since 2 is the only occupied position, we return [2]. Constraints: * 1 <= nums.length <= 105 * 1 <= moveFrom.length <= 105 * moveFrom.length == moveTo.length * 1 <= nums[i], moveFrom[i], moveTo[i] <= 109 * The test cases are generated such that there is at least a marble in moveFrom[i] at the moment we want to apply the ith move. Please complete the code below to solve above prblem: ```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" }
""" Given a binary string s, partition the string into one or more substrings such that each substring is beautiful. A string is beautiful if: * It doesn't contain leading zeros. * It's the binary representation of a number that is a power of 5. Return the minimum number of substrings in such partition. If it is impossible to partition the string s into beautiful substrings, return -1. A substring is a contiguous sequence of characters in a string. Example 1: Input: s = "1011" Output: 2 Explanation: We can paritition the given string into ["101", "1"]. - The string "101" does not contain leading zeros and is the binary representation of integer 51 = 5. - The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into. Example 2: Input: s = "111" Output: 3 Explanation: We can paritition the given string into ["1", "1", "1"]. - The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into. Example 3: Input: s = "0" Output: -1 Explanation: We can not partition the given string into beautiful substrings. Constraints: * 1 <= s.length <= 15 * s[i] is either '0' or '1'. """ class Solution: def minimumBeautifulSubstrings(self, s: str) -> int:
Given a binary string s, partition the string into one or more substrings such that each substring is beautiful. A string is beautiful if: * It doesn't contain leading zeros. * It's the binary representation of a number that is a power of 5. Return the minimum number of substrings in such partition. If it is impossible to partition the string s into beautiful substrings, return -1. A substring is a contiguous sequence of characters in a string. Example 1: Input: s = "1011" Output: 2 Explanation: We can paritition the given string into ["101", "1"]. - The string "101" does not contain leading zeros and is the binary representation of integer 51 = 5. - The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into. Example 2: Input: s = "111" Output: 3 Explanation: We can paritition the given string into ["1", "1", "1"]. - The string "1" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into. Example 3: Input: s = "0" Output: -1 Explanation: We can not partition the given string into beautiful substrings. Constraints: * 1 <= s.length <= 15 * s[i] is either '0' or '1'. Please complete the code below to solve above prblem: ```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" }
""" You are given two integers m and n representing the dimensions of a 0-indexed m x n grid. You are also given a 0-indexed 2D integer matrix coordinates, where coordinates[i] = [x, y] indicates that the cell with coordinates [x, y] is colored black. All cells in the grid that do not appear in coordinates are white. A block is defined as a 2 x 2 submatrix of the grid. More formally, a block with cell [x, y] as its top-left corner where 0 <= x < m - 1 and 0 <= y < n - 1 contains the coordinates [x, y], [x + 1, y], [x, y + 1], and [x + 1, y + 1]. Return a 0-indexed integer array arr of size 5 such that arr[i] is the number of blocks that contains exactly i black cells. Example 1: Input: m = 3, n = 3, coordinates = [[0,0]] Output: [3,1,0,0,0] Explanation: The grid looks like this: [https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png] There is only 1 block with one black cell, and it is the block starting with cell [0,0]. The other 3 blocks start with cells [0,1], [1,0] and [1,1]. They all have zero black cells. Thus, we return [3,1,0,0,0]. Example 2: Input: m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]] Output: [0,2,2,0,0] Explanation: The grid looks like this: [https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png] There are 2 blocks with two black cells (the ones starting with cell coordinates [0,0] and [0,1]). The other 2 blocks have starting cell coordinates of [1,0] and [1,1]. They both have 1 black cell. Therefore, we return [0,2,2,0,0]. Constraints: * 2 <= m <= 105 * 2 <= n <= 105 * 0 <= coordinates.length <= 104 * coordinates[i].length == 2 * 0 <= coordinates[i][0] < m * 0 <= coordinates[i][1] < n * It is guaranteed that coordinates contains pairwise distinct coordinates. """ class Solution: def countBlackBlocks(self, m: int, n: int, coordinates: List[List[int]]) -> List[int]:
You are given two integers m and n representing the dimensions of a 0-indexed m x n grid. You are also given a 0-indexed 2D integer matrix coordinates, where coordinates[i] = [x, y] indicates that the cell with coordinates [x, y] is colored black. All cells in the grid that do not appear in coordinates are white. A block is defined as a 2 x 2 submatrix of the grid. More formally, a block with cell [x, y] as its top-left corner where 0 <= x < m - 1 and 0 <= y < n - 1 contains the coordinates [x, y], [x + 1, y], [x, y + 1], and [x + 1, y + 1]. Return a 0-indexed integer array arr of size 5 such that arr[i] is the number of blocks that contains exactly i black cells. Example 1: Input: m = 3, n = 3, coordinates = [[0,0]] Output: [3,1,0,0,0] Explanation: The grid looks like this: [https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png] There is only 1 block with one black cell, and it is the block starting with cell [0,0]. The other 3 blocks start with cells [0,1], [1,0] and [1,1]. They all have zero black cells. Thus, we return [3,1,0,0,0]. Example 2: Input: m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]] Output: [0,2,2,0,0] Explanation: The grid looks like this: [https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png] There are 2 blocks with two black cells (the ones starting with cell coordinates [0,0] and [0,1]). The other 2 blocks have starting cell coordinates of [1,0] and [1,1]. They both have 1 black cell. Therefore, we return [0,2,2,0,0]. Constraints: * 2 <= m <= 105 * 2 <= n <= 105 * 0 <= coordinates.length <= 104 * coordinates[i].length == 2 * 0 <= coordinates[i][0] < m * 0 <= coordinates[i][1] < n * It is guaranteed that coordinates contains pairwise distinct coordinates. Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums and an integer threshold. Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions: * nums[l] % 2 == 0 * For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2 * For all indices i in the range [l, r], nums[i] <= threshold Return an integer denoting the length of the longest such subarray. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [3,2,5,4], threshold = 5 Output: 3 Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. Example 2: Input: nums = [1,2], threshold = 2 Output: 1 Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2]. It satisfies all the conditions and we can show that 1 is the maximum possible achievable length. Example 3: Input: nums = [2,3,4,5], threshold = 4 Output: 3 Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4]. It satisfies all the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. Constraints: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 100 * 1 <= threshold <= 100 """ class Solution: def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
You are given a 0-indexed integer array nums and an integer threshold. Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions: * nums[l] % 2 == 0 * For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2 * For all indices i in the range [l, r], nums[i] <= threshold Return an integer denoting the length of the longest such subarray. Note: A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [3,2,5,4], threshold = 5 Output: 3 Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. Example 2: Input: nums = [1,2], threshold = 2 Output: 1 Explanation: In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2]. It satisfies all the conditions and we can show that 1 is the maximum possible achievable length. Example 3: Input: nums = [2,3,4,5], threshold = 4 Output: 3 Explanation: In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4]. It satisfies all the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. Constraints: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 100 * 1 <= threshold <= 100 Please complete the code below to solve above prblem: ```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" }
""" You are given an integer n. We say that two integers x and y form a prime number pair if: * 1 <= x <= y <= n * x + y == n * x and y are prime numbers Return the 2D sorted list of prime number pairs [xi, yi]. The list should be sorted in increasing order of xi. If there are no prime number pairs at all, return an empty array. Note: A prime number is a natural number greater than 1 with only two factors, itself and 1. Example 1: Input: n = 10 Output: [[3,7],[5,5]] Explanation: In this example, there are two prime pairs that satisfy the criteria. These pairs are [3,7] and [5,5], and we return them in the sorted order as described in the problem statement. Example 2: Input: n = 2 Output: [] Explanation: We can show that there is no prime number pair that gives a sum of 2, so we return an empty array. Constraints: * 1 <= n <= 106 """ class Solution: def findPrimePairs(self, n: int) -> List[List[int]]:
You are given an integer n. We say that two integers x and y form a prime number pair if: * 1 <= x <= y <= n * x + y == n * x and y are prime numbers Return the 2D sorted list of prime number pairs [xi, yi]. The list should be sorted in increasing order of xi. If there are no prime number pairs at all, return an empty array. Note: A prime number is a natural number greater than 1 with only two factors, itself and 1. Example 1: Input: n = 10 Output: [[3,7],[5,5]] Explanation: In this example, there are two prime pairs that satisfy the criteria. These pairs are [3,7] and [5,5], and we return them in the sorted order as described in the problem statement. Example 2: Input: n = 2 Output: [] Explanation: We can show that there is no prime number pair that gives a sum of 2, so we return an empty array. Constraints: * 1 <= n <= 106 Please complete the code below to solve above prblem: ```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" }
""" You are given a 0-indexed integer array nums. A subarray of nums is called continuous if: * Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2. Return the total number of continuous subarrays. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [5,4,2,4] Output: 8 Explanation: Continuous subarray of size 1: [5], [4], [2], [4]. Continuous subarray of size 2: [5,4], [4,2], [2,4]. Continuous subarray of size 3: [4,2,4]. Thereare no subarrys of size 4. Total continuous subarrays = 4 + 3 + 1 = 8. It can be shown that there are no more continuous subarrays. Example 2: Input: nums = [1,2,3] Output: 6 Explanation: Continuous subarray of size 1: [1], [2], [3]. Continuous subarray of size 2: [1,2], [2,3]. Continuous subarray of size 3: [1,2,3]. Total continuous subarrays = 3 + 2 + 1 = 6. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 """ class Solution: def continuousSubarrays(self, nums: List[int]) -> int:
You are given a 0-indexed integer array nums. A subarray of nums is called continuous if: * Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2. Return the total number of continuous subarrays. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [5,4,2,4] Output: 8 Explanation: Continuous subarray of size 1: [5], [4], [2], [4]. Continuous subarray of size 2: [5,4], [4,2], [2,4]. Continuous subarray of size 3: [4,2,4]. Thereare no subarrys of size 4. Total continuous subarrays = 4 + 3 + 1 = 8. It can be shown that there are no more continuous subarrays. Example 2: Input: nums = [1,2,3] Output: 6 Explanation: Continuous subarray of size 1: [1], [2], [3]. Continuous subarray of size 2: [1,2], [2,3]. Continuous subarray of size 3: [1,2,3]. Total continuous subarrays = 3 + 2 + 1 = 6. Constraints: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 Please complete the code below to solve above prblem: ```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" }
""" The imbalance number of a 0-indexed integer array arr of length n is defined as the number of indices in sarr = sorted(arr) such that: * 0 <= i < n - 1, and * sarr[i+1] - sarr[i] > 1 Here, sorted(arr) is the function that returns the sorted version of arr. Given a 0-indexed integer array nums, return the sum of imbalance numbers of all its subarrays. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2,3,1,4] Output: 3 Explanation: There are 3 subarrays with non-zero imbalance numbers: - Subarray [3, 1] with an imbalance number of 1. - Subarray [3, 1, 4] with an imbalance number of 1. - Subarray [1, 4] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3. Example 2: Input: nums = [1,3,3,3,5] Output: 8 Explanation: There are 7 subarrays with non-zero imbalance numbers: - Subarray [1, 3] with an imbalance number of 1. - Subarray [1, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3, 5] with an imbalance number of 2. - Subarray [3, 3, 3, 5] with an imbalance number of 1. - Subarray [3, 3, 5] with an imbalance number of 1. - Subarray [3, 5] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= nums.length """ class Solution: def sumImbalanceNumbers(self, nums: List[int]) -> int:
The imbalance number of a 0-indexed integer array arr of length n is defined as the number of indices in sarr = sorted(arr) such that: * 0 <= i < n - 1, and * sarr[i+1] - sarr[i] > 1 Here, sorted(arr) is the function that returns the sorted version of arr. Given a 0-indexed integer array nums, return the sum of imbalance numbers of all its subarrays. A subarray is a contiguous non-empty sequence of elements within an array. Example 1: Input: nums = [2,3,1,4] Output: 3 Explanation: There are 3 subarrays with non-zero imbalance numbers: - Subarray [3, 1] with an imbalance number of 1. - Subarray [3, 1, 4] with an imbalance number of 1. - Subarray [1, 4] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3. Example 2: Input: nums = [1,3,3,3,5] Output: 8 Explanation: There are 7 subarrays with non-zero imbalance numbers: - Subarray [1, 3] with an imbalance number of 1. - Subarray [1, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3, 5] with an imbalance number of 2. - Subarray [3, 3, 3, 5] with an imbalance number of 1. - Subarray [3, 3, 5] with an imbalance number of 1. - Subarray [3, 5] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8. Constraints: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= nums.length Please complete the code below to solve above prblem: ```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