task_id
stringlengths
33
83
url
stringlengths
44
94
title
stringlengths
14
64
meta
dict
prompt
stringlengths
399
2.26k
prompt_sft
stringlengths
424
2.29k
test
stringlengths
8.25k
44.9k
start_time
int64
1.69B
1.71B
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" }
""" 给你一个字符串 word,由 不同 小写英文字母组成。 电话键盘上的按键与 不同 小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 2 对应 ["a","b","c"],我们需要按一次键来输入 "a",按两次键来输入 "b",按三次键来输入 "c"。 现在允许你将编号为 2 到 9 的按键重新映射到 不同 字母集合。每个按键可以映射到 任意数量 的字母,但每个字母 必须 恰好 映射到 一个 按键上。你需要找到输入字符串 word 所需的 最少 按键次数。 返回重新映射按键后输入 word 所需的 最少 按键次数。 下面给出了一种电话键盘上字母到按键的映射作为示例。注意 1,*,# 和 0 不 对应任何字母。 示例 1: 输入:word = "abcde" 输出:5 解释:图片中给出的重新映射方案的输入成本最小。 "a" -> 在按键 2 上按一次 "b" -> 在按键 3 上按一次 "c" -> 在按键 4 上按一次 "d" -> 在按键 5 上按一次 "e" -> 在按键 6 上按一次 总成本为 1 + 1 + 1 + 1 + 1 = 5 。 可以证明不存在其他成本更低的映射方案。 示例 2: 输入:word = "xycdefghij" 输出:12 解释:图片中给出的重新映射方案的输入成本最小。 "x" -> 在按键 2 上按一次 "y" -> 在按键 2 上按两次 "c" -> 在按键 3 上按一次 "d" -> 在按键 3 上按两次 "e" -> 在按键 4 上按一次 "f" -> 在按键 5 上按一次 "g" -> 在按键 6 上按一次 "h" -> 在按键 7 上按一次 "i" -> 在按键 8 上按一次 "j" -> 在按键 9 上按一次 总成本为 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12 。 可以证明不存在其他成本更低的映射方案。 提示: 1 <= word.length <= 26 word 仅由小写英文字母组成。 word 中的所有字母互不相同。 """ class Solution: def minimumPushes(self, word: str) -> int:
给你一个字符串 word,由 不同 小写英文字母组成。 电话键盘上的按键与 不同 小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 2 对应 ["a","b","c"],我们需要按一次键来输入 "a",按两次键来输入 "b",按三次键来输入 "c"。 现在允许你将编号为 2 到 9 的按键重新映射到 不同 字母集合。每个按键可以映射到 任意数量 的字母,但每个字母 必须 恰好 映射到 一个 按键上。你需要找到输入字符串 word 所需的 最少 按键次数。 返回重新映射按键后输入 word 所需的 最少 按键次数。 下面给出了一种电话键盘上字母到按键的映射作为示例。注意 1,*,# 和 0 不 对应任何字母。 示例 1: 输入:word = "abcde" 输出:5 解释:图片中给出的重新映射方案的输入成本最小。 "a" -> 在按键 2 上按一次 "b" -> 在按键 3 上按一次 "c" -> 在按键 4 上按一次 "d" -> 在按键 5 上按一次 "e" -> 在按键 6 上按一次 总成本为 1 + 1 + 1 + 1 + 1 = 5 。 可以证明不存在其他成本更低的映射方案。 示例 2: 输入:word = "xycdefghij" 输出:12 解释:图片中给出的重新映射方案的输入成本最小。 "x" -> 在按键 2 上按一次 "y" -> 在按键 2 上按两次 "c" -> 在按键 3 上按一次 "d" -> 在按键 3 上按两次 "e" -> 在按键 4 上按一次 "f" -> 在按键 5 上按一次 "g" -> 在按键 6 上按一次 "h" -> 在按键 7 上按一次 "i" -> 在按键 8 上按一次 "j" -> 在按键 9 上按一次 总成本为 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12 。 可以证明不存在其他成本更低的映射方案。 提示: 1 <= word.length <= 26 word 仅由小写英文字母组成。 word 中的所有字母互不相同。 请完成下面的代码来解决上述问题: ```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" }
""" 给你三个 正整数 n 、x 和 y 。 在城市中,存在编号从 1 到 n 的房屋,由 n 条街道相连。对所有 1 <= i < n ,都存在一条街道连接编号为 i 的房屋与编号为 i + 1 的房屋。另存在一条街道连接编号为 x 的房屋与编号为 y 的房屋。 对于每个 k(1 <= k <= n),你需要找出所有满足要求的 房屋对 [house1, house2] ,即从 house1 到 house2 需要经过的 最少 街道数为 k 。 返回一个下标从 1 开始且长度为 n 的数组 result ,其中 result[k] 表示所有满足要求的房屋对的数量,即从一个房屋到另一个房屋需要经过的 最少 街道数为 k 。 注意,x 与 y 可以 相等 。 示例 1: 输入:n = 3, x = 1, y = 3 输出:[6,0,0] 解释:让我们检视每个房屋对 - 对于房屋对 (1, 2),可以直接从房屋 1 到房屋 2。 - 对于房屋对 (2, 1),可以直接从房屋 2 到房屋 1。 - 对于房屋对 (1, 3),可以直接从房屋 1 到房屋 3。 - 对于房屋对 (3, 1),可以直接从房屋 3 到房屋 1。 - 对于房屋对 (2, 3),可以直接从房屋 2 到房屋 3。 - 对于房屋对 (3, 2),可以直接从房屋 3 到房屋 2。 示例 2: 输入:n = 5, x = 2, y = 4 输出:[10,8,2,0,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), 以及 (5, 4)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), 以及 (5, 3)。 - 对于 k == 3,满足要求的房屋对有 (1, 5),以及 (5, 1) 。 - 对于 k == 4 和 k == 5,不存在满足要求的房屋对。 示例 3: 输入:n = 4, x = 1, y = 1 输出:[6,4,2,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), 以及 (4, 3)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (2, 4), 以及 (4, 2)。 - 对于 k == 3,满足要求的房屋对有 (1, 4), 以及 (4, 1)。 - 对于 k == 4,不存在满足要求的房屋对。 提示: 2 <= n <= 100 1 <= x, y <= n """ class Solution: def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
给你三个 正整数 n 、x 和 y 。 在城市中,存在编号从 1 到 n 的房屋,由 n 条街道相连。对所有 1 <= i < n ,都存在一条街道连接编号为 i 的房屋与编号为 i + 1 的房屋。另存在一条街道连接编号为 x 的房屋与编号为 y 的房屋。 对于每个 k(1 <= k <= n),你需要找出所有满足要求的 房屋对 [house1, house2] ,即从 house1 到 house2 需要经过的 最少 街道数为 k 。 返回一个下标从 1 开始且长度为 n 的数组 result ,其中 result[k] 表示所有满足要求的房屋对的数量,即从一个房屋到另一个房屋需要经过的 最少 街道数为 k 。 注意,x 与 y 可以 相等 。 示例 1: 输入:n = 3, x = 1, y = 3 输出:[6,0,0] 解释:让我们检视每个房屋对 - 对于房屋对 (1, 2),可以直接从房屋 1 到房屋 2。 - 对于房屋对 (2, 1),可以直接从房屋 2 到房屋 1。 - 对于房屋对 (1, 3),可以直接从房屋 1 到房屋 3。 - 对于房屋对 (3, 1),可以直接从房屋 3 到房屋 1。 - 对于房屋对 (2, 3),可以直接从房屋 2 到房屋 3。 - 对于房屋对 (3, 2),可以直接从房屋 3 到房屋 2。 示例 2: 输入:n = 5, x = 2, y = 4 输出:[10,8,2,0,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), 以及 (5, 4)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), 以及 (5, 3)。 - 对于 k == 3,满足要求的房屋对有 (1, 5),以及 (5, 1) 。 - 对于 k == 4 和 k == 5,不存在满足要求的房屋对。 示例 3: 输入:n = 4, x = 1, y = 1 输出:[6,4,2,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), 以及 (4, 3)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (2, 4), 以及 (4, 2)。 - 对于 k == 3,满足要求的房屋对有 (1, 4), 以及 (4, 1)。 - 对于 k == 4,不存在满足要求的房屋对。 提示: 2 <= n <= 100 1 <= x, y <= n 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个字符串 word,由 不同 小写英文字母组成。 电话键盘上的按键与 不同 小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 2 对应 ["a","b","c"],我们需要按一次键来输入 "a",按两次键来输入 "b",按三次键来输入 "c"。 现在允许你将编号为 2 到 9 的按键重新映射到 不同 字母集合。每个按键可以映射到 任意数量 的字母,但每个字母 必须 恰好 映射到 一个 按键上。你需要找到输入字符串 word 所需的 最少 按键次数。 返回重新映射按键后输入 word 所需的 最少 按键次数。 下面给出了一种电话键盘上字母到按键的映射作为示例。注意 1,*,# 和 0 不 对应任何字母。 示例 1: 输入:word = "abcde" 输出:5 解释:图片中给出的重新映射方案的输入成本最小。 "a" -> 在按键 2 上按一次 "b" -> 在按键 3 上按一次 "c" -> 在按键 4 上按一次 "d" -> 在按键 5 上按一次 "e" -> 在按键 6 上按一次 总成本为 1 + 1 + 1 + 1 + 1 = 5 。 可以证明不存在其他成本更低的映射方案。 示例 2: 输入:word = "xyzxyzxyzxyz" 输出:12 解释:图片中给出的重新映射方案的输入成本最小。 "x" -> 在按键 2 上按一次 "y" -> 在按键 3 上按一次 "z" -> 在按键 4 上按一次 总成本为 1 * 4 + 1 * 4 + 1 * 4 = 12 。 可以证明不存在其他成本更低的映射方案。 注意按键 9 没有映射到任何字母:不必让每个按键都存在与之映射的字母,但是每个字母都必须映射到按键上。 示例 3: 输入:word = "aabbccddeeffgghhiiiiii" 输出:24 解释:图片中给出的重新映射方案的输入成本最小。 "a" -> 在按键 2 上按一次 "b" -> 在按键 3 上按一次 "c" -> 在按键 4 上按一次 "d" -> 在按键 5 上按一次 "e" -> 在按键 6 上按一次 "f" -> 在按键 7 上按一次 "g" -> 在按键 8 上按一次 "h" -> 在按键 9 上按两次 "i" -> 在按键 9 上按一次 总成本为 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24 。 可以证明不存在其他成本更低的映射方案。 提示: 1 <= word.length <= 105 word 仅由小写英文字母组成。 """ class Solution: def minimumPushes(self, word: str) -> int:
给你一个字符串 word,由 不同 小写英文字母组成。 电话键盘上的按键与 不同 小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 2 对应 ["a","b","c"],我们需要按一次键来输入 "a",按两次键来输入 "b",按三次键来输入 "c"。 现在允许你将编号为 2 到 9 的按键重新映射到 不同 字母集合。每个按键可以映射到 任意数量 的字母,但每个字母 必须 恰好 映射到 一个 按键上。你需要找到输入字符串 word 所需的 最少 按键次数。 返回重新映射按键后输入 word 所需的 最少 按键次数。 下面给出了一种电话键盘上字母到按键的映射作为示例。注意 1,*,# 和 0 不 对应任何字母。 示例 1: 输入:word = "abcde" 输出:5 解释:图片中给出的重新映射方案的输入成本最小。 "a" -> 在按键 2 上按一次 "b" -> 在按键 3 上按一次 "c" -> 在按键 4 上按一次 "d" -> 在按键 5 上按一次 "e" -> 在按键 6 上按一次 总成本为 1 + 1 + 1 + 1 + 1 = 5 。 可以证明不存在其他成本更低的映射方案。 示例 2: 输入:word = "xyzxyzxyzxyz" 输出:12 解释:图片中给出的重新映射方案的输入成本最小。 "x" -> 在按键 2 上按一次 "y" -> 在按键 3 上按一次 "z" -> 在按键 4 上按一次 总成本为 1 * 4 + 1 * 4 + 1 * 4 = 12 。 可以证明不存在其他成本更低的映射方案。 注意按键 9 没有映射到任何字母:不必让每个按键都存在与之映射的字母,但是每个字母都必须映射到按键上。 示例 3: 输入:word = "aabbccddeeffgghhiiiiii" 输出:24 解释:图片中给出的重新映射方案的输入成本最小。 "a" -> 在按键 2 上按一次 "b" -> 在按键 3 上按一次 "c" -> 在按键 4 上按一次 "d" -> 在按键 5 上按一次 "e" -> 在按键 6 上按一次 "f" -> 在按键 7 上按一次 "g" -> 在按键 8 上按一次 "h" -> 在按键 9 上按两次 "i" -> 在按键 9 上按一次 总成本为 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24 。 可以证明不存在其他成本更低的映射方案。 提示: 1 <= word.length <= 105 word 仅由小写英文字母组成。 请完成下面的代码来解决上述问题: ```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" }
""" 给你三个 正整数 n 、x 和 y 。 在城市中,存在编号从 1 到 n 的房屋,由 n 条街道相连。对所有 1 <= i < n ,都存在一条街道连接编号为 i 的房屋与编号为 i + 1 的房屋。另存在一条街道连接编号为 x 的房屋与编号为 y 的房屋。 对于每个 k(1 <= k <= n),你需要找出所有满足要求的 房屋对 [house1, house2] ,即从 house1 到 house2 需要经过的 最少 街道数为 k 。 返回一个下标从 1 开始且长度为 n 的数组 result ,其中 result[k] 表示所有满足要求的房屋对的数量,即从一个房屋到另一个房屋需要经过的 最少 街道数为 k 。 注意,x 与 y 可以 相等 。 示例 1: 输入:n = 3, x = 1, y = 3 输出:[6,0,0] 解释:让我们检视每个房屋对 - 对于房屋对 (1, 2),可以直接从房屋 1 到房屋 2。 - 对于房屋对 (2, 1),可以直接从房屋 2 到房屋 1。 - 对于房屋对 (1, 3),可以直接从房屋 1 到房屋 3。 - 对于房屋对 (3, 1),可以直接从房屋 3 到房屋 1。 - 对于房屋对 (2, 3),可以直接从房屋 2 到房屋 3。 - 对于房屋对 (3, 2),可以直接从房屋 3 到房屋 2。 示例 2: 输入:n = 5, x = 2, y = 4 输出:[10,8,2,0,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), 以及 (5, 4)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), 以及 (5, 3)。 - 对于 k == 3,满足要求的房屋对有 (1, 5),以及 (5, 1) 。 - 对于 k == 4 和 k == 5,不存在满足要求的房屋对。 示例 3: 输入:n = 4, x = 1, y = 1 输出:[6,4,2,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), 以及 (4, 3)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (2, 4), 以及 (4, 2)。 - 对于 k == 3,满足要求的房屋对有 (1, 4), 以及 (4, 1)。 - 对于 k == 4,不存在满足要求的房屋对。 提示: 2 <= n <= 105 1 <= x, y <= n """ class Solution: def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
给你三个 正整数 n 、x 和 y 。 在城市中,存在编号从 1 到 n 的房屋,由 n 条街道相连。对所有 1 <= i < n ,都存在一条街道连接编号为 i 的房屋与编号为 i + 1 的房屋。另存在一条街道连接编号为 x 的房屋与编号为 y 的房屋。 对于每个 k(1 <= k <= n),你需要找出所有满足要求的 房屋对 [house1, house2] ,即从 house1 到 house2 需要经过的 最少 街道数为 k 。 返回一个下标从 1 开始且长度为 n 的数组 result ,其中 result[k] 表示所有满足要求的房屋对的数量,即从一个房屋到另一个房屋需要经过的 最少 街道数为 k 。 注意,x 与 y 可以 相等 。 示例 1: 输入:n = 3, x = 1, y = 3 输出:[6,0,0] 解释:让我们检视每个房屋对 - 对于房屋对 (1, 2),可以直接从房屋 1 到房屋 2。 - 对于房屋对 (2, 1),可以直接从房屋 2 到房屋 1。 - 对于房屋对 (1, 3),可以直接从房屋 1 到房屋 3。 - 对于房屋对 (3, 1),可以直接从房屋 3 到房屋 1。 - 对于房屋对 (2, 3),可以直接从房屋 2 到房屋 3。 - 对于房屋对 (3, 2),可以直接从房屋 3 到房屋 2。 示例 2: 输入:n = 5, x = 2, y = 4 输出:[10,8,2,0,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), 以及 (5, 4)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), 以及 (5, 3)。 - 对于 k == 3,满足要求的房屋对有 (1, 5),以及 (5, 1) 。 - 对于 k == 4 和 k == 5,不存在满足要求的房屋对。 示例 3: 输入:n = 4, x = 1, y = 1 输出:[6,4,2,0] 解释:对于每个距离 k ,满足要求的房屋对如下: - 对于 k == 1,满足要求的房屋对有 (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), 以及 (4, 3)。 - 对于 k == 2,满足要求的房屋对有 (1, 3), (3, 1), (2, 4), 以及 (4, 2)。 - 对于 k == 3,满足要求的房屋对有 (1, 4), 以及 (4, 1)。 - 对于 k == 4,不存在满足要求的房屋对。 提示: 2 <= n <= 105 1 <= x, y <= n 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个长度为 n的整数数组nums。 一个数组的 代价是它的 第一个元素。比方说,[1,2,3]的代价是1,[3,4,1]的代价是3。 你需要将nums分成3个连续且没有交集的子数组。 请你返回这些子数组的 最小代价总和。 示例 1: 输入:nums = [1,2,3,12] 输出:6 解释:最佳分割成 3 个子数组的方案是:[1] ,[2] 和 [3,12] ,总代价为 1 + 2 + 3 = 6 。 其他得到 3 个子数组的方案是: - [1] ,[2,3] 和 [12] ,总代价是 1 + 2 + 12 = 15 。 - [1,2] ,[3] 和 [12] ,总代价是 1 + 3 + 12 = 16 。 示例 2: 输入:nums = [5,4,3] 输出:12 解释:最佳分割成 3 个子数组的方案是:[5] ,[4] 和 [3] ,总代价为 5 + 4 + 3 = 12 。 12 是所有分割方案里的最小总代价。 示例 3: 输入:nums = [10,3,1,1] 输出:12 解释:最佳分割成 3 个子数组的方案是:[10,3] ,[1] 和 [1] ,总代价为 10 + 1 + 1 = 12 。 12 是所有分割方案里的最小总代价。 提示: 3 <= n <= 50 1 <= nums[i] <= 50 """ class Solution: def minimumCost(self, nums: List[int]) -> int:
给你一个长度为 n的整数数组nums。 一个数组的 代价是它的 第一个元素。比方说,[1,2,3]的代价是1,[3,4,1]的代价是3。 你需要将nums分成3个连续且没有交集的子数组。 请你返回这些子数组的 最小代价总和。 示例 1: 输入:nums = [1,2,3,12] 输出:6 解释:最佳分割成 3 个子数组的方案是:[1] ,[2] 和 [3,12] ,总代价为 1 + 2 + 3 = 6 。 其他得到 3 个子数组的方案是: - [1] ,[2,3] 和 [12] ,总代价是 1 + 2 + 12 = 15 。 - [1,2] ,[3] 和 [12] ,总代价是 1 + 3 + 12 = 16 。 示例 2: 输入:nums = [5,4,3] 输出:12 解释:最佳分割成 3 个子数组的方案是:[5] ,[4] 和 [3] ,总代价为 5 + 4 + 3 = 12 。 12 是所有分割方案里的最小总代价。 示例 3: 输入:nums = [10,3,1,1] 输出:12 解释:最佳分割成 3 个子数组的方案是:[10,3] ,[1] 和 [1] ,总代价为 10 + 1 + 1 = 12 。 12 是所有分割方案里的最小总代价。 提示: 3 <= n <= 50 1 <= nums[i] <= 50 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个下标从 0开始且全是 正整数的数组nums。 一次 操作中,如果两个 相邻元素在二进制下数位为 1的数目 相同,那么你可以将这两个元素交换。你可以执行这个操作 任意次(也可以 0 次)。 如果你可以使数组变有序,请你返回true ,否则返回false。 示例 1: 输入:nums = [8,4,2,30,15] 输出:true 解释:我们先观察每个元素的二进制表示。 2 ,4 和 8 分别都只有一个数位为 1 ,分别为 "10" ,"100" 和 "1000" 。15 和 30 分别有 4 个数位为 1 :"1111" 和 "11110" 。 我们可以通过 4 个操作使数组有序: - 交换 nums[0] 和 nums[1] 。8 和 4 分别只有 1 个数位为 1 。数组变为 [4,8,2,30,15] 。 - 交换 nums[1] 和 nums[2] 。8 和 2 分别只有 1 个数位为 1 。数组变为 [4,2,8,30,15] 。 - 交换 nums[0] 和 nums[1] 。4 和 2 分别只有 1 个数位为 1 。数组变为 [2,4,8,30,15] 。 - 交换 nums[3] 和 nums[4] 。30 和 15 分别有 4 个数位为 1 ,数组变为 [2,4,8,15,30] 。 数组变成有序的,所以我们返回 true 。 注意我们还可以通过其他的操作序列使数组变得有序。 示例 2: 输入:nums = [1,2,3,4,5] 输出:true 解释:数组已经是有序的,所以我们返回 true 。 示例 3: 输入:nums = [3,16,8,4,2] 输出:false 解释:无法通过操作使数组变为有序。 提示: 1 <= nums.length <= 100 1 <= nums[i] <= 28 """ class Solution: def canSortArray(self, nums: List[int]) -> bool:
给你一个下标从 0开始且全是 正整数的数组nums。 一次 操作中,如果两个 相邻元素在二进制下数位为 1的数目 相同,那么你可以将这两个元素交换。你可以执行这个操作 任意次(也可以 0 次)。 如果你可以使数组变有序,请你返回true ,否则返回false。 示例 1: 输入:nums = [8,4,2,30,15] 输出:true 解释:我们先观察每个元素的二进制表示。 2 ,4 和 8 分别都只有一个数位为 1 ,分别为 "10" ,"100" 和 "1000" 。15 和 30 分别有 4 个数位为 1 :"1111" 和 "11110" 。 我们可以通过 4 个操作使数组有序: - 交换 nums[0] 和 nums[1] 。8 和 4 分别只有 1 个数位为 1 。数组变为 [4,8,2,30,15] 。 - 交换 nums[1] 和 nums[2] 。8 和 2 分别只有 1 个数位为 1 。数组变为 [4,2,8,30,15] 。 - 交换 nums[0] 和 nums[1] 。4 和 2 分别只有 1 个数位为 1 。数组变为 [2,4,8,30,15] 。 - 交换 nums[3] 和 nums[4] 。30 和 15 分别有 4 个数位为 1 ,数组变为 [2,4,8,15,30] 。 数组变成有序的,所以我们返回 true 。 注意我们还可以通过其他的操作序列使数组变得有序。 示例 2: 输入:nums = [1,2,3,4,5] 输出:true 解释:数组已经是有序的,所以我们返回 true 。 示例 3: 输入:nums = [3,16,8,4,2] 输出:false 解释:无法通过操作使数组变为有序。 提示: 1 <= nums.length <= 100 1 <= nums[i] <= 28 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个下标从 0开始的整数数组nums,它只包含 正整数。 你的任务是通过进行以下操作任意次(可以是 0 次)最小化nums的长度: 在 nums中选择 两个不同的下标i和j,满足nums[i] > 0且nums[j] > 0。 将结果nums[i] % nums[j]插入nums的结尾。 将 nums中下标为i和j的元素删除。 请你返回一个整数,它表示进行任意次操作以后nums的 最小长度。 示例 1: 输入:nums = [1,4,3,1] 输出:1 解释:使数组长度最小的一种方法是: 操作 1 :选择下标 2 和 1 ,插入 nums[2] % nums[1] 到数组末尾,得到 [1,4,3,1,3] ,然后删除下标为 2 和 1 的元素。 nums 变为 [1,1,3] 。 操作 2 :选择下标 1 和 2 ,插入 nums[1] % nums[2] 到数组末尾,得到 [1,1,3,1] ,然后删除下标为 1 和 2 的元素。 nums 变为 [1,1] 。 操作 3 :选择下标 1 和 0 ,插入 nums[1] % nums[0] 到数组末尾,得到 [1,1,0] ,然后删除下标为 1 和 0 的元素。 nums 变为 [0] 。 nums 的长度无法进一步减小,所以答案为 1 。 1 是可以得到的最小长度。 示例 2: 输入:nums = [5,5,5,10,5] 输出:2 解释:使数组长度最小的一种方法是: 操作 1 :选择下标 0 和 3 ,插入 nums[0] % nums[3] 到数组末尾,得到 [5,5,5,10,5,5] ,然后删除下标为 0 和 3 的元素。 nums 变为 [5,5,5,5] 。 操作 2 :选择下标 2 和 3 ,插入 nums[2] % nums[3] 到数组末尾,得到 [5,5,5,5,0] ,然后删除下标为 2 和 3 的元素。 nums 变为 [5,5,0] 。 操作 3 :选择下标 0 和 1 ,插入 nums[0] % nums[1] 到数组末尾,得到 [5,5,0,0] ,然后删除下标为 0 和 1 的元素。 nums 变为 [0,0] 。 nums 的长度无法进一步减小,所以答案为 2 。 2 是可以得到的最小长度。 示例 3: 输入:nums = [2,3,4] 输出:1 解释:使数组长度最小的一种方法是: 操作 1 :选择下标 1 和 2 ,插入 nums[1] % nums[2] 到数组末尾,得到 [2,3,4,3] ,然后删除下标为 1 和 2 的元素。 nums 变为 [2,3] 。 操作 2 :选择下标 1 和 0 ,插入 nums[1] % nums[0] 到数组末尾,得到 [2,3,1] ,然后删除下标为 1 和 0 的元素。 nums 变为 [1] 。 nums 的长度无法进一步减小,所以答案为 1 。 1 是可以得到的最小长度。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 """ class Solution: def minimumArrayLength(self, nums: List[int]) -> int:
给你一个下标从 0开始的整数数组nums,它只包含 正整数。 你的任务是通过进行以下操作任意次(可以是 0 次)最小化nums的长度: 在 nums中选择 两个不同的下标i和j,满足nums[i] > 0且nums[j] > 0。 将结果nums[i] % nums[j]插入nums的结尾。 将 nums中下标为i和j的元素删除。 请你返回一个整数,它表示进行任意次操作以后nums的 最小长度。 示例 1: 输入:nums = [1,4,3,1] 输出:1 解释:使数组长度最小的一种方法是: 操作 1 :选择下标 2 和 1 ,插入 nums[2] % nums[1] 到数组末尾,得到 [1,4,3,1,3] ,然后删除下标为 2 和 1 的元素。 nums 变为 [1,1,3] 。 操作 2 :选择下标 1 和 2 ,插入 nums[1] % nums[2] 到数组末尾,得到 [1,1,3,1] ,然后删除下标为 1 和 2 的元素。 nums 变为 [1,1] 。 操作 3 :选择下标 1 和 0 ,插入 nums[1] % nums[0] 到数组末尾,得到 [1,1,0] ,然后删除下标为 1 和 0 的元素。 nums 变为 [0] 。 nums 的长度无法进一步减小,所以答案为 1 。 1 是可以得到的最小长度。 示例 2: 输入:nums = [5,5,5,10,5] 输出:2 解释:使数组长度最小的一种方法是: 操作 1 :选择下标 0 和 3 ,插入 nums[0] % nums[3] 到数组末尾,得到 [5,5,5,10,5,5] ,然后删除下标为 0 和 3 的元素。 nums 变为 [5,5,5,5] 。 操作 2 :选择下标 2 和 3 ,插入 nums[2] % nums[3] 到数组末尾,得到 [5,5,5,5,0] ,然后删除下标为 2 和 3 的元素。 nums 变为 [5,5,0] 。 操作 3 :选择下标 0 和 1 ,插入 nums[0] % nums[1] 到数组末尾,得到 [5,5,0,0] ,然后删除下标为 0 和 1 的元素。 nums 变为 [0,0] 。 nums 的长度无法进一步减小,所以答案为 2 。 2 是可以得到的最小长度。 示例 3: 输入:nums = [2,3,4] 输出:1 解释:使数组长度最小的一种方法是: 操作 1 :选择下标 1 和 2 ,插入 nums[1] % nums[2] 到数组末尾,得到 [2,3,4,3] ,然后删除下标为 1 和 2 的元素。 nums 变为 [2,3] 。 操作 2 :选择下标 1 和 0 ,插入 nums[1] % nums[0] 到数组末尾,得到 [2,3,1] ,然后删除下标为 1 和 0 的元素。 nums 变为 [1] 。 nums 的长度无法进一步减小,所以答案为 1 。 1 是可以得到的最小长度。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个下标从 0开始长度为 n的整数数组nums和两个 正整数k 和dist。 一个数组的 代价是数组中的 第一个元素。比方说,[1,2,3]的代价为1,[3,4,1]的代价为3。 你需要将 nums分割成 k个 连续且互不相交的子数组,满足 第二个子数组与第 k个子数组中第一个元素的下标距离 不超过dist。换句话说,如果你将nums分割成子数组nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)],那么它需要满足ik-1 - i1 <= dist。 请你返回这些子数组的 最小总代价。 示例 1: 输入:nums = [1,3,2,6,4,2], k = 3, dist = 3 输出:5 解释:将数组分割成 3 个子数组的最优方案是:[1,3] ,[2,6,4] 和 [2] 。这是一个合法分割,因为 ik-1 - i1 等于 5 - 2 = 3 ,等于 dist 。总代价为 nums[0] + nums[2] + nums[5] ,也就是 1 + 2 + 2 = 5 。 5 是分割成 3 个子数组的最小总代价。 示例 2: 输入:nums = [10,1,2,2,2,1], k = 4, dist = 3 输出:15 解释:将数组分割成 4 个子数组的最优方案是:[10] ,[1] ,[2] 和 [2,2,1] 。这是一个合法分割,因为 ik-1 - i1 等于 3 - 1 = 2 ,小于 dist 。总代价为 nums[0] + nums[1] + nums[2] + nums[3] ,也就是 10 + 1 + 2 + 2 = 15 。 分割 [10] ,[1] ,[2,2,2] 和 [1] 不是一个合法分割,因为 ik-1 和 i1 的差为 5 - 1 = 4 ,大于 dist 。 15 是分割成 4 个子数组的最小总代价。 示例 3: 输入:nums = [10,8,18,9], k = 3, dist = 1 输出:36 解释:将数组分割成 4 个子数组的最优方案是:[10] ,[8] 和 [18,9] 。这是一个合法分割,因为 ik-1 - i1 等于 2 - 1 = 1 ,等于 dist 。总代价为 nums[0] + nums[1] + nums[2] ,也就是 10 + 8 + 18 = 36 。 分割 [10] ,[8,18] 和 [9] 不是一个合法分割,因为 ik-1 和 i1 的差为 3 - 1 = 2 ,大于 dist 。 36 是分割成 3 个子数组的最小总代价。 提示: 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:
给你一个下标从 0开始长度为 n的整数数组nums和两个 正整数k 和dist。 一个数组的 代价是数组中的 第一个元素。比方说,[1,2,3]的代价为1,[3,4,1]的代价为3。 你需要将 nums分割成 k个 连续且互不相交的子数组,满足 第二个子数组与第 k个子数组中第一个元素的下标距离 不超过dist。换句话说,如果你将nums分割成子数组nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)],那么它需要满足ik-1 - i1 <= dist。 请你返回这些子数组的 最小总代价。 示例 1: 输入:nums = [1,3,2,6,4,2], k = 3, dist = 3 输出:5 解释:将数组分割成 3 个子数组的最优方案是:[1,3] ,[2,6,4] 和 [2] 。这是一个合法分割,因为 ik-1 - i1 等于 5 - 2 = 3 ,等于 dist 。总代价为 nums[0] + nums[2] + nums[5] ,也就是 1 + 2 + 2 = 5 。 5 是分割成 3 个子数组的最小总代价。 示例 2: 输入:nums = [10,1,2,2,2,1], k = 4, dist = 3 输出:15 解释:将数组分割成 4 个子数组的最优方案是:[10] ,[1] ,[2] 和 [2,2,1] 。这是一个合法分割,因为 ik-1 - i1 等于 3 - 1 = 2 ,小于 dist 。总代价为 nums[0] + nums[1] + nums[2] + nums[3] ,也就是 10 + 1 + 2 + 2 = 15 。 分割 [10] ,[1] ,[2,2,2] 和 [1] 不是一个合法分割,因为 ik-1 和 i1 的差为 5 - 1 = 4 ,大于 dist 。 15 是分割成 4 个子数组的最小总代价。 示例 3: 输入:nums = [10,8,18,9], k = 3, dist = 1 输出:36 解释:将数组分割成 4 个子数组的最优方案是:[10] ,[8] 和 [18,9] 。这是一个合法分割,因为 ik-1 - i1 等于 2 - 1 = 1 ,等于 dist 。总代价为 nums[0] + nums[1] + nums[2] ,也就是 10 + 8 + 18 = 36 。 分割 [10] ,[8,18] 和 [9] 不是一个合法分割,因为 ik-1 和 i1 的差为 3 - 1 = 2 ,大于 dist 。 36 是分割成 3 个子数组的最小总代价。 提示: 3 <= n <= 105 1 <= nums[i] <= 109 3 <= k <= n k - 2 <= dist <= n - 2 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个由 正整数 组成的数组 nums 。 返回数组 nums 中所有具有 最大 频率的元素的 总频率 。 元素的 频率 是指该元素在数组中出现的次数。 示例 1: 输入:nums = [1,2,2,3,1,4] 输出:4 解释:元素 1 和 2 的频率为 2 ,是数组中的最大频率。 因此具有最大频率的元素在数组中的数量是 4 。 示例 2: 输入:nums = [1,2,3,4,5] 输出:5 解释:数组中的所有元素的频率都为 1 ,是最大频率。 因此具有最大频率的元素在数组中的数量是 5 。 提示: 1 <= nums.length <= 100 1 <= nums[i] <= 100 """ class Solution: def maxFrequencyElements(self, nums: List[int]) -> int:
给你一个由 正整数 组成的数组 nums 。 返回数组 nums 中所有具有 最大 频率的元素的 总频率 。 元素的 频率 是指该元素在数组中出现的次数。 示例 1: 输入:nums = [1,2,2,3,1,4] 输出:4 解释:元素 1 和 2 的频率为 2 ,是数组中的最大频率。 因此具有最大频率的元素在数组中的数量是 4 。 示例 2: 输入:nums = [1,2,3,4,5] 输出:5 解释:数组中的所有元素的频率都为 1 ,是最大频率。 因此具有最大频率的元素在数组中的数量是 5 。 提示: 1 <= nums.length <= 100 1 <= nums[i] <= 100 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个下标从 0 开始的字符串 s 、字符串 a 、字符串 b 和一个整数 k 。 如果下标 i 满足以下条件,则认为它是一个 美丽下标: 0 <= i <= s.length - a.length s[i..(i + a.length - 1)] == a 存在下标 j 使得: 0 <= j <= s.length - b.length s[j..(j + b.length - 1)] == b |j - i| <= k 以数组形式按 从小到大排序 返回美丽下标。 示例 1: 输入:s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 输出:[16,33] 解释:存在 2 个美丽下标:[16,33]。 - 下标 16 是美丽下标,因为 s[16..17] == "my" ,且存在下标 4 ,满足 s[4..11] == "squirrel" 且 |16 - 4| <= 15 。 - 下标 33 是美丽下标,因为 s[33..34] == "my" ,且存在下标 18 ,满足 s[18..25] == "squirrel" 且 |33 - 18| <= 15 。 因此返回 [16,33] 作为结果。 示例 2: 输入:s = "abcd", a = "a", b = "a", k = 4 输出:[0] 解释:存在 1 个美丽下标:[0]。 - 下标 0 是美丽下标,因为 s[0..0] == "a" ,且存在下标 0 ,满足 s[0..0] == "a" 且 |0 - 0| <= 4 。 因此返回 [0] 作为结果。 提示: 1 <= k <= s.length <= 105 1 <= a.length, b.length <= 10 s、a、和 b 只包含小写英文字母。 """ class Solution: def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
给你一个下标从 0 开始的字符串 s 、字符串 a 、字符串 b 和一个整数 k 。 如果下标 i 满足以下条件,则认为它是一个 美丽下标: 0 <= i <= s.length - a.length s[i..(i + a.length - 1)] == a 存在下标 j 使得: 0 <= j <= s.length - b.length s[j..(j + b.length - 1)] == b |j - i| <= k 以数组形式按 从小到大排序 返回美丽下标。 示例 1: 输入:s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 输出:[16,33] 解释:存在 2 个美丽下标:[16,33]。 - 下标 16 是美丽下标,因为 s[16..17] == "my" ,且存在下标 4 ,满足 s[4..11] == "squirrel" 且 |16 - 4| <= 15 。 - 下标 33 是美丽下标,因为 s[33..34] == "my" ,且存在下标 18 ,满足 s[18..25] == "squirrel" 且 |33 - 18| <= 15 。 因此返回 [16,33] 作为结果。 示例 2: 输入:s = "abcd", a = "a", b = "a", k = 4 输出:[0] 解释:存在 1 个美丽下标:[0]。 - 下标 0 是美丽下标,因为 s[0..0] == "a" ,且存在下标 0 ,满足 s[0..0] == "a" 且 |0 - 0| <= 4 。 因此返回 [0] 作为结果。 提示: 1 <= k <= s.length <= 105 1 <= a.length, b.length <= 10 s、a、和 b 只包含小写英文字母。 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个整数k和一个整数x。 令 s为整数num的下标从 1开始的二进制表示。我们说一个整数num的 价值是满足i % x == 0 且s[i]是 设置位的 i的数目。 请你返回最大整数num,满足从 1到 num的所有整数的 价值和小于等于 k。 注意: 一个整数二进制表示下 设置位是值为 1的数位。 一个整数的二进制表示下标从右到左编号,比方说如果s == 11100,那么s[4] == 1 且s[2] == 0。 示例 1: 输入:k = 9, x = 1 输出:6 解释:数字 1 ,2 ,3 ,4 ,5 和 6 二进制表示分别为 "1" ,"10" ,"11" ,"100" ,"101" 和 "110" 。 由于 x 等于 1 ,每个数字的价值分别为所有设置位的数目。 这些数字的所有设置位数目总数是 9 ,所以前 6 个数字的价值和为 9 。 所以答案为 6 。 示例 2: 输入:k = 7, x = 2 输出:9 解释:由于 x 等于 2 ,我们检查每个数字的偶数位。 2 和 3 在二进制表示下的第二个数位为设置位,所以它们的价值和为 2 。 6 和 7 在二进制表示下的第二个数位为设置位,所以它们的价值和为 2 。 8 和 9 在二进制表示下的第四个数位为设置位但第二个数位不是设置位,所以它们的价值和为 2 。 数字 1 ,4 和 5 在二进制下偶数位都不是设置位,所以它们的价值和为 0 。 10 在二进制表示下的第二个数位和第四个数位都是设置位,所以它的价值为 2 。 前 9 个数字的价值和为 6 。 前 10 个数字的价值和为 8,超过了 k = 7 ,所以答案为 9 。 提示: 1 <= k <= 1015 1 <= x <= 8 """ class Solution: def findMaximumNumber(self, k: int, x: int) -> int:
给你一个整数k和一个整数x。 令 s为整数num的下标从 1开始的二进制表示。我们说一个整数num的 价值是满足i % x == 0 且s[i]是 设置位的 i的数目。 请你返回最大整数num,满足从 1到 num的所有整数的 价值和小于等于 k。 注意: 一个整数二进制表示下 设置位是值为 1的数位。 一个整数的二进制表示下标从右到左编号,比方说如果s == 11100,那么s[4] == 1 且s[2] == 0。 示例 1: 输入:k = 9, x = 1 输出:6 解释:数字 1 ,2 ,3 ,4 ,5 和 6 二进制表示分别为 "1" ,"10" ,"11" ,"100" ,"101" 和 "110" 。 由于 x 等于 1 ,每个数字的价值分别为所有设置位的数目。 这些数字的所有设置位数目总数是 9 ,所以前 6 个数字的价值和为 9 。 所以答案为 6 。 示例 2: 输入:k = 7, x = 2 输出:9 解释:由于 x 等于 2 ,我们检查每个数字的偶数位。 2 和 3 在二进制表示下的第二个数位为设置位,所以它们的价值和为 2 。 6 和 7 在二进制表示下的第二个数位为设置位,所以它们的价值和为 2 。 8 和 9 在二进制表示下的第四个数位为设置位但第二个数位不是设置位,所以它们的价值和为 2 。 数字 1 ,4 和 5 在二进制下偶数位都不是设置位,所以它们的价值和为 0 。 10 在二进制表示下的第二个数位和第四个数位都是设置位,所以它的价值为 2 。 前 9 个数字的价值和为 6 。 前 10 个数字的价值和为 8,超过了 k = 7 ,所以答案为 9 。 提示: 1 <= k <= 1015 1 <= x <= 8 请完成下面的代码来解决上述问题: ```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" }
""" 给你一个下标从 0开始的字符串s、字符串a、字符串b和一个整数k。 如果下标 i满足以下条件,则认为它是一个 美丽下标: 0 <= i <= s.length - a.length s[i..(i + a.length - 1)] == a 存在下标j使得: 0 <= j <= s.length - b.length s[j..(j + b.length - 1)] == b |j - i| <= k 以数组形式按从小到大排序返回美丽下标。 示例 1: 输入:s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 输出:[16,33] 解释:存在 2 个美丽下标:[16,33]。 - 下标 16 是美丽下标,因为 s[16..17] == "my" ,且存在下标 4 ,满足 s[4..11] == "squirrel" 且 |16 - 4| <= 15 。 - 下标 33 是美丽下标,因为 s[33..34] == "my" ,且存在下标 18 ,满足 s[18..25] == "squirrel" 且 |33 - 18| <= 15 。 因此返回 [16,33] 作为结果。 示例 2: 输入:s = "abcd", a = "a", b = "a", k = 4 输出:[0] 解释:存在 1 个美丽下标:[0]。 - 下标 0 是美丽下标,因为 s[0..0] == "a" ,且存在下标 0 ,满足 s[0..0] == "a" 且 |0 - 0| <= 4 。 因此返回 [0] 作为结果。 提示: 1 <= k <= s.length <= 5 * 105 1 <= a.length, b.length <= 5 * 105 s、a、和b只包含小写英文字母。 """ class Solution: def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
给你一个下标从 0开始的字符串s、字符串a、字符串b和一个整数k。 如果下标 i满足以下条件,则认为它是一个 美丽下标: 0 <= i <= s.length - a.length s[i..(i + a.length - 1)] == a 存在下标j使得: 0 <= j <= s.length - b.length s[j..(j + b.length - 1)] == b |j - i| <= k 以数组形式按从小到大排序返回美丽下标。 示例 1: 输入:s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15 输出:[16,33] 解释:存在 2 个美丽下标:[16,33]。 - 下标 16 是美丽下标,因为 s[16..17] == "my" ,且存在下标 4 ,满足 s[4..11] == "squirrel" 且 |16 - 4| <= 15 。 - 下标 33 是美丽下标,因为 s[33..34] == "my" ,且存在下标 18 ,满足 s[18..25] == "squirrel" 且 |33 - 18| <= 15 。 因此返回 [16,33] 作为结果。 示例 2: 输入:s = "abcd", a = "a", b = "a", k = 4 输出:[0] 解释:存在 1 个美丽下标:[0]。 - 下标 0 是美丽下标,因为 s[0..0] == "a" ,且存在下标 0 ,满足 s[0..0] == "a" 且 |0 - 0| <= 4 。 因此返回 [0] 作为结果。 提示: 1 <= k <= s.length <= 5 * 105 1 <= a.length, b.length <= 5 * 105 s、a、和b只包含小写英文字母。 请完成下面的代码来解决上述问题: ```python class Solution: def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]: ```
my_solution = Solution() test_input = { "s": "isawsquirrelnearmysquirrelhouseohmy", "a": "my", "b": "squirrel", "k": 15 } assert my_solution.beautifulIndices(**test_input) == [16,33] test_input = { "s": "abcd", "a": "a", "b": "a", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [0] test_input = { "s": "a", "a": "a", "b": "a", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [0] test_input = { "s": "aba", "a": "a", "b": "a", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [0,2] test_input = { "s": "nvnvt", "a": "eq", "b": "nv", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "npearbvede", "a": "myqpb", "b": "pearb", "k": 9 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "vatevavakz", "a": "va", "b": "lbda", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "ithhi", "a": "t", "b": "hhi", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [1] test_input = { "s": "osuv", "a": "osuv", "b": "wrn", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "dc", "a": "dreec", "b": "dc", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "jajrfw", "a": "rf", "b": "j", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [3] test_input = { "s": "zcvx", "a": "kfdvv", "b": "tru", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "wltmqbxt", "a": "mqbxt", "b": "lt", "k": 7 } assert my_solution.beautifulIndices(**test_input) == [3] test_input = { "s": "gggsytwgzg", "a": "sytwg", "b": "g", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [3] test_input = { "s": "diive", "a": "viw", "b": "lqqdn", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "ss", "a": "omkdt", "b": "s", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "hfzoxcm", "a": "hfzo", "b": "ipelr", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "xllimtmil", "a": "imt", "b": "iwqx", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "vdyl", "a": "i", "b": "ir", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "ouwpaz", "a": "mxre", "b": "pa", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "vlxgolxgoi", "a": "xf", "b": "lxgo", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "pnb", "a": "cx", "b": "pn", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "owhixi", "a": "hixi", "b": "anlc", "k": 6 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "lrtsi", "a": "lrts", "b": "i", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "tpyq", "a": "sa", "b": "py", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "imxclscgz", "a": "iujc", "b": "mxcls", "k": 7 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "sc", "a": "fc", "b": "th", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "tgs", "a": "ldy", "b": "tgs", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "ssaeqzzvvg", "a": "ssa", "b": "z", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [0] test_input = { "s": "zchdy", "a": "zch", "b": "dm", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "youob", "a": "y", "b": "o", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [0] test_input = { "s": "snwj", "a": "snwj", "b": "ry", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "rneq", "a": "ynprc", "b": "yts", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "vxqf", "a": "tcnzs", "b": "qf", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "avnzbrpb", "a": "yzfgy", "b": "cfri", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "de", "a": "segs", "b": "bvdhs", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "ulgzs", "a": "eiib", "b": "ulgz", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "tw", "a": "ypf", "b": "svl", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "sotqbvds", "a": "uoj", "b": "s", "k": 6 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "oexy", "a": "e", "b": "a", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "sywgismky", "a": "sywgi", "b": "t", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "onwawarwa", "a": "wa", "b": "r", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [4,7] test_input = { "s": "xpuldtpxpu", "a": "vkhl", "b": "xpu", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "dg", "a": "amhb", "b": "aqwcf", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "spro", "a": "spro", "b": "lytwu", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "gfjuakm", "a": "fj", "b": "jytnd", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "so", "a": "kkhvu", "b": "rukp", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "zwedfgnra", "a": "dfgn", "b": "zwe", "k": 9 } assert my_solution.beautifulIndices(**test_input) == [3] test_input = { "s": "nipetnupg", "a": "n", "b": "ipet", "k": 7 } assert my_solution.beautifulIndices(**test_input) == [0,5] test_input = { "s": "xckrhkrnfe", "a": "xc", "b": "kr", "k": 9 } assert my_solution.beautifulIndices(**test_input) == [0] test_input = { "s": "xibrjp", "a": "ibr", "b": "bpfuf", "k": 6 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "cj", "a": "x", "b": "dea", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "xfejay", "a": "xfej", "b": "koc", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "ijkqk", "a": "nzxwn", "b": "vqk", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "dqa", "a": "qj", "b": "norvy", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "vbgvuo", "a": "u", "b": "rewjx", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "uw", "a": "flap", "b": "lowqe", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "xoi", "a": "vefut", "b": "x", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "qzfsogwd", "a": "qzfs", "b": "txsdv", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "yalimlim", "a": "lim", "b": "bwi", "k": 8 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "mjvosrhrip", "a": "jzz", "b": "vo", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "dyswswiz", "a": "tib", "b": "dysws", "k": 7 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "ftyhkld", "a": "tyh", "b": "znru", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "adubkehe", "a": "kdtxl", "b": "dubke", "k": 7 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "svpbvld", "a": "d", "b": "svpbv", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "rmiwdb", "a": "rmiw", "b": "xgwcv", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "arikarikox", "a": "o", "b": "arik", "k": 8 } assert my_solution.beautifulIndices(**test_input) == [8] test_input = { "s": "cigzky", "a": "cigz", "b": "tu", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "hjlllm", "a": "l", "b": "h", "k": 6 } assert my_solution.beautifulIndices(**test_input) == [2,3,4] test_input = { "s": "xiyebjzdbv", "a": "iqku", "b": "yebjz", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "qrmogc", "a": "g", "b": "rm", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "lagopphhnl", "a": "gopph", "b": "hnl", "k": 8 } assert my_solution.beautifulIndices(**test_input) == [2] test_input = { "s": "xkggxk", "a": "xk", "b": "g", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [0,4] test_input = { "s": "cdvr", "a": "iemxd", "b": "dt", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "swuaumom", "a": "swuau", "b": "m", "k": 8 } assert my_solution.beautifulIndices(**test_input) == [0] test_input = { "s": "qftqft", "a": "o", "b": "qft", "k": 1 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "bc", "a": "ucfx", "b": "lzgx", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "egzttzmtot", "a": "boxwe", "b": "t", "k": 9 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "agbx", "a": "a", "b": "tw", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "fkm", "a": "gu", "b": "fkm", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "lt", "a": "z", "b": "lt", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "qkddvykd", "a": "kd", "b": "tprs", "k": 8 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "vndnqdehvr", "a": "dnqd", "b": "ybboz", "k": 8 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "lpnltfewly", "a": "few", "b": "l", "k": 6 } assert my_solution.beautifulIndices(**test_input) == [5] test_input = { "s": "lgioimioim", "a": "imsfs", "b": "ioim", "k": 10 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "jhucel", "a": "iox", "b": "nx", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "vdgtlvls", "a": "lvl", "b": "cu", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "buyryryrjq", "a": "yr", "b": "u", "k": 9 } assert my_solution.beautifulIndices(**test_input) == [2,4,6] test_input = { "s": "fwohvc", "a": "agagg", "b": "fwoh", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "tjityoib", "a": "vh", "b": "jityo", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "tssjrbpsck", "a": "ssjr", "b": "sc", "k": 10 } assert my_solution.beautifulIndices(**test_input) == [1] test_input = { "s": "aqxv", "a": "t", "b": "x", "k": 4 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "zw", "a": "l", "b": "c", "k": 2 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "cbccekck", "a": "c", "b": "k", "k": 6 } assert my_solution.beautifulIndices(**test_input) == [0,2,3,6] test_input = { "s": "angkytf", "a": "ngk", "b": "ytf", "k": 7 } assert my_solution.beautifulIndices(**test_input) == [1] test_input = { "s": "oloxjjjqj", "a": "olox", "b": "j", "k": 7 } assert my_solution.beautifulIndices(**test_input) == [0] test_input = { "s": "zbriomnn", "a": "omn", "b": "zbr", "k": 6 } assert my_solution.beautifulIndices(**test_input) == [4] test_input = { "s": "ydmlx", "a": "tu", "b": "dml", "k": 5 } assert my_solution.beautifulIndices(**test_input) == [] test_input = { "s": "lnoffflno", "a": "lno", "b": "f", "k": 3 } assert my_solution.beautifulIndices(**test_input) == [0,6] test_input = { "s": "hwayzb", "a": "fc", "b": "hway", "k": 2 } assert my_solution.beautifulIndices(**test_input) == []
1,705,199,400
weekly-contest-379-maximum-area-of-longest-diagonal-rectangle
https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle
maximum-area-of-longest-diagonal-rectangle
{ "questionId": "3251", "questionFrontendId": "10035", "title": "Maximum Area of Longest Diagonal Rectangle", "titleSlug": "maximum-area-of-longest-diagonal-rectangle", "isPaidOnly": false, "difficulty": "Easy", "likes": 38, "dislikes": 7, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的二维整数数组 dimensions。 对于所有下标 i(0 <= i < dimensions.length),dimensions[i][0] 表示矩形 i 的长度,而 dimensions[i][1] 表示矩形 i 的宽度。 返回对角线最 长 的矩形的 面积 。如果存在多个对角线长度相同的矩形,返回面积最 大 的矩形的面积。 示例 1: 输入:dimensions = [[9,3],[8,6]] 输出:48 解释: 下标 = 0,长度 = 9,宽度 = 3。对角线长度 = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487。 下标 = 1,长度 = 8,宽度 = 6。对角线长度 = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10。 因此,下标为 1 的矩形对角线更长,所以返回面积 = 8 * 6 = 48。 示例 2: 输入:dimensions = [[3,4],[4,3]] 输出:12 解释:两个矩形的对角线长度相同,为 5,所以最大面积 = 12。 提示: 1 <= dimensions.length <= 100 dimensions[i].length == 2 1 <= dimensions[i][0], dimensions[i][1] <= 100 """ class Solution: def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
给你一个下标从 0 开始的二维整数数组 dimensions。 对于所有下标 i(0 <= i < dimensions.length),dimensions[i][0] 表示矩形 i 的长度,而 dimensions[i][1] 表示矩形 i 的宽度。 返回对角线最 长 的矩形的 面积 。如果存在多个对角线长度相同的矩形,返回面积最 大 的矩形的面积。 示例 1: 输入:dimensions = [[9,3],[8,6]] 输出:48 解释: 下标 = 0,长度 = 9,宽度 = 3。对角线长度 = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487。 下标 = 1,长度 = 8,宽度 = 6。对角线长度 = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10。 因此,下标为 1 的矩形对角线更长,所以返回面积 = 8 * 6 = 48。 示例 2: 输入:dimensions = [[3,4],[4,3]] 输出:12 解释:两个矩形的对角线长度相同,为 5,所以最大面积 = 12。 提示: 1 <= dimensions.length <= 100 dimensions[i].length == 2 1 <= dimensions[i][0], dimensions[i][1] <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: ```
my_solution = Solution() test_input = { "dimensions": [[9,3],[8,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 48 test_input = { "dimensions": [[3,4],[4,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 12 test_input = { "dimensions": [[4,10],[4,9],[9,3],[10,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[2,6],[5,1],[3,10],[8,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 30 test_input = { "dimensions": [[3,7],[2,10],[3,4],[9,9],[5,10]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 81 test_input = { "dimensions": [[10,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 40 test_input = { "dimensions": [[9,9],[1,8],[10,5],[2,8],[6,3],[7,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 81 test_input = { "dimensions": [[10,3],[5,9],[8,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 30 test_input = { "dimensions": [[2,7],[3,2],[3,3],[10,4],[5,3],[8,10],[8,8],[4,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[1,10],[3,10],[4,4],[2,6],[6,3],[6,4],[9,1],[6,1],[2,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 30 test_input = { "dimensions": [[4,7],[10,10],[3,7],[9,1],[5,7],[3,9],[10,4],[4,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[1,1],[6,8],[6,9],[7,2],[6,8],[1,3],[3,1],[1,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 54 test_input = { "dimensions": [[6,6],[1,3],[8,10],[10,1],[3,10],[7,7],[10,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[6,5],[8,6],[2,10],[8,1],[9,2],[3,5],[3,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 20 test_input = { "dimensions": [[5,1],[4,9],[9,1],[5,8],[2,9],[3,2],[10,10],[5,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[8,3],[9,10],[7,7],[6,5],[6,9],[9,10],[5,10]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[6,10],[8,6],[10,1],[7,10],[10,10],[9,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[9,5],[9,2],[2,2],[8,9],[5,7],[8,10],[3,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[3,9],[9,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 45 test_input = { "dimensions": [[10,10],[5,5],[3,2],[2,6],[3,1],[10,7],[4,8],[7,9],[9,9],[1,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[2,3],[3,5],[2,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 15 test_input = { "dimensions": [[4,4],[7,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 49 test_input = { "dimensions": [[7,5],[9,6],[9,4],[5,7],[2,6],[10,3],[9,9],[9,4],[8,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 81 test_input = { "dimensions": [[5,1],[9,1],[7,1],[7,1],[3,1],[10,7],[9,1],[7,2],[4,6],[3,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 70 test_input = { "dimensions": [[8,4],[7,4],[1,5],[7,8],[5,6],[5,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 56 test_input = { "dimensions": [[5,10],[3,7],[8,6],[8,6],[5,9],[10,5],[7,8],[1,9],[2,5],[6,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 50 test_input = { "dimensions": [[9,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 36 test_input = { "dimensions": [[7,6],[2,8],[9,6],[1,10],[5,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 54 test_input = { "dimensions": [[4,2],[1,6],[2,1],[4,10],[10,1],[7,5],[8,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 40 test_input = { "dimensions": [[1,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 4 test_input = { "dimensions": [[9,4],[6,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 36 test_input = { "dimensions": [[7,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 35 test_input = { "dimensions": [[1,9],[9,7],[8,4],[6,6],[7,8],[4,6],[7,4],[9,9],[9,8],[8,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 81 test_input = { "dimensions": [[3,8],[6,3],[5,2],[3,7],[1,3],[9,8],[4,2],[3,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 72 test_input = { "dimensions": [[5,4],[2,4],[8,5],[8,4],[1,2],[6,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 40 test_input = { "dimensions": [[7,2],[4,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 14 test_input = { "dimensions": [[8,10],[5,2],[4,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[9,2],[5,6],[4,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 18 test_input = { "dimensions": [[3,8],[2,9],[7,7],[1,5],[1,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 49 test_input = { "dimensions": [[6,2],[8,2],[6,8],[7,6],[1,2],[6,8],[10,9],[2,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[3,8],[4,1],[5,2],[2,6],[4,9],[10,6],[6,10],[3,4],[6,6],[4,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[5,5],[3,8],[2,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 24 test_input = { "dimensions": [[8,1],[5,8],[3,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 40 test_input = { "dimensions": [[2,8],[8,1],[7,10],[5,7],[2,4],[3,10],[2,10],[7,10],[5,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 70 test_input = { "dimensions": [[3,10],[1,3],[10,5],[5,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 50 test_input = { "dimensions": [[10,6],[4,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[7,8],[8,6],[10,10],[6,7],[7,10]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[7,2],[7,3],[4,6],[4,4],[7,8],[2,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 56 test_input = { "dimensions": [[4,7],[3,1],[1,10],[4,2],[4,10],[8,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 64 test_input = { "dimensions": [[1,8],[4,3],[7,7],[10,6],[5,5],[1,3],[9,1],[8,3],[3,2],[5,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[6,7],[1,7],[5,10],[10,1],[8,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 50 test_input = { "dimensions": [[3,5],[2,7],[4,4],[4,9],[7,6],[2,4],[5,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 36 test_input = { "dimensions": [[8,8],[6,10],[6,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[10,2],[3,3],[5,9],[3,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 45 test_input = { "dimensions": [[4,3],[4,1],[8,9],[10,1],[2,7],[7,7],[9,3],[8,6],[1,5],[8,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 72 test_input = { "dimensions": [[6,8],[2,3],[4,9],[1,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 48 test_input = { "dimensions": [[1,6],[2,10],[1,5],[9,3],[9,1],[2,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 20 test_input = { "dimensions": [[6,5],[7,10],[1,2],[10,3],[4,2],[4,8],[5,10],[5,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 70 test_input = { "dimensions": [[1,2],[1,2],[2,4],[9,9],[3,8],[3,9],[2,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 81 test_input = { "dimensions": [[4,4],[6,1],[1,10],[10,7],[10,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 70 test_input = { "dimensions": [[3,2],[2,8],[10,9],[9,8],[2,2],[9,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[4,10],[9,6],[4,10],[6,7],[2,3],[7,9],[9,2],[1,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 63 test_input = { "dimensions": [[7,4],[10,2],[10,8],[4,9],[4,9],[10,3],[5,4],[4,5],[10,6],[3,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[2,5],[7,4],[5,3],[2,4],[3,10],[3,5],[4,5],[4,4],[6,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 30 test_input = { "dimensions": [[3,2],[7,10],[8,10],[7,4],[6,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[3,8],[4,5],[3,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 24 test_input = { "dimensions": [[6,8],[9,9],[1,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 81 test_input = { "dimensions": [[8,1],[7,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 35 test_input = { "dimensions": [[10,6],[5,1],[9,5],[5,7],[5,8],[6,5],[8,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[8,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 48 test_input = { "dimensions": [[5,2],[5,9],[9,5],[5,5],[8,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 45 test_input = { "dimensions": [[7,8],[9,9],[3,5],[8,1],[1,3],[8,2],[8,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 81 test_input = { "dimensions": [[3,10],[6,8],[4,5],[8,1],[7,2],[9,8],[3,7],[3,3],[9,10]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[1,1],[8,7],[4,6],[5,2],[5,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 56 test_input = { "dimensions": [[6,2],[8,4],[8,6],[2,10],[6,1],[9,8],[10,8],[10,10],[5,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[10,2],[9,7],[4,2],[8,6],[9,10],[10,7],[7,5],[5,10],[5,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[1,4],[7,2],[2,6],[7,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 49 test_input = { "dimensions": [[2,5],[10,10],[4,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[2,10],[10,4],[3,9],[6,10],[2,10],[10,1],[4,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[3,6],[5,4],[9,5],[6,2],[4,4],[7,2],[6,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 45 test_input = { "dimensions": [[1,1],[1,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 7 test_input = { "dimensions": [[1,2],[8,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 64 test_input = { "dimensions": [[3,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 21 test_input = { "dimensions": [[6,7],[1,5],[10,9],[10,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[7,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 56 test_input = { "dimensions": [[2,6],[10,3],[10,5],[1,9],[5,2],[9,10],[7,2],[7,7],[1,10]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[3,4],[8,2],[9,3],[2,9],[6,5],[10,5],[4,1],[8,7],[3,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 50 test_input = { "dimensions": [[7,6],[6,8],[5,7],[1,1],[4,5],[6,10],[9,3],[4,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[1,3],[2,4],[4,9],[10,9],[3,9],[7,5],[2,3],[10,7],[2,3],[1,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[1,8],[6,10],[4,8],[3,8],[4,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 60 test_input = { "dimensions": [[6,5],[3,10],[8,7],[10,10],[2,8],[5,8],[10,8],[9,10],[2,8],[8,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[1,6],[8,3],[6,1],[2,10],[2,5],[3,8]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 20 test_input = { "dimensions": [[7,2],[3,8],[10,10],[7,1],[6,8],[6,7],[10,6],[4,6],[5,7],[10,4]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 100 test_input = { "dimensions": [[9,6]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 54 test_input = { "dimensions": [[8,2],[7,6],[1,4],[1,6],[4,8],[10,9],[9,4],[1,5]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 90 test_input = { "dimensions": [[7,3],[2,5],[7,1],[10,7],[7,4],[8,1]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 70 test_input = { "dimensions": [[9,2]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 18 test_input = { "dimensions": [[9,2],[7,2],[2,7]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 18 test_input = { "dimensions": [[2,8],[10,6],[8,10],[9,9]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 80 test_input = { "dimensions": [[3,3]] } assert my_solution.areaOfMaxDiagonal(**test_input) == 9
1,704,594,600
weekly-contest-379-minimum-moves-to-capture-the-queen
https://leetcode.com/problems/minimum-moves-to-capture-the-queen
minimum-moves-to-capture-the-queen
{ "questionId": "3270", "questionFrontendId": "10036", "title": "Minimum Moves to Capture The Queen", "titleSlug": "minimum-moves-to-capture-the-queen", "isPaidOnly": false, "difficulty": "Medium", "likes": 71, "dislikes": 115, "categoryTitle": "Algorithms" }
""" 现有一个下标从 0 开始的 8 x 8 棋盘,上面有 3 枚棋子。 给你 6 个整数 a 、b 、c 、d 、e 和 f ,其中: (a, b) 表示白色车的位置。 (c, d) 表示白色象的位置。 (e, f) 表示黑皇后的位置。 假定你只能移动白色棋子,返回捕获黑皇后所需的最少移动次数。 请注意: 车可以向垂直或水平方向移动任意数量的格子,但不能跳过其他棋子。 象可以沿对角线方向移动任意数量的格子,但不能跳过其他棋子。 如果车或象能移向皇后所在的格子,则认为它们可以捕获皇后。 皇后不能移动。 示例 1: 输入:a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 输出:2 解释:将白色车先移动到 (1, 3) ,然后移动到 (2, 3) 来捕获黑皇后,共需移动 2 次。 由于起始时没有任何棋子正在攻击黑皇后,要想捕获黑皇后,移动次数不可能少于 2 次。 示例 2: 输入:a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 输出:1 解释:可以通过以下任一方式移动 1 次捕获黑皇后: - 将白色车移动到 (5, 2) 。 - 将白色象移动到 (5, 2) 。 提示: 1 <= a, b, c, d, e, f <= 8 两枚棋子不会同时出现在同一个格子上。 """ class Solution: def minMovesToCaptureTheQueen(self, a: int, b: int, c: int, d: int, e: int, f: int) -> int:
现有一个下标从 0 开始的 8 x 8 棋盘,上面有 3 枚棋子。 给你 6 个整数 a 、b 、c 、d 、e 和 f ,其中: (a, b) 表示白色车的位置。 (c, d) 表示白色象的位置。 (e, f) 表示黑皇后的位置。 假定你只能移动白色棋子,返回捕获黑皇后所需的最少移动次数。 请注意: 车可以向垂直或水平方向移动任意数量的格子,但不能跳过其他棋子。 象可以沿对角线方向移动任意数量的格子,但不能跳过其他棋子。 如果车或象能移向皇后所在的格子,则认为它们可以捕获皇后。 皇后不能移动。 示例 1: 输入:a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 输出:2 解释:将白色车先移动到 (1, 3) ,然后移动到 (2, 3) 来捕获黑皇后,共需移动 2 次。 由于起始时没有任何棋子正在攻击黑皇后,要想捕获黑皇后,移动次数不可能少于 2 次。 示例 2: 输入:a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 输出:1 解释:可以通过以下任一方式移动 1 次捕获黑皇后: - 将白色车移动到 (5, 2) 。 - 将白色象移动到 (5, 2) 。 提示: 1 <= a, b, c, d, e, f <= 8 两枚棋子不会同时出现在同一个格子上。 请完成下面的代码来解决上述问题: ```python class Solution: def minMovesToCaptureTheQueen(self, a: int, b: int, c: int, d: int, e: int, f: int) -> int: ```
my_solution = Solution() test_input = { "a": 1, "b": 1, "c": 8, "d": 8, "e": 2, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 3, "c": 3, "d": 4, "e": 5, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 4, "b": 3, "c": 3, "d": 4, "e": 5, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 1, "c": 1, "d": 4, "e": 1, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 3, "c": 3, "d": 4, "e": 2, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 8, "b": 4, "c": 7, "d": 5, "e": 5, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 8, "c": 4, "d": 3, "e": 2, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 6, "c": 3, "d": 3, "e": 5, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 5, "b": 3, "c": 6, "d": 6, "e": 6, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 1, "c": 6, "d": 6, "e": 2, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 4, "c": 8, "d": 8, "e": 7, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 5, "b": 1, "c": 6, "d": 5, "e": 3, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 2, "c": 8, "d": 6, "e": 7, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 1, "c": 8, "d": 2, "e": 4, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 3, "b": 1, "c": 1, "d": 1, "e": 2, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 5, "c": 2, "d": 4, "e": 5, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 8, "b": 1, "c": 5, "d": 8, "e": 1, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 6, "c": 6, "d": 2, "e": 1, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 5, "c": 7, "d": 6, "e": 2, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 6, "c": 3, "d": 2, "e": 6, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 3, "b": 3, "c": 8, "d": 1, "e": 2, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 5, "c": 6, "d": 1, "e": 4, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 7, "b": 2, "c": 2, "d": 8, "e": 7, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 3, "b": 8, "c": 2, "d": 7, "e": 1, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 3, "b": 5, "c": 5, "d": 3, "e": 1, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 2, "b": 2, "c": 7, "d": 4, "e": 3, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 3, "c": 1, "d": 5, "e": 5, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 7, "b": 5, "c": 2, "d": 5, "e": 6, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 7, "c": 8, "d": 1, "e": 3, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 6, "c": 6, "d": 4, "e": 1, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 8, "c": 4, "d": 4, "e": 3, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 4, "c": 7, "d": 7, "e": 4, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 5, "b": 7, "c": 4, "d": 2, "e": 3, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 6, "b": 1, "c": 7, "d": 8, "e": 8, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 3, "b": 7, "c": 7, "d": 6, "e": 7, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 3, "c": 6, "d": 4, "e": 6, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 7, "c": 1, "d": 1, "e": 5, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 6, "b": 8, "c": 5, "d": 8, "e": 5, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 2, "c": 3, "d": 7, "e": 6, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 3, "b": 2, "c": 6, "d": 1, "e": 6, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 5, "b": 4, "c": 6, "d": 7, "e": 1, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 6, "c": 2, "d": 3, "e": 3, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 7, "b": 6, "c": 2, "d": 1, "e": 2, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 5, "b": 8, "c": 6, "d": 5, "e": 6, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 6, "b": 2, "c": 6, "d": 7, "e": 5, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 5, "c": 4, "d": 4, "e": 7, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 6, "b": 1, "c": 4, "d": 4, "e": 2, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 3, "b": 4, "c": 1, "d": 8, "e": 3, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 7, "b": 7, "c": 1, "d": 6, "e": 3, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 6, "b": 8, "c": 2, "d": 2, "e": 3, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 5, "c": 3, "d": 7, "e": 5, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 7, "b": 3, "c": 5, "d": 3, "e": 8, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 4, "c": 1, "d": 4, "e": 7, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 6, "b": 3, "c": 7, "d": 3, "e": 4, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 4, "c": 6, "d": 7, "e": 4, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 4, "b": 5, "c": 6, "d": 8, "e": 2, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 6, "c": 5, "d": 7, "e": 8, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 1, "c": 8, "d": 2, "e": 1, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 2, "c": 8, "d": 3, "e": 2, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 1, "b": 7, "c": 5, "d": 5, "e": 4, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 3, "c": 7, "d": 8, "e": 1, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 7, "c": 6, "d": 8, "e": 6, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 3, "c": 4, "d": 7, "e": 7, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 4, "c": 6, "d": 6, "e": 2, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 2, "c": 6, "d": 4, "e": 1, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 3, "b": 5, "c": 3, "d": 2, "e": 1, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 3, "b": 6, "c": 1, "d": 3, "e": 3, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 1, "b": 1, "c": 7, "d": 6, "e": 4, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 1, "b": 8, "c": 7, "d": 2, "e": 6, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 5, "b": 8, "c": 4, "d": 5, "e": 7, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 3, "c": 8, "d": 8, "e": 5, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 7, "b": 2, "c": 8, "d": 1, "e": 4, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 3, "b": 5, "c": 8, "d": 7, "e": 6, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 5, "c": 2, "d": 2, "e": 1, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 1, "b": 8, "c": 8, "d": 4, "e": 4, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 3, "b": 6, "c": 7, "d": 6, "e": 8, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 6, "c": 3, "d": 7, "e": 3, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 1, "b": 5, "c": 3, "d": 4, "e": 7, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 6, "c": 2, "d": 2, "e": 5, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 7, "b": 3, "c": 2, "d": 6, "e": 3, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 4, "c": 6, "d": 5, "e": 5, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 1, "b": 1, "c": 2, "d": 4, "e": 5, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 4, "b": 5, "c": 5, "d": 4, "e": 2, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 6, "b": 5, "c": 6, "d": 2, "e": 2, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 5, "c": 4, "d": 2, "e": 8, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 1, "c": 2, "d": 5, "e": 8, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 4, "b": 8, "c": 3, "d": 8, "e": 8, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 8, "c": 4, "d": 7, "e": 5, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 5, "b": 5, "c": 3, "d": 6, "e": 8, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 2, "c": 3, "d": 1, "e": 2, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 8, "c": 6, "d": 3, "e": 2, "f": 8 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 6, "b": 1, "c": 8, "d": 8, "e": 7, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 8, "c": 6, "d": 1, "e": 4, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 6, "c": 4, "d": 2, "e": 8, "f": 5 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 1, "b": 4, "c": 2, "d": 5, "e": 5, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 4, "b": 4, "c": 3, "d": 2, "e": 6, "f": 7 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 8, "b": 3, "c": 6, "d": 7, "e": 4, "f": 6 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 2, "b": 5, "c": 1, "d": 1, "e": 8, "f": 4 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 7, "b": 8, "c": 1, "d": 4, "e": 5, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 5, "c": 2, "d": 1, "e": 1, "f": 1 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2
1,704,594,600
weekly-contest-379-maximum-size-of-a-set-after-removals
https://leetcode.com/problems/maximum-size-of-a-set-after-removals
maximum-size-of-a-set-after-removals
{ "questionId": "3228", "questionFrontendId": "10037", "title": "Maximum Size of a Set After Removals", "titleSlug": "maximum-size-of-a-set-after-removals", "isPaidOnly": false, "difficulty": "Medium", "likes": 116, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,它们的长度都是偶数 n 。 你必须从 nums1 中移除 n / 2 个元素,同时从 nums2 中也移除 n / 2 个元素。移除之后,你将 nums1 和 nums2 中剩下的元素插入到集合 s 中。 返回集合 s可能的 最多 包含多少元素。 示例 1: 输入:nums1 = [1,2,1,2], nums2 = [1,1,1,1] 输出:2 解释:从 nums1 和 nums2 中移除两个 1 。移除后,数组变为 nums1 = [2,2] 和 nums2 = [1,1] 。因此,s = {1,2} 。 可以证明,在移除之后,集合 s 最多可以包含 2 个元素。 示例 2: 输入:nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3] 输出:5 解释:从 nums1 中移除 2、3 和 6 ,同时从 nums2 中移除两个 3 和一个 2 。移除后,数组变为 nums1 = [1,4,5] 和 nums2 = [2,3,2] 。因此,s = {1,2,3,4,5} 。 可以证明,在移除之后,集合 s 最多可以包含 5 个元素。 示例 3: 输入:nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6] 输出:6 解释:从 nums1 中移除 1、2 和 3 ,同时从 nums2 中移除 4、5 和 6 。移除后,数组变为 nums1 = [1,2,3] 和 nums2 = [4,5,6] 。因此,s = {1,2,3,4,5,6} 。 可以证明,在移除之后,集合 s 最多可以包含 6 个元素。 提示: n == nums1.length == nums2.length 1 <= n <= 2 * 104 n是偶数。 1 <= nums1[i], nums2[i] <= 109 """ class Solution: def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,它们的长度都是偶数 n 。 你必须从 nums1 中移除 n / 2 个元素,同时从 nums2 中也移除 n / 2 个元素。移除之后,你将 nums1 和 nums2 中剩下的元素插入到集合 s 中。 返回集合 s可能的 最多 包含多少元素。 示例 1: 输入:nums1 = [1,2,1,2], nums2 = [1,1,1,1] 输出:2 解释:从 nums1 和 nums2 中移除两个 1 。移除后,数组变为 nums1 = [2,2] 和 nums2 = [1,1] 。因此,s = {1,2} 。 可以证明,在移除之后,集合 s 最多可以包含 2 个元素。 示例 2: 输入:nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3] 输出:5 解释:从 nums1 中移除 2、3 和 6 ,同时从 nums2 中移除两个 3 和一个 2 。移除后,数组变为 nums1 = [1,4,5] 和 nums2 = [2,3,2] 。因此,s = {1,2,3,4,5} 。 可以证明,在移除之后,集合 s 最多可以包含 5 个元素。 示例 3: 输入:nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6] 输出:6 解释:从 nums1 中移除 1、2 和 3 ,同时从 nums2 中移除 4、5 和 6 。移除后,数组变为 nums1 = [1,2,3] 和 nums2 = [4,5,6] 。因此,s = {1,2,3,4,5,6} 。 可以证明,在移除之后,集合 s 最多可以包含 6 个元素。 提示: n == nums1.length == nums2.length 1 <= n <= 2 * 104 n是偶数。 1 <= nums1[i], nums2[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums1": [1,2,1,2], "nums2": [1,1,1,1] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,2,3,4,5,6], "nums2": [2,3,2,3,2,3] } assert my_solution.maximumSetSize(**test_input) == 5 test_input = { "nums1": [1,1,2,2,3,3], "nums2": [4,4,5,5,6,6] } assert my_solution.maximumSetSize(**test_input) == 6 test_input = { "nums1": [1,2,1,1], "nums2": [1,2,3,4] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [1,1,1,1], "nums2": [12,23,41,9] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [12,23,41,9], "nums2": [1,1,1,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [9,8,4,7], "nums2": [5,5,9,5] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [8,9], "nums2": [4,3] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [7,1], "nums2": [6,10] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [10,3], "nums2": [5,6] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [3,6], "nums2": [6,6] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [5,1], "nums2": [6,6] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [10,7], "nums2": [8,4] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [10,8,7,9], "nums2": [7,9,9,5] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [1,10,6,5], "nums2": [3,7,10,10] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [5,2,8,6], "nums2": [7,4,1,1] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [2,4,1,4], "nums2": [10,2,4,10] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [5,7], "nums2": [3,1] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,10,1,2], "nums2": [9,5,8,5] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [9,4], "nums2": [5,7] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,5,10,8], "nums2": [1,7,4,10] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [6,6,2,9], "nums2": [1,4,10,7] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [7,10], "nums2": [6,10] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [8,8], "nums2": [6,3] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [6,8], "nums2": [9,3] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [3,8,1,9], "nums2": [2,5,4,5] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [6,1,4,7], "nums2": [10,7,2,2] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [8,7,9,3], "nums2": [10,3,8,2] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [1,4,5,9], "nums2": [2,5,2,7] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [3,5], "nums2": [5,3] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,10,8,2], "nums2": [2,9,10,7] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [3,9], "nums2": [1,4] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,5], "nums2": [10,5] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [7,5], "nums2": [2,10] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [6,10], "nums2": [3,1] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [9,8,1,3], "nums2": [4,9,8,6] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [4,1], "nums2": [9,9] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [10,7], "nums2": [10,8] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [4,4,3,9], "nums2": [6,8,4,7] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [4,10,4,9], "nums2": [5,7,4,2] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [8,6], "nums2": [1,7] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [9,8,10,7], "nums2": [3,7,7,6] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [4,10,9,10], "nums2": [9,7,3,6] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [6,7], "nums2": [5,7] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,1,10,5], "nums2": [6,6,8,5] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [5,3,4,2], "nums2": [10,3,7,10] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [1,3], "nums2": [9,2] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [6,1,6,2], "nums2": [5,4,6,7] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [9,9], "nums2": [8,7] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [6,2,9,3], "nums2": [10,3,4,7] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [6,2,10,1], "nums2": [9,2,6,5] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [6,5,6,1], "nums2": [6,2,6,9] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [10,5,4,7], "nums2": [5,4,4,9] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [7,10,4,6], "nums2": [1,4,4,2] } assert my_solution.maximumSetSize(**test_input) == 4 test_input = { "nums1": [3,4], "nums2": [1,8] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [7,7], "nums2": [7,1] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,1,2,2,1,1], "nums2": [1,3,2,2,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,3,3,2,1,2,1,2], "nums2": [1,2,3,2,2,3,3,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,3,2], "nums2": [2,2,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,1,1,3,3,3,1,1], "nums2": [3,2,3,2,3,3,3,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,3,1,1,3,2], "nums2": [2,2,1,1,2,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,3,2,3,3,1], "nums2": [2,3,1,2,1,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,1,3,3,1,3,3,3], "nums2": [2,3,1,3,1,1,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,3,2,2,1,3,1,1], "nums2": [3,1,2,2,3,1,2,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,2,2,3,1,1,3,3], "nums2": [3,1,3,1,2,3,2,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,3,1,3,2,2,3,1], "nums2": [3,1,2,3,1,3,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,1,1,2,1,1,1,1,1,2], "nums2": [3,1,3,3,1,1,3,2,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,2,2,3], "nums2": [1,1,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,3,3,1,2,1,2,2], "nums2": [1,2,2,1,2,1,2,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,2,1,3,2,3], "nums2": [3,3,1,1,3,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,1,3,3,3,3,2,2,2], "nums2": [2,1,3,2,2,3,3,1,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,1,1,2,2,1,1], "nums2": [2,3,3,1,3,2,3,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,3,2,2,2,2,1,2], "nums2": [3,1,2,3,3,1,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,3,2,1,2,3], "nums2": [2,1,1,3,2,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,1,2,1,1,2,3], "nums2": [1,2,2,3,2,3,3,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,1,1,3,1,2,1,1], "nums2": [2,3,3,2,3,3,1,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,1,3,3,2,2,2,3,1,1], "nums2": [1,1,2,1,3,3,1,3,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,1,1,2,3,2,1,1], "nums2": [2,1,2,2,2,1,3,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,1,3,2,2,2,2,2], "nums2": [1,3,3,2,2,2,3,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,1,1], "nums2": [1,3,1,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,3,1,3,2], "nums2": [2,1,3,2,2,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,2,3,3,2,2], "nums2": [1,3,1,3,1,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,1,3,2,3], "nums2": [2,1,1,1,2,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,1,3,1,3,1], "nums2": [3,3,1,2,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,2,2,2,2,3,1,3], "nums2": [2,3,3,2,1,2,3,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,3,1,2,3,1], "nums2": [3,3,2,2,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,1,2,2,1,3], "nums2": [1,1,1,3,2,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,2,1,2,1,3,3,1], "nums2": [3,1,3,1,1,3,3,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,1,3,3,2,3], "nums2": [1,3,3,2,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,2,1,3,3,2], "nums2": [1,1,3,3,1,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,1,3,2,3,3], "nums2": [2,2,2,3,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,3,3,1,2,2,3,1], "nums2": [3,1,2,3,2,3,2,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,1,3,1,2,1,2,2,2,2], "nums2": [2,1,3,1,1,1,2,2,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [1,2,2,1,3,3], "nums2": [1,1,1,3,2,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,1,2,3], "nums2": [3,2,3,2] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,1,2,2], "nums2": [2,1,1,1] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [2,2,3,2], "nums2": [2,1,3,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,3,2,1,3,3,2,3], "nums2": [3,1,2,3,2,2,1,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,3,3,1,2,2], "nums2": [1,3,1,2,2,3] } assert my_solution.maximumSetSize(**test_input) == 3 test_input = { "nums1": [3,1,1,3,1,1,3,2], "nums2": [3,2,3,3,3,1,2,3] } assert my_solution.maximumSetSize(**test_input) == 3
1,704,594,600
weekly-contest-379-maximize-the-number-of-partitions-after-operations
https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations
maximize-the-number-of-partitions-after-operations
{ "questionId": "3233", "questionFrontendId": "10038", "title": "Maximize the Number of Partitions After Operations", "titleSlug": "maximize-the-number-of-partitions-after-operations", "isPaidOnly": false, "difficulty": "Hard", "likes": 41, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的字符串s和一个整数k。 你需要执行以下分割操作,直到字符串s变为空: 选择s的最长前缀,该前缀最多包含k个不同字符。 删除这个前缀,并将分割数量加一。如果有剩余字符,它们在s中保持原来的顺序。 执行操作之 前 ,你可以将s中至多一处 下标的对应字符更改为另一个小写英文字母。 在最优选择情形下改变至多一处下标对应字符后,用整数表示并返回操作结束时得到的最大分割数量。 示例 1: 输入:s = "accca", k = 2 输出:3 解释:在此示例中,为了最大化得到的分割数量,可以将 s[2] 改为 'b'。 s 变为 "acbca"。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至多包含 2 个不同字符的前缀,"acbca"。 - 删除该前缀,s 变为 "bca"。现在分割数量为 1。 - 选择最长且至多包含 2 个不同字符的前缀,"bca"。 - 删除该前缀,s 变为 "a"。现在分割数量为 2。 - 选择最长且至多包含 2 个不同字符的前缀,"a"。 - 删除该前缀,s 变为空。现在分割数量为 3。 因此,答案是 3。 可以证明,分割数量不可能超过 3。 示例 2: 输入:s = "aabaab", k = 3 输出:1 解释:在此示例中,为了最大化得到的分割数量,可以保持 s 不变。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至多包含 3 个不同字符的前缀,"aabaab"。 - 删除该前缀,s 变为空。现在分割数量为 1。 因此,答案是 1。 可以证明,分割数量不可能超过 1。 示例 3: 输入:s = "xxyz", k = 1 输出:4 解释:在此示例中,为了最大化得到的分割数量,可以将 s[1] 改为 'a'。 s 变为 "xayz"。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至多包含 1 个不同字符的前缀,"xayz"。 - 删除该前缀,s 变为 "ayz"。现在分割数量为 1。 - 选择最长且至多包含 1 个不同字符的前缀,"ayz"。 - 删除该前缀,s 变为 "yz",现在分割数量为 2。 - 选择最长且至多包含 1 个不同字符的前缀,"yz"。 - 删除该前缀,s 变为 "z"。现在分割数量为 3。 - 选择最且至多包含 1 个不同字符的前缀,"z"。 - 删除该前缀,s 变为空。现在分割数量为 4。 因此,答案是 4。 可以证明,分割数量不可能超过 4。 提示: 1 <= s.length <= 104 s只包含小写英文字母。 1 <= k <= 26 """ class Solution: def maxPartitionsAfterOperations(self, s: str, k: int) -> int:
给你一个下标从 0 开始的字符串s和一个整数k。 你需要执行以下分割操作,直到字符串s变为空: 选择s的最长前缀,该前缀最多包含k个不同字符。 删除这个前缀,并将分割数量加一。如果有剩余字符,它们在s中保持原来的顺序。 执行操作之 前 ,你可以将s中至多一处 下标的对应字符更改为另一个小写英文字母。 在最优选择情形下改变至多一处下标对应字符后,用整数表示并返回操作结束时得到的最大分割数量。 示例 1: 输入:s = "accca", k = 2 输出:3 解释:在此示例中,为了最大化得到的分割数量,可以将 s[2] 改为 'b'。 s 变为 "acbca"。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至多包含 2 个不同字符的前缀,"acbca"。 - 删除该前缀,s 变为 "bca"。现在分割数量为 1。 - 选择最长且至多包含 2 个不同字符的前缀,"bca"。 - 删除该前缀,s 变为 "a"。现在分割数量为 2。 - 选择最长且至多包含 2 个不同字符的前缀,"a"。 - 删除该前缀,s 变为空。现在分割数量为 3。 因此,答案是 3。 可以证明,分割数量不可能超过 3。 示例 2: 输入:s = "aabaab", k = 3 输出:1 解释:在此示例中,为了最大化得到的分割数量,可以保持 s 不变。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至多包含 3 个不同字符的前缀,"aabaab"。 - 删除该前缀,s 变为空。现在分割数量为 1。 因此,答案是 1。 可以证明,分割数量不可能超过 1。 示例 3: 输入:s = "xxyz", k = 1 输出:4 解释:在此示例中,为了最大化得到的分割数量,可以将 s[1] 改为 'a'。 s 变为 "xayz"。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至多包含 1 个不同字符的前缀,"xayz"。 - 删除该前缀,s 变为 "ayz"。现在分割数量为 1。 - 选择最长且至多包含 1 个不同字符的前缀,"ayz"。 - 删除该前缀,s 变为 "yz",现在分割数量为 2。 - 选择最长且至多包含 1 个不同字符的前缀,"yz"。 - 删除该前缀,s 变为 "z"。现在分割数量为 3。 - 选择最且至多包含 1 个不同字符的前缀,"z"。 - 删除该前缀,s 变为空。现在分割数量为 4。 因此,答案是 4。 可以证明,分割数量不可能超过 4。 提示: 1 <= s.length <= 104 s只包含小写英文字母。 1 <= k <= 26 请完成下面的代码来解决上述问题: ```python class Solution: def maxPartitionsAfterOperations(self, s: str, k: int) -> int: ```
my_solution = Solution() test_input = { "s": "accca", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "aabaab", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "xxyz", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "c", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "c", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "h", "k": 17 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "p", "k": 13 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "ab", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "ba", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "ba", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "ca", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "fh", "k": 8 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "abb", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "aca", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "acb", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "acb", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bab", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "cba", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "cbb", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "cca", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "fjz", "k": 11 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "jxg", "k": 23 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "xfj", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "abcc", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "acab", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "altj", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "baac", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "baca", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "bbkk", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "cbcc", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "toqm", "k": 14 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "yhbo", "k": 9 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "aaabc", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 5 test_input = { "s": "accba", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "bcbab", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 5 test_input = { "s": "bccaa", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "caaaa", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "cacaa", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "cacac", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 5 test_input = { "s": "cbbab", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "ccacb", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "ccbba", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "yhqlp", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "aabaaa", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "acbabb", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 6 test_input = { "s": "bbaaab", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bbbcca", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bbcaab", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "cacbaa", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 6 test_input = { "s": "hnhdfs", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "nihnrq", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 6 test_input = { "s": "odxttm", "k": 19 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "uxqozq", "k": 11 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "aabcaac", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "aabcacc", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "abbaaca", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "baacaac", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "baacbaa", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "baacccb", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 6 test_input = { "s": "baccacb", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "baccccb", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "bcbbccb", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "cbbcaab", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "ccabbaa", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "cccabcc", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "tmdhzhy", "k": 26 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "abhujxlb", "k": 13 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "acabcbcb", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "acbcbcac", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "adklnsqm", "k": 13 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bababaca", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bcacbaaa", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "bcccbacc", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "aaaabcccb", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 6 test_input = { "s": "acabcaacc", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "bcbbabcaa", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bccaabcaa", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "cbcabbcca", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "ccbaabbba", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "aaaaabbcab", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "aababbbbca", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "aabbbbccab", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 7 test_input = { "s": "abbcbbcbba", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 8 test_input = { "s": "abcbbaccbb", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "acabacccac", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "acbbcacbab", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bbaccabbac", "k": 4 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "bbbbcbcbbc", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "bsoefqekpl", "k": 6 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "caacccbcac", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "cccacccbcb", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "vwfyagymtp", "k": 20 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "abaaccbaaaa", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 4 test_input = { "s": "abcccaccccc", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "cbbbccaacaa", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 2 test_input = { "s": "ccaaabaaaaa", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "wcmgarcruky", "k": 10 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "yyogqjsswyn", "k": 17 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "aaaccccbbbca", "k": 1 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 7 test_input = { "s": "baaccaabaccc", "k": 5 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1
1,704,594,600
biweekly-contest-121-smallest-missing-integer-greater-than-sequential-prefix-sum
https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum
smallest-missing-integer-greater-than-sequential-prefix-sum
{ "questionId": "3236", "questionFrontendId": "10031", "title": "Smallest Missing Integer Greater Than Sequential Prefix Sum", "titleSlug": "smallest-missing-integer-greater-than-sequential-prefix-sum", "isPaidOnly": false, "difficulty": "Easy", "likes": 41, "dislikes": 122, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的整数数组nums。 如果一个前缀nums[0..i]满足对于1 <= j <= i的所有元素都有nums[j] = nums[j - 1] + 1,那么我们称这个前缀是一个 顺序前缀 。特殊情况是,只包含nums[0]的前缀也是一个 顺序前缀 。 请你返回 nums中没有出现过的 最小整数x,满足x大于等于最长 顺序前缀的和。 示例 1: 输入:nums = [1,2,3,2,5] 输出:6 解释:nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。 示例 2: 输入:nums = [3,4,5,1,12,14,13] 输出:15 解释:nums 的最长顺序前缀是 [3,4,5] ,和为 12 ,12、13 和 14 都在数组中,但 15 不在,所以 15 是大于等于最长顺序前缀和的最小整数。 提示: 1 <= nums.length <= 50 1 <= nums[i] <= 50 """ class Solution: def missingInteger(self, nums: List[int]) -> int:
给你一个下标从 0开始的整数数组nums。 如果一个前缀nums[0..i]满足对于1 <= j <= i的所有元素都有nums[j] = nums[j - 1] + 1,那么我们称这个前缀是一个 顺序前缀 。特殊情况是,只包含nums[0]的前缀也是一个 顺序前缀 。 请你返回 nums中没有出现过的 最小整数x,满足x大于等于最长 顺序前缀的和。 示例 1: 输入:nums = [1,2,3,2,5] 输出:6 解释:nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。 示例 2: 输入:nums = [3,4,5,1,12,14,13] 输出:15 解释:nums 的最长顺序前缀是 [3,4,5] ,和为 12 ,12、13 和 14 都在数组中,但 15 不在,所以 15 是大于等于最长顺序前缀和的最小整数。 提示: 1 <= nums.length <= 50 1 <= nums[i] <= 50 请完成下面的代码来解决上述问题: ```python class Solution: def missingInteger(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,2,5] } assert my_solution.missingInteger(**test_input) == 6 test_input = { "nums": [3,4,5,1,12,14,13] } assert my_solution.missingInteger(**test_input) == 15 test_input = { "nums": [29,30,31,32,33,34,35,36,37] } assert my_solution.missingInteger(**test_input) == 297 test_input = { "nums": [19,20,21,22] } assert my_solution.missingInteger(**test_input) == 82 test_input = { "nums": [18,19,20,21,22,23,24,25,26,27,28,9] } assert my_solution.missingInteger(**test_input) == 253 test_input = { "nums": [4,5,6,7,8,8,9,4,3,2,7] } assert my_solution.missingInteger(**test_input) == 30 test_input = { "nums": [38] } assert my_solution.missingInteger(**test_input) == 39 test_input = { "nums": [1] } assert my_solution.missingInteger(**test_input) == 2 test_input = { "nums": [11,12,13] } assert my_solution.missingInteger(**test_input) == 36 test_input = { "nums": [47,48,49,5,3] } assert my_solution.missingInteger(**test_input) == 144 test_input = { "nums": [23,24,25,4,5,1] } assert my_solution.missingInteger(**test_input) == 72 test_input = { "nums": [8,9,10,10,7,8] } assert my_solution.missingInteger(**test_input) == 27 test_input = { "nums": [31,32,33,34,10,8,7,9,7,9,9,5,10,1] } assert my_solution.missingInteger(**test_input) == 130 test_input = { "nums": [17,18,19,20,21,22,3,7,10,10] } assert my_solution.missingInteger(**test_input) == 117 test_input = { "nums": [6,7,8,9,10,8,6,7,4,1] } assert my_solution.missingInteger(**test_input) == 40 test_input = { "nums": [46,8,2,4,1,4,10,2,4,10,2,5,7,3,1] } assert my_solution.missingInteger(**test_input) == 47 test_input = { "nums": [37,1,2,9,5,8,5,2,9,4] } assert my_solution.missingInteger(**test_input) == 38 test_input = { "nums": [31,32,33,34,1] } assert my_solution.missingInteger(**test_input) == 130 test_input = { "nums": [45,46,47,48,49,10,8,1,7,4,10,10,6,6,2] } assert my_solution.missingInteger(**test_input) == 235 test_input = { "nums": [13,10,7,5,7,10,6,10,2] } assert my_solution.missingInteger(**test_input) == 14 test_input = { "nums": [32,33,34,35,36,37,38,39,40,41,42,43,44,8,6] } assert my_solution.missingInteger(**test_input) == 494 test_input = { "nums": [24,8,9] } assert my_solution.missingInteger(**test_input) == 25 test_input = { "nums": [47,48,49,9,3,8,1,9,2,5,4,5,9] } assert my_solution.missingInteger(**test_input) == 144 test_input = { "nums": [4,5,6,7,8,9,4,7,10,7,2] } assert my_solution.missingInteger(**test_input) == 39 test_input = { "nums": [28,29] } assert my_solution.missingInteger(**test_input) == 57 test_input = { "nums": [40,41,42,3,8,2,7,1,4] } assert my_solution.missingInteger(**test_input) == 123 test_input = { "nums": [17,18,19,20,21,22,23,24,25,26,27,9,2,5] } assert my_solution.missingInteger(**test_input) == 242 test_input = { "nums": [43,44] } assert my_solution.missingInteger(**test_input) == 87 test_input = { "nums": [19,20,5,3,10] } assert my_solution.missingInteger(**test_input) == 39 test_input = { "nums": [5] } assert my_solution.missingInteger(**test_input) == 6 test_input = { "nums": [14,15] } assert my_solution.missingInteger(**test_input) == 29 test_input = { "nums": [47,48,49] } assert my_solution.missingInteger(**test_input) == 144 test_input = { "nums": [10] } assert my_solution.missingInteger(**test_input) == 11 test_input = { "nums": [39] } assert my_solution.missingInteger(**test_input) == 40 test_input = { "nums": [11,12,13,14,15,7,5,2,10,5,6] } assert my_solution.missingInteger(**test_input) == 65 test_input = { "nums": [3,4,5,7,9,8,1,3,4,9] } assert my_solution.missingInteger(**test_input) == 12 test_input = { "nums": [29,30,31,32,33,34,35,36,37,38,39,40,41] } assert my_solution.missingInteger(**test_input) == 455 test_input = { "nums": [24,25,26,27,28,29,30,31,32,33,34,35,6,4,1] } assert my_solution.missingInteger(**test_input) == 354 test_input = { "nums": [7,8,9,10,11,12,13,14,15] } assert my_solution.missingInteger(**test_input) == 99 test_input = { "nums": [39,40,41,42,43,44,45,8,10,4] } assert my_solution.missingInteger(**test_input) == 294 test_input = { "nums": [36,37,6,8] } assert my_solution.missingInteger(**test_input) == 73 test_input = { "nums": [27,28,29,30] } assert my_solution.missingInteger(**test_input) == 114 test_input = { "nums": [34,35,5,7] } assert my_solution.missingInteger(**test_input) == 69 test_input = { "nums": [9,8,6,1] } assert my_solution.missingInteger(**test_input) == 10 test_input = { "nums": [36,37,38,39,8,10,7] } assert my_solution.missingInteger(**test_input) == 150 test_input = { "nums": [28,29,6] } assert my_solution.missingInteger(**test_input) == 57 test_input = { "nums": [14,15,16,17,18,19,20,10,9,10,9,7,3,6] } assert my_solution.missingInteger(**test_input) == 119 test_input = { "nums": [27,28,29,5] } assert my_solution.missingInteger(**test_input) == 84 test_input = { "nums": [42,43,44,45,46,47,48] } assert my_solution.missingInteger(**test_input) == 315 test_input = { "nums": [2,3,4,5,6,7,8,9,10,11,1,10,5,6] } assert my_solution.missingInteger(**test_input) == 65 test_input = { "nums": [32,33,34,35,36,37,5,8,5,3,4,2,10,3,7] } assert my_solution.missingInteger(**test_input) == 207 test_input = { "nums": [24,25,26,27,28,29,30,31,32,33,1,3,9] } assert my_solution.missingInteger(**test_input) == 285 test_input = { "nums": [48,49] } assert my_solution.missingInteger(**test_input) == 97 test_input = { "nums": [46,47,6,7,1] } assert my_solution.missingInteger(**test_input) == 93 test_input = { "nums": [32,33,34,35,36,37,38,39,40] } assert my_solution.missingInteger(**test_input) == 324 test_input = { "nums": [40,41,42,43,44,45,6] } assert my_solution.missingInteger(**test_input) == 255 test_input = { "nums": [5,6,7,8,9,10,11,12,13,14,15,16,17,18,9] } assert my_solution.missingInteger(**test_input) == 161 test_input = { "nums": [39,40,41,3,4,7,10,6,2,10,1,9] } assert my_solution.missingInteger(**test_input) == 120 test_input = { "nums": [17,18] } assert my_solution.missingInteger(**test_input) == 35 test_input = { "nums": [41,42,43,44,45,46,5,6] } assert my_solution.missingInteger(**test_input) == 261 test_input = { "nums": [6] } assert my_solution.missingInteger(**test_input) == 7 test_input = { "nums": [46,47,48,49,50,7] } assert my_solution.missingInteger(**test_input) == 240 test_input = { "nums": [17,18,19,20,21,22,23,24,25,26,4,7,5,4,4] } assert my_solution.missingInteger(**test_input) == 215 test_input = { "nums": [40,41,42,43,44,45,46,4,6] } assert my_solution.missingInteger(**test_input) == 301 test_input = { "nums": [13,4,2,2,3,4,1,8,3,7,7,7,1,6,3] } assert my_solution.missingInteger(**test_input) == 14 test_input = { "nums": [4,5,6,7,8,9,10,11,12,13,14,15,16,6,8] } assert my_solution.missingInteger(**test_input) == 130 test_input = { "nums": [12,10] } assert my_solution.missingInteger(**test_input) == 13 test_input = { "nums": [17,18,19,20,21,5,3,7,10,5,3,7,3,5,3] } assert my_solution.missingInteger(**test_input) == 95 test_input = { "nums": [38,39,40,41,42,43,44,45,5,7,9,9,4,1] } assert my_solution.missingInteger(**test_input) == 332 test_input = { "nums": [32,33,34,35] } assert my_solution.missingInteger(**test_input) == 134 test_input = { "nums": [33,34,7,3,4,4] } assert my_solution.missingInteger(**test_input) == 67 test_input = { "nums": [33,34,35,36,37,38,39,40,41,42,43,44,45,46,47] } assert my_solution.missingInteger(**test_input) == 600 test_input = { "nums": [14,9,6,9,7,9,10,4,9,9,4,4] } assert my_solution.missingInteger(**test_input) == 15 test_input = { "nums": [18,19,20,21,22,23,24,25,26,6,8,2,1] } assert my_solution.missingInteger(**test_input) == 198 test_input = { "nums": [19,20,21,7,9] } assert my_solution.missingInteger(**test_input) == 60 test_input = { "nums": [19,20,21,10,1,8,2,1] } assert my_solution.missingInteger(**test_input) == 60 test_input = { "nums": [1,2,3,9,2,10,8,3,10,2] } assert my_solution.missingInteger(**test_input) == 6 test_input = { "nums": [48,10] } assert my_solution.missingInteger(**test_input) == 49 test_input = { "nums": [20,21,22,23,24,25,5] } assert my_solution.missingInteger(**test_input) == 135 test_input = { "nums": [40,41,42,43,3,4,10,3,7,8,9,1,5] } assert my_solution.missingInteger(**test_input) == 166 test_input = { "nums": [21,22,23,24,25,26,27,8] } assert my_solution.missingInteger(**test_input) == 168 test_input = { "nums": [2,3,4,5,6,4] } assert my_solution.missingInteger(**test_input) == 20 test_input = { "nums": [9,10,11,12,13,14,15,16,17,4] } assert my_solution.missingInteger(**test_input) == 117 test_input = { "nums": [25,26,27,28,29,6,8] } assert my_solution.missingInteger(**test_input) == 135 test_input = { "nums": [16,17,18,19,20,21,22,23,24,25,6] } assert my_solution.missingInteger(**test_input) == 205 test_input = { "nums": [7,8,9,10,11,12] } assert my_solution.missingInteger(**test_input) == 57 test_input = { "nums": [32,9,2,6,4,1,4,3,5] } assert my_solution.missingInteger(**test_input) == 33 test_input = { "nums": [1,4,3] } assert my_solution.missingInteger(**test_input) == 2 test_input = { "nums": [34,35,36,37,38,39,1,9,3,3,10,7,1] } assert my_solution.missingInteger(**test_input) == 219 test_input = { "nums": [37,7,6,4,3,1,10,8,7,2,6] } assert my_solution.missingInteger(**test_input) == 38 test_input = { "nums": [32] } assert my_solution.missingInteger(**test_input) == 33 test_input = { "nums": [25,26,27,4] } assert my_solution.missingInteger(**test_input) == 78 test_input = { "nums": [31,32,33,8,5,3,7,2] } assert my_solution.missingInteger(**test_input) == 96 test_input = { "nums": [38,39,40,41,42,43,44,45,1] } assert my_solution.missingInteger(**test_input) == 332 test_input = { "nums": [35,36,3,10] } assert my_solution.missingInteger(**test_input) == 71 test_input = { "nums": [31,32,33,34,35,7,6,1,9] } assert my_solution.missingInteger(**test_input) == 165 test_input = { "nums": [47,48,49,2,2] } assert my_solution.missingInteger(**test_input) == 144 test_input = { "nums": [3,4,5,6,7,8,9,10,11,10,1] } assert my_solution.missingInteger(**test_input) == 63 test_input = { "nums": [50] } assert my_solution.missingInteger(**test_input) == 51 test_input = { "nums": [14,15,16,17,7,10,3,10] } assert my_solution.missingInteger(**test_input) == 62
1,704,551,400
biweekly-contest-121-minimum-number-of-operations-to-make-array-xor-equal-to-k
https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k
minimum-number-of-operations-to-make-array-xor-equal-to-k
{ "questionId": "3249", "questionFrontendId": "10032", "title": "Minimum Number of Operations to Make Array XOR Equal to K", "titleSlug": "minimum-number-of-operations-to-make-array-xor-equal-to-k", "isPaidOnly": false, "difficulty": "Medium", "likes": 56, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的整数数组nums和一个正整数k。 你可以对数组执行以下操作 任意次: 选择数组里的 任意一个元素,并将它的二进制表示翻转一个数位,翻转数位表示将0 变成1或者将 1变成 0。 你的目标是让数组里 所有元素的按位异或和得到 k,请你返回达成这一目标的 最少操作次数。 注意,你也可以将一个数的前导 0 翻转。比方说,数字(101)2翻转第四个数位,得到(1101)2。 示例 1: 输入:nums = [2,1,3,4], k = 1 输出:2 解释:我们可以执行以下操作: - 选择下标为 2 的元素,也就是 3 == (011)2,我们翻转第一个数位得到 (010)2 == 2 。数组变为 [2,1,2,4] 。 - 选择下标为 0 的元素,也就是 2 == (010)2 ,我们翻转第三个数位得到 (110)2 == 6 。数组变为 [6,1,2,4] 。 最终数组的所有元素异或和为 (6 XOR 1 XOR 2 XOR 4) == 1 == k 。 无法用少于 2 次操作得到异或和等于 k 。 示例 2: 输入:nums = [2,0,2,0], k = 0 输出:0 解释:数组所有元素的异或和为 (2 XOR 0 XOR 2 XOR 0) == 0 == k 。所以不需要进行任何操作。 提示: 1 <= nums.length <= 105 0 <= nums[i] <= 106 0 <= k <= 106 """ class Solution: def minOperations(self, nums: List[int], k: int) -> int:
给你一个下标从 0开始的整数数组nums和一个正整数k。 你可以对数组执行以下操作 任意次: 选择数组里的 任意一个元素,并将它的二进制表示翻转一个数位,翻转数位表示将0 变成1或者将 1变成 0。 你的目标是让数组里 所有元素的按位异或和得到 k,请你返回达成这一目标的 最少操作次数。 注意,你也可以将一个数的前导 0 翻转。比方说,数字(101)2翻转第四个数位,得到(1101)2。 示例 1: 输入:nums = [2,1,3,4], k = 1 输出:2 解释:我们可以执行以下操作: - 选择下标为 2 的元素,也就是 3 == (011)2,我们翻转第一个数位得到 (010)2 == 2 。数组变为 [2,1,2,4] 。 - 选择下标为 0 的元素,也就是 2 == (010)2 ,我们翻转第三个数位得到 (110)2 == 6 。数组变为 [6,1,2,4] 。 最终数组的所有元素异或和为 (6 XOR 1 XOR 2 XOR 4) == 1 == k 。 无法用少于 2 次操作得到异或和等于 k 。 示例 2: 输入:nums = [2,0,2,0], k = 0 输出:0 解释:数组所有元素的异或和为 (2 XOR 0 XOR 2 XOR 0) == 0 == k 。所以不需要进行任何操作。 提示: 1 <= nums.length <= 105 0 <= nums[i] <= 106 0 <= k <= 106 请完成下面的代码来解决上述问题: ```python class Solution: def minOperations(self, nums: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "nums": [2,1,3,4], "k": 1 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [2,0,2,0], "k": 0 } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums": [4], "k": 7 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [3,13,9,8,5,18,11,10], "k": 13 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [9,7,9,14,8,6], "k": 12 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [13,9,10,16,11,8,1], "k": 17 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [12,14], "k": 1 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [18,18], "k": 20 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [3,5,1,1], "k": 19 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [7,0,0,0], "k": 8 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [13,15,19,18,2,9,18,11,0,7], "k": 6 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [9,15,19,15,10,15,14,0,2,5], "k": 20 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [19,4,19,6,3,19,14,4,16,12], "k": 4 } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums": [2,10,5,5,12,3,14,6,11,14], "k": 3 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [11,20], "k": 10 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [10,12,5,3,16,0], "k": 1 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [0,4,4,7,14,13], "k": 1 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [16,2,20,13,15,20,13], "k": 16 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [19,11,11,0,16,2,2,0,9], "k": 4 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [10,17,19,8,15], "k": 19 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [8,17,7,18], "k": 6 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [10,20], "k": 7 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [11,14,5,9,19,3,1], "k": 10 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [19,13,16], "k": 4 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [12,18,13,2,1,5,8,5,8,6], "k": 7 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [15], "k": 9 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [8,5,4,5,13,18], "k": 0 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [9,18], "k": 3 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [6,9,15,17,16], "k": 19 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [14,0,17], "k": 2 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [12,1,14,13], "k": 4 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [4,10,6,10,10,16], "k": 18 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [2,11,6,12,2,15,4,8,11], "k": 3 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [7,3,12,5,1,12,8], "k": 11 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [11,14,18,14,6,18,4,16,20,5], "k": 16 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [20,2,6,0,7], "k": 20 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [9,18,19,16,8,11,15], "k": 14 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [0,3,20,0,15,7,17,4], "k": 3 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [15,6], "k": 8 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [2,7,13,16,2,2], "k": 15 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [2,12,11,11,2,12], "k": 17 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [10,8,10], "k": 11 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [1,10,2,13], "k": 0 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [1,20,4,19,12,18,5,3,11,8], "k": 14 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [16,12,12], "k": 20 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [2,1,7,3,4,9], "k": 6 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [20,0,19,14,7,0], "k": 18 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [0,15,9,1,15], "k": 11 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [9,11,8,20,10], "k": 0 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [2,10,2,14,7,13,4,9,2], "k": 20 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [7,12,8], "k": 14 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [20,11,5,8,1,8,4,16], "k": 7 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [8,2,19,9,8], "k": 9 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [17], "k": 8 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [19,6], "k": 13 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [12,3,20,19], "k": 4 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [4,10,18,17,20,6,4], "k": 10 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [8,6,12,6,6], "k": 4 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [18,12,9,18,12,12,1], "k": 12 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [14,4,0,18,18,8,4,9], "k": 17 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [7,16,16,6], "k": 19 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [7,16,2,13,0,17,16], "k": 18 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [3,17,4,2,3,9], "k": 12 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [13,14,9,19,5,13], "k": 8 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [4,15,10,15,11,1,3,5,18,13], "k": 16 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [9,7,8], "k": 11 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [7,4,6,20,9,9,6,6], "k": 10 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [1,9,13,19,19,0,16,20,4], "k": 2 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [20,3,9,6,5,8], "k": 20 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [11,20,5,16,15,11,8], "k": 11 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [12,10,16,18,17,4,2,19,17,2], "k": 19 } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums": [15,2], "k": 10 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [13,3,10,2,9,13,5,11,5], "k": 20 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [20,12,9,3,2,11], "k": 13 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [3,19,0,18,6], "k": 15 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [12,6], "k": 18 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [10,11,12,6,10,1,15], "k": 8 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [12,8,1,16,6,12], "k": 2 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [11,5,9], "k": 2 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [2,7], "k": 6 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [20,1], "k": 8 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [9], "k": 16 } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums": [9,5,7,11,8,18,5,1,4], "k": 8 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [1,8,7,19,3,20,13,9,10], "k": 1 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [19,18,6], "k": 8 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [19,12,3,18,12,19,5,20], "k": 0 } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums": [6,18,12,9,20], "k": 13 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [19,5,5,7,4,7,15], "k": 7 } assert my_solution.minOperations(**test_input) == 5 test_input = { "nums": [17,7,19], "k": 18 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [14,13,3,15,18,20,2,9,3], "k": 14 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [16,10,3,2,3,19], "k": 4 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [2,0,8], "k": 18 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [19,5,5,12,20,2,10,17], "k": 12 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [6,0,0,1,15,9,19,12], "k": 6 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [8,16,13,8,18,9,16,16,19,11], "k": 12 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [3,7,6,7,4,3,2], "k": 2 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [0,2,9], "k": 10 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [14,10,16,9,6,13,11,13,11,16], "k": 20 } assert my_solution.minOperations(**test_input) == 5 test_input = { "nums": [13,19,0,12,11,10,11,2,6], "k": 7 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [6,1,4,9,1,7,11,15,15,0], "k": 8 } assert my_solution.minOperations(**test_input) == 4
1,704,551,400
biweekly-contest-121-minimum-number-of-operations-to-make-x-and-y-equal
https://leetcode.com/problems/minimum-number-of-operations-to-make-x-and-y-equal
minimum-number-of-operations-to-make-x-and-y-equal
{ "questionId": "3239", "questionFrontendId": "10033", "title": "Minimum Number of Operations to Make X and Y Equal", "titleSlug": "minimum-number-of-operations-to-make-x-and-y-equal", "isPaidOnly": false, "difficulty": "Medium", "likes": 103, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你两个正整数x 和y。 一次操作中,你可以执行以下四种操作之一: 如果 x是 11的倍数,将x除以11。 如果 x是 5的倍数,将 x除以 5。 将x 减1。 将x加1。 请你返回让 x和 y相等的 最少操作次数。 示例 1: 输入:x = 26, y = 1 输出:3 解释:我们可以通过以下操作将 26 变为 1 : 1. 将 x 减 1 2. 将 x 除以 5 3. 将 x 除以 5 将 26 变为 1 最少需要 3 次操作。 示例 2: 输入:x = 54, y = 2 输出:4 解释:我们可以通过以下操作将 54 变为 2 : 1. 将 x 加 1 2. 将 x 除以 11 3. 将 x 除以 5 4. 将 x 加 1 将 54 变为 2 最少需要 4 次操作。 示例 3: 输入:x = 25, y = 30 输出:5 解释:我们可以通过以下操作将 25 变为 30 : 1. 将 x 加 1 2. 将 x 加 1 3. 将 x 加 1 4. 将 x 加 1 5. 将 x 加 1 将 25 变为 30 最少需要 5 次操作。 提示: 1 <= x, y <= 104 """ class Solution: def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
给你两个正整数x 和y。 一次操作中,你可以执行以下四种操作之一: 如果 x是 11的倍数,将x除以11。 如果 x是 5的倍数,将 x除以 5。 将x 减1。 将x加1。 请你返回让 x和 y相等的 最少操作次数。 示例 1: 输入:x = 26, y = 1 输出:3 解释:我们可以通过以下操作将 26 变为 1 : 1. 将 x 减 1 2. 将 x 除以 5 3. 将 x 除以 5 将 26 变为 1 最少需要 3 次操作。 示例 2: 输入:x = 54, y = 2 输出:4 解释:我们可以通过以下操作将 54 变为 2 : 1. 将 x 加 1 2. 将 x 除以 11 3. 将 x 除以 5 4. 将 x 加 1 将 54 变为 2 最少需要 4 次操作。 示例 3: 输入:x = 25, y = 30 输出:5 解释:我们可以通过以下操作将 25 变为 30 : 1. 将 x 加 1 2. 将 x 加 1 3. 将 x 加 1 4. 将 x 加 1 5. 将 x 加 1 将 25 变为 30 最少需要 5 次操作。 提示: 1 <= x, y <= 104 请完成下面的代码来解决上述问题: ```python class Solution: def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: ```
my_solution = Solution() test_input = { "x": 26, "y": 1 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3 test_input = { "x": 54, "y": 2 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4 test_input = { "x": 25, "y": 30 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5 test_input = { "x": 1, "y": 1 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0 test_input = { "x": 1, "y": 2 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1 test_input = { "x": 1, "y": 3 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2 test_input = { "x": 1, "y": 4 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3 test_input = { "x": 1, "y": 5 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4 test_input = { "x": 1, "y": 6 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5 test_input = { "x": 1, "y": 7 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6 test_input = { "x": 1, "y": 8 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7 test_input = { "x": 1, "y": 9 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8 test_input = { "x": 1, "y": 10 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9 test_input = { "x": 1, "y": 11 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10 test_input = { "x": 1, "y": 12 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11 test_input = { "x": 1, "y": 13 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12 test_input = { "x": 1, "y": 14 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13 test_input = { "x": 1, "y": 15 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14 test_input = { "x": 1, "y": 16 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15 test_input = { "x": 1, "y": 17 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16 test_input = { "x": 1, "y": 18 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17 test_input = { "x": 1, "y": 19 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18 test_input = { "x": 1, "y": 20 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 19 test_input = { "x": 1, "y": 21 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 20 test_input = { "x": 1, "y": 22 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 21 test_input = { "x": 1, "y": 23 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 22 test_input = { "x": 1, "y": 24 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 23 test_input = { "x": 1, "y": 25 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 24 test_input = { "x": 2, "y": 1 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1 test_input = { "x": 2, "y": 2 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0 test_input = { "x": 2, "y": 3 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1 test_input = { "x": 2, "y": 4 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2 test_input = { "x": 2, "y": 5 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3 test_input = { "x": 2, "y": 6 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4 test_input = { "x": 2, "y": 7 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5 test_input = { "x": 2, "y": 8 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6 test_input = { "x": 2, "y": 9 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7 test_input = { "x": 2, "y": 10 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8 test_input = { "x": 2, "y": 11 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9 test_input = { "x": 2, "y": 12 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10 test_input = { "x": 2, "y": 13 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11 test_input = { "x": 2, "y": 14 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12 test_input = { "x": 2, "y": 15 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13 test_input = { "x": 2, "y": 16 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14 test_input = { "x": 2, "y": 17 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15 test_input = { "x": 2, "y": 18 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16 test_input = { "x": 2, "y": 19 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17 test_input = { "x": 2, "y": 20 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18 test_input = { "x": 2, "y": 21 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 19 test_input = { "x": 2, "y": 22 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 20 test_input = { "x": 2, "y": 23 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 21 test_input = { "x": 2, "y": 24 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 22 test_input = { "x": 2, "y": 25 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 23 test_input = { "x": 3, "y": 1 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2 test_input = { "x": 3, "y": 2 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1 test_input = { "x": 3, "y": 3 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0 test_input = { "x": 3, "y": 4 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1 test_input = { "x": 3, "y": 5 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2 test_input = { "x": 3, "y": 6 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3 test_input = { "x": 3, "y": 7 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4 test_input = { "x": 3, "y": 8 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5 test_input = { "x": 3, "y": 9 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6 test_input = { "x": 3, "y": 10 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7 test_input = { "x": 3, "y": 11 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8 test_input = { "x": 3, "y": 12 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9 test_input = { "x": 3, "y": 13 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10 test_input = { "x": 3, "y": 14 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11 test_input = { "x": 3, "y": 15 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12 test_input = { "x": 3, "y": 16 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13 test_input = { "x": 3, "y": 17 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14 test_input = { "x": 3, "y": 18 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15 test_input = { "x": 3, "y": 19 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16 test_input = { "x": 3, "y": 20 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17 test_input = { "x": 3, "y": 21 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18 test_input = { "x": 3, "y": 22 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 19 test_input = { "x": 3, "y": 23 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 20 test_input = { "x": 3, "y": 24 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 21 test_input = { "x": 3, "y": 25 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 22 test_input = { "x": 4, "y": 1 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2 test_input = { "x": 4, "y": 2 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2 test_input = { "x": 4, "y": 3 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1 test_input = { "x": 4, "y": 4 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 0 test_input = { "x": 4, "y": 5 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 1 test_input = { "x": 4, "y": 6 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 2 test_input = { "x": 4, "y": 7 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3 test_input = { "x": 4, "y": 8 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4 test_input = { "x": 4, "y": 9 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 5 test_input = { "x": 4, "y": 10 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 6 test_input = { "x": 4, "y": 11 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 7 test_input = { "x": 4, "y": 12 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 8 test_input = { "x": 4, "y": 13 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 9 test_input = { "x": 4, "y": 14 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 10 test_input = { "x": 4, "y": 15 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 11 test_input = { "x": 4, "y": 16 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 12 test_input = { "x": 4, "y": 17 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 13 test_input = { "x": 4, "y": 18 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 14 test_input = { "x": 4, "y": 19 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 15 test_input = { "x": 4, "y": 20 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 16 test_input = { "x": 4, "y": 21 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 17 test_input = { "x": 4, "y": 22 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 18
1,704,551,400
biweekly-contest-121-count-the-number-of-powerful-integers
https://leetcode.com/problems/count-the-number-of-powerful-integers
count-the-number-of-powerful-integers
{ "questionId": "3243", "questionFrontendId": "10034", "title": "Count the Number of Powerful Integers", "titleSlug": "count-the-number-of-powerful-integers", "isPaidOnly": false, "difficulty": "Hard", "likes": 53, "dislikes": 1, "categoryTitle": "Algorithms" }
""" 给你三个整数start,finish和limit。同时给你一个下标从0开始的字符串s,表示一个 正整数。 如果一个 正整数x 末尾部分是s(换句话说,s是 x的 后缀),且 x中的每个数位至多是 limit,那么我们称 x是 强大的。 请你返回区间[start..finish]内强大整数的总数目。 如果一个字符串 x是 y中某个下标开始(包括0),到下标为y.length - 1结束的子字符串,那么我们称x是y的一个后缀。比方说,25是5125的一个后缀,但不是512的后缀。 示例 1: 输入:start = 1, finish = 6000, limit = 4, s = "124" 输出:5 解释:区间 [1..6000] 内的强大数字为 124 ,1124 ,2124 ,3124 和 4124 。这些整数的各个数位都 <= 4 且 "124" 是它们的后缀。注意 5124 不是强大整数,因为第一个数位 5 大于 4 。 这个区间内总共只有这 5 个强大整数。 示例 2: 输入:start = 15, finish = 215, limit = 6, s = "10" 输出:2 解释:区间 [15..215] 内的强大整数为 110 和 210 。这些整数的各个数位都 <= 6 且 "10" 是它们的后缀。 这个区间总共只有这 2 个强大整数。 示例 3: 输入:start = 1000, finish = 2000, limit = 4, s = "3000" 输出:0 解释:区间 [1000..2000] 内的整数都小于 3000 ,所以 "3000" 不可能是这个区间内任何整数的后缀。 提示: 1 <= start <= finish <= 1015 1 <= limit <= 9 1 <= s.length <= floor(log10(finish)) + 1 s数位中每个数字都小于等于limit。 s不包含任何前导 0 。 """ class Solution: def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
给你三个整数start,finish和limit。同时给你一个下标从0开始的字符串s,表示一个 正整数。 如果一个 正整数x 末尾部分是s(换句话说,s是 x的 后缀),且 x中的每个数位至多是 limit,那么我们称 x是 强大的。 请你返回区间[start..finish]内强大整数的总数目。 如果一个字符串 x是 y中某个下标开始(包括0),到下标为y.length - 1结束的子字符串,那么我们称x是y的一个后缀。比方说,25是5125的一个后缀,但不是512的后缀。 示例 1: 输入:start = 1, finish = 6000, limit = 4, s = "124" 输出:5 解释:区间 [1..6000] 内的强大数字为 124 ,1124 ,2124 ,3124 和 4124 。这些整数的各个数位都 <= 4 且 "124" 是它们的后缀。注意 5124 不是强大整数,因为第一个数位 5 大于 4 。 这个区间内总共只有这 5 个强大整数。 示例 2: 输入:start = 15, finish = 215, limit = 6, s = "10" 输出:2 解释:区间 [15..215] 内的强大整数为 110 和 210 。这些整数的各个数位都 <= 6 且 "10" 是它们的后缀。 这个区间总共只有这 2 个强大整数。 示例 3: 输入:start = 1000, finish = 2000, limit = 4, s = "3000" 输出:0 解释:区间 [1000..2000] 内的整数都小于 3000 ,所以 "3000" 不可能是这个区间内任何整数的后缀。 提示: 1 <= start <= finish <= 1015 1 <= limit <= 9 1 <= s.length <= floor(log10(finish)) + 1 s数位中每个数字都小于等于limit。 s不包含任何前导 0 。 请完成下面的代码来解决上述问题: ```python class Solution: def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int: ```
my_solution = Solution() test_input = { "start": 1, "finish": 6000, "limit": 4, "s": "124" } assert my_solution.numberOfPowerfulInt(**test_input) == 5 test_input = { "start": 15, "finish": 215, "limit": 6, "s": "10" } assert my_solution.numberOfPowerfulInt(**test_input) == 2 test_input = { "start": 1000, "finish": 2000, "limit": 4, "s": "3000" } assert my_solution.numberOfPowerfulInt(**test_input) == 0 test_input = { "start": 141, "finish": 148, "limit": 9, "s": "9" } assert my_solution.numberOfPowerfulInt(**test_input) == 0 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "17" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "27" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "41" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "47" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "61" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "66" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "71" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 1, "finish": 971, "limit": 9, "s": "72" } assert my_solution.numberOfPowerfulInt(**test_input) == 9 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "20" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "24" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "32" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "33" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "40" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "41" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "42" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "43" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 20, "finish": 1159, "limit": 5, "s": "44" } assert my_solution.numberOfPowerfulInt(**test_input) == 8 test_input = { "start": 1300, "finish": 1400, "limit": 5, "s": "245" } assert my_solution.numberOfPowerfulInt(**test_input) == 0 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "11" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "12" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "13" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "14" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "20" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "21" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "34" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "40" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 3, "finish": 1429, "limit": 5, "s": "43" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "11" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "12" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "14" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "21" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "23" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "31" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "34" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 15, "finish": 1440, "limit": 5, "s": "42" } assert my_solution.numberOfPowerfulInt(**test_input) == 10 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "12" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "20" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "24" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "30" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "33" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "40" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "42" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 10, "finish": 1844, "limit": 5, "s": "44" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "11" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "13" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "14" } assert my_solution.numberOfPowerfulInt(**test_input) == 11 test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "22" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "30" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "33" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 16, "finish": 1848, "limit": 5, "s": "43" } assert my_solution.numberOfPowerfulInt(**test_input) == 12 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "1" } assert my_solution.numberOfPowerfulInt(**test_input) == 162 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "10" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "100" } assert my_solution.numberOfPowerfulInt(**test_input) == 2 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "11" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "12" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "13" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "14" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "15" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "16" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "17" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "18" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "2" } assert my_solution.numberOfPowerfulInt(**test_input) == 162 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "20" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "21" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "22" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "23" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "24" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "25" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "26" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "27" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "28" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "3" } assert my_solution.numberOfPowerfulInt(**test_input) == 162 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "30" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "31" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "32" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "33" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "34" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "35" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "36" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "37" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "38" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "4" } assert my_solution.numberOfPowerfulInt(**test_input) == 162 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "40" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "41" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "42" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "43" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "44" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "45" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "46" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "47" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "48" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "5" } assert my_solution.numberOfPowerfulInt(**test_input) == 162 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "50" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "51" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "52" } assert my_solution.numberOfPowerfulInt(**test_input) == 18 test_input = { "start": 1, "finish": 2000, "limit": 8, "s": "53" } assert my_solution.numberOfPowerfulInt(**test_input) == 18
1,704,551,400
weekly-contest-378-check-if-bitwise-or-has-trailing-zeros
https://leetcode.com/problems/check-if-bitwise-or-has-trailing-zeros
check-if-bitwise-or-has-trailing-zeros
{ "questionId": "3246", "questionFrontendId": "2980", "title": "Check if Bitwise OR Has Trailing Zeros", "titleSlug": "check-if-bitwise-or-has-trailing-zeros", "isPaidOnly": false, "difficulty": "Easy", "likes": 68, "dislikes": 5, "categoryTitle": "Algorithms" }
""" 给你一个 正整数 数组 nums 。 你需要检查是否可以从数组中选出 两个或更多 元素,满足这些元素的按位或运算( OR)结果的二进制表示中 至少 存在一个尾随零。 例如,数字 5 的二进制表示是 "101",不存在尾随零,而数字 4 的二进制表示是 "100",存在两个尾随零。 如果可以选择两个或更多元素,其按位或运算结果存在尾随零,返回 true;否则,返回 false 。 示例 1: 输入:nums = [1,2,3,4,5] 输出:true 解释:如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110" ,存在一个尾随零。 示例 2: 输入:nums = [2,4,8,16] 输出:true 解释:如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110",存在一个尾随零。 其他按位或运算结果存在尾随零的可能选择方案包括:(2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), 以及 (2, 4, 8, 16) 。 示例 3: 输入:nums = [1,3,5,7,9] 输出:false 解释:不存在按位或运算结果存在尾随零的选择方案。 提示: 2 <= nums.length <= 100 1 <= nums[i] <= 100 """ class Solution: def hasTrailingZeros(self, nums: List[int]) -> bool:
给你一个 正整数 数组 nums 。 你需要检查是否可以从数组中选出 两个或更多 元素,满足这些元素的按位或运算( OR)结果的二进制表示中 至少 存在一个尾随零。 例如,数字 5 的二进制表示是 "101",不存在尾随零,而数字 4 的二进制表示是 "100",存在两个尾随零。 如果可以选择两个或更多元素,其按位或运算结果存在尾随零,返回 true;否则,返回 false 。 示例 1: 输入:nums = [1,2,3,4,5] 输出:true 解释:如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110" ,存在一个尾随零。 示例 2: 输入:nums = [2,4,8,16] 输出:true 解释:如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110",存在一个尾随零。 其他按位或运算结果存在尾随零的可能选择方案包括:(2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), 以及 (2, 4, 8, 16) 。 示例 3: 输入:nums = [1,3,5,7,9] 输出:false 解释:不存在按位或运算结果存在尾随零的选择方案。 提示: 2 <= nums.length <= 100 1 <= nums[i] <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def hasTrailingZeros(self, nums: List[int]) -> bool: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [1,3,5,7,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [1,2] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [2,4,8,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [1,3] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [1,7] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [3,3] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [3,4] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [2,2] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [4,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [4,32] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [6,2] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [6,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [3,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [4,3] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [5,6] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [7,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [8,2] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [8,4] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [7,10] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [9,73] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [8,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [10,5] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [11,17] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [19,11] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [19,35] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [19,51] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [21,61] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [23,21] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [25,25] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [10,2] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [12,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [27,77] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [16,4] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [16,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [16,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [16,32] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [29,13] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [37,69] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [39,53] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [24,32] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [32,32] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [42,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [45,24] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [64,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [49,23] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [4,6,4] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [8,16,4] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [57,27] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [8,16,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [10,4,6] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [12,8,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [63,47] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [67,69] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [69,87] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [16,8,4] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [77,49] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [89,31] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [1,3,4] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [1,5,3] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [1,7,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [3,69,59] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [16,16,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [6,5,5] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [7,77,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [9,77,51] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [16,32,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [16,32,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [10,1,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [10,7,5] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [11,23,27] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [15,13,63] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [21,27,79] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [23,23,47] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [35,91,15] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [32,4,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [32,8,48] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [33,40,84] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [41,83,53] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [64,48,6] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [75,34,58] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [3,8,2,3] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [43,15,41] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [43,65,79] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [47,7,19] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [4,6,1,1] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [4,10,1,7] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [49,73,81] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [6,3,10,6] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [6,4,2,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [6,12,12,24] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [7,2,4,4] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [8,6,4,32] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [8,12,16,2] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [12,64,16,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [16,4,8,16] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [51,33,25] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [16,8,64,4] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [16,16,12,8] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [51,43,9] } assert my_solution.hasTrailingZeros(**test_input) == False
1,703,989,800
weekly-contest-378-find-longest-special-substring-that-occurs-thrice-i
https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i
find-longest-special-substring-that-occurs-thrice-i
{ "questionId": "3267", "questionFrontendId": "2981", "title": "Find Longest Special Substring That Occurs Thrice I", "titleSlug": "find-longest-special-substring-that-occurs-thrice-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 112, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存在出现至少三次的特殊子字符串。因此返回 -1 。 示例 3: 输入:s = "abcaba" 输出:1 解释:出现三次的最长特殊子字符串是 "a" :子字符串 "abcaba"、"abcaba" 和 "abcaba"。 可以证明最大长度是 1 。 提示: 3 <= s.length <= 50 s 仅由小写英文字母组成。 """ class Solution: def maximumLength(self, s: str) -> int:
给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存在出现至少三次的特殊子字符串。因此返回 -1 。 示例 3: 输入:s = "abcaba" 输出:1 解释:出现三次的最长特殊子字符串是 "a" :子字符串 "abcaba"、"abcaba" 和 "abcaba"。 可以证明最大长度是 1 。 提示: 3 <= s.length <= 50 s 仅由小写英文字母组成。 请完成下面的代码来解决上述问题: ```python class Solution: def maximumLength(self, s: str) -> int: ```
my_solution = Solution() test_input = { "s": "aaaa" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "abcdef" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "abcaba" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "abcccccdddd" } assert my_solution.maximumLength(**test_input) == 3 test_input = { "s": "aaa" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "acc" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "cab" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "cad" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "cbc" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "ccc" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "dca" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "ddd" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "fff" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ggg" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "hhh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "kkk" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "lll" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ooo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ppp" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "qqq" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "rrr" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "xxx" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "yyy" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "aaau" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "affe" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "agae" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "aiii" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bbbb" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "bbbz" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bddd" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "beee" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bnnn" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bsss" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bxxx" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "cafc" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "ccag" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "cfff" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "dddd" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "dsss" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "eccc" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "eeew" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "gfdc" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "giii" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "hhhn" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "hyyy" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "iiii" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "jiii" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "jjjj" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "kbbb" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "kddd" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "looo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "nnnn" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "oaaa" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "osss" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "pppp" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "pppw" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "qqqc" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "qqqo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "qqqq" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "reee" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "rzzz" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "thhh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "tttt" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "unnn" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "uuuu" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "uyyy" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "vddd" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "vfff" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "vvvv" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "wbbb" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "wqqq" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "wwwg" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "xxxk" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "xxxx" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "zfff" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ammmm" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "aqqqu" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "axxxx" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "bahhh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bbbbz" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "bbccc" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "biaei" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "bjjjj" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "cccll" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ceaaa" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "cjlll" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "crqqq" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "cyyyy" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "ddddj" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "ddddt" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "dkkkk" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "eefff" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "efage" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "epppp" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "fafff" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ffbbb" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ffffr" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "gcgbf" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "gcooo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "gfgec" } assert my_solution.maximumLength(**test_input) == -1
1,703,989,800
weekly-contest-378-find-longest-special-substring-that-occurs-thrice-ii
https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-ii
find-longest-special-substring-that-occurs-thrice-ii
{ "questionId": "3266", "questionFrontendId": "2982", "title": "Find Longest Special Substring That Occurs Thrice II", "titleSlug": "find-longest-special-substring-that-occurs-thrice-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 172, "dislikes": 17, "categoryTitle": "Algorithms" }
""" 给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存在出现至少三次的特殊子字符串。因此返回 -1 。 示例 3: 输入:s = "abcaba" 输出:1 解释:出现三次的最长特殊子字符串是 "a" :子字符串 "abcaba"、"abcaba" 和 "abcaba"。 可以证明最大长度是 1 。 提示: 3 <= s.length <= 5 * 105 s 仅由小写英文字母组成。 """ class Solution: def maximumLength(self, s: str) -> int:
给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存在出现至少三次的特殊子字符串。因此返回 -1 。 示例 3: 输入:s = "abcaba" 输出:1 解释:出现三次的最长特殊子字符串是 "a" :子字符串 "abcaba"、"abcaba" 和 "abcaba"。 可以证明最大长度是 1 。 提示: 3 <= s.length <= 5 * 105 s 仅由小写英文字母组成。 请完成下面的代码来解决上述问题: ```python class Solution: def maximumLength(self, s: str) -> int: ```
my_solution = Solution() test_input = { "s": "aaaa" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "abcdef" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "abcaba" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "abcccccdddd" } assert my_solution.maximumLength(**test_input) == 3 test_input = { "s": "acd" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "bad" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "bbc" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "ccc" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "cda" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "dab" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "ddd" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "eee" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "fff" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "hhh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "jjj" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "kkk" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "lll" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "mmm" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "nnn" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ooo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ppp" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "qqq" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "rrr" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "sss" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "vvv" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "yyy" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "aaaj" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "aada" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "accc" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "auuu" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "azzz" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bbbb" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "bbbr" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bccc" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "brrr" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "cccc" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "clll" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "dbac" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "dddd" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "dddr" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "dddu" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "dzzz" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "eeee" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "efda" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "efdc" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "ekkk" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ggge" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "gggp" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "gggr" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "gggy" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "guuu" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "hwww" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "hxxx" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "jjjj" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "jjjk" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "kkkk" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "lllm" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "lttt" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "mwww" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "nnnd" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "nnni" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "oooe" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "oooh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "oooo" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "oooq" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "paaa" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "pggg" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "qooo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "rrrr" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "smmm" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "thhh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "tttt" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "uuuu" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "vppp" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "xqqq" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "xxxj" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ysss" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "zzzz" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "aaaak" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "bbbjj" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bbvvv" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "bfooo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "buooo" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "cffff" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "cirrr" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "cwwwv" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "ddhhh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "deeee" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "deiii" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "dtttk" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "eeeeq" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "eeekk" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "eesss" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "euuuu" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "euylz" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "fffhh" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "fpddd" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "gcccn" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "gtwww" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "hcfff" } assert my_solution.maximumLength(**test_input) == 1
1,703,989,800
weekly-contest-378-palindrome-rearrangement-queries
https://leetcode.com/problems/palindrome-rearrangement-queries
palindrome-rearrangement-queries
{ "questionId": "3203", "questionFrontendId": "2983", "title": "Palindrome Rearrangement Queries", "titleSlug": "palindrome-rearrangement-queries", "isPaidOnly": false, "difficulty": "Hard", "likes": 73, "dislikes": 23, "categoryTitle": "Algorithms" }
""" 给你一个长度为 偶数n,下标从 0开始的字符串s。 同时给你一个下标从 0开始的二维整数数组queries,其中queries[i] = [ai, bi, ci, di]。 对于每个查询i,你需要执行以下操作: 将下标在范围0 <= ai <= bi < n / 2内的子字符串s[ai:bi]中的字符重新排列。 将下标在范围 n / 2 <= ci <= di < n内的 子字符串s[ci:di]中的字符重新排列。 对于每个查询,你的任务是判断执行操作后能否让 s变成一个 回文串 。 每个查询与其他查询都是 独立的。 请你返回一个下标从 0开始的数组answer,如果第i个查询执行操作后,可以将s变为一个回文串,那么answer[i] =true,否则为false。 子字符串指的是一个字符串中一段连续的字符序列。 s[x:y]表示 s中从下标 x到 y且两个端点 都包含的子字符串。 示例 1: 输入:s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]] 输出:[true,true] 解释:这个例子中,有 2 个查询: 第一个查询: - a0 = 1, b0 = 1, c0 = 3, d0 = 5 - 你可以重新排列 s[1:1] => abcabc 和 s[3:5] => abcabc。 - 为了让 s 变为回文串,s[3:5] 可以重新排列得到 => abccba 。 - 现在 s 是一个回文串。所以 answer[0] = true 。 第二个查询: - a1 = 0, b1 = 2, c1 = 5, d1 = 5. - 你可以重新排列 s[0:2] => abcabc 和 s[5:5] => abcabc。 - 为了让 s 变为回文串,s[0:2] 可以重新排列得到 => cbaabc 。 - 现在 s 是一个回文串,所以 answer[1] = true 。 示例 2: 输入:s = "abbcdecbba", queries = [[0,2,7,9]] 输出:[false] 解释:这个示例中,只有一个查询。 a0 = 0, b0 = 2, c0 = 7, d0 = 9. 你可以重新排列 s[0:2] => abbcdecbba 和 s[7:9] => abbcdecbba。 无法通过重新排列这些子字符串使 s 变为一个回文串,因为 s[3:6] 不是一个回文串。 所以 answer[0] = false 。 示例 3: 输入:s = "acbcab", queries = [[1,2,4,5]] 输出:[true] 解释:这个示例中,只有一个查询。 a0 = 1, b0 = 2, c0 = 4, d0 = 5. 你可以重新排列 s[1:2] => acbcab 和 s[4:5] => acbcab。 为了让 s 变为回文串,s[1:2] 可以重新排列得到 => abccab。 然后 s[4:5] 重新排列得到 abccba。 现在 s 是一个回文串,所以 answer[0] = true 。 提示: 2 <= n == s.length <= 105 1 <= queries.length <= 105 queries[i].length == 4 ai == queries[i][0], bi == queries[i][1] ci == queries[i][2], di == queries[i][3] 0 <= ai <= bi < n / 2 n / 2 <= ci <= di < n n是一个偶数。 s 只包含小写英文字母。 """ class Solution: def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
给你一个长度为 偶数n,下标从 0开始的字符串s。 同时给你一个下标从 0开始的二维整数数组queries,其中queries[i] = [ai, bi, ci, di]。 对于每个查询i,你需要执行以下操作: 将下标在范围0 <= ai <= bi < n / 2内的子字符串s[ai:bi]中的字符重新排列。 将下标在范围 n / 2 <= ci <= di < n内的 子字符串s[ci:di]中的字符重新排列。 对于每个查询,你的任务是判断执行操作后能否让 s变成一个 回文串 。 每个查询与其他查询都是 独立的。 请你返回一个下标从 0开始的数组answer,如果第i个查询执行操作后,可以将s变为一个回文串,那么answer[i] =true,否则为false。 子字符串指的是一个字符串中一段连续的字符序列。 s[x:y]表示 s中从下标 x到 y且两个端点 都包含的子字符串。 示例 1: 输入:s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]] 输出:[true,true] 解释:这个例子中,有 2 个查询: 第一个查询: - a0 = 1, b0 = 1, c0 = 3, d0 = 5 - 你可以重新排列 s[1:1] => abcabc 和 s[3:5] => abcabc。 - 为了让 s 变为回文串,s[3:5] 可以重新排列得到 => abccba 。 - 现在 s 是一个回文串。所以 answer[0] = true 。 第二个查询: - a1 = 0, b1 = 2, c1 = 5, d1 = 5. - 你可以重新排列 s[0:2] => abcabc 和 s[5:5] => abcabc。 - 为了让 s 变为回文串,s[0:2] 可以重新排列得到 => cbaabc 。 - 现在 s 是一个回文串,所以 answer[1] = true 。 示例 2: 输入:s = "abbcdecbba", queries = [[0,2,7,9]] 输出:[false] 解释:这个示例中,只有一个查询。 a0 = 0, b0 = 2, c0 = 7, d0 = 9. 你可以重新排列 s[0:2] => abbcdecbba 和 s[7:9] => abbcdecbba。 无法通过重新排列这些子字符串使 s 变为一个回文串,因为 s[3:6] 不是一个回文串。 所以 answer[0] = false 。 示例 3: 输入:s = "acbcab", queries = [[1,2,4,5]] 输出:[true] 解释:这个示例中,只有一个查询。 a0 = 1, b0 = 2, c0 = 4, d0 = 5. 你可以重新排列 s[1:2] => acbcab 和 s[4:5] => acbcab。 为了让 s 变为回文串,s[1:2] 可以重新排列得到 => abccab。 然后 s[4:5] 重新排列得到 abccba。 现在 s 是一个回文串,所以 answer[0] = true 。 提示: 2 <= n == s.length <= 105 1 <= queries.length <= 105 queries[i].length == 4 ai == queries[i][0], bi == queries[i][1] ci == queries[i][2], di == queries[i][3] 0 <= ai <= bi < n / 2 n / 2 <= ci <= di < n n是一个偶数。 s 只包含小写英文字母。 请完成下面的代码来解决上述问题: ```python class Solution: def canMakePalindromeQueries(self, s: str, queries: List[List[int]]) -> List[bool]: ```
my_solution = Solution() test_input = { "s": "abcabc", "queries": [[1,1,3,5],[0,2,5,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "abbcdecbba", "queries": [[0,2,7,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "acbcab", "queries": [[1,2,4,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "bcdbdc", "queries": [[1,2,3,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "bb", "queries": [[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "eoquueqo", "queries": [[3,3,6,6]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "dd", "queries": [[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "ceddceddcc", "queries": [[0,1,6,8]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "bdbd", "queries": [[0,0,2,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "eeee", "queries": [[0,1,2,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "deebdeeddb", "queries": [[1,2,5,7]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "xwaswsxwaw", "queries": [[1,3,5,6]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "askloakosala", "queries": [[2,4,7,10]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "bbccbb", "queries": [[0,1,4,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "djaypzjpyzad", "queries": [[0,3,7,8]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "ajvnbnznjnzbva", "queries": [[5,6,10,11]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "vbeptwzvtwpzbe", "queries": [[3,6,10,11]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "cababc", "queries": [[1,2,3,4]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "cbbbbc", "queries": [[1,1,5,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "qupzexxhqxpzhxeu", "queries": [[2,4,8,12]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "cdbdbc", "queries": [[1,2,3,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "odaxusaweuasuoeudxwa", "queries": [[0,5,10,14]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "ujfscqolkwjucoswlkfq", "queries": [[1,9,17,18]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "ceacea", "queries": [[0,2,3,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "bnjzcgmnecxxbmnjngxzecxc", "queries": [[8,9,19,22]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "daeaed", "queries": [[0,2,3,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "ddaadd", "queries": [[0,2,3,4]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "ddedde", "queries": [[0,2,4,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "pmzwetzhzursuhmeswpzrztz", "queries": [[4,6,16,17]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "qcryjkdzmqyoojzrckymdqyq", "queries": [[2,8,21,22]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "qdltkndnclarncadtqnlldkr", "queries": [[3,4,15,16]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "ecbbce", "queries": [[0,1,3,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "eczecz", "queries": [[0,0,3,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "etuouqokbalafokfbuqaaoetlu", "queries": [[3,11,21,23]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "mpepem", "queries": [[0,2,3,4]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "nbpechkpmudbsenphdmsbbupck", "queries": [[6,7,18,19]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "bccacacb", "queries": [[3,3,4,7]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "stgjtzqwgkuadjgqugkwdtzast", "queries": [[5,10,13,23]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "qiyikbayvhkcgxyaqckgxkhivbyi", "queries": [[5,12,17,24]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "ceedceed", "queries": [[0,1,4,7]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "rcguwczbjhjhgqrggqcbwjzhjuch", "queries": [[5,7,16,20]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "ckwbnmqmtzbixrrkixbtbqzmnwmc", "queries": [[1,9,15,24]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True] test_input = { "s": "riirxzxuqpspoiixpirsoxrzpiuq", "queries": [[1,6,14,21]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "geettndnusqufidtqdfgtsieenundu", "queries": [[6,8,19,23]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "bb", "queries": [[0,0,1,1],[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "cc", "queries": [[0,0,1,1],[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "dd", "queries": [[0,0,1,1],[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "ee", "queries": [[0,0,1,1],[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "aeae", "queries": [[1,1,2,3],[1,1,3,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,False] test_input = { "s": "eaae", "queries": [[0,1,3,3],[0,0,2,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "eded", "queries": [[0,1,2,3],[1,1,2,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "lrlr", "queries": [[0,1,2,3],[0,0,2,2]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,False] test_input = { "s": "dbaabd", "queries": [[0,1,5,5],[1,2,4,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "dcbcbd", "queries": [[0,1,4,4],[0,2,3,4]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,True] test_input = { "s": "hykkyh", "queries": [[0,0,3,4],[1,2,3,4]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "lvrvrl", "queries": [[2,2,4,5],[0,2,3,4]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,True] test_input = { "s": "adceaecd", "queries": [[3,3,5,5],[0,1,4,6]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,True] test_input = { "s": "baadadba", "queries": [[1,2,4,5],[0,2,4,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,True] test_input = { "s": "bceaabec", "queries": [[1,3,4,7],[0,2,6,6]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "ifchcifh", "queries": [[1,2,5,6],[1,1,4,6]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "adeeeeeaed", "queries": [[2,4,7,9],[3,4,8,8]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,False] test_input = { "s": "aedbdbddea", "queries": [[4,4,7,8],[2,2,8,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "caeaaaaaec", "queries": [[0,2,5,8],[0,0,5,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "dbaccccdba", "queries": [[4,4,6,7],[2,3,8,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "deabadabea", "queries": [[0,3,7,9],[0,2,5,7]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,True] test_input = { "s": "eddeededee", "queries": [[0,3,6,9],[0,0,6,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "eedbbedebb", "queries": [[2,2,6,7],[2,2,5,6]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "gvtkakgvat", "queries": [[1,2,7,7],[2,3,7,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "bzvvicviczbv", "queries": [[1,2,7,8],[1,4,7,8]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "ljccjajcljac", "queries": [[2,4,6,10],[3,5,7,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "rxvzvezvrvxe", "queries": [[1,4,8,10],[3,3,10,11]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "amgpelwpmlaewg", "queries": [[3,4,7,9],[0,6,7,10]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,True] test_input = { "s": "leubdglmbglleudm", "queries": [[1,3,9,14],[2,6,13,14]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "ooxuznriuzrooixn", "queries": [[1,3,10,12],[1,4,9,13]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "nlaonaphinpnalohai", "queries": [[2,5,13,13],[2,7,9,14]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "rujokutobuttlysjusrtltuobkoytu", "queries": [[5,6,18,23],[10,13,15,26]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False] test_input = { "s": "bb", "queries": [[0,0,1,1],[0,0,1,1],[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "cc", "queries": [[0,0,1,1],[0,0,1,1],[0,0,1,1]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "dbdb", "queries": [[0,0,2,2],[1,1,3,3],[0,1,2,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,True] test_input = { "s": "ebbe", "queries": [[0,1,3,3],[1,1,2,2],[0,0,2,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "acacaa", "queries": [[0,1,5,5],[1,1,4,4],[1,2,3,4]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,True] test_input = { "s": "bbaabb", "queries": [[0,1,4,5],[0,2,3,5],[2,2,5,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "bbebbe", "queries": [[0,1,3,5],[2,2,4,5],[0,1,5,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,False,False] test_input = { "s": "ddaadd", "queries": [[1,1,4,4],[0,0,4,4],[0,2,3,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "nlhhln", "queries": [[2,2,4,5],[1,2,5,5],[2,2,3,3]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "zbebez", "queries": [[0,2,5,5],[1,1,3,5],[0,2,4,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "cbcbbcbc", "queries": [[0,2,7,7],[1,2,4,7],[0,2,4,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,True] test_input = { "s": "deceecde", "queries": [[3,3,6,7],[1,2,4,5],[2,3,7,7]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,False,False] test_input = { "s": "fydyfyyd", "queries": [[0,2,4,6],[1,3,4,7],[2,3,6,7]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True,False] test_input = { "s": "dccabcdbca", "queries": [[1,3,5,8],[2,4,7,7],[0,2,6,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "eacbdeacbd", "queries": [[4,4,8,9],[3,4,7,9],[0,0,6,8]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "eddaaedada", "queries": [[0,1,7,8],[0,1,7,8],[0,3,7,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "vvsbgsvgbv", "queries": [[0,1,6,9],[2,3,8,9],[0,0,6,7]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "sukesivksseuiv", "queries": [[2,3,11,13],[5,5,7,13],[2,5,8,13]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,True,False] test_input = { "s": "pbcjpsfxwtbcfjwpsptx", "queries": [[0,4,13,13],[5,5,15,18],[2,3,13,18]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "natvhtruvwyutyvvnarhwt", "queries": [[5,7,14,21],[0,8,11,19],[2,8,11,14]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "yjsjywxbriejyxieysrwbj", "queries": [[4,5,15,19],[1,9,12,17],[3,6,12,18]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "ellaghdbmazdallhmegabddz", "queries": [[4,6,13,19],[4,11,17,17],[7,10,13,16]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "wcfnhuaulqxbuuxafcwhnbql", "queries": [[5,11,13,18],[7,8,18,21],[3,6,21,23]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False] test_input = { "s": "qyioinjmvpgpropimvqiygrnjp", "queries": [[1,4,15,25],[1,8,19,22],[4,9,18,23]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False,False,False]
1,703,989,800
weekly-contest-377-minimum-number-game
https://leetcode.com/problems/minimum-number-game
minimum-number-game
{ "questionId": "3226", "questionFrontendId": "2974", "title": "Minimum Number Game", "titleSlug": "minimum-number-game", "isPaidOnly": false, "difficulty": "Easy", "likes": 38, "dislikes": 2, "categoryTitle": "Algorithms" }
""" 你有一个下标从 0 开始、长度为 偶数 的整数数组 nums ,同时还有一个空数组 arr 。Alice 和 Bob 决定玩一个游戏,游戏中每一轮 Alice 和 Bob 都会各自执行一次操作。游戏规则如下: 每一轮,Alice 先从 nums 中移除一个 最小 元素,然后 Bob 执行同样的操作。 接着,Bob 会将移除的元素添加到数组 arr 中,然后 Alice 也执行同样的操作。 游戏持续进行,直到 nums 变为空。 返回结果数组 arr 。 示例 1: 输入:nums = [5,4,2,3] 输出:[3,2,5,4] 解释:第一轮,Alice 先移除 2 ,然后 Bob 移除 3 。然后 Bob 先将 3 添加到 arr 中,接着 Alice 再将 2 添加到 arr 中。于是 arr = [3,2] 。 第二轮开始时,nums = [5,4] 。Alice 先移除 4 ,然后 Bob 移除 5 。接着他们都将元素添加到 arr 中,arr 变为 [3,2,5,4] 。 示例 2: 输入:nums = [2,5] 输出:[5,2] 解释:第一轮,Alice 先移除 2 ,然后 Bob 移除 5 。然后 Bob 先将 5 添加到 arr 中,接着 Alice 再将 2 添加到 arr 中。于是 arr = [5,2] 。 提示: 1 <= nums.length <= 100 1 <= nums[i] <= 100 nums.length % 2 == 0 """ class Solution: def numberGame(self, nums: List[int]) -> List[int]:
你有一个下标从 0 开始、长度为 偶数 的整数数组 nums ,同时还有一个空数组 arr 。Alice 和 Bob 决定玩一个游戏,游戏中每一轮 Alice 和 Bob 都会各自执行一次操作。游戏规则如下: 每一轮,Alice 先从 nums 中移除一个 最小 元素,然后 Bob 执行同样的操作。 接着,Bob 会将移除的元素添加到数组 arr 中,然后 Alice 也执行同样的操作。 游戏持续进行,直到 nums 变为空。 返回结果数组 arr 。 示例 1: 输入:nums = [5,4,2,3] 输出:[3,2,5,4] 解释:第一轮,Alice 先移除 2 ,然后 Bob 移除 3 。然后 Bob 先将 3 添加到 arr 中,接着 Alice 再将 2 添加到 arr 中。于是 arr = [3,2] 。 第二轮开始时,nums = [5,4] 。Alice 先移除 4 ,然后 Bob 移除 5 。接着他们都将元素添加到 arr 中,arr 变为 [3,2,5,4] 。 示例 2: 输入:nums = [2,5] 输出:[5,2] 解释:第一轮,Alice 先移除 2 ,然后 Bob 移除 5 。然后 Bob 先将 5 添加到 arr 中,接着 Alice 再将 2 添加到 arr 中。于是 arr = [5,2] 。 提示: 1 <= nums.length <= 100 1 <= nums[i] <= 100 nums.length % 2 == 0 请完成下面的代码来解决上述问题: ```python class Solution: def numberGame(self, nums: List[int]) -> List[int]: ```
my_solution = Solution() test_input = { "nums": [5,4,2,3] } assert my_solution.numberGame(**test_input) == [3,2,5,4] test_input = { "nums": [2,5] } assert my_solution.numberGame(**test_input) == [5,2] test_input = { "nums": [4,4,3,8] } assert my_solution.numberGame(**test_input) == [4,3,8,4] test_input = { "nums": [2,5,3,8] } assert my_solution.numberGame(**test_input) == [3,2,8,5] test_input = { "nums": [2,7,9,6,4,6] } assert my_solution.numberGame(**test_input) == [4,2,6,6,9,7] test_input = { "nums": [18,26,37,46,13,33,39,1,37,16] } assert my_solution.numberGame(**test_input) == [13,1,18,16,33,26,37,37,46,39] test_input = { "nums": [17,2,4,11,14,18] } assert my_solution.numberGame(**test_input) == [4,2,14,11,18,17] test_input = { "nums": [20,30,12,3,11,17,32,12] } assert my_solution.numberGame(**test_input) == [11,3,12,12,20,17,32,30] test_input = { "nums": [9,32,6,11,11,39,18,29,44,29] } assert my_solution.numberGame(**test_input) == [9,6,11,11,29,18,32,29,44,39] test_input = { "nums": [7,2,3,4] } assert my_solution.numberGame(**test_input) == [3,2,7,4] test_input = { "nums": [8,7,1,3] } assert my_solution.numberGame(**test_input) == [3,1,8,7] test_input = { "nums": [2,6,6,6] } assert my_solution.numberGame(**test_input) == [6,2,6,6] test_input = { "nums": [1,2] } assert my_solution.numberGame(**test_input) == [2,1] test_input = { "nums": [4,1,1,3] } assert my_solution.numberGame(**test_input) == [1,1,4,3] test_input = { "nums": [13,12,18,11,15,28,26,2] } assert my_solution.numberGame(**test_input) == [11,2,13,12,18,15,28,26] test_input = { "nums": [14,30,29,3,23,21,26,23] } assert my_solution.numberGame(**test_input) == [14,3,23,21,26,23,30,29] test_input = { "nums": [1,1] } assert my_solution.numberGame(**test_input) == [1,1] test_input = { "nums": [2,1] } assert my_solution.numberGame(**test_input) == [2,1] test_input = { "nums": [12,1,28,23,2,31,11,26] } assert my_solution.numberGame(**test_input) == [2,1,12,11,26,23,31,28] test_input = { "nums": [21,11,37,1,40,50,49,45,28,47] } assert my_solution.numberGame(**test_input) == [11,1,28,21,40,37,47,45,50,49] test_input = { "nums": [25,22,31,7,30,9,9,18] } assert my_solution.numberGame(**test_input) == [9,7,18,9,25,22,31,30] test_input = { "nums": [2,4,10,9,16,9] } assert my_solution.numberGame(**test_input) == [4,2,9,9,16,10] test_input = { "nums": [5,2,3,5] } assert my_solution.numberGame(**test_input) == [3,2,5,5] test_input = { "nums": [6,44,37,6,28,44,30,36,25,24] } assert my_solution.numberGame(**test_input) == [6,6,25,24,30,28,37,36,44,44] test_input = { "nums": [17,10,6,14,10,18] } assert my_solution.numberGame(**test_input) == [10,6,14,10,18,17] test_input = { "nums": [40,24,23,29,37,26,39,34,39,23] } assert my_solution.numberGame(**test_input) == [23,23,26,24,34,29,39,37,40,39] test_input = { "nums": [2,2] } assert my_solution.numberGame(**test_input) == [2,2] test_input = { "nums": [33,5,31,43,48,18,31,11,19,8] } assert my_solution.numberGame(**test_input) == [8,5,18,11,31,19,33,31,48,43] test_input = { "nums": [37,46,42,19,10,8,43,10,40,13] } assert my_solution.numberGame(**test_input) == [10,8,13,10,37,19,42,40,46,43] test_input = { "nums": [2,19,8,22,1,27,29,7] } assert my_solution.numberGame(**test_input) == [2,1,8,7,22,19,29,27] test_input = { "nums": [2,3,2,3] } assert my_solution.numberGame(**test_input) == [2,2,3,3] test_input = { "nums": [1,4,7,14,8,14] } assert my_solution.numberGame(**test_input) == [4,1,8,7,14,14] test_input = { "nums": [28,47,36,34,19,7,40,46,33,43] } assert my_solution.numberGame(**test_input) == [19,7,33,28,36,34,43,40,47,46] test_input = { "nums": [29,41,20,22,16,27,22,44,10,47] } assert my_solution.numberGame(**test_input) == [16,10,22,20,27,22,41,29,47,44] test_input = { "nums": [14,6,40,19,47,46,34,27,28,10] } assert my_solution.numberGame(**test_input) == [10,6,19,14,28,27,40,34,47,46] test_input = { "nums": [42,43,50,43,36,26,16,12,3,2] } assert my_solution.numberGame(**test_input) == [3,2,16,12,36,26,43,42,50,43] test_input = { "nums": [1,7,24,24,23,32,28,2] } assert my_solution.numberGame(**test_input) == [2,1,23,7,24,24,32,28] test_input = { "nums": [20,19,16,16,19,29,21,5] } assert my_solution.numberGame(**test_input) == [16,5,19,16,20,19,29,21] test_input = { "nums": [20,9,29,29,17,39,27,44,1,8] } assert my_solution.numberGame(**test_input) == [8,1,17,9,27,20,29,29,44,39] test_input = { "nums": [14,11,12,18,9,15] } assert my_solution.numberGame(**test_input) == [11,9,14,12,18,15] test_input = { "nums": [17,22,2,35,15,19,25,5,33,44] } assert my_solution.numberGame(**test_input) == [5,2,17,15,22,19,33,25,44,35] test_input = { "nums": [22,3,26,15,1,5,14,28] } assert my_solution.numberGame(**test_input) == [3,1,14,5,22,15,28,26] test_input = { "nums": [5,24,3,2,17,9,2,4] } assert my_solution.numberGame(**test_input) == [2,2,4,3,9,5,24,17] test_input = { "nums": [2,6,4,7] } assert my_solution.numberGame(**test_input) == [4,2,7,6] test_input = { "nums": [1,33,29,21,25,14,26,35,34,30] } assert my_solution.numberGame(**test_input) == [14,1,25,21,29,26,33,30,35,34] test_input = { "nums": [50,25,42,41,16,23,47,31,23,16] } assert my_solution.numberGame(**test_input) == [16,16,23,23,31,25,42,41,50,47] test_input = { "nums": [31,31,31,12,24,17,11,3,33,13] } assert my_solution.numberGame(**test_input) == [11,3,13,12,24,17,31,31,33,31] test_input = { "nums": [8,3,2,7] } assert my_solution.numberGame(**test_input) == [3,2,8,7] test_input = { "nums": [8,2,8,6] } assert my_solution.numberGame(**test_input) == [6,2,8,8] test_input = { "nums": [4,15,16,2,12,7] } assert my_solution.numberGame(**test_input) == [4,2,12,7,16,15] test_input = { "nums": [5,4,2,4] } assert my_solution.numberGame(**test_input) == [4,2,5,4] test_input = { "nums": [17,13,7,12,19,15,6,22] } assert my_solution.numberGame(**test_input) == [7,6,13,12,17,15,22,19] test_input = { "nums": [2,15,12,16,12,13] } assert my_solution.numberGame(**test_input) == [12,2,13,12,16,15] test_input = { "nums": [3,15,18,16,6,7] } assert my_solution.numberGame(**test_input) == [6,3,15,7,18,16] test_input = { "nums": [4,7,11,6,11,8] } assert my_solution.numberGame(**test_input) == [6,4,8,7,11,11] test_input = { "nums": [1,7,24,23,16,21,9,11] } assert my_solution.numberGame(**test_input) == [7,1,11,9,21,16,24,23] test_input = { "nums": [6,3,10,16,15,6] } assert my_solution.numberGame(**test_input) == [6,3,10,6,16,15] test_input = { "nums": [17,9,1,29,30,5,31,26] } assert my_solution.numberGame(**test_input) == [5,1,17,9,29,26,31,30] test_input = { "nums": [3,6,4,14,9,15] } assert my_solution.numberGame(**test_input) == [4,3,9,6,15,14] test_input = { "nums": [37,38,24,15,12,1,37,19,38,11] } assert my_solution.numberGame(**test_input) == [11,1,15,12,24,19,37,37,38,38] test_input = { "nums": [17,3,8,12,6,9] } assert my_solution.numberGame(**test_input) == [6,3,9,8,17,12] test_input = { "nums": [32,23,27,32,24,26,24,27] } assert my_solution.numberGame(**test_input) == [24,23,26,24,27,27,32,32] test_input = { "nums": [15,16,26,6,5,9,22,14] } assert my_solution.numberGame(**test_input) == [6,5,14,9,16,15,26,22] test_input = { "nums": [14,21,13,10,2,16,14,30] } assert my_solution.numberGame(**test_input) == [10,2,14,13,16,14,30,21] test_input = { "nums": [1,6,30,1,13,25,18,1] } assert my_solution.numberGame(**test_input) == [1,1,6,1,18,13,30,25] test_input = { "nums": [32,12,17,32,11,25,22,18,10,1] } assert my_solution.numberGame(**test_input) == [10,1,12,11,18,17,25,22,32,32] test_input = { "nums": [2,8,5,6] } assert my_solution.numberGame(**test_input) == [5,2,8,6] test_input = { "nums": [27,3,10,25,10,7,15,16] } assert my_solution.numberGame(**test_input) == [7,3,10,10,16,15,27,25] test_input = { "nums": [5,18,19,25,13,21,16,7] } assert my_solution.numberGame(**test_input) == [7,5,16,13,19,18,25,21] test_input = { "nums": [8,6,6,8] } assert my_solution.numberGame(**test_input) == [6,6,8,8] test_input = { "nums": [23,15,39,9,19,10,6,9,33,28] } assert my_solution.numberGame(**test_input) == [9,6,10,9,19,15,28,23,39,33] test_input = { "nums": [16,42,47,16,31,39,8,26,50,33] } assert my_solution.numberGame(**test_input) == [16,8,26,16,33,31,42,39,50,47] test_input = { "nums": [4,31,9,2,4,28,28,12] } assert my_solution.numberGame(**test_input) == [4,2,9,4,28,12,31,28] test_input = { "nums": [9,5,8,11,4,7] } assert my_solution.numberGame(**test_input) == [5,4,8,7,11,9] test_input = { "nums": [44,2,23,3,7,2,36,33,7,21] } assert my_solution.numberGame(**test_input) == [2,2,7,3,21,7,33,23,44,36] test_input = { "nums": [19,9,4,7,29,22,50,28,2,40] } assert my_solution.numberGame(**test_input) == [4,2,9,7,22,19,29,28,50,40] test_input = { "nums": [4,5,5,5] } assert my_solution.numberGame(**test_input) == [5,4,5,5] test_input = { "nums": [42,6,44,47,11,6,30,38,41,43] } assert my_solution.numberGame(**test_input) == [6,6,30,11,41,38,43,42,47,44] test_input = { "nums": [28,4,47,1,7,35,10,10,5,8] } assert my_solution.numberGame(**test_input) == [4,1,7,5,10,8,28,10,47,35] test_input = { "nums": [12,20,14,46,22,1,42,50,47,47] } assert my_solution.numberGame(**test_input) == [12,1,20,14,42,22,47,46,50,47] test_input = { "nums": [37,13,1,38,28,46,18,22,12,7] } assert my_solution.numberGame(**test_input) == [7,1,13,12,22,18,37,28,46,38] test_input = { "nums": [36,41,5,33,5,30,33,31,6,45] } assert my_solution.numberGame(**test_input) == [5,5,30,6,33,31,36,33,45,41] test_input = { "nums": [13,50,42,24,47,41,8,26,34,3] } assert my_solution.numberGame(**test_input) == [8,3,24,13,34,26,42,41,50,47] test_input = { "nums": [24,39,26,46,47,9,33,6,33,40] } assert my_solution.numberGame(**test_input) == [9,6,26,24,33,33,40,39,47,46] test_input = { "nums": [14,13,17,14,12,15,6,32] } assert my_solution.numberGame(**test_input) == [12,6,14,13,15,14,32,17] test_input = { "nums": [46,50,35,11,14,44,17,45,23,34] } assert my_solution.numberGame(**test_input) == [14,11,23,17,35,34,45,44,50,46] test_input = { "nums": [8,27,19,7,10,12,14,50,45,14] } assert my_solution.numberGame(**test_input) == [8,7,12,10,14,14,27,19,50,45] test_input = { "nums": [9,8,5,7,10,9] } assert my_solution.numberGame(**test_input) == [7,5,9,8,10,9] test_input = { "nums": [5,5,3,7] } assert my_solution.numberGame(**test_input) == [5,3,7,5] test_input = { "nums": [26,21,7,13,3,10,9,15] } assert my_solution.numberGame(**test_input) == [7,3,10,9,15,13,26,21] test_input = { "nums": [8,5,8,3] } assert my_solution.numberGame(**test_input) == [5,3,8,8] test_input = { "nums": [18,1,16,18,13,3] } assert my_solution.numberGame(**test_input) == [3,1,16,13,18,18] test_input = { "nums": [25,2,17,26,17,20,19,24] } assert my_solution.numberGame(**test_input) == [17,2,19,17,24,20,26,25] test_input = { "nums": [24,1,18,25,29,17,9,3] } assert my_solution.numberGame(**test_input) == [3,1,17,9,24,18,29,25] test_input = { "nums": [23,17,18,18,18,30,8,19] } assert my_solution.numberGame(**test_input) == [17,8,18,18,19,18,30,23] test_input = { "nums": [12,13,13,18,5,16] } assert my_solution.numberGame(**test_input) == [12,5,13,13,18,16] test_input = { "nums": [19,4,11,7,24,12,24,14] } assert my_solution.numberGame(**test_input) == [7,4,12,11,19,14,24,24] test_input = { "nums": [28,11,11,29,18,2,6,32] } assert my_solution.numberGame(**test_input) == [6,2,11,11,28,18,32,29] test_input = { "nums": [12,17,3,31,15,18,18,2] } assert my_solution.numberGame(**test_input) == [3,2,15,12,18,17,31,18] test_input = { "nums": [24,6,21,30,29,8,23,18] } assert my_solution.numberGame(**test_input) == [8,6,21,18,24,23,30,29]
1,703,385,000
weekly-contest-377-maximum-square-area-by-removing-fences-from-a-field
https://leetcode.com/problems/maximum-square-area-by-removing-fences-from-a-field
maximum-square-area-by-removing-fences-from-a-field
{ "questionId": "3250", "questionFrontendId": "2975", "title": "Maximum Square Area by Removing Fences From a Field", "titleSlug": "maximum-square-area-by-removing-fences-from-a-field", "isPaidOnly": false, "difficulty": "Medium", "likes": 67, "dislikes": 68, "categoryTitle": "Algorithms" }
""" 有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1) 和 (m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFences 和 vFences 给出。 水平栅栏为坐标 (hFences[i], 1) 到 (hFences[i], n),垂直栅栏为坐标 (1, vFences[i]) 到 (m, vFences[i]) 。 返回通过 移除 一些栅栏(可能不移除)所能形成的最大面积的 正方形 田地的面积,或者如果无法形成正方形田地则返回 -1。 由于答案可能很大,所以请返回结果对 109 + 7 取余 后的值。 注意:田地外围两个水平栅栏(坐标 (1, 1) 到 (1, n) 和坐标 (m, 1) 到 (m, n) )以及两个垂直栅栏(坐标 (1, 1) 到 (m, 1) 和坐标 (1, n) 到 (m, n) )所包围。这些栅栏 不能 被移除。 示例 1: 输入:m = 4, n = 3, hFences = [2,3], vFences = [2] 输出:4 解释:移除位于 2 的水平栅栏和位于 2 的垂直栅栏将得到一个面积为 4 的正方形田地。 示例 2: 输入:m = 6, n = 7, hFences = [2], vFences = [4] 输出:-1 解释:可以证明无法通过移除栅栏形成正方形田地。 提示: 3 <= m, n <= 109 1 <= hFences.length, vFences.length <= 600 1 < hFences[i] < m 1 < vFences[i] < n hFences 和 vFences 中的元素是唯一的。 """ class Solution: def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[int]) -> int:
有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1) 和 (m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFences 和 vFences 给出。 水平栅栏为坐标 (hFences[i], 1) 到 (hFences[i], n),垂直栅栏为坐标 (1, vFences[i]) 到 (m, vFences[i]) 。 返回通过 移除 一些栅栏(可能不移除)所能形成的最大面积的 正方形 田地的面积,或者如果无法形成正方形田地则返回 -1。 由于答案可能很大,所以请返回结果对 109 + 7 取余 后的值。 注意:田地外围两个水平栅栏(坐标 (1, 1) 到 (1, n) 和坐标 (m, 1) 到 (m, n) )以及两个垂直栅栏(坐标 (1, 1) 到 (m, 1) 和坐标 (1, n) 到 (m, n) )所包围。这些栅栏 不能 被移除。 示例 1: 输入:m = 4, n = 3, hFences = [2,3], vFences = [2] 输出:4 解释:移除位于 2 的水平栅栏和位于 2 的垂直栅栏将得到一个面积为 4 的正方形田地。 示例 2: 输入:m = 6, n = 7, hFences = [2], vFences = [4] 输出:-1 解释:可以证明无法通过移除栅栏形成正方形田地。 提示: 3 <= m, n <= 109 1 <= hFences.length, vFences.length <= 600 1 < hFences[i] < m 1 < vFences[i] < n hFences 和 vFences 中的元素是唯一的。 请完成下面的代码来解决上述问题: ```python class Solution: def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[int]) -> int: ```
my_solution = Solution() test_input = { "m": 4, "n": 3, "hFences": [2,3], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 6, "n": 7, "hFences": [2], "vFences": [4] } assert my_solution.maximizeSquareArea(**test_input) == -1 test_input = { "m": 4, "n": 4, "hFences": [2], "vFences": [2,3] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 8, "n": 5, "hFences": [5,4], "vFences": [4] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 4, "n": 5, "hFences": [2], "vFences": [4] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 5, "n": 6, "hFences": [4,2,3], "vFences": [4,5] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 3, "n": 9, "hFences": [2], "vFences": [8,6,5,4] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 6, "n": 4, "hFences": [3], "vFences": [3,2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 7, "n": 4, "hFences": [2,3,6,5], "vFences": [3,2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 8, "n": 8, "hFences": [2,3,6,7], "vFences": [6,5,7,4,2,3] } assert my_solution.maximizeSquareArea(**test_input) == 49 test_input = { "m": 9, "n": 9, "hFences": [2,4], "vFences": [6,4,2] } assert my_solution.maximizeSquareArea(**test_input) == 64 test_input = { "m": 7, "n": 7, "hFences": [4,3], "vFences": [2,6] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 4, "n": 6, "hFences": [2], "vFences": [3,4,2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 8, "n": 6, "hFences": [7,5,2,4,3], "vFences": [5,3] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 9, "n": 3, "hFences": [7,4,5], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 4, "n": 3, "hFences": [3], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 4, "n": 3, "hFences": [3,2], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 8, "n": 6, "hFences": [6,4,3,7,2,5], "vFences": [5,3,4,2] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 8, "n": 8, "hFences": [6,3,7,5], "vFences": [6,2,7] } assert my_solution.maximizeSquareArea(**test_input) == 49 test_input = { "m": 5, "n": 5, "hFences": [4,3], "vFences": [4] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 5, "n": 3, "hFences": [4], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 1 test_input = { "m": 7, "n": 9, "hFences": [5], "vFences": [2,7,6,8,3] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 8, "n": 6, "hFences": [5,4,6,7,3], "vFences": [5,3] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 9, "n": 7, "hFences": [6,4,7,5,8], "vFences": [6,4,2,3] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 6, "n": 7, "hFences": [5], "vFences": [5,3,6] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 3, "n": 3, "hFences": [2], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 7, "n": 8, "hFences": [4,6,2,5,3], "vFences": [3,5,2,4,7] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 3, "n": 5, "hFences": [2], "vFences": [4,2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 4, "n": 3, "hFences": [2], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 9, "n": 3, "hFences": [3], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 6, "n": 7, "hFences": [4,2,3,5], "vFences": [3,5,6] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 8, "n": 4, "hFences": [6,3,2,4,7,5], "vFences": [2,3] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 4, "n": 4, "hFences": [2], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 3, "n": 6, "hFences": [2], "vFences": [4,3,5] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 8, "n": 6, "hFences": [5], "vFences": [4,2,5] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 9, "n": 9, "hFences": [5,4,2], "vFences": [8,4,3,5,6] } assert my_solution.maximizeSquareArea(**test_input) == 64 test_input = { "m": 5, "n": 9, "hFences": [3,2,4], "vFences": [7,6,5] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 5, "n": 5, "hFences": [4,3,2], "vFences": [3,4,2] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 7, "n": 4, "hFences": [5,2,4], "vFences": [3] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 8, "n": 5, "hFences": [4,6,2,3], "vFences": [4] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 9, "n": 5, "hFences": [6], "vFences": [3] } assert my_solution.maximizeSquareArea(**test_input) == -1 test_input = { "m": 9, "n": 4, "hFences": [2,8,3,7,4,6,5], "vFences": [2,3] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 5, "n": 8, "hFences": [2,3,4], "vFences": [3,5,6,4] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 9, "n": 6, "hFences": [7,3,4,5,8,2], "vFences": [5,3,2,4] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 5, "n": 4, "hFences": [4], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 9, "n": 6, "hFences": [2,5], "vFences": [5,2] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 6, "n": 9, "hFences": [4,5,2], "vFences": [5,7,8,2,3,6] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 4, "n": 5, "hFences": [2], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 9, "n": 6, "hFences": [2,4], "vFences": [5,3] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 4, "n": 7, "hFences": [2], "vFences": [4,3,6,2,5] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 8, "n": 3, "hFences": [3,2,5], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 6, "n": 8, "hFences": [4,2,3,5], "vFences": [7,4,5,6] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 3, "n": 5, "hFences": [2], "vFences": [3] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 8, "n": 8, "hFences": [2,5,6], "vFences": [3,7,4,2,5] } assert my_solution.maximizeSquareArea(**test_input) == 49 test_input = { "m": 8, "n": 7, "hFences": [3,4,7], "vFences": [2,6,3,4] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 7, "n": 4, "hFences": [3,6,5], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 5, "n": 6, "hFences": [2,3], "vFences": [3,2,5] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 6, "n": 7, "hFences": [5], "vFences": [4,2,5,6] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 8, "n": 8, "hFences": [4,5,2,7], "vFences": [5,3,4,2,7,6] } assert my_solution.maximizeSquareArea(**test_input) == 49 test_input = { "m": 7, "n": 9, "hFences": [6,3,4], "vFences": [8,6,2] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 7, "n": 4, "hFences": [3,4,6], "vFences": [3,2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 6, "n": 8, "hFences": [5,4,3], "vFences": [5,7,3,2,6,4] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 8, "n": 9, "hFences": [2,3,4,7,6,5], "vFences": [3,7,8] } assert my_solution.maximizeSquareArea(**test_input) == 49 test_input = { "m": 3, "n": 4, "hFences": [2], "vFences": [2,3] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 9, "n": 9, "hFences": [8,5,6,2,7], "vFences": [8,6,4,2] } assert my_solution.maximizeSquareArea(**test_input) == 64 test_input = { "m": 7, "n": 6, "hFences": [4,5], "vFences": [5,3] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 5, "n": 4, "hFences": [2], "vFences": [3] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 5, "n": 5, "hFences": [3], "vFences": [3,4,2] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 9, "n": 7, "hFences": [3], "vFences": [3,2,5,4] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 8, "n": 6, "hFences": [4], "vFences": [5,2,3] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 6, "n": 9, "hFences": [3,4,2], "vFences": [3,2,8] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 7, "n": 6, "hFences": [3,6,5,2,4], "vFences": [2,3,5,4] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 7, "n": 3, "hFences": [5,3,6,4], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 4, "n": 7, "hFences": [3], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 1 test_input = { "m": 3, "n": 7, "hFences": [2], "vFences": [4,3,2,6,5] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 5, "n": 9, "hFences": [2,4], "vFences": [4,7] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 5, "n": 9, "hFences": [4,2], "vFences": [8,7,3,2,6,4] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 3, "n": 8, "hFences": [2], "vFences": [3,7,2,5,4] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 4, "n": 7, "hFences": [2,3], "vFences": [4,2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 8, "n": 9, "hFences": [7,6,3,2,5,4], "vFences": [3,2,6] } assert my_solution.maximizeSquareArea(**test_input) == 49 test_input = { "m": 7, "n": 8, "hFences": [6], "vFences": [5] } assert my_solution.maximizeSquareArea(**test_input) == -1 test_input = { "m": 9, "n": 7, "hFences": [2,7,8,5], "vFences": [6,3] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 9, "n": 9, "hFences": [4,7,2,5,8,6], "vFences": [7,6,4,5,8,2,3] } assert my_solution.maximizeSquareArea(**test_input) == 64 test_input = { "m": 7, "n": 7, "hFences": [6,3,2,5], "vFences": [6,3] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 7, "n": 7, "hFences": [5], "vFences": [2,3,6] } assert my_solution.maximizeSquareArea(**test_input) == 36 test_input = { "m": 4, "n": 9, "hFences": [3], "vFences": [4,3,7,6,8,5,2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 4, "n": 5, "hFences": [2,3], "vFences": [3,4] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 3, "n": 7, "hFences": [2], "vFences": [3,6] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 4, "n": 9, "hFences": [3,2], "vFences": [4,6] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 9, "n": 9, "hFences": [5], "vFences": [6,5,2,7,3,8] } assert my_solution.maximizeSquareArea(**test_input) == 64 test_input = { "m": 5, "n": 6, "hFences": [3,4,2], "vFences": [5,3,2] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 5, "n": 6, "hFences": [3,2], "vFences": [5] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 5, "n": 5, "hFences": [3,4], "vFences": [3,2,4] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 9, "n": 5, "hFences": [7,5,8], "vFences": [4,2,3] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 3, "n": 6, "hFences": [2], "vFences": [2,5,4] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 5, "n": 9, "hFences": [4,2,3], "vFences": [8,3,5,6,4,2] } assert my_solution.maximizeSquareArea(**test_input) == 16 test_input = { "m": 4, "n": 7, "hFences": [3,2], "vFences": [5] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 6, "n": 9, "hFences": [4,2], "vFences": [3,4,7,2] } assert my_solution.maximizeSquareArea(**test_input) == 25 test_input = { "m": 6, "n": 5, "hFences": [4], "vFences": [3,2] } assert my_solution.maximizeSquareArea(**test_input) == 9 test_input = { "m": 8, "n": 5, "hFences": [6,7,4,5,2], "vFences": [4] } assert my_solution.maximizeSquareArea(**test_input) == 16
1,703,385,000
weekly-contest-377-minimum-cost-to-convert-string-i
https://leetcode.com/problems/minimum-cost-to-convert-string-i
minimum-cost-to-convert-string-i
{ "questionId": "3235", "questionFrontendId": "2976", "title": "Minimum Cost to Convert String I", "titleSlug": "minimum-cost-to-convert-string-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 93, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符 original[i] 更改为字符 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y 。你就可以选择字符串中的一个字符 x 并以 z 的成本将其更改为字符 y 。 返回将字符串 source 转换为字符串 target 所需的 最小 成本。如果不可能完成转换,则返回 -1 。 注意,可能存在下标 i 、j 使得 original[j] == original[i] 且 changed[j] == changed[i] 。 示例 1: 输入:source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20] 输出:28 解释:将字符串 "abcd" 转换为字符串 "acbe" : - 更改下标 1 处的值 'b' 为 'c' ,成本为 5 。 - 更改下标 2 处的值 'c' 为 'e' ,成本为 1 。 - 更改下标 2 处的值 'e' 为 'b' ,成本为 2 。 - 更改下标 3 处的值 'd' 为 'e' ,成本为 20 。 产生的总成本是 5 + 1 + 2 + 20 = 28 。 可以证明这是可能的最小成本。 示例 2: 输入:source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2] 输出:12 解释:要将字符 'a' 更改为 'b': - 将字符 'a' 更改为 'c',成本为 1 - 将字符 'c' 更改为 'b',成本为 2 产生的总成本是 1 + 2 = 3。 将所有 'a' 更改为 'b',产生的总成本是 3 * 4 = 12 。 示例 3: 输入:source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000] 输出:-1 解释:无法将 source 字符串转换为 target 字符串,因为下标 3 处的值无法从 'd' 更改为 'e' 。 提示: 1 <= source.length == target.length <= 105 source、target 均由小写英文字母组成 1 <= cost.length== original.length == changed.length <= 2000 original[i]、changed[i] 是小写英文字母 1 <= cost[i] <= 106 original[i] != changed[i] """ class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符 original[i] 更改为字符 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y 。你就可以选择字符串中的一个字符 x 并以 z 的成本将其更改为字符 y 。 返回将字符串 source 转换为字符串 target 所需的 最小 成本。如果不可能完成转换,则返回 -1 。 注意,可能存在下标 i 、j 使得 original[j] == original[i] 且 changed[j] == changed[i] 。 示例 1: 输入:source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20] 输出:28 解释:将字符串 "abcd" 转换为字符串 "acbe" : - 更改下标 1 处的值 'b' 为 'c' ,成本为 5 。 - 更改下标 2 处的值 'c' 为 'e' ,成本为 1 。 - 更改下标 2 处的值 'e' 为 'b' ,成本为 2 。 - 更改下标 3 处的值 'd' 为 'e' ,成本为 20 。 产生的总成本是 5 + 1 + 2 + 20 = 28 。 可以证明这是可能的最小成本。 示例 2: 输入:source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2] 输出:12 解释:要将字符 'a' 更改为 'b': - 将字符 'a' 更改为 'c',成本为 1 - 将字符 'c' 更改为 'b',成本为 2 产生的总成本是 1 + 2 = 3。 将所有 'a' 更改为 'b',产生的总成本是 3 * 4 = 12 。 示例 3: 输入:source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000] 输出:-1 解释:无法将 source 字符串转换为 target 字符串,因为下标 3 处的值无法从 'd' 更改为 'e' 。 提示: 1 <= source.length == target.length <= 105 source、target 均由小写英文字母组成 1 <= cost.length== original.length == changed.length <= 2000 original[i]、changed[i] 是小写英文字母 1 <= cost[i] <= 106 original[i] != changed[i] 请完成下面的代码来解决上述问题: ```python class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: ```
my_solution = Solution() test_input = { "source": "abcd", "target": "acbe", "original": ["a","b","c","c","e","d"], "changed": ["b","c","b","e","b","e"], "cost": [2,5,5,1,2,20] } assert my_solution.minimumCost(**test_input) == 28 test_input = { "source": "aaaa", "target": "bbbb", "original": ["a","c"], "changed": ["c","b"], "cost": [1,2] } assert my_solution.minimumCost(**test_input) == 12 test_input = { "source": "abcd", "target": "abce", "original": ["a"], "changed": ["e"], "cost": [10000] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "aaaabadaaa", "target": "dbdadddbad", "original": ["c","a","c","a","a","b","b","b","d","d","c"], "changed": ["a","c","b","d","b","c","a","d","c","b","d"], "cost": [7,8,11,9,7,6,4,6,9,5,9] } assert my_solution.minimumCost(**test_input) == 56 test_input = { "source": "aaadbdcdac", "target": "cdbabaddba", "original": ["a","c","b","d","b","a","c"], "changed": ["c","a","d","b","c","b","d"], "cost": [7,2,1,3,6,1,7] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "source": "aababdaacb", "target": "bcdcdcbdcb", "original": ["a","d","d","a","c","b","c","a","c","d","b","b"], "changed": ["b","c","b","d","a","a","b","c","d","a","c","d"], "cost": [11,4,3,2,7,11,7,6,9,2,1,7] } assert my_solution.minimumCost(**test_input) == 42 test_input = { "source": "aababdbddc", "target": "adcbbbcdba", "original": ["a","d","b","a","d","c","d","b"], "changed": ["b","a","d","c","c","a","b","a"], "cost": [10,6,8,3,6,10,8,6] } assert my_solution.minimumCost(**test_input) == 72 test_input = { "source": "aabbcabbdb", "target": "acddbabbdd", "original": ["c","d","c","a","d","c","a","d","b","a","b"], "changed": ["d","b","a","c","c","b","b","a","d","d","c"], "cost": [5,3,8,10,9,7,8,7,5,1,10] } assert my_solution.minimumCost(**test_input) == 32 test_input = { "source": "aabbddccbc", "target": "abbbaabaca", "original": ["a","b","c","b","a","d"], "changed": ["d","c","b","d","b","b"], "cost": [3,8,7,6,7,10] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "aabdbaabaa", "target": "bdaacabcab", "original": ["b","d","d","a","c","c","a","d","a","b"], "changed": ["c","c","b","d","b","d","b","a","c","a"], "cost": [9,1,7,9,2,1,3,8,8,2] } assert my_solution.minimumCost(**test_input) == 43 test_input = { "source": "aacacaaccd", "target": "dadaacaabd", "original": ["c","c","a","a","d","b","d","d"], "changed": ["b","d","d","b","b","c","c","a"], "cost": [7,8,9,11,4,6,9,10] } assert my_solution.minimumCost(**test_input) == 77 test_input = { "source": "aacbabbacc", "target": "adbdbcbdaa", "original": ["c","b","a","b","a","c","d","c","d"], "changed": ["b","c","b","d","d","a","b","d","c"], "cost": [2,6,7,4,7,4,3,5,6] } assert my_solution.minimumCost(**test_input) == 41 test_input = { "source": "aacbbabdad", "target": "ddadcababd", "original": ["d","b","c","a","b","c","d","c","b","a","a"], "changed": ["c","d","d","b","c","b","b","a","a","c","d"], "cost": [7,10,4,2,7,4,4,4,6,2,8] } assert my_solution.minimumCost(**test_input) == 45 test_input = { "source": "aacbbbbcab", "target": "cdacdcddac", "original": ["b","d","c","c","b","a"], "changed": ["c","c","b","a","a","d"], "cost": [4,7,9,11,3,4] } assert my_solution.minimumCost(**test_input) == 67 test_input = { "source": "aacbcabcad", "target": "bbcadddcdd", "original": ["b","a","d","a","b","c","a","d","d","b"], "changed": ["d","b","b","d","c","a","c","c","a","a"], "cost": [7,7,9,8,6,3,8,2,1,5] } assert my_solution.minimumCost(**test_input) == 53 test_input = { "source": "aacbdbcdca", "target": "bbbdbcaacd", "original": ["a","c","b","d","d","a","c","d"], "changed": ["c","b","c","c","b","d","d","a"], "cost": [9,5,4,1,2,4,7,1] } assert my_solution.minimumCost(**test_input) == 47 test_input = { "source": "aadbbcdbbd", "target": "badddbdbac", "original": ["c","d","c","d","b","a"], "changed": ["b","b","a","a","a","d"], "cost": [11,4,7,8,5,2] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "aadbccbddd", "target": "cacdbabadc", "original": ["d","b","c","d","a","a","c","b"], "changed": ["c","c","b","b","b","d","a","a"], "cost": [5,8,7,2,4,7,1,5] } assert my_solution.minimumCost(**test_input) == 46 test_input = { "source": "aadbddcabd", "target": "bdcdccbada", "original": ["d","a","a","b","d","b"], "changed": ["b","c","d","c","a","d"], "cost": [6,10,5,8,11,4] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "aaddadccad", "target": "cbaaadbcba", "original": ["c","a","a","d","c","c","b","b","a","d"], "changed": ["a","c","d","c","d","b","d","c","b","b"], "cost": [1,10,2,8,9,1,9,10,5,1] } assert my_solution.minimumCost(**test_input) == 44 test_input = { "source": "aaddadcdba", "target": "caaaccbbca", "original": ["b","b","c","d","b","c","a","a"], "changed": ["a","d","d","a","c","b","c","b"], "cost": [11,7,10,8,7,5,10,10] } assert my_solution.minimumCost(**test_input) == 84 test_input = { "source": "abaacbbcaa", "target": "bdbdbcbdcd", "original": ["d","a","d","a","b","b"], "changed": ["a","d","b","b","a","c"], "cost": [10,9,8,11,4,11] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "abacaadcba", "target": "cadbadcdbd", "original": ["b","d","c","a","b","d","b"], "changed": ["a","b","b","b","c","a","d"], "cost": [9,10,6,2,7,10,9] } assert my_solution.minimumCost(**test_input) == 89 test_input = { "source": "abacbadadc", "target": "aabbdaaccb", "original": ["d","a","b","d","a","a","c","b","c","c","d","b"], "changed": ["c","b","a","a","d","c","b","c","d","a","b","d"], "cost": [2,10,3,6,4,1,5,5,11,4,2,1] } assert my_solution.minimumCost(**test_input) == 28 test_input = { "source": "abadbbabcd", "target": "cdcbdddcbb", "original": ["d","d","b","a","c","c","c","a"], "changed": ["c","a","a","c","b","d","a","d"], "cost": [2,10,11,7,6,11,7,1] } assert my_solution.minimumCost(**test_input) == 79 test_input = { "source": "abadcadacc", "target": "cbabaddcba", "original": ["a","d","a","b","c","a","d","b","b","d","c","c"], "changed": ["b","b","d","d","a","c","a","c","a","c","d","b"], "cost": [7,6,11,11,8,10,4,11,2,3,11,7] } assert my_solution.minimumCost(**test_input) == 60 test_input = { "source": "abadcdadac", "target": "baddbccdac", "original": ["d","c","d","c","b","a"], "changed": ["b","b","c","a","d","d"], "cost": [8,5,9,1,10,2] } assert my_solution.minimumCost(**test_input) == 57 test_input = { "source": "abbaadacba", "target": "cdbbcadddd", "original": ["d","a","d","c","b","b","c","d","c","a","a"], "changed": ["a","c","c","d","a","d","a","b","b","d","b"], "cost": [8,3,5,8,3,9,3,4,11,4,9] } assert my_solution.minimumCost(**test_input) == 50 test_input = { "source": "abbaddaacd", "target": "ccbbaccacc", "original": ["d","d","a","b","c","b"], "changed": ["a","c","c","d","b","c"], "cost": [9,8,2,8,3,1] } assert my_solution.minimumCost(**test_input) == 35 test_input = { "source": "abbbcabddb", "target": "bbccdbbadc", "original": ["c","d","c","a","b","d","d","a","b","b"], "changed": ["d","a","b","c","c","b","c","d","a","d"], "cost": [3,6,9,4,9,6,9,6,10,7] } assert my_solution.minimumCost(**test_input) == 60 test_input = { "source": "abbbcbabab", "target": "abcacbaddd", "original": ["b","c","a","c","a","d","d","c"], "changed": ["a","b","d","a","b","b","c","d"], "cost": [11,5,8,1,7,7,1,2] } assert my_solution.minimumCost(**test_input) == 77 test_input = { "source": "abbcaccabb", "target": "ddddddcacc", "original": ["a","b","c","b","a","c","c"], "changed": ["c","c","d","a","d","a","b"], "cost": [2,6,10,11,9,7,3] } assert my_solution.minimumCost(**test_input) == 82 test_input = { "source": "abbcaccdba", "target": "accadababc", "original": ["d","a","a","c","b","d"], "changed": ["c","d","b","a","a","b"], "cost": [7,4,10,11,5,5] } assert my_solution.minimumCost(**test_input) == 99 test_input = { "source": "abbdaccada", "target": "acddaccddc", "original": ["b","b","c","a","d","a","d"], "changed": ["a","c","b","b","b","c","c"], "cost": [4,9,3,1,11,3,3] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "abcabdaddb", "target": "dcbadaaacc", "original": ["d","b","a","a","c","c"], "changed": ["c","c","d","b","b","a"], "cost": [3,3,9,3,7,6] } assert my_solution.minimumCost(**test_input) == 61 test_input = { "source": "abcadcabaa", "target": "bbbdddcaba", "original": ["b","d","c","d","a","c","b","b"], "changed": ["c","b","a","a","c","b","d","a"], "cost": [1,8,4,3,8,3,11,5] } assert my_solution.minimumCost(**test_input) == 74 test_input = { "source": "abccabacaa", "target": "aaabacbcbb", "original": ["c","c","d","a","d","b","c","b","d","a"], "changed": ["b","a","a","b","b","d","d","a","c","c"], "cost": [9,10,8,6,9,10,2,6,6,8] } assert my_solution.minimumCost(**test_input) == 57 test_input = { "source": "abdaababbb", "target": "dbdadabadc", "original": ["a","c","c","b","d","a","b"], "changed": ["c","a","b","c","b","b","d"], "cost": [3,4,6,1,8,11,6] } assert my_solution.minimumCost(**test_input) == 56 test_input = { "source": "abdbaaacaa", "target": "abbbccccad", "original": ["a","a","c","b","d","d","b"], "changed": ["d","b","b","a","a","b","c"], "cost": [3,10,7,2,5,7,3] } assert my_solution.minimumCost(**test_input) == 49 test_input = { "source": "abdcbdbccc", "target": "dbbcdcabba", "original": ["c","c","d","b","a","c","a","d","b","d","a","b"], "changed": ["d","a","b","d","c","b","b","a","c","c","d","a"], "cost": [9,5,9,6,5,5,5,10,7,7,3,6] } assert my_solution.minimumCost(**test_input) == 46 test_input = { "source": "acabbbdbdb", "target": "accbccbbab", "original": ["b","d","c","d","b","c"], "changed": ["a","a","a","c","d","d"], "cost": [7,7,10,9,7,1] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "acadadccbb", "target": "dcaaabbbdd", "original": ["a","c","c","a","d","b","b","d","b","a"], "changed": ["d","d","b","b","c","d","a","a","c","c"], "cost": [1,2,1,4,9,4,8,5,11,7] } assert my_solution.minimumCost(**test_input) == 25 test_input = { "source": "acadbbcdcb", "target": "bcacabdcdd", "original": ["a","b","d","b","b","c","a"], "changed": ["d","c","c","a","d","b","b"], "cost": [3,6,3,10,11,3,6] } assert my_solution.minimumCost(**test_input) == 61 test_input = { "source": "acaddccaad", "target": "daacadcdda", "original": ["c","c","a","b","b","a","b","d","c","a","d"], "changed": ["a","b","b","d","a","d","c","b","d","c","c"], "cost": [10,8,4,8,3,1,2,8,11,8,6] } assert my_solution.minimumCost(**test_input) == 52 test_input = { "source": "acbbabcaac", "target": "bdcbaadcab", "original": ["d","c","a","c","b","a","a","b","c","d"], "changed": ["a","a","d","d","c","c","b","a","b","c"], "cost": [9,11,8,6,11,11,1,1,9,9] } assert my_solution.minimumCost(**test_input) == 45 test_input = { "source": "accabbadbc", "target": "adbbccbcbd", "original": ["a","c","a","d","b","a","c","c","b","b"], "changed": ["c","d","b","c","d","d","b","a","c","a"], "cost": [7,10,9,3,2,5,1,8,11,2] } assert my_solution.minimumCost(**test_input) == 36 test_input = { "source": "accabbdddd", "target": "cacdccbcad", "original": ["c","a","d","b","d","c","a","b","b","c","d"], "changed": ["d","b","a","d","c","a","d","a","c","b","b"], "cost": [11,6,6,4,7,11,2,7,7,7,2] } assert my_solution.minimumCost(**test_input) == 51 test_input = { "source": "accbaadbdb", "target": "baccbaacbb", "original": ["b","b","a","d","d","a","c"], "changed": ["a","d","d","b","a","c","a"], "cost": [9,11,6,7,4,2,2] } assert my_solution.minimumCost(**test_input) == 61 test_input = { "source": "accbddaaab", "target": "baddbaabbd", "original": ["a","b","c","d","d","b","d","b"], "changed": ["b","a","a","b","a","c","c","d"], "cost": [6,3,4,6,1,6,10,6] } assert my_solution.minimumCost(**test_input) == 57 test_input = { "source": "acccbcdccb", "target": "bdadccdbad", "original": ["a","b","c","c","a","d","d"], "changed": ["b","c","a","d","c","a","c"], "cost": [8,1,1,9,3,10,4] } assert my_solution.minimumCost(**test_input) == 48 test_input = { "source": "accccbccda", "target": "daadbbcaac", "original": ["a","c","a","a","d","d"], "changed": ["c","d","d","b","b","c"], "cost": [3,6,6,10,9,8] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "acdacbdadb", "target": "aacccbbacd", "original": ["b","b","a","a","d","c"], "changed": ["d","a","b","d","a","a"], "cost": [6,1,9,6,8,11] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "acdbcdadbd", "target": "daaadaaadd", "original": ["c","a","b","b","b","d","a"], "changed": ["d","c","d","c","a","b","d"], "cost": [3,9,4,6,1,9,3] } assert my_solution.minimumCost(**test_input) == 54 test_input = { "source": "acddadcbca", "target": "ddaabaaaac", "original": ["b","d","c","a","b","d","c","b","d","c","a"], "changed": ["c","a","b","c","d","c","a","a","b","d","d"], "cost": [7,8,2,10,1,7,8,1,1,11,4] } assert my_solution.minimumCost(**test_input) == 35 test_input = { "source": "adaadcaddd", "target": "cdddbdccad", "original": ["c","c","c","d","b","a","d"], "changed": ["d","a","b","b","d","b","c"], "cost": [10,9,2,2,7,1,10] } assert my_solution.minimumCost(**test_input) == 92 test_input = { "source": "adaaddacba", "target": "aabbddbbdd", "original": ["c","b","a","b","c","b"], "changed": ["b","c","b","a","d","d"], "cost": [10,7,7,6,8,5] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "adacdcdacd", "target": "ccbabbbbdc", "original": ["a","b","c","a","d","b","a","c"], "changed": ["c","d","d","d","a","c","b","a"], "cost": [4,3,1,1,6,4,10,6] } assert my_solution.minimumCost(**test_input) == 99 test_input = { "source": "adadbabcdd", "target": "abbcdcbdba", "original": ["c","d","b","a","c","b","a"], "changed": ["d","b","d","b","b","a","c"], "cost": [11,10,6,1,5,3,8] } assert my_solution.minimumCost(**test_input) == 80 test_input = { "source": "adadcabbda", "target": "cabadddccc", "original": ["c","a","b","b","a","d","d"], "changed": ["b","d","d","c","b","c","b"], "cost": [7,2,8,4,4,4,7] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "adbaabacdc", "target": "bccbbadcdc", "original": ["c","b","b","d","c","a","b"], "changed": ["b","a","c","a","a","d","d"], "cost": [5,2,6,1,7,7,1] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "adbadbaacb", "target": "bccdbdccab", "original": ["d","a","c","b","c","d","a","b","c","b","a","d"], "changed": ["a","c","b","c","a","b","b","d","d","a","d","c"], "cost": [3,7,7,9,2,9,10,2,9,5,11,8] } assert my_solution.minimumCost(**test_input) == 65 test_input = { "source": "adbcdaddda", "target": "cbdccabcbc", "original": ["c","a","d","d","b","b","b"], "changed": ["a","c","c","a","d","c","a"], "cost": [8,5,5,10,10,3,9] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "adcacaaabb", "target": "daaadadcbb", "original": ["d","b","a","a","d","c","b","b","d","c","c","a"], "changed": ["b","c","d","c","c","a","a","d","a","b","d","b"], "cost": [2,3,2,4,5,9,11,3,10,1,9,2] } assert my_solution.minimumCost(**test_input) == 31 test_input = { "source": "adcbbbdada", "target": "cdaabadcdc", "original": ["a","a","b","c","d","b","d"], "changed": ["c","d","c","d","a","a","c"], "cost": [11,11,2,8,5,7,5] } assert my_solution.minimumCost(**test_input) == 60 test_input = { "source": "adccbabbca", "target": "dcdbbdabba", "original": ["a","d","d","b","c","b","a"], "changed": ["c","b","c","c","a","d","d"], "cost": [5,10,10,1,6,7,7] } assert my_solution.minimumCost(**test_input) == 90 test_input = { "source": "adcdcbacab", "target": "acddaddadc", "original": ["b","d","c","d","c","d","b","c","a","a","a","b"], "changed": ["a","b","b","c","a","a","d","d","c","b","d","c"], "cost": [2,11,11,9,1,3,6,9,6,4,8,5] } assert my_solution.minimumCost(**test_input) == 47 test_input = { "source": "addbaccbbd", "target": "cabdcdadcc", "original": ["b","d","d","d","c","b","a","c","c"], "changed": ["a","c","a","b","b","d","d","a","d"], "cost": [10,11,5,6,10,1,4,8,8] } assert my_solution.minimumCost(**test_input) == 82 test_input = { "source": "addbacdaac", "target": "abddcadbcb", "original": ["d","a","c","b","a","c","a","d","c","d","b"], "changed": ["c","c","b","d","d","d","b","b","a","a","c"], "cost": [9,2,9,4,11,6,10,3,7,2,5] } assert my_solution.minimumCost(**test_input) == 37 test_input = { "source": "addbcccdcb", "target": "cbbdbddacb", "original": ["d","c","a","a","b","b","c"], "changed": ["c","d","b","d","d","c","b"], "cost": [2,6,4,3,7,7,8] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "addcadccaa", "target": "dbbcaccabc", "original": ["a","b","d","b","d","a","c","b","a","c"], "changed": ["c","d","a","c","b","b","d","a","d","a"], "cost": [8,11,5,1,11,4,3,8,11,4] } assert my_solution.minimumCost(**test_input) == 49 test_input = { "source": "addcdbdadb", "target": "bcabdcccbd", "original": ["b","b","a","c","d","a","b","c","a"], "changed": ["a","c","c","a","a","b","d","b","d"], "cost": [2,4,8,8,3,5,2,7,2] } assert my_solution.minimumCost(**test_input) == 59 test_input = { "source": "adddbbdbdb", "target": "cdbadcaccc", "original": ["b","c","c","b","a","c","b","a","a","d","d"], "changed": ["d","a","b","a","b","d","c","c","d","a","c"], "cost": [1,1,1,8,6,9,3,6,5,3,10] } assert my_solution.minimumCost(**test_input) == 40 test_input = { "source": "adddccacca", "target": "cdcdcccdac", "original": ["d","c","a","d","b","b","b","a","d","a","c","c"], "changed": ["c","a","c","a","d","a","c","b","b","d","d","b"], "cost": [7,7,6,10,1,1,11,5,3,2,10,3] } assert my_solution.minimumCost(**test_input) == 33 test_input = { "source": "baacbbcdaa", "target": "abdbdbbabd", "original": ["c","d","c","b","a","c","b","d","b"], "changed": ["d","c","b","a","d","a","d","a","c"], "cost": [9,5,5,2,9,4,5,3,6] } assert my_solution.minimumCost(**test_input) == 76 test_input = { "source": "baadcdabbc", "target": "acbccadccd", "original": ["b","b","a","a","a","d","d","d","c","c","b"], "changed": ["c","a","c","b","d","c","a","b","b","d","d"], "cost": [8,6,5,10,11,1,1,6,3,1,4] } assert my_solution.minimumCost(**test_input) == 37 test_input = { "source": "baadcdabda", "target": "abdbcdaaca", "original": ["b","b","c","d","d","a","c"], "changed": ["d","a","a","b","c","b","d"], "cost": [11,8,7,3,10,4,1] } assert my_solution.minimumCost(**test_input) == 48 test_input = { "source": "babababdba", "target": "ccdaaabbac", "original": ["c","d","b","a","d","a","b"], "changed": ["b","a","d","c","c","d","c"], "cost": [8,5,6,2,8,6,8] } assert my_solution.minimumCost(**test_input) == 55 test_input = { "source": "babbacabba", "target": "adacccdcba", "original": ["c","d","d","d","c","b","b","b","a"], "changed": ["a","b","c","a","d","a","c","d","c"], "cost": [1,1,8,3,2,2,11,8,3] } assert my_solution.minimumCost(**test_input) == 27 test_input = { "source": "babbadbabc", "target": "ccdabbcbba", "original": ["a","c","d","a","b","d","b","b"], "changed": ["b","d","c","d","a","a","c","d"], "cost": [3,3,1,4,4,8,2,9] } assert my_solution.minimumCost(**test_input) == 46 test_input = { "source": "bacbddaacb", "target": "dcdaaadcda", "original": ["a","d","a","a","d","c","b","c","c","b"], "changed": ["d","c","c","b","b","b","a","a","d","c"], "cost": [8,5,1,10,8,6,2,1,6,8] } assert my_solution.minimumCost(**test_input) == 46 test_input = { "source": "baccbbcdcb", "target": "cabadbbacc", "original": ["c","a","c","b","c","d","a","b","d","b"], "changed": ["a","b","b","a","d","b","c","d","c","c"], "cost": [4,4,2,11,9,9,1,4,6,1] } assert my_solution.minimumCost(**test_input) == 24 test_input = { "source": "bacdbbcdba", "target": "cdcdddbbcd", "original": ["d","a","c","b","d","a","b","c","b","c","d"], "changed": ["c","c","a","d","b","b","a","d","c","b","a"], "cost": [3,8,4,6,5,8,6,2,1,6,2] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "source": "bacdbccabb", "target": "caaccdbaac", "original": ["c","d","d","a","d","c"], "changed": ["d","b","a","b","c","a"], "cost": [8,4,4,4,3,6] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "bacddacdba", "target": "bcbbaacdda", "original": ["b","c","c","d","d","b","b","d","a","a","c","a"], "changed": ["a","b","d","b","a","c","d","c","c","d","a","b"], "cost": [5,6,7,4,4,8,8,6,8,3,6,11] } assert my_solution.minimumCost(**test_input) == 30 test_input = { "source": "badaabbaba", "target": "caadbcadcd", "original": ["b","c","a","c","d","a","b","d","d","b","c"], "changed": ["c","a","d","b","a","c","d","c","b","a","d"], "cost": [4,8,6,3,1,8,3,8,3,6,9] } assert my_solution.minimumCost(**test_input) == 44 test_input = { "source": "badabbbbac", "target": "dacaabbcaa", "original": ["d","b","a","d","c","b","d","c","a","c","a"], "changed": ["b","c","d","c","a","d","a","d","c","b","b"], "cost": [11,5,9,7,11,11,7,9,6,11,5] } assert my_solution.minimumCost(**test_input) == 50 test_input = { "source": "badaccbdbd", "target": "dbbdacaaab", "original": ["b","d","b","c","b","d","a","d","a","a","c","c"], "changed": ["d","a","a","a","c","b","d","c","b","c","d","b"], "cost": [3,9,3,11,11,6,2,11,11,2,11,1] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "source": "badadcccba", "target": "bbdbababcc", "original": ["c","c","a","d","d","d"], "changed": ["a","d","b","a","b","c"], "cost": [1,3,2,2,4,4] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "badbbbccdb", "target": "bbbabbccbd", "original": ["a","c","a","c","d","c","d","b","d","b","a"], "changed": ["c","b","d","d","c","a","b","d","a","c","b"], "cost": [5,4,5,3,4,4,6,3,8,11,4] } assert my_solution.minimumCost(**test_input) == 30 test_input = { "source": "badcbccabc", "target": "bdcaacbcad", "original": ["d","d","c","d","a","b","c","a"], "changed": ["a","c","d","b","b","a","b","d"], "cost": [1,5,4,5,11,10,8,11] } assert my_solution.minimumCost(**test_input) == 69 test_input = { "source": "badcbdddcd", "target": "cdcbaddadc", "original": ["c","b","c","d","a","b","b","d","a","c","a","d"], "changed": ["a","c","b","a","c","d","a","b","d","d","b","c"], "cost": [3,2,8,9,11,5,11,11,9,2,8,1] } assert my_solution.minimumCost(**test_input) == 32 test_input = { "source": "baddbcbdbd", "target": "acdbcadabd", "original": ["a","b","b","c","a","a","b","d","c","d","d"], "changed": ["c","a","d","d","b","d","c","c","b","b","a"], "cost": [9,10,5,4,1,5,7,8,11,9,8] } assert my_solution.minimumCost(**test_input) == 59 test_input = { "source": "baddbdacad", "target": "cadaccbbab", "original": ["b","a","d","a","c","b","d","c","a","d"], "changed": ["d","b","b","d","a","c","a","b","c","c"], "cost": [3,5,6,7,2,4,4,2,3,9] } assert my_solution.minimumCost(**test_input) == 32 test_input = { "source": "bbacdcdcda", "target": "cbadabbdcb", "original": ["a","d","b","c","b","c","d","a","d"], "changed": ["b","c","d","a","a","b","b","d","a"], "cost": [11,6,2,8,5,7,5,8,9] } assert my_solution.minimumCost(**test_input) == 64 test_input = { "source": "bbadbbabbb", "target": "cbaaddaddc", "original": ["d","a","b","c","d","c","b"], "changed": ["b","d","c","b","c","d","d"], "cost": [4,10,1,11,7,1,11] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "bbadcbcbbc", "target": "aaaccbccbb", "original": ["b","d","b","d","a","c","c","a","b"], "changed": ["c","a","a","b","b","a","b","c","d"], "cost": [8,5,9,4,1,10,1,11,4] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "source": "bbbadaccbb", "target": "dadcaccadb", "original": ["a","d","d","d","c","b","c","b","c","b"], "changed": ["d","b","c","a","b","d","d","c","a","a"], "cost": [10,1,4,6,4,1,10,3,1,6] } assert my_solution.minimumCost(**test_input) == 41 test_input = { "source": "bbbadcbadb", "target": "aacbdcddcd", "original": ["a","d","d","c","a","b","c","b"], "changed": ["d","b","a","a","c","c","b","d"], "cost": [3,1,1,9,1,2,1,2] } assert my_solution.minimumCost(**test_input) == 19 test_input = { "source": "bbbbabbcbc", "target": "adacababac", "original": ["a","c","a","b","b","d","d","a","d","b","c"], "changed": ["d","b","c","c","d","a","b","b","c","a","d"], "cost": [7,9,9,7,3,2,6,8,11,5,8] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "source": "bbbbdacbcd", "target": "cbadccdaaa", "original": ["d","a","c","b","d","c","a","d","b","a"], "changed": ["c","c","b","c","a","d","d","b","d","b"], "cost": [2,9,11,3,6,11,6,4,5,5] } assert my_solution.minimumCost(**test_input) == 74
1,703,385,000
weekly-contest-377-minimum-cost-to-convert-string-ii
https://leetcode.com/problems/minimum-cost-to-convert-string-ii
minimum-cost-to-convert-string-ii
{ "questionId": "3238", "questionFrontendId": "2977", "title": "Minimum Cost to Convert String II", "titleSlug": "minimum-cost-to-convert-string-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 40, "dislikes": 36, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符串数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符串 original[i] 更改为字符串 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y ,你就可以选择字符串中的 子串 x 并以 z 的成本将其更改为 y 。 你可以执行 任意数量 的操作,但是任两次操作必须满足 以下两个 条件 之一 : 在两次操作中选择的子串分别是 source[a..b] 和 source[c..d] ,满足 b < c 或 d < a 。换句话说,两次操作中选择的下标 不相交 。 在两次操作中选择的子串分别是 source[a..b] 和 source[c..d] ,满足 a == c 且 b == d 。换句话说,两次操作中选择的下标 相同 。 返回将字符串 source 转换为字符串 target 所需的 最小 成本。如果不可能完成转换,则返回 -1 。 注意,可能存在下标 i 、j 使得 original[j] == original[i] 且 changed[j] == changed[i] 。 示例 1: 输入:source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20] 输出:28 解释:将 "abcd" 转换为 "acbe",执行以下操作: - 将子串 source[1..1] 从 "b" 改为 "c" ,成本为 5 。 - 将子串 source[2..2] 从 "c" 改为 "e" ,成本为 1 。 - 将子串 source[2..2] 从 "e" 改为 "b" ,成本为 2 。 - 将子串 source[3..3] 从 "d" 改为 "e" ,成本为 20 。 产生的总成本是 5 + 1 + 2 + 20 = 28 。 可以证明这是可能的最小成本。 示例 2: 输入:source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5] 输出:9 解释:将 "abcdefgh" 转换为 "acdeeghh",执行以下操作: - 将子串 source[1..3] 从 "bcd" 改为 "cde" ,成本为 1 。 - 将子串 source[5..7] 从 "fgh" 改为 "thh" ,成本为 3 。可以执行此操作,因为下标 [5,7] 与第一次操作选中的下标不相交。 - 将子串 source[5..7] 从 "thh" 改为 "ghh" ,成本为 5 。可以执行此操作,因为下标 [5,7] 与第一次操作选中的下标不相交,且与第二次操作选中的下标相同。 产生的总成本是 1 + 3 + 5 = 9 。 可以证明这是可能的最小成本。 示例 3: 输入:source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578] 输出:-1 解释:无法将 "abcdefgh" 转换为 "addddddd" 。 如果选择子串 source[1..3] 执行第一次操作,以将 "abcdefgh" 改为 "adddefgh" ,你无法选择子串 source[3..7] 执行第二次操作,因为两次操作有一个共用下标 3 。 如果选择子串 source[3..7] 执行第一次操作,以将 "abcdefgh" 改为 "abcddddd" ,你无法选择子串 source[1..3] 执行第二次操作,因为两次操作有一个共用下标 3 。 提示: 1 <= source.length == target.length <= 1000 source、target 均由小写英文字母组成 1 <= cost.length == original.length == changed.length <= 100 1 <= original[i].length == changed[i].length <= source.length original[i]、changed[i] 均由小写英文字母组成 original[i] != changed[i] 1 <= cost[i] <= 106 """ class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int:
给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符串数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符串 original[i] 更改为字符串 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y ,你就可以选择字符串中的 子串 x 并以 z 的成本将其更改为 y 。 你可以执行 任意数量 的操作,但是任两次操作必须满足 以下两个 条件 之一 : 在两次操作中选择的子串分别是 source[a..b] 和 source[c..d] ,满足 b < c 或 d < a 。换句话说,两次操作中选择的下标 不相交 。 在两次操作中选择的子串分别是 source[a..b] 和 source[c..d] ,满足 a == c 且 b == d 。换句话说,两次操作中选择的下标 相同 。 返回将字符串 source 转换为字符串 target 所需的 最小 成本。如果不可能完成转换,则返回 -1 。 注意,可能存在下标 i 、j 使得 original[j] == original[i] 且 changed[j] == changed[i] 。 示例 1: 输入:source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20] 输出:28 解释:将 "abcd" 转换为 "acbe",执行以下操作: - 将子串 source[1..1] 从 "b" 改为 "c" ,成本为 5 。 - 将子串 source[2..2] 从 "c" 改为 "e" ,成本为 1 。 - 将子串 source[2..2] 从 "e" 改为 "b" ,成本为 2 。 - 将子串 source[3..3] 从 "d" 改为 "e" ,成本为 20 。 产生的总成本是 5 + 1 + 2 + 20 = 28 。 可以证明这是可能的最小成本。 示例 2: 输入:source = "abcdefgh", target = "acdeeghh", original = ["bcd","fgh","thh"], changed = ["cde","thh","ghh"], cost = [1,3,5] 输出:9 解释:将 "abcdefgh" 转换为 "acdeeghh",执行以下操作: - 将子串 source[1..3] 从 "bcd" 改为 "cde" ,成本为 1 。 - 将子串 source[5..7] 从 "fgh" 改为 "thh" ,成本为 3 。可以执行此操作,因为下标 [5,7] 与第一次操作选中的下标不相交。 - 将子串 source[5..7] 从 "thh" 改为 "ghh" ,成本为 5 。可以执行此操作,因为下标 [5,7] 与第一次操作选中的下标不相交,且与第二次操作选中的下标相同。 产生的总成本是 1 + 3 + 5 = 9 。 可以证明这是可能的最小成本。 示例 3: 输入:source = "abcdefgh", target = "addddddd", original = ["bcd","defgh"], changed = ["ddd","ddddd"], cost = [100,1578] 输出:-1 解释:无法将 "abcdefgh" 转换为 "addddddd" 。 如果选择子串 source[1..3] 执行第一次操作,以将 "abcdefgh" 改为 "adddefgh" ,你无法选择子串 source[3..7] 执行第二次操作,因为两次操作有一个共用下标 3 。 如果选择子串 source[3..7] 执行第一次操作,以将 "abcdefgh" 改为 "abcddddd" ,你无法选择子串 source[1..3] 执行第二次操作,因为两次操作有一个共用下标 3 。 提示: 1 <= source.length == target.length <= 1000 source、target 均由小写英文字母组成 1 <= cost.length == original.length == changed.length <= 100 1 <= original[i].length == changed[i].length <= source.length original[i]、changed[i] 均由小写英文字母组成 original[i] != changed[i] 1 <= cost[i] <= 106 请完成下面的代码来解决上述问题: ```python class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: ```
my_solution = Solution() test_input = { "source": "abcd", "target": "acbe", "original": ["a","b","c","c","e","d"], "changed": ["b","c","b","e","b","e"], "cost": [2,5,5,1,2,20] } assert my_solution.minimumCost(**test_input) == 28 test_input = { "source": "abcdefgh", "target": "acdeeghh", "original": ["bcd","fgh","thh"], "changed": ["cde","thh","ghh"], "cost": [1,3,5] } assert my_solution.minimumCost(**test_input) == 9 test_input = { "source": "abcdefgh", "target": "addddddd", "original": ["bcd","defgh"], "changed": ["ddd","ddddd"], "cost": [100,1578] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "a", "target": "b", "original": ["a"], "changed": ["b"], "cost": [1] } assert my_solution.minimumCost(**test_input) == 1 test_input = { "source": "a", "target": "c", "original": ["a","b","a","a"], "changed": ["b","c","c","c"], "cost": [1,2,10,1] } assert my_solution.minimumCost(**test_input) == 1 test_input = { "source": "a", "target": "d", "original": ["a"], "changed": ["b"], "cost": [1] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ajhpd", "target": "djjdc", "original": ["hpd","iyk","qzd","hpi","aic","znh","cea","fug","wir","kwu","yjo","rzi","a","n","f","q","u","w","x","i","x","s","o","u"], "changed": ["iyk","qzd","hpi","aic","znh","cea","fug","wir","kwu","yjo","rzi","jdc","n","f","q","u","w","x","i","x","s","o","u","d"], "cost": [80257,95140,96349,89449,81714,5859,96734,96109,41211,99975,57611,32644,82896,22164,99889,98061,95403,90922,64031,94558,58418,99717,96588,88286] } assert my_solution.minimumCost(**test_input) == 1264348 test_input = { "source": "bzshh", "target": "mlosr", "original": ["shh","wbs","hup","sab","csp","tel","mhq","ezp","eap","fqb","iea","cej","b","v","g","e","d","x","q","v","g","x","u","m","u","q","z","q","n","p"], "changed": ["wbs","hup","sab","csp","tel","mhq","ezp","eap","fqb","iea","cej","osr","v","g","e","d","x","q","v","g","x","u","m","u","q","m","q","n","p","l"], "cost": [69565,82190,75322,85502,89675,98424,86521,85852,32285,99465,82356,97775,30173,88276,82158,40971,75361,65284,89814,68219,44777,95082,99781,99072,74513,49667,99719,93132,99203,54171] } assert my_solution.minimumCost(**test_input) == 1589277 test_input = { "source": "fjybg", "target": "apyyt", "original": ["bg","xr","cc","ip","vq","po","ym","rh","vw","lf","lo","ee","qv","yr","f","w","i","u","g","a","e","f","s","r","p","j","o","g","i","u"], "changed": ["xr","cc","ip","vq","po","ym","rh","vw","lf","lo","ee","qv","yr","yt","w","i","u","g","a","e","f","s","r","p","a","o","g","i","u","p"], "cost": [97733,90086,87125,85361,75644,46301,21616,79538,52507,95884,79353,61127,58665,96031,95035,12116,41158,91096,47819,88522,25493,80186,66981,87597,56691,86820,89031,99954,41271,39699] } assert my_solution.minimumCost(**test_input) == 1628332 test_input = { "source": "htkdz", "target": "oaqaw", "original": ["kdz","yyv","cde","oks","fzu","hkm","dmb","arh","lix","eij","ksv","t","u","f","w","b","u","v","h","o","b","o","p","z","h","w","t","p","x","y"], "changed": ["yyv","cde","oks","fzu","hkm","dmb","arh","lix","eij","ksv","qaw","u","f","w","b","u","v","h","o","b","o","p","z","a","w","t","p","x","y","o"], "cost": [90243,86765,84893,80924,85915,42672,99995,99429,88069,84925,71184,54929,83245,72750,87238,30151,58657,94445,98330,90683,83980,96513,75536,95212,79301,74556,94836,94781,76273,86147] } assert my_solution.minimumCost(**test_input) == 1278928 test_input = { "source": "iktgh", "target": "srwcg", "original": ["h","e","y","g","q","y","t","n","r","e","i","x","iktg","xwgv","ddrp","saxt","rvdq","moiy","loln","bkgj","jjgi","vatf"], "changed": ["e","y","g","q","y","t","n","r","e","i","x","g","xwgv","ddrp","saxt","rvdq","moiy","loln","bkgj","jjgi","vatf","srwc"], "cost": [70839,75691,55903,82637,97906,86576,92197,74464,86638,61531,80041,52732,96361,39766,74988,59857,69068,89990,74293,82838,37650,26885] } assert my_solution.minimumCost(**test_input) == 854129 test_input = { "source": "imbin", "target": "dmhjv", "original": ["bin","pwo","fwt","xwi","xal","uqt","lmp","erq","kac","dgv","qgh","rei","nbx","i","u","b","v","c","q","p","f","q","v","t","n","b"], "changed": ["pwo","fwt","xwi","xal","uqt","lmp","erq","kac","dgv","qgh","rei","nbx","hjv","u","b","v","c","q","p","f","q","v","t","n","b","d"], "cost": [47307,30907,64949,35735,84284,83424,69858,92113,51405,69242,97014,91471,78165,92733,79709,99573,78055,20529,85549,90496,60896,75354,50630,49094,41380,46980] } assert my_solution.minimumCost(**test_input) == 1115296 test_input = { "source": "jegbx", "target": "ezhfc", "original": ["egbx","hrbf","twne","snjd","ysrf","qzqg","rcll","ekvz","inpr","frxs","xcww","unsw","vdug","ycvs","j","v","j","y","n","q","w","a","z","g","b","d"], "changed": ["hrbf","twne","snjd","ysrf","qzqg","rcll","ekvz","inpr","frxs","xcww","unsw","vdug","ycvs","zhfc","v","j","y","n","q","w","a","z","g","b","d","e"], "cost": [50682,89150,91153,85032,97960,96862,81138,86570,77628,45200,44955,70845,99254,80325,91331,95349,84374,94177,53994,94284,79531,92353,60384,100000,93152,19787] } assert my_solution.minimumCost(**test_input) == 1868790 test_input = { "source": "jpyjj", "target": "jqnfp", "original": ["j","i","q","u","y","w","d","a","h","s","i","y","w","pyj","qng","lrn","nrm","tvn","fei","fpj","qlw","lrb","ufu","kll","nqp"], "changed": ["i","q","u","y","w","d","a","h","s","i","y","w","p","qng","lrn","nrm","tvn","fei","fpj","qlw","lrb","ufu","kll","nqp","qnf"], "cost": [62657,90954,55348,88767,87756,55487,49700,51801,94877,81661,99027,91814,62872,25235,62153,96875,12009,85321,68993,75866,72888,96411,78568,83975,60456] } assert my_solution.minimumCost(**test_input) == 1131062 test_input = { "source": "nialx", "target": "qvqfl", "original": ["x","r","a","x","c","w","s","a","n","e","q","p","v","k","o","ial","qzu","owr","kyq","ukk","gpq","jdp","dus","eng","btu","cbp"], "changed": ["r","a","x","c","w","s","a","l","e","q","p","v","k","o","q","qzu","owr","kyq","ukk","gpq","jdp","dus","eng","btu","cbp","vqf"], "cost": [64196,95812,96987,40860,41507,99365,99208,53062,44440,65136,95625,86166,61798,84228,92555,97678,97576,19742,92989,98167,68457,82411,39923,81778,87792,7523] } assert my_solution.minimumCost(**test_input) == 1096682 test_input = { "source": "pagpe", "target": "xacng", "original": ["gpe","owt","wyv","eba","xgp","uny","ibc","usb","mzj","wdo","lyc","eof","oci","p","e","p","u","h","w","i","l"], "changed": ["owt","wyv","eba","xgp","uny","ibc","usb","mzj","wdo","lyc","eof","oci","cng","e","p","u","h","w","i","l","x"], "cost": [56193,92982,90717,67407,91949,77752,88841,43278,51149,43646,99585,41038,84989,57688,64474,96532,77511,37031,90895,62831,87342] } assert my_solution.minimumCost(**test_input) == 1381668 test_input = { "source": "aaabbebbbhbbbbebaaeh", "target": "hhbebebbahhhehhbbhee", "original": ["a","b","b","b","e","a","h"], "changed": ["b","e","a","h","h","h","e"], "cost": [9,8,5,9,3,7,9] } assert my_solution.minimumCost(**test_input) == 99 test_input = { "source": "abbbeebebehbbhhhbeab", "target": "aehebehebaeaebbaahhb", "original": ["b","b","e","e","h","h","h","b","e","a"], "changed": ["e","h","b","a","e","b","a","a","h","h"], "cost": [10,2,9,10,7,8,10,10,6,9] } assert my_solution.minimumCost(**test_input) == 118 test_input = { "source": "abebbeeeahhbahaehaab", "target": "eebhheeahaahbaebaaea", "original": ["a","b","e","a","h","a","e","b"], "changed": ["e","h","a","h","a","b","b","a"], "cost": [6,8,5,10,10,10,10,8] } assert my_solution.minimumCost(**test_input) == 149 test_input = { "source": "aeaaebhbhehbeehbehea", "target": "babehheaaeebeebahhba", "original": ["a","e","a","e","b","h","b","h","h","e"], "changed": ["b","a","e","h","h","e","a","a","b","b"], "cost": [8,6,3,8,7,9,9,10,10,5] } assert my_solution.minimumCost(**test_input) == 109 test_input = { "source": "aeehhhaeebhhbeabeeha", "target": "haaeaabeeeheehbaehha", "original": ["a","e","h","a","h","b","e","b"], "changed": ["h","a","a","b","e","e","h","a"], "cost": [7,9,10,7,8,9,8,8] } assert my_solution.minimumCost(**test_input) == 117 test_input = { "source": "ahhebhhbbhbebaeehbbh", "target": "hbebaeebebhabeehahhb", "original": ["a","h","h","b","b","e","a","e","h","b"], "changed": ["h","b","e","a","e","a","e","h","a","h"], "cost": [4,8,2,8,9,9,9,8,9,6] } assert my_solution.minimumCost(**test_input) == 116 test_input = { "source": "babhbaaabbabehhhaaea", "target": "aabhhaebehbaehahbahb", "original": ["b","b","a","a","e"], "changed": ["a","h","e","b","h"], "cost": [2,10,8,4,6] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "bbaaaebhhbehaaaabbab", "target": "aebahaaabheebbaehbbb", "original": ["b","b","a","e","h","h","a","a","b"], "changed": ["a","e","h","a","a","b","b","e","h"], "cost": [6,8,3,10,10,7,7,9,10] } assert my_solution.minimumCost(**test_input) == 120 test_input = { "source": "bbabeehehhbhbhbbaabb", "target": "heaabheabehhahhabhhe", "original": ["b","b","b","e","h","e","h","a","a"], "changed": ["h","e","a","b","e","a","b","b","h"], "cost": [7,6,5,9,7,3,10,10,6] } assert my_solution.minimumCost(**test_input) == 116 test_input = { "source": "bbhbahbbbabhbbbbbhaa", "target": "aheebebehaeheehhbahh", "original": ["b","b","h","b","a","a"], "changed": ["a","h","e","e","b","h"], "cost": [3,5,9,9,5,10] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "beeeaehhbbbaeaeebabh", "target": "ahehaahaaehhabaehbah", "original": ["b","e","e","h","b","a","a"], "changed": ["a","h","a","a","h","h","b"], "cost": [3,5,5,10,1,7,2] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "bhabahbhabaahbhahabb", "target": "ehbbhehbbhebaeeheebe", "original": ["a","a","h","b","h","a"], "changed": ["b","h","e","h","b","e"], "cost": [9,10,5,10,10,8] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "bhhbaaahheaebehhabeh", "target": "bbhebbeeahahhbeabeba", "original": ["h","b","h","h","e","b","e","a"], "changed": ["b","e","e","a","h","h","b","b"], "cost": [2,8,8,8,10,7,5,7] } assert my_solution.minimumCost(**test_input) == 131 test_input = { "source": "eaabhhahhhehbabaabae", "target": "bebaehabeehehbehhahh", "original": ["a","a","b","h","h","b","a","e"], "changed": ["e","b","a","e","b","e","h","h"], "cost": [9,9,9,8,6,4,7,10] } assert my_solution.minimumCost(**test_input) == 158 test_input = { "source": "ebbaebeheabhheeeaeaa", "target": "eehbhebhheeabehbebea", "original": ["b","a","e","e","h","h","a"], "changed": ["e","b","h","b","a","b","e"], "cost": [6,9,10,7,7,7,9] } assert my_solution.minimumCost(**test_input) == 135 test_input = { "source": "ebbhbheeaeaeeahehahh", "target": "ehhebhahhhhheaaaaahb", "original": ["b","h","e","a","h","e","h"], "changed": ["h","e","h","h","a","a","b"], "cost": [10,7,10,8,10,3,9] } assert my_solution.minimumCost(**test_input) == 108 test_input = { "source": "eebhehaabeaaaaheheha", "target": "abbbaeaebbhabehbabbb", "original": ["e","h","e","h","a","a","a","h"], "changed": ["b","b","a","e","h","b","e","a"], "cost": [10,10,10,8,9,6,10,10] } assert my_solution.minimumCost(**test_input) == 139 test_input = { "source": "eeeaehbabbebhhaehaha", "target": "hehbbahabhhababeeeeh", "original": ["e","a","e","h","b","h","h","a","a"], "changed": ["h","b","b","a","h","b","e","e","h"], "cost": [3,9,4,8,9,10,6,10,6] } assert my_solution.minimumCost(**test_input) == 120 test_input = { "source": "eeehababeeeheebeehah", "target": "hhhabbbbahhehhhbhbab", "original": ["e","h","a","e","h","b","e"], "changed": ["h","a","b","a","e","h","b"], "cost": [7,8,6,10,10,10,9] } assert my_solution.minimumCost(**test_input) == 143 test_input = { "source": "eehhhbbhebeeehahaaae", "target": "bahaeebhbhhebbbahbhh", "original": ["e","h","b","b","e","e","h","a","h","a"], "changed": ["a","e","e","h","h","b","b","b","a","h"], "cost": [9,9,9,5,3,8,10,2,2,1] } assert my_solution.minimumCost(**test_input) == 69 test_input = { "source": "ehaaeabaebaehbbhbhbe", "target": "baheeebehhebhbeebbbe", "original": ["e","h","a","a","e","b","b","h"], "changed": ["b","a","h","e","h","h","e","e"], "cost": [8,8,4,10,7,10,10,10] } assert my_solution.minimumCost(**test_input) == 123 test_input = { "source": "ehaehehbeebaebaeebeb", "target": "eehhebaheaheahhhbeaa", "original": ["h","e","b","e","a","a","e","b","b"], "changed": ["e","h","h","a","e","h","b","e","a"], "cost": [9,8,8,10,10,10,3,10,9] } assert my_solution.minimumCost(**test_input) == 162 test_input = { "source": "ehbahbbaabhbabahbbhh", "target": "ahbhbehbahaehhaaehhh", "original": ["a","b","h"], "changed": ["h","h","a"], "cost": [9,9,2] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ehbeabbhhebhbahbhbab", "target": "hhbaaebbaaabhhbehhae", "original": ["e","e","b","h","b","a","b"], "changed": ["h","a","a","b","h","h","e"], "cost": [3,8,10,7,5,10,10] } assert my_solution.minimumCost(**test_input) == 117 test_input = { "source": "hbhheeehehbbhbbehaae", "target": "aebhbheehbbbhabbhebh", "original": ["h","b","h","e","h","e","a","a"], "changed": ["a","e","b","h","e","b","e","b"], "cost": [3,9,8,10,7,8,4,9] } assert my_solution.minimumCost(**test_input) == 116 test_input = { "source": "hebeebhhhhabaaheabbh", "target": "aeheabaeaaeababeheae", "original": ["h","b","h","a","b","a","h","a","b"], "changed": ["a","h","e","e","a","b","b","h","e"], "cost": [10,6,8,10,10,5,10,10,2] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "hebhhaaaahbehahebaba", "target": "ahhabehbahehhahaehhh", "original": ["h","b","h","a","a","b","e","e"], "changed": ["a","h","b","e","h","e","h","a"], "cost": [6,6,10,9,7,10,10,7] } assert my_solution.minimumCost(**test_input) == 128 test_input = { "source": "hheahabebabhehahaahe", "target": "eaahbbbaehhhahhhebhe", "original": ["h","h","e","a","a","b","a"], "changed": ["e","a","a","b","h","h","e"], "cost": [3,10,10,8,10,10,10] } assert my_solution.minimumCost(**test_input) == 140 test_input = { "source": "hhebabehhhhbehaahbhh", "target": "ehhabeahaheaabbehhbe", "original": ["h","e","b","a","b","e","h","h","b"], "changed": ["e","h","a","b","e","a","a","b","h"], "cost": [5,3,9,10,10,6,9,5,10] } assert my_solution.minimumCost(**test_input) == 127 test_input = { "source": "hhhbbbhhaeabhheaehea", "target": "bebhhaeheahhebbeeahb", "original": ["h","h","b","a","e","a","e","h","e","a"], "changed": ["b","e","h","e","a","h","b","a","h","b"], "cost": [10,6,8,9,6,8,10,5,8,9] } assert my_solution.minimumCost(**test_input) == 149 test_input = { "source": "anrlqxdnlqcxqdlsceokwgrzkakyqw", "target": "fxynzghpiexaarjuaepxxpaudqipxx", "original": ["a","n","r","l","q","x","d","n","l","q","d","s","k","w","g","z","k","a","k","q"], "changed": ["f","x","y","n","z","g","h","p","i","a","r","u","x","x","p","u","d","q","i","x"], "cost": [78,19,91,27,96,16,95,100,38,99,99,94,82,75,71,100,96,88,78,89] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ayvhtgqvcputvzdksluictwibnvlxx", "target": "pssmcmkjyqniiyiiubwghwldxptuix", "original": ["a","h","t","g","v","c","t","v","z","d","k","s","l","u","i","c","w","b","v","l","x"], "changed": ["p","m","c","m","j","y","i","i","y","i","i","u","b","w","g","h","l","x","t","u","i"], "cost": [82,77,100,95,86,34,77,38,90,31,97,96,77,32,79,87,72,65,100,98,56] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "bdkgumfyqsosetnboawzhflcfkhryb", "target": "rqwqiefmafkzmataueoobbfkkxsung", "original": ["b","d","k","g","u","m","y","q","s","o","s","e","t","o","z","h","l","c","f","k","h","r","y","b"], "changed": ["r","q","w","q","i","e","m","a","f","k","z","m","a","u","o","b","f","k","k","x","s","u","n","g"], "cost": [97,97,72,71,84,96,81,53,85,81,85,48,82,47,54,79,63,94,86,66,96,39,80,82] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "dmhqhwozzfespijaadiwceabbxlfgd", "target": "owmyowokqacnhwmaellbhcnoodviac", "original": ["d","m","h","h","z","z","f","s","p","a","d","i","w","c","a","b","x","l","g","d"], "changed": ["o","w","m","o","k","q","a","n","h","e","l","l","b","h","n","o","d","v","a","c"], "cost": [100,55,84,66,97,92,86,86,98,78,71,100,89,74,77,15,59,59,87,86] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "erjbgdadefwtaydgneprfphalkagce", "target": "uzslvcynelwivxyzvhlpoxeulnguvi", "original": ["e","r","j","b","g","d","a","d","f","t","a","y","d","g","e","p","r","f","h","a","k","g","c","e"], "changed": ["u","z","s","l","v","c","y","n","l","i","v","x","y","z","h","l","p","o","e","u","n","u","v","i"], "cost": [36,100,100,98,99,90,98,93,29,80,37,98,82,84,94,97,86,97,73,96,73,92,94,57] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "gsolujonrufkcbigtjvpsgwtcaafjk", "target": "niymoyhbyicgjgcjkqkonhcgyqvhwr", "original": ["g","s","o","j","o","n","u","b","i","g","t","j","v","p","s","g","t","c","a","a","j"], "changed": ["n","i","y","y","h","b","i","g","c","j","k","q","k","o","n","h","g","y","q","v","w"], "cost": [90,91,99,100,97,95,72,56,85,55,96,77,65,21,38,18,54,91,90,99,87] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "haqgciljqjmplyboytkbvzvncembca", "target": "vxjxtxjkvyfvnkfbcfxzwjgmvflduz", "original": ["a","q","g","c","i","l","j","q","j","p","l","b","y","t","b","z","n","c","e","m","c","a"], "changed": ["x","j","x","t","x","j","k","v","y","v","n","f","c","f","z","j","m","v","f","l","u","z"], "cost": [92,99,90,100,86,49,32,98,72,80,87,87,54,56,93,19,94,81,94,98,76,94] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "jdsusoktswdtjkwuawzxxxruaybamd", "target": "ycnwpqplsmumuzggvekrmgtjxxdqwd", "original": ["j","s","u","s","k","t","d","t","j","w","u","a","w","z","x","x","r","u","y","b","m"], "changed": ["y","n","w","p","p","l","u","m","u","g","g","v","e","k","r","g","t","j","x","d","w"], "cost": [40,97,100,79,39,99,59,49,84,95,90,82,87,22,95,90,76,70,66,96,92] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "nwukaumgrqigiynrvteerkcwheiiim", "target": "nkimfbwdsbbbpccfepohlvxapvgxlg", "original": ["w","k","u","m","r","q","g","i","y","n","r","v","t","e","e","r","k","h","e","i","i","i","m"], "changed": ["k","m","b","w","s","b","b","p","c","c","f","e","p","o","h","l","v","p","v","g","x","l","g"], "cost": [63,94,50,93,100,95,99,92,100,28,98,73,80,99,73,77,84,98,64,87,98,60,85] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ovtbwsxlxgefyzlwgslddghfjyvyif", "target": "iobyykxbenmagxgftwubmkrxuvhaxq", "original": ["v","t","s","l","x","g","e","f","y","z","l","g","s","l","d","d","g","h","f","j","y","v","i","f"], "changed": ["o","b","k","b","e","n","m","a","g","x","g","t","w","u","b","m","k","r","x","u","v","h","x","q"], "cost": [95,63,97,83,95,93,81,95,100,83,63,99,97,94,45,100,38,99,18,81,39,73,92,24] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "pkluljeraiornkwyxkowpqlpeemdha", "target": "ckwzwedmdxrkbtgrhrpozwvzwijlri", "original": ["p","l","u","e","a","i","r","n","k","w","y","x","k","o","w","p","q","l","e","e","m"], "changed": ["c","w","z","d","d","x","k","b","t","g","r","h","r","p","o","z","w","v","w","i","j"], "cost": [95,88,96,84,49,86,39,68,64,84,99,96,83,46,95,84,74,64,95,83,67] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "qxzlstqdqpfgswlrztsnnclfnsaajc", "target": "hgjeamofjdcawvbdmwczbctphfupgl", "original": ["q","x","z","l","s","t","q","q","p","g","l","r","z","t","n","n","f","n","s","a","a","j","c"], "changed": ["h","g","j","e","a","m","o","j","d","a","b","d","m","w","z","b","p","h","f","u","p","g","l"], "cost": [91,10,86,97,98,76,100,96,97,59,95,97,67,93,84,64,55,81,97,69,99,81,81] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "sozjzrckkcytmscpjjhbkzsfgofzml", "target": "xjwgnxzbukkqwtocdtjeglsxkjhfsg", "original": ["s","o","z","j","z","r","k","k","s","j","j","h","b","k","z","f","g","f","z","m","l"], "changed": ["x","j","w","g","n","x","b","u","t","d","t","j","e","g","l","x","k","h","f","s","g"], "cost": [92,100,97,100,21,93,47,100,50,44,84,84,50,90,64,83,55,75,73,42,89] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "tahngidhiduqtsaimwqhjfkoyvubhx", "target": "xsglbaliykddukogzcfpchirxnzdlp", "original": ["t","a","h","n","g","i","d","h","i","d","u","q","t","s","a","i","m","w","q","h","f","k","o","v","b","h","x"], "changed": ["x","s","g","l","b","a","l","i","y","k","d","d","u","k","o","g","z","c","f","p","h","i","r","n","d","l","p"], "cost": [26,97,84,85,78,59,98,50,91,100,98,7,96,96,73,82,23,96,59,75,87,79,69,95,41,87,100] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "tqamxbehkpaapskhicnkuyyzkvjxfl", "target": "jlykiobovnobxxwnevqcluhcfmutqu", "original": ["t","q","a","m","x","b","e","h","k","p","p","s","h","i","n","k","u","y","y","z","v","x","l"], "changed": ["j","l","y","k","i","o","b","o","v","n","x","x","n","e","q","c","l","u","h","c","m","t","u"], "cost": [68,73,57,37,99,81,75,93,100,88,50,93,89,96,88,85,70,36,71,77,54,65,94] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ujdbivstmdpnnpnnpggqiwankpoyfw", "target": "grttiosznxkzeapbsjcisymwaetxcl", "original": ["u","d","b","v","t","m","p","n","n","p","n","n","p","g","q","i","w","a","n","p","o","f"], "changed": ["g","t","t","o","z","n","k","z","e","a","p","b","s","c","i","s","y","m","w","e","t","c"], "cost": [100,99,62,99,77,64,56,90,46,94,75,99,87,90,75,83,78,49,100,87,75,20] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "wqomlunjedgsdmdmcwohbxmkeqayxa", "target": "pojjjjzfsrvrhezcdbinplekiwhllb", "original": ["w","o","m","l","u","n","e","d","d","m","d","m","c","o","h","b","x","e","a","y"], "changed": ["p","j","j","j","j","z","s","r","h","e","z","c","d","i","n","p","l","i","h","l"], "cost": [91,91,51,66,89,97,95,89,72,58,98,65,99,63,58,42,89,79,52,42] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "yzflltfyomcnxhwfxcijnsxotwgcuy", "target": "pwdaiitmmlwdyhwpcrhcnncughvchb", "original": ["y","l","l","f","y","o","c","n","x","f","i","j","s","x","o","t","w","g","u","y"], "changed": ["p","a","i","t","m","m","w","d","y","p","h","c","n","c","u","g","h","v","h","b"], "cost": [84,87,90,88,64,80,95,59,96,76,100,93,72,50,97,79,60,90,72,67] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "zchjedhjmkrzqkqnywdgxakcdmsdwg", "target": "cbftcpgezcgjguefrieolevyaadkgb", "original": ["z","c","h","j","e","m","k","r","z","q","k","q","n","y","w","d","x","k","c","m","s","d","w","g"], "changed": ["c","b","f","t","c","z","c","g","j","g","u","e","f","r","i","e","l","v","y","a","d","k","g","b"], "cost": [57,94,71,85,53,96,97,43,93,65,76,93,94,100,81,85,62,100,34,99,95,85,77,68] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "zpxcysgujcbbcgbqqofookukkotwij", "target": "dfxsvtmexhdnfhbdrcdgqlajddteph", "original": ["z","s","g","u","j","c","g","q","o","f","k","u","k","o","w","j"], "changed": ["d","t","m","e","x","f","h","r","c","d","l","a","j","d","e","h"], "cost": [75,28,71,70,92,88,82,82,80,91,97,93,77,14,68,75] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "bavusatavvvuubavsauavubtusubsvtsvsbttbvs", "target": "ssauttbvssatusutusbattuttsutabubutuasvuu", "original": ["b","a","v","s","t","a","v","a","v","s","u","u","b","s","v","t","s","b","t","b"], "changed": ["s","s","a","t","b","v","s","u","t","u","b","t","t","a","b","u","b","u","a","v"], "cost": [948,467,690,969,300,877,924,924,791,724,809,652,388,592,772,829,912,679,751,529] } assert my_solution.minimumCost(**test_input) == 27579 test_input = { "source": "bbvstvuatvbasbbsvsuvvuvvauutttvtsuavsvua", "target": "buvbtutbsbuttbtvabbuvubvbvabsbattutvbvvu", "original": ["b","s","v","u","a","t","v","a","s","u","u","u","t","v","a"], "changed": ["u","b","u","t","b","s","b","t","v","b","v","a","b","a","u"], "cost": [795,956,694,238,665,894,519,867,887,715,845,16,942,429,282] } assert my_solution.minimumCost(**test_input) == 22249 test_input = { "source": "busbsusauusbbasssutaauttavbbabtbustvubtv", "target": "vuabutaustubbbsbabbusavsttbtubavsabuvvbt", "original": ["b","s","u","s","a","u","s","t","a","u","t","t","v","b","v"], "changed": ["v","u","t","a","u","s","b","b","s","a","v","s","t","t","u"], "cost": [927,992,291,999,989,996,318,196,155,948,375,845,960,949,250] } assert my_solution.minimumCost(**test_input) == 24291 test_input = { "source": "buuasstsvvvvtsaavstbvubtbstbussbavsvvvbt", "target": "utsabasvssauvuvavvbstbbaauvvvtvtbastatua", "original": ["b","u","u","s","s","v","v","t","b","u","b","s","t","b","u","b","a","v"], "changed": ["u","t","s","b","v","s","a","b","s","b","a","u","v","v","v","t","b","t"], "cost": [935,827,806,951,298,554,896,852,759,853,530,891,942,944,464,952,882,887] } assert my_solution.minimumCost(**test_input) == 31617 test_input = { "source": "bvbsstasaabvubbaabvbsbsttuvvuutsabbaubau", "target": "sbuutvuttaubsbbsaatbvsbtsvutuutvtabaubat", "original": ["b","b","s","t","a","u","a","v","s","s","v"], "changed": ["s","u","t","v","u","s","s","t","v","b","u"], "cost": [985,604,940,913,910,765,729,905,848,793,468] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "satuaabaavaauussvsvssavtuuvbttassbtususv", "target": "ababsttvsabvuvaatuvtsvtsasbaatavuauvvstv", "original": ["s","u","a","a","a","v","a","u","s","s","v","t","u","u","v","b","t","s","t"], "changed": ["a","b","s","t","v","a","b","v","u","t","t","s","a","s","b","a","a","v","u"], "cost": [848,908,555,510,759,404,799,963,855,901,609,854,878,915,879,594,748,937,942] } assert my_solution.minimumCost(**test_input) == 27052 test_input = { "source": "sauaavvasvsatabbvuusvatsabssavvtsbvuusva", "target": "abtuuvutubsbbtuttbtubbasuaustsbssatubsvb", "original": ["s","a","a","a","v","t","b","b","v","u","u","s","t","b","v","t"], "changed": ["a","b","u","t","b","b","u","t","t","b","t","u","a","a","s","s"], "cost": [689,325,535,931,528,722,965,786,645,499,791,717,557,988,229,834] } assert my_solution.minimumCost(**test_input) == 21007 test_input = { "source": "ssautbbbuattvtutbavabtbvbusbsbaavutvbutu", "target": "svbvuttvtbtvuavbvtstutuvutvbasbauvabsvvv", "original": ["s","a","b","u","t","v","t","u","b","a","b","v","b"], "changed": ["v","b","t","t","v","u","a","v","v","t","u","b","s"], "cost": [943,915,778,641,540,872,999,856,979,848,856,935,677] } assert my_solution.minimumCost(**test_input) == 33036 test_input = { "source": "sstsvvvvvabubtvsvaatsavssbatstbbabbtbvvt", "target": "tattabsubsatbtbbbussususassautbabsbvabvu", "original": ["s","s","v","v","v","v","a","b","s","a","t","s","t","a","b","t","t"], "changed": ["t","a","a","b","s","u","s","a","b","u","s","u","a","b","s","v","u"], "cost": [723,861,682,949,830,969,880,892,750,461,870,592,233,718,967,621,472] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ssuuavtuuutubutvtssbasssvttuvuubbabubvsu", "target": "ustbvvsutaasabtasabuaasuatuaabatubsbtvbt", "original": ["s","u","u","a","t","u","b","s","b","t","u","v","b","a","b","s"], "changed": ["u","t","b","v","a","s","a","a","u","u","a","a","t","b","s","b"], "cost": [709,599,774,993,858,730,987,700,596,200,984,567,656,971,777,790] } assert my_solution.minimumCost(**test_input) == 23824 test_input = { "source": "suvsavbasssavasvvbttubvbvatbstbabsutvuua", "target": "aaavasusvsatsuvuatbsstbvstuatttuuvubavau", "original": ["s","v","b","a","s","a","t","t","u","b","v","b","b","s","a","u","u"], "changed": ["v","s","u","s","a","t","b","s","s","t","b","v","a","t","u","v","a"], "cost": [944,923,845,911,686,687,403,705,378,929,315,296,484,666,354,375,649] } assert my_solution.minimumCost(**test_input) == 25662 test_input = { "source": "tavauavbtsuuubtbuaatsbutvastasaavbtbsubs", "target": "stsautbvvtbuvvtstaaubavvuaabubavsvbasvvb", "original": ["t","a","v","v","s","u","b","b","u","t","s","b","u","t","v","s","t","a"], "changed": ["s","t","s","b","t","b","v","s","t","u","b","a","v","v","u","a","b","u"], "cost": [936,867,711,886,565,650,500,694,394,662,948,878,864,993,925,508,714,815] } assert my_solution.minimumCost(**test_input) == 24476 test_input = { "source": "tbsttbstvtvusvbaavuuuvvuavuuusbusabsubta", "target": "uvvbttvbvtbtausvtavsuavusvusuabuvtutttas", "original": ["t","b","s","u","v","a","a","v","u","u","a","s","b","s","b","t"], "changed": ["u","v","v","t","u","v","t","a","v","s","s","a","u","t","t","a"], "cost": [409,241,815,861,536,968,983,726,882,674,981,516,918,653,368,845] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "tsauvattvvavaasbutvuavvvttuvasavbvavtsaa", "target": "vbtsubbbbbavvvbsvstavasaaaatsattvbtubsvs", "original": ["t","s","u","a","t","v","a","b","u","v","u","v","v","t","a","a","b"], "changed": ["v","b","s","b","b","b","v","s","v","t","a","a","s","a","s","t","v"], "cost": [731,965,614,651,952,991,940,606,664,261,468,295,202,675,921,628,690] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "tsvsstututatsbvatauatsausaubsuuassssbuts", "target": "assutuavuvusbubbtavsavbvusvbvtbubvvuvtvb", "original": ["t","v","s","s","t","u","t","a","s","b","v","a","s","u","a","u","u","b"], "changed": ["a","s","u","t","u","a","v","u","b","u","b","b","v","v","s","t","b","v"], "cost": [924,489,946,903,425,474,965,605,168,870,992,903,367,995,425,580,918,991] } assert my_solution.minimumCost(**test_input) == 23945 test_input = { "source": "ttuvauavausttvusavuvsuababtubbbvvsttbasv", "target": "butttussvtvbbtbtasuusstattuvvvuavbtsbttu", "original": ["t","t","u","a","a","u","s","v","v","u","a","b","b","u","b","b","v","s","t"], "changed": ["b","u","t","s","v","b","t","s","u","s","t","a","t","v","v","u","a","b","s"], "cost": [790,396,583,83,931,950,760,364,698,503,957,984,611,688,1000,986,679,913,906] } assert my_solution.minimumCost(**test_input) == 25663 test_input = { "source": "tuasbutsuubbvauvautsbtabsaaauubbvaavtuua", "target": "tvauvutvbbavuatsbaatvtbavatavtsvttttuvta", "original": ["u","s","b","u","v","u","v","a","u","t","b","s","a","b","v","t"], "changed": ["v","u","v","b","u","t","s","b","a","a","a","v","t","s","t","u"], "cost": [958,880,986,571,311,294,673,433,733,984,444,576,666,847,516,866] } assert my_solution.minimumCost(**test_input) == 21164 test_input = { "source": "tvsbbttvsvubbtasvbusataaatautauavvstsbub", "target": "tbttvaavutvbstvutavavtbuvtuabsubsuvbtusa", "original": ["v","b","s","u","b","v","s","a","a","a","u","t","v","v","s","s","u","b"], "changed": ["b","v","u","v","s","t","a","v","b","u","a","b","s","u","v","t","s","a"], "cost": [977,323,878,825,625,908,794,831,973,881,946,981,437,904,564,826,720,583] } assert my_solution.minimumCost(**test_input) == 29347 test_input = { "source": "uatavuaavatstabvtvabbbuvatvabbauvsvtauuu", "target": "ustbbuvbtusutuutvsuttbuubuuubasvubuubusb", "original": ["a","a","v","a","v","a","t","b","t","b","v","t","s","u","u"], "changed": ["s","b","b","v","t","u","s","u","v","t","u","u","b","s","b"], "cost": [681,801,894,221,732,732,984,989,750,705,181,993,888,957,912] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ubvtusbavavtbssuutvsvasttbauvtvttsvtttss", "target": "usasbvasuttsbvvuuvvtubssuuuuvuvubtsbsstu", "original": ["b","t","a","v","a","s","s","t","b","a","t","v","s"], "changed": ["s","s","s","u","t","v","t","u","u","u","b","s","u"], "cost": [933,864,772,686,918,885,923,892,538,120,913,892,441] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "ubvuasvuvuavasvvasausabbtausaaubbvbtuubv", "target": "sbbutubbvtvauatbavstvbttavbubvsvsutuavta", "original": ["u","v","a","s","u","a","v","a","s","s","a","a","b","b","u","u"], "changed": ["s","b","t","u","b","v","a","u","a","v","s","b","t","s","a","v"], "cost": [951,655,564,597,402,910,850,664,352,937,698,281,997,798,368,963] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "utauutasstbuubsussstaauatvaavuuabvvbvaba", "target": "vttavbbbvbbbvuaavauubbsavaststtatsbssaab", "original": ["a","u","t","a","s","s","s","t","v","a","u","b","v","b","v","b"], "changed": ["t","a","b","b","b","a","v","v","a","s","t","t","b","s","s","a"], "cost": [862,888,579,525,903,443,828,892,953,879,855,705,961,438,478,998] } assert my_solution.minimumCost(**test_input) == -1 test_input = { "source": "uttbbsvttssubvbbaabsavaabvsvbbbbbatvbtsu", "target": "tatsstaubbtbvsabvasbubvvbutasssvttuvvtvs", "original": ["u","t","b","s","v","t","t","u","b","v","b","a","s","v","v","b","a","s","u"], "changed": ["t","a","s","t","a","u","b","b","v","s","a","v","b","b","u","t","t","v","s"], "cost": [880,822,798,549,420,189,730,621,865,189,950,921,887,816,772,69,907,868,715] } assert my_solution.minimumCost(**test_input) == 23971 test_input = { "source": "utvsausubbtuasvtuttatbtasuvbtuuubsatabau", "target": "tstsatsbsttaatasbvvsabbabbtabtvbvuaavaau", "original": ["u","b","b","u","s","v","u","t","a","t","s","b","u","b","s","a"], "changed": ["t","s","t","a","t","a","b","v","s","a","b","a","v","v","u","v"], "cost": [425,837,948,892,923,403,619,690,775,951,577,1000,816,999,602,800] } assert my_solution.minimumCost(**test_input) == 29865 test_input = { "source": "uvtavsbvvvvavsstavustttuuvvavatattbavttv", "target": "vvbuuutbtvbtutatuttvsssvtvsbbvbbvuausavu", "original": ["t","a","v","s","b","v","v","a","s","s","u","s","t","u","v","a","t","t","b"], "changed": ["b","u","u","u","t","b","t","t","t","a","t","v","s","v","s","b","v","u","a"], "cost": [984,969,379,449,842,818,399,954,718,823,932,935,405,591,643,965,1000,700,189] } assert my_solution.minimumCost(**test_input) == 27084 test_input = { "source": "vabsuaavabatbastaauaaaututtutsvuusatavas", "target": "svubbsaavaubbbvasatssvvubtsssabsabsbstau", "original": ["a","s","u","a","v","b","a","s","t","u","u","t","u","t","s","v","u","t","v","s"], "changed": ["v","b","b","s","a","a","u","v","a","t","v","u","s","s","a","b","a","b","t","u"], "cost": [948,531,696,662,908,460,829,859,857,374,1000,996,356,810,804,751,988,815,933,924] } assert my_solution.minimumCost(**test_input) == 27967 test_input = { "source": "vsvstbbutastuuaubuausuuavvttvabvsaavbtvs", "target": "vbbtbuatasbauavbbabbbvbutaavsvtubvbuttst", "original": ["s","v","s","t","b","b","u","a","t","u","a","u","a","u","a","v","v","b","v"], "changed": ["b","b","t","b","u","a","t","s","a","a","v","b","b","v","u","t","s","t","u"], "cost": [510,990,975,894,921,775,819,665,76,509,943,691,757,617,773,404,777,932,837] } assert my_solution.minimumCost(**test_input) == 25040 test_input = { "source": "vusbbaubbusbbtusttbtavsavbbbausavbausbub", "target": "uvvbsututvavautuatuussbbttbaaaasbbuvavaa", "original": ["u","b","a","u","b","b","s","b","b","t","t","s","a","v","u","a","v"], "changed": ["v","s","u","t","u","t","a","v","a","u","a","b","b","t","a","s","b"], "cost": [555,920,950,949,936,35,455,910,746,791,661,864,926,11,833,870,854] } assert my_solution.minimumCost(**test_input) == 26439 test_input = { "source": "vvabsutautvsutvavtsvstabvbvtutbvuautaatb", "target": "taasvsstbstasbtuauvtbtusbssbbssbuusuvbbu", "original": ["v","b","s","u","t","u","v","t","a","t","s","v","v","a"], "changed": ["a","s","v","s","s","b","t","b","u","u","b","s","b","b"], "cost": [813,860,971,768,419,950,866,741,989,756,325,806,507,571] } assert my_solution.minimumCost(**test_input) == 36258 test_input = { "source": "vvvsaavtavaabtssasstbvuusbubbbsastvttbba", "target": "utssutvbabttvstsbbusttuuutvvbtasvuvbvvav", "original": ["v","a","a","v","b","t","s","a","s","b","s","u","s","a","s","t","t","b","a"], "changed": ["t","u","t","b","v","s","t","b","b","t","u","v","a","s","v","b","v","a","v"], "cost": [722,823,981,465,482,999,572,191,410,968,377,549,658,692,898,263,928,197,831] } assert my_solution.minimumCost(**test_input) == 22953 test_input = { "source": "asmlmoumomvummakmlbabvmvvavlavtsvbvssuumsllttsusts", "target": "asmlmoumomvummakmlbabvmvvaolklllvbsltuomkslvmmusts", "original": ["vlavtsvbvssuumslltts","mtmubkasuvumkobbmsmo","vsbbvauauvuvsauaastl","uovumoluksslvkvlkmam","smsvsuubusskmublvvst","momuatbkubosmmavvssk","mommltotttbtvlvalsbt","vbbbuvslutblvvkvtmmo","boaosasttvtvtabtubab","mbtkumblvbasoobaauvm","vbotklaoltambktlulot","vluamsokkbaalsmmalav","mttmbuosbmlttabmbabl","sskvkbmlabaulluomovt","lstbuomkaoatmavsmvml"], "changed": ["mtmubkasuvumkobbmsmo","vsbbvauauvuvsauaastl","uovumoluksslvkvlkmam","smsvsuubusskmublvvst","momuatbkubosmmavvssk","mommltotttbtvlvalsbt","vbbbuvslutblvvkvtmmo","boaosasttvtvtabtubab","mbtkumblvbasoobaauvm","vbotklaoltambktlulot","vluamsokkbaalsmmalav","mttmbuosbmlttabmbabl","sskvkbmlabaulluomovt","lstbuomkaoatmavsmvml","olklllvbsltuomkslvmm"], "cost": [9758,7133,9355,8885,6055,7360,9168,9288,7422,6995,8167,6154,6939,6343,9733] } assert my_solution.minimumCost(**test_input) == 118755 test_input = { "source": "bkmltoaakmatkvllubamuvbmkolamvolsaottsokbmutktsvlo", "target": "skmltoaakmatkvllubamuvbmkolamvolsosuataatovmktsvlo", "original": ["aottsokbmut","ktuumtblakk","mkumbambakt","tubtvmuuoat","kkkksosllks","sastauoammb","sbomolbklsk","kaabakosmsb","ltaltkmukoa","lmlaovmluta","kusalltssaa","mattouslbou","obaavmovsal","bk","ko"], "changed": ["ktuumtblakk","mkumbambakt","tubtvmuuoat","kkkksosllks","sastauoammb","sbomolbklsk","kaabakosmsb","ltaltkmukoa","lmlaovmluta","kusalltssaa","mattouslbou","obaavmovsal","osuataatovm","ko","sk"], "cost": [5819,9018,7484,6655,5163,5728,3077,7032,4630,8093,6974,5623,9179,7307,9974] } assert my_solution.minimumCost(**test_input) == 101756 test_input = { "source": "blvalvmkosattusaubkbuvusmoolmkloavaskmkbovkkbvtaas", "target": "blvalvmkosattlooutkvsmabuvaumkloavaskmkbovkkbvtaas", "original": ["usaubkbuvusmoolm","stvkuuablkvlvbuv","laosbokmbsusulta","tosusvtstuousmtv","tkbbalmtoubtmlvk","vauvllvbootbvtsv","malabvmoaavulomv","ootsoksuosvlakov","alaobmvbttmtobvl","oauamtksvbuovmbt","ubtlssmbbaloatsa","sstskkmtkoobaavt","avsskubbbtossbsu","aumbsbautvkmsauu","btsuvmosbtomvmma"], "changed": ["stvkuuablkvlvbuv","laosbokmbsusulta","tosusvtstuousmtv","tkbbalmtoubtmlvk","vauvllvbootbvtsv","malabvmoaavulomv","ootsoksuosvlakov","alaobmvbttmtobvl","oauamtksvbuovmbt","ubtlssmbbaloatsa","sstskkmtkoobaavt","avsskubbbtossbsu","aumbsbautvkmsauu","btsuvmosbtomvmma","looutkvsmabuvaum"], "cost": [8903,5338,8835,8645,8789,7933,8044,3865,7564,5782,9245,9165,8886,7691,8039] } assert my_solution.minimumCost(**test_input) == 116724 test_input = { "source": "mobbmmmsabbomsbukkotbttvsuoubtvuabaktsuoltvamlltbv", "target": "mobbmmmsauotksusvmvtbmaovtsvtaklabaktsuoltbamtumtl", "original": ["bbomsbukkotbttvsuoubtvua","sbuastublskotvtotmokuota","kssvtltakbtmlbmtoskaousb","vuososkmtvsobkbvuvbklvbv","vtsvllsklslkbulusbuastlm","bttokabvovvktkavatskoamt","lbmobulvomkovaalbtkoukso","tmmlusaokamvstsmuksmbulu","lkktooukbstvkumvbbsllaas","mbvtkltbvuumuvobstooammv","skovmvassobkbutolttvkokb","olastbtotmlusbmlukmokubl","kvbomsvuaskvkvvmssavubtt","buusuklobuoukatusulotmks","vamlltbv"], "changed": ["sbuastublskotvtotmokuota","kssvtltakbtmlbmtoskaousb","vuososkmtvsobkbvuvbklvbv","vtsvllsklslkbulusbuastlm","bttokabvovvktkavatskoamt","lbmobulvomkovaalbtkoukso","tmmlusaokamvstsmuksmbulu","lkktooukbstvkumvbbsllaas","mbvtkltbvuumuvobstooammv","skovmvassobkbutolttvkokb","olastbtotmlusbmlukmokubl","kvbomsvuaskvkvvmssavubtt","buusuklobuoukatusulotmks","uotksusvmvtbmaovtsvtakla","bamtumtl"], "cost": [8432,9912,7958,9938,8402,7223,5772,9501,8749,8597,6195,7504,7103,9582,8898] } assert my_solution.minimumCost(**test_input) == 123766
1,703,385,000
biweekly-contest-120-count-the-number-of-incremovable-subarrays-i
https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i
count-the-number-of-incremovable-subarrays-i
{ "questionId": "3252", "questionFrontendId": "2970", "title": "Count the Number of Incremovable Subarrays I", "titleSlug": "count-the-number-of-incremovable-subarrays-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 38, "dislikes": 49, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4] 和 [1,2,3,4]。移除任意一个子数组后,剩余元素都是递增的。注意,空数组不是移除递增子数组。 示例 2: 输入:nums = [6,5,7,8] 输出:7 解释:7个移除递增子数组分别为:[5], [6], [5,7], [6,5], [5,7,8], [6,5,7] 和 [6,5,7,8] 。 nums 中只有这 7 个移除递增子数组。 示例 3: 输入:nums = [8,7,6,6] 输出:3 解释:3 个移除递增子数组分别为:[8,7,6], [7,6,6] 和 [8,7,6,6] 。注意 [8,7] 不是移除递增子数组因为移除 [8,7] 后 nums 变为 [6,6] ,它不是严格递增的。 提示: 1 <= nums.length <= 50 1 <= nums[i] <= 50 """ class Solution: def incremovableSubarrayCount(self, nums: List[int]) -> int:
给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4] 和 [1,2,3,4]。移除任意一个子数组后,剩余元素都是递增的。注意,空数组不是移除递增子数组。 示例 2: 输入:nums = [6,5,7,8] 输出:7 解释:7个移除递增子数组分别为:[5], [6], [5,7], [6,5], [5,7,8], [6,5,7] 和 [6,5,7,8] 。 nums 中只有这 7 个移除递增子数组。 示例 3: 输入:nums = [8,7,6,6] 输出:3 解释:3 个移除递增子数组分别为:[8,7,6], [7,6,6] 和 [8,7,6,6] 。注意 [8,7] 不是移除递增子数组因为移除 [8,7] 后 nums 变为 [6,6] ,它不是严格递增的。 提示: 1 <= nums.length <= 50 1 <= nums[i] <= 50 请完成下面的代码来解决上述问题: ```python class Solution: def incremovableSubarrayCount(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 10 test_input = { "nums": [6,5,7,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [8,7,6,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [1] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [2] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [3] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [4] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [5] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [6] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [7] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [8] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [9] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [10] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [1,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [1,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [1,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [2,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [3,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [3,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [3,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [4,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [4,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [4,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,1] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [6,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [6,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [6,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [9,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [9,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [9,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [9,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [10,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [10,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [1,2,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [1,2,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [2,1,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [2,4,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [2,6,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [2,6,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [2,10,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [3,1,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [3,5,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [3,7,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [3,8,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [3,10,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [4,5,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [5,8,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [5,9,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [5,9,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [6,7,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [8,7,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [8,7,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [8,9,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [9,2,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [9,5,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [9,6,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [9,9,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [10,7,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [10,10,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [2,1,1,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [2,5,7,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 10 test_input = { "nums": [3,5,3,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [3,7,10,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [3,8,3,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [4,1,3,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [4,3,5,1] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [4,3,7,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [4,8,7,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [4,9,10,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [5,4,3,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [5,5,9,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,10,10,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [6,4,4,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [6,5,2,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [7,3,2,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [7,5,1,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [7,9,7,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [7,9,8,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [7,10,4,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [8,8,1,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [9,2,8,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [10,5,9,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [10,7,2,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [10,9,1,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [1,1,4,4,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [1,2,3,4,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 15 test_input = { "nums": [1,2,8,9,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [1,7,4,9,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [2,2,4,6,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [2,7,1,3,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 9 test_input = { "nums": [3,1,9,6,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [3,3,3,6,1] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [3,7,1,8,1] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [4,1,6,10,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,4,8,4,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [6,1,1,8,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 7
1,703,341,800
biweekly-contest-120-find-polygon-with-the-largest-perimeter
https://leetcode.com/problems/find-polygon-with-the-largest-perimeter
find-polygon-with-the-largest-perimeter
{ "questionId": "3262", "questionFrontendId": "2971", "title": "Find Polygon With the Largest Perimeter", "titleSlug": "find-polygon-with-the-largest-perimeter", "isPaidOnly": false, "difficulty": "Medium", "likes": 53, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个长度为n的正整数数组nums。 多边形指的是一个至少有 3条边的封闭二维图形。多边形的 最长边一定 小于所有其他边长度之和。 如果你有k(k >= 3)个正数a1,a2,a3, ...,ak 满足a1 <= a2 <= a3 <= ... <= ak 且 a1 + a2 + a3 + ... + ak-1 > ak,那么 一定存在一个k条边的多边形,每条边的长度分别为a1,a2,a3,...,ak。 一个多边形的 周长指的是它所有边之和。 请你返回从 nums中可以构造的 多边形的 最大周长。如果不能构造出任何多边形,请你返回 -1。 示例 1: 输入:nums = [5,5,5] 输出:15 解释:nums 中唯一可以构造的多边形为三角形,每条边的长度分别为 5 ,5 和 5 ,周长为 5 + 5 + 5 = 15 。 示例 2: 输入:nums = [1,12,1,2,5,50,3] 输出:12 解释:最大周长多边形为五边形,每条边的长度分别为 1 ,1 ,2 ,3 和 5 ,周长为 1 + 1 + 2 + 3 + 5 = 12 。 我们无法构造一个包含变长为 12 或者 50 的多边形,因为其他边之和没法大于两者中的任何一个。 所以最大周长为 12 。 示例 3: 输入:nums = [5,5,50] 输出:-1 解释:无法构造任何多边形,因为多边形至少要有 3 条边且 50 > 5 + 5 。 提示: 3 <= n <= 105 1 <= nums[i] <= 109 """ class Solution: def largestPerimeter(self, nums: List[int]) -> int:
给你一个长度为n的正整数数组nums。 多边形指的是一个至少有 3条边的封闭二维图形。多边形的 最长边一定 小于所有其他边长度之和。 如果你有k(k >= 3)个正数a1,a2,a3, ...,ak 满足a1 <= a2 <= a3 <= ... <= ak 且 a1 + a2 + a3 + ... + ak-1 > ak,那么 一定存在一个k条边的多边形,每条边的长度分别为a1,a2,a3,...,ak。 一个多边形的 周长指的是它所有边之和。 请你返回从 nums中可以构造的 多边形的 最大周长。如果不能构造出任何多边形,请你返回 -1。 示例 1: 输入:nums = [5,5,5] 输出:15 解释:nums 中唯一可以构造的多边形为三角形,每条边的长度分别为 5 ,5 和 5 ,周长为 5 + 5 + 5 = 15 。 示例 2: 输入:nums = [1,12,1,2,5,50,3] 输出:12 解释:最大周长多边形为五边形,每条边的长度分别为 1 ,1 ,2 ,3 和 5 ,周长为 1 + 1 + 2 + 3 + 5 = 12 。 我们无法构造一个包含变长为 12 或者 50 的多边形,因为其他边之和没法大于两者中的任何一个。 所以最大周长为 12 。 示例 3: 输入:nums = [5,5,50] 输出:-1 解释:无法构造任何多边形,因为多边形至少要有 3 条边且 50 > 5 + 5 。 提示: 3 <= n <= 105 1 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def largestPerimeter(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [5,5,5] } assert my_solution.largestPerimeter(**test_input) == 15 test_input = { "nums": [1,12,1,2,5,50,3] } assert my_solution.largestPerimeter(**test_input) == 12 test_input = { "nums": [5,5,50] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,1,1] } assert my_solution.largestPerimeter(**test_input) == 3 test_input = { "nums": [1,1,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,1,3] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,1,4] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,1,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,2,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,2,2] } assert my_solution.largestPerimeter(**test_input) == 5 test_input = { "nums": [1,2,3] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,2,4] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,2,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,3,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,3,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,3,3] } assert my_solution.largestPerimeter(**test_input) == 7 test_input = { "nums": [1,3,4] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,3,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,4,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,4,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,4,3] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,4,4] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [1,4,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,5,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,5,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,5,3] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,5,4] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [1,5,5] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [2,1,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,1,2] } assert my_solution.largestPerimeter(**test_input) == 5 test_input = { "nums": [2,1,3] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,1,4] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,1,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,2,1] } assert my_solution.largestPerimeter(**test_input) == 5 test_input = { "nums": [2,2,2] } assert my_solution.largestPerimeter(**test_input) == 6 test_input = { "nums": [2,2,3] } assert my_solution.largestPerimeter(**test_input) == 7 test_input = { "nums": [2,2,4] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,2,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,3,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,3,2] } assert my_solution.largestPerimeter(**test_input) == 7 test_input = { "nums": [2,3,3] } assert my_solution.largestPerimeter(**test_input) == 8 test_input = { "nums": [2,3,4] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [2,3,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,4,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,4,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,4,3] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [2,4,4] } assert my_solution.largestPerimeter(**test_input) == 10 test_input = { "nums": [2,4,5] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [2,5,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,5,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,5,3] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [2,5,4] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [2,5,5] } assert my_solution.largestPerimeter(**test_input) == 12 test_input = { "nums": [3,1,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,1,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,1,3] } assert my_solution.largestPerimeter(**test_input) == 7 test_input = { "nums": [3,1,4] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,1,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,2,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,2,2] } assert my_solution.largestPerimeter(**test_input) == 7 test_input = { "nums": [3,2,3] } assert my_solution.largestPerimeter(**test_input) == 8 test_input = { "nums": [3,2,4] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [3,2,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,3,1] } assert my_solution.largestPerimeter(**test_input) == 7 test_input = { "nums": [3,3,2] } assert my_solution.largestPerimeter(**test_input) == 8 test_input = { "nums": [3,3,3] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [3,3,4] } assert my_solution.largestPerimeter(**test_input) == 10 test_input = { "nums": [3,3,5] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [3,4,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,4,2] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [3,4,3] } assert my_solution.largestPerimeter(**test_input) == 10 test_input = { "nums": [3,4,4] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [3,4,5] } assert my_solution.largestPerimeter(**test_input) == 12 test_input = { "nums": [3,5,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,5,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [3,5,3] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [3,5,4] } assert my_solution.largestPerimeter(**test_input) == 12 test_input = { "nums": [3,5,5] } assert my_solution.largestPerimeter(**test_input) == 13 test_input = { "nums": [4,1,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,1,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,1,3] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,1,4] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [4,1,5] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,2,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,2,2] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,2,3] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [4,2,4] } assert my_solution.largestPerimeter(**test_input) == 10 test_input = { "nums": [4,2,5] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [4,3,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,3,2] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [4,3,3] } assert my_solution.largestPerimeter(**test_input) == 10 test_input = { "nums": [4,3,4] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [4,3,5] } assert my_solution.largestPerimeter(**test_input) == 12 test_input = { "nums": [4,4,1] } assert my_solution.largestPerimeter(**test_input) == 9 test_input = { "nums": [4,4,2] } assert my_solution.largestPerimeter(**test_input) == 10 test_input = { "nums": [4,4,3] } assert my_solution.largestPerimeter(**test_input) == 11 test_input = { "nums": [4,4,4] } assert my_solution.largestPerimeter(**test_input) == 12 test_input = { "nums": [4,4,5] } assert my_solution.largestPerimeter(**test_input) == 13 test_input = { "nums": [4,5,1] } assert my_solution.largestPerimeter(**test_input) == -1 test_input = { "nums": [4,5,2] } assert my_solution.largestPerimeter(**test_input) == 11
1,703,341,800
biweekly-contest-120-count-the-number-of-incremovable-subarrays-ii
https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-ii
count-the-number-of-incremovable-subarrays-ii
{ "questionId": "3248", "questionFrontendId": "2972", "title": "Count the Number of Incremovable Subarrays II", "titleSlug": "count-the-number-of-incremovable-subarrays-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 97, "dislikes": 15, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4] 和 [1,2,3,4]。移除任意一个子数组后,剩余元素都是递增的。注意,空数组不是移除递增子数组。 示例 2: 输入:nums = [6,5,7,8] 输出:7 解释:7个移除递增子数组分别为:[5], [6], [5,7], [6,5], [5,7,8], [6,5,7] 和 [6,5,7,8] 。 nums 中只有这 7 个移除递增子数组。 示例 3: 输入:nums = [8,7,6,6] 输出:3 解释:3 个移除递增子数组分别为:[8,7,6], [7,6,6] 和 [8,7,6,6] 。注意 [8,7] 不是移除递增子数组因为移除 [8,7] 后 nums 变为 [6,6] ,它不是严格递增的。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 """ class Solution: def incremovableSubarrayCount(self, nums: List[int]) -> int:
给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4] 和 [1,2,3,4]。移除任意一个子数组后,剩余元素都是递增的。注意,空数组不是移除递增子数组。 示例 2: 输入:nums = [6,5,7,8] 输出:7 解释:7个移除递增子数组分别为:[5], [6], [5,7], [6,5], [5,7,8], [6,5,7] 和 [6,5,7,8] 。 nums 中只有这 7 个移除递增子数组。 示例 3: 输入:nums = [8,7,6,6] 输出:3 解释:3 个移除递增子数组分别为:[8,7,6], [7,6,6] 和 [8,7,6,6] 。注意 [8,7] 不是移除递增子数组因为移除 [8,7] 后 nums 变为 [6,6] ,它不是严格递增的。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def incremovableSubarrayCount(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 10 test_input = { "nums": [6,5,7,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [8,7,6,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [1] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [2] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [3] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [4] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [5] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [6] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [7] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [8] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [9] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [10] } assert my_solution.incremovableSubarrayCount(**test_input) == 1 test_input = { "nums": [4,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [7,3] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [8,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [8,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [9,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [5,5,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [6,7,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [8,1,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [9,2,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [9,3,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [1,2,10,6] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [1,9,6,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [4,8,5,9] } assert my_solution.incremovableSubarrayCount(**test_input) == 8 test_input = { "nums": [6,2,6,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [10,2,5,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [1,2,4,7,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 15 test_input = { "nums": [1,5,9,5,7] } assert my_solution.incremovableSubarrayCount(**test_input) == 9 test_input = { "nums": [6,6,5,3,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [8,5,5,3,10] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [9,10,1,8,2] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [11,50,14,33,45,38,33,19,28,34] } assert my_solution.incremovableSubarrayCount(**test_input) == 9 test_input = { "nums": [25,9,28,31,38,15,31,44,46,49] } assert my_solution.incremovableSubarrayCount(**test_input) == 11 test_input = { "nums": [25,26,49,31,40,47,30,29,32,34] } assert my_solution.incremovableSubarrayCount(**test_input) == 13 test_input = { "nums": [25,39,29,30,40,28,30,39,30,42] } assert my_solution.incremovableSubarrayCount(**test_input) == 8 test_input = { "nums": [25,41,31,38,30,38,37,41,36,32] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [25,45,49,28,47,44,42,34,28,25] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [25,46,48,41,29,47,32,34,41,34] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [25,47,25,35,48,49,27,37,36,43] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [26,26,50,38,30,38,31,26,39,45] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [26,32,41,38,45,32,31,27,48,41] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [26,45,44,26,33,35,34,36,44,38] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [26,49,42,26,37,41,31,36,45,41] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [27,39,32,30,38,41,28,26,49,49] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [27,43,36,37,33,46,48,35,49,49] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [28,17,12,21,21,49,31,30,40,13] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [28,30,39,31,33,41,47,36,43,46] } assert my_solution.incremovableSubarrayCount(**test_input) == 15 test_input = { "nums": [29,27,32,38,26,38,39,30,41,45] } assert my_solution.incremovableSubarrayCount(**test_input) == 8 test_input = { "nums": [29,31,48,28,27,38,32,28,30,44] } assert my_solution.incremovableSubarrayCount(**test_input) == 10 test_input = { "nums": [29,34,44,27,45,31,37,32,50,26] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [29,38,36,42,31,38,27,48,42,28] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [29,46,34,47,46,41,29,29,38,39] } assert my_solution.incremovableSubarrayCount(**test_input) == 8 test_input = { "nums": [29,49,32,35,38,37,27,25,50,46] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [30,27,38,33,28,48,41,30,25,50] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [30,29,31,44,31,25,50,35,35,47] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [30,30,46,35,31,41,30,37,37,33] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [30,41,28,45,35,41,47,32,29,33] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [30,45,44,27,43,40,28,34,39,40] } assert my_solution.incremovableSubarrayCount(**test_input) == 10 test_input = { "nums": [30,49,34,26,50,50,48,49,39,26] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [31,34,25,43,38,34,29,50,27,34] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [31,35,39,38,41,47,26,43,47,46] } assert my_solution.incremovableSubarrayCount(**test_input) == 8 test_input = { "nums": [31,37,29,41,32,46,25,28,30,29] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [31,38,31,47,25,25,36,29,43,28] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [31,41,25,37,43,29,30,26,30,46] } assert my_solution.incremovableSubarrayCount(**test_input) == 8 test_input = { "nums": [31,42,40,36,39,28,43,29,35,50] } assert my_solution.incremovableSubarrayCount(**test_input) == 9 test_input = { "nums": [32,29,13,39,34,47,38,15,10,5] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [32,35,32,50,32,26,29,49,40,41] } assert my_solution.incremovableSubarrayCount(**test_input) == 9 test_input = { "nums": [32,37,35,26,45,44,47,29,31,28] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [32,43,44,44,36,41,31,33,41,43] } assert my_solution.incremovableSubarrayCount(**test_input) == 11 test_input = { "nums": [32,50,50,46,32,30,32,32,31,39] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [33,27,34,46,42,35,36,49,25,40] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [33,28,35,32,36,38,33,47,36,35] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [33,31,36,38,39,46,42,41,27,33] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [33,45,25,48,45,42,35,38,47,43] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [33,46,38,37,42,48,31,43,38,29] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [33,48,50,48,46,33,34,26,32,33] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [34,25,38,41,31,46,40,46,39,30] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [34,38,35,28,30,37,35,25,48,28] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [34,47,26,48,30,25,26,43,44,41] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [35,22,35,6,20,47,3,29,45,30] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [35,25,29,40,32,29,35,39,39,32] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [35,26,39,41,26,44,36,26,46,50] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [35,27,29,45,29,30,48,42,37,50] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [35,31,27,45,39,46,47,49,26,27] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [35,39,36,30,32,48,34,25,37,45] } assert my_solution.incremovableSubarrayCount(**test_input) == 9 test_input = { "nums": [36,26,44,32,36,29,44,28,48,30] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [36,28,28,45,40,40,32,48,34,48] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [36,28,34,49,48,36,50,25,43,40] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [36,30,47,32,32,35,41,49,41,45] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [36,31,45,34,47,48,49,31,34,34] } assert my_solution.incremovableSubarrayCount(**test_input) == 3 test_input = { "nums": [37,33,50,48,25,37,29,49,46,45] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [37,35,29,27,39,38,49,48,27,37] } assert my_solution.incremovableSubarrayCount(**test_input) == 4 test_input = { "nums": [37,40,42,41,30,40,46,44,47,27] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [37,43,49,40,30,46,31,44,47,25] } assert my_solution.incremovableSubarrayCount(**test_input) == 5 test_input = { "nums": [37,45,49,26,32,45,33,40,35,43] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [37,50,42,50,40,26,34,25,28,44] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [38,28,25,31,28,44,35,26,33,41] } assert my_solution.incremovableSubarrayCount(**test_input) == 6 test_input = { "nums": [38,30,36,50,36,40,41,25,43,43] } assert my_solution.incremovableSubarrayCount(**test_input) == 4
1,703,341,800
biweekly-contest-120-find-number-of-coins-to-place-in-tree-nodes
https://leetcode.com/problems/find-number-of-coins-to-place-in-tree-nodes
find-number-of-coins-to-place-in-tree-nodes
{ "questionId": "3218", "questionFrontendId": "2973", "title": "Find Number of Coins to Place in Tree Nodes", "titleSlug": "find-number-of-coins-to-place-in-tree-nodes", "isPaidOnly": false, "difficulty": "Hard", "likes": 67, "dislikes": 2, "categoryTitle": "Algorithms" }
""" 给你一棵n个节点的无向树,节点编号为0到n - 1,树的根节点在节点0处。同时给你一个长度为 n - 1的二维整数数组edges,其中edges[i] = [ai, bi]表示树中节点ai 和bi之间有一条边。 给你一个长度为 n下标从 0开始的整数数组cost,其中cost[i]是第 i个节点的 开销。 你需要在树中每个节点都放置金币,在节点 i处的金币数目计算方法如下: 如果节点 i对应的子树中的节点数目小于3,那么放1个金币。 否则,计算节点 i 对应的子树内 3 个不同节点的开销乘积的 最大值 ,并在节点 i 处放置对应数目的金币。如果最大乘积是 负数,那么放置 0个金币。 请你返回一个长度为 n的数组coin,coin[i]是节点i处的金币数目。 示例 1: 输入:edges = [[0,1],[0,2],[0,3],[0,4],[0,5]], cost = [1,2,3,4,5,6] 输出:[120,1,1,1,1,1] 解释:在节点 0 处放置 6 * 5 * 4 = 120 个金币。所有其他节点都是叶子节点,子树中只有 1 个节点,所以其他每个节点都放 1 个金币。 示例 2: 输入:edges = [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], cost = [1,4,2,3,5,7,8,-4,2] 输出:[280,140,32,1,1,1,1,1,1] 解释:每个节点放置的金币数分别为: - 节点 0 处放置 8 * 7 * 5 = 280 个金币。 - 节点 1 处放置 7 * 5 * 4 = 140 个金币。 - 节点 2 处放置 8 * 2 * 2 = 32 个金币。 - 其他节点都是叶子节点,子树内节点数目为 1 ,所以其他每个节点都放 1 个金币。 示例 3: 输入:edges = [[0,1],[0,2]], cost = [1,2,-2] 输出:[0,1,1] 解释:节点 1 和 2 都是叶子节点,子树内节点数目为 1 ,各放置 1 个金币。节点 0 处唯一的开销乘积是 2 * 1 * -2 = -4 。所以在节点 0 处放置 0 个金币。 提示: 2 <= n <= 2 * 104 edges.length == n - 1 edges[i].length == 2 0 <= ai, bi < n cost.length == n 1 <= |cost[i]| <= 104 edges一定是一棵合法的树。 """ class Solution: def placedCoins(self, edges: List[List[int]], cost: List[int]) -> List[int]:
给你一棵n个节点的无向树,节点编号为0到n - 1,树的根节点在节点0处。同时给你一个长度为 n - 1的二维整数数组edges,其中edges[i] = [ai, bi]表示树中节点ai 和bi之间有一条边。 给你一个长度为 n下标从 0开始的整数数组cost,其中cost[i]是第 i个节点的 开销。 你需要在树中每个节点都放置金币,在节点 i处的金币数目计算方法如下: 如果节点 i对应的子树中的节点数目小于3,那么放1个金币。 否则,计算节点 i 对应的子树内 3 个不同节点的开销乘积的 最大值 ,并在节点 i 处放置对应数目的金币。如果最大乘积是 负数,那么放置 0个金币。 请你返回一个长度为 n的数组coin,coin[i]是节点i处的金币数目。 示例 1: 输入:edges = [[0,1],[0,2],[0,3],[0,4],[0,5]], cost = [1,2,3,4,5,6] 输出:[120,1,1,1,1,1] 解释:在节点 0 处放置 6 * 5 * 4 = 120 个金币。所有其他节点都是叶子节点,子树中只有 1 个节点,所以其他每个节点都放 1 个金币。 示例 2: 输入:edges = [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], cost = [1,4,2,3,5,7,8,-4,2] 输出:[280,140,32,1,1,1,1,1,1] 解释:每个节点放置的金币数分别为: - 节点 0 处放置 8 * 7 * 5 = 280 个金币。 - 节点 1 处放置 7 * 5 * 4 = 140 个金币。 - 节点 2 处放置 8 * 2 * 2 = 32 个金币。 - 其他节点都是叶子节点,子树内节点数目为 1 ,所以其他每个节点都放 1 个金币。 示例 3: 输入:edges = [[0,1],[0,2]], cost = [1,2,-2] 输出:[0,1,1] 解释:节点 1 和 2 都是叶子节点,子树内节点数目为 1 ,各放置 1 个金币。节点 0 处唯一的开销乘积是 2 * 1 * -2 = -4 。所以在节点 0 处放置 0 个金币。 提示: 2 <= n <= 2 * 104 edges.length == n - 1 edges[i].length == 2 0 <= ai, bi < n cost.length == n 1 <= |cost[i]| <= 104 edges一定是一棵合法的树。 请完成下面的代码来解决上述问题: ```python class Solution: def placedCoins(self, edges: List[List[int]], cost: List[int]) -> List[int]: ```
my_solution = Solution() test_input = { "edges": [[0,1],[0,2],[0,3],[0,4],[0,5]], "cost": [1,2,3,4,5,6] } assert my_solution.placedCoins(**test_input) == [120,1,1,1,1,1] test_input = { "edges": [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], "cost": [1,4,2,3,5,7,8,-4,2] } assert my_solution.placedCoins(**test_input) == [280,140,32,1,1,1,1,1,1] test_input = { "edges": [[0,1],[0,2]], "cost": [1,2,-2] } assert my_solution.placedCoins(**test_input) == [0,1,1] test_input = { "edges": [[0,1]], "cost": [1,2] } assert my_solution.placedCoins(**test_input) == [1,1] test_input = { "edges": [[0,1]], "cost": [2,1] } assert my_solution.placedCoins(**test_input) == [1,1] test_input = { "edges": [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[0,21],[0,22],[0,23],[0,24],[0,25],[0,26],[0,27],[0,28],[0,29],[0,30],[0,31],[0,32],[0,33],[0,34],[0,35],[0,36],[0,37],[0,38],[0,39],[0,40],[0,41],[0,42],[0,43],[0,44],[0,45],[0,46],[0,47],[0,48],[0,49],[0,50],[0,51],[0,52],[0,53],[0,54],[0,55],[0,56],[0,57],[0,58],[0,59],[0,60],[0,61],[0,62],[0,63],[0,64],[0,65],[0,66],[0,67],[0,68],[0,69],[0,70],[0,71],[0,72],[0,73],[0,74],[0,75],[0,76],[0,77],[0,78],[0,79],[0,80],[0,81],[0,82],[0,83],[0,84],[0,85],[0,86],[0,87],[0,88],[0,89],[0,90],[0,91],[0,92],[0,93],[0,94],[0,95],[0,96],[0,97],[0,98],[0,99]], "cost": [-5959,602,-6457,7055,-1462,6347,7226,-8422,-6088,2997,-7909,6433,5217,3294,-3792,7463,8538,-3811,5009,151,5659,4458,-1702,-1877,2799,9861,-9668,-1765,2181,-8128,7046,9529,6202,-8026,6464,1345,121,1922,7274,-1227,-9914,3025,1046,-9368,-7368,6205,-6342,8091,-6732,-7620,3276,5136,6871,4823,-1885,-4005,-3974,-2725,-3845,-8508,7201,-9566,-7236,-3386,4021,6793,-8759,5066,5879,-5171,1011,1242,8536,-8405,-9646,-214,2251,-9934,-8820,6206,1006,1318,-9712,7230,5608,-4601,9185,346,3056,8913,-2454,-3445,-4295,4802,-8852,-6121,-4538,-5580,-9246,-6462] } assert my_solution.placedCoins(**test_input) == [971167251036,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] test_input = { "edges": [[0,1],[0,2],[2,3]], "cost": [10000,-10000,10000,-10000] } assert my_solution.placedCoins(**test_input) == [1000000000000,1,1,1] test_input = { "edges": [[0,2],[0,6],[1,4],[3,5],[7,6],[3,6],[1,8],[3,1],[9,3]], "cost": [63,13,-6,20,56,-14,61,25,-99,54] } assert my_solution.placedCoins(**test_input) == [215208,0,1,77616,1,1,184464,1,1,1] test_input = { "edges": [[7,0],[4,3],[4,8],[1,5],[6,2],[2,7],[7,9],[1,8],[1,9]], "cost": [37,-48,30,-67,-84,36,-96,24,29,38] } assert my_solution.placedCoins(**test_input) == [306432,202608,1,1,1,1,1,306432,163212,213864] test_input = { "edges": [[0,2],[2,7],[3,4],[5,4],[5,8],[7,6],[7,1],[8,1],[1,9]], "cost": [-18,15,-82,-85,63,-89,60,63,20,18] } assert my_solution.placedCoins(**test_input) == [476595,476595,476595,1,1,476595,1,476595,476595,1] test_input = { "edges": [[2,0],[1,4],[3,8],[4,9],[6,8],[7,2],[2,8],[5,2],[5,9]], "cost": [-9,46,17,34,43,92,41,-50,4,76] } assert my_solution.placedCoins(**test_input) == [321632,1,321632,1,1,321632,1,1,5576,150328] test_input = { "edges": [[0,6],[3,2],[3,1],[6,1],[8,7],[7,1],[5,1],[5,4],[9,4]], "cost": [86,50,10,-54,-85,-65,54,-19,39,37] } assert my_solution.placedCoins(**test_input) == [475150,276250,1,1,1,204425,298350,1,1,1] test_input = { "edges": [[0,8],[8,1],[9,2],[4,6],[7,4],[3,7],[3,8],[5,8],[5,9]], "cost": [-4,83,-97,40,86,-85,-6,-84,-16,-53] } assert my_solution.placedCoins(**test_input) == [709070,1,1,43344,1,0,1,43344,709070,1] test_input = { "edges": [[4,2],[1,3],[4,5],[7,5],[5,0],[8,1],[0,1],[0,6],[9,6]], "cost": [-72,-18,-27,38,13,-53,43,-95,-100,-77] } assert my_solution.placedCoins(**test_input) == [408500,68400,1,1,1,65455,1,1,1,1] test_input = { "edges": [[0,2],[5,2],[1,5],[7,4],[4,8],[1,8],[1,6],[6,3],[9,3]], "cost": [6,93,59,-14,1,-71,-87,95,16,-12] } assert my_solution.placedCoins(**test_input) == [586815,141360,586815,1,1,586815,0,1,1520,1] test_input = { "edges": [[0,1],[5,4],[4,2],[7,2],[7,3],[8,6],[3,6],[3,1],[9,1]], "cost": [66,-54,74,18,-77,-57,49,-82,-71,80] } assert my_solution.placedCoins(**test_input) == [505120,505120,324786,467236,1,1,1,467236,1,1] test_input = { "edges": [[3,8],[2,4],[5,6],[6,7],[0,8],[2,0],[2,6],[1,6],[9,1]], "cost": [92,-71,-10,-70,-56,-47,69,51,100,65] } assert my_solution.placedCoins(**test_input) == [634800,1,274344,1,1,1,230253,1,1,1] test_input = { "edges": [[0,3],[2,8],[5,6],[9,5],[7,9],[8,1],[1,4],[4,3],[9,3]], "cost": [-71,66,86,99,50,-29,-30,27,16,-65] } assert my_solution.placedCoins(**test_input) == [561924,90816,1,561924,283800,1,1,1,1,52650] test_input = { "edges": [[1,0],[2,7],[6,3],[0,4],[6,5],[5,8],[0,7],[9,0],[9,8]], "cost": [40,8,43,31,-27,-21,-21,55,-36,75] } assert my_solution.placedCoins(**test_input) == [177375,1,1,1,1,13671,1,1,23436,56700] test_input = { "edges": [[9,0],[9,3],[4,5],[1,6],[1,5],[8,5],[9,7],[8,2],[2,9]], "cost": [64,35,-1,-28,-50,38,-77,-13,-72,35] } assert my_solution.placedCoins(**test_input) == [354816,1,210672,1,1,146300,1,1,210672,210672] test_input = { "edges": [[5,3],[4,2],[5,2],[6,5],[5,1],[0,1],[0,8],[9,7],[9,8]], "cost": [-100,44,-76,55,8,-8,38,26,-41,-83] } assert my_solution.placedCoins(**test_input) == [456500,91960,1,1,1,33440,1,1,88478,1] test_input = { "edges": [[0,8],[9,3],[4,5],[8,5],[2,6],[7,8],[2,8],[1,2],[9,1]], "cost": [36,33,52,-24,7,-82,63,85,21,-64] } assert my_solution.placedCoins(**test_input) == [446080,50688,108108,1,1,1,1,1,446080,1] test_input = { "edges": [[9,2],[1,3],[8,4],[1,5],[1,9],[8,6],[7,9],[8,0],[0,9]], "cost": [-67,-82,-2,32,-97,6,-85,14,8,1] } assert my_solution.placedCoins(**test_input) == [263840,0,1,1,1,1,1,1,65960,5248] test_input = { "edges": [[7,0],[3,1],[7,3],[4,8],[8,5],[7,6],[2,6],[2,9],[8,9]], "cost": [-48,-69,-25,-65,65,51,85,34,17,-28] } assert my_solution.placedCoins(**test_input) == [381225,1,56355,1,1,1,281775,381225,56355,56355] test_input = { "edges": [[4,2],[7,2],[3,5],[6,3],[3,0],[7,0],[1,8],[1,0],[9,0]], "cost": [-14,39,40,-76,-69,66,43,82,-66,-45] } assert my_solution.placedCoins(**test_input) == [430008,1,1,0,1,1,1,0,1,1] test_input = { "edges": [[1,9],[3,7],[0,6],[7,0],[8,5],[2,5],[4,2],[4,0],[0,9]], "cost": [34,-87,-34,87,58,76,5,43,14,-45] } assert my_solution.placedCoins(**test_input) == [383496,1,0,1,61712,1,1,1,1,1] test_input = { "edges": [[7,1],[2,7],[4,3],[9,5],[6,7],[3,6],[8,3],[0,3],[0,9]], "cost": [-9,67,10,-67,91,29,-4,-35,60,-84] } assert my_solution.placedCoins(**test_input) == [512148,1,1,365820,1,1,9380,0,1,1] test_input = { "edges": [[2,3],[3,6],[3,7],[8,5],[1,5],[4,1],[4,0],[0,3],[9,3]], "cost": [88,-73,8,-82,64,-14,56,-82,-2,-89] } assert my_solution.placedCoins(**test_input) == [642224,0,1,408688,65408,1,1,1,1,1] test_input = { "edges": [[8,4],[7,3],[3,0],[0,6],[6,8],[8,2],[5,2],[1,5],[1,9]], "cost": [-50,34,51,-69,-28,72,61,-76,-30,76] } assert my_solution.placedCoins(**test_input) == [398544,1,279072,1,1,186048,333792,1,279072,1] test_input = { "edges": [[5,0],[1,6],[7,2],[4,9],[3,5],[3,7],[8,7],[6,7],[6,9]], "cost": [-75,-39,-30,-69,84,-7,98,92,-42,-51] } assert my_solution.placedCoins(**test_input) == [757344,1,1,757344,1,757344,194922,757344,1,1] test_input = { "edges": [[0,8],[2,5],[3,5],[1,6],[4,1],[8,4],[7,8],[8,5],[9,5]], "cost": [81,-76,-61,1,39,-3,-21,-33,42,-78] } assert my_solution.placedCoins(**test_input) == [480168,1,1,1,62244,4758,1,1,248976,1] test_input = { "edges": [[7,1],[0,2],[0,3],[6,5],[6,0],[0,7],[7,4],[9,4],[8,9]], "cost": [-37,-11,71,-57,-2,-78,87,55,-21,-66] } assert my_solution.placedCoins(**test_input) == [447876,1,1,1,0,1,1,76230,1,1] test_input = { "edges": [[0,9],[8,2],[3,7],[6,4],[1,5],[8,1],[6,8],[6,7],[7,9]], "cost": [61,-53,-97,-86,-91,-32,-8,79,100,75] } assert my_solution.placedCoins(**test_input) == [882700,1,1,1,1,1,882700,882700,514100,882700] test_input = { "edges": [[8,3],[4,6],[2,5],[2,1],[6,8],[8,0],[0,7],[1,7],[1,9]], "cost": [-40,-4,60,-47,35,39,-8,-12,-29,-43] } assert my_solution.placedCoins(**test_input) == [121260,10320,1,1,1,1,1,30960,47705,1] test_input = { "edges": [[3,0],[1,7],[6,3],[7,5],[4,5],[4,6],[6,9],[2,8],[9,2]], "cost": [36,84,5,32,-36,86,-35,58,36,-100] } assert my_solution.placedCoins(**test_input) == [418992,1,1,418992,418992,418992,418992,1,1,0] test_input = { "edges": [[1,0],[3,0],[2,5],[3,5],[6,4],[4,9],[7,3],[8,3],[3,9]], "cost": [39,22,-9,-65,9,-53,83,-94,-34,12] } assert my_solution.placedCoins(**test_input) == [507130,1,1,507130,1,1,1,1,1,8964] test_input = { "edges": [[2,0],[7,0],[4,3],[5,8],[7,9],[8,1],[3,1],[3,6],[9,6]], "cost": [-68,1,-68,65,-45,-26,36,-3,-85,40] } assert my_solution.placedCoins(**test_input) == [375700,2210,1,248625,1,1,248625,248625,1,248625] test_input = { "edges": [[4,0],[7,2],[2,6],[8,6],[5,6],[4,5],[4,3],[1,3],[1,9]], "cost": [-45,26,-26,-34,-33,81,-33,55,-87,52] } assert my_solution.placedCoins(**test_input) == [317115,1,1,0,239598,232551,157905,1,1,1] test_input = { "edges": [[2,1],[0,1],[6,3],[5,4],[6,5],[0,6],[9,0],[8,7],[9,7]], "cost": [94,76,-92,61,27,78,-94,39,-12,77] } assert my_solution.placedCoins(**test_input) == [812912,1,1,1,1,1,128466,1,1,0] test_input = { "edges": [[8,0],[3,2],[6,2],[1,2],[1,5],[4,7],[9,4],[8,5],[9,5]], "cost": [-6,-89,62,-82,-78,97,-71,58,-43,12] } assert my_solution.placedCoins(**test_input) == [707906,452476,360964,1,1,707906,1,1,707906,0] test_input = { "edges": [[1,0],[2,0],[4,0],[4,3],[4,6],[6,5],[7,5],[7,8],[9,8]], "cost": [-35,4,-1,-44,-27,-93,10,55,-14,93] } assert my_solution.placedCoins(**test_input) == [380556,1,1,1,380556,121086,121086,0,1,1] test_input = { "edges": [[0,6],[1,5],[8,2],[8,3],[9,6],[4,7],[5,8],[5,4],[4,9]], "cost": [26,22,32,-27,54,44,-58,-88,79,-77] } assert my_solution.placedCoins(**test_input) == [535304,1,1,1,187704,111232,535304,1,0,535304] test_input = { "edges": [[7,2],[3,1],[5,1],[8,5],[6,9],[7,0],[0,4],[4,8],[9,4]], "cost": [-70,-80,17,23,19,-71,84,-52,-21,-44] } assert my_solution.placedCoins(**test_input) == [477120,1,1,1,477120,130640,1,1,130640,1] test_input = { "edges": [[2,1],[3,6],[0,6],[7,2],[5,2],[5,4],[4,0],[0,8],[9,0]], "cost": [-27,11,-5,22,-81,48,-28,-85,85,-44] } assert my_solution.placedCoins(**test_input) == [585225,1,4675,1,330480,20400,1,1,1,1] test_input = { "edges": [[8,1],[2,5],[4,3],[8,5],[5,6],[6,3],[0,3],[7,0],[9,7]], "cost": [79,-73,75,-96,61,87,-74,69,84,41] } assert my_solution.placedCoins(**test_input) == [618048,1,1,618048,1,548100,548100,1,1,1] test_input = { "edges": [[1,0],[2,0],[3,0],[4,0],[0,7],[8,0],[8,6],[5,6],[5,9]], "cost": [-97,-61,-67,60,-75,-85,-21,75,-89,59] } assert my_solution.placedCoins(**test_input) == [647475,1,1,1,1,1,105315,1,446335,1] test_input = { "edges": [[0,1],[0,4],[9,3],[9,4],[5,2],[2,7],[6,8],[8,7],[8,9]], "cost": [-88,-7,-16,-49,-53,46,-19,38,-2,-12] } assert my_solution.placedCoins(**test_input) == [214544,1,1,1,119462,1,1,0,13984,42826] test_input = { "edges": [[2,0],[4,6],[4,5],[1,5],[3,1],[3,2],[2,7],[8,7],[7,9]], "cost": [-71,-13,56,3,95,74,79,81,-50,-24] } assert my_solution.placedCoins(**test_input) == [607905,555370,607905,555370,1,555370,1,97200,1,1] test_input = { "edges": [[3,2],[7,3],[4,8],[1,5],[6,7],[0,7],[8,1],[0,1],[9,0]], "cost": [25,-2,22,-40,-53,-17,-97,-49,29,36] } assert my_solution.placedCoins(**test_input) == [185076,26129,1,1,1,1,1,104566,1,1] test_input = { "edges": [[1,8],[2,6],[7,2],[7,3],[8,3],[8,4],[5,4],[5,0],[9,0]], "cost": [-54,-16,51,-79,73,-83,-54,5,45,14] } assert my_solution.placedCoins(**test_input) == [478661,1,1,217566,311418,478661,1,0,217566,1] test_input = { "edges": [[3,1],[4,0],[1,5],[6,1],[6,0],[9,7],[0,8],[0,2],[2,9]], "cost": [23,-50,-5,-36,-49,49,39,-82,-8,81] } assert my_solution.placedCoins(**test_input) == [332100,88200,33210,1,1,1,88200,1,1,1] test_input = { "edges": [[0,7],[8,1],[5,7],[8,2],[2,3],[3,7],[4,7],[4,6],[6,9]], "cost": [-12,-83,-3,-43,12,48,-46,19,-92,69] } assert my_solution.placedCoins(**test_input) == [526884,1,0,0,0,1,1,526884,1,1] test_input = { "edges": [[0,1],[2,5],[7,2],[7,6],[1,8],[1,3],[3,7],[4,7],[9,4]], "cost": [56,61,-17,-3,-100,-28,81,42,1,-86] } assert my_solution.placedCoins(**test_input) == [696600,696600,1,696600,1,1,1,696600,1,1] test_input = { "edges": [[0,3],[8,1],[3,6],[5,3],[2,8],[4,2],[4,7],[7,5],[5,9]], "cost": [-42,72,54,-46,57,95,94,21,-19,-92] } assert my_solution.placedCoins(**test_input) == [642960,1,0,642960,221616,389880,1,221616,1,1] test_input = { "edges": [[2,0],[2,4],[6,5],[8,6],[3,7],[3,1],[1,8],[2,1],[2,9]], "cost": [-58,-82,-70,33,20,-40,21,-93,18,-6] } assert my_solution.placedCoins(**test_input) == [251658,251658,251658,1,1,1,1,1,0,1] test_input = { "edges": [[1,5],[3,0],[0,2],[2,4],[5,2],[7,2],[6,7],[8,7],[7,9]], "cost": [-96,-98,41,59,-69,-51,-78,43,-40,-8] } assert my_solution.placedCoins(**test_input) == [555072,1,328692,1,1,1,1,134160,1,1] test_input = { "edges": [[0,4],[1,4],[3,6],[5,7],[4,6],[7,2],[4,2],[4,9],[9,8]], "cost": [-98,-100,-37,62,38,-54,56,56,1,-72] } assert my_solution.placedCoins(**test_input) == [607600,1,111888,1,446400,1,1,1,1,1] test_input = { "edges": [[8,0],[4,3],[6,3],[5,7],[6,8],[8,2],[1,2],[7,1],[9,7]], "cost": [-70,-59,-87,-64,56,-15,-62,-48,-58,-85] } assert my_solution.placedCoins(**test_input) == [414120,0,0,1,1,1,222208,0,414120,1] test_input = { "edges": [[3,0],[1,8],[6,2],[5,3],[4,5],[8,4],[7,8],[8,6],[6,9]], "cost": [-56,-14,-44,-2,31,34,-61,53,-39,-21] } assert my_solution.placedCoins(**test_input) == [181048,1,1,142252,142252,142252,0,1,142252,1] test_input = { "edges": [[2,1],[1,3],[5,8],[9,6],[8,1],[1,4],[4,0],[7,0],[7,9]], "cost": [-18,-10,25,-60,-48,4,14,38,26,16] } assert my_solution.placedCoins(**test_input) == [109440,15600,1,1,74880,1,1,8512,1,1] test_input = { "edges": [[0,2],[2,5],[3,4],[5,3],[6,8],[7,1],[5,1],[5,9],[8,9]], "cost": [46,96,34,76,19,29,-36,48,-71,-45] } assert my_solution.placedCoins(**test_input) == [350208,1,350208,1,1,350208,1,1,1,0] test_input = { "edges": [[0,2],[7,2],[3,4],[4,7],[5,1],[6,1],[7,6],[9,6],[8,9]], "cost": [4,70,65,-34,-59,-70,-83,-21,66,-10] } assert my_solution.placedCoins(**test_input) == [406700,1,406700,1,1,1,406700,406700,1,1] test_input = { "edges": [[0,1],[2,3],[5,2],[9,2],[6,9],[9,7],[1,8],[4,1],[9,4]], "cost": [54,72,-52,45,-62,96,-54,28,-76,86] } assert my_solution.placedCoins(**test_input) == [594432,594432,0,1,371520,1,1,1,1,371520] test_input = { "edges": [[1,0],[0,4],[3,5],[6,7],[6,3],[3,8],[8,0],[0,2],[9,2]], "cost": [-98,-85,82,-30,64,-76,36,-54,84,85] } assert my_solution.placedCoins(**test_input) == [708050,1,1,147744,1,1,1,1,344736,1] test_input = { "edges": [[0,8],[2,6],[4,7],[5,1],[1,9],[3,7],[3,8],[6,8],[9,6]], "cost": [60,-76,-76,38,-5,-33,-80,-36,28,63] } assert my_solution.placedCoins(**test_input) == [383040,1,1,6840,1,1,383040,1,383040,158004] test_input = { "edges": [[5,3],[6,5],[7,6],[6,2],[2,1],[0,1],[0,9],[4,8],[4,9]], "cost": [-56,-92,-10,-70,52,22,43,37,88,48] } assert my_solution.placedCoins(**test_input) == [566720,276920,35002,1,1,1,35002,1,1,219648] test_input = { "edges": [[8,2],[9,4],[1,7],[3,1],[3,5],[5,8],[5,0],[0,6],[9,6]], "cost": [-70,17,-31,41,-93,17,-19,21,-66,-29] } assert my_solution.placedCoins(**test_input) == [266910,1,1,14637,1,83886,0,1,1,1] test_input = { "edges": [[8,1],[2,4],[7,3],[2,3],[2,6],[6,5],[0,5],[0,8],[9,8]], "cost": [88,86,55,-61,3,-70,12,44,-92,-72] } assert my_solution.placedCoins(**test_input) == [582912,1,7260,1,1,234850,29040,1,569664,1] test_input = { "edges": [[0,7],[1,3],[1,6],[4,2],[6,2],[5,6],[5,9],[7,8],[9,8]], "cost": [15,78,-48,58,-27,28,60,-9,-64,-71] } assert my_solution.placedCoins(**test_input) == [354432,1,1,1,1,271440,271440,354432,354432,271440] test_input = { "edges": [[4,0],[8,0],[5,6],[8,6],[6,1],[7,1],[3,7],[2,3],[9,2]], "cost": [14,10,10,-73,-43,19,92,62,16,-27] } assert my_solution.placedCoins(**test_input) == [288788,122202,1,19710,1,1,181332,122202,181332,1] test_input = { "edges": [[8,0],[3,1],[1,2],[8,5],[6,7],[4,7],[9,4],[2,8],[2,9]], "cost": [97,35,-74,5,65,5,86,61,-55,53] } assert my_solution.placedCoins(**test_input) == [542230,1,340990,1,340990,1,1,1,350020,340990] test_input = { "edges": [[0,1],[7,0],[8,2],[3,4],[4,7],[4,8],[4,6],[5,6],[9,5]], "cost": [-74,40,73,-97,-62,9,-96,-98,-38,63] } assert my_solution.placedCoins(**test_input) == [693938,1,1,1,679776,1,0,693938,1,1] test_input = { "edges": [[2,3],[0,3],[0,6],[6,5],[7,1],[1,5],[8,5],[4,5],[9,4]], "cost": [4,-30,59,61,78,-22,-24,85,-19,-89] } assert my_solution.placedCoins(**test_input) == [404430,1,1,1,1,226950,226950,1,1,1] test_input = { "edges": [[1,2],[5,4],[5,6],[7,1],[3,1],[3,5],[5,8],[0,5],[0,9]], "cost": [11,-80,95,64,-76,56,61,22,13,-58] } assert my_solution.placedCoins(**test_input) == [577600,0,1,133760,1,577600,1,1,1,1] test_input = { "edges": [[1,9],[2,7],[3,5],[4,3],[0,4],[0,6],[8,0],[7,0],[9,7]], "cost": [-69,18,-39,-59,-48,-65,97,1,74,-63] } assert my_solution.placedCoins(**test_input) == [435045,1,1,1,0,1,1,44226,1,1] test_input = { "edges": [[0,6],[1,4],[5,4],[6,8],[3,8],[5,3],[5,2],[7,2],[9,7]], "cost": [-84,27,16,75,49,4,72,46,-17,48] } assert my_solution.placedCoins(**test_input) == [264600,1,35328,176400,1,108192,264600,1,176400,1] test_input = { "edges": [[0,9],[1,7],[6,3],[8,7],[7,3],[5,3],[2,5],[2,4],[4,9]], "cost": [1,-53,88,-67,-55,-31,-89,-39,21,-96] } assert my_solution.placedCoins(**test_input) == [751872,1,524744,125223,524744,125223,1,43407,1,751872] test_input = { "edges": [[3,6],[0,6],[8,0],[7,4],[4,1],[2,1],[2,9],[8,5],[9,5]], "cost": [78,-10,-51,-50,-55,-72,-7,31,-94,4] } assert my_solution.placedCoins(**test_input) == [527904,17050,86955,1,1,122760,1,1,209808,86955] test_input = { "edges": [[2,5],[4,7],[4,5],[5,3],[3,6],[6,0],[1,0],[8,1],[9,8]], "cost": [-19,93,-23,-86,54,-70,-70,9,69,13] } assert my_solution.placedCoins(**test_input) == [559860,83421,1,325080,1,86940,325080,1,1,1] test_input = { "edges": [[2,7],[3,9],[7,5],[4,5],[4,1],[6,1],[6,0],[8,0],[0,9]], "cost": [45,-53,-16,-26,99,50,33,-57,-97,74] } assert my_solution.placedCoins(**test_input) == [547371,299079,1,1,90288,45600,299079,1,1,1] test_input = { "edges": [[5,0],[9,1],[3,6],[2,4],[2,6],[6,9],[8,7],[7,5],[5,9]], "cost": [-14,-4,-49,52,-45,77,-17,-79,21,-33] } assert my_solution.placedCoins(**test_input) == [298067,1,1,1,1,298067,114660,1,1,114660] test_input = { "edges": [[6,3],[7,0],[2,0],[2,4],[9,4],[1,8],[1,6],[5,6],[9,5]], "cost": [-58,-55,17,68,37,-32,91,-63,79,69] } assert my_solution.placedCoins(**test_input) == [496041,1,496041,1,496041,488852,488852,1,1,496041] test_input = { "edges": [[1,8],[0,3],[2,4],[0,2],[8,5],[0,5],[7,0],[7,6],[9,6]], "cost": [-8,-99,36,31,94,5,-35,54,33,19] } assert my_solution.placedCoins(**test_input) == [325710,1,1,1,1,0,1,0,1,1] test_input = { "edges": [[1,0],[1,7],[3,4],[4,5],[5,6],[7,5],[5,2],[8,2],[2,9]], "cost": [70,-75,-13,30,-87,20,-67,76,20,-30] } assert my_solution.placedCoins(**test_input) == [495900,495900,7800,1,1,174870,1,443004,1,1] test_input = { "edges": [[5,1],[2,1],[0,2],[6,0],[0,3],[3,7],[9,7],[4,8],[9,4]], "cost": [-10,-31,-7,-77,64,-80,-53,37,1,10] } assert my_solution.placedCoins(**test_input) == [394240,1,0,23680,1,1,1,23680,1,640] test_input = { "edges": [[2,0],[1,2],[2,8],[4,3],[4,7],[5,9],[6,9],[8,7],[7,9]], "cost": [-80,77,-18,-72,11,66,82,80,32,61] } assert my_solution.placedCoins(**test_input) == [505120,1,505120,1,1,1,1,432960,432960,330132] test_input = { "edges": [[0,3],[1,0],[1,8],[5,4],[9,6],[7,4],[9,4],[8,2],[9,2]], "cost": [70,-14,-91,98,-12,30,-24,79,-62,11] } assert my_solution.placedCoins(**test_input) == [552916,445718,172536,1,0,1,1,1,445718,26070] test_input = { "edges": [[5,8],[2,6],[2,0],[0,3],[7,8],[8,4],[3,4],[1,3],[1,9]], "cost": [55,69,29,87,27,-35,-83,71,-82,-8] } assert my_solution.placedCoins(**test_input) == [592122,1,1,426213,203770,1,1,1,203770,1] test_input = { "edges": [[1,9],[5,4],[5,3],[6,3],[6,2],[2,8],[0,7],[0,9],[8,9]], "cost": [-25,28,47,-75,-78,-39,23,93,-20,61] } assert my_solution.placedCoins(**test_input) == [544050,1,274950,0,1,1,134550,1,274950,356850] test_input = { "edges": [[5,0],[1,8],[6,4],[4,9],[7,3],[8,5],[3,5],[3,2],[9,2]], "cost": [-11,-91,-54,53,58,16,-60,85,20,51] } assert my_solution.placedCoins(**test_input) == [464100,1,187920,275400,1,464100,1,1,1,0] test_input = { "edges": [[1,3],[1,4],[2,4],[6,5],[7,5],[0,5],[9,0],[8,2],[9,2]], "cost": [-74,26,99,58,42,-55,-1,-56,29,-35] } assert my_solution.placedCoins(**test_input) == [410256,1,241164,1,63336,0,1,1,1,241164] test_input = { "edges": [[2,5],[5,3],[0,4],[5,9],[6,1],[7,1],[7,0],[0,9],[8,9]], "cost": [72,68,-18,95,87,-58,-55,20,40,2] } assert my_solution.placedCoins(**test_input) == [595080,1,1,1,1,99180,1,0,1,99180] test_input = { "edges": [[1,6],[3,2],[9,3],[6,5],[5,9],[7,0],[4,8],[0,4],[0,9]], "cost": [85,19,-56,-71,41,-72,59,30,55,67] } assert my_solution.placedCoins(**test_input) == [434520,1,1,1,1,0,1,1,1,342504] test_input = { "edges": [[2,1],[7,1],[0,3],[8,4],[0,5],[7,6],[0,6],[8,0],[9,8]], "cost": [13,4,84,36,29,-97,-59,-40,77,41] } assert my_solution.placedCoins(**test_input) == [480732,1,1,1,1,1,198240,0,91553,1] test_input = { "edges": [[2,1],[4,5],[6,3],[1,3],[1,0],[7,0],[8,7],[5,8],[9,5]], "cost": [85,64,-67,-60,5,-14,31,-84,47,-36] } assert my_solution.placedCoins(**test_input) == [478380,257280,1,1,1,2520,1,142128,23688,1] test_input = { "edges": [[6,1],[4,2],[7,4],[5,7],[0,8],[6,0],[3,6],[7,3],[7,9]], "cost": [-51,-55,82,25,-53,13,-15,98,39,29] } assert my_solution.placedCoins(**test_input) == [313404,1,1,233044,1,1,285670,233044,1,1] test_input = { "edges": [[0,2],[5,1],[6,2],[7,3],[5,4],[5,9],[6,9],[7,9],[8,9]], "cost": [-96,-75,-58,26,-73,-25,-9,87,57,3] } assert my_solution.placedCoins(**test_input) == [626400,1,476325,1,1,0,476325,1,1,476325] test_input = { "edges": [[0,4],[0,9],[5,1],[2,6],[3,7],[3,8],[1,8],[1,2],[2,9]], "cost": [-58,20,21,77,-96,53,-77,-66,-32,42] } assert my_solution.placedCoins(**test_input) == [569184,162624,391314,1,1,1,1,1,162624,391314] test_input = { "edges": [[2,4],[5,2],[6,3],[3,0],[7,5],[8,5],[1,8],[1,0],[0,9]], "cost": [-59,-25,-25,-78,22,29,9,-12,-11,-5] } assert my_solution.placedCoins(**test_input) == [133458,18125,1,1,1,8700,1,1,8700,1] test_input = { "edges": [[8,1],[3,2],[5,4],[6,3],[0,3],[8,5],[5,7],[7,0],[9,0]], "cost": [86,-87,-96,-74,51,75,-76,74,-2,-60] } assert my_solution.placedCoins(**test_input) == [718272,1,1,0,1,13050,1,283050,1,1]
1,703,341,800
weekly-contest-376-find-missing-and-repeated-values
https://leetcode.com/problems/find-missing-and-repeated-values
find-missing-and-repeated-values
{ "questionId": "3227", "questionFrontendId": "2965", "title": "Find Missing and Repeated Values", "titleSlug": "find-missing-and-repeated-values", "isPaidOnly": false, "difficulty": "Easy", "likes": 90, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的二维整数矩阵 grid,大小为 n * n ,其中的值在 [1, n2] 范围内。除了 a 出现 两次,b 缺失 之外,每个整数都 恰好出现一次 。 任务是找出重复的数字a 和缺失的数字 b 。 返回一个下标从 0 开始、长度为 2 的整数数组 ans ,其中 ans[0] 等于 a ,ans[1] 等于 b 。 示例 1: 输入:grid = [[1,3],[2,2]] 输出:[2,4] 解释:数字 2 重复,数字 4 缺失,所以答案是 [2,4] 。 示例 2: 输入:grid = [[9,1,7],[8,9,2],[3,4,6]] 输出:[9,5] 解释:数字 9 重复,数字 5 缺失,所以答案是 [9,5] 。 提示: 2 <= n == grid.length == grid[i].length <= 50 1 <= grid[i][j] <= n * n 对于所有满足1 <= x <= n * n 的 x ,恰好存在一个 x 与矩阵中的任何成员都不相等。 对于所有满足1 <= x <= n * n 的 x ,恰好存在一个 x 与矩阵中的两个成员相等。 除上述的两个之外,对于所有满足1 <= x <= n * n 的 x ,都恰好存在一对 i, j 满足 0 <= i, j <= n - 1 且 grid[i][j] == x 。 """ class Solution: def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
给你一个下标从 0 开始的二维整数矩阵 grid,大小为 n * n ,其中的值在 [1, n2] 范围内。除了 a 出现 两次,b 缺失 之外,每个整数都 恰好出现一次 。 任务是找出重复的数字a 和缺失的数字 b 。 返回一个下标从 0 开始、长度为 2 的整数数组 ans ,其中 ans[0] 等于 a ,ans[1] 等于 b 。 示例 1: 输入:grid = [[1,3],[2,2]] 输出:[2,4] 解释:数字 2 重复,数字 4 缺失,所以答案是 [2,4] 。 示例 2: 输入:grid = [[9,1,7],[8,9,2],[3,4,6]] 输出:[9,5] 解释:数字 9 重复,数字 5 缺失,所以答案是 [9,5] 。 提示: 2 <= n == grid.length == grid[i].length <= 50 1 <= grid[i][j] <= n * n 对于所有满足1 <= x <= n * n 的 x ,恰好存在一个 x 与矩阵中的任何成员都不相等。 对于所有满足1 <= x <= n * n 的 x ,恰好存在一个 x 与矩阵中的两个成员相等。 除上述的两个之外,对于所有满足1 <= x <= n * n 的 x ,都恰好存在一对 i, j 满足 0 <= i, j <= n - 1 且 grid[i][j] == x 。 请完成下面的代码来解决上述问题: ```python class Solution: def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: ```
my_solution = Solution() test_input = { "grid": [[1,3],[2,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,4] test_input = { "grid": [[9,1,7],[8,9,2],[3,4,6]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,5] test_input = { "grid": [[1,1],[3,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,4] test_input = { "grid": [[1,1],[3,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2] test_input = { "grid": [[1,2],[1,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,4] test_input = { "grid": [[1,2],[1,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3] test_input = { "grid": [[1,2],[3,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,4] test_input = { "grid": [[1,2],[4,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3] test_input = { "grid": [[1,2],[4,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3] test_input = { "grid": [[1,2],[4,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3] test_input = { "grid": [[1,4],[1,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2] test_input = { "grid": [[1,4],[2,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3] test_input = { "grid": [[1,4],[3,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2] test_input = { "grid": [[1,4],[3,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,2] test_input = { "grid": [[1,4],[4,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3] test_input = { "grid": [[2,1],[4,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3] test_input = { "grid": [[2,1],[4,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3] test_input = { "grid": [[2,2],[3,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1] test_input = { "grid": [[2,2],[4,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3] test_input = { "grid": [[2,3],[2,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,4] test_input = { "grid": [[2,3],[4,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,1] test_input = { "grid": [[2,4],[3,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1] test_input = { "grid": [[2,4],[3,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1] test_input = { "grid": [[2,4],[4,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3] test_input = { "grid": [[3,1],[3,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,4] test_input = { "grid": [[3,1],[3,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2] test_input = { "grid": [[3,1],[4,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,2] test_input = { "grid": [[3,3],[1,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2] test_input = { "grid": [[3,4],[2,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1] test_input = { "grid": [[3,4],[2,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1] test_input = { "grid": [[3,4],[3,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2] test_input = { "grid": [[3,4],[4,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,2] test_input = { "grid": [[4,1],[1,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3] test_input = { "grid": [[4,1],[2,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,3] test_input = { "grid": [[4,1],[2,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3] test_input = { "grid": [[4,1],[3,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2] test_input = { "grid": [[4,1],[3,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2] test_input = { "grid": [[4,1],[4,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3] test_input = { "grid": [[4,2],[2,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1] test_input = { "grid": [[4,2],[4,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,3] test_input = { "grid": [[4,3],[1,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,2] test_input = { "grid": [[4,3],[2,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1] test_input = { "grid": [[4,3],[2,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1] test_input = { "grid": [[4,3],[3,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,2] test_input = { "grid": [[4,4],[2,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1] test_input = { "grid": [[1,3,4],[9,7,5],[8,2,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,6] test_input = { "grid": [[1,5,2],[8,4,3],[7,8,6]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,9] test_input = { "grid": [[1,5,8],[2,7,3],[6,1,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,9] test_input = { "grid": [[1,6,1],[4,3,7],[5,2,8]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,9] test_input = { "grid": [[1,6,4],[9,7,5],[7,8,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,3] test_input = { "grid": [[1,6,7],[3,6,8],[9,5,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,2] test_input = { "grid": [[1,7,4],[8,6,2],[8,3,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,9] test_input = { "grid": [[1,7,8],[4,5,6],[3,9,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,2] test_input = { "grid": [[1,8,4],[9,2,7],[6,3,8]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,5] test_input = { "grid": [[1,8,5],[4,3,2],[7,9,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,6] test_input = { "grid": [[1,9,3],[2,7,8],[2,4,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,6] test_input = { "grid": [[1,9,7],[8,4,2],[6,3,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,5] test_input = { "grid": [[2,1,3],[2,9,4],[6,8,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,7] test_input = { "grid": [[2,2,4],[7,5,3],[1,6,8]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,9] test_input = { "grid": [[2,3,9],[5,6,4],[2,8,7]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1] test_input = { "grid": [[2,4,6],[4,8,9],[7,3,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,1] test_input = { "grid": [[2,5,5],[4,8,7],[9,3,6]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,1] test_input = { "grid": [[2,6,4],[6,9,5],[3,7,8]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,1] test_input = { "grid": [[2,6,9],[1,7,9],[4,8,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,3] test_input = { "grid": [[2,7,1],[8,6,2],[9,3,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,5] test_input = { "grid": [[2,7,5],[7,6,4],[1,3,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,8] test_input = { "grid": [[2,7,9],[6,8,1],[4,1,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,3] test_input = { "grid": [[2,9,7],[8,5,1],[6,7,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,3] test_input = { "grid": [[3,4,5],[8,2,4],[6,1,7]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,9] test_input = { "grid": [[3,5,7],[8,6,9],[1,5,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,4] test_input = { "grid": [[3,6,1],[5,9,2],[1,7,8]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,4] test_input = { "grid": [[3,9,4],[3,6,1],[5,7,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,8] test_input = { "grid": [[4,2,6],[3,5,8],[3,1,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,7] test_input = { "grid": [[4,3,2],[6,9,9],[8,7,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,1] test_input = { "grid": [[4,6,5],[3,5,7],[2,8,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,1] test_input = { "grid": [[4,8,7],[4,6,9],[3,2,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [4,5] test_input = { "grid": [[4,9,6],[2,5,8],[3,7,7]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,1] test_input = { "grid": [[5,3,6],[1,4,2],[9,8,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,7] test_input = { "grid": [[5,6,9],[3,7,8],[2,2,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,1] test_input = { "grid": [[5,7,8],[1,3,2],[7,6,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,4] test_input = { "grid": [[6,1,3],[2,4,2],[8,9,7]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,5] test_input = { "grid": [[6,4,2],[3,7,8],[5,6,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,1] test_input = { "grid": [[6,4,5],[7,9,3],[1,2,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,8] test_input = { "grid": [[6,4,8],[8,1,2],[9,3,7]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,5] test_input = { "grid": [[6,9,3],[8,9,7],[5,4,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,1] test_input = { "grid": [[7,2,1],[6,5,3],[2,9,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,8] test_input = { "grid": [[7,2,4],[5,8,7],[9,3,1]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,6] test_input = { "grid": [[7,3,1],[8,9,2],[4,5,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,6] test_input = { "grid": [[7,4,2],[9,1,9],[8,3,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,6] test_input = { "grid": [[7,4,8],[1,1,3],[2,6,9]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [1,5] test_input = { "grid": [[7,5,3],[4,6,3],[9,2,8]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,1] test_input = { "grid": [[7,5,7],[3,1,6],[8,9,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,2] test_input = { "grid": [[8,2,6],[1,8,9],[4,5,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,7] test_input = { "grid": [[8,2,7],[3,5,1],[9,6,3]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [3,4] test_input = { "grid": [[8,6,3],[1,9,5],[5,4,7]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [5,2] test_input = { "grid": [[8,6,5],[3,9,1],[8,7,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,2] test_input = { "grid": [[8,9,6],[6,1,3],[2,7,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [6,4] test_input = { "grid": [[8,9,6],[7,4,2],[7,1,5]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,3] test_input = { "grid": [[9,2,3],[7,6,4],[5,8,8]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [8,1] test_input = { "grid": [[9,2,7],[3,8,7],[1,5,4]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [7,6]
1,702,780,200
weekly-contest-376-divide-array-into-arrays-with-max-difference
https://leetcode.com/problems/divide-array-into-arrays-with-max-difference
divide-array-into-arrays-with-max-difference
{ "questionId": "3241", "questionFrontendId": "2966", "title": "Divide Array Into Arrays With Max Difference", "titleSlug": "divide-array-into-arrays-with-max-difference", "isPaidOnly": false, "difficulty": "Medium", "likes": 95, "dislikes": 20, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 的整数数组 nums,以及一个正整数 k 。 将这个数组划分为一个或多个长度为 3 的子数组,并满足以下条件: nums 中的 每个 元素都必须 恰好 存在于某个子数组中。 子数组中 任意 两个元素的差必须小于或等于 k 。 返回一个 二维数组 ,包含所有的子数组。如果不可能满足条件,就返回一个空数组。如果有多个答案,返回 任意一个 即可。 示例 1: 输入:nums = [1,3,4,8,7,9,3,5,1], k = 2 输出:[[1,1,3],[3,4,5],[7,8,9]] 解释:可以将数组划分为以下子数组:[1,1,3],[3,4,5] 和 [7,8,9] 。 每个子数组中任意两个元素的差都小于或等于 2 。 注意,元素的顺序并不重要。 示例 2: 输入:nums = [1,3,3,2,7,3], k = 3 输出:[] 解释:无法划分数组满足所有条件。 提示: n == nums.length 1 <= n <= 105 n 是 3 的倍数 1 <= nums[i] <= 105 1 <= k <= 105 """ class Solution: def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
给你一个长度为 n 的整数数组 nums,以及一个正整数 k 。 将这个数组划分为一个或多个长度为 3 的子数组,并满足以下条件: nums 中的 每个 元素都必须 恰好 存在于某个子数组中。 子数组中 任意 两个元素的差必须小于或等于 k 。 返回一个 二维数组 ,包含所有的子数组。如果不可能满足条件,就返回一个空数组。如果有多个答案,返回 任意一个 即可。 示例 1: 输入:nums = [1,3,4,8,7,9,3,5,1], k = 2 输出:[[1,1,3],[3,4,5],[7,8,9]] 解释:可以将数组划分为以下子数组:[1,1,3],[3,4,5] 和 [7,8,9] 。 每个子数组中任意两个元素的差都小于或等于 2 。 注意,元素的顺序并不重要。 示例 2: 输入:nums = [1,3,3,2,7,3], k = 3 输出:[] 解释:无法划分数组满足所有条件。 提示: n == nums.length 1 <= n <= 105 n 是 3 的倍数 1 <= nums[i] <= 105 1 <= k <= 105 请完成下面的代码来解决上述问题: ```python class Solution: def divideArray(self, nums: List[int], k: int) -> List[List[int]]: ```
my_solution = Solution() test_input = { "nums": [1,3,4,8,7,9,3,5,1], "k": 2 } assert my_solution.divideArray(**test_input) == [[1,1,3],[3,4,5],[7,8,9]] test_input = { "nums": [1,3,3,2,7,3], "k": 3 } assert my_solution.divideArray(**test_input) == [] test_input = { "nums": [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], "k": 14 } assert my_solution.divideArray(**test_input) == [[2,2,2],[4,5,5],[5,5,7],[7,8,8],[9,9,10],[11,12,12]] test_input = { "nums": [33,26,4,18,16,24,24,15,8,18,34,20,24,16,3], "k": 16 } assert my_solution.divideArray(**test_input) == [[3,4,8],[15,16,16],[18,18,20],[24,24,24],[26,33,34]] test_input = { "nums": [6,1,8,8,5,8,5,9,8,9,5,8,3,4,6], "k": 7 } assert my_solution.divideArray(**test_input) == [[1,3,4],[5,5,5],[6,6,8],[8,8,8],[8,9,9]] test_input = { "nums": [20,21,34,3,19,2,23,32,20,17,14,13,19,20,6], "k": 15 } assert my_solution.divideArray(**test_input) == [[2,3,6],[13,14,17],[19,19,20],[20,20,21],[23,32,34]] test_input = { "nums": [6,10,5,12,7,11,6,6,12,12,11,7], "k": 2 } assert my_solution.divideArray(**test_input) == [[5,6,6],[6,7,7],[10,11,11],[12,12,12]] test_input = { "nums": [12,15,26,7,10,13,15,5,27,16,14,15], "k": 18 } assert my_solution.divideArray(**test_input) == [[5,7,10],[12,13,14],[15,15,15],[16,26,27]] test_input = { "nums": [12,7,13,10,7,19,11,23,3,3,7,9], "k": 16 } assert my_solution.divideArray(**test_input) == [[3,3,7],[7,7,9],[10,11,12],[13,19,23]] test_input = { "nums": [19,3,23,4,8,1,1,3,26], "k": 7 } assert my_solution.divideArray(**test_input) == [[1,1,3],[3,4,8],[19,23,26]] test_input = { "nums": [11,13,24,11,9,23,16,19,13], "k": 8 } assert my_solution.divideArray(**test_input) == [[9,11,11],[13,13,16],[19,23,24]] test_input = { "nums": [6,12,21,12,6,12,25,20,15,22,11,19,8,4,18,26,17,18,12,5,8], "k": 11 } assert my_solution.divideArray(**test_input) == [[4,5,6],[6,8,8],[11,12,12],[12,12,15],[17,18,18],[19,20,21],[22,25,26]] test_input = { "nums": [15,17,14,3,25,15,11,25,15,16,12,18], "k": 10 } assert my_solution.divideArray(**test_input) == [[3,11,12],[14,15,15],[15,16,17],[18,25,25]] test_input = { "nums": [16,20,16,19,20,13,14,20,14], "k": 10 } assert my_solution.divideArray(**test_input) == [[13,14,14],[16,16,19],[20,20,20]] test_input = { "nums": [2,13,15,14,18,15,3,13,2], "k": 1 } assert my_solution.divideArray(**test_input) == [] test_input = { "nums": [1,14,20,7,17,2,14,1,8], "k": 11 } assert my_solution.divideArray(**test_input) == [[1,1,2],[7,8,14],[14,17,20]] test_input = { "nums": [8,12,19,8,9,19,9,19,9,8,6,9,6,6,12], "k": 3 } assert my_solution.divideArray(**test_input) == [[6,6,6],[8,8,8],[9,9,9],[9,12,12],[19,19,19]] test_input = { "nums": [18,16,17,19,12,25,11,27,11,32,32,17], "k": 20 } assert my_solution.divideArray(**test_input) == [[11,11,12],[16,17,17],[18,19,25],[27,32,32]] test_input = { "nums": [21,11,24,20,17,13,7,20,20,16,24,20,12,17,16,15,7,7,18,15,20], "k": 6 } assert my_solution.divideArray(**test_input) == [[7,7,7],[11,12,13],[15,15,16],[16,17,17],[18,20,20],[20,20,20],[21,24,24]] test_input = { "nums": [6,7,7,6,7,6], "k": 13 } assert my_solution.divideArray(**test_input) == [[6,6,6],[7,7,7]] test_input = { "nums": [11,12,12,5,6,5], "k": 9 } assert my_solution.divideArray(**test_input) == [[5,5,6],[11,12,12]] test_input = { "nums": [5,5,12,5,5,22,2,2,5,2,5,5,16,2,22,2,12,16,15,13,19], "k": 3 } assert my_solution.divideArray(**test_input) == [[2,2,2],[2,2,5],[5,5,5],[5,5,5],[12,12,13],[15,16,16],[19,22,22]] test_input = { "nums": [11,28,12,5,19,15,16,9,21,13,12,9,19,19,18], "k": 9 } assert my_solution.divideArray(**test_input) == [[5,9,9],[11,12,12],[13,15,16],[18,19,19],[19,21,28]] test_input = { "nums": [10,14,17], "k": 15 } assert my_solution.divideArray(**test_input) == [[10,14,17]] test_input = { "nums": [16,15,9,20,17,19,11,18,16], "k": 9 } assert my_solution.divideArray(**test_input) == [[9,11,15],[16,16,17],[18,19,20]] test_input = { "nums": [16,28,16,7,18,13,5,27,27,16,20,22,13,6,17], "k": 11 } assert my_solution.divideArray(**test_input) == [[5,6,7],[13,13,16],[16,16,17],[18,20,22],[27,27,28]] test_input = { "nums": [14,7,13,2,3,7,17,13,13,2,14,7], "k": 3 } assert my_solution.divideArray(**test_input) == [[2,2,3],[7,7,7],[13,13,13],[14,14,17]] test_input = { "nums": [20,8,6,5,10,5,10,2,20,6,12,13,13,20,4], "k": 6 } assert my_solution.divideArray(**test_input) == [[2,4,5],[5,6,6],[8,10,10],[12,13,13],[20,20,20]] test_input = { "nums": [12,14,16,9,20,18,16,4,24,14,16,30,1,17,30,16,30,6], "k": 13 } assert my_solution.divideArray(**test_input) == [[1,4,6],[9,12,14],[14,16,16],[16,16,17],[18,20,24],[30,30,30]] test_input = { "nums": [13,6,19,21,16,11,1,14,7], "k": 20 } assert my_solution.divideArray(**test_input) == [[1,6,7],[11,13,14],[16,19,21]] test_input = { "nums": [13,2,12,22,18,15,3,20,2,18,3,14,2,10,14,9,14,3,14,17,5], "k": 9 } assert my_solution.divideArray(**test_input) == [[2,2,2],[3,3,3],[5,9,10],[12,13,14],[14,14,14],[15,17,18],[18,20,22]] test_input = { "nums": [12,13,12,14,14,6,5,7,23,21,21,16,15,20,22,14,20,7], "k": 10 } assert my_solution.divideArray(**test_input) == [[5,6,7],[7,12,12],[13,14,14],[14,15,16],[20,20,21],[21,22,23]] test_input = { "nums": [15,14,3,19,17,18,19,23,2,16,19,3], "k": 5 } assert my_solution.divideArray(**test_input) == [[2,3,3],[14,15,16],[17,18,19],[19,19,23]] test_input = { "nums": [12,8,18,6,12,6,8,33,20,6,17,17,27,8,12], "k": 16 } assert my_solution.divideArray(**test_input) == [[6,6,6],[8,8,8],[12,12,12],[17,17,18],[20,27,33]] test_input = { "nums": [1,1,23,17,18,1], "k": 12 } assert my_solution.divideArray(**test_input) == [[1,1,1],[17,18,23]] test_input = { "nums": [13,13,3,7,6,13,6,4,3], "k": 1 } assert my_solution.divideArray(**test_input) == [[3,3,4],[6,6,7],[13,13,13]] test_input = { "nums": [19,10,9,20,29,28,29,9,18,27,23,4,16,8,11,19,10,12,10,10,21], "k": 20 } assert my_solution.divideArray(**test_input) == [[4,8,9],[9,10,10],[10,10,11],[12,16,18],[19,19,20],[21,23,27],[28,29,29]] test_input = { "nums": [13,12,12,11,22,10], "k": 15 } assert my_solution.divideArray(**test_input) == [[10,11,12],[12,13,22]] test_input = { "nums": [15,16,12,34,16,16,24,21,3,24,29,10], "k": 20 } assert my_solution.divideArray(**test_input) == [[3,10,12],[15,16,16],[16,21,24],[24,29,34]] test_input = { "nums": [17,16,17,11,13,6], "k": 19 } assert my_solution.divideArray(**test_input) == [[6,11,13],[16,17,17]] test_input = { "nums": [11,16,16,6,8,20,21,3,20,11,16,6,6,11,6], "k": 3 } assert my_solution.divideArray(**test_input) == [[3,6,6],[6,6,8],[11,11,11],[16,16,16],[20,20,21]] test_input = { "nums": [2,16,8,7,15,16], "k": 9 } assert my_solution.divideArray(**test_input) == [[2,7,8],[15,16,16]] test_input = { "nums": [15,17,22], "k": 14 } assert my_solution.divideArray(**test_input) == [[15,17,22]] test_input = { "nums": [8,4,9,18,18,5,10,11,19,18,19,23,4,15,25,20,20,6], "k": 7 } assert my_solution.divideArray(**test_input) == [[4,4,5],[6,8,9],[10,11,15],[18,18,18],[19,19,20],[20,23,25]] test_input = { "nums": [12,20,16,12,15,16,15,20,14,16,19,13], "k": 1 } assert my_solution.divideArray(**test_input) == [[12,12,13],[14,15,15],[16,16,16],[19,20,20]] test_input = { "nums": [20,19,8,21,13,18,21,12,12,18,9,9], "k": 1 } assert my_solution.divideArray(**test_input) == [[8,9,9],[12,12,13],[18,18,19],[20,21,21]] test_input = { "nums": [6,14,19,17,13,4,17,10,17], "k": 19 } assert my_solution.divideArray(**test_input) == [[4,6,10],[13,14,17],[17,17,19]] test_input = { "nums": [8,8,12], "k": 4 } assert my_solution.divideArray(**test_input) == [[8,8,12]] test_input = { "nums": [3,16,17,18,10,8,20,16,20,10,10,21], "k": 16 } assert my_solution.divideArray(**test_input) == [[3,8,10],[10,10,16],[16,17,18],[20,20,21]] test_input = { "nums": [19,14,17,20,16,16,7,10,18,8,16,15,15,13,12,14,17,11], "k": 8 } assert my_solution.divideArray(**test_input) == [[7,8,10],[11,12,13],[14,14,15],[15,16,16],[16,17,17],[18,19,20]] test_input = { "nums": [18,7,11,13,13,9,22,20,21,13,7,18,8,8,16], "k": 4 } assert my_solution.divideArray(**test_input) == [[7,7,8],[8,9,11],[13,13,13],[16,18,18],[20,21,22]] test_input = { "nums": [10,15,9,15,15,10], "k": 1 } assert my_solution.divideArray(**test_input) == [[9,10,10],[15,15,15]] test_input = { "nums": [16,17,16], "k": 16 } assert my_solution.divideArray(**test_input) == [[16,16,17]] test_input = { "nums": [15,1,15,14,18,17,1,18,12,16,6,6,7,1,12], "k": 4 } assert my_solution.divideArray(**test_input) == [[1,1,1],[6,6,7],[12,12,14],[15,15,16],[17,18,18]] test_input = { "nums": [6,11,6,18,11,13,13,8,11,4,4,11,12,17,11], "k": 12 } assert my_solution.divideArray(**test_input) == [[4,4,6],[6,8,11],[11,11,11],[11,12,13],[13,17,18]] test_input = { "nums": [5,13,4,14,11,18,9,10,20,5,17,11,5,8,20,5,14,4,18,17,17], "k": 8 } assert my_solution.divideArray(**test_input) == [[4,4,5],[5,5,5],[8,9,10],[11,11,13],[14,14,17],[17,17,18],[18,20,20]] test_input = { "nums": [13,6,20,13,12,8,7,12,22,16,13,7,12,17,5], "k": 6 } assert my_solution.divideArray(**test_input) == [[5,6,7],[7,8,12],[12,12,13],[13,13,16],[17,20,22]] test_input = { "nums": [23,2,15,20,18,14,20,7,2,22,4,14,7,9,15,14,2,7], "k": 8 } assert my_solution.divideArray(**test_input) == [[2,2,2],[4,7,7],[7,9,14],[14,14,15],[15,18,20],[20,22,23]] test_input = { "nums": [19,9,2,4,17,2,27,18,17], "k": 18 } assert my_solution.divideArray(**test_input) == [[2,2,4],[9,17,17],[18,19,27]] test_input = { "nums": [5,20,29,4,12,14,31,6,11,2,15,17,15,19,4], "k": 20 } assert my_solution.divideArray(**test_input) == [[2,4,4],[5,6,11],[12,14,15],[15,17,19],[20,29,31]] test_input = { "nums": [15,20,5,24,18,16,25,21,28,12,19,28,25,20,14,18,24,28], "k": 17 } assert my_solution.divideArray(**test_input) == [[5,12,14],[15,16,18],[18,19,20],[20,21,24],[24,25,25],[28,28,28]] test_input = { "nums": [9,6,23,17,7,17], "k": 20 } assert my_solution.divideArray(**test_input) == [[6,7,9],[17,17,23]] test_input = { "nums": [24,23,19], "k": 6 } assert my_solution.divideArray(**test_input) == [[19,23,24]] test_input = { "nums": [6,19,22,7,17,7,15,17,7,18,4,14,9,10,16], "k": 9 } assert my_solution.divideArray(**test_input) == [[4,6,7],[7,7,9],[10,14,15],[16,17,17],[18,19,22]] test_input = { "nums": [4,3,15,1,15,15], "k": 4 } assert my_solution.divideArray(**test_input) == [[1,3,4],[15,15,15]] test_input = { "nums": [10,22,18,15,7,21,6,7,11,9,7,6,7,10,18], "k": 8 } assert my_solution.divideArray(**test_input) == [[6,6,7],[7,7,7],[9,10,10],[11,15,18],[18,21,22]] test_input = { "nums": [16,17,2,17,9,7,22,17,12,4,14,17,4,19,12,18,19,8,17,5,6], "k": 7 } assert my_solution.divideArray(**test_input) == [[2,4,4],[5,6,7],[8,9,12],[12,14,16],[17,17,17],[17,17,18],[19,19,22]] test_input = { "nums": [20,18,18,22,7,9,9,10,16,4,18,18,11,9,18,11,11,21], "k": 7 } assert my_solution.divideArray(**test_input) == [[4,7,9],[9,9,10],[11,11,11],[16,18,18],[18,18,18],[20,21,22]] test_input = { "nums": [5,11,15,9,17,6,16,14,4,9,5,13,10,12,13,15,13,12,16,12,13], "k": 5 } assert my_solution.divideArray(**test_input) == [[4,5,5],[6,9,9],[10,11,12],[12,12,13],[13,13,13],[14,15,15],[16,16,17]] test_input = { "nums": [4,16,17], "k": 20 } assert my_solution.divideArray(**test_input) == [[4,16,17]] test_input = { "nums": [10,9,22,13,17,11,6,9,11], "k": 10 } assert my_solution.divideArray(**test_input) == [[6,9,9],[10,11,11],[13,17,22]] test_input = { "nums": [3,11,19,8,22,23,15,18,37,7,25,20,12,19,7], "k": 18 } assert my_solution.divideArray(**test_input) == [[3,7,7],[8,11,12],[15,18,19],[19,20,22],[23,25,37]] test_input = { "nums": [4,6,6,3,11,11], "k": 16 } assert my_solution.divideArray(**test_input) == [[3,4,6],[6,11,11]] test_input = { "nums": [10,17,10,15,16,8], "k": 7 } assert my_solution.divideArray(**test_input) == [[8,10,10],[15,16,17]] test_input = { "nums": [4,20,4,19,8,7,4,20,7], "k": 3 } assert my_solution.divideArray(**test_input) == [[4,4,4],[7,7,8],[19,20,20]] test_input = { "nums": [4,4,4], "k": 17 } assert my_solution.divideArray(**test_input) == [[4,4,4]] test_input = { "nums": [18,6,15,20,5,27,23,15,26,11,11,4,17,23,11], "k": 15 } assert my_solution.divideArray(**test_input) == [[4,5,6],[11,11,11],[15,15,17],[18,20,23],[23,26,27]] test_input = { "nums": [8,9,5], "k": 15 } assert my_solution.divideArray(**test_input) == [[5,8,9]] test_input = { "nums": [20,15,8,11,11,10,19,7,20], "k": 7 } assert my_solution.divideArray(**test_input) == [[7,8,10],[11,11,15],[19,20,20]] test_input = { "nums": [12,11,18,13,13,21], "k": 11 } assert my_solution.divideArray(**test_input) == [[11,12,13],[13,18,21]] test_input = { "nums": [19,29,11,18,19,17,29,19,7], "k": 14 } assert my_solution.divideArray(**test_input) == [[7,11,17],[18,19,19],[19,29,29]] test_input = { "nums": [14,1,25,1,14,19,2,2,4,16,17,11,26,29,12], "k": 17 } assert my_solution.divideArray(**test_input) == [[1,1,2],[2,4,11],[12,14,14],[16,17,19],[25,26,29]] test_input = { "nums": [14,25,16,11,7,13,12,16,24,19,5,17], "k": 13 } assert my_solution.divideArray(**test_input) == [[5,7,11],[12,13,14],[16,16,17],[19,24,25]] test_input = { "nums": [11,26,19,10,16,10,11,18,9], "k": 11 } assert my_solution.divideArray(**test_input) == [[9,10,10],[11,11,16],[18,19,26]] test_input = { "nums": [16,8,15], "k": 16 } assert my_solution.divideArray(**test_input) == [[8,15,16]] test_input = { "nums": [12,8,18,8,18,13,12,18,18,13,12,23,21,8,13], "k": 5 } assert my_solution.divideArray(**test_input) == [[8,8,8],[12,12,12],[13,13,13],[18,18,18],[18,21,23]] test_input = { "nums": [12,16,9,8,22,16], "k": 16 } assert my_solution.divideArray(**test_input) == [[8,9,12],[16,16,22]] test_input = { "nums": [15,16,18,8,12,7,5,17,23,17,18,13,5,4,13,18,7,20], "k": 6 } assert my_solution.divideArray(**test_input) == [[4,5,5],[7,7,8],[12,13,13],[15,16,17],[17,18,18],[18,20,23]] test_input = { "nums": [12,11,14,13,9,16,31,19,21,22,7,1,22,23,9,2,21,21], "k": 15 } assert my_solution.divideArray(**test_input) == [[1,2,7],[9,9,11],[12,13,14],[16,19,21],[21,21,22],[22,23,31]] test_input = { "nums": [7,15,18,20,6,21,18,17,11,1,14,15,18,8,17,13,11,8,5,12,11], "k": 10 } assert my_solution.divideArray(**test_input) == [[1,5,6],[7,8,8],[11,11,11],[12,13,14],[15,15,17],[17,18,18],[18,20,21]] test_input = { "nums": [13,16,17,16,6,12], "k": 11 } assert my_solution.divideArray(**test_input) == [[6,12,13],[16,16,17]] test_input = { "nums": [17,17,17,16,17,17], "k": 1 } assert my_solution.divideArray(**test_input) == [[16,17,17],[17,17,17]] test_input = { "nums": [6,14,6,15,14,6], "k": 17 } assert my_solution.divideArray(**test_input) == [[6,6,6],[14,14,15]] test_input = { "nums": [23,19,21,10,10,13,15,19,19,3,15,3], "k": 12 } assert my_solution.divideArray(**test_input) == [[3,3,10],[10,13,15],[15,19,19],[19,21,23]] test_input = { "nums": [11,4,3,11,3,27,19,10,6,12,11,24,27,1,31], "k": 17 } assert my_solution.divideArray(**test_input) == [[1,3,3],[4,6,10],[11,11,11],[12,19,24],[27,27,31]] test_input = { "nums": [8,18,18,20,20,19,20,31,7], "k": 17 } assert my_solution.divideArray(**test_input) == [[7,8,18],[18,19,20],[20,20,31]] test_input = { "nums": [4,22,8,12,1,4,4,17,22,4,10,1], "k": 12 } assert my_solution.divideArray(**test_input) == [[1,1,4],[4,4,4],[8,10,12],[17,22,22]] test_input = { "nums": [16,15,16,6,9,22,14,16,10,26,18,16,11,18,7], "k": 10 } assert my_solution.divideArray(**test_input) == [[6,7,9],[10,11,14],[15,16,16],[16,16,18],[18,22,26]] test_input = { "nums": [5,16,12,26,16,18,1,6,23,2,1,21,8,11,9], "k": 14 } assert my_solution.divideArray(**test_input) == [[1,1,2],[5,6,8],[9,11,12],[16,16,18],[21,23,26]] test_input = { "nums": [6,3,24,13,19,24,13,12,15,3,6,3], "k": 17 } assert my_solution.divideArray(**test_input) == [[3,3,3],[6,6,12],[13,13,15],[19,24,24]]
1,702,780,200
weekly-contest-376-minimum-cost-to-make-array-equalindromic
https://leetcode.com/problems/minimum-cost-to-make-array-equalindromic
minimum-cost-to-make-array-equalindromic
{ "questionId": "3229", "questionFrontendId": "2967", "title": "Minimum Cost to Make Array Equalindromic", "titleSlug": "minimum-cost-to-make-array-equalindromic", "isPaidOnly": false, "difficulty": "Medium", "likes": 164, "dislikes": 70, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n下标从 0开始的整数数组nums。 你可以对 nums执行特殊操作 任意次(也可以 0次)。每一次特殊操作中,你需要 按顺序执行以下步骤: 从范围[0, n - 1]里选择一个下标 i和一个 正整数x。 将|nums[i] - x|添加到总代价里。 将 nums[i]变为x。 如果一个正整数正着读和反着读都相同,那么我们称这个数是回文数。比方说,121,2552 和65756都是回文数,但是24,46,235都不是回文数。 如果一个数组中的所有元素都等于一个整数y,且y是一个小于109的回文数,那么我们称这个数组是一个 等数数组。 请你返回一个整数,表示执行任意次特殊操作后使 nums成为 等数数组的 最小总代价。 示例 1: 输入:nums = [1,2,3,4,5] 输出:6 解释:我们可以将数组中所有元素变为回文数 3 得到等数数组,数组变成 [3,3,3,3,3] 需要执行 4 次特殊操作,代价为 |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6 。 将所有元素变为其他回文数的总代价都大于 6 。 示例 2: 输入:nums = [10,12,13,14,15] 输出:11 解释:我们可以将数组中所有元素变为回文数 11 得到等数数组,数组变成 [11,11,11,11,11] 需要执行 5 次特殊操作,代价为 |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11 。 将所有元素变为其他回文数的总代价都大于 11 。 示例 3 : 输入:nums = [22,33,22,33,22] 输出:22 解释:我们可以将数组中所有元素变为回文数 22 得到等数数组,数组变为 [22,22,22,22,22] 需要执行 2 次特殊操作,代价为 |33 - 22| + |33 - 22| = 22 。 将所有元素变为其他回文数的总代价都大于 22 。 提示: 1 <= n <= 105 1 <= nums[i] <= 109 """ class Solution: def minimumCost(self, nums: List[int]) -> int:
给你一个长度为 n下标从 0开始的整数数组nums。 你可以对 nums执行特殊操作 任意次(也可以 0次)。每一次特殊操作中,你需要 按顺序执行以下步骤: 从范围[0, n - 1]里选择一个下标 i和一个 正整数x。 将|nums[i] - x|添加到总代价里。 将 nums[i]变为x。 如果一个正整数正着读和反着读都相同,那么我们称这个数是回文数。比方说,121,2552 和65756都是回文数,但是24,46,235都不是回文数。 如果一个数组中的所有元素都等于一个整数y,且y是一个小于109的回文数,那么我们称这个数组是一个 等数数组。 请你返回一个整数,表示执行任意次特殊操作后使 nums成为 等数数组的 最小总代价。 示例 1: 输入:nums = [1,2,3,4,5] 输出:6 解释:我们可以将数组中所有元素变为回文数 3 得到等数数组,数组变成 [3,3,3,3,3] 需要执行 4 次特殊操作,代价为 |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6 。 将所有元素变为其他回文数的总代价都大于 6 。 示例 2: 输入:nums = [10,12,13,14,15] 输出:11 解释:我们可以将数组中所有元素变为回文数 11 得到等数数组,数组变成 [11,11,11,11,11] 需要执行 5 次特殊操作,代价为 |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11 。 将所有元素变为其他回文数的总代价都大于 11 。 示例 3 : 输入:nums = [22,33,22,33,22] 输出:22 解释:我们可以将数组中所有元素变为回文数 22 得到等数数组,数组变为 [22,22,22,22,22] 需要执行 2 次特殊操作,代价为 |33 - 22| + |33 - 22| = 22 。 将所有元素变为其他回文数的总代价都大于 22 。 提示: 1 <= n <= 105 1 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def minimumCost(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.minimumCost(**test_input) == 6 test_input = { "nums": [10,12,13,14,15] } assert my_solution.minimumCost(**test_input) == 11 test_input = { "nums": [22,33,22,33,22] } assert my_solution.minimumCost(**test_input) == 22 test_input = { "nums": [1] } assert my_solution.minimumCost(**test_input) == 0 test_input = { "nums": [2] } assert my_solution.minimumCost(**test_input) == 0 test_input = { "nums": [3] } assert my_solution.minimumCost(**test_input) == 0 test_input = { "nums": [4] } assert my_solution.minimumCost(**test_input) == 0 test_input = { "nums": [5] } assert my_solution.minimumCost(**test_input) == 0 test_input = { "nums": [2,1] } assert my_solution.minimumCost(**test_input) == 1 test_input = { "nums": [3,1] } assert my_solution.minimumCost(**test_input) == 2 test_input = { "nums": [3,2] } assert my_solution.minimumCost(**test_input) == 1 test_input = { "nums": [4,1] } assert my_solution.minimumCost(**test_input) == 3 test_input = { "nums": [4,2] } assert my_solution.minimumCost(**test_input) == 2 test_input = { "nums": [4,3] } assert my_solution.minimumCost(**test_input) == 1 test_input = { "nums": [5,1] } assert my_solution.minimumCost(**test_input) == 4 test_input = { "nums": [5,2] } assert my_solution.minimumCost(**test_input) == 3 test_input = { "nums": [5,3] } assert my_solution.minimumCost(**test_input) == 2 test_input = { "nums": [5,4] } assert my_solution.minimumCost(**test_input) == 1 test_input = { "nums": [3,2,1] } assert my_solution.minimumCost(**test_input) == 2 test_input = { "nums": [4,2,1] } assert my_solution.minimumCost(**test_input) == 3 test_input = { "nums": [4,3,1] } assert my_solution.minimumCost(**test_input) == 3 test_input = { "nums": [4,3,2] } assert my_solution.minimumCost(**test_input) == 2 test_input = { "nums": [5,2,1] } assert my_solution.minimumCost(**test_input) == 4 test_input = { "nums": [5,3,1] } assert my_solution.minimumCost(**test_input) == 4 test_input = { "nums": [5,3,2] } assert my_solution.minimumCost(**test_input) == 3 test_input = { "nums": [5,4,1] } assert my_solution.minimumCost(**test_input) == 4 test_input = { "nums": [5,4,2] } assert my_solution.minimumCost(**test_input) == 3 test_input = { "nums": [5,4,3] } assert my_solution.minimumCost(**test_input) == 2 test_input = { "nums": [4,3,2,1] } assert my_solution.minimumCost(**test_input) == 4 test_input = { "nums": [5,3,2,1] } assert my_solution.minimumCost(**test_input) == 5 test_input = { "nums": [5,4,2,1] } assert my_solution.minimumCost(**test_input) == 6 test_input = { "nums": [5,4,3,1] } assert my_solution.minimumCost(**test_input) == 5 test_input = { "nums": [5,4,3,2] } assert my_solution.minimumCost(**test_input) == 4 test_input = { "nums": [301,309,312,322] } assert my_solution.minimumCost(**test_input) == 26 test_input = { "nums": [302,306,316,329] } assert my_solution.minimumCost(**test_input) == 37 test_input = { "nums": [302,315,317,320] } assert my_solution.minimumCost(**test_input) == 24 test_input = { "nums": [304,310,324,328] } assert my_solution.minimumCost(**test_input) == 38 test_input = { "nums": [307,321,322,327] } assert my_solution.minimumCost(**test_input) == 23 test_input = { "nums": [307,323,325,330] } assert my_solution.minimumCost(**test_input) == 25 test_input = { "nums": [308,313,319,322] } assert my_solution.minimumCost(**test_input) == 20 test_input = { "nums": [311,313,320,324] } assert my_solution.minimumCost(**test_input) == 20 test_input = { "nums": [313,318,323,328] } assert my_solution.minimumCost(**test_input) == 20 test_input = { "nums": [101,102,105,108,124] } assert my_solution.minimumCost(**test_input) == 35 test_input = { "nums": [101,102,105,120,124] } assert my_solution.minimumCost(**test_input) == 47 test_input = { "nums": [101,102,111,125,126] } assert my_solution.minimumCost(**test_input) == 48 test_input = { "nums": [101,104,107,126,130] } assert my_solution.minimumCost(**test_input) == 55 test_input = { "nums": [101,106,113,120,124] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "nums": [101,115,116,120,122] } assert my_solution.minimumCost(**test_input) == 33 test_input = { "nums": [102,103,105,106,109] } assert my_solution.minimumCost(**test_input) == 20 test_input = { "nums": [102,103,107,125,128] } assert my_solution.minimumCost(**test_input) == 52 test_input = { "nums": [102,103,111,119,125] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "nums": [102,105,120,128,129] } assert my_solution.minimumCost(**test_input) == 51 test_input = { "nums": [103,104,113,116,119] } assert my_solution.minimumCost(**test_input) == 30 test_input = { "nums": [103,105,107,113,125] } assert my_solution.minimumCost(**test_input) == 34 test_input = { "nums": [103,106,113,120,130] } assert my_solution.minimumCost(**test_input) == 43 test_input = { "nums": [103,109,110,118,123] } assert my_solution.minimumCost(**test_input) == 30 test_input = { "nums": [103,110,121,127,129] } assert my_solution.minimumCost(**test_input) == 43 test_input = { "nums": [103,114,120,123,125] } assert my_solution.minimumCost(**test_input) == 32 test_input = { "nums": [103,115,120,122,128] } assert my_solution.minimumCost(**test_input) == 33 test_input = { "nums": [104,107,120,122,124] } assert my_solution.minimumCost(**test_input) == 36 test_input = { "nums": [104,111,121,128,129] } assert my_solution.minimumCost(**test_input) == 42 test_input = { "nums": [104,119,123,125,126] } assert my_solution.minimumCost(**test_input) == 30 test_input = { "nums": [105,108,112,119,130] } assert my_solution.minimumCost(**test_input) == 37 test_input = { "nums": [105,108,122,126,130] } assert my_solution.minimumCost(**test_input) == 44 test_input = { "nums": [105,110,118,123,125] } assert my_solution.minimumCost(**test_input) == 36 test_input = { "nums": [105,116,118,123,129] } assert my_solution.minimumCost(**test_input) == 34 test_input = { "nums": [106,107,114,117,127] } assert my_solution.minimumCost(**test_input) == 34 test_input = { "nums": [106,109,118,124,125] } assert my_solution.minimumCost(**test_input) == 37 test_input = { "nums": [106,111,113,123,129] } assert my_solution.minimumCost(**test_input) == 37 test_input = { "nums": [106,113,116,120,122] } assert my_solution.minimumCost(**test_input) == 30 test_input = { "nums": [107,108,113,114,122] } assert my_solution.minimumCost(**test_input) == 23 test_input = { "nums": [107,109,112,118,128] } assert my_solution.minimumCost(**test_input) == 31 test_input = { "nums": [107,109,115,116,129] } assert my_solution.minimumCost(**test_input) == 33 test_input = { "nums": [107,110,119,125,130] } assert my_solution.minimumCost(**test_input) == 40 test_input = { "nums": [107,112,116,121,124] } assert my_solution.minimumCost(**test_input) == 31 test_input = { "nums": [107,115,116,120,127] } assert my_solution.minimumCost(**test_input) == 32 test_input = { "nums": [107,116,123,125,128] } assert my_solution.minimumCost(**test_input) == 32 test_input = { "nums": [108,110,116,121,130] } assert my_solution.minimumCost(**test_input) == 38 test_input = { "nums": [108,113,114,115,119] } assert my_solution.minimumCost(**test_input) == 20 test_input = { "nums": [108,113,116,124,129] } assert my_solution.minimumCost(**test_input) == 37 test_input = { "nums": [108,115,124,127,129] } assert my_solution.minimumCost(**test_input) == 36 test_input = { "nums": [109,113,115,122,128] } assert my_solution.minimumCost(**test_input) == 34 test_input = { "nums": [110,111,112,126,129] } assert my_solution.minimumCost(**test_input) == 35 test_input = { "nums": [110,113,119,124,125] } assert my_solution.minimumCost(**test_input) == 28 test_input = { "nums": [111,114,117,118,125] } assert my_solution.minimumCost(**test_input) == 28 test_input = { "nums": [112,113,114,120,130] } assert my_solution.minimumCost(**test_input) == 34 test_input = { "nums": [112,115,120,123,129] } assert my_solution.minimumCost(**test_input) == 26 test_input = { "nums": [117,118,119,124,128] } assert my_solution.minimumCost(**test_input) == 19 test_input = { "nums": [201,202,203,205,215,228] } assert my_solution.minimumCost(**test_input) == 44 test_input = { "nums": [201,202,204,205,215,225] } assert my_solution.minimumCost(**test_input) == 42 test_input = { "nums": [201,202,204,207,220,224] } assert my_solution.minimumCost(**test_input) == 48 test_input = { "nums": [201,202,204,208,216,225] } assert my_solution.minimumCost(**test_input) == 46 test_input = { "nums": [201,202,204,218,223,224] } assert my_solution.minimumCost(**test_input) == 58 test_input = { "nums": [201,202,205,210,227,228] } assert my_solution.minimumCost(**test_input) == 61 test_input = { "nums": [201,202,205,211,214,225] } assert my_solution.minimumCost(**test_input) == 44 test_input = { "nums": [201,202,207,210,223,226] } assert my_solution.minimumCost(**test_input) == 53 test_input = { "nums": [201,202,207,212,215,222] } assert my_solution.minimumCost(**test_input) == 39 test_input = { "nums": [201,202,207,216,220,223] } assert my_solution.minimumCost(**test_input) == 49 test_input = { "nums": [201,202,211,213,221,223] } assert my_solution.minimumCost(**test_input) == 43 test_input = { "nums": [201,202,212,213,219,229] } assert my_solution.minimumCost(**test_input) == 46
1,702,780,200
weekly-contest-376-apply-operations-to-maximize-frequency-score
https://leetcode.com/problems/apply-operations-to-maximize-frequency-score
apply-operations-to-maximize-frequency-score
{ "questionId": "3196", "questionFrontendId": "2968", "title": "Apply Operations to Maximize Frequency Score", "titleSlug": "apply-operations-to-maximize-frequency-score", "isPaidOnly": false, "difficulty": "Hard", "likes": 175, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的整数数组nums和一个整数k。 你可以对数组执行 至多k次操作: 从数组中选择一个下标 i,将nums[i] 增加或者减少1。 最终数组的频率分数定义为数组中众数的 频率。 请你返回你可以得到的 最大频率分数。 众数指的是数组中出现次数最多的数。一个元素的频率指的是数组中这个元素的出现次数。 示例 1: 输入:nums = [1,2,6,4], k = 3 输出:3 解释:我们可以对数组执行以下操作: - 选择 i = 0 ,将 nums[0] 增加 1 。得到数组 [2,2,6,4] 。 - 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,3] 。 - 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,2] 。 元素 2 是最终数组中的众数,出现了 3 次,所以频率分数为 3 。 3 是所有可行方案里的最大频率分数。 示例 2: 输入:nums = [1,4,4,2,4], k = 0 输出:3 解释:我们无法执行任何操作,所以得到的频率分数是原数组中众数的频率 3 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 0 <= k <= 1014 """ class Solution: def maxFrequencyScore(self, nums: List[int], k: int) -> int:
给你一个下标从 0开始的整数数组nums和一个整数k。 你可以对数组执行 至多k次操作: 从数组中选择一个下标 i,将nums[i] 增加或者减少1。 最终数组的频率分数定义为数组中众数的 频率。 请你返回你可以得到的 最大频率分数。 众数指的是数组中出现次数最多的数。一个元素的频率指的是数组中这个元素的出现次数。 示例 1: 输入:nums = [1,2,6,4], k = 3 输出:3 解释:我们可以对数组执行以下操作: - 选择 i = 0 ,将 nums[0] 增加 1 。得到数组 [2,2,6,4] 。 - 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,3] 。 - 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,2] 。 元素 2 是最终数组中的众数,出现了 3 次,所以频率分数为 3 。 3 是所有可行方案里的最大频率分数。 示例 2: 输入:nums = [1,4,4,2,4], k = 0 输出:3 解释:我们无法执行任何操作,所以得到的频率分数是原数组中众数的频率 3 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 0 <= k <= 1014 请完成下面的代码来解决上述问题: ```python class Solution: def maxFrequencyScore(self, nums: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,6,4], "k": 3 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [1,4,4,2,4], "k": 0 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [3,20,13,2,3,15,24,19,8,13,19,20,21], "k": 45 } assert my_solution.maxFrequencyScore(**test_input) == 10 test_input = { "nums": [13,22,29,21,13,17,5,2,27,6,10,4,23,29,27], "k": 117 } assert my_solution.maxFrequencyScore(**test_input) == 14 test_input = { "nums": [27,8,30,3,13,28,7,14,21,19,24,28,29,1,14,22,6], "k": 23 } assert my_solution.maxFrequencyScore(**test_input) == 8 test_input = { "nums": [10,11,3], "k": 1 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [10,19,26,18,27,18], "k": 9 } assert my_solution.maxFrequencyScore(**test_input) == 4 test_input = { "nums": [17,24,10,23,22,15,25,2,13,24,22,25,25,21], "k": 52 } assert my_solution.maxFrequencyScore(**test_input) == 13 test_input = { "nums": [28,6,22,10], "k": 12 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [17,17,25,14,29,28,20,14,16,22,4,28,2,5,3,11,6,20,17], "k": 76 } assert my_solution.maxFrequencyScore(**test_input) == 14 test_input = { "nums": [23,10,18,21,16,23,14], "k": 2 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [5,13,7], "k": 8 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [6,29,3,19,10,6,20,26,1,30,11,25,29,12,29,14,15,16,5], "k": 64 } assert my_solution.maxFrequencyScore(**test_input) == 12 test_input = { "nums": [10,26,21,18,30,25,1], "k": 8 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [29,10,26,1,2,2,17,7,5,16,24,27,7,7,26,26,24], "k": 3 } assert my_solution.maxFrequencyScore(**test_input) == 5 test_input = { "nums": [11,16,6,12,3,8,5,29,9,15,7,9,14,6,11,14,12,23,22,14], "k": 79 } assert my_solution.maxFrequencyScore(**test_input) == 19 test_input = { "nums": [5,17,15,14,27,11,22,6,4], "k": 26 } assert my_solution.maxFrequencyScore(**test_input) == 6 test_input = { "nums": [13,22,17], "k": 4 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [24,6,14,6,30,9,6,11,21,10,12,27,1], "k": 90 } assert my_solution.maxFrequencyScore(**test_input) == 13 test_input = { "nums": [19,5,2,23,16,22,3,2,5,20,17,3,22,1], "k": 15 } assert my_solution.maxFrequencyScore(**test_input) == 7 test_input = { "nums": [15,20], "k": 5 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [9,2,5,14,19,5,10,10,2,25,1,1,1,14,9,13,5,6,10,1], "k": 80 } assert my_solution.maxFrequencyScore(**test_input) == 18 test_input = { "nums": [2,29,24,19,5], "k": 24 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [4,10,5], "k": 6 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [5,2,22,7,18,26,15,4,24,26,24], "k": 19 } assert my_solution.maxFrequencyScore(**test_input) == 6 test_input = { "nums": [23,21,10], "k": 13 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [5,23,7,2,1,5,12,2,20,24,5,4], "k": 71 } assert my_solution.maxFrequencyScore(**test_input) == 11 test_input = { "nums": [22,13,30], "k": 17 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [7,23,29,8,9,3,4,16,24,6,18,20,19,14,5], "k": 55 } assert my_solution.maxFrequencyScore(**test_input) == 11 test_input = { "nums": [3,11,24,27,10], "k": 24 } assert my_solution.maxFrequencyScore(**test_input) == 4 test_input = { "nums": [12,11,21,6,13,27,11,2,27,26,24,13], "k": 0 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [4,10,26,16,21,26,11,26,30,24,18,30,23,26,24], "k": 50 } assert my_solution.maxFrequencyScore(**test_input) == 12 test_input = { "nums": [4,2,18,14,9,29], "k": 4 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [9,27,19,18], "k": 9 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [11,17,29,9,22,7,13,14,12,24,9], "k": 47 } assert my_solution.maxFrequencyScore(**test_input) == 10 test_input = { "nums": [20,10,15,16], "k": 10 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [16,2,6,20,2,18,16,8,15,19,22,29,24,2,26,19], "k": 40 } assert my_solution.maxFrequencyScore(**test_input) == 11 test_input = { "nums": [17,13,19,28,6,8,5,25,2,3,9,4,21,6,13,10,5,3], "k": 113 } assert my_solution.maxFrequencyScore(**test_input) == 18 test_input = { "nums": [16,5,10,15], "k": 5 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [23,2,23,27,21], "k": 2 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [15,26,3,14,3,18,16,19,11,9,2,18,14,8,20,9], "k": 75 } assert my_solution.maxFrequencyScore(**test_input) == 15 test_input = { "nums": [13,23,4,5,2], "k": 3 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [3,1,9,12], "k": 8 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [4,27,21,16,11,5,5,1,5,10], "k": 18 } assert my_solution.maxFrequencyScore(**test_input) == 7 test_input = { "nums": [14,4,23,27,8,25,7,12,12,21,21,11,20,23,30,11,12,29,22], "k": 77 } assert my_solution.maxFrequencyScore(**test_input) == 15 test_input = { "nums": [10,30,25], "k": 20 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [4,8,22,25,27,26,18,14,18], "k": 56 } assert my_solution.maxFrequencyScore(**test_input) == 9 test_input = { "nums": [2,19,27,25,23,17,8,16,28,10,6,24,6], "k": 28 } assert my_solution.maxFrequencyScore(**test_input) == 7 test_input = { "nums": [27,25,27,10,23,14,24,17,12,22,14,11,19,16,7,15], "k": 21 } assert my_solution.maxFrequencyScore(**test_input) == 9 test_input = { "nums": [23,11], "k": 12 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [20,28,15,11,22,26,29,2,16,9], "k": 61 } assert my_solution.maxFrequencyScore(**test_input) == 9 test_input = { "nums": [21,11,1,17,20,19,24,10,1,4,10,30,11,29,20,12,18,5,4,27], "k": 110 } assert my_solution.maxFrequencyScore(**test_input) == 17 test_input = { "nums": [22,29,7,1,26,22,27,1,16,25,25], "k": 26 } assert my_solution.maxFrequencyScore(**test_input) == 8 test_input = { "nums": [26,6,24], "k": 20 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [29,24,9,4,2,9,28,1,25,25,13,22,27,26,15,18], "k": 2 } assert my_solution.maxFrequencyScore(**test_input) == 4 test_input = { "nums": [9,19,1,24,15,19,22,13,10,8,4,10,26,23,11,8], "k": 89 } assert my_solution.maxFrequencyScore(**test_input) == 15 test_input = { "nums": [18,6,20,22,25,21,19,19,15,5,7,29,28,7,17,4], "k": 104 } assert my_solution.maxFrequencyScore(**test_input) == 16 test_input = { "nums": [9,11,28,24,30,6,1,30,22,16,20,19,21,17], "k": 62 } assert my_solution.maxFrequencyScore(**test_input) == 11 test_input = { "nums": [15,13,29,28], "k": 15 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [28,26,8], "k": 18 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [6,12,24,4,25,23,5,13,7,5], "k": 12 } assert my_solution.maxFrequencyScore(**test_input) == 6 test_input = { "nums": [8,23,15,15,3,19,6,20,12,18,7,8,18,19,11,20,4,18], "k": 54 } assert my_solution.maxFrequencyScore(**test_input) == 14 test_input = { "nums": [30,2,4,7,19,3,3,14,24,4,26,17,1,12,4,11], "k": 36 } assert my_solution.maxFrequencyScore(**test_input) == 11 test_input = { "nums": [12,15,21,17,7,20,16,30,8,6,28,28,23,6,12,14,19,26,27,5], "k": 87 } assert my_solution.maxFrequencyScore(**test_input) == 16 test_input = { "nums": [15,1,27,4,5,20,5,26,28], "k": 38 } assert my_solution.maxFrequencyScore(**test_input) == 6 test_input = { "nums": [27,14,30,6,14,29,5,8], "k": 51 } assert my_solution.maxFrequencyScore(**test_input) == 7 test_input = { "nums": [18,22], "k": 4 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [17,28,16,24,29], "k": 5 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [2,22,27,12,30,9,27,3,26,23,25,30,20,19,9,1,23,14,18,26], "k": 30 } assert my_solution.maxFrequencyScore(**test_input) == 11 test_input = { "nums": [26,16,9,7,10,16,26,22,24,1,30,8,15,5,28,16,13,12], "k": 84 } assert my_solution.maxFrequencyScore(**test_input) == 15 test_input = { "nums": [26,8,27,27,22,28,8,26,24,15,6,13,20,12], "k": 55 } assert my_solution.maxFrequencyScore(**test_input) == 11 test_input = { "nums": [19,19,20,14,19,20,5,4,14,26,12,17,14,29,3,9], "k": 94 } assert my_solution.maxFrequencyScore(**test_input) == 16 test_input = { "nums": [9,19,14,17,14,20,27,9,22,29,15,20,6,25,8,17,18,24,23], "k": 44 } assert my_solution.maxFrequencyScore(**test_input) == 13 test_input = { "nums": [2,10], "k": 8 } assert my_solution.maxFrequencyScore(**test_input) == 2 test_input = { "nums": [20,2,27,27,19,20,8,21,15,20,14,18,25], "k": 17 } assert my_solution.maxFrequencyScore(**test_input) == 8 test_input = { "nums": [6,27,6,30,2,1,7,24,18,4,2,18,17], "k": 56 } assert my_solution.maxFrequencyScore(**test_input) == 10 test_input = { "nums": [23,18,30,24,5,21], "k": 33 } assert my_solution.maxFrequencyScore(**test_input) == 6 test_input = { "nums": [12,16,24,18,12,20,26,15,11,23,4,25], "k": 35 } assert my_solution.maxFrequencyScore(**test_input) == 9 test_input = { "nums": [9,9,11,18], "k": 11 } assert my_solution.maxFrequencyScore(**test_input) == 4 test_input = { "nums": [19,21,7,15,21,10,5,27,2,27,14], "k": 63 } assert my_solution.maxFrequencyScore(**test_input) == 9 test_input = { "nums": [25,17,13,6,3,19,21,12,29,1,16,14,24,27,25,13,1,5,17], "k": 136 } assert my_solution.maxFrequencyScore(**test_input) == 19 test_input = { "nums": [4,4,8,9,14,7,27,8,2,29,1,28,23,13], "k": 109 } assert my_solution.maxFrequencyScore(**test_input) == 14 test_input = { "nums": [14,2,18,30,28,17,25,10,7,10,19,3,26,22,12,17,8,4], "k": 24 } assert my_solution.maxFrequencyScore(**test_input) == 8 test_input = { "nums": [27,17,12,19,25,1,9,4,9,20,2,5], "k": 70 } assert my_solution.maxFrequencyScore(**test_input) == 10 test_input = { "nums": [18,25,12,20,19,26,2,15,3,6,29,29,2,24,4,9], "k": 106 } assert my_solution.maxFrequencyScore(**test_input) == 14 test_input = { "nums": [17,18,14,1,28,15,14,13,9,16,28,9,21,23,2,11], "k": 65 } assert my_solution.maxFrequencyScore(**test_input) == 14 test_input = { "nums": [17,15,29,30,12,13,10], "k": 37 } assert my_solution.maxFrequencyScore(**test_input) == 6 test_input = { "nums": [15,4,11,1,18,29,9,23,14,25,15,12,15,6,30,28], "k": 60 } assert my_solution.maxFrequencyScore(**test_input) == 12 test_input = { "nums": [20,6,8,15,12,8,26,7,27,8,5,25,17,12,7,1,23,24,8], "k": 62 } assert my_solution.maxFrequencyScore(**test_input) == 14 test_input = { "nums": [22,21,30,16,23,24,2,2,24], "k": 39 } assert my_solution.maxFrequencyScore(**test_input) == 7 test_input = { "nums": [8,9,6,30,28,2,1,3,14,8,21,26,13,29,23,3,14,9,6,25], "k": 91 } assert my_solution.maxFrequencyScore(**test_input) == 16 test_input = { "nums": [7,10,16,23,17,22,28,7,4,21,25,21,19,30,13,19,15,21,23], "k": 53 } assert my_solution.maxFrequencyScore(**test_input) == 15 test_input = { "nums": [25,17,1,24,3,6,8,29,19,4,16,12,9,28,1,21,13,29], "k": 151 } assert my_solution.maxFrequencyScore(**test_input) == 18 test_input = { "nums": [1,6,14,9], "k": 8 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [5,7,17,2,23,6,3,13,2,11,10,8,18,16,3,11,26], "k": 30 } assert my_solution.maxFrequencyScore(**test_input) == 10 test_input = { "nums": [27,27,16,18,24,7,26,30,21,25,28,28,29,27,28,6], "k": 0 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [29,16,9,21,2,16,4,17,22,11,20,23,5,22,7,27,20], "k": 85 } assert my_solution.maxFrequencyScore(**test_input) == 15 test_input = { "nums": [25,5,24,2,30,15,17,27,15,15,27], "k": 69 } assert my_solution.maxFrequencyScore(**test_input) == 10 test_input = { "nums": [6,29,24,14,9,14,1,1,28,20,19,21,13,25,17,24,30,14], "k": 107 } assert my_solution.maxFrequencyScore(**test_input) == 16 test_input = { "nums": [17,20,2,11,5,7,28], "k": 36 } assert my_solution.maxFrequencyScore(**test_input) == 6
1,702,780,200
weekly-contest-375-count-tested-devices-after-test-operations
https://leetcode.com/problems/count-tested-devices-after-test-operations
count-tested-devices-after-test-operations
{ "questionId": "3220", "questionFrontendId": "2960", "title": "Count Tested Devices After Test Operations", "titleSlug": "count-tested-devices-after-test-operations", "isPaidOnly": false, "difficulty": "Easy", "likes": 90, "dislikes": 5, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 、下标从 0 开始的整数数组 batteryPercentages ,表示 n 个设备的电池百分比。 你的任务是按照顺序测试每个设备 i,执行以下测试操作: 如果 batteryPercentages[i] 大于 0: 增加 已测试设备的计数。 将下标在 [i + 1, n - 1] 的所有设备的电池百分比减少 1,确保它们的电池百分比 不会低于 0 ,即 batteryPercentages[j] = max(0, batteryPercentages[j] - 1)。 移动到下一个设备。 否则,移动到下一个设备而不执行任何测试。 返回一个整数,表示按顺序执行测试操作后 已测试设备 的数量。 示例 1: 输入:batteryPercentages = [1,1,2,1,3] 输出:3 解释:按顺序从设备 0 开始执行测试操作: 在设备 0 上,batteryPercentages[0] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [1,0,1,0,2] 。 在设备 1 上,batteryPercentages[1] == 0 ,移动到下一个设备而不进行测试。 在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 变为 [1,0,1,0,1] 。 在设备 3 上,batteryPercentages[3] == 0 ,移动到下一个设备而不进行测试。 在设备 4 上,batteryPercentages[4] > 0 ,现在有 3 个已测试设备,batteryPercentages 保持不变。 因此,答案是 3 。 示例 2: 输入:batteryPercentages = [0,1,2] 输出:2 解释:按顺序从设备 0 开始执行测试操作: 在设备 0 上,batteryPercentages[0] == 0 ,移动到下一个设备而不进行测试。 在设备 1 上,batteryPercentages[1] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [0,1,1] 。 在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 保持不变。 因此,答案是 2 。 提示: 1 <= n == batteryPercentages.length <= 100 0 <= batteryPercentages[i] <= 100 """ class Solution: def countTestedDevices(self, batteryPercentages: List[int]) -> int:
给你一个长度为 n 、下标从 0 开始的整数数组 batteryPercentages ,表示 n 个设备的电池百分比。 你的任务是按照顺序测试每个设备 i,执行以下测试操作: 如果 batteryPercentages[i] 大于 0: 增加 已测试设备的计数。 将下标在 [i + 1, n - 1] 的所有设备的电池百分比减少 1,确保它们的电池百分比 不会低于 0 ,即 batteryPercentages[j] = max(0, batteryPercentages[j] - 1)。 移动到下一个设备。 否则,移动到下一个设备而不执行任何测试。 返回一个整数,表示按顺序执行测试操作后 已测试设备 的数量。 示例 1: 输入:batteryPercentages = [1,1,2,1,3] 输出:3 解释:按顺序从设备 0 开始执行测试操作: 在设备 0 上,batteryPercentages[0] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [1,0,1,0,2] 。 在设备 1 上,batteryPercentages[1] == 0 ,移动到下一个设备而不进行测试。 在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 变为 [1,0,1,0,1] 。 在设备 3 上,batteryPercentages[3] == 0 ,移动到下一个设备而不进行测试。 在设备 4 上,batteryPercentages[4] > 0 ,现在有 3 个已测试设备,batteryPercentages 保持不变。 因此,答案是 3 。 示例 2: 输入:batteryPercentages = [0,1,2] 输出:2 解释:按顺序从设备 0 开始执行测试操作: 在设备 0 上,batteryPercentages[0] == 0 ,移动到下一个设备而不进行测试。 在设备 1 上,batteryPercentages[1] > 0 ,现在有 1 个已测试设备,batteryPercentages 变为 [0,1,1] 。 在设备 2 上,batteryPercentages[2] > 0 ,现在有 2 个已测试设备,batteryPercentages 保持不变。 因此,答案是 2 。 提示: 1 <= n == batteryPercentages.length <= 100 0 <= batteryPercentages[i] <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def countTestedDevices(self, batteryPercentages: List[int]) -> int: ```
my_solution = Solution() test_input = { "batteryPercentages": [1,1,2,1,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,1,2] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [0] } assert my_solution.countTestedDevices(**test_input) == 0 test_input = { "batteryPercentages": [1] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [0,0] } assert my_solution.countTestedDevices(**test_input) == 0 test_input = { "batteryPercentages": [0,1] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [0,2] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [1,0] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [1,2] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [2,1] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [2,2] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [0,0,1] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [0,0,2] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [1,1,0] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [1,2,0] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [1,3,1] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [2,0,1] } assert my_solution.countTestedDevices(**test_input) == 1 test_input = { "batteryPercentages": [2,2,0] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [2,2,2] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [3,0,3] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [3,3,1] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [3,3,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,2,1,4] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [1,4,4,1] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [3,1,2,0] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [3,2,1,1] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [3,2,1,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [4,1,4,4] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [4,2,0,1] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [4,2,1,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [4,4,4,2] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,3,1,3,5] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,4,2,5,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,5,4,2,0] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [2,2,3,0,2] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [2,3,5,0,1] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [2,4,5,2,0] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [4,3,3,5,4] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [5,4,1,0,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [5,5,5,2,0] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,2,4,3,0,2] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,4,5,3,3,2] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [1,3,1,5,4,5] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [1,6,0,3,3,6] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [3,1,3,5,2,0] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [3,2,6,2,6,0] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [4,1,5,3,5,2] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [4,3,3,2,4,3] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [4,5,2,3,6,2] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [5,1,1,2,1,4] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [5,1,6,6,3,6] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [6,1,5,1,4,5] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [6,2,2,3,4,6] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [6,2,3,0,2,0] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [1,0,6,3,6,3,1] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [2,1,7,3,0,3,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [2,3,7,0,6,4,4] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [2,5,2,4,2,1,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [2,5,2,7,6,5,5] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [4,2,6,4,7,6,7] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [4,2,6,6,3,3,7] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [4,4,3,0,2,6,6] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [5,2,2,3,4,6,6] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [5,4,6,0,7,2,2] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [6,6,7,0,1,7,2] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [0,5,1,4,5,0,4,8] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [1,0,7,0,7,4,5,7] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [2,5,3,4,4,8,6,5] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [2,6,3,4,5,6,2,6] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [4,5,2,1,3,7,3,5] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [6,5,4,8,6,8,3,6] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [7,4,0,8,5,5,2,0] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [7,5,3,2,3,5,8,6] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [8,0,4,3,2,6,6,1] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [8,3,0,1,0,8,6,8] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [8,6,7,1,0,1,3,7] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [0,6,8,8,0,1,2,3,4] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [2,7,9,7,2,9,0,3,9] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [8,1,9,8,5,3,4,4,1] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [8,4,0,1,1,6,5,3,5] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [8,4,1,5,8,5,8,7,9] } assert my_solution.countTestedDevices(**test_input) == 8 test_input = { "batteryPercentages": [8,4,9,8,9,0,0,4,9] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [8,9,4,4,1,9,8,9,1] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [2,5,8,9,1,5,10,9,6,3] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [2,6,5,4,1,5,3,3,3,9] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [7,7,7,3,6,6,4,3,5,10] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [9,3,10,1,8,2,4,3,3,0] } assert my_solution.countTestedDevices(**test_input) == 4 test_input = { "batteryPercentages": [10,10,2,0,2,7,6,7,10,4] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [0,8,7,9,4,10,4,3,7,11,7] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [1,2,3,5,6,11,3,2,11,0,8] } assert my_solution.countTestedDevices(**test_input) == 8 test_input = { "batteryPercentages": [5,10,4,10,10,6,8,1,8,10,3] } assert my_solution.countTestedDevices(**test_input) == 9 test_input = { "batteryPercentages": [7,10,2,7,11,8,11,4,1,4,5] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [7,11,0,4,1,10,5,3,2,0,2] } assert my_solution.countTestedDevices(**test_input) == 5 test_input = { "batteryPercentages": [8,8,1,8,6,2,5,2,8,5,6] } assert my_solution.countTestedDevices(**test_input) == 6 test_input = { "batteryPercentages": [8,9,10,10,1,5,4,6,7,2,4] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [9,9,2,3,2,2,9,6,11,1,10] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [10,0,6,2,6,6,11,1,8,10,5] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [1,4,7,2,12,8,1,11,5,10,2,3] } assert my_solution.countTestedDevices(**test_input) == 7 test_input = { "batteryPercentages": [2,5,4,4,9,6,10,0,11,8,2,10] } assert my_solution.countTestedDevices(**test_input) == 9 test_input = { "batteryPercentages": [4,11,9,8,9,11,11,5,11,6,12,11] } assert my_solution.countTestedDevices(**test_input) == 10
1,702,175,400
weekly-contest-375-double-modular-exponentiation
https://leetcode.com/problems/double-modular-exponentiation
double-modular-exponentiation
{ "questionId": "3234", "questionFrontendId": "2961", "title": "Double Modular Exponentiation", "titleSlug": "double-modular-exponentiation", "isPaidOnly": false, "difficulty": "Medium", "likes": 87, "dislikes": 12, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的二维数组 variables ,其中 variables[i] = [ai, bi, ci, mi],以及一个整数 target 。 如果满足以下公式,则下标 i 是 好下标: 0 <= i < variables.length ((aibi % 10)ci) % mi == target 返回一个由 好下标 组成的数组,顺序不限 。 示例 1: 输入:variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2 输出:[0,2] 解释:对于 variables 数组中的每个下标 i : 1) 对于下标 0 ,variables[0] = [2,3,3,10] ,(23 % 10)3 % 10 = 2 。 2) 对于下标 1 ,variables[1] = [3,3,3,1] ,(33 % 10)3 % 1 = 0 。 3) 对于下标 2 ,variables[2] = [6,1,1,4] ,(61 % 10)1 % 4 = 2 。 因此,返回 [0,2] 作为答案。 示例 2: 输入:variables = [[39,3,1000,1000]], target = 17 输出:[] 解释:对于 variables 数组中的每个下标 i : 1) 对于下标 0 ,variables[0] = [39,3,1000,1000] ,(393 % 10)1000 % 1000 = 1 。 因此,返回 [] 作为答案。 提示: 1 <= variables.length <= 100 variables[i] == [ai, bi, ci, mi] 1 <= ai, bi, ci, mi <= 103 0 <= target <= 103 """ class Solution: def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]:
给你一个下标从 0 开始的二维数组 variables ,其中 variables[i] = [ai, bi, ci, mi],以及一个整数 target 。 如果满足以下公式,则下标 i 是 好下标: 0 <= i < variables.length ((aibi % 10)ci) % mi == target 返回一个由 好下标 组成的数组,顺序不限 。 示例 1: 输入:variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2 输出:[0,2] 解释:对于 variables 数组中的每个下标 i : 1) 对于下标 0 ,variables[0] = [2,3,3,10] ,(23 % 10)3 % 10 = 2 。 2) 对于下标 1 ,variables[1] = [3,3,3,1] ,(33 % 10)3 % 1 = 0 。 3) 对于下标 2 ,variables[2] = [6,1,1,4] ,(61 % 10)1 % 4 = 2 。 因此,返回 [0,2] 作为答案。 示例 2: 输入:variables = [[39,3,1000,1000]], target = 17 输出:[] 解释:对于 variables 数组中的每个下标 i : 1) 对于下标 0 ,variables[0] = [39,3,1000,1000] ,(393 % 10)1000 % 1000 = 1 。 因此,返回 [] 作为答案。 提示: 1 <= variables.length <= 100 variables[i] == [ai, bi, ci, mi] 1 <= ai, bi, ci, mi <= 103 0 <= target <= 103 请完成下面的代码来解决上述问题: ```python class Solution: def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]: ```
my_solution = Solution() test_input = { "variables": [[2,3,3,10],[3,3,3,1],[6,1,1,4]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [0,2] test_input = { "variables": [[39,3,1000,1000]], "target": 17 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,2,4,2],[3,3,1,3],[2,2,2,4],[4,4,2,3],[2,4,1,3]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[9,2,8,5],[7,8,8,8],[8,9,6,1],[8,6,2,2],[3,6,3,1]], "target": 9 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[2,2,3,2],[1,3,3,2],[3,2,2,3],[3,1,2,3],[1,2,3,1],[2,2,2,2],[2,1,3,1],[3,2,2,2],[2,1,3,1],[3,3,1,3]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,2,3,4,5,6,8] test_input = { "variables": [[1,3,2,3],[4,2,3,3],[4,1,4,4],[4,2,3,1],[4,2,1,1],[1,2,4,1],[1,1,4,2],[1,4,4,3],[1,2,2,3]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[5,4,1,3],[2,5,5,1],[5,3,4,1]], "target": 5 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,7,6,7],[7,6,6,4],[6,8,2,3],[8,3,5,8]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,1,2,3,4,5,6] test_input = { "variables": [[3,5,1,2],[3,2,5,2],[4,4,3,2],[3,2,5,3],[1,5,1,4]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [0,1,4] test_input = { "variables": [[1,2,1,1],[2,2,2,2],[1,1,1,2],[1,2,2,2]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,3,5,6],[8,2,9,2],[1,4,6,1],[6,4,7,7]], "target": 8 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,5,4,3],[1,3,3,1],[3,3,5,5],[4,5,5,5],[5,1,4,3],[2,5,3,4]], "target": 7 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[9,7,2,7],[9,1,8,1],[9,3,5,6],[6,1,8,4],[9,6,2,3]], "target": 8 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[10,6,8,7],[3,6,1,8]], "target": 5 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,6,5,2],[2,6,4,6],[4,6,3,6],[2,2,6,5],[6,5,5,2]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[5,6,5,1],[4,3,1,6],[5,4,4,2]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [1] test_input = { "variables": [[5,1,2,4],[4,5,5,5],[5,9,7,4],[7,9,6,3],[1,8,6,1],[1,1,9,9],[3,7,6,5],[2,6,2,6]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [0,2,3,5] test_input = { "variables": [[1,3,2,5],[5,4,1,2],[2,2,3,2],[4,2,5,4],[1,5,4,1],[2,2,5,2],[3,3,2,1],[2,5,4,3],[2,1,5,1]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,2,1,3],[1,2,1,1],[2,1,3,2],[2,3,1,3],[3,3,1,1],[2,3,2,1],[2,1,3,3],[1,2,2,2],[3,2,1,3]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [7] test_input = { "variables": [[3,3,2,2],[3,3,2,2]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,6,7,5],[6,3,1,5],[7,5,5,4],[6,2,2,4],[6,1,1,2],[2,6,5,4]], "target": 8 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[7,8,9,3],[7,8,2,8],[2,4,4,8],[8,8,4,4]], "target": 6 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[6,7,6,6],[1,2,4,8],[6,4,2,4],[3,2,4,5]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[2,3,1,1],[3,2,1,1],[2,1,2,3]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[9,8,10,1],[7,1,3,9],[6,8,9,10],[4,8,8,9]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,3,2,2],[3,6,4,6],[1,4,1,4],[5,2,5,1],[8,3,6,3],[8,4,8,3]], "target": 6 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,4,4,2],[4,4,1,2],[1,1,3,2],[3,1,4,3],[2,2,3,4],[2,3,2,4],[3,1,4,4],[1,4,1,4],[3,2,1,4]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [1,3,4,5] test_input = { "variables": [[2,1,1,1],[2,2,2,2],[1,2,1,2],[1,1,1,1],[1,1,1,1],[2,1,2,2],[1,1,2,1],[2,2,2,2]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,1,3,4,5,6,7] test_input = { "variables": [[3,1,2,4],[3,3,4,2],[3,4,3,4],[3,3,4,2]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [0,1,2,3] test_input = { "variables": [[4,10,5,8],[7,7,5,8],[4,8,6,2],[6,3,3,2]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,5,3,6],[3,1,6,3],[6,4,1,5],[3,2,3,5],[1,4,7,5],[6,6,6,2],[7,5,6,3],[1,2,7,1],[1,1,6,3]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [3] test_input = { "variables": [[1,4,2,3],[4,3,1,4],[3,3,1,3],[1,4,1,3]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [1] test_input = { "variables": [[1,4,2,2],[5,5,1,2],[3,4,2,3]], "target": 6 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,5,1,1],[4,8,6,8],[5,1,4,9],[4,3,1,2],[5,9,4,7],[8,7,7,1],[9,3,7,5]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,1,3,5] test_input = { "variables": [[1,1,1,1],[2,2,2,1],[2,2,2,1],[2,2,2,2],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[5,5,6,5],[4,1,2,2],[6,2,5,3],[1,1,5,5],[3,5,6,5]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,4,1,3],[2,4,4,1],[1,1,3,1],[2,3,3,1]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [1,2,3] test_input = { "variables": [[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,2,1,7],[3,3,7,1],[5,5,8,2],[5,1,3,1]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,5,7,2],[2,10,7,10],[6,8,2,2],[9,4,1,2],[1,7,4,1]], "target": 3 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[5,5,3,5],[4,2,5,9],[4,6,6,1],[4,5,3,6]], "target": 8 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,2,1],[1,1,3,2],[2,3,3,2],[1,2,3,2],[1,1,1,3],[2,2,1,2]], "target": 3 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,1,3,5],[4,7,1,6],[7,3,5,4],[2,4,2,7],[6,3,4,7]], "target": 7 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,2,3,7],[2,9,6,2],[3,8,9,2],[1,7,7,3],[1,3,8,1],[2,4,5,1],[3,6,3,2],[4,4,6,8]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[8,3,7,1],[7,8,3,2],[4,1,5,3],[6,6,6,3],[2,4,7,5]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,6,6],[1,1,1,2],[3,6,6,1],[4,5,5,6],[3,1,6,6],[3,2,2,1],[6,1,1,2]], "target": 6 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,2,2,1],[6,3,2,1],[2,4,3,2],[1,1,6,6],[4,6,2,1],[5,4,2,1],[1,2,6,1],[6,2,4,4]], "target": 6 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[2,2,2,2],[1,2,2,3]], "target": 3 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,1] test_input = { "variables": [[2,5,8,2],[2,6,1,2],[7,4,8,9],[6,3,1,4],[7,1,6,7],[4,6,2,7],[8,2,2,7],[4,5,3,8],[1,2,6,4]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [3,6] test_input = { "variables": [[4,2,4,1],[6,1,2,6],[4,3,3,2]], "target": 8 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[6,4,3,5],[7,4,2,6],[1,4,2,1],[4,5,4,5],[7,2,2,7],[7,5,4,3],[2,7,1,3],[6,7,2,2],[4,7,4,1],[7,3,2,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [0,1,3,5] test_input = { "variables": [[4,10,5,8],[8,8,9,8],[7,1,5,4],[8,9,2,2],[2,2,8,7],[6,8,10,3],[6,8,4,4],[5,4,10,5],[3,7,8,2]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [8] test_input = { "variables": [[7,5,4,2],[2,1,3,6],[7,2,2,3],[1,4,7,3]], "target": 3 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[2,3,3,3],[3,2,1,3],[2,2,3,2],[3,1,2,2],[1,2,2,1],[2,3,3,3],[3,1,2,2]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [3,6] test_input = { "variables": [[10,2,6,2],[8,10,5,7]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0] test_input = { "variables": [[6,8,3,6],[4,8,3,1],[6,8,6,5],[7,4,7,1],[5,2,1,5],[2,3,5,7],[3,2,6,3],[4,3,7,1]], "target": 6 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[5,5,5,1],[6,1,3,2],[3,1,2,6],[2,6,6,5],[6,1,3,6],[4,2,3,1],[2,5,3,5],[6,6,6,2]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,2],[2,1,1,1],[2,1,1,1],[1,1,1,1],[2,1,2,2],[2,2,2,2],[1,1,1,1],[1,2,2,2],[1,2,1,1]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [1,2,3,4,5,6,8] test_input = { "variables": [[9,4,4,9],[9,4,2,6],[7,5,1,4],[9,2,2,3],[6,5,1,2],[2,7,2,9],[1,8,1,6],[5,4,9,7],[8,1,7,4]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [0,1,3,5,6] test_input = { "variables": [[1,3,1,3],[2,1,3,2],[2,2,1,1],[1,2,1,3]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,5,5,1],[4,2,6,3],[3,5,6,6],[5,3,1,1],[5,1,3,4],[6,1,6,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [4] test_input = { "variables": [[1,2,1,2],[1,2,1,2]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,4,2,4],[5,5,3,5],[3,5,3,4],[2,4,5,5],[5,4,4,5],[2,2,2,3]], "target": 5 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,4,4,4],[5,2,4,4],[1,1,5,3],[3,4,1,2],[3,1,2,3],[4,3,3,3],[3,5,4,1],[2,1,4,5],[3,3,1,3]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [0,1,2,3,5,7,8] test_input = { "variables": [[1,1,6,1],[3,8,4,7],[8,5,5,9],[4,9,1,3],[9,1,1,9]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,4] test_input = { "variables": [[2,1,2,2],[2,2,2,1],[2,1,2,2],[1,2,1,1],[2,1,2,2],[2,2,2,2]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[5,7,4,2],[2,8,10,10],[4,4,7,2],[7,4,4,6]], "target": 10 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,1,2,3,4,5,6,7,8,9] test_input = { "variables": [[9,3,6,3],[9,7,2,5],[2,8,9,9],[4,7,7,4],[2,7,3,9],[8,5,5,3],[7,5,4,3],[9,9,2,9],[9,4,8,8]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[7,5,2,3],[1,7,2,3],[9,1,7,1]], "target": 3 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[2,5,7,3],[2,6,5,1],[4,3,6,5]], "target": 3 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[7,8,1,1],[1,5,4,1],[4,7,8,9],[7,9,2,4],[5,1,4,3],[3,9,4,1],[6,6,1,8],[4,8,5,1],[1,4,5,9]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [3,4,8] test_input = { "variables": [[5,1,6,1],[3,6,5,2],[4,2,5,4],[2,3,5,2],[2,4,3,1],[3,2,3,6],[6,2,4,6],[6,3,3,2]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,2,3,4,6,7] test_input = { "variables": [[3,2,5,3],[9,4,2,4],[2,4,7,7],[1,4,9,2],[5,1,5,5],[9,5,6,7],[9,1,4,7]], "target": 5 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,2,2,2],[3,1,3,2],[2,1,2,2],[3,2,3,3],[1,1,2,3],[1,1,3,1]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [2,3,5] test_input = { "variables": [[1,5,2,1],[3,5,3,2],[1,2,4,1],[1,4,1,4],[4,4,1,3],[4,2,4,5],[2,2,4,1],[2,1,3,3]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,2,7,6],[10,6,5,10],[2,4,10,7],[9,5,8,6],[10,6,3,10],[9,6,5,2],[8,10,1,2],[7,1,8,8],[7,7,4,8],[8,3,8,1]], "target": 4 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[6,6,5,2],[3,5,4,3],[2,4,3,3],[6,3,4,4],[4,1,3,6],[1,6,3,5],[3,3,5,5]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,1,2,3] test_input = { "variables": [[10,3,8,9],[9,1,5,5],[4,5,10,5],[9,8,3,5],[3,5,4,7],[1,10,2,3],[6,2,4,8],[6,4,3,2],[5,9,9,2]], "target": 8 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[7,7,4,3],[2,10,10,4],[8,1,9,1],[9,7,7,9],[8,9,8,5],[9,8,4,2],[1,9,3,8],[6,8,3,1]], "target": 8 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[9,8,1,4],[5,2,7,4],[5,6,3,4],[9,5,9,8],[2,1,10,10],[10,9,9,2],[8,5,2,3],[10,10,3,8],[1,7,8,1],[1,4,3,5]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[2,3,4,4],[2,3,7,2],[4,2,6,3],[2,3,3,6],[5,1,2,7],[7,6,7,1]], "target": 5 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,3,8,8],[2,5,6,5],[8,1,2,3],[1,4,8,7],[8,5,5,7],[6,6,3,9],[5,6,7,1],[4,7,5,1],[1,5,1,5],[5,3,2,1]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,1,7,8],[4,3,7,8],[7,4,2,2]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [2] test_input = { "variables": [[4,4,8,8],[4,7,8,7],[1,4,8,2],[5,5,6,4],[7,8,4,3],[8,6,2,1]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [2,3,4] test_input = { "variables": [[2,5,2,5],[1,1,4,1],[3,2,4,3],[3,1,3,4]], "target": 1 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[2,2,1,2],[2,2,2,2],[2,2,1,2]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[7,6,4,6],[3,7,2,3],[7,7,1,7],[7,7,6,5],[6,1,1,4],[1,4,2,3],[1,2,4,2],[3,2,2,1],[7,6,2,5],[2,4,5,7]], "target": 7 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[1,1,2,1],[1,1,1,2],[1,1,2,1],[2,1,1,2],[1,1,2,1],[2,1,1,1]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[4,5,8,5],[4,2,9,9],[2,3,3,3],[8,6,3,1]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [2] test_input = { "variables": [[4,1,4,2],[3,4,3,4],[5,5,1,5],[5,1,1,4],[4,2,1,5],[5,2,1,1],[1,4,1,4],[1,4,5,5],[5,1,4,5],[1,2,1,2]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,1,3,3],[1,2,1,2],[3,1,1,3],[2,2,1,1],[3,3,2,2],[2,3,1,1]], "target": 0 } assert my_solution.getGoodIndices(**test_input) == [0,2,3,5]
1,702,175,400
weekly-contest-375-count-subarrays-where-max-element-appears-at-least-k-times
https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times
count-subarrays-where-max-element-appears-at-least-k-times
{ "questionId": "3213", "questionFrontendId": "2962", "title": "Count Subarrays Where Max Element Appears at Least K Times", "titleSlug": "count-subarrays-where-max-element-appears-at-least-k-times", "isPaidOnly": false, "difficulty": "Medium", "likes": 215, "dislikes": 10, "categoryTitle": "Algorithms" }
""" 给你一个整数数组 nums 和一个 正整数 k 。 请你统计有多少满足 「nums 中的 最大 元素」至少出现 k 次的子数组,并返回满足这一条件的子数组的数目。 子数组是数组中的一个连续元素序列。 示例 1: 输入:nums = [1,3,2,3,3], k = 2 输出:6 解释:包含元素 3 至少 2 次的子数组为:[1,3,2,3]、[1,3,2,3,3]、[3,2,3]、[3,2,3,3]、[2,3,3] 和 [3,3] 。 示例 2: 输入:nums = [1,4,2,1], k = 3 输出:0 解释:没有子数组包含元素 4 至少 3 次。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 106 1 <= k <= 105 """ class Solution: def countSubarrays(self, nums: List[int], k: int) -> int:
给你一个整数数组 nums 和一个 正整数 k 。 请你统计有多少满足 「nums 中的 最大 元素」至少出现 k 次的子数组,并返回满足这一条件的子数组的数目。 子数组是数组中的一个连续元素序列。 示例 1: 输入:nums = [1,3,2,3,3], k = 2 输出:6 解释:包含元素 3 至少 2 次的子数组为:[1,3,2,3]、[1,3,2,3,3]、[3,2,3]、[3,2,3,3]、[2,3,3] 和 [3,3] 。 示例 2: 输入:nums = [1,4,2,1], k = 3 输出:0 解释:没有子数组包含元素 4 至少 3 次。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 106 1 <= k <= 105 请完成下面的代码来解决上述问题: ```python class Solution: def countSubarrays(self, nums: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "nums": [1,3,2,3,3], "k": 2 } assert my_solution.countSubarrays(**test_input) == 6 test_input = { "nums": [1,4,2,1], "k": 3 } assert my_solution.countSubarrays(**test_input) == 0 test_input = { "nums": [61,23,38,23,56,40,82,56,82,82,82,70,8,69,8,7,19,14,58,42,82,10,82,78,15,82], "k": 2 } assert my_solution.countSubarrays(**test_input) == 224 test_input = { "nums": [37,20,38,66,34,38,9,41,1,14,25,63,8,12,66,66,60,12,35,27,16,38,12,66,38,36,59,54,66,54,66,48,59,66,34,11,50,66,42,51,53,66,31,24,66,44,66,1,66,66,29,54], "k": 5 } assert my_solution.countSubarrays(**test_input) == 594 test_input = { "nums": [28,5,58,91,24,91,53,9,48,85,16,70,91,91,47,91,61,4,54,61,49], "k": 1 } assert my_solution.countSubarrays(**test_input) == 187 test_input = { "nums": [43,105,105,88,19,82,95,32,80,37,49,105,25,105,46,54,45,84,105,88,26,20,49,54,31,105,8,103,37,32,105,105,97,27,105,89,105,47,25,87,29,105,105,105,24,105,105,48,19,91,96,71], "k": 7 } assert my_solution.countSubarrays(**test_input) == 454 test_input = { "nums": [107,101,180,137,191,148,83,15,188,22,100,124,69,94,191,181,171,64,136,96,91,191,107,191,191,191,107,191,191,11,140,33,4,110,83,5,86,33,42,186,191,6,42,61,94,129,191,119,191,134,43,182,191,187,63,116,172,118,50,141,124,191,125,145,191,34,191,191], "k": 9 } assert my_solution.countSubarrays(**test_input) == 548 test_input = { "nums": [41,121,92,15,24,59,45,110,97,132,75,72,31,38,103,37,132,91,132,132,105,24], "k": 3 } assert my_solution.countSubarrays(**test_input) == 61 test_input = { "nums": [21,11,13,15,16,21,8,9,6,21], "k": 2 } assert my_solution.countSubarrays(**test_input) == 10 test_input = { "nums": [31,18,36,166,166,166,135,166,166,12,102], "k": 3 } assert my_solution.countSubarrays(**test_input) == 31 test_input = { "nums": [2,2,2,2,1,3,3,2,2,1,1,3,1,1,2,3,2,1,1,2,1,1,2,1,2,1,2,1,3,1,3,3], "k": 5 } assert my_solution.countSubarrays(**test_input) == 31 test_input = { "nums": [3,2,4,4,3,4,3,1,1,1,1,3,2,1,2,1,3,4,4,1,2,4,1,1,2,3,3,3,4,4,4,1,3,1,4,1,4,4,4,2,2,3,4,3,3,2,2,2,1,2,4,2,2,4,4,1,3,2,3,2,4,4,4,2,3,4,2,4,1,4,1,4,1,4,4,3,4,2,4,3,3,2,3,3,2,3,4,2,1,1,1,2,3], "k": 23 } assert my_solution.countSubarrays(**test_input) == 473 test_input = { "nums": [1,1,1,2,3,2,1,2,3,3,3,3,2,3,2,1,1,2,2,1,3,2,3,1,2,1,3,1,1,3,1,2,1,1,1,1,1,1,3], "k": 8 } assert my_solution.countSubarrays(**test_input) == 148 test_input = { "nums": [54,161,161,161,161,31,74,51,87,19,161,116,108,149,6,19,155,101,161,161,154,161,78,132,62,156,112,51,161,42,92,151,142,17,110,85], "k": 4 } assert my_solution.countSubarrays(**test_input) == 279 test_input = { "nums": [97,102,144,55,144,128,16,93,144,9,144,15,144,144,32,68,144,60,94,56,103,5,41,27,48,144,12,86,129,144,144,99,93,96,144,73,106,76,107,144,53,21,144,144,98,32,85,97,71,127,144,9,144,144,133,125,144,135,52,144,144,46,134,23,23,144,79], "k": 1 } assert my_solution.countSubarrays(**test_input) == 2163 test_input = { "nums": [17,17,15,9,14,11,15,1,6,2,1,17,3,17,11,12,9,11,2,4,15,17,3,17,8,6,7,12,3,16,2,9,14,17,17,17,3,7,8,9,8,17,17,17,4,2,12,17,7,17,17,16,17,17,8,12,11,3,10,4,10,17,14,7,5,17,12,10,17,13,5,17,8,14,9,17,17,17,7,16,10,13,17,15,1,14,6,8,11,3], "k": 15 } assert my_solution.countSubarrays(**test_input) == 1055 test_input = { "nums": [17,12,16,17,7,1,12,6,17,5,17,13,16,16,17,14,17,6,17,17,17,17,16,17,14,8,14,1,12,13,17,17,14,8,14,5,16,17,17], "k": 5 } assert my_solution.countSubarrays(**test_input) == 404 test_input = { "nums": [98,59,98,32,45,15,98,98,98,65,98,10,98,89,87,51,42,58,76,23,85,98,98,35,18,65,39,88,56,62,10,32,8,16,32,98,6,39,14,24,98,95,68,98,77,47,98,23,69,98,49,98,7,11,92,98,27,25,85,98,45,30,50,62,46,1,79,58,69,15,59,57,85,19,98,95,98,67,52,98,59,8,98,98,98,73,86,20,98,96,21,98,79,97,52,22,98,86], "k": 12 } assert my_solution.countSubarrays(**test_input) == 1168 test_input = { "nums": [6,50,118,27,133,133,3,121,133,72,117,133,91,57,107,93,66,122,133,6,133,122,81,20,133,133,121,133,46,25,133,133,133,17,8,49,133,116,40,133,67,9,133,133,133,133,109,41,127,13,39,133,133,133,122,58,8,125,33,62], "k": 12 } assert my_solution.countSubarrays(**test_input) == 538 test_input = { "nums": [94,34,112,106,112,13,12,112,112,21,48,71,112,104,112,29,99,58,23,11,49,112,20,86], "k": 4 } assert my_solution.countSubarrays(**test_input) == 105 test_input = { "nums": [21,27,9,85,1,7,28,11,44,39,85,52,51,30,67,83,75,10,57,59,53,85,75,33,35,85,76,85,65,85,85,85,35,4,60,85,85,72,57,42,34,85,53,85,85,36,85,56,13,16,69,55,81,24,85,27,54,66,10,85,30,58,71,43,85,66,42,27,85,70], "k": 13 } assert my_solution.countSubarrays(**test_input) == 508 test_input = { "nums": [8,14,7,1,11,10,1,13,7,14,14,6,13], "k": 2 } assert my_solution.countSubarrays(**test_input) == 32 test_input = { "nums": [165,135,165,46,126,165,73,165,165,155,150,165,40,38,165,145,137,106,10], "k": 7 } assert my_solution.countSubarrays(**test_input) == 5 test_input = { "nums": [9,3,12,6,24,23,24], "k": 2 } assert my_solution.countSubarrays(**test_input) == 5 test_input = { "nums": [42,85,78,92,46,63,21,14,22,37,96,50,74], "k": 1 } assert my_solution.countSubarrays(**test_input) == 33 test_input = { "nums": [73,54,15,4,23,70,53,65,73,73,2,72,36,71,73,69,35,18,62,73,62,73,73,50,30,73,20,71,60,9,12,57,48,73,40,20,8,73,73,73,34,59,31,49,73,5,51,36,47,38,36,58,34,42,23,28,52,73], "k": 1 } assert my_solution.countSubarrays(**test_input) == 1537 test_input = { "nums": [52,88,92,92,44,4,92,37,27,59,3,3,76,51,21,89,92,31,26,10,47,69,30,68,60,92,80,19,65,38,92,4,54,88,92,75,56,71,11,92,44,43,56,92,16,66,22,70], "k": 2 } assert my_solution.countSubarrays(**test_input) == 796 test_input = { "nums": [29,9,43,5,8,52,24,52,52,41,33,52,27,52,8,6,35,52,27,52,7,2,9,52,52,42,52,52], "k": 7 } assert my_solution.countSubarrays(**test_input) == 76 test_input = { "nums": [165,165,58,153,45,124,165,143,38,165,165,165,165,73,8,138,165,139,165,165,59,40,120,165,123,92,98,136,161], "k": 1 } assert my_solution.countSubarrays(**test_input) == 394 test_input = { "nums": [28,64,64,63,21,64,55,64,10,30,12,5,64,56,63,64,64,31,31,64,19,54,53,64,58,44,64,28,64,64,63,10,64,64,57,29,44,32,50,55,49,21,64,64,34,26,28,64,15,31,28,64,45,64,19,54,9,41,25,33,7,60,1,7,34,14,4,64,64,64,55,49,3,41,28,42,40,52,25,46,25,15], "k": 18 } assert my_solution.countSubarrays(**test_input) == 229 test_input = { "nums": [97,23,53,33,141,150,128,153,71,39,153,35,125,143], "k": 2 } assert my_solution.countSubarrays(**test_input) == 32 test_input = { "nums": [144,144,87,144,18,53,129,61,34,123,141,68,37,23,94,28,64,58,16,36,27,112,144,80,77,144,97,142,8,101,14,74,37,115,115,144,99,37,144,48,28,110,13,78,144,144,83,7,112,144,144,144,78,61,87,144,144,61,144,44,123,74,144,142], "k": 4 } assert my_solution.countSubarrays(**test_input) == 1083 test_input = { "nums": [63,129,134,61,134,134,134,43,74,4,111], "k": 2 } assert my_solution.countSubarrays(**test_input) == 38 test_input = { "nums": [46,105,44,36,106,35,91,8,52,106,95,86,75,7,19,30,25,27,18,72,106,106,33,106,6,63,67,45,15,106,106,6,42,106,27,14,18,106,4,106,95,64,23,93,106,37,106,106,16,81,91,79,106,97,106,66,31,59,58], "k": 1 } assert my_solution.countSubarrays(**test_input) == 1637 test_input = { "nums": [78,120,110,53,53,120,116,39,64,120,120,120,120,120,97,28,92,120,101,5,46,92], "k": 1 } assert my_solution.countSubarrays(**test_input) == 224 test_input = { "nums": [111,111,72,111,111,56,21,95,111,101,38,77,111,111,76,58,70,72,32,72,19,111,111,63,39,111], "k": 9 } assert my_solution.countSubarrays(**test_input) == 5 test_input = { "nums": [33,82,82,82,71,39,17,82,38,75,82,2,82,82,9,82,57,12,78,65,29,20,82,82,50,11,39,74,65,69,81,71,25,82,46,43,49,80], "k": 6 } assert my_solution.countSubarrays(**test_input) == 219 test_input = { "nums": [83,72,17,147,147,57,147,22,120,107,59,133,123,91,147,147,72,147,31,147,147,147,96,147,18,25,13,8,18,59,46,91,15,147,25,30,6,147,113,27,84,95,38,147,147,147,106,53,127,132,55,147,22,147,124,102,17,69,131,147,4,95,59,38,147,147,41,99,142,147,136,142,57,26,16,3,142], "k": 8 } assert my_solution.countSubarrays(**test_input) == 1336 test_input = { "nums": [52,95,173,26,173,16,4,144,173,77,22,103,162,120,77,173,173,89,173,104,62,151,173,124,173,117,113,164,3,70,15,144,161,118,139,16,157,173,154,151,37,69,60,173,173,168,148,97,173,125,161,128,85,64,70,102,100,168,56,57,157,112,119,135,42,72,135,173,173,124,143,121,75,37,162,161,102,50,173,173,107], "k": 4 } assert my_solution.countSubarrays(**test_input) == 1940 test_input = { "nums": [4,18,6,22,19,15,20,12,22,22,19,6,10,7,20,4,22,21,7,17,3,16,13,17,22,14,8,2,3,22,18,18,22,22,7,22,13,10,20,4,14,17,9,19,1,12,3,11,19,15,6,4,10], "k": 6 } assert my_solution.countSubarrays(**test_input) == 347 test_input = { "nums": [55,103,123,68,16,72,104,63,40,15,180,162,82,180,131,46,180,2,120,107,100,97,180,180,17,134,180,124,40,125,15,132,4,112,180,180,28,66,180,122,99,46,15,180,180,111,30,169,132,180,10,180,180,180,107,74,95,28,180,66,180,128,61,180,118,180,28,103,37,180,88,152], "k": 8 } assert my_solution.countSubarrays(**test_input) == 1181 test_input = { "nums": [20,6,49,60,16,54,13,2,35,6,27,62,67,56,27,6,33,51,67,42,9,59,67,14,59,7,67,34,51,5,67,48,53,20,35,67,65,34,67,67,62,7,27,18,40,10,67,67,9,8,60,12,2,67,64,67,60,28,60,26,37,2,67,33,49,23,2,36,67,6,67,7,67,44,18], "k": 8 } assert my_solution.countSubarrays(**test_input) == 1034 test_input = { "nums": [191,2,46,65,191,166,191,156,157,181,167,123,26,191,191,104,33,126,51,191,191,191,6,152,74,84,126,191,191,162,188,38,30,191,191,125,30,56,12,151,45,163,91,168,15,125,60,4,108,27,67,97,125,147,167,152,191,159,142,105], "k": 7 } assert my_solution.countSubarrays(**test_input) == 647 test_input = { "nums": [2,4,11,30,23,1,8,18,4,6,30,30,30,10,30,17,24,13,17,30,25,30,30,12,15,29,24,28,21,30,25,11,1,30,9,30,21,3,10,6,30,5,5,24,21,30,17,29,21,30,3,30,8,18,17], "k": 7 } assert my_solution.countSubarrays(**test_input) == 584 test_input = { "nums": [141,106,141,141,94,98,33,141,2,115,11,141,9,131,104,2,141,75,141,141,24,141,28,68,141,134,141,110,15,21,141,65,108,141,35,95,94,141,117,25], "k": 10 } assert my_solution.countSubarrays(**test_input) == 94 test_input = { "nums": [139,94,77,139,139,139,139,92,61,105,25,139,93,139,113,128,139,81,70,139,25,139,37,118,15,5,139,115,133,1], "k": 3 } assert my_solution.countSubarrays(**test_input) == 292 test_input = { "nums": [107,160,86,160,69,160,160,73,120,129,130,104,112,136,7,100,21,160,160,94,3,96,160,65,74,87,110,160,145,116,38,72,127,152,71,24,35,79,160,120,160,80,50,160,129,50,82,160,140,160,3,17,129,18,108,34,132,69,4,160,124,108,30,125,160,102,51,138,160,120,159,160,49,68,160,19,87,160,6,160,76,160,110], "k": 16 } assert my_solution.countSubarrays(**test_input) == 124 test_input = { "nums": [89,9,89,82,89,11,31,45,61,56,27,15,33,6,5,89,28,73,8,48,11,89,5,89,4,65,18,20,17,38,4,36,59,34,5,81,10,6,44,19,20,86,58,60,27,89,34,29,36,88,89,10,73], "k": 7 } assert my_solution.countSubarrays(**test_input) == 14 test_input = { "nums": [45,40,44,51,51,33,33,38,46,38,51,40,9,29,51,40,51,36,39,36,51,24,39,51,31,50,12,50,1,51,32,51,49,12,44,19,4,26,7,51,14,4,33,36,19,18,14,20,16,11,51,51,7,18,7,10,8,8,48,51,43,41,51], "k": 10 } assert my_solution.countSubarrays(**test_input) == 199 test_input = { "nums": [102,4,3,22,78,96,21,126,103,52,99,94,57,126,49,20,75,126,93,1,4,126,122,123,21,111,23,110,126,81,112,92,121,30,41,126,20,10,126,54,15,27,126,126,9,126,126,1,106,34,119,108,126,126,34,57,27,126,110,126,65,125,126,59,117,126,67,114,115,38,79,123,118,126,33,52,1,119,11,105,21,51,75,126,84], "k": 9 } assert my_solution.countSubarrays(**test_input) == 1500 test_input = { "nums": [71,122,36,39,48,158,83,20,131,41,126,1,33,19,138,133,80,106,92,2,68,158,158,111,158,50,158,81,158,138,108,36,149], "k": 4 } assert my_solution.countSubarrays(**test_input) == 171 test_input = { "nums": [39,136,153,85,134,19,34,22,5,124,116,91,122,160,112,160,22,111,160,160,113,34,40,16,160,117,61,160,31,34,145,160], "k": 6 } assert my_solution.countSubarrays(**test_input) == 72 test_input = { "nums": [14,14,1,8,2,11,14,14,5,1,8,1,6,3,14,14,14,2,9,10,14,2,3,14,2,5,5,11,10,11,14,5,3,10,5,3,1,3,14,5,13,9,2,9,3,5,14,14,2,3,10,4,14,14,10,14,2,10,9,2,7,9,11,14,9,5,1,5,13,6,10,1,7,4,13,13,9,10,2,10,3,8,14,3,14,13,1,14,8,12,1,6,12,14,14], "k": 14 } assert my_solution.countSubarrays(**test_input) == 438 test_input = { "nums": [1,7,4,10,12,10,10,1,12,1,6,6,9,7,10,6,12,10,7,9,6,10,12,8,11,9,8,3,8,3,12,12,12,3,2,2,3,1,10,2,12,12,12,9,10,1,8,10,12,4,8,8,6,2,11,7,3,3,12,12,2,7,8,11,4,3,12,5,8,12,10,2,9,6,5,10,8], "k": 7 } assert my_solution.countSubarrays(**test_input) == 1236 test_input = { "nums": [1,7,11,13,10,13,8,6,4,11,13,6,1,6,8,10,5,13,4,2,3,7,12,5,1,1,11,13,8,9,1,8], "k": 2 } assert my_solution.countSubarrays(**test_input) == 262 test_input = { "nums": [73,24,67,11,66,73,73,40,4,47,25,26,48,40,27,69,73,28,23,9,16,8,63,65,73,57,73,21,43,73,19], "k": 1 } assert my_solution.countSubarrays(**test_input) == 408 test_input = { "nums": [7,47,50,16,35,24,61,44,53,49], "k": 1 } assert my_solution.countSubarrays(**test_input) == 28 test_input = { "nums": [14,89,43,1,12,64,23,89,55,23,56,69,62,89,89,86,89,89,89,76,84,57,18,54,29,50,67,69,65,3,22,26,8,77,51,74,74,40,89,89,18,74,89,26,16,27,1,66,72,22,78,20,15,14,63,77,73,23,65,89,79,32,18,89,59,16,24,39,87,78,29,84,89,49,80,69,89,44,89,89,58,6,55,38,89,53,89,3,81,28,65,39,30,14,16,89,22,4,23,84], "k": 7 } assert my_solution.countSubarrays(**test_input) == 2045 test_input = { "nums": [16,11,12,16,5,17,11,13,12,17,16,2,3,13,1,4,10,2,17,17,8,7,4,17,11,17,8,2,15,17,4,16,9,8,17,2,17,16,17,4,6,8,12,17,16,13,4,11,9,11,16,10,17,17,10,13,17,13,1,13,3,7,4,2,15,6,11,12,17,17,7,15,9,16,7,2,17,7,17,16,5,8], "k": 8 } assert my_solution.countSubarrays(**test_input) == 1317 test_input = { "nums": [52,46,47,52,52,4,2,21,2,26,47,26,52,7,12,35,52,33,47,3,31,37,36,52,38,19,12,40,52,7,40,16,51,41,52,23,20,52,18,52,21,2,52,49,5,48,23,52,52], "k": 8 } assert my_solution.countSubarrays(**test_input) == 132 test_input = { "nums": [99,155,73,80,32,69,113,37,126,155,95,155,155,48,155,43,37,68,131,68,150,155,153,155,45,59,155,155,155,77,155,155,100,4,127,155,107,151,101,104,155,155,71,147,153,37,155,18,155,100,155,153,155,155,138,4,114,153,111,83,74,144,18,64,94,155,50,45,51,122,146,50,43], "k": 8 } assert my_solution.countSubarrays(**test_input) == 1346 test_input = { "nums": [64,156,156,119,156,35,108,82,86,18,107,156,68,83,130,86,80,8,129,95,23,7,71,131,19,156,17,21,43,156,25,156,124,51,91,156,77,88,156,156,62,105,135,142,156,156,78,156,113,156,47,156,156,22,71,49,156,57,71,156,36,84,139,156,17,49,156,121,46,7,155,156,156,156,93,150,102,81,90,52,52,91,2,63,156,49,118,77,156,156,156,79], "k": 19 } assert my_solution.countSubarrays(**test_input) == 590 test_input = { "nums": [24,9,28,46,14,41], "k": 1 } assert my_solution.countSubarrays(**test_input) == 12 test_input = { "nums": [169,19], "k": 1 } assert my_solution.countSubarrays(**test_input) == 2 test_input = { "nums": [95,109,79,198,195,198,198,97,34,43,165,198,198,195,98,198,198,170,39,78,21,198,140,187,29,107,198,132,198,174,109,187,173,198,58,38,62,179,198,68,114,198,10,198,81,198,40,10,71,82,196,128,50,153,146,101,195], "k": 12 } assert my_solution.countSubarrays(**test_input) == 182 test_input = { "nums": [9,15,39,33,43,47,15,29,14,12,48,37,9,37,15,48,48,3,1,48,37,39,43,29,43,15,35,2,33,48,28,37,48,45,9,36,3,48,29,14,48,11,24,30,38,18,24,12,47,31,22,10,29,46,14,48,15,29,43,48,37,48,46,14,32,33,15,42,9,12,48,20,35,44,48,4], "k": 10 } assert my_solution.countSubarrays(**test_input) == 274 test_input = { "nums": [37,12,14,46,29,98,149,149,149,67,97,56,81,71,11,149,32,149,119,149,44,149,43,149,149,32,75,54,24,148,41], "k": 2 } assert my_solution.countSubarrays(**test_input) == 379 test_input = { "nums": [59,101,127,118,19,55,18,127,127,26,127,103,4,127,26,43,26,125,80,127,127,112,2,107,127,127,110,122,77,127,11,86,127,127,91,27,85,86,71,36,41,127,86,37,11], "k": 6 } assert my_solution.countSubarrays(**test_input) == 418 test_input = { "nums": [82,82,42,51,82,64,13,16,36,49,22,52,82,10,72,9,6,42,80,74,37,80,73,10,82,31,78,22,14,11,82,60,76,67,82,2,61,52,79,72,77,12,23,33,44,11,82,4,14,65,19,66,56,11,75,82,42,82,56,77,82,81,51,48,19,70,33,51,9,78,62,31,41,46,13,82,82,77,55,24,49,82,82,8,3,44,34], "k": 9 } assert my_solution.countSubarrays(**test_input) == 427 test_input = { "nums": [30,83,42,83,83,60,61,60,62,83,74,32,83,83,46,82,25,81,31,83,48,15,49,43,41,83,29,36,45,53,83,74,55,63,1,19,74,2,15,83,61,82,46,48,83,83,8,45,83,80,30,33,83,83,83,22,65,79,57,15,24,25,83,83,60,60,83,44,9,29,60,69,2,83,35,7,40,74,55,83,7,21,11,59,5,80], "k": 17 } assert my_solution.countSubarrays(**test_input) == 200 test_input = { "nums": [11,25,22,14,14,29,6,28,12,14,2,15,29,2,6,27,22,29,26,29,11,1,7,27,24,29,7,29,6], "k": 1 } assert my_solution.countSubarrays(**test_input) == 371 test_input = { "nums": [173,97,53,181,161,119,152,97,69,181,123,84,83,9,169,135,86,27,119,181,64,147,7,181,154,43,83,181,14,181,45,77,181,83,181,53,181,117,181,27,181,174,181,47], "k": 6 } assert my_solution.countSubarrays(**test_input) == 302 test_input = { "nums": [124,52,111,24,191,117,128,153,69,190,51,1,112,52,28,191,188,191,1,124,128,111,191,94,34,167,191,191,9,191,164,60,113,69,151,130,15,86,150,191,175,36,113,23,119,68,191,87,90,159,178,50,104,191,187,48,97,100,136,155,140,132,1,180,182,191,130,110,191,191,191,191,191,177,73,118,191,27,129,124,43,140,191,132,191,41,44,191,169,49,191,191,191,191,113,4], "k": 7 } assert my_solution.countSubarrays(**test_input) == 2434 test_input = { "nums": [86,89,92,23,92,72,41,92,92,92,47,30,46,76,20,80,92,60,20,9,92,92,92,36,4,38,92,74,15,20,92,2,73,58,68,2,29,13,92,91,92,44,46,8,57,10,47,92,6,90,92,92,76,92,86,26,22,67,92,92,17,92,18,23,22,40,7], "k": 9 } assert my_solution.countSubarrays(**test_input) == 738 test_input = { "nums": [75,65,37,83,80,17,69,83,83,76,64,58,13,83,18,66,25,55,25,60,83,83,83,50,70,39,82,83,47,39,74,83,75,83,34,8,81,46,52,72,45,65,46,2,9,4,23,47,83,83,59,32,54,43,53,56,83], "k": 9 } assert my_solution.countSubarrays(**test_input) == 256 test_input = { "nums": [23,70,2,70,49,65,6,69,5,26,29,70,70,15,17,22,70,63,51,25,18,68,31,3,43,60,70,6,61,23,46,21,66,67,63,3,7,70,66,47,15,65,52,70,70,38,8,18,29,33,50,9,70,9], "k": 1 } assert my_solution.countSubarrays(**test_input) == 1305 test_input = { "nums": [23,12,6,3,4,7,23,23,6,23,23,9,23,2,14,11,21,23,8,9,19,10,17,23,11,3,13,23,18,3,6,7,6,19,17,14,17,7,23,21,10,22,6,23,23,3,1,20,14,7,19,20,23,19,23,15,4,23,2,6,20,23,8,6,17,14,23,6,10,23,17,6,11,8,3,6,23,16,19,16,2,19,2,23,1,16,20,4,20,12,1], "k": 11 } assert my_solution.countSubarrays(**test_input) == 942 test_input = { "nums": [199,146,138,199,97,169,199,198,199,199,11,62,68,122,193,199,22,41,199,181,199,157,199,44,199,199,199,142,132,112,199,199,155,199,97,101,26,52,199,45,164,112,188,97,180,103,199,3,130,64,131,199,194,135,36,199,80,67,41,67,158,183,188,12,126], "k": 13 } assert my_solution.countSubarrays(**test_input) == 420 test_input = { "nums": [25,32,40,47,35,9,39,58,67,42,77,57,77,77,34,28,13,77,15,33,77,10,64,67,35,21,61,60,74,57,77,71,28,77,48,67,17,48,77,77,77,60,26,30,77,49,77,3,77,33,75,77,20,77,77], "k": 9 } assert my_solution.countSubarrays(**test_input) == 325 test_input = { "nums": [50,108,19,118,46,45,126,118,89,126,46,63,30,126,120,10,126,126,108,95,126,94], "k": 3 } assert my_solution.countSubarrays(**test_input) == 107 test_input = { "nums": [28,94,94,5,1,74,33,3,88,76,78,30], "k": 1 } assert my_solution.countSubarrays(**test_input) == 32 test_input = { "nums": [44,4,4,31,33,51,51,40,51,2,27,48,51,6,51,27,45,1,25,2,20,43,51,12,11,44,40,28,29,51,51,45,30,24,51,51,30,51,13,18,29,51,15,11,39], "k": 11 } assert my_solution.countSubarrays(**test_input) == 52 test_input = { "nums": [6,30,19,32,24,8,28,2,18,32,5,31,3,31,28,30,30,22,32,22,31,1,9,2,7,32,14,24,24,6,23,6,25,32,32,22,10,11,4,2,32,18,15,1,22,20,6,26,11,13,26,22,32,30,18], "k": 4 } assert my_solution.countSubarrays(**test_input) == 570 test_input = { "nums": [17,41,71,95,56,88,25,95,73,95,91,95,8,43,2,52,95,88,5,49,20,48,95,84,95,44,27,95,87,32,45,95,95,95,51,56,6,5,65,21,52,56,84,95,75,33,95,62,47,95], "k": 13 } assert my_solution.countSubarrays(**test_input) == 20 test_input = { "nums": [5,12,6,13,11,13,9,13,10,13,13,12,7,11,2,11,4,7,6,6,13,9,1,12,13,11,7,11,11,13,2,13,7,4,9,5,13,8,4,1,2,5,13,7,7,12,2,2,8,11,12,1,8,5,3,6,4,2,9,10,6,6,13,12,13,6,13,13,13,13,13,3,4,4,10,1,2,12,12,13,13,6,13,4,13,1,12,11,9,12,2,5], "k": 3 } assert my_solution.countSubarrays(**test_input) == 3240 test_input = { "nums": [13,16,2,27,10,2,44,44,44,28,44,44,23], "k": 4 } assert my_solution.countSubarrays(**test_input) == 23 test_input = { "nums": [69,46,80,10,80,48,76,15,67,1,80,80,34,4,14,15,2,38,62,31,17,56,58,17,38,29,67], "k": 4 } assert my_solution.countSubarrays(**test_input) == 48 test_input = { "nums": [39,38,136,136,97,122,54,102,112,125,135,57,136], "k": 1 } assert my_solution.countSubarrays(**test_input) == 52 test_input = { "nums": [39,67,17,52,89,63,52,8,14,90,76,2,90,65,90,80,90,33,61,76,90,32,43,55,62,24,29,90,35,36,90,8,40,1,72,54,64,90,58,88,77,89,35,79,90,81,90], "k": 2 } assert my_solution.countSubarrays(**test_input) == 822 test_input = { "nums": [16,22,10,22,4,16,16,15,3,22,22,15,7,7,21,17,16,1,10,13,16,17,2,18,2,5,11], "k": 3 } assert my_solution.countSubarrays(**test_input) == 70 test_input = { "nums": [120,58,118,34,32,110,94,10,119,133,70,154,151,107,124,148,154,154,24,154,6,83,20,6,3,72,154,28,148,107,154,73,126,154,41], "k": 5 } assert my_solution.countSubarrays(**test_input) == 135 test_input = { "nums": [15,2,1,21,20,33,16,19], "k": 1 } assert my_solution.countSubarrays(**test_input) == 18 test_input = { "nums": [45,25], "k": 1 } assert my_solution.countSubarrays(**test_input) == 2 test_input = { "nums": [179,127,54,149,90,119,179,127,115,82,159,128,6,55,33,43,2,172,105,159,83,179,30,179,175,125,179,179,105,179,74,77,179,153,145,124,70,179,129,31,62,172,179,29,130,179,82,64,98,179,91,179,89,166,60,159,54,179,179,137,54,158,64,177,56,165,97,142,90,170,179,127,111,179,145,179,8], "k": 16 } assert my_solution.countSubarrays(**test_input) == 61 test_input = { "nums": [25,41,11,41,26,30,41,34,31,41,40,23,14,41,10,34,8,15,41,10,14,41,37,20,37,35,37,8,21,30,11,7,33,3,25,1,3,38,27,26,27,20,29,41,30,7,23,15,41,41,41,25,18,41,19,41,34,35,33,41,4,41,15], "k": 3 } assert my_solution.countSubarrays(**test_input) == 1227 test_input = { "nums": [7,6,3,9,6,3,4,4,9,7,3,3,8,9,2,4,8,8,8,6,3,2,9,9,9,4,2,6,9,3], "k": 1 } assert my_solution.countSubarrays(**test_input) == 396 test_input = { "nums": [158,2,138,177,96,104,175,81,46,19,85,1,174,177,115,145,32,177,174,95,96,101,177,114,115,137,77,98,15,177,125,162,177,177,111,106,112,177,174,40,177,177,176,40,177,145,177,99,177,177,163,177,143,147,177,11,142,177,44,171,52,98,177,163,140,139,61,147,71,20,177,45,172], "k": 5 } assert my_solution.countSubarrays(**test_input) == 1642 test_input = { "nums": [3,1], "k": 1 } assert my_solution.countSubarrays(**test_input) == 2 test_input = { "nums": [99,166,166,5,166,44,83,73,40,64,166,135,166,24,166,41,70,93,166,166,166,49,157,3,135,137,1,133,18,166,15,82,4,166,13,55,95,166,166,151,102,166,166,34,32,31,48,166,166,13,166,166,94,28,166,166,119,103,157,12,103,19,126,13,117,71,85,166,166,81,132,105,128,166,166,125,73,161,166,139,6,32,5,31,137], "k": 24 } assert my_solution.countSubarrays(**test_input) == 49 test_input = { "nums": [121,135,135,135,57,18,7,22,135,57,96,72,23,68,32,39,135,135,135,135,51,25,100,49,72,135,99,38,126,110,52,63,48,135,135,132,111,114,135,135,24,125,135,135,120,93,55,40,135,44,135,22,135,48,35,12,116,79,80,22,135,135,111,135], "k": 20 } assert my_solution.countSubarrays(**test_input) == 7
1,702,175,400
weekly-contest-375-count-the-number-of-good-partitions
https://leetcode.com/problems/count-the-number-of-good-partitions
count-the-number-of-good-partitions
{ "questionId": "3212", "questionFrontendId": "2963", "title": "Count the Number of Good Partitions", "titleSlug": "count-the-number-of-good-partitions", "isPaidOnly": false, "difficulty": "Hard", "likes": 163, "dislikes": 1, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、由 正整数 组成的数组 nums。 将数组分割成一个或多个 连续 子数组,如果不存在包含了相同数字的两个子数组,则认为是一种 好分割方案 。 返回 nums 的 好分割方案 的 数目。 由于答案可能很大,请返回答案对 109 + 7 取余 的结果。 示例 1: 输入:nums = [1,2,3,4] 输出:8 解释:有 8 种 好分割方案 :([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]) 和 ([1,2,3,4]) 。 示例 2: 输入:nums = [1,1,1,1] 输出:1 解释:唯一的 好分割方案 是:([1,1,1,1]) 。 示例 3: 输入:nums = [1,2,1,3] 输出:2 解释:有 2 种 好分割方案 :([1,2,1], [3]) 和 ([1,2,1,3]) 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 """ class Solution: def numberOfGoodPartitions(self, nums: List[int]) -> int:
给你一个下标从 0 开始、由 正整数 组成的数组 nums。 将数组分割成一个或多个 连续 子数组,如果不存在包含了相同数字的两个子数组,则认为是一种 好分割方案 。 返回 nums 的 好分割方案 的 数目。 由于答案可能很大,请返回答案对 109 + 7 取余 的结果。 示例 1: 输入:nums = [1,2,3,4] 输出:8 解释:有 8 种 好分割方案 :([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]) 和 ([1,2,3,4]) 。 示例 2: 输入:nums = [1,1,1,1] 输出:1 解释:唯一的 好分割方案 是:([1,1,1,1]) 。 示例 3: 输入:nums = [1,2,1,3] 输出:2 解释:有 2 种 好分割方案 :([1,2,1], [3]) 和 ([1,2,1,3]) 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def numberOfGoodPartitions(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [1,1,1,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [1,2,1,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [1] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [100000] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [1000000000] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [1,1,1,3,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [1,1,1,9,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [1,1,5,9,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [1,4,1,7,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [1,5,1,5,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [1,5,1,10,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [1,6,8,1,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [1,6,9,4,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [1,7,1,6,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [1,9,1,1,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [2,1,6,7,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [2,3,2,6,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [2,3,2,8,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [2,3,9,2,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [2,4,2,7,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [2,4,7,1,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [2,5,1,2,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [2,5,1,4,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [2,5,6,4,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [2,6,1,9,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [2,7,8,9,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [2,9,1,2,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [2,10,4,2,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [3,3,3,9,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [3,3,8,1,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [3,3,10,4,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [3,4,5,1,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [3,7,6,4,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [3,8,10,7,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [3,10,10,10,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [4,1,2,3,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [4,1,7,9,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [4,8,4,8,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [5,1,1,9,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [5,2,8,10,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [5,3,6,6,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [5,3,8,8,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [5,4,5,9,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [5,4,10,2,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [5,5,7,3,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [5,5,8,4,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [5,9,1,9,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [5,10,1,1,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [6,1,7,9,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [6,3,5,1,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [6,3,9,9,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [6,6,5,5,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [6,7,3,3,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [6,8,6,5,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [6,9,10,5,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [6,10,2,6,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [6,10,5,4,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [7,1,2,3,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [7,1,9,2,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [7,2,1,2,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [7,5,1,10,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [7,5,7,4,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [7,5,8,4,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [7,9,4,8,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [7,9,8,8,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [8,2,2,5,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [8,3,2,1,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [8,3,4,9,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [8,3,6,5,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [8,3,8,3,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [8,3,8,3,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [8,4,4,5,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [8,4,8,10,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [8,5,5,3,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [8,7,4,2,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [8,7,6,10,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [8,9,7,3,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [8,10,5,9,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [9,1,2,2,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [9,1,4,3,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [9,1,7,8,9] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [9,2,7,5,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [9,3,6,5,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [9,4,7,1,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [9,5,5,8,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [9,7,2,7,10] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [9,7,8,3,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [9,8,2,5,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [9,8,2,10,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [9,8,3,7,6] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [9,8,6,6,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 test_input = { "nums": [9,8,8,2,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [9,8,9,5,7] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [10,1,3,2,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [10,1,10,3,5] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [10,2,1,4,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [10,3,1,3,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 4 test_input = { "nums": [10,5,6,2,8] } assert my_solution.numberOfGoodPartitions(**test_input) == 16 test_input = { "nums": [10,6,5,10,2] } assert my_solution.numberOfGoodPartitions(**test_input) == 2
1,702,175,400
biweekly-contest-119-find-common-elements-between-two-arrays
https://leetcode.com/problems/find-common-elements-between-two-arrays
find-common-elements-between-two-arrays
{ "questionId": "3206", "questionFrontendId": "2956", "title": "Find Common Elements Between Two Arrays", "titleSlug": "find-common-elements-between-two-arrays", "isPaidOnly": false, "difficulty": "Easy", "likes": 66, "dislikes": 25, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0开始的整数数组nums1和nums2,它们分别含有 n和 m个元素。 请你计算以下两个数值: 统计0 <= i < n中的下标i,满足nums1[i]在 nums2中 至少出现了一次。 统计0 <= i < m中的下标i,满足nums2[i]在 nums1中 至少出现了一次。 请你返回一个长度为 2的整数数组answer,按顺序分别为以上两个数值。 示例 1: 输入:nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6] 输出:[3,4] 解释:分别计算两个数值: - nums1 中下标为 1 ,2 和 3 的元素在 nums2 中至少出现了一次,所以第一个值为 3 。 - nums2 中下标为 0 ,1 ,3 和 4 的元素在 nums1 中至少出现了一次,所以第二个值为 4 。 示例 2: 输入:nums1 = [3,4,2,3], nums2 = [1,5] 输出:[0,0] 解释:两个数组中没有公共元素,所以两个值都为 0 。 提示: n == nums1.length m == nums2.length 1 <= n, m <= 100 1 <= nums1[i], nums2[i] <= 100 """ class Solution: def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
给你两个下标从 0开始的整数数组nums1和nums2,它们分别含有 n和 m个元素。 请你计算以下两个数值: 统计0 <= i < n中的下标i,满足nums1[i]在 nums2中 至少出现了一次。 统计0 <= i < m中的下标i,满足nums2[i]在 nums1中 至少出现了一次。 请你返回一个长度为 2的整数数组answer,按顺序分别为以上两个数值。 示例 1: 输入:nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6] 输出:[3,4] 解释:分别计算两个数值: - nums1 中下标为 1 ,2 和 3 的元素在 nums2 中至少出现了一次,所以第一个值为 3 。 - nums2 中下标为 0 ,1 ,3 和 4 的元素在 nums1 中至少出现了一次,所以第二个值为 4 。 示例 2: 输入:nums1 = [3,4,2,3], nums2 = [1,5] 输出:[0,0] 解释:两个数组中没有公共元素,所以两个值都为 0 。 提示: n == nums1.length m == nums2.length 1 <= n, m <= 100 1 <= nums1[i], nums2[i] <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]: ```
my_solution = Solution() test_input = { "nums1": [4,3,2,3,1], "nums2": [2,2,5,2,3,6] } assert my_solution.findIntersectionValues(**test_input) == [3,4] test_input = { "nums1": [3,4,2,3], "nums2": [1,5] } assert my_solution.findIntersectionValues(**test_input) == [0,0] test_input = { "nums1": [24,28,7,27,7,27,9,24,9,10], "nums2": [12,29,9,7,5] } assert my_solution.findIntersectionValues(**test_input) == [4,2] test_input = { "nums1": [10,30,16,18], "nums2": [23,30,30,6,10,26,9,27,6,16,18,10,27,2,20,7,16] } assert my_solution.findIntersectionValues(**test_input) == [4,7] test_input = { "nums1": [7,23,27,20,21,29,7,27,27,18,7,6,20,10], "nums2": [27,27,28,24,20,4,6,17,9,29,20,14,20] } assert my_solution.findIntersectionValues(**test_input) == [7,7] test_input = { "nums1": [15,30,6,6], "nums2": [15,4,16,10,7,23,24,3,4,6,14,8,18,1,29,27,2,17] } assert my_solution.findIntersectionValues(**test_input) == [3,2] test_input = { "nums1": [24,7,8,6,22,28,22,28,7,19], "nums2": [3,7,28,7,3,3] } assert my_solution.findIntersectionValues(**test_input) == [4,3] test_input = { "nums1": [23,4,26,17,23,13], "nums2": [24,17,20,16,1,13,17,28,17] } assert my_solution.findIntersectionValues(**test_input) == [2,4] test_input = { "nums1": [5,8,18,27,16,29,27,12,1,29,16,27,22,19,14,12,11,25], "nums2": [24,8,16] } assert my_solution.findIntersectionValues(**test_input) == [3,2] test_input = { "nums1": [29,17,30,17,15,30,11,2,24,28,28,30,30,27,30,2,30,9,1,7], "nums2": [12,12,11,21,2,28,5,24,12,17,24,29,22,19,11,17,1,23] } assert my_solution.findIntersectionValues(**test_input) == [10,10] test_input = { "nums1": [4,27,12,16,16,21,26,7,19,21,24,26,12,24,22,12,16], "nums2": [1,25,8,27,23,27,27,24] } assert my_solution.findIntersectionValues(**test_input) == [3,4] test_input = { "nums1": [27,19,20,16,24,27,27,24], "nums2": [30,21,21,6,17,16] } assert my_solution.findIntersectionValues(**test_input) == [1,1] test_input = { "nums1": [3,19,21,5,24,26,22,22,5], "nums2": [23,26,20,14,30,9,10,24,19,22,19,6,3,20,22,22,5,24,24] } assert my_solution.findIntersectionValues(**test_input) == [8,11] test_input = { "nums1": [13,13,29,12], "nums2": [29,29,13,7,30,22] } assert my_solution.findIntersectionValues(**test_input) == [3,3] test_input = { "nums1": [30,4,16,14,14,14,20,15,20,30,6,10,14], "nums2": [30,16,20,2,18,10,5,6,30,20,22,18,14,23,15] } assert my_solution.findIntersectionValues(**test_input) == [12,9] test_input = { "nums1": [22,1,22,4,11,22,4,20,11,29,11,11,4,26,20,12,20,8,26,17], "nums2": [4,17,7,15] } assert my_solution.findIntersectionValues(**test_input) == [4,2] test_input = { "nums1": [30,15,16,15,11,16,26,15,21], "nums2": [22,25,27,2,26,20,18,15,26,20,16] } assert my_solution.findIntersectionValues(**test_input) == [6,4] test_input = { "nums1": [5,6], "nums2": [13,12,8,5,19,13,27] } assert my_solution.findIntersectionValues(**test_input) == [1,1] test_input = { "nums1": [27,28,15,20,5,13,28,29,24,29,20,15,5,20,20,25,9,20,24,20], "nums2": [16,20,13,24,11] } assert my_solution.findIntersectionValues(**test_input) == [9,3] test_input = { "nums1": [25,7,18], "nums2": [28,1,14,22,24,8,25,17] } assert my_solution.findIntersectionValues(**test_input) == [1,1] test_input = { "nums1": [10,15], "nums2": [4,10,15,28] } assert my_solution.findIntersectionValues(**test_input) == [2,2] test_input = { "nums1": [11,11,25], "nums2": [11,28,25,13,23,11] } assert my_solution.findIntersectionValues(**test_input) == [3,3] test_input = { "nums1": [10,30,27,8,8,5,11,12,17,13,14,27,17,19,13,20,27], "nums2": [10,14,25,2,17,29,10,9,5,30,15,27] } assert my_solution.findIntersectionValues(**test_input) == [9,7] test_input = { "nums1": [19,22,22,22,22,29,22,28,29], "nums2": [7,28,29,22,16,22,22,4,17,11,22,22,22,25,25] } assert my_solution.findIntersectionValues(**test_input) == [8,8] test_input = { "nums1": [18,1,23,1,1], "nums2": [16,9,1,4,15,11] } assert my_solution.findIntersectionValues(**test_input) == [3,1] test_input = { "nums1": [30,11,15,1,15,6,5,26,15,15], "nums2": [1,20,19,30,17,10,6,15] } assert my_solution.findIntersectionValues(**test_input) == [7,4] test_input = { "nums1": [17,6,30,30,15,30,22,2,18,22,21,21,17,19,25,30,18,30,1], "nums2": [2,16,25,5,25,1,14,11] } assert my_solution.findIntersectionValues(**test_input) == [3,4] test_input = { "nums1": [3,21,21,23,14], "nums2": [1,28,1,3,27,15,28,29,22,14,8,24] } assert my_solution.findIntersectionValues(**test_input) == [2,2] test_input = { "nums1": [8,20,29,23,29,2,2,2,20], "nums2": [2,24,20,28,11,8,6,25] } assert my_solution.findIntersectionValues(**test_input) == [6,3] test_input = { "nums1": [22,27,4,27,30,22,25,8,8,30,1,16,1], "nums2": [9,21,8,12] } assert my_solution.findIntersectionValues(**test_input) == [2,1] test_input = { "nums1": [19,11,13,1,26,25,19,24,3,10,1,11,1,15,20,20,26,13,13], "nums2": [13,23] } assert my_solution.findIntersectionValues(**test_input) == [3,1] test_input = { "nums1": [21,16,11,21], "nums2": [21,11,21,2,2,8,16,29,16,16,18,14,18,16,29,10,2] } assert my_solution.findIntersectionValues(**test_input) == [4,7] test_input = { "nums1": [15,7,23,12,23,16,18,1,16,28,28,19,7,30,19], "nums2": [9,1,10,15,23,8,8,24,30] } assert my_solution.findIntersectionValues(**test_input) == [5,4] test_input = { "nums1": [2,2,22,24,20,22,1,27,27,10,8,26,22,22,22,10,13,29], "nums2": [8,11,1,11] } assert my_solution.findIntersectionValues(**test_input) == [2,2] test_input = { "nums1": [25,29,15,15,21,14,10,23,10,18,11,30,28,16,29], "nums2": [1,16,10,2,25,1,15] } assert my_solution.findIntersectionValues(**test_input) == [6,4] test_input = { "nums1": [18,18,11,27,18,20,20], "nums2": [16,28,25,28,20,15,8,21,4,6,19,20,20,20,29] } assert my_solution.findIntersectionValues(**test_input) == [2,4] test_input = { "nums1": [1,25,15,20,25,11,4,1,1,21,17,1,19], "nums2": [19,19,9,23,1,5,28,28,17,28,3,9,8] } assert my_solution.findIntersectionValues(**test_input) == [6,4] test_input = { "nums1": [7,30,7,7,30,2,7,7], "nums2": [19,7,1,7,17,17,20,7,21,30,8,21,10,30,14] } assert my_solution.findIntersectionValues(**test_input) == [7,5] test_input = { "nums1": [7,18,13,27,13,9,22,30], "nums2": [27,29,21,30,16,13,29,5,9,16,29,27] } assert my_solution.findIntersectionValues(**test_input) == [5,5] test_input = { "nums1": [19,19,25,24,24,3,19,24,3], "nums2": [16,19,19,17,19,24,5,19] } assert my_solution.findIntersectionValues(**test_input) == [6,5] test_input = { "nums1": [19,11,3,11,22,12,23,12,29,19,25,15,23,23], "nums2": [4,29,19,23,23,10,2,10,10,15,19,20,19,12,2,19,15,29] } assert my_solution.findIntersectionValues(**test_input) == [9,11] test_input = { "nums1": [25,21], "nums2": [20,12,5,13,21,25,9,30,21,7,21,12,20,7] } assert my_solution.findIntersectionValues(**test_input) == [2,4] test_input = { "nums1": [16,17,16,20,29,16,30,24], "nums2": [1,30,24] } assert my_solution.findIntersectionValues(**test_input) == [2,2] test_input = { "nums1": [10,6,7,24,17,24,3,24], "nums2": [24,27,26,8,7,3,19,24,6,7,30,6] } assert my_solution.findIntersectionValues(**test_input) == [6,7] test_input = { "nums1": [3,26,7,6,23,22,26,8,11,23,17,26,7,2], "nums2": [13,11,10,8,4,23] } assert my_solution.findIntersectionValues(**test_input) == [4,3] test_input = { "nums1": [29,10,9,26,30,21,11,26,30], "nums2": [2,9,12,9,30,9,30,21,8,3,17,15,25,26,9,15] } assert my_solution.findIntersectionValues(**test_input) == [6,8] test_input = { "nums1": [14,29,15,12,20,27,24,29,4,29,12,6,12,4,7], "nums2": [2,19,6,29,10,20,26,11,11,19,4,12,30,22,13,4,24] } assert my_solution.findIntersectionValues(**test_input) == [11,7] test_input = { "nums1": [11,5,3,4,15,30,25,25,30,6,3,28,25,6,30,17,15], "nums2": [4,25,17,2,24,28,25,15,4,25,8,6,15] } assert my_solution.findIntersectionValues(**test_input) == [10,10] test_input = { "nums1": [5,23,17,6,5,15,29,2,7,27,5], "nums2": [28,14,1,1,27,26,23,20,6,17,11] } assert my_solution.findIntersectionValues(**test_input) == [4,4] test_input = { "nums1": [26,20,12,2,11,23,8,28,28,2,28,20,2,13,13,28,22], "nums2": [8,7,12,15,20] } assert my_solution.findIntersectionValues(**test_input) == [4,3] test_input = { "nums1": [15,6,14,24,6,22,6,24,6,6,6,16,24,3,7,6], "nums2": [11,6,18,20,12,14,17,3,11,6,2,3,17,19,3] } assert my_solution.findIntersectionValues(**test_input) == [9,6] test_input = { "nums1": [21,10,13,2,3,29,2,29,12,21,16,7,21,26], "nums2": [26,16,18,29,16,15,2,16,23,24,26,21,26,13,4,29,13,17,10] } assert my_solution.findIntersectionValues(**test_input) == [11,13] test_input = { "nums1": [5,18,7,30,16,1,24,5,1,15,28,24,25], "nums2": [20,29,16,14] } assert my_solution.findIntersectionValues(**test_input) == [1,1] test_input = { "nums1": [1,11,11,28,28,10,15,28,6], "nums2": [27,21,28,18,7,7,20,26,4,28,11,22,16,30,11,9,9] } assert my_solution.findIntersectionValues(**test_input) == [5,4] test_input = { "nums1": [27,3], "nums2": [29,29,27,1,26,21,27,1,8,3,7,24,19] } assert my_solution.findIntersectionValues(**test_input) == [2,3] test_input = { "nums1": [19,20,25,16,22,23,25,16,23,16,23,14], "nums2": [16,5] } assert my_solution.findIntersectionValues(**test_input) == [3,1] test_input = { "nums1": [9,9,5,28,22,15,11,28,5,3,15,6,16,13,29,30], "nums2": [18,12,3,5,24,15] } assert my_solution.findIntersectionValues(**test_input) == [5,3] test_input = { "nums1": [21,19,11,24,7,5,10], "nums2": [19,19,14,3,4,14,27,18,14,10] } assert my_solution.findIntersectionValues(**test_input) == [2,3] test_input = { "nums1": [6,18,18,20,5,18,1,15,18,26,28,26], "nums2": [13,12,2,24,20,28,27,20,11] } assert my_solution.findIntersectionValues(**test_input) == [2,3] test_input = { "nums1": [18,14,14,15,10,14,7,1,28,15], "nums2": [11,18,15,18,27,12] } assert my_solution.findIntersectionValues(**test_input) == [3,3] test_input = { "nums1": [29,18,29,18,27,11,11,8,4,18,11,14,5,21,21,29,11], "nums2": [25,29,15,17,27,20,9,23,11,13,26,8,11,6] } assert my_solution.findIntersectionValues(**test_input) == [9,5] test_input = { "nums1": [14,5,8,21,24,5,21,19,29], "nums2": [15,10,9,13,24,4,9,10,3,6,5,20,24,26,14,27,14,10,22] } assert my_solution.findIntersectionValues(**test_input) == [4,5] test_input = { "nums1": [2,11,11,9,25,11,27,16,28,10,18,3,22,15,16,11], "nums2": [11,3,21,9,3,13,23,9,28,25,8,28,29,2,23,12,13,14,14] } assert my_solution.findIntersectionValues(**test_input) == [9,9] test_input = { "nums1": [12,11,23,17,23,3,17], "nums2": [18,20,8,29,28,27,14,28,13,25,24,2,11,23] } assert my_solution.findIntersectionValues(**test_input) == [3,2] test_input = { "nums1": [8,18,7,7,7,24,16,8,23,23,16,16,3,16,22,18,8], "nums2": [29,3,14,22,17,22,25,25,1,23,6,23,7,12,16] } assert my_solution.findIntersectionValues(**test_input) == [11,7] test_input = { "nums1": [25,9,11,13,21,3,7,24,29,14,2,7,18,30,18], "nums2": [2,3,28,3,25,25,21,10,4,19,23,11,27] } assert my_solution.findIntersectionValues(**test_input) == [5,7] test_input = { "nums1": [5,8,12,18,5,8], "nums2": [12,19,30,16,13] } assert my_solution.findIntersectionValues(**test_input) == [1,1] test_input = { "nums1": [14,22,29,29,3,22,4,29,28,27], "nums2": [14,29] } assert my_solution.findIntersectionValues(**test_input) == [4,2] test_input = { "nums1": [28,28,11,5,18,5,18,17,21,4,9,4], "nums2": [19,6,12,17,13] } assert my_solution.findIntersectionValues(**test_input) == [1,1] test_input = { "nums1": [24,29,19,25,7,26,7,25,7,25,2], "nums2": [9,4,2,20,29,1,27] } assert my_solution.findIntersectionValues(**test_input) == [2,2] test_input = { "nums1": [19,14,14,21,14,11,21,18,11,14,18,28,4], "nums2": [25,30,1] } assert my_solution.findIntersectionValues(**test_input) == [0,0] test_input = { "nums1": [9,17,21,21,18,9,9,16,9,3,17,9,3], "nums2": [9,10,20,7,3,13,13,22,15] } assert my_solution.findIntersectionValues(**test_input) == [7,2] test_input = { "nums1": [21,14,14,14,5,11,8,7,9,3,7,3], "nums2": [2,24,28,8,15,5,3,6,14,3,19,25,5] } assert my_solution.findIntersectionValues(**test_input) == [7,6] test_input = { "nums1": [3,17,13,18,18,12,5,12,27,6,3,13,7,3,12,27,6], "nums2": [17,28,13,26,12,27,20,12,27,7,10,24] } assert my_solution.findIntersectionValues(**test_input) == [9,7] test_input = { "nums1": [18,9,30,9,3,13,25,5,30,25,13,19,25,3,28,29,9,9,9,12], "nums2": [17,20,28,30,27,1,22] } assert my_solution.findIntersectionValues(**test_input) == [3,2] test_input = { "nums1": [18,19,13,20,26,26,13,13,26,25,22,20,17], "nums2": [3,21,12,12,18,20,26,17,30,6,22,13] } assert my_solution.findIntersectionValues(**test_input) == [11,6] test_input = { "nums1": [19,10,2,18,15,24,4,11,12,24,10,10,9,12,6,10,17,22,11,12], "nums2": [10,16,7,2,27,22,19,17,11,15,27,24] } assert my_solution.findIntersectionValues(**test_input) == [13,8] test_input = { "nums1": [8,8], "nums2": [8,24,8,8,19,27,7,21,8,8] } assert my_solution.findIntersectionValues(**test_input) == [2,5] test_input = { "nums1": [22,23,22], "nums2": [22,21,3,22,17,27] } assert my_solution.findIntersectionValues(**test_input) == [2,2] test_input = { "nums1": [20,10], "nums2": [10,20,12] } assert my_solution.findIntersectionValues(**test_input) == [2,2] test_input = { "nums1": [15,28,15,17,3,6], "nums2": [3,15,17,30,18,22,4] } assert my_solution.findIntersectionValues(**test_input) == [4,3] test_input = { "nums1": [30,15], "nums2": [15,25,23,26,14,30,8,19,15,8,10,14,26,15,28,30] } assert my_solution.findIntersectionValues(**test_input) == [2,5] test_input = { "nums1": [16,11,16,24,7,9,9,24], "nums2": [19,2,9,18,25,11,1,16,24,18,20,9,24,7,9,29,16,22,15] } assert my_solution.findIntersectionValues(**test_input) == [8,9] test_input = { "nums1": [13,25,8,8,1,14,8,4,8,8,25,8,22], "nums2": [17,8,13,8,8,20,26,20,8,22,17,14,8,16,26,2,23,18,18,4] } assert my_solution.findIntersectionValues(**test_input) == [10,9] test_input = { "nums1": [9,9,9,29,11,9,18,23,9,9,26,9,23,9,9,2,28,7], "nums2": [17,6,2,11,10,23,11,30,11,24,1,11,13,9,23,25] } assert my_solution.findIntersectionValues(**test_input) == [13,8] test_input = { "nums1": [27,16,16,15], "nums2": [3,16,7,16,23,16,3,26,27,30,4,28,26,24,7] } assert my_solution.findIntersectionValues(**test_input) == [3,4] test_input = { "nums1": [19,1,26,15,15], "nums2": [6,25] } assert my_solution.findIntersectionValues(**test_input) == [0,0] test_input = { "nums1": [13,29,29,12,25,22,2,25,11,3,22,13,23,19,24,24,8,30], "nums2": [20,25,12,5,2,28,14,27,24,3,21,15,25,2,12,28,19,7,5] } assert my_solution.findIntersectionValues(**test_input) == [8,9] test_input = { "nums1": [14,14,26,25,28,26], "nums2": [4,23,9,3,1,2,27,8,22,6,24] } assert my_solution.findIntersectionValues(**test_input) == [0,0] test_input = { "nums1": [14,29,2,26,14,10,1,23,28,5,17,1,21,5,28,14,6,4,18], "nums2": [1,20,7,15,18,26,5,10,8,6,27] } assert my_solution.findIntersectionValues(**test_input) == [8,6] test_input = { "nums1": [29,3,15,7,2,20,30,15,7,29,2,21], "nums2": [15,23,22,19,21,5,7,29,23,2,17,27,21,15,6,7] } assert my_solution.findIntersectionValues(**test_input) == [9,8] test_input = { "nums1": [7,23,23,15,23,10,30,23,30,10,30,17,30,10,3,7,10], "nums2": [21,21] } assert my_solution.findIntersectionValues(**test_input) == [0,0] test_input = { "nums1": [8,13,1,13,13,12,27,21,4,4,17], "nums2": [12,13,1,27,4,9,12,8,25,29,4,8,4,29,21,28,1,8,6,6] } assert my_solution.findIntersectionValues(**test_input) == [10,13] test_input = { "nums1": [6,15,7,1,7,14,21,3,30,23,22,29], "nums2": [30,1,7,29,3,4] } assert my_solution.findIntersectionValues(**test_input) == [6,5] test_input = { "nums1": [15,10,22,22,6,8,15,8,10], "nums2": [10,4,8,15,29,6,9,22,3,3,23,3,13,8,5,8,3] } assert my_solution.findIntersectionValues(**test_input) == [9,7] test_input = { "nums1": [14,4,1,27,22,14,7,22,15,3,22,8], "nums2": [30,4,4,27,6,4,16,11,23,14,4,7,21,22,9,14,4,27,17,27] } assert my_solution.findIntersectionValues(**test_input) == [8,12] test_input = { "nums1": [23,15,15,15], "nums2": [23,17,12,15,21] } assert my_solution.findIntersectionValues(**test_input) == [4,2] test_input = { "nums1": [28,29,15,19,1,23,25,9,29,25,19,11,9,19], "nums2": [9,4,11,23,13,8,24,9,23] } assert my_solution.findIntersectionValues(**test_input) == [4,5] test_input = { "nums1": [19,24,7,2,3,10,27,10,4,4,9,29,10,7], "nums2": [23,4,7,4,27,13,2,9,23] } assert my_solution.findIntersectionValues(**test_input) == [7,6] test_input = { "nums1": [24,22,17,24,22,16,1,5], "nums2": [1,27,7,22,27,13,4,5,12,8,22,18,5] } assert my_solution.findIntersectionValues(**test_input) == [4,5]
1,702,132,200
biweekly-contest-119-remove-adjacent-almost-equal-characters
https://leetcode.com/problems/remove-adjacent-almost-equal-characters
remove-adjacent-almost-equal-characters
{ "questionId": "3230", "questionFrontendId": "2957", "title": "Remove Adjacent Almost-Equal Characters", "titleSlug": "remove-adjacent-almost-equal-characters", "isPaidOnly": false, "difficulty": "Medium", "likes": 115, "dislikes": 16, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的字符串word。 一次操作中,你可以选择word中任意一个下标 i,将word[i] 修改成任意一个小写英文字母。 请你返回消除 word中所有相邻 近似相等字符的 最少操作次数。 两个字符a 和b如果满足a == b或者a 和b在字母表中是相邻的,那么我们称它们是 近似相等字符。 示例 1: 输入:word = "aaaaa" 输出:2 解释:我们将 word 变为 "acaca" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 2 次操作。 示例 2: 输入:word = "abddez" 输出:2 解释:我们将 word 变为 "ybdoez" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 2 次操作。 示例 3: 输入:word = "zyxyxyz" 输出:3 解释:我们将 word 变为 "zaxaxaz" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 3 次操作 提示: 1 <= word.length <= 100 word只包含小写英文字母。 """ class Solution: def removeAlmostEqualCharacters(self, word: str) -> int:
给你一个下标从 0开始的字符串word。 一次操作中,你可以选择word中任意一个下标 i,将word[i] 修改成任意一个小写英文字母。 请你返回消除 word中所有相邻 近似相等字符的 最少操作次数。 两个字符a 和b如果满足a == b或者a 和b在字母表中是相邻的,那么我们称它们是 近似相等字符。 示例 1: 输入:word = "aaaaa" 输出:2 解释:我们将 word 变为 "acaca" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 2 次操作。 示例 2: 输入:word = "abddez" 输出:2 解释:我们将 word 变为 "ybdoez" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 2 次操作。 示例 3: 输入:word = "zyxyxyz" 输出:3 解释:我们将 word 变为 "zaxaxaz" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 3 次操作 提示: 1 <= word.length <= 100 word只包含小写英文字母。 请完成下面的代码来解决上述问题: ```python class Solution: def removeAlmostEqualCharacters(self, word: str) -> int: ```
my_solution = Solution() test_input = { "word": "aaaaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abddez" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "zyxyxyz" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 3 test_input = { "word": "a" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "b" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "c" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "aa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "ab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "ac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "ba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "ca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "cb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "cc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "abb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "abc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "acb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "acc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "baa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bbb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bbc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bcb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bcc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "caa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "cab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "cac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "cba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "cbb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "cbc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "cca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "ccb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "ccc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aaaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "aaab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "aaac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aaba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "aabb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "aabc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "aaca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "aacb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "aacc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "abba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abbb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abbc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "abcb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abcc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "acaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "acab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "acac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 0 test_input = { "word": "acba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "acbb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "acbc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "acca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "accb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "accc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "baaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "baab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "baac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "baba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "babb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "babc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "baca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bacb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bacc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bbaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bbab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bbac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bbba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bbbb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bbbc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bbca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bbcb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bbcc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bcaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bcab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bcac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bcba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bcbb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bcbc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bcca" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "bccb" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "bccc" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "caaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "caab" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "caac" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1 test_input = { "word": "caba" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 1
1,702,132,200
biweekly-contest-119-length-of-longest-subarray-with-at-most-k-frequency
https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency
length-of-longest-subarray-with-at-most-k-frequency
{ "questionId": "3225", "questionFrontendId": "2958", "title": "Length of Longest Subarray With at Most K Frequency", "titleSlug": "length-of-longest-subarray-with-at-most-k-frequency", "isPaidOnly": false, "difficulty": "Medium", "likes": 139, "dislikes": 5, "categoryTitle": "Algorithms" }
""" 给你一个整数数组nums和一个整数k。 一个元素 x在数组中的 频率指的是它在数组中的出现次数。 如果一个数组中所有元素的频率都 小于等于k,那么我们称这个数组是 好数组。 请你返回 nums中 最长好子数组的长度。 子数组 指的是一个数组中一段连续非空的元素序列。 示例 1: 输入:nums = [1,2,3,1,2,3,1,2], k = 2 输出:6 解释:最长好子数组是 [1,2,3,1,2,3] ,值 1 ,2 和 3 在子数组中的频率都没有超过 k = 2 。[2,3,1,2,3,1] 和 [3,1,2,3,1,2] 也是好子数组。 最长好子数组的长度为 6 。 示例 2: 输入:nums = [1,2,1,2,1,2,1,2], k = 1 输出:2 解释:最长好子数组是 [1,2] ,值 1 和 2 在子数组中的频率都没有超过 k = 1 。[2,1] 也是好子数组。 最长好子数组的长度为 2 。 示例 3: 输入:nums = [5,5,5,5,5,5,5], k = 4 输出:4 解释:最长好子数组是 [5,5,5,5] ,值 5 在子数组中的频率没有超过 k = 4 。 最长好子数组的长度为 4 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 1 <= k <= nums.length """ class Solution: def maxSubarrayLength(self, nums: List[int], k: int) -> int:
给你一个整数数组nums和一个整数k。 一个元素 x在数组中的 频率指的是它在数组中的出现次数。 如果一个数组中所有元素的频率都 小于等于k,那么我们称这个数组是 好数组。 请你返回 nums中 最长好子数组的长度。 子数组 指的是一个数组中一段连续非空的元素序列。 示例 1: 输入:nums = [1,2,3,1,2,3,1,2], k = 2 输出:6 解释:最长好子数组是 [1,2,3,1,2,3] ,值 1 ,2 和 3 在子数组中的频率都没有超过 k = 2 。[2,3,1,2,3,1] 和 [3,1,2,3,1,2] 也是好子数组。 最长好子数组的长度为 6 。 示例 2: 输入:nums = [1,2,1,2,1,2,1,2], k = 1 输出:2 解释:最长好子数组是 [1,2] ,值 1 和 2 在子数组中的频率都没有超过 k = 1 。[2,1] 也是好子数组。 最长好子数组的长度为 2 。 示例 3: 输入:nums = [5,5,5,5,5,5,5], k = 4 输出:4 解释:最长好子数组是 [5,5,5,5] ,值 5 在子数组中的频率没有超过 k = 4 。 最长好子数组的长度为 4 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 1 <= k <= nums.length 请完成下面的代码来解决上述问题: ```python class Solution: def maxSubarrayLength(self, nums: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,1,2,3,1,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 6 test_input = { "nums": [1,2,1,2,1,2,1,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [5,5,5,5,5,5,5], "k": 4 } assert my_solution.maxSubarrayLength(**test_input) == 4 test_input = { "nums": [1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [3], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [4], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [5], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [6], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [7], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [8], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [9], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [10], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [1,11], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [2,11], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [3,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [4,6], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [5,8], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [6,7], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [7,9], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [8,8], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 1 test_input = { "nums": [9,8], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [10,5], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [1,1,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,1,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,2,3], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,2,4], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,3,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,3,5], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,4,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [1,4,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,5,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,5,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,6,4], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,6,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,7,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [1,7,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,8,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,8,4], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,9,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,9,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [1,10,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,1,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,2,3], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [2,2,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,3,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,3,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,4,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,4,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,5,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [2,5,4], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,6,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [2,6,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,7,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,7,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,8,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,8,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,9,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [2,9,5], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,10,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [2,10,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,1,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [3,1,4], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,2,3], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [3,2,4], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,3,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,3,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [3,4,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,4,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,5,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,5,5], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [3,6,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,6,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,7,5], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,7,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,8,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,8,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,9,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,9,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,10,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [3,10,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,1,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,1,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,2,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,2,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,3,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,3,3], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [4,4,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,4,5], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,5,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,5,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,6,1], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,6,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,7,1], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,7,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,8,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,8,4], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,9,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,9,3], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 3 test_input = { "nums": [4,10,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 3
1,702,132,200
biweekly-contest-119-number-of-possible-sets-of-closing-branches
https://leetcode.com/problems/number-of-possible-sets-of-closing-branches
number-of-possible-sets-of-closing-branches
{ "questionId": "3217", "questionFrontendId": "2959", "title": "Number of Possible Sets of Closing Branches", "titleSlug": "number-of-possible-sets-of-closing-branches", "isPaidOnly": false, "difficulty": "Hard", "likes": 110, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 一个公司在全国有 n个分部,它们之间有的有道路连接。一开始,所有分部通过这些道路两两之间互相可以到达。 公司意识到在分部之间旅行花费了太多时间,所以它们决定关闭一些分部(也可能不关闭任何分部),同时保证剩下的分部之间两两互相可以到达且最远距离不超过maxDistance。 两个分部之间的 距离 是通过道路长度之和的 最小值。 给你整数n,maxDistance和下标从 0开始的二维整数数组roads,其中roads[i] = [ui, vi, wi]表示一条从ui到vi长度为wi的无向道路。 请你返回关闭分部的可行方案数目,满足每个方案里剩余分部之间的最远距离不超过maxDistance。 注意,关闭一个分部后,与之相连的所有道路不可通行。 注意,两个分部之间可能会有多条道路。 示例 1: 输入:n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]] 输出:5 解释:可行的关闭分部方案有: - 关闭分部集合 [2] ,剩余分部为 [0,1] ,它们之间的距离为 2 。 - 关闭分部集合 [0,1] ,剩余分部为 [2] 。 - 关闭分部集合 [1,2] ,剩余分部为 [0] 。 - 关闭分部集合 [0,2] ,剩余分部为 [1] 。 - 关闭分部集合 [0,1,2] ,关闭后没有剩余分部。 总共有 5 种可行的关闭方案。 示例 2: 输入:n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]] 输出:7 解释:可行的关闭分部方案有: - 关闭分部集合 [] ,剩余分部为 [0,1,2] ,它们之间的最远距离为 4 。 - 关闭分部集合 [0] ,剩余分部为 [1,2] ,它们之间的距离为 2 。 - 关闭分部集合 [1] ,剩余分部为 [0,2] ,它们之间的距离为 2 。 - 关闭分部集合 [0,1] ,剩余分部为 [2] 。 - 关闭分部集合 [1,2] ,剩余分部为 [0] 。 - 关闭分部集合 [0,2] ,剩余分部为 [1] 。 - 关闭分部集合 [0,1,2] ,关闭后没有剩余分部。 总共有 7 种可行的关闭方案。 示例 3: 输入:n = 1, maxDistance = 10, roads = [] 输出:2 解释:可行的关闭分部方案有: - 关闭分部集合 [] ,剩余分部为 [0] 。 - 关闭分部集合 [0] ,关闭后没有剩余分部。 总共有 2 种可行的关闭方案。 提示: 1 <= n <= 10 1 <= maxDistance <= 105 0 <= roads.length <= 1000 roads[i].length == 3 0 <= ui, vi <= n - 1 ui != vi 1 <= wi <= 1000 一开始所有分部之间通过道路互相可以到达。 """ class Solution: def numberOfSets(self, n: int, maxDistance: int, roads: List[List[int]]) -> int:
一个公司在全国有 n个分部,它们之间有的有道路连接。一开始,所有分部通过这些道路两两之间互相可以到达。 公司意识到在分部之间旅行花费了太多时间,所以它们决定关闭一些分部(也可能不关闭任何分部),同时保证剩下的分部之间两两互相可以到达且最远距离不超过maxDistance。 两个分部之间的 距离 是通过道路长度之和的 最小值。 给你整数n,maxDistance和下标从 0开始的二维整数数组roads,其中roads[i] = [ui, vi, wi]表示一条从ui到vi长度为wi的无向道路。 请你返回关闭分部的可行方案数目,满足每个方案里剩余分部之间的最远距离不超过maxDistance。 注意,关闭一个分部后,与之相连的所有道路不可通行。 注意,两个分部之间可能会有多条道路。 示例 1: 输入:n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]] 输出:5 解释:可行的关闭分部方案有: - 关闭分部集合 [2] ,剩余分部为 [0,1] ,它们之间的距离为 2 。 - 关闭分部集合 [0,1] ,剩余分部为 [2] 。 - 关闭分部集合 [1,2] ,剩余分部为 [0] 。 - 关闭分部集合 [0,2] ,剩余分部为 [1] 。 - 关闭分部集合 [0,1,2] ,关闭后没有剩余分部。 总共有 5 种可行的关闭方案。 示例 2: 输入:n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]] 输出:7 解释:可行的关闭分部方案有: - 关闭分部集合 [] ,剩余分部为 [0,1,2] ,它们之间的最远距离为 4 。 - 关闭分部集合 [0] ,剩余分部为 [1,2] ,它们之间的距离为 2 。 - 关闭分部集合 [1] ,剩余分部为 [0,2] ,它们之间的距离为 2 。 - 关闭分部集合 [0,1] ,剩余分部为 [2] 。 - 关闭分部集合 [1,2] ,剩余分部为 [0] 。 - 关闭分部集合 [0,2] ,剩余分部为 [1] 。 - 关闭分部集合 [0,1,2] ,关闭后没有剩余分部。 总共有 7 种可行的关闭方案。 示例 3: 输入:n = 1, maxDistance = 10, roads = [] 输出:2 解释:可行的关闭分部方案有: - 关闭分部集合 [] ,剩余分部为 [0] 。 - 关闭分部集合 [0] ,关闭后没有剩余分部。 总共有 2 种可行的关闭方案。 提示: 1 <= n <= 10 1 <= maxDistance <= 105 0 <= roads.length <= 1000 roads[i].length == 3 0 <= ui, vi <= n - 1 ui != vi 1 <= wi <= 1000 一开始所有分部之间通过道路互相可以到达。 请完成下面的代码来解决上述问题: ```python class Solution: def numberOfSets(self, n: int, maxDistance: int, roads: List[List[int]]) -> int: ```
my_solution = Solution() test_input = { "n": 3, "maxDistance": 5, "roads": [[0,1,2],[1,2,10],[0,2,10]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 3, "maxDistance": 5, "roads": [[0,1,20],[0,1,10],[1,2,2],[0,2,2]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 1, "maxDistance": 10, "roads": [] } assert my_solution.numberOfSets(**test_input) == 2 test_input = { "n": 3, "maxDistance": 12, "roads": [[1,0,11],[1,0,16],[0,2,13]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 3, "maxDistance": 3, "roads": [[2,0,14],[1,0,15],[1,0,7]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 3, "maxDistance": 19, "roads": [[1,0,7],[0,2,18]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 3, "maxDistance": 5, "roads": [[2,0,4],[1,0,3],[1,0,2]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 4, "maxDistance": 3, "roads": [[2,1,8],[1,0,3],[0,3,8]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 5, "maxDistance": 20, "roads": [[3,2,20],[1,0,10],[0,2,19],[0,3,13],[0,4,19]] } assert my_solution.numberOfSets(**test_input) == 12 test_input = { "n": 2, "maxDistance": 30, "roads": [[1,0,33]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 3, "maxDistance": 12, "roads": [[2,1,4],[0,1,4],[0,2,6]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 3, "maxDistance": 3, "roads": [[1,0,4],[0,2,5]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 3, "maxDistance": 27, "roads": [[2,1,23],[0,1,14],[0,2,18]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 4, "maxDistance": 20, "roads": [[2,0,16],[0,1,13],[0,3,11]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 3, "maxDistance": 22, "roads": [[1,0,21],[2,1,28]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 2, "maxDistance": 2, "roads": [[1,0,6]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 3, "maxDistance": 22, "roads": [[1,0,16],[1,0,12],[0,2,14]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 2, "maxDistance": 8, "roads": [[1,0,9]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 2, "maxDistance": 18, "roads": [[1,0,3]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 4, "maxDistance": 27, "roads": [[3,2,3],[0,1,27],[0,2,6],[0,3,17]] } assert my_solution.numberOfSets(**test_input) == 10 test_input = { "n": 5, "maxDistance": 14, "roads": [[1,0,13],[2,0,19],[0,3,16],[0,4,18]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 5, "maxDistance": 8, "roads": [[1,0,3],[3,1,10],[4,0,6],[2,0,9],[3,2,11],[4,0,12]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 2, "maxDistance": 4, "roads": [[1,0,7]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 4, "maxDistance": 39, "roads": [[2,0,44],[3,2,42],[2,1,45]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 5, "maxDistance": 2, "roads": [[4,1,11],[3,1,5],[1,0,4],[0,2,7]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 3, "maxDistance": 9, "roads": [[1,0,17],[0,2,22]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 2, "maxDistance": 1, "roads": [[1,0,3]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 3, "maxDistance": 39, "roads": [[1,0,28],[0,2,35]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 4, "maxDistance": 17, "roads": [[2,1,28],[2,0,6],[1,0,28],[1,0,24],[1,0,18],[1,0,25],[0,3,10]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 5, "maxDistance": 16, "roads": [[2,1,27],[3,0,22],[2,1,4],[1,0,11],[2,1,48],[1,0,40],[4,2,33],[4,3,44],[1,0,1]] } assert my_solution.numberOfSets(**test_input) == 9 test_input = { "n": 3, "maxDistance": 23, "roads": [[2,1,20],[0,1,12],[0,2,10]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 5, "maxDistance": 3, "roads": [[4,0,5],[1,0,2],[3,0,5],[3,0,4],[4,2,5],[4,2,1]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 3, "maxDistance": 5, "roads": [[1,0,6],[2,0,7]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 3, "maxDistance": 21, "roads": [[2,1,30],[0,1,36],[0,2,44]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 5, "maxDistance": 25, "roads": [[1,0,17],[1,0,1],[2,1,24],[3,2,12],[1,0,7],[3,2,4],[2,1,15],[0,4,14]] } assert my_solution.numberOfSets(**test_input) == 14 test_input = { "n": 2, "maxDistance": 3, "roads": [[1,0,6]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 3, "maxDistance": 4, "roads": [[1,0,6],[2,1,6],[2,0,2]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 4, "maxDistance": 21, "roads": [[3,2,18],[1,0,15],[2,1,15],[3,0,19],[3,2,19]] } assert my_solution.numberOfSets(**test_input) == 9 test_input = { "n": 4, "maxDistance": 1, "roads": [[1,0,4],[1,0,2],[3,1,2],[2,1,1],[1,0,3],[2,0,3]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 3, "maxDistance": 33, "roads": [[2,1,2],[1,0,40],[2,1,43]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 4, "maxDistance": 29, "roads": [[2,1,20],[1,0,38],[2,1,15],[2,0,32],[0,3,18]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 3, "maxDistance": 1, "roads": [[2,1,4],[2,0,2],[1,0,12]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 2, "maxDistance": 2, "roads": [[1,0,3]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 3, "maxDistance": 13, "roads": [[1,0,18],[2,0,1],[2,1,2]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 3, "maxDistance": 1, "roads": [[1,0,23],[0,2,37]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 3, "maxDistance": 18, "roads": [[2,0,39],[0,1,47]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 4, "maxDistance": 6, "roads": [[3,0,6],[0,1,3],[0,2,9]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 3, "maxDistance": 35, "roads": [[1,0,10],[1,0,15],[0,2,32]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 5, "maxDistance": 10, "roads": [[4,0,38],[4,0,11],[2,0,24],[3,0,5],[2,1,18],[2,0,38],[1,0,7],[2,1,3],[2,1,2],[3,1,36]] } assert my_solution.numberOfSets(**test_input) == 10 test_input = { "n": 5, "maxDistance": 16, "roads": [[2,0,32],[4,0,17],[2,0,22],[3,1,38],[3,0,13],[3,1,34],[3,1,36],[1,0,36],[3,1,18],[3,2,10]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 2, "maxDistance": 4, "roads": [[1,0,18]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 5, "maxDistance": 13, "roads": [[3,0,5],[2,1,3],[1,0,6],[3,2,19],[2,1,29],[2,1,30],[1,0,5],[2,0,29],[4,3,15],[2,1,23]] } assert my_solution.numberOfSets(**test_input) == 12 test_input = { "n": 3, "maxDistance": 23, "roads": [[2,1,12],[1,0,8],[2,1,7]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 5, "maxDistance": 38, "roads": [[2,0,8],[2,1,10],[0,3,25],[0,4,48]] } assert my_solution.numberOfSets(**test_input) == 11 test_input = { "n": 2, "maxDistance": 4, "roads": [[1,0,2]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 4, "maxDistance": 12, "roads": [[2,1,18],[0,1,25],[0,2,24],[0,3,16]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 4, "maxDistance": 1, "roads": [[2,0,1],[3,2,2],[2,0,2],[2,0,1],[1,0,1],[1,0,1]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 3, "maxDistance": 1, "roads": [[1,0,1],[0,2,1]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 2, "maxDistance": 3, "roads": [[1,0,24]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 2, "maxDistance": 10, "roads": [[1,0,13]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 3, "maxDistance": 1, "roads": [[2,1,1],[0,1,1],[0,2,1]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 5, "maxDistance": 13, "roads": [[1,0,17],[1,0,21],[3,0,9],[1,0,10],[2,0,11],[4,1,12],[2,1,11],[1,0,18]] } assert my_solution.numberOfSets(**test_input) == 12 test_input = { "n": 2, "maxDistance": 19, "roads": [[1,0,28]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 3, "maxDistance": 8, "roads": [[2,0,7],[2,0,28],[0,1,34]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 5, "maxDistance": 7, "roads": [[1,0,32],[0,2,35],[0,3,20],[0,4,27]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 4, "maxDistance": 1, "roads": [[1,0,4],[1,0,3],[3,0,4],[1,0,7],[0,2,11]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 2, "maxDistance": 34, "roads": [[1,0,11]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 5, "maxDistance": 10, "roads": [[2,1,32],[4,3,14],[3,2,3],[2,1,21],[3,1,37],[2,1,30],[0,1,18],[0,2,26],[0,3,19],[0,4,23]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 5, "maxDistance": 14, "roads": [[2,0,19],[3,1,24],[4,3,10],[4,1,15],[0,1,21],[0,3,21],[0,4,12]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 4, "maxDistance": 30, "roads": [[2,0,5],[0,1,27],[0,3,24]] } assert my_solution.numberOfSets(**test_input) == 9 test_input = { "n": 2, "maxDistance": 6, "roads": [[1,0,23]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 5, "maxDistance": 6, "roads": [[4,0,17],[2,1,7],[4,1,23],[1,0,1],[1,0,19],[0,3,20]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 3, "maxDistance": 5, "roads": [[1,0,1],[1,0,3],[2,0,4]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 4, "maxDistance": 19, "roads": [[3,2,21],[2,1,3],[0,1,15],[0,2,22],[0,3,8]] } assert my_solution.numberOfSets(**test_input) == 9 test_input = { "n": 2, "maxDistance": 5, "roads": [[1,0,4]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 4, "maxDistance": 18, "roads": [[2,1,7],[2,0,5],[0,3,10]] } assert my_solution.numberOfSets(**test_input) == 10 test_input = { "n": 5, "maxDistance": 35, "roads": [[1,0,39],[1,0,3],[3,1,45],[2,0,21],[3,2,40],[3,0,27],[2,1,44],[4,2,6],[4,2,45],[3,0,22]] } assert my_solution.numberOfSets(**test_input) == 14 test_input = { "n": 4, "maxDistance": 2, "roads": [[1,0,2],[1,0,3],[1,0,15],[1,0,7],[0,2,4],[0,3,6]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 4, "maxDistance": 5, "roads": [[2,0,1],[1,0,3],[0,3,4]] } assert my_solution.numberOfSets(**test_input) == 10 test_input = { "n": 3, "maxDistance": 19, "roads": [[1,0,9],[0,2,4]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 4, "maxDistance": 1, "roads": [[3,0,4],[2,1,4],[1,0,4]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 2, "maxDistance": 29, "roads": [[1,0,18]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 2, "maxDistance": 11, "roads": [[1,0,28]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 2, "maxDistance": 12, "roads": [[1,0,10]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 5, "maxDistance": 9, "roads": [[3,2,23],[4,3,11],[1,0,16],[2,0,11],[2,0,16],[1,0,20],[4,0,16],[2,0,36],[3,0,7]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 5, "maxDistance": 17, "roads": [[3,2,22],[3,2,3],[4,0,19],[3,0,21],[4,3,4],[1,0,24],[3,0,7],[3,2,12],[1,0,17]] } assert my_solution.numberOfSets(**test_input) == 14 test_input = { "n": 4, "maxDistance": 5, "roads": [[2,0,26],[1,0,15],[3,2,17]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 4, "maxDistance": 4, "roads": [[1,0,11],[0,2,3],[0,3,3]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 5, "maxDistance": 13, "roads": [[3,1,16],[0,1,3],[0,2,19],[0,3,10],[0,4,2]] } assert my_solution.numberOfSets(**test_input) == 13 test_input = { "n": 4, "maxDistance": 5, "roads": [[1,0,21],[1,0,13],[1,0,19],[2,1,15],[1,0,17],[1,0,3],[0,3,1]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 5, "maxDistance": 25, "roads": [[1,0,18],[3,0,20],[2,0,17],[2,1,21],[0,4,3]] } assert my_solution.numberOfSets(**test_input) == 16 test_input = { "n": 5, "maxDistance": 15, "roads": [[2,1,33],[1,0,18],[2,0,16],[3,1,37],[3,0,26],[0,4,18]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 4, "maxDistance": 1, "roads": [[1,0,1],[1,0,1],[0,2,1],[0,3,1]] } assert my_solution.numberOfSets(**test_input) == 8 test_input = { "n": 4, "maxDistance": 10, "roads": [[2,0,22],[2,1,6],[2,0,21],[2,1,27],[3,1,12]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 2, "maxDistance": 13, "roads": [[1,0,21]] } assert my_solution.numberOfSets(**test_input) == 3 test_input = { "n": 4, "maxDistance": 31, "roads": [[3,1,7],[2,1,10],[2,0,25],[2,0,27]] } assert my_solution.numberOfSets(**test_input) == 9 test_input = { "n": 3, "maxDistance": 17, "roads": [[1,0,8],[1,0,8],[0,2,14]] } assert my_solution.numberOfSets(**test_input) == 6 test_input = { "n": 2, "maxDistance": 3, "roads": [[1,0,3]] } assert my_solution.numberOfSets(**test_input) == 4 test_input = { "n": 5, "maxDistance": 5, "roads": [[4,1,37],[4,1,7],[2,1,6],[3,2,8],[2,1,35],[1,0,28],[4,1,3],[2,1,2]] } assert my_solution.numberOfSets(**test_input) == 9 test_input = { "n": 4, "maxDistance": 1, "roads": [[2,0,2],[2,0,1],[0,1,2],[0,3,2]] } assert my_solution.numberOfSets(**test_input) == 6
1,702,132,200
weekly-contest-374-find-the-peaks
https://leetcode.com/problems/find-the-peaks
find-the-peaks
{ "questionId": "3221", "questionFrontendId": "2951", "title": "Find the Peaks", "titleSlug": "find-the-peaks", "isPaidOnly": false, "difficulty": "Easy", "likes": 91, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的数组 mountain 。你的任务是找出数组mountain 中的所有 峰值。 以数组形式返回给定数组中 峰值 的下标,顺序不限 。 注意: 峰值 是指一个严格大于其相邻元素的元素。 数组的第一个和最后一个元素 不 是峰值。 示例 1: 输入:mountain = [2,4,4] 输出:[] 解释:mountain[0] 和 mountain[2] 不可能是峰值,因为它们是数组的第一个和最后一个元素。 mountain[1] 也不可能是峰值,因为它不严格大于 mountain[2] 。 因此,答案为 [] 。 示例 2: 输入:mountain = [1,4,3,8,5] 输出:[1,3] 解释:mountain[0] 和 mountain[4] 不可能是峰值,因为它们是数组的第一个和最后一个元素。 mountain[2] 也不可能是峰值,因为它不严格大于 mountain[3] 和 mountain[1] 。 但是 mountain[1] 和 mountain[3] 严格大于它们的相邻元素。 因此,答案是 [1,3] 。 提示: 3 <= mountain.length <= 100 1 <= mountain[i] <= 100 """ class Solution: def findPeaks(self, mountain: List[int]) -> List[int]:
给你一个下标从 0 开始的数组 mountain 。你的任务是找出数组mountain 中的所有 峰值。 以数组形式返回给定数组中 峰值 的下标,顺序不限 。 注意: 峰值 是指一个严格大于其相邻元素的元素。 数组的第一个和最后一个元素 不 是峰值。 示例 1: 输入:mountain = [2,4,4] 输出:[] 解释:mountain[0] 和 mountain[2] 不可能是峰值,因为它们是数组的第一个和最后一个元素。 mountain[1] 也不可能是峰值,因为它不严格大于 mountain[2] 。 因此,答案为 [] 。 示例 2: 输入:mountain = [1,4,3,8,5] 输出:[1,3] 解释:mountain[0] 和 mountain[4] 不可能是峰值,因为它们是数组的第一个和最后一个元素。 mountain[2] 也不可能是峰值,因为它不严格大于 mountain[3] 和 mountain[1] 。 但是 mountain[1] 和 mountain[3] 严格大于它们的相邻元素。 因此,答案是 [1,3] 。 提示: 3 <= mountain.length <= 100 1 <= mountain[i] <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def findPeaks(self, mountain: List[int]) -> List[int]: ```
my_solution = Solution() test_input = { "mountain": [2,4,4] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,4,3,8,5] } assert my_solution.findPeaks(**test_input) == [1,3] test_input = { "mountain": [1,1,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,1,3] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,1,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,2,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,4,1] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [1,4,3] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [1,5,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,6,4] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,1,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [2,1,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [2,2,3] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [2,2,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [2,3,2] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,3,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [2,4,3] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,4,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [2,6,4] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [3,3,3] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [3,3,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [3,4,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [3,5,1] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [3,5,3] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [3,5,4] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [3,5,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,2,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,2,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,2,4] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,2,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,4,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,4,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,4,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,5,4] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [4,6,1] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [4,6,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,1,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,2,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,2,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,2,4] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,3,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,5,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,5,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,5,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,6,1] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [5,6,4] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [6,1,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,1,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,1,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,2,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,2,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,3,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,3,3] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,3,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,4,3] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,5,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,5,4] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [6,6,4] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,1,1,4] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,1,7,7] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,3,6,5] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [1,4,7,8] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,6,6,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,8,1,8] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,2,1,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [2,3,7,6] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [2,5,4,5] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,7,1,2] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,7,2,6] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,7,5,3] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [2,7,7,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [3,1,2,5] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [3,3,4,2] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [3,3,7,8] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [3,4,2,4] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [3,4,5,4] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [3,4,7,6] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [3,5,5,3] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [3,6,4,7] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [3,8,5,5] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [4,2,4,3] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [4,2,6,8] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,3,3,8] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [4,4,8,7] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [4,5,1,1] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [4,6,1,7] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [4,6,2,1] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [4,6,2,2] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [5,1,7,6] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [5,3,2,2] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,3,6,3] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [5,3,8,3] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [5,4,4,6] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,4,4,8] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,5,1,7] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [5,5,8,2] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [5,6,7,4] } assert my_solution.findPeaks(**test_input) == [2] test_input = { "mountain": [5,7,4,3] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [5,8,7,8] } assert my_solution.findPeaks(**test_input) == [1] test_input = { "mountain": [6,2,8,6] } assert my_solution.findPeaks(**test_input) == [2]
1,701,570,600
weekly-contest-374-minimum-number-of-coins-to-be-added
https://leetcode.com/problems/minimum-number-of-coins-to-be-added
minimum-number-of-coins-to-be-added
{ "questionId": "3231", "questionFrontendId": "2952", "title": "Minimum Number of Coins to be Added", "titleSlug": "minimum-number-of-coins-to-be-added", "isPaidOnly": false, "difficulty": "Medium", "likes": 227, "dislikes": 40, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 coins,表示可用的硬币的面值,以及一个整数 target 。 如果存在某个 coins 的子序列总和为 x,那么整数 x 就是一个 可取得的金额 。 返回需要添加到数组中的 任意面值 硬币的 最小数量 ,使范围 [1, target] 内的每个整数都属于 可取得的金额 。 数组的 子序列 是通过删除原始数组的一些(可能不删除)元素而形成的新的 非空 数组,删除过程不会改变剩余元素的相对位置。 示例 1: 输入:coins = [1,4,10], target = 19 输出:2 解释:需要添加面值为 2 和 8 的硬币各一枚,得到硬币数组 [1,2,4,8,10] 。 可以证明从 1 到 19 的所有整数都可由数组中的硬币组合得到,且需要添加到数组中的硬币数目最小为 2 。 示例 2: 输入:coins = [1,4,10,5,7,19], target = 19 输出:1 解释:只需要添加一枚面值为 2 的硬币,得到硬币数组 [1,2,4,5,7,10,19] 。 可以证明从 1 到 19 的所有整数都可由数组中的硬币组合得到,且需要添加到数组中的硬币数目最小为 1 。 示例 3: 输入:coins = [1,1,1], target = 20 输出:3 解释: 需要添加面值为 4 、8 和 16 的硬币各一枚,得到硬币数组 [1,1,1,4,8,16] 。 可以证明从 1 到 20 的所有整数都可由数组中的硬币组合得到,且需要添加到数组中的硬币数目最小为 3 。 提示: 1 <= target <= 105 1 <= coins.length <= 105 1 <= coins[i] <= target """ class Solution: def minimumAddedCoins(self, coins: List[int], target: int) -> int:
给你一个下标从 0 开始的整数数组 coins,表示可用的硬币的面值,以及一个整数 target 。 如果存在某个 coins 的子序列总和为 x,那么整数 x 就是一个 可取得的金额 。 返回需要添加到数组中的 任意面值 硬币的 最小数量 ,使范围 [1, target] 内的每个整数都属于 可取得的金额 。 数组的 子序列 是通过删除原始数组的一些(可能不删除)元素而形成的新的 非空 数组,删除过程不会改变剩余元素的相对位置。 示例 1: 输入:coins = [1,4,10], target = 19 输出:2 解释:需要添加面值为 2 和 8 的硬币各一枚,得到硬币数组 [1,2,4,8,10] 。 可以证明从 1 到 19 的所有整数都可由数组中的硬币组合得到,且需要添加到数组中的硬币数目最小为 2 。 示例 2: 输入:coins = [1,4,10,5,7,19], target = 19 输出:1 解释:只需要添加一枚面值为 2 的硬币,得到硬币数组 [1,2,4,5,7,10,19] 。 可以证明从 1 到 19 的所有整数都可由数组中的硬币组合得到,且需要添加到数组中的硬币数目最小为 1 。 示例 3: 输入:coins = [1,1,1], target = 20 输出:3 解释: 需要添加面值为 4 、8 和 16 的硬币各一枚,得到硬币数组 [1,1,1,4,8,16] 。 可以证明从 1 到 20 的所有整数都可由数组中的硬币组合得到,且需要添加到数组中的硬币数目最小为 3 。 提示: 1 <= target <= 105 1 <= coins.length <= 105 1 <= coins[i] <= target 请完成下面的代码来解决上述问题: ```python class Solution: def minimumAddedCoins(self, coins: List[int], target: int) -> int: ```
my_solution = Solution() test_input = { "coins": [1,4,10], "target": 19 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [1,4,10,5,7,19], "target": 19 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [1,1,1], "target": 20 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [1], "target": 100000 } assert my_solution.minimumAddedCoins(**test_input) == 16 test_input = { "coins": [100000], "target": 100000 } assert my_solution.minimumAddedCoins(**test_input) == 17 test_input = { "coins": [2], "target": 5 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [5,6,7], "target": 10 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [5,6,7], "target": 15 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [4,11,13,15,7,5,12,11,5,9], "target": 34 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [8,12,9], "target": 27 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [2,13,7,1,11], "target": 35 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [10,3,5,11,6], "target": 27 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [6,6,6,15,4], "target": 31 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [6,15,6], "target": 22 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [8,14,15,4,14,15,8,10,8], "target": 42 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [9,14,14,9,14,5,12,10,11], "target": 17 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [14,5,13,3,7,10,10,10], "target": 32 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [8,6,7,12], "target": 26 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [15,1,12], "target": 43 } assert my_solution.minimumAddedCoins(**test_input) == 4 test_input = { "coins": [4,1,4,10], "target": 16 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [10,2,13,5,7,15], "target": 26 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [10,1,10], "target": 10 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [9,5,13,8], "target": 30 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [13,9,4,5], "target": 37 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [1,15,5,12,13,10,14,8,1,7], "target": 29 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [14,14,6,2,9,1,4,10], "target": 38 } assert my_solution.minimumAddedCoins(**test_input) == 0 test_input = { "coins": [7,10,6,14,10,11,2], "target": 45 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [6,3,2,6,8,9,13,3,12,13], "target": 47 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [8,1,9,2,15], "target": 34 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [5,13,9,11,6,1], "target": 27 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [10,15,7,14,2,2,12,14,13], "target": 45 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [9,3,10,3,8,2,7,11,1], "target": 26 } assert my_solution.minimumAddedCoins(**test_input) == 0 test_input = { "coins": [9,11,2,5,2,7,11], "target": 28 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [5,5,15,3,13], "target": 17 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [2,2,9,10,7,15,4,3,9,15], "target": 16 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [3,1,12,15,5,10], "target": 34 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [12,7,5,2,12], "target": 50 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [11,6,10,3,1,7,11], "target": 44 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [13,12,1,11,3,4,11,9,13,13], "target": 41 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [6,4,1,9,9,2,10,7], "target": 48 } assert my_solution.minimumAddedCoins(**test_input) == 0 test_input = { "coins": [10,4,4,3,9,6,8,4,7,7], "target": 22 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [15,9,5,7,4,13], "target": 19 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [13,11,8,6,11], "target": 16 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [8,14,15,9,8,10,13,7,3], "target": 42 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [6,14,7,4,10,9,10,9,7], "target": 22 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [4,6,7,15,13,14,5,7], "target": 16 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [10,12,5], "target": 32 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [8,5,14,13,13,11,14,13], "target": 43 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [3,14,4,2,10,3,7], "target": 50 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [1,3,11,9,2,10,6,12], "target": 12 } assert my_solution.minimumAddedCoins(**test_input) == 0 test_input = { "coins": [2,5,4,12,6,7,11,15], "target": 17 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [7,12,10,15,6,15,14,2,9,12], "target": 24 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [4,7,15,10,14], "target": 38 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [10,1,12,9], "target": 16 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [5,8,12,6,15,13,11,5], "target": 35 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [6,2,6], "target": 39 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [15,10,5,4,7,12,12,5,11], "target": 30 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [9,10,7,12,10,4], "target": 35 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [4,4,2], "target": 8 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [13,4,15,1,8], "target": 25 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [14,7,7,1,6,14,3,15,13], "target": 18 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [8,2,14,2,3,10,15,5], "target": 31 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [3,7,12,10,11,5,3], "target": 36 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [5,3,14,8,10], "target": 33 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [9,14,9,14,4,1,4,12,12], "target": 41 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [6,3,9,7,3,4,4,15,15,10], "target": 47 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [4,9,6], "target": 43 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [12,9,13,12,10,4,9,9,4], "target": 28 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [6,8,2,13,1,5,6], "target": 31 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [9,8,9,9,3,5,10,15,1], "target": 45 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [1,10,15,15], "target": 24 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [9,12,2], "target": 43 } assert my_solution.minimumAddedCoins(**test_input) == 4 test_input = { "coins": [14,13,10,2,2], "target": 16 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [11,5,5,13,4,13,10,3,4], "target": 21 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [8,9,1,5,8,7,6,8,6], "target": 47 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [4,10,9], "target": 18 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [7,9,7,6,8,11], "target": 50 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [11,6,6,14,12,2], "target": 46 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [8,9,2], "target": 31 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [12,1,4,3,5,3], "target": 18 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [4,3,13], "target": 34 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [9,11,3], "target": 22 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [1,11,15,1,10,13,7,6,12], "target": 28 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [1,10,8,7,12], "target": 19 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [1,5,15,13,8,4,5,7], "target": 29 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [3,8,13,8,5,3,7,2,9,8], "target": 50 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [4,13,12], "target": 47 } assert my_solution.minimumAddedCoins(**test_input) == 4 test_input = { "coins": [9,3,10,9,11], "target": 48 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [8,7,6,1,9,5,5], "target": 13 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [11,9,1,15], "target": 16 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [8,13,8], "target": 27 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [7,10,11,3,10,14], "target": 36 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [14,9,6,11,13,8,8,5,6], "target": 22 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [9,3,6,10,11,1,5,14,3], "target": 27 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [4,5,9,6,2,2,10,5,13], "target": 43 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [3,10,2,13,6,13,14,14,3], "target": 28 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [14,15,15,12,13,4,15], "target": 38 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [10,5,12,11,9,8,1], "target": 12 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [15,13,12,4], "target": 31 } assert my_solution.minimumAddedCoins(**test_input) == 3 test_input = { "coins": [4,5,14,13,10,12], "target": 31 } assert my_solution.minimumAddedCoins(**test_input) == 2
1,701,570,600
weekly-contest-374-count-complete-substrings
https://leetcode.com/problems/count-complete-substrings
count-complete-substrings
{ "questionId": "3223", "questionFrontendId": "2953", "title": "Count Complete Substrings", "titleSlug": "count-complete-substrings", "isPaidOnly": false, "difficulty": "Hard", "likes": 154, "dislikes": 30, "categoryTitle": "Algorithms" }
""" 给你一个字符串word和一个整数 k。 如果word的一个子字符串 s满足以下条件,我们称它是 完全字符串: s中每个字符 恰好出现 k次。 相邻字符在字母表中的顺序 至多相差2。也就是说,s中两个相邻字符c1 和c2,它们在字母表中的位置相差至多为 2 。 请你返回 word中 完全子字符串的数目。 子字符串指的是一个字符串中一段连续 非空的字符序列。 示例 1: 输入:word = "igigee", k = 2 输出:3 解释:完全子字符串需要满足每个字符恰好出现 2 次,且相邻字符相差至多为 2 :igigee, igigee, igigee。 示例 2: 输入:word = "aaabbbccc", k = 3 输出:6 解释:完全子字符串需要满足每个字符恰好出现 3 次,且相邻字符相差至多为 2 :aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc 。 提示: 1 <= word.length <= 105 word只包含小写英文字母。 1 <= k <= word.length """ class Solution: def countCompleteSubstrings(self, word: str, k: int) -> int:
给你一个字符串word和一个整数 k。 如果word的一个子字符串 s满足以下条件,我们称它是 完全字符串: s中每个字符 恰好出现 k次。 相邻字符在字母表中的顺序 至多相差2。也就是说,s中两个相邻字符c1 和c2,它们在字母表中的位置相差至多为 2 。 请你返回 word中 完全子字符串的数目。 子字符串指的是一个字符串中一段连续 非空的字符序列。 示例 1: 输入:word = "igigee", k = 2 输出:3 解释:完全子字符串需要满足每个字符恰好出现 2 次,且相邻字符相差至多为 2 :igigee, igigee, igigee。 示例 2: 输入:word = "aaabbbccc", k = 3 输出:6 解释:完全子字符串需要满足每个字符恰好出现 3 次,且相邻字符相差至多为 2 :aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc, aaabbbccc 。 提示: 1 <= word.length <= 105 word只包含小写英文字母。 1 <= k <= word.length 请完成下面的代码来解决上述问题: ```python class Solution: def countCompleteSubstrings(self, word: str, k: int) -> int: ```
my_solution = Solution() test_input = { "word": "igigee", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "aaabbbccc", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "a", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "b", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "c", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "aa", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "ab", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "ac", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "ba", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "bb", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "bc", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "ca", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "cb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "cc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "aaa", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "aab", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "aac", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "aba", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "abb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 4 test_input = { "word": "abc", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "aca", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 5 test_input = { "word": "acb", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "acc", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "baa", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "bab", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bac", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bba", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 4 test_input = { "word": "bbb", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 2 test_input = { "word": "bbc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "bca", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bcb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 5 test_input = { "word": "bcc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "caa", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 4 test_input = { "word": "cab", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "cac", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "cba", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "cbb", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "cbc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "cca", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "ccb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 4 test_input = { "word": "ccc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 2 test_input = { "word": "aaaa", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "aaab", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "aaac", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "aaba", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "aabb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 5 test_input = { "word": "aabc", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "aaca", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "aacb", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "aacc", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "abaa", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "abab", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 7 test_input = { "word": "abac", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "abba", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "abbb", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "abbc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "abca", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 9 test_input = { "word": "abcb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 8 test_input = { "word": "abcc", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "acaa", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "acab", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "acac", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 7 test_input = { "word": "acba", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "acbb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 7 test_input = { "word": "acbc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "acca", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "accb", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "accc", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "baaa", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 2 test_input = { "word": "baab", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "baac", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "baba", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 7 test_input = { "word": "babb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "babc", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "baca", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bacb", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bacc", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 7 test_input = { "word": "bbaa", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "bbab", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bbac", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bbba", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bbbb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 4 test_input = { "word": "bbbc", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "bbca", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bbcb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "bbcc", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bcaa", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "bcab", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bcac", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bcba", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 8 test_input = { "word": "bcbb", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "bcbc", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "bcca", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bccb", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "bccc", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "caaa", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 1 test_input = { "word": "caab", "k": 1 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "caac", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "caba", "k": 4 } assert my_solution.countCompleteSubstrings(**test_input) == 0 test_input = { "word": "cabb", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 0
1,701,570,600
weekly-contest-374-count-the-number-of-infection-sequences
https://leetcode.com/problems/count-the-number-of-infection-sequences
count-the-number-of-infection-sequences
{ "questionId": "3224", "questionFrontendId": "2954", "title": "Count the Number of Infection Sequences", "titleSlug": "count-the-number-of-infection-sequences", "isPaidOnly": false, "difficulty": "Hard", "likes": 50, "dislikes": 12, "categoryTitle": "Algorithms" }
""" 给你一个整数n和一个下标从 0开始的整数数组sick,数组按 升序排序。 有n位小朋友站成一排,按顺序编号为 0到 n - 1。数组sick包含一开始得了感冒的小朋友的位置。如果位置为i的小朋友得了感冒,他会传染给下标为 i - 1或者 i + 1的小朋友,前提 是被传染的小朋友存在且还没有得感冒。每一秒中, 至多一位还没感冒的小朋友会被传染。 经过有限的秒数后,队列中所有小朋友都会感冒。感冒序列指的是 所有一开始没有感冒的小朋友最后得感冒的顺序序列。请你返回所有感冒序列的数目。 由于答案可能很大,请你将答案对109 + 7取余后返回。 注意,感冒序列 不 包含一开始就得了感冒的小朋友的下标。 示例 1: 输入:n = 5, sick = [0,4] 输出:4 解释:一开始,下标为 1 ,2 和 3 的小朋友没有感冒。总共有 4 个可能的感冒序列: - 一开始,下标为 1 和 3 的小朋友可以被传染,因为他们分别挨着有感冒的小朋友 0 和 4 ,令下标为 1 的小朋友先被传染。 然后,下标为 2 的小朋友挨着感冒的小朋友 1 ,下标为 3 的小朋友挨着感冒的小朋友 4 ,两位小朋友都可以被传染,令下标为 2 的小朋友被传染。 最后,下标为 3 的小朋友被传染,因为他挨着感冒的小朋友 2 和 4 ,感冒序列为 [1,2,3] 。 - 一开始,下标为 1 和 3 的小朋友可以被传染,因为他们分别挨着感冒的小朋友 0 和 4 ,令下标为 1 的小朋友先被传染。 然后,下标为 2 的小朋友挨着感冒的小朋友 1 ,下标为 3 的小朋友挨着感冒的小朋友 4 ,两位小朋友都可以被传染,令下标为 3 的小朋友被传染。 最后,下标为 2 的小朋友被传染,因为他挨着感冒的小朋友 1 和 3 ,感冒序列为 [1,3,2] 。 - 感冒序列 [3,1,2] ,被传染的顺序:[0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] 。 - 感冒序列 [3,2,1] ,被传染的顺序:[0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] 。 示例 2: 输入:n = 4, sick = [1] 输出:3 解释:一开始,下标为 0 ,2 和 3 的小朋友没有感冒。总共有 3 个可能的感冒序列: - 感冒序列 [0,2,3] ,被传染的顺序:[0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3] 。 - 感冒序列 [2,0,3] ,被传染的顺序:[0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3] 。 - 感冒序列 [2,3,0] ,被传染的顺序:[0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3] 。 提示: 2 <= n <= 105 1 <= sick.length <= n - 1 0 <= sick[i] <= n - 1 sick按升序排列。 """ class Solution: def numberOfSequence(self, n: int, sick: List[int]) -> int:
给你一个整数n和一个下标从 0开始的整数数组sick,数组按 升序排序。 有n位小朋友站成一排,按顺序编号为 0到 n - 1。数组sick包含一开始得了感冒的小朋友的位置。如果位置为i的小朋友得了感冒,他会传染给下标为 i - 1或者 i + 1的小朋友,前提 是被传染的小朋友存在且还没有得感冒。每一秒中, 至多一位还没感冒的小朋友会被传染。 经过有限的秒数后,队列中所有小朋友都会感冒。感冒序列指的是 所有一开始没有感冒的小朋友最后得感冒的顺序序列。请你返回所有感冒序列的数目。 由于答案可能很大,请你将答案对109 + 7取余后返回。 注意,感冒序列 不 包含一开始就得了感冒的小朋友的下标。 示例 1: 输入:n = 5, sick = [0,4] 输出:4 解释:一开始,下标为 1 ,2 和 3 的小朋友没有感冒。总共有 4 个可能的感冒序列: - 一开始,下标为 1 和 3 的小朋友可以被传染,因为他们分别挨着有感冒的小朋友 0 和 4 ,令下标为 1 的小朋友先被传染。 然后,下标为 2 的小朋友挨着感冒的小朋友 1 ,下标为 3 的小朋友挨着感冒的小朋友 4 ,两位小朋友都可以被传染,令下标为 2 的小朋友被传染。 最后,下标为 3 的小朋友被传染,因为他挨着感冒的小朋友 2 和 4 ,感冒序列为 [1,2,3] 。 - 一开始,下标为 1 和 3 的小朋友可以被传染,因为他们分别挨着感冒的小朋友 0 和 4 ,令下标为 1 的小朋友先被传染。 然后,下标为 2 的小朋友挨着感冒的小朋友 1 ,下标为 3 的小朋友挨着感冒的小朋友 4 ,两位小朋友都可以被传染,令下标为 3 的小朋友被传染。 最后,下标为 2 的小朋友被传染,因为他挨着感冒的小朋友 1 和 3 ,感冒序列为 [1,3,2] 。 - 感冒序列 [3,1,2] ,被传染的顺序:[0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] 。 - 感冒序列 [3,2,1] ,被传染的顺序:[0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] 。 示例 2: 输入:n = 4, sick = [1] 输出:3 解释:一开始,下标为 0 ,2 和 3 的小朋友没有感冒。总共有 3 个可能的感冒序列: - 感冒序列 [0,2,3] ,被传染的顺序:[0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3] 。 - 感冒序列 [2,0,3] ,被传染的顺序:[0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3] 。 - 感冒序列 [2,3,0] ,被传染的顺序:[0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3] 。 提示: 2 <= n <= 105 1 <= sick.length <= n - 1 0 <= sick[i] <= n - 1 sick按升序排列。 请完成下面的代码来解决上述问题: ```python class Solution: def numberOfSequence(self, n: int, sick: List[int]) -> int: ```
my_solution = Solution() test_input = { "n": 5, "sick": [0,4] } assert my_solution.numberOfSequence(**test_input) == 4 test_input = { "n": 4, "sick": [1] } assert my_solution.numberOfSequence(**test_input) == 3 test_input = { "n": 2, "sick": [0] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [0] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 100, "sick": [0] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 2, "sick": [1] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [1] } assert my_solution.numberOfSequence(**test_input) == 4 test_input = { "n": 5, "sick": [2] } assert my_solution.numberOfSequence(**test_input) == 6 test_input = { "n": 5, "sick": [3] } assert my_solution.numberOfSequence(**test_input) == 4 test_input = { "n": 5, "sick": [4] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [0,1] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 3, "sick": [0,2] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [0,2] } assert my_solution.numberOfSequence(**test_input) == 3 test_input = { "n": 5, "sick": [0,3] } assert my_solution.numberOfSequence(**test_input) == 6 test_input = { "n": 5, "sick": [1,2] } assert my_solution.numberOfSequence(**test_input) == 3 test_input = { "n": 5, "sick": [1,3] } assert my_solution.numberOfSequence(**test_input) == 6 test_input = { "n": 5, "sick": [1,4] } assert my_solution.numberOfSequence(**test_input) == 6 test_input = { "n": 5, "sick": [2,3] } assert my_solution.numberOfSequence(**test_input) == 3 test_input = { "n": 5, "sick": [2,4] } assert my_solution.numberOfSequence(**test_input) == 3 test_input = { "n": 5, "sick": [3,4] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [0,1,2] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [0,1,3] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [0,1,4] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [0,2,3] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [0,2,4] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [0,3,4] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [1,2,3] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [1,2,4] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [1,3,4] } assert my_solution.numberOfSequence(**test_input) == 2 test_input = { "n": 5, "sick": [2,3,4] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [0,1,2,3] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 10, "sick": [0,1,2,3] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 5, "sick": [0,1,2,4] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 10, "sick": [0,1,2,4] } assert my_solution.numberOfSequence(**test_input) == 6 test_input = { "n": 10, "sick": [0,1,2,5] } assert my_solution.numberOfSequence(**test_input) == 30 test_input = { "n": 10, "sick": [0,1,2,8] } assert my_solution.numberOfSequence(**test_input) == 96 test_input = { "n": 10, "sick": [0,1,2,9] } assert my_solution.numberOfSequence(**test_input) == 32 test_input = { "n": 5, "sick": [0,1,3,4] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 10, "sick": [0,1,3,5] } assert my_solution.numberOfSequence(**test_input) == 30 test_input = { "n": 10, "sick": [0,1,3,6] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,1,3,7] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,1,3,8] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,1,4,5] } assert my_solution.numberOfSequence(**test_input) == 30 test_input = { "n": 10, "sick": [0,1,4,6] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,1,4,7] } assert my_solution.numberOfSequence(**test_input) == 360 test_input = { "n": 10, "sick": [0,1,4,8] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,1,4,9] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,1,5,6] } assert my_solution.numberOfSequence(**test_input) == 80 test_input = { "n": 10, "sick": [0,1,5,7] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,1,5,8] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,1,5,9] } assert my_solution.numberOfSequence(**test_input) == 320 test_input = { "n": 10, "sick": [0,1,6,7] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,1,6,8] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,1,6,9] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,1,7,8] } assert my_solution.numberOfSequence(**test_input) == 96 test_input = { "n": 10, "sick": [0,1,7,9] } assert my_solution.numberOfSequence(**test_input) == 96 test_input = { "n": 10, "sick": [0,1,8,9] } assert my_solution.numberOfSequence(**test_input) == 32 test_input = { "n": 5, "sick": [0,2,3,4] } assert my_solution.numberOfSequence(**test_input) == 1 test_input = { "n": 10, "sick": [0,2,3,4] } assert my_solution.numberOfSequence(**test_input) == 6 test_input = { "n": 10, "sick": [0,2,3,5] } assert my_solution.numberOfSequence(**test_input) == 30 test_input = { "n": 10, "sick": [0,2,3,6] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,2,3,7] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,2,3,8] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,2,3,9] } assert my_solution.numberOfSequence(**test_input) == 96 test_input = { "n": 10, "sick": [0,2,4,5] } assert my_solution.numberOfSequence(**test_input) == 30 test_input = { "n": 10, "sick": [0,2,4,6] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,2,4,7] } assert my_solution.numberOfSequence(**test_input) == 360 test_input = { "n": 10, "sick": [0,2,4,8] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,2,4,9] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,2,5,6] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,2,5,7] } assert my_solution.numberOfSequence(**test_input) == 360 test_input = { "n": 10, "sick": [0,2,5,8] } assert my_solution.numberOfSequence(**test_input) == 720 test_input = { "n": 10, "sick": [0,2,5,9] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,2,6,7] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,2,6,8] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,2,7,8] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,2,7,9] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,2,8,9] } assert my_solution.numberOfSequence(**test_input) == 96 test_input = { "n": 10, "sick": [0,3,4,5] } assert my_solution.numberOfSequence(**test_input) == 30 test_input = { "n": 10, "sick": [0,3,4,7] } assert my_solution.numberOfSequence(**test_input) == 360 test_input = { "n": 10, "sick": [0,3,4,8] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,3,4,9] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,3,5,6] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,3,5,7] } assert my_solution.numberOfSequence(**test_input) == 360 test_input = { "n": 10, "sick": [0,3,6,7] } assert my_solution.numberOfSequence(**test_input) == 360 test_input = { "n": 10, "sick": [0,3,6,8] } assert my_solution.numberOfSequence(**test_input) == 720 test_input = { "n": 10, "sick": [0,3,6,9] } assert my_solution.numberOfSequence(**test_input) == 720 test_input = { "n": 10, "sick": [0,3,7,8] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,3,7,9] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,3,8,9] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,4,5,6] } assert my_solution.numberOfSequence(**test_input) == 80 test_input = { "n": 10, "sick": [0,4,5,7] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,4,5,9] } assert my_solution.numberOfSequence(**test_input) == 320 test_input = { "n": 10, "sick": [0,4,6,7] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,4,6,8] } assert my_solution.numberOfSequence(**test_input) == 480 test_input = { "n": 10, "sick": [0,4,8,9] } assert my_solution.numberOfSequence(**test_input) == 320 test_input = { "n": 10, "sick": [0,5,6,7] } assert my_solution.numberOfSequence(**test_input) == 120 test_input = { "n": 10, "sick": [0,5,6,9] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,5,7,8] } assert my_solution.numberOfSequence(**test_input) == 240 test_input = { "n": 10, "sick": [0,5,7,9] } assert my_solution.numberOfSequence(**test_input) == 240
1,701,570,600
weekly-contest-373-matrix-similarity-after-cyclic-shifts
https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts
matrix-similarity-after-cyclic-shifts
{ "questionId": "3215", "questionFrontendId": "2946", "title": "Matrix Similarity After Cyclic Shifts", "titleSlug": "matrix-similarity-after-cyclic-shifts", "isPaidOnly": false, "difficulty": "Easy", "likes": 72, "dislikes": 48, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始且大小为 m x n 的整数矩阵 mat 和一个整数 k 。请你将矩阵中的 奇数 行循环 右 移 k 次,偶数 行循环 左 移 k 次。 如果初始矩阵和最终矩阵完全相同,则返回 true ,否则返回 false 。 示例 1: 输入:mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2 输出:true 解释: 初始矩阵如图一所示。 图二表示对奇数行右移一次且对偶数行左移一次后的矩阵状态。 图三是经过两次循环移位后的最终矩阵状态,与初始矩阵相同。 因此,返回 true 。 示例 2: 输入:mat = [[2,2],[2,2]], k = 3 输出:true 解释:由于矩阵中的所有值都相等,即使进行循环移位,矩阵仍然保持不变。因此,返回 true 。 示例 3: 输入:mat = [[1,2]], k = 1 输出:false 解释:循环移位一次后,mat = [[2,1]],与初始矩阵不相等。因此,返回 false 。 提示: 1 <= mat.length <= 25 1 <= mat[i].length <= 25 1 <= mat[i][j] <= 25 1 <= k <= 50 """ class Solution: def areSimilar(self, mat: List[List[int]], k: int) -> bool:
给你一个下标从 0 开始且大小为 m x n 的整数矩阵 mat 和一个整数 k 。请你将矩阵中的 奇数 行循环 右 移 k 次,偶数 行循环 左 移 k 次。 如果初始矩阵和最终矩阵完全相同,则返回 true ,否则返回 false 。 示例 1: 输入:mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2 输出:true 解释: 初始矩阵如图一所示。 图二表示对奇数行右移一次且对偶数行左移一次后的矩阵状态。 图三是经过两次循环移位后的最终矩阵状态,与初始矩阵相同。 因此,返回 true 。 示例 2: 输入:mat = [[2,2],[2,2]], k = 3 输出:true 解释:由于矩阵中的所有值都相等,即使进行循环移位,矩阵仍然保持不变。因此,返回 true 。 示例 3: 输入:mat = [[1,2]], k = 1 输出:false 解释:循环移位一次后,mat = [[2,1]],与初始矩阵不相等。因此,返回 false 。 提示: 1 <= mat.length <= 25 1 <= mat[i].length <= 25 1 <= mat[i][j] <= 25 1 <= k <= 50 请完成下面的代码来解决上述问题: ```python class Solution: def areSimilar(self, mat: List[List[int]], k: int) -> bool: ```
my_solution = Solution() test_input = { "mat": [[1,2]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[1,2,1,2],[5,5,5,5],[6,3,6,3]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4,9,10,10],[9,3,8,4],[2,5,3,8],[6,1,10,4]], "k": 5 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[5,8,8,4,7,2,3,4,3,10],[8,7,9,1,3,4,2,6,6,9],[6,2,10,10,4,6,3,4,1,1]], "k": 3 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[4,7,9,1,10,5,2,6,1,7],[8,9,9,2,3,2,3,2,3,5],[1,2,4,7,4,7,9,7,9,9]], "k": 5 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[10,6,3,6],[4,8,1,2]], "k": 6 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[7,10,6,7,7,4,4,7,2,2],[3,6,4,8,4,6,4,3,1,4],[4,8,7,1,10,2,10,8,10,1],[4,7,10,5,1,9,8,3,5,8],[3,7,6,5,3,1,3,2,8,5],[6,1,5,10,8,7,7,10,1,3]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[6,5,3],[4,6,2],[4,1,8],[3,9,1],[6,1,2],[1,9,9],[2,6,10]], "k": 5 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,4],[9,8]], "k": 9 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,2],[2,2]], "k": 3 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[9,1,10,6,10,7,3],[9,2,9,10,7,10,10]], "k": 4 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[7,7],[10,10],[4,4]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[6,6,6,6,6,6,6,6,6,6],[9,9,9,9,9,9,9,9,9,9],[1,1,1,1,1,1,1,1,1,1],[10,10,10,10,10,10,10,10,10,10],[2,2,2,2,2,2,2,2,2,2],[6,6,6,6,6,6,6,6,6,6],[7,7,7,7,7,7,7,7,7,7],[9,9,9,9,9,9,9,9,9,9],[8,8,8,8,8,8,8,8,8,8]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[6,9,1],[8,9,7],[2,8,7],[1,5,7],[10,5,9],[5,5,6],[8,6,1],[5,7,8]], "k": 5 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[3,10,3,10,3,10,3,10],[5,8,5,8,5,8,5,8],[3,9,3,9,3,9,3,9],[3,8,3,8,3,8,3,8],[2,3,2,3,2,3,2,3]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[9,5,3,10],[4,7,10,7],[1,7,9,4],[8,8,1,6],[6,7,6,1],[3,1,1,8],[9,2,8,3],[1,9,7,6]], "k": 4 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4,6],[10,1],[8,8],[10,9],[9,10]], "k": 9 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[1,9,6,7,1,4,7,6,7],[7,10,6,6,4,9,6,8,2],[3,9,8,10,9,9,3,9,5],[8,5,2,3,4,7,3,3,1],[1,5,9,9,6,1,9,7,5],[8,3,10,2,4,8,7,9,9],[5,9,6,8,4,3,4,6,4],[7,2,6,9,2,4,5,4,9],[4,8,7,5,3,6,3,9,5]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[9,3,3,7,7,5,3,3],[10,9,9,3,6,8,7,5],[8,9,3,10,10,10,2,1],[9,7,8,2,3,4,8,4],[5,9,5,2,2,6,5,7],[1,5,9,7,8,1,1,1]], "k": 10 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[10,6,10,6,10,6,10,6]], "k": 4 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[2,4],[6,1],[1,2],[2,10],[6,5],[4,9]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[8,8,8,8,8,8,8,8,8,8],[7,7,7,7,7,7,7,7,7,7],[6,6,6,6,6,6,6,6,6,6]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[8,10,1,7,1,3,9,6,8],[9,10,4,8,8,9,3,10,10],[4,3,2,2,3,6,4,6,1],[9,4,1,4,5,2,5,1,8],[3,10,6,3,8,4,8,3,10]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[8,9],[3,3],[5,6],[10,1],[2,5],[5,8],[5,4],[9,5]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[9,1,8,9,2,9,1,8,9,2],[10,2,7,8,9,10,2,7,8,9],[7,6,6,9,5,7,6,6,9,5]], "k": 5 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4,4,4,2,7,9,1,8,9,8],[3,3,6,3,8,8,7,7,4,5],[10,1,3,7,6,5,7,10,3,10]], "k": 5 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[9,10,10,6,6,8,10,7,10,9],[10,6,1,10,10,5,7,9,9,2],[8,5,8,3,5,2,2,9,7,10]], "k": 20 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4,5,1,3,10],[10,5,9,10,2],[8,10,2,8,1],[5,8,9,3,4],[6,6,10,10,10],[6,1,7,9,4],[6,7,6,2,10]], "k": 8 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,7,1,10,5,3],[10,7,8,2,2,2],[9,6,1,4,10,6],[6,1,1,9,2,5],[6,4,7,3,6,4],[10,10,5,4,2,1],[7,3,3,7,1,5],[5,8,2,10,5,1],[3,1,5,1,5,7]], "k": 2 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[7,7,4],[8,9,9],[9,7,5],[6,3,6],[4,9,5],[1,10,3],[4,4,7],[4,7,6]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[10,10],[10,10],[5,5],[3,3],[2,2]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[6,4,7,6,3,9,4,2,10,5],[9,7,7,3,10,9,7,4,3,1]], "k": 20 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[7]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[6,3,2]], "k": 4 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[6,8]], "k": 4 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[6,6,7,7,1],[10,3,3,2,2],[7,9,8,10,7],[10,8,2,7,1],[2,2,1,2,3],[6,2,8,10,10],[6,2,6,3,3],[2,2,2,4,7]], "k": 4 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[8,8,5,3,7,8],[8,9,1,7,3,10],[4,3,9,8,4,7],[2,2,5,8,2,2],[6,1,2,7,4,8],[10,9,6,3,1,4],[7,1,6,7,4,6]], "k": 2 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[7],[5],[5],[4],[4],[5],[8]], "k": 6 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[8,8,5,10,7,8,8,5,10,7],[1,2,6,10,7,1,2,6,10,7]], "k": 5 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[10,2,6,7,6,6,5],[6,3,3,4,6,5,7],[6,8,5,10,8,4,1]], "k": 8 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[4,10,9,7,9,9,2],[3,9,2,1,8,9,10],[7,10,9,7,2,3,8]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[1,7,10,10,9,2,1],[6,4,5,2,3,3,10],[2,6,8,3,6,1,4]], "k": 9 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,9,2,2,6,10,4,8,3],[10,8,4,5,10,3,3,8,5],[2,6,4,5,4,8,5,5,4],[1,3,2,10,5,3,10,9,4],[2,4,2,4,7,7,1,4,9]], "k": 2 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[9,5,6,9,5,6],[1,9,4,1,9,4],[5,7,2,5,7,2],[9,1,5,9,1,5],[6,8,6,6,8,6],[10,1,7,10,1,7]], "k": 6 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[2,7,6],[10,6,5],[10,2,4],[10,7,9],[5,8,6],[10,6,3],[10,9,6],[5,2,8],[10,1,2]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[5,4,5,10,5]], "k": 9 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[10,10,9],[5,6,7],[1,4,7],[5,1,1],[5,1,5],[5,10,3]], "k": 2 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[9,4,5],[8,5,4],[2,9,9]], "k": 10 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[4,2,2,7,9,1,1,2],[1,8,7,5,7,5,9,6],[2,9,4,10,1,8,5,4]], "k": 3 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[10],[7],[8],[2]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[10],[1],[5],[3],[1],[1]], "k": 4 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[7,1,7,7,1,7,7,1,7],[5,10,1,5,10,1,5,10,1]], "k": 3 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4,7,9,9,4,7,9,9],[8,9,7,4,8,9,7,4],[6,8,6,4,6,8,6,4],[9,8,8,8,9,8,8,8],[3,6,5,3,3,6,5,3],[1,9,4,3,1,9,4,3],[8,3,2,7,8,3,2,7],[3,8,2,8,3,8,2,8],[6,5,2,8,6,5,2,8]], "k": 8 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4],[5],[4],[2],[4],[2],[7],[4]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[3,8,5,4,10,2],[9,3,9,5,4,2]], "k": 6 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[8,9,10,6,5,7],[8,9,9,3,3,9],[4,5,4,4,4,10],[2,6,3,9,7,1],[10,10,4,4,6,10]], "k": 9 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[7,7,7,7,7],[1,1,1,1,1]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[5,7,5,5,1,9,1,8,6,7],[8,1,9,10,10,5,4,9,1,8],[10,6,8,10,2,10,9,4,9,6],[4,7,10,2,7,4,2,10,3,5],[2,2,4,9,10,1,6,2,8,3],[1,3,5,9,9,8,10,8,9,10],[7,8,7,7,6,9,2,5,8,4],[6,9,4,2,4,10,10,8,10,7]], "k": 8 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,9,10],[7,3,3],[7,6,2]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,5,8,9,6,8],[3,6,4,10,10,6],[9,6,10,9,6,5]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,2],[4,5],[3,2],[4,6],[1,9],[5,3],[3,5],[2,4],[3,9]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[1,8,6,8,6,7,1,6]], "k": 16 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[7,9,9,2,7],[8,5,8,6,7],[2,9,8,5,2],[9,9,2,6,8],[7,4,10,10,8]], "k": 6 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[8,8],[6,6],[2,2],[8,8],[9,9],[8,8],[10,10],[3,3],[4,4],[5,5]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[3,3,3,3,3,3],[5,3,5,3,5,3],[2,5,2,5,2,5],[8,8,8,8,8,8],[3,8,3,8,3,8],[5,3,5,3,5,3],[1,8,1,8,1,8],[8,9,8,9,8,9],[2,8,2,8,2,8]], "k": 4 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[2,2,2,2,2],[7,7,7,7,7],[5,5,5,5,5],[8,8,8,8,8],[1,1,1,1,1],[10,10,10,10,10],[7,7,7,7,7]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[3,1,10,5,10,3,1,10,5,10],[3,5,9,2,10,3,5,9,2,10],[4,6,3,5,7,4,6,3,5,7],[8,10,6,7,8,8,10,6,7,8]], "k": 5 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[10,7,1,7],[3,5,9,5],[2,8,3,10],[8,7,1,9],[3,8,6,3],[6,5,8,9],[8,7,5,1],[10,4,9,9],[4,6,1,9],[6,10,1,7]], "k": 3 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[1,10,3,9,6],[7,1,3,4,10]], "k": 3 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[7,7],[2,2],[5,5]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4,4,4,4,4,4,4],[2,2,2,2,2,2,2],[3,3,3,3,3,3,3],[8,8,8,8,8,8,8],[6,6,6,6,6,6,6]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[10,3,5,3,10,3,5,3],[2,3,9,7,2,3,9,7],[10,4,4,8,10,4,4,8],[10,2,7,9,10,2,7,9],[8,1,8,3,8,1,8,3],[1,9,1,7,1,9,1,7]], "k": 4 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[6],[7],[1]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[7,6,4,5]], "k": 5 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[5,5,5,5],[5,5,5,5],[10,10,10,10],[2,2,2,2],[3,3,3,3],[2,2,2,2],[8,8,8,8],[10,10,10,10],[9,9,9,9],[7,7,7,7]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[5,1,1,9,4,1,7,6],[8,7,7,6,2,2,1,5],[10,2,5,3,10,7,7,5],[10,6,1,6,8,4,6,3],[10,10,9,8,2,10,8,7],[7,4,2,10,2,3,8,7],[4,7,5,9,10,4,3,2],[10,9,7,7,6,3,9,7],[1,4,8,4,6,5,5,1]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[3]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[1,1,1,1,1],[10,10,10,10,10],[10,10,10,10,10]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[6],[3],[2],[10]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[9,7,5,6],[5,2,1,8],[9,4,3,6],[5,7,4,1],[8,1,8,9],[4,3,6,5],[6,2,7,3],[1,3,6,4],[4,9,5,5]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[10,7,2,10,5,2,7],[10,10,3,8,3,3,8],[4,3,10,10,10,4,10]], "k": 4 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[8,5],[8,10],[8,10],[1,1],[2,1]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[9,9],[8,8],[2,2],[1,1],[8,8],[4,4],[9,9],[4,4],[6,6]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[10,1,1],[7,10,6],[9,6,6],[9,8,10],[8,2,1],[6,8,3],[8,6,6]], "k": 5 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[2,10,5,6,5,5],[6,3,1,5,4,7],[5,6,3,2,4,10],[9,2,6,8,6,2],[3,6,8,4,9,1]], "k": 8 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[10,3,4,2,8,10,3,4,2,8],[9,9,3,4,5,9,9,3,4,5],[6,9,9,2,7,6,9,9,2,7],[5,2,3,3,4,5,2,3,3,4]], "k": 5 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[3,4,10,3,4,10],[5,5,4,5,5,4],[5,5,3,5,5,3],[7,8,7,7,8,7]], "k": 3 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[7,1,9,3,6],[5,6,5,5,6],[2,3,5,10,8],[5,10,2,5,4],[7,9,1,7,10],[8,2,3,4,2],[1,6,9,2,1]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[3,3],[3,3],[4,4],[3,3],[8,8],[5,5]], "k": 1 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[2,10,2,6,3,6],[4,5,10,7,7,9],[1,7,4,1,9,4],[3,7,6,3,1,4],[4,10,4,6,3,5],[1,5,5,9,5,1],[10,2,5,4,7,10],[2,9,7,4,5,3],[5,5,1,2,8,3]], "k": 2 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[5],[5],[5]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[9,5,5,6,7],[7,9,3,8,1],[8,8,8,9,5],[1,3,2,6,9],[3,6,4,8,7],[9,3,3,9,10],[8,5,1,2,8],[7,3,10,5,1],[8,4,5,5,1]], "k": 5 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[5,8,5,2,8,5,9],[7,8,2,2,8,2,2],[4,5,6,7,3,9,9],[5,7,4,8,2,9,2],[9,5,3,3,5,7,3],[3,8,9,6,3,10,7],[6,7,3,7,3,6,6]], "k": 8 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[8,8,8,8,8,8],[8,8,8,8,8,8],[2,2,2,2,2,2],[6,6,6,6,6,6],[9,9,9,9,9,9],[10,10,10,10,10,10],[10,10,10,10,10,10]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[7,7,10,2],[3,5,7,6],[2,10,1,8],[8,3,1,10],[5,1,3,3],[6,3,4,9],[8,9,1,1]], "k": 7 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[5,2,7,2,6,10,7,5],[10,9,4,1,7,2,7,4],[2,6,7,3,2,10,4,5],[10,4,7,2,10,3,6,2]], "k": 16 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[9,10,10,1],[1,7,3,5],[9,6,4,7],[6,6,4,5],[2,4,2,7],[2,1,1,1],[7,2,1,8],[2,8,1,3],[7,4,6,1],[10,10,7,5]], "k": 4 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[7,3,10,2,3,1,10],[7,6,10,1,3,2,1],[9,1,5,7,1,8,3],[4,10,10,7,7,9,7],[7,9,1,5,3,8,4],[4,9,5,10,2,8,10],[2,5,10,3,6,2,9],[6,7,2,3,4,2,2]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[8,8],[9,9],[2,2],[10,10],[10,10],[1,1],[5,5],[9,9],[7,7]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[2,1,7,3,7,6,7,9,9,3],[3,9,10,4,4,6,8,10,5,6],[9,8,6,2,3,4,9,1,9,10],[7,10,8,8,3,9,9,5,8,9],[9,5,6,9,9,6,4,3,2,3],[3,10,6,2,7,6,10,6,2,6],[7,9,7,4,5,7,2,4,9,5],[4,7,9,6,7,4,6,4,10,4]], "k": 6 } assert my_solution.areSimilar(**test_input) == False
1,700,965,800
weekly-contest-373-count-beautiful-substrings-i
https://leetcode.com/problems/count-beautiful-substrings-i
count-beautiful-substrings-i
{ "questionId": "3210", "questionFrontendId": "2947", "title": "Count Beautiful Substrings I", "titleSlug": "count-beautiful-substrings-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 102, "dislikes": 8, "categoryTitle": "Algorithms" }
""" 给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh", k = 2 输出:2 解释:字符串 s 中有 2 个美丽子字符串。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["y","h"])。 可以看出字符串 "aeyh" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["b","y"])。 可以看出字符串 "baey" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 可以证明字符串 s 中只有 2 个美丽子字符串。 示例 2: 输入:s = "abba", k = 1 输出:3 解释:字符串 s 中有 3 个美丽子字符串。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 2(["a","a"]),consonants = 2(["b","b"])。 可以证明字符串 s 中只有 3 个美丽子字符串。 示例 3: 输入:s = "bcdf", k = 1 输出:0 解释:字符串 s 中没有美丽子字符串。 提示: 1 <= s.length <= 1000 1 <= k <= 1000 s 仅由小写英文字母组成。 """ class Solution: def beautifulSubstrings(self, s: str, k: int) -> int:
给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh", k = 2 输出:2 解释:字符串 s 中有 2 个美丽子字符串。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["y","h"])。 可以看出字符串 "aeyh" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["b","y"])。 可以看出字符串 "baey" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 可以证明字符串 s 中只有 2 个美丽子字符串。 示例 2: 输入:s = "abba", k = 1 输出:3 解释:字符串 s 中有 3 个美丽子字符串。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 2(["a","a"]),consonants = 2(["b","b"])。 可以证明字符串 s 中只有 3 个美丽子字符串。 示例 3: 输入:s = "bcdf", k = 1 输出:0 解释:字符串 s 中没有美丽子字符串。 提示: 1 <= s.length <= 1000 1 <= k <= 1000 s 仅由小写英文字母组成。 请完成下面的代码来解决上述问题: ```python class Solution: def beautifulSubstrings(self, s: str, k: int) -> int: ```
my_solution = Solution() test_input = { "s": "baeyh", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "abba", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "bcdf", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ihroyeeb", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "uzuxpzou", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "ouuoeqd", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "eeebjoxxujuaeoqibd", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 4 test_input = { "s": "ilougekqlovegioemdvu", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 21 test_input = { "s": "tqaewreikaztwpfwnef", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "oykiuhsafgfjumnzb", "k": 7 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ifvsa", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "svzauyuevujektj", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "urahjig", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "ime", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "oacghieut", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "aoluu", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ioaoiciiuoziout", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "ouafupsuhid", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ox", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "tlaiwoauazutusiaaui", "k": 10 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "caepeym", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "apyxvceue", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "imkqbb", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "caaz", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "pyicoy", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "uopmyrsntjhiroikup", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "aujfxqxcj", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "eeizejuoxeumz", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "uuouuaifnboeiulttio", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 10 test_input = { "s": "woozzxd", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "pulorolqcvhafexui", "k": 9 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "hmuaewojioizoguvoaje", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 4 test_input = { "s": "b", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "aiejouohnqnketinvat", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "mjiogpri", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "movbyaeouil", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "puureouausxmitvav", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "op", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "iuhoezpooxcohtlapolo", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 39 test_input = { "s": "cioi", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "pueutaoyaxk", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "iiuresacruaaan", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "agntyaazvpejidwaph", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "wiybolyniexiibou", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "coiyakadxi", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 8 test_input = { "s": "oraajoeruiakixj", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "jeayap", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 7 test_input = { "s": "iu", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "awozoy", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "fheabmlsyeeeuoeogyz", "k": 9 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "eaizneuxi", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 10 test_input = { "s": "uurqufaucsuoqljh", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 14 test_input = { "s": "jrtept", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "olgioxooiejooosaed", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "uizoy", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "lswabfiujjhexzos", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "iuu", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qeaxut", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 7 test_input = { "s": "aojiau", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "oaiaaaargkonlcsoaygf", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "zoowrawkm", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "uqiwuoevkfhkkua", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "kavuaaeodvaxicm", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qpxeceq", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "iaabaofuodcbek", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "eel", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "ikeuhe", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "lueikvo", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "oauau", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qzoieeotieeakqraeao", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 4 test_input = { "s": "ehaascocsdmgekni", "k": 9 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "euqeklniykiji", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "vaeiiioidiioxhduu", "k": 7 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "aa", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "chaua", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "edfrglfr", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "dqbe", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "ghooirorxge", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "fodartekaonq", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 9 test_input = { "s": "feeanzkjpfehzeuni", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ignoouesduu", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "yif", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "gondfjaeeuhbuuasgip", "k": 10 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "djooomsffoonelyeode", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 14 test_input = { "s": "pgaimei", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "naipqentonee", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 8 test_input = { "s": "bouov", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "lcuhoypz", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "g", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qc", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "mhznea", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "uxvjixdujgyfauo", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "iyjkuox", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "xbjfoayfpafatnuyord", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "nvoede", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "usnuaxpaktrweatruu", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "euojmsora", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 10 test_input = { "s": "iapgoi", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "uafuimcpxyeoixgbyeio", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "weuaatpu", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0
1,700,965,800
weekly-contest-373-make-lexicographically-smallest-array-by-swapping-elements
https://leetcode.com/problems/make-lexicographically-smallest-array-by-swapping-elements
make-lexicographically-smallest-array-by-swapping-elements
{ "questionId": "3219", "questionFrontendId": "2948", "title": "Make Lexicographically Smallest Array by Swapping Elements", "titleSlug": "make-lexicographically-smallest-array-by-swapping-elements", "isPaidOnly": false, "difficulty": "Medium", "likes": 197, "dislikes": 10, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的 正整数 数组 nums 和一个 正整数 limit 。 在一次操作中,你可以选择任意两个下标 i 和 j,如果 满足 |nums[i] - nums[j]| <= limit ,则交换 nums[i] 和 nums[j] 。 返回执行任意次操作后能得到的 字典序最小的数组 。 如果在数组 a 和数组 b 第一个不同的位置上,数组 a 中的对应元素比数组 b 中的对应元素的字典序更小,则认为数组 a 就比数组 b 字典序更小。例如,数组 [2,10,3] 比数组 [10,2,3] 字典序更小,下标 0 处是两个数组第一个不同的位置,且 2 < 10 。 示例 1: 输入:nums = [1,5,3,9,8], limit = 2 输出:[1,3,5,8,9] 解释:执行 2 次操作: - 交换 nums[1] 和 nums[2] 。数组变为 [1,3,5,9,8] 。 - 交换 nums[3] 和 nums[4] 。数组变为 [1,3,5,8,9] 。 即便执行更多次操作,也无法得到字典序更小的数组。 注意,执行不同的操作也可能会得到相同的结果。 示例 2: 输入:nums = [1,7,6,18,2,1], limit = 3 输出:[1,6,7,18,1,2] 解释:执行 3 次操作: - 交换 nums[1] 和 nums[2] 。数组变为 [1,6,7,18,2,1] 。 - 交换 nums[0] 和 nums[4] 。数组变为 [2,6,7,18,1,1] 。 - 交换 nums[0] 和 nums[5] 。数组变为 [1,6,7,18,1,2] 。 即便执行更多次操作,也无法得到字典序更小的数组。 示例 3: 输入:nums = [1,7,28,19,10], limit = 3 输出:[1,7,28,19,10] 解释:[1,7,28,19,10] 是字典序最小的数组,因为不管怎么选择下标都无法执行操作。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 1 <= limit <= 109 """ class Solution: def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]:
给你一个下标从 0 开始的 正整数 数组 nums 和一个 正整数 limit 。 在一次操作中,你可以选择任意两个下标 i 和 j,如果 满足 |nums[i] - nums[j]| <= limit ,则交换 nums[i] 和 nums[j] 。 返回执行任意次操作后能得到的 字典序最小的数组 。 如果在数组 a 和数组 b 第一个不同的位置上,数组 a 中的对应元素比数组 b 中的对应元素的字典序更小,则认为数组 a 就比数组 b 字典序更小。例如,数组 [2,10,3] 比数组 [10,2,3] 字典序更小,下标 0 处是两个数组第一个不同的位置,且 2 < 10 。 示例 1: 输入:nums = [1,5,3,9,8], limit = 2 输出:[1,3,5,8,9] 解释:执行 2 次操作: - 交换 nums[1] 和 nums[2] 。数组变为 [1,3,5,9,8] 。 - 交换 nums[3] 和 nums[4] 。数组变为 [1,3,5,8,9] 。 即便执行更多次操作,也无法得到字典序更小的数组。 注意,执行不同的操作也可能会得到相同的结果。 示例 2: 输入:nums = [1,7,6,18,2,1], limit = 3 输出:[1,6,7,18,1,2] 解释:执行 3 次操作: - 交换 nums[1] 和 nums[2] 。数组变为 [1,6,7,18,2,1] 。 - 交换 nums[0] 和 nums[4] 。数组变为 [2,6,7,18,1,1] 。 - 交换 nums[0] 和 nums[5] 。数组变为 [1,6,7,18,1,2] 。 即便执行更多次操作,也无法得到字典序更小的数组。 示例 3: 输入:nums = [1,7,28,19,10], limit = 3 输出:[1,7,28,19,10] 解释:[1,7,28,19,10] 是字典序最小的数组,因为不管怎么选择下标都无法执行操作。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 109 1 <= limit <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]: ```
my_solution = Solution() test_input = { "nums": [1,5,3,9,8], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,3,5,8,9] test_input = { "nums": [1,7,6,18,2,1], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,6,7,18,1,2] test_input = { "nums": [1,7,28,19,10], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,7,28,19,10] test_input = { "nums": [1000000000], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1000000000] test_input = { "nums": [1,60,34,84,62,56,39,76,49,38], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,56,34,84,60,62,38,76,49,39] test_input = { "nums": [1,81,10,79,36,2,87,12,20,77], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,77,10,79,36,2,81,12,20,87] test_input = { "nums": [2,71,5,87,11,15,70,70,14,38], "limit": 14 } assert my_solution.lexicographicallySmallestArray(**test_input) == [2,70,5,87,11,14,70,71,15,38] test_input = { "nums": [4,3,23,84,34,88,44,44,18,15], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [3,4,23,84,34,88,44,44,15,18] test_input = { "nums": [4,34,29,73,51,11,8,53,98,47], "limit": 10 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,29,34,73,47,8,11,51,98,53] test_input = { "nums": [4,52,38,59,71,27,31,83,88,10], "limit": 14 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,27,31,38,52,59,71,83,88,10] test_input = { "nums": [4,68,8,10,70,62,27,5,42,61], "limit": 11 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,61,5,8,62,68,27,10,42,70] test_input = { "nums": [5,9,35,60,73,91,61,57,87,76], "limit": 11 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,9,35,57,73,76,60,61,87,91] test_input = { "nums": [5,15,68,47,49,67,9,6,35,14], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,14,67,47,49,68,6,9,35,15] test_input = { "nums": [5,16,43,15,66,21,58,74,55,66], "limit": 9 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,15,43,16,55,21,58,66,66,74] test_input = { "nums": [5,30,92,4,31,2,17,39,15,7], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [2,30,92,4,31,5,15,39,17,7] test_input = { "nums": [5,38,68,80,64,79,50,5,8,95], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,38,64,79,68,80,50,5,8,95] test_input = { "nums": [5,100,44,45,16,30,14,65,83,64], "limit": 15 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,100,14,16,30,44,45,64,83,65] test_input = { "nums": [6,57,100,67,4,63,47,59,21,66], "limit": 8 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,57,100,59,6,63,47,66,21,67] test_input = { "nums": [6,70,90,1,33,81,60,80,68,44], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,68,90,6,33,80,60,81,70,44] test_input = { "nums": [6,74,74,74,30,70,91,74,76,41], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [6,74,74,74,30,70,91,74,76,41] test_input = { "nums": [6,77,68,15,3,98,56,22,81,72], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [6,77,68,15,3,98,56,22,81,72] test_input = { "nums": [7,17,79,29,29,83,21,12,5,1], "limit": 10 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,5,79,7,12,83,17,21,29,29] test_input = { "nums": [7,66,85,9,29,1,25,69,57,95], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,57,85,7,25,9,29,66,69,95] test_input = { "nums": [7,73,1,97,13,55,74,29,76,19], "limit": 14 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,73,7,97,13,55,74,19,76,29] test_input = { "nums": [8,4,47,23,73,79,63,62,35,51], "limit": 11 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,8,47,23,51,62,63,73,35,79] test_input = { "nums": [8,17,20,100,59,98,64,78,64,53], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [8,17,20,100,59,98,64,78,64,53] test_input = { "nums": [8,70,99,5,49,27,79,2,57,49], "limit": 14 } assert my_solution.lexicographicallySmallestArray(**test_input) == [2,49,99,5,49,27,57,8,70,79] test_input = { "nums": [9,67,94,37,5,90,43,13,27,21], "limit": 11 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,67,90,9,13,94,21,27,37,43] test_input = { "nums": [10,22,17,76,6,64,51,60,65,37], "limit": 9 } assert my_solution.lexicographicallySmallestArray(**test_input) == [6,10,17,76,22,51,60,64,65,37] test_input = { "nums": [10,34,63,88,76,30,70,80,52,13], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [10,30,63,88,70,34,76,80,52,13] test_input = { "nums": [10,69,4,28,15,30,23,53,41,93], "limit": 9 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,69,10,15,23,28,30,53,41,93] test_input = { "nums": [12,86,98,73,64,77,30,76,46,69], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [12,86,98,69,64,73,30,76,46,77] test_input = { "nums": [13,43,32,15,45,69,58,89,64,76], "limit": 12 } assert my_solution.lexicographicallySmallestArray(**test_input) == [13,32,43,15,45,58,64,89,69,76] test_input = { "nums": [13,70,11,74,73,21,4,45,95,38], "limit": 9 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,70,11,73,74,13,21,38,95,45] test_input = { "nums": [14,15,53,11,38,18,27,69,55,2], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [2,11,53,14,15,18,27,69,55,38] test_input = { "nums": [14,28,61,49,10,25,80,83,42,100], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [14,25,61,49,10,28,80,83,42,100] test_input = { "nums": [14,71,7,77,99,90,20,81,100,65], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [7,65,14,71,99,90,20,77,100,81] test_input = { "nums": [14,95,75,100,33,98,88,2,74,26], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [14,95,74,100,33,98,88,2,75,26] test_input = { "nums": [15,29,16,37,10,70,58,5,33,76], "limit": 8 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,29,10,33,15,70,58,16,37,76] test_input = { "nums": [15,33,1,74,47,6,60,95,78,72], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [15,33,1,72,47,6,60,95,74,78] test_input = { "nums": [16,20,79,92,17,7,70,41,54,18], "limit": 6 } assert my_solution.lexicographicallySmallestArray(**test_input) == [16,17,79,92,18,7,70,41,54,20] test_input = { "nums": [16,43,19,36,99,15,70,89,45,71], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [15,43,16,36,99,19,70,89,45,71] test_input = { "nums": [17,99,88,73,13,1,3,5,55,4], "limit": 15 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,73,88,99,3,4,5,13,55,17] test_input = { "nums": [18,97,57,1,23,36,77,80,47,91], "limit": 10 } assert my_solution.lexicographicallySmallestArray(**test_input) == [18,91,47,1,23,36,77,80,57,97] test_input = { "nums": [19,25,49,96,35,69,81,81,51,50], "limit": 12 } assert my_solution.lexicographicallySmallestArray(**test_input) == [19,25,49,96,35,69,81,81,50,51] test_input = { "nums": [19,37,12,11,70,99,88,36,64,9], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [19,36,9,11,70,99,88,37,64,12] test_input = { "nums": [19,64,26,5,70,10,17,66,51,36], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [5,51,10,17,64,19,26,66,70,36] test_input = { "nums": [21,14,21,34,4,88,39,62,30,20], "limit": 12 } assert my_solution.lexicographicallySmallestArray(**test_input) == [4,14,20,21,21,88,30,62,34,39] test_input = { "nums": [22,94,100,54,97,14,100,48,41,35], "limit": 6 } assert my_solution.lexicographicallySmallestArray(**test_input) == [22,94,97,48,100,14,100,54,35,41] test_input = { "nums": [23,50,8,48,62,26,92,5,96,9], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [23,48,5,50,62,26,92,8,96,9] test_input = { "nums": [25,47,34,69,36,91,14,44,37,2], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [25,47,34,69,36,91,14,44,37,2] test_input = { "nums": [25,58,36,16,42,57,17,96,10,2], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [2,57,10,16,17,58,25,96,36,42] test_input = { "nums": [26,21,9,15,94,47,52,86,89,7], "limit": 9 } assert my_solution.lexicographicallySmallestArray(**test_input) == [7,9,15,21,86,47,52,89,94,26] test_input = { "nums": [27,26,24,2,95,90,41,14,20,35], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [24,26,27,2,95,90,41,14,20,35] test_input = { "nums": [27,56,68,41,39,80,60,36,24,5], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [24,56,60,27,36,68,80,39,41,5] test_input = { "nums": [27,71,52,71,68,2,49,37,34,97], "limit": 8 } assert my_solution.lexicographicallySmallestArray(**test_input) == [27,68,49,71,71,2,52,34,37,97] test_input = { "nums": [29,82,25,91,17,9,38,25,29,68], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [25,82,25,91,17,9,38,29,29,68] test_input = { "nums": [30,48,76,86,21,1,55,49,90,9], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [30,48,76,86,21,1,55,49,90,9] test_input = { "nums": [31,10,64,15,60,32,88,79,79,33], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [31,10,64,15,60,32,88,79,79,33] test_input = { "nums": [32,70,43,51,40,73,56,39,75,45], "limit": 8 } assert my_solution.lexicographicallySmallestArray(**test_input) == [32,70,39,40,43,73,45,51,75,56] test_input = { "nums": [32,95,51,87,29,43,21,55,45,84], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [32,95,51,87,29,43,21,55,45,84] test_input = { "nums": [33,25,25,65,82,71,56,82,13,46], "limit": 14 } assert my_solution.lexicographicallySmallestArray(**test_input) == [13,25,25,33,46,56,65,71,82,82] test_input = { "nums": [33,37,77,41,83,75,96,97,4,60], "limit": 15 } assert my_solution.lexicographicallySmallestArray(**test_input) == [33,37,60,41,75,77,83,96,4,97] test_input = { "nums": [35,81,18,79,47,53,20,2,98,22], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [35,79,18,81,47,53,20,2,98,22] test_input = { "nums": [36,39,100,4,44,33,65,11,15,35], "limit": 10 } assert my_solution.lexicographicallySmallestArray(**test_input) == [33,35,100,4,36,39,65,11,15,44] test_input = { "nums": [38,56,60,98,21,15,70,37,24,61], "limit": 15 } assert my_solution.lexicographicallySmallestArray(**test_input) == [15,56,60,98,21,24,61,37,38,70] test_input = { "nums": [39,36,18,39,99,51,68,92,5,38], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [36,38,18,39,99,51,68,92,5,39] test_input = { "nums": [39,89,81,37,67,37,98,89,49,47], "limit": 12 } assert my_solution.lexicographicallySmallestArray(**test_input) == [37,81,89,37,67,39,89,98,47,49] test_input = { "nums": [40,67,99,53,95,47,59,99,64,44], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [40,44,95,47,99,53,59,99,64,67] test_input = { "nums": [40,97,72,48,55,91,83,82,91,63], "limit": 11 } assert my_solution.lexicographicallySmallestArray(**test_input) == [40,48,55,63,72,82,83,91,91,97] test_input = { "nums": [41,10,22,43,17,38,67,7,68,70], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [38,7,10,41,17,43,67,22,68,70] test_input = { "nums": [41,25,83,44,39,37,67,33,58,5], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [41,25,83,44,39,37,67,33,58,5] test_input = { "nums": [41,87,34,74,77,62,18,28,5,8], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [41,87,34,74,77,62,18,28,5,8] test_input = { "nums": [45,1,66,44,45,74,75,96,31,47], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [44,1,66,45,45,74,75,96,31,47] test_input = { "nums": [45,64,77,71,73,6,24,55,82,25], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [45,64,77,71,73,6,24,55,82,25] test_input = { "nums": [46,72,1,33,1,51,78,96,44,20], "limit": 1 } assert my_solution.lexicographicallySmallestArray(**test_input) == [46,72,1,33,1,51,78,96,44,20] test_input = { "nums": [47,32,72,79,16,69,85,70,87,73], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [47,32,69,79,16,70,85,72,87,73] test_input = { "nums": [47,94,72,49,50,62,17,22,85,86], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [47,94,72,49,50,62,17,22,85,86] test_input = { "nums": [48,39,45,58,26,57,38,63,82,80], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [48,38,45,57,26,58,39,63,80,82] test_input = { "nums": [48,51,51,39,54,56,57,6,1,40], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [48,51,51,39,54,56,57,6,1,40] test_input = { "nums": [49,4,95,24,20,12,70,60,82,62], "limit": 11 } assert my_solution.lexicographicallySmallestArray(**test_input) == [49,4,95,12,20,24,60,62,82,70] test_input = { "nums": [49,7,92,79,43,88,31,89,36,97], "limit": 8 } assert my_solution.lexicographicallySmallestArray(**test_input) == [31,7,88,79,36,89,43,92,49,97] test_input = { "nums": [49,16,32,11,7,57,69,41,52,23], "limit": 15 } assert my_solution.lexicographicallySmallestArray(**test_input) == [7,11,16,23,32,41,49,52,57,69] test_input = { "nums": [49,26,82,77,52,76,90,23,64,42], "limit": 12 } assert my_solution.lexicographicallySmallestArray(**test_input) == [42,23,49,52,64,76,77,26,82,90] test_input = { "nums": [49,62,63,32,57,22,74,87,42,19], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [19,22,32,42,49,57,62,63,74,87] test_input = { "nums": [49,93,5,15,56,2,65,74,82,42], "limit": 13 } assert my_solution.lexicographicallySmallestArray(**test_input) == [42,49,2,5,56,15,65,74,82,93] test_input = { "nums": [49,93,100,79,76,14,90,32,4,5], "limit": 10 } assert my_solution.lexicographicallySmallestArray(**test_input) == [49,90,93,76,79,4,100,32,5,14] test_input = { "nums": [49,96,75,44,74,78,82,40,43,68], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [49,96,74,40,75,78,82,43,44,68] test_input = { "nums": [51,61,49,11,69,78,40,98,68,36], "limit": 10 } assert my_solution.lexicographicallySmallestArray(**test_input) == [36,40,49,11,51,61,68,98,69,78] test_input = { "nums": [51,99,52,80,83,69,18,49,71,13], "limit": 14 } assert my_solution.lexicographicallySmallestArray(**test_input) == [49,99,51,69,71,80,13,52,83,18] test_input = { "nums": [52,28,93,16,33,37,37,21,47,64], "limit": 12 } assert my_solution.lexicographicallySmallestArray(**test_input) == [16,21,93,28,33,37,37,47,52,64] test_input = { "nums": [53,7,99,22,3,50,62,70,56,40], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [53,7,99,22,3,50,62,70,56,40] test_input = { "nums": [53,17,39,72,5,78,40,3,84,20], "limit": 5 } assert my_solution.lexicographicallySmallestArray(**test_input) == [53,17,39,72,3,78,40,5,84,20] test_input = { "nums": [53,71,55,38,26,89,20,98,55,21], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [53,71,55,38,26,89,20,98,55,21] test_input = { "nums": [53,71,74,7,99,64,95,99,90,22], "limit": 9 } assert my_solution.lexicographicallySmallestArray(**test_input) == [53,64,71,7,90,74,95,99,99,22] test_input = { "nums": [55,19,82,86,12,64,44,76,88,31], "limit": 4 } assert my_solution.lexicographicallySmallestArray(**test_input) == [55,19,82,86,12,64,44,76,88,31] test_input = { "nums": [56,13,55,1,18,36,45,25,20,52], "limit": 14 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,13,18,20,25,36,45,52,55,56] test_input = { "nums": [56,28,50,100,56,99,80,71,6,5], "limit": 7 } assert my_solution.lexicographicallySmallestArray(**test_input) == [50,28,56,99,56,100,80,71,5,6] test_input = { "nums": [56,35,19,2,83,20,96,42,33,68], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [56,33,19,2,83,20,96,42,35,68] test_input = { "nums": [56,69,94,21,65,46,64,91,75,25], "limit": 5 } assert my_solution.lexicographicallySmallestArray(**test_input) == [56,64,91,21,65,46,69,94,75,25]
1,700,965,800
weekly-contest-373-count-beautiful-substrings-ii
https://leetcode.com/problems/count-beautiful-substrings-ii
count-beautiful-substrings-ii
{ "questionId": "3208", "questionFrontendId": "2949", "title": "Count Beautiful Substrings II", "titleSlug": "count-beautiful-substrings-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 137, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh", k = 2 输出:2 解释:字符串 s 中有 2 个美丽子字符串。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["y","h"])。 可以看出字符串 "aeyh" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["b","y"])。 可以看出字符串 "baey" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 可以证明字符串 s 中只有 2 个美丽子字符串。 示例 2: 输入:s = "abba", k = 1 输出:3 解释:字符串 s 中有 3 个美丽子字符串。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 2(["a","a"]),consonants = 2(["b","b"])。 可以证明字符串 s 中只有 3 个美丽子字符串。 示例 3: 输入:s = "bcdf", k = 1 输出:0 解释:字符串 s 中没有美丽子字符串。 提示: 1 <= s.length <= 5 * 104 1 <= k <= 1000 s 仅由小写英文字母组成。 """ class Solution: def beautifulSubstrings(self, s: str, k: int) -> int:
给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh", k = 2 输出:2 解释:字符串 s 中有 2 个美丽子字符串。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["y","h"])。 可以看出字符串 "aeyh" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 - 子字符串 "baeyh",vowels = 2(["a","e"]),consonants = 2(["b","y"])。 可以看出字符串 "baey" 是美丽字符串,因为 vowels == consonants 且 vowels * consonants % k == 0 。 可以证明字符串 s 中只有 2 个美丽子字符串。 示例 2: 输入:s = "abba", k = 1 输出:3 解释:字符串 s 中有 3 个美丽子字符串。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 1(["a"]),consonants = 1(["b"])。 - 子字符串 "abba",vowels = 2(["a","a"]),consonants = 2(["b","b"])。 可以证明字符串 s 中只有 3 个美丽子字符串。 示例 3: 输入:s = "bcdf", k = 1 输出:0 解释:字符串 s 中没有美丽子字符串。 提示: 1 <= s.length <= 5 * 104 1 <= k <= 1000 s 仅由小写英文字母组成。 请完成下面的代码来解决上述问题: ```python class Solution: def beautifulSubstrings(self, s: str, k: int) -> int: ```
my_solution = Solution() test_input = { "s": "baeyh", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "abba", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "bcdf", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ihroyeeb", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "uzuxpzou", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "ouuoeqd", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "eeebjoxxujuaeoqibd", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 4 test_input = { "s": "ilougekqlovegioemdvu", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 21 test_input = { "s": "tqaewreikaztwpfwnef", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "oykiuhsafgfjumnzb", "k": 7 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ifvsa", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "svzauyuevujektj", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "urahjig", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "ime", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "oacghieut", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "aoluu", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ioaoiciiuoziout", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "ouafupsuhid", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ox", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "tlaiwoauazutusiaaui", "k": 10 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "caepeym", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "apyxvceue", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "imkqbb", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "caaz", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "pyicoy", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "uopmyrsntjhiroikup", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "aujfxqxcj", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "eeizejuoxeumz", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "uuouuaifnboeiulttio", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 10 test_input = { "s": "woozzxd", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "pulorolqcvhafexui", "k": 9 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "hmuaewojioizoguvoaje", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 4 test_input = { "s": "b", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "aiejouohnqnketinvat", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "mjiogpri", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "movbyaeouil", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "puureouausxmitvav", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "op", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "iuhoezpooxcohtlapolo", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 39 test_input = { "s": "cioi", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "pueutaoyaxk", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "iiuresacruaaan", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "agntyaazvpejidwaph", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "wiybolyniexiibou", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "coiyakadxi", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 8 test_input = { "s": "oraajoeruiakixj", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "jeayap", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 7 test_input = { "s": "iu", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "awozoy", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "fheabmlsyeeeuoeogyz", "k": 9 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "eaizneuxi", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 10 test_input = { "s": "uurqufaucsuoqljh", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 14 test_input = { "s": "jrtept", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "olgioxooiejooosaed", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "uizoy", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "lswabfiujjhexzos", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "iuu", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qeaxut", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 7 test_input = { "s": "aojiau", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "oaiaaaargkonlcsoaygf", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "zoowrawkm", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "uqiwuoevkfhkkua", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "kavuaaeodvaxicm", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qpxeceq", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "iaabaofuodcbek", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "eel", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "ikeuhe", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 5 test_input = { "s": "lueikvo", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "oauau", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qzoieeotieeakqraeao", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 4 test_input = { "s": "ehaascocsdmgekni", "k": 9 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "euqeklniykiji", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "vaeiiioidiioxhduu", "k": 7 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "aa", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "chaua", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "edfrglfr", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "dqbe", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "ghooirorxge", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "fodartekaonq", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 9 test_input = { "s": "feeanzkjpfehzeuni", "k": 6 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "ignoouesduu", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 6 test_input = { "s": "yif", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "gondfjaeeuhbuuasgip", "k": 10 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "djooomsffoonelyeode", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 14 test_input = { "s": "pgaimei", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "naipqentonee", "k": 4 } assert my_solution.beautifulSubstrings(**test_input) == 8 test_input = { "s": "bouov", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "lcuhoypz", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "g", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "qc", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "mhznea", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "uxvjixdujgyfauo", "k": 8 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "iyjkuox", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "xbjfoayfpafatnuyord", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "nvoede", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 1 test_input = { "s": "usnuaxpaktrweatruu", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "euojmsora", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 10 test_input = { "s": "iapgoi", "k": 3 } assert my_solution.beautifulSubstrings(**test_input) == 0 test_input = { "s": "uafuimcpxyeoixgbyeio", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "weuaatpu", "k": 5 } assert my_solution.beautifulSubstrings(**test_input) == 0
1,700,965,800
biweekly-contest-118-find-words-containing-character
https://leetcode.com/problems/find-words-containing-character
find-words-containing-character
{ "questionId": "3194", "questionFrontendId": "2942", "title": "Find Words Containing Character", "titleSlug": "find-words-containing-character", "isPaidOnly": false, "difficulty": "Easy", "likes": 134, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的字符串数组words和一个字符x。 请你返回一个 下标数组,表示下标在数组中对应的单词包含字符 x。 注意,返回的数组可以是任意顺序。 示例 1: 输入:words = ["leet","code"], x = "e" 输出:[0,1] 解释:"e" 在两个单词中都出现了:"leet" 和 "code" 。所以我们返回下标 0 和 1 。 示例 2: 输入:words = ["abc","bcd","aaaa","cbc"], x = "a" 输出:[0,2] 解释:"a" 在 "abc" 和 "aaaa" 中出现了,所以我们返回下标 0 和 2 。 示例 3: 输入:words = ["abc","bcd","aaaa","cbc"], x = "z" 输出:[] 解释:"z" 没有在任何单词中出现。所以我们返回空数组。 提示: 1 <= words.length <= 50 1 <= words[i].length <= 50 x是一个小写英文字母。 words[i]只包含小写英文字母。 """ class Solution: def findWordsContaining(self, words: List[str], x: str) -> List[int]:
给你一个下标从 0开始的字符串数组words和一个字符x。 请你返回一个 下标数组,表示下标在数组中对应的单词包含字符 x。 注意,返回的数组可以是任意顺序。 示例 1: 输入:words = ["leet","code"], x = "e" 输出:[0,1] 解释:"e" 在两个单词中都出现了:"leet" 和 "code" 。所以我们返回下标 0 和 1 。 示例 2: 输入:words = ["abc","bcd","aaaa","cbc"], x = "a" 输出:[0,2] 解释:"a" 在 "abc" 和 "aaaa" 中出现了,所以我们返回下标 0 和 2 。 示例 3: 输入:words = ["abc","bcd","aaaa","cbc"], x = "z" 输出:[] 解释:"z" 没有在任何单词中出现。所以我们返回空数组。 提示: 1 <= words.length <= 50 1 <= words[i].length <= 50 x是一个小写英文字母。 words[i]只包含小写英文字母。 请完成下面的代码来解决上述问题: ```python class Solution: def findWordsContaining(self, words: List[str], x: str) -> List[int]: ```
my_solution = Solution() test_input = { "words": ["leet","code"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [0,1] test_input = { "words": ["abc","bcd","aaaa","cbc"], "x": "a" } assert my_solution.findWordsContaining(**test_input) == [0,2] test_input = { "words": ["abc","bcd","aaaa","cbc"], "x": "z" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["sgtkshnss","m","ryvbkyvuz","ezittyjwgb","wudlwg"], "x": "x" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["lkwnhpbj","tlohm","juazsb","f","rq"], "x": "v" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["aaa","imvtfjmxr","wbzfoovjnf","hqwrwmi"], "x": "c" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["utyeachht","bgpkcs","skeecqvvvw","nccrd"], "x": "i" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["alcpxexztg","r"], "x": "h" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["ekcpg","pdknua","fot","janppw","ofomkfvx"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["dq","rlvopu"], "x": "d" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["wzppkd","jxvk","zaztizmwuv","hvcdtobr"], "x": "b" } assert my_solution.findWordsContaining(**test_input) == [3] test_input = { "words": ["y","hs","qznrkpi"], "x": "v" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["pze","yojczsb","mjvyr","i","xsygks"], "x": "q" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["qsgtjagcu","m"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["kidtwmw","ogh","trdedlh","wwbtlindg","naoylytpof","ujcbzwzkm","doamcoxdv"], "x": "o" } assert my_solution.findWordsContaining(**test_input) == [1,4,6] test_input = { "words": ["tsmeupctki"], "x": "t" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["dqxlbljmpf","uvdzfoiqg","jsnbnx","fbedae","nodewb","o","ivepktj"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [1] test_input = { "words": ["fjlmmecm","sautsoorhl","n","hsyco","amlukrpjpv","rmhdnj","g"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["khjchmeciv","vgx","xghr","bbufgegu","qyfxu"], "x": "r" } assert my_solution.findWordsContaining(**test_input) == [2] test_input = { "words": ["jhtcugtcpl","bvhlgmmla","ntfkwzite","imbtzafaj","sdl","t"], "x": "m" } assert my_solution.findWordsContaining(**test_input) == [1,3] test_input = { "words": ["kxoziqoafc","vifcxifq"], "x": "q" } assert my_solution.findWordsContaining(**test_input) == [0,1] test_input = { "words": ["ckfkjjsonl","scaaug","rmvqzyiwc","a","smymw"], "x": "p" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["t","exo","npr","skd","bxpmbu"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [1] test_input = { "words": ["eulsl","fwooyct","ypytexil"], "x": "c" } assert my_solution.findWordsContaining(**test_input) == [1] test_input = { "words": ["nhd","zheyegi","ogz","fpybmcc","ntbbwtde"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [1,2] test_input = { "words": ["gwzvusl","upcpvbfyxy","hg","yu","wsfqgzhh","zgphqacsyo"], "x": "o" } assert my_solution.findWordsContaining(**test_input) == [5] test_input = { "words": ["uiovpph","xxj","uwzxzvkobk"], "x": "r" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["abtrpwo","sgaegnavk","pfmv"], "x": "z" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["m","fxtphsdmgy","otq","vwuhhnebr","yen"], "x": "y" } assert my_solution.findWordsContaining(**test_input) == [1,4] test_input = { "words": ["irlzx","lbrknhl","roupfj","fskaieszo","nz","ijfyejq"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [3,5] test_input = { "words": ["raavc","tx"], "x": "l" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["bkpuvcrexw","hxtbcdprhr","ovt","xgurm","pjcz","sbhwpjmyz"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [3] test_input = { "words": ["f","xlmy","akbiqa","fobo"], "x": "s" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["mhan"], "x": "a" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["uisx"], "x": "o" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["znqdolksyn","keewspe","ffod","lah","gadhym","awnyymd","fvkl"], "x": "v" } assert my_solution.findWordsContaining(**test_input) == [6] test_input = { "words": ["ftujx","dnbwrurk","t","x","zjzhdl","jc"], "x": "t" } assert my_solution.findWordsContaining(**test_input) == [0,2] test_input = { "words": ["zrwf","thp","qecwlnq","w","teetdaxx"], "x": "t" } assert my_solution.findWordsContaining(**test_input) == [1,4] test_input = { "words": ["xyzgb","qflfrfqgaf"], "x": "l" } assert my_solution.findWordsContaining(**test_input) == [1] test_input = { "words": ["shnjr","qfvop"], "x": "y" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["fmwclqh","xbphhgreze","yi","gmtzrfdab","uicqa","n"], "x": "i" } assert my_solution.findWordsContaining(**test_input) == [2,4] test_input = { "words": ["jgkv","njhwihtv","v"], "x": "z" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["tqkwoofh","bcgngl","frjpqgrr","drvb"], "x": "x" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["npkvocbw","tn","dp","c","g","fsxvzcnty","ywnf"], "x": "k" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["leompil","vta","fzrsps","yp","bykmgwgk"], "x": "o" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["umq","c","ctuh","eadzeuui","tabum","isuct"], "x": "p" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["rnmpdkmrnb","icxxsvss","h","gd"], "x": "s" } assert my_solution.findWordsContaining(**test_input) == [1] test_input = { "words": ["ft","hsjf","e","xi"], "x": "w" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["ozf","xkehlkgp","vliewlbv","okgaahah"], "x": "b" } assert my_solution.findWordsContaining(**test_input) == [2] test_input = { "words": ["gbktzr","kbamubluz","dwoi","crhldx","idjronpded","rqaz"], "x": "c" } assert my_solution.findWordsContaining(**test_input) == [3] test_input = { "words": ["gvbzqcb","rwtbra","iuijl","qbmpbi"], "x": "c" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["lsh","szhxhcdc","quem","zupiydjeqp","czxyvysrrb","aqnlqtnfiv"], "x": "p" } assert my_solution.findWordsContaining(**test_input) == [3] test_input = { "words": ["leuah","liaoczeuch","ol","ify","layh","ifzudwuybw","x"], "x": "p" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["ksdpwwho","ktunsikyu"], "x": "a" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["vpypaumzlp","kqrb","pgw"], "x": "l" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["jkrpnx","c","kqi","xrsaviyusg","waoxq","fld","otxfgcp"], "x": "l" } assert my_solution.findWordsContaining(**test_input) == [5] test_input = { "words": ["tetw","zl","wd","hnkxoxlnz","dexgufawjd","oolpr","yyfwizbsl"], "x": "p" } assert my_solution.findWordsContaining(**test_input) == [5] test_input = { "words": ["hihprd","kitgiflc","nr","idduuahfkm"], "x": "x" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["flfxeca","g"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["st","betf","ipacxza","jpnw"], "x": "r" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["cvuxnzaib","c","tiytr","yiav","hp","yg"], "x": "d" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["yz","k","midujexvn","kwcgbht"], "x": "y" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["qcxobdaxv"], "x": "q" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["b","shrexcf","ve","eqpbnuy","qdhahodo","aerdf","bdjlaakagk"], "x": "p" } assert my_solution.findWordsContaining(**test_input) == [3] test_input = { "words": ["ympv"], "x": "q" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["thfy","lnfzoyafiy","qmc","boijcl","pvbzmsa","yjarwylcyc"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["hqptwi"], "x": "o" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["bv","xgrhtjnxh","fdtljkxa","po","hejof"], "x": "k" } assert my_solution.findWordsContaining(**test_input) == [2] test_input = { "words": ["mfdrclyx","pith"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["bxeblhrl","o","uvv"], "x": "b" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["giygz"], "x": "u" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["ffqw","nykncbxrqi","pgzy","of","oye","f"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [2] test_input = { "words": ["jjnh","nrbh","z"], "x": "l" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["gdzkdtvrm","ps","kp","sbdlkac","s","bt"], "x": "n" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["hpsk","stjltzz","gvbjwzktgg","hmeovbxvv","gqaxqoshbh","mqnwyabqq","sq"], "x": "f" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["gwmg","qdjeaxgc","rlajltxpd","d"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [0,1] test_input = { "words": ["dupx","r","j","wq","macfcfoz"], "x": "r" } assert my_solution.findWordsContaining(**test_input) == [1] test_input = { "words": ["rmypzoyto","wvhtrbuz","dgt","tmhqswmkx","trpjwzitp","tbetdxic"], "x": "t" } assert my_solution.findWordsContaining(**test_input) == [0,1,2,3,4,5] test_input = { "words": ["vpkjymgdb","s","gv","geie"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [0,2,3] test_input = { "words": ["epnmbry","hhfhprvqba"], "x": "l" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["zst","mjzbdxsks","dza","neqj","oqeilr"], "x": "d" } assert my_solution.findWordsContaining(**test_input) == [1,2] test_input = { "words": ["ffruqk","sse","cyj","tntq","mibbhhpce"], "x": "c" } assert my_solution.findWordsContaining(**test_input) == [2,4] test_input = { "words": ["vumzrbe","qudq","qfrt"], "x": "u" } assert my_solution.findWordsContaining(**test_input) == [0,1] test_input = { "words": ["wcrrprvu","fizkw","vzcjxhjy","e"], "x": "r" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["gjk","vri"], "x": "n" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["fds","vbmg","p","iesyvc","wgmyxhoo","yfllvzr"], "x": "f" } assert my_solution.findWordsContaining(**test_input) == [0,5] test_input = { "words": ["mifbjo","kpjlwfbas","skhueysodn","zeewicisy"], "x": "g" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["pvkmoccv","j"], "x": "y" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["s","uhcfwsssbe","iwofeukmx","yfta","ovrdcb","psnje"], "x": "s" } assert my_solution.findWordsContaining(**test_input) == [0,1,5] test_input = { "words": ["klpzrjw","qmrhbpa"], "x": "v" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["fzegksjmw","masiwhjue","gngsht","xwvmp","aahn","dwxr"], "x": "c" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["mveahpesx","tsqds","g","mux","bivffitjx","zfsqdje"], "x": "f" } assert my_solution.findWordsContaining(**test_input) == [4,5] test_input = { "words": ["c"], "x": "a" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["jzmhnhqkq"], "x": "a" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["cfdgbc","ltpvko","batjenrlq","edwefhw"], "x": "t" } assert my_solution.findWordsContaining(**test_input) == [1,2] test_input = { "words": ["smlcojfydr","slb"], "x": "r" } assert my_solution.findWordsContaining(**test_input) == [0] test_input = { "words": ["lnjimir"], "x": "x" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["do"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["xyyvbxsb","dc","mmqpb","mmbwv","wdreyof","kpk","reeb"], "x": "l" } assert my_solution.findWordsContaining(**test_input) == [] test_input = { "words": ["ytvyknnmzv","jsoe","wctzk"], "x": "i" } assert my_solution.findWordsContaining(**test_input) == []
1,700,922,600
biweekly-contest-118-maximize-area-of-square-hole-in-grid
https://leetcode.com/problems/maximize-area-of-square-hole-in-grid
maximize-area-of-square-hole-in-grid
{ "questionId": "3214", "questionFrontendId": "2943", "title": "Maximize Area of Square Hole in Grid", "titleSlug": "maximize-area-of-square-hole-in-grid", "isPaidOnly": false, "difficulty": "Medium", "likes": 73, "dislikes": 110, "categoryTitle": "Algorithms" }
""" 给你一个网格图,由n + 2条 横线段和m + 2条竖线段组成,一开始所有区域均为1 x 1的单元格。 所有线段的编号从 1开始。 给你两个整数n 和m。 同时给你两个整数数组hBars 和vBars。 hBars 包含区间[2, n + 1]内互不相同的横线段编号。 vBars包含[2, m + 1]内互不相同的竖线段编号。 如果满足以下条件之一,你可以 移除两个数组中的部分线段: 如果移除的是横线段,它必须是hBars中的值。 如果移除的是竖线段,它必须是vBars中的值。 请你返回移除一些线段后(可能不移除任何线段),剩余网格图中 最大正方形空洞的面积,正方形空洞的意思是正方形 内部 不含有任何线段。 示例 1: 输入:n = 2, m = 1, hBars = [2,3], vBars = [2] 输出:4 解释:左边的图是一开始的网格图。 横线编号的范围是区间 [1,4] ,竖线编号的范围是区间 [1,3] 。 可以移除的横线段为 [2,3] ,竖线段为 [2] 。 一种得到最大正方形面积的方法是移除横线段 2 和竖线段 2 。 操作后得到的网格图如右图所示。 正方形空洞面积为 4。 无法得到面积大于 4 的正方形空洞。 所以答案为 4 。 示例 2: 输入:n = 1, m = 1, hBars = [2], vBars = [2] 输出:4 解释:左边的图是一开始的网格图。 横线编号的范围是区间 [1,3] ,竖线编号的范围是区间 [1,3] 。 可以移除的横线段为 [2] ,竖线段为 [2] 。 一种得到最大正方形面积的方法是移除横线段 2 和竖线段 2 。 操作后得到的网格图如右图所示。 正方形空洞面积为 4。 无法得到面积大于 4 的正方形空洞。 所以答案为 4 。 示例 3: 输入:n = 2, m = 3, hBars = [2,3], vBars = [2,3,4] 输出:9 解释:左边的图是一开始的网格图。 横线编号的范围是区间 [1,4] ,竖线编号的范围是区间 [1,5] 。 可以移除的横线段为 [2,3] ,竖线段为 [2,3,4] 。 一种得到最大正方形面积的方法是移除横线段 2、3 和竖线段 3、4 。 操作后得到的网格图如右图所示。 正方形空洞面积为 9。 无法得到面积大于 9 的正方形空洞。 所以答案为 9 。 提示: 1 <= n <= 109 1 <= m <= 109 1 <= hBars.length <= 100 2 <= hBars[i] <= n + 1 1 <= vBars.length <= 100 2 <= vBars[i] <= m + 1 hBars中的值互不相同。 vBars 中的值互不相同。 """ class Solution: def maximizeSquareHoleArea(self, n: int, m: int, hBars: List[int], vBars: List[int]) -> int:
给你一个网格图,由n + 2条 横线段和m + 2条竖线段组成,一开始所有区域均为1 x 1的单元格。 所有线段的编号从 1开始。 给你两个整数n 和m。 同时给你两个整数数组hBars 和vBars。 hBars 包含区间[2, n + 1]内互不相同的横线段编号。 vBars包含[2, m + 1]内互不相同的竖线段编号。 如果满足以下条件之一,你可以 移除两个数组中的部分线段: 如果移除的是横线段,它必须是hBars中的值。 如果移除的是竖线段,它必须是vBars中的值。 请你返回移除一些线段后(可能不移除任何线段),剩余网格图中 最大正方形空洞的面积,正方形空洞的意思是正方形 内部 不含有任何线段。 示例 1: 输入:n = 2, m = 1, hBars = [2,3], vBars = [2] 输出:4 解释:左边的图是一开始的网格图。 横线编号的范围是区间 [1,4] ,竖线编号的范围是区间 [1,3] 。 可以移除的横线段为 [2,3] ,竖线段为 [2] 。 一种得到最大正方形面积的方法是移除横线段 2 和竖线段 2 。 操作后得到的网格图如右图所示。 正方形空洞面积为 4。 无法得到面积大于 4 的正方形空洞。 所以答案为 4 。 示例 2: 输入:n = 1, m = 1, hBars = [2], vBars = [2] 输出:4 解释:左边的图是一开始的网格图。 横线编号的范围是区间 [1,3] ,竖线编号的范围是区间 [1,3] 。 可以移除的横线段为 [2] ,竖线段为 [2] 。 一种得到最大正方形面积的方法是移除横线段 2 和竖线段 2 。 操作后得到的网格图如右图所示。 正方形空洞面积为 4。 无法得到面积大于 4 的正方形空洞。 所以答案为 4 。 示例 3: 输入:n = 2, m = 3, hBars = [2,3], vBars = [2,3,4] 输出:9 解释:左边的图是一开始的网格图。 横线编号的范围是区间 [1,4] ,竖线编号的范围是区间 [1,5] 。 可以移除的横线段为 [2,3] ,竖线段为 [2,3,4] 。 一种得到最大正方形面积的方法是移除横线段 2、3 和竖线段 3、4 。 操作后得到的网格图如右图所示。 正方形空洞面积为 9。 无法得到面积大于 9 的正方形空洞。 所以答案为 9 。 提示: 1 <= n <= 109 1 <= m <= 109 1 <= hBars.length <= 100 2 <= hBars[i] <= n + 1 1 <= vBars.length <= 100 2 <= vBars[i] <= m + 1 hBars中的值互不相同。 vBars 中的值互不相同。 请完成下面的代码来解决上述问题: ```python class Solution: def maximizeSquareHoleArea(self, n: int, m: int, hBars: List[int], vBars: List[int]) -> int: ```
my_solution = Solution() test_input = { "n": 2, "m": 1, "hBars": [2,3], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 1, "hBars": [2], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 3, "hBars": [2,3], "vBars": [2,3,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 1, "m": 5, "hBars": [2], "vBars": [2,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 4, "hBars": [3,2], "vBars": [4,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 4, "hBars": [2], "vBars": [2,3,5,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 4, "hBars": [2], "vBars": [4,3,2,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 3, "m": 2, "hBars": [3,2,4], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 3, "m": 2, "hBars": [4,2,3], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 14, "m": 4, "hBars": [13], "vBars": [3,4,5,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 19, "m": 7, "hBars": [6,12,4], "vBars": [6,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 4, "hBars": [2,3], "vBars": [4,2,3,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 4, "m": 2, "hBars": [2,5,4,3], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 5, "m": 1, "hBars": [2,4,3,6,5], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 6, "hBars": [2], "vBars": [3,2,7,4,6,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 13, "hBars": [2], "vBars": [4,14,2,12,11,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 5, "hBars": [2,3], "vBars": [6,2,5,4,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 5, "m": 2, "hBars": [2,3,6,4,5], "vBars": [2,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 6, "m": 1, "hBars": [7,4,3,2,5,6], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 4, "m": 4, "hBars": [2,3,4,5], "vBars": [5,4,3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 4, "m": 4, "hBars": [3,4,2,5], "vBars": [2,5,3,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 6, "m": 2, "hBars": [7,3,5,4,6,2], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 7, "m": 11, "hBars": [7,4,5,2,8,6,3], "vBars": [4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 8, "hBars": [2], "vBars": [9,7,8,2,5,6,4,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 9, "hBars": [2], "vBars": [5,2,10,4,3,6,8,7] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 7, "hBars": [2,3], "vBars": [2,5,6,8,7,3,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 2, "m": 7, "hBars": [2,3], "vBars": [2,8,6,7,5,3,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 4, "m": 5, "hBars": [3,2,4,5], "vBars": [4,3,6,5,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 4, "m": 5, "hBars": [5,3,4,2], "vBars": [5,3,6,4,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 4, "m": 40, "hBars": [5,3,2,4], "vBars": [36,41,6,34,33] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 8, "m": 1, "hBars": [4,7,9,8,6,2,3,5], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 11, "m": 6, "hBars": [8,9,6], "vBars": [5,3,6,4,2,7] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 22, "m": 50, "hBars": [6,19,8,17,23], "vBars": [51,3,32,44] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 27, "m": 2, "hBars": [2,26,28,22,4,8,23], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 1, "m": 9, "hBars": [2], "vBars": [3,6,10,4,8,5,9,7,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 9, "hBars": [2], "vBars": [3,7,5,9,10,2,4,8,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 3, "m": 7, "hBars": [2,4,3], "vBars": [5,4,2,3,7,6,8] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 3, "m": 7, "hBars": [4,3,2], "vBars": [2,7,3,6,5,4,8] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 3, "m": 7, "hBars": [4,3,2], "vBars": [3,7,5,2,6,4,8] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 3, "m": 7, "hBars": [4,3,2], "vBars": [8,2,5,3,6,4,7] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 3, "m": 13, "hBars": [2,4,3], "vBars": [4,6,7,12,10,13,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 4, "m": 6, "hBars": [2,3,4,5], "vBars": [7,2,4,6,3,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 5, "m": 5, "hBars": [4,6,5,2,3], "vBars": [2,4,5,6,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 7, "m": 3, "hBars": [8,6,4,5,7,2,3], "vBars": [4,3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 8, "m": 2, "hBars": [4,2,6,8,7,5,3,9], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 9, "m": 1, "hBars": [2,9,3,10,4,6,7,8,5], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 9, "m": 1, "hBars": [9,5,4,8,7,10,3,2,6], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 12, "m": 5, "hBars": [10,9,13,6,3], "vBars": [3,4,2,5,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 29, "m": 2, "hBars": [25,14,11,29,7,10,16,8], "vBars": [2,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 1, "m": 10, "hBars": [2], "vBars": [3,4,6,8,5,7,9,10,11,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 10, "hBars": [2], "vBars": [10,6,5,7,4,3,11,8,9,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 9, "hBars": [2,3], "vBars": [6,7,9,3,10,2,5,4,8] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 2, "m": 9, "hBars": [3,2], "vBars": [4,8,2,6,7,3,5,9,10] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 4, "m": 7, "hBars": [5,4,3,2], "vBars": [8,7,5,2,4,3,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 5, "m": 6, "hBars": [2,6,5,3,4], "vBars": [4,2,5,3,7,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 5, "m": 6, "hBars": [5,3,6,2,4], "vBars": [5,7,2,4,3,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 5, "m": 6, "hBars": [6,4,3,5,2], "vBars": [2,4,5,7,6,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 5, "m": 11, "hBars": [4,2,6,3,5], "vBars": [8,11,10,12,6,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 6, "m": 5, "hBars": [4,5,3,2,7,6], "vBars": [6,3,5,4,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 6, "m": 5, "hBars": [5,2,3,7,4,6], "vBars": [6,2,4,3,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 6, "m": 5, "hBars": [6,3,4,2,7,5], "vBars": [2,5,4,3,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 6, "m": 5, "hBars": [7,2,3,4,5,6], "vBars": [6,5,4,2,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 8, "m": 3, "hBars": [4,6,9,3,8,2,7,5], "vBars": [2,4,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 8, "m": 3, "hBars": [5,6,9,3,2,4,8,7], "vBars": [2,4,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 8, "m": 3, "hBars": [8,6,4,3,7,2,9,5], "vBars": [4,2,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 8, "m": 3, "hBars": [9,2,7,6,8,3,4,5], "vBars": [4,2,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 9, "m": 2, "hBars": [5,4,6,8,9,10,2,3,7], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 9, "m": 2, "hBars": [6,3,5,4,8,9,2,10,7], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 10, "m": 1, "hBars": [4,3,10,2,11,5,6,9,8,7], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 11, "hBars": [2], "vBars": [7,12,6,3,4,9,5,10,11,2,8] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 10, "hBars": [2,3], "vBars": [11,10,2,8,7,5,6,9,3,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 4, "m": 8, "hBars": [5,2,3,4], "vBars": [8,7,5,9,4,2,3,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 5, "m": 7, "hBars": [2,3,6,4,5], "vBars": [6,8,4,5,3,7,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 6, "m": 10, "hBars": [2,4,3,6,5,7], "vBars": [11,3,9,6,10,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 10, "m": 2, "hBars": [8,5,4,3,10,2,11,9,6,7], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 11, "m": 1, "hBars": [2,6,9,7,5,11,3,10,4,12,8], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 44, "m": 2, "hBars": [5,16,18,28,3,9,6,35,14,10], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 1, "m": 12, "hBars": [2], "vBars": [12,9,3,13,7,2,6,11,10,8,4,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 11, "hBars": [2,3], "vBars": [3,7,2,5,12,9,10,4,8,11,6] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 2, "m": 11, "hBars": [2,3], "vBars": [12,10,6,7,2,3,5,11,4,8,9] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 5, "m": 8, "hBars": [2,4,6,3,5], "vBars": [8,7,9,4,2,5,6,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 6, "m": 7, "hBars": [5,4,6,3,2,7], "vBars": [4,7,6,5,2,8,3] } assert my_solution.maximizeSquareHoleArea(**test_input) == 49 test_input = { "n": 6, "m": 7, "hBars": [6,3,2,7,4,5], "vBars": [6,7,5,2,3,4,8] } assert my_solution.maximizeSquareHoleArea(**test_input) == 49 test_input = { "n": 8, "m": 5, "hBars": [7,4,3,9,2,8,6,5], "vBars": [5,2,6,3,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 9, "m": 4, "hBars": [4,5,6,10,7,2,3,9,8], "vBars": [5,3,2,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 9, "m": 4, "hBars": [9,6,3,10,2,8,4,5,7], "vBars": [4,3,2,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 10, "m": 3, "hBars": [5,4,9,8,3,6,11,2,10,7], "vBars": [4,3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16 test_input = { "n": 10, "m": 6, "hBars": [6,2,8,3,11,9,10,7,4,5], "vBars": [6,2,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 11, "m": 2, "hBars": [8,12,9,3,5,2,10,6,7,4,11], "vBars": [3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 28, "m": 31, "hBars": [29,24,4], "vBars": [22,24,2,14,26,4,29,13,15,25] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 13, "hBars": [2], "vBars": [9,5,2,6,8,11,7,10,3,13,14,4,12] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 12, "hBars": [3,2], "vBars": [13,2,7,4,12,9,10,3,6,5,8,11] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 2, "m": 12, "hBars": [3,2], "vBars": [13,4,7,8,3,2,11,12,5,9,6,10] } assert my_solution.maximizeSquareHoleArea(**test_input) == 9 test_input = { "n": 5, "m": 9, "hBars": [2,5,6,3,4], "vBars": [2,7,3,9,4,10,8,6,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 6, "m": 9, "hBars": [4,2,5,7,6,3], "vBars": [4,2,6,8,10,3,7,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 49 test_input = { "n": 8, "m": 6, "hBars": [4,8,6,7,2,9,3,5], "vBars": [4,7,6,3,2,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 49 test_input = { "n": 9, "m": 5, "hBars": [2,5,4,8,3,7,6,10,9], "vBars": [6,3,5,2,4] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 9, "m": 5, "hBars": [8,9,5,2,6,7,4,10,3], "vBars": [6,2,3,4,5] } assert my_solution.maximizeSquareHoleArea(**test_input) == 36 test_input = { "n": 10, "m": 4, "hBars": [4,8,6,3,10,2,7,9,5,11], "vBars": [5,4,3,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 25 test_input = { "n": 11, "m": 3, "hBars": [2,8,5,3,12,10,4,11,6,7,9], "vBars": [3,4,2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 16
1,700,922,600
biweekly-contest-118-minimum-number-of-coins-for-fruits
https://leetcode.com/problems/minimum-number-of-coins-for-fruits
minimum-number-of-coins-for-fruits
{ "questionId": "3209", "questionFrontendId": "2944", "title": "Minimum Number of Coins for Fruits", "titleSlug": "minimum-number-of-coins-for-fruits", "isPaidOnly": false, "difficulty": "Medium", "likes": 141, "dislikes": 27, "categoryTitle": "Algorithms" }
""" 你在一个水果超市里,货架上摆满了玲琅满目的奇珍异果。 给你一个下标从 1开始的数组prices,其中prices[i]表示你购买第 i个水果需要花费的金币数目。 水果超市有如下促销活动: 如果你花费 price[i]购买了水果i,那么接下来的 i个水果你都可以免费获得。 注意,即使你可以免费获得水果j,你仍然可以花费prices[j]个金币去购买它以便能免费获得接下来的 j个水果。 请你返回获得所有水果所需要的 最少金币数。 示例 1: 输入:prices = [3,1,2] 输出:4 解释:你可以按如下方法获得所有水果: - 花 3 个金币购买水果 1 ,然后免费获得水果 2 。 - 花 1 个金币购买水果 2 ,然后免费获得水果 3 。 - 免费获得水果 3 。 注意,虽然你可以免费获得水果 2 ,但你还是花 1 个金币去购买它,因为这样的总花费最少。 购买所有水果需要最少花费 4 个金币。 示例 2: 输入:prices = [1,10,1,1] 输出:2 解释:你可以按如下方法获得所有水果: - 花 1 个金币购买水果 1 ,然后免费获得水果 2 。 - 免费获得水果 2 。 - 花 1 个金币购买水果 3 ,然后免费获得水果 4 。 - 免费获得水果 4 。 购买所有水果需要最少花费 2 个金币。 提示: 1 <= prices.length <= 1000 1 <= prices[i] <= 105 """ class Solution: def minimumCoins(self, prices: List[int]) -> int:
你在一个水果超市里,货架上摆满了玲琅满目的奇珍异果。 给你一个下标从 1开始的数组prices,其中prices[i]表示你购买第 i个水果需要花费的金币数目。 水果超市有如下促销活动: 如果你花费 price[i]购买了水果i,那么接下来的 i个水果你都可以免费获得。 注意,即使你可以免费获得水果j,你仍然可以花费prices[j]个金币去购买它以便能免费获得接下来的 j个水果。 请你返回获得所有水果所需要的 最少金币数。 示例 1: 输入:prices = [3,1,2] 输出:4 解释:你可以按如下方法获得所有水果: - 花 3 个金币购买水果 1 ,然后免费获得水果 2 。 - 花 1 个金币购买水果 2 ,然后免费获得水果 3 。 - 免费获得水果 3 。 注意,虽然你可以免费获得水果 2 ,但你还是花 1 个金币去购买它,因为这样的总花费最少。 购买所有水果需要最少花费 4 个金币。 示例 2: 输入:prices = [1,10,1,1] 输出:2 解释:你可以按如下方法获得所有水果: - 花 1 个金币购买水果 1 ,然后免费获得水果 2 。 - 免费获得水果 2 。 - 花 1 个金币购买水果 3 ,然后免费获得水果 4 。 - 免费获得水果 4 。 购买所有水果需要最少花费 2 个金币。 提示: 1 <= prices.length <= 1000 1 <= prices[i] <= 105 请完成下面的代码来解决上述问题: ```python class Solution: def minimumCoins(self, prices: List[int]) -> int: ```
my_solution = Solution() test_input = { "prices": [3,1,2] } assert my_solution.minimumCoins(**test_input) == 4 test_input = { "prices": [1,10,1,1] } assert my_solution.minimumCoins(**test_input) == 2 test_input = { "prices": [26,18,6,12,49,7,45,45] } assert my_solution.minimumCoins(**test_input) == 39 test_input = { "prices": [27,17,29,45,3,39,42,26] } assert my_solution.minimumCoins(**test_input) == 47 test_input = { "prices": [14,37,37,38,24,15,12] } assert my_solution.minimumCoins(**test_input) == 63 test_input = { "prices": [1,37,19,38,11,42,18,33,6,37,15,48,23,12,41,18,27,32] } assert my_solution.minimumCoins(**test_input) == 37 test_input = { "prices": [38,23,27,32,47,45,48,24,39,26,37,42,24,45,27,26,15,16,26,6] } assert my_solution.minimumCoins(**test_input) == 132 test_input = { "prices": [45,44,5,9,22,14,29,14,21,13,45,10,2,16,14,30,26,1,49] } assert my_solution.minimumCoins(**test_input) == 66 test_input = { "prices": [37,42,6,50,50,38,30,38,1,13,25,39,18,1,35,32,12] } assert my_solution.minimumCoins(**test_input) == 74 test_input = { "prices": [17,32,11,25,22] } assert my_solution.minimumCoins(**test_input) == 28 test_input = { "prices": [18,10,1,11,6,30,19,24,1,18,37,29,28,27,38] } assert my_solution.minimumCoins(**test_input) == 26 test_input = { "prices": [3,10,25,47,49,10,49] } assert my_solution.minimumCoins(**test_input) == 38 test_input = { "prices": [46,7,15] } assert my_solution.minimumCoins(**test_input) == 53 test_input = { "prices": [16,45,25,5,18,19,25,13,33] } assert my_solution.minimumCoins(**test_input) == 59 test_input = { "prices": [21,16,7,10,30] } assert my_solution.minimumCoins(**test_input) == 28 test_input = { "prices": [21,22,29,37,23,15,39,9,19,10,6,9,33,28,43] } assert my_solution.minimumCoins(**test_input) == 71 test_input = { "prices": [37,16,42,47,16,31,39,8,26,50,33] } assert my_solution.minimumCoins(**test_input) == 77 test_input = { "prices": [32,4] } assert my_solution.minimumCoins(**test_input) == 32 test_input = { "prices": [31,9,2,36,4,45,28,28,12,22,44,17,10,48,15,22,7,14,41] } assert my_solution.minimumCoins(**test_input) == 56 test_input = { "prices": [1,31,9,36,44,2,23] } assert my_solution.minimumCoins(**test_input) == 12 test_input = { "prices": [3,7,2,36,33,7,21,40,19] } assert my_solution.minimumCoins(**test_input) == 12 test_input = { "prices": [9,4,7,29,22,50] } assert my_solution.minimumCoins(**test_input) == 16 test_input = { "prices": [28,2,40,15] } assert my_solution.minimumCoins(**test_input) == 30 test_input = { "prices": [16,17,47,20,18,37] } assert my_solution.minimumCoins(**test_input) == 51 test_input = { "prices": [42,6,44,47,11,6,30,38,41,43,46,35,28,4,47,1,7,35] } assert my_solution.minimumCoins(**test_input) == 93 test_input = { "prices": [10,10,5,8,5,13,34,31,36] } assert my_solution.minimumCoins(**test_input) == 20 test_input = { "prices": [12,20,14,46,22,1,42,50,47,47,38,37,13] } assert my_solution.minimumCoins(**test_input) == 40 test_input = { "prices": [1,38,28,46,18,22,12,7,44,44,40,36,41,5,33,5,30,33,31] } assert my_solution.minimumCoins(**test_input) == 46 test_input = { "prices": [6,45,2,29,44,14,44] } assert my_solution.minimumCoins(**test_input) == 22 test_input = { "prices": [34,13,50,42,24,47,41,8,26,34,3,48,39,24,39,26,46] } assert my_solution.minimumCoins(**test_input) == 74 test_input = { "prices": [47,9,33,6,33,40,28,37,49,39,45,14,13,40,17,14,39,12,15,6] } assert my_solution.minimumCoins(**test_input) == 103 test_input = { "prices": [32] } assert my_solution.minimumCoins(**test_input) == 32 test_input = { "prices": [35,46,50,35,11,14,44,17,45,23,34,33,8,27,19,7,10,12,14] } assert my_solution.minimumCoins(**test_input) == 107 test_input = { "prices": [50,45,14,24,18,15,9,14] } assert my_solution.minimumCoins(**test_input) == 73 test_input = { "prices": [38,19,18,15,20,43,18,9,44,26,29] } assert my_solution.minimumCoins(**test_input) == 74 test_input = { "prices": [26,21,7,40,37,44,13,3,10,9,15,12,30,18,31,10,23] } assert my_solution.minimumCoins(**test_input) == 55 test_input = { "prices": [36,50] } assert my_solution.minimumCoins(**test_input) == 36 test_input = { "prices": [1,32,48,36,26,5,30,25,2,17,26,39,17,46,34,47,43,45,20,48] } assert my_solution.minimumCoins(**test_input) == 71 test_input = { "prices": [19,24,31,24] } assert my_solution.minimumCoins(**test_input) == 43 test_input = { "prices": [1,18,25,29,17,9,3,29,23,17,18] } assert my_solution.minimumCoins(**test_input) == 29 test_input = { "prices": [18,36,18,44,30,8,42,33,45,19,50,19,24,48] } assert my_solution.minimumCoins(**test_input) == 63 test_input = { "prices": [26,25,47,36,9,31,1,29,29,42,29,42,36,19,45,4,11,7] } assert my_solution.minimumCoins(**test_input) == 80 test_input = { "prices": [24,34,47,12,24,48,14,30,28,43,35,45,11,11,35,38] } assert my_solution.minimumCoins(**test_input) == 95 test_input = { "prices": [29,18,2,6,47,32,27,12,38,17] } assert my_solution.minimumCoins(**test_input) == 49 test_input = { "prices": [3,31,15,18,47,18,2,27,24,6,36,35,41,21,30] } assert my_solution.minimumCoins(**test_input) == 26 test_input = { "prices": [29,45,8,45,23,35] } assert my_solution.minimumCoins(**test_input) == 37 test_input = { "prices": [39,37] } assert my_solution.minimumCoins(**test_input) == 39 test_input = { "prices": [18,45,6,14,41,41] } assert my_solution.minimumCoins(**test_input) == 24 test_input = { "prices": [50,21,38,2,32,49,32,40,41,34,33,40,36,16,29,34,42,40,46] } assert my_solution.minimumCoins(**test_input) == 121 test_input = { "prices": [28,17,42,20,6,26,47,6,23] } assert my_solution.minimumCoins(**test_input) == 51 test_input = { "prices": [37,27,17,40,50,35,16,4,28,5,27,13,46,7,23,27] } assert my_solution.minimumCoins(**test_input) == 74 test_input = { "prices": [11,5,40,16,20,38] } assert my_solution.minimumCoins(**test_input) == 32 test_input = { "prices": [16,27,5,38,12,24,7,49,40,13,38,13,34,38,37,2,4,44] } assert my_solution.minimumCoins(**test_input) == 41 test_input = { "prices": [25,9,49,19,33] } assert my_solution.minimumCoins(**test_input) == 53 test_input = { "prices": [47,23,46,13,26,44,43,22,43,24,13,20,6,16,8,26] } assert my_solution.minimumCoins(**test_input) == 105 test_input = { "prices": [8,1,1] } assert my_solution.minimumCoins(**test_input) == 9 test_input = { "prices": [47,45,2,25,7,46] } assert my_solution.minimumCoins(**test_input) == 49 test_input = { "prices": [4,31,50,45,5,50] } assert my_solution.minimumCoins(**test_input) == 40 test_input = { "prices": [30,41,1,49,9,49,41,27,41,14,23,3,46,40,37,28,45,19,36,49] } assert my_solution.minimumCoins(**test_input) == 54 test_input = { "prices": [6,3,49,28,31,36,5,50,39] } assert my_solution.minimumCoins(**test_input) == 40 test_input = { "prices": [37,2,19,36,26,27,3,23,10,20,33,8,39,6,28] } assert my_solution.minimumCoins(**test_input) == 65 test_input = { "prices": [37,34,12,30,43,35,6,21,47,38,14,31,49,11,14] } assert my_solution.minimumCoins(**test_input) == 66 test_input = { "prices": [49,6,12,35,17,17,2] } assert my_solution.minimumCoins(**test_input) == 63 test_input = { "prices": [45,27,43,34,41,30,28,45,24,50,20,4,15,42] } assert my_solution.minimumCoins(**test_input) == 116 test_input = { "prices": [48,22,36] } assert my_solution.minimumCoins(**test_input) == 70 test_input = { "prices": [47,13,23,31,41,25] } assert my_solution.minimumCoins(**test_input) == 70 test_input = { "prices": [3,44,17,37,9,14,37] } assert my_solution.minimumCoins(**test_input) == 29 test_input = { "prices": [4,43,7,15,38] } assert my_solution.minimumCoins(**test_input) == 11 test_input = { "prices": [10,25,7,37,6,43,4,50,9,14,36,35,36,44,17,10,44,46,50] } assert my_solution.minimumCoins(**test_input) == 35 test_input = { "prices": [45,28,10,18,18,3,42,24,14,11,13,32,37,31,50,32] } assert my_solution.minimumCoins(**test_input) == 69 test_input = { "prices": [12,38,44,24,42,9,32,40,8,20,46,39,33] } assert my_solution.minimumCoins(**test_input) == 73 test_input = { "prices": [5,42,30,20,37,26,38,30,30,32,39,31,33,41,23,4,29] } assert my_solution.minimumCoins(**test_input) == 85 test_input = { "prices": [44,22] } assert my_solution.minimumCoins(**test_input) == 44 test_input = { "prices": [8,8,11,21,9] } assert my_solution.minimumCoins(**test_input) == 19 test_input = { "prices": [2,37,19,30,37,27,10,37] } assert my_solution.minimumCoins(**test_input) == 31 test_input = { "prices": [43,27,48,22] } assert my_solution.minimumCoins(**test_input) == 70 test_input = { "prices": [50,23,37,49,45,14,18,39,50,7,31] } assert my_solution.minimumCoins(**test_input) == 101 test_input = { "prices": [37,3,32,25,21,22,26,49,14,45,1,36] } assert my_solution.minimumCoins(**test_input) == 62 test_input = { "prices": [21,29,31,28,2,41,4,43,41,16,38,33,3,6,43,22,15] } assert my_solution.minimumCoins(**test_input) == 59 test_input = { "prices": [24,47,32,41,35,14,18,23,27,8,27] } assert my_solution.minimumCoins(**test_input) == 70 test_input = { "prices": [40,25,32] } assert my_solution.minimumCoins(**test_input) == 65 test_input = { "prices": [9,18,2,26,15,3,2,33,46,6,11,34,27,7,5,7,26,13,48] } assert my_solution.minimumCoins(**test_input) == 18 test_input = { "prices": [41,8,38,32,36,30,23,49,40,46,42,34,2,12,12,19,20,50,40] } assert my_solution.minimumCoins(**test_input) == 104 test_input = { "prices": [28,8,24,14,34,36,48] } assert my_solution.minimumCoins(**test_input) == 50 test_input = { "prices": [2,36,22,41,42,26,1,48,14,27,22,26] } assert my_solution.minimumCoins(**test_input) == 25 test_input = { "prices": [25,39,21,21,16] } assert my_solution.minimumCoins(**test_input) == 46 test_input = { "prices": [44,41,36,42,21,32,45,5] } assert my_solution.minimumCoins(**test_input) == 101 test_input = { "prices": [10] } assert my_solution.minimumCoins(**test_input) == 10 test_input = { "prices": [35,29] } assert my_solution.minimumCoins(**test_input) == 35 test_input = { "prices": [38,35,33,11,43,33] } assert my_solution.minimumCoins(**test_input) == 71 test_input = { "prices": [19,29,49,20,8,12,13,28,45,9,12,3,1,17,35] } assert my_solution.minimumCoins(**test_input) == 65 test_input = { "prices": [34,22,1,41,34,27,18] } assert my_solution.minimumCoins(**test_input) == 53 test_input = { "prices": [31,9,39,6,14,32,28,35,34,42,19,41,35,24,32,16,12,49,16] } assert my_solution.minimumCoins(**test_input) == 73 test_input = { "prices": [46,50,21,14,26,47] } assert my_solution.minimumCoins(**test_input) == 67 test_input = { "prices": [5,22,36,6,15,49,20,16,36,15,32,27,50,19,12,22,9,33] } assert my_solution.minimumCoins(**test_input) == 57 test_input = { "prices": [17,4,36,4,32,11,42,12,20] } assert my_solution.minimumCoins(**test_input) == 36 test_input = { "prices": [48,33,39,1,13,40] } assert my_solution.minimumCoins(**test_input) == 82 test_input = { "prices": [32,19,33,30,32,44,47,8,10,1,23,6,28,19,20,48,12,10,20,22] } assert my_solution.minimumCoins(**test_input) == 84 test_input = { "prices": [29,5,46,34,38,7,1,15] } assert my_solution.minimumCoins(**test_input) == 68 test_input = { "prices": [39,50,22,1,38,22,49,16,27,48,45,28,43,34] } assert my_solution.minimumCoins(**test_input) == 78
1,700,922,600
biweekly-contest-118-find-maximum-non-decreasing-array-length
https://leetcode.com/problems/find-maximum-non-decreasing-array-length
find-maximum-non-decreasing-array-length
{ "questionId": "3211", "questionFrontendId": "2945", "title": "Find Maximum Non-decreasing Array Length", "titleSlug": "find-maximum-non-decreasing-array-length", "isPaidOnly": false, "difficulty": "Hard", "likes": 90, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的整数数组nums。 你可以执行任意次操作。每次操作中,你需要选择一个 子数组,并将这个子数组用它所包含元素的 和替换。比方说,给定数组是[1,3,5,6],你可以选择子数组[3,5],用子数组的和 8替换掉子数组,然后数组会变为[1,8,6]。 请你返回执行任意次操作以后,可以得到的 最长非递减数组的长度。 子数组指的是一个数组中一段连续 非空的元素序列。 示例 1: 输入:nums = [5,2,2] 输出:1 解释:这个长度为 3 的数组不是非递减的。 我们有 2 种方案使数组长度为 2 。 第一种,选择子数组 [2,2] ,对数组执行操作后得到 [5,4] 。 第二种,选择子数组 [5,2] ,对数组执行操作后得到 [7,2] 。 这两种方案中,数组最后都不是 非递减的,所以不是可行的答案。 如果我们选择子数组 [5,2,2] ,并将它替换为 [9] ,数组变成非递减的。 所以答案为 1 。 示例 2: 输入:nums = [1,2,3,4] 输出:4 解释:数组已经是非递减的。所以答案为 4 。 示例 3: 输入:nums = [4,3,2,6] 输出:3 解释:将 [3,2] 替换为 [5] ,得到数组 [4,5,6] ,它是非递减的。 最大可能的答案为 3 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 105 """ class Solution: def findMaximumLength(self, nums: List[int]) -> int:
给你一个下标从 0开始的整数数组nums。 你可以执行任意次操作。每次操作中,你需要选择一个 子数组,并将这个子数组用它所包含元素的 和替换。比方说,给定数组是[1,3,5,6],你可以选择子数组[3,5],用子数组的和 8替换掉子数组,然后数组会变为[1,8,6]。 请你返回执行任意次操作以后,可以得到的 最长非递减数组的长度。 子数组指的是一个数组中一段连续 非空的元素序列。 示例 1: 输入:nums = [5,2,2] 输出:1 解释:这个长度为 3 的数组不是非递减的。 我们有 2 种方案使数组长度为 2 。 第一种,选择子数组 [2,2] ,对数组执行操作后得到 [5,4] 。 第二种,选择子数组 [5,2] ,对数组执行操作后得到 [7,2] 。 这两种方案中,数组最后都不是 非递减的,所以不是可行的答案。 如果我们选择子数组 [5,2,2] ,并将它替换为 [9] ,数组变成非递减的。 所以答案为 1 。 示例 2: 输入:nums = [1,2,3,4] 输出:4 解释:数组已经是非递减的。所以答案为 4 。 示例 3: 输入:nums = [4,3,2,6] 输出:3 解释:将 [3,2] 替换为 [5] ,得到数组 [4,5,6] ,它是非递减的。 最大可能的答案为 3 。 提示: 1 <= nums.length <= 105 1 <= nums[i] <= 105 请完成下面的代码来解决上述问题: ```python class Solution: def findMaximumLength(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [5,2,2] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [1,2,3,4] } assert my_solution.findMaximumLength(**test_input) == 4 test_input = { "nums": [4,3,2,6] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "nums": [32] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [38] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [60] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [79] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [85] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [170] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [198] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [220] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [318] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [350] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [381] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [413] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [426] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [429] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [431] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [445] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [488] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [492] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [497] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [515] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [582] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [589] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [620] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [632] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [703] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [748] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [776] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [977] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [986] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [990] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [29,859] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [48,612] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [76,837] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [103,341] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [171,323] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [248,719] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [253,61] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [274,467] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [351,665] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [372,382] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [404,409] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [455,40] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [472,843] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [504,838] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [549,747] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [570,810] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [621,809] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [643,802] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [689,192] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [709,481] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [742,67] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [745,725] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [773,877] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [776,962] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [791,434] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [824,783] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [840,388] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [876,264] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [940,694] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [959,372] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [981,998] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [41,340,784] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "nums": [103,652,579] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [116,635,966] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "nums": [137,32,745] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [247,173,316] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [276,315,947] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "nums": [278,754,912] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "nums": [314,882,708] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [371,101,367] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [402,305,990] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [403,553,697] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "nums": [431,780,315] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [479,322,44] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [512,234,679] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [513,847,778] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [525,177,936] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [588,42,18] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [646,174,827] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [680,242,726] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [769,131,241] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [780,591,213] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [783,23,848] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [787,201,30] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [791,470,87] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [797,181,492] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [868,4,455] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [881,306,316] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [886,116,68] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [893,531,805] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [926,641,145] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [964,624,279] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [987,694,396] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [38,986,134,156] } assert my_solution.findMaximumLength(**test_input) == 2 test_input = { "nums": [43,236,417,521] } assert my_solution.findMaximumLength(**test_input) == 4 test_input = { "nums": [58,890,892,52] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "nums": [81,738,403,654] } assert my_solution.findMaximumLength(**test_input) == 3
1,700,922,600
weekly-contest-372-make-three-strings-equal
https://leetcode.com/problems/make-three-strings-equal/
make-three-strings-equal
{ "questionId": "3207", "questionFrontendId": "2937", "title": "Make Three Strings Equal", "titleSlug": "make-three-strings-equal", "isPaidOnly": false, "difficulty": "Easy", "likes": 114, "dislikes": 30, "categoryTitle": "Algorithms" }
""" 给你三个字符串 s1、s2 和 s3。 你可以根据需要对这三个字符串执行以下操作 任意次数 。 在每次操作中,你可以选择其中一个长度至少为 2 的字符串 并删除其 最右位置上 的字符。 如果存在某种方法能够使这三个字符串相等,请返回使它们相等所需的 最小 操作次数;否则,返回 -1。 示例 1: 输入:s1 = "abc",s2 = "abb",s3 = "ab" 输出:2 解释:对 s1 和 s2 进行一次操作后,可以得到三个相等的字符串。 可以证明,不可能用少于两次操作使它们相等。 示例 2: 输入:s1 = "dac",s2 = "bac",s3 = "cac" 输出:-1 解释:因为 s1 和 s2 的最左位置上的字母不相等,所以无论进行多少次操作,它们都不可能相等。因此答案是 -1 。 提示: 1 <= s1.length, s2.length, s3.length <= 100 s1、s2 和 s3 仅由小写英文字母组成。 """ class Solution: def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
给你三个字符串 s1、s2 和 s3。 你可以根据需要对这三个字符串执行以下操作 任意次数 。 在每次操作中,你可以选择其中一个长度至少为 2 的字符串 并删除其 最右位置上 的字符。 如果存在某种方法能够使这三个字符串相等,请返回使它们相等所需的 最小 操作次数;否则,返回 -1。 示例 1: 输入:s1 = "abc",s2 = "abb",s3 = "ab" 输出:2 解释:对 s1 和 s2 进行一次操作后,可以得到三个相等的字符串。 可以证明,不可能用少于两次操作使它们相等。 示例 2: 输入:s1 = "dac",s2 = "bac",s3 = "cac" 输出:-1 解释:因为 s1 和 s2 的最左位置上的字母不相等,所以无论进行多少次操作,它们都不可能相等。因此答案是 -1 。 提示: 1 <= s1.length, s2.length, s3.length <= 100 s1、s2 和 s3 仅由小写英文字母组成。 请完成下面的代码来解决上述问题: ```python class Solution: def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int: ```
my_solution = Solution() test_input = { "s1": "abc", "s2": "abb", "s3": "ab" } assert my_solution.findMinimumOperations(**test_input) == 2 test_input = { "s1": "dac", "s2": "bac", "s3": "cac" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "a", "s2": "a", "s3": "a" } assert my_solution.findMinimumOperations(**test_input) == 0 test_input = { "s1": "kui", "s2": "m", "s3": "v" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "a", "s2": "aabc", "s3": "a" } assert my_solution.findMinimumOperations(**test_input) == 3 test_input = { "s1": "cc", "s2": "cccb", "s3": "c" } assert my_solution.findMinimumOperations(**test_input) == 4 test_input = { "s1": "luso", "s2": "lu", "s3": "lu" } assert my_solution.findMinimumOperations(**test_input) == 2 test_input = { "s1": "xx", "s2": "phe", "s3": "xie" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "gzd", "s2": "bcju", "s3": "db" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "cbba", "s2": "cbaa", "s3": "c" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "k", "s2": "kfb", "s3": "krcnf" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "oby", "s2": "obz", "s3": "obf" } assert my_solution.findMinimumOperations(**test_input) == 3 test_input = { "s1": "b", "s2": "aba", "s3": "aaccaa" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "a", "s2": "accabb", "s3": "aaa" } assert my_solution.findMinimumOperations(**test_input) == 7 test_input = { "s1": "b", "s2": "bccaaba", "s3": "ba" } assert my_solution.findMinimumOperations(**test_input) == 7 test_input = { "s1": "b", "s2": "bacccab", "s3": "cc" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "ca", "s2": "cccabb", "s3": "cb" } assert my_solution.findMinimumOperations(**test_input) == 7 test_input = { "s1": "ccb", "s2": "ccba", "s3": "ccb" } assert my_solution.findMinimumOperations(**test_input) == 1 test_input = { "s1": "mbooi", "s2": "pdq", "s3": "br" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "xxfzj", "s2": "faho", "s3": "c" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "acbc", "s2": "acba", "s3": "acb" } assert my_solution.findMinimumOperations(**test_input) == 2 test_input = { "s1": "aduyyk", "s2": "v", "s3": "lpyt" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "xd", "s2": "sl", "s3": "azoeaje" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "bbbaa", "s2": "bacab", "s3": "b" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "cibn", "s2": "ioom", "s3": "bxa" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "bcb", "s2": "bbac", "s3": "cbbc" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "aabbb", "s2": "cc", "s3": "cccb" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "i", "s2": "xqsfy", "s3": "diqae" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "iq", "s2": "iimanmy", "s3": "id" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "pitggt", "s2": "pi", "s3": "pih" } assert my_solution.findMinimumOperations(**test_input) == 5 test_input = { "s1": "ten", "s2": "ten", "s3": "tenob" } assert my_solution.findMinimumOperations(**test_input) == 2 test_input = { "s1": "vejy", "s2": "fbqfo", "s3": "gl" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "aca", "s2": "abcc", "s3": "accba" } assert my_solution.findMinimumOperations(**test_input) == 9 test_input = { "s1": "br", "s2": "br", "s3": "brvhgtou" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "c", "s2": "bcc", "s3": "aacbcaca" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "cab", "s2": "caac", "s3": "cacbb" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "ccab", "s2": "cbbcbb", "s3": "ca" } assert my_solution.findMinimumOperations(**test_input) == 9 test_input = { "s1": "inuc", "s2": "iwpdfj", "s3": "ib" } assert my_solution.findMinimumOperations(**test_input) == 9 test_input = { "s1": "jrrpyyc", "s2": "jr", "s3": "jrt" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "l", "s2": "gebqrgnz", "s3": "jkr" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "naynn", "s2": "ax", "s3": "bhdcz" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "rexmx", "s2": "ujmbg", "s3": "gg" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "aacbcb", "s2": "a", "s3": "acaaac" } assert my_solution.findMinimumOperations(**test_input) == 10 test_input = { "s1": "acbb", "s2": "acbacc", "s3": "acb" } assert my_solution.findMinimumOperations(**test_input) == 4 test_input = { "s1": "baacbab", "s2": "bcc", "s3": "bca" } assert my_solution.findMinimumOperations(**test_input) == 10 test_input = { "s1": "bcacbba", "s2": "bca", "s3": "bca" } assert my_solution.findMinimumOperations(**test_input) == 4 test_input = { "s1": "bcaca", "s2": "bcaba", "s3": "bca" } assert my_solution.findMinimumOperations(**test_input) == 4 test_input = { "s1": "ba", "s2": "bcbcab", "s3": "bbcbb" } assert my_solution.findMinimumOperations(**test_input) == 10 test_input = { "s1": "cabc", "s2": "cab", "s3": "cabbac" } assert my_solution.findMinimumOperations(**test_input) == 4 test_input = { "s1": "bbbbcaac", "s2": "a", "s3": "cbcc" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "gjbtodtym", "s2": "gxp", "s3": "g" } assert my_solution.findMinimumOperations(**test_input) == 10 test_input = { "s1": "hfkq", "s2": "hfrbvno", "s3": "hf" } assert my_solution.findMinimumOperations(**test_input) == 7 test_input = { "s1": "hym", "s2": "hl", "s3": "hshxmbbj" } assert my_solution.findMinimumOperations(**test_input) == 10 test_input = { "s1": "mkdflu", "s2": "mmbn", "s3": "mge" } assert my_solution.findMinimumOperations(**test_input) == 10 test_input = { "s1": "nvlobl", "s2": "mekbzd", "s3": "s" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "rpa", "s2": "rpaxpoh", "s3": "rpa" } assert my_solution.findMinimumOperations(**test_input) == 4 test_input = { "s1": "ac", "s2": "aacccccbc", "s3": "acc" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "abb", "s2": "abaac", "s3": "abcaca" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "caa", "s2": "caccaccacb", "s3": "c" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "baccaab", "s2": "cababc", "s3": "a" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "cacbb", "s2": "ca", "s3": "cacbcac" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "cbba", "s2": "cabcabab", "s3": "ca" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "cb", "s2": "cbcbb", "s3": "cbaaabb" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "ccabaa", "s2": "ccabc", "s3": "cca" } assert my_solution.findMinimumOperations(**test_input) == 5 test_input = { "s1": "ccb", "s2": "ccac", "s3": "cccaaca" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "ccccbb", "s2": "cccc", "s3": "cccc" } assert my_solution.findMinimumOperations(**test_input) == 2 test_input = { "s1": "cxxq", "s2": "cxx", "s3": "cxxdeqh" } assert my_solution.findMinimumOperations(**test_input) == 5 test_input = { "s1": "d", "s2": "dffjiulzya", "s3": "dke" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "dogv", "s2": "dofjkhx", "s3": "dog" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "dwefrocz", "s2": "dzz", "s3": "dwn" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "etr", "s2": "ejb", "s3": "etpubpvr" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "f", "s2": "morycy", "s3": "vledqoo" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "ful", "s2": "fular", "s3": "fulvkv" } assert my_solution.findMinimumOperations(**test_input) == 5 test_input = { "s1": "kzwat", "s2": "ku", "s3": "koftvbd" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "qey", "s2": "qevtkbss", "s3": "qeb" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "saqy", "s2": "hvufcpko", "s3": "xm" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "tllwgcdi", "s2": "t", "s3": "tvham" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "vmwdmadn", "s2": "vmw", "s3": "vmw" } assert my_solution.findMinimumOperations(**test_input) == 5 test_input = { "s1": "xobwwhu", "s2": "xobb", "s3": "xob" } assert my_solution.findMinimumOperations(**test_input) == 5 test_input = { "s1": "yptajimiz", "s2": "yp", "s3": "ypr" } assert my_solution.findMinimumOperations(**test_input) == 8 test_input = { "s1": "z", "s2": "zcrouxlukb", "s3": "zbb" } assert my_solution.findMinimumOperations(**test_input) == 11 test_input = { "s1": "aaabc", "s2": "aaaa", "s3": "aaaabc" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "aaa", "s2": "aab", "s3": "aabaacaab" } assert my_solution.findMinimumOperations(**test_input) == 9 test_input = { "s1": "aac", "s2": "aac", "s3": "aacabbbca" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "abaab", "s2": "abaabb", "s3": "abaa" } assert my_solution.findMinimumOperations(**test_input) == 3 test_input = { "s1": "abcomon", "s2": "gkuneup", "s3": "q" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "acc", "s2": "accacb", "s3": "acbabc" } assert my_solution.findMinimumOperations(**test_input) == 9 test_input = { "s1": "cca", "s2": "caaab", "s3": "babbacc" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "bccbc", "s2": "bc", "s3": "bcccbcac" } assert my_solution.findMinimumOperations(**test_input) == 9 test_input = { "s1": "bpp", "s2": "bin", "s3": "bfkbyhubw" } assert my_solution.findMinimumOperations(**test_input) == 12 test_input = { "s1": "bbsyg", "s2": "blbp", "s3": "brghkr" } assert my_solution.findMinimumOperations(**test_input) == 12 test_input = { "s1": "bxpvamp", "s2": "bxpv", "s3": "bxpv" } assert my_solution.findMinimumOperations(**test_input) == 3 test_input = { "s1": "ccbabca", "s2": "cbcbaca", "s3": "c" } assert my_solution.findMinimumOperations(**test_input) == 12 test_input = { "s1": "accb", "s2": "bbc", "s3": "cbbaccba" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "cbccc", "s2": "cbc", "s3": "cbcccba" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "cac", "s2": "ccacc", "s3": "cabacba" } assert my_solution.findMinimumOperations(**test_input) == 12 test_input = { "s1": "ccb", "s2": "ccbcb", "s3": "ccbccab" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "caacabcbc", "s2": "ccb", "s3": "ccc" } assert my_solution.findMinimumOperations(**test_input) == 12 test_input = { "s1": "cccabaacc", "s2": "ccc", "s3": "ccc" } assert my_solution.findMinimumOperations(**test_input) == 6 test_input = { "s1": "ajjdre", "s2": "gsrq", "s3": "eurcj" } assert my_solution.findMinimumOperations(**test_input) == -1
1,700,361,000
weekly-contest-372-separate-black-and-white-balls
https://leetcode.com/problems/separate-black-and-white-balls
separate-black-and-white-balls
{ "questionId": "3195", "questionFrontendId": "2938", "title": "Separate Black and White Balls", "titleSlug": "separate-black-and-white-balls", "isPaidOnly": false, "difficulty": "Medium", "likes": 133, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 桌子上有 n 个球,每个球的颜色不是黑色,就是白色。 给你一个长度为 n 、下标从 0 开始的二进制字符串 s,其中 1 和 0 分别代表黑色和白色的球。 在每一步中,你可以选择两个相邻的球并交换它们。 返回「将所有黑色球都移到右侧,所有白色球都移到左侧所需的 最小步数」。 示例 1: 输入:s = "101" 输出:1 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "011"。 最开始,1 没有都在右侧,需要至少 1 步将其移到右侧。 示例 2: 输入:s = "100" 输出:2 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "010"。 - 交换 s[1] 和 s[2],s = "001"。 可以证明所需的最小步数为 2 。 示例 3: 输入:s = "0111" 输出:0 解释:所有黑色球都已经在右侧。 提示: 1 <= n == s.length <= 105 s[i] 不是 '0',就是 '1'。 """ class Solution: def minimumSteps(self, s: str) -> int:
桌子上有 n 个球,每个球的颜色不是黑色,就是白色。 给你一个长度为 n 、下标从 0 开始的二进制字符串 s,其中 1 和 0 分别代表黑色和白色的球。 在每一步中,你可以选择两个相邻的球并交换它们。 返回「将所有黑色球都移到右侧,所有白色球都移到左侧所需的 最小步数」。 示例 1: 输入:s = "101" 输出:1 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "011"。 最开始,1 没有都在右侧,需要至少 1 步将其移到右侧。 示例 2: 输入:s = "100" 输出:2 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "010"。 - 交换 s[1] 和 s[2],s = "001"。 可以证明所需的最小步数为 2 。 示例 3: 输入:s = "0111" 输出:0 解释:所有黑色球都已经在右侧。 提示: 1 <= n == s.length <= 105 s[i] 不是 '0',就是 '1'。 请完成下面的代码来解决上述问题: ```python class Solution: def minimumSteps(self, s: str) -> int: ```
my_solution = Solution() test_input = { "s": "101" } assert my_solution.minimumSteps(**test_input) == 1 test_input = { "s": "100" } assert my_solution.minimumSteps(**test_input) == 2 test_input = { "s": "0111" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "11000111" } assert my_solution.minimumSteps(**test_input) == 6 test_input = { "s": "01010001" } assert my_solution.minimumSteps(**test_input) == 7 test_input = { "s": "0100101" } assert my_solution.minimumSteps(**test_input) == 4 test_input = { "s": "111111111100100010" } assert my_solution.minimumSteps(**test_input) == 65 test_input = { "s": "10100000110010011010" } assert my_solution.minimumSteps(**test_input) == 44 test_input = { "s": "1101110000111011110" } assert my_solution.minimumSteps(**test_input) == 42 test_input = { "s": "01000010111010001" } assert my_solution.minimumSteps(**test_input) == 29 test_input = { "s": "11110" } assert my_solution.minimumSteps(**test_input) == 4 test_input = { "s": "010001001011010" } assert my_solution.minimumSteps(**test_input) == 21 test_input = { "s": "0011011" } assert my_solution.minimumSteps(**test_input) == 2 test_input = { "s": "001" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "000100100" } assert my_solution.minimumSteps(**test_input) == 6 test_input = { "s": "00110" } assert my_solution.minimumSteps(**test_input) == 2 test_input = { "s": "001110001110001" } assert my_solution.minimumSteps(**test_input) == 27 test_input = { "s": "10000000001" } assert my_solution.minimumSteps(**test_input) == 9 test_input = { "s": "01" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "0100011100001100100" } assert my_solution.minimumSteps(**test_input) == 45 test_input = { "s": "1010110" } assert my_solution.minimumSteps(**test_input) == 7 test_input = { "s": "010010000" } assert my_solution.minimumSteps(**test_input) == 10 test_input = { "s": "100110" } assert my_solution.minimumSteps(**test_input) == 5 test_input = { "s": "1100" } assert my_solution.minimumSteps(**test_input) == 4 test_input = { "s": "000110" } assert my_solution.minimumSteps(**test_input) == 2 test_input = { "s": "001101101111000000" } assert my_solution.minimumSteps(**test_input) == 54 test_input = { "s": "011101011" } assert my_solution.minimumSteps(**test_input) == 7 test_input = { "s": "0110111111110" } assert my_solution.minimumSteps(**test_input) == 12 test_input = { "s": "1111001111111011111" } assert my_solution.minimumSteps(**test_input) == 19 test_input = { "s": "0101111" } assert my_solution.minimumSteps(**test_input) == 1 test_input = { "s": "00010101100110011" } assert my_solution.minimumSteps(**test_input) == 23 test_input = { "s": "01010110110111011001" } assert my_solution.minimumSteps(**test_input) == 44 test_input = { "s": "0" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "1101111010111001010" } assert my_solution.minimumSteps(**test_input) == 58 test_input = { "s": "01100010" } assert my_solution.minimumSteps(**test_input) == 9 test_input = { "s": "10010000010" } assert my_solution.minimumSteps(**test_input) == 15 test_input = { "s": "11011101000100001" } assert my_solution.minimumSteps(**test_input) == 53 test_input = { "s": "00" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "10100101011001100111" } assert my_solution.minimumSteps(**test_input) == 40 test_input = { "s": "0101" } assert my_solution.minimumSteps(**test_input) == 1 test_input = { "s": "00101010100" } assert my_solution.minimumSteps(**test_input) == 14 test_input = { "s": "00110111110010" } assert my_solution.minimumSteps(**test_input) == 24 test_input = { "s": "111010000001000001" } assert my_solution.minimumSteps(**test_input) == 52 test_input = { "s": "0101101000111010" } assert my_solution.minimumSteps(**test_input) == 31 test_input = { "s": "1010111111" } assert my_solution.minimumSteps(**test_input) == 3 test_input = { "s": "110110001001110" } assert my_solution.minimumSteps(**test_input) == 32 test_input = { "s": "111010" } assert my_solution.minimumSteps(**test_input) == 7 test_input = { "s": "10" } assert my_solution.minimumSteps(**test_input) == 1 test_input = { "s": "111110" } assert my_solution.minimumSteps(**test_input) == 5 test_input = { "s": "0111100000101101110" } assert my_solution.minimumSteps(**test_input) == 42 test_input = { "s": "111010100" } assert my_solution.minimumSteps(**test_input) == 17 test_input = { "s": "0001000100111000" } assert my_solution.minimumSteps(**test_input) == 22 test_input = { "s": "011001111110110010" } assert my_solution.minimumSteps(**test_input) == 43 test_input = { "s": "11100" } assert my_solution.minimumSteps(**test_input) == 6 test_input = { "s": "0100001101110001" } assert my_solution.minimumSteps(**test_input) == 25 test_input = { "s": "111" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "100010" } assert my_solution.minimumSteps(**test_input) == 5 test_input = { "s": "101001" } assert my_solution.minimumSteps(**test_input) == 5 test_input = { "s": "00111010100100100111" } assert my_solution.minimumSteps(**test_input) == 43 test_input = { "s": "100010111" } assert my_solution.minimumSteps(**test_input) == 5 test_input = { "s": "001110000101011" } assert my_solution.minimumSteps(**test_input) == 21 test_input = { "s": "111011011101101" } assert my_solution.minimumSteps(**test_input) == 26 test_input = { "s": "1110101" } assert my_solution.minimumSteps(**test_input) == 7 test_input = { "s": "00111010100111" } assert my_solution.minimumSteps(**test_input) == 17 test_input = { "s": "010" } assert my_solution.minimumSteps(**test_input) == 1 test_input = { "s": "111001" } assert my_solution.minimumSteps(**test_input) == 6 test_input = { "s": "1100011" } assert my_solution.minimumSteps(**test_input) == 6 test_input = { "s": "0110001011001110111" } assert my_solution.minimumSteps(**test_input) == 27 test_input = { "s": "0001111111011101" } assert my_solution.minimumSteps(**test_input) == 17 test_input = { "s": "0110110100001" } assert my_solution.minimumSteps(**test_input) == 26 test_input = { "s": "11110110011000011" } assert my_solution.minimumSteps(**test_input) == 48 test_input = { "s": "10100" } assert my_solution.minimumSteps(**test_input) == 5 test_input = { "s": "10100001" } assert my_solution.minimumSteps(**test_input) == 9 test_input = { "s": "01000011000" } assert my_solution.minimumSteps(**test_input) == 13 test_input = { "s": "110001001001" } assert my_solution.minimumSteps(**test_input) == 20 test_input = { "s": "01100011010011110" } assert my_solution.minimumSteps(**test_input) == 29 test_input = { "s": "01111001111" } assert my_solution.minimumSteps(**test_input) == 8 test_input = { "s": "1110100010000110110" } assert my_solution.minimumSteps(**test_input) == 51 test_input = { "s": "1101000111001111100" } assert my_solution.minimumSteps(**test_input) == 45 test_input = { "s": "0110000" } assert my_solution.minimumSteps(**test_input) == 8 test_input = { "s": "001010010010" } assert my_solution.minimumSteps(**test_input) == 15 test_input = { "s": "11101" } assert my_solution.minimumSteps(**test_input) == 3 test_input = { "s": "00110110" } assert my_solution.minimumSteps(**test_input) == 6 test_input = { "s": "001010" } assert my_solution.minimumSteps(**test_input) == 3 test_input = { "s": "000101010101010" } assert my_solution.minimumSteps(**test_input) == 21 test_input = { "s": "1111011" } assert my_solution.minimumSteps(**test_input) == 4 test_input = { "s": "0000101011001101011" } assert my_solution.minimumSteps(**test_input) == 24 test_input = { "s": "011010" } assert my_solution.minimumSteps(**test_input) == 5 test_input = { "s": "110010100011110101" } assert my_solution.minimumSteps(**test_input) == 36 test_input = { "s": "000000001" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "011001" } assert my_solution.minimumSteps(**test_input) == 4 test_input = { "s": "01111010011110111111" } assert my_solution.minimumSteps(**test_input) == 23 test_input = { "s": "10001111" } assert my_solution.minimumSteps(**test_input) == 3 test_input = { "s": "11011010001111" } assert my_solution.minimumSteps(**test_input) == 21 test_input = { "s": "0110101010100000111" } assert my_solution.minimumSteps(**test_input) == 44 test_input = { "s": "0000001000110" } assert my_solution.minimumSteps(**test_input) == 6 test_input = { "s": "1" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "0111100110100" } assert my_solution.minimumSteps(**test_input) == 28 test_input = { "s": "101010110011101011" } assert my_solution.minimumSteps(**test_input) == 33 test_input = { "s": "1011101" } assert my_solution.minimumSteps(**test_input) == 5
1,700,361,000
weekly-contest-372-maximum-xor-product
https://leetcode.com/problems/maximum-xor-product
maximum-xor-product
{ "questionId": "3192", "questionFrontendId": "2939", "title": "Maximum Xor Product", "titleSlug": "maximum-xor-product", "isPaidOnly": false, "difficulty": "Medium", "likes": 140, "dislikes": 63, "categoryTitle": "Algorithms" }
""" 给你三个整数a,b和n,请你返回(a XOR x) * (b XOR x)的最大值且 x需要满足 0 <= x < 2n。 由于答案可能会很大,返回它对109 + 7取余后的结果。 注意,XOR是按位异或操作。 示例 1: 输入:a = 12, b = 5, n = 4 输出:98 解释:当 x = 2 时,(a XOR x) = 14 且 (b XOR x) = 7 。所以,(a XOR x) * (b XOR x) = 98 。 98 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 示例 2: 输入:a = 6, b = 7 , n = 5 输出:930 解释:当 x = 25 时,(a XOR x) = 31 且 (b XOR x) = 30 。所以,(a XOR x) * (b XOR x) = 930 。 930 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 示例 3: 输入:a = 1, b = 6, n = 3 输出:12 解释: 当 x = 5 时,(a XOR x) = 4 且 (b XOR x) = 3 。所以,(a XOR x) * (b XOR x) = 12 。 12 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 提示: 0 <= a, b < 250 0 <= n <= 50 """ class Solution: def maximumXorProduct(self, a: int, b: int, n: int) -> int:
给你三个整数a,b和n,请你返回(a XOR x) * (b XOR x)的最大值且 x需要满足 0 <= x < 2n。 由于答案可能会很大,返回它对109 + 7取余后的结果。 注意,XOR是按位异或操作。 示例 1: 输入:a = 12, b = 5, n = 4 输出:98 解释:当 x = 2 时,(a XOR x) = 14 且 (b XOR x) = 7 。所以,(a XOR x) * (b XOR x) = 98 。 98 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 示例 2: 输入:a = 6, b = 7 , n = 5 输出:930 解释:当 x = 25 时,(a XOR x) = 31 且 (b XOR x) = 30 。所以,(a XOR x) * (b XOR x) = 930 。 930 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 示例 3: 输入:a = 1, b = 6, n = 3 输出:12 解释: 当 x = 5 时,(a XOR x) = 4 且 (b XOR x) = 3 。所以,(a XOR x) * (b XOR x) = 12 。 12 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 提示: 0 <= a, b < 250 0 <= n <= 50 请完成下面的代码来解决上述问题: ```python class Solution: def maximumXorProduct(self, a: int, b: int, n: int) -> int: ```
my_solution = Solution() test_input = { "a": 12, "b": 5, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 98 test_input = { "a": 6, "b": 7, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 930 test_input = { "a": 1, "b": 6, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 12 test_input = { "a": 0, "b": 0, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 1 test_input = { "a": 0, "b": 1, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3906 test_input = { "a": 0, "b": 2, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 15875 test_input = { "a": 0, "b": 3, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 2 test_input = { "a": 0, "b": 4, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 0 test_input = { "a": 0, "b": 5, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3658 test_input = { "a": 0, "b": 6, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 7 test_input = { "a": 0, "b": 7, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 12 test_input = { "a": 0, "b": 8, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 713 test_input = { "a": 0, "b": 9, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 30 test_input = { "a": 0, "b": 10, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 14875 test_input = { "a": 0, "b": 11, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 84 test_input = { "a": 0, "b": 12, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 45 test_input = { "a": 0, "b": 13, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 42 test_input = { "a": 0, "b": 14, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 0 test_input = { "a": 0, "b": 15, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3080 test_input = { "a": 1, "b": 0, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 42 test_input = { "a": 1, "b": 1, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 225 test_input = { "a": 1, "b": 2, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3782 test_input = { "a": 1, "b": 3, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 3 test_input = { "a": 1, "b": 4, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 810 test_input = { "a": 1, "b": 5, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 165 test_input = { "a": 1, "b": 6, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 132 test_input = { "a": 1, "b": 7, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 15 test_input = { "a": 1, "b": 8, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 8 test_input = { "a": 1, "b": 9, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 33 test_input = { "a": 1, "b": 10, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 14756 test_input = { "a": 1, "b": 11, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 14875 test_input = { "a": 1, "b": 12, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 14518 test_input = { "a": 1, "b": 13, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 13 test_input = { "a": 1, "b": 14, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 552 test_input = { "a": 1, "b": 15, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 63 test_input = { "a": 2, "b": 0, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 0 test_input = { "a": 2, "b": 1, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 2 test_input = { "a": 2, "b": 2, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 4 test_input = { "a": 2, "b": 3, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 930 test_input = { "a": 2, "b": 4, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 143 test_input = { "a": 2, "b": 5, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 756 test_input = { "a": 2, "b": 6, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 165 test_input = { "a": 2, "b": 7, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 18 test_input = { "a": 2, "b": 8, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 27 test_input = { "a": 2, "b": 9, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 24 test_input = { "a": 2, "b": 10, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 713 test_input = { "a": 2, "b": 11, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 22 test_input = { "a": 2, "b": 12, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 39 test_input = { "a": 2, "b": 13, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 36 test_input = { "a": 2, "b": 14, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 28 test_input = { "a": 2, "b": 15, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 42 test_input = { "a": 3, "b": 0, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3782 test_input = { "a": 3, "b": 1, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3843 test_input = { "a": 3, "b": 2, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 6 test_input = { "a": 3, "b": 3, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 16129 test_input = { "a": 3, "b": 4, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 756 test_input = { "a": 3, "b": 5, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 143 test_input = { "a": 3, "b": 6, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 18 test_input = { "a": 3, "b": 7, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 21 test_input = { "a": 3, "b": 8, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3300 test_input = { "a": 3, "b": 9, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 27 test_input = { "a": 3, "b": 10, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 690 test_input = { "a": 3, "b": 11, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 33 test_input = { "a": 3, "b": 12, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 36 test_input = { "a": 3, "b": 13, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 39 test_input = { "a": 3, "b": 14, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 42 test_input = { "a": 3, "b": 15, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3245 test_input = { "a": 4, "b": 0, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 165 test_input = { "a": 4, "b": 1, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 18 test_input = { "a": 4, "b": 2, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 15 test_input = { "a": 4, "b": 3, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 12 test_input = { "a": 4, "b": 4, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 49 test_input = { "a": 4, "b": 5, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 42 test_input = { "a": 4, "b": 6, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 35 test_input = { "a": 4, "b": 7, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 30 test_input = { "a": 4, "b": 8, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 77 test_input = { "a": 4, "b": 9, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 14518 test_input = { "a": 4, "b": 10, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 40 test_input = { "a": 4, "b": 11, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 552 test_input = { "a": 4, "b": 12, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3465 test_input = { "a": 4, "b": 13, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 14994 test_input = { "a": 4, "b": 14, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 667 test_input = { "a": 4, "b": 15, "n": 6 } assert my_solution.maximumXorProduct(**test_input) == 3300 test_input = { "a": 5, "b": 0, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 4 test_input = { "a": 5, "b": 1, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 21 test_input = { "a": 5, "b": 2, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 12 test_input = { "a": 5, "b": 3, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 15375 test_input = { "a": 5, "b": 4, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 16002 test_input = { "a": 5, "b": 5, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 25 test_input = { "a": 5, "b": 6, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 30 test_input = { "a": 5, "b": 7, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 35 test_input = { "a": 5, "b": 8, "n": 2 } assert my_solution.maximumXorProduct(**test_input) == 70 test_input = { "a": 5, "b": 9, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 77 test_input = { "a": 5, "b": 10, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 552 test_input = { "a": 5, "b": 11, "n": 7 } assert my_solution.maximumXorProduct(**test_input) == 14399 test_input = { "a": 5, "b": 12, "n": 1 } assert my_solution.maximumXorProduct(**test_input) == 60 test_input = { "a": 5, "b": 13, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 713 test_input = { "a": 5, "b": 14, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 84 test_input = { "a": 5, "b": 15, "n": 0 } assert my_solution.maximumXorProduct(**test_input) == 75 test_input = { "a": 6, "b": 0, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 143
1,700,361,000
weekly-contest-372-find-building-where-alice-and-bob-can-meet
https://leetcode.com/problems/find-building-where-alice-and-bob-can-meet
find-building-where-alice-and-bob-can-meet
{ "questionId": "3181", "questionFrontendId": "2940", "title": "Find Building Where Alice and Bob Can Meet", "titleSlug": "find-building-where-alice-and-bob-can-meet", "isPaidOnly": false, "difficulty": "Hard", "likes": 171, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的正整数数组heights,其中heights[i]表示第 i栋建筑的高度。 如果一个人在建筑i,且存在i < j的建筑j满足heights[i] < heights[j],那么这个人可以移动到建筑j。 给你另外一个数组queries,其中queries[i] = [ai, bi]。第i个查询中,Alice 在建筑ai ,Bob 在建筑bi。 请你能返回一个数组ans,其中ans[i]是第i个查询中,Alice 和 Bob 可以相遇的最左边的建筑。如果对于查询i,Alice 和 Bob 不能相遇,令ans[i] 为-1。 示例 1: 输入:heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] 输出:[2,5,-1,5,2] 解释:第一个查询中,Alice 和 Bob 可以移动到建筑 2 ,因为 heights[0] < heights[2] 且 heights[1] < heights[2] 。 第二个查询中,Alice 和 Bob 可以移动到建筑 5 ,因为 heights[0] < heights[5] 且 heights[3] < heights[5] 。 第三个查询中,Alice 无法与 Bob 相遇,因为 Alice 不能移动到任何其他建筑。 第四个查询中,Alice 和 Bob 可以移动到建筑 5 ,因为 heights[3] < heights[5] 且 heights[4] < heights[5] 。 第五个查询中,Alice 和 Bob 已经在同一栋建筑中。 对于 ans[i] != -1 ,ans[i] 是 Alice 和 Bob 可以相遇的建筑中最左边建筑的下标。 对于 ans[i] == -1 ,不存在 Alice 和 Bob 可以相遇的建筑。 示例 2: 输入:heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] 输出:[7,6,-1,4,6] 解释:第一个查询中,Alice 可以直接移动到 Bob 的建筑,因为 heights[0] < heights[7] 。 第二个查询中,Alice 和 Bob 可以移动到建筑 6 ,因为 heights[3] < heights[6] 且 heights[5] < heights[6] 。 第三个查询中,Alice 无法与 Bob 相遇,因为 Bob 不能移动到任何其他建筑。 第四个查询中,Alice 和 Bob 可以移动到建筑 4 ,因为 heights[3] < heights[4] 且 heights[0] < heights[4] 。 第五个查询中,Alice 可以直接移动到 Bob 的建筑,因为 heights[1] < heights[6] 。 对于 ans[i] != -1 ,ans[i] 是 Alice 和 Bob 可以相遇的建筑中最左边建筑的下标。 对于 ans[i] == -1 ,不存在 Alice 和 Bob 可以相遇的建筑。 提示: 1 <= heights.length <= 5 * 104 1 <= heights[i] <= 109 1 <= queries.length <= 5 * 104 queries[i] = [ai, bi] 0 <= ai, bi <= heights.length - 1 """ class Solution: def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]:
给你一个下标从 0开始的正整数数组heights,其中heights[i]表示第 i栋建筑的高度。 如果一个人在建筑i,且存在i < j的建筑j满足heights[i] < heights[j],那么这个人可以移动到建筑j。 给你另外一个数组queries,其中queries[i] = [ai, bi]。第i个查询中,Alice 在建筑ai ,Bob 在建筑bi。 请你能返回一个数组ans,其中ans[i]是第i个查询中,Alice 和 Bob 可以相遇的最左边的建筑。如果对于查询i,Alice 和 Bob 不能相遇,令ans[i] 为-1。 示例 1: 输入:heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] 输出:[2,5,-1,5,2] 解释:第一个查询中,Alice 和 Bob 可以移动到建筑 2 ,因为 heights[0] < heights[2] 且 heights[1] < heights[2] 。 第二个查询中,Alice 和 Bob 可以移动到建筑 5 ,因为 heights[0] < heights[5] 且 heights[3] < heights[5] 。 第三个查询中,Alice 无法与 Bob 相遇,因为 Alice 不能移动到任何其他建筑。 第四个查询中,Alice 和 Bob 可以移动到建筑 5 ,因为 heights[3] < heights[5] 且 heights[4] < heights[5] 。 第五个查询中,Alice 和 Bob 已经在同一栋建筑中。 对于 ans[i] != -1 ,ans[i] 是 Alice 和 Bob 可以相遇的建筑中最左边建筑的下标。 对于 ans[i] == -1 ,不存在 Alice 和 Bob 可以相遇的建筑。 示例 2: 输入:heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] 输出:[7,6,-1,4,6] 解释:第一个查询中,Alice 可以直接移动到 Bob 的建筑,因为 heights[0] < heights[7] 。 第二个查询中,Alice 和 Bob 可以移动到建筑 6 ,因为 heights[3] < heights[6] 且 heights[5] < heights[6] 。 第三个查询中,Alice 无法与 Bob 相遇,因为 Bob 不能移动到任何其他建筑。 第四个查询中,Alice 和 Bob 可以移动到建筑 4 ,因为 heights[3] < heights[4] 且 heights[0] < heights[4] 。 第五个查询中,Alice 可以直接移动到 Bob 的建筑,因为 heights[1] < heights[6] 。 对于 ans[i] != -1 ,ans[i] 是 Alice 和 Bob 可以相遇的建筑中最左边建筑的下标。 对于 ans[i] == -1 ,不存在 Alice 和 Bob 可以相遇的建筑。 提示: 1 <= heights.length <= 5 * 104 1 <= heights[i] <= 109 1 <= queries.length <= 5 * 104 queries[i] = [ai, bi] 0 <= ai, bi <= heights.length - 1 请完成下面的代码来解决上述问题: ```python class Solution: def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]: ```
my_solution = Solution() test_input = { "heights": [6,4,8,5,2,7], "queries": [[0,1],[0,3],[2,4],[3,4],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [2,5,-1,5,2] test_input = { "heights": [5,3,8,2,6,1,4,6], "queries": [[0,7],[3,5],[5,2],[3,0],[1,6]] } assert my_solution.leftmostBuildingQueries(**test_input) == [7,6,-1,4,6] test_input = { "heights": [1], "queries": [[0,0]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0] test_input = { "heights": [1000000000], "queries": [[0,0]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0] test_input = { "heights": [1,2], "queries": [[0,0],[0,1],[1,0],[1,1]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,1,1] test_input = { "heights": [2,1], "queries": [[0,0],[0,1],[1,0],[1,1]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,1] test_input = { "heights": [1,2,3], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,1,1,2,2,2,2] test_input = { "heights": [1,3,2], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,1,1,-1,2,-1,2] test_input = { "heights": [2,1,3], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,2,1,2,2,2,2] test_input = { "heights": [2,3,1], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,-1,1,1,-1,-1,-1,2] test_input = { "heights": [3,1,2], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,1,2,-1,2,2] test_input = { "heights": [3,2,1], "queries": [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,1,-1,-1,-1,2] test_input = { "heights": [1,2,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,2,3,2,2,2,3,3,3,3,3] test_input = { "heights": [1,2,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,2,3,2,2,2,-1,3,3,-1,3] test_input = { "heights": [1,3,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,3,3,2,3,2,3,3,3,3,3] test_input = { "heights": [1,3,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,2,-1,2,2,2,-1,3,-1,-1,3] test_input = { "heights": [1,4,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,-1,-1,2,-1,2,3,3,-1,3,3] test_input = { "heights": [1,4,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,1,1,-1,-1,2,-1,2,-1,3,-1,-1,3] test_input = { "heights": [2,1,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,2,1,2,3,2,2,2,3,3,3,3,3] test_input = { "heights": [2,1,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,2,1,2,3,2,2,2,-1,3,3,-1,3] test_input = { "heights": [2,3,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,1,1,3,3,3,3,2,3,3,3,3,3] test_input = { "heights": [2,3,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,-1,1,1,2,-1,2,2,2,-1,-1,-1,-1,3] test_input = { "heights": [2,4,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,1,1,-1,-1,3,-1,2,3,3,-1,3,3] test_input = { "heights": [2,4,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,-1,1,1,-1,-1,2,-1,2,-1,-1,-1,-1,3] test_input = { "heights": [3,1,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,3,1,2,3,3,2,2,3,3,3,3,3] test_input = { "heights": [3,1,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,-1,2,1,2,3,2,2,2,-1,-1,3,-1,3] test_input = { "heights": [3,2,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,3,1,3,3,3,3,2,3,3,3,3,3] test_input = { "heights": [3,2,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,-1,2,1,2,-1,2,2,2,-1,-1,-1,-1,3] test_input = { "heights": [3,4,1,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,-1,-1,1,1,-1,-1,-1,-1,2,3,-1,-1,3,3] test_input = { "heights": [3,4,2,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,-1,-1,1,1,-1,-1,-1,-1,2,-1,-1,-1,-1,3] test_input = { "heights": [4,1,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,2,3,-1,2,2,3,-1,3,3,3] test_input = { "heights": [4,1,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,2,3,-1,2,2,-1,-1,3,-1,3] test_input = { "heights": [4,2,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,3,3,-1,3,2,3,-1,3,3,3] test_input = { "heights": [4,2,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,2,-1,-1,2,2,-1,-1,-1,-1,3] test_input = { "heights": [4,3,1,2], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,-1,-1,-1,-1,2,3,-1,-1,3,3] test_input = { "heights": [4,3,2,1], "queries": [[0,0],[0,1],[0,2],[0,3],[1,0],[1,1],[1,2],[1,3],[2,0],[2,1],[2,2],[2,3],[3,0],[3,1],[3,2],[3,3]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,-1,-1,-1,-1,1,-1,-1,-1,-1,2,-1,-1,-1,-1,3] test_input = { "heights": [1,2,3,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,3,4,3,3,3,3,4,4,4,4,4,4] test_input = { "heights": [1,2,3,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,3,4,3,3,3,3,-1,4,4,4,-1,4] test_input = { "heights": [1,2,4,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,4,4,3,3,4,3,4,4,4,4,4,4] test_input = { "heights": [1,2,4,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,3,-1,3,3,3,3,-1,4,4,-1,-1,4] test_input = { "heights": [1,2,5,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,4,4,4,-1,4,4] test_input = { "heights": [1,2,5,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,-1,4,4,-1,-1,4] test_input = { "heights": [1,3,2,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,4,2,3,2,3,4,3,3,3,3,4,4,4,4,4,4] test_input = { "heights": [1,3,2,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,4,2,3,2,3,4,3,3,3,3,-1,4,4,4,-1,4] test_input = { "heights": [1,3,4,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,4,4,2,2,2,4,4,3,4,4,3,4,4,4,4,4,4] test_input = { "heights": [1,3,4,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,-1,2,2,2,3,-1,3,3,3,3,-1,4,-1,-1,-1,4] test_input = { "heights": [1,3,5,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,4,4,2,2,2,-1,-1,3,4,-1,3,4,4,4,-1,4,4] test_input = { "heights": [1,3,5,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,3,-1,2,2,2,-1,-1,3,3,-1,3,-1,4,-1,-1,-1,4] test_input = { "heights": [1,4,2,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,4,4,4,2,4,2,3,4,3,4,3,3,4,4,4,4,4,4] test_input = { "heights": [1,4,2,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,-1,2,3,2,3,4,3,3,3,3,-1,4,-1,4,-1,4] test_input = { "heights": [1,4,3,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,4,4,4,2,4,2,4,4,3,4,4,3,4,4,4,4,4,4] test_input = { "heights": [1,4,3,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,3,3,-1,2,3,2,3,-1,3,3,3,3,-1,4,-1,-1,-1,4] test_input = { "heights": [1,4,5,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,-1,-1,2,2,2,-1,-1,3,-1,-1,3,4,4,-1,-1,4,4] test_input = { "heights": [1,4,5,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,2,-1,-1,2,2,2,-1,-1,3,-1,-1,3,-1,4,-1,-1,-1,4] test_input = { "heights": [1,5,2,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,3,4,3,-1,3,3,4,4,-1,4,4,4] test_input = { "heights": [1,5,2,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,3,4,3,-1,3,3,-1,4,-1,4,-1,4] test_input = { "heights": [1,5,3,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,4,4,3,-1,4,3,4,4,-1,4,4,4] test_input = { "heights": [1,5,3,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,3,-1,3,-1,3,3,-1,4,-1,-1,-1,4] test_input = { "heights": [1,5,4,2,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,-1,-1,3,-1,-1,3,4,4,-1,-1,4,4] test_input = { "heights": [1,5,4,3,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,4,1,1,-1,-1,-1,2,-1,2,-1,-1,3,-1,-1,3,-1,4,-1,-1,-1,4] test_input = { "heights": [2,1,3,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,3,4,3,3,3,3,4,4,4,4,4,4] test_input = { "heights": [2,1,3,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,3,4,3,3,3,3,-1,4,4,4,-1,4] test_input = { "heights": [2,1,4,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,4,4,3,3,4,3,4,4,4,4,4,4] test_input = { "heights": [2,1,4,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,3,-1,3,3,3,3,-1,4,4,-1,-1,4] test_input = { "heights": [2,1,5,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,4,4,4,-1,4,4] test_input = { "heights": [2,1,5,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,4,2,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,-1,4,4,-1,-1,4] test_input = { "heights": [2,3,1,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,3,3,4,3,3,2,3,4,3,3,3,3,4,4,4,4,4,4] test_input = { "heights": [2,3,1,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,3,3,4,3,3,2,3,4,3,3,3,3,-1,4,4,4,-1,4] test_input = { "heights": [2,3,4,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,2,4,4,2,2,2,4,4,4,4,4,3,4,4,4,4,4,4] test_input = { "heights": [2,3,4,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,2,3,-1,2,2,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [2,3,5,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,2,4,4,2,2,2,-1,-1,4,4,-1,3,4,4,4,-1,4,4] test_input = { "heights": [2,3,5,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,2,3,-1,2,2,2,-1,-1,3,3,-1,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [2,4,1,3,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,4,4,4,3,4,2,3,4,3,4,3,3,4,4,4,4,4,4] test_input = { "heights": [2,4,1,5,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,3,3,-1,3,3,2,3,4,3,3,3,3,-1,4,-1,4,-1,4] test_input = { "heights": [2,4,3,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,4,4,4,2,4,2,4,4,4,4,4,3,4,4,4,4,4,4] test_input = { "heights": [2,4,3,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,3,3,-1,2,3,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [2,4,5,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,2,-1,-1,2,2,2,-1,-1,4,-1,-1,3,4,4,-1,-1,4,4] test_input = { "heights": [2,4,5,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,2,-1,-1,2,2,2,-1,-1,3,-1,-1,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [2,5,1,3,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,-1,-1,-1,3,-1,2,3,4,3,-1,3,3,4,4,-1,4,4,4] test_input = { "heights": [2,5,1,4,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,4,1,1,-1,-1,-1,3,-1,2,3,4,3,-1,3,3,-1,4,-1,4,-1,4] test_input = { "heights": [2,5,3,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,-1,-1,-1,2,-1,2,4,4,4,-1,4,3,4,4,-1,4,4,4] test_input = { "heights": [2,5,3,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,-1,-1,-1,2,-1,2,3,-1,3,-1,3,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [2,5,4,1,3], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,4,4,1,1,-1,-1,-1,2,-1,2,-1,-1,4,-1,-1,3,4,4,-1,-1,4,4] test_input = { "heights": [2,5,4,3,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,2,3,-1,1,1,-1,-1,-1,2,-1,2,-1,-1,3,-1,-1,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [3,1,2,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,2,3,4,3,2,2,3,4,3,3,3,3,4,4,4,4,4,4] test_input = { "heights": [3,1,2,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,2,3,4,3,2,2,3,4,3,3,3,3,-1,4,4,4,-1,4] test_input = { "heights": [3,1,4,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,3,4,2,2,2,4,4,4,3,4,3,4,4,4,4,4,4] test_input = { "heights": [3,1,4,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,4,2,2,2,3,-1,3,3,3,3,-1,-1,4,-1,-1,4] test_input = { "heights": [3,1,5,2,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,3,4,2,2,2,-1,-1,4,3,-1,3,4,4,4,-1,4,4] test_input = { "heights": [3,1,5,4,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,4,2,2,2,-1,-1,3,3,-1,3,-1,-1,4,-1,-1,4] test_input = { "heights": [3,2,1,4,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,3,3,4,3,3,2,3,4,3,3,3,3,4,4,4,4,4,4] test_input = { "heights": [3,2,1,5,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,3,3,3,4,3,1,3,3,4,3,3,2,3,4,3,3,3,3,-1,4,4,4,-1,4] test_input = { "heights": [3,2,4,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,4,4,2,2,2,4,4,4,4,4,3,4,4,4,4,4,4] test_input = { "heights": [3,2,4,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,-1,2,2,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [3,2,5,1,4], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,4,4,2,1,2,4,4,2,2,2,-1,-1,4,4,-1,3,4,4,4,-1,4,4] test_input = { "heights": [3,2,5,4,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,2,2,3,-1,2,1,2,3,-1,2,2,2,-1,-1,3,3,-1,3,-1,-1,-1,-1,-1,4] test_input = { "heights": [3,4,1,2,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,4,4,4,1,1,4,4,4,4,4,2,3,4,4,4,3,3,4,4,4,4,4,4] test_input = { "heights": [3,4,1,5,2], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,-1,1,1,3,3,-1,3,3,2,3,4,3,3,3,3,-1,-1,-1,4,-1,4] test_input = { "heights": [3,4,2,1,5], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,4,4,4,1,1,4,4,4,4,4,2,4,4,4,4,4,3,4,4,4,4,4,4] test_input = { "heights": [3,4,2,5,1], "queries": [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]] } assert my_solution.leftmostBuildingQueries(**test_input) == [0,1,3,3,-1,1,1,3,3,-1,3,3,2,3,-1,3,3,3,3,-1,-1,-1,-1,-1,4]
1,700,361,000
weekly-contest-371-maximum-strong-pair-xor-i
https://leetcode.com/problems/maximum-strong-pair-xor-i
maximum-strong-pair-xor-i
{ "questionId": "3193", "questionFrontendId": "2932", "title": "Maximum Strong Pair XOR I", "titleSlug": "maximum-strong-pair-xor-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 77, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) 和 (5, 5) 。 这些强数对中的最大异或值是 3 XOR 4 = 7 。 示例 2: 输入:nums = [10,100] 输出:0 解释:数组 nums 中有 2 个强数对:(10, 10) 和 (100, 100) 。 这些强数对中的最大异或值是 10 XOR 10 = 0 ,数对 (100, 100) 的异或值也是 100 XOR 100 = 0 。 示例 3: 输入:nums = [5,6,25,30] 输出:7 解释:数组 nums 中有 6 个强数对:(5, 5), (5, 6), (6, 6), (25, 25), (25, 30) 和 (30, 30) 。 这些强数对中的最大异或值是 25 XOR 30 = 7 ;另一个异或值非零的数对是 (5, 6) ,其异或值是 5 XOR 6 = 3 。 提示: 1 <= nums.length <= 50 1 <= nums[i] <= 100 """ class Solution: def maximumStrongPairXor(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) 和 (5, 5) 。 这些强数对中的最大异或值是 3 XOR 4 = 7 。 示例 2: 输入:nums = [10,100] 输出:0 解释:数组 nums 中有 2 个强数对:(10, 10) 和 (100, 100) 。 这些强数对中的最大异或值是 10 XOR 10 = 0 ,数对 (100, 100) 的异或值也是 100 XOR 100 = 0 。 示例 3: 输入:nums = [5,6,25,30] 输出:7 解释:数组 nums 中有 6 个强数对:(5, 5), (5, 6), (6, 6), (25, 25), (25, 30) 和 (30, 30) 。 这些强数对中的最大异或值是 25 XOR 30 = 7 ;另一个异或值非零的数对是 (5, 6) ,其异或值是 5 XOR 6 = 3 。 提示: 1 <= nums.length <= 50 1 <= nums[i] <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def maximumStrongPairXor(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [10,100] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [5,6,25,30] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [100] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,1,2,3,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,1,3,8,7] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,1,4,4,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [1,1,6,6,9] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,1,10,3,9] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,2,1,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,2,2,1,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,2,3,8,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,2,5,5,10] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,2,8,3,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,2,9,2,8] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,3,3,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,3,8,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 13 test_input = { "nums": [1,3,9,6,5] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,4,1,2,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,4,3,9,7] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [1,4,4,3,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [1,4,5,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,4,8,6,6] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [1,5,1,9,1] } assert my_solution.maximumStrongPairXor(**test_input) == 12 test_input = { "nums": [1,5,4,1,7] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,5,5,2,7] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,5,9,1,2] } assert my_solution.maximumStrongPairXor(**test_input) == 12 test_input = { "nums": [1,5,9,10,4] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,6,2,7,9] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,6,3,3,10] } assert my_solution.maximumStrongPairXor(**test_input) == 12 test_input = { "nums": [1,6,4,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [1,6,4,6,1] } assert my_solution.maximumStrongPairXor(**test_input) == 2 test_input = { "nums": [1,6,5,5,8] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [1,6,6,1,9] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,6,8,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [1,6,8,10,3] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [1,6,9,8,2] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,6,10,1,2] } assert my_solution.maximumStrongPairXor(**test_input) == 12 test_input = { "nums": [1,7,1,4,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [1,7,2,1,10] } assert my_solution.maximumStrongPairXor(**test_input) == 13 test_input = { "nums": [1,7,2,10,10] } assert my_solution.maximumStrongPairXor(**test_input) == 13 test_input = { "nums": [1,7,6,1,4] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,7,8,6,8] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,8,1,1,8] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,8,4,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 12 test_input = { "nums": [1,8,4,5,6] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [1,8,5,10,4] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,8,8,8,3] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,8,10,2,6] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [1,9,4,4,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,9,6,5,7] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,9,6,8,1] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,9,8,2,8] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,9,8,9,6] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,9,9,7,6] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,10,1,1,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,10,5,10,6] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,10,8,7,2] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [1,10,9,9,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,1,1,5,5] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,1,1,7,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,1,8,3,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,1,9,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,2,4,1,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,2,5,5,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,2,5,10,6] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,2,8,2,10] } assert my_solution.maximumStrongPairXor(**test_input) == 2 test_input = { "nums": [2,2,10,5,9] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,3,3,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,3,8,8,10] } assert my_solution.maximumStrongPairXor(**test_input) == 2 test_input = { "nums": [2,4,5,3,2] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [2,4,6,8,8] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [2,4,6,9,8] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,4,8,7,5] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,4,10,4,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,4,10,6,2] } assert my_solution.maximumStrongPairXor(**test_input) == 12 test_input = { "nums": [2,4,10,7,8] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,4,10,9,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [2,5,2,5,5] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [2,5,3,10,10] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,5,7,2,7] } assert my_solution.maximumStrongPairXor(**test_input) == 2 test_input = { "nums": [2,5,9,8,9] } assert my_solution.maximumStrongPairXor(**test_input) == 13 test_input = { "nums": [2,5,10,8,4] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,6,1,6,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,6,1,8,7] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,6,7,4,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,6,8,6,10] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [2,6,10,5,4] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,7,1,8,9] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,7,2,8,10] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,7,3,3,7] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [2,7,4,8,1] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,7,9,2,6] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,7,10,7,1] } assert my_solution.maximumStrongPairXor(**test_input) == 13 test_input = { "nums": [2,8,1,9,5] } assert my_solution.maximumStrongPairXor(**test_input) == 13 test_input = { "nums": [2,8,7,10,3] } assert my_solution.maximumStrongPairXor(**test_input) == 15 test_input = { "nums": [2,9,1,7,7] } assert my_solution.maximumStrongPairXor(**test_input) == 14 test_input = { "nums": [2,9,2,8,2] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [2,9,5,10,10] } assert my_solution.maximumStrongPairXor(**test_input) == 15
1,699,756,200
weekly-contest-371-high-access-employees
https://leetcode.com/problems/high-access-employees
high-access-employees
{ "questionId": "3202", "questionFrontendId": "2933", "title": "High-Access Employees", "titleSlug": "high-access-employees", "isPaidOnly": false, "difficulty": "Medium", "likes": 110, "dislikes": 14, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 、下标从 0 开始的二维字符串数组 access_times 。对于每个 i(0 <= i <= n - 1 ),access_times[i][0] 表示某位员工的姓名,access_times[i][1] 表示该员工的访问时间。access_times 中的所有条目都发生在同一天内。 访问时间用 四位 数字表示, 符合 24 小时制 ,例如 "0800" 或 "2250" 。 如果员工在 同一小时内 访问系统 三次或更多 ,则称其为 高访问 员工。 时间间隔正好相差一小时的时间 不 被视为同一小时内。例如,"0815" 和 "0915" 不属于同一小时内。 一天开始和结束时的访问时间不被计算为同一小时内。例如,"0005" 和 "2350" 不属于同一小时内。 以列表形式,按任意顺序,返回所有 高访问 员工的姓名。 示例 1: 输入:access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]] 输出:["a"] 解释:"a" 在时间段 [05:32, 06:31] 内有三条访问记录,时间分别为 05:32 、05:49 和 06:21 。 但是 "b" 的访问记录只有两条。 因此,答案是 ["a"] 。 示例 2: 输入:access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]] 输出:["c","d"] 解释:"c" 在时间段 [08:08, 09:07] 内有三条访问记录,时间分别为 08:08 、08:09 和 08:29 。 "d" 在时间段 [14:10, 15:09] 内有三条访问记录,时间分别为 14:10 、14:44 和 15:08 。 然而,"e" 只有一条访问记录,因此不能包含在答案中,最终答案是 ["c","d"] 。 示例 3: 输入:access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]] 输出:["ab","cd"] 解释:"ab"在时间段 [10:25, 11:24] 内有三条访问记录,时间分别为 10:25 、11:20 和 11:24 。 "cd" 在时间段 [10:25, 11:24] 内有三条访问记录,时间分别为 10:25 、10:46 和 10:55 。 因此,答案是 ["ab","cd"] 。 提示: 1 <= access_times.length <= 100 access_times[i].length == 2 1 <= access_times[i][0].length <= 10 access_times[i][0] 仅由小写英文字母组成。 access_times[i][1].length == 4 access_times[i][1] 采用24小时制表示时间。 access_times[i][1] 仅由数字 '0' 到 '9' 组成。 """ class Solution: def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
给你一个长度为 n 、下标从 0 开始的二维字符串数组 access_times 。对于每个 i(0 <= i <= n - 1 ),access_times[i][0] 表示某位员工的姓名,access_times[i][1] 表示该员工的访问时间。access_times 中的所有条目都发生在同一天内。 访问时间用 四位 数字表示, 符合 24 小时制 ,例如 "0800" 或 "2250" 。 如果员工在 同一小时内 访问系统 三次或更多 ,则称其为 高访问 员工。 时间间隔正好相差一小时的时间 不 被视为同一小时内。例如,"0815" 和 "0915" 不属于同一小时内。 一天开始和结束时的访问时间不被计算为同一小时内。例如,"0005" 和 "2350" 不属于同一小时内。 以列表形式,按任意顺序,返回所有 高访问 员工的姓名。 示例 1: 输入:access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]] 输出:["a"] 解释:"a" 在时间段 [05:32, 06:31] 内有三条访问记录,时间分别为 05:32 、05:49 和 06:21 。 但是 "b" 的访问记录只有两条。 因此,答案是 ["a"] 。 示例 2: 输入:access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]] 输出:["c","d"] 解释:"c" 在时间段 [08:08, 09:07] 内有三条访问记录,时间分别为 08:08 、08:09 和 08:29 。 "d" 在时间段 [14:10, 15:09] 内有三条访问记录,时间分别为 14:10 、14:44 和 15:08 。 然而,"e" 只有一条访问记录,因此不能包含在答案中,最终答案是 ["c","d"] 。 示例 3: 输入:access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]] 输出:["ab","cd"] 解释:"ab"在时间段 [10:25, 11:24] 内有三条访问记录,时间分别为 10:25 、11:20 和 11:24 。 "cd" 在时间段 [10:25, 11:24] 内有三条访问记录,时间分别为 10:25 、10:46 和 10:55 。 因此,答案是 ["ab","cd"] 。 提示: 1 <= access_times.length <= 100 access_times[i].length == 2 1 <= access_times[i][0].length <= 10 access_times[i][0] 仅由小写英文字母组成。 access_times[i][1].length == 4 access_times[i][1] 采用24小时制表示时间。 access_times[i][1] 仅由数字 '0' 到 '9' 组成。 请完成下面的代码来解决上述问题: ```python class Solution: def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]: ```
my_solution = Solution() test_input = { "access_times": [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["a"] test_input = { "access_times": [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["c","d"] test_input = { "access_times": [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["ab","cd"] test_input = { "access_times": [["baipstt","1456"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["bouo","1126"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["cavfbqg","2304"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["cenjcq","1007"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["cqotrwqcaq","0131"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["downbuk","1951"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["dqsoiyz","2204"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["duzeyrov","0243"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["erfg","1223"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["fwhefd","2026"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["gbefbne","0911"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["gp","1540"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["ht","1319"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["inahnsjdqz","1750"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["jwxvijxo","0851"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["kibwwvjuez","0716"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["lvry","0706"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["mbsyxxfzjf","0114"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["mlehvzqb","1620"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["mmgat","0516"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["mxatapbs","2240"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["mzxbgtfc","1531"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["nnhh","1445"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["o","1414"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["qaxqifxxww","1557"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["rjy","0200"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["sgpgh","0539"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["sxx","0325"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["tkvgcf","1645"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["ttk","0304"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["un","0833"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["vlifcdn","0731"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["w","2224"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["wkmehwsg","2023"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["y","1005"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["ynnale","1331"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["yt","0900"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["zbgzk","0527"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["a","0039"],["a","0042"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["ajhzcltqse","0605"],["ajhzcltqse","0558"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["cbaqsymoi","0001"],["cbaqsymoi","0004"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["df","1958"],["df","2002"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["dhmnhvou","0529"],["dhmnhvou","0531"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["epghzrog","0333"],["epghzrog","0333"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["gda","1529"],["gda","1534"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["gjhtgm","2207"],["gjhtgm","2156"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["gsd","2030"],["gsd","2046"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["gsstuktwm","1403"],["gsstuktwm","1357"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["h","2159"],["h","2203"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["hxrdffk","1736"],["hxrdffk","1724"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["iaxsnenx","2037"],["iaxsnenx","2050"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["ikwjvflxq","0055"],["ikwjvflxq","0056"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["jkgjmku","0743"],["jkgjmku","0754"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["jkw","0241"],["jkw","0235"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["jykugiprxf","1633"],["jykugiprxf","1641"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["kdxw","1338"],["kdxw","1336"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["kenltmrg","0932"],["kenltmrg","0941"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["kptjrr","1356"],["kptjrr","1349"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["mcd","1333"],["mcd","1325"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["mhkizga","1552"],["mhkizga","1551"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["monxm","1748"],["monxm","1742"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["msjydtinfy","1301"],["msjydtinfy","1245"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["myhdmu","1407"],["myhdmu","1419"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["nyoezc","1050"],["nyoezc","1041"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["oksvrskxch","0053"],["oksvrskxch","0111"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["pxc","1915"],["pxc","1910"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["qedxyj","0609"],["qedxyj","0614"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["qmslkyxnph","0946"],["qmslkyxnph","0958"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["r","0206"],["r","0202"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["r","2041"],["r","2052"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["rf","2205"],["rf","2203"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["rswegeuhqd","0235"],["rswegeuhqd","0238"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["skfgl","0718"],["skfgl","0712"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["smnnl","2329"],["smnnl","2340"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["tpbbxpx","0409"],["tpbbxpx","0408"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["uiqxqp","0515"],["uiqxqp","0516"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["uyuz","1530"],["uyuz","1543"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["vfeunkee","1500"],["vfeunkee","1508"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["wbyd","1848"],["wbyd","1839"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["x","0522"],["x","0506"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["xhrhdy","1455"],["xhrhdy","1454"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["xmsypay","1605"],["xmsypay","1612"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["xy","0015"],["xy","0021"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["ydtnnpzw","0516"],["ydtnnpzw","0520"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["zh","2348"],["zh","2334"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["zinywjn","0017"],["zinywjn","0019"]] } assert my_solution.findHighAccessEmployees(**test_input) == [] test_input = { "access_times": [["aczdfmsd","0317"],["aczdfmsd","0314"],["aczdfmsd","0320"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["aczdfmsd"] test_input = { "access_times": [["bsluadumi","1518"],["bsluadumi","1516"],["bsluadumi","1510"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["bsluadumi"] test_input = { "access_times": [["ckrdpxq","1122"],["ckrdpxq","1125"],["ckrdpxq","1121"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["ckrdpxq"] test_input = { "access_times": [["fe","1320"],["fe","1326"],["fe","1331"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["fe"] test_input = { "access_times": [["ff","1508"],["ff","1508"],["ff","1516"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["ff"] test_input = { "access_times": [["fnlmbcedu","0052"],["fnlmbcedu","0103"],["fnlmbcedu","0055"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["fnlmbcedu"] test_input = { "access_times": [["hffgwjjve","0159"],["hffgwjjve","0152"],["hffgwjjve","0159"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["hffgwjjve"] test_input = { "access_times": [["ivlvfgwsx","0122"],["ivlvfgwsx","0135"],["ivlvfgwsx","0139"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["ivlvfgwsx"] test_input = { "access_times": [["jlfnksqlt","0304"],["jlfnksqlt","0252"],["jlfnksqlt","0304"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["jlfnksqlt"] test_input = { "access_times": [["jy","0647"],["jy","0652"],["jy","0704"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["jy"] test_input = { "access_times": [["kchzzdso","2329"],["kchzzdso","2326"],["kchzzdso","2329"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["kchzzdso"]
1,699,756,200
weekly-contest-371-minimum-operations-to-maximize-last-elements-in-arrays
https://leetcode.com/problems/minimum-operations-to-maximize-last-elements-in-arrays
minimum-operations-to-maximize-last-elements-in-arrays
{ "questionId": "3190", "questionFrontendId": "2934", "title": "Minimum Operations to Maximize Last Elements in Arrays", "titleSlug": "minimum-operations-to-maximize-last-elements-in-arrays", "isPaidOnly": false, "difficulty": "Medium", "likes": 152, "dislikes": 12, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,这两个数组的长度都是 n 。 你可以执行一系列 操作(可能不执行)。 在每次操作中,你可以选择一个在范围 [0, n - 1] 内的下标 i ,并交换 nums1[i] 和 nums2[i] 的值。 你的任务是找到满足以下条件所需的 最小 操作次数: nums1[n - 1] 等于 nums1 中所有元素的 最大值 ,即 nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1]) 。 nums2[n - 1] 等于 nums2 中所有元素的 最大值 ,即 nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1]) 。 以整数形式,表示并返回满足上述 全部 条件所需的 最小 操作次数,如果无法同时满足两个条件,则返回 -1 。 示例 1: 输入:nums1 = [1,2,7],nums2 = [4,5,3] 输出:1 解释:在这个示例中,可以选择下标 i = 2 执行一次操作。 交换 nums1[2] 和 nums2[2] 的值,nums1 变为 [1,2,3] ,nums2 变为 [4,5,7] 。 同时满足两个条件。 可以证明,需要执行的最小操作次数为 1 。 因此,答案是 1 。 示例 2: 输入:nums1 = [2,3,4,5,9],nums2 = [8,8,4,4,4] 输出:2 解释:在这个示例中,可以执行以下操作: 首先,选择下标 i = 4 执行操作。 交换 nums1[4] 和 nums2[4] 的值,nums1 变为 [2,3,4,5,4] ,nums2 变为 [8,8,4,4,9] 。 然后,选择下标 i = 3 执行操作。 交换 nums1[3] 和 nums2[3] 的值,nums1 变为 [2,3,4,4,4] ,nums2 变为 [8,8,4,5,9] 。 同时满足两个条件。 可以证明,需要执行的最小操作次数为 2 。 因此,答案是 2 。 示例 3: 输入:nums1 = [1,5,4],nums2 = [2,5,3] 输出:-1 解释:在这个示例中,无法同时满足两个条件。 因此,答案是 -1 。 提示: 1 <= n == nums1.length == nums2.length <= 1000 1 <= nums1[i] <= 109 1 <= nums2[i] <= 109 """ class Solution: def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,这两个数组的长度都是 n 。 你可以执行一系列 操作(可能不执行)。 在每次操作中,你可以选择一个在范围 [0, n - 1] 内的下标 i ,并交换 nums1[i] 和 nums2[i] 的值。 你的任务是找到满足以下条件所需的 最小 操作次数: nums1[n - 1] 等于 nums1 中所有元素的 最大值 ,即 nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1]) 。 nums2[n - 1] 等于 nums2 中所有元素的 最大值 ,即 nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1]) 。 以整数形式,表示并返回满足上述 全部 条件所需的 最小 操作次数,如果无法同时满足两个条件,则返回 -1 。 示例 1: 输入:nums1 = [1,2,7],nums2 = [4,5,3] 输出:1 解释:在这个示例中,可以选择下标 i = 2 执行一次操作。 交换 nums1[2] 和 nums2[2] 的值,nums1 变为 [1,2,3] ,nums2 变为 [4,5,7] 。 同时满足两个条件。 可以证明,需要执行的最小操作次数为 1 。 因此,答案是 1 。 示例 2: 输入:nums1 = [2,3,4,5,9],nums2 = [8,8,4,4,4] 输出:2 解释:在这个示例中,可以执行以下操作: 首先,选择下标 i = 4 执行操作。 交换 nums1[4] 和 nums2[4] 的值,nums1 变为 [2,3,4,5,4] ,nums2 变为 [8,8,4,4,9] 。 然后,选择下标 i = 3 执行操作。 交换 nums1[3] 和 nums2[3] 的值,nums1 变为 [2,3,4,4,4] ,nums2 变为 [8,8,4,5,9] 。 同时满足两个条件。 可以证明,需要执行的最小操作次数为 2 。 因此,答案是 2 。 示例 3: 输入:nums1 = [1,5,4],nums2 = [2,5,3] 输出:-1 解释:在这个示例中,无法同时满足两个条件。 因此,答案是 -1 。 提示: 1 <= n == nums1.length == nums2.length <= 1000 1 <= nums1[i] <= 109 1 <= nums2[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def minOperations(self, nums1: List[int], nums2: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums1": [1,2,7], "nums2": [4,5,3] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [2,3,4,5,9], "nums2": [8,8,4,4,4] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [1,5,4], "nums2": [2,5,3] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [1], "nums2": [1] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,2], "nums2": [2,1] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [1,1,10], "nums2": [1,5,1] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [1,4,16], "nums2": [16,16,16] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,5,15], "nums2": [1,1,1] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [2,5,7], "nums2": [2,2,2] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [8,9,10], "nums2": [10,9,9] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [9,14,14], "nums2": [14,11,14] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [16,16,16], "nums2": [6,7,16] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [19,7,19], "nums2": [5,19,19] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,1,8,9], "nums2": [1,7,1,1] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [1,5,9,9], "nums2": [9,9,8,9] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,7,7,7], "nums2": [7,3,3,7] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [10,18,12,12], "nums2": [19,6,5,12] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [12,9,11,12], "nums2": [3,9,9,9] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [15,54,22,54], "nums2": [54,19,54,54] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [20,20,20,20], "nums2": [5,8,19,20] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,3,4,6,7], "nums2": [1,1,1,1,1] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,11,17,1,18], "nums2": [1,1,1,18,1] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [3,3,3,14,14], "nums2": [3,3,4,3,3] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [4,4,8,4,17], "nums2": [4,8,4,14,4] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [4,12,11,11,12], "nums2": [12,6,12,12,12] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [6,21,87,63,87], "nums2": [87,87,23,63,63] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [10,6,6,6,10], "nums2": [6,6,10,10,6] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [16,12,15,15,16], "nums2": [8,16,16,15,15] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [17,3,6,6,17], "nums2": [3,17,6,14,6] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [19,13,18,18,19], "nums2": [5,13,13,13,13] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [20,20,53,20,68], "nums2": [20,28,20,61,20] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [6,6,2,4,4,6], "nums2": [1,1,6,4,4,4] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [8,6,6,6,7,8], "nums2": [5,8,8,8,7,7] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [15,1,15,6,12,15], "nums2": [1,15,2,15,15,15] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [17,13,19,9,6,14], "nums2": [17,14,15,1,19,19] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [20,8,10,12,17,20], "nums2": [7,20,20,20,20,20] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [20,18,15,11,17,20], "nums2": [7,15,13,7,20,12] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [20,20,20,20,19,20], "nums2": [3,6,7,15,20,20] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [28,43,79,32,40,3], "nums2": [95,25,74,16,82,56] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [29,81,58,99,41,36], "nums2": [15,34,47,57,31,95] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [1,1,4,1,7,1,15], "nums2": [1,1,1,4,1,9,1] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [1,4,4,4,7,4,9], "nums2": [9,9,4,6,4,9,4] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [2,88,17,88,68,68,88], "nums2": [88,16,88,68,68,68,68] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [3,3,5,3,3,3,18], "nums2": [3,4,3,11,13,14,3] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [3,41,98,71,71,71,98], "nums2": [98,98,49,71,85,94,71] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [5,5,27,53,5,59,65], "nums2": [5,16,5,5,58,5,5] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [5,6,11,11,13,11,14], "nums2": [14,14,11,11,11,14,11] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [6,5,1,10,8,4,5], "nums2": [3,8,8,3,6,1,4] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [14,3,4,4,4,4,14], "nums2": [2,14,4,4,5,12,4] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [14,28,68,68,65,67,68], "nums2": [68,68,49,64,68,67,67] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [19,19,8,9,10,13,19], "nums2": [1,5,19,19,19,13,13] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [20,6,20,20,20,20,20], "nums2": [3,20,7,9,13,14,20] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [85,85,62,85,78,84,85], "nums2": [5,37,85,76,85,85,85] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,2,2,2,2,2,2,10], "nums2": [10,2,2,2,7,8,9,2] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [1,9,3,3,4,9,9,9], "nums2": [9,2,9,9,9,6,8,9] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [3,3,4,3,3,3,3,8], "nums2": [3,3,3,5,7,7,8,3] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [4,8,11,19,16,16,19,19], "nums2": [19,19,19,13,16,16,16,16] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [8,20,4,9,9,4,4,20], "nums2": [9,14,10,1,1,20,16,15] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [9,9,1,2,2,6,8,9], "nums2": [1,1,9,2,2,2,2,2] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [10,4,10,9,10,10,10,10], "nums2": [4,10,9,9,9,9,9,9] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [13,95,19,56,59,95,95,95], "nums2": [95,14,95,95,95,83,88,95] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [19,7,19,8,9,15,9,19], "nums2": [7,19,7,19,9,9,18,9] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [19,19,3,3,15,16,3,19], "nums2": [2,2,3,7,3,3,17,3] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [1,8,8,5,8,8,8,7,8], "nums2": [8,2,4,8,5,6,6,8,8] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,10,10,5,10,8,10,10,10], "nums2": [10,2,4,10,7,8,8,8,8] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [2,7,7,7,7,7,7,17,18], "nums2": [18,7,10,11,13,15,17,7,7] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [3,13,22,24,13,13,13,81,91], "nums2": [91,13,13,13,45,59,71,13,13] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [4,4,9,4,16,4,17,4,18], "nums2": [4,8,4,14,4,16,4,18,4] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [6,9,65,13,65,65,28,28,65], "nums2": [65,65,13,65,24,27,28,34,28] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [8,3,5,6,6,3,3,3,8], "nums2": [1,3,3,3,3,7,7,8,3] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [10,1,10,10,10,9,9,9,10], "nums2": [1,10,5,6,8,9,9,10,9] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [15,15,3,5,15,15,9,13,15], "nums2": [1,2,15,15,6,8,9,9,9] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [16,3,3,7,3,11,3,3,16], "nums2": [2,3,6,3,10,3,15,16,3] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [16,3,16,7,16,16,10,10,16], "nums2": [2,16,4,16,9,10,10,13,10] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [80,80,18,39,39,62,39,39,80], "nums2": [2,10,80,39,42,39,66,66,39] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [1,1,1,1,1,1,1,7,1,9], "nums2": [9,1,3,3,3,5,5,1,9,1] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [1,2,2,2,2,5,8,2,2,9], "nums2": [9,2,2,2,4,2,2,9,9,2] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [1,2,18,18,10,18,18,17,18,18], "nums2": [18,18,5,5,18,13,14,18,18,18] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [1,3,1,1,1,1,16,16,1,19], "nums2": [1,1,7,7,9,12,1,1,18,1] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [1,9,3,9,4,5,7,8,8,9], "nums2": [9,1,9,3,9,9,7,7,7,7] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [2,2,2,2,10,11,2,2,2,20], "nums2": [2,3,9,9,2,2,12,14,19,2] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [3,15,6,7,7,10,7,7,15,15], "nums2": [15,6,15,7,9,7,12,13,7,7] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [5,20,20,8,8,12,20,19,20,20], "nums2": [20,6,7,20,20,20,15,20,20,20] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [8,8,8,8,8,8,8,8,6,8], "nums2": [1,2,3,4,4,5,5,5,8,8] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [9,3,3,3,3,6,7,7,3,9], "nums2": [3,3,4,5,5,3,3,3,7,3] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [9,8,6,2,1,10,9,7,9,4], "nums2": [6,2,8,2,5,3,6,5,6,10] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [10,2,2,2,3,2,2,2,2,10], "nums2": [1,10,2,2,2,6,6,8,10,2] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [12,19,10,4,17,12,18,6,7,7], "nums2": [20,5,15,7,10,8,2,16,14,1] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums1": [17,31,31,34,31,42,31,65,31,89], "nums2": [89,31,32,31,37,31,45,31,84,31] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [19,7,7,19,9,19,19,19,17,19], "nums2": [6,19,19,8,19,10,10,16,19,19] } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums1": [20,20,7,20,8,11,11,11,11,20], "nums2": [5,6,20,7,20,11,15,18,20,11] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [90,7,90,13,90,33,33,83,33,90], "nums2": [6,90,13,90,22,33,62,33,86,33] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [1,1,1,1,1,10,1,15,16,17,20], "nums2": [1,2,2,9,10,1,14,1,1,1,1] } assert my_solution.minOperations(**test_input) == 5 test_input = { "nums1": [1,1,1,1,3,1,1,6,1,1,8], "nums2": [1,1,1,3,1,3,5,1,6,7,1] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [1,2,2,6,2,2,2,2,2,2,10], "nums2": [10,2,4,2,6,6,7,8,9,9,2] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [1,2,3,20,8,8,19,19,19,20,20], "nums2": [20,20,20,5,20,20,20,19,19,19,19] } assert my_solution.minOperations(**test_input) == 3 test_input = { "nums1": [1,2,4,9,5,9,9,6,7,6,9], "nums2": [9,9,9,4,9,5,5,6,6,7,6] } assert my_solution.minOperations(**test_input) == 5 test_input = { "nums1": [1,7,7,7,7,7,13,16,7,19,19], "nums2": [19,7,10,11,11,12,7,7,16,7,7] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [1,10,4,10,10,10,6,10,7,9,10], "nums2": [10,3,10,5,5,6,10,7,10,9,9] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums1": [1,20,20,7,20,20,13,17,17,19,20], "nums2": [20,2,4,20,7,7,20,20,17,17,17] } assert my_solution.minOperations(**test_input) == 4
1,699,756,200
weekly-contest-371-maximum-strong-pair-xor-ii
https://leetcode.com/problems/maximum-strong-pair-xor-ii
maximum-strong-pair-xor-ii
{ "questionId": "3197", "questionFrontendId": "2935", "title": "Maximum Strong Pair XOR II", "titleSlug": "maximum-strong-pair-xor-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 140, "dislikes": 1, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) 和 (5, 5) 。 这些强数对中的最大异或值是 3 XOR 4 = 7 。 示例 2: 输入:nums = [10,100] 输出:0 解释:数组 nums 中有 2 个强数对:(10, 10) 和 (100, 100) 。 这些强数对中的最大异或值是 10 XOR 10 = 0 ,数对 (100, 100) 的异或值也是 100 XOR 100 = 0 。 示例 3: 输入:nums = [500,520,2500,3000] 输出:1020 解释:数组 nums 中有 6 个强数对:(500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) 和 (3000, 3000) 。 这些强数对中的最大异或值是 500 XOR 520 = 1020 ;另一个异或值非零的数对是 (5, 6) ,其异或值是 2500 XOR 3000 = 636 。 提示: 1 <= nums.length <= 5 * 104 1 <= nums[i] <= 220 - 1 """ class Solution: def maximumStrongPairXor(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) 和 (5, 5) 。 这些强数对中的最大异或值是 3 XOR 4 = 7 。 示例 2: 输入:nums = [10,100] 输出:0 解释:数组 nums 中有 2 个强数对:(10, 10) 和 (100, 100) 。 这些强数对中的最大异或值是 10 XOR 10 = 0 ,数对 (100, 100) 的异或值也是 100 XOR 100 = 0 。 示例 3: 输入:nums = [500,520,2500,3000] 输出:1020 解释:数组 nums 中有 6 个强数对:(500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) 和 (3000, 3000) 。 这些强数对中的最大异或值是 500 XOR 520 = 1020 ;另一个异或值非零的数对是 (5, 6) ,其异或值是 2500 XOR 3000 = 636 。 提示: 1 <= nums.length <= 5 * 104 1 <= nums[i] <= 220 - 1 请完成下面的代码来解决上述问题: ```python class Solution: def maximumStrongPairXor(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [10,100] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [500,520,2500,3000] } assert my_solution.maximumStrongPairXor(**test_input) == 1020 test_input = { "nums": [1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [2,3] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [3,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [5,6] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,1,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,1,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,1,3] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,1,4] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,1,5] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,2,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,2,3] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,2,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,2,5] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,3,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,3,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,3,3] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,3,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [1,3,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,4,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,4,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,4,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [1,4,4] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [1,5,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [1,5,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [1,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [1,5,4] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [1,5,5] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [2,1,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,1,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,1,3] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,1,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,1,5] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,2,2] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [2,2,3] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [2,2,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,2,5] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [2,3,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,3,2] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [2,3,3] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [2,3,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [2,3,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,4,1] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,4,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,4,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [2,4,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,5,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [2,5,2] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [2,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,5,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [2,5,5] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [3,1,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [3,1,2] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [3,1,3] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [3,1,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,1,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [3,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 3 test_input = { "nums": [3,2,2] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [3,2,3] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [3,2,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,2,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [3,3,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [3,3,2] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [3,3,3] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [3,3,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,3,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [3,4,1] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,4,2] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,4,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,4,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,5,1] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [3,5,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [3,5,3] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [3,5,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [3,5,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [4,1,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [4,1,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [4,1,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,1,4] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [4,1,5] } assert my_solution.maximumStrongPairXor(**test_input) == 1 test_input = { "nums": [4,2,1] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [4,2,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [4,2,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,2,4] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [4,2,5] } assert my_solution.maximumStrongPairXor(**test_input) == 6 test_input = { "nums": [4,3,1] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,3,2] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,3,3] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,3,4] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,3,5] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [4,4,1] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [4,4,2] } assert my_solution.maximumStrongPairXor(**test_input) == 6
1,699,756,200
biweekly-contest-117-distribute-candies-among-children-i
https://leetcode.com/problems/distribute-candies-among-children-i
distribute-candies-among-children-i
{ "questionId": "3199", "questionFrontendId": "2928", "title": "Distribute Candies Among Children I", "titleSlug": "distribute-candies-among-children-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 50, "dislikes": 25, "categoryTitle": "Algorithms" }
""" 给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1, 1, 1) ,(1, 2, 0) ,(2, 0, 1) ,(2, 1, 0) 和 (3, 0, 0) 。 提示: * 1 <= n <= 50 * 1 <= limit <= 50 """ class Solution: def distributeCandies(self, n: int, limit: int) -> int:
给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1, 1, 1) ,(1, 2, 0) ,(2, 0, 1) ,(2, 1, 0) 和 (3, 0, 0) 。 提示: * 1 <= n <= 50 * 1 <= limit <= 50 请完成下面的代码来解决上述问题: ```python class Solution: def distributeCandies(self, n: int, limit: int) -> int: ```
my_solution = Solution() test_input = { "n": 5, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 3, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 1, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 21 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 22 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 23 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 24 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 25 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 2, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 2, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 21 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 22 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 23 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 24 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 25 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 3, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 1 test_input = { "n": 3, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 7 test_input = { "n": 3, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 21 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 22 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 23 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 24 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 25 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 4, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 0 test_input = { "n": 4, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 4, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 12 test_input = { "n": 4, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 21 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 22 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 23 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 24 } assert my_solution.distributeCandies(**test_input) == 15
1,699,713,000
biweekly-contest-117-distribute-candies-among-children-ii
https://leetcode.com/problems/distribute-candies-among-children-ii
distribute-candies-among-children-ii
{ "questionId": "3201", "questionFrontendId": "2929", "title": "Distribute Candies Among Children II", "titleSlug": "distribute-candies-among-children-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 62, "dislikes": 95, "categoryTitle": "Algorithms" }
""" 给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1, 1, 1) ,(1, 2, 0) ,(2, 0, 1) ,(2, 1, 0) 和 (3, 0, 0) 。 提示: * 1 <= n <= 106 * 1 <= limit <= 106 """ class Solution: def distributeCandies(self, n: int, limit: int) -> int:
给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1, 1, 1) ,(1, 2, 0) ,(2, 0, 1) ,(2, 1, 0) 和 (3, 0, 0) 。 提示: * 1 <= n <= 106 * 1 <= limit <= 106 请完成下面的代码来解决上述问题: ```python class Solution: def distributeCandies(self, n: int, limit: int) -> int: ```
my_solution = Solution() test_input = { "n": 5, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 3, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 1, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 1, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 2, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 2, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 2, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 3, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 1 test_input = { "n": 3, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 7 test_input = { "n": 3, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 3, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 4, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 0 test_input = { "n": 4, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 6 test_input = { "n": 4, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 12 test_input = { "n": 4, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 4, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 15 test_input = { "n": 5, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 0 test_input = { "n": 5, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 12 test_input = { "n": 5, "limit": 4 } assert my_solution.distributeCandies(**test_input) == 18 test_input = { "n": 5, "limit": 5 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 6 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 7 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 8 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 9 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 10 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 11 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 12 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 13 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 14 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 15 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 16 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 17 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 18 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 19 } assert my_solution.distributeCandies(**test_input) == 21 test_input = { "n": 5, "limit": 20 } assert my_solution.distributeCandies(**test_input) == 21
1,699,713,000
biweekly-contest-117-number-of-strings-which-can-be-rearranged-to-contain-substring
https://leetcode.com/problems/number-of-strings-which-can-be-rearranged-to-contain-substring
number-of-strings-which-can-be-rearranged-to-contain-substring
{ "questionId": "3200", "questionFrontendId": "2930", "title": "Number of Strings Which Can Be Rearranged to Contain Substring", "titleSlug": "number-of-strings-which-can-be-rearranged-to-contain-substring", "isPaidOnly": false, "difficulty": "Medium", "likes": 119, "dislikes": 54, "categoryTitle": "Algorithms" }
""" 给你一个整数 n 。 如果一个字符串 s 只包含小写英文字母,且 将 s 的字符重新排列后,新字符串包含 子字符串 "leet" ,那么我们称字符串 s 是一个 好 字符串。 比方说: * 字符串 "lteer" 是好字符串,因为重新排列后可以得到 "leetr" 。 * "letl" 不是好字符串,因为无法重新排列并得到子字符串 "leet" 。 请你返回长度为 n 的好字符串 总 数目。 由于答案可能很大,将答案对 109 + 7 取余 后返回。 子字符串 是一个字符串中一段连续的字符序列。 示例 1: 输入:n = 4 输出:12 解释:总共有 12 个字符串重新排列后包含子字符串 "leet" :"eelt" ,"eetl" ,"elet" ,"elte" ,"etel" ,"etle" ,"leet" ,"lete" ,"ltee" ,"teel" ,"tele" 和 "tlee" 。 示例 2: 输入:n = 10 输出:83943898 解释:长度为 10 的字符串重新排列后包含子字符串 "leet" 的方案数为 526083947580 。所以答案为 526083947580 % (109 + 7) = 83943898 。 提示: * 1 <= n <= 105 """ class Solution: def stringCount(self, n: int) -> int:
给你一个整数 n 。 如果一个字符串 s 只包含小写英文字母,且 将 s 的字符重新排列后,新字符串包含 子字符串 "leet" ,那么我们称字符串 s 是一个 好 字符串。 比方说: * 字符串 "lteer" 是好字符串,因为重新排列后可以得到 "leetr" 。 * "letl" 不是好字符串,因为无法重新排列并得到子字符串 "leet" 。 请你返回长度为 n 的好字符串 总 数目。 由于答案可能很大,将答案对 109 + 7 取余 后返回。 子字符串 是一个字符串中一段连续的字符序列。 示例 1: 输入:n = 4 输出:12 解释:总共有 12 个字符串重新排列后包含子字符串 "leet" :"eelt" ,"eetl" ,"elet" ,"elte" ,"etel" ,"etle" ,"leet" ,"lete" ,"ltee" ,"teel" ,"tele" 和 "tlee" 。 示例 2: 输入:n = 10 输出:83943898 解释:长度为 10 的字符串重新排列后包含子字符串 "leet" 的方案数为 526083947580 。所以答案为 526083947580 % (109 + 7) = 83943898 。 提示: * 1 <= n <= 105 请完成下面的代码来解决上述问题: ```python class Solution: def stringCount(self, n: int) -> int: ```
my_solution = Solution() test_input = { "n": 4 } assert my_solution.stringCount(**test_input) == 12 test_input = { "n": 10 } assert my_solution.stringCount(**test_input) == 83943898 test_input = { "n": 1 } assert my_solution.stringCount(**test_input) == 0 test_input = { "n": 2 } assert my_solution.stringCount(**test_input) == 0 test_input = { "n": 3 } assert my_solution.stringCount(**test_input) == 0 test_input = { "n": 5 } assert my_solution.stringCount(**test_input) == 1460 test_input = { "n": 6 } assert my_solution.stringCount(**test_input) == 106620 test_input = { "n": 7 } assert my_solution.stringCount(**test_input) == 6058192 test_input = { "n": 8 } assert my_solution.stringCount(**test_input) == 295164156 test_input = { "n": 9 } assert my_solution.stringCount(**test_input) == 947613240 test_input = { "n": 11 } assert my_solution.stringCount(**test_input) == 795234177 test_input = { "n": 12 } assert my_solution.stringCount(**test_input) == 55396773 test_input = { "n": 13 } assert my_solution.stringCount(**test_input) == 968092561 test_input = { "n": 14 } assert my_solution.stringCount(**test_input) == 715599898 test_input = { "n": 15 } assert my_solution.stringCount(**test_input) == 430509685 test_input = { "n": 16 } assert my_solution.stringCount(**test_input) == 462719236 test_input = { "n": 17 } assert my_solution.stringCount(**test_input) == 155543310 test_input = { "n": 18 } assert my_solution.stringCount(**test_input) == 159683962 test_input = { "n": 19 } assert my_solution.stringCount(**test_input) == 808507313 test_input = { "n": 20 } assert my_solution.stringCount(**test_input) == 291395991 test_input = { "n": 21 } assert my_solution.stringCount(**test_input) == 461951930 test_input = { "n": 22 } assert my_solution.stringCount(**test_input) == 871561520 test_input = { "n": 23 } assert my_solution.stringCount(**test_input) == 993268925 test_input = { "n": 24 } assert my_solution.stringCount(**test_input) == 871982505 test_input = { "n": 25 } assert my_solution.stringCount(**test_input) == 935610434 test_input = { "n": 26 } assert my_solution.stringCount(**test_input) == 867518559 test_input = { "n": 27 } assert my_solution.stringCount(**test_input) == 3067523 test_input = { "n": 28 } assert my_solution.stringCount(**test_input) == 716801469 test_input = { "n": 29 } assert my_solution.stringCount(**test_input) == 452206104 test_input = { "n": 30 } assert my_solution.stringCount(**test_input) == 52805056 test_input = { "n": 31 } assert my_solution.stringCount(**test_input) == 61992724 test_input = { "n": 32 } assert my_solution.stringCount(**test_input) == 76928250 test_input = { "n": 33 } assert my_solution.stringCount(**test_input) == 257967635 test_input = { "n": 34 } assert my_solution.stringCount(**test_input) == 549347744 test_input = { "n": 35 } assert my_solution.stringCount(**test_input) == 290653839 test_input = { "n": 36 } assert my_solution.stringCount(**test_input) == 123906995 test_input = { "n": 37 } assert my_solution.stringCount(**test_input) == 41253530 test_input = { "n": 38 } assert my_solution.stringCount(**test_input) == 828924891 test_input = { "n": 39 } assert my_solution.stringCount(**test_input) == 60893212 test_input = { "n": 40 } assert my_solution.stringCount(**test_input) == 618599272 test_input = { "n": 41 } assert my_solution.stringCount(**test_input) == 840600409 test_input = { "n": 42 } assert my_solution.stringCount(**test_input) == 995406621 test_input = { "n": 43 } assert my_solution.stringCount(**test_input) == 991833054 test_input = { "n": 44 } assert my_solution.stringCount(**test_input) == 403185520 test_input = { "n": 45 } assert my_solution.stringCount(**test_input) == 904195428 test_input = { "n": 46 } assert my_solution.stringCount(**test_input) == 643609894 test_input = { "n": 47 } assert my_solution.stringCount(**test_input) == 177947842 test_input = { "n": 48 } assert my_solution.stringCount(**test_input) == 826753905 test_input = { "n": 49 } assert my_solution.stringCount(**test_input) == 855443295 test_input = { "n": 50 } assert my_solution.stringCount(**test_input) == 232825199 test_input = { "n": 51 } assert my_solution.stringCount(**test_input) == 227116084 test_input = { "n": 52 } assert my_solution.stringCount(**test_input) == 417264566 test_input = { "n": 53 } assert my_solution.stringCount(**test_input) == 468973861 test_input = { "n": 54 } assert my_solution.stringCount(**test_input) == 999145386 test_input = { "n": 55 } assert my_solution.stringCount(**test_input) == 721276317 test_input = { "n": 56 } assert my_solution.stringCount(**test_input) == 385673910 test_input = { "n": 57 } assert my_solution.stringCount(**test_input) == 7891114 test_input = { "n": 58 } assert my_solution.stringCount(**test_input) == 85081065 test_input = { "n": 59 } assert my_solution.stringCount(**test_input) == 194677227 test_input = { "n": 60 } assert my_solution.stringCount(**test_input) == 759126147 test_input = { "n": 61 } assert my_solution.stringCount(**test_input) == 273111337 test_input = { "n": 62 } assert my_solution.stringCount(**test_input) == 166598301 test_input = { "n": 63 } assert my_solution.stringCount(**test_input) == 955460796 test_input = { "n": 64 } assert my_solution.stringCount(**test_input) == 685704195 test_input = { "n": 65 } assert my_solution.stringCount(**test_input) == 821093882 test_input = { "n": 66 } assert my_solution.stringCount(**test_input) == 172674695 test_input = { "n": 67 } assert my_solution.stringCount(**test_input) == 464621746 test_input = { "n": 68 } assert my_solution.stringCount(**test_input) == 432202634 test_input = { "n": 69 } assert my_solution.stringCount(**test_input) == 465445347 test_input = { "n": 70 } assert my_solution.stringCount(**test_input) == 654273613 test_input = { "n": 71 } assert my_solution.stringCount(**test_input) == 366864502 test_input = { "n": 72 } assert my_solution.stringCount(**test_input) == 124689502 test_input = { "n": 73 } assert my_solution.stringCount(**test_input) == 419691288 test_input = { "n": 74 } assert my_solution.stringCount(**test_input) == 987033948 test_input = { "n": 75 } assert my_solution.stringCount(**test_input) == 842828500 test_input = { "n": 76 } assert my_solution.stringCount(**test_input) == 409614634 test_input = { "n": 77 } assert my_solution.stringCount(**test_input) == 73844796 test_input = { "n": 78 } assert my_solution.stringCount(**test_input) == 584672527 test_input = { "n": 79 } assert my_solution.stringCount(**test_input) == 113476429 test_input = { "n": 80 } assert my_solution.stringCount(**test_input) == 974106352 test_input = { "n": 81 } assert my_solution.stringCount(**test_input) == 646239862 test_input = { "n": 82 } assert my_solution.stringCount(**test_input) == 420253116 test_input = { "n": 83 } assert my_solution.stringCount(**test_input) == 817573615 test_input = { "n": 84 } assert my_solution.stringCount(**test_input) == 471199144 test_input = { "n": 85 } assert my_solution.stringCount(**test_input) == 567239979 test_input = { "n": 86 } assert my_solution.stringCount(**test_input) == 221534816 test_input = { "n": 87 } assert my_solution.stringCount(**test_input) == 707218848 test_input = { "n": 88 } assert my_solution.stringCount(**test_input) == 687360104 test_input = { "n": 89 } assert my_solution.stringCount(**test_input) == 551986596 test_input = { "n": 90 } assert my_solution.stringCount(**test_input) == 122933939 test_input = { "n": 91 } assert my_solution.stringCount(**test_input) == 427294641 test_input = { "n": 92 } assert my_solution.stringCount(**test_input) == 14022454 test_input = { "n": 93 } assert my_solution.stringCount(**test_input) == 568729284 test_input = { "n": 94 } assert my_solution.stringCount(**test_input) == 653568519 test_input = { "n": 95 } assert my_solution.stringCount(**test_input) == 15509440 test_input = { "n": 96 } assert my_solution.stringCount(**test_input) == 991824044 test_input = { "n": 97 } assert my_solution.stringCount(**test_input) == 690441338 test_input = { "n": 98 } assert my_solution.stringCount(**test_input) == 600462833 test_input = { "n": 99 } assert my_solution.stringCount(**test_input) == 880019356 test_input = { "n": 100 } assert my_solution.stringCount(**test_input) == 86731066
1,699,713,000
biweekly-contest-117-maximum-spending-after-buying-items
https://leetcode.com/problems/maximum-spending-after-buying-items
maximum-spending-after-buying-items
{ "questionId": "3107", "questionFrontendId": "2931", "title": "Maximum Spending After Buying Items", "titleSlug": "maximum-spending-after-buying-items", "isPaidOnly": false, "difficulty": "Hard", "likes": 66, "dislikes": 20, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始大小为 m * n 的整数矩阵 values ,表示 m 个不同商店里 m * n 件不同的物品。每个商店有 n 件物品,第 i 个商店的第 j 件物品的价值为 values[i][j] 。除此以外,第 i 个商店的物品已经按照价值非递增排好序了,也就是说对于所有 0 <= j < n - 1 都有 values[i][j] >= values[i][j + 1] 。 每一天,你可以在一个商店里购买一件物品。具体来说,在第 d 天,你可以: * 选择商店 i 。 * 购买数组中最右边的物品 j ,开销为 values[i][j] * d 。换句话说,选择该商店中还没购买过的物品中最大的下标 j ,并且花费 values[i][j] * d 去购买。 注意,所有物品都视为不同的物品。比方说如果你已经从商店 1 购买了物品 0 ,你还可以在别的商店里购买其他商店的物品 0 。 请你返回购买所有 m * n 件物品需要的 最大开销 。 示例 1: 输入:values = [[8,5,2],[6,4,1],[9,7,3]] 输出:285 解释:第一天,从商店 1 购买物品 2 ,开销为 values[1][2] * 1 = 1 。 第二天,从商店 0 购买物品 2 ,开销为 values[0][2] * 2 = 4 。 第三天,从商店 2 购买物品 2 ,开销为 values[2][2] * 3 = 9 。 第四天,从商店 1 购买物品 1 ,开销为 values[1][1] * 4 = 16 。 第五天,从商店 0 购买物品 1 ,开销为 values[0][1] * 5 = 25 。 第六天,从商店 1 购买物品 0 ,开销为 values[1][0] * 6 = 36 。 第七天,从商店 2 购买物品 1 ,开销为 values[2][1] * 7 = 49 。 第八天,从商店 0 购买物品 0 ,开销为 values[0][0] * 8 = 64 。 第九天,从商店 2 购买物品 0 ,开销为 values[2][0] * 9 = 81 。 所以总开销为 285 。 285 是购买所有 m * n 件物品的最大总开销。 示例 2: 输入:values = [[10,8,6,4,2],[9,7,5,3,2]] 输出:386 解释:第一天,从商店 0 购买物品 4 ,开销为 values[0][4] * 1 = 2 。 第二天,从商店 1 购买物品 4 ,开销为 values[1][4] * 2 = 4 。 第三天,从商店 1 购买物品 3 ,开销为 values[1][3] * 3 = 9 。 第四天,从商店 0 购买物品 3 ,开销为 values[0][3] * 4 = 16 。 第五天,从商店 1 购买物品 2 ,开销为 values[1][2] * 5 = 25 。 第六天,从商店 0 购买物品 2 ,开销为 values[0][2] * 6 = 36 。 第七天,从商店 1 购买物品 1 ,开销为 values[1][1] * 7 = 49 。 第八天,从商店 0 购买物品 1 ,开销为 values[0][1] * 8 = 64 。 第九天,从商店 1 购买物品 0 ,开销为 values[1][0] * 9 = 81 。 第十天,从商店 0 购买物品 0 ,开销为 values[0][0] * 10 = 100 。 所以总开销为 386 。 386 是购买所有 m * n 件物品的最大总开销。 提示: * 1 <= m == values.length <= 10 * 1 <= n == values[i].length <= 104 * 1 <= values[i][j] <= 106 * values[i] 按照非递增顺序排序。 """ class Solution: def maxSpending(self, values: List[List[int]]) -> int:
给你一个下标从 0 开始大小为 m * n 的整数矩阵 values ,表示 m 个不同商店里 m * n 件不同的物品。每个商店有 n 件物品,第 i 个商店的第 j 件物品的价值为 values[i][j] 。除此以外,第 i 个商店的物品已经按照价值非递增排好序了,也就是说对于所有 0 <= j < n - 1 都有 values[i][j] >= values[i][j + 1] 。 每一天,你可以在一个商店里购买一件物品。具体来说,在第 d 天,你可以: * 选择商店 i 。 * 购买数组中最右边的物品 j ,开销为 values[i][j] * d 。换句话说,选择该商店中还没购买过的物品中最大的下标 j ,并且花费 values[i][j] * d 去购买。 注意,所有物品都视为不同的物品。比方说如果你已经从商店 1 购买了物品 0 ,你还可以在别的商店里购买其他商店的物品 0 。 请你返回购买所有 m * n 件物品需要的 最大开销 。 示例 1: 输入:values = [[8,5,2],[6,4,1],[9,7,3]] 输出:285 解释:第一天,从商店 1 购买物品 2 ,开销为 values[1][2] * 1 = 1 。 第二天,从商店 0 购买物品 2 ,开销为 values[0][2] * 2 = 4 。 第三天,从商店 2 购买物品 2 ,开销为 values[2][2] * 3 = 9 。 第四天,从商店 1 购买物品 1 ,开销为 values[1][1] * 4 = 16 。 第五天,从商店 0 购买物品 1 ,开销为 values[0][1] * 5 = 25 。 第六天,从商店 1 购买物品 0 ,开销为 values[1][0] * 6 = 36 。 第七天,从商店 2 购买物品 1 ,开销为 values[2][1] * 7 = 49 。 第八天,从商店 0 购买物品 0 ,开销为 values[0][0] * 8 = 64 。 第九天,从商店 2 购买物品 0 ,开销为 values[2][0] * 9 = 81 。 所以总开销为 285 。 285 是购买所有 m * n 件物品的最大总开销。 示例 2: 输入:values = [[10,8,6,4,2],[9,7,5,3,2]] 输出:386 解释:第一天,从商店 0 购买物品 4 ,开销为 values[0][4] * 1 = 2 。 第二天,从商店 1 购买物品 4 ,开销为 values[1][4] * 2 = 4 。 第三天,从商店 1 购买物品 3 ,开销为 values[1][3] * 3 = 9 。 第四天,从商店 0 购买物品 3 ,开销为 values[0][3] * 4 = 16 。 第五天,从商店 1 购买物品 2 ,开销为 values[1][2] * 5 = 25 。 第六天,从商店 0 购买物品 2 ,开销为 values[0][2] * 6 = 36 。 第七天,从商店 1 购买物品 1 ,开销为 values[1][1] * 7 = 49 。 第八天,从商店 0 购买物品 1 ,开销为 values[0][1] * 8 = 64 。 第九天,从商店 1 购买物品 0 ,开销为 values[1][0] * 9 = 81 。 第十天,从商店 0 购买物品 0 ,开销为 values[0][0] * 10 = 100 。 所以总开销为 386 。 386 是购买所有 m * n 件物品的最大总开销。 提示: * 1 <= m == values.length <= 10 * 1 <= n == values[i].length <= 104 * 1 <= values[i][j] <= 106 * values[i] 按照非递增顺序排序。 请完成下面的代码来解决上述问题: ```python class Solution: def maxSpending(self, values: List[List[int]]) -> int: ```
my_solution = Solution() test_input = { "values": [[8,5,2],[6,4,1],[9,7,3]] } assert my_solution.maxSpending(**test_input) == 285 test_input = { "values": [[10,8,6,4,2],[9,7,5,3,2]] } assert my_solution.maxSpending(**test_input) == 386 test_input = { "values": [[1000000]] } assert my_solution.maxSpending(**test_input) == 1000000 test_input = { "values": [[1]] } assert my_solution.maxSpending(**test_input) == 1 test_input = { "values": [[1],[2]] } assert my_solution.maxSpending(**test_input) == 5 test_input = { "values": [[2],[1]] } assert my_solution.maxSpending(**test_input) == 5 test_input = { "values": [[1],[1]] } assert my_solution.maxSpending(**test_input) == 3 test_input = { "values": [[5,2]] } assert my_solution.maxSpending(**test_input) == 12 test_input = { "values": [[5,5]] } assert my_solution.maxSpending(**test_input) == 15 test_input = { "values": [[7,5]] } assert my_solution.maxSpending(**test_input) == 19 test_input = { "values": [[3,2,1]] } assert my_solution.maxSpending(**test_input) == 14 test_input = { "values": [[2,2,1]] } assert my_solution.maxSpending(**test_input) == 11 test_input = { "values": [[3,3,2]] } assert my_solution.maxSpending(**test_input) == 17 test_input = { "values": [[3],[2],[1]] } assert my_solution.maxSpending(**test_input) == 14 test_input = { "values": [[2],[10],[1]] } assert my_solution.maxSpending(**test_input) == 35 test_input = { "values": [[1000000,1000000,1000000]] } assert my_solution.maxSpending(**test_input) == 6000000 test_input = { "values": [[1000000,1000000,1000000,1000000]] } assert my_solution.maxSpending(**test_input) == 10000000 test_input = { "values": [[1000000],[1000000],[1000000],[1000000]] } assert my_solution.maxSpending(**test_input) == 10000000 test_input = { "values": [[1000000,1000000],[1000000,1000000]] } assert my_solution.maxSpending(**test_input) == 10000000 test_input = { "values": [[2,1],[4,3]] } assert my_solution.maxSpending(**test_input) == 30 test_input = { "values": [[3,1],[4,2]] } assert my_solution.maxSpending(**test_input) == 30 test_input = { "values": [[4,1],[3,2]] } assert my_solution.maxSpending(**test_input) == 30 test_input = { "values": [[15,13,13,12,12,12,12,11,11,11,11,9,9,8,7,5,5,5,1]] } assert my_solution.maxSpending(**test_input) == 2162 test_input = { "values": [[13,13,11,7,2,1],[13,10,10,6,3,3]] } assert my_solution.maxSpending(**test_input) == 776 test_input = { "values": [[12,6],[13,5],[13,3],[6,6],[15,6],[5,4],[6,1]] } assert my_solution.maxSpending(**test_input) == 971 test_input = { "values": [[15,15,14,14,14,10,10,9,9,8,8,8,7,7,7,4]] } assert my_solution.maxSpending(**test_input) == 1585 test_input = { "values": [[11,10],[10,1],[14,6],[13,5],[7,3],[10,10],[10,5]] } assert my_solution.maxSpending(**test_input) == 1061 test_input = { "values": [[15,13,12,7,6,4,1]] } assert my_solution.maxSpending(**test_input) == 298 test_input = { "values": [[8,2],[4,1],[10,4]] } assert my_solution.maxSpending(**test_input) == 133 test_input = { "values": [[10,4],[13,2],[7,5],[15,11]] } assert my_solution.maxSpending(**test_input) == 380 test_input = { "values": [[13,10,10,9,8,5,5,2,1,1]] } assert my_solution.maxSpending(**test_input) == 465 test_input = { "values": [[15,14,8,7,5,5,1]] } assert my_solution.maxSpending(**test_input) == 283 test_input = { "values": [[15,15,14,14,14,14,13,11,10,10,10,9,7,7,6,6,4,4,2,1]] } assert my_solution.maxSpending(**test_input) == 2449 test_input = { "values": [[15,14,11,10,10,6,2],[13,8,8,6,6,3,2]] } assert my_solution.maxSpending(**test_input) == 1084 test_input = { "values": [[15,15,14,13,13,12,11,9,9,9,8,6,5,5,4,3,2,1,1]] } assert my_solution.maxSpending(**test_input) == 2030 test_input = { "values": [[15,10,7],[14,2,2],[14,13,12],[13,13,10]] } assert my_solution.maxSpending(**test_input) == 975 test_input = { "values": [[10,9,3,3],[11,8,7,2],[14,14,13,1],[14,14,11,4],[13,11,9,5]] } assert my_solution.maxSpending(**test_input) == 2338 test_input = { "values": [[11,7,5,2],[15,5,5,3]] } assert my_solution.maxSpending(**test_input) == 307 test_input = { "values": [[15,10,10,9,8,7,4,2,2]] } assert my_solution.maxSpending(**test_input) == 425 test_input = { "values": [[15,13,12,12,10,5,4,3,1]] } assert my_solution.maxSpending(**test_input) == 484 test_input = { "values": [[12,12,12,11,10,10,7,7,6,5,5,3,2,1]] } assert my_solution.maxSpending(**test_input) == 979 test_input = { "values": [[15,13,13,13,12,12,11,11,10,9,9,9,8,6,6,4,4,3,2,1]] } assert my_solution.maxSpending(**test_input) == 2253 test_input = { "values": [[14,11],[11,8],[10,4],[4,3],[9,6],[8,4],[7,7],[10,4],[14,4]] } assert my_solution.maxSpending(**test_input) == 1621 test_input = { "values": [[14,13,13,11,11,8,8,7,6,4,3,2,1]] } assert my_solution.maxSpending(**test_input) == 912 test_input = { "values": [[15,14,14,10,10,9,9,7,7,7,7,7,6,6,6,4,4,3,3]] } assert my_solution.maxSpending(**test_input) == 1823 test_input = { "values": [[14,13,11,10,10,1,1],[15,12,8,6,6,5,5]] } assert my_solution.maxSpending(**test_input) == 1120 test_input = { "values": [[15,14,13,12,11,10,10,10,9,7,6,6,4,3,3,3,2,2,1]] } assert my_solution.maxSpending(**test_input) == 1860 test_input = { "values": [[12,6,5,4,2]] } assert my_solution.maxSpending(**test_input) == 109 test_input = { "values": [[15,15,14,13,12,12,12,11,10,10,9,9,9,8,7,6,3,2]] } assert my_solution.maxSpending(**test_input) == 2006 test_input = { "values": [[15,14,12,11,10,10,7,6,2,1]] } assert my_solution.maxSpending(**test_input) == 610 test_input = { "values": [[15,13,12,11,8,6,5]] } assert my_solution.maxSpending(**test_input) == 328 test_input = { "values": [[15,15,12,10,9,7,7,6,6,5,4,2,1]] } assert my_solution.maxSpending(**test_input) == 896 test_input = { "values": [[15,15,12,12,10,10,9,7,6,4,4,4,1]] } assert my_solution.maxSpending(**test_input) == 969 test_input = { "values": [[15,14,14,13,8,7,7,4,3,3,2,1]] } assert my_solution.maxSpending(**test_input) == 792 test_input = { "values": [[15,15,14,14,14,13,10,8,6,6,5,5,3,3,2,2,1]] } assert my_solution.maxSpending(**test_input) == 1634 test_input = { "values": [[14,12,11,10,10,8,7,7,5,5,3,3,3]] } assert my_solution.maxSpending(**test_input) == 855 test_input = { "values": [[14,14,9,9,4,4,1],[12,12,9,8,7,6,3]] } assert my_solution.maxSpending(**test_input) == 1060 test_input = { "values": [[15,15,15,13,12,12,10,9,9,9,9,7,6,4,4,4,1]] } assert my_solution.maxSpending(**test_input) == 1727 test_input = { "values": [[9,4],[13,4],[13,9],[6,5],[8,2],[13,1]] } assert my_solution.maxSpending(**test_input) == 732 test_input = { "values": [[14,9,9,9,8,7,5],[15,14,10,8,5,3,1]] } assert my_solution.maxSpending(**test_input) == 1094 test_input = { "values": [[15,15,14,13,10,3,1]] } assert my_solution.maxSpending(**test_input) == 354 test_input = { "values": [[13,12],[13,12],[11,8],[14,3]] } assert my_solution.maxSpending(**test_input) == 441 test_input = { "values": [[13,12,11,10,7,5,5,1]] } assert my_solution.maxSpending(**test_input) == 358 test_input = { "values": [[15,10,9,8,8,7,7,5,4,3,1]] } assert my_solution.maxSpending(**test_input) == 582 test_input = { "values": [[10,10,6,4,1],[14,13,13,11,9],[14,11,7,4,3]] } assert my_solution.maxSpending(**test_input) == 1302 test_input = { "values": [[15,14,12,12,11,11,10,9,9,8,8,6,6,4,2,2,1]] } assert my_solution.maxSpending(**test_input) == 1596 test_input = { "values": [[12,5,4,3,3,2,1,1],[13,11,9,9,6,4,3,1]] } assert my_solution.maxSpending(**test_input) == 1019 test_input = { "values": [[15,15,13,11,10,10,7,1,1]] } assert my_solution.maxSpending(**test_input) == 526 test_input = { "values": [[14,12,10,4,3,1],[12,12,8,7,6,2],[15,13,8,5,4,1]] } assert my_solution.maxSpending(**test_input) == 1719 test_input = { "values": [[13,8,8,8,5,4,3],[15,13,9,8,7,3,2]] } assert my_solution.maxSpending(**test_input) == 1006 test_input = { "values": [[13,13,12,12,9]] } assert my_solution.maxSpending(**test_input) == 186 test_input = { "values": [[10,3],[11,9],[10,5],[8,7],[6,1]] } assert my_solution.maxSpending(**test_input) == 472 test_input = { "values": [[15,15,12,12,11,10,9,8,2,2,1,1,1]] } assert my_solution.maxSpending(**test_input) == 941 test_input = { "values": [[14,14,13,13,11,11,10,10,10,9,8,8,7,7,6,6,5,4,3]] } assert my_solution.maxSpending(**test_input) == 2023 test_input = { "values": [[11,10,9,7,6,5,3,1],[14,14,10,9,7,4,4,3]] } assert my_solution.maxSpending(**test_input) == 1270 test_input = { "values": [[6,2],[10,2],[13,5],[8,7],[10,3],[13,2],[13,6]] } assert my_solution.maxSpending(**test_input) == 972 test_input = { "values": [[15,15,15,14,13,12,6,5,5,4]] } assert my_solution.maxSpending(**test_input) == 694 test_input = { "values": [[11,9],[8,1],[2,1],[15,13],[12,4],[14,5],[13,4]] } assert my_solution.maxSpending(**test_input) == 1113 test_input = { "values": [[15,14,14,13,12,11,10,10,5,4,2]] } assert my_solution.maxSpending(**test_input) == 800 test_input = { "values": [[13,12,12,6,5,5,4,2,2,2,1]] } assert my_solution.maxSpending(**test_input) == 523 test_input = { "values": [[13,8],[15,1],[9,1],[13,1]] } assert my_solution.maxSpending(**test_input) == 372 test_input = { "values": [[12,8,4,1],[14,8,8,3]] } assert my_solution.maxSpending(**test_input) == 335 test_input = { "values": [[15,14,14,13,13,13,12,12,9,9,8,7,6,5,5,5,4,3,1]] } assert my_solution.maxSpending(**test_input) == 2113 test_input = { "values": [[13,9,8,2,1],[9,4,3,2,1],[10,8,6,3,1]] } assert my_solution.maxSpending(**test_input) == 877 test_input = { "values": [[8,6],[13,1],[12,8],[8,7]] } assert my_solution.maxSpending(**test_input) == 342 test_input = { "values": [[9,7],[15,15],[9,8],[6,1],[11,4]] } assert my_solution.maxSpending(**test_input) == 585 test_input = { "values": [[14,9,9,8,6,4,2]] } assert my_solution.maxSpending(**test_input) == 257 test_input = { "values": [[15,13,11,11,10,9,5,5,4,4,4,4,3,2]] } assert my_solution.maxSpending(**test_input) == 968 test_input = { "values": [[13,7,7,4],[8,8,6,6]] } assert my_solution.maxSpending(**test_input) == 305 test_input = { "values": [[15,13,12,10,10,10,9,9,9,8,7,6,6,5,4,4,2,1,1]] } assert my_solution.maxSpending(**test_input) == 1805 test_input = { "values": [[13,13,1],[11,5,4],[14,5,1],[15,5,2],[10,1,1],[15,12,1]] } assert my_solution.maxSpending(**test_input) == 1714 test_input = { "values": [[15,15,14,14,11,10,10,8,8,8,3,1,1]] } assert my_solution.maxSpending(**test_input) == 1050 test_input = { "values": [[3,3],[15,10],[14,9]] } assert my_solution.maxSpending(**test_input) == 236 test_input = { "values": [[14,13,11,10,9,7,2]] } assert my_solution.maxSpending(**test_input) == 314 test_input = { "values": [[11,7],[5,3],[11,6],[6,1],[15,13]] } assert my_solution.maxSpending(**test_input) == 550 test_input = { "values": [[14,13,13,12,12,12,11,11,9,8,8,6,5,5,5,5,3,3,2]] } assert my_solution.maxSpending(**test_input) == 1962 test_input = { "values": [[14,14,13,13,13,12,12,11,10,9,9,8,8,7,3,2,2]] } assert my_solution.maxSpending(**test_input) == 1750 test_input = { "values": [[15,8],[12,2],[15,10],[9,3],[5,4],[14,2],[7,6]] } assert my_solution.maxSpending(**test_input) == 1091 test_input = { "values": [[15,9,7,3,3],[7,6,4,3,2],[15,9,8,4,4],[15,10,9,7,4]] } assert my_solution.maxSpending(**test_input) == 1952 test_input = { "values": [[12,12,10,8,8,8,8,6,2,2,1]] } assert my_solution.maxSpending(**test_input) == 585
1,699,713,000
weekly-contest-370-find-champion-i
https://leetcode.com/problems/find-champion-i
find-champion-i
{ "questionId": "3188", "questionFrontendId": "2923", "title": "Find Champion I", "titleSlug": "find-champion-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 84, "dislikes": 20, "categoryTitle": "Algorithms" }
""" 一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。 给你一个下标从 0 开始、大小为 n * n 的二维布尔矩阵 grid 。对于满足 0 <= i, j <= n - 1 且 i != j 的所有 i, j :如果 grid[i][j] == 1,那么 i 队比 j 队 强 ;否则,j 队比 i 队 强 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 返回这场比赛中将会成为冠军的队伍。 示例 1: 输入:grid = [[0,1],[0,0]] 输出:0 解释:比赛中有两支队伍。 grid[0][1] == 1 表示 0 队比 1 队强。所以 0 队是冠军。 示例 2: 输入:grid = [[0,0,1],[1,0,1],[0,0,0]] 输出:1 解释:比赛中有三支队伍。 grid[1][0] == 1 表示 1 队比 0 队强。 grid[1][2] == 1 表示 1 队比 2 队强。 所以 1 队是冠军。 提示: * n == grid.length * n == grid[i].length * 2 <= n <= 100 * grid[i][j] 的值为 0 或 1 * 对于所有 i, grid[i][i] 等于 0. * 对于满足 i != j 的所有 i, j ,grid[i][j] != grid[j][i] 均成立 * 生成的输入满足:如果 a 队比 b 队强,b 队比 c 队强,那么 a 队比 c 队强 """ class Solution: def findChampion(self, grid: List[List[int]]) -> int:
一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。 给你一个下标从 0 开始、大小为 n * n 的二维布尔矩阵 grid 。对于满足 0 <= i, j <= n - 1 且 i != j 的所有 i, j :如果 grid[i][j] == 1,那么 i 队比 j 队 强 ;否则,j 队比 i 队 强 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 返回这场比赛中将会成为冠军的队伍。 示例 1: 输入:grid = [[0,1],[0,0]] 输出:0 解释:比赛中有两支队伍。 grid[0][1] == 1 表示 0 队比 1 队强。所以 0 队是冠军。 示例 2: 输入:grid = [[0,0,1],[1,0,1],[0,0,0]] 输出:1 解释:比赛中有三支队伍。 grid[1][0] == 1 表示 1 队比 0 队强。 grid[1][2] == 1 表示 1 队比 2 队强。 所以 1 队是冠军。 提示: * n == grid.length * n == grid[i].length * 2 <= n <= 100 * grid[i][j] 的值为 0 或 1 * 对于所有 i, grid[i][i] 等于 0. * 对于满足 i != j 的所有 i, j ,grid[i][j] != grid[j][i] 均成立 * 生成的输入满足:如果 a 队比 b 队强,b 队比 c 队强,那么 a 队比 c 队强 请完成下面的代码来解决上述问题: ```python class Solution: def findChampion(self, grid: List[List[int]]) -> int: ```
my_solution = Solution() test_input = { "grid": [[0,1],[0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,0,1],[1,0,1],[0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0],[1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0],[1,0,0],[1,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0],[1,0,1],[1,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,1,0],[0,0,0],[1,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,1],[0,0,0],[0,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1],[0,0,1],[0,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,0,0,0],[1,0,0,0],[1,1,0,0],[1,1,1,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,0,0],[1,0,0,0],[1,1,0,1],[1,1,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0],[1,0,0,1],[1,1,0,1],[1,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0],[1,0,1,0],[1,0,0,0],[1,1,1,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,0,0],[1,0,1,1],[1,0,0,0],[1,0,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,0],[1,0,1,1],[1,0,0,1],[1,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,1],[1,0,0,1],[1,1,0,1],[0,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,1],[1,0,1,1],[1,0,0,1],[0,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,1,0],[1,0,1,0],[0,0,0,0],[1,1,1,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,1,0],[1,0,1,1],[0,0,0,0],[1,0,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,1,1],[1,0,1,1],[0,0,0,0],[0,0,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,1,1],[1,0,1,1],[0,0,0,1],[0,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,1,0,0],[0,0,0,0],[1,1,0,0],[1,1,1,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,0,0],[0,0,0,0],[1,1,0,1],[1,1,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,0,1],[0,0,0,0],[1,1,0,1],[0,1,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,0,1],[0,0,0,1],[1,1,0,1],[0,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,1,0],[0,0,0,0],[0,1,0,0],[1,1,1,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,0],[0,0,1,0],[0,0,0,0],[1,1,1,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,1],[0,0,0,0],[0,1,0,0],[0,1,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1],[0,0,0,0],[0,1,0,1],[0,1,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1],[0,0,0,1],[0,1,0,1],[0,0,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1],[0,0,1,0],[0,0,0,0],[0,1,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1],[0,0,1,1],[0,0,0,0],[0,0,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1],[0,0,1,1],[0,0,0,1],[0,0,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,0,0],[1,1,1,0,1],[1,1,1,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,0],[1,1,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1],[1,1,0,0,0],[1,1,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1],[1,1,0,0,1],[1,1,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,1],[1,1,0,0,1],[1,1,1,0,1],[1,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,0,0,0],[1,0,0,0,1],[1,1,0,1,1],[1,1,0,0,1],[1,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0,0],[1,0,0,1,0],[1,1,0,1,0],[1,0,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,0,0,0],[1,0,0,1,0],[1,1,0,1,1],[1,0,0,0,0],[1,1,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0,0],[1,0,0,1,1],[1,1,0,1,1],[1,0,0,0,1],[1,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0,0],[1,0,1,0,1],[1,0,0,0,0],[1,1,1,0,1],[1,0,1,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,0],[1,0,0,0,0],[1,0,1,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,0],[1,0,0,1,0],[1,0,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,1],[1,0,0,0,0],[1,0,1,0,1],[1,0,1,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,0,0],[1,0,1,1,1],[1,0,0,1,1],[1,0,0,0,1],[1,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,0,1],[1,0,0,0,1],[1,1,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,0,0,1],[1,0,0,0,1],[1,1,0,1,1],[1,1,0,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0,1],[1,0,0,1,1],[1,1,0,1,1],[1,0,0,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,0,1],[1,0,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,1,0],[1,0,0,1,0],[1,1,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,0,1,0],[1,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0],[1,1,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,1,0],[1,0,0,1,1],[1,1,0,1,1],[0,0,0,0,0],[1,0,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,1,0],[1,0,1,1,0],[1,0,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,0,1,0],[1,0,1,1,1],[1,0,0,1,0],[0,0,0,0,0],[1,0,1,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,1,0],[1,0,1,1,1],[1,0,0,1,1],[0,0,0,0,0],[1,0,0,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,1,1],[1,0,0,1,1],[1,1,0,1,1],[0,0,0,0,0],[0,0,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,1,1],[1,0,0,1,1],[1,1,0,1,1],[0,0,0,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,0,0,1,1],[1,0,1,1,1],[1,0,0,1,1],[0,0,0,0,0],[0,0,0,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,0,1,1],[1,0,1,1,1],[1,0,0,1,1],[0,0,0,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,1,0,0],[1,0,1,0,0],[0,0,0,0,0],[1,1,1,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,1,0,0],[1,0,1,1,0],[0,0,0,0,0],[1,0,1,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,1,0,0],[1,0,1,1,1],[0,0,0,0,0],[1,0,1,0,1],[1,0,1,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,1,0,1],[1,0,1,0,1],[0,0,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,0,1,1,0],[1,0,1,1,0],[0,0,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,0,1,1,0],[1,0,1,1,1],[0,0,0,0,0],[0,0,1,0,0],[1,0,1,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,1,1,1],[1,0,1,1,1],[0,0,0,1,0],[0,0,0,0,0],[0,0,1,1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0,1,1,1],[1,0,1,1,1],[0,0,0,1,1],[0,0,0,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,1,0,0,0],[0,0,0,0,0],[1,1,0,0,0],[1,1,1,0,1],[1,1,1,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,0,0,0],[0,0,0,0,0],[1,1,0,1,1],[1,1,0,0,0],[1,1,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,0,0,1],[0,0,0,0,0],[1,1,0,0,1],[1,1,1,0,1],[0,1,0,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,0,0,1],[0,0,0,0,0],[1,1,0,1,1],[1,1,0,0,1],[0,1,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,0,1,0],[0,0,0,0,0],[1,1,0,1,0],[0,1,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,1,0,1,0],[0,0,0,0,0],[1,1,0,1,1],[0,1,0,0,0],[1,1,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,0,1,0],[0,0,0,1,0],[1,1,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,1,0,1,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0],[1,1,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,0,1,1],[0,0,0,0,0],[1,1,0,1,1],[0,1,0,0,1],[0,1,0,0,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,0,1,1],[0,0,0,1,1],[1,1,0,1,1],[0,0,0,0,0],[0,0,0,1,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "grid": [[0,1,1,0,0],[0,0,0,0,0],[0,1,0,0,0],[1,1,1,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,1,1,0,0],[0,0,1,0,0],[0,0,0,0,0],[1,1,1,0,1],[1,1,1,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,0,1],[0,0,0,0,0],[0,1,0,0,1],[1,1,1,0,1],[0,1,0,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,0,1],[0,0,0,0,1],[0,1,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,0,1],[0,0,1,0,0],[0,0,0,0,0],[1,1,1,0,1],[0,1,1,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,0,1],[0,0,1,0,1],[0,0,0,0,0],[1,1,1,0,1],[0,0,1,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,0,1],[0,0,1,0,1],[0,0,0,0,1],[1,1,1,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "grid": [[0,1,1,1,0],[0,0,0,1,0],[0,1,0,1,0],[0,0,0,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,1,1,1,0],[0,0,1,0,0],[0,0,0,0,0],[0,1,1,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,1,1,1,0],[0,0,1,1,0],[0,0,0,0,0],[0,0,1,0,0],[1,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,0,0],[0,1,1,0,0],[0,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,0,0],[0,1,1,0,1],[0,1,1,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,1,0,1],[0,1,0,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,1,0],[0,1,0,0,0],[0,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,1,1],[0,1,0,0,0],[0,1,0,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,0],[0,1,0,1,1],[0,1,0,0,1],[0,1,0,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,0,0,1],[0,1,0,0,1],[0,1,1,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,1,0,0],[0,0,0,0,0],[0,1,1,0,0],[0,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,1,0,0],[0,0,0,0,0],[0,1,1,0,1],[0,1,1,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,1,0,1],[0,0,0,0,1],[0,1,1,0,1],[0,0,0,0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,1,1,1,1],[0,0,1,1,0],[0,0,0,0,0],[0,0,1,0,0],[0,1,1,1,0]] } assert my_solution.findChampion(**test_input) == 0
1,699,151,400
weekly-contest-370-find-champion-ii
https://leetcode.com/problems/find-champion-ii
find-champion-ii
{ "questionId": "3189", "questionFrontendId": "2924", "title": "Find Champion II", "titleSlug": "find-champion-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 92, "dislikes": 7, "categoryTitle": "Algorithms" }
""" 一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。每支队伍也是 有向无环图(DAG) 上的一个节点。 给你一个整数 n 和一个下标从 0 开始、长度为 m 的二维整数数组 edges 表示这个有向无环图,其中 edges[i] = [ui, vi] 表示图中存在一条从 ui 队到 vi 队的有向边。 从 a 队到 b 队的有向边意味着 a 队比 b 队 强 ,也就是 b 队比 a 队 弱 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 如果这场比赛存在 唯一 一个冠军,则返回将会成为冠军的队伍。否则,返回 -1 。 注意 * 环 是形如 a1, a2, ..., an, an+1 的一个序列,且满足:节点 a1 与节点 an+1 是同一个节点;节点 a1, a2, ..., an 互不相同;对于范围 [1, n] 中的每个 i ,均存在一条从节点 ai 到节点 ai+1 的有向边。 * 有向无环图 是不存在任何环的有向图。 示例 1: [https://assets.leetcode.com/uploads/2023/10/19/graph-3.png] 输入:n = 3, edges = [[0,1],[1,2]] 输出:0 解释:1 队比 0 队弱。2 队比 1 队弱。所以冠军是 0 队。 示例 2: [https://assets.leetcode.com/uploads/2023/10/19/graph-4.png] 输入:n = 4, edges = [[0,2],[1,3],[1,2]] 输出:-1 解释:2 队比 0 队和 1 队弱。3 队比 1 队弱。但是 1 队和 0 队之间不存在强弱对比。所以答案是 -1 。 提示: * 1 <= n <= 100 * m == edges.length * 0 <= m <= n * (n - 1) / 2 * edges[i].length == 2 * 0 <= edge[i][j] <= n - 1 * edges[i][0] != edges[i][1] * 生成的输入满足:如果 a 队比 b 队强,就不存在 b 队比 a 队强 * 生成的输入满足:如果 a 队比 b 队强,b 队比 c 队强,那么 a 队比 c 队强 """ class Solution: def findChampion(self, n: int, edges: List[List[int]]) -> int:
一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。每支队伍也是 有向无环图(DAG) 上的一个节点。 给你一个整数 n 和一个下标从 0 开始、长度为 m 的二维整数数组 edges 表示这个有向无环图,其中 edges[i] = [ui, vi] 表示图中存在一条从 ui 队到 vi 队的有向边。 从 a 队到 b 队的有向边意味着 a 队比 b 队 强 ,也就是 b 队比 a 队 弱 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 如果这场比赛存在 唯一 一个冠军,则返回将会成为冠军的队伍。否则,返回 -1 。 注意 * 环 是形如 a1, a2, ..., an, an+1 的一个序列,且满足:节点 a1 与节点 an+1 是同一个节点;节点 a1, a2, ..., an 互不相同;对于范围 [1, n] 中的每个 i ,均存在一条从节点 ai 到节点 ai+1 的有向边。 * 有向无环图 是不存在任何环的有向图。 示例 1: [https://assets.leetcode.com/uploads/2023/10/19/graph-3.png] 输入:n = 3, edges = [[0,1],[1,2]] 输出:0 解释:1 队比 0 队弱。2 队比 1 队弱。所以冠军是 0 队。 示例 2: [https://assets.leetcode.com/uploads/2023/10/19/graph-4.png] 输入:n = 4, edges = [[0,2],[1,3],[1,2]] 输出:-1 解释:2 队比 0 队和 1 队弱。3 队比 1 队弱。但是 1 队和 0 队之间不存在强弱对比。所以答案是 -1 。 提示: * 1 <= n <= 100 * m == edges.length * 0 <= m <= n * (n - 1) / 2 * edges[i].length == 2 * 0 <= edge[i][j] <= n - 1 * edges[i][0] != edges[i][1] * 生成的输入满足:如果 a 队比 b 队强,就不存在 b 队比 a 队强 * 生成的输入满足:如果 a 队比 b 队强,b 队比 c 队强,那么 a 队比 c 队强 请完成下面的代码来解决上述问题: ```python class Solution: def findChampion(self, n: int, edges: List[List[int]]) -> int: ```
my_solution = Solution() test_input = { "n": 3, "edges": [[0,1],[1,2]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 4, "edges": [[0,2],[1,3],[1,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 1, "edges": [] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 2, "edges": [] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 2, "edges": [[0,1]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 2, "edges": [[1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 3, "edges": [] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 3, "edges": [[0,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 3, "edges": [[0,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 3, "edges": [[1,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 3, "edges": [[2,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 3, "edges": [[0,1],[2,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 3, "edges": [[0,2],[0,1]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 3, "edges": [[0,2],[1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 3, "edges": [[2,0],[1,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 3, "edges": [[2,0],[2,1]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "n": 3, "edges": [[2,1],[2,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "n": 3, "edges": [[0,1],[1,2],[0,2]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 3, "edges": [[0,1],[2,1],[0,2]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 3, "edges": [[0,2],[0,1],[1,2]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 3, "edges": [[0,2],[0,1],[2,1]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 3, "edges": [[0,2],[1,2],[1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 3, "edges": [[1,0],[0,2],[1,2]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 3, "edges": [[2,1],[1,0],[2,0]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "n": 4, "edges": [] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[0,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[1,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[1,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[3,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[0,1],[2,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[0,3],[2,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[1,3],[2,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[2,1],[1,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[3,0],[3,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[0,1],[2,0],[2,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[0,2],[3,2],[1,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[1,0],[2,3],[1,2]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 4, "edges": [[1,2],[0,3],[1,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[1,2],[1,0],[1,3]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 4, "edges": [[1,3],[1,2],[0,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[1,3],[3,0],[2,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[3,1],[2,0],[1,2]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 4, "edges": [[3,1],[2,1],[0,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[0,2],[0,3],[1,2],[1,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 4, "edges": [[2,0],[2,3],[3,1],[2,1]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "n": 4, "edges": [[2,1],[1,0],[3,0],[2,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[3,0],[3,1],[2,1],[0,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[3,0],[1,0],[1,2],[3,2],[2,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 4, "edges": [[3,0],[2,0],[1,0],[2,3],[1,2]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 4, "edges": [[3,2],[0,1],[3,0],[3,1],[2,0]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 4, "edges": [[0,3],[2,3],[2,1],[1,0],[2,0],[1,3]] } assert my_solution.findChampion(**test_input) == 2 test_input = { "n": 4, "edges": [[1,2],[2,3],[0,2],[0,1],[0,3],[1,3]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 4, "edges": [[2,1],[3,1],[3,0],[3,2],[2,0],[0,1]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 4, "edges": [[2,3],[2,1],[0,1],[0,3],[3,1],[0,2]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 5, "edges": [] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[0,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[2,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[4,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[1,4],[2,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[2,0],[4,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[3,2],[0,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[3,4],[2,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[0,4],[1,4],[0,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[1,3],[4,2],[1,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[2,3],[4,1],[3,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[0,4],[2,0],[1,3],[2,4]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[0,1],[2,1],[4,1],[4,2],[4,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[0,2],[2,1],[3,2],[4,1],[0,4]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[2,3],[0,4],[1,4],[1,0],[4,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[3,1],[0,2],[4,2],[0,1],[1,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[3,2],[3,4],[3,0],[3,1],[0,2]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 5, "edges": [[4,0],[3,0],[2,4],[3,4],[4,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[4,3],[1,0],[1,2],[3,2],[4,1]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "n": 5, "edges": [[2,1],[0,3],[0,1],[0,4],[0,2],[4,1]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 5, "edges": [[2,1],[4,1],[3,0],[2,0],[3,4],[3,2]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 5, "edges": [[0,1],[0,4],[2,0],[3,4],[3,1],[2,1],[3,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 5, "edges": [[1,4],[3,1],[0,1],[3,0],[0,2],[2,4],[3,4]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 5, "edges": [[3,2],[1,2],[2,0],[2,4],[1,4],[3,1],[3,4]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 5, "edges": [[0,4],[0,3],[4,3],[4,2],[1,2],[4,1],[0,1],[3,2]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 5, "edges": [[4,3],[4,2],[4,1],[2,3],[4,0],[3,1],[2,0],[0,3]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "n": 5, "edges": [[1,2],[1,4],[2,3],[0,2],[1,0],[1,3],[0,3],[4,3],[0,4]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 5, "edges": [[1,3],[3,0],[3,4],[2,0],[3,2],[0,4],[2,4],[1,0],[1,2]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "n": 5, "edges": [[3,0],[4,0],[3,2],[0,1],[0,2],[4,3],[1,2],[4,2],[3,1]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "n": 5, "edges": [[4,0],[2,3],[4,3],[4,2],[2,0],[4,1],[1,3],[1,0],[3,0]] } assert my_solution.findChampion(**test_input) == 4 test_input = { "n": 5, "edges": [[0,2],[1,3],[4,1],[4,2],[2,1],[0,3],[0,1],[2,3],[0,4],[4,3]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 5, "edges": [[2,0],[4,0],[3,4],[4,2],[1,2],[1,0],[3,1],[3,0],[1,4],[3,2]] } assert my_solution.findChampion(**test_input) == 3 test_input = { "n": 6, "edges": [] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[2,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[2,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[3,5]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[5,4]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[1,2],[4,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[0,4],[4,5],[3,1]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[0,4],[5,2],[5,4],[3,0],[1,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[1,0],[1,4],[0,2],[3,5],[3,4]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[5,0],[2,4],[0,4],[3,2],[2,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[1,2],[3,2],[3,5],[4,0],[1,5],[0,5]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[3,2],[1,2],[3,0],[5,0],[5,1],[5,3]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[4,2],[0,4],[1,3],[3,4],[1,2],[1,0]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 6, "edges": [[0,5],[2,0],[1,5],[3,2],[2,1],[2,4],[4,5]] } assert my_solution.findChampion(**test_input) == 3
1,699,151,400
weekly-contest-370-maximum-score-after-applying-operations-on-a-tree
https://leetcode.com/problems/maximum-score-after-applying-operations-on-a-tree
maximum-score-after-applying-operations-on-a-tree
{ "questionId": "3191", "questionFrontendId": "2925", "title": "Maximum Score After Applying Operations on a Tree", "titleSlug": "maximum-score-after-applying-operations-on-a-tree", "isPaidOnly": false, "difficulty": "Medium", "likes": 243, "dislikes": 45, "categoryTitle": "Algorithms" }
""" 有一棵 n 个节点的无向树,节点编号为 0 到 n - 1 ,根节点编号为 0 。给你一个长度为 n - 1 的二维整数数组 edges 表示这棵树,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。 同时给你一个长度为 n 下标从 0 开始的整数数组 values ,其中 values[i] 表示第 i 个节点的值。 一开始你的分数为 0 ,每次操作中,你将执行: * 选择节点 i 。 * 将 values[i] 加入你的分数。 * 将 values[i] 变为 0 。 如果从根节点出发,到任意叶子节点经过的路径上的节点值之和都不等于 0 ,那么我们称这棵树是 健康的 。 你可以对这棵树执行任意次操作,但要求执行完所有操作以后树是 健康的 ,请你返回你可以获得的 最大分数 。 示例 1: [https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png] 输入:edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1] 输出:11 解释:我们可以选择节点 1 ,2 ,3 ,4 和 5 。根节点的值是非 0 的。所以从根出发到任意叶子节点路径上节点值之和都不为 0 。所以树是健康的。你的得分之和为 values[1] + values[2] + values[3] + values[4] + values[5] = 11 。 11 是你对树执行任意次操作以后可以获得的最大得分之和。 示例 2: [https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png] 输入:edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5] 输出:40 解释:我们选择节点 0 ,2 ,3 和 4 。 - 从 0 到 4 的节点值之和为 10 。 - 从 0 到 3 的节点值之和为 10 。 - 从 0 到 5 的节点值之和为 3 。 - 从 0 到 6 的节点值之和为 5 。 所以树是健康的。你的得分之和为 values[0] + values[2] + values[3] + values[4] = 40 。 40 是你对树执行任意次操作以后可以获得的最大得分之和。 提示: * 2 <= n <= 2 * 104 * edges.length == n - 1 * edges[i].length == 2 * 0 <= ai, bi < n * values.length == n * 1 <= values[i] <= 109 * 输入保证 edges 构成一棵合法的树。 """ class Solution: def maximumScoreAfterOperations(self, edges: List[List[int]], values: List[int]) -> int:
有一棵 n 个节点的无向树,节点编号为 0 到 n - 1 ,根节点编号为 0 。给你一个长度为 n - 1 的二维整数数组 edges 表示这棵树,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。 同时给你一个长度为 n 下标从 0 开始的整数数组 values ,其中 values[i] 表示第 i 个节点的值。 一开始你的分数为 0 ,每次操作中,你将执行: * 选择节点 i 。 * 将 values[i] 加入你的分数。 * 将 values[i] 变为 0 。 如果从根节点出发,到任意叶子节点经过的路径上的节点值之和都不等于 0 ,那么我们称这棵树是 健康的 。 你可以对这棵树执行任意次操作,但要求执行完所有操作以后树是 健康的 ,请你返回你可以获得的 最大分数 。 示例 1: [https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png] 输入:edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1] 输出:11 解释:我们可以选择节点 1 ,2 ,3 ,4 和 5 。根节点的值是非 0 的。所以从根出发到任意叶子节点路径上节点值之和都不为 0 。所以树是健康的。你的得分之和为 values[1] + values[2] + values[3] + values[4] + values[5] = 11 。 11 是你对树执行任意次操作以后可以获得的最大得分之和。 示例 2: [https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png] 输入:edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5] 输出:40 解释:我们选择节点 0 ,2 ,3 和 4 。 - 从 0 到 4 的节点值之和为 10 。 - 从 0 到 3 的节点值之和为 10 。 - 从 0 到 5 的节点值之和为 3 。 - 从 0 到 6 的节点值之和为 5 。 所以树是健康的。你的得分之和为 values[0] + values[2] + values[3] + values[4] = 40 。 40 是你对树执行任意次操作以后可以获得的最大得分之和。 提示: * 2 <= n <= 2 * 104 * edges.length == n - 1 * edges[i].length == 2 * 0 <= ai, bi < n * values.length == n * 1 <= values[i] <= 109 * 输入保证 edges 构成一棵合法的树。 请完成下面的代码来解决上述问题: ```python class Solution: def maximumScoreAfterOperations(self, edges: List[List[int]], values: List[int]) -> int: ```
my_solution = Solution() test_input = { "edges": [[0,1],[0,2],[0,3],[2,4],[4,5]], "values": [5,2,5,2,1,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 11 test_input = { "edges": [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], "values": [20,10,9,7,4,3,5] } assert my_solution.maximumScoreAfterOperations(**test_input) == 40 test_input = { "edges": [[0,1]], "values": [1,2] } assert my_solution.maximumScoreAfterOperations(**test_input) == 2 test_input = { "edges": [[0,1]], "values": [2,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 2 test_input = { "edges": [[0,1],[0,2]], "values": [1000000000,1000000000,1000000000] } assert my_solution.maximumScoreAfterOperations(**test_input) == 2000000000 test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [1000000000,1000000000,1000000000,1000000000] } assert my_solution.maximumScoreAfterOperations(**test_input) == 3000000000 test_input = { "edges": [[7,0],[3,1],[6,2],[4,3],[4,5],[4,6],[4,7]], "values": [2,16,23,17,22,21,8,6] } assert my_solution.maximumScoreAfterOperations(**test_input) == 113 test_input = { "edges": [[3,1],[0,2],[0,3]], "values": [21,12,19,5] } assert my_solution.maximumScoreAfterOperations(**test_input) == 36 test_input = { "edges": [[2,0],[4,1],[5,3],[4,6],[2,4],[5,2],[5,7]], "values": [12,12,7,9,2,11,12,25] } assert my_solution.maximumScoreAfterOperations(**test_input) == 83 test_input = { "edges": [[1,0],[9,1],[6,2],[7,4],[3,5],[7,3],[9,6],[7,8],[7,9]], "values": [14,17,13,18,17,10,23,19,22,2] } assert my_solution.maximumScoreAfterOperations(**test_input) == 153 test_input = { "edges": [[5,0],[4,3],[2,5],[6,2],[4,6],[1,4],[1,7]], "values": [15,12,13,23,8,1,2,23] } assert my_solution.maximumScoreAfterOperations(**test_input) == 96 test_input = { "edges": [[0,2],[1,3],[0,5],[1,0],[4,1],[4,6]], "values": [22,25,4,21,8,20,4] } assert my_solution.maximumScoreAfterOperations(**test_input) == 82 test_input = { "edges": [[4,1],[6,3],[2,4],[0,2],[9,5],[0,6],[9,7],[0,8],[0,9]], "values": [3,18,10,16,9,3,25,17,8,9] } assert my_solution.maximumScoreAfterOperations(**test_input) == 115 test_input = { "edges": [[6,1],[3,4],[0,3],[2,0],[5,2],[5,6]], "values": [25,20,16,2,13,8,19] } assert my_solution.maximumScoreAfterOperations(**test_input) == 93 test_input = { "edges": [[9,2],[5,4],[5,6],[1,5],[8,1],[0,7],[3,0],[9,3],[8,9]], "values": [21,13,10,14,20,11,19,22,3,16] } assert my_solution.maximumScoreAfterOperations(**test_input) == 128 test_input = { "edges": [[2,4],[1,5],[0,1],[2,0],[3,2],[3,6]], "values": [17,5,24,18,6,16,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 75 test_input = { "edges": [[1,2],[4,1],[3,6],[4,3],[5,4],[0,5],[0,7]], "values": [8,13,19,13,4,3,24,25] } assert my_solution.maximumScoreAfterOperations(**test_input) == 101 test_input = { "edges": [[9,0],[7,1],[6,3],[8,4],[2,5],[9,6],[2,8],[7,2],[7,9]], "values": [4,13,21,1,25,8,21,22,9,18] } assert my_solution.maximumScoreAfterOperations(**test_input) == 138 test_input = { "edges": [[1,0],[4,3],[1,5],[6,1],[4,6],[2,7],[4,2],[4,8]], "values": [10,5,25,19,2,20,15,3,3] } assert my_solution.maximumScoreAfterOperations(**test_input) == 97 test_input = { "edges": [[6,1],[3,4],[2,5],[0,6],[3,0],[2,3],[2,7]], "values": [2,23,10,20,22,10,6,24] } assert my_solution.maximumScoreAfterOperations(**test_input) == 115 test_input = { "edges": [[2,1],[0,2],[5,0],[4,3],[5,4],[5,6]], "values": [9,21,12,20,5,2,13] } assert my_solution.maximumScoreAfterOperations(**test_input) == 73 test_input = { "edges": [[8,1],[7,4],[0,5],[2,0],[3,2],[8,3],[7,6],[7,8]], "values": [23,24,25,12,12,7,1,17,17] } assert my_solution.maximumScoreAfterOperations(**test_input) == 119 test_input = { "edges": [[3,0],[3,1],[2,3]], "values": [19,8,8,5] } assert my_solution.maximumScoreAfterOperations(**test_input) == 35 test_input = { "edges": [[5,0],[7,1],[3,2],[6,4],[6,5],[3,6],[3,7]], "values": [19,7,17,9,13,7,25,3] } assert my_solution.maximumScoreAfterOperations(**test_input) == 93 test_input = { "edges": [[3,1],[2,3],[0,2],[0,4]], "values": [14,15,18,15,20] } assert my_solution.maximumScoreAfterOperations(**test_input) == 68 test_input = { "edges": [[6,0],[2,1],[6,2],[6,4],[5,7],[6,5],[3,6],[9,3],[8,9]], "values": [17,20,17,13,5,12,8,12,14,25] } assert my_solution.maximumScoreAfterOperations(**test_input) == 135 test_input = { "edges": [[4,1],[6,2],[9,3],[0,6],[0,7],[9,0],[4,8],[5,4],[5,9]], "values": [11,16,10,25,21,25,15,10,5,7] } assert my_solution.maximumScoreAfterOperations(**test_input) == 134 test_input = { "edges": [[1,0],[3,1],[2,3]], "values": [25,19,12,2] } assert my_solution.maximumScoreAfterOperations(**test_input) == 56 test_input = { "edges": [[4,1],[0,2],[4,0],[3,4]], "values": [12,24,1,11,3] } assert my_solution.maximumScoreAfterOperations(**test_input) == 47 test_input = { "edges": [[1,0],[5,1],[2,4],[3,2],[3,5]], "values": [21,2,17,18,22,16] } assert my_solution.maximumScoreAfterOperations(**test_input) == 94 test_input = { "edges": [[5,1],[4,3],[2,4],[6,2],[0,5],[0,6]], "values": [18,24,5,20,23,6,7] } assert my_solution.maximumScoreAfterOperations(**test_input) == 92 test_input = { "edges": [[4,1],[6,3],[2,4],[5,2],[0,5],[0,6]], "values": [19,2,23,18,3,12,9] } assert my_solution.maximumScoreAfterOperations(**test_input) == 75 test_input = { "edges": [[5,0],[2,1],[6,3],[5,4],[2,5],[2,6]], "values": [22,11,2,11,3,11,17] } assert my_solution.maximumScoreAfterOperations(**test_input) == 72 test_input = { "edges": [[6,0],[4,1],[3,2],[6,5],[4,6],[3,4],[3,7],[3,8]], "values": [18,20,14,23,20,8,24,12,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 122 test_input = { "edges": [[1,2],[0,1],[0,3],[0,4]], "values": [6,8,2,16,6] } assert my_solution.maximumScoreAfterOperations(**test_input) == 32 test_input = { "edges": [[6,1],[3,4],[3,5],[0,3],[2,0],[2,6]], "values": [5,25,2,12,15,3,3] } assert my_solution.maximumScoreAfterOperations(**test_input) == 60 test_input = { "edges": [[4,0],[2,1],[2,3],[2,4]], "values": [9,5,14,24,19] } assert my_solution.maximumScoreAfterOperations(**test_input) == 62 test_input = { "edges": [[4,0],[2,1],[6,3],[2,5],[4,6],[2,4],[7,2],[7,8]], "values": [19,10,14,18,17,8,2,8,24] } assert my_solution.maximumScoreAfterOperations(**test_input) == 104 test_input = { "edges": [[6,0],[7,1],[5,3],[2,4],[8,5],[6,7],[2,6],[2,8]], "values": [8,8,19,17,24,1,7,18,12] } assert my_solution.maximumScoreAfterOperations(**test_input) == 107 test_input = { "edges": [[6,0],[3,1],[3,2],[3,6],[4,3],[5,4],[5,7]], "values": [15,3,25,2,10,11,10,13] } assert my_solution.maximumScoreAfterOperations(**test_input) == 87 test_input = { "edges": [[7,3],[2,5],[4,2],[0,4],[7,0],[1,6],[1,7]], "values": [1,19,5,1,18,24,4,20] } assert my_solution.maximumScoreAfterOperations(**test_input) == 91 test_input = { "edges": [[7,3],[2,4],[9,2],[1,5],[1,7],[0,1],[9,0],[6,8],[6,9]], "values": [21,10,9,25,7,20,5,8,20,5] } assert my_solution.maximumScoreAfterOperations(**test_input) == 115 test_input = { "edges": [[3,1],[4,5],[0,4],[2,0],[3,2],[3,6]], "values": [13,11,16,12,20,1,7] } assert my_solution.maximumScoreAfterOperations(**test_input) == 67 test_input = { "edges": [[2,0],[3,1],[6,2],[6,3],[5,4],[7,5],[6,7]], "values": [21,20,8,21,11,12,23,4] } assert my_solution.maximumScoreAfterOperations(**test_input) == 112 test_input = { "edges": [[0,2],[7,4],[1,6],[5,1],[3,5],[0,3],[0,7]], "values": [8,6,9,5,4,1,8,6] } assert my_solution.maximumScoreAfterOperations(**test_input) == 39 test_input = { "edges": [[5,1],[8,3],[0,4],[8,0],[2,5],[2,7],[9,2],[6,8],[6,9]], "values": [2,22,2,19,25,14,11,24,7,6] } assert my_solution.maximumScoreAfterOperations(**test_input) == 130 test_input = { "edges": [[3,0],[3,1],[2,3]], "values": [24,24,4,12] } assert my_solution.maximumScoreAfterOperations(**test_input) == 52 test_input = { "edges": [[2,0],[3,1],[2,3]], "values": [7,10,13,8] } assert my_solution.maximumScoreAfterOperations(**test_input) == 31 test_input = { "edges": [[1,0],[3,1],[6,4],[3,5],[2,3],[2,6]], "values": [6,14,4,17,16,19,24] } assert my_solution.maximumScoreAfterOperations(**test_input) == 94 test_input = { "edges": [[0,1],[3,0],[8,3],[2,4],[8,5],[2,6],[8,2],[7,8]], "values": [22,19,10,16,14,11,2,17,9] } assert my_solution.maximumScoreAfterOperations(**test_input) == 98 test_input = { "edges": [[5,1],[5,2],[0,3],[0,4],[0,5]], "values": [21,18,2,20,1,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 42 test_input = { "edges": [[6,0],[3,2],[1,3],[6,4],[1,5],[1,6]], "values": [14,19,22,6,19,12,20] } assert my_solution.maximumScoreAfterOperations(**test_input) == 98 test_input = { "edges": [[6,0],[4,1],[2,3],[5,2],[6,4],[5,6]], "values": [16,16,9,12,5,14,17] } assert my_solution.maximumScoreAfterOperations(**test_input) == 75 test_input = { "edges": [[2,1],[4,2],[0,3],[0,4]], "values": [9,6,7,17,19] } assert my_solution.maximumScoreAfterOperations(**test_input) == 49 test_input = { "edges": [[1,3],[2,1],[4,5],[2,4],[0,2],[0,6]], "values": [4,19,10,25,16,13,6] } assert my_solution.maximumScoreAfterOperations(**test_input) == 89 test_input = { "edges": [[1,0],[5,1],[2,5],[3,2],[4,6],[3,4],[3,7]], "values": [12,9,19,10,24,22,18,16] } assert my_solution.maximumScoreAfterOperations(**test_input) == 121 test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [25,23,7,9] } assert my_solution.maximumScoreAfterOperations(**test_input) == 39 test_input = { "edges": [[3,0],[3,2],[4,5],[9,6],[4,7],[9,4],[3,8],[1,3],[1,9]], "values": [23,22,7,22,19,12,10,11,24,3] } assert my_solution.maximumScoreAfterOperations(**test_input) == 131 test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [9,8,24,21] } assert my_solution.maximumScoreAfterOperations(**test_input) == 53 test_input = { "edges": [[3,1],[0,2],[0,3]], "values": [17,2,2,4] } assert my_solution.maximumScoreAfterOperations(**test_input) == 21 test_input = { "edges": [[5,2],[3,4],[0,3],[1,0],[1,5]], "values": [16,6,15,15,10,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 52 test_input = { "edges": [[8,0],[8,1],[7,2],[3,7],[6,3],[4,6],[5,4],[5,8]], "values": [10,25,10,6,21,17,11,15,15] } assert my_solution.maximumScoreAfterOperations(**test_input) == 120 test_input = { "edges": [[0,4],[1,5],[2,6],[0,2],[1,0],[3,1],[3,7]], "values": [20,1,16,12,5,23,21,4] } assert my_solution.maximumScoreAfterOperations(**test_input) == 82 test_input = { "edges": [[7,1],[5,2],[0,3],[7,0],[7,4],[6,5],[6,7]], "values": [6,20,14,17,18,16,11,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 97 test_input = { "edges": [[3,2],[8,4],[1,6],[0,1],[5,0],[3,5],[8,3],[7,8]], "values": [3,18,16,22,10,2,7,3,10] } assert my_solution.maximumScoreAfterOperations(**test_input) == 88 test_input = { "edges": [[4,0],[3,4],[3,5],[3,6],[2,3],[1,2],[1,7]], "values": [12,14,7,25,13,16,12,15] } assert my_solution.maximumScoreAfterOperations(**test_input) == 102 test_input = { "edges": [[3,0],[5,2],[1,3],[4,1],[4,5]], "values": [11,18,19,14,8,11] } assert my_solution.maximumScoreAfterOperations(**test_input) == 73 test_input = { "edges": [[1,2],[4,1],[0,4],[8,5],[0,6],[7,0],[3,7],[3,8]], "values": [17,22,14,15,2,21,7,9,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 98 test_input = { "edges": [[2,1],[0,2],[4,3],[0,4],[0,5]], "values": [1,2,24,25,9,24] } assert my_solution.maximumScoreAfterOperations(**test_input) == 84 test_input = { "edges": [[3,1],[3,2],[0,4],[3,5],[0,3],[0,6]], "values": [21,19,5,20,2,25,20] } assert my_solution.maximumScoreAfterOperations(**test_input) == 91 test_input = { "edges": [[1,0],[4,1],[4,2],[3,4]], "values": [3,6,17,4,20] } assert my_solution.maximumScoreAfterOperations(**test_input) == 47 test_input = { "edges": [[6,1],[2,3],[6,7],[4,6],[2,4],[5,2],[5,8],[0,5],[0,9]], "values": [23,19,15,4,3,18,25,22,6,11] } assert my_solution.maximumScoreAfterOperations(**test_input) == 123 test_input = { "edges": [[2,0],[1,2],[5,1],[3,4],[3,5]], "values": [23,24,10,15,10,22] } assert my_solution.maximumScoreAfterOperations(**test_input) == 94 test_input = { "edges": [[1,0],[6,1],[6,2],[4,3],[5,4],[5,6]], "values": [18,21,3,13,25,15,20] } assert my_solution.maximumScoreAfterOperations(**test_input) == 99 test_input = { "edges": [[1,0],[3,1],[2,3]], "values": [23,10,24,15] } assert my_solution.maximumScoreAfterOperations(**test_input) == 62 test_input = { "edges": [[1,0],[1,2],[1,3]], "values": [10,4,12,14] } assert my_solution.maximumScoreAfterOperations(**test_input) == 36 test_input = { "edges": [[2,0],[3,2],[1,3],[4,5],[1,4],[1,6]], "values": [3,8,13,11,6,18,8] } assert my_solution.maximumScoreAfterOperations(**test_input) == 64 test_input = { "edges": [[3,0],[3,2],[1,3],[1,4]], "values": [18,19,7,7,2] } assert my_solution.maximumScoreAfterOperations(**test_input) == 46 test_input = { "edges": [[7,0],[5,2],[1,3],[7,4],[1,5],[7,1],[6,7]], "values": [24,24,16,17,25,9,3,23] } assert my_solution.maximumScoreAfterOperations(**test_input) == 118 test_input = { "edges": [[2,0],[2,1],[2,3]], "values": [25,12,5,7] } assert my_solution.maximumScoreAfterOperations(**test_input) == 44 test_input = { "edges": [[0,1],[2,0],[2,3],[2,4]], "values": [24,14,9,5,24] } assert my_solution.maximumScoreAfterOperations(**test_input) == 53 test_input = { "edges": [[6,2],[7,3],[0,5],[4,0],[1,6],[4,1],[4,7]], "values": [20,15,15,2,22,7,19,24] } assert my_solution.maximumScoreAfterOperations(**test_input) == 104 test_input = { "edges": [[1,0],[4,2],[7,5],[3,6],[1,3],[4,1],[4,7]], "values": [11,21,15,23,2,7,21,3] } assert my_solution.maximumScoreAfterOperations(**test_input) == 92 test_input = { "edges": [[4,0],[2,1],[2,3],[2,4]], "values": [22,20,20,8,14] } assert my_solution.maximumScoreAfterOperations(**test_input) == 70 test_input = { "edges": [[0,2],[4,3],[0,4],[1,0],[1,6],[5,1],[5,7]], "values": [18,10,19,9,11,14,11,18] } assert my_solution.maximumScoreAfterOperations(**test_input) == 92 test_input = { "edges": [[6,1],[4,2],[4,3],[0,5],[4,0],[4,6]], "values": [18,10,5,23,16,13,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 68 test_input = { "edges": [[4,0],[6,2],[6,3],[6,4],[1,5],[7,1],[6,7]], "values": [14,15,22,9,13,2,25,3] } assert my_solution.maximumScoreAfterOperations(**test_input) == 90 test_input = { "edges": [[6,2],[0,3],[5,0],[1,5],[4,1],[4,6]], "values": [22,10,19,14,18,24,8] } assert my_solution.maximumScoreAfterOperations(**test_input) == 93 test_input = { "edges": [[0,1],[0,2],[0,3]], "values": [6,22,9,5] } assert my_solution.maximumScoreAfterOperations(**test_input) == 36 test_input = { "edges": [[2,1],[0,2],[6,0],[6,4],[3,5],[3,6],[3,7]], "values": [9,16,13,9,19,1,16,19] } assert my_solution.maximumScoreAfterOperations(**test_input) == 93 test_input = { "edges": [[3,5],[7,3],[0,6],[4,0],[1,4],[2,1],[2,7]], "values": [16,9,8,14,6,18,23,25] } assert my_solution.maximumScoreAfterOperations(**test_input) == 103 test_input = { "edges": [[7,1],[4,5],[3,6],[4,3],[2,4],[0,2],[0,7]], "values": [7,22,12,22,5,3,6,7] } assert my_solution.maximumScoreAfterOperations(**test_input) == 77 test_input = { "edges": [[5,0],[4,1],[5,2],[3,5],[4,3],[7,4],[6,7]], "values": [19,16,8,13,15,13,3,5] } assert my_solution.maximumScoreAfterOperations(**test_input) == 79 test_input = { "edges": [[7,1],[6,2],[3,5],[8,3],[0,6],[4,0],[7,4],[7,8]], "values": [23,11,22,6,19,6,19,8,25] } assert my_solution.maximumScoreAfterOperations(**test_input) == 116 test_input = { "edges": [[4,0],[4,2],[1,3],[4,1],[4,5]], "values": [23,22,13,1,11,25] } assert my_solution.maximumScoreAfterOperations(**test_input) == 84 test_input = { "edges": [[4,0],[4,1],[2,3],[2,4]], "values": [3,17,5,24,14] } assert my_solution.maximumScoreAfterOperations(**test_input) == 60 test_input = { "edges": [[4,0],[2,4],[1,2],[3,1],[3,5]], "values": [6,12,21,6,8,8] } assert my_solution.maximumScoreAfterOperations(**test_input) == 55 test_input = { "edges": [[4,2],[5,4],[0,5],[0,6],[3,0],[1,3],[7,8],[1,7],[1,9]], "values": [25,16,16,14,22,19,2,22,13,11] } assert my_solution.maximumScoreAfterOperations(**test_input) == 135 test_input = { "edges": [[4,0],[1,3],[4,1],[7,5],[2,6],[4,2],[4,7]], "values": [23,16,24,25,3,21,3,25] } assert my_solution.maximumScoreAfterOperations(**test_input) == 137 test_input = { "edges": [[0,1],[2,0],[2,3]], "values": [22,17,9,9] } assert my_solution.maximumScoreAfterOperations(**test_input) == 35
1,699,151,400
weekly-contest-370-maximum-balanced-subsequence-sum
https://leetcode.com/problems/maximum-balanced-subsequence-sum
maximum-balanced-subsequence-sum
{ "questionId": "3184", "questionFrontendId": "2926", "title": "Maximum Balanced Subsequence Sum", "titleSlug": "maximum-balanced-subsequence-sum", "isPaidOnly": false, "difficulty": "Hard", "likes": 133, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 nums 一个长度为 k 的 子序列 指的是选出 k 个 下标 i0 < i1 < ... < ik-1 ,如果这个子序列满足以下条件,我们说它是 平衡的 : * 对于范围 [1, k - 1] 内的所有 j ,nums[ij] - nums[ij-1] >= ij - ij-1 都成立。 nums 长度为 1 的 子序列 是平衡的。 请你返回一个整数,表示 nums 平衡 子序列里面的 最大元素和 。 一个数组的 子序列 指的是从原数组中删除一些元素(也可能一个元素也不删除)后,剩余元素保持相对顺序得到的 非空 新数组。 示例 1: 输入:nums = [3,3,5,6] 输出:14 解释:这个例子中,选择子序列 [3,5,6] ,下标为 0 ,2 和 3 的元素被选中。 nums[2] - nums[0] >= 2 - 0 。 nums[3] - nums[2] >= 3 - 2 。 所以,这是一个平衡子序列,且它的和是所有平衡子序列里最大的。 包含下标 1 ,2 和 3 的子序列也是一个平衡的子序列。 最大平衡子序列和为 14 。 示例 2: 输入:nums = [5,-1,-3,8] 输出:13 解释:这个例子中,选择子序列 [5,8] ,下标为 0 和 3 的元素被选中。 nums[3] - nums[0] >= 3 - 0 。 所以,这是一个平衡子序列,且它的和是所有平衡子序列里最大的。 最大平衡子序列和为 13 。 示例 3: 输入:nums = [-2,-1] 输出:-1 解释:这个例子中,选择子序列 [-1] 。 这是一个平衡子序列,而且它的和是 nums 所有平衡子序列里最大的。 提示: * 1 <= nums.length <= 105 * -109 <= nums[i] <= 109 """ class Solution: def maxBalancedSubsequenceSum(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。 nums 一个长度为 k 的 子序列 指的是选出 k 个 下标 i0 < i1 < ... < ik-1 ,如果这个子序列满足以下条件,我们说它是 平衡的 : * 对于范围 [1, k - 1] 内的所有 j ,nums[ij] - nums[ij-1] >= ij - ij-1 都成立。 nums 长度为 1 的 子序列 是平衡的。 请你返回一个整数,表示 nums 平衡 子序列里面的 最大元素和 。 一个数组的 子序列 指的是从原数组中删除一些元素(也可能一个元素也不删除)后,剩余元素保持相对顺序得到的 非空 新数组。 示例 1: 输入:nums = [3,3,5,6] 输出:14 解释:这个例子中,选择子序列 [3,5,6] ,下标为 0 ,2 和 3 的元素被选中。 nums[2] - nums[0] >= 2 - 0 。 nums[3] - nums[2] >= 3 - 2 。 所以,这是一个平衡子序列,且它的和是所有平衡子序列里最大的。 包含下标 1 ,2 和 3 的子序列也是一个平衡的子序列。 最大平衡子序列和为 14 。 示例 2: 输入:nums = [5,-1,-3,8] 输出:13 解释:这个例子中,选择子序列 [5,8] ,下标为 0 和 3 的元素被选中。 nums[3] - nums[0] >= 3 - 0 。 所以,这是一个平衡子序列,且它的和是所有平衡子序列里最大的。 最大平衡子序列和为 13 。 示例 3: 输入:nums = [-2,-1] 输出:-1 解释:这个例子中,选择子序列 [-1] 。 这是一个平衡子序列,而且它的和是 nums 所有平衡子序列里最大的。 提示: * 1 <= nums.length <= 105 * -109 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def maxBalancedSubsequenceSum(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [3,3,5,6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 14 test_input = { "nums": [5,-1,-3,8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 13 test_input = { "nums": [-2,-1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1 test_input = { "nums": [0] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [-47] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -47 test_input = { "nums": [-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -8 test_input = { "nums": [-7] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -7 test_input = { "nums": [-6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -6 test_input = { "nums": [-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -5 test_input = { "nums": [-3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -3 test_input = { "nums": [-2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2 test_input = { "nums": [-1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1 test_input = { "nums": [1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1 test_input = { "nums": [3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3 test_input = { "nums": [4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4 test_input = { "nums": [5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [7] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8 test_input = { "nums": [9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [45] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 45 test_input = { "nums": [-9,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -5 test_input = { "nums": [-6,8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8 test_input = { "nums": [-5,-1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1 test_input = { "nums": [-5,0] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [-3,-1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1 test_input = { "nums": [-2,3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3 test_input = { "nums": [-1,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1 test_input = { "nums": [-1,0] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [-1,3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3 test_input = { "nums": [0,-3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [0,5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [2,7] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [5,-1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [6,-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6 test_input = { "nums": [7,-2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [7,2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [8,-9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8 test_input = { "nums": [9,-9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [-43,23,-49] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 23 test_input = { "nums": [-9,-6,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -5 test_input = { "nums": [-9,-2,4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4 test_input = { "nums": [-9,5,-6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [-8,9,-9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [-5,-1,-9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1 test_input = { "nums": [-4,-9,3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3 test_input = { "nums": [-4,-2,-7] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2 test_input = { "nums": [-3,-4,-2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2 test_input = { "nums": [-2,-1,-2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -1 test_input = { "nums": [-1,-6,1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1 test_input = { "nums": [0,-6,-4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [0,-5,4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4 test_input = { "nums": [1,-4,4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [4,1,-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4 test_input = { "nums": [5,-6,-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [5,-4,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [6,-3,9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 15 test_input = { "nums": [6,0,2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6 test_input = { "nums": [7,-6,0] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [8,-7,2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8 test_input = { "nums": [14,-21,-18] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 14 test_input = { "nums": [-9,7,-8,1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [-8,2,-5,-7] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 2 test_input = { "nums": [-7,1,-2,-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1 test_input = { "nums": [-6,-8,7,-3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [-5,-1,0,6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6 test_input = { "nums": [-4,8,9,2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 17 test_input = { "nums": [-4,9,7,-4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [-2,-6,0,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [-2,-3,9,3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [0,-6,-3,5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [0,1,0,-2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 1 test_input = { "nums": [1,-7,-8,6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [3,-7,9,-3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 12 test_input = { "nums": [3,-7,9,2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 12 test_input = { "nums": [4,-8,-1,8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 12 test_input = { "nums": [5,4,1,0] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [7,7,-9,-4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 7 test_input = { "nums": [8,0,4,-2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 8 test_input = { "nums": [9,7,-2,1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [34,34,32,33] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 65 test_input = { "nums": [-9,-6,-8,-2,4] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4 test_input = { "nums": [-9,-5,2,2,7] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [-9,-2,-6,0,6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6 test_input = { "nums": [-7,0,-8,-9,-3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [-7,0,9,-4,9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [-6,-2,-8,-4,-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == -2 test_input = { "nums": [-6,2,-3,0,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 2 test_input = { "nums": [-6,3,-6,-3,-2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 3 test_input = { "nums": [-5,-9,1,3,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 4 test_input = { "nums": [-5,1,5,-5,-1] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 6 test_input = { "nums": [-3,7,0,4,6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 10 test_input = { "nums": [-2,-2,9,-2,-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 9 test_input = { "nums": [0,-6,-9,-8,-3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [0,-2,-7,-1,-8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [0,-1,-4,-6,-9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 0 test_input = { "nums": [1,-3,-8,9,-9] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 10 test_input = { "nums": [2,-6,-2,0,-3] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 2 test_input = { "nums": [2,9,-4,4,2] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 11 test_input = { "nums": [4,-1,5,-1,-7] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 5 test_input = { "nums": [4,6,-8,-8,-5] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 10
1,699,151,400
weekly-contest-369-find-the-k-or-of-an-array
https://leetcode.com/problems/find-the-k-or-of-an-array
find-the-k-or-of-an-array
{ "questionId": "3183", "questionFrontendId": "2917", "title": "Find the K-or of an Array", "titleSlug": "find-the-k-or-of-an-array", "isPaidOnly": false, "difficulty": "Easy", "likes": 55, "dislikes": 201, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。 nums 中的 K-or 是一个满足以下条件的非负整数: * 只有在 nums 中,至少存在 k 个元素的第 i 位值为 1 ,那么 K-or 中的第 i 位的值才是 1 。 返回 nums 的 K-or 值。 注意 :对于整数 x ,如果 (2i AND x) == 2i ,则 x 中的第 i 位值为 1 ,其中 AND 为按位与运算符。 示例 1: 输入:nums = [7,12,9,8,9,15], k = 4 输出:9 解释:nums[0]、nums[2]、nums[4] 和 nums[5] 的第 0 位的值为 1 。 nums[0] 和 nums[5] 的第 1 位的值为 1 。 nums[0]、nums[1] 和 nums[5] 的第 2 位的值为 1 。 nums[1]、nums[2]、nums[3]、nums[4] 和 nums[5] 的第 3 位的值为 1 。 只有第 0 位和第 3 位满足数组中至少存在 k 个元素在对应位上的值为 1 。因此,答案为 2^0 + 2^3 = 9 。 示例 2: 输入:nums = [2,12,1,11,4,5], k = 6 输出:0 解释:因为 k == 6 == nums.length ,所以数组的 6-or 等于其中所有元素按位与运算的结果。因此,答案为 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0 。 示例 3: 输入:nums = [10,8,5,9,11,6,8], k = 1 输出:15 解释:因为 k == 1 ,数组的 1-or 等于其中所有元素按位或运算的结果。因此,答案为 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15 。 提示: * 1 <= nums.length <= 50 * 0 <= nums[i] < 231 * 1 <= k <= nums.length """ class Solution: def findKOr(self, nums: List[int], k: int) -> int:
给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。 nums 中的 K-or 是一个满足以下条件的非负整数: * 只有在 nums 中,至少存在 k 个元素的第 i 位值为 1 ,那么 K-or 中的第 i 位的值才是 1 。 返回 nums 的 K-or 值。 注意 :对于整数 x ,如果 (2i AND x) == 2i ,则 x 中的第 i 位值为 1 ,其中 AND 为按位与运算符。 示例 1: 输入:nums = [7,12,9,8,9,15], k = 4 输出:9 解释:nums[0]、nums[2]、nums[4] 和 nums[5] 的第 0 位的值为 1 。 nums[0] 和 nums[5] 的第 1 位的值为 1 。 nums[0]、nums[1] 和 nums[5] 的第 2 位的值为 1 。 nums[1]、nums[2]、nums[3]、nums[4] 和 nums[5] 的第 3 位的值为 1 。 只有第 0 位和第 3 位满足数组中至少存在 k 个元素在对应位上的值为 1 。因此,答案为 2^0 + 2^3 = 9 。 示例 2: 输入:nums = [2,12,1,11,4,5], k = 6 输出:0 解释:因为 k == 6 == nums.length ,所以数组的 6-or 等于其中所有元素按位与运算的结果。因此,答案为 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0 。 示例 3: 输入:nums = [10,8,5,9,11,6,8], k = 1 输出:15 解释:因为 k == 1 ,数组的 1-or 等于其中所有元素按位或运算的结果。因此,答案为 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15 。 提示: * 1 <= nums.length <= 50 * 0 <= nums[i] < 231 * 1 <= k <= nums.length 请完成下面的代码来解决上述问题: ```python class Solution: def findKOr(self, nums: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "nums": [7,12,9,8,9,15], "k": 4 } assert my_solution.findKOr(**test_input) == 9 test_input = { "nums": [2,12,1,11,4,5], "k": 6 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [10,8,5,9,11,6,8], "k": 1 } assert my_solution.findKOr(**test_input) == 15 test_input = { "nums": [14,7,12,9,8,9,1,15], "k": 4 } assert my_solution.findKOr(**test_input) == 13 test_input = { "nums": [2,12,1,11,4,5], "k": 3 } assert my_solution.findKOr(**test_input) == 5 test_input = { "nums": [10,8,5,10,11,11,6,8], "k": 1 } assert my_solution.findKOr(**test_input) == 15 test_input = { "nums": [0], "k": 1 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [1], "k": 1 } assert my_solution.findKOr(**test_input) == 1 test_input = { "nums": [2], "k": 1 } assert my_solution.findKOr(**test_input) == 2 test_input = { "nums": [3], "k": 1 } assert my_solution.findKOr(**test_input) == 3 test_input = { "nums": [4], "k": 1 } assert my_solution.findKOr(**test_input) == 4 test_input = { "nums": [5], "k": 1 } assert my_solution.findKOr(**test_input) == 5 test_input = { "nums": [6], "k": 1 } assert my_solution.findKOr(**test_input) == 6 test_input = { "nums": [7], "k": 1 } assert my_solution.findKOr(**test_input) == 7 test_input = { "nums": [8], "k": 1 } assert my_solution.findKOr(**test_input) == 8 test_input = { "nums": [9], "k": 1 } assert my_solution.findKOr(**test_input) == 9 test_input = { "nums": [10], "k": 1 } assert my_solution.findKOr(**test_input) == 10 test_input = { "nums": [11], "k": 1 } assert my_solution.findKOr(**test_input) == 11 test_input = { "nums": [12], "k": 1 } assert my_solution.findKOr(**test_input) == 12 test_input = { "nums": [13], "k": 1 } assert my_solution.findKOr(**test_input) == 13 test_input = { "nums": [14], "k": 1 } assert my_solution.findKOr(**test_input) == 14 test_input = { "nums": [15], "k": 1 } assert my_solution.findKOr(**test_input) == 15 test_input = { "nums": [16], "k": 1 } assert my_solution.findKOr(**test_input) == 16 test_input = { "nums": [17], "k": 1 } assert my_solution.findKOr(**test_input) == 17 test_input = { "nums": [18], "k": 1 } assert my_solution.findKOr(**test_input) == 18 test_input = { "nums": [19], "k": 1 } assert my_solution.findKOr(**test_input) == 19 test_input = { "nums": [20], "k": 1 } assert my_solution.findKOr(**test_input) == 20 test_input = { "nums": [21], "k": 1 } assert my_solution.findKOr(**test_input) == 21 test_input = { "nums": [22], "k": 1 } assert my_solution.findKOr(**test_input) == 22 test_input = { "nums": [23], "k": 1 } assert my_solution.findKOr(**test_input) == 23 test_input = { "nums": [24], "k": 1 } assert my_solution.findKOr(**test_input) == 24 test_input = { "nums": [25], "k": 1 } assert my_solution.findKOr(**test_input) == 25 test_input = { "nums": [26], "k": 1 } assert my_solution.findKOr(**test_input) == 26 test_input = { "nums": [27], "k": 1 } assert my_solution.findKOr(**test_input) == 27 test_input = { "nums": [28], "k": 1 } assert my_solution.findKOr(**test_input) == 28 test_input = { "nums": [29], "k": 1 } assert my_solution.findKOr(**test_input) == 29 test_input = { "nums": [30], "k": 1 } assert my_solution.findKOr(**test_input) == 30 test_input = { "nums": [31], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [22,7,27,30,15,30,28], "k": 4 } assert my_solution.findKOr(**test_input) == 30 test_input = { "nums": [24,18,3,23,16,11,27,18,5,29], "k": 6 } assert my_solution.findKOr(**test_input) == 19 test_input = { "nums": [14,1,2,28,4,15,3,12], "k": 2 } assert my_solution.findKOr(**test_input) == 15 test_input = { "nums": [7,18,25,11,2], "k": 5 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [0,4], "k": 2 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [17,5,14,16,24,30,3,19,31], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [14,20,23,7,1,12,24,19], "k": 7 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [5,31,29,22,8,6,23], "k": 4 } assert my_solution.findKOr(**test_input) == 23 test_input = { "nums": [9,10,30,0,7,19,14,19,20,3], "k": 4 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [25,6,5,30,27,11,10,30], "k": 2 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [0,15,16,6,19,5,24,17], "k": 3 } assert my_solution.findKOr(**test_input) == 23 test_input = { "nums": [19,8,2,28,4,5], "k": 5 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [13,9,1,15,9,2,19,19], "k": 3 } assert my_solution.findKOr(**test_input) == 11 test_input = { "nums": [16,6,16,22,8,2,25,30], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [14,28,23,22], "k": 2 } assert my_solution.findKOr(**test_input) == 30 test_input = { "nums": [6,26], "k": 2 } assert my_solution.findKOr(**test_input) == 2 test_input = { "nums": [14,9,22,30,15], "k": 4 } assert my_solution.findKOr(**test_input) == 14 test_input = { "nums": [12,13,16,25,12,4,8,29], "k": 6 } assert my_solution.findKOr(**test_input) == 8 test_input = { "nums": [27,29], "k": 2 } assert my_solution.findKOr(**test_input) == 25 test_input = { "nums": [9,27,27,20,24,13,25,8], "k": 6 } assert my_solution.findKOr(**test_input) == 8 test_input = { "nums": [22,26,18,26,1], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [20,20,31,19,29,19], "k": 2 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [5,8,27,23,3], "k": 2 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [4,23,0,20,4,19,14,22,26,2], "k": 9 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [31,26,21,4,9,11,13,24,23,5], "k": 10 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [4,22], "k": 2 } assert my_solution.findKOr(**test_input) == 4 test_input = { "nums": [22,17,20,3,21,5,20,25,16], "k": 4 } assert my_solution.findKOr(**test_input) == 21 test_input = { "nums": [16,15,13,26,15,23,0,12], "k": 4 } assert my_solution.findKOr(**test_input) == 15 test_input = { "nums": [4,11,14], "k": 1 } assert my_solution.findKOr(**test_input) == 15 test_input = { "nums": [10,26,27,25,3,21,9,3,22], "k": 8 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [4,11,16], "k": 2 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [9,27,19,9,24,11], "k": 3 } assert my_solution.findKOr(**test_input) == 27 test_input = { "nums": [29,19,27,14], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [27,31,21,8,25], "k": 2 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [14,1,13,22,27], "k": 5 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [14,15,17,23,29], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [19,21], "k": 2 } assert my_solution.findKOr(**test_input) == 17 test_input = { "nums": [29,9,18,0,30,5,1,9], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [6,31,11,7,6,2,26,19,17,13], "k": 10 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [15,8,27,28], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [28,24,20,31,23,1], "k": 2 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [8,13,27,24,20,28,15,21,23,6], "k": 9 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [31,6], "k": 2 } assert my_solution.findKOr(**test_input) == 6 test_input = { "nums": [3,14,11,17,9], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [14,11], "k": 2 } assert my_solution.findKOr(**test_input) == 10 test_input = { "nums": [2,9,11,25,3,2,26,21,13,11], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [26,7,23,0,16,31,12,18,24], "k": 4 } assert my_solution.findKOr(**test_input) == 30 test_input = { "nums": [11,30,30,17,10,27,6,31,0], "k": 4 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [11,9,18,30,27,20,2,17,18,4], "k": 2 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [2,1,0,30,29,14,13,26,10,22], "k": 10 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [21,30,30,17,23,8,26,9], "k": 6 } assert my_solution.findKOr(**test_input) == 16 test_input = { "nums": [16,10], "k": 1 } assert my_solution.findKOr(**test_input) == 26 test_input = { "nums": [26,12,19,22,5,6,19,30,24,11], "k": 10 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [20,10,14], "k": 2 } assert my_solution.findKOr(**test_input) == 14 test_input = { "nums": [23,17,18,30,3], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [13,16,12], "k": 1 } assert my_solution.findKOr(**test_input) == 29 test_input = { "nums": [17,12,5,13,23,19], "k": 3 } assert my_solution.findKOr(**test_input) == 21 test_input = { "nums": [5,4,3], "k": 2 } assert my_solution.findKOr(**test_input) == 5 test_input = { "nums": [6,28,1,3,2], "k": 1 } assert my_solution.findKOr(**test_input) == 31 test_input = { "nums": [28,3,15,30,10,29], "k": 4 } assert my_solution.findKOr(**test_input) == 14 test_input = { "nums": [0,31,13,24,16,21], "k": 3 } assert my_solution.findKOr(**test_input) == 29 test_input = { "nums": [11,20,28,29,3,4], "k": 2 } assert my_solution.findKOr(**test_input) == 31
1,698,546,600
weekly-contest-369-minimum-equal-sum-of-two-arrays-after-replacing-zeros
https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros
minimum-equal-sum-of-two-arrays-after-replacing-zeros
{ "questionId": "3171", "questionFrontendId": "2918", "title": "Minimum Equal Sum of Two Arrays After Replacing Zeros", "titleSlug": "minimum-equal-sum-of-two-arrays-after-replacing-zeros", "isPaidOnly": false, "difficulty": "Medium", "likes": 131, "dislikes": 15, "categoryTitle": "Algorithms" }
""" 给你两个由正整数和 0 组成的数组 nums1 和 nums2 。 你必须将两个数组中的 所有 0 替换为 严格 正整数,并且满足两个数组中所有元素的和 相等 。 返回 最小 相等和 ,如果无法使两数组相等,则返回 -1 。 示例 1: 输入:nums1 = [3,2,0,1,0], nums2 = [6,5,0] 输出:12 解释:可以按下述方式替换数组中的 0 : - 用 2 和 4 替换 nums1 中的两个 0 。得到 nums1 = [3,2,2,1,4] 。 - 用 1 替换 nums2 中的一个 0 。得到 nums2 = [6,5,1] 。 两个数组的元素和相等,都等于 12 。可以证明这是可以获得的最小相等和。 示例 2: 输入:nums1 = [2,0,2,0], nums2 = [1,4] 输出:-1 解释:无法使两个数组的和相等。 提示: * 1 <= nums1.length, nums2.length <= 105 * 0 <= nums1[i], nums2[i] <= 106 """ class Solution: def minSum(self, nums1: List[int], nums2: List[int]) -> int:
给你两个由正整数和 0 组成的数组 nums1 和 nums2 。 你必须将两个数组中的 所有 0 替换为 严格 正整数,并且满足两个数组中所有元素的和 相等 。 返回 最小 相等和 ,如果无法使两数组相等,则返回 -1 。 示例 1: 输入:nums1 = [3,2,0,1,0], nums2 = [6,5,0] 输出:12 解释:可以按下述方式替换数组中的 0 : - 用 2 和 4 替换 nums1 中的两个 0 。得到 nums1 = [3,2,2,1,4] 。 - 用 1 替换 nums2 中的一个 0 。得到 nums2 = [6,5,1] 。 两个数组的元素和相等,都等于 12 。可以证明这是可以获得的最小相等和。 示例 2: 输入:nums1 = [2,0,2,0], nums2 = [1,4] 输出:-1 解释:无法使两个数组的和相等。 提示: * 1 <= nums1.length, nums2.length <= 105 * 0 <= nums1[i], nums2[i] <= 106 请完成下面的代码来解决上述问题: ```python class Solution: def minSum(self, nums1: List[int], nums2: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums1": [3,2,0,1,0], "nums2": [6,5,0] } assert my_solution.minSum(**test_input) == 12 test_input = { "nums1": [2,0,2,0], "nums2": [1,4] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,7,28,17,18], "nums2": [1,2,6,26,1,0,27,3,0,30] } assert my_solution.minSum(**test_input) == 98 test_input = { "nums1": [8,13,15,18,0,18,0,0,5,20,12,27,3,14,22,0], "nums2": [29,1,6,0,10,24,27,17,14,13,2,19,2,11] } assert my_solution.minSum(**test_input) == 179 test_input = { "nums1": [9,5], "nums2": [15,12,5,21,4,26,27,9,6,29,0,18,16,0,0,0,20] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,29,5,22,5,9,30,11,20,0,18,16,26,11,3,0,24,24,14,24], "nums2": [30,12,16,3,24,6,13,0,16] } assert my_solution.minSum(**test_input) == 294 test_input = { "nums1": [9,13,0,0,12,10,0,8,0,0,5,13,0], "nums2": [8,14,11,2,27,0,0] } assert my_solution.minSum(**test_input) == 76 test_input = { "nums1": [3,0,20,9,20,0,20,25,26,9,0,12,6,11,0,6], "nums2": [0,3,8,13,27,0,0,0,29,27,0,11,23,0,19,19,0] } assert my_solution.minSum(**test_input) == 186 test_input = { "nums1": [25,28,13,0,14,23,14,0,3,3,12], "nums2": [24,30,0,15,20,19,18,0,23,23,0,16,26,0,29,19,16,25] } assert my_solution.minSum(**test_input) == 307 test_input = { "nums1": [0,29,30,18,5,24,16,5,17,0,18,16,26,0,15,19,14,20,3,26], "nums2": [0,8,14,11,13,6,8,0,13] } assert my_solution.minSum(**test_input) == 304 test_input = { "nums1": [0,17,20,17,5,0,14,19,7,8,16,18,6], "nums2": [21,1,27,19,2,2,24,21,16,1,13,27,8,5,3,11,13,7,29,7] } assert my_solution.minSum(**test_input) == 257 test_input = { "nums1": [26,1,25,10,14,14,4,0,10,0,23], "nums2": [23,8,30,18,8,15,6,9,0,2,0,0,19,8,19,4,10] } assert my_solution.minSum(**test_input) == 182 test_input = { "nums1": [15,10,7,16], "nums2": [8,16,2,6,4,12,6,16,24,0] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,0,0,17,0,6,2,22,12,0,25,18,1,12,19,0,0], "nums2": [0,0,0,30,4,3,13,25,9,25,3,0,1,12,2,10,4,7,30,16] } assert my_solution.minSum(**test_input) == 198 test_input = { "nums1": [23,17], "nums2": [7,3,22,0,12] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [15,0,8,30,6,3,24,6,0,11,13,30,6,25,23,3], "nums2": [12,20,0,6,0,0,14,0,0,8,5,19,16,0,0,15] } assert my_solution.minSum(**test_input) == 205 test_input = { "nums1": [3,25,1,13], "nums2": [19,13,10,27,10,20,27,0,3,12,16,26,0,27] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,0], "nums2": [29,28] } assert my_solution.minSum(**test_input) == 57 test_input = { "nums1": [17,4,11,8,0,17,0,0,12,27,20,28,0,30,21,18,12], "nums2": [0,2,30,0,5,17,0,0,0,15,11,2,25,18,18] } assert my_solution.minSum(**test_input) == 229 test_input = { "nums1": [0,17,0,7,29,10,22,27,13,8,19], "nums2": [26,23,8,14,0,17,20,4,26,15,0,9,14,0,12,10,23,16] } assert my_solution.minSum(**test_input) == 240 test_input = { "nums1": [0,25,8,0,22,0], "nums2": [6,12,22,3,0,28,19,0,20,21,2,3] } assert my_solution.minSum(**test_input) == 138 test_input = { "nums1": [2,17,23,16,2,0,6,12,10], "nums2": [19,11,7,16,0] } assert my_solution.minSum(**test_input) == 89 test_input = { "nums1": [4,7,14,15,18,7,0,6,8], "nums2": [27,2,13,18,20,23,9,0,0,25,5,0,17,0,0,14] } assert my_solution.minSum(**test_input) == 178 test_input = { "nums1": [16,0,7,19,0,0,7,26,12,0,4,0,7,0,22,12,0,26], "nums2": [7,25,0,25,18,0,6,14,0] } assert my_solution.minSum(**test_input) == 165 test_input = { "nums1": [7,0,3,6,5,24,0,0,0,30,20,13,0,5,19,4,25,17], "nums2": [11,19,28,25,27,6,0,18,0,19,18,16,0,16,9,0,2,23,23,10] } assert my_solution.minSum(**test_input) == 274 test_input = { "nums1": [23,19,24,0,8,19,30,0,14], "nums2": [25,17,18,6,30] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,15,8,0,4,0,21,20,0,0,18], "nums2": [16,0] } assert my_solution.minSum(**test_input) == 91 test_input = { "nums1": [0,0,24,21,16,4,22,21], "nums2": [18,7,28,0,0,11,2,0] } assert my_solution.minSum(**test_input) == 110 test_input = { "nums1": [4,11,17,30,11,21,21,10,2,10,7,29,21,1,0,9,15,5], "nums2": [0,0,1,7,8,0,27,20] } assert my_solution.minSum(**test_input) == 225 test_input = { "nums1": [12,16,1], "nums2": [1,0,19,24,21,0,0,24,24,18,26,19,13,14,30,9,0,4,20] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,0,8,0], "nums2": [23,0,15,29,25] } assert my_solution.minSum(**test_input) == 93 test_input = { "nums1": [28,25,14,10], "nums2": [0,6,16,2,0,13,0,0,4,2,16,6,18,0,8,14,10] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,0,8,30,1,0,22,0,0,1,0], "nums2": [22,6,0,13,0,23,14,27,20,4,0,11,11,25,9,22,11,17,17] } assert my_solution.minSum(**test_input) == 255 test_input = { "nums1": [0,0,27], "nums2": [18,0,0,7,26,11,28,20,1,19] } assert my_solution.minSum(**test_input) == 132 test_input = { "nums1": [30,6,0,8,14,0,15,0,11,13,0,8,28,8,8,0,28,0,25], "nums2": [21,8,0,10,28,2,6,3,0,20,1,2,24,12,29] } assert my_solution.minSum(**test_input) == 208 test_input = { "nums1": [18,27,20,10,28,12,29,5,24,0,27,9,22,0,14,0,5,11], "nums2": [24,0,14,26,1,9,24,0,12,30,13,21] } assert my_solution.minSum(**test_input) == 264 test_input = { "nums1": [0,5], "nums2": [17,12,5,6,0,13,19,7] } assert my_solution.minSum(**test_input) == 80 test_input = { "nums1": [30,2,20,30], "nums2": [8,14,0,2,0,18,9,24,0,0,28,0,1,14,27] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,17,0,1,19,0,0,7,23,0,0,0,5,0], "nums2": [7,1,28,8,1,0,0,5,5,18,17,23] } assert my_solution.minSum(**test_input) == 115 test_input = { "nums1": [17,4,26,28,0,0,1,0,24,5,4,6,10,8,8,16,27], "nums2": [0,18,12,0,0,22,15] } assert my_solution.minSum(**test_input) == 187 test_input = { "nums1": [17,1,13,12,3,13], "nums2": [2,25] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [19,19,21,6,0,0,28,3], "nums2": [5,0,16] } assert my_solution.minSum(**test_input) == 98 test_input = { "nums1": [27,17,9,23,21,18,8,27,19,3,0,0,0,0,19], "nums2": [4,7,27,21,27,0,28,0,28,19,20,14,0,12,24,3] } assert my_solution.minSum(**test_input) == 237 test_input = { "nums1": [25,25,0,5,15,13,26,5,25,23,19,20,1,15], "nums2": [23,6] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [9,0,15,10,18,30,7,0,1,27,24,2,8,0], "nums2": [0,0,1,13,0,9,21,3,0,20] } assert my_solution.minSum(**test_input) == 154 test_input = { "nums1": [23,7,0,4,21,20,18,3,17,18,4,0,0,13,29,17], "nums2": [0,0,18,9,0,11,21,0] } assert my_solution.minSum(**test_input) == 197 test_input = { "nums1": [7,0,5,7,19,12,0,11,7,24,22], "nums2": [8,3,0,23,19,24,2,10,4,20,0,14] } assert my_solution.minSum(**test_input) == 129 test_input = { "nums1": [15,23,12,0,0,1,29,24,0,5,21,9,7,6,27,11,0,19,20], "nums2": [14,25,7,18] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [26,14,0], "nums2": [0,16,0,8,14,7,0,2,0,0,10,10,7,14,0,18,11] } assert my_solution.minSum(**test_input) == 123 test_input = { "nums1": [16,15,27,20,29], "nums2": [27,19,0,11,2,19,28,16,0,16,24,11,0,4,2,24,8] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [26,0,28,0,28,24,15,30,9,17,0,1,21,26,21,8,0,28,0,11], "nums2": [0,20,9,12,10,16,2,21,12,0,26,11,0,21,0,0,0,29,23,22] } assert my_solution.minSum(**test_input) == 298 test_input = { "nums1": [0,0,9,22,3,14,13,26,21], "nums2": [21,9,21,28,17,6,15,11,5,14,17,22,0,24] } assert my_solution.minSum(**test_input) == 211 test_input = { "nums1": [0,0,18,27,7,20,9,10,29], "nums2": [29,26,19,0,0,0,0,8,24] } assert my_solution.minSum(**test_input) == 122 test_input = { "nums1": [5,0,0,0,27,22,0,0,1,9], "nums2": [20,22,5,0,0,24,22,27,15] } assert my_solution.minSum(**test_input) == 137 test_input = { "nums1": [2,3,0,0,0,1,18,14,25,1,0,0,3,1,13,29,0,11,0,0], "nums2": [0,12,5,14,2,0,0,14,1,10,5,17,17,8,0,0,9] } assert my_solution.minSum(**test_input) == 129 test_input = { "nums1": [22,0,16,16,27,21,13,9,15,28,0,7,21,8,28,27,26,4], "nums2": [0,16,23,0,26,4,0,13,19,0,0,0,14,18,5,14,20,0,27] } assert my_solution.minSum(**test_input) == 290 test_input = { "nums1": [26,0,26,18,25,20,20,3,0,14,13,5,13,0,20], "nums2": [16,17,0,12,2,26,14,0,27,17,14,10,0,0,28,29,8,25,3,7] } assert my_solution.minSum(**test_input) == 259 test_input = { "nums1": [0,25,27,23], "nums2": [28,0,12,0,24,4,14,5,16,30,26,15,6,9,28,0] } assert my_solution.minSum(**test_input) == 220 test_input = { "nums1": [3,23,15,19,0,7,24,27,25,0,0,16,28,15], "nums2": [26,3,21,0,26,0,9,12,0,0,21,28,23,0,0,4,16,9,7] } assert my_solution.minSum(**test_input) == 211 test_input = { "nums1": [0,2,0,11,22,0,26,0,1,0,6,0,24,2,24,19,15,12], "nums2": [9,14,0,25,24,29,17,16,24,26,1,28,27,4,11,5,14] } assert my_solution.minSum(**test_input) == 275 test_input = { "nums1": [1,1,1], "nums2": [18] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [29,15,0,0,0,0,1,0,9,0,0], "nums2": [16,0,30,28,23,0,0,0,0,24,14,27,0,0] } assert my_solution.minSum(**test_input) == 169 test_input = { "nums1": [27,10,0,13,25], "nums2": [24,10,18,27,3,0,23,6,6] } assert my_solution.minSum(**test_input) == 118 test_input = { "nums1": [5,0,10,0,4,0,13,0,27,20,12,10,15,29], "nums2": [13,25,4,0,11,0,30,0,29,17,7,28,23] } assert my_solution.minSum(**test_input) == 190 test_input = { "nums1": [29,6,30,0,25,8,12,0,25,7,2,15,12,1,5,0,0,12], "nums2": [12,14,13,0,0,24,25,22,0,5,8,28,23,6,20,3] } assert my_solution.minSum(**test_input) == 206 test_input = { "nums1": [9,11,20,0,0,0,21,25,0,0,0,3,11,5,18], "nums2": [0,27,27,21,28,25,3,0,25,0,21,0,3,0,30,20,17] } assert my_solution.minSum(**test_input) == 252 test_input = { "nums1": [0,0,10,4], "nums2": [9,0,22,9,22,3,16,3,9,19,0,29,3,1,0,1,8,12] } assert my_solution.minSum(**test_input) == 169 test_input = { "nums1": [20,10,0,16,18,0,16,21,22,4,0,15,0,8], "nums2": [0,1,2,0,20] } assert my_solution.minSum(**test_input) == 154 test_input = { "nums1": [23,24,0], "nums2": [0,0,26,27,12,18,0] } assert my_solution.minSum(**test_input) == 86 test_input = { "nums1": [0,17,27,12,0,22], "nums2": [20,0] } assert my_solution.minSum(**test_input) == 80 test_input = { "nums1": [1,29], "nums2": [20,0,8,11,13,17,0,18,0,2,5,3,27,11,7,17] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [19,29,0,0,1,0,0,0,0,24,18,0,24,0,11,14,16,18], "nums2": [2,0,26,8,17] } assert my_solution.minSum(**test_input) == 182 test_input = { "nums1": [2,0,0,19,6,29], "nums2": [25,4,0,11,0,13,28,0,28,7,4,2,16,0,22] } assert my_solution.minSum(**test_input) == 164 test_input = { "nums1": [0,0], "nums2": [26,5,7,0,1,3,0,7,0,0,5,25,26,20,0,3,20,23,18] } assert my_solution.minSum(**test_input) == 194 test_input = { "nums1": [21,2,0,0,12,2,0,4,6,29,15,0], "nums2": [12,20,3,10,16,25,17,8,27,0,0,23,2,0,2,4,10,27] } assert my_solution.minSum(**test_input) == 209 test_input = { "nums1": [25,29,10,12,25,26,19,6,19,10,18], "nums2": [0,0,22,2,17,0,7,23,22,18,20,0,13,22,0,0,0,13,6,8] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,0,16,23,28,20,0,22,4,1,0,0,19,0,0,3,2,28], "nums2": [20,28] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [16,14,0,11,9,7,18,2,24,0,0,11,0,0,23], "nums2": [0,0,0,7,0,24,24,6,0,0,12,18,1,0,0] } assert my_solution.minSum(**test_input) == 140 test_input = { "nums1": [4,15,7,10,8,11,2,0,0,22,11,0,4,14,0,16,29,0,0,27], "nums2": [13,23,8,16] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [7], "nums2": [6,26,25,0,14,19,0,29,16,29,5,26,29,6,0,25,12,0,19,19] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,10,5,16,13,20,20,0,15,4,0,4,3,0,0,15,0,24,0], "nums2": [16,14,27,0,20,0,23,0,5,10,28,21,9,28,21,8,28,0,27,0] } assert my_solution.minSum(**test_input) == 290 test_input = { "nums1": [12,14,25,12,3], "nums2": [3,26,0,21,22] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,1,6,20,13,9,28,30,0,14,6,0,25,25,24,16,2,21], "nums2": [5,3,0] } assert my_solution.minSum(**test_input) == 243 test_input = { "nums1": [0,12,18,0,2,12,29,0,20,29,26,14], "nums2": [1,0,29,14,24] } assert my_solution.minSum(**test_input) == 165 test_input = { "nums1": [0,21,18,13,9,0,10], "nums2": [0,22,27,1,0,0,23,23] } assert my_solution.minSum(**test_input) == 99 test_input = { "nums1": [28,16,0,0,0,0,0,26,3,0,3,7,5,0,19,27,1,7], "nums2": [28,9,0,16,14] } assert my_solution.minSum(**test_input) == 149 test_input = { "nums1": [9,17,6,0,24,18,14,10,14,10,0,0,12,0,3,28,25,5,0,30], "nums2": [13,11,30,30,17,27,0,24,15,0,0] } assert my_solution.minSum(**test_input) == 230 test_input = { "nums1": [26,2,0,0,13,14,18,17,0], "nums2": [27,30,26,14,10,24,17,2,10,25,27] } assert my_solution.minSum(**test_input) == 212 test_input = { "nums1": [9,0,26], "nums2": [0,16] } assert my_solution.minSum(**test_input) == 36 test_input = { "nums1": [4,17,6,0,1,8,19,30,21,11,26,0,0,19,0,12], "nums2": [29,17,7,4,29,5,0,25,11,6,0,0,13,22] } assert my_solution.minSum(**test_input) == 178 test_input = { "nums1": [1], "nums2": [10,29,23,4,0,6,23,23,0,8,29,16,7,20,15,23] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,20,12,15,0,1,5,4,16,13,8,8,0,28,2,9,0,12], "nums2": [13,21,9,0,11,26,0,16,0,29,7,0,0,7,4,0,28,0,2] } assert my_solution.minSum(**test_input) == 180 test_input = { "nums1": [0,20,0,0,8,29,17,25,4,0,0,0,0,7,13,14], "nums2": [6,21,24,14,20,19,0,0,7,21,0,11,0,0,0,0,17,16,0,6] } assert my_solution.minSum(**test_input) == 190 test_input = { "nums1": [28,25,9,0,10,19,23,21,25,8,24,25,18,5], "nums2": [0,3,0] } assert my_solution.minSum(**test_input) == 241 test_input = { "nums1": [23,7,15,16,25,9,30,14,8,0,0,2,25,1,7,0,16,0,19], "nums2": [3,1,24,0,25,0,7,24,0,0,17,27,0] } assert my_solution.minSum(**test_input) == 221 test_input = { "nums1": [0,14,10,29,0,5,13,0,0,1,18,0,0,0,11,3,28,0], "nums2": [30,2,24,0,0,0,14,12,23,3,17,12,14,13,0,28,29,0,21] } assert my_solution.minSum(**test_input) == 247 test_input = { "nums1": [5,29], "nums2": [23,24] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [23,2,12,27,0,5,14,0,1,6,30,0,0,2,6,0,11,0], "nums2": [1,26,27,5,0,14,28,24,2,2,15,25,7,13,9] } assert my_solution.minSum(**test_input) == 199 test_input = { "nums1": [0,10,29,11,11,22,0,0,12,10], "nums2": [14,0,1,3,13,29,21] } assert my_solution.minSum(**test_input) == 108 test_input = { "nums1": [0,9,22,25,28], "nums2": [0,0,0,14,19,6,0,7,19,15,0,30,19,18,11,1,0,15,10,18] } assert my_solution.minSum(**test_input) == 208
1,698,546,600
weekly-contest-369-minimum-increment-operations-to-make-array-beautiful
https://leetcode.com/problems/minimum-increment-operations-to-make-array-beautiful
minimum-increment-operations-to-make-array-beautiful
{ "questionId": "3178", "questionFrontendId": "2919", "title": "Minimum Increment Operations to Make Array Beautiful", "titleSlug": "minimum-increment-operations-to-make-array-beautiful", "isPaidOnly": false, "difficulty": "Medium", "likes": 254, "dislikes": 15, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、长度为 n 的整数数组 nums ,和一个整数 k 。 你可以执行下述 递增 运算 任意 次(可以是 0 次): * 从范围 [0, n - 1] 中选择一个下标 i ,并将 nums[i] 的值加 1 。 如果数组中任何长度 大于或等于 3 的子数组,其 最大 元素都大于或等于 k ,则认为数组是一个 美丽数组 。 以整数形式返回使数组变为 美丽数组 需要执行的 最小 递增运算数。 子数组是数组中的一个连续 非空 元素序列。 示例 1: 输入:nums = [2,3,0,0,2], k = 4 输出:3 解释:可以执行下述递增运算,使 nums 变为美丽数组: 选择下标 i = 1 ,并且将 nums[1] 的值加 1 -> [2,4,0,0,2] 。 选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,3] 。 选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,4] 。 长度大于或等于 3 的子数组为 [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4] 。 在所有子数组中,最大元素都等于 k = 4 ,所以 nums 现在是美丽数组。 可以证明无法用少于 3 次递增运算使 nums 变为美丽数组。 因此,答案为 3 。 示例 2: 输入:nums = [0,1,3,3], k = 5 输出:2 解释:可以执行下述递增运算,使 nums 变为美丽数组: 选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,4,3] 。 选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,5,3] 。 长度大于或等于 3 的子数组为 [0,1,5]、[1,5,3]、[0,1,5,3] 。 在所有子数组中,最大元素都等于 k = 5 ,所以 nums 现在是美丽数组。 可以证明无法用少于 2 次递增运算使 nums 变为美丽数组。 因此,答案为 2 。 示例 3: 输入:nums = [1,1,2], k = 1 输出:0 解释:在这个示例中,只有一个长度大于或等于 3 的子数组 [1,1,2] 。 其最大元素 2 已经大于 k = 1 ,所以无需执行任何增量运算。 因此,答案为 0 。 提示: * 3 <= n == nums.length <= 105 * 0 <= nums[i] <= 109 * 0 <= k <= 109 """ class Solution: def minIncrementOperations(self, nums: List[int], k: int) -> int:
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,和一个整数 k 。 你可以执行下述 递增 运算 任意 次(可以是 0 次): * 从范围 [0, n - 1] 中选择一个下标 i ,并将 nums[i] 的值加 1 。 如果数组中任何长度 大于或等于 3 的子数组,其 最大 元素都大于或等于 k ,则认为数组是一个 美丽数组 。 以整数形式返回使数组变为 美丽数组 需要执行的 最小 递增运算数。 子数组是数组中的一个连续 非空 元素序列。 示例 1: 输入:nums = [2,3,0,0,2], k = 4 输出:3 解释:可以执行下述递增运算,使 nums 变为美丽数组: 选择下标 i = 1 ,并且将 nums[1] 的值加 1 -> [2,4,0,0,2] 。 选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,3] 。 选择下标 i = 4 ,并且将 nums[4] 的值加 1 -> [2,4,0,0,4] 。 长度大于或等于 3 的子数组为 [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4] 。 在所有子数组中,最大元素都等于 k = 4 ,所以 nums 现在是美丽数组。 可以证明无法用少于 3 次递增运算使 nums 变为美丽数组。 因此,答案为 3 。 示例 2: 输入:nums = [0,1,3,3], k = 5 输出:2 解释:可以执行下述递增运算,使 nums 变为美丽数组: 选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,4,3] 。 选择下标 i = 2 ,并且将 nums[2] 的值加 1 -> [0,1,5,3] 。 长度大于或等于 3 的子数组为 [0,1,5]、[1,5,3]、[0,1,5,3] 。 在所有子数组中,最大元素都等于 k = 5 ,所以 nums 现在是美丽数组。 可以证明无法用少于 2 次递增运算使 nums 变为美丽数组。 因此,答案为 2 。 示例 3: 输入:nums = [1,1,2], k = 1 输出:0 解释:在这个示例中,只有一个长度大于或等于 3 的子数组 [1,1,2] 。 其最大元素 2 已经大于 k = 1 ,所以无需执行任何增量运算。 因此,答案为 0 。 提示: * 3 <= n == nums.length <= 105 * 0 <= nums[i] <= 109 * 0 <= k <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def minIncrementOperations(self, nums: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "nums": [2,3,0,0,2], "k": 4 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [0,1,3,3], "k": 5 } assert my_solution.minIncrementOperations(**test_input) == 2 test_input = { "nums": [1,1,2], "k": 1 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [0,5,5], "k": 8 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [0,18,28], "k": 93 } assert my_solution.minIncrementOperations(**test_input) == 65 test_input = { "nums": [0,24,14], "k": 7 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [2,3,4], "k": 3 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [3,5,9], "k": 6 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [4,3,0], "k": 2 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [5,6,5], "k": 9 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [6,9,6], "k": 3 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [7,9,0], "k": 6 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [7,47,16], "k": 39 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [9,6,1], "k": 6 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [41,44,37], "k": 55 } assert my_solution.minIncrementOperations(**test_input) == 11 test_input = { "nums": [48,3,13], "k": 1 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [1,2,6,9], "k": 8 } assert my_solution.minIncrementOperations(**test_input) == 2 test_input = { "nums": [1,3,1,6], "k": 6 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [2,35,41,20], "k": 4 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [3,9,9,7], "k": 6 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [7,7,2,7], "k": 9 } assert my_solution.minIncrementOperations(**test_input) == 2 test_input = { "nums": [10,2,0,2], "k": 6 } assert my_solution.minIncrementOperations(**test_input) == 4 test_input = { "nums": [20,2,22,30], "k": 67 } assert my_solution.minIncrementOperations(**test_input) == 45 test_input = { "nums": [22,49,0,20], "k": 52 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [25,2,1,41], "k": 9 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [31,86,1,2], "k": 354 } assert my_solution.minIncrementOperations(**test_input) == 268 test_input = { "nums": [43,31,14,4], "k": 73 } assert my_solution.minIncrementOperations(**test_input) == 42 test_input = { "nums": [44,24,28,47], "k": 16 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [1,9,5,2,0], "k": 2 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [3,7,9,6,0], "k": 7 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [5,9,0,10,3], "k": 3 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [6,2,8,10,6], "k": 9 } assert my_solution.minIncrementOperations(**test_input) == 1 test_input = { "nums": [6,14,17,4,7], "k": 22 } assert my_solution.minIncrementOperations(**test_input) == 5 test_input = { "nums": [10,9,5,2,4], "k": 1 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [20,38,29,34,6], "k": 95 } assert my_solution.minIncrementOperations(**test_input) == 66 test_input = { "nums": [21,45,33,14,22], "k": 58 } assert my_solution.minIncrementOperations(**test_input) == 25 test_input = { "nums": [32,14,31,43,29], "k": 46 } assert my_solution.minIncrementOperations(**test_input) == 15 test_input = { "nums": [39,21,10,46,40], "k": 81 } assert my_solution.minIncrementOperations(**test_input) == 71 test_input = { "nums": [42,7,32,19,4], "k": 66 } assert my_solution.minIncrementOperations(**test_input) == 34 test_input = { "nums": [74,91,93,96,12], "k": 964 } assert my_solution.minIncrementOperations(**test_input) == 871 test_input = { "nums": [84,17,58,61,72], "k": 432 } assert my_solution.minIncrementOperations(**test_input) == 374 test_input = { "nums": [4,0,10,2,10,6], "k": 8 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [4,0,22,41,29,28], "k": 30 } assert my_solution.minIncrementOperations(**test_input) == 8 test_input = { "nums": [4,1,8,0,3,9], "k": 2 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [4,7,6,9,2,6], "k": 1 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [5,1,3,9,8,8], "k": 4 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [6,5,8,6,0,9], "k": 9 } assert my_solution.minIncrementOperations(**test_input) == 1 test_input = { "nums": [7,4,10,2,0,8], "k": 7 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [8,10,7,1,9,6], "k": 1 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [9,5,0,10,9,0], "k": 8 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [13,34,0,13,9,19], "k": 82 } assert my_solution.minIncrementOperations(**test_input) == 117 test_input = { "nums": [16,50,23,35,38,13], "k": 34 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [20,1,40,48,32,24], "k": 38 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [28,5,10,26,38,6], "k": 17 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [30,42,46,45,23,31], "k": 13 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [3,8,0,6,46,24,31], "k": 30 } assert my_solution.minIncrementOperations(**test_input) == 22 test_input = { "nums": [4,7,2,10,4,10,5], "k": 10 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [8,10,1,5,8,9,7], "k": 4 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [10,7,2,5,9,6,3], "k": 2 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [10,24,21,42,6,10,43], "k": 55 } assert my_solution.minIncrementOperations(**test_input) == 56 test_input = { "nums": [18,48,1,19,43,25,49], "k": 21 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [37,82,95,82,77,87,93], "k": 239 } assert my_solution.minIncrementOperations(**test_input) == 296 test_input = { "nums": [40,17,28,38,41,32,9], "k": 97 } assert my_solution.minIncrementOperations(**test_input) == 125 test_input = { "nums": [41,22,4,41,4,47,36], "k": 25 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [42,19,52,36,8,5,9], "k": 656 } assert my_solution.minIncrementOperations(**test_input) == 1252 test_input = { "nums": [45,58,6,16,70,69,87], "k": 26 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [48,24,41,16,4,20,38], "k": 97 } assert my_solution.minIncrementOperations(**test_input) == 133 test_input = { "nums": [50,3,17,36,16,10,2], "k": 19 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [57,41,90,16,41,25,2], "k": 934 } assert my_solution.minIncrementOperations(**test_input) == 1737 test_input = { "nums": [74,33,13,74,75,95,11], "k": 426 } assert my_solution.minIncrementOperations(**test_input) == 744 test_input = { "nums": [83,54,75,22,32,59,30], "k": 298 } assert my_solution.minIncrementOperations(**test_input) == 462 test_input = { "nums": [1,9,3,6,3,1,10,4], "k": 2 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [1,29,18,3,38,4,7,47], "k": 67 } assert my_solution.minIncrementOperations(**test_input) == 87 test_input = { "nums": [2,1,1,7,2,3,5,6], "k": 9 } assert my_solution.minIncrementOperations(**test_input) == 13 test_input = { "nums": [2,3,6,3,0,0,7,4], "k": 8 } assert my_solution.minIncrementOperations(**test_input) == 8 test_input = { "nums": [2,9,6,9,1,9,4,0], "k": 6 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [5,1,6,9,5,4,9,2], "k": 10 } assert my_solution.minIncrementOperations(**test_input) == 6 test_input = { "nums": [5,10,8,7,6,7,1,10], "k": 3 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [6,7,9,5,0,2,7,7], "k": 1 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [7,2,2,6,7,5,0,2], "k": 3 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [7,12,36,8,27,48,39,35], "k": 36 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [15,47,34,46,42,26,23,11], "k": 15 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [19,40,43,32,15,7,43,5], "k": 85 } assert my_solution.minIncrementOperations(**test_input) == 120 test_input = { "nums": [22,45,6,7,7,23,6,3], "k": 43 } assert my_solution.minIncrementOperations(**test_input) == 56 test_input = { "nums": [25,1,70,71,54,96,46,77], "k": 549 } assert my_solution.minIncrementOperations(**test_input) == 932 test_input = { "nums": [33,41,14,18,43,20,49,23], "k": 25 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [39,26,16,36,19,5,6,28], "k": 98 } assert my_solution.minIncrementOperations(**test_input) == 175 test_input = { "nums": [44,44,31,36,1,8,39,46], "k": 45 } assert my_solution.minIncrementOperations(**test_input) == 16 test_input = { "nums": [2,17,43,26,33,12,37,28,34], "k": 3 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [3,1,6,1,0,5,4,5,7], "k": 10 } assert my_solution.minIncrementOperations(**test_input) == 12 test_input = { "nums": [3,10,4,2,9,8,2,1,4], "k": 7 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [7,5,4,7,6,5,10,8,8], "k": 8 } assert my_solution.minIncrementOperations(**test_input) == 2 test_input = { "nums": [7,40,36,45,42,23,10,33,17], "k": 25 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [10,4,7,4,8,7,8,4,6], "k": 3 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [12,32,12,13,18,38,21,15,13], "k": 95 } assert my_solution.minIncrementOperations(**test_input) == 214 test_input = { "nums": [17,50,14,27,10,37,24,35,23], "k": 45 } assert my_solution.minIncrementOperations(**test_input) == 36 test_input = { "nums": [20,17,29,44,18,20,17,26,2], "k": 1 } assert my_solution.minIncrementOperations(**test_input) == 0 test_input = { "nums": [28,4,38,38,37,13,47,48,49], "k": 61 } assert my_solution.minIncrementOperations(**test_input) == 60 test_input = { "nums": [29,0,34,5,5,24,43,23,27], "k": 98 } assert my_solution.minIncrementOperations(**test_input) == 193 test_input = { "nums": [41,53,77,44,79,66,2,46,64], "k": 204 } assert my_solution.minIncrementOperations(**test_input) == 405
1,698,546,600
weekly-contest-369-maximum-points-after-collecting-coins-from-all-nodes
https://leetcode.com/problems/maximum-points-after-collecting-coins-from-all-nodes
maximum-points-after-collecting-coins-from-all-nodes
{ "questionId": "3179", "questionFrontendId": "2920", "title": "Maximum Points After Collecting Coins From All Nodes", "titleSlug": "maximum-points-after-collecting-coins-from-all-nodes", "isPaidOnly": false, "difficulty": "Hard", "likes": 161, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 有一棵由 n 个节点组成的无向树,以 0  为根节点,节点编号从 0 到 n - 1 。给你一个长度为 n - 1 的二维 整数 数组 edges ,其中 edges[i] = [ai, bi] 表示在树上的节点 ai 和 bi 之间存在一条边。另给你一个下标从 0 开始、长度为 n 的数组 coins 和一个整数 k ,其中 coins[i] 表示节点 i 处的金币数量。 从根节点开始,你必须收集所有金币。要想收集节点上的金币,必须先收集该节点的祖先节点上的金币。 节点 i 上的金币可以用下述方法之一进行收集: * 收集所有金币,得到共计 coins[i] - k 点积分。如果 coins[i] - k 是负数,你将会失去 abs(coins[i] - k) 点积分。 * 收集所有金币,得到共计 floor(coins[i] / 2) 点积分。如果采用这种方法,节点 i 子树中所有节点 j 的金币数 coins[j] 将会减少至 floor(coins[j] / 2) 。 返回收集 所有 树节点的金币之后可以获得的最大积分。 示例 1: [https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png] 输入:edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5 输出:11 解释: 使用第一种方法收集节点 0 上的所有金币。总积分 = 10 - 5 = 5 。 使用第一种方法收集节点 1 上的所有金币。总积分 = 5 + (10 - 5) = 10 。 使用第二种方法收集节点 2 上的所有金币。所以节点 3 上的金币将会变为 floor(3 / 2) = 1 ,总积分 = 10 + floor(3 / 2) = 11 。 使用第二种方法收集节点 3 上的所有金币。总积分 = 11 + floor(1 / 2) = 11. 可以证明收集所有节点上的金币能获得的最大积分是 11 。 示例 2: [https://assets.leetcode.com/uploads/2023/09/18/ex2.png] 输入:edges = [[0,1],[0,2]], coins = [8,4,4], k = 0 输出:16 解释: 使用第一种方法收集所有节点上的金币,因此,总积分 = (8 - 0) + (4 - 0) + (4 - 0) = 16 。 提示: * n == coins.length * 2 <= n <= 105 * 0 <= coins[i] <= 104 * edges.length == n - 1 * 0 <= edges[i][0], edges[i][1] < n * 0 <= k <= 104 """ class Solution: def maximumPoints(self, edges: List[List[int]], coins: List[int], k: int) -> int:
有一棵由 n 个节点组成的无向树,以 0  为根节点,节点编号从 0 到 n - 1 。给你一个长度为 n - 1 的二维 整数 数组 edges ,其中 edges[i] = [ai, bi] 表示在树上的节点 ai 和 bi 之间存在一条边。另给你一个下标从 0 开始、长度为 n 的数组 coins 和一个整数 k ,其中 coins[i] 表示节点 i 处的金币数量。 从根节点开始,你必须收集所有金币。要想收集节点上的金币,必须先收集该节点的祖先节点上的金币。 节点 i 上的金币可以用下述方法之一进行收集: * 收集所有金币,得到共计 coins[i] - k 点积分。如果 coins[i] - k 是负数,你将会失去 abs(coins[i] - k) 点积分。 * 收集所有金币,得到共计 floor(coins[i] / 2) 点积分。如果采用这种方法,节点 i 子树中所有节点 j 的金币数 coins[j] 将会减少至 floor(coins[j] / 2) 。 返回收集 所有 树节点的金币之后可以获得的最大积分。 示例 1: [https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png] 输入:edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5 输出:11 解释: 使用第一种方法收集节点 0 上的所有金币。总积分 = 10 - 5 = 5 。 使用第一种方法收集节点 1 上的所有金币。总积分 = 5 + (10 - 5) = 10 。 使用第二种方法收集节点 2 上的所有金币。所以节点 3 上的金币将会变为 floor(3 / 2) = 1 ,总积分 = 10 + floor(3 / 2) = 11 。 使用第二种方法收集节点 3 上的所有金币。总积分 = 11 + floor(1 / 2) = 11. 可以证明收集所有节点上的金币能获得的最大积分是 11 。 示例 2: [https://assets.leetcode.com/uploads/2023/09/18/ex2.png] 输入:edges = [[0,1],[0,2]], coins = [8,4,4], k = 0 输出:16 解释: 使用第一种方法收集所有节点上的金币,因此,总积分 = (8 - 0) + (4 - 0) + (4 - 0) = 16 。 提示: * n == coins.length * 2 <= n <= 105 * 0 <= coins[i] <= 104 * edges.length == n - 1 * 0 <= edges[i][0], edges[i][1] < n * 0 <= k <= 104 请完成下面的代码来解决上述问题: ```python class Solution: def maximumPoints(self, edges: List[List[int]], coins: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "edges": [[0,1],[1,2],[2,3]], "coins": [10,10,3,3], "k": 5 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[0,1],[0,2]], "coins": [8,4,4], "k": 0 } assert my_solution.maximumPoints(**test_input) == 16 test_input = { "edges": [[0,1],[2,0],[0,3],[4,2]], "coins": [7,5,0,9,3], "k": 4 } assert my_solution.maximumPoints(**test_input) == 10 test_input = { "edges": [[1,0],[0,2],[1,3]], "coins": [9,3,8,9], "k": 0 } assert my_solution.maximumPoints(**test_input) == 29 test_input = { "edges": [[0,1],[0,2],[3,2],[0,4]], "coins": [5,6,8,7,4], "k": 7 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[1,0],[2,1],[3,1]], "coins": [8,2,7,1], "k": 2 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[0,1],[1,2],[0,3]], "coins": [6,1,2,3], "k": 2 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[0,1],[0,2],[0,3],[2,4],[5,4],[6,0],[4,7],[8,5]], "coins": [2,3,10,0,0,2,7,3,9], "k": 2 } assert my_solution.maximumPoints(**test_input) == 20 test_input = { "edges": [[0,1],[0,2],[1,3],[3,4],[0,5],[6,3],[5,7],[3,8],[9,7]], "coins": [0,5,10,5,6,5,0,2,0,0], "k": 7 } assert my_solution.maximumPoints(**test_input) == 4 test_input = { "edges": [[0,1],[2,1],[3,0],[3,4],[5,0],[6,4],[7,1],[6,8],[9,5]], "coins": [9,0,9,6,7,6,5,7,1,10], "k": 7 } assert my_solution.maximumPoints(**test_input) == 14 test_input = { "edges": [[0,1],[2,1],[3,2],[4,0],[5,2],[3,6],[7,2],[8,4],[9,2]], "coins": [9,4,0,8,0,7,8,1,10,9], "k": 1 } assert my_solution.maximumPoints(**test_input) == 46 test_input = { "edges": [[1,0],[2,1],[3,1],[2,4],[5,4],[6,3],[6,7]], "coins": [9,9,5,5,7,9,6,9], "k": 8 } assert my_solution.maximumPoints(**test_input) == 10 test_input = { "edges": [[0,1],[2,1],[2,3],[4,0],[5,2],[6,1]], "coins": [1,1,8,6,9,4,1], "k": 10 } assert my_solution.maximumPoints(**test_input) == 3 test_input = { "edges": [[1,0],[1,2],[0,3]], "coins": [10,2,9,3], "k": 6 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[1,0],[1,2],[1,3],[3,4],[5,3],[4,6],[7,0],[1,8],[9,1]], "coins": [2,10,4,0,1,3,6,10,3,6], "k": 8 } assert my_solution.maximumPoints(**test_input) == 7 test_input = { "edges": [[1,0],[0,2],[3,2],[4,3],[2,5],[1,6],[7,2]], "coins": [2,8,3,1,9,4,8,6], "k": 6 } assert my_solution.maximumPoints(**test_input) == 6 test_input = { "edges": [[1,0],[2,0],[3,0]], "coins": [0,0,0,6], "k": 0 } assert my_solution.maximumPoints(**test_input) == 6 test_input = { "edges": [[1,0],[1,2]], "coins": [7,6,0], "k": 6 } assert my_solution.maximumPoints(**test_input) == 4 test_input = { "edges": [[0,1],[0,2],[1,3],[2,4],[5,4]], "coins": [4,2,7,7,4,9], "k": 6 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[0,1]], "coins": [10,9], "k": 6 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[0,1],[2,1],[3,1],[2,4],[5,0],[6,1]], "coins": [6,1,8,10,0,4,10], "k": 5 } assert my_solution.maximumPoints(**test_input) == 13 test_input = { "edges": [[1,0],[2,0],[0,3],[0,4],[5,1],[6,4],[3,7],[5,8]], "coins": [9,0,4,2,0,0,3,1,8], "k": 5 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[0,1],[1,2],[3,2],[4,0]], "coins": [7,5,6,3,6], "k": 8 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[0,1],[1,2],[3,2],[4,2],[5,3],[6,1],[4,7],[7,8],[2,9]], "coins": [4,2,0,8,3,2,7,2,1,6], "k": 2 } assert my_solution.maximumPoints(**test_input) == 18 test_input = { "edges": [[0,1],[1,2],[1,3],[4,0],[3,5],[6,3],[7,6],[8,0]], "coins": [3,3,4,3,1,3,1,6,3], "k": 1 } assert my_solution.maximumPoints(**test_input) == 18 test_input = { "edges": [[1,0],[2,1],[2,3],[4,1],[4,5],[2,6]], "coins": [3,10,1,5,10,1,4], "k": 2 } assert my_solution.maximumPoints(**test_input) == 21 test_input = { "edges": [[0,1],[2,0]], "coins": [7,10,8], "k": 10 } assert my_solution.maximumPoints(**test_input) == 7 test_input = { "edges": [[1,0],[2,1],[3,1],[4,2],[5,3],[6,0],[7,4]], "coins": [1,1,7,10,5,1,7,8], "k": 7 } assert my_solution.maximumPoints(**test_input) == 2 test_input = { "edges": [[1,0],[0,2],[3,2]], "coins": [5,2,10,5], "k": 3 } assert my_solution.maximumPoints(**test_input) == 12 test_input = { "edges": [[1,0],[2,0],[3,2],[2,4],[4,5],[6,2],[5,7],[8,2]], "coins": [4,2,1,4,7,7,2,7,4], "k": 2 } assert my_solution.maximumPoints(**test_input) == 22 test_input = { "edges": [[1,0]], "coins": [8,1], "k": 7 } assert my_solution.maximumPoints(**test_input) == 4 test_input = { "edges": [[1,0],[0,2],[3,0],[3,4],[3,5],[6,0],[7,5]], "coins": [3,9,9,9,5,3,2,0], "k": 9 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[0,1],[2,1],[3,2],[4,1],[2,5]], "coins": [2,10,4,6,7,9], "k": 5 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[1,0],[2,1],[3,2],[3,4],[5,4],[2,6],[7,3]], "coins": [3,3,6,1,10,1,2,5], "k": 3 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[1,0],[2,0],[0,3],[1,4],[3,5],[0,6],[7,4],[1,8]], "coins": [9,7,9,0,3,6,9,4,0], "k": 8 } assert my_solution.maximumPoints(**test_input) == 13 test_input = { "edges": [[0,1],[2,1],[3,0],[2,4],[1,5],[6,1],[7,3],[5,8]], "coins": [4,9,7,6,6,9,0,2,6], "k": 2 } assert my_solution.maximumPoints(**test_input) == 34 test_input = { "edges": [[1,0],[1,2]], "coins": [4,4,6], "k": 3 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[0,1],[0,2],[3,1],[2,4],[4,5],[6,2],[4,7],[4,8],[9,1]], "coins": [8,6,10,9,3,10,3,7,9,1], "k": 6 } assert my_solution.maximumPoints(**test_input) == 21 test_input = { "edges": [[1,0],[2,1],[0,3],[0,4],[5,3],[6,1],[7,5],[8,2],[9,3]], "coins": [1,3,10,0,7,2,8,10,0,5], "k": 0 } assert my_solution.maximumPoints(**test_input) == 46 test_input = { "edges": [[0,1],[2,0]], "coins": [9,4,2], "k": 0 } assert my_solution.maximumPoints(**test_input) == 15 test_input = { "edges": [[0,1],[2,1]], "coins": [0,9,3], "k": 3 } assert my_solution.maximumPoints(**test_input) == 4 test_input = { "edges": [[1,0],[2,1],[3,2],[4,1],[3,5]], "coins": [10,10,8,6,0,0], "k": 1 } assert my_solution.maximumPoints(**test_input) == 30 test_input = { "edges": [[0,1],[2,0],[3,0],[3,4],[5,1],[6,1],[7,2],[8,3],[0,9]], "coins": [6,4,5,2,1,10,10,9,8,10], "k": 1 } assert my_solution.maximumPoints(**test_input) == 55 test_input = { "edges": [[0,1],[2,1],[3,2],[0,4],[1,5],[6,5],[7,1],[5,8],[1,9]], "coins": [8,0,9,5,9,6,2,8,1,8], "k": 9 } assert my_solution.maximumPoints(**test_input) == 10 test_input = { "edges": [[0,1],[2,1],[2,3],[1,4],[3,5],[6,4],[7,6]], "coins": [8,2,3,10,4,5,8,8], "k": 1 } assert my_solution.maximumPoints(**test_input) == 40 test_input = { "edges": [[0,1],[2,0],[0,3],[4,1],[5,2],[6,1],[7,1]], "coins": [3,9,4,4,3,4,10,4], "k": 2 } assert my_solution.maximumPoints(**test_input) == 25 test_input = { "edges": [[1,0],[1,2],[3,0],[4,1],[5,1],[1,6],[1,7]], "coins": [7,4,7,2,5,8,0,7], "k": 8 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[0,1],[2,1],[3,1],[4,2],[5,4],[6,5],[7,5]], "coins": [4,5,7,5,0,4,6,7], "k": 4 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[1,0],[1,2],[1,3]], "coins": [8,4,10,7], "k": 1 } assert my_solution.maximumPoints(**test_input) == 25 test_input = { "edges": [[0,1],[2,1],[2,3],[3,4],[4,5],[6,3],[3,7],[7,8],[9,8]], "coins": [0,2,1,5,8,2,5,3,7,6], "k": 10 } assert my_solution.maximumPoints(**test_input) == 0 test_input = { "edges": [[1,0],[1,2],[3,0],[4,3],[1,5],[6,2],[7,3],[4,8],[4,9]], "coins": [6,5,1,8,8,10,5,7,7,1], "k": 5 } assert my_solution.maximumPoints(**test_input) == 19 test_input = { "edges": [[0,1],[2,0],[2,3],[4,2],[5,0],[3,6],[7,5],[3,8],[9,8]], "coins": [9,6,4,10,4,1,6,1,5,9], "k": 6 } assert my_solution.maximumPoints(**test_input) == 17 test_input = { "edges": [[0,1],[2,1],[3,1],[0,4],[3,5]], "coins": [1,9,3,4,9,3], "k": 6 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[0,1],[0,2]], "coins": [9,3,9], "k": 3 } assert my_solution.maximumPoints(**test_input) == 13 test_input = { "edges": [[0,1],[2,0],[3,1],[2,4],[5,2],[6,5],[7,3],[8,5],[9,5]], "coins": [4,1,3,1,6,1,0,0,0,6], "k": 1 } assert my_solution.maximumPoints(**test_input) == 15 test_input = { "edges": [[0,1]], "coins": [1,7], "k": 10 } assert my_solution.maximumPoints(**test_input) == 1 test_input = { "edges": [[0,1]], "coins": [10,6], "k": 10 } assert my_solution.maximumPoints(**test_input) == 6 test_input = { "edges": [[1,0],[1,2],[2,3],[0,4]], "coins": [6,7,8,1,9], "k": 9 } assert my_solution.maximumPoints(**test_input) == 7 test_input = { "edges": [[0,1]], "coins": [6,6], "k": 0 } assert my_solution.maximumPoints(**test_input) == 12 test_input = { "edges": [[0,1],[0,2],[0,3],[1,4],[5,4],[2,6]], "coins": [9,3,7,2,3,1,2], "k": 8 } assert my_solution.maximumPoints(**test_input) == 6 test_input = { "edges": [[1,0],[0,2],[3,1],[3,4],[2,5]], "coins": [4,0,3,10,5,8], "k": 3 } assert my_solution.maximumPoints(**test_input) == 12 test_input = { "edges": [[0,1],[2,1],[3,2],[4,1],[4,5],[5,6]], "coins": [3,9,2,6,1,9,1], "k": 10 } assert my_solution.maximumPoints(**test_input) == 3 test_input = { "edges": [[1,0]], "coins": [8,8], "k": 8 } assert my_solution.maximumPoints(**test_input) == 6 test_input = { "edges": [[0,1],[1,2],[1,3],[0,4],[5,2]], "coins": [2,3,7,9,7,7], "k": 2 } assert my_solution.maximumPoints(**test_input) == 23 test_input = { "edges": [[0,1],[2,0],[3,1],[4,0],[3,5],[2,6]], "coins": [6,9,7,7,7,9,7], "k": 8 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[1,0],[2,1],[3,0],[0,4],[5,2],[0,6]], "coins": [9,4,7,9,6,2,9], "k": 10 } assert my_solution.maximumPoints(**test_input) == 13 test_input = { "edges": [[0,1],[1,2],[3,2],[4,2],[0,5],[6,4],[7,3]], "coins": [5,5,6,3,0,8,5,7], "k": 7 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[1,0],[2,0],[2,3],[4,0],[5,2],[2,6],[7,3]], "coins": [8,3,4,5,6,1,6,9], "k": 6 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[0,1],[2,0],[0,3],[4,0],[5,0],[6,1],[7,3],[8,1]], "coins": [1,6,3,10,1,9,7,8,7], "k": 5 } assert my_solution.maximumPoints(**test_input) == 17 test_input = { "edges": [[0,1],[2,0],[3,1],[3,4],[2,5],[6,4],[5,7],[5,8],[6,9]], "coins": [3,6,5,6,6,9,5,5,3,10], "k": 10 } assert my_solution.maximumPoints(**test_input) == 4 test_input = { "edges": [[0,1],[2,0],[3,2],[4,1],[4,5],[6,3],[7,2],[8,2],[3,9]], "coins": [2,2,0,0,4,8,8,5,0,10], "k": 8 } assert my_solution.maximumPoints(**test_input) == 1 test_input = { "edges": [[1,0],[0,2],[3,0],[4,3],[4,5],[6,5],[7,5]], "coins": [8,5,7,3,2,3,5,3], "k": 6 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[0,1],[2,0]], "coins": [4,3,8], "k": 3 } assert my_solution.maximumPoints(**test_input) == 7 test_input = { "edges": [[1,0],[2,1],[3,1],[4,2],[5,0],[4,6],[7,3],[8,2]], "coins": [8,9,0,3,9,7,4,8,7], "k": 7 } assert my_solution.maximumPoints(**test_input) == 12 test_input = { "edges": [[0,1],[2,1]], "coins": [1,6,4], "k": 4 } assert my_solution.maximumPoints(**test_input) == 1 test_input = { "edges": [[0,1],[2,1],[3,1]], "coins": [8,9,1,0], "k": 3 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[1,0],[2,1],[0,3],[4,3],[5,4],[4,6]], "coins": [4,1,4,4,0,5,5], "k": 8 } assert my_solution.maximumPoints(**test_input) == 3 test_input = { "edges": [[1,0],[2,1],[3,2],[3,4],[4,5],[6,5],[7,3]], "coins": [10,9,6,8,9,9,0,7], "k": 0 } assert my_solution.maximumPoints(**test_input) == 58 test_input = { "edges": [[0,1],[1,2],[3,1],[2,4],[3,5],[4,6],[5,7],[1,8],[2,9]], "coins": [9,5,7,6,2,5,0,7,5,7], "k": 8 } assert my_solution.maximumPoints(**test_input) == 8 test_input = { "edges": [[0,1],[1,2],[3,1]], "coins": [4,10,2,2], "k": 10 } assert my_solution.maximumPoints(**test_input) == 4 test_input = { "edges": [[1,0],[1,2],[1,3],[2,4],[3,5],[1,6],[7,4],[8,1],[0,9]], "coins": [6,7,1,2,3,7,3,4,8,4], "k": 1 } assert my_solution.maximumPoints(**test_input) == 35 test_input = { "edges": [[0,1],[2,1]], "coins": [4,8,10], "k": 6 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[0,1],[0,2],[2,3],[2,4],[5,3],[3,6],[7,2],[8,5]], "coins": [7,8,4,3,4,8,10,8,1], "k": 7 } assert my_solution.maximumPoints(**test_input) == 12 test_input = { "edges": [[1,0],[2,1],[3,0],[4,2],[0,5]], "coins": [9,9,3,3,4,4], "k": 4 } assert my_solution.maximumPoints(**test_input) == 15 test_input = { "edges": [[0,1],[0,2],[1,3],[2,4],[5,0]], "coins": [7,5,0,10,0,0], "k": 8 } assert my_solution.maximumPoints(**test_input) == 5 test_input = { "edges": [[1,0],[2,1],[2,3],[4,2],[0,5],[6,2],[4,7],[8,5],[0,9]], "coins": [5,2,8,8,6,0,3,2,2,5], "k": 3 } assert my_solution.maximumPoints(**test_input) == 18 test_input = { "edges": [[1,0],[1,2],[3,0],[4,3],[1,5],[6,1],[7,4],[2,8]], "coins": [5,5,2,1,3,8,6,4,3], "k": 1 } assert my_solution.maximumPoints(**test_input) == 28 test_input = { "edges": [[1,0],[0,2]], "coins": [8,2,5], "k": 0 } assert my_solution.maximumPoints(**test_input) == 15 test_input = { "edges": [[1,0],[1,2],[3,2]], "coins": [10,9,2,0], "k": 7 } assert my_solution.maximumPoints(**test_input) == 7 test_input = { "edges": [[0,1],[2,0],[2,3],[0,4],[2,5],[6,4],[7,1],[8,3]], "coins": [10,4,4,8,9,5,5,8,1], "k": 0 } assert my_solution.maximumPoints(**test_input) == 54 test_input = { "edges": [[0,1],[1,2],[1,3],[2,4],[1,5],[6,2],[3,7],[8,4],[9,3]], "coins": [5,1,1,3,5,0,0,1,1,9], "k": 5 } assert my_solution.maximumPoints(**test_input) == 2 test_input = { "edges": [[0,1],[1,2],[3,1],[1,4],[5,3]], "coins": [1,7,1,3,3,7], "k": 7 } assert my_solution.maximumPoints(**test_input) == 1 test_input = { "edges": [[0,1],[1,2],[3,2],[4,3],[5,2],[0,6],[3,7],[5,8]], "coins": [9,1,3,2,1,3,4,2,6], "k": 1 } assert my_solution.maximumPoints(**test_input) == 22 test_input = { "edges": [[0,1],[0,2]], "coins": [5,1,5], "k": 10 } assert my_solution.maximumPoints(**test_input) == 3 test_input = { "edges": [[0,1],[0,2],[3,1],[1,4],[4,5]], "coins": [5,7,8,9,3,10], "k": 7 } assert my_solution.maximumPoints(**test_input) == 9 test_input = { "edges": [[0,1],[0,2],[0,3],[0,4],[0,5],[5,6],[6,7],[8,5]], "coins": [8,3,0,3,4,1,4,0,7], "k": 2 } assert my_solution.maximumPoints(**test_input) == 16 test_input = { "edges": [[1,0],[0,2],[3,0],[4,3],[2,5],[3,6]], "coins": [3,6,7,1,2,8,0], "k": 9 } assert my_solution.maximumPoints(**test_input) == 4 test_input = { "edges": [[1,0]], "coins": [4,0], "k": 9 } assert my_solution.maximumPoints(**test_input) == 2 test_input = { "edges": [[1,0],[2,0],[3,1],[4,1],[5,2],[6,1],[4,7],[5,8]], "coins": [9,7,8,9,6,8,9,1,6], "k": 1 } assert my_solution.maximumPoints(**test_input) == 54 test_input = { "edges": [[1,0],[0,2],[1,3],[0,4],[5,4],[6,0],[3,7]], "coins": [7,10,8,4,6,0,6,2], "k": 4 } assert my_solution.maximumPoints(**test_input) == 21
1,698,546,600
biweekly-contest-116-subarrays-distinct-element-sum-of-squares-i
https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i
subarrays-distinct-element-sum-of-squares-i
{ "questionId": "3163", "questionFrontendId": "2913", "title": "Subarrays Distinct Element Sum of Squares I", "titleSlug": "subarrays-distinct-element-sum-of-squares-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 82, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为: [1]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [1]: 1 个互不相同的元素。 [1,2]: 2 个互不相同的元素。 [2,1]: 2 个互不相同的元素。 [1,2,1]: 2 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 + 22 + 22 + 22 = 15 。 示例 2: 输入:nums = [2,2] 输出:3 解释:三个子数组分别为: [2]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [2,2]: 1 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 = 3 。 提示: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 100 """ class Solution: def sumCounts(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为: [1]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [1]: 1 个互不相同的元素。 [1,2]: 2 个互不相同的元素。 [2,1]: 2 个互不相同的元素。 [1,2,1]: 2 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 + 22 + 22 + 22 = 15 。 示例 2: 输入:nums = [2,2] 输出:3 解释:三个子数组分别为: [2]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [2,2]: 1 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 = 3 。 提示: * 1 <= nums.length <= 100 * 1 <= nums[i] <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def sumCounts(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,1] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [1,1] } assert my_solution.sumCounts(**test_input) == 3 test_input = { "nums": [2,2,5,5] } assert my_solution.sumCounts(**test_input) == 22 test_input = { "nums": [5,2,4,2,1,3,2,4,3,1] } assert my_solution.sumCounts(**test_input) == 578 test_input = { "nums": [2,3,2,1,2,5,3,4,5,2] } assert my_solution.sumCounts(**test_input) == 629 test_input = { "nums": [5,1,5,2,3,5,1,5,1] } assert my_solution.sumCounts(**test_input) == 385 test_input = { "nums": [4,5,4,3,4,2] } assert my_solution.sumCounts(**test_input) == 120 test_input = { "nums": [2] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [3,4,2,5,2,4,1,2,2,5] } assert my_solution.sumCounts(**test_input) == 535 test_input = { "nums": [4,4,2,4,1] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [2,2,5] } assert my_solution.sumCounts(**test_input) == 12 test_input = { "nums": [4,5,1,2,2,1,3,3] } assert my_solution.sumCounts(**test_input) == 266 test_input = { "nums": [3,1,5,5,2,3,2,2,1] } assert my_solution.sumCounts(**test_input) == 334 test_input = { "nums": [2,5,2,5,3,2,5,2] } assert my_solution.sumCounts(**test_input) == 205 test_input = { "nums": [5,4,1,4,5,2,4] } assert my_solution.sumCounts(**test_input) == 203 test_input = { "nums": [1,3,3,4,3,1,2,1] } assert my_solution.sumCounts(**test_input) == 253 test_input = { "nums": [4] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [1,4,2,1,5,4,3,1,4] } assert my_solution.sumCounts(**test_input) == 507 test_input = { "nums": [2,4,5,3,2,5,1,5,4,4] } assert my_solution.sumCounts(**test_input) == 626 test_input = { "nums": [3,4,1,4,5,2,2] } assert my_solution.sumCounts(**test_input) == 220 test_input = { "nums": [3,5,1,1,3] } assert my_solution.sumCounts(**test_input) == 62 test_input = { "nums": [4,3,2,5,3] } assert my_solution.sumCounts(**test_input) == 89 test_input = { "nums": [2,5] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [1,5,1,4,5] } assert my_solution.sumCounts(**test_input) == 70 test_input = { "nums": [5,1] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,5,4,3,3,5,3] } assert my_solution.sumCounts(**test_input) == 138 test_input = { "nums": [5,4,3] } assert my_solution.sumCounts(**test_input) == 20 test_input = { "nums": [5,5,3,3,4,5,4,5,5] } assert my_solution.sumCounts(**test_input) == 234 test_input = { "nums": [3,1,5,5,3,4,5,5,1,4] } assert my_solution.sumCounts(**test_input) == 456 test_input = { "nums": [4,2,3,1,1] } assert my_solution.sumCounts(**test_input) == 81 test_input = { "nums": [4,5,3,1,2,5,5,3,5] } assert my_solution.sumCounts(**test_input) == 434 test_input = { "nums": [3,2,1,2,5,2,4,5,1,5] } assert my_solution.sumCounts(**test_input) == 531 test_input = { "nums": [1,3,1,4,4] } assert my_solution.sumCounts(**test_input) == 62 test_input = { "nums": [5,1,2,1,2,1,2,3,1] } assert my_solution.sumCounts(**test_input) == 257 test_input = { "nums": [2,4] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,5,4,5] } assert my_solution.sumCounts(**test_input) == 28 test_input = { "nums": [3,1,5,5,5,4,3,3,2] } assert my_solution.sumCounts(**test_input) == 334 test_input = { "nums": [3,2,5,2,1,5,3] } assert my_solution.sumCounts(**test_input) == 203 test_input = { "nums": [4,4,2,5,5,4,2,2,1] } assert my_solution.sumCounts(**test_input) == 294 test_input = { "nums": [1] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [1,1,3,3,3,4,4] } assert my_solution.sumCounts(**test_input) == 96 test_input = { "nums": [3,2,2,3,4] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [1,5,3,2,4,4] } assert my_solution.sumCounts(**test_input) == 161 test_input = { "nums": [5,4,1,1,3] } assert my_solution.sumCounts(**test_input) == 69 test_input = { "nums": [4,3,3,5,3,4,5,3,3,1] } assert my_solution.sumCounts(**test_input) == 376 test_input = { "nums": [2,3,4,1,5,1,3,3,4] } assert my_solution.sumCounts(**test_input) == 432 test_input = { "nums": [5,1,4,2,1,1] } assert my_solution.sumCounts(**test_input) == 129 test_input = { "nums": [5,4,4,1] } assert my_solution.sumCounts(**test_input) == 30 test_input = { "nums": [1,5,1,3,2,1] } assert my_solution.sumCounts(**test_input) == 139 test_input = { "nums": [5,3] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,1,4,3] } assert my_solution.sumCounts(**test_input) == 38 test_input = { "nums": [1,5,4,3,4,2,4,5,5,4] } assert my_solution.sumCounts(**test_input) == 513 test_input = { "nums": [4,2,3,4,3,2,5,4,4] } assert my_solution.sumCounts(**test_input) == 378 test_input = { "nums": [2,3,3,2,1,5,2,2] } assert my_solution.sumCounts(**test_input) == 262 test_input = { "nums": [2,1,4,2,4,1,4,3] } assert my_solution.sumCounts(**test_input) == 243 test_input = { "nums": [1,4,4,1,3] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [2,3,2,1] } assert my_solution.sumCounts(**test_input) == 38 test_input = { "nums": [1,4,2,1] } assert my_solution.sumCounts(**test_input) == 43 test_input = { "nums": [2,4,3,2,5,1] } assert my_solution.sumCounts(**test_input) == 169 test_input = { "nums": [2,5,3,2,1,3,1,3,2] } assert my_solution.sumCounts(**test_input) == 348 test_input = { "nums": [4,1] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,3,1,4,3,4,3,4,1] } assert my_solution.sumCounts(**test_input) == 263 test_input = { "nums": [5,1,1,1,4,3] } assert my_solution.sumCounts(**test_input) == 89 test_input = { "nums": [4,5] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [5,2,2,3,1,2,5,3] } assert my_solution.sumCounts(**test_input) == 289 test_input = { "nums": [3,2,4] } assert my_solution.sumCounts(**test_input) == 20 test_input = { "nums": [5,3,5,2,3,2] } assert my_solution.sumCounts(**test_input) == 106 test_input = { "nums": [3,2,1] } assert my_solution.sumCounts(**test_input) == 20 test_input = { "nums": [4,4,2,4,3] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [1,4,4] } assert my_solution.sumCounts(**test_input) == 12 test_input = { "nums": [1,4,4,3,1,2,1,4,3] } assert my_solution.sumCounts(**test_input) == 387 test_input = { "nums": [1,5,4,2,5,5,5,3] } assert my_solution.sumCounts(**test_input) == 249 test_input = { "nums": [2,1,5,3] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [2,3,5,1,5,2,3,2,3,4] } assert my_solution.sumCounts(**test_input) == 533 test_input = { "nums": [5,3,4,4,3,5,4,5] } assert my_solution.sumCounts(**test_input) == 202 test_input = { "nums": [4,4,2,2,4,1] } assert my_solution.sumCounts(**test_input) == 80 test_input = { "nums": [2,3] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,2,3,2] } assert my_solution.sumCounts(**test_input) == 38 test_input = { "nums": [1,2,2] } assert my_solution.sumCounts(**test_input) == 12 test_input = { "nums": [4,1,5,1,5,4,5,1] } assert my_solution.sumCounts(**test_input) == 205 test_input = { "nums": [4,5,3,1] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [4,2,3,4,2,4,3,3,2] } assert my_solution.sumCounts(**test_input) == 275 test_input = { "nums": [4,3] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [1,3,5,4,4,4] } assert my_solution.sumCounts(**test_input) == 113 test_input = { "nums": [1,2,4,2,1,2,2,4,1,3] } assert my_solution.sumCounts(**test_input) == 391 test_input = { "nums": [4,2,5,3,2] } assert my_solution.sumCounts(**test_input) == 89 test_input = { "nums": [3,4,5,3,2,5] } assert my_solution.sumCounts(**test_input) == 144 test_input = { "nums": [5,4,5] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [2,4,5,1] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [5,4,1,4,2,1,5] } assert my_solution.sumCounts(**test_input) == 203 test_input = { "nums": [2,3,3,2,2,3,1] } assert my_solution.sumCounts(**test_input) == 110 test_input = { "nums": [1,4,2,5] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [3] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [5] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [1,3,5,3,2,1,1,4,3] } assert my_solution.sumCounts(**test_input) == 441 test_input = { "nums": [1,5,2,2,3,3,3] } assert my_solution.sumCounts(**test_input) == 140 test_input = { "nums": [1,2,1,4,5,5,4,1,1,1] } assert my_solution.sumCounts(**test_input) == 407 test_input = { "nums": [2,2,1,1,1,2,5,4,5] } assert my_solution.sumCounts(**test_input) == 296 test_input = { "nums": [3,2,3] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [2,1,5,4,3,3,2,1,5,5] } assert my_solution.sumCounts(**test_input) == 652
1,698,503,400
biweekly-contest-116-minimum-number-of-changes-to-make-binary-string-beautiful
https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful
minimum-number-of-changes-to-make-binary-string-beautiful
{ "questionId": "3174", "questionFrontendId": "2914", "title": "Minimum Number of Changes to Make Binary String Beautiful", "titleSlug": "minimum-number-of-changes-to-make-binary-string-beautiful", "isPaidOnly": false, "difficulty": "Medium", "likes": 111, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个长度为偶数下标从 0 开始的二进制字符串 s 。 如果可以将一个字符串分割成一个或者更多满足以下条件的子字符串,那么我们称这个字符串是 美丽的 : * 每个子字符串的长度都是 偶数 。 * 每个子字符串都 只 包含 1 或 只 包含 0 。 你可以将 s 中任一字符改成 0 或者 1 。 请你返回让字符串 s 美丽的 最少 字符修改次数。 示例 1: 输入:s = "1001" 输出:2 解释:我们将 s[1] 改为 1 ,且将 s[3] 改为 0 ,得到字符串 "1100" 。 字符串 "1100" 是美丽的,因为我们可以将它分割成 "11|00" 。 将字符串变美丽最少需要 2 次修改。 示例 2: 输入:s = "10" 输出:1 解释:我们将 s[1] 改为 1 ,得到字符串 "11" 。 字符串 "11" 是美丽的,因为它已经是美丽的。 将字符串变美丽最少需要 1 次修改。 示例 3: 输入:s = "0000" 输出:0 解释:不需要进行任何修改,字符串 "0000" 已经是美丽字符串。 提示: * 2 <= s.length <= 105 * s 的长度为偶数。 * s[i] 要么是 '0' ,要么是 '1' 。 """ class Solution: def minChanges(self, s: str) -> int:
给你一个长度为偶数下标从 0 开始的二进制字符串 s 。 如果可以将一个字符串分割成一个或者更多满足以下条件的子字符串,那么我们称这个字符串是 美丽的 : * 每个子字符串的长度都是 偶数 。 * 每个子字符串都 只 包含 1 或 只 包含 0 。 你可以将 s 中任一字符改成 0 或者 1 。 请你返回让字符串 s 美丽的 最少 字符修改次数。 示例 1: 输入:s = "1001" 输出:2 解释:我们将 s[1] 改为 1 ,且将 s[3] 改为 0 ,得到字符串 "1100" 。 字符串 "1100" 是美丽的,因为我们可以将它分割成 "11|00" 。 将字符串变美丽最少需要 2 次修改。 示例 2: 输入:s = "10" 输出:1 解释:我们将 s[1] 改为 1 ,得到字符串 "11" 。 字符串 "11" 是美丽的,因为它已经是美丽的。 将字符串变美丽最少需要 1 次修改。 示例 3: 输入:s = "0000" 输出:0 解释:不需要进行任何修改,字符串 "0000" 已经是美丽字符串。 提示: * 2 <= s.length <= 105 * s 的长度为偶数。 * s[i] 要么是 '0' ,要么是 '1' 。 请完成下面的代码来解决上述问题: ```python class Solution: def minChanges(self, s: str) -> int: ```
my_solution = Solution() test_input = { "s": "1001" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "10" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "0000" } assert my_solution.minChanges(**test_input) == 0 test_input = { "s": "11000111" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "01010001" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "010010" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "111111111110010001" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "01010000011001001101" } assert my_solution.minChanges(**test_input) == 6 test_input = { "s": "011011100001110111" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "1001000010111010" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "0011" } assert my_solution.minChanges(**test_input) == 0 test_input = { "s": "11100100010010" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "110100" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "01" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "10110010" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "0010" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "01000011000111" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "0001110001" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "000000001010100011" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "100001" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "10010010" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "101100" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "000010" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "011011000001100011" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "01101111" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "000000011101" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "011011011111111011" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "110011" } assert my_solution.minChanges(**test_input) == 0 test_input = { "s": "1111101111101011" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "11000101011001100110" } assert my_solution.minChanges(**test_input) == 8 test_input = { "s": "101101101110110010" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "11011110" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "1011100101" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "0011000101001000" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "00" } assert my_solution.minChanges(**test_input) == 0 test_input = { "s": "10110111010001000010" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "0101" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "0010101100" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "11001110101001" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "010101000011011111" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "0010111010000001" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "0000010101" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "10100011101010" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "101111" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "11" } assert my_solution.minChanges(**test_input) == 0 test_input = { "s": "110110" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "001001110111010101" } assert my_solution.minChanges(**test_input) == 6 test_input = { "s": "11110011" } assert my_solution.minChanges(**test_input) == 0 test_input = { "s": "1100000101101110" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "111010" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "100000100010011100" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "0111" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "0100110011111101" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "010111" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "000100" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "00110111000111110001" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "01010010" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "01110101001001" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "00111100010111" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "001110" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "00010101111101" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "111011" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "011110" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "1010" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "011101010011101011" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "1001110001111100" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "011000101100" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "1110111000111111" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "1110" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "10110110" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "1000" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "0111110110" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "011000011101" } assert my_solution.minChanges(**test_input) == 4 test_input = { "s": "0100101000010101" } assert my_solution.minChanges(**test_input) == 6 test_input = { "s": "0100001100" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "100010010010110001" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "101001111001111001" } assert my_solution.minChanges(**test_input) == 7 test_input = { "s": "111001" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "111010001000" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "0110" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "11011010" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "100111" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "11000110000001" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "010111010011011000" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "000101" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "000010101010101011" } assert my_solution.minChanges(**test_input) == 6 test_input = { "s": "11011000" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "010101" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "10011010110110101100" } assert my_solution.minChanges(**test_input) == 7 test_input = { "s": "10100011" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "11010100000000" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "101100101111010011" } assert my_solution.minChanges(**test_input) == 3 test_input = { "s": "110111111100" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "111110110100" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "011110110101010100" } assert my_solution.minChanges(**test_input) == 6 test_input = { "s": "000111" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "00000010001101" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "01111001101001" } assert my_solution.minChanges(**test_input) == 6 test_input = { "s": "010110011101" } assert my_solution.minChanges(**test_input) == 5 test_input = { "s": "0111011101" } assert my_solution.minChanges(**test_input) == 3
1,698,503,400
biweekly-contest-116-length-of-the-longest-subsequence-that-sums-to-target
https://leetcode.com/problems/length-of-the-longest-subsequence-that-sums-to-target
length-of-the-longest-subsequence-that-sums-to-target
{ "questionId": "3106", "questionFrontendId": "2915", "title": "Length of the Longest Subsequence That Sums to Target", "titleSlug": "length-of-the-longest-subsequence-that-sums-to-target", "isPaidOnly": false, "difficulty": "Medium", "likes": 141, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 和一个整数 target 。 返回和为 target 的 nums 子序列中,子序列 长度的最大值 。如果不存在和为 target 的子序列,返回 -1 。 子序列 指的是从原数组中删除一些或者不删除任何元素后,剩余元素保持原来的顺序构成的数组。 示例 1: 输入:nums = [1,2,3,4,5], target = 9 输出:3 解释:总共有 3 个子序列的和为 9 :[4,5] ,[1,3,5] 和 [2,3,4] 。最长的子序列是 [1,3,5] 和 [2,3,4] 。所以答案为 3 。 示例 2: 输入:nums = [4,1,3,2,1,5], target = 7 输出:4 解释:总共有 5 个子序列的和为 7 :[4,3] ,[4,1,2] ,[4,2,1] ,[1,1,5] 和 [1,3,2,1] 。最长子序列为 [1,3,2,1] 。所以答案为 4 。 示例 3: 输入:nums = [1,1,5,4,5], target = 3 输出:-1 解释:无法得到和为 3 的子序列。 提示: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 1000 * 1 <= target <= 1000 """ class Solution: def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
给你一个下标从 0 开始的整数数组 nums 和一个整数 target 。 返回和为 target 的 nums 子序列中,子序列 长度的最大值 。如果不存在和为 target 的子序列,返回 -1 。 子序列 指的是从原数组中删除一些或者不删除任何元素后,剩余元素保持原来的顺序构成的数组。 示例 1: 输入:nums = [1,2,3,4,5], target = 9 输出:3 解释:总共有 3 个子序列的和为 9 :[4,5] ,[1,3,5] 和 [2,3,4] 。最长的子序列是 [1,3,5] 和 [2,3,4] 。所以答案为 3 。 示例 2: 输入:nums = [4,1,3,2,1,5], target = 7 输出:4 解释:总共有 5 个子序列的和为 7 :[4,3] ,[4,1,2] ,[4,2,1] ,[1,1,5] 和 [1,3,2,1] 。最长子序列为 [1,3,2,1] 。所以答案为 4 。 示例 3: 输入:nums = [1,1,5,4,5], target = 3 输出:-1 解释:无法得到和为 3 的子序列。 提示: * 1 <= nums.length <= 1000 * 1 <= nums[i] <= 1000 * 1 <= target <= 1000 请完成下面的代码来解决上述问题: ```python class Solution: def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3,4,5], "target": 9 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 3 test_input = { "nums": [4,1,3,2,1,5], "target": 7 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 4 test_input = { "nums": [1,1,5,4,5], "target": 3 } assert my_solution.lengthOfLongestSubsequence(**test_input) == -1 test_input = { "nums": [1000], "target": 12 } assert my_solution.lengthOfLongestSubsequence(**test_input) == -1 test_input = { "nums": [1000], "target": 1000 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 1 test_input = { "nums": [1,2], "target": 10 } assert my_solution.lengthOfLongestSubsequence(**test_input) == -1 test_input = { "nums": [1,1000], "target": 5 } assert my_solution.lengthOfLongestSubsequence(**test_input) == -1 test_input = { "nums": [2,3], "target": 3 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 1 test_input = { "nums": [2,3], "target": 5 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 2 test_input = { "nums": [2,3,5], "target": 5 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 2 test_input = { "nums": [1,3,3,7], "target": 1000 } assert my_solution.lengthOfLongestSubsequence(**test_input) == -1 test_input = { "nums": [1,3,3,7], "target": 2 } assert my_solution.lengthOfLongestSubsequence(**test_input) == -1 test_input = { "nums": [1,3,3,8], "target": 7 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 3 test_input = { "nums": [1,1,2,1], "target": 2 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 2 test_input = { "nums": [1,1,1,1], "target": 5 } assert my_solution.lengthOfLongestSubsequence(**test_input) == -1 test_input = { "nums": [1,1,1,2], "target": 3 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 3 test_input = { "nums": [9,12,8,4,11,13,15,7,5], "target": 84 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [11,5,9,11,12,13,12,5,1,8], "target": 87 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [9,11,11,15,4,14,3,2,13,7], "target": 89 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [11,13,6,13,10], "target": 53 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [10,3,5,11,6,12], "target": 47 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [13,3,6,6,6,15,4], "target": 53 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [1,6,15,6,14,13,14], "target": 69 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [10,7,8,14,15], "target": 54 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [14,15,8,10,8,7], "target": 62 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [7,9,14,14,9,14,5,12,10], "target": 94 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [1,10,6,14,5,13,3,7,10,10], "target": 79 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [5,2,8,6,7,12,13,4,1], "target": 58 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [12,8,2,4,1], "target": 27 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [10,14,11,13,2,11], "target": 61 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [10,2,13,5,7,15], "target": 52 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [3,1,10,1,10,1,2,9,5,13], "target": 55 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [5,13,2,13,9,4,5,7], "target": 58 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [1,15,5,12,13,10,14,8], "target": 78 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [7,4,14,10,13], "target": 48 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [6,14,14,6,2,9,1,4,10], "target": 66 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [14,15,7,5,7,10,6,14,10,11], "target": 99 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [15,13,8,8,6], "target": 50 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [2,6,8,9,13,3], "target": 41 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [13,15,9,3,8,1,9,2,15,5], "target": 80 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [5,13,9,11,6,1], "target": 45 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [7,10,15,7,14,2], "target": 55 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [12,14,13,13,13], "target": 65 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [12,8,7,9,3,10,3,8,2], "target": 62 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [11,1,14,13,14,4,14,11], "target": 82 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [5,9,11,2,5,2,7,11,5,3], "target": 60 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [5,15,3,13,14,15,10], "target": 75 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [10,8,2,2,9], "target": 31 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [7,15,4,3,9,15,12,1,12], "target": 78 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [3,1,12,15,5,10], "target": 46 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [5,3,12,7,5,2,12,10,12,5], "target": 73 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [6,10,3,1,7,11,9,8,13,12], "target": 80 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [11,3,4,11,9], "target": 38 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [15,12,12,13,6,6,4,1], "target": 69 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [9,2,10,7,10,11,14,11,8], "target": 82 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [4,4,3,9,6,8,4,7,7], "target": 52 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [10,14,4,15,9,5], "target": 57 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [4,13,2,3,13,11,8,6], "target": 60 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [1,7,8,14,15,9,8,10,13,7], "target": 92 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [7,7,6,14,7,4], "target": 45 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [9,10,9,7,14,3,6,4,6], "target": 68 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [15,13,14,5,7,13,11,14], "target": 92 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [1,1,10,12,5,6,15,6,8], "target": 64 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [14,13,13,11,14,13,8], "target": 86 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [3,14,4,2,10,3,7], "target": 43 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [6,1,3,11,9,2,10,6,12], "target": 60 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [6,2,5,4,12], "target": 29 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [7,11,15,1,9,9,11], "target": 63 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [7,12,10,15,6,15,14,2], "target": 81 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [12,3,10,12,13,3,4,7,15], "target": 79 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [14,6,11,2,10,1,12,9,2], "target": 67 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [5,8,12,6,15,13,11], "target": 70 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [11,6,1,6,2,6,15], "target": 47 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [12,7,15,10,5,4,7,12,12], "target": 84 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [11,4,4,9,10,7,12], "target": 57 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [4,12,15,6,15,1,4,4,2], "target": 63 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [3,13,4,15,1], "target": 36 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [14,3,7,14,7,7,1,6], "target": 59 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [15,13,1,14,6,8], "target": 57 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [14,2,3,10,15], "target": 44 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [5,5,3,7,12,10,11], "target": 53 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [3,7,3,5,3,14,8], "target": 43 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [5,7,9,14,9,14,4,1,4], "target": 67 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [12,7,8,6,3,9,7,3,4,4], "target": 63 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [9,12,1,4,9,6,15,9,7], "target": 72 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [9,13,12,10,4,9,9,4,4,13], "target": 87 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [13,5,6,8,2,13,1,5,6], "target": 59 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [7,9,8,9,9,3,5], "target": 50 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [15,1,14,8,2,1,10,15,15], "target": 81 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [13,14,1,9,12,2], "target": 51 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [13,12,12,13,8,11,3,14,13], "target": 99 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [2,2,1,12,10,7,11,5,5], "target": 55 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [13,10,3,4,10,3], "target": 43 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 6 test_input = { "nums": [8,9,1,5,8,7,6,8], "target": 52 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [10,1,4,10,9,13,14], "target": 61 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 7 test_input = { "nums": [3,14,11,4,7,9,7,6,8,11], "target": 80 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 10 test_input = { "nums": [4,11,6,6,14,12,2,9,1], "target": 65 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 9 test_input = { "nums": [9,2,15,12,15,6,4,12], "target": 75 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 8 test_input = { "nums": [4,3,5,3,2], "target": 17 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5 test_input = { "nums": [4,3,13,6,9], "target": 35 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 5
1,698,503,400
biweekly-contest-116-subarrays-distinct-element-sum-of-squares-ii
https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-ii
subarrays-distinct-element-sum-of-squares-ii
{ "questionId": "3139", "questionFrontendId": "2916", "title": "Subarrays Distinct Element Sum of Squares II", "titleSlug": "subarrays-distinct-element-sum-of-squares-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 115, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为: [1]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [1]: 1 个互不相同的元素。 [1,2]: 2 个互不相同的元素。 [2,1]: 2 个互不相同的元素。 [1,2,1]: 2 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 + 22 + 22 + 22 = 15 。 示例 2: 输入:nums = [2,2] 输出:3 解释:三个子数组分别为: [2]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [2,2]: 1 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 = 3 。 提示: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 105 """ class Solution: def sumCounts(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为: [1]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [1]: 1 个互不相同的元素。 [1,2]: 2 个互不相同的元素。 [2,1]: 2 个互不相同的元素。 [1,2,1]: 2 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 + 22 + 22 + 22 = 15 。 示例 2: 输入:nums = [2,2] 输出:3 解释:三个子数组分别为: [2]: 1 个互不相同的元素。 [2]: 1 个互不相同的元素。 [2,2]: 1 个互不相同的元素。 所有不同计数的平方和为 12 + 12 + 12 = 3 。 提示: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 105 请完成下面的代码来解决上述问题: ```python class Solution: def sumCounts(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,1] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [2,2] } assert my_solution.sumCounts(**test_input) == 3 test_input = { "nums": [2,2,5,5] } assert my_solution.sumCounts(**test_input) == 22 test_input = { "nums": [5,2,4,2,1,3,2,4,3,1] } assert my_solution.sumCounts(**test_input) == 578 test_input = { "nums": [2,3,2,1,2,5,3,4,5,2] } assert my_solution.sumCounts(**test_input) == 629 test_input = { "nums": [5,1,5,2,3,5,1,5,1] } assert my_solution.sumCounts(**test_input) == 385 test_input = { "nums": [4,5,4,3,4,2] } assert my_solution.sumCounts(**test_input) == 120 test_input = { "nums": [2] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [3,4,2,5,2,4,1,2,2,5] } assert my_solution.sumCounts(**test_input) == 535 test_input = { "nums": [4,4,2,4,1] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [2,2,5] } assert my_solution.sumCounts(**test_input) == 12 test_input = { "nums": [4,5,1,2,2,1,3,3] } assert my_solution.sumCounts(**test_input) == 266 test_input = { "nums": [3,1,5,5,2,3,2,2,1] } assert my_solution.sumCounts(**test_input) == 334 test_input = { "nums": [2,5,2,5,3,2,5,2] } assert my_solution.sumCounts(**test_input) == 205 test_input = { "nums": [5,4,1,4,5,2,4] } assert my_solution.sumCounts(**test_input) == 203 test_input = { "nums": [1,3,3,4,3,1,2,1] } assert my_solution.sumCounts(**test_input) == 253 test_input = { "nums": [4] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [1,4,2,1,5,4,3,1,4] } assert my_solution.sumCounts(**test_input) == 507 test_input = { "nums": [2,4,5,3,2,5,1,5,4,4] } assert my_solution.sumCounts(**test_input) == 626 test_input = { "nums": [3,4,1,4,5,2,2] } assert my_solution.sumCounts(**test_input) == 220 test_input = { "nums": [3,5,1,1,3] } assert my_solution.sumCounts(**test_input) == 62 test_input = { "nums": [4,3,2,5,3] } assert my_solution.sumCounts(**test_input) == 89 test_input = { "nums": [2,5] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [1,5,1,4,5] } assert my_solution.sumCounts(**test_input) == 70 test_input = { "nums": [5,1] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,5,4,3,3,5,3] } assert my_solution.sumCounts(**test_input) == 138 test_input = { "nums": [5,4,3] } assert my_solution.sumCounts(**test_input) == 20 test_input = { "nums": [5,5,3,3,4,5,4,5,5] } assert my_solution.sumCounts(**test_input) == 234 test_input = { "nums": [3,1,5,5,3,4,5,5,1,4] } assert my_solution.sumCounts(**test_input) == 456 test_input = { "nums": [4,2,3,1,1] } assert my_solution.sumCounts(**test_input) == 81 test_input = { "nums": [4,5,3,1,2,5,5,3,5] } assert my_solution.sumCounts(**test_input) == 434 test_input = { "nums": [3,2,1,2,5,2,4,5,1,5] } assert my_solution.sumCounts(**test_input) == 531 test_input = { "nums": [1,3,1,4,4] } assert my_solution.sumCounts(**test_input) == 62 test_input = { "nums": [5,1,2,1,2,1,2,3,1] } assert my_solution.sumCounts(**test_input) == 257 test_input = { "nums": [2,4] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,5,4,5] } assert my_solution.sumCounts(**test_input) == 28 test_input = { "nums": [3,1,5,5,5,4,3,3,2] } assert my_solution.sumCounts(**test_input) == 334 test_input = { "nums": [3,2,5,2,1,5,3] } assert my_solution.sumCounts(**test_input) == 203 test_input = { "nums": [4,4,2,5,5,4,2,2,1] } assert my_solution.sumCounts(**test_input) == 294 test_input = { "nums": [1] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [1,1,3,3,3,4,4] } assert my_solution.sumCounts(**test_input) == 96 test_input = { "nums": [3,2,2,3,4] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [1,5,3,2,4,4] } assert my_solution.sumCounts(**test_input) == 161 test_input = { "nums": [5,4,1,1,3] } assert my_solution.sumCounts(**test_input) == 69 test_input = { "nums": [4,3,3,5,3,4,5,3,3,1] } assert my_solution.sumCounts(**test_input) == 376 test_input = { "nums": [2,3,4,1,5,1,3,3,4] } assert my_solution.sumCounts(**test_input) == 432 test_input = { "nums": [5,1,4,2,1,1] } assert my_solution.sumCounts(**test_input) == 129 test_input = { "nums": [5,4,4,1] } assert my_solution.sumCounts(**test_input) == 30 test_input = { "nums": [1,5,1,3,2,1] } assert my_solution.sumCounts(**test_input) == 139 test_input = { "nums": [5,3] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,1,4,3] } assert my_solution.sumCounts(**test_input) == 38 test_input = { "nums": [1,5,4,3,4,2,4,5,5,4] } assert my_solution.sumCounts(**test_input) == 513 test_input = { "nums": [4,2,3,4,3,2,5,4,4] } assert my_solution.sumCounts(**test_input) == 378 test_input = { "nums": [2,3,3,2,1,5,2,2] } assert my_solution.sumCounts(**test_input) == 262 test_input = { "nums": [2,1,4,2,4,1,4,3] } assert my_solution.sumCounts(**test_input) == 243 test_input = { "nums": [1,4,4,1,3] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [2,3,2,1] } assert my_solution.sumCounts(**test_input) == 38 test_input = { "nums": [1,4,2,1] } assert my_solution.sumCounts(**test_input) == 43 test_input = { "nums": [2,4,3,2,5,1] } assert my_solution.sumCounts(**test_input) == 169 test_input = { "nums": [2,5,3,2,1,3,1,3,2] } assert my_solution.sumCounts(**test_input) == 348 test_input = { "nums": [4,1] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,3,1,4,3,4,3,4,1] } assert my_solution.sumCounts(**test_input) == 263 test_input = { "nums": [5,1,1,1,4,3] } assert my_solution.sumCounts(**test_input) == 89 test_input = { "nums": [4,5] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [5,2,2,3,1,2,5,3] } assert my_solution.sumCounts(**test_input) == 289 test_input = { "nums": [3,2,4] } assert my_solution.sumCounts(**test_input) == 20 test_input = { "nums": [5,3,5,2,3,2] } assert my_solution.sumCounts(**test_input) == 106 test_input = { "nums": [3,2,1] } assert my_solution.sumCounts(**test_input) == 20 test_input = { "nums": [4,4,2,4,3] } assert my_solution.sumCounts(**test_input) == 57 test_input = { "nums": [1,4,4] } assert my_solution.sumCounts(**test_input) == 12 test_input = { "nums": [1,4,4,3,1,2,1,4,3] } assert my_solution.sumCounts(**test_input) == 387 test_input = { "nums": [1,5,4,2,5,5,5,3] } assert my_solution.sumCounts(**test_input) == 249 test_input = { "nums": [2,1,5,3] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [2,3,5,1,5,2,3,2,3,4] } assert my_solution.sumCounts(**test_input) == 533 test_input = { "nums": [5,3,4,4,3,5,4,5] } assert my_solution.sumCounts(**test_input) == 202 test_input = { "nums": [4,4,2,2,4,1] } assert my_solution.sumCounts(**test_input) == 80 test_input = { "nums": [2,3] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [4,2,3,2] } assert my_solution.sumCounts(**test_input) == 38 test_input = { "nums": [1,2,2] } assert my_solution.sumCounts(**test_input) == 12 test_input = { "nums": [4,1,5,1,5,4,5,1] } assert my_solution.sumCounts(**test_input) == 205 test_input = { "nums": [4,5,3,1] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [4,2,3,4,2,4,3,3,2] } assert my_solution.sumCounts(**test_input) == 275 test_input = { "nums": [4,3] } assert my_solution.sumCounts(**test_input) == 6 test_input = { "nums": [1,3,5,4,4,4] } assert my_solution.sumCounts(**test_input) == 113 test_input = { "nums": [1,2,4,2,1,2,2,4,1,3] } assert my_solution.sumCounts(**test_input) == 391 test_input = { "nums": [4,2,5,3,2] } assert my_solution.sumCounts(**test_input) == 89 test_input = { "nums": [3,4,5,3,2,5] } assert my_solution.sumCounts(**test_input) == 144 test_input = { "nums": [5,4,5] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [2,4,5,1] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [5,4,1,4,2,1,5] } assert my_solution.sumCounts(**test_input) == 203 test_input = { "nums": [2,3,3,2,2,3,1] } assert my_solution.sumCounts(**test_input) == 110 test_input = { "nums": [1,4,2,5] } assert my_solution.sumCounts(**test_input) == 50 test_input = { "nums": [3] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [5] } assert my_solution.sumCounts(**test_input) == 1 test_input = { "nums": [1,3,5,3,2,1,1,4,3] } assert my_solution.sumCounts(**test_input) == 441 test_input = { "nums": [1,5,2,2,3,3,3] } assert my_solution.sumCounts(**test_input) == 140 test_input = { "nums": [1,2,1,4,5,5,4,1,1,1] } assert my_solution.sumCounts(**test_input) == 407 test_input = { "nums": [2,2,1,1,1,2,5,4,5] } assert my_solution.sumCounts(**test_input) == 296 test_input = { "nums": [3,2,3] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [2,1,5,4,3,3,2,1,5,5] } assert my_solution.sumCounts(**test_input) == 652
1,698,503,400
weekly-contest-368-minimum-sum-of-mountain-triplets-i
https://leetcode.com/problems/minimum-sum-of-mountain-triplets-i
minimum-sum-of-mountain-triplets-i
{ "questionId": "3176", "questionFrontendId": "2908", "title": "Minimum Sum of Mountain Triplets I", "titleSlug": "minimum-sum-of-mountain-triplets-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 113, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。 示例 2: 输入:nums = [5,4,8,7,10,2] 输出:13 解释:三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为: - 1 < 3 < 5 - nums[1] < nums[3] 且 nums[5] < nums[3] 这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。 示例 3: 输入:nums = [6,5,4,3,4,5] 输出:-1 解释:可以证明 nums 中不存在山形三元组。 提示: * 3 <= nums.length <= 50 * 1 <= nums[i] <= 50 """ class Solution: def minimumSum(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。 示例 2: 输入:nums = [5,4,8,7,10,2] 输出:13 解释:三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为: - 1 < 3 < 5 - nums[1] < nums[3] 且 nums[5] < nums[3] 这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。 示例 3: 输入:nums = [6,5,4,3,4,5] 输出:-1 解释:可以证明 nums 中不存在山形三元组。 提示: * 3 <= nums.length <= 50 * 1 <= nums[i] <= 50 请完成下面的代码来解决上述问题: ```python class Solution: def minimumSum(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [8,6,1,5,3] } assert my_solution.minimumSum(**test_input) == 9 test_input = { "nums": [5,4,8,7,10,2] } assert my_solution.minimumSum(**test_input) == 13 test_input = { "nums": [6,5,4,3,4,5] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [50,50,50] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [49,50,48] } assert my_solution.minimumSum(**test_input) == 147 test_input = { "nums": [48,50,49] } assert my_solution.minimumSum(**test_input) == 147 test_input = { "nums": [1,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,2,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,3,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [2,3,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [2,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,2,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,3,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,3,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,1,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,1,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,1,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,3,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,4,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,1,4,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,1,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,1,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,1,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,1,2] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,1,3] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,1,4] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,2,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,3,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,2,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,3,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,4,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,4,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,2,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,2,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,1,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,1,2] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,1,3] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,1,4] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,3,2,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,2,3] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,2,4] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,3,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,4,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,4,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,3,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,4,1,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,1,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,1,3] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,1,4] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,4,2,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,2,3] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,2,4] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,4,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,3,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,4,3,4] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,4,4,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,4,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,4,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,1,3] } assert my_solution.minimumSum(**test_input) == -1
1,697,941,800
weekly-contest-368-minimum-sum-of-mountain-triplets-ii
https://leetcode.com/problems/minimum-sum-of-mountain-triplets-ii
minimum-sum-of-mountain-triplets-ii
{ "questionId": "3186", "questionFrontendId": "2909", "title": "Minimum Sum of Mountain Triplets II", "titleSlug": "minimum-sum-of-mountain-triplets-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 160, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。 示例 2: 输入:nums = [5,4,8,7,10,2] 输出:13 解释:三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为: - 1 < 3 < 5 - nums[1] < nums[3] 且 nums[5] < nums[3] 这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。 示例 3: 输入:nums = [6,5,4,3,4,5] 输出:-1 解释:可以证明 nums 中不存在山形三元组。 提示: * 3 <= nums.length <= 105 * 1 <= nums[i] <= 108 """ class Solution: def minimumSum(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。 示例 2: 输入:nums = [5,4,8,7,10,2] 输出:13 解释:三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为: - 1 < 3 < 5 - nums[1] < nums[3] 且 nums[5] < nums[3] 这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。 示例 3: 输入:nums = [6,5,4,3,4,5] 输出:-1 解释:可以证明 nums 中不存在山形三元组。 提示: * 3 <= nums.length <= 105 * 1 <= nums[i] <= 108 请完成下面的代码来解决上述问题: ```python class Solution: def minimumSum(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [8,6,1,5,3] } assert my_solution.minimumSum(**test_input) == 9 test_input = { "nums": [5,4,8,7,10,2] } assert my_solution.minimumSum(**test_input) == 13 test_input = { "nums": [6,5,4,3,4,5] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [50,50,50] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [49,50,48] } assert my_solution.minimumSum(**test_input) == 147 test_input = { "nums": [48,50,49] } assert my_solution.minimumSum(**test_input) == 147 test_input = { "nums": [99999999,100000000,99999999] } assert my_solution.minimumSum(**test_input) == 299999998 test_input = { "nums": [1,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,2,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,3,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [2,3,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [2,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,2,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,3,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,3,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [3,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,1,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,1,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,2,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,1,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,1,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,3,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,1,4,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,1,4,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,1,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,1,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,2,1,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,1,2] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,1,3] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,1,4] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,2,2] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,2,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,2,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,3,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,3,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,2,4,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,2,4,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,2,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,2,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,1,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,1,2] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,1,3] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,1,4] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,3,2,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,2,3] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,2,4] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,3,3] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,3,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,3,4,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,3,4,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,3,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,3,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [1,4,1,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,1,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,1,3] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,1,4] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,2,1] } assert my_solution.minimumSum(**test_input) == 4 test_input = { "nums": [1,4,2,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,2,3] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,2,4] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,3,1] } assert my_solution.minimumSum(**test_input) == 5 test_input = { "nums": [1,4,3,2] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,3,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,4,3,4] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,4,4,1] } assert my_solution.minimumSum(**test_input) == 6 test_input = { "nums": [1,4,4,2] } assert my_solution.minimumSum(**test_input) == 7 test_input = { "nums": [1,4,4,3] } assert my_solution.minimumSum(**test_input) == 8 test_input = { "nums": [1,4,4,4] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,1,1] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [2,1,1,2] } assert my_solution.minimumSum(**test_input) == -1
1,697,941,800
weekly-contest-368-minimum-number-of-groups-to-create-a-valid-assignment
https://leetcode.com/problems/minimum-number-of-groups-to-create-a-valid-assignment
minimum-number-of-groups-to-create-a-valid-assignment
{ "questionId": "3166", "questionFrontendId": "2910", "title": "Minimum Number of Groups to Create a Valid Assignment", "titleSlug": "minimum-number-of-groups-to-create-a-valid-assignment", "isPaidOnly": false, "difficulty": "Medium", "likes": 231, "dislikes": 151, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 下标从 0 开始的整数数组 nums 。 我们想将下标进行分组,使得 [0, n - 1] 内所有下标 i 都 恰好 被分到其中一组。 如果以下条件成立,我们说这个分组方案是合法的: * 对于每个组 g ,同一组内所有下标在 nums 中对应的数值都相等。 * 对于任意两个组 g1 和 g2 ,两个组中 下标数量 的 差值不超过 1 。 请你返回一个整数,表示得到一个合法分组方案的 最少 组数。 示例 1: 输入:nums = [3,2,3,2,3] 输出:2 解释:一个得到 2 个分组的方案如下,中括号内的数字都是下标: 组 1 -> [0,2,4] 组 2 -> [1,3] 所有下标都只属于一个组。 组 1 中,nums[0] == nums[2] == nums[4] ,所有下标对应的数值都相等。 组 2 中,nums[1] == nums[3] ,所有下标对应的数值都相等。 组 1 中下标数目为 3 ,组 2 中下标数目为 2 。 两者之差不超过 1 。 无法得到一个小于 2 组的答案,因为如果只有 1 组,组内所有下标对应的数值都要相等。 所以答案为 2 。 示例 2: 输入:nums = [10,10,10,3,1,1] 输出:4 解释:一个得到 2 个分组的方案如下,中括号内的数字都是下标: 组 1 -> [0] 组 2 -> [1,2] 组 3 -> [3] 组 4 -> [4,5] 分组方案满足题目要求的两个条件。 无法得到一个小于 4 组的答案。 所以答案为 4 。 提示: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 """ class Solution: def minGroupsForValidAssignment(self, nums: List[int]) -> int:
给你一个长度为 n 下标从 0 开始的整数数组 nums 。 我们想将下标进行分组,使得 [0, n - 1] 内所有下标 i 都 恰好 被分到其中一组。 如果以下条件成立,我们说这个分组方案是合法的: * 对于每个组 g ,同一组内所有下标在 nums 中对应的数值都相等。 * 对于任意两个组 g1 和 g2 ,两个组中 下标数量 的 差值不超过 1 。 请你返回一个整数,表示得到一个合法分组方案的 最少 组数。 示例 1: 输入:nums = [3,2,3,2,3] 输出:2 解释:一个得到 2 个分组的方案如下,中括号内的数字都是下标: 组 1 -> [0,2,4] 组 2 -> [1,3] 所有下标都只属于一个组。 组 1 中,nums[0] == nums[2] == nums[4] ,所有下标对应的数值都相等。 组 2 中,nums[1] == nums[3] ,所有下标对应的数值都相等。 组 1 中下标数目为 3 ,组 2 中下标数目为 2 。 两者之差不超过 1 。 无法得到一个小于 2 组的答案,因为如果只有 1 组,组内所有下标对应的数值都要相等。 所以答案为 2 。 示例 2: 输入:nums = [10,10,10,3,1,1] 输出:4 解释:一个得到 2 个分组的方案如下,中括号内的数字都是下标: 组 1 -> [0] 组 2 -> [1,2] 组 3 -> [3] 组 4 -> [4,5] 分组方案满足题目要求的两个条件。 无法得到一个小于 4 组的答案。 所以答案为 4 。 提示: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def minGroupsForValidAssignment(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [3,2,3,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [10,10,10,3,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 1 test_input = { "nums": [1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 1 test_input = { "nums": [3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 1 test_input = { "nums": [3,4] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [10,4] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [10,10] } assert my_solution.minGroupsForValidAssignment(**test_input) == 1 test_input = { "nums": [14,15] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [1,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 1 test_input = { "nums": [1,2,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [1,7,10] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,2,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,3,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,8,5] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [3,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,10,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [4,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [5,4,8] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [5,6,8] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [5,9,4] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [6,4,9] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [9,2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [10,5,5] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [14,7,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,1,1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,2,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,2,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [1,2,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,2,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,3,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,3,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,2,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,2,3,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [2,3,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,3,2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,5,10,4] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [3,2,2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [3,3,2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [5,1,5,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [13,11,4,13] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,1,1,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 1 test_input = { "nums": [1,2,2,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,2,2,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,1,1,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,1,3,3,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,3,1,2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,3,2,2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,3,3,1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [3,1,1,3,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,1,3,2,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [3,1,3,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [3,2,2,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [3,3,1,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [4,1,7,8,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 5 test_input = { "nums": [7,5,9,4,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 5 test_input = { "nums": [10,2,2,1,10] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,1,1,3,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [1,1,3,1,1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,2,1,3,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,3,1,2,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [1,5,12,11,5,9] } assert my_solution.minGroupsForValidAssignment(**test_input) == 5 test_input = { "nums": [1,6,5,8,7,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 5 test_input = { "nums": [2,1,1,1,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,1,2,1,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,1,3,2,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,2,1,1,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,2,1,2,3,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,3,3,1,2,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,3,3,1,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,5,9,3,1,4] } assert my_solution.minGroupsForValidAssignment(**test_input) == 6 test_input = { "nums": [3,2,2,3,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [3,3,1,2,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [6,7,4,8,8,10] } assert my_solution.minGroupsForValidAssignment(**test_input) == 5 test_input = { "nums": [8,5,8,1,4,5] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [8,7,4,5,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 6 test_input = { "nums": [10,8,3,3,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 5 test_input = { "nums": [1,2,1,2,1,1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [1,2,2,1,1,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [1,2,3,3,3,3,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 5 test_input = { "nums": [1,3,1,7,4,5,8] } assert my_solution.minGroupsForValidAssignment(**test_input) == 6 test_input = { "nums": [2,1,1,3,3,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,1,3,3,3,1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,2,2,1,2,3,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,2,2,2,2,1,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [2,3,1,3,1,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [2,3,1,3,2,1,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 3 test_input = { "nums": [3,1,3,2,3,3,2] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4
1,697,941,800
weekly-contest-368-minimum-changes-to-make-k-semi-palindromes
https://leetcode.com/problems/minimum-changes-to-make-k-semi-palindromes
minimum-changes-to-make-k-semi-palindromes
{ "questionId": "2879", "questionFrontendId": "2911", "title": "Minimum Changes to Make K Semi-palindromes", "titleSlug": "minimum-changes-to-make-k-semi-palindromes", "isPaidOnly": false, "difficulty": "Hard", "likes": 50, "dislikes": 78, "categoryTitle": "Algorithms" }
""" 给你一个字符串 s 和一个整数 k ,请你将 s 分成 k 个 子字符串 ,使得每个 子字符串 变成 半回文串 需要修改的字符数目最少。 请你返回一个整数,表示需要修改的 最少 字符数目。 注意: * 如果一个字符串从左往右和从右往左读是一样的,那么它是一个 回文串 。 * 如果长度为 len 的字符串存在一个满足 1 <= d < len 的正整数 d ,len % d == 0 成立且所有对 d 做除法余数相同的下标对应的字符连起来得到的字符串都是 回文串 ,那么我们说这个字符串是 半回文串 。比方说 "aa" ,"aba" ,"adbgad" 和 "abab" 都是 半回文串 ,而 "a" ,"ab" 和 "abca" 不是。 * 子字符串 指的是一个字符串中一段连续的字符序列。 示例 1: 输入:s = "abcac", k = 2 输出:1 解释:我们可以将 s 分成子字符串 "ab" 和 "cac" 。子字符串 "cac" 已经是半回文串。如果我们将 "ab" 变成 "aa" ,它也会变成一个 d = 1 的半回文串。 该方案是将 s 分成 2 个子字符串的前提下,得到 2 个半回文子字符串需要的最少修改次数。所以答案为 1 。 示例 2: 输入:s = "abcdef", k = 2 输出:2 解释:我们可以将 s 分成子字符串 "abc" 和 "def" 。子字符串 "abc" 和 "def" 都需要修改一个字符得到半回文串,所以我们总共需要 2 次字符修改使所有子字符串变成半回文串。 该方案是将 s 分成 2 个子字符串的前提下,得到 2 个半回文子字符串需要的最少修改次数。所以答案为 2 。 示例 3: 输入:s = "aabbaa", k = 3 输出:0 解释:我们可以将 s 分成子字符串 "aa" ,"bb" 和 "aa" 。 字符串 "aa" 和 "bb" 都已经是半回文串了。所以答案为 0 。 提示: * 2 <= s.length <= 200 * 1 <= k <= s.length / 2 * s 只包含小写英文字母。 """ class Solution: def minimumChanges(self, s: str, k: int) -> int:
给你一个字符串 s 和一个整数 k ,请你将 s 分成 k 个 子字符串 ,使得每个 子字符串 变成 半回文串 需要修改的字符数目最少。 请你返回一个整数,表示需要修改的 最少 字符数目。 注意: * 如果一个字符串从左往右和从右往左读是一样的,那么它是一个 回文串 。 * 如果长度为 len 的字符串存在一个满足 1 <= d < len 的正整数 d ,len % d == 0 成立且所有对 d 做除法余数相同的下标对应的字符连起来得到的字符串都是 回文串 ,那么我们说这个字符串是 半回文串 。比方说 "aa" ,"aba" ,"adbgad" 和 "abab" 都是 半回文串 ,而 "a" ,"ab" 和 "abca" 不是。 * 子字符串 指的是一个字符串中一段连续的字符序列。 示例 1: 输入:s = "abcac", k = 2 输出:1 解释:我们可以将 s 分成子字符串 "ab" 和 "cac" 。子字符串 "cac" 已经是半回文串。如果我们将 "ab" 变成 "aa" ,它也会变成一个 d = 1 的半回文串。 该方案是将 s 分成 2 个子字符串的前提下,得到 2 个半回文子字符串需要的最少修改次数。所以答案为 1 。 示例 2: 输入:s = "abcdef", k = 2 输出:2 解释:我们可以将 s 分成子字符串 "abc" 和 "def" 。子字符串 "abc" 和 "def" 都需要修改一个字符得到半回文串,所以我们总共需要 2 次字符修改使所有子字符串变成半回文串。 该方案是将 s 分成 2 个子字符串的前提下,得到 2 个半回文子字符串需要的最少修改次数。所以答案为 2 。 示例 3: 输入:s = "aabbaa", k = 3 输出:0 解释:我们可以将 s 分成子字符串 "aa" ,"bb" 和 "aa" 。 字符串 "aa" 和 "bb" 都已经是半回文串了。所以答案为 0 。 提示: * 2 <= s.length <= 200 * 1 <= k <= s.length / 2 * s 只包含小写英文字母。 请完成下面的代码来解决上述问题: ```python class Solution: def minimumChanges(self, s: str, k: int) -> int: ```
my_solution = Solution() test_input = { "s": "abcac", "k": 2 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "abcdef", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "aabbaa", "k": 3 } assert my_solution.minimumChanges(**test_input) == 0 test_input = { "s": "aq", "k": 1 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "bb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 0 test_input = { "s": "aac", "k": 1 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "abcc", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "acba", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "edaswf", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "aabcbaa", "k": 1 } assert my_solution.minimumChanges(**test_input) == 0 test_input = { "s": "dqpldq", "k": 3 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "eksddulf", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "aaaaacabbb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "aaabacacbb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "abbbbacaaa", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "abcccbaccb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "baacbbbaba", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "babcbaccba", "k": 1 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "cabbcabcbc", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "ccbccaaabb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "cccbabbbbc", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "cccccbaaac", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "dyfnbbbqbm", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "hafrypzupv", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "aabcacccabc", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "abbcaabaaac", "k": 1 } assert my_solution.minimumChanges(**test_input) == 5 test_input = { "s": "baabaabbcb", "k": 2 } assert my_solution.minimumChanges(**test_input) == 0 test_input = { "s": "bbbabcbaccb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "bcababccaa", "k": 2 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "bcacacacaab", "k": 1 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "bcacccacbaa", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "bccaaccacb", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "caacbacbaca", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "ccccaaacca", "k": 2 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "efrsgmjneph", "k": 1 } assert my_solution.minimumChanges(**test_input) == 5 test_input = { "s": "ehdvhthgbxq", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "eymakkvrvc", "k": 2 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "gilkaelnfr", "k": 2 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "iiaenfiasiv", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "piazrazesdk", "k": 1 } assert my_solution.minimumChanges(**test_input) == 5 test_input = { "s": "pypwcllynf", "k": 2 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "uqicxuvkorn", "k": 1 } assert my_solution.minimumChanges(**test_input) == 5 test_input = { "s": "ziirnywodfz", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "zpogsiabazr", "k": 1 } assert my_solution.minimumChanges(**test_input) == 5 test_input = { "s": "aacacaacabba", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "abacacabba", "k": 3 } assert my_solution.minimumChanges(**test_input) == 0 test_input = { "s": "acbcbccccba", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "bbcaaaaacbb", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "cbabaabccba", "k": 2 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "cbacccbabcaa", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "cbbcbcacca", "k": 3 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "ccaabbbccacb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "ccabcbbcaa", "k": 3 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "epenvgssid", "k": 3 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "grllkopehr", "k": 3 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "iaemfpyhrtgb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "iqjvqxzhjc", "k": 3 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "kpkzjgcvgopr", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "neocjmpaltv", "k": 2 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "uvdbxsjyso", "k": 3 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "wsezruidpcy", "k": 2 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "ybexlzsvsi", "k": 3 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "abacabccaa", "k": 4 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "abccbacbcbc", "k": 3 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "abccccbaaba", "k": 3 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "acabbbacacbb", "k": 2 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "acbbbbccacc", "k": 3 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "acbcbbaaca", "k": 4 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "accbabbbaacaa", "k": 1 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "baaaccbaaa", "k": 4 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "baababcacc", "k": 4 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "baabbccbbc", "k": 4 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "bacbbaaccb", "k": 4 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "baccbbccab", "k": 4 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "bbababccabca", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "bbacbccbca", "k": 4 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "bbacccbbaabbb", "k": 1 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "bbccbbbcaab", "k": 3 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "bbccbcccaba", "k": 3 } assert my_solution.minimumChanges(**test_input) == 0 test_input = { "s": "bcaacaabaa", "k": 4 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "bcbcbabaabaa", "k": 2 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "bofqvqapnjo", "k": 3 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "bvatyzbdffqdp", "k": 1 } assert my_solution.minimumChanges(**test_input) == 6 test_input = { "s": "cabbcbcbcbcca", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "cbacbbcbccccc", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "ccaccaacbcaac", "k": 1 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "ccbabcbabb", "k": 4 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "ccbacacbcbac", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "edulrtnsbb", "k": 4 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "feecuhvurk", "k": 4 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "ffqbqdocclh", "k": 3 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "gceeouniipz", "k": 3 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "gdlitshyeehtx", "k": 1 } assert my_solution.minimumChanges(**test_input) == 6 test_input = { "s": "hpbijyuygkk", "k": 3 } assert my_solution.minimumChanges(**test_input) == 3 test_input = { "s": "kxvwhuewyftpp", "k": 1 } assert my_solution.minimumChanges(**test_input) == 6 test_input = { "s": "mrqvwotsqjtfv", "k": 1 } assert my_solution.minimumChanges(**test_input) == 6 test_input = { "s": "qhzievvxauf", "k": 3 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "rbiuxrgidyzuu", "k": 1 } assert my_solution.minimumChanges(**test_input) == 6 test_input = { "s": "rkyidomzyud", "k": 3 } assert my_solution.minimumChanges(**test_input) == 4 test_input = { "s": "wvewmaevkzjp", "k": 2 } assert my_solution.minimumChanges(**test_input) == 3
1,697,941,800
weekly-contest-367-find-indices-with-index-and-value-difference-i
https://leetcode.com/problems/find-indices-with-index-and-value-difference-i
find-indices-with-index-and-value-difference-i
{ "questionId": "3165", "questionFrontendId": "2903", "title": "Find Indices With Index and Value Difference I", "titleSlug": "find-indices-with-index-and-value-difference-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 97, "dislikes": 7, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j 可能 相等 。 示例 1: 输入:nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 输出:[0,3] 解释:在示例中,可以选择 i = 0 和 j = 3 。 abs(0 - 3) >= 2 且 abs(nums[0] - nums[3]) >= 4 。 因此,[0,3] 是一个符合题目要求的答案。 [3,0] 也是符合题目要求的答案。 示例 2: 输入:nums = [2,1], indexDifference = 0, valueDifference = 0 输出:[0,0] 解释: 在示例中,可以选择 i = 0 和 j = 0 。 abs(0 - 0) >= 0 且 abs(nums[0] - nums[0]) >= 0 。 因此,[0,0] 是一个符合题目要求的答案。 [0,1]、[1,0] 和 [1,1] 也是符合题目要求的答案。 示例 3: 输入:nums = [1,2,3], indexDifference = 2, valueDifference = 4 输出:[-1,-1] 解释:在示例中,可以证明无法找出 2 个满足所有条件的下标。 因此,返回 [-1,-1] 。 提示: * 1 <= n == nums.length <= 100 * 0 <= nums[i] <= 50 * 0 <= indexDifference <= 100 * 0 <= valueDifference <= 50 """ class Solution: def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j 可能 相等 。 示例 1: 输入:nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 输出:[0,3] 解释:在示例中,可以选择 i = 0 和 j = 3 。 abs(0 - 3) >= 2 且 abs(nums[0] - nums[3]) >= 4 。 因此,[0,3] 是一个符合题目要求的答案。 [3,0] 也是符合题目要求的答案。 示例 2: 输入:nums = [2,1], indexDifference = 0, valueDifference = 0 输出:[0,0] 解释: 在示例中,可以选择 i = 0 和 j = 0 。 abs(0 - 0) >= 0 且 abs(nums[0] - nums[0]) >= 0 。 因此,[0,0] 是一个符合题目要求的答案。 [0,1]、[1,0] 和 [1,1] 也是符合题目要求的答案。 示例 3: 输入:nums = [1,2,3], indexDifference = 2, valueDifference = 4 输出:[-1,-1] 解释:在示例中,可以证明无法找出 2 个满足所有条件的下标。 因此,返回 [-1,-1] 。 提示: * 1 <= n == nums.length <= 100 * 0 <= nums[i] <= 50 * 0 <= indexDifference <= 100 * 0 <= valueDifference <= 50 请完成下面的代码来解决上述问题: ```python class Solution: def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]: ```
my_solution = Solution() test_input = { "nums": [5,1,4,1], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [2,1], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [1,2,3], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [0], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [3], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [3], "indexDifference": 1, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [4], "indexDifference": 1, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [5], "indexDifference": 1, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [7], "indexDifference": 1, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [8], "indexDifference": 0, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [8], "indexDifference": 1, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [10], "indexDifference": 0, "valueDifference": 9 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [11], "indexDifference": 1, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [18], "indexDifference": 1, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [38], "indexDifference": 1, "valueDifference": 34 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [40], "indexDifference": 1, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [5,10], "indexDifference": 1, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [5,48], "indexDifference": 0, "valueDifference": 29 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [6,3], "indexDifference": 1, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [7,6], "indexDifference": 1, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [8,8], "indexDifference": 1, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [17,31], "indexDifference": 1, "valueDifference": 9 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [21,22], "indexDifference": 1, "valueDifference": 21 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [48,40], "indexDifference": 2, "valueDifference": 31 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [2,8,0], "indexDifference": 2, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [2,29,0], "indexDifference": 0, "valueDifference": 12 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [3,0,7], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [4,22,43], "indexDifference": 0, "valueDifference": 34 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [5,0,3], "indexDifference": 1, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [5,9,2], "indexDifference": 0, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [6,2,7], "indexDifference": 2, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [6,5,1], "indexDifference": 2, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [6,8,0], "indexDifference": 1, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [7,36,21], "indexDifference": 1, "valueDifference": 20 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [9,4,7], "indexDifference": 0, "valueDifference": 9 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [9,50,31], "indexDifference": 1, "valueDifference": 8 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [31,23,36], "indexDifference": 1, "valueDifference": 11 } assert my_solution.findIndices(**test_input) == [1,2] test_input = { "nums": [40,21,1], "indexDifference": 2, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [0,5,10,5], "indexDifference": 3, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [1,28,24,35], "indexDifference": 3, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [2,7,10,4], "indexDifference": 0, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [3,1,0,3], "indexDifference": 2, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [7,5,6,2], "indexDifference": 2, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [9,3,6,4], "indexDifference": 1, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [34,46,11,45], "indexDifference": 1, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [36,37,40,9], "indexDifference": 2, "valueDifference": 8 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [37,25,48,13], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [45,6,29,21], "indexDifference": 3, "valueDifference": 36 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [1,5,7,9,2], "indexDifference": 3, "valueDifference": 8 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [7,2,1,8,3], "indexDifference": 0, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [2,3] test_input = { "nums": [8,9,8,0,4], "indexDifference": 1, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [9,1,10,0,10], "indexDifference": 0, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [9,9,4,5,5], "indexDifference": 2, "valueDifference": 9 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [10,1,10,12,1], "indexDifference": 1, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [17,46,31,28,28], "indexDifference": 0, "valueDifference": 46 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [17,49,1,47,12], "indexDifference": 2, "valueDifference": 17 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [32,49,3,40,44], "indexDifference": 1, "valueDifference": 37 } assert my_solution.findIndices(**test_input) == [1,2] test_input = { "nums": [46,43,16,16,34], "indexDifference": 3, "valueDifference": 13 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [49,36,18,4,33], "indexDifference": 3, "valueDifference": 20 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [0,7,10,6,6,5], "indexDifference": 1, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [2,0,3,4,0,5], "indexDifference": 3, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [1,5] test_input = { "nums": [3,8,9,7,2,3], "indexDifference": 3, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [1,4] test_input = { "nums": [3,27,38,47,38,4], "indexDifference": 0, "valueDifference": 10 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [4,13,48,50,1,26], "indexDifference": 4, "valueDifference": 34 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [6,1,2,6,4,6], "indexDifference": 2, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [7,1,0,9,5,9], "indexDifference": 2, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [7,3,7,5,7,9], "indexDifference": 1, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [12,37,7,16,5,34], "indexDifference": 3, "valueDifference": 27 } assert my_solution.findIndices(**test_input) == [1,4] test_input = { "nums": [17,46,48,25,22,4], "indexDifference": 2, "valueDifference": 30 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [18,18,7,10,9,50], "indexDifference": 2, "valueDifference": 32 } assert my_solution.findIndices(**test_input) == [0,5] test_input = { "nums": [18,42,37,13,49,42], "indexDifference": 3, "valueDifference": 46 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [23,31,14,42,0,49], "indexDifference": 4, "valueDifference": 44 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [50,46,15,16,48,7], "indexDifference": 1, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [5,6,8,5,6,3,1], "indexDifference": 0, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,6] test_input = { "nums": [5,50,13,3,44,7,29], "indexDifference": 1, "valueDifference": 45 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [8,7,18,47,27,25,41], "indexDifference": 0, "valueDifference": 45 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [9,1,0,6,7,5,8], "indexDifference": 1, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [11,3,36,17,13,0,26], "indexDifference": 2, "valueDifference": 33 } assert my_solution.findIndices(**test_input) == [2,5] test_input = { "nums": [13,0,16,32,47,27,25], "indexDifference": 1, "valueDifference": 35 } assert my_solution.findIndices(**test_input) == [1,4] test_input = { "nums": [13,16,30,33,50,50,38], "indexDifference": 3, "valueDifference": 30 } assert my_solution.findIndices(**test_input) == [0,4] test_input = { "nums": [21,44,22,1,21,9,17], "indexDifference": 1, "valueDifference": 41 } assert my_solution.findIndices(**test_input) == [1,3] test_input = { "nums": [35,31,36,28,49,4,46], "indexDifference": 4, "valueDifference": 27 } assert my_solution.findIndices(**test_input) == [0,5] test_input = { "nums": [39,18,49,25,40,41,26], "indexDifference": 1, "valueDifference": 43 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [40,46,11,36,25,46,47], "indexDifference": 0, "valueDifference": 37 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [46,7,6,3,43,7,48], "indexDifference": 0, "valueDifference": 48 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [0,1,6,8,8,3,9,10], "indexDifference": 2, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [1,0,6,4,8,7,2,5], "indexDifference": 3, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,4] test_input = { "nums": [3,8,3,8,0,5,5,7], "indexDifference": 4, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [5,10,4,4,8,6,0,4], "indexDifference": 2, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [1,6] test_input = { "nums": [6,1,3,7,4,4,2,1], "indexDifference": 0, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [9,36,31,2,46,1,27,37], "indexDifference": 3, "valueDifference": 45 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [10,4,0,1,4,7,2,0], "indexDifference": 5, "valueDifference": 8 } assert my_solution.findIndices(**test_input) == [0,6] test_input = { "nums": [26,20,19,36,20,28,33,39], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [29,3,30,34,25,40,10,37], "indexDifference": 5, "valueDifference": 22 } assert my_solution.findIndices(**test_input) == [1,7] test_input = { "nums": [39,26,46,9,5,34,0,20], "indexDifference": 0, "valueDifference": 24 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [46,12,38,21,12,9,18,29], "indexDifference": 3, "valueDifference": 35 } assert my_solution.findIndices(**test_input) == [0,5] test_input = { "nums": [1,7,7,2,4,10,1,5,9], "indexDifference": 4, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,5] test_input = { "nums": [2,4,1,5,2,0,3,5,7], "indexDifference": 5, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,5] test_input = { "nums": [2,5,6,5,9,7,2,3,6], "indexDifference": 4, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,4] test_input = { "nums": [2,6,9,4,9,4,10,9,2], "indexDifference": 0, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,1]
1,697,337,000
weekly-contest-367-shortest-and-lexicographically-smallest-beautiful-string
https://leetcode.com/problems/shortest-and-lexicographically-smallest-beautiful-string
shortest-and-lexicographically-smallest-beautiful-string
{ "questionId": "3150", "questionFrontendId": "2904", "title": "Shortest and Lexicographically Smallest Beautiful String", "titleSlug": "shortest-and-lexicographically-smallest-beautiful-string", "isPaidOnly": false, "difficulty": "Medium", "likes": 147, "dislikes": 8, "categoryTitle": "Algorithms" }
""" 给你一个二进制字符串 s 和一个正整数 k 。 如果 s 的某个子字符串中 1 的个数恰好等于 k ,则称这个子字符串是一个 美丽子字符串 。 令 len 等于 最短 美丽子字符串的长度。 返回长度等于 len 且字典序 最小 的美丽子字符串。如果 s 中不含美丽子字符串,则返回一个 空 字符串。 对于相同长度的两个字符串 a 和 b ,如果在 a 和 b 出现不同的第一个位置上,a 中该位置上的字符严格大于 b 中的对应字符,则认为字符串 a 字典序 大于 字符串 b 。 * 例如,"abcd" 的字典序大于 "abcc" ,因为两个字符串出现不同的第一个位置对应第四个字符,而 d 大于 c 。 示例 1: 输入:s = "100011001", k = 3 输出:"11001" 解释:示例中共有 7 个美丽子字符串: 1. 子字符串 "100011001" 。 2. 子字符串 "100011001" 。 3. 子字符串 "100011001" 。 4. 子字符串 "100011001" 。 5. 子字符串 "100011001" 。 6. 子字符串 "100011001" 。 7. 子字符串 "100011001" 。 最短美丽子字符串的长度是 5 。 长度为 5 且字典序最小的美丽子字符串是子字符串 "11001" 。 示例 2: 输入:s = "1011", k = 2 输出:"11" 解释:示例中共有 3 个美丽子字符串: 1. 子字符串 "1011" 。 2. 子字符串 "1011" 。 3. 子字符串 "1011" 。 最短美丽子字符串的长度是 2 。 长度为 2 且字典序最小的美丽子字符串是子字符串 "11" 。 示例 3: 输入:s = "000", k = 1 输出:"" 解释:示例中不存在美丽子字符串。 提示: * 1 <= s.length <= 100 * 1 <= k <= s.length """ class Solution: def shortestBeautifulSubstring(self, s: str, k: int) -> str:
给你一个二进制字符串 s 和一个正整数 k 。 如果 s 的某个子字符串中 1 的个数恰好等于 k ,则称这个子字符串是一个 美丽子字符串 。 令 len 等于 最短 美丽子字符串的长度。 返回长度等于 len 且字典序 最小 的美丽子字符串。如果 s 中不含美丽子字符串,则返回一个 空 字符串。 对于相同长度的两个字符串 a 和 b ,如果在 a 和 b 出现不同的第一个位置上,a 中该位置上的字符严格大于 b 中的对应字符,则认为字符串 a 字典序 大于 字符串 b 。 * 例如,"abcd" 的字典序大于 "abcc" ,因为两个字符串出现不同的第一个位置对应第四个字符,而 d 大于 c 。 示例 1: 输入:s = "100011001", k = 3 输出:"11001" 解释:示例中共有 7 个美丽子字符串: 1. 子字符串 "100011001" 。 2. 子字符串 "100011001" 。 3. 子字符串 "100011001" 。 4. 子字符串 "100011001" 。 5. 子字符串 "100011001" 。 6. 子字符串 "100011001" 。 7. 子字符串 "100011001" 。 最短美丽子字符串的长度是 5 。 长度为 5 且字典序最小的美丽子字符串是子字符串 "11001" 。 示例 2: 输入:s = "1011", k = 2 输出:"11" 解释:示例中共有 3 个美丽子字符串: 1. 子字符串 "1011" 。 2. 子字符串 "1011" 。 3. 子字符串 "1011" 。 最短美丽子字符串的长度是 2 。 长度为 2 且字典序最小的美丽子字符串是子字符串 "11" 。 示例 3: 输入:s = "000", k = 1 输出:"" 解释:示例中不存在美丽子字符串。 提示: * 1 <= s.length <= 100 * 1 <= k <= s.length 请完成下面的代码来解决上述问题: ```python class Solution: def shortestBeautifulSubstring(self, s: str, k: int) -> str: ```
my_solution = Solution() test_input = { "s": "100011001", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11001" test_input = { "s": "1011", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "000", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "11000111", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "10100010", "k": 5 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "0010111", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "111111110010001010", "k": 11 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11111111001000101" test_input = { "s": "00000110010011010110", "k": 13 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1100001110111100100", "k": 8 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11101111001" test_input = { "s": "01011101000111110", "k": 5 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11111" test_input = { "s": "10001", "k": 5 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "001011010001101", "k": 11 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1001000", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "001", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "000110001", "k": 6 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "10001", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "100011000000000", "k": 9 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "10101000111", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "111" test_input = { "s": "00", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1100100101011001001", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1100100101011" test_input = { "s": "0001001", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "101100000", "k": 6 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "100011", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "1101", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "110000", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "001110101101101111", "k": 10 } assert my_solution.shortestBeautifulSubstring(**test_input) == "10101101101111" test_input = { "s": "111011110", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "1111111011111", "k": 12 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1111111011111" test_input = { "s": "0101111000101011001", "k": 9 } assert my_solution.shortestBeautifulSubstring(**test_input) == "101111000101011" test_input = { "s": "0011010", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "01101101110110010", "k": 16 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "10111101011100101001", "k": 14 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "0", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "0101001000001011011", "k": 12 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "01000100", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "01001010010", "k": 6 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "01100110011101010", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "111" test_input = { "s": "10", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "01010000110111110010", "k": 12 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1101", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "00000100000", "k": 9 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "10101101000111", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "111" test_input = { "s": "101010111111110110", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1111" test_input = { "s": "0100111011101010", "k": 16 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1111001111", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "000010110111011", "k": 8 } assert my_solution.shortestBeautifulSubstring(**test_input) == "10110111011" test_input = { "s": "010100", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "00", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "000100", "k": 5 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1110001110100110011", "k": 13 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "111011001", "k": 9 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "0111000100001101", "k": 9 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "100011", "k": 5 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "111000101010010011", "k": 10 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "01010", "k": 5 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "0100100111100010", "k": 14 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "110", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "111000", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "101011", "k": 6 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "11101101110110111101", "k": 17 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "010011101", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11101" test_input = { "s": "100111010111001", "k": 11 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "110001111100011", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "0010110", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "11101110001111", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1110001111" test_input = { "s": "110", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "110101", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1101" test_input = { "s": "0110100", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "00111", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "111" test_input = { "s": "1011001100001110101", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11101" test_input = { "s": "0101000010101010", "k": 6 } assert my_solution.shortestBeautifulSubstring(**test_input) == "10100001010101" test_input = { "s": "0011000110001", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "01001011000110100", "k": 11 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "11", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "00111100", "k": 5 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1110", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "111" test_input = { "s": "00010000110", "k": 9 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "110110100011", "k": 8 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "00111110001100000", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1111100011" test_input = { "s": "10100100101", "k": 11 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "110", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "1001101100000010100", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "0101010101010111101", "k": 15 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "0000101", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "110011010110", "k": 10 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "11010", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "10010100", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "1", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "11", "k": 1 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1" test_input = { "s": "101000", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "101" test_input = { "s": "000010110010111", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "10110010111" test_input = { "s": "0100111", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "100111" test_input = { "s": "0111111100011111101", "k": 12 } assert my_solution.shortestBeautifulSubstring(**test_input) == "111111000111111" test_input = { "s": "010001", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "110110101010100000", "k": 10 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "110000001", "k": 4 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "001101", "k": 6 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "01111001101001010101", "k": 16 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "00111010", "k": 6 } assert my_solution.shortestBeautifulSubstring(**test_input) == "" test_input = { "s": "11011101010100", "k": 7 } assert my_solution.shortestBeautifulSubstring(**test_input) == "1101110101"
1,697,337,000
weekly-contest-367-find-indices-with-index-and-value-difference-ii
https://leetcode.com/problems/find-indices-with-index-and-value-difference-ii
find-indices-with-index-and-value-difference-ii
{ "questionId": "3170", "questionFrontendId": "2905", "title": "Find Indices With Index and Value Difference II", "titleSlug": "find-indices-with-index-and-value-difference-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 218, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j 可能 相等 。 示例 1: 输入:nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 输出:[0,3] 解释:在示例中,可以选择 i = 0 和 j = 3 。 abs(0 - 3) >= 2 且 abs(nums[0] - nums[3]) >= 4 。 因此,[0,3] 是一个符合题目要求的答案。 [3,0] 也是符合题目要求的答案。 示例 2: 输入:nums = [2,1], indexDifference = 0, valueDifference = 0 输出:[0,0] 解释: 在示例中,可以选择 i = 0 和 j = 0 。 abs(0 - 0) >= 0 且 abs(nums[0] - nums[0]) >= 0 。 因此,[0,0] 是一个符合题目要求的答案。 [0,1]、[1,0] 和 [1,1] 也是符合题目要求的答案。 示例 3: 输入:nums = [1,2,3], indexDifference = 2, valueDifference = 4 输出:[-1,-1] 解释:在示例中,可以证明无法找出 2 个满足所有条件的下标。 因此,返回 [-1,-1] 。 提示: * 1 <= n == nums.length <= 105 * 0 <= nums[i] <= 109 * 0 <= indexDifference <= 105 * 0 <= valueDifference <= 109 """ class Solution: def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j 可能 相等 。 示例 1: 输入:nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 输出:[0,3] 解释:在示例中,可以选择 i = 0 和 j = 3 。 abs(0 - 3) >= 2 且 abs(nums[0] - nums[3]) >= 4 。 因此,[0,3] 是一个符合题目要求的答案。 [3,0] 也是符合题目要求的答案。 示例 2: 输入:nums = [2,1], indexDifference = 0, valueDifference = 0 输出:[0,0] 解释: 在示例中,可以选择 i = 0 和 j = 0 。 abs(0 - 0) >= 0 且 abs(nums[0] - nums[0]) >= 0 。 因此,[0,0] 是一个符合题目要求的答案。 [0,1]、[1,0] 和 [1,1] 也是符合题目要求的答案。 示例 3: 输入:nums = [1,2,3], indexDifference = 2, valueDifference = 4 输出:[-1,-1] 解释:在示例中,可以证明无法找出 2 个满足所有条件的下标。 因此,返回 [-1,-1] 。 提示: * 1 <= n == nums.length <= 105 * 0 <= nums[i] <= 109 * 0 <= indexDifference <= 105 * 0 <= valueDifference <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]: ```
my_solution = Solution() test_input = { "nums": [5,1,4,1], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [2,1], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [1,2,3], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [1], "indexDifference": 0, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [1], "indexDifference": 1, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [2], "indexDifference": 0, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [6], "indexDifference": 1, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [7], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [8], "indexDifference": 1, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [9], "indexDifference": 0, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [9], "indexDifference": 1, "valueDifference": 9 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [12], "indexDifference": 0, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [16], "indexDifference": 0, "valueDifference": 16 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [16], "indexDifference": 1, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [46], "indexDifference": 0, "valueDifference": 36 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [0,10], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [2,7], "indexDifference": 2, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [5,1], "indexDifference": 2, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [5,12], "indexDifference": 0, "valueDifference": 10 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [8,0], "indexDifference": 1, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [8,4], "indexDifference": 1, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [9,8], "indexDifference": 0, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [15,8], "indexDifference": 0, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [20,21], "indexDifference": 1, "valueDifference": 14 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [23,47], "indexDifference": 2, "valueDifference": 47 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [32,14], "indexDifference": 1, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [36,14], "indexDifference": 2, "valueDifference": 11 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [40,12], "indexDifference": 1, "valueDifference": 20 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [45,41], "indexDifference": 2, "valueDifference": 21 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [50,24], "indexDifference": 1, "valueDifference": 18 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [2,7,0], "indexDifference": 0, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [3,9,3], "indexDifference": 0, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [3,12,40], "indexDifference": 0, "valueDifference": 9 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [4,10,8], "indexDifference": 0, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [5,27,26], "indexDifference": 1, "valueDifference": 15 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [7,8,10], "indexDifference": 2, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [8,2,6], "indexDifference": 1, "valueDifference": 8 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [8,7,5], "indexDifference": 2, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [9,5,4], "indexDifference": 1, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [9,9,8], "indexDifference": 1, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [10,4,10], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [10,5,2], "indexDifference": 2, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [37,45,23], "indexDifference": 1, "valueDifference": 28 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [37,49,25], "indexDifference": 1, "valueDifference": 29 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [45,32,30], "indexDifference": 2, "valueDifference": 45 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [0,2,5,8], "indexDifference": 1, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [0,4,7,2], "indexDifference": 2, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [1,3,4,4], "indexDifference": 2, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [1,8,6,10], "indexDifference": 2, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [2,0,9,2], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [7,4,8,3], "indexDifference": 3, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [9,4,0,9], "indexDifference": 2, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [9,9,7,7], "indexDifference": 3, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [10,33,39,15], "indexDifference": 1, "valueDifference": 12 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [22,28,50,25], "indexDifference": 0, "valueDifference": 28 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [36,35,45,34], "indexDifference": 1, "valueDifference": 31 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [50,21,32,5], "indexDifference": 0, "valueDifference": 26 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [0,6,2,10,4], "indexDifference": 3, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [0,8,2,5,10], "indexDifference": 0, "valueDifference": 10 } assert my_solution.findIndices(**test_input) == [0,4] test_input = { "nums": [0,9,0,5,9], "indexDifference": 3, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [0,9,4,9,9], "indexDifference": 0, "valueDifference": 8 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [3,7,7,10,10], "indexDifference": 2, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [4,0,1,5,0], "indexDifference": 0, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [4,1,7,0,3], "indexDifference": 1, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [6,0,6,7,9], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [6,19,40,41,36], "indexDifference": 0, "valueDifference": 34 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [7,10,4,5,1], "indexDifference": 2, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [1,4] test_input = { "nums": [9,1,7,8,5], "indexDifference": 2, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [10,39,25,12,7], "indexDifference": 1, "valueDifference": 35 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [15,28,50,37,43], "indexDifference": 2, "valueDifference": 46 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [0,6,6,0,0,6], "indexDifference": 2, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [1,7,4,4,5,3], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [3,3,1,6,3,3], "indexDifference": 3, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [3,3,4,1,3,7], "indexDifference": 3, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [4,3,8,6,2,8], "indexDifference": 2, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [6,1,2,9,6,5], "indexDifference": 1, "valueDifference": 1 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [6,5,5,10,4,3], "indexDifference": 1, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [1,3] test_input = { "nums": [6,7,0,2,2,6], "indexDifference": 1, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [7,6,3,5,9,5], "indexDifference": 2, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [2,4] test_input = { "nums": [9,1,0,4,3,3], "indexDifference": 4, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,4] test_input = { "nums": [9,8,8,0,1,4], "indexDifference": 3, "valueDifference": 9 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [25,49,11,15,32,33], "indexDifference": 2, "valueDifference": 37 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [0,44,42,26,15,37,23], "indexDifference": 0, "valueDifference": 17 } assert my_solution.findIndices(**test_input) == [0,1] test_input = { "nums": [3,5,3,44,8,47,10], "indexDifference": 3, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [3,8,8,0,2,0,10], "indexDifference": 4, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,5] test_input = { "nums": [5,1,0,7,5,2,0], "indexDifference": 4, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,5] test_input = { "nums": [9,10,1,3,1,5,0], "indexDifference": 2, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [13,3,15,48,32,19,33], "indexDifference": 1, "valueDifference": 25 } assert my_solution.findIndices(**test_input) == [1,3] test_input = { "nums": [29,12,42,16,50,22,10], "indexDifference": 2, "valueDifference": 29 } assert my_solution.findIndices(**test_input) == [1,4] test_input = { "nums": [30,48,12,15,43,17,13], "indexDifference": 3, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [31,33,46,4,13,45,37], "indexDifference": 0, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,2] test_input = { "nums": [43,37,25,44,45,21,47], "indexDifference": 1, "valueDifference": 37 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [0,4,0,4,1,2,1,9], "indexDifference": 1, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [0,7] test_input = { "nums": [0,7,3,2,6,1,7,4], "indexDifference": 4, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [0,4] test_input = { "nums": [2,10,2,0,6,0,7,7], "indexDifference": 2, "valueDifference": 3 } assert my_solution.findIndices(**test_input) == [1,3] test_input = { "nums": [3,9,6,10,10,3,2,9], "indexDifference": 5, "valueDifference": 10 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [4,4,3,0,6,1,7,8], "indexDifference": 3, "valueDifference": 6 } assert my_solution.findIndices(**test_input) == [3,6] test_input = { "nums": [4,6,3,4,2,4,5,2], "indexDifference": 2, "valueDifference": 5 } assert my_solution.findIndices(**test_input) == [-1,-1] test_input = { "nums": [6,5,6,3,0,10,9,8], "indexDifference": 2, "valueDifference": 2 } assert my_solution.findIndices(**test_input) == [1,3] test_input = { "nums": [7,4,3,1,6,6,2,6], "indexDifference": 3, "valueDifference": 7 } assert my_solution.findIndices(**test_input) == [-1,-1]
1,697,337,000
weekly-contest-367-construct-product-matrix
https://leetcode.com/problems/construct-product-matrix
construct-product-matrix
{ "questionId": "3031", "questionFrontendId": "2906", "title": "Construct Product Matrix", "titleSlug": "construct-product-matrix", "isPaidOnly": false, "difficulty": "Medium", "likes": 187, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、大小为 n * m 的二维整数矩阵 grid ,定义一个下标从 0 开始、大小为 n * m 的的二维矩阵 p。如果满足以下条件,则称 p 为 grid 的 乘积矩阵 : * 对于每个元素 p[i][j] ,它的值等于除了 grid[i][j] 外所有元素的乘积。乘积对 12345 取余数。 返回 grid 的乘积矩阵。 示例 1: 输入:grid = [[1,2],[3,4]] 输出:[[24,12],[8,6]] 解释:p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24 p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12 p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8 p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6 所以答案是 [[24,12],[8,6]] 。 示例 2: 输入:grid = [[12345],[2],[1]] 输出:[[2],[0],[0]] 解释:p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2 p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0 ,所以 p[0][1] = 0 p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0 ,所以 p[0][2] = 0 所以答案是 [[2],[0],[0]] 。 提示: * 1 <= n == grid.length <= 105 * 1 <= m == grid[i].length <= 105 * 2 <= n * m <= 105 * 1 <= grid[i][j] <= 109 """ class Solution: def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
给你一个下标从 0 开始、大小为 n * m 的二维整数矩阵 grid ,定义一个下标从 0 开始、大小为 n * m 的的二维矩阵 p。如果满足以下条件,则称 p 为 grid 的 乘积矩阵 : * 对于每个元素 p[i][j] ,它的值等于除了 grid[i][j] 外所有元素的乘积。乘积对 12345 取余数。 返回 grid 的乘积矩阵。 示例 1: 输入:grid = [[1,2],[3,4]] 输出:[[24,12],[8,6]] 解释:p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24 p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12 p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8 p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6 所以答案是 [[24,12],[8,6]] 。 示例 2: 输入:grid = [[12345],[2],[1]] 输出:[[2],[0],[0]] 解释:p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2 p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0 ,所以 p[0][1] = 0 p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0 ,所以 p[0][2] = 0 所以答案是 [[2],[0],[0]] 。 提示: * 1 <= n == grid.length <= 105 * 1 <= m == grid[i].length <= 105 * 2 <= n * m <= 105 * 1 <= grid[i][j] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]: ```
my_solution = Solution() test_input = { "grid": [[1,2],[3,4]] } assert my_solution.constructProductMatrix(**test_input) == [[24,12],[8,6]] test_input = { "grid": [[12345],[2],[1]] } assert my_solution.constructProductMatrix(**test_input) == [[2],[0],[0]] test_input = { "grid": [[1],[2]] } assert my_solution.constructProductMatrix(**test_input) == [[2],[1]] test_input = { "grid": [[1,2]] } assert my_solution.constructProductMatrix(**test_input) == [[2,1]] test_input = { "grid": [[12345,12345]] } assert my_solution.constructProductMatrix(**test_input) == [[0,0]] test_input = { "grid": [[1],[4]] } assert my_solution.constructProductMatrix(**test_input) == [[4],[1]] test_input = { "grid": [[3],[4]] } assert my_solution.constructProductMatrix(**test_input) == [[4],[3]] test_input = { "grid": [[4],[3]] } assert my_solution.constructProductMatrix(**test_input) == [[3],[4]] test_input = { "grid": [[1,1,1]] } assert my_solution.constructProductMatrix(**test_input) == [[1,1,1]] test_input = { "grid": [[2,1,1]] } assert my_solution.constructProductMatrix(**test_input) == [[1,2,2]] test_input = { "grid": [[3],[5],[2]] } assert my_solution.constructProductMatrix(**test_input) == [[10],[6],[15]] test_input = { "grid": [[1,2],[1,1],[6,4]] } assert my_solution.constructProductMatrix(**test_input) == [[48,24],[48,48],[8,12]] test_input = { "grid": [[1,2,2],[1,4,3]] } assert my_solution.constructProductMatrix(**test_input) == [[48,24,24],[48,12,16]] test_input = { "grid": [[2],[7],[2],[6]] } assert my_solution.constructProductMatrix(**test_input) == [[84],[24],[84],[28]] test_input = { "grid": [[3],[4],[7],[7]] } assert my_solution.constructProductMatrix(**test_input) == [[196],[147],[84],[84]] test_input = { "grid": [[3,1,1],[1,3,4]] } assert my_solution.constructProductMatrix(**test_input) == [[12,36,36],[36,12,9]] test_input = { "grid": [[4],[8],[3],[7]] } assert my_solution.constructProductMatrix(**test_input) == [[168],[84],[224],[96]] test_input = { "grid": [[5],[8],[8],[3]] } assert my_solution.constructProductMatrix(**test_input) == [[192],[120],[120],[320]] test_input = { "grid": [[6],[5],[8],[5]] } assert my_solution.constructProductMatrix(**test_input) == [[200],[240],[150],[240]] test_input = { "grid": [[8],[1],[3],[8]] } assert my_solution.constructProductMatrix(**test_input) == [[24],[192],[64],[24]] test_input = { "grid": [[1],[10],[3],[10],[9]] } assert my_solution.constructProductMatrix(**test_input) == [[2700],[270],[900],[270],[300]] test_input = { "grid": [[1,1,1,1,1]] } assert my_solution.constructProductMatrix(**test_input) == [[1,1,1,1,1]] test_input = { "grid": [[1,1,2,2,1]] } assert my_solution.constructProductMatrix(**test_input) == [[4,4,2,2,4]] test_input = { "grid": [[1,2,3],[3,3,5],[3,4,2]] } assert my_solution.constructProductMatrix(**test_input) == [[6480,3240,2160],[2160,2160,1296],[2160,1620,3240]] test_input = { "grid": [[2],[7],[5],[3],[4]] } assert my_solution.constructProductMatrix(**test_input) == [[420],[120],[168],[280],[210]] test_input = { "grid": [[2,2,2,2,1]] } assert my_solution.constructProductMatrix(**test_input) == [[8,8,8,8,16]] test_input = { "grid": [[2,2,4,4],[3,2,1,4]] } assert my_solution.constructProductMatrix(**test_input) == [[768,768,384,384],[512,768,1536,384]] test_input = { "grid": [[2,4,1,1],[3,4,4,1]] } assert my_solution.constructProductMatrix(**test_input) == [[192,96,384,384],[128,96,96,384]] test_input = { "grid": [[3,1,1,4],[1,4,1,1]] } assert my_solution.constructProductMatrix(**test_input) == [[16,48,48,12],[48,12,48,48]] test_input = { "grid": [[3,2,5],[6,4,3],[6,3,1]] } assert my_solution.constructProductMatrix(**test_input) == [[615,7095,7776],[6480,9720,615],[6480,615,1845]] test_input = { "grid": [[5,5,5],[4,3,1],[4,5,1]] } assert my_solution.constructProductMatrix(**test_input) == [[6000,6000,6000],[7500,10000,5310],[7500,6000,5310]] test_input = { "grid": [[6,3],[1,5],[2,7],[6,5]] } assert my_solution.constructProductMatrix(**test_input) == [[6300,255],[765,7560],[6555,5400],[6300,7560]] test_input = { "grid": [[6,3,2],[2,3,1],[5,5,4]] } assert my_solution.constructProductMatrix(**test_input) == [[3600,7200,10800],[10800,7200,9255],[4320,4320,5400]] test_input = { "grid": [[6,5,3],[4,4,5],[3,2,5]] } assert my_solution.constructProductMatrix(**test_input) == [[11310,6165,10275],[4620,4620,6165],[10275,9240,6165]] test_input = { "grid": [[8],[5],[5],[9],[8]] } assert my_solution.constructProductMatrix(**test_input) == [[1800],[2880],[2880],[1600],[1800]] test_input = { "grid": [[10],[5],[6],[8],[6]] } assert my_solution.constructProductMatrix(**test_input) == [[1440],[2880],[2400],[1800],[2400]] test_input = { "grid": [[10],[9],[3],[4],[3]] } assert my_solution.constructProductMatrix(**test_input) == [[324],[360],[1080],[810],[1080]] test_input = { "grid": [[1,1,1,2,2,1]] } assert my_solution.constructProductMatrix(**test_input) == [[4,4,4,2,2,4]] test_input = { "grid": [[1,1,2,1,2,1]] } assert my_solution.constructProductMatrix(**test_input) == [[4,4,2,4,2,4]] test_input = { "grid": [[1,1,2,1,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[8,8,4,8,4,4]] test_input = { "grid": [[1,1,2,2,1,2]] } assert my_solution.constructProductMatrix(**test_input) == [[8,8,4,4,8,4]] test_input = { "grid": [[1,1,2,2,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[16,16,8,8,8,8]] test_input = { "grid": [[1,1,3,3],[3,4,4,2],[6,6,3,4]] } assert my_solution.constructProductMatrix(**test_input) == [[2898,2898,966,966],[966,6897,6897,1449],[483,483,966,6897]] test_input = { "grid": [[1,2,1,1,1,2]] } assert my_solution.constructProductMatrix(**test_input) == [[4,2,4,4,4,2]] test_input = { "grid": [[1,2,1,1,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[8,4,8,8,4,4]] test_input = { "grid": [[1,2,2,4,3],[3,4,1,4,2]] } assert my_solution.constructProductMatrix(**test_input) == [[4608,2304,2304,1152,1536],[1536,1152,4608,1152,2304]] test_input = { "grid": [[1,3,1,3,1],[2,1,3,2,3]] } assert my_solution.constructProductMatrix(**test_input) == [[324,108,324,108,324],[162,324,108,162,108]] test_input = { "grid": [[1,3,3,3,4],[2,2,1,4,4]] } assert my_solution.constructProductMatrix(**test_input) == [[6912,2304,2304,2304,1728],[3456,3456,6912,1728,1728]] test_input = { "grid": [[1,4,5],[1,3,2],[7,2,6],[6,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[7365,11100,11349],[7365,6570,9855],[9870,9855,3285],[3285,9855,9855]] test_input = { "grid": [[1,6,6,4],[4,1,2,5],[5,4,6,2]] } assert my_solution.constructProductMatrix(**test_input) == [[12105,8190,8190,12285],[12285,12105,12225,4890],[4890,12285,8190,12225]] test_input = { "grid": [[2,1,3,2,1],[3,1,3,1,2]] } assert my_solution.constructProductMatrix(**test_input) == [[108,216,72,108,216],[72,216,72,216,108]] test_input = { "grid": [[2,2,2,1,1,1]] } assert my_solution.constructProductMatrix(**test_input) == [[4,4,4,8,8,8]] test_input = { "grid": [[2,2,2,2,1,2]] } assert my_solution.constructProductMatrix(**test_input) == [[16,16,16,16,32,16]] test_input = { "grid": [[2,2,3,2,3],[3,1,3,2,4]] } assert my_solution.constructProductMatrix(**test_input) == [[2592,2592,1728,2592,1728],[1728,5184,1728,2592,1296]] test_input = { "grid": [[2,3,4],[2,4,2],[1,8,1],[8,8,8]] } assert my_solution.constructProductMatrix(**test_input) == [[8697,5798,10521],[8697,10521,8697],[5049,11433,5049],[11433,11433,11433]] test_input = { "grid": [[2,4,4,5],[5,5,1,4],[4,2,2,5]] } assert my_solution.constructProductMatrix(**test_input) == [[10405,11375,11375,9100],[9100,9100,8465,11375],[11375,10405,10405,9100]] test_input = { "grid": [[3,4,1,1],[3,1,4,4],[1,5,1,5]] } assert my_solution.constructProductMatrix(**test_input) == [[4800,3600,2055,2055],[4800,2055,3600,3600],[2055,2880,2055,2880]] test_input = { "grid": [[3,4,6,3],[4,5,4,4],[5,2,6,3]] } assert my_solution.constructProductMatrix(**test_input) == [[11625,11805,11985,11625],[11805,6975,11805,11805],[6975,11265,11985,11625]] test_input = { "grid": [[4,1,4,4,4],[2,2,2,3,3]] } assert my_solution.constructProductMatrix(**test_input) == [[4608,6087,4608,4608,4608],[9216,9216,9216,6144,6144]] test_input = { "grid": [[4,8,8],[6,2,5],[7,3,7],[6,3,5]] } assert my_solution.constructProductMatrix(**test_input) == [[3525,7935,7935],[6465,7050,2820],[7305,585,7305],[6465,585,2820]] test_input = { "grid": [[6],[8],[2],[12],[6],[4]] } assert my_solution.constructProductMatrix(**test_input) == [[4608],[3456],[1479],[2304],[4608],[6912]] test_input = { "grid": [[6,2,5,2],[5,5,2,3],[4,6,5,2]] } assert my_solution.constructProductMatrix(**test_input) == [[3990,11970,12195,11970],[12195,12195,11970,7980],[5985,3990,12195,11970]] test_input = { "grid": [[6,3,4,3],[6,4,2,5],[3,3,6,1]] } assert my_solution.constructProductMatrix(**test_input) == [[9795,7245,8520,7245],[9795,8520,4695,4347],[7245,7245,9795,9390]] test_input = { "grid": [[6,5,6,6],[3,2,6,4],[1,3,4,1]] } assert my_solution.constructProductMatrix(**test_input) == [[2415,2898,2415,2415],[4830,7245,2415,9795],[2145,4830,9795,2145]] test_input = { "grid": [[7],[11],[12],[7],[12],[7]] } assert my_solution.constructProductMatrix(**test_input) == [[3546],[12],[8241],[3546],[8241],[3546]] test_input = { "grid": [[7,4,5],[2,1,2],[5,3,8],[3,6,7]] } assert my_solution.constructProductMatrix(**test_input) == [[12135,5805,2175],[11610,10875,11610],[2175,7740,9075],[7740,3870,12135]] test_input = { "grid": [[8],[6],[7],[2],[7],[4]] } assert my_solution.constructProductMatrix(**test_input) == [[2352],[3136],[2688],[9408],[2688],[4704]] test_input = { "grid": [[8],[6],[7],[5],[3],[6]] } assert my_solution.constructProductMatrix(**test_input) == [[3780],[5040],[4320],[6048],[10080],[5040]] test_input = { "grid": [[8,1],[9,6],[2,4],[1,3],[3,6]] } assert my_solution.constructProductMatrix(**test_input) == [[10983,1449],[8391,6414],[6897,9621],[1449,483],[483,6414]] test_input = { "grid": [[8,4,3],[7,7,4],[1,2,3],[5,2,4]] } assert my_solution.constructProductMatrix(**test_input) == [[8955,5565,11535],[3180,3180,5565],[9915,11130,11535],[1983,11130,5565]] test_input = { "grid": [[12],[8],[4],[3],[9],[5]] } assert my_solution.constructProductMatrix(**test_input) == [[4320],[6480],[615],[4935],[5760],[10368]] test_input = { "grid": [[1,1,1,1,2,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[8,8,8,8,4,4,4]] test_input = { "grid": [[1,1,1,2,1,1,1]] } assert my_solution.constructProductMatrix(**test_input) == [[2,2,2,1,2,2,2]] test_input = { "grid": [[1,1,2,1,2,1,2]] } assert my_solution.constructProductMatrix(**test_input) == [[8,8,4,8,4,8,4]] test_input = { "grid": [[1,1,6,5,6],[1,6,6,2,5],[3,4,1,1,4]] } assert my_solution.constructProductMatrix(**test_input) == [[11805,11805,12255,4830,12255],[11805,12255,12255,12075,4830],[12165,12210,11805,11805,12210]] test_input = { "grid": [[1,2,1,1,1,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[8,4,8,8,8,4,4]] test_input = { "grid": [[1,2,1,2,2,1,1]] } assert my_solution.constructProductMatrix(**test_input) == [[8,4,8,4,4,8,8]] test_input = { "grid": [[1,3,1,2,1,2],[3,2,3,4,2,3]] } assert my_solution.constructProductMatrix(**test_input) == [[5184,1728,5184,2592,5184,2592],[1728,2592,1728,1296,2592,1728]] test_input = { "grid": [[1,3,5,2],[1,3,6,5],[8,1,2,7],[6,2,3,5]] } assert my_solution.constructProductMatrix(**test_input) == [[2895,9195,10455,7620],[2895,9195,10770,10455],[1905,2895,7620,10995],[10770,7620,9195,10455]] test_input = { "grid": [[1,4,4,4,3,3],[1,1,3,4,4,1]] } assert my_solution.constructProductMatrix(**test_input) == [[2958,6912,6912,6912,9216,9216],[2958,2958,9216,6912,6912,2958]] test_input = { "grid": [[1,5,5,6],[4,7,6,1],[2,3,3,6],[3,6,3,7]] } assert my_solution.constructProductMatrix(**test_input) == [[6570,11190,11190,1095],[7815,11520,1095,6570],[3285,2190,2190,1095],[2190,1095,2190,11520]] test_input = { "grid": [[1,6,4,4,1],[6,2,5,1,4],[6,4,3,5,6]] } assert my_solution.constructProductMatrix(**test_input) == [[3705,10905,10185,10185,3705],[10905,8025,3210,3705,10185],[10905,10185,9465,3210,10905]] test_input = { "grid": [[1,7,2,8],[3,7,2,5],[2,3,5,6],[5,4,2,7]] } assert my_solution.constructProductMatrix(**test_input) == [[4065,7635,8205,11310],[9585,7635,8205,8220],[8205,9585,8220,10965],[8220,10275,8205,7635]] test_input = { "grid": [[2,1,1,1,1,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[4,8,8,8,8,4,4]] test_input = { "grid": [[2,1,1,1,2,2,1]] } assert my_solution.constructProductMatrix(**test_input) == [[4,8,8,8,4,4,8]] test_input = { "grid": [[2,1,2,2,2,2,2]] } assert my_solution.constructProductMatrix(**test_input) == [[32,64,32,32,32,32,32]] test_input = { "grid": [[2,1,3,2,4,4],[1,3,2,2,4,1]] } assert my_solution.constructProductMatrix(**test_input) == [[4608,9216,3072,4608,2304,2304],[9216,3072,4608,4608,2304,9216]] test_input = { "grid": [[2,2,2,2,1,2,1]] } assert my_solution.constructProductMatrix(**test_input) == [[16,16,16,16,32,16,32]] test_input = { "grid": [[2,4,3,3,3,4],[3,1,4,3,4,2]] } assert my_solution.constructProductMatrix(**test_input) == [[966,483,8874,8874,8874,483],[8874,1932,483,8874,483,966]] test_input = { "grid": [[2,5,4,8],[4,6,3,3],[1,5,1,4],[8,6,6,5]] } assert my_solution.constructProductMatrix(**test_input) == [[30,4950,15,6180],[15,4125,8250,8250],[60,4950,60,15],[6180,4125,4125,4950]] test_input = { "grid": [[2,7],[10,12],[2,4],[8,11],[2,12],[11,2]] } assert my_solution.constructProductMatrix(**test_input) == [[8340,5910],[6606,5505],[8340,4170],[2085,8250],[8340,5505],[8250,8340]] test_input = { "grid": [[2,9],[1,4],[10,12],[2,7],[4,10],[10,8]] } assert my_solution.constructProductMatrix(**test_input) == [[3435,10365],[6870,7890],[5625,10860],[3435,2745],[7890,5625],[5625,3945]] test_input = { "grid": [[3],[9],[5],[1],[4],[14],[12]] } assert my_solution.constructProductMatrix(**test_input) == [[5550],[10080],[5799],[4305],[10335],[6480],[7560]] test_input = { "grid": [[3,2,2,2,4,3],[4,1,3,1,1,2]] } assert my_solution.constructProductMatrix(**test_input) == [[2304,3456,3456,3456,1728,2304],[1728,6912,2304,6912,6912,3456]] test_input = { "grid": [[3,2,4,2,2,3],[2,2,4,1,2,3]] } assert my_solution.constructProductMatrix(**test_input) == [[9216,1479,6912,1479,1479,9216],[1479,1479,6912,2958,1479,9216]] test_input = { "grid": [[3,6,2,2,5],[1,4,3,4,1],[4,5,2,4,4]] } assert my_solution.constructProductMatrix(**test_input) == [[7590,3795,11385,11385,2085],[10425,11865,7590,11865,10425],[11865,2085,11385,11865,11865]] test_input = { "grid": [[4,1,5,4,5],[6,5,3,4,4],[3,2,6,2,3]] } assert my_solution.constructProductMatrix(**test_input) == [[6945,3090,8025,6945,8025],[8745,8025,5145,6945,6945],[5145,1545,8745,1545,5145]] test_input = { "grid": [[4,3,9],[3,9,10],[9,7,8],[8,4,7],[6,1,3]] } assert my_solution.constructProductMatrix(**test_input) == [[3255,225,75],[225,75,11178],[75,1860,7800],[7800,3255,1860],[6285,675,225]] test_input = { "grid": [[5,1,1,1,5],[3,4,2,6,6],[3,3,2,5,1]] } assert my_solution.constructProductMatrix(**test_input) == [[6105,5835,5835,5835,6105],[6060,4545,9090,3030,3030],[6060,6060,9090,6105,5835]] test_input = { "grid": [[5,3],[9,2],[2,6],[4,9],[12,2],[10,4]] } assert my_solution.constructProductMatrix(**test_input) == [[1050,5865],[10185,2625],[2625,9105],[7485,10185],[10725,2625],[525,7485]]
1,697,337,000
biweekly-contest-115-last-visited-integers
https://leetcode.com/problems/last-visited-integers
last-visited-integers
{ "questionId": "3164", "questionFrontendId": "2899", "title": "Last Visited Integers", "titleSlug": "last-visited-integers", "isPaidOnly": false, "difficulty": "Easy", "likes": 74, "dislikes": 139, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的字符串数组 words ,其中 words[i] 要么是一个字符串形式的正整数,要么是字符串 "prev" 。 我们从数组的开头开始遍历,对于 words 中的每个 "prev" 字符串,找到 words 中的 上一个遍历的整数 ,定义如下: * k 表示到当前位置为止的连续 "prev" 字符串数目(包含当前字符串),令下标从 0 开始的 整数 数组 nums 表示目前为止遍历过的所有整数,同时用 nums_reverse 表示 nums 反转得到的数组,那么当前 "prev" 对应的 上一个遍历的整数 是 nums_reverse 数组中下标为 (k - 1) 的整数。 * 如果 k 比目前为止遍历过的整数数目 更多 ,那么上一个遍历的整数为 -1 。 请你返回一个整数数组,包含所有上一个遍历的整数。 示例 1: 输入:words = ["1","2","prev","prev","prev"] 输出:[2,1,-1] 解释: 对于下标为 2 处的 "prev" ,上一个遍历的整数是 2 ,因为连续 "prev" 数目为 1 ,同时在数组 reverse_nums 中,第一个元素是 2 。 对于下标为 3 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev" 数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。 对于下标为 4 处的 "prev" ,上一个遍历的整数是 -1 ,因为连续 "prev" 数目为 3 ,但总共只遍历过 2 个整数。 示例 2: 输入:words = ["1","prev","2","prev","prev"] 输出:[1,2,1] 解释: 对于下标为 1 处的 "prev" ,上一个遍历的整数是 1 。 对于下标为 3 处的 "prev" ,上一个遍历的整数是 2 。 对于下标为 4 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev" 数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。 提示: * 1 <= words.length <= 100 * words[i] == "prev" 或 1 <= int(words[i]) <= 100 """ class Solution: def lastVisitedIntegers(self, words: List[str]) -> List[int]:
给你一个下标从 0 开始的字符串数组 words ,其中 words[i] 要么是一个字符串形式的正整数,要么是字符串 "prev" 。 我们从数组的开头开始遍历,对于 words 中的每个 "prev" 字符串,找到 words 中的 上一个遍历的整数 ,定义如下: * k 表示到当前位置为止的连续 "prev" 字符串数目(包含当前字符串),令下标从 0 开始的 整数 数组 nums 表示目前为止遍历过的所有整数,同时用 nums_reverse 表示 nums 反转得到的数组,那么当前 "prev" 对应的 上一个遍历的整数 是 nums_reverse 数组中下标为 (k - 1) 的整数。 * 如果 k 比目前为止遍历过的整数数目 更多 ,那么上一个遍历的整数为 -1 。 请你返回一个整数数组,包含所有上一个遍历的整数。 示例 1: 输入:words = ["1","2","prev","prev","prev"] 输出:[2,1,-1] 解释: 对于下标为 2 处的 "prev" ,上一个遍历的整数是 2 ,因为连续 "prev" 数目为 1 ,同时在数组 reverse_nums 中,第一个元素是 2 。 对于下标为 3 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev" 数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。 对于下标为 4 处的 "prev" ,上一个遍历的整数是 -1 ,因为连续 "prev" 数目为 3 ,但总共只遍历过 2 个整数。 示例 2: 输入:words = ["1","prev","2","prev","prev"] 输出:[1,2,1] 解释: 对于下标为 1 处的 "prev" ,上一个遍历的整数是 1 。 对于下标为 3 处的 "prev" ,上一个遍历的整数是 2 。 对于下标为 4 处的 "prev" ,上一个遍历的整数是 1 ,因为连续 "prev" 数目为 2 ,同时在数组 reverse_nums 中,第二个元素是 1 。 提示: * 1 <= words.length <= 100 * words[i] == "prev" 或 1 <= int(words[i]) <= 100 请完成下面的代码来解决上述问题: ```python class Solution: def lastVisitedIntegers(self, words: List[str]) -> List[int]: ```
my_solution = Solution() test_input = { "words": ["1","2","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [2,1,-1] test_input = { "words": ["1","prev","2","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [1,2,1] test_input = { "words": ["prev","prev","prev","27"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1] test_input = { "words": ["17","42"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1] test_input = { "words": ["prev","prev","prev","52","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,52] test_input = { "words": ["prev","prev","68","prev","prev","53","40","23","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,68,-1,23] test_input = { "words": ["99","23","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [23] test_input = { "words": ["prev","prev","prev","58","99","prev","10","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,99,10] test_input = { "words": ["prev","51","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,51,-1] test_input = { "words": ["prev","46","9","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,9] test_input = { "words": ["prev","prev","prev","prev","prev","26"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1] test_input = { "words": ["prev","21","prev","76","82","prev","96","prev","57","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,21,82,96,57] test_input = { "words": ["52","4","prev","prev","prev","69"] } assert my_solution.lastVisitedIntegers(**test_input) == [4,52,-1] test_input = { "words": ["24","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [24] test_input = { "words": ["46","prev","78","prev","83","21","prev","94","50"] } assert my_solution.lastVisitedIntegers(**test_input) == [46,78,21] test_input = { "words": ["14","66","prev","prev","46","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [66,14,46] test_input = { "words": ["35","90"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["prev","9","prev","8","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,9,8] test_input = { "words": ["prev","prev","88","71","47","65","24","39"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1] test_input = { "words": ["45","73","78","2","54","prev","85","62","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [54,62] test_input = { "words": ["prev","prev","80","9","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,9] test_input = { "words": ["79","19","prev","prev","prev","67","prev","16","2"] } assert my_solution.lastVisitedIntegers(**test_input) == [19,79,-1,67] test_input = { "words": ["94","prev","prev","prev","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [94,-1,-1,-1,-1,-1] test_input = { "words": ["prev","prev","prev","82","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,82] test_input = { "words": ["94","14","81","43","prev","43","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [43,43] test_input = { "words": ["prev","prev","94","56","prev","32","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,56,32,56,94] test_input = { "words": ["93"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["46","91","3","40","31","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [31] test_input = { "words": ["41","prev","17","58","78"] } assert my_solution.lastVisitedIntegers(**test_input) == [41] test_input = { "words": ["prev","prev","82","41","96","89","71"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1] test_input = { "words": ["4","prev","50","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [4,50,4] test_input = { "words": ["59","76","prev","29","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [76,29] test_input = { "words": ["prev","62"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1] test_input = { "words": ["6","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [6] test_input = { "words": ["prev","prev","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1] test_input = { "words": ["28","5","35","prev","41","27","70","65","84"] } assert my_solution.lastVisitedIntegers(**test_input) == [35] test_input = { "words": ["94","45","prev","61"] } assert my_solution.lastVisitedIntegers(**test_input) == [45] test_input = { "words": ["prev","34","prev","prev","prev","prev","21","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,34,-1,-1,-1,21] test_input = { "words": ["prev","12","100","33","prev","85","93"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,33] test_input = { "words": ["26"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["27","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [27,-1,-1] test_input = { "words": ["prev","prev","22","33","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,33,22,-1] test_input = { "words": ["30","prev","87","prev","19","prev","8","prev","81"] } assert my_solution.lastVisitedIntegers(**test_input) == [30,87,19,8] test_input = { "words": ["35","prev","47","82","86","84","prev","76","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [35,84,76] test_input = { "words": ["prev","87"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1] test_input = { "words": ["prev","69","78","prev","prev","16"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,78,69] test_input = { "words": ["22","97","prev","2"] } assert my_solution.lastVisitedIntegers(**test_input) == [97] test_input = { "words": ["72","74"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["84","prev","prev","21"] } assert my_solution.lastVisitedIntegers(**test_input) == [84,-1] test_input = { "words": ["64","24"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["17","prev","59","prev","51","11","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [17,59,11,51] test_input = { "words": ["57","prev","27","30","prev","prev","75"] } assert my_solution.lastVisitedIntegers(**test_input) == [57,30,27] test_input = { "words": ["65","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [65,-1] test_input = { "words": ["prev","53","76","54","94","77"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1] test_input = { "words": ["89","51","prev","prev","12","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [51,89,12,51] test_input = { "words": ["prev","28","25","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,25,28,-1] test_input = { "words": ["51","prev","prev","76"] } assert my_solution.lastVisitedIntegers(**test_input) == [51,-1] test_input = { "words": ["2","24","63","prev","43","19","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [63,19] test_input = { "words": ["prev","38","1"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1] test_input = { "words": ["56","75","prev","prev","94"] } assert my_solution.lastVisitedIntegers(**test_input) == [75,56] test_input = { "words": ["prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1] test_input = { "words": ["prev","37","25","31","prev","prev","42"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,31,25] test_input = { "words": ["73","30","prev","20","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [30,20,30] test_input = { "words": ["85","prev","prev","78","prev","100","8","17","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [85,-1,78,17] test_input = { "words": ["prev","55","prev","87","19","prev","13","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,55,19,13,19] test_input = { "words": ["prev","prev","5","prev","prev","prev","80","17"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,5,-1,-1] test_input = { "words": ["100","3","prev","prev","93","35","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [3,100,35,93] test_input = { "words": ["75","7"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["prev","prev","prev","prev","prev","prev","71","prev","27"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1,-1,71] test_input = { "words": ["prev","prev","prev","91","44","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,44] test_input = { "words": ["prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1] test_input = { "words": ["11","prev","87","prev","prev","94","prev","68"] } assert my_solution.lastVisitedIntegers(**test_input) == [11,87,11,94] test_input = { "words": ["78"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["prev","prev","73","prev","prev","27","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,73,-1,27] test_input = { "words": ["prev","70","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,70,-1] test_input = { "words": ["68","prev","38","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [68,38,68] test_input = { "words": ["prev","prev","36"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1] test_input = { "words": ["prev","prev","36","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,36] test_input = { "words": ["18","58","41","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [41,58] test_input = { "words": ["prev","prev","35"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1] test_input = { "words": ["prev","72","prev","96","9","50","prev","52"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,72,50] test_input = { "words": ["92","95","47","48","prev","50","34","prev","prev","46"] } assert my_solution.lastVisitedIntegers(**test_input) == [48,34,50] test_input = { "words": ["36","88","15","99","48"] } assert my_solution.lastVisitedIntegers(**test_input) == [] test_input = { "words": ["93","prev","2","58","83","90","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [93,90] test_input = { "words": ["prev","68","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,68] test_input = { "words": ["prev","56","prev","prev","36","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,56,-1,36] test_input = { "words": ["53","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [53] test_input = { "words": ["5","29","94","3","48","prev","59","90","prev","69"] } assert my_solution.lastVisitedIntegers(**test_input) == [48,90] test_input = { "words": ["89","prev","prev","75","prev","98","80","prev","68","33"] } assert my_solution.lastVisitedIntegers(**test_input) == [89,-1,75,80] test_input = { "words": ["prev","93","prev","74","33"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,93] test_input = { "words": ["prev","prev","10","25","prev","54","prev","prev","prev","76"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,25,54,25,10] test_input = { "words": ["9","prev","14","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [9,14] test_input = { "words": ["prev","prev","prev","18","66","92","prev","87","85","25"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,92] test_input = { "words": ["prev","prev","prev","16","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,16] test_input = { "words": ["prev","prev","99","prev","82","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,99,82,99] test_input = { "words": ["prev","5","90","71","prev","prev","61"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,71,90] test_input = { "words": ["prev","prev","71","54","prev","20","65","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,54,65,20] test_input = { "words": ["prev","85","prev","93","prev","98","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,85,93,98] test_input = { "words": ["prev","prev","34","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,34]
1,697,293,800
biweekly-contest-115-longest-unequal-adjacent-groups-subsequence-i
https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i
longest-unequal-adjacent-groups-subsequence-i
{ "questionId": "3143", "questionFrontendId": "2900", "title": "Longest Unequal Adjacent Groups Subsequence I", "titleSlug": "longest-unequal-adjacent-groups-subsequence-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 86, "dislikes": 26, "categoryTitle": "Algorithms" }
""" 给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的 二进制 数组 groups ,两个数组长度都是 n 。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 请你返回一个字符串数组,它是下标子序列 依次 对应 words 数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。 子序列 指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不改变相对位置得到的新的数组。 注意:words 中的字符串长度可能 不相等 。 示例 1: 输入:n = 3, words = ["e","a","b"], groups = [0,0,1] 输出:["e","b"] 解释:一个可行的子序列是 [0,2] ,因为 groups[0] != groups[2] 。 所以一个可行的答案是 [words[0],words[2]] = ["e","b"] 。 另一个可行的子序列是 [1,2] ,因为 groups[1] != groups[2] 。 得到答案为 [words[1],words[2]] = ["a","b"] 。 这也是一个可行的答案。 符合题意的最长子序列的长度为 2 。 示例 2: 输入:n = 4, words = ["a","b","c","d"], groups = [1,0,1,1] 输出:["a","b","c"] 解释:一个可行的子序列为 [0,1,2] 因为 groups[0] != groups[1] 且 groups[1] != groups[2] 。 所以一个可行的答案是 [words[0],words[1],words[2]] = ["a","b","c"] 。 另一个可行的子序列为 [0,1,3] 因为 groups[0] != groups[1] 且 groups[1] != groups[3] 。 得到答案为 [words[0],words[1],words[3]] = ["a","b","d"] 。 这也是一个可行的答案。 符合题意的最长子序列的长度为 3 。 提示: * 1 <= n == words.length == groups.length <= 100 * 1 <= words[i].length <= 10 * 0 <= groups[i] < 2 * words 中的字符串 互不相同 。 * words[i] 只包含小写英文字母。 """ class Solution: def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的 二进制 数组 groups ,两个数组长度都是 n 。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 请你返回一个字符串数组,它是下标子序列 依次 对应 words 数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。 子序列 指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不改变相对位置得到的新的数组。 注意:words 中的字符串长度可能 不相等 。 示例 1: 输入:n = 3, words = ["e","a","b"], groups = [0,0,1] 输出:["e","b"] 解释:一个可行的子序列是 [0,2] ,因为 groups[0] != groups[2] 。 所以一个可行的答案是 [words[0],words[2]] = ["e","b"] 。 另一个可行的子序列是 [1,2] ,因为 groups[1] != groups[2] 。 得到答案为 [words[1],words[2]] = ["a","b"] 。 这也是一个可行的答案。 符合题意的最长子序列的长度为 2 。 示例 2: 输入:n = 4, words = ["a","b","c","d"], groups = [1,0,1,1] 输出:["a","b","c"] 解释:一个可行的子序列为 [0,1,2] 因为 groups[0] != groups[1] 且 groups[1] != groups[2] 。 所以一个可行的答案是 [words[0],words[1],words[2]] = ["a","b","c"] 。 另一个可行的子序列为 [0,1,3] 因为 groups[0] != groups[1] 且 groups[1] != groups[3] 。 得到答案为 [words[0],words[1],words[3]] = ["a","b","d"] 。 这也是一个可行的答案。 符合题意的最长子序列的长度为 3 。 提示: * 1 <= n == words.length == groups.length <= 100 * 1 <= words[i].length <= 10 * 0 <= groups[i] < 2 * words 中的字符串 互不相同 。 * words[i] 只包含小写英文字母。 请完成下面的代码来解决上述问题: ```python class Solution: def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]: ```
my_solution = Solution() test_input = { "n": 3, "words": ["e","a","b"], "groups": [0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["e","b"] test_input = { "n": 4, "words": ["a","b","c","d"], "groups": [1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["a","b","c"] test_input = { "n": 1, "words": ["c"], "groups": [0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c"] test_input = { "n": 1, "words": ["d"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d"] test_input = { "n": 1, "words": ["e"], "groups": [0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["e"] test_input = { "n": 1, "words": ["fe"], "groups": [0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["fe"] test_input = { "n": 1, "words": ["frl"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["frl"] test_input = { "n": 1, "words": ["ha"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ha"] test_input = { "n": 1, "words": ["l"], "groups": [0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["l"] test_input = { "n": 1, "words": ["n"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n"] test_input = { "n": 1, "words": ["s"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["s"] test_input = { "n": 2, "words": ["d","g"], "groups": [0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d","g"] test_input = { "n": 2, "words": ["lr","h"], "groups": [0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lr"] test_input = { "n": 2, "words": ["wx","h"], "groups": [0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["wx","h"] test_input = { "n": 2, "words": ["yw","n"], "groups": [0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["yw","n"] test_input = { "n": 2, "words": ["z","n"], "groups": [0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z"] test_input = { "n": 2, "words": ["zr","a"], "groups": [0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["zr"] test_input = { "n": 3, "words": ["h","vv","kp"], "groups": [0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["h","vv","kp"] test_input = { "n": 3, "words": ["m","v","y"], "groups": [0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["m","v","y"] test_input = { "n": 3, "words": ["o","cfy","en"], "groups": [1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["o","cfy"] test_input = { "n": 3, "words": ["tu","rv","bn"], "groups": [0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["tu"] test_input = { "n": 4, "words": ["c","f","y","i"], "groups": [1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","f","y"] test_input = { "n": 4, "words": ["c","w","h","s"], "groups": [0,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","s"] test_input = { "n": 4, "words": ["d","a","v","b"], "groups": [1,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d","a","b"] test_input = { "n": 4, "words": ["hh","svj","a","nr"], "groups": [1,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["hh"] test_input = { "n": 4, "words": ["im","j","xq","cjs"], "groups": [1,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["im","j","cjs"] test_input = { "n": 4, "words": ["m","dkg","r","h"], "groups": [1,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["m","h"] test_input = { "n": 4, "words": ["ow","qay","r","j"], "groups": [1,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ow"] test_input = { "n": 4, "words": ["r","k","pb","x"], "groups": [0,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["r","pb","x"] test_input = { "n": 4, "words": ["sq","do","bcj","nm"], "groups": [0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["sq","do","nm"] test_input = { "n": 4, "words": ["sz","mq","j","u"], "groups": [0,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["sz","j","u"] test_input = { "n": 4, "words": ["x","nf","p","asn"], "groups": [1,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["x"] test_input = { "n": 4, "words": ["z","tkt","x","swy"], "groups": [1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z","tkt","x"] test_input = { "n": 5, "words": ["ht","lw","ax","vi","fo"], "groups": [0,0,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ht","ax","vi"] test_input = { "n": 5, "words": ["mc","kh","x","q","z"], "groups": [0,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["mc","x","z"] test_input = { "n": 5, "words": ["n","fg","fy","tv","gv"], "groups": [1,1,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n"] test_input = { "n": 5, "words": ["n","l","e","d","m"], "groups": [1,1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n","e","d"] test_input = { "n": 5, "words": ["n","m","g","b","d"], "groups": [0,0,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n","g","b"] test_input = { "n": 5, "words": ["nz","zwt","hig","s","jze"], "groups": [1,1,1,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["nz","s","jze"] test_input = { "n": 5, "words": ["o","i","b","k","kz"], "groups": [0,0,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["o","b"] test_input = { "n": 5, "words": ["r","o","k","d","f"], "groups": [0,0,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["r","d"] test_input = { "n": 5, "words": ["sfh","exd","j","w","gc"], "groups": [1,0,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["sfh","exd","j"] test_input = { "n": 5, "words": ["v","f","k","l","p"], "groups": [0,0,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["v","k","l"] test_input = { "n": 5, "words": ["vbd","ua","muo","mu","qi"], "groups": [0,0,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["vbd","mu","qi"] test_input = { "n": 5, "words": ["we","ch","tl","yx","utx"], "groups": [1,0,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["we","ch","yx"] test_input = { "n": 5, "words": ["x","vlk","tds","dfn","kr"], "groups": [0,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["x","tds","kr"] test_input = { "n": 5, "words": ["y","j","u","r","f"], "groups": [0,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","u","f"] test_input = { "n": 5, "words": ["y","r","z","x","q"], "groups": [0,1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","r","z","x"] test_input = { "n": 5, "words": ["yc","fgq","gg","og","tca"], "groups": [0,1,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["yc","fgq","tca"] test_input = { "n": 5, "words": ["z","d","p","c","m"], "groups": [0,0,0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z"] test_input = { "n": 6, "words": ["c","i","to","kv","op","u"], "groups": [0,0,1,0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","to","kv"] test_input = { "n": 6, "words": ["d","h","e","k","j","r"], "groups": [0,1,1,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["d","h","k","j","r"] test_input = { "n": 6, "words": ["l","f","v","b","w","k"], "groups": [1,0,1,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["l","f","v","w"] test_input = { "n": 6, "words": ["lj","vf","pa","w","z","q"], "groups": [0,0,1,0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lj","pa","w"] test_input = { "n": 7, "words": ["cd","oki","ho","oi","m","yvy","i"], "groups": [1,1,0,1,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd","ho","oi"] test_input = { "n": 7, "words": ["exb","c","oq","lq","xh","zmo","aug"], "groups": [1,1,0,1,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["exb","oq","lq","zmo"] test_input = { "n": 7, "words": ["f","r","k","h","m","v","p"], "groups": [1,0,0,0,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["f","r","m","v"] test_input = { "n": 7, "words": ["fd","fc","jm","z","lg","kl","ux"], "groups": [0,1,0,1,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["fd","fc","jm","z","lg","kl","ux"] test_input = { "n": 7, "words": ["ft","iw","m","v","gx","d","pm"], "groups": [1,1,1,0,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ft","v","gx"] test_input = { "n": 7, "words": ["lma","i","rt","xar","bfx","np","x"], "groups": [1,1,1,1,1,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lma","np","x"] test_input = { "n": 7, "words": ["nsv","r","o","qo","pb","xqv","clb"], "groups": [1,1,0,0,0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["nsv","o"] test_input = { "n": 7, "words": ["p","qdb","zcd","l","tv","ln","ogb"], "groups": [1,1,0,1,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["p","zcd","l","tv","ogb"] test_input = { "n": 7, "words": ["z","cee","j","jqu","w","ljr","k"], "groups": [1,0,1,1,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["z","cee","j","w","k"] test_input = { "n": 8, "words": ["h","p","q","t","j","a","c","n"], "groups": [0,1,1,1,0,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["h","p","j","c"] test_input = { "n": 8, "words": ["r","v","c","t","d","a","x","o"], "groups": [1,1,0,1,1,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["r","c","t","a","o"] test_input = { "n": 8, "words": ["u","l","a","y","j","s","h","q"], "groups": [0,0,0,0,0,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["u","s","h"] test_input = { "n": 8, "words": ["x","mr","yyf","l","z","q","zvj","zqt"], "groups": [0,1,1,1,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["x","mr","z","q","zqt"] test_input = { "n": 8, "words": ["y","x","i","xtm","ze","n","cma","dgk"], "groups": [0,1,0,0,1,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","x","i","ze","cma"] test_input = { "n": 8, "words": ["yun","x","zpp","bpr","ii","ezg","dn","k"], "groups": [0,1,1,1,1,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["yun","x","ezg","dn","k"] test_input = { "n": 9, "words": ["ckr","iz","top","of","sb","wv","hb","da","wd"], "groups": [1,1,0,1,1,0,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ckr","top","of","wv","wd"] test_input = { "n": 9, "words": ["g","h","u","n","w","o","f","p","m"], "groups": [1,0,0,1,1,0,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["g","h","n","o","p","m"] test_input = { "n": 9, "words": ["ilw","t","dyy","irz","oxy","k","rfj","hi","zxe"], "groups": [1,1,1,0,1,1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ilw","irz","oxy","rfj","hi"] test_input = { "n": 9, "words": ["l","iuz","d","tfw","mu","a","rp","mrb","wnl"], "groups": [1,1,1,1,1,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["l","a","rp","wnl"] test_input = { "n": 9, "words": ["mc","b","yr","cj","zk","wi","esm","yu","cw"], "groups": [0,0,1,1,0,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["mc","yr","zk","esm","cw"] test_input = { "n": 9, "words": ["nw","hx","ygc","vjo","jmv","p","juv","b","y"], "groups": [0,1,0,0,1,0,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["nw","hx","ygc","jmv","p","b","y"] test_input = { "n": 9, "words": ["osq","qiw","h","tc","xg","tvt","fqp","zq","b"], "groups": [0,0,1,0,1,1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["osq","h","tc","xg","fqp","zq"] test_input = { "n": 9, "words": ["vr","lw","e","g","dz","kf","qe","h","p"], "groups": [1,0,0,1,1,0,0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["vr","lw","g","kf"] test_input = { "n": 10, "words": ["gy","nd","l","hr","i","qf","zz","nq","e","oa"], "groups": [0,1,0,0,1,0,1,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["gy","nd","l","i","qf","zz","oa"] test_input = { "n": 10, "words": ["j","r","h","t","z","b","a","s","v","q"], "groups": [1,0,1,1,1,1,0,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["j","r","h","a","q"] test_input = { "n": 10, "words": ["k","f","u","h","x","w","c","e","l","p"], "groups": [0,1,1,1,1,1,1,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["k","f","p"] test_input = { "n": 10, "words": ["lj","huy","lg","h","o","b","ava","ay","r","us"], "groups": [1,1,1,1,0,0,1,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["lj","o","ava"] test_input = { "n": 10, "words": ["m","d","xv","dp","nq","xi","e","g","n","qw"], "groups": [1,0,1,1,1,1,0,1,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["m","d","xv","e","g","n","qw"] test_input = { "n": 10, "words": ["n","c","y","h","w","m","g","t","x","v"], "groups": [1,1,1,0,0,1,0,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["n","h","m","g","v"] test_input = { "n": 10, "words": ["o","w","l","g","m","x","f","q","c","v"], "groups": [1,1,1,0,1,1,1,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["o","g","m","q","v"] test_input = { "n": 10, "words": ["p","mw","m","xld","j","jv","n","so","pkd","rwt"], "groups": [0,0,1,0,1,1,0,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["p","m","xld","j","n","pkd"] test_input = { "n": 10, "words": ["vyv","msl","d","bu","ubl","bgk","sz","njv","pf","s"], "groups": [1,0,1,1,0,0,1,0,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["vyv","msl","d","ubl","sz","njv","pf","s"] test_input = { "n": 10, "words": ["y","mz","lt","ur","o","m","djh","tb","w","j"], "groups": [0,0,1,0,1,1,0,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","lt","ur","o","djh","tb","w"] test_input = { "n": 10, "words": ["y","s","i","v","a","w","l","q","k","t"], "groups": [0,1,1,1,0,1,1,1,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["y","s","a","w","k"] test_input = { "n": 11, "words": ["a","tea","ldt","ybm","zkw","r","d","dms","le","u","ze"], "groups": [1,1,0,0,0,1,1,1,1,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["a","ldt","r","u","ze"] test_input = { "n": 11, "words": ["c","o","e","r","x","w","b","d","h","y","z"], "groups": [1,0,1,0,1,0,1,1,1,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["c","o","e","r","x","w","b","y","z"] test_input = { "n": 11, "words": ["chu","a","qdx","fgd","qe","bqc","x","kbx","sv","ly","br"], "groups": [1,0,0,0,0,0,1,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["chu","a","x","kbx","sv","br"] test_input = { "n": 11, "words": ["ec","jdf","b","wa","kjd","bb","ty","yi","ybw","ilj","cv"], "groups": [0,1,0,1,1,1,1,1,1,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ec","jdf","b","wa","ilj","cv"] test_input = { "n": 11, "words": ["ew","isn","fl","mg","pdg","d","p","hh","e","y","whm"], "groups": [0,0,1,1,0,0,0,0,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ew","fl","pdg","y"] test_input = { "n": 11, "words": ["h","o","d","y","r","c","p","b","g","j","k"], "groups": [1,1,0,1,1,0,1,1,0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["h","d","y","c","p","g"] test_input = { "n": 11, "words": ["ipr","l","zy","j","h","hdt","m","d","pd","nv","wy"], "groups": [1,1,1,1,0,1,0,1,1,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ipr","h","hdt","m","d"] test_input = { "n": 11, "words": ["j","g","go","a","f","bg","o","l","ze","kq","w"], "groups": [0,0,1,0,1,0,0,0,1,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["j","go","a","f","bg","ze","kq","w"] test_input = { "n": 11, "words": ["j","r","a","g","x","b","y","v","k","i","c"], "groups": [0,1,0,0,0,0,1,1,0,0,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["j","r","a","y","k"] test_input = { "n": 11, "words": ["kgo","han","nlu","tv","us","pk","xw","cxc","eml","v","msz"], "groups": [1,0,0,1,0,0,1,0,1,1,0] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["kgo","han","tv","us","xw","cxc","eml","msz"] test_input = { "n": 11, "words": ["kh","op","ij","te","hk","pmt","v","ne","en","b","zuj"], "groups": [0,0,1,1,1,0,1,1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["kh","ij","pmt","v","en","b"] test_input = { "n": 11, "words": ["ms","t","oz","x","pw","ik","d","gj","z","ps","i"], "groups": [1,1,0,1,0,0,1,1,0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ms","oz","x","pw","d","z","i"]
1,697,293,800
biweekly-contest-115-longest-unequal-adjacent-groups-subsequence-ii
https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-ii
longest-unequal-adjacent-groups-subsequence-ii
{ "questionId": "3142", "questionFrontendId": "2901", "title": "Longest Unequal Adjacent Groups Subsequence II", "titleSlug": "longest-unequal-adjacent-groups-subsequence-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 172, "dislikes": 16, "categoryTitle": "Algorithms" }
""" 给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的数组 groups ,两个数组长度都是 n 。 两个长度相等字符串的 汉明距离 定义为对应位置字符 不同 的数目。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,它需要满足以下条件: * 相邻 下标对应的 groups 值 不同。即,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 * 对于所有 0 < j + 1 < k 的下标 j ,都满足 words[ij] 和 words[ij + 1] 的长度 相等 ,且两个字符串之间的 汉明距离 为 1 。 请你返回一个字符串数组,它是下标子序列 依次 对应 words 数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。 子序列 指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不改变相对位置得到的新的数组。 注意:words 中的字符串长度可能 不相等 。 示例 1: 输入:n = 3, words = ["bab","dab","cab"], groups = [1,2,2] 输出:["bab","cab"] 解释:一个可行的子序列是 [0,2] 。 - groups[0] != groups[2] - words[0].length == words[2].length 且它们之间的汉明距离为 1 。 所以一个可行的答案是 [words[0],words[2]] = ["bab","cab"] 。 另一个可行的子序列是 [0,1] 。 - groups[0] != groups[1] - words[0].length = words[1].length 且它们之间的汉明距离为 1 。 所以另一个可行的答案是 [words[0],words[1]] = ["bab","dab"] 。 符合题意的最长子序列的长度为 2 。 示例 2: 输入:n = 4, words = ["a","b","c","d"], groups = [1,2,3,4] 输出:["a","b","c","d"] 解释:我们选择子序列 [0,1,2,3] 。 它同时满足两个条件。 所以答案为 [words[0],words[1],words[2],words[3]] = ["a","b","c","d"] 。 它是所有下标子序列里最长且满足所有条件的。 所以它是唯一的答案。 提示: * 1 <= n == words.length == groups.length <= 1000 * 1 <= words[i].length <= 10 * 1 <= groups[i] <= n * words 中的字符串 互不相同 。 * words[i] 只包含小写英文字母。 """ class Solution: def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的数组 groups ,两个数组长度都是 n 。 两个长度相等字符串的 汉明距离 定义为对应位置字符 不同 的数目。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,它需要满足以下条件: * 相邻 下标对应的 groups 值 不同。即,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 * 对于所有 0 < j + 1 < k 的下标 j ,都满足 words[ij] 和 words[ij + 1] 的长度 相等 ,且两个字符串之间的 汉明距离 为 1 。 请你返回一个字符串数组,它是下标子序列 依次 对应 words 数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。 子序列 指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不改变相对位置得到的新的数组。 注意:words 中的字符串长度可能 不相等 。 示例 1: 输入:n = 3, words = ["bab","dab","cab"], groups = [1,2,2] 输出:["bab","cab"] 解释:一个可行的子序列是 [0,2] 。 - groups[0] != groups[2] - words[0].length == words[2].length 且它们之间的汉明距离为 1 。 所以一个可行的答案是 [words[0],words[2]] = ["bab","cab"] 。 另一个可行的子序列是 [0,1] 。 - groups[0] != groups[1] - words[0].length = words[1].length 且它们之间的汉明距离为 1 。 所以另一个可行的答案是 [words[0],words[1]] = ["bab","dab"] 。 符合题意的最长子序列的长度为 2 。 示例 2: 输入:n = 4, words = ["a","b","c","d"], groups = [1,2,3,4] 输出:["a","b","c","d"] 解释:我们选择子序列 [0,1,2,3] 。 它同时满足两个条件。 所以答案为 [words[0],words[1],words[2],words[3]] = ["a","b","c","d"] 。 它是所有下标子序列里最长且满足所有条件的。 所以它是唯一的答案。 提示: * 1 <= n == words.length == groups.length <= 1000 * 1 <= words[i].length <= 10 * 1 <= groups[i] <= n * words 中的字符串 互不相同 。 * words[i] 只包含小写英文字母。 请完成下面的代码来解决上述问题: ```python class Solution: def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]: ```
my_solution = Solution() test_input = { "n": 3, "words": ["bab","dab","cab"], "groups": [1,2,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bab","cab"] test_input = { "n": 4, "words": ["a","b","c","d"], "groups": [1,2,3,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["a","b","c","d"] test_input = { "n": 1, "words": ["abbbb"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abbbb"] test_input = { "n": 1, "words": ["ad"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ad"] test_input = { "n": 1, "words": ["baaccb"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["baaccb"] test_input = { "n": 1, "words": ["bc"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bc"] test_input = { "n": 1, "words": ["bdb"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdb"] test_input = { "n": 1, "words": ["cc"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cc"] test_input = { "n": 1, "words": ["cd"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd"] test_input = { "n": 1, "words": ["cdb"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdb"] test_input = { "n": 1, "words": ["cea"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cea"] test_input = { "n": 1, "words": ["cebbbb"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cebbbb"] test_input = { "n": 1, "words": ["da"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["da"] test_input = { "n": 1, "words": ["daab"], "groups": [1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["daab"] test_input = { "n": 2, "words": ["adbe","acace"], "groups": [2,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["acace"] test_input = { "n": 2, "words": ["ba","dc"], "groups": [1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dc"] test_input = { "n": 2, "words": ["baa","ada"], "groups": [1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ada"] test_input = { "n": 2, "words": ["bebea","ddecc"], "groups": [1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ddecc"] test_input = { "n": 2, "words": ["cedbca","db"], "groups": [1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["db"] test_input = { "n": 2, "words": ["dbcdd","baba"], "groups": [2,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["baba"] test_input = { "n": 2, "words": ["ddb","bdb"], "groups": [1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdb"] test_input = { "n": 2, "words": ["dee","bb"], "groups": [2,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bb"] test_input = { "n": 2, "words": ["ecd","dbeed"], "groups": [1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbeed"] test_input = { "n": 3, "words": ["aaac","dbede","cbdeee"], "groups": [2,2,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cbdeee"] test_input = { "n": 3, "words": ["aab","ca","cbd"], "groups": [3,3,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cbd"] test_input = { "n": 3, "words": ["aeb","bc","abdb"], "groups": [2,3,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abdb"] test_input = { "n": 3, "words": ["bdb","aaa","ada"], "groups": [2,1,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aaa","ada"] test_input = { "n": 3, "words": ["cc","aa","dda"], "groups": [2,2,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dda"] test_input = { "n": 3, "words": ["cc","aba","dbd"], "groups": [1,1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbd"] test_input = { "n": 3, "words": ["ccd","bb","ccc"], "groups": [1,1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ccd","ccc"] test_input = { "n": 3, "words": ["cda","bb","bdc"], "groups": [3,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdc"] test_input = { "n": 3, "words": ["ceea","ade","aeacba"], "groups": [2,1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aeacba"] test_input = { "n": 3, "words": ["db","ccce","edbac"], "groups": [3,2,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["edbac"] test_input = { "n": 3, "words": ["dba","bb","aa"], "groups": [2,2,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aa"] test_input = { "n": 3, "words": ["dbdaad","daca","cdbdb"], "groups": [1,1,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdbdb"] test_input = { "n": 3, "words": ["dc","bca","ddd"], "groups": [1,3,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ddd"] test_input = { "n": 3, "words": ["dd","bb","aac"], "groups": [3,3,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aac"] test_input = { "n": 3, "words": ["dedcc","cbac","dab"], "groups": [1,3,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dab"] test_input = { "n": 3, "words": ["eee","abecab","dc"], "groups": [2,2,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dc"] test_input = { "n": 4, "words": ["ac","caa","cda","ba"], "groups": [3,1,2,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["caa","cda"] test_input = { "n": 4, "words": ["bab","bac","dbd","dd"], "groups": [1,1,3,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dd"] test_input = { "n": 4, "words": ["bab","bdd","bca","dab"], "groups": [2,4,1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dab"] test_input = { "n": 4, "words": ["bbbd","babca","ebddde","cce"], "groups": [3,4,3,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cce"] test_input = { "n": 4, "words": ["beb","eacedc","aeeb","cdd"], "groups": [1,4,3,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdd"] test_input = { "n": 4, "words": ["cac","aaa","dd","cda"], "groups": [1,4,2,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cda"] test_input = { "n": 4, "words": ["cbb","db","bdd","bd"], "groups": [2,3,4,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bd"] test_input = { "n": 4, "words": ["cdc","dc","bd","aca"], "groups": [3,2,2,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aca"] test_input = { "n": 4, "words": ["ceacd","ac","bebdae","dbbbcb"], "groups": [2,3,1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbbbcb"] test_input = { "n": 4, "words": ["dcaacc","da","ddcbd","dd"], "groups": [2,3,1,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["da","dd"] test_input = { "n": 4, "words": ["deeb","edbea","ad","ecedd"], "groups": [1,1,1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ecedd"] test_input = { "n": 4, "words": ["ebe","bcca","caabaa","abb"], "groups": [1,4,4,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abb"] test_input = { "n": 5, "words": ["abd","bab","bc","ac","acd"], "groups": [3,5,3,3,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abd","acd"] test_input = { "n": 5, "words": ["acc","ab","baa","dac","aa"], "groups": [3,1,2,3,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","aa"] test_input = { "n": 5, "words": ["acda","caae","ccad","ac","ddeedb"], "groups": [4,2,4,1,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ddeedb"] test_input = { "n": 5, "words": ["ade","ea","aabd","bc","aaaabe"], "groups": [4,2,4,4,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aaaabe"] test_input = { "n": 5, "words": ["ba","ee","ed","ddddd","ce"], "groups": [4,4,4,5,5] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ee","ce"] test_input = { "n": 5, "words": ["bacd","adbbab","ba","ec","deecbe"], "groups": [4,4,5,5,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["deecbe"] test_input = { "n": 5, "words": ["bad","cab","abb","cd","ba"], "groups": [2,5,3,4,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ba"] test_input = { "n": 5, "words": ["ca","cb","bcd","bb","ddc"], "groups": [2,4,2,5,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ca","cb","bb"] test_input = { "n": 5, "words": ["ccb","ac","aa","bad","ab"], "groups": [3,5,2,2,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ac","aa","ab"] test_input = { "n": 5, "words": ["cd","dd","ad","aaa","db"], "groups": [2,3,3,5,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd","dd","db"] test_input = { "n": 5, "words": ["cda","ab","cb","ccb","baa"], "groups": [3,1,2,1,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","cb"] test_input = { "n": 5, "words": ["dc","eca","cdade","aaaccd","deb"], "groups": [3,2,3,1,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["deb"] test_input = { "n": 5, "words": ["dceba","dbcab","bacd","bacdab","bdeca"], "groups": [4,3,5,3,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bdeca"] test_input = { "n": 5, "words": ["deeb","ee","bbbbe","bddba","cdb"], "groups": [4,4,1,3,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cdb"] test_input = { "n": 6, "words": ["aab","cab","ba","dba","daa","bca"], "groups": [4,3,4,6,4,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dba","daa"] test_input = { "n": 6, "words": ["aca","dd","aab","dac","adb","bad"], "groups": [2,6,2,1,4,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aab","adb"] test_input = { "n": 6, "words": ["ad","bb","cc","bc","bcb","abc"], "groups": [3,5,5,5,1,5] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["abc"] test_input = { "n": 6, "words": ["bcb","cba","cab","cca","ad","cd"], "groups": [6,5,1,5,5,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ad","cd"] test_input = { "n": 6, "words": ["bdaaee","cb","ecaad","accdd","ba","adad"], "groups": [2,4,4,6,1,5] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["adad"] test_input = { "n": 6, "words": ["bdccb","cece","dbdda","bbc","bcbae","badc"], "groups": [4,2,4,1,1,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["badc"] test_input = { "n": 6, "words": ["cba","cc","cd","ccc","aba","ac"], "groups": [3,6,2,4,2,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cba","aba"] test_input = { "n": 6, "words": ["ccd","db","cbb","cb","cab","acd"], "groups": [5,1,5,5,6,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ccd","acd"] test_input = { "n": 6, "words": ["eb","eaab","accdba","ecba","aec","dacacd"], "groups": [4,6,1,6,2,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dacacd"] test_input = { "n": 6, "words": ["ee","aab","db","cc","dead","aee"], "groups": [5,5,4,2,5,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aee"] test_input = { "n": 7, "words": ["aad","cba","bda","dc","aba","dbc","ac"], "groups": [6,4,1,7,5,2,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dc","ac"] test_input = { "n": 7, "words": ["adcaa","db","dced","ded","eeadce","bdbbe","acaadc"], "groups": [7,5,4,2,1,5,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["acaadc"] test_input = { "n": 7, "words": ["bcabd","cd","cbaadc","cda","bcde","ccedca","ba"], "groups": [7,3,6,7,1,7,5] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ba"] test_input = { "n": 7, "words": ["bd","dbd","dcc","cb","ac","abd","bb"], "groups": [1,3,3,6,6,4,5] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cb","bb"] test_input = { "n": 7, "words": ["cbde","aad","dbdceb","ae","eca","bd","bba"], "groups": [7,5,7,6,4,5,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bba"] test_input = { "n": 7, "words": ["cdcdad","baaee","cba","ceae","ab","bedbab","eb"], "groups": [5,3,7,1,7,6,7] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["eb"] test_input = { "n": 7, "words": ["dabbdb","eacbdb","bbdea","cdcaa","eaeeb","cebabe","ad"], "groups": [3,5,5,6,3,4,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ad"] test_input = { "n": 7, "words": ["dbcca","dcdd","bebbc","cbed","cb","abed","ac"], "groups": [4,6,6,6,3,7,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cbed","abed"] test_input = { "n": 7, "words": ["dcc","cba","ab","cb","aac","aba","db"], "groups": [3,2,1,6,2,6,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","db"] test_input = { "n": 7, "words": ["ddd","cd","adb","bcc","da","ab","ad"], "groups": [2,3,6,7,3,7,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","ad"] test_input = { "n": 7, "words": ["edded","ab","bc","aeac","ec","db","be"], "groups": [3,2,3,6,3,4,3] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ab","db"] test_input = { "n": 8, "words": ["addb","beeddc","dcdce","ddaeed","ddbbb","aeea","adee","dbcbdc"], "groups": [1,5,7,1,5,7,7,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dbcbdc"] test_input = { "n": 8, "words": ["ba","cca","dcb","cd","aa","bd","cda","bcb"], "groups": [6,1,2,2,3,7,7,7] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dcb","bcb"] test_input = { "n": 8, "words": ["baa","cb","aab","ddc","bba","cdb","abb","dc"], "groups": [1,8,1,1,8,6,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["baa","bba"] test_input = { "n": 8, "words": ["bbde","edea","dcd","eebbed","ddab","ae","ec","ade"], "groups": [7,3,2,4,7,3,6,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ade"] test_input = { "n": 8, "words": ["bc","aa","cb","dd","aaa","ccb","da","bbb"], "groups": [5,4,6,8,6,7,7,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dd","da"] test_input = { "n": 8, "words": ["bcc","aac","ac","dd","bdd","ada","bbb","db"], "groups": [7,7,1,7,1,1,6,7] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["db"] test_input = { "n": 8, "words": ["cb","dcc","da","cbb","bd","dbc","ab","db"], "groups": [4,5,5,7,8,1,3,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cb","ab","db"] test_input = { "n": 8, "words": ["cbaeeb","decd","dbc","cbdcca","acbcbe","adccc","eb","ecbbea"], "groups": [6,7,8,6,3,5,1,7] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["ecbbea"] test_input = { "n": 8, "words": ["cc","aa","cab","dbc","bbb","adc","cba","cca"], "groups": [6,1,6,6,4,8,1,5] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cba","cca"] test_input = { "n": 8, "words": ["cc","dcd","dac","dc","ac","ad","bbb","cbb"], "groups": [7,7,2,5,4,1,6,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cc","dc","ac","ad"] test_input = { "n": 8, "words": ["ccdbdc","dcce","ebedde","ceb","edee","ca","ad","dddee"], "groups": [6,6,3,4,7,1,5,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["dddee"] test_input = { "n": 8, "words": ["cd","bd","ada","ba","ac","bac","aad","ccb"], "groups": [3,4,8,7,6,7,2,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cd","bd","ba"] test_input = { "n": 8, "words": ["da","bd","ccd","dd","ab","cc","aab","ac"], "groups": [6,5,4,8,8,2,2,6] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["cc","ac"] test_input = { "n": 8, "words": ["dab","ecedc","badca","cedacb","bdeab","bccedc","bebc","aeade"], "groups": [3,7,6,8,6,6,7,5] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["aeade"]
1,697,293,800
biweekly-contest-115-count-of-sub-multisets-with-bounded-sum
https://leetcode.com/problems/count-of-sub-multisets-with-bounded-sum
count-of-sub-multisets-with-bounded-sum
{ "questionId": "3091", "questionFrontendId": "2902", "title": "Count of Sub-Multisets With Bounded Sum", "titleSlug": "count-of-sub-multisets-with-bounded-sum", "isPaidOnly": false, "difficulty": "Hard", "likes": 125, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的非负整数数组 nums 和两个整数 l 和 r 。 请你返回 nums 中子多重集合的和在闭区间 [l, r] 之间的 子多重集合的数目 。 由于答案可能很大,请你将答案对 109 + 7 取余后返回。 子多重集合 指的是从数组中选出一些元素构成的 无序 集合,每个元素 x 出现的次数可以是 0, 1, ..., occ[x] 次,其中 occ[x] 是元素 x 在数组中的出现次数。 注意: * 如果两个子多重集合中的元素排序后一模一样,那么它们两个是相同的 子多重集合 。 * 空 集合的和是 0 。 示例 1: 输入:nums = [1,2,2,3], l = 6, r = 6 输出:1 解释:唯一和为 6 的子集合是 {1, 2, 3} 。 示例 2: 输入:nums = [2,1,4,2,7], l = 1, r = 5 输出:7 解释:和在闭区间 [1, 5] 之间的子多重集合为 {1} ,{2} ,{4} ,{2, 2} ,{1, 2} ,{1, 4} 和 {1, 2, 2} 。 示例 3: 输入:nums = [1,2,1,3,5,2], l = 3, r = 5 输出:9 解释:和在闭区间 [3, 5] 之间的子多重集合为 {3} ,{5} ,{1, 2} ,{1, 3} ,{2, 2} ,{2, 3} ,{1, 1, 2} ,{1, 1, 3} 和 {1, 2, 2} 。 提示: * 1 <= nums.length <= 2 * 104 * 0 <= nums[i] <= 2 * 104 * nums 的和不超过 2 * 104 。 * 0 <= l <= r <= 2 * 104 """ class Solution: def countSubMultisets(self, nums: List[int], l: int, r: int) -> int:
给你一个下标从 0 开始的非负整数数组 nums 和两个整数 l 和 r 。 请你返回 nums 中子多重集合的和在闭区间 [l, r] 之间的 子多重集合的数目 。 由于答案可能很大,请你将答案对 109 + 7 取余后返回。 子多重集合 指的是从数组中选出一些元素构成的 无序 集合,每个元素 x 出现的次数可以是 0, 1, ..., occ[x] 次,其中 occ[x] 是元素 x 在数组中的出现次数。 注意: * 如果两个子多重集合中的元素排序后一模一样,那么它们两个是相同的 子多重集合 。 * 空 集合的和是 0 。 示例 1: 输入:nums = [1,2,2,3], l = 6, r = 6 输出:1 解释:唯一和为 6 的子集合是 {1, 2, 3} 。 示例 2: 输入:nums = [2,1,4,2,7], l = 1, r = 5 输出:7 解释:和在闭区间 [1, 5] 之间的子多重集合为 {1} ,{2} ,{4} ,{2, 2} ,{1, 2} ,{1, 4} 和 {1, 2, 2} 。 示例 3: 输入:nums = [1,2,1,3,5,2], l = 3, r = 5 输出:9 解释:和在闭区间 [3, 5] 之间的子多重集合为 {3} ,{5} ,{1, 2} ,{1, 3} ,{2, 2} ,{2, 3} ,{1, 1, 2} ,{1, 1, 3} 和 {1, 2, 2} 。 提示: * 1 <= nums.length <= 2 * 104 * 0 <= nums[i] <= 2 * 104 * nums 的和不超过 2 * 104 。 * 0 <= l <= r <= 2 * 104 请完成下面的代码来解决上述问题: ```python class Solution: def countSubMultisets(self, nums: List[int], l: int, r: int) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,2,3], "l": 6, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,1,4,2,7], "l": 1, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 7 test_input = { "nums": [1,2,1,3,5,2], "l": 3, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 9 test_input = { "nums": [0,0,1,2,3], "l": 2, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 9 test_input = { "nums": [0,0,0,0,0], "l": 0, "r": 0 } assert my_solution.countSubMultisets(**test_input) == 6 test_input = { "nums": [0,0,0,1,2,5,2,3], "l": 0, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 20 test_input = { "nums": [1,1], "l": 2, "r": 2 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,1,1], "l": 2, "r": 2 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,1,2], "l": 2, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [1,2,1], "l": 2, "r": 2 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,2,2], "l": 3, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [2,1,1], "l": 1, "r": 2 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [2,1,2], "l": 2, "r": 2 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,2,1], "l": 4, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,2,2], "l": 3, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,1,1,1], "l": 3, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,1,1,2], "l": 1, "r": 1 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,1,1,3], "l": 4, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,1,2,1], "l": 3, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [1,1,2,2], "l": 2, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [1,1,2,3], "l": 4, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [1,1,3,1], "l": 2, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [1,1,3,2], "l": 6, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,1,3,3], "l": 8, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,2,1,1], "l": 3, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,2,1,2], "l": 4, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [1,2,1,3], "l": 4, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 6 test_input = { "nums": [1,2,2,1], "l": 4, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [1,2,2,2], "l": 1, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 7 test_input = { "nums": [1,2,2,3], "l": 3, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 9 test_input = { "nums": [1,2,3,1], "l": 1, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 7 test_input = { "nums": [1,2,3,2], "l": 4, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 7 test_input = { "nums": [1,2,3,3], "l": 6, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,3,1,1], "l": 6, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,3,1,2], "l": 7, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,3,1,3], "l": 5, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,3,2,1], "l": 2, "r": 2 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,3,2,2], "l": 7, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,3,2,3], "l": 1, "r": 9 } assert my_solution.countSubMultisets(**test_input) == 11 test_input = { "nums": [1,3,3,1], "l": 7, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,3,3,2], "l": 1, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 8 test_input = { "nums": [1,3,3,3], "l": 9, "r": 10 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,1,1,1], "l": 2, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [2,1,1,2], "l": 1, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 6 test_input = { "nums": [2,1,1,3], "l": 7, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,1,2,1], "l": 3, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,1,2,2], "l": 6, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,1,2,3], "l": 7, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,1,3,1], "l": 4, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,1,3,2], "l": 3, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [2,1,3,3], "l": 1, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 6 test_input = { "nums": [2,2,1,1], "l": 6, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,2,1,2], "l": 2, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [2,2,1,3], "l": 6, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [2,2,2,1], "l": 4, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [2,2,2,2], "l": 3, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,2,2,3], "l": 5, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,2,3,1], "l": 7, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,2,3,2], "l": 5, "r": 9 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [2,2,3,3], "l": 4, "r": 10 } assert my_solution.countSubMultisets(**test_input) == 6 test_input = { "nums": [2,3,1,1], "l": 4, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 5 test_input = { "nums": [2,3,1,2], "l": 3, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [2,3,1,3], "l": 2, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 9 test_input = { "nums": [2,3,2,1], "l": 3, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,3,2,2], "l": 3, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,3,2,3], "l": 5, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [2,3,3,1], "l": 9, "r": 9 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,3,3,2], "l": 8, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,3,3,3], "l": 3, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [3,1,1,1], "l": 4, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [3,1,1,2], "l": 7, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [3,1,1,3], "l": 1, "r": 1 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [3,1,2,1], "l": 4, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [3,1,2,2], "l": 3, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 9 test_input = { "nums": [3,1,2,3], "l": 5, "r": 9 } assert my_solution.countSubMultisets(**test_input) == 6 test_input = { "nums": [3,1,3,1], "l": 1, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [3,1,3,2], "l": 3, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [3,1,3,3], "l": 9, "r": 10 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [3,2,1,1], "l": 1, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 9 test_input = { "nums": [3,2,1,2], "l": 1, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 9 test_input = { "nums": [3,2,1,3], "l": 5, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [3,2,2,1], "l": 4, "r": 4 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [3,2,2,2], "l": 6, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [3,2,2,3], "l": 2, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 6 test_input = { "nums": [3,2,3,1], "l": 2, "r": 3 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [3,2,3,2], "l": 8, "r": 10 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [3,2,3,3], "l": 1, "r": 11 } assert my_solution.countSubMultisets(**test_input) == 7 test_input = { "nums": [3,3,1,1], "l": 8, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [3,3,1,2], "l": 2, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 5 test_input = { "nums": [3,3,1,3], "l": 1, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 5 test_input = { "nums": [3,3,2,1], "l": 4, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [3,3,2,2], "l": 7, "r": 10 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [3,3,2,3], "l": 4, "r": 10 } assert my_solution.countSubMultisets(**test_input) == 4 test_input = { "nums": [3,3,3,1], "l": 6, "r": 9 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [3,3,3,2], "l": 11, "r": 11 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [3,3,3,3], "l": 8, "r": 9 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [1,1,1,1,1], "l": 3, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 3 test_input = { "nums": [1,1,1,1,2], "l": 2, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 7 test_input = { "nums": [1,1,1,1,3], "l": 6, "r": 7 } assert my_solution.countSubMultisets(**test_input) == 2 test_input = { "nums": [1,1,1,1,4], "l": 8, "r": 8 } assert my_solution.countSubMultisets(**test_input) == 1
1,697,293,800
weekly-contest-366-divisible-and-non-divisible-sums-difference
https://leetcode.com/problems/divisible-and-non-divisible-sums-difference
divisible-and-non-divisible-sums-difference
{ "questionId": "3172", "questionFrontendId": "2894", "title": "Divisible and Non-divisible Sums Difference", "titleSlug": "divisible-and-non-divisible-sums-difference", "isPaidOnly": false, "difficulty": "Easy", "likes": 115, "dislikes": 10, "categoryTitle": "Algorithms" }
""" 给你两个正整数 n 和 m 。 现定义两个整数 num1 和 num2 ,如下所示: * num1:范围 [1, n] 内所有 无法被 m 整除 的整数之和。 * num2:范围 [1, n] 内所有 能够被 m 整除 的整数之和。 返回整数 num1 - num2 。 示例 1: 输入:n = 10, m = 3 输出:19 解释:在这个示例中: - 范围 [1, 10] 内无法被 3 整除的整数为 [1,2,4,5,7,8,10] ,num1 = 这些整数之和 = 37 。 - 范围 [1, 10] 内能够被 3 整除的整数为 [3,6,9] ,num2 = 这些整数之和 = 18 。 返回 37 - 18 = 19 作为答案。 示例 2: 输入:n = 5, m = 6 输出:15 解释:在这个示例中: - 范围 [1, 5] 内无法被 6 整除的整数为 [1,2,3,4,5] ,num1 = 这些整数之和 = 15 。 - 范围 [1, 5] 内能够被 6 整除的整数为 [] ,num2 = 这些整数之和 = 0 。 返回 15 - 0 = 15 作为答案。 示例 3: 输入:n = 5, m = 1 输出:-15 解释:在这个示例中: - 范围 [1, 5] 内无法被 1 整除的整数为 [] ,num1 = 这些整数之和 = 0 。 - 范围 [1, 5] 内能够被 1 整除的整数为 [1,2,3,4,5] ,num2 = 这些整数之和 = 15 。 返回 0 - 15 = -15 作为答案。 提示: * 1 <= n, m <= 1000 """ class Solution: def differenceOfSums(self, n: int, m: int) -> int:
给你两个正整数 n 和 m 。 现定义两个整数 num1 和 num2 ,如下所示: * num1:范围 [1, n] 内所有 无法被 m 整除 的整数之和。 * num2:范围 [1, n] 内所有 能够被 m 整除 的整数之和。 返回整数 num1 - num2 。 示例 1: 输入:n = 10, m = 3 输出:19 解释:在这个示例中: - 范围 [1, 10] 内无法被 3 整除的整数为 [1,2,4,5,7,8,10] ,num1 = 这些整数之和 = 37 。 - 范围 [1, 10] 内能够被 3 整除的整数为 [3,6,9] ,num2 = 这些整数之和 = 18 。 返回 37 - 18 = 19 作为答案。 示例 2: 输入:n = 5, m = 6 输出:15 解释:在这个示例中: - 范围 [1, 5] 内无法被 6 整除的整数为 [1,2,3,4,5] ,num1 = 这些整数之和 = 15 。 - 范围 [1, 5] 内能够被 6 整除的整数为 [] ,num2 = 这些整数之和 = 0 。 返回 15 - 0 = 15 作为答案。 示例 3: 输入:n = 5, m = 1 输出:-15 解释:在这个示例中: - 范围 [1, 5] 内无法被 1 整除的整数为 [] ,num1 = 这些整数之和 = 0 。 - 范围 [1, 5] 内能够被 1 整除的整数为 [1,2,3,4,5] ,num2 = 这些整数之和 = 15 。 返回 0 - 15 = -15 作为答案。 提示: * 1 <= n, m <= 1000 请完成下面的代码来解决上述问题: ```python class Solution: def differenceOfSums(self, n: int, m: int) -> int: ```
my_solution = Solution() test_input = { "n": 10, "m": 3 } assert my_solution.differenceOfSums(**test_input) == 19 test_input = { "n": 5, "m": 6 } assert my_solution.differenceOfSums(**test_input) == 15 test_input = { "n": 5, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -15 test_input = { "n": 15, "m": 9 } assert my_solution.differenceOfSums(**test_input) == 102 test_input = { "n": 8, "m": 10 } assert my_solution.differenceOfSums(**test_input) == 36 test_input = { "n": 23, "m": 36 } assert my_solution.differenceOfSums(**test_input) == 276 test_input = { "n": 1, "m": 32 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 36, "m": 7 } assert my_solution.differenceOfSums(**test_input) == 456 test_input = { "n": 3, "m": 8 } assert my_solution.differenceOfSums(**test_input) == 6 test_input = { "n": 4, "m": 2 } assert my_solution.differenceOfSums(**test_input) == -2 test_input = { "n": 9, "m": 7 } assert my_solution.differenceOfSums(**test_input) == 31 test_input = { "n": 20, "m": 9 } assert my_solution.differenceOfSums(**test_input) == 156 test_input = { "n": 3, "m": 19 } assert my_solution.differenceOfSums(**test_input) == 6 test_input = { "n": 6, "m": 16 } assert my_solution.differenceOfSums(**test_input) == 21 test_input = { "n": 6, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -21 test_input = { "n": 5, "m": 25 } assert my_solution.differenceOfSums(**test_input) == 15 test_input = { "n": 9, "m": 3 } assert my_solution.differenceOfSums(**test_input) == 9 test_input = { "n": 8, "m": 23 } assert my_solution.differenceOfSums(**test_input) == 36 test_input = { "n": 17, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -153 test_input = { "n": 18, "m": 9 } assert my_solution.differenceOfSums(**test_input) == 117 test_input = { "n": 22, "m": 30 } assert my_solution.differenceOfSums(**test_input) == 253 test_input = { "n": 1, "m": 42 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 33, "m": 19 } assert my_solution.differenceOfSums(**test_input) == 523 test_input = { "n": 7, "m": 19 } assert my_solution.differenceOfSums(**test_input) == 28 test_input = { "n": 12, "m": 24 } assert my_solution.differenceOfSums(**test_input) == 78 test_input = { "n": 26, "m": 25 } assert my_solution.differenceOfSums(**test_input) == 301 test_input = { "n": 9, "m": 16 } assert my_solution.differenceOfSums(**test_input) == 45 test_input = { "n": 1, "m": 8 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 29, "m": 42 } assert my_solution.differenceOfSums(**test_input) == 435 test_input = { "n": 2, "m": 11 } assert my_solution.differenceOfSums(**test_input) == 3 test_input = { "n": 36, "m": 10 } assert my_solution.differenceOfSums(**test_input) == 546 test_input = { "n": 45, "m": 4 } assert my_solution.differenceOfSums(**test_input) == 507 test_input = { "n": 3, "m": 7 } assert my_solution.differenceOfSums(**test_input) == 6 test_input = { "n": 6, "m": 12 } assert my_solution.differenceOfSums(**test_input) == 21 test_input = { "n": 3, "m": 4 } assert my_solution.differenceOfSums(**test_input) == 6 test_input = { "n": 8, "m": 28 } assert my_solution.differenceOfSums(**test_input) == 36 test_input = { "n": 18, "m": 23 } assert my_solution.differenceOfSums(**test_input) == 171 test_input = { "n": 11, "m": 6 } assert my_solution.differenceOfSums(**test_input) == 54 test_input = { "n": 35, "m": 10 } assert my_solution.differenceOfSums(**test_input) == 510 test_input = { "n": 29, "m": 18 } assert my_solution.differenceOfSums(**test_input) == 399 test_input = { "n": 1, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -1 test_input = { "n": 12, "m": 8 } assert my_solution.differenceOfSums(**test_input) == 62 test_input = { "n": 7, "m": 12 } assert my_solution.differenceOfSums(**test_input) == 28 test_input = { "n": 17, "m": 3 } assert my_solution.differenceOfSums(**test_input) == 63 test_input = { "n": 16, "m": 15 } assert my_solution.differenceOfSums(**test_input) == 106 test_input = { "n": 18, "m": 3 } assert my_solution.differenceOfSums(**test_input) == 45 test_input = { "n": 4, "m": 12 } assert my_solution.differenceOfSums(**test_input) == 10 test_input = { "n": 3, "m": 21 } assert my_solution.differenceOfSums(**test_input) == 6 test_input = { "n": 15, "m": 4 } assert my_solution.differenceOfSums(**test_input) == 72 test_input = { "n": 9, "m": 39 } assert my_solution.differenceOfSums(**test_input) == 45 test_input = { "n": 19, "m": 18 } assert my_solution.differenceOfSums(**test_input) == 154 test_input = { "n": 2, "m": 4 } assert my_solution.differenceOfSums(**test_input) == 3 test_input = { "n": 41, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -861 test_input = { "n": 3, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -6 test_input = { "n": 16, "m": 13 } assert my_solution.differenceOfSums(**test_input) == 110 test_input = { "n": 32, "m": 10 } assert my_solution.differenceOfSums(**test_input) == 408 test_input = { "n": 41, "m": 34 } assert my_solution.differenceOfSums(**test_input) == 793 test_input = { "n": 33, "m": 40 } assert my_solution.differenceOfSums(**test_input) == 561 test_input = { "n": 36, "m": 8 } assert my_solution.differenceOfSums(**test_input) == 506 test_input = { "n": 8, "m": 34 } assert my_solution.differenceOfSums(**test_input) == 36 test_input = { "n": 40, "m": 12 } assert my_solution.differenceOfSums(**test_input) == 676 test_input = { "n": 28, "m": 9 } assert my_solution.differenceOfSums(**test_input) == 298 test_input = { "n": 20, "m": 6 } assert my_solution.differenceOfSums(**test_input) == 138 test_input = { "n": 13, "m": 6 } assert my_solution.differenceOfSums(**test_input) == 55 test_input = { "n": 2, "m": 37 } assert my_solution.differenceOfSums(**test_input) == 3 test_input = { "n": 14, "m": 17 } assert my_solution.differenceOfSums(**test_input) == 105 test_input = { "n": 35, "m": 4 } assert my_solution.differenceOfSums(**test_input) == 342 test_input = { "n": 2, "m": 14 } assert my_solution.differenceOfSums(**test_input) == 3 test_input = { "n": 5, "m": 2 } assert my_solution.differenceOfSums(**test_input) == 3 test_input = { "n": 7, "m": 7 } assert my_solution.differenceOfSums(**test_input) == 14 test_input = { "n": 12, "m": 26 } assert my_solution.differenceOfSums(**test_input) == 78 test_input = { "n": 14, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -105 test_input = { "n": 2, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -3 test_input = { "n": 20, "m": 3 } assert my_solution.differenceOfSums(**test_input) == 84 test_input = { "n": 8, "m": 27 } assert my_solution.differenceOfSums(**test_input) == 36 test_input = { "n": 1, "m": 12 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 13, "m": 19 } assert my_solution.differenceOfSums(**test_input) == 91 test_input = { "n": 7, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -28 test_input = { "n": 31, "m": 4 } assert my_solution.differenceOfSums(**test_input) == 272 test_input = { "n": 11, "m": 25 } assert my_solution.differenceOfSums(**test_input) == 66 test_input = { "n": 5, "m": 19 } assert my_solution.differenceOfSums(**test_input) == 15 test_input = { "n": 33, "m": 12 } assert my_solution.differenceOfSums(**test_input) == 489 test_input = { "n": 4, "m": 26 } assert my_solution.differenceOfSums(**test_input) == 10 test_input = { "n": 1, "m": 24 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 13, "m": 20 } assert my_solution.differenceOfSums(**test_input) == 91 test_input = { "n": 6, "m": 8 } assert my_solution.differenceOfSums(**test_input) == 21 test_input = { "n": 8, "m": 26 } assert my_solution.differenceOfSums(**test_input) == 36 test_input = { "n": 4, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -10 test_input = { "n": 1, "m": 6 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 2, "m": 25 } assert my_solution.differenceOfSums(**test_input) == 3 test_input = { "n": 4, "m": 4 } assert my_solution.differenceOfSums(**test_input) == 2 test_input = { "n": 8, "m": 2 } assert my_solution.differenceOfSums(**test_input) == -4 test_input = { "n": 15, "m": 21 } assert my_solution.differenceOfSums(**test_input) == 120 test_input = { "n": 1, "m": 2 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 12, "m": 2 } assert my_solution.differenceOfSums(**test_input) == -6 test_input = { "n": 40, "m": 14 } assert my_solution.differenceOfSums(**test_input) == 736 test_input = { "n": 14, "m": 19 } assert my_solution.differenceOfSums(**test_input) == 105 test_input = { "n": 18, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -171 test_input = { "n": 1, "m": 28 } assert my_solution.differenceOfSums(**test_input) == 1 test_input = { "n": 31, "m": 18 } assert my_solution.differenceOfSums(**test_input) == 460
1,696,732,200
weekly-contest-366-minimum-processing-time
https://leetcode.com/problems/minimum-processing-time
minimum-processing-time
{ "questionId": "3151", "questionFrontendId": "2895", "title": "Minimum Processing Time", "titleSlug": "minimum-processing-time", "isPaidOnly": false, "difficulty": "Medium", "likes": 119, "dislikes": 22, "categoryTitle": "Algorithms" }
""" 你有 n 颗处理器,每颗处理器都有 4 个核心。现有 n * 4 个待执行任务,每个核心只执行 一个 任务。 给你一个下标从 0 开始的整数数组 processorTime ,表示每颗处理器最早空闲时间。另给你一个下标从 0 开始的整数数组 tasks ,表示执行每个任务所需的时间。返回所有任务都执行完毕需要的 最小时间 。 注意:每个核心独立执行任务。 示例 1: 输入:processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5] 输出:16 解释: 最优的方案是将下标为 4, 5, 6, 7 的任务分配给第一颗处理器(最早空闲时间 time = 8),下标为 0, 1, 2, 3 的任务分配给第二颗处理器(最早空闲时间 time = 10)。 第一颗处理器执行完所有任务需要花费的时间 = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16 。 第二颗处理器执行完所有任务需要花费的时间 = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13 。 因此,可以证明执行完所有任务需要花费的最小时间是 16 。 示例 2: 输入:processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3] 输出:23 解释: 最优的方案是将下标为 1, 4, 5, 6 的任务分配给第一颗处理器(最早空闲时间 time = 10),下标为 0, 2, 3, 7 的任务分配给第二颗处理器(最早空闲时间 time = 20)。 第一颗处理器执行完所有任务需要花费的时间 = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18 。 第二颗处理器执行完所有任务需要花费的时间 = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23 。 因此,可以证明执行完所有任务需要花费的最小时间是 23 。 提示: * 1 <= n == processorTime.length <= 25000 * 1 <= tasks.length <= 105 * 0 <= processorTime[i] <= 109 * 1 <= tasks[i] <= 109 * tasks.length == 4 * n """ class Solution: def minProcessingTime(self, processorTime: List[int], tasks: List[int]) -> int:
你有 n 颗处理器,每颗处理器都有 4 个核心。现有 n * 4 个待执行任务,每个核心只执行 一个 任务。 给你一个下标从 0 开始的整数数组 processorTime ,表示每颗处理器最早空闲时间。另给你一个下标从 0 开始的整数数组 tasks ,表示执行每个任务所需的时间。返回所有任务都执行完毕需要的 最小时间 。 注意:每个核心独立执行任务。 示例 1: 输入:processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5] 输出:16 解释: 最优的方案是将下标为 4, 5, 6, 7 的任务分配给第一颗处理器(最早空闲时间 time = 8),下标为 0, 1, 2, 3 的任务分配给第二颗处理器(最早空闲时间 time = 10)。 第一颗处理器执行完所有任务需要花费的时间 = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16 。 第二颗处理器执行完所有任务需要花费的时间 = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13 。 因此,可以证明执行完所有任务需要花费的最小时间是 16 。 示例 2: 输入:processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3] 输出:23 解释: 最优的方案是将下标为 1, 4, 5, 6 的任务分配给第一颗处理器(最早空闲时间 time = 10),下标为 0, 2, 3, 7 的任务分配给第二颗处理器(最早空闲时间 time = 20)。 第一颗处理器执行完所有任务需要花费的时间 = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18 。 第二颗处理器执行完所有任务需要花费的时间 = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23 。 因此,可以证明执行完所有任务需要花费的最小时间是 23 。 提示: * 1 <= n == processorTime.length <= 25000 * 1 <= tasks.length <= 105 * 0 <= processorTime[i] <= 109 * 1 <= tasks[i] <= 109 * tasks.length == 4 * n 请完成下面的代码来解决上述问题: ```python class Solution: def minProcessingTime(self, processorTime: List[int], tasks: List[int]) -> int: ```
my_solution = Solution() test_input = { "processorTime": [8,10], "tasks": [2,2,3,1,8,7,4,5] } assert my_solution.minProcessingTime(**test_input) == 16 test_input = { "processorTime": [10,20], "tasks": [2,3,1,2,5,8,4,3] } assert my_solution.minProcessingTime(**test_input) == 23 test_input = { "processorTime": [121,99], "tasks": [287,315,293,260,333,362,69,233] } assert my_solution.minProcessingTime(**test_input) == 461 test_input = { "processorTime": [33,320], "tasks": [132,68,232,166,30,300,112,138] } assert my_solution.minProcessingTime(**test_input) == 452 test_input = { "processorTime": [50,82], "tasks": [288,138,205,295,367,100,258,308] } assert my_solution.minProcessingTime(**test_input) == 417 test_input = { "processorTime": [291], "tasks": [125,169,269,32] } assert my_solution.minProcessingTime(**test_input) == 560 test_input = { "processorTime": [55,350,166,210,389], "tasks": [276,253,157,237,92,396,331,19,82,301,136,396,251,92,280,70,253,47,81,84] } assert my_solution.minProcessingTime(**test_input) == 470 test_input = { "processorTime": [143,228,349,231,392], "tasks": [102,365,363,211,38,96,98,79,365,289,252,201,259,346,21,68,128,56,167,183] } assert my_solution.minProcessingTime(**test_input) == 517 test_input = { "processorTime": [168,32,299,303,96], "tasks": [382,183,337,73,115,350,6,18,93,238,102,302,96,381,327,385,387,288,138,83] } assert my_solution.minProcessingTime(**test_input) == 456 test_input = { "processorTime": [324,117,374,219,303], "tasks": [374,202,328,11,353,208,383,287,107,236,226,387,21,183,352,164,207,182,15,65] } assert my_solution.minProcessingTime(**test_input) == 571 test_input = { "processorTime": [376], "tasks": [21,247,274,38] } assert my_solution.minProcessingTime(**test_input) == 650 test_input = { "processorTime": [93,3,281,218], "tasks": [182,16,241,312,81,339,207,330,306,166,82,290,7,317,396,389] } assert my_solution.minProcessingTime(**test_input) == 459 test_input = { "processorTime": [374,250,197,170], "tasks": [247,56,330,361,240,261,67,65,138,181,308,26,59,150,137,244] } assert my_solution.minProcessingTime(**test_input) == 531 test_input = { "processorTime": [115,271,137], "tasks": [34,72,328,312,159,32,283,6,234,280,46,349] } assert my_solution.minProcessingTime(**test_input) == 464 test_input = { "processorTime": [47,217,349,233,283], "tasks": [195,188,181,259,145,96,298,322,213,154,278,292,315,191,177,228,291,204,310,266] } assert my_solution.minProcessingTime(**test_input) == 526 test_input = { "processorTime": [177,6,326,318,294], "tasks": [136,215,260,259,35,248,340,377,144,248,83,150,63,48,269,197,317,135,36,344] } assert my_solution.minProcessingTime(**test_input) == 542 test_input = { "processorTime": [266,372], "tasks": [260,325,159,316,296,366,335,146] } assert my_solution.minProcessingTime(**test_input) == 668 test_input = { "processorTime": [63,339], "tasks": [79,316,98,354,220,267,333,11] } assert my_solution.minProcessingTime(**test_input) == 559 test_input = { "processorTime": [149,60,172,5,212], "tasks": [230,374,276,281,55,96,52,83,56,399,69,333,145,6,50,101,216,327,120,209] } assert my_solution.minProcessingTime(**test_input) == 404 test_input = { "processorTime": [220,375,285,267,150], "tasks": [53,317,367,258,337,280,232,322,153,169,121,211,171,345,76,370,265,107,45,320] } assert my_solution.minProcessingTime(**test_input) == 542 test_input = { "processorTime": [373,367,267], "tasks": [214,221,78,330,340,309,330,338,396,337,285,207] } assert my_solution.minProcessingTime(**test_input) == 697 test_input = { "processorTime": [92,20], "tasks": [11,354,399,11,20,9,217,372] } assert my_solution.minProcessingTime(**test_input) == 419 test_input = { "processorTime": [51], "tasks": [349,186,191,183] } assert my_solution.minProcessingTime(**test_input) == 400 test_input = { "processorTime": [362,220,10,219], "tasks": [160,369,385,145,122,124,147,231,162,37,293,160,68,232,232,130] } assert my_solution.minProcessingTime(**test_input) == 486 test_input = { "processorTime": [210,348,3,57,174], "tasks": [328,296,222,161,190,381,283,137,353,227,284,134,170,13,275,113,148,198,33,260] } assert my_solution.minProcessingTime(**test_input) == 482 test_input = { "processorTime": [153], "tasks": [342,166,234,175] } assert my_solution.minProcessingTime(**test_input) == 495 test_input = { "processorTime": [23,204,114,380,3], "tasks": [40,105,311,221,247,34,399,190,23,289,16,129,68,12,32,364,364,111,361,49] } assert my_solution.minProcessingTime(**test_input) == 412 test_input = { "processorTime": [167,110,221,19,211], "tasks": [140,351,307,4,262,228,161,200,108,206,280,266,240,258,396,194,333,328,121,179] } assert my_solution.minProcessingTime(**test_input) == 425 test_input = { "processorTime": [179,127,280,242], "tasks": [244,243,92,188,134,84,22,258,100,77,237,83,41,396,218,87] } assert my_solution.minProcessingTime(**test_input) == 523 test_input = { "processorTime": [45,249,396,163], "tasks": [131,365,52,366,229,340,242,371,20,181,103,97,141,106,46,119] } assert my_solution.minProcessingTime(**test_input) == 493 test_input = { "processorTime": [205], "tasks": [117,63,174,87] } assert my_solution.minProcessingTime(**test_input) == 379 test_input = { "processorTime": [128,101,302,53], "tasks": [265,96,358,287,148,117,331,47,173,347,62,145,73,47,206,29] } assert my_solution.minProcessingTime(**test_input) == 411 test_input = { "processorTime": [228], "tasks": [321,378,268,351] } assert my_solution.minProcessingTime(**test_input) == 606 test_input = { "processorTime": [136,22,229,187], "tasks": [246,326,188,341,375,207,334,1,189,301,62,39,44,239,346,376] } assert my_solution.minProcessingTime(**test_input) == 470 test_input = { "processorTime": [47,238,274], "tasks": [251,312,87,111,142,62,112,325,305,164,85,338] } assert my_solution.minProcessingTime(**test_input) == 489 test_input = { "processorTime": [386,121,249], "tasks": [5,376,53,187,287,369,400,178,293,121,164,336] } assert my_solution.minProcessingTime(**test_input) == 550 test_input = { "processorTime": [82,152], "tasks": [82,46,149,255,225,93,227,131] } assert my_solution.minProcessingTime(**test_input) == 337 test_input = { "processorTime": [394,6], "tasks": [231,236,288,35,247,204,141,41] } assert my_solution.minProcessingTime(**test_input) == 598 test_input = { "processorTime": [389,54], "tasks": [353,358,211,133,225,358,19,310] } assert my_solution.minProcessingTime(**test_input) == 614 test_input = { "processorTime": [106,291,291,301], "tasks": [192,120,93,5,293,147,299,81,334,137,259,48,296,117,379,182] } assert my_solution.minProcessingTime(**test_input) == 584 test_input = { "processorTime": [320,139], "tasks": [210,255,304,181,216,255,375,360] } assert my_solution.minProcessingTime(**test_input) == 575 test_input = { "processorTime": [306,207,295], "tasks": [335,188,355,209,201,113,122,206,46,355,350,38] } assert my_solution.minProcessingTime(**test_input) == 562 test_input = { "processorTime": [175,111], "tasks": [225,110,163,100,353,77,12,124] } assert my_solution.minProcessingTime(**test_input) == 464 test_input = { "processorTime": [233,201], "tasks": [1,389,291,333,42,399,399,300] } assert my_solution.minProcessingTime(**test_input) == 600 test_input = { "processorTime": [302,5,102,195], "tasks": [311,144,7,277,253,96,136,251,81,195,171,140,73,2,84,42] } assert my_solution.minProcessingTime(**test_input) == 375 test_input = { "processorTime": [151,185,3,137], "tasks": [294,230,221,216,299,24,79,194,375,387,77,388,366,51,117,126] } assert my_solution.minProcessingTime(**test_input) == 436 test_input = { "processorTime": [39,141,145,199], "tasks": [99,257,161,121,56,80,235,168,171,228,290,180,118,307,66,151] } assert my_solution.minProcessingTime(**test_input) == 369 test_input = { "processorTime": [40,67], "tasks": [259,217,337,295,126,335,369,123] } assert my_solution.minProcessingTime(**test_input) == 409 test_input = { "processorTime": [310,56,207,396], "tasks": [260,255,30,243,66,11,285,31,358,219,218,90,176,346,134,74] } assert my_solution.minProcessingTime(**test_input) == 486 test_input = { "processorTime": [174,48], "tasks": [106,323,4,247,70,281,348,12] } assert my_solution.minProcessingTime(**test_input) == 396 test_input = { "processorTime": [16,52,13], "tasks": [281,261,55,165,317,150,68,26,52,227,176,399] } assert my_solution.minProcessingTime(**test_input) == 412 test_input = { "processorTime": [8,315,115,123], "tasks": [134,371,160,138,289,330,48,349,376,88,46,235,298,321,343,365] } assert my_solution.minProcessingTime(**test_input) == 458 test_input = { "processorTime": [221,24,372,6,50], "tasks": [274,79,78,37,57,39,102,272,242,283,95,155,105,363,174,1,333,400,375,376] } assert my_solution.minProcessingTime(**test_input) == 429 test_input = { "processorTime": [289,98,7,303,219], "tasks": [363,140,173,92,52,348,350,316,281,327,40,259,39,235,263,244,42,354,11,232] } assert my_solution.minProcessingTime(**test_input) == 478 test_input = { "processorTime": [348,268], "tasks": [104,397,333,188,373,325,57,202] } assert my_solution.minProcessingTime(**test_input) == 665 test_input = { "processorTime": [18,377,305,188,311], "tasks": [207,365,369,66,263,47,257,317,221,292,386,308,357,105,99,314,133,106,311,90] } assert my_solution.minProcessingTime(**test_input) == 597 test_input = { "processorTime": [44,254], "tasks": [277,361,398,276,84,105,350,134] } assert my_solution.minProcessingTime(**test_input) == 530 test_input = { "processorTime": [270,257,58], "tasks": [212,151,50,78,91,110,399,360,108,192,142,115] } assert my_solution.minProcessingTime(**test_input) == 457 test_input = { "processorTime": [108,301], "tasks": [150,143,119,160,340,139,72,349] } assert my_solution.minProcessingTime(**test_input) == 457 test_input = { "processorTime": [231,207,162,49], "tasks": [318,289,351,103,19,77,65,116,94,234,139,246,80,184,286,397] } assert my_solution.minProcessingTime(**test_input) == 448 test_input = { "processorTime": [252], "tasks": [384,281,207,33] } assert my_solution.minProcessingTime(**test_input) == 636 test_input = { "processorTime": [199,8,129,204], "tasks": [308,133,366,272,373,343,357,159,378,149,185,248,190,1,142,199] } assert my_solution.minProcessingTime(**test_input) == 472 test_input = { "processorTime": [135,65,19,225], "tasks": [183,135,138,142,282,141,349,236,57,333,258,353,152,396,152,191] } assert my_solution.minProcessingTime(**test_input) == 415 test_input = { "processorTime": [199,371,283,70], "tasks": [244,7,226,230,331,232,332,288,151,360,26,87,49,188,269,375] } assert my_solution.minProcessingTime(**test_input) == 513 test_input = { "processorTime": [184,378], "tasks": [105,239,221,343,276,359,86,84] } assert my_solution.minProcessingTime(**test_input) == 599 test_input = { "processorTime": [297,229,142,8,47], "tasks": [373,256,210,92,304,134,20,246,116,139,376,139,10,210,192,43,282,278,322,167] } assert my_solution.minProcessingTime(**test_input) == 389 test_input = { "processorTime": [224,358,58,352], "tasks": [177,274,306,295,142,353,44,111,325,328,394,168,300,15,252,389] } assert my_solution.minProcessingTime(**test_input) == 626 test_input = { "processorTime": [318,321,264,259], "tasks": [316,284,127,227,269,332,317,364,220,130,330,155,45,205,369,42] } assert my_solution.minProcessingTime(**test_input) == 628 test_input = { "processorTime": [295,214,130], "tasks": [316,395,280,122,27,224,40,210,99,366,55,183] } assert my_solution.minProcessingTime(**test_input) == 525 test_input = { "processorTime": [81,38,313,121], "tasks": [158,304,127,214,34,298,95,188,56,391,317,99,304,101,266,302] } assert my_solution.minProcessingTime(**test_input) == 429 test_input = { "processorTime": [8,400,28,348,193], "tasks": [72,391,149,264,370,183,365,102,201,348,341,176,338,186,97,156,47,125,61,202] } assert my_solution.minProcessingTime(**test_input) == 504 test_input = { "processorTime": [0], "tasks": [8,369,353,14] } assert my_solution.minProcessingTime(**test_input) == 369 test_input = { "processorTime": [55,364,28,246], "tasks": [396,357,37,400,239,327,5,387,70,389,323,213,322,111,179,19] } assert my_solution.minProcessingTime(**test_input) == 485 test_input = { "processorTime": [288,219,356,146,282], "tasks": [390,46,24,391,222,241,281,33,400,312,290,11,147,282,204,214,22,178,77,156] } assert my_solution.minProcessingTime(**test_input) == 546 test_input = { "processorTime": [60,309,40,219,294], "tasks": [267,94,238,338,279,48,164,371,302,110,247,392,83,107,389,46,92,273,131,136] } assert my_solution.minProcessingTime(**test_input) == 466 test_input = { "processorTime": [357], "tasks": [211,344,270,324] } assert my_solution.minProcessingTime(**test_input) == 701 test_input = { "processorTime": [220,355,190,393], "tasks": [158,27,113,335,382,172,285,373,104,177,247,321,197,22,347,136] } assert my_solution.minProcessingTime(**test_input) == 572 test_input = { "processorTime": [67,105,290,26,343], "tasks": [50,118,302,74,198,56,292,46,337,27,394,69,109,287,274,283,346,132,77,352] } assert my_solution.minProcessingTime(**test_input) == 420 test_input = { "processorTime": [77,143,142,23], "tasks": [336,190,105,87,102,254,295,243,400,254,96,303,350,191,331,70] } assert my_solution.minProcessingTime(**test_input) == 423 test_input = { "processorTime": [319,58,155,360], "tasks": [311,257,35,330,235,159,293,204,298,240,233,250,309,242,262,324] } assert my_solution.minProcessingTime(**test_input) == 593 test_input = { "processorTime": [28,225,347], "tasks": [176,57,60,81,161,66,13,294,145,239,295,210] } assert my_solution.minProcessingTime(**test_input) == 413 test_input = { "processorTime": [291,337], "tasks": [210,378,169,400,182,290,386,360] } assert my_solution.minProcessingTime(**test_input) == 691 test_input = { "processorTime": [141,310], "tasks": [396,56,241,289,21,254,196,165] } assert my_solution.minProcessingTime(**test_input) == 537 test_input = { "processorTime": [204,390,104], "tasks": [355,4,287,161,230,242,218,12,321,28,341,326] } assert my_solution.minProcessingTime(**test_input) == 551 test_input = { "processorTime": [299,258], "tasks": [20,44,341,172,118,185,369,249] } assert my_solution.minProcessingTime(**test_input) == 627 test_input = { "processorTime": [107,141,178,211,62], "tasks": [215,318,196,251,71,144,10,208,113,17,13,263,367,42,85,267,212,54,36,54] } assert my_solution.minProcessingTime(**test_input) == 429 test_input = { "processorTime": [101,383,326,62], "tasks": [304,256,281,240,180,387,318,368,331,267,14,91,93,147,156,394] } assert my_solution.minProcessingTime(**test_input) == 582 test_input = { "processorTime": [221,62,187,104,266], "tasks": [284,378,9,288,173,327,329,202,3,383,105,213,175,201,196,305,162,161,127,347] } assert my_solution.minProcessingTime(**test_input) == 445 test_input = { "processorTime": [328,162,249,357,35], "tasks": [77,275,231,298,273,257,88,339,261,147,229,392,156,63,90,97,219,353,66,91] } assert my_solution.minProcessingTime(**test_input) == 480 test_input = { "processorTime": [7], "tasks": [132,278,270,176] } assert my_solution.minProcessingTime(**test_input) == 285 test_input = { "processorTime": [326], "tasks": [269,211,137,244] } assert my_solution.minProcessingTime(**test_input) == 595 test_input = { "processorTime": [310,44], "tasks": [109,250,222,275,268,332,146,328] } assert my_solution.minProcessingTime(**test_input) == 560 test_input = { "processorTime": [184,254,121,90,389], "tasks": [124,365,400,167,109,207,369,37,174,287,41,114,388,158,125,283,119,254,210,399] } assert my_solution.minProcessingTime(**test_input) == 503 test_input = { "processorTime": [94,171,66], "tasks": [261,134,26,281,29,253,84,333,90,157,382,263] } assert my_solution.minProcessingTime(**test_input) == 448 test_input = { "processorTime": [5,99,318,252,151], "tasks": [264,235,250,347,376,57,73,7,178,45,220,148,159,379,89,73,159,172,228,39] } assert my_solution.minProcessingTime(**test_input) == 400 test_input = { "processorTime": [303,52,2,118,305], "tasks": [398,173,5,301,169,389,126,212,384,359,222,340,267,173,264,238,141,44,144,148] } assert my_solution.minProcessingTime(**test_input) == 476 test_input = { "processorTime": [177], "tasks": [164,277,289,197] } assert my_solution.minProcessingTime(**test_input) == 466 test_input = { "processorTime": [297,259,318,30,213], "tasks": [162,97,265,153,216,233,286,346,389,208,55,345,308,197,266,292,369,320,1,235] } assert my_solution.minProcessingTime(**test_input) == 533 test_input = { "processorTime": [290], "tasks": [333,282,72,362] } assert my_solution.minProcessingTime(**test_input) == 652 test_input = { "processorTime": [372,189,344], "tasks": [191,26,247,99,395,270,192,340,60,78,260,395] } assert my_solution.minProcessingTime(**test_input) == 604
1,696,732,200
weekly-contest-366-apply-operations-to-make-two-strings-equal
https://leetcode.com/problems/apply-operations-to-make-two-strings-equal
apply-operations-to-make-two-strings-equal
{ "questionId": "3033", "questionFrontendId": "2896", "title": "Apply Operations to Make Two Strings Equal", "titleSlug": "apply-operations-to-make-two-strings-equal", "isPaidOnly": false, "difficulty": "Medium", "likes": 291, "dislikes": 62, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的二进制字符串 s1 和 s2 ,两个字符串的长度都是 n ,再给你一个正整数 x 。 你可以对字符串 s1 执行以下操作 任意次 : * 选择两个下标 i 和 j ,将 s1[i] 和 s1[j] 都反转,操作的代价为 x 。 * 选择满足 i < n - 1 的下标 i ,反转 s1[i] 和 s1[i + 1] ,操作的代价为 1 。 请你返回使字符串 s1 和 s2 相等的 最小 操作代价之和,如果无法让二者相等,返回 -1 。 注意 ,反转字符的意思是将 0 变成 1 ,或者 1 变成 0 。 示例 1: 输入:s1 = "1100011000", s2 = "0101001010", x = 2 输出:4 解释:我们可以执行以下操作: - 选择 i = 3 执行第二个操作。结果字符串是 s1 = "1101111000" 。 - 选择 i = 4 执行第二个操作。结果字符串是 s1 = "1101001000" 。 - 选择 i = 0 和 j = 8 ,执行第一个操作。结果字符串是 s1 = "0101001010" = s2 。 总代价是 1 + 1 + 2 = 4 。这是最小代价和。 示例 2: 输入:s1 = "10110", s2 = "00011", x = 4 输出:-1 解释:无法使两个字符串相等。 提示: * n == s1.length == s2.length * 1 <= n, x <= 500 * s1 和 s2 只包含字符 '0' 和 '1' 。 """ class Solution: def minOperations(self, s1: str, s2: str, x: int) -> int:
给你两个下标从 0 开始的二进制字符串 s1 和 s2 ,两个字符串的长度都是 n ,再给你一个正整数 x 。 你可以对字符串 s1 执行以下操作 任意次 : * 选择两个下标 i 和 j ,将 s1[i] 和 s1[j] 都反转,操作的代价为 x 。 * 选择满足 i < n - 1 的下标 i ,反转 s1[i] 和 s1[i + 1] ,操作的代价为 1 。 请你返回使字符串 s1 和 s2 相等的 最小 操作代价之和,如果无法让二者相等,返回 -1 。 注意 ,反转字符的意思是将 0 变成 1 ,或者 1 变成 0 。 示例 1: 输入:s1 = "1100011000", s2 = "0101001010", x = 2 输出:4 解释:我们可以执行以下操作: - 选择 i = 3 执行第二个操作。结果字符串是 s1 = "1101111000" 。 - 选择 i = 4 执行第二个操作。结果字符串是 s1 = "1101001000" 。 - 选择 i = 0 和 j = 8 ,执行第一个操作。结果字符串是 s1 = "0101001010" = s2 。 总代价是 1 + 1 + 2 = 4 。这是最小代价和。 示例 2: 输入:s1 = "10110", s2 = "00011", x = 4 输出:-1 解释:无法使两个字符串相等。 提示: * n == s1.length == s2.length * 1 <= n, x <= 500 * s1 和 s2 只包含字符 '0' 和 '1' 。 请完成下面的代码来解决上述问题: ```python class Solution: def minOperations(self, s1: str, s2: str, x: int) -> int: ```
my_solution = Solution() test_input = { "s1": "1100011000", "s2": "0101001010", "x": 2 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "10110", "s2": "00011", "x": 4 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "101101", "s2": "000000", "x": 6 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "0", "s2": "1", "x": 2 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1011100100111000", "s2": "1001010001011100", "x": 19 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00101101100010", "s2": "00001010001111", "x": 30 } assert my_solution.minOperations(**test_input) == 8 test_input = { "s1": "1011000", "s2": "0001101", "x": 30 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "1111110101010110", "s2": "1000100111100101", "x": 19 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1011100000100100101", "s2": "1110001001110000011", "x": 14 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0", "s2": "1", "x": 17 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0", "s2": "1", "x": 3 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0001110010", "s2": "0110100111", "x": 29 } assert my_solution.minOperations(**test_input) == 5 test_input = { "s1": "01111101010100110100", "s2": "10010011011001011000", "x": 21 } assert my_solution.minOperations(**test_input) == 7 test_input = { "s1": "00000101", "s2": "01001010", "x": 10 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "01", "s2": "00", "x": 30 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "11111", "s2": "01011", "x": 4 } assert my_solution.minOperations(**test_input) == 2 test_input = { "s1": "001010101011001", "s2": "110111000101110", "x": 14 } assert my_solution.minOperations(**test_input) == 7 test_input = { "s1": "010011101", "s2": "101111000", "x": 17 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "11110111", "s2": "10011111", "x": 16 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0011", "s2": "1100", "x": 14 } assert my_solution.minOperations(**test_input) == 2 test_input = { "s1": "000011", "s2": "010101", "x": 27 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "01111010", "s2": "10110011", "x": 21 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "10010111001", "s2": "11101011110", "x": 1 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "10001", "s2": "11000", "x": 11 } assert my_solution.minOperations(**test_input) == 3 test_input = { "s1": "11001011111", "s2": "01111000110", "x": 2 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "111010100001011", "s2": "100000101100111", "x": 29 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "010000110111000111", "s2": "100011100010010111", "x": 15 } assert my_solution.minOperations(**test_input) == 6 test_input = { "s1": "010010111", "s2": "011100010", "x": 21 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "01011111", "s2": "11110101", "x": 7 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "00001011110000", "s2": "01011110001001", "x": 12 } assert my_solution.minOperations(**test_input) == 8 test_input = { "s1": "1100110100001001", "s2": "0100111010111001", "x": 18 } assert my_solution.minOperations(**test_input) == 8 test_input = { "s1": "00101101", "s2": "10010101", "x": 6 } assert my_solution.minOperations(**test_input) == 3 test_input = { "s1": "110010", "s2": "011011", "x": 21 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00101", "s2": "11000", "x": 25 } assert my_solution.minOperations(**test_input) == 3 test_input = { "s1": "110", "s2": "100", "x": 27 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1101", "s2": "0000", "x": 9 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "11", "s2": "01", "x": 7 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "100000000", "s2": "001011111", "x": 26 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0111001011", "s2": "0010011111", "x": 21 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "10", "s2": "00", "x": 19 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "110111", "s2": "101101", "x": 3 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1010111", "s2": "0110011", "x": 25 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "001", "s2": "101", "x": 19 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "111100000100111", "s2": "110100010110001", "x": 7 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "111", "s2": "110", "x": 4 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "10000010010", "s2": "11100000010", "x": 22 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00100110", "s2": "10101111", "x": 16 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0110010001101011010", "s2": "1011110101000001100", "x": 3 } assert my_solution.minOperations(**test_input) == 8 test_input = { "s1": "0001100000001", "s2": "0011000011101", "x": 28 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0000110011", "s2": "0000000011", "x": 3 } assert my_solution.minOperations(**test_input) == 1 test_input = { "s1": "11101100", "s2": "11111011", "x": 10 } assert my_solution.minOperations(**test_input) == 3 test_input = { "s1": "011101110001001010", "s2": "111000011001101010", "x": 30 } assert my_solution.minOperations(**test_input) == 8 test_input = { "s1": "1111111100", "s2": "1010001010", "x": 5 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "111011", "s2": "111010", "x": 19 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00010", "s2": "00010", "x": 9 } assert my_solution.minOperations(**test_input) == 0 test_input = { "s1": "11100000", "s2": "11110010", "x": 13 } assert my_solution.minOperations(**test_input) == 3 test_input = { "s1": "111101000111", "s2": "101111010010", "x": 16 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0", "s2": "0", "x": 20 } assert my_solution.minOperations(**test_input) == 0 test_input = { "s1": "0011111100011", "s2": "1001100101000", "x": 26 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1000101111101001", "s2": "0110000010110010", "x": 25 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "11000010000", "s2": "11111000001", "x": 17 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1110111001000001", "s2": "0110011110101101", "x": 1 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "0111101101", "s2": "0000111001", "x": 2 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "000001110", "s2": "000101001", "x": 22 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "1110000000", "s2": "1100111100", "x": 5 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1101101010", "s2": "0101010011", "x": 15 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0010011011000101001", "s2": "1110101001110100010", "x": 19 } assert my_solution.minOperations(**test_input) == 9 test_input = { "s1": "11010011101011110111", "s2": "11101111011010010011", "x": 8 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "101100010", "s2": "100011110", "x": 29 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00100001", "s2": "10011101", "x": 29 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "111111110110010", "s2": "111011111010001", "x": 29 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "11011110110010", "s2": "01010100000111", "x": 9 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "011101010101", "s2": "111001010100", "x": 19 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00001", "s2": "11111", "x": 27 } assert my_solution.minOperations(**test_input) == 2 test_input = { "s1": "110", "s2": "101", "x": 15 } assert my_solution.minOperations(**test_input) == 1 test_input = { "s1": "11011001111000111001", "s2": "11100011111011110001", "x": 12 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1010100", "s2": "0100111", "x": 2 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "000", "s2": "010", "x": 21 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1011010100111101", "s2": "1010001100110110", "x": 18 } assert my_solution.minOperations(**test_input) == 9 test_input = { "s1": "111", "s2": "000", "x": 4 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00101100110110010", "s2": "00001111111011011", "x": 22 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "000110011", "s2": "010010001", "x": 8 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1011000000", "s2": "0001010010", "x": 16 } assert my_solution.minOperations(**test_input) == 5 test_input = { "s1": "01101100110011011011", "s2": "10101101010011001011", "x": 5 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "00010", "s2": "00011", "x": 8 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1010110111000111", "s2": "1110110001001000", "x": 22 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "011110100000", "s2": "101100010100", "x": 21 } assert my_solution.minOperations(**test_input) == 5 test_input = { "s1": "1011010010100101101", "s2": "1001001110101100000", "x": 29 } assert my_solution.minOperations(**test_input) == 9 test_input = { "s1": "1111100", "s2": "0010100", "x": 20 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0100100110001", "s2": "1001111110001", "x": 4 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "110", "s2": "011", "x": 2 } assert my_solution.minOperations(**test_input) == 2 test_input = { "s1": "111111010101", "s2": "000011101101", "x": 30 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "101111110110010100", "s2": "010001111100000100", "x": 3 } assert my_solution.minOperations(**test_input) == 7 test_input = { "s1": "0110111", "s2": "0111010", "x": 19 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "1101010101", "s2": "1011000110", "x": 14 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "01100101", "s2": "11010111", "x": 17 } assert my_solution.minOperations(**test_input) == 5 test_input = { "s1": "0011111", "s2": "0110101", "x": 13 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "11110110111111011", "s2": "11101101111010110", "x": 19 } assert my_solution.minOperations(**test_input) == 6 test_input = { "s1": "110100", "s2": "000011", "x": 20 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "0101", "s2": "0010", "x": 29 } assert my_solution.minOperations(**test_input) == -1
1,696,732,200
weekly-contest-366-apply-operations-on-array-to-maximize-sum-of-squares
https://leetcode.com/problems/apply-operations-on-array-to-maximize-sum-of-squares
apply-operations-on-array-to-maximize-sum-of-squares
{ "questionId": "3153", "questionFrontendId": "2897", "title": "Apply Operations on Array to Maximize Sum of Squares", "titleSlug": "apply-operations-on-array-to-maximize-sum-of-squares", "isPaidOnly": false, "difficulty": "Hard", "likes": 159, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 和一个 正 整数 k 。 你可以对数组执行以下操作 任意次 : * 选择两个互不相同的下标 i 和 j ,同时 将 nums[i] 更新为 (nums[i] AND nums[j]) 且将 nums[j] 更新为 (nums[i] OR nums[j]) ,OR 表示按位 或 运算,AND 表示按位 与 运算。 你需要从最终的数组里选择 k 个元素,并计算它们的 平方 之和。 请你返回你可以得到的 最大 平方和。 由于答案可能会很大,将答案对 109 + 7 取余 后返回。 示例 1: 输入:nums = [2,6,5,8], k = 2 输出:261 解释:我们可以对数组执行以下操作: - 选择 i = 0 和 j = 3 ,同时将 nums[0] 变为 (2 AND 8) = 0 且 nums[3] 变为 (2 OR 8) = 10 ,结果数组为 nums = [0,6,5,10] 。 - 选择 i = 2 和 j = 3 ,同时将 nums[2] 变为 (5 AND 10) = 0 且 nums[3] 变为 (5 OR 10) = 15 ,结果数组为 nums = [0,6,0,15] 。 从最终数组里选择元素 15 和 6 ,平方和为 152 + 62 = 261 。 261 是可以得到的最大结果。 示例 2: 输入:nums = [4,5,4,7], k = 3 输出:90 解释:不需要执行任何操作。 选择元素 7 ,5 和 4 ,平方和为 72 + 52 + 42 = 90 。 90 是可以得到的最大结果。 提示: * 1 <= k <= nums.length <= 105 * 1 <= nums[i] <= 109 """ class Solution: def maxSum(self, nums: List[int], k: int) -> int:
给你一个下标从 0 开始的整数数组 nums 和一个 正 整数 k 。 你可以对数组执行以下操作 任意次 : * 选择两个互不相同的下标 i 和 j ,同时 将 nums[i] 更新为 (nums[i] AND nums[j]) 且将 nums[j] 更新为 (nums[i] OR nums[j]) ,OR 表示按位 或 运算,AND 表示按位 与 运算。 你需要从最终的数组里选择 k 个元素,并计算它们的 平方 之和。 请你返回你可以得到的 最大 平方和。 由于答案可能会很大,将答案对 109 + 7 取余 后返回。 示例 1: 输入:nums = [2,6,5,8], k = 2 输出:261 解释:我们可以对数组执行以下操作: - 选择 i = 0 和 j = 3 ,同时将 nums[0] 变为 (2 AND 8) = 0 且 nums[3] 变为 (2 OR 8) = 10 ,结果数组为 nums = [0,6,5,10] 。 - 选择 i = 2 和 j = 3 ,同时将 nums[2] 变为 (5 AND 10) = 0 且 nums[3] 变为 (5 OR 10) = 15 ,结果数组为 nums = [0,6,0,15] 。 从最终数组里选择元素 15 和 6 ,平方和为 152 + 62 = 261 。 261 是可以得到的最大结果。 示例 2: 输入:nums = [4,5,4,7], k = 3 输出:90 解释:不需要执行任何操作。 选择元素 7 ,5 和 4 ,平方和为 72 + 52 + 42 = 90 。 90 是可以得到的最大结果。 提示: * 1 <= k <= nums.length <= 105 * 1 <= nums[i] <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def maxSum(self, nums: List[int], k: int) -> int: ```
my_solution = Solution() test_input = { "nums": [2,6,5,8], "k": 2 } assert my_solution.maxSum(**test_input) == 261 test_input = { "nums": [4,5,4,7], "k": 3 } assert my_solution.maxSum(**test_input) == 90 test_input = { "nums": [32,85,61], "k": 1 } assert my_solution.maxSum(**test_input) == 15625 test_input = { "nums": [123], "k": 1 } assert my_solution.maxSum(**test_input) == 15129 test_input = { "nums": [96,66,60,58,32,17,63,21,30,44,15,8,98,93], "k": 2 } assert my_solution.maxSum(**test_input) == 32258 test_input = { "nums": [30,8,63,69,52,94,41,28,94,86,28,13,68,38,53,11,21,33], "k": 2 } assert my_solution.maxSum(**test_input) == 32258 test_input = { "nums": [2,38,15,2,73,100,47,14,25,58,40,64,23,9,53,38,91,75,9,2], "k": 3 } assert my_solution.maxSum(**test_input) == 48387 test_input = { "nums": [25,52,75,65], "k": 4 } assert my_solution.maxSum(**test_input) == 24051 test_input = { "nums": [96,36,72,61,13,25,5,33,9,51,9,78,40], "k": 13 } assert my_solution.maxSum(**test_input) == 53776 test_input = { "nums": [38,21,15,84,65,35,57,82,94,26,27,89,73,22,25,6,97,17], "k": 4 } assert my_solution.maxSum(**test_input) == 64516 test_input = { "nums": [18,72,52,56,7,21,55,68,98,31,35,49,100,49,64,20], "k": 4 } assert my_solution.maxSum(**test_input) == 62548 test_input = { "nums": [2,73,75], "k": 3 } assert my_solution.maxSum(**test_input) == 11250 test_input = { "nums": [73,37,41,84], "k": 2 } assert my_solution.maxSum(**test_input) == 27506 test_input = { "nums": [62,83,11,3,53], "k": 3 } assert my_solution.maxSum(**test_input) == 20459 test_input = { "nums": [53,59,71,38,5,15,98,86,9,8,35,54,65,77,3,68,11,5,41,18], "k": 9 } assert my_solution.maxSum(**test_input) == 95273 test_input = { "nums": [53,67,91,79,21,27,63,34,60,94,51], "k": 4 } assert my_solution.maxSum(**test_input) == 64516 test_input = { "nums": [41,15,6,31,40,97,11,45,81,91,91,62], "k": 3 } assert my_solution.maxSum(**test_input) == 48387 test_input = { "nums": [10,9], "k": 2 } assert my_solution.maxSum(**test_input) == 185 test_input = { "nums": [9,6,8,32,92,12,47,45,62,96,5,66,82,90,34,39,49,86,16], "k": 13 } assert my_solution.maxSum(**test_input) == 102770 test_input = { "nums": [1,19,29,30,68,13,80,16,71,32,8,76,41,24,16,2,30], "k": 14 } assert my_solution.maxSum(**test_input) == 53470 test_input = { "nums": [22,64,30,71,28,69,86,12,26,39,69,92], "k": 2 } assert my_solution.maxSum(**test_input) == 25154 test_input = { "nums": [91,26,29,38,97,40,1,18,15,3,43,37,9,55,4,46], "k": 2 } assert my_solution.maxSum(**test_input) == 32258 test_input = { "nums": [27,73], "k": 1 } assert my_solution.maxSum(**test_input) == 8281 test_input = { "nums": [12,33,29,75,94,48,25,21], "k": 8 } assert my_solution.maxSum(**test_input) == 34565 test_input = { "nums": [39,91,84,10,65,28,94,28,62,77,78,50,93,65,21,16,5,35,81], "k": 14 } assert my_solution.maxSum(**test_input) == 110106 test_input = { "nums": [14,45,76,33,35,53,67,19,6,31,33], "k": 10 } assert my_solution.maxSum(**test_input) == 40008 test_input = { "nums": [59,88,2,47,75], "k": 2 } assert my_solution.maxSum(**test_input) == 31258 test_input = { "nums": [96,77,77,33,5,86,90,21,84,73,86,45,88,35,93,14,63,25], "k": 17 } assert my_solution.maxSum(**test_input) == 121571 test_input = { "nums": [35,5,21,65,34,90,60,8,34,35,28,78,77], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [14,10,19], "k": 2 } assert my_solution.maxSum(**test_input) == 1061 test_input = { "nums": [100,4,88,29,13,78,89,11,62,63,66,46,99,87,41,29,36,71,57], "k": 18 } assert my_solution.maxSum(**test_input) == 129739 test_input = { "nums": [86,52,100,68,30,40,49,28,61,30,3,80], "k": 11 } assert my_solution.maxSum(**test_input) == 69063 test_input = { "nums": [29,30,61,12,98,95], "k": 2 } assert my_solution.maxSum(**test_input) == 32258 test_input = { "nums": [23,13,35,41,29,57,84,67,70,96,55,85,15,72,23,52,1,11,62,1], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [34,60,85,22,83], "k": 2 } assert my_solution.maxSum(**test_input) == 30290 test_input = { "nums": [65,26,44,70,79,65,46,18], "k": 8 } assert my_solution.maxSum(**test_input) == 44587 test_input = { "nums": [99,50,13,62,12,60,6,29], "k": 6 } assert my_solution.maxSum(**test_input) == 28071 test_input = { "nums": [73,66,75,44], "k": 3 } assert my_solution.maxSum(**test_input) == 23130 test_input = { "nums": [43,85,7,66,16,96], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [45,5,3,84,81,54,21,37,99,60], "k": 2 } assert my_solution.maxSum(**test_input) == 32258 test_input = { "nums": [97,6,44,57,63,5], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [65,43,82,46,34,42,65,67,8,67,3,83,87,71,98,31,15,22], "k": 5 } assert my_solution.maxSum(**test_input) == 80645 test_input = { "nums": [79,33,75,32,64,68,30,46,60,50,6,54,18,34,43,11,84,78,54,4], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [17,9,3,23,33,99,94,15,93,17,39,55,13,26,22,44,13], "k": 3 } assert my_solution.maxSum(**test_input) == 48387 test_input = { "nums": [83,29,2,67,79,88,71,98,70], "k": 4 } assert my_solution.maxSum(**test_input) == 39220 test_input = { "nums": [60,81,60,88,37,38,10,42,84,70], "k": 10 } assert my_solution.maxSum(**test_input) == 67626 test_input = { "nums": [33,51,100,33,46], "k": 3 } assert my_solution.maxSum(**test_input) == 18739 test_input = { "nums": [29,4,67,44,74,62,41,86,91,11,26,58,59,48,46,41,26,68,4,81], "k": 4 } assert my_solution.maxSum(**test_input) == 64516 test_input = { "nums": [86,54,20,57,87,63,2,24,73,87,7,16,50,1,58], "k": 7 } assert my_solution.maxSum(**test_input) == 69543 test_input = { "nums": [91,2,16,77,2], "k": 1 } assert my_solution.maxSum(**test_input) == 9025 test_input = { "nums": [19,94], "k": 2 } assert my_solution.maxSum(**test_input) == 9349 test_input = { "nums": [14,67,79,58], "k": 2 } assert my_solution.maxSum(**test_input) == 22370 test_input = { "nums": [44,17,10,19,3,97,45,65,98,7,73,30,76,5,52,33,62], "k": 4 } assert my_solution.maxSum(**test_input) == 64516 test_input = { "nums": [8,80,93], "k": 1 } assert my_solution.maxSum(**test_input) == 8649 test_input = { "nums": [51,79,26,30,41,74,6,11,10,66,61,25,41,32,83,52,71,70], "k": 18 } assert my_solution.maxSum(**test_input) == 98085 test_input = { "nums": [15,58,38,69,71,43], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [13,57,34,69,80,98,63,22,29,38,70,94,79,95,13,76,39,22], "k": 5 } assert my_solution.maxSum(**test_input) == 80645 test_input = { "nums": [98,88,17,85,57,97,42,15,25,71,31,72,76,89,28,47,73,85], "k": 9 } assert my_solution.maxSum(**test_input) == 114889 test_input = { "nums": [95,28,26,65,87,4,14,25,47,97,67,48,29,14,96,76,77,25], "k": 9 } assert my_solution.maxSum(**test_input) == 100409 test_input = { "nums": [37,16,76,9,88,44,71,61,95,32,63,10,29,33], "k": 11 } assert my_solution.maxSum(**test_input) == 72360 test_input = { "nums": [54,96,73,18,15,35,79,96,2,12,50,75,7,93,66,35,40,16], "k": 18 } assert my_solution.maxSum(**test_input) == 104436 test_input = { "nums": [66,84,85,7,45,34,61,91,83,13,87,89,51,52,65], "k": 6 } assert my_solution.maxSum(**test_input) == 88214 test_input = { "nums": [76,22,86,88,58,10,61,21,42], "k": 2 } assert my_solution.maxSum(**test_input) == 32258 test_input = { "nums": [16,52,8,99,68,73], "k": 2 } assert my_solution.maxSum(**test_input) == 31754 test_input = { "nums": [46,91,73,38,36,79,24,78,24,42], "k": 8 } assert my_solution.maxSum(**test_input) == 60911 test_input = { "nums": [5,100,85,52,5,28,79,30,9,67,87,50,17,29,99,57], "k": 14 } assert my_solution.maxSum(**test_input) == 91019 test_input = { "nums": [52,57,77,95,79,28,9,94,70,8,89,75,27,53,41,88,68,8,10,59], "k": 5 } assert my_solution.maxSum(**test_input) == 80645 test_input = { "nums": [66,64,52,90,73,84,2], "k": 6 } assert my_solution.maxSum(**test_input) == 39881 test_input = { "nums": [97,100], "k": 1 } assert my_solution.maxSum(**test_input) == 10201 test_input = { "nums": [27,85,57,44,16,55,77,77,24,62,72], "k": 10 } assert my_solution.maxSum(**test_input) == 66334 test_input = { "nums": [15,6,18,22,72,63,38,72,4,84,9,19,70,76,72,98,35,51,11,9], "k": 20 } assert my_solution.maxSum(**test_input) == 96344 test_input = { "nums": [10,67,54,100,6,93,91,4,59], "k": 3 } assert my_solution.maxSum(**test_input) == 48387 test_input = { "nums": [90,48,91,62,39,94,75,8,21,72,9,55,16,30,27,73,81,39,97], "k": 8 } assert my_solution.maxSum(**test_input) == 114080 test_input = { "nums": [43,58,51,40,39,92,36,57], "k": 4 } assert my_solution.maxSum(**test_input) == 27548 test_input = { "nums": [85,51,49,13,7,66,21,59,100,14,5,66], "k": 8 } assert my_solution.maxSum(**test_input) == 63152 test_input = { "nums": [54,52,36,17,34,100,81,82,16,46,26,73,77,55,43,53], "k": 14 } assert my_solution.maxSum(**test_input) == 91703 test_input = { "nums": [97,1,30,41,82,77,99,56,21,68,14,39,15,26,72], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [17,10,28,78,68,68,29,44], "k": 5 } assert my_solution.maxSum(**test_input) == 33906 test_input = { "nums": [59,30,26,58,87,1,6,98,29,50,57,64,64], "k": 10 } assert my_solution.maxSum(**test_input) == 69321 test_input = { "nums": [77,23,43,94,74,41,26,39,83,57,85,49,83,34,63,37,42,55,20,18], "k": 3 } assert my_solution.maxSum(**test_input) == 48387 test_input = { "nums": [54,19,69,95,26,59,68,90,77,62,67,54,42,25,50,23,30,53,29,78], "k": 12 } assert my_solution.maxSum(**test_input) == 117170 test_input = { "nums": [54,17,16,30,35,63,34,38,26,41,33], "k": 9 } assert my_solution.maxSum(**test_input) == 22133 test_input = { "nums": [17,24,8,57,68,54,64,53], "k": 6 } assert my_solution.maxSum(**test_input) == 35987 test_input = { "nums": [11,90,27,72,22,24,54,64,68,94,1,20,45,5,63], "k": 10 } assert my_solution.maxSum(**test_input) == 69082 test_input = { "nums": [92,84,89,72,80,1,8], "k": 3 } assert my_solution.maxSum(**test_input) == 25042 test_input = { "nums": [29,14,37,61,7,10,53,95,47,81,1,59,18,25,3,53,43,64,33], "k": 5 } assert my_solution.maxSum(**test_input) == 56325 test_input = { "nums": [44,94,83,38,63,16,45,90,74,20], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [13,65], "k": 2 } assert my_solution.maxSum(**test_input) == 5930 test_input = { "nums": [16,9,2,66], "k": 2 } assert my_solution.maxSum(**test_input) == 8285 test_input = { "nums": [30,68,85,13,49,96,59,61,39], "k": 7 } assert my_solution.maxSum(**test_input) == 54942 test_input = { "nums": [29,47,38,4], "k": 2 } assert my_solution.maxSum(**test_input) == 6178 test_input = { "nums": [49,30], "k": 2 } assert my_solution.maxSum(**test_input) == 4225 test_input = { "nums": [64,71,33,46,77,45,33,55,84,30,1,40,2,92,54,88], "k": 8 } assert my_solution.maxSum(**test_input) == 98815 test_input = { "nums": [55,72,75], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [3,34,63,27,49,28,86], "k": 3 } assert my_solution.maxSum(**test_input) == 24067 test_input = { "nums": [99,29,94,21,54,43,20,79,27,40,90,76,55,27,40,46,76,70,34], "k": 13 } assert my_solution.maxSum(**test_input) == 118264 test_input = { "nums": [64,11,2,12,11,82,10,42,63,98,99,13], "k": 1 } assert my_solution.maxSum(**test_input) == 16129 test_input = { "nums": [23,25,45,88,88,93,91,25,34,83,9,85,18], "k": 8 } assert my_solution.maxSum(**test_input) == 67760 test_input = { "nums": [84,95,7,53,19,46,41,48,48,38], "k": 7 } assert my_solution.maxSum(**test_input) == 44981 test_input = { "nums": [92,88,27,34], "k": 4 } assert my_solution.maxSum(**test_input) == 24805
1,696,732,200
weekly-contest-365-maximum-value-of-an-ordered-triplet-i
https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i
maximum-value-of-an-ordered-triplet-i
{ "questionId": "3154", "questionFrontendId": "2873", "title": "Maximum Value of an Ordered Triplet I", "titleSlug": "maximum-value-of-an-ordered-triplet-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 130, "dislikes": 10, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:133 解释:下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。 可以证明不存在值大于 133 的有序下标三元组。 示例 3: 输入:nums = [1,2,3] 输出:0 解释:唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。 提示: * 3 <= nums.length <= 100 * 1 <= nums[i] <= 106 """ class Solution: def maximumTripletValue(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:133 解释:下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。 可以证明不存在值大于 133 的有序下标三元组。 示例 3: 输入:nums = [1,2,3] 输出:0 解释:唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。 提示: * 3 <= nums.length <= 100 * 1 <= nums[i] <= 106 请完成下面的代码来解决上述问题: ```python class Solution: def maximumTripletValue(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [12,6,1,2,7] } assert my_solution.maximumTripletValue(**test_input) == 77 test_input = { "nums": [1,10,3,4,19] } assert my_solution.maximumTripletValue(**test_input) == 133 test_input = { "nums": [1,2,3] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [2,3,1] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [5,7,8,4] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [1000000,1,1000000] } assert my_solution.maximumTripletValue(**test_input) == 999999000000 test_input = { "nums": [18,15,8,13,10,9,17,10,2,16,17] } assert my_solution.maximumTripletValue(**test_input) == 272 test_input = { "nums": [8,6,3,13,2,12,19,5,19,6,10,11,9] } assert my_solution.maximumTripletValue(**test_input) == 266 test_input = { "nums": [6,11,12,12,7,9,2,11,12,4,19,14,16,8,16] } assert my_solution.maximumTripletValue(**test_input) == 190 test_input = { "nums": [15,14,17,13,18,17,10,19,2,20,12,9] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [6,14,20,19,19,10,3,15,12,13,8,1,2,15,3] } assert my_solution.maximumTripletValue(**test_input) == 285 test_input = { "nums": [2,7,19,4,8,20] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [10,13,6,2] } assert my_solution.maximumTripletValue(**test_input) == 14 test_input = { "nums": [1,19,1,3,18,10,16,9,3,17,8,9] } assert my_solution.maximumTripletValue(**test_input) == 324 test_input = { "nums": [16,2,10,20,16,2,13,8,19] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [19,11,12,4,17,1,7,20,13,10,14,20,11,19,3] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [16,15,12,5,4,12,15,17,5,18,6,16,1,17,4] } assert my_solution.maximumTripletValue(**test_input) == 289 test_input = { "nums": [8,10,17,11,2,8,13] } assert my_solution.maximumTripletValue(**test_input) == 195 test_input = { "nums": [13,4,3,19,16,14,17,6,20,6,16,4] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [1,8,9,18,4,10,3,13,9] } assert my_solution.maximumTripletValue(**test_input) == 195 test_input = { "nums": [10,10,5,19,2] } assert my_solution.maximumTripletValue(**test_input) == 95 test_input = { "nums": [15,3,3,18,19,13,7,5,18,1,8,5] } assert my_solution.maximumTripletValue(**test_input) == 252 test_input = { "nums": [10,20,10] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [14,9,4,20,9] } assert my_solution.maximumTripletValue(**test_input) == 200 test_input = { "nums": [12,20,5,2,13,17,16,1,5,8,18,15,12] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [7,1,17,17,4,20,14,20] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [16,19,8,8,5,18,12,16,8,14,14,7,19] } assert my_solution.maximumTripletValue(**test_input) == 266 test_input = { "nums": [17,9,13,7,3,5] } assert my_solution.maximumTripletValue(**test_input) == 104 test_input = { "nums": [15,12,2,14,15,18,15,20,14,5,14,14,11,13,7] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [17,20,17,13,5,12,8,12,14,10,14,20] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [1,19,10] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [11,16,10,15,10,5,7,3] } assert my_solution.maximumTripletValue(**test_input) == 90 test_input = { "nums": [5,14,19,12,2,5,18,3,20,12,1,11] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [10,8,12,14] } assert my_solution.maximumTripletValue(**test_input) == 28 test_input = { "nums": [2,17,18,16,14,20,11,3,18,5,20,6,7] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [19,12,3,19,2,18,3,12,9] } assert my_solution.maximumTripletValue(**test_input) == 306 test_input = { "nums": [12,9,11,2,11,3,11,17,13,19] } assert my_solution.maximumTripletValue(**test_input) == 190 test_input = { "nums": [8,13,9,8,7,18,20] } assert my_solution.maximumTripletValue(**test_input) == 120 test_input = { "nums": [20,8,12,1,7,8,3,3,6] } assert my_solution.maximumTripletValue(**test_input) == 152 test_input = { "nums": [8,2,16,6,14,14,13,2,11,5,2,12,15,3,3] } assert my_solution.maximumTripletValue(**test_input) == 210 test_input = { "nums": [19,9,9,9,5] } assert my_solution.maximumTripletValue(**test_input) == 90 test_input = { "nums": [19,10,5,13,6,9,5,15,19] } assert my_solution.maximumTripletValue(**test_input) == 266 test_input = { "nums": [14,18,17,8,2,8,14] } assert my_solution.maximumTripletValue(**test_input) == 224 test_input = { "nums": [11,5,17,13,5,8,8,19,17,1] } assert my_solution.maximumTripletValue(**test_input) == 228 test_input = { "nums": [18,12,18,14,17,19] } assert my_solution.maximumTripletValue(**test_input) == 114 test_input = { "nums": [18,17,8,8,18,9] } assert my_solution.maximumTripletValue(**test_input) == 180 test_input = { "nums": [15,3,2,10,11,10,13,18] } assert my_solution.maximumTripletValue(**test_input) == 234 test_input = { "nums": [17,17,5,10,19,1,16,3,1,19] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [1,18,4,20,16] } assert my_solution.maximumTripletValue(**test_input) == 280 test_input = { "nums": [6,20,4,4,2,19,14,10,9,7,20,5,8] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [5,14,15,18,2,9,15,13,11,16,12,20] } assert my_solution.maximumTripletValue(**test_input) == 320 test_input = { "nums": [7,19,17] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [5,7,14,18,13,11,15,20,8,11,12,4,17,2,16] } assert my_solution.maximumTripletValue(**test_input) == 288 test_input = { "nums": [4,12,7,2,8,6,9,5,4,1,8] } assert my_solution.maximumTripletValue(**test_input) == 90 test_input = { "nums": [11,17,2,18,5] } assert my_solution.maximumTripletValue(**test_input) == 270 test_input = { "nums": [19,13,2,2,19] } assert my_solution.maximumTripletValue(**test_input) == 323 test_input = { "nums": [14,11,7,6,2,20,16,14,4,12,1,9,16,7,10] } assert my_solution.maximumTripletValue(**test_input) == 304 test_input = { "nums": [8,15,6,16,16,9,6,14,4] } assert my_solution.maximumTripletValue(**test_input) == 144 test_input = { "nums": [16,19,1,7,18,6,18,5,19,18,19] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [16,14,11,2,17,9,10] } assert my_solution.maximumTripletValue(**test_input) == 238 test_input = { "nums": [3,4,18,2,20,1,1,16,15,8,7,14,19,6] } assert my_solution.maximumTripletValue(**test_input) == 361 test_input = { "nums": [12,20,14,18,11,16,16,9,12,5,14,17] } assert my_solution.maximumTripletValue(**test_input) == 255 test_input = { "nums": [12,19,2,9,6] } assert my_solution.maximumTripletValue(**test_input) == 153 test_input = { "nums": [17,19,14,7,10,18] } assert my_solution.maximumTripletValue(**test_input) == 216 test_input = { "nums": [3,4,19,10,16,13,6,20] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [11,6,8,9] } assert my_solution.maximumTripletValue(**test_input) == 45 test_input = { "nums": [7,12,9,19,10,18,16,2,1,3,7,9,7,7] } assert my_solution.maximumTripletValue(**test_input) == 162 test_input = { "nums": [20,9,20,7,3,7,19] } assert my_solution.maximumTripletValue(**test_input) == 323 test_input = { "nums": [10,11,3,3,3,2,9,8] } assert my_solution.maximumTripletValue(**test_input) == 81 test_input = { "nums": [4,20,15,1,17,2,2,4,10,15,2,8,16,6] } assert my_solution.maximumTripletValue(**test_input) == 323 test_input = { "nums": [15,10,1,18,18,16,7,13,9,11] } assert my_solution.maximumTripletValue(**test_input) == 252 test_input = { "nums": [10,6,17,11,15,15,18] } assert my_solution.maximumTripletValue(**test_input) == 108 test_input = { "nums": [3,6,18] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [4,7,20] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [16,12,5] } assert my_solution.maximumTripletValue(**test_input) == 20 test_input = { "nums": [4,17,15,12,2,16,16,13,6,20,14,17,18,16] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [1,7,18,3,1,11,7,17] } assert my_solution.maximumTripletValue(**test_input) == 289 test_input = { "nums": [18,16,10,2] } assert my_solution.maximumTripletValue(**test_input) == 20 test_input = { "nums": [3,10,18,10,7,8] } assert my_solution.maximumTripletValue(**test_input) == 88 test_input = { "nums": [8,6,20,20,4,12,14,7,13,16,12,15,12] } assert my_solution.maximumTripletValue(**test_input) == 256 test_input = { "nums": [5,19,11,18,19,14,8,11,4,10] } assert my_solution.maximumTripletValue(**test_input) == 152 test_input = { "nums": [17,1,16] } assert my_solution.maximumTripletValue(**test_input) == 256 test_input = { "nums": [20,8,17,14,15,2,7,9,1,10,10,4,19,2,1] } assert my_solution.maximumTripletValue(**test_input) == 361 test_input = { "nums": [9,16,16] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [2,16,2,19,5,20,2,20,6,6] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [18,3,6,17,4,20,14,6,13,9,5,11] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [12,2,19,15,4,3,18,6,11,9,9,6,15] } assert my_solution.maximumTripletValue(**test_input) == 288 test_input = { "nums": [10,15,10,13,7,18,18,3,13,15,20,4,6,15] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [10,15,4,19,6,17,7,10,4,12,14,16,9,14] } assert my_solution.maximumTripletValue(**test_input) == 240 test_input = { "nums": [17,6,3,8,13] } assert my_solution.maximumTripletValue(**test_input) == 182 test_input = { "nums": [6,18,8,8,16,14,7,18] } assert my_solution.maximumTripletValue(**test_input) == 198 test_input = { "nums": [7,7,2,19,16,11,3,15,3,15,16,17] } assert my_solution.maximumTripletValue(**test_input) == 272 test_input = { "nums": [9,3,3,12,9,12,5,7,6,2,9,9,14,9,5] } assert my_solution.maximumTripletValue(**test_input) == 140 test_input = { "nums": [19,14,15,1,20,10,20,4,10,20,15,15,2,7] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [17,4,10,16,8,20,4,9,11,15,2,7] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [3,8,17,10,10,20,20,8,14,20,1,10,1] } assert my_solution.maximumTripletValue(**test_input) == 240 test_input = { "nums": [3,4,11,18,10,19,9,11,14,11,18,15,17,19,3] } assert my_solution.maximumTripletValue(**test_input) == 190 test_input = { "nums": [18,10,5,16,13,1,19,10,17,14,14,20] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [18,3,16,14,15,9,13,2,3] } assert my_solution.maximumTripletValue(**test_input) == 240 test_input = { "nums": [2,6,19,10,19,14,18,8,3,2] } assert my_solution.maximumTripletValue(**test_input) == 171
1,696,127,400
weekly-contest-365-maximum-value-of-an-ordered-triplet-ii
https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii
maximum-value-of-an-ordered-triplet-ii
{ "questionId": "3152", "questionFrontendId": "2874", "title": "Maximum Value of an Ordered Triplet II", "titleSlug": "maximum-value-of-an-ordered-triplet-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 238, "dislikes": 8, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:133 解释:下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。 可以证明不存在值大于 133 的有序下标三元组。 示例 3: 输入:nums = [1,2,3] 输出:0 解释:唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。 提示: * 3 <= nums.length <= 105 * 1 <= nums[i] <= 106 """ class Solution: def maximumTripletValue(self, nums: List[int]) -> int:
给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:133 解释:下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。 可以证明不存在值大于 133 的有序下标三元组。 示例 3: 输入:nums = [1,2,3] 输出:0 解释:唯一的下标三元组 (0, 1, 2) 的值是一个负数,(nums[0] - nums[1]) * nums[2] = -3 。因此,答案是 0 。 提示: * 3 <= nums.length <= 105 * 1 <= nums[i] <= 106 请完成下面的代码来解决上述问题: ```python class Solution: def maximumTripletValue(self, nums: List[int]) -> int: ```
my_solution = Solution() test_input = { "nums": [12,6,1,2,7] } assert my_solution.maximumTripletValue(**test_input) == 77 test_input = { "nums": [1,10,3,4,19] } assert my_solution.maximumTripletValue(**test_input) == 133 test_input = { "nums": [1,2,3] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [2,3,1] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [5,7,8,4] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [1000000,1,1000000] } assert my_solution.maximumTripletValue(**test_input) == 999999000000 test_input = { "nums": [18,15,8,13,10,9,17,10,2,16,17] } assert my_solution.maximumTripletValue(**test_input) == 272 test_input = { "nums": [8,6,3,13,2,12,19,5,19,6,10,11,9] } assert my_solution.maximumTripletValue(**test_input) == 266 test_input = { "nums": [6,11,12,12,7,9,2,11,12,4,19,14,16,8,16] } assert my_solution.maximumTripletValue(**test_input) == 190 test_input = { "nums": [15,14,17,13,18,17,10,19,2,20,12,9] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [6,14,20,19,19,10,3,15,12,13,8,1,2,15,3] } assert my_solution.maximumTripletValue(**test_input) == 285 test_input = { "nums": [2,7,19,4,8,20] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [10,13,6,2] } assert my_solution.maximumTripletValue(**test_input) == 14 test_input = { "nums": [1,19,1,3,18,10,16,9,3,17,8,9] } assert my_solution.maximumTripletValue(**test_input) == 324 test_input = { "nums": [16,2,10,20,16,2,13,8,19] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [19,11,12,4,17,1,7,20,13,10,14,20,11,19,3] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [16,15,12,5,4,12,15,17,5,18,6,16,1,17,4] } assert my_solution.maximumTripletValue(**test_input) == 289 test_input = { "nums": [8,10,17,11,2,8,13] } assert my_solution.maximumTripletValue(**test_input) == 195 test_input = { "nums": [13,4,3,19,16,14,17,6,20,6,16,4] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [1,8,9,18,4,10,3,13,9] } assert my_solution.maximumTripletValue(**test_input) == 195 test_input = { "nums": [10,10,5,19,2] } assert my_solution.maximumTripletValue(**test_input) == 95 test_input = { "nums": [15,3,3,18,19,13,7,5,18,1,8,5] } assert my_solution.maximumTripletValue(**test_input) == 252 test_input = { "nums": [10,20,10] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [14,9,4,20,9] } assert my_solution.maximumTripletValue(**test_input) == 200 test_input = { "nums": [12,20,5,2,13,17,16,1,5,8,18,15,12] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [7,1,17,17,4,20,14,20] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [16,19,8,8,5,18,12,16,8,14,14,7,19] } assert my_solution.maximumTripletValue(**test_input) == 266 test_input = { "nums": [17,9,13,7,3,5] } assert my_solution.maximumTripletValue(**test_input) == 104 test_input = { "nums": [15,12,2,14,15,18,15,20,14,5,14,14,11,13,7] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [17,20,17,13,5,12,8,12,14,10,14,20] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [1,19,10] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [11,16,10,15,10,5,7,3] } assert my_solution.maximumTripletValue(**test_input) == 90 test_input = { "nums": [5,14,19,12,2,5,18,3,20,12,1,11] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [10,8,12,14] } assert my_solution.maximumTripletValue(**test_input) == 28 test_input = { "nums": [2,17,18,16,14,20,11,3,18,5,20,6,7] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [19,12,3,19,2,18,3,12,9] } assert my_solution.maximumTripletValue(**test_input) == 306 test_input = { "nums": [12,9,11,2,11,3,11,17,13,19] } assert my_solution.maximumTripletValue(**test_input) == 190 test_input = { "nums": [8,13,9,8,7,18,20] } assert my_solution.maximumTripletValue(**test_input) == 120 test_input = { "nums": [20,8,12,1,7,8,3,3,6] } assert my_solution.maximumTripletValue(**test_input) == 152 test_input = { "nums": [8,2,16,6,14,14,13,2,11,5,2,12,15,3,3] } assert my_solution.maximumTripletValue(**test_input) == 210 test_input = { "nums": [19,9,9,9,5] } assert my_solution.maximumTripletValue(**test_input) == 90 test_input = { "nums": [19,10,5,13,6,9,5,15,19] } assert my_solution.maximumTripletValue(**test_input) == 266 test_input = { "nums": [14,18,17,8,2,8,14] } assert my_solution.maximumTripletValue(**test_input) == 224 test_input = { "nums": [11,5,17,13,5,8,8,19,17,1] } assert my_solution.maximumTripletValue(**test_input) == 228 test_input = { "nums": [18,12,18,14,17,19] } assert my_solution.maximumTripletValue(**test_input) == 114 test_input = { "nums": [18,17,8,8,18,9] } assert my_solution.maximumTripletValue(**test_input) == 180 test_input = { "nums": [15,3,2,10,11,10,13,18] } assert my_solution.maximumTripletValue(**test_input) == 234 test_input = { "nums": [17,17,5,10,19,1,16,3,1,19] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [1,18,4,20,16] } assert my_solution.maximumTripletValue(**test_input) == 280 test_input = { "nums": [6,20,4,4,2,19,14,10,9,7,20,5,8] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [5,14,15,18,2,9,15,13,11,16,12,20] } assert my_solution.maximumTripletValue(**test_input) == 320 test_input = { "nums": [7,19,17] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [5,7,14,18,13,11,15,20,8,11,12,4,17,2,16] } assert my_solution.maximumTripletValue(**test_input) == 288 test_input = { "nums": [4,12,7,2,8,6,9,5,4,1,8] } assert my_solution.maximumTripletValue(**test_input) == 90 test_input = { "nums": [11,17,2,18,5] } assert my_solution.maximumTripletValue(**test_input) == 270 test_input = { "nums": [19,13,2,2,19] } assert my_solution.maximumTripletValue(**test_input) == 323 test_input = { "nums": [14,11,7,6,2,20,16,14,4,12,1,9,16,7,10] } assert my_solution.maximumTripletValue(**test_input) == 304 test_input = { "nums": [8,15,6,16,16,9,6,14,4] } assert my_solution.maximumTripletValue(**test_input) == 144 test_input = { "nums": [16,19,1,7,18,6,18,5,19,18,19] } assert my_solution.maximumTripletValue(**test_input) == 342 test_input = { "nums": [16,14,11,2,17,9,10] } assert my_solution.maximumTripletValue(**test_input) == 238 test_input = { "nums": [3,4,18,2,20,1,1,16,15,8,7,14,19,6] } assert my_solution.maximumTripletValue(**test_input) == 361 test_input = { "nums": [12,20,14,18,11,16,16,9,12,5,14,17] } assert my_solution.maximumTripletValue(**test_input) == 255 test_input = { "nums": [12,19,2,9,6] } assert my_solution.maximumTripletValue(**test_input) == 153 test_input = { "nums": [17,19,14,7,10,18] } assert my_solution.maximumTripletValue(**test_input) == 216 test_input = { "nums": [3,4,19,10,16,13,6,20] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [11,6,8,9] } assert my_solution.maximumTripletValue(**test_input) == 45 test_input = { "nums": [7,12,9,19,10,18,16,2,1,3,7,9,7,7] } assert my_solution.maximumTripletValue(**test_input) == 162 test_input = { "nums": [20,9,20,7,3,7,19] } assert my_solution.maximumTripletValue(**test_input) == 323 test_input = { "nums": [10,11,3,3,3,2,9,8] } assert my_solution.maximumTripletValue(**test_input) == 81 test_input = { "nums": [4,20,15,1,17,2,2,4,10,15,2,8,16,6] } assert my_solution.maximumTripletValue(**test_input) == 323 test_input = { "nums": [15,10,1,18,18,16,7,13,9,11] } assert my_solution.maximumTripletValue(**test_input) == 252 test_input = { "nums": [10,6,17,11,15,15,18] } assert my_solution.maximumTripletValue(**test_input) == 108 test_input = { "nums": [3,6,18] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [4,7,20] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [16,12,5] } assert my_solution.maximumTripletValue(**test_input) == 20 test_input = { "nums": [4,17,15,12,2,16,16,13,6,20,14,17,18,16] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [1,7,18,3,1,11,7,17] } assert my_solution.maximumTripletValue(**test_input) == 289 test_input = { "nums": [18,16,10,2] } assert my_solution.maximumTripletValue(**test_input) == 20 test_input = { "nums": [3,10,18,10,7,8] } assert my_solution.maximumTripletValue(**test_input) == 88 test_input = { "nums": [8,6,20,20,4,12,14,7,13,16,12,15,12] } assert my_solution.maximumTripletValue(**test_input) == 256 test_input = { "nums": [5,19,11,18,19,14,8,11,4,10] } assert my_solution.maximumTripletValue(**test_input) == 152 test_input = { "nums": [17,1,16] } assert my_solution.maximumTripletValue(**test_input) == 256 test_input = { "nums": [20,8,17,14,15,2,7,9,1,10,10,4,19,2,1] } assert my_solution.maximumTripletValue(**test_input) == 361 test_input = { "nums": [9,16,16] } assert my_solution.maximumTripletValue(**test_input) == 0 test_input = { "nums": [2,16,2,19,5,20,2,20,6,6] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [18,3,6,17,4,20,14,6,13,9,5,11] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [12,2,19,15,4,3,18,6,11,9,9,6,15] } assert my_solution.maximumTripletValue(**test_input) == 288 test_input = { "nums": [10,15,10,13,7,18,18,3,13,15,20,4,6,15] } assert my_solution.maximumTripletValue(**test_input) == 300 test_input = { "nums": [10,15,4,19,6,17,7,10,4,12,14,16,9,14] } assert my_solution.maximumTripletValue(**test_input) == 240 test_input = { "nums": [17,6,3,8,13] } assert my_solution.maximumTripletValue(**test_input) == 182 test_input = { "nums": [6,18,8,8,16,14,7,18] } assert my_solution.maximumTripletValue(**test_input) == 198 test_input = { "nums": [7,7,2,19,16,11,3,15,3,15,16,17] } assert my_solution.maximumTripletValue(**test_input) == 272 test_input = { "nums": [9,3,3,12,9,12,5,7,6,2,9,9,14,9,5] } assert my_solution.maximumTripletValue(**test_input) == 140 test_input = { "nums": [19,14,15,1,20,10,20,4,10,20,15,15,2,7] } assert my_solution.maximumTripletValue(**test_input) == 360 test_input = { "nums": [17,4,10,16,8,20,4,9,11,15,2,7] } assert my_solution.maximumTripletValue(**test_input) == 260 test_input = { "nums": [3,8,17,10,10,20,20,8,14,20,1,10,1] } assert my_solution.maximumTripletValue(**test_input) == 240 test_input = { "nums": [3,4,11,18,10,19,9,11,14,11,18,15,17,19,3] } assert my_solution.maximumTripletValue(**test_input) == 190 test_input = { "nums": [18,10,5,16,13,1,19,10,17,14,14,20] } assert my_solution.maximumTripletValue(**test_input) == 340 test_input = { "nums": [18,3,16,14,15,9,13,2,3] } assert my_solution.maximumTripletValue(**test_input) == 240 test_input = { "nums": [2,6,19,10,19,14,18,8,3,2] } assert my_solution.maximumTripletValue(**test_input) == 171
1,696,127,400
weekly-contest-365-minimum-size-subarray-in-infinite-array
https://leetcode.com/problems/minimum-size-subarray-in-infinite-array
minimum-size-subarray-in-infinite-array
{ "questionId": "3141", "questionFrontendId": "2875", "title": "Minimum Size Subarray in Infinite Array", "titleSlug": "minimum-size-subarray-in-infinite-array", "isPaidOnly": false, "difficulty": "Medium", "likes": 309, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的数组 nums 和一个整数 target 。 下标从 0 开始的数组 infinite_nums 是通过无限地将 nums 的元素追加到自己之后生成的。 请你从 infinite_nums 中找出满足 元素和 等于 target 的 最短 子数组,并返回该子数组的长度。如果不存在满足条件的子数组,返回 -1 。 示例 1: 输入:nums = [1,2,3], target = 5 输出:2 解释:在这个例子中 infinite_nums = [1,2,3,1,2,3,1,2,...] 。 区间 [1,2] 内的子数组的元素和等于 target = 5 ,且长度 length = 2 。 可以证明,当元素和等于目标值 target = 5 时,2 是子数组的最短长度。 示例 2: 输入:nums = [1,1,1,2,3], target = 4 输出:2 解释:在这个例子中 infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...]. 区间 [4,5] 内的子数组的元素和等于 target = 4 ,且长度 length = 2 。 可以证明,当元素和等于目标值 target = 4 时,2 是子数组的最短长度。 示例 3: 输入:nums = [2,4,6,8], target = 3 输出:-1 解释:在这个例子中 infinite_nums = [2,4,6,8,2,4,6,8,...] 。 可以证明,不存在元素和等于目标值 target = 3 的子数组。 提示: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 105 * 1 <= target <= 109 """ class Solution: def minSizeSubarray(self, nums: List[int], target: int) -> int:
给你一个下标从 0 开始的数组 nums 和一个整数 target 。 下标从 0 开始的数组 infinite_nums 是通过无限地将 nums 的元素追加到自己之后生成的。 请你从 infinite_nums 中找出满足 元素和 等于 target 的 最短 子数组,并返回该子数组的长度。如果不存在满足条件的子数组,返回 -1 。 示例 1: 输入:nums = [1,2,3], target = 5 输出:2 解释:在这个例子中 infinite_nums = [1,2,3,1,2,3,1,2,...] 。 区间 [1,2] 内的子数组的元素和等于 target = 5 ,且长度 length = 2 。 可以证明,当元素和等于目标值 target = 5 时,2 是子数组的最短长度。 示例 2: 输入:nums = [1,1,1,2,3], target = 4 输出:2 解释:在这个例子中 infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...]. 区间 [4,5] 内的子数组的元素和等于 target = 4 ,且长度 length = 2 。 可以证明,当元素和等于目标值 target = 4 时,2 是子数组的最短长度。 示例 3: 输入:nums = [2,4,6,8], target = 3 输出:-1 解释:在这个例子中 infinite_nums = [2,4,6,8,2,4,6,8,...] 。 可以证明,不存在元素和等于目标值 target = 3 的子数组。 提示: * 1 <= nums.length <= 105 * 1 <= nums[i] <= 105 * 1 <= target <= 109 请完成下面的代码来解决上述问题: ```python class Solution: def minSizeSubarray(self, nums: List[int], target: int) -> int: ```
my_solution = Solution() test_input = { "nums": [1,2,3], "target": 5 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [1,1,1,2,3], "target": 4 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [2,4,6,8], "target": 3 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [2,1,5,7,7,1,6,3], "target": 39 } assert my_solution.minSizeSubarray(**test_input) == 9 test_input = { "nums": [17,4,3,14,17,6,15], "target": 85 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [18,3,11,19,7,16,6,7,3,6,18,9,9,1,14,17,15,14,12,10], "target": 7 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [2,3,5,2,3,4,4,1,3,5,2,2,5,1,1,2,5], "target": 19 } assert my_solution.minSizeSubarray(**test_input) == 6 test_input = { "nums": [4,1,5,7,1,6,1,7,2,2,5,5,5,6,3], "target": 20 } assert my_solution.minSizeSubarray(**test_input) == 5 test_input = { "nums": [7,3,5], "target": 36 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [1,11,6,4,13], "target": 22 } assert my_solution.minSizeSubarray(**test_input) == 4 test_input = { "nums": [1,2,2,2,1,2,1,2,1,2,1], "target": 83 } assert my_solution.minSizeSubarray(**test_input) == 53 test_input = { "nums": [4,3,5,4,5,4,4,4,5,7,4,5,6,3,1,4,6,3,7], "target": 15 } assert my_solution.minSizeSubarray(**test_input) == 3 test_input = { "nums": [1,2,3,2,1,5,3,4,5], "target": 53 } assert my_solution.minSizeSubarray(**test_input) == 19 test_input = { "nums": [2,5,6,4], "target": 95 } assert my_solution.minSizeSubarray(**test_input) == 22 test_input = { "nums": [6,6,4,5,2,8,1,8,7,6,6,7,4,1,9,6,8,8], "target": 55 } assert my_solution.minSizeSubarray(**test_input) == 9 test_input = { "nums": [1,2,8,19,17,2,3,11,8,12,16,18,7], "target": 36 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [12,14,4,14,13,16,5], "target": 36 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], "target": 37 } assert my_solution.minSizeSubarray(**test_input) == 37 test_input = { "nums": [5,7,2,6,4,1,6,7,1,4,7,6,7,7,6,6,4,6,8], "target": 90 } assert my_solution.minSizeSubarray(**test_input) == 17 test_input = { "nums": [3,5,15,17,6,17,10,15,10,4,6], "target": 25 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [14,5], "target": 23 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [1,1,5,9], "target": 68 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [11,1,17,14,9,16,5,3,7,16,14,18,17,10], "target": 82 } assert my_solution.minSizeSubarray(**test_input) == 6 test_input = { "nums": [9,6,8,4,3,4,6,4,7,2,6,9,2,4,5,4], "target": 71 } assert my_solution.minSizeSubarray(**test_input) == 14 test_input = { "nums": [2,4,4,3,2,3,2,5,3,1,5,1,4,2,6], "target": 23 } assert my_solution.minSizeSubarray(**test_input) == 7 test_input = { "nums": [3,6], "target": 66 } assert my_solution.minSizeSubarray(**test_input) == 15 test_input = { "nums": [1,4,8,5,9,8,8,2,3,1,6,2,7,5,5,3,3,5,6], "target": 57 } assert my_solution.minSizeSubarray(**test_input) == 10 test_input = { "nums": [1,6,5,5,1,1,2,5,3,1,5,3,2,4,6,6], "target": 56 } assert my_solution.minSizeSubarray(**test_input) == 16 test_input = { "nums": [5,3,5,4,3,1,3,3,1,3,3,5,5,4,5,5,5,5], "target": 8 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [2,2,1,3,2,2,2,3,3,2,1,3,3,2,3,3], "target": 93 } assert my_solution.minSizeSubarray(**test_input) == 40 test_input = { "nums": [5,1,4,1,5,6], "target": 71 } assert my_solution.minSizeSubarray(**test_input) == 19 test_input = { "nums": [3,5,6,6,1,8,4,9,6,2,3,9,6,8,7,3,6,1,8,6], "target": 60 } assert my_solution.minSizeSubarray(**test_input) == 11 test_input = { "nums": [12,15,9,3,3,12,13,14,7,11,7,15,12,5,11], "target": 18 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [3,11,10,12,9,13,9], "target": 19 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [3,4,4], "target": 35 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [4,5,2,5,5,5,1], "target": 87 } assert my_solution.minSizeSubarray(**test_input) == 23 test_input = { "nums": [2,13,15,3,6,7,16,7,9,10,4,3,12,9,13,2,9,13,15], "target": 4 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [1,2,16,10,15,15,13,11,10,6,12,15,9], "target": 30 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [3,5], "target": 85 } assert my_solution.minSizeSubarray(**test_input) == 21 test_input = { "nums": [1,4,3,1,4,4,2,3], "target": 6 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [2,10,12,10,4,4,12,5,12,12,5], "target": 33 } assert my_solution.minSizeSubarray(**test_input) == 4 test_input = { "nums": [5,9,7,10,4,7,9,11,6,3,1,8,6,1,11,1,1], "target": 72 } assert my_solution.minSizeSubarray(**test_input) == 11 test_input = { "nums": [19,18,6], "target": 56 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [3,5,7,6,5,3,7,7,1,5,3,1,5,6,3,1,6,1,3], "target": 20 } assert my_solution.minSizeSubarray(**test_input) == 4 test_input = { "nums": [5,5,4,1,2,2,2,3,2,4,2,5], "target": 56 } assert my_solution.minSizeSubarray(**test_input) == 16 test_input = { "nums": [1,1,1,1,1,1,1,1], "target": 22 } assert my_solution.minSizeSubarray(**test_input) == 22 test_input = { "nums": [1,2], "target": 72 } assert my_solution.minSizeSubarray(**test_input) == 48 test_input = { "nums": [4,3,6,6,2,6,1,6,7,5,7,6,1,5,7], "target": 82 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [4,1,5,2,3,1,2,4,1,5,3,3,5,2,6,6,5,2,1], "target": 63 } assert my_solution.minSizeSubarray(**test_input) == 20 test_input = { "nums": [8,2,5,4,1,6,6,6,6,4,4,5,5,9,6,6,9,2], "target": 4 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [18,12,13,9,17,11], "target": 82 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [13,3,1,5,13,7,12,5], "target": 35 } assert my_solution.minSizeSubarray(**test_input) == 5 test_input = { "nums": [4,10,12,6,2,2,4,12,6,1,1,2,2,10,6,11,5,4,9], "target": 49 } assert my_solution.minSizeSubarray(**test_input) == 7 test_input = { "nums": [8], "target": 68 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [7,2,6,7,6,4,4,1,6,4,1,7,7,2,2,4,4,4], "target": 29 } assert my_solution.minSizeSubarray(**test_input) == 6 test_input = { "nums": [4,7,6,12,10,13,7,6,6,1,15,2,4,8,12], "target": 43 } assert my_solution.minSizeSubarray(**test_input) == 6 test_input = { "nums": [4,10], "target": 10 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [4,3,2,4,5,3,7,12,2,2,10], "target": 43 } assert my_solution.minSizeSubarray(**test_input) == 8 test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1], "target": 58 } assert my_solution.minSizeSubarray(**test_input) == 58 test_input = { "nums": [1,1,1,1,1], "target": 20 } assert my_solution.minSizeSubarray(**test_input) == 20 test_input = { "nums": [4,3], "target": 23 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [3,2,1,3,2,1,3,1,1,1,2,1,2,1,2,3,3,1], "target": 78 } assert my_solution.minSizeSubarray(**test_input) == 41 test_input = { "nums": [3,2,4,2,4,2,5,4,5,3,4,4,2,4,4,1], "target": 19 } assert my_solution.minSizeSubarray(**test_input) == 5 test_input = { "nums": [17], "target": 1 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [10,12,1,11,9,5,4,5,1,10,8,12,5,4], "target": 82 } assert my_solution.minSizeSubarray(**test_input) == 10 test_input = { "nums": [6], "target": 44 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [4,6,2,6,3,5,2,5,5,4,3,1,5,4,5,5,4,5,5,6], "target": 12 } assert my_solution.minSizeSubarray(**test_input) == 3 test_input = { "nums": [14,4,13,12,18,8,4,15,4,14,17,4,2], "target": 8 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [4,4,6,5,3,4,1,4,2,6,3], "target": 32 } assert my_solution.minSizeSubarray(**test_input) == 9 test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], "target": 28 } assert my_solution.minSizeSubarray(**test_input) == 28 test_input = { "nums": [5,10,1,3,14,7,13,6,5,7,10,3,10,5,8,5,7,5,6,7], "target": 25 } assert my_solution.minSizeSubarray(**test_input) == 4 test_input = { "nums": [12,3,4,10,5,8,12,7,12,7,5,8,4,8,11,11], "target": 48 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [3,11,5,5,3,10,12,12,12,3,10], "target": 88 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [8,2,10,5], "target": 28 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [3,5,1,4,5,2,5,3,1,2,1,1,1,3,3,3,5], "target": 68 } assert my_solution.minSizeSubarray(**test_input) == 23 test_input = { "nums": [1,4,1,4,4], "target": 21 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [15,8,8,19,8,12,15,3,15,8,10,9], "target": 77 } assert my_solution.minSizeSubarray(**test_input) == 7 test_input = { "nums": [4,4], "target": 80 } assert my_solution.minSizeSubarray(**test_input) == 20 test_input = { "nums": [4,9,3,7,5,4,5,1,3,5], "target": 69 } assert my_solution.minSizeSubarray(**test_input) == 14 test_input = { "nums": [2,8,2,8,1,5,8,9,3,4,6,6,6,1,7,9], "target": 93 } assert my_solution.minSizeSubarray(**test_input) == 17 test_input = { "nums": [7,11,14,12,3,16,11,9], "target": 10 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [2,1,2,1,3,2,1,1,2], "target": 17 } assert my_solution.minSizeSubarray(**test_input) == 10 test_input = { "nums": [5,20,18,2,8], "target": 45 } assert my_solution.minSizeSubarray(**test_input) == 4 test_input = { "nums": [2,1,2,1,2,1,1,1,2,2], "target": 58 } assert my_solution.minSizeSubarray(**test_input) == 38 test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], "target": 17 } assert my_solution.minSizeSubarray(**test_input) == 17 test_input = { "nums": [18,6,8,17,3,10,14,12,4,13,12,10,5,18,11], "target": 95 } assert my_solution.minSizeSubarray(**test_input) == 9 test_input = { "nums": [19,12,14], "target": 57 } assert my_solution.minSizeSubarray(**test_input) == 4 test_input = { "nums": [3,13,14], "target": 11 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [17,6,8,7,4,6,6,3,8,1,8,10,18,13,2], "target": 32 } assert my_solution.minSizeSubarray(**test_input) == 3 test_input = { "nums": [3,12,4,9,5,2,2,9,9,6,9,11,9], "target": 41 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [3,14,2,9,5,14,15,4,3,4,17,11], "target": 3 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [2,5,3,6,3,6,1,1,5,1], "target": 37 } assert my_solution.minSizeSubarray(**test_input) == -1 test_input = { "nums": [13,6,4,7,3,6,4,10,13,10,5,4,2,1,7,11,3,3,12], "target": 51 } assert my_solution.minSizeSubarray(**test_input) == 7 test_input = { "nums": [1,10,9,16,3,10,2,5,1,10], "target": 83 } assert my_solution.minSizeSubarray(**test_input) == 11 test_input = { "nums": [2,10,13,3,4,19,14,20,11,15,4,3,17,8,2,3,1,13,8], "target": 1 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [4,5,5,3,5,4,2,11,5,9,4,6], "target": 41 } assert my_solution.minSizeSubarray(**test_input) == 7 test_input = { "nums": [2,8,9,6,11,17,3,6,9,7,2,8,9,11,19], "target": 39 } assert my_solution.minSizeSubarray(**test_input) == 3 test_input = { "nums": [8,13,9,5,8,6,17,16,14,7,10,15,16], "target": 8 } assert my_solution.minSizeSubarray(**test_input) == 1 test_input = { "nums": [10,20,16,1,11,18,13,6,13,6,9,14,16,12,13,7,19], "target": 59 } assert my_solution.minSizeSubarray(**test_input) == 5 test_input = { "nums": [1,1,1,1,1,1,1,1,1,1,1,1], "target": 6 } assert my_solution.minSizeSubarray(**test_input) == 6
1,696,127,400
weekly-contest-365-count-visited-nodes-in-a-directed-graph
https://leetcode.com/problems/count-visited-nodes-in-a-directed-graph
count-visited-nodes-in-a-directed-graph
{ "questionId": "3140", "questionFrontendId": "2876", "title": "Count Visited Nodes in a Directed Graph", "titleSlug": "count-visited-nodes-in-a-directed-graph", "isPaidOnly": false, "difficulty": "Hard", "likes": 274, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 现有一个有向图,其中包含 n 个节点,节点编号从 0 到 n - 1 。此外,该图还包含了 n 条有向边。 给你一个下标从 0 开始的数组 edges ,其中 edges[i] 表示存在一条从节点 i 到节点 edges[i] 的边。 想象在图上发生以下过程: * 你从节点 x 开始,通过边访问其他节点,直到你在 此过程 中再次访问到之前已经访问过的节点。 返回数组 answer 作为答案,其中 answer[i] 表示如果从节点 i 开始执行该过程,你可以访问到的不同节点数。 示例 1: [https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.png] 输入:edges = [1,2,0,0] 输出:[3,3,3,4] 解释:从每个节点开始执行该过程,记录如下: - 从节点 0 开始,访问节点 0 -> 1 -> 2 -> 0 。访问的不同节点数是 3 。 - 从节点 1 开始,访问节点 1 -> 2 -> 0 -> 1 。访问的不同节点数是 3 。 - 从节点 2 开始,访问节点 2 -> 0 -> 1 -> 2 。访问的不同节点数是 3 。 - 从节点 3 开始,访问节点 3 -> 0 -> 1 -> 2 -> 0 。访问的不同节点数是 4 。 示例 2: [https://assets.leetcode.com/uploads/2023/08/31/graaph2drawio.png] 输入:edges = [1,2,3,4,0] 输出:[5,5,5,5,5] 解释:无论从哪个节点开始,在这个过程中,都可以访问到图中的每一个节点。 提示: * n == edges.length * 2 <= n <= 105 * 0 <= edges[i] <= n - 1 * edges[i] != i """ class Solution: def countVisitedNodes(self, edges: List[int]) -> List[int]:
现有一个有向图,其中包含 n 个节点,节点编号从 0 到 n - 1 。此外,该图还包含了 n 条有向边。 给你一个下标从 0 开始的数组 edges ,其中 edges[i] 表示存在一条从节点 i 到节点 edges[i] 的边。 想象在图上发生以下过程: * 你从节点 x 开始,通过边访问其他节点,直到你在 此过程 中再次访问到之前已经访问过的节点。 返回数组 answer 作为答案,其中 answer[i] 表示如果从节点 i 开始执行该过程,你可以访问到的不同节点数。 示例 1: [https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.png] 输入:edges = [1,2,0,0] 输出:[3,3,3,4] 解释:从每个节点开始执行该过程,记录如下: - 从节点 0 开始,访问节点 0 -> 1 -> 2 -> 0 。访问的不同节点数是 3 。 - 从节点 1 开始,访问节点 1 -> 2 -> 0 -> 1 。访问的不同节点数是 3 。 - 从节点 2 开始,访问节点 2 -> 0 -> 1 -> 2 。访问的不同节点数是 3 。 - 从节点 3 开始,访问节点 3 -> 0 -> 1 -> 2 -> 0 。访问的不同节点数是 4 。 示例 2: [https://assets.leetcode.com/uploads/2023/08/31/graaph2drawio.png] 输入:edges = [1,2,3,4,0] 输出:[5,5,5,5,5] 解释:无论从哪个节点开始,在这个过程中,都可以访问到图中的每一个节点。 提示: * n == edges.length * 2 <= n <= 105 * 0 <= edges[i] <= n - 1 * edges[i] != i 请完成下面的代码来解决上述问题: ```python class Solution: def countVisitedNodes(self, edges: List[int]) -> List[int]: ```
my_solution = Solution() test_input = { "edges": [1,2,0,0] } assert my_solution.countVisitedNodes(**test_input) == [3,3,3,4] test_input = { "edges": [1,2,3,4,0] } assert my_solution.countVisitedNodes(**test_input) == [5,5,5,5,5] test_input = { "edges": [3,6,1,0,5,7,4,3] } assert my_solution.countVisitedNodes(**test_input) == [2,7,8,2,5,4,6,3] test_input = { "edges": [7,0,7,0,5,3,3,0] } assert my_solution.countVisitedNodes(**test_input) == [2,3,3,3,5,4,4,2] test_input = { "edges": [6,3,6,1,0,8,0,6,6] } assert my_solution.countVisitedNodes(**test_input) == [2,2,3,2,3,4,2,3,3] test_input = { "edges": [8,17,14,8,14,12,16,11,4,14,19,6,8,8,2,10,2,1,1,18] } assert my_solution.countVisitedNodes(**test_input) == [5,2,2,5,3,6,4,6,4,3,5,5,5,5,2,6,3,2,3,4] test_input = { "edges": [11,9,6,8,3,2,8,11,14,2,3,7,2,2,1] } assert my_solution.countVisitedNodes(**test_input) == [3,6,6,7,8,7,6,2,6,6,8,2,7,7,6] test_input = { "edges": [9,4,4,8,5,2,3,6,5,5] } assert my_solution.countVisitedNodes(**test_input) == [5,4,3,5,3,3,6,7,4,4] test_input = { "edges": [1,0,1,1] } assert my_solution.countVisitedNodes(**test_input) == [2,2,3,3] test_input = { "edges": [4,0,3,2,3] } assert my_solution.countVisitedNodes(**test_input) == [4,5,2,2,3] test_input = { "edges": [7,7,0,9,5,6,10,16,7,4,15,13,2,16,1,7,6] } assert my_solution.countVisitedNodes(**test_input) == [6,6,7,9,7,6,5,5,6,8,5,7,8,6,7,5,5] test_input = { "edges": [2,6,3,1,5,3,5] } assert my_solution.countVisitedNodes(**test_input) == [6,4,5,4,5,4,4] test_input = { "edges": [15,4,13,12,12,2,11,6,14,10,15,3,5,5,2,4] } assert my_solution.countVisitedNodes(**test_input) == [7,6,3,5,5,3,7,8,5,8,7,6,4,3,4,6] test_input = { "edges": [1,5,0,5,2,7,1,2] } assert my_solution.countVisitedNodes(**test_input) == [5,5,5,6,6,5,6,5] test_input = { "edges": [9,6,13,1,11,4,17,9,2,18,15,4,14,15,7,2,18,16,1] } assert my_solution.countVisitedNodes(**test_input) == [7,5,3,6,2,3,5,7,4,6,4,2,9,3,8,3,5,5,5] test_input = { "edges": [18,18,4,6,1,8,14,4,16,11,13,6,10,10,6,18,14,11,4] } assert my_solution.countVisitedNodes(**test_input) == [4,3,4,3,3,5,2,4,4,4,2,3,3,2,2,4,3,4,3] test_input = { "edges": [5,4,1,6,3,10,3,10,11,10,8,1] } assert my_solution.countVisitedNodes(**test_input) == [9,4,5,2,3,8,2,8,6,8,7,5] test_input = { "edges": [8,6,3,1,0,6,8,1,4,7,8] } assert my_solution.countVisitedNodes(**test_input) == [3,5,7,6,3,5,4,6,3,7,4] test_input = { "edges": [9,5,18,15,8,4,3,3,18,5,13,0,1,18,9,6,18,9,14,15] } assert my_solution.countVisitedNodes(**test_input) == [7,7,7,3,6,6,3,4,6,6,8,8,8,7,6,3,7,7,6,4] test_input = { "edges": [5,2,1,0,6,9,10,12,12,2,16,2,9,17,0,4,9,6] } assert my_solution.countVisitedNodes(**test_input) == [5,2,2,6,7,4,6,5,5,3,5,3,4,8,6,8,4,7] test_input = { "edges": [6,4,1,2,3,2,0] } assert my_solution.countVisitedNodes(**test_input) == [2,4,4,4,4,5,2] test_input = { "edges": [1,13,4,12,15,11,1,8,15,10,1,3,0,3,2,2] } assert my_solution.countVisitedNodes(**test_input) == [5,5,3,5,3,7,6,5,4,7,6,6,5,5,4,3] test_input = { "edges": [2,2,0] } assert my_solution.countVisitedNodes(**test_input) == [2,3,2] test_input = { "edges": [11,8,8,11,5,8,9,11,6,8,0,12,9,12] } assert my_solution.countVisitedNodes(**test_input) == [6,4,4,6,5,4,3,6,3,3,7,5,4,5] test_input = { "edges": [2,3,6,8,0,4,8,6,1] } assert my_solution.countVisitedNodes(**test_input) == [6,3,5,3,7,8,4,5,3] test_input = { "edges": [2,7,17,14,3,14,11,12,9,0,15,18,1,18,0,19,11,4,1,0] } assert my_solution.countVisitedNodes(**test_input) == [6,3,6,6,6,7,6,3,8,7,9,5,3,5,6,8,6,6,4,7] test_input = { "edges": [5,17,10,13,16,4,7,10,19,6,15,6,9,0,1,0,12,18,10,16] } assert my_solution.countVisitedNodes(**test_input) == [10,13,11,12,10,10,10,10,12,10,10,11,10,11,14,10,10,12,11,11] test_input = { "edges": [1,2,6,6,1,4,4] } assert my_solution.countVisitedNodes(**test_input) == [5,4,4,5,4,5,4] test_input = { "edges": [2,0,0,2] } assert my_solution.countVisitedNodes(**test_input) == [2,3,2,3] test_input = { "edges": [12,10,5,0,12,8,0,4,3,1,9,4,6] } assert my_solution.countVisitedNodes(**test_input) == [3,3,7,4,4,6,3,5,5,3,3,5,3] test_input = { "edges": [8,4,0,0,8,2,3,8,7] } assert my_solution.countVisitedNodes(**test_input) == [3,4,4,4,3,5,5,2,2] test_input = { "edges": [6,7,1,10,2,10,3,5,10,4,2] } assert my_solution.countVisitedNodes(**test_input) == [8,5,5,6,6,5,7,5,6,7,5] test_input = { "edges": [2,7,5,4,8,7,2,3,0] } assert my_solution.countVisitedNodes(**test_input) == [7,8,7,7,7,7,8,7,7] test_input = { "edges": [2,3,1,1,0,4] } assert my_solution.countVisitedNodes(**test_input) == [4,2,3,2,5,6] test_input = { "edges": [5,2,3,1,3,1] } assert my_solution.countVisitedNodes(**test_input) == [5,3,3,3,4,4] test_input = { "edges": [7,6,12,0,1,9,13,6,9,6,0,0,3,9,12,13,0] } assert my_solution.countVisitedNodes(**test_input) == [5,4,8,6,5,4,3,4,4,3,6,6,7,3,8,4,6] test_input = { "edges": [1,4,9,11,11,11,14,10,11,14,2,0,14,5,10] } assert my_solution.countVisitedNodes(**test_input) == [4,4,4,5,4,5,5,5,5,4,4,4,5,6,4] test_input = { "edges": [4,3,3,1,3] } assert my_solution.countVisitedNodes(**test_input) == [4,2,3,2,3] test_input = { "edges": [7,7,6,8,0,7,8,0,0,10,9] } assert my_solution.countVisitedNodes(**test_input) == [2,3,5,4,3,3,4,2,3,2,2] test_input = { "edges": [16,5,11,9,7,17,16,8,14,5,5,1,0,8,0,16,14,15,19,4] } assert my_solution.countVisitedNodes(**test_input) == [3,7,9,8,6,6,4,5,4,7,7,8,4,5,3,4,3,5,8,7] test_input = { "edges": [1,8,10,6,2,1,8,9,6,12,5,10,3] } assert my_solution.countVisitedNodes(**test_input) == [4,3,6,3,7,4,2,6,2,5,5,6,4] test_input = { "edges": [4,0,1,5,0,2] } assert my_solution.countVisitedNodes(**test_input) == [2,3,4,6,2,5] test_input = { "edges": [9,13,1,2,13,1,0,5,10,8,2,2,3,12] } assert my_solution.countVisitedNodes(**test_input) == [9,5,5,5,6,6,10,7,7,8,6,6,5,5] test_input = { "edges": [12,13,16,11,17,11,2,15,12,14,4,9,3,4,17,3,4,9] } assert my_solution.countVisitedNodes(**test_input) == [7,6,6,5,4,5,7,7,7,3,5,4,6,5,3,6,5,3] test_input = { "edges": [2,0,1] } assert my_solution.countVisitedNodes(**test_input) == [3,3,3] test_input = { "edges": [7,10,15,18,7,1,7,16,11,8,2,13,13,15,16,0,18,5,16] } assert my_solution.countVisitedNodes(**test_input) == [4,8,6,3,4,9,4,3,8,9,7,7,7,6,3,5,2,10,2] test_input = { "edges": [1,2,0] } assert my_solution.countVisitedNodes(**test_input) == [3,3,3] test_input = { "edges": [10,13,4,11,11,6,9,2,7,4,5,7,0,11] } assert my_solution.countVisitedNodes(**test_input) == [9,6,4,5,4,7,6,4,5,5,8,4,10,5] test_input = { "edges": [12,15,15,2,10,1,5,6,1,2,11,13,10,4,2,0,1] } assert my_solution.countVisitedNodes(**test_input) == [6,8,8,9,4,9,10,11,9,9,4,4,5,4,9,7,9] test_input = { "edges": [1,0] } assert my_solution.countVisitedNodes(**test_input) == [2,2] test_input = { "edges": [7,17,3,7,7,12,15,1,14,15,16,3,13,0,3,8,0,11,1] } assert my_solution.countVisitedNodes(**test_input) == [6,5,6,5,6,9,9,5,7,9,8,5,8,7,6,8,7,5,6] test_input = { "edges": [9,4,6,2,8,6,7,4,9,8,0,2,7] } assert my_solution.countVisitedNodes(**test_input) == [3,4,6,7,3,6,5,4,2,2,4,7,5] test_input = { "edges": [13,10,6,12,12,3,4,3,10,12,1,7,8,12,9] } assert my_solution.countVisitedNodes(**test_input) == [6,2,7,5,5,6,6,6,3,5,2,7,4,5,6] test_input = { "edges": [10,3,4,4,5,7,2,9,7,0,5] } assert my_solution.countVisitedNodes(**test_input) == [5,8,7,7,6,5,8,5,6,5,5] test_input = { "edges": [2,4,0,4,2] } assert my_solution.countVisitedNodes(**test_input) == [2,4,2,4,3] test_input = { "edges": [2,2,1] } assert my_solution.countVisitedNodes(**test_input) == [3,2,2] test_input = { "edges": [19,15,1,6,8,15,5,6,4,4,19,13,3,0,15,10,13,5,6,3] } assert my_solution.countVisitedNodes(**test_input) == [7,7,8,6,2,6,6,7,2,3,6,9,7,8,7,6,9,7,7,6] test_input = { "edges": [11,9,5,0,5,3,9,8,1,10,4,4] } assert my_solution.countVisitedNodes(**test_input) == [5,8,6,5,5,5,8,10,9,7,6,5] test_input = { "edges": [13,10,12,11,5,17,0,10,7,16,5,4,9,3,15,5,4,1] } assert my_solution.countVisitedNodes(**test_input) == [9,4,9,7,5,4,10,5,6,7,4,6,8,8,6,5,6,4] test_input = { "edges": [7,0,9,0,7,6,2,0,7,7] } assert my_solution.countVisitedNodes(**test_input) == [2,3,4,3,3,6,5,2,3,3] test_input = { "edges": [1,0,0] } assert my_solution.countVisitedNodes(**test_input) == [2,2,3] test_input = { "edges": [5,9,10,17,12,3,15,5,0,3,15,5,5,15,17,5,13,15,1] } assert my_solution.countVisitedNodes(**test_input) == [5,6,6,4,6,4,5,5,6,5,5,5,5,5,5,4,6,4,7] test_input = { "edges": [1,5,1,8,1,0,1,4,6] } assert my_solution.countVisitedNodes(**test_input) == [3,3,4,6,4,3,4,5,5] test_input = { "edges": [5,3,3,4,1,4] } assert my_solution.countVisitedNodes(**test_input) == [5,3,4,3,3,4] test_input = { "edges": [7,12,12,5,10,11,5,0,3,12,12,9,1,3,7] } assert my_solution.countVisitedNodes(**test_input) == [2,2,3,6,4,5,6,2,7,3,3,4,2,7,3] test_input = { "edges": [3,3,3,1] } assert my_solution.countVisitedNodes(**test_input) == [3,2,3,2] test_input = { "edges": [13,3,15,10,12,7,13,15,3,0,1,5,15,12,10,9] } assert my_solution.countVisitedNodes(**test_input) == [5,3,6,3,6,7,6,6,4,5,3,8,5,5,4,5] test_input = { "edges": [8,9,0,9,0,0,9,0,7,6,5,0] } assert my_solution.countVisitedNodes(**test_input) == [3,3,4,3,4,4,2,3,3,2,5,4] test_input = { "edges": [2,0,7,7,3,3,0,4,5,8] } assert my_solution.countVisitedNodes(**test_input) == [5,6,4,3,3,4,6,3,5,6] test_input = { "edges": [13,8,7,13,10,6,11,13,13,6,8,6,0,10] } assert my_solution.countVisitedNodes(**test_input) == [4,4,5,4,4,3,2,4,3,3,3,2,5,3] test_input = { "edges": [12,14,5,17,7,0,15,18,5,10,6,18,10,11,1,1,18,16,0] } assert my_solution.countVisitedNodes(**test_input) == [7,2,9,11,10,8,4,9,9,6,5,9,6,10,2,3,9,10,8] test_input = { "edges": [8,6,17,7,12,10,13,14,10,10,2,15,11,5,4,13,12,12] } assert my_solution.countVisitedNodes(**test_input) == [10,10,8,12,9,8,9,11,9,9,8,8,8,8,10,8,9,8] test_input = { "edges": [1,0,1,1,1] } assert my_solution.countVisitedNodes(**test_input) == [2,2,3,3,3] test_input = { "edges": [17,16,15,5,11,4,4,10,14,1,17,1,4,3,12,17,14,10] } assert my_solution.countVisitedNodes(**test_input) == [3,6,4,8,6,7,7,3,7,7,2,6,6,9,6,3,6,2] test_input = { "edges": [14,10,16,16,12,13,13,16,13,14,15,14,1,4,13,11,4,7] } assert my_solution.countVisitedNodes(**test_input) == [9,8,10,10,8,9,9,10,9,9,8,8,8,8,8,8,9,11] test_input = { "edges": [2,3,0,1] } assert my_solution.countVisitedNodes(**test_input) == [2,2,2,2] test_input = { "edges": [8,0,11,2,0,12,0,4,11,12,0,7,3] } assert my_solution.countVisitedNodes(**test_input) == [5,6,6,7,5,9,6,5,5,9,6,5,8] test_input = { "edges": [3,0,1,0] } assert my_solution.countVisitedNodes(**test_input) == [2,3,4,2] test_input = { "edges": [1,3,0,1,10,11,2,6,0,5,1,4] } assert my_solution.countVisitedNodes(**test_input) == [3,2,4,2,4,6,5,6,4,7,3,5] test_input = { "edges": [1,16,10,6,15,10,7,9,2,15,5,6,13,1,0,16,13] } assert my_solution.countVisitedNodes(**test_input) == [4,3,3,8,5,2,7,6,4,5,2,8,4,3,5,4,3] test_input = { "edges": [4,15,10,11,0,7,9,10,1,2,9,12,5,10,6,13] } assert my_solution.countVisitedNodes(**test_input) == [2,6,3,8,2,5,4,4,7,3,3,7,6,4,5,5] test_input = { "edges": [4,9,8,9,1,4,7,11,5,6,4,6,4,4,2] } assert my_solution.countVisitedNodes(**test_input) == [7,5,9,5,6,7,3,3,8,4,7,3,7,7,10] test_input = { "edges": [16,14,1,6,6,1,1,15,16,16,13,14,9,3,3,11,10] } assert my_solution.countVisitedNodes(**test_input) == [8,4,5,4,5,5,4,7,8,8,6,5,9,5,4,6,7] test_input = { "edges": [6,11,15,7,5,8,11,5,14,2,6,0,8,5,13,5] } assert my_solution.countVisitedNodes(**test_input) == [3,4,6,6,5,4,3,5,4,7,4,3,5,4,4,5] test_input = { "edges": [14,17,11,8,5,4,10,17,3,3,2,17,13,1,17,5,5,0] } assert my_solution.countVisitedNodes(**test_input) == [3,4,5,2,2,2,7,4,2,3,6,4,6,5,3,3,3,3] test_input = { "edges": [1,6,8,6,2,4,2,5,9,3] } assert my_solution.countVisitedNodes(**test_input) == [7,6,5,5,6,7,5,8,5,5] test_input = { "edges": [1,5,4,1,5,3,4,0] } assert my_solution.countVisitedNodes(**test_input) == [4,3,5,3,4,3,5,5] test_input = { "edges": [17,4,3,7,3,1,15,15,13,18,4,14,1,10,13,1,0,15,16] } assert my_solution.countVisitedNodes(**test_input) == [7,5,6,5,5,6,6,5,8,10,6,9,6,7,8,5,8,6,9] test_input = { "edges": [12,2,6,6,7,8,8,13,1,12,15,1,8,3,8,2,6] } assert my_solution.countVisitedNodes(**test_input) == [6,4,4,5,8,5,4,7,4,6,6,5,5,6,5,5,5] test_input = { "edges": [3,0,0,2] } assert my_solution.countVisitedNodes(**test_input) == [3,4,3,3] test_input = { "edges": [5,0,1,1,2,0] } assert my_solution.countVisitedNodes(**test_input) == [2,3,4,4,5,2] test_input = { "edges": [16,2,13,6,7,10,1,1,7,14,7,13,9,16,4,8,15] } assert my_solution.countVisitedNodes(**test_input) == [8,7,7,9,8,9,8,7,7,10,8,8,11,7,9,7,7] test_input = { "edges": [14,5,0,16,7,15,1,18,18,6,11,15,0,2,3,0,17,4,12] } assert my_solution.countVisitedNodes(**test_input) == [9,12,10,9,9,11,13,9,10,14,12,11,9,11,9,10,9,9,9] test_input = { "edges": [7,8,1,9,7,10,1,4,2,0,9] } assert my_solution.countVisitedNodes(**test_input) == [3,3,3,5,2,6,4,2,3,4,5] test_input = { "edges": [5,3,6,2,1,3,1,3] } assert my_solution.countVisitedNodes(**test_input) == [6,4,4,4,5,5,4,5] test_input = { "edges": [8,14,5,13,6,9,8,11,9,4,11,14,5,12,10,10,12] } assert my_solution.countVisitedNodes(**test_input) == [5,4,6,8,4,5,4,4,4,4,3,3,6,7,3,4,7] test_input = { "edges": [6,13,11,11,6,3,3,6,1,12,4,7,14,11,0,10] } assert my_solution.countVisitedNodes(**test_input) == [5,6,5,4,5,5,4,4,7,8,6,4,7,5,6,7] test_input = { "edges": [12,8,6,1,5,0,1,1,11,0,12,2,0] } assert my_solution.countVisitedNodes(**test_input) == [2,5,5,6,4,3,5,6,5,3,3,5,2] test_input = { "edges": [8,2,7,17,5,1,5,17,10,2,12,3,1,0,11,3,7,4] } assert my_solution.countVisitedNodes(**test_input) == [10,6,6,7,6,6,7,6,9,7,8,8,7,11,9,8,7,6] test_input = { "edges": [4,2,4,2,3] } assert my_solution.countVisitedNodes(**test_input) == [4,4,3,3,3]
1,696,127,400