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
|
---|---|---|---|---|---|---|---|
weekly-contest-381-minimum-number-of-pushes-to-type-word-i | https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i | minimum-number-of-pushes-to-type-word-i | {
"questionId": "3275",
"questionFrontendId": "3014",
"title": "Minimum Number of Pushes to Type Word I",
"titleSlug": "minimum-number-of-pushes-to-type-word-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 39,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
You are given a string word containing distinct lowercase English letters.
Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c" .
It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. You need to find the minimum number of times the keys will be pushed to type the string word.
Return the minimum number of pushes needed to type word after remapping the keys.
An example mapping of letters to keys on a telephone keypad is given below. Note that 1, *, #, and 0 do not map to any letters.
Example 1:
Input: word = "abcde"
Output: 5
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
Example 2:
Input: word = "xycdefghij"
Output: 12
Explanation: The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
Constraints:
1 <= word.length <= 26
word consists of lowercase English letters.
All letters in word are distinct.
"""
class Solution:
def minimumPushes(self, word: str) -> int:
| You are given a string word containing distinct lowercase English letters.
Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c" .
It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. You need to find the minimum number of times the keys will be pushed to type the string word.
Return the minimum number of pushes needed to type word after remapping the keys.
An example mapping of letters to keys on a telephone keypad is given below. Note that 1, *, #, and 0 do not map to any letters.
Example 1:
Input: word = "abcde"
Output: 5
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
Example 2:
Input: word = "xycdefghij"
Output: 12
Explanation: The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> two pushes on key 2
"c" -> one push on key 3
"d" -> two pushes on key 3
"e" -> one push on key 4
"f" -> one push on key 5
"g" -> one push on key 6
"h" -> one push on key 7
"i" -> one push on key 8
"j" -> one push on key 9
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12.
It can be shown that no other mapping can provide a lower cost.
Constraints:
1 <= word.length <= 26
word consists of lowercase English letters.
All letters in word are distinct.
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumPushes(self, word: str) -> int:
``` |
my_solution = Solution()
test_input = { "word": "abcde" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "xycdefghij" }
assert my_solution.minimumPushes(**test_input) == 12
test_input = { "word": "b" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "d" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "e" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "f" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "g" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "h" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "i" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "k" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "n" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "o" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "q" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "u" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "v" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "w" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "x" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "bc" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "cu" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "dl" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "dn" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "ev" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "gn" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "gq" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "hu" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "jr" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "ln" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "lz" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "mv" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "mw" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "sw" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "wz" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "amw" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "bco" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "btx" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "cgp" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "cjq" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "clu" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "clx" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "crs" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "csz" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "dfp" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "htv" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "iwz" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "kux" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "nsv" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "svz" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "cfos" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "demr" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "dimo" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "dnpt" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "dorz" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "fgkn" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "fimn" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "hior" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "jkpy" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "jluv" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "knpv" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "kopu" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "lmpt" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "ltuw" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "qwxz" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "abhoz" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "aejwx" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "agjnr" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "aikmu" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "ajkmv" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "cflvx" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "dhstu" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "djmnx" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "dlovx" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "eglqy" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "ejntw" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "ekrsz" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "fopuz" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "jlnvz" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "jnstu" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "afikno" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "almsyz" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "bcehov" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "bdmprt" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "bfhmnu" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "bfhpty" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "bfjstu" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "cdfjmw" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "dfilps" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "dmswyz" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "dpqruw" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "fhmprz" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "gjqrvy" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "ijopsv" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "lmqrtz" }
assert my_solution.minimumPushes(**test_input) == 6
test_input = { "word": "bxnqpha" }
assert my_solution.minimumPushes(**test_input) == 7
test_input = { "word": "ekbfqat" }
assert my_solution.minimumPushes(**test_input) == 7
test_input = { "word": "esoizcx" }
assert my_solution.minimumPushes(**test_input) == 7
test_input = { "word": "fmteczo" }
assert my_solution.minimumPushes(**test_input) == 7
test_input = { "word": "lrywetm" }
assert my_solution.minimumPushes(**test_input) == 7
test_input = { "word": "lvbornx" }
assert my_solution.minimumPushes(**test_input) == 7
test_input = { "word": "pesmonc" }
assert my_solution.minimumPushes(**test_input) == 7
test_input = { "word": "pudymjw" }
assert my_solution.minimumPushes(**test_input) == 7 | 1,705,804,200 |
weekly-contest-381-count-the-number-of-houses-at-a-certain-distance-i | https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-i | count-the-number-of-houses-at-a-certain-distance-i | {
"questionId": "3271",
"questionFrontendId": "3015",
"title": "Count the Number of Houses at a Certain Distance I",
"titleSlug": "count-the-number-of-houses-at-a-certain-distance-i",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 62,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
You are given three positive integers n, x, and y.
In a city, there exist houses numbered 1 to n connected by n streets. There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1 . An additional street connects the house numbered x with the house numbered y.
For each k, such that 1 <= k <= n, you need to find the number of pairs of houses (house1, house2) such that the minimum number of streets that need to be traveled to reach house2 from house1 is k.
Return a 1-indexed array result of length n where result[k] represents the total number of pairs of houses such that the minimum streets required to reach one house from the other is k.
Note that x and y can be equal.
Example 1:
Input: n = 3, x = 1, y = 3
Output: [6,0,0]
Explanation: Let's look at each pair of houses:
- For the pair (1, 2), we can go from house 1 to house 2 directly.
- For the pair (2, 1), we can go from house 2 to house 1 directly.
- For the pair (1, 3), we can go from house 1 to house 3 directly.
- For the pair (3, 1), we can go from house 3 to house 1 directly.
- For the pair (2, 3), we can go from house 2 to house 3 directly.
- For the pair (3, 2), we can go from house 3 to house 2 directly.
Example 2:
Input: n = 5, x = 2, y = 4
Output: [10,8,2,0,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
- For k == 3, the pairs are (1, 5), and (5, 1).
- For k == 4 and k == 5, there are no pairs.
Example 3:
Input: n = 4, x = 1, y = 1
Output: [6,4,2,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
- For k == 3, the pairs are (1, 4), and (4, 1).
- For k == 4, there are no pairs.
Constraints:
2 <= n <= 100
1 <= x, y <= n
"""
class Solution:
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
| You are given three positive integers n, x, and y.
In a city, there exist houses numbered 1 to n connected by n streets. There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1 . An additional street connects the house numbered x with the house numbered y.
For each k, such that 1 <= k <= n, you need to find the number of pairs of houses (house1, house2) such that the minimum number of streets that need to be traveled to reach house2 from house1 is k.
Return a 1-indexed array result of length n where result[k] represents the total number of pairs of houses such that the minimum streets required to reach one house from the other is k.
Note that x and y can be equal.
Example 1:
Input: n = 3, x = 1, y = 3
Output: [6,0,0]
Explanation: Let's look at each pair of houses:
- For the pair (1, 2), we can go from house 1 to house 2 directly.
- For the pair (2, 1), we can go from house 2 to house 1 directly.
- For the pair (1, 3), we can go from house 1 to house 3 directly.
- For the pair (3, 1), we can go from house 3 to house 1 directly.
- For the pair (2, 3), we can go from house 2 to house 3 directly.
- For the pair (3, 2), we can go from house 3 to house 2 directly.
Example 2:
Input: n = 5, x = 2, y = 4
Output: [10,8,2,0,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
- For k == 3, the pairs are (1, 5), and (5, 1).
- For k == 4 and k == 5, there are no pairs.
Example 3:
Input: n = 4, x = 1, y = 1
Output: [6,4,2,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
- For k == 3, the pairs are (1, 4), and (4, 1).
- For k == 4, there are no pairs.
Constraints:
2 <= n <= 100
1 <= x, y <= n
Please complete the code below to solve above prblem:
```python
class Solution:
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "n": 3, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,0,0]
test_input = { "n": 5, "x": 2, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 4, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 2, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 2, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 2, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 2, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 3, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 2, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 3, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [6,0,0]
test_input = { "n": 3, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 3, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 4, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 1, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 2, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 2, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 3, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 3, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 3, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 4, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 4, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 4, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 5, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [10,6,4,0,0]
test_input = { "n": 5, "x": 1, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 5, "x": 1, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,10,0,0,0]
test_input = { "n": 5, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 2, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 2, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 5, "x": 3, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [10,6,4,0,0]
test_input = { "n": 5, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 3, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 3, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 3, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,6,4,0,0]
test_input = { "n": 5, "x": 4, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 5, "x": 4, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 5, "x": 4, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 4, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 5, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [10,10,0,0,0]
test_input = { "n": 5, "x": 5, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 5, "x": 5, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [10,6,4,0,0]
test_input = { "n": 5, "x": 5, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 5, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 6, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [12,8,6,4,0,0]
test_input = { "n": 6, "x": 1, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 1, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [12,14,4,0,0,0]
test_input = { "n": 6, "x": 1, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [12,12,6,0,0,0]
test_input = { "n": 6, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 2, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [12,12,6,0,0,0]
test_input = { "n": 6, "x": 2, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [12,14,4,0,0,0]
test_input = { "n": 6, "x": 3, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [12,8,6,4,0,0]
test_input = { "n": 6, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 3, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 3, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 3, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 3, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 4, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 4, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 4, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 4, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [12,8,6,4,0,0]
test_input = { "n": 6, "x": 5, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [12,14,4,0,0,0]
test_input = { "n": 6, "x": 5, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,12,6,0,0,0]
test_input = { "n": 6, "x": 5, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 5, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 5, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 5, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 6, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [12,12,6,0,0,0]
test_input = { "n": 6, "x": 6, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,14,4,0,0,0]
test_input = { "n": 6, "x": 6, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 6, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [12,8,6,4,0,0]
test_input = { "n": 6, "x": 6, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 6, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 7, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [14,10,8,6,4,0,0]
test_input = { "n": 7, "x": 1, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [14,12,8,6,2,0,0]
test_input = { "n": 7, "x": 1, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [14,16,8,4,0,0,0]
test_input = { "n": 7, "x": 1, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [14,16,10,2,0,0,0]
test_input = { "n": 7, "x": 1, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [14,14,14,0,0,0,0]
test_input = { "n": 7, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 2, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 2, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [14,16,10,2,0,0,0]
test_input = { "n": 7, "x": 3, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [14,10,8,6,4,0,0]
test_input = { "n": 7, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 3, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0] | 1,705,804,200 |
weekly-contest-381-minimum-number-of-pushes-to-type-word-ii | https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii | minimum-number-of-pushes-to-type-word-ii | {
"questionId": "3276",
"questionFrontendId": "3016",
"title": "Minimum Number of Pushes to Type Word II",
"titleSlug": "minimum-number-of-pushes-to-type-word-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 59,
"dislikes": 2,
"categoryTitle": "Algorithms"
} | """
You are given a string word containing lowercase English letters.
Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c" .
It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. You need to find the minimum number of times the keys will be pushed to type the string word.
Return the minimum number of pushes needed to type word after remapping the keys.
An example mapping of letters to keys on a telephone keypad is given below. Note that 1, *, #, and 0 do not map to any letters.
Example 1:
Input: word = "abcde"
Output: 5
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
Example 2:
Input: word = "xyzxyzxyzxyz"
Output: 12
Explanation: The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> one push on key 3
"z" -> one push on key 4
Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12
It can be shown that no other mapping can provide a lower cost.
Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
Example 3:
Input: word = "aabbccddeeffgghhiiiiii"
Output: 24
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
"f" -> one push on key 7
"g" -> one push on key 8
"h" -> two pushes on key 9
"i" -> one push on key 9
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24.
It can be shown that no other mapping can provide a lower cost.
Constraints:
1 <= word.length <= 105
word consists of lowercase English letters.
"""
class Solution:
def minimumPushes(self, word: str) -> int:
| You are given a string word containing lowercase English letters.
Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c" .
It is allowed to remap the keys numbered 2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. You need to find the minimum number of times the keys will be pushed to type the string word.
Return the minimum number of pushes needed to type word after remapping the keys.
An example mapping of letters to keys on a telephone keypad is given below. Note that 1, *, #, and 0 do not map to any letters.
Example 1:
Input: word = "abcde"
Output: 5
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
Example 2:
Input: word = "xyzxyzxyzxyz"
Output: 12
Explanation: The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> one push on key 3
"z" -> one push on key 4
Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12
It can be shown that no other mapping can provide a lower cost.
Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
Example 3:
Input: word = "aabbccddeeffgghhiiiiii"
Output: 24
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
"f" -> one push on key 7
"g" -> one push on key 8
"h" -> two pushes on key 9
"i" -> one push on key 9
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24.
It can be shown that no other mapping can provide a lower cost.
Constraints:
1 <= word.length <= 105
word consists of lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumPushes(self, word: str) -> int:
``` |
my_solution = Solution()
test_input = { "word": "abcde" }
assert my_solution.minimumPushes(**test_input) == 5
test_input = { "word": "xyzxyzxyzxyz" }
assert my_solution.minimumPushes(**test_input) == 12
test_input = { "word": "aabbccddeeffgghhiiiiii" }
assert my_solution.minimumPushes(**test_input) == 24
test_input = { "word": "a" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "f" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "h" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "i" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "l" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "o" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "q" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "s" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "t" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "u" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "x" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "y" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "z" }
assert my_solution.minimumPushes(**test_input) == 1
test_input = { "word": "at" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "aw" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "bd" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "bs" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "ck" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "de" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "ex" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "fy" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "fz" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "hu" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "ir" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "iz" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "km" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "lr" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "lu" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "mz" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "ng" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "nu" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "oo" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "qc" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "rv" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "se" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "up" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "wb" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "xg" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "yg" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "zv" }
assert my_solution.minimumPushes(**test_input) == 2
test_input = { "word": "abx" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "amw" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "bem" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "bhs" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "blg" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "bwq" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "cku" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "cmo" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "cnr" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "dgh" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "dmh" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "dqf" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "eys" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "fff" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "foz" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "fqw" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "fsh" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "gjz" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "gpx" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "gqu" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "jcc" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "nmu" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "pzm" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "rdz" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "rvy" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "rya" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "sbn" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "szd" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "tbd" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "uqk" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "vbh" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "vgr" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "vxy" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "xbp" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "yex" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "ynx" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "yuv" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "zih" }
assert my_solution.minimumPushes(**test_input) == 3
test_input = { "word": "acpy" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "ainw" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "aluw" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "ayfb" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "bbmr" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "bgta" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "bitn" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "bwif" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "bwrz" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "cdcl" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "dglo" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "dxng" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "earx" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "feig" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "fgjk" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "flmd" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "fnfp" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "glms" }
assert my_solution.minimumPushes(**test_input) == 4
test_input = { "word": "glou" }
assert my_solution.minimumPushes(**test_input) == 4 | 1,705,804,200 |
weekly-contest-381-count-the-number-of-houses-at-a-certain-distance-ii | https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-ii | count-the-number-of-houses-at-a-certain-distance-ii | {
"questionId": "3310",
"questionFrontendId": "3017",
"title": "Count the Number of Houses at a Certain Distance II",
"titleSlug": "count-the-number-of-houses-at-a-certain-distance-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 27,
"dislikes": 12,
"categoryTitle": "Algorithms"
} | """
You are given three positive integers n, x, and y.
In a city, there exist houses numbered 1 to n connected by n streets. There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1 . An additional street connects the house numbered x with the house numbered y.
For each k, such that 1 <= k <= n, you need to find the number of pairs of houses (house1, house2) such that the minimum number of streets that need to be traveled to reach house2 from house1 is k.
Return a 1-indexed array result of length n where result[k] represents the total number of pairs of houses such that the minimum streets required to reach one house from the other is k.
Note that x and y can be equal.
Example 1:
Input: n = 3, x = 1, y = 3
Output: [6,0,0]
Explanation: Let's look at each pair of houses:
- For the pair (1, 2), we can go from house 1 to house 2 directly.
- For the pair (2, 1), we can go from house 2 to house 1 directly.
- For the pair (1, 3), we can go from house 1 to house 3 directly.
- For the pair (3, 1), we can go from house 3 to house 1 directly.
- For the pair (2, 3), we can go from house 2 to house 3 directly.
- For the pair (3, 2), we can go from house 3 to house 2 directly.
Example 2:
Input: n = 5, x = 2, y = 4
Output: [10,8,2,0,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
- For k == 3, the pairs are (1, 5), and (5, 1).
- For k == 4 and k == 5, there are no pairs.
Example 3:
Input: n = 4, x = 1, y = 1
Output: [6,4,2,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
- For k == 3, the pairs are (1, 4), and (4, 1).
- For k == 4, there are no pairs.
Constraints:
2 <= n <= 105
1 <= x, y <= n
"""
class Solution:
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
| You are given three positive integers n, x, and y.
In a city, there exist houses numbered 1 to n connected by n streets. There is a street connecting the house numbered i with the house numbered i + 1 for all 1 <= i <= n - 1 . An additional street connects the house numbered x with the house numbered y.
For each k, such that 1 <= k <= n, you need to find the number of pairs of houses (house1, house2) such that the minimum number of streets that need to be traveled to reach house2 from house1 is k.
Return a 1-indexed array result of length n where result[k] represents the total number of pairs of houses such that the minimum streets required to reach one house from the other is k.
Note that x and y can be equal.
Example 1:
Input: n = 3, x = 1, y = 3
Output: [6,0,0]
Explanation: Let's look at each pair of houses:
- For the pair (1, 2), we can go from house 1 to house 2 directly.
- For the pair (2, 1), we can go from house 2 to house 1 directly.
- For the pair (1, 3), we can go from house 1 to house 3 directly.
- For the pair (3, 1), we can go from house 3 to house 1 directly.
- For the pair (2, 3), we can go from house 2 to house 3 directly.
- For the pair (3, 2), we can go from house 3 to house 2 directly.
Example 2:
Input: n = 5, x = 2, y = 4
Output: [10,8,2,0,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
- For k == 3, the pairs are (1, 5), and (5, 1).
- For k == 4 and k == 5, there are no pairs.
Example 3:
Input: n = 4, x = 1, y = 1
Output: [6,4,2,0]
Explanation: For each distance k the pairs are:
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
- For k == 3, the pairs are (1, 4), and (4, 1).
- For k == 4, there are no pairs.
Constraints:
2 <= n <= 105
1 <= x, y <= n
Please complete the code below to solve above prblem:
```python
class Solution:
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "n": 3, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,0,0]
test_input = { "n": 5, "x": 2, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 4, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 2, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 2, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 2, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 2, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [2,0]
test_input = { "n": 3, "x": 1, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 2, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 3, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [6,0,0]
test_input = { "n": 3, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 3, "x": 3, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [4,2,0]
test_input = { "n": 4, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 1, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 2, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 2, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 2, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 3, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 3, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 3, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 4, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 4, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [8,4,0,0]
test_input = { "n": 4, "x": 4, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 4, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [6,4,2,0]
test_input = { "n": 5, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [10,6,4,0,0]
test_input = { "n": 5, "x": 2, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 2, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 5, "x": 3, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,6,4,0,0]
test_input = { "n": 5, "x": 4, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [10,8,2,0,0]
test_input = { "n": 5, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 5, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [10,6,4,0,0]
test_input = { "n": 5, "x": 5, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 5, "x": 5, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [8,6,4,2,0]
test_input = { "n": 6, "x": 1, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 1, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [12,14,4,0,0,0]
test_input = { "n": 6, "x": 1, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [12,12,6,0,0,0]
test_input = { "n": 6, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 3, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [12,10,6,2,0,0]
test_input = { "n": 6, "x": 4, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 4, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 4, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [12,8,6,4,0,0]
test_input = { "n": 6, "x": 5, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,12,6,0,0,0]
test_input = { "n": 6, "x": 5, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 6, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,14,4,0,0,0]
test_input = { "n": 6, "x": 6, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [12,8,6,4,0,0]
test_input = { "n": 6, "x": 6, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 6, "x": 6, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [10,8,6,4,2,0]
test_input = { "n": 7, "x": 1, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [14,14,14,0,0,0,0]
test_input = { "n": 7, "x": 2, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [14,18,10,0,0,0,0]
test_input = { "n": 7, "x": 3, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 3, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 3, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [14,14,10,4,0,0,0]
test_input = { "n": 7, "x": 4, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 4, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [14,12,8,6,2,0,0]
test_input = { "n": 7, "x": 5, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,4,2,0,0]
test_input = { "n": 7, "x": 5, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 5, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [14,10,8,6,4,0,0]
test_input = { "n": 7, "x": 6, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 6, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [12,10,8,6,4,2,0]
test_input = { "n": 7, "x": 7, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [14,12,8,6,2,0,0]
test_input = { "n": 8, "x": 1, "y": 2 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,8,6,4,2,0]
test_input = { "n": 8, "x": 2, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [16,20,16,4,0,0,0,0]
test_input = { "n": 8, "x": 3, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,8,4,2,0,0]
test_input = { "n": 8, "x": 3, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [16,16,14,8,2,0,0,0]
test_input = { "n": 8, "x": 4, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,8,6,4,2,0]
test_input = { "n": 8, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,8,6,4,2,0]
test_input = { "n": 8, "x": 4, "y": 8 }
assert my_solution.countOfPairs(**test_input) == [16,18,10,8,4,0,0,0]
test_input = { "n": 8, "x": 5, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,8,4,2,0,0]
test_input = { "n": 8, "x": 5, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [16,14,10,8,6,2,0,0]
test_input = { "n": 8, "x": 6, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [16,16,14,8,2,0,0,0]
test_input = { "n": 8, "x": 6, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,8,6,4,2,0]
test_input = { "n": 8, "x": 6, "y": 8 }
assert my_solution.countOfPairs(**test_input) == [16,12,10,8,6,4,0,0]
test_input = { "n": 8, "x": 7, "y": 8 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,8,6,4,2,0]
test_input = { "n": 8, "x": 8, "y": 1 }
assert my_solution.countOfPairs(**test_input) == [16,16,16,8,0,0,0,0]
test_input = { "n": 8, "x": 8, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,8,6,4,2,0]
test_input = { "n": 8, "x": 8, "y": 8 }
assert my_solution.countOfPairs(**test_input) == [14,12,10,8,6,4,2,0]
test_input = { "n": 9, "x": 1, "y": 3 }
assert my_solution.countOfPairs(**test_input) == [18,14,12,10,8,6,4,0,0]
test_input = { "n": 9, "x": 4, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,10,8,6,4,2,0]
test_input = { "n": 9, "x": 4, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [18,16,14,12,6,4,2,0,0]
test_input = { "n": 9, "x": 4, "y": 9 }
assert my_solution.countOfPairs(**test_input) == [18,20,16,10,6,2,0,0,0]
test_input = { "n": 9, "x": 5, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,10,8,6,4,2,0]
test_input = { "n": 9, "x": 5, "y": 9 }
assert my_solution.countOfPairs(**test_input) == [18,20,12,10,8,4,0,0,0]
test_input = { "n": 9, "x": 6, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,10,8,6,4,2,0]
test_input = { "n": 9, "x": 7, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,10,8,6,4,2,0]
test_input = { "n": 9, "x": 8, "y": 4 }
assert my_solution.countOfPairs(**test_input) == [18,22,16,10,6,0,0,0,0]
test_input = { "n": 9, "x": 8, "y": 6 }
assert my_solution.countOfPairs(**test_input) == [18,16,12,10,8,6,2,0,0]
test_input = { "n": 9, "x": 8, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,10,8,6,4,2,0]
test_input = { "n": 9, "x": 8, "y": 9 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,10,8,6,4,2,0]
test_input = { "n": 9, "x": 9, "y": 5 }
assert my_solution.countOfPairs(**test_input) == [18,20,12,10,8,4,0,0,0]
test_input = { "n": 9, "x": 9, "y": 7 }
assert my_solution.countOfPairs(**test_input) == [18,14,12,10,8,6,4,0,0]
test_input = { "n": 9, "x": 9, "y": 8 }
assert my_solution.countOfPairs(**test_input) == [16,14,12,10,8,6,4,2,0] | 1,705,804,200 |
biweekly-contest-122-divide-an-array-into-subarrays-with-minimum-cost-i | https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i | divide-an-array-into-subarrays-with-minimum-cost-i | {
"questionId": "3263",
"questionFrontendId": "3010",
"title": "Divide an Array Into Subarrays With Minimum Cost I",
"titleSlug": "divide-an-array-into-subarrays-with-minimum-cost-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 40,
"dislikes": 2,
"categoryTitle": "Algorithms"
} | """
You are given an array of integers nums of length n.
The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3.
You need to divide nums into 3 disjoint contiguous subarrays.
Return the minimum possible sum of the cost of these subarrays.
Example 1:
Input: nums = [1,2,3,12]
Output: 6
Explanation: The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
Example 2:
Input: nums = [5,4,3]
Output: 12
Explanation: The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
Example 3:
Input: nums = [10,3,1,1]
Output: 12
Explanation: The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
Constraints:
3 <= n <= 50
1 <= nums[i] <= 50
"""
class Solution:
def minimumCost(self, nums: List[int]) -> int:
| You are given an array of integers nums of length n.
The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3.
You need to divide nums into 3 disjoint contiguous subarrays.
Return the minimum possible sum of the cost of these subarrays.
Example 1:
Input: nums = [1,2,3,12]
Output: 6
Explanation: The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
Example 2:
Input: nums = [5,4,3]
Output: 12
Explanation: The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
Example 3:
Input: nums = [10,3,1,1]
Output: 12
Explanation: The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
Constraints:
3 <= n <= 50
1 <= nums[i] <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumCost(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,12] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [5,4,3] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [10,3,1,1] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [1,1,1] }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [1,1,2] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [1,1,3] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [1,1,4] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [1,1,5] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [1,2,1] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [1,2,2] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [1,2,3] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [1,2,4] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [1,2,5] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [1,3,1] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [1,3,2] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [1,3,3] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [1,3,4] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [1,3,5] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,4,1] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [1,4,2] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [1,4,3] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [1,4,4] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,4,5] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [1,5,1] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [1,5,2] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [1,5,3] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,5,4] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [1,5,5] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [2,1,1] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [2,1,2] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [2,1,3] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [2,1,4] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [2,1,5] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [2,2,1] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [2,2,2] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [2,2,3] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [2,2,4] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [2,2,5] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [2,3,1] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [2,3,2] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [2,3,3] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [2,3,4] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [2,3,5] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [2,4,1] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [2,4,2] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [2,4,3] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [2,4,4] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [2,4,5] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [2,5,1] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [2,5,2] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [2,5,3] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [2,5,4] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [2,5,5] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [3,1,1] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [3,1,2] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [3,1,3] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [3,1,4] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [3,1,5] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [3,2,1] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [3,2,2] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [3,2,3] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [3,2,4] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [3,2,5] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [3,3,1] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [3,3,2] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [3,3,3] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [3,3,4] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [3,3,5] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,4,1] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [3,4,2] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [3,4,3] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [3,4,4] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,4,5] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [3,5,1] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [3,5,2] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [3,5,3] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,5,4] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [3,5,5] }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [4,1,1] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [4,1,2] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [4,1,3] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [4,1,4] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [4,1,5] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [4,2,1] }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [4,2,2] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [4,2,3] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [4,2,4] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [4,2,5] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [4,3,1] }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [4,3,2] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [4,3,3] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [4,3,4] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [4,3,5] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [4,4,1] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [4,4,2] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [4,4,3] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [4,4,4] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [4,4,5] }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [4,5,1] }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [4,5,2] }
assert my_solution.minimumCost(**test_input) == 11 | 1,705,761,000 |
biweekly-contest-122-find-if-array-can-be-sorted | https://leetcode.com/problems/find-if-array-can-be-sorted | find-if-array-can-be-sorted | {
"questionId": "3291",
"questionFrontendId": "3011",
"title": "Find if Array Can Be Sorted",
"titleSlug": "find-if-array-can-be-sorted",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 52,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of positive integers nums.
In one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times (including zero).
Return true if you can sort the array, else return false.
Example 1:
Input: nums = [8,4,2,30,15]
Output: true
Explanation: Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
We can sort the array using 4 operations:
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
The array has become sorted, hence we return true.
Note that there may be other sequences of operations which also sort the array.
Example 2:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: The array is already sorted, hence we return true.
Example 3:
Input: nums = [3,16,8,4,2]
Output: false
Explanation: It can be shown that it is not possible to sort the input array using any number of operations.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 28
"""
class Solution:
def canSortArray(self, nums: List[int]) -> bool:
| You are given a 0-indexed array of positive integers nums.
In one operation, you can swap any two adjacent elements if they have the same number of set bits. You are allowed to do this operation any number of times (including zero).
Return true if you can sort the array, else return false.
Example 1:
Input: nums = [8,4,2,30,15]
Output: true
Explanation: Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110".
We can sort the array using 4 operations:
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
The array has become sorted, hence we return true.
Note that there may be other sequences of operations which also sort the array.
Example 2:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: The array is already sorted, hence we return true.
Example 3:
Input: nums = [3,16,8,4,2]
Output: false
Explanation: It can be shown that it is not possible to sort the input array using any number of operations.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 28
Please complete the code below to solve above prblem:
```python
class Solution:
def canSortArray(self, nums: List[int]) -> bool:
``` |
my_solution = Solution()
test_input = { "nums": [8,4,2,30,15] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [1,2,3,4,5] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [1] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [4] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [3,16,8,4,2] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [7] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [10] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [20,16] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [18] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [21,17] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [30] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [26,10] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [1,2] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [2,28,9] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [2,17] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [18,3,8] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [31,18,23] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [75,34,30] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [107,76,52] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [125,92,159] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [136,256,10] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [160,247,127] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [187,4,32] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [197,171,144] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [214,200,176] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [222,191,39] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [24,12] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [225,163,64] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [128,128] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [229,253,127] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [1,2,3] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [1,256,64] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [6,6,192] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [239,83,71] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [6,96,20] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [247,153,90] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [256,255,255] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [1,201,251,191] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [4,157,191,127] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [8,8,2] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [10,34,130] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [12,19,1,11] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [10,91,127] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [15,8,21,25] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [17,25,4,27] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [10,130,206] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [14,183,251] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [29,20,17,4] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [15,147,174] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [16,245,125] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [32,12,25,19] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [22,21,26] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [23,30,32] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [24,72,160] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [33,223,239] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [35,143,127,254] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [55,147,16,8] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [34,52,104] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [100,104,96,144] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [129,70,126,253] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [129,162,158,253] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [145,127,55,43] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [36,177,244] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [159,111,124,233] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [36,213,236] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [175,231,27,92] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [205,234,127,223] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [215,10,8,256] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [223,127,172,210] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [38,221,224] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [41,14,50] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [41,79,239] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [44,124,247] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [225,201,121,103] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [232,45,175,231] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [250,131,50,46] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [254,249,173,163] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [255,255,214,229] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [256,151,141,15] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [47,205,182] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [48,64,251] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [51,253,254] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [53,172,195] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [57,127,251] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [4,98,210,79,254] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [59,31,236] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [8,5,103,247,235] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [8,74,170,254,132] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [8,148,182,62,255] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [62,153,210] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [64,93,253] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [9,28,18,26,11] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [12,208,240,216,139] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [13,21,23,13,32] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [16,24,13,46,156] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [16,192,71,31,239] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [64,195,203] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [65,254,239] }
assert my_solution.canSortArray(**test_input) == True
test_input = { "nums": [17,11,5,20,8] }
assert my_solution.canSortArray(**test_input) == False
test_input = { "nums": [23,12,22,29,20] }
assert my_solution.canSortArray(**test_input) == False | 1,705,761,000 |
biweekly-contest-122-minimize-length-of-array-using-operations | https://leetcode.com/problems/minimize-length-of-array-using-operations | minimize-length-of-array-using-operations | {
"questionId": "3244",
"questionFrontendId": "3012",
"title": "Minimize Length of Array Using Operations",
"titleSlug": "minimize-length-of-array-using-operations",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 79,
"dislikes": 30,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums containing positive integers.
Your task is to minimize the length of nums by performing the following operations any number of times (including zero):
Select two distinct indices i and j from nums, such that nums[i] > 0 and nums[j] > 0.
Insert the result of nums[i] % nums[j] at the end of nums.
Delete the elements at indices i and j from nums.
Return an integer denoting the minimum length of nums after performing the operation any number of times.
Example 1:
Input: nums = [1,4,3,1]
Output: 1
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1.
nums becomes [1,1,3].
Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2.
nums becomes [1,1].
Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0.
nums becomes [0].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
Example 2:
Input: nums = [5,5,5,10,5]
Output: 2
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3.
nums becomes [5,5,5,5].
Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3.
nums becomes [5,5,0].
Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1.
nums becomes [0,0].
The length of nums cannot be reduced further. Hence, the answer is 2.
It can be shown that 2 is the minimum achievable length.
Example 3:
Input: nums = [2,3,4]
Output: 1
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2.
nums becomes [2,3].
Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0.
nums becomes [1].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
"""
class Solution:
def minimumArrayLength(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums containing positive integers.
Your task is to minimize the length of nums by performing the following operations any number of times (including zero):
Select two distinct indices i and j from nums, such that nums[i] > 0 and nums[j] > 0.
Insert the result of nums[i] % nums[j] at the end of nums.
Delete the elements at indices i and j from nums.
Return an integer denoting the minimum length of nums after performing the operation any number of times.
Example 1:
Input: nums = [1,4,3,1]
Output: 1
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1.
nums becomes [1,1,3].
Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2.
nums becomes [1,1].
Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0.
nums becomes [0].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
Example 2:
Input: nums = [5,5,5,10,5]
Output: 2
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3.
nums becomes [5,5,5,5].
Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3.
nums becomes [5,5,0].
Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1.
nums becomes [0,0].
The length of nums cannot be reduced further. Hence, the answer is 2.
It can be shown that 2 is the minimum achievable length.
Example 3:
Input: nums = [2,3,4]
Output: 1
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2.
nums becomes [2,3].
Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0.
nums becomes [1].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumArrayLength(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,4,3,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,5,5,10,5] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [2,3,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [6] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [6,9] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [8,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [9,9] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [9,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,2,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,1,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,7,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,9,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,2,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,2,7] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,3,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,1,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,4,4] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [5,1,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,5,5] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [6,5,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [8,4,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [9,2,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,2,3,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,3,1,7] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,5,5,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,8,7,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,2,1,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,7,10,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,10,1,7] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,2,3,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,4,1,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,3,10,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [6,3,4,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [6,5,2,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,2,5,9] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,3,4,4,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,1,7,10,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,3,1,4,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,6,2,6,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [6,10,6,3,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,4,5,4,5,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,6,6,9,5,7] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,1,2,5,3,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,2,4,4,2,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,5,2,5,5,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,1,4,4,5,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,3,1,2,5,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,8,8,7,6,8] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,2,2,2,9,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,3,2,4,3,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,5,6,6,7,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [8,3,9,4,5,8] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,5,4,3,5,5,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,5,5,1,2,5,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,8,7,4,9,3,9] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,3,5,7,9,10,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,1,9,3,9,2,6] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,10,1,8,6,1,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [9,1,10,7,3,9,7] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [10,10,3,9,8,3,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [10,10,4,8,5,2,6] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,5,2,10,4,5,10,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,1,3,3,3,3,1,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,2,7,4,5,5,1,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,4,5,5,3,5,2,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,10,6,7,7,2,3,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,3,2,2,4,2,3,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,4,3,4,1,1,1,2] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [3,4,4,3,5,4,5,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [3,6,7,7,6,9,1,6] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,1,1,1,1,5,5,5] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [6,7,5,5,3,6,1,8] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [8,5,4,5,4,7,6,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [8,10,4,6,7,9,2,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [2,6,3,8,9,10,9,3,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,2,2,1,3,1,5,3,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,4,5,1,2,1,1,1,2] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [4,5,3,5,5,4,4,2,1] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,1,5,1,1,5,4,3,3] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [5,1,5,3,3,2,2,4,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [6,4,5,7,9,10,10,6,9] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [6,5,6,4,9,8,8,3,7] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [9,7,6,10,1,8,5,4,2] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [9,10,1,6,4,10,1,3,4] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [10,5,4,8,4,3,7,10,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,1,1,2,2,2,5,5,1,3] }
assert my_solution.minimumArrayLength(**test_input) == 2
test_input = { "nums": [4,5,1,8,2,7,2,7,7,6] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,1,3,10,1,4,5,2,9,7] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,2,2,9,5,6,6,10,2,3] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [7,7,3,6,8,10,3,7,6,9] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [1,4,5,8,9,3,1,4,7,4,5] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [4,10,3,8,9,5,7,6,9,10,10] }
assert my_solution.minimumArrayLength(**test_input) == 1
test_input = { "nums": [5,9,3,9,3,10,1,1,6,3,10] }
assert my_solution.minimumArrayLength(**test_input) == 1 | 1,705,761,000 |
biweekly-contest-122-divide-an-array-into-subarrays-with-minimum-cost-ii | https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-ii | divide-an-array-into-subarrays-with-minimum-cost-ii | {
"questionId": "3260",
"questionFrontendId": "3013",
"title": "Divide an Array Into Subarrays With Minimum Cost II",
"titleSlug": "divide-an-array-into-subarrays-with-minimum-cost-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 54,
"dislikes": 2,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of integers nums of length n, and two positive integers k and dist.
The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3.
You need to divide nums into k disjoint contiguous subarrays, such that the difference between the starting index of the second subarray and the starting index of the kth subarray should be less than or equal to dist. In other words, if you divide nums into the subarrays nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)], then ik-1 - i1 <= dist.
Return the minimum possible sum of the cost of these subarrays.
Example 1:
Input: nums = [1,3,2,6,4,2], k = 3, dist = 3
Output: 5
Explanation: The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
Example 2:
Input: nums = [10,1,2,2,2,1], k = 4, dist = 3
Output: 15
Explanation: The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
Example 3:
Input: nums = [10,8,18,9], k = 3, dist = 1
Output: 36
Explanation: The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
Constraints:
3 <= n <= 105
1 <= nums[i] <= 109
3 <= k <= n
k - 2 <= dist <= n - 2
"""
class Solution:
def minimumCost(self, nums: List[int], k: int, dist: int) -> int:
| You are given a 0-indexed array of integers nums of length n, and two positive integers k and dist.
The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3.
You need to divide nums into k disjoint contiguous subarrays, such that the difference between the starting index of the second subarray and the starting index of the kth subarray should be less than or equal to dist. In other words, if you divide nums into the subarrays nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)], then ik-1 - i1 <= dist.
Return the minimum possible sum of the cost of these subarrays.
Example 1:
Input: nums = [1,3,2,6,4,2], k = 3, dist = 3
Output: 5
Explanation: The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.
Example 2:
Input: nums = [10,1,2,2,2,1], k = 4, dist = 3
Output: 15
Explanation: The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.
Example 3:
Input: nums = [10,8,18,9], k = 3, dist = 1
Output: 36
Explanation: The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.
Constraints:
3 <= n <= 105
1 <= nums[i] <= 109
3 <= k <= n
k - 2 <= dist <= n - 2
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumCost(self, nums: List[int], k: int, dist: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,2,6,4,2], "k": 3, "dist": 3 }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [10,1,2,2,2,1], "k": 4, "dist": 3 }
assert my_solution.minimumCost(**test_input) == 15
test_input = { "nums": [10,8,18,9], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 36
test_input = { "nums": [1,1,1], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [1,1,3], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [1,2,2], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [1,2,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [1,4,4], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [2,2,1], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [2,3,2], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [2,5,4], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,1,2], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [3,1,3], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [3,2,2], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [3,3,2], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [3,4,1], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [3,5,3], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [4,1,4], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [4,1,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [4,2,1], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 7
test_input = { "nums": [4,2,2], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [4,2,4], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [4,2,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [4,3,1], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [4,3,2], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [4,5,3], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [5,2,1], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 8
test_input = { "nums": [5,3,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [50,50,50], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 150
test_input = { "nums": [1,5,3,6], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,5,3,7], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,5,3,7], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,5,3,8], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,5,3,8], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,5,4,6], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 16
test_input = { "nums": [1,6,3,5], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [1,6,3,8], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [1,6,4,5], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 16
test_input = { "nums": [1,7,4,6], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 18
test_input = { "nums": [1,7,4,8], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [1,8,3,8], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [1,8,4,7], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [2,5,3,8], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [2,5,4,7], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 18
test_input = { "nums": [2,5,4,8], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "nums": [2,6,3,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [2,6,3,6], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [2,6,4,5], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 17
test_input = { "nums": [2,6,4,7], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "nums": [2,6,4,8], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [2,7,3,5], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [2,7,3,8], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [2,7,4,6], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "nums": [2,8,3,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 10
test_input = { "nums": [2,8,4,5], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "nums": [3,5,3,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,5,3,5], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,5,3,8], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,5,4,7], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "nums": [3,6,3,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,6,3,7], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [3,6,3,8], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [3,6,4,5], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 18
test_input = { "nums": [3,7,3,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,7,3,5], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,7,3,6], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [3,7,3,8], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [3,7,4,7], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 21
test_input = { "nums": [3,8,3,5], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [3,8,4,6], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 21
test_input = { "nums": [4,5,3,5], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [4,5,3,6], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [4,5,3,8], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [4,5,4,5], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 18
test_input = { "nums": [4,6,3,6], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [4,6,3,7], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [4,6,4,5], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "nums": [4,6,4,8], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 22
test_input = { "nums": [4,7,3,6], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [4,7,4,5], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [4,7,4,7], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 22
test_input = { "nums": [4,8,3,5], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [4,8,3,6], "k": 3, "dist": 1 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [4,8,3,7], "k": 3, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 14
test_input = { "nums": [4,8,4,6], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 22
test_input = { "nums": [4,8,4,8], "k": 4, "dist": 2 }
assert my_solution.minimumCost(**test_input) == 24
test_input = { "nums": [1,5,6,6,3,7,2], "k": 6, "dist": 5 }
assert my_solution.minimumCost(**test_input) == 23
test_input = { "nums": [1,6,4,6,2,9,11], "k": 4, "dist": 3 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [1,6,4,7,9,6,1], "k": 4, "dist": 4 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [1,6,5,6,4,9,11], "k": 5, "dist": 5 }
assert my_solution.minimumCost(**test_input) == 22
test_input = { "nums": [1,6,5,7,8,7,5], "k": 5, "dist": 4 }
assert my_solution.minimumCost(**test_input) == 25
test_input = { "nums": [1,6,5,8,11,10,6], "k": 5, "dist": 3 }
assert my_solution.minimumCost(**test_input) == 31
test_input = { "nums": [1,6,6,8,4,8,7], "k": 6, "dist": 4 }
assert my_solution.minimumCost(**test_input) == 33
test_input = { "nums": [1,7,6,8,5,10,10], "k": 6, "dist": 5 }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "nums": [1,8,3,8,11,11,10], "k": 3, "dist": 5 }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "nums": [1,8,4,7,11,1,8], "k": 4, "dist": 4 }
assert my_solution.minimumCost(**test_input) == 13
test_input = { "nums": [1,8,6,5,6,12,12], "k": 6, "dist": 5 }
assert my_solution.minimumCost(**test_input) == 38
test_input = { "nums": [1,8,6,6,12,5,2], "k": 6, "dist": 5 }
assert my_solution.minimumCost(**test_input) == 28
test_input = { "nums": [2,5,3,5,7,4,3], "k": 3, "dist": 3 }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "nums": [2,5,4,6,6,1,3], "k": 4, "dist": 5 }
assert my_solution.minimumCost(**test_input) == 10 | 1,705,761,000 |
weekly-contest-380-count-elements-with-maximum-frequency | https://leetcode.com/problems/count-elements-with-maximum-frequency | count-elements-with-maximum-frequency | {
"questionId": "3242",
"questionFrontendId": "3005",
"title": "Count Elements With Maximum Frequency",
"titleSlug": "count-elements-with-maximum-frequency",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 47,
"dislikes": 2,
"categoryTitle": "Algorithms"
} | """
You are given an array nums consisting of positive integers.
Return the total frequencies of elements in numssuch that those elements all have the maximum frequency.
The frequency of an element is the number of occurrences of that element in the array.
Example 1:
Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.
Example 2:
Input: nums = [1,2,3,4,5]
Output: 5
Explanation: All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
"""
class Solution:
def maxFrequencyElements(self, nums: List[int]) -> int:
| You are given an array nums consisting of positive integers.
Return the total frequencies of elements in numssuch that those elements all have the maximum frequency.
The frequency of an element is the number of occurrences of that element in the array.
Example 1:
Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.
Example 2:
Input: nums = [1,2,3,4,5]
Output: 5
Explanation: All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def maxFrequencyElements(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,2,3,1,4] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [1,2,3,4,5] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [15] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [10,12,11,9,6,19,11] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [2,12,17,18,11] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [19,19,19,20,19,8,19] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [1,1,1,1] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [10,1,12,10,10,19,10] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [1,1,1,20,6,1] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [17,17] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [6,13,15,15,11,6,7,12,4,11] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [1,2] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [14,14,17] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [17,17,2,12,20,17,12] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [3,9,11,11,20] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [8,15,8,11,8,13,12,11,8] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [17,8,17,19,17,13,17,17,17,5] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [11] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [5] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [4,4,10] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [15,13,2,16,2,5,1,18,8,16] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [1,17,12,7,17,3] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [8,2,8,6,1,1] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [3,9,7,9] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [20,20,20,5,12,20,9,16] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [2,14,3,8,16,4,4,3] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [6,12,3,3,11,2] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [5,2,13,19,15,20] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [2,13,13] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [4,5] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [20,20,15,20,20,20] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [16,16,16,16,1,10,16,9] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [5,3,5,8,5,3,5,15] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [17] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [2,2,3,3,9] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [5,11,4,2] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [13,13,7] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [2,15,10,10,10,4,13] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [3,7,1] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [19,6,19,19,19,19,19] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [15,3,12,4,9,14,10] }
assert my_solution.maxFrequencyElements(**test_input) == 7
test_input = { "nums": [1,19,12,1,12,12,1,6] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [17,7,3,3,6,5,6,2] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [12,4,2,9,17,14,1,12,6] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [16,11] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [11,11,11,11,10,11,3,11,11] }
assert my_solution.maxFrequencyElements(**test_input) == 7
test_input = { "nums": [16,4,20,10,12] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [3,11,3,11] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [13,9,13,13,13,13,2,13] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [2,8,9,4,3] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [19,6,9,12,12] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [20] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [1,11] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [6,4,7,19,20,10,13,14] }
assert my_solution.maxFrequencyElements(**test_input) == 8
test_input = { "nums": [16,8,5] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [15,15,4,7,15,15,15,15,15,7] }
assert my_solution.maxFrequencyElements(**test_input) == 7
test_input = { "nums": [5,20] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [13] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [7,15,13,18,3,11,13,7,1,13] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [17,5,17,5,5] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [4,5,3,5] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [11,2] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [1,17,17,20,2,2] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [2,5,2,2] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [1,1,1,3,8,1] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [1,19,19,5,14,13,1,20,6] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [19,12,8,20,3,1,12,17] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [7,15,1,1,6,3] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [8,8,8,3,8,8,3] }
assert my_solution.maxFrequencyElements(**test_input) == 5
test_input = { "nums": [5,1,2,2,2,1,1] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [12,13,6] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [18,12,8,2,16,19] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [15,10,2,18,11,14,9] }
assert my_solution.maxFrequencyElements(**test_input) == 7
test_input = { "nums": [19,17,9,13,1,13] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [4,12,15,1,4,4,2] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [16,16,16,8] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [2] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [13,15,1] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [10,10,5,16,17,6,18] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [3,2,14,2,18,7] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [16,16,3] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [1,8,10,11,8,15] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [8,19,2,7,5,6,3,4] }
assert my_solution.maxFrequencyElements(**test_input) == 8
test_input = { "nums": [9] }
assert my_solution.maxFrequencyElements(**test_input) == 1
test_input = { "nums": [13,6,13,10] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [14,13,14,4,4,14] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [9,9,1,9,9,1] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [14,4,11,14,14,4,4] }
assert my_solution.maxFrequencyElements(**test_input) == 6
test_input = { "nums": [4,20,20,4,1] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [5,11,8,3,11,11,11] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [3,2,18,5] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [3,8,20,7,16,20,18,13] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [19,9,16,4,10,3,18] }
assert my_solution.maxFrequencyElements(**test_input) == 7
test_input = { "nums": [11,2,2,3,19,3,11,2,14,1] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [19,14,11,7,19,1,11,2,16] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [18,15,3,2,8,12,19,14,12] }
assert my_solution.maxFrequencyElements(**test_input) == 2
test_input = { "nums": [5,6,11,9,5,5,5] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [8,4,4,12,8,1] }
assert my_solution.maxFrequencyElements(**test_input) == 4
test_input = { "nums": [9,1,9,9,3] }
assert my_solution.maxFrequencyElements(**test_input) == 3
test_input = { "nums": [18] }
assert my_solution.maxFrequencyElements(**test_input) == 1 | 1,705,199,400 |
weekly-contest-380-find-beautiful-indices-in-the-given-array-i | https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i | find-beautiful-indices-in-the-given-array-i | {
"questionId": "3245",
"questionFrontendId": "3006",
"title": "Find Beautiful Indices in the Given Array I",
"titleSlug": "find-beautiful-indices-in-the-given-array-i",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 91,
"dislikes": 21,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed string s, a string a, a string b, and an integer k.
An index i is beautiful if:
0 <= i <= s.length - a.length
s[i..(i + a.length - 1)] == a
There exists an index j such that:
0 <= j <= s.length - b.length
s[j..(j + b.length - 1)] == b
|j - i| <= k
Return the array that contains beautiful indices in sorted order from smallest to largest.
Example 1:
Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
Output: [16,33]
Explanation: There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
Example 2:
Input: s = "abcd", a = "a", b = "a", k = 4
Output: [0]
Explanation: There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
Constraints:
1 <= k <= s.length <= 105
1 <= a.length, b.length <= 10
s, a, and b contain only lowercase English letters.
"""
class Solution:
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
| You are given a 0-indexed string s, a string a, a string b, and an integer k.
An index i is beautiful if:
0 <= i <= s.length - a.length
s[i..(i + a.length - 1)] == a
There exists an index j such that:
0 <= j <= s.length - b.length
s[j..(j + b.length - 1)] == b
|j - i| <= k
Return the array that contains beautiful indices in sorted order from smallest to largest.
Example 1:
Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
Output: [16,33]
Explanation: There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
Example 2:
Input: s = "abcd", a = "a", b = "a", k = 4
Output: [0]
Explanation: There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
Constraints:
1 <= k <= s.length <= 105
1 <= a.length, b.length <= 10
s, a, and b contain only lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "s": "isawsquirrelnearmysquirrelhouseohmy", "a": "my", "b": "squirrel", "k": 15 }
assert my_solution.beautifulIndices(**test_input) == [16,33]
test_input = { "s": "abcd", "a": "a", "b": "a", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "sqgrt", "a": "rt", "b": "sq", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [3]
test_input = { "s": "mquz", "a": "tklr", "b": "caz", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "wl", "a": "xjigt", "b": "wl", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bavgoc", "a": "ba", "b": "c", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "xpcp", "a": "yxnod", "b": "xpc", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "lahhnlwx", "a": "hhnlw", "b": "ty", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "dexgscgecd", "a": "gscge", "b": "d", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [3]
test_input = { "s": "vjrao", "a": "vjr", "b": "yxpsw", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "oo", "a": "swhup", "b": "o", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bxlzgxc", "a": "ducf", "b": "xlzgx", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "wetlgztzm", "a": "box", "b": "wetl", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ocmm", "a": "m", "b": "oc", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [2,3]
test_input = { "s": "goxmox", "a": "gibs", "b": "ox", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "kzlrqzldvy", "a": "zl", "b": "tfsr", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "qhd", "a": "hd", "b": "od", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bozpeh", "a": "bozp", "b": "vrjn", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ggfsg", "a": "gfsg", "b": "g", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [1]
test_input = { "s": "fape", "a": "vq", "b": "ap", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "isitbenom", "a": "pmng", "b": "itben", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gw", "a": "ln", "b": "gw", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "jhu", "a": "sio", "b": "xnx", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "elcklvcvdg", "a": "lck", "b": "e", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == [1]
test_input = { "s": "subsu", "a": "tdo", "b": "su", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "jqcdc", "a": "c", "b": "d", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [2,4]
test_input = { "s": "hhvc", "a": "gfwo", "b": "hh", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "tyoq", "a": "vhjit", "b": "yoq", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "rtbp", "a": "migjb", "b": "es", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gkkstqvl", "a": "gkkst", "b": "xszl", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bc", "a": "spzk", "b": "wsick", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gyalx", "a": "neet", "b": "rbhl", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "qo", "a": "agt", "b": "xrh", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "rinzbrrr", "a": "nzb", "b": "r", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [2]
test_input = { "s": "tjly", "a": "j", "b": "n", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "frkxslnnn", "a": "rkxsl", "b": "n", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "cffczbccc", "a": "ff", "b": "c", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == [1]
test_input = { "s": "uiddqbeoaw", "a": "iddq", "b": "rlr", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "fh", "a": "ywab", "b": "qcjyl", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gdbm", "a": "gdbm", "b": "uefwm", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bpcwswu", "a": "zi", "b": "pcwsw", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "dh", "a": "jmcds", "b": "nytk", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "qjgckhiif", "a": "hiif", "b": "jgc", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [5]
test_input = { "s": "qyixufgyk", "a": "y", "b": "ixuf", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == [1,7]
test_input = { "s": "wiwiwinwio", "a": "hm", "b": "wi", "k": 8 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ffnlge", "a": "bjt", "b": "pavkr", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "rj", "a": "m", "b": "umg", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bkgqxl", "a": "yufy", "b": "kgq", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "hhcwp", "a": "sixek", "b": "cwp", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "czr", "a": "cz", "b": "wxxql", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "tdbnme", "a": "t", "b": "dbnme", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "px", "a": "acgz", "b": "jaxel", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "wfa", "a": "fyntx", "b": "a", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ixfkxfld", "a": "ixfk", "b": "urkke", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "kmjvlkjy", "a": "gll", "b": "vlk", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bsbsvnmvnm", "a": "vnm", "b": "bs", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == [4,7]
test_input = { "s": "uzqauzqw", "a": "uzq", "b": "psnso", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "fsvkche", "a": "yot", "b": "svkc", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "cwwzmfzz", "a": "fnlgc", "b": "cwwzm", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "profguo", "a": "o", "b": "oyzje", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ckbdnw", "a": "djpc", "b": "ckbdn", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ankfahcorr", "a": "r", "b": "kfah", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == [8,9]
test_input = { "s": "ahjzfg", "a": "hjzf", "b": "zs", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "eueuau", "a": "u", "b": "e", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [1,3,5]
test_input = { "s": "etuwwhwljf", "a": "uwwh", "b": "efcuq", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vvjhgg", "a": "g", "b": "kj", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "igytmsmsgx", "a": "msmsg", "b": "gyt", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [4]
test_input = { "s": "cheoeo", "a": "eo", "b": "y", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gqzf", "a": "cgpdn", "b": "zf", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "zapqwtmx", "a": "apqwt", "b": "m", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "klxtee", "a": "e", "b": "klx", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "xa", "a": "gzsj", "b": "oooq", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gxoxqgxoxq", "a": "gxoxq", "b": "x", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [0,5]
test_input = { "s": "lsuo", "a": "d", "b": "uo", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "yhi", "a": "ph", "b": "yhi", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "cj", "a": "j", "b": "em", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "clxzclxz", "a": "ge", "b": "clxz", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gjtcpyiniv", "a": "cpyi", "b": "hjvtq", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "kyrvedszzo", "a": "rve", "b": "y", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [2]
test_input = { "s": "makolbcrme", "a": "qlhpf", "b": "akol", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vgxshd", "a": "vgx", "b": "en", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "wfvxfzut", "a": "wfv", "b": "ut", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "xxtxxuftxt", "a": "tx", "b": "x", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [2,7]
test_input = { "s": "cwtybs", "a": "wgfez", "b": "cwty", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "opnkctux", "a": "op", "b": "nkctu", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "swswmcsksw", "a": "mcsk", "b": "sw", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [4]
test_input = { "s": "qqnb", "a": "q", "b": "q", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [0,1]
test_input = { "s": "tt", "a": "t", "b": "q", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "lllclbii", "a": "l", "b": "i", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == [0,1,2,4]
test_input = { "s": "oanyzue", "a": "yzu", "b": "oan", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == [3]
test_input = { "s": "opmfgzthj", "a": "opmf", "b": "g", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "uiddidde", "a": "idd", "b": "sal", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gzzau", "a": "za", "b": "rwu", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "srpxqurxx", "a": "nsr", "b": "x", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "sxaono", "a": "jy", "b": "xaon", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "acxtjiova", "a": "acx", "b": "tjiov", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "iltazkww", "a": "k", "b": "z", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [5]
test_input = { "s": "ltxbhpi", "a": "cjfbb", "b": "ltxb", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gysgysh", "a": "gys", "b": "qzvae", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "zypvgt", "a": "zypv", "b": "ljxni", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [] | 1,705,199,400 |
weekly-contest-380-maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k | https://leetcode.com/problems/maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k | maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k | {
"questionId": "3240",
"questionFrontendId": "3007",
"title": "Maximum Number That Sum of the Prices Is Less Than or Equal to K",
"titleSlug": "maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 125,
"dislikes": 74,
"categoryTitle": "Algorithms"
} | """
You are given an integer k and an integer x.
Consider s is the 1-indexed binary representation of an integer num. The price of a number num is the number of i's such that i % x == 0 and s[i] is a set bit.
Return the greatest integer num such that the sum of prices of all numbers from 1 to num is less than or equal to k.
Note:
In the binary representation of a number set bit is a bit of value 1.
The binary representation of a number will be indexed from right to left. For example, if s == 11100, s[4] == 1 and s[2] == 0.
Example 1:
Input: k = 9, x = 1
Output: 6
Explanation: The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively.
Since x is equal to 1, the price of each number is the number of its set bits.
The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9.
So the answer is 6.
Example 2:
Input: k = 7, x = 2
Output: 9
Explanation: Since x is equal to 2, we should just check eventh bits.
The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2.
The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2.
The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2.
Numbers 1, 4, and 5 don't have set bits in their eventh bits in their binary representation. So the sum of their prices is 0.
The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2.
The sum of the prices of the first 9 numbers is 6.
Because the sum of the prices of the first 10 numbers is 8, the answer is 9.
Constraints:
1 <= k <= 1015
1 <= x <= 8
"""
class Solution:
def findMaximumNumber(self, k: int, x: int) -> int:
| You are given an integer k and an integer x.
Consider s is the 1-indexed binary representation of an integer num. The price of a number num is the number of i's such that i % x == 0 and s[i] is a set bit.
Return the greatest integer num such that the sum of prices of all numbers from 1 to num is less than or equal to k.
Note:
In the binary representation of a number set bit is a bit of value 1.
The binary representation of a number will be indexed from right to left. For example, if s == 11100, s[4] == 1 and s[2] == 0.
Example 1:
Input: k = 9, x = 1
Output: 6
Explanation: The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively.
Since x is equal to 1, the price of each number is the number of its set bits.
The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9.
So the answer is 6.
Example 2:
Input: k = 7, x = 2
Output: 9
Explanation: Since x is equal to 2, we should just check eventh bits.
The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2.
The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2.
The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2.
Numbers 1, 4, and 5 don't have set bits in their eventh bits in their binary representation. So the sum of their prices is 0.
The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2.
The sum of the prices of the first 9 numbers is 6.
Because the sum of the prices of the first 10 numbers is 8, the answer is 9.
Constraints:
1 <= k <= 1015
1 <= x <= 8
Please complete the code below to solve above prblem:
```python
class Solution:
def findMaximumNumber(self, k: int, x: int) -> int:
``` |
my_solution = Solution()
test_input = { "k": 9, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 6
test_input = { "k": 7, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 9
test_input = { "k": 19, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 50
test_input = { "k": 57, "x": 4 }
assert my_solution.findMaximumNumber(**test_input) == 120
test_input = { "k": 58, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 121
test_input = { "k": 60, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 187
test_input = { "k": 72, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 151
test_input = { "k": 81, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 176
test_input = { "k": 83, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 33
test_input = { "k": 83, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 210
test_input = { "k": 116, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 243
test_input = { "k": 157, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 316
test_input = { "k": 201, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 212
test_input = { "k": 268, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 555
test_input = { "k": 281, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 531
test_input = { "k": 283, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 274
test_input = { "k": 309, "x": 4 }
assert my_solution.findMaximumNumber(**test_input) == 364
test_input = { "k": 363, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 746
test_input = { "k": 409, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 220
test_input = { "k": 456, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 967
test_input = { "k": 466, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 365
test_input = { "k": 500, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 379
test_input = { "k": 513, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 148
test_input = { "k": 521, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 1160
test_input = { "k": 540, "x": 4 }
assert my_solution.findMaximumNumber(**test_input) == 571
test_input = { "k": 545, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 156
test_input = { "k": 579, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 165
test_input = { "k": 584, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 166
test_input = { "k": 589, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 427
test_input = { "k": 599, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 1206
test_input = { "k": 632, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 346
test_input = { "k": 692, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 481
test_input = { "k": 701, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 1404
test_input = { "k": 704, "x": 4 }
assert my_solution.findMaximumNumber(**test_input) == 727
test_input = { "k": 731, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 1498
test_input = { "k": 781, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 210
test_input = { "k": 782, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 1613
test_input = { "k": 808, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 1639
test_input = { "k": 814, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 1645
test_input = { "k": 818, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 218
test_input = { "k": 821, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 433
test_input = { "k": 829, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 1660
test_input = { "k": 865, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 1760
test_input = { "k": 874, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 1769
test_input = { "k": 879, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 230
test_input = { "k": 879, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 628
test_input = { "k": 898, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 1921
test_input = { "k": 902, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 653
test_input = { "k": 905, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 1928
test_input = { "k": 937, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 1960
test_input = { "k": 957, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 701
test_input = { "k": 973, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 247
test_input = { "k": 978, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 737
test_input = { "k": 991, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 1006
test_input = { "k": 1029, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 771
test_input = { "k": 1065, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2083
test_input = { "k": 1086, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 805
test_input = { "k": 1105, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 280
test_input = { "k": 1113, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 815
test_input = { "k": 1143, "x": 4 }
assert my_solution.findMaximumNumber(**test_input) == 1190
test_input = { "k": 1148, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 564
test_input = { "k": 1150, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 2301
test_input = { "k": 1156, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 835
test_input = { "k": 1171, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 2386
test_input = { "k": 1172, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 297
test_input = { "k": 1227, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 2506
test_input = { "k": 1236, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 2515
test_input = { "k": 1270, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 1525
test_input = { "k": 1274, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2220
test_input = { "k": 1281, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2223
test_input = { "k": 1282, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2224
test_input = { "k": 1288, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 1543
test_input = { "k": 1376, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2287
test_input = { "k": 1393, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 2800
test_input = { "k": 1415, "x": 4 }
assert my_solution.findMaximumNumber(**test_input) == 1454
test_input = { "k": 1446, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 2917
test_input = { "k": 1459, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 358
test_input = { "k": 1520, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 1017
test_input = { "k": 1539, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2400
test_input = { "k": 1545, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 3144
test_input = { "k": 1573, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 1732
test_input = { "k": 1588, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 3251
test_input = { "k": 1590, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 3189
test_input = { "k": 1617, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 3280
test_input = { "k": 1633, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2463
test_input = { "k": 1634, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 3297
test_input = { "k": 1687, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 741
test_input = { "k": 1731, "x": 6 }
assert my_solution.findMaximumNumber(**test_input) == 2528
test_input = { "k": 1750, "x": 5 }
assert my_solution.findMaximumNumber(**test_input) == 1850
test_input = { "k": 1751, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 3542
test_input = { "k": 1760, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 3551
test_input = { "k": 1782, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 3573
test_input = { "k": 1787, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 766
test_input = { "k": 1851, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 797
test_input = { "k": 1856, "x": 2 }
assert my_solution.findMaximumNumber(**test_input) == 799
test_input = { "k": 1874, "x": 8 }
assert my_solution.findMaximumNumber(**test_input) == 3793
test_input = { "k": 1893, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 3812
test_input = { "k": 1900, "x": 1 }
assert my_solution.findMaximumNumber(**test_input) == 444
test_input = { "k": 1900, "x": 7 }
assert my_solution.findMaximumNumber(**test_input) == 3819
test_input = { "k": 1902, "x": 3 }
assert my_solution.findMaximumNumber(**test_input) == 1336 | 1,705,199,400 |
weekly-contest-380-find-beautiful-indices-in-the-given-array-ii | https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-ii | find-beautiful-indices-in-the-given-array-ii | {
"questionId": "3303",
"questionFrontendId": "3008",
"title": "Find Beautiful Indices in the Given Array II",
"titleSlug": "find-beautiful-indices-in-the-given-array-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 101,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed string s, a string a, a string b, and an integer k.
An index i is beautiful if:
0 <= i <= s.length - a.length
s[i..(i + a.length - 1)] == a
There exists an index j such that:
0 <= j <= s.length - b.length
s[j..(j + b.length - 1)] == b
|j - i| <= k
Return the array that contains beautiful indices in sorted order from smallest to largest.
Example 1:
Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
Output: [16,33]
Explanation: There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
Example 2:
Input: s = "abcd", a = "a", b = "a", k = 4
Output: [0]
Explanation: There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
Constraints:
1 <= k <= s.length <= 5 * 105
1 <= a.length, b.length <= 5 * 105
s, a, and b contain only lowercase English letters.
"""
class Solution:
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
| You are given a 0-indexed string s, a string a, a string b, and an integer k.
An index i is beautiful if:
0 <= i <= s.length - a.length
s[i..(i + a.length - 1)] == a
There exists an index j such that:
0 <= j <= s.length - b.length
s[j..(j + b.length - 1)] == b
|j - i| <= k
Return the array that contains beautiful indices in sorted order from smallest to largest.
Example 1:
Input: s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
Output: [16,33]
Explanation: There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
Example 2:
Input: s = "abcd", a = "a", b = "a", k = 4
Output: [0]
Explanation: There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
Constraints:
1 <= k <= s.length <= 5 * 105
1 <= a.length, b.length <= 5 * 105
s, a, and b contain only lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "s": "isawsquirrelnearmysquirrelhouseohmy", "a": "my", "b": "squirrel", "k": 15 }
assert my_solution.beautifulIndices(**test_input) == [16,33]
test_input = { "s": "abcd", "a": "a", "b": "a", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "a", "a": "a", "b": "a", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "aba", "a": "a", "b": "a", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == [0,2]
test_input = { "s": "nvnvt", "a": "eq", "b": "nv", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "npearbvede", "a": "myqpb", "b": "pearb", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vatevavakz", "a": "va", "b": "lbda", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ithhi", "a": "t", "b": "hhi", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == [1]
test_input = { "s": "osuv", "a": "osuv", "b": "wrn", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "dc", "a": "dreec", "b": "dc", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "jajrfw", "a": "rf", "b": "j", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [3]
test_input = { "s": "zcvx", "a": "kfdvv", "b": "tru", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "wltmqbxt", "a": "mqbxt", "b": "lt", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == [3]
test_input = { "s": "gggsytwgzg", "a": "sytwg", "b": "g", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == [3]
test_input = { "s": "diive", "a": "viw", "b": "lqqdn", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ss", "a": "omkdt", "b": "s", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "hfzoxcm", "a": "hfzo", "b": "ipelr", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "xllimtmil", "a": "imt", "b": "iwqx", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vdyl", "a": "i", "b": "ir", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ouwpaz", "a": "mxre", "b": "pa", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vlxgolxgoi", "a": "xf", "b": "lxgo", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "pnb", "a": "cx", "b": "pn", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "owhixi", "a": "hixi", "b": "anlc", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "lrtsi", "a": "lrts", "b": "i", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "tpyq", "a": "sa", "b": "py", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "imxclscgz", "a": "iujc", "b": "mxcls", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "sc", "a": "fc", "b": "th", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "tgs", "a": "ldy", "b": "tgs", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ssaeqzzvvg", "a": "ssa", "b": "z", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "zchdy", "a": "zch", "b": "dm", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "youob", "a": "y", "b": "o", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "snwj", "a": "snwj", "b": "ry", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "rneq", "a": "ynprc", "b": "yts", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vxqf", "a": "tcnzs", "b": "qf", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "avnzbrpb", "a": "yzfgy", "b": "cfri", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "de", "a": "segs", "b": "bvdhs", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ulgzs", "a": "eiib", "b": "ulgz", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "tw", "a": "ypf", "b": "svl", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "sotqbvds", "a": "uoj", "b": "s", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "oexy", "a": "e", "b": "a", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "sywgismky", "a": "sywgi", "b": "t", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "onwawarwa", "a": "wa", "b": "r", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [4,7]
test_input = { "s": "xpuldtpxpu", "a": "vkhl", "b": "xpu", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "dg", "a": "amhb", "b": "aqwcf", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "spro", "a": "spro", "b": "lytwu", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "gfjuakm", "a": "fj", "b": "jytnd", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "so", "a": "kkhvu", "b": "rukp", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "zwedfgnra", "a": "dfgn", "b": "zwe", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == [3]
test_input = { "s": "nipetnupg", "a": "n", "b": "ipet", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == [0,5]
test_input = { "s": "xckrhkrnfe", "a": "xc", "b": "kr", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "xibrjp", "a": "ibr", "b": "bpfuf", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "cj", "a": "x", "b": "dea", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "xfejay", "a": "xfej", "b": "koc", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ijkqk", "a": "nzxwn", "b": "vqk", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "dqa", "a": "qj", "b": "norvy", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vbgvuo", "a": "u", "b": "rewjx", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "uw", "a": "flap", "b": "lowqe", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "xoi", "a": "vefut", "b": "x", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "qzfsogwd", "a": "qzfs", "b": "txsdv", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "yalimlim", "a": "lim", "b": "bwi", "k": 8 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "mjvosrhrip", "a": "jzz", "b": "vo", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "dyswswiz", "a": "tib", "b": "dysws", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "ftyhkld", "a": "tyh", "b": "znru", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "adubkehe", "a": "kdtxl", "b": "dubke", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "svpbvld", "a": "d", "b": "svpbv", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "rmiwdb", "a": "rmiw", "b": "xgwcv", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "arikarikox", "a": "o", "b": "arik", "k": 8 }
assert my_solution.beautifulIndices(**test_input) == [8]
test_input = { "s": "cigzky", "a": "cigz", "b": "tu", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "hjlllm", "a": "l", "b": "h", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [2,3,4]
test_input = { "s": "xiyebjzdbv", "a": "iqku", "b": "yebjz", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "qrmogc", "a": "g", "b": "rm", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "lagopphhnl", "a": "gopph", "b": "hnl", "k": 8 }
assert my_solution.beautifulIndices(**test_input) == [2]
test_input = { "s": "xkggxk", "a": "xk", "b": "g", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [0,4]
test_input = { "s": "cdvr", "a": "iemxd", "b": "dt", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "swuaumom", "a": "swuau", "b": "m", "k": 8 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "qftqft", "a": "o", "b": "qft", "k": 1 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "bc", "a": "ucfx", "b": "lzgx", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "egzttzmtot", "a": "boxwe", "b": "t", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "agbx", "a": "a", "b": "tw", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "fkm", "a": "gu", "b": "fkm", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "lt", "a": "z", "b": "lt", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "qkddvykd", "a": "kd", "b": "tprs", "k": 8 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vndnqdehvr", "a": "dnqd", "b": "ybboz", "k": 8 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "lpnltfewly", "a": "few", "b": "l", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [5]
test_input = { "s": "lgioimioim", "a": "imsfs", "b": "ioim", "k": 10 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "jhucel", "a": "iox", "b": "nx", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "vdgtlvls", "a": "lvl", "b": "cu", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "buyryryrjq", "a": "yr", "b": "u", "k": 9 }
assert my_solution.beautifulIndices(**test_input) == [2,4,6]
test_input = { "s": "fwohvc", "a": "agagg", "b": "fwoh", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "tjityoib", "a": "vh", "b": "jityo", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "tssjrbpsck", "a": "ssjr", "b": "sc", "k": 10 }
assert my_solution.beautifulIndices(**test_input) == [1]
test_input = { "s": "aqxv", "a": "t", "b": "x", "k": 4 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "zw", "a": "l", "b": "c", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "cbccekck", "a": "c", "b": "k", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [0,2,3,6]
test_input = { "s": "angkytf", "a": "ngk", "b": "ytf", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == [1]
test_input = { "s": "oloxjjjqj", "a": "olox", "b": "j", "k": 7 }
assert my_solution.beautifulIndices(**test_input) == [0]
test_input = { "s": "zbriomnn", "a": "omn", "b": "zbr", "k": 6 }
assert my_solution.beautifulIndices(**test_input) == [4]
test_input = { "s": "ydmlx", "a": "tu", "b": "dml", "k": 5 }
assert my_solution.beautifulIndices(**test_input) == []
test_input = { "s": "lnoffflno", "a": "lno", "b": "f", "k": 3 }
assert my_solution.beautifulIndices(**test_input) == [0,6]
test_input = { "s": "hwayzb", "a": "fc", "b": "hway", "k": 2 }
assert my_solution.beautifulIndices(**test_input) == [] | 1,705,199,400 |
weekly-contest-379-maximum-area-of-longest-diagonal-rectangle | https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle | maximum-area-of-longest-diagonal-rectangle | {
"questionId": "3251",
"questionFrontendId": "10035",
"title": "Maximum Area of Longest Diagonal Rectangle",
"titleSlug": "maximum-area-of-longest-diagonal-rectangle",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 38,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
You are given a 2D 0-indexed integer array dimensions.
For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i.
Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area.
Example 1:
Input: dimensions = [[9,3],[8,6]]
Output: 48
Explanation:
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
Example 2:
Input: dimensions = [[3,4],[4,3]]
Output: 12
Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.
Constraints:
1 <= dimensions.length <= 100
dimensions[i].length == 2
1 <= dimensions[i][0], dimensions[i][1] <= 100
"""
class Solution:
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
| You are given a 2D 0-indexed integer array dimensions.
For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i.
Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area.
Example 1:
Input: dimensions = [[9,3],[8,6]]
Output: 48
Explanation:
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
Example 2:
Input: dimensions = [[3,4],[4,3]]
Output: 12
Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.
Constraints:
1 <= dimensions.length <= 100
dimensions[i].length == 2
1 <= dimensions[i][0], dimensions[i][1] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "dimensions": [[9,3],[8,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 48
test_input = { "dimensions": [[3,4],[4,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 12
test_input = { "dimensions": [[4,10],[4,9],[9,3],[10,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[2,6],[5,1],[3,10],[8,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 30
test_input = { "dimensions": [[3,7],[2,10],[3,4],[9,9],[5,10]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 81
test_input = { "dimensions": [[10,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 40
test_input = { "dimensions": [[9,9],[1,8],[10,5],[2,8],[6,3],[7,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 81
test_input = { "dimensions": [[10,3],[5,9],[8,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 30
test_input = { "dimensions": [[2,7],[3,2],[3,3],[10,4],[5,3],[8,10],[8,8],[4,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[1,10],[3,10],[4,4],[2,6],[6,3],[6,4],[9,1],[6,1],[2,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 30
test_input = { "dimensions": [[4,7],[10,10],[3,7],[9,1],[5,7],[3,9],[10,4],[4,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[1,1],[6,8],[6,9],[7,2],[6,8],[1,3],[3,1],[1,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 54
test_input = { "dimensions": [[6,6],[1,3],[8,10],[10,1],[3,10],[7,7],[10,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[6,5],[8,6],[2,10],[8,1],[9,2],[3,5],[3,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 20
test_input = { "dimensions": [[5,1],[4,9],[9,1],[5,8],[2,9],[3,2],[10,10],[5,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[8,3],[9,10],[7,7],[6,5],[6,9],[9,10],[5,10]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[6,10],[8,6],[10,1],[7,10],[10,10],[9,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[9,5],[9,2],[2,2],[8,9],[5,7],[8,10],[3,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[3,9],[9,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 45
test_input = { "dimensions": [[10,10],[5,5],[3,2],[2,6],[3,1],[10,7],[4,8],[7,9],[9,9],[1,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[2,3],[3,5],[2,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 15
test_input = { "dimensions": [[4,4],[7,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 49
test_input = { "dimensions": [[7,5],[9,6],[9,4],[5,7],[2,6],[10,3],[9,9],[9,4],[8,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 81
test_input = { "dimensions": [[5,1],[9,1],[7,1],[7,1],[3,1],[10,7],[9,1],[7,2],[4,6],[3,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 70
test_input = { "dimensions": [[8,4],[7,4],[1,5],[7,8],[5,6],[5,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 56
test_input = { "dimensions": [[5,10],[3,7],[8,6],[8,6],[5,9],[10,5],[7,8],[1,9],[2,5],[6,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 50
test_input = { "dimensions": [[9,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 36
test_input = { "dimensions": [[7,6],[2,8],[9,6],[1,10],[5,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 54
test_input = { "dimensions": [[4,2],[1,6],[2,1],[4,10],[10,1],[7,5],[8,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 40
test_input = { "dimensions": [[1,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 4
test_input = { "dimensions": [[9,4],[6,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 36
test_input = { "dimensions": [[7,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 35
test_input = { "dimensions": [[1,9],[9,7],[8,4],[6,6],[7,8],[4,6],[7,4],[9,9],[9,8],[8,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 81
test_input = { "dimensions": [[3,8],[6,3],[5,2],[3,7],[1,3],[9,8],[4,2],[3,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 72
test_input = { "dimensions": [[5,4],[2,4],[8,5],[8,4],[1,2],[6,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 40
test_input = { "dimensions": [[7,2],[4,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 14
test_input = { "dimensions": [[8,10],[5,2],[4,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[9,2],[5,6],[4,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 18
test_input = { "dimensions": [[3,8],[2,9],[7,7],[1,5],[1,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 49
test_input = { "dimensions": [[6,2],[8,2],[6,8],[7,6],[1,2],[6,8],[10,9],[2,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[3,8],[4,1],[5,2],[2,6],[4,9],[10,6],[6,10],[3,4],[6,6],[4,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[5,5],[3,8],[2,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 24
test_input = { "dimensions": [[8,1],[5,8],[3,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 40
test_input = { "dimensions": [[2,8],[8,1],[7,10],[5,7],[2,4],[3,10],[2,10],[7,10],[5,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 70
test_input = { "dimensions": [[3,10],[1,3],[10,5],[5,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 50
test_input = { "dimensions": [[10,6],[4,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[7,8],[8,6],[10,10],[6,7],[7,10]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[7,2],[7,3],[4,6],[4,4],[7,8],[2,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 56
test_input = { "dimensions": [[4,7],[3,1],[1,10],[4,2],[4,10],[8,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 64
test_input = { "dimensions": [[1,8],[4,3],[7,7],[10,6],[5,5],[1,3],[9,1],[8,3],[3,2],[5,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[6,7],[1,7],[5,10],[10,1],[8,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 50
test_input = { "dimensions": [[3,5],[2,7],[4,4],[4,9],[7,6],[2,4],[5,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 36
test_input = { "dimensions": [[8,8],[6,10],[6,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[10,2],[3,3],[5,9],[3,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 45
test_input = { "dimensions": [[4,3],[4,1],[8,9],[10,1],[2,7],[7,7],[9,3],[8,6],[1,5],[8,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 72
test_input = { "dimensions": [[6,8],[2,3],[4,9],[1,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 48
test_input = { "dimensions": [[1,6],[2,10],[1,5],[9,3],[9,1],[2,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 20
test_input = { "dimensions": [[6,5],[7,10],[1,2],[10,3],[4,2],[4,8],[5,10],[5,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 70
test_input = { "dimensions": [[1,2],[1,2],[2,4],[9,9],[3,8],[3,9],[2,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 81
test_input = { "dimensions": [[4,4],[6,1],[1,10],[10,7],[10,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 70
test_input = { "dimensions": [[3,2],[2,8],[10,9],[9,8],[2,2],[9,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[4,10],[9,6],[4,10],[6,7],[2,3],[7,9],[9,2],[1,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 63
test_input = { "dimensions": [[7,4],[10,2],[10,8],[4,9],[4,9],[10,3],[5,4],[4,5],[10,6],[3,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[2,5],[7,4],[5,3],[2,4],[3,10],[3,5],[4,5],[4,4],[6,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 30
test_input = { "dimensions": [[3,2],[7,10],[8,10],[7,4],[6,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[3,8],[4,5],[3,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 24
test_input = { "dimensions": [[6,8],[9,9],[1,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 81
test_input = { "dimensions": [[8,1],[7,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 35
test_input = { "dimensions": [[10,6],[5,1],[9,5],[5,7],[5,8],[6,5],[8,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[8,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 48
test_input = { "dimensions": [[5,2],[5,9],[9,5],[5,5],[8,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 45
test_input = { "dimensions": [[7,8],[9,9],[3,5],[8,1],[1,3],[8,2],[8,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 81
test_input = { "dimensions": [[3,10],[6,8],[4,5],[8,1],[7,2],[9,8],[3,7],[3,3],[9,10]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[1,1],[8,7],[4,6],[5,2],[5,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 56
test_input = { "dimensions": [[6,2],[8,4],[8,6],[2,10],[6,1],[9,8],[10,8],[10,10],[5,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[10,2],[9,7],[4,2],[8,6],[9,10],[10,7],[7,5],[5,10],[5,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[1,4],[7,2],[2,6],[7,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 49
test_input = { "dimensions": [[2,5],[10,10],[4,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[2,10],[10,4],[3,9],[6,10],[2,10],[10,1],[4,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[3,6],[5,4],[9,5],[6,2],[4,4],[7,2],[6,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 45
test_input = { "dimensions": [[1,1],[1,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 7
test_input = { "dimensions": [[1,2],[8,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 64
test_input = { "dimensions": [[3,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 21
test_input = { "dimensions": [[6,7],[1,5],[10,9],[10,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[7,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 56
test_input = { "dimensions": [[2,6],[10,3],[10,5],[1,9],[5,2],[9,10],[7,2],[7,7],[1,10]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[3,4],[8,2],[9,3],[2,9],[6,5],[10,5],[4,1],[8,7],[3,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 50
test_input = { "dimensions": [[7,6],[6,8],[5,7],[1,1],[4,5],[6,10],[9,3],[4,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[1,3],[2,4],[4,9],[10,9],[3,9],[7,5],[2,3],[10,7],[2,3],[1,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[1,8],[6,10],[4,8],[3,8],[4,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 60
test_input = { "dimensions": [[6,5],[3,10],[8,7],[10,10],[2,8],[5,8],[10,8],[9,10],[2,8],[8,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[1,6],[8,3],[6,1],[2,10],[2,5],[3,8]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 20
test_input = { "dimensions": [[7,2],[3,8],[10,10],[7,1],[6,8],[6,7],[10,6],[4,6],[5,7],[10,4]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 100
test_input = { "dimensions": [[9,6]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 54
test_input = { "dimensions": [[8,2],[7,6],[1,4],[1,6],[4,8],[10,9],[9,4],[1,5]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 90
test_input = { "dimensions": [[7,3],[2,5],[7,1],[10,7],[7,4],[8,1]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 70
test_input = { "dimensions": [[9,2]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 18
test_input = { "dimensions": [[9,2],[7,2],[2,7]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 18
test_input = { "dimensions": [[2,8],[10,6],[8,10],[9,9]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 80
test_input = { "dimensions": [[3,3]] }
assert my_solution.areaOfMaxDiagonal(**test_input) == 9 | 1,704,594,600 |
weekly-contest-379-minimum-moves-to-capture-the-queen | https://leetcode.com/problems/minimum-moves-to-capture-the-queen | minimum-moves-to-capture-the-queen | {
"questionId": "3270",
"questionFrontendId": "10036",
"title": "Minimum Moves to Capture The Queen",
"titleSlug": "minimum-moves-to-capture-the-queen",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 71,
"dislikes": 115,
"categoryTitle": "Algorithms"
} | """
There is a 1-indexed 8 x 8 chessboard containing 3 pieces.
You are given 6 integers a, b, c, d, e, and f where:
(a, b) denotes the position of the white rook.
(c, d) denotes the position of the white bishop.
(e, f) denotes the position of the black queen.
Given that you can only move the white pieces, return the minimum number of moves required to capture the black queen.
Note that:
Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.
Bishops can move any number of squares diagonally, but cannot jump over other pieces.
A rook or a bishop can capture the queen if it is located in a square that they can move to.
The queen does not move.
Example 1:
Input: a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
Output: 2
Explanation: We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).
It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.
Example 2:
Input: a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
Output: 1
Explanation: We can capture the black queen in a single move by doing one of the following:
- Move the white rook to (5, 2).
- Move the white bishop to (5, 2).
Constraints:
1 <= a, b, c, d, e, f <= 8
No two pieces are on the same square.
"""
class Solution:
def minMovesToCaptureTheQueen(self, a: int, b: int, c: int, d: int, e: int, f: int) -> int:
| There is a 1-indexed 8 x 8 chessboard containing 3 pieces.
You are given 6 integers a, b, c, d, e, and f where:
(a, b) denotes the position of the white rook.
(c, d) denotes the position of the white bishop.
(e, f) denotes the position of the black queen.
Given that you can only move the white pieces, return the minimum number of moves required to capture the black queen.
Note that:
Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.
Bishops can move any number of squares diagonally, but cannot jump over other pieces.
A rook or a bishop can capture the queen if it is located in a square that they can move to.
The queen does not move.
Example 1:
Input: a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
Output: 2
Explanation: We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).
It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.
Example 2:
Input: a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
Output: 1
Explanation: We can capture the black queen in a single move by doing one of the following:
- Move the white rook to (5, 2).
- Move the white bishop to (5, 2).
Constraints:
1 <= a, b, c, d, e, f <= 8
No two pieces are on the same square.
Please complete the code below to solve above prblem:
```python
class Solution:
def minMovesToCaptureTheQueen(self, a: int, b: int, c: int, d: int, e: int, f: int) -> int:
``` |
my_solution = Solution()
test_input = { "a": 1, "b": 1, "c": 8, "d": 8, "e": 2, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 3, "c": 3, "d": 4, "e": 5, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 4, "b": 3, "c": 3, "d": 4, "e": 5, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 1, "c": 1, "d": 4, "e": 1, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 3, "c": 3, "d": 4, "e": 2, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 8, "b": 4, "c": 7, "d": 5, "e": 5, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 8, "c": 4, "d": 3, "e": 2, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 6, "c": 3, "d": 3, "e": 5, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 5, "b": 3, "c": 6, "d": 6, "e": 6, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 1, "c": 6, "d": 6, "e": 2, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 4, "c": 8, "d": 8, "e": 7, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 5, "b": 1, "c": 6, "d": 5, "e": 3, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 2, "c": 8, "d": 6, "e": 7, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 1, "c": 8, "d": 2, "e": 4, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 3, "b": 1, "c": 1, "d": 1, "e": 2, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 5, "c": 2, "d": 4, "e": 5, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 8, "b": 1, "c": 5, "d": 8, "e": 1, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 6, "c": 6, "d": 2, "e": 1, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 5, "c": 7, "d": 6, "e": 2, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 6, "c": 3, "d": 2, "e": 6, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 3, "b": 3, "c": 8, "d": 1, "e": 2, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 5, "c": 6, "d": 1, "e": 4, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 7, "b": 2, "c": 2, "d": 8, "e": 7, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 3, "b": 8, "c": 2, "d": 7, "e": 1, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 3, "b": 5, "c": 5, "d": 3, "e": 1, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 2, "b": 2, "c": 7, "d": 4, "e": 3, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 3, "c": 1, "d": 5, "e": 5, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 7, "b": 5, "c": 2, "d": 5, "e": 6, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 7, "c": 8, "d": 1, "e": 3, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 6, "c": 6, "d": 4, "e": 1, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 8, "c": 4, "d": 4, "e": 3, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 4, "c": 7, "d": 7, "e": 4, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 5, "b": 7, "c": 4, "d": 2, "e": 3, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 6, "b": 1, "c": 7, "d": 8, "e": 8, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 3, "b": 7, "c": 7, "d": 6, "e": 7, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 3, "c": 6, "d": 4, "e": 6, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 7, "c": 1, "d": 1, "e": 5, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 6, "b": 8, "c": 5, "d": 8, "e": 5, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 2, "c": 3, "d": 7, "e": 6, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 3, "b": 2, "c": 6, "d": 1, "e": 6, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 5, "b": 4, "c": 6, "d": 7, "e": 1, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 6, "c": 2, "d": 3, "e": 3, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 7, "b": 6, "c": 2, "d": 1, "e": 2, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 5, "b": 8, "c": 6, "d": 5, "e": 6, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 6, "b": 2, "c": 6, "d": 7, "e": 5, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 5, "c": 4, "d": 4, "e": 7, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 6, "b": 1, "c": 4, "d": 4, "e": 2, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 3, "b": 4, "c": 1, "d": 8, "e": 3, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 7, "b": 7, "c": 1, "d": 6, "e": 3, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 6, "b": 8, "c": 2, "d": 2, "e": 3, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 5, "c": 3, "d": 7, "e": 5, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 7, "b": 3, "c": 5, "d": 3, "e": 8, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 4, "c": 1, "d": 4, "e": 7, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 6, "b": 3, "c": 7, "d": 3, "e": 4, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 4, "c": 6, "d": 7, "e": 4, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 4, "b": 5, "c": 6, "d": 8, "e": 2, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 6, "c": 5, "d": 7, "e": 8, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 1, "c": 8, "d": 2, "e": 1, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 2, "c": 8, "d": 3, "e": 2, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 1, "b": 7, "c": 5, "d": 5, "e": 4, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 3, "c": 7, "d": 8, "e": 1, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 7, "c": 6, "d": 8, "e": 6, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 3, "c": 4, "d": 7, "e": 7, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 4, "c": 6, "d": 6, "e": 2, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 2, "c": 6, "d": 4, "e": 1, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 3, "b": 5, "c": 3, "d": 2, "e": 1, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 3, "b": 6, "c": 1, "d": 3, "e": 3, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 1, "b": 1, "c": 7, "d": 6, "e": 4, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 1, "b": 8, "c": 7, "d": 2, "e": 6, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 5, "b": 8, "c": 4, "d": 5, "e": 7, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 3, "c": 8, "d": 8, "e": 5, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 7, "b": 2, "c": 8, "d": 1, "e": 4, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 3, "b": 5, "c": 8, "d": 7, "e": 6, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 5, "c": 2, "d": 2, "e": 1, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 1, "b": 8, "c": 8, "d": 4, "e": 4, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 3, "b": 6, "c": 7, "d": 6, "e": 8, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 6, "c": 3, "d": 7, "e": 3, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 1, "b": 5, "c": 3, "d": 4, "e": 7, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 6, "c": 2, "d": 2, "e": 5, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 7, "b": 3, "c": 2, "d": 6, "e": 3, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 4, "c": 6, "d": 5, "e": 5, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 1, "b": 1, "c": 2, "d": 4, "e": 5, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 4, "b": 5, "c": 5, "d": 4, "e": 2, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 6, "b": 5, "c": 6, "d": 2, "e": 2, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 5, "c": 4, "d": 2, "e": 8, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 1, "c": 2, "d": 5, "e": 8, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 4, "b": 8, "c": 3, "d": 8, "e": 8, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 8, "c": 4, "d": 7, "e": 5, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 5, "b": 5, "c": 3, "d": 6, "e": 8, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 2, "c": 3, "d": 1, "e": 2, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 8, "c": 6, "d": 3, "e": 2, "f": 8 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 6, "b": 1, "c": 8, "d": 8, "e": 7, "f": 3 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 8, "c": 6, "d": 1, "e": 4, "f": 2 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 6, "c": 4, "d": 2, "e": 8, "f": 5 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 1, "b": 4, "c": 2, "d": 5, "e": 5, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1
test_input = { "a": 4, "b": 4, "c": 3, "d": 2, "e": 6, "f": 7 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 8, "b": 3, "c": 6, "d": 7, "e": 4, "f": 6 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 2, "b": 5, "c": 1, "d": 1, "e": 8, "f": 4 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 7, "b": 8, "c": 1, "d": 4, "e": 5, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
test_input = { "a": 5, "b": 5, "c": 2, "d": 1, "e": 1, "f": 1 }
assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 | 1,704,594,600 |
weekly-contest-379-maximum-size-of-a-set-after-removals | https://leetcode.com/problems/maximum-size-of-a-set-after-removals | maximum-size-of-a-set-after-removals | {
"questionId": "3228",
"questionFrontendId": "10037",
"title": "Maximum Size of a Set After Removals",
"titleSlug": "maximum-size-of-a-set-after-removals",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 116,
"dislikes": 11,
"categoryTitle": "Algorithms"
} | """
You are given two 0-indexed integer arrays nums1 and nums2 of even length n.
You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s.
Return the maximum possible size of the set s.
Example 1:
Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
Output: 2
Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
It can be shown that 2 is the maximum possible size of the set s after the removals.
Example 2:
Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
Output: 5
Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
It can be shown that 5 is the maximum possible size of the set s after the removals.
Example 3:
Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
Output: 6
Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
It can be shown that 6 is the maximum possible size of the set s after the removals.
Constraints:
n == nums1.length == nums2.length
1 <= n <= 2 * 104
n is even.
1 <= nums1[i], nums2[i] <= 109
"""
class Solution:
def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
| You are given two 0-indexed integer arrays nums1 and nums2 of even length n.
You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s.
Return the maximum possible size of the set s.
Example 1:
Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
Output: 2
Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
It can be shown that 2 is the maximum possible size of the set s after the removals.
Example 2:
Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
Output: 5
Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
It can be shown that 5 is the maximum possible size of the set s after the removals.
Example 3:
Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
Output: 6
Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
It can be shown that 6 is the maximum possible size of the set s after the removals.
Constraints:
n == nums1.length == nums2.length
1 <= n <= 2 * 104
n is even.
1 <= nums1[i], nums2[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums1": [1,2,1,2], "nums2": [1,1,1,1] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [1,2,3,4,5,6], "nums2": [2,3,2,3,2,3] }
assert my_solution.maximumSetSize(**test_input) == 5
test_input = { "nums1": [1,1,2,2,3,3], "nums2": [4,4,5,5,6,6] }
assert my_solution.maximumSetSize(**test_input) == 6
test_input = { "nums1": [1,2,1,1], "nums2": [1,2,3,4] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [1,1,1,1], "nums2": [12,23,41,9] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [12,23,41,9], "nums2": [1,1,1,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [9,8,4,7], "nums2": [5,5,9,5] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [8,9], "nums2": [4,3] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [7,1], "nums2": [6,10] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [10,3], "nums2": [5,6] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [3,6], "nums2": [6,6] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [5,1], "nums2": [6,6] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [10,7], "nums2": [8,4] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [10,8,7,9], "nums2": [7,9,9,5] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [1,10,6,5], "nums2": [3,7,10,10] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [5,2,8,6], "nums2": [7,4,1,1] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [2,4,1,4], "nums2": [10,2,4,10] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [5,7], "nums2": [3,1] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [1,10,1,2], "nums2": [9,5,8,5] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [9,4], "nums2": [5,7] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [1,5,10,8], "nums2": [1,7,4,10] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [6,6,2,9], "nums2": [1,4,10,7] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [7,10], "nums2": [6,10] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [8,8], "nums2": [6,3] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [6,8], "nums2": [9,3] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [3,8,1,9], "nums2": [2,5,4,5] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [6,1,4,7], "nums2": [10,7,2,2] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [8,7,9,3], "nums2": [10,3,8,2] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [1,4,5,9], "nums2": [2,5,2,7] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [3,5], "nums2": [5,3] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [1,10,8,2], "nums2": [2,9,10,7] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [3,9], "nums2": [1,4] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [1,5], "nums2": [10,5] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [7,5], "nums2": [2,10] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [6,10], "nums2": [3,1] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [9,8,1,3], "nums2": [4,9,8,6] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [4,1], "nums2": [9,9] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [10,7], "nums2": [10,8] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [4,4,3,9], "nums2": [6,8,4,7] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [4,10,4,9], "nums2": [5,7,4,2] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [8,6], "nums2": [1,7] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [9,8,10,7], "nums2": [3,7,7,6] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [4,10,9,10], "nums2": [9,7,3,6] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [6,7], "nums2": [5,7] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [1,1,10,5], "nums2": [6,6,8,5] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [5,3,4,2], "nums2": [10,3,7,10] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [1,3], "nums2": [9,2] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [6,1,6,2], "nums2": [5,4,6,7] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [9,9], "nums2": [8,7] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [6,2,9,3], "nums2": [10,3,4,7] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [6,2,10,1], "nums2": [9,2,6,5] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [6,5,6,1], "nums2": [6,2,6,9] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [10,5,4,7], "nums2": [5,4,4,9] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [7,10,4,6], "nums2": [1,4,4,2] }
assert my_solution.maximumSetSize(**test_input) == 4
test_input = { "nums1": [3,4], "nums2": [1,8] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [7,7], "nums2": [7,1] }
assert my_solution.maximumSetSize(**test_input) == 2
test_input = { "nums1": [1,1,2,2,1,1], "nums2": [1,3,2,2,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,3,3,2,1,2,1,2], "nums2": [1,2,3,2,2,3,3,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,3,2], "nums2": [2,2,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,1,1,3,3,3,1,1], "nums2": [3,2,3,2,3,3,3,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,3,1,1,3,2], "nums2": [2,2,1,1,2,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,3,2,3,3,1], "nums2": [2,3,1,2,1,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,1,3,3,1,3,3,3], "nums2": [2,3,1,3,1,1,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,3,2,2,1,3,1,1], "nums2": [3,1,2,2,3,1,2,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,2,2,3,1,1,3,3], "nums2": [3,1,3,1,2,3,2,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,3,1,3,2,2,3,1], "nums2": [3,1,2,3,1,3,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,1,1,2,1,1,1,1,1,2], "nums2": [3,1,3,3,1,1,3,2,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,2,2,3], "nums2": [1,1,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,3,3,1,2,1,2,2], "nums2": [1,2,2,1,2,1,2,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,2,1,3,2,3], "nums2": [3,3,1,1,3,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,1,3,3,3,3,2,2,2], "nums2": [2,1,3,2,2,3,3,1,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,1,1,2,2,1,1], "nums2": [2,3,3,1,3,2,3,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,3,2,2,2,2,1,2], "nums2": [3,1,2,3,3,1,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,3,2,1,2,3], "nums2": [2,1,1,3,2,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,1,2,1,1,2,3], "nums2": [1,2,2,3,2,3,3,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,1,1,3,1,2,1,1], "nums2": [2,3,3,2,3,3,1,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,1,3,3,2,2,2,3,1,1], "nums2": [1,1,2,1,3,3,1,3,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,1,1,2,3,2,1,1], "nums2": [2,1,2,2,2,1,3,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,1,3,2,2,2,2,2], "nums2": [1,3,3,2,2,2,3,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,1,1], "nums2": [1,3,1,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,3,1,3,2], "nums2": [2,1,3,2,2,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,2,3,3,2,2], "nums2": [1,3,1,3,1,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,1,3,2,3], "nums2": [2,1,1,1,2,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,1,3,1,3,1], "nums2": [3,3,1,2,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,2,2,2,2,3,1,3], "nums2": [2,3,3,2,1,2,3,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,3,1,2,3,1], "nums2": [3,3,2,2,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,1,2,2,1,3], "nums2": [1,1,1,3,2,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,2,1,2,1,3,3,1], "nums2": [3,1,3,1,1,3,3,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,1,3,3,2,3], "nums2": [1,3,3,2,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,2,1,3,3,2], "nums2": [1,1,3,3,1,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,1,3,2,3,3], "nums2": [2,2,2,3,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,3,3,1,2,2,3,1], "nums2": [3,1,2,3,2,3,2,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,1,3,1,2,1,2,2,2,2], "nums2": [2,1,3,1,1,1,2,2,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [1,2,2,1,3,3], "nums2": [1,1,1,3,2,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,1,2,3], "nums2": [3,2,3,2] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,1,2,2], "nums2": [2,1,1,1] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [2,2,3,2], "nums2": [2,1,3,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,3,2,1,3,3,2,3], "nums2": [3,1,2,3,2,2,1,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,3,3,1,2,2], "nums2": [1,3,1,2,2,3] }
assert my_solution.maximumSetSize(**test_input) == 3
test_input = { "nums1": [3,1,1,3,1,1,3,2], "nums2": [3,2,3,3,3,1,2,3] }
assert my_solution.maximumSetSize(**test_input) == 3 | 1,704,594,600 |
weekly-contest-379-maximize-the-number-of-partitions-after-operations | https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations | maximize-the-number-of-partitions-after-operations | {
"questionId": "3233",
"questionFrontendId": "10038",
"title": "Maximize the Number of Partitions After Operations",
"titleSlug": "maximize-the-number-of-partitions-after-operations",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 41,
"dislikes": 11,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed string s and an integer k.
You are to perform the following partitioning operations until s is empty:
Choose the longest prefix of s containing at most k distinct characters.
Delete the prefix from s and increase the number of partitions by one. The remaining characters (if any) in s maintain their initial order.
Before the operations, you are allowed to change at most one index in s to another lowercase English letter.
Return an integer denoting the maximum number of resulting partitions after the operations by optimally choosing at most one index to change.
Example 1:
Input: s = "accca", k = 2
Output: 3
Explanation: In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'.
s becomes "acbca".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 2 distinct characters, "acbca".
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
- Choose the longest prefix containing at most 2 distinct characters, "bca".
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
- Choose the longest prefix containing at most 2 distinct characters, "a".
- Delete the prefix, and s becomes empty. The number of partitions is now 3.
Hence, the answer is 3.
It can be shown that it is not possible to obtain more than 3 partitions.
Example 2:
Input: s = "aabaab", k = 3
Output: 1
Explanation: In this example, to maximize the number of resulting partitions we can leave s as it is.
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 3 distinct characters, "aabaab".
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.
Hence, the answer is 1.
It can be shown that it is not possible to obtain more than 1 partition.
Example 3:
Input: s = "xxyz", k = 1
Output: 4
Explanation: In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'.
s becomes "xayz".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 1 distinct character, "xayz".
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
- Choose the longest prefix containing at most 1 distinct character, "ayz".
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
- Choose the longest prefix containing at most 1 distinct character, "yz".
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
- Choose the longest prefix containing at most 1 distinct character, "z".
- Delete the prefix, and s becomes empty. The number of partitions is now 4.
Hence, the answer is 4.
It can be shown that it is not possible to obtain more than 4 partitions.
Constraints:
1 <= s.length <= 104
s consists only of lowercase English letters.
1 <= k <= 26
"""
class Solution:
def maxPartitionsAfterOperations(self, s: str, k: int) -> int:
| You are given a 0-indexed string s and an integer k.
You are to perform the following partitioning operations until s is empty:
Choose the longest prefix of s containing at most k distinct characters.
Delete the prefix from s and increase the number of partitions by one. The remaining characters (if any) in s maintain their initial order.
Before the operations, you are allowed to change at most one index in s to another lowercase English letter.
Return an integer denoting the maximum number of resulting partitions after the operations by optimally choosing at most one index to change.
Example 1:
Input: s = "accca", k = 2
Output: 3
Explanation: In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'.
s becomes "acbca".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 2 distinct characters, "acbca".
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
- Choose the longest prefix containing at most 2 distinct characters, "bca".
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
- Choose the longest prefix containing at most 2 distinct characters, "a".
- Delete the prefix, and s becomes empty. The number of partitions is now 3.
Hence, the answer is 3.
It can be shown that it is not possible to obtain more than 3 partitions.
Example 2:
Input: s = "aabaab", k = 3
Output: 1
Explanation: In this example, to maximize the number of resulting partitions we can leave s as it is.
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 3 distinct characters, "aabaab".
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.
Hence, the answer is 1.
It can be shown that it is not possible to obtain more than 1 partition.
Example 3:
Input: s = "xxyz", k = 1
Output: 4
Explanation: In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'.
s becomes "xayz".
The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 1 distinct character, "xayz".
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
- Choose the longest prefix containing at most 1 distinct character, "ayz".
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
- Choose the longest prefix containing at most 1 distinct character, "yz".
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
- Choose the longest prefix containing at most 1 distinct character, "z".
- Delete the prefix, and s becomes empty. The number of partitions is now 4.
Hence, the answer is 4.
It can be shown that it is not possible to obtain more than 4 partitions.
Constraints:
1 <= s.length <= 104
s consists only of lowercase English letters.
1 <= k <= 26
Please complete the code below to solve above prblem:
```python
class Solution:
def maxPartitionsAfterOperations(self, s: str, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "s": "accca", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "aabaab", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "xxyz", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "c", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "c", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "h", "k": 17 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "p", "k": 13 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "ab", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "ba", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "ba", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "ca", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "fh", "k": 8 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "abb", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "aca", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "acb", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "acb", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bab", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "cba", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "cbb", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "cca", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "fjz", "k": 11 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "jxg", "k": 23 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "xfj", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "abcc", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "acab", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "altj", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "baac", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "baca", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "bbkk", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "cbcc", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "toqm", "k": 14 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "yhbo", "k": 9 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "aaabc", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 5
test_input = { "s": "accba", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "bcbab", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 5
test_input = { "s": "bccaa", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "caaaa", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "cacaa", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "cacac", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 5
test_input = { "s": "cbbab", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "ccacb", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "ccbba", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "yhqlp", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "aabaaa", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "acbabb", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 6
test_input = { "s": "bbaaab", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bbbcca", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bbcaab", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "cacbaa", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 6
test_input = { "s": "hnhdfs", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "nihnrq", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 6
test_input = { "s": "odxttm", "k": 19 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "uxqozq", "k": 11 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "aabcaac", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "aabcacc", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "abbaaca", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "baacaac", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "baacbaa", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "baacccb", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 6
test_input = { "s": "baccacb", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "baccccb", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "bcbbccb", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "cbbcaab", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "ccabbaa", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "cccabcc", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "tmdhzhy", "k": 26 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "abhujxlb", "k": 13 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "acabcbcb", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "acbcbcac", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "adklnsqm", "k": 13 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bababaca", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bcacbaaa", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "bcccbacc", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "aaaabcccb", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 6
test_input = { "s": "acabcaacc", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "bcbbabcaa", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bccaabcaa", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "cbcabbcca", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "ccbaabbba", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "aaaaabbcab", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "aababbbbca", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "aabbbbccab", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 7
test_input = { "s": "abbcbbcbba", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 8
test_input = { "s": "abcbbaccbb", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "acabacccac", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "acbbcacbab", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bbaccabbac", "k": 4 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "bbbbcbcbbc", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "bsoefqekpl", "k": 6 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "caacccbcac", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "cccacccbcb", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "vwfyagymtp", "k": 20 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "abaaccbaaaa", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 4
test_input = { "s": "abcccaccccc", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "cbbbccaacaa", "k": 3 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 2
test_input = { "s": "ccaaabaaaaa", "k": 2 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 3
test_input = { "s": "wcmgarcruky", "k": 10 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "yyogqjsswyn", "k": 17 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
test_input = { "s": "aaaccccbbbca", "k": 1 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 7
test_input = { "s": "baaccaabaccc", "k": 5 }
assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 | 1,704,594,600 |
biweekly-contest-121-smallest-missing-integer-greater-than-sequential-prefix-sum | https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum | smallest-missing-integer-greater-than-sequential-prefix-sum | {
"questionId": "3236",
"questionFrontendId": "10031",
"title": "Smallest Missing Integer Greater Than Sequential Prefix Sum",
"titleSlug": "smallest-missing-integer-greater-than-sequential-prefix-sum",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 41,
"dislikes": 122,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of integers nums.
A prefix nums[0..i] is sequential if, for all 1 <= j <= i, nums[j] = nums[j - 1] + 1. In particular, the prefix consisting only of nums[0] is sequential.
Return the smallest integer x missing from nums such that x is greater than or equal to the sum of the longest sequential prefix.
Example 1:
Input: nums = [1,2,3,2,5]
Output: 6
Explanation: The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
Example 2:
Input: nums = [3,4,5,1,12,14,13]
Output: 15
Explanation: The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 50
"""
class Solution:
def missingInteger(self, nums: List[int]) -> int:
| You are given a 0-indexed array of integers nums.
A prefix nums[0..i] is sequential if, for all 1 <= j <= i, nums[j] = nums[j - 1] + 1. In particular, the prefix consisting only of nums[0] is sequential.
Return the smallest integer x missing from nums such that x is greater than or equal to the sum of the longest sequential prefix.
Example 1:
Input: nums = [1,2,3,2,5]
Output: 6
Explanation: The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
Example 2:
Input: nums = [3,4,5,1,12,14,13]
Output: 15
Explanation: The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def missingInteger(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,2,5] }
assert my_solution.missingInteger(**test_input) == 6
test_input = { "nums": [3,4,5,1,12,14,13] }
assert my_solution.missingInteger(**test_input) == 15
test_input = { "nums": [29,30,31,32,33,34,35,36,37] }
assert my_solution.missingInteger(**test_input) == 297
test_input = { "nums": [19,20,21,22] }
assert my_solution.missingInteger(**test_input) == 82
test_input = { "nums": [18,19,20,21,22,23,24,25,26,27,28,9] }
assert my_solution.missingInteger(**test_input) == 253
test_input = { "nums": [4,5,6,7,8,8,9,4,3,2,7] }
assert my_solution.missingInteger(**test_input) == 30
test_input = { "nums": [38] }
assert my_solution.missingInteger(**test_input) == 39
test_input = { "nums": [1] }
assert my_solution.missingInteger(**test_input) == 2
test_input = { "nums": [11,12,13] }
assert my_solution.missingInteger(**test_input) == 36
test_input = { "nums": [47,48,49,5,3] }
assert my_solution.missingInteger(**test_input) == 144
test_input = { "nums": [23,24,25,4,5,1] }
assert my_solution.missingInteger(**test_input) == 72
test_input = { "nums": [8,9,10,10,7,8] }
assert my_solution.missingInteger(**test_input) == 27
test_input = { "nums": [31,32,33,34,10,8,7,9,7,9,9,5,10,1] }
assert my_solution.missingInteger(**test_input) == 130
test_input = { "nums": [17,18,19,20,21,22,3,7,10,10] }
assert my_solution.missingInteger(**test_input) == 117
test_input = { "nums": [6,7,8,9,10,8,6,7,4,1] }
assert my_solution.missingInteger(**test_input) == 40
test_input = { "nums": [46,8,2,4,1,4,10,2,4,10,2,5,7,3,1] }
assert my_solution.missingInteger(**test_input) == 47
test_input = { "nums": [37,1,2,9,5,8,5,2,9,4] }
assert my_solution.missingInteger(**test_input) == 38
test_input = { "nums": [31,32,33,34,1] }
assert my_solution.missingInteger(**test_input) == 130
test_input = { "nums": [45,46,47,48,49,10,8,1,7,4,10,10,6,6,2] }
assert my_solution.missingInteger(**test_input) == 235
test_input = { "nums": [13,10,7,5,7,10,6,10,2] }
assert my_solution.missingInteger(**test_input) == 14
test_input = { "nums": [32,33,34,35,36,37,38,39,40,41,42,43,44,8,6] }
assert my_solution.missingInteger(**test_input) == 494
test_input = { "nums": [24,8,9] }
assert my_solution.missingInteger(**test_input) == 25
test_input = { "nums": [47,48,49,9,3,8,1,9,2,5,4,5,9] }
assert my_solution.missingInteger(**test_input) == 144
test_input = { "nums": [4,5,6,7,8,9,4,7,10,7,2] }
assert my_solution.missingInteger(**test_input) == 39
test_input = { "nums": [28,29] }
assert my_solution.missingInteger(**test_input) == 57
test_input = { "nums": [40,41,42,3,8,2,7,1,4] }
assert my_solution.missingInteger(**test_input) == 123
test_input = { "nums": [17,18,19,20,21,22,23,24,25,26,27,9,2,5] }
assert my_solution.missingInteger(**test_input) == 242
test_input = { "nums": [43,44] }
assert my_solution.missingInteger(**test_input) == 87
test_input = { "nums": [19,20,5,3,10] }
assert my_solution.missingInteger(**test_input) == 39
test_input = { "nums": [5] }
assert my_solution.missingInteger(**test_input) == 6
test_input = { "nums": [14,15] }
assert my_solution.missingInteger(**test_input) == 29
test_input = { "nums": [47,48,49] }
assert my_solution.missingInteger(**test_input) == 144
test_input = { "nums": [10] }
assert my_solution.missingInteger(**test_input) == 11
test_input = { "nums": [39] }
assert my_solution.missingInteger(**test_input) == 40
test_input = { "nums": [11,12,13,14,15,7,5,2,10,5,6] }
assert my_solution.missingInteger(**test_input) == 65
test_input = { "nums": [3,4,5,7,9,8,1,3,4,9] }
assert my_solution.missingInteger(**test_input) == 12
test_input = { "nums": [29,30,31,32,33,34,35,36,37,38,39,40,41] }
assert my_solution.missingInteger(**test_input) == 455
test_input = { "nums": [24,25,26,27,28,29,30,31,32,33,34,35,6,4,1] }
assert my_solution.missingInteger(**test_input) == 354
test_input = { "nums": [7,8,9,10,11,12,13,14,15] }
assert my_solution.missingInteger(**test_input) == 99
test_input = { "nums": [39,40,41,42,43,44,45,8,10,4] }
assert my_solution.missingInteger(**test_input) == 294
test_input = { "nums": [36,37,6,8] }
assert my_solution.missingInteger(**test_input) == 73
test_input = { "nums": [27,28,29,30] }
assert my_solution.missingInteger(**test_input) == 114
test_input = { "nums": [34,35,5,7] }
assert my_solution.missingInteger(**test_input) == 69
test_input = { "nums": [9,8,6,1] }
assert my_solution.missingInteger(**test_input) == 10
test_input = { "nums": [36,37,38,39,8,10,7] }
assert my_solution.missingInteger(**test_input) == 150
test_input = { "nums": [28,29,6] }
assert my_solution.missingInteger(**test_input) == 57
test_input = { "nums": [14,15,16,17,18,19,20,10,9,10,9,7,3,6] }
assert my_solution.missingInteger(**test_input) == 119
test_input = { "nums": [27,28,29,5] }
assert my_solution.missingInteger(**test_input) == 84
test_input = { "nums": [42,43,44,45,46,47,48] }
assert my_solution.missingInteger(**test_input) == 315
test_input = { "nums": [2,3,4,5,6,7,8,9,10,11,1,10,5,6] }
assert my_solution.missingInteger(**test_input) == 65
test_input = { "nums": [32,33,34,35,36,37,5,8,5,3,4,2,10,3,7] }
assert my_solution.missingInteger(**test_input) == 207
test_input = { "nums": [24,25,26,27,28,29,30,31,32,33,1,3,9] }
assert my_solution.missingInteger(**test_input) == 285
test_input = { "nums": [48,49] }
assert my_solution.missingInteger(**test_input) == 97
test_input = { "nums": [46,47,6,7,1] }
assert my_solution.missingInteger(**test_input) == 93
test_input = { "nums": [32,33,34,35,36,37,38,39,40] }
assert my_solution.missingInteger(**test_input) == 324
test_input = { "nums": [40,41,42,43,44,45,6] }
assert my_solution.missingInteger(**test_input) == 255
test_input = { "nums": [5,6,7,8,9,10,11,12,13,14,15,16,17,18,9] }
assert my_solution.missingInteger(**test_input) == 161
test_input = { "nums": [39,40,41,3,4,7,10,6,2,10,1,9] }
assert my_solution.missingInteger(**test_input) == 120
test_input = { "nums": [17,18] }
assert my_solution.missingInteger(**test_input) == 35
test_input = { "nums": [41,42,43,44,45,46,5,6] }
assert my_solution.missingInteger(**test_input) == 261
test_input = { "nums": [6] }
assert my_solution.missingInteger(**test_input) == 7
test_input = { "nums": [46,47,48,49,50,7] }
assert my_solution.missingInteger(**test_input) == 240
test_input = { "nums": [17,18,19,20,21,22,23,24,25,26,4,7,5,4,4] }
assert my_solution.missingInteger(**test_input) == 215
test_input = { "nums": [40,41,42,43,44,45,46,4,6] }
assert my_solution.missingInteger(**test_input) == 301
test_input = { "nums": [13,4,2,2,3,4,1,8,3,7,7,7,1,6,3] }
assert my_solution.missingInteger(**test_input) == 14
test_input = { "nums": [4,5,6,7,8,9,10,11,12,13,14,15,16,6,8] }
assert my_solution.missingInteger(**test_input) == 130
test_input = { "nums": [12,10] }
assert my_solution.missingInteger(**test_input) == 13
test_input = { "nums": [17,18,19,20,21,5,3,7,10,5,3,7,3,5,3] }
assert my_solution.missingInteger(**test_input) == 95
test_input = { "nums": [38,39,40,41,42,43,44,45,5,7,9,9,4,1] }
assert my_solution.missingInteger(**test_input) == 332
test_input = { "nums": [32,33,34,35] }
assert my_solution.missingInteger(**test_input) == 134
test_input = { "nums": [33,34,7,3,4,4] }
assert my_solution.missingInteger(**test_input) == 67
test_input = { "nums": [33,34,35,36,37,38,39,40,41,42,43,44,45,46,47] }
assert my_solution.missingInteger(**test_input) == 600
test_input = { "nums": [14,9,6,9,7,9,10,4,9,9,4,4] }
assert my_solution.missingInteger(**test_input) == 15
test_input = { "nums": [18,19,20,21,22,23,24,25,26,6,8,2,1] }
assert my_solution.missingInteger(**test_input) == 198
test_input = { "nums": [19,20,21,7,9] }
assert my_solution.missingInteger(**test_input) == 60
test_input = { "nums": [19,20,21,10,1,8,2,1] }
assert my_solution.missingInteger(**test_input) == 60
test_input = { "nums": [1,2,3,9,2,10,8,3,10,2] }
assert my_solution.missingInteger(**test_input) == 6
test_input = { "nums": [48,10] }
assert my_solution.missingInteger(**test_input) == 49
test_input = { "nums": [20,21,22,23,24,25,5] }
assert my_solution.missingInteger(**test_input) == 135
test_input = { "nums": [40,41,42,43,3,4,10,3,7,8,9,1,5] }
assert my_solution.missingInteger(**test_input) == 166
test_input = { "nums": [21,22,23,24,25,26,27,8] }
assert my_solution.missingInteger(**test_input) == 168
test_input = { "nums": [2,3,4,5,6,4] }
assert my_solution.missingInteger(**test_input) == 20
test_input = { "nums": [9,10,11,12,13,14,15,16,17,4] }
assert my_solution.missingInteger(**test_input) == 117
test_input = { "nums": [25,26,27,28,29,6,8] }
assert my_solution.missingInteger(**test_input) == 135
test_input = { "nums": [16,17,18,19,20,21,22,23,24,25,6] }
assert my_solution.missingInteger(**test_input) == 205
test_input = { "nums": [7,8,9,10,11,12] }
assert my_solution.missingInteger(**test_input) == 57
test_input = { "nums": [32,9,2,6,4,1,4,3,5] }
assert my_solution.missingInteger(**test_input) == 33
test_input = { "nums": [1,4,3] }
assert my_solution.missingInteger(**test_input) == 2
test_input = { "nums": [34,35,36,37,38,39,1,9,3,3,10,7,1] }
assert my_solution.missingInteger(**test_input) == 219
test_input = { "nums": [37,7,6,4,3,1,10,8,7,2,6] }
assert my_solution.missingInteger(**test_input) == 38
test_input = { "nums": [32] }
assert my_solution.missingInteger(**test_input) == 33
test_input = { "nums": [25,26,27,4] }
assert my_solution.missingInteger(**test_input) == 78
test_input = { "nums": [31,32,33,8,5,3,7,2] }
assert my_solution.missingInteger(**test_input) == 96
test_input = { "nums": [38,39,40,41,42,43,44,45,1] }
assert my_solution.missingInteger(**test_input) == 332
test_input = { "nums": [35,36,3,10] }
assert my_solution.missingInteger(**test_input) == 71
test_input = { "nums": [31,32,33,34,35,7,6,1,9] }
assert my_solution.missingInteger(**test_input) == 165
test_input = { "nums": [47,48,49,2,2] }
assert my_solution.missingInteger(**test_input) == 144
test_input = { "nums": [3,4,5,6,7,8,9,10,11,10,1] }
assert my_solution.missingInteger(**test_input) == 63
test_input = { "nums": [50] }
assert my_solution.missingInteger(**test_input) == 51
test_input = { "nums": [14,15,16,17,7,10,3,10] }
assert my_solution.missingInteger(**test_input) == 62 | 1,704,551,400 |
biweekly-contest-121-minimum-number-of-operations-to-make-array-xor-equal-to-k | https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k | minimum-number-of-operations-to-make-array-xor-equal-to-k | {
"questionId": "3249",
"questionFrontendId": "10032",
"title": "Minimum Number of Operations to Make Array XOR Equal to K",
"titleSlug": "minimum-number-of-operations-to-make-array-xor-equal-to-k",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 56,
"dislikes": 4,
"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 element of the array and flip a bit in its binary representation. Flipping a bit means changing a 0 to 1 or vice versa.
Return the minimum number of operations required to make the bitwise XOR of all elements of the final array equal to k.
Note that you can flip leading zero bits in the binary representation of elements. For example, for the number (101)2 you can flip the fourth bit and obtain (1101)2.
Example 1:
Input: nums = [2,1,3,4], k = 1
Output: 2
Explanation: We can do the following operations:
- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
It can be shown that we cannot make the XOR equal to k in less than 2 operations.
Example 2:
Input: nums = [2,0,2,0], k = 0
Output: 0
Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 106
0 <= k <= 106
"""
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
| You are given a 0-indexed integer array nums and a positive integer k.
You can apply the following operation on the array any number of times:
Choose any element of the array and flip a bit in its binary representation. Flipping a bit means changing a 0 to 1 or vice versa.
Return the minimum number of operations required to make the bitwise XOR of all elements of the final array equal to k.
Note that you can flip leading zero bits in the binary representation of elements. For example, for the number (101)2 you can flip the fourth bit and obtain (1101)2.
Example 1:
Input: nums = [2,1,3,4], k = 1
Output: 2
Explanation: We can do the following operations:
- Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4].
- Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4].
The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
It can be shown that we cannot make the XOR equal to k in less than 2 operations.
Example 2:
Input: nums = [2,0,2,0], k = 0
Output: 0
Explanation: The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 106
0 <= k <= 106
Please 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": [2,1,3,4], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,0,2,0], "k": 0 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [4], "k": 7 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [3,13,9,8,5,18,11,10], "k": 13 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [9,7,9,14,8,6], "k": 12 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [13,9,10,16,11,8,1], "k": 17 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [12,14], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [18,18], "k": 20 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [3,5,1,1], "k": 19 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [7,0,0,0], "k": 8 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [13,15,19,18,2,9,18,11,0,7], "k": 6 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [9,15,19,15,10,15,14,0,2,5], "k": 20 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [19,4,19,6,3,19,14,4,16,12], "k": 4 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [2,10,5,5,12,3,14,6,11,14], "k": 3 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [11,20], "k": 10 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [10,12,5,3,16,0], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [0,4,4,7,14,13], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [16,2,20,13,15,20,13], "k": 16 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [19,11,11,0,16,2,2,0,9], "k": 4 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [10,17,19,8,15], "k": 19 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [8,17,7,18], "k": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [10,20], "k": 7 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [11,14,5,9,19,3,1], "k": 10 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [19,13,16], "k": 4 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [12,18,13,2,1,5,8,5,8,6], "k": 7 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [15], "k": 9 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [8,5,4,5,13,18], "k": 0 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [9,18], "k": 3 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [6,9,15,17,16], "k": 19 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [14,0,17], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [12,1,14,13], "k": 4 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [4,10,6,10,10,16], "k": 18 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,11,6,12,2,15,4,8,11], "k": 3 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [7,3,12,5,1,12,8], "k": 11 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [11,14,18,14,6,18,4,16,20,5], "k": 16 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [20,2,6,0,7], "k": 20 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [9,18,19,16,8,11,15], "k": 14 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [0,3,20,0,15,7,17,4], "k": 3 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [15,6], "k": 8 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,7,13,16,2,2], "k": 15 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [2,12,11,11,2,12], "k": 17 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [10,8,10], "k": 11 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,10,2,13], "k": 0 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [1,20,4,19,12,18,5,3,11,8], "k": 14 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [16,12,12], "k": 20 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [2,1,7,3,4,9], "k": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [20,0,19,14,7,0], "k": 18 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [0,15,9,1,15], "k": 11 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [9,11,8,20,10], "k": 0 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,10,2,14,7,13,4,9,2], "k": 20 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [7,12,8], "k": 14 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [20,11,5,8,1,8,4,16], "k": 7 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [8,2,19,9,8], "k": 9 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [17], "k": 8 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [19,6], "k": 13 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [12,3,20,19], "k": 4 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [4,10,18,17,20,6,4], "k": 10 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [8,6,12,6,6], "k": 4 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [18,12,9,18,12,12,1], "k": 12 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [14,4,0,18,18,8,4,9], "k": 17 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [7,16,16,6], "k": 19 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [7,16,2,13,0,17,16], "k": 18 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [3,17,4,2,3,9], "k": 12 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [13,14,9,19,5,13], "k": 8 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [4,15,10,15,11,1,3,5,18,13], "k": 16 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [9,7,8], "k": 11 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [7,4,6,20,9,9,6,6], "k": 10 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [1,9,13,19,19,0,16,20,4], "k": 2 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [20,3,9,6,5,8], "k": 20 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [11,20,5,16,15,11,8], "k": 11 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [12,10,16,18,17,4,2,19,17,2], "k": 19 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [15,2], "k": 10 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [13,3,10,2,9,13,5,11,5], "k": 20 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [20,12,9,3,2,11], "k": 13 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [3,19,0,18,6], "k": 15 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [12,6], "k": 18 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [10,11,12,6,10,1,15], "k": 8 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [12,8,1,16,6,12], "k": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [11,5,9], "k": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [2,7], "k": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [20,1], "k": 8 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [9], "k": 16 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums": [9,5,7,11,8,18,5,1,4], "k": 8 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [1,8,7,19,3,20,13,9,10], "k": 1 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [19,18,6], "k": 8 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [19,12,3,18,12,19,5,20], "k": 0 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums": [6,18,12,9,20], "k": 13 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [19,5,5,7,4,7,15], "k": 7 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [17,7,19], "k": 18 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [14,13,3,15,18,20,2,9,3], "k": 14 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [16,10,3,2,3,19], "k": 4 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [2,0,8], "k": 18 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [19,5,5,12,20,2,10,17], "k": 12 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [6,0,0,1,15,9,19,12], "k": 6 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [8,16,13,8,18,9,16,16,19,11], "k": 12 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums": [3,7,6,7,4,3,2], "k": 2 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [0,2,9], "k": 10 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums": [14,10,16,9,6,13,11,13,11,16], "k": 20 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums": [13,19,0,12,11,10,11,2,6], "k": 7 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums": [6,1,4,9,1,7,11,15,15,0], "k": 8 }
assert my_solution.minOperations(**test_input) == 4 | 1,704,551,400 |
biweekly-contest-121-minimum-number-of-operations-to-make-x-and-y-equal | https://leetcode.com/problems/minimum-number-of-operations-to-make-x-and-y-equal | minimum-number-of-operations-to-make-x-and-y-equal | {
"questionId": "3239",
"questionFrontendId": "10033",
"title": "Minimum Number of Operations to Make X and Y Equal",
"titleSlug": "minimum-number-of-operations-to-make-x-and-y-equal",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 103,
"dislikes": 19,
"categoryTitle": "Algorithms"
} | """
You are given two positive integers x and y.
In one operation, you can do one of the four following operations:
Divide x by 11 if x is a multiple of 11.
Divide x by 5 if x is a multiple of 5.
Decrement x by 1.
Increment x by 1.
Return the minimum number of operations required to make x and y equal.
Example 1:
Input: x = 26, y = 1
Output: 3
Explanation: We can make 26 equal to 1 by applying the following operations:
1. Decrement x by 1
2. Divide x by 5
3. Divide x by 5
It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.
Example 2:
Input: x = 54, y = 2
Output: 4
Explanation: We can make 54 equal to 2 by applying the following operations:
1. Increment x by 1
2. Divide x by 11
3. Divide x by 5
4. Increment x by 1
It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.
Example 3:
Input: x = 25, y = 30
Output: 5
Explanation: We can make 25 equal to 30 by applying the following operations:
1. Increment x by 1
2. Increment x by 1
3. Increment x by 1
4. Increment x by 1
5. Increment x by 1
It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.
Constraints:
1 <= x, y <= 104
"""
class Solution:
def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
| You are given two positive integers x and y.
In one operation, you can do one of the four following operations:
Divide x by 11 if x is a multiple of 11.
Divide x by 5 if x is a multiple of 5.
Decrement x by 1.
Increment x by 1.
Return the minimum number of operations required to make x and y equal.
Example 1:
Input: x = 26, y = 1
Output: 3
Explanation: We can make 26 equal to 1 by applying the following operations:
1. Decrement x by 1
2. Divide x by 5
3. Divide x by 5
It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.
Example 2:
Input: x = 54, y = 2
Output: 4
Explanation: We can make 54 equal to 2 by applying the following operations:
1. Increment x by 1
2. Divide x by 11
3. Divide x by 5
4. Increment x by 1
It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.
Example 3:
Input: x = 25, y = 30
Output: 5
Explanation: We can make 25 equal to 30 by applying the following operations:
1. Increment x by 1
2. Increment x by 1
3. Increment x by 1
4. Increment x by 1
5. Increment x by 1
It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.
Constraints:
1 <= x, y <= 104
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
``` |
my_solution = Solution()
test_input = { "x": 26, "y": 1 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3
test_input = { "x": 54, "y": 2 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4
test_input = { "x": 25, "y": 30 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5
test_input = { "x": 1, "y": 1 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0
test_input = { "x": 1, "y": 2 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1
test_input = { "x": 1, "y": 3 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2
test_input = { "x": 1, "y": 4 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3
test_input = { "x": 1, "y": 5 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4
test_input = { "x": 1, "y": 6 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5
test_input = { "x": 1, "y": 7 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6
test_input = { "x": 1, "y": 8 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7
test_input = { "x": 1, "y": 9 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8
test_input = { "x": 1, "y": 10 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9
test_input = { "x": 1, "y": 11 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10
test_input = { "x": 1, "y": 12 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11
test_input = { "x": 1, "y": 13 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12
test_input = { "x": 1, "y": 14 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13
test_input = { "x": 1, "y": 15 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14
test_input = { "x": 1, "y": 16 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15
test_input = { "x": 1, "y": 17 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16
test_input = { "x": 1, "y": 18 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17
test_input = { "x": 1, "y": 19 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18
test_input = { "x": 1, "y": 20 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 19
test_input = { "x": 1, "y": 21 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 20
test_input = { "x": 1, "y": 22 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 21
test_input = { "x": 1, "y": 23 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 22
test_input = { "x": 1, "y": 24 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 23
test_input = { "x": 1, "y": 25 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 24
test_input = { "x": 2, "y": 1 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1
test_input = { "x": 2, "y": 2 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0
test_input = { "x": 2, "y": 3 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1
test_input = { "x": 2, "y": 4 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2
test_input = { "x": 2, "y": 5 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3
test_input = { "x": 2, "y": 6 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4
test_input = { "x": 2, "y": 7 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5
test_input = { "x": 2, "y": 8 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6
test_input = { "x": 2, "y": 9 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7
test_input = { "x": 2, "y": 10 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8
test_input = { "x": 2, "y": 11 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9
test_input = { "x": 2, "y": 12 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10
test_input = { "x": 2, "y": 13 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11
test_input = { "x": 2, "y": 14 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12
test_input = { "x": 2, "y": 15 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13
test_input = { "x": 2, "y": 16 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14
test_input = { "x": 2, "y": 17 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15
test_input = { "x": 2, "y": 18 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16
test_input = { "x": 2, "y": 19 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17
test_input = { "x": 2, "y": 20 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18
test_input = { "x": 2, "y": 21 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 19
test_input = { "x": 2, "y": 22 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 20
test_input = { "x": 2, "y": 23 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 21
test_input = { "x": 2, "y": 24 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 22
test_input = { "x": 2, "y": 25 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 23
test_input = { "x": 3, "y": 1 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2
test_input = { "x": 3, "y": 2 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1
test_input = { "x": 3, "y": 3 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0
test_input = { "x": 3, "y": 4 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1
test_input = { "x": 3, "y": 5 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2
test_input = { "x": 3, "y": 6 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3
test_input = { "x": 3, "y": 7 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4
test_input = { "x": 3, "y": 8 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5
test_input = { "x": 3, "y": 9 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6
test_input = { "x": 3, "y": 10 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7
test_input = { "x": 3, "y": 11 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8
test_input = { "x": 3, "y": 12 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9
test_input = { "x": 3, "y": 13 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10
test_input = { "x": 3, "y": 14 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11
test_input = { "x": 3, "y": 15 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12
test_input = { "x": 3, "y": 16 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13
test_input = { "x": 3, "y": 17 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14
test_input = { "x": 3, "y": 18 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15
test_input = { "x": 3, "y": 19 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16
test_input = { "x": 3, "y": 20 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17
test_input = { "x": 3, "y": 21 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18
test_input = { "x": 3, "y": 22 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 19
test_input = { "x": 3, "y": 23 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 20
test_input = { "x": 3, "y": 24 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 21
test_input = { "x": 3, "y": 25 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 22
test_input = { "x": 4, "y": 1 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2
test_input = { "x": 4, "y": 2 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2
test_input = { "x": 4, "y": 3 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1
test_input = { "x": 4, "y": 4 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0
test_input = { "x": 4, "y": 5 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1
test_input = { "x": 4, "y": 6 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2
test_input = { "x": 4, "y": 7 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3
test_input = { "x": 4, "y": 8 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4
test_input = { "x": 4, "y": 9 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5
test_input = { "x": 4, "y": 10 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6
test_input = { "x": 4, "y": 11 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7
test_input = { "x": 4, "y": 12 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8
test_input = { "x": 4, "y": 13 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9
test_input = { "x": 4, "y": 14 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10
test_input = { "x": 4, "y": 15 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11
test_input = { "x": 4, "y": 16 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12
test_input = { "x": 4, "y": 17 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13
test_input = { "x": 4, "y": 18 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14
test_input = { "x": 4, "y": 19 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15
test_input = { "x": 4, "y": 20 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16
test_input = { "x": 4, "y": 21 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17
test_input = { "x": 4, "y": 22 }
assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18 | 1,704,551,400 |
biweekly-contest-121-count-the-number-of-powerful-integers | https://leetcode.com/problems/count-the-number-of-powerful-integers | count-the-number-of-powerful-integers | {
"questionId": "3243",
"questionFrontendId": "10034",
"title": "Count the Number of Powerful Integers",
"titleSlug": "count-the-number-of-powerful-integers",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 53,
"dislikes": 1,
"categoryTitle": "Algorithms"
} | """
You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer.
A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at most limit.
Return the total number of powerful integers in the range [start..finish].
A string x is a suffix of a string y if and only if x is a substring of y that starts from some index (including 0) in y and extends to the index y.length - 1. For example, 25 is a suffix of 5125 whereas 512 is not.
Example 1:
Input: start = 1, finish = 6000, limit = 4, s = "124"
Output: 5
Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
It can be shown that there are only 5 powerful integers in this range.
Example 2:
Input: start = 15, finish = 215, limit = 6, s = "10"
Output: 2
Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
It can be shown that there are only 2 powerful integers in this range.
Example 3:
Input: start = 1000, finish = 2000, limit = 4, s = "3000"
Output: 0
Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
Constraints:
1 <= start <= finish <= 1015
1 <= limit <= 9
1 <= s.length <= floor(log10(finish)) + 1
s only consists of numeric digits which are at most limit.
s does not have leading zeros.
"""
class Solution:
def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
| You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer.
A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at most limit.
Return the total number of powerful integers in the range [start..finish].
A string x is a suffix of a string y if and only if x is a substring of y that starts from some index (including 0) in y and extends to the index y.length - 1. For example, 25 is a suffix of 5125 whereas 512 is not.
Example 1:
Input: start = 1, finish = 6000, limit = 4, s = "124"
Output: 5
Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
It can be shown that there are only 5 powerful integers in this range.
Example 2:
Input: start = 15, finish = 215, limit = 6, s = "10"
Output: 2
Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
It can be shown that there are only 2 powerful integers in this range.
Example 3:
Input: start = 1000, finish = 2000, limit = 4, s = "3000"
Output: 0
Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
Constraints:
1 <= start <= finish <= 1015
1 <= limit <= 9
1 <= s.length <= floor(log10(finish)) + 1
s only consists of numeric digits which are at most limit.
s does not have leading zeros.
Please complete the code below to solve above prblem:
```python
class Solution:
def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
``` |
my_solution = Solution()
test_input = { "start": 1, "finish": 6000, "limit": 4, "s": "124" }
assert my_solution.numberOfPowerfulInt(**test_input) == 5
test_input = { "start": 15, "finish": 215, "limit": 6, "s": "10" }
assert my_solution.numberOfPowerfulInt(**test_input) == 2
test_input = { "start": 1000, "finish": 2000, "limit": 4, "s": "3000" }
assert my_solution.numberOfPowerfulInt(**test_input) == 0
test_input = { "start": 141, "finish": 148, "limit": 9, "s": "9" }
assert my_solution.numberOfPowerfulInt(**test_input) == 0
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "17" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "27" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "41" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "47" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "61" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "66" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "71" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 1, "finish": 971, "limit": 9, "s": "72" }
assert my_solution.numberOfPowerfulInt(**test_input) == 9
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "20" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "24" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "32" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "33" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "40" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "41" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "42" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "43" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "44" }
assert my_solution.numberOfPowerfulInt(**test_input) == 8
test_input = { "start": 1300, "finish": 1400, "limit": 5, "s": "245" }
assert my_solution.numberOfPowerfulInt(**test_input) == 0
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "11" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "12" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "13" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "14" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "20" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "21" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "34" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "40" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "43" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "11" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "12" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "14" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "21" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "23" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "31" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "34" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "42" }
assert my_solution.numberOfPowerfulInt(**test_input) == 10
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "12" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "20" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "24" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "30" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "33" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "40" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "42" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "44" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "11" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "13" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "14" }
assert my_solution.numberOfPowerfulInt(**test_input) == 11
test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "22" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "30" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "33" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "43" }
assert my_solution.numberOfPowerfulInt(**test_input) == 12
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "1" }
assert my_solution.numberOfPowerfulInt(**test_input) == 162
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "10" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "100" }
assert my_solution.numberOfPowerfulInt(**test_input) == 2
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "11" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "12" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "13" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "14" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "15" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "16" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "17" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "18" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "2" }
assert my_solution.numberOfPowerfulInt(**test_input) == 162
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "20" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "21" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "22" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "23" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "24" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "25" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "26" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "27" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "28" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "3" }
assert my_solution.numberOfPowerfulInt(**test_input) == 162
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "30" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "31" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "32" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "33" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "34" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "35" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "36" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "37" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "38" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "4" }
assert my_solution.numberOfPowerfulInt(**test_input) == 162
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "40" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "41" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "42" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "43" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "44" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "45" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "46" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "47" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "48" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "5" }
assert my_solution.numberOfPowerfulInt(**test_input) == 162
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "50" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "51" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "52" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18
test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "53" }
assert my_solution.numberOfPowerfulInt(**test_input) == 18 | 1,704,551,400 |
weekly-contest-378-check-if-bitwise-or-has-trailing-zeros | https://leetcode.com/problems/check-if-bitwise-or-has-trailing-zeros | check-if-bitwise-or-has-trailing-zeros | {
"questionId": "3246",
"questionFrontendId": "2980",
"title": "Check if Bitwise OR Has Trailing Zeros",
"titleSlug": "check-if-bitwise-or-has-trailing-zeros",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 68,
"dislikes": 5,
"categoryTitle": "Algorithms"
} | """
You are given an array of positive integers nums.
You have to check if it is possible to select two or more elements in the array such that the bitwise OR of the selected elements has at least one trailing zero in its binary representation.
For example, the binary representation of 5, which is "101", does not have any trailing zeros, whereas the binary representation of 4, which is "100", has two trailing zeros.
Return true if it is possible to select two or more elements whose bitwise OR has trailing zeros, return false otherwise.
Example 1:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
Example 2:
Input: nums = [2,4,8,16]
Output: true
Explanation: If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).
Example 3:
Input: nums = [1,3,5,7,9]
Output: false
Explanation: There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
Constraints:
2 <= nums.length <= 100
1 <= nums[i] <= 100
"""
class Solution:
def hasTrailingZeros(self, nums: List[int]) -> bool:
| You are given an array of positive integers nums.
You have to check if it is possible to select two or more elements in the array such that the bitwise OR of the selected elements has at least one trailing zero in its binary representation.
For example, the binary representation of 5, which is "101", does not have any trailing zeros, whereas the binary representation of 4, which is "100", has two trailing zeros.
Return true if it is possible to select two or more elements whose bitwise OR has trailing zeros, return false otherwise.
Example 1:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
Example 2:
Input: nums = [2,4,8,16]
Output: true
Explanation: If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation "110" with one trailing zero.
Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).
Example 3:
Input: nums = [1,3,5,7,9]
Output: false
Explanation: There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
Constraints:
2 <= nums.length <= 100
1 <= nums[i] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def hasTrailingZeros(self, nums: List[int]) -> bool:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4,5] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [1,3,5,7,9] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [1,2] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [2,4,8,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [1,3] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [1,7] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [3,3] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [3,4] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [2,2] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [4,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [4,32] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [6,2] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [6,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [3,9] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [4,3] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [5,6] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [7,9] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [8,2] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [8,4] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [7,10] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [9,73] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [8,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [10,5] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [11,17] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [19,11] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [19,35] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [19,51] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [21,61] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [23,21] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [25,25] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [10,2] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [12,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [27,77] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [16,4] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [16,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [16,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [16,32] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [29,13] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [37,69] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [39,53] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [24,32] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [32,32] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [42,9] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [45,24] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [64,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [49,23] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [4,6,4] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [8,16,4] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [57,27] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [8,16,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [10,4,6] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [12,8,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [63,47] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [67,69] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [69,87] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [16,8,4] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [77,49] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [89,31] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [1,3,4] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [1,5,3] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [1,7,9] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [3,69,59] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [16,16,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [6,5,5] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [7,77,9] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [9,77,51] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [16,32,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [16,32,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [10,1,9] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [10,7,5] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [11,23,27] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [15,13,63] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [21,27,79] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [23,23,47] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [35,91,15] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [32,4,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [32,8,48] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [33,40,84] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [41,83,53] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [64,48,6] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [75,34,58] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [3,8,2,3] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [43,15,41] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [43,65,79] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [47,7,19] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [4,6,1,1] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [4,10,1,7] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [49,73,81] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [6,3,10,6] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [6,4,2,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [6,12,12,24] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [7,2,4,4] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [8,6,4,32] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [8,12,16,2] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [12,64,16,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [16,4,8,16] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [51,33,25] }
assert my_solution.hasTrailingZeros(**test_input) == False
test_input = { "nums": [16,8,64,4] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [16,16,12,8] }
assert my_solution.hasTrailingZeros(**test_input) == True
test_input = { "nums": [51,43,9] }
assert my_solution.hasTrailingZeros(**test_input) == False | 1,703,989,800 |
weekly-contest-378-find-longest-special-substring-that-occurs-thrice-i | https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i | find-longest-special-substring-that-occurs-thrice-i | {
"questionId": "3267",
"questionFrontendId": "2981",
"title": "Find Longest Special Substring That Occurs Thrice I",
"titleSlug": "find-longest-special-substring-that-occurs-thrice-i",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 112,
"dislikes": 6,
"categoryTitle": "Algorithms"
} | """
You are given a string s that consists of lowercase English letters.
A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.
Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "aaaa"
Output: 2
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
It can be shown that the maximum length achievable is 2.
Example 2:
Input: s = "abcdef"
Output: -1
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
Example 3:
Input: s = "abcaba"
Output: 1
Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
It can be shown that the maximum length achievable is 1.
Constraints:
3 <= s.length <= 50
s consists of only lowercase English letters.
"""
class Solution:
def maximumLength(self, s: str) -> int:
| You are given a string s that consists of lowercase English letters.
A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.
Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "aaaa"
Output: 2
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
It can be shown that the maximum length achievable is 2.
Example 2:
Input: s = "abcdef"
Output: -1
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
Example 3:
Input: s = "abcaba"
Output: 1
Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
It can be shown that the maximum length achievable is 1.
Constraints:
3 <= s.length <= 50
s consists of only lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumLength(self, s: str) -> int:
``` |
my_solution = Solution()
test_input = { "s": "aaaa" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "abcdef" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "abcaba" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "abcccccdddd" }
assert my_solution.maximumLength(**test_input) == 3
test_input = { "s": "aaa" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "acc" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "cab" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "cad" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "cbc" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "ccc" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "dca" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "ddd" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "fff" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ggg" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "hhh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "kkk" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "lll" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ooo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ppp" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "qqq" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "rrr" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "xxx" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "yyy" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "aaau" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "affe" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "agae" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "aiii" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bbbb" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "bbbz" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bddd" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "beee" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bnnn" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bsss" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bxxx" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "cafc" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "ccag" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "cfff" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "dddd" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "dsss" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "eccc" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "eeew" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "gfdc" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "giii" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "hhhn" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "hyyy" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "iiii" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "jiii" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "jjjj" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "kbbb" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "kddd" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "looo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "nnnn" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "oaaa" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "osss" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "pppp" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "pppw" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "qqqc" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "qqqo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "qqqq" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "reee" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "rzzz" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "thhh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "tttt" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "unnn" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "uuuu" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "uyyy" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "vddd" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "vfff" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "vvvv" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "wbbb" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "wqqq" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "wwwg" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "xxxk" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "xxxx" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "zfff" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ammmm" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "aqqqu" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "axxxx" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "bahhh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bbbbz" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "bbccc" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "biaei" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "bjjjj" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "cccll" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ceaaa" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "cjlll" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "crqqq" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "cyyyy" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "ddddj" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "ddddt" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "dkkkk" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "eefff" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "efage" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "epppp" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "fafff" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ffbbb" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ffffr" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "gcgbf" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "gcooo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "gfgec" }
assert my_solution.maximumLength(**test_input) == -1 | 1,703,989,800 |
weekly-contest-378-find-longest-special-substring-that-occurs-thrice-ii | https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-ii | find-longest-special-substring-that-occurs-thrice-ii | {
"questionId": "3266",
"questionFrontendId": "2982",
"title": "Find Longest Special Substring That Occurs Thrice II",
"titleSlug": "find-longest-special-substring-that-occurs-thrice-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 172,
"dislikes": 17,
"categoryTitle": "Algorithms"
} | """
You are given a string s that consists of lowercase English letters.
A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.
Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "aaaa"
Output: 2
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
It can be shown that the maximum length achievable is 2.
Example 2:
Input: s = "abcdef"
Output: -1
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
Example 3:
Input: s = "abcaba"
Output: 1
Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
It can be shown that the maximum length achievable is 1.
Constraints:
3 <= s.length <= 5 * 105
s consists of only lowercase English letters.
"""
class Solution:
def maximumLength(self, s: str) -> int:
| You are given a string s that consists of lowercase English letters.
A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.
Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "aaaa"
Output: 2
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
It can be shown that the maximum length achievable is 2.
Example 2:
Input: s = "abcdef"
Output: -1
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
Example 3:
Input: s = "abcaba"
Output: 1
Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
It can be shown that the maximum length achievable is 1.
Constraints:
3 <= s.length <= 5 * 105
s consists of only lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumLength(self, s: str) -> int:
``` |
my_solution = Solution()
test_input = { "s": "aaaa" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "abcdef" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "abcaba" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "abcccccdddd" }
assert my_solution.maximumLength(**test_input) == 3
test_input = { "s": "acd" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "bad" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "bbc" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "ccc" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "cda" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "dab" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "ddd" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "eee" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "fff" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "hhh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "jjj" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "kkk" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "lll" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "mmm" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "nnn" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ooo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ppp" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "qqq" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "rrr" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "sss" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "vvv" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "yyy" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "aaaj" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "aada" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "accc" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "auuu" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "azzz" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bbbb" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "bbbr" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bccc" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "brrr" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "cccc" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "clll" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "dbac" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "dddd" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "dddr" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "dddu" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "dzzz" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "eeee" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "efda" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "efdc" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "ekkk" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ggge" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "gggp" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "gggr" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "gggy" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "guuu" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "hwww" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "hxxx" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "jjjj" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "jjjk" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "kkkk" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "lllm" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "lttt" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "mwww" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "nnnd" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "nnni" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "oooe" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "oooh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "oooo" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "oooq" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "paaa" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "pggg" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "qooo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "rrrr" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "smmm" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "thhh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "tttt" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "uuuu" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "vppp" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "xqqq" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "xxxj" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ysss" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "zzzz" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "aaaak" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "bbbjj" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bbvvv" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "bfooo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "buooo" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "cffff" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "cirrr" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "cwwwv" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "ddhhh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "deeee" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "deiii" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "dtttk" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "eeeeq" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "eeekk" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "eesss" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "euuuu" }
assert my_solution.maximumLength(**test_input) == 2
test_input = { "s": "euylz" }
assert my_solution.maximumLength(**test_input) == -1
test_input = { "s": "fffhh" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "fpddd" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "gcccn" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "gtwww" }
assert my_solution.maximumLength(**test_input) == 1
test_input = { "s": "hcfff" }
assert my_solution.maximumLength(**test_input) == 1 | 1,703,989,800 |
weekly-contest-378-palindrome-rearrangement-queries | https://leetcode.com/problems/palindrome-rearrangement-queries | palindrome-rearrangement-queries | {
"questionId": "3203",
"questionFrontendId": "2983",
"title": "Palindrome Rearrangement Queries",
"titleSlug": "palindrome-rearrangement-queries",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 73,
"dislikes": 23,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed string s having an even length n.
You are also given a 0-indexed 2D integer array, queries, where queries[i] = [ai, bi, ci, di].
For each query i, you are allowed to perform the following operations:
Rearrange the characters within the substring s[ai:bi], where 0 <= ai <= bi < n / 2.
Rearrange the characters within the substring s[ci:di], where n / 2 <= ci <= di < n.
For each query, your task is to determine whether it is possible to make s a palindrome by performing the operations.
Each query is answered independently of the others.
Return a 0-indexed array answer, where answer[i] == true if it is possible to make s a palindrome by performing operations specified by the ith query, and false otherwise.
A substring is a contiguous sequence of characters within a string.
s[x:y] represents the substring consisting of characters from the index x to index y in s, both inclusive.
Example 1:
Input: s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]]
Output: [true,true]
Explanation: In this example, there are two queries:
In the first query:
- a0 = 1, b0 = 1, c0 = 3, d0 = 5.
- So, you are allowed to rearrange s[1:1] => abcabc and s[3:5] => abcabc.
- To make s a palindrome, s[3:5] can be rearranged to become => abccba.
- Now, s is a palindrome. So, answer[0] = true.
In the second query:
- a1 = 0, b1 = 2, c1 = 5, d1 = 5.
- So, you are allowed to rearrange s[0:2] => abcabc and s[5:5] => abcabc.
- To make s a palindrome, s[0:2] can be rearranged to become => cbaabc.
- Now, s is a palindrome. So, answer[1] = true.
Example 2:
Input: s = "abbcdecbba", queries = [[0,2,7,9]]
Output: [false]
Explanation: In this example, there is only one query.
a0 = 0, b0 = 2, c0 = 7, d0 = 9.
So, you are allowed to rearrange s[0:2] => abbcdecbba and s[7:9] => abbcdecbba.
It is not possible to make s a palindrome by rearranging these substrings because s[3:6] is not a palindrome.
So, answer[0] = false.
Example 3:
Input: s = "acbcab", queries = [[1,2,4,5]]
Output: [true]
Explanation: In this example, there is only one query.
a0 = 1, b0 = 2, c0 = 4, d0 = 5.
So, you are allowed to rearrange s[1:2] => acbcab and s[4:5] => acbcab.
To make s a palindrome s[1:2] can be rearranged to become abccab.
Then, s[4:5] can be rearranged to become abccba.
Now, s is a palindrome. So, answer[0] = true.
Constraints:
2 <= n == s.length <= 105
1 <= queries.length <= 105
queries[i].length == 4
ai == queries[i][0], bi == queries[i][1]
ci == queries[i][2], di == queries[i][3]
0 <= ai <= bi < n / 2
n / 2 <= ci <= di < n
n is even.
s consists of only lowercase English letters.
"""
class Solution:
def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
| You are given a 0-indexed string s having an even length n.
You are also given a 0-indexed 2D integer array, queries, where queries[i] = [ai, bi, ci, di].
For each query i, you are allowed to perform the following operations:
Rearrange the characters within the substring s[ai:bi], where 0 <= ai <= bi < n / 2.
Rearrange the characters within the substring s[ci:di], where n / 2 <= ci <= di < n.
For each query, your task is to determine whether it is possible to make s a palindrome by performing the operations.
Each query is answered independently of the others.
Return a 0-indexed array answer, where answer[i] == true if it is possible to make s a palindrome by performing operations specified by the ith query, and false otherwise.
A substring is a contiguous sequence of characters within a string.
s[x:y] represents the substring consisting of characters from the index x to index y in s, both inclusive.
Example 1:
Input: s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]]
Output: [true,true]
Explanation: In this example, there are two queries:
In the first query:
- a0 = 1, b0 = 1, c0 = 3, d0 = 5.
- So, you are allowed to rearrange s[1:1] => abcabc and s[3:5] => abcabc.
- To make s a palindrome, s[3:5] can be rearranged to become => abccba.
- Now, s is a palindrome. So, answer[0] = true.
In the second query:
- a1 = 0, b1 = 2, c1 = 5, d1 = 5.
- So, you are allowed to rearrange s[0:2] => abcabc and s[5:5] => abcabc.
- To make s a palindrome, s[0:2] can be rearranged to become => cbaabc.
- Now, s is a palindrome. So, answer[1] = true.
Example 2:
Input: s = "abbcdecbba", queries = [[0,2,7,9]]
Output: [false]
Explanation: In this example, there is only one query.
a0 = 0, b0 = 2, c0 = 7, d0 = 9.
So, you are allowed to rearrange s[0:2] => abbcdecbba and s[7:9] => abbcdecbba.
It is not possible to make s a palindrome by rearranging these substrings because s[3:6] is not a palindrome.
So, answer[0] = false.
Example 3:
Input: s = "acbcab", queries = [[1,2,4,5]]
Output: [true]
Explanation: In this example, there is only one query.
a0 = 1, b0 = 2, c0 = 4, d0 = 5.
So, you are allowed to rearrange s[1:2] => acbcab and s[4:5] => acbcab.
To make s a palindrome s[1:2] can be rearranged to become abccab.
Then, s[4:5] can be rearranged to become abccba.
Now, s is a palindrome. So, answer[0] = true.
Constraints:
2 <= n == s.length <= 105
1 <= queries.length <= 105
queries[i].length == 4
ai == queries[i][0], bi == queries[i][1]
ci == queries[i][2], di == queries[i][3]
0 <= ai <= bi < n / 2
n / 2 <= ci <= di < n
n is even.
s consists of only lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
``` |
my_solution = Solution()
test_input = { "s": "abcabc", "queries": [[1,1,3,5],[0,2,5,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "abbcdecbba", "queries": [[0,2,7,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "acbcab", "queries": [[1,2,4,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "bcdbdc", "queries": [[1,2,3,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "bb", "queries": [[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "eoquueqo", "queries": [[3,3,6,6]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "dd", "queries": [[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "ceddceddcc", "queries": [[0,1,6,8]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "bdbd", "queries": [[0,0,2,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "eeee", "queries": [[0,1,2,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "deebdeeddb", "queries": [[1,2,5,7]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "xwaswsxwaw", "queries": [[1,3,5,6]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "askloakosala", "queries": [[2,4,7,10]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "bbccbb", "queries": [[0,1,4,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "djaypzjpyzad", "queries": [[0,3,7,8]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "ajvnbnznjnzbva", "queries": [[5,6,10,11]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "vbeptwzvtwpzbe", "queries": [[3,6,10,11]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "cababc", "queries": [[1,2,3,4]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "cbbbbc", "queries": [[1,1,5,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "qupzexxhqxpzhxeu", "queries": [[2,4,8,12]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "cdbdbc", "queries": [[1,2,3,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "odaxusaweuasuoeudxwa", "queries": [[0,5,10,14]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "ujfscqolkwjucoswlkfq", "queries": [[1,9,17,18]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "ceacea", "queries": [[0,2,3,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "bnjzcgmnecxxbmnjngxzecxc", "queries": [[8,9,19,22]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "daeaed", "queries": [[0,2,3,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "ddaadd", "queries": [[0,2,3,4]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "ddedde", "queries": [[0,2,4,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "pmzwetzhzursuhmeswpzrztz", "queries": [[4,6,16,17]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "qcryjkdzmqyoojzrckymdqyq", "queries": [[2,8,21,22]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "qdltkndnclarncadtqnlldkr", "queries": [[3,4,15,16]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "ecbbce", "queries": [[0,1,3,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "eczecz", "queries": [[0,0,3,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "etuouqokbalafokfbuqaaoetlu", "queries": [[3,11,21,23]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "mpepem", "queries": [[0,2,3,4]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "nbpechkpmudbsenphdmsbbupck", "queries": [[6,7,18,19]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "bccacacb", "queries": [[3,3,4,7]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "stgjtzqwgkuadjgqugkwdtzast", "queries": [[5,10,13,23]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "qiyikbayvhkcgxyaqckgxkhivbyi", "queries": [[5,12,17,24]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "ceedceed", "queries": [[0,1,4,7]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "rcguwczbjhjhgqrggqcbwjzhjuch", "queries": [[5,7,16,20]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "ckwbnmqmtzbixrrkixbtbqzmnwmc", "queries": [[1,9,15,24]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True]
test_input = { "s": "riirxzxuqpspoiixpirsoxrzpiuq", "queries": [[1,6,14,21]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "geettndnusqufidtqdfgtsieenundu", "queries": [[6,8,19,23]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False]
test_input = { "s": "bb", "queries": [[0,0,1,1],[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "cc", "queries": [[0,0,1,1],[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "dd", "queries": [[0,0,1,1],[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "ee", "queries": [[0,0,1,1],[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "aeae", "queries": [[1,1,2,3],[1,1,3,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,False]
test_input = { "s": "eaae", "queries": [[0,1,3,3],[0,0,2,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "eded", "queries": [[0,1,2,3],[1,1,2,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "lrlr", "queries": [[0,1,2,3],[0,0,2,2]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,False]
test_input = { "s": "dbaabd", "queries": [[0,1,5,5],[1,2,4,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "dcbcbd", "queries": [[0,1,4,4],[0,2,3,4]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,True]
test_input = { "s": "hykkyh", "queries": [[0,0,3,4],[1,2,3,4]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "lvrvrl", "queries": [[2,2,4,5],[0,2,3,4]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,True]
test_input = { "s": "adceaecd", "queries": [[3,3,5,5],[0,1,4,6]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,True]
test_input = { "s": "baadadba", "queries": [[1,2,4,5],[0,2,4,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,True]
test_input = { "s": "bceaabec", "queries": [[1,3,4,7],[0,2,6,6]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "ifchcifh", "queries": [[1,2,5,6],[1,1,4,6]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "adeeeeeaed", "queries": [[2,4,7,9],[3,4,8,8]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,False]
test_input = { "s": "aedbdbddea", "queries": [[4,4,7,8],[2,2,8,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "caeaaaaaec", "queries": [[0,2,5,8],[0,0,5,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True]
test_input = { "s": "dbaccccdba", "queries": [[4,4,6,7],[2,3,8,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "deabadabea", "queries": [[0,3,7,9],[0,2,5,7]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,True]
test_input = { "s": "eddeededee", "queries": [[0,3,6,9],[0,0,6,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "eedbbedebb", "queries": [[2,2,6,7],[2,2,5,6]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "gvtkakgvat", "queries": [[1,2,7,7],[2,3,7,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "bzvvicviczbv", "queries": [[1,2,7,8],[1,4,7,8]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "ljccjajcljac", "queries": [[2,4,6,10],[3,5,7,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "rxvzvezvrvxe", "queries": [[1,4,8,10],[3,3,10,11]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "amgpelwpmlaewg", "queries": [[3,4,7,9],[0,6,7,10]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,True]
test_input = { "s": "leubdglmbglleudm", "queries": [[1,3,9,14],[2,6,13,14]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "ooxuznriuzrooixn", "queries": [[1,3,10,12],[1,4,9,13]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "nlaonaphinpnalohai", "queries": [[2,5,13,13],[2,7,9,14]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "rujokutobuttlysjusrtltuobkoytu", "queries": [[5,6,18,23],[10,13,15,26]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False]
test_input = { "s": "bb", "queries": [[0,0,1,1],[0,0,1,1],[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "cc", "queries": [[0,0,1,1],[0,0,1,1],[0,0,1,1]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "dbdb", "queries": [[0,0,2,2],[1,1,3,3],[0,1,2,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,True]
test_input = { "s": "ebbe", "queries": [[0,1,3,3],[1,1,2,2],[0,0,2,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "acacaa", "queries": [[0,1,5,5],[1,1,4,4],[1,2,3,4]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,True]
test_input = { "s": "bbaabb", "queries": [[0,1,4,5],[0,2,3,5],[2,2,5,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "bbebbe", "queries": [[0,1,3,5],[2,2,4,5],[0,1,5,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,False,False]
test_input = { "s": "ddaadd", "queries": [[1,1,4,4],[0,0,4,4],[0,2,3,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "nlhhln", "queries": [[2,2,4,5],[1,2,5,5],[2,2,3,3]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "zbebez", "queries": [[0,2,5,5],[1,1,3,5],[0,2,4,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "cbcbbcbc", "queries": [[0,2,7,7],[1,2,4,7],[0,2,4,5]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True]
test_input = { "s": "deceecde", "queries": [[3,3,6,7],[1,2,4,5],[2,3,7,7]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,False,False]
test_input = { "s": "fydyfyyd", "queries": [[0,2,4,6],[1,3,4,7],[2,3,6,7]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,False]
test_input = { "s": "dccabcdbca", "queries": [[1,3,5,8],[2,4,7,7],[0,2,6,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "eacbdeacbd", "queries": [[4,4,8,9],[3,4,7,9],[0,0,6,8]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "eddaaedada", "queries": [[0,1,7,8],[0,1,7,8],[0,3,7,9]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "vvsbgsvgbv", "queries": [[0,1,6,9],[2,3,8,9],[0,0,6,7]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "sukesivksseuiv", "queries": [[2,3,11,13],[5,5,7,13],[2,5,8,13]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,True,False]
test_input = { "s": "pbcjpsfxwtbcfjwpsptx", "queries": [[0,4,13,13],[5,5,15,18],[2,3,13,18]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "natvhtruvwyutyvvnarhwt", "queries": [[5,7,14,21],[0,8,11,19],[2,8,11,14]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "yjsjywxbriejyxieysrwbj", "queries": [[4,5,15,19],[1,9,12,17],[3,6,12,18]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "ellaghdbmazdallhmegabddz", "queries": [[4,6,13,19],[4,11,17,17],[7,10,13,16]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "wcfnhuaulqxbuuxafcwhnbql", "queries": [[5,11,13,18],[7,8,18,21],[3,6,21,23]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
test_input = { "s": "qyioinjmvpgpropimvqiygrnjp", "queries": [[1,4,15,25],[1,8,19,22],[4,9,18,23]] }
assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] | 1,703,989,800 |
weekly-contest-377-minimum-number-game | https://leetcode.com/problems/minimum-number-game | minimum-number-game | {
"questionId": "3226",
"questionFrontendId": "2974",
"title": "Minimum Number Game",
"titleSlug": "minimum-number-game",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 38,
"dislikes": 2,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:
Every round, first Alice will remove the minimum element from nums, and then Bob does the same.
Now, first Bob will append the removed element in the array arr, and then Alice does the same.
The game continues until nums becomes empty.
Return the resulting array arr.
Example 1:
Input: nums = [5,4,2,3]
Output: [3,2,5,4]
Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].
Example 2:
Input: nums = [2,5]
Output: [5,2]
Explanation: In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
nums.length % 2 == 0
"""
class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
| You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:
Every round, first Alice will remove the minimum element from nums, and then Bob does the same.
Now, first Bob will append the removed element in the array arr, and then Alice does the same.
The game continues until nums becomes empty.
Return the resulting array arr.
Example 1:
Input: nums = [5,4,2,3]
Output: [3,2,5,4]
Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].
Example 2:
Input: nums = [2,5]
Output: [5,2]
Explanation: In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
nums.length % 2 == 0
Please complete the code below to solve above prblem:
```python
class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "nums": [5,4,2,3] }
assert my_solution.numberGame(**test_input) == [3,2,5,4]
test_input = { "nums": [2,5] }
assert my_solution.numberGame(**test_input) == [5,2]
test_input = { "nums": [4,4,3,8] }
assert my_solution.numberGame(**test_input) == [4,3,8,4]
test_input = { "nums": [2,5,3,8] }
assert my_solution.numberGame(**test_input) == [3,2,8,5]
test_input = { "nums": [2,7,9,6,4,6] }
assert my_solution.numberGame(**test_input) == [4,2,6,6,9,7]
test_input = { "nums": [18,26,37,46,13,33,39,1,37,16] }
assert my_solution.numberGame(**test_input) == [13,1,18,16,33,26,37,37,46,39]
test_input = { "nums": [17,2,4,11,14,18] }
assert my_solution.numberGame(**test_input) == [4,2,14,11,18,17]
test_input = { "nums": [20,30,12,3,11,17,32,12] }
assert my_solution.numberGame(**test_input) == [11,3,12,12,20,17,32,30]
test_input = { "nums": [9,32,6,11,11,39,18,29,44,29] }
assert my_solution.numberGame(**test_input) == [9,6,11,11,29,18,32,29,44,39]
test_input = { "nums": [7,2,3,4] }
assert my_solution.numberGame(**test_input) == [3,2,7,4]
test_input = { "nums": [8,7,1,3] }
assert my_solution.numberGame(**test_input) == [3,1,8,7]
test_input = { "nums": [2,6,6,6] }
assert my_solution.numberGame(**test_input) == [6,2,6,6]
test_input = { "nums": [1,2] }
assert my_solution.numberGame(**test_input) == [2,1]
test_input = { "nums": [4,1,1,3] }
assert my_solution.numberGame(**test_input) == [1,1,4,3]
test_input = { "nums": [13,12,18,11,15,28,26,2] }
assert my_solution.numberGame(**test_input) == [11,2,13,12,18,15,28,26]
test_input = { "nums": [14,30,29,3,23,21,26,23] }
assert my_solution.numberGame(**test_input) == [14,3,23,21,26,23,30,29]
test_input = { "nums": [1,1] }
assert my_solution.numberGame(**test_input) == [1,1]
test_input = { "nums": [2,1] }
assert my_solution.numberGame(**test_input) == [2,1]
test_input = { "nums": [12,1,28,23,2,31,11,26] }
assert my_solution.numberGame(**test_input) == [2,1,12,11,26,23,31,28]
test_input = { "nums": [21,11,37,1,40,50,49,45,28,47] }
assert my_solution.numberGame(**test_input) == [11,1,28,21,40,37,47,45,50,49]
test_input = { "nums": [25,22,31,7,30,9,9,18] }
assert my_solution.numberGame(**test_input) == [9,7,18,9,25,22,31,30]
test_input = { "nums": [2,4,10,9,16,9] }
assert my_solution.numberGame(**test_input) == [4,2,9,9,16,10]
test_input = { "nums": [5,2,3,5] }
assert my_solution.numberGame(**test_input) == [3,2,5,5]
test_input = { "nums": [6,44,37,6,28,44,30,36,25,24] }
assert my_solution.numberGame(**test_input) == [6,6,25,24,30,28,37,36,44,44]
test_input = { "nums": [17,10,6,14,10,18] }
assert my_solution.numberGame(**test_input) == [10,6,14,10,18,17]
test_input = { "nums": [40,24,23,29,37,26,39,34,39,23] }
assert my_solution.numberGame(**test_input) == [23,23,26,24,34,29,39,37,40,39]
test_input = { "nums": [2,2] }
assert my_solution.numberGame(**test_input) == [2,2]
test_input = { "nums": [33,5,31,43,48,18,31,11,19,8] }
assert my_solution.numberGame(**test_input) == [8,5,18,11,31,19,33,31,48,43]
test_input = { "nums": [37,46,42,19,10,8,43,10,40,13] }
assert my_solution.numberGame(**test_input) == [10,8,13,10,37,19,42,40,46,43]
test_input = { "nums": [2,19,8,22,1,27,29,7] }
assert my_solution.numberGame(**test_input) == [2,1,8,7,22,19,29,27]
test_input = { "nums": [2,3,2,3] }
assert my_solution.numberGame(**test_input) == [2,2,3,3]
test_input = { "nums": [1,4,7,14,8,14] }
assert my_solution.numberGame(**test_input) == [4,1,8,7,14,14]
test_input = { "nums": [28,47,36,34,19,7,40,46,33,43] }
assert my_solution.numberGame(**test_input) == [19,7,33,28,36,34,43,40,47,46]
test_input = { "nums": [29,41,20,22,16,27,22,44,10,47] }
assert my_solution.numberGame(**test_input) == [16,10,22,20,27,22,41,29,47,44]
test_input = { "nums": [14,6,40,19,47,46,34,27,28,10] }
assert my_solution.numberGame(**test_input) == [10,6,19,14,28,27,40,34,47,46]
test_input = { "nums": [42,43,50,43,36,26,16,12,3,2] }
assert my_solution.numberGame(**test_input) == [3,2,16,12,36,26,43,42,50,43]
test_input = { "nums": [1,7,24,24,23,32,28,2] }
assert my_solution.numberGame(**test_input) == [2,1,23,7,24,24,32,28]
test_input = { "nums": [20,19,16,16,19,29,21,5] }
assert my_solution.numberGame(**test_input) == [16,5,19,16,20,19,29,21]
test_input = { "nums": [20,9,29,29,17,39,27,44,1,8] }
assert my_solution.numberGame(**test_input) == [8,1,17,9,27,20,29,29,44,39]
test_input = { "nums": [14,11,12,18,9,15] }
assert my_solution.numberGame(**test_input) == [11,9,14,12,18,15]
test_input = { "nums": [17,22,2,35,15,19,25,5,33,44] }
assert my_solution.numberGame(**test_input) == [5,2,17,15,22,19,33,25,44,35]
test_input = { "nums": [22,3,26,15,1,5,14,28] }
assert my_solution.numberGame(**test_input) == [3,1,14,5,22,15,28,26]
test_input = { "nums": [5,24,3,2,17,9,2,4] }
assert my_solution.numberGame(**test_input) == [2,2,4,3,9,5,24,17]
test_input = { "nums": [2,6,4,7] }
assert my_solution.numberGame(**test_input) == [4,2,7,6]
test_input = { "nums": [1,33,29,21,25,14,26,35,34,30] }
assert my_solution.numberGame(**test_input) == [14,1,25,21,29,26,33,30,35,34]
test_input = { "nums": [50,25,42,41,16,23,47,31,23,16] }
assert my_solution.numberGame(**test_input) == [16,16,23,23,31,25,42,41,50,47]
test_input = { "nums": [31,31,31,12,24,17,11,3,33,13] }
assert my_solution.numberGame(**test_input) == [11,3,13,12,24,17,31,31,33,31]
test_input = { "nums": [8,3,2,7] }
assert my_solution.numberGame(**test_input) == [3,2,8,7]
test_input = { "nums": [8,2,8,6] }
assert my_solution.numberGame(**test_input) == [6,2,8,8]
test_input = { "nums": [4,15,16,2,12,7] }
assert my_solution.numberGame(**test_input) == [4,2,12,7,16,15]
test_input = { "nums": [5,4,2,4] }
assert my_solution.numberGame(**test_input) == [4,2,5,4]
test_input = { "nums": [17,13,7,12,19,15,6,22] }
assert my_solution.numberGame(**test_input) == [7,6,13,12,17,15,22,19]
test_input = { "nums": [2,15,12,16,12,13] }
assert my_solution.numberGame(**test_input) == [12,2,13,12,16,15]
test_input = { "nums": [3,15,18,16,6,7] }
assert my_solution.numberGame(**test_input) == [6,3,15,7,18,16]
test_input = { "nums": [4,7,11,6,11,8] }
assert my_solution.numberGame(**test_input) == [6,4,8,7,11,11]
test_input = { "nums": [1,7,24,23,16,21,9,11] }
assert my_solution.numberGame(**test_input) == [7,1,11,9,21,16,24,23]
test_input = { "nums": [6,3,10,16,15,6] }
assert my_solution.numberGame(**test_input) == [6,3,10,6,16,15]
test_input = { "nums": [17,9,1,29,30,5,31,26] }
assert my_solution.numberGame(**test_input) == [5,1,17,9,29,26,31,30]
test_input = { "nums": [3,6,4,14,9,15] }
assert my_solution.numberGame(**test_input) == [4,3,9,6,15,14]
test_input = { "nums": [37,38,24,15,12,1,37,19,38,11] }
assert my_solution.numberGame(**test_input) == [11,1,15,12,24,19,37,37,38,38]
test_input = { "nums": [17,3,8,12,6,9] }
assert my_solution.numberGame(**test_input) == [6,3,9,8,17,12]
test_input = { "nums": [32,23,27,32,24,26,24,27] }
assert my_solution.numberGame(**test_input) == [24,23,26,24,27,27,32,32]
test_input = { "nums": [15,16,26,6,5,9,22,14] }
assert my_solution.numberGame(**test_input) == [6,5,14,9,16,15,26,22]
test_input = { "nums": [14,21,13,10,2,16,14,30] }
assert my_solution.numberGame(**test_input) == [10,2,14,13,16,14,30,21]
test_input = { "nums": [1,6,30,1,13,25,18,1] }
assert my_solution.numberGame(**test_input) == [1,1,6,1,18,13,30,25]
test_input = { "nums": [32,12,17,32,11,25,22,18,10,1] }
assert my_solution.numberGame(**test_input) == [10,1,12,11,18,17,25,22,32,32]
test_input = { "nums": [2,8,5,6] }
assert my_solution.numberGame(**test_input) == [5,2,8,6]
test_input = { "nums": [27,3,10,25,10,7,15,16] }
assert my_solution.numberGame(**test_input) == [7,3,10,10,16,15,27,25]
test_input = { "nums": [5,18,19,25,13,21,16,7] }
assert my_solution.numberGame(**test_input) == [7,5,16,13,19,18,25,21]
test_input = { "nums": [8,6,6,8] }
assert my_solution.numberGame(**test_input) == [6,6,8,8]
test_input = { "nums": [23,15,39,9,19,10,6,9,33,28] }
assert my_solution.numberGame(**test_input) == [9,6,10,9,19,15,28,23,39,33]
test_input = { "nums": [16,42,47,16,31,39,8,26,50,33] }
assert my_solution.numberGame(**test_input) == [16,8,26,16,33,31,42,39,50,47]
test_input = { "nums": [4,31,9,2,4,28,28,12] }
assert my_solution.numberGame(**test_input) == [4,2,9,4,28,12,31,28]
test_input = { "nums": [9,5,8,11,4,7] }
assert my_solution.numberGame(**test_input) == [5,4,8,7,11,9]
test_input = { "nums": [44,2,23,3,7,2,36,33,7,21] }
assert my_solution.numberGame(**test_input) == [2,2,7,3,21,7,33,23,44,36]
test_input = { "nums": [19,9,4,7,29,22,50,28,2,40] }
assert my_solution.numberGame(**test_input) == [4,2,9,7,22,19,29,28,50,40]
test_input = { "nums": [4,5,5,5] }
assert my_solution.numberGame(**test_input) == [5,4,5,5]
test_input = { "nums": [42,6,44,47,11,6,30,38,41,43] }
assert my_solution.numberGame(**test_input) == [6,6,30,11,41,38,43,42,47,44]
test_input = { "nums": [28,4,47,1,7,35,10,10,5,8] }
assert my_solution.numberGame(**test_input) == [4,1,7,5,10,8,28,10,47,35]
test_input = { "nums": [12,20,14,46,22,1,42,50,47,47] }
assert my_solution.numberGame(**test_input) == [12,1,20,14,42,22,47,46,50,47]
test_input = { "nums": [37,13,1,38,28,46,18,22,12,7] }
assert my_solution.numberGame(**test_input) == [7,1,13,12,22,18,37,28,46,38]
test_input = { "nums": [36,41,5,33,5,30,33,31,6,45] }
assert my_solution.numberGame(**test_input) == [5,5,30,6,33,31,36,33,45,41]
test_input = { "nums": [13,50,42,24,47,41,8,26,34,3] }
assert my_solution.numberGame(**test_input) == [8,3,24,13,34,26,42,41,50,47]
test_input = { "nums": [24,39,26,46,47,9,33,6,33,40] }
assert my_solution.numberGame(**test_input) == [9,6,26,24,33,33,40,39,47,46]
test_input = { "nums": [14,13,17,14,12,15,6,32] }
assert my_solution.numberGame(**test_input) == [12,6,14,13,15,14,32,17]
test_input = { "nums": [46,50,35,11,14,44,17,45,23,34] }
assert my_solution.numberGame(**test_input) == [14,11,23,17,35,34,45,44,50,46]
test_input = { "nums": [8,27,19,7,10,12,14,50,45,14] }
assert my_solution.numberGame(**test_input) == [8,7,12,10,14,14,27,19,50,45]
test_input = { "nums": [9,8,5,7,10,9] }
assert my_solution.numberGame(**test_input) == [7,5,9,8,10,9]
test_input = { "nums": [5,5,3,7] }
assert my_solution.numberGame(**test_input) == [5,3,7,5]
test_input = { "nums": [26,21,7,13,3,10,9,15] }
assert my_solution.numberGame(**test_input) == [7,3,10,9,15,13,26,21]
test_input = { "nums": [8,5,8,3] }
assert my_solution.numberGame(**test_input) == [5,3,8,8]
test_input = { "nums": [18,1,16,18,13,3] }
assert my_solution.numberGame(**test_input) == [3,1,16,13,18,18]
test_input = { "nums": [25,2,17,26,17,20,19,24] }
assert my_solution.numberGame(**test_input) == [17,2,19,17,24,20,26,25]
test_input = { "nums": [24,1,18,25,29,17,9,3] }
assert my_solution.numberGame(**test_input) == [3,1,17,9,24,18,29,25]
test_input = { "nums": [23,17,18,18,18,30,8,19] }
assert my_solution.numberGame(**test_input) == [17,8,18,18,19,18,30,23]
test_input = { "nums": [12,13,13,18,5,16] }
assert my_solution.numberGame(**test_input) == [12,5,13,13,18,16]
test_input = { "nums": [19,4,11,7,24,12,24,14] }
assert my_solution.numberGame(**test_input) == [7,4,12,11,19,14,24,24]
test_input = { "nums": [28,11,11,29,18,2,6,32] }
assert my_solution.numberGame(**test_input) == [6,2,11,11,28,18,32,29]
test_input = { "nums": [12,17,3,31,15,18,18,2] }
assert my_solution.numberGame(**test_input) == [3,2,15,12,18,17,31,18]
test_input = { "nums": [24,6,21,30,29,8,23,18] }
assert my_solution.numberGame(**test_input) == [8,6,21,18,24,23,30,29] | 1,703,385,000 |
weekly-contest-377-maximum-square-area-by-removing-fences-from-a-field | https://leetcode.com/problems/maximum-square-area-by-removing-fences-from-a-field | maximum-square-area-by-removing-fences-from-a-field | {
"questionId": "3250",
"questionFrontendId": "2975",
"title": "Maximum Square Area by Removing Fences From a Field",
"titleSlug": "maximum-square-area-by-removing-fences-from-a-field",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 67,
"dislikes": 68,
"categoryTitle": "Algorithms"
} | """
There is a large (m - 1) x (n - 1) rectangular field with corners at (1, 1) and (m, n) containing some horizontal and vertical fences given in arrays hFences and vFences respectively.
Horizontal fences are from the coordinates (hFences[i], 1) to (hFences[i], n) and vertical fences are from the coordinates (1, vFences[i]) to (m, vFences[i]).
Return the maximum area of a square field that can be formed by removing some fences (possibly none) or -1 if it is impossible to make a square field.
Since the answer may be large, return it modulo 109 + 7.
Note: The field is surrounded by two horizontal fences from the coordinates (1, 1) to (1, n) and (m, 1) to (m, n) and two vertical fences from the coordinates (1, 1) to (m, 1) and (1, n) to (m, n). These fences cannot be removed.
Example 1:
Input: m = 4, n = 3, hFences = [2,3], vFences = [2]
Output: 4
Explanation: Removing the horizontal fence at 2 and the vertical fence at 2 will give a square field of area 4.
Example 2:
Input: m = 6, n = 7, hFences = [2], vFences = [4]
Output: -1
Explanation: It can be proved that there is no way to create a square field by removing fences.
Constraints:
3 <= m, n <= 109
1 <= hFences.length, vFences.length <= 600
1 < hFences[i] < m
1 < vFences[i] < n
hFences and vFences are unique.
"""
class Solution:
def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[int]) -> int:
| There is a large (m - 1) x (n - 1) rectangular field with corners at (1, 1) and (m, n) containing some horizontal and vertical fences given in arrays hFences and vFences respectively.
Horizontal fences are from the coordinates (hFences[i], 1) to (hFences[i], n) and vertical fences are from the coordinates (1, vFences[i]) to (m, vFences[i]).
Return the maximum area of a square field that can be formed by removing some fences (possibly none) or -1 if it is impossible to make a square field.
Since the answer may be large, return it modulo 109 + 7.
Note: The field is surrounded by two horizontal fences from the coordinates (1, 1) to (1, n) and (m, 1) to (m, n) and two vertical fences from the coordinates (1, 1) to (m, 1) and (1, n) to (m, n). These fences cannot be removed.
Example 1:
Input: m = 4, n = 3, hFences = [2,3], vFences = [2]
Output: 4
Explanation: Removing the horizontal fence at 2 and the vertical fence at 2 will give a square field of area 4.
Example 2:
Input: m = 6, n = 7, hFences = [2], vFences = [4]
Output: -1
Explanation: It can be proved that there is no way to create a square field by removing fences.
Constraints:
3 <= m, n <= 109
1 <= hFences.length, vFences.length <= 600
1 < hFences[i] < m
1 < vFences[i] < n
hFences and vFences are unique.
Please complete the code below to solve above prblem:
```python
class Solution:
def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "m": 4, "n": 3, "hFences": [2,3], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 6, "n": 7, "hFences": [2], "vFences": [4] }
assert my_solution.maximizeSquareArea(**test_input) == -1
test_input = { "m": 4, "n": 4, "hFences": [2], "vFences": [2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 8, "n": 5, "hFences": [5,4], "vFences": [4] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 4, "n": 5, "hFences": [2], "vFences": [4] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 5, "n": 6, "hFences": [4,2,3], "vFences": [4,5] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 3, "n": 9, "hFences": [2], "vFences": [8,6,5,4] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 6, "n": 4, "hFences": [3], "vFences": [3,2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 7, "n": 4, "hFences": [2,3,6,5], "vFences": [3,2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 8, "n": 8, "hFences": [2,3,6,7], "vFences": [6,5,7,4,2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 49
test_input = { "m": 9, "n": 9, "hFences": [2,4], "vFences": [6,4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 64
test_input = { "m": 7, "n": 7, "hFences": [4,3], "vFences": [2,6] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 4, "n": 6, "hFences": [2], "vFences": [3,4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 8, "n": 6, "hFences": [7,5,2,4,3], "vFences": [5,3] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 9, "n": 3, "hFences": [7,4,5], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 4, "n": 3, "hFences": [3], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 4, "n": 3, "hFences": [3,2], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 8, "n": 6, "hFences": [6,4,3,7,2,5], "vFences": [5,3,4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 8, "n": 8, "hFences": [6,3,7,5], "vFences": [6,2,7] }
assert my_solution.maximizeSquareArea(**test_input) == 49
test_input = { "m": 5, "n": 5, "hFences": [4,3], "vFences": [4] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 5, "n": 3, "hFences": [4], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 1
test_input = { "m": 7, "n": 9, "hFences": [5], "vFences": [2,7,6,8,3] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 8, "n": 6, "hFences": [5,4,6,7,3], "vFences": [5,3] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 9, "n": 7, "hFences": [6,4,7,5,8], "vFences": [6,4,2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 6, "n": 7, "hFences": [5], "vFences": [5,3,6] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 3, "n": 3, "hFences": [2], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 7, "n": 8, "hFences": [4,6,2,5,3], "vFences": [3,5,2,4,7] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 3, "n": 5, "hFences": [2], "vFences": [4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 4, "n": 3, "hFences": [2], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 9, "n": 3, "hFences": [3], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 6, "n": 7, "hFences": [4,2,3,5], "vFences": [3,5,6] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 8, "n": 4, "hFences": [6,3,2,4,7,5], "vFences": [2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 4, "n": 4, "hFences": [2], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 3, "n": 6, "hFences": [2], "vFences": [4,3,5] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 8, "n": 6, "hFences": [5], "vFences": [4,2,5] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 9, "n": 9, "hFences": [5,4,2], "vFences": [8,4,3,5,6] }
assert my_solution.maximizeSquareArea(**test_input) == 64
test_input = { "m": 5, "n": 9, "hFences": [3,2,4], "vFences": [7,6,5] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 5, "n": 5, "hFences": [4,3,2], "vFences": [3,4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 7, "n": 4, "hFences": [5,2,4], "vFences": [3] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 8, "n": 5, "hFences": [4,6,2,3], "vFences": [4] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 9, "n": 5, "hFences": [6], "vFences": [3] }
assert my_solution.maximizeSquareArea(**test_input) == -1
test_input = { "m": 9, "n": 4, "hFences": [2,8,3,7,4,6,5], "vFences": [2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 5, "n": 8, "hFences": [2,3,4], "vFences": [3,5,6,4] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 9, "n": 6, "hFences": [7,3,4,5,8,2], "vFences": [5,3,2,4] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 5, "n": 4, "hFences": [4], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 9, "n": 6, "hFences": [2,5], "vFences": [5,2] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 6, "n": 9, "hFences": [4,5,2], "vFences": [5,7,8,2,3,6] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 4, "n": 5, "hFences": [2], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 9, "n": 6, "hFences": [2,4], "vFences": [5,3] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 4, "n": 7, "hFences": [2], "vFences": [4,3,6,2,5] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 8, "n": 3, "hFences": [3,2,5], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 6, "n": 8, "hFences": [4,2,3,5], "vFences": [7,4,5,6] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 3, "n": 5, "hFences": [2], "vFences": [3] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 8, "n": 8, "hFences": [2,5,6], "vFences": [3,7,4,2,5] }
assert my_solution.maximizeSquareArea(**test_input) == 49
test_input = { "m": 8, "n": 7, "hFences": [3,4,7], "vFences": [2,6,3,4] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 7, "n": 4, "hFences": [3,6,5], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 5, "n": 6, "hFences": [2,3], "vFences": [3,2,5] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 6, "n": 7, "hFences": [5], "vFences": [4,2,5,6] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 8, "n": 8, "hFences": [4,5,2,7], "vFences": [5,3,4,2,7,6] }
assert my_solution.maximizeSquareArea(**test_input) == 49
test_input = { "m": 7, "n": 9, "hFences": [6,3,4], "vFences": [8,6,2] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 7, "n": 4, "hFences": [3,4,6], "vFences": [3,2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 6, "n": 8, "hFences": [5,4,3], "vFences": [5,7,3,2,6,4] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 8, "n": 9, "hFences": [2,3,4,7,6,5], "vFences": [3,7,8] }
assert my_solution.maximizeSquareArea(**test_input) == 49
test_input = { "m": 3, "n": 4, "hFences": [2], "vFences": [2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 9, "n": 9, "hFences": [8,5,6,2,7], "vFences": [8,6,4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 64
test_input = { "m": 7, "n": 6, "hFences": [4,5], "vFences": [5,3] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 5, "n": 4, "hFences": [2], "vFences": [3] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 5, "n": 5, "hFences": [3], "vFences": [3,4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 9, "n": 7, "hFences": [3], "vFences": [3,2,5,4] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 8, "n": 6, "hFences": [4], "vFences": [5,2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 6, "n": 9, "hFences": [3,4,2], "vFences": [3,2,8] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 7, "n": 6, "hFences": [3,6,5,2,4], "vFences": [2,3,5,4] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 7, "n": 3, "hFences": [5,3,6,4], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 4, "n": 7, "hFences": [3], "vFences": [2] }
assert my_solution.maximizeSquareArea(**test_input) == 1
test_input = { "m": 3, "n": 7, "hFences": [2], "vFences": [4,3,2,6,5] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 5, "n": 9, "hFences": [2,4], "vFences": [4,7] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 5, "n": 9, "hFences": [4,2], "vFences": [8,7,3,2,6,4] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 3, "n": 8, "hFences": [2], "vFences": [3,7,2,5,4] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 4, "n": 7, "hFences": [2,3], "vFences": [4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 8, "n": 9, "hFences": [7,6,3,2,5,4], "vFences": [3,2,6] }
assert my_solution.maximizeSquareArea(**test_input) == 49
test_input = { "m": 7, "n": 8, "hFences": [6], "vFences": [5] }
assert my_solution.maximizeSquareArea(**test_input) == -1
test_input = { "m": 9, "n": 7, "hFences": [2,7,8,5], "vFences": [6,3] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 9, "n": 9, "hFences": [4,7,2,5,8,6], "vFences": [7,6,4,5,8,2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 64
test_input = { "m": 7, "n": 7, "hFences": [6,3,2,5], "vFences": [6,3] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 7, "n": 7, "hFences": [5], "vFences": [2,3,6] }
assert my_solution.maximizeSquareArea(**test_input) == 36
test_input = { "m": 4, "n": 9, "hFences": [3], "vFences": [4,3,7,6,8,5,2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 4, "n": 5, "hFences": [2,3], "vFences": [3,4] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 3, "n": 7, "hFences": [2], "vFences": [3,6] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 4, "n": 9, "hFences": [3,2], "vFences": [4,6] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 9, "n": 9, "hFences": [5], "vFences": [6,5,2,7,3,8] }
assert my_solution.maximizeSquareArea(**test_input) == 64
test_input = { "m": 5, "n": 6, "hFences": [3,4,2], "vFences": [5,3,2] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 5, "n": 6, "hFences": [3,2], "vFences": [5] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 5, "n": 5, "hFences": [3,4], "vFences": [3,2,4] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 9, "n": 5, "hFences": [7,5,8], "vFences": [4,2,3] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 3, "n": 6, "hFences": [2], "vFences": [2,5,4] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 5, "n": 9, "hFences": [4,2,3], "vFences": [8,3,5,6,4,2] }
assert my_solution.maximizeSquareArea(**test_input) == 16
test_input = { "m": 4, "n": 7, "hFences": [3,2], "vFences": [5] }
assert my_solution.maximizeSquareArea(**test_input) == 4
test_input = { "m": 6, "n": 9, "hFences": [4,2], "vFences": [3,4,7,2] }
assert my_solution.maximizeSquareArea(**test_input) == 25
test_input = { "m": 6, "n": 5, "hFences": [4], "vFences": [3,2] }
assert my_solution.maximizeSquareArea(**test_input) == 9
test_input = { "m": 8, "n": 5, "hFences": [6,7,4,5,2], "vFences": [4] }
assert my_solution.maximizeSquareArea(**test_input) == 16 | 1,703,385,000 |
weekly-contest-377-minimum-cost-to-convert-string-i | https://leetcode.com/problems/minimum-cost-to-convert-string-i | minimum-cost-to-convert-string-i | {
"questionId": "3235",
"questionFrontendId": "2976",
"title": "Minimum Cost to Convert String I",
"titleSlug": "minimum-cost-to-convert-string-i",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 93,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].
You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.
Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.
Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
Example 1:
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
Explanation: To convert the string "abcd" to string "acbe":
- Change value at index 1 from 'b' to 'c' at a cost of 5.
- Change value at index 2 from 'c' to 'e' at a cost of 1.
- Change value at index 2 from 'e' to 'b' at a cost of 2.
- Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Example 2:
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
Output: 12
Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
Example 3:
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
Output: -1
Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
Constraints:
1 <= source.length == target.length <= 105
source, target consist of lowercase English letters.
1 <= cost.length == original.length == changed.length <= 2000
original[i], changed[i] are lowercase English letters.
1 <= cost[i] <= 106
original[i] != changed[i]
"""
class Solution:
def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
| You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].
You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.
Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.
Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
Example 1:
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
Explanation: To convert the string "abcd" to string "acbe":
- Change value at index 1 from 'b' to 'c' at a cost of 5.
- Change value at index 2 from 'c' to 'e' at a cost of 1.
- Change value at index 2 from 'e' to 'b' at a cost of 2.
- Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Example 2:
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
Output: 12
Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
Example 3:
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
Output: -1
Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
Constraints:
1 <= source.length == target.length <= 105
source, target consist of lowercase English letters.
1 <= cost.length == original.length == changed.length <= 2000
original[i], changed[i] are lowercase English letters.
1 <= cost[i] <= 106
original[i] != changed[i]
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "source": "abcd", "target": "acbe", "original": ["a","b","c","c","e","d"], "changed": ["b","c","b","e","b","e"], "cost": [2,5,5,1,2,20] }
assert my_solution.minimumCost(**test_input) == 28
test_input = { "source": "aaaa", "target": "bbbb", "original": ["a","c"], "changed": ["c","b"], "cost": [1,2] }
assert my_solution.minimumCost(**test_input) == 12
test_input = { "source": "abcd", "target": "abce", "original": ["a"], "changed": ["e"], "cost": [10000] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "aaaabadaaa", "target": "dbdadddbad", "original": ["c","a","c","a","a","b","b","b","d","d","c"], "changed": ["a","c","b","d","b","c","a","d","c","b","d"], "cost": [7,8,11,9,7,6,4,6,9,5,9] }
assert my_solution.minimumCost(**test_input) == 56
test_input = { "source": "aaadbdcdac", "target": "cdbabaddba", "original": ["a","c","b","d","b","a","c"], "changed": ["c","a","d","b","c","b","d"], "cost": [7,2,1,3,6,1,7] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "source": "aababdaacb", "target": "bcdcdcbdcb", "original": ["a","d","d","a","c","b","c","a","c","d","b","b"], "changed": ["b","c","b","d","a","a","b","c","d","a","c","d"], "cost": [11,4,3,2,7,11,7,6,9,2,1,7] }
assert my_solution.minimumCost(**test_input) == 42
test_input = { "source": "aababdbddc", "target": "adcbbbcdba", "original": ["a","d","b","a","d","c","d","b"], "changed": ["b","a","d","c","c","a","b","a"], "cost": [10,6,8,3,6,10,8,6] }
assert my_solution.minimumCost(**test_input) == 72
test_input = { "source": "aabbcabbdb", "target": "acddbabbdd", "original": ["c","d","c","a","d","c","a","d","b","a","b"], "changed": ["d","b","a","c","c","b","b","a","d","d","c"], "cost": [5,3,8,10,9,7,8,7,5,1,10] }
assert my_solution.minimumCost(**test_input) == 32
test_input = { "source": "aabbddccbc", "target": "abbbaabaca", "original": ["a","b","c","b","a","d"], "changed": ["d","c","b","d","b","b"], "cost": [3,8,7,6,7,10] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "aabdbaabaa", "target": "bdaacabcab", "original": ["b","d","d","a","c","c","a","d","a","b"], "changed": ["c","c","b","d","b","d","b","a","c","a"], "cost": [9,1,7,9,2,1,3,8,8,2] }
assert my_solution.minimumCost(**test_input) == 43
test_input = { "source": "aacacaaccd", "target": "dadaacaabd", "original": ["c","c","a","a","d","b","d","d"], "changed": ["b","d","d","b","b","c","c","a"], "cost": [7,8,9,11,4,6,9,10] }
assert my_solution.minimumCost(**test_input) == 77
test_input = { "source": "aacbabbacc", "target": "adbdbcbdaa", "original": ["c","b","a","b","a","c","d","c","d"], "changed": ["b","c","b","d","d","a","b","d","c"], "cost": [2,6,7,4,7,4,3,5,6] }
assert my_solution.minimumCost(**test_input) == 41
test_input = { "source": "aacbbabdad", "target": "ddadcababd", "original": ["d","b","c","a","b","c","d","c","b","a","a"], "changed": ["c","d","d","b","c","b","b","a","a","c","d"], "cost": [7,10,4,2,7,4,4,4,6,2,8] }
assert my_solution.minimumCost(**test_input) == 45
test_input = { "source": "aacbbbbcab", "target": "cdacdcddac", "original": ["b","d","c","c","b","a"], "changed": ["c","c","b","a","a","d"], "cost": [4,7,9,11,3,4] }
assert my_solution.minimumCost(**test_input) == 67
test_input = { "source": "aacbcabcad", "target": "bbcadddcdd", "original": ["b","a","d","a","b","c","a","d","d","b"], "changed": ["d","b","b","d","c","a","c","c","a","a"], "cost": [7,7,9,8,6,3,8,2,1,5] }
assert my_solution.minimumCost(**test_input) == 53
test_input = { "source": "aacbdbcdca", "target": "bbbdbcaacd", "original": ["a","c","b","d","d","a","c","d"], "changed": ["c","b","c","c","b","d","d","a"], "cost": [9,5,4,1,2,4,7,1] }
assert my_solution.minimumCost(**test_input) == 47
test_input = { "source": "aadbbcdbbd", "target": "badddbdbac", "original": ["c","d","c","d","b","a"], "changed": ["b","b","a","a","a","d"], "cost": [11,4,7,8,5,2] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "aadbccbddd", "target": "cacdbabadc", "original": ["d","b","c","d","a","a","c","b"], "changed": ["c","c","b","b","b","d","a","a"], "cost": [5,8,7,2,4,7,1,5] }
assert my_solution.minimumCost(**test_input) == 46
test_input = { "source": "aadbddcabd", "target": "bdcdccbada", "original": ["d","a","a","b","d","b"], "changed": ["b","c","d","c","a","d"], "cost": [6,10,5,8,11,4] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "aaddadccad", "target": "cbaaadbcba", "original": ["c","a","a","d","c","c","b","b","a","d"], "changed": ["a","c","d","c","d","b","d","c","b","b"], "cost": [1,10,2,8,9,1,9,10,5,1] }
assert my_solution.minimumCost(**test_input) == 44
test_input = { "source": "aaddadcdba", "target": "caaaccbbca", "original": ["b","b","c","d","b","c","a","a"], "changed": ["a","d","d","a","c","b","c","b"], "cost": [11,7,10,8,7,5,10,10] }
assert my_solution.minimumCost(**test_input) == 84
test_input = { "source": "abaacbbcaa", "target": "bdbdbcbdcd", "original": ["d","a","d","a","b","b"], "changed": ["a","d","b","b","a","c"], "cost": [10,9,8,11,4,11] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "abacaadcba", "target": "cadbadcdbd", "original": ["b","d","c","a","b","d","b"], "changed": ["a","b","b","b","c","a","d"], "cost": [9,10,6,2,7,10,9] }
assert my_solution.minimumCost(**test_input) == 89
test_input = { "source": "abacbadadc", "target": "aabbdaaccb", "original": ["d","a","b","d","a","a","c","b","c","c","d","b"], "changed": ["c","b","a","a","d","c","b","c","d","a","b","d"], "cost": [2,10,3,6,4,1,5,5,11,4,2,1] }
assert my_solution.minimumCost(**test_input) == 28
test_input = { "source": "abadbbabcd", "target": "cdcbdddcbb", "original": ["d","d","b","a","c","c","c","a"], "changed": ["c","a","a","c","b","d","a","d"], "cost": [2,10,11,7,6,11,7,1] }
assert my_solution.minimumCost(**test_input) == 79
test_input = { "source": "abadcadacc", "target": "cbabaddcba", "original": ["a","d","a","b","c","a","d","b","b","d","c","c"], "changed": ["b","b","d","d","a","c","a","c","a","c","d","b"], "cost": [7,6,11,11,8,10,4,11,2,3,11,7] }
assert my_solution.minimumCost(**test_input) == 60
test_input = { "source": "abadcdadac", "target": "baddbccdac", "original": ["d","c","d","c","b","a"], "changed": ["b","b","c","a","d","d"], "cost": [8,5,9,1,10,2] }
assert my_solution.minimumCost(**test_input) == 57
test_input = { "source": "abbaadacba", "target": "cdbbcadddd", "original": ["d","a","d","c","b","b","c","d","c","a","a"], "changed": ["a","c","c","d","a","d","a","b","b","d","b"], "cost": [8,3,5,8,3,9,3,4,11,4,9] }
assert my_solution.minimumCost(**test_input) == 50
test_input = { "source": "abbaddaacd", "target": "ccbbaccacc", "original": ["d","d","a","b","c","b"], "changed": ["a","c","c","d","b","c"], "cost": [9,8,2,8,3,1] }
assert my_solution.minimumCost(**test_input) == 35
test_input = { "source": "abbbcabddb", "target": "bbccdbbadc", "original": ["c","d","c","a","b","d","d","a","b","b"], "changed": ["d","a","b","c","c","b","c","d","a","d"], "cost": [3,6,9,4,9,6,9,6,10,7] }
assert my_solution.minimumCost(**test_input) == 60
test_input = { "source": "abbbcbabab", "target": "abcacbaddd", "original": ["b","c","a","c","a","d","d","c"], "changed": ["a","b","d","a","b","b","c","d"], "cost": [11,5,8,1,7,7,1,2] }
assert my_solution.minimumCost(**test_input) == 77
test_input = { "source": "abbcaccabb", "target": "ddddddcacc", "original": ["a","b","c","b","a","c","c"], "changed": ["c","c","d","a","d","a","b"], "cost": [2,6,10,11,9,7,3] }
assert my_solution.minimumCost(**test_input) == 82
test_input = { "source": "abbcaccdba", "target": "accadababc", "original": ["d","a","a","c","b","d"], "changed": ["c","d","b","a","a","b"], "cost": [7,4,10,11,5,5] }
assert my_solution.minimumCost(**test_input) == 99
test_input = { "source": "abbdaccada", "target": "acddaccddc", "original": ["b","b","c","a","d","a","d"], "changed": ["a","c","b","b","b","c","c"], "cost": [4,9,3,1,11,3,3] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "abcabdaddb", "target": "dcbadaaacc", "original": ["d","b","a","a","c","c"], "changed": ["c","c","d","b","b","a"], "cost": [3,3,9,3,7,6] }
assert my_solution.minimumCost(**test_input) == 61
test_input = { "source": "abcadcabaa", "target": "bbbdddcaba", "original": ["b","d","c","d","a","c","b","b"], "changed": ["c","b","a","a","c","b","d","a"], "cost": [1,8,4,3,8,3,11,5] }
assert my_solution.minimumCost(**test_input) == 74
test_input = { "source": "abccabacaa", "target": "aaabacbcbb", "original": ["c","c","d","a","d","b","c","b","d","a"], "changed": ["b","a","a","b","b","d","d","a","c","c"], "cost": [9,10,8,6,9,10,2,6,6,8] }
assert my_solution.minimumCost(**test_input) == 57
test_input = { "source": "abdaababbb", "target": "dbdadabadc", "original": ["a","c","c","b","d","a","b"], "changed": ["c","a","b","c","b","b","d"], "cost": [3,4,6,1,8,11,6] }
assert my_solution.minimumCost(**test_input) == 56
test_input = { "source": "abdbaaacaa", "target": "abbbccccad", "original": ["a","a","c","b","d","d","b"], "changed": ["d","b","b","a","a","b","c"], "cost": [3,10,7,2,5,7,3] }
assert my_solution.minimumCost(**test_input) == 49
test_input = { "source": "abdcbdbccc", "target": "dbbcdcabba", "original": ["c","c","d","b","a","c","a","d","b","d","a","b"], "changed": ["d","a","b","d","c","b","b","a","c","c","d","a"], "cost": [9,5,9,6,5,5,5,10,7,7,3,6] }
assert my_solution.minimumCost(**test_input) == 46
test_input = { "source": "acabbbdbdb", "target": "accbccbbab", "original": ["b","d","c","d","b","c"], "changed": ["a","a","a","c","d","d"], "cost": [7,7,10,9,7,1] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "acadadccbb", "target": "dcaaabbbdd", "original": ["a","c","c","a","d","b","b","d","b","a"], "changed": ["d","d","b","b","c","d","a","a","c","c"], "cost": [1,2,1,4,9,4,8,5,11,7] }
assert my_solution.minimumCost(**test_input) == 25
test_input = { "source": "acadbbcdcb", "target": "bcacabdcdd", "original": ["a","b","d","b","b","c","a"], "changed": ["d","c","c","a","d","b","b"], "cost": [3,6,3,10,11,3,6] }
assert my_solution.minimumCost(**test_input) == 61
test_input = { "source": "acaddccaad", "target": "daacadcdda", "original": ["c","c","a","b","b","a","b","d","c","a","d"], "changed": ["a","b","b","d","a","d","c","b","d","c","c"], "cost": [10,8,4,8,3,1,2,8,11,8,6] }
assert my_solution.minimumCost(**test_input) == 52
test_input = { "source": "acbbabcaac", "target": "bdcbaadcab", "original": ["d","c","a","c","b","a","a","b","c","d"], "changed": ["a","a","d","d","c","c","b","a","b","c"], "cost": [9,11,8,6,11,11,1,1,9,9] }
assert my_solution.minimumCost(**test_input) == 45
test_input = { "source": "accabbadbc", "target": "adbbccbcbd", "original": ["a","c","a","d","b","a","c","c","b","b"], "changed": ["c","d","b","c","d","d","b","a","c","a"], "cost": [7,10,9,3,2,5,1,8,11,2] }
assert my_solution.minimumCost(**test_input) == 36
test_input = { "source": "accabbdddd", "target": "cacdccbcad", "original": ["c","a","d","b","d","c","a","b","b","c","d"], "changed": ["d","b","a","d","c","a","d","a","c","b","b"], "cost": [11,6,6,4,7,11,2,7,7,7,2] }
assert my_solution.minimumCost(**test_input) == 51
test_input = { "source": "accbaadbdb", "target": "baccbaacbb", "original": ["b","b","a","d","d","a","c"], "changed": ["a","d","d","b","a","c","a"], "cost": [9,11,6,7,4,2,2] }
assert my_solution.minimumCost(**test_input) == 61
test_input = { "source": "accbddaaab", "target": "baddbaabbd", "original": ["a","b","c","d","d","b","d","b"], "changed": ["b","a","a","b","a","c","c","d"], "cost": [6,3,4,6,1,6,10,6] }
assert my_solution.minimumCost(**test_input) == 57
test_input = { "source": "acccbcdccb", "target": "bdadccdbad", "original": ["a","b","c","c","a","d","d"], "changed": ["b","c","a","d","c","a","c"], "cost": [8,1,1,9,3,10,4] }
assert my_solution.minimumCost(**test_input) == 48
test_input = { "source": "accccbccda", "target": "daadbbcaac", "original": ["a","c","a","a","d","d"], "changed": ["c","d","d","b","b","c"], "cost": [3,6,6,10,9,8] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "acdacbdadb", "target": "aacccbbacd", "original": ["b","b","a","a","d","c"], "changed": ["d","a","b","d","a","a"], "cost": [6,1,9,6,8,11] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "acdbcdadbd", "target": "daaadaaadd", "original": ["c","a","b","b","b","d","a"], "changed": ["d","c","d","c","a","b","d"], "cost": [3,9,4,6,1,9,3] }
assert my_solution.minimumCost(**test_input) == 54
test_input = { "source": "acddadcbca", "target": "ddaabaaaac", "original": ["b","d","c","a","b","d","c","b","d","c","a"], "changed": ["c","a","b","c","d","c","a","a","b","d","d"], "cost": [7,8,2,10,1,7,8,1,1,11,4] }
assert my_solution.minimumCost(**test_input) == 35
test_input = { "source": "adaadcaddd", "target": "cdddbdccad", "original": ["c","c","c","d","b","a","d"], "changed": ["d","a","b","b","d","b","c"], "cost": [10,9,2,2,7,1,10] }
assert my_solution.minimumCost(**test_input) == 92
test_input = { "source": "adaaddacba", "target": "aabbddbbdd", "original": ["c","b","a","b","c","b"], "changed": ["b","c","b","a","d","d"], "cost": [10,7,7,6,8,5] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "adacdcdacd", "target": "ccbabbbbdc", "original": ["a","b","c","a","d","b","a","c"], "changed": ["c","d","d","d","a","c","b","a"], "cost": [4,3,1,1,6,4,10,6] }
assert my_solution.minimumCost(**test_input) == 99
test_input = { "source": "adadbabcdd", "target": "abbcdcbdba", "original": ["c","d","b","a","c","b","a"], "changed": ["d","b","d","b","b","a","c"], "cost": [11,10,6,1,5,3,8] }
assert my_solution.minimumCost(**test_input) == 80
test_input = { "source": "adadcabbda", "target": "cabadddccc", "original": ["c","a","b","b","a","d","d"], "changed": ["b","d","d","c","b","c","b"], "cost": [7,2,8,4,4,4,7] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "adbaabacdc", "target": "bccbbadcdc", "original": ["c","b","b","d","c","a","b"], "changed": ["b","a","c","a","a","d","d"], "cost": [5,2,6,1,7,7,1] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "adbadbaacb", "target": "bccdbdccab", "original": ["d","a","c","b","c","d","a","b","c","b","a","d"], "changed": ["a","c","b","c","a","b","b","d","d","a","d","c"], "cost": [3,7,7,9,2,9,10,2,9,5,11,8] }
assert my_solution.minimumCost(**test_input) == 65
test_input = { "source": "adbcdaddda", "target": "cbdccabcbc", "original": ["c","a","d","d","b","b","b"], "changed": ["a","c","c","a","d","c","a"], "cost": [8,5,5,10,10,3,9] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "adcacaaabb", "target": "daaadadcbb", "original": ["d","b","a","a","d","c","b","b","d","c","c","a"], "changed": ["b","c","d","c","c","a","a","d","a","b","d","b"], "cost": [2,3,2,4,5,9,11,3,10,1,9,2] }
assert my_solution.minimumCost(**test_input) == 31
test_input = { "source": "adcbbbdada", "target": "cdaabadcdc", "original": ["a","a","b","c","d","b","d"], "changed": ["c","d","c","d","a","a","c"], "cost": [11,11,2,8,5,7,5] }
assert my_solution.minimumCost(**test_input) == 60
test_input = { "source": "adccbabbca", "target": "dcdbbdabba", "original": ["a","d","d","b","c","b","a"], "changed": ["c","b","c","c","a","d","d"], "cost": [5,10,10,1,6,7,7] }
assert my_solution.minimumCost(**test_input) == 90
test_input = { "source": "adcdcbacab", "target": "acddaddadc", "original": ["b","d","c","d","c","d","b","c","a","a","a","b"], "changed": ["a","b","b","c","a","a","d","d","c","b","d","c"], "cost": [2,11,11,9,1,3,6,9,6,4,8,5] }
assert my_solution.minimumCost(**test_input) == 47
test_input = { "source": "addbaccbbd", "target": "cabdcdadcc", "original": ["b","d","d","d","c","b","a","c","c"], "changed": ["a","c","a","b","b","d","d","a","d"], "cost": [10,11,5,6,10,1,4,8,8] }
assert my_solution.minimumCost(**test_input) == 82
test_input = { "source": "addbacdaac", "target": "abddcadbcb", "original": ["d","a","c","b","a","c","a","d","c","d","b"], "changed": ["c","c","b","d","d","d","b","b","a","a","c"], "cost": [9,2,9,4,11,6,10,3,7,2,5] }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "source": "addbcccdcb", "target": "cbbdbddacb", "original": ["d","c","a","a","b","b","c"], "changed": ["c","d","b","d","d","c","b"], "cost": [2,6,4,3,7,7,8] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "addcadccaa", "target": "dbbcaccabc", "original": ["a","b","d","b","d","a","c","b","a","c"], "changed": ["c","d","a","c","b","b","d","a","d","a"], "cost": [8,11,5,1,11,4,3,8,11,4] }
assert my_solution.minimumCost(**test_input) == 49
test_input = { "source": "addcdbdadb", "target": "bcabdcccbd", "original": ["b","b","a","c","d","a","b","c","a"], "changed": ["a","c","c","a","a","b","d","b","d"], "cost": [2,4,8,8,3,5,2,7,2] }
assert my_solution.minimumCost(**test_input) == 59
test_input = { "source": "adddbbdbdb", "target": "cdbadcaccc", "original": ["b","c","c","b","a","c","b","a","a","d","d"], "changed": ["d","a","b","a","b","d","c","c","d","a","c"], "cost": [1,1,1,8,6,9,3,6,5,3,10] }
assert my_solution.minimumCost(**test_input) == 40
test_input = { "source": "adddccacca", "target": "cdcdcccdac", "original": ["d","c","a","d","b","b","b","a","d","a","c","c"], "changed": ["c","a","c","a","d","a","c","b","b","d","d","b"], "cost": [7,7,6,10,1,1,11,5,3,2,10,3] }
assert my_solution.minimumCost(**test_input) == 33
test_input = { "source": "baacbbcdaa", "target": "abdbdbbabd", "original": ["c","d","c","b","a","c","b","d","b"], "changed": ["d","c","b","a","d","a","d","a","c"], "cost": [9,5,5,2,9,4,5,3,6] }
assert my_solution.minimumCost(**test_input) == 76
test_input = { "source": "baadcdabbc", "target": "acbccadccd", "original": ["b","b","a","a","a","d","d","d","c","c","b"], "changed": ["c","a","c","b","d","c","a","b","b","d","d"], "cost": [8,6,5,10,11,1,1,6,3,1,4] }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "source": "baadcdabda", "target": "abdbcdaaca", "original": ["b","b","c","d","d","a","c"], "changed": ["d","a","a","b","c","b","d"], "cost": [11,8,7,3,10,4,1] }
assert my_solution.minimumCost(**test_input) == 48
test_input = { "source": "babababdba", "target": "ccdaaabbac", "original": ["c","d","b","a","d","a","b"], "changed": ["b","a","d","c","c","d","c"], "cost": [8,5,6,2,8,6,8] }
assert my_solution.minimumCost(**test_input) == 55
test_input = { "source": "babbacabba", "target": "adacccdcba", "original": ["c","d","d","d","c","b","b","b","a"], "changed": ["a","b","c","a","d","a","c","d","c"], "cost": [1,1,8,3,2,2,11,8,3] }
assert my_solution.minimumCost(**test_input) == 27
test_input = { "source": "babbadbabc", "target": "ccdabbcbba", "original": ["a","c","d","a","b","d","b","b"], "changed": ["b","d","c","d","a","a","c","d"], "cost": [3,3,1,4,4,8,2,9] }
assert my_solution.minimumCost(**test_input) == 46
test_input = { "source": "bacbddaacb", "target": "dcdaaadcda", "original": ["a","d","a","a","d","c","b","c","c","b"], "changed": ["d","c","c","b","b","b","a","a","d","c"], "cost": [8,5,1,10,8,6,2,1,6,8] }
assert my_solution.minimumCost(**test_input) == 46
test_input = { "source": "baccbbcdcb", "target": "cabadbbacc", "original": ["c","a","c","b","c","d","a","b","d","b"], "changed": ["a","b","b","a","d","b","c","d","c","c"], "cost": [4,4,2,11,9,9,1,4,6,1] }
assert my_solution.minimumCost(**test_input) == 24
test_input = { "source": "bacdbbcdba", "target": "cdcdddbbcd", "original": ["d","a","c","b","d","a","b","c","b","c","d"], "changed": ["c","c","a","d","b","b","a","d","c","b","a"], "cost": [3,8,4,6,5,8,6,2,1,6,2] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "source": "bacdbccabb", "target": "caaccdbaac", "original": ["c","d","d","a","d","c"], "changed": ["d","b","a","b","c","a"], "cost": [8,4,4,4,3,6] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "bacddacdba", "target": "bcbbaacdda", "original": ["b","c","c","d","d","b","b","d","a","a","c","a"], "changed": ["a","b","d","b","a","c","d","c","c","d","a","b"], "cost": [5,6,7,4,4,8,8,6,8,3,6,11] }
assert my_solution.minimumCost(**test_input) == 30
test_input = { "source": "badaabbaba", "target": "caadbcadcd", "original": ["b","c","a","c","d","a","b","d","d","b","c"], "changed": ["c","a","d","b","a","c","d","c","b","a","d"], "cost": [4,8,6,3,1,8,3,8,3,6,9] }
assert my_solution.minimumCost(**test_input) == 44
test_input = { "source": "badabbbbac", "target": "dacaabbcaa", "original": ["d","b","a","d","c","b","d","c","a","c","a"], "changed": ["b","c","d","c","a","d","a","d","c","b","b"], "cost": [11,5,9,7,11,11,7,9,6,11,5] }
assert my_solution.minimumCost(**test_input) == 50
test_input = { "source": "badaccbdbd", "target": "dbbdacaaab", "original": ["b","d","b","c","b","d","a","d","a","a","c","c"], "changed": ["d","a","a","a","c","b","d","c","b","c","d","b"], "cost": [3,9,3,11,11,6,2,11,11,2,11,1] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "source": "badadcccba", "target": "bbdbababcc", "original": ["c","c","a","d","d","d"], "changed": ["a","d","b","a","b","c"], "cost": [1,3,2,2,4,4] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "badbbbccdb", "target": "bbbabbccbd", "original": ["a","c","a","c","d","c","d","b","d","b","a"], "changed": ["c","b","d","d","c","a","b","d","a","c","b"], "cost": [5,4,5,3,4,4,6,3,8,11,4] }
assert my_solution.minimumCost(**test_input) == 30
test_input = { "source": "badcbccabc", "target": "bdcaacbcad", "original": ["d","d","c","d","a","b","c","a"], "changed": ["a","c","d","b","b","a","b","d"], "cost": [1,5,4,5,11,10,8,11] }
assert my_solution.minimumCost(**test_input) == 69
test_input = { "source": "badcbdddcd", "target": "cdcbaddadc", "original": ["c","b","c","d","a","b","b","d","a","c","a","d"], "changed": ["a","c","b","a","c","d","a","b","d","d","b","c"], "cost": [3,2,8,9,11,5,11,11,9,2,8,1] }
assert my_solution.minimumCost(**test_input) == 32
test_input = { "source": "baddbcbdbd", "target": "acdbcadabd", "original": ["a","b","b","c","a","a","b","d","c","d","d"], "changed": ["c","a","d","d","b","d","c","c","b","b","a"], "cost": [9,10,5,4,1,5,7,8,11,9,8] }
assert my_solution.minimumCost(**test_input) == 59
test_input = { "source": "baddbdacad", "target": "cadaccbbab", "original": ["b","a","d","a","c","b","d","c","a","d"], "changed": ["d","b","b","d","a","c","a","b","c","c"], "cost": [3,5,6,7,2,4,4,2,3,9] }
assert my_solution.minimumCost(**test_input) == 32
test_input = { "source": "bbacdcdcda", "target": "cbadabbdcb", "original": ["a","d","b","c","b","c","d","a","d"], "changed": ["b","c","d","a","a","b","b","d","a"], "cost": [11,6,2,8,5,7,5,8,9] }
assert my_solution.minimumCost(**test_input) == 64
test_input = { "source": "bbadbbabbb", "target": "cbaaddaddc", "original": ["d","a","b","c","d","c","b"], "changed": ["b","d","c","b","c","d","d"], "cost": [4,10,1,11,7,1,11] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "bbadcbcbbc", "target": "aaaccbccbb", "original": ["b","d","b","d","a","c","c","a","b"], "changed": ["c","a","a","b","b","a","b","c","d"], "cost": [8,5,9,4,1,10,1,11,4] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "source": "bbbadaccbb", "target": "dadcaccadb", "original": ["a","d","d","d","c","b","c","b","c","b"], "changed": ["d","b","c","a","b","d","d","c","a","a"], "cost": [10,1,4,6,4,1,10,3,1,6] }
assert my_solution.minimumCost(**test_input) == 41
test_input = { "source": "bbbadcbadb", "target": "aacbdcddcd", "original": ["a","d","d","c","a","b","c","b"], "changed": ["d","b","a","a","c","c","b","d"], "cost": [3,1,1,9,1,2,1,2] }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "source": "bbbbabbcbc", "target": "adacababac", "original": ["a","c","a","b","b","d","d","a","d","b","c"], "changed": ["d","b","c","c","d","a","b","b","c","a","d"], "cost": [7,9,9,7,3,2,6,8,11,5,8] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "source": "bbbbdacbcd", "target": "cbadccdaaa", "original": ["d","a","c","b","d","c","a","d","b","a"], "changed": ["c","c","b","c","a","d","d","b","d","b"], "cost": [2,9,11,3,6,11,6,4,5,5] }
assert my_solution.minimumCost(**test_input) == 74 | 1,703,385,000 |
weekly-contest-377-minimum-cost-to-convert-string-ii | https://leetcode.com/problems/minimum-cost-to-convert-string-ii | minimum-cost-to-convert-string-ii | {
"questionId": "3238",
"questionFrontendId": "2977",
"title": "Minimum Cost to Convert String II",
"titleSlug": "minimum-cost-to-convert-string-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 40,
"dislikes": 36,
"categoryTitle": "Algorithms"
} | """
You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English characters. You are also given two 0-indexed string arrays original and changed, and an integer array cost, where cost[i] represents the cost of converting the string original[i] to the string changed[i].
You start with the string source. In one operation, you can pick a substring x from the string, and change it to y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y. You are allowed to do any number of operations, but any pair of operations must satisfy either of these two conditions:
The substrings picked in the operations are source[a..b] and source[c..d] with either b < c or d < a. In other words, the indices picked in both operations are disjoint.
The substrings picked in the operations are source[a..b] and source[c..d] with a == c and b == d. In other words, the indices picked in both operations are identical.
Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.
Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
Example 1:
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
Explanation: To convert "abcd" to "acbe", do the following operations:
- Change substring source[1..1] from "b" to "c" at a cost of 5.
- Change substring source[2..2] from "c" to "e" at a cost of 1.
- Change substring source[2..2] from "e" to "b" at a cost of 2.
- Change substring source[3..3] from "d" to "e" at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Example 2:
Input: source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5]
Output: 9
Explanation: To convert "abcdefgh" to "acdeeghh", do the following operations:
- Change substring source[1..3] from "bcd" to "cde" at a cost of 1.
- Change substring source[5..7] from "fgh" to "thh" at a cost of 3. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation.
- Change substring source[5..7] from "thh" to "ghh" at a cost of 5. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation, and identical with indices picked in the second operation.
The total cost incurred is 1 + 3 + 5 = 9.
It can be shown that this is the minimum possible cost.
Example 3:
Input: source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578]
Output: -1
Explanation: It is impossible to convert "abcdefgh" to "addddddd".
If you select substring source[1..3] as the first operation to change "abcdefgh" to "adddefgh", you cannot select substring source[3..7] as the second operation because it has a common index, 3, with the first operation.
If you select substring source[3..7] as the first operation to change "abcdefgh" to "abcddddd", you cannot select substring source[1..3] as the second operation because it has a common index, 3, with the first operation.
Constraints:
1 <= source.length == target.length <= 1000
source, target consist only of lowercase English characters.
1 <= cost.length == original.length == changed.length <= 100
1 <= original[i].length == changed[i].length <= source.length
original[i], changed[i] consist only of lowercase English characters.
original[i] != changed[i]
1 <= cost[i] <= 106
"""
class Solution:
def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
| You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English characters. You are also given two 0-indexed string arrays original and changed, and an integer array cost, where cost[i] represents the cost of converting the string original[i] to the string changed[i].
You start with the string source. In one operation, you can pick a substring x from the string, and change it to y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y. You are allowed to do any number of operations, but any pair of operations must satisfy either of these two conditions:
The substrings picked in the operations are source[a..b] and source[c..d] with either b < c or d < a. In other words, the indices picked in both operations are disjoint.
The substrings picked in the operations are source[a..b] and source[c..d] with a == c and b == d. In other words, the indices picked in both operations are identical.
Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.
Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
Example 1:
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
Explanation: To convert "abcd" to "acbe", do the following operations:
- Change substring source[1..1] from "b" to "c" at a cost of 5.
- Change substring source[2..2] from "c" to "e" at a cost of 1.
- Change substring source[2..2] from "e" to "b" at a cost of 2.
- Change substring source[3..3] from "d" to "e" at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Example 2:
Input: source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5]
Output: 9
Explanation: To convert "abcdefgh" to "acdeeghh", do the following operations:
- Change substring source[1..3] from "bcd" to "cde" at a cost of 1.
- Change substring source[5..7] from "fgh" to "thh" at a cost of 3. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation.
- Change substring source[5..7] from "thh" to "ghh" at a cost of 5. We can do this operation because indices [5,7] are disjoint with indices picked in the first operation, and identical with indices picked in the second operation.
The total cost incurred is 1 + 3 + 5 = 9.
It can be shown that this is the minimum possible cost.
Example 3:
Input: source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578]
Output: -1
Explanation: It is impossible to convert "abcdefgh" to "addddddd".
If you select substring source[1..3] as the first operation to change "abcdefgh" to "adddefgh", you cannot select substring source[3..7] as the second operation because it has a common index, 3, with the first operation.
If you select substring source[3..7] as the first operation to change "abcdefgh" to "abcddddd", you cannot select substring source[1..3] as the second operation because it has a common index, 3, with the first operation.
Constraints:
1 <= source.length == target.length <= 1000
source, target consist only of lowercase English characters.
1 <= cost.length == original.length == changed.length <= 100
1 <= original[i].length == changed[i].length <= source.length
original[i], changed[i] consist only of lowercase English characters.
original[i] != changed[i]
1 <= cost[i] <= 106
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "source": "abcd", "target": "acbe", "original": ["a","b","c","c","e","d"], "changed": ["b","c","b","e","b","e"], "cost": [2,5,5,1,2,20] }
assert my_solution.minimumCost(**test_input) == 28
test_input = { "source": "abcdefgh", "target": "acdeeghh", "original": ["bcd","fgh","thh"], "changed": ["cde","thh","ghh"], "cost": [1,3,5] }
assert my_solution.minimumCost(**test_input) == 9
test_input = { "source": "abcdefgh", "target": "addddddd", "original": ["bcd","defgh"], "changed": ["ddd","ddddd"], "cost": [100,1578] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "a", "target": "b", "original": ["a"], "changed": ["b"], "cost": [1] }
assert my_solution.minimumCost(**test_input) == 1
test_input = { "source": "a", "target": "c", "original": ["a","b","a","a"], "changed": ["b","c","c","c"], "cost": [1,2,10,1] }
assert my_solution.minimumCost(**test_input) == 1
test_input = { "source": "a", "target": "d", "original": ["a"], "changed": ["b"], "cost": [1] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ajhpd", "target": "djjdc", "original": ["hpd","iyk","qzd","hpi","aic","znh","cea","fug","wir","kwu","yjo","rzi","a","n","f","q","u","w","x","i","x","s","o","u"], "changed": ["iyk","qzd","hpi","aic","znh","cea","fug","wir","kwu","yjo","rzi","jdc","n","f","q","u","w","x","i","x","s","o","u","d"], "cost": [80257,95140,96349,89449,81714,5859,96734,96109,41211,99975,57611,32644,82896,22164,99889,98061,95403,90922,64031,94558,58418,99717,96588,88286] }
assert my_solution.minimumCost(**test_input) == 1264348
test_input = { "source": "bzshh", "target": "mlosr", "original": ["shh","wbs","hup","sab","csp","tel","mhq","ezp","eap","fqb","iea","cej","b","v","g","e","d","x","q","v","g","x","u","m","u","q","z","q","n","p"], "changed": ["wbs","hup","sab","csp","tel","mhq","ezp","eap","fqb","iea","cej","osr","v","g","e","d","x","q","v","g","x","u","m","u","q","m","q","n","p","l"], "cost": [69565,82190,75322,85502,89675,98424,86521,85852,32285,99465,82356,97775,30173,88276,82158,40971,75361,65284,89814,68219,44777,95082,99781,99072,74513,49667,99719,93132,99203,54171] }
assert my_solution.minimumCost(**test_input) == 1589277
test_input = { "source": "fjybg", "target": "apyyt", "original": ["bg","xr","cc","ip","vq","po","ym","rh","vw","lf","lo","ee","qv","yr","f","w","i","u","g","a","e","f","s","r","p","j","o","g","i","u"], "changed": ["xr","cc","ip","vq","po","ym","rh","vw","lf","lo","ee","qv","yr","yt","w","i","u","g","a","e","f","s","r","p","a","o","g","i","u","p"], "cost": [97733,90086,87125,85361,75644,46301,21616,79538,52507,95884,79353,61127,58665,96031,95035,12116,41158,91096,47819,88522,25493,80186,66981,87597,56691,86820,89031,99954,41271,39699] }
assert my_solution.minimumCost(**test_input) == 1628332
test_input = { "source": "htkdz", "target": "oaqaw", "original": ["kdz","yyv","cde","oks","fzu","hkm","dmb","arh","lix","eij","ksv","t","u","f","w","b","u","v","h","o","b","o","p","z","h","w","t","p","x","y"], "changed": ["yyv","cde","oks","fzu","hkm","dmb","arh","lix","eij","ksv","qaw","u","f","w","b","u","v","h","o","b","o","p","z","a","w","t","p","x","y","o"], "cost": [90243,86765,84893,80924,85915,42672,99995,99429,88069,84925,71184,54929,83245,72750,87238,30151,58657,94445,98330,90683,83980,96513,75536,95212,79301,74556,94836,94781,76273,86147] }
assert my_solution.minimumCost(**test_input) == 1278928
test_input = { "source": "iktgh", "target": "srwcg", "original": ["h","e","y","g","q","y","t","n","r","e","i","x","iktg","xwgv","ddrp","saxt","rvdq","moiy","loln","bkgj","jjgi","vatf"], "changed": ["e","y","g","q","y","t","n","r","e","i","x","g","xwgv","ddrp","saxt","rvdq","moiy","loln","bkgj","jjgi","vatf","srwc"], "cost": [70839,75691,55903,82637,97906,86576,92197,74464,86638,61531,80041,52732,96361,39766,74988,59857,69068,89990,74293,82838,37650,26885] }
assert my_solution.minimumCost(**test_input) == 854129
test_input = { "source": "imbin", "target": "dmhjv", "original": ["bin","pwo","fwt","xwi","xal","uqt","lmp","erq","kac","dgv","qgh","rei","nbx","i","u","b","v","c","q","p","f","q","v","t","n","b"], "changed": ["pwo","fwt","xwi","xal","uqt","lmp","erq","kac","dgv","qgh","rei","nbx","hjv","u","b","v","c","q","p","f","q","v","t","n","b","d"], "cost": [47307,30907,64949,35735,84284,83424,69858,92113,51405,69242,97014,91471,78165,92733,79709,99573,78055,20529,85549,90496,60896,75354,50630,49094,41380,46980] }
assert my_solution.minimumCost(**test_input) == 1115296
test_input = { "source": "jegbx", "target": "ezhfc", "original": ["egbx","hrbf","twne","snjd","ysrf","qzqg","rcll","ekvz","inpr","frxs","xcww","unsw","vdug","ycvs","j","v","j","y","n","q","w","a","z","g","b","d"], "changed": ["hrbf","twne","snjd","ysrf","qzqg","rcll","ekvz","inpr","frxs","xcww","unsw","vdug","ycvs","zhfc","v","j","y","n","q","w","a","z","g","b","d","e"], "cost": [50682,89150,91153,85032,97960,96862,81138,86570,77628,45200,44955,70845,99254,80325,91331,95349,84374,94177,53994,94284,79531,92353,60384,100000,93152,19787] }
assert my_solution.minimumCost(**test_input) == 1868790
test_input = { "source": "jpyjj", "target": "jqnfp", "original": ["j","i","q","u","y","w","d","a","h","s","i","y","w","pyj","qng","lrn","nrm","tvn","fei","fpj","qlw","lrb","ufu","kll","nqp"], "changed": ["i","q","u","y","w","d","a","h","s","i","y","w","p","qng","lrn","nrm","tvn","fei","fpj","qlw","lrb","ufu","kll","nqp","qnf"], "cost": [62657,90954,55348,88767,87756,55487,49700,51801,94877,81661,99027,91814,62872,25235,62153,96875,12009,85321,68993,75866,72888,96411,78568,83975,60456] }
assert my_solution.minimumCost(**test_input) == 1131062
test_input = { "source": "nialx", "target": "qvqfl", "original": ["x","r","a","x","c","w","s","a","n","e","q","p","v","k","o","ial","qzu","owr","kyq","ukk","gpq","jdp","dus","eng","btu","cbp"], "changed": ["r","a","x","c","w","s","a","l","e","q","p","v","k","o","q","qzu","owr","kyq","ukk","gpq","jdp","dus","eng","btu","cbp","vqf"], "cost": [64196,95812,96987,40860,41507,99365,99208,53062,44440,65136,95625,86166,61798,84228,92555,97678,97576,19742,92989,98167,68457,82411,39923,81778,87792,7523] }
assert my_solution.minimumCost(**test_input) == 1096682
test_input = { "source": "pagpe", "target": "xacng", "original": ["gpe","owt","wyv","eba","xgp","uny","ibc","usb","mzj","wdo","lyc","eof","oci","p","e","p","u","h","w","i","l"], "changed": ["owt","wyv","eba","xgp","uny","ibc","usb","mzj","wdo","lyc","eof","oci","cng","e","p","u","h","w","i","l","x"], "cost": [56193,92982,90717,67407,91949,77752,88841,43278,51149,43646,99585,41038,84989,57688,64474,96532,77511,37031,90895,62831,87342] }
assert my_solution.minimumCost(**test_input) == 1381668
test_input = { "source": "aaabbebbbhbbbbebaaeh", "target": "hhbebebbahhhehhbbhee", "original": ["a","b","b","b","e","a","h"], "changed": ["b","e","a","h","h","h","e"], "cost": [9,8,5,9,3,7,9] }
assert my_solution.minimumCost(**test_input) == 99
test_input = { "source": "abbbeebebehbbhhhbeab", "target": "aehebehebaeaebbaahhb", "original": ["b","b","e","e","h","h","h","b","e","a"], "changed": ["e","h","b","a","e","b","a","a","h","h"], "cost": [10,2,9,10,7,8,10,10,6,9] }
assert my_solution.minimumCost(**test_input) == 118
test_input = { "source": "abebbeeeahhbahaehaab", "target": "eebhheeahaahbaebaaea", "original": ["a","b","e","a","h","a","e","b"], "changed": ["e","h","a","h","a","b","b","a"], "cost": [6,8,5,10,10,10,10,8] }
assert my_solution.minimumCost(**test_input) == 149
test_input = { "source": "aeaaebhbhehbeehbehea", "target": "babehheaaeebeebahhba", "original": ["a","e","a","e","b","h","b","h","h","e"], "changed": ["b","a","e","h","h","e","a","a","b","b"], "cost": [8,6,3,8,7,9,9,10,10,5] }
assert my_solution.minimumCost(**test_input) == 109
test_input = { "source": "aeehhhaeebhhbeabeeha", "target": "haaeaabeeeheehbaehha", "original": ["a","e","h","a","h","b","e","b"], "changed": ["h","a","a","b","e","e","h","a"], "cost": [7,9,10,7,8,9,8,8] }
assert my_solution.minimumCost(**test_input) == 117
test_input = { "source": "ahhebhhbbhbebaeehbbh", "target": "hbebaeebebhabeehahhb", "original": ["a","h","h","b","b","e","a","e","h","b"], "changed": ["h","b","e","a","e","a","e","h","a","h"], "cost": [4,8,2,8,9,9,9,8,9,6] }
assert my_solution.minimumCost(**test_input) == 116
test_input = { "source": "babhbaaabbabehhhaaea", "target": "aabhhaebehbaehahbahb", "original": ["b","b","a","a","e"], "changed": ["a","h","e","b","h"], "cost": [2,10,8,4,6] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "bbaaaebhhbehaaaabbab", "target": "aebahaaabheebbaehbbb", "original": ["b","b","a","e","h","h","a","a","b"], "changed": ["a","e","h","a","a","b","b","e","h"], "cost": [6,8,3,10,10,7,7,9,10] }
assert my_solution.minimumCost(**test_input) == 120
test_input = { "source": "bbabeehehhbhbhbbaabb", "target": "heaabheabehhahhabhhe", "original": ["b","b","b","e","h","e","h","a","a"], "changed": ["h","e","a","b","e","a","b","b","h"], "cost": [7,6,5,9,7,3,10,10,6] }
assert my_solution.minimumCost(**test_input) == 116
test_input = { "source": "bbhbahbbbabhbbbbbhaa", "target": "aheebebehaeheehhbahh", "original": ["b","b","h","b","a","a"], "changed": ["a","h","e","e","b","h"], "cost": [3,5,9,9,5,10] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "beeeaehhbbbaeaeebabh", "target": "ahehaahaaehhabaehbah", "original": ["b","e","e","h","b","a","a"], "changed": ["a","h","a","a","h","h","b"], "cost": [3,5,5,10,1,7,2] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "bhabahbhabaahbhahabb", "target": "ehbbhehbbhebaeeheebe", "original": ["a","a","h","b","h","a"], "changed": ["b","h","e","h","b","e"], "cost": [9,10,5,10,10,8] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "bhhbaaahheaebehhabeh", "target": "bbhebbeeahahhbeabeba", "original": ["h","b","h","h","e","b","e","a"], "changed": ["b","e","e","a","h","h","b","b"], "cost": [2,8,8,8,10,7,5,7] }
assert my_solution.minimumCost(**test_input) == 131
test_input = { "source": "eaabhhahhhehbabaabae", "target": "bebaehabeehehbehhahh", "original": ["a","a","b","h","h","b","a","e"], "changed": ["e","b","a","e","b","e","h","h"], "cost": [9,9,9,8,6,4,7,10] }
assert my_solution.minimumCost(**test_input) == 158
test_input = { "source": "ebbaebeheabhheeeaeaa", "target": "eehbhebhheeabehbebea", "original": ["b","a","e","e","h","h","a"], "changed": ["e","b","h","b","a","b","e"], "cost": [6,9,10,7,7,7,9] }
assert my_solution.minimumCost(**test_input) == 135
test_input = { "source": "ebbhbheeaeaeeahehahh", "target": "ehhebhahhhhheaaaaahb", "original": ["b","h","e","a","h","e","h"], "changed": ["h","e","h","h","a","a","b"], "cost": [10,7,10,8,10,3,9] }
assert my_solution.minimumCost(**test_input) == 108
test_input = { "source": "eebhehaabeaaaaheheha", "target": "abbbaeaebbhabehbabbb", "original": ["e","h","e","h","a","a","a","h"], "changed": ["b","b","a","e","h","b","e","a"], "cost": [10,10,10,8,9,6,10,10] }
assert my_solution.minimumCost(**test_input) == 139
test_input = { "source": "eeeaehbabbebhhaehaha", "target": "hehbbahabhhababeeeeh", "original": ["e","a","e","h","b","h","h","a","a"], "changed": ["h","b","b","a","h","b","e","e","h"], "cost": [3,9,4,8,9,10,6,10,6] }
assert my_solution.minimumCost(**test_input) == 120
test_input = { "source": "eeehababeeeheebeehah", "target": "hhhabbbbahhehhhbhbab", "original": ["e","h","a","e","h","b","e"], "changed": ["h","a","b","a","e","h","b"], "cost": [7,8,6,10,10,10,9] }
assert my_solution.minimumCost(**test_input) == 143
test_input = { "source": "eehhhbbhebeeehahaaae", "target": "bahaeebhbhhebbbahbhh", "original": ["e","h","b","b","e","e","h","a","h","a"], "changed": ["a","e","e","h","h","b","b","b","a","h"], "cost": [9,9,9,5,3,8,10,2,2,1] }
assert my_solution.minimumCost(**test_input) == 69
test_input = { "source": "ehaaeabaebaehbbhbhbe", "target": "baheeebehhebhbeebbbe", "original": ["e","h","a","a","e","b","b","h"], "changed": ["b","a","h","e","h","h","e","e"], "cost": [8,8,4,10,7,10,10,10] }
assert my_solution.minimumCost(**test_input) == 123
test_input = { "source": "ehaehehbeebaebaeebeb", "target": "eehhebaheaheahhhbeaa", "original": ["h","e","b","e","a","a","e","b","b"], "changed": ["e","h","h","a","e","h","b","e","a"], "cost": [9,8,8,10,10,10,3,10,9] }
assert my_solution.minimumCost(**test_input) == 162
test_input = { "source": "ehbahbbaabhbabahbbhh", "target": "ahbhbehbahaehhaaehhh", "original": ["a","b","h"], "changed": ["h","h","a"], "cost": [9,9,2] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ehbeabbhhebhbahbhbab", "target": "hhbaaebbaaabhhbehhae", "original": ["e","e","b","h","b","a","b"], "changed": ["h","a","a","b","h","h","e"], "cost": [3,8,10,7,5,10,10] }
assert my_solution.minimumCost(**test_input) == 117
test_input = { "source": "hbhheeehehbbhbbehaae", "target": "aebhbheehbbbhabbhebh", "original": ["h","b","h","e","h","e","a","a"], "changed": ["a","e","b","h","e","b","e","b"], "cost": [3,9,8,10,7,8,4,9] }
assert my_solution.minimumCost(**test_input) == 116
test_input = { "source": "hebeebhhhhabaaheabbh", "target": "aeheabaeaaeababeheae", "original": ["h","b","h","a","b","a","h","a","b"], "changed": ["a","h","e","e","a","b","b","h","e"], "cost": [10,6,8,10,10,5,10,10,2] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "hebhhaaaahbehahebaba", "target": "ahhabehbahehhahaehhh", "original": ["h","b","h","a","a","b","e","e"], "changed": ["a","h","b","e","h","e","h","a"], "cost": [6,6,10,9,7,10,10,7] }
assert my_solution.minimumCost(**test_input) == 128
test_input = { "source": "hheahabebabhehahaahe", "target": "eaahbbbaehhhahhhebhe", "original": ["h","h","e","a","a","b","a"], "changed": ["e","a","a","b","h","h","e"], "cost": [3,10,10,8,10,10,10] }
assert my_solution.minimumCost(**test_input) == 140
test_input = { "source": "hhebabehhhhbehaahbhh", "target": "ehhabeahaheaabbehhbe", "original": ["h","e","b","a","b","e","h","h","b"], "changed": ["e","h","a","b","e","a","a","b","h"], "cost": [5,3,9,10,10,6,9,5,10] }
assert my_solution.minimumCost(**test_input) == 127
test_input = { "source": "hhhbbbhhaeabhheaehea", "target": "bebhhaeheahhebbeeahb", "original": ["h","h","b","a","e","a","e","h","e","a"], "changed": ["b","e","h","e","a","h","b","a","h","b"], "cost": [10,6,8,9,6,8,10,5,8,9] }
assert my_solution.minimumCost(**test_input) == 149
test_input = { "source": "anrlqxdnlqcxqdlsceokwgrzkakyqw", "target": "fxynzghpiexaarjuaepxxpaudqipxx", "original": ["a","n","r","l","q","x","d","n","l","q","d","s","k","w","g","z","k","a","k","q"], "changed": ["f","x","y","n","z","g","h","p","i","a","r","u","x","x","p","u","d","q","i","x"], "cost": [78,19,91,27,96,16,95,100,38,99,99,94,82,75,71,100,96,88,78,89] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ayvhtgqvcputvzdksluictwibnvlxx", "target": "pssmcmkjyqniiyiiubwghwldxptuix", "original": ["a","h","t","g","v","c","t","v","z","d","k","s","l","u","i","c","w","b","v","l","x"], "changed": ["p","m","c","m","j","y","i","i","y","i","i","u","b","w","g","h","l","x","t","u","i"], "cost": [82,77,100,95,86,34,77,38,90,31,97,96,77,32,79,87,72,65,100,98,56] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "bdkgumfyqsosetnboawzhflcfkhryb", "target": "rqwqiefmafkzmataueoobbfkkxsung", "original": ["b","d","k","g","u","m","y","q","s","o","s","e","t","o","z","h","l","c","f","k","h","r","y","b"], "changed": ["r","q","w","q","i","e","m","a","f","k","z","m","a","u","o","b","f","k","k","x","s","u","n","g"], "cost": [97,97,72,71,84,96,81,53,85,81,85,48,82,47,54,79,63,94,86,66,96,39,80,82] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "dmhqhwozzfespijaadiwceabbxlfgd", "target": "owmyowokqacnhwmaellbhcnoodviac", "original": ["d","m","h","h","z","z","f","s","p","a","d","i","w","c","a","b","x","l","g","d"], "changed": ["o","w","m","o","k","q","a","n","h","e","l","l","b","h","n","o","d","v","a","c"], "cost": [100,55,84,66,97,92,86,86,98,78,71,100,89,74,77,15,59,59,87,86] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "erjbgdadefwtaydgneprfphalkagce", "target": "uzslvcynelwivxyzvhlpoxeulnguvi", "original": ["e","r","j","b","g","d","a","d","f","t","a","y","d","g","e","p","r","f","h","a","k","g","c","e"], "changed": ["u","z","s","l","v","c","y","n","l","i","v","x","y","z","h","l","p","o","e","u","n","u","v","i"], "cost": [36,100,100,98,99,90,98,93,29,80,37,98,82,84,94,97,86,97,73,96,73,92,94,57] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "gsolujonrufkcbigtjvpsgwtcaafjk", "target": "niymoyhbyicgjgcjkqkonhcgyqvhwr", "original": ["g","s","o","j","o","n","u","b","i","g","t","j","v","p","s","g","t","c","a","a","j"], "changed": ["n","i","y","y","h","b","i","g","c","j","k","q","k","o","n","h","g","y","q","v","w"], "cost": [90,91,99,100,97,95,72,56,85,55,96,77,65,21,38,18,54,91,90,99,87] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "haqgciljqjmplyboytkbvzvncembca", "target": "vxjxtxjkvyfvnkfbcfxzwjgmvflduz", "original": ["a","q","g","c","i","l","j","q","j","p","l","b","y","t","b","z","n","c","e","m","c","a"], "changed": ["x","j","x","t","x","j","k","v","y","v","n","f","c","f","z","j","m","v","f","l","u","z"], "cost": [92,99,90,100,86,49,32,98,72,80,87,87,54,56,93,19,94,81,94,98,76,94] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "jdsusoktswdtjkwuawzxxxruaybamd", "target": "ycnwpqplsmumuzggvekrmgtjxxdqwd", "original": ["j","s","u","s","k","t","d","t","j","w","u","a","w","z","x","x","r","u","y","b","m"], "changed": ["y","n","w","p","p","l","u","m","u","g","g","v","e","k","r","g","t","j","x","d","w"], "cost": [40,97,100,79,39,99,59,49,84,95,90,82,87,22,95,90,76,70,66,96,92] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "nwukaumgrqigiynrvteerkcwheiiim", "target": "nkimfbwdsbbbpccfepohlvxapvgxlg", "original": ["w","k","u","m","r","q","g","i","y","n","r","v","t","e","e","r","k","h","e","i","i","i","m"], "changed": ["k","m","b","w","s","b","b","p","c","c","f","e","p","o","h","l","v","p","v","g","x","l","g"], "cost": [63,94,50,93,100,95,99,92,100,28,98,73,80,99,73,77,84,98,64,87,98,60,85] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ovtbwsxlxgefyzlwgslddghfjyvyif", "target": "iobyykxbenmagxgftwubmkrxuvhaxq", "original": ["v","t","s","l","x","g","e","f","y","z","l","g","s","l","d","d","g","h","f","j","y","v","i","f"], "changed": ["o","b","k","b","e","n","m","a","g","x","g","t","w","u","b","m","k","r","x","u","v","h","x","q"], "cost": [95,63,97,83,95,93,81,95,100,83,63,99,97,94,45,100,38,99,18,81,39,73,92,24] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "pkluljeraiornkwyxkowpqlpeemdha", "target": "ckwzwedmdxrkbtgrhrpozwvzwijlri", "original": ["p","l","u","e","a","i","r","n","k","w","y","x","k","o","w","p","q","l","e","e","m"], "changed": ["c","w","z","d","d","x","k","b","t","g","r","h","r","p","o","z","w","v","w","i","j"], "cost": [95,88,96,84,49,86,39,68,64,84,99,96,83,46,95,84,74,64,95,83,67] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "qxzlstqdqpfgswlrztsnnclfnsaajc", "target": "hgjeamofjdcawvbdmwczbctphfupgl", "original": ["q","x","z","l","s","t","q","q","p","g","l","r","z","t","n","n","f","n","s","a","a","j","c"], "changed": ["h","g","j","e","a","m","o","j","d","a","b","d","m","w","z","b","p","h","f","u","p","g","l"], "cost": [91,10,86,97,98,76,100,96,97,59,95,97,67,93,84,64,55,81,97,69,99,81,81] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "sozjzrckkcytmscpjjhbkzsfgofzml", "target": "xjwgnxzbukkqwtocdtjeglsxkjhfsg", "original": ["s","o","z","j","z","r","k","k","s","j","j","h","b","k","z","f","g","f","z","m","l"], "changed": ["x","j","w","g","n","x","b","u","t","d","t","j","e","g","l","x","k","h","f","s","g"], "cost": [92,100,97,100,21,93,47,100,50,44,84,84,50,90,64,83,55,75,73,42,89] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "tahngidhiduqtsaimwqhjfkoyvubhx", "target": "xsglbaliykddukogzcfpchirxnzdlp", "original": ["t","a","h","n","g","i","d","h","i","d","u","q","t","s","a","i","m","w","q","h","f","k","o","v","b","h","x"], "changed": ["x","s","g","l","b","a","l","i","y","k","d","d","u","k","o","g","z","c","f","p","h","i","r","n","d","l","p"], "cost": [26,97,84,85,78,59,98,50,91,100,98,7,96,96,73,82,23,96,59,75,87,79,69,95,41,87,100] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "tqamxbehkpaapskhicnkuyyzkvjxfl", "target": "jlykiobovnobxxwnevqcluhcfmutqu", "original": ["t","q","a","m","x","b","e","h","k","p","p","s","h","i","n","k","u","y","y","z","v","x","l"], "changed": ["j","l","y","k","i","o","b","o","v","n","x","x","n","e","q","c","l","u","h","c","m","t","u"], "cost": [68,73,57,37,99,81,75,93,100,88,50,93,89,96,88,85,70,36,71,77,54,65,94] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ujdbivstmdpnnpnnpggqiwankpoyfw", "target": "grttiosznxkzeapbsjcisymwaetxcl", "original": ["u","d","b","v","t","m","p","n","n","p","n","n","p","g","q","i","w","a","n","p","o","f"], "changed": ["g","t","t","o","z","n","k","z","e","a","p","b","s","c","i","s","y","m","w","e","t","c"], "cost": [100,99,62,99,77,64,56,90,46,94,75,99,87,90,75,83,78,49,100,87,75,20] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "wqomlunjedgsdmdmcwohbxmkeqayxa", "target": "pojjjjzfsrvrhezcdbinplekiwhllb", "original": ["w","o","m","l","u","n","e","d","d","m","d","m","c","o","h","b","x","e","a","y"], "changed": ["p","j","j","j","j","z","s","r","h","e","z","c","d","i","n","p","l","i","h","l"], "cost": [91,91,51,66,89,97,95,89,72,58,98,65,99,63,58,42,89,79,52,42] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "yzflltfyomcnxhwfxcijnsxotwgcuy", "target": "pwdaiitmmlwdyhwpcrhcnncughvchb", "original": ["y","l","l","f","y","o","c","n","x","f","i","j","s","x","o","t","w","g","u","y"], "changed": ["p","a","i","t","m","m","w","d","y","p","h","c","n","c","u","g","h","v","h","b"], "cost": [84,87,90,88,64,80,95,59,96,76,100,93,72,50,97,79,60,90,72,67] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "zchjedhjmkrzqkqnywdgxakcdmsdwg", "target": "cbftcpgezcgjguefrieolevyaadkgb", "original": ["z","c","h","j","e","m","k","r","z","q","k","q","n","y","w","d","x","k","c","m","s","d","w","g"], "changed": ["c","b","f","t","c","z","c","g","j","g","u","e","f","r","i","e","l","v","y","a","d","k","g","b"], "cost": [57,94,71,85,53,96,97,43,93,65,76,93,94,100,81,85,62,100,34,99,95,85,77,68] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "zpxcysgujcbbcgbqqofookukkotwij", "target": "dfxsvtmexhdnfhbdrcdgqlajddteph", "original": ["z","s","g","u","j","c","g","q","o","f","k","u","k","o","w","j"], "changed": ["d","t","m","e","x","f","h","r","c","d","l","a","j","d","e","h"], "cost": [75,28,71,70,92,88,82,82,80,91,97,93,77,14,68,75] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "bavusatavvvuubavsauavubtusubsvtsvsbttbvs", "target": "ssauttbvssatusutusbattuttsutabubutuasvuu", "original": ["b","a","v","s","t","a","v","a","v","s","u","u","b","s","v","t","s","b","t","b"], "changed": ["s","s","a","t","b","v","s","u","t","u","b","t","t","a","b","u","b","u","a","v"], "cost": [948,467,690,969,300,877,924,924,791,724,809,652,388,592,772,829,912,679,751,529] }
assert my_solution.minimumCost(**test_input) == 27579
test_input = { "source": "bbvstvuatvbasbbsvsuvvuvvauutttvtsuavsvua", "target": "buvbtutbsbuttbtvabbuvubvbvabsbattutvbvvu", "original": ["b","s","v","u","a","t","v","a","s","u","u","u","t","v","a"], "changed": ["u","b","u","t","b","s","b","t","v","b","v","a","b","a","u"], "cost": [795,956,694,238,665,894,519,867,887,715,845,16,942,429,282] }
assert my_solution.minimumCost(**test_input) == 22249
test_input = { "source": "busbsusauusbbasssutaauttavbbabtbustvubtv", "target": "vuabutaustubbbsbabbusavsttbtubavsabuvvbt", "original": ["b","s","u","s","a","u","s","t","a","u","t","t","v","b","v"], "changed": ["v","u","t","a","u","s","b","b","s","a","v","s","t","t","u"], "cost": [927,992,291,999,989,996,318,196,155,948,375,845,960,949,250] }
assert my_solution.minimumCost(**test_input) == 24291
test_input = { "source": "buuasstsvvvvtsaavstbvubtbstbussbavsvvvbt", "target": "utsabasvssauvuvavvbstbbaauvvvtvtbastatua", "original": ["b","u","u","s","s","v","v","t","b","u","b","s","t","b","u","b","a","v"], "changed": ["u","t","s","b","v","s","a","b","s","b","a","u","v","v","v","t","b","t"], "cost": [935,827,806,951,298,554,896,852,759,853,530,891,942,944,464,952,882,887] }
assert my_solution.minimumCost(**test_input) == 31617
test_input = { "source": "bvbsstasaabvubbaabvbsbsttuvvuutsabbaubau", "target": "sbuutvuttaubsbbsaatbvsbtsvutuutvtabaubat", "original": ["b","b","s","t","a","u","a","v","s","s","v"], "changed": ["s","u","t","v","u","s","s","t","v","b","u"], "cost": [985,604,940,913,910,765,729,905,848,793,468] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "satuaabaavaauussvsvssavtuuvbttassbtususv", "target": "ababsttvsabvuvaatuvtsvtsasbaatavuauvvstv", "original": ["s","u","a","a","a","v","a","u","s","s","v","t","u","u","v","b","t","s","t"], "changed": ["a","b","s","t","v","a","b","v","u","t","t","s","a","s","b","a","a","v","u"], "cost": [848,908,555,510,759,404,799,963,855,901,609,854,878,915,879,594,748,937,942] }
assert my_solution.minimumCost(**test_input) == 27052
test_input = { "source": "sauaavvasvsatabbvuusvatsabssavvtsbvuusva", "target": "abtuuvutubsbbtuttbtubbasuaustsbssatubsvb", "original": ["s","a","a","a","v","t","b","b","v","u","u","s","t","b","v","t"], "changed": ["a","b","u","t","b","b","u","t","t","b","t","u","a","a","s","s"], "cost": [689,325,535,931,528,722,965,786,645,499,791,717,557,988,229,834] }
assert my_solution.minimumCost(**test_input) == 21007
test_input = { "source": "ssautbbbuattvtutbavabtbvbusbsbaavutvbutu", "target": "svbvuttvtbtvuavbvtstutuvutvbasbauvabsvvv", "original": ["s","a","b","u","t","v","t","u","b","a","b","v","b"], "changed": ["v","b","t","t","v","u","a","v","v","t","u","b","s"], "cost": [943,915,778,641,540,872,999,856,979,848,856,935,677] }
assert my_solution.minimumCost(**test_input) == 33036
test_input = { "source": "sstsvvvvvabubtvsvaatsavssbatstbbabbtbvvt", "target": "tattabsubsatbtbbbussususassautbabsbvabvu", "original": ["s","s","v","v","v","v","a","b","s","a","t","s","t","a","b","t","t"], "changed": ["t","a","a","b","s","u","s","a","b","u","s","u","a","b","s","v","u"], "cost": [723,861,682,949,830,969,880,892,750,461,870,592,233,718,967,621,472] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ssuuavtuuutubutvtssbasssvttuvuubbabubvsu", "target": "ustbvvsutaasabtasabuaasuatuaabatubsbtvbt", "original": ["s","u","u","a","t","u","b","s","b","t","u","v","b","a","b","s"], "changed": ["u","t","b","v","a","s","a","a","u","u","a","a","t","b","s","b"], "cost": [709,599,774,993,858,730,987,700,596,200,984,567,656,971,777,790] }
assert my_solution.minimumCost(**test_input) == 23824
test_input = { "source": "suvsavbasssavasvvbttubvbvatbstbabsutvuua", "target": "aaavasusvsatsuvuatbsstbvstuatttuuvubavau", "original": ["s","v","b","a","s","a","t","t","u","b","v","b","b","s","a","u","u"], "changed": ["v","s","u","s","a","t","b","s","s","t","b","v","a","t","u","v","a"], "cost": [944,923,845,911,686,687,403,705,378,929,315,296,484,666,354,375,649] }
assert my_solution.minimumCost(**test_input) == 25662
test_input = { "source": "tavauavbtsuuubtbuaatsbutvastasaavbtbsubs", "target": "stsautbvvtbuvvtstaaubavvuaabubavsvbasvvb", "original": ["t","a","v","v","s","u","b","b","u","t","s","b","u","t","v","s","t","a"], "changed": ["s","t","s","b","t","b","v","s","t","u","b","a","v","v","u","a","b","u"], "cost": [936,867,711,886,565,650,500,694,394,662,948,878,864,993,925,508,714,815] }
assert my_solution.minimumCost(**test_input) == 24476
test_input = { "source": "tbsttbstvtvusvbaavuuuvvuavuuusbusabsubta", "target": "uvvbttvbvtbtausvtavsuavusvusuabuvtutttas", "original": ["t","b","s","u","v","a","a","v","u","u","a","s","b","s","b","t"], "changed": ["u","v","v","t","u","v","t","a","v","s","s","a","u","t","t","a"], "cost": [409,241,815,861,536,968,983,726,882,674,981,516,918,653,368,845] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "tsauvattvvavaasbutvuavvvttuvasavbvavtsaa", "target": "vbtsubbbbbavvvbsvstavasaaaatsattvbtubsvs", "original": ["t","s","u","a","t","v","a","b","u","v","u","v","v","t","a","a","b"], "changed": ["v","b","s","b","b","b","v","s","v","t","a","a","s","a","s","t","v"], "cost": [731,965,614,651,952,991,940,606,664,261,468,295,202,675,921,628,690] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "tsvsstututatsbvatauatsausaubsuuassssbuts", "target": "assutuavuvusbubbtavsavbvusvbvtbubvvuvtvb", "original": ["t","v","s","s","t","u","t","a","s","b","v","a","s","u","a","u","u","b"], "changed": ["a","s","u","t","u","a","v","u","b","u","b","b","v","v","s","t","b","v"], "cost": [924,489,946,903,425,474,965,605,168,870,992,903,367,995,425,580,918,991] }
assert my_solution.minimumCost(**test_input) == 23945
test_input = { "source": "ttuvauavausttvusavuvsuababtubbbvvsttbasv", "target": "butttussvtvbbtbtasuusstattuvvvuavbtsbttu", "original": ["t","t","u","a","a","u","s","v","v","u","a","b","b","u","b","b","v","s","t"], "changed": ["b","u","t","s","v","b","t","s","u","s","t","a","t","v","v","u","a","b","s"], "cost": [790,396,583,83,931,950,760,364,698,503,957,984,611,688,1000,986,679,913,906] }
assert my_solution.minimumCost(**test_input) == 25663
test_input = { "source": "tuasbutsuubbvauvautsbtabsaaauubbvaavtuua", "target": "tvauvutvbbavuatsbaatvtbavatavtsvttttuvta", "original": ["u","s","b","u","v","u","v","a","u","t","b","s","a","b","v","t"], "changed": ["v","u","v","b","u","t","s","b","a","a","a","v","t","s","t","u"], "cost": [958,880,986,571,311,294,673,433,733,984,444,576,666,847,516,866] }
assert my_solution.minimumCost(**test_input) == 21164
test_input = { "source": "tvsbbttvsvubbtasvbusataaatautauavvstsbub", "target": "tbttvaavutvbstvutavavtbuvtuabsubsuvbtusa", "original": ["v","b","s","u","b","v","s","a","a","a","u","t","v","v","s","s","u","b"], "changed": ["b","v","u","v","s","t","a","v","b","u","a","b","s","u","v","t","s","a"], "cost": [977,323,878,825,625,908,794,831,973,881,946,981,437,904,564,826,720,583] }
assert my_solution.minimumCost(**test_input) == 29347
test_input = { "source": "uatavuaavatstabvtvabbbuvatvabbauvsvtauuu", "target": "ustbbuvbtusutuutvsuttbuubuuubasvubuubusb", "original": ["a","a","v","a","v","a","t","b","t","b","v","t","s","u","u"], "changed": ["s","b","b","v","t","u","s","u","v","t","u","u","b","s","b"], "cost": [681,801,894,221,732,732,984,989,750,705,181,993,888,957,912] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ubvtusbavavtbssuutvsvasttbauvtvttsvtttss", "target": "usasbvasuttsbvvuuvvtubssuuuuvuvubtsbsstu", "original": ["b","t","a","v","a","s","s","t","b","a","t","v","s"], "changed": ["s","s","s","u","t","v","t","u","u","u","b","s","u"], "cost": [933,864,772,686,918,885,923,892,538,120,913,892,441] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "ubvuasvuvuavasvvasausabbtausaaubbvbtuubv", "target": "sbbutubbvtvauatbavstvbttavbubvsvsutuavta", "original": ["u","v","a","s","u","a","v","a","s","s","a","a","b","b","u","u"], "changed": ["s","b","t","u","b","v","a","u","a","v","s","b","t","s","a","v"], "cost": [951,655,564,597,402,910,850,664,352,937,698,281,997,798,368,963] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "utauutasstbuubsussstaauatvaavuuabvvbvaba", "target": "vttavbbbvbbbvuaavauubbsavaststtatsbssaab", "original": ["a","u","t","a","s","s","s","t","v","a","u","b","v","b","v","b"], "changed": ["t","a","b","b","b","a","v","v","a","s","t","t","b","s","s","a"], "cost": [862,888,579,525,903,443,828,892,953,879,855,705,961,438,478,998] }
assert my_solution.minimumCost(**test_input) == -1
test_input = { "source": "uttbbsvttssubvbbaabsavaabvsvbbbbbatvbtsu", "target": "tatsstaubbtbvsabvasbubvvbutasssvttuvvtvs", "original": ["u","t","b","s","v","t","t","u","b","v","b","a","s","v","v","b","a","s","u"], "changed": ["t","a","s","t","a","u","b","b","v","s","a","v","b","b","u","t","t","v","s"], "cost": [880,822,798,549,420,189,730,621,865,189,950,921,887,816,772,69,907,868,715] }
assert my_solution.minimumCost(**test_input) == 23971
test_input = { "source": "utvsausubbtuasvtuttatbtasuvbtuuubsatabau", "target": "tstsatsbsttaatasbvvsabbabbtabtvbvuaavaau", "original": ["u","b","b","u","s","v","u","t","a","t","s","b","u","b","s","a"], "changed": ["t","s","t","a","t","a","b","v","s","a","b","a","v","v","u","v"], "cost": [425,837,948,892,923,403,619,690,775,951,577,1000,816,999,602,800] }
assert my_solution.minimumCost(**test_input) == 29865
test_input = { "source": "uvtavsbvvvvavsstavustttuuvvavatattbavttv", "target": "vvbuuutbtvbtutatuttvsssvtvsbbvbbvuausavu", "original": ["t","a","v","s","b","v","v","a","s","s","u","s","t","u","v","a","t","t","b"], "changed": ["b","u","u","u","t","b","t","t","t","a","t","v","s","v","s","b","v","u","a"], "cost": [984,969,379,449,842,818,399,954,718,823,932,935,405,591,643,965,1000,700,189] }
assert my_solution.minimumCost(**test_input) == 27084
test_input = { "source": "vabsuaavabatbastaauaaaututtutsvuusatavas", "target": "svubbsaavaubbbvasatssvvubtsssabsabsbstau", "original": ["a","s","u","a","v","b","a","s","t","u","u","t","u","t","s","v","u","t","v","s"], "changed": ["v","b","b","s","a","a","u","v","a","t","v","u","s","s","a","b","a","b","t","u"], "cost": [948,531,696,662,908,460,829,859,857,374,1000,996,356,810,804,751,988,815,933,924] }
assert my_solution.minimumCost(**test_input) == 27967
test_input = { "source": "vsvstbbutastuuaubuausuuavvttvabvsaavbtvs", "target": "vbbtbuatasbauavbbabbbvbutaavsvtubvbuttst", "original": ["s","v","s","t","b","b","u","a","t","u","a","u","a","u","a","v","v","b","v"], "changed": ["b","b","t","b","u","a","t","s","a","a","v","b","b","v","u","t","s","t","u"], "cost": [510,990,975,894,921,775,819,665,76,509,943,691,757,617,773,404,777,932,837] }
assert my_solution.minimumCost(**test_input) == 25040
test_input = { "source": "vusbbaubbusbbtusttbtavsavbbbausavbausbub", "target": "uvvbsututvavautuatuussbbttbaaaasbbuvavaa", "original": ["u","b","a","u","b","b","s","b","b","t","t","s","a","v","u","a","v"], "changed": ["v","s","u","t","u","t","a","v","a","u","a","b","b","t","a","s","b"], "cost": [555,920,950,949,936,35,455,910,746,791,661,864,926,11,833,870,854] }
assert my_solution.minimumCost(**test_input) == 26439
test_input = { "source": "vvabsutautvsutvavtsvstabvbvtutbvuautaatb", "target": "taasvsstbstasbtuauvtbtusbssbbssbuusuvbbu", "original": ["v","b","s","u","t","u","v","t","a","t","s","v","v","a"], "changed": ["a","s","v","s","s","b","t","b","u","u","b","s","b","b"], "cost": [813,860,971,768,419,950,866,741,989,756,325,806,507,571] }
assert my_solution.minimumCost(**test_input) == 36258
test_input = { "source": "vvvsaavtavaabtssasstbvuusbubbbsastvttbba", "target": "utssutvbabttvstsbbusttuuutvvbtasvuvbvvav", "original": ["v","a","a","v","b","t","s","a","s","b","s","u","s","a","s","t","t","b","a"], "changed": ["t","u","t","b","v","s","t","b","b","t","u","v","a","s","v","b","v","a","v"], "cost": [722,823,981,465,482,999,572,191,410,968,377,549,658,692,898,263,928,197,831] }
assert my_solution.minimumCost(**test_input) == 22953
test_input = { "source": "asmlmoumomvummakmlbabvmvvavlavtsvbvssuumsllttsusts", "target": "asmlmoumomvummakmlbabvmvvaolklllvbsltuomkslvmmusts", "original": ["vlavtsvbvssuumslltts","mtmubkasuvumkobbmsmo","vsbbvauauvuvsauaastl","uovumoluksslvkvlkmam","smsvsuubusskmublvvst","momuatbkubosmmavvssk","mommltotttbtvlvalsbt","vbbbuvslutblvvkvtmmo","boaosasttvtvtabtubab","mbtkumblvbasoobaauvm","vbotklaoltambktlulot","vluamsokkbaalsmmalav","mttmbuosbmlttabmbabl","sskvkbmlabaulluomovt","lstbuomkaoatmavsmvml"], "changed": ["mtmubkasuvumkobbmsmo","vsbbvauauvuvsauaastl","uovumoluksslvkvlkmam","smsvsuubusskmublvvst","momuatbkubosmmavvssk","mommltotttbtvlvalsbt","vbbbuvslutblvvkvtmmo","boaosasttvtvtabtubab","mbtkumblvbasoobaauvm","vbotklaoltambktlulot","vluamsokkbaalsmmalav","mttmbuosbmlttabmbabl","sskvkbmlabaulluomovt","lstbuomkaoatmavsmvml","olklllvbsltuomkslvmm"], "cost": [9758,7133,9355,8885,6055,7360,9168,9288,7422,6995,8167,6154,6939,6343,9733] }
assert my_solution.minimumCost(**test_input) == 118755
test_input = { "source": "bkmltoaakmatkvllubamuvbmkolamvolsaottsokbmutktsvlo", "target": "skmltoaakmatkvllubamuvbmkolamvolsosuataatovmktsvlo", "original": ["aottsokbmut","ktuumtblakk","mkumbambakt","tubtvmuuoat","kkkksosllks","sastauoammb","sbomolbklsk","kaabakosmsb","ltaltkmukoa","lmlaovmluta","kusalltssaa","mattouslbou","obaavmovsal","bk","ko"], "changed": ["ktuumtblakk","mkumbambakt","tubtvmuuoat","kkkksosllks","sastauoammb","sbomolbklsk","kaabakosmsb","ltaltkmukoa","lmlaovmluta","kusalltssaa","mattouslbou","obaavmovsal","osuataatovm","ko","sk"], "cost": [5819,9018,7484,6655,5163,5728,3077,7032,4630,8093,6974,5623,9179,7307,9974] }
assert my_solution.minimumCost(**test_input) == 101756
test_input = { "source": "blvalvmkosattusaubkbuvusmoolmkloavaskmkbovkkbvtaas", "target": "blvalvmkosattlooutkvsmabuvaumkloavaskmkbovkkbvtaas", "original": ["usaubkbuvusmoolm","stvkuuablkvlvbuv","laosbokmbsusulta","tosusvtstuousmtv","tkbbalmtoubtmlvk","vauvllvbootbvtsv","malabvmoaavulomv","ootsoksuosvlakov","alaobmvbttmtobvl","oauamtksvbuovmbt","ubtlssmbbaloatsa","sstskkmtkoobaavt","avsskubbbtossbsu","aumbsbautvkmsauu","btsuvmosbtomvmma"], "changed": ["stvkuuablkvlvbuv","laosbokmbsusulta","tosusvtstuousmtv","tkbbalmtoubtmlvk","vauvllvbootbvtsv","malabvmoaavulomv","ootsoksuosvlakov","alaobmvbttmtobvl","oauamtksvbuovmbt","ubtlssmbbaloatsa","sstskkmtkoobaavt","avsskubbbtossbsu","aumbsbautvkmsauu","btsuvmosbtomvmma","looutkvsmabuvaum"], "cost": [8903,5338,8835,8645,8789,7933,8044,3865,7564,5782,9245,9165,8886,7691,8039] }
assert my_solution.minimumCost(**test_input) == 116724
test_input = { "source": "mobbmmmsabbomsbukkotbttvsuoubtvuabaktsuoltvamlltbv", "target": "mobbmmmsauotksusvmvtbmaovtsvtaklabaktsuoltbamtumtl", "original": ["bbomsbukkotbttvsuoubtvua","sbuastublskotvtotmokuota","kssvtltakbtmlbmtoskaousb","vuososkmtvsobkbvuvbklvbv","vtsvllsklslkbulusbuastlm","bttokabvovvktkavatskoamt","lbmobulvomkovaalbtkoukso","tmmlusaokamvstsmuksmbulu","lkktooukbstvkumvbbsllaas","mbvtkltbvuumuvobstooammv","skovmvassobkbutolttvkokb","olastbtotmlusbmlukmokubl","kvbomsvuaskvkvvmssavubtt","buusuklobuoukatusulotmks","vamlltbv"], "changed": ["sbuastublskotvtotmokuota","kssvtltakbtmlbmtoskaousb","vuososkmtvsobkbvuvbklvbv","vtsvllsklslkbulusbuastlm","bttokabvovvktkavatskoamt","lbmobulvomkovaalbtkoukso","tmmlusaokamvstsmuksmbulu","lkktooukbstvkumvbbsllaas","mbvtkltbvuumuvobstooammv","skovmvassobkbutolttvkokb","olastbtotmlusbmlukmokubl","kvbomsvuaskvkvvmssavubtt","buusuklobuoukatusulotmks","uotksusvmvtbmaovtsvtakla","bamtumtl"], "cost": [8432,9912,7958,9938,8402,7223,5772,9501,8749,8597,6195,7504,7103,9582,8898] }
assert my_solution.minimumCost(**test_input) == 123766 | 1,703,385,000 |
biweekly-contest-120-count-the-number-of-incremovable-subarrays-i | https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i | count-the-number-of-incremovable-subarrays-i | {
"questionId": "3252",
"questionFrontendId": "2970",
"title": "Count the Number of Incremovable Subarrays I",
"titleSlug": "count-the-number-of-incremovable-subarrays-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 38,
"dislikes": 49,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of positive integers nums.
A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
Return the total number of incremovable subarrays of nums.
Note that an empty array is considered strictly increasing.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,4]
Output: 10
Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
Example 2:
Input: nums = [6,5,7,8]
Output: 7
Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.
Example 3:
Input: nums = [8,7,6,6]
Output: 3
Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 50
"""
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
| You are given a 0-indexed array of positive integers nums.
A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
Return the total number of incremovable subarrays of nums.
Note that an empty array is considered strictly increasing.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,4]
Output: 10
Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
Example 2:
Input: nums = [6,5,7,8]
Output: 7
Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.
Example 3:
Input: nums = [8,7,6,6]
Output: 3
Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 10
test_input = { "nums": [6,5,7,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [8,7,6,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [1] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [1,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [1,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [1,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [2,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [3,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [3,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [3,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [4,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [4,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [4,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,1] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [6,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [6,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [6,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [9,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [9,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [9,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [9,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [10,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [10,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [1,2,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [1,2,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [2,1,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [2,4,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [2,6,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [2,6,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [2,10,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [3,1,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [3,5,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [3,7,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [3,8,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [3,10,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [4,5,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [5,8,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [5,9,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [5,9,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [6,7,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [8,7,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [8,7,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [8,9,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [9,2,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [9,5,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [9,6,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [9,9,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [10,7,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [10,10,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [2,1,1,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [2,5,7,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 10
test_input = { "nums": [3,5,3,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [3,7,10,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [3,8,3,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [4,1,3,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [4,3,5,1] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [4,3,7,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [4,8,7,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [4,9,10,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [5,4,3,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [5,5,9,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,10,10,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [6,4,4,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [6,5,2,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [7,3,2,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [7,5,1,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [7,9,7,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [7,9,8,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [7,10,4,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [8,8,1,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [9,2,8,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [10,5,9,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [10,7,2,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [10,9,1,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [1,1,4,4,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [1,2,3,4,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 15
test_input = { "nums": [1,2,8,9,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [1,7,4,9,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [2,2,4,6,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [2,7,1,3,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 9
test_input = { "nums": [3,1,9,6,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [3,3,3,6,1] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [3,7,1,8,1] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [4,1,6,10,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,4,8,4,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [6,1,1,8,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7 | 1,703,341,800 |
biweekly-contest-120-find-polygon-with-the-largest-perimeter | https://leetcode.com/problems/find-polygon-with-the-largest-perimeter | find-polygon-with-the-largest-perimeter | {
"questionId": "3262",
"questionFrontendId": "2971",
"title": "Find Polygon With the Largest Perimeter",
"titleSlug": "find-polygon-with-the-largest-perimeter",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 53,
"dislikes": 6,
"categoryTitle": "Algorithms"
} | """
You are given an array of positive integers nums of length n.
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.
Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak.
The perimeter of a polygon is the sum of lengths of its sides.
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.
Example 1:
Input: nums = [5,5,5]
Output: 15
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
Example 2:
Input: nums = [1,12,1,2,5,50,3]
Output: 12
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
It can be shown that the largest possible perimeter is 12.
Example 3:
Input: nums = [5,5,50]
Output: -1
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
Constraints:
3 <= n <= 105
1 <= nums[i] <= 109
"""
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
| You are given an array of positive integers nums of length n.
A polygon is a closed plane figure that has at least 3 sides. The longest side of a polygon is smaller than the sum of its other sides.
Conversely, if you have k (k >= 3) positive real numbers a1, a2, a3, ..., ak where a1 <= a2 <= a3 <= ... <= ak and a1 + a2 + a3 + ... + ak-1 > ak, then there always exists a polygon with k sides whose lengths are a1, a2, a3, ..., ak.
The perimeter of a polygon is the sum of lengths of its sides.
Return the largest possible perimeter of a polygon whose sides can be formed from nums, or -1 if it is not possible to create a polygon.
Example 1:
Input: nums = [5,5,5]
Output: 15
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
Example 2:
Input: nums = [1,12,1,2,5,50,3]
Output: 12
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
It can be shown that the largest possible perimeter is 12.
Example 3:
Input: nums = [5,5,50]
Output: -1
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
Constraints:
3 <= n <= 105
1 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [5,5,5] }
assert my_solution.largestPerimeter(**test_input) == 15
test_input = { "nums": [1,12,1,2,5,50,3] }
assert my_solution.largestPerimeter(**test_input) == 12
test_input = { "nums": [5,5,50] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,1,1] }
assert my_solution.largestPerimeter(**test_input) == 3
test_input = { "nums": [1,1,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,1,3] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,1,4] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,1,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,2,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,2,2] }
assert my_solution.largestPerimeter(**test_input) == 5
test_input = { "nums": [1,2,3] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,2,4] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,2,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,3,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,3,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,3,3] }
assert my_solution.largestPerimeter(**test_input) == 7
test_input = { "nums": [1,3,4] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,3,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,4,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,4,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,4,3] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,4,4] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [1,4,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,5,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,5,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,5,3] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,5,4] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [1,5,5] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [2,1,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,1,2] }
assert my_solution.largestPerimeter(**test_input) == 5
test_input = { "nums": [2,1,3] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,1,4] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,1,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,2,1] }
assert my_solution.largestPerimeter(**test_input) == 5
test_input = { "nums": [2,2,2] }
assert my_solution.largestPerimeter(**test_input) == 6
test_input = { "nums": [2,2,3] }
assert my_solution.largestPerimeter(**test_input) == 7
test_input = { "nums": [2,2,4] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,2,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,3,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,3,2] }
assert my_solution.largestPerimeter(**test_input) == 7
test_input = { "nums": [2,3,3] }
assert my_solution.largestPerimeter(**test_input) == 8
test_input = { "nums": [2,3,4] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [2,3,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,4,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,4,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,4,3] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [2,4,4] }
assert my_solution.largestPerimeter(**test_input) == 10
test_input = { "nums": [2,4,5] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [2,5,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,5,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,5,3] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [2,5,4] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [2,5,5] }
assert my_solution.largestPerimeter(**test_input) == 12
test_input = { "nums": [3,1,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,1,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,1,3] }
assert my_solution.largestPerimeter(**test_input) == 7
test_input = { "nums": [3,1,4] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,1,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,2,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,2,2] }
assert my_solution.largestPerimeter(**test_input) == 7
test_input = { "nums": [3,2,3] }
assert my_solution.largestPerimeter(**test_input) == 8
test_input = { "nums": [3,2,4] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [3,2,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,3,1] }
assert my_solution.largestPerimeter(**test_input) == 7
test_input = { "nums": [3,3,2] }
assert my_solution.largestPerimeter(**test_input) == 8
test_input = { "nums": [3,3,3] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [3,3,4] }
assert my_solution.largestPerimeter(**test_input) == 10
test_input = { "nums": [3,3,5] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [3,4,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,4,2] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [3,4,3] }
assert my_solution.largestPerimeter(**test_input) == 10
test_input = { "nums": [3,4,4] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [3,4,5] }
assert my_solution.largestPerimeter(**test_input) == 12
test_input = { "nums": [3,5,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,5,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [3,5,3] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [3,5,4] }
assert my_solution.largestPerimeter(**test_input) == 12
test_input = { "nums": [3,5,5] }
assert my_solution.largestPerimeter(**test_input) == 13
test_input = { "nums": [4,1,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,1,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,1,3] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,1,4] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [4,1,5] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,2,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,2,2] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,2,3] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [4,2,4] }
assert my_solution.largestPerimeter(**test_input) == 10
test_input = { "nums": [4,2,5] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [4,3,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,3,2] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [4,3,3] }
assert my_solution.largestPerimeter(**test_input) == 10
test_input = { "nums": [4,3,4] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [4,3,5] }
assert my_solution.largestPerimeter(**test_input) == 12
test_input = { "nums": [4,4,1] }
assert my_solution.largestPerimeter(**test_input) == 9
test_input = { "nums": [4,4,2] }
assert my_solution.largestPerimeter(**test_input) == 10
test_input = { "nums": [4,4,3] }
assert my_solution.largestPerimeter(**test_input) == 11
test_input = { "nums": [4,4,4] }
assert my_solution.largestPerimeter(**test_input) == 12
test_input = { "nums": [4,4,5] }
assert my_solution.largestPerimeter(**test_input) == 13
test_input = { "nums": [4,5,1] }
assert my_solution.largestPerimeter(**test_input) == -1
test_input = { "nums": [4,5,2] }
assert my_solution.largestPerimeter(**test_input) == 11 | 1,703,341,800 |
biweekly-contest-120-count-the-number-of-incremovable-subarrays-ii | https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-ii | count-the-number-of-incremovable-subarrays-ii | {
"questionId": "3248",
"questionFrontendId": "2972",
"title": "Count the Number of Incremovable Subarrays II",
"titleSlug": "count-the-number-of-incremovable-subarrays-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 97,
"dislikes": 15,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of positive integers nums.
A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
Return the total number of incremovable subarrays of nums.
Note that an empty array is considered strictly increasing.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,4]
Output: 10
Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
Example 2:
Input: nums = [6,5,7,8]
Output: 7
Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.
Example 3:
Input: nums = [8,7,6,6]
Output: 3
Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
"""
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
| You are given a 0-indexed array of positive integers nums.
A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
Return the total number of incremovable subarrays of nums.
Note that an empty array is considered strictly increasing.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,4]
Output: 10
Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
Example 2:
Input: nums = [6,5,7,8]
Output: 7
Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
It can be shown that there are only 7 incremovable subarrays in nums.
Example 3:
Input: nums = [8,7,6,6]
Output: 3
Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 10
test_input = { "nums": [6,5,7,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [8,7,6,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [1] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 1
test_input = { "nums": [4,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [7,3] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [8,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [8,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [9,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [5,5,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [6,7,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [8,1,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [9,2,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [9,3,8] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [1,2,10,6] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [1,9,6,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [4,8,5,9] }
assert my_solution.incremovableSubarrayCount(**test_input) == 8
test_input = { "nums": [6,2,6,4] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [10,2,5,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [1,2,4,7,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 15
test_input = { "nums": [1,5,9,5,7] }
assert my_solution.incremovableSubarrayCount(**test_input) == 9
test_input = { "nums": [6,6,5,3,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [8,5,5,3,10] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [9,10,1,8,2] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [11,50,14,33,45,38,33,19,28,34] }
assert my_solution.incremovableSubarrayCount(**test_input) == 9
test_input = { "nums": [25,9,28,31,38,15,31,44,46,49] }
assert my_solution.incremovableSubarrayCount(**test_input) == 11
test_input = { "nums": [25,26,49,31,40,47,30,29,32,34] }
assert my_solution.incremovableSubarrayCount(**test_input) == 13
test_input = { "nums": [25,39,29,30,40,28,30,39,30,42] }
assert my_solution.incremovableSubarrayCount(**test_input) == 8
test_input = { "nums": [25,41,31,38,30,38,37,41,36,32] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [25,45,49,28,47,44,42,34,28,25] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [25,46,48,41,29,47,32,34,41,34] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [25,47,25,35,48,49,27,37,36,43] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [26,26,50,38,30,38,31,26,39,45] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [26,32,41,38,45,32,31,27,48,41] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [26,45,44,26,33,35,34,36,44,38] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [26,49,42,26,37,41,31,36,45,41] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [27,39,32,30,38,41,28,26,49,49] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [27,43,36,37,33,46,48,35,49,49] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [28,17,12,21,21,49,31,30,40,13] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [28,30,39,31,33,41,47,36,43,46] }
assert my_solution.incremovableSubarrayCount(**test_input) == 15
test_input = { "nums": [29,27,32,38,26,38,39,30,41,45] }
assert my_solution.incremovableSubarrayCount(**test_input) == 8
test_input = { "nums": [29,31,48,28,27,38,32,28,30,44] }
assert my_solution.incremovableSubarrayCount(**test_input) == 10
test_input = { "nums": [29,34,44,27,45,31,37,32,50,26] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [29,38,36,42,31,38,27,48,42,28] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [29,46,34,47,46,41,29,29,38,39] }
assert my_solution.incremovableSubarrayCount(**test_input) == 8
test_input = { "nums": [29,49,32,35,38,37,27,25,50,46] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [30,27,38,33,28,48,41,30,25,50] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [30,29,31,44,31,25,50,35,35,47] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [30,30,46,35,31,41,30,37,37,33] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [30,41,28,45,35,41,47,32,29,33] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [30,45,44,27,43,40,28,34,39,40] }
assert my_solution.incremovableSubarrayCount(**test_input) == 10
test_input = { "nums": [30,49,34,26,50,50,48,49,39,26] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [31,34,25,43,38,34,29,50,27,34] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [31,35,39,38,41,47,26,43,47,46] }
assert my_solution.incremovableSubarrayCount(**test_input) == 8
test_input = { "nums": [31,37,29,41,32,46,25,28,30,29] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [31,38,31,47,25,25,36,29,43,28] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [31,41,25,37,43,29,30,26,30,46] }
assert my_solution.incremovableSubarrayCount(**test_input) == 8
test_input = { "nums": [31,42,40,36,39,28,43,29,35,50] }
assert my_solution.incremovableSubarrayCount(**test_input) == 9
test_input = { "nums": [32,29,13,39,34,47,38,15,10,5] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [32,35,32,50,32,26,29,49,40,41] }
assert my_solution.incremovableSubarrayCount(**test_input) == 9
test_input = { "nums": [32,37,35,26,45,44,47,29,31,28] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [32,43,44,44,36,41,31,33,41,43] }
assert my_solution.incremovableSubarrayCount(**test_input) == 11
test_input = { "nums": [32,50,50,46,32,30,32,32,31,39] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [33,27,34,46,42,35,36,49,25,40] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [33,28,35,32,36,38,33,47,36,35] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [33,31,36,38,39,46,42,41,27,33] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [33,45,25,48,45,42,35,38,47,43] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [33,46,38,37,42,48,31,43,38,29] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [33,48,50,48,46,33,34,26,32,33] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [34,25,38,41,31,46,40,46,39,30] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [34,38,35,28,30,37,35,25,48,28] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [34,47,26,48,30,25,26,43,44,41] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [35,22,35,6,20,47,3,29,45,30] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [35,25,29,40,32,29,35,39,39,32] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [35,26,39,41,26,44,36,26,46,50] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [35,27,29,45,29,30,48,42,37,50] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [35,31,27,45,39,46,47,49,26,27] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [35,39,36,30,32,48,34,25,37,45] }
assert my_solution.incremovableSubarrayCount(**test_input) == 9
test_input = { "nums": [36,26,44,32,36,29,44,28,48,30] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [36,28,28,45,40,40,32,48,34,48] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [36,28,34,49,48,36,50,25,43,40] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [36,30,47,32,32,35,41,49,41,45] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [36,31,45,34,47,48,49,31,34,34] }
assert my_solution.incremovableSubarrayCount(**test_input) == 3
test_input = { "nums": [37,33,50,48,25,37,29,49,46,45] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [37,35,29,27,39,38,49,48,27,37] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4
test_input = { "nums": [37,40,42,41,30,40,46,44,47,27] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [37,43,49,40,30,46,31,44,47,25] }
assert my_solution.incremovableSubarrayCount(**test_input) == 5
test_input = { "nums": [37,45,49,26,32,45,33,40,35,43] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [37,50,42,50,40,26,34,25,28,44] }
assert my_solution.incremovableSubarrayCount(**test_input) == 7
test_input = { "nums": [38,28,25,31,28,44,35,26,33,41] }
assert my_solution.incremovableSubarrayCount(**test_input) == 6
test_input = { "nums": [38,30,36,50,36,40,41,25,43,43] }
assert my_solution.incremovableSubarrayCount(**test_input) == 4 | 1,703,341,800 |
biweekly-contest-120-find-number-of-coins-to-place-in-tree-nodes | https://leetcode.com/problems/find-number-of-coins-to-place-in-tree-nodes | find-number-of-coins-to-place-in-tree-nodes | {
"questionId": "3218",
"questionFrontendId": "2973",
"title": "Find Number of Coins to Place in Tree Nodes",
"titleSlug": "find-number-of-coins-to-place-in-tree-nodes",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 67,
"dislikes": 2,
"categoryTitle": "Algorithms"
} | """
You are given an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given 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 cost of length n, where cost[i] is the cost assigned to the ith node.
You need to place some coins on every node of the tree. The number of coins to be placed at node i can be calculated as:
If size of the subtree of node i is less than 3, place 1 coin.
Otherwise, place an amount of coins equal to the maximum product of cost values assigned to 3 distinct nodes in the subtree of node i. If this product is negative, place 0 coins.
Return an array coin of size n such that coin[i] is the number of coins placed at node i.
Example 1:
Input: edges = [[0,1],[0,2],[0,3],[0,4],[0,5]], cost = [1,2,3,4,5,6]
Output: [120,1,1,1,1,1]
Explanation: For node 0 place 6 * 5 * 4 = 120 coins. All other nodes are leaves with subtree of size 1, place 1 coin on each of them.
Example 2:
Input: edges = [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], cost = [1,4,2,3,5,7,8,-4,2]
Output: [280,140,32,1,1,1,1,1,1]
Explanation: The coins placed on each node are:
- Place 8 * 7 * 5 = 280 coins on node 0.
- Place 7 * 5 * 4 = 140 coins on node 1.
- Place 8 * 2 * 2 = 32 coins on node 2.
- All other nodes are leaves with subtree of size 1, place 1 coin on each of them.
Example 3:
Input: edges = [[0,1],[0,2]], cost = [1,2,-2]
Output: [0,1,1]
Explanation: Node 1 and 2 are leaves with subtree of size 1, place 1 coin on each of them. For node 0 the only possible product of cost is 2 * 1 * -2 = -4. Hence place 0 coins on node 0.
Constraints:
2 <= n <= 2 * 104
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
cost.length == n
1 <= |cost[i]| <= 104
The input is generated such that edges represents a valid tree.
"""
class Solution:
def placedCoins(self, edges: List[List[int]], cost: List[int]) -> List[int]:
| You are given an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given 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 cost of length n, where cost[i] is the cost assigned to the ith node.
You need to place some coins on every node of the tree. The number of coins to be placed at node i can be calculated as:
If size of the subtree of node i is less than 3, place 1 coin.
Otherwise, place an amount of coins equal to the maximum product of cost values assigned to 3 distinct nodes in the subtree of node i. If this product is negative, place 0 coins.
Return an array coin of size n such that coin[i] is the number of coins placed at node i.
Example 1:
Input: edges = [[0,1],[0,2],[0,3],[0,4],[0,5]], cost = [1,2,3,4,5,6]
Output: [120,1,1,1,1,1]
Explanation: For node 0 place 6 * 5 * 4 = 120 coins. All other nodes are leaves with subtree of size 1, place 1 coin on each of them.
Example 2:
Input: edges = [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], cost = [1,4,2,3,5,7,8,-4,2]
Output: [280,140,32,1,1,1,1,1,1]
Explanation: The coins placed on each node are:
- Place 8 * 7 * 5 = 280 coins on node 0.
- Place 7 * 5 * 4 = 140 coins on node 1.
- Place 8 * 2 * 2 = 32 coins on node 2.
- All other nodes are leaves with subtree of size 1, place 1 coin on each of them.
Example 3:
Input: edges = [[0,1],[0,2]], cost = [1,2,-2]
Output: [0,1,1]
Explanation: Node 1 and 2 are leaves with subtree of size 1, place 1 coin on each of them. For node 0 the only possible product of cost is 2 * 1 * -2 = -4. Hence place 0 coins on node 0.
Constraints:
2 <= n <= 2 * 104
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
cost.length == n
1 <= |cost[i]| <= 104
The input is generated such that edges represents a valid tree.
Please complete the code below to solve above prblem:
```python
class Solution:
def placedCoins(self, edges: List[List[int]], cost: List[int]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "edges": [[0,1],[0,2],[0,3],[0,4],[0,5]], "cost": [1,2,3,4,5,6] }
assert my_solution.placedCoins(**test_input) == [120,1,1,1,1,1]
test_input = { "edges": [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], "cost": [1,4,2,3,5,7,8,-4,2] }
assert my_solution.placedCoins(**test_input) == [280,140,32,1,1,1,1,1,1]
test_input = { "edges": [[0,1],[0,2]], "cost": [1,2,-2] }
assert my_solution.placedCoins(**test_input) == [0,1,1]
test_input = { "edges": [[0,1]], "cost": [1,2] }
assert my_solution.placedCoins(**test_input) == [1,1]
test_input = { "edges": [[0,1]], "cost": [2,1] }
assert my_solution.placedCoins(**test_input) == [1,1]
test_input = { "edges": [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[0,21],[0,22],[0,23],[0,24],[0,25],[0,26],[0,27],[0,28],[0,29],[0,30],[0,31],[0,32],[0,33],[0,34],[0,35],[0,36],[0,37],[0,38],[0,39],[0,40],[0,41],[0,42],[0,43],[0,44],[0,45],[0,46],[0,47],[0,48],[0,49],[0,50],[0,51],[0,52],[0,53],[0,54],[0,55],[0,56],[0,57],[0,58],[0,59],[0,60],[0,61],[0,62],[0,63],[0,64],[0,65],[0,66],[0,67],[0,68],[0,69],[0,70],[0,71],[0,72],[0,73],[0,74],[0,75],[0,76],[0,77],[0,78],[0,79],[0,80],[0,81],[0,82],[0,83],[0,84],[0,85],[0,86],[0,87],[0,88],[0,89],[0,90],[0,91],[0,92],[0,93],[0,94],[0,95],[0,96],[0,97],[0,98],[0,99]], "cost": [-5959,602,-6457,7055,-1462,6347,7226,-8422,-6088,2997,-7909,6433,5217,3294,-3792,7463,8538,-3811,5009,151,5659,4458,-1702,-1877,2799,9861,-9668,-1765,2181,-8128,7046,9529,6202,-8026,6464,1345,121,1922,7274,-1227,-9914,3025,1046,-9368,-7368,6205,-6342,8091,-6732,-7620,3276,5136,6871,4823,-1885,-4005,-3974,-2725,-3845,-8508,7201,-9566,-7236,-3386,4021,6793,-8759,5066,5879,-5171,1011,1242,8536,-8405,-9646,-214,2251,-9934,-8820,6206,1006,1318,-9712,7230,5608,-4601,9185,346,3056,8913,-2454,-3445,-4295,4802,-8852,-6121,-4538,-5580,-9246,-6462] }
assert my_solution.placedCoins(**test_input) == [971167251036,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
test_input = { "edges": [[0,1],[0,2],[2,3]], "cost": [10000,-10000,10000,-10000] }
assert my_solution.placedCoins(**test_input) == [1000000000000,1,1,1]
test_input = { "edges": [[0,2],[0,6],[1,4],[3,5],[7,6],[3,6],[1,8],[3,1],[9,3]], "cost": [63,13,-6,20,56,-14,61,25,-99,54] }
assert my_solution.placedCoins(**test_input) == [215208,0,1,77616,1,1,184464,1,1,1]
test_input = { "edges": [[7,0],[4,3],[4,8],[1,5],[6,2],[2,7],[7,9],[1,8],[1,9]], "cost": [37,-48,30,-67,-84,36,-96,24,29,38] }
assert my_solution.placedCoins(**test_input) == [306432,202608,1,1,1,1,1,306432,163212,213864]
test_input = { "edges": [[0,2],[2,7],[3,4],[5,4],[5,8],[7,6],[7,1],[8,1],[1,9]], "cost": [-18,15,-82,-85,63,-89,60,63,20,18] }
assert my_solution.placedCoins(**test_input) == [476595,476595,476595,1,1,476595,1,476595,476595,1]
test_input = { "edges": [[2,0],[1,4],[3,8],[4,9],[6,8],[7,2],[2,8],[5,2],[5,9]], "cost": [-9,46,17,34,43,92,41,-50,4,76] }
assert my_solution.placedCoins(**test_input) == [321632,1,321632,1,1,321632,1,1,5576,150328]
test_input = { "edges": [[0,6],[3,2],[3,1],[6,1],[8,7],[7,1],[5,1],[5,4],[9,4]], "cost": [86,50,10,-54,-85,-65,54,-19,39,37] }
assert my_solution.placedCoins(**test_input) == [475150,276250,1,1,1,204425,298350,1,1,1]
test_input = { "edges": [[0,8],[8,1],[9,2],[4,6],[7,4],[3,7],[3,8],[5,8],[5,9]], "cost": [-4,83,-97,40,86,-85,-6,-84,-16,-53] }
assert my_solution.placedCoins(**test_input) == [709070,1,1,43344,1,0,1,43344,709070,1]
test_input = { "edges": [[4,2],[1,3],[4,5],[7,5],[5,0],[8,1],[0,1],[0,6],[9,6]], "cost": [-72,-18,-27,38,13,-53,43,-95,-100,-77] }
assert my_solution.placedCoins(**test_input) == [408500,68400,1,1,1,65455,1,1,1,1]
test_input = { "edges": [[0,2],[5,2],[1,5],[7,4],[4,8],[1,8],[1,6],[6,3],[9,3]], "cost": [6,93,59,-14,1,-71,-87,95,16,-12] }
assert my_solution.placedCoins(**test_input) == [586815,141360,586815,1,1,586815,0,1,1520,1]
test_input = { "edges": [[0,1],[5,4],[4,2],[7,2],[7,3],[8,6],[3,6],[3,1],[9,1]], "cost": [66,-54,74,18,-77,-57,49,-82,-71,80] }
assert my_solution.placedCoins(**test_input) == [505120,505120,324786,467236,1,1,1,467236,1,1]
test_input = { "edges": [[3,8],[2,4],[5,6],[6,7],[0,8],[2,0],[2,6],[1,6],[9,1]], "cost": [92,-71,-10,-70,-56,-47,69,51,100,65] }
assert my_solution.placedCoins(**test_input) == [634800,1,274344,1,1,1,230253,1,1,1]
test_input = { "edges": [[0,3],[2,8],[5,6],[9,5],[7,9],[8,1],[1,4],[4,3],[9,3]], "cost": [-71,66,86,99,50,-29,-30,27,16,-65] }
assert my_solution.placedCoins(**test_input) == [561924,90816,1,561924,283800,1,1,1,1,52650]
test_input = { "edges": [[1,0],[2,7],[6,3],[0,4],[6,5],[5,8],[0,7],[9,0],[9,8]], "cost": [40,8,43,31,-27,-21,-21,55,-36,75] }
assert my_solution.placedCoins(**test_input) == [177375,1,1,1,1,13671,1,1,23436,56700]
test_input = { "edges": [[9,0],[9,3],[4,5],[1,6],[1,5],[8,5],[9,7],[8,2],[2,9]], "cost": [64,35,-1,-28,-50,38,-77,-13,-72,35] }
assert my_solution.placedCoins(**test_input) == [354816,1,210672,1,1,146300,1,1,210672,210672]
test_input = { "edges": [[5,3],[4,2],[5,2],[6,5],[5,1],[0,1],[0,8],[9,7],[9,8]], "cost": [-100,44,-76,55,8,-8,38,26,-41,-83] }
assert my_solution.placedCoins(**test_input) == [456500,91960,1,1,1,33440,1,1,88478,1]
test_input = { "edges": [[0,8],[9,3],[4,5],[8,5],[2,6],[7,8],[2,8],[1,2],[9,1]], "cost": [36,33,52,-24,7,-82,63,85,21,-64] }
assert my_solution.placedCoins(**test_input) == [446080,50688,108108,1,1,1,1,1,446080,1]
test_input = { "edges": [[9,2],[1,3],[8,4],[1,5],[1,9],[8,6],[7,9],[8,0],[0,9]], "cost": [-67,-82,-2,32,-97,6,-85,14,8,1] }
assert my_solution.placedCoins(**test_input) == [263840,0,1,1,1,1,1,1,65960,5248]
test_input = { "edges": [[7,0],[3,1],[7,3],[4,8],[8,5],[7,6],[2,6],[2,9],[8,9]], "cost": [-48,-69,-25,-65,65,51,85,34,17,-28] }
assert my_solution.placedCoins(**test_input) == [381225,1,56355,1,1,1,281775,381225,56355,56355]
test_input = { "edges": [[4,2],[7,2],[3,5],[6,3],[3,0],[7,0],[1,8],[1,0],[9,0]], "cost": [-14,39,40,-76,-69,66,43,82,-66,-45] }
assert my_solution.placedCoins(**test_input) == [430008,1,1,0,1,1,1,0,1,1]
test_input = { "edges": [[1,9],[3,7],[0,6],[7,0],[8,5],[2,5],[4,2],[4,0],[0,9]], "cost": [34,-87,-34,87,58,76,5,43,14,-45] }
assert my_solution.placedCoins(**test_input) == [383496,1,0,1,61712,1,1,1,1,1]
test_input = { "edges": [[7,1],[2,7],[4,3],[9,5],[6,7],[3,6],[8,3],[0,3],[0,9]], "cost": [-9,67,10,-67,91,29,-4,-35,60,-84] }
assert my_solution.placedCoins(**test_input) == [512148,1,1,365820,1,1,9380,0,1,1]
test_input = { "edges": [[2,3],[3,6],[3,7],[8,5],[1,5],[4,1],[4,0],[0,3],[9,3]], "cost": [88,-73,8,-82,64,-14,56,-82,-2,-89] }
assert my_solution.placedCoins(**test_input) == [642224,0,1,408688,65408,1,1,1,1,1]
test_input = { "edges": [[8,4],[7,3],[3,0],[0,6],[6,8],[8,2],[5,2],[1,5],[1,9]], "cost": [-50,34,51,-69,-28,72,61,-76,-30,76] }
assert my_solution.placedCoins(**test_input) == [398544,1,279072,1,1,186048,333792,1,279072,1]
test_input = { "edges": [[5,0],[1,6],[7,2],[4,9],[3,5],[3,7],[8,7],[6,7],[6,9]], "cost": [-75,-39,-30,-69,84,-7,98,92,-42,-51] }
assert my_solution.placedCoins(**test_input) == [757344,1,1,757344,1,757344,194922,757344,1,1]
test_input = { "edges": [[0,8],[2,5],[3,5],[1,6],[4,1],[8,4],[7,8],[8,5],[9,5]], "cost": [81,-76,-61,1,39,-3,-21,-33,42,-78] }
assert my_solution.placedCoins(**test_input) == [480168,1,1,1,62244,4758,1,1,248976,1]
test_input = { "edges": [[7,1],[0,2],[0,3],[6,5],[6,0],[0,7],[7,4],[9,4],[8,9]], "cost": [-37,-11,71,-57,-2,-78,87,55,-21,-66] }
assert my_solution.placedCoins(**test_input) == [447876,1,1,1,0,1,1,76230,1,1]
test_input = { "edges": [[0,9],[8,2],[3,7],[6,4],[1,5],[8,1],[6,8],[6,7],[7,9]], "cost": [61,-53,-97,-86,-91,-32,-8,79,100,75] }
assert my_solution.placedCoins(**test_input) == [882700,1,1,1,1,1,882700,882700,514100,882700]
test_input = { "edges": [[8,3],[4,6],[2,5],[2,1],[6,8],[8,0],[0,7],[1,7],[1,9]], "cost": [-40,-4,60,-47,35,39,-8,-12,-29,-43] }
assert my_solution.placedCoins(**test_input) == [121260,10320,1,1,1,1,1,30960,47705,1]
test_input = { "edges": [[3,0],[1,7],[6,3],[7,5],[4,5],[4,6],[6,9],[2,8],[9,2]], "cost": [36,84,5,32,-36,86,-35,58,36,-100] }
assert my_solution.placedCoins(**test_input) == [418992,1,1,418992,418992,418992,418992,1,1,0]
test_input = { "edges": [[1,0],[3,0],[2,5],[3,5],[6,4],[4,9],[7,3],[8,3],[3,9]], "cost": [39,22,-9,-65,9,-53,83,-94,-34,12] }
assert my_solution.placedCoins(**test_input) == [507130,1,1,507130,1,1,1,1,1,8964]
test_input = { "edges": [[2,0],[7,0],[4,3],[5,8],[7,9],[8,1],[3,1],[3,6],[9,6]], "cost": [-68,1,-68,65,-45,-26,36,-3,-85,40] }
assert my_solution.placedCoins(**test_input) == [375700,2210,1,248625,1,1,248625,248625,1,248625]
test_input = { "edges": [[4,0],[7,2],[2,6],[8,6],[5,6],[4,5],[4,3],[1,3],[1,9]], "cost": [-45,26,-26,-34,-33,81,-33,55,-87,52] }
assert my_solution.placedCoins(**test_input) == [317115,1,1,0,239598,232551,157905,1,1,1]
test_input = { "edges": [[2,1],[0,1],[6,3],[5,4],[6,5],[0,6],[9,0],[8,7],[9,7]], "cost": [94,76,-92,61,27,78,-94,39,-12,77] }
assert my_solution.placedCoins(**test_input) == [812912,1,1,1,1,1,128466,1,1,0]
test_input = { "edges": [[8,0],[3,2],[6,2],[1,2],[1,5],[4,7],[9,4],[8,5],[9,5]], "cost": [-6,-89,62,-82,-78,97,-71,58,-43,12] }
assert my_solution.placedCoins(**test_input) == [707906,452476,360964,1,1,707906,1,1,707906,0]
test_input = { "edges": [[1,0],[2,0],[4,0],[4,3],[4,6],[6,5],[7,5],[7,8],[9,8]], "cost": [-35,4,-1,-44,-27,-93,10,55,-14,93] }
assert my_solution.placedCoins(**test_input) == [380556,1,1,1,380556,121086,121086,0,1,1]
test_input = { "edges": [[0,6],[1,5],[8,2],[8,3],[9,6],[4,7],[5,8],[5,4],[4,9]], "cost": [26,22,32,-27,54,44,-58,-88,79,-77] }
assert my_solution.placedCoins(**test_input) == [535304,1,1,1,187704,111232,535304,1,0,535304]
test_input = { "edges": [[7,2],[3,1],[5,1],[8,5],[6,9],[7,0],[0,4],[4,8],[9,4]], "cost": [-70,-80,17,23,19,-71,84,-52,-21,-44] }
assert my_solution.placedCoins(**test_input) == [477120,1,1,1,477120,130640,1,1,130640,1]
test_input = { "edges": [[2,1],[3,6],[0,6],[7,2],[5,2],[5,4],[4,0],[0,8],[9,0]], "cost": [-27,11,-5,22,-81,48,-28,-85,85,-44] }
assert my_solution.placedCoins(**test_input) == [585225,1,4675,1,330480,20400,1,1,1,1]
test_input = { "edges": [[8,1],[2,5],[4,3],[8,5],[5,6],[6,3],[0,3],[7,0],[9,7]], "cost": [79,-73,75,-96,61,87,-74,69,84,41] }
assert my_solution.placedCoins(**test_input) == [618048,1,1,618048,1,548100,548100,1,1,1]
test_input = { "edges": [[1,0],[2,0],[3,0],[4,0],[0,7],[8,0],[8,6],[5,6],[5,9]], "cost": [-97,-61,-67,60,-75,-85,-21,75,-89,59] }
assert my_solution.placedCoins(**test_input) == [647475,1,1,1,1,1,105315,1,446335,1]
test_input = { "edges": [[0,1],[0,4],[9,3],[9,4],[5,2],[2,7],[6,8],[8,7],[8,9]], "cost": [-88,-7,-16,-49,-53,46,-19,38,-2,-12] }
assert my_solution.placedCoins(**test_input) == [214544,1,1,1,119462,1,1,0,13984,42826]
test_input = { "edges": [[2,0],[4,6],[4,5],[1,5],[3,1],[3,2],[2,7],[8,7],[7,9]], "cost": [-71,-13,56,3,95,74,79,81,-50,-24] }
assert my_solution.placedCoins(**test_input) == [607905,555370,607905,555370,1,555370,1,97200,1,1]
test_input = { "edges": [[3,2],[7,3],[4,8],[1,5],[6,7],[0,7],[8,1],[0,1],[9,0]], "cost": [25,-2,22,-40,-53,-17,-97,-49,29,36] }
assert my_solution.placedCoins(**test_input) == [185076,26129,1,1,1,1,1,104566,1,1]
test_input = { "edges": [[1,8],[2,6],[7,2],[7,3],[8,3],[8,4],[5,4],[5,0],[9,0]], "cost": [-54,-16,51,-79,73,-83,-54,5,45,14] }
assert my_solution.placedCoins(**test_input) == [478661,1,1,217566,311418,478661,1,0,217566,1]
test_input = { "edges": [[3,1],[4,0],[1,5],[6,1],[6,0],[9,7],[0,8],[0,2],[2,9]], "cost": [23,-50,-5,-36,-49,49,39,-82,-8,81] }
assert my_solution.placedCoins(**test_input) == [332100,88200,33210,1,1,1,88200,1,1,1]
test_input = { "edges": [[0,7],[8,1],[5,7],[8,2],[2,3],[3,7],[4,7],[4,6],[6,9]], "cost": [-12,-83,-3,-43,12,48,-46,19,-92,69] }
assert my_solution.placedCoins(**test_input) == [526884,1,0,0,0,1,1,526884,1,1]
test_input = { "edges": [[0,1],[2,5],[7,2],[7,6],[1,8],[1,3],[3,7],[4,7],[9,4]], "cost": [56,61,-17,-3,-100,-28,81,42,1,-86] }
assert my_solution.placedCoins(**test_input) == [696600,696600,1,696600,1,1,1,696600,1,1]
test_input = { "edges": [[0,3],[8,1],[3,6],[5,3],[2,8],[4,2],[4,7],[7,5],[5,9]], "cost": [-42,72,54,-46,57,95,94,21,-19,-92] }
assert my_solution.placedCoins(**test_input) == [642960,1,0,642960,221616,389880,1,221616,1,1]
test_input = { "edges": [[2,0],[2,4],[6,5],[8,6],[3,7],[3,1],[1,8],[2,1],[2,9]], "cost": [-58,-82,-70,33,20,-40,21,-93,18,-6] }
assert my_solution.placedCoins(**test_input) == [251658,251658,251658,1,1,1,1,1,0,1]
test_input = { "edges": [[1,5],[3,0],[0,2],[2,4],[5,2],[7,2],[6,7],[8,7],[7,9]], "cost": [-96,-98,41,59,-69,-51,-78,43,-40,-8] }
assert my_solution.placedCoins(**test_input) == [555072,1,328692,1,1,1,1,134160,1,1]
test_input = { "edges": [[0,4],[1,4],[3,6],[5,7],[4,6],[7,2],[4,2],[4,9],[9,8]], "cost": [-98,-100,-37,62,38,-54,56,56,1,-72] }
assert my_solution.placedCoins(**test_input) == [607600,1,111888,1,446400,1,1,1,1,1]
test_input = { "edges": [[8,0],[4,3],[6,3],[5,7],[6,8],[8,2],[1,2],[7,1],[9,7]], "cost": [-70,-59,-87,-64,56,-15,-62,-48,-58,-85] }
assert my_solution.placedCoins(**test_input) == [414120,0,0,1,1,1,222208,0,414120,1]
test_input = { "edges": [[3,0],[1,8],[6,2],[5,3],[4,5],[8,4],[7,8],[8,6],[6,9]], "cost": [-56,-14,-44,-2,31,34,-61,53,-39,-21] }
assert my_solution.placedCoins(**test_input) == [181048,1,1,142252,142252,142252,0,1,142252,1]
test_input = { "edges": [[2,1],[1,3],[5,8],[9,6],[8,1],[1,4],[4,0],[7,0],[7,9]], "cost": [-18,-10,25,-60,-48,4,14,38,26,16] }
assert my_solution.placedCoins(**test_input) == [109440,15600,1,1,74880,1,1,8512,1,1]
test_input = { "edges": [[0,2],[2,5],[3,4],[5,3],[6,8],[7,1],[5,1],[5,9],[8,9]], "cost": [46,96,34,76,19,29,-36,48,-71,-45] }
assert my_solution.placedCoins(**test_input) == [350208,1,350208,1,1,350208,1,1,1,0]
test_input = { "edges": [[0,2],[7,2],[3,4],[4,7],[5,1],[6,1],[7,6],[9,6],[8,9]], "cost": [4,70,65,-34,-59,-70,-83,-21,66,-10] }
assert my_solution.placedCoins(**test_input) == [406700,1,406700,1,1,1,406700,406700,1,1]
test_input = { "edges": [[0,1],[2,3],[5,2],[9,2],[6,9],[9,7],[1,8],[4,1],[9,4]], "cost": [54,72,-52,45,-62,96,-54,28,-76,86] }
assert my_solution.placedCoins(**test_input) == [594432,594432,0,1,371520,1,1,1,1,371520]
test_input = { "edges": [[1,0],[0,4],[3,5],[6,7],[6,3],[3,8],[8,0],[0,2],[9,2]], "cost": [-98,-85,82,-30,64,-76,36,-54,84,85] }
assert my_solution.placedCoins(**test_input) == [708050,1,1,147744,1,1,1,1,344736,1]
test_input = { "edges": [[0,8],[2,6],[4,7],[5,1],[1,9],[3,7],[3,8],[6,8],[9,6]], "cost": [60,-76,-76,38,-5,-33,-80,-36,28,63] }
assert my_solution.placedCoins(**test_input) == [383040,1,1,6840,1,1,383040,1,383040,158004]
test_input = { "edges": [[5,3],[6,5],[7,6],[6,2],[2,1],[0,1],[0,9],[4,8],[4,9]], "cost": [-56,-92,-10,-70,52,22,43,37,88,48] }
assert my_solution.placedCoins(**test_input) == [566720,276920,35002,1,1,1,35002,1,1,219648]
test_input = { "edges": [[8,2],[9,4],[1,7],[3,1],[3,5],[5,8],[5,0],[0,6],[9,6]], "cost": [-70,17,-31,41,-93,17,-19,21,-66,-29] }
assert my_solution.placedCoins(**test_input) == [266910,1,1,14637,1,83886,0,1,1,1]
test_input = { "edges": [[8,1],[2,4],[7,3],[2,3],[2,6],[6,5],[0,5],[0,8],[9,8]], "cost": [88,86,55,-61,3,-70,12,44,-92,-72] }
assert my_solution.placedCoins(**test_input) == [582912,1,7260,1,1,234850,29040,1,569664,1]
test_input = { "edges": [[0,7],[1,3],[1,6],[4,2],[6,2],[5,6],[5,9],[7,8],[9,8]], "cost": [15,78,-48,58,-27,28,60,-9,-64,-71] }
assert my_solution.placedCoins(**test_input) == [354432,1,1,1,1,271440,271440,354432,354432,271440]
test_input = { "edges": [[4,0],[8,0],[5,6],[8,6],[6,1],[7,1],[3,7],[2,3],[9,2]], "cost": [14,10,10,-73,-43,19,92,62,16,-27] }
assert my_solution.placedCoins(**test_input) == [288788,122202,1,19710,1,1,181332,122202,181332,1]
test_input = { "edges": [[8,0],[3,1],[1,2],[8,5],[6,7],[4,7],[9,4],[2,8],[2,9]], "cost": [97,35,-74,5,65,5,86,61,-55,53] }
assert my_solution.placedCoins(**test_input) == [542230,1,340990,1,340990,1,1,1,350020,340990]
test_input = { "edges": [[0,1],[7,0],[8,2],[3,4],[4,7],[4,8],[4,6],[5,6],[9,5]], "cost": [-74,40,73,-97,-62,9,-96,-98,-38,63] }
assert my_solution.placedCoins(**test_input) == [693938,1,1,1,679776,1,0,693938,1,1]
test_input = { "edges": [[2,3],[0,3],[0,6],[6,5],[7,1],[1,5],[8,5],[4,5],[9,4]], "cost": [4,-30,59,61,78,-22,-24,85,-19,-89] }
assert my_solution.placedCoins(**test_input) == [404430,1,1,1,1,226950,226950,1,1,1]
test_input = { "edges": [[1,2],[5,4],[5,6],[7,1],[3,1],[3,5],[5,8],[0,5],[0,9]], "cost": [11,-80,95,64,-76,56,61,22,13,-58] }
assert my_solution.placedCoins(**test_input) == [577600,0,1,133760,1,577600,1,1,1,1]
test_input = { "edges": [[1,9],[2,7],[3,5],[4,3],[0,4],[0,6],[8,0],[7,0],[9,7]], "cost": [-69,18,-39,-59,-48,-65,97,1,74,-63] }
assert my_solution.placedCoins(**test_input) == [435045,1,1,1,0,1,1,44226,1,1]
test_input = { "edges": [[0,6],[1,4],[5,4],[6,8],[3,8],[5,3],[5,2],[7,2],[9,7]], "cost": [-84,27,16,75,49,4,72,46,-17,48] }
assert my_solution.placedCoins(**test_input) == [264600,1,35328,176400,1,108192,264600,1,176400,1]
test_input = { "edges": [[0,9],[1,7],[6,3],[8,7],[7,3],[5,3],[2,5],[2,4],[4,9]], "cost": [1,-53,88,-67,-55,-31,-89,-39,21,-96] }
assert my_solution.placedCoins(**test_input) == [751872,1,524744,125223,524744,125223,1,43407,1,751872]
test_input = { "edges": [[3,6],[0,6],[8,0],[7,4],[4,1],[2,1],[2,9],[8,5],[9,5]], "cost": [78,-10,-51,-50,-55,-72,-7,31,-94,4] }
assert my_solution.placedCoins(**test_input) == [527904,17050,86955,1,1,122760,1,1,209808,86955]
test_input = { "edges": [[2,5],[4,7],[4,5],[5,3],[3,6],[6,0],[1,0],[8,1],[9,8]], "cost": [-19,93,-23,-86,54,-70,-70,9,69,13] }
assert my_solution.placedCoins(**test_input) == [559860,83421,1,325080,1,86940,325080,1,1,1]
test_input = { "edges": [[2,7],[3,9],[7,5],[4,5],[4,1],[6,1],[6,0],[8,0],[0,9]], "cost": [45,-53,-16,-26,99,50,33,-57,-97,74] }
assert my_solution.placedCoins(**test_input) == [547371,299079,1,1,90288,45600,299079,1,1,1]
test_input = { "edges": [[5,0],[9,1],[3,6],[2,4],[2,6],[6,9],[8,7],[7,5],[5,9]], "cost": [-14,-4,-49,52,-45,77,-17,-79,21,-33] }
assert my_solution.placedCoins(**test_input) == [298067,1,1,1,1,298067,114660,1,1,114660]
test_input = { "edges": [[6,3],[7,0],[2,0],[2,4],[9,4],[1,8],[1,6],[5,6],[9,5]], "cost": [-58,-55,17,68,37,-32,91,-63,79,69] }
assert my_solution.placedCoins(**test_input) == [496041,1,496041,1,496041,488852,488852,1,1,496041]
test_input = { "edges": [[1,8],[0,3],[2,4],[0,2],[8,5],[0,5],[7,0],[7,6],[9,6]], "cost": [-8,-99,36,31,94,5,-35,54,33,19] }
assert my_solution.placedCoins(**test_input) == [325710,1,1,1,1,0,1,0,1,1]
test_input = { "edges": [[1,0],[1,7],[3,4],[4,5],[5,6],[7,5],[5,2],[8,2],[2,9]], "cost": [70,-75,-13,30,-87,20,-67,76,20,-30] }
assert my_solution.placedCoins(**test_input) == [495900,495900,7800,1,1,174870,1,443004,1,1]
test_input = { "edges": [[5,1],[2,1],[0,2],[6,0],[0,3],[3,7],[9,7],[4,8],[9,4]], "cost": [-10,-31,-7,-77,64,-80,-53,37,1,10] }
assert my_solution.placedCoins(**test_input) == [394240,1,0,23680,1,1,1,23680,1,640]
test_input = { "edges": [[2,0],[1,2],[2,8],[4,3],[4,7],[5,9],[6,9],[8,7],[7,9]], "cost": [-80,77,-18,-72,11,66,82,80,32,61] }
assert my_solution.placedCoins(**test_input) == [505120,1,505120,1,1,1,1,432960,432960,330132]
test_input = { "edges": [[0,3],[1,0],[1,8],[5,4],[9,6],[7,4],[9,4],[8,2],[9,2]], "cost": [70,-14,-91,98,-12,30,-24,79,-62,11] }
assert my_solution.placedCoins(**test_input) == [552916,445718,172536,1,0,1,1,1,445718,26070]
test_input = { "edges": [[5,8],[2,6],[2,0],[0,3],[7,8],[8,4],[3,4],[1,3],[1,9]], "cost": [55,69,29,87,27,-35,-83,71,-82,-8] }
assert my_solution.placedCoins(**test_input) == [592122,1,1,426213,203770,1,1,1,203770,1]
test_input = { "edges": [[1,9],[5,4],[5,3],[6,3],[6,2],[2,8],[0,7],[0,9],[8,9]], "cost": [-25,28,47,-75,-78,-39,23,93,-20,61] }
assert my_solution.placedCoins(**test_input) == [544050,1,274950,0,1,1,134550,1,274950,356850]
test_input = { "edges": [[5,0],[1,8],[6,4],[4,9],[7,3],[8,5],[3,5],[3,2],[9,2]], "cost": [-11,-91,-54,53,58,16,-60,85,20,51] }
assert my_solution.placedCoins(**test_input) == [464100,1,187920,275400,1,464100,1,1,1,0]
test_input = { "edges": [[1,3],[1,4],[2,4],[6,5],[7,5],[0,5],[9,0],[8,2],[9,2]], "cost": [-74,26,99,58,42,-55,-1,-56,29,-35] }
assert my_solution.placedCoins(**test_input) == [410256,1,241164,1,63336,0,1,1,1,241164]
test_input = { "edges": [[2,5],[5,3],[0,4],[5,9],[6,1],[7,1],[7,0],[0,9],[8,9]], "cost": [72,68,-18,95,87,-58,-55,20,40,2] }
assert my_solution.placedCoins(**test_input) == [595080,1,1,1,1,99180,1,0,1,99180]
test_input = { "edges": [[1,6],[3,2],[9,3],[6,5],[5,9],[7,0],[4,8],[0,4],[0,9]], "cost": [85,19,-56,-71,41,-72,59,30,55,67] }
assert my_solution.placedCoins(**test_input) == [434520,1,1,1,1,0,1,1,1,342504]
test_input = { "edges": [[2,1],[7,1],[0,3],[8,4],[0,5],[7,6],[0,6],[8,0],[9,8]], "cost": [13,4,84,36,29,-97,-59,-40,77,41] }
assert my_solution.placedCoins(**test_input) == [480732,1,1,1,1,1,198240,0,91553,1]
test_input = { "edges": [[2,1],[4,5],[6,3],[1,3],[1,0],[7,0],[8,7],[5,8],[9,5]], "cost": [85,64,-67,-60,5,-14,31,-84,47,-36] }
assert my_solution.placedCoins(**test_input) == [478380,257280,1,1,1,2520,1,142128,23688,1]
test_input = { "edges": [[6,1],[4,2],[7,4],[5,7],[0,8],[6,0],[3,6],[7,3],[7,9]], "cost": [-51,-55,82,25,-53,13,-15,98,39,29] }
assert my_solution.placedCoins(**test_input) == [313404,1,1,233044,1,1,285670,233044,1,1]
test_input = { "edges": [[0,2],[5,1],[6,2],[7,3],[5,4],[5,9],[6,9],[7,9],[8,9]], "cost": [-96,-75,-58,26,-73,-25,-9,87,57,3] }
assert my_solution.placedCoins(**test_input) == [626400,1,476325,1,1,0,476325,1,1,476325]
test_input = { "edges": [[0,4],[0,9],[5,1],[2,6],[3,7],[3,8],[1,8],[1,2],[2,9]], "cost": [-58,20,21,77,-96,53,-77,-66,-32,42] }
assert my_solution.placedCoins(**test_input) == [569184,162624,391314,1,1,1,1,1,162624,391314]
test_input = { "edges": [[2,4],[5,2],[6,3],[3,0],[7,5],[8,5],[1,8],[1,0],[0,9]], "cost": [-59,-25,-25,-78,22,29,9,-12,-11,-5] }
assert my_solution.placedCoins(**test_input) == [133458,18125,1,1,1,8700,1,1,8700,1]
test_input = { "edges": [[8,1],[3,2],[5,4],[6,3],[0,3],[8,5],[5,7],[7,0],[9,0]], "cost": [86,-87,-96,-74,51,75,-76,74,-2,-60] }
assert my_solution.placedCoins(**test_input) == [718272,1,1,0,1,13050,1,283050,1,1] | 1,703,341,800 |
weekly-contest-376-find-missing-and-repeated-values | https://leetcode.com/problems/find-missing-and-repeated-values | find-missing-and-repeated-values | {
"questionId": "3227",
"questionFrontendId": "2965",
"title": "Find Missing and Repeated Values",
"titleSlug": "find-missing-and-repeated-values",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 90,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. Each integer appears exactly once except a which appears twice and b which is missing. The task is to find the repeating and missing numbers a and b.
Return a 0-indexed integer array ans of size 2 where ans[0] equals to a and ans[1] equals to b.
Example 1:
Input: grid = [[1,3],[2,2]]
Output: [2,4]
Explanation: Number 2 is repeated and number 4 is missing so the answer is [2,4].
Example 2:
Input: grid = [[9,1,7],[8,9,2],[3,4,6]]
Output: [9,5]
Explanation: Number 9 is repeated and number 5 is missing so the answer is [9,5].
Constraints:
2 <= n == grid.length == grid[i].length <= 50
1 <= grid[i][j] <= n * n
For all x that 1 <= x <= n * n there is exactly one x that is not equal to any of the grid members.
For all x that 1 <= x <= n * n there is exactly one x that is equal to exactly two of the grid members.
For all x that 1 <= x <= n * n except two of them there is exatly one pair of i, j that 0 <= i, j <= n - 1 and grid[i][j] == x.
"""
class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
| You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. Each integer appears exactly once except a which appears twice and b which is missing. The task is to find the repeating and missing numbers a and b.
Return a 0-indexed integer array ans of size 2 where ans[0] equals to a and ans[1] equals to b.
Example 1:
Input: grid = [[1,3],[2,2]]
Output: [2,4]
Explanation: Number 2 is repeated and number 4 is missing so the answer is [2,4].
Example 2:
Input: grid = [[9,1,7],[8,9,2],[3,4,6]]
Output: [9,5]
Explanation: Number 9 is repeated and number 5 is missing so the answer is [9,5].
Constraints:
2 <= n == grid.length == grid[i].length <= 50
1 <= grid[i][j] <= n * n
For all x that 1 <= x <= n * n there is exactly one x that is not equal to any of the grid members.
For all x that 1 <= x <= n * n there is exactly one x that is equal to exactly two of the grid members.
For all x that 1 <= x <= n * n except two of them there is exatly one pair of i, j that 0 <= i, j <= n - 1 and grid[i][j] == x.
Please complete the code below to solve above prblem:
```python
class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "grid": [[1,3],[2,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,4]
test_input = { "grid": [[9,1,7],[8,9,2],[3,4,6]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,5]
test_input = { "grid": [[1,1],[3,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,4]
test_input = { "grid": [[1,1],[3,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2]
test_input = { "grid": [[1,2],[1,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,4]
test_input = { "grid": [[1,2],[1,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3]
test_input = { "grid": [[1,2],[3,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,4]
test_input = { "grid": [[1,2],[4,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3]
test_input = { "grid": [[1,2],[4,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3]
test_input = { "grid": [[1,2],[4,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3]
test_input = { "grid": [[1,4],[1,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2]
test_input = { "grid": [[1,4],[2,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3]
test_input = { "grid": [[1,4],[3,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2]
test_input = { "grid": [[1,4],[3,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,2]
test_input = { "grid": [[1,4],[4,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3]
test_input = { "grid": [[2,1],[4,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3]
test_input = { "grid": [[2,1],[4,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3]
test_input = { "grid": [[2,2],[3,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1]
test_input = { "grid": [[2,2],[4,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3]
test_input = { "grid": [[2,3],[2,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,4]
test_input = { "grid": [[2,3],[4,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,1]
test_input = { "grid": [[2,4],[3,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1]
test_input = { "grid": [[2,4],[3,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1]
test_input = { "grid": [[2,4],[4,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3]
test_input = { "grid": [[3,1],[3,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,4]
test_input = { "grid": [[3,1],[3,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2]
test_input = { "grid": [[3,1],[4,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,2]
test_input = { "grid": [[3,3],[1,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2]
test_input = { "grid": [[3,4],[2,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1]
test_input = { "grid": [[3,4],[2,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1]
test_input = { "grid": [[3,4],[3,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2]
test_input = { "grid": [[3,4],[4,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,2]
test_input = { "grid": [[4,1],[1,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3]
test_input = { "grid": [[4,1],[2,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3]
test_input = { "grid": [[4,1],[2,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3]
test_input = { "grid": [[4,1],[3,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2]
test_input = { "grid": [[4,1],[3,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2]
test_input = { "grid": [[4,1],[4,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3]
test_input = { "grid": [[4,2],[2,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1]
test_input = { "grid": [[4,2],[4,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3]
test_input = { "grid": [[4,3],[1,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2]
test_input = { "grid": [[4,3],[2,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1]
test_input = { "grid": [[4,3],[2,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1]
test_input = { "grid": [[4,3],[3,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2]
test_input = { "grid": [[4,4],[2,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1]
test_input = { "grid": [[1,3,4],[9,7,5],[8,2,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,6]
test_input = { "grid": [[1,5,2],[8,4,3],[7,8,6]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,9]
test_input = { "grid": [[1,5,8],[2,7,3],[6,1,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,9]
test_input = { "grid": [[1,6,1],[4,3,7],[5,2,8]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,9]
test_input = { "grid": [[1,6,4],[9,7,5],[7,8,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,3]
test_input = { "grid": [[1,6,7],[3,6,8],[9,5,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,2]
test_input = { "grid": [[1,7,4],[8,6,2],[8,3,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,9]
test_input = { "grid": [[1,7,8],[4,5,6],[3,9,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,2]
test_input = { "grid": [[1,8,4],[9,2,7],[6,3,8]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,5]
test_input = { "grid": [[1,8,5],[4,3,2],[7,9,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,6]
test_input = { "grid": [[1,9,3],[2,7,8],[2,4,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,6]
test_input = { "grid": [[1,9,7],[8,4,2],[6,3,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,5]
test_input = { "grid": [[2,1,3],[2,9,4],[6,8,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,7]
test_input = { "grid": [[2,2,4],[7,5,3],[1,6,8]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,9]
test_input = { "grid": [[2,3,9],[5,6,4],[2,8,7]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1]
test_input = { "grid": [[2,4,6],[4,8,9],[7,3,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1]
test_input = { "grid": [[2,5,5],[4,8,7],[9,3,6]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,1]
test_input = { "grid": [[2,6,4],[6,9,5],[3,7,8]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,1]
test_input = { "grid": [[2,6,9],[1,7,9],[4,8,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,3]
test_input = { "grid": [[2,7,1],[8,6,2],[9,3,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,5]
test_input = { "grid": [[2,7,5],[7,6,4],[1,3,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,8]
test_input = { "grid": [[2,7,9],[6,8,1],[4,1,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3]
test_input = { "grid": [[2,9,7],[8,5,1],[6,7,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,3]
test_input = { "grid": [[3,4,5],[8,2,4],[6,1,7]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,9]
test_input = { "grid": [[3,5,7],[8,6,9],[1,5,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,4]
test_input = { "grid": [[3,6,1],[5,9,2],[1,7,8]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,4]
test_input = { "grid": [[3,9,4],[3,6,1],[5,7,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,8]
test_input = { "grid": [[4,2,6],[3,5,8],[3,1,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,7]
test_input = { "grid": [[4,3,2],[6,9,9],[8,7,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,1]
test_input = { "grid": [[4,6,5],[3,5,7],[2,8,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,1]
test_input = { "grid": [[4,8,7],[4,6,9],[3,2,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,5]
test_input = { "grid": [[4,9,6],[2,5,8],[3,7,7]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,1]
test_input = { "grid": [[5,3,6],[1,4,2],[9,8,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,7]
test_input = { "grid": [[5,6,9],[3,7,8],[2,2,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1]
test_input = { "grid": [[5,7,8],[1,3,2],[7,6,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,4]
test_input = { "grid": [[6,1,3],[2,4,2],[8,9,7]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,5]
test_input = { "grid": [[6,4,2],[3,7,8],[5,6,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,1]
test_input = { "grid": [[6,4,5],[7,9,3],[1,2,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,8]
test_input = { "grid": [[6,4,8],[8,1,2],[9,3,7]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,5]
test_input = { "grid": [[6,9,3],[8,9,7],[5,4,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,1]
test_input = { "grid": [[7,2,1],[6,5,3],[2,9,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,8]
test_input = { "grid": [[7,2,4],[5,8,7],[9,3,1]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,6]
test_input = { "grid": [[7,3,1],[8,9,2],[4,5,2]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,6]
test_input = { "grid": [[7,4,2],[9,1,9],[8,3,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,6]
test_input = { "grid": [[7,4,8],[1,1,3],[2,6,9]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,5]
test_input = { "grid": [[7,5,3],[4,6,3],[9,2,8]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,1]
test_input = { "grid": [[7,5,7],[3,1,6],[8,9,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,2]
test_input = { "grid": [[8,2,6],[1,8,9],[4,5,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,7]
test_input = { "grid": [[8,2,7],[3,5,1],[9,6,3]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,4]
test_input = { "grid": [[8,6,3],[1,9,5],[5,4,7]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,2]
test_input = { "grid": [[8,6,5],[3,9,1],[8,7,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,2]
test_input = { "grid": [[8,9,6],[6,1,3],[2,7,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,4]
test_input = { "grid": [[8,9,6],[7,4,2],[7,1,5]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,3]
test_input = { "grid": [[9,2,3],[7,6,4],[5,8,8]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,1]
test_input = { "grid": [[9,2,7],[3,8,7],[1,5,4]] }
assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,6] | 1,702,780,200 |
weekly-contest-376-divide-array-into-arrays-with-max-difference | https://leetcode.com/problems/divide-array-into-arrays-with-max-difference | divide-array-into-arrays-with-max-difference | {
"questionId": "3241",
"questionFrontendId": "2966",
"title": "Divide Array Into Arrays With Max Difference",
"titleSlug": "divide-array-into-arrays-with-max-difference",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 95,
"dislikes": 20,
"categoryTitle": "Algorithms"
} | """
You are given an integer array nums of size n and a positive integer k.
Divide the array into one or more arrays of size 3 satisfying the following conditions:
Each element of nums should be in exactly one array.
The difference between any two elements in one array is less than or equal to k.
Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
Example 1:
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
Output: [[1,1,3],[3,4,5],[7,8,9]]
Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
The difference between any two elements in each array is less than or equal to 2.
Note that the order of elements is not important.
Example 2:
Input: nums = [1,3,3,2,7,3], k = 3
Output: []
Explanation: It is not possible to divide the array satisfying all the conditions.
Constraints:
n == nums.length
1 <= n <= 105
n is a multiple of 3.
1 <= nums[i] <= 105
1 <= k <= 105
"""
class Solution:
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
| You are given an integer array nums of size n and a positive integer k.
Divide the array into one or more arrays of size 3 satisfying the following conditions:
Each element of nums should be in exactly one array.
The difference between any two elements in one array is less than or equal to k.
Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
Example 1:
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
Output: [[1,1,3],[3,4,5],[7,8,9]]
Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
The difference between any two elements in each array is less than or equal to 2.
Note that the order of elements is not important.
Example 2:
Input: nums = [1,3,3,2,7,3], k = 3
Output: []
Explanation: It is not possible to divide the array satisfying all the conditions.
Constraints:
n == nums.length
1 <= n <= 105
n is a multiple of 3.
1 <= nums[i] <= 105
1 <= k <= 105
Please complete the code below to solve above prblem:
```python
class Solution:
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,4,8,7,9,3,5,1], "k": 2 }
assert my_solution.divideArray(**test_input) == [[1,1,3],[3,4,5],[7,8,9]]
test_input = { "nums": [1,3,3,2,7,3], "k": 3 }
assert my_solution.divideArray(**test_input) == []
test_input = { "nums": [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], "k": 14 }
assert my_solution.divideArray(**test_input) == [[2,2,2],[4,5,5],[5,5,7],[7,8,8],[9,9,10],[11,12,12]]
test_input = { "nums": [33,26,4,18,16,24,24,15,8,18,34,20,24,16,3], "k": 16 }
assert my_solution.divideArray(**test_input) == [[3,4,8],[15,16,16],[18,18,20],[24,24,24],[26,33,34]]
test_input = { "nums": [6,1,8,8,5,8,5,9,8,9,5,8,3,4,6], "k": 7 }
assert my_solution.divideArray(**test_input) == [[1,3,4],[5,5,5],[6,6,8],[8,8,8],[8,9,9]]
test_input = { "nums": [20,21,34,3,19,2,23,32,20,17,14,13,19,20,6], "k": 15 }
assert my_solution.divideArray(**test_input) == [[2,3,6],[13,14,17],[19,19,20],[20,20,21],[23,32,34]]
test_input = { "nums": [6,10,5,12,7,11,6,6,12,12,11,7], "k": 2 }
assert my_solution.divideArray(**test_input) == [[5,6,6],[6,7,7],[10,11,11],[12,12,12]]
test_input = { "nums": [12,15,26,7,10,13,15,5,27,16,14,15], "k": 18 }
assert my_solution.divideArray(**test_input) == [[5,7,10],[12,13,14],[15,15,15],[16,26,27]]
test_input = { "nums": [12,7,13,10,7,19,11,23,3,3,7,9], "k": 16 }
assert my_solution.divideArray(**test_input) == [[3,3,7],[7,7,9],[10,11,12],[13,19,23]]
test_input = { "nums": [19,3,23,4,8,1,1,3,26], "k": 7 }
assert my_solution.divideArray(**test_input) == [[1,1,3],[3,4,8],[19,23,26]]
test_input = { "nums": [11,13,24,11,9,23,16,19,13], "k": 8 }
assert my_solution.divideArray(**test_input) == [[9,11,11],[13,13,16],[19,23,24]]
test_input = { "nums": [6,12,21,12,6,12,25,20,15,22,11,19,8,4,18,26,17,18,12,5,8], "k": 11 }
assert my_solution.divideArray(**test_input) == [[4,5,6],[6,8,8],[11,12,12],[12,12,15],[17,18,18],[19,20,21],[22,25,26]]
test_input = { "nums": [15,17,14,3,25,15,11,25,15,16,12,18], "k": 10 }
assert my_solution.divideArray(**test_input) == [[3,11,12],[14,15,15],[15,16,17],[18,25,25]]
test_input = { "nums": [16,20,16,19,20,13,14,20,14], "k": 10 }
assert my_solution.divideArray(**test_input) == [[13,14,14],[16,16,19],[20,20,20]]
test_input = { "nums": [2,13,15,14,18,15,3,13,2], "k": 1 }
assert my_solution.divideArray(**test_input) == []
test_input = { "nums": [1,14,20,7,17,2,14,1,8], "k": 11 }
assert my_solution.divideArray(**test_input) == [[1,1,2],[7,8,14],[14,17,20]]
test_input = { "nums": [8,12,19,8,9,19,9,19,9,8,6,9,6,6,12], "k": 3 }
assert my_solution.divideArray(**test_input) == [[6,6,6],[8,8,8],[9,9,9],[9,12,12],[19,19,19]]
test_input = { "nums": [18,16,17,19,12,25,11,27,11,32,32,17], "k": 20 }
assert my_solution.divideArray(**test_input) == [[11,11,12],[16,17,17],[18,19,25],[27,32,32]]
test_input = { "nums": [21,11,24,20,17,13,7,20,20,16,24,20,12,17,16,15,7,7,18,15,20], "k": 6 }
assert my_solution.divideArray(**test_input) == [[7,7,7],[11,12,13],[15,15,16],[16,17,17],[18,20,20],[20,20,20],[21,24,24]]
test_input = { "nums": [6,7,7,6,7,6], "k": 13 }
assert my_solution.divideArray(**test_input) == [[6,6,6],[7,7,7]]
test_input = { "nums": [11,12,12,5,6,5], "k": 9 }
assert my_solution.divideArray(**test_input) == [[5,5,6],[11,12,12]]
test_input = { "nums": [5,5,12,5,5,22,2,2,5,2,5,5,16,2,22,2,12,16,15,13,19], "k": 3 }
assert my_solution.divideArray(**test_input) == [[2,2,2],[2,2,5],[5,5,5],[5,5,5],[12,12,13],[15,16,16],[19,22,22]]
test_input = { "nums": [11,28,12,5,19,15,16,9,21,13,12,9,19,19,18], "k": 9 }
assert my_solution.divideArray(**test_input) == [[5,9,9],[11,12,12],[13,15,16],[18,19,19],[19,21,28]]
test_input = { "nums": [10,14,17], "k": 15 }
assert my_solution.divideArray(**test_input) == [[10,14,17]]
test_input = { "nums": [16,15,9,20,17,19,11,18,16], "k": 9 }
assert my_solution.divideArray(**test_input) == [[9,11,15],[16,16,17],[18,19,20]]
test_input = { "nums": [16,28,16,7,18,13,5,27,27,16,20,22,13,6,17], "k": 11 }
assert my_solution.divideArray(**test_input) == [[5,6,7],[13,13,16],[16,16,17],[18,20,22],[27,27,28]]
test_input = { "nums": [14,7,13,2,3,7,17,13,13,2,14,7], "k": 3 }
assert my_solution.divideArray(**test_input) == [[2,2,3],[7,7,7],[13,13,13],[14,14,17]]
test_input = { "nums": [20,8,6,5,10,5,10,2,20,6,12,13,13,20,4], "k": 6 }
assert my_solution.divideArray(**test_input) == [[2,4,5],[5,6,6],[8,10,10],[12,13,13],[20,20,20]]
test_input = { "nums": [12,14,16,9,20,18,16,4,24,14,16,30,1,17,30,16,30,6], "k": 13 }
assert my_solution.divideArray(**test_input) == [[1,4,6],[9,12,14],[14,16,16],[16,16,17],[18,20,24],[30,30,30]]
test_input = { "nums": [13,6,19,21,16,11,1,14,7], "k": 20 }
assert my_solution.divideArray(**test_input) == [[1,6,7],[11,13,14],[16,19,21]]
test_input = { "nums": [13,2,12,22,18,15,3,20,2,18,3,14,2,10,14,9,14,3,14,17,5], "k": 9 }
assert my_solution.divideArray(**test_input) == [[2,2,2],[3,3,3],[5,9,10],[12,13,14],[14,14,14],[15,17,18],[18,20,22]]
test_input = { "nums": [12,13,12,14,14,6,5,7,23,21,21,16,15,20,22,14,20,7], "k": 10 }
assert my_solution.divideArray(**test_input) == [[5,6,7],[7,12,12],[13,14,14],[14,15,16],[20,20,21],[21,22,23]]
test_input = { "nums": [15,14,3,19,17,18,19,23,2,16,19,3], "k": 5 }
assert my_solution.divideArray(**test_input) == [[2,3,3],[14,15,16],[17,18,19],[19,19,23]]
test_input = { "nums": [12,8,18,6,12,6,8,33,20,6,17,17,27,8,12], "k": 16 }
assert my_solution.divideArray(**test_input) == [[6,6,6],[8,8,8],[12,12,12],[17,17,18],[20,27,33]]
test_input = { "nums": [1,1,23,17,18,1], "k": 12 }
assert my_solution.divideArray(**test_input) == [[1,1,1],[17,18,23]]
test_input = { "nums": [13,13,3,7,6,13,6,4,3], "k": 1 }
assert my_solution.divideArray(**test_input) == [[3,3,4],[6,6,7],[13,13,13]]
test_input = { "nums": [19,10,9,20,29,28,29,9,18,27,23,4,16,8,11,19,10,12,10,10,21], "k": 20 }
assert my_solution.divideArray(**test_input) == [[4,8,9],[9,10,10],[10,10,11],[12,16,18],[19,19,20],[21,23,27],[28,29,29]]
test_input = { "nums": [13,12,12,11,22,10], "k": 15 }
assert my_solution.divideArray(**test_input) == [[10,11,12],[12,13,22]]
test_input = { "nums": [15,16,12,34,16,16,24,21,3,24,29,10], "k": 20 }
assert my_solution.divideArray(**test_input) == [[3,10,12],[15,16,16],[16,21,24],[24,29,34]]
test_input = { "nums": [17,16,17,11,13,6], "k": 19 }
assert my_solution.divideArray(**test_input) == [[6,11,13],[16,17,17]]
test_input = { "nums": [11,16,16,6,8,20,21,3,20,11,16,6,6,11,6], "k": 3 }
assert my_solution.divideArray(**test_input) == [[3,6,6],[6,6,8],[11,11,11],[16,16,16],[20,20,21]]
test_input = { "nums": [2,16,8,7,15,16], "k": 9 }
assert my_solution.divideArray(**test_input) == [[2,7,8],[15,16,16]]
test_input = { "nums": [15,17,22], "k": 14 }
assert my_solution.divideArray(**test_input) == [[15,17,22]]
test_input = { "nums": [8,4,9,18,18,5,10,11,19,18,19,23,4,15,25,20,20,6], "k": 7 }
assert my_solution.divideArray(**test_input) == [[4,4,5],[6,8,9],[10,11,15],[18,18,18],[19,19,20],[20,23,25]]
test_input = { "nums": [12,20,16,12,15,16,15,20,14,16,19,13], "k": 1 }
assert my_solution.divideArray(**test_input) == [[12,12,13],[14,15,15],[16,16,16],[19,20,20]]
test_input = { "nums": [20,19,8,21,13,18,21,12,12,18,9,9], "k": 1 }
assert my_solution.divideArray(**test_input) == [[8,9,9],[12,12,13],[18,18,19],[20,21,21]]
test_input = { "nums": [6,14,19,17,13,4,17,10,17], "k": 19 }
assert my_solution.divideArray(**test_input) == [[4,6,10],[13,14,17],[17,17,19]]
test_input = { "nums": [8,8,12], "k": 4 }
assert my_solution.divideArray(**test_input) == [[8,8,12]]
test_input = { "nums": [3,16,17,18,10,8,20,16,20,10,10,21], "k": 16 }
assert my_solution.divideArray(**test_input) == [[3,8,10],[10,10,16],[16,17,18],[20,20,21]]
test_input = { "nums": [19,14,17,20,16,16,7,10,18,8,16,15,15,13,12,14,17,11], "k": 8 }
assert my_solution.divideArray(**test_input) == [[7,8,10],[11,12,13],[14,14,15],[15,16,16],[16,17,17],[18,19,20]]
test_input = { "nums": [18,7,11,13,13,9,22,20,21,13,7,18,8,8,16], "k": 4 }
assert my_solution.divideArray(**test_input) == [[7,7,8],[8,9,11],[13,13,13],[16,18,18],[20,21,22]]
test_input = { "nums": [10,15,9,15,15,10], "k": 1 }
assert my_solution.divideArray(**test_input) == [[9,10,10],[15,15,15]]
test_input = { "nums": [16,17,16], "k": 16 }
assert my_solution.divideArray(**test_input) == [[16,16,17]]
test_input = { "nums": [15,1,15,14,18,17,1,18,12,16,6,6,7,1,12], "k": 4 }
assert my_solution.divideArray(**test_input) == [[1,1,1],[6,6,7],[12,12,14],[15,15,16],[17,18,18]]
test_input = { "nums": [6,11,6,18,11,13,13,8,11,4,4,11,12,17,11], "k": 12 }
assert my_solution.divideArray(**test_input) == [[4,4,6],[6,8,11],[11,11,11],[11,12,13],[13,17,18]]
test_input = { "nums": [5,13,4,14,11,18,9,10,20,5,17,11,5,8,20,5,14,4,18,17,17], "k": 8 }
assert my_solution.divideArray(**test_input) == [[4,4,5],[5,5,5],[8,9,10],[11,11,13],[14,14,17],[17,17,18],[18,20,20]]
test_input = { "nums": [13,6,20,13,12,8,7,12,22,16,13,7,12,17,5], "k": 6 }
assert my_solution.divideArray(**test_input) == [[5,6,7],[7,8,12],[12,12,13],[13,13,16],[17,20,22]]
test_input = { "nums": [23,2,15,20,18,14,20,7,2,22,4,14,7,9,15,14,2,7], "k": 8 }
assert my_solution.divideArray(**test_input) == [[2,2,2],[4,7,7],[7,9,14],[14,14,15],[15,18,20],[20,22,23]]
test_input = { "nums": [19,9,2,4,17,2,27,18,17], "k": 18 }
assert my_solution.divideArray(**test_input) == [[2,2,4],[9,17,17],[18,19,27]]
test_input = { "nums": [5,20,29,4,12,14,31,6,11,2,15,17,15,19,4], "k": 20 }
assert my_solution.divideArray(**test_input) == [[2,4,4],[5,6,11],[12,14,15],[15,17,19],[20,29,31]]
test_input = { "nums": [15,20,5,24,18,16,25,21,28,12,19,28,25,20,14,18,24,28], "k": 17 }
assert my_solution.divideArray(**test_input) == [[5,12,14],[15,16,18],[18,19,20],[20,21,24],[24,25,25],[28,28,28]]
test_input = { "nums": [9,6,23,17,7,17], "k": 20 }
assert my_solution.divideArray(**test_input) == [[6,7,9],[17,17,23]]
test_input = { "nums": [24,23,19], "k": 6 }
assert my_solution.divideArray(**test_input) == [[19,23,24]]
test_input = { "nums": [6,19,22,7,17,7,15,17,7,18,4,14,9,10,16], "k": 9 }
assert my_solution.divideArray(**test_input) == [[4,6,7],[7,7,9],[10,14,15],[16,17,17],[18,19,22]]
test_input = { "nums": [4,3,15,1,15,15], "k": 4 }
assert my_solution.divideArray(**test_input) == [[1,3,4],[15,15,15]]
test_input = { "nums": [10,22,18,15,7,21,6,7,11,9,7,6,7,10,18], "k": 8 }
assert my_solution.divideArray(**test_input) == [[6,6,7],[7,7,7],[9,10,10],[11,15,18],[18,21,22]]
test_input = { "nums": [16,17,2,17,9,7,22,17,12,4,14,17,4,19,12,18,19,8,17,5,6], "k": 7 }
assert my_solution.divideArray(**test_input) == [[2,4,4],[5,6,7],[8,9,12],[12,14,16],[17,17,17],[17,17,18],[19,19,22]]
test_input = { "nums": [20,18,18,22,7,9,9,10,16,4,18,18,11,9,18,11,11,21], "k": 7 }
assert my_solution.divideArray(**test_input) == [[4,7,9],[9,9,10],[11,11,11],[16,18,18],[18,18,18],[20,21,22]]
test_input = { "nums": [5,11,15,9,17,6,16,14,4,9,5,13,10,12,13,15,13,12,16,12,13], "k": 5 }
assert my_solution.divideArray(**test_input) == [[4,5,5],[6,9,9],[10,11,12],[12,12,13],[13,13,13],[14,15,15],[16,16,17]]
test_input = { "nums": [4,16,17], "k": 20 }
assert my_solution.divideArray(**test_input) == [[4,16,17]]
test_input = { "nums": [10,9,22,13,17,11,6,9,11], "k": 10 }
assert my_solution.divideArray(**test_input) == [[6,9,9],[10,11,11],[13,17,22]]
test_input = { "nums": [3,11,19,8,22,23,15,18,37,7,25,20,12,19,7], "k": 18 }
assert my_solution.divideArray(**test_input) == [[3,7,7],[8,11,12],[15,18,19],[19,20,22],[23,25,37]]
test_input = { "nums": [4,6,6,3,11,11], "k": 16 }
assert my_solution.divideArray(**test_input) == [[3,4,6],[6,11,11]]
test_input = { "nums": [10,17,10,15,16,8], "k": 7 }
assert my_solution.divideArray(**test_input) == [[8,10,10],[15,16,17]]
test_input = { "nums": [4,20,4,19,8,7,4,20,7], "k": 3 }
assert my_solution.divideArray(**test_input) == [[4,4,4],[7,7,8],[19,20,20]]
test_input = { "nums": [4,4,4], "k": 17 }
assert my_solution.divideArray(**test_input) == [[4,4,4]]
test_input = { "nums": [18,6,15,20,5,27,23,15,26,11,11,4,17,23,11], "k": 15 }
assert my_solution.divideArray(**test_input) == [[4,5,6],[11,11,11],[15,15,17],[18,20,23],[23,26,27]]
test_input = { "nums": [8,9,5], "k": 15 }
assert my_solution.divideArray(**test_input) == [[5,8,9]]
test_input = { "nums": [20,15,8,11,11,10,19,7,20], "k": 7 }
assert my_solution.divideArray(**test_input) == [[7,8,10],[11,11,15],[19,20,20]]
test_input = { "nums": [12,11,18,13,13,21], "k": 11 }
assert my_solution.divideArray(**test_input) == [[11,12,13],[13,18,21]]
test_input = { "nums": [19,29,11,18,19,17,29,19,7], "k": 14 }
assert my_solution.divideArray(**test_input) == [[7,11,17],[18,19,19],[19,29,29]]
test_input = { "nums": [14,1,25,1,14,19,2,2,4,16,17,11,26,29,12], "k": 17 }
assert my_solution.divideArray(**test_input) == [[1,1,2],[2,4,11],[12,14,14],[16,17,19],[25,26,29]]
test_input = { "nums": [14,25,16,11,7,13,12,16,24,19,5,17], "k": 13 }
assert my_solution.divideArray(**test_input) == [[5,7,11],[12,13,14],[16,16,17],[19,24,25]]
test_input = { "nums": [11,26,19,10,16,10,11,18,9], "k": 11 }
assert my_solution.divideArray(**test_input) == [[9,10,10],[11,11,16],[18,19,26]]
test_input = { "nums": [16,8,15], "k": 16 }
assert my_solution.divideArray(**test_input) == [[8,15,16]]
test_input = { "nums": [12,8,18,8,18,13,12,18,18,13,12,23,21,8,13], "k": 5 }
assert my_solution.divideArray(**test_input) == [[8,8,8],[12,12,12],[13,13,13],[18,18,18],[18,21,23]]
test_input = { "nums": [12,16,9,8,22,16], "k": 16 }
assert my_solution.divideArray(**test_input) == [[8,9,12],[16,16,22]]
test_input = { "nums": [15,16,18,8,12,7,5,17,23,17,18,13,5,4,13,18,7,20], "k": 6 }
assert my_solution.divideArray(**test_input) == [[4,5,5],[7,7,8],[12,13,13],[15,16,17],[17,18,18],[18,20,23]]
test_input = { "nums": [12,11,14,13,9,16,31,19,21,22,7,1,22,23,9,2,21,21], "k": 15 }
assert my_solution.divideArray(**test_input) == [[1,2,7],[9,9,11],[12,13,14],[16,19,21],[21,21,22],[22,23,31]]
test_input = { "nums": [7,15,18,20,6,21,18,17,11,1,14,15,18,8,17,13,11,8,5,12,11], "k": 10 }
assert my_solution.divideArray(**test_input) == [[1,5,6],[7,8,8],[11,11,11],[12,13,14],[15,15,17],[17,18,18],[18,20,21]]
test_input = { "nums": [13,16,17,16,6,12], "k": 11 }
assert my_solution.divideArray(**test_input) == [[6,12,13],[16,16,17]]
test_input = { "nums": [17,17,17,16,17,17], "k": 1 }
assert my_solution.divideArray(**test_input) == [[16,17,17],[17,17,17]]
test_input = { "nums": [6,14,6,15,14,6], "k": 17 }
assert my_solution.divideArray(**test_input) == [[6,6,6],[14,14,15]]
test_input = { "nums": [23,19,21,10,10,13,15,19,19,3,15,3], "k": 12 }
assert my_solution.divideArray(**test_input) == [[3,3,10],[10,13,15],[15,19,19],[19,21,23]]
test_input = { "nums": [11,4,3,11,3,27,19,10,6,12,11,24,27,1,31], "k": 17 }
assert my_solution.divideArray(**test_input) == [[1,3,3],[4,6,10],[11,11,11],[12,19,24],[27,27,31]]
test_input = { "nums": [8,18,18,20,20,19,20,31,7], "k": 17 }
assert my_solution.divideArray(**test_input) == [[7,8,18],[18,19,20],[20,20,31]]
test_input = { "nums": [4,22,8,12,1,4,4,17,22,4,10,1], "k": 12 }
assert my_solution.divideArray(**test_input) == [[1,1,4],[4,4,4],[8,10,12],[17,22,22]]
test_input = { "nums": [16,15,16,6,9,22,14,16,10,26,18,16,11,18,7], "k": 10 }
assert my_solution.divideArray(**test_input) == [[6,7,9],[10,11,14],[15,16,16],[16,16,18],[18,22,26]]
test_input = { "nums": [5,16,12,26,16,18,1,6,23,2,1,21,8,11,9], "k": 14 }
assert my_solution.divideArray(**test_input) == [[1,1,2],[5,6,8],[9,11,12],[16,16,18],[21,23,26]]
test_input = { "nums": [6,3,24,13,19,24,13,12,15,3,6,3], "k": 17 }
assert my_solution.divideArray(**test_input) == [[3,3,3],[6,6,12],[13,13,15],[19,24,24]] | 1,702,780,200 |
weekly-contest-376-minimum-cost-to-make-array-equalindromic | https://leetcode.com/problems/minimum-cost-to-make-array-equalindromic | minimum-cost-to-make-array-equalindromic | {
"questionId": "3229",
"questionFrontendId": "2967",
"title": "Minimum Cost to Make Array Equalindromic",
"titleSlug": "minimum-cost-to-make-array-equalindromic",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 164,
"dislikes": 70,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums having length n.
You are allowed to perform a special move any number of times (including zero) on nums. In one special move you perform the following steps in order:
Choose an index i in the range [0, n - 1], and a positive integer x.
Add |nums[i] - x| to the total cost.
Change the value of nums[i] to x.
A palindromic number is a positive integer that remains the same when its digits are reversed. For example, 121, 2552 and 65756 are palindromic numbers whereas 24, 46, 235 are not palindromic numbers.
An array is considered equalindromic if all the elements in the array are equal to an integer y, where y is a palindromic number less than 109.
Return an integer denoting the minimum possible total cost to make nums equalindromic by performing any number of special moves.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 6
Explanation: We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
Example 2:
Input: nums = [10,12,13,14,15]
Output: 11
Explanation: We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
Example 3:
Input: nums = [22,33,22,33,22]
Output: 22
Explanation: We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
Constraints:
1 <= n <= 105
1 <= nums[i] <= 109
"""
class Solution:
def minimumCost(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums having length n.
You are allowed to perform a special move any number of times (including zero) on nums. In one special move you perform the following steps in order:
Choose an index i in the range [0, n - 1], and a positive integer x.
Add |nums[i] - x| to the total cost.
Change the value of nums[i] to x.
A palindromic number is a positive integer that remains the same when its digits are reversed. For example, 121, 2552 and 65756 are palindromic numbers whereas 24, 46, 235 are not palindromic numbers.
An array is considered equalindromic if all the elements in the array are equal to an integer y, where y is a palindromic number less than 109.
Return an integer denoting the minimum possible total cost to make nums equalindromic by performing any number of special moves.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 6
Explanation: We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6.
It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
Example 2:
Input: nums = [10,12,13,14,15]
Output: 11
Explanation: We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11.
It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
Example 3:
Input: nums = [22,33,22,33,22]
Output: 22
Explanation: We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22.
It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
Constraints:
1 <= n <= 105
1 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumCost(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4,5] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [10,12,13,14,15] }
assert my_solution.minimumCost(**test_input) == 11
test_input = { "nums": [22,33,22,33,22] }
assert my_solution.minimumCost(**test_input) == 22
test_input = { "nums": [1] }
assert my_solution.minimumCost(**test_input) == 0
test_input = { "nums": [2] }
assert my_solution.minimumCost(**test_input) == 0
test_input = { "nums": [3] }
assert my_solution.minimumCost(**test_input) == 0
test_input = { "nums": [4] }
assert my_solution.minimumCost(**test_input) == 0
test_input = { "nums": [5] }
assert my_solution.minimumCost(**test_input) == 0
test_input = { "nums": [2,1] }
assert my_solution.minimumCost(**test_input) == 1
test_input = { "nums": [3,1] }
assert my_solution.minimumCost(**test_input) == 2
test_input = { "nums": [3,2] }
assert my_solution.minimumCost(**test_input) == 1
test_input = { "nums": [4,1] }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [4,2] }
assert my_solution.minimumCost(**test_input) == 2
test_input = { "nums": [4,3] }
assert my_solution.minimumCost(**test_input) == 1
test_input = { "nums": [5,1] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [5,2] }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [5,3] }
assert my_solution.minimumCost(**test_input) == 2
test_input = { "nums": [5,4] }
assert my_solution.minimumCost(**test_input) == 1
test_input = { "nums": [3,2,1] }
assert my_solution.minimumCost(**test_input) == 2
test_input = { "nums": [4,2,1] }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [4,3,1] }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [4,3,2] }
assert my_solution.minimumCost(**test_input) == 2
test_input = { "nums": [5,2,1] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [5,3,1] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [5,3,2] }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [5,4,1] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [5,4,2] }
assert my_solution.minimumCost(**test_input) == 3
test_input = { "nums": [5,4,3] }
assert my_solution.minimumCost(**test_input) == 2
test_input = { "nums": [4,3,2,1] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [5,3,2,1] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [5,4,2,1] }
assert my_solution.minimumCost(**test_input) == 6
test_input = { "nums": [5,4,3,1] }
assert my_solution.minimumCost(**test_input) == 5
test_input = { "nums": [5,4,3,2] }
assert my_solution.minimumCost(**test_input) == 4
test_input = { "nums": [301,309,312,322] }
assert my_solution.minimumCost(**test_input) == 26
test_input = { "nums": [302,306,316,329] }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "nums": [302,315,317,320] }
assert my_solution.minimumCost(**test_input) == 24
test_input = { "nums": [304,310,324,328] }
assert my_solution.minimumCost(**test_input) == 38
test_input = { "nums": [307,321,322,327] }
assert my_solution.minimumCost(**test_input) == 23
test_input = { "nums": [307,323,325,330] }
assert my_solution.minimumCost(**test_input) == 25
test_input = { "nums": [308,313,319,322] }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [311,313,320,324] }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [313,318,323,328] }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [101,102,105,108,124] }
assert my_solution.minimumCost(**test_input) == 35
test_input = { "nums": [101,102,105,120,124] }
assert my_solution.minimumCost(**test_input) == 47
test_input = { "nums": [101,102,111,125,126] }
assert my_solution.minimumCost(**test_input) == 48
test_input = { "nums": [101,104,107,126,130] }
assert my_solution.minimumCost(**test_input) == 55
test_input = { "nums": [101,106,113,120,124] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "nums": [101,115,116,120,122] }
assert my_solution.minimumCost(**test_input) == 33
test_input = { "nums": [102,103,105,106,109] }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [102,103,107,125,128] }
assert my_solution.minimumCost(**test_input) == 52
test_input = { "nums": [102,103,111,119,125] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "nums": [102,105,120,128,129] }
assert my_solution.minimumCost(**test_input) == 51
test_input = { "nums": [103,104,113,116,119] }
assert my_solution.minimumCost(**test_input) == 30
test_input = { "nums": [103,105,107,113,125] }
assert my_solution.minimumCost(**test_input) == 34
test_input = { "nums": [103,106,113,120,130] }
assert my_solution.minimumCost(**test_input) == 43
test_input = { "nums": [103,109,110,118,123] }
assert my_solution.minimumCost(**test_input) == 30
test_input = { "nums": [103,110,121,127,129] }
assert my_solution.minimumCost(**test_input) == 43
test_input = { "nums": [103,114,120,123,125] }
assert my_solution.minimumCost(**test_input) == 32
test_input = { "nums": [103,115,120,122,128] }
assert my_solution.minimumCost(**test_input) == 33
test_input = { "nums": [104,107,120,122,124] }
assert my_solution.minimumCost(**test_input) == 36
test_input = { "nums": [104,111,121,128,129] }
assert my_solution.minimumCost(**test_input) == 42
test_input = { "nums": [104,119,123,125,126] }
assert my_solution.minimumCost(**test_input) == 30
test_input = { "nums": [105,108,112,119,130] }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "nums": [105,108,122,126,130] }
assert my_solution.minimumCost(**test_input) == 44
test_input = { "nums": [105,110,118,123,125] }
assert my_solution.minimumCost(**test_input) == 36
test_input = { "nums": [105,116,118,123,129] }
assert my_solution.minimumCost(**test_input) == 34
test_input = { "nums": [106,107,114,117,127] }
assert my_solution.minimumCost(**test_input) == 34
test_input = { "nums": [106,109,118,124,125] }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "nums": [106,111,113,123,129] }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "nums": [106,113,116,120,122] }
assert my_solution.minimumCost(**test_input) == 30
test_input = { "nums": [107,108,113,114,122] }
assert my_solution.minimumCost(**test_input) == 23
test_input = { "nums": [107,109,112,118,128] }
assert my_solution.minimumCost(**test_input) == 31
test_input = { "nums": [107,109,115,116,129] }
assert my_solution.minimumCost(**test_input) == 33
test_input = { "nums": [107,110,119,125,130] }
assert my_solution.minimumCost(**test_input) == 40
test_input = { "nums": [107,112,116,121,124] }
assert my_solution.minimumCost(**test_input) == 31
test_input = { "nums": [107,115,116,120,127] }
assert my_solution.minimumCost(**test_input) == 32
test_input = { "nums": [107,116,123,125,128] }
assert my_solution.minimumCost(**test_input) == 32
test_input = { "nums": [108,110,116,121,130] }
assert my_solution.minimumCost(**test_input) == 38
test_input = { "nums": [108,113,114,115,119] }
assert my_solution.minimumCost(**test_input) == 20
test_input = { "nums": [108,113,116,124,129] }
assert my_solution.minimumCost(**test_input) == 37
test_input = { "nums": [108,115,124,127,129] }
assert my_solution.minimumCost(**test_input) == 36
test_input = { "nums": [109,113,115,122,128] }
assert my_solution.minimumCost(**test_input) == 34
test_input = { "nums": [110,111,112,126,129] }
assert my_solution.minimumCost(**test_input) == 35
test_input = { "nums": [110,113,119,124,125] }
assert my_solution.minimumCost(**test_input) == 28
test_input = { "nums": [111,114,117,118,125] }
assert my_solution.minimumCost(**test_input) == 28
test_input = { "nums": [112,113,114,120,130] }
assert my_solution.minimumCost(**test_input) == 34
test_input = { "nums": [112,115,120,123,129] }
assert my_solution.minimumCost(**test_input) == 26
test_input = { "nums": [117,118,119,124,128] }
assert my_solution.minimumCost(**test_input) == 19
test_input = { "nums": [201,202,203,205,215,228] }
assert my_solution.minimumCost(**test_input) == 44
test_input = { "nums": [201,202,204,205,215,225] }
assert my_solution.minimumCost(**test_input) == 42
test_input = { "nums": [201,202,204,207,220,224] }
assert my_solution.minimumCost(**test_input) == 48
test_input = { "nums": [201,202,204,208,216,225] }
assert my_solution.minimumCost(**test_input) == 46
test_input = { "nums": [201,202,204,218,223,224] }
assert my_solution.minimumCost(**test_input) == 58
test_input = { "nums": [201,202,205,210,227,228] }
assert my_solution.minimumCost(**test_input) == 61
test_input = { "nums": [201,202,205,211,214,225] }
assert my_solution.minimumCost(**test_input) == 44
test_input = { "nums": [201,202,207,210,223,226] }
assert my_solution.minimumCost(**test_input) == 53
test_input = { "nums": [201,202,207,212,215,222] }
assert my_solution.minimumCost(**test_input) == 39
test_input = { "nums": [201,202,207,216,220,223] }
assert my_solution.minimumCost(**test_input) == 49
test_input = { "nums": [201,202,211,213,221,223] }
assert my_solution.minimumCost(**test_input) == 43
test_input = { "nums": [201,202,212,213,219,229] }
assert my_solution.minimumCost(**test_input) == 46 | 1,702,780,200 |
weekly-contest-376-apply-operations-to-maximize-frequency-score | https://leetcode.com/problems/apply-operations-to-maximize-frequency-score | apply-operations-to-maximize-frequency-score | {
"questionId": "3196",
"questionFrontendId": "2968",
"title": "Apply Operations to Maximize Frequency Score",
"titleSlug": "apply-operations-to-maximize-frequency-score",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 175,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums and an integer k.
You can perform the following operation on the array at most k times:
Choose any index i from the array and increase or decrease nums[i] by 1.
The score of the final array is the frequency of the most frequent element in the array.
Return the maximum score you can achieve.
The frequency of an element is the number of occurences of that element in the array.
Example 1:
Input: nums = [1,2,6,4], k = 3
Output: 3
Explanation: We can do the following operations on the array:
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
The element 2 is the most frequent in the final array so our score is 3.
It can be shown that we cannot achieve a better score.
Example 2:
Input: nums = [1,4,4,2,4], k = 0
Output: 3
Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
0 <= k <= 1014
"""
class Solution:
def maxFrequencyScore(self, nums: List[int], k: int) -> int:
| You are given a 0-indexed integer array nums and an integer k.
You can perform the following operation on the array at most k times:
Choose any index i from the array and increase or decrease nums[i] by 1.
The score of the final array is the frequency of the most frequent element in the array.
Return the maximum score you can achieve.
The frequency of an element is the number of occurences of that element in the array.
Example 1:
Input: nums = [1,2,6,4], k = 3
Output: 3
Explanation: We can do the following operations on the array:
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
The element 2 is the most frequent in the final array so our score is 3.
It can be shown that we cannot achieve a better score.
Example 2:
Input: nums = [1,4,4,2,4], k = 0
Output: 3
Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
0 <= k <= 1014
Please complete the code below to solve above prblem:
```python
class Solution:
def maxFrequencyScore(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,6,4], "k": 3 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [1,4,4,2,4], "k": 0 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [3,20,13,2,3,15,24,19,8,13,19,20,21], "k": 45 }
assert my_solution.maxFrequencyScore(**test_input) == 10
test_input = { "nums": [13,22,29,21,13,17,5,2,27,6,10,4,23,29,27], "k": 117 }
assert my_solution.maxFrequencyScore(**test_input) == 14
test_input = { "nums": [27,8,30,3,13,28,7,14,21,19,24,28,29,1,14,22,6], "k": 23 }
assert my_solution.maxFrequencyScore(**test_input) == 8
test_input = { "nums": [10,11,3], "k": 1 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [10,19,26,18,27,18], "k": 9 }
assert my_solution.maxFrequencyScore(**test_input) == 4
test_input = { "nums": [17,24,10,23,22,15,25,2,13,24,22,25,25,21], "k": 52 }
assert my_solution.maxFrequencyScore(**test_input) == 13
test_input = { "nums": [28,6,22,10], "k": 12 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [17,17,25,14,29,28,20,14,16,22,4,28,2,5,3,11,6,20,17], "k": 76 }
assert my_solution.maxFrequencyScore(**test_input) == 14
test_input = { "nums": [23,10,18,21,16,23,14], "k": 2 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [5,13,7], "k": 8 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [6,29,3,19,10,6,20,26,1,30,11,25,29,12,29,14,15,16,5], "k": 64 }
assert my_solution.maxFrequencyScore(**test_input) == 12
test_input = { "nums": [10,26,21,18,30,25,1], "k": 8 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [29,10,26,1,2,2,17,7,5,16,24,27,7,7,26,26,24], "k": 3 }
assert my_solution.maxFrequencyScore(**test_input) == 5
test_input = { "nums": [11,16,6,12,3,8,5,29,9,15,7,9,14,6,11,14,12,23,22,14], "k": 79 }
assert my_solution.maxFrequencyScore(**test_input) == 19
test_input = { "nums": [5,17,15,14,27,11,22,6,4], "k": 26 }
assert my_solution.maxFrequencyScore(**test_input) == 6
test_input = { "nums": [13,22,17], "k": 4 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [24,6,14,6,30,9,6,11,21,10,12,27,1], "k": 90 }
assert my_solution.maxFrequencyScore(**test_input) == 13
test_input = { "nums": [19,5,2,23,16,22,3,2,5,20,17,3,22,1], "k": 15 }
assert my_solution.maxFrequencyScore(**test_input) == 7
test_input = { "nums": [15,20], "k": 5 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [9,2,5,14,19,5,10,10,2,25,1,1,1,14,9,13,5,6,10,1], "k": 80 }
assert my_solution.maxFrequencyScore(**test_input) == 18
test_input = { "nums": [2,29,24,19,5], "k": 24 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [4,10,5], "k": 6 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [5,2,22,7,18,26,15,4,24,26,24], "k": 19 }
assert my_solution.maxFrequencyScore(**test_input) == 6
test_input = { "nums": [23,21,10], "k": 13 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [5,23,7,2,1,5,12,2,20,24,5,4], "k": 71 }
assert my_solution.maxFrequencyScore(**test_input) == 11
test_input = { "nums": [22,13,30], "k": 17 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [7,23,29,8,9,3,4,16,24,6,18,20,19,14,5], "k": 55 }
assert my_solution.maxFrequencyScore(**test_input) == 11
test_input = { "nums": [3,11,24,27,10], "k": 24 }
assert my_solution.maxFrequencyScore(**test_input) == 4
test_input = { "nums": [12,11,21,6,13,27,11,2,27,26,24,13], "k": 0 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [4,10,26,16,21,26,11,26,30,24,18,30,23,26,24], "k": 50 }
assert my_solution.maxFrequencyScore(**test_input) == 12
test_input = { "nums": [4,2,18,14,9,29], "k": 4 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [9,27,19,18], "k": 9 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [11,17,29,9,22,7,13,14,12,24,9], "k": 47 }
assert my_solution.maxFrequencyScore(**test_input) == 10
test_input = { "nums": [20,10,15,16], "k": 10 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [16,2,6,20,2,18,16,8,15,19,22,29,24,2,26,19], "k": 40 }
assert my_solution.maxFrequencyScore(**test_input) == 11
test_input = { "nums": [17,13,19,28,6,8,5,25,2,3,9,4,21,6,13,10,5,3], "k": 113 }
assert my_solution.maxFrequencyScore(**test_input) == 18
test_input = { "nums": [16,5,10,15], "k": 5 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [23,2,23,27,21], "k": 2 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [15,26,3,14,3,18,16,19,11,9,2,18,14,8,20,9], "k": 75 }
assert my_solution.maxFrequencyScore(**test_input) == 15
test_input = { "nums": [13,23,4,5,2], "k": 3 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [3,1,9,12], "k": 8 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [4,27,21,16,11,5,5,1,5,10], "k": 18 }
assert my_solution.maxFrequencyScore(**test_input) == 7
test_input = { "nums": [14,4,23,27,8,25,7,12,12,21,21,11,20,23,30,11,12,29,22], "k": 77 }
assert my_solution.maxFrequencyScore(**test_input) == 15
test_input = { "nums": [10,30,25], "k": 20 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [4,8,22,25,27,26,18,14,18], "k": 56 }
assert my_solution.maxFrequencyScore(**test_input) == 9
test_input = { "nums": [2,19,27,25,23,17,8,16,28,10,6,24,6], "k": 28 }
assert my_solution.maxFrequencyScore(**test_input) == 7
test_input = { "nums": [27,25,27,10,23,14,24,17,12,22,14,11,19,16,7,15], "k": 21 }
assert my_solution.maxFrequencyScore(**test_input) == 9
test_input = { "nums": [23,11], "k": 12 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [20,28,15,11,22,26,29,2,16,9], "k": 61 }
assert my_solution.maxFrequencyScore(**test_input) == 9
test_input = { "nums": [21,11,1,17,20,19,24,10,1,4,10,30,11,29,20,12,18,5,4,27], "k": 110 }
assert my_solution.maxFrequencyScore(**test_input) == 17
test_input = { "nums": [22,29,7,1,26,22,27,1,16,25,25], "k": 26 }
assert my_solution.maxFrequencyScore(**test_input) == 8
test_input = { "nums": [26,6,24], "k": 20 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [29,24,9,4,2,9,28,1,25,25,13,22,27,26,15,18], "k": 2 }
assert my_solution.maxFrequencyScore(**test_input) == 4
test_input = { "nums": [9,19,1,24,15,19,22,13,10,8,4,10,26,23,11,8], "k": 89 }
assert my_solution.maxFrequencyScore(**test_input) == 15
test_input = { "nums": [18,6,20,22,25,21,19,19,15,5,7,29,28,7,17,4], "k": 104 }
assert my_solution.maxFrequencyScore(**test_input) == 16
test_input = { "nums": [9,11,28,24,30,6,1,30,22,16,20,19,21,17], "k": 62 }
assert my_solution.maxFrequencyScore(**test_input) == 11
test_input = { "nums": [15,13,29,28], "k": 15 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [28,26,8], "k": 18 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [6,12,24,4,25,23,5,13,7,5], "k": 12 }
assert my_solution.maxFrequencyScore(**test_input) == 6
test_input = { "nums": [8,23,15,15,3,19,6,20,12,18,7,8,18,19,11,20,4,18], "k": 54 }
assert my_solution.maxFrequencyScore(**test_input) == 14
test_input = { "nums": [30,2,4,7,19,3,3,14,24,4,26,17,1,12,4,11], "k": 36 }
assert my_solution.maxFrequencyScore(**test_input) == 11
test_input = { "nums": [12,15,21,17,7,20,16,30,8,6,28,28,23,6,12,14,19,26,27,5], "k": 87 }
assert my_solution.maxFrequencyScore(**test_input) == 16
test_input = { "nums": [15,1,27,4,5,20,5,26,28], "k": 38 }
assert my_solution.maxFrequencyScore(**test_input) == 6
test_input = { "nums": [27,14,30,6,14,29,5,8], "k": 51 }
assert my_solution.maxFrequencyScore(**test_input) == 7
test_input = { "nums": [18,22], "k": 4 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [17,28,16,24,29], "k": 5 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [2,22,27,12,30,9,27,3,26,23,25,30,20,19,9,1,23,14,18,26], "k": 30 }
assert my_solution.maxFrequencyScore(**test_input) == 11
test_input = { "nums": [26,16,9,7,10,16,26,22,24,1,30,8,15,5,28,16,13,12], "k": 84 }
assert my_solution.maxFrequencyScore(**test_input) == 15
test_input = { "nums": [26,8,27,27,22,28,8,26,24,15,6,13,20,12], "k": 55 }
assert my_solution.maxFrequencyScore(**test_input) == 11
test_input = { "nums": [19,19,20,14,19,20,5,4,14,26,12,17,14,29,3,9], "k": 94 }
assert my_solution.maxFrequencyScore(**test_input) == 16
test_input = { "nums": [9,19,14,17,14,20,27,9,22,29,15,20,6,25,8,17,18,24,23], "k": 44 }
assert my_solution.maxFrequencyScore(**test_input) == 13
test_input = { "nums": [2,10], "k": 8 }
assert my_solution.maxFrequencyScore(**test_input) == 2
test_input = { "nums": [20,2,27,27,19,20,8,21,15,20,14,18,25], "k": 17 }
assert my_solution.maxFrequencyScore(**test_input) == 8
test_input = { "nums": [6,27,6,30,2,1,7,24,18,4,2,18,17], "k": 56 }
assert my_solution.maxFrequencyScore(**test_input) == 10
test_input = { "nums": [23,18,30,24,5,21], "k": 33 }
assert my_solution.maxFrequencyScore(**test_input) == 6
test_input = { "nums": [12,16,24,18,12,20,26,15,11,23,4,25], "k": 35 }
assert my_solution.maxFrequencyScore(**test_input) == 9
test_input = { "nums": [9,9,11,18], "k": 11 }
assert my_solution.maxFrequencyScore(**test_input) == 4
test_input = { "nums": [19,21,7,15,21,10,5,27,2,27,14], "k": 63 }
assert my_solution.maxFrequencyScore(**test_input) == 9
test_input = { "nums": [25,17,13,6,3,19,21,12,29,1,16,14,24,27,25,13,1,5,17], "k": 136 }
assert my_solution.maxFrequencyScore(**test_input) == 19
test_input = { "nums": [4,4,8,9,14,7,27,8,2,29,1,28,23,13], "k": 109 }
assert my_solution.maxFrequencyScore(**test_input) == 14
test_input = { "nums": [14,2,18,30,28,17,25,10,7,10,19,3,26,22,12,17,8,4], "k": 24 }
assert my_solution.maxFrequencyScore(**test_input) == 8
test_input = { "nums": [27,17,12,19,25,1,9,4,9,20,2,5], "k": 70 }
assert my_solution.maxFrequencyScore(**test_input) == 10
test_input = { "nums": [18,25,12,20,19,26,2,15,3,6,29,29,2,24,4,9], "k": 106 }
assert my_solution.maxFrequencyScore(**test_input) == 14
test_input = { "nums": [17,18,14,1,28,15,14,13,9,16,28,9,21,23,2,11], "k": 65 }
assert my_solution.maxFrequencyScore(**test_input) == 14
test_input = { "nums": [17,15,29,30,12,13,10], "k": 37 }
assert my_solution.maxFrequencyScore(**test_input) == 6
test_input = { "nums": [15,4,11,1,18,29,9,23,14,25,15,12,15,6,30,28], "k": 60 }
assert my_solution.maxFrequencyScore(**test_input) == 12
test_input = { "nums": [20,6,8,15,12,8,26,7,27,8,5,25,17,12,7,1,23,24,8], "k": 62 }
assert my_solution.maxFrequencyScore(**test_input) == 14
test_input = { "nums": [22,21,30,16,23,24,2,2,24], "k": 39 }
assert my_solution.maxFrequencyScore(**test_input) == 7
test_input = { "nums": [8,9,6,30,28,2,1,3,14,8,21,26,13,29,23,3,14,9,6,25], "k": 91 }
assert my_solution.maxFrequencyScore(**test_input) == 16
test_input = { "nums": [7,10,16,23,17,22,28,7,4,21,25,21,19,30,13,19,15,21,23], "k": 53 }
assert my_solution.maxFrequencyScore(**test_input) == 15
test_input = { "nums": [25,17,1,24,3,6,8,29,19,4,16,12,9,28,1,21,13,29], "k": 151 }
assert my_solution.maxFrequencyScore(**test_input) == 18
test_input = { "nums": [1,6,14,9], "k": 8 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [5,7,17,2,23,6,3,13,2,11,10,8,18,16,3,11,26], "k": 30 }
assert my_solution.maxFrequencyScore(**test_input) == 10
test_input = { "nums": [27,27,16,18,24,7,26,30,21,25,28,28,29,27,28,6], "k": 0 }
assert my_solution.maxFrequencyScore(**test_input) == 3
test_input = { "nums": [29,16,9,21,2,16,4,17,22,11,20,23,5,22,7,27,20], "k": 85 }
assert my_solution.maxFrequencyScore(**test_input) == 15
test_input = { "nums": [25,5,24,2,30,15,17,27,15,15,27], "k": 69 }
assert my_solution.maxFrequencyScore(**test_input) == 10
test_input = { "nums": [6,29,24,14,9,14,1,1,28,20,19,21,13,25,17,24,30,14], "k": 107 }
assert my_solution.maxFrequencyScore(**test_input) == 16
test_input = { "nums": [17,20,2,11,5,7,28], "k": 36 }
assert my_solution.maxFrequencyScore(**test_input) == 6 | 1,702,780,200 |
weekly-contest-375-count-tested-devices-after-test-operations | https://leetcode.com/problems/count-tested-devices-after-test-operations | count-tested-devices-after-test-operations | {
"questionId": "3220",
"questionFrontendId": "2960",
"title": "Count Tested Devices After Test Operations",
"titleSlug": "count-tested-devices-after-test-operations",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 90,
"dislikes": 5,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices.
Your task is to test each device i in order from 0 to n - 1, by performing the following test operations:
If batteryPercentages[i] is greater than 0:
Increment the count of tested devices.
Decrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1).
Move to the next device.
Otherwise, move to the next device without performing any test.
Return an integer denoting the number of devices that will be tested after performing the test operations in order.
Example 1:
Input: batteryPercentages = [1,1,2,1,3]
Output: 3
Explanation: Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
So, the answer is 3.
Example 2:
Input: batteryPercentages = [0,1,2]
Output: 2
Explanation: Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
So, the answer is 2.
Constraints:
1 <= n == batteryPercentages.length <= 100
0 <= batteryPercentages[i] <= 100
"""
class Solution:
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
| You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices.
Your task is to test each device i in order from 0 to n - 1, by performing the following test operations:
If batteryPercentages[i] is greater than 0:
Increment the count of tested devices.
Decrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1).
Move to the next device.
Otherwise, move to the next device without performing any test.
Return an integer denoting the number of devices that will be tested after performing the test operations in order.
Example 1:
Input: batteryPercentages = [1,1,2,1,3]
Output: 3
Explanation: Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
So, the answer is 3.
Example 2:
Input: batteryPercentages = [0,1,2]
Output: 2
Explanation: Performing the test operations in order starting from device 0:
At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
So, the answer is 2.
Constraints:
1 <= n == batteryPercentages.length <= 100
0 <= batteryPercentages[i] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "batteryPercentages": [1,1,2,1,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [0,1,2] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [0] }
assert my_solution.countTestedDevices(**test_input) == 0
test_input = { "batteryPercentages": [1] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [0,0] }
assert my_solution.countTestedDevices(**test_input) == 0
test_input = { "batteryPercentages": [0,1] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [0,2] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [1,0] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [1,2] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [2,1] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [2,2] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [0,0,1] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [0,0,2] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [1,1,0] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [1,2,0] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [1,3,1] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [2,0,1] }
assert my_solution.countTestedDevices(**test_input) == 1
test_input = { "batteryPercentages": [2,2,0] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [2,2,2] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [3,0,3] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [3,3,1] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [3,3,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [0,2,1,4] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [1,4,4,1] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [3,1,2,0] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [3,2,1,1] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [3,2,1,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [4,1,4,4] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [4,2,0,1] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [4,2,1,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [4,4,4,2] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [0,3,1,3,5] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [0,4,2,5,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [0,5,4,2,0] }
assert my_solution.countTestedDevices(**test_input) == 2
test_input = { "batteryPercentages": [2,2,3,0,2] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [2,3,5,0,1] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [2,4,5,2,0] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [4,3,3,5,4] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [5,4,1,0,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [5,5,5,2,0] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [0,2,4,3,0,2] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [0,4,5,3,3,2] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [1,3,1,5,4,5] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [1,6,0,3,3,6] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [3,1,3,5,2,0] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [3,2,6,2,6,0] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [4,1,5,3,5,2] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [4,3,3,2,4,3] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [4,5,2,3,6,2] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [5,1,1,2,1,4] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [5,1,6,6,3,6] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [6,1,5,1,4,5] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [6,2,2,3,4,6] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [6,2,3,0,2,0] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [1,0,6,3,6,3,1] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [2,1,7,3,0,3,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [2,3,7,0,6,4,4] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [2,5,2,4,2,1,3] }
assert my_solution.countTestedDevices(**test_input) == 3
test_input = { "batteryPercentages": [2,5,2,7,6,5,5] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [4,2,6,4,7,6,7] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [4,2,6,6,3,3,7] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [4,4,3,0,2,6,6] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [5,2,2,3,4,6,6] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [5,4,6,0,7,2,2] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [6,6,7,0,1,7,2] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [0,5,1,4,5,0,4,8] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [1,0,7,0,7,4,5,7] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [2,5,3,4,4,8,6,5] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [2,6,3,4,5,6,2,6] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [4,5,2,1,3,7,3,5] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [6,5,4,8,6,8,3,6] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [7,4,0,8,5,5,2,0] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [7,5,3,2,3,5,8,6] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [8,0,4,3,2,6,6,1] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [8,3,0,1,0,8,6,8] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [8,6,7,1,0,1,3,7] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [0,6,8,8,0,1,2,3,4] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [2,7,9,7,2,9,0,3,9] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [8,1,9,8,5,3,4,4,1] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [8,4,0,1,1,6,5,3,5] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [8,4,1,5,8,5,8,7,9] }
assert my_solution.countTestedDevices(**test_input) == 8
test_input = { "batteryPercentages": [8,4,9,8,9,0,0,4,9] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [8,9,4,4,1,9,8,9,1] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [2,5,8,9,1,5,10,9,6,3] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [2,6,5,4,1,5,3,3,3,9] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [7,7,7,3,6,6,4,3,5,10] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [9,3,10,1,8,2,4,3,3,0] }
assert my_solution.countTestedDevices(**test_input) == 4
test_input = { "batteryPercentages": [10,10,2,0,2,7,6,7,10,4] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [0,8,7,9,4,10,4,3,7,11,7] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [1,2,3,5,6,11,3,2,11,0,8] }
assert my_solution.countTestedDevices(**test_input) == 8
test_input = { "batteryPercentages": [5,10,4,10,10,6,8,1,8,10,3] }
assert my_solution.countTestedDevices(**test_input) == 9
test_input = { "batteryPercentages": [7,10,2,7,11,8,11,4,1,4,5] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [7,11,0,4,1,10,5,3,2,0,2] }
assert my_solution.countTestedDevices(**test_input) == 5
test_input = { "batteryPercentages": [8,8,1,8,6,2,5,2,8,5,6] }
assert my_solution.countTestedDevices(**test_input) == 6
test_input = { "batteryPercentages": [8,9,10,10,1,5,4,6,7,2,4] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [9,9,2,3,2,2,9,6,11,1,10] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [10,0,6,2,6,6,11,1,8,10,5] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [1,4,7,2,12,8,1,11,5,10,2,3] }
assert my_solution.countTestedDevices(**test_input) == 7
test_input = { "batteryPercentages": [2,5,4,4,9,6,10,0,11,8,2,10] }
assert my_solution.countTestedDevices(**test_input) == 9
test_input = { "batteryPercentages": [4,11,9,8,9,11,11,5,11,6,12,11] }
assert my_solution.countTestedDevices(**test_input) == 10 | 1,702,175,400 |
weekly-contest-375-double-modular-exponentiation | https://leetcode.com/problems/double-modular-exponentiation | double-modular-exponentiation | {
"questionId": "3234",
"questionFrontendId": "2961",
"title": "Double Modular Exponentiation",
"titleSlug": "double-modular-exponentiation",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 87,
"dislikes": 12,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed 2D array variables where variables[i] = [ai, bi, ci, mi], and an integer target.
An index i is good if the following formula holds:
0 <= i < variables.length
((aibi % 10)ci) % mi == target
Return an array consisting of good indices in any order.
Example 1:
Input: variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
Output: [0,2]
Explanation: For each index i in the variables array:
1) For the index 0, variables[0] = [2,3,3,10], (23 % 10)3 % 10 = 2.
2) For the index 1, variables[1] = [3,3,3,1], (33 % 10)3 % 1 = 0.
3) For the index 2, variables[2] = [6,1,1,4], (61 % 10)1 % 4 = 2.
Therefore we return [0,2] as the answer.
Example 2:
Input: variables = [[39,3,1000,1000]], target = 17
Output: []
Explanation: For each index i in the variables array:
1) For the index 0, variables[0] = [39,3,1000,1000], (393 % 10)1000 % 1000 = 1.
Therefore we return [] as the answer.
Constraints:
1 <= variables.length <= 100
variables[i] == [ai, bi, ci, mi]
1 <= ai, bi, ci, mi <= 103
0 <= target <= 103
"""
class Solution:
def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]:
| You are given a 0-indexed 2D array variables where variables[i] = [ai, bi, ci, mi], and an integer target.
An index i is good if the following formula holds:
0 <= i < variables.length
((aibi % 10)ci) % mi == target
Return an array consisting of good indices in any order.
Example 1:
Input: variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
Output: [0,2]
Explanation: For each index i in the variables array:
1) For the index 0, variables[0] = [2,3,3,10], (23 % 10)3 % 10 = 2.
2) For the index 1, variables[1] = [3,3,3,1], (33 % 10)3 % 1 = 0.
3) For the index 2, variables[2] = [6,1,1,4], (61 % 10)1 % 4 = 2.
Therefore we return [0,2] as the answer.
Example 2:
Input: variables = [[39,3,1000,1000]], target = 17
Output: []
Explanation: For each index i in the variables array:
1) For the index 0, variables[0] = [39,3,1000,1000], (393 % 10)1000 % 1000 = 1.
Therefore we return [] as the answer.
Constraints:
1 <= variables.length <= 100
variables[i] == [ai, bi, ci, mi]
1 <= ai, bi, ci, mi <= 103
0 <= target <= 103
Please complete the code below to solve above prblem:
```python
class Solution:
def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "variables": [[2,3,3,10],[3,3,3,1],[6,1,1,4]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == [0,2]
test_input = { "variables": [[39,3,1000,1000]], "target": 17 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,2,4,2],[3,3,1,3],[2,2,2,4],[4,4,2,3],[2,4,1,3]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[9,2,8,5],[7,8,8,8],[8,9,6,1],[8,6,2,2],[3,6,3,1]], "target": 9 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[2,2,3,2],[1,3,3,2],[3,2,2,3],[3,1,2,3],[1,2,3,1],[2,2,2,2],[2,1,3,1],[3,2,2,2],[2,1,3,1],[3,3,1,3]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,2,3,4,5,6,8]
test_input = { "variables": [[1,3,2,3],[4,2,3,3],[4,1,4,4],[4,2,3,1],[4,2,1,1],[1,2,4,1],[1,1,4,2],[1,4,4,3],[1,2,2,3]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[5,4,1,3],[2,5,5,1],[5,3,4,1]], "target": 5 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,7,6,7],[7,6,6,4],[6,8,2,3],[8,3,5,8]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,1,2,3,4,5,6]
test_input = { "variables": [[3,5,1,2],[3,2,5,2],[4,4,3,2],[3,2,5,3],[1,5,1,4]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [0,1,4]
test_input = { "variables": [[1,2,1,1],[2,2,2,2],[1,1,1,2],[1,2,2,2]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,3,5,6],[8,2,9,2],[1,4,6,1],[6,4,7,7]], "target": 8 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,5,4,3],[1,3,3,1],[3,3,5,5],[4,5,5,5],[5,1,4,3],[2,5,3,4]], "target": 7 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[9,7,2,7],[9,1,8,1],[9,3,5,6],[6,1,8,4],[9,6,2,3]], "target": 8 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[10,6,8,7],[3,6,1,8]], "target": 5 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,6,5,2],[2,6,4,6],[4,6,3,6],[2,2,6,5],[6,5,5,2]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[5,6,5,1],[4,3,1,6],[5,4,4,2]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == [1]
test_input = { "variables": [[5,1,2,4],[4,5,5,5],[5,9,7,4],[7,9,6,3],[1,8,6,1],[1,1,9,9],[3,7,6,5],[2,6,2,6]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [0,2,3,5]
test_input = { "variables": [[1,3,2,5],[5,4,1,2],[2,2,3,2],[4,2,5,4],[1,5,4,1],[2,2,5,2],[3,3,2,1],[2,5,4,3],[2,1,5,1]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,2,1,3],[1,2,1,1],[2,1,3,2],[2,3,1,3],[3,3,1,1],[2,3,2,1],[2,1,3,3],[1,2,2,2],[3,2,1,3]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [7]
test_input = { "variables": [[3,3,2,2],[3,3,2,2]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,6,7,5],[6,3,1,5],[7,5,5,4],[6,2,2,4],[6,1,1,2],[2,6,5,4]], "target": 8 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[7,8,9,3],[7,8,2,8],[2,4,4,8],[8,8,4,4]], "target": 6 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[6,7,6,6],[1,2,4,8],[6,4,2,4],[3,2,4,5]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[2,3,1,1],[3,2,1,1],[2,1,2,3]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[9,8,10,1],[7,1,3,9],[6,8,9,10],[4,8,8,9]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,3,2,2],[3,6,4,6],[1,4,1,4],[5,2,5,1],[8,3,6,3],[8,4,8,3]], "target": 6 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,4,4,2],[4,4,1,2],[1,1,3,2],[3,1,4,3],[2,2,3,4],[2,3,2,4],[3,1,4,4],[1,4,1,4],[3,2,1,4]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [1,3,4,5]
test_input = { "variables": [[2,1,1,1],[2,2,2,2],[1,2,1,2],[1,1,1,1],[1,1,1,1],[2,1,2,2],[1,1,2,1],[2,2,2,2]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,1,3,4,5,6,7]
test_input = { "variables": [[3,1,2,4],[3,3,4,2],[3,4,3,4],[3,3,4,2]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [0,1,2,3]
test_input = { "variables": [[4,10,5,8],[7,7,5,8],[4,8,6,2],[6,3,3,2]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,5,3,6],[3,1,6,3],[6,4,1,5],[3,2,3,5],[1,4,7,5],[6,6,6,2],[7,5,6,3],[1,2,7,1],[1,1,6,3]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == [3]
test_input = { "variables": [[1,4,2,3],[4,3,1,4],[3,3,1,3],[1,4,1,3]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [1]
test_input = { "variables": [[1,4,2,2],[5,5,1,2],[3,4,2,3]], "target": 6 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,5,1,1],[4,8,6,8],[5,1,4,9],[4,3,1,2],[5,9,4,7],[8,7,7,1],[9,3,7,5]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,1,3,5]
test_input = { "variables": [[1,1,1,1],[2,2,2,1],[2,2,2,1],[2,2,2,2],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[5,5,6,5],[4,1,2,2],[6,2,5,3],[1,1,5,5],[3,5,6,5]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,4,1,3],[2,4,4,1],[1,1,3,1],[2,3,3,1]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [1,2,3]
test_input = { "variables": [[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,2,1,7],[3,3,7,1],[5,5,8,2],[5,1,3,1]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,5,7,2],[2,10,7,10],[6,8,2,2],[9,4,1,2],[1,7,4,1]], "target": 3 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[5,5,3,5],[4,2,5,9],[4,6,6,1],[4,5,3,6]], "target": 8 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,2,1],[1,1,3,2],[2,3,3,2],[1,2,3,2],[1,1,1,3],[2,2,1,2]], "target": 3 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,1,3,5],[4,7,1,6],[7,3,5,4],[2,4,2,7],[6,3,4,7]], "target": 7 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,2,3,7],[2,9,6,2],[3,8,9,2],[1,7,7,3],[1,3,8,1],[2,4,5,1],[3,6,3,2],[4,4,6,8]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[8,3,7,1],[7,8,3,2],[4,1,5,3],[6,6,6,3],[2,4,7,5]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,6,6],[1,1,1,2],[3,6,6,1],[4,5,5,6],[3,1,6,6],[3,2,2,1],[6,1,1,2]], "target": 6 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,2,2,1],[6,3,2,1],[2,4,3,2],[1,1,6,6],[4,6,2,1],[5,4,2,1],[1,2,6,1],[6,2,4,4]], "target": 6 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[2,2,2,2],[1,2,2,3]], "target": 3 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,1]
test_input = { "variables": [[2,5,8,2],[2,6,1,2],[7,4,8,9],[6,3,1,4],[7,1,6,7],[4,6,2,7],[8,2,2,7],[4,5,3,8],[1,2,6,4]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == [3,6]
test_input = { "variables": [[4,2,4,1],[6,1,2,6],[4,3,3,2]], "target": 8 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[6,4,3,5],[7,4,2,6],[1,4,2,1],[4,5,4,5],[7,2,2,7],[7,5,4,3],[2,7,1,3],[6,7,2,2],[4,7,4,1],[7,3,2,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [0,1,3,5]
test_input = { "variables": [[4,10,5,8],[8,8,9,8],[7,1,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]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [8]
test_input = { "variables": [[7,5,4,2],[2,1,3,6],[7,2,2,3],[1,4,7,3]], "target": 3 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[2,3,3,3],[3,2,1,3],[2,2,3,2],[3,1,2,2],[1,2,2,1],[2,3,3,3],[3,1,2,2]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [3,6]
test_input = { "variables": [[10,2,6,2],[8,10,5,7]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0]
test_input = { "variables": [[6,8,3,6],[4,8,3,1],[6,8,6,5],[7,4,7,1],[5,2,1,5],[2,3,5,7],[3,2,6,3],[4,3,7,1]], "target": 6 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[5,5,5,1],[6,1,3,2],[3,1,2,6],[2,6,6,5],[6,1,3,6],[4,2,3,1],[2,5,3,5],[6,6,6,2]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,2],[2,1,1,1],[2,1,1,1],[1,1,1,1],[2,1,2,2],[2,2,2,2],[1,1,1,1],[1,2,2,2],[1,2,1,1]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [1,2,3,4,5,6,8]
test_input = { "variables": [[9,4,4,9],[9,4,2,6],[7,5,1,4],[9,2,2,3],[6,5,1,2],[2,7,2,9],[1,8,1,6],[5,4,9,7],[8,1,7,4]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [0,1,3,5,6]
test_input = { "variables": [[1,3,1,3],[2,1,3,2],[2,2,1,1],[1,2,1,3]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,5,5,1],[4,2,6,3],[3,5,6,6],[5,3,1,1],[5,1,3,4],[6,1,6,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [4]
test_input = { "variables": [[1,2,1,2],[1,2,1,2]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,4,2,4],[5,5,3,5],[3,5,3,4],[2,4,5,5],[5,4,4,5],[2,2,2,3]], "target": 5 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,4,4,4],[5,2,4,4],[1,1,5,3],[3,4,1,2],[3,1,2,3],[4,3,3,3],[3,5,4,1],[2,1,4,5],[3,3,1,3]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [0,1,2,3,5,7,8]
test_input = { "variables": [[1,1,6,1],[3,8,4,7],[8,5,5,9],[4,9,1,3],[9,1,1,9]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,4]
test_input = { "variables": [[2,1,2,2],[2,2,2,1],[2,1,2,2],[1,2,1,1],[2,1,2,2],[2,2,2,2]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[5,7,4,2],[2,8,10,10],[4,4,7,2],[7,4,4,6]], "target": 10 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,1,2,3,4,5,6,7,8,9]
test_input = { "variables": [[9,3,6,3],[9,7,2,5],[2,8,9,9],[4,7,7,4],[2,7,3,9],[8,5,5,3],[7,5,4,3],[9,9,2,9],[9,4,8,8]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[7,5,2,3],[1,7,2,3],[9,1,7,1]], "target": 3 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[2,5,7,3],[2,6,5,1],[4,3,6,5]], "target": 3 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[7,8,1,1],[1,5,4,1],[4,7,8,9],[7,9,2,4],[5,1,4,3],[3,9,4,1],[6,6,1,8],[4,8,5,1],[1,4,5,9]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [3,4,8]
test_input = { "variables": [[5,1,6,1],[3,6,5,2],[4,2,5,4],[2,3,5,2],[2,4,3,1],[3,2,3,6],[6,2,4,6],[6,3,3,2]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,2,3,4,6,7]
test_input = { "variables": [[3,2,5,3],[9,4,2,4],[2,4,7,7],[1,4,9,2],[5,1,5,5],[9,5,6,7],[9,1,4,7]], "target": 5 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,2,2,2],[3,1,3,2],[2,1,2,2],[3,2,3,3],[1,1,2,3],[1,1,3,1]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [2,3,5]
test_input = { "variables": [[1,5,2,1],[3,5,3,2],[1,2,4,1],[1,4,1,4],[4,4,1,3],[4,2,4,5],[2,2,4,1],[2,1,3,3]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,2,7,6],[10,6,5,10],[2,4,10,7],[9,5,8,6],[10,6,3,10],[9,6,5,2],[8,10,1,2],[7,1,8,8],[7,7,4,8],[8,3,8,1]], "target": 4 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[6,6,5,2],[3,5,4,3],[2,4,3,3],[6,3,4,4],[4,1,3,6],[1,6,3,5],[3,3,5,5]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,1,2,3]
test_input = { "variables": [[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,4,3,2],[5,9,9,2]], "target": 8 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[7,7,4,3],[2,10,10,4],[8,1,9,1],[9,7,7,9],[8,9,8,5],[9,8,4,2],[1,9,3,8],[6,8,3,1]], "target": 8 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[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,5,2,3],[10,10,3,8],[1,7,8,1],[1,4,3,5]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[2,3,4,4],[2,3,7,2],[4,2,6,3],[2,3,3,6],[5,1,2,7],[7,6,7,1]], "target": 5 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,3,8,8],[2,5,6,5],[8,1,2,3],[1,4,8,7],[8,5,5,7],[6,6,3,9],[5,6,7,1],[4,7,5,1],[1,5,1,5],[5,3,2,1]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,1,7,8],[4,3,7,8],[7,4,2,2]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [2]
test_input = { "variables": [[4,4,8,8],[4,7,8,7],[1,4,8,2],[5,5,6,4],[7,8,4,3],[8,6,2,1]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == [2,3,4]
test_input = { "variables": [[2,5,2,5],[1,1,4,1],[3,2,4,3],[3,1,3,4]], "target": 1 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[2,2,1,2],[2,2,2,2],[2,2,1,2]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[7,6,4,6],[3,7,2,3],[7,7,1,7],[7,7,6,5],[6,1,1,4],[1,4,2,3],[1,2,4,2],[3,2,2,1],[7,6,2,5],[2,4,5,7]], "target": 7 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[1,1,2,1],[1,1,1,2],[1,1,2,1],[2,1,1,2],[1,1,2,1],[2,1,1,1]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[4,5,8,5],[4,2,9,9],[2,3,3,3],[8,6,3,1]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == [2]
test_input = { "variables": [[4,1,4,2],[3,4,3,4],[5,5,1,5],[5,1,1,4],[4,2,1,5],[5,2,1,1],[1,4,1,4],[1,4,5,5],[5,1,4,5],[1,2,1,2]], "target": 2 }
assert my_solution.getGoodIndices(**test_input) == []
test_input = { "variables": [[3,1,3,3],[1,2,1,2],[3,1,1,3],[2,2,1,1],[3,3,2,2],[2,3,1,1]], "target": 0 }
assert my_solution.getGoodIndices(**test_input) == [0,2,3,5] | 1,702,175,400 |
weekly-contest-375-count-subarrays-where-max-element-appears-at-least-k-times | https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times | count-subarrays-where-max-element-appears-at-least-k-times | {
"questionId": "3213",
"questionFrontendId": "2962",
"title": "Count Subarrays Where Max Element Appears at Least K Times",
"titleSlug": "count-subarrays-where-max-element-appears-at-least-k-times",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 215,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
You are given an integer array nums and a positive integer k.
Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: nums = [1,3,2,3,3], k = 2
Output: 6
Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
Example 2:
Input: nums = [1,4,2,1], k = 3
Output: 0
Explanation: No subarray contains the element 4 at least 3 times.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 106
1 <= k <= 105
"""
class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
| You are given an integer array nums and a positive integer k.
Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: nums = [1,3,2,3,3], k = 2
Output: 6
Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
Example 2:
Input: nums = [1,4,2,1], k = 3
Output: 0
Explanation: No subarray contains the element 4 at least 3 times.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 106
1 <= k <= 105
Please complete the code below to solve above prblem:
```python
class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,3,2,3,3], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 6
test_input = { "nums": [1,4,2,1], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 0
test_input = { "nums": [61,23,38,23,56,40,82,56,82,82,82,70,8,69,8,7,19,14,58,42,82,10,82,78,15,82], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 224
test_input = { "nums": [37,20,38,66,34,38,9,41,1,14,25,63,8,12,66,66,60,12,35,27,16,38,12,66,38,36,59,54,66,54,66,48,59,66,34,11,50,66,42,51,53,66,31,24,66,44,66,1,66,66,29,54], "k": 5 }
assert my_solution.countSubarrays(**test_input) == 594
test_input = { "nums": [28,5,58,91,24,91,53,9,48,85,16,70,91,91,47,91,61,4,54,61,49], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 187
test_input = { "nums": [43,105,105,88,19,82,95,32,80,37,49,105,25,105,46,54,45,84,105,88,26,20,49,54,31,105,8,103,37,32,105,105,97,27,105,89,105,47,25,87,29,105,105,105,24,105,105,48,19,91,96,71], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 454
test_input = { "nums": [107,101,180,137,191,148,83,15,188,22,100,124,69,94,191,181,171,64,136,96,91,191,107,191,191,191,107,191,191,11,140,33,4,110,83,5,86,33,42,186,191,6,42,61,94,129,191,119,191,134,43,182,191,187,63,116,172,118,50,141,124,191,125,145,191,34,191,191], "k": 9 }
assert my_solution.countSubarrays(**test_input) == 548
test_input = { "nums": [41,121,92,15,24,59,45,110,97,132,75,72,31,38,103,37,132,91,132,132,105,24], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 61
test_input = { "nums": [21,11,13,15,16,21,8,9,6,21], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 10
test_input = { "nums": [31,18,36,166,166,166,135,166,166,12,102], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 31
test_input = { "nums": [2,2,2,2,1,3,3,2,2,1,1,3,1,1,2,3,2,1,1,2,1,1,2,1,2,1,2,1,3,1,3,3], "k": 5 }
assert my_solution.countSubarrays(**test_input) == 31
test_input = { "nums": [3,2,4,4,3,4,3,1,1,1,1,3,2,1,2,1,3,4,4,1,2,4,1,1,2,3,3,3,4,4,4,1,3,1,4,1,4,4,4,2,2,3,4,3,3,2,2,2,1,2,4,2,2,4,4,1,3,2,3,2,4,4,4,2,3,4,2,4,1,4,1,4,1,4,4,3,4,2,4,3,3,2,3,3,2,3,4,2,1,1,1,2,3], "k": 23 }
assert my_solution.countSubarrays(**test_input) == 473
test_input = { "nums": [1,1,1,2,3,2,1,2,3,3,3,3,2,3,2,1,1,2,2,1,3,2,3,1,2,1,3,1,1,3,1,2,1,1,1,1,1,1,3], "k": 8 }
assert my_solution.countSubarrays(**test_input) == 148
test_input = { "nums": [54,161,161,161,161,31,74,51,87,19,161,116,108,149,6,19,155,101,161,161,154,161,78,132,62,156,112,51,161,42,92,151,142,17,110,85], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 279
test_input = { "nums": [97,102,144,55,144,128,16,93,144,9,144,15,144,144,32,68,144,60,94,56,103,5,41,27,48,144,12,86,129,144,144,99,93,96,144,73,106,76,107,144,53,21,144,144,98,32,85,97,71,127,144,9,144,144,133,125,144,135,52,144,144,46,134,23,23,144,79], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 2163
test_input = { "nums": [17,17,15,9,14,11,15,1,6,2,1,17,3,17,11,12,9,11,2,4,15,17,3,17,8,6,7,12,3,16,2,9,14,17,17,17,3,7,8,9,8,17,17,17,4,2,12,17,7,17,17,16,17,17,8,12,11,3,10,4,10,17,14,7,5,17,12,10,17,13,5,17,8,14,9,17,17,17,7,16,10,13,17,15,1,14,6,8,11,3], "k": 15 }
assert my_solution.countSubarrays(**test_input) == 1055
test_input = { "nums": [17,12,16,17,7,1,12,6,17,5,17,13,16,16,17,14,17,6,17,17,17,17,16,17,14,8,14,1,12,13,17,17,14,8,14,5,16,17,17], "k": 5 }
assert my_solution.countSubarrays(**test_input) == 404
test_input = { "nums": [98,59,98,32,45,15,98,98,98,65,98,10,98,89,87,51,42,58,76,23,85,98,98,35,18,65,39,88,56,62,10,32,8,16,32,98,6,39,14,24,98,95,68,98,77,47,98,23,69,98,49,98,7,11,92,98,27,25,85,98,45,30,50,62,46,1,79,58,69,15,59,57,85,19,98,95,98,67,52,98,59,8,98,98,98,73,86,20,98,96,21,98,79,97,52,22,98,86], "k": 12 }
assert my_solution.countSubarrays(**test_input) == 1168
test_input = { "nums": [6,50,118,27,133,133,3,121,133,72,117,133,91,57,107,93,66,122,133,6,133,122,81,20,133,133,121,133,46,25,133,133,133,17,8,49,133,116,40,133,67,9,133,133,133,133,109,41,127,13,39,133,133,133,122,58,8,125,33,62], "k": 12 }
assert my_solution.countSubarrays(**test_input) == 538
test_input = { "nums": [94,34,112,106,112,13,12,112,112,21,48,71,112,104,112,29,99,58,23,11,49,112,20,86], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 105
test_input = { "nums": [21,27,9,85,1,7,28,11,44,39,85,52,51,30,67,83,75,10,57,59,53,85,75,33,35,85,76,85,65,85,85,85,35,4,60,85,85,72,57,42,34,85,53,85,85,36,85,56,13,16,69,55,81,24,85,27,54,66,10,85,30,58,71,43,85,66,42,27,85,70], "k": 13 }
assert my_solution.countSubarrays(**test_input) == 508
test_input = { "nums": [8,14,7,1,11,10,1,13,7,14,14,6,13], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 32
test_input = { "nums": [165,135,165,46,126,165,73,165,165,155,150,165,40,38,165,145,137,106,10], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 5
test_input = { "nums": [9,3,12,6,24,23,24], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 5
test_input = { "nums": [42,85,78,92,46,63,21,14,22,37,96,50,74], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 33
test_input = { "nums": [73,54,15,4,23,70,53,65,73,73,2,72,36,71,73,69,35,18,62,73,62,73,73,50,30,73,20,71,60,9,12,57,48,73,40,20,8,73,73,73,34,59,31,49,73,5,51,36,47,38,36,58,34,42,23,28,52,73], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 1537
test_input = { "nums": [52,88,92,92,44,4,92,37,27,59,3,3,76,51,21,89,92,31,26,10,47,69,30,68,60,92,80,19,65,38,92,4,54,88,92,75,56,71,11,92,44,43,56,92,16,66,22,70], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 796
test_input = { "nums": [29,9,43,5,8,52,24,52,52,41,33,52,27,52,8,6,35,52,27,52,7,2,9,52,52,42,52,52], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 76
test_input = { "nums": [165,165,58,153,45,124,165,143,38,165,165,165,165,73,8,138,165,139,165,165,59,40,120,165,123,92,98,136,161], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 394
test_input = { "nums": [28,64,64,63,21,64,55,64,10,30,12,5,64,56,63,64,64,31,31,64,19,54,53,64,58,44,64,28,64,64,63,10,64,64,57,29,44,32,50,55,49,21,64,64,34,26,28,64,15,31,28,64,45,64,19,54,9,41,25,33,7,60,1,7,34,14,4,64,64,64,55,49,3,41,28,42,40,52,25,46,25,15], "k": 18 }
assert my_solution.countSubarrays(**test_input) == 229
test_input = { "nums": [97,23,53,33,141,150,128,153,71,39,153,35,125,143], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 32
test_input = { "nums": [144,144,87,144,18,53,129,61,34,123,141,68,37,23,94,28,64,58,16,36,27,112,144,80,77,144,97,142,8,101,14,74,37,115,115,144,99,37,144,48,28,110,13,78,144,144,83,7,112,144,144,144,78,61,87,144,144,61,144,44,123,74,144,142], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 1083
test_input = { "nums": [63,129,134,61,134,134,134,43,74,4,111], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 38
test_input = { "nums": [46,105,44,36,106,35,91,8,52,106,95,86,75,7,19,30,25,27,18,72,106,106,33,106,6,63,67,45,15,106,106,6,42,106,27,14,18,106,4,106,95,64,23,93,106,37,106,106,16,81,91,79,106,97,106,66,31,59,58], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 1637
test_input = { "nums": [78,120,110,53,53,120,116,39,64,120,120,120,120,120,97,28,92,120,101,5,46,92], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 224
test_input = { "nums": [111,111,72,111,111,56,21,95,111,101,38,77,111,111,76,58,70,72,32,72,19,111,111,63,39,111], "k": 9 }
assert my_solution.countSubarrays(**test_input) == 5
test_input = { "nums": [33,82,82,82,71,39,17,82,38,75,82,2,82,82,9,82,57,12,78,65,29,20,82,82,50,11,39,74,65,69,81,71,25,82,46,43,49,80], "k": 6 }
assert my_solution.countSubarrays(**test_input) == 219
test_input = { "nums": [83,72,17,147,147,57,147,22,120,107,59,133,123,91,147,147,72,147,31,147,147,147,96,147,18,25,13,8,18,59,46,91,15,147,25,30,6,147,113,27,84,95,38,147,147,147,106,53,127,132,55,147,22,147,124,102,17,69,131,147,4,95,59,38,147,147,41,99,142,147,136,142,57,26,16,3,142], "k": 8 }
assert my_solution.countSubarrays(**test_input) == 1336
test_input = { "nums": [52,95,173,26,173,16,4,144,173,77,22,103,162,120,77,173,173,89,173,104,62,151,173,124,173,117,113,164,3,70,15,144,161,118,139,16,157,173,154,151,37,69,60,173,173,168,148,97,173,125,161,128,85,64,70,102,100,168,56,57,157,112,119,135,42,72,135,173,173,124,143,121,75,37,162,161,102,50,173,173,107], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 1940
test_input = { "nums": [4,18,6,22,19,15,20,12,22,22,19,6,10,7,20,4,22,21,7,17,3,16,13,17,22,14,8,2,3,22,18,18,22,22,7,22,13,10,20,4,14,17,9,19,1,12,3,11,19,15,6,4,10], "k": 6 }
assert my_solution.countSubarrays(**test_input) == 347
test_input = { "nums": [55,103,123,68,16,72,104,63,40,15,180,162,82,180,131,46,180,2,120,107,100,97,180,180,17,134,180,124,40,125,15,132,4,112,180,180,28,66,180,122,99,46,15,180,180,111,30,169,132,180,10,180,180,180,107,74,95,28,180,66,180,128,61,180,118,180,28,103,37,180,88,152], "k": 8 }
assert my_solution.countSubarrays(**test_input) == 1181
test_input = { "nums": [20,6,49,60,16,54,13,2,35,6,27,62,67,56,27,6,33,51,67,42,9,59,67,14,59,7,67,34,51,5,67,48,53,20,35,67,65,34,67,67,62,7,27,18,40,10,67,67,9,8,60,12,2,67,64,67,60,28,60,26,37,2,67,33,49,23,2,36,67,6,67,7,67,44,18], "k": 8 }
assert my_solution.countSubarrays(**test_input) == 1034
test_input = { "nums": [191,2,46,65,191,166,191,156,157,181,167,123,26,191,191,104,33,126,51,191,191,191,6,152,74,84,126,191,191,162,188,38,30,191,191,125,30,56,12,151,45,163,91,168,15,125,60,4,108,27,67,97,125,147,167,152,191,159,142,105], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 647
test_input = { "nums": [2,4,11,30,23,1,8,18,4,6,30,30,30,10,30,17,24,13,17,30,25,30,30,12,15,29,24,28,21,30,25,11,1,30,9,30,21,3,10,6,30,5,5,24,21,30,17,29,21,30,3,30,8,18,17], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 584
test_input = { "nums": [141,106,141,141,94,98,33,141,2,115,11,141,9,131,104,2,141,75,141,141,24,141,28,68,141,134,141,110,15,21,141,65,108,141,35,95,94,141,117,25], "k": 10 }
assert my_solution.countSubarrays(**test_input) == 94
test_input = { "nums": [139,94,77,139,139,139,139,92,61,105,25,139,93,139,113,128,139,81,70,139,25,139,37,118,15,5,139,115,133,1], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 292
test_input = { "nums": [107,160,86,160,69,160,160,73,120,129,130,104,112,136,7,100,21,160,160,94,3,96,160,65,74,87,110,160,145,116,38,72,127,152,71,24,35,79,160,120,160,80,50,160,129,50,82,160,140,160,3,17,129,18,108,34,132,69,4,160,124,108,30,125,160,102,51,138,160,120,159,160,49,68,160,19,87,160,6,160,76,160,110], "k": 16 }
assert my_solution.countSubarrays(**test_input) == 124
test_input = { "nums": [89,9,89,82,89,11,31,45,61,56,27,15,33,6,5,89,28,73,8,48,11,89,5,89,4,65,18,20,17,38,4,36,59,34,5,81,10,6,44,19,20,86,58,60,27,89,34,29,36,88,89,10,73], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 14
test_input = { "nums": [45,40,44,51,51,33,33,38,46,38,51,40,9,29,51,40,51,36,39,36,51,24,39,51,31,50,12,50,1,51,32,51,49,12,44,19,4,26,7,51,14,4,33,36,19,18,14,20,16,11,51,51,7,18,7,10,8,8,48,51,43,41,51], "k": 10 }
assert my_solution.countSubarrays(**test_input) == 199
test_input = { "nums": [102,4,3,22,78,96,21,126,103,52,99,94,57,126,49,20,75,126,93,1,4,126,122,123,21,111,23,110,126,81,112,92,121,30,41,126,20,10,126,54,15,27,126,126,9,126,126,1,106,34,119,108,126,126,34,57,27,126,110,126,65,125,126,59,117,126,67,114,115,38,79,123,118,126,33,52,1,119,11,105,21,51,75,126,84], "k": 9 }
assert my_solution.countSubarrays(**test_input) == 1500
test_input = { "nums": [71,122,36,39,48,158,83,20,131,41,126,1,33,19,138,133,80,106,92,2,68,158,158,111,158,50,158,81,158,138,108,36,149], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 171
test_input = { "nums": [39,136,153,85,134,19,34,22,5,124,116,91,122,160,112,160,22,111,160,160,113,34,40,16,160,117,61,160,31,34,145,160], "k": 6 }
assert my_solution.countSubarrays(**test_input) == 72
test_input = { "nums": [14,14,1,8,2,11,14,14,5,1,8,1,6,3,14,14,14,2,9,10,14,2,3,14,2,5,5,11,10,11,14,5,3,10,5,3,1,3,14,5,13,9,2,9,3,5,14,14,2,3,10,4,14,14,10,14,2,10,9,2,7,9,11,14,9,5,1,5,13,6,10,1,7,4,13,13,9,10,2,10,3,8,14,3,14,13,1,14,8,12,1,6,12,14,14], "k": 14 }
assert my_solution.countSubarrays(**test_input) == 438
test_input = { "nums": [1,7,4,10,12,10,10,1,12,1,6,6,9,7,10,6,12,10,7,9,6,10,12,8,11,9,8,3,8,3,12,12,12,3,2,2,3,1,10,2,12,12,12,9,10,1,8,10,12,4,8,8,6,2,11,7,3,3,12,12,2,7,8,11,4,3,12,5,8,12,10,2,9,6,5,10,8], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 1236
test_input = { "nums": [1,7,11,13,10,13,8,6,4,11,13,6,1,6,8,10,5,13,4,2,3,7,12,5,1,1,11,13,8,9,1,8], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 262
test_input = { "nums": [73,24,67,11,66,73,73,40,4,47,25,26,48,40,27,69,73,28,23,9,16,8,63,65,73,57,73,21,43,73,19], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 408
test_input = { "nums": [7,47,50,16,35,24,61,44,53,49], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 28
test_input = { "nums": [14,89,43,1,12,64,23,89,55,23,56,69,62,89,89,86,89,89,89,76,84,57,18,54,29,50,67,69,65,3,22,26,8,77,51,74,74,40,89,89,18,74,89,26,16,27,1,66,72,22,78,20,15,14,63,77,73,23,65,89,79,32,18,89,59,16,24,39,87,78,29,84,89,49,80,69,89,44,89,89,58,6,55,38,89,53,89,3,81,28,65,39,30,14,16,89,22,4,23,84], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 2045
test_input = { "nums": [16,11,12,16,5,17,11,13,12,17,16,2,3,13,1,4,10,2,17,17,8,7,4,17,11,17,8,2,15,17,4,16,9,8,17,2,17,16,17,4,6,8,12,17,16,13,4,11,9,11,16,10,17,17,10,13,17,13,1,13,3,7,4,2,15,6,11,12,17,17,7,15,9,16,7,2,17,7,17,16,5,8], "k": 8 }
assert my_solution.countSubarrays(**test_input) == 1317
test_input = { "nums": [52,46,47,52,52,4,2,21,2,26,47,26,52,7,12,35,52,33,47,3,31,37,36,52,38,19,12,40,52,7,40,16,51,41,52,23,20,52,18,52,21,2,52,49,5,48,23,52,52], "k": 8 }
assert my_solution.countSubarrays(**test_input) == 132
test_input = { "nums": [99,155,73,80,32,69,113,37,126,155,95,155,155,48,155,43,37,68,131,68,150,155,153,155,45,59,155,155,155,77,155,155,100,4,127,155,107,151,101,104,155,155,71,147,153,37,155,18,155,100,155,153,155,155,138,4,114,153,111,83,74,144,18,64,94,155,50,45,51,122,146,50,43], "k": 8 }
assert my_solution.countSubarrays(**test_input) == 1346
test_input = { "nums": [64,156,156,119,156,35,108,82,86,18,107,156,68,83,130,86,80,8,129,95,23,7,71,131,19,156,17,21,43,156,25,156,124,51,91,156,77,88,156,156,62,105,135,142,156,156,78,156,113,156,47,156,156,22,71,49,156,57,71,156,36,84,139,156,17,49,156,121,46,7,155,156,156,156,93,150,102,81,90,52,52,91,2,63,156,49,118,77,156,156,156,79], "k": 19 }
assert my_solution.countSubarrays(**test_input) == 590
test_input = { "nums": [24,9,28,46,14,41], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 12
test_input = { "nums": [169,19], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 2
test_input = { "nums": [95,109,79,198,195,198,198,97,34,43,165,198,198,195,98,198,198,170,39,78,21,198,140,187,29,107,198,132,198,174,109,187,173,198,58,38,62,179,198,68,114,198,10,198,81,198,40,10,71,82,196,128,50,153,146,101,195], "k": 12 }
assert my_solution.countSubarrays(**test_input) == 182
test_input = { "nums": [9,15,39,33,43,47,15,29,14,12,48,37,9,37,15,48,48,3,1,48,37,39,43,29,43,15,35,2,33,48,28,37,48,45,9,36,3,48,29,14,48,11,24,30,38,18,24,12,47,31,22,10,29,46,14,48,15,29,43,48,37,48,46,14,32,33,15,42,9,12,48,20,35,44,48,4], "k": 10 }
assert my_solution.countSubarrays(**test_input) == 274
test_input = { "nums": [37,12,14,46,29,98,149,149,149,67,97,56,81,71,11,149,32,149,119,149,44,149,43,149,149,32,75,54,24,148,41], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 379
test_input = { "nums": [59,101,127,118,19,55,18,127,127,26,127,103,4,127,26,43,26,125,80,127,127,112,2,107,127,127,110,122,77,127,11,86,127,127,91,27,85,86,71,36,41,127,86,37,11], "k": 6 }
assert my_solution.countSubarrays(**test_input) == 418
test_input = { "nums": [82,82,42,51,82,64,13,16,36,49,22,52,82,10,72,9,6,42,80,74,37,80,73,10,82,31,78,22,14,11,82,60,76,67,82,2,61,52,79,72,77,12,23,33,44,11,82,4,14,65,19,66,56,11,75,82,42,82,56,77,82,81,51,48,19,70,33,51,9,78,62,31,41,46,13,82,82,77,55,24,49,82,82,8,3,44,34], "k": 9 }
assert my_solution.countSubarrays(**test_input) == 427
test_input = { "nums": [30,83,42,83,83,60,61,60,62,83,74,32,83,83,46,82,25,81,31,83,48,15,49,43,41,83,29,36,45,53,83,74,55,63,1,19,74,2,15,83,61,82,46,48,83,83,8,45,83,80,30,33,83,83,83,22,65,79,57,15,24,25,83,83,60,60,83,44,9,29,60,69,2,83,35,7,40,74,55,83,7,21,11,59,5,80], "k": 17 }
assert my_solution.countSubarrays(**test_input) == 200
test_input = { "nums": [11,25,22,14,14,29,6,28,12,14,2,15,29,2,6,27,22,29,26,29,11,1,7,27,24,29,7,29,6], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 371
test_input = { "nums": [173,97,53,181,161,119,152,97,69,181,123,84,83,9,169,135,86,27,119,181,64,147,7,181,154,43,83,181,14,181,45,77,181,83,181,53,181,117,181,27,181,174,181,47], "k": 6 }
assert my_solution.countSubarrays(**test_input) == 302
test_input = { "nums": [124,52,111,24,191,117,128,153,69,190,51,1,112,52,28,191,188,191,1,124,128,111,191,94,34,167,191,191,9,191,164,60,113,69,151,130,15,86,150,191,175,36,113,23,119,68,191,87,90,159,178,50,104,191,187,48,97,100,136,155,140,132,1,180,182,191,130,110,191,191,191,191,191,177,73,118,191,27,129,124,43,140,191,132,191,41,44,191,169,49,191,191,191,191,113,4], "k": 7 }
assert my_solution.countSubarrays(**test_input) == 2434
test_input = { "nums": [86,89,92,23,92,72,41,92,92,92,47,30,46,76,20,80,92,60,20,9,92,92,92,36,4,38,92,74,15,20,92,2,73,58,68,2,29,13,92,91,92,44,46,8,57,10,47,92,6,90,92,92,76,92,86,26,22,67,92,92,17,92,18,23,22,40,7], "k": 9 }
assert my_solution.countSubarrays(**test_input) == 738
test_input = { "nums": [75,65,37,83,80,17,69,83,83,76,64,58,13,83,18,66,25,55,25,60,83,83,83,50,70,39,82,83,47,39,74,83,75,83,34,8,81,46,52,72,45,65,46,2,9,4,23,47,83,83,59,32,54,43,53,56,83], "k": 9 }
assert my_solution.countSubarrays(**test_input) == 256
test_input = { "nums": [23,70,2,70,49,65,6,69,5,26,29,70,70,15,17,22,70,63,51,25,18,68,31,3,43,60,70,6,61,23,46,21,66,67,63,3,7,70,66,47,15,65,52,70,70,38,8,18,29,33,50,9,70,9], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 1305
test_input = { "nums": [23,12,6,3,4,7,23,23,6,23,23,9,23,2,14,11,21,23,8,9,19,10,17,23,11,3,13,23,18,3,6,7,6,19,17,14,17,7,23,21,10,22,6,23,23,3,1,20,14,7,19,20,23,19,23,15,4,23,2,6,20,23,8,6,17,14,23,6,10,23,17,6,11,8,3,6,23,16,19,16,2,19,2,23,1,16,20,4,20,12,1], "k": 11 }
assert my_solution.countSubarrays(**test_input) == 942
test_input = { "nums": [199,146,138,199,97,169,199,198,199,199,11,62,68,122,193,199,22,41,199,181,199,157,199,44,199,199,199,142,132,112,199,199,155,199,97,101,26,52,199,45,164,112,188,97,180,103,199,3,130,64,131,199,194,135,36,199,80,67,41,67,158,183,188,12,126], "k": 13 }
assert my_solution.countSubarrays(**test_input) == 420
test_input = { "nums": [25,32,40,47,35,9,39,58,67,42,77,57,77,77,34,28,13,77,15,33,77,10,64,67,35,21,61,60,74,57,77,71,28,77,48,67,17,48,77,77,77,60,26,30,77,49,77,3,77,33,75,77,20,77,77], "k": 9 }
assert my_solution.countSubarrays(**test_input) == 325
test_input = { "nums": [50,108,19,118,46,45,126,118,89,126,46,63,30,126,120,10,126,126,108,95,126,94], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 107
test_input = { "nums": [28,94,94,5,1,74,33,3,88,76,78,30], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 32
test_input = { "nums": [44,4,4,31,33,51,51,40,51,2,27,48,51,6,51,27,45,1,25,2,20,43,51,12,11,44,40,28,29,51,51,45,30,24,51,51,30,51,13,18,29,51,15,11,39], "k": 11 }
assert my_solution.countSubarrays(**test_input) == 52
test_input = { "nums": [6,30,19,32,24,8,28,2,18,32,5,31,3,31,28,30,30,22,32,22,31,1,9,2,7,32,14,24,24,6,23,6,25,32,32,22,10,11,4,2,32,18,15,1,22,20,6,26,11,13,26,22,32,30,18], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 570
test_input = { "nums": [17,41,71,95,56,88,25,95,73,95,91,95,8,43,2,52,95,88,5,49,20,48,95,84,95,44,27,95,87,32,45,95,95,95,51,56,6,5,65,21,52,56,84,95,75,33,95,62,47,95], "k": 13 }
assert my_solution.countSubarrays(**test_input) == 20
test_input = { "nums": [5,12,6,13,11,13,9,13,10,13,13,12,7,11,2,11,4,7,6,6,13,9,1,12,13,11,7,11,11,13,2,13,7,4,9,5,13,8,4,1,2,5,13,7,7,12,2,2,8,11,12,1,8,5,3,6,4,2,9,10,6,6,13,12,13,6,13,13,13,13,13,3,4,4,10,1,2,12,12,13,13,6,13,4,13,1,12,11,9,12,2,5], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 3240
test_input = { "nums": [13,16,2,27,10,2,44,44,44,28,44,44,23], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 23
test_input = { "nums": [69,46,80,10,80,48,76,15,67,1,80,80,34,4,14,15,2,38,62,31,17,56,58,17,38,29,67], "k": 4 }
assert my_solution.countSubarrays(**test_input) == 48
test_input = { "nums": [39,38,136,136,97,122,54,102,112,125,135,57,136], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 52
test_input = { "nums": [39,67,17,52,89,63,52,8,14,90,76,2,90,65,90,80,90,33,61,76,90,32,43,55,62,24,29,90,35,36,90,8,40,1,72,54,64,90,58,88,77,89,35,79,90,81,90], "k": 2 }
assert my_solution.countSubarrays(**test_input) == 822
test_input = { "nums": [16,22,10,22,4,16,16,15,3,22,22,15,7,7,21,17,16,1,10,13,16,17,2,18,2,5,11], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 70
test_input = { "nums": [120,58,118,34,32,110,94,10,119,133,70,154,151,107,124,148,154,154,24,154,6,83,20,6,3,72,154,28,148,107,154,73,126,154,41], "k": 5 }
assert my_solution.countSubarrays(**test_input) == 135
test_input = { "nums": [15,2,1,21,20,33,16,19], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 18
test_input = { "nums": [45,25], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 2
test_input = { "nums": [179,127,54,149,90,119,179,127,115,82,159,128,6,55,33,43,2,172,105,159,83,179,30,179,175,125,179,179,105,179,74,77,179,153,145,124,70,179,129,31,62,172,179,29,130,179,82,64,98,179,91,179,89,166,60,159,54,179,179,137,54,158,64,177,56,165,97,142,90,170,179,127,111,179,145,179,8], "k": 16 }
assert my_solution.countSubarrays(**test_input) == 61
test_input = { "nums": [25,41,11,41,26,30,41,34,31,41,40,23,14,41,10,34,8,15,41,10,14,41,37,20,37,35,37,8,21,30,11,7,33,3,25,1,3,38,27,26,27,20,29,41,30,7,23,15,41,41,41,25,18,41,19,41,34,35,33,41,4,41,15], "k": 3 }
assert my_solution.countSubarrays(**test_input) == 1227
test_input = { "nums": [7,6,3,9,6,3,4,4,9,7,3,3,8,9,2,4,8,8,8,6,3,2,9,9,9,4,2,6,9,3], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 396
test_input = { "nums": [158,2,138,177,96,104,175,81,46,19,85,1,174,177,115,145,32,177,174,95,96,101,177,114,115,137,77,98,15,177,125,162,177,177,111,106,112,177,174,40,177,177,176,40,177,145,177,99,177,177,163,177,143,147,177,11,142,177,44,171,52,98,177,163,140,139,61,147,71,20,177,45,172], "k": 5 }
assert my_solution.countSubarrays(**test_input) == 1642
test_input = { "nums": [3,1], "k": 1 }
assert my_solution.countSubarrays(**test_input) == 2
test_input = { "nums": [99,166,166,5,166,44,83,73,40,64,166,135,166,24,166,41,70,93,166,166,166,49,157,3,135,137,1,133,18,166,15,82,4,166,13,55,95,166,166,151,102,166,166,34,32,31,48,166,166,13,166,166,94,28,166,166,119,103,157,12,103,19,126,13,117,71,85,166,166,81,132,105,128,166,166,125,73,161,166,139,6,32,5,31,137], "k": 24 }
assert my_solution.countSubarrays(**test_input) == 49
test_input = { "nums": [121,135,135,135,57,18,7,22,135,57,96,72,23,68,32,39,135,135,135,135,51,25,100,49,72,135,99,38,126,110,52,63,48,135,135,132,111,114,135,135,24,125,135,135,120,93,55,40,135,44,135,22,135,48,35,12,116,79,80,22,135,135,111,135], "k": 20 }
assert my_solution.countSubarrays(**test_input) == 7 | 1,702,175,400 |
weekly-contest-375-count-the-number-of-good-partitions | https://leetcode.com/problems/count-the-number-of-good-partitions | count-the-number-of-good-partitions | {
"questionId": "3212",
"questionFrontendId": "2963",
"title": "Count the Number of Good Partitions",
"titleSlug": "count-the-number-of-good-partitions",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 163,
"dislikes": 1,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array nums consisting of positive integers.
A partition of an array into one or more contiguous subarrays is called good if no two subarrays contain the same number.
Return the total number of good partitions of nums.
Since the answer may be large, return it modulo 109 + 7.
Example 1:
Input: nums = [1,2,3,4]
Output: 8
Explanation: The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).
Example 2:
Input: nums = [1,1,1,1]
Output: 1
Explanation: The only possible good partition is: ([1,1,1,1]).
Example 3:
Input: nums = [1,2,1,3]
Output: 2
Explanation: The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
"""
class Solution:
def numberOfGoodPartitions(self, nums: List[int]) -> int:
| You are given a 0-indexed array nums consisting of positive integers.
A partition of an array into one or more contiguous subarrays is called good if no two subarrays contain the same number.
Return the total number of good partitions of nums.
Since the answer may be large, return it modulo 109 + 7.
Example 1:
Input: nums = [1,2,3,4]
Output: 8
Explanation: The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).
Example 2:
Input: nums = [1,1,1,1]
Output: 1
Explanation: The only possible good partition is: ([1,1,1,1]).
Example 3:
Input: nums = [1,2,1,3]
Output: 2
Explanation: The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def numberOfGoodPartitions(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [1,1,1,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [1,2,1,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [100000] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [1000000000] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [1,1,1,3,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [1,1,1,9,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [1,1,5,9,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [1,4,1,7,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [1,5,1,5,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [1,5,1,10,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [1,6,8,1,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [1,6,9,4,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [1,7,1,6,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [1,9,1,1,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [2,1,6,7,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [2,3,2,6,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [2,3,2,8,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [2,3,9,2,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [2,4,2,7,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [2,4,7,1,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [2,5,1,2,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [2,5,1,4,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [2,5,6,4,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [2,6,1,9,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [2,7,8,9,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [2,9,1,2,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [2,10,4,2,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [3,3,3,9,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [3,3,8,1,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [3,3,10,4,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [3,4,5,1,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [3,7,6,4,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [3,8,10,7,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [3,10,10,10,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [4,1,2,3,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [4,1,7,9,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [4,8,4,8,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [5,1,1,9,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [5,2,8,10,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [5,3,6,6,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [5,3,8,8,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [5,4,5,9,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [5,4,10,2,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [5,5,7,3,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [5,5,8,4,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [5,9,1,9,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [5,10,1,1,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [6,1,7,9,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [6,3,5,1,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [6,3,9,9,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [6,6,5,5,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [6,7,3,3,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [6,8,6,5,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [6,9,10,5,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [6,10,2,6,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [6,10,5,4,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [7,1,2,3,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [7,1,9,2,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [7,2,1,2,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [7,5,1,10,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [7,5,7,4,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [7,5,8,4,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [7,9,4,8,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [7,9,8,8,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [8,2,2,5,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [8,3,2,1,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [8,3,4,9,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [8,3,6,5,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [8,3,8,3,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [8,3,8,3,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [8,4,4,5,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [8,4,8,10,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [8,5,5,3,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [8,7,4,2,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [8,7,6,10,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [8,9,7,3,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [8,10,5,9,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [9,1,2,2,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [9,1,4,3,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [9,1,7,8,9] }
assert my_solution.numberOfGoodPartitions(**test_input) == 1
test_input = { "nums": [9,2,7,5,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [9,3,6,5,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [9,4,7,1,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [9,5,5,8,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [9,7,2,7,10] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [9,7,8,3,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [9,8,2,5,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [9,8,2,10,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [9,8,3,7,6] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [9,8,6,6,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2
test_input = { "nums": [9,8,8,2,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 8
test_input = { "nums": [9,8,9,5,7] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [10,1,3,2,3] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [10,1,10,3,5] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [10,2,1,4,1] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [10,3,1,3,4] }
assert my_solution.numberOfGoodPartitions(**test_input) == 4
test_input = { "nums": [10,5,6,2,8] }
assert my_solution.numberOfGoodPartitions(**test_input) == 16
test_input = { "nums": [10,6,5,10,2] }
assert my_solution.numberOfGoodPartitions(**test_input) == 2 | 1,702,175,400 |
biweekly-contest-119-find-common-elements-between-two-arrays | https://leetcode.com/problems/find-common-elements-between-two-arrays | find-common-elements-between-two-arrays | {
"questionId": "3206",
"questionFrontendId": "2956",
"title": "Find Common Elements Between Two Arrays",
"titleSlug": "find-common-elements-between-two-arrays",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 66,
"dislikes": 25,
"categoryTitle": "Algorithms"
} | """
You are given two 0-indexed integer arrays nums1 and nums2 of sizes n and m, respectively.
Consider calculating the following values:
The number of indices i such that 0 <= i < n and nums1[i] occurs at least once in nums2.
The number of indices i such that 0 <= i < m and nums2[i] occurs at least once in nums1.
Return an integer array answer of size 2 containing the two values in the above order.
Example 1:
Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
Output: [3,4]
Explanation: We calculate the values as follows:
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.
Example 2:
Input: nums1 = [3,4,2,3], nums2 = [1,5]
Output: [0,0]
Explanation: There are no common elements between the two arrays, so the two values will be 0.
Constraints:
n == nums1.length
m == nums2.length
1 <= n, m <= 100
1 <= nums1[i], nums2[i] <= 100
"""
class Solution:
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
| You are given two 0-indexed integer arrays nums1 and nums2 of sizes n and m, respectively.
Consider calculating the following values:
The number of indices i such that 0 <= i < n and nums1[i] occurs at least once in nums2.
The number of indices i such that 0 <= i < m and nums2[i] occurs at least once in nums1.
Return an integer array answer of size 2 containing the two values in the above order.
Example 1:
Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
Output: [3,4]
Explanation: We calculate the values as follows:
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.
Example 2:
Input: nums1 = [3,4,2,3], nums2 = [1,5]
Output: [0,0]
Explanation: There are no common elements between the two arrays, so the two values will be 0.
Constraints:
n == nums1.length
m == nums2.length
1 <= n, m <= 100
1 <= nums1[i], nums2[i] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "nums1": [4,3,2,3,1], "nums2": [2,2,5,2,3,6] }
assert my_solution.findIntersectionValues(**test_input) == [3,4]
test_input = { "nums1": [3,4,2,3], "nums2": [1,5] }
assert my_solution.findIntersectionValues(**test_input) == [0,0]
test_input = { "nums1": [24,28,7,27,7,27,9,24,9,10], "nums2": [12,29,9,7,5] }
assert my_solution.findIntersectionValues(**test_input) == [4,2]
test_input = { "nums1": [10,30,16,18], "nums2": [23,30,30,6,10,26,9,27,6,16,18,10,27,2,20,7,16] }
assert my_solution.findIntersectionValues(**test_input) == [4,7]
test_input = { "nums1": [7,23,27,20,21,29,7,27,27,18,7,6,20,10], "nums2": [27,27,28,24,20,4,6,17,9,29,20,14,20] }
assert my_solution.findIntersectionValues(**test_input) == [7,7]
test_input = { "nums1": [15,30,6,6], "nums2": [15,4,16,10,7,23,24,3,4,6,14,8,18,1,29,27,2,17] }
assert my_solution.findIntersectionValues(**test_input) == [3,2]
test_input = { "nums1": [24,7,8,6,22,28,22,28,7,19], "nums2": [3,7,28,7,3,3] }
assert my_solution.findIntersectionValues(**test_input) == [4,3]
test_input = { "nums1": [23,4,26,17,23,13], "nums2": [24,17,20,16,1,13,17,28,17] }
assert my_solution.findIntersectionValues(**test_input) == [2,4]
test_input = { "nums1": [5,8,18,27,16,29,27,12,1,29,16,27,22,19,14,12,11,25], "nums2": [24,8,16] }
assert my_solution.findIntersectionValues(**test_input) == [3,2]
test_input = { "nums1": [29,17,30,17,15,30,11,2,24,28,28,30,30,27,30,2,30,9,1,7], "nums2": [12,12,11,21,2,28,5,24,12,17,24,29,22,19,11,17,1,23] }
assert my_solution.findIntersectionValues(**test_input) == [10,10]
test_input = { "nums1": [4,27,12,16,16,21,26,7,19,21,24,26,12,24,22,12,16], "nums2": [1,25,8,27,23,27,27,24] }
assert my_solution.findIntersectionValues(**test_input) == [3,4]
test_input = { "nums1": [27,19,20,16,24,27,27,24], "nums2": [30,21,21,6,17,16] }
assert my_solution.findIntersectionValues(**test_input) == [1,1]
test_input = { "nums1": [3,19,21,5,24,26,22,22,5], "nums2": [23,26,20,14,30,9,10,24,19,22,19,6,3,20,22,22,5,24,24] }
assert my_solution.findIntersectionValues(**test_input) == [8,11]
test_input = { "nums1": [13,13,29,12], "nums2": [29,29,13,7,30,22] }
assert my_solution.findIntersectionValues(**test_input) == [3,3]
test_input = { "nums1": [30,4,16,14,14,14,20,15,20,30,6,10,14], "nums2": [30,16,20,2,18,10,5,6,30,20,22,18,14,23,15] }
assert my_solution.findIntersectionValues(**test_input) == [12,9]
test_input = { "nums1": [22,1,22,4,11,22,4,20,11,29,11,11,4,26,20,12,20,8,26,17], "nums2": [4,17,7,15] }
assert my_solution.findIntersectionValues(**test_input) == [4,2]
test_input = { "nums1": [30,15,16,15,11,16,26,15,21], "nums2": [22,25,27,2,26,20,18,15,26,20,16] }
assert my_solution.findIntersectionValues(**test_input) == [6,4]
test_input = { "nums1": [5,6], "nums2": [13,12,8,5,19,13,27] }
assert my_solution.findIntersectionValues(**test_input) == [1,1]
test_input = { "nums1": [27,28,15,20,5,13,28,29,24,29,20,15,5,20,20,25,9,20,24,20], "nums2": [16,20,13,24,11] }
assert my_solution.findIntersectionValues(**test_input) == [9,3]
test_input = { "nums1": [25,7,18], "nums2": [28,1,14,22,24,8,25,17] }
assert my_solution.findIntersectionValues(**test_input) == [1,1]
test_input = { "nums1": [10,15], "nums2": [4,10,15,28] }
assert my_solution.findIntersectionValues(**test_input) == [2,2]
test_input = { "nums1": [11,11,25], "nums2": [11,28,25,13,23,11] }
assert my_solution.findIntersectionValues(**test_input) == [3,3]
test_input = { "nums1": [10,30,27,8,8,5,11,12,17,13,14,27,17,19,13,20,27], "nums2": [10,14,25,2,17,29,10,9,5,30,15,27] }
assert my_solution.findIntersectionValues(**test_input) == [9,7]
test_input = { "nums1": [19,22,22,22,22,29,22,28,29], "nums2": [7,28,29,22,16,22,22,4,17,11,22,22,22,25,25] }
assert my_solution.findIntersectionValues(**test_input) == [8,8]
test_input = { "nums1": [18,1,23,1,1], "nums2": [16,9,1,4,15,11] }
assert my_solution.findIntersectionValues(**test_input) == [3,1]
test_input = { "nums1": [30,11,15,1,15,6,5,26,15,15], "nums2": [1,20,19,30,17,10,6,15] }
assert my_solution.findIntersectionValues(**test_input) == [7,4]
test_input = { "nums1": [17,6,30,30,15,30,22,2,18,22,21,21,17,19,25,30,18,30,1], "nums2": [2,16,25,5,25,1,14,11] }
assert my_solution.findIntersectionValues(**test_input) == [3,4]
test_input = { "nums1": [3,21,21,23,14], "nums2": [1,28,1,3,27,15,28,29,22,14,8,24] }
assert my_solution.findIntersectionValues(**test_input) == [2,2]
test_input = { "nums1": [8,20,29,23,29,2,2,2,20], "nums2": [2,24,20,28,11,8,6,25] }
assert my_solution.findIntersectionValues(**test_input) == [6,3]
test_input = { "nums1": [22,27,4,27,30,22,25,8,8,30,1,16,1], "nums2": [9,21,8,12] }
assert my_solution.findIntersectionValues(**test_input) == [2,1]
test_input = { "nums1": [19,11,13,1,26,25,19,24,3,10,1,11,1,15,20,20,26,13,13], "nums2": [13,23] }
assert my_solution.findIntersectionValues(**test_input) == [3,1]
test_input = { "nums1": [21,16,11,21], "nums2": [21,11,21,2,2,8,16,29,16,16,18,14,18,16,29,10,2] }
assert my_solution.findIntersectionValues(**test_input) == [4,7]
test_input = { "nums1": [15,7,23,12,23,16,18,1,16,28,28,19,7,30,19], "nums2": [9,1,10,15,23,8,8,24,30] }
assert my_solution.findIntersectionValues(**test_input) == [5,4]
test_input = { "nums1": [2,2,22,24,20,22,1,27,27,10,8,26,22,22,22,10,13,29], "nums2": [8,11,1,11] }
assert my_solution.findIntersectionValues(**test_input) == [2,2]
test_input = { "nums1": [25,29,15,15,21,14,10,23,10,18,11,30,28,16,29], "nums2": [1,16,10,2,25,1,15] }
assert my_solution.findIntersectionValues(**test_input) == [6,4]
test_input = { "nums1": [18,18,11,27,18,20,20], "nums2": [16,28,25,28,20,15,8,21,4,6,19,20,20,20,29] }
assert my_solution.findIntersectionValues(**test_input) == [2,4]
test_input = { "nums1": [1,25,15,20,25,11,4,1,1,21,17,1,19], "nums2": [19,19,9,23,1,5,28,28,17,28,3,9,8] }
assert my_solution.findIntersectionValues(**test_input) == [6,4]
test_input = { "nums1": [7,30,7,7,30,2,7,7], "nums2": [19,7,1,7,17,17,20,7,21,30,8,21,10,30,14] }
assert my_solution.findIntersectionValues(**test_input) == [7,5]
test_input = { "nums1": [7,18,13,27,13,9,22,30], "nums2": [27,29,21,30,16,13,29,5,9,16,29,27] }
assert my_solution.findIntersectionValues(**test_input) == [5,5]
test_input = { "nums1": [19,19,25,24,24,3,19,24,3], "nums2": [16,19,19,17,19,24,5,19] }
assert my_solution.findIntersectionValues(**test_input) == [6,5]
test_input = { "nums1": [19,11,3,11,22,12,23,12,29,19,25,15,23,23], "nums2": [4,29,19,23,23,10,2,10,10,15,19,20,19,12,2,19,15,29] }
assert my_solution.findIntersectionValues(**test_input) == [9,11]
test_input = { "nums1": [25,21], "nums2": [20,12,5,13,21,25,9,30,21,7,21,12,20,7] }
assert my_solution.findIntersectionValues(**test_input) == [2,4]
test_input = { "nums1": [16,17,16,20,29,16,30,24], "nums2": [1,30,24] }
assert my_solution.findIntersectionValues(**test_input) == [2,2]
test_input = { "nums1": [10,6,7,24,17,24,3,24], "nums2": [24,27,26,8,7,3,19,24,6,7,30,6] }
assert my_solution.findIntersectionValues(**test_input) == [6,7]
test_input = { "nums1": [3,26,7,6,23,22,26,8,11,23,17,26,7,2], "nums2": [13,11,10,8,4,23] }
assert my_solution.findIntersectionValues(**test_input) == [4,3]
test_input = { "nums1": [29,10,9,26,30,21,11,26,30], "nums2": [2,9,12,9,30,9,30,21,8,3,17,15,25,26,9,15] }
assert my_solution.findIntersectionValues(**test_input) == [6,8]
test_input = { "nums1": [14,29,15,12,20,27,24,29,4,29,12,6,12,4,7], "nums2": [2,19,6,29,10,20,26,11,11,19,4,12,30,22,13,4,24] }
assert my_solution.findIntersectionValues(**test_input) == [11,7]
test_input = { "nums1": [11,5,3,4,15,30,25,25,30,6,3,28,25,6,30,17,15], "nums2": [4,25,17,2,24,28,25,15,4,25,8,6,15] }
assert my_solution.findIntersectionValues(**test_input) == [10,10]
test_input = { "nums1": [5,23,17,6,5,15,29,2,7,27,5], "nums2": [28,14,1,1,27,26,23,20,6,17,11] }
assert my_solution.findIntersectionValues(**test_input) == [4,4]
test_input = { "nums1": [26,20,12,2,11,23,8,28,28,2,28,20,2,13,13,28,22], "nums2": [8,7,12,15,20] }
assert my_solution.findIntersectionValues(**test_input) == [4,3]
test_input = { "nums1": [15,6,14,24,6,22,6,24,6,6,6,16,24,3,7,6], "nums2": [11,6,18,20,12,14,17,3,11,6,2,3,17,19,3] }
assert my_solution.findIntersectionValues(**test_input) == [9,6]
test_input = { "nums1": [21,10,13,2,3,29,2,29,12,21,16,7,21,26], "nums2": [26,16,18,29,16,15,2,16,23,24,26,21,26,13,4,29,13,17,10] }
assert my_solution.findIntersectionValues(**test_input) == [11,13]
test_input = { "nums1": [5,18,7,30,16,1,24,5,1,15,28,24,25], "nums2": [20,29,16,14] }
assert my_solution.findIntersectionValues(**test_input) == [1,1]
test_input = { "nums1": [1,11,11,28,28,10,15,28,6], "nums2": [27,21,28,18,7,7,20,26,4,28,11,22,16,30,11,9,9] }
assert my_solution.findIntersectionValues(**test_input) == [5,4]
test_input = { "nums1": [27,3], "nums2": [29,29,27,1,26,21,27,1,8,3,7,24,19] }
assert my_solution.findIntersectionValues(**test_input) == [2,3]
test_input = { "nums1": [19,20,25,16,22,23,25,16,23,16,23,14], "nums2": [16,5] }
assert my_solution.findIntersectionValues(**test_input) == [3,1]
test_input = { "nums1": [9,9,5,28,22,15,11,28,5,3,15,6,16,13,29,30], "nums2": [18,12,3,5,24,15] }
assert my_solution.findIntersectionValues(**test_input) == [5,3]
test_input = { "nums1": [21,19,11,24,7,5,10], "nums2": [19,19,14,3,4,14,27,18,14,10] }
assert my_solution.findIntersectionValues(**test_input) == [2,3]
test_input = { "nums1": [6,18,18,20,5,18,1,15,18,26,28,26], "nums2": [13,12,2,24,20,28,27,20,11] }
assert my_solution.findIntersectionValues(**test_input) == [2,3]
test_input = { "nums1": [18,14,14,15,10,14,7,1,28,15], "nums2": [11,18,15,18,27,12] }
assert my_solution.findIntersectionValues(**test_input) == [3,3]
test_input = { "nums1": [29,18,29,18,27,11,11,8,4,18,11,14,5,21,21,29,11], "nums2": [25,29,15,17,27,20,9,23,11,13,26,8,11,6] }
assert my_solution.findIntersectionValues(**test_input) == [9,5]
test_input = { "nums1": [14,5,8,21,24,5,21,19,29], "nums2": [15,10,9,13,24,4,9,10,3,6,5,20,24,26,14,27,14,10,22] }
assert my_solution.findIntersectionValues(**test_input) == [4,5]
test_input = { "nums1": [2,11,11,9,25,11,27,16,28,10,18,3,22,15,16,11], "nums2": [11,3,21,9,3,13,23,9,28,25,8,28,29,2,23,12,13,14,14] }
assert my_solution.findIntersectionValues(**test_input) == [9,9]
test_input = { "nums1": [12,11,23,17,23,3,17], "nums2": [18,20,8,29,28,27,14,28,13,25,24,2,11,23] }
assert my_solution.findIntersectionValues(**test_input) == [3,2]
test_input = { "nums1": [8,18,7,7,7,24,16,8,23,23,16,16,3,16,22,18,8], "nums2": [29,3,14,22,17,22,25,25,1,23,6,23,7,12,16] }
assert my_solution.findIntersectionValues(**test_input) == [11,7]
test_input = { "nums1": [25,9,11,13,21,3,7,24,29,14,2,7,18,30,18], "nums2": [2,3,28,3,25,25,21,10,4,19,23,11,27] }
assert my_solution.findIntersectionValues(**test_input) == [5,7]
test_input = { "nums1": [5,8,12,18,5,8], "nums2": [12,19,30,16,13] }
assert my_solution.findIntersectionValues(**test_input) == [1,1]
test_input = { "nums1": [14,22,29,29,3,22,4,29,28,27], "nums2": [14,29] }
assert my_solution.findIntersectionValues(**test_input) == [4,2]
test_input = { "nums1": [28,28,11,5,18,5,18,17,21,4,9,4], "nums2": [19,6,12,17,13] }
assert my_solution.findIntersectionValues(**test_input) == [1,1]
test_input = { "nums1": [24,29,19,25,7,26,7,25,7,25,2], "nums2": [9,4,2,20,29,1,27] }
assert my_solution.findIntersectionValues(**test_input) == [2,2]
test_input = { "nums1": [19,14,14,21,14,11,21,18,11,14,18,28,4], "nums2": [25,30,1] }
assert my_solution.findIntersectionValues(**test_input) == [0,0]
test_input = { "nums1": [9,17,21,21,18,9,9,16,9,3,17,9,3], "nums2": [9,10,20,7,3,13,13,22,15] }
assert my_solution.findIntersectionValues(**test_input) == [7,2]
test_input = { "nums1": [21,14,14,14,5,11,8,7,9,3,7,3], "nums2": [2,24,28,8,15,5,3,6,14,3,19,25,5] }
assert my_solution.findIntersectionValues(**test_input) == [7,6]
test_input = { "nums1": [3,17,13,18,18,12,5,12,27,6,3,13,7,3,12,27,6], "nums2": [17,28,13,26,12,27,20,12,27,7,10,24] }
assert my_solution.findIntersectionValues(**test_input) == [9,7]
test_input = { "nums1": [18,9,30,9,3,13,25,5,30,25,13,19,25,3,28,29,9,9,9,12], "nums2": [17,20,28,30,27,1,22] }
assert my_solution.findIntersectionValues(**test_input) == [3,2]
test_input = { "nums1": [18,19,13,20,26,26,13,13,26,25,22,20,17], "nums2": [3,21,12,12,18,20,26,17,30,6,22,13] }
assert my_solution.findIntersectionValues(**test_input) == [11,6]
test_input = { "nums1": [19,10,2,18,15,24,4,11,12,24,10,10,9,12,6,10,17,22,11,12], "nums2": [10,16,7,2,27,22,19,17,11,15,27,24] }
assert my_solution.findIntersectionValues(**test_input) == [13,8]
test_input = { "nums1": [8,8], "nums2": [8,24,8,8,19,27,7,21,8,8] }
assert my_solution.findIntersectionValues(**test_input) == [2,5]
test_input = { "nums1": [22,23,22], "nums2": [22,21,3,22,17,27] }
assert my_solution.findIntersectionValues(**test_input) == [2,2]
test_input = { "nums1": [20,10], "nums2": [10,20,12] }
assert my_solution.findIntersectionValues(**test_input) == [2,2]
test_input = { "nums1": [15,28,15,17,3,6], "nums2": [3,15,17,30,18,22,4] }
assert my_solution.findIntersectionValues(**test_input) == [4,3]
test_input = { "nums1": [30,15], "nums2": [15,25,23,26,14,30,8,19,15,8,10,14,26,15,28,30] }
assert my_solution.findIntersectionValues(**test_input) == [2,5]
test_input = { "nums1": [16,11,16,24,7,9,9,24], "nums2": [19,2,9,18,25,11,1,16,24,18,20,9,24,7,9,29,16,22,15] }
assert my_solution.findIntersectionValues(**test_input) == [8,9]
test_input = { "nums1": [13,25,8,8,1,14,8,4,8,8,25,8,22], "nums2": [17,8,13,8,8,20,26,20,8,22,17,14,8,16,26,2,23,18,18,4] }
assert my_solution.findIntersectionValues(**test_input) == [10,9]
test_input = { "nums1": [9,9,9,29,11,9,18,23,9,9,26,9,23,9,9,2,28,7], "nums2": [17,6,2,11,10,23,11,30,11,24,1,11,13,9,23,25] }
assert my_solution.findIntersectionValues(**test_input) == [13,8]
test_input = { "nums1": [27,16,16,15], "nums2": [3,16,7,16,23,16,3,26,27,30,4,28,26,24,7] }
assert my_solution.findIntersectionValues(**test_input) == [3,4]
test_input = { "nums1": [19,1,26,15,15], "nums2": [6,25] }
assert my_solution.findIntersectionValues(**test_input) == [0,0]
test_input = { "nums1": [13,29,29,12,25,22,2,25,11,3,22,13,23,19,24,24,8,30], "nums2": [20,25,12,5,2,28,14,27,24,3,21,15,25,2,12,28,19,7,5] }
assert my_solution.findIntersectionValues(**test_input) == [8,9]
test_input = { "nums1": [14,14,26,25,28,26], "nums2": [4,23,9,3,1,2,27,8,22,6,24] }
assert my_solution.findIntersectionValues(**test_input) == [0,0]
test_input = { "nums1": [14,29,2,26,14,10,1,23,28,5,17,1,21,5,28,14,6,4,18], "nums2": [1,20,7,15,18,26,5,10,8,6,27] }
assert my_solution.findIntersectionValues(**test_input) == [8,6]
test_input = { "nums1": [29,3,15,7,2,20,30,15,7,29,2,21], "nums2": [15,23,22,19,21,5,7,29,23,2,17,27,21,15,6,7] }
assert my_solution.findIntersectionValues(**test_input) == [9,8]
test_input = { "nums1": [7,23,23,15,23,10,30,23,30,10,30,17,30,10,3,7,10], "nums2": [21,21] }
assert my_solution.findIntersectionValues(**test_input) == [0,0]
test_input = { "nums1": [8,13,1,13,13,12,27,21,4,4,17], "nums2": [12,13,1,27,4,9,12,8,25,29,4,8,4,29,21,28,1,8,6,6] }
assert my_solution.findIntersectionValues(**test_input) == [10,13]
test_input = { "nums1": [6,15,7,1,7,14,21,3,30,23,22,29], "nums2": [30,1,7,29,3,4] }
assert my_solution.findIntersectionValues(**test_input) == [6,5]
test_input = { "nums1": [15,10,22,22,6,8,15,8,10], "nums2": [10,4,8,15,29,6,9,22,3,3,23,3,13,8,5,8,3] }
assert my_solution.findIntersectionValues(**test_input) == [9,7]
test_input = { "nums1": [14,4,1,27,22,14,7,22,15,3,22,8], "nums2": [30,4,4,27,6,4,16,11,23,14,4,7,21,22,9,14,4,27,17,27] }
assert my_solution.findIntersectionValues(**test_input) == [8,12]
test_input = { "nums1": [23,15,15,15], "nums2": [23,17,12,15,21] }
assert my_solution.findIntersectionValues(**test_input) == [4,2]
test_input = { "nums1": [28,29,15,19,1,23,25,9,29,25,19,11,9,19], "nums2": [9,4,11,23,13,8,24,9,23] }
assert my_solution.findIntersectionValues(**test_input) == [4,5]
test_input = { "nums1": [19,24,7,2,3,10,27,10,4,4,9,29,10,7], "nums2": [23,4,7,4,27,13,2,9,23] }
assert my_solution.findIntersectionValues(**test_input) == [7,6]
test_input = { "nums1": [24,22,17,24,22,16,1,5], "nums2": [1,27,7,22,27,13,4,5,12,8,22,18,5] }
assert my_solution.findIntersectionValues(**test_input) == [4,5] | 1,702,132,200 |
biweekly-contest-119-remove-adjacent-almost-equal-characters | https://leetcode.com/problems/remove-adjacent-almost-equal-characters | remove-adjacent-almost-equal-characters | {
"questionId": "3230",
"questionFrontendId": "2957",
"title": "Remove Adjacent Almost-Equal Characters",
"titleSlug": "remove-adjacent-almost-equal-characters",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 115,
"dislikes": 16,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed string word.
In one operation, you can pick any index i of word and change word[i] to any lowercase English letter.
Return the minimum number of operations needed to remove all adjacent almost-equal characters from word.
Two characters a and b are almost-equal if a == b or a and b are adjacent in the alphabet.
Example 1:
Input: word = "aaaaa"
Output: 2
Explanation: We can change word into "acaca" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
Example 2:
Input: word = "abddez"
Output: 2
Explanation: We can change word into "ybdoez" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
Example 3:
Input: word = "zyxyxyz"
Output: 3
Explanation: We can change word into "zaxaxaz" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.
Constraints:
1 <= word.length <= 100
word consists only of lowercase English letters.
"""
class Solution:
def removeAlmostEqualCharacters(self, word: str) -> int:
| You are given a 0-indexed string word.
In one operation, you can pick any index i of word and change word[i] to any lowercase English letter.
Return the minimum number of operations needed to remove all adjacent almost-equal characters from word.
Two characters a and b are almost-equal if a == b or a and b are adjacent in the alphabet.
Example 1:
Input: word = "aaaaa"
Output: 2
Explanation: We can change word into "acaca" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
Example 2:
Input: word = "abddez"
Output: 2
Explanation: We can change word into "ybdoez" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.
Example 3:
Input: word = "zyxyxyz"
Output: 3
Explanation: We can change word into "zaxaxaz" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.
Constraints:
1 <= word.length <= 100
word consists only of lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def removeAlmostEqualCharacters(self, word: str) -> int:
``` |
my_solution = Solution()
test_input = { "word": "aaaaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abddez" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "zyxyxyz" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 3
test_input = { "word": "a" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "b" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "c" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "aa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "ab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "ac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "ba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "ca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "cb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "cc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "abb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "abc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "acb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "acc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "baa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bbb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bbc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bcb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bcc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "caa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "cab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "cac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "cba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "cbb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "cbc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "cca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "ccb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "ccc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aaaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "aaab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "aaac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aaba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "aabb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "aabc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "aaca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "aacb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "aacc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "abba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abbb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abbc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "abcb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "abcc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "acaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "acab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "acac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 0
test_input = { "word": "acba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "acbb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "acbc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "acca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "accb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "accc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "baaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "baab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "baac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "baba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "babb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "babc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "baca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bacb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bacc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bbaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bbab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bbac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bbba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bbbb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bbbc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bbca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bbcb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bbcc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bcaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bcab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bcac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bcba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bcbb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bcbc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bcca" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "bccb" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "bccc" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 2
test_input = { "word": "caaa" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "caab" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "caac" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
test_input = { "word": "caba" }
assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 | 1,702,132,200 |
biweekly-contest-119-length-of-longest-subarray-with-at-most-k-frequency | https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency | length-of-longest-subarray-with-at-most-k-frequency | {
"questionId": "3225",
"questionFrontendId": "2958",
"title": "Length of Longest Subarray With at Most K Frequency",
"titleSlug": "length-of-longest-subarray-with-at-most-k-frequency",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 139,
"dislikes": 5,
"categoryTitle": "Algorithms"
} | """
You are given an integer array nums and an integer k.
The frequency of an element x is the number of times it occurs in an array.
An array is called good if the frequency of each element in this array is less than or equal to k.
Return the length of the longest good subarray of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,1,2,3,1,2], k = 2
Output: 6
Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
It can be shown that there are no good subarrays with length more than 6.
Example 2:
Input: nums = [1,2,1,2,1,2,1,2], k = 1
Output: 2
Explanation: The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
It can be shown that there are no good subarrays with length more than 2.
Example 3:
Input: nums = [5,5,5,5,5,5,5], k = 4
Output: 4
Explanation: The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
It can be shown that there are no good subarrays with length more than 4.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= nums.length
"""
class Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
| You are given an integer array nums and an integer k.
The frequency of an element x is the number of times it occurs in an array.
An array is called good if the frequency of each element in this array is less than or equal to k.
Return the length of the longest good subarray of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,1,2,3,1,2], k = 2
Output: 6
Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
It can be shown that there are no good subarrays with length more than 6.
Example 2:
Input: nums = [1,2,1,2,1,2,1,2], k = 1
Output: 2
Explanation: The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
It can be shown that there are no good subarrays with length more than 2.
Example 3:
Input: nums = [5,5,5,5,5,5,5], k = 4
Output: 4
Explanation: The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
It can be shown that there are no good subarrays with length more than 4.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= nums.length
Please complete the code below to solve above prblem:
```python
class Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,1,2,3,1,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 6
test_input = { "nums": [1,2,1,2,1,2,1,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [5,5,5,5,5,5,5], "k": 4 }
assert my_solution.maxSubarrayLength(**test_input) == 4
test_input = { "nums": [1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [3], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [4], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [5], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [6], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [7], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [8], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [9], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [10], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [1,11], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [2,11], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [3,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [4,6], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [5,8], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [6,7], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [7,9], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [8,8], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 1
test_input = { "nums": [9,8], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [10,5], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [1,1,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,1,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,2,3], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,2,4], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,3,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,3,5], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,4,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [1,4,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,5,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,5,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,6,4], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,6,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,7,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [1,7,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,8,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,8,4], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,9,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,9,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [1,10,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,1,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,2,3], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [2,2,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,3,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,3,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,4,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,4,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,5,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [2,5,4], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,6,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [2,6,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,7,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,7,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,8,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,8,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,9,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [2,9,5], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,10,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [2,10,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,1,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [3,1,4], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,2,3], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [3,2,4], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,3,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,3,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [3,4,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,4,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,5,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,5,5], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [3,6,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,6,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,7,5], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,7,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,8,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,8,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,9,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,9,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,10,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [3,10,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,1,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,1,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,2,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,2,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,3,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,3,3], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 2
test_input = { "nums": [4,4,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,4,5], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,5,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,5,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,6,1], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,6,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,7,1], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,7,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,8,2], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,8,4], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,9,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,9,3], "k": 2 }
assert my_solution.maxSubarrayLength(**test_input) == 3
test_input = { "nums": [4,10,2], "k": 1 }
assert my_solution.maxSubarrayLength(**test_input) == 3 | 1,702,132,200 |
biweekly-contest-119-number-of-possible-sets-of-closing-branches | https://leetcode.com/problems/number-of-possible-sets-of-closing-branches | number-of-possible-sets-of-closing-branches | {
"questionId": "3217",
"questionFrontendId": "2959",
"title": "Number of Possible Sets of Closing Branches",
"titleSlug": "number-of-possible-sets-of-closing-branches",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 110,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
There is a company with n branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.
The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (possibly none). However, they want to ensure that the remaining branches have a distance of at most maxDistance from each other.
The distance between two branches is the minimum total traveled length needed to reach one branch from another.
You are given integers n, maxDistance, and a 0-indexed 2D array roads, where roads[i] = [ui, vi, wi] represents the undirected road between branches ui and vi with length wi.
Return the number of possible sets of closing branches, so that any branch has a distance of at most maxDistance from any other.
Note that, after closing a branch, the company will no longer have access to any roads connected to it.
Note that, multiple roads are allowed.
Example 1:
Input: n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]]
Output: 5
Explanation: The possible sets of closing branches are:
- The set [2], after closing, active branches are [0,1] and they are reachable to each other within distance 2.
- The set [0,1], after closing, the active branch is [2].
- The set [1,2], after closing, the active branch is [0].
- The set [0,2], after closing, the active branch is [1].
- The set [0,1,2], after closing, there are no active branches.
It can be proven, that there are only 5 possible sets of closing branches.
Example 2:
Input: n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]]
Output: 7
Explanation: The possible sets of closing branches are:
- The set [], after closing, active branches are [0,1,2] and they are reachable to each other within distance 4.
- The set [0], after closing, active branches are [1,2] and they are reachable to each other within distance 2.
- The set [1], after closing, active branches are [0,2] and they are reachable to each other within distance 2.
- The set [0,1], after closing, the active branch is [2].
- The set [1,2], after closing, the active branch is [0].
- The set [0,2], after closing, the active branch is [1].
- The set [0,1,2], after closing, there are no active branches.
It can be proven, that there are only 7 possible sets of closing branches.
Example 3:
Input: n = 1, maxDistance = 10, roads = []
Output: 2
Explanation: The possible sets of closing branches are:
- The set [], after closing, the active branch is [0].
- The set [0], after closing, there are no active branches.
It can be proven, that there are only 2 possible sets of closing branches.
Constraints:
1 <= n <= 10
1 <= maxDistance <= 105
0 <= roads.length <= 1000
roads[i].length == 3
0 <= ui, vi <= n - 1
ui != vi
1 <= wi <= 1000
All branches are reachable from each other by traveling some roads.
"""
class Solution:
def numberOfSets(self, n: int, maxDistance: int, roads: List[List[int]]) -> int:
| There is a company with n branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.
The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (possibly none). However, they want to ensure that the remaining branches have a distance of at most maxDistance from each other.
The distance between two branches is the minimum total traveled length needed to reach one branch from another.
You are given integers n, maxDistance, and a 0-indexed 2D array roads, where roads[i] = [ui, vi, wi] represents the undirected road between branches ui and vi with length wi.
Return the number of possible sets of closing branches, so that any branch has a distance of at most maxDistance from any other.
Note that, after closing a branch, the company will no longer have access to any roads connected to it.
Note that, multiple roads are allowed.
Example 1:
Input: n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]]
Output: 5
Explanation: The possible sets of closing branches are:
- The set [2], after closing, active branches are [0,1] and they are reachable to each other within distance 2.
- The set [0,1], after closing, the active branch is [2].
- The set [1,2], after closing, the active branch is [0].
- The set [0,2], after closing, the active branch is [1].
- The set [0,1,2], after closing, there are no active branches.
It can be proven, that there are only 5 possible sets of closing branches.
Example 2:
Input: n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]]
Output: 7
Explanation: The possible sets of closing branches are:
- The set [], after closing, active branches are [0,1,2] and they are reachable to each other within distance 4.
- The set [0], after closing, active branches are [1,2] and they are reachable to each other within distance 2.
- The set [1], after closing, active branches are [0,2] and they are reachable to each other within distance 2.
- The set [0,1], after closing, the active branch is [2].
- The set [1,2], after closing, the active branch is [0].
- The set [0,2], after closing, the active branch is [1].
- The set [0,1,2], after closing, there are no active branches.
It can be proven, that there are only 7 possible sets of closing branches.
Example 3:
Input: n = 1, maxDistance = 10, roads = []
Output: 2
Explanation: The possible sets of closing branches are:
- The set [], after closing, the active branch is [0].
- The set [0], after closing, there are no active branches.
It can be proven, that there are only 2 possible sets of closing branches.
Constraints:
1 <= n <= 10
1 <= maxDistance <= 105
0 <= roads.length <= 1000
roads[i].length == 3
0 <= ui, vi <= n - 1
ui != vi
1 <= wi <= 1000
All branches are reachable from each other by traveling some roads.
Please complete the code below to solve above prblem:
```python
class Solution:
def numberOfSets(self, n: int, maxDistance: int, roads: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "n": 3, "maxDistance": 5, "roads": [[0,1,2],[1,2,10],[0,2,10]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 3, "maxDistance": 5, "roads": [[0,1,20],[0,1,10],[1,2,2],[0,2,2]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 1, "maxDistance": 10, "roads": [] }
assert my_solution.numberOfSets(**test_input) == 2
test_input = { "n": 3, "maxDistance": 12, "roads": [[1,0,11],[1,0,16],[0,2,13]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 3, "maxDistance": 3, "roads": [[2,0,14],[1,0,15],[1,0,7]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 3, "maxDistance": 19, "roads": [[1,0,7],[0,2,18]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 3, "maxDistance": 5, "roads": [[2,0,4],[1,0,3],[1,0,2]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 4, "maxDistance": 3, "roads": [[2,1,8],[1,0,3],[0,3,8]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 5, "maxDistance": 20, "roads": [[3,2,20],[1,0,10],[0,2,19],[0,3,13],[0,4,19]] }
assert my_solution.numberOfSets(**test_input) == 12
test_input = { "n": 2, "maxDistance": 30, "roads": [[1,0,33]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 3, "maxDistance": 12, "roads": [[2,1,4],[0,1,4],[0,2,6]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 3, "maxDistance": 3, "roads": [[1,0,4],[0,2,5]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 3, "maxDistance": 27, "roads": [[2,1,23],[0,1,14],[0,2,18]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 4, "maxDistance": 20, "roads": [[2,0,16],[0,1,13],[0,3,11]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 3, "maxDistance": 22, "roads": [[1,0,21],[2,1,28]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 2, "maxDistance": 2, "roads": [[1,0,6]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 3, "maxDistance": 22, "roads": [[1,0,16],[1,0,12],[0,2,14]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 2, "maxDistance": 8, "roads": [[1,0,9]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 2, "maxDistance": 18, "roads": [[1,0,3]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 4, "maxDistance": 27, "roads": [[3,2,3],[0,1,27],[0,2,6],[0,3,17]] }
assert my_solution.numberOfSets(**test_input) == 10
test_input = { "n": 5, "maxDistance": 14, "roads": [[1,0,13],[2,0,19],[0,3,16],[0,4,18]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 5, "maxDistance": 8, "roads": [[1,0,3],[3,1,10],[4,0,6],[2,0,9],[3,2,11],[4,0,12]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 2, "maxDistance": 4, "roads": [[1,0,7]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 4, "maxDistance": 39, "roads": [[2,0,44],[3,2,42],[2,1,45]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 5, "maxDistance": 2, "roads": [[4,1,11],[3,1,5],[1,0,4],[0,2,7]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 3, "maxDistance": 9, "roads": [[1,0,17],[0,2,22]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 2, "maxDistance": 1, "roads": [[1,0,3]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 3, "maxDistance": 39, "roads": [[1,0,28],[0,2,35]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 4, "maxDistance": 17, "roads": [[2,1,28],[2,0,6],[1,0,28],[1,0,24],[1,0,18],[1,0,25],[0,3,10]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 5, "maxDistance": 16, "roads": [[2,1,27],[3,0,22],[2,1,4],[1,0,11],[2,1,48],[1,0,40],[4,2,33],[4,3,44],[1,0,1]] }
assert my_solution.numberOfSets(**test_input) == 9
test_input = { "n": 3, "maxDistance": 23, "roads": [[2,1,20],[0,1,12],[0,2,10]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 5, "maxDistance": 3, "roads": [[4,0,5],[1,0,2],[3,0,5],[3,0,4],[4,2,5],[4,2,1]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 3, "maxDistance": 5, "roads": [[1,0,6],[2,0,7]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 3, "maxDistance": 21, "roads": [[2,1,30],[0,1,36],[0,2,44]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 5, "maxDistance": 25, "roads": [[1,0,17],[1,0,1],[2,1,24],[3,2,12],[1,0,7],[3,2,4],[2,1,15],[0,4,14]] }
assert my_solution.numberOfSets(**test_input) == 14
test_input = { "n": 2, "maxDistance": 3, "roads": [[1,0,6]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 3, "maxDistance": 4, "roads": [[1,0,6],[2,1,6],[2,0,2]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 4, "maxDistance": 21, "roads": [[3,2,18],[1,0,15],[2,1,15],[3,0,19],[3,2,19]] }
assert my_solution.numberOfSets(**test_input) == 9
test_input = { "n": 4, "maxDistance": 1, "roads": [[1,0,4],[1,0,2],[3,1,2],[2,1,1],[1,0,3],[2,0,3]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 3, "maxDistance": 33, "roads": [[2,1,2],[1,0,40],[2,1,43]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 4, "maxDistance": 29, "roads": [[2,1,20],[1,0,38],[2,1,15],[2,0,32],[0,3,18]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 3, "maxDistance": 1, "roads": [[2,1,4],[2,0,2],[1,0,12]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 2, "maxDistance": 2, "roads": [[1,0,3]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 3, "maxDistance": 13, "roads": [[1,0,18],[2,0,1],[2,1,2]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 3, "maxDistance": 1, "roads": [[1,0,23],[0,2,37]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 3, "maxDistance": 18, "roads": [[2,0,39],[0,1,47]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 4, "maxDistance": 6, "roads": [[3,0,6],[0,1,3],[0,2,9]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 3, "maxDistance": 35, "roads": [[1,0,10],[1,0,15],[0,2,32]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 5, "maxDistance": 10, "roads": [[4,0,38],[4,0,11],[2,0,24],[3,0,5],[2,1,18],[2,0,38],[1,0,7],[2,1,3],[2,1,2],[3,1,36]] }
assert my_solution.numberOfSets(**test_input) == 10
test_input = { "n": 5, "maxDistance": 16, "roads": [[2,0,32],[4,0,17],[2,0,22],[3,1,38],[3,0,13],[3,1,34],[3,1,36],[1,0,36],[3,1,18],[3,2,10]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 2, "maxDistance": 4, "roads": [[1,0,18]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 5, "maxDistance": 13, "roads": [[3,0,5],[2,1,3],[1,0,6],[3,2,19],[2,1,29],[2,1,30],[1,0,5],[2,0,29],[4,3,15],[2,1,23]] }
assert my_solution.numberOfSets(**test_input) == 12
test_input = { "n": 3, "maxDistance": 23, "roads": [[2,1,12],[1,0,8],[2,1,7]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 5, "maxDistance": 38, "roads": [[2,0,8],[2,1,10],[0,3,25],[0,4,48]] }
assert my_solution.numberOfSets(**test_input) == 11
test_input = { "n": 2, "maxDistance": 4, "roads": [[1,0,2]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 4, "maxDistance": 12, "roads": [[2,1,18],[0,1,25],[0,2,24],[0,3,16]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 4, "maxDistance": 1, "roads": [[2,0,1],[3,2,2],[2,0,2],[2,0,1],[1,0,1],[1,0,1]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 3, "maxDistance": 1, "roads": [[1,0,1],[0,2,1]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 2, "maxDistance": 3, "roads": [[1,0,24]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 2, "maxDistance": 10, "roads": [[1,0,13]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 3, "maxDistance": 1, "roads": [[2,1,1],[0,1,1],[0,2,1]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 5, "maxDistance": 13, "roads": [[1,0,17],[1,0,21],[3,0,9],[1,0,10],[2,0,11],[4,1,12],[2,1,11],[1,0,18]] }
assert my_solution.numberOfSets(**test_input) == 12
test_input = { "n": 2, "maxDistance": 19, "roads": [[1,0,28]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 3, "maxDistance": 8, "roads": [[2,0,7],[2,0,28],[0,1,34]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 5, "maxDistance": 7, "roads": [[1,0,32],[0,2,35],[0,3,20],[0,4,27]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 4, "maxDistance": 1, "roads": [[1,0,4],[1,0,3],[3,0,4],[1,0,7],[0,2,11]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 2, "maxDistance": 34, "roads": [[1,0,11]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 5, "maxDistance": 10, "roads": [[2,1,32],[4,3,14],[3,2,3],[2,1,21],[3,1,37],[2,1,30],[0,1,18],[0,2,26],[0,3,19],[0,4,23]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 5, "maxDistance": 14, "roads": [[2,0,19],[3,1,24],[4,3,10],[4,1,15],[0,1,21],[0,3,21],[0,4,12]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 4, "maxDistance": 30, "roads": [[2,0,5],[0,1,27],[0,3,24]] }
assert my_solution.numberOfSets(**test_input) == 9
test_input = { "n": 2, "maxDistance": 6, "roads": [[1,0,23]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 5, "maxDistance": 6, "roads": [[4,0,17],[2,1,7],[4,1,23],[1,0,1],[1,0,19],[0,3,20]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 3, "maxDistance": 5, "roads": [[1,0,1],[1,0,3],[2,0,4]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 4, "maxDistance": 19, "roads": [[3,2,21],[2,1,3],[0,1,15],[0,2,22],[0,3,8]] }
assert my_solution.numberOfSets(**test_input) == 9
test_input = { "n": 2, "maxDistance": 5, "roads": [[1,0,4]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 4, "maxDistance": 18, "roads": [[2,1,7],[2,0,5],[0,3,10]] }
assert my_solution.numberOfSets(**test_input) == 10
test_input = { "n": 5, "maxDistance": 35, "roads": [[1,0,39],[1,0,3],[3,1,45],[2,0,21],[3,2,40],[3,0,27],[2,1,44],[4,2,6],[4,2,45],[3,0,22]] }
assert my_solution.numberOfSets(**test_input) == 14
test_input = { "n": 4, "maxDistance": 2, "roads": [[1,0,2],[1,0,3],[1,0,15],[1,0,7],[0,2,4],[0,3,6]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 4, "maxDistance": 5, "roads": [[2,0,1],[1,0,3],[0,3,4]] }
assert my_solution.numberOfSets(**test_input) == 10
test_input = { "n": 3, "maxDistance": 19, "roads": [[1,0,9],[0,2,4]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 4, "maxDistance": 1, "roads": [[3,0,4],[2,1,4],[1,0,4]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 2, "maxDistance": 29, "roads": [[1,0,18]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 2, "maxDistance": 11, "roads": [[1,0,28]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 2, "maxDistance": 12, "roads": [[1,0,10]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 5, "maxDistance": 9, "roads": [[3,2,23],[4,3,11],[1,0,16],[2,0,11],[2,0,16],[1,0,20],[4,0,16],[2,0,36],[3,0,7]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 5, "maxDistance": 17, "roads": [[3,2,22],[3,2,3],[4,0,19],[3,0,21],[4,3,4],[1,0,24],[3,0,7],[3,2,12],[1,0,17]] }
assert my_solution.numberOfSets(**test_input) == 14
test_input = { "n": 4, "maxDistance": 5, "roads": [[2,0,26],[1,0,15],[3,2,17]] }
assert my_solution.numberOfSets(**test_input) == 5
test_input = { "n": 4, "maxDistance": 4, "roads": [[1,0,11],[0,2,3],[0,3,3]] }
assert my_solution.numberOfSets(**test_input) == 7
test_input = { "n": 5, "maxDistance": 13, "roads": [[3,1,16],[0,1,3],[0,2,19],[0,3,10],[0,4,2]] }
assert my_solution.numberOfSets(**test_input) == 13
test_input = { "n": 4, "maxDistance": 5, "roads": [[1,0,21],[1,0,13],[1,0,19],[2,1,15],[1,0,17],[1,0,3],[0,3,1]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 5, "maxDistance": 25, "roads": [[1,0,18],[3,0,20],[2,0,17],[2,1,21],[0,4,3]] }
assert my_solution.numberOfSets(**test_input) == 16
test_input = { "n": 5, "maxDistance": 15, "roads": [[2,1,33],[1,0,18],[2,0,16],[3,1,37],[3,0,26],[0,4,18]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 4, "maxDistance": 1, "roads": [[1,0,1],[1,0,1],[0,2,1],[0,3,1]] }
assert my_solution.numberOfSets(**test_input) == 8
test_input = { "n": 4, "maxDistance": 10, "roads": [[2,0,22],[2,1,6],[2,0,21],[2,1,27],[3,1,12]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 2, "maxDistance": 13, "roads": [[1,0,21]] }
assert my_solution.numberOfSets(**test_input) == 3
test_input = { "n": 4, "maxDistance": 31, "roads": [[3,1,7],[2,1,10],[2,0,25],[2,0,27]] }
assert my_solution.numberOfSets(**test_input) == 9
test_input = { "n": 3, "maxDistance": 17, "roads": [[1,0,8],[1,0,8],[0,2,14]] }
assert my_solution.numberOfSets(**test_input) == 6
test_input = { "n": 2, "maxDistance": 3, "roads": [[1,0,3]] }
assert my_solution.numberOfSets(**test_input) == 4
test_input = { "n": 5, "maxDistance": 5, "roads": [[4,1,37],[4,1,7],[2,1,6],[3,2,8],[2,1,35],[1,0,28],[4,1,3],[2,1,2]] }
assert my_solution.numberOfSets(**test_input) == 9
test_input = { "n": 4, "maxDistance": 1, "roads": [[2,0,2],[2,0,1],[0,1,2],[0,3,2]] }
assert my_solution.numberOfSets(**test_input) == 6 | 1,702,132,200 |
weekly-contest-374-find-the-peaks | https://leetcode.com/problems/find-the-peaks | find-the-peaks | {
"questionId": "3221",
"questionFrontendId": "2951",
"title": "Find the Peaks",
"titleSlug": "find-the-peaks",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 91,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.
Return an array that consists of indices of peaks in the given array in any order.
Notes:
A peak is defined as an element that is strictly greater than its neighboring elements.
The first and last elements of the array are not a peak.
Example 1:
Input: mountain = [2,4,4]
Output: []
Explanation: mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array.
mountain[1] also can not be a peak because it is not strictly greater than mountain[2].
So the answer is [].
Example 2:
Input: mountain = [1,4,3,8,5]
Output: [1,3]
Explanation: mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array.
mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1].
But mountain [1] and mountain[3] are strictly greater than their neighboring elements.
So the answer is [1,3].
Constraints:
3 <= mountain.length <= 100
1 <= mountain[i] <= 100
"""
class Solution:
def findPeaks(self, mountain: List[int]) -> List[int]:
| You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.
Return an array that consists of indices of peaks in the given array in any order.
Notes:
A peak is defined as an element that is strictly greater than its neighboring elements.
The first and last elements of the array are not a peak.
Example 1:
Input: mountain = [2,4,4]
Output: []
Explanation: mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array.
mountain[1] also can not be a peak because it is not strictly greater than mountain[2].
So the answer is [].
Example 2:
Input: mountain = [1,4,3,8,5]
Output: [1,3]
Explanation: mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array.
mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1].
But mountain [1] and mountain[3] are strictly greater than their neighboring elements.
So the answer is [1,3].
Constraints:
3 <= mountain.length <= 100
1 <= mountain[i] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def findPeaks(self, mountain: List[int]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "mountain": [2,4,4] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,4,3,8,5] }
assert my_solution.findPeaks(**test_input) == [1,3]
test_input = { "mountain": [1,1,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,1,3] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,1,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,2,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,4,1] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [1,4,3] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [1,5,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,6,4] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,1,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [2,1,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [2,2,3] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [2,2,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [2,3,2] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,3,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [2,4,3] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,4,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [2,6,4] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [3,3,3] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [3,3,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [3,4,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [3,5,1] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [3,5,3] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [3,5,4] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [3,5,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,2,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,2,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,2,4] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,2,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,4,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,4,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,4,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,5,4] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [4,6,1] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [4,6,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,1,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,2,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,2,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,2,4] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,3,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,5,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,5,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,5,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,6,1] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [5,6,4] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [6,1,1] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,1,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,1,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,2,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,2,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,3,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,3,3] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,3,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,4,3] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,5,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,5,4] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [6,6,4] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,1,1,4] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,1,7,7] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,3,6,5] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [1,4,7,8] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,6,6,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [1,8,1,8] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,2,1,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [2,3,7,6] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [2,5,4,5] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,7,1,2] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,7,2,6] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,7,5,3] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [2,7,7,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [3,1,2,5] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [3,3,4,2] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [3,3,7,8] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [3,4,2,4] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [3,4,5,4] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [3,4,7,6] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [3,5,5,3] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [3,6,4,7] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [3,8,5,5] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [4,2,4,3] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [4,2,6,8] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,3,3,8] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [4,4,8,7] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [4,5,1,1] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [4,6,1,7] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [4,6,2,1] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [4,6,2,2] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [5,1,7,6] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [5,3,2,2] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,3,6,3] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [5,3,8,3] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [5,4,4,6] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,4,4,8] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,5,1,7] }
assert my_solution.findPeaks(**test_input) == []
test_input = { "mountain": [5,5,8,2] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [5,6,7,4] }
assert my_solution.findPeaks(**test_input) == [2]
test_input = { "mountain": [5,7,4,3] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [5,8,7,8] }
assert my_solution.findPeaks(**test_input) == [1]
test_input = { "mountain": [6,2,8,6] }
assert my_solution.findPeaks(**test_input) == [2] | 1,701,570,600 |
weekly-contest-374-minimum-number-of-coins-to-be-added | https://leetcode.com/problems/minimum-number-of-coins-to-be-added | minimum-number-of-coins-to-be-added | {
"questionId": "3231",
"questionFrontendId": "2952",
"title": "Minimum Number of Coins to be Added",
"titleSlug": "minimum-number-of-coins-to-be-added",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 227,
"dislikes": 40,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array coins, representing the values of the coins available, and an integer target.
An integer x is obtainable if there exists a subsequence of coins that sums to x.
Return the minimum number of coins of any value that need to be added to the array so that every integer in the range [1, target] is obtainable.
A subsequence of an array is a new non-empty array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Example 1:
Input: coins = [1,4,10], target = 19
Output: 2
Explanation: We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array.
Example 2:
Input: coins = [1,4,10,5,7,19], target = 19
Output: 1
Explanation: We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array.
Example 3:
Input: coins = [1,1,1], target = 20
Output: 3
Explanation: We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16].
It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.
Constraints:
1 <= target <= 105
1 <= coins.length <= 105
1 <= coins[i] <= target
"""
class Solution:
def minimumAddedCoins(self, coins: List[int], target: int) -> int:
| You are given a 0-indexed integer array coins, representing the values of the coins available, and an integer target.
An integer x is obtainable if there exists a subsequence of coins that sums to x.
Return the minimum number of coins of any value that need to be added to the array so that every integer in the range [1, target] is obtainable.
A subsequence of an array is a new non-empty array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Example 1:
Input: coins = [1,4,10], target = 19
Output: 2
Explanation: We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array.
Example 2:
Input: coins = [1,4,10,5,7,19], target = 19
Output: 1
Explanation: We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array.
Example 3:
Input: coins = [1,1,1], target = 20
Output: 3
Explanation: We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16].
It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.
Constraints:
1 <= target <= 105
1 <= coins.length <= 105
1 <= coins[i] <= target
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumAddedCoins(self, coins: List[int], target: int) -> int:
``` |
my_solution = Solution()
test_input = { "coins": [1,4,10], "target": 19 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [1,4,10,5,7,19], "target": 19 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [1,1,1], "target": 20 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [1], "target": 100000 }
assert my_solution.minimumAddedCoins(**test_input) == 16
test_input = { "coins": [100000], "target": 100000 }
assert my_solution.minimumAddedCoins(**test_input) == 17
test_input = { "coins": [2], "target": 5 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [5,6,7], "target": 10 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [5,6,7], "target": 15 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [4,11,13,15,7,5,12,11,5,9], "target": 34 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [8,12,9], "target": 27 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [2,13,7,1,11], "target": 35 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [10,3,5,11,6], "target": 27 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [6,6,6,15,4], "target": 31 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [6,15,6], "target": 22 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [8,14,15,4,14,15,8,10,8], "target": 42 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [9,14,14,9,14,5,12,10,11], "target": 17 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [14,5,13,3,7,10,10,10], "target": 32 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [8,6,7,12], "target": 26 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [15,1,12], "target": 43 }
assert my_solution.minimumAddedCoins(**test_input) == 4
test_input = { "coins": [4,1,4,10], "target": 16 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [10,2,13,5,7,15], "target": 26 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [10,1,10], "target": 10 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [9,5,13,8], "target": 30 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [13,9,4,5], "target": 37 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [1,15,5,12,13,10,14,8,1,7], "target": 29 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [14,14,6,2,9,1,4,10], "target": 38 }
assert my_solution.minimumAddedCoins(**test_input) == 0
test_input = { "coins": [7,10,6,14,10,11,2], "target": 45 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [6,3,2,6,8,9,13,3,12,13], "target": 47 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [8,1,9,2,15], "target": 34 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [5,13,9,11,6,1], "target": 27 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [10,15,7,14,2,2,12,14,13], "target": 45 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [9,3,10,3,8,2,7,11,1], "target": 26 }
assert my_solution.minimumAddedCoins(**test_input) == 0
test_input = { "coins": [9,11,2,5,2,7,11], "target": 28 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [5,5,15,3,13], "target": 17 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [2,2,9,10,7,15,4,3,9,15], "target": 16 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [3,1,12,15,5,10], "target": 34 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [12,7,5,2,12], "target": 50 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [11,6,10,3,1,7,11], "target": 44 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [13,12,1,11,3,4,11,9,13,13], "target": 41 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [6,4,1,9,9,2,10,7], "target": 48 }
assert my_solution.minimumAddedCoins(**test_input) == 0
test_input = { "coins": [10,4,4,3,9,6,8,4,7,7], "target": 22 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [15,9,5,7,4,13], "target": 19 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [13,11,8,6,11], "target": 16 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [8,14,15,9,8,10,13,7,3], "target": 42 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [6,14,7,4,10,9,10,9,7], "target": 22 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [4,6,7,15,13,14,5,7], "target": 16 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [10,12,5], "target": 32 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [8,5,14,13,13,11,14,13], "target": 43 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [3,14,4,2,10,3,7], "target": 50 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [1,3,11,9,2,10,6,12], "target": 12 }
assert my_solution.minimumAddedCoins(**test_input) == 0
test_input = { "coins": [2,5,4,12,6,7,11,15], "target": 17 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [7,12,10,15,6,15,14,2,9,12], "target": 24 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [4,7,15,10,14], "target": 38 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [10,1,12,9], "target": 16 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [5,8,12,6,15,13,11,5], "target": 35 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [6,2,6], "target": 39 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [15,10,5,4,7,12,12,5,11], "target": 30 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [9,10,7,12,10,4], "target": 35 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [4,4,2], "target": 8 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [13,4,15,1,8], "target": 25 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [14,7,7,1,6,14,3,15,13], "target": 18 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [8,2,14,2,3,10,15,5], "target": 31 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [3,7,12,10,11,5,3], "target": 36 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [5,3,14,8,10], "target": 33 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [9,14,9,14,4,1,4,12,12], "target": 41 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [6,3,9,7,3,4,4,15,15,10], "target": 47 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [4,9,6], "target": 43 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [12,9,13,12,10,4,9,9,4], "target": 28 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [6,8,2,13,1,5,6], "target": 31 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [9,8,9,9,3,5,10,15,1], "target": 45 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [1,10,15,15], "target": 24 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [9,12,2], "target": 43 }
assert my_solution.minimumAddedCoins(**test_input) == 4
test_input = { "coins": [14,13,10,2,2], "target": 16 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [11,5,5,13,4,13,10,3,4], "target": 21 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [8,9,1,5,8,7,6,8,6], "target": 47 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [4,10,9], "target": 18 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [7,9,7,6,8,11], "target": 50 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [11,6,6,14,12,2], "target": 46 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [8,9,2], "target": 31 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [12,1,4,3,5,3], "target": 18 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [4,3,13], "target": 34 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [9,11,3], "target": 22 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [1,11,15,1,10,13,7,6,12], "target": 28 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [1,10,8,7,12], "target": 19 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [1,5,15,13,8,4,5,7], "target": 29 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [3,8,13,8,5,3,7,2,9,8], "target": 50 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [4,13,12], "target": 47 }
assert my_solution.minimumAddedCoins(**test_input) == 4
test_input = { "coins": [9,3,10,9,11], "target": 48 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [8,7,6,1,9,5,5], "target": 13 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [11,9,1,15], "target": 16 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [8,13,8], "target": 27 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [7,10,11,3,10,14], "target": 36 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [14,9,6,11,13,8,8,5,6], "target": 22 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [9,3,6,10,11,1,5,14,3], "target": 27 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [4,5,9,6,2,2,10,5,13], "target": 43 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [3,10,2,13,6,13,14,14,3], "target": 28 }
assert my_solution.minimumAddedCoins(**test_input) == 1
test_input = { "coins": [14,15,15,12,13,4,15], "target": 38 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [10,5,12,11,9,8,1], "target": 12 }
assert my_solution.minimumAddedCoins(**test_input) == 2
test_input = { "coins": [15,13,12,4], "target": 31 }
assert my_solution.minimumAddedCoins(**test_input) == 3
test_input = { "coins": [4,5,14,13,10,12], "target": 31 }
assert my_solution.minimumAddedCoins(**test_input) == 2 | 1,701,570,600 |
weekly-contest-374-count-complete-substrings | https://leetcode.com/problems/count-complete-substrings | count-complete-substrings | {
"questionId": "3223",
"questionFrontendId": "2953",
"title": "Count Complete Substrings",
"titleSlug": "count-complete-substrings",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 154,
"dislikes": 30,
"categoryTitle": "Algorithms"
} | """
You are given a string word and an integer k.
A substring s of word is complete if:
Each character in s occurs exactly k times.
The difference between two adjacent characters is at most 2. That is, for any two adjacent characters c1 and c2 in s, the absolute difference in their positions in the alphabet is at most 2.
Return the number of complete substrings of word.
A substring is a non-empty contiguous sequence of characters in a string.
Example 1:
Input: word = "igigee", k = 2
Output: 3
Explanation: The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: igigee, igigee, igigee.
Example 2:
Input: word = "aaabbbccc", k = 3
Output: 6
Explanation: The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc.
Constraints:
1 <= word.length <= 105
word consists only of lowercase English letters.
1 <= k <= word.length
"""
class Solution:
def countCompleteSubstrings(self, word: str, k: int) -> int:
| You are given a string word and an integer k.
A substring s of word is complete if:
Each character in s occurs exactly k times.
The difference between two adjacent characters is at most 2. That is, for any two adjacent characters c1 and c2 in s, the absolute difference in their positions in the alphabet is at most 2.
Return the number of complete substrings of word.
A substring is a non-empty contiguous sequence of characters in a string.
Example 1:
Input: word = "igigee", k = 2
Output: 3
Explanation: The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: igigee, igigee, igigee.
Example 2:
Input: word = "aaabbbccc", k = 3
Output: 6
Explanation: The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc.
Constraints:
1 <= word.length <= 105
word consists only of lowercase English letters.
1 <= k <= word.length
Please complete the code below to solve above prblem:
```python
class Solution:
def countCompleteSubstrings(self, word: str, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "word": "igigee", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 3
test_input = { "word": "aaabbbccc", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "a", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "b", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "c", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "aa", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "ab", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "ac", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "ba", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 3
test_input = { "word": "bb", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "bc", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 3
test_input = { "word": "ca", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 3
test_input = { "word": "cb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 3
test_input = { "word": "cc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "aaa", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 3
test_input = { "word": "aab", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "aac", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "aba", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "abb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 4
test_input = { "word": "abc", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "aca", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 5
test_input = { "word": "acb", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "acc", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "baa", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "bab", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bac", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bba", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 4
test_input = { "word": "bbb", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 2
test_input = { "word": "bbc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "bca", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bcb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 5
test_input = { "word": "bcc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "caa", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 4
test_input = { "word": "cab", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "cac", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "cba", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "cbb", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "cbc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "cca", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "ccb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 4
test_input = { "word": "ccc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 2
test_input = { "word": "aaaa", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "aaab", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "aaac", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "aaba", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "aabb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 5
test_input = { "word": "aabc", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "aaca", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "aacb", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "aacc", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "abaa", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "abab", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 7
test_input = { "word": "abac", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "abba", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "abbb", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "abbc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "abca", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 9
test_input = { "word": "abcb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 8
test_input = { "word": "abcc", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "acaa", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "acab", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "acac", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 7
test_input = { "word": "acba", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "acbb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 7
test_input = { "word": "acbc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "acca", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "accb", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "accc", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "baaa", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 2
test_input = { "word": "baab", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "baac", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "baba", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 7
test_input = { "word": "babb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "babc", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "baca", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bacb", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bacc", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 7
test_input = { "word": "bbaa", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 3
test_input = { "word": "bbab", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bbac", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bbba", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bbbb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 4
test_input = { "word": "bbbc", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "bbca", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bbcb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "bbcc", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bcaa", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "bcab", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bcac", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bcba", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 8
test_input = { "word": "bcbb", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "bcbc", "k": 2 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "bcca", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bccb", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "bccc", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "caaa", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 1
test_input = { "word": "caab", "k": 1 }
assert my_solution.countCompleteSubstrings(**test_input) == 6
test_input = { "word": "caac", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "caba", "k": 4 }
assert my_solution.countCompleteSubstrings(**test_input) == 0
test_input = { "word": "cabb", "k": 3 }
assert my_solution.countCompleteSubstrings(**test_input) == 0 | 1,701,570,600 |
weekly-contest-374-count-the-number-of-infection-sequences | https://leetcode.com/problems/count-the-number-of-infection-sequences | count-the-number-of-infection-sequences | {
"questionId": "3224",
"questionFrontendId": "2954",
"title": "Count the Number of Infection Sequences",
"titleSlug": "count-the-number-of-infection-sequences",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 50,
"dislikes": 12,
"categoryTitle": "Algorithms"
} | """
You are given an integer n and a 0-indexed integer array sick which is sorted in increasing order.
There are n children standing in a queue with positions 0 to n - 1 assigned to them. The array sick contains the positions of the children who are infected with an infectious disease. An infected child at position i can spread the disease to either of its immediate neighboring children at positions i - 1 and i + 1 if they exist and are currently not infected. At most one child who was previously not infected can get infected with the disease in one second.
It can be shown that after a finite number of seconds, all the children in the queue will get infected with the disease. An infection sequence is the sequential order of positions in which all of the non-infected children get infected with the disease. Return the total number of possible infection sequences.
Since the answer may be large, return it modulo 109 + 7.
Note that an infection sequence does not contain positions of children who were already infected with the disease in the beginning.
Example 1:
Input: n = 5, sick = [0,4]
Output: 4
Explanation: Children at positions 1, 2, and 3 are not infected in the beginning. There are 4 possible infection sequences:
- The children at positions 1 and 3 can get infected since their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 2 gets infected.
Finally, the child at position 3 gets infected because it is adjacent to children at positions 2 and 4 who are infected. The infection sequence is [1,2,3].
- The children at positions 1 and 3 can get infected because their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 3 gets infected.
Finally, the child at position 2 gets infected because it is adjacent to children at positions 1 and 3 who are infected. The infection sequence is [1,3,2].
- The infection sequence is [3,1,2]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].
- The infection sequence is [3,2,1]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].
Example 2:
Input: n = 4, sick = [1]
Output: 3
Explanation: Children at positions 0, 2, and 3 are not infected in the beginning. There are 3 possible infection sequences:
- The infection sequence is [0,2,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,0,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,3,0]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
Constraints:
2 <= n <= 105
1 <= sick.length <= n - 1
0 <= sick[i] <= n - 1
sick is sorted in increasing order.
"""
class Solution:
def numberOfSequence(self, n: int, sick: List[int]) -> int:
| You are given an integer n and a 0-indexed integer array sick which is sorted in increasing order.
There are n children standing in a queue with positions 0 to n - 1 assigned to them. The array sick contains the positions of the children who are infected with an infectious disease. An infected child at position i can spread the disease to either of its immediate neighboring children at positions i - 1 and i + 1 if they exist and are currently not infected. At most one child who was previously not infected can get infected with the disease in one second.
It can be shown that after a finite number of seconds, all the children in the queue will get infected with the disease. An infection sequence is the sequential order of positions in which all of the non-infected children get infected with the disease. Return the total number of possible infection sequences.
Since the answer may be large, return it modulo 109 + 7.
Note that an infection sequence does not contain positions of children who were already infected with the disease in the beginning.
Example 1:
Input: n = 5, sick = [0,4]
Output: 4
Explanation: Children at positions 1, 2, and 3 are not infected in the beginning. There are 4 possible infection sequences:
- The children at positions 1 and 3 can get infected since their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 2 gets infected.
Finally, the child at position 3 gets infected because it is adjacent to children at positions 2 and 4 who are infected. The infection sequence is [1,2,3].
- The children at positions 1 and 3 can get infected because their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 3 gets infected.
Finally, the child at position 2 gets infected because it is adjacent to children at positions 1 and 3 who are infected. The infection sequence is [1,3,2].
- The infection sequence is [3,1,2]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].
- The infection sequence is [3,2,1]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].
Example 2:
Input: n = 4, sick = [1]
Output: 3
Explanation: Children at positions 0, 2, and 3 are not infected in the beginning. There are 3 possible infection sequences:
- The infection sequence is [0,2,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,0,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,3,0]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
Constraints:
2 <= n <= 105
1 <= sick.length <= n - 1
0 <= sick[i] <= n - 1
sick is sorted in increasing order.
Please complete the code below to solve above prblem:
```python
class Solution:
def numberOfSequence(self, n: int, sick: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "n": 5, "sick": [0,4] }
assert my_solution.numberOfSequence(**test_input) == 4
test_input = { "n": 4, "sick": [1] }
assert my_solution.numberOfSequence(**test_input) == 3
test_input = { "n": 2, "sick": [0] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [0] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 100, "sick": [0] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 2, "sick": [1] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [1] }
assert my_solution.numberOfSequence(**test_input) == 4
test_input = { "n": 5, "sick": [2] }
assert my_solution.numberOfSequence(**test_input) == 6
test_input = { "n": 5, "sick": [3] }
assert my_solution.numberOfSequence(**test_input) == 4
test_input = { "n": 5, "sick": [4] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [0,1] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 3, "sick": [0,2] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [0,2] }
assert my_solution.numberOfSequence(**test_input) == 3
test_input = { "n": 5, "sick": [0,3] }
assert my_solution.numberOfSequence(**test_input) == 6
test_input = { "n": 5, "sick": [1,2] }
assert my_solution.numberOfSequence(**test_input) == 3
test_input = { "n": 5, "sick": [1,3] }
assert my_solution.numberOfSequence(**test_input) == 6
test_input = { "n": 5, "sick": [1,4] }
assert my_solution.numberOfSequence(**test_input) == 6
test_input = { "n": 5, "sick": [2,3] }
assert my_solution.numberOfSequence(**test_input) == 3
test_input = { "n": 5, "sick": [2,4] }
assert my_solution.numberOfSequence(**test_input) == 3
test_input = { "n": 5, "sick": [3,4] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [0,1,2] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [0,1,3] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [0,1,4] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [0,2,3] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [0,2,4] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [0,3,4] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [1,2,3] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [1,2,4] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [1,3,4] }
assert my_solution.numberOfSequence(**test_input) == 2
test_input = { "n": 5, "sick": [2,3,4] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [0,1,2,3] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 10, "sick": [0,1,2,3] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 5, "sick": [0,1,2,4] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 10, "sick": [0,1,2,4] }
assert my_solution.numberOfSequence(**test_input) == 6
test_input = { "n": 10, "sick": [0,1,2,5] }
assert my_solution.numberOfSequence(**test_input) == 30
test_input = { "n": 10, "sick": [0,1,2,8] }
assert my_solution.numberOfSequence(**test_input) == 96
test_input = { "n": 10, "sick": [0,1,2,9] }
assert my_solution.numberOfSequence(**test_input) == 32
test_input = { "n": 5, "sick": [0,1,3,4] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 10, "sick": [0,1,3,5] }
assert my_solution.numberOfSequence(**test_input) == 30
test_input = { "n": 10, "sick": [0,1,3,6] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,1,3,7] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,1,3,8] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,1,4,5] }
assert my_solution.numberOfSequence(**test_input) == 30
test_input = { "n": 10, "sick": [0,1,4,6] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,1,4,7] }
assert my_solution.numberOfSequence(**test_input) == 360
test_input = { "n": 10, "sick": [0,1,4,8] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,1,4,9] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,1,5,6] }
assert my_solution.numberOfSequence(**test_input) == 80
test_input = { "n": 10, "sick": [0,1,5,7] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,1,5,8] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,1,5,9] }
assert my_solution.numberOfSequence(**test_input) == 320
test_input = { "n": 10, "sick": [0,1,6,7] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,1,6,8] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,1,6,9] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,1,7,8] }
assert my_solution.numberOfSequence(**test_input) == 96
test_input = { "n": 10, "sick": [0,1,7,9] }
assert my_solution.numberOfSequence(**test_input) == 96
test_input = { "n": 10, "sick": [0,1,8,9] }
assert my_solution.numberOfSequence(**test_input) == 32
test_input = { "n": 5, "sick": [0,2,3,4] }
assert my_solution.numberOfSequence(**test_input) == 1
test_input = { "n": 10, "sick": [0,2,3,4] }
assert my_solution.numberOfSequence(**test_input) == 6
test_input = { "n": 10, "sick": [0,2,3,5] }
assert my_solution.numberOfSequence(**test_input) == 30
test_input = { "n": 10, "sick": [0,2,3,6] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,2,3,7] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,2,3,8] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,2,3,9] }
assert my_solution.numberOfSequence(**test_input) == 96
test_input = { "n": 10, "sick": [0,2,4,5] }
assert my_solution.numberOfSequence(**test_input) == 30
test_input = { "n": 10, "sick": [0,2,4,6] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,2,4,7] }
assert my_solution.numberOfSequence(**test_input) == 360
test_input = { "n": 10, "sick": [0,2,4,8] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,2,4,9] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,2,5,6] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,2,5,7] }
assert my_solution.numberOfSequence(**test_input) == 360
test_input = { "n": 10, "sick": [0,2,5,8] }
assert my_solution.numberOfSequence(**test_input) == 720
test_input = { "n": 10, "sick": [0,2,5,9] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,2,6,7] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,2,6,8] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,2,7,8] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,2,7,9] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,2,8,9] }
assert my_solution.numberOfSequence(**test_input) == 96
test_input = { "n": 10, "sick": [0,3,4,5] }
assert my_solution.numberOfSequence(**test_input) == 30
test_input = { "n": 10, "sick": [0,3,4,7] }
assert my_solution.numberOfSequence(**test_input) == 360
test_input = { "n": 10, "sick": [0,3,4,8] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,3,4,9] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,3,5,6] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,3,5,7] }
assert my_solution.numberOfSequence(**test_input) == 360
test_input = { "n": 10, "sick": [0,3,6,7] }
assert my_solution.numberOfSequence(**test_input) == 360
test_input = { "n": 10, "sick": [0,3,6,8] }
assert my_solution.numberOfSequence(**test_input) == 720
test_input = { "n": 10, "sick": [0,3,6,9] }
assert my_solution.numberOfSequence(**test_input) == 720
test_input = { "n": 10, "sick": [0,3,7,8] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,3,7,9] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,3,8,9] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,4,5,6] }
assert my_solution.numberOfSequence(**test_input) == 80
test_input = { "n": 10, "sick": [0,4,5,7] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,4,5,9] }
assert my_solution.numberOfSequence(**test_input) == 320
test_input = { "n": 10, "sick": [0,4,6,7] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,4,6,8] }
assert my_solution.numberOfSequence(**test_input) == 480
test_input = { "n": 10, "sick": [0,4,8,9] }
assert my_solution.numberOfSequence(**test_input) == 320
test_input = { "n": 10, "sick": [0,5,6,7] }
assert my_solution.numberOfSequence(**test_input) == 120
test_input = { "n": 10, "sick": [0,5,6,9] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,5,7,8] }
assert my_solution.numberOfSequence(**test_input) == 240
test_input = { "n": 10, "sick": [0,5,7,9] }
assert my_solution.numberOfSequence(**test_input) == 240 | 1,701,570,600 |
weekly-contest-373-matrix-similarity-after-cyclic-shifts | https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts | matrix-similarity-after-cyclic-shifts | {
"questionId": "3215",
"questionFrontendId": "2946",
"title": "Matrix Similarity After Cyclic Shifts",
"titleSlug": "matrix-similarity-after-cyclic-shifts",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 72,
"dislikes": 48,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed m x n integer matrix mat and an integer k. You have to cyclically right shift odd indexed rows k times and cyclically left shift even indexed rows k times.
Return true if the initial and final matrix are exactly the same and false otherwise.
Example 1:
Input: mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2
Output: true
Explanation:
Initially, the matrix looks like the first figure.
Second figure represents the state of the matrix after one right and left cyclic shifts to even and odd indexed rows.
Third figure is the final state of the matrix after two cyclic shifts which is similar to the initial matrix.
Therefore, return true.
Example 2:
Input: mat = [[2,2],[2,2]], k = 3
Output: true
Explanation: As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same. Therefeore, we return true.
Example 3:
Input: mat = [[1,2]], k = 1
Output: false
Explanation: After one cyclic shift, mat = [[2,1]] which is not equal to the initial matrix. Therefore we return false.
Constraints:
1 <= mat.length <= 25
1 <= mat[i].length <= 25
1 <= mat[i][j] <= 25
1 <= k <= 50
"""
class Solution:
def areSimilar(self, mat: List[List[int]], k: int) -> bool:
| You are given a 0-indexed m x n integer matrix mat and an integer k. You have to cyclically right shift odd indexed rows k times and cyclically left shift even indexed rows k times.
Return true if the initial and final matrix are exactly the same and false otherwise.
Example 1:
Input: mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2
Output: true
Explanation:
Initially, the matrix looks like the first figure.
Second figure represents the state of the matrix after one right and left cyclic shifts to even and odd indexed rows.
Third figure is the final state of the matrix after two cyclic shifts which is similar to the initial matrix.
Therefore, return true.
Example 2:
Input: mat = [[2,2],[2,2]], k = 3
Output: true
Explanation: As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same. Therefeore, we return true.
Example 3:
Input: mat = [[1,2]], k = 1
Output: false
Explanation: After one cyclic shift, mat = [[2,1]] which is not equal to the initial matrix. Therefore we return false.
Constraints:
1 <= mat.length <= 25
1 <= mat[i].length <= 25
1 <= mat[i][j] <= 25
1 <= k <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def areSimilar(self, mat: List[List[int]], k: int) -> bool:
``` |
my_solution = Solution()
test_input = { "mat": [[1,2]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[1,2,1,2],[5,5,5,5],[6,3,6,3]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[4,9,10,10],[9,3,8,4],[2,5,3,8],[6,1,10,4]], "k": 5 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[5,8,8,4,7,2,3,4,3,10],[8,7,9,1,3,4,2,6,6,9],[6,2,10,10,4,6,3,4,1,1]], "k": 3 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[4,7,9,1,10,5,2,6,1,7],[8,9,9,2,3,2,3,2,3,5],[1,2,4,7,4,7,9,7,9,9]], "k": 5 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[10,6,3,6],[4,8,1,2]], "k": 6 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[7,10,6,7,7,4,4,7,2,2],[3,6,4,8,4,6,4,3,1,4],[4,8,7,1,10,2,10,8,10,1],[4,7,10,5,1,9,8,3,5,8],[3,7,6,5,3,1,3,2,8,5],[6,1,5,10,8,7,7,10,1,3]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[6,5,3],[4,6,2],[4,1,8],[3,9,1],[6,1,2],[1,9,9],[2,6,10]], "k": 5 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[2,4],[9,8]], "k": 9 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[2,2],[2,2]], "k": 3 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[9,1,10,6,10,7,3],[9,2,9,10,7,10,10]], "k": 4 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[7,7],[10,10],[4,4]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[6,6,6,6,6,6,6,6,6,6],[9,9,9,9,9,9,9,9,9,9],[1,1,1,1,1,1,1,1,1,1],[10,10,10,10,10,10,10,10,10,10],[2,2,2,2,2,2,2,2,2,2],[6,6,6,6,6,6,6,6,6,6],[7,7,7,7,7,7,7,7,7,7],[9,9,9,9,9,9,9,9,9,9],[8,8,8,8,8,8,8,8,8,8]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[6,9,1],[8,9,7],[2,8,7],[1,5,7],[10,5,9],[5,5,6],[8,6,1],[5,7,8]], "k": 5 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[3,10,3,10,3,10,3,10],[5,8,5,8,5,8,5,8],[3,9,3,9,3,9,3,9],[3,8,3,8,3,8,3,8],[2,3,2,3,2,3,2,3]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[9,5,3,10],[4,7,10,7],[1,7,9,4],[8,8,1,6],[6,7,6,1],[3,1,1,8],[9,2,8,3],[1,9,7,6]], "k": 4 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[4,6],[10,1],[8,8],[10,9],[9,10]], "k": 9 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[1,9,6,7,1,4,7,6,7],[7,10,6,6,4,9,6,8,2],[3,9,8,10,9,9,3,9,5],[8,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,4,6,4],[7,2,6,9,2,4,5,4,9],[4,8,7,5,3,6,3,9,5]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[9,3,3,7,7,5,3,3],[10,9,9,3,6,8,7,5],[8,9,3,10,10,10,2,1],[9,7,8,2,3,4,8,4],[5,9,5,2,2,6,5,7],[1,5,9,7,8,1,1,1]], "k": 10 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[10,6,10,6,10,6,10,6]], "k": 4 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[2,4],[6,1],[1,2],[2,10],[6,5],[4,9]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[8,8,8,8,8,8,8,8,8,8],[7,7,7,7,7,7,7,7,7,7],[6,6,6,6,6,6,6,6,6,6]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[8,10,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,8,3,10]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[8,9],[3,3],[5,6],[10,1],[2,5],[5,8],[5,4],[9,5]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[9,1,8,9,2,9,1,8,9,2],[10,2,7,8,9,10,2,7,8,9],[7,6,6,9,5,7,6,6,9,5]], "k": 5 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[4,4,4,2,7,9,1,8,9,8],[3,3,6,3,8,8,7,7,4,5],[10,1,3,7,6,5,7,10,3,10]], "k": 5 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[9,10,10,6,6,8,10,7,10,9],[10,6,1,10,10,5,7,9,9,2],[8,5,8,3,5,2,2,9,7,10]], "k": 20 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[4,5,1,3,10],[10,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]], "k": 8 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[2,7,1,10,5,3],[10,7,8,2,2,2],[9,6,1,4,10,6],[6,1,1,9,2,5],[6,4,7,3,6,4],[10,10,5,4,2,1],[7,3,3,7,1,5],[5,8,2,10,5,1],[3,1,5,1,5,7]], "k": 2 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[7,7,4],[8,9,9],[9,7,5],[6,3,6],[4,9,5],[1,10,3],[4,4,7],[4,7,6]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[10,10],[10,10],[5,5],[3,3],[2,2]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[6,4,7,6,3,9,4,2,10,5],[9,7,7,3,10,9,7,4,3,1]], "k": 20 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[7]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[6,3,2]], "k": 4 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[6,8]], "k": 4 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[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]], "k": 4 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[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,4,8],[10,9,6,3,1,4],[7,1,6,7,4,6]], "k": 2 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[7],[5],[5],[4],[4],[5],[8]], "k": 6 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[8,8,5,10,7,8,8,5,10,7],[1,2,6,10,7,1,2,6,10,7]], "k": 5 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[10,2,6,7,6,6,5],[6,3,3,4,6,5,7],[6,8,5,10,8,4,1]], "k": 8 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[4,10,9,7,9,9,2],[3,9,2,1,8,9,10],[7,10,9,7,2,3,8]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[1,7,10,10,9,2,1],[6,4,5,2,3,3,10],[2,6,8,3,6,1,4]], "k": 9 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[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]], "k": 2 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[9,5,6,9,5,6],[1,9,4,1,9,4],[5,7,2,5,7,2],[9,1,5,9,1,5],[6,8,6,6,8,6],[10,1,7,10,1,7]], "k": 6 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[2,7,6],[10,6,5],[10,2,4],[10,7,9],[5,8,6],[10,6,3],[10,9,6],[5,2,8],[10,1,2]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[5,4,5,10,5]], "k": 9 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[10,10,9],[5,6,7],[1,4,7],[5,1,1],[5,1,5],[5,10,3]], "k": 2 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[9,4,5],[8,5,4],[2,9,9]], "k": 10 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[4,2,2,7,9,1,1,2],[1,8,7,5,7,5,9,6],[2,9,4,10,1,8,5,4]], "k": 3 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[10],[7],[8],[2]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[10],[1],[5],[3],[1],[1]], "k": 4 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[7,1,7,7,1,7,7,1,7],[5,10,1,5,10,1,5,10,1]], "k": 3 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[4,7,9,9,4,7,9,9],[8,9,7,4,8,9,7,4],[6,8,6,4,6,8,6,4],[9,8,8,8,9,8,8,8],[3,6,5,3,3,6,5,3],[1,9,4,3,1,9,4,3],[8,3,2,7,8,3,2,7],[3,8,2,8,3,8,2,8],[6,5,2,8,6,5,2,8]], "k": 8 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[4],[5],[4],[2],[4],[2],[7],[4]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[3,8,5,4,10,2],[9,3,9,5,4,2]], "k": 6 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[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]], "k": 9 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[7,7,7,7,7],[1,1,1,1,1]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[5,7,5,5,1,9,1,8,6,7],[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,10,3,5],[2,2,4,9,10,1,6,2,8,3],[1,3,5,9,9,8,10,8,9,10],[7,8,7,7,6,9,2,5,8,4],[6,9,4,2,4,10,10,8,10,7]], "k": 8 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[2,9,10],[7,3,3],[7,6,2]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[2,5,8,9,6,8],[3,6,4,10,10,6],[9,6,10,9,6,5]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[2,2],[4,5],[3,2],[4,6],[1,9],[5,3],[3,5],[2,4],[3,9]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[1,8,6,8,6,7,1,6]], "k": 16 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[7,9,9,2,7],[8,5,8,6,7],[2,9,8,5,2],[9,9,2,6,8],[7,4,10,10,8]], "k": 6 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[8,8],[6,6],[2,2],[8,8],[9,9],[8,8],[10,10],[3,3],[4,4],[5,5]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[3,3,3,3,3,3],[5,3,5,3,5,3],[2,5,2,5,2,5],[8,8,8,8,8,8],[3,8,3,8,3,8],[5,3,5,3,5,3],[1,8,1,8,1,8],[8,9,8,9,8,9],[2,8,2,8,2,8]], "k": 4 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[2,2,2,2,2],[7,7,7,7,7],[5,5,5,5,5],[8,8,8,8,8],[1,1,1,1,1],[10,10,10,10,10],[7,7,7,7,7]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[3,1,10,5,10,3,1,10,5,10],[3,5,9,2,10,3,5,9,2,10],[4,6,3,5,7,4,6,3,5,7],[8,10,6,7,8,8,10,6,7,8]], "k": 5 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[10,7,1,7],[3,5,9,5],[2,8,3,10],[8,7,1,9],[3,8,6,3],[6,5,8,9],[8,7,5,1],[10,4,9,9],[4,6,1,9],[6,10,1,7]], "k": 3 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[1,10,3,9,6],[7,1,3,4,10]], "k": 3 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[7,7],[2,2],[5,5]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[4,4,4,4,4,4,4],[2,2,2,2,2,2,2],[3,3,3,3,3,3,3],[8,8,8,8,8,8,8],[6,6,6,6,6,6,6]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[10,3,5,3,10,3,5,3],[2,3,9,7,2,3,9,7],[10,4,4,8,10,4,4,8],[10,2,7,9,10,2,7,9],[8,1,8,3,8,1,8,3],[1,9,1,7,1,9,1,7]], "k": 4 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[6],[7],[1]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[7,6,4,5]], "k": 5 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[5,5,5,5],[5,5,5,5],[10,10,10,10],[2,2,2,2],[3,3,3,3],[2,2,2,2],[8,8,8,8],[10,10,10,10],[9,9,9,9],[7,7,7,7]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[5,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,2],[10,9,7,7,6,3,9,7],[1,4,8,4,6,5,5,1]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[3]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[1,1,1,1,1],[10,10,10,10,10],[10,10,10,10,10]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[6],[3],[2],[10]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[9,7,5,6],[5,2,1,8],[9,4,3,6],[5,7,4,1],[8,1,8,9],[4,3,6,5],[6,2,7,3],[1,3,6,4],[4,9,5,5]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[10,7,2,10,5,2,7],[10,10,3,8,3,3,8],[4,3,10,10,10,4,10]], "k": 4 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[8,5],[8,10],[8,10],[1,1],[2,1]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[9,9],[8,8],[2,2],[1,1],[8,8],[4,4],[9,9],[4,4],[6,6]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[10,1,1],[7,10,6],[9,6,6],[9,8,10],[8,2,1],[6,8,3],[8,6,6]], "k": 5 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[2,10,5,6,5,5],[6,3,1,5,4,7],[5,6,3,2,4,10],[9,2,6,8,6,2],[3,6,8,4,9,1]], "k": 8 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[10,3,4,2,8,10,3,4,2,8],[9,9,3,4,5,9,9,3,4,5],[6,9,9,2,7,6,9,9,2,7],[5,2,3,3,4,5,2,3,3,4]], "k": 5 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[3,4,10,3,4,10],[5,5,4,5,5,4],[5,5,3,5,5,3],[7,8,7,7,8,7]], "k": 3 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[7,1,9,3,6],[5,6,5,5,6],[2,3,5,10,8],[5,10,2,5,4],[7,9,1,7,10],[8,2,3,4,2],[1,6,9,2,1]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[3,3],[3,3],[4,4],[3,3],[8,8],[5,5]], "k": 1 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[2,10,2,6,3,6],[4,5,10,7,7,9],[1,7,4,1,9,4],[3,7,6,3,1,4],[4,10,4,6,3,5],[1,5,5,9,5,1],[10,2,5,4,7,10],[2,9,7,4,5,3],[5,5,1,2,8,3]], "k": 2 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[5],[5],[5]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[9,5,5,6,7],[7,9,3,8,1],[8,8,8,9,5],[1,3,2,6,9],[3,6,4,8,7],[9,3,3,9,10],[8,5,1,2,8],[7,3,10,5,1],[8,4,5,5,1]], "k": 5 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[5,8,5,2,8,5,9],[7,8,2,2,8,2,2],[4,5,6,7,3,9,9],[5,7,4,8,2,9,2],[9,5,3,3,5,7,3],[3,8,9,6,3,10,7],[6,7,3,7,3,6,6]], "k": 8 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[8,8,8,8,8,8],[8,8,8,8,8,8],[2,2,2,2,2,2],[6,6,6,6,6,6],[9,9,9,9,9,9],[10,10,10,10,10,10],[10,10,10,10,10,10]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[7,7,10,2],[3,5,7,6],[2,10,1,8],[8,3,1,10],[5,1,3,3],[6,3,4,9],[8,9,1,1]], "k": 7 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[5,2,7,2,6,10,7,5],[10,9,4,1,7,2,7,4],[2,6,7,3,2,10,4,5],[10,4,7,2,10,3,6,2]], "k": 16 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[9,10,10,1],[1,7,3,5],[9,6,4,7],[6,6,4,5],[2,4,2,7],[2,1,1,1],[7,2,1,8],[2,8,1,3],[7,4,6,1],[10,10,7,5]], "k": 4 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[7,3,10,2,3,1,10],[7,6,10,1,3,2,1],[9,1,5,7,1,8,3],[4,10,10,7,7,9,7],[7,9,1,5,3,8,4],[4,9,5,10,2,8,10],[2,5,10,3,6,2,9],[6,7,2,3,4,2,2]], "k": 1 }
assert my_solution.areSimilar(**test_input) == False
test_input = { "mat": [[8,8],[9,9],[2,2],[10,10],[10,10],[1,1],[5,5],[9,9],[7,7]], "k": 2 }
assert my_solution.areSimilar(**test_input) == True
test_input = { "mat": [[2,1,7,3,7,6,7,9,9,3],[3,9,10,4,4,6,8,10,5,6],[9,8,6,2,3,4,9,1,9,10],[7,10,8,8,3,9,9,5,8,9],[9,5,6,9,9,6,4,3,2,3],[3,10,6,2,7,6,10,6,2,6],[7,9,7,4,5,7,2,4,9,5],[4,7,9,6,7,4,6,4,10,4]], "k": 6 }
assert my_solution.areSimilar(**test_input) == False | 1,700,965,800 |
weekly-contest-373-count-beautiful-substrings-i | https://leetcode.com/problems/count-beautiful-substrings-i | count-beautiful-substrings-i | {
"questionId": "3210",
"questionFrontendId": "2947",
"title": "Count Beautiful Substrings I",
"titleSlug": "count-beautiful-substrings-i",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 102,
"dislikes": 8,
"categoryTitle": "Algorithms"
} | """
You are given a string s and a positive integer k.
Let vowels and consonants be the number of vowels and consonants in a string.
A string is beautiful if:
vowels == consonants.
(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.
Return the number of non-empty beautiful substrings in the given string s.
A substring is a contiguous sequence of characters in a string.
Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
Consonant letters in English are every letter except vowels.
Example 1:
Input: s = "baeyh", k = 2
Output: 2
Explanation: There are 2 beautiful substrings in the given string.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
It can be shown that there are only 2 beautiful substrings in the given string.
Example 2:
Input: s = "abba", k = 1
Output: 3
Explanation: There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
It can be shown that there are only 3 beautiful substrings in the given string.
Example 3:
Input: s = "bcdf", k = 1
Output: 0
Explanation: There are no beautiful substrings in the given string.
Constraints:
1 <= s.length <= 1000
1 <= k <= 1000
s consists of only English lowercase letters.
"""
class Solution:
def beautifulSubstrings(self, s: str, k: int) -> int:
| You are given a string s and a positive integer k.
Let vowels and consonants be the number of vowels and consonants in a string.
A string is beautiful if:
vowels == consonants.
(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.
Return the number of non-empty beautiful substrings in the given string s.
A substring is a contiguous sequence of characters in a string.
Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
Consonant letters in English are every letter except vowels.
Example 1:
Input: s = "baeyh", k = 2
Output: 2
Explanation: There are 2 beautiful substrings in the given string.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
It can be shown that there are only 2 beautiful substrings in the given string.
Example 2:
Input: s = "abba", k = 1
Output: 3
Explanation: There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
It can be shown that there are only 3 beautiful substrings in the given string.
Example 3:
Input: s = "bcdf", k = 1
Output: 0
Explanation: There are no beautiful substrings in the given string.
Constraints:
1 <= s.length <= 1000
1 <= k <= 1000
s consists of only English lowercase letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def beautifulSubstrings(self, s: str, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "s": "baeyh", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "abba", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "bcdf", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ihroyeeb", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "uzuxpzou", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "ouuoeqd", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "eeebjoxxujuaeoqibd", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 4
test_input = { "s": "ilougekqlovegioemdvu", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 21
test_input = { "s": "tqaewreikaztwpfwnef", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "oykiuhsafgfjumnzb", "k": 7 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ifvsa", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "svzauyuevujektj", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "urahjig", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "ime", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "oacghieut", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "aoluu", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ioaoiciiuoziout", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "ouafupsuhid", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ox", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "tlaiwoauazutusiaaui", "k": 10 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "caepeym", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "apyxvceue", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "imkqbb", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "caaz", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "pyicoy", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "uopmyrsntjhiroikup", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "aujfxqxcj", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "eeizejuoxeumz", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "uuouuaifnboeiulttio", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 10
test_input = { "s": "woozzxd", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "pulorolqcvhafexui", "k": 9 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "hmuaewojioizoguvoaje", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 4
test_input = { "s": "b", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "aiejouohnqnketinvat", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "mjiogpri", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "movbyaeouil", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "puureouausxmitvav", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "op", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "iuhoezpooxcohtlapolo", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 39
test_input = { "s": "cioi", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "pueutaoyaxk", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "iiuresacruaaan", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "agntyaazvpejidwaph", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "wiybolyniexiibou", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "coiyakadxi", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 8
test_input = { "s": "oraajoeruiakixj", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "jeayap", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 7
test_input = { "s": "iu", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "awozoy", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "fheabmlsyeeeuoeogyz", "k": 9 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "eaizneuxi", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 10
test_input = { "s": "uurqufaucsuoqljh", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 14
test_input = { "s": "jrtept", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "olgioxooiejooosaed", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "uizoy", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "lswabfiujjhexzos", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "iuu", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qeaxut", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 7
test_input = { "s": "aojiau", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "oaiaaaargkonlcsoaygf", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "zoowrawkm", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "uqiwuoevkfhkkua", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "kavuaaeodvaxicm", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qpxeceq", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "iaabaofuodcbek", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "eel", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "ikeuhe", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "lueikvo", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "oauau", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qzoieeotieeakqraeao", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 4
test_input = { "s": "ehaascocsdmgekni", "k": 9 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "euqeklniykiji", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "vaeiiioidiioxhduu", "k": 7 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "aa", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "chaua", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "edfrglfr", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "dqbe", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "ghooirorxge", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "fodartekaonq", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 9
test_input = { "s": "feeanzkjpfehzeuni", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ignoouesduu", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "yif", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "gondfjaeeuhbuuasgip", "k": 10 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "djooomsffoonelyeode", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 14
test_input = { "s": "pgaimei", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "naipqentonee", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 8
test_input = { "s": "bouov", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "lcuhoypz", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "g", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qc", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "mhznea", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "uxvjixdujgyfauo", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "iyjkuox", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "xbjfoayfpafatnuyord", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "nvoede", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "usnuaxpaktrweatruu", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "euojmsora", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 10
test_input = { "s": "iapgoi", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "uafuimcpxyeoixgbyeio", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "weuaatpu", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0 | 1,700,965,800 |
weekly-contest-373-make-lexicographically-smallest-array-by-swapping-elements | https://leetcode.com/problems/make-lexicographically-smallest-array-by-swapping-elements | make-lexicographically-smallest-array-by-swapping-elements | {
"questionId": "3219",
"questionFrontendId": "2948",
"title": "Make Lexicographically Smallest Array by Swapping Elements",
"titleSlug": "make-lexicographically-smallest-array-by-swapping-elements",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 197,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of positive integers nums and a positive integer limit.
In one operation, you can choose any two indices i and j and swap nums[i] and nums[j] if |nums[i] - nums[j]| <= limit.
Return the lexicographically smallest array that can be obtained by performing the operation any number of times.
An array a is lexicographically smaller than an array b if in the first position where a and b differ, array a has an element that is less than the corresponding element in b. For example, the array [2,10,3] is lexicographically smaller than the array [10,2,3] because they differ at index 0 and 2 < 10.
Example 1:
Input: nums = [1,5,3,9,8], limit = 2
Output: [1,3,5,8,9]
Explanation: Apply the operation 2 times:
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
We cannot obtain a lexicographically smaller array by applying any more operations.
Note that it may be possible to get the same result by doing different operations.
Example 2:
Input: nums = [1,7,6,18,2,1], limit = 3
Output: [1,6,7,18,1,2]
Explanation: Apply the operation 3 times:
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
We cannot obtain a lexicographically smaller array by applying any more operations.
Example 3:
Input: nums = [1,7,28,19,10], limit = 3
Output: [1,7,28,19,10]
Explanation: [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= limit <= 109
"""
class Solution:
def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]:
| You are given a 0-indexed array of positive integers nums and a positive integer limit.
In one operation, you can choose any two indices i and j and swap nums[i] and nums[j] if |nums[i] - nums[j]| <= limit.
Return the lexicographically smallest array that can be obtained by performing the operation any number of times.
An array a is lexicographically smaller than an array b if in the first position where a and b differ, array a has an element that is less than the corresponding element in b. For example, the array [2,10,3] is lexicographically smaller than the array [10,2,3] because they differ at index 0 and 2 < 10.
Example 1:
Input: nums = [1,5,3,9,8], limit = 2
Output: [1,3,5,8,9]
Explanation: Apply the operation 2 times:
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
We cannot obtain a lexicographically smaller array by applying any more operations.
Note that it may be possible to get the same result by doing different operations.
Example 2:
Input: nums = [1,7,6,18,2,1], limit = 3
Output: [1,6,7,18,1,2]
Explanation: Apply the operation 3 times:
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
We cannot obtain a lexicographically smaller array by applying any more operations.
Example 3:
Input: nums = [1,7,28,19,10], limit = 3
Output: [1,7,28,19,10]
Explanation: [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= limit <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "nums": [1,5,3,9,8], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,3,5,8,9]
test_input = { "nums": [1,7,6,18,2,1], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,6,7,18,1,2]
test_input = { "nums": [1,7,28,19,10], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,7,28,19,10]
test_input = { "nums": [1000000000], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1000000000]
test_input = { "nums": [1,60,34,84,62,56,39,76,49,38], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,56,34,84,60,62,38,76,49,39]
test_input = { "nums": [1,81,10,79,36,2,87,12,20,77], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,77,10,79,36,2,81,12,20,87]
test_input = { "nums": [2,71,5,87,11,15,70,70,14,38], "limit": 14 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [2,70,5,87,11,14,70,71,15,38]
test_input = { "nums": [4,3,23,84,34,88,44,44,18,15], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [3,4,23,84,34,88,44,44,15,18]
test_input = { "nums": [4,34,29,73,51,11,8,53,98,47], "limit": 10 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,29,34,73,47,8,11,51,98,53]
test_input = { "nums": [4,52,38,59,71,27,31,83,88,10], "limit": 14 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,27,31,38,52,59,71,83,88,10]
test_input = { "nums": [4,68,8,10,70,62,27,5,42,61], "limit": 11 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,61,5,8,62,68,27,10,42,70]
test_input = { "nums": [5,9,35,60,73,91,61,57,87,76], "limit": 11 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,9,35,57,73,76,60,61,87,91]
test_input = { "nums": [5,15,68,47,49,67,9,6,35,14], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,14,67,47,49,68,6,9,35,15]
test_input = { "nums": [5,16,43,15,66,21,58,74,55,66], "limit": 9 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,15,43,16,55,21,58,66,66,74]
test_input = { "nums": [5,30,92,4,31,2,17,39,15,7], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [2,30,92,4,31,5,15,39,17,7]
test_input = { "nums": [5,38,68,80,64,79,50,5,8,95], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,38,64,79,68,80,50,5,8,95]
test_input = { "nums": [5,100,44,45,16,30,14,65,83,64], "limit": 15 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,100,14,16,30,44,45,64,83,65]
test_input = { "nums": [6,57,100,67,4,63,47,59,21,66], "limit": 8 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,57,100,59,6,63,47,66,21,67]
test_input = { "nums": [6,70,90,1,33,81,60,80,68,44], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,68,90,6,33,80,60,81,70,44]
test_input = { "nums": [6,74,74,74,30,70,91,74,76,41], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [6,74,74,74,30,70,91,74,76,41]
test_input = { "nums": [6,77,68,15,3,98,56,22,81,72], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [6,77,68,15,3,98,56,22,81,72]
test_input = { "nums": [7,17,79,29,29,83,21,12,5,1], "limit": 10 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,5,79,7,12,83,17,21,29,29]
test_input = { "nums": [7,66,85,9,29,1,25,69,57,95], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,57,85,7,25,9,29,66,69,95]
test_input = { "nums": [7,73,1,97,13,55,74,29,76,19], "limit": 14 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,73,7,97,13,55,74,19,76,29]
test_input = { "nums": [8,4,47,23,73,79,63,62,35,51], "limit": 11 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,8,47,23,51,62,63,73,35,79]
test_input = { "nums": [8,17,20,100,59,98,64,78,64,53], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [8,17,20,100,59,98,64,78,64,53]
test_input = { "nums": [8,70,99,5,49,27,79,2,57,49], "limit": 14 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [2,49,99,5,49,27,57,8,70,79]
test_input = { "nums": [9,67,94,37,5,90,43,13,27,21], "limit": 11 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,67,90,9,13,94,21,27,37,43]
test_input = { "nums": [10,22,17,76,6,64,51,60,65,37], "limit": 9 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [6,10,17,76,22,51,60,64,65,37]
test_input = { "nums": [10,34,63,88,76,30,70,80,52,13], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [10,30,63,88,70,34,76,80,52,13]
test_input = { "nums": [10,69,4,28,15,30,23,53,41,93], "limit": 9 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,69,10,15,23,28,30,53,41,93]
test_input = { "nums": [12,86,98,73,64,77,30,76,46,69], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [12,86,98,69,64,73,30,76,46,77]
test_input = { "nums": [13,43,32,15,45,69,58,89,64,76], "limit": 12 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [13,32,43,15,45,58,64,89,69,76]
test_input = { "nums": [13,70,11,74,73,21,4,45,95,38], "limit": 9 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,70,11,73,74,13,21,38,95,45]
test_input = { "nums": [14,15,53,11,38,18,27,69,55,2], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [2,11,53,14,15,18,27,69,55,38]
test_input = { "nums": [14,28,61,49,10,25,80,83,42,100], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [14,25,61,49,10,28,80,83,42,100]
test_input = { "nums": [14,71,7,77,99,90,20,81,100,65], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [7,65,14,71,99,90,20,77,100,81]
test_input = { "nums": [14,95,75,100,33,98,88,2,74,26], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [14,95,74,100,33,98,88,2,75,26]
test_input = { "nums": [15,29,16,37,10,70,58,5,33,76], "limit": 8 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,29,10,33,15,70,58,16,37,76]
test_input = { "nums": [15,33,1,74,47,6,60,95,78,72], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [15,33,1,72,47,6,60,95,74,78]
test_input = { "nums": [16,20,79,92,17,7,70,41,54,18], "limit": 6 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [16,17,79,92,18,7,70,41,54,20]
test_input = { "nums": [16,43,19,36,99,15,70,89,45,71], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [15,43,16,36,99,19,70,89,45,71]
test_input = { "nums": [17,99,88,73,13,1,3,5,55,4], "limit": 15 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,73,88,99,3,4,5,13,55,17]
test_input = { "nums": [18,97,57,1,23,36,77,80,47,91], "limit": 10 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [18,91,47,1,23,36,77,80,57,97]
test_input = { "nums": [19,25,49,96,35,69,81,81,51,50], "limit": 12 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [19,25,49,96,35,69,81,81,50,51]
test_input = { "nums": [19,37,12,11,70,99,88,36,64,9], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [19,36,9,11,70,99,88,37,64,12]
test_input = { "nums": [19,64,26,5,70,10,17,66,51,36], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [5,51,10,17,64,19,26,66,70,36]
test_input = { "nums": [21,14,21,34,4,88,39,62,30,20], "limit": 12 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [4,14,20,21,21,88,30,62,34,39]
test_input = { "nums": [22,94,100,54,97,14,100,48,41,35], "limit": 6 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [22,94,97,48,100,14,100,54,35,41]
test_input = { "nums": [23,50,8,48,62,26,92,5,96,9], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [23,48,5,50,62,26,92,8,96,9]
test_input = { "nums": [25,47,34,69,36,91,14,44,37,2], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [25,47,34,69,36,91,14,44,37,2]
test_input = { "nums": [25,58,36,16,42,57,17,96,10,2], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [2,57,10,16,17,58,25,96,36,42]
test_input = { "nums": [26,21,9,15,94,47,52,86,89,7], "limit": 9 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [7,9,15,21,86,47,52,89,94,26]
test_input = { "nums": [27,26,24,2,95,90,41,14,20,35], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [24,26,27,2,95,90,41,14,20,35]
test_input = { "nums": [27,56,68,41,39,80,60,36,24,5], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [24,56,60,27,36,68,80,39,41,5]
test_input = { "nums": [27,71,52,71,68,2,49,37,34,97], "limit": 8 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [27,68,49,71,71,2,52,34,37,97]
test_input = { "nums": [29,82,25,91,17,9,38,25,29,68], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [25,82,25,91,17,9,38,29,29,68]
test_input = { "nums": [30,48,76,86,21,1,55,49,90,9], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [30,48,76,86,21,1,55,49,90,9]
test_input = { "nums": [31,10,64,15,60,32,88,79,79,33], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [31,10,64,15,60,32,88,79,79,33]
test_input = { "nums": [32,70,43,51,40,73,56,39,75,45], "limit": 8 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [32,70,39,40,43,73,45,51,75,56]
test_input = { "nums": [32,95,51,87,29,43,21,55,45,84], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [32,95,51,87,29,43,21,55,45,84]
test_input = { "nums": [33,25,25,65,82,71,56,82,13,46], "limit": 14 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [13,25,25,33,46,56,65,71,82,82]
test_input = { "nums": [33,37,77,41,83,75,96,97,4,60], "limit": 15 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [33,37,60,41,75,77,83,96,4,97]
test_input = { "nums": [35,81,18,79,47,53,20,2,98,22], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [35,79,18,81,47,53,20,2,98,22]
test_input = { "nums": [36,39,100,4,44,33,65,11,15,35], "limit": 10 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [33,35,100,4,36,39,65,11,15,44]
test_input = { "nums": [38,56,60,98,21,15,70,37,24,61], "limit": 15 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [15,56,60,98,21,24,61,37,38,70]
test_input = { "nums": [39,36,18,39,99,51,68,92,5,38], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [36,38,18,39,99,51,68,92,5,39]
test_input = { "nums": [39,89,81,37,67,37,98,89,49,47], "limit": 12 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [37,81,89,37,67,39,89,98,47,49]
test_input = { "nums": [40,67,99,53,95,47,59,99,64,44], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [40,44,95,47,99,53,59,99,64,67]
test_input = { "nums": [40,97,72,48,55,91,83,82,91,63], "limit": 11 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [40,48,55,63,72,82,83,91,91,97]
test_input = { "nums": [41,10,22,43,17,38,67,7,68,70], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [38,7,10,41,17,43,67,22,68,70]
test_input = { "nums": [41,25,83,44,39,37,67,33,58,5], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [41,25,83,44,39,37,67,33,58,5]
test_input = { "nums": [41,87,34,74,77,62,18,28,5,8], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [41,87,34,74,77,62,18,28,5,8]
test_input = { "nums": [45,1,66,44,45,74,75,96,31,47], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [44,1,66,45,45,74,75,96,31,47]
test_input = { "nums": [45,64,77,71,73,6,24,55,82,25], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [45,64,77,71,73,6,24,55,82,25]
test_input = { "nums": [46,72,1,33,1,51,78,96,44,20], "limit": 1 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [46,72,1,33,1,51,78,96,44,20]
test_input = { "nums": [47,32,72,79,16,69,85,70,87,73], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [47,32,69,79,16,70,85,72,87,73]
test_input = { "nums": [47,94,72,49,50,62,17,22,85,86], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [47,94,72,49,50,62,17,22,85,86]
test_input = { "nums": [48,39,45,58,26,57,38,63,82,80], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [48,38,45,57,26,58,39,63,80,82]
test_input = { "nums": [48,51,51,39,54,56,57,6,1,40], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [48,51,51,39,54,56,57,6,1,40]
test_input = { "nums": [49,4,95,24,20,12,70,60,82,62], "limit": 11 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [49,4,95,12,20,24,60,62,82,70]
test_input = { "nums": [49,7,92,79,43,88,31,89,36,97], "limit": 8 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [31,7,88,79,36,89,43,92,49,97]
test_input = { "nums": [49,16,32,11,7,57,69,41,52,23], "limit": 15 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [7,11,16,23,32,41,49,52,57,69]
test_input = { "nums": [49,26,82,77,52,76,90,23,64,42], "limit": 12 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [42,23,49,52,64,76,77,26,82,90]
test_input = { "nums": [49,62,63,32,57,22,74,87,42,19], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [19,22,32,42,49,57,62,63,74,87]
test_input = { "nums": [49,93,5,15,56,2,65,74,82,42], "limit": 13 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [42,49,2,5,56,15,65,74,82,93]
test_input = { "nums": [49,93,100,79,76,14,90,32,4,5], "limit": 10 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [49,90,93,76,79,4,100,32,5,14]
test_input = { "nums": [49,96,75,44,74,78,82,40,43,68], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [49,96,74,40,75,78,82,43,44,68]
test_input = { "nums": [51,61,49,11,69,78,40,98,68,36], "limit": 10 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [36,40,49,11,51,61,68,98,69,78]
test_input = { "nums": [51,99,52,80,83,69,18,49,71,13], "limit": 14 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [49,99,51,69,71,80,13,52,83,18]
test_input = { "nums": [52,28,93,16,33,37,37,21,47,64], "limit": 12 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [16,21,93,28,33,37,37,47,52,64]
test_input = { "nums": [53,7,99,22,3,50,62,70,56,40], "limit": 2 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [53,7,99,22,3,50,62,70,56,40]
test_input = { "nums": [53,17,39,72,5,78,40,3,84,20], "limit": 5 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [53,17,39,72,3,78,40,5,84,20]
test_input = { "nums": [53,71,55,38,26,89,20,98,55,21], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [53,71,55,38,26,89,20,98,55,21]
test_input = { "nums": [53,71,74,7,99,64,95,99,90,22], "limit": 9 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [53,64,71,7,90,74,95,99,99,22]
test_input = { "nums": [55,19,82,86,12,64,44,76,88,31], "limit": 4 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [55,19,82,86,12,64,44,76,88,31]
test_input = { "nums": [56,13,55,1,18,36,45,25,20,52], "limit": 14 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [1,13,18,20,25,36,45,52,55,56]
test_input = { "nums": [56,28,50,100,56,99,80,71,6,5], "limit": 7 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [50,28,56,99,56,100,80,71,5,6]
test_input = { "nums": [56,35,19,2,83,20,96,42,33,68], "limit": 3 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [56,33,19,2,83,20,96,42,35,68]
test_input = { "nums": [56,69,94,21,65,46,64,91,75,25], "limit": 5 }
assert my_solution.lexicographicallySmallestArray(**test_input) == [56,64,91,21,65,46,69,94,75,25] | 1,700,965,800 |
weekly-contest-373-count-beautiful-substrings-ii | https://leetcode.com/problems/count-beautiful-substrings-ii | count-beautiful-substrings-ii | {
"questionId": "3208",
"questionFrontendId": "2949",
"title": "Count Beautiful Substrings II",
"titleSlug": "count-beautiful-substrings-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 137,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
You are given a string s and a positive integer k.
Let vowels and consonants be the number of vowels and consonants in a string.
A string is beautiful if:
vowels == consonants.
(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.
Return the number of non-empty beautiful substrings in the given string s.
A substring is a contiguous sequence of characters in a string.
Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
Consonant letters in English are every letter except vowels.
Example 1:
Input: s = "baeyh", k = 2
Output: 2
Explanation: There are 2 beautiful substrings in the given string.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
It can be shown that there are only 2 beautiful substrings in the given string.
Example 2:
Input: s = "abba", k = 1
Output: 3
Explanation: There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
It can be shown that there are only 3 beautiful substrings in the given string.
Example 3:
Input: s = "bcdf", k = 1
Output: 0
Explanation: There are no beautiful substrings in the given string.
Constraints:
1 <= s.length <= 5 * 104
1 <= k <= 1000
s consists of only English lowercase letters.
"""
class Solution:
def beautifulSubstrings(self, s: str, k: int) -> int:
| You are given a string s and a positive integer k.
Let vowels and consonants be the number of vowels and consonants in a string.
A string is beautiful if:
vowels == consonants.
(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.
Return the number of non-empty beautiful substrings in the given string s.
A substring is a contiguous sequence of characters in a string.
Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
Consonant letters in English are every letter except vowels.
Example 1:
Input: s = "baeyh", k = 2
Output: 2
Explanation: There are 2 beautiful substrings in the given string.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).
You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
It can be shown that there are only 2 beautiful substrings in the given string.
Example 2:
Input: s = "abba", k = 1
Output: 3
Explanation: There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
It can be shown that there are only 3 beautiful substrings in the given string.
Example 3:
Input: s = "bcdf", k = 1
Output: 0
Explanation: There are no beautiful substrings in the given string.
Constraints:
1 <= s.length <= 5 * 104
1 <= k <= 1000
s consists of only English lowercase letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def beautifulSubstrings(self, s: str, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "s": "baeyh", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "abba", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "bcdf", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ihroyeeb", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "uzuxpzou", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "ouuoeqd", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "eeebjoxxujuaeoqibd", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 4
test_input = { "s": "ilougekqlovegioemdvu", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 21
test_input = { "s": "tqaewreikaztwpfwnef", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "oykiuhsafgfjumnzb", "k": 7 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ifvsa", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "svzauyuevujektj", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "urahjig", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "ime", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "oacghieut", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "aoluu", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ioaoiciiuoziout", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "ouafupsuhid", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ox", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "tlaiwoauazutusiaaui", "k": 10 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "caepeym", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "apyxvceue", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "imkqbb", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "caaz", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "pyicoy", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "uopmyrsntjhiroikup", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "aujfxqxcj", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "eeizejuoxeumz", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "uuouuaifnboeiulttio", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 10
test_input = { "s": "woozzxd", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "pulorolqcvhafexui", "k": 9 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "hmuaewojioizoguvoaje", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 4
test_input = { "s": "b", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "aiejouohnqnketinvat", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "mjiogpri", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "movbyaeouil", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "puureouausxmitvav", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "op", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "iuhoezpooxcohtlapolo", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 39
test_input = { "s": "cioi", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "pueutaoyaxk", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "iiuresacruaaan", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "agntyaazvpejidwaph", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "wiybolyniexiibou", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "coiyakadxi", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 8
test_input = { "s": "oraajoeruiakixj", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "jeayap", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 7
test_input = { "s": "iu", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "awozoy", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "fheabmlsyeeeuoeogyz", "k": 9 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "eaizneuxi", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 10
test_input = { "s": "uurqufaucsuoqljh", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 14
test_input = { "s": "jrtept", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "olgioxooiejooosaed", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "uizoy", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "lswabfiujjhexzos", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "iuu", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qeaxut", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 7
test_input = { "s": "aojiau", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "oaiaaaargkonlcsoaygf", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "zoowrawkm", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "uqiwuoevkfhkkua", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "kavuaaeodvaxicm", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qpxeceq", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "iaabaofuodcbek", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "eel", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "ikeuhe", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 5
test_input = { "s": "lueikvo", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "oauau", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qzoieeotieeakqraeao", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 4
test_input = { "s": "ehaascocsdmgekni", "k": 9 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "euqeklniykiji", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "vaeiiioidiioxhduu", "k": 7 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "aa", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "chaua", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "edfrglfr", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "dqbe", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "ghooirorxge", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "fodartekaonq", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 9
test_input = { "s": "feeanzkjpfehzeuni", "k": 6 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "ignoouesduu", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 6
test_input = { "s": "yif", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "gondfjaeeuhbuuasgip", "k": 10 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "djooomsffoonelyeode", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 14
test_input = { "s": "pgaimei", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 2
test_input = { "s": "naipqentonee", "k": 4 }
assert my_solution.beautifulSubstrings(**test_input) == 8
test_input = { "s": "bouov", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "lcuhoypz", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "g", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "qc", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "mhznea", "k": 2 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "uxvjixdujgyfauo", "k": 8 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "iyjkuox", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "xbjfoayfpafatnuyord", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "nvoede", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 1
test_input = { "s": "usnuaxpaktrweatruu", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "euojmsora", "k": 1 }
assert my_solution.beautifulSubstrings(**test_input) == 10
test_input = { "s": "iapgoi", "k": 3 }
assert my_solution.beautifulSubstrings(**test_input) == 0
test_input = { "s": "uafuimcpxyeoixgbyeio", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 3
test_input = { "s": "weuaatpu", "k": 5 }
assert my_solution.beautifulSubstrings(**test_input) == 0 | 1,700,965,800 |
biweekly-contest-118-find-words-containing-character | https://leetcode.com/problems/find-words-containing-character | find-words-containing-character | {
"questionId": "3194",
"questionFrontendId": "2942",
"title": "Find Words Containing Character",
"titleSlug": "find-words-containing-character",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 134,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of strings words and a character x.
Return an array of indices representing the words that contain the character x.
Note that the returned array may be in any order.
Example 1:
Input: words = ["leet","code"], x = "e"
Output: [0,1]
Explanation: "e" occurs in both words: "leet", and "code". Hence, we return indices 0 and 1.
Example 2:
Input: words = ["abc","bcd","aaaa","cbc"], x = "a"
Output: [0,2]
Explanation: "a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.
Example 3:
Input: words = ["abc","bcd","aaaa","cbc"], x = "z"
Output: []
Explanation: "z" does not occur in any of the words. Hence, we return an empty array.
Constraints:
1 <= words.length <= 50
1 <= words[i].length <= 50
x is a lowercase English letter.
words[i] consists only of lowercase English letters.
"""
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
| You are given a 0-indexed array of strings words and a character x.
Return an array of indices representing the words that contain the character x.
Note that the returned array may be in any order.
Example 1:
Input: words = ["leet","code"], x = "e"
Output: [0,1]
Explanation: "e" occurs in both words: "leet", and "code". Hence, we return indices 0 and 1.
Example 2:
Input: words = ["abc","bcd","aaaa","cbc"], x = "a"
Output: [0,2]
Explanation: "a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.
Example 3:
Input: words = ["abc","bcd","aaaa","cbc"], x = "z"
Output: []
Explanation: "z" does not occur in any of the words. Hence, we return an empty array.
Constraints:
1 <= words.length <= 50
1 <= words[i].length <= 50
x is a lowercase English letter.
words[i] consists only of lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
``` |
my_solution = Solution()
test_input = { "words": ["leet","code"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == [0,1]
test_input = { "words": ["abc","bcd","aaaa","cbc"], "x": "a" }
assert my_solution.findWordsContaining(**test_input) == [0,2]
test_input = { "words": ["abc","bcd","aaaa","cbc"], "x": "z" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["sgtkshnss","m","ryvbkyvuz","ezittyjwgb","wudlwg"], "x": "x" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["lkwnhpbj","tlohm","juazsb","f","rq"], "x": "v" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["aaa","imvtfjmxr","wbzfoovjnf","hqwrwmi"], "x": "c" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["utyeachht","bgpkcs","skeecqvvvw","nccrd"], "x": "i" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["alcpxexztg","r"], "x": "h" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["ekcpg","pdknua","fot","janppw","ofomkfvx"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["dq","rlvopu"], "x": "d" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["wzppkd","jxvk","zaztizmwuv","hvcdtobr"], "x": "b" }
assert my_solution.findWordsContaining(**test_input) == [3]
test_input = { "words": ["y","hs","qznrkpi"], "x": "v" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["pze","yojczsb","mjvyr","i","xsygks"], "x": "q" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["qsgtjagcu","m"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["kidtwmw","ogh","trdedlh","wwbtlindg","naoylytpof","ujcbzwzkm","doamcoxdv"], "x": "o" }
assert my_solution.findWordsContaining(**test_input) == [1,4,6]
test_input = { "words": ["tsmeupctki"], "x": "t" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["dqxlbljmpf","uvdzfoiqg","jsnbnx","fbedae","nodewb","o","ivepktj"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == [1]
test_input = { "words": ["fjlmmecm","sautsoorhl","n","hsyco","amlukrpjpv","rmhdnj","g"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["khjchmeciv","vgx","xghr","bbufgegu","qyfxu"], "x": "r" }
assert my_solution.findWordsContaining(**test_input) == [2]
test_input = { "words": ["jhtcugtcpl","bvhlgmmla","ntfkwzite","imbtzafaj","sdl","t"], "x": "m" }
assert my_solution.findWordsContaining(**test_input) == [1,3]
test_input = { "words": ["kxoziqoafc","vifcxifq"], "x": "q" }
assert my_solution.findWordsContaining(**test_input) == [0,1]
test_input = { "words": ["ckfkjjsonl","scaaug","rmvqzyiwc","a","smymw"], "x": "p" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["t","exo","npr","skd","bxpmbu"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == [1]
test_input = { "words": ["eulsl","fwooyct","ypytexil"], "x": "c" }
assert my_solution.findWordsContaining(**test_input) == [1]
test_input = { "words": ["nhd","zheyegi","ogz","fpybmcc","ntbbwtde"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == [1,2]
test_input = { "words": ["gwzvusl","upcpvbfyxy","hg","yu","wsfqgzhh","zgphqacsyo"], "x": "o" }
assert my_solution.findWordsContaining(**test_input) == [5]
test_input = { "words": ["uiovpph","xxj","uwzxzvkobk"], "x": "r" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["abtrpwo","sgaegnavk","pfmv"], "x": "z" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["m","fxtphsdmgy","otq","vwuhhnebr","yen"], "x": "y" }
assert my_solution.findWordsContaining(**test_input) == [1,4]
test_input = { "words": ["irlzx","lbrknhl","roupfj","fskaieszo","nz","ijfyejq"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == [3,5]
test_input = { "words": ["raavc","tx"], "x": "l" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["bkpuvcrexw","hxtbcdprhr","ovt","xgurm","pjcz","sbhwpjmyz"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == [3]
test_input = { "words": ["f","xlmy","akbiqa","fobo"], "x": "s" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["mhan"], "x": "a" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["uisx"], "x": "o" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["znqdolksyn","keewspe","ffod","lah","gadhym","awnyymd","fvkl"], "x": "v" }
assert my_solution.findWordsContaining(**test_input) == [6]
test_input = { "words": ["ftujx","dnbwrurk","t","x","zjzhdl","jc"], "x": "t" }
assert my_solution.findWordsContaining(**test_input) == [0,2]
test_input = { "words": ["zrwf","thp","qecwlnq","w","teetdaxx"], "x": "t" }
assert my_solution.findWordsContaining(**test_input) == [1,4]
test_input = { "words": ["xyzgb","qflfrfqgaf"], "x": "l" }
assert my_solution.findWordsContaining(**test_input) == [1]
test_input = { "words": ["shnjr","qfvop"], "x": "y" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["fmwclqh","xbphhgreze","yi","gmtzrfdab","uicqa","n"], "x": "i" }
assert my_solution.findWordsContaining(**test_input) == [2,4]
test_input = { "words": ["jgkv","njhwihtv","v"], "x": "z" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["tqkwoofh","bcgngl","frjpqgrr","drvb"], "x": "x" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["npkvocbw","tn","dp","c","g","fsxvzcnty","ywnf"], "x": "k" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["leompil","vta","fzrsps","yp","bykmgwgk"], "x": "o" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["umq","c","ctuh","eadzeuui","tabum","isuct"], "x": "p" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["rnmpdkmrnb","icxxsvss","h","gd"], "x": "s" }
assert my_solution.findWordsContaining(**test_input) == [1]
test_input = { "words": ["ft","hsjf","e","xi"], "x": "w" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["ozf","xkehlkgp","vliewlbv","okgaahah"], "x": "b" }
assert my_solution.findWordsContaining(**test_input) == [2]
test_input = { "words": ["gbktzr","kbamubluz","dwoi","crhldx","idjronpded","rqaz"], "x": "c" }
assert my_solution.findWordsContaining(**test_input) == [3]
test_input = { "words": ["gvbzqcb","rwtbra","iuijl","qbmpbi"], "x": "c" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["lsh","szhxhcdc","quem","zupiydjeqp","czxyvysrrb","aqnlqtnfiv"], "x": "p" }
assert my_solution.findWordsContaining(**test_input) == [3]
test_input = { "words": ["leuah","liaoczeuch","ol","ify","layh","ifzudwuybw","x"], "x": "p" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["ksdpwwho","ktunsikyu"], "x": "a" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["vpypaumzlp","kqrb","pgw"], "x": "l" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["jkrpnx","c","kqi","xrsaviyusg","waoxq","fld","otxfgcp"], "x": "l" }
assert my_solution.findWordsContaining(**test_input) == [5]
test_input = { "words": ["tetw","zl","wd","hnkxoxlnz","dexgufawjd","oolpr","yyfwizbsl"], "x": "p" }
assert my_solution.findWordsContaining(**test_input) == [5]
test_input = { "words": ["hihprd","kitgiflc","nr","idduuahfkm"], "x": "x" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["flfxeca","g"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["st","betf","ipacxza","jpnw"], "x": "r" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["cvuxnzaib","c","tiytr","yiav","hp","yg"], "x": "d" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["yz","k","midujexvn","kwcgbht"], "x": "y" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["qcxobdaxv"], "x": "q" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["b","shrexcf","ve","eqpbnuy","qdhahodo","aerdf","bdjlaakagk"], "x": "p" }
assert my_solution.findWordsContaining(**test_input) == [3]
test_input = { "words": ["ympv"], "x": "q" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["thfy","lnfzoyafiy","qmc","boijcl","pvbzmsa","yjarwylcyc"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["hqptwi"], "x": "o" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["bv","xgrhtjnxh","fdtljkxa","po","hejof"], "x": "k" }
assert my_solution.findWordsContaining(**test_input) == [2]
test_input = { "words": ["mfdrclyx","pith"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["bxeblhrl","o","uvv"], "x": "b" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["giygz"], "x": "u" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["ffqw","nykncbxrqi","pgzy","of","oye","f"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == [2]
test_input = { "words": ["jjnh","nrbh","z"], "x": "l" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["gdzkdtvrm","ps","kp","sbdlkac","s","bt"], "x": "n" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["hpsk","stjltzz","gvbjwzktgg","hmeovbxvv","gqaxqoshbh","mqnwyabqq","sq"], "x": "f" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["gwmg","qdjeaxgc","rlajltxpd","d"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == [0,1]
test_input = { "words": ["dupx","r","j","wq","macfcfoz"], "x": "r" }
assert my_solution.findWordsContaining(**test_input) == [1]
test_input = { "words": ["rmypzoyto","wvhtrbuz","dgt","tmhqswmkx","trpjwzitp","tbetdxic"], "x": "t" }
assert my_solution.findWordsContaining(**test_input) == [0,1,2,3,4,5]
test_input = { "words": ["vpkjymgdb","s","gv","geie"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == [0,2,3]
test_input = { "words": ["epnmbry","hhfhprvqba"], "x": "l" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["zst","mjzbdxsks","dza","neqj","oqeilr"], "x": "d" }
assert my_solution.findWordsContaining(**test_input) == [1,2]
test_input = { "words": ["ffruqk","sse","cyj","tntq","mibbhhpce"], "x": "c" }
assert my_solution.findWordsContaining(**test_input) == [2,4]
test_input = { "words": ["vumzrbe","qudq","qfrt"], "x": "u" }
assert my_solution.findWordsContaining(**test_input) == [0,1]
test_input = { "words": ["wcrrprvu","fizkw","vzcjxhjy","e"], "x": "r" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["gjk","vri"], "x": "n" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["fds","vbmg","p","iesyvc","wgmyxhoo","yfllvzr"], "x": "f" }
assert my_solution.findWordsContaining(**test_input) == [0,5]
test_input = { "words": ["mifbjo","kpjlwfbas","skhueysodn","zeewicisy"], "x": "g" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["pvkmoccv","j"], "x": "y" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["s","uhcfwsssbe","iwofeukmx","yfta","ovrdcb","psnje"], "x": "s" }
assert my_solution.findWordsContaining(**test_input) == [0,1,5]
test_input = { "words": ["klpzrjw","qmrhbpa"], "x": "v" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["fzegksjmw","masiwhjue","gngsht","xwvmp","aahn","dwxr"], "x": "c" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["mveahpesx","tsqds","g","mux","bivffitjx","zfsqdje"], "x": "f" }
assert my_solution.findWordsContaining(**test_input) == [4,5]
test_input = { "words": ["c"], "x": "a" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["jzmhnhqkq"], "x": "a" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["cfdgbc","ltpvko","batjenrlq","edwefhw"], "x": "t" }
assert my_solution.findWordsContaining(**test_input) == [1,2]
test_input = { "words": ["smlcojfydr","slb"], "x": "r" }
assert my_solution.findWordsContaining(**test_input) == [0]
test_input = { "words": ["lnjimir"], "x": "x" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["do"], "x": "e" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["xyyvbxsb","dc","mmqpb","mmbwv","wdreyof","kpk","reeb"], "x": "l" }
assert my_solution.findWordsContaining(**test_input) == []
test_input = { "words": ["ytvyknnmzv","jsoe","wctzk"], "x": "i" }
assert my_solution.findWordsContaining(**test_input) == [] | 1,700,922,600 |
biweekly-contest-118-maximize-area-of-square-hole-in-grid | https://leetcode.com/problems/maximize-area-of-square-hole-in-grid | maximize-area-of-square-hole-in-grid | {
"questionId": "3214",
"questionFrontendId": "2943",
"title": "Maximize Area of Square Hole in Grid",
"titleSlug": "maximize-area-of-square-hole-in-grid",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 73,
"dislikes": 110,
"categoryTitle": "Algorithms"
} | """
There is a grid with n + 2 horizontal bars and m + 2 vertical bars, and initially containing 1 x 1 unit cells.
The bars are 1-indexed.
You are given the two integers, n and m.
You are also given two integer arrays: hBars and vBars.
hBars contains distinct horizontal bars in the range [2, n + 1].
vBars contains distinct vertical bars in the range [2, m + 1].
You are allowed to remove bars that satisfy any of the following conditions:
If it is a horizontal bar, it must correspond to a value in hBars.
If it is a vertical bar, it must correspond to a value in vBars.
Return an integer denoting the maximum area of a square-shaped hole in the grid after removing some bars (possibly none).
Example 1:
Input: n = 2, m = 1, hBars = [2,3], vBars = [2]
Output: 4
Explanation: The left image shows the initial grid formed by the bars.
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,3].
It is allowed to remove horizontal bars [2,3] and the vertical bar [2].
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
The resulting grid is shown on the right.
The hole has an area of 4.
It can be shown that it is not possible to get a square hole with an area more than 4.
Hence, the answer is 4.
Example 2:
Input: n = 1, m = 1, hBars = [2], vBars = [2]
Output: 4
Explanation: The left image shows the initial grid formed by the bars.
The horizontal bars are in the range [1,3], and the vertical bars are in the range [1,3].
It is allowed to remove the horizontal bar [2] and the vertical bar [2].
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
The resulting grid is shown on the right.
The hole has an area of 4.
Hence, the answer is 4, and it is the maximum possible.
Example 3:
Input: n = 2, m = 3, hBars = [2,3], vBars = [2,3,4]
Output: 9
Explanation: The left image shows the initial grid formed by the bars.
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,5].
It is allowed to remove horizontal bars [2,3] and vertical bars [2,3,4].
One way to get the maximum square-shaped hole is by removing horizontal bars 2 and 3, and vertical bars 3 and 4.
The resulting grid is shown on the right.
The hole has an area of 9.
It can be shown that it is not possible to get a square hole with an area more than 9.
Hence, the answer is 9.
Constraints:
1 <= n <= 109
1 <= m <= 109
1 <= hBars.length <= 100
2 <= hBars[i] <= n + 1
1 <= vBars.length <= 100
2 <= vBars[i] <= m + 1
All values in hBars are distinct.
All values in vBars are distinct.
"""
class Solution:
def maximizeSquareHoleArea(self, n: int, m: int, hBars: List[int], vBars: List[int]) -> int:
| There is a grid with n + 2 horizontal bars and m + 2 vertical bars, and initially containing 1 x 1 unit cells.
The bars are 1-indexed.
You are given the two integers, n and m.
You are also given two integer arrays: hBars and vBars.
hBars contains distinct horizontal bars in the range [2, n + 1].
vBars contains distinct vertical bars in the range [2, m + 1].
You are allowed to remove bars that satisfy any of the following conditions:
If it is a horizontal bar, it must correspond to a value in hBars.
If it is a vertical bar, it must correspond to a value in vBars.
Return an integer denoting the maximum area of a square-shaped hole in the grid after removing some bars (possibly none).
Example 1:
Input: n = 2, m = 1, hBars = [2,3], vBars = [2]
Output: 4
Explanation: The left image shows the initial grid formed by the bars.
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,3].
It is allowed to remove horizontal bars [2,3] and the vertical bar [2].
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
The resulting grid is shown on the right.
The hole has an area of 4.
It can be shown that it is not possible to get a square hole with an area more than 4.
Hence, the answer is 4.
Example 2:
Input: n = 1, m = 1, hBars = [2], vBars = [2]
Output: 4
Explanation: The left image shows the initial grid formed by the bars.
The horizontal bars are in the range [1,3], and the vertical bars are in the range [1,3].
It is allowed to remove the horizontal bar [2] and the vertical bar [2].
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
The resulting grid is shown on the right.
The hole has an area of 4.
Hence, the answer is 4, and it is the maximum possible.
Example 3:
Input: n = 2, m = 3, hBars = [2,3], vBars = [2,3,4]
Output: 9
Explanation: The left image shows the initial grid formed by the bars.
The horizontal bars are in the range [1,4], and the vertical bars are in the range [1,5].
It is allowed to remove horizontal bars [2,3] and vertical bars [2,3,4].
One way to get the maximum square-shaped hole is by removing horizontal bars 2 and 3, and vertical bars 3 and 4.
The resulting grid is shown on the right.
The hole has an area of 9.
It can be shown that it is not possible to get a square hole with an area more than 9.
Hence, the answer is 9.
Constraints:
1 <= n <= 109
1 <= m <= 109
1 <= hBars.length <= 100
2 <= hBars[i] <= n + 1
1 <= vBars.length <= 100
2 <= vBars[i] <= m + 1
All values in hBars are distinct.
All values in vBars are distinct.
Please complete the code below to solve above prblem:
```python
class Solution:
def maximizeSquareHoleArea(self, n: int, m: int, hBars: List[int], vBars: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "n": 2, "m": 1, "hBars": [2,3], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 1, "hBars": [2], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 3, "hBars": [2,3], "vBars": [2,3,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 1, "m": 5, "hBars": [2], "vBars": [2,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 4, "hBars": [3,2], "vBars": [4,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 4, "hBars": [2], "vBars": [2,3,5,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 4, "hBars": [2], "vBars": [4,3,2,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 3, "m": 2, "hBars": [3,2,4], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 3, "m": 2, "hBars": [4,2,3], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 14, "m": 4, "hBars": [13], "vBars": [3,4,5,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 19, "m": 7, "hBars": [6,12,4], "vBars": [6,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 4, "hBars": [2,3], "vBars": [4,2,3,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 4, "m": 2, "hBars": [2,5,4,3], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 5, "m": 1, "hBars": [2,4,3,6,5], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 6, "hBars": [2], "vBars": [3,2,7,4,6,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 13, "hBars": [2], "vBars": [4,14,2,12,11,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 5, "hBars": [2,3], "vBars": [6,2,5,4,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 5, "m": 2, "hBars": [2,3,6,4,5], "vBars": [2,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 6, "m": 1, "hBars": [7,4,3,2,5,6], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 4, "m": 4, "hBars": [2,3,4,5], "vBars": [5,4,3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 4, "m": 4, "hBars": [3,4,2,5], "vBars": [2,5,3,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 6, "m": 2, "hBars": [7,3,5,4,6,2], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 7, "m": 11, "hBars": [7,4,5,2,8,6,3], "vBars": [4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 8, "hBars": [2], "vBars": [9,7,8,2,5,6,4,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 9, "hBars": [2], "vBars": [5,2,10,4,3,6,8,7] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 7, "hBars": [2,3], "vBars": [2,5,6,8,7,3,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 2, "m": 7, "hBars": [2,3], "vBars": [2,8,6,7,5,3,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 4, "m": 5, "hBars": [3,2,4,5], "vBars": [4,3,6,5,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 4, "m": 5, "hBars": [5,3,4,2], "vBars": [5,3,6,4,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 4, "m": 40, "hBars": [5,3,2,4], "vBars": [36,41,6,34,33] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 8, "m": 1, "hBars": [4,7,9,8,6,2,3,5], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 11, "m": 6, "hBars": [8,9,6], "vBars": [5,3,6,4,2,7] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 22, "m": 50, "hBars": [6,19,8,17,23], "vBars": [51,3,32,44] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 27, "m": 2, "hBars": [2,26,28,22,4,8,23], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 1, "m": 9, "hBars": [2], "vBars": [3,6,10,4,8,5,9,7,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 9, "hBars": [2], "vBars": [3,7,5,9,10,2,4,8,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 3, "m": 7, "hBars": [2,4,3], "vBars": [5,4,2,3,7,6,8] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 3, "m": 7, "hBars": [4,3,2], "vBars": [2,7,3,6,5,4,8] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 3, "m": 7, "hBars": [4,3,2], "vBars": [3,7,5,2,6,4,8] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 3, "m": 7, "hBars": [4,3,2], "vBars": [8,2,5,3,6,4,7] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 3, "m": 13, "hBars": [2,4,3], "vBars": [4,6,7,12,10,13,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 4, "m": 6, "hBars": [2,3,4,5], "vBars": [7,2,4,6,3,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 5, "m": 5, "hBars": [4,6,5,2,3], "vBars": [2,4,5,6,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 7, "m": 3, "hBars": [8,6,4,5,7,2,3], "vBars": [4,3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 8, "m": 2, "hBars": [4,2,6,8,7,5,3,9], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 9, "m": 1, "hBars": [2,9,3,10,4,6,7,8,5], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 9, "m": 1, "hBars": [9,5,4,8,7,10,3,2,6], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 12, "m": 5, "hBars": [10,9,13,6,3], "vBars": [3,4,2,5,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 29, "m": 2, "hBars": [25,14,11,29,7,10,16,8], "vBars": [2,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 1, "m": 10, "hBars": [2], "vBars": [3,4,6,8,5,7,9,10,11,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 10, "hBars": [2], "vBars": [10,6,5,7,4,3,11,8,9,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 9, "hBars": [2,3], "vBars": [6,7,9,3,10,2,5,4,8] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 2, "m": 9, "hBars": [3,2], "vBars": [4,8,2,6,7,3,5,9,10] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 4, "m": 7, "hBars": [5,4,3,2], "vBars": [8,7,5,2,4,3,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 5, "m": 6, "hBars": [2,6,5,3,4], "vBars": [4,2,5,3,7,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 5, "m": 6, "hBars": [5,3,6,2,4], "vBars": [5,7,2,4,3,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 5, "m": 6, "hBars": [6,4,3,5,2], "vBars": [2,4,5,7,6,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 5, "m": 11, "hBars": [4,2,6,3,5], "vBars": [8,11,10,12,6,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 6, "m": 5, "hBars": [4,5,3,2,7,6], "vBars": [6,3,5,4,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 6, "m": 5, "hBars": [5,2,3,7,4,6], "vBars": [6,2,4,3,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 6, "m": 5, "hBars": [6,3,4,2,7,5], "vBars": [2,5,4,3,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 6, "m": 5, "hBars": [7,2,3,4,5,6], "vBars": [6,5,4,2,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 8, "m": 3, "hBars": [4,6,9,3,8,2,7,5], "vBars": [2,4,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 8, "m": 3, "hBars": [5,6,9,3,2,4,8,7], "vBars": [2,4,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 8, "m": 3, "hBars": [8,6,4,3,7,2,9,5], "vBars": [4,2,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 8, "m": 3, "hBars": [9,2,7,6,8,3,4,5], "vBars": [4,2,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 9, "m": 2, "hBars": [5,4,6,8,9,10,2,3,7], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 9, "m": 2, "hBars": [6,3,5,4,8,9,2,10,7], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 10, "m": 1, "hBars": [4,3,10,2,11,5,6,9,8,7], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 11, "hBars": [2], "vBars": [7,12,6,3,4,9,5,10,11,2,8] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 10, "hBars": [2,3], "vBars": [11,10,2,8,7,5,6,9,3,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 4, "m": 8, "hBars": [5,2,3,4], "vBars": [8,7,5,9,4,2,3,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 5, "m": 7, "hBars": [2,3,6,4,5], "vBars": [6,8,4,5,3,7,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 6, "m": 10, "hBars": [2,4,3,6,5,7], "vBars": [11,3,9,6,10,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 10, "m": 2, "hBars": [8,5,4,3,10,2,11,9,6,7], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 11, "m": 1, "hBars": [2,6,9,7,5,11,3,10,4,12,8], "vBars": [2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 44, "m": 2, "hBars": [5,16,18,28,3,9,6,35,14,10], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 1, "m": 12, "hBars": [2], "vBars": [12,9,3,13,7,2,6,11,10,8,4,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 11, "hBars": [2,3], "vBars": [3,7,2,5,12,9,10,4,8,11,6] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 2, "m": 11, "hBars": [2,3], "vBars": [12,10,6,7,2,3,5,11,4,8,9] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 5, "m": 8, "hBars": [2,4,6,3,5], "vBars": [8,7,9,4,2,5,6,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 6, "m": 7, "hBars": [5,4,6,3,2,7], "vBars": [4,7,6,5,2,8,3] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 49
test_input = { "n": 6, "m": 7, "hBars": [6,3,2,7,4,5], "vBars": [6,7,5,2,3,4,8] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 49
test_input = { "n": 8, "m": 5, "hBars": [7,4,3,9,2,8,6,5], "vBars": [5,2,6,3,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 9, "m": 4, "hBars": [4,5,6,10,7,2,3,9,8], "vBars": [5,3,2,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 9, "m": 4, "hBars": [9,6,3,10,2,8,4,5,7], "vBars": [4,3,2,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 10, "m": 3, "hBars": [5,4,9,8,3,6,11,2,10,7], "vBars": [4,3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16
test_input = { "n": 10, "m": 6, "hBars": [6,2,8,3,11,9,10,7,4,5], "vBars": [6,2,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 11, "m": 2, "hBars": [8,12,9,3,5,2,10,6,7,4,11], "vBars": [3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 28, "m": 31, "hBars": [29,24,4], "vBars": [22,24,2,14,26,4,29,13,15,25] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 1, "m": 13, "hBars": [2], "vBars": [9,5,2,6,8,11,7,10,3,13,14,4,12] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 4
test_input = { "n": 2, "m": 12, "hBars": [3,2], "vBars": [13,2,7,4,12,9,10,3,6,5,8,11] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 2, "m": 12, "hBars": [3,2], "vBars": [13,4,7,8,3,2,11,12,5,9,6,10] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 9
test_input = { "n": 5, "m": 9, "hBars": [2,5,6,3,4], "vBars": [2,7,3,9,4,10,8,6,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 6, "m": 9, "hBars": [4,2,5,7,6,3], "vBars": [4,2,6,8,10,3,7,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 49
test_input = { "n": 8, "m": 6, "hBars": [4,8,6,7,2,9,3,5], "vBars": [4,7,6,3,2,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 49
test_input = { "n": 9, "m": 5, "hBars": [2,5,4,8,3,7,6,10,9], "vBars": [6,3,5,2,4] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 9, "m": 5, "hBars": [8,9,5,2,6,7,4,10,3], "vBars": [6,2,3,4,5] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 36
test_input = { "n": 10, "m": 4, "hBars": [4,8,6,3,10,2,7,9,5,11], "vBars": [5,4,3,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 25
test_input = { "n": 11, "m": 3, "hBars": [2,8,5,3,12,10,4,11,6,7,9], "vBars": [3,4,2] }
assert my_solution.maximizeSquareHoleArea(**test_input) == 16 | 1,700,922,600 |
biweekly-contest-118-minimum-number-of-coins-for-fruits | https://leetcode.com/problems/minimum-number-of-coins-for-fruits | minimum-number-of-coins-for-fruits | {
"questionId": "3209",
"questionFrontendId": "2944",
"title": "Minimum Number of Coins for Fruits",
"titleSlug": "minimum-number-of-coins-for-fruits",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 141,
"dislikes": 27,
"categoryTitle": "Algorithms"
} | """
You are at a fruit market with different types of exotic fruits on display.
You are given a 1-indexed array prices, where prices[i] denotes the number of coins needed to purchase the ith fruit.
The fruit market has the following offer:
If you purchase the ith fruit at prices[i] coins, you can get the next i fruits for free.
Note that even if you can take fruit j for free, you can still purchase it for prices[j] coins to receive a new offer.
Return the minimum number of coins needed to acquire all the fruits.
Example 1:
Input: prices = [3,1,2]
Output: 4
Explanation: You can acquire the fruits as follows:
- Purchase the 1st fruit with 3 coins, you are allowed to take the 2nd fruit for free.
- Purchase the 2nd fruit with 1 coin, you are allowed to take the 3rd fruit for free.
- Take the 3rd fruit for free.
Note that even though you were allowed to take the 2nd fruit for free, you purchased it because it is more optimal.
It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.
Example 2:
Input: prices = [1,10,1,1]
Output: 2
Explanation: You can acquire the fruits as follows:
- Purchase the 1st fruit with 1 coin, you are allowed to take the 2nd fruit for free.
- Take the 2nd fruit for free.
- Purchase the 3rd fruit for 1 coin, you are allowed to take the 4th fruit for free.
- Take the 4th fruit for free.
It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.
Constraints:
1 <= prices.length <= 1000
1 <= prices[i] <= 105
"""
class Solution:
def minimumCoins(self, prices: List[int]) -> int:
| You are at a fruit market with different types of exotic fruits on display.
You are given a 1-indexed array prices, where prices[i] denotes the number of coins needed to purchase the ith fruit.
The fruit market has the following offer:
If you purchase the ith fruit at prices[i] coins, you can get the next i fruits for free.
Note that even if you can take fruit j for free, you can still purchase it for prices[j] coins to receive a new offer.
Return the minimum number of coins needed to acquire all the fruits.
Example 1:
Input: prices = [3,1,2]
Output: 4
Explanation: You can acquire the fruits as follows:
- Purchase the 1st fruit with 3 coins, you are allowed to take the 2nd fruit for free.
- Purchase the 2nd fruit with 1 coin, you are allowed to take the 3rd fruit for free.
- Take the 3rd fruit for free.
Note that even though you were allowed to take the 2nd fruit for free, you purchased it because it is more optimal.
It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.
Example 2:
Input: prices = [1,10,1,1]
Output: 2
Explanation: You can acquire the fruits as follows:
- Purchase the 1st fruit with 1 coin, you are allowed to take the 2nd fruit for free.
- Take the 2nd fruit for free.
- Purchase the 3rd fruit for 1 coin, you are allowed to take the 4th fruit for free.
- Take the 4th fruit for free.
It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.
Constraints:
1 <= prices.length <= 1000
1 <= prices[i] <= 105
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumCoins(self, prices: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "prices": [3,1,2] }
assert my_solution.minimumCoins(**test_input) == 4
test_input = { "prices": [1,10,1,1] }
assert my_solution.minimumCoins(**test_input) == 2
test_input = { "prices": [26,18,6,12,49,7,45,45] }
assert my_solution.minimumCoins(**test_input) == 39
test_input = { "prices": [27,17,29,45,3,39,42,26] }
assert my_solution.minimumCoins(**test_input) == 47
test_input = { "prices": [14,37,37,38,24,15,12] }
assert my_solution.minimumCoins(**test_input) == 63
test_input = { "prices": [1,37,19,38,11,42,18,33,6,37,15,48,23,12,41,18,27,32] }
assert my_solution.minimumCoins(**test_input) == 37
test_input = { "prices": [38,23,27,32,47,45,48,24,39,26,37,42,24,45,27,26,15,16,26,6] }
assert my_solution.minimumCoins(**test_input) == 132
test_input = { "prices": [45,44,5,9,22,14,29,14,21,13,45,10,2,16,14,30,26,1,49] }
assert my_solution.minimumCoins(**test_input) == 66
test_input = { "prices": [37,42,6,50,50,38,30,38,1,13,25,39,18,1,35,32,12] }
assert my_solution.minimumCoins(**test_input) == 74
test_input = { "prices": [17,32,11,25,22] }
assert my_solution.minimumCoins(**test_input) == 28
test_input = { "prices": [18,10,1,11,6,30,19,24,1,18,37,29,28,27,38] }
assert my_solution.minimumCoins(**test_input) == 26
test_input = { "prices": [3,10,25,47,49,10,49] }
assert my_solution.minimumCoins(**test_input) == 38
test_input = { "prices": [46,7,15] }
assert my_solution.minimumCoins(**test_input) == 53
test_input = { "prices": [16,45,25,5,18,19,25,13,33] }
assert my_solution.minimumCoins(**test_input) == 59
test_input = { "prices": [21,16,7,10,30] }
assert my_solution.minimumCoins(**test_input) == 28
test_input = { "prices": [21,22,29,37,23,15,39,9,19,10,6,9,33,28,43] }
assert my_solution.minimumCoins(**test_input) == 71
test_input = { "prices": [37,16,42,47,16,31,39,8,26,50,33] }
assert my_solution.minimumCoins(**test_input) == 77
test_input = { "prices": [32,4] }
assert my_solution.minimumCoins(**test_input) == 32
test_input = { "prices": [31,9,2,36,4,45,28,28,12,22,44,17,10,48,15,22,7,14,41] }
assert my_solution.minimumCoins(**test_input) == 56
test_input = { "prices": [1,31,9,36,44,2,23] }
assert my_solution.minimumCoins(**test_input) == 12
test_input = { "prices": [3,7,2,36,33,7,21,40,19] }
assert my_solution.minimumCoins(**test_input) == 12
test_input = { "prices": [9,4,7,29,22,50] }
assert my_solution.minimumCoins(**test_input) == 16
test_input = { "prices": [28,2,40,15] }
assert my_solution.minimumCoins(**test_input) == 30
test_input = { "prices": [16,17,47,20,18,37] }
assert my_solution.minimumCoins(**test_input) == 51
test_input = { "prices": [42,6,44,47,11,6,30,38,41,43,46,35,28,4,47,1,7,35] }
assert my_solution.minimumCoins(**test_input) == 93
test_input = { "prices": [10,10,5,8,5,13,34,31,36] }
assert my_solution.minimumCoins(**test_input) == 20
test_input = { "prices": [12,20,14,46,22,1,42,50,47,47,38,37,13] }
assert my_solution.minimumCoins(**test_input) == 40
test_input = { "prices": [1,38,28,46,18,22,12,7,44,44,40,36,41,5,33,5,30,33,31] }
assert my_solution.minimumCoins(**test_input) == 46
test_input = { "prices": [6,45,2,29,44,14,44] }
assert my_solution.minimumCoins(**test_input) == 22
test_input = { "prices": [34,13,50,42,24,47,41,8,26,34,3,48,39,24,39,26,46] }
assert my_solution.minimumCoins(**test_input) == 74
test_input = { "prices": [47,9,33,6,33,40,28,37,49,39,45,14,13,40,17,14,39,12,15,6] }
assert my_solution.minimumCoins(**test_input) == 103
test_input = { "prices": [32] }
assert my_solution.minimumCoins(**test_input) == 32
test_input = { "prices": [35,46,50,35,11,14,44,17,45,23,34,33,8,27,19,7,10,12,14] }
assert my_solution.minimumCoins(**test_input) == 107
test_input = { "prices": [50,45,14,24,18,15,9,14] }
assert my_solution.minimumCoins(**test_input) == 73
test_input = { "prices": [38,19,18,15,20,43,18,9,44,26,29] }
assert my_solution.minimumCoins(**test_input) == 74
test_input = { "prices": [26,21,7,40,37,44,13,3,10,9,15,12,30,18,31,10,23] }
assert my_solution.minimumCoins(**test_input) == 55
test_input = { "prices": [36,50] }
assert my_solution.minimumCoins(**test_input) == 36
test_input = { "prices": [1,32,48,36,26,5,30,25,2,17,26,39,17,46,34,47,43,45,20,48] }
assert my_solution.minimumCoins(**test_input) == 71
test_input = { "prices": [19,24,31,24] }
assert my_solution.minimumCoins(**test_input) == 43
test_input = { "prices": [1,18,25,29,17,9,3,29,23,17,18] }
assert my_solution.minimumCoins(**test_input) == 29
test_input = { "prices": [18,36,18,44,30,8,42,33,45,19,50,19,24,48] }
assert my_solution.minimumCoins(**test_input) == 63
test_input = { "prices": [26,25,47,36,9,31,1,29,29,42,29,42,36,19,45,4,11,7] }
assert my_solution.minimumCoins(**test_input) == 80
test_input = { "prices": [24,34,47,12,24,48,14,30,28,43,35,45,11,11,35,38] }
assert my_solution.minimumCoins(**test_input) == 95
test_input = { "prices": [29,18,2,6,47,32,27,12,38,17] }
assert my_solution.minimumCoins(**test_input) == 49
test_input = { "prices": [3,31,15,18,47,18,2,27,24,6,36,35,41,21,30] }
assert my_solution.minimumCoins(**test_input) == 26
test_input = { "prices": [29,45,8,45,23,35] }
assert my_solution.minimumCoins(**test_input) == 37
test_input = { "prices": [39,37] }
assert my_solution.minimumCoins(**test_input) == 39
test_input = { "prices": [18,45,6,14,41,41] }
assert my_solution.minimumCoins(**test_input) == 24
test_input = { "prices": [50,21,38,2,32,49,32,40,41,34,33,40,36,16,29,34,42,40,46] }
assert my_solution.minimumCoins(**test_input) == 121
test_input = { "prices": [28,17,42,20,6,26,47,6,23] }
assert my_solution.minimumCoins(**test_input) == 51
test_input = { "prices": [37,27,17,40,50,35,16,4,28,5,27,13,46,7,23,27] }
assert my_solution.minimumCoins(**test_input) == 74
test_input = { "prices": [11,5,40,16,20,38] }
assert my_solution.minimumCoins(**test_input) == 32
test_input = { "prices": [16,27,5,38,12,24,7,49,40,13,38,13,34,38,37,2,4,44] }
assert my_solution.minimumCoins(**test_input) == 41
test_input = { "prices": [25,9,49,19,33] }
assert my_solution.minimumCoins(**test_input) == 53
test_input = { "prices": [47,23,46,13,26,44,43,22,43,24,13,20,6,16,8,26] }
assert my_solution.minimumCoins(**test_input) == 105
test_input = { "prices": [8,1,1] }
assert my_solution.minimumCoins(**test_input) == 9
test_input = { "prices": [47,45,2,25,7,46] }
assert my_solution.minimumCoins(**test_input) == 49
test_input = { "prices": [4,31,50,45,5,50] }
assert my_solution.minimumCoins(**test_input) == 40
test_input = { "prices": [30,41,1,49,9,49,41,27,41,14,23,3,46,40,37,28,45,19,36,49] }
assert my_solution.minimumCoins(**test_input) == 54
test_input = { "prices": [6,3,49,28,31,36,5,50,39] }
assert my_solution.minimumCoins(**test_input) == 40
test_input = { "prices": [37,2,19,36,26,27,3,23,10,20,33,8,39,6,28] }
assert my_solution.minimumCoins(**test_input) == 65
test_input = { "prices": [37,34,12,30,43,35,6,21,47,38,14,31,49,11,14] }
assert my_solution.minimumCoins(**test_input) == 66
test_input = { "prices": [49,6,12,35,17,17,2] }
assert my_solution.minimumCoins(**test_input) == 63
test_input = { "prices": [45,27,43,34,41,30,28,45,24,50,20,4,15,42] }
assert my_solution.minimumCoins(**test_input) == 116
test_input = { "prices": [48,22,36] }
assert my_solution.minimumCoins(**test_input) == 70
test_input = { "prices": [47,13,23,31,41,25] }
assert my_solution.minimumCoins(**test_input) == 70
test_input = { "prices": [3,44,17,37,9,14,37] }
assert my_solution.minimumCoins(**test_input) == 29
test_input = { "prices": [4,43,7,15,38] }
assert my_solution.minimumCoins(**test_input) == 11
test_input = { "prices": [10,25,7,37,6,43,4,50,9,14,36,35,36,44,17,10,44,46,50] }
assert my_solution.minimumCoins(**test_input) == 35
test_input = { "prices": [45,28,10,18,18,3,42,24,14,11,13,32,37,31,50,32] }
assert my_solution.minimumCoins(**test_input) == 69
test_input = { "prices": [12,38,44,24,42,9,32,40,8,20,46,39,33] }
assert my_solution.minimumCoins(**test_input) == 73
test_input = { "prices": [5,42,30,20,37,26,38,30,30,32,39,31,33,41,23,4,29] }
assert my_solution.minimumCoins(**test_input) == 85
test_input = { "prices": [44,22] }
assert my_solution.minimumCoins(**test_input) == 44
test_input = { "prices": [8,8,11,21,9] }
assert my_solution.minimumCoins(**test_input) == 19
test_input = { "prices": [2,37,19,30,37,27,10,37] }
assert my_solution.minimumCoins(**test_input) == 31
test_input = { "prices": [43,27,48,22] }
assert my_solution.minimumCoins(**test_input) == 70
test_input = { "prices": [50,23,37,49,45,14,18,39,50,7,31] }
assert my_solution.minimumCoins(**test_input) == 101
test_input = { "prices": [37,3,32,25,21,22,26,49,14,45,1,36] }
assert my_solution.minimumCoins(**test_input) == 62
test_input = { "prices": [21,29,31,28,2,41,4,43,41,16,38,33,3,6,43,22,15] }
assert my_solution.minimumCoins(**test_input) == 59
test_input = { "prices": [24,47,32,41,35,14,18,23,27,8,27] }
assert my_solution.minimumCoins(**test_input) == 70
test_input = { "prices": [40,25,32] }
assert my_solution.minimumCoins(**test_input) == 65
test_input = { "prices": [9,18,2,26,15,3,2,33,46,6,11,34,27,7,5,7,26,13,48] }
assert my_solution.minimumCoins(**test_input) == 18
test_input = { "prices": [41,8,38,32,36,30,23,49,40,46,42,34,2,12,12,19,20,50,40] }
assert my_solution.minimumCoins(**test_input) == 104
test_input = { "prices": [28,8,24,14,34,36,48] }
assert my_solution.minimumCoins(**test_input) == 50
test_input = { "prices": [2,36,22,41,42,26,1,48,14,27,22,26] }
assert my_solution.minimumCoins(**test_input) == 25
test_input = { "prices": [25,39,21,21,16] }
assert my_solution.minimumCoins(**test_input) == 46
test_input = { "prices": [44,41,36,42,21,32,45,5] }
assert my_solution.minimumCoins(**test_input) == 101
test_input = { "prices": [10] }
assert my_solution.minimumCoins(**test_input) == 10
test_input = { "prices": [35,29] }
assert my_solution.minimumCoins(**test_input) == 35
test_input = { "prices": [38,35,33,11,43,33] }
assert my_solution.minimumCoins(**test_input) == 71
test_input = { "prices": [19,29,49,20,8,12,13,28,45,9,12,3,1,17,35] }
assert my_solution.minimumCoins(**test_input) == 65
test_input = { "prices": [34,22,1,41,34,27,18] }
assert my_solution.minimumCoins(**test_input) == 53
test_input = { "prices": [31,9,39,6,14,32,28,35,34,42,19,41,35,24,32,16,12,49,16] }
assert my_solution.minimumCoins(**test_input) == 73
test_input = { "prices": [46,50,21,14,26,47] }
assert my_solution.minimumCoins(**test_input) == 67
test_input = { "prices": [5,22,36,6,15,49,20,16,36,15,32,27,50,19,12,22,9,33] }
assert my_solution.minimumCoins(**test_input) == 57
test_input = { "prices": [17,4,36,4,32,11,42,12,20] }
assert my_solution.minimumCoins(**test_input) == 36
test_input = { "prices": [48,33,39,1,13,40] }
assert my_solution.minimumCoins(**test_input) == 82
test_input = { "prices": [32,19,33,30,32,44,47,8,10,1,23,6,28,19,20,48,12,10,20,22] }
assert my_solution.minimumCoins(**test_input) == 84
test_input = { "prices": [29,5,46,34,38,7,1,15] }
assert my_solution.minimumCoins(**test_input) == 68
test_input = { "prices": [39,50,22,1,38,22,49,16,27,48,45,28,43,34] }
assert my_solution.minimumCoins(**test_input) == 78 | 1,700,922,600 |
biweekly-contest-118-find-maximum-non-decreasing-array-length | https://leetcode.com/problems/find-maximum-non-decreasing-array-length | find-maximum-non-decreasing-array-length | {
"questionId": "3211",
"questionFrontendId": "2945",
"title": "Find Maximum Non-decreasing Array Length",
"titleSlug": "find-maximum-non-decreasing-array-length",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 90,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums.
You can perform any number of operations, where each operation involves selecting a subarray of the array and replacing it with the sum of its elements. For example, if the given array is [1,3,5,6] and you select subarray [3,5] the array will convert to [1,8,6].
Return the maximum length of a non-decreasing array that can be made after applying operations.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [5,2,2]
Output: 1
Explanation: This array with length 3 is not non-decreasing.
We have two ways to make the array length two.
First, choosing subarray [2,2] converts the array to [5,4].
Second, choosing subarray [5,2] converts the array to [7,2].
In these two ways the array is not non-decreasing.
And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing.
So the answer is 1.
Example 2:
Input: nums = [1,2,3,4]
Output: 4
Explanation: The array is non-decreasing. So the answer is 4.
Example 3:
Input: nums = [4,3,2,6]
Output: 3
Explanation: Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing.
Because the given array is not non-decreasing, the maximum possible answer is 3.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
"""
class Solution:
def findMaximumLength(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums.
You can perform any number of operations, where each operation involves selecting a subarray of the array and replacing it with the sum of its elements. For example, if the given array is [1,3,5,6] and you select subarray [3,5] the array will convert to [1,8,6].
Return the maximum length of a non-decreasing array that can be made after applying operations.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [5,2,2]
Output: 1
Explanation: This array with length 3 is not non-decreasing.
We have two ways to make the array length two.
First, choosing subarray [2,2] converts the array to [5,4].
Second, choosing subarray [5,2] converts the array to [7,2].
In these two ways the array is not non-decreasing.
And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing.
So the answer is 1.
Example 2:
Input: nums = [1,2,3,4]
Output: 4
Explanation: The array is non-decreasing. So the answer is 4.
Example 3:
Input: nums = [4,3,2,6]
Output: 3
Explanation: Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing.
Because the given array is not non-decreasing, the maximum possible answer is 3.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
Please complete the code below to solve above prblem:
```python
class Solution:
def findMaximumLength(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [5,2,2] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [1,2,3,4] }
assert my_solution.findMaximumLength(**test_input) == 4
test_input = { "nums": [4,3,2,6] }
assert my_solution.findMaximumLength(**test_input) == 3
test_input = { "nums": [32] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [38] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [60] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [79] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [85] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [170] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [198] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [220] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [318] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [350] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [381] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [413] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [426] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [429] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [431] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [445] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [488] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [492] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [497] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [515] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [582] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [589] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [620] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [632] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [703] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [748] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [776] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [977] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [986] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [990] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [29,859] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [48,612] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [76,837] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [103,341] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [171,323] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [248,719] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [253,61] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [274,467] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [351,665] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [372,382] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [404,409] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [455,40] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [472,843] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [504,838] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [549,747] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [570,810] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [621,809] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [643,802] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [689,192] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [709,481] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [742,67] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [745,725] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [773,877] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [776,962] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [791,434] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [824,783] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [840,388] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [876,264] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [940,694] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [959,372] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [981,998] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [41,340,784] }
assert my_solution.findMaximumLength(**test_input) == 3
test_input = { "nums": [103,652,579] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [116,635,966] }
assert my_solution.findMaximumLength(**test_input) == 3
test_input = { "nums": [137,32,745] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [247,173,316] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [276,315,947] }
assert my_solution.findMaximumLength(**test_input) == 3
test_input = { "nums": [278,754,912] }
assert my_solution.findMaximumLength(**test_input) == 3
test_input = { "nums": [314,882,708] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [371,101,367] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [402,305,990] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [403,553,697] }
assert my_solution.findMaximumLength(**test_input) == 3
test_input = { "nums": [431,780,315] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [479,322,44] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [512,234,679] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [513,847,778] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [525,177,936] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [588,42,18] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [646,174,827] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [680,242,726] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [769,131,241] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [780,591,213] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [783,23,848] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [787,201,30] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [791,470,87] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [797,181,492] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [868,4,455] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [881,306,316] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [886,116,68] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [893,531,805] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [926,641,145] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [964,624,279] }
assert my_solution.findMaximumLength(**test_input) == 1
test_input = { "nums": [987,694,396] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [38,986,134,156] }
assert my_solution.findMaximumLength(**test_input) == 2
test_input = { "nums": [43,236,417,521] }
assert my_solution.findMaximumLength(**test_input) == 4
test_input = { "nums": [58,890,892,52] }
assert my_solution.findMaximumLength(**test_input) == 3
test_input = { "nums": [81,738,403,654] }
assert my_solution.findMaximumLength(**test_input) == 3 | 1,700,922,600 |
weekly-contest-372-make-three-strings-equal | https://leetcode.com/problems/make-three-strings-equal/ | make-three-strings-equal | {
"questionId": "3207",
"questionFrontendId": "2937",
"title": "Make Three Strings Equal",
"titleSlug": "make-three-strings-equal",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 114,
"dislikes": 30,
"categoryTitle": "Algorithms"
} | """
You are given three strings s1, s2, and s3. You have to perform the following operation on these three strings as many times as you want.
In one operation you can choose one of these three strings such that its length is at least 2 and delete the rightmost character of it.
Return the minimum number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return -1.
Example 1:
Input: s1 = "abc", s2 = "abb", s3 = "ab"
Output: 2
Explanation: Performing operations on s1 and s2 once will lead to three equal strings.
It can be shown that there is no way to make them equal with less than two operations.
Example 2:
Input: s1 = "dac", s2 = "bac", s3 = "cac"
Output: -1
Explanation: Because the leftmost letters of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1.
Constraints:
1 <= s1.length, s2.length, s3.length <= 100
s1, s2 and s3 consist only of lowercase English letters.
"""
class Solution:
def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
| You are given three strings s1, s2, and s3. You have to perform the following operation on these three strings as many times as you want.
In one operation you can choose one of these three strings such that its length is at least 2 and delete the rightmost character of it.
Return the minimum number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return -1.
Example 1:
Input: s1 = "abc", s2 = "abb", s3 = "ab"
Output: 2
Explanation: Performing operations on s1 and s2 once will lead to three equal strings.
It can be shown that there is no way to make them equal with less than two operations.
Example 2:
Input: s1 = "dac", s2 = "bac", s3 = "cac"
Output: -1
Explanation: Because the leftmost letters of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1.
Constraints:
1 <= s1.length, s2.length, s3.length <= 100
s1, s2 and s3 consist only of lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
``` |
my_solution = Solution()
test_input = { "s1": "abc", "s2": "abb", "s3": "ab" }
assert my_solution.findMinimumOperations(**test_input) == 2
test_input = { "s1": "dac", "s2": "bac", "s3": "cac" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "a", "s2": "a", "s3": "a" }
assert my_solution.findMinimumOperations(**test_input) == 0
test_input = { "s1": "kui", "s2": "m", "s3": "v" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "a", "s2": "aabc", "s3": "a" }
assert my_solution.findMinimumOperations(**test_input) == 3
test_input = { "s1": "cc", "s2": "cccb", "s3": "c" }
assert my_solution.findMinimumOperations(**test_input) == 4
test_input = { "s1": "luso", "s2": "lu", "s3": "lu" }
assert my_solution.findMinimumOperations(**test_input) == 2
test_input = { "s1": "xx", "s2": "phe", "s3": "xie" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "gzd", "s2": "bcju", "s3": "db" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "cbba", "s2": "cbaa", "s3": "c" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "k", "s2": "kfb", "s3": "krcnf" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "oby", "s2": "obz", "s3": "obf" }
assert my_solution.findMinimumOperations(**test_input) == 3
test_input = { "s1": "b", "s2": "aba", "s3": "aaccaa" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "a", "s2": "accabb", "s3": "aaa" }
assert my_solution.findMinimumOperations(**test_input) == 7
test_input = { "s1": "b", "s2": "bccaaba", "s3": "ba" }
assert my_solution.findMinimumOperations(**test_input) == 7
test_input = { "s1": "b", "s2": "bacccab", "s3": "cc" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "ca", "s2": "cccabb", "s3": "cb" }
assert my_solution.findMinimumOperations(**test_input) == 7
test_input = { "s1": "ccb", "s2": "ccba", "s3": "ccb" }
assert my_solution.findMinimumOperations(**test_input) == 1
test_input = { "s1": "mbooi", "s2": "pdq", "s3": "br" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "xxfzj", "s2": "faho", "s3": "c" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "acbc", "s2": "acba", "s3": "acb" }
assert my_solution.findMinimumOperations(**test_input) == 2
test_input = { "s1": "aduyyk", "s2": "v", "s3": "lpyt" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "xd", "s2": "sl", "s3": "azoeaje" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "bbbaa", "s2": "bacab", "s3": "b" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "cibn", "s2": "ioom", "s3": "bxa" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "bcb", "s2": "bbac", "s3": "cbbc" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "aabbb", "s2": "cc", "s3": "cccb" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "i", "s2": "xqsfy", "s3": "diqae" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "iq", "s2": "iimanmy", "s3": "id" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "pitggt", "s2": "pi", "s3": "pih" }
assert my_solution.findMinimumOperations(**test_input) == 5
test_input = { "s1": "ten", "s2": "ten", "s3": "tenob" }
assert my_solution.findMinimumOperations(**test_input) == 2
test_input = { "s1": "vejy", "s2": "fbqfo", "s3": "gl" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "aca", "s2": "abcc", "s3": "accba" }
assert my_solution.findMinimumOperations(**test_input) == 9
test_input = { "s1": "br", "s2": "br", "s3": "brvhgtou" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "c", "s2": "bcc", "s3": "aacbcaca" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "cab", "s2": "caac", "s3": "cacbb" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "ccab", "s2": "cbbcbb", "s3": "ca" }
assert my_solution.findMinimumOperations(**test_input) == 9
test_input = { "s1": "inuc", "s2": "iwpdfj", "s3": "ib" }
assert my_solution.findMinimumOperations(**test_input) == 9
test_input = { "s1": "jrrpyyc", "s2": "jr", "s3": "jrt" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "l", "s2": "gebqrgnz", "s3": "jkr" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "naynn", "s2": "ax", "s3": "bhdcz" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "rexmx", "s2": "ujmbg", "s3": "gg" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "aacbcb", "s2": "a", "s3": "acaaac" }
assert my_solution.findMinimumOperations(**test_input) == 10
test_input = { "s1": "acbb", "s2": "acbacc", "s3": "acb" }
assert my_solution.findMinimumOperations(**test_input) == 4
test_input = { "s1": "baacbab", "s2": "bcc", "s3": "bca" }
assert my_solution.findMinimumOperations(**test_input) == 10
test_input = { "s1": "bcacbba", "s2": "bca", "s3": "bca" }
assert my_solution.findMinimumOperations(**test_input) == 4
test_input = { "s1": "bcaca", "s2": "bcaba", "s3": "bca" }
assert my_solution.findMinimumOperations(**test_input) == 4
test_input = { "s1": "ba", "s2": "bcbcab", "s3": "bbcbb" }
assert my_solution.findMinimumOperations(**test_input) == 10
test_input = { "s1": "cabc", "s2": "cab", "s3": "cabbac" }
assert my_solution.findMinimumOperations(**test_input) == 4
test_input = { "s1": "bbbbcaac", "s2": "a", "s3": "cbcc" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "gjbtodtym", "s2": "gxp", "s3": "g" }
assert my_solution.findMinimumOperations(**test_input) == 10
test_input = { "s1": "hfkq", "s2": "hfrbvno", "s3": "hf" }
assert my_solution.findMinimumOperations(**test_input) == 7
test_input = { "s1": "hym", "s2": "hl", "s3": "hshxmbbj" }
assert my_solution.findMinimumOperations(**test_input) == 10
test_input = { "s1": "mkdflu", "s2": "mmbn", "s3": "mge" }
assert my_solution.findMinimumOperations(**test_input) == 10
test_input = { "s1": "nvlobl", "s2": "mekbzd", "s3": "s" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "rpa", "s2": "rpaxpoh", "s3": "rpa" }
assert my_solution.findMinimumOperations(**test_input) == 4
test_input = { "s1": "ac", "s2": "aacccccbc", "s3": "acc" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "abb", "s2": "abaac", "s3": "abcaca" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "caa", "s2": "caccaccacb", "s3": "c" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "baccaab", "s2": "cababc", "s3": "a" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "cacbb", "s2": "ca", "s3": "cacbcac" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "cbba", "s2": "cabcabab", "s3": "ca" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "cb", "s2": "cbcbb", "s3": "cbaaabb" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "ccabaa", "s2": "ccabc", "s3": "cca" }
assert my_solution.findMinimumOperations(**test_input) == 5
test_input = { "s1": "ccb", "s2": "ccac", "s3": "cccaaca" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "ccccbb", "s2": "cccc", "s3": "cccc" }
assert my_solution.findMinimumOperations(**test_input) == 2
test_input = { "s1": "cxxq", "s2": "cxx", "s3": "cxxdeqh" }
assert my_solution.findMinimumOperations(**test_input) == 5
test_input = { "s1": "d", "s2": "dffjiulzya", "s3": "dke" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "dogv", "s2": "dofjkhx", "s3": "dog" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "dwefrocz", "s2": "dzz", "s3": "dwn" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "etr", "s2": "ejb", "s3": "etpubpvr" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "f", "s2": "morycy", "s3": "vledqoo" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "ful", "s2": "fular", "s3": "fulvkv" }
assert my_solution.findMinimumOperations(**test_input) == 5
test_input = { "s1": "kzwat", "s2": "ku", "s3": "koftvbd" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "qey", "s2": "qevtkbss", "s3": "qeb" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "saqy", "s2": "hvufcpko", "s3": "xm" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "tllwgcdi", "s2": "t", "s3": "tvham" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "vmwdmadn", "s2": "vmw", "s3": "vmw" }
assert my_solution.findMinimumOperations(**test_input) == 5
test_input = { "s1": "xobwwhu", "s2": "xobb", "s3": "xob" }
assert my_solution.findMinimumOperations(**test_input) == 5
test_input = { "s1": "yptajimiz", "s2": "yp", "s3": "ypr" }
assert my_solution.findMinimumOperations(**test_input) == 8
test_input = { "s1": "z", "s2": "zcrouxlukb", "s3": "zbb" }
assert my_solution.findMinimumOperations(**test_input) == 11
test_input = { "s1": "aaabc", "s2": "aaaa", "s3": "aaaabc" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "aaa", "s2": "aab", "s3": "aabaacaab" }
assert my_solution.findMinimumOperations(**test_input) == 9
test_input = { "s1": "aac", "s2": "aac", "s3": "aacabbbca" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "abaab", "s2": "abaabb", "s3": "abaa" }
assert my_solution.findMinimumOperations(**test_input) == 3
test_input = { "s1": "abcomon", "s2": "gkuneup", "s3": "q" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "acc", "s2": "accacb", "s3": "acbabc" }
assert my_solution.findMinimumOperations(**test_input) == 9
test_input = { "s1": "cca", "s2": "caaab", "s3": "babbacc" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "bccbc", "s2": "bc", "s3": "bcccbcac" }
assert my_solution.findMinimumOperations(**test_input) == 9
test_input = { "s1": "bpp", "s2": "bin", "s3": "bfkbyhubw" }
assert my_solution.findMinimumOperations(**test_input) == 12
test_input = { "s1": "bbsyg", "s2": "blbp", "s3": "brghkr" }
assert my_solution.findMinimumOperations(**test_input) == 12
test_input = { "s1": "bxpvamp", "s2": "bxpv", "s3": "bxpv" }
assert my_solution.findMinimumOperations(**test_input) == 3
test_input = { "s1": "ccbabca", "s2": "cbcbaca", "s3": "c" }
assert my_solution.findMinimumOperations(**test_input) == 12
test_input = { "s1": "accb", "s2": "bbc", "s3": "cbbaccba" }
assert my_solution.findMinimumOperations(**test_input) == -1
test_input = { "s1": "cbccc", "s2": "cbc", "s3": "cbcccba" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "cac", "s2": "ccacc", "s3": "cabacba" }
assert my_solution.findMinimumOperations(**test_input) == 12
test_input = { "s1": "ccb", "s2": "ccbcb", "s3": "ccbccab" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "caacabcbc", "s2": "ccb", "s3": "ccc" }
assert my_solution.findMinimumOperations(**test_input) == 12
test_input = { "s1": "cccabaacc", "s2": "ccc", "s3": "ccc" }
assert my_solution.findMinimumOperations(**test_input) == 6
test_input = { "s1": "ajjdre", "s2": "gsrq", "s3": "eurcj" }
assert my_solution.findMinimumOperations(**test_input) == -1 | 1,700,361,000 |
weekly-contest-372-separate-black-and-white-balls | https://leetcode.com/problems/separate-black-and-white-balls | separate-black-and-white-balls | {
"questionId": "3195",
"questionFrontendId": "2938",
"title": "Separate Black and White Balls",
"titleSlug": "separate-black-and-white-balls",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 133,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
There are n balls on a table, each ball has a color black or white.
You are given a 0-indexed binary string s of length n, where 1 and 0 represent black and white balls, respectively.
In each step, you can choose two adjacent balls and swap them.
Return the minimum number of steps to group all the black balls to the right and all the white balls to the left.
Example 1:
Input: s = "101"
Output: 1
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.
Example 2:
Input: s = "100"
Output: 2
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.
Example 3:
Input: s = "0111"
Output: 0
Explanation: All the black balls are already grouped to the right.
Constraints:
1 <= n == s.length <= 105
s[i] is either '0' or '1'.
"""
class Solution:
def minimumSteps(self, s: str) -> int:
| There are n balls on a table, each ball has a color black or white.
You are given a 0-indexed binary string s of length n, where 1 and 0 represent black and white balls, respectively.
In each step, you can choose two adjacent balls and swap them.
Return the minimum number of steps to group all the black balls to the right and all the white balls to the left.
Example 1:
Input: s = "101"
Output: 1
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.
Example 2:
Input: s = "100"
Output: 2
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.
Example 3:
Input: s = "0111"
Output: 0
Explanation: All the black balls are already grouped to the right.
Constraints:
1 <= n == s.length <= 105
s[i] is either '0' or '1'.
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumSteps(self, s: str) -> int:
``` |
my_solution = Solution()
test_input = { "s": "101" }
assert my_solution.minimumSteps(**test_input) == 1
test_input = { "s": "100" }
assert my_solution.minimumSteps(**test_input) == 2
test_input = { "s": "0111" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "11000111" }
assert my_solution.minimumSteps(**test_input) == 6
test_input = { "s": "01010001" }
assert my_solution.minimumSteps(**test_input) == 7
test_input = { "s": "0100101" }
assert my_solution.minimumSteps(**test_input) == 4
test_input = { "s": "111111111100100010" }
assert my_solution.minimumSteps(**test_input) == 65
test_input = { "s": "10100000110010011010" }
assert my_solution.minimumSteps(**test_input) == 44
test_input = { "s": "1101110000111011110" }
assert my_solution.minimumSteps(**test_input) == 42
test_input = { "s": "01000010111010001" }
assert my_solution.minimumSteps(**test_input) == 29
test_input = { "s": "11110" }
assert my_solution.minimumSteps(**test_input) == 4
test_input = { "s": "010001001011010" }
assert my_solution.minimumSteps(**test_input) == 21
test_input = { "s": "0011011" }
assert my_solution.minimumSteps(**test_input) == 2
test_input = { "s": "001" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "000100100" }
assert my_solution.minimumSteps(**test_input) == 6
test_input = { "s": "00110" }
assert my_solution.minimumSteps(**test_input) == 2
test_input = { "s": "001110001110001" }
assert my_solution.minimumSteps(**test_input) == 27
test_input = { "s": "10000000001" }
assert my_solution.minimumSteps(**test_input) == 9
test_input = { "s": "01" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "0100011100001100100" }
assert my_solution.minimumSteps(**test_input) == 45
test_input = { "s": "1010110" }
assert my_solution.minimumSteps(**test_input) == 7
test_input = { "s": "010010000" }
assert my_solution.minimumSteps(**test_input) == 10
test_input = { "s": "100110" }
assert my_solution.minimumSteps(**test_input) == 5
test_input = { "s": "1100" }
assert my_solution.minimumSteps(**test_input) == 4
test_input = { "s": "000110" }
assert my_solution.minimumSteps(**test_input) == 2
test_input = { "s": "001101101111000000" }
assert my_solution.minimumSteps(**test_input) == 54
test_input = { "s": "011101011" }
assert my_solution.minimumSteps(**test_input) == 7
test_input = { "s": "0110111111110" }
assert my_solution.minimumSteps(**test_input) == 12
test_input = { "s": "1111001111111011111" }
assert my_solution.minimumSteps(**test_input) == 19
test_input = { "s": "0101111" }
assert my_solution.minimumSteps(**test_input) == 1
test_input = { "s": "00010101100110011" }
assert my_solution.minimumSteps(**test_input) == 23
test_input = { "s": "01010110110111011001" }
assert my_solution.minimumSteps(**test_input) == 44
test_input = { "s": "0" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "1101111010111001010" }
assert my_solution.minimumSteps(**test_input) == 58
test_input = { "s": "01100010" }
assert my_solution.minimumSteps(**test_input) == 9
test_input = { "s": "10010000010" }
assert my_solution.minimumSteps(**test_input) == 15
test_input = { "s": "11011101000100001" }
assert my_solution.minimumSteps(**test_input) == 53
test_input = { "s": "00" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "10100101011001100111" }
assert my_solution.minimumSteps(**test_input) == 40
test_input = { "s": "0101" }
assert my_solution.minimumSteps(**test_input) == 1
test_input = { "s": "00101010100" }
assert my_solution.minimumSteps(**test_input) == 14
test_input = { "s": "00110111110010" }
assert my_solution.minimumSteps(**test_input) == 24
test_input = { "s": "111010000001000001" }
assert my_solution.minimumSteps(**test_input) == 52
test_input = { "s": "0101101000111010" }
assert my_solution.minimumSteps(**test_input) == 31
test_input = { "s": "1010111111" }
assert my_solution.minimumSteps(**test_input) == 3
test_input = { "s": "110110001001110" }
assert my_solution.minimumSteps(**test_input) == 32
test_input = { "s": "111010" }
assert my_solution.minimumSteps(**test_input) == 7
test_input = { "s": "10" }
assert my_solution.minimumSteps(**test_input) == 1
test_input = { "s": "111110" }
assert my_solution.minimumSteps(**test_input) == 5
test_input = { "s": "0111100000101101110" }
assert my_solution.minimumSteps(**test_input) == 42
test_input = { "s": "111010100" }
assert my_solution.minimumSteps(**test_input) == 17
test_input = { "s": "0001000100111000" }
assert my_solution.minimumSteps(**test_input) == 22
test_input = { "s": "011001111110110010" }
assert my_solution.minimumSteps(**test_input) == 43
test_input = { "s": "11100" }
assert my_solution.minimumSteps(**test_input) == 6
test_input = { "s": "0100001101110001" }
assert my_solution.minimumSteps(**test_input) == 25
test_input = { "s": "111" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "100010" }
assert my_solution.minimumSteps(**test_input) == 5
test_input = { "s": "101001" }
assert my_solution.minimumSteps(**test_input) == 5
test_input = { "s": "00111010100100100111" }
assert my_solution.minimumSteps(**test_input) == 43
test_input = { "s": "100010111" }
assert my_solution.minimumSteps(**test_input) == 5
test_input = { "s": "001110000101011" }
assert my_solution.minimumSteps(**test_input) == 21
test_input = { "s": "111011011101101" }
assert my_solution.minimumSteps(**test_input) == 26
test_input = { "s": "1110101" }
assert my_solution.minimumSteps(**test_input) == 7
test_input = { "s": "00111010100111" }
assert my_solution.minimumSteps(**test_input) == 17
test_input = { "s": "010" }
assert my_solution.minimumSteps(**test_input) == 1
test_input = { "s": "111001" }
assert my_solution.minimumSteps(**test_input) == 6
test_input = { "s": "1100011" }
assert my_solution.minimumSteps(**test_input) == 6
test_input = { "s": "0110001011001110111" }
assert my_solution.minimumSteps(**test_input) == 27
test_input = { "s": "0001111111011101" }
assert my_solution.minimumSteps(**test_input) == 17
test_input = { "s": "0110110100001" }
assert my_solution.minimumSteps(**test_input) == 26
test_input = { "s": "11110110011000011" }
assert my_solution.minimumSteps(**test_input) == 48
test_input = { "s": "10100" }
assert my_solution.minimumSteps(**test_input) == 5
test_input = { "s": "10100001" }
assert my_solution.minimumSteps(**test_input) == 9
test_input = { "s": "01000011000" }
assert my_solution.minimumSteps(**test_input) == 13
test_input = { "s": "110001001001" }
assert my_solution.minimumSteps(**test_input) == 20
test_input = { "s": "01100011010011110" }
assert my_solution.minimumSteps(**test_input) == 29
test_input = { "s": "01111001111" }
assert my_solution.minimumSteps(**test_input) == 8
test_input = { "s": "1110100010000110110" }
assert my_solution.minimumSteps(**test_input) == 51
test_input = { "s": "1101000111001111100" }
assert my_solution.minimumSteps(**test_input) == 45
test_input = { "s": "0110000" }
assert my_solution.minimumSteps(**test_input) == 8
test_input = { "s": "001010010010" }
assert my_solution.minimumSteps(**test_input) == 15
test_input = { "s": "11101" }
assert my_solution.minimumSteps(**test_input) == 3
test_input = { "s": "00110110" }
assert my_solution.minimumSteps(**test_input) == 6
test_input = { "s": "001010" }
assert my_solution.minimumSteps(**test_input) == 3
test_input = { "s": "000101010101010" }
assert my_solution.minimumSteps(**test_input) == 21
test_input = { "s": "1111011" }
assert my_solution.minimumSteps(**test_input) == 4
test_input = { "s": "0000101011001101011" }
assert my_solution.minimumSteps(**test_input) == 24
test_input = { "s": "011010" }
assert my_solution.minimumSteps(**test_input) == 5
test_input = { "s": "110010100011110101" }
assert my_solution.minimumSteps(**test_input) == 36
test_input = { "s": "000000001" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "011001" }
assert my_solution.minimumSteps(**test_input) == 4
test_input = { "s": "01111010011110111111" }
assert my_solution.minimumSteps(**test_input) == 23
test_input = { "s": "10001111" }
assert my_solution.minimumSteps(**test_input) == 3
test_input = { "s": "11011010001111" }
assert my_solution.minimumSteps(**test_input) == 21
test_input = { "s": "0110101010100000111" }
assert my_solution.minimumSteps(**test_input) == 44
test_input = { "s": "0000001000110" }
assert my_solution.minimumSteps(**test_input) == 6
test_input = { "s": "1" }
assert my_solution.minimumSteps(**test_input) == 0
test_input = { "s": "0111100110100" }
assert my_solution.minimumSteps(**test_input) == 28
test_input = { "s": "101010110011101011" }
assert my_solution.minimumSteps(**test_input) == 33
test_input = { "s": "1011101" }
assert my_solution.minimumSteps(**test_input) == 5 | 1,700,361,000 |
weekly-contest-372-maximum-xor-product | https://leetcode.com/problems/maximum-xor-product | maximum-xor-product | {
"questionId": "3192",
"questionFrontendId": "2939",
"title": "Maximum Xor Product",
"titleSlug": "maximum-xor-product",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 140,
"dislikes": 63,
"categoryTitle": "Algorithms"
} | """
Given three integers a, b, and n, return the maximum value of (a XOR x) * (b XOR x) where 0 <= x < 2n.
Since the answer may be too large, return it modulo 109 + 7.
Note that XOR is the bitwise XOR operation.
Example 1:
Input: a = 12, b = 5, n = 4
Output: 98
Explanation: For x = 2, (a XOR x) = 14 and (b XOR x) = 7. Hence, (a XOR x) * (b XOR x) = 98.
It can be shown that 98 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Example 2:
Input: a = 6, b = 7 , n = 5
Output: 930
Explanation: For x = 25, (a XOR x) = 31 and (b XOR x) = 30. Hence, (a XOR x) * (b XOR x) = 930.
It can be shown that 930 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Example 3:
Input: a = 1, b = 6, n = 3
Output: 12
Explanation: For x = 5, (a XOR x) = 4 and (b XOR x) = 3. Hence, (a XOR x) * (b XOR x) = 12.
It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Constraints:
0 <= a, b < 250
0 <= n <= 50
"""
class Solution:
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
| Given three integers a, b, and n, return the maximum value of (a XOR x) * (b XOR x) where 0 <= x < 2n.
Since the answer may be too large, return it modulo 109 + 7.
Note that XOR is the bitwise XOR operation.
Example 1:
Input: a = 12, b = 5, n = 4
Output: 98
Explanation: For x = 2, (a XOR x) = 14 and (b XOR x) = 7. Hence, (a XOR x) * (b XOR x) = 98.
It can be shown that 98 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Example 2:
Input: a = 6, b = 7 , n = 5
Output: 930
Explanation: For x = 25, (a XOR x) = 31 and (b XOR x) = 30. Hence, (a XOR x) * (b XOR x) = 930.
It can be shown that 930 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Example 3:
Input: a = 1, b = 6, n = 3
Output: 12
Explanation: For x = 5, (a XOR x) = 4 and (b XOR x) = 3. Hence, (a XOR x) * (b XOR x) = 12.
It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0 <= x < 2n.
Constraints:
0 <= a, b < 250
0 <= n <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
``` |
my_solution = Solution()
test_input = { "a": 12, "b": 5, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 98
test_input = { "a": 6, "b": 7, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 930
test_input = { "a": 1, "b": 6, "n": 3 }
assert my_solution.maximumXorProduct(**test_input) == 12
test_input = { "a": 0, "b": 0, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 1
test_input = { "a": 0, "b": 1, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3906
test_input = { "a": 0, "b": 2, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 15875
test_input = { "a": 0, "b": 3, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 2
test_input = { "a": 0, "b": 4, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 0
test_input = { "a": 0, "b": 5, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3658
test_input = { "a": 0, "b": 6, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 7
test_input = { "a": 0, "b": 7, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 12
test_input = { "a": 0, "b": 8, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 713
test_input = { "a": 0, "b": 9, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 30
test_input = { "a": 0, "b": 10, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 14875
test_input = { "a": 0, "b": 11, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 84
test_input = { "a": 0, "b": 12, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 45
test_input = { "a": 0, "b": 13, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 42
test_input = { "a": 0, "b": 14, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 0
test_input = { "a": 0, "b": 15, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3080
test_input = { "a": 1, "b": 0, "n": 3 }
assert my_solution.maximumXorProduct(**test_input) == 42
test_input = { "a": 1, "b": 1, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 225
test_input = { "a": 1, "b": 2, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3782
test_input = { "a": 1, "b": 3, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 3
test_input = { "a": 1, "b": 4, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 810
test_input = { "a": 1, "b": 5, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 165
test_input = { "a": 1, "b": 6, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 132
test_input = { "a": 1, "b": 7, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 15
test_input = { "a": 1, "b": 8, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 8
test_input = { "a": 1, "b": 9, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 33
test_input = { "a": 1, "b": 10, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 14756
test_input = { "a": 1, "b": 11, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 14875
test_input = { "a": 1, "b": 12, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 14518
test_input = { "a": 1, "b": 13, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 13
test_input = { "a": 1, "b": 14, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 552
test_input = { "a": 1, "b": 15, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 63
test_input = { "a": 2, "b": 0, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 0
test_input = { "a": 2, "b": 1, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 2
test_input = { "a": 2, "b": 2, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 4
test_input = { "a": 2, "b": 3, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 930
test_input = { "a": 2, "b": 4, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 143
test_input = { "a": 2, "b": 5, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 756
test_input = { "a": 2, "b": 6, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 165
test_input = { "a": 2, "b": 7, "n": 3 }
assert my_solution.maximumXorProduct(**test_input) == 18
test_input = { "a": 2, "b": 8, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 27
test_input = { "a": 2, "b": 9, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 24
test_input = { "a": 2, "b": 10, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 713
test_input = { "a": 2, "b": 11, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 22
test_input = { "a": 2, "b": 12, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 39
test_input = { "a": 2, "b": 13, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 36
test_input = { "a": 2, "b": 14, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 28
test_input = { "a": 2, "b": 15, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 42
test_input = { "a": 3, "b": 0, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3782
test_input = { "a": 3, "b": 1, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3843
test_input = { "a": 3, "b": 2, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 6
test_input = { "a": 3, "b": 3, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 16129
test_input = { "a": 3, "b": 4, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 756
test_input = { "a": 3, "b": 5, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 143
test_input = { "a": 3, "b": 6, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 18
test_input = { "a": 3, "b": 7, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 21
test_input = { "a": 3, "b": 8, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3300
test_input = { "a": 3, "b": 9, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 27
test_input = { "a": 3, "b": 10, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 690
test_input = { "a": 3, "b": 11, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 33
test_input = { "a": 3, "b": 12, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 36
test_input = { "a": 3, "b": 13, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 39
test_input = { "a": 3, "b": 14, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 42
test_input = { "a": 3, "b": 15, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3245
test_input = { "a": 4, "b": 0, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 165
test_input = { "a": 4, "b": 1, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 18
test_input = { "a": 4, "b": 2, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 15
test_input = { "a": 4, "b": 3, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 12
test_input = { "a": 4, "b": 4, "n": 3 }
assert my_solution.maximumXorProduct(**test_input) == 49
test_input = { "a": 4, "b": 5, "n": 3 }
assert my_solution.maximumXorProduct(**test_input) == 42
test_input = { "a": 4, "b": 6, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 35
test_input = { "a": 4, "b": 7, "n": 3 }
assert my_solution.maximumXorProduct(**test_input) == 30
test_input = { "a": 4, "b": 8, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 77
test_input = { "a": 4, "b": 9, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 14518
test_input = { "a": 4, "b": 10, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 40
test_input = { "a": 4, "b": 11, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 552
test_input = { "a": 4, "b": 12, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3465
test_input = { "a": 4, "b": 13, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 14994
test_input = { "a": 4, "b": 14, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 667
test_input = { "a": 4, "b": 15, "n": 6 }
assert my_solution.maximumXorProduct(**test_input) == 3300
test_input = { "a": 5, "b": 0, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 4
test_input = { "a": 5, "b": 1, "n": 3 }
assert my_solution.maximumXorProduct(**test_input) == 21
test_input = { "a": 5, "b": 2, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 12
test_input = { "a": 5, "b": 3, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 15375
test_input = { "a": 5, "b": 4, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 16002
test_input = { "a": 5, "b": 5, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 25
test_input = { "a": 5, "b": 6, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 30
test_input = { "a": 5, "b": 7, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 35
test_input = { "a": 5, "b": 8, "n": 2 }
assert my_solution.maximumXorProduct(**test_input) == 70
test_input = { "a": 5, "b": 9, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 77
test_input = { "a": 5, "b": 10, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 552
test_input = { "a": 5, "b": 11, "n": 7 }
assert my_solution.maximumXorProduct(**test_input) == 14399
test_input = { "a": 5, "b": 12, "n": 1 }
assert my_solution.maximumXorProduct(**test_input) == 60
test_input = { "a": 5, "b": 13, "n": 5 }
assert my_solution.maximumXorProduct(**test_input) == 713
test_input = { "a": 5, "b": 14, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 84
test_input = { "a": 5, "b": 15, "n": 0 }
assert my_solution.maximumXorProduct(**test_input) == 75
test_input = { "a": 6, "b": 0, "n": 4 }
assert my_solution.maximumXorProduct(**test_input) == 143 | 1,700,361,000 |
weekly-contest-372-find-building-where-alice-and-bob-can-meet | https://leetcode.com/problems/find-building-where-alice-and-bob-can-meet | find-building-where-alice-and-bob-can-meet | {
"questionId": "3181",
"questionFrontendId": "2940",
"title": "Find Building Where Alice and Bob Can Meet",
"titleSlug": "find-building-where-alice-and-bob-can-meet",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 171,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array heights of positive integers, where heights[i] represents the height of the ith building.
If a person is in building i, they can move to any other building j if and only if i < j and heights[i] < heights[j].
You are also given another array queries where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi.
Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the ith query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1.
Example 1:
Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
Output: [2,5,-1,5,2]
Explanation: In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2].
In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5].
In the third query, Alice cannot meet Bob since Alice cannot move to any other building.
In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5].
In the fifth query, Alice and Bob are already in the same building.
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Example 2:
Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]
Output: [7,6,-1,4,6]
Explanation: In the first query, Alice can directly move to Bob's building since heights[0] < heights[7].
In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6].
In the third query, Alice cannot meet Bob since Bob cannot move to any other building.
In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4].
In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6].
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Constraints:
1 <= heights.length <= 5 * 104
1 <= heights[i] <= 109
1 <= queries.length <= 5 * 104
queries[i] = [ai, bi]
0 <= ai, bi <= heights.length - 1
"""
class Solution:
def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]:
| You are given a 0-indexed array heights of positive integers, where heights[i] represents the height of the ith building.
If a person is in building i, they can move to any other building j if and only if i < j and heights[i] < heights[j].
You are also given another array queries where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi.
Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the ith query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1.
Example 1:
Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
Output: [2,5,-1,5,2]
Explanation: In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2].
In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5].
In the third query, Alice cannot meet Bob since Alice cannot move to any other building.
In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5].
In the fifth query, Alice and Bob are already in the same building.
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Example 2:
Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]
Output: [7,6,-1,4,6]
Explanation: In the first query, Alice can directly move to Bob's building since heights[0] < heights[7].
In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6].
In the third query, Alice cannot meet Bob since Bob cannot move to any other building.
In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4].
In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6].
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Constraints:
1 <= heights.length <= 5 * 104
1 <= heights[i] <= 109
1 <= queries.length <= 5 * 104
queries[i] = [ai, bi]
0 <= ai, bi <= heights.length - 1
Please complete the code below to solve above prblem:
```python
class Solution:
def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "heights": [6,4,8,5,2,7], "queries": [[0,1],[0,3],[2,4],[3,4],[2,2]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [2,5,-1,5,2]
test_input = { "heights": [5,3,8,2,6,1,4,6], "queries": [[0,7],[3,5],[5,2],[3,0],[1,6]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [7,6,-1,4,6]
test_input = { "heights": [1], "queries": [[0,0]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0]
test_input = { "heights": [1000000000], "queries": [[0,0]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0]
test_input = { "heights": [1,2], "queries": [[0,0],[0,1],[1,0],[1,1]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,1,1]
test_input = { "heights": [2,1], "queries": [[0,0],[0,1],[1,0],[1,1]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,1]
test_input = { "heights": [1,2,3], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,1,1,2,2,2,2]
test_input = { "heights": [1,3,2], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,1,1,-1,2,-1,2]
test_input = { "heights": [2,1,3], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,2,1,2,2,2,2]
test_input = { "heights": [2,3,1], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,-1,1,1,-1,-1,-1,2]
test_input = { "heights": [3,1,2], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,1,2,-1,2,2]
test_input = { "heights": [3,2,1], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,1,-1,-1,-1,2]
test_input = { "heights": [1,2,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,2,3,2,2,2,3,3,3,3,3]
test_input = { "heights": [1,2,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,2,3,2,2,2,-1,3,3,-1,3]
test_input = { "heights": [1,3,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,3,3,2,3,2,3,3,3,3,3]
test_input = { "heights": [1,3,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,2,-1,2,2,2,-1,3,-1,-1,3]
test_input = { "heights": [1,4,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,-1,-1,2,-1,2,3,3,-1,3,3]
test_input = { "heights": [1,4,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,-1,-1,2,-1,2,-1,3,-1,-1,3]
test_input = { "heights": [2,1,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,2,1,2,3,2,2,2,3,3,3,3,3]
test_input = { "heights": [2,1,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,2,1,2,3,2,2,2,-1,3,3,-1,3]
test_input = { "heights": [2,3,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,1,1,3,3,3,3,2,3,3,3,3,3]
test_input = { "heights": [2,3,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,-1,1,1,2,-1,2,2,2,-1,-1,-1,-1,3]
test_input = { "heights": [2,4,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,1,1,-1,-1,3,-1,2,3,3,-1,3,3]
test_input = { "heights": [2,4,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,-1,1,1,-1,-1,2,-1,2,-1,-1,-1,-1,3]
test_input = { "heights": [3,1,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,3,1,2,3,3,2,2,3,3,3,3,3]
test_input = { "heights": [3,1,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,-1,2,1,2,3,2,2,2,-1,-1,3,-1,3]
test_input = { "heights": [3,2,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,3,1,3,3,3,3,2,3,3,3,3,3]
test_input = { "heights": [3,2,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,-1,2,1,2,-1,2,2,2,-1,-1,-1,-1,3]
test_input = { "heights": [3,4,1,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,-1,-1,1,1,-1,-1,-1,-1,2,3,-1,-1,3,3]
test_input = { "heights": [3,4,2,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,-1,-1,1,1,-1,-1,-1,-1,2,-1,-1,-1,-1,3]
test_input = { "heights": [4,1,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,2,3,-1,2,2,3,-1,3,3,3]
test_input = { "heights": [4,1,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,2,3,-1,2,2,-1,-1,3,-1,3]
test_input = { "heights": [4,2,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,3,3,-1,3,2,3,-1,3,3,3]
test_input = { "heights": [4,2,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,2,-1,-1,2,2,-1,-1,-1,-1,3]
test_input = { "heights": [4,3,1,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,-1,-1,-1,-1,2,3,-1,-1,3,3]
test_input = { "heights": [4,3,2,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,-1,-1,-1,-1,2,-1,-1,-1,-1,3]
test_input = { "heights": [1,2,3,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,3,4,3,3,3,3,4,4,4,4,4,4]
test_input = { "heights": [1,2,3,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,3,4,3,3,3,3,-1,4,4,4,-1,4]
test_input = { "heights": [1,2,4,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,4,4,3,3,4,3,4,4,4,4,4,4]
test_input = { "heights": [1,2,4,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,3,-1,3,3,3,3,-1,4,4,-1,-1,4]
test_input = { "heights": [1,2,5,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,4,4,4,-1,4,4]
test_input = { "heights": [1,2,5,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,-1,4,4,-1,-1,4]
test_input = { "heights": [1,3,2,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,4,2,3,2,3,4,3,3,3,3,4,4,4,4,4,4]
test_input = { "heights": [1,3,2,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,4,2,3,2,3,4,3,3,3,3,-1,4,4,4,-1,4]
test_input = { "heights": [1,3,4,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,4,4,2,2,2,4,4,3,4,4,3,4,4,4,4,4,4]
test_input = { "heights": [1,3,4,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,-1,2,2,2,3,-1,3,3,3,3,-1,4,-1,-1,-1,4]
test_input = { "heights": [1,3,5,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,4,4,2,2,2,-1,-1,3,4,-1,3,4,4,4,-1,4,4]
test_input = { "heights": [1,3,5,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,-1,2,2,2,-1,-1,3,3,-1,3,-1,4,-1,-1,-1,4]
test_input = { "heights": [1,4,2,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,4,4,4,2,4,2,3,4,3,4,3,3,4,4,4,4,4,4]
test_input = { "heights": [1,4,2,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,-1,2,3,2,3,4,3,3,3,3,-1,4,-1,4,-1,4]
test_input = { "heights": [1,4,3,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,4,4,4,2,4,2,4,4,3,4,4,3,4,4,4,4,4,4]
test_input = { "heights": [1,4,3,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,-1,2,3,2,3,-1,3,3,3,3,-1,4,-1,-1,-1,4]
test_input = { "heights": [1,4,5,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,-1,-1,2,2,2,-1,-1,3,-1,-1,3,4,4,-1,-1,4,4]
test_input = { "heights": [1,4,5,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,-1,-1,2,2,2,-1,-1,3,-1,-1,3,-1,4,-1,-1,-1,4]
test_input = { "heights": [1,5,2,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,3,4,3,-1,3,3,4,4,-1,4,4,4]
test_input = { "heights": [1,5,2,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,3,4,3,-1,3,3,-1,4,-1,4,-1,4]
test_input = { "heights": [1,5,3,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,4,4,3,-1,4,3,4,4,-1,4,4,4]
test_input = { "heights": [1,5,3,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,3,-1,3,-1,3,3,-1,4,-1,-1,-1,4]
test_input = { "heights": [1,5,4,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,-1,-1,3,-1,-1,3,4,4,-1,-1,4,4]
test_input = { "heights": [1,5,4,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,-1,-1,3,-1,-1,3,-1,4,-1,-1,-1,4]
test_input = { "heights": [2,1,3,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,3,4,3,3,3,3,4,4,4,4,4,4]
test_input = { "heights": [2,1,3,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,3,4,3,3,3,3,-1,4,4,4,-1,4]
test_input = { "heights": [2,1,4,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,4,4,3,3,4,3,4,4,4,4,4,4]
test_input = { "heights": [2,1,4,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,3,-1,3,3,3,3,-1,4,4,-1,-1,4]
test_input = { "heights": [2,1,5,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,4,4,4,-1,4,4]
test_input = { "heights": [2,1,5,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,-1,4,4,-1,-1,4]
test_input = { "heights": [2,3,1,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,3,3,4,3,3,2,3,4,3,3,3,3,4,4,4,4,4,4]
test_input = { "heights": [2,3,1,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,3,3,4,3,3,2,3,4,3,3,3,3,-1,4,4,4,-1,4]
test_input = { "heights": [2,3,4,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,2,4,4,2,2,2,4,4,4,4,4,3,4,4,4,4,4,4]
test_input = { "heights": [2,3,4,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,2,3,-1,2,2,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [2,3,5,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,2,4,4,2,2,2,-1,-1,4,4,-1,3,4,4,4,-1,4,4]
test_input = { "heights": [2,3,5,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,2,3,-1,2,2,2,-1,-1,3,3,-1,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [2,4,1,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,4,4,4,3,4,2,3,4,3,4,3,3,4,4,4,4,4,4]
test_input = { "heights": [2,4,1,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,3,3,-1,3,3,2,3,4,3,3,3,3,-1,4,-1,4,-1,4]
test_input = { "heights": [2,4,3,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,4,4,4,2,4,2,4,4,4,4,4,3,4,4,4,4,4,4]
test_input = { "heights": [2,4,3,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,3,3,-1,2,3,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [2,4,5,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,2,-1,-1,2,2,2,-1,-1,4,-1,-1,3,4,4,-1,-1,4,4]
test_input = { "heights": [2,4,5,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,2,-1,-1,2,2,2,-1,-1,3,-1,-1,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [2,5,1,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,-1,-1,-1,3,-1,2,3,4,3,-1,3,3,4,4,-1,4,4,4]
test_input = { "heights": [2,5,1,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,-1,-1,-1,3,-1,2,3,4,3,-1,3,3,-1,4,-1,4,-1,4]
test_input = { "heights": [2,5,3,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,-1,-1,-1,2,-1,2,4,4,4,-1,4,3,4,4,-1,4,4,4]
test_input = { "heights": [2,5,3,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,-1,-1,-1,2,-1,2,3,-1,3,-1,3,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [2,5,4,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,-1,-1,-1,2,-1,2,-1,-1,4,-1,-1,3,4,4,-1,-1,4,4]
test_input = { "heights": [2,5,4,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,-1,-1,-1,2,-1,2,-1,-1,3,-1,-1,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [3,1,2,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,2,3,4,3,2,2,3,4,3,3,3,3,4,4,4,4,4,4]
test_input = { "heights": [3,1,2,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,2,3,4,3,2,2,3,4,3,3,3,3,-1,4,4,4,-1,4]
test_input = { "heights": [3,1,4,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,3,4,2,2,2,4,4,4,3,4,3,4,4,4,4,4,4]
test_input = { "heights": [3,1,4,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,4,2,2,2,3,-1,3,3,3,3,-1,-1,4,-1,-1,4]
test_input = { "heights": [3,1,5,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,3,4,2,2,2,-1,-1,4,3,-1,3,4,4,4,-1,4,4]
test_input = { "heights": [3,1,5,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,-1,-1,4,-1,-1,4]
test_input = { "heights": [3,2,1,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,3,3,4,3,3,2,3,4,3,3,3,3,4,4,4,4,4,4]
test_input = { "heights": [3,2,1,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,3,3,4,3,3,2,3,4,3,3,3,3,-1,4,4,4,-1,4]
test_input = { "heights": [3,2,4,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,4,4,2,2,2,4,4,4,4,4,3,4,4,4,4,4,4]
test_input = { "heights": [3,2,4,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,-1,2,2,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [3,2,5,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,4,4,2,2,2,-1,-1,4,4,-1,3,4,4,4,-1,4,4]
test_input = { "heights": [3,2,5,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,-1,2,2,2,-1,-1,3,3,-1,3,-1,-1,-1,-1,-1,4]
test_input = { "heights": [3,4,1,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,4,4,4,1,1,4,4,4,4,4,2,3,4,4,4,3,3,4,4,4,4,4,4]
test_input = { "heights": [3,4,1,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,-1,1,1,3,3,-1,3,3,2,3,4,3,3,3,3,-1,-1,-1,4,-1,4]
test_input = { "heights": [3,4,2,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,4,4,4,1,1,4,4,4,4,4,2,4,4,4,4,4,3,4,4,4,4,4,4]
test_input = { "heights": [3,4,2,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] }
assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,-1,1,1,3,3,-1,3,3,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4] | 1,700,361,000 |
weekly-contest-371-maximum-strong-pair-xor-i | https://leetcode.com/problems/maximum-strong-pair-xor-i | maximum-strong-pair-xor-i | {
"questionId": "3193",
"questionFrontendId": "2932",
"title": "Maximum Strong Pair XOR I",
"titleSlug": "maximum-strong-pair-xor-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 77,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums. A pair of integers x and y is called a strong pair if it satisfies the condition:
|x - y| <= min(x, y)
You need to select two integers from nums such that they form a strong pair and their bitwise XOR is the maximum among all strong pairs in the array.
Return the maximum XOR value out of all possible strong pairs in the array nums.
Note that you can pick the same integer twice to form a pair.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 7
Explanation: There are 11 strong pairs in the array nums: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
Example 2:
Input: nums = [10,100]
Output: 0
Explanation: There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
Example 3:
Input: nums = [5,6,25,30]
Output: 7
Explanation: There are 6 strong pairs in the array nums: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 100
"""
class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums. A pair of integers x and y is called a strong pair if it satisfies the condition:
|x - y| <= min(x, y)
You need to select two integers from nums such that they form a strong pair and their bitwise XOR is the maximum among all strong pairs in the array.
Return the maximum XOR value out of all possible strong pairs in the array nums.
Note that you can pick the same integer twice to form a pair.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 7
Explanation: There are 11 strong pairs in the array nums: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
Example 2:
Input: nums = [10,100]
Output: 0
Explanation: There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
Example 3:
Input: nums = [5,6,25,30]
Output: 7
Explanation: There are 6 strong pairs in the array nums: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [10,100] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [5,6,25,30] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [100] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,1,2,3,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,1,3,8,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,1,4,4,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [1,1,6,6,9] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,1,10,3,9] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,2,1,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,2,2,1,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,2,3,8,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,2,5,5,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,2,8,3,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,2,9,2,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,3,3,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,3,8,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 13
test_input = { "nums": [1,3,9,6,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,4,1,2,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,4,3,9,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [1,4,4,3,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [1,4,5,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,4,8,6,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [1,5,1,9,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 12
test_input = { "nums": [1,5,4,1,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,5,5,2,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,5,9,1,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 12
test_input = { "nums": [1,5,9,10,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,6,2,7,9] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,6,3,3,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 12
test_input = { "nums": [1,6,4,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [1,6,4,6,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 2
test_input = { "nums": [1,6,5,5,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [1,6,6,1,9] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,6,8,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [1,6,8,10,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [1,6,9,8,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,6,10,1,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 12
test_input = { "nums": [1,7,1,4,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [1,7,2,1,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 13
test_input = { "nums": [1,7,2,10,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 13
test_input = { "nums": [1,7,6,1,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,7,8,6,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,8,1,1,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,8,4,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 12
test_input = { "nums": [1,8,4,5,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [1,8,5,10,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,8,8,8,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,8,10,2,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [1,9,4,4,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,9,6,5,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,9,6,8,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,9,8,2,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,9,8,9,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,9,9,7,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,10,1,1,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,10,5,10,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,10,8,7,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [1,10,9,9,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,1,1,5,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,1,1,7,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,1,8,3,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,1,9,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,2,4,1,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,2,5,5,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,2,5,10,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,2,8,2,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 2
test_input = { "nums": [2,2,10,5,9] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,3,3,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,3,8,8,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 2
test_input = { "nums": [2,4,5,3,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [2,4,6,8,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [2,4,6,9,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,4,8,7,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,4,10,4,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,4,10,6,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 12
test_input = { "nums": [2,4,10,7,8] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,4,10,9,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [2,5,2,5,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [2,5,3,10,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,5,7,2,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 2
test_input = { "nums": [2,5,9,8,9] }
assert my_solution.maximumStrongPairXor(**test_input) == 13
test_input = { "nums": [2,5,10,8,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,6,1,6,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,6,1,8,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,6,7,4,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,6,8,6,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [2,6,10,5,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,7,1,8,9] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,7,2,8,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,7,3,3,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [2,7,4,8,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,7,9,2,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,7,10,7,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 13
test_input = { "nums": [2,8,1,9,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 13
test_input = { "nums": [2,8,7,10,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 15
test_input = { "nums": [2,9,1,7,7] }
assert my_solution.maximumStrongPairXor(**test_input) == 14
test_input = { "nums": [2,9,2,8,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [2,9,5,10,10] }
assert my_solution.maximumStrongPairXor(**test_input) == 15 | 1,699,756,200 |
weekly-contest-371-high-access-employees | https://leetcode.com/problems/high-access-employees | high-access-employees | {
"questionId": "3202",
"questionFrontendId": "2933",
"title": "High-Access Employees",
"titleSlug": "high-access-employees",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 110,
"dislikes": 14,
"categoryTitle": "Algorithms"
} | """
You are given a 2D 0-indexed array of strings, access_times, with size n. For each i where 0 <= i <= n - 1, access_times[i][0] represents the name of an employee, and access_times[i][1] represents the access time of that employee. All entries in access_times are within the same day.
The access time is represented as four digits using a 24-hour time format, for example, "0800" or "2250".
An employee is said to be high-access if he has accessed the system three or more times within a one-hour period.
Times with exactly one hour of difference are not considered part of the same one-hour period. For example, "0815" and "0915" are not part of the same one-hour period.
Access times at the start and end of the day are not counted within the same one-hour period. For example, "0005" and "2350" are not part of the same one-hour period.
Return a list that contains the names of high-access employees with any order you want.
Example 1:
Input: access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
Output: ["a"]
Explanation: "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
But "b" does not have more than two access times at all.
So the answer is ["a"].
Example 2:
Input: access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
Output: ["c","d"]
Explanation: "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
"d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].
Example 3:
Input: access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
Output: ["ab","cd"]
Explanation: "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
"cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
So the answer is ["ab","cd"].
Constraints:
1 <= access_times.length <= 100
access_times[i].length == 2
1 <= access_times[i][0].length <= 10
access_times[i][0] consists only of English small letters.
access_times[i][1].length == 4
access_times[i][1] is in 24-hour time format.
access_times[i][1] consists only of '0' to '9'.
"""
class Solution:
def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
| You are given a 2D 0-indexed array of strings, access_times, with size n. For each i where 0 <= i <= n - 1, access_times[i][0] represents the name of an employee, and access_times[i][1] represents the access time of that employee. All entries in access_times are within the same day.
The access time is represented as four digits using a 24-hour time format, for example, "0800" or "2250".
An employee is said to be high-access if he has accessed the system three or more times within a one-hour period.
Times with exactly one hour of difference are not considered part of the same one-hour period. For example, "0815" and "0915" are not part of the same one-hour period.
Access times at the start and end of the day are not counted within the same one-hour period. For example, "0005" and "2350" are not part of the same one-hour period.
Return a list that contains the names of high-access employees with any order you want.
Example 1:
Input: access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
Output: ["a"]
Explanation: "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
But "b" does not have more than two access times at all.
So the answer is ["a"].
Example 2:
Input: access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
Output: ["c","d"]
Explanation: "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
"d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].
Example 3:
Input: access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
Output: ["ab","cd"]
Explanation: "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
"cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
So the answer is ["ab","cd"].
Constraints:
1 <= access_times.length <= 100
access_times[i].length == 2
1 <= access_times[i][0].length <= 10
access_times[i][0] consists only of English small letters.
access_times[i][1].length == 4
access_times[i][1] is in 24-hour time format.
access_times[i][1] consists only of '0' to '9'.
Please complete the code below to solve above prblem:
```python
class Solution:
def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
``` |
my_solution = Solution()
test_input = { "access_times": [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["a"]
test_input = { "access_times": [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["c","d"]
test_input = { "access_times": [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["ab","cd"]
test_input = { "access_times": [["baipstt","1456"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["bouo","1126"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["cavfbqg","2304"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["cenjcq","1007"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["cqotrwqcaq","0131"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["downbuk","1951"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["dqsoiyz","2204"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["duzeyrov","0243"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["erfg","1223"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["fwhefd","2026"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["gbefbne","0911"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["gp","1540"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["ht","1319"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["inahnsjdqz","1750"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["jwxvijxo","0851"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["kibwwvjuez","0716"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["lvry","0706"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["mbsyxxfzjf","0114"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["mlehvzqb","1620"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["mmgat","0516"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["mxatapbs","2240"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["mzxbgtfc","1531"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["nnhh","1445"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["o","1414"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["qaxqifxxww","1557"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["rjy","0200"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["sgpgh","0539"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["sxx","0325"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["tkvgcf","1645"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["ttk","0304"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["un","0833"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["vlifcdn","0731"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["w","2224"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["wkmehwsg","2023"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["y","1005"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["ynnale","1331"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["yt","0900"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["zbgzk","0527"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["a","0039"],["a","0042"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["ajhzcltqse","0605"],["ajhzcltqse","0558"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["cbaqsymoi","0001"],["cbaqsymoi","0004"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["df","1958"],["df","2002"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["dhmnhvou","0529"],["dhmnhvou","0531"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["epghzrog","0333"],["epghzrog","0333"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["gda","1529"],["gda","1534"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["gjhtgm","2207"],["gjhtgm","2156"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["gsd","2030"],["gsd","2046"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["gsstuktwm","1403"],["gsstuktwm","1357"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["h","2159"],["h","2203"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["hxrdffk","1736"],["hxrdffk","1724"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["iaxsnenx","2037"],["iaxsnenx","2050"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["ikwjvflxq","0055"],["ikwjvflxq","0056"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["jkgjmku","0743"],["jkgjmku","0754"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["jkw","0241"],["jkw","0235"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["jykugiprxf","1633"],["jykugiprxf","1641"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["kdxw","1338"],["kdxw","1336"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["kenltmrg","0932"],["kenltmrg","0941"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["kptjrr","1356"],["kptjrr","1349"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["mcd","1333"],["mcd","1325"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["mhkizga","1552"],["mhkizga","1551"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["monxm","1748"],["monxm","1742"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["msjydtinfy","1301"],["msjydtinfy","1245"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["myhdmu","1407"],["myhdmu","1419"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["nyoezc","1050"],["nyoezc","1041"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["oksvrskxch","0053"],["oksvrskxch","0111"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["pxc","1915"],["pxc","1910"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["qedxyj","0609"],["qedxyj","0614"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["qmslkyxnph","0946"],["qmslkyxnph","0958"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["r","0206"],["r","0202"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["r","2041"],["r","2052"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["rf","2205"],["rf","2203"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["rswegeuhqd","0235"],["rswegeuhqd","0238"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["skfgl","0718"],["skfgl","0712"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["smnnl","2329"],["smnnl","2340"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["tpbbxpx","0409"],["tpbbxpx","0408"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["uiqxqp","0515"],["uiqxqp","0516"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["uyuz","1530"],["uyuz","1543"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["vfeunkee","1500"],["vfeunkee","1508"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["wbyd","1848"],["wbyd","1839"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["x","0522"],["x","0506"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["xhrhdy","1455"],["xhrhdy","1454"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["xmsypay","1605"],["xmsypay","1612"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["xy","0015"],["xy","0021"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["ydtnnpzw","0516"],["ydtnnpzw","0520"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["zh","2348"],["zh","2334"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["zinywjn","0017"],["zinywjn","0019"]] }
assert my_solution.findHighAccessEmployees(**test_input) == []
test_input = { "access_times": [["aczdfmsd","0317"],["aczdfmsd","0314"],["aczdfmsd","0320"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["aczdfmsd"]
test_input = { "access_times": [["bsluadumi","1518"],["bsluadumi","1516"],["bsluadumi","1510"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["bsluadumi"]
test_input = { "access_times": [["ckrdpxq","1122"],["ckrdpxq","1125"],["ckrdpxq","1121"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["ckrdpxq"]
test_input = { "access_times": [["fe","1320"],["fe","1326"],["fe","1331"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["fe"]
test_input = { "access_times": [["ff","1508"],["ff","1508"],["ff","1516"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["ff"]
test_input = { "access_times": [["fnlmbcedu","0052"],["fnlmbcedu","0103"],["fnlmbcedu","0055"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["fnlmbcedu"]
test_input = { "access_times": [["hffgwjjve","0159"],["hffgwjjve","0152"],["hffgwjjve","0159"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["hffgwjjve"]
test_input = { "access_times": [["ivlvfgwsx","0122"],["ivlvfgwsx","0135"],["ivlvfgwsx","0139"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["ivlvfgwsx"]
test_input = { "access_times": [["jlfnksqlt","0304"],["jlfnksqlt","0252"],["jlfnksqlt","0304"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["jlfnksqlt"]
test_input = { "access_times": [["jy","0647"],["jy","0652"],["jy","0704"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["jy"]
test_input = { "access_times": [["kchzzdso","2329"],["kchzzdso","2326"],["kchzzdso","2329"]] }
assert my_solution.findHighAccessEmployees(**test_input) == ["kchzzdso"] | 1,699,756,200 |
weekly-contest-371-minimum-operations-to-maximize-last-elements-in-arrays | https://leetcode.com/problems/minimum-operations-to-maximize-last-elements-in-arrays | minimum-operations-to-maximize-last-elements-in-arrays | {
"questionId": "3190",
"questionFrontendId": "2934",
"title": "Minimum Operations to Maximize Last Elements in Arrays",
"titleSlug": "minimum-operations-to-maximize-last-elements-in-arrays",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 152,
"dislikes": 12,
"categoryTitle": "Algorithms"
} | """
You are given two 0-indexed integer arrays, nums1 and nums2, both having length n.
You are allowed to perform a series of operations (possibly none).
In an operation, you select an index i in the range [0, n - 1] and swap the values of nums1[i] and nums2[i].
Your task is to find the minimum number of operations required to satisfy the following conditions:
nums1[n - 1] is equal to the maximum value among all elements of nums1, i.e., nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1]).
nums2[n - 1] is equal to the maximum value among all elements of nums2, i.e., nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1]).
Return an integer denoting the minimum number of operations needed to meet both conditions, or -1 if it is impossible to satisfy both conditions.
Example 1:
Input: nums1 = [1,2,7], nums2 = [4,5,3]
Output: 1
Explanation: In this example, an operation can be performed using index i = 2.
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
Both conditions are now satisfied.
It can be shown that the minimum number of operations needed to be performed is 1.
So, the answer is 1.
Example 2:
Input: nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]
Output: 2
Explanation: In this example, the following operations can be performed:
First operation using index i = 4.
When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9].
Another operation using index i = 3.
When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9].
Both conditions are now satisfied.
It can be shown that the minimum number of operations needed to be performed is 2.
So, the answer is 2.
Example 3:
Input: nums1 = [1,5,4], nums2 = [2,5,3]
Output: -1
Explanation: In this example, it is not possible to satisfy both conditions.
So, the answer is -1.
Constraints:
1 <= n == nums1.length == nums2.length <= 1000
1 <= nums1[i] <= 109
1 <= nums2[i] <= 109
"""
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
| You are given two 0-indexed integer arrays, nums1 and nums2, both having length n.
You are allowed to perform a series of operations (possibly none).
In an operation, you select an index i in the range [0, n - 1] and swap the values of nums1[i] and nums2[i].
Your task is to find the minimum number of operations required to satisfy the following conditions:
nums1[n - 1] is equal to the maximum value among all elements of nums1, i.e., nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1]).
nums2[n - 1] is equal to the maximum value among all elements of nums2, i.e., nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1]).
Return an integer denoting the minimum number of operations needed to meet both conditions, or -1 if it is impossible to satisfy both conditions.
Example 1:
Input: nums1 = [1,2,7], nums2 = [4,5,3]
Output: 1
Explanation: In this example, an operation can be performed using index i = 2.
When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7].
Both conditions are now satisfied.
It can be shown that the minimum number of operations needed to be performed is 1.
So, the answer is 1.
Example 2:
Input: nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]
Output: 2
Explanation: In this example, the following operations can be performed:
First operation using index i = 4.
When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9].
Another operation using index i = 3.
When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9].
Both conditions are now satisfied.
It can be shown that the minimum number of operations needed to be performed is 2.
So, the answer is 2.
Example 3:
Input: nums1 = [1,5,4], nums2 = [2,5,3]
Output: -1
Explanation: In this example, it is not possible to satisfy both conditions.
So, the answer is -1.
Constraints:
1 <= n == nums1.length == nums2.length <= 1000
1 <= nums1[i] <= 109
1 <= nums2[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums1": [1,2,7], "nums2": [4,5,3] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [2,3,4,5,9], "nums2": [8,8,4,4,4] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [1,5,4], "nums2": [2,5,3] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [1], "nums2": [1] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,2], "nums2": [2,1] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [1,1,10], "nums2": [1,5,1] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [1,4,16], "nums2": [16,16,16] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,5,15], "nums2": [1,1,1] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [2,5,7], "nums2": [2,2,2] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [8,9,10], "nums2": [10,9,9] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [9,14,14], "nums2": [14,11,14] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [16,16,16], "nums2": [6,7,16] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [19,7,19], "nums2": [5,19,19] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,1,8,9], "nums2": [1,7,1,1] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [1,5,9,9], "nums2": [9,9,8,9] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,7,7,7], "nums2": [7,3,3,7] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [10,18,12,12], "nums2": [19,6,5,12] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [12,9,11,12], "nums2": [3,9,9,9] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [15,54,22,54], "nums2": [54,19,54,54] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [20,20,20,20], "nums2": [5,8,19,20] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,3,4,6,7], "nums2": [1,1,1,1,1] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,11,17,1,18], "nums2": [1,1,1,18,1] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [3,3,3,14,14], "nums2": [3,3,4,3,3] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [4,4,8,4,17], "nums2": [4,8,4,14,4] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [4,12,11,11,12], "nums2": [12,6,12,12,12] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [6,21,87,63,87], "nums2": [87,87,23,63,63] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [10,6,6,6,10], "nums2": [6,6,10,10,6] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [16,12,15,15,16], "nums2": [8,16,16,15,15] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [17,3,6,6,17], "nums2": [3,17,6,14,6] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [19,13,18,18,19], "nums2": [5,13,13,13,13] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [20,20,53,20,68], "nums2": [20,28,20,61,20] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [6,6,2,4,4,6], "nums2": [1,1,6,4,4,4] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [8,6,6,6,7,8], "nums2": [5,8,8,8,7,7] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [15,1,15,6,12,15], "nums2": [1,15,2,15,15,15] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [17,13,19,9,6,14], "nums2": [17,14,15,1,19,19] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [20,8,10,12,17,20], "nums2": [7,20,20,20,20,20] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [20,18,15,11,17,20], "nums2": [7,15,13,7,20,12] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [20,20,20,20,19,20], "nums2": [3,6,7,15,20,20] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [28,43,79,32,40,3], "nums2": [95,25,74,16,82,56] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [29,81,58,99,41,36], "nums2": [15,34,47,57,31,95] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [1,1,4,1,7,1,15], "nums2": [1,1,1,4,1,9,1] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [1,4,4,4,7,4,9], "nums2": [9,9,4,6,4,9,4] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [2,88,17,88,68,68,88], "nums2": [88,16,88,68,68,68,68] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [3,3,5,3,3,3,18], "nums2": [3,4,3,11,13,14,3] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [3,41,98,71,71,71,98], "nums2": [98,98,49,71,85,94,71] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [5,5,27,53,5,59,65], "nums2": [5,16,5,5,58,5,5] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [5,6,11,11,13,11,14], "nums2": [14,14,11,11,11,14,11] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [6,5,1,10,8,4,5], "nums2": [3,8,8,3,6,1,4] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [14,3,4,4,4,4,14], "nums2": [2,14,4,4,5,12,4] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [14,28,68,68,65,67,68], "nums2": [68,68,49,64,68,67,67] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [19,19,8,9,10,13,19], "nums2": [1,5,19,19,19,13,13] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [20,6,20,20,20,20,20], "nums2": [3,20,7,9,13,14,20] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [85,85,62,85,78,84,85], "nums2": [5,37,85,76,85,85,85] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,2,2,2,2,2,2,10], "nums2": [10,2,2,2,7,8,9,2] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [1,9,3,3,4,9,9,9], "nums2": [9,2,9,9,9,6,8,9] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [3,3,4,3,3,3,3,8], "nums2": [3,3,3,5,7,7,8,3] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [4,8,11,19,16,16,19,19], "nums2": [19,19,19,13,16,16,16,16] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [8,20,4,9,9,4,4,20], "nums2": [9,14,10,1,1,20,16,15] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [9,9,1,2,2,6,8,9], "nums2": [1,1,9,2,2,2,2,2] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [10,4,10,9,10,10,10,10], "nums2": [4,10,9,9,9,9,9,9] }
assert my_solution.minOperations(**test_input) == 1
test_input = { "nums1": [13,95,19,56,59,95,95,95], "nums2": [95,14,95,95,95,83,88,95] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [19,7,19,8,9,15,9,19], "nums2": [7,19,7,19,9,9,18,9] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [19,19,3,3,15,16,3,19], "nums2": [2,2,3,7,3,3,17,3] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [1,8,8,5,8,8,8,7,8], "nums2": [8,2,4,8,5,6,6,8,8] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,10,10,5,10,8,10,10,10], "nums2": [10,2,4,10,7,8,8,8,8] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [2,7,7,7,7,7,7,17,18], "nums2": [18,7,10,11,13,15,17,7,7] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [3,13,22,24,13,13,13,81,91], "nums2": [91,13,13,13,45,59,71,13,13] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [4,4,9,4,16,4,17,4,18], "nums2": [4,8,4,14,4,16,4,18,4] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [6,9,65,13,65,65,28,28,65], "nums2": [65,65,13,65,24,27,28,34,28] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [8,3,5,6,6,3,3,3,8], "nums2": [1,3,3,3,3,7,7,8,3] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [10,1,10,10,10,9,9,9,10], "nums2": [1,10,5,6,8,9,9,10,9] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [15,15,3,5,15,15,9,13,15], "nums2": [1,2,15,15,6,8,9,9,9] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [16,3,3,7,3,11,3,3,16], "nums2": [2,3,6,3,10,3,15,16,3] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [16,3,16,7,16,16,10,10,16], "nums2": [2,16,4,16,9,10,10,13,10] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [80,80,18,39,39,62,39,39,80], "nums2": [2,10,80,39,42,39,66,66,39] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [1,1,1,1,1,1,1,7,1,9], "nums2": [9,1,3,3,3,5,5,1,9,1] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [1,2,2,2,2,5,8,2,2,9], "nums2": [9,2,2,2,4,2,2,9,9,2] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [1,2,18,18,10,18,18,17,18,18], "nums2": [18,18,5,5,18,13,14,18,18,18] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [1,3,1,1,1,1,16,16,1,19], "nums2": [1,1,7,7,9,12,1,1,18,1] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [1,9,3,9,4,5,7,8,8,9], "nums2": [9,1,9,3,9,9,7,7,7,7] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [2,2,2,2,10,11,2,2,2,20], "nums2": [2,3,9,9,2,2,12,14,19,2] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [3,15,6,7,7,10,7,7,15,15], "nums2": [15,6,15,7,9,7,12,13,7,7] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [5,20,20,8,8,12,20,19,20,20], "nums2": [20,6,7,20,20,20,15,20,20,20] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [8,8,8,8,8,8,8,8,6,8], "nums2": [1,2,3,4,4,5,5,5,8,8] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [9,3,3,3,3,6,7,7,3,9], "nums2": [3,3,4,5,5,3,3,3,7,3] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [9,8,6,2,1,10,9,7,9,4], "nums2": [6,2,8,2,5,3,6,5,6,10] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [10,2,2,2,3,2,2,2,2,10], "nums2": [1,10,2,2,2,6,6,8,10,2] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [12,19,10,4,17,12,18,6,7,7], "nums2": [20,5,15,7,10,8,2,16,14,1] }
assert my_solution.minOperations(**test_input) == -1
test_input = { "nums1": [17,31,31,34,31,42,31,65,31,89], "nums2": [89,31,32,31,37,31,45,31,84,31] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [19,7,7,19,9,19,19,19,17,19], "nums2": [6,19,19,8,19,10,10,16,19,19] }
assert my_solution.minOperations(**test_input) == 0
test_input = { "nums1": [20,20,7,20,8,11,11,11,11,20], "nums2": [5,6,20,7,20,11,15,18,20,11] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [90,7,90,13,90,33,33,83,33,90], "nums2": [6,90,13,90,22,33,62,33,86,33] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [1,1,1,1,1,10,1,15,16,17,20], "nums2": [1,2,2,9,10,1,14,1,1,1,1] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums1": [1,1,1,1,3,1,1,6,1,1,8], "nums2": [1,1,1,3,1,3,5,1,6,7,1] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [1,2,2,6,2,2,2,2,2,2,10], "nums2": [10,2,4,2,6,6,7,8,9,9,2] }
assert my_solution.minOperations(**test_input) == 2
test_input = { "nums1": [1,2,3,20,8,8,19,19,19,20,20], "nums2": [20,20,20,5,20,20,20,19,19,19,19] }
assert my_solution.minOperations(**test_input) == 3
test_input = { "nums1": [1,2,4,9,5,9,9,6,7,6,9], "nums2": [9,9,9,4,9,5,5,6,6,7,6] }
assert my_solution.minOperations(**test_input) == 5
test_input = { "nums1": [1,7,7,7,7,7,13,16,7,19,19], "nums2": [19,7,10,11,11,12,7,7,16,7,7] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [1,10,4,10,10,10,6,10,7,9,10], "nums2": [10,3,10,5,5,6,10,7,10,9,9] }
assert my_solution.minOperations(**test_input) == 4
test_input = { "nums1": [1,20,20,7,20,20,13,17,17,19,20], "nums2": [20,2,4,20,7,7,20,20,17,17,17] }
assert my_solution.minOperations(**test_input) == 4 | 1,699,756,200 |
weekly-contest-371-maximum-strong-pair-xor-ii | https://leetcode.com/problems/maximum-strong-pair-xor-ii | maximum-strong-pair-xor-ii | {
"questionId": "3197",
"questionFrontendId": "2935",
"title": "Maximum Strong Pair XOR II",
"titleSlug": "maximum-strong-pair-xor-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 140,
"dislikes": 1,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums. A pair of integers x and y is called a strong pair if it satisfies the condition:
|x - y| <= min(x, y)
You need to select two integers from nums such that they form a strong pair and their bitwise XOR is the maximum among all strong pairs in the array.
Return the maximum XOR value out of all possible strong pairs in the array nums.
Note that you can pick the same integer twice to form a pair.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 7
Explanation: There are 11 strong pairs in the array nums: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
Example 2:
Input: nums = [10,100]
Output: 0
Explanation: There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
Example 3:
Input: nums = [500,520,2500,3000]
Output: 1020
Explanation: There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
Constraints:
1 <= nums.length <= 5 * 104
1 <= nums[i] <= 220 - 1
"""
class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums. A pair of integers x and y is called a strong pair if it satisfies the condition:
|x - y| <= min(x, y)
You need to select two integers from nums such that they form a strong pair and their bitwise XOR is the maximum among all strong pairs in the array.
Return the maximum XOR value out of all possible strong pairs in the array nums.
Note that you can pick the same integer twice to form a pair.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 7
Explanation: There are 11 strong pairs in the array nums: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
Example 2:
Input: nums = [10,100]
Output: 0
Explanation: There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
Example 3:
Input: nums = [500,520,2500,3000]
Output: 1020
Explanation: There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
Constraints:
1 <= nums.length <= 5 * 104
1 <= nums[i] <= 220 - 1
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [10,100] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [500,520,2500,3000] }
assert my_solution.maximumStrongPairXor(**test_input) == 1020
test_input = { "nums": [1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [2,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [3,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [5,6] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,1,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,1,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,1,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,1,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,1,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,2,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,2,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,2,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,2,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,3,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,3,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,3,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,3,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [1,3,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,4,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,4,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,4,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [1,4,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,4,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [1,5,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [1,5,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [1,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [1,5,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [1,5,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [2,1,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,1,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,1,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,1,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,1,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,2,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [2,2,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [2,2,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,2,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [2,3,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,3,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [2,3,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [2,3,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [2,3,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,4,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,4,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,4,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [2,4,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,4,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,5,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [2,5,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [2,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,5,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [2,5,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [3,1,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [3,1,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [3,1,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [3,1,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,1,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [3,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 3
test_input = { "nums": [3,2,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [3,2,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [3,2,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,2,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [3,3,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [3,3,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [3,3,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [3,3,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,3,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [3,4,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,4,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,4,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,4,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,4,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,5,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [3,5,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [3,5,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [3,5,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [3,5,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [4,1,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [4,1,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [4,1,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,1,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [4,1,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 1
test_input = { "nums": [4,2,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [4,2,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [4,2,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,2,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [4,2,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 6
test_input = { "nums": [4,3,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,3,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,3,3] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,3,4] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,3,5] }
assert my_solution.maximumStrongPairXor(**test_input) == 7
test_input = { "nums": [4,4,1] }
assert my_solution.maximumStrongPairXor(**test_input) == 0
test_input = { "nums": [4,4,2] }
assert my_solution.maximumStrongPairXor(**test_input) == 6 | 1,699,756,200 |
biweekly-contest-117-distribute-candies-among-children-i | https://leetcode.com/problems/distribute-candies-among-children-i | distribute-candies-among-children-i | {
"questionId": "3199",
"questionFrontendId": "2928",
"title": "Distribute Candies Among Children I",
"titleSlug": "distribute-candies-among-children-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 50,
"dislikes": 25,
"categoryTitle": "Algorithms"
} | """
You are given two positive integers n and limit.
Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
Example 1:
Input: n = 5, limit = 2
Output: 3
Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Example 2:
Input: n = 3, limit = 3
Output: 10
Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
Constraints:
* 1 <= n <= 50
* 1 <= limit <= 50
"""
class Solution:
def distributeCandies(self, n: int, limit: int) -> int:
| You are given two positive integers n and limit.
Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
Example 1:
Input: n = 5, limit = 2
Output: 3
Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Example 2:
Input: n = 3, limit = 3
Output: 10
Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
Constraints:
* 1 <= n <= 50
* 1 <= limit <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def distributeCandies(self, n: int, limit: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 5, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 3, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 1, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 21 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 22 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 23 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 24 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 25 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 2, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 2, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 21 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 22 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 23 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 24 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 25 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 3, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 1
test_input = { "n": 3, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 7
test_input = { "n": 3, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 21 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 22 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 23 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 24 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 25 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 4, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 0
test_input = { "n": 4, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 4, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 12
test_input = { "n": 4, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 21 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 22 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 23 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 24 }
assert my_solution.distributeCandies(**test_input) == 15 | 1,699,713,000 |
biweekly-contest-117-distribute-candies-among-children-ii | https://leetcode.com/problems/distribute-candies-among-children-ii | distribute-candies-among-children-ii | {
"questionId": "3201",
"questionFrontendId": "2929",
"title": "Distribute Candies Among Children II",
"titleSlug": "distribute-candies-among-children-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 62,
"dislikes": 95,
"categoryTitle": "Algorithms"
} | """
You are given two positive integers n and limit.
Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
Example 1:
Input: n = 5, limit = 2
Output: 3
Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Example 2:
Input: n = 3, limit = 3
Output: 10
Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
Constraints:
* 1 <= n <= 106
* 1 <= limit <= 106
"""
class Solution:
def distributeCandies(self, n: int, limit: int) -> int:
| You are given two positive integers n and limit.
Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
Example 1:
Input: n = 5, limit = 2
Output: 3
Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Example 2:
Input: n = 3, limit = 3
Output: 10
Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
Constraints:
* 1 <= n <= 106
* 1 <= limit <= 106
Please complete the code below to solve above prblem:
```python
class Solution:
def distributeCandies(self, n: int, limit: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 5, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 3, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 1, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 1, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 2, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 3
test_input = { "n": 2, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 2, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 3, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 1
test_input = { "n": 3, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 7
test_input = { "n": 3, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 3, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 10
test_input = { "n": 4, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 0
test_input = { "n": 4, "limit": 2 }
assert my_solution.distributeCandies(**test_input) == 6
test_input = { "n": 4, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 12
test_input = { "n": 4, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 4, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 15
test_input = { "n": 5, "limit": 1 }
assert my_solution.distributeCandies(**test_input) == 0
test_input = { "n": 5, "limit": 3 }
assert my_solution.distributeCandies(**test_input) == 12
test_input = { "n": 5, "limit": 4 }
assert my_solution.distributeCandies(**test_input) == 18
test_input = { "n": 5, "limit": 5 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 6 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 7 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 8 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 9 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 10 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 11 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 12 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 13 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 14 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 15 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 16 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 17 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 18 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 19 }
assert my_solution.distributeCandies(**test_input) == 21
test_input = { "n": 5, "limit": 20 }
assert my_solution.distributeCandies(**test_input) == 21 | 1,699,713,000 |
biweekly-contest-117-number-of-strings-which-can-be-rearranged-to-contain-substring | https://leetcode.com/problems/number-of-strings-which-can-be-rearranged-to-contain-substring | number-of-strings-which-can-be-rearranged-to-contain-substring | {
"questionId": "3200",
"questionFrontendId": "2930",
"title": "Number of Strings Which Can Be Rearranged to Contain Substring",
"titleSlug": "number-of-strings-which-can-be-rearranged-to-contain-substring",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 119,
"dislikes": 54,
"categoryTitle": "Algorithms"
} | """
You are given an integer n.
A string s is called good if it contains only lowercase English characters and it is possible to rearrange the characters of s such that the new string contains "leet" as a substring.
For example:
* The string "lteer" is good because we can rearrange it to form "leetr" .
* "letl" is not good because we cannot rearrange it to contain "leet" as a substring.
Return the total number of good strings of length n.
Since the answer may be large, return it modulo 109 + 7.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: n = 4
Output: 12
Explanation: The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
Example 2:
Input: n = 10
Output: 83943898
Explanation: The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (109 + 7) = 83943898.
Constraints:
* 1 <= n <= 105
"""
class Solution:
def stringCount(self, n: int) -> int:
| You are given an integer n.
A string s is called good if it contains only lowercase English characters and it is possible to rearrange the characters of s such that the new string contains "leet" as a substring.
For example:
* The string "lteer" is good because we can rearrange it to form "leetr" .
* "letl" is not good because we cannot rearrange it to contain "leet" as a substring.
Return the total number of good strings of length n.
Since the answer may be large, return it modulo 109 + 7.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: n = 4
Output: 12
Explanation: The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
Example 2:
Input: n = 10
Output: 83943898
Explanation: The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (109 + 7) = 83943898.
Constraints:
* 1 <= n <= 105
Please complete the code below to solve above prblem:
```python
class Solution:
def stringCount(self, n: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 4 }
assert my_solution.stringCount(**test_input) == 12
test_input = { "n": 10 }
assert my_solution.stringCount(**test_input) == 83943898
test_input = { "n": 1 }
assert my_solution.stringCount(**test_input) == 0
test_input = { "n": 2 }
assert my_solution.stringCount(**test_input) == 0
test_input = { "n": 3 }
assert my_solution.stringCount(**test_input) == 0
test_input = { "n": 5 }
assert my_solution.stringCount(**test_input) == 1460
test_input = { "n": 6 }
assert my_solution.stringCount(**test_input) == 106620
test_input = { "n": 7 }
assert my_solution.stringCount(**test_input) == 6058192
test_input = { "n": 8 }
assert my_solution.stringCount(**test_input) == 295164156
test_input = { "n": 9 }
assert my_solution.stringCount(**test_input) == 947613240
test_input = { "n": 11 }
assert my_solution.stringCount(**test_input) == 795234177
test_input = { "n": 12 }
assert my_solution.stringCount(**test_input) == 55396773
test_input = { "n": 13 }
assert my_solution.stringCount(**test_input) == 968092561
test_input = { "n": 14 }
assert my_solution.stringCount(**test_input) == 715599898
test_input = { "n": 15 }
assert my_solution.stringCount(**test_input) == 430509685
test_input = { "n": 16 }
assert my_solution.stringCount(**test_input) == 462719236
test_input = { "n": 17 }
assert my_solution.stringCount(**test_input) == 155543310
test_input = { "n": 18 }
assert my_solution.stringCount(**test_input) == 159683962
test_input = { "n": 19 }
assert my_solution.stringCount(**test_input) == 808507313
test_input = { "n": 20 }
assert my_solution.stringCount(**test_input) == 291395991
test_input = { "n": 21 }
assert my_solution.stringCount(**test_input) == 461951930
test_input = { "n": 22 }
assert my_solution.stringCount(**test_input) == 871561520
test_input = { "n": 23 }
assert my_solution.stringCount(**test_input) == 993268925
test_input = { "n": 24 }
assert my_solution.stringCount(**test_input) == 871982505
test_input = { "n": 25 }
assert my_solution.stringCount(**test_input) == 935610434
test_input = { "n": 26 }
assert my_solution.stringCount(**test_input) == 867518559
test_input = { "n": 27 }
assert my_solution.stringCount(**test_input) == 3067523
test_input = { "n": 28 }
assert my_solution.stringCount(**test_input) == 716801469
test_input = { "n": 29 }
assert my_solution.stringCount(**test_input) == 452206104
test_input = { "n": 30 }
assert my_solution.stringCount(**test_input) == 52805056
test_input = { "n": 31 }
assert my_solution.stringCount(**test_input) == 61992724
test_input = { "n": 32 }
assert my_solution.stringCount(**test_input) == 76928250
test_input = { "n": 33 }
assert my_solution.stringCount(**test_input) == 257967635
test_input = { "n": 34 }
assert my_solution.stringCount(**test_input) == 549347744
test_input = { "n": 35 }
assert my_solution.stringCount(**test_input) == 290653839
test_input = { "n": 36 }
assert my_solution.stringCount(**test_input) == 123906995
test_input = { "n": 37 }
assert my_solution.stringCount(**test_input) == 41253530
test_input = { "n": 38 }
assert my_solution.stringCount(**test_input) == 828924891
test_input = { "n": 39 }
assert my_solution.stringCount(**test_input) == 60893212
test_input = { "n": 40 }
assert my_solution.stringCount(**test_input) == 618599272
test_input = { "n": 41 }
assert my_solution.stringCount(**test_input) == 840600409
test_input = { "n": 42 }
assert my_solution.stringCount(**test_input) == 995406621
test_input = { "n": 43 }
assert my_solution.stringCount(**test_input) == 991833054
test_input = { "n": 44 }
assert my_solution.stringCount(**test_input) == 403185520
test_input = { "n": 45 }
assert my_solution.stringCount(**test_input) == 904195428
test_input = { "n": 46 }
assert my_solution.stringCount(**test_input) == 643609894
test_input = { "n": 47 }
assert my_solution.stringCount(**test_input) == 177947842
test_input = { "n": 48 }
assert my_solution.stringCount(**test_input) == 826753905
test_input = { "n": 49 }
assert my_solution.stringCount(**test_input) == 855443295
test_input = { "n": 50 }
assert my_solution.stringCount(**test_input) == 232825199
test_input = { "n": 51 }
assert my_solution.stringCount(**test_input) == 227116084
test_input = { "n": 52 }
assert my_solution.stringCount(**test_input) == 417264566
test_input = { "n": 53 }
assert my_solution.stringCount(**test_input) == 468973861
test_input = { "n": 54 }
assert my_solution.stringCount(**test_input) == 999145386
test_input = { "n": 55 }
assert my_solution.stringCount(**test_input) == 721276317
test_input = { "n": 56 }
assert my_solution.stringCount(**test_input) == 385673910
test_input = { "n": 57 }
assert my_solution.stringCount(**test_input) == 7891114
test_input = { "n": 58 }
assert my_solution.stringCount(**test_input) == 85081065
test_input = { "n": 59 }
assert my_solution.stringCount(**test_input) == 194677227
test_input = { "n": 60 }
assert my_solution.stringCount(**test_input) == 759126147
test_input = { "n": 61 }
assert my_solution.stringCount(**test_input) == 273111337
test_input = { "n": 62 }
assert my_solution.stringCount(**test_input) == 166598301
test_input = { "n": 63 }
assert my_solution.stringCount(**test_input) == 955460796
test_input = { "n": 64 }
assert my_solution.stringCount(**test_input) == 685704195
test_input = { "n": 65 }
assert my_solution.stringCount(**test_input) == 821093882
test_input = { "n": 66 }
assert my_solution.stringCount(**test_input) == 172674695
test_input = { "n": 67 }
assert my_solution.stringCount(**test_input) == 464621746
test_input = { "n": 68 }
assert my_solution.stringCount(**test_input) == 432202634
test_input = { "n": 69 }
assert my_solution.stringCount(**test_input) == 465445347
test_input = { "n": 70 }
assert my_solution.stringCount(**test_input) == 654273613
test_input = { "n": 71 }
assert my_solution.stringCount(**test_input) == 366864502
test_input = { "n": 72 }
assert my_solution.stringCount(**test_input) == 124689502
test_input = { "n": 73 }
assert my_solution.stringCount(**test_input) == 419691288
test_input = { "n": 74 }
assert my_solution.stringCount(**test_input) == 987033948
test_input = { "n": 75 }
assert my_solution.stringCount(**test_input) == 842828500
test_input = { "n": 76 }
assert my_solution.stringCount(**test_input) == 409614634
test_input = { "n": 77 }
assert my_solution.stringCount(**test_input) == 73844796
test_input = { "n": 78 }
assert my_solution.stringCount(**test_input) == 584672527
test_input = { "n": 79 }
assert my_solution.stringCount(**test_input) == 113476429
test_input = { "n": 80 }
assert my_solution.stringCount(**test_input) == 974106352
test_input = { "n": 81 }
assert my_solution.stringCount(**test_input) == 646239862
test_input = { "n": 82 }
assert my_solution.stringCount(**test_input) == 420253116
test_input = { "n": 83 }
assert my_solution.stringCount(**test_input) == 817573615
test_input = { "n": 84 }
assert my_solution.stringCount(**test_input) == 471199144
test_input = { "n": 85 }
assert my_solution.stringCount(**test_input) == 567239979
test_input = { "n": 86 }
assert my_solution.stringCount(**test_input) == 221534816
test_input = { "n": 87 }
assert my_solution.stringCount(**test_input) == 707218848
test_input = { "n": 88 }
assert my_solution.stringCount(**test_input) == 687360104
test_input = { "n": 89 }
assert my_solution.stringCount(**test_input) == 551986596
test_input = { "n": 90 }
assert my_solution.stringCount(**test_input) == 122933939
test_input = { "n": 91 }
assert my_solution.stringCount(**test_input) == 427294641
test_input = { "n": 92 }
assert my_solution.stringCount(**test_input) == 14022454
test_input = { "n": 93 }
assert my_solution.stringCount(**test_input) == 568729284
test_input = { "n": 94 }
assert my_solution.stringCount(**test_input) == 653568519
test_input = { "n": 95 }
assert my_solution.stringCount(**test_input) == 15509440
test_input = { "n": 96 }
assert my_solution.stringCount(**test_input) == 991824044
test_input = { "n": 97 }
assert my_solution.stringCount(**test_input) == 690441338
test_input = { "n": 98 }
assert my_solution.stringCount(**test_input) == 600462833
test_input = { "n": 99 }
assert my_solution.stringCount(**test_input) == 880019356
test_input = { "n": 100 }
assert my_solution.stringCount(**test_input) == 86731066 | 1,699,713,000 |
biweekly-contest-117-maximum-spending-after-buying-items | https://leetcode.com/problems/maximum-spending-after-buying-items | maximum-spending-after-buying-items | {
"questionId": "3107",
"questionFrontendId": "2931",
"title": "Maximum Spending After Buying Items",
"titleSlug": "maximum-spending-after-buying-items",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 66,
"dislikes": 20,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed m * n integer matrix values, representing the values of m * n different items in m different shops. Each shop has n items where the jth item in the ith shop has a value of values[i][j]. Additionally, the items in the ith shop are sorted in non-increasing order of value. That is, values[i][j] >= values[i][j + 1] for all 0 <= j < n - 1.
On each day, you would like to buy a single item from one of the shops. Specifically, On the dth day you can:
* Pick any shop i.
* Buy the rightmost available item j for the price of values[i][j] * d. That is, find the greatest index j such that item j was never bought before, and buy it for the price of values[i][j] * d.
Note that all items are pairwise different. For example, if you have bought item 0 from shop 1, you can still buy item 0 from any other shop.
Return the maximum amount of money that can be spent on buying all m * n products.
Example 1:
Input: values = [[8,5,2],[6,4,1],[9,7,3]]
Output: 285
Explanation: On the first day, we buy product 2 from shop 1 for a price of values[1][2] * 1 = 1.
On the second day, we buy product 2 from shop 0 for a price of values[0][2] * 2 = 4.
On the third day, we buy product 2 from shop 2 for a price of values[2][2] * 3 = 9.
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] * 4 = 16.
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] * 5 = 25.
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] * 6 = 36.
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] * 7 = 49.
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] * 8 = 64.
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] * 9 = 81.
Hence, our total spending is equal to 285.
It can be shown that 285 is the maximum amount of money that can be spent buying all m * n products.
Example 2:
Input: values = [[10,8,6,4,2],[9,7,5,3,2]]
Output: 386
Explanation: On the first day, we buy product 4 from shop 0 for a price of values[0][4] * 1 = 2.
On the second day, we buy product 4 from shop 1 for a price of values[1][4] * 2 = 4.
On the third day, we buy product 3 from shop 1 for a price of values[1][3] * 3 = 9.
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] * 4 = 16.
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] * 5 = 25.
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] * 6 = 36.
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] * 7 = 49.
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] * 8 = 64
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] * 9 = 81.
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] * 10 = 100.
Hence, our total spending is equal to 386.
It can be shown that 386 is the maximum amount of money that can be spent buying all m * n products.
Constraints:
* 1 <= m == values.length <= 10
* 1 <= n == values[i].length <= 104
* 1 <= values[i][j] <= 106
* values[i] are sorted in non-increasing order.
"""
class Solution:
def maxSpending(self, values: List[List[int]]) -> int:
| You are given a 0-indexed m * n integer matrix values, representing the values of m * n different items in m different shops. Each shop has n items where the jth item in the ith shop has a value of values[i][j]. Additionally, the items in the ith shop are sorted in non-increasing order of value. That is, values[i][j] >= values[i][j + 1] for all 0 <= j < n - 1.
On each day, you would like to buy a single item from one of the shops. Specifically, On the dth day you can:
* Pick any shop i.
* Buy the rightmost available item j for the price of values[i][j] * d. That is, find the greatest index j such that item j was never bought before, and buy it for the price of values[i][j] * d.
Note that all items are pairwise different. For example, if you have bought item 0 from shop 1, you can still buy item 0 from any other shop.
Return the maximum amount of money that can be spent on buying all m * n products.
Example 1:
Input: values = [[8,5,2],[6,4,1],[9,7,3]]
Output: 285
Explanation: On the first day, we buy product 2 from shop 1 for a price of values[1][2] * 1 = 1.
On the second day, we buy product 2 from shop 0 for a price of values[0][2] * 2 = 4.
On the third day, we buy product 2 from shop 2 for a price of values[2][2] * 3 = 9.
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] * 4 = 16.
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] * 5 = 25.
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] * 6 = 36.
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] * 7 = 49.
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] * 8 = 64.
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] * 9 = 81.
Hence, our total spending is equal to 285.
It can be shown that 285 is the maximum amount of money that can be spent buying all m * n products.
Example 2:
Input: values = [[10,8,6,4,2],[9,7,5,3,2]]
Output: 386
Explanation: On the first day, we buy product 4 from shop 0 for a price of values[0][4] * 1 = 2.
On the second day, we buy product 4 from shop 1 for a price of values[1][4] * 2 = 4.
On the third day, we buy product 3 from shop 1 for a price of values[1][3] * 3 = 9.
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] * 4 = 16.
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] * 5 = 25.
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] * 6 = 36.
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] * 7 = 49.
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] * 8 = 64
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] * 9 = 81.
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] * 10 = 100.
Hence, our total spending is equal to 386.
It can be shown that 386 is the maximum amount of money that can be spent buying all m * n products.
Constraints:
* 1 <= m == values.length <= 10
* 1 <= n == values[i].length <= 104
* 1 <= values[i][j] <= 106
* values[i] are sorted in non-increasing order.
Please complete the code below to solve above prblem:
```python
class Solution:
def maxSpending(self, values: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "values": [[8,5,2],[6,4,1],[9,7,3]] }
assert my_solution.maxSpending(**test_input) == 285
test_input = { "values": [[10,8,6,4,2],[9,7,5,3,2]] }
assert my_solution.maxSpending(**test_input) == 386
test_input = { "values": [[1000000]] }
assert my_solution.maxSpending(**test_input) == 1000000
test_input = { "values": [[1]] }
assert my_solution.maxSpending(**test_input) == 1
test_input = { "values": [[1],[2]] }
assert my_solution.maxSpending(**test_input) == 5
test_input = { "values": [[2],[1]] }
assert my_solution.maxSpending(**test_input) == 5
test_input = { "values": [[1],[1]] }
assert my_solution.maxSpending(**test_input) == 3
test_input = { "values": [[5,2]] }
assert my_solution.maxSpending(**test_input) == 12
test_input = { "values": [[5,5]] }
assert my_solution.maxSpending(**test_input) == 15
test_input = { "values": [[7,5]] }
assert my_solution.maxSpending(**test_input) == 19
test_input = { "values": [[3,2,1]] }
assert my_solution.maxSpending(**test_input) == 14
test_input = { "values": [[2,2,1]] }
assert my_solution.maxSpending(**test_input) == 11
test_input = { "values": [[3,3,2]] }
assert my_solution.maxSpending(**test_input) == 17
test_input = { "values": [[3],[2],[1]] }
assert my_solution.maxSpending(**test_input) == 14
test_input = { "values": [[2],[10],[1]] }
assert my_solution.maxSpending(**test_input) == 35
test_input = { "values": [[1000000,1000000,1000000]] }
assert my_solution.maxSpending(**test_input) == 6000000
test_input = { "values": [[1000000,1000000,1000000,1000000]] }
assert my_solution.maxSpending(**test_input) == 10000000
test_input = { "values": [[1000000],[1000000],[1000000],[1000000]] }
assert my_solution.maxSpending(**test_input) == 10000000
test_input = { "values": [[1000000,1000000],[1000000,1000000]] }
assert my_solution.maxSpending(**test_input) == 10000000
test_input = { "values": [[2,1],[4,3]] }
assert my_solution.maxSpending(**test_input) == 30
test_input = { "values": [[3,1],[4,2]] }
assert my_solution.maxSpending(**test_input) == 30
test_input = { "values": [[4,1],[3,2]] }
assert my_solution.maxSpending(**test_input) == 30
test_input = { "values": [[15,13,13,12,12,12,12,11,11,11,11,9,9,8,7,5,5,5,1]] }
assert my_solution.maxSpending(**test_input) == 2162
test_input = { "values": [[13,13,11,7,2,1],[13,10,10,6,3,3]] }
assert my_solution.maxSpending(**test_input) == 776
test_input = { "values": [[12,6],[13,5],[13,3],[6,6],[15,6],[5,4],[6,1]] }
assert my_solution.maxSpending(**test_input) == 971
test_input = { "values": [[15,15,14,14,14,10,10,9,9,8,8,8,7,7,7,4]] }
assert my_solution.maxSpending(**test_input) == 1585
test_input = { "values": [[11,10],[10,1],[14,6],[13,5],[7,3],[10,10],[10,5]] }
assert my_solution.maxSpending(**test_input) == 1061
test_input = { "values": [[15,13,12,7,6,4,1]] }
assert my_solution.maxSpending(**test_input) == 298
test_input = { "values": [[8,2],[4,1],[10,4]] }
assert my_solution.maxSpending(**test_input) == 133
test_input = { "values": [[10,4],[13,2],[7,5],[15,11]] }
assert my_solution.maxSpending(**test_input) == 380
test_input = { "values": [[13,10,10,9,8,5,5,2,1,1]] }
assert my_solution.maxSpending(**test_input) == 465
test_input = { "values": [[15,14,8,7,5,5,1]] }
assert my_solution.maxSpending(**test_input) == 283
test_input = { "values": [[15,15,14,14,14,14,13,11,10,10,10,9,7,7,6,6,4,4,2,1]] }
assert my_solution.maxSpending(**test_input) == 2449
test_input = { "values": [[15,14,11,10,10,6,2],[13,8,8,6,6,3,2]] }
assert my_solution.maxSpending(**test_input) == 1084
test_input = { "values": [[15,15,14,13,13,12,11,9,9,9,8,6,5,5,4,3,2,1,1]] }
assert my_solution.maxSpending(**test_input) == 2030
test_input = { "values": [[15,10,7],[14,2,2],[14,13,12],[13,13,10]] }
assert my_solution.maxSpending(**test_input) == 975
test_input = { "values": [[10,9,3,3],[11,8,7,2],[14,14,13,1],[14,14,11,4],[13,11,9,5]] }
assert my_solution.maxSpending(**test_input) == 2338
test_input = { "values": [[11,7,5,2],[15,5,5,3]] }
assert my_solution.maxSpending(**test_input) == 307
test_input = { "values": [[15,10,10,9,8,7,4,2,2]] }
assert my_solution.maxSpending(**test_input) == 425
test_input = { "values": [[15,13,12,12,10,5,4,3,1]] }
assert my_solution.maxSpending(**test_input) == 484
test_input = { "values": [[12,12,12,11,10,10,7,7,6,5,5,3,2,1]] }
assert my_solution.maxSpending(**test_input) == 979
test_input = { "values": [[15,13,13,13,12,12,11,11,10,9,9,9,8,6,6,4,4,3,2,1]] }
assert my_solution.maxSpending(**test_input) == 2253
test_input = { "values": [[14,11],[11,8],[10,4],[4,3],[9,6],[8,4],[7,7],[10,4],[14,4]] }
assert my_solution.maxSpending(**test_input) == 1621
test_input = { "values": [[14,13,13,11,11,8,8,7,6,4,3,2,1]] }
assert my_solution.maxSpending(**test_input) == 912
test_input = { "values": [[15,14,14,10,10,9,9,7,7,7,7,7,6,6,6,4,4,3,3]] }
assert my_solution.maxSpending(**test_input) == 1823
test_input = { "values": [[14,13,11,10,10,1,1],[15,12,8,6,6,5,5]] }
assert my_solution.maxSpending(**test_input) == 1120
test_input = { "values": [[15,14,13,12,11,10,10,10,9,7,6,6,4,3,3,3,2,2,1]] }
assert my_solution.maxSpending(**test_input) == 1860
test_input = { "values": [[12,6,5,4,2]] }
assert my_solution.maxSpending(**test_input) == 109
test_input = { "values": [[15,15,14,13,12,12,12,11,10,10,9,9,9,8,7,6,3,2]] }
assert my_solution.maxSpending(**test_input) == 2006
test_input = { "values": [[15,14,12,11,10,10,7,6,2,1]] }
assert my_solution.maxSpending(**test_input) == 610
test_input = { "values": [[15,13,12,11,8,6,5]] }
assert my_solution.maxSpending(**test_input) == 328
test_input = { "values": [[15,15,12,10,9,7,7,6,6,5,4,2,1]] }
assert my_solution.maxSpending(**test_input) == 896
test_input = { "values": [[15,15,12,12,10,10,9,7,6,4,4,4,1]] }
assert my_solution.maxSpending(**test_input) == 969
test_input = { "values": [[15,14,14,13,8,7,7,4,3,3,2,1]] }
assert my_solution.maxSpending(**test_input) == 792
test_input = { "values": [[15,15,14,14,14,13,10,8,6,6,5,5,3,3,2,2,1]] }
assert my_solution.maxSpending(**test_input) == 1634
test_input = { "values": [[14,12,11,10,10,8,7,7,5,5,3,3,3]] }
assert my_solution.maxSpending(**test_input) == 855
test_input = { "values": [[14,14,9,9,4,4,1],[12,12,9,8,7,6,3]] }
assert my_solution.maxSpending(**test_input) == 1060
test_input = { "values": [[15,15,15,13,12,12,10,9,9,9,9,7,6,4,4,4,1]] }
assert my_solution.maxSpending(**test_input) == 1727
test_input = { "values": [[9,4],[13,4],[13,9],[6,5],[8,2],[13,1]] }
assert my_solution.maxSpending(**test_input) == 732
test_input = { "values": [[14,9,9,9,8,7,5],[15,14,10,8,5,3,1]] }
assert my_solution.maxSpending(**test_input) == 1094
test_input = { "values": [[15,15,14,13,10,3,1]] }
assert my_solution.maxSpending(**test_input) == 354
test_input = { "values": [[13,12],[13,12],[11,8],[14,3]] }
assert my_solution.maxSpending(**test_input) == 441
test_input = { "values": [[13,12,11,10,7,5,5,1]] }
assert my_solution.maxSpending(**test_input) == 358
test_input = { "values": [[15,10,9,8,8,7,7,5,4,3,1]] }
assert my_solution.maxSpending(**test_input) == 582
test_input = { "values": [[10,10,6,4,1],[14,13,13,11,9],[14,11,7,4,3]] }
assert my_solution.maxSpending(**test_input) == 1302
test_input = { "values": [[15,14,12,12,11,11,10,9,9,8,8,6,6,4,2,2,1]] }
assert my_solution.maxSpending(**test_input) == 1596
test_input = { "values": [[12,5,4,3,3,2,1,1],[13,11,9,9,6,4,3,1]] }
assert my_solution.maxSpending(**test_input) == 1019
test_input = { "values": [[15,15,13,11,10,10,7,1,1]] }
assert my_solution.maxSpending(**test_input) == 526
test_input = { "values": [[14,12,10,4,3,1],[12,12,8,7,6,2],[15,13,8,5,4,1]] }
assert my_solution.maxSpending(**test_input) == 1719
test_input = { "values": [[13,8,8,8,5,4,3],[15,13,9,8,7,3,2]] }
assert my_solution.maxSpending(**test_input) == 1006
test_input = { "values": [[13,13,12,12,9]] }
assert my_solution.maxSpending(**test_input) == 186
test_input = { "values": [[10,3],[11,9],[10,5],[8,7],[6,1]] }
assert my_solution.maxSpending(**test_input) == 472
test_input = { "values": [[15,15,12,12,11,10,9,8,2,2,1,1,1]] }
assert my_solution.maxSpending(**test_input) == 941
test_input = { "values": [[14,14,13,13,11,11,10,10,10,9,8,8,7,7,6,6,5,4,3]] }
assert my_solution.maxSpending(**test_input) == 2023
test_input = { "values": [[11,10,9,7,6,5,3,1],[14,14,10,9,7,4,4,3]] }
assert my_solution.maxSpending(**test_input) == 1270
test_input = { "values": [[6,2],[10,2],[13,5],[8,7],[10,3],[13,2],[13,6]] }
assert my_solution.maxSpending(**test_input) == 972
test_input = { "values": [[15,15,15,14,13,12,6,5,5,4]] }
assert my_solution.maxSpending(**test_input) == 694
test_input = { "values": [[11,9],[8,1],[2,1],[15,13],[12,4],[14,5],[13,4]] }
assert my_solution.maxSpending(**test_input) == 1113
test_input = { "values": [[15,14,14,13,12,11,10,10,5,4,2]] }
assert my_solution.maxSpending(**test_input) == 800
test_input = { "values": [[13,12,12,6,5,5,4,2,2,2,1]] }
assert my_solution.maxSpending(**test_input) == 523
test_input = { "values": [[13,8],[15,1],[9,1],[13,1]] }
assert my_solution.maxSpending(**test_input) == 372
test_input = { "values": [[12,8,4,1],[14,8,8,3]] }
assert my_solution.maxSpending(**test_input) == 335
test_input = { "values": [[15,14,14,13,13,13,12,12,9,9,8,7,6,5,5,5,4,3,1]] }
assert my_solution.maxSpending(**test_input) == 2113
test_input = { "values": [[13,9,8,2,1],[9,4,3,2,1],[10,8,6,3,1]] }
assert my_solution.maxSpending(**test_input) == 877
test_input = { "values": [[8,6],[13,1],[12,8],[8,7]] }
assert my_solution.maxSpending(**test_input) == 342
test_input = { "values": [[9,7],[15,15],[9,8],[6,1],[11,4]] }
assert my_solution.maxSpending(**test_input) == 585
test_input = { "values": [[14,9,9,8,6,4,2]] }
assert my_solution.maxSpending(**test_input) == 257
test_input = { "values": [[15,13,11,11,10,9,5,5,4,4,4,4,3,2]] }
assert my_solution.maxSpending(**test_input) == 968
test_input = { "values": [[13,7,7,4],[8,8,6,6]] }
assert my_solution.maxSpending(**test_input) == 305
test_input = { "values": [[15,13,12,10,10,10,9,9,9,8,7,6,6,5,4,4,2,1,1]] }
assert my_solution.maxSpending(**test_input) == 1805
test_input = { "values": [[13,13,1],[11,5,4],[14,5,1],[15,5,2],[10,1,1],[15,12,1]] }
assert my_solution.maxSpending(**test_input) == 1714
test_input = { "values": [[15,15,14,14,11,10,10,8,8,8,3,1,1]] }
assert my_solution.maxSpending(**test_input) == 1050
test_input = { "values": [[3,3],[15,10],[14,9]] }
assert my_solution.maxSpending(**test_input) == 236
test_input = { "values": [[14,13,11,10,9,7,2]] }
assert my_solution.maxSpending(**test_input) == 314
test_input = { "values": [[11,7],[5,3],[11,6],[6,1],[15,13]] }
assert my_solution.maxSpending(**test_input) == 550
test_input = { "values": [[14,13,13,12,12,12,11,11,9,8,8,6,5,5,5,5,3,3,2]] }
assert my_solution.maxSpending(**test_input) == 1962
test_input = { "values": [[14,14,13,13,13,12,12,11,10,9,9,8,8,7,3,2,2]] }
assert my_solution.maxSpending(**test_input) == 1750
test_input = { "values": [[15,8],[12,2],[15,10],[9,3],[5,4],[14,2],[7,6]] }
assert my_solution.maxSpending(**test_input) == 1091
test_input = { "values": [[15,9,7,3,3],[7,6,4,3,2],[15,9,8,4,4],[15,10,9,7,4]] }
assert my_solution.maxSpending(**test_input) == 1952
test_input = { "values": [[12,12,10,8,8,8,8,6,2,2,1]] }
assert my_solution.maxSpending(**test_input) == 585 | 1,699,713,000 |
weekly-contest-370-find-champion-i | https://leetcode.com/problems/find-champion-i | find-champion-i | {
"questionId": "3188",
"questionFrontendId": "2923",
"title": "Find Champion I",
"titleSlug": "find-champion-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 84,
"dislikes": 20,
"categoryTitle": "Algorithms"
} | """
There are n teams numbered from 0 to n - 1 in a tournament.
Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n - 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i.
Team a will be the champion of the tournament if there is no team b that is stronger than team a.
Return the team that will be the champion of the tournament.
Example 1:
Input: grid = [[0,1],[0,0]]
Output: 0
Explanation: There are two teams in this tournament.
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.
Example 2:
Input: grid = [[0,0,1],[1,0,1],[0,0,0]]
Output: 1
Explanation: There are three teams in this tournament.
grid[1][0] == 1 means that team 1 is stronger than team 0.
grid[1][2] == 1 means that team 1 is stronger than team 2.
So team 1 will be the champion.
Constraints:
* n == grid.length
* n == grid[i].length
* 2 <= n <= 100
* grid[i][j] is either 0 or 1.
* For all i grid[i][i] is 0.
* For all i, j that i != j, grid[i][j] != grid[j][i].
* The input is generated such that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.
"""
class Solution:
def findChampion(self, grid: List[List[int]]) -> int:
| There are n teams numbered from 0 to n - 1 in a tournament.
Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n - 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i.
Team a will be the champion of the tournament if there is no team b that is stronger than team a.
Return the team that will be the champion of the tournament.
Example 1:
Input: grid = [[0,1],[0,0]]
Output: 0
Explanation: There are two teams in this tournament.
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.
Example 2:
Input: grid = [[0,0,1],[1,0,1],[0,0,0]]
Output: 1
Explanation: There are three teams in this tournament.
grid[1][0] == 1 means that team 1 is stronger than team 0.
grid[1][2] == 1 means that team 1 is stronger than team 2.
So team 1 will be the champion.
Constraints:
* n == grid.length
* n == grid[i].length
* 2 <= n <= 100
* grid[i][j] is either 0 or 1.
* For all i grid[i][i] is 0.
* For all i, j that i != j, grid[i][j] != grid[j][i].
* The input is generated such that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.
Please complete the code below to solve above prblem:
```python
class Solution:
def findChampion(self, grid: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "grid": [[0,1],[0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,0,1],[1,0,1],[0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0],[1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0],[1,0,0],[1,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0],[1,0,1],[1,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,1,0],[0,0,0],[1,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,1],[0,0,0],[0,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1],[0,0,1],[0,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,0,0,0],[1,0,0,0],[1,1,0,0],[1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,0,0],[1,0,0,0],[1,1,0,1],[1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0],[1,0,0,1],[1,1,0,1],[1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0],[1,0,1,0],[1,0,0,0],[1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,0,0],[1,0,1,1],[1,0,0,0],[1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,0],[1,0,1,1],[1,0,0,1],[1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,1],[1,0,0,1],[1,1,0,1],[0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,1],[1,0,1,1],[1,0,0,1],[0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,1,0],[1,0,1,0],[0,0,0,0],[1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,1,0],[1,0,1,1],[0,0,0,0],[1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,1,1],[1,0,1,1],[0,0,0,0],[0,0,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,1,1],[1,0,1,1],[0,0,0,1],[0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,1,0,0],[0,0,0,0],[1,1,0,0],[1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,0,0],[0,0,0,0],[1,1,0,1],[1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,0,1],[0,0,0,0],[1,1,0,1],[0,1,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,0,1],[0,0,0,1],[1,1,0,1],[0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,1,0],[0,0,0,0],[0,1,0,0],[1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,0],[0,0,1,0],[0,0,0,0],[1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,1],[0,0,0,0],[0,1,0,0],[0,1,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1],[0,0,0,0],[0,1,0,1],[0,1,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1],[0,0,0,1],[0,1,0,1],[0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1],[0,0,1,0],[0,0,0,0],[0,1,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1],[0,0,1,1],[0,0,0,0],[0,0,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1],[0,0,1,1],[0,0,0,1],[0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,0,0],[1,1,1,0,1],[1,1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,0],[1,1,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1],[1,1,0,0,0],[1,1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1],[1,1,0,0,1],[1,1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,1],[1,1,0,0,1],[1,1,1,0,1],[1,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,1],[1,1,0,1,1],[1,1,0,0,1],[1,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0,0],[1,0,0,1,0],[1,1,0,1,0],[1,0,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,0,0,0],[1,0,0,1,0],[1,1,0,1,1],[1,0,0,0,0],[1,1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0,0],[1,0,0,1,1],[1,1,0,1,1],[1,0,0,0,1],[1,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0,0],[1,0,1,0,1],[1,0,0,0,0],[1,1,1,0,1],[1,0,1,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,0],[1,0,0,0,0],[1,0,1,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,0],[1,0,0,1,0],[1,0,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,1],[1,0,0,0,0],[1,0,1,0,1],[1,0,1,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,1],[1,0,0,1,1],[1,0,0,0,1],[1,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,0,1],[1,0,0,0,1],[1,1,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,0,0,1],[1,0,0,0,1],[1,1,0,1,1],[1,1,0,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0,1],[1,0,0,1,1],[1,1,0,1,1],[1,0,0,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,0,1],[1,0,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,1,0],[1,0,0,1,0],[1,1,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,0,1,0],[1,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0],[1,1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,1,0],[1,0,0,1,1],[1,1,0,1,1],[0,0,0,0,0],[1,0,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,1,0],[1,0,1,1,0],[1,0,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,0,1,0],[1,0,1,1,1],[1,0,0,1,0],[0,0,0,0,0],[1,0,1,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,1,0],[1,0,1,1,1],[1,0,0,1,1],[0,0,0,0,0],[1,0,0,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,1,1],[1,0,0,1,1],[1,1,0,1,1],[0,0,0,0,0],[0,0,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,1,1],[1,0,0,1,1],[1,1,0,1,1],[0,0,0,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,0,0,1,1],[1,0,1,1,1],[1,0,0,1,1],[0,0,0,0,0],[0,0,0,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,0,1,1],[1,0,1,1,1],[1,0,0,1,1],[0,0,0,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,1,0,0],[1,0,1,0,0],[0,0,0,0,0],[1,1,1,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,1,0,0],[1,0,1,1,0],[0,0,0,0,0],[1,0,1,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,1,0,0],[1,0,1,1,1],[0,0,0,0,0],[1,0,1,0,1],[1,0,1,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,1,0,1],[1,0,1,0,1],[0,0,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,0,1,1,0],[1,0,1,1,0],[0,0,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,0,1,1,0],[1,0,1,1,1],[0,0,0,0,0],[0,0,1,0,0],[1,0,1,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,1,1,1],[1,0,1,1,1],[0,0,0,1,0],[0,0,0,0,0],[0,0,1,1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,0,1,1,1],[1,0,1,1,1],[0,0,0,1,1],[0,0,0,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "grid": [[0,1,0,0,0],[0,0,0,0,0],[1,1,0,0,0],[1,1,1,0,1],[1,1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,0,0,0],[0,0,0,0,0],[1,1,0,1,1],[1,1,0,0,0],[1,1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,0,0,1],[0,0,0,0,0],[1,1,0,0,1],[1,1,1,0,1],[0,1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,0,0,1],[0,0,0,0,0],[1,1,0,1,1],[1,1,0,0,1],[0,1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,0,1,0],[0,0,0,0,0],[1,1,0,1,0],[0,1,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,1,0,1,0],[0,0,0,0,0],[1,1,0,1,1],[0,1,0,0,0],[1,1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,0,1,0],[0,0,0,1,0],[1,1,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,1,0,1,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0],[1,1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,0,1,1],[0,0,0,0,0],[1,1,0,1,1],[0,1,0,0,1],[0,1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,0,1,1],[0,0,0,1,1],[1,1,0,1,1],[0,0,0,0,0],[0,0,0,1,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "grid": [[0,1,1,0,0],[0,0,0,0,0],[0,1,0,0,0],[1,1,1,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,1,1,0,0],[0,0,1,0,0],[0,0,0,0,0],[1,1,1,0,1],[1,1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,0,1],[0,0,0,0,0],[0,1,0,0,1],[1,1,1,0,1],[0,1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,0,1],[0,0,0,0,1],[0,1,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,0,1],[0,0,1,0,0],[0,0,0,0,0],[1,1,1,0,1],[0,1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,0,1],[0,0,1,0,1],[0,0,0,0,0],[1,1,1,0,1],[0,0,1,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,0,1],[0,0,1,0,1],[0,0,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "grid": [[0,1,1,1,0],[0,0,0,1,0],[0,1,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,1,1,1,0],[0,0,1,0,0],[0,0,0,0,0],[0,1,1,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,1,1,1,0],[0,0,1,1,0],[0,0,0,0,0],[0,0,1,0,0],[1,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,0,0],[0,1,1,0,0],[0,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,0,0],[0,1,1,0,1],[0,1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,1,0,1],[0,1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,1,0],[0,1,0,0,0],[0,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,1,1],[0,1,0,0,0],[0,1,0,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,1,1],[0,1,0,0,1],[0,1,0,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,1],[0,1,0,0,1],[0,1,1,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,1,0,0],[0,0,0,0,0],[0,1,1,0,0],[0,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,1,0,0],[0,0,0,0,0],[0,1,1,0,1],[0,1,1,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,1,0,1],[0,0,0,0,1],[0,1,1,0,1],[0,0,0,0,0]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "grid": [[0,1,1,1,1],[0,0,1,1,0],[0,0,0,0,0],[0,0,1,0,0],[0,1,1,1,0]] }
assert my_solution.findChampion(**test_input) == 0 | 1,699,151,400 |
weekly-contest-370-find-champion-ii | https://leetcode.com/problems/find-champion-ii | find-champion-ii | {
"questionId": "3189",
"questionFrontendId": "2924",
"title": "Find Champion II",
"titleSlug": "find-champion-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 92,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
There are n teams numbered from 0 to n - 1 in a tournament; each team is also a node in a DAG.
You are given the integer n and a 0-indexed 2D integer array edges of length m representing the DAG, where edges[i] = [ui, vi] indicates that there is a directed edge from team ui to team vi in the graph.
A directed edge from a to b in the graph means that team a is stronger than team b and team b is weaker than team a.
Team a will be the champion of the tournament if there is no team b that is stronger than team a.
Return the team that will be the champion of the tournament if there is a unique champion, otherwise, return -1.
Notes
* A cycle is a series of nodes a1, a2, ..., an, an+1 such that node a1 is the same node as node an+1, the nodes a1, a2, ..., an are distinct, and there is a directed edge from the node ai to node ai+1 for every i in the range [1, n].
* A DAG is a directed graph that does not have any cycle.
Example 1:
[https://assets.leetcode.com/uploads/2023/10/19/graph-3.png]
Input: n = 3, edges = [[0,1],[1,2]]
Output: 0
Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
Example 2:
[https://assets.leetcode.com/uploads/2023/10/19/graph-4.png]
Input: n = 4, edges = [[0,2],[1,3],[1,2]]
Output: -1
Explanation: Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
Constraints:
* 1 <= n <= 100
* m == edges.length
* 0 <= m <= n * (n - 1) / 2
* edges[i].length == 2
* 0 <= edge[i][j] <= n - 1
* edges[i][0] != edges[i][1]
* The input is generated such that if team a is stronger than team b, team b is not stronger than team a.
* The input is generated such that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.
"""
class Solution:
def findChampion(self, n: int, edges: List[List[int]]) -> int:
| There are n teams numbered from 0 to n - 1 in a tournament; each team is also a node in a DAG.
You are given the integer n and a 0-indexed 2D integer array edges of length m representing the DAG, where edges[i] = [ui, vi] indicates that there is a directed edge from team ui to team vi in the graph.
A directed edge from a to b in the graph means that team a is stronger than team b and team b is weaker than team a.
Team a will be the champion of the tournament if there is no team b that is stronger than team a.
Return the team that will be the champion of the tournament if there is a unique champion, otherwise, return -1.
Notes
* A cycle is a series of nodes a1, a2, ..., an, an+1 such that node a1 is the same node as node an+1, the nodes a1, a2, ..., an are distinct, and there is a directed edge from the node ai to node ai+1 for every i in the range [1, n].
* A DAG is a directed graph that does not have any cycle.
Example 1:
[https://assets.leetcode.com/uploads/2023/10/19/graph-3.png]
Input: n = 3, edges = [[0,1],[1,2]]
Output: 0
Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
Example 2:
[https://assets.leetcode.com/uploads/2023/10/19/graph-4.png]
Input: n = 4, edges = [[0,2],[1,3],[1,2]]
Output: -1
Explanation: Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
Constraints:
* 1 <= n <= 100
* m == edges.length
* 0 <= m <= n * (n - 1) / 2
* edges[i].length == 2
* 0 <= edge[i][j] <= n - 1
* edges[i][0] != edges[i][1]
* The input is generated such that if team a is stronger than team b, team b is not stronger than team a.
* The input is generated such that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.
Please complete the code below to solve above prblem:
```python
class Solution:
def findChampion(self, n: int, edges: List[List[int]]) -> int:
``` |
my_solution = Solution()
test_input = { "n": 3, "edges": [[0,1],[1,2]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 4, "edges": [[0,2],[1,3],[1,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 1, "edges": [] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 2, "edges": [] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 2, "edges": [[0,1]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 2, "edges": [[1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 3, "edges": [] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 3, "edges": [[0,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 3, "edges": [[0,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 3, "edges": [[1,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 3, "edges": [[2,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 3, "edges": [[0,1],[2,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 3, "edges": [[0,2],[0,1]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 3, "edges": [[0,2],[1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 3, "edges": [[2,0],[1,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 3, "edges": [[2,0],[2,1]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "n": 3, "edges": [[2,1],[2,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "n": 3, "edges": [[0,1],[1,2],[0,2]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 3, "edges": [[0,1],[2,1],[0,2]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 3, "edges": [[0,2],[0,1],[1,2]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 3, "edges": [[0,2],[0,1],[2,1]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 3, "edges": [[0,2],[1,2],[1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 3, "edges": [[1,0],[0,2],[1,2]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 3, "edges": [[2,1],[1,0],[2,0]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "n": 4, "edges": [] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[0,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[1,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[1,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[3,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[0,1],[2,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[0,3],[2,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[1,3],[2,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[2,1],[1,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[3,0],[3,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[0,1],[2,0],[2,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[0,2],[3,2],[1,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[1,0],[2,3],[1,2]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 4, "edges": [[1,2],[0,3],[1,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[1,2],[1,0],[1,3]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 4, "edges": [[1,3],[1,2],[0,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[1,3],[3,0],[2,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[3,1],[2,0],[1,2]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 4, "edges": [[3,1],[2,1],[0,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[0,2],[0,3],[1,2],[1,0]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 4, "edges": [[2,0],[2,3],[3,1],[2,1]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "n": 4, "edges": [[2,1],[1,0],[3,0],[2,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[3,0],[3,1],[2,1],[0,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[3,0],[1,0],[1,2],[3,2],[2,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 4, "edges": [[3,0],[2,0],[1,0],[2,3],[1,2]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 4, "edges": [[3,2],[0,1],[3,0],[3,1],[2,0]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 4, "edges": [[0,3],[2,3],[2,1],[1,0],[2,0],[1,3]] }
assert my_solution.findChampion(**test_input) == 2
test_input = { "n": 4, "edges": [[1,2],[2,3],[0,2],[0,1],[0,3],[1,3]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 4, "edges": [[2,1],[3,1],[3,0],[3,2],[2,0],[0,1]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 4, "edges": [[2,3],[2,1],[0,1],[0,3],[3,1],[0,2]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 5, "edges": [] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[0,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[2,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[4,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[1,4],[2,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[2,0],[4,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[3,2],[0,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[3,4],[2,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[0,4],[1,4],[0,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[1,3],[4,2],[1,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[2,3],[4,1],[3,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[0,4],[2,0],[1,3],[2,4]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[0,1],[2,1],[4,1],[4,2],[4,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[0,2],[2,1],[3,2],[4,1],[0,4]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[2,3],[0,4],[1,4],[1,0],[4,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[3,1],[0,2],[4,2],[0,1],[1,2]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[3,2],[3,4],[3,0],[3,1],[0,2]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 5, "edges": [[4,0],[3,0],[2,4],[3,4],[4,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[4,3],[1,0],[1,2],[3,2],[4,1]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "n": 5, "edges": [[2,1],[0,3],[0,1],[0,4],[0,2],[4,1]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 5, "edges": [[2,1],[4,1],[3,0],[2,0],[3,4],[3,2]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 5, "edges": [[0,1],[0,4],[2,0],[3,4],[3,1],[2,1],[3,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 5, "edges": [[1,4],[3,1],[0,1],[3,0],[0,2],[2,4],[3,4]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 5, "edges": [[3,2],[1,2],[2,0],[2,4],[1,4],[3,1],[3,4]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 5, "edges": [[0,4],[0,3],[4,3],[4,2],[1,2],[4,1],[0,1],[3,2]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 5, "edges": [[4,3],[4,2],[4,1],[2,3],[4,0],[3,1],[2,0],[0,3]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "n": 5, "edges": [[1,2],[1,4],[2,3],[0,2],[1,0],[1,3],[0,3],[4,3],[0,4]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 5, "edges": [[1,3],[3,0],[3,4],[2,0],[3,2],[0,4],[2,4],[1,0],[1,2]] }
assert my_solution.findChampion(**test_input) == 1
test_input = { "n": 5, "edges": [[3,0],[4,0],[3,2],[0,1],[0,2],[4,3],[1,2],[4,2],[3,1]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "n": 5, "edges": [[4,0],[2,3],[4,3],[4,2],[2,0],[4,1],[1,3],[1,0],[3,0]] }
assert my_solution.findChampion(**test_input) == 4
test_input = { "n": 5, "edges": [[0,2],[1,3],[4,1],[4,2],[2,1],[0,3],[0,1],[2,3],[0,4],[4,3]] }
assert my_solution.findChampion(**test_input) == 0
test_input = { "n": 5, "edges": [[2,0],[4,0],[3,4],[4,2],[1,2],[1,0],[3,1],[3,0],[1,4],[3,2]] }
assert my_solution.findChampion(**test_input) == 3
test_input = { "n": 6, "edges": [] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[2,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[2,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[3,5]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[5,4]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[1,2],[4,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[0,4],[4,5],[3,1]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[0,4],[5,2],[5,4],[3,0],[1,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[1,0],[1,4],[0,2],[3,5],[3,4]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[5,0],[2,4],[0,4],[3,2],[2,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[1,2],[3,2],[3,5],[4,0],[1,5],[0,5]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[3,2],[1,2],[3,0],[5,0],[5,1],[5,3]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[4,2],[0,4],[1,3],[3,4],[1,2],[1,0]] }
assert my_solution.findChampion(**test_input) == -1
test_input = { "n": 6, "edges": [[0,5],[2,0],[1,5],[3,2],[2,1],[2,4],[4,5]] }
assert my_solution.findChampion(**test_input) == 3 | 1,699,151,400 |
weekly-contest-370-maximum-score-after-applying-operations-on-a-tree | https://leetcode.com/problems/maximum-score-after-applying-operations-on-a-tree | maximum-score-after-applying-operations-on-a-tree | {
"questionId": "3191",
"questionFrontendId": "2925",
"title": "Maximum Score After Applying Operations on a Tree",
"titleSlug": "maximum-score-after-applying-operations-on-a-tree",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 243,
"dislikes": 45,
"categoryTitle": "Algorithms"
} | """
There is an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given 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.
You start with a score of 0. In one operation, you can:
* Pick any node i.
* Add values[i] to your score.
* Set values[i] to 0.
A tree is healthy if the sum of values on the path from the root to any leaf node is different than zero.
Return the maximum score you can obtain after performing these operations on the tree any number of times so that it remains healthy.
Example 1:
[https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png]
Input: edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
Output: 11
Explanation: We can choose nodes 1, 2, 3, 4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1] + values[2] + values[3] + values[4] + values[5] = 11.
It can be shown that 11 is the maximum score obtainable after any number of operations on the tree.
Example 2:
[https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png]
Input: edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
Output: 40
Explanation: We can choose nodes 0, 2, 3, and 4.
- The sum of values on the path from 0 to 4 is equal to 10.
- The sum of values on the path from 0 to 3 is equal to 10.
- The sum of values on the path from 0 to 5 is equal to 3.
- The sum of values on the path from 0 to 6 is equal to 5.
Therefore, the tree is healthy and the score is values[0] + values[2] + values[3] + values[4] = 40.
It can be shown that 40 is the maximum score obtainable after any number of operations on the tree.
Constraints:
* 2 <= n <= 2 * 104
* edges.length == n - 1
* edges[i].length == 2
* 0 <= ai, bi < n
* values.length == n
* 1 <= values[i] <= 109
* The input is generated such that edges represents a valid tree.
"""
class Solution:
def maximumScoreAfterOperations(self, edges: List[List[int]], values: List[int]) -> int:
| There is an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given 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.
You start with a score of 0. In one operation, you can:
* Pick any node i.
* Add values[i] to your score.
* Set values[i] to 0.
A tree is healthy if the sum of values on the path from the root to any leaf node is different than zero.
Return the maximum score you can obtain after performing these operations on the tree any number of times so that it remains healthy.
Example 1:
[https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png]
Input: edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
Output: 11
Explanation: We can choose nodes 1, 2, 3, 4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1] + values[2] + values[3] + values[4] + values[5] = 11.
It can be shown that 11 is the maximum score obtainable after any number of operations on the tree.
Example 2:
[https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png]
Input: edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
Output: 40
Explanation: We can choose nodes 0, 2, 3, and 4.
- The sum of values on the path from 0 to 4 is equal to 10.
- The sum of values on the path from 0 to 3 is equal to 10.
- The sum of values on the path from 0 to 5 is equal to 3.
- The sum of values on the path from 0 to 6 is equal to 5.
Therefore, the tree is healthy and the score is values[0] + values[2] + values[3] + values[4] = 40.
It can be shown that 40 is the maximum score obtainable after any number of operations on the tree.
Constraints:
* 2 <= n <= 2 * 104
* edges.length == n - 1
* edges[i].length == 2
* 0 <= ai, bi < n
* values.length == n
* 1 <= values[i] <= 109
* The input is generated such that edges represents a valid tree.
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumScoreAfterOperations(self, edges: List[List[int]], values: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "edges": [[0,1],[0,2],[0,3],[2,4],[4,5]], "values": [5,2,5,2,1,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 11
test_input = { "edges": [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], "values": [20,10,9,7,4,3,5] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 40
test_input = { "edges": [[0,1]], "values": [1,2] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 2
test_input = { "edges": [[0,1]], "values": [2,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 2
test_input = { "edges": [[0,1],[0,2]], "values": [1000000000,1000000000,1000000000] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 2000000000
test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [1000000000,1000000000,1000000000,1000000000] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 3000000000
test_input = { "edges": [[7,0],[3,1],[6,2],[4,3],[4,5],[4,6],[4,7]], "values": [2,16,23,17,22,21,8,6] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 113
test_input = { "edges": [[3,1],[0,2],[0,3]], "values": [21,12,19,5] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 36
test_input = { "edges": [[2,0],[4,1],[5,3],[4,6],[2,4],[5,2],[5,7]], "values": [12,12,7,9,2,11,12,25] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 83
test_input = { "edges": [[1,0],[9,1],[6,2],[7,4],[3,5],[7,3],[9,6],[7,8],[7,9]], "values": [14,17,13,18,17,10,23,19,22,2] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 153
test_input = { "edges": [[5,0],[4,3],[2,5],[6,2],[4,6],[1,4],[1,7]], "values": [15,12,13,23,8,1,2,23] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 96
test_input = { "edges": [[0,2],[1,3],[0,5],[1,0],[4,1],[4,6]], "values": [22,25,4,21,8,20,4] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 82
test_input = { "edges": [[4,1],[6,3],[2,4],[0,2],[9,5],[0,6],[9,7],[0,8],[0,9]], "values": [3,18,10,16,9,3,25,17,8,9] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 115
test_input = { "edges": [[6,1],[3,4],[0,3],[2,0],[5,2],[5,6]], "values": [25,20,16,2,13,8,19] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 93
test_input = { "edges": [[9,2],[5,4],[5,6],[1,5],[8,1],[0,7],[3,0],[9,3],[8,9]], "values": [21,13,10,14,20,11,19,22,3,16] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 128
test_input = { "edges": [[2,4],[1,5],[0,1],[2,0],[3,2],[3,6]], "values": [17,5,24,18,6,16,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 75
test_input = { "edges": [[1,2],[4,1],[3,6],[4,3],[5,4],[0,5],[0,7]], "values": [8,13,19,13,4,3,24,25] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 101
test_input = { "edges": [[9,0],[7,1],[6,3],[8,4],[2,5],[9,6],[2,8],[7,2],[7,9]], "values": [4,13,21,1,25,8,21,22,9,18] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 138
test_input = { "edges": [[1,0],[4,3],[1,5],[6,1],[4,6],[2,7],[4,2],[4,8]], "values": [10,5,25,19,2,20,15,3,3] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 97
test_input = { "edges": [[6,1],[3,4],[2,5],[0,6],[3,0],[2,3],[2,7]], "values": [2,23,10,20,22,10,6,24] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 115
test_input = { "edges": [[2,1],[0,2],[5,0],[4,3],[5,4],[5,6]], "values": [9,21,12,20,5,2,13] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 73
test_input = { "edges": [[8,1],[7,4],[0,5],[2,0],[3,2],[8,3],[7,6],[7,8]], "values": [23,24,25,12,12,7,1,17,17] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 119
test_input = { "edges": [[3,0],[3,1],[2,3]], "values": [19,8,8,5] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 35
test_input = { "edges": [[5,0],[7,1],[3,2],[6,4],[6,5],[3,6],[3,7]], "values": [19,7,17,9,13,7,25,3] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 93
test_input = { "edges": [[3,1],[2,3],[0,2],[0,4]], "values": [14,15,18,15,20] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 68
test_input = { "edges": [[6,0],[2,1],[6,2],[6,4],[5,7],[6,5],[3,6],[9,3],[8,9]], "values": [17,20,17,13,5,12,8,12,14,25] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 135
test_input = { "edges": [[4,1],[6,2],[9,3],[0,6],[0,7],[9,0],[4,8],[5,4],[5,9]], "values": [11,16,10,25,21,25,15,10,5,7] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 134
test_input = { "edges": [[1,0],[3,1],[2,3]], "values": [25,19,12,2] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 56
test_input = { "edges": [[4,1],[0,2],[4,0],[3,4]], "values": [12,24,1,11,3] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 47
test_input = { "edges": [[1,0],[5,1],[2,4],[3,2],[3,5]], "values": [21,2,17,18,22,16] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 94
test_input = { "edges": [[5,1],[4,3],[2,4],[6,2],[0,5],[0,6]], "values": [18,24,5,20,23,6,7] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 92
test_input = { "edges": [[4,1],[6,3],[2,4],[5,2],[0,5],[0,6]], "values": [19,2,23,18,3,12,9] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 75
test_input = { "edges": [[5,0],[2,1],[6,3],[5,4],[2,5],[2,6]], "values": [22,11,2,11,3,11,17] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 72
test_input = { "edges": [[6,0],[4,1],[3,2],[6,5],[4,6],[3,4],[3,7],[3,8]], "values": [18,20,14,23,20,8,24,12,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 122
test_input = { "edges": [[1,2],[0,1],[0,3],[0,4]], "values": [6,8,2,16,6] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 32
test_input = { "edges": [[6,1],[3,4],[3,5],[0,3],[2,0],[2,6]], "values": [5,25,2,12,15,3,3] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 60
test_input = { "edges": [[4,0],[2,1],[2,3],[2,4]], "values": [9,5,14,24,19] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 62
test_input = { "edges": [[4,0],[2,1],[6,3],[2,5],[4,6],[2,4],[7,2],[7,8]], "values": [19,10,14,18,17,8,2,8,24] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 104
test_input = { "edges": [[6,0],[7,1],[5,3],[2,4],[8,5],[6,7],[2,6],[2,8]], "values": [8,8,19,17,24,1,7,18,12] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 107
test_input = { "edges": [[6,0],[3,1],[3,2],[3,6],[4,3],[5,4],[5,7]], "values": [15,3,25,2,10,11,10,13] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 87
test_input = { "edges": [[7,3],[2,5],[4,2],[0,4],[7,0],[1,6],[1,7]], "values": [1,19,5,1,18,24,4,20] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 91
test_input = { "edges": [[7,3],[2,4],[9,2],[1,5],[1,7],[0,1],[9,0],[6,8],[6,9]], "values": [21,10,9,25,7,20,5,8,20,5] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 115
test_input = { "edges": [[3,1],[4,5],[0,4],[2,0],[3,2],[3,6]], "values": [13,11,16,12,20,1,7] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 67
test_input = { "edges": [[2,0],[3,1],[6,2],[6,3],[5,4],[7,5],[6,7]], "values": [21,20,8,21,11,12,23,4] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 112
test_input = { "edges": [[0,2],[7,4],[1,6],[5,1],[3,5],[0,3],[0,7]], "values": [8,6,9,5,4,1,8,6] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 39
test_input = { "edges": [[5,1],[8,3],[0,4],[8,0],[2,5],[2,7],[9,2],[6,8],[6,9]], "values": [2,22,2,19,25,14,11,24,7,6] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 130
test_input = { "edges": [[3,0],[3,1],[2,3]], "values": [24,24,4,12] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 52
test_input = { "edges": [[2,0],[3,1],[2,3]], "values": [7,10,13,8] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 31
test_input = { "edges": [[1,0],[3,1],[6,4],[3,5],[2,3],[2,6]], "values": [6,14,4,17,16,19,24] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 94
test_input = { "edges": [[0,1],[3,0],[8,3],[2,4],[8,5],[2,6],[8,2],[7,8]], "values": [22,19,10,16,14,11,2,17,9] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 98
test_input = { "edges": [[5,1],[5,2],[0,3],[0,4],[0,5]], "values": [21,18,2,20,1,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 42
test_input = { "edges": [[6,0],[3,2],[1,3],[6,4],[1,5],[1,6]], "values": [14,19,22,6,19,12,20] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 98
test_input = { "edges": [[6,0],[4,1],[2,3],[5,2],[6,4],[5,6]], "values": [16,16,9,12,5,14,17] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 75
test_input = { "edges": [[2,1],[4,2],[0,3],[0,4]], "values": [9,6,7,17,19] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 49
test_input = { "edges": [[1,3],[2,1],[4,5],[2,4],[0,2],[0,6]], "values": [4,19,10,25,16,13,6] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 89
test_input = { "edges": [[1,0],[5,1],[2,5],[3,2],[4,6],[3,4],[3,7]], "values": [12,9,19,10,24,22,18,16] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 121
test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [25,23,7,9] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 39
test_input = { "edges": [[3,0],[3,2],[4,5],[9,6],[4,7],[9,4],[3,8],[1,3],[1,9]], "values": [23,22,7,22,19,12,10,11,24,3] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 131
test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [9,8,24,21] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 53
test_input = { "edges": [[3,1],[0,2],[0,3]], "values": [17,2,2,4] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 21
test_input = { "edges": [[5,2],[3,4],[0,3],[1,0],[1,5]], "values": [16,6,15,15,10,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 52
test_input = { "edges": [[8,0],[8,1],[7,2],[3,7],[6,3],[4,6],[5,4],[5,8]], "values": [10,25,10,6,21,17,11,15,15] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 120
test_input = { "edges": [[0,4],[1,5],[2,6],[0,2],[1,0],[3,1],[3,7]], "values": [20,1,16,12,5,23,21,4] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 82
test_input = { "edges": [[7,1],[5,2],[0,3],[7,0],[7,4],[6,5],[6,7]], "values": [6,20,14,17,18,16,11,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 97
test_input = { "edges": [[3,2],[8,4],[1,6],[0,1],[5,0],[3,5],[8,3],[7,8]], "values": [3,18,16,22,10,2,7,3,10] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 88
test_input = { "edges": [[4,0],[3,4],[3,5],[3,6],[2,3],[1,2],[1,7]], "values": [12,14,7,25,13,16,12,15] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 102
test_input = { "edges": [[3,0],[5,2],[1,3],[4,1],[4,5]], "values": [11,18,19,14,8,11] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 73
test_input = { "edges": [[1,2],[4,1],[0,4],[8,5],[0,6],[7,0],[3,7],[3,8]], "values": [17,22,14,15,2,21,7,9,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 98
test_input = { "edges": [[2,1],[0,2],[4,3],[0,4],[0,5]], "values": [1,2,24,25,9,24] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 84
test_input = { "edges": [[3,1],[3,2],[0,4],[3,5],[0,3],[0,6]], "values": [21,19,5,20,2,25,20] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 91
test_input = { "edges": [[1,0],[4,1],[4,2],[3,4]], "values": [3,6,17,4,20] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 47
test_input = { "edges": [[6,1],[2,3],[6,7],[4,6],[2,4],[5,2],[5,8],[0,5],[0,9]], "values": [23,19,15,4,3,18,25,22,6,11] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 123
test_input = { "edges": [[2,0],[1,2],[5,1],[3,4],[3,5]], "values": [23,24,10,15,10,22] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 94
test_input = { "edges": [[1,0],[6,1],[6,2],[4,3],[5,4],[5,6]], "values": [18,21,3,13,25,15,20] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 99
test_input = { "edges": [[1,0],[3,1],[2,3]], "values": [23,10,24,15] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 62
test_input = { "edges": [[1,0],[1,2],[1,3]], "values": [10,4,12,14] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 36
test_input = { "edges": [[2,0],[3,2],[1,3],[4,5],[1,4],[1,6]], "values": [3,8,13,11,6,18,8] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 64
test_input = { "edges": [[3,0],[3,2],[1,3],[1,4]], "values": [18,19,7,7,2] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 46
test_input = { "edges": [[7,0],[5,2],[1,3],[7,4],[1,5],[7,1],[6,7]], "values": [24,24,16,17,25,9,3,23] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 118
test_input = { "edges": [[2,0],[2,1],[2,3]], "values": [25,12,5,7] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 44
test_input = { "edges": [[0,1],[2,0],[2,3],[2,4]], "values": [24,14,9,5,24] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 53
test_input = { "edges": [[6,2],[7,3],[0,5],[4,0],[1,6],[4,1],[4,7]], "values": [20,15,15,2,22,7,19,24] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 104
test_input = { "edges": [[1,0],[4,2],[7,5],[3,6],[1,3],[4,1],[4,7]], "values": [11,21,15,23,2,7,21,3] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 92
test_input = { "edges": [[4,0],[2,1],[2,3],[2,4]], "values": [22,20,20,8,14] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 70
test_input = { "edges": [[0,2],[4,3],[0,4],[1,0],[1,6],[5,1],[5,7]], "values": [18,10,19,9,11,14,11,18] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 92
test_input = { "edges": [[6,1],[4,2],[4,3],[0,5],[4,0],[4,6]], "values": [18,10,5,23,16,13,1] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 68
test_input = { "edges": [[4,0],[6,2],[6,3],[6,4],[1,5],[7,1],[6,7]], "values": [14,15,22,9,13,2,25,3] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 90
test_input = { "edges": [[6,2],[0,3],[5,0],[1,5],[4,1],[4,6]], "values": [22,10,19,14,18,24,8] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 93
test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [6,22,9,5] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 36
test_input = { "edges": [[2,1],[0,2],[6,0],[6,4],[3,5],[3,6],[3,7]], "values": [9,16,13,9,19,1,16,19] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 93
test_input = { "edges": [[3,5],[7,3],[0,6],[4,0],[1,4],[2,1],[2,7]], "values": [16,9,8,14,6,18,23,25] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 103
test_input = { "edges": [[7,1],[4,5],[3,6],[4,3],[2,4],[0,2],[0,7]], "values": [7,22,12,22,5,3,6,7] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 77
test_input = { "edges": [[5,0],[4,1],[5,2],[3,5],[4,3],[7,4],[6,7]], "values": [19,16,8,13,15,13,3,5] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 79
test_input = { "edges": [[7,1],[6,2],[3,5],[8,3],[0,6],[4,0],[7,4],[7,8]], "values": [23,11,22,6,19,6,19,8,25] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 116
test_input = { "edges": [[4,0],[4,2],[1,3],[4,1],[4,5]], "values": [23,22,13,1,11,25] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 84
test_input = { "edges": [[4,0],[4,1],[2,3],[2,4]], "values": [3,17,5,24,14] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 60
test_input = { "edges": [[4,0],[2,4],[1,2],[3,1],[3,5]], "values": [6,12,21,6,8,8] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 55
test_input = { "edges": [[4,2],[5,4],[0,5],[0,6],[3,0],[1,3],[7,8],[1,7],[1,9]], "values": [25,16,16,14,22,19,2,22,13,11] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 135
test_input = { "edges": [[4,0],[1,3],[4,1],[7,5],[2,6],[4,2],[4,7]], "values": [23,16,24,25,3,21,3,25] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 137
test_input = { "edges": [[0,1],[2,0],[2,3]], "values": [22,17,9,9] }
assert my_solution.maximumScoreAfterOperations(**test_input) == 35 | 1,699,151,400 |
weekly-contest-370-maximum-balanced-subsequence-sum | https://leetcode.com/problems/maximum-balanced-subsequence-sum | maximum-balanced-subsequence-sum | {
"questionId": "3184",
"questionFrontendId": "2926",
"title": "Maximum Balanced Subsequence Sum",
"titleSlug": "maximum-balanced-subsequence-sum",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 133,
"dislikes": 6,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums.
A subsequence of nums having length k and consisting of indices i0 < i1 < ... < ik-1 is balanced if the following holds:
* nums[ij] - nums[ij-1] >= ij - ij-1, for every j in the range [1, k - 1].
A subsequence of nums having length 1 is considered balanced.
Return an integer denoting the maximum possible sum of elements in a balanced subsequence of nums.
A subsequence of an array is a new non-empty array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Example 1:
Input: nums = [3,3,5,6]
Output: 14
Explanation: In this example, the subsequence [3,5,6] consisting of indices 0, 2, and 3 can be selected.
nums[2] - nums[0] >= 2 - 0.
nums[3] - nums[2] >= 3 - 2.
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
The subsequence consisting of indices 1, 2, and 3 is also valid.
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 14.
Example 2:
Input: nums = [5,-1,-3,8]
Output: 13
Explanation: In this example, the subsequence [5,8] consisting of indices 0 and 3 can be selected.
nums[3] - nums[0] >= 3 - 0.
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 13.
Example 3:
Input: nums = [-2,-1]
Output: -1
Explanation: In this example, the subsequence [-1] can be selected.
It is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
Constraints:
* 1 <= nums.length <= 105
* -109 <= nums[i] <= 109
"""
class Solution:
def maxBalancedSubsequenceSum(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums.
A subsequence of nums having length k and consisting of indices i0 < i1 < ... < ik-1 is balanced if the following holds:
* nums[ij] - nums[ij-1] >= ij - ij-1, for every j in the range [1, k - 1].
A subsequence of nums having length 1 is considered balanced.
Return an integer denoting the maximum possible sum of elements in a balanced subsequence of nums.
A subsequence of an array is a new non-empty array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Example 1:
Input: nums = [3,3,5,6]
Output: 14
Explanation: In this example, the subsequence [3,5,6] consisting of indices 0, 2, and 3 can be selected.
nums[2] - nums[0] >= 2 - 0.
nums[3] - nums[2] >= 3 - 2.
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
The subsequence consisting of indices 1, 2, and 3 is also valid.
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 14.
Example 2:
Input: nums = [5,-1,-3,8]
Output: 13
Explanation: In this example, the subsequence [5,8] consisting of indices 0 and 3 can be selected.
nums[3] - nums[0] >= 3 - 0.
Hence, it is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
It can be shown that it is not possible to get a balanced subsequence with a sum greater than 13.
Example 3:
Input: nums = [-2,-1]
Output: -1
Explanation: In this example, the subsequence [-1] can be selected.
It is a balanced subsequence, and its sum is the maximum among the balanced subsequences of nums.
Constraints:
* 1 <= nums.length <= 105
* -109 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def maxBalancedSubsequenceSum(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [3,3,5,6] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 14
test_input = { "nums": [5,-1,-3,8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 13
test_input = { "nums": [-2,-1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1
test_input = { "nums": [0] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [-47] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -47
test_input = { "nums": [-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -8
test_input = { "nums": [-7] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -7
test_input = { "nums": [-6] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -6
test_input = { "nums": [-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -5
test_input = { "nums": [-3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -3
test_input = { "nums": [-2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2
test_input = { "nums": [-1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1
test_input = { "nums": [1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1
test_input = { "nums": [3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3
test_input = { "nums": [4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4
test_input = { "nums": [5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [7] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8
test_input = { "nums": [9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [45] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 45
test_input = { "nums": [-9,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -5
test_input = { "nums": [-6,8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8
test_input = { "nums": [-5,-1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1
test_input = { "nums": [-5,0] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [-3,-1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1
test_input = { "nums": [-2,3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3
test_input = { "nums": [-1,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1
test_input = { "nums": [-1,0] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [-1,3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3
test_input = { "nums": [0,-3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [0,5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [2,7] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [5,-1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [6,-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6
test_input = { "nums": [7,-2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [7,2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [8,-9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8
test_input = { "nums": [9,-9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [-43,23,-49] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 23
test_input = { "nums": [-9,-6,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -5
test_input = { "nums": [-9,-2,4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4
test_input = { "nums": [-9,5,-6] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [-8,9,-9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [-5,-1,-9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1
test_input = { "nums": [-4,-9,3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3
test_input = { "nums": [-4,-2,-7] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2
test_input = { "nums": [-3,-4,-2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2
test_input = { "nums": [-2,-1,-2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1
test_input = { "nums": [-1,-6,1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1
test_input = { "nums": [0,-6,-4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [0,-5,4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4
test_input = { "nums": [1,-4,4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [4,1,-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4
test_input = { "nums": [5,-6,-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [5,-4,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [6,-3,9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 15
test_input = { "nums": [6,0,2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6
test_input = { "nums": [7,-6,0] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [8,-7,2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8
test_input = { "nums": [14,-21,-18] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 14
test_input = { "nums": [-9,7,-8,1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [-8,2,-5,-7] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 2
test_input = { "nums": [-7,1,-2,-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1
test_input = { "nums": [-6,-8,7,-3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [-5,-1,0,6] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6
test_input = { "nums": [-4,8,9,2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 17
test_input = { "nums": [-4,9,7,-4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [-2,-6,0,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [-2,-3,9,3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [0,-6,-3,5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [0,1,0,-2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1
test_input = { "nums": [1,-7,-8,6] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [3,-7,9,-3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 12
test_input = { "nums": [3,-7,9,2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 12
test_input = { "nums": [4,-8,-1,8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 12
test_input = { "nums": [5,4,1,0] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [7,7,-9,-4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7
test_input = { "nums": [8,0,4,-2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8
test_input = { "nums": [9,7,-2,1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [34,34,32,33] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 65
test_input = { "nums": [-9,-6,-8,-2,4] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4
test_input = { "nums": [-9,-5,2,2,7] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [-9,-2,-6,0,6] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6
test_input = { "nums": [-7,0,-8,-9,-3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [-7,0,9,-4,9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [-6,-2,-8,-4,-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2
test_input = { "nums": [-6,2,-3,0,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 2
test_input = { "nums": [-6,3,-6,-3,-2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3
test_input = { "nums": [-5,-9,1,3,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4
test_input = { "nums": [-5,1,5,-5,-1] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6
test_input = { "nums": [-3,7,0,4,6] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 10
test_input = { "nums": [-2,-2,9,-2,-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9
test_input = { "nums": [0,-6,-9,-8,-3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [0,-2,-7,-1,-8] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [0,-1,-4,-6,-9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0
test_input = { "nums": [1,-3,-8,9,-9] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 10
test_input = { "nums": [2,-6,-2,0,-3] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 2
test_input = { "nums": [2,9,-4,4,2] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 11
test_input = { "nums": [4,-1,5,-1,-7] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5
test_input = { "nums": [4,6,-8,-8,-5] }
assert my_solution.maxBalancedSubsequenceSum(**test_input) == 10 | 1,699,151,400 |
weekly-contest-369-find-the-k-or-of-an-array | https://leetcode.com/problems/find-the-k-or-of-an-array | find-the-k-or-of-an-array | {
"questionId": "3183",
"questionFrontendId": "2917",
"title": "Find the K-or of an Array",
"titleSlug": "find-the-k-or-of-an-array",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 55,
"dislikes": 201,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums, and an integer k.
The K-or of nums is a non-negative integer that satisfies the following:
* The ith bit is set in the K-or if and only if there are at least k elements of nums in which bit i is set.
Return the K-or of nums.
Note that a bit i is set in x if (2i AND x) == 2i, where AND is the bitwise AND operator.
Example 1:
Input: nums = [7,12,9,8,9,15], k = 4
Output: 9
Explanation: Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].
Bit 1 is set at nums[0], and nums[5].
Bit 2 is set at nums[0], nums[1], and nums[5].
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].
Only bits 0 and 3 are set in at least k elements of the array, and bits i >= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9.
Example 2:
Input: nums = [2,12,1,11,4,5], k = 6
Output: 0
Explanation: Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0.
Example 3:
Input: nums = [10,8,5,9,11,6,8], k = 1
Output: 15
Explanation: Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.
Constraints:
* 1 <= nums.length <= 50
* 0 <= nums[i] < 231
* 1 <= k <= nums.length
"""
class Solution:
def findKOr(self, nums: List[int], k: int) -> int:
| You are given a 0-indexed integer array nums, and an integer k.
The K-or of nums is a non-negative integer that satisfies the following:
* The ith bit is set in the K-or if and only if there are at least k elements of nums in which bit i is set.
Return the K-or of nums.
Note that a bit i is set in x if (2i AND x) == 2i, where AND is the bitwise AND operator.
Example 1:
Input: nums = [7,12,9,8,9,15], k = 4
Output: 9
Explanation: Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].
Bit 1 is set at nums[0], and nums[5].
Bit 2 is set at nums[0], nums[1], and nums[5].
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].
Only bits 0 and 3 are set in at least k elements of the array, and bits i >= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9.
Example 2:
Input: nums = [2,12,1,11,4,5], k = 6
Output: 0
Explanation: Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0.
Example 3:
Input: nums = [10,8,5,9,11,6,8], k = 1
Output: 15
Explanation: Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.
Constraints:
* 1 <= nums.length <= 50
* 0 <= nums[i] < 231
* 1 <= k <= nums.length
Please complete the code below to solve above prblem:
```python
class Solution:
def findKOr(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [7,12,9,8,9,15], "k": 4 }
assert my_solution.findKOr(**test_input) == 9
test_input = { "nums": [2,12,1,11,4,5], "k": 6 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [10,8,5,9,11,6,8], "k": 1 }
assert my_solution.findKOr(**test_input) == 15
test_input = { "nums": [14,7,12,9,8,9,1,15], "k": 4 }
assert my_solution.findKOr(**test_input) == 13
test_input = { "nums": [2,12,1,11,4,5], "k": 3 }
assert my_solution.findKOr(**test_input) == 5
test_input = { "nums": [10,8,5,10,11,11,6,8], "k": 1 }
assert my_solution.findKOr(**test_input) == 15
test_input = { "nums": [0], "k": 1 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [1], "k": 1 }
assert my_solution.findKOr(**test_input) == 1
test_input = { "nums": [2], "k": 1 }
assert my_solution.findKOr(**test_input) == 2
test_input = { "nums": [3], "k": 1 }
assert my_solution.findKOr(**test_input) == 3
test_input = { "nums": [4], "k": 1 }
assert my_solution.findKOr(**test_input) == 4
test_input = { "nums": [5], "k": 1 }
assert my_solution.findKOr(**test_input) == 5
test_input = { "nums": [6], "k": 1 }
assert my_solution.findKOr(**test_input) == 6
test_input = { "nums": [7], "k": 1 }
assert my_solution.findKOr(**test_input) == 7
test_input = { "nums": [8], "k": 1 }
assert my_solution.findKOr(**test_input) == 8
test_input = { "nums": [9], "k": 1 }
assert my_solution.findKOr(**test_input) == 9
test_input = { "nums": [10], "k": 1 }
assert my_solution.findKOr(**test_input) == 10
test_input = { "nums": [11], "k": 1 }
assert my_solution.findKOr(**test_input) == 11
test_input = { "nums": [12], "k": 1 }
assert my_solution.findKOr(**test_input) == 12
test_input = { "nums": [13], "k": 1 }
assert my_solution.findKOr(**test_input) == 13
test_input = { "nums": [14], "k": 1 }
assert my_solution.findKOr(**test_input) == 14
test_input = { "nums": [15], "k": 1 }
assert my_solution.findKOr(**test_input) == 15
test_input = { "nums": [16], "k": 1 }
assert my_solution.findKOr(**test_input) == 16
test_input = { "nums": [17], "k": 1 }
assert my_solution.findKOr(**test_input) == 17
test_input = { "nums": [18], "k": 1 }
assert my_solution.findKOr(**test_input) == 18
test_input = { "nums": [19], "k": 1 }
assert my_solution.findKOr(**test_input) == 19
test_input = { "nums": [20], "k": 1 }
assert my_solution.findKOr(**test_input) == 20
test_input = { "nums": [21], "k": 1 }
assert my_solution.findKOr(**test_input) == 21
test_input = { "nums": [22], "k": 1 }
assert my_solution.findKOr(**test_input) == 22
test_input = { "nums": [23], "k": 1 }
assert my_solution.findKOr(**test_input) == 23
test_input = { "nums": [24], "k": 1 }
assert my_solution.findKOr(**test_input) == 24
test_input = { "nums": [25], "k": 1 }
assert my_solution.findKOr(**test_input) == 25
test_input = { "nums": [26], "k": 1 }
assert my_solution.findKOr(**test_input) == 26
test_input = { "nums": [27], "k": 1 }
assert my_solution.findKOr(**test_input) == 27
test_input = { "nums": [28], "k": 1 }
assert my_solution.findKOr(**test_input) == 28
test_input = { "nums": [29], "k": 1 }
assert my_solution.findKOr(**test_input) == 29
test_input = { "nums": [30], "k": 1 }
assert my_solution.findKOr(**test_input) == 30
test_input = { "nums": [31], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [22,7,27,30,15,30,28], "k": 4 }
assert my_solution.findKOr(**test_input) == 30
test_input = { "nums": [24,18,3,23,16,11,27,18,5,29], "k": 6 }
assert my_solution.findKOr(**test_input) == 19
test_input = { "nums": [14,1,2,28,4,15,3,12], "k": 2 }
assert my_solution.findKOr(**test_input) == 15
test_input = { "nums": [7,18,25,11,2], "k": 5 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [0,4], "k": 2 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [17,5,14,16,24,30,3,19,31], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [14,20,23,7,1,12,24,19], "k": 7 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [5,31,29,22,8,6,23], "k": 4 }
assert my_solution.findKOr(**test_input) == 23
test_input = { "nums": [9,10,30,0,7,19,14,19,20,3], "k": 4 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [25,6,5,30,27,11,10,30], "k": 2 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [0,15,16,6,19,5,24,17], "k": 3 }
assert my_solution.findKOr(**test_input) == 23
test_input = { "nums": [19,8,2,28,4,5], "k": 5 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [13,9,1,15,9,2,19,19], "k": 3 }
assert my_solution.findKOr(**test_input) == 11
test_input = { "nums": [16,6,16,22,8,2,25,30], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [14,28,23,22], "k": 2 }
assert my_solution.findKOr(**test_input) == 30
test_input = { "nums": [6,26], "k": 2 }
assert my_solution.findKOr(**test_input) == 2
test_input = { "nums": [14,9,22,30,15], "k": 4 }
assert my_solution.findKOr(**test_input) == 14
test_input = { "nums": [12,13,16,25,12,4,8,29], "k": 6 }
assert my_solution.findKOr(**test_input) == 8
test_input = { "nums": [27,29], "k": 2 }
assert my_solution.findKOr(**test_input) == 25
test_input = { "nums": [9,27,27,20,24,13,25,8], "k": 6 }
assert my_solution.findKOr(**test_input) == 8
test_input = { "nums": [22,26,18,26,1], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [20,20,31,19,29,19], "k": 2 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [5,8,27,23,3], "k": 2 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [4,23,0,20,4,19,14,22,26,2], "k": 9 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [31,26,21,4,9,11,13,24,23,5], "k": 10 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [4,22], "k": 2 }
assert my_solution.findKOr(**test_input) == 4
test_input = { "nums": [22,17,20,3,21,5,20,25,16], "k": 4 }
assert my_solution.findKOr(**test_input) == 21
test_input = { "nums": [16,15,13,26,15,23,0,12], "k": 4 }
assert my_solution.findKOr(**test_input) == 15
test_input = { "nums": [4,11,14], "k": 1 }
assert my_solution.findKOr(**test_input) == 15
test_input = { "nums": [10,26,27,25,3,21,9,3,22], "k": 8 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [4,11,16], "k": 2 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [9,27,19,9,24,11], "k": 3 }
assert my_solution.findKOr(**test_input) == 27
test_input = { "nums": [29,19,27,14], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [27,31,21,8,25], "k": 2 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [14,1,13,22,27], "k": 5 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [14,15,17,23,29], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [19,21], "k": 2 }
assert my_solution.findKOr(**test_input) == 17
test_input = { "nums": [29,9,18,0,30,5,1,9], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [6,31,11,7,6,2,26,19,17,13], "k": 10 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [15,8,27,28], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [28,24,20,31,23,1], "k": 2 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [8,13,27,24,20,28,15,21,23,6], "k": 9 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [31,6], "k": 2 }
assert my_solution.findKOr(**test_input) == 6
test_input = { "nums": [3,14,11,17,9], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [14,11], "k": 2 }
assert my_solution.findKOr(**test_input) == 10
test_input = { "nums": [2,9,11,25,3,2,26,21,13,11], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [26,7,23,0,16,31,12,18,24], "k": 4 }
assert my_solution.findKOr(**test_input) == 30
test_input = { "nums": [11,30,30,17,10,27,6,31,0], "k": 4 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [11,9,18,30,27,20,2,17,18,4], "k": 2 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [2,1,0,30,29,14,13,26,10,22], "k": 10 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [21,30,30,17,23,8,26,9], "k": 6 }
assert my_solution.findKOr(**test_input) == 16
test_input = { "nums": [16,10], "k": 1 }
assert my_solution.findKOr(**test_input) == 26
test_input = { "nums": [26,12,19,22,5,6,19,30,24,11], "k": 10 }
assert my_solution.findKOr(**test_input) == 0
test_input = { "nums": [20,10,14], "k": 2 }
assert my_solution.findKOr(**test_input) == 14
test_input = { "nums": [23,17,18,30,3], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [13,16,12], "k": 1 }
assert my_solution.findKOr(**test_input) == 29
test_input = { "nums": [17,12,5,13,23,19], "k": 3 }
assert my_solution.findKOr(**test_input) == 21
test_input = { "nums": [5,4,3], "k": 2 }
assert my_solution.findKOr(**test_input) == 5
test_input = { "nums": [6,28,1,3,2], "k": 1 }
assert my_solution.findKOr(**test_input) == 31
test_input = { "nums": [28,3,15,30,10,29], "k": 4 }
assert my_solution.findKOr(**test_input) == 14
test_input = { "nums": [0,31,13,24,16,21], "k": 3 }
assert my_solution.findKOr(**test_input) == 29
test_input = { "nums": [11,20,28,29,3,4], "k": 2 }
assert my_solution.findKOr(**test_input) == 31 | 1,698,546,600 |
weekly-contest-369-minimum-equal-sum-of-two-arrays-after-replacing-zeros | https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros | minimum-equal-sum-of-two-arrays-after-replacing-zeros | {
"questionId": "3171",
"questionFrontendId": "2918",
"title": "Minimum Equal Sum of Two Arrays After Replacing Zeros",
"titleSlug": "minimum-equal-sum-of-two-arrays-after-replacing-zeros",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 131,
"dislikes": 15,
"categoryTitle": "Algorithms"
} | """
You are given two arrays nums1 and nums2 consisting of positive integers.
You have to replace all the 0's in both arrays with strictly positive integers such that the sum of elements of both arrays becomes equal.
Return the minimum equal sum you can obtain, or -1 if it is impossible.
Example 1:
Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0]
Output: 12
Explanation: We can replace 0's in the following way:
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
Example 2:
Input: nums1 = [2,0,2,0], nums2 = [1,4]
Output: -1
Explanation: It is impossible to make the sum of both arrays equal.
Constraints:
* 1 <= nums1.length, nums2.length <= 105
* 0 <= nums1[i], nums2[i] <= 106
"""
class Solution:
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
| You are given two arrays nums1 and nums2 consisting of positive integers.
You have to replace all the 0's in both arrays with strictly positive integers such that the sum of elements of both arrays becomes equal.
Return the minimum equal sum you can obtain, or -1 if it is impossible.
Example 1:
Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0]
Output: 12
Explanation: We can replace 0's in the following way:
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
Example 2:
Input: nums1 = [2,0,2,0], nums2 = [1,4]
Output: -1
Explanation: It is impossible to make the sum of both arrays equal.
Constraints:
* 1 <= nums1.length, nums2.length <= 105
* 0 <= nums1[i], nums2[i] <= 106
Please complete the code below to solve above prblem:
```python
class Solution:
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums1": [3,2,0,1,0], "nums2": [6,5,0] }
assert my_solution.minSum(**test_input) == 12
test_input = { "nums1": [2,0,2,0], "nums2": [1,4] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,7,28,17,18], "nums2": [1,2,6,26,1,0,27,3,0,30] }
assert my_solution.minSum(**test_input) == 98
test_input = { "nums1": [8,13,15,18,0,18,0,0,5,20,12,27,3,14,22,0], "nums2": [29,1,6,0,10,24,27,17,14,13,2,19,2,11] }
assert my_solution.minSum(**test_input) == 179
test_input = { "nums1": [9,5], "nums2": [15,12,5,21,4,26,27,9,6,29,0,18,16,0,0,0,20] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,29,5,22,5,9,30,11,20,0,18,16,26,11,3,0,24,24,14,24], "nums2": [30,12,16,3,24,6,13,0,16] }
assert my_solution.minSum(**test_input) == 294
test_input = { "nums1": [9,13,0,0,12,10,0,8,0,0,5,13,0], "nums2": [8,14,11,2,27,0,0] }
assert my_solution.minSum(**test_input) == 76
test_input = { "nums1": [3,0,20,9,20,0,20,25,26,9,0,12,6,11,0,6], "nums2": [0,3,8,13,27,0,0,0,29,27,0,11,23,0,19,19,0] }
assert my_solution.minSum(**test_input) == 186
test_input = { "nums1": [25,28,13,0,14,23,14,0,3,3,12], "nums2": [24,30,0,15,20,19,18,0,23,23,0,16,26,0,29,19,16,25] }
assert my_solution.minSum(**test_input) == 307
test_input = { "nums1": [0,29,30,18,5,24,16,5,17,0,18,16,26,0,15,19,14,20,3,26], "nums2": [0,8,14,11,13,6,8,0,13] }
assert my_solution.minSum(**test_input) == 304
test_input = { "nums1": [0,17,20,17,5,0,14,19,7,8,16,18,6], "nums2": [21,1,27,19,2,2,24,21,16,1,13,27,8,5,3,11,13,7,29,7] }
assert my_solution.minSum(**test_input) == 257
test_input = { "nums1": [26,1,25,10,14,14,4,0,10,0,23], "nums2": [23,8,30,18,8,15,6,9,0,2,0,0,19,8,19,4,10] }
assert my_solution.minSum(**test_input) == 182
test_input = { "nums1": [15,10,7,16], "nums2": [8,16,2,6,4,12,6,16,24,0] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,0,0,17,0,6,2,22,12,0,25,18,1,12,19,0,0], "nums2": [0,0,0,30,4,3,13,25,9,25,3,0,1,12,2,10,4,7,30,16] }
assert my_solution.minSum(**test_input) == 198
test_input = { "nums1": [23,17], "nums2": [7,3,22,0,12] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [15,0,8,30,6,3,24,6,0,11,13,30,6,25,23,3], "nums2": [12,20,0,6,0,0,14,0,0,8,5,19,16,0,0,15] }
assert my_solution.minSum(**test_input) == 205
test_input = { "nums1": [3,25,1,13], "nums2": [19,13,10,27,10,20,27,0,3,12,16,26,0,27] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,0], "nums2": [29,28] }
assert my_solution.minSum(**test_input) == 57
test_input = { "nums1": [17,4,11,8,0,17,0,0,12,27,20,28,0,30,21,18,12], "nums2": [0,2,30,0,5,17,0,0,0,15,11,2,25,18,18] }
assert my_solution.minSum(**test_input) == 229
test_input = { "nums1": [0,17,0,7,29,10,22,27,13,8,19], "nums2": [26,23,8,14,0,17,20,4,26,15,0,9,14,0,12,10,23,16] }
assert my_solution.minSum(**test_input) == 240
test_input = { "nums1": [0,25,8,0,22,0], "nums2": [6,12,22,3,0,28,19,0,20,21,2,3] }
assert my_solution.minSum(**test_input) == 138
test_input = { "nums1": [2,17,23,16,2,0,6,12,10], "nums2": [19,11,7,16,0] }
assert my_solution.minSum(**test_input) == 89
test_input = { "nums1": [4,7,14,15,18,7,0,6,8], "nums2": [27,2,13,18,20,23,9,0,0,25,5,0,17,0,0,14] }
assert my_solution.minSum(**test_input) == 178
test_input = { "nums1": [16,0,7,19,0,0,7,26,12,0,4,0,7,0,22,12,0,26], "nums2": [7,25,0,25,18,0,6,14,0] }
assert my_solution.minSum(**test_input) == 165
test_input = { "nums1": [7,0,3,6,5,24,0,0,0,30,20,13,0,5,19,4,25,17], "nums2": [11,19,28,25,27,6,0,18,0,19,18,16,0,16,9,0,2,23,23,10] }
assert my_solution.minSum(**test_input) == 274
test_input = { "nums1": [23,19,24,0,8,19,30,0,14], "nums2": [25,17,18,6,30] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,15,8,0,4,0,21,20,0,0,18], "nums2": [16,0] }
assert my_solution.minSum(**test_input) == 91
test_input = { "nums1": [0,0,24,21,16,4,22,21], "nums2": [18,7,28,0,0,11,2,0] }
assert my_solution.minSum(**test_input) == 110
test_input = { "nums1": [4,11,17,30,11,21,21,10,2,10,7,29,21,1,0,9,15,5], "nums2": [0,0,1,7,8,0,27,20] }
assert my_solution.minSum(**test_input) == 225
test_input = { "nums1": [12,16,1], "nums2": [1,0,19,24,21,0,0,24,24,18,26,19,13,14,30,9,0,4,20] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,0,8,0], "nums2": [23,0,15,29,25] }
assert my_solution.minSum(**test_input) == 93
test_input = { "nums1": [28,25,14,10], "nums2": [0,6,16,2,0,13,0,0,4,2,16,6,18,0,8,14,10] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,0,8,30,1,0,22,0,0,1,0], "nums2": [22,6,0,13,0,23,14,27,20,4,0,11,11,25,9,22,11,17,17] }
assert my_solution.minSum(**test_input) == 255
test_input = { "nums1": [0,0,27], "nums2": [18,0,0,7,26,11,28,20,1,19] }
assert my_solution.minSum(**test_input) == 132
test_input = { "nums1": [30,6,0,8,14,0,15,0,11,13,0,8,28,8,8,0,28,0,25], "nums2": [21,8,0,10,28,2,6,3,0,20,1,2,24,12,29] }
assert my_solution.minSum(**test_input) == 208
test_input = { "nums1": [18,27,20,10,28,12,29,5,24,0,27,9,22,0,14,0,5,11], "nums2": [24,0,14,26,1,9,24,0,12,30,13,21] }
assert my_solution.minSum(**test_input) == 264
test_input = { "nums1": [0,5], "nums2": [17,12,5,6,0,13,19,7] }
assert my_solution.minSum(**test_input) == 80
test_input = { "nums1": [30,2,20,30], "nums2": [8,14,0,2,0,18,9,24,0,0,28,0,1,14,27] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,17,0,1,19,0,0,7,23,0,0,0,5,0], "nums2": [7,1,28,8,1,0,0,5,5,18,17,23] }
assert my_solution.minSum(**test_input) == 115
test_input = { "nums1": [17,4,26,28,0,0,1,0,24,5,4,6,10,8,8,16,27], "nums2": [0,18,12,0,0,22,15] }
assert my_solution.minSum(**test_input) == 187
test_input = { "nums1": [17,1,13,12,3,13], "nums2": [2,25] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [19,19,21,6,0,0,28,3], "nums2": [5,0,16] }
assert my_solution.minSum(**test_input) == 98
test_input = { "nums1": [27,17,9,23,21,18,8,27,19,3,0,0,0,0,19], "nums2": [4,7,27,21,27,0,28,0,28,19,20,14,0,12,24,3] }
assert my_solution.minSum(**test_input) == 237
test_input = { "nums1": [25,25,0,5,15,13,26,5,25,23,19,20,1,15], "nums2": [23,6] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [9,0,15,10,18,30,7,0,1,27,24,2,8,0], "nums2": [0,0,1,13,0,9,21,3,0,20] }
assert my_solution.minSum(**test_input) == 154
test_input = { "nums1": [23,7,0,4,21,20,18,3,17,18,4,0,0,13,29,17], "nums2": [0,0,18,9,0,11,21,0] }
assert my_solution.minSum(**test_input) == 197
test_input = { "nums1": [7,0,5,7,19,12,0,11,7,24,22], "nums2": [8,3,0,23,19,24,2,10,4,20,0,14] }
assert my_solution.minSum(**test_input) == 129
test_input = { "nums1": [15,23,12,0,0,1,29,24,0,5,21,9,7,6,27,11,0,19,20], "nums2": [14,25,7,18] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [26,14,0], "nums2": [0,16,0,8,14,7,0,2,0,0,10,10,7,14,0,18,11] }
assert my_solution.minSum(**test_input) == 123
test_input = { "nums1": [16,15,27,20,29], "nums2": [27,19,0,11,2,19,28,16,0,16,24,11,0,4,2,24,8] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [26,0,28,0,28,24,15,30,9,17,0,1,21,26,21,8,0,28,0,11], "nums2": [0,20,9,12,10,16,2,21,12,0,26,11,0,21,0,0,0,29,23,22] }
assert my_solution.minSum(**test_input) == 298
test_input = { "nums1": [0,0,9,22,3,14,13,26,21], "nums2": [21,9,21,28,17,6,15,11,5,14,17,22,0,24] }
assert my_solution.minSum(**test_input) == 211
test_input = { "nums1": [0,0,18,27,7,20,9,10,29], "nums2": [29,26,19,0,0,0,0,8,24] }
assert my_solution.minSum(**test_input) == 122
test_input = { "nums1": [5,0,0,0,27,22,0,0,1,9], "nums2": [20,22,5,0,0,24,22,27,15] }
assert my_solution.minSum(**test_input) == 137
test_input = { "nums1": [2,3,0,0,0,1,18,14,25,1,0,0,3,1,13,29,0,11,0,0], "nums2": [0,12,5,14,2,0,0,14,1,10,5,17,17,8,0,0,9] }
assert my_solution.minSum(**test_input) == 129
test_input = { "nums1": [22,0,16,16,27,21,13,9,15,28,0,7,21,8,28,27,26,4], "nums2": [0,16,23,0,26,4,0,13,19,0,0,0,14,18,5,14,20,0,27] }
assert my_solution.minSum(**test_input) == 290
test_input = { "nums1": [26,0,26,18,25,20,20,3,0,14,13,5,13,0,20], "nums2": [16,17,0,12,2,26,14,0,27,17,14,10,0,0,28,29,8,25,3,7] }
assert my_solution.minSum(**test_input) == 259
test_input = { "nums1": [0,25,27,23], "nums2": [28,0,12,0,24,4,14,5,16,30,26,15,6,9,28,0] }
assert my_solution.minSum(**test_input) == 220
test_input = { "nums1": [3,23,15,19,0,7,24,27,25,0,0,16,28,15], "nums2": [26,3,21,0,26,0,9,12,0,0,21,28,23,0,0,4,16,9,7] }
assert my_solution.minSum(**test_input) == 211
test_input = { "nums1": [0,2,0,11,22,0,26,0,1,0,6,0,24,2,24,19,15,12], "nums2": [9,14,0,25,24,29,17,16,24,26,1,28,27,4,11,5,14] }
assert my_solution.minSum(**test_input) == 275
test_input = { "nums1": [1,1,1], "nums2": [18] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [29,15,0,0,0,0,1,0,9,0,0], "nums2": [16,0,30,28,23,0,0,0,0,24,14,27,0,0] }
assert my_solution.minSum(**test_input) == 169
test_input = { "nums1": [27,10,0,13,25], "nums2": [24,10,18,27,3,0,23,6,6] }
assert my_solution.minSum(**test_input) == 118
test_input = { "nums1": [5,0,10,0,4,0,13,0,27,20,12,10,15,29], "nums2": [13,25,4,0,11,0,30,0,29,17,7,28,23] }
assert my_solution.minSum(**test_input) == 190
test_input = { "nums1": [29,6,30,0,25,8,12,0,25,7,2,15,12,1,5,0,0,12], "nums2": [12,14,13,0,0,24,25,22,0,5,8,28,23,6,20,3] }
assert my_solution.minSum(**test_input) == 206
test_input = { "nums1": [9,11,20,0,0,0,21,25,0,0,0,3,11,5,18], "nums2": [0,27,27,21,28,25,3,0,25,0,21,0,3,0,30,20,17] }
assert my_solution.minSum(**test_input) == 252
test_input = { "nums1": [0,0,10,4], "nums2": [9,0,22,9,22,3,16,3,9,19,0,29,3,1,0,1,8,12] }
assert my_solution.minSum(**test_input) == 169
test_input = { "nums1": [20,10,0,16,18,0,16,21,22,4,0,15,0,8], "nums2": [0,1,2,0,20] }
assert my_solution.minSum(**test_input) == 154
test_input = { "nums1": [23,24,0], "nums2": [0,0,26,27,12,18,0] }
assert my_solution.minSum(**test_input) == 86
test_input = { "nums1": [0,17,27,12,0,22], "nums2": [20,0] }
assert my_solution.minSum(**test_input) == 80
test_input = { "nums1": [1,29], "nums2": [20,0,8,11,13,17,0,18,0,2,5,3,27,11,7,17] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [19,29,0,0,1,0,0,0,0,24,18,0,24,0,11,14,16,18], "nums2": [2,0,26,8,17] }
assert my_solution.minSum(**test_input) == 182
test_input = { "nums1": [2,0,0,19,6,29], "nums2": [25,4,0,11,0,13,28,0,28,7,4,2,16,0,22] }
assert my_solution.minSum(**test_input) == 164
test_input = { "nums1": [0,0], "nums2": [26,5,7,0,1,3,0,7,0,0,5,25,26,20,0,3,20,23,18] }
assert my_solution.minSum(**test_input) == 194
test_input = { "nums1": [21,2,0,0,12,2,0,4,6,29,15,0], "nums2": [12,20,3,10,16,25,17,8,27,0,0,23,2,0,2,4,10,27] }
assert my_solution.minSum(**test_input) == 209
test_input = { "nums1": [25,29,10,12,25,26,19,6,19,10,18], "nums2": [0,0,22,2,17,0,7,23,22,18,20,0,13,22,0,0,0,13,6,8] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,0,16,23,28,20,0,22,4,1,0,0,19,0,0,3,2,28], "nums2": [20,28] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [16,14,0,11,9,7,18,2,24,0,0,11,0,0,23], "nums2": [0,0,0,7,0,24,24,6,0,0,12,18,1,0,0] }
assert my_solution.minSum(**test_input) == 140
test_input = { "nums1": [4,15,7,10,8,11,2,0,0,22,11,0,4,14,0,16,29,0,0,27], "nums2": [13,23,8,16] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [7], "nums2": [6,26,25,0,14,19,0,29,16,29,5,26,29,6,0,25,12,0,19,19] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,10,5,16,13,20,20,0,15,4,0,4,3,0,0,15,0,24,0], "nums2": [16,14,27,0,20,0,23,0,5,10,28,21,9,28,21,8,28,0,27,0] }
assert my_solution.minSum(**test_input) == 290
test_input = { "nums1": [12,14,25,12,3], "nums2": [3,26,0,21,22] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,1,6,20,13,9,28,30,0,14,6,0,25,25,24,16,2,21], "nums2": [5,3,0] }
assert my_solution.minSum(**test_input) == 243
test_input = { "nums1": [0,12,18,0,2,12,29,0,20,29,26,14], "nums2": [1,0,29,14,24] }
assert my_solution.minSum(**test_input) == 165
test_input = { "nums1": [0,21,18,13,9,0,10], "nums2": [0,22,27,1,0,0,23,23] }
assert my_solution.minSum(**test_input) == 99
test_input = { "nums1": [28,16,0,0,0,0,0,26,3,0,3,7,5,0,19,27,1,7], "nums2": [28,9,0,16,14] }
assert my_solution.minSum(**test_input) == 149
test_input = { "nums1": [9,17,6,0,24,18,14,10,14,10,0,0,12,0,3,28,25,5,0,30], "nums2": [13,11,30,30,17,27,0,24,15,0,0] }
assert my_solution.minSum(**test_input) == 230
test_input = { "nums1": [26,2,0,0,13,14,18,17,0], "nums2": [27,30,26,14,10,24,17,2,10,25,27] }
assert my_solution.minSum(**test_input) == 212
test_input = { "nums1": [9,0,26], "nums2": [0,16] }
assert my_solution.minSum(**test_input) == 36
test_input = { "nums1": [4,17,6,0,1,8,19,30,21,11,26,0,0,19,0,12], "nums2": [29,17,7,4,29,5,0,25,11,6,0,0,13,22] }
assert my_solution.minSum(**test_input) == 178
test_input = { "nums1": [1], "nums2": [10,29,23,4,0,6,23,23,0,8,29,16,7,20,15,23] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [0,20,12,15,0,1,5,4,16,13,8,8,0,28,2,9,0,12], "nums2": [13,21,9,0,11,26,0,16,0,29,7,0,0,7,4,0,28,0,2] }
assert my_solution.minSum(**test_input) == 180
test_input = { "nums1": [0,20,0,0,8,29,17,25,4,0,0,0,0,7,13,14], "nums2": [6,21,24,14,20,19,0,0,7,21,0,11,0,0,0,0,17,16,0,6] }
assert my_solution.minSum(**test_input) == 190
test_input = { "nums1": [28,25,9,0,10,19,23,21,25,8,24,25,18,5], "nums2": [0,3,0] }
assert my_solution.minSum(**test_input) == 241
test_input = { "nums1": [23,7,15,16,25,9,30,14,8,0,0,2,25,1,7,0,16,0,19], "nums2": [3,1,24,0,25,0,7,24,0,0,17,27,0] }
assert my_solution.minSum(**test_input) == 221
test_input = { "nums1": [0,14,10,29,0,5,13,0,0,1,18,0,0,0,11,3,28,0], "nums2": [30,2,24,0,0,0,14,12,23,3,17,12,14,13,0,28,29,0,21] }
assert my_solution.minSum(**test_input) == 247
test_input = { "nums1": [5,29], "nums2": [23,24] }
assert my_solution.minSum(**test_input) == -1
test_input = { "nums1": [23,2,12,27,0,5,14,0,1,6,30,0,0,2,6,0,11,0], "nums2": [1,26,27,5,0,14,28,24,2,2,15,25,7,13,9] }
assert my_solution.minSum(**test_input) == 199
test_input = { "nums1": [0,10,29,11,11,22,0,0,12,10], "nums2": [14,0,1,3,13,29,21] }
assert my_solution.minSum(**test_input) == 108
test_input = { "nums1": [0,9,22,25,28], "nums2": [0,0,0,14,19,6,0,7,19,15,0,30,19,18,11,1,0,15,10,18] }
assert my_solution.minSum(**test_input) == 208 | 1,698,546,600 |
weekly-contest-369-minimum-increment-operations-to-make-array-beautiful | https://leetcode.com/problems/minimum-increment-operations-to-make-array-beautiful | minimum-increment-operations-to-make-array-beautiful | {
"questionId": "3178",
"questionFrontendId": "2919",
"title": "Minimum Increment Operations to Make Array Beautiful",
"titleSlug": "minimum-increment-operations-to-make-array-beautiful",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 254,
"dislikes": 15,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums having length n, and an integer k.
You can perform the following increment operation any number of times (including zero):
* Choose an index i in the range [0, n - 1], and increase nums[i] by 1.
An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.
Return an integer denoting the minimum number of increment operations needed to make nums beautiful.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [2,3,0,0,2], k = 4
Output: 3
Explanation: We can perform the following increment operations to make nums beautiful:
Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
Hence, the answer is 3.
Example 2:
Input: nums = [0,1,3,3], k = 5
Output: 2
Explanation: We can perform the following increment operations to make nums beautiful:
Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
Hence, the answer is 2.
Example 3:
Input: nums = [1,1,2], k = 1
Output: 0
Explanation: The only subarray with a size of 3 or more in this example is [1,1,2].
The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
Hence, the answer is 0.
Constraints:
* 3 <= n == nums.length <= 105
* 0 <= nums[i] <= 109
* 0 <= k <= 109
"""
class Solution:
def minIncrementOperations(self, nums: List[int], k: int) -> int:
| You are given a 0-indexed integer array nums having length n, and an integer k.
You can perform the following increment operation any number of times (including zero):
* Choose an index i in the range [0, n - 1], and increase nums[i] by 1.
An array is considered beautiful if, for any subarray with a size of 3 or more, its maximum element is greater than or equal to k.
Return an integer denoting the minimum number of increment operations needed to make nums beautiful.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [2,3,0,0,2], k = 4
Output: 3
Explanation: We can perform the following increment operations to make nums beautiful:
Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
Hence, the answer is 3.
Example 2:
Input: nums = [0,1,3,3], k = 5
Output: 2
Explanation: We can perform the following increment operations to make nums beautiful:
Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
Hence, the answer is 2.
Example 3:
Input: nums = [1,1,2], k = 1
Output: 0
Explanation: The only subarray with a size of 3 or more in this example is [1,1,2].
The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
Hence, the answer is 0.
Constraints:
* 3 <= n == nums.length <= 105
* 0 <= nums[i] <= 109
* 0 <= k <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def minIncrementOperations(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,3,0,0,2], "k": 4 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [0,1,3,3], "k": 5 }
assert my_solution.minIncrementOperations(**test_input) == 2
test_input = { "nums": [1,1,2], "k": 1 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [0,5,5], "k": 8 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [0,18,28], "k": 93 }
assert my_solution.minIncrementOperations(**test_input) == 65
test_input = { "nums": [0,24,14], "k": 7 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [2,3,4], "k": 3 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [3,5,9], "k": 6 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [4,3,0], "k": 2 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [5,6,5], "k": 9 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [6,9,6], "k": 3 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [7,9,0], "k": 6 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [7,47,16], "k": 39 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [9,6,1], "k": 6 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [41,44,37], "k": 55 }
assert my_solution.minIncrementOperations(**test_input) == 11
test_input = { "nums": [48,3,13], "k": 1 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [1,2,6,9], "k": 8 }
assert my_solution.minIncrementOperations(**test_input) == 2
test_input = { "nums": [1,3,1,6], "k": 6 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [2,35,41,20], "k": 4 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [3,9,9,7], "k": 6 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [7,7,2,7], "k": 9 }
assert my_solution.minIncrementOperations(**test_input) == 2
test_input = { "nums": [10,2,0,2], "k": 6 }
assert my_solution.minIncrementOperations(**test_input) == 4
test_input = { "nums": [20,2,22,30], "k": 67 }
assert my_solution.minIncrementOperations(**test_input) == 45
test_input = { "nums": [22,49,0,20], "k": 52 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [25,2,1,41], "k": 9 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [31,86,1,2], "k": 354 }
assert my_solution.minIncrementOperations(**test_input) == 268
test_input = { "nums": [43,31,14,4], "k": 73 }
assert my_solution.minIncrementOperations(**test_input) == 42
test_input = { "nums": [44,24,28,47], "k": 16 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [1,9,5,2,0], "k": 2 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [3,7,9,6,0], "k": 7 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [5,9,0,10,3], "k": 3 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [6,2,8,10,6], "k": 9 }
assert my_solution.minIncrementOperations(**test_input) == 1
test_input = { "nums": [6,14,17,4,7], "k": 22 }
assert my_solution.minIncrementOperations(**test_input) == 5
test_input = { "nums": [10,9,5,2,4], "k": 1 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [20,38,29,34,6], "k": 95 }
assert my_solution.minIncrementOperations(**test_input) == 66
test_input = { "nums": [21,45,33,14,22], "k": 58 }
assert my_solution.minIncrementOperations(**test_input) == 25
test_input = { "nums": [32,14,31,43,29], "k": 46 }
assert my_solution.minIncrementOperations(**test_input) == 15
test_input = { "nums": [39,21,10,46,40], "k": 81 }
assert my_solution.minIncrementOperations(**test_input) == 71
test_input = { "nums": [42,7,32,19,4], "k": 66 }
assert my_solution.minIncrementOperations(**test_input) == 34
test_input = { "nums": [74,91,93,96,12], "k": 964 }
assert my_solution.minIncrementOperations(**test_input) == 871
test_input = { "nums": [84,17,58,61,72], "k": 432 }
assert my_solution.minIncrementOperations(**test_input) == 374
test_input = { "nums": [4,0,10,2,10,6], "k": 8 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [4,0,22,41,29,28], "k": 30 }
assert my_solution.minIncrementOperations(**test_input) == 8
test_input = { "nums": [4,1,8,0,3,9], "k": 2 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [4,7,6,9,2,6], "k": 1 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [5,1,3,9,8,8], "k": 4 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [6,5,8,6,0,9], "k": 9 }
assert my_solution.minIncrementOperations(**test_input) == 1
test_input = { "nums": [7,4,10,2,0,8], "k": 7 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [8,10,7,1,9,6], "k": 1 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [9,5,0,10,9,0], "k": 8 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [13,34,0,13,9,19], "k": 82 }
assert my_solution.minIncrementOperations(**test_input) == 117
test_input = { "nums": [16,50,23,35,38,13], "k": 34 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [20,1,40,48,32,24], "k": 38 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [28,5,10,26,38,6], "k": 17 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [30,42,46,45,23,31], "k": 13 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [3,8,0,6,46,24,31], "k": 30 }
assert my_solution.minIncrementOperations(**test_input) == 22
test_input = { "nums": [4,7,2,10,4,10,5], "k": 10 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [8,10,1,5,8,9,7], "k": 4 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [10,7,2,5,9,6,3], "k": 2 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [10,24,21,42,6,10,43], "k": 55 }
assert my_solution.minIncrementOperations(**test_input) == 56
test_input = { "nums": [18,48,1,19,43,25,49], "k": 21 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [37,82,95,82,77,87,93], "k": 239 }
assert my_solution.minIncrementOperations(**test_input) == 296
test_input = { "nums": [40,17,28,38,41,32,9], "k": 97 }
assert my_solution.minIncrementOperations(**test_input) == 125
test_input = { "nums": [41,22,4,41,4,47,36], "k": 25 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [42,19,52,36,8,5,9], "k": 656 }
assert my_solution.minIncrementOperations(**test_input) == 1252
test_input = { "nums": [45,58,6,16,70,69,87], "k": 26 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [48,24,41,16,4,20,38], "k": 97 }
assert my_solution.minIncrementOperations(**test_input) == 133
test_input = { "nums": [50,3,17,36,16,10,2], "k": 19 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [57,41,90,16,41,25,2], "k": 934 }
assert my_solution.minIncrementOperations(**test_input) == 1737
test_input = { "nums": [74,33,13,74,75,95,11], "k": 426 }
assert my_solution.minIncrementOperations(**test_input) == 744
test_input = { "nums": [83,54,75,22,32,59,30], "k": 298 }
assert my_solution.minIncrementOperations(**test_input) == 462
test_input = { "nums": [1,9,3,6,3,1,10,4], "k": 2 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [1,29,18,3,38,4,7,47], "k": 67 }
assert my_solution.minIncrementOperations(**test_input) == 87
test_input = { "nums": [2,1,1,7,2,3,5,6], "k": 9 }
assert my_solution.minIncrementOperations(**test_input) == 13
test_input = { "nums": [2,3,6,3,0,0,7,4], "k": 8 }
assert my_solution.minIncrementOperations(**test_input) == 8
test_input = { "nums": [2,9,6,9,1,9,4,0], "k": 6 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [5,1,6,9,5,4,9,2], "k": 10 }
assert my_solution.minIncrementOperations(**test_input) == 6
test_input = { "nums": [5,10,8,7,6,7,1,10], "k": 3 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [6,7,9,5,0,2,7,7], "k": 1 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [7,2,2,6,7,5,0,2], "k": 3 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [7,12,36,8,27,48,39,35], "k": 36 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [15,47,34,46,42,26,23,11], "k": 15 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [19,40,43,32,15,7,43,5], "k": 85 }
assert my_solution.minIncrementOperations(**test_input) == 120
test_input = { "nums": [22,45,6,7,7,23,6,3], "k": 43 }
assert my_solution.minIncrementOperations(**test_input) == 56
test_input = { "nums": [25,1,70,71,54,96,46,77], "k": 549 }
assert my_solution.minIncrementOperations(**test_input) == 932
test_input = { "nums": [33,41,14,18,43,20,49,23], "k": 25 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [39,26,16,36,19,5,6,28], "k": 98 }
assert my_solution.minIncrementOperations(**test_input) == 175
test_input = { "nums": [44,44,31,36,1,8,39,46], "k": 45 }
assert my_solution.minIncrementOperations(**test_input) == 16
test_input = { "nums": [2,17,43,26,33,12,37,28,34], "k": 3 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [3,1,6,1,0,5,4,5,7], "k": 10 }
assert my_solution.minIncrementOperations(**test_input) == 12
test_input = { "nums": [3,10,4,2,9,8,2,1,4], "k": 7 }
assert my_solution.minIncrementOperations(**test_input) == 3
test_input = { "nums": [7,5,4,7,6,5,10,8,8], "k": 8 }
assert my_solution.minIncrementOperations(**test_input) == 2
test_input = { "nums": [7,40,36,45,42,23,10,33,17], "k": 25 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [10,4,7,4,8,7,8,4,6], "k": 3 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [12,32,12,13,18,38,21,15,13], "k": 95 }
assert my_solution.minIncrementOperations(**test_input) == 214
test_input = { "nums": [17,50,14,27,10,37,24,35,23], "k": 45 }
assert my_solution.minIncrementOperations(**test_input) == 36
test_input = { "nums": [20,17,29,44,18,20,17,26,2], "k": 1 }
assert my_solution.minIncrementOperations(**test_input) == 0
test_input = { "nums": [28,4,38,38,37,13,47,48,49], "k": 61 }
assert my_solution.minIncrementOperations(**test_input) == 60
test_input = { "nums": [29,0,34,5,5,24,43,23,27], "k": 98 }
assert my_solution.minIncrementOperations(**test_input) == 193
test_input = { "nums": [41,53,77,44,79,66,2,46,64], "k": 204 }
assert my_solution.minIncrementOperations(**test_input) == 405 | 1,698,546,600 |
weekly-contest-369-maximum-points-after-collecting-coins-from-all-nodes | https://leetcode.com/problems/maximum-points-after-collecting-coins-from-all-nodes | maximum-points-after-collecting-coins-from-all-nodes | {
"questionId": "3179",
"questionFrontendId": "2920",
"title": "Maximum Points After Collecting Coins From All Nodes",
"titleSlug": "maximum-points-after-collecting-coins-from-all-nodes",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 161,
"dislikes": 11,
"categoryTitle": "Algorithms"
} | """
There exists an undirected tree rooted at node 0 with n nodes labeled from 0 to n - 1. You are given 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 array coins of size n where coins[i] indicates the number of coins in the vertex i, and an integer k.
Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.
Coins at nodei can be collected in one of the following ways:
* Collect all the coins, but you will get coins[i] - k points. If coins[i] - k is negative then you will lose abs(coins[i] - k) points.
* Collect all the coins, but you will get floor(coins[i] / 2) points. If this way is used, then for all the nodej present in the subtree of nodei, coins[j] will get reduced to floor(coins[j] / 2).
Return the maximum points you can get after collecting the coins from all the tree nodes.
Example 1:
[https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png]
Input: edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
Output: 11
Explanation:
Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
Example 2:
[https://assets.leetcode.com/uploads/2023/09/18/ex2.png]
Input: edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
Output: 16
Explanation:
Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
Constraints:
* n == coins.length
* 2 <= n <= 105
* 0 <= coins[i] <= 104
* edges.length == n - 1
* 0 <= edges[i][0], edges[i][1] < n
* 0 <= k <= 104
"""
class Solution:
def maximumPoints(self, edges: List[List[int]], coins: List[int], k: int) -> int:
| There exists an undirected tree rooted at node 0 with n nodes labeled from 0 to n - 1. You are given 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 array coins of size n where coins[i] indicates the number of coins in the vertex i, and an integer k.
Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.
Coins at nodei can be collected in one of the following ways:
* Collect all the coins, but you will get coins[i] - k points. If coins[i] - k is negative then you will lose abs(coins[i] - k) points.
* Collect all the coins, but you will get floor(coins[i] / 2) points. If this way is used, then for all the nodej present in the subtree of nodei, coins[j] will get reduced to floor(coins[j] / 2).
Return the maximum points you can get after collecting the coins from all the tree nodes.
Example 1:
[https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png]
Input: edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
Output: 11
Explanation:
Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
Example 2:
[https://assets.leetcode.com/uploads/2023/09/18/ex2.png]
Input: edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
Output: 16
Explanation:
Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
Constraints:
* n == coins.length
* 2 <= n <= 105
* 0 <= coins[i] <= 104
* edges.length == n - 1
* 0 <= edges[i][0], edges[i][1] < n
* 0 <= k <= 104
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumPoints(self, edges: List[List[int]], coins: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "edges": [[0,1],[1,2],[2,3]], "coins": [10,10,3,3], "k": 5 }
assert my_solution.maximumPoints(**test_input) == 11
test_input = { "edges": [[0,1],[0,2]], "coins": [8,4,4], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 16
test_input = { "edges": [[0,1],[2,0],[0,3],[4,2]], "coins": [7,5,0,9,3], "k": 4 }
assert my_solution.maximumPoints(**test_input) == 10
test_input = { "edges": [[1,0],[0,2],[1,3]], "coins": [9,3,8,9], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 29
test_input = { "edges": [[0,1],[0,2],[3,2],[0,4]], "coins": [5,6,8,7,4], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[1,0],[2,1],[3,1]], "coins": [8,2,7,1], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 11
test_input = { "edges": [[0,1],[1,2],[0,3]], "coins": [6,1,2,3], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[0,1],[0,2],[0,3],[2,4],[5,4],[6,0],[4,7],[8,5]], "coins": [2,3,10,0,0,2,7,3,9], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 20
test_input = { "edges": [[0,1],[0,2],[1,3],[3,4],[0,5],[6,3],[5,7],[3,8],[9,7]], "coins": [0,5,10,5,6,5,0,2,0,0], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 4
test_input = { "edges": [[0,1],[2,1],[3,0],[3,4],[5,0],[6,4],[7,1],[6,8],[9,5]], "coins": [9,0,9,6,7,6,5,7,1,10], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 14
test_input = { "edges": [[0,1],[2,1],[3,2],[4,0],[5,2],[3,6],[7,2],[8,4],[9,2]], "coins": [9,4,0,8,0,7,8,1,10,9], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 46
test_input = { "edges": [[1,0],[2,1],[3,1],[2,4],[5,4],[6,3],[6,7]], "coins": [9,9,5,5,7,9,6,9], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 10
test_input = { "edges": [[0,1],[2,1],[2,3],[4,0],[5,2],[6,1]], "coins": [1,1,8,6,9,4,1], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 3
test_input = { "edges": [[1,0],[1,2],[0,3]], "coins": [10,2,9,3], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[1,0],[1,2],[1,3],[3,4],[5,3],[4,6],[7,0],[1,8],[9,1]], "coins": [2,10,4,0,1,3,6,10,3,6], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 7
test_input = { "edges": [[1,0],[0,2],[3,2],[4,3],[2,5],[1,6],[7,2]], "coins": [2,8,3,1,9,4,8,6], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 6
test_input = { "edges": [[1,0],[2,0],[3,0]], "coins": [0,0,0,6], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 6
test_input = { "edges": [[1,0],[1,2]], "coins": [7,6,0], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 4
test_input = { "edges": [[0,1],[0,2],[1,3],[2,4],[5,4]], "coins": [4,2,7,7,4,9], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[0,1]], "coins": [10,9], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[0,1],[2,1],[3,1],[2,4],[5,0],[6,1]], "coins": [6,1,8,10,0,4,10], "k": 5 }
assert my_solution.maximumPoints(**test_input) == 13
test_input = { "edges": [[1,0],[2,0],[0,3],[0,4],[5,1],[6,4],[3,7],[5,8]], "coins": [9,0,4,2,0,0,3,1,8], "k": 5 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[0,1],[1,2],[3,2],[4,0]], "coins": [7,5,6,3,6], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[0,1],[1,2],[3,2],[4,2],[5,3],[6,1],[4,7],[7,8],[2,9]], "coins": [4,2,0,8,3,2,7,2,1,6], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 18
test_input = { "edges": [[0,1],[1,2],[1,3],[4,0],[3,5],[6,3],[7,6],[8,0]], "coins": [3,3,4,3,1,3,1,6,3], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 18
test_input = { "edges": [[1,0],[2,1],[2,3],[4,1],[4,5],[2,6]], "coins": [3,10,1,5,10,1,4], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 21
test_input = { "edges": [[0,1],[2,0]], "coins": [7,10,8], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 7
test_input = { "edges": [[1,0],[2,1],[3,1],[4,2],[5,3],[6,0],[7,4]], "coins": [1,1,7,10,5,1,7,8], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 2
test_input = { "edges": [[1,0],[0,2],[3,2]], "coins": [5,2,10,5], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 12
test_input = { "edges": [[1,0],[2,0],[3,2],[2,4],[4,5],[6,2],[5,7],[8,2]], "coins": [4,2,1,4,7,7,2,7,4], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 22
test_input = { "edges": [[1,0]], "coins": [8,1], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 4
test_input = { "edges": [[1,0],[0,2],[3,0],[3,4],[3,5],[6,0],[7,5]], "coins": [3,9,9,9,5,3,2,0], "k": 9 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[0,1],[2,1],[3,2],[4,1],[2,5]], "coins": [2,10,4,6,7,9], "k": 5 }
assert my_solution.maximumPoints(**test_input) == 11
test_input = { "edges": [[1,0],[2,1],[3,2],[3,4],[5,4],[2,6],[7,3]], "coins": [3,3,6,1,10,1,2,5], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 11
test_input = { "edges": [[1,0],[2,0],[0,3],[1,4],[3,5],[0,6],[7,4],[1,8]], "coins": [9,7,9,0,3,6,9,4,0], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 13
test_input = { "edges": [[0,1],[2,1],[3,0],[2,4],[1,5],[6,1],[7,3],[5,8]], "coins": [4,9,7,6,6,9,0,2,6], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 34
test_input = { "edges": [[1,0],[1,2]], "coins": [4,4,6], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[0,1],[0,2],[3,1],[2,4],[4,5],[6,2],[4,7],[4,8],[9,1]], "coins": [8,6,10,9,3,10,3,7,9,1], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 21
test_input = { "edges": [[1,0],[2,1],[0,3],[0,4],[5,3],[6,1],[7,5],[8,2],[9,3]], "coins": [1,3,10,0,7,2,8,10,0,5], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 46
test_input = { "edges": [[0,1],[2,0]], "coins": [9,4,2], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 15
test_input = { "edges": [[0,1],[2,1]], "coins": [0,9,3], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 4
test_input = { "edges": [[1,0],[2,1],[3,2],[4,1],[3,5]], "coins": [10,10,8,6,0,0], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 30
test_input = { "edges": [[0,1],[2,0],[3,0],[3,4],[5,1],[6,1],[7,2],[8,3],[0,9]], "coins": [6,4,5,2,1,10,10,9,8,10], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 55
test_input = { "edges": [[0,1],[2,1],[3,2],[0,4],[1,5],[6,5],[7,1],[5,8],[1,9]], "coins": [8,0,9,5,9,6,2,8,1,8], "k": 9 }
assert my_solution.maximumPoints(**test_input) == 10
test_input = { "edges": [[0,1],[2,1],[2,3],[1,4],[3,5],[6,4],[7,6]], "coins": [8,2,3,10,4,5,8,8], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 40
test_input = { "edges": [[0,1],[2,0],[0,3],[4,1],[5,2],[6,1],[7,1]], "coins": [3,9,4,4,3,4,10,4], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 25
test_input = { "edges": [[1,0],[1,2],[3,0],[4,1],[5,1],[1,6],[1,7]], "coins": [7,4,7,2,5,8,0,7], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[0,1],[2,1],[3,1],[4,2],[5,4],[6,5],[7,5]], "coins": [4,5,7,5,0,4,6,7], "k": 4 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[1,0],[1,2],[1,3]], "coins": [8,4,10,7], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 25
test_input = { "edges": [[0,1],[2,1],[2,3],[3,4],[4,5],[6,3],[3,7],[7,8],[9,8]], "coins": [0,2,1,5,8,2,5,3,7,6], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 0
test_input = { "edges": [[1,0],[1,2],[3,0],[4,3],[1,5],[6,2],[7,3],[4,8],[4,9]], "coins": [6,5,1,8,8,10,5,7,7,1], "k": 5 }
assert my_solution.maximumPoints(**test_input) == 19
test_input = { "edges": [[0,1],[2,0],[2,3],[4,2],[5,0],[3,6],[7,5],[3,8],[9,8]], "coins": [9,6,4,10,4,1,6,1,5,9], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 17
test_input = { "edges": [[0,1],[2,1],[3,1],[0,4],[3,5]], "coins": [1,9,3,4,9,3], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[0,1],[0,2]], "coins": [9,3,9], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 13
test_input = { "edges": [[0,1],[2,0],[3,1],[2,4],[5,2],[6,5],[7,3],[8,5],[9,5]], "coins": [4,1,3,1,6,1,0,0,0,6], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 15
test_input = { "edges": [[0,1]], "coins": [1,7], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 1
test_input = { "edges": [[0,1]], "coins": [10,6], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 6
test_input = { "edges": [[1,0],[1,2],[2,3],[0,4]], "coins": [6,7,8,1,9], "k": 9 }
assert my_solution.maximumPoints(**test_input) == 7
test_input = { "edges": [[0,1]], "coins": [6,6], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 12
test_input = { "edges": [[0,1],[0,2],[0,3],[1,4],[5,4],[2,6]], "coins": [9,3,7,2,3,1,2], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 6
test_input = { "edges": [[1,0],[0,2],[3,1],[3,4],[2,5]], "coins": [4,0,3,10,5,8], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 12
test_input = { "edges": [[0,1],[2,1],[3,2],[4,1],[4,5],[5,6]], "coins": [3,9,2,6,1,9,1], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 3
test_input = { "edges": [[1,0]], "coins": [8,8], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 6
test_input = { "edges": [[0,1],[1,2],[1,3],[0,4],[5,2]], "coins": [2,3,7,9,7,7], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 23
test_input = { "edges": [[0,1],[2,0],[3,1],[4,0],[3,5],[2,6]], "coins": [6,9,7,7,7,9,7], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 11
test_input = { "edges": [[1,0],[2,1],[3,0],[0,4],[5,2],[0,6]], "coins": [9,4,7,9,6,2,9], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 13
test_input = { "edges": [[0,1],[1,2],[3,2],[4,2],[0,5],[6,4],[7,3]], "coins": [5,5,6,3,0,8,5,7], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[1,0],[2,0],[2,3],[4,0],[5,2],[2,6],[7,3]], "coins": [8,3,4,5,6,1,6,9], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 11
test_input = { "edges": [[0,1],[2,0],[0,3],[4,0],[5,0],[6,1],[7,3],[8,1]], "coins": [1,6,3,10,1,9,7,8,7], "k": 5 }
assert my_solution.maximumPoints(**test_input) == 17
test_input = { "edges": [[0,1],[2,0],[3,1],[3,4],[2,5],[6,4],[5,7],[5,8],[6,9]], "coins": [3,6,5,6,6,9,5,5,3,10], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 4
test_input = { "edges": [[0,1],[2,0],[3,2],[4,1],[4,5],[6,3],[7,2],[8,2],[3,9]], "coins": [2,2,0,0,4,8,8,5,0,10], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 1
test_input = { "edges": [[1,0],[0,2],[3,0],[4,3],[4,5],[6,5],[7,5]], "coins": [8,5,7,3,2,3,5,3], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[0,1],[2,0]], "coins": [4,3,8], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 7
test_input = { "edges": [[1,0],[2,1],[3,1],[4,2],[5,0],[4,6],[7,3],[8,2]], "coins": [8,9,0,3,9,7,4,8,7], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 12
test_input = { "edges": [[0,1],[2,1]], "coins": [1,6,4], "k": 4 }
assert my_solution.maximumPoints(**test_input) == 1
test_input = { "edges": [[0,1],[2,1],[3,1]], "coins": [8,9,1,0], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 11
test_input = { "edges": [[1,0],[2,1],[0,3],[4,3],[5,4],[4,6]], "coins": [4,1,4,4,0,5,5], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 3
test_input = { "edges": [[1,0],[2,1],[3,2],[3,4],[4,5],[6,5],[7,3]], "coins": [10,9,6,8,9,9,0,7], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 58
test_input = { "edges": [[0,1],[1,2],[3,1],[2,4],[3,5],[4,6],[5,7],[1,8],[2,9]], "coins": [9,5,7,6,2,5,0,7,5,7], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 8
test_input = { "edges": [[0,1],[1,2],[3,1]], "coins": [4,10,2,2], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 4
test_input = { "edges": [[1,0],[1,2],[1,3],[2,4],[3,5],[1,6],[7,4],[8,1],[0,9]], "coins": [6,7,1,2,3,7,3,4,8,4], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 35
test_input = { "edges": [[0,1],[2,1]], "coins": [4,8,10], "k": 6 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[0,1],[0,2],[2,3],[2,4],[5,3],[3,6],[7,2],[8,5]], "coins": [7,8,4,3,4,8,10,8,1], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 12
test_input = { "edges": [[1,0],[2,1],[3,0],[4,2],[0,5]], "coins": [9,9,3,3,4,4], "k": 4 }
assert my_solution.maximumPoints(**test_input) == 15
test_input = { "edges": [[0,1],[0,2],[1,3],[2,4],[5,0]], "coins": [7,5,0,10,0,0], "k": 8 }
assert my_solution.maximumPoints(**test_input) == 5
test_input = { "edges": [[1,0],[2,1],[2,3],[4,2],[0,5],[6,2],[4,7],[8,5],[0,9]], "coins": [5,2,8,8,6,0,3,2,2,5], "k": 3 }
assert my_solution.maximumPoints(**test_input) == 18
test_input = { "edges": [[1,0],[1,2],[3,0],[4,3],[1,5],[6,1],[7,4],[2,8]], "coins": [5,5,2,1,3,8,6,4,3], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 28
test_input = { "edges": [[1,0],[0,2]], "coins": [8,2,5], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 15
test_input = { "edges": [[1,0],[1,2],[3,2]], "coins": [10,9,2,0], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 7
test_input = { "edges": [[0,1],[2,0],[2,3],[0,4],[2,5],[6,4],[7,1],[8,3]], "coins": [10,4,4,8,9,5,5,8,1], "k": 0 }
assert my_solution.maximumPoints(**test_input) == 54
test_input = { "edges": [[0,1],[1,2],[1,3],[2,4],[1,5],[6,2],[3,7],[8,4],[9,3]], "coins": [5,1,1,3,5,0,0,1,1,9], "k": 5 }
assert my_solution.maximumPoints(**test_input) == 2
test_input = { "edges": [[0,1],[1,2],[3,1],[1,4],[5,3]], "coins": [1,7,1,3,3,7], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 1
test_input = { "edges": [[0,1],[1,2],[3,2],[4,3],[5,2],[0,6],[3,7],[5,8]], "coins": [9,1,3,2,1,3,4,2,6], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 22
test_input = { "edges": [[0,1],[0,2]], "coins": [5,1,5], "k": 10 }
assert my_solution.maximumPoints(**test_input) == 3
test_input = { "edges": [[0,1],[0,2],[3,1],[1,4],[4,5]], "coins": [5,7,8,9,3,10], "k": 7 }
assert my_solution.maximumPoints(**test_input) == 9
test_input = { "edges": [[0,1],[0,2],[0,3],[0,4],[0,5],[5,6],[6,7],[8,5]], "coins": [8,3,0,3,4,1,4,0,7], "k": 2 }
assert my_solution.maximumPoints(**test_input) == 16
test_input = { "edges": [[1,0],[0,2],[3,0],[4,3],[2,5],[3,6]], "coins": [3,6,7,1,2,8,0], "k": 9 }
assert my_solution.maximumPoints(**test_input) == 4
test_input = { "edges": [[1,0]], "coins": [4,0], "k": 9 }
assert my_solution.maximumPoints(**test_input) == 2
test_input = { "edges": [[1,0],[2,0],[3,1],[4,1],[5,2],[6,1],[4,7],[5,8]], "coins": [9,7,8,9,6,8,9,1,6], "k": 1 }
assert my_solution.maximumPoints(**test_input) == 54
test_input = { "edges": [[1,0],[0,2],[1,3],[0,4],[5,4],[6,0],[3,7]], "coins": [7,10,8,4,6,0,6,2], "k": 4 }
assert my_solution.maximumPoints(**test_input) == 21 | 1,698,546,600 |
biweekly-contest-116-subarrays-distinct-element-sum-of-squares-i | https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i | subarrays-distinct-element-sum-of-squares-i | {
"questionId": "3163",
"questionFrontendId": "2913",
"title": "Subarrays Distinct Element Sum of Squares I",
"titleSlug": "subarrays-distinct-element-sum-of-squares-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 82,
"dislikes": 9,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums.
The distinct count of a subarray of nums is defined as:
* Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].
Return the sum of the squares of distinct counts of all subarrays of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1]
Output: 15
Explanation: Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.
Example 2:
Input: nums = [1,1]
Output: 3
Explanation: Three possible subarrays are:
[1]: 1 distinct value
[1]: 1 distinct value
[1,1]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
Constraints:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 100
"""
class Solution:
def sumCounts(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums.
The distinct count of a subarray of nums is defined as:
* Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].
Return the sum of the squares of distinct counts of all subarrays of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1]
Output: 15
Explanation: Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.
Example 2:
Input: nums = [1,1]
Output: 3
Explanation: Three possible subarrays are:
[1]: 1 distinct value
[1]: 1 distinct value
[1,1]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
Constraints:
* 1 <= nums.length <= 100
* 1 <= nums[i] <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def sumCounts(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,1] }
assert my_solution.sumCounts(**test_input) == 15
test_input = { "nums": [1,1] }
assert my_solution.sumCounts(**test_input) == 3
test_input = { "nums": [2,2,5,5] }
assert my_solution.sumCounts(**test_input) == 22
test_input = { "nums": [5,2,4,2,1,3,2,4,3,1] }
assert my_solution.sumCounts(**test_input) == 578
test_input = { "nums": [2,3,2,1,2,5,3,4,5,2] }
assert my_solution.sumCounts(**test_input) == 629
test_input = { "nums": [5,1,5,2,3,5,1,5,1] }
assert my_solution.sumCounts(**test_input) == 385
test_input = { "nums": [4,5,4,3,4,2] }
assert my_solution.sumCounts(**test_input) == 120
test_input = { "nums": [2] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [3,4,2,5,2,4,1,2,2,5] }
assert my_solution.sumCounts(**test_input) == 535
test_input = { "nums": [4,4,2,4,1] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [2,2,5] }
assert my_solution.sumCounts(**test_input) == 12
test_input = { "nums": [4,5,1,2,2,1,3,3] }
assert my_solution.sumCounts(**test_input) == 266
test_input = { "nums": [3,1,5,5,2,3,2,2,1] }
assert my_solution.sumCounts(**test_input) == 334
test_input = { "nums": [2,5,2,5,3,2,5,2] }
assert my_solution.sumCounts(**test_input) == 205
test_input = { "nums": [5,4,1,4,5,2,4] }
assert my_solution.sumCounts(**test_input) == 203
test_input = { "nums": [1,3,3,4,3,1,2,1] }
assert my_solution.sumCounts(**test_input) == 253
test_input = { "nums": [4] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [1,4,2,1,5,4,3,1,4] }
assert my_solution.sumCounts(**test_input) == 507
test_input = { "nums": [2,4,5,3,2,5,1,5,4,4] }
assert my_solution.sumCounts(**test_input) == 626
test_input = { "nums": [3,4,1,4,5,2,2] }
assert my_solution.sumCounts(**test_input) == 220
test_input = { "nums": [3,5,1,1,3] }
assert my_solution.sumCounts(**test_input) == 62
test_input = { "nums": [4,3,2,5,3] }
assert my_solution.sumCounts(**test_input) == 89
test_input = { "nums": [2,5] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [1,5,1,4,5] }
assert my_solution.sumCounts(**test_input) == 70
test_input = { "nums": [5,1] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,5,4,3,3,5,3] }
assert my_solution.sumCounts(**test_input) == 138
test_input = { "nums": [5,4,3] }
assert my_solution.sumCounts(**test_input) == 20
test_input = { "nums": [5,5,3,3,4,5,4,5,5] }
assert my_solution.sumCounts(**test_input) == 234
test_input = { "nums": [3,1,5,5,3,4,5,5,1,4] }
assert my_solution.sumCounts(**test_input) == 456
test_input = { "nums": [4,2,3,1,1] }
assert my_solution.sumCounts(**test_input) == 81
test_input = { "nums": [4,5,3,1,2,5,5,3,5] }
assert my_solution.sumCounts(**test_input) == 434
test_input = { "nums": [3,2,1,2,5,2,4,5,1,5] }
assert my_solution.sumCounts(**test_input) == 531
test_input = { "nums": [1,3,1,4,4] }
assert my_solution.sumCounts(**test_input) == 62
test_input = { "nums": [5,1,2,1,2,1,2,3,1] }
assert my_solution.sumCounts(**test_input) == 257
test_input = { "nums": [2,4] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,5,4,5] }
assert my_solution.sumCounts(**test_input) == 28
test_input = { "nums": [3,1,5,5,5,4,3,3,2] }
assert my_solution.sumCounts(**test_input) == 334
test_input = { "nums": [3,2,5,2,1,5,3] }
assert my_solution.sumCounts(**test_input) == 203
test_input = { "nums": [4,4,2,5,5,4,2,2,1] }
assert my_solution.sumCounts(**test_input) == 294
test_input = { "nums": [1] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [1,1,3,3,3,4,4] }
assert my_solution.sumCounts(**test_input) == 96
test_input = { "nums": [3,2,2,3,4] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [1,5,3,2,4,4] }
assert my_solution.sumCounts(**test_input) == 161
test_input = { "nums": [5,4,1,1,3] }
assert my_solution.sumCounts(**test_input) == 69
test_input = { "nums": [4,3,3,5,3,4,5,3,3,1] }
assert my_solution.sumCounts(**test_input) == 376
test_input = { "nums": [2,3,4,1,5,1,3,3,4] }
assert my_solution.sumCounts(**test_input) == 432
test_input = { "nums": [5,1,4,2,1,1] }
assert my_solution.sumCounts(**test_input) == 129
test_input = { "nums": [5,4,4,1] }
assert my_solution.sumCounts(**test_input) == 30
test_input = { "nums": [1,5,1,3,2,1] }
assert my_solution.sumCounts(**test_input) == 139
test_input = { "nums": [5,3] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,1,4,3] }
assert my_solution.sumCounts(**test_input) == 38
test_input = { "nums": [1,5,4,3,4,2,4,5,5,4] }
assert my_solution.sumCounts(**test_input) == 513
test_input = { "nums": [4,2,3,4,3,2,5,4,4] }
assert my_solution.sumCounts(**test_input) == 378
test_input = { "nums": [2,3,3,2,1,5,2,2] }
assert my_solution.sumCounts(**test_input) == 262
test_input = { "nums": [2,1,4,2,4,1,4,3] }
assert my_solution.sumCounts(**test_input) == 243
test_input = { "nums": [1,4,4,1,3] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [2,3,2,1] }
assert my_solution.sumCounts(**test_input) == 38
test_input = { "nums": [1,4,2,1] }
assert my_solution.sumCounts(**test_input) == 43
test_input = { "nums": [2,4,3,2,5,1] }
assert my_solution.sumCounts(**test_input) == 169
test_input = { "nums": [2,5,3,2,1,3,1,3,2] }
assert my_solution.sumCounts(**test_input) == 348
test_input = { "nums": [4,1] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,3,1,4,3,4,3,4,1] }
assert my_solution.sumCounts(**test_input) == 263
test_input = { "nums": [5,1,1,1,4,3] }
assert my_solution.sumCounts(**test_input) == 89
test_input = { "nums": [4,5] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [5,2,2,3,1,2,5,3] }
assert my_solution.sumCounts(**test_input) == 289
test_input = { "nums": [3,2,4] }
assert my_solution.sumCounts(**test_input) == 20
test_input = { "nums": [5,3,5,2,3,2] }
assert my_solution.sumCounts(**test_input) == 106
test_input = { "nums": [3,2,1] }
assert my_solution.sumCounts(**test_input) == 20
test_input = { "nums": [4,4,2,4,3] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [1,4,4] }
assert my_solution.sumCounts(**test_input) == 12
test_input = { "nums": [1,4,4,3,1,2,1,4,3] }
assert my_solution.sumCounts(**test_input) == 387
test_input = { "nums": [1,5,4,2,5,5,5,3] }
assert my_solution.sumCounts(**test_input) == 249
test_input = { "nums": [2,1,5,3] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [2,3,5,1,5,2,3,2,3,4] }
assert my_solution.sumCounts(**test_input) == 533
test_input = { "nums": [5,3,4,4,3,5,4,5] }
assert my_solution.sumCounts(**test_input) == 202
test_input = { "nums": [4,4,2,2,4,1] }
assert my_solution.sumCounts(**test_input) == 80
test_input = { "nums": [2,3] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,2,3,2] }
assert my_solution.sumCounts(**test_input) == 38
test_input = { "nums": [1,2,2] }
assert my_solution.sumCounts(**test_input) == 12
test_input = { "nums": [4,1,5,1,5,4,5,1] }
assert my_solution.sumCounts(**test_input) == 205
test_input = { "nums": [4,5,3,1] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [4,2,3,4,2,4,3,3,2] }
assert my_solution.sumCounts(**test_input) == 275
test_input = { "nums": [4,3] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [1,3,5,4,4,4] }
assert my_solution.sumCounts(**test_input) == 113
test_input = { "nums": [1,2,4,2,1,2,2,4,1,3] }
assert my_solution.sumCounts(**test_input) == 391
test_input = { "nums": [4,2,5,3,2] }
assert my_solution.sumCounts(**test_input) == 89
test_input = { "nums": [3,4,5,3,2,5] }
assert my_solution.sumCounts(**test_input) == 144
test_input = { "nums": [5,4,5] }
assert my_solution.sumCounts(**test_input) == 15
test_input = { "nums": [2,4,5,1] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [5,4,1,4,2,1,5] }
assert my_solution.sumCounts(**test_input) == 203
test_input = { "nums": [2,3,3,2,2,3,1] }
assert my_solution.sumCounts(**test_input) == 110
test_input = { "nums": [1,4,2,5] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [3] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [5] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [1,3,5,3,2,1,1,4,3] }
assert my_solution.sumCounts(**test_input) == 441
test_input = { "nums": [1,5,2,2,3,3,3] }
assert my_solution.sumCounts(**test_input) == 140
test_input = { "nums": [1,2,1,4,5,5,4,1,1,1] }
assert my_solution.sumCounts(**test_input) == 407
test_input = { "nums": [2,2,1,1,1,2,5,4,5] }
assert my_solution.sumCounts(**test_input) == 296
test_input = { "nums": [3,2,3] }
assert my_solution.sumCounts(**test_input) == 15
test_input = { "nums": [2,1,5,4,3,3,2,1,5,5] }
assert my_solution.sumCounts(**test_input) == 652 | 1,698,503,400 |
biweekly-contest-116-minimum-number-of-changes-to-make-binary-string-beautiful | https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful | minimum-number-of-changes-to-make-binary-string-beautiful | {
"questionId": "3174",
"questionFrontendId": "2914",
"title": "Minimum Number of Changes to Make Binary String Beautiful",
"titleSlug": "minimum-number-of-changes-to-make-binary-string-beautiful",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 111,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed binary string s having an even length.
A string is beautiful if it's possible to partition it into one or more substrings such that:
* Each substring has an even length.
* Each substring contains only 1's or only 0's.
You can change any character in s to 0 or 1.
Return the minimum number of changes required to make the string s beautiful.
Example 1:
Input: s = "1001"
Output: 2
Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100".
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
Example 2:
Input: s = "10"
Output: 1
Explanation: We change s[1] to 1 to get string "11".
It can be seen that the string "11" is beautiful because we can partition it into "11".
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
Example 3:
Input: s = "0000"
Output: 0
Explanation: We don't need to make any changes as the string "0000" is beautiful already.
Constraints:
* 2 <= s.length <= 105
* s has an even length.
* s[i] is either '0' or '1'.
"""
class Solution:
def minChanges(self, s: str) -> int:
| You are given a 0-indexed binary string s having an even length.
A string is beautiful if it's possible to partition it into one or more substrings such that:
* Each substring has an even length.
* Each substring contains only 1's or only 0's.
You can change any character in s to 0 or 1.
Return the minimum number of changes required to make the string s beautiful.
Example 1:
Input: s = "1001"
Output: 2
Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100".
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
Example 2:
Input: s = "10"
Output: 1
Explanation: We change s[1] to 1 to get string "11".
It can be seen that the string "11" is beautiful because we can partition it into "11".
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
Example 3:
Input: s = "0000"
Output: 0
Explanation: We don't need to make any changes as the string "0000" is beautiful already.
Constraints:
* 2 <= s.length <= 105
* s has an even length.
* s[i] is either '0' or '1'.
Please complete the code below to solve above prblem:
```python
class Solution:
def minChanges(self, s: str) -> int:
``` |
my_solution = Solution()
test_input = { "s": "1001" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "10" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "0000" }
assert my_solution.minChanges(**test_input) == 0
test_input = { "s": "11000111" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "01010001" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "010010" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "111111111110010001" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "01010000011001001101" }
assert my_solution.minChanges(**test_input) == 6
test_input = { "s": "011011100001110111" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "1001000010111010" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "0011" }
assert my_solution.minChanges(**test_input) == 0
test_input = { "s": "11100100010010" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "110100" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "01" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "10110010" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "0010" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "01000011000111" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "0001110001" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "000000001010100011" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "100001" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "10010010" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "101100" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "000010" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "011011000001100011" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "01101111" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "000000011101" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "011011011111111011" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "110011" }
assert my_solution.minChanges(**test_input) == 0
test_input = { "s": "1111101111101011" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "11000101011001100110" }
assert my_solution.minChanges(**test_input) == 8
test_input = { "s": "101101101110110010" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "11011110" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "1011100101" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "0011000101001000" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "00" }
assert my_solution.minChanges(**test_input) == 0
test_input = { "s": "10110111010001000010" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "0101" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "0010101100" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "11001110101001" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "010101000011011111" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "0010111010000001" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "0000010101" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "10100011101010" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "101111" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "11" }
assert my_solution.minChanges(**test_input) == 0
test_input = { "s": "110110" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "001001110111010101" }
assert my_solution.minChanges(**test_input) == 6
test_input = { "s": "11110011" }
assert my_solution.minChanges(**test_input) == 0
test_input = { "s": "1100000101101110" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "111010" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "100000100010011100" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "0111" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "0100110011111101" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "010111" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "000100" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "00110111000111110001" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "01010010" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "01110101001001" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "00111100010111" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "001110" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "00010101111101" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "111011" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "011110" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "1010" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "011101010011101011" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "1001110001111100" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "011000101100" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "1110111000111111" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "1110" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "10110110" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "1000" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "0111110110" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "011000011101" }
assert my_solution.minChanges(**test_input) == 4
test_input = { "s": "0100101000010101" }
assert my_solution.minChanges(**test_input) == 6
test_input = { "s": "0100001100" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "100010010010110001" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "101001111001111001" }
assert my_solution.minChanges(**test_input) == 7
test_input = { "s": "111001" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "111010001000" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "0110" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "11011010" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "100111" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "11000110000001" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "010111010011011000" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "000101" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "000010101010101011" }
assert my_solution.minChanges(**test_input) == 6
test_input = { "s": "11011000" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "010101" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "10011010110110101100" }
assert my_solution.minChanges(**test_input) == 7
test_input = { "s": "10100011" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "11010100000000" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "101100101111010011" }
assert my_solution.minChanges(**test_input) == 3
test_input = { "s": "110111111100" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "111110110100" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "011110110101010100" }
assert my_solution.minChanges(**test_input) == 6
test_input = { "s": "000111" }
assert my_solution.minChanges(**test_input) == 1
test_input = { "s": "00000010001101" }
assert my_solution.minChanges(**test_input) == 2
test_input = { "s": "01111001101001" }
assert my_solution.minChanges(**test_input) == 6
test_input = { "s": "010110011101" }
assert my_solution.minChanges(**test_input) == 5
test_input = { "s": "0111011101" }
assert my_solution.minChanges(**test_input) == 3 | 1,698,503,400 |
biweekly-contest-116-length-of-the-longest-subsequence-that-sums-to-target | https://leetcode.com/problems/length-of-the-longest-subsequence-that-sums-to-target | length-of-the-longest-subsequence-that-sums-to-target | {
"questionId": "3106",
"questionFrontendId": "2915",
"title": "Length of the Longest Subsequence That Sums to Target",
"titleSlug": "length-of-the-longest-subsequence-that-sums-to-target",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 141,
"dislikes": 19,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array of integers nums, and an integer target.
Return the length of the longest subsequence of nums that sums up to target. If no such subsequence exists, return -1.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [1,2,3,4,5], target = 9
Output: 3
Explanation: There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3.
Example 2:
Input: nums = [4,1,3,2,1,5], target = 7
Output: 4
Explanation: There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4.
Example 3:
Input: nums = [1,1,5,4,5], target = 3
Output: -1
Explanation: It can be shown that nums has no subsequence that sums up to 3.
Constraints:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 1000
* 1 <= target <= 1000
"""
class Solution:
def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
| You are given a 0-indexed array of integers nums, and an integer target.
Return the length of the longest subsequence of nums that sums up to target. If no such subsequence exists, return -1.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [1,2,3,4,5], target = 9
Output: 3
Explanation: There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3.
Example 2:
Input: nums = [4,1,3,2,1,5], target = 7
Output: 4
Explanation: There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4.
Example 3:
Input: nums = [1,1,5,4,5], target = 3
Output: -1
Explanation: It can be shown that nums has no subsequence that sums up to 3.
Constraints:
* 1 <= nums.length <= 1000
* 1 <= nums[i] <= 1000
* 1 <= target <= 1000
Please complete the code below to solve above prblem:
```python
class Solution:
def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3,4,5], "target": 9 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 3
test_input = { "nums": [4,1,3,2,1,5], "target": 7 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 4
test_input = { "nums": [1,1,5,4,5], "target": 3 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == -1
test_input = { "nums": [1000], "target": 12 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == -1
test_input = { "nums": [1000], "target": 1000 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 1
test_input = { "nums": [1,2], "target": 10 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == -1
test_input = { "nums": [1,1000], "target": 5 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == -1
test_input = { "nums": [2,3], "target": 3 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 1
test_input = { "nums": [2,3], "target": 5 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 2
test_input = { "nums": [2,3,5], "target": 5 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 2
test_input = { "nums": [1,3,3,7], "target": 1000 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == -1
test_input = { "nums": [1,3,3,7], "target": 2 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == -1
test_input = { "nums": [1,3,3,8], "target": 7 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 3
test_input = { "nums": [1,1,2,1], "target": 2 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 2
test_input = { "nums": [1,1,1,1], "target": 5 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == -1
test_input = { "nums": [1,1,1,2], "target": 3 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 3
test_input = { "nums": [9,12,8,4,11,13,15,7,5], "target": 84 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [11,5,9,11,12,13,12,5,1,8], "target": 87 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [9,11,11,15,4,14,3,2,13,7], "target": 89 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [11,13,6,13,10], "target": 53 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [10,3,5,11,6,12], "target": 47 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [13,3,6,6,6,15,4], "target": 53 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [1,6,15,6,14,13,14], "target": 69 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [10,7,8,14,15], "target": 54 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [14,15,8,10,8,7], "target": 62 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [7,9,14,14,9,14,5,12,10], "target": 94 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [1,10,6,14,5,13,3,7,10,10], "target": 79 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [5,2,8,6,7,12,13,4,1], "target": 58 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [12,8,2,4,1], "target": 27 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [10,14,11,13,2,11], "target": 61 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [10,2,13,5,7,15], "target": 52 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [3,1,10,1,10,1,2,9,5,13], "target": 55 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [5,13,2,13,9,4,5,7], "target": 58 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [1,15,5,12,13,10,14,8], "target": 78 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [7,4,14,10,13], "target": 48 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [6,14,14,6,2,9,1,4,10], "target": 66 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [14,15,7,5,7,10,6,14,10,11], "target": 99 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [15,13,8,8,6], "target": 50 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [2,6,8,9,13,3], "target": 41 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [13,15,9,3,8,1,9,2,15,5], "target": 80 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [5,13,9,11,6,1], "target": 45 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [7,10,15,7,14,2], "target": 55 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [12,14,13,13,13], "target": 65 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [12,8,7,9,3,10,3,8,2], "target": 62 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [11,1,14,13,14,4,14,11], "target": 82 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [5,9,11,2,5,2,7,11,5,3], "target": 60 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [5,15,3,13,14,15,10], "target": 75 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [10,8,2,2,9], "target": 31 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [7,15,4,3,9,15,12,1,12], "target": 78 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [3,1,12,15,5,10], "target": 46 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [5,3,12,7,5,2,12,10,12,5], "target": 73 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [6,10,3,1,7,11,9,8,13,12], "target": 80 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [11,3,4,11,9], "target": 38 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [15,12,12,13,6,6,4,1], "target": 69 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [9,2,10,7,10,11,14,11,8], "target": 82 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [4,4,3,9,6,8,4,7,7], "target": 52 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [10,14,4,15,9,5], "target": 57 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [4,13,2,3,13,11,8,6], "target": 60 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [1,7,8,14,15,9,8,10,13,7], "target": 92 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [7,7,6,14,7,4], "target": 45 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [9,10,9,7,14,3,6,4,6], "target": 68 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [15,13,14,5,7,13,11,14], "target": 92 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [1,1,10,12,5,6,15,6,8], "target": 64 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [14,13,13,11,14,13,8], "target": 86 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [3,14,4,2,10,3,7], "target": 43 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [6,1,3,11,9,2,10,6,12], "target": 60 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [6,2,5,4,12], "target": 29 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [7,11,15,1,9,9,11], "target": 63 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [7,12,10,15,6,15,14,2], "target": 81 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [12,3,10,12,13,3,4,7,15], "target": 79 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [14,6,11,2,10,1,12,9,2], "target": 67 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [5,8,12,6,15,13,11], "target": 70 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [11,6,1,6,2,6,15], "target": 47 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [12,7,15,10,5,4,7,12,12], "target": 84 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [11,4,4,9,10,7,12], "target": 57 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [4,12,15,6,15,1,4,4,2], "target": 63 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [3,13,4,15,1], "target": 36 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [14,3,7,14,7,7,1,6], "target": 59 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [15,13,1,14,6,8], "target": 57 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [14,2,3,10,15], "target": 44 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [5,5,3,7,12,10,11], "target": 53 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [3,7,3,5,3,14,8], "target": 43 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [5,7,9,14,9,14,4,1,4], "target": 67 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [12,7,8,6,3,9,7,3,4,4], "target": 63 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [9,12,1,4,9,6,15,9,7], "target": 72 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [9,13,12,10,4,9,9,4,4,13], "target": 87 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [13,5,6,8,2,13,1,5,6], "target": 59 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [7,9,8,9,9,3,5], "target": 50 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [15,1,14,8,2,1,10,15,15], "target": 81 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [13,14,1,9,12,2], "target": 51 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [13,12,12,13,8,11,3,14,13], "target": 99 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [2,2,1,12,10,7,11,5,5], "target": 55 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [13,10,3,4,10,3], "target": 43 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 6
test_input = { "nums": [8,9,1,5,8,7,6,8], "target": 52 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [10,1,4,10,9,13,14], "target": 61 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 7
test_input = { "nums": [3,14,11,4,7,9,7,6,8,11], "target": 80 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 10
test_input = { "nums": [4,11,6,6,14,12,2,9,1], "target": 65 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 9
test_input = { "nums": [9,2,15,12,15,6,4,12], "target": 75 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 8
test_input = { "nums": [4,3,5,3,2], "target": 17 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
test_input = { "nums": [4,3,13,6,9], "target": 35 }
assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 | 1,698,503,400 |
biweekly-contest-116-subarrays-distinct-element-sum-of-squares-ii | https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-ii | subarrays-distinct-element-sum-of-squares-ii | {
"questionId": "3139",
"questionFrontendId": "2916",
"title": "Subarrays Distinct Element Sum of Squares II",
"titleSlug": "subarrays-distinct-element-sum-of-squares-ii",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 115,
"dislikes": 6,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums.
The distinct count of a subarray of nums is defined as:
* Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].
Return the sum of the squares of distinct counts of all subarrays of nums.
Since the answer may be very large, return it modulo 109 + 7.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1]
Output: 15
Explanation: Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Three possible subarrays are:
[2]: 1 distinct value
[2]: 1 distinct value
[2,2]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
Constraints:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 105
"""
class Solution:
def sumCounts(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums.
The distinct count of a subarray of nums is defined as:
* Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].
Return the sum of the squares of distinct counts of all subarrays of nums.
Since the answer may be very large, return it modulo 109 + 7.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1]
Output: 15
Explanation: Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Three possible subarrays are:
[2]: 1 distinct value
[2]: 1 distinct value
[2,2]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
Constraints:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 105
Please complete the code below to solve above prblem:
```python
class Solution:
def sumCounts(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,1] }
assert my_solution.sumCounts(**test_input) == 15
test_input = { "nums": [2,2] }
assert my_solution.sumCounts(**test_input) == 3
test_input = { "nums": [2,2,5,5] }
assert my_solution.sumCounts(**test_input) == 22
test_input = { "nums": [5,2,4,2,1,3,2,4,3,1] }
assert my_solution.sumCounts(**test_input) == 578
test_input = { "nums": [2,3,2,1,2,5,3,4,5,2] }
assert my_solution.sumCounts(**test_input) == 629
test_input = { "nums": [5,1,5,2,3,5,1,5,1] }
assert my_solution.sumCounts(**test_input) == 385
test_input = { "nums": [4,5,4,3,4,2] }
assert my_solution.sumCounts(**test_input) == 120
test_input = { "nums": [2] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [3,4,2,5,2,4,1,2,2,5] }
assert my_solution.sumCounts(**test_input) == 535
test_input = { "nums": [4,4,2,4,1] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [2,2,5] }
assert my_solution.sumCounts(**test_input) == 12
test_input = { "nums": [4,5,1,2,2,1,3,3] }
assert my_solution.sumCounts(**test_input) == 266
test_input = { "nums": [3,1,5,5,2,3,2,2,1] }
assert my_solution.sumCounts(**test_input) == 334
test_input = { "nums": [2,5,2,5,3,2,5,2] }
assert my_solution.sumCounts(**test_input) == 205
test_input = { "nums": [5,4,1,4,5,2,4] }
assert my_solution.sumCounts(**test_input) == 203
test_input = { "nums": [1,3,3,4,3,1,2,1] }
assert my_solution.sumCounts(**test_input) == 253
test_input = { "nums": [4] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [1,4,2,1,5,4,3,1,4] }
assert my_solution.sumCounts(**test_input) == 507
test_input = { "nums": [2,4,5,3,2,5,1,5,4,4] }
assert my_solution.sumCounts(**test_input) == 626
test_input = { "nums": [3,4,1,4,5,2,2] }
assert my_solution.sumCounts(**test_input) == 220
test_input = { "nums": [3,5,1,1,3] }
assert my_solution.sumCounts(**test_input) == 62
test_input = { "nums": [4,3,2,5,3] }
assert my_solution.sumCounts(**test_input) == 89
test_input = { "nums": [2,5] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [1,5,1,4,5] }
assert my_solution.sumCounts(**test_input) == 70
test_input = { "nums": [5,1] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,5,4,3,3,5,3] }
assert my_solution.sumCounts(**test_input) == 138
test_input = { "nums": [5,4,3] }
assert my_solution.sumCounts(**test_input) == 20
test_input = { "nums": [5,5,3,3,4,5,4,5,5] }
assert my_solution.sumCounts(**test_input) == 234
test_input = { "nums": [3,1,5,5,3,4,5,5,1,4] }
assert my_solution.sumCounts(**test_input) == 456
test_input = { "nums": [4,2,3,1,1] }
assert my_solution.sumCounts(**test_input) == 81
test_input = { "nums": [4,5,3,1,2,5,5,3,5] }
assert my_solution.sumCounts(**test_input) == 434
test_input = { "nums": [3,2,1,2,5,2,4,5,1,5] }
assert my_solution.sumCounts(**test_input) == 531
test_input = { "nums": [1,3,1,4,4] }
assert my_solution.sumCounts(**test_input) == 62
test_input = { "nums": [5,1,2,1,2,1,2,3,1] }
assert my_solution.sumCounts(**test_input) == 257
test_input = { "nums": [2,4] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,5,4,5] }
assert my_solution.sumCounts(**test_input) == 28
test_input = { "nums": [3,1,5,5,5,4,3,3,2] }
assert my_solution.sumCounts(**test_input) == 334
test_input = { "nums": [3,2,5,2,1,5,3] }
assert my_solution.sumCounts(**test_input) == 203
test_input = { "nums": [4,4,2,5,5,4,2,2,1] }
assert my_solution.sumCounts(**test_input) == 294
test_input = { "nums": [1] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [1,1,3,3,3,4,4] }
assert my_solution.sumCounts(**test_input) == 96
test_input = { "nums": [3,2,2,3,4] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [1,5,3,2,4,4] }
assert my_solution.sumCounts(**test_input) == 161
test_input = { "nums": [5,4,1,1,3] }
assert my_solution.sumCounts(**test_input) == 69
test_input = { "nums": [4,3,3,5,3,4,5,3,3,1] }
assert my_solution.sumCounts(**test_input) == 376
test_input = { "nums": [2,3,4,1,5,1,3,3,4] }
assert my_solution.sumCounts(**test_input) == 432
test_input = { "nums": [5,1,4,2,1,1] }
assert my_solution.sumCounts(**test_input) == 129
test_input = { "nums": [5,4,4,1] }
assert my_solution.sumCounts(**test_input) == 30
test_input = { "nums": [1,5,1,3,2,1] }
assert my_solution.sumCounts(**test_input) == 139
test_input = { "nums": [5,3] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,1,4,3] }
assert my_solution.sumCounts(**test_input) == 38
test_input = { "nums": [1,5,4,3,4,2,4,5,5,4] }
assert my_solution.sumCounts(**test_input) == 513
test_input = { "nums": [4,2,3,4,3,2,5,4,4] }
assert my_solution.sumCounts(**test_input) == 378
test_input = { "nums": [2,3,3,2,1,5,2,2] }
assert my_solution.sumCounts(**test_input) == 262
test_input = { "nums": [2,1,4,2,4,1,4,3] }
assert my_solution.sumCounts(**test_input) == 243
test_input = { "nums": [1,4,4,1,3] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [2,3,2,1] }
assert my_solution.sumCounts(**test_input) == 38
test_input = { "nums": [1,4,2,1] }
assert my_solution.sumCounts(**test_input) == 43
test_input = { "nums": [2,4,3,2,5,1] }
assert my_solution.sumCounts(**test_input) == 169
test_input = { "nums": [2,5,3,2,1,3,1,3,2] }
assert my_solution.sumCounts(**test_input) == 348
test_input = { "nums": [4,1] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,3,1,4,3,4,3,4,1] }
assert my_solution.sumCounts(**test_input) == 263
test_input = { "nums": [5,1,1,1,4,3] }
assert my_solution.sumCounts(**test_input) == 89
test_input = { "nums": [4,5] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [5,2,2,3,1,2,5,3] }
assert my_solution.sumCounts(**test_input) == 289
test_input = { "nums": [3,2,4] }
assert my_solution.sumCounts(**test_input) == 20
test_input = { "nums": [5,3,5,2,3,2] }
assert my_solution.sumCounts(**test_input) == 106
test_input = { "nums": [3,2,1] }
assert my_solution.sumCounts(**test_input) == 20
test_input = { "nums": [4,4,2,4,3] }
assert my_solution.sumCounts(**test_input) == 57
test_input = { "nums": [1,4,4] }
assert my_solution.sumCounts(**test_input) == 12
test_input = { "nums": [1,4,4,3,1,2,1,4,3] }
assert my_solution.sumCounts(**test_input) == 387
test_input = { "nums": [1,5,4,2,5,5,5,3] }
assert my_solution.sumCounts(**test_input) == 249
test_input = { "nums": [2,1,5,3] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [2,3,5,1,5,2,3,2,3,4] }
assert my_solution.sumCounts(**test_input) == 533
test_input = { "nums": [5,3,4,4,3,5,4,5] }
assert my_solution.sumCounts(**test_input) == 202
test_input = { "nums": [4,4,2,2,4,1] }
assert my_solution.sumCounts(**test_input) == 80
test_input = { "nums": [2,3] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [4,2,3,2] }
assert my_solution.sumCounts(**test_input) == 38
test_input = { "nums": [1,2,2] }
assert my_solution.sumCounts(**test_input) == 12
test_input = { "nums": [4,1,5,1,5,4,5,1] }
assert my_solution.sumCounts(**test_input) == 205
test_input = { "nums": [4,5,3,1] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [4,2,3,4,2,4,3,3,2] }
assert my_solution.sumCounts(**test_input) == 275
test_input = { "nums": [4,3] }
assert my_solution.sumCounts(**test_input) == 6
test_input = { "nums": [1,3,5,4,4,4] }
assert my_solution.sumCounts(**test_input) == 113
test_input = { "nums": [1,2,4,2,1,2,2,4,1,3] }
assert my_solution.sumCounts(**test_input) == 391
test_input = { "nums": [4,2,5,3,2] }
assert my_solution.sumCounts(**test_input) == 89
test_input = { "nums": [3,4,5,3,2,5] }
assert my_solution.sumCounts(**test_input) == 144
test_input = { "nums": [5,4,5] }
assert my_solution.sumCounts(**test_input) == 15
test_input = { "nums": [2,4,5,1] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [5,4,1,4,2,1,5] }
assert my_solution.sumCounts(**test_input) == 203
test_input = { "nums": [2,3,3,2,2,3,1] }
assert my_solution.sumCounts(**test_input) == 110
test_input = { "nums": [1,4,2,5] }
assert my_solution.sumCounts(**test_input) == 50
test_input = { "nums": [3] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [5] }
assert my_solution.sumCounts(**test_input) == 1
test_input = { "nums": [1,3,5,3,2,1,1,4,3] }
assert my_solution.sumCounts(**test_input) == 441
test_input = { "nums": [1,5,2,2,3,3,3] }
assert my_solution.sumCounts(**test_input) == 140
test_input = { "nums": [1,2,1,4,5,5,4,1,1,1] }
assert my_solution.sumCounts(**test_input) == 407
test_input = { "nums": [2,2,1,1,1,2,5,4,5] }
assert my_solution.sumCounts(**test_input) == 296
test_input = { "nums": [3,2,3] }
assert my_solution.sumCounts(**test_input) == 15
test_input = { "nums": [2,1,5,4,3,3,2,1,5,5] }
assert my_solution.sumCounts(**test_input) == 652 | 1,698,503,400 |
weekly-contest-368-minimum-sum-of-mountain-triplets-i | https://leetcode.com/problems/minimum-sum-of-mountain-triplets-i | minimum-sum-of-mountain-triplets-i | {
"questionId": "3176",
"questionFrontendId": "2908",
"title": "Minimum Sum of Mountain Triplets I",
"titleSlug": "minimum-sum-of-mountain-triplets-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 113,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array nums of integers.
A triplet of indices (i, j, k) is a mountain if:
* i < j < k
* nums[i] < nums[j] and nums[k] < nums[j]
Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.
Example 1:
Input: nums = [8,6,1,5,3]
Output: 9
Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
- 2 < 3 < 4
- nums[2] < nums[3] and nums[4] < nums[3]
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
Example 2:
Input: nums = [5,4,8,7,10,2]
Output: 13
Explanation: Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
- 1 < 3 < 5
- nums[1] < nums[3] and nums[5] < nums[3]
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
Example 3:
Input: nums = [6,5,4,3,4,5]
Output: -1
Explanation: It can be shown that there are no mountain triplets in nums.
Constraints:
* 3 <= nums.length <= 50
* 1 <= nums[i] <= 50
"""
class Solution:
def minimumSum(self, nums: List[int]) -> int:
| You are given a 0-indexed array nums of integers.
A triplet of indices (i, j, k) is a mountain if:
* i < j < k
* nums[i] < nums[j] and nums[k] < nums[j]
Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.
Example 1:
Input: nums = [8,6,1,5,3]
Output: 9
Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
- 2 < 3 < 4
- nums[2] < nums[3] and nums[4] < nums[3]
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
Example 2:
Input: nums = [5,4,8,7,10,2]
Output: 13
Explanation: Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
- 1 < 3 < 5
- nums[1] < nums[3] and nums[5] < nums[3]
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
Example 3:
Input: nums = [6,5,4,3,4,5]
Output: -1
Explanation: It can be shown that there are no mountain triplets in nums.
Constraints:
* 3 <= nums.length <= 50
* 1 <= nums[i] <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumSum(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [8,6,1,5,3] }
assert my_solution.minimumSum(**test_input) == 9
test_input = { "nums": [5,4,8,7,10,2] }
assert my_solution.minimumSum(**test_input) == 13
test_input = { "nums": [6,5,4,3,4,5] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [50,50,50] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [49,50,48] }
assert my_solution.minimumSum(**test_input) == 147
test_input = { "nums": [48,50,49] }
assert my_solution.minimumSum(**test_input) == 147
test_input = { "nums": [1,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,2,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,3,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [2,3,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [2,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,2,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,3,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,3,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,1,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,1,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,1,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,3,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,4,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,1,4,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,1,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,1,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,1,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,1,2] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,1,3] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,1,4] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,2,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,3,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,2,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,3,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,4,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,4,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,2,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,2,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,1,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,1,2] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,1,3] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,1,4] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,3,2,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,2,3] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,2,4] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,3,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,4,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,4,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,3,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,4,1,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,1,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,1,3] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,1,4] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,4,2,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,2,3] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,2,4] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,4,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,3,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,4,3,4] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,4,4,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,4,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,4,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,1,3] }
assert my_solution.minimumSum(**test_input) == -1 | 1,697,941,800 |
weekly-contest-368-minimum-sum-of-mountain-triplets-ii | https://leetcode.com/problems/minimum-sum-of-mountain-triplets-ii | minimum-sum-of-mountain-triplets-ii | {
"questionId": "3186",
"questionFrontendId": "2909",
"title": "Minimum Sum of Mountain Triplets II",
"titleSlug": "minimum-sum-of-mountain-triplets-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 160,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array nums of integers.
A triplet of indices (i, j, k) is a mountain if:
* i < j < k
* nums[i] < nums[j] and nums[k] < nums[j]
Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.
Example 1:
Input: nums = [8,6,1,5,3]
Output: 9
Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
- 2 < 3 < 4
- nums[2] < nums[3] and nums[4] < nums[3]
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
Example 2:
Input: nums = [5,4,8,7,10,2]
Output: 13
Explanation: Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
- 1 < 3 < 5
- nums[1] < nums[3] and nums[5] < nums[3]
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
Example 3:
Input: nums = [6,5,4,3,4,5]
Output: -1
Explanation: It can be shown that there are no mountain triplets in nums.
Constraints:
* 3 <= nums.length <= 105
* 1 <= nums[i] <= 108
"""
class Solution:
def minimumSum(self, nums: List[int]) -> int:
| You are given a 0-indexed array nums of integers.
A triplet of indices (i, j, k) is a mountain if:
* i < j < k
* nums[i] < nums[j] and nums[k] < nums[j]
Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.
Example 1:
Input: nums = [8,6,1,5,3]
Output: 9
Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
- 2 < 3 < 4
- nums[2] < nums[3] and nums[4] < nums[3]
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
Example 2:
Input: nums = [5,4,8,7,10,2]
Output: 13
Explanation: Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
- 1 < 3 < 5
- nums[1] < nums[3] and nums[5] < nums[3]
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
Example 3:
Input: nums = [6,5,4,3,4,5]
Output: -1
Explanation: It can be shown that there are no mountain triplets in nums.
Constraints:
* 3 <= nums.length <= 105
* 1 <= nums[i] <= 108
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumSum(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [8,6,1,5,3] }
assert my_solution.minimumSum(**test_input) == 9
test_input = { "nums": [5,4,8,7,10,2] }
assert my_solution.minimumSum(**test_input) == 13
test_input = { "nums": [6,5,4,3,4,5] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [50,50,50] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [49,50,48] }
assert my_solution.minimumSum(**test_input) == 147
test_input = { "nums": [48,50,49] }
assert my_solution.minimumSum(**test_input) == 147
test_input = { "nums": [99999999,100000000,99999999] }
assert my_solution.minimumSum(**test_input) == 299999998
test_input = { "nums": [1,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,2,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,3,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [2,3,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [2,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,2,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,3,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,3,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [3,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,1,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,1,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,2,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,1,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,1,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,3,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,1,4,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,1,4,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,1,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,1,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,2,1,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,1,2] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,1,3] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,1,4] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,2,2] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,2,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,2,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,3,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,3,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,2,4,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,2,4,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,2,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,2,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,1,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,1,2] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,1,3] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,1,4] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,3,2,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,2,3] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,2,4] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,3,3] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,3,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,3,4,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,3,4,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,3,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,3,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [1,4,1,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,1,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,1,3] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,1,4] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,2,1] }
assert my_solution.minimumSum(**test_input) == 4
test_input = { "nums": [1,4,2,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,2,3] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,2,4] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,3,1] }
assert my_solution.minimumSum(**test_input) == 5
test_input = { "nums": [1,4,3,2] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,3,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,4,3,4] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,4,4,1] }
assert my_solution.minimumSum(**test_input) == 6
test_input = { "nums": [1,4,4,2] }
assert my_solution.minimumSum(**test_input) == 7
test_input = { "nums": [1,4,4,3] }
assert my_solution.minimumSum(**test_input) == 8
test_input = { "nums": [1,4,4,4] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,1,1] }
assert my_solution.minimumSum(**test_input) == -1
test_input = { "nums": [2,1,1,2] }
assert my_solution.minimumSum(**test_input) == -1 | 1,697,941,800 |
weekly-contest-368-minimum-number-of-groups-to-create-a-valid-assignment | https://leetcode.com/problems/minimum-number-of-groups-to-create-a-valid-assignment | minimum-number-of-groups-to-create-a-valid-assignment | {
"questionId": "3166",
"questionFrontendId": "2910",
"title": "Minimum Number of Groups to Create a Valid Assignment",
"titleSlug": "minimum-number-of-groups-to-create-a-valid-assignment",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 231,
"dislikes": 151,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums of length n.
We want to group the indices so for each index i in the range [0, n - 1], it is assigned to exactly one group.
A group assignment is valid if the following conditions hold:
* For every group g, all indices i assigned to group g have the same value in nums.
* For any two groups g1 and g2, the difference between the number of indices assigned to g1 and g2 should not exceed 1.
Return an integer denoting the minimum number of groups needed to create a valid group assignment.
Example 1:
Input: nums = [3,2,3,2,3]
Output: 2
Explanation: One way the indices can be assigned to 2 groups is as follows, where the values in square brackets are indices:
group 1 -> [0,2,4]
group 2 -> [1,3]
All indices are assigned to one group.
In group 1, nums[0] == nums[2] == nums[4], so all indices have the same value.
In group 2, nums[1] == nums[3], so all indices have the same value.
The number of indices assigned to group 1 is 3, and the number of indices assigned to group 2 is 2.
Their difference doesn't exceed 1.
It is not possible to use fewer than 2 groups because, in order to use just 1 group, all indices assigned to that group must have the same value.
Hence, the answer is 2.
Example 2:
Input: nums = [10,10,10,3,1,1]
Output: 4
Explanation: One way the indices can be assigned to 4 groups is as follows, where the values in square brackets are indices:
group 1 -> [0]
group 2 -> [1,2]
group 3 -> [3]
group 4 -> [4,5]
The group assignment above satisfies both conditions.
It can be shown that it is not possible to create a valid assignment using fewer than 4 groups.
Hence, the answer is 4.
Constraints:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
"""
class Solution:
def minGroupsForValidAssignment(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums of length n.
We want to group the indices so for each index i in the range [0, n - 1], it is assigned to exactly one group.
A group assignment is valid if the following conditions hold:
* For every group g, all indices i assigned to group g have the same value in nums.
* For any two groups g1 and g2, the difference between the number of indices assigned to g1 and g2 should not exceed 1.
Return an integer denoting the minimum number of groups needed to create a valid group assignment.
Example 1:
Input: nums = [3,2,3,2,3]
Output: 2
Explanation: One way the indices can be assigned to 2 groups is as follows, where the values in square brackets are indices:
group 1 -> [0,2,4]
group 2 -> [1,3]
All indices are assigned to one group.
In group 1, nums[0] == nums[2] == nums[4], so all indices have the same value.
In group 2, nums[1] == nums[3], so all indices have the same value.
The number of indices assigned to group 1 is 3, and the number of indices assigned to group 2 is 2.
Their difference doesn't exceed 1.
It is not possible to use fewer than 2 groups because, in order to use just 1 group, all indices assigned to that group must have the same value.
Hence, the answer is 2.
Example 2:
Input: nums = [10,10,10,3,1,1]
Output: 4
Explanation: One way the indices can be assigned to 4 groups is as follows, where the values in square brackets are indices:
group 1 -> [0]
group 2 -> [1,2]
group 3 -> [3]
group 4 -> [4,5]
The group assignment above satisfies both conditions.
It can be shown that it is not possible to create a valid assignment using fewer than 4 groups.
Hence, the answer is 4.
Constraints:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def minGroupsForValidAssignment(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [3,2,3,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [10,10,10,3,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 1
test_input = { "nums": [1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 1
test_input = { "nums": [3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 1
test_input = { "nums": [3,4] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [10,4] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [10,10] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 1
test_input = { "nums": [14,15] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [1,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 1
test_input = { "nums": [1,2,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [1,7,10] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,2,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,3,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,8,5] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [3,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,10,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [4,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [5,4,8] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [5,6,8] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [5,9,4] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [6,4,9] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [9,2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [10,5,5] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [14,7,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,1,1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,2,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,2,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [1,2,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,2,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,3,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,3,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,2,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,2,3,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [2,3,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,3,2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,5,10,4] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [3,2,2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [3,3,2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [5,1,5,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [13,11,4,13] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,1,1,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 1
test_input = { "nums": [1,2,2,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,2,2,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,1,1,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,1,3,3,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,3,1,2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,3,2,2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,3,3,1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [3,1,1,3,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,1,3,2,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [3,1,3,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [3,2,2,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [3,3,1,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [4,1,7,8,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 5
test_input = { "nums": [7,5,9,4,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 5
test_input = { "nums": [10,2,2,1,10] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,1,1,3,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [1,1,3,1,1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,2,1,3,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,3,1,2,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [1,5,12,11,5,9] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 5
test_input = { "nums": [1,6,5,8,7,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 5
test_input = { "nums": [2,1,1,1,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,1,2,1,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,1,3,2,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,2,1,1,1,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,2,1,2,3,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,3,3,1,2,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,3,3,1,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,5,9,3,1,4] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 6
test_input = { "nums": [3,2,2,3,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [3,3,1,2,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [6,7,4,8,8,10] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 5
test_input = { "nums": [8,5,8,1,4,5] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [8,7,4,5,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 6
test_input = { "nums": [10,8,3,3,2,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 5
test_input = { "nums": [1,2,1,2,1,1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [1,2,2,1,1,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 2
test_input = { "nums": [1,2,3,3,3,3,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 5
test_input = { "nums": [1,3,1,7,4,5,8] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 6
test_input = { "nums": [2,1,1,3,3,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,1,3,3,3,1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,2,2,1,2,3,1] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,2,2,2,2,1,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4
test_input = { "nums": [2,3,1,3,1,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [2,3,1,3,2,1,3] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 3
test_input = { "nums": [3,1,3,2,3,3,2] }
assert my_solution.minGroupsForValidAssignment(**test_input) == 4 | 1,697,941,800 |
weekly-contest-368-minimum-changes-to-make-k-semi-palindromes | https://leetcode.com/problems/minimum-changes-to-make-k-semi-palindromes | minimum-changes-to-make-k-semi-palindromes | {
"questionId": "2879",
"questionFrontendId": "2911",
"title": "Minimum Changes to Make K Semi-palindromes",
"titleSlug": "minimum-changes-to-make-k-semi-palindromes",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 50,
"dislikes": 78,
"categoryTitle": "Algorithms"
} | """
Given a string s and an integer k, partition s into k substrings such that the sum of the number of letter changes required to turn each substring into a semi-palindrome is minimized.
Return an integer denoting the minimum number of letter changes required.
Notes
* A string is a palindrome if it can be read the same way from left to right and right to left.
* A string with a length of len is considered a semi-palindrome if there exists a positive integer d such that 1 <= d < len and len % d == 0, and if we take indices that have the same modulo by d, they form a palindrome. For example, "aa", "aba", "adbgad", and, "abab" are semi-palindrome and "a", "ab", and, "abca" are not.
* A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "abcac", k = 2
Output: 1
Explanation: We can divide s into substrings "ab" and "cac". The string "cac" is already a semi-palindrome. If we change "ab" to "aa", it becomes a semi-palindrome with d = 1.
It can be shown that there is no way to divide the string "abcac" into two semi-palindrome substrings. Therefore, the answer would be at least 1.
Example 2:
Input: s = "abcdef", k = 2
Output: 2
Explanation: We can divide it into substrings "abc" and "def". Each of the substrings "abc" and "def" requires one change to become a semi-palindrome, so we need 2 changes in total to make all substrings semi-palindrome.
It can be shown that we cannot divide the given string into two substrings in a way that it would require less than 2 changes.
Example 3:
Input: s = "aabbaa", k = 3
Output: 0
Explanation: We can divide it into substrings "aa", "bb" and "aa".
The strings "aa" and "bb" are already semi-palindromes. Thus, the answer is zero.
Constraints:
* 2 <= s.length <= 200
* 1 <= k <= s.length / 2
* s consists only of lowercase English letters.
"""
class Solution:
def minimumChanges(self, s: str, k: int) -> int:
| Given a string s and an integer k, partition s into k substrings such that the sum of the number of letter changes required to turn each substring into a semi-palindrome is minimized.
Return an integer denoting the minimum number of letter changes required.
Notes
* A string is a palindrome if it can be read the same way from left to right and right to left.
* A string with a length of len is considered a semi-palindrome if there exists a positive integer d such that 1 <= d < len and len % d == 0, and if we take indices that have the same modulo by d, they form a palindrome. For example, "aa", "aba", "adbgad", and, "abab" are semi-palindrome and "a", "ab", and, "abca" are not.
* A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "abcac", k = 2
Output: 1
Explanation: We can divide s into substrings "ab" and "cac". The string "cac" is already a semi-palindrome. If we change "ab" to "aa", it becomes a semi-palindrome with d = 1.
It can be shown that there is no way to divide the string "abcac" into two semi-palindrome substrings. Therefore, the answer would be at least 1.
Example 2:
Input: s = "abcdef", k = 2
Output: 2
Explanation: We can divide it into substrings "abc" and "def". Each of the substrings "abc" and "def" requires one change to become a semi-palindrome, so we need 2 changes in total to make all substrings semi-palindrome.
It can be shown that we cannot divide the given string into two substrings in a way that it would require less than 2 changes.
Example 3:
Input: s = "aabbaa", k = 3
Output: 0
Explanation: We can divide it into substrings "aa", "bb" and "aa".
The strings "aa" and "bb" are already semi-palindromes. Thus, the answer is zero.
Constraints:
* 2 <= s.length <= 200
* 1 <= k <= s.length / 2
* s consists only of lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def minimumChanges(self, s: str, k: int) -> int:
``` |
my_solution = Solution()
test_input = { "s": "abcac", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "abcdef", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "aabbaa", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 0
test_input = { "s": "aq", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "bb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 0
test_input = { "s": "aac", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "abcc", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "acba", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "edaswf", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "aabcbaa", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 0
test_input = { "s": "dqpldq", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "eksddulf", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "aaaaacabbb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "aaabacacbb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "abbbbacaaa", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "abcccbaccb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "baacbbbaba", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "babcbaccba", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "cabbcabcbc", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "ccbccaaabb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "cccbabbbbc", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "cccccbaaac", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "dyfnbbbqbm", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "hafrypzupv", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "aabcacccabc", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "abbcaabaaac", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 5
test_input = { "s": "baabaabbcb", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 0
test_input = { "s": "bbbabcbaccb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "bcababccaa", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "bcacacacaab", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "bcacccacbaa", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "bccaaccacb", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "caacbacbaca", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "ccccaaacca", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "efrsgmjneph", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 5
test_input = { "s": "ehdvhthgbxq", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "eymakkvrvc", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "gilkaelnfr", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "iiaenfiasiv", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "piazrazesdk", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 5
test_input = { "s": "pypwcllynf", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "uqicxuvkorn", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 5
test_input = { "s": "ziirnywodfz", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "zpogsiabazr", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 5
test_input = { "s": "aacacaacabba", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "abacacabba", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 0
test_input = { "s": "acbcbccccba", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "bbcaaaaacbb", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "cbabaabccba", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "cbacccbabcaa", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "cbbcbcacca", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "ccaabbbccacb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "ccabcbbcaa", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "epenvgssid", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "grllkopehr", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "iaemfpyhrtgb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "iqjvqxzhjc", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "kpkzjgcvgopr", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "neocjmpaltv", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "uvdbxsjyso", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "wsezruidpcy", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "ybexlzsvsi", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "abacabccaa", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "abccbacbcbc", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "abccccbaaba", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "acabbbacacbb", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "acbbbbccacc", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "acbcbbaaca", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "accbabbbaacaa", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "baaaccbaaa", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "baababcacc", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "baabbccbbc", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "bacbbaaccb", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "baccbbccab", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "bbababccabca", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "bbacbccbca", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "bbacccbbaabbb", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "bbccbbbcaab", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "bbccbcccaba", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 0
test_input = { "s": "bcaacaabaa", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "bcbcbabaabaa", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "bofqvqapnjo", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "bvatyzbdffqdp", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 6
test_input = { "s": "cabbcbcbcbcca", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "cbacbbcbccccc", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "ccaccaacbcaac", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "ccbabcbabb", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 1
test_input = { "s": "ccbacacbcbac", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "edulrtnsbb", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "feecuhvurk", "k": 4 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "ffqbqdocclh", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 2
test_input = { "s": "gceeouniipz", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "gdlitshyeehtx", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 6
test_input = { "s": "hpbijyuygkk", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 3
test_input = { "s": "kxvwhuewyftpp", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 6
test_input = { "s": "mrqvwotsqjtfv", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 6
test_input = { "s": "qhzievvxauf", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "rbiuxrgidyzuu", "k": 1 }
assert my_solution.minimumChanges(**test_input) == 6
test_input = { "s": "rkyidomzyud", "k": 3 }
assert my_solution.minimumChanges(**test_input) == 4
test_input = { "s": "wvewmaevkzjp", "k": 2 }
assert my_solution.minimumChanges(**test_input) == 3 | 1,697,941,800 |
weekly-contest-367-find-indices-with-index-and-value-difference-i | https://leetcode.com/problems/find-indices-with-index-and-value-difference-i | find-indices-with-index-and-value-difference-i | {
"questionId": "3165",
"questionFrontendId": "2903",
"title": "Find Indices With Index and Value Difference I",
"titleSlug": "find-indices-with-index-and-value-difference-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 97,
"dislikes": 7,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.
Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
* abs(i - j) >= indexDifference, and
* abs(nums[i] - nums[j]) >= valueDifference
Return an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
Note: i and j may be equal.
Example 1:
Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
Output: [0,3]
Explanation: In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
Example 2:
Input: nums = [2,1], indexDifference = 0, valueDifference = 0
Output: [0,0]
Explanation: In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
Example 3:
Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
Output: [-1,-1]
Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
Constraints:
* 1 <= n == nums.length <= 100
* 0 <= nums[i] <= 50
* 0 <= indexDifference <= 100
* 0 <= valueDifference <= 50
"""
class Solution:
def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
| You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.
Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
* abs(i - j) >= indexDifference, and
* abs(nums[i] - nums[j]) >= valueDifference
Return an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
Note: i and j may be equal.
Example 1:
Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
Output: [0,3]
Explanation: In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
Example 2:
Input: nums = [2,1], indexDifference = 0, valueDifference = 0
Output: [0,0]
Explanation: In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
Example 3:
Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
Output: [-1,-1]
Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
Constraints:
* 1 <= n == nums.length <= 100
* 0 <= nums[i] <= 50
* 0 <= indexDifference <= 100
* 0 <= valueDifference <= 50
Please complete the code below to solve above prblem:
```python
class Solution:
def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "nums": [5,1,4,1], "indexDifference": 2, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [2,1], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [1,2,3], "indexDifference": 2, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [0], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [3], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [3], "indexDifference": 1, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [4], "indexDifference": 1, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [5], "indexDifference": 1, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [7], "indexDifference": 1, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [8], "indexDifference": 0, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [8], "indexDifference": 1, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [10], "indexDifference": 0, "valueDifference": 9 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [11], "indexDifference": 1, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [18], "indexDifference": 1, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [38], "indexDifference": 1, "valueDifference": 34 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [40], "indexDifference": 1, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [5,10], "indexDifference": 1, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [5,48], "indexDifference": 0, "valueDifference": 29 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [6,3], "indexDifference": 1, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [7,6], "indexDifference": 1, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [8,8], "indexDifference": 1, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [17,31], "indexDifference": 1, "valueDifference": 9 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [21,22], "indexDifference": 1, "valueDifference": 21 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [48,40], "indexDifference": 2, "valueDifference": 31 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [2,8,0], "indexDifference": 2, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [2,29,0], "indexDifference": 0, "valueDifference": 12 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [3,0,7], "indexDifference": 2, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [4,22,43], "indexDifference": 0, "valueDifference": 34 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [5,0,3], "indexDifference": 1, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [5,9,2], "indexDifference": 0, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [6,2,7], "indexDifference": 2, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [6,5,1], "indexDifference": 2, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [6,8,0], "indexDifference": 1, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [7,36,21], "indexDifference": 1, "valueDifference": 20 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [9,4,7], "indexDifference": 0, "valueDifference": 9 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [9,50,31], "indexDifference": 1, "valueDifference": 8 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [31,23,36], "indexDifference": 1, "valueDifference": 11 }
assert my_solution.findIndices(**test_input) == [1,2]
test_input = { "nums": [40,21,1], "indexDifference": 2, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [0,5,10,5], "indexDifference": 3, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [1,28,24,35], "indexDifference": 3, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [2,7,10,4], "indexDifference": 0, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [3,1,0,3], "indexDifference": 2, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [7,5,6,2], "indexDifference": 2, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [9,3,6,4], "indexDifference": 1, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [34,46,11,45], "indexDifference": 1, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [36,37,40,9], "indexDifference": 2, "valueDifference": 8 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [37,25,48,13], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [45,6,29,21], "indexDifference": 3, "valueDifference": 36 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [1,5,7,9,2], "indexDifference": 3, "valueDifference": 8 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [7,2,1,8,3], "indexDifference": 0, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [2,3]
test_input = { "nums": [8,9,8,0,4], "indexDifference": 1, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [9,1,10,0,10], "indexDifference": 0, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [9,9,4,5,5], "indexDifference": 2, "valueDifference": 9 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [10,1,10,12,1], "indexDifference": 1, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [17,46,31,28,28], "indexDifference": 0, "valueDifference": 46 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [17,49,1,47,12], "indexDifference": 2, "valueDifference": 17 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [32,49,3,40,44], "indexDifference": 1, "valueDifference": 37 }
assert my_solution.findIndices(**test_input) == [1,2]
test_input = { "nums": [46,43,16,16,34], "indexDifference": 3, "valueDifference": 13 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [49,36,18,4,33], "indexDifference": 3, "valueDifference": 20 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [0,7,10,6,6,5], "indexDifference": 1, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [2,0,3,4,0,5], "indexDifference": 3, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [1,5]
test_input = { "nums": [3,8,9,7,2,3], "indexDifference": 3, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [1,4]
test_input = { "nums": [3,27,38,47,38,4], "indexDifference": 0, "valueDifference": 10 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [4,13,48,50,1,26], "indexDifference": 4, "valueDifference": 34 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [6,1,2,6,4,6], "indexDifference": 2, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [7,1,0,9,5,9], "indexDifference": 2, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [7,3,7,5,7,9], "indexDifference": 1, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [12,37,7,16,5,34], "indexDifference": 3, "valueDifference": 27 }
assert my_solution.findIndices(**test_input) == [1,4]
test_input = { "nums": [17,46,48,25,22,4], "indexDifference": 2, "valueDifference": 30 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [18,18,7,10,9,50], "indexDifference": 2, "valueDifference": 32 }
assert my_solution.findIndices(**test_input) == [0,5]
test_input = { "nums": [18,42,37,13,49,42], "indexDifference": 3, "valueDifference": 46 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [23,31,14,42,0,49], "indexDifference": 4, "valueDifference": 44 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [50,46,15,16,48,7], "indexDifference": 1, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [5,6,8,5,6,3,1], "indexDifference": 0, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,6]
test_input = { "nums": [5,50,13,3,44,7,29], "indexDifference": 1, "valueDifference": 45 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [8,7,18,47,27,25,41], "indexDifference": 0, "valueDifference": 45 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [9,1,0,6,7,5,8], "indexDifference": 1, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [11,3,36,17,13,0,26], "indexDifference": 2, "valueDifference": 33 }
assert my_solution.findIndices(**test_input) == [2,5]
test_input = { "nums": [13,0,16,32,47,27,25], "indexDifference": 1, "valueDifference": 35 }
assert my_solution.findIndices(**test_input) == [1,4]
test_input = { "nums": [13,16,30,33,50,50,38], "indexDifference": 3, "valueDifference": 30 }
assert my_solution.findIndices(**test_input) == [0,4]
test_input = { "nums": [21,44,22,1,21,9,17], "indexDifference": 1, "valueDifference": 41 }
assert my_solution.findIndices(**test_input) == [1,3]
test_input = { "nums": [35,31,36,28,49,4,46], "indexDifference": 4, "valueDifference": 27 }
assert my_solution.findIndices(**test_input) == [0,5]
test_input = { "nums": [39,18,49,25,40,41,26], "indexDifference": 1, "valueDifference": 43 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [40,46,11,36,25,46,47], "indexDifference": 0, "valueDifference": 37 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [46,7,6,3,43,7,48], "indexDifference": 0, "valueDifference": 48 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [0,1,6,8,8,3,9,10], "indexDifference": 2, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [1,0,6,4,8,7,2,5], "indexDifference": 3, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,4]
test_input = { "nums": [3,8,3,8,0,5,5,7], "indexDifference": 4, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [5,10,4,4,8,6,0,4], "indexDifference": 2, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [1,6]
test_input = { "nums": [6,1,3,7,4,4,2,1], "indexDifference": 0, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [9,36,31,2,46,1,27,37], "indexDifference": 3, "valueDifference": 45 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [10,4,0,1,4,7,2,0], "indexDifference": 5, "valueDifference": 8 }
assert my_solution.findIndices(**test_input) == [0,6]
test_input = { "nums": [26,20,19,36,20,28,33,39], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [29,3,30,34,25,40,10,37], "indexDifference": 5, "valueDifference": 22 }
assert my_solution.findIndices(**test_input) == [1,7]
test_input = { "nums": [39,26,46,9,5,34,0,20], "indexDifference": 0, "valueDifference": 24 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [46,12,38,21,12,9,18,29], "indexDifference": 3, "valueDifference": 35 }
assert my_solution.findIndices(**test_input) == [0,5]
test_input = { "nums": [1,7,7,2,4,10,1,5,9], "indexDifference": 4, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,5]
test_input = { "nums": [2,4,1,5,2,0,3,5,7], "indexDifference": 5, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,5]
test_input = { "nums": [2,5,6,5,9,7,2,3,6], "indexDifference": 4, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,4]
test_input = { "nums": [2,6,9,4,9,4,10,9,2], "indexDifference": 0, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,1] | 1,697,337,000 |
weekly-contest-367-shortest-and-lexicographically-smallest-beautiful-string | https://leetcode.com/problems/shortest-and-lexicographically-smallest-beautiful-string | shortest-and-lexicographically-smallest-beautiful-string | {
"questionId": "3150",
"questionFrontendId": "2904",
"title": "Shortest and Lexicographically Smallest Beautiful String",
"titleSlug": "shortest-and-lexicographically-smallest-beautiful-string",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 147,
"dislikes": 8,
"categoryTitle": "Algorithms"
} | """
You are given a binary string s and a positive integer k.
A substring of s is beautiful if the number of 1's in it is exactly k.
Let len be the length of the shortest beautiful substring.
Return the lexicographically smallest beautiful substring of string s with length equal to len. If s doesn't contain a beautiful substring, return an empty string.
A string a is lexicographically larger than a string b (of the same length) if in the first position where a and b differ, a has a character strictly larger than the corresponding character in b.
* For example, "abcd" is lexicographically larger than "abcc" because the first position they differ is at the fourth character, and d is greater than c.
Example 1:
Input: s = "100011001", k = 3
Output: "11001"
Explanation: There are 7 beautiful substrings in this example:
1. The substring "100011001".
2. The substring "100011001".
3. The substring "100011001".
4. The substring "100011001".
5. The substring "100011001".
6. The substring "100011001".
7. The substring "100011001".
The length of the shortest beautiful substring is 5.
The lexicographically smallest beautiful substring with length 5 is the substring "11001".
Example 2:
Input: s = "1011", k = 2
Output: "11"
Explanation: There are 3 beautiful substrings in this example:
1. The substring "1011".
2. The substring "1011".
3. The substring "1011".
The length of the shortest beautiful substring is 2.
The lexicographically smallest beautiful substring with length 2 is the substring "11".
Example 3:
Input: s = "000", k = 1
Output: ""
Explanation: There are no beautiful substrings in this example.
Constraints:
* 1 <= s.length <= 100
* 1 <= k <= s.length
"""
class Solution:
def shortestBeautifulSubstring(self, s: str, k: int) -> str:
| You are given a binary string s and a positive integer k.
A substring of s is beautiful if the number of 1's in it is exactly k.
Let len be the length of the shortest beautiful substring.
Return the lexicographically smallest beautiful substring of string s with length equal to len. If s doesn't contain a beautiful substring, return an empty string.
A string a is lexicographically larger than a string b (of the same length) if in the first position where a and b differ, a has a character strictly larger than the corresponding character in b.
* For example, "abcd" is lexicographically larger than "abcc" because the first position they differ is at the fourth character, and d is greater than c.
Example 1:
Input: s = "100011001", k = 3
Output: "11001"
Explanation: There are 7 beautiful substrings in this example:
1. The substring "100011001".
2. The substring "100011001".
3. The substring "100011001".
4. The substring "100011001".
5. The substring "100011001".
6. The substring "100011001".
7. The substring "100011001".
The length of the shortest beautiful substring is 5.
The lexicographically smallest beautiful substring with length 5 is the substring "11001".
Example 2:
Input: s = "1011", k = 2
Output: "11"
Explanation: There are 3 beautiful substrings in this example:
1. The substring "1011".
2. The substring "1011".
3. The substring "1011".
The length of the shortest beautiful substring is 2.
The lexicographically smallest beautiful substring with length 2 is the substring "11".
Example 3:
Input: s = "000", k = 1
Output: ""
Explanation: There are no beautiful substrings in this example.
Constraints:
* 1 <= s.length <= 100
* 1 <= k <= s.length
Please complete the code below to solve above prblem:
```python
class Solution:
def shortestBeautifulSubstring(self, s: str, k: int) -> str:
``` |
my_solution = Solution()
test_input = { "s": "100011001", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11001"
test_input = { "s": "1011", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "000", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "11000111", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "10100010", "k": 5 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "0010111", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "111111110010001010", "k": 11 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11111111001000101"
test_input = { "s": "00000110010011010110", "k": 13 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1100001110111100100", "k": 8 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11101111001"
test_input = { "s": "01011101000111110", "k": 5 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11111"
test_input = { "s": "10001", "k": 5 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "001011010001101", "k": 11 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1001000", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "001", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "000110001", "k": 6 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "10001", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "100011000000000", "k": 9 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "10101000111", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "111"
test_input = { "s": "00", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1100100101011001001", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1100100101011"
test_input = { "s": "0001001", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "101100000", "k": 6 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "100011", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "1101", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "110000", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "001110101101101111", "k": 10 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "10101101101111"
test_input = { "s": "111011110", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "1111111011111", "k": 12 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1111111011111"
test_input = { "s": "0101111000101011001", "k": 9 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "101111000101011"
test_input = { "s": "0011010", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "01101101110110010", "k": 16 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "10111101011100101001", "k": 14 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "0", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "0101001000001011011", "k": 12 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "01000100", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "01001010010", "k": 6 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "01100110011101010", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "111"
test_input = { "s": "10", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "01010000110111110010", "k": 12 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1101", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "00000100000", "k": 9 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "10101101000111", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "111"
test_input = { "s": "101010111111110110", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1111"
test_input = { "s": "0100111011101010", "k": 16 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1111001111", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "000010110111011", "k": 8 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "10110111011"
test_input = { "s": "010100", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "00", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "000100", "k": 5 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1110001110100110011", "k": 13 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "111011001", "k": 9 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "0111000100001101", "k": 9 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "100011", "k": 5 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "111000101010010011", "k": 10 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "01010", "k": 5 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "0100100111100010", "k": 14 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "110", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "111000", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "101011", "k": 6 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "11101101110110111101", "k": 17 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "010011101", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11101"
test_input = { "s": "100111010111001", "k": 11 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "110001111100011", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "0010110", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "11101110001111", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1110001111"
test_input = { "s": "110", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "110101", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1101"
test_input = { "s": "0110100", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "00111", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "111"
test_input = { "s": "1011001100001110101", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11101"
test_input = { "s": "0101000010101010", "k": 6 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "10100001010101"
test_input = { "s": "0011000110001", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "01001011000110100", "k": 11 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "11", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "11"
test_input = { "s": "00111100", "k": 5 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1110", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "111"
test_input = { "s": "00010000110", "k": 9 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "110110100011", "k": 8 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "00111110001100000", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1111100011"
test_input = { "s": "10100100101", "k": 11 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "110", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "1001101100000010100", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "0101010101010111101", "k": 15 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "0000101", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "110011010110", "k": 10 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "11010", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "10010100", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "1", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "11", "k": 1 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1"
test_input = { "s": "101000", "k": 2 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "101"
test_input = { "s": "000010110010111", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "10110010111"
test_input = { "s": "0100111", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "100111"
test_input = { "s": "0111111100011111101", "k": 12 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "111111000111111"
test_input = { "s": "010001", "k": 3 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "110110101010100000", "k": 10 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "110000001", "k": 4 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "001101", "k": 6 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "01111001101001010101", "k": 16 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "00111010", "k": 6 }
assert my_solution.shortestBeautifulSubstring(**test_input) == ""
test_input = { "s": "11011101010100", "k": 7 }
assert my_solution.shortestBeautifulSubstring(**test_input) == "1101110101" | 1,697,337,000 |
weekly-contest-367-find-indices-with-index-and-value-difference-ii | https://leetcode.com/problems/find-indices-with-index-and-value-difference-ii | find-indices-with-index-and-value-difference-ii | {
"questionId": "3170",
"questionFrontendId": "2905",
"title": "Find Indices With Index and Value Difference II",
"titleSlug": "find-indices-with-index-and-value-difference-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 218,
"dislikes": 6,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.
Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
* abs(i - j) >= indexDifference, and
* abs(nums[i] - nums[j]) >= valueDifference
Return an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
Note: i and j may be equal.
Example 1:
Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
Output: [0,3]
Explanation: In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
Example 2:
Input: nums = [2,1], indexDifference = 0, valueDifference = 0
Output: [0,0]
Explanation: In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
Example 3:
Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
Output: [-1,-1]
Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
Constraints:
* 1 <= n == nums.length <= 105
* 0 <= nums[i] <= 109
* 0 <= indexDifference <= 105
* 0 <= valueDifference <= 109
"""
class Solution:
def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
| You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.
Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
* abs(i - j) >= indexDifference, and
* abs(nums[i] - nums[j]) >= valueDifference
Return an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
Note: i and j may be equal.
Example 1:
Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
Output: [0,3]
Explanation: In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
Example 2:
Input: nums = [2,1], indexDifference = 0, valueDifference = 0
Output: [0,0]
Explanation: In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
Example 3:
Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
Output: [-1,-1]
Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
Constraints:
* 1 <= n == nums.length <= 105
* 0 <= nums[i] <= 109
* 0 <= indexDifference <= 105
* 0 <= valueDifference <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
``` |
my_solution = Solution()
test_input = { "nums": [5,1,4,1], "indexDifference": 2, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [2,1], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [1,2,3], "indexDifference": 2, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [1], "indexDifference": 0, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [1], "indexDifference": 1, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [2], "indexDifference": 0, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [6], "indexDifference": 1, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [7], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [8], "indexDifference": 1, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [9], "indexDifference": 0, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [9], "indexDifference": 1, "valueDifference": 9 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [12], "indexDifference": 0, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [16], "indexDifference": 0, "valueDifference": 16 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [16], "indexDifference": 1, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [46], "indexDifference": 0, "valueDifference": 36 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [0,10], "indexDifference": 2, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [2,7], "indexDifference": 2, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [5,1], "indexDifference": 2, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [5,12], "indexDifference": 0, "valueDifference": 10 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [8,0], "indexDifference": 1, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [8,4], "indexDifference": 1, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [9,8], "indexDifference": 0, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [15,8], "indexDifference": 0, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [20,21], "indexDifference": 1, "valueDifference": 14 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [23,47], "indexDifference": 2, "valueDifference": 47 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [32,14], "indexDifference": 1, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [36,14], "indexDifference": 2, "valueDifference": 11 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [40,12], "indexDifference": 1, "valueDifference": 20 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [45,41], "indexDifference": 2, "valueDifference": 21 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [50,24], "indexDifference": 1, "valueDifference": 18 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [2,7,0], "indexDifference": 0, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [3,9,3], "indexDifference": 0, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [3,12,40], "indexDifference": 0, "valueDifference": 9 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [4,10,8], "indexDifference": 0, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [5,27,26], "indexDifference": 1, "valueDifference": 15 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [7,8,10], "indexDifference": 2, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [8,2,6], "indexDifference": 1, "valueDifference": 8 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [8,7,5], "indexDifference": 2, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [9,5,4], "indexDifference": 1, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [9,9,8], "indexDifference": 1, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [10,4,10], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [10,5,2], "indexDifference": 2, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [37,45,23], "indexDifference": 1, "valueDifference": 28 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [37,49,25], "indexDifference": 1, "valueDifference": 29 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [45,32,30], "indexDifference": 2, "valueDifference": 45 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [0,2,5,8], "indexDifference": 1, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [0,4,7,2], "indexDifference": 2, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [1,3,4,4], "indexDifference": 2, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [1,8,6,10], "indexDifference": 2, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [2,0,9,2], "indexDifference": 2, "valueDifference": 4 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [7,4,8,3], "indexDifference": 3, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [9,4,0,9], "indexDifference": 2, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [9,9,7,7], "indexDifference": 3, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [10,33,39,15], "indexDifference": 1, "valueDifference": 12 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [22,28,50,25], "indexDifference": 0, "valueDifference": 28 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [36,35,45,34], "indexDifference": 1, "valueDifference": 31 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [50,21,32,5], "indexDifference": 0, "valueDifference": 26 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [0,6,2,10,4], "indexDifference": 3, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [0,8,2,5,10], "indexDifference": 0, "valueDifference": 10 }
assert my_solution.findIndices(**test_input) == [0,4]
test_input = { "nums": [0,9,0,5,9], "indexDifference": 3, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [0,9,4,9,9], "indexDifference": 0, "valueDifference": 8 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [3,7,7,10,10], "indexDifference": 2, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [4,0,1,5,0], "indexDifference": 0, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [4,1,7,0,3], "indexDifference": 1, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [6,0,6,7,9], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [6,19,40,41,36], "indexDifference": 0, "valueDifference": 34 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [7,10,4,5,1], "indexDifference": 2, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [1,4]
test_input = { "nums": [9,1,7,8,5], "indexDifference": 2, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [10,39,25,12,7], "indexDifference": 1, "valueDifference": 35 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [15,28,50,37,43], "indexDifference": 2, "valueDifference": 46 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [0,6,6,0,0,6], "indexDifference": 2, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [1,7,4,4,5,3], "indexDifference": 0, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,0]
test_input = { "nums": [3,3,1,6,3,3], "indexDifference": 3, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [3,3,4,1,3,7], "indexDifference": 3, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [4,3,8,6,2,8], "indexDifference": 2, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [6,1,2,9,6,5], "indexDifference": 1, "valueDifference": 1 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [6,5,5,10,4,3], "indexDifference": 1, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [1,3]
test_input = { "nums": [6,7,0,2,2,6], "indexDifference": 1, "valueDifference": 0 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [7,6,3,5,9,5], "indexDifference": 2, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [2,4]
test_input = { "nums": [9,1,0,4,3,3], "indexDifference": 4, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,4]
test_input = { "nums": [9,8,8,0,1,4], "indexDifference": 3, "valueDifference": 9 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [25,49,11,15,32,33], "indexDifference": 2, "valueDifference": 37 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [0,44,42,26,15,37,23], "indexDifference": 0, "valueDifference": 17 }
assert my_solution.findIndices(**test_input) == [0,1]
test_input = { "nums": [3,5,3,44,8,47,10], "indexDifference": 3, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [3,8,8,0,2,0,10], "indexDifference": 4, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,5]
test_input = { "nums": [5,1,0,7,5,2,0], "indexDifference": 4, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,5]
test_input = { "nums": [9,10,1,3,1,5,0], "indexDifference": 2, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [13,3,15,48,32,19,33], "indexDifference": 1, "valueDifference": 25 }
assert my_solution.findIndices(**test_input) == [1,3]
test_input = { "nums": [29,12,42,16,50,22,10], "indexDifference": 2, "valueDifference": 29 }
assert my_solution.findIndices(**test_input) == [1,4]
test_input = { "nums": [30,48,12,15,43,17,13], "indexDifference": 3, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,3]
test_input = { "nums": [31,33,46,4,13,45,37], "indexDifference": 0, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,2]
test_input = { "nums": [43,37,25,44,45,21,47], "indexDifference": 1, "valueDifference": 37 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [0,4,0,4,1,2,1,9], "indexDifference": 1, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [0,7]
test_input = { "nums": [0,7,3,2,6,1,7,4], "indexDifference": 4, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [0,4]
test_input = { "nums": [2,10,2,0,6,0,7,7], "indexDifference": 2, "valueDifference": 3 }
assert my_solution.findIndices(**test_input) == [1,3]
test_input = { "nums": [3,9,6,10,10,3,2,9], "indexDifference": 5, "valueDifference": 10 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [4,4,3,0,6,1,7,8], "indexDifference": 3, "valueDifference": 6 }
assert my_solution.findIndices(**test_input) == [3,6]
test_input = { "nums": [4,6,3,4,2,4,5,2], "indexDifference": 2, "valueDifference": 5 }
assert my_solution.findIndices(**test_input) == [-1,-1]
test_input = { "nums": [6,5,6,3,0,10,9,8], "indexDifference": 2, "valueDifference": 2 }
assert my_solution.findIndices(**test_input) == [1,3]
test_input = { "nums": [7,4,3,1,6,6,2,6], "indexDifference": 3, "valueDifference": 7 }
assert my_solution.findIndices(**test_input) == [-1,-1] | 1,697,337,000 |
weekly-contest-367-construct-product-matrix | https://leetcode.com/problems/construct-product-matrix | construct-product-matrix | {
"questionId": "3031",
"questionFrontendId": "2906",
"title": "Construct Product Matrix",
"titleSlug": "construct-product-matrix",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 187,
"dislikes": 11,
"categoryTitle": "Algorithms"
} | """
Given a 0-indexed 2D integer matrix grid of size n * m, we define a 0-indexed 2D matrix p of size n * m as the product matrix of grid if the following condition is met:
* Each element p[i][j] is calculated as the product of all elements in grid except for the element grid[i][j]. This product is then taken modulo 12345.
Return the product matrix of grid.
Example 1:
Input: grid = [[1,2],[3,4]]
Output: [[24,12],[8,6]]
Explanation: p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24
p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12
p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8
p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6
So the answer is [[24,12],[8,6]].
Example 2:
Input: grid = [[12345],[2],[1]]
Output: [[2],[0],[0]]
Explanation: p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2.
p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0.
p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0.
So the answer is [[2],[0],[0]].
Constraints:
* 1 <= n == grid.length <= 105
* 1 <= m == grid[i].length <= 105
* 2 <= n * m <= 105
* 1 <= grid[i][j] <= 109
"""
class Solution:
def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
| Given a 0-indexed 2D integer matrix grid of size n * m, we define a 0-indexed 2D matrix p of size n * m as the product matrix of grid if the following condition is met:
* Each element p[i][j] is calculated as the product of all elements in grid except for the element grid[i][j]. This product is then taken modulo 12345.
Return the product matrix of grid.
Example 1:
Input: grid = [[1,2],[3,4]]
Output: [[24,12],[8,6]]
Explanation: p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24
p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12
p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8
p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6
So the answer is [[24,12],[8,6]].
Example 2:
Input: grid = [[12345],[2],[1]]
Output: [[2],[0],[0]]
Explanation: p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2.
p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0.
p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0.
So the answer is [[2],[0],[0]].
Constraints:
* 1 <= n == grid.length <= 105
* 1 <= m == grid[i].length <= 105
* 2 <= n * m <= 105
* 1 <= grid[i][j] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
``` |
my_solution = Solution()
test_input = { "grid": [[1,2],[3,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[24,12],[8,6]]
test_input = { "grid": [[12345],[2],[1]] }
assert my_solution.constructProductMatrix(**test_input) == [[2],[0],[0]]
test_input = { "grid": [[1],[2]] }
assert my_solution.constructProductMatrix(**test_input) == [[2],[1]]
test_input = { "grid": [[1,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[2,1]]
test_input = { "grid": [[12345,12345]] }
assert my_solution.constructProductMatrix(**test_input) == [[0,0]]
test_input = { "grid": [[1],[4]] }
assert my_solution.constructProductMatrix(**test_input) == [[4],[1]]
test_input = { "grid": [[3],[4]] }
assert my_solution.constructProductMatrix(**test_input) == [[4],[3]]
test_input = { "grid": [[4],[3]] }
assert my_solution.constructProductMatrix(**test_input) == [[3],[4]]
test_input = { "grid": [[1,1,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[1,1,1]]
test_input = { "grid": [[2,1,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[1,2,2]]
test_input = { "grid": [[3],[5],[2]] }
assert my_solution.constructProductMatrix(**test_input) == [[10],[6],[15]]
test_input = { "grid": [[1,2],[1,1],[6,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[48,24],[48,48],[8,12]]
test_input = { "grid": [[1,2,2],[1,4,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[48,24,24],[48,12,16]]
test_input = { "grid": [[2],[7],[2],[6]] }
assert my_solution.constructProductMatrix(**test_input) == [[84],[24],[84],[28]]
test_input = { "grid": [[3],[4],[7],[7]] }
assert my_solution.constructProductMatrix(**test_input) == [[196],[147],[84],[84]]
test_input = { "grid": [[3,1,1],[1,3,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[12,36,36],[36,12,9]]
test_input = { "grid": [[4],[8],[3],[7]] }
assert my_solution.constructProductMatrix(**test_input) == [[168],[84],[224],[96]]
test_input = { "grid": [[5],[8],[8],[3]] }
assert my_solution.constructProductMatrix(**test_input) == [[192],[120],[120],[320]]
test_input = { "grid": [[6],[5],[8],[5]] }
assert my_solution.constructProductMatrix(**test_input) == [[200],[240],[150],[240]]
test_input = { "grid": [[8],[1],[3],[8]] }
assert my_solution.constructProductMatrix(**test_input) == [[24],[192],[64],[24]]
test_input = { "grid": [[1],[10],[3],[10],[9]] }
assert my_solution.constructProductMatrix(**test_input) == [[2700],[270],[900],[270],[300]]
test_input = { "grid": [[1,1,1,1,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[1,1,1,1,1]]
test_input = { "grid": [[1,1,2,2,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[4,4,2,2,4]]
test_input = { "grid": [[1,2,3],[3,3,5],[3,4,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[6480,3240,2160],[2160,2160,1296],[2160,1620,3240]]
test_input = { "grid": [[2],[7],[5],[3],[4]] }
assert my_solution.constructProductMatrix(**test_input) == [[420],[120],[168],[280],[210]]
test_input = { "grid": [[2,2,2,2,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,8,8,8,16]]
test_input = { "grid": [[2,2,4,4],[3,2,1,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[768,768,384,384],[512,768,1536,384]]
test_input = { "grid": [[2,4,1,1],[3,4,4,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[192,96,384,384],[128,96,96,384]]
test_input = { "grid": [[3,1,1,4],[1,4,1,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[16,48,48,12],[48,12,48,48]]
test_input = { "grid": [[3,2,5],[6,4,3],[6,3,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[615,7095,7776],[6480,9720,615],[6480,615,1845]]
test_input = { "grid": [[5,5,5],[4,3,1],[4,5,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[6000,6000,6000],[7500,10000,5310],[7500,6000,5310]]
test_input = { "grid": [[6,3],[1,5],[2,7],[6,5]] }
assert my_solution.constructProductMatrix(**test_input) == [[6300,255],[765,7560],[6555,5400],[6300,7560]]
test_input = { "grid": [[6,3,2],[2,3,1],[5,5,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[3600,7200,10800],[10800,7200,9255],[4320,4320,5400]]
test_input = { "grid": [[6,5,3],[4,4,5],[3,2,5]] }
assert my_solution.constructProductMatrix(**test_input) == [[11310,6165,10275],[4620,4620,6165],[10275,9240,6165]]
test_input = { "grid": [[8],[5],[5],[9],[8]] }
assert my_solution.constructProductMatrix(**test_input) == [[1800],[2880],[2880],[1600],[1800]]
test_input = { "grid": [[10],[5],[6],[8],[6]] }
assert my_solution.constructProductMatrix(**test_input) == [[1440],[2880],[2400],[1800],[2400]]
test_input = { "grid": [[10],[9],[3],[4],[3]] }
assert my_solution.constructProductMatrix(**test_input) == [[324],[360],[1080],[810],[1080]]
test_input = { "grid": [[1,1,1,2,2,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[4,4,4,2,2,4]]
test_input = { "grid": [[1,1,2,1,2,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[4,4,2,4,2,4]]
test_input = { "grid": [[1,1,2,1,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,8,4,8,4,4]]
test_input = { "grid": [[1,1,2,2,1,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,8,4,4,8,4]]
test_input = { "grid": [[1,1,2,2,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[16,16,8,8,8,8]]
test_input = { "grid": [[1,1,3,3],[3,4,4,2],[6,6,3,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[2898,2898,966,966],[966,6897,6897,1449],[483,483,966,6897]]
test_input = { "grid": [[1,2,1,1,1,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[4,2,4,4,4,2]]
test_input = { "grid": [[1,2,1,1,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,4,8,8,4,4]]
test_input = { "grid": [[1,2,2,4,3],[3,4,1,4,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[4608,2304,2304,1152,1536],[1536,1152,4608,1152,2304]]
test_input = { "grid": [[1,3,1,3,1],[2,1,3,2,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[324,108,324,108,324],[162,324,108,162,108]]
test_input = { "grid": [[1,3,3,3,4],[2,2,1,4,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[6912,2304,2304,2304,1728],[3456,3456,6912,1728,1728]]
test_input = { "grid": [[1,4,5],[1,3,2],[7,2,6],[6,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[7365,11100,11349],[7365,6570,9855],[9870,9855,3285],[3285,9855,9855]]
test_input = { "grid": [[1,6,6,4],[4,1,2,5],[5,4,6,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[12105,8190,8190,12285],[12285,12105,12225,4890],[4890,12285,8190,12225]]
test_input = { "grid": [[2,1,3,2,1],[3,1,3,1,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[108,216,72,108,216],[72,216,72,216,108]]
test_input = { "grid": [[2,2,2,1,1,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[4,4,4,8,8,8]]
test_input = { "grid": [[2,2,2,2,1,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[16,16,16,16,32,16]]
test_input = { "grid": [[2,2,3,2,3],[3,1,3,2,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[2592,2592,1728,2592,1728],[1728,5184,1728,2592,1296]]
test_input = { "grid": [[2,3,4],[2,4,2],[1,8,1],[8,8,8]] }
assert my_solution.constructProductMatrix(**test_input) == [[8697,5798,10521],[8697,10521,8697],[5049,11433,5049],[11433,11433,11433]]
test_input = { "grid": [[2,4,4,5],[5,5,1,4],[4,2,2,5]] }
assert my_solution.constructProductMatrix(**test_input) == [[10405,11375,11375,9100],[9100,9100,8465,11375],[11375,10405,10405,9100]]
test_input = { "grid": [[3,4,1,1],[3,1,4,4],[1,5,1,5]] }
assert my_solution.constructProductMatrix(**test_input) == [[4800,3600,2055,2055],[4800,2055,3600,3600],[2055,2880,2055,2880]]
test_input = { "grid": [[3,4,6,3],[4,5,4,4],[5,2,6,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[11625,11805,11985,11625],[11805,6975,11805,11805],[6975,11265,11985,11625]]
test_input = { "grid": [[4,1,4,4,4],[2,2,2,3,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[4608,6087,4608,4608,4608],[9216,9216,9216,6144,6144]]
test_input = { "grid": [[4,8,8],[6,2,5],[7,3,7],[6,3,5]] }
assert my_solution.constructProductMatrix(**test_input) == [[3525,7935,7935],[6465,7050,2820],[7305,585,7305],[6465,585,2820]]
test_input = { "grid": [[6],[8],[2],[12],[6],[4]] }
assert my_solution.constructProductMatrix(**test_input) == [[4608],[3456],[1479],[2304],[4608],[6912]]
test_input = { "grid": [[6,2,5,2],[5,5,2,3],[4,6,5,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[3990,11970,12195,11970],[12195,12195,11970,7980],[5985,3990,12195,11970]]
test_input = { "grid": [[6,3,4,3],[6,4,2,5],[3,3,6,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[9795,7245,8520,7245],[9795,8520,4695,4347],[7245,7245,9795,9390]]
test_input = { "grid": [[6,5,6,6],[3,2,6,4],[1,3,4,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[2415,2898,2415,2415],[4830,7245,2415,9795],[2145,4830,9795,2145]]
test_input = { "grid": [[7],[11],[12],[7],[12],[7]] }
assert my_solution.constructProductMatrix(**test_input) == [[3546],[12],[8241],[3546],[8241],[3546]]
test_input = { "grid": [[7,4,5],[2,1,2],[5,3,8],[3,6,7]] }
assert my_solution.constructProductMatrix(**test_input) == [[12135,5805,2175],[11610,10875,11610],[2175,7740,9075],[7740,3870,12135]]
test_input = { "grid": [[8],[6],[7],[2],[7],[4]] }
assert my_solution.constructProductMatrix(**test_input) == [[2352],[3136],[2688],[9408],[2688],[4704]]
test_input = { "grid": [[8],[6],[7],[5],[3],[6]] }
assert my_solution.constructProductMatrix(**test_input) == [[3780],[5040],[4320],[6048],[10080],[5040]]
test_input = { "grid": [[8,1],[9,6],[2,4],[1,3],[3,6]] }
assert my_solution.constructProductMatrix(**test_input) == [[10983,1449],[8391,6414],[6897,9621],[1449,483],[483,6414]]
test_input = { "grid": [[8,4,3],[7,7,4],[1,2,3],[5,2,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[8955,5565,11535],[3180,3180,5565],[9915,11130,11535],[1983,11130,5565]]
test_input = { "grid": [[12],[8],[4],[3],[9],[5]] }
assert my_solution.constructProductMatrix(**test_input) == [[4320],[6480],[615],[4935],[5760],[10368]]
test_input = { "grid": [[1,1,1,1,2,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,8,8,8,4,4,4]]
test_input = { "grid": [[1,1,1,2,1,1,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[2,2,2,1,2,2,2]]
test_input = { "grid": [[1,1,2,1,2,1,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,8,4,8,4,8,4]]
test_input = { "grid": [[1,1,6,5,6],[1,6,6,2,5],[3,4,1,1,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[11805,11805,12255,4830,12255],[11805,12255,12255,12075,4830],[12165,12210,11805,11805,12210]]
test_input = { "grid": [[1,2,1,1,1,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,4,8,8,8,4,4]]
test_input = { "grid": [[1,2,1,2,2,1,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[8,4,8,4,4,8,8]]
test_input = { "grid": [[1,3,1,2,1,2],[3,2,3,4,2,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[5184,1728,5184,2592,5184,2592],[1728,2592,1728,1296,2592,1728]]
test_input = { "grid": [[1,3,5,2],[1,3,6,5],[8,1,2,7],[6,2,3,5]] }
assert my_solution.constructProductMatrix(**test_input) == [[2895,9195,10455,7620],[2895,9195,10770,10455],[1905,2895,7620,10995],[10770,7620,9195,10455]]
test_input = { "grid": [[1,4,4,4,3,3],[1,1,3,4,4,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[2958,6912,6912,6912,9216,9216],[2958,2958,9216,6912,6912,2958]]
test_input = { "grid": [[1,5,5,6],[4,7,6,1],[2,3,3,6],[3,6,3,7]] }
assert my_solution.constructProductMatrix(**test_input) == [[6570,11190,11190,1095],[7815,11520,1095,6570],[3285,2190,2190,1095],[2190,1095,2190,11520]]
test_input = { "grid": [[1,6,4,4,1],[6,2,5,1,4],[6,4,3,5,6]] }
assert my_solution.constructProductMatrix(**test_input) == [[3705,10905,10185,10185,3705],[10905,8025,3210,3705,10185],[10905,10185,9465,3210,10905]]
test_input = { "grid": [[1,7,2,8],[3,7,2,5],[2,3,5,6],[5,4,2,7]] }
assert my_solution.constructProductMatrix(**test_input) == [[4065,7635,8205,11310],[9585,7635,8205,8220],[8205,9585,8220,10965],[8220,10275,8205,7635]]
test_input = { "grid": [[2,1,1,1,1,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[4,8,8,8,8,4,4]]
test_input = { "grid": [[2,1,1,1,2,2,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[4,8,8,8,4,4,8]]
test_input = { "grid": [[2,1,2,2,2,2,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[32,64,32,32,32,32,32]]
test_input = { "grid": [[2,1,3,2,4,4],[1,3,2,2,4,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[4608,9216,3072,4608,2304,2304],[9216,3072,4608,4608,2304,9216]]
test_input = { "grid": [[2,2,2,2,1,2,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[16,16,16,16,32,16,32]]
test_input = { "grid": [[2,4,3,3,3,4],[3,1,4,3,4,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[966,483,8874,8874,8874,483],[8874,1932,483,8874,483,966]]
test_input = { "grid": [[2,5,4,8],[4,6,3,3],[1,5,1,4],[8,6,6,5]] }
assert my_solution.constructProductMatrix(**test_input) == [[30,4950,15,6180],[15,4125,8250,8250],[60,4950,60,15],[6180,4125,4125,4950]]
test_input = { "grid": [[2,7],[10,12],[2,4],[8,11],[2,12],[11,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[8340,5910],[6606,5505],[8340,4170],[2085,8250],[8340,5505],[8250,8340]]
test_input = { "grid": [[2,9],[1,4],[10,12],[2,7],[4,10],[10,8]] }
assert my_solution.constructProductMatrix(**test_input) == [[3435,10365],[6870,7890],[5625,10860],[3435,2745],[7890,5625],[5625,3945]]
test_input = { "grid": [[3],[9],[5],[1],[4],[14],[12]] }
assert my_solution.constructProductMatrix(**test_input) == [[5550],[10080],[5799],[4305],[10335],[6480],[7560]]
test_input = { "grid": [[3,2,2,2,4,3],[4,1,3,1,1,2]] }
assert my_solution.constructProductMatrix(**test_input) == [[2304,3456,3456,3456,1728,2304],[1728,6912,2304,6912,6912,3456]]
test_input = { "grid": [[3,2,4,2,2,3],[2,2,4,1,2,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[9216,1479,6912,1479,1479,9216],[1479,1479,6912,2958,1479,9216]]
test_input = { "grid": [[3,6,2,2,5],[1,4,3,4,1],[4,5,2,4,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[7590,3795,11385,11385,2085],[10425,11865,7590,11865,10425],[11865,2085,11385,11865,11865]]
test_input = { "grid": [[4,1,5,4,5],[6,5,3,4,4],[3,2,6,2,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[6945,3090,8025,6945,8025],[8745,8025,5145,6945,6945],[5145,1545,8745,1545,5145]]
test_input = { "grid": [[4,3,9],[3,9,10],[9,7,8],[8,4,7],[6,1,3]] }
assert my_solution.constructProductMatrix(**test_input) == [[3255,225,75],[225,75,11178],[75,1860,7800],[7800,3255,1860],[6285,675,225]]
test_input = { "grid": [[5,1,1,1,5],[3,4,2,6,6],[3,3,2,5,1]] }
assert my_solution.constructProductMatrix(**test_input) == [[6105,5835,5835,5835,6105],[6060,4545,9090,3030,3030],[6060,6060,9090,6105,5835]]
test_input = { "grid": [[5,3],[9,2],[2,6],[4,9],[12,2],[10,4]] }
assert my_solution.constructProductMatrix(**test_input) == [[1050,5865],[10185,2625],[2625,9105],[7485,10185],[10725,2625],[525,7485]] | 1,697,337,000 |
biweekly-contest-115-last-visited-integers | https://leetcode.com/problems/last-visited-integers | last-visited-integers | {
"questionId": "3164",
"questionFrontendId": "2899",
"title": "Last Visited Integers",
"titleSlug": "last-visited-integers",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 74,
"dislikes": 139,
"categoryTitle": "Algorithms"
} | """
Given a 0-indexed array of strings words where words[i] is either a positive integer represented as a string or the string "prev".
Start iterating from the beginning of the array; for every "prev" string seen in words, find the last visited integer in words which is defined as follows:
* Let k be the number of consecutive "prev" strings seen so far (containing the current string). Let nums be the 0-indexed array of integers seen so far and nums_reverse be the reverse of nums, then the integer at (k - 1)th index of nums_reverse will be the last visited integer for this "prev".
* If k is greater than the total visited integers, then the last visited integer will be -1.
Return an integer array containing the last visited integers.
Example 1:
Input: words = ["1","2","prev","prev","prev"]
Output: [2,1,-1]
Explanation:
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element.
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.
Example 2:
Input: words = ["1","prev","2","prev","prev"]
Output: [1,2,1]
Explanation:
For "prev" at index = 1, last visited integer will be 1.
For "prev" at index = 3, last visited integer will be 2.
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
Constraints:
* 1 <= words.length <= 100
* words[i] == "prev" or 1 <= int(words[i]) <= 100
"""
class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
| Given a 0-indexed array of strings words where words[i] is either a positive integer represented as a string or the string "prev".
Start iterating from the beginning of the array; for every "prev" string seen in words, find the last visited integer in words which is defined as follows:
* Let k be the number of consecutive "prev" strings seen so far (containing the current string). Let nums be the 0-indexed array of integers seen so far and nums_reverse be the reverse of nums, then the integer at (k - 1)th index of nums_reverse will be the last visited integer for this "prev".
* If k is greater than the total visited integers, then the last visited integer will be -1.
Return an integer array containing the last visited integers.
Example 1:
Input: words = ["1","2","prev","prev","prev"]
Output: [2,1,-1]
Explanation:
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element.
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.
Example 2:
Input: words = ["1","prev","2","prev","prev"]
Output: [1,2,1]
Explanation:
For "prev" at index = 1, last visited integer will be 1.
For "prev" at index = 3, last visited integer will be 2.
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
Constraints:
* 1 <= words.length <= 100
* words[i] == "prev" or 1 <= int(words[i]) <= 100
Please complete the code below to solve above prblem:
```python
class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "words": ["1","2","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [2,1,-1]
test_input = { "words": ["1","prev","2","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [1,2,1]
test_input = { "words": ["prev","prev","prev","27"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1]
test_input = { "words": ["17","42"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]
test_input = { "words": ["prev","prev","prev","52","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,52]
test_input = { "words": ["prev","prev","68","prev","prev","53","40","23","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,68,-1,23]
test_input = { "words": ["99","23","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [23]
test_input = { "words": ["prev","prev","prev","58","99","prev","10","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,99,10]
test_input = { "words": ["prev","51","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,51,-1]
test_input = { "words": ["prev","46","9","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,9]
test_input = { "words": ["prev","prev","prev","prev","prev","26"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1]
test_input = { "words": ["prev","21","prev","76","82","prev","96","prev","57","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,21,82,96,57]
test_input = { "words": ["52","4","prev","prev","prev","69"] }
assert my_solution.lastVisitedIntegers(**test_input) == [4,52,-1]
test_input = { "words": ["24","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [24]
test_input = { "words": ["46","prev","78","prev","83","21","prev","94","50"] }
assert my_solution.lastVisitedIntegers(**test_input) == [46,78,21]
test_input = { "words": ["14","66","prev","prev","46","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [66,14,46]
test_input = { "words": ["35","90"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["prev","9","prev","8","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,9,8]
test_input = { "words": ["prev","prev","88","71","47","65","24","39"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]
test_input = { "words": ["45","73","78","2","54","prev","85","62","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [54,62]
test_input = { "words": ["prev","prev","80","9","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,9]
test_input = { "words": ["79","19","prev","prev","prev","67","prev","16","2"] }
assert my_solution.lastVisitedIntegers(**test_input) == [19,79,-1,67]
test_input = { "words": ["94","prev","prev","prev","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [94,-1,-1,-1,-1,-1]
test_input = { "words": ["prev","prev","prev","82","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,82]
test_input = { "words": ["94","14","81","43","prev","43","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [43,43]
test_input = { "words": ["prev","prev","94","56","prev","32","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,56,32,56,94]
test_input = { "words": ["93"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["46","91","3","40","31","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [31]
test_input = { "words": ["41","prev","17","58","78"] }
assert my_solution.lastVisitedIntegers(**test_input) == [41]
test_input = { "words": ["prev","prev","82","41","96","89","71"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]
test_input = { "words": ["4","prev","50","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [4,50,4]
test_input = { "words": ["59","76","prev","29","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [76,29]
test_input = { "words": ["prev","62"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]
test_input = { "words": ["6","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [6]
test_input = { "words": ["prev","prev","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1]
test_input = { "words": ["28","5","35","prev","41","27","70","65","84"] }
assert my_solution.lastVisitedIntegers(**test_input) == [35]
test_input = { "words": ["94","45","prev","61"] }
assert my_solution.lastVisitedIntegers(**test_input) == [45]
test_input = { "words": ["prev","34","prev","prev","prev","prev","21","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,34,-1,-1,-1,21]
test_input = { "words": ["prev","12","100","33","prev","85","93"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,33]
test_input = { "words": ["26"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["27","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [27,-1,-1]
test_input = { "words": ["prev","prev","22","33","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,33,22,-1]
test_input = { "words": ["30","prev","87","prev","19","prev","8","prev","81"] }
assert my_solution.lastVisitedIntegers(**test_input) == [30,87,19,8]
test_input = { "words": ["35","prev","47","82","86","84","prev","76","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [35,84,76]
test_input = { "words": ["prev","87"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]
test_input = { "words": ["prev","69","78","prev","prev","16"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,78,69]
test_input = { "words": ["22","97","prev","2"] }
assert my_solution.lastVisitedIntegers(**test_input) == [97]
test_input = { "words": ["72","74"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["84","prev","prev","21"] }
assert my_solution.lastVisitedIntegers(**test_input) == [84,-1]
test_input = { "words": ["64","24"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["17","prev","59","prev","51","11","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [17,59,11,51]
test_input = { "words": ["57","prev","27","30","prev","prev","75"] }
assert my_solution.lastVisitedIntegers(**test_input) == [57,30,27]
test_input = { "words": ["65","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [65,-1]
test_input = { "words": ["prev","53","76","54","94","77"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]
test_input = { "words": ["89","51","prev","prev","12","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [51,89,12,51]
test_input = { "words": ["prev","28","25","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,25,28,-1]
test_input = { "words": ["51","prev","prev","76"] }
assert my_solution.lastVisitedIntegers(**test_input) == [51,-1]
test_input = { "words": ["2","24","63","prev","43","19","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [63,19]
test_input = { "words": ["prev","38","1"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]
test_input = { "words": ["56","75","prev","prev","94"] }
assert my_solution.lastVisitedIntegers(**test_input) == [75,56]
test_input = { "words": ["prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1]
test_input = { "words": ["prev","37","25","31","prev","prev","42"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,31,25]
test_input = { "words": ["73","30","prev","20","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [30,20,30]
test_input = { "words": ["85","prev","prev","78","prev","100","8","17","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [85,-1,78,17]
test_input = { "words": ["prev","55","prev","87","19","prev","13","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,55,19,13,19]
test_input = { "words": ["prev","prev","5","prev","prev","prev","80","17"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,5,-1,-1]
test_input = { "words": ["100","3","prev","prev","93","35","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [3,100,35,93]
test_input = { "words": ["75","7"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["prev","prev","prev","prev","prev","prev","71","prev","27"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1,-1,71]
test_input = { "words": ["prev","prev","prev","91","44","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,44]
test_input = { "words": ["prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]
test_input = { "words": ["11","prev","87","prev","prev","94","prev","68"] }
assert my_solution.lastVisitedIntegers(**test_input) == [11,87,11,94]
test_input = { "words": ["78"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["prev","prev","73","prev","prev","27","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,73,-1,27]
test_input = { "words": ["prev","70","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,70,-1]
test_input = { "words": ["68","prev","38","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [68,38,68]
test_input = { "words": ["prev","prev","36"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]
test_input = { "words": ["prev","prev","36","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,36]
test_input = { "words": ["18","58","41","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [41,58]
test_input = { "words": ["prev","prev","35"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]
test_input = { "words": ["prev","72","prev","96","9","50","prev","52"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,72,50]
test_input = { "words": ["92","95","47","48","prev","50","34","prev","prev","46"] }
assert my_solution.lastVisitedIntegers(**test_input) == [48,34,50]
test_input = { "words": ["36","88","15","99","48"] }
assert my_solution.lastVisitedIntegers(**test_input) == []
test_input = { "words": ["93","prev","2","58","83","90","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [93,90]
test_input = { "words": ["prev","68","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,68]
test_input = { "words": ["prev","56","prev","prev","36","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,56,-1,36]
test_input = { "words": ["53","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [53]
test_input = { "words": ["5","29","94","3","48","prev","59","90","prev","69"] }
assert my_solution.lastVisitedIntegers(**test_input) == [48,90]
test_input = { "words": ["89","prev","prev","75","prev","98","80","prev","68","33"] }
assert my_solution.lastVisitedIntegers(**test_input) == [89,-1,75,80]
test_input = { "words": ["prev","93","prev","74","33"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,93]
test_input = { "words": ["prev","prev","10","25","prev","54","prev","prev","prev","76"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,25,54,25,10]
test_input = { "words": ["9","prev","14","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [9,14]
test_input = { "words": ["prev","prev","prev","18","66","92","prev","87","85","25"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,92]
test_input = { "words": ["prev","prev","prev","16","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,16]
test_input = { "words": ["prev","prev","99","prev","82","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,99,82,99]
test_input = { "words": ["prev","5","90","71","prev","prev","61"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,71,90]
test_input = { "words": ["prev","prev","71","54","prev","20","65","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,54,65,20]
test_input = { "words": ["prev","85","prev","93","prev","98","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,85,93,98]
test_input = { "words": ["prev","prev","34","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,34] | 1,697,293,800 |
biweekly-contest-115-longest-unequal-adjacent-groups-subsequence-i | https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i | longest-unequal-adjacent-groups-subsequence-i | {
"questionId": "3143",
"questionFrontendId": "2900",
"title": "Longest Unequal Adjacent Groups Subsequence I",
"titleSlug": "longest-unequal-adjacent-groups-subsequence-i",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 86,
"dislikes": 26,
"categoryTitle": "Algorithms"
} | """
You are given an integer n, a 0-indexed string array words, and a 0-indexed binary array groups, both arrays having length n.
You need to select the longest subsequence from an array of indices [0, 1, ..., n - 1], such that for the subsequence denoted as [i0, i1, ..., ik - 1] having length k, groups[ij] != groups[ij + 1], for each j where 0 < j + 1 < k.
Return a string array containing the words corresponding to the indices (in order) in the selected subsequence. If there are multiple answers, return any of them.
A subsequence of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Note: strings in words may be unequal in length.
Example 1:
Input: n = 3, words = ["e","a","b"], groups = [0,0,1]
Output: ["e","b"]
Explanation: A subsequence that can be selected is [0,2] because groups[0] != groups[2].
So, a valid answer is [words[0],words[2]] = ["e","b"].
Another subsequence that can be selected is [1,2] because groups[1] != groups[2].
This results in [words[1],words[2]] = ["a","b"].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 2.
Example 2:
Input: n = 4, words = ["a","b","c","d"], groups = [1,0,1,1]
Output: ["a","b","c"]
Explanation: A subsequence that can be selected is [0,1,2] because groups[0] != groups[1] and groups[1] != groups[2].
So, a valid answer is [words[0],words[1],words[2]] = ["a","b","c"].
Another subsequence that can be selected is [0,1,3] because groups[0] != groups[1] and groups[1] != groups[3].
This results in [words[0],words[1],words[3]] = ["a","b","d"].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
Constraints:
* 1 <= n == words.length == groups.length <= 100
* 1 <= words[i].length <= 10
* 0 <= groups[i] < 2
* words consists of distinct strings.
* words[i] consists of lowercase English letters.
"""
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
| You are given an integer n, a 0-indexed string array words, and a 0-indexed binary array groups, both arrays having length n.
You need to select the longest subsequence from an array of indices [0, 1, ..., n - 1], such that for the subsequence denoted as [i0, i1, ..., ik - 1] having length k, groups[ij] != groups[ij + 1], for each j where 0 < j + 1 < k.
Return a string array containing the words corresponding to the indices (in order) in the selected subsequence. If there are multiple answers, return any of them.
A subsequence of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Note: strings in words may be unequal in length.
Example 1:
Input: n = 3, words = ["e","a","b"], groups = [0,0,1]
Output: ["e","b"]
Explanation: A subsequence that can be selected is [0,2] because groups[0] != groups[2].
So, a valid answer is [words[0],words[2]] = ["e","b"].
Another subsequence that can be selected is [1,2] because groups[1] != groups[2].
This results in [words[1],words[2]] = ["a","b"].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 2.
Example 2:
Input: n = 4, words = ["a","b","c","d"], groups = [1,0,1,1]
Output: ["a","b","c"]
Explanation: A subsequence that can be selected is [0,1,2] because groups[0] != groups[1] and groups[1] != groups[2].
So, a valid answer is [words[0],words[1],words[2]] = ["a","b","c"].
Another subsequence that can be selected is [0,1,3] because groups[0] != groups[1] and groups[1] != groups[3].
This results in [words[0],words[1],words[3]] = ["a","b","d"].
It is also a valid answer.
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
Constraints:
* 1 <= n == words.length == groups.length <= 100
* 1 <= words[i].length <= 10
* 0 <= groups[i] < 2
* words consists of distinct strings.
* words[i] consists of lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
``` |
my_solution = Solution()
test_input = { "n": 3, "words": ["e","a","b"], "groups": [0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["e","b"]
test_input = { "n": 4, "words": ["a","b","c","d"], "groups": [1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["a","b","c"]
test_input = { "n": 1, "words": ["c"], "groups": [0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c"]
test_input = { "n": 1, "words": ["d"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d"]
test_input = { "n": 1, "words": ["e"], "groups": [0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["e"]
test_input = { "n": 1, "words": ["fe"], "groups": [0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["fe"]
test_input = { "n": 1, "words": ["frl"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["frl"]
test_input = { "n": 1, "words": ["ha"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ha"]
test_input = { "n": 1, "words": ["l"], "groups": [0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["l"]
test_input = { "n": 1, "words": ["n"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n"]
test_input = { "n": 1, "words": ["s"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["s"]
test_input = { "n": 2, "words": ["d","g"], "groups": [0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d","g"]
test_input = { "n": 2, "words": ["lr","h"], "groups": [0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lr"]
test_input = { "n": 2, "words": ["wx","h"], "groups": [0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["wx","h"]
test_input = { "n": 2, "words": ["yw","n"], "groups": [0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["yw","n"]
test_input = { "n": 2, "words": ["z","n"], "groups": [0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z"]
test_input = { "n": 2, "words": ["zr","a"], "groups": [0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["zr"]
test_input = { "n": 3, "words": ["h","vv","kp"], "groups": [0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["h","vv","kp"]
test_input = { "n": 3, "words": ["m","v","y"], "groups": [0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["m","v","y"]
test_input = { "n": 3, "words": ["o","cfy","en"], "groups": [1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["o","cfy"]
test_input = { "n": 3, "words": ["tu","rv","bn"], "groups": [0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["tu"]
test_input = { "n": 4, "words": ["c","f","y","i"], "groups": [1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","f","y"]
test_input = { "n": 4, "words": ["c","w","h","s"], "groups": [0,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","s"]
test_input = { "n": 4, "words": ["d","a","v","b"], "groups": [1,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d","a","b"]
test_input = { "n": 4, "words": ["hh","svj","a","nr"], "groups": [1,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["hh"]
test_input = { "n": 4, "words": ["im","j","xq","cjs"], "groups": [1,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["im","j","cjs"]
test_input = { "n": 4, "words": ["m","dkg","r","h"], "groups": [1,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["m","h"]
test_input = { "n": 4, "words": ["ow","qay","r","j"], "groups": [1,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ow"]
test_input = { "n": 4, "words": ["r","k","pb","x"], "groups": [0,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["r","pb","x"]
test_input = { "n": 4, "words": ["sq","do","bcj","nm"], "groups": [0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["sq","do","nm"]
test_input = { "n": 4, "words": ["sz","mq","j","u"], "groups": [0,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["sz","j","u"]
test_input = { "n": 4, "words": ["x","nf","p","asn"], "groups": [1,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["x"]
test_input = { "n": 4, "words": ["z","tkt","x","swy"], "groups": [1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z","tkt","x"]
test_input = { "n": 5, "words": ["ht","lw","ax","vi","fo"], "groups": [0,0,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ht","ax","vi"]
test_input = { "n": 5, "words": ["mc","kh","x","q","z"], "groups": [0,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["mc","x","z"]
test_input = { "n": 5, "words": ["n","fg","fy","tv","gv"], "groups": [1,1,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n"]
test_input = { "n": 5, "words": ["n","l","e","d","m"], "groups": [1,1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n","e","d"]
test_input = { "n": 5, "words": ["n","m","g","b","d"], "groups": [0,0,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n","g","b"]
test_input = { "n": 5, "words": ["nz","zwt","hig","s","jze"], "groups": [1,1,1,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["nz","s","jze"]
test_input = { "n": 5, "words": ["o","i","b","k","kz"], "groups": [0,0,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["o","b"]
test_input = { "n": 5, "words": ["r","o","k","d","f"], "groups": [0,0,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["r","d"]
test_input = { "n": 5, "words": ["sfh","exd","j","w","gc"], "groups": [1,0,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["sfh","exd","j"]
test_input = { "n": 5, "words": ["v","f","k","l","p"], "groups": [0,0,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["v","k","l"]
test_input = { "n": 5, "words": ["vbd","ua","muo","mu","qi"], "groups": [0,0,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["vbd","mu","qi"]
test_input = { "n": 5, "words": ["we","ch","tl","yx","utx"], "groups": [1,0,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["we","ch","yx"]
test_input = { "n": 5, "words": ["x","vlk","tds","dfn","kr"], "groups": [0,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["x","tds","kr"]
test_input = { "n": 5, "words": ["y","j","u","r","f"], "groups": [0,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","u","f"]
test_input = { "n": 5, "words": ["y","r","z","x","q"], "groups": [0,1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","r","z","x"]
test_input = { "n": 5, "words": ["yc","fgq","gg","og","tca"], "groups": [0,1,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["yc","fgq","tca"]
test_input = { "n": 5, "words": ["z","d","p","c","m"], "groups": [0,0,0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z"]
test_input = { "n": 6, "words": ["c","i","to","kv","op","u"], "groups": [0,0,1,0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","to","kv"]
test_input = { "n": 6, "words": ["d","h","e","k","j","r"], "groups": [0,1,1,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d","h","k","j","r"]
test_input = { "n": 6, "words": ["l","f","v","b","w","k"], "groups": [1,0,1,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["l","f","v","w"]
test_input = { "n": 6, "words": ["lj","vf","pa","w","z","q"], "groups": [0,0,1,0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lj","pa","w"]
test_input = { "n": 7, "words": ["cd","oki","ho","oi","m","yvy","i"], "groups": [1,1,0,1,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd","ho","oi"]
test_input = { "n": 7, "words": ["exb","c","oq","lq","xh","zmo","aug"], "groups": [1,1,0,1,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["exb","oq","lq","zmo"]
test_input = { "n": 7, "words": ["f","r","k","h","m","v","p"], "groups": [1,0,0,0,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["f","r","m","v"]
test_input = { "n": 7, "words": ["fd","fc","jm","z","lg","kl","ux"], "groups": [0,1,0,1,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["fd","fc","jm","z","lg","kl","ux"]
test_input = { "n": 7, "words": ["ft","iw","m","v","gx","d","pm"], "groups": [1,1,1,0,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ft","v","gx"]
test_input = { "n": 7, "words": ["lma","i","rt","xar","bfx","np","x"], "groups": [1,1,1,1,1,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lma","np","x"]
test_input = { "n": 7, "words": ["nsv","r","o","qo","pb","xqv","clb"], "groups": [1,1,0,0,0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["nsv","o"]
test_input = { "n": 7, "words": ["p","qdb","zcd","l","tv","ln","ogb"], "groups": [1,1,0,1,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["p","zcd","l","tv","ogb"]
test_input = { "n": 7, "words": ["z","cee","j","jqu","w","ljr","k"], "groups": [1,0,1,1,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z","cee","j","w","k"]
test_input = { "n": 8, "words": ["h","p","q","t","j","a","c","n"], "groups": [0,1,1,1,0,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["h","p","j","c"]
test_input = { "n": 8, "words": ["r","v","c","t","d","a","x","o"], "groups": [1,1,0,1,1,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["r","c","t","a","o"]
test_input = { "n": 8, "words": ["u","l","a","y","j","s","h","q"], "groups": [0,0,0,0,0,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["u","s","h"]
test_input = { "n": 8, "words": ["x","mr","yyf","l","z","q","zvj","zqt"], "groups": [0,1,1,1,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["x","mr","z","q","zqt"]
test_input = { "n": 8, "words": ["y","x","i","xtm","ze","n","cma","dgk"], "groups": [0,1,0,0,1,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","x","i","ze","cma"]
test_input = { "n": 8, "words": ["yun","x","zpp","bpr","ii","ezg","dn","k"], "groups": [0,1,1,1,1,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["yun","x","ezg","dn","k"]
test_input = { "n": 9, "words": ["ckr","iz","top","of","sb","wv","hb","da","wd"], "groups": [1,1,0,1,1,0,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ckr","top","of","wv","wd"]
test_input = { "n": 9, "words": ["g","h","u","n","w","o","f","p","m"], "groups": [1,0,0,1,1,0,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["g","h","n","o","p","m"]
test_input = { "n": 9, "words": ["ilw","t","dyy","irz","oxy","k","rfj","hi","zxe"], "groups": [1,1,1,0,1,1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ilw","irz","oxy","rfj","hi"]
test_input = { "n": 9, "words": ["l","iuz","d","tfw","mu","a","rp","mrb","wnl"], "groups": [1,1,1,1,1,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["l","a","rp","wnl"]
test_input = { "n": 9, "words": ["mc","b","yr","cj","zk","wi","esm","yu","cw"], "groups": [0,0,1,1,0,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["mc","yr","zk","esm","cw"]
test_input = { "n": 9, "words": ["nw","hx","ygc","vjo","jmv","p","juv","b","y"], "groups": [0,1,0,0,1,0,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["nw","hx","ygc","jmv","p","b","y"]
test_input = { "n": 9, "words": ["osq","qiw","h","tc","xg","tvt","fqp","zq","b"], "groups": [0,0,1,0,1,1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["osq","h","tc","xg","fqp","zq"]
test_input = { "n": 9, "words": ["vr","lw","e","g","dz","kf","qe","h","p"], "groups": [1,0,0,1,1,0,0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["vr","lw","g","kf"]
test_input = { "n": 10, "words": ["gy","nd","l","hr","i","qf","zz","nq","e","oa"], "groups": [0,1,0,0,1,0,1,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["gy","nd","l","i","qf","zz","oa"]
test_input = { "n": 10, "words": ["j","r","h","t","z","b","a","s","v","q"], "groups": [1,0,1,1,1,1,0,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["j","r","h","a","q"]
test_input = { "n": 10, "words": ["k","f","u","h","x","w","c","e","l","p"], "groups": [0,1,1,1,1,1,1,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["k","f","p"]
test_input = { "n": 10, "words": ["lj","huy","lg","h","o","b","ava","ay","r","us"], "groups": [1,1,1,1,0,0,1,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lj","o","ava"]
test_input = { "n": 10, "words": ["m","d","xv","dp","nq","xi","e","g","n","qw"], "groups": [1,0,1,1,1,1,0,1,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["m","d","xv","e","g","n","qw"]
test_input = { "n": 10, "words": ["n","c","y","h","w","m","g","t","x","v"], "groups": [1,1,1,0,0,1,0,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n","h","m","g","v"]
test_input = { "n": 10, "words": ["o","w","l","g","m","x","f","q","c","v"], "groups": [1,1,1,0,1,1,1,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["o","g","m","q","v"]
test_input = { "n": 10, "words": ["p","mw","m","xld","j","jv","n","so","pkd","rwt"], "groups": [0,0,1,0,1,1,0,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["p","m","xld","j","n","pkd"]
test_input = { "n": 10, "words": ["vyv","msl","d","bu","ubl","bgk","sz","njv","pf","s"], "groups": [1,0,1,1,0,0,1,0,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["vyv","msl","d","ubl","sz","njv","pf","s"]
test_input = { "n": 10, "words": ["y","mz","lt","ur","o","m","djh","tb","w","j"], "groups": [0,0,1,0,1,1,0,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","lt","ur","o","djh","tb","w"]
test_input = { "n": 10, "words": ["y","s","i","v","a","w","l","q","k","t"], "groups": [0,1,1,1,0,1,1,1,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","s","a","w","k"]
test_input = { "n": 11, "words": ["a","tea","ldt","ybm","zkw","r","d","dms","le","u","ze"], "groups": [1,1,0,0,0,1,1,1,1,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["a","ldt","r","u","ze"]
test_input = { "n": 11, "words": ["c","o","e","r","x","w","b","d","h","y","z"], "groups": [1,0,1,0,1,0,1,1,1,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","o","e","r","x","w","b","y","z"]
test_input = { "n": 11, "words": ["chu","a","qdx","fgd","qe","bqc","x","kbx","sv","ly","br"], "groups": [1,0,0,0,0,0,1,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["chu","a","x","kbx","sv","br"]
test_input = { "n": 11, "words": ["ec","jdf","b","wa","kjd","bb","ty","yi","ybw","ilj","cv"], "groups": [0,1,0,1,1,1,1,1,1,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ec","jdf","b","wa","ilj","cv"]
test_input = { "n": 11, "words": ["ew","isn","fl","mg","pdg","d","p","hh","e","y","whm"], "groups": [0,0,1,1,0,0,0,0,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ew","fl","pdg","y"]
test_input = { "n": 11, "words": ["h","o","d","y","r","c","p","b","g","j","k"], "groups": [1,1,0,1,1,0,1,1,0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["h","d","y","c","p","g"]
test_input = { "n": 11, "words": ["ipr","l","zy","j","h","hdt","m","d","pd","nv","wy"], "groups": [1,1,1,1,0,1,0,1,1,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ipr","h","hdt","m","d"]
test_input = { "n": 11, "words": ["j","g","go","a","f","bg","o","l","ze","kq","w"], "groups": [0,0,1,0,1,0,0,0,1,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["j","go","a","f","bg","ze","kq","w"]
test_input = { "n": 11, "words": ["j","r","a","g","x","b","y","v","k","i","c"], "groups": [0,1,0,0,0,0,1,1,0,0,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["j","r","a","y","k"]
test_input = { "n": 11, "words": ["kgo","han","nlu","tv","us","pk","xw","cxc","eml","v","msz"], "groups": [1,0,0,1,0,0,1,0,1,1,0] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["kgo","han","tv","us","xw","cxc","eml","msz"]
test_input = { "n": 11, "words": ["kh","op","ij","te","hk","pmt","v","ne","en","b","zuj"], "groups": [0,0,1,1,1,0,1,1,0,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["kh","ij","pmt","v","en","b"]
test_input = { "n": 11, "words": ["ms","t","oz","x","pw","ik","d","gj","z","ps","i"], "groups": [1,1,0,1,0,0,1,1,0,0,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ms","oz","x","pw","d","z","i"] | 1,697,293,800 |
biweekly-contest-115-longest-unequal-adjacent-groups-subsequence-ii | https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-ii | longest-unequal-adjacent-groups-subsequence-ii | {
"questionId": "3142",
"questionFrontendId": "2901",
"title": "Longest Unequal Adjacent Groups Subsequence II",
"titleSlug": "longest-unequal-adjacent-groups-subsequence-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 172,
"dislikes": 16,
"categoryTitle": "Algorithms"
} | """
You are given an integer n, a 0-indexed string array words, and a 0-indexed array groups, both arrays having length n.
The hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different.
You need to select the longest subsequence from an array of indices [0, 1, ..., n - 1], such that for the subsequence denoted as [i0, i1, ..., ik - 1] having length k, the following holds:
* For adjacent indices in the subsequence, their corresponding groups are unequal, i.e., groups[ij] != groups[ij + 1], for each j where 0 < j + 1 < k.
* words[ij] and words[ij + 1] are equal in length, and the hamming distance between them is 1, where 0 < j + 1 < k, for all indices in the subsequence.
Return a string array containing the words corresponding to the indices (in order) in the selected subsequence. If there are multiple answers, return any of them.
A subsequence of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Note: strings in words may be unequal in length.
Example 1:
Input: n = 3, words = ["bab","dab","cab"], groups = [1,2,2]
Output: ["bab","cab"]
Explanation: A subsequence that can be selected is [0,2].
- groups[0] != groups[2]
- words[0].length == words[2].length, and the hamming distance between them is 1.
So, a valid answer is [words[0],words[2]] = ["bab","cab"].
Another subsequence that can be selected is [0,1].
- groups[0] != groups[1]
- words[0].length == words[1].length, and the hamming distance between them is 1.
So, another valid answer is [words[0],words[1]] = ["bab","dab"].
It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2.
Example 2:
Input: n = 4, words = ["a","b","c","d"], groups = [1,2,3,4]
Output: ["a","b","c","d"]
Explanation: We can select the subsequence [0,1,2,3].
It satisfies both conditions.
Hence, the answer is [words[0],words[1],words[2],words[3]] = ["a","b","c","d"].
It has the longest length among all subsequences of indices that satisfy the conditions.
Hence, it is the only answer.
Constraints:
* 1 <= n == words.length == groups.length <= 1000
* 1 <= words[i].length <= 10
* 1 <= groups[i] <= n
* words consists of distinct strings.
* words[i] consists of lowercase English letters.
"""
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
| You are given an integer n, a 0-indexed string array words, and a 0-indexed array groups, both arrays having length n.
The hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different.
You need to select the longest subsequence from an array of indices [0, 1, ..., n - 1], such that for the subsequence denoted as [i0, i1, ..., ik - 1] having length k, the following holds:
* For adjacent indices in the subsequence, their corresponding groups are unequal, i.e., groups[ij] != groups[ij + 1], for each j where 0 < j + 1 < k.
* words[ij] and words[ij + 1] are equal in length, and the hamming distance between them is 1, where 0 < j + 1 < k, for all indices in the subsequence.
Return a string array containing the words corresponding to the indices (in order) in the selected subsequence. If there are multiple answers, return any of them.
A subsequence of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Note: strings in words may be unequal in length.
Example 1:
Input: n = 3, words = ["bab","dab","cab"], groups = [1,2,2]
Output: ["bab","cab"]
Explanation: A subsequence that can be selected is [0,2].
- groups[0] != groups[2]
- words[0].length == words[2].length, and the hamming distance between them is 1.
So, a valid answer is [words[0],words[2]] = ["bab","cab"].
Another subsequence that can be selected is [0,1].
- groups[0] != groups[1]
- words[0].length == words[1].length, and the hamming distance between them is 1.
So, another valid answer is [words[0],words[1]] = ["bab","dab"].
It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2.
Example 2:
Input: n = 4, words = ["a","b","c","d"], groups = [1,2,3,4]
Output: ["a","b","c","d"]
Explanation: We can select the subsequence [0,1,2,3].
It satisfies both conditions.
Hence, the answer is [words[0],words[1],words[2],words[3]] = ["a","b","c","d"].
It has the longest length among all subsequences of indices that satisfy the conditions.
Hence, it is the only answer.
Constraints:
* 1 <= n == words.length == groups.length <= 1000
* 1 <= words[i].length <= 10
* 1 <= groups[i] <= n
* words consists of distinct strings.
* words[i] consists of lowercase English letters.
Please complete the code below to solve above prblem:
```python
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
``` |
my_solution = Solution()
test_input = { "n": 3, "words": ["bab","dab","cab"], "groups": [1,2,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bab","cab"]
test_input = { "n": 4, "words": ["a","b","c","d"], "groups": [1,2,3,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["a","b","c","d"]
test_input = { "n": 1, "words": ["abbbb"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abbbb"]
test_input = { "n": 1, "words": ["ad"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ad"]
test_input = { "n": 1, "words": ["baaccb"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["baaccb"]
test_input = { "n": 1, "words": ["bc"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bc"]
test_input = { "n": 1, "words": ["bdb"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdb"]
test_input = { "n": 1, "words": ["cc"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cc"]
test_input = { "n": 1, "words": ["cd"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd"]
test_input = { "n": 1, "words": ["cdb"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdb"]
test_input = { "n": 1, "words": ["cea"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cea"]
test_input = { "n": 1, "words": ["cebbbb"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cebbbb"]
test_input = { "n": 1, "words": ["da"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["da"]
test_input = { "n": 1, "words": ["daab"], "groups": [1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["daab"]
test_input = { "n": 2, "words": ["adbe","acace"], "groups": [2,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["acace"]
test_input = { "n": 2, "words": ["ba","dc"], "groups": [1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dc"]
test_input = { "n": 2, "words": ["baa","ada"], "groups": [1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ada"]
test_input = { "n": 2, "words": ["bebea","ddecc"], "groups": [1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ddecc"]
test_input = { "n": 2, "words": ["cedbca","db"], "groups": [1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["db"]
test_input = { "n": 2, "words": ["dbcdd","baba"], "groups": [2,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["baba"]
test_input = { "n": 2, "words": ["ddb","bdb"], "groups": [1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdb"]
test_input = { "n": 2, "words": ["dee","bb"], "groups": [2,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bb"]
test_input = { "n": 2, "words": ["ecd","dbeed"], "groups": [1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbeed"]
test_input = { "n": 3, "words": ["aaac","dbede","cbdeee"], "groups": [2,2,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cbdeee"]
test_input = { "n": 3, "words": ["aab","ca","cbd"], "groups": [3,3,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cbd"]
test_input = { "n": 3, "words": ["aeb","bc","abdb"], "groups": [2,3,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abdb"]
test_input = { "n": 3, "words": ["bdb","aaa","ada"], "groups": [2,1,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aaa","ada"]
test_input = { "n": 3, "words": ["cc","aa","dda"], "groups": [2,2,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dda"]
test_input = { "n": 3, "words": ["cc","aba","dbd"], "groups": [1,1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbd"]
test_input = { "n": 3, "words": ["ccd","bb","ccc"], "groups": [1,1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ccd","ccc"]
test_input = { "n": 3, "words": ["cda","bb","bdc"], "groups": [3,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdc"]
test_input = { "n": 3, "words": ["ceea","ade","aeacba"], "groups": [2,1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aeacba"]
test_input = { "n": 3, "words": ["db","ccce","edbac"], "groups": [3,2,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["edbac"]
test_input = { "n": 3, "words": ["dba","bb","aa"], "groups": [2,2,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aa"]
test_input = { "n": 3, "words": ["dbdaad","daca","cdbdb"], "groups": [1,1,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdbdb"]
test_input = { "n": 3, "words": ["dc","bca","ddd"], "groups": [1,3,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ddd"]
test_input = { "n": 3, "words": ["dd","bb","aac"], "groups": [3,3,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aac"]
test_input = { "n": 3, "words": ["dedcc","cbac","dab"], "groups": [1,3,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dab"]
test_input = { "n": 3, "words": ["eee","abecab","dc"], "groups": [2,2,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dc"]
test_input = { "n": 4, "words": ["ac","caa","cda","ba"], "groups": [3,1,2,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["caa","cda"]
test_input = { "n": 4, "words": ["bab","bac","dbd","dd"], "groups": [1,1,3,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dd"]
test_input = { "n": 4, "words": ["bab","bdd","bca","dab"], "groups": [2,4,1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dab"]
test_input = { "n": 4, "words": ["bbbd","babca","ebddde","cce"], "groups": [3,4,3,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cce"]
test_input = { "n": 4, "words": ["beb","eacedc","aeeb","cdd"], "groups": [1,4,3,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdd"]
test_input = { "n": 4, "words": ["cac","aaa","dd","cda"], "groups": [1,4,2,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cda"]
test_input = { "n": 4, "words": ["cbb","db","bdd","bd"], "groups": [2,3,4,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bd"]
test_input = { "n": 4, "words": ["cdc","dc","bd","aca"], "groups": [3,2,2,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aca"]
test_input = { "n": 4, "words": ["ceacd","ac","bebdae","dbbbcb"], "groups": [2,3,1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbbbcb"]
test_input = { "n": 4, "words": ["dcaacc","da","ddcbd","dd"], "groups": [2,3,1,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["da","dd"]
test_input = { "n": 4, "words": ["deeb","edbea","ad","ecedd"], "groups": [1,1,1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ecedd"]
test_input = { "n": 4, "words": ["ebe","bcca","caabaa","abb"], "groups": [1,4,4,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abb"]
test_input = { "n": 5, "words": ["abd","bab","bc","ac","acd"], "groups": [3,5,3,3,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abd","acd"]
test_input = { "n": 5, "words": ["acc","ab","baa","dac","aa"], "groups": [3,1,2,3,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","aa"]
test_input = { "n": 5, "words": ["acda","caae","ccad","ac","ddeedb"], "groups": [4,2,4,1,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ddeedb"]
test_input = { "n": 5, "words": ["ade","ea","aabd","bc","aaaabe"], "groups": [4,2,4,4,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aaaabe"]
test_input = { "n": 5, "words": ["ba","ee","ed","ddddd","ce"], "groups": [4,4,4,5,5] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ee","ce"]
test_input = { "n": 5, "words": ["bacd","adbbab","ba","ec","deecbe"], "groups": [4,4,5,5,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["deecbe"]
test_input = { "n": 5, "words": ["bad","cab","abb","cd","ba"], "groups": [2,5,3,4,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ba"]
test_input = { "n": 5, "words": ["ca","cb","bcd","bb","ddc"], "groups": [2,4,2,5,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ca","cb","bb"]
test_input = { "n": 5, "words": ["ccb","ac","aa","bad","ab"], "groups": [3,5,2,2,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ac","aa","ab"]
test_input = { "n": 5, "words": ["cd","dd","ad","aaa","db"], "groups": [2,3,3,5,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd","dd","db"]
test_input = { "n": 5, "words": ["cda","ab","cb","ccb","baa"], "groups": [3,1,2,1,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","cb"]
test_input = { "n": 5, "words": ["dc","eca","cdade","aaaccd","deb"], "groups": [3,2,3,1,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["deb"]
test_input = { "n": 5, "words": ["dceba","dbcab","bacd","bacdab","bdeca"], "groups": [4,3,5,3,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdeca"]
test_input = { "n": 5, "words": ["deeb","ee","bbbbe","bddba","cdb"], "groups": [4,4,1,3,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdb"]
test_input = { "n": 6, "words": ["aab","cab","ba","dba","daa","bca"], "groups": [4,3,4,6,4,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dba","daa"]
test_input = { "n": 6, "words": ["aca","dd","aab","dac","adb","bad"], "groups": [2,6,2,1,4,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aab","adb"]
test_input = { "n": 6, "words": ["ad","bb","cc","bc","bcb","abc"], "groups": [3,5,5,5,1,5] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abc"]
test_input = { "n": 6, "words": ["bcb","cba","cab","cca","ad","cd"], "groups": [6,5,1,5,5,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ad","cd"]
test_input = { "n": 6, "words": ["bdaaee","cb","ecaad","accdd","ba","adad"], "groups": [2,4,4,6,1,5] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["adad"]
test_input = { "n": 6, "words": ["bdccb","cece","dbdda","bbc","bcbae","badc"], "groups": [4,2,4,1,1,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["badc"]
test_input = { "n": 6, "words": ["cba","cc","cd","ccc","aba","ac"], "groups": [3,6,2,4,2,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cba","aba"]
test_input = { "n": 6, "words": ["ccd","db","cbb","cb","cab","acd"], "groups": [5,1,5,5,6,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ccd","acd"]
test_input = { "n": 6, "words": ["eb","eaab","accdba","ecba","aec","dacacd"], "groups": [4,6,1,6,2,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dacacd"]
test_input = { "n": 6, "words": ["ee","aab","db","cc","dead","aee"], "groups": [5,5,4,2,5,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aee"]
test_input = { "n": 7, "words": ["aad","cba","bda","dc","aba","dbc","ac"], "groups": [6,4,1,7,5,2,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dc","ac"]
test_input = { "n": 7, "words": ["adcaa","db","dced","ded","eeadce","bdbbe","acaadc"], "groups": [7,5,4,2,1,5,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["acaadc"]
test_input = { "n": 7, "words": ["bcabd","cd","cbaadc","cda","bcde","ccedca","ba"], "groups": [7,3,6,7,1,7,5] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ba"]
test_input = { "n": 7, "words": ["bd","dbd","dcc","cb","ac","abd","bb"], "groups": [1,3,3,6,6,4,5] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cb","bb"]
test_input = { "n": 7, "words": ["cbde","aad","dbdceb","ae","eca","bd","bba"], "groups": [7,5,7,6,4,5,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bba"]
test_input = { "n": 7, "words": ["cdcdad","baaee","cba","ceae","ab","bedbab","eb"], "groups": [5,3,7,1,7,6,7] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["eb"]
test_input = { "n": 7, "words": ["dabbdb","eacbdb","bbdea","cdcaa","eaeeb","cebabe","ad"], "groups": [3,5,5,6,3,4,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ad"]
test_input = { "n": 7, "words": ["dbcca","dcdd","bebbc","cbed","cb","abed","ac"], "groups": [4,6,6,6,3,7,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cbed","abed"]
test_input = { "n": 7, "words": ["dcc","cba","ab","cb","aac","aba","db"], "groups": [3,2,1,6,2,6,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","db"]
test_input = { "n": 7, "words": ["ddd","cd","adb","bcc","da","ab","ad"], "groups": [2,3,6,7,3,7,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","ad"]
test_input = { "n": 7, "words": ["edded","ab","bc","aeac","ec","db","be"], "groups": [3,2,3,6,3,4,3] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","db"]
test_input = { "n": 8, "words": ["addb","beeddc","dcdce","ddaeed","ddbbb","aeea","adee","dbcbdc"], "groups": [1,5,7,1,5,7,7,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbcbdc"]
test_input = { "n": 8, "words": ["ba","cca","dcb","cd","aa","bd","cda","bcb"], "groups": [6,1,2,2,3,7,7,7] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dcb","bcb"]
test_input = { "n": 8, "words": ["baa","cb","aab","ddc","bba","cdb","abb","dc"], "groups": [1,8,1,1,8,6,1,1] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["baa","bba"]
test_input = { "n": 8, "words": ["bbde","edea","dcd","eebbed","ddab","ae","ec","ade"], "groups": [7,3,2,4,7,3,6,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ade"]
test_input = { "n": 8, "words": ["bc","aa","cb","dd","aaa","ccb","da","bbb"], "groups": [5,4,6,8,6,7,7,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dd","da"]
test_input = { "n": 8, "words": ["bcc","aac","ac","dd","bdd","ada","bbb","db"], "groups": [7,7,1,7,1,1,6,7] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["db"]
test_input = { "n": 8, "words": ["cb","dcc","da","cbb","bd","dbc","ab","db"], "groups": [4,5,5,7,8,1,3,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cb","ab","db"]
test_input = { "n": 8, "words": ["cbaeeb","decd","dbc","cbdcca","acbcbe","adccc","eb","ecbbea"], "groups": [6,7,8,6,3,5,1,7] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ecbbea"]
test_input = { "n": 8, "words": ["cc","aa","cab","dbc","bbb","adc","cba","cca"], "groups": [6,1,6,6,4,8,1,5] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cba","cca"]
test_input = { "n": 8, "words": ["cc","dcd","dac","dc","ac","ad","bbb","cbb"], "groups": [7,7,2,5,4,1,6,2] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cc","dc","ac","ad"]
test_input = { "n": 8, "words": ["ccdbdc","dcce","ebedde","ceb","edee","ca","ad","dddee"], "groups": [6,6,3,4,7,1,5,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dddee"]
test_input = { "n": 8, "words": ["cd","bd","ada","ba","ac","bac","aad","ccb"], "groups": [3,4,8,7,6,7,2,4] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd","bd","ba"]
test_input = { "n": 8, "words": ["da","bd","ccd","dd","ab","cc","aab","ac"], "groups": [6,5,4,8,8,2,2,6] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cc","ac"]
test_input = { "n": 8, "words": ["dab","ecedc","badca","cedacb","bdeab","bccedc","bebc","aeade"], "groups": [3,7,6,8,6,6,7,5] }
assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aeade"] | 1,697,293,800 |
biweekly-contest-115-count-of-sub-multisets-with-bounded-sum | https://leetcode.com/problems/count-of-sub-multisets-with-bounded-sum | count-of-sub-multisets-with-bounded-sum | {
"questionId": "3091",
"questionFrontendId": "2902",
"title": "Count of Sub-Multisets With Bounded Sum",
"titleSlug": "count-of-sub-multisets-with-bounded-sum",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 125,
"dislikes": 19,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array nums of non-negative integers, and two integers l and r.
Return the count of sub-multisets within nums where the sum of elements in each subset falls within the inclusive range of [l, r].
Since the answer may be large, return it modulo 109 + 7.
A sub-multiset is an unordered collection of elements of the array in which a given value x can occur 0, 1, ..., occ[x] times, where occ[x] is the number of occurrences of x in the array.
Note that:
* Two sub-multisets are the same if sorting both sub-multisets results in identical multisets.
* The sum of an empty multiset is 0.
Example 1:
Input: nums = [1,2,2,3], l = 6, r = 6
Output: 1
Explanation: The only subset of nums that has a sum of 6 is {1, 2, 3}.
Example 2:
Input: nums = [2,1,4,2,7], l = 1, r = 5
Output: 7
Explanation: The subsets of nums that have a sum within the range [1, 5] are {1}, {2}, {4}, {2, 2}, {1, 2}, {1, 4}, and {1, 2, 2}.
Example 3:
Input: nums = [1,2,1,3,5,2], l = 3, r = 5
Output: 9
Explanation: The subsets of nums that have a sum within the range [3, 5] are {3}, {5}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {1, 1, 2}, {1, 1, 3}, and {1, 2, 2}.
Constraints:
* 1 <= nums.length <= 2 * 104
* 0 <= nums[i] <= 2 * 104
* Sum of nums does not exceed 2 * 104.
* 0 <= l <= r <= 2 * 104
"""
class Solution:
def countSubMultisets(self, nums: List[int], l: int, r: int) -> int:
| You are given a 0-indexed array nums of non-negative integers, and two integers l and r.
Return the count of sub-multisets within nums where the sum of elements in each subset falls within the inclusive range of [l, r].
Since the answer may be large, return it modulo 109 + 7.
A sub-multiset is an unordered collection of elements of the array in which a given value x can occur 0, 1, ..., occ[x] times, where occ[x] is the number of occurrences of x in the array.
Note that:
* Two sub-multisets are the same if sorting both sub-multisets results in identical multisets.
* The sum of an empty multiset is 0.
Example 1:
Input: nums = [1,2,2,3], l = 6, r = 6
Output: 1
Explanation: The only subset of nums that has a sum of 6 is {1, 2, 3}.
Example 2:
Input: nums = [2,1,4,2,7], l = 1, r = 5
Output: 7
Explanation: The subsets of nums that have a sum within the range [1, 5] are {1}, {2}, {4}, {2, 2}, {1, 2}, {1, 4}, and {1, 2, 2}.
Example 3:
Input: nums = [1,2,1,3,5,2], l = 3, r = 5
Output: 9
Explanation: The subsets of nums that have a sum within the range [3, 5] are {3}, {5}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {1, 1, 2}, {1, 1, 3}, and {1, 2, 2}.
Constraints:
* 1 <= nums.length <= 2 * 104
* 0 <= nums[i] <= 2 * 104
* Sum of nums does not exceed 2 * 104.
* 0 <= l <= r <= 2 * 104
Please complete the code below to solve above prblem:
```python
class Solution:
def countSubMultisets(self, nums: List[int], l: int, r: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,2,3], "l": 6, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,1,4,2,7], "l": 1, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 7
test_input = { "nums": [1,2,1,3,5,2], "l": 3, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 9
test_input = { "nums": [0,0,1,2,3], "l": 2, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 9
test_input = { "nums": [0,0,0,0,0], "l": 0, "r": 0 }
assert my_solution.countSubMultisets(**test_input) == 6
test_input = { "nums": [0,0,0,1,2,5,2,3], "l": 0, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 20
test_input = { "nums": [1,1], "l": 2, "r": 2 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,1,1], "l": 2, "r": 2 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,1,2], "l": 2, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [1,2,1], "l": 2, "r": 2 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,2,2], "l": 3, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [2,1,1], "l": 1, "r": 2 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [2,1,2], "l": 2, "r": 2 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,2,1], "l": 4, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,2,2], "l": 3, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,1,1,1], "l": 3, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,1,1,2], "l": 1, "r": 1 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,1,1,3], "l": 4, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,1,2,1], "l": 3, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [1,1,2,2], "l": 2, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [1,1,2,3], "l": 4, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [1,1,3,1], "l": 2, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [1,1,3,2], "l": 6, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,1,3,3], "l": 8, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,2,1,1], "l": 3, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,2,1,2], "l": 4, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [1,2,1,3], "l": 4, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 6
test_input = { "nums": [1,2,2,1], "l": 4, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [1,2,2,2], "l": 1, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 7
test_input = { "nums": [1,2,2,3], "l": 3, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 9
test_input = { "nums": [1,2,3,1], "l": 1, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 7
test_input = { "nums": [1,2,3,2], "l": 4, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 7
test_input = { "nums": [1,2,3,3], "l": 6, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,3,1,1], "l": 6, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,3,1,2], "l": 7, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,3,1,3], "l": 5, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,3,2,1], "l": 2, "r": 2 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,3,2,2], "l": 7, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,3,2,3], "l": 1, "r": 9 }
assert my_solution.countSubMultisets(**test_input) == 11
test_input = { "nums": [1,3,3,1], "l": 7, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,3,3,2], "l": 1, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 8
test_input = { "nums": [1,3,3,3], "l": 9, "r": 10 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,1,1,1], "l": 2, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [2,1,1,2], "l": 1, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 6
test_input = { "nums": [2,1,1,3], "l": 7, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,1,2,1], "l": 3, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,1,2,2], "l": 6, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,1,2,3], "l": 7, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,1,3,1], "l": 4, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,1,3,2], "l": 3, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [2,1,3,3], "l": 1, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 6
test_input = { "nums": [2,2,1,1], "l": 6, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,2,1,2], "l": 2, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [2,2,1,3], "l": 6, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [2,2,2,1], "l": 4, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [2,2,2,2], "l": 3, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,2,2,3], "l": 5, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,2,3,1], "l": 7, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,2,3,2], "l": 5, "r": 9 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [2,2,3,3], "l": 4, "r": 10 }
assert my_solution.countSubMultisets(**test_input) == 6
test_input = { "nums": [2,3,1,1], "l": 4, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 5
test_input = { "nums": [2,3,1,2], "l": 3, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [2,3,1,3], "l": 2, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 9
test_input = { "nums": [2,3,2,1], "l": 3, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,3,2,2], "l": 3, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,3,2,3], "l": 5, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [2,3,3,1], "l": 9, "r": 9 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,3,3,2], "l": 8, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [2,3,3,3], "l": 3, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [3,1,1,1], "l": 4, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [3,1,1,2], "l": 7, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [3,1,1,3], "l": 1, "r": 1 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [3,1,2,1], "l": 4, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [3,1,2,2], "l": 3, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 9
test_input = { "nums": [3,1,2,3], "l": 5, "r": 9 }
assert my_solution.countSubMultisets(**test_input) == 6
test_input = { "nums": [3,1,3,1], "l": 1, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [3,1,3,2], "l": 3, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [3,1,3,3], "l": 9, "r": 10 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [3,2,1,1], "l": 1, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 9
test_input = { "nums": [3,2,1,2], "l": 1, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 9
test_input = { "nums": [3,2,1,3], "l": 5, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [3,2,2,1], "l": 4, "r": 4 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [3,2,2,2], "l": 6, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [3,2,2,3], "l": 2, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 6
test_input = { "nums": [3,2,3,1], "l": 2, "r": 3 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [3,2,3,2], "l": 8, "r": 10 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [3,2,3,3], "l": 1, "r": 11 }
assert my_solution.countSubMultisets(**test_input) == 7
test_input = { "nums": [3,3,1,1], "l": 8, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [3,3,1,2], "l": 2, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 5
test_input = { "nums": [3,3,1,3], "l": 1, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 5
test_input = { "nums": [3,3,2,1], "l": 4, "r": 6 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [3,3,2,2], "l": 7, "r": 10 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [3,3,2,3], "l": 4, "r": 10 }
assert my_solution.countSubMultisets(**test_input) == 4
test_input = { "nums": [3,3,3,1], "l": 6, "r": 9 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [3,3,3,2], "l": 11, "r": 11 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [3,3,3,3], "l": 8, "r": 9 }
assert my_solution.countSubMultisets(**test_input) == 1
test_input = { "nums": [1,1,1,1,1], "l": 3, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 3
test_input = { "nums": [1,1,1,1,2], "l": 2, "r": 5 }
assert my_solution.countSubMultisets(**test_input) == 7
test_input = { "nums": [1,1,1,1,3], "l": 6, "r": 7 }
assert my_solution.countSubMultisets(**test_input) == 2
test_input = { "nums": [1,1,1,1,4], "l": 8, "r": 8 }
assert my_solution.countSubMultisets(**test_input) == 1 | 1,697,293,800 |
weekly-contest-366-divisible-and-non-divisible-sums-difference | https://leetcode.com/problems/divisible-and-non-divisible-sums-difference | divisible-and-non-divisible-sums-difference | {
"questionId": "3172",
"questionFrontendId": "2894",
"title": "Divisible and Non-divisible Sums Difference",
"titleSlug": "divisible-and-non-divisible-sums-difference",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 115,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
You are given positive integers n and m.
Define two integers, num1 and num2, as follows:
* num1: The sum of all integers in the range [1, n] that are not divisible by m.
* num2: The sum of all integers in the range [1, n] that are divisible by m.
Return the integer num1 - num2.
Example 1:
Input: n = 10, m = 3
Output: 19
Explanation: In the given example:
- Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
We return 37 - 18 = 19 as the answer.
Example 2:
Input: n = 5, m = 6
Output: 15
Explanation: In the given example:
- Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
We return 15 - 0 = 15 as the answer.
Example 3:
Input: n = 5, m = 1
Output: -15
Explanation: In the given example:
- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
- Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
We return 0 - 15 = -15 as the answer.
Constraints:
* 1 <= n, m <= 1000
"""
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
| You are given positive integers n and m.
Define two integers, num1 and num2, as follows:
* num1: The sum of all integers in the range [1, n] that are not divisible by m.
* num2: The sum of all integers in the range [1, n] that are divisible by m.
Return the integer num1 - num2.
Example 1:
Input: n = 10, m = 3
Output: 19
Explanation: In the given example:
- Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
We return 37 - 18 = 19 as the answer.
Example 2:
Input: n = 5, m = 6
Output: 15
Explanation: In the given example:
- Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
We return 15 - 0 = 15 as the answer.
Example 3:
Input: n = 5, m = 1
Output: -15
Explanation: In the given example:
- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
- Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
We return 0 - 15 = -15 as the answer.
Constraints:
* 1 <= n, m <= 1000
Please complete the code below to solve above prblem:
```python
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
``` |
my_solution = Solution()
test_input = { "n": 10, "m": 3 }
assert my_solution.differenceOfSums(**test_input) == 19
test_input = { "n": 5, "m": 6 }
assert my_solution.differenceOfSums(**test_input) == 15
test_input = { "n": 5, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -15
test_input = { "n": 15, "m": 9 }
assert my_solution.differenceOfSums(**test_input) == 102
test_input = { "n": 8, "m": 10 }
assert my_solution.differenceOfSums(**test_input) == 36
test_input = { "n": 23, "m": 36 }
assert my_solution.differenceOfSums(**test_input) == 276
test_input = { "n": 1, "m": 32 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 36, "m": 7 }
assert my_solution.differenceOfSums(**test_input) == 456
test_input = { "n": 3, "m": 8 }
assert my_solution.differenceOfSums(**test_input) == 6
test_input = { "n": 4, "m": 2 }
assert my_solution.differenceOfSums(**test_input) == -2
test_input = { "n": 9, "m": 7 }
assert my_solution.differenceOfSums(**test_input) == 31
test_input = { "n": 20, "m": 9 }
assert my_solution.differenceOfSums(**test_input) == 156
test_input = { "n": 3, "m": 19 }
assert my_solution.differenceOfSums(**test_input) == 6
test_input = { "n": 6, "m": 16 }
assert my_solution.differenceOfSums(**test_input) == 21
test_input = { "n": 6, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -21
test_input = { "n": 5, "m": 25 }
assert my_solution.differenceOfSums(**test_input) == 15
test_input = { "n": 9, "m": 3 }
assert my_solution.differenceOfSums(**test_input) == 9
test_input = { "n": 8, "m": 23 }
assert my_solution.differenceOfSums(**test_input) == 36
test_input = { "n": 17, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -153
test_input = { "n": 18, "m": 9 }
assert my_solution.differenceOfSums(**test_input) == 117
test_input = { "n": 22, "m": 30 }
assert my_solution.differenceOfSums(**test_input) == 253
test_input = { "n": 1, "m": 42 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 33, "m": 19 }
assert my_solution.differenceOfSums(**test_input) == 523
test_input = { "n": 7, "m": 19 }
assert my_solution.differenceOfSums(**test_input) == 28
test_input = { "n": 12, "m": 24 }
assert my_solution.differenceOfSums(**test_input) == 78
test_input = { "n": 26, "m": 25 }
assert my_solution.differenceOfSums(**test_input) == 301
test_input = { "n": 9, "m": 16 }
assert my_solution.differenceOfSums(**test_input) == 45
test_input = { "n": 1, "m": 8 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 29, "m": 42 }
assert my_solution.differenceOfSums(**test_input) == 435
test_input = { "n": 2, "m": 11 }
assert my_solution.differenceOfSums(**test_input) == 3
test_input = { "n": 36, "m": 10 }
assert my_solution.differenceOfSums(**test_input) == 546
test_input = { "n": 45, "m": 4 }
assert my_solution.differenceOfSums(**test_input) == 507
test_input = { "n": 3, "m": 7 }
assert my_solution.differenceOfSums(**test_input) == 6
test_input = { "n": 6, "m": 12 }
assert my_solution.differenceOfSums(**test_input) == 21
test_input = { "n": 3, "m": 4 }
assert my_solution.differenceOfSums(**test_input) == 6
test_input = { "n": 8, "m": 28 }
assert my_solution.differenceOfSums(**test_input) == 36
test_input = { "n": 18, "m": 23 }
assert my_solution.differenceOfSums(**test_input) == 171
test_input = { "n": 11, "m": 6 }
assert my_solution.differenceOfSums(**test_input) == 54
test_input = { "n": 35, "m": 10 }
assert my_solution.differenceOfSums(**test_input) == 510
test_input = { "n": 29, "m": 18 }
assert my_solution.differenceOfSums(**test_input) == 399
test_input = { "n": 1, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -1
test_input = { "n": 12, "m": 8 }
assert my_solution.differenceOfSums(**test_input) == 62
test_input = { "n": 7, "m": 12 }
assert my_solution.differenceOfSums(**test_input) == 28
test_input = { "n": 17, "m": 3 }
assert my_solution.differenceOfSums(**test_input) == 63
test_input = { "n": 16, "m": 15 }
assert my_solution.differenceOfSums(**test_input) == 106
test_input = { "n": 18, "m": 3 }
assert my_solution.differenceOfSums(**test_input) == 45
test_input = { "n": 4, "m": 12 }
assert my_solution.differenceOfSums(**test_input) == 10
test_input = { "n": 3, "m": 21 }
assert my_solution.differenceOfSums(**test_input) == 6
test_input = { "n": 15, "m": 4 }
assert my_solution.differenceOfSums(**test_input) == 72
test_input = { "n": 9, "m": 39 }
assert my_solution.differenceOfSums(**test_input) == 45
test_input = { "n": 19, "m": 18 }
assert my_solution.differenceOfSums(**test_input) == 154
test_input = { "n": 2, "m": 4 }
assert my_solution.differenceOfSums(**test_input) == 3
test_input = { "n": 41, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -861
test_input = { "n": 3, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -6
test_input = { "n": 16, "m": 13 }
assert my_solution.differenceOfSums(**test_input) == 110
test_input = { "n": 32, "m": 10 }
assert my_solution.differenceOfSums(**test_input) == 408
test_input = { "n": 41, "m": 34 }
assert my_solution.differenceOfSums(**test_input) == 793
test_input = { "n": 33, "m": 40 }
assert my_solution.differenceOfSums(**test_input) == 561
test_input = { "n": 36, "m": 8 }
assert my_solution.differenceOfSums(**test_input) == 506
test_input = { "n": 8, "m": 34 }
assert my_solution.differenceOfSums(**test_input) == 36
test_input = { "n": 40, "m": 12 }
assert my_solution.differenceOfSums(**test_input) == 676
test_input = { "n": 28, "m": 9 }
assert my_solution.differenceOfSums(**test_input) == 298
test_input = { "n": 20, "m": 6 }
assert my_solution.differenceOfSums(**test_input) == 138
test_input = { "n": 13, "m": 6 }
assert my_solution.differenceOfSums(**test_input) == 55
test_input = { "n": 2, "m": 37 }
assert my_solution.differenceOfSums(**test_input) == 3
test_input = { "n": 14, "m": 17 }
assert my_solution.differenceOfSums(**test_input) == 105
test_input = { "n": 35, "m": 4 }
assert my_solution.differenceOfSums(**test_input) == 342
test_input = { "n": 2, "m": 14 }
assert my_solution.differenceOfSums(**test_input) == 3
test_input = { "n": 5, "m": 2 }
assert my_solution.differenceOfSums(**test_input) == 3
test_input = { "n": 7, "m": 7 }
assert my_solution.differenceOfSums(**test_input) == 14
test_input = { "n": 12, "m": 26 }
assert my_solution.differenceOfSums(**test_input) == 78
test_input = { "n": 14, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -105
test_input = { "n": 2, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -3
test_input = { "n": 20, "m": 3 }
assert my_solution.differenceOfSums(**test_input) == 84
test_input = { "n": 8, "m": 27 }
assert my_solution.differenceOfSums(**test_input) == 36
test_input = { "n": 1, "m": 12 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 13, "m": 19 }
assert my_solution.differenceOfSums(**test_input) == 91
test_input = { "n": 7, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -28
test_input = { "n": 31, "m": 4 }
assert my_solution.differenceOfSums(**test_input) == 272
test_input = { "n": 11, "m": 25 }
assert my_solution.differenceOfSums(**test_input) == 66
test_input = { "n": 5, "m": 19 }
assert my_solution.differenceOfSums(**test_input) == 15
test_input = { "n": 33, "m": 12 }
assert my_solution.differenceOfSums(**test_input) == 489
test_input = { "n": 4, "m": 26 }
assert my_solution.differenceOfSums(**test_input) == 10
test_input = { "n": 1, "m": 24 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 13, "m": 20 }
assert my_solution.differenceOfSums(**test_input) == 91
test_input = { "n": 6, "m": 8 }
assert my_solution.differenceOfSums(**test_input) == 21
test_input = { "n": 8, "m": 26 }
assert my_solution.differenceOfSums(**test_input) == 36
test_input = { "n": 4, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -10
test_input = { "n": 1, "m": 6 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 2, "m": 25 }
assert my_solution.differenceOfSums(**test_input) == 3
test_input = { "n": 4, "m": 4 }
assert my_solution.differenceOfSums(**test_input) == 2
test_input = { "n": 8, "m": 2 }
assert my_solution.differenceOfSums(**test_input) == -4
test_input = { "n": 15, "m": 21 }
assert my_solution.differenceOfSums(**test_input) == 120
test_input = { "n": 1, "m": 2 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 12, "m": 2 }
assert my_solution.differenceOfSums(**test_input) == -6
test_input = { "n": 40, "m": 14 }
assert my_solution.differenceOfSums(**test_input) == 736
test_input = { "n": 14, "m": 19 }
assert my_solution.differenceOfSums(**test_input) == 105
test_input = { "n": 18, "m": 1 }
assert my_solution.differenceOfSums(**test_input) == -171
test_input = { "n": 1, "m": 28 }
assert my_solution.differenceOfSums(**test_input) == 1
test_input = { "n": 31, "m": 18 }
assert my_solution.differenceOfSums(**test_input) == 460 | 1,696,732,200 |
weekly-contest-366-minimum-processing-time | https://leetcode.com/problems/minimum-processing-time | minimum-processing-time | {
"questionId": "3151",
"questionFrontendId": "2895",
"title": "Minimum Processing Time",
"titleSlug": "minimum-processing-time",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 119,
"dislikes": 22,
"categoryTitle": "Algorithms"
} | """
You have n processors each having 4 cores and n * 4 tasks that need to be executed such that each core should perform only one task.
Given a 0-indexed integer array processorTime representing the time at which each processor becomes available for the first time and a 0-indexed integer array tasks representing the time it takes to execute each task, return the minimum time when all of the tasks have been executed by the processors.
Note: Each core executes the task independently of the others.
Example 1:
Input: processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
Output: 16
Explanation:
It's optimal to assign the tasks at indexes 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indexes 0, 1, 2, 3 to the second processor which becomes available at time = 10.
Time taken by the first processor to finish execution of all tasks = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
Time taken by the second processor to finish execution of all tasks = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
Hence, it can be shown that the minimum time taken to execute all the tasks is 16.
Example 2:
Input: processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
Output: 23
Explanation:
It's optimal to assign the tasks at indexes 1, 4, 5, 6 to the first processor which becomes available at time = 10, and the tasks at indexes 0, 2, 3, 7 to the second processor which becomes available at time = 20.
Time taken by the first processor to finish execution of all tasks = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18.
Time taken by the second processor to finish execution of all tasks = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23.
Hence, it can be shown that the minimum time taken to execute all the tasks is 23.
Constraints:
* 1 <= n == processorTime.length <= 25000
* 1 <= tasks.length <= 105
* 0 <= processorTime[i] <= 109
* 1 <= tasks[i] <= 109
* tasks.length == 4 * n
"""
class Solution:
def minProcessingTime(self, processorTime: List[int], tasks: List[int]) -> int:
| You have n processors each having 4 cores and n * 4 tasks that need to be executed such that each core should perform only one task.
Given a 0-indexed integer array processorTime representing the time at which each processor becomes available for the first time and a 0-indexed integer array tasks representing the time it takes to execute each task, return the minimum time when all of the tasks have been executed by the processors.
Note: Each core executes the task independently of the others.
Example 1:
Input: processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
Output: 16
Explanation:
It's optimal to assign the tasks at indexes 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indexes 0, 1, 2, 3 to the second processor which becomes available at time = 10.
Time taken by the first processor to finish execution of all tasks = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
Time taken by the second processor to finish execution of all tasks = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
Hence, it can be shown that the minimum time taken to execute all the tasks is 16.
Example 2:
Input: processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
Output: 23
Explanation:
It's optimal to assign the tasks at indexes 1, 4, 5, 6 to the first processor which becomes available at time = 10, and the tasks at indexes 0, 2, 3, 7 to the second processor which becomes available at time = 20.
Time taken by the first processor to finish execution of all tasks = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18.
Time taken by the second processor to finish execution of all tasks = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23.
Hence, it can be shown that the minimum time taken to execute all the tasks is 23.
Constraints:
* 1 <= n == processorTime.length <= 25000
* 1 <= tasks.length <= 105
* 0 <= processorTime[i] <= 109
* 1 <= tasks[i] <= 109
* tasks.length == 4 * n
Please complete the code below to solve above prblem:
```python
class Solution:
def minProcessingTime(self, processorTime: List[int], tasks: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "processorTime": [8,10], "tasks": [2,2,3,1,8,7,4,5] }
assert my_solution.minProcessingTime(**test_input) == 16
test_input = { "processorTime": [10,20], "tasks": [2,3,1,2,5,8,4,3] }
assert my_solution.minProcessingTime(**test_input) == 23
test_input = { "processorTime": [121,99], "tasks": [287,315,293,260,333,362,69,233] }
assert my_solution.minProcessingTime(**test_input) == 461
test_input = { "processorTime": [33,320], "tasks": [132,68,232,166,30,300,112,138] }
assert my_solution.minProcessingTime(**test_input) == 452
test_input = { "processorTime": [50,82], "tasks": [288,138,205,295,367,100,258,308] }
assert my_solution.minProcessingTime(**test_input) == 417
test_input = { "processorTime": [291], "tasks": [125,169,269,32] }
assert my_solution.minProcessingTime(**test_input) == 560
test_input = { "processorTime": [55,350,166,210,389], "tasks": [276,253,157,237,92,396,331,19,82,301,136,396,251,92,280,70,253,47,81,84] }
assert my_solution.minProcessingTime(**test_input) == 470
test_input = { "processorTime": [143,228,349,231,392], "tasks": [102,365,363,211,38,96,98,79,365,289,252,201,259,346,21,68,128,56,167,183] }
assert my_solution.minProcessingTime(**test_input) == 517
test_input = { "processorTime": [168,32,299,303,96], "tasks": [382,183,337,73,115,350,6,18,93,238,102,302,96,381,327,385,387,288,138,83] }
assert my_solution.minProcessingTime(**test_input) == 456
test_input = { "processorTime": [324,117,374,219,303], "tasks": [374,202,328,11,353,208,383,287,107,236,226,387,21,183,352,164,207,182,15,65] }
assert my_solution.minProcessingTime(**test_input) == 571
test_input = { "processorTime": [376], "tasks": [21,247,274,38] }
assert my_solution.minProcessingTime(**test_input) == 650
test_input = { "processorTime": [93,3,281,218], "tasks": [182,16,241,312,81,339,207,330,306,166,82,290,7,317,396,389] }
assert my_solution.minProcessingTime(**test_input) == 459
test_input = { "processorTime": [374,250,197,170], "tasks": [247,56,330,361,240,261,67,65,138,181,308,26,59,150,137,244] }
assert my_solution.minProcessingTime(**test_input) == 531
test_input = { "processorTime": [115,271,137], "tasks": [34,72,328,312,159,32,283,6,234,280,46,349] }
assert my_solution.minProcessingTime(**test_input) == 464
test_input = { "processorTime": [47,217,349,233,283], "tasks": [195,188,181,259,145,96,298,322,213,154,278,292,315,191,177,228,291,204,310,266] }
assert my_solution.minProcessingTime(**test_input) == 526
test_input = { "processorTime": [177,6,326,318,294], "tasks": [136,215,260,259,35,248,340,377,144,248,83,150,63,48,269,197,317,135,36,344] }
assert my_solution.minProcessingTime(**test_input) == 542
test_input = { "processorTime": [266,372], "tasks": [260,325,159,316,296,366,335,146] }
assert my_solution.minProcessingTime(**test_input) == 668
test_input = { "processorTime": [63,339], "tasks": [79,316,98,354,220,267,333,11] }
assert my_solution.minProcessingTime(**test_input) == 559
test_input = { "processorTime": [149,60,172,5,212], "tasks": [230,374,276,281,55,96,52,83,56,399,69,333,145,6,50,101,216,327,120,209] }
assert my_solution.minProcessingTime(**test_input) == 404
test_input = { "processorTime": [220,375,285,267,150], "tasks": [53,317,367,258,337,280,232,322,153,169,121,211,171,345,76,370,265,107,45,320] }
assert my_solution.minProcessingTime(**test_input) == 542
test_input = { "processorTime": [373,367,267], "tasks": [214,221,78,330,340,309,330,338,396,337,285,207] }
assert my_solution.minProcessingTime(**test_input) == 697
test_input = { "processorTime": [92,20], "tasks": [11,354,399,11,20,9,217,372] }
assert my_solution.minProcessingTime(**test_input) == 419
test_input = { "processorTime": [51], "tasks": [349,186,191,183] }
assert my_solution.minProcessingTime(**test_input) == 400
test_input = { "processorTime": [362,220,10,219], "tasks": [160,369,385,145,122,124,147,231,162,37,293,160,68,232,232,130] }
assert my_solution.minProcessingTime(**test_input) == 486
test_input = { "processorTime": [210,348,3,57,174], "tasks": [328,296,222,161,190,381,283,137,353,227,284,134,170,13,275,113,148,198,33,260] }
assert my_solution.minProcessingTime(**test_input) == 482
test_input = { "processorTime": [153], "tasks": [342,166,234,175] }
assert my_solution.minProcessingTime(**test_input) == 495
test_input = { "processorTime": [23,204,114,380,3], "tasks": [40,105,311,221,247,34,399,190,23,289,16,129,68,12,32,364,364,111,361,49] }
assert my_solution.minProcessingTime(**test_input) == 412
test_input = { "processorTime": [167,110,221,19,211], "tasks": [140,351,307,4,262,228,161,200,108,206,280,266,240,258,396,194,333,328,121,179] }
assert my_solution.minProcessingTime(**test_input) == 425
test_input = { "processorTime": [179,127,280,242], "tasks": [244,243,92,188,134,84,22,258,100,77,237,83,41,396,218,87] }
assert my_solution.minProcessingTime(**test_input) == 523
test_input = { "processorTime": [45,249,396,163], "tasks": [131,365,52,366,229,340,242,371,20,181,103,97,141,106,46,119] }
assert my_solution.minProcessingTime(**test_input) == 493
test_input = { "processorTime": [205], "tasks": [117,63,174,87] }
assert my_solution.minProcessingTime(**test_input) == 379
test_input = { "processorTime": [128,101,302,53], "tasks": [265,96,358,287,148,117,331,47,173,347,62,145,73,47,206,29] }
assert my_solution.minProcessingTime(**test_input) == 411
test_input = { "processorTime": [228], "tasks": [321,378,268,351] }
assert my_solution.minProcessingTime(**test_input) == 606
test_input = { "processorTime": [136,22,229,187], "tasks": [246,326,188,341,375,207,334,1,189,301,62,39,44,239,346,376] }
assert my_solution.minProcessingTime(**test_input) == 470
test_input = { "processorTime": [47,238,274], "tasks": [251,312,87,111,142,62,112,325,305,164,85,338] }
assert my_solution.minProcessingTime(**test_input) == 489
test_input = { "processorTime": [386,121,249], "tasks": [5,376,53,187,287,369,400,178,293,121,164,336] }
assert my_solution.minProcessingTime(**test_input) == 550
test_input = { "processorTime": [82,152], "tasks": [82,46,149,255,225,93,227,131] }
assert my_solution.minProcessingTime(**test_input) == 337
test_input = { "processorTime": [394,6], "tasks": [231,236,288,35,247,204,141,41] }
assert my_solution.minProcessingTime(**test_input) == 598
test_input = { "processorTime": [389,54], "tasks": [353,358,211,133,225,358,19,310] }
assert my_solution.minProcessingTime(**test_input) == 614
test_input = { "processorTime": [106,291,291,301], "tasks": [192,120,93,5,293,147,299,81,334,137,259,48,296,117,379,182] }
assert my_solution.minProcessingTime(**test_input) == 584
test_input = { "processorTime": [320,139], "tasks": [210,255,304,181,216,255,375,360] }
assert my_solution.minProcessingTime(**test_input) == 575
test_input = { "processorTime": [306,207,295], "tasks": [335,188,355,209,201,113,122,206,46,355,350,38] }
assert my_solution.minProcessingTime(**test_input) == 562
test_input = { "processorTime": [175,111], "tasks": [225,110,163,100,353,77,12,124] }
assert my_solution.minProcessingTime(**test_input) == 464
test_input = { "processorTime": [233,201], "tasks": [1,389,291,333,42,399,399,300] }
assert my_solution.minProcessingTime(**test_input) == 600
test_input = { "processorTime": [302,5,102,195], "tasks": [311,144,7,277,253,96,136,251,81,195,171,140,73,2,84,42] }
assert my_solution.minProcessingTime(**test_input) == 375
test_input = { "processorTime": [151,185,3,137], "tasks": [294,230,221,216,299,24,79,194,375,387,77,388,366,51,117,126] }
assert my_solution.minProcessingTime(**test_input) == 436
test_input = { "processorTime": [39,141,145,199], "tasks": [99,257,161,121,56,80,235,168,171,228,290,180,118,307,66,151] }
assert my_solution.minProcessingTime(**test_input) == 369
test_input = { "processorTime": [40,67], "tasks": [259,217,337,295,126,335,369,123] }
assert my_solution.minProcessingTime(**test_input) == 409
test_input = { "processorTime": [310,56,207,396], "tasks": [260,255,30,243,66,11,285,31,358,219,218,90,176,346,134,74] }
assert my_solution.minProcessingTime(**test_input) == 486
test_input = { "processorTime": [174,48], "tasks": [106,323,4,247,70,281,348,12] }
assert my_solution.minProcessingTime(**test_input) == 396
test_input = { "processorTime": [16,52,13], "tasks": [281,261,55,165,317,150,68,26,52,227,176,399] }
assert my_solution.minProcessingTime(**test_input) == 412
test_input = { "processorTime": [8,315,115,123], "tasks": [134,371,160,138,289,330,48,349,376,88,46,235,298,321,343,365] }
assert my_solution.minProcessingTime(**test_input) == 458
test_input = { "processorTime": [221,24,372,6,50], "tasks": [274,79,78,37,57,39,102,272,242,283,95,155,105,363,174,1,333,400,375,376] }
assert my_solution.minProcessingTime(**test_input) == 429
test_input = { "processorTime": [289,98,7,303,219], "tasks": [363,140,173,92,52,348,350,316,281,327,40,259,39,235,263,244,42,354,11,232] }
assert my_solution.minProcessingTime(**test_input) == 478
test_input = { "processorTime": [348,268], "tasks": [104,397,333,188,373,325,57,202] }
assert my_solution.minProcessingTime(**test_input) == 665
test_input = { "processorTime": [18,377,305,188,311], "tasks": [207,365,369,66,263,47,257,317,221,292,386,308,357,105,99,314,133,106,311,90] }
assert my_solution.minProcessingTime(**test_input) == 597
test_input = { "processorTime": [44,254], "tasks": [277,361,398,276,84,105,350,134] }
assert my_solution.minProcessingTime(**test_input) == 530
test_input = { "processorTime": [270,257,58], "tasks": [212,151,50,78,91,110,399,360,108,192,142,115] }
assert my_solution.minProcessingTime(**test_input) == 457
test_input = { "processorTime": [108,301], "tasks": [150,143,119,160,340,139,72,349] }
assert my_solution.minProcessingTime(**test_input) == 457
test_input = { "processorTime": [231,207,162,49], "tasks": [318,289,351,103,19,77,65,116,94,234,139,246,80,184,286,397] }
assert my_solution.minProcessingTime(**test_input) == 448
test_input = { "processorTime": [252], "tasks": [384,281,207,33] }
assert my_solution.minProcessingTime(**test_input) == 636
test_input = { "processorTime": [199,8,129,204], "tasks": [308,133,366,272,373,343,357,159,378,149,185,248,190,1,142,199] }
assert my_solution.minProcessingTime(**test_input) == 472
test_input = { "processorTime": [135,65,19,225], "tasks": [183,135,138,142,282,141,349,236,57,333,258,353,152,396,152,191] }
assert my_solution.minProcessingTime(**test_input) == 415
test_input = { "processorTime": [199,371,283,70], "tasks": [244,7,226,230,331,232,332,288,151,360,26,87,49,188,269,375] }
assert my_solution.minProcessingTime(**test_input) == 513
test_input = { "processorTime": [184,378], "tasks": [105,239,221,343,276,359,86,84] }
assert my_solution.minProcessingTime(**test_input) == 599
test_input = { "processorTime": [297,229,142,8,47], "tasks": [373,256,210,92,304,134,20,246,116,139,376,139,10,210,192,43,282,278,322,167] }
assert my_solution.minProcessingTime(**test_input) == 389
test_input = { "processorTime": [224,358,58,352], "tasks": [177,274,306,295,142,353,44,111,325,328,394,168,300,15,252,389] }
assert my_solution.minProcessingTime(**test_input) == 626
test_input = { "processorTime": [318,321,264,259], "tasks": [316,284,127,227,269,332,317,364,220,130,330,155,45,205,369,42] }
assert my_solution.minProcessingTime(**test_input) == 628
test_input = { "processorTime": [295,214,130], "tasks": [316,395,280,122,27,224,40,210,99,366,55,183] }
assert my_solution.minProcessingTime(**test_input) == 525
test_input = { "processorTime": [81,38,313,121], "tasks": [158,304,127,214,34,298,95,188,56,391,317,99,304,101,266,302] }
assert my_solution.minProcessingTime(**test_input) == 429
test_input = { "processorTime": [8,400,28,348,193], "tasks": [72,391,149,264,370,183,365,102,201,348,341,176,338,186,97,156,47,125,61,202] }
assert my_solution.minProcessingTime(**test_input) == 504
test_input = { "processorTime": [0], "tasks": [8,369,353,14] }
assert my_solution.minProcessingTime(**test_input) == 369
test_input = { "processorTime": [55,364,28,246], "tasks": [396,357,37,400,239,327,5,387,70,389,323,213,322,111,179,19] }
assert my_solution.minProcessingTime(**test_input) == 485
test_input = { "processorTime": [288,219,356,146,282], "tasks": [390,46,24,391,222,241,281,33,400,312,290,11,147,282,204,214,22,178,77,156] }
assert my_solution.minProcessingTime(**test_input) == 546
test_input = { "processorTime": [60,309,40,219,294], "tasks": [267,94,238,338,279,48,164,371,302,110,247,392,83,107,389,46,92,273,131,136] }
assert my_solution.minProcessingTime(**test_input) == 466
test_input = { "processorTime": [357], "tasks": [211,344,270,324] }
assert my_solution.minProcessingTime(**test_input) == 701
test_input = { "processorTime": [220,355,190,393], "tasks": [158,27,113,335,382,172,285,373,104,177,247,321,197,22,347,136] }
assert my_solution.minProcessingTime(**test_input) == 572
test_input = { "processorTime": [67,105,290,26,343], "tasks": [50,118,302,74,198,56,292,46,337,27,394,69,109,287,274,283,346,132,77,352] }
assert my_solution.minProcessingTime(**test_input) == 420
test_input = { "processorTime": [77,143,142,23], "tasks": [336,190,105,87,102,254,295,243,400,254,96,303,350,191,331,70] }
assert my_solution.minProcessingTime(**test_input) == 423
test_input = { "processorTime": [319,58,155,360], "tasks": [311,257,35,330,235,159,293,204,298,240,233,250,309,242,262,324] }
assert my_solution.minProcessingTime(**test_input) == 593
test_input = { "processorTime": [28,225,347], "tasks": [176,57,60,81,161,66,13,294,145,239,295,210] }
assert my_solution.minProcessingTime(**test_input) == 413
test_input = { "processorTime": [291,337], "tasks": [210,378,169,400,182,290,386,360] }
assert my_solution.minProcessingTime(**test_input) == 691
test_input = { "processorTime": [141,310], "tasks": [396,56,241,289,21,254,196,165] }
assert my_solution.minProcessingTime(**test_input) == 537
test_input = { "processorTime": [204,390,104], "tasks": [355,4,287,161,230,242,218,12,321,28,341,326] }
assert my_solution.minProcessingTime(**test_input) == 551
test_input = { "processorTime": [299,258], "tasks": [20,44,341,172,118,185,369,249] }
assert my_solution.minProcessingTime(**test_input) == 627
test_input = { "processorTime": [107,141,178,211,62], "tasks": [215,318,196,251,71,144,10,208,113,17,13,263,367,42,85,267,212,54,36,54] }
assert my_solution.minProcessingTime(**test_input) == 429
test_input = { "processorTime": [101,383,326,62], "tasks": [304,256,281,240,180,387,318,368,331,267,14,91,93,147,156,394] }
assert my_solution.minProcessingTime(**test_input) == 582
test_input = { "processorTime": [221,62,187,104,266], "tasks": [284,378,9,288,173,327,329,202,3,383,105,213,175,201,196,305,162,161,127,347] }
assert my_solution.minProcessingTime(**test_input) == 445
test_input = { "processorTime": [328,162,249,357,35], "tasks": [77,275,231,298,273,257,88,339,261,147,229,392,156,63,90,97,219,353,66,91] }
assert my_solution.minProcessingTime(**test_input) == 480
test_input = { "processorTime": [7], "tasks": [132,278,270,176] }
assert my_solution.minProcessingTime(**test_input) == 285
test_input = { "processorTime": [326], "tasks": [269,211,137,244] }
assert my_solution.minProcessingTime(**test_input) == 595
test_input = { "processorTime": [310,44], "tasks": [109,250,222,275,268,332,146,328] }
assert my_solution.minProcessingTime(**test_input) == 560
test_input = { "processorTime": [184,254,121,90,389], "tasks": [124,365,400,167,109,207,369,37,174,287,41,114,388,158,125,283,119,254,210,399] }
assert my_solution.minProcessingTime(**test_input) == 503
test_input = { "processorTime": [94,171,66], "tasks": [261,134,26,281,29,253,84,333,90,157,382,263] }
assert my_solution.minProcessingTime(**test_input) == 448
test_input = { "processorTime": [5,99,318,252,151], "tasks": [264,235,250,347,376,57,73,7,178,45,220,148,159,379,89,73,159,172,228,39] }
assert my_solution.minProcessingTime(**test_input) == 400
test_input = { "processorTime": [303,52,2,118,305], "tasks": [398,173,5,301,169,389,126,212,384,359,222,340,267,173,264,238,141,44,144,148] }
assert my_solution.minProcessingTime(**test_input) == 476
test_input = { "processorTime": [177], "tasks": [164,277,289,197] }
assert my_solution.minProcessingTime(**test_input) == 466
test_input = { "processorTime": [297,259,318,30,213], "tasks": [162,97,265,153,216,233,286,346,389,208,55,345,308,197,266,292,369,320,1,235] }
assert my_solution.minProcessingTime(**test_input) == 533
test_input = { "processorTime": [290], "tasks": [333,282,72,362] }
assert my_solution.minProcessingTime(**test_input) == 652
test_input = { "processorTime": [372,189,344], "tasks": [191,26,247,99,395,270,192,340,60,78,260,395] }
assert my_solution.minProcessingTime(**test_input) == 604 | 1,696,732,200 |
weekly-contest-366-apply-operations-to-make-two-strings-equal | https://leetcode.com/problems/apply-operations-to-make-two-strings-equal | apply-operations-to-make-two-strings-equal | {
"questionId": "3033",
"questionFrontendId": "2896",
"title": "Apply Operations to Make Two Strings Equal",
"titleSlug": "apply-operations-to-make-two-strings-equal",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 291,
"dislikes": 62,
"categoryTitle": "Algorithms"
} | """
You are given two 0-indexed binary strings s1 and s2, both of length n, and a positive integer x.
You can perform any of the following operations on the string s1 any number of times:
* Choose two indices i and j, and flip both s1[i] and s1[j]. The cost of this operation is x.
* Choose an index i such that i < n - 1 and flip both s1[i] and s1[i + 1]. The cost of this operation is 1.
Return the minimum cost needed to make the strings s1 and s2 equal, or return -1 if it is impossible.
Note that flipping a character means changing it from 0 to 1 or vice-versa.
Example 1:
Input: s1 = "1100011000", s2 = "0101001010", x = 2
Output: 4
Explanation: We can do the following operations:
- Choose i = 3 and apply the second operation. The resulting string is s1 = "1101111000".
- Choose i = 4 and apply the second operation. The resulting string is s1 = "1101001000".
- Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "0101001010" = s2.
The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
Example 2:
Input: s1 = "10110", s2 = "00011", x = 4
Output: -1
Explanation: It is not possible to make the two strings equal.
Constraints:
* n == s1.length == s2.length
* 1 <= n, x <= 500
* s1 and s2 consist only of the characters '0' and '1'.
"""
class Solution:
def minOperations(self, s1: str, s2: str, x: int) -> int:
| You are given two 0-indexed binary strings s1 and s2, both of length n, and a positive integer x.
You can perform any of the following operations on the string s1 any number of times:
* Choose two indices i and j, and flip both s1[i] and s1[j]. The cost of this operation is x.
* Choose an index i such that i < n - 1 and flip both s1[i] and s1[i + 1]. The cost of this operation is 1.
Return the minimum cost needed to make the strings s1 and s2 equal, or return -1 if it is impossible.
Note that flipping a character means changing it from 0 to 1 or vice-versa.
Example 1:
Input: s1 = "1100011000", s2 = "0101001010", x = 2
Output: 4
Explanation: We can do the following operations:
- Choose i = 3 and apply the second operation. The resulting string is s1 = "1101111000".
- Choose i = 4 and apply the second operation. The resulting string is s1 = "1101001000".
- Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "0101001010" = s2.
The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
Example 2:
Input: s1 = "10110", s2 = "00011", x = 4
Output: -1
Explanation: It is not possible to make the two strings equal.
Constraints:
* n == s1.length == s2.length
* 1 <= n, x <= 500
* s1 and s2 consist only of the characters '0' and '1'.
Please complete the code below to solve above prblem:
```python
class Solution:
def minOperations(self, s1: str, s2: str, x: int) -> int:
``` |
my_solution = Solution()
test_input = { "s1": "1100011000", "s2": "0101001010", "x": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "10110", "s2": "00011", "x": 4 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "101101", "s2": "000000", "x": 6 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "0", "s2": "1", "x": 2 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1011100100111000", "s2": "1001010001011100", "x": 19 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00101101100010", "s2": "00001010001111", "x": 30 }
assert my_solution.minOperations(**test_input) == 8
test_input = { "s1": "1011000", "s2": "0001101", "x": 30 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "1111110101010110", "s2": "1000100111100101", "x": 19 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1011100000100100101", "s2": "1110001001110000011", "x": 14 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0", "s2": "1", "x": 17 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0", "s2": "1", "x": 3 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0001110010", "s2": "0110100111", "x": 29 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "s1": "01111101010100110100", "s2": "10010011011001011000", "x": 21 }
assert my_solution.minOperations(**test_input) == 7
test_input = { "s1": "00000101", "s2": "01001010", "x": 10 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "01", "s2": "00", "x": 30 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "11111", "s2": "01011", "x": 4 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "s1": "001010101011001", "s2": "110111000101110", "x": 14 }
assert my_solution.minOperations(**test_input) == 7
test_input = { "s1": "010011101", "s2": "101111000", "x": 17 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "11110111", "s2": "10011111", "x": 16 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0011", "s2": "1100", "x": 14 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "s1": "000011", "s2": "010101", "x": 27 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "01111010", "s2": "10110011", "x": 21 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "10010111001", "s2": "11101011110", "x": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "10001", "s2": "11000", "x": 11 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "s1": "11001011111", "s2": "01111000110", "x": 2 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "111010100001011", "s2": "100000101100111", "x": 29 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "010000110111000111", "s2": "100011100010010111", "x": 15 }
assert my_solution.minOperations(**test_input) == 6
test_input = { "s1": "010010111", "s2": "011100010", "x": 21 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "01011111", "s2": "11110101", "x": 7 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "00001011110000", "s2": "01011110001001", "x": 12 }
assert my_solution.minOperations(**test_input) == 8
test_input = { "s1": "1100110100001001", "s2": "0100111010111001", "x": 18 }
assert my_solution.minOperations(**test_input) == 8
test_input = { "s1": "00101101", "s2": "10010101", "x": 6 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "s1": "110010", "s2": "011011", "x": 21 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00101", "s2": "11000", "x": 25 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "s1": "110", "s2": "100", "x": 27 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1101", "s2": "0000", "x": 9 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "11", "s2": "01", "x": 7 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "100000000", "s2": "001011111", "x": 26 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0111001011", "s2": "0010011111", "x": 21 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "10", "s2": "00", "x": 19 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "110111", "s2": "101101", "x": 3 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1010111", "s2": "0110011", "x": 25 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "001", "s2": "101", "x": 19 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "111100000100111", "s2": "110100010110001", "x": 7 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "111", "s2": "110", "x": 4 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "10000010010", "s2": "11100000010", "x": 22 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00100110", "s2": "10101111", "x": 16 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0110010001101011010", "s2": "1011110101000001100", "x": 3 }
assert my_solution.minOperations(**test_input) == 8
test_input = { "s1": "0001100000001", "s2": "0011000011101", "x": 28 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0000110011", "s2": "0000000011", "x": 3 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "s1": "11101100", "s2": "11111011", "x": 10 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "s1": "011101110001001010", "s2": "111000011001101010", "x": 30 }
assert my_solution.minOperations(**test_input) == 8
test_input = { "s1": "1111111100", "s2": "1010001010", "x": 5 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "111011", "s2": "111010", "x": 19 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00010", "s2": "00010", "x": 9 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "s1": "11100000", "s2": "11110010", "x": 13 }
assert my_solution.minOperations(**test_input) == 3
test_input = { "s1": "111101000111", "s2": "101111010010", "x": 16 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0", "s2": "0", "x": 20 }
assert my_solution.minOperations(**test_input) == 0
test_input = { "s1": "0011111100011", "s2": "1001100101000", "x": 26 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1000101111101001", "s2": "0110000010110010", "x": 25 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "11000010000", "s2": "11111000001", "x": 17 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1110111001000001", "s2": "0110011110101101", "x": 1 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "0111101101", "s2": "0000111001", "x": 2 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "000001110", "s2": "000101001", "x": 22 }
assert my_solution.minOperations(**test_input) == 4
test_input = { "s1": "1110000000", "s2": "1100111100", "x": 5 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1101101010", "s2": "0101010011", "x": 15 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0010011011000101001", "s2": "1110101001110100010", "x": 19 }
assert my_solution.minOperations(**test_input) == 9
test_input = { "s1": "11010011101011110111", "s2": "11101111011010010011", "x": 8 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "101100010", "s2": "100011110", "x": 29 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00100001", "s2": "10011101", "x": 29 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "111111110110010", "s2": "111011111010001", "x": 29 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "11011110110010", "s2": "01010100000111", "x": 9 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "011101010101", "s2": "111001010100", "x": 19 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00001", "s2": "11111", "x": 27 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "s1": "110", "s2": "101", "x": 15 }
assert my_solution.minOperations(**test_input) == 1
test_input = { "s1": "11011001111000111001", "s2": "11100011111011110001", "x": 12 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1010100", "s2": "0100111", "x": 2 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "000", "s2": "010", "x": 21 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1011010100111101", "s2": "1010001100110110", "x": 18 }
assert my_solution.minOperations(**test_input) == 9
test_input = { "s1": "111", "s2": "000", "x": 4 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00101100110110010", "s2": "00001111111011011", "x": 22 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "000110011", "s2": "010010001", "x": 8 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1011000000", "s2": "0001010010", "x": 16 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "s1": "01101100110011011011", "s2": "10101101010011001011", "x": 5 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "00010", "s2": "00011", "x": 8 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1010110111000111", "s2": "1110110001001000", "x": 22 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "011110100000", "s2": "101100010100", "x": 21 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "s1": "1011010010100101101", "s2": "1001001110101100000", "x": 29 }
assert my_solution.minOperations(**test_input) == 9
test_input = { "s1": "1111100", "s2": "0010100", "x": 20 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0100100110001", "s2": "1001111110001", "x": 4 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "110", "s2": "011", "x": 2 }
assert my_solution.minOperations(**test_input) == 2
test_input = { "s1": "111111010101", "s2": "000011101101", "x": 30 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "101111110110010100", "s2": "010001111100000100", "x": 3 }
assert my_solution.minOperations(**test_input) == 7
test_input = { "s1": "0110111", "s2": "0111010", "x": 19 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "1101010101", "s2": "1011000110", "x": 14 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "01100101", "s2": "11010111", "x": 17 }
assert my_solution.minOperations(**test_input) == 5
test_input = { "s1": "0011111", "s2": "0110101", "x": 13 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "11110110111111011", "s2": "11101101111010110", "x": 19 }
assert my_solution.minOperations(**test_input) == 6
test_input = { "s1": "110100", "s2": "000011", "x": 20 }
assert my_solution.minOperations(**test_input) == -1
test_input = { "s1": "0101", "s2": "0010", "x": 29 }
assert my_solution.minOperations(**test_input) == -1 | 1,696,732,200 |
weekly-contest-366-apply-operations-on-array-to-maximize-sum-of-squares | https://leetcode.com/problems/apply-operations-on-array-to-maximize-sum-of-squares | apply-operations-on-array-to-maximize-sum-of-squares | {
"questionId": "3153",
"questionFrontendId": "2897",
"title": "Apply Operations on Array to Maximize Sum of Squares",
"titleSlug": "apply-operations-on-array-to-maximize-sum-of-squares",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 159,
"dislikes": 3,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums and a positive integer k.
You can do the following operation on the array any number of times:
* Choose any two distinct indices i and j and simultaneously update the values of nums[i] to (nums[i] AND nums[j]) and nums[j] to (nums[i] OR nums[j]). Here, OR denotes the bitwise OR operation, and AND denotes the bitwise AND operation.
You have to choose k elements from the final array and calculate the sum of their squares.
Return the maximum sum of squares you can achieve.
Since the answer can be very large, return it modulo 109 + 7.
Example 1:
Input: nums = [2,6,5,8], k = 2
Output: 261
Explanation: We can do the following operations on the array:
- Choose i = 0 and j = 3, then change nums[0] to (2 AND 8) = 0 and nums[3] to (2 OR 8) = 10. The resulting array is nums = [0,6,5,10].
- Choose i = 2 and j = 3, then change nums[2] to (5 AND 10) = 0 and nums[3] to (5 OR 10) = 15. The resulting array is nums = [0,6,0,15].
We can choose the elements 15 and 6 from the final array. The sum of squares is 152 + 62 = 261.
It can be shown that this is the maximum value we can get.
Example 2:
Input: nums = [4,5,4,7], k = 3
Output: 90
Explanation: We do not need to apply any operations.
We can choose the elements 7, 5, and 4 with a sum of squares: 72 + 52 + 42 = 90.
It can be shown that this is the maximum value we can get.
Constraints:
* 1 <= k <= nums.length <= 105
* 1 <= nums[i] <= 109
"""
class Solution:
def maxSum(self, nums: List[int], k: int) -> int:
| You are given a 0-indexed integer array nums and a positive integer k.
You can do the following operation on the array any number of times:
* Choose any two distinct indices i and j and simultaneously update the values of nums[i] to (nums[i] AND nums[j]) and nums[j] to (nums[i] OR nums[j]). Here, OR denotes the bitwise OR operation, and AND denotes the bitwise AND operation.
You have to choose k elements from the final array and calculate the sum of their squares.
Return the maximum sum of squares you can achieve.
Since the answer can be very large, return it modulo 109 + 7.
Example 1:
Input: nums = [2,6,5,8], k = 2
Output: 261
Explanation: We can do the following operations on the array:
- Choose i = 0 and j = 3, then change nums[0] to (2 AND 8) = 0 and nums[3] to (2 OR 8) = 10. The resulting array is nums = [0,6,5,10].
- Choose i = 2 and j = 3, then change nums[2] to (5 AND 10) = 0 and nums[3] to (5 OR 10) = 15. The resulting array is nums = [0,6,0,15].
We can choose the elements 15 and 6 from the final array. The sum of squares is 152 + 62 = 261.
It can be shown that this is the maximum value we can get.
Example 2:
Input: nums = [4,5,4,7], k = 3
Output: 90
Explanation: We do not need to apply any operations.
We can choose the elements 7, 5, and 4 with a sum of squares: 72 + 52 + 42 = 90.
It can be shown that this is the maximum value we can get.
Constraints:
* 1 <= k <= nums.length <= 105
* 1 <= nums[i] <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def maxSum(self, nums: List[int], k: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [2,6,5,8], "k": 2 }
assert my_solution.maxSum(**test_input) == 261
test_input = { "nums": [4,5,4,7], "k": 3 }
assert my_solution.maxSum(**test_input) == 90
test_input = { "nums": [32,85,61], "k": 1 }
assert my_solution.maxSum(**test_input) == 15625
test_input = { "nums": [123], "k": 1 }
assert my_solution.maxSum(**test_input) == 15129
test_input = { "nums": [96,66,60,58,32,17,63,21,30,44,15,8,98,93], "k": 2 }
assert my_solution.maxSum(**test_input) == 32258
test_input = { "nums": [30,8,63,69,52,94,41,28,94,86,28,13,68,38,53,11,21,33], "k": 2 }
assert my_solution.maxSum(**test_input) == 32258
test_input = { "nums": [2,38,15,2,73,100,47,14,25,58,40,64,23,9,53,38,91,75,9,2], "k": 3 }
assert my_solution.maxSum(**test_input) == 48387
test_input = { "nums": [25,52,75,65], "k": 4 }
assert my_solution.maxSum(**test_input) == 24051
test_input = { "nums": [96,36,72,61,13,25,5,33,9,51,9,78,40], "k": 13 }
assert my_solution.maxSum(**test_input) == 53776
test_input = { "nums": [38,21,15,84,65,35,57,82,94,26,27,89,73,22,25,6,97,17], "k": 4 }
assert my_solution.maxSum(**test_input) == 64516
test_input = { "nums": [18,72,52,56,7,21,55,68,98,31,35,49,100,49,64,20], "k": 4 }
assert my_solution.maxSum(**test_input) == 62548
test_input = { "nums": [2,73,75], "k": 3 }
assert my_solution.maxSum(**test_input) == 11250
test_input = { "nums": [73,37,41,84], "k": 2 }
assert my_solution.maxSum(**test_input) == 27506
test_input = { "nums": [62,83,11,3,53], "k": 3 }
assert my_solution.maxSum(**test_input) == 20459
test_input = { "nums": [53,59,71,38,5,15,98,86,9,8,35,54,65,77,3,68,11,5,41,18], "k": 9 }
assert my_solution.maxSum(**test_input) == 95273
test_input = { "nums": [53,67,91,79,21,27,63,34,60,94,51], "k": 4 }
assert my_solution.maxSum(**test_input) == 64516
test_input = { "nums": [41,15,6,31,40,97,11,45,81,91,91,62], "k": 3 }
assert my_solution.maxSum(**test_input) == 48387
test_input = { "nums": [10,9], "k": 2 }
assert my_solution.maxSum(**test_input) == 185
test_input = { "nums": [9,6,8,32,92,12,47,45,62,96,5,66,82,90,34,39,49,86,16], "k": 13 }
assert my_solution.maxSum(**test_input) == 102770
test_input = { "nums": [1,19,29,30,68,13,80,16,71,32,8,76,41,24,16,2,30], "k": 14 }
assert my_solution.maxSum(**test_input) == 53470
test_input = { "nums": [22,64,30,71,28,69,86,12,26,39,69,92], "k": 2 }
assert my_solution.maxSum(**test_input) == 25154
test_input = { "nums": [91,26,29,38,97,40,1,18,15,3,43,37,9,55,4,46], "k": 2 }
assert my_solution.maxSum(**test_input) == 32258
test_input = { "nums": [27,73], "k": 1 }
assert my_solution.maxSum(**test_input) == 8281
test_input = { "nums": [12,33,29,75,94,48,25,21], "k": 8 }
assert my_solution.maxSum(**test_input) == 34565
test_input = { "nums": [39,91,84,10,65,28,94,28,62,77,78,50,93,65,21,16,5,35,81], "k": 14 }
assert my_solution.maxSum(**test_input) == 110106
test_input = { "nums": [14,45,76,33,35,53,67,19,6,31,33], "k": 10 }
assert my_solution.maxSum(**test_input) == 40008
test_input = { "nums": [59,88,2,47,75], "k": 2 }
assert my_solution.maxSum(**test_input) == 31258
test_input = { "nums": [96,77,77,33,5,86,90,21,84,73,86,45,88,35,93,14,63,25], "k": 17 }
assert my_solution.maxSum(**test_input) == 121571
test_input = { "nums": [35,5,21,65,34,90,60,8,34,35,28,78,77], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [14,10,19], "k": 2 }
assert my_solution.maxSum(**test_input) == 1061
test_input = { "nums": [100,4,88,29,13,78,89,11,62,63,66,46,99,87,41,29,36,71,57], "k": 18 }
assert my_solution.maxSum(**test_input) == 129739
test_input = { "nums": [86,52,100,68,30,40,49,28,61,30,3,80], "k": 11 }
assert my_solution.maxSum(**test_input) == 69063
test_input = { "nums": [29,30,61,12,98,95], "k": 2 }
assert my_solution.maxSum(**test_input) == 32258
test_input = { "nums": [23,13,35,41,29,57,84,67,70,96,55,85,15,72,23,52,1,11,62,1], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [34,60,85,22,83], "k": 2 }
assert my_solution.maxSum(**test_input) == 30290
test_input = { "nums": [65,26,44,70,79,65,46,18], "k": 8 }
assert my_solution.maxSum(**test_input) == 44587
test_input = { "nums": [99,50,13,62,12,60,6,29], "k": 6 }
assert my_solution.maxSum(**test_input) == 28071
test_input = { "nums": [73,66,75,44], "k": 3 }
assert my_solution.maxSum(**test_input) == 23130
test_input = { "nums": [43,85,7,66,16,96], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [45,5,3,84,81,54,21,37,99,60], "k": 2 }
assert my_solution.maxSum(**test_input) == 32258
test_input = { "nums": [97,6,44,57,63,5], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [65,43,82,46,34,42,65,67,8,67,3,83,87,71,98,31,15,22], "k": 5 }
assert my_solution.maxSum(**test_input) == 80645
test_input = { "nums": [79,33,75,32,64,68,30,46,60,50,6,54,18,34,43,11,84,78,54,4], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [17,9,3,23,33,99,94,15,93,17,39,55,13,26,22,44,13], "k": 3 }
assert my_solution.maxSum(**test_input) == 48387
test_input = { "nums": [83,29,2,67,79,88,71,98,70], "k": 4 }
assert my_solution.maxSum(**test_input) == 39220
test_input = { "nums": [60,81,60,88,37,38,10,42,84,70], "k": 10 }
assert my_solution.maxSum(**test_input) == 67626
test_input = { "nums": [33,51,100,33,46], "k": 3 }
assert my_solution.maxSum(**test_input) == 18739
test_input = { "nums": [29,4,67,44,74,62,41,86,91,11,26,58,59,48,46,41,26,68,4,81], "k": 4 }
assert my_solution.maxSum(**test_input) == 64516
test_input = { "nums": [86,54,20,57,87,63,2,24,73,87,7,16,50,1,58], "k": 7 }
assert my_solution.maxSum(**test_input) == 69543
test_input = { "nums": [91,2,16,77,2], "k": 1 }
assert my_solution.maxSum(**test_input) == 9025
test_input = { "nums": [19,94], "k": 2 }
assert my_solution.maxSum(**test_input) == 9349
test_input = { "nums": [14,67,79,58], "k": 2 }
assert my_solution.maxSum(**test_input) == 22370
test_input = { "nums": [44,17,10,19,3,97,45,65,98,7,73,30,76,5,52,33,62], "k": 4 }
assert my_solution.maxSum(**test_input) == 64516
test_input = { "nums": [8,80,93], "k": 1 }
assert my_solution.maxSum(**test_input) == 8649
test_input = { "nums": [51,79,26,30,41,74,6,11,10,66,61,25,41,32,83,52,71,70], "k": 18 }
assert my_solution.maxSum(**test_input) == 98085
test_input = { "nums": [15,58,38,69,71,43], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [13,57,34,69,80,98,63,22,29,38,70,94,79,95,13,76,39,22], "k": 5 }
assert my_solution.maxSum(**test_input) == 80645
test_input = { "nums": [98,88,17,85,57,97,42,15,25,71,31,72,76,89,28,47,73,85], "k": 9 }
assert my_solution.maxSum(**test_input) == 114889
test_input = { "nums": [95,28,26,65,87,4,14,25,47,97,67,48,29,14,96,76,77,25], "k": 9 }
assert my_solution.maxSum(**test_input) == 100409
test_input = { "nums": [37,16,76,9,88,44,71,61,95,32,63,10,29,33], "k": 11 }
assert my_solution.maxSum(**test_input) == 72360
test_input = { "nums": [54,96,73,18,15,35,79,96,2,12,50,75,7,93,66,35,40,16], "k": 18 }
assert my_solution.maxSum(**test_input) == 104436
test_input = { "nums": [66,84,85,7,45,34,61,91,83,13,87,89,51,52,65], "k": 6 }
assert my_solution.maxSum(**test_input) == 88214
test_input = { "nums": [76,22,86,88,58,10,61,21,42], "k": 2 }
assert my_solution.maxSum(**test_input) == 32258
test_input = { "nums": [16,52,8,99,68,73], "k": 2 }
assert my_solution.maxSum(**test_input) == 31754
test_input = { "nums": [46,91,73,38,36,79,24,78,24,42], "k": 8 }
assert my_solution.maxSum(**test_input) == 60911
test_input = { "nums": [5,100,85,52,5,28,79,30,9,67,87,50,17,29,99,57], "k": 14 }
assert my_solution.maxSum(**test_input) == 91019
test_input = { "nums": [52,57,77,95,79,28,9,94,70,8,89,75,27,53,41,88,68,8,10,59], "k": 5 }
assert my_solution.maxSum(**test_input) == 80645
test_input = { "nums": [66,64,52,90,73,84,2], "k": 6 }
assert my_solution.maxSum(**test_input) == 39881
test_input = { "nums": [97,100], "k": 1 }
assert my_solution.maxSum(**test_input) == 10201
test_input = { "nums": [27,85,57,44,16,55,77,77,24,62,72], "k": 10 }
assert my_solution.maxSum(**test_input) == 66334
test_input = { "nums": [15,6,18,22,72,63,38,72,4,84,9,19,70,76,72,98,35,51,11,9], "k": 20 }
assert my_solution.maxSum(**test_input) == 96344
test_input = { "nums": [10,67,54,100,6,93,91,4,59], "k": 3 }
assert my_solution.maxSum(**test_input) == 48387
test_input = { "nums": [90,48,91,62,39,94,75,8,21,72,9,55,16,30,27,73,81,39,97], "k": 8 }
assert my_solution.maxSum(**test_input) == 114080
test_input = { "nums": [43,58,51,40,39,92,36,57], "k": 4 }
assert my_solution.maxSum(**test_input) == 27548
test_input = { "nums": [85,51,49,13,7,66,21,59,100,14,5,66], "k": 8 }
assert my_solution.maxSum(**test_input) == 63152
test_input = { "nums": [54,52,36,17,34,100,81,82,16,46,26,73,77,55,43,53], "k": 14 }
assert my_solution.maxSum(**test_input) == 91703
test_input = { "nums": [97,1,30,41,82,77,99,56,21,68,14,39,15,26,72], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [17,10,28,78,68,68,29,44], "k": 5 }
assert my_solution.maxSum(**test_input) == 33906
test_input = { "nums": [59,30,26,58,87,1,6,98,29,50,57,64,64], "k": 10 }
assert my_solution.maxSum(**test_input) == 69321
test_input = { "nums": [77,23,43,94,74,41,26,39,83,57,85,49,83,34,63,37,42,55,20,18], "k": 3 }
assert my_solution.maxSum(**test_input) == 48387
test_input = { "nums": [54,19,69,95,26,59,68,90,77,62,67,54,42,25,50,23,30,53,29,78], "k": 12 }
assert my_solution.maxSum(**test_input) == 117170
test_input = { "nums": [54,17,16,30,35,63,34,38,26,41,33], "k": 9 }
assert my_solution.maxSum(**test_input) == 22133
test_input = { "nums": [17,24,8,57,68,54,64,53], "k": 6 }
assert my_solution.maxSum(**test_input) == 35987
test_input = { "nums": [11,90,27,72,22,24,54,64,68,94,1,20,45,5,63], "k": 10 }
assert my_solution.maxSum(**test_input) == 69082
test_input = { "nums": [92,84,89,72,80,1,8], "k": 3 }
assert my_solution.maxSum(**test_input) == 25042
test_input = { "nums": [29,14,37,61,7,10,53,95,47,81,1,59,18,25,3,53,43,64,33], "k": 5 }
assert my_solution.maxSum(**test_input) == 56325
test_input = { "nums": [44,94,83,38,63,16,45,90,74,20], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [13,65], "k": 2 }
assert my_solution.maxSum(**test_input) == 5930
test_input = { "nums": [16,9,2,66], "k": 2 }
assert my_solution.maxSum(**test_input) == 8285
test_input = { "nums": [30,68,85,13,49,96,59,61,39], "k": 7 }
assert my_solution.maxSum(**test_input) == 54942
test_input = { "nums": [29,47,38,4], "k": 2 }
assert my_solution.maxSum(**test_input) == 6178
test_input = { "nums": [49,30], "k": 2 }
assert my_solution.maxSum(**test_input) == 4225
test_input = { "nums": [64,71,33,46,77,45,33,55,84,30,1,40,2,92,54,88], "k": 8 }
assert my_solution.maxSum(**test_input) == 98815
test_input = { "nums": [55,72,75], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [3,34,63,27,49,28,86], "k": 3 }
assert my_solution.maxSum(**test_input) == 24067
test_input = { "nums": [99,29,94,21,54,43,20,79,27,40,90,76,55,27,40,46,76,70,34], "k": 13 }
assert my_solution.maxSum(**test_input) == 118264
test_input = { "nums": [64,11,2,12,11,82,10,42,63,98,99,13], "k": 1 }
assert my_solution.maxSum(**test_input) == 16129
test_input = { "nums": [23,25,45,88,88,93,91,25,34,83,9,85,18], "k": 8 }
assert my_solution.maxSum(**test_input) == 67760
test_input = { "nums": [84,95,7,53,19,46,41,48,48,38], "k": 7 }
assert my_solution.maxSum(**test_input) == 44981
test_input = { "nums": [92,88,27,34], "k": 4 }
assert my_solution.maxSum(**test_input) == 24805 | 1,696,732,200 |
weekly-contest-365-maximum-value-of-an-ordered-triplet-i | https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i | maximum-value-of-an-ordered-triplet-i | {
"questionId": "3154",
"questionFrontendId": "2873",
"title": "Maximum Value of an Ordered Triplet I",
"titleSlug": "maximum-value-of-an-ordered-triplet-i",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 130,
"dislikes": 10,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums.
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
* 3 <= nums.length <= 100
* 1 <= nums[i] <= 106
"""
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums.
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
* 3 <= nums.length <= 100
* 1 <= nums[i] <= 106
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [12,6,1,2,7] }
assert my_solution.maximumTripletValue(**test_input) == 77
test_input = { "nums": [1,10,3,4,19] }
assert my_solution.maximumTripletValue(**test_input) == 133
test_input = { "nums": [1,2,3] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [2,3,1] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [5,7,8,4] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [1000000,1,1000000] }
assert my_solution.maximumTripletValue(**test_input) == 999999000000
test_input = { "nums": [18,15,8,13,10,9,17,10,2,16,17] }
assert my_solution.maximumTripletValue(**test_input) == 272
test_input = { "nums": [8,6,3,13,2,12,19,5,19,6,10,11,9] }
assert my_solution.maximumTripletValue(**test_input) == 266
test_input = { "nums": [6,11,12,12,7,9,2,11,12,4,19,14,16,8,16] }
assert my_solution.maximumTripletValue(**test_input) == 190
test_input = { "nums": [15,14,17,13,18,17,10,19,2,20,12,9] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [6,14,20,19,19,10,3,15,12,13,8,1,2,15,3] }
assert my_solution.maximumTripletValue(**test_input) == 285
test_input = { "nums": [2,7,19,4,8,20] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [10,13,6,2] }
assert my_solution.maximumTripletValue(**test_input) == 14
test_input = { "nums": [1,19,1,3,18,10,16,9,3,17,8,9] }
assert my_solution.maximumTripletValue(**test_input) == 324
test_input = { "nums": [16,2,10,20,16,2,13,8,19] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [19,11,12,4,17,1,7,20,13,10,14,20,11,19,3] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [16,15,12,5,4,12,15,17,5,18,6,16,1,17,4] }
assert my_solution.maximumTripletValue(**test_input) == 289
test_input = { "nums": [8,10,17,11,2,8,13] }
assert my_solution.maximumTripletValue(**test_input) == 195
test_input = { "nums": [13,4,3,19,16,14,17,6,20,6,16,4] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [1,8,9,18,4,10,3,13,9] }
assert my_solution.maximumTripletValue(**test_input) == 195
test_input = { "nums": [10,10,5,19,2] }
assert my_solution.maximumTripletValue(**test_input) == 95
test_input = { "nums": [15,3,3,18,19,13,7,5,18,1,8,5] }
assert my_solution.maximumTripletValue(**test_input) == 252
test_input = { "nums": [10,20,10] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [14,9,4,20,9] }
assert my_solution.maximumTripletValue(**test_input) == 200
test_input = { "nums": [12,20,5,2,13,17,16,1,5,8,18,15,12] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [7,1,17,17,4,20,14,20] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [16,19,8,8,5,18,12,16,8,14,14,7,19] }
assert my_solution.maximumTripletValue(**test_input) == 266
test_input = { "nums": [17,9,13,7,3,5] }
assert my_solution.maximumTripletValue(**test_input) == 104
test_input = { "nums": [15,12,2,14,15,18,15,20,14,5,14,14,11,13,7] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [17,20,17,13,5,12,8,12,14,10,14,20] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [1,19,10] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [11,16,10,15,10,5,7,3] }
assert my_solution.maximumTripletValue(**test_input) == 90
test_input = { "nums": [5,14,19,12,2,5,18,3,20,12,1,11] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [10,8,12,14] }
assert my_solution.maximumTripletValue(**test_input) == 28
test_input = { "nums": [2,17,18,16,14,20,11,3,18,5,20,6,7] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [19,12,3,19,2,18,3,12,9] }
assert my_solution.maximumTripletValue(**test_input) == 306
test_input = { "nums": [12,9,11,2,11,3,11,17,13,19] }
assert my_solution.maximumTripletValue(**test_input) == 190
test_input = { "nums": [8,13,9,8,7,18,20] }
assert my_solution.maximumTripletValue(**test_input) == 120
test_input = { "nums": [20,8,12,1,7,8,3,3,6] }
assert my_solution.maximumTripletValue(**test_input) == 152
test_input = { "nums": [8,2,16,6,14,14,13,2,11,5,2,12,15,3,3] }
assert my_solution.maximumTripletValue(**test_input) == 210
test_input = { "nums": [19,9,9,9,5] }
assert my_solution.maximumTripletValue(**test_input) == 90
test_input = { "nums": [19,10,5,13,6,9,5,15,19] }
assert my_solution.maximumTripletValue(**test_input) == 266
test_input = { "nums": [14,18,17,8,2,8,14] }
assert my_solution.maximumTripletValue(**test_input) == 224
test_input = { "nums": [11,5,17,13,5,8,8,19,17,1] }
assert my_solution.maximumTripletValue(**test_input) == 228
test_input = { "nums": [18,12,18,14,17,19] }
assert my_solution.maximumTripletValue(**test_input) == 114
test_input = { "nums": [18,17,8,8,18,9] }
assert my_solution.maximumTripletValue(**test_input) == 180
test_input = { "nums": [15,3,2,10,11,10,13,18] }
assert my_solution.maximumTripletValue(**test_input) == 234
test_input = { "nums": [17,17,5,10,19,1,16,3,1,19] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [1,18,4,20,16] }
assert my_solution.maximumTripletValue(**test_input) == 280
test_input = { "nums": [6,20,4,4,2,19,14,10,9,7,20,5,8] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [5,14,15,18,2,9,15,13,11,16,12,20] }
assert my_solution.maximumTripletValue(**test_input) == 320
test_input = { "nums": [7,19,17] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [5,7,14,18,13,11,15,20,8,11,12,4,17,2,16] }
assert my_solution.maximumTripletValue(**test_input) == 288
test_input = { "nums": [4,12,7,2,8,6,9,5,4,1,8] }
assert my_solution.maximumTripletValue(**test_input) == 90
test_input = { "nums": [11,17,2,18,5] }
assert my_solution.maximumTripletValue(**test_input) == 270
test_input = { "nums": [19,13,2,2,19] }
assert my_solution.maximumTripletValue(**test_input) == 323
test_input = { "nums": [14,11,7,6,2,20,16,14,4,12,1,9,16,7,10] }
assert my_solution.maximumTripletValue(**test_input) == 304
test_input = { "nums": [8,15,6,16,16,9,6,14,4] }
assert my_solution.maximumTripletValue(**test_input) == 144
test_input = { "nums": [16,19,1,7,18,6,18,5,19,18,19] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [16,14,11,2,17,9,10] }
assert my_solution.maximumTripletValue(**test_input) == 238
test_input = { "nums": [3,4,18,2,20,1,1,16,15,8,7,14,19,6] }
assert my_solution.maximumTripletValue(**test_input) == 361
test_input = { "nums": [12,20,14,18,11,16,16,9,12,5,14,17] }
assert my_solution.maximumTripletValue(**test_input) == 255
test_input = { "nums": [12,19,2,9,6] }
assert my_solution.maximumTripletValue(**test_input) == 153
test_input = { "nums": [17,19,14,7,10,18] }
assert my_solution.maximumTripletValue(**test_input) == 216
test_input = { "nums": [3,4,19,10,16,13,6,20] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [11,6,8,9] }
assert my_solution.maximumTripletValue(**test_input) == 45
test_input = { "nums": [7,12,9,19,10,18,16,2,1,3,7,9,7,7] }
assert my_solution.maximumTripletValue(**test_input) == 162
test_input = { "nums": [20,9,20,7,3,7,19] }
assert my_solution.maximumTripletValue(**test_input) == 323
test_input = { "nums": [10,11,3,3,3,2,9,8] }
assert my_solution.maximumTripletValue(**test_input) == 81
test_input = { "nums": [4,20,15,1,17,2,2,4,10,15,2,8,16,6] }
assert my_solution.maximumTripletValue(**test_input) == 323
test_input = { "nums": [15,10,1,18,18,16,7,13,9,11] }
assert my_solution.maximumTripletValue(**test_input) == 252
test_input = { "nums": [10,6,17,11,15,15,18] }
assert my_solution.maximumTripletValue(**test_input) == 108
test_input = { "nums": [3,6,18] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [4,7,20] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [16,12,5] }
assert my_solution.maximumTripletValue(**test_input) == 20
test_input = { "nums": [4,17,15,12,2,16,16,13,6,20,14,17,18,16] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [1,7,18,3,1,11,7,17] }
assert my_solution.maximumTripletValue(**test_input) == 289
test_input = { "nums": [18,16,10,2] }
assert my_solution.maximumTripletValue(**test_input) == 20
test_input = { "nums": [3,10,18,10,7,8] }
assert my_solution.maximumTripletValue(**test_input) == 88
test_input = { "nums": [8,6,20,20,4,12,14,7,13,16,12,15,12] }
assert my_solution.maximumTripletValue(**test_input) == 256
test_input = { "nums": [5,19,11,18,19,14,8,11,4,10] }
assert my_solution.maximumTripletValue(**test_input) == 152
test_input = { "nums": [17,1,16] }
assert my_solution.maximumTripletValue(**test_input) == 256
test_input = { "nums": [20,8,17,14,15,2,7,9,1,10,10,4,19,2,1] }
assert my_solution.maximumTripletValue(**test_input) == 361
test_input = { "nums": [9,16,16] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [2,16,2,19,5,20,2,20,6,6] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [18,3,6,17,4,20,14,6,13,9,5,11] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [12,2,19,15,4,3,18,6,11,9,9,6,15] }
assert my_solution.maximumTripletValue(**test_input) == 288
test_input = { "nums": [10,15,10,13,7,18,18,3,13,15,20,4,6,15] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [10,15,4,19,6,17,7,10,4,12,14,16,9,14] }
assert my_solution.maximumTripletValue(**test_input) == 240
test_input = { "nums": [17,6,3,8,13] }
assert my_solution.maximumTripletValue(**test_input) == 182
test_input = { "nums": [6,18,8,8,16,14,7,18] }
assert my_solution.maximumTripletValue(**test_input) == 198
test_input = { "nums": [7,7,2,19,16,11,3,15,3,15,16,17] }
assert my_solution.maximumTripletValue(**test_input) == 272
test_input = { "nums": [9,3,3,12,9,12,5,7,6,2,9,9,14,9,5] }
assert my_solution.maximumTripletValue(**test_input) == 140
test_input = { "nums": [19,14,15,1,20,10,20,4,10,20,15,15,2,7] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [17,4,10,16,8,20,4,9,11,15,2,7] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [3,8,17,10,10,20,20,8,14,20,1,10,1] }
assert my_solution.maximumTripletValue(**test_input) == 240
test_input = { "nums": [3,4,11,18,10,19,9,11,14,11,18,15,17,19,3] }
assert my_solution.maximumTripletValue(**test_input) == 190
test_input = { "nums": [18,10,5,16,13,1,19,10,17,14,14,20] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [18,3,16,14,15,9,13,2,3] }
assert my_solution.maximumTripletValue(**test_input) == 240
test_input = { "nums": [2,6,19,10,19,14,18,8,3,2] }
assert my_solution.maximumTripletValue(**test_input) == 171 | 1,696,127,400 |
weekly-contest-365-maximum-value-of-an-ordered-triplet-ii | https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii | maximum-value-of-an-ordered-triplet-ii | {
"questionId": "3152",
"questionFrontendId": "2874",
"title": "Maximum Value of an Ordered Triplet II",
"titleSlug": "maximum-value-of-an-ordered-triplet-ii",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 238,
"dislikes": 8,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed integer array nums.
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
* 3 <= nums.length <= 105
* 1 <= nums[i] <= 106
"""
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
| You are given a 0-indexed integer array nums.
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
* 3 <= nums.length <= 105
* 1 <= nums[i] <= 106
Please complete the code below to solve above prblem:
```python
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [12,6,1,2,7] }
assert my_solution.maximumTripletValue(**test_input) == 77
test_input = { "nums": [1,10,3,4,19] }
assert my_solution.maximumTripletValue(**test_input) == 133
test_input = { "nums": [1,2,3] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [2,3,1] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [5,7,8,4] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [1000000,1,1000000] }
assert my_solution.maximumTripletValue(**test_input) == 999999000000
test_input = { "nums": [18,15,8,13,10,9,17,10,2,16,17] }
assert my_solution.maximumTripletValue(**test_input) == 272
test_input = { "nums": [8,6,3,13,2,12,19,5,19,6,10,11,9] }
assert my_solution.maximumTripletValue(**test_input) == 266
test_input = { "nums": [6,11,12,12,7,9,2,11,12,4,19,14,16,8,16] }
assert my_solution.maximumTripletValue(**test_input) == 190
test_input = { "nums": [15,14,17,13,18,17,10,19,2,20,12,9] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [6,14,20,19,19,10,3,15,12,13,8,1,2,15,3] }
assert my_solution.maximumTripletValue(**test_input) == 285
test_input = { "nums": [2,7,19,4,8,20] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [10,13,6,2] }
assert my_solution.maximumTripletValue(**test_input) == 14
test_input = { "nums": [1,19,1,3,18,10,16,9,3,17,8,9] }
assert my_solution.maximumTripletValue(**test_input) == 324
test_input = { "nums": [16,2,10,20,16,2,13,8,19] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [19,11,12,4,17,1,7,20,13,10,14,20,11,19,3] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [16,15,12,5,4,12,15,17,5,18,6,16,1,17,4] }
assert my_solution.maximumTripletValue(**test_input) == 289
test_input = { "nums": [8,10,17,11,2,8,13] }
assert my_solution.maximumTripletValue(**test_input) == 195
test_input = { "nums": [13,4,3,19,16,14,17,6,20,6,16,4] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [1,8,9,18,4,10,3,13,9] }
assert my_solution.maximumTripletValue(**test_input) == 195
test_input = { "nums": [10,10,5,19,2] }
assert my_solution.maximumTripletValue(**test_input) == 95
test_input = { "nums": [15,3,3,18,19,13,7,5,18,1,8,5] }
assert my_solution.maximumTripletValue(**test_input) == 252
test_input = { "nums": [10,20,10] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [14,9,4,20,9] }
assert my_solution.maximumTripletValue(**test_input) == 200
test_input = { "nums": [12,20,5,2,13,17,16,1,5,8,18,15,12] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [7,1,17,17,4,20,14,20] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [16,19,8,8,5,18,12,16,8,14,14,7,19] }
assert my_solution.maximumTripletValue(**test_input) == 266
test_input = { "nums": [17,9,13,7,3,5] }
assert my_solution.maximumTripletValue(**test_input) == 104
test_input = { "nums": [15,12,2,14,15,18,15,20,14,5,14,14,11,13,7] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [17,20,17,13,5,12,8,12,14,10,14,20] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [1,19,10] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [11,16,10,15,10,5,7,3] }
assert my_solution.maximumTripletValue(**test_input) == 90
test_input = { "nums": [5,14,19,12,2,5,18,3,20,12,1,11] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [10,8,12,14] }
assert my_solution.maximumTripletValue(**test_input) == 28
test_input = { "nums": [2,17,18,16,14,20,11,3,18,5,20,6,7] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [19,12,3,19,2,18,3,12,9] }
assert my_solution.maximumTripletValue(**test_input) == 306
test_input = { "nums": [12,9,11,2,11,3,11,17,13,19] }
assert my_solution.maximumTripletValue(**test_input) == 190
test_input = { "nums": [8,13,9,8,7,18,20] }
assert my_solution.maximumTripletValue(**test_input) == 120
test_input = { "nums": [20,8,12,1,7,8,3,3,6] }
assert my_solution.maximumTripletValue(**test_input) == 152
test_input = { "nums": [8,2,16,6,14,14,13,2,11,5,2,12,15,3,3] }
assert my_solution.maximumTripletValue(**test_input) == 210
test_input = { "nums": [19,9,9,9,5] }
assert my_solution.maximumTripletValue(**test_input) == 90
test_input = { "nums": [19,10,5,13,6,9,5,15,19] }
assert my_solution.maximumTripletValue(**test_input) == 266
test_input = { "nums": [14,18,17,8,2,8,14] }
assert my_solution.maximumTripletValue(**test_input) == 224
test_input = { "nums": [11,5,17,13,5,8,8,19,17,1] }
assert my_solution.maximumTripletValue(**test_input) == 228
test_input = { "nums": [18,12,18,14,17,19] }
assert my_solution.maximumTripletValue(**test_input) == 114
test_input = { "nums": [18,17,8,8,18,9] }
assert my_solution.maximumTripletValue(**test_input) == 180
test_input = { "nums": [15,3,2,10,11,10,13,18] }
assert my_solution.maximumTripletValue(**test_input) == 234
test_input = { "nums": [17,17,5,10,19,1,16,3,1,19] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [1,18,4,20,16] }
assert my_solution.maximumTripletValue(**test_input) == 280
test_input = { "nums": [6,20,4,4,2,19,14,10,9,7,20,5,8] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [5,14,15,18,2,9,15,13,11,16,12,20] }
assert my_solution.maximumTripletValue(**test_input) == 320
test_input = { "nums": [7,19,17] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [5,7,14,18,13,11,15,20,8,11,12,4,17,2,16] }
assert my_solution.maximumTripletValue(**test_input) == 288
test_input = { "nums": [4,12,7,2,8,6,9,5,4,1,8] }
assert my_solution.maximumTripletValue(**test_input) == 90
test_input = { "nums": [11,17,2,18,5] }
assert my_solution.maximumTripletValue(**test_input) == 270
test_input = { "nums": [19,13,2,2,19] }
assert my_solution.maximumTripletValue(**test_input) == 323
test_input = { "nums": [14,11,7,6,2,20,16,14,4,12,1,9,16,7,10] }
assert my_solution.maximumTripletValue(**test_input) == 304
test_input = { "nums": [8,15,6,16,16,9,6,14,4] }
assert my_solution.maximumTripletValue(**test_input) == 144
test_input = { "nums": [16,19,1,7,18,6,18,5,19,18,19] }
assert my_solution.maximumTripletValue(**test_input) == 342
test_input = { "nums": [16,14,11,2,17,9,10] }
assert my_solution.maximumTripletValue(**test_input) == 238
test_input = { "nums": [3,4,18,2,20,1,1,16,15,8,7,14,19,6] }
assert my_solution.maximumTripletValue(**test_input) == 361
test_input = { "nums": [12,20,14,18,11,16,16,9,12,5,14,17] }
assert my_solution.maximumTripletValue(**test_input) == 255
test_input = { "nums": [12,19,2,9,6] }
assert my_solution.maximumTripletValue(**test_input) == 153
test_input = { "nums": [17,19,14,7,10,18] }
assert my_solution.maximumTripletValue(**test_input) == 216
test_input = { "nums": [3,4,19,10,16,13,6,20] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [11,6,8,9] }
assert my_solution.maximumTripletValue(**test_input) == 45
test_input = { "nums": [7,12,9,19,10,18,16,2,1,3,7,9,7,7] }
assert my_solution.maximumTripletValue(**test_input) == 162
test_input = { "nums": [20,9,20,7,3,7,19] }
assert my_solution.maximumTripletValue(**test_input) == 323
test_input = { "nums": [10,11,3,3,3,2,9,8] }
assert my_solution.maximumTripletValue(**test_input) == 81
test_input = { "nums": [4,20,15,1,17,2,2,4,10,15,2,8,16,6] }
assert my_solution.maximumTripletValue(**test_input) == 323
test_input = { "nums": [15,10,1,18,18,16,7,13,9,11] }
assert my_solution.maximumTripletValue(**test_input) == 252
test_input = { "nums": [10,6,17,11,15,15,18] }
assert my_solution.maximumTripletValue(**test_input) == 108
test_input = { "nums": [3,6,18] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [4,7,20] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [16,12,5] }
assert my_solution.maximumTripletValue(**test_input) == 20
test_input = { "nums": [4,17,15,12,2,16,16,13,6,20,14,17,18,16] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [1,7,18,3,1,11,7,17] }
assert my_solution.maximumTripletValue(**test_input) == 289
test_input = { "nums": [18,16,10,2] }
assert my_solution.maximumTripletValue(**test_input) == 20
test_input = { "nums": [3,10,18,10,7,8] }
assert my_solution.maximumTripletValue(**test_input) == 88
test_input = { "nums": [8,6,20,20,4,12,14,7,13,16,12,15,12] }
assert my_solution.maximumTripletValue(**test_input) == 256
test_input = { "nums": [5,19,11,18,19,14,8,11,4,10] }
assert my_solution.maximumTripletValue(**test_input) == 152
test_input = { "nums": [17,1,16] }
assert my_solution.maximumTripletValue(**test_input) == 256
test_input = { "nums": [20,8,17,14,15,2,7,9,1,10,10,4,19,2,1] }
assert my_solution.maximumTripletValue(**test_input) == 361
test_input = { "nums": [9,16,16] }
assert my_solution.maximumTripletValue(**test_input) == 0
test_input = { "nums": [2,16,2,19,5,20,2,20,6,6] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [18,3,6,17,4,20,14,6,13,9,5,11] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [12,2,19,15,4,3,18,6,11,9,9,6,15] }
assert my_solution.maximumTripletValue(**test_input) == 288
test_input = { "nums": [10,15,10,13,7,18,18,3,13,15,20,4,6,15] }
assert my_solution.maximumTripletValue(**test_input) == 300
test_input = { "nums": [10,15,4,19,6,17,7,10,4,12,14,16,9,14] }
assert my_solution.maximumTripletValue(**test_input) == 240
test_input = { "nums": [17,6,3,8,13] }
assert my_solution.maximumTripletValue(**test_input) == 182
test_input = { "nums": [6,18,8,8,16,14,7,18] }
assert my_solution.maximumTripletValue(**test_input) == 198
test_input = { "nums": [7,7,2,19,16,11,3,15,3,15,16,17] }
assert my_solution.maximumTripletValue(**test_input) == 272
test_input = { "nums": [9,3,3,12,9,12,5,7,6,2,9,9,14,9,5] }
assert my_solution.maximumTripletValue(**test_input) == 140
test_input = { "nums": [19,14,15,1,20,10,20,4,10,20,15,15,2,7] }
assert my_solution.maximumTripletValue(**test_input) == 360
test_input = { "nums": [17,4,10,16,8,20,4,9,11,15,2,7] }
assert my_solution.maximumTripletValue(**test_input) == 260
test_input = { "nums": [3,8,17,10,10,20,20,8,14,20,1,10,1] }
assert my_solution.maximumTripletValue(**test_input) == 240
test_input = { "nums": [3,4,11,18,10,19,9,11,14,11,18,15,17,19,3] }
assert my_solution.maximumTripletValue(**test_input) == 190
test_input = { "nums": [18,10,5,16,13,1,19,10,17,14,14,20] }
assert my_solution.maximumTripletValue(**test_input) == 340
test_input = { "nums": [18,3,16,14,15,9,13,2,3] }
assert my_solution.maximumTripletValue(**test_input) == 240
test_input = { "nums": [2,6,19,10,19,14,18,8,3,2] }
assert my_solution.maximumTripletValue(**test_input) == 171 | 1,696,127,400 |
weekly-contest-365-minimum-size-subarray-in-infinite-array | https://leetcode.com/problems/minimum-size-subarray-in-infinite-array | minimum-size-subarray-in-infinite-array | {
"questionId": "3141",
"questionFrontendId": "2875",
"title": "Minimum Size Subarray in Infinite Array",
"titleSlug": "minimum-size-subarray-in-infinite-array",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 309,
"dislikes": 19,
"categoryTitle": "Algorithms"
} | """
You are given a 0-indexed array nums and an integer target.
A 0-indexed array infinite_nums is generated by infinitely appending the elements of nums to itself.
Return the length of the shortest subarray of the array infinite_nums with a sum equal to target. If there is no such subarray return -1.
Example 1:
Input: nums = [1,2,3], target = 5
Output: 2
Explanation: In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
Example 2:
Input: nums = [1,1,1,2,3], target = 4
Output: 2
Explanation: In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
Example 3:
Input: nums = [2,4,6,8], target = 3
Output: -1
Explanation: In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
It can be proven that there is no subarray with sum equal to target = 3.
Constraints:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 105
* 1 <= target <= 109
"""
class Solution:
def minSizeSubarray(self, nums: List[int], target: int) -> int:
| You are given a 0-indexed array nums and an integer target.
A 0-indexed array infinite_nums is generated by infinitely appending the elements of nums to itself.
Return the length of the shortest subarray of the array infinite_nums with a sum equal to target. If there is no such subarray return -1.
Example 1:
Input: nums = [1,2,3], target = 5
Output: 2
Explanation: In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
Example 2:
Input: nums = [1,1,1,2,3], target = 4
Output: 2
Explanation: In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
Example 3:
Input: nums = [2,4,6,8], target = 3
Output: -1
Explanation: In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
It can be proven that there is no subarray with sum equal to target = 3.
Constraints:
* 1 <= nums.length <= 105
* 1 <= nums[i] <= 105
* 1 <= target <= 109
Please complete the code below to solve above prblem:
```python
class Solution:
def minSizeSubarray(self, nums: List[int], target: int) -> int:
``` |
my_solution = Solution()
test_input = { "nums": [1,2,3], "target": 5 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [1,1,1,2,3], "target": 4 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [2,4,6,8], "target": 3 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [2,1,5,7,7,1,6,3], "target": 39 }
assert my_solution.minSizeSubarray(**test_input) == 9
test_input = { "nums": [17,4,3,14,17,6,15], "target": 85 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [18,3,11,19,7,16,6,7,3,6,18,9,9,1,14,17,15,14,12,10], "target": 7 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [2,3,5,2,3,4,4,1,3,5,2,2,5,1,1,2,5], "target": 19 }
assert my_solution.minSizeSubarray(**test_input) == 6
test_input = { "nums": [4,1,5,7,1,6,1,7,2,2,5,5,5,6,3], "target": 20 }
assert my_solution.minSizeSubarray(**test_input) == 5
test_input = { "nums": [7,3,5], "target": 36 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [1,11,6,4,13], "target": 22 }
assert my_solution.minSizeSubarray(**test_input) == 4
test_input = { "nums": [1,2,2,2,1,2,1,2,1,2,1], "target": 83 }
assert my_solution.minSizeSubarray(**test_input) == 53
test_input = { "nums": [4,3,5,4,5,4,4,4,5,7,4,5,6,3,1,4,6,3,7], "target": 15 }
assert my_solution.minSizeSubarray(**test_input) == 3
test_input = { "nums": [1,2,3,2,1,5,3,4,5], "target": 53 }
assert my_solution.minSizeSubarray(**test_input) == 19
test_input = { "nums": [2,5,6,4], "target": 95 }
assert my_solution.minSizeSubarray(**test_input) == 22
test_input = { "nums": [6,6,4,5,2,8,1,8,7,6,6,7,4,1,9,6,8,8], "target": 55 }
assert my_solution.minSizeSubarray(**test_input) == 9
test_input = { "nums": [1,2,8,19,17,2,3,11,8,12,16,18,7], "target": 36 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [12,14,4,14,13,16,5], "target": 36 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], "target": 37 }
assert my_solution.minSizeSubarray(**test_input) == 37
test_input = { "nums": [5,7,2,6,4,1,6,7,1,4,7,6,7,7,6,6,4,6,8], "target": 90 }
assert my_solution.minSizeSubarray(**test_input) == 17
test_input = { "nums": [3,5,15,17,6,17,10,15,10,4,6], "target": 25 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [14,5], "target": 23 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [1,1,5,9], "target": 68 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [11,1,17,14,9,16,5,3,7,16,14,18,17,10], "target": 82 }
assert my_solution.minSizeSubarray(**test_input) == 6
test_input = { "nums": [9,6,8,4,3,4,6,4,7,2,6,9,2,4,5,4], "target": 71 }
assert my_solution.minSizeSubarray(**test_input) == 14
test_input = { "nums": [2,4,4,3,2,3,2,5,3,1,5,1,4,2,6], "target": 23 }
assert my_solution.minSizeSubarray(**test_input) == 7
test_input = { "nums": [3,6], "target": 66 }
assert my_solution.minSizeSubarray(**test_input) == 15
test_input = { "nums": [1,4,8,5,9,8,8,2,3,1,6,2,7,5,5,3,3,5,6], "target": 57 }
assert my_solution.minSizeSubarray(**test_input) == 10
test_input = { "nums": [1,6,5,5,1,1,2,5,3,1,5,3,2,4,6,6], "target": 56 }
assert my_solution.minSizeSubarray(**test_input) == 16
test_input = { "nums": [5,3,5,4,3,1,3,3,1,3,3,5,5,4,5,5,5,5], "target": 8 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [2,2,1,3,2,2,2,3,3,2,1,3,3,2,3,3], "target": 93 }
assert my_solution.minSizeSubarray(**test_input) == 40
test_input = { "nums": [5,1,4,1,5,6], "target": 71 }
assert my_solution.minSizeSubarray(**test_input) == 19
test_input = { "nums": [3,5,6,6,1,8,4,9,6,2,3,9,6,8,7,3,6,1,8,6], "target": 60 }
assert my_solution.minSizeSubarray(**test_input) == 11
test_input = { "nums": [12,15,9,3,3,12,13,14,7,11,7,15,12,5,11], "target": 18 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [3,11,10,12,9,13,9], "target": 19 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [3,4,4], "target": 35 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [4,5,2,5,5,5,1], "target": 87 }
assert my_solution.minSizeSubarray(**test_input) == 23
test_input = { "nums": [2,13,15,3,6,7,16,7,9,10,4,3,12,9,13,2,9,13,15], "target": 4 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [1,2,16,10,15,15,13,11,10,6,12,15,9], "target": 30 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [3,5], "target": 85 }
assert my_solution.minSizeSubarray(**test_input) == 21
test_input = { "nums": [1,4,3,1,4,4,2,3], "target": 6 }
assert my_solution.minSizeSubarray(**test_input) == 2
test_input = { "nums": [2,10,12,10,4,4,12,5,12,12,5], "target": 33 }
assert my_solution.minSizeSubarray(**test_input) == 4
test_input = { "nums": [5,9,7,10,4,7,9,11,6,3,1,8,6,1,11,1,1], "target": 72 }
assert my_solution.minSizeSubarray(**test_input) == 11
test_input = { "nums": [19,18,6], "target": 56 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [3,5,7,6,5,3,7,7,1,5,3,1,5,6,3,1,6,1,3], "target": 20 }
assert my_solution.minSizeSubarray(**test_input) == 4
test_input = { "nums": [5,5,4,1,2,2,2,3,2,4,2,5], "target": 56 }
assert my_solution.minSizeSubarray(**test_input) == 16
test_input = { "nums": [1,1,1,1,1,1,1,1], "target": 22 }
assert my_solution.minSizeSubarray(**test_input) == 22
test_input = { "nums": [1,2], "target": 72 }
assert my_solution.minSizeSubarray(**test_input) == 48
test_input = { "nums": [4,3,6,6,2,6,1,6,7,5,7,6,1,5,7], "target": 82 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [4,1,5,2,3,1,2,4,1,5,3,3,5,2,6,6,5,2,1], "target": 63 }
assert my_solution.minSizeSubarray(**test_input) == 20
test_input = { "nums": [8,2,5,4,1,6,6,6,6,4,4,5,5,9,6,6,9,2], "target": 4 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [18,12,13,9,17,11], "target": 82 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [13,3,1,5,13,7,12,5], "target": 35 }
assert my_solution.minSizeSubarray(**test_input) == 5
test_input = { "nums": [4,10,12,6,2,2,4,12,6,1,1,2,2,10,6,11,5,4,9], "target": 49 }
assert my_solution.minSizeSubarray(**test_input) == 7
test_input = { "nums": [8], "target": 68 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [7,2,6,7,6,4,4,1,6,4,1,7,7,2,2,4,4,4], "target": 29 }
assert my_solution.minSizeSubarray(**test_input) == 6
test_input = { "nums": [4,7,6,12,10,13,7,6,6,1,15,2,4,8,12], "target": 43 }
assert my_solution.minSizeSubarray(**test_input) == 6
test_input = { "nums": [4,10], "target": 10 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [4,3,2,4,5,3,7,12,2,2,10], "target": 43 }
assert my_solution.minSizeSubarray(**test_input) == 8
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1], "target": 58 }
assert my_solution.minSizeSubarray(**test_input) == 58
test_input = { "nums": [1,1,1,1,1], "target": 20 }
assert my_solution.minSizeSubarray(**test_input) == 20
test_input = { "nums": [4,3], "target": 23 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [3,2,1,3,2,1,3,1,1,1,2,1,2,1,2,3,3,1], "target": 78 }
assert my_solution.minSizeSubarray(**test_input) == 41
test_input = { "nums": [3,2,4,2,4,2,5,4,5,3,4,4,2,4,4,1], "target": 19 }
assert my_solution.minSizeSubarray(**test_input) == 5
test_input = { "nums": [17], "target": 1 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [10,12,1,11,9,5,4,5,1,10,8,12,5,4], "target": 82 }
assert my_solution.minSizeSubarray(**test_input) == 10
test_input = { "nums": [6], "target": 44 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [4,6,2,6,3,5,2,5,5,4,3,1,5,4,5,5,4,5,5,6], "target": 12 }
assert my_solution.minSizeSubarray(**test_input) == 3
test_input = { "nums": [14,4,13,12,18,8,4,15,4,14,17,4,2], "target": 8 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [4,4,6,5,3,4,1,4,2,6,3], "target": 32 }
assert my_solution.minSizeSubarray(**test_input) == 9
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], "target": 28 }
assert my_solution.minSizeSubarray(**test_input) == 28
test_input = { "nums": [5,10,1,3,14,7,13,6,5,7,10,3,10,5,8,5,7,5,6,7], "target": 25 }
assert my_solution.minSizeSubarray(**test_input) == 4
test_input = { "nums": [12,3,4,10,5,8,12,7,12,7,5,8,4,8,11,11], "target": 48 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [3,11,5,5,3,10,12,12,12,3,10], "target": 88 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [8,2,10,5], "target": 28 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [3,5,1,4,5,2,5,3,1,2,1,1,1,3,3,3,5], "target": 68 }
assert my_solution.minSizeSubarray(**test_input) == 23
test_input = { "nums": [1,4,1,4,4], "target": 21 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [15,8,8,19,8,12,15,3,15,8,10,9], "target": 77 }
assert my_solution.minSizeSubarray(**test_input) == 7
test_input = { "nums": [4,4], "target": 80 }
assert my_solution.minSizeSubarray(**test_input) == 20
test_input = { "nums": [4,9,3,7,5,4,5,1,3,5], "target": 69 }
assert my_solution.minSizeSubarray(**test_input) == 14
test_input = { "nums": [2,8,2,8,1,5,8,9,3,4,6,6,6,1,7,9], "target": 93 }
assert my_solution.minSizeSubarray(**test_input) == 17
test_input = { "nums": [7,11,14,12,3,16,11,9], "target": 10 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [2,1,2,1,3,2,1,1,2], "target": 17 }
assert my_solution.minSizeSubarray(**test_input) == 10
test_input = { "nums": [5,20,18,2,8], "target": 45 }
assert my_solution.minSizeSubarray(**test_input) == 4
test_input = { "nums": [2,1,2,1,2,1,1,1,2,2], "target": 58 }
assert my_solution.minSizeSubarray(**test_input) == 38
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], "target": 17 }
assert my_solution.minSizeSubarray(**test_input) == 17
test_input = { "nums": [18,6,8,17,3,10,14,12,4,13,12,10,5,18,11], "target": 95 }
assert my_solution.minSizeSubarray(**test_input) == 9
test_input = { "nums": [19,12,14], "target": 57 }
assert my_solution.minSizeSubarray(**test_input) == 4
test_input = { "nums": [3,13,14], "target": 11 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [17,6,8,7,4,6,6,3,8,1,8,10,18,13,2], "target": 32 }
assert my_solution.minSizeSubarray(**test_input) == 3
test_input = { "nums": [3,12,4,9,5,2,2,9,9,6,9,11,9], "target": 41 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [3,14,2,9,5,14,15,4,3,4,17,11], "target": 3 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [2,5,3,6,3,6,1,1,5,1], "target": 37 }
assert my_solution.minSizeSubarray(**test_input) == -1
test_input = { "nums": [13,6,4,7,3,6,4,10,13,10,5,4,2,1,7,11,3,3,12], "target": 51 }
assert my_solution.minSizeSubarray(**test_input) == 7
test_input = { "nums": [1,10,9,16,3,10,2,5,1,10], "target": 83 }
assert my_solution.minSizeSubarray(**test_input) == 11
test_input = { "nums": [2,10,13,3,4,19,14,20,11,15,4,3,17,8,2,3,1,13,8], "target": 1 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [4,5,5,3,5,4,2,11,5,9,4,6], "target": 41 }
assert my_solution.minSizeSubarray(**test_input) == 7
test_input = { "nums": [2,8,9,6,11,17,3,6,9,7,2,8,9,11,19], "target": 39 }
assert my_solution.minSizeSubarray(**test_input) == 3
test_input = { "nums": [8,13,9,5,8,6,17,16,14,7,10,15,16], "target": 8 }
assert my_solution.minSizeSubarray(**test_input) == 1
test_input = { "nums": [10,20,16,1,11,18,13,6,13,6,9,14,16,12,13,7,19], "target": 59 }
assert my_solution.minSizeSubarray(**test_input) == 5
test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1], "target": 6 }
assert my_solution.minSizeSubarray(**test_input) == 6 | 1,696,127,400 |
weekly-contest-365-count-visited-nodes-in-a-directed-graph | https://leetcode.com/problems/count-visited-nodes-in-a-directed-graph | count-visited-nodes-in-a-directed-graph | {
"questionId": "3140",
"questionFrontendId": "2876",
"title": "Count Visited Nodes in a Directed Graph",
"titleSlug": "count-visited-nodes-in-a-directed-graph",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 274,
"dislikes": 4,
"categoryTitle": "Algorithms"
} | """
There is a directed graph consisting of n nodes numbered from 0 to n - 1 and n directed edges.
You are given a 0-indexed array edges where edges[i] indicates that there is an edge from node i to node edges[i].
Consider the following process on the graph:
* You start from a node x and keep visiting other nodes through edges until you reach a node that you have already visited before on this same process.
Return an array answer where answer[i] is the number of different nodes that you will visit if you perform the process starting from node i.
Example 1:
[https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.png]
Input: edges = [1,2,0,0]
Output: [3,3,3,4]
Explanation: We perform the process starting from each node in the following way:
- Starting from node 0, we visit the nodes 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 3.
- Starting from node 1, we visit the nodes 1 -> 2 -> 0 -> 1. The number of different nodes we visit is 3.
- Starting from node 2, we visit the nodes 2 -> 0 -> 1 -> 2. The number of different nodes we visit is 3.
- Starting from node 3, we visit the nodes 3 -> 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 4.
Example 2:
[https://assets.leetcode.com/uploads/2023/08/31/graaph2drawio.png]
Input: edges = [1,2,3,4,0]
Output: [5,5,5,5,5]
Explanation: Starting from any node we can visit every node in the graph in the process.
Constraints:
* n == edges.length
* 2 <= n <= 105
* 0 <= edges[i] <= n - 1
* edges[i] != i
"""
class Solution:
def countVisitedNodes(self, edges: List[int]) -> List[int]:
| There is a directed graph consisting of n nodes numbered from 0 to n - 1 and n directed edges.
You are given a 0-indexed array edges where edges[i] indicates that there is an edge from node i to node edges[i].
Consider the following process on the graph:
* You start from a node x and keep visiting other nodes through edges until you reach a node that you have already visited before on this same process.
Return an array answer where answer[i] is the number of different nodes that you will visit if you perform the process starting from node i.
Example 1:
[https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.png]
Input: edges = [1,2,0,0]
Output: [3,3,3,4]
Explanation: We perform the process starting from each node in the following way:
- Starting from node 0, we visit the nodes 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 3.
- Starting from node 1, we visit the nodes 1 -> 2 -> 0 -> 1. The number of different nodes we visit is 3.
- Starting from node 2, we visit the nodes 2 -> 0 -> 1 -> 2. The number of different nodes we visit is 3.
- Starting from node 3, we visit the nodes 3 -> 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 4.
Example 2:
[https://assets.leetcode.com/uploads/2023/08/31/graaph2drawio.png]
Input: edges = [1,2,3,4,0]
Output: [5,5,5,5,5]
Explanation: Starting from any node we can visit every node in the graph in the process.
Constraints:
* n == edges.length
* 2 <= n <= 105
* 0 <= edges[i] <= n - 1
* edges[i] != i
Please complete the code below to solve above prblem:
```python
class Solution:
def countVisitedNodes(self, edges: List[int]) -> List[int]:
``` |
my_solution = Solution()
test_input = { "edges": [1,2,0,0] }
assert my_solution.countVisitedNodes(**test_input) == [3,3,3,4]
test_input = { "edges": [1,2,3,4,0] }
assert my_solution.countVisitedNodes(**test_input) == [5,5,5,5,5]
test_input = { "edges": [3,6,1,0,5,7,4,3] }
assert my_solution.countVisitedNodes(**test_input) == [2,7,8,2,5,4,6,3]
test_input = { "edges": [7,0,7,0,5,3,3,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,3,3,5,4,4,2]
test_input = { "edges": [6,3,6,1,0,8,0,6,6] }
assert my_solution.countVisitedNodes(**test_input) == [2,2,3,2,3,4,2,3,3]
test_input = { "edges": [8,17,14,8,14,12,16,11,4,14,19,6,8,8,2,10,2,1,1,18] }
assert my_solution.countVisitedNodes(**test_input) == [5,2,2,5,3,6,4,6,4,3,5,5,5,5,2,6,3,2,3,4]
test_input = { "edges": [11,9,6,8,3,2,8,11,14,2,3,7,2,2,1] }
assert my_solution.countVisitedNodes(**test_input) == [3,6,6,7,8,7,6,2,6,6,8,2,7,7,6]
test_input = { "edges": [9,4,4,8,5,2,3,6,5,5] }
assert my_solution.countVisitedNodes(**test_input) == [5,4,3,5,3,3,6,7,4,4]
test_input = { "edges": [1,0,1,1] }
assert my_solution.countVisitedNodes(**test_input) == [2,2,3,3]
test_input = { "edges": [4,0,3,2,3] }
assert my_solution.countVisitedNodes(**test_input) == [4,5,2,2,3]
test_input = { "edges": [7,7,0,9,5,6,10,16,7,4,15,13,2,16,1,7,6] }
assert my_solution.countVisitedNodes(**test_input) == [6,6,7,9,7,6,5,5,6,8,5,7,8,6,7,5,5]
test_input = { "edges": [2,6,3,1,5,3,5] }
assert my_solution.countVisitedNodes(**test_input) == [6,4,5,4,5,4,4]
test_input = { "edges": [15,4,13,12,12,2,11,6,14,10,15,3,5,5,2,4] }
assert my_solution.countVisitedNodes(**test_input) == [7,6,3,5,5,3,7,8,5,8,7,6,4,3,4,6]
test_input = { "edges": [1,5,0,5,2,7,1,2] }
assert my_solution.countVisitedNodes(**test_input) == [5,5,5,6,6,5,6,5]
test_input = { "edges": [9,6,13,1,11,4,17,9,2,18,15,4,14,15,7,2,18,16,1] }
assert my_solution.countVisitedNodes(**test_input) == [7,5,3,6,2,3,5,7,4,6,4,2,9,3,8,3,5,5,5]
test_input = { "edges": [18,18,4,6,1,8,14,4,16,11,13,6,10,10,6,18,14,11,4] }
assert my_solution.countVisitedNodes(**test_input) == [4,3,4,3,3,5,2,4,4,4,2,3,3,2,2,4,3,4,3]
test_input = { "edges": [5,4,1,6,3,10,3,10,11,10,8,1] }
assert my_solution.countVisitedNodes(**test_input) == [9,4,5,2,3,8,2,8,6,8,7,5]
test_input = { "edges": [8,6,3,1,0,6,8,1,4,7,8] }
assert my_solution.countVisitedNodes(**test_input) == [3,5,7,6,3,5,4,6,3,7,4]
test_input = { "edges": [9,5,18,15,8,4,3,3,18,5,13,0,1,18,9,6,18,9,14,15] }
assert my_solution.countVisitedNodes(**test_input) == [7,7,7,3,6,6,3,4,6,6,8,8,8,7,6,3,7,7,6,4]
test_input = { "edges": [5,2,1,0,6,9,10,12,12,2,16,2,9,17,0,4,9,6] }
assert my_solution.countVisitedNodes(**test_input) == [5,2,2,6,7,4,6,5,5,3,5,3,4,8,6,8,4,7]
test_input = { "edges": [6,4,1,2,3,2,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,4,4,4,4,5,2]
test_input = { "edges": [1,13,4,12,15,11,1,8,15,10,1,3,0,3,2,2] }
assert my_solution.countVisitedNodes(**test_input) == [5,5,3,5,3,7,6,5,4,7,6,6,5,5,4,3]
test_input = { "edges": [2,2,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,2]
test_input = { "edges": [11,8,8,11,5,8,9,11,6,8,0,12,9,12] }
assert my_solution.countVisitedNodes(**test_input) == [6,4,4,6,5,4,3,6,3,3,7,5,4,5]
test_input = { "edges": [2,3,6,8,0,4,8,6,1] }
assert my_solution.countVisitedNodes(**test_input) == [6,3,5,3,7,8,4,5,3]
test_input = { "edges": [2,7,17,14,3,14,11,12,9,0,15,18,1,18,0,19,11,4,1,0] }
assert my_solution.countVisitedNodes(**test_input) == [6,3,6,6,6,7,6,3,8,7,9,5,3,5,6,8,6,6,4,7]
test_input = { "edges": [5,17,10,13,16,4,7,10,19,6,15,6,9,0,1,0,12,18,10,16] }
assert my_solution.countVisitedNodes(**test_input) == [10,13,11,12,10,10,10,10,12,10,10,11,10,11,14,10,10,12,11,11]
test_input = { "edges": [1,2,6,6,1,4,4] }
assert my_solution.countVisitedNodes(**test_input) == [5,4,4,5,4,5,4]
test_input = { "edges": [2,0,0,2] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,2,3]
test_input = { "edges": [12,10,5,0,12,8,0,4,3,1,9,4,6] }
assert my_solution.countVisitedNodes(**test_input) == [3,3,7,4,4,6,3,5,5,3,3,5,3]
test_input = { "edges": [8,4,0,0,8,2,3,8,7] }
assert my_solution.countVisitedNodes(**test_input) == [3,4,4,4,3,5,5,2,2]
test_input = { "edges": [6,7,1,10,2,10,3,5,10,4,2] }
assert my_solution.countVisitedNodes(**test_input) == [8,5,5,6,6,5,7,5,6,7,5]
test_input = { "edges": [2,7,5,4,8,7,2,3,0] }
assert my_solution.countVisitedNodes(**test_input) == [7,8,7,7,7,7,8,7,7]
test_input = { "edges": [2,3,1,1,0,4] }
assert my_solution.countVisitedNodes(**test_input) == [4,2,3,2,5,6]
test_input = { "edges": [5,2,3,1,3,1] }
assert my_solution.countVisitedNodes(**test_input) == [5,3,3,3,4,4]
test_input = { "edges": [7,6,12,0,1,9,13,6,9,6,0,0,3,9,12,13,0] }
assert my_solution.countVisitedNodes(**test_input) == [5,4,8,6,5,4,3,4,4,3,6,6,7,3,8,4,6]
test_input = { "edges": [1,4,9,11,11,11,14,10,11,14,2,0,14,5,10] }
assert my_solution.countVisitedNodes(**test_input) == [4,4,4,5,4,5,5,5,5,4,4,4,5,6,4]
test_input = { "edges": [4,3,3,1,3] }
assert my_solution.countVisitedNodes(**test_input) == [4,2,3,2,3]
test_input = { "edges": [7,7,6,8,0,7,8,0,0,10,9] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,5,4,3,3,4,2,3,2,2]
test_input = { "edges": [16,5,11,9,7,17,16,8,14,5,5,1,0,8,0,16,14,15,19,4] }
assert my_solution.countVisitedNodes(**test_input) == [3,7,9,8,6,6,4,5,4,7,7,8,4,5,3,4,3,5,8,7]
test_input = { "edges": [1,8,10,6,2,1,8,9,6,12,5,10,3] }
assert my_solution.countVisitedNodes(**test_input) == [4,3,6,3,7,4,2,6,2,5,5,6,4]
test_input = { "edges": [4,0,1,5,0,2] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,4,6,2,5]
test_input = { "edges": [9,13,1,2,13,1,0,5,10,8,2,2,3,12] }
assert my_solution.countVisitedNodes(**test_input) == [9,5,5,5,6,6,10,7,7,8,6,6,5,5]
test_input = { "edges": [12,13,16,11,17,11,2,15,12,14,4,9,3,4,17,3,4,9] }
assert my_solution.countVisitedNodes(**test_input) == [7,6,6,5,4,5,7,7,7,3,5,4,6,5,3,6,5,3]
test_input = { "edges": [2,0,1] }
assert my_solution.countVisitedNodes(**test_input) == [3,3,3]
test_input = { "edges": [7,10,15,18,7,1,7,16,11,8,2,13,13,15,16,0,18,5,16] }
assert my_solution.countVisitedNodes(**test_input) == [4,8,6,3,4,9,4,3,8,9,7,7,7,6,3,5,2,10,2]
test_input = { "edges": [1,2,0] }
assert my_solution.countVisitedNodes(**test_input) == [3,3,3]
test_input = { "edges": [10,13,4,11,11,6,9,2,7,4,5,7,0,11] }
assert my_solution.countVisitedNodes(**test_input) == [9,6,4,5,4,7,6,4,5,5,8,4,10,5]
test_input = { "edges": [12,15,15,2,10,1,5,6,1,2,11,13,10,4,2,0,1] }
assert my_solution.countVisitedNodes(**test_input) == [6,8,8,9,4,9,10,11,9,9,4,4,5,4,9,7,9]
test_input = { "edges": [1,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,2]
test_input = { "edges": [7,17,3,7,7,12,15,1,14,15,16,3,13,0,3,8,0,11,1] }
assert my_solution.countVisitedNodes(**test_input) == [6,5,6,5,6,9,9,5,7,9,8,5,8,7,6,8,7,5,6]
test_input = { "edges": [9,4,6,2,8,6,7,4,9,8,0,2,7] }
assert my_solution.countVisitedNodes(**test_input) == [3,4,6,7,3,6,5,4,2,2,4,7,5]
test_input = { "edges": [13,10,6,12,12,3,4,3,10,12,1,7,8,12,9] }
assert my_solution.countVisitedNodes(**test_input) == [6,2,7,5,5,6,6,6,3,5,2,7,4,5,6]
test_input = { "edges": [10,3,4,4,5,7,2,9,7,0,5] }
assert my_solution.countVisitedNodes(**test_input) == [5,8,7,7,6,5,8,5,6,5,5]
test_input = { "edges": [2,4,0,4,2] }
assert my_solution.countVisitedNodes(**test_input) == [2,4,2,4,3]
test_input = { "edges": [2,2,1] }
assert my_solution.countVisitedNodes(**test_input) == [3,2,2]
test_input = { "edges": [19,15,1,6,8,15,5,6,4,4,19,13,3,0,15,10,13,5,6,3] }
assert my_solution.countVisitedNodes(**test_input) == [7,7,8,6,2,6,6,7,2,3,6,9,7,8,7,6,9,7,7,6]
test_input = { "edges": [11,9,5,0,5,3,9,8,1,10,4,4] }
assert my_solution.countVisitedNodes(**test_input) == [5,8,6,5,5,5,8,10,9,7,6,5]
test_input = { "edges": [13,10,12,11,5,17,0,10,7,16,5,4,9,3,15,5,4,1] }
assert my_solution.countVisitedNodes(**test_input) == [9,4,9,7,5,4,10,5,6,7,4,6,8,8,6,5,6,4]
test_input = { "edges": [7,0,9,0,7,6,2,0,7,7] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,4,3,3,6,5,2,3,3]
test_input = { "edges": [1,0,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,2,3]
test_input = { "edges": [5,9,10,17,12,3,15,5,0,3,15,5,5,15,17,5,13,15,1] }
assert my_solution.countVisitedNodes(**test_input) == [5,6,6,4,6,4,5,5,6,5,5,5,5,5,5,4,6,4,7]
test_input = { "edges": [1,5,1,8,1,0,1,4,6] }
assert my_solution.countVisitedNodes(**test_input) == [3,3,4,6,4,3,4,5,5]
test_input = { "edges": [5,3,3,4,1,4] }
assert my_solution.countVisitedNodes(**test_input) == [5,3,4,3,3,4]
test_input = { "edges": [7,12,12,5,10,11,5,0,3,12,12,9,1,3,7] }
assert my_solution.countVisitedNodes(**test_input) == [2,2,3,6,4,5,6,2,7,3,3,4,2,7,3]
test_input = { "edges": [3,3,3,1] }
assert my_solution.countVisitedNodes(**test_input) == [3,2,3,2]
test_input = { "edges": [13,3,15,10,12,7,13,15,3,0,1,5,15,12,10,9] }
assert my_solution.countVisitedNodes(**test_input) == [5,3,6,3,6,7,6,6,4,5,3,8,5,5,4,5]
test_input = { "edges": [8,9,0,9,0,0,9,0,7,6,5,0] }
assert my_solution.countVisitedNodes(**test_input) == [3,3,4,3,4,4,2,3,3,2,5,4]
test_input = { "edges": [2,0,7,7,3,3,0,4,5,8] }
assert my_solution.countVisitedNodes(**test_input) == [5,6,4,3,3,4,6,3,5,6]
test_input = { "edges": [13,8,7,13,10,6,11,13,13,6,8,6,0,10] }
assert my_solution.countVisitedNodes(**test_input) == [4,4,5,4,4,3,2,4,3,3,3,2,5,3]
test_input = { "edges": [12,14,5,17,7,0,15,18,5,10,6,18,10,11,1,1,18,16,0] }
assert my_solution.countVisitedNodes(**test_input) == [7,2,9,11,10,8,4,9,9,6,5,9,6,10,2,3,9,10,8]
test_input = { "edges": [8,6,17,7,12,10,13,14,10,10,2,15,11,5,4,13,12,12] }
assert my_solution.countVisitedNodes(**test_input) == [10,10,8,12,9,8,9,11,9,9,8,8,8,8,10,8,9,8]
test_input = { "edges": [1,0,1,1,1] }
assert my_solution.countVisitedNodes(**test_input) == [2,2,3,3,3]
test_input = { "edges": [17,16,15,5,11,4,4,10,14,1,17,1,4,3,12,17,14,10] }
assert my_solution.countVisitedNodes(**test_input) == [3,6,4,8,6,7,7,3,7,7,2,6,6,9,6,3,6,2]
test_input = { "edges": [14,10,16,16,12,13,13,16,13,14,15,14,1,4,13,11,4,7] }
assert my_solution.countVisitedNodes(**test_input) == [9,8,10,10,8,9,9,10,9,9,8,8,8,8,8,8,9,11]
test_input = { "edges": [2,3,0,1] }
assert my_solution.countVisitedNodes(**test_input) == [2,2,2,2]
test_input = { "edges": [8,0,11,2,0,12,0,4,11,12,0,7,3] }
assert my_solution.countVisitedNodes(**test_input) == [5,6,6,7,5,9,6,5,5,9,6,5,8]
test_input = { "edges": [3,0,1,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,4,2]
test_input = { "edges": [1,3,0,1,10,11,2,6,0,5,1,4] }
assert my_solution.countVisitedNodes(**test_input) == [3,2,4,2,4,6,5,6,4,7,3,5]
test_input = { "edges": [1,16,10,6,15,10,7,9,2,15,5,6,13,1,0,16,13] }
assert my_solution.countVisitedNodes(**test_input) == [4,3,3,8,5,2,7,6,4,5,2,8,4,3,5,4,3]
test_input = { "edges": [4,15,10,11,0,7,9,10,1,2,9,12,5,10,6,13] }
assert my_solution.countVisitedNodes(**test_input) == [2,6,3,8,2,5,4,4,7,3,3,7,6,4,5,5]
test_input = { "edges": [4,9,8,9,1,4,7,11,5,6,4,6,4,4,2] }
assert my_solution.countVisitedNodes(**test_input) == [7,5,9,5,6,7,3,3,8,4,7,3,7,7,10]
test_input = { "edges": [16,14,1,6,6,1,1,15,16,16,13,14,9,3,3,11,10] }
assert my_solution.countVisitedNodes(**test_input) == [8,4,5,4,5,5,4,7,8,8,6,5,9,5,4,6,7]
test_input = { "edges": [6,11,15,7,5,8,11,5,14,2,6,0,8,5,13,5] }
assert my_solution.countVisitedNodes(**test_input) == [3,4,6,6,5,4,3,5,4,7,4,3,5,4,4,5]
test_input = { "edges": [14,17,11,8,5,4,10,17,3,3,2,17,13,1,17,5,5,0] }
assert my_solution.countVisitedNodes(**test_input) == [3,4,5,2,2,2,7,4,2,3,6,4,6,5,3,3,3,3]
test_input = { "edges": [1,6,8,6,2,4,2,5,9,3] }
assert my_solution.countVisitedNodes(**test_input) == [7,6,5,5,6,7,5,8,5,5]
test_input = { "edges": [1,5,4,1,5,3,4,0] }
assert my_solution.countVisitedNodes(**test_input) == [4,3,5,3,4,3,5,5]
test_input = { "edges": [17,4,3,7,3,1,15,15,13,18,4,14,1,10,13,1,0,15,16] }
assert my_solution.countVisitedNodes(**test_input) == [7,5,6,5,5,6,6,5,8,10,6,9,6,7,8,5,8,6,9]
test_input = { "edges": [12,2,6,6,7,8,8,13,1,12,15,1,8,3,8,2,6] }
assert my_solution.countVisitedNodes(**test_input) == [6,4,4,5,8,5,4,7,4,6,6,5,5,6,5,5,5]
test_input = { "edges": [3,0,0,2] }
assert my_solution.countVisitedNodes(**test_input) == [3,4,3,3]
test_input = { "edges": [5,0,1,1,2,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,3,4,4,5,2]
test_input = { "edges": [16,2,13,6,7,10,1,1,7,14,7,13,9,16,4,8,15] }
assert my_solution.countVisitedNodes(**test_input) == [8,7,7,9,8,9,8,7,7,10,8,8,11,7,9,7,7]
test_input = { "edges": [14,5,0,16,7,15,1,18,18,6,11,15,0,2,3,0,17,4,12] }
assert my_solution.countVisitedNodes(**test_input) == [9,12,10,9,9,11,13,9,10,14,12,11,9,11,9,10,9,9,9]
test_input = { "edges": [7,8,1,9,7,10,1,4,2,0,9] }
assert my_solution.countVisitedNodes(**test_input) == [3,3,3,5,2,6,4,2,3,4,5]
test_input = { "edges": [5,3,6,2,1,3,1,3] }
assert my_solution.countVisitedNodes(**test_input) == [6,4,4,4,5,5,4,5]
test_input = { "edges": [8,14,5,13,6,9,8,11,9,4,11,14,5,12,10,10,12] }
assert my_solution.countVisitedNodes(**test_input) == [5,4,6,8,4,5,4,4,4,4,3,3,6,7,3,4,7]
test_input = { "edges": [6,13,11,11,6,3,3,6,1,12,4,7,14,11,0,10] }
assert my_solution.countVisitedNodes(**test_input) == [5,6,5,4,5,5,4,4,7,8,6,4,7,5,6,7]
test_input = { "edges": [12,8,6,1,5,0,1,1,11,0,12,2,0] }
assert my_solution.countVisitedNodes(**test_input) == [2,5,5,6,4,3,5,6,5,3,3,5,2]
test_input = { "edges": [8,2,7,17,5,1,5,17,10,2,12,3,1,0,11,3,7,4] }
assert my_solution.countVisitedNodes(**test_input) == [10,6,6,7,6,6,7,6,9,7,8,8,7,11,9,8,7,6]
test_input = { "edges": [4,2,4,2,3] }
assert my_solution.countVisitedNodes(**test_input) == [4,4,3,3,3] | 1,696,127,400 |