Datasets:

Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
ArXiv:
Tags:
code
Libraries:
Datasets
pandas
License:
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

LeetCode Contest Benchmark

A new benchmark for evaluating Code LLMs proposed by DeepSeek-Coder, which consists of the latest algorithm problems of different difficulties.

Usage

git clone https://github.com/deepseek-ai/DeepSeek-Coder.git
cd Evaluation/LeetCode

# Set the model or path here
MODEL="deepseek-ai/deepseek-coder-7b-instruct"

python vllm_inference.py --model_name_or_path $MODEL --saved_path output/20240121-Jul.deepseek-coder-7b-instruct.jsonl

python evaluate_leetcode.py --generation_path output/20240121-Jul.deepseek-coder-7b-instruct.jsonl --result_path output/20240121-Jul.deepseek-coder-7b-instruct.result.jsonl

Citation

@article{guo2024deepseekcoder,
  title   = {DeepSeek-Coder: When the Large Language Model Meets Programming - The Rise of Code Intelligence},
  author  = {Daya Guo and Qihao Zhu and Dejian Yang and Zhenda Xie and Kai Dong and Wentao Zhang and Guanting Chen and Xiao Bi and Y. Wu and Y. K. Li and Fuli Luo and Yingfei Xiong and Wenfeng Liang},
  year    = {2024},
  journal = {arXiv preprint arXiv: 2401.14196}
}
Downloads last month
39
Edit dataset card