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 不 对应任何字母。 ...
给你一个字符串 word,由 不同 小写英文字母组成。 电话键盘上的按键与 不同 小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 2 对应 ["a","b","c"],我们需要按一次键来输入 "a",按两次键来输入 "b",按三次键来输入 "c"。 现在允许你将编号为 2 到 9 的按键重新映射到 不同 字母集合。每个按键可以映射到 任意数量 的字母,但每个字母 必须 恰好 映射到 一个 按键上。你需要找到输入字符串 word 所需的 最少 按键次数。 返回重新映射按键后输入 word 所需的 最少 按键次数。 下面给出了一种电话键盘上字母到按键的映射作为示例。注意 1,*,# 和 0 不 对应任何字母。 示例 ...
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" } asse...
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...
给你三个 正整数 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...
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,...
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 不 对应任何字母。 ...
给你一个字符串 word,由 不同 小写英文字母组成。 电话键盘上的按键与 不同 小写英文字母集合相映射,可以通过按压按键来组成单词。例如,按键 2 对应 ["a","b","c"],我们需要按一次键来输入 "a",按两次键来输入 "b",按三次键来输入 "c"。 现在允许你将编号为 2 到 9 的按键重新映射到 不同 字母集合。每个按键可以映射到 任意数量 的字母,但每个字母 必须 恰好 映射到 一个 按键上。你需要找到输入字符串 word 所需的 最少 按键次数。 返回重新映射按键后输入 word 所需的 最少 按键次数。 下面给出了一种电话键盘上字母到按键的映射作为示例。注意 1,*,# 和 0 不 对应任何字母。 示例 ...
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_inpu...
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...
给你三个 正整数 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...
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,...
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 。 示例 ...
给你一个长度为 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: ...
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] } ...
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] 和 n...
给你一个下标从 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[...
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": ...
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] ,然后...
给你一个下标从 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] ,然后删除下标...
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 = ...
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],...
给你一个下标从 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 =...
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_solutio...
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...
给你一个由 正整数 组成的数组 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 请完成下面的代码来解决...
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_in...
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 = "squi...
给你一个下标从 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...
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":...
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"...
""" 给你一个整数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" 。 ...
给你一个整数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...
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, ...
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 ...
给你一个下标从 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 输出:[...
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":...
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,宽度 =...
给你一个下标从 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。对...
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.areaOfMax...
1,704,594,600
weekly-contest-379-minimum-moves-to-capture-the-queen
https://leetcode.com/problems/minimum-moves-to-capture-the-queen
minimum-moves-to-capture-the-queen
{ "questionId": "3270", "questionFrontendId": "10036", "title": "Minimum Moves to Capture The Queen", "titleSlug": "minimum-moves-to-capture-the-queen", "isPaidOnly": false, "difficulty": "Medium", "likes": 71, "dislikes": 115, "categoryTitle": "Algorithms" }
""" 现有一个下标从 0 开始的 8 x 8 棋盘,上面有 3 枚棋子。 给你 6 个整数 a 、b 、c 、d 、e 和 f ,其中: (a, b) 表示白色车的位置。 (c, d) 表示白色象的位置。 (e, f) 表示黑皇后的位置。 假定你只能移动白色棋子,返回捕获黑皇后所需的最少移动次数。 请注意: 车可以向垂直或水平方向移动任意数量的格子,但不能跳过其他棋子。 象可以沿对角线方向移动任意数量的格子,但不能跳过其他棋子。 如果车或象能移向皇后所在的格子,则认为它们可以捕获皇后。 皇后不能移动。 示例 1: 输入:a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 输出:2 解释:...
现有一个下标从 0 开始的 8 x 8 棋盘,上面有 3 枚棋子。 给你 6 个整数 a 、b 、c 、d 、e 和 f ,其中: (a, b) 表示白色车的位置。 (c, d) 表示白色象的位置。 (e, f) 表示黑皇后的位置。 假定你只能移动白色棋子,返回捕获黑皇后所需的最少移动次数。 请注意: 车可以向垂直或水平方向移动任意数量的格子,但不能跳过其他棋子。 象可以沿对角线方向移动任意数量的格子,但不能跳过其他棋子。 如果车或象能移向皇后所在的格子,则认为它们可以捕获皇后。 皇后不能移动。 示例 1: 输入:a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 输出:2 解释:将白色车...
my_solution = Solution() test_input = { "a": 1, "b": 1, "c": 8, "d": 8, "e": 2, "f": 3 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 2 test_input = { "a": 5, "b": 3, "c": 3, "d": 4, "e": 5, "f": 2 } assert my_solution.minMovesToCaptureTheQueen(**test_input) == 1 test_input = { "a": 4, "b": 3, "c":...
1,704,594,600
weekly-contest-379-maximum-size-of-a-set-after-removals
https://leetcode.com/problems/maximum-size-of-a-set-after-removals
maximum-size-of-a-set-after-removals
{ "questionId": "3228", "questionFrontendId": "10037", "title": "Maximum Size of a Set After Removals", "titleSlug": "maximum-size-of-a-set-after-removals", "isPaidOnly": false, "difficulty": "Medium", "likes": 116, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,它们的长度都是偶数 n 。 你必须从 nums1 中移除 n / 2 个元素,同时从 nums2 中也移除 n / 2 个元素。移除之后,你将 nums1 和 nums2 中剩下的元素插入到集合 s 中。 返回集合 s可能的 最多 包含多少元素。 示例 1: 输入:nums1 = [1,2,1,2], nums2 = [1,1,1,1] 输出:2 解释:从 nums1 和 nums2 中移除两个 1 。移除后,数组变为 nums1 = [2,2] 和 nums2 = [1,1] 。因此,s = {1,2} 。 可以证明,在移除之后,集合 s 最多可以包含 ...
给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,它们的长度都是偶数 n 。 你必须从 nums1 中移除 n / 2 个元素,同时从 nums2 中也移除 n / 2 个元素。移除之后,你将 nums1 和 nums2 中剩下的元素插入到集合 s 中。 返回集合 s可能的 最多 包含多少元素。 示例 1: 输入:nums1 = [1,2,1,2], nums2 = [1,1,1,1] 输出:2 解释:从 nums1 和 nums2 中移除两个 1 。移除后,数组变为 nums1 = [2,2] 和 nums2 = [1,1] 。因此,s = {1,2} 。 可以证明,在移除之后,集合 s 最多可以包含 2 个元...
my_solution = Solution() test_input = { "nums1": [1,2,1,2], "nums2": [1,1,1,1] } assert my_solution.maximumSetSize(**test_input) == 2 test_input = { "nums1": [1,2,3,4,5,6], "nums2": [2,3,2,3,2,3] } assert my_solution.maximumSetSize(**test_input) == 5 test_input = { "nums1": [1,1,2,2,3,3], "nums2": [4,4,5,5,6,6] } a...
1,704,594,600
weekly-contest-379-maximize-the-number-of-partitions-after-operations
https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations
maximize-the-number-of-partitions-after-operations
{ "questionId": "3233", "questionFrontendId": "10038", "title": "Maximize the Number of Partitions After Operations", "titleSlug": "maximize-the-number-of-partitions-after-operations", "isPaidOnly": false, "difficulty": "Hard", "likes": 41, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的字符串s和一个整数k。 你需要执行以下分割操作,直到字符串s变为空: 选择s的最长前缀,该前缀最多包含k个不同字符。 删除这个前缀,并将分割数量加一。如果有剩余字符,它们在s中保持原来的顺序。 执行操作之 前 ,你可以将s中至多一处 下标的对应字符更改为另一个小写英文字母。 在最优选择情形下改变至多一处下标对应字符后,用整数表示并返回操作结束时得到的最大分割数量。 示例 1: 输入:s = "accca", k = 2 输出:3 解释:在此示例中,为了最大化得到的分割数量,可以将 s[2] 改为 'b'。 s 变为 "acbca"。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至...
给你一个下标从 0 开始的字符串s和一个整数k。 你需要执行以下分割操作,直到字符串s变为空: 选择s的最长前缀,该前缀最多包含k个不同字符。 删除这个前缀,并将分割数量加一。如果有剩余字符,它们在s中保持原来的顺序。 执行操作之 前 ,你可以将s中至多一处 下标的对应字符更改为另一个小写英文字母。 在最优选择情形下改变至多一处下标对应字符后,用整数表示并返回操作结束时得到的最大分割数量。 示例 1: 输入:s = "accca", k = 2 输出:3 解释:在此示例中,为了最大化得到的分割数量,可以将 s[2] 改为 'b'。 s 变为 "acbca"。 按照以下方式执行操作,直到 s 变为空: - 选择最长且至多包含 ...
my_solution = Solution() test_input = { "s": "accca", "k": 2 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 3 test_input = { "s": "aabaab", "k": 3 } assert my_solution.maxPartitionsAfterOperations(**test_input) == 1 test_input = { "s": "xxyz", "k": 1 } assert my_solution.maxPartitionsAfterOperat...
1,704,594,600
biweekly-contest-121-smallest-missing-integer-greater-than-sequential-prefix-sum
https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum
smallest-missing-integer-greater-than-sequential-prefix-sum
{ "questionId": "3236", "questionFrontendId": "10031", "title": "Smallest Missing Integer Greater Than Sequential Prefix Sum", "titleSlug": "smallest-missing-integer-greater-than-sequential-prefix-sum", "isPaidOnly": false, "difficulty": "Easy", "likes": 41, "dislikes": 122, "categoryTitle": "Algorith...
""" 给你一个下标从 0开始的整数数组nums。 如果一个前缀nums[0..i]满足对于1 <= j <= i的所有元素都有nums[j] = nums[j - 1] + 1,那么我们称这个前缀是一个 顺序前缀 。特殊情况是,只包含nums[0]的前缀也是一个 顺序前缀 。 请你返回 nums中没有出现过的 最小整数x,满足x大于等于最长 顺序前缀的和。 示例 1: 输入:nums = [1,2,3,2,5] 输出:6 解释:nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。 示例 2: 输入:nums = [3,4,5,1,12,14,13] 输出:...
给你一个下标从 0开始的整数数组nums。 如果一个前缀nums[0..i]满足对于1 <= j <= i的所有元素都有nums[j] = nums[j - 1] + 1,那么我们称这个前缀是一个 顺序前缀 。特殊情况是,只包含nums[0]的前缀也是一个 顺序前缀 。 请你返回 nums中没有出现过的 最小整数x,满足x大于等于最长 顺序前缀的和。 示例 1: 输入:nums = [1,2,3,2,5] 输出:6 解释:nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。 示例 2: 输入:nums = [3,4,5,1,12,14,13] 输出:15 解...
my_solution = Solution() test_input = { "nums": [1,2,3,2,5] } assert my_solution.missingInteger(**test_input) == 6 test_input = { "nums": [3,4,5,1,12,14,13] } assert my_solution.missingInteger(**test_input) == 15 test_input = { "nums": [29,30,31,32,33,34,35,36,37] } assert my_solution.missingInteger(**test_input) =...
1,704,551,400
biweekly-contest-121-minimum-number-of-operations-to-make-array-xor-equal-to-k
https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k
minimum-number-of-operations-to-make-array-xor-equal-to-k
{ "questionId": "3249", "questionFrontendId": "10032", "title": "Minimum Number of Operations to Make Array XOR Equal to K", "titleSlug": "minimum-number-of-operations-to-make-array-xor-equal-to-k", "isPaidOnly": false, "difficulty": "Medium", "likes": 56, "dislikes": 4, "categoryTitle": "Algorithms" ...
""" 给你一个下标从 0开始的整数数组nums和一个正整数k。 你可以对数组执行以下操作 任意次: 选择数组里的 任意一个元素,并将它的二进制表示翻转一个数位,翻转数位表示将0 变成1或者将 1变成 0。 你的目标是让数组里 所有元素的按位异或和得到 k,请你返回达成这一目标的 最少操作次数。 注意,你也可以将一个数的前导 0 翻转。比方说,数字(101)2翻转第四个数位,得到(1101)2。 示例 1: 输入:nums = [2,1,3,4], k = 1 输出:2 解释:我们可以执行以下操作: - 选择下标为 2 的元素,也就是 3 == (011)2,我们翻转第一个数位得到 (010)2 == 2 。数组变为 [2,...
给你一个下标从 0开始的整数数组nums和一个正整数k。 你可以对数组执行以下操作 任意次: 选择数组里的 任意一个元素,并将它的二进制表示翻转一个数位,翻转数位表示将0 变成1或者将 1变成 0。 你的目标是让数组里 所有元素的按位异或和得到 k,请你返回达成这一目标的 最少操作次数。 注意,你也可以将一个数的前导 0 翻转。比方说,数字(101)2翻转第四个数位,得到(1101)2。 示例 1: 输入:nums = [2,1,3,4], k = 1 输出:2 解释:我们可以执行以下操作: - 选择下标为 2 的元素,也就是 3 == (011)2,我们翻转第一个数位得到 (010)2 == 2 。数组变为 [2,1,2,...
my_solution = Solution() test_input = { "nums": [2,1,3,4], "k": 1 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [2,0,2,0], "k": 0 } assert my_solution.minOperations(**test_input) == 0 test_input = { "nums": [4], "k": 7 } assert my_solution.minOperations(**test_input) == 2 test_input ...
1,704,551,400
biweekly-contest-121-minimum-number-of-operations-to-make-x-and-y-equal
https://leetcode.com/problems/minimum-number-of-operations-to-make-x-and-y-equal
minimum-number-of-operations-to-make-x-and-y-equal
{ "questionId": "3239", "questionFrontendId": "10033", "title": "Minimum Number of Operations to Make X and Y Equal", "titleSlug": "minimum-number-of-operations-to-make-x-and-y-equal", "isPaidOnly": false, "difficulty": "Medium", "likes": 103, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你两个正整数x 和y。 一次操作中,你可以执行以下四种操作之一: 如果 x是 11的倍数,将x除以11。 如果 x是 5的倍数,将 x除以 5。 将x 减1。 将x加1。 请你返回让 x和 y相等的 最少操作次数。 示例 1: 输入:x = 26, y = 1 输出:3 解释:我们可以通过以下操作将 26 变为 1 : 1. 将 x 减 1 2. 将 x 除以 5 3. 将 x 除以 5 将 26 变为 1 最少需要 3 次操作。 示例 2: 输入:x = 54, y = 2 输出:4 解释:我们可以通过以下操作将 54 变为 2 : 1. 将 x 加 1 2. 将 x 除以 11 3. 将 x 除以 5 4....
给你两个正整数x 和y。 一次操作中,你可以执行以下四种操作之一: 如果 x是 11的倍数,将x除以11。 如果 x是 5的倍数,将 x除以 5。 将x 减1。 将x加1。 请你返回让 x和 y相等的 最少操作次数。 示例 1: 输入:x = 26, y = 1 输出:3 解释:我们可以通过以下操作将 26 变为 1 : 1. 将 x 减 1 2. 将 x 除以 5 3. 将 x 除以 5 将 26 变为 1 最少需要 3 次操作。 示例 2: 输入:x = 54, y = 2 输出:4 解释:我们可以通过以下操作将 54 变为 2 : 1. 将 x 加 1 2. 将 x 除以 11 3. 将 x 除以 5 4. 将 x...
my_solution = Solution() test_input = { "x": 26, "y": 1 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 3 test_input = { "x": 54, "y": 2 } assert my_solution.minimumOperationsToMakeEqual(**test_input) == 4 test_input = { "x": 25, "y": 30 } assert my_solution.minimumOperationsToMakeEqual(**test_in...
1,704,551,400
biweekly-contest-121-count-the-number-of-powerful-integers
https://leetcode.com/problems/count-the-number-of-powerful-integers
count-the-number-of-powerful-integers
{ "questionId": "3243", "questionFrontendId": "10034", "title": "Count the Number of Powerful Integers", "titleSlug": "count-the-number-of-powerful-integers", "isPaidOnly": false, "difficulty": "Hard", "likes": 53, "dislikes": 1, "categoryTitle": "Algorithms" }
""" 给你三个整数start,finish和limit。同时给你一个下标从0开始的字符串s,表示一个 正整数。 如果一个 正整数x 末尾部分是s(换句话说,s是 x的 后缀),且 x中的每个数位至多是 limit,那么我们称 x是 强大的。 请你返回区间[start..finish]内强大整数的总数目。 如果一个字符串 x是 y中某个下标开始(包括0),到下标为y.length - 1结束的子字符串,那么我们称x是y的一个后缀。比方说,25是5125的一个后缀,但不是512的后缀。 示例 1: 输入:start = 1, finish = 6000, limit = 4, s = "124" 输出:5 解释:区间 [1..60...
给你三个整数start,finish和limit。同时给你一个下标从0开始的字符串s,表示一个 正整数。 如果一个 正整数x 末尾部分是s(换句话说,s是 x的 后缀),且 x中的每个数位至多是 limit,那么我们称 x是 强大的。 请你返回区间[start..finish]内强大整数的总数目。 如果一个字符串 x是 y中某个下标开始(包括0),到下标为y.length - 1结束的子字符串,那么我们称x是y的一个后缀。比方说,25是5125的一个后缀,但不是512的后缀。 示例 1: 输入:start = 1, finish = 6000, limit = 4, s = "124" 输出:5 解释:区间 [1..6000] ...
my_solution = Solution() test_input = { "start": 1, "finish": 6000, "limit": 4, "s": "124" } assert my_solution.numberOfPowerfulInt(**test_input) == 5 test_input = { "start": 15, "finish": 215, "limit": 6, "s": "10" } assert my_solution.numberOfPowerfulInt(**test_input) == 2 test_input = { "start": 1000, "finish": ...
1,704,551,400
weekly-contest-378-check-if-bitwise-or-has-trailing-zeros
https://leetcode.com/problems/check-if-bitwise-or-has-trailing-zeros
check-if-bitwise-or-has-trailing-zeros
{ "questionId": "3246", "questionFrontendId": "2980", "title": "Check if Bitwise OR Has Trailing Zeros", "titleSlug": "check-if-bitwise-or-has-trailing-zeros", "isPaidOnly": false, "difficulty": "Easy", "likes": 68, "dislikes": 5, "categoryTitle": "Algorithms" }
""" 给你一个 正整数 数组 nums 。 你需要检查是否可以从数组中选出 两个或更多 元素,满足这些元素的按位或运算( OR)结果的二进制表示中 至少 存在一个尾随零。 例如,数字 5 的二进制表示是 "101",不存在尾随零,而数字 4 的二进制表示是 "100",存在两个尾随零。 如果可以选择两个或更多元素,其按位或运算结果存在尾随零,返回 true;否则,返回 false 。 示例 1: 输入:nums = [1,2,3,4,5] 输出:true 解释:如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110" ,存在一个尾随零。 示例 2: 输入:nums = [2,4,8,16] 输出:true ...
给你一个 正整数 数组 nums 。 你需要检查是否可以从数组中选出 两个或更多 元素,满足这些元素的按位或运算( OR)结果的二进制表示中 至少 存在一个尾随零。 例如,数字 5 的二进制表示是 "101",不存在尾随零,而数字 4 的二进制表示是 "100",存在两个尾随零。 如果可以选择两个或更多元素,其按位或运算结果存在尾随零,返回 true;否则,返回 false 。 示例 1: 输入:nums = [1,2,3,4,5] 输出:true 解释:如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110" ,存在一个尾随零。 示例 2: 输入:nums = [2,4,8,16] 输出:true 解释:如...
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.hasTrailingZeros(**test_input) == True test_input = { "nums": [1,3,5,7,9] } assert my_solution.hasTrailingZeros(**test_input) == False test_input = { "nums": [1,2] } assert my_solution.hasTrailingZeros(**test_input) == False test_inpu...
1,703,989,800
weekly-contest-378-find-longest-special-substring-that-occurs-thrice-i
https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i
find-longest-special-substring-that-occurs-thrice-i
{ "questionId": "3267", "questionFrontendId": "2981", "title": "Find Longest Special Substring That Occurs Thrice I", "titleSlug": "find-longest-special-substring-that-occurs-thrice-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 112, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存...
给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存在出现至...
my_solution = Solution() test_input = { "s": "aaaa" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "abcdef" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "abcaba" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "abcccccdddd" } assert...
1,703,989,800
weekly-contest-378-find-longest-special-substring-that-occurs-thrice-ii
https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-ii
find-longest-special-substring-that-occurs-thrice-ii
{ "questionId": "3266", "questionFrontendId": "2982", "title": "Find Longest Special Substring That Occurs Thrice II", "titleSlug": "find-longest-special-substring-that-occurs-thrice-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 172, "dislikes": 17, "categoryTitle": "Algorithms" }
""" 给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存...
给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd"、"zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1 。 子字符串 是字符串中的一个连续 非空 字符序列。 示例 1: 输入:s = "aaaa" 输出:2 解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。 可以证明最大长度是 2 。 示例 2: 输入:s = "abcdef" 输出:-1 解释:不存在出现至...
my_solution = Solution() test_input = { "s": "aaaa" } assert my_solution.maximumLength(**test_input) == 2 test_input = { "s": "abcdef" } assert my_solution.maximumLength(**test_input) == -1 test_input = { "s": "abcaba" } assert my_solution.maximumLength(**test_input) == 1 test_input = { "s": "abcccccdddd" } assert...
1,703,989,800
weekly-contest-378-palindrome-rearrangement-queries
https://leetcode.com/problems/palindrome-rearrangement-queries
palindrome-rearrangement-queries
{ "questionId": "3203", "questionFrontendId": "2983", "title": "Palindrome Rearrangement Queries", "titleSlug": "palindrome-rearrangement-queries", "isPaidOnly": false, "difficulty": "Hard", "likes": 73, "dislikes": 23, "categoryTitle": "Algorithms" }
""" 给你一个长度为 偶数n,下标从 0开始的字符串s。 同时给你一个下标从 0开始的二维整数数组queries,其中queries[i] = [ai, bi, ci, di]。 对于每个查询i,你需要执行以下操作: 将下标在范围0 <= ai <= bi < n / 2内的子字符串s[ai:bi]中的字符重新排列。 将下标在范围 n / 2 <= ci <= di < n内的 子字符串s[ci:di]中的字符重新排列。 对于每个查询,你的任务是判断执行操作后能否让 s变成一个 回文串 。 每个查询与其他查询都是 独立的。 请你返回一个下标从 0开始的数组answer,如果第i个查询执行操作后,可以将s变为一个回文串,那么an...
给你一个长度为 偶数n,下标从 0开始的字符串s。 同时给你一个下标从 0开始的二维整数数组queries,其中queries[i] = [ai, bi, ci, di]。 对于每个查询i,你需要执行以下操作: 将下标在范围0 <= ai <= bi < n / 2内的子字符串s[ai:bi]中的字符重新排列。 将下标在范围 n / 2 <= ci <= di < n内的 子字符串s[ci:di]中的字符重新排列。 对于每个查询,你的任务是判断执行操作后能否让 s变成一个 回文串 。 每个查询与其他查询都是 独立的。 请你返回一个下标从 0开始的数组answer,如果第i个查询执行操作后,可以将s变为一个回文串,那么answer...
my_solution = Solution() test_input = { "s": "abcabc", "queries": [[1,1,3,5],[0,2,5,5]] } assert my_solution.canMakePalindromeQueries(**test_input) == [True,True] test_input = { "s": "abbcdecbba", "queries": [[0,2,7,9]] } assert my_solution.canMakePalindromeQueries(**test_input) == [False] test_input = { "s": "acbc...
1,703,989,800
weekly-contest-377-minimum-number-game
https://leetcode.com/problems/minimum-number-game
minimum-number-game
{ "questionId": "3226", "questionFrontendId": "2974", "title": "Minimum Number Game", "titleSlug": "minimum-number-game", "isPaidOnly": false, "difficulty": "Easy", "likes": 38, "dislikes": 2, "categoryTitle": "Algorithms" }
""" 你有一个下标从 0 开始、长度为 偶数 的整数数组 nums ,同时还有一个空数组 arr 。Alice 和 Bob 决定玩一个游戏,游戏中每一轮 Alice 和 Bob 都会各自执行一次操作。游戏规则如下: 每一轮,Alice 先从 nums 中移除一个 最小 元素,然后 Bob 执行同样的操作。 接着,Bob 会将移除的元素添加到数组 arr 中,然后 Alice 也执行同样的操作。 游戏持续进行,直到 nums 变为空。 返回结果数组 arr 。 示例 1: 输入:nums = [5,4,2,3] 输出:[3,2,5,4] 解释:第一轮,Alice 先移除 2 ,然后 Bob 移除 3 。然后 Bob 先将 3...
你有一个下标从 0 开始、长度为 偶数 的整数数组 nums ,同时还有一个空数组 arr 。Alice 和 Bob 决定玩一个游戏,游戏中每一轮 Alice 和 Bob 都会各自执行一次操作。游戏规则如下: 每一轮,Alice 先从 nums 中移除一个 最小 元素,然后 Bob 执行同样的操作。 接着,Bob 会将移除的元素添加到数组 arr 中,然后 Alice 也执行同样的操作。 游戏持续进行,直到 nums 变为空。 返回结果数组 arr 。 示例 1: 输入:nums = [5,4,2,3] 输出:[3,2,5,4] 解释:第一轮,Alice 先移除 2 ,然后 Bob 移除 3 。然后 Bob 先将 3 添加到...
my_solution = Solution() test_input = { "nums": [5,4,2,3] } assert my_solution.numberGame(**test_input) == [3,2,5,4] test_input = { "nums": [2,5] } assert my_solution.numberGame(**test_input) == [5,2] test_input = { "nums": [4,4,3,8] } assert my_solution.numberGame(**test_input) == [4,3,8,4] test_input = { "nums":...
1,703,385,000
weekly-contest-377-maximum-square-area-by-removing-fences-from-a-field
https://leetcode.com/problems/maximum-square-area-by-removing-fences-from-a-field
maximum-square-area-by-removing-fences-from-a-field
{ "questionId": "3250", "questionFrontendId": "2975", "title": "Maximum Square Area by Removing Fences From a Field", "titleSlug": "maximum-square-area-by-removing-fences-from-a-field", "isPaidOnly": false, "difficulty": "Medium", "likes": 67, "dislikes": 68, "categoryTitle": "Algorithms" }
""" 有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1) 和 (m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFences 和 vFences 给出。 水平栅栏为坐标 (hFences[i], 1) 到 (hFences[i], n),垂直栅栏为坐标 (1, vFences[i]) 到 (m, vFences[i]) 。 返回通过 移除 一些栅栏(可能不移除)所能形成的最大面积的 正方形 田地的面积,或者如果无法形成正方形田地则返回 -1。 由于答案可能很大,所以请返回结果对 109 + 7 取余 后的值。 注意:田地外围两个水平栅栏(坐标 (1, 1) 到 (1, n) 和...
有一个大型的 (m - 1) x (n - 1) 矩形田地,其两个对角分别是 (1, 1) 和 (m, n) ,田地内部有一些水平栅栏和垂直栅栏,分别由数组 hFences 和 vFences 给出。 水平栅栏为坐标 (hFences[i], 1) 到 (hFences[i], n),垂直栅栏为坐标 (1, vFences[i]) 到 (m, vFences[i]) 。 返回通过 移除 一些栅栏(可能不移除)所能形成的最大面积的 正方形 田地的面积,或者如果无法形成正方形田地则返回 -1。 由于答案可能很大,所以请返回结果对 109 + 7 取余 后的值。 注意:田地外围两个水平栅栏(坐标 (1, 1) 到 (1, n) 和坐标 (...
my_solution = Solution() test_input = { "m": 4, "n": 3, "hFences": [2,3], "vFences": [2] } assert my_solution.maximizeSquareArea(**test_input) == 4 test_input = { "m": 6, "n": 7, "hFences": [2], "vFences": [4] } assert my_solution.maximizeSquareArea(**test_input) == -1 test_input = { "m": 4, "n": 4, "hFences": [2],...
1,703,385,000
weekly-contest-377-minimum-cost-to-convert-string-i
https://leetcode.com/problems/minimum-cost-to-convert-string-i
minimum-cost-to-convert-string-i
{ "questionId": "3235", "questionFrontendId": "2976", "title": "Minimum Cost to Convert String I", "titleSlug": "minimum-cost-to-convert-string-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 93, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符 original[i] 更改为字符 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y 。你就可以选择字符串中的一个字符 x 并以 z 的成本将其更改为字符 y 。 返回将字符串 source 转换为字符串 ta...
给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符 original[i] 更改为字符 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y 。你就可以选择字符串中的一个字符 x 并以 z 的成本将其更改为字符 y 。 返回将字符串 source 转换为字符串 target...
my_solution = Solution() test_input = { "source": "abcd", "target": "acbe", "original": ["a","b","c","c","e","d"], "changed": ["b","c","b","e","b","e"], "cost": [2,5,5,1,2,20] } assert my_solution.minimumCost(**test_input) == 28 test_input = { "source": "aaaa", "target": "bbbb", "original": ["a","c"], "changed": ["c...
1,703,385,000
weekly-contest-377-minimum-cost-to-convert-string-ii
https://leetcode.com/problems/minimum-cost-to-convert-string-ii
minimum-cost-to-convert-string-ii
{ "questionId": "3238", "questionFrontendId": "2977", "title": "Minimum Cost to Convert String II", "titleSlug": "minimum-cost-to-convert-string-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 40, "dislikes": 36, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符串数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符串 original[i] 更改为字符串 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y ,你就可以选择字符串中的 子串 x 并以 z 的成本将其更改为 y 。 你可以执行 任意数量 的操作,但是任两次操作必...
给你两个下标从 0 开始的字符串 source 和 target ,它们的长度均为 n 并且由 小写 英文字母组成。 另给你两个下标从 0 开始的字符串数组 original 和 changed ,以及一个整数数组 cost ,其中 cost[i] 代表将字符串 original[i] 更改为字符串 changed[i] 的成本。 你从字符串 source 开始。在一次操作中,如果 存在 任意 下标 j 满足 cost[j] == z 、original[j] == x 以及 changed[j] == y ,你就可以选择字符串中的 子串 x 并以 z 的成本将其更改为 y 。 你可以执行 任意数量 的操作,但是任两次操作必须满足 ...
my_solution = Solution() test_input = { "source": "abcd", "target": "acbe", "original": ["a","b","c","c","e","d"], "changed": ["b","c","b","e","b","e"], "cost": [2,5,5,1,2,20] } assert my_solution.minimumCost(**test_input) == 28 test_input = { "source": "abcdefgh", "target": "acdeeghh", "original": ["bcd","fgh","thh...
1,703,385,000
biweekly-contest-120-count-the-number-of-incremovable-subarrays-i
https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i
count-the-number-of-incremovable-subarrays-i
{ "questionId": "3252", "questionFrontendId": "2970", "title": "Count the Number of Incremovable Subarrays I", "titleSlug": "count-the-number-of-incremovable-subarrays-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 38, "dislikes": 49, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], ...
给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], [3,4...
my_solution = Solution() test_input = { "nums": [1,2,3,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 10 test_input = { "nums": [6,5,7,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [8,7,6,6] } assert my_solution.incremovableSubarrayCount(**test_input...
1,703,341,800
biweekly-contest-120-find-polygon-with-the-largest-perimeter
https://leetcode.com/problems/find-polygon-with-the-largest-perimeter
find-polygon-with-the-largest-perimeter
{ "questionId": "3262", "questionFrontendId": "2971", "title": "Find Polygon With the Largest Perimeter", "titleSlug": "find-polygon-with-the-largest-perimeter", "isPaidOnly": false, "difficulty": "Medium", "likes": 53, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个长度为n的正整数数组nums。 多边形指的是一个至少有 3条边的封闭二维图形。多边形的 最长边一定 小于所有其他边长度之和。 如果你有k(k >= 3)个正数a1,a2,a3, ...,ak 满足a1 <= a2 <= a3 <= ... <= ak 且 a1 + a2 + a3 + ... + ak-1 > ak,那么 一定存在一个k条边的多边形,每条边的长度分别为a1,a2,a3,...,ak。 一个多边形的 周长指的是它所有边之和。 请你返回从 nums中可以构造的 多边形的 最大周长。如果不能构造出任何多边形,请你返回 -1。 示例 1: 输入:nums = [5,5,5] 输出:15 解释:nums 中...
给你一个长度为n的正整数数组nums。 多边形指的是一个至少有 3条边的封闭二维图形。多边形的 最长边一定 小于所有其他边长度之和。 如果你有k(k >= 3)个正数a1,a2,a3, ...,ak 满足a1 <= a2 <= a3 <= ... <= ak 且 a1 + a2 + a3 + ... + ak-1 > ak,那么 一定存在一个k条边的多边形,每条边的长度分别为a1,a2,a3,...,ak。 一个多边形的 周长指的是它所有边之和。 请你返回从 nums中可以构造的 多边形的 最大周长。如果不能构造出任何多边形,请你返回 -1。 示例 1: 输入:nums = [5,5,5] 输出:15 解释:nums 中唯一可以...
my_solution = Solution() test_input = { "nums": [5,5,5] } assert my_solution.largestPerimeter(**test_input) == 15 test_input = { "nums": [1,12,1,2,5,50,3] } assert my_solution.largestPerimeter(**test_input) == 12 test_input = { "nums": [5,5,50] } assert my_solution.largestPerimeter(**test_input) == -1 test_input =...
1,703,341,800
biweekly-contest-120-count-the-number-of-incremovable-subarrays-ii
https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-ii
count-the-number-of-incremovable-subarrays-ii
{ "questionId": "3248", "questionFrontendId": "2972", "title": "Count the Number of Incremovable Subarrays II", "titleSlug": "count-the-number-of-incremovable-subarrays-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 97, "dislikes": 15, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], ...
给你一个下标从 0开始的 正整数数组nums。 如果 nums的一个子数组满足:移除这个子数组后剩余元素 严格递增,那么我们称这个子数组为 移除递增子数组。比方说,[5, 3, 4, 6, 7]中的 [3, 4]是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7]变为[5, 6, 7],是严格递增的。 请你返回 nums中 移除递增子数组的总数目。 注意,剩余元素为空的数组也视为是递增的。 子数组 指的是一个数组中一段连续的元素序列。 示例 1: 输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], [3,4...
my_solution = Solution() test_input = { "nums": [1,2,3,4] } assert my_solution.incremovableSubarrayCount(**test_input) == 10 test_input = { "nums": [6,5,7,8] } assert my_solution.incremovableSubarrayCount(**test_input) == 7 test_input = { "nums": [8,7,6,6] } assert my_solution.incremovableSubarrayCount(**test_input...
1,703,341,800
biweekly-contest-120-find-number-of-coins-to-place-in-tree-nodes
https://leetcode.com/problems/find-number-of-coins-to-place-in-tree-nodes
find-number-of-coins-to-place-in-tree-nodes
{ "questionId": "3218", "questionFrontendId": "2973", "title": "Find Number of Coins to Place in Tree Nodes", "titleSlug": "find-number-of-coins-to-place-in-tree-nodes", "isPaidOnly": false, "difficulty": "Hard", "likes": 67, "dislikes": 2, "categoryTitle": "Algorithms" }
""" 给你一棵n个节点的无向树,节点编号为0到n - 1,树的根节点在节点0处。同时给你一个长度为 n - 1的二维整数数组edges,其中edges[i] = [ai, bi]表示树中节点ai 和bi之间有一条边。 给你一个长度为 n下标从 0开始的整数数组cost,其中cost[i]是第 i个节点的 开销。 你需要在树中每个节点都放置金币,在节点 i处的金币数目计算方法如下: 如果节点 i对应的子树中的节点数目小于3,那么放1个金币。 否则,计算节点 i 对应的子树内 3 个不同节点的开销乘积的 最大值 ,并在节点 i 处放置对应数目的金币。如果最大乘积是 负数,那么放置 0个金币。 请你返回一个长度为 n的数组coin,...
给你一棵n个节点的无向树,节点编号为0到n - 1,树的根节点在节点0处。同时给你一个长度为 n - 1的二维整数数组edges,其中edges[i] = [ai, bi]表示树中节点ai 和bi之间有一条边。 给你一个长度为 n下标从 0开始的整数数组cost,其中cost[i]是第 i个节点的 开销。 你需要在树中每个节点都放置金币,在节点 i处的金币数目计算方法如下: 如果节点 i对应的子树中的节点数目小于3,那么放1个金币。 否则,计算节点 i 对应的子树内 3 个不同节点的开销乘积的 最大值 ,并在节点 i 处放置对应数目的金币。如果最大乘积是 负数,那么放置 0个金币。 请你返回一个长度为 n的数组coin,coin...
my_solution = Solution() test_input = { "edges": [[0,1],[0,2],[0,3],[0,4],[0,5]], "cost": [1,2,3,4,5,6] } assert my_solution.placedCoins(**test_input) == [120,1,1,1,1,1] test_input = { "edges": [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], "cost": [1,4,2,3,5,7,8,-4,2] } assert my_solution.placedCoins(**test_inp...
1,703,341,800
weekly-contest-376-find-missing-and-repeated-values
https://leetcode.com/problems/find-missing-and-repeated-values
find-missing-and-repeated-values
{ "questionId": "3227", "questionFrontendId": "2965", "title": "Find Missing and Repeated Values", "titleSlug": "find-missing-and-repeated-values", "isPaidOnly": false, "difficulty": "Easy", "likes": 90, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的二维整数矩阵 grid,大小为 n * n ,其中的值在 [1, n2] 范围内。除了 a 出现 两次,b 缺失 之外,每个整数都 恰好出现一次 。 任务是找出重复的数字a 和缺失的数字 b 。 返回一个下标从 0 开始、长度为 2 的整数数组 ans ,其中 ans[0] 等于 a ,ans[1] 等于 b 。 示例 1: 输入:grid = [[1,3],[2,2]] 输出:[2,4] 解释:数字 2 重复,数字 4 缺失,所以答案是 [2,4] 。 示例 2: 输入:grid = [[9,1,7],[8,9,2],[3,4,6]] 输出:[9,5] 解释:数字 9 重复,数字 5 缺失...
给你一个下标从 0 开始的二维整数矩阵 grid,大小为 n * n ,其中的值在 [1, n2] 范围内。除了 a 出现 两次,b 缺失 之外,每个整数都 恰好出现一次 。 任务是找出重复的数字a 和缺失的数字 b 。 返回一个下标从 0 开始、长度为 2 的整数数组 ans ,其中 ans[0] 等于 a ,ans[1] 等于 b 。 示例 1: 输入:grid = [[1,3],[2,2]] 输出:[2,4] 解释:数字 2 重复,数字 4 缺失,所以答案是 [2,4] 。 示例 2: 输入:grid = [[9,1,7],[8,9,2],[3,4,6]] 输出:[9,5] 解释:数字 9 重复,数字 5 缺失,所以答...
my_solution = Solution() test_input = { "grid": [[1,3],[2,2]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [2,4] test_input = { "grid": [[9,1,7],[8,9,2],[3,4,6]] } assert my_solution.findMissingAndRepeatedValues(**test_input) == [9,5] test_input = { "grid": [[1,1],[3,2]] } assert my_solution.f...
1,702,780,200
weekly-contest-376-divide-array-into-arrays-with-max-difference
https://leetcode.com/problems/divide-array-into-arrays-with-max-difference
divide-array-into-arrays-with-max-difference
{ "questionId": "3241", "questionFrontendId": "2966", "title": "Divide Array Into Arrays With Max Difference", "titleSlug": "divide-array-into-arrays-with-max-difference", "isPaidOnly": false, "difficulty": "Medium", "likes": 95, "dislikes": 20, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 的整数数组 nums,以及一个正整数 k 。 将这个数组划分为一个或多个长度为 3 的子数组,并满足以下条件: nums 中的 每个 元素都必须 恰好 存在于某个子数组中。 子数组中 任意 两个元素的差必须小于或等于 k 。 返回一个 二维数组 ,包含所有的子数组。如果不可能满足条件,就返回一个空数组。如果有多个答案,返回 任意一个 即可。 示例 1: 输入:nums = [1,3,4,8,7,9,3,5,1], k = 2 输出:[[1,1,3],[3,4,5],[7,8,9]] 解释:可以将数组划分为以下子数组:[1,1,3],[3,4,5] 和 [7,8,9] 。 每个子数组中任意两个元素的...
给你一个长度为 n 的整数数组 nums,以及一个正整数 k 。 将这个数组划分为一个或多个长度为 3 的子数组,并满足以下条件: nums 中的 每个 元素都必须 恰好 存在于某个子数组中。 子数组中 任意 两个元素的差必须小于或等于 k 。 返回一个 二维数组 ,包含所有的子数组。如果不可能满足条件,就返回一个空数组。如果有多个答案,返回 任意一个 即可。 示例 1: 输入:nums = [1,3,4,8,7,9,3,5,1], k = 2 输出:[[1,1,3],[3,4,5],[7,8,9]] 解释:可以将数组划分为以下子数组:[1,1,3],[3,4,5] 和 [7,8,9] 。 每个子数组中任意两个元素的差都小于...
my_solution = Solution() test_input = { "nums": [1,3,4,8,7,9,3,5,1], "k": 2 } assert my_solution.divideArray(**test_input) == [[1,1,3],[3,4,5],[7,8,9]] test_input = { "nums": [1,3,3,2,7,3], "k": 3 } assert my_solution.divideArray(**test_input) == [] test_input = { "nums": [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], ...
1,702,780,200
weekly-contest-376-minimum-cost-to-make-array-equalindromic
https://leetcode.com/problems/minimum-cost-to-make-array-equalindromic
minimum-cost-to-make-array-equalindromic
{ "questionId": "3229", "questionFrontendId": "2967", "title": "Minimum Cost to Make Array Equalindromic", "titleSlug": "minimum-cost-to-make-array-equalindromic", "isPaidOnly": false, "difficulty": "Medium", "likes": 164, "dislikes": 70, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n下标从 0开始的整数数组nums。 你可以对 nums执行特殊操作 任意次(也可以 0次)。每一次特殊操作中,你需要 按顺序执行以下步骤: 从范围[0, n - 1]里选择一个下标 i和一个 正整数x。 将|nums[i] - x|添加到总代价里。 将 nums[i]变为x。 如果一个正整数正着读和反着读都相同,那么我们称这个数是回文数。比方说,121,2552 和65756都是回文数,但是24,46,235都不是回文数。 如果一个数组中的所有元素都等于一个整数y,且y是一个小于109的回文数,那么我们称这个数组是一个 等数数组。 请你返回一个整数,表示执行任意次特殊操作后使 nums成为 等数数组的 ...
给你一个长度为 n下标从 0开始的整数数组nums。 你可以对 nums执行特殊操作 任意次(也可以 0次)。每一次特殊操作中,你需要 按顺序执行以下步骤: 从范围[0, n - 1]里选择一个下标 i和一个 正整数x。 将|nums[i] - x|添加到总代价里。 将 nums[i]变为x。 如果一个正整数正着读和反着读都相同,那么我们称这个数是回文数。比方说,121,2552 和65756都是回文数,但是24,46,235都不是回文数。 如果一个数组中的所有元素都等于一个整数y,且y是一个小于109的回文数,那么我们称这个数组是一个 等数数组。 请你返回一个整数,表示执行任意次特殊操作后使 nums成为 等数数组的 最小总代...
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.minimumCost(**test_input) == 6 test_input = { "nums": [10,12,13,14,15] } assert my_solution.minimumCost(**test_input) == 11 test_input = { "nums": [22,33,22,33,22] } assert my_solution.minimumCost(**test_input) == 22 test_input = { "n...
1,702,780,200
weekly-contest-376-apply-operations-to-maximize-frequency-score
https://leetcode.com/problems/apply-operations-to-maximize-frequency-score
apply-operations-to-maximize-frequency-score
{ "questionId": "3196", "questionFrontendId": "2968", "title": "Apply Operations to Maximize Frequency Score", "titleSlug": "apply-operations-to-maximize-frequency-score", "isPaidOnly": false, "difficulty": "Hard", "likes": 175, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的整数数组nums和一个整数k。 你可以对数组执行 至多k次操作: 从数组中选择一个下标 i,将nums[i] 增加或者减少1。 最终数组的频率分数定义为数组中众数的 频率。 请你返回你可以得到的 最大频率分数。 众数指的是数组中出现次数最多的数。一个元素的频率指的是数组中这个元素的出现次数。 示例 1: 输入:nums = [1,2,6,4], k = 3 输出:3 解释:我们可以对数组执行以下操作: - 选择 i = 0 ,将 nums[0] 增加 1 。得到数组 [2,2,6,4] 。 - 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,3] 。 - 选择 i ...
给你一个下标从 0开始的整数数组nums和一个整数k。 你可以对数组执行 至多k次操作: 从数组中选择一个下标 i,将nums[i] 增加或者减少1。 最终数组的频率分数定义为数组中众数的 频率。 请你返回你可以得到的 最大频率分数。 众数指的是数组中出现次数最多的数。一个元素的频率指的是数组中这个元素的出现次数。 示例 1: 输入:nums = [1,2,6,4], k = 3 输出:3 解释:我们可以对数组执行以下操作: - 选择 i = 0 ,将 nums[0] 增加 1 。得到数组 [2,2,6,4] 。 - 选择 i = 3 ,将 nums[3] 减少 1 ,得到数组 [2,2,6,3] 。 - 选择 i = 3 ...
my_solution = Solution() test_input = { "nums": [1,2,6,4], "k": 3 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [1,4,4,2,4], "k": 0 } assert my_solution.maxFrequencyScore(**test_input) == 3 test_input = { "nums": [3,20,13,2,3,15,24,19,8,13,19,20,21], "k": 45 } assert my_solution.m...
1,702,780,200
weekly-contest-375-count-tested-devices-after-test-operations
https://leetcode.com/problems/count-tested-devices-after-test-operations
count-tested-devices-after-test-operations
{ "questionId": "3220", "questionFrontendId": "2960", "title": "Count Tested Devices After Test Operations", "titleSlug": "count-tested-devices-after-test-operations", "isPaidOnly": false, "difficulty": "Easy", "likes": 90, "dislikes": 5, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 、下标从 0 开始的整数数组 batteryPercentages ,表示 n 个设备的电池百分比。 你的任务是按照顺序测试每个设备 i,执行以下测试操作: 如果 batteryPercentages[i] 大于 0: 增加 已测试设备的计数。 将下标在 [i + 1, n - 1] 的所有设备的电池百分比减少 1,确保它们的电池百分比 不会低于 0 ,即 batteryPercentages[j] = max(0, batteryPercentages[j] - 1)。 移动到下一个设备。 否则,移动到下一个设备而不执行任何测试。 返回一个整数,表示按顺序执行测试操作后 已测试设备 的数量...
给你一个长度为 n 、下标从 0 开始的整数数组 batteryPercentages ,表示 n 个设备的电池百分比。 你的任务是按照顺序测试每个设备 i,执行以下测试操作: 如果 batteryPercentages[i] 大于 0: 增加 已测试设备的计数。 将下标在 [i + 1, n - 1] 的所有设备的电池百分比减少 1,确保它们的电池百分比 不会低于 0 ,即 batteryPercentages[j] = max(0, batteryPercentages[j] - 1)。 移动到下一个设备。 否则,移动到下一个设备而不执行任何测试。 返回一个整数,表示按顺序执行测试操作后 已测试设备 的数量。 示...
my_solution = Solution() test_input = { "batteryPercentages": [1,1,2,1,3] } assert my_solution.countTestedDevices(**test_input) == 3 test_input = { "batteryPercentages": [0,1,2] } assert my_solution.countTestedDevices(**test_input) == 2 test_input = { "batteryPercentages": [0] } assert my_solution.countTestedDevice...
1,702,175,400
weekly-contest-375-double-modular-exponentiation
https://leetcode.com/problems/double-modular-exponentiation
double-modular-exponentiation
{ "questionId": "3234", "questionFrontendId": "2961", "title": "Double Modular Exponentiation", "titleSlug": "double-modular-exponentiation", "isPaidOnly": false, "difficulty": "Medium", "likes": 87, "dislikes": 12, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的二维数组 variables ,其中 variables[i] = [ai, bi, ci, mi],以及一个整数 target 。 如果满足以下公式,则下标 i 是 好下标: 0 <= i < variables.length ((aibi % 10)ci) % mi == target 返回一个由 好下标 组成的数组,顺序不限 。 示例 1: 输入:variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2 输出:[0,2] 解释:对于 variables 数组中的每个下标 i : 1) 对于下标 0 ,variables[0] = ...
给你一个下标从 0 开始的二维数组 variables ,其中 variables[i] = [ai, bi, ci, mi],以及一个整数 target 。 如果满足以下公式,则下标 i 是 好下标: 0 <= i < variables.length ((aibi % 10)ci) % mi == target 返回一个由 好下标 组成的数组,顺序不限 。 示例 1: 输入:variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2 输出:[0,2] 解释:对于 variables 数组中的每个下标 i : 1) 对于下标 0 ,variables[0] = [2,3...
my_solution = Solution() test_input = { "variables": [[2,3,3,10],[3,3,3,1],[6,1,1,4]], "target": 2 } assert my_solution.getGoodIndices(**test_input) == [0,2] test_input = { "variables": [[39,3,1000,1000]], "target": 17 } assert my_solution.getGoodIndices(**test_input) == [] test_input = { "variables": [[3,2,4,2],[3...
1,702,175,400
weekly-contest-375-count-subarrays-where-max-element-appears-at-least-k-times
https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times
count-subarrays-where-max-element-appears-at-least-k-times
{ "questionId": "3213", "questionFrontendId": "2962", "title": "Count Subarrays Where Max Element Appears at Least K Times", "titleSlug": "count-subarrays-where-max-element-appears-at-least-k-times", "isPaidOnly": false, "difficulty": "Medium", "likes": 215, "dislikes": 10, "categoryTitle": "Algorithm...
""" 给你一个整数数组 nums 和一个 正整数 k 。 请你统计有多少满足 「nums 中的 最大 元素」至少出现 k 次的子数组,并返回满足这一条件的子数组的数目。 子数组是数组中的一个连续元素序列。 示例 1: 输入:nums = [1,3,2,3,3], k = 2 输出:6 解释:包含元素 3 至少 2 次的子数组为:[1,3,2,3]、[1,3,2,3,3]、[3,2,3]、[3,2,3,3]、[2,3,3] 和 [3,3] 。 示例 2: 输入:nums = [1,4,2,1], k = 3 输出:0 解释:没有子数组包含元素 4 至少 3 次。 提示: 1 <= nums.length <= 105 ...
给你一个整数数组 nums 和一个 正整数 k 。 请你统计有多少满足 「nums 中的 最大 元素」至少出现 k 次的子数组,并返回满足这一条件的子数组的数目。 子数组是数组中的一个连续元素序列。 示例 1: 输入:nums = [1,3,2,3,3], k = 2 输出:6 解释:包含元素 3 至少 2 次的子数组为:[1,3,2,3]、[1,3,2,3,3]、[3,2,3]、[3,2,3,3]、[2,3,3] 和 [3,3] 。 示例 2: 输入:nums = [1,4,2,1], k = 3 输出:0 解释:没有子数组包含元素 4 至少 3 次。 提示: 1 <= nums.length <= 105 1 <=...
my_solution = Solution() test_input = { "nums": [1,3,2,3,3], "k": 2 } assert my_solution.countSubarrays(**test_input) == 6 test_input = { "nums": [1,4,2,1], "k": 3 } assert my_solution.countSubarrays(**test_input) == 0 test_input = { "nums": [61,23,38,23,56,40,82,56,82,82,82,70,8,69,8,7,19,14,58,42,82,10,82,78,15,8...
1,702,175,400
weekly-contest-375-count-the-number-of-good-partitions
https://leetcode.com/problems/count-the-number-of-good-partitions
count-the-number-of-good-partitions
{ "questionId": "3212", "questionFrontendId": "2963", "title": "Count the Number of Good Partitions", "titleSlug": "count-the-number-of-good-partitions", "isPaidOnly": false, "difficulty": "Hard", "likes": 163, "dislikes": 1, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、由 正整数 组成的数组 nums。 将数组分割成一个或多个 连续 子数组,如果不存在包含了相同数字的两个子数组,则认为是一种 好分割方案 。 返回 nums 的 好分割方案 的 数目。 由于答案可能很大,请返回答案对 109 + 7 取余 的结果。 示例 1: 输入:nums = [1,2,3,4] 输出:8 解释:有 8 种 好分割方案 :([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]) 和 (...
给你一个下标从 0 开始、由 正整数 组成的数组 nums。 将数组分割成一个或多个 连续 子数组,如果不存在包含了相同数字的两个子数组,则认为是一种 好分割方案 。 返回 nums 的 好分割方案 的 数目。 由于答案可能很大,请返回答案对 109 + 7 取余 的结果。 示例 1: 输入:nums = [1,2,3,4] 输出:8 解释:有 8 种 好分割方案 :([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]) 和 ([1,2...
my_solution = Solution() test_input = { "nums": [1,2,3,4] } assert my_solution.numberOfGoodPartitions(**test_input) == 8 test_input = { "nums": [1,1,1,1] } assert my_solution.numberOfGoodPartitions(**test_input) == 1 test_input = { "nums": [1,2,1,3] } assert my_solution.numberOfGoodPartitions(**test_input) == 2 te...
1,702,175,400
biweekly-contest-119-find-common-elements-between-two-arrays
https://leetcode.com/problems/find-common-elements-between-two-arrays
find-common-elements-between-two-arrays
{ "questionId": "3206", "questionFrontendId": "2956", "title": "Find Common Elements Between Two Arrays", "titleSlug": "find-common-elements-between-two-arrays", "isPaidOnly": false, "difficulty": "Easy", "likes": 66, "dislikes": 25, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0开始的整数数组nums1和nums2,它们分别含有 n和 m个元素。 请你计算以下两个数值: 统计0 <= i < n中的下标i,满足nums1[i]在 nums2中 至少出现了一次。 统计0 <= i < m中的下标i,满足nums2[i]在 nums1中 至少出现了一次。 请你返回一个长度为 2的整数数组answer,按顺序分别为以上两个数值。 示例 1: 输入:nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6] 输出:[3,4] 解释:分别计算两个数值: - nums1 中下标为 1 ,2 和 3 的元素在 nums2 中至少出现了一次,所以第一个值为 3 。...
给你两个下标从 0开始的整数数组nums1和nums2,它们分别含有 n和 m个元素。 请你计算以下两个数值: 统计0 <= i < n中的下标i,满足nums1[i]在 nums2中 至少出现了一次。 统计0 <= i < m中的下标i,满足nums2[i]在 nums1中 至少出现了一次。 请你返回一个长度为 2的整数数组answer,按顺序分别为以上两个数值。 示例 1: 输入:nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6] 输出:[3,4] 解释:分别计算两个数值: - nums1 中下标为 1 ,2 和 3 的元素在 nums2 中至少出现了一次,所以第一个值为 3 。 - n...
my_solution = Solution() test_input = { "nums1": [4,3,2,3,1], "nums2": [2,2,5,2,3,6] } assert my_solution.findIntersectionValues(**test_input) == [3,4] test_input = { "nums1": [3,4,2,3], "nums2": [1,5] } assert my_solution.findIntersectionValues(**test_input) == [0,0] test_input = { "nums1": [24,28,7,27,7,27,9,24,9...
1,702,132,200
biweekly-contest-119-remove-adjacent-almost-equal-characters
https://leetcode.com/problems/remove-adjacent-almost-equal-characters
remove-adjacent-almost-equal-characters
{ "questionId": "3230", "questionFrontendId": "2957", "title": "Remove Adjacent Almost-Equal Characters", "titleSlug": "remove-adjacent-almost-equal-characters", "isPaidOnly": false, "difficulty": "Medium", "likes": 115, "dislikes": 16, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的字符串word。 一次操作中,你可以选择word中任意一个下标 i,将word[i] 修改成任意一个小写英文字母。 请你返回消除 word中所有相邻 近似相等字符的 最少操作次数。 两个字符a 和b如果满足a == b或者a 和b在字母表中是相邻的,那么我们称它们是 近似相等字符。 示例 1: 输入:word = "aaaaa" 输出:2 解释:我们将 word 变为 "acaca" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 2 次操作。 示例 2: 输入:word = "abddez" 输出:2 解释:我们将 word 变为 "ybdoez" ,该字符...
给你一个下标从 0开始的字符串word。 一次操作中,你可以选择word中任意一个下标 i,将word[i] 修改成任意一个小写英文字母。 请你返回消除 word中所有相邻 近似相等字符的 最少操作次数。 两个字符a 和b如果满足a == b或者a 和b在字母表中是相邻的,那么我们称它们是 近似相等字符。 示例 1: 输入:word = "aaaaa" 输出:2 解释:我们将 word 变为 "acaca" ,该字符串没有相邻近似相等字符。 消除 word 中所有相邻近似相等字符最少需要 2 次操作。 示例 2: 输入:word = "abddez" 输出:2 解释:我们将 word 变为 "ybdoez" ,该字符串没有相...
my_solution = Solution() test_input = { "word": "aaaaa" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "abddez" } assert my_solution.removeAlmostEqualCharacters(**test_input) == 2 test_input = { "word": "zyxyxyz" } assert my_solution.removeAlmostEqualCharacters(**test_inp...
1,702,132,200
biweekly-contest-119-length-of-longest-subarray-with-at-most-k-frequency
https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency
length-of-longest-subarray-with-at-most-k-frequency
{ "questionId": "3225", "questionFrontendId": "2958", "title": "Length of Longest Subarray With at Most K Frequency", "titleSlug": "length-of-longest-subarray-with-at-most-k-frequency", "isPaidOnly": false, "difficulty": "Medium", "likes": 139, "dislikes": 5, "categoryTitle": "Algorithms" }
""" 给你一个整数数组nums和一个整数k。 一个元素 x在数组中的 频率指的是它在数组中的出现次数。 如果一个数组中所有元素的频率都 小于等于k,那么我们称这个数组是 好数组。 请你返回 nums中 最长好子数组的长度。 子数组 指的是一个数组中一段连续非空的元素序列。 示例 1: 输入:nums = [1,2,3,1,2,3,1,2], k = 2 输出:6 解释:最长好子数组是 [1,2,3,1,2,3] ,值 1 ,2 和 3 在子数组中的频率都没有超过 k = 2 。[2,3,1,2,3,1] 和 [3,1,2,3,1,2] 也是好子数组。 最长好子数组的长度为 6 。 示例 2: 输入:nums = [1,2,...
给你一个整数数组nums和一个整数k。 一个元素 x在数组中的 频率指的是它在数组中的出现次数。 如果一个数组中所有元素的频率都 小于等于k,那么我们称这个数组是 好数组。 请你返回 nums中 最长好子数组的长度。 子数组 指的是一个数组中一段连续非空的元素序列。 示例 1: 输入:nums = [1,2,3,1,2,3,1,2], k = 2 输出:6 解释:最长好子数组是 [1,2,3,1,2,3] ,值 1 ,2 和 3 在子数组中的频率都没有超过 k = 2 。[2,3,1,2,3,1] 和 [3,1,2,3,1,2] 也是好子数组。 最长好子数组的长度为 6 。 示例 2: 输入:nums = [1,2,1,2,...
my_solution = Solution() test_input = { "nums": [1,2,3,1,2,3,1,2], "k": 2 } assert my_solution.maxSubarrayLength(**test_input) == 6 test_input = { "nums": [1,2,1,2,1,2,1,2], "k": 1 } assert my_solution.maxSubarrayLength(**test_input) == 2 test_input = { "nums": [5,5,5,5,5,5,5], "k": 4 } assert my_solution.maxSubarr...
1,702,132,200
biweekly-contest-119-number-of-possible-sets-of-closing-branches
https://leetcode.com/problems/number-of-possible-sets-of-closing-branches
number-of-possible-sets-of-closing-branches
{ "questionId": "3217", "questionFrontendId": "2959", "title": "Number of Possible Sets of Closing Branches", "titleSlug": "number-of-possible-sets-of-closing-branches", "isPaidOnly": false, "difficulty": "Hard", "likes": 110, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 一个公司在全国有 n个分部,它们之间有的有道路连接。一开始,所有分部通过这些道路两两之间互相可以到达。 公司意识到在分部之间旅行花费了太多时间,所以它们决定关闭一些分部(也可能不关闭任何分部),同时保证剩下的分部之间两两互相可以到达且最远距离不超过maxDistance。 两个分部之间的 距离 是通过道路长度之和的 最小值。 给你整数n,maxDistance和下标从 0开始的二维整数数组roads,其中roads[i] = [ui, vi, wi]表示一条从ui到vi长度为wi的无向道路。 请你返回关闭分部的可行方案数目,满足每个方案里剩余分部之间的最远距离不超过maxDistance。 注意,关闭一个分部后,与之相连的所...
一个公司在全国有 n个分部,它们之间有的有道路连接。一开始,所有分部通过这些道路两两之间互相可以到达。 公司意识到在分部之间旅行花费了太多时间,所以它们决定关闭一些分部(也可能不关闭任何分部),同时保证剩下的分部之间两两互相可以到达且最远距离不超过maxDistance。 两个分部之间的 距离 是通过道路长度之和的 最小值。 给你整数n,maxDistance和下标从 0开始的二维整数数组roads,其中roads[i] = [ui, vi, wi]表示一条从ui到vi长度为wi的无向道路。 请你返回关闭分部的可行方案数目,满足每个方案里剩余分部之间的最远距离不超过maxDistance。 注意,关闭一个分部后,与之相连的所有道路不...
my_solution = Solution() test_input = { "n": 3, "maxDistance": 5, "roads": [[0,1,2],[1,2,10],[0,2,10]] } assert my_solution.numberOfSets(**test_input) == 5 test_input = { "n": 3, "maxDistance": 5, "roads": [[0,1,20],[0,1,10],[1,2,2],[0,2,2]] } assert my_solution.numberOfSets(**test_input) == 7 test_input = { "n": 1...
1,702,132,200
weekly-contest-374-find-the-peaks
https://leetcode.com/problems/find-the-peaks
find-the-peaks
{ "questionId": "3221", "questionFrontendId": "2951", "title": "Find the Peaks", "titleSlug": "find-the-peaks", "isPaidOnly": false, "difficulty": "Easy", "likes": 91, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的数组 mountain 。你的任务是找出数组mountain 中的所有 峰值。 以数组形式返回给定数组中 峰值 的下标,顺序不限 。 注意: 峰值 是指一个严格大于其相邻元素的元素。 数组的第一个和最后一个元素 不 是峰值。 示例 1: 输入:mountain = [2,4,4] 输出:[] 解释:mountain[0] 和 mountain[2] 不可能是峰值,因为它们是数组的第一个和最后一个元素。 mountain[1] 也不可能是峰值,因为它不严格大于 mountain[2] 。 因此,答案为 [] 。 示例 2: 输入:mountain = [1,4,3,8,5] 输出:[1,3...
给你一个下标从 0 开始的数组 mountain 。你的任务是找出数组mountain 中的所有 峰值。 以数组形式返回给定数组中 峰值 的下标,顺序不限 。 注意: 峰值 是指一个严格大于其相邻元素的元素。 数组的第一个和最后一个元素 不 是峰值。 示例 1: 输入:mountain = [2,4,4] 输出:[] 解释:mountain[0] 和 mountain[2] 不可能是峰值,因为它们是数组的第一个和最后一个元素。 mountain[1] 也不可能是峰值,因为它不严格大于 mountain[2] 。 因此,答案为 [] 。 示例 2: 输入:mountain = [1,4,3,8,5] 输出:[1,3] 解释...
my_solution = Solution() test_input = { "mountain": [2,4,4] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain": [1,4,3,8,5] } assert my_solution.findPeaks(**test_input) == [1,3] test_input = { "mountain": [1,1,1] } assert my_solution.findPeaks(**test_input) == [] test_input = { "mountain"...
1,701,570,600
weekly-contest-374-minimum-number-of-coins-to-be-added
https://leetcode.com/problems/minimum-number-of-coins-to-be-added
minimum-number-of-coins-to-be-added
{ "questionId": "3231", "questionFrontendId": "2952", "title": "Minimum Number of Coins to be Added", "titleSlug": "minimum-number-of-coins-to-be-added", "isPaidOnly": false, "difficulty": "Medium", "likes": 227, "dislikes": 40, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 coins,表示可用的硬币的面值,以及一个整数 target 。 如果存在某个 coins 的子序列总和为 x,那么整数 x 就是一个 可取得的金额 。 返回需要添加到数组中的 任意面值 硬币的 最小数量 ,使范围 [1, target] 内的每个整数都属于 可取得的金额 。 数组的 子序列 是通过删除原始数组的一些(可能不删除)元素而形成的新的 非空 数组,删除过程不会改变剩余元素的相对位置。 示例 1: 输入:coins = [1,4,10], target = 19 输出:2 解释:需要添加面值为 2 和 8 的硬币各一枚,得到硬币数组 [1,2,4,8,10] 。 可以证明从 ...
给你一个下标从 0 开始的整数数组 coins,表示可用的硬币的面值,以及一个整数 target 。 如果存在某个 coins 的子序列总和为 x,那么整数 x 就是一个 可取得的金额 。 返回需要添加到数组中的 任意面值 硬币的 最小数量 ,使范围 [1, target] 内的每个整数都属于 可取得的金额 。 数组的 子序列 是通过删除原始数组的一些(可能不删除)元素而形成的新的 非空 数组,删除过程不会改变剩余元素的相对位置。 示例 1: 输入:coins = [1,4,10], target = 19 输出:2 解释:需要添加面值为 2 和 8 的硬币各一枚,得到硬币数组 [1,2,4,8,10] 。 可以证明从 1 到 ...
my_solution = Solution() test_input = { "coins": [1,4,10], "target": 19 } assert my_solution.minimumAddedCoins(**test_input) == 2 test_input = { "coins": [1,4,10,5,7,19], "target": 19 } assert my_solution.minimumAddedCoins(**test_input) == 1 test_input = { "coins": [1,1,1], "target": 20 } assert my_solution.minimum...
1,701,570,600
weekly-contest-374-count-complete-substrings
https://leetcode.com/problems/count-complete-substrings
count-complete-substrings
{ "questionId": "3223", "questionFrontendId": "2953", "title": "Count Complete Substrings", "titleSlug": "count-complete-substrings", "isPaidOnly": false, "difficulty": "Hard", "likes": 154, "dislikes": 30, "categoryTitle": "Algorithms" }
""" 给你一个字符串word和一个整数 k。 如果word的一个子字符串 s满足以下条件,我们称它是 完全字符串: s中每个字符 恰好出现 k次。 相邻字符在字母表中的顺序 至多相差2。也就是说,s中两个相邻字符c1 和c2,它们在字母表中的位置相差至多为 2 。 请你返回 word中 完全子字符串的数目。 子字符串指的是一个字符串中一段连续 非空的字符序列。 示例 1: 输入:word = "igigee", k = 2 输出:3 解释:完全子字符串需要满足每个字符恰好出现 2 次,且相邻字符相差至多为 2 :igigee, igigee, igigee。 示例 2: 输入:word = "aaabbbccc", k...
给你一个字符串word和一个整数 k。 如果word的一个子字符串 s满足以下条件,我们称它是 完全字符串: s中每个字符 恰好出现 k次。 相邻字符在字母表中的顺序 至多相差2。也就是说,s中两个相邻字符c1 和c2,它们在字母表中的位置相差至多为 2 。 请你返回 word中 完全子字符串的数目。 子字符串指的是一个字符串中一段连续 非空的字符序列。 示例 1: 输入:word = "igigee", k = 2 输出:3 解释:完全子字符串需要满足每个字符恰好出现 2 次,且相邻字符相差至多为 2 :igigee, igigee, igigee。 示例 2: 输入:word = "aaabbbccc", k = 3...
my_solution = Solution() test_input = { "word": "igigee", "k": 2 } assert my_solution.countCompleteSubstrings(**test_input) == 3 test_input = { "word": "aaabbbccc", "k": 3 } assert my_solution.countCompleteSubstrings(**test_input) == 6 test_input = { "word": "a", "k": 1 } assert my_solution.countCompleteSubstrings(...
1,701,570,600
weekly-contest-374-count-the-number-of-infection-sequences
https://leetcode.com/problems/count-the-number-of-infection-sequences
count-the-number-of-infection-sequences
{ "questionId": "3224", "questionFrontendId": "2954", "title": "Count the Number of Infection Sequences", "titleSlug": "count-the-number-of-infection-sequences", "isPaidOnly": false, "difficulty": "Hard", "likes": 50, "dislikes": 12, "categoryTitle": "Algorithms" }
""" 给你一个整数n和一个下标从 0开始的整数数组sick,数组按 升序排序。 有n位小朋友站成一排,按顺序编号为 0到 n - 1。数组sick包含一开始得了感冒的小朋友的位置。如果位置为i的小朋友得了感冒,他会传染给下标为 i - 1或者 i + 1的小朋友,前提 是被传染的小朋友存在且还没有得感冒。每一秒中, 至多一位还没感冒的小朋友会被传染。 经过有限的秒数后,队列中所有小朋友都会感冒。感冒序列指的是 所有一开始没有感冒的小朋友最后得感冒的顺序序列。请你返回所有感冒序列的数目。 由于答案可能很大,请你将答案对109 + 7取余后返回。 注意,感冒序列 不 包含一开始就得了感冒的小朋友的下标。 示例 1: 输入:n = ...
给你一个整数n和一个下标从 0开始的整数数组sick,数组按 升序排序。 有n位小朋友站成一排,按顺序编号为 0到 n - 1。数组sick包含一开始得了感冒的小朋友的位置。如果位置为i的小朋友得了感冒,他会传染给下标为 i - 1或者 i + 1的小朋友,前提 是被传染的小朋友存在且还没有得感冒。每一秒中, 至多一位还没感冒的小朋友会被传染。 经过有限的秒数后,队列中所有小朋友都会感冒。感冒序列指的是 所有一开始没有感冒的小朋友最后得感冒的顺序序列。请你返回所有感冒序列的数目。 由于答案可能很大,请你将答案对109 + 7取余后返回。 注意,感冒序列 不 包含一开始就得了感冒的小朋友的下标。 示例 1: 输入:n = 5, s...
my_solution = Solution() test_input = { "n": 5, "sick": [0,4] } assert my_solution.numberOfSequence(**test_input) == 4 test_input = { "n": 4, "sick": [1] } assert my_solution.numberOfSequence(**test_input) == 3 test_input = { "n": 2, "sick": [0] } assert my_solution.numberOfSequence(**test_input) == 1 test_input =...
1,701,570,600
weekly-contest-373-matrix-similarity-after-cyclic-shifts
https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts
matrix-similarity-after-cyclic-shifts
{ "questionId": "3215", "questionFrontendId": "2946", "title": "Matrix Similarity After Cyclic Shifts", "titleSlug": "matrix-similarity-after-cyclic-shifts", "isPaidOnly": false, "difficulty": "Easy", "likes": 72, "dislikes": 48, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始且大小为 m x n 的整数矩阵 mat 和一个整数 k 。请你将矩阵中的 奇数 行循环 右 移 k 次,偶数 行循环 左 移 k 次。 如果初始矩阵和最终矩阵完全相同,则返回 true ,否则返回 false 。 示例 1: 输入:mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2 输出:true 解释: 初始矩阵如图一所示。 图二表示对奇数行右移一次且对偶数行左移一次后的矩阵状态。 图三是经过两次循环移位后的最终矩阵状态,与初始矩阵相同。 因此,返回 true 。 示例 2: 输入:mat = [[2,2],[2,2]], k = 3 输出:tru...
给你一个下标从 0 开始且大小为 m x n 的整数矩阵 mat 和一个整数 k 。请你将矩阵中的 奇数 行循环 右 移 k 次,偶数 行循环 左 移 k 次。 如果初始矩阵和最终矩阵完全相同,则返回 true ,否则返回 false 。 示例 1: 输入:mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2 输出:true 解释: 初始矩阵如图一所示。 图二表示对奇数行右移一次且对偶数行左移一次后的矩阵状态。 图三是经过两次循环移位后的最终矩阵状态,与初始矩阵相同。 因此,返回 true 。 示例 2: 输入:mat = [[2,2],[2,2]], k = 3 输出:true 解释...
my_solution = Solution() test_input = { "mat": [[1,2]], "k": 1 } assert my_solution.areSimilar(**test_input) == False test_input = { "mat": [[1,2,1,2],[5,5,5,5],[6,3,6,3]], "k": 2 } assert my_solution.areSimilar(**test_input) == True test_input = { "mat": [[4,9,10,10],[9,3,8,4],[2,5,3,8],[6,1,10,4]], "k": 5 } asser...
1,700,965,800
weekly-contest-373-count-beautiful-substrings-i
https://leetcode.com/problems/count-beautiful-substrings-i
count-beautiful-substrings-i
{ "questionId": "3210", "questionFrontendId": "2947", "title": "Count Beautiful Substrings I", "titleSlug": "count-beautiful-substrings-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 102, "dislikes": 8, "categoryTitle": "Algorithms" }
""" 给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh"...
给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh", k ...
my_solution = Solution() test_input = { "s": "baeyh", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "abba", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "bcdf", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test...
1,700,965,800
weekly-contest-373-make-lexicographically-smallest-array-by-swapping-elements
https://leetcode.com/problems/make-lexicographically-smallest-array-by-swapping-elements
make-lexicographically-smallest-array-by-swapping-elements
{ "questionId": "3219", "questionFrontendId": "2948", "title": "Make Lexicographically Smallest Array by Swapping Elements", "titleSlug": "make-lexicographically-smallest-array-by-swapping-elements", "isPaidOnly": false, "difficulty": "Medium", "likes": 197, "dislikes": 10, "categoryTitle": "Algorithm...
""" 给你一个下标从 0 开始的 正整数 数组 nums 和一个 正整数 limit 。 在一次操作中,你可以选择任意两个下标 i 和 j,如果 满足 |nums[i] - nums[j]| <= limit ,则交换 nums[i] 和 nums[j] 。 返回执行任意次操作后能得到的 字典序最小的数组 。 如果在数组 a 和数组 b 第一个不同的位置上,数组 a 中的对应元素比数组 b 中的对应元素的字典序更小,则认为数组 a 就比数组 b 字典序更小。例如,数组 [2,10,3] 比数组 [10,2,3] 字典序更小,下标 0 处是两个数组第一个不同的位置,且 2 < 10 。 示例 1: 输入:nums = [1,5,...
给你一个下标从 0 开始的 正整数 数组 nums 和一个 正整数 limit 。 在一次操作中,你可以选择任意两个下标 i 和 j,如果 满足 |nums[i] - nums[j]| <= limit ,则交换 nums[i] 和 nums[j] 。 返回执行任意次操作后能得到的 字典序最小的数组 。 如果在数组 a 和数组 b 第一个不同的位置上,数组 a 中的对应元素比数组 b 中的对应元素的字典序更小,则认为数组 a 就比数组 b 字典序更小。例如,数组 [2,10,3] 比数组 [10,2,3] 字典序更小,下标 0 处是两个数组第一个不同的位置,且 2 < 10 。 示例 1: 输入:nums = [1,5,3,9,...
my_solution = Solution() test_input = { "nums": [1,5,3,9,8], "limit": 2 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,3,5,8,9] test_input = { "nums": [1,7,6,18,2,1], "limit": 3 } assert my_solution.lexicographicallySmallestArray(**test_input) == [1,6,7,18,1,2] test_input = { "nums": [1,7,2...
1,700,965,800
weekly-contest-373-count-beautiful-substrings-ii
https://leetcode.com/problems/count-beautiful-substrings-ii
count-beautiful-substrings-ii
{ "questionId": "3208", "questionFrontendId": "2949", "title": "Count Beautiful Substrings II", "titleSlug": "count-beautiful-substrings-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 137, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh"...
给你一个字符串 s 和一个正整数 k 。 用 vowels 和 consonants 分别表示字符串中元音字母和辅音字母的数量。 如果某个字符串满足以下条件,则称其为 美丽字符串 : vowels == consonants,即元音字母和辅音字母的数量相等。 (vowels * consonants) % k == 0,即元音字母和辅音字母的数量的乘积能被 k 整除。 返回字符串 s 中 非空美丽子字符串 的数量。 子字符串是字符串中的一个连续字符序列。 英语中的 元音字母 为 'a'、'e'、'i'、'o' 和 'u' 。 英语中的 辅音字母 为除了元音字母之外的所有字母。 示例 1: 输入:s = "baeyh", k ...
my_solution = Solution() test_input = { "s": "baeyh", "k": 2 } assert my_solution.beautifulSubstrings(**test_input) == 2 test_input = { "s": "abba", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 3 test_input = { "s": "bcdf", "k": 1 } assert my_solution.beautifulSubstrings(**test_input) == 0 test...
1,700,965,800
biweekly-contest-118-find-words-containing-character
https://leetcode.com/problems/find-words-containing-character
find-words-containing-character
{ "questionId": "3194", "questionFrontendId": "2942", "title": "Find Words Containing Character", "titleSlug": "find-words-containing-character", "isPaidOnly": false, "difficulty": "Easy", "likes": 134, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的字符串数组words和一个字符x。 请你返回一个 下标数组,表示下标在数组中对应的单词包含字符 x。 注意,返回的数组可以是任意顺序。 示例 1: 输入:words = ["leet","code"], x = "e" 输出:[0,1] 解释:"e" 在两个单词中都出现了:"leet" 和 "code" 。所以我们返回下标 0 和 1 。 示例 2: 输入:words = ["abc","bcd","aaaa","cbc"], x = "a" 输出:[0,2] 解释:"a" 在 "abc" 和 "aaaa" 中出现了,所以我们返回下标 0 和 2 。 示例 3: 输入:words = ["...
给你一个下标从 0开始的字符串数组words和一个字符x。 请你返回一个 下标数组,表示下标在数组中对应的单词包含字符 x。 注意,返回的数组可以是任意顺序。 示例 1: 输入:words = ["leet","code"], x = "e" 输出:[0,1] 解释:"e" 在两个单词中都出现了:"leet" 和 "code" 。所以我们返回下标 0 和 1 。 示例 2: 输入:words = ["abc","bcd","aaaa","cbc"], x = "a" 输出:[0,2] 解释:"a" 在 "abc" 和 "aaaa" 中出现了,所以我们返回下标 0 和 2 。 示例 3: 输入:words = ["abc"...
my_solution = Solution() test_input = { "words": ["leet","code"], "x": "e" } assert my_solution.findWordsContaining(**test_input) == [0,1] test_input = { "words": ["abc","bcd","aaaa","cbc"], "x": "a" } assert my_solution.findWordsContaining(**test_input) == [0,2] test_input = { "words": ["abc","bcd","aaaa","cbc"], ...
1,700,922,600
biweekly-contest-118-maximize-area-of-square-hole-in-grid
https://leetcode.com/problems/maximize-area-of-square-hole-in-grid
maximize-area-of-square-hole-in-grid
{ "questionId": "3214", "questionFrontendId": "2943", "title": "Maximize Area of Square Hole in Grid", "titleSlug": "maximize-area-of-square-hole-in-grid", "isPaidOnly": false, "difficulty": "Medium", "likes": 73, "dislikes": 110, "categoryTitle": "Algorithms" }
""" 给你一个网格图,由n + 2条 横线段和m + 2条竖线段组成,一开始所有区域均为1 x 1的单元格。 所有线段的编号从 1开始。 给你两个整数n 和m。 同时给你两个整数数组hBars 和vBars。 hBars 包含区间[2, n + 1]内互不相同的横线段编号。 vBars包含[2, m + 1]内互不相同的竖线段编号。 如果满足以下条件之一,你可以 移除两个数组中的部分线段: 如果移除的是横线段,它必须是hBars中的值。 如果移除的是竖线段,它必须是vBars中的值。 请你返回移除一些线段后(可能不移除任何线段),剩余网格图中 最大正方形空洞的面积,正方形空洞的意思是正方形 内部 不含有任何线段。 示例 ...
给你一个网格图,由n + 2条 横线段和m + 2条竖线段组成,一开始所有区域均为1 x 1的单元格。 所有线段的编号从 1开始。 给你两个整数n 和m。 同时给你两个整数数组hBars 和vBars。 hBars 包含区间[2, n + 1]内互不相同的横线段编号。 vBars包含[2, m + 1]内互不相同的竖线段编号。 如果满足以下条件之一,你可以 移除两个数组中的部分线段: 如果移除的是横线段,它必须是hBars中的值。 如果移除的是竖线段,它必须是vBars中的值。 请你返回移除一些线段后(可能不移除任何线段),剩余网格图中 最大正方形空洞的面积,正方形空洞的意思是正方形 内部 不含有任何线段。 示例 1: ...
my_solution = Solution() test_input = { "n": 2, "m": 1, "hBars": [2,3], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 1, "m": 1, "hBars": [2], "vBars": [2] } assert my_solution.maximizeSquareHoleArea(**test_input) == 4 test_input = { "n": 2, "m": 3, "hBars": [2,3], ...
1,700,922,600
biweekly-contest-118-minimum-number-of-coins-for-fruits
https://leetcode.com/problems/minimum-number-of-coins-for-fruits
minimum-number-of-coins-for-fruits
{ "questionId": "3209", "questionFrontendId": "2944", "title": "Minimum Number of Coins for Fruits", "titleSlug": "minimum-number-of-coins-for-fruits", "isPaidOnly": false, "difficulty": "Medium", "likes": 141, "dislikes": 27, "categoryTitle": "Algorithms" }
""" 你在一个水果超市里,货架上摆满了玲琅满目的奇珍异果。 给你一个下标从 1开始的数组prices,其中prices[i]表示你购买第 i个水果需要花费的金币数目。 水果超市有如下促销活动: 如果你花费 price[i]购买了水果i,那么接下来的 i个水果你都可以免费获得。 注意,即使你可以免费获得水果j,你仍然可以花费prices[j]个金币去购买它以便能免费获得接下来的 j个水果。 请你返回获得所有水果所需要的 最少金币数。 示例 1: 输入:prices = [3,1,2] 输出:4 解释:你可以按如下方法获得所有水果: - 花 3 个金币购买水果 1 ,然后免费获得水果 2 。 - 花 1 个金币购买水果 2 ,...
你在一个水果超市里,货架上摆满了玲琅满目的奇珍异果。 给你一个下标从 1开始的数组prices,其中prices[i]表示你购买第 i个水果需要花费的金币数目。 水果超市有如下促销活动: 如果你花费 price[i]购买了水果i,那么接下来的 i个水果你都可以免费获得。 注意,即使你可以免费获得水果j,你仍然可以花费prices[j]个金币去购买它以便能免费获得接下来的 j个水果。 请你返回获得所有水果所需要的 最少金币数。 示例 1: 输入:prices = [3,1,2] 输出:4 解释:你可以按如下方法获得所有水果: - 花 3 个金币购买水果 1 ,然后免费获得水果 2 。 - 花 1 个金币购买水果 2 ,然后免费...
my_solution = Solution() test_input = { "prices": [3,1,2] } assert my_solution.minimumCoins(**test_input) == 4 test_input = { "prices": [1,10,1,1] } assert my_solution.minimumCoins(**test_input) == 2 test_input = { "prices": [26,18,6,12,49,7,45,45] } assert my_solution.minimumCoins(**test_input) == 39 test_input =...
1,700,922,600
biweekly-contest-118-find-maximum-non-decreasing-array-length
https://leetcode.com/problems/find-maximum-non-decreasing-array-length
find-maximum-non-decreasing-array-length
{ "questionId": "3211", "questionFrontendId": "2945", "title": "Find Maximum Non-decreasing Array Length", "titleSlug": "find-maximum-non-decreasing-array-length", "isPaidOnly": false, "difficulty": "Hard", "likes": 90, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的整数数组nums。 你可以执行任意次操作。每次操作中,你需要选择一个 子数组,并将这个子数组用它所包含元素的 和替换。比方说,给定数组是[1,3,5,6],你可以选择子数组[3,5],用子数组的和 8替换掉子数组,然后数组会变为[1,8,6]。 请你返回执行任意次操作以后,可以得到的 最长非递减数组的长度。 子数组指的是一个数组中一段连续 非空的元素序列。 示例 1: 输入:nums = [5,2,2] 输出:1 解释:这个长度为 3 的数组不是非递减的。 我们有 2 种方案使数组长度为 2 。 第一种,选择子数组 [2,2] ,对数组执行操作后得到 [5,4] 。 第二种,选择子数组 [5,2]...
给你一个下标从 0开始的整数数组nums。 你可以执行任意次操作。每次操作中,你需要选择一个 子数组,并将这个子数组用它所包含元素的 和替换。比方说,给定数组是[1,3,5,6],你可以选择子数组[3,5],用子数组的和 8替换掉子数组,然后数组会变为[1,8,6]。 请你返回执行任意次操作以后,可以得到的 最长非递减数组的长度。 子数组指的是一个数组中一段连续 非空的元素序列。 示例 1: 输入:nums = [5,2,2] 输出:1 解释:这个长度为 3 的数组不是非递减的。 我们有 2 种方案使数组长度为 2 。 第一种,选择子数组 [2,2] ,对数组执行操作后得到 [5,4] 。 第二种,选择子数组 [5,2] ,对数...
my_solution = Solution() test_input = { "nums": [5,2,2] } assert my_solution.findMaximumLength(**test_input) == 1 test_input = { "nums": [1,2,3,4] } assert my_solution.findMaximumLength(**test_input) == 4 test_input = { "nums": [4,3,2,6] } assert my_solution.findMaximumLength(**test_input) == 3 test_input = { "num...
1,700,922,600
weekly-contest-372-make-three-strings-equal
https://leetcode.com/problems/make-three-strings-equal/
make-three-strings-equal
{ "questionId": "3207", "questionFrontendId": "2937", "title": "Make Three Strings Equal", "titleSlug": "make-three-strings-equal", "isPaidOnly": false, "difficulty": "Easy", "likes": 114, "dislikes": 30, "categoryTitle": "Algorithms" }
""" 给你三个字符串 s1、s2 和 s3。 你可以根据需要对这三个字符串执行以下操作 任意次数 。 在每次操作中,你可以选择其中一个长度至少为 2 的字符串 并删除其 最右位置上 的字符。 如果存在某种方法能够使这三个字符串相等,请返回使它们相等所需的 最小 操作次数;否则,返回 -1。 示例 1: 输入:s1 = "abc",s2 = "abb",s3 = "ab" 输出:2 解释:对 s1 和 s2 进行一次操作后,可以得到三个相等的字符串。 可以证明,不可能用少于两次操作使它们相等。 示例 2: 输入:s1 = "dac",s2 = "bac",s3 = "cac" 输出:-1 解释:因为 s1 和 s2 的最左位...
给你三个字符串 s1、s2 和 s3。 你可以根据需要对这三个字符串执行以下操作 任意次数 。 在每次操作中,你可以选择其中一个长度至少为 2 的字符串 并删除其 最右位置上 的字符。 如果存在某种方法能够使这三个字符串相等,请返回使它们相等所需的 最小 操作次数;否则,返回 -1。 示例 1: 输入:s1 = "abc",s2 = "abb",s3 = "ab" 输出:2 解释:对 s1 和 s2 进行一次操作后,可以得到三个相等的字符串。 可以证明,不可能用少于两次操作使它们相等。 示例 2: 输入:s1 = "dac",s2 = "bac",s3 = "cac" 输出:-1 解释:因为 s1 和 s2 的最左位置上的字...
my_solution = Solution() test_input = { "s1": "abc", "s2": "abb", "s3": "ab" } assert my_solution.findMinimumOperations(**test_input) == 2 test_input = { "s1": "dac", "s2": "bac", "s3": "cac" } assert my_solution.findMinimumOperations(**test_input) == -1 test_input = { "s1": "a", "s2": "a", "s3": "a" } assert my_so...
1,700,361,000
weekly-contest-372-separate-black-and-white-balls
https://leetcode.com/problems/separate-black-and-white-balls
separate-black-and-white-balls
{ "questionId": "3195", "questionFrontendId": "2938", "title": "Separate Black and White Balls", "titleSlug": "separate-black-and-white-balls", "isPaidOnly": false, "difficulty": "Medium", "likes": 133, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 桌子上有 n 个球,每个球的颜色不是黑色,就是白色。 给你一个长度为 n 、下标从 0 开始的二进制字符串 s,其中 1 和 0 分别代表黑色和白色的球。 在每一步中,你可以选择两个相邻的球并交换它们。 返回「将所有黑色球都移到右侧,所有白色球都移到左侧所需的 最小步数」。 示例 1: 输入:s = "101" 输出:1 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "011"。 最开始,1 没有都在右侧,需要至少 1 步将其移到右侧。 示例 2: 输入:s = "100" 输出:2 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "...
桌子上有 n 个球,每个球的颜色不是黑色,就是白色。 给你一个长度为 n 、下标从 0 开始的二进制字符串 s,其中 1 和 0 分别代表黑色和白色的球。 在每一步中,你可以选择两个相邻的球并交换它们。 返回「将所有黑色球都移到右侧,所有白色球都移到左侧所需的 最小步数」。 示例 1: 输入:s = "101" 输出:1 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "011"。 最开始,1 没有都在右侧,需要至少 1 步将其移到右侧。 示例 2: 输入:s = "100" 输出:2 解释:我们可以按以下方式将所有黑色球移到右侧: - 交换 s[0] 和 s[1],s = "010"...
my_solution = Solution() test_input = { "s": "101" } assert my_solution.minimumSteps(**test_input) == 1 test_input = { "s": "100" } assert my_solution.minimumSteps(**test_input) == 2 test_input = { "s": "0111" } assert my_solution.minimumSteps(**test_input) == 0 test_input = { "s": "11000111" } assert my_solution....
1,700,361,000
weekly-contest-372-maximum-xor-product
https://leetcode.com/problems/maximum-xor-product
maximum-xor-product
{ "questionId": "3192", "questionFrontendId": "2939", "title": "Maximum Xor Product", "titleSlug": "maximum-xor-product", "isPaidOnly": false, "difficulty": "Medium", "likes": 140, "dislikes": 63, "categoryTitle": "Algorithms" }
""" 给你三个整数a,b和n,请你返回(a XOR x) * (b XOR x)的最大值且 x需要满足 0 <= x < 2n。 由于答案可能会很大,返回它对109 + 7取余后的结果。 注意,XOR是按位异或操作。 示例 1: 输入:a = 12, b = 5, n = 4 输出:98 解释:当 x = 2 时,(a XOR x) = 14 且 (b XOR x) = 7 。所以,(a XOR x) * (b XOR x) = 98 。 98 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 示例 2: 输入:a = 6, b = 7 , n = 5 输出:930 解释:当 ...
给你三个整数a,b和n,请你返回(a XOR x) * (b XOR x)的最大值且 x需要满足 0 <= x < 2n。 由于答案可能会很大,返回它对109 + 7取余后的结果。 注意,XOR是按位异或操作。 示例 1: 输入:a = 12, b = 5, n = 4 输出:98 解释:当 x = 2 时,(a XOR x) = 14 且 (b XOR x) = 7 。所以,(a XOR x) * (b XOR x) = 98 。 98 是所有满足 0 <= x < 2n 中 (a XOR x) * (b XOR x) 的最大值。 示例 2: 输入:a = 6, b = 7 , n = 5 输出:930 解释:当 x = ...
my_solution = Solution() test_input = { "a": 12, "b": 5, "n": 4 } assert my_solution.maximumXorProduct(**test_input) == 98 test_input = { "a": 6, "b": 7, "n": 5 } assert my_solution.maximumXorProduct(**test_input) == 930 test_input = { "a": 1, "b": 6, "n": 3 } assert my_solution.maximumXorProduct(**test_input) == 1...
1,700,361,000
weekly-contest-372-find-building-where-alice-and-bob-can-meet
https://leetcode.com/problems/find-building-where-alice-and-bob-can-meet
find-building-where-alice-and-bob-can-meet
{ "questionId": "3181", "questionFrontendId": "2940", "title": "Find Building Where Alice and Bob Can Meet", "titleSlug": "find-building-where-alice-and-bob-can-meet", "isPaidOnly": false, "difficulty": "Hard", "likes": 171, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0开始的正整数数组heights,其中heights[i]表示第 i栋建筑的高度。 如果一个人在建筑i,且存在i < j的建筑j满足heights[i] < heights[j],那么这个人可以移动到建筑j。 给你另外一个数组queries,其中queries[i] = [ai, bi]。第i个查询中,Alice 在建筑ai ,Bob 在建筑bi。 请你能返回一个数组ans,其中ans[i]是第i个查询中,Alice 和 Bob 可以相遇的最左边的建筑。如果对于查询i,Alice 和 Bob 不能相遇,令ans[i] 为-1。 示例 1: 输入:heights = [6,4,8,5,2,7], queri...
给你一个下标从 0开始的正整数数组heights,其中heights[i]表示第 i栋建筑的高度。 如果一个人在建筑i,且存在i < j的建筑j满足heights[i] < heights[j],那么这个人可以移动到建筑j。 给你另外一个数组queries,其中queries[i] = [ai, bi]。第i个查询中,Alice 在建筑ai ,Bob 在建筑bi。 请你能返回一个数组ans,其中ans[i]是第i个查询中,Alice 和 Bob 可以相遇的最左边的建筑。如果对于查询i,Alice 和 Bob 不能相遇,令ans[i] 为-1。 示例 1: 输入:heights = [6,4,8,5,2,7], queries =...
my_solution = Solution() test_input = { "heights": [6,4,8,5,2,7], "queries": [[0,1],[0,3],[2,4],[3,4],[2,2]] } assert my_solution.leftmostBuildingQueries(**test_input) == [2,5,-1,5,2] test_input = { "heights": [5,3,8,2,6,1,4,6], "queries": [[0,7],[3,5],[5,2],[3,0],[1,6]] } assert my_solution.leftmostBuildingQueries(...
1,700,361,000
weekly-contest-371-maximum-strong-pair-xor-i
https://leetcode.com/problems/maximum-strong-pair-xor-i
maximum-strong-pair-xor-i
{ "questionId": "3193", "questionFrontendId": "2932", "title": "Maximum Strong Pair XOR I", "titleSlug": "maximum-strong-pair-xor-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 77, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), ...
给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, ...
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [10,100] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [5,6,25,30] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_...
1,699,756,200
weekly-contest-371-high-access-employees
https://leetcode.com/problems/high-access-employees
high-access-employees
{ "questionId": "3202", "questionFrontendId": "2933", "title": "High-Access Employees", "titleSlug": "high-access-employees", "isPaidOnly": false, "difficulty": "Medium", "likes": 110, "dislikes": 14, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 、下标从 0 开始的二维字符串数组 access_times 。对于每个 i(0 <= i <= n - 1 ),access_times[i][0] 表示某位员工的姓名,access_times[i][1] 表示该员工的访问时间。access_times 中的所有条目都发生在同一天内。 访问时间用 四位 数字表示, 符合 24 小时制 ,例如 "0800" 或 "2250" 。 如果员工在 同一小时内 访问系统 三次或更多 ,则称其为 高访问 员工。 时间间隔正好相差一小时的时间 不 被视为同一小时内。例如,"0815" 和 "0915" 不属于同一小时内。 一天开始和结束时的访问时间不被计算为同一小时...
给你一个长度为 n 、下标从 0 开始的二维字符串数组 access_times 。对于每个 i(0 <= i <= n - 1 ),access_times[i][0] 表示某位员工的姓名,access_times[i][1] 表示该员工的访问时间。access_times 中的所有条目都发生在同一天内。 访问时间用 四位 数字表示, 符合 24 小时制 ,例如 "0800" 或 "2250" 。 如果员工在 同一小时内 访问系统 三次或更多 ,则称其为 高访问 员工。 时间间隔正好相差一小时的时间 不 被视为同一小时内。例如,"0815" 和 "0915" 不属于同一小时内。 一天开始和结束时的访问时间不被计算为同一小时内。例如...
my_solution = Solution() test_input = { "access_times": [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]] } assert my_solution.findHighAccessEmployees(**test_input) == ["a"] test_input = { "access_times": [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c...
1,699,756,200
weekly-contest-371-minimum-operations-to-maximize-last-elements-in-arrays
https://leetcode.com/problems/minimum-operations-to-maximize-last-elements-in-arrays
minimum-operations-to-maximize-last-elements-in-arrays
{ "questionId": "3190", "questionFrontendId": "2934", "title": "Minimum Operations to Maximize Last Elements in Arrays", "titleSlug": "minimum-operations-to-maximize-last-elements-in-arrays", "isPaidOnly": false, "difficulty": "Medium", "likes": 152, "dislikes": 12, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,这两个数组的长度都是 n 。 你可以执行一系列 操作(可能不执行)。 在每次操作中,你可以选择一个在范围 [0, n - 1] 内的下标 i ,并交换 nums1[i] 和 nums2[i] 的值。 你的任务是找到满足以下条件所需的 最小 操作次数: nums1[n - 1] 等于 nums1 中所有元素的 最大值 ,即 nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1]) 。 nums2[n - 1] 等于 nums2 中所有元素的 最大值 ,即 nums2[n - 1] = max(nums2...
给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,这两个数组的长度都是 n 。 你可以执行一系列 操作(可能不执行)。 在每次操作中,你可以选择一个在范围 [0, n - 1] 内的下标 i ,并交换 nums1[i] 和 nums2[i] 的值。 你的任务是找到满足以下条件所需的 最小 操作次数: nums1[n - 1] 等于 nums1 中所有元素的 最大值 ,即 nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1]) 。 nums2[n - 1] 等于 nums2 中所有元素的 最大值 ,即 nums2[n - 1] = max(nums2[0],...
my_solution = Solution() test_input = { "nums1": [1,2,7], "nums2": [4,5,3] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums1": [2,3,4,5,9], "nums2": [8,8,4,4,4] } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums1": [1,5,4], "nums2": [2,5,3] } assert my_solution.minO...
1,699,756,200
weekly-contest-371-maximum-strong-pair-xor-ii
https://leetcode.com/problems/maximum-strong-pair-xor-ii
maximum-strong-pair-xor-ii
{ "questionId": "3197", "questionFrontendId": "2935", "title": "Maximum Strong Pair XOR II", "titleSlug": "maximum-strong-pair-xor-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 140, "dislikes": 1, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), ...
给你一个下标从 0 开始的整数数组 nums 。如果一对整数 x 和 y 满足以下条件,则称其为 强数对 : |x - y| <= min(x, y) 你需要从 nums 中选出两个整数,且满足:这两个整数可以形成一个强数对,并且它们的按位异或(XOR)值是在该数组所有强数对中的 最大值 。 返回数组 nums 所有可能的强数对中的 最大 异或值。 注意,你可以选择同一个整数两次来形成一个强数对。 示例 1: 输入:nums = [1,2,3,4,5] 输出:7 解释:数组 nums 中有 11 个强数对:(1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, ...
my_solution = Solution() test_input = { "nums": [1,2,3,4,5] } assert my_solution.maximumStrongPairXor(**test_input) == 7 test_input = { "nums": [10,100] } assert my_solution.maximumStrongPairXor(**test_input) == 0 test_input = { "nums": [500,520,2500,3000] } assert my_solution.maximumStrongPairXor(**test_input) == ...
1,699,756,200
biweekly-contest-117-distribute-candies-among-children-i
https://leetcode.com/problems/distribute-candies-among-children-i
distribute-candies-among-children-i
{ "questionId": "3199", "questionFrontendId": "2928", "title": "Distribute Candies Among Children I", "titleSlug": "distribute-candies-among-children-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 50, "dislikes": 25, "categoryTitle": "Algorithms" }
""" 给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ...
给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1,...
my_solution = Solution() test_input = { "n": 5, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 3, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 1, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 3 test_input = ...
1,699,713,000
biweekly-contest-117-distribute-candies-among-children-ii
https://leetcode.com/problems/distribute-candies-among-children-ii
distribute-candies-among-children-ii
{ "questionId": "3201", "questionFrontendId": "2929", "title": "Distribute Candies Among Children II", "titleSlug": "distribute-candies-among-children-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 62, "dislikes": 95, "categoryTitle": "Algorithms" }
""" 给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ...
给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入:n = 5, limit = 2 输出:3 解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。 示例 2: 输入:n = 3, limit = 3 输出:10 解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1,...
my_solution = Solution() test_input = { "n": 5, "limit": 2 } assert my_solution.distributeCandies(**test_input) == 3 test_input = { "n": 3, "limit": 3 } assert my_solution.distributeCandies(**test_input) == 10 test_input = { "n": 1, "limit": 1 } assert my_solution.distributeCandies(**test_input) == 3 test_input = ...
1,699,713,000
biweekly-contest-117-number-of-strings-which-can-be-rearranged-to-contain-substring
https://leetcode.com/problems/number-of-strings-which-can-be-rearranged-to-contain-substring
number-of-strings-which-can-be-rearranged-to-contain-substring
{ "questionId": "3200", "questionFrontendId": "2930", "title": "Number of Strings Which Can Be Rearranged to Contain Substring", "titleSlug": "number-of-strings-which-can-be-rearranged-to-contain-substring", "isPaidOnly": false, "difficulty": "Medium", "likes": 119, "dislikes": 54, "categoryTitle": "A...
""" 给你一个整数 n 。 如果一个字符串 s 只包含小写英文字母,且 将 s 的字符重新排列后,新字符串包含 子字符串 "leet" ,那么我们称字符串 s 是一个 好 字符串。 比方说: * 字符串 "lteer" 是好字符串,因为重新排列后可以得到 "leetr" 。 * "letl" 不是好字符串,因为无法重新排列并得到子字符串 "leet" 。 请你返回长度为 n 的好字符串 总 数目。 由于答案可能很大,将答案对 109 + 7 取余 后返回。 子字符串 是一个字符串中一段连续的字符序列。 示例 1: 输入:n = 4 输出:12 解释:总共有 12 个字符串重新排列后包含子字符串 "leet" :...
给你一个整数 n 。 如果一个字符串 s 只包含小写英文字母,且 将 s 的字符重新排列后,新字符串包含 子字符串 "leet" ,那么我们称字符串 s 是一个 好 字符串。 比方说: * 字符串 "lteer" 是好字符串,因为重新排列后可以得到 "leetr" 。 * "letl" 不是好字符串,因为无法重新排列并得到子字符串 "leet" 。 请你返回长度为 n 的好字符串 总 数目。 由于答案可能很大,将答案对 109 + 7 取余 后返回。 子字符串 是一个字符串中一段连续的字符序列。 示例 1: 输入:n = 4 输出:12 解释:总共有 12 个字符串重新排列后包含子字符串 "leet" :"eel...
my_solution = Solution() test_input = { "n": 4 } assert my_solution.stringCount(**test_input) == 12 test_input = { "n": 10 } assert my_solution.stringCount(**test_input) == 83943898 test_input = { "n": 1 } assert my_solution.stringCount(**test_input) == 0 test_input = { "n": 2 } assert my_solution.stringCount(**te...
1,699,713,000
biweekly-contest-117-maximum-spending-after-buying-items
https://leetcode.com/problems/maximum-spending-after-buying-items
maximum-spending-after-buying-items
{ "questionId": "3107", "questionFrontendId": "2931", "title": "Maximum Spending After Buying Items", "titleSlug": "maximum-spending-after-buying-items", "isPaidOnly": false, "difficulty": "Hard", "likes": 66, "dislikes": 20, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始大小为 m * n 的整数矩阵 values ,表示 m 个不同商店里 m * n 件不同的物品。每个商店有 n 件物品,第 i 个商店的第 j 件物品的价值为 values[i][j] 。除此以外,第 i 个商店的物品已经按照价值非递增排好序了,也就是说对于所有 0 <= j < n - 1 都有 values[i][j] >= values[i][j + 1] 。 每一天,你可以在一个商店里购买一件物品。具体来说,在第 d 天,你可以: * 选择商店 i 。 * 购买数组中最右边的物品 j ,开销为 values[i][j] * d 。换句话说,选择该商店中还没购买过的物品中最大的下标 j...
给你一个下标从 0 开始大小为 m * n 的整数矩阵 values ,表示 m 个不同商店里 m * n 件不同的物品。每个商店有 n 件物品,第 i 个商店的第 j 件物品的价值为 values[i][j] 。除此以外,第 i 个商店的物品已经按照价值非递增排好序了,也就是说对于所有 0 <= j < n - 1 都有 values[i][j] >= values[i][j + 1] 。 每一天,你可以在一个商店里购买一件物品。具体来说,在第 d 天,你可以: * 选择商店 i 。 * 购买数组中最右边的物品 j ,开销为 values[i][j] * d 。换句话说,选择该商店中还没购买过的物品中最大的下标 j ,并且...
my_solution = Solution() test_input = { "values": [[8,5,2],[6,4,1],[9,7,3]] } assert my_solution.maxSpending(**test_input) == 285 test_input = { "values": [[10,8,6,4,2],[9,7,5,3,2]] } assert my_solution.maxSpending(**test_input) == 386 test_input = { "values": [[1000000]] } assert my_solution.maxSpending(**test_inp...
1,699,713,000
weekly-contest-370-find-champion-i
https://leetcode.com/problems/find-champion-i
find-champion-i
{ "questionId": "3188", "questionFrontendId": "2923", "title": "Find Champion I", "titleSlug": "find-champion-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 84, "dislikes": 20, "categoryTitle": "Algorithms" }
""" 一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。 给你一个下标从 0 开始、大小为 n * n 的二维布尔矩阵 grid 。对于满足 0 <= i, j <= n - 1 且 i != j 的所有 i, j :如果 grid[i][j] == 1,那么 i 队比 j 队 强 ;否则,j 队比 i 队 强 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 返回这场比赛中将会成为冠军的队伍。 示例 1: 输入:grid = [[0,1],[0,0]] 输出:0 解释:比赛中有两支队伍。 grid[0][1] == 1 表示 0 队比 1 队强。所以 0 队是冠军。 示例...
一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。 给你一个下标从 0 开始、大小为 n * n 的二维布尔矩阵 grid 。对于满足 0 <= i, j <= n - 1 且 i != j 的所有 i, j :如果 grid[i][j] == 1,那么 i 队比 j 队 强 ;否则,j 队比 i 队 强 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 返回这场比赛中将会成为冠军的队伍。 示例 1: 输入:grid = [[0,1],[0,0]] 输出:0 解释:比赛中有两支队伍。 grid[0][1] == 1 表示 0 队比 1 队强。所以 0 队是冠军。 示例 2: ...
my_solution = Solution() test_input = { "grid": [[0,1],[0,0]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "grid": [[0,0,1],[1,0,1],[0,0,0]] } assert my_solution.findChampion(**test_input) == 1 test_input = { "grid": [[0,0],[1,0]] } assert my_solution.findChampion(**test_input) == 1 test_inp...
1,699,151,400
weekly-contest-370-find-champion-ii
https://leetcode.com/problems/find-champion-ii
find-champion-ii
{ "questionId": "3189", "questionFrontendId": "2924", "title": "Find Champion II", "titleSlug": "find-champion-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 92, "dislikes": 7, "categoryTitle": "Algorithms" }
""" 一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。每支队伍也是 有向无环图(DAG) 上的一个节点。 给你一个整数 n 和一个下标从 0 开始、长度为 m 的二维整数数组 edges 表示这个有向无环图,其中 edges[i] = [ui, vi] 表示图中存在一条从 ui 队到 vi 队的有向边。 从 a 队到 b 队的有向边意味着 a 队比 b 队 强 ,也就是 b 队比 a 队 弱 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 如果这场比赛存在 唯一 一个冠军,则返回将会成为冠军的队伍。否则,返回 -1 。 注意 * 环 是形如 a1, a2, ..., ...
一场比赛中共有 n 支队伍,按从 0 到  n - 1 编号。每支队伍也是 有向无环图(DAG) 上的一个节点。 给你一个整数 n 和一个下标从 0 开始、长度为 m 的二维整数数组 edges 表示这个有向无环图,其中 edges[i] = [ui, vi] 表示图中存在一条从 ui 队到 vi 队的有向边。 从 a 队到 b 队的有向边意味着 a 队比 b 队 强 ,也就是 b 队比 a 队 弱 。 在这场比赛中,如果不存在某支强于 a 队的队伍,则认为 a 队将会是 冠军 。 如果这场比赛存在 唯一 一个冠军,则返回将会成为冠军的队伍。否则,返回 -1 。 注意 * 环 是形如 a1, a2, ..., an, ...
my_solution = Solution() test_input = { "n": 3, "edges": [[0,1],[1,2]] } assert my_solution.findChampion(**test_input) == 0 test_input = { "n": 4, "edges": [[0,2],[1,3],[1,2]] } assert my_solution.findChampion(**test_input) == -1 test_input = { "n": 1, "edges": [] } assert my_solution.findChampion(**test_input) == ...
1,699,151,400
weekly-contest-370-maximum-score-after-applying-operations-on-a-tree
https://leetcode.com/problems/maximum-score-after-applying-operations-on-a-tree
maximum-score-after-applying-operations-on-a-tree
{ "questionId": "3191", "questionFrontendId": "2925", "title": "Maximum Score After Applying Operations on a Tree", "titleSlug": "maximum-score-after-applying-operations-on-a-tree", "isPaidOnly": false, "difficulty": "Medium", "likes": 243, "dislikes": 45, "categoryTitle": "Algorithms" }
""" 有一棵 n 个节点的无向树,节点编号为 0 到 n - 1 ,根节点编号为 0 。给你一个长度为 n - 1 的二维整数数组 edges 表示这棵树,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。 同时给你一个长度为 n 下标从 0 开始的整数数组 values ,其中 values[i] 表示第 i 个节点的值。 一开始你的分数为 0 ,每次操作中,你将执行: * 选择节点 i 。 * 将 values[i] 加入你的分数。 * 将 values[i] 变为 0 。 如果从根节点出发,到任意叶子节点经过的路径上的节点值之和都不等于 0 ,那么我们称这棵树是 健康的 。 ...
有一棵 n 个节点的无向树,节点编号为 0 到 n - 1 ,根节点编号为 0 。给你一个长度为 n - 1 的二维整数数组 edges 表示这棵树,其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 有一条边。 同时给你一个长度为 n 下标从 0 开始的整数数组 values ,其中 values[i] 表示第 i 个节点的值。 一开始你的分数为 0 ,每次操作中,你将执行: * 选择节点 i 。 * 将 values[i] 加入你的分数。 * 将 values[i] 变为 0 。 如果从根节点出发,到任意叶子节点经过的路径上的节点值之和都不等于 0 ,那么我们称这棵树是 健康的 。 你可以...
my_solution = Solution() test_input = { "edges": [[0,1],[0,2],[0,3],[2,4],[4,5]], "values": [5,2,5,2,1,1] } assert my_solution.maximumScoreAfterOperations(**test_input) == 11 test_input = { "edges": [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], "values": [20,10,9,7,4,3,5] } assert my_solution.maximumScoreAfterOperations(**...
1,699,151,400
weekly-contest-370-maximum-balanced-subsequence-sum
https://leetcode.com/problems/maximum-balanced-subsequence-sum
maximum-balanced-subsequence-sum
{ "questionId": "3184", "questionFrontendId": "2926", "title": "Maximum Balanced Subsequence Sum", "titleSlug": "maximum-balanced-subsequence-sum", "isPaidOnly": false, "difficulty": "Hard", "likes": 133, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 nums 一个长度为 k 的 子序列 指的是选出 k 个 下标 i0 < i1 < ... < ik-1 ,如果这个子序列满足以下条件,我们说它是 平衡的 : * 对于范围 [1, k - 1] 内的所有 j ,nums[ij] - nums[ij-1] >= ij - ij-1 都成立。 nums 长度为 1 的 子序列 是平衡的。 请你返回一个整数,表示 nums 平衡 子序列里面的 最大元素和 。 一个数组的 子序列 指的是从原数组中删除一些元素(也可能一个元素也不删除)后,剩余元素保持相对顺序得到的 非空 新数组。 示例 1: 输入:nums = [3...
给你一个下标从 0 开始的整数数组 nums 。 nums 一个长度为 k 的 子序列 指的是选出 k 个 下标 i0 < i1 < ... < ik-1 ,如果这个子序列满足以下条件,我们说它是 平衡的 : * 对于范围 [1, k - 1] 内的所有 j ,nums[ij] - nums[ij-1] >= ij - ij-1 都成立。 nums 长度为 1 的 子序列 是平衡的。 请你返回一个整数,表示 nums 平衡 子序列里面的 最大元素和 。 一个数组的 子序列 指的是从原数组中删除一些元素(也可能一个元素也不删除)后,剩余元素保持相对顺序得到的 非空 新数组。 示例 1: 输入:nums = [3,3,5...
my_solution = Solution() test_input = { "nums": [3,3,5,6] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 14 test_input = { "nums": [5,-1,-3,8] } assert my_solution.maxBalancedSubsequenceSum(**test_input) == 13 test_input = { "nums": [-2,-1] } assert my_solution.maxBalancedSubsequenceSum(**test_inpu...
1,699,151,400
weekly-contest-369-find-the-k-or-of-an-array
https://leetcode.com/problems/find-the-k-or-of-an-array
find-the-k-or-of-an-array
{ "questionId": "3183", "questionFrontendId": "2917", "title": "Find the K-or of an Array", "titleSlug": "find-the-k-or-of-an-array", "isPaidOnly": false, "difficulty": "Easy", "likes": 55, "dislikes": 201, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。 nums 中的 K-or 是一个满足以下条件的非负整数: * 只有在 nums 中,至少存在 k 个元素的第 i 位值为 1 ,那么 K-or 中的第 i 位的值才是 1 。 返回 nums 的 K-or 值。 注意 :对于整数 x ,如果 (2i AND x) == 2i ,则 x 中的第 i 位值为 1 ,其中 AND 为按位与运算符。 示例 1: 输入:nums = [7,12,9,8,9,15], k = 4 输出:9 解释:nums[0]、nums[2]、nums[4] 和 nums[5] 的第 0 位的值为 1 。 nums[0] 和...
给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。 nums 中的 K-or 是一个满足以下条件的非负整数: * 只有在 nums 中,至少存在 k 个元素的第 i 位值为 1 ,那么 K-or 中的第 i 位的值才是 1 。 返回 nums 的 K-or 值。 注意 :对于整数 x ,如果 (2i AND x) == 2i ,则 x 中的第 i 位值为 1 ,其中 AND 为按位与运算符。 示例 1: 输入:nums = [7,12,9,8,9,15], k = 4 输出:9 解释:nums[0]、nums[2]、nums[4] 和 nums[5] 的第 0 位的值为 1 。 nums[0] 和 num...
my_solution = Solution() test_input = { "nums": [7,12,9,8,9,15], "k": 4 } assert my_solution.findKOr(**test_input) == 9 test_input = { "nums": [2,12,1,11,4,5], "k": 6 } assert my_solution.findKOr(**test_input) == 0 test_input = { "nums": [10,8,5,9,11,6,8], "k": 1 } assert my_solution.findKOr(**test_input) == 15 te...
1,698,546,600
weekly-contest-369-minimum-equal-sum-of-two-arrays-after-replacing-zeros
https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros
minimum-equal-sum-of-two-arrays-after-replacing-zeros
{ "questionId": "3171", "questionFrontendId": "2918", "title": "Minimum Equal Sum of Two Arrays After Replacing Zeros", "titleSlug": "minimum-equal-sum-of-two-arrays-after-replacing-zeros", "isPaidOnly": false, "difficulty": "Medium", "likes": 131, "dislikes": 15, "categoryTitle": "Algorithms" }
""" 给你两个由正整数和 0 组成的数组 nums1 和 nums2 。 你必须将两个数组中的 所有 0 替换为 严格 正整数,并且满足两个数组中所有元素的和 相等 。 返回 最小 相等和 ,如果无法使两数组相等,则返回 -1 。 示例 1: 输入:nums1 = [3,2,0,1,0], nums2 = [6,5,0] 输出:12 解释:可以按下述方式替换数组中的 0 : - 用 2 和 4 替换 nums1 中的两个 0 。得到 nums1 = [3,2,2,1,4] 。 - 用 1 替换 nums2 中的一个 0 。得到 nums2 = [6,5,1] 。 两个数组的元素和相等,都等于 12 。可以证明这是可以获得的...
给你两个由正整数和 0 组成的数组 nums1 和 nums2 。 你必须将两个数组中的 所有 0 替换为 严格 正整数,并且满足两个数组中所有元素的和 相等 。 返回 最小 相等和 ,如果无法使两数组相等,则返回 -1 。 示例 1: 输入:nums1 = [3,2,0,1,0], nums2 = [6,5,0] 输出:12 解释:可以按下述方式替换数组中的 0 : - 用 2 和 4 替换 nums1 中的两个 0 。得到 nums1 = [3,2,2,1,4] 。 - 用 1 替换 nums2 中的一个 0 。得到 nums2 = [6,5,1] 。 两个数组的元素和相等,都等于 12 。可以证明这是可以获得的最小相等...
my_solution = Solution() test_input = { "nums1": [3,2,0,1,0], "nums2": [6,5,0] } assert my_solution.minSum(**test_input) == 12 test_input = { "nums1": [2,0,2,0], "nums2": [1,4] } assert my_solution.minSum(**test_input) == -1 test_input = { "nums1": [0,7,28,17,18], "nums2": [1,2,6,26,1,0,27,3,0,30] } assert my_solut...
1,698,546,600
weekly-contest-369-minimum-increment-operations-to-make-array-beautiful
https://leetcode.com/problems/minimum-increment-operations-to-make-array-beautiful
minimum-increment-operations-to-make-array-beautiful
{ "questionId": "3178", "questionFrontendId": "2919", "title": "Minimum Increment Operations to Make Array Beautiful", "titleSlug": "minimum-increment-operations-to-make-array-beautiful", "isPaidOnly": false, "difficulty": "Medium", "likes": 254, "dislikes": 15, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、长度为 n 的整数数组 nums ,和一个整数 k 。 你可以执行下述 递增 运算 任意 次(可以是 0 次): * 从范围 [0, n - 1] 中选择一个下标 i ,并将 nums[i] 的值加 1 。 如果数组中任何长度 大于或等于 3 的子数组,其 最大 元素都大于或等于 k ,则认为数组是一个 美丽数组 。 以整数形式返回使数组变为 美丽数组 需要执行的 最小 递增运算数。 子数组是数组中的一个连续 非空 元素序列。 示例 1: 输入:nums = [2,3,0,0,2], k = 4 输出:3 解释:可以执行下述递增运算,使 nums 变为美丽数组: 选择下标 i = 1...
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,和一个整数 k 。 你可以执行下述 递增 运算 任意 次(可以是 0 次): * 从范围 [0, n - 1] 中选择一个下标 i ,并将 nums[i] 的值加 1 。 如果数组中任何长度 大于或等于 3 的子数组,其 最大 元素都大于或等于 k ,则认为数组是一个 美丽数组 。 以整数形式返回使数组变为 美丽数组 需要执行的 最小 递增运算数。 子数组是数组中的一个连续 非空 元素序列。 示例 1: 输入:nums = [2,3,0,0,2], k = 4 输出:3 解释:可以执行下述递增运算,使 nums 变为美丽数组: 选择下标 i = 1 ,并且...
my_solution = Solution() test_input = { "nums": [2,3,0,0,2], "k": 4 } assert my_solution.minIncrementOperations(**test_input) == 3 test_input = { "nums": [0,1,3,3], "k": 5 } assert my_solution.minIncrementOperations(**test_input) == 2 test_input = { "nums": [1,1,2], "k": 1 } assert my_solution.minIncrementOperation...
1,698,546,600
weekly-contest-369-maximum-points-after-collecting-coins-from-all-nodes
https://leetcode.com/problems/maximum-points-after-collecting-coins-from-all-nodes
maximum-points-after-collecting-coins-from-all-nodes
{ "questionId": "3179", "questionFrontendId": "2920", "title": "Maximum Points After Collecting Coins From All Nodes", "titleSlug": "maximum-points-after-collecting-coins-from-all-nodes", "isPaidOnly": false, "difficulty": "Hard", "likes": 161, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 有一棵由 n 个节点组成的无向树,以 0  为根节点,节点编号从 0 到 n - 1 。给你一个长度为 n - 1 的二维 整数 数组 edges ,其中 edges[i] = [ai, bi] 表示在树上的节点 ai 和 bi 之间存在一条边。另给你一个下标从 0 开始、长度为 n 的数组 coins 和一个整数 k ,其中 coins[i] 表示节点 i 处的金币数量。 从根节点开始,你必须收集所有金币。要想收集节点上的金币,必须先收集该节点的祖先节点上的金币。 节点 i 上的金币可以用下述方法之一进行收集: * 收集所有金币,得到共计 coins[i] - k 点积分。如果 coins[i] - k 是负数,你...
有一棵由 n 个节点组成的无向树,以 0  为根节点,节点编号从 0 到 n - 1 。给你一个长度为 n - 1 的二维 整数 数组 edges ,其中 edges[i] = [ai, bi] 表示在树上的节点 ai 和 bi 之间存在一条边。另给你一个下标从 0 开始、长度为 n 的数组 coins 和一个整数 k ,其中 coins[i] 表示节点 i 处的金币数量。 从根节点开始,你必须收集所有金币。要想收集节点上的金币,必须先收集该节点的祖先节点上的金币。 节点 i 上的金币可以用下述方法之一进行收集: * 收集所有金币,得到共计 coins[i] - k 点积分。如果 coins[i] - k 是负数,你将会失去...
my_solution = Solution() test_input = { "edges": [[0,1],[1,2],[2,3]], "coins": [10,10,3,3], "k": 5 } assert my_solution.maximumPoints(**test_input) == 11 test_input = { "edges": [[0,1],[0,2]], "coins": [8,4,4], "k": 0 } assert my_solution.maximumPoints(**test_input) == 16 test_input = { "edges": [[0,1],[2,0],[0,3],...
1,698,546,600
biweekly-contest-116-subarrays-distinct-element-sum-of-squares-i
https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i
subarrays-distinct-element-sum-of-squares-i
{ "questionId": "3163", "questionFrontendId": "2913", "title": "Subarrays Distinct Element Sum of Squares I", "titleSlug": "subarrays-distinct-element-sum-of-squares-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 82, "dislikes": 9, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为...
给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为: [1...
my_solution = Solution() test_input = { "nums": [1,2,1] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [1,1] } assert my_solution.sumCounts(**test_input) == 3 test_input = { "nums": [2,2,5,5] } assert my_solution.sumCounts(**test_input) == 22 test_input = { "nums": [5,2,4,2,1,3,2,4,3,1] ...
1,698,503,400
biweekly-contest-116-minimum-number-of-changes-to-make-binary-string-beautiful
https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful
minimum-number-of-changes-to-make-binary-string-beautiful
{ "questionId": "3174", "questionFrontendId": "2914", "title": "Minimum Number of Changes to Make Binary String Beautiful", "titleSlug": "minimum-number-of-changes-to-make-binary-string-beautiful", "isPaidOnly": false, "difficulty": "Medium", "likes": 111, "dislikes": 4, "categoryTitle": "Algorithms" ...
""" 给你一个长度为偶数下标从 0 开始的二进制字符串 s 。 如果可以将一个字符串分割成一个或者更多满足以下条件的子字符串,那么我们称这个字符串是 美丽的 : * 每个子字符串的长度都是 偶数 。 * 每个子字符串都 只 包含 1 或 只 包含 0 。 你可以将 s 中任一字符改成 0 或者 1 。 请你返回让字符串 s 美丽的 最少 字符修改次数。 示例 1: 输入:s = "1001" 输出:2 解释:我们将 s[1] 改为 1 ,且将 s[3] 改为 0 ,得到字符串 "1100" 。 字符串 "1100" 是美丽的,因为我们可以将它分割成 "11|00" 。 将字符串变美丽最少需要 2 次修改。 示例 ...
给你一个长度为偶数下标从 0 开始的二进制字符串 s 。 如果可以将一个字符串分割成一个或者更多满足以下条件的子字符串,那么我们称这个字符串是 美丽的 : * 每个子字符串的长度都是 偶数 。 * 每个子字符串都 只 包含 1 或 只 包含 0 。 你可以将 s 中任一字符改成 0 或者 1 。 请你返回让字符串 s 美丽的 最少 字符修改次数。 示例 1: 输入:s = "1001" 输出:2 解释:我们将 s[1] 改为 1 ,且将 s[3] 改为 0 ,得到字符串 "1100" 。 字符串 "1100" 是美丽的,因为我们可以将它分割成 "11|00" 。 将字符串变美丽最少需要 2 次修改。 示例 2: ...
my_solution = Solution() test_input = { "s": "1001" } assert my_solution.minChanges(**test_input) == 2 test_input = { "s": "10" } assert my_solution.minChanges(**test_input) == 1 test_input = { "s": "0000" } assert my_solution.minChanges(**test_input) == 0 test_input = { "s": "11000111" } assert my_solution.minCha...
1,698,503,400
biweekly-contest-116-length-of-the-longest-subsequence-that-sums-to-target
https://leetcode.com/problems/length-of-the-longest-subsequence-that-sums-to-target
length-of-the-longest-subsequence-that-sums-to-target
{ "questionId": "3106", "questionFrontendId": "2915", "title": "Length of the Longest Subsequence That Sums to Target", "titleSlug": "length-of-the-longest-subsequence-that-sums-to-target", "isPaidOnly": false, "difficulty": "Medium", "likes": 141, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 和一个整数 target 。 返回和为 target 的 nums 子序列中,子序列 长度的最大值 。如果不存在和为 target 的子序列,返回 -1 。 子序列 指的是从原数组中删除一些或者不删除任何元素后,剩余元素保持原来的顺序构成的数组。 示例 1: 输入:nums = [1,2,3,4,5], target = 9 输出:3 解释:总共有 3 个子序列的和为 9 :[4,5] ,[1,3,5] 和 [2,3,4] 。最长的子序列是 [1,3,5] 和 [2,3,4] 。所以答案为 3 。 示例 2: 输入:nums = [4,1,3,2,1,5], targ...
给你一个下标从 0 开始的整数数组 nums 和一个整数 target 。 返回和为 target 的 nums 子序列中,子序列 长度的最大值 。如果不存在和为 target 的子序列,返回 -1 。 子序列 指的是从原数组中删除一些或者不删除任何元素后,剩余元素保持原来的顺序构成的数组。 示例 1: 输入:nums = [1,2,3,4,5], target = 9 输出:3 解释:总共有 3 个子序列的和为 9 :[4,5] ,[1,3,5] 和 [2,3,4] 。最长的子序列是 [1,3,5] 和 [2,3,4] 。所以答案为 3 。 示例 2: 输入:nums = [4,1,3,2,1,5], target =...
my_solution = Solution() test_input = { "nums": [1,2,3,4,5], "target": 9 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 3 test_input = { "nums": [4,1,3,2,1,5], "target": 7 } assert my_solution.lengthOfLongestSubsequence(**test_input) == 4 test_input = { "nums": [1,1,5,4,5], "target": 3 } assert my...
1,698,503,400
biweekly-contest-116-subarrays-distinct-element-sum-of-squares-ii
https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-ii
subarrays-distinct-element-sum-of-squares-ii
{ "questionId": "3139", "questionFrontendId": "2916", "title": "Subarrays Distinct Element Sum of Squares II", "titleSlug": "subarrays-distinct-element-sum-of-squares-ii", "isPaidOnly": false, "difficulty": "Hard", "likes": 115, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为...
给你一个下标从 0 开始的整数数组 nums 。 定义 nums 一个子数组的 不同计数 值如下: * 令 nums[i..j] 表示 nums 中所有下标在 i 到 j 范围内的元素构成的子数组(满足 0 <= i <= j < nums.length ),那么我们称子数组 nums[i..j] 中不同值的数目为 nums[i..j] 的不同计数。 请你返回 nums 中所有子数组的 不同计数 的 平方 和。 由于答案可能会很大,请你将它对 109 + 7 取余 后返回。 子数组指的是一个数组里面一段连续 非空 的元素序列。 示例 1: 输入:nums = [1,2,1] 输出:15 解释:六个子数组分别为: [1...
my_solution = Solution() test_input = { "nums": [1,2,1] } assert my_solution.sumCounts(**test_input) == 15 test_input = { "nums": [2,2] } assert my_solution.sumCounts(**test_input) == 3 test_input = { "nums": [2,2,5,5] } assert my_solution.sumCounts(**test_input) == 22 test_input = { "nums": [5,2,4,2,1,3,2,4,3,1] ...
1,698,503,400
weekly-contest-368-minimum-sum-of-mountain-triplets-i
https://leetcode.com/problems/minimum-sum-of-mountain-triplets-i
minimum-sum-of-mountain-triplets-i
{ "questionId": "3176", "questionFrontendId": "2908", "title": "Minimum Sum of Mountain Triplets I", "titleSlug": "minimum-sum-of-mountain-triplets-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 113, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素...
给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素和等于 ...
my_solution = Solution() test_input = { "nums": [8,6,1,5,3] } assert my_solution.minimumSum(**test_input) == 9 test_input = { "nums": [5,4,8,7,10,2] } assert my_solution.minimumSum(**test_input) == 13 test_input = { "nums": [6,5,4,3,4,5] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [5...
1,697,941,800
weekly-contest-368-minimum-sum-of-mountain-triplets-ii
https://leetcode.com/problems/minimum-sum-of-mountain-triplets-ii
minimum-sum-of-mountain-triplets-ii
{ "questionId": "3186", "questionFrontendId": "2909", "title": "Minimum Sum of Mountain Triplets II", "titleSlug": "minimum-sum-of-mountain-triplets-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 160, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素...
给你一个下标从 0 开始的整数数组 nums 。 如果下标三元组 (i, j, k) 满足下述全部条件,则认为它是一个 山形三元组 : * i < j < k * nums[i] < nums[j] 且 nums[k] < nums[j] 请你找出 nums 中 元素和最小 的山形三元组,并返回其 元素和 。如果不存在满足条件的三元组,返回 -1 。 示例 1: 输入:nums = [8,6,1,5,3] 输出:9 解释:三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为: - 2 < 3 < 4 - nums[2] < nums[3] 且 nums[4] < nums[3] 这个三元组的元素和等于 ...
my_solution = Solution() test_input = { "nums": [8,6,1,5,3] } assert my_solution.minimumSum(**test_input) == 9 test_input = { "nums": [5,4,8,7,10,2] } assert my_solution.minimumSum(**test_input) == 13 test_input = { "nums": [6,5,4,3,4,5] } assert my_solution.minimumSum(**test_input) == -1 test_input = { "nums": [5...
1,697,941,800
weekly-contest-368-minimum-number-of-groups-to-create-a-valid-assignment
https://leetcode.com/problems/minimum-number-of-groups-to-create-a-valid-assignment
minimum-number-of-groups-to-create-a-valid-assignment
{ "questionId": "3166", "questionFrontendId": "2910", "title": "Minimum Number of Groups to Create a Valid Assignment", "titleSlug": "minimum-number-of-groups-to-create-a-valid-assignment", "isPaidOnly": false, "difficulty": "Medium", "likes": 231, "dislikes": 151, "categoryTitle": "Algorithms" }
""" 给你一个长度为 n 下标从 0 开始的整数数组 nums 。 我们想将下标进行分组,使得 [0, n - 1] 内所有下标 i 都 恰好 被分到其中一组。 如果以下条件成立,我们说这个分组方案是合法的: * 对于每个组 g ,同一组内所有下标在 nums 中对应的数值都相等。 * 对于任意两个组 g1 和 g2 ,两个组中 下标数量 的 差值不超过 1 。 请你返回一个整数,表示得到一个合法分组方案的 最少 组数。 示例 1: 输入:nums = [3,2,3,2,3] 输出:2 解释:一个得到 2 个分组的方案如下,中括号内的数字都是下标: 组 1 -> [0,2,4] 组 2 -> [1,3] 所有下标都...
给你一个长度为 n 下标从 0 开始的整数数组 nums 。 我们想将下标进行分组,使得 [0, n - 1] 内所有下标 i 都 恰好 被分到其中一组。 如果以下条件成立,我们说这个分组方案是合法的: * 对于每个组 g ,同一组内所有下标在 nums 中对应的数值都相等。 * 对于任意两个组 g1 和 g2 ,两个组中 下标数量 的 差值不超过 1 。 请你返回一个整数,表示得到一个合法分组方案的 最少 组数。 示例 1: 输入:nums = [3,2,3,2,3] 输出:2 解释:一个得到 2 个分组的方案如下,中括号内的数字都是下标: 组 1 -> [0,2,4] 组 2 -> [1,3] 所有下标都只属于一...
my_solution = Solution() test_input = { "nums": [3,2,3,2,3] } assert my_solution.minGroupsForValidAssignment(**test_input) == 2 test_input = { "nums": [10,10,10,3,1,1] } assert my_solution.minGroupsForValidAssignment(**test_input) == 4 test_input = { "nums": [1,1] } assert my_solution.minGroupsForValidAssignment(**...
1,697,941,800
weekly-contest-368-minimum-changes-to-make-k-semi-palindromes
https://leetcode.com/problems/minimum-changes-to-make-k-semi-palindromes
minimum-changes-to-make-k-semi-palindromes
{ "questionId": "2879", "questionFrontendId": "2911", "title": "Minimum Changes to Make K Semi-palindromes", "titleSlug": "minimum-changes-to-make-k-semi-palindromes", "isPaidOnly": false, "difficulty": "Hard", "likes": 50, "dislikes": 78, "categoryTitle": "Algorithms" }
""" 给你一个字符串 s 和一个整数 k ,请你将 s 分成 k 个 子字符串 ,使得每个 子字符串 变成 半回文串 需要修改的字符数目最少。 请你返回一个整数,表示需要修改的 最少 字符数目。 注意: * 如果一个字符串从左往右和从右往左读是一样的,那么它是一个 回文串 。 * 如果长度为 len 的字符串存在一个满足 1 <= d < len 的正整数 d ,len % d == 0 成立且所有对 d 做除法余数相同的下标对应的字符连起来得到的字符串都是 回文串 ,那么我们说这个字符串是 半回文串 。比方说 "aa" ,"aba" ,"adbgad" 和 "abab" 都是 半回文串 ,而 "a" ,"ab" 和 "...
给你一个字符串 s 和一个整数 k ,请你将 s 分成 k 个 子字符串 ,使得每个 子字符串 变成 半回文串 需要修改的字符数目最少。 请你返回一个整数,表示需要修改的 最少 字符数目。 注意: * 如果一个字符串从左往右和从右往左读是一样的,那么它是一个 回文串 。 * 如果长度为 len 的字符串存在一个满足 1 <= d < len 的正整数 d ,len % d == 0 成立且所有对 d 做除法余数相同的下标对应的字符连起来得到的字符串都是 回文串 ,那么我们说这个字符串是 半回文串 。比方说 "aa" ,"aba" ,"adbgad" 和 "abab" 都是 半回文串 ,而 "a" ,"ab" 和 "abca...
my_solution = Solution() test_input = { "s": "abcac", "k": 2 } assert my_solution.minimumChanges(**test_input) == 1 test_input = { "s": "abcdef", "k": 2 } assert my_solution.minimumChanges(**test_input) == 2 test_input = { "s": "aabbaa", "k": 3 } assert my_solution.minimumChanges(**test_input) == 0 test_input = { ...
1,697,941,800
weekly-contest-367-find-indices-with-index-and-value-difference-i
https://leetcode.com/problems/find-indices-with-index-and-value-difference-i
find-indices-with-index-and-value-difference-i
{ "questionId": "3165", "questionFrontendId": "2903", "title": "Find Indices With Index and Value Difference I", "titleSlug": "find-indices-with-index-and-value-difference-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 97, "dislikes": 7, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j ...
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j 可能 相...
my_solution = Solution() test_input = { "nums": [5,1,4,1], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [2,1], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [1,2,3...
1,697,337,000
weekly-contest-367-shortest-and-lexicographically-smallest-beautiful-string
https://leetcode.com/problems/shortest-and-lexicographically-smallest-beautiful-string
shortest-and-lexicographically-smallest-beautiful-string
{ "questionId": "3150", "questionFrontendId": "2904", "title": "Shortest and Lexicographically Smallest Beautiful String", "titleSlug": "shortest-and-lexicographically-smallest-beautiful-string", "isPaidOnly": false, "difficulty": "Medium", "likes": 147, "dislikes": 8, "categoryTitle": "Algorithms" }
""" 给你一个二进制字符串 s 和一个正整数 k 。 如果 s 的某个子字符串中 1 的个数恰好等于 k ,则称这个子字符串是一个 美丽子字符串 。 令 len 等于 最短 美丽子字符串的长度。 返回长度等于 len 且字典序 最小 的美丽子字符串。如果 s 中不含美丽子字符串,则返回一个 空 字符串。 对于相同长度的两个字符串 a 和 b ,如果在 a 和 b 出现不同的第一个位置上,a 中该位置上的字符严格大于 b 中的对应字符,则认为字符串 a 字典序 大于 字符串 b 。 * 例如,"abcd" 的字典序大于 "abcc" ,因为两个字符串出现不同的第一个位置对应第四个字符,而 d 大于 c 。 示例 1: ...
给你一个二进制字符串 s 和一个正整数 k 。 如果 s 的某个子字符串中 1 的个数恰好等于 k ,则称这个子字符串是一个 美丽子字符串 。 令 len 等于 最短 美丽子字符串的长度。 返回长度等于 len 且字典序 最小 的美丽子字符串。如果 s 中不含美丽子字符串,则返回一个 空 字符串。 对于相同长度的两个字符串 a 和 b ,如果在 a 和 b 出现不同的第一个位置上,a 中该位置上的字符严格大于 b 中的对应字符,则认为字符串 a 字典序 大于 字符串 b 。 * 例如,"abcd" 的字典序大于 "abcc" ,因为两个字符串出现不同的第一个位置对应第四个字符,而 d 大于 c 。 示例 1: 输入:...
my_solution = Solution() test_input = { "s": "100011001", "k": 3 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11001" test_input = { "s": "1011", "k": 2 } assert my_solution.shortestBeautifulSubstring(**test_input) == "11" test_input = { "s": "000", "k": 1 } assert my_solution.shortestBeautifulS...
1,697,337,000
weekly-contest-367-find-indices-with-index-and-value-difference-ii
https://leetcode.com/problems/find-indices-with-index-and-value-difference-ii
find-indices-with-index-and-value-difference-ii
{ "questionId": "3170", "questionFrontendId": "2905", "title": "Find Indices With Index and Value Difference II", "titleSlug": "find-indices-with-index-and-value-difference-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 218, "dislikes": 6, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j ...
给你一个下标从 0 开始、长度为 n 的整数数组 nums ,以及整数 indexDifference 和整数 valueDifference 。 你的任务是从范围 [0, n - 1] 内找出  2 个满足下述所有条件的下标 i 和 j : * abs(i - j) >= indexDifference 且 * abs(nums[i] - nums[j]) >= valueDifference 返回整数数组 answer。如果存在满足题目要求的两个下标,则 answer = [i, j] ;否则,answer = [-1, -1] 。如果存在多组可供选择的下标对,只需要返回其中任意一组即可。 注意:i 和 j 可能 相...
my_solution = Solution() test_input = { "nums": [5,1,4,1], "indexDifference": 2, "valueDifference": 4 } assert my_solution.findIndices(**test_input) == [0,3] test_input = { "nums": [2,1], "indexDifference": 0, "valueDifference": 0 } assert my_solution.findIndices(**test_input) == [0,0] test_input = { "nums": [1,2,3...
1,697,337,000
weekly-contest-367-construct-product-matrix
https://leetcode.com/problems/construct-product-matrix
construct-product-matrix
{ "questionId": "3031", "questionFrontendId": "2906", "title": "Construct Product Matrix", "titleSlug": "construct-product-matrix", "isPaidOnly": false, "difficulty": "Medium", "likes": 187, "dislikes": 11, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始、大小为 n * m 的二维整数矩阵 grid ,定义一个下标从 0 开始、大小为 n * m 的的二维矩阵 p。如果满足以下条件,则称 p 为 grid 的 乘积矩阵 : * 对于每个元素 p[i][j] ,它的值等于除了 grid[i][j] 外所有元素的乘积。乘积对 12345 取余数。 返回 grid 的乘积矩阵。 示例 1: 输入:grid = [[1,2],[3,4]] 输出:[[24,12],[8,6]] 解释:p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24 p[0][1] = grid[0][0]...
给你一个下标从 0 开始、大小为 n * m 的二维整数矩阵 grid ,定义一个下标从 0 开始、大小为 n * m 的的二维矩阵 p。如果满足以下条件,则称 p 为 grid 的 乘积矩阵 : * 对于每个元素 p[i][j] ,它的值等于除了 grid[i][j] 外所有元素的乘积。乘积对 12345 取余数。 返回 grid 的乘积矩阵。 示例 1: 输入:grid = [[1,2],[3,4]] 输出:[[24,12],[8,6]] 解释:p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24 p[0][1] = grid[0][0] * g...
my_solution = Solution() test_input = { "grid": [[1,2],[3,4]] } assert my_solution.constructProductMatrix(**test_input) == [[24,12],[8,6]] test_input = { "grid": [[12345],[2],[1]] } assert my_solution.constructProductMatrix(**test_input) == [[2],[0],[0]] test_input = { "grid": [[1],[2]] } assert my_solution.constru...
1,697,337,000
biweekly-contest-115-last-visited-integers
https://leetcode.com/problems/last-visited-integers
last-visited-integers
{ "questionId": "3164", "questionFrontendId": "2899", "title": "Last Visited Integers", "titleSlug": "last-visited-integers", "isPaidOnly": false, "difficulty": "Easy", "likes": 74, "dislikes": 139, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的字符串数组 words ,其中 words[i] 要么是一个字符串形式的正整数,要么是字符串 "prev" 。 我们从数组的开头开始遍历,对于 words 中的每个 "prev" 字符串,找到 words 中的 上一个遍历的整数 ,定义如下: * k 表示到当前位置为止的连续 "prev" 字符串数目(包含当前字符串),令下标从 0 开始的 整数 数组 nums 表示目前为止遍历过的所有整数,同时用 nums_reverse 表示 nums 反转得到的数组,那么当前 "prev" 对应的 上一个遍历的整数 是 nums_reverse 数组中下标为 (k - 1) 的整数。 * 如果 k 比...
给你一个下标从 0 开始的字符串数组 words ,其中 words[i] 要么是一个字符串形式的正整数,要么是字符串 "prev" 。 我们从数组的开头开始遍历,对于 words 中的每个 "prev" 字符串,找到 words 中的 上一个遍历的整数 ,定义如下: * k 表示到当前位置为止的连续 "prev" 字符串数目(包含当前字符串),令下标从 0 开始的 整数 数组 nums 表示目前为止遍历过的所有整数,同时用 nums_reverse 表示 nums 反转得到的数组,那么当前 "prev" 对应的 上一个遍历的整数 是 nums_reverse 数组中下标为 (k - 1) 的整数。 * 如果 k 比目前为止...
my_solution = Solution() test_input = { "words": ["1","2","prev","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [2,1,-1] test_input = { "words": ["1","prev","2","prev","prev"] } assert my_solution.lastVisitedIntegers(**test_input) == [1,2,1] test_input = { "words": ["prev","prev","prev","2...
1,697,293,800
biweekly-contest-115-longest-unequal-adjacent-groups-subsequence-i
https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i
longest-unequal-adjacent-groups-subsequence-i
{ "questionId": "3143", "questionFrontendId": "2900", "title": "Longest Unequal Adjacent Groups Subsequence I", "titleSlug": "longest-unequal-adjacent-groups-subsequence-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 86, "dislikes": 26, "categoryTitle": "Algorithms" }
""" 给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的 二进制 数组 groups ,两个数组长度都是 n 。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 请你返回一个字符串数组,它是下标子序列 依次 对应 words 数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。 子序列 指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不...
给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的 二进制 数组 groups ,两个数组长度都是 n 。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 请你返回一个字符串数组,它是下标子序列 依次 对应 words 数组中的字符串连接形成的字符串数组。如果有多个答案,返回任意一个。 子序列 指的是从原数组中删掉一些(也可能一个也不删掉)元素,剩余元素不改变相对...
my_solution = Solution() test_input = { "n": 3, "words": ["e","a","b"], "groups": [0,0,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["e","b"] test_input = { "n": 4, "words": ["a","b","c","d"], "groups": [1,0,1,1] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["a","b","c"...
1,697,293,800
biweekly-contest-115-longest-unequal-adjacent-groups-subsequence-ii
https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-ii
longest-unequal-adjacent-groups-subsequence-ii
{ "questionId": "3142", "questionFrontendId": "2901", "title": "Longest Unequal Adjacent Groups Subsequence II", "titleSlug": "longest-unequal-adjacent-groups-subsequence-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 172, "dislikes": 16, "categoryTitle": "Algorithms" }
""" 给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的数组 groups ,两个数组长度都是 n 。 两个长度相等字符串的 汉明距离 定义为对应位置字符 不同 的数目。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,它需要满足以下条件: * 相邻 下标对应的 groups 值 不同。即,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 * 对于所有 0 < j + 1 < k 的下标 j ,都满足 wo...
给你一个整数 n 和一个下标从 0 开始的字符串数组 words ,和一个下标从 0 开始的数组 groups ,两个数组长度都是 n 。 两个长度相等字符串的 汉明距离 定义为对应位置字符 不同 的数目。 你需要从下标 [0, 1, ..., n - 1] 中选出一个 最长子序列 ,将这个子序列记作长度为 k 的 [i0, i1, ..., ik - 1] ,它需要满足以下条件: * 相邻 下标对应的 groups 值 不同。即,对于所有满足 0 < j + 1 < k 的 j 都有 groups[ij] != groups[ij + 1] 。 * 对于所有 0 < j + 1 < k 的下标 j ,都满足 words[...
my_solution = Solution() test_input = { "n": 3, "words": ["bab","dab","cab"], "groups": [1,2,2] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["bab","cab"] test_input = { "n": 4, "words": ["a","b","c","d"], "groups": [1,2,3,4] } assert my_solution.getWordsInLongestSubsequence(**test_input) == ["...
1,697,293,800
biweekly-contest-115-count-of-sub-multisets-with-bounded-sum
https://leetcode.com/problems/count-of-sub-multisets-with-bounded-sum
count-of-sub-multisets-with-bounded-sum
{ "questionId": "3091", "questionFrontendId": "2902", "title": "Count of Sub-Multisets With Bounded Sum", "titleSlug": "count-of-sub-multisets-with-bounded-sum", "isPaidOnly": false, "difficulty": "Hard", "likes": 125, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的非负整数数组 nums 和两个整数 l 和 r 。 请你返回 nums 中子多重集合的和在闭区间 [l, r] 之间的 子多重集合的数目 。 由于答案可能很大,请你将答案对 109 + 7 取余后返回。 子多重集合 指的是从数组中选出一些元素构成的 无序 集合,每个元素 x 出现的次数可以是 0, 1, ..., occ[x] 次,其中 occ[x] 是元素 x 在数组中的出现次数。 注意: * 如果两个子多重集合中的元素排序后一模一样,那么它们两个是相同的 子多重集合 。 * 空 集合的和是 0 。 示例 1: 输入:nums = [1,2,2,3], l = 6, r = 6 ...
给你一个下标从 0 开始的非负整数数组 nums 和两个整数 l 和 r 。 请你返回 nums 中子多重集合的和在闭区间 [l, r] 之间的 子多重集合的数目 。 由于答案可能很大,请你将答案对 109 + 7 取余后返回。 子多重集合 指的是从数组中选出一些元素构成的 无序 集合,每个元素 x 出现的次数可以是 0, 1, ..., occ[x] 次,其中 occ[x] 是元素 x 在数组中的出现次数。 注意: * 如果两个子多重集合中的元素排序后一模一样,那么它们两个是相同的 子多重集合 。 * 空 集合的和是 0 。 示例 1: 输入:nums = [1,2,2,3], l = 6, r = 6 输出:1...
my_solution = Solution() test_input = { "nums": [1,2,2,3], "l": 6, "r": 6 } assert my_solution.countSubMultisets(**test_input) == 1 test_input = { "nums": [2,1,4,2,7], "l": 1, "r": 5 } assert my_solution.countSubMultisets(**test_input) == 7 test_input = { "nums": [1,2,1,3,5,2], "l": 3, "r": 5 } assert my_solution.c...
1,697,293,800
weekly-contest-366-divisible-and-non-divisible-sums-difference
https://leetcode.com/problems/divisible-and-non-divisible-sums-difference
divisible-and-non-divisible-sums-difference
{ "questionId": "3172", "questionFrontendId": "2894", "title": "Divisible and Non-divisible Sums Difference", "titleSlug": "divisible-and-non-divisible-sums-difference", "isPaidOnly": false, "difficulty": "Easy", "likes": 115, "dislikes": 10, "categoryTitle": "Algorithms" }
""" 给你两个正整数 n 和 m 。 现定义两个整数 num1 和 num2 ,如下所示: * num1:范围 [1, n] 内所有 无法被 m 整除 的整数之和。 * num2:范围 [1, n] 内所有 能够被 m 整除 的整数之和。 返回整数 num1 - num2 。 示例 1: 输入:n = 10, m = 3 输出:19 解释:在这个示例中: - 范围 [1, 10] 内无法被 3 整除的整数为 [1,2,4,5,7,8,10] ,num1 = 这些整数之和 = 37 。 - 范围 [1, 10] 内能够被 3 整除的整数为 [3,6,9] ,num2 = 这些整数之和 = 18 。 返回 37 - 18...
给你两个正整数 n 和 m 。 现定义两个整数 num1 和 num2 ,如下所示: * num1:范围 [1, n] 内所有 无法被 m 整除 的整数之和。 * num2:范围 [1, n] 内所有 能够被 m 整除 的整数之和。 返回整数 num1 - num2 。 示例 1: 输入:n = 10, m = 3 输出:19 解释:在这个示例中: - 范围 [1, 10] 内无法被 3 整除的整数为 [1,2,4,5,7,8,10] ,num1 = 这些整数之和 = 37 。 - 范围 [1, 10] 内能够被 3 整除的整数为 [3,6,9] ,num2 = 这些整数之和 = 18 。 返回 37 - 18 = 1...
my_solution = Solution() test_input = { "n": 10, "m": 3 } assert my_solution.differenceOfSums(**test_input) == 19 test_input = { "n": 5, "m": 6 } assert my_solution.differenceOfSums(**test_input) == 15 test_input = { "n": 5, "m": 1 } assert my_solution.differenceOfSums(**test_input) == -15 test_input = { "n": 15, ...
1,696,732,200
weekly-contest-366-minimum-processing-time
https://leetcode.com/problems/minimum-processing-time
minimum-processing-time
{ "questionId": "3151", "questionFrontendId": "2895", "title": "Minimum Processing Time", "titleSlug": "minimum-processing-time", "isPaidOnly": false, "difficulty": "Medium", "likes": 119, "dislikes": 22, "categoryTitle": "Algorithms" }
""" 你有 n 颗处理器,每颗处理器都有 4 个核心。现有 n * 4 个待执行任务,每个核心只执行 一个 任务。 给你一个下标从 0 开始的整数数组 processorTime ,表示每颗处理器最早空闲时间。另给你一个下标从 0 开始的整数数组 tasks ,表示执行每个任务所需的时间。返回所有任务都执行完毕需要的 最小时间 。 注意:每个核心独立执行任务。 示例 1: 输入:processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5] 输出:16 解释: 最优的方案是将下标为 4, 5, 6, 7 的任务分配给第一颗处理器(最早空闲时间 time = 8),下标为 0, 1, 2,...
你有 n 颗处理器,每颗处理器都有 4 个核心。现有 n * 4 个待执行任务,每个核心只执行 一个 任务。 给你一个下标从 0 开始的整数数组 processorTime ,表示每颗处理器最早空闲时间。另给你一个下标从 0 开始的整数数组 tasks ,表示执行每个任务所需的时间。返回所有任务都执行完毕需要的 最小时间 。 注意:每个核心独立执行任务。 示例 1: 输入:processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5] 输出:16 解释: 最优的方案是将下标为 4, 5, 6, 7 的任务分配给第一颗处理器(最早空闲时间 time = 8),下标为 0, 1, 2, 3 的...
my_solution = Solution() test_input = { "processorTime": [8,10], "tasks": [2,2,3,1,8,7,4,5] } assert my_solution.minProcessingTime(**test_input) == 16 test_input = { "processorTime": [10,20], "tasks": [2,3,1,2,5,8,4,3] } assert my_solution.minProcessingTime(**test_input) == 23 test_input = { "processorTime": [121,9...
1,696,732,200
weekly-contest-366-apply-operations-to-make-two-strings-equal
https://leetcode.com/problems/apply-operations-to-make-two-strings-equal
apply-operations-to-make-two-strings-equal
{ "questionId": "3033", "questionFrontendId": "2896", "title": "Apply Operations to Make Two Strings Equal", "titleSlug": "apply-operations-to-make-two-strings-equal", "isPaidOnly": false, "difficulty": "Medium", "likes": 291, "dislikes": 62, "categoryTitle": "Algorithms" }
""" 给你两个下标从 0 开始的二进制字符串 s1 和 s2 ,两个字符串的长度都是 n ,再给你一个正整数 x 。 你可以对字符串 s1 执行以下操作 任意次 : * 选择两个下标 i 和 j ,将 s1[i] 和 s1[j] 都反转,操作的代价为 x 。 * 选择满足 i < n - 1 的下标 i ,反转 s1[i] 和 s1[i + 1] ,操作的代价为 1 。 请你返回使字符串 s1 和 s2 相等的 最小 操作代价之和,如果无法让二者相等,返回 -1 。 注意 ,反转字符的意思是将 0 变成 1 ,或者 1 变成 0 。 示例 1: 输入:s1 = "1100011000", s2 = "0101001...
给你两个下标从 0 开始的二进制字符串 s1 和 s2 ,两个字符串的长度都是 n ,再给你一个正整数 x 。 你可以对字符串 s1 执行以下操作 任意次 : * 选择两个下标 i 和 j ,将 s1[i] 和 s1[j] 都反转,操作的代价为 x 。 * 选择满足 i < n - 1 的下标 i ,反转 s1[i] 和 s1[i + 1] ,操作的代价为 1 。 请你返回使字符串 s1 和 s2 相等的 最小 操作代价之和,如果无法让二者相等,返回 -1 。 注意 ,反转字符的意思是将 0 变成 1 ,或者 1 变成 0 。 示例 1: 输入:s1 = "1100011000", s2 = "0101001010"...
my_solution = Solution() test_input = { "s1": "1100011000", "s2": "0101001010", "x": 2 } assert my_solution.minOperations(**test_input) == 4 test_input = { "s1": "10110", "s2": "00011", "x": 4 } assert my_solution.minOperations(**test_input) == -1 test_input = { "s1": "101101", "s2": "000000", "x": 6 } assert my_so...
1,696,732,200
weekly-contest-366-apply-operations-on-array-to-maximize-sum-of-squares
https://leetcode.com/problems/apply-operations-on-array-to-maximize-sum-of-squares
apply-operations-on-array-to-maximize-sum-of-squares
{ "questionId": "3153", "questionFrontendId": "2897", "title": "Apply Operations on Array to Maximize Sum of Squares", "titleSlug": "apply-operations-on-array-to-maximize-sum-of-squares", "isPaidOnly": false, "difficulty": "Hard", "likes": 159, "dislikes": 3, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 和一个 正 整数 k 。 你可以对数组执行以下操作 任意次 : * 选择两个互不相同的下标 i 和 j ,同时 将 nums[i] 更新为 (nums[i] AND nums[j]) 且将 nums[j] 更新为 (nums[i] OR nums[j]) ,OR 表示按位 或 运算,AND 表示按位 与 运算。 你需要从最终的数组里选择 k 个元素,并计算它们的 平方 之和。 请你返回你可以得到的 最大 平方和。 由于答案可能会很大,将答案对 109 + 7 取余 后返回。 示例 1: 输入:nums = [2,6,5,8], k = 2 输出:261 解释:我们...
给你一个下标从 0 开始的整数数组 nums 和一个 正 整数 k 。 你可以对数组执行以下操作 任意次 : * 选择两个互不相同的下标 i 和 j ,同时 将 nums[i] 更新为 (nums[i] AND nums[j]) 且将 nums[j] 更新为 (nums[i] OR nums[j]) ,OR 表示按位 或 运算,AND 表示按位 与 运算。 你需要从最终的数组里选择 k 个元素,并计算它们的 平方 之和。 请你返回你可以得到的 最大 平方和。 由于答案可能会很大,将答案对 109 + 7 取余 后返回。 示例 1: 输入:nums = [2,6,5,8], k = 2 输出:261 解释:我们可以对数...
my_solution = Solution() test_input = { "nums": [2,6,5,8], "k": 2 } assert my_solution.maxSum(**test_input) == 261 test_input = { "nums": [4,5,4,7], "k": 3 } assert my_solution.maxSum(**test_input) == 90 test_input = { "nums": [32,85,61], "k": 1 } assert my_solution.maxSum(**test_input) == 15625 test_input = { "nu...
1,696,732,200
weekly-contest-365-maximum-value-of-an-ordered-triplet-i
https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i
maximum-value-of-an-ordered-triplet-i
{ "questionId": "3154", "questionFrontendId": "2873", "title": "Maximum Value of an Ordered Triplet I", "titleSlug": "maximum-value-of-an-ordered-triplet-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 130, "dislikes": 10, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:1...
给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:133 解...
my_solution = Solution() test_input = { "nums": [12,6,1,2,7] } assert my_solution.maximumTripletValue(**test_input) == 77 test_input = { "nums": [1,10,3,4,19] } assert my_solution.maximumTripletValue(**test_input) == 133 test_input = { "nums": [1,2,3] } assert my_solution.maximumTripletValue(**test_input) == 0 tes...
1,696,127,400
weekly-contest-365-maximum-value-of-an-ordered-triplet-ii
https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii
maximum-value-of-an-ordered-triplet-ii
{ "questionId": "3152", "questionFrontendId": "2874", "title": "Maximum Value of an Ordered Triplet II", "titleSlug": "maximum-value-of-an-ordered-triplet-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 238, "dislikes": 8, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:1...
给你一个下标从 0 开始的整数数组 nums 。 请你从所有满足 i < j < k 的下标三元组 (i, j, k) 中,找出并返回下标三元组的最大值。如果所有满足条件的三元组的值都是负数,则返回 0 。 下标三元组 (i, j, k) 的值等于 (nums[i] - nums[j]) * nums[k] 。 示例 1: 输入:nums = [12,6,1,2,7] 输出:77 解释:下标三元组 (0, 2, 4) 的值是 (nums[0] - nums[2]) * nums[4] = 77 。 可以证明不存在值大于 77 的有序下标三元组。 示例 2: 输入:nums = [1,10,3,4,19] 输出:133 解...
my_solution = Solution() test_input = { "nums": [12,6,1,2,7] } assert my_solution.maximumTripletValue(**test_input) == 77 test_input = { "nums": [1,10,3,4,19] } assert my_solution.maximumTripletValue(**test_input) == 133 test_input = { "nums": [1,2,3] } assert my_solution.maximumTripletValue(**test_input) == 0 tes...
1,696,127,400
weekly-contest-365-minimum-size-subarray-in-infinite-array
https://leetcode.com/problems/minimum-size-subarray-in-infinite-array
minimum-size-subarray-in-infinite-array
{ "questionId": "3141", "questionFrontendId": "2875", "title": "Minimum Size Subarray in Infinite Array", "titleSlug": "minimum-size-subarray-in-infinite-array", "isPaidOnly": false, "difficulty": "Medium", "likes": 309, "dislikes": 19, "categoryTitle": "Algorithms" }
""" 给你一个下标从 0 开始的数组 nums 和一个整数 target 。 下标从 0 开始的数组 infinite_nums 是通过无限地将 nums 的元素追加到自己之后生成的。 请你从 infinite_nums 中找出满足 元素和 等于 target 的 最短 子数组,并返回该子数组的长度。如果不存在满足条件的子数组,返回 -1 。 示例 1: 输入:nums = [1,2,3], target = 5 输出:2 解释:在这个例子中 infinite_nums = [1,2,3,1,2,3,1,2,...] 。 区间 [1,2] 内的子数组的元素和等于 target = 5 ,且长度 length = 2 。 可...
给你一个下标从 0 开始的数组 nums 和一个整数 target 。 下标从 0 开始的数组 infinite_nums 是通过无限地将 nums 的元素追加到自己之后生成的。 请你从 infinite_nums 中找出满足 元素和 等于 target 的 最短 子数组,并返回该子数组的长度。如果不存在满足条件的子数组,返回 -1 。 示例 1: 输入:nums = [1,2,3], target = 5 输出:2 解释:在这个例子中 infinite_nums = [1,2,3,1,2,3,1,2,...] 。 区间 [1,2] 内的子数组的元素和等于 target = 5 ,且长度 length = 2 。 可以证明,...
my_solution = Solution() test_input = { "nums": [1,2,3], "target": 5 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [1,1,1,2,3], "target": 4 } assert my_solution.minSizeSubarray(**test_input) == 2 test_input = { "nums": [2,4,6,8], "target": 3 } assert my_solution.minSizeSubarray(**te...
1,696,127,400
weekly-contest-365-count-visited-nodes-in-a-directed-graph
https://leetcode.com/problems/count-visited-nodes-in-a-directed-graph
count-visited-nodes-in-a-directed-graph
{ "questionId": "3140", "questionFrontendId": "2876", "title": "Count Visited Nodes in a Directed Graph", "titleSlug": "count-visited-nodes-in-a-directed-graph", "isPaidOnly": false, "difficulty": "Hard", "likes": 274, "dislikes": 4, "categoryTitle": "Algorithms" }
""" 现有一个有向图,其中包含 n 个节点,节点编号从 0 到 n - 1 。此外,该图还包含了 n 条有向边。 给你一个下标从 0 开始的数组 edges ,其中 edges[i] 表示存在一条从节点 i 到节点 edges[i] 的边。 想象在图上发生以下过程: * 你从节点 x 开始,通过边访问其他节点,直到你在 此过程 中再次访问到之前已经访问过的节点。 返回数组 answer 作为答案,其中 answer[i] 表示如果从节点 i 开始执行该过程,你可以访问到的不同节点数。 示例 1: [https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-...
现有一个有向图,其中包含 n 个节点,节点编号从 0 到 n - 1 。此外,该图还包含了 n 条有向边。 给你一个下标从 0 开始的数组 edges ,其中 edges[i] 表示存在一条从节点 i 到节点 edges[i] 的边。 想象在图上发生以下过程: * 你从节点 x 开始,通过边访问其他节点,直到你在 此过程 中再次访问到之前已经访问过的节点。 返回数组 answer 作为答案,其中 answer[i] 表示如果从节点 i 开始执行该过程,你可以访问到的不同节点数。 示例 1: [https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.pn...
my_solution = Solution() test_input = { "edges": [1,2,0,0] } assert my_solution.countVisitedNodes(**test_input) == [3,3,3,4] test_input = { "edges": [1,2,3,4,0] } assert my_solution.countVisitedNodes(**test_input) == [5,5,5,5,5] test_input = { "edges": [3,6,1,0,5,7,4,3] } assert my_solution.countVisitedNodes(**test...
1,696,127,400
Free AI Image Generator No sign-up. Instant results. Open Now