Datasets:

Languages:
Chinese
ArXiv:
Tags:
code
License:
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

LeetCode Contest Benchmark

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

Usage

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

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

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

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

Citation

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