task_id
stringlengths
33
83
url
stringlengths
44
94
title
stringlengths
14
64
meta
dict
prompt
stringlengths
718
3.82k
prompt_sft
stringlengths
780
3.89k
test
stringlengths
8.25k
44.9k
start_time
int64
1.69B
1.71B
biweekly-contest-114-minimum-operations-to-collect-elements
https://leetcode.com/problems/minimum-operations-to-collect-elements
minimum-operations-to-collect-elements
{ "questionId": "3044", "questionFrontendId": "2869", "title": "Minimum Operations to Collect Elements", "titleSlug": "minimum-operations-to-collect-elements", "isPaidOnly": false, "difficulty": "Easy", "likes": 130, "dislikes": 10, "categoryTitle": "Algorithms" }
""" You are given an array nums of positive integers and an integer k. In one operation, you can remove the last element of the array and add it to your collection. Return the minimum number of operations needed to collect elements 1, 2, ..., k. Example 1: Input: nums = [3,1,5,4,2], k = 2 Output: 4 Explanation: Aft...
You are given an array nums of positive integers and an integer k. In one operation, you can remove the last element of the array and add it to your collection. Return the minimum number of operations needed to collect elements 1, 2, ..., k. Example 1: Input: nums = [3,1,5,4,2], k = 2 Output: 4 Explanation: After 4...
my_solution = Solution() test_input = { "nums": [3,1,5,4,2], "k": 2 } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [3,1,5,4,2], "k": 5 } assert my_solution.minOperations(**test_input) == 5 test_input = { "nums": [3,2,5,3,1], "k": 3 } assert my_solution.minOperations(**test_input) == 4 ...
1,696,084,200
biweekly-contest-114-minimum-number-of-operations-to-make-array-empty
https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty
minimum-number-of-operations-to-make-array-empty
{ "questionId": "3094", "questionFrontendId": "2870", "title": "Minimum Number of Operations to Make Array Empty", "titleSlug": "minimum-number-of-operations-to-make-array-empty", "isPaidOnly": false, "difficulty": "Medium", "likes": 147, "dislikes": 5, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array nums consisting of positive integers. There are two types of operations that you can apply on the array any number of times: * Choose two elements with equal values and delete them from the array. * Choose three elements with equal values and delete them from the array. Return t...
You are given a 0-indexed array nums consisting of positive integers. There are two types of operations that you can apply on the array any number of times: * Choose two elements with equal values and delete them from the array. * Choose three elements with equal values and delete them from the array. Return the m...
my_solution = Solution() test_input = { "nums": [2,3,3,2,2,4,2,3,4] } assert my_solution.minOperations(**test_input) == 4 test_input = { "nums": [2,1,2,2,3,3] } assert my_solution.minOperations(**test_input) == -1 test_input = { "nums": [3,3] } assert my_solution.minOperations(**test_input) == 1 test_input = { "nu...
1,696,084,200
biweekly-contest-114-split-array-into-maximum-number-of-subarrays
https://leetcode.com/problems/split-array-into-maximum-number-of-subarrays
split-array-into-maximum-number-of-subarrays
{ "questionId": "3080", "questionFrontendId": "2871", "title": "Split Array Into Maximum Number of Subarrays", "titleSlug": "split-array-into-maximum-number-of-subarrays", "isPaidOnly": false, "difficulty": "Medium", "likes": 188, "dislikes": 24, "categoryTitle": "Algorithms" }
""" You are given an array nums consisting of non-negative integers. We define the score of subarray nums[l..r] such that l <= r as nums[l] AND nums[l + 1] AND ... AND nums[r] where AND is the bitwise AND operation. Consider splitting the array into one or more subarrays such that the following conditions are satisfi...
You are given an array nums consisting of non-negative integers. We define the score of subarray nums[l..r] such that l <= r as nums[l] AND nums[l + 1] AND ... AND nums[r] where AND is the bitwise AND operation. Consider splitting the array into one or more subarrays such that the following conditions are satisfied: ...
my_solution = Solution() test_input = { "nums": [1,0,2,0,1,2] } assert my_solution.maxSubarrays(**test_input) == 3 test_input = { "nums": [5,7,1,3] } assert my_solution.maxSubarrays(**test_input) == 1 test_input = { "nums": [1,0,2,1] } assert my_solution.maxSubarrays(**test_input) == 2 test_input = { "nums": [0] }...
1,696,084,200
biweekly-contest-114-maximum-number-of-k-divisible-components
https://leetcode.com/problems/maximum-number-of-k-divisible-components
maximum-number-of-k-divisible-components
{ "questionId": "3058", "questionFrontendId": "2872", "title": "Maximum Number of K-Divisible Components", "titleSlug": "maximum-number-of-k-divisible-components", "isPaidOnly": false, "difficulty": "Hard", "likes": 170, "dislikes": 3, "categoryTitle": "Algorithms" }
""" There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given a 0-indexed integer array values of length n, where values[i]...
There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given a 0-indexed integer array values of length n, where values[i] is ...
my_solution = Solution() test_input = { "n": 5, "edges": [[0,2],[1,2],[1,3],[2,4]], "values": [1,8,1,4,4], "k": 6 } assert my_solution.maxKDivisibleComponents(**test_input) == 2 test_input = { "n": 7, "edges": [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], "values": [3,0,6,1,5,2,1], "k": 3 } assert my_solution.maxKDivisible...
1,696,084,200
weekly-contest-364-maximum-odd-binary-number
https://leetcode.com/problems/maximum-odd-binary-number
maximum-odd-binary-number
{ "questionId": "3055", "questionFrontendId": "2864", "title": "Maximum Odd Binary Number", "titleSlug": "maximum-odd-binary-number", "isPaidOnly": false, "difficulty": "Easy", "likes": 149, "dislikes": 1, "categoryTitle": "Algorithms" }
""" You are given a binary string s that contains at least one '1'. You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination. Return a string representing the maximum odd binary number that can be created from the given co...
You are given a binary string s that contains at least one '1'. You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination. Return a string representing the maximum odd binary number that can be created from the given combin...
my_solution = Solution() test_input = { "s": "010" } assert my_solution.maximumOddBinaryNumber(**test_input) == "001" test_input = { "s": "0101" } assert my_solution.maximumOddBinaryNumber(**test_input) == "1001" test_input = { "s": "1" } assert my_solution.maximumOddBinaryNumber(**test_input) == "1" test_input = ...
1,695,522,600
weekly-contest-364-beautiful-towers-i
https://leetcode.com/problems/beautiful-towers-i
beautiful-towers-i
{ "questionId": "3114", "questionFrontendId": "2865", "title": "Beautiful Towers I", "titleSlug": "beautiful-towers-i", "isPaidOnly": false, "difficulty": "Medium", "likes": 160, "dislikes": 26, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights ...
You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights is a...
my_solution = Solution() test_input = { "maxHeights": [5,3,4,1,1] } assert my_solution.maximumSumOfHeights(**test_input) == 13 test_input = { "maxHeights": [6,5,3,9,2,7] } assert my_solution.maximumSumOfHeights(**test_input) == 22 test_input = { "maxHeights": [3,2,5,5,2,3] } assert my_solution.maximumSumOfHeights(*...
1,695,522,600
weekly-contest-364-beautiful-towers-ii
https://leetcode.com/problems/beautiful-towers-ii
beautiful-towers-ii
{ "questionId": "3113", "questionFrontendId": "2866", "title": "Beautiful Towers II", "titleSlug": "beautiful-towers-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 348, "dislikes": 22, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights ...
You are given a 0-indexed array maxHeights of n integers. You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i]. A configuration of towers is beautiful if the following conditions hold: 1. 1 <= heights[i] <= maxHeights[i] 2. heights is a...
my_solution = Solution() test_input = { "maxHeights": [5,3,4,1,1] } assert my_solution.maximumSumOfHeights(**test_input) == 13 test_input = { "maxHeights": [6,5,3,9,2,7] } assert my_solution.maximumSumOfHeights(**test_input) == 22 test_input = { "maxHeights": [3,2,5,5,2,3] } assert my_solution.maximumSumOfHeights(*...
1,695,522,600
weekly-contest-364-count-valid-paths-in-a-tree
https://leetcode.com/problems/count-valid-paths-in-a-tree
count-valid-paths-in-a-tree
{ "questionId": "3112", "questionFrontendId": "2867", "title": "Count Valid Paths in a Tree", "titleSlug": "count-valid-paths-in-a-tree", "isPaidOnly": false, "difficulty": "Hard", "likes": 212, "dislikes": 5, "categoryTitle": "Algorithms" }
""" There is an undirected tree with n nodes labeled from 1 to n. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. Return the number of valid paths in the tree. A path (a, b) is valid if there exist...
There is an undirected tree with n nodes labeled from 1 to n. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. Return the number of valid paths in the tree. A path (a, b) is valid if there exists ex...
my_solution = Solution() test_input = { "n": 5, "edges": [[1,2],[1,3],[2,4],[2,5]] } assert my_solution.countPaths(**test_input) == 4 test_input = { "n": 6, "edges": [[1,2],[1,3],[2,4],[3,5],[3,6]] } assert my_solution.countPaths(**test_input) == 6 test_input = { "n": 1, "edges": [] } assert my_solution.countPaths(...
1,695,522,600
weekly-contest-363-sum-of-values-at-indices-with-k-set-bits
https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits
sum-of-values-at-indices-with-k-set-bits
{ "questionId": "3093", "questionFrontendId": "2859", "title": "Sum of Values at Indices With K Set Bits", "titleSlug": "sum-of-values-at-indices-with-k-set-bits", "isPaidOnly": false, "difficulty": "Easy", "likes": 154, "dislikes": 16, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums and an integer k. Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set bits in their binary representation. The set bits in an integer are the 1's present when it is written in binary. * For example, the binary rep...
You are given a 0-indexed integer array nums and an integer k. Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set bits in their binary representation. The set bits in an integer are the 1's present when it is written in binary. * For example, the binary represe...
my_solution = Solution() test_input = { "nums": [5,10,1,5,2], "k": 1 } assert my_solution.sumIndicesWithKSetBits(**test_input) == 13 test_input = { "nums": [4,3,2,1], "k": 2 } assert my_solution.sumIndicesWithKSetBits(**test_input) == 1 test_input = { "nums": [1], "k": 0 } assert my_solution.sumIndicesWithKSetBits(...
1,694,917,800
weekly-contest-363-happy-students
https://leetcode.com/problems/happy-students
happy-students
{ "questionId": "3104", "questionFrontendId": "2860", "title": "Happy Students", "titleSlug": "happy-students", "isPaidOnly": false, "difficulty": "Medium", "likes": 144, "dislikes": 272, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums of length n where n is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy. The ith student will become happy if one of these two conditions is met: * The student is selected and the to...
You are given a 0-indexed integer array nums of length n where n is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy. The ith student will become happy if one of these two conditions is met: * The student is selected and the total ...
my_solution = Solution() test_input = { "nums": [1,1] } assert my_solution.countWays(**test_input) == 2 test_input = { "nums": [6,0,3,3,6,7,2,7] } assert my_solution.countWays(**test_input) == 3 test_input = { "nums": [1,1,0,1] } assert my_solution.countWays(**test_input) == 1 test_input = { "nums": [5,0,3,4,2,1,2...
1,694,917,800
weekly-contest-363-maximum-number-of-alloys
https://leetcode.com/problems/maximum-number-of-alloys
maximum-number-of-alloys
{ "questionId": "3095", "questionFrontendId": "2861", "title": "Maximum Number of Alloys", "titleSlug": "maximum-number-of-alloys", "isPaidOnly": false, "difficulty": "Medium", "likes": 227, "dislikes": 36, "categoryTitle": "Algorithms" }
""" You are the owner of a company that creates alloys using various types of metals. There are n different types of metals available, and you have access to k machines that can be used to create alloys. Each machine requires a specific amount of each metal type to create an alloy. For the ith machine to create an all...
You are the owner of a company that creates alloys using various types of metals. There are n different types of metals available, and you have access to k machines that can be used to create alloys. Each machine requires a specific amount of each metal type to create an alloy. For the ith machine to create an alloy, ...
my_solution = Solution() test_input = { "n": 3, "k": 2, "budget": 15, "composition": [[1,1,1],[1,1,10]], "stock": [0,0,0], "cost": [1,2,3] } assert my_solution.maxNumberOfAlloys(**test_input) == 2 test_input = { "n": 3, "k": 2, "budget": 15, "composition": [[1,1,1],[1,1,10]], "stock": [0,0,100], "cost": [1,2,3] } as...
1,694,917,800
weekly-contest-363-maximum-element-sum-of-a-complete-subset-of-indices
https://leetcode.com/problems/maximum-element-sum-of-a-complete-subset-of-indices
maximum-element-sum-of-a-complete-subset-of-indices
{ "questionId": "3047", "questionFrontendId": "2862", "title": "Maximum Element-Sum of a Complete Subset of Indices", "titleSlug": "maximum-element-sum-of-a-complete-subset-of-indices", "isPaidOnly": false, "difficulty": "Hard", "likes": 157, "dislikes": 27, "categoryTitle": "Algorithms" }
""" You are given a 1-indexed array nums of n integers. A set of numbers is complete if the product of every pair of its elements is a perfect square. For a subset of the indices set {1, 2, ..., n} represented as {i1, i2, ..., ik}, we define its element-sum as: nums[i1] + nums[i2] + ... + nums[ik]. Return the maximu...
You are given a 1-indexed array nums of n integers. A set of numbers is complete if the product of every pair of its elements is a perfect square. For a subset of the indices set {1, 2, ..., n} represented as {i1, i2, ..., ik}, we define its element-sum as: nums[i1] + nums[i2] + ... + nums[ik]. Return the maximum el...
my_solution = Solution() test_input = { "nums": [8,7,3,5,7,2,4,9] } assert my_solution.maximumSum(**test_input) == 16 test_input = { "nums": [5,10,3,10,1,13,7,9,4] } assert my_solution.maximumSum(**test_input) == 19 test_input = { "nums": [1,1,1,1] } assert my_solution.maximumSum(**test_input) == 2 test_input = { ...
1,694,917,800
biweekly-contest-113-minimum-right-shifts-to-sort-the-array
https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array
minimum-right-shifts-to-sort-the-array
{ "questionId": "3045", "questionFrontendId": "2855", "title": "Minimum Right Shifts to Sort the Array", "titleSlug": "minimum-right-shifts-to-sort-the-array", "isPaidOnly": false, "difficulty": "Easy", "likes": 169, "dislikes": 6, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array nums of length n containing distinct positive integers. Return the minimum number of right shifts required to sort nums and -1 if this is not possible. A right shift is defined as shifting the element at index i to index (i + 1) % n, for all indices. Example 1: Input: nums = [3,4,...
You are given a 0-indexed array nums of length n containing distinct positive integers. Return the minimum number of right shifts required to sort nums and -1 if this is not possible. A right shift is defined as shifting the element at index i to index (i + 1) % n, for all indices. Example 1: Input: nums = [3,4,5,1,...
my_solution = Solution() test_input = { "nums": [3,4,5,1,2] } assert my_solution.minimumRightShifts(**test_input) == 2 test_input = { "nums": [1,3,5] } assert my_solution.minimumRightShifts(**test_input) == 0 test_input = { "nums": [2,1,4] } assert my_solution.minimumRightShifts(**test_input) == -1 test_input = { ...
1,694,874,600
biweekly-contest-113-minimum-array-length-after-pair-removals
https://leetcode.com/problems/minimum-array-length-after-pair-removals
minimum-array-length-after-pair-removals
{ "questionId": "3081", "questionFrontendId": "2856", "title": "Minimum Array Length After Pair Removals", "titleSlug": "minimum-array-length-after-pair-removals", "isPaidOnly": false, "difficulty": "Medium", "likes": 259, "dislikes": 88, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed sorted array of integers nums. You can perform the following operation any number of times: * Choose two indices, i and j, where i < j, such that nums[i] < nums[j]. * Then, remove the elements at indices i and j from nums. The remaining elements retain their original order, and the arr...
You are given a 0-indexed sorted array of integers nums. You can perform the following operation any number of times: * Choose two indices, i and j, where i < j, such that nums[i] < nums[j]. * Then, remove the elements at indices i and j from nums. The remaining elements retain their original order, and the array i...
my_solution = Solution() test_input = { "nums": [1,3,4,9] } assert my_solution.minLengthAfterRemovals(**test_input) == 0 test_input = { "nums": [2,3,6,9] } assert my_solution.minLengthAfterRemovals(**test_input) == 0 test_input = { "nums": [1,1,2] } assert my_solution.minLengthAfterRemovals(**test_input) == 1 test...
1,694,874,600
biweekly-contest-113-count-pairs-of-points-with-distance-k
https://leetcode.com/problems/count-pairs-of-points-with-distance-k
count-pairs-of-points-with-distance-k
{ "questionId": "2953", "questionFrontendId": "2857", "title": "Count Pairs of Points With Distance k", "titleSlug": "count-pairs-of-points-with-distance-k", "isPaidOnly": false, "difficulty": "Medium", "likes": 238, "dislikes": 34, "categoryTitle": "Algorithms" }
""" You are given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the ith point in a 2D plane. We define the distance between two points (x1, y1) and (x2, y2) as (x1 XOR x2) + (y1 XOR y2) where XOR is the bitwise XOR operation. Return the number of pairs (i, j) ...
You are given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the ith point in a 2D plane. We define the distance between two points (x1, y1) and (x2, y2) as (x1 XOR x2) + (y1 XOR y2) where XOR is the bitwise XOR operation. Return the number of pairs (i, j) such...
my_solution = Solution() test_input = { "coordinates": [[1,2],[4,2],[1,3],[5,2]], "k": 5 } assert my_solution.countPairs(**test_input) == 2 test_input = { "coordinates": [[1,3],[1,3],[1,3],[1,3],[1,3]], "k": 0 } assert my_solution.countPairs(**test_input) == 10 test_input = { "coordinates": [[27,94],[61,68],[47,0],...
1,694,874,600
biweekly-contest-113-minimum-edge-reversals-so-every-node-is-reachable
https://leetcode.com/problems/minimum-edge-reversals-so-every-node-is-reachable
minimum-edge-reversals-so-every-node-is-reachable
{ "questionId": "3105", "questionFrontendId": "2858", "title": "Minimum Edge Reversals So Every Node Is Reachable", "titleSlug": "minimum-edge-reversals-so-every-node-is-reachable", "isPaidOnly": false, "difficulty": "Hard", "likes": 227, "dislikes": 3, "categoryTitle": "Algorithms" }
""" There is a simple directed graph with n nodes labeled from 0 to n - 1. The graph would form a tree if its edges were bi-directional. You are given an integer n and a 2D integer array edges, where edges[i] = [ui, vi] represents a directed edge going from node ui to node vi. An edge reversal changes the direction o...
There is a simple directed graph with n nodes labeled from 0 to n - 1. The graph would form a tree if its edges were bi-directional. You are given an integer n and a 2D integer array edges, where edges[i] = [ui, vi] represents a directed edge going from node ui to node vi. An edge reversal changes the direction of an...
my_solution = Solution() test_input = { "n": 4, "edges": [[2,0],[2,1],[1,3]] } assert my_solution.minEdgeReversals(**test_input) == [1,1,0,2] test_input = { "n": 3, "edges": [[1,2],[2,0]] } assert my_solution.minEdgeReversals(**test_input) == [2,0,1] test_input = { "n": 2, "edges": [[0,1]] } assert my_solution.minE...
1,694,874,600
weekly-contest-362-points-that-intersect-with-cars
https://leetcode.com/problems/points-that-intersect-with-cars
points-that-intersect-with-cars
{ "questionId": "3034", "questionFrontendId": "2848", "title": "Points That Intersect With Cars", "titleSlug": "points-that-intersect-with-cars", "isPaidOnly": false, "difficulty": "Easy", "likes": 211, "dislikes": 13, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed 2D integer array nums representing the coordinates of the cars parking on a number line. For any index i, nums[i] = [starti, endi] where starti is the starting point of the ith car and endi is the ending point of the ith car. Return the number of integer points on the line that are covere...
You are given a 0-indexed 2D integer array nums representing the coordinates of the cars parking on a number line. For any index i, nums[i] = [starti, endi] where starti is the starting point of the ith car and endi is the ending point of the ith car. Return the number of integer points on the line that are covered wi...
my_solution = Solution() test_input = { "nums": [[3,6],[1,5],[4,7]] } assert my_solution.numberOfPoints(**test_input) == 7 test_input = { "nums": [[1,3],[5,8]] } assert my_solution.numberOfPoints(**test_input) == 7 test_input = { "nums": [[4,4],[9,10],[9,10],[3,8]] } assert my_solution.numberOfPoints(**test_input) ...
1,694,313,000
weekly-contest-362-determine-if-a-cell-is-reachable-at-a-given-time
https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time
determine-if-a-cell-is-reachable-at-a-given-time
{ "questionId": "3056", "questionFrontendId": "2849", "title": "Determine if a Cell Is Reachable at a Given Time", "titleSlug": "determine-if-a-cell-is-reachable-at-a-given-time", "isPaidOnly": false, "difficulty": "Medium", "likes": 782, "dislikes": 730, "categoryTitle": "Algorithms" }
""" You are given four integers sx, sy, fx, fy, and a non-negative integer t. In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells. Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise. A cell's adjacent cells are the 8 cell...
You are given four integers sx, sy, fx, fy, and a non-negative integer t. In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells. Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise. A cell's adjacent cells are the 8 cells ar...
my_solution = Solution() test_input = { "sx": 3, "sy": 1, "fx": 7, "fy": 3, "t": 3 } assert my_solution.isReachableAtTime(**test_input) == False test_input = { "sx": 2, "sy": 4, "fx": 7, "fy": 7, "t": 6 } assert my_solution.isReachableAtTime(**test_input) == True test_input = { "sx": 1, "sy": 1, "fx": 1, "fy": 2, "...
1,694,313,000
weekly-contest-362-minimum-moves-to-spread-stones-over-grid
https://leetcode.com/problems/minimum-moves-to-spread-stones-over-grid
minimum-moves-to-spread-stones-over-grid
{ "questionId": "3092", "questionFrontendId": "2850", "title": "Minimum Moves to Spread Stones Over Grid", "titleSlug": "minimum-moves-to-spread-stones-over-grid", "isPaidOnly": false, "difficulty": "Medium", "likes": 419, "dislikes": 43, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed 2D integer matrix grid of size 3 * 3, representing the number of stones in each cell. The grid contains exactly 9 stones, and there can be multiple stones in a single cell. In one move, you can move a single stone from its current cell to any other cell if the two cells share a side. Ret...
You are given a 0-indexed 2D integer matrix grid of size 3 * 3, representing the number of stones in each cell. The grid contains exactly 9 stones, and there can be multiple stones in a single cell. In one move, you can move a single stone from its current cell to any other cell if the two cells share a side. Return ...
my_solution = Solution() test_input = { "grid": [[1,1,0],[1,1,1],[1,2,1]] } assert my_solution.minimumMoves(**test_input) == 3 test_input = { "grid": [[1,3,0],[1,0,0],[1,0,3]] } assert my_solution.minimumMoves(**test_input) == 4 test_input = { "grid": [[1,2,2],[1,1,0],[0,1,1]] } assert my_solution.minimumMoves(**te...
1,694,313,000
weekly-contest-362-string-transformation
https://leetcode.com/problems/string-transformation
string-transformation
{ "questionId": "3024", "questionFrontendId": "2851", "title": "String Transformation", "titleSlug": "string-transformation", "isPaidOnly": false, "difficulty": "Hard", "likes": 140, "dislikes": 19, "categoryTitle": "Algorithms" }
""" You are given two strings s and t of equal length n. You can perform the following operation on the string s: * Remove a suffix of s of length l where 0 < l < n and append it at the start of s. For example, let s = 'abcd' then in one operation you can remove the suffix 'cd' and append it in front of s making s...
You are given two strings s and t of equal length n. You can perform the following operation on the string s: * Remove a suffix of s of length l where 0 < l < n and append it at the start of s. For example, let s = 'abcd' then in one operation you can remove the suffix 'cd' and append it in front of s making s = '...
my_solution = Solution() test_input = { "s": "abcd", "t": "cdab", "k": 2 } assert my_solution.numberOfWays(**test_input) == 2 test_input = { "s": "ababab", "t": "ababab", "k": 1 } assert my_solution.numberOfWays(**test_input) == 2 test_input = { "s": "goxoq", "t": "dfqgl", "k": 244326024901249 } assert my_solution....
1,694,313,000
weekly-contest-361-count-symmetric-integers
https://leetcode.com/problems/count-symmetric-integers
count-symmetric-integers
{ "questionId": "2998", "questionFrontendId": "2843", "title": " Count Symmetric Integers", "titleSlug": "count-symmetric-integers", "isPaidOnly": false, "difficulty": "Easy", "likes": 210, "dislikes": 9, "categoryTitle": "Algorithms" }
""" You are given two positive integers low and high. An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric. Return the number of symmetric integers in the range [low, high]. E...
You are given two positive integers low and high. An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric. Return the number of symmetric integers in the range [low, high]. Examp...
my_solution = Solution() test_input = { "low": 1, "high": 100 } assert my_solution.countSymmetricIntegers(**test_input) == 9 test_input = { "low": 1200, "high": 1230 } assert my_solution.countSymmetricIntegers(**test_input) == 4 test_input = { "low": 1, "high": 1 } assert my_solution.countSymmetricIntegers(**test_i...
1,693,708,200
weekly-contest-361-minimum-operations-to-make-a-special-number
https://leetcode.com/problems/minimum-operations-to-make-a-special-number
minimum-operations-to-make-a-special-number
{ "questionId": "3046", "questionFrontendId": "2844", "title": "Minimum Operations to Make a Special Number", "titleSlug": "minimum-operations-to-make-a-special-number", "isPaidOnly": false, "difficulty": "Medium", "likes": 317, "dislikes": 48, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed string num representing a non-negative integer. In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0. Return the minimum number of operations required to make num special. An integer x is considered special if it is ...
You are given a 0-indexed string num representing a non-negative integer. In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0. Return the minimum number of operations required to make num special. An integer x is considered special if it is divi...
my_solution = Solution() test_input = { "num": "2245047" } assert my_solution.minimumOperations(**test_input) == 2 test_input = { "num": "2908305" } assert my_solution.minimumOperations(**test_input) == 3 test_input = { "num": "10" } assert my_solution.minimumOperations(**test_input) == 1 test_input = { "num": "1"...
1,693,708,200
weekly-contest-361-count-of-interesting-subarrays
https://leetcode.com/problems/count-of-interesting-subarrays
count-of-interesting-subarrays
{ "questionId": "2915", "questionFrontendId": "2845", "title": "Count of Interesting Subarrays", "titleSlug": "count-of-interesting-subarrays", "isPaidOnly": false, "difficulty": "Medium", "likes": 449, "dislikes": 62, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums, an integer modulo, and an integer k. Your task is to find the count of subarrays that are interesting. A subarray nums[l..r] is interesting if the following condition holds: * Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Th...
You are given a 0-indexed integer array nums, an integer modulo, and an integer k. Your task is to find the count of subarrays that are interesting. A subarray nums[l..r] is interesting if the following condition holds: * Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, ...
my_solution = Solution() test_input = { "nums": [3,2,4], "modulo": 2, "k": 1 } assert my_solution.countInterestingSubarrays(**test_input) == 3 test_input = { "nums": [3,1,9,6], "modulo": 3, "k": 0 } assert my_solution.countInterestingSubarrays(**test_input) == 2 test_input = { "nums": [11,12,21,31], "modulo": 10, "...
1,693,708,200
weekly-contest-361-minimum-edge-weight-equilibrium-queries-in-a-tree
https://leetcode.com/problems/minimum-edge-weight-equilibrium-queries-in-a-tree
minimum-edge-weight-equilibrium-queries-in-a-tree
{ "questionId": "3079", "questionFrontendId": "2846", "title": "Minimum Edge Weight Equilibrium Queries in a Tree", "titleSlug": "minimum-edge-weight-equilibrium-queries-in-a-tree", "isPaidOnly": false, "difficulty": "Hard", "likes": 260, "dislikes": 4, "categoryTitle": "Algorithms" }
""" There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree. You are also given a 2D integer array queries of length m, wh...
There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree. You are also given a 2D integer array queries of length m, where ...
my_solution = Solution() test_input = { "n": 7, "edges": [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], "queries": [[0,3],[3,6],[2,6],[0,6]] } assert my_solution.minOperationsQueries(**test_input) == [0,0,1,3] test_input = { "n": 8, "edges": [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], "queries": [...
1,693,708,200
biweekly-contest-112-check-if-strings-can-be-made-equal-with-operations-i
https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i
check-if-strings-can-be-made-equal-with-operations-i
{ "questionId": "2999", "questionFrontendId": "2839", "title": "Check if Strings Can be Made Equal With Operations I", "titleSlug": "check-if-strings-can-be-made-equal-with-operations-i", "isPaidOnly": false, "difficulty": "Easy", "likes": 164, "dislikes": 20, "categoryTitle": "Algorithms" }
""" You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that j - i = 2, then swap the two characters at those indices in the string. Return true if y...
You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that j - i = 2, then swap the two characters at those indices in the string. Return true if you c...
my_solution = Solution() test_input = { "s1": "abcd", "s2": "cdab" } assert my_solution.canBeEqual(**test_input) == True test_input = { "s1": "abcd", "s2": "dacb" } assert my_solution.canBeEqual(**test_input) == False test_input = { "s1": "gudo", "s2": "ogdu" } assert my_solution.canBeEqual(**test_input) == False ...
1,693,665,000
biweekly-contest-112-check-if-strings-can-be-made-equal-with-operations-ii
https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-ii
check-if-strings-can-be-made-equal-with-operations-ii
{ "questionId": "2978", "questionFrontendId": "2840", "title": "Check if Strings Can be Made Equal With Operations II", "titleSlug": "check-if-strings-can-be-made-equal-with-operations-ii", "isPaidOnly": false, "difficulty": "Medium", "likes": 231, "dislikes": 2, "categoryTitle": "Algorithms" }
""" You are given two strings s1 and s2, both of length n, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in ...
You are given two strings s1 and s2, both of length n, consisting of lowercase English letters. You can apply the following operation on any of the two strings any number of times: * Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in the ...
my_solution = Solution() test_input = { "s1": "abe", "s2": "bea" } assert my_solution.checkStrings(**test_input) == False test_input = { "s1": "abcdba", "s2": "cabdab" } assert my_solution.checkStrings(**test_input) == True test_input = { "s1": "ublnlasppynwgx", "s2": "ganplbuylnswpx" } assert my_solution.checkStri...
1,693,665,000
biweekly-contest-112-maximum-sum-of-almost-unique-subarray
https://leetcode.com/problems/maximum-sum-of-almost-unique-subarray
maximum-sum-of-almost-unique-subarray
{ "questionId": "2954", "questionFrontendId": "2841", "title": "Maximum Sum of Almost Unique Subarray", "titleSlug": "maximum-sum-of-almost-unique-subarray", "isPaidOnly": false, "difficulty": "Medium", "likes": 230, "dislikes": 133, "categoryTitle": "Algorithms" }
""" You are given an integer array nums and two positive integers m and k. Return the maximum sum out of all almost unique subarrays of length k of nums. If no such subarray exists, return 0. A subarray of nums is almost unique if it contains at least m distinct elements. A subarray is a contiguous non-empty sequenc...
You are given an integer array nums and two positive integers m and k. Return the maximum sum out of all almost unique subarrays of length k of nums. If no such subarray exists, return 0. A subarray of nums is almost unique if it contains at least m distinct elements. A subarray is a contiguous non-empty sequence of...
my_solution = Solution() test_input = { "nums": [2,6,7,3,1,7], "m": 3, "k": 4 } assert my_solution.maxSum(**test_input) == 18 test_input = { "nums": [5,9,9,2,4,5,4], "m": 1, "k": 3 } assert my_solution.maxSum(**test_input) == 23 test_input = { "nums": [1,2,1,2,1,2,1], "m": 3, "k": 3 } assert my_solution.maxSum(**te...
1,693,665,000
biweekly-contest-112-count-k-subsequences-of-a-string-with-maximum-beauty
https://leetcode.com/problems/count-k-subsequences-of-a-string-with-maximum-beauty
count-k-subsequences-of-a-string-with-maximum-beauty
{ "questionId": "3057", "questionFrontendId": "2842", "title": "Count K-Subsequences of a String With Maximum Beauty", "titleSlug": "count-k-subsequences-of-a-string-with-maximum-beauty", "isPaidOnly": false, "difficulty": "Hard", "likes": 286, "dislikes": 21, "categoryTitle": "Algorithms" }
""" You are given a string s and an integer k. A k-subsequence is a subsequence of s, having length k, and all its characters are unique, i.e., every character occurs once. Let f(c) denote the number of times the character c occurs in s. The beauty of a k-subsequence is the sum of f(c) for every character c in the k...
You are given a string s and an integer k. A k-subsequence is a subsequence of s, having length k, and all its characters are unique, i.e., every character occurs once. Let f(c) denote the number of times the character c occurs in s. The beauty of a k-subsequence is the sum of f(c) for every character c in the k-sub...
my_solution = Solution() test_input = { "s": "bcca", "k": 2 } assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 4 test_input = { "s": "abbcd", "k": 4 } assert my_solution.countKSubsequencesWithMaxBeauty(**test_input) == 2 test_input = { "s": "am", "k": 2 } assert my_solution.countKSubsequencesWith...
1,693,665,000
weekly-contest-360-furthest-point-from-origin
https://leetcode.com/problems/furthest-point-from-origin
furthest-point-from-origin
{ "questionId": "3019", "questionFrontendId": "2833", "title": "Furthest Point From Origin", "titleSlug": "furthest-point-from-origin", "isPaidOnly": false, "difficulty": "Easy", "likes": 203, "dislikes": 29, "categoryTitle": "Algorithms" }
""" You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0. In the ith move, you can choose one of the following directions: * move to the left if moves[i] = 'L' or moves[i] = '_' * move to the right i...
You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0. In the ith move, you can choose one of the following directions: * move to the left if moves[i] = 'L' or moves[i] = '_' * move to the right if mo...
my_solution = Solution() test_input = { "moves": "L_RL__R" } assert my_solution.furthestDistanceFromOrigin(**test_input) == 3 test_input = { "moves": "_R__LL_" } assert my_solution.furthestDistanceFromOrigin(**test_input) == 5 test_input = { "moves": "_______" } assert my_solution.furthestDistanceFromOrigin(**test_...
1,693,103,400
weekly-contest-360-find-the-minimum-possible-sum-of-a-beautiful-array
https://leetcode.com/problems/find-the-minimum-possible-sum-of-a-beautiful-array
find-the-minimum-possible-sum-of-a-beautiful-array
{ "questionId": "3026", "questionFrontendId": "2834", "title": "Find the Minimum Possible Sum of a Beautiful Array", "titleSlug": "find-the-minimum-possible-sum-of-a-beautiful-array", "isPaidOnly": false, "difficulty": "Medium", "likes": 276, "dislikes": 37, "categoryTitle": "Algorithms" }
""" You are given positive integers n and target. An array nums is beautiful if it meets the following conditions: * nums.length == n. * nums consists of pairwise distinct positive integers. * There doesn't exist two distinct indices, i and j, in the range [0, n - 1], such that nums[i] + nums[j] == target. Return...
You are given positive integers n and target. An array nums is beautiful if it meets the following conditions: * nums.length == n. * nums consists of pairwise distinct positive integers. * There doesn't exist two distinct indices, i and j, in the range [0, n - 1], such that nums[i] + nums[j] == target. Return the...
my_solution = Solution() test_input = { "n": 2, "target": 3 } assert my_solution.minimumPossibleSum(**test_input) == 4 test_input = { "n": 3, "target": 3 } assert my_solution.minimumPossibleSum(**test_input) == 8 test_input = { "n": 1, "target": 1 } assert my_solution.minimumPossibleSum(**test_input) == 1 test_inp...
1,693,103,400
weekly-contest-360-minimum-operations-to-form-subsequence-with-target-sum
https://leetcode.com/problems/minimum-operations-to-form-subsequence-with-target-sum
minimum-operations-to-form-subsequence-with-target-sum
{ "questionId": "3025", "questionFrontendId": "2835", "title": "Minimum Operations to Form Subsequence With Target Sum", "titleSlug": "minimum-operations-to-form-subsequence-with-target-sum", "isPaidOnly": false, "difficulty": "Hard", "likes": 503, "dislikes": 125, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array nums consisting of non-negative powers of 2, and an integer target. In one operation, you must apply the following changes to the array: * Choose any element of the array nums[i] such that nums[i] > 1. * Remove nums[i] from the array. * Add two occurrences of nums[i] / 2 to the ...
You are given a 0-indexed array nums consisting of non-negative powers of 2, and an integer target. In one operation, you must apply the following changes to the array: * Choose any element of the array nums[i] such that nums[i] > 1. * Remove nums[i] from the array. * Add two occurrences of nums[i] / 2 to the end ...
my_solution = Solution() test_input = { "nums": [1,2,8], "target": 7 } assert my_solution.minOperations(**test_input) == 1 test_input = { "nums": [1,32,1,2], "target": 12 } assert my_solution.minOperations(**test_input) == 2 test_input = { "nums": [1,32,1], "target": 35 } assert my_solution.minOperations(**test_inp...
1,693,103,400
weekly-contest-360-maximize-value-of-function-in-a-ball-passing-game
https://leetcode.com/problems/maximize-value-of-function-in-a-ball-passing-game
maximize-value-of-function-in-a-ball-passing-game
{ "questionId": "3032", "questionFrontendId": "2836", "title": "Maximize Value of Function in a Ball Passing Game", "titleSlug": "maximize-value-of-function-in-a-ball-passing-game", "isPaidOnly": false, "difficulty": "Hard", "likes": 202, "dislikes": 85, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array receiver of length n and an integer k. There are n players having a unique id in the range [0, n - 1] who will play a ball passing game, and receiver[i] is the id of the player who receives passes from the player with id i. Players can pass to themselves, i.e. receiver[i] ma...
You are given a 0-indexed integer array receiver of length n and an integer k. There are n players having a unique id in the range [0, n - 1] who will play a ball passing game, and receiver[i] is the id of the player who receives passes from the player with id i. Players can pass to themselves, i.e. receiver[i] may be...
my_solution = Solution() test_input = { "receiver": [2,0,1], "k": 4 } assert my_solution.getMaxFunctionValue(**test_input) == 6 test_input = { "receiver": [1,1,1,2,3], "k": 3 } assert my_solution.getMaxFunctionValue(**test_input) == 10 test_input = { "receiver": [0], "k": 1 } assert my_solution.getMaxFunctionValue(...
1,693,103,400
weekly-contest-359-check-if-a-string-is-an-acronym-of-words
https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words
check-if-a-string-is-an-acronym-of-words
{ "questionId": "2977", "questionFrontendId": "2828", "title": "Check if a String Is an Acronym of Words", "titleSlug": "check-if-a-string-is-an-acronym-of-words", "isPaidOnly": false, "difficulty": "Easy", "likes": 257, "dislikes": 7, "categoryTitle": "Algorithms" }
""" Given an array of strings words and a string s, determine if s is an acronym of words. The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["be...
Given an array of strings words and a string s, determine if s is an acronym of words. The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["bear",...
my_solution = Solution() test_input = { "words": ["an","apple"], "s": "a" } assert my_solution.isAcronym(**test_input) == False test_input = { "words": ["alice","bob","charlie"], "s": "abc" } assert my_solution.isAcronym(**test_input) == True test_input = { "words": ["never","gonna","give","up","on","you"], "s": "n...
1,692,498,600
weekly-contest-359-determine-the-minimum-sum-of-a-k-avoiding-array
https://leetcode.com/problems/determine-the-minimum-sum-of-a-k-avoiding-array
determine-the-minimum-sum-of-a-k-avoiding-array
{ "questionId": "2811", "questionFrontendId": "2829", "title": "Determine the Minimum Sum of a k-avoiding Array", "titleSlug": "determine-the-minimum-sum-of-a-k-avoiding-array", "isPaidOnly": false, "difficulty": "Medium", "likes": 305, "dislikes": 8, "categoryTitle": "Algorithms" }
""" You are given two integers, n and k. An array of distinct positive integers is called a k-avoiding array if there does not exist any pair of distinct elements that sum to k. Return the minimum possible sum of a k-avoiding array of length n. Example 1: Input: n = 5, k = 4 Output: 18 Explanation: Consider the k-a...
You are given two integers, n and k. An array of distinct positive integers is called a k-avoiding array if there does not exist any pair of distinct elements that sum to k. Return the minimum possible sum of a k-avoiding array of length n. Example 1: Input: n = 5, k = 4 Output: 18 Explanation: Consider the k-avoid...
my_solution = Solution() test_input = { "n": 5, "k": 4 } assert my_solution.minimumSum(**test_input) == 18 test_input = { "n": 2, "k": 6 } assert my_solution.minimumSum(**test_input) == 3 test_input = { "n": 1, "k": 1 } assert my_solution.minimumSum(**test_input) == 1 test_input = { "n": 1, "k": 2 } assert my_solu...
1,692,498,600
weekly-contest-359-maximize-the-profit-as-the-salesman
https://leetcode.com/problems/maximize-the-profit-as-the-salesman
maximize-the-profit-as-the-salesman
{ "questionId": "2979", "questionFrontendId": "2830", "title": "Maximize the Profit as the Salesman", "titleSlug": "maximize-the-profit-as-the-salesman", "isPaidOnly": false, "difficulty": "Medium", "likes": 603, "dislikes": 19, "categoryTitle": "Algorithms" }
""" You are given an integer n representing the number of houses on a number line, numbered from 0 to n - 1. Additionally, you are given a 2D integer array offers where offers[i] = [starti, endi, goldi], indicating that ith buyer wants to buy all the houses from starti to endi for goldi amount of gold. As a salesman,...
You are given an integer n representing the number of houses on a number line, numbered from 0 to n - 1. Additionally, you are given a 2D integer array offers where offers[i] = [starti, endi, goldi], indicating that ith buyer wants to buy all the houses from starti to endi for goldi amount of gold. As a salesman, you...
my_solution = Solution() test_input = { "n": 5, "offers": [[0,0,1],[0,2,2],[1,3,2]] } assert my_solution.maximizeTheProfit(**test_input) == 3 test_input = { "n": 5, "offers": [[0,0,1],[0,2,10],[1,3,2]] } assert my_solution.maximizeTheProfit(**test_input) == 10 test_input = { "n": 4, "offers": [[1,3,10],[1,3,3],[0,0...
1,692,498,600
weekly-contest-359-find-the-longest-equal-subarray
https://leetcode.com/problems/find-the-longest-equal-subarray
find-the-longest-equal-subarray
{ "questionId": "2832", "questionFrontendId": "2831", "title": "Find the Longest Equal Subarray", "titleSlug": "find-the-longest-equal-subarray", "isPaidOnly": false, "difficulty": "Medium", "likes": 579, "dislikes": 14, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums and an integer k. A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray. Return the length of the longest possible equal subarray after deleting at most k elements from nums. A subarray is a contiguous, possibly...
You are given a 0-indexed integer array nums and an integer k. A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray. Return the length of the longest possible equal subarray after deleting at most k elements from nums. A subarray is a contiguous, possibly emp...
my_solution = Solution() test_input = { "nums": [1,3,2,3,1,3], "k": 3 } assert my_solution.longestEqualSubarray(**test_input) == 3 test_input = { "nums": [1,1,2,2,1,1], "k": 2 } assert my_solution.longestEqualSubarray(**test_input) == 4 test_input = { "nums": [1], "k": 0 } assert my_solution.longestEqualSubarray(**...
1,692,498,600
biweekly-contest-111-count-pairs-whose-sum-is-less-than-target
https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target
count-pairs-whose-sum-is-less-than-target
{ "questionId": "2917", "questionFrontendId": "2824", "title": "Count Pairs Whose Sum is Less than Target", "titleSlug": "count-pairs-whose-sum-is-less-than-target", "isPaidOnly": false, "difficulty": "Easy", "likes": 358, "dislikes": 23, "categoryTitle": "Algorithms" }
""" Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target. Example 1: Input: nums = [-1,1,2,3,1], target = 2 Output: 3 Explanation: There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1...
Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target. Example 1: Input: nums = [-1,1,2,3,1], target = 2 Output: 3 Explanation: There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1) si...
my_solution = Solution() test_input = { "nums": [-1,1,2,3,1], "target": 2 } assert my_solution.countPairs(**test_input) == 3 test_input = { "nums": [-6,2,5,-2,-7,-1,3], "target": -2 } assert my_solution.countPairs(**test_input) == 10 test_input = { "nums": [9,-5,-5,5,-5,-4,-6,6,-6], "target": 3 } assert my_solution...
1,692,455,400
biweekly-contest-111-make-string-a-subsequence-using-cyclic-increments
https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments
make-string-a-subsequence-using-cyclic-increments
{ "questionId": "3018", "questionFrontendId": "2825", "title": "Make String a Subsequence Using Cyclic Increments", "titleSlug": "make-string-a-subsequence-using-cyclic-increments", "isPaidOnly": false, "difficulty": "Medium", "likes": 273, "dislikes": 9, "categoryTitle": "Algorithms" }
""" You are given two 0-indexed strings str1 and str2. In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'. Return true if it is possible to make str2 a subse...
You are given two 0-indexed strings str1 and str2. In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'. Return true if it is possible to make str2 a subsequen...
my_solution = Solution() test_input = { "str1": "ab", "str2": "d" } assert my_solution.canMakeSubsequence(**test_input) == False test_input = { "str1": "a", "str2": "d" } assert my_solution.canMakeSubsequence(**test_input) == False test_input = { "str1": "abc", "str2": "ad" } assert my_solution.canMakeSubsequence(*...
1,692,455,400
biweekly-contest-111-sorting-three-groups
https://leetcode.com/problems/sorting-three-groups
sorting-three-groups
{ "questionId": "2904", "questionFrontendId": "2826", "title": "Sorting Three Groups", "titleSlug": "sorting-three-groups", "isPaidOnly": false, "difficulty": "Medium", "likes": 347, "dislikes": 72, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums of length n. The numbers from 0 to n - 1 are divided into three groups numbered from 1 to 3, where number i belongs to group nums[i]. Notice that some groups may be empty. You are allowed to perform this operation any number of times: * Pick number x and change its g...
You are given a 0-indexed integer array nums of length n. The numbers from 0 to n - 1 are divided into three groups numbered from 1 to 3, where number i belongs to group nums[i]. Notice that some groups may be empty. You are allowed to perform this operation any number of times: * Pick number x and change its group...
my_solution = Solution() test_input = { "nums": [2,1,3,2,1] } assert my_solution.minimumOperations(**test_input) == 3 test_input = { "nums": [1,3,2,1,3,3] } assert my_solution.minimumOperations(**test_input) == 2 test_input = { "nums": [2,2,2,2,3,3] } assert my_solution.minimumOperations(**test_input) == 0 test_in...
1,692,455,400
biweekly-contest-111-number-of-beautiful-integers-in-the-range
https://leetcode.com/problems/number-of-beautiful-integers-in-the-range
number-of-beautiful-integers-in-the-range
{ "questionId": "3017", "questionFrontendId": "2827", "title": "Number of Beautiful Integers in the Range", "titleSlug": "number-of-beautiful-integers-in-the-range", "isPaidOnly": false, "difficulty": "Hard", "likes": 313, "dislikes": 24, "categoryTitle": "Algorithms" }
""" You are given positive integers low, high, and k. A number is beautiful if it meets both of the following conditions: * The count of even digits in the number is equal to the count of odd digits. * The number is divisible by k. Return the number of beautiful integers in the range [low, high]. Example 1: Inpu...
You are given positive integers low, high, and k. A number is beautiful if it meets both of the following conditions: * The count of even digits in the number is equal to the count of odd digits. * The number is divisible by k. Return the number of beautiful integers in the range [low, high]. Example 1: Input: l...
my_solution = Solution() test_input = { "low": 10, "high": 20, "k": 3 } assert my_solution.numberOfBeautifulIntegers(**test_input) == 2 test_input = { "low": 1, "high": 10, "k": 1 } assert my_solution.numberOfBeautifulIntegers(**test_input) == 1 test_input = { "low": 5, "high": 5, "k": 2 } assert my_solution.number...
1,692,455,400
weekly-contest-358-max-pair-sum-in-an-array
https://leetcode.com/problems/max-pair-sum-in-an-array
max-pair-sum-in-an-array
{ "questionId": "2902", "questionFrontendId": "2815", "title": "Max Pair Sum in an Array", "titleSlug": "max-pair-sum-in-an-array", "isPaidOnly": false, "difficulty": "Easy", "likes": 274, "dislikes": 91, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the maximum digit in both numbers are equal. Return the maximum sum or -1 if no such pair exists. Example 1: Input: nums = [51,71,17,24,42] Output: 88 Explanation: For i = 1 and j = 2, nums[i] ...
You are given a 0-indexed integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the maximum digit in both numbers are equal. Return the maximum sum or -1 if no such pair exists. Example 1: Input: nums = [51,71,17,24,42] Output: 88 Explanation: For i = 1 and j = 2, nums[i] and ...
my_solution = Solution() test_input = { "nums": [51,71,17,24,42] } assert my_solution.maxSum(**test_input) == 88 test_input = { "nums": [1,2,3,4] } assert my_solution.maxSum(**test_input) == -1 test_input = { "nums": [31,25,72,79,74] } assert my_solution.maxSum(**test_input) == 146 test_input = { "nums": [84,91,18...
1,691,893,800
weekly-contest-358-double-a-number-represented-as-a-linked-list
https://leetcode.com/problems/double-a-number-represented-as-a-linked-list
double-a-number-represented-as-a-linked-list
{ "questionId": "2871", "questionFrontendId": "2816", "title": "Double a Number Represented as a Linked List", "titleSlug": "double-a-number-represented-as-a-linked-list", "isPaidOnly": false, "difficulty": "Medium", "likes": 397, "dislikes": 5, "categoryTitle": "Algorithms" }
""" You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Return the head of the linked list after doubling it. Example 1: [https://assets.leetcode.com/uploads/2023/05/28/example.png] Input: head = [1,8,9] Output: [3,7,8] Explanation: The figure above correspo...
You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Return the head of the linked list after doubling it. Example 1: [https://assets.leetcode.com/uploads/2023/05/28/example.png] Input: head = [1,8,9] Output: [3,7,8] Explanation: The figure above corresponds ...
my_solution = Solution() _f1 = lambda lst: ListNode(lst[0], _f1(lst[1:])) if lst else None _f2 = lambda node: [node.val] + _f2(node.next) if node else [] test_input = { "head": _f1([1,8,9]) } assert _f2(my_solution.doubleIt(**test_input)) == [3,7,8] test_input = { "head": _f1([9,9,9]) } assert _f2(my_solution.doub...
1,691,893,800
weekly-contest-358-minimum-absolute-difference-between-elements-with-constraint
https://leetcode.com/problems/minimum-absolute-difference-between-elements-with-constraint
minimum-absolute-difference-between-elements-with-constraint
{ "questionId": "3000", "questionFrontendId": "2817", "title": "Minimum Absolute Difference Between Elements With Constraint", "titleSlug": "minimum-absolute-difference-between-elements-with-constraint", "isPaidOnly": false, "difficulty": "Medium", "likes": 618, "dislikes": 64, "categoryTitle": "Algor...
""" You are given a 0-indexed integer array nums and an integer x. Find the minimum absolute difference between two elements in the array that are at least x indices apart. In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized. Return an integer denoting the minim...
You are given a 0-indexed integer array nums and an integer x. Find the minimum absolute difference between two elements in the array that are at least x indices apart. In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized. Return an integer denoting the minimum a...
my_solution = Solution() test_input = { "nums": [4,3,2,4], "x": 2 } assert my_solution.minAbsoluteDifference(**test_input) == 0 test_input = { "nums": [5,3,2,10,15], "x": 1 } assert my_solution.minAbsoluteDifference(**test_input) == 1 test_input = { "nums": [1,2,3,4], "x": 3 } assert my_solution.minAbsoluteDifferen...
1,691,893,800
weekly-contest-358-apply-operations-to-maximize-score
https://leetcode.com/problems/apply-operations-to-maximize-score
apply-operations-to-maximize-score
{ "questionId": "3001", "questionFrontendId": "2818", "title": "Apply Operations to Maximize Score", "titleSlug": "apply-operations-to-maximize-score", "isPaidOnly": false, "difficulty": "Hard", "likes": 301, "dislikes": 9, "categoryTitle": "Algorithms" }
""" You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1. You have to maximize your score by applying the following operation at most k times: * Choose any non-empty subarray nums[l, ..., r] that you haven't chosen previously. * Choose an element x of nums[l, ....
You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1. You have to maximize your score by applying the following operation at most k times: * Choose any non-empty subarray nums[l, ..., r] that you haven't chosen previously. * Choose an element x of nums[l, ..., ...
my_solution = Solution() test_input = { "nums": [8,3,9,3,8], "k": 2 } assert my_solution.maximumScore(**test_input) == 81 test_input = { "nums": [19,12,14,6,10,18], "k": 3 } assert my_solution.maximumScore(**test_input) == 4788 test_input = { "nums": [3289,2832,14858,22011], "k": 6 } assert my_solution.maximumScore...
1,691,893,800
weekly-contest-357-faulty-keyboard
https://leetcode.com/problems/faulty-keyboard
faulty-keyboard
{ "questionId": "2886", "questionFrontendId": "2810", "title": "Faulty Keyboard", "titleSlug": "faulty-keyboard", "isPaidOnly": false, "difficulty": "Easy", "likes": 343, "dislikes": 5, "categoryTitle": "Algorithms" }
""" Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected. You are given a 0-indexed string s, and you type each character of s using your faulty keyboard. Return the final string that will be present on you...
Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected. You are given a 0-indexed string s, and you type each character of s using your faulty keyboard. Return the final string that will be present on your la...
my_solution = Solution() test_input = { "s": "string" } assert my_solution.finalString(**test_input) == "rtsng" test_input = { "s": "poiinter" } assert my_solution.finalString(**test_input) == "ponter" test_input = { "s": "goci" } assert my_solution.finalString(**test_input) == "cog" test_input = { "s": "ksi" } as...
1,691,289,000
weekly-contest-357-check-if-it-is-possible-to-split-array
https://leetcode.com/problems/check-if-it-is-possible-to-split-array
check-if-it-is-possible-to-split-array
{ "questionId": "2916", "questionFrontendId": "2811", "title": "Check if it is Possible to Split Array", "titleSlug": "check-if-it-is-possible-to-split-array", "isPaidOnly": false, "difficulty": "Medium", "likes": 425, "dislikes": 84, "categoryTitle": "Algorithms" }
""" You are given an array nums of length n and an integer m. You need to determine if it is possible to split the array into n non-empty arrays by performing a series of steps. In each step, you can select an existing array (which may be the result of previous steps) with a length of at least two and split it into tw...
You are given an array nums of length n and an integer m. You need to determine if it is possible to split the array into n non-empty arrays by performing a series of steps. In each step, you can select an existing array (which may be the result of previous steps) with a length of at least two and split it into two su...
my_solution = Solution() test_input = { "nums": [2, 2, 1], "m": 4 } assert my_solution.canSplitArray(**test_input) == True test_input = { "nums": [2, 3, 3, 2, 3], "m": 6 } assert my_solution.canSplitArray(**test_input) == True test_input = { "nums": [1], "m": 1 } assert my_solution.canSplitArray(**test_input) == Tr...
1,691,289,000
weekly-contest-357-find-the-safest-path-in-a-grid
https://leetcode.com/problems/find-the-safest-path-in-a-grid
find-the-safest-path-in-a-grid
{ "questionId": "2914", "questionFrontendId": "2812", "title": "Find the Safest Path in a Grid", "titleSlug": "find-the-safest-path-in-a-grid", "isPaidOnly": false, "difficulty": "Medium", "likes": 706, "dislikes": 68, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: * A cell containing a thief if grid[r][c] = 1 * An empty cell if grid[r][c] = 0 You are initially positioned at cell (0, 0). In one move, you can move to any adjacent cell in the grid, including cells containing thieves. The safen...
You are given a 0-indexed 2D matrix grid of size n x n, where (r, c) represents: * A cell containing a thief if grid[r][c] = 1 * An empty cell if grid[r][c] = 0 You are initially positioned at cell (0, 0). In one move, you can move to any adjacent cell in the grid, including cells containing thieves. The safeness ...
my_solution = Solution() test_input = { "grid": [[1,0,0],[0,0,0],[0,0,1]] } assert my_solution.maximumSafenessFactor(**test_input) == 0 test_input = { "grid": [[0,0,1],[0,0,0],[0,0,0]] } assert my_solution.maximumSafenessFactor(**test_input) == 2 test_input = { "grid": [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] } as...
1,691,289,000
weekly-contest-357-maximum-elegance-of-a-k-length-subsequence
https://leetcode.com/problems/maximum-elegance-of-a-k-length-subsequence
maximum-elegance-of-a-k-length-subsequence
{ "questionId": "2894", "questionFrontendId": "2813", "title": "Maximum Elegance of a K-Length Subsequence", "titleSlug": "maximum-elegance-of-a-k-length-subsequence", "isPaidOnly": false, "difficulty": "Hard", "likes": 270, "dislikes": 3, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed 2D integer array items of length n and an integer k. items[i] = [profiti, categoryi], where profiti and categoryi denote the profit and category of the ith item respectively. Let's define the elegance of a subsequence of items as total_profit + distinct_categories2, where total_profit is...
You are given a 0-indexed 2D integer array items of length n and an integer k. items[i] = [profiti, categoryi], where profiti and categoryi denote the profit and category of the ith item respectively. Let's define the elegance of a subsequence of items as total_profit + distinct_categories2, where total_profit is the...
my_solution = Solution() test_input = { "items": [[3,2],[5,1],[10,1]], "k": 2 } assert my_solution.findMaximumElegance(**test_input) == 17 test_input = { "items": [[3,1],[3,1],[2,2],[5,3]], "k": 3 } assert my_solution.findMaximumElegance(**test_input) == 19 test_input = { "items": [[1,1],[2,1],[3,1]], "k": 3 } asse...
1,691,289,000
biweekly-contest-110-account-balance-after-rounded-purchase
https://leetcode.com/problems/account-balance-after-rounded-purchase
account-balance-after-rounded-purchase
{ "questionId": "2955", "questionFrontendId": "2806", "title": "Account Balance After Rounded Purchase", "titleSlug": "account-balance-after-rounded-purchase", "isPaidOnly": false, "difficulty": "Easy", "likes": 178, "dislikes": 37, "categoryTitle": "Algorithms" }
""" Initially, you have a bank account balance of 100 dollars. You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars. At the store where you will make the purchase, the purchase amount is rounded to the nearest multiple of 10. In other words, you pay a non-negative am...
Initially, you have a bank account balance of 100 dollars. You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars. At the store where you will make the purchase, the purchase amount is rounded to the nearest multiple of 10. In other words, you pay a non-negative amount...
my_solution = Solution() test_input = { "purchaseAmount": 9 } assert my_solution.accountBalanceAfterPurchase(**test_input) == 90 test_input = { "purchaseAmount": 15 } assert my_solution.accountBalanceAfterPurchase(**test_input) == 80 test_input = { "purchaseAmount": 10 } assert my_solution.accountBalanceAfterPurcha...
1,691,245,800
biweekly-contest-110-insert-greatest-common-divisors-in-linked-list
https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list
insert-greatest-common-divisors-in-linked-list
{ "questionId": "2903", "questionFrontendId": "2807", "title": "Insert Greatest Common Divisors in Linked List", "titleSlug": "insert-greatest-common-divisors-in-linked-list", "isPaidOnly": false, "difficulty": "Medium", "likes": 390, "dislikes": 12, "categoryTitle": "Algorithms" }
""" Given the head of a linked list head, in which each node contains an integer value. Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them. Return the linked list after insertion. The greatest common divisor of two numbers is the largest positive integer...
Given the head of a linked list head, in which each node contains an integer value. Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them. Return the linked list after insertion. The greatest common divisor of two numbers is the largest positive integer tha...
my_solution = Solution() _f1 = lambda lst: ListNode(lst[0], _f1(lst[1:])) if lst else None _f2 = lambda node: [node.val] + _f2(node.next) if node else [] test_input = { "head": _f1([18,6,10,3]) } assert _f2(my_solution.insertGreatestCommonDivisors(**test_input)) == [18,6,6,2,10,1,3] test_input = { "head": _f1([7])...
1,691,245,800
biweekly-contest-110-minimum-seconds-to-equalize-a-circular-array
https://leetcode.com/problems/minimum-seconds-to-equalize-a-circular-array
minimum-seconds-to-equalize-a-circular-array
{ "questionId": "2920", "questionFrontendId": "2808", "title": "Minimum Seconds to Equalize a Circular Array", "titleSlug": "minimum-seconds-to-equalize-a-circular-array", "isPaidOnly": false, "difficulty": "Medium", "likes": 481, "dislikes": 25, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array nums containing n integers. At each second, you perform the following operation on the array: * For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n]. Note that all the elements get replaced simultaneously. Re...
You are given a 0-indexed array nums containing n integers. At each second, you perform the following operation on the array: * For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n]. Note that all the elements get replaced simultaneously. Return...
my_solution = Solution() test_input = { "nums": [1,2,1,2] } assert my_solution.minimumSeconds(**test_input) == 1 test_input = { "nums": [2,1,3,3,2] } assert my_solution.minimumSeconds(**test_input) == 2 test_input = { "nums": [5,5,5,5] } assert my_solution.minimumSeconds(**test_input) == 0 test_input = { "nums": [...
1,691,245,800
biweekly-contest-110-minimum-time-to-make-array-sum-at-most-x
https://leetcode.com/problems/minimum-time-to-make-array-sum-at-most-x
minimum-time-to-make-array-sum-at-most-x
{ "questionId": "2952", "questionFrontendId": "2809", "title": "Minimum Time to Make Array Sum At Most x", "titleSlug": "minimum-time-to-make-array-sum-at-most-x", "isPaidOnly": false, "difficulty": "Hard", "likes": 207, "dislikes": 10, "categoryTitle": "Algorithms" }
""" You are given two 0-indexed integer arrays nums1 and nums2 of equal length. Every second, for all indices 0 <= i < nums1.length, value of nums1[i] is incremented by nums2[i]. After this is done, you can do the following operation: * Choose an index 0 <= i < nums1.length and make nums1[i] = 0. You are also given ...
You are given two 0-indexed integer arrays nums1 and nums2 of equal length. Every second, for all indices 0 <= i < nums1.length, value of nums1[i] is incremented by nums2[i]. After this is done, you can do the following operation: * Choose an index 0 <= i < nums1.length and make nums1[i] = 0. You are also given an i...
my_solution = Solution() test_input = { "nums1": [1,2,3], "nums2": [1,2,3], "x": 4 } assert my_solution.minimumTime(**test_input) == 3 test_input = { "nums1": [1,2,3], "nums2": [3,3,3], "x": 4 } assert my_solution.minimumTime(**test_input) == -1 test_input = { "nums1": [4,4,9,10], "nums2": [4,4,1,3], "x": 16 } asse...
1,691,245,800
weekly-contest-356-number-of-employees-who-met-the-target
https://leetcode.com/problems/number-of-employees-who-met-the-target
number-of-employees-who-met-the-target
{ "questionId": "2876", "questionFrontendId": "2798", "title": "Number of Employees Who Met the Target", "titleSlug": "number-of-employees-who-met-the-target", "isPaidOnly": false, "difficulty": "Easy", "likes": 362, "dislikes": 46, "categoryTitle": "Algorithms" }
""" There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company. The company requires each employee to work for at least target hours. You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target. Return th...
There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company. The company requires each employee to work for at least target hours. You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target. Return the in...
my_solution = Solution() test_input = { "hours": [0,1,2,3,4], "target": 2 } assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 3 test_input = { "hours": [5,1,4,2,2], "target": 6 } assert my_solution.numberOfEmployeesWhoMetTarget(**test_input) == 0 test_input = { "hours": [98], "target": 5 } assert my...
1,690,684,200
weekly-contest-356-count-complete-subarrays-in-an-array
https://leetcode.com/problems/count-complete-subarrays-in-an-array
count-complete-subarrays-in-an-array
{ "questionId": "2856", "questionFrontendId": "2799", "title": "Count Complete Subarrays in an Array", "titleSlug": "count-complete-subarrays-in-an-array", "isPaidOnly": false, "difficulty": "Medium", "likes": 464, "dislikes": 9, "categoryTitle": "Algorithms" }
""" You are given an array nums consisting of positive integers. We call a subarray of an array complete if the following condition is satisfied: * The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array. Return the number of complete subarrays. A subarray is ...
You are given an array nums consisting of positive integers. We call a subarray of an array complete if the following condition is satisfied: * The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array. Return the number of complete subarrays. A subarray is a co...
my_solution = Solution() test_input = { "nums": [1,3,1,2,2] } assert my_solution.countCompleteSubarrays(**test_input) == 4 test_input = { "nums": [5,5,5,5] } assert my_solution.countCompleteSubarrays(**test_input) == 10 test_input = { "nums": [459,459,962,1579,1435,756,1872,1597] } assert my_solution.countCompleteS...
1,690,684,200
weekly-contest-356-shortest-string-that-contains-three-strings
https://leetcode.com/problems/shortest-string-that-contains-three-strings
shortest-string-that-contains-three-strings
{ "questionId": "2877", "questionFrontendId": "2800", "title": "Shortest String That Contains Three Strings", "titleSlug": "shortest-string-that-contains-three-strings", "isPaidOnly": false, "difficulty": "Medium", "likes": 301, "dislikes": 244, "categoryTitle": "Algorithms" }
""" Given three strings a, b, and c, your task is to find a string that has the minimum length and contains all three strings as substrings. If there are multiple such strings, return the lexicographically smallest one. Return a string denoting the answer to the problem. Notes * A string a is lexicographically sma...
Given three strings a, b, and c, your task is to find a string that has the minimum length and contains all three strings as substrings. If there are multiple such strings, return the lexicographically smallest one. Return a string denoting the answer to the problem. Notes * A string a is lexicographically smaller...
my_solution = Solution() test_input = { "a": "abc", "b": "bca", "c": "aaa" } assert my_solution.minimumString(**test_input) == "aaabca" test_input = { "a": "ab", "b": "ba", "c": "aba" } assert my_solution.minimumString(**test_input) == "aba" test_input = { "a": "xyyyz", "b": "xzyz", "c": "zzz" } assert my_solution....
1,690,684,200
weekly-contest-356-count-stepping-numbers-in-range
https://leetcode.com/problems/count-stepping-numbers-in-range
count-stepping-numbers-in-range
{ "questionId": "2921", "questionFrontendId": "2801", "title": "Count Stepping Numbers in Range", "titleSlug": "count-stepping-numbers-in-range", "isPaidOnly": false, "difficulty": "Hard", "likes": 312, "dislikes": 8, "categoryTitle": "Algorithms" }
""" Given two positive integers low and high represented as strings, find the count of stepping numbers in the inclusive range [low, high]. A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1. Return an integer denoting the count of stepping numbers in the inc...
Given two positive integers low and high represented as strings, find the count of stepping numbers in the inclusive range [low, high]. A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1. Return an integer denoting the count of stepping numbers in the inclusi...
my_solution = Solution() test_input = { "low": "1", "high": "11" } assert my_solution.countSteppingNumbers(**test_input) == 10 test_input = { "low": "90", "high": "101" } assert my_solution.countSteppingNumbers(**test_input) == 2 test_input = { "low": "2", "high": "40" } assert my_solution.countSteppingNumbers(**te...
1,690,684,200
weekly-contest-355-split-strings-by-separator
https://leetcode.com/problems/split-strings-by-separator
split-strings-by-separator
{ "questionId": "2881", "questionFrontendId": "2788", "title": "Split Strings by Separator", "titleSlug": "split-strings-by-separator", "isPaidOnly": false, "difficulty": "Easy", "likes": 262, "dislikes": 4, "categoryTitle": "Algorithms" }
""" Given an array of strings words and a character separator, split each string in words by separator. Return an array of strings containing the new strings formed after the splits, excluding empty strings. Notes * separator is used to determine where the split should occur, but it is not included as part of the r...
Given an array of strings words and a character separator, split each string in words by separator. Return an array of strings containing the new strings formed after the splits, excluding empty strings. Notes * separator is used to determine where the split should occur, but it is not included as part of the resul...
my_solution = Solution() test_input = { "words": ["one.two.three","four.five","six"], "separator": "." } assert my_solution.splitWordsBySeparator(**test_input) == ["one","two","three","four","five","six"] test_input = { "words": ["$easy$","$problem$"], "separator": "$" } assert my_solution.splitWordsBySeparator(**te...
1,690,079,400
weekly-contest-355-largest-element-in-an-array-after-merge-operations
https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations
largest-element-in-an-array-after-merge-operations
{ "questionId": "2872", "questionFrontendId": "2789", "title": "Largest Element in an Array after Merge Operations", "titleSlug": "largest-element-in-an-array-after-merge-operations", "isPaidOnly": false, "difficulty": "Medium", "likes": 413, "dislikes": 26, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array nums consisting of positive integers. You can do the following operation on the array any number of times: * Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i]...
You are given a 0-indexed array nums consisting of positive integers. You can do the following operation on the array any number of times: * Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] fro...
my_solution = Solution() test_input = { "nums": [2,3,7,9,3] } assert my_solution.maxArrayValue(**test_input) == 21 test_input = { "nums": [5,3,3] } assert my_solution.maxArrayValue(**test_input) == 11 test_input = { "nums": [77] } assert my_solution.maxArrayValue(**test_input) == 77 test_input = { "nums": [34,95,5...
1,690,079,400
weekly-contest-355-maximum-number-of-groups-with-increasing-length
https://leetcode.com/problems/maximum-number-of-groups-with-increasing-length
maximum-number-of-groups-with-increasing-length
{ "questionId": "2919", "questionFrontendId": "2790", "title": "Maximum Number of Groups With Increasing Length", "titleSlug": "maximum-number-of-groups-with-increasing-length", "isPaidOnly": false, "difficulty": "Hard", "likes": 383, "dislikes": 38, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array usageLimits of length n. Your task is to create groups using numbers from 0 to n - 1, ensuring that each number, i, is used no more than usageLimits[i] times in total across all groups. You must also satisfy the following conditions: * Each group must consist of distinct numbers, ...
You are given a 0-indexed array usageLimits of length n. Your task is to create groups using numbers from 0 to n - 1, ensuring that each number, i, is used no more than usageLimits[i] times in total across all groups. You must also satisfy the following conditions: * Each group must consist of distinct numbers, mean...
my_solution = Solution() test_input = { "usageLimits": [1,2,5] } assert my_solution.maxIncreasingGroups(**test_input) == 3 test_input = { "usageLimits": [2,1,2] } assert my_solution.maxIncreasingGroups(**test_input) == 2 test_input = { "usageLimits": [1,1] } assert my_solution.maxIncreasingGroups(**test_input) == 1...
1,690,079,400
weekly-contest-355-count-paths-that-can-form-a-palindrome-in-a-tree
https://leetcode.com/problems/count-paths-that-can-form-a-palindrome-in-a-tree
count-paths-that-can-form-a-palindrome-in-a-tree
{ "questionId": "2905", "questionFrontendId": "2791", "title": "Count Paths That Can Form a Palindrome in a Tree", "titleSlug": "count-paths-that-can-form-a-palindrome-in-a-tree", "isPaidOnly": false, "difficulty": "Hard", "likes": 343, "dislikes": 4, "categoryTitle": "Algorithms" }
""" You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1. You are also given a strin...
You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1. You are also given a string s ...
my_solution = Solution() test_input = { "parent": [-1,0,0,1,1,2], "s": "acaabc" } assert my_solution.countPalindromePaths(**test_input) == 8 test_input = { "parent": [-1,0,0,0,0], "s": "aaaaa" } assert my_solution.countPalindromePaths(**test_input) == 10 test_input = { "parent": [-1,0], "s": "pi" } assert my_soluti...
1,690,079,400
biweekly-contest-109-check-if-array-is-good
https://leetcode.com/problems/check-if-array-is-good
check-if-array-is-good
{ "questionId": "2892", "questionFrontendId": "2784", "title": "Check if Array is Good", "titleSlug": "check-if-array-is-good", "isPaidOnly": false, "difficulty": "Easy", "likes": 233, "dislikes": 43, "categoryTitle": "Algorithms" }
""" You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3]....
You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3]. Re...
my_solution = Solution() test_input = { "nums": [1, 3, 3, 2] } assert my_solution.isGood(**test_input) == True test_input = { "nums": [2, 1, 3] } assert my_solution.isGood(**test_input) == False test_input = { "nums": [1, 1] } assert my_solution.isGood(**test_input) == True test_input = { "nums": [1, 2, 2] } asser...
1,690,036,200
biweekly-contest-109-sort-vowels-in-a-string
https://leetcode.com/problems/sort-vowels-in-a-string
sort-vowels-in-a-string
{ "questionId": "2887", "questionFrontendId": "2785", "title": "Sort Vowels in a String", "titleSlug": "sort-vowels-in-a-string", "isPaidOnly": false, "difficulty": "Medium", "likes": 883, "dislikes": 50, "categoryTitle": "Algorithms" }
""" Given a 0-indexed string s, permute s to get a new string t such that: * All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i]. * The vowels must be sorted in the nondecreasing order of their ASCII values. More...
Given a 0-indexed string s, permute s to get a new string t such that: * All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i]. * The vowels must be sorted in the nondecreasing order of their ASCII values. More for...
my_solution = Solution() test_input = { "s": "lEetcOde" } assert my_solution.sortVowels(**test_input) == "lEOtcede" test_input = { "s": "lYmpH" } assert my_solution.sortVowels(**test_input) == "lYmpH" test_input = { "s": "mDVD" } assert my_solution.sortVowels(**test_input) == "mDVD" test_input = { "s": "xdX" } ass...
1,690,036,200
biweekly-contest-109-visit-array-positions-to-maximize-score
https://leetcode.com/problems/visit-array-positions-to-maximize-score
visit-array-positions-to-maximize-score
{ "questionId": "2893", "questionFrontendId": "2786", "title": "Visit Array Positions to Maximize Score", "titleSlug": "visit-array-positions-to-maximize-score", "isPaidOnly": false, "difficulty": "Medium", "likes": 438, "dislikes": 21, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums and a positive integer x. You are initially at position 0 in the array and you can visit other positions according to the following rules: * If you are currently in position i, then you can move to any position j such that i < j. * For each position i that you visit,...
You are given a 0-indexed integer array nums and a positive integer x. You are initially at position 0 in the array and you can visit other positions according to the following rules: * If you are currently in position i, then you can move to any position j such that i < j. * For each position i that you visit, you...
my_solution = Solution() test_input = { "nums": [2,3,6,1,9,2], "x": 5 } assert my_solution.maxScore(**test_input) == 13 test_input = { "nums": [2,4,6,8], "x": 3 } assert my_solution.maxScore(**test_input) == 20 test_input = { "nums": [38,92,23,30,25,96,6,71,78,77,33,23,71,48,87,77,53,28,6,20,90,83,42,21,64,95,84,29...
1,690,036,200
biweekly-contest-109-ways-to-express-an-integer-as-sum-of-powers
https://leetcode.com/problems/ways-to-express-an-integer-as-sum-of-powers
ways-to-express-an-integer-as-sum-of-powers
{ "questionId": "2882", "questionFrontendId": "2787", "title": "Ways to Express an Integer as Sum of Powers", "titleSlug": "ways-to-express-an-integer-as-sum-of-powers", "isPaidOnly": false, "difficulty": "Medium", "likes": 346, "dislikes": 11, "categoryTitle": "Algorithms" }
""" Given two positive integers n and x. Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx. Since the result can be very large, return it modulo 109 + 7. For exa...
Given two positive integers n and x. Return the number of ways n can be expressed as the sum of the xth power of unique positive integers, in other words, the number of sets of unique integers [n1, n2, ..., nk] where n = n1x + n2x + ... + nkx. Since the result can be very large, return it modulo 109 + 7. For example...
my_solution = Solution() test_input = { "n": 10, "x": 2 } assert my_solution.numberOfWays(**test_input) == 1 test_input = { "n": 4, "x": 1 } assert my_solution.numberOfWays(**test_input) == 2 test_input = { "n": 1, "x": 1 } assert my_solution.numberOfWays(**test_input) == 1 test_input = { "n": 1, "x": 2 } assert m...
1,690,036,200
weekly-contest-354-sum-of-squares-of-special-elements
https://leetcode.com/problems/sum-of-squares-of-special-elements
sum-of-squares-of-special-elements
{ "questionId": "2844", "questionFrontendId": "2778", "title": "Sum of Squares of Special Elements ", "titleSlug": "sum-of-squares-of-special-elements", "isPaidOnly": false, "difficulty": "Easy", "likes": 218, "dislikes": 65, "categoryTitle": "Algorithms" }
""" You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums. Example 1: Input: nums = [1,2,3,4] Output: 21 Explanation: There are exactly 3 special elements in nums: nums[1] ...
You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums. Example 1: Input: nums = [1,2,3,4] Output: 21 Explanation: There are exactly 3 special elements in nums: nums[1] sinc...
my_solution = Solution() test_input = { "nums": [1,2,3,4] } assert my_solution.sumOfSquares(**test_input) == 21 test_input = { "nums": [2,7,1,19,18,3] } assert my_solution.sumOfSquares(**test_input) == 63 test_input = { "nums": [1] } assert my_solution.sumOfSquares(**test_input) == 1 test_input = { "nums": [2] } a...
1,689,474,600
weekly-contest-354-maximum-beauty-of-an-array-after-applying-operation
https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation
maximum-beauty-of-an-array-after-applying-operation
{ "questionId": "2891", "questionFrontendId": "2779", "title": "Maximum Beauty of an Array After Applying Operation", "titleSlug": "maximum-beauty-of-an-array-after-applying-operation", "isPaidOnly": false, "difficulty": "Medium", "likes": 559, "dislikes": 12, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array nums and a non-negative integer k. In one operation, you can do the following: * Choose an index i that hasn't been chosen before from the range [0, nums.length - 1]. * Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k]. The beauty of the array is the len...
You are given a 0-indexed array nums and a non-negative integer k. In one operation, you can do the following: * Choose an index i that hasn't been chosen before from the range [0, nums.length - 1]. * Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k]. The beauty of the array is the length ...
my_solution = Solution() test_input = { "nums": [4,6,1,2], "k": 2 } assert my_solution.maximumBeauty(**test_input) == 3 test_input = { "nums": [1,1,1,1], "k": 10 } assert my_solution.maximumBeauty(**test_input) == 4 test_input = { "nums": [12,71], "k": 10 } assert my_solution.maximumBeauty(**test_input) == 1 test_...
1,689,474,600
weekly-contest-354-minimum-index-of-a-valid-split
https://leetcode.com/problems/minimum-index-of-a-valid-split
minimum-index-of-a-valid-split
{ "questionId": "2888", "questionFrontendId": "2780", "title": "Minimum Index of a Valid Split", "titleSlug": "minimum-index-of-a-valid-split", "isPaidOnly": false, "difficulty": "Medium", "likes": 282, "dislikes": 11, "categoryTitle": "Algorithms" }
""" An element x of an integer array arr of length m is dominant if freq(x) * 2 > m, where freq(x) is the number of occurrences of x in arr. Note that this definition implies that arr can have at most one dominant element. You are given a 0-indexed integer array nums of length n with one dominant element. You can spl...
An element x of an integer array arr of length m is dominant if freq(x) * 2 > m, where freq(x) is the number of occurrences of x in arr. Note that this definition implies that arr can have at most one dominant element. You are given a 0-indexed integer array nums of length n with one dominant element. You can split n...
my_solution = Solution() test_input = { "nums": [1,2,2,2] } assert my_solution.minimumIndex(**test_input) == 2 test_input = { "nums": [2,1,3,1,1,1,7,1,2,1] } assert my_solution.minimumIndex(**test_input) == 4 test_input = { "nums": [3,3,3,3,7,2,2] } assert my_solution.minimumIndex(**test_input) == -1 test_input = ...
1,689,474,600
weekly-contest-354-length-of-the-longest-valid-substring
https://leetcode.com/problems/length-of-the-longest-valid-substring
length-of-the-longest-valid-substring
{ "questionId": "2884", "questionFrontendId": "2781", "title": "Length of the Longest Valid Substring", "titleSlug": "length-of-the-longest-valid-substring", "isPaidOnly": false, "difficulty": "Hard", "likes": 447, "dislikes": 9, "categoryTitle": "Algorithms" }
""" You are given a string word and an array of strings forbidden. A string is called valid if none of its substrings are present in forbidden. Return the length of the longest valid substring of the string word. A substring is a contiguous sequence of characters in a string, possibly empty. Example 1: Input: word...
You are given a string word and an array of strings forbidden. A string is called valid if none of its substrings are present in forbidden. Return the length of the longest valid substring of the string word. A substring is a contiguous sequence of characters in a string, possibly empty. Example 1: Input: word = "...
my_solution = Solution() test_input = { "word": "cbaaaabc", "forbidden": ["aaa","cb"] } assert my_solution.longestValidSubstring(**test_input) == 4 test_input = { "word": "leetcode", "forbidden": ["de","le","e"] } assert my_solution.longestValidSubstring(**test_input) == 4 test_input = { "word": "a", "forbidden": [...
1,689,474,600
weekly-contest-353-find-the-maximum-achievable-number
https://leetcode.com/problems/find-the-maximum-achievable-number
find-the-maximum-achievable-number
{ "questionId": "2812", "questionFrontendId": "2769", "title": "Find the Maximum Achievable Number", "titleSlug": "find-the-maximum-achievable-number", "isPaidOnly": false, "difficulty": "Easy", "likes": 230, "dislikes": 286, "categoryTitle": "Algorithms" }
""" You are given two integers, num and t. An integer x is called achievable if it can become equal to num after applying the following operation no more than t times: * Increase or decrease x by 1, and simultaneously increase or decrease num by 1. Return the maximum possible achievable number. It can be proven tha...
You are given two integers, num and t. An integer x is called achievable if it can become equal to num after applying the following operation no more than t times: * Increase or decrease x by 1, and simultaneously increase or decrease num by 1. Return the maximum possible achievable number. It can be proven that th...
my_solution = Solution() test_input = { "num": 4, "t": 1 } assert my_solution.theMaximumAchievableX(**test_input) == 6 test_input = { "num": 3, "t": 2 } assert my_solution.theMaximumAchievableX(**test_input) == 7 test_input = { "num": 1, "t": 1 } assert my_solution.theMaximumAchievableX(**test_input) == 3 test_inp...
1,688,869,800
weekly-contest-353-maximum-number-of-jumps-to-reach-the-last-index
https://leetcode.com/problems/maximum-number-of-jumps-to-reach-the-last-index
maximum-number-of-jumps-to-reach-the-last-index
{ "questionId": "2855", "questionFrontendId": "2770", "title": "Maximum Number of Jumps to Reach the Last Index", "titleSlug": "maximum-number-of-jumps-to-reach-the-last-index", "isPaidOnly": false, "difficulty": "Medium", "likes": 356, "dislikes": 10, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0. In one step, you can jump from index i to any index j such that: * 0 <= i < j < n * -target <= nums[j] - nums[i] <= target Return the maximum number of jumps you can make to reach index n - 1. If...
You are given a 0-indexed array nums of n integers and an integer target. You are initially positioned at index 0. In one step, you can jump from index i to any index j such that: * 0 <= i < j < n * -target <= nums[j] - nums[i] <= target Return the maximum number of jumps you can make to reach index n - 1. If the...
my_solution = Solution() test_input = { "nums": [1,3,6,4,1,2], "target": 2 } assert my_solution.maximumJumps(**test_input) == 3 test_input = { "nums": [1,3,6,4,1,2], "target": 3 } assert my_solution.maximumJumps(**test_input) == 5 test_input = { "nums": [1,3,6,4,1,2], "target": 0 } assert my_solution.maximumJumps(*...
1,688,869,800
weekly-contest-353-longest-non-decreasing-subarray-from-two-arrays
https://leetcode.com/problems/longest-non-decreasing-subarray-from-two-arrays
longest-non-decreasing-subarray-from-two-arrays
{ "questionId": "2869", "questionFrontendId": "2771", "title": "Longest Non-decreasing Subarray From Two Arrays", "titleSlug": "longest-non-decreasing-subarray-from-two-arrays", "isPaidOnly": false, "difficulty": "Medium", "likes": 505, "dislikes": 10, "categoryTitle": "Algorithms" }
""" You are given two 0-indexed integer arrays nums1 and nums2 of length n. Let's define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i]. Your task is to maximize the length of the longest non-decreasing subarray in ...
You are given two 0-indexed integer arrays nums1 and nums2 of length n. Let's define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i]. Your task is to maximize the length of the longest non-decreasing subarray in nums...
my_solution = Solution() test_input = { "nums1": [2,3,1], "nums2": [1,2,1] } assert my_solution.maxNonDecreasingLength(**test_input) == 2 test_input = { "nums1": [1,3,2,1], "nums2": [2,2,3,4] } assert my_solution.maxNonDecreasingLength(**test_input) == 4 test_input = { "nums1": [1,1], "nums2": [2,2] } assert my_sol...
1,688,869,800
weekly-contest-353-apply-operations-to-make-all-array-elements-equal-to-zero
https://leetcode.com/problems/apply-operations-to-make-all-array-elements-equal-to-zero
apply-operations-to-make-all-array-elements-equal-to-zero
{ "questionId": "2878", "questionFrontendId": "2772", "title": "Apply Operations to Make All Array Elements Equal to Zero", "titleSlug": "apply-operations-to-make-all-array-elements-equal-to-zero", "isPaidOnly": false, "difficulty": "Medium", "likes": 339, "dislikes": 19, "categoryTitle": "Algorithms"...
""" You are given a 0-indexed integer array nums and a positive integer k. You can apply the following operation on the array any number of times: * Choose any subarray of size k from the array and decrease all its elements by 1. Return true if you can make all the array elements equal to 0, or false otherwise. A ...
You are given a 0-indexed integer array nums and a positive integer k. You can apply the following operation on the array any number of times: * Choose any subarray of size k from the array and decrease all its elements by 1. Return true if you can make all the array elements equal to 0, or false otherwise. A suba...
my_solution = Solution() test_input = { "nums": [1,3,1,1], "k": 2 } assert my_solution.checkArray(**test_input) == False test_input = { "nums": [2,2,3,1,1,0], "k": 3 } assert my_solution.checkArray(**test_input) == True test_input = { "nums": [24,24,14,37,31,88,94,38,94,0,100,100,4,46,5,50,0,33,22,25,0], "k": 10 } ...
1,688,869,800
biweekly-contest-108-longest-alternating-subarray
https://leetcode.com/problems/longest-alternating-subarray
longest-alternating-subarray
{ "questionId": "2870", "questionFrontendId": "2765", "title": "Longest Alternating Subarray", "titleSlug": "longest-alternating-subarray", "isPaidOnly": false, "difficulty": "Easy", "likes": 181, "dislikes": 153, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if: * m is greater than 1. * s1 = s0 + 1. * The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] ...
You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if: * m is greater than 1. * s1 = s0 + 1. * The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-...
my_solution = Solution() test_input = { "nums": [2,3,4,3,4] } assert my_solution.alternatingSubarray(**test_input) == 4 test_input = { "nums": [4,5,6] } assert my_solution.alternatingSubarray(**test_input) == 2 test_input = { "nums": [31,32,31,32,33] } assert my_solution.alternatingSubarray(**test_input) == 4 test...
1,688,826,600
biweekly-contest-108-relocate-marbles
https://leetcode.com/problems/relocate-marbles
relocate-marbles
{ "questionId": "2834", "questionFrontendId": "2766", "title": "Relocate Marbles", "titleSlug": "relocate-marbles", "isPaidOnly": false, "difficulty": "Medium", "likes": 170, "dislikes": 13, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums representing the initial positions of some marbles. You are also given two 0-indexed integer arrays moveFrom and moveTo of equal length. Throughout moveFrom.length steps, you will change the positions of the marbles. On the ith step, you will move all marbles at positio...
You are given a 0-indexed integer array nums representing the initial positions of some marbles. You are also given two 0-indexed integer arrays moveFrom and moveTo of equal length. Throughout moveFrom.length steps, you will change the positions of the marbles. On the ith step, you will move all marbles at position mo...
my_solution = Solution() test_input = { "nums": [1,6,7,8], "moveFrom": [1,7,2], "moveTo": [2,9,5] } assert my_solution.relocateMarbles(**test_input) == [5,6,8,9] test_input = { "nums": [1,1,3,3], "moveFrom": [1,3], "moveTo": [2,2] } assert my_solution.relocateMarbles(**test_input) == [2] test_input = { "nums": [5,7...
1,688,826,600
biweekly-contest-108-partition-string-into-minimum-beautiful-substrings
https://leetcode.com/problems/partition-string-into-minimum-beautiful-substrings
partition-string-into-minimum-beautiful-substrings
{ "questionId": "2883", "questionFrontendId": "2767", "title": "Partition String Into Minimum Beautiful Substrings", "titleSlug": "partition-string-into-minimum-beautiful-substrings", "isPaidOnly": false, "difficulty": "Medium", "likes": 289, "dislikes": 8, "categoryTitle": "Algorithms" }
""" Given a binary string s, partition the string into one or more substrings such that each substring is beautiful. A string is beautiful if: * It doesn't contain leading zeros. * It's the binary representation of a number that is a power of 5. Return the minimum number of substrings in such partition. If it is i...
Given a binary string s, partition the string into one or more substrings such that each substring is beautiful. A string is beautiful if: * It doesn't contain leading zeros. * It's the binary representation of a number that is a power of 5. Return the minimum number of substrings in such partition. If it is impos...
my_solution = Solution() test_input = { "s": "1011" } assert my_solution.minimumBeautifulSubstrings(**test_input) == 2 test_input = { "s": "111" } assert my_solution.minimumBeautifulSubstrings(**test_input) == 3 test_input = { "s": "0" } assert my_solution.minimumBeautifulSubstrings(**test_input) == -1 test_input ...
1,688,826,600
biweekly-contest-108-number-of-black-blocks
https://leetcode.com/problems/number-of-black-blocks
number-of-black-blocks
{ "questionId": "2889", "questionFrontendId": "2768", "title": "Number of Black Blocks", "titleSlug": "number-of-black-blocks", "isPaidOnly": false, "difficulty": "Medium", "likes": 204, "dislikes": 21, "categoryTitle": "Algorithms" }
""" You are given two integers m and n representing the dimensions of a 0-indexed m x n grid. You are also given a 0-indexed 2D integer matrix coordinates, where coordinates[i] = [x, y] indicates that the cell with coordinates [x, y] is colored black. All cells in the grid that do not appear in coordinates are white. ...
You are given two integers m and n representing the dimensions of a 0-indexed m x n grid. You are also given a 0-indexed 2D integer matrix coordinates, where coordinates[i] = [x, y] indicates that the cell with coordinates [x, y] is colored black. All cells in the grid that do not appear in coordinates are white. A b...
my_solution = Solution() test_input = { "m": 3, "n": 3, "coordinates": [[0,0]] } assert my_solution.countBlackBlocks(**test_input) == [3,1,0,0,0] test_input = { "m": 3, "n": 3, "coordinates": [[0,0],[1,1],[0,2]] } assert my_solution.countBlackBlocks(**test_input) == [0,2,2,0,0] test_input = { "m": 32, "n": 32, "coo...
1,688,826,600
weekly-contest-352-longest-even-odd-subarray-with-threshold
https://leetcode.com/problems/longest-even-odd-subarray-with-threshold
longest-even-odd-subarray-with-threshold
{ "questionId": "2866", "questionFrontendId": "2760", "title": "Longest Even Odd Subarray With Threshold", "titleSlug": "longest-even-odd-subarray-with-threshold", "isPaidOnly": false, "difficulty": "Easy", "likes": 248, "dislikes": 243, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums and an integer threshold. Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions: * nums[l] % 2 == 0 * For all indices i in the range [l, r - 1], nums[i] % 2 != num...
You are given a 0-indexed integer array nums and an integer threshold. Find the length of the longest subarray of nums starting at index l and ending at index r (0 <= l <= r < nums.length) that satisfies the following conditions: * nums[l] % 2 == 0 * For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i ...
my_solution = Solution() test_input = { "nums": [3,2,5,4], "threshold": 5 } assert my_solution.longestAlternatingSubarray(**test_input) == 3 test_input = { "nums": [1,2], "threshold": 2 } assert my_solution.longestAlternatingSubarray(**test_input) == 1 test_input = { "nums": [2,3,4,5], "threshold": 4 } assert my_so...
1,688,265,000
weekly-contest-352-prime-pairs-with-target-sum
https://leetcode.com/problems/prime-pairs-with-target-sum
prime-pairs-with-target-sum
{ "questionId": "2873", "questionFrontendId": "2761", "title": "Prime Pairs With Target Sum", "titleSlug": "prime-pairs-with-target-sum", "isPaidOnly": false, "difficulty": "Medium", "likes": 311, "dislikes": 30, "categoryTitle": "Algorithms" }
""" You are given an integer n. We say that two integers x and y form a prime number pair if: * 1 <= x <= y <= n * x + y == n * x and y are prime numbers Return the 2D sorted list of prime number pairs [xi, yi]. The list should be sorted in increasing order of xi. If there are no prime number pairs at all, return ...
You are given an integer n. We say that two integers x and y form a prime number pair if: * 1 <= x <= y <= n * x + y == n * x and y are prime numbers Return the 2D sorted list of prime number pairs [xi, yi]. The list should be sorted in increasing order of xi. If there are no prime number pairs at all, return an e...
my_solution = Solution() test_input = { "n": 10 } assert my_solution.findPrimePairs(**test_input) == [[3,7],[5,5]] test_input = { "n": 2 } assert my_solution.findPrimePairs(**test_input) == [] test_input = { "n": 1 } assert my_solution.findPrimePairs(**test_input) == [] test_input = { "n": 3 } assert my_solution.f...
1,688,265,000
weekly-contest-352-continuous-subarrays
https://leetcode.com/problems/continuous-subarrays
continuous-subarrays
{ "questionId": "2868", "questionFrontendId": "2762", "title": "Continuous Subarrays", "titleSlug": "continuous-subarrays", "isPaidOnly": false, "difficulty": "Medium", "likes": 604, "dislikes": 17, "categoryTitle": "Algorithms" }
""" You are given a 0-indexed integer array nums. A subarray of nums is called continuous if: * Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2. Return the total number of continuous subarrays. A subarray is a contiguous non-empty...
You are given a 0-indexed integer array nums. A subarray of nums is called continuous if: * Let i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j, 0 <= |nums[i1] - nums[i2]| <= 2. Return the total number of continuous subarrays. A subarray is a contiguous non-empty seq...
my_solution = Solution() test_input = { "nums": [5,4,2,4] } assert my_solution.continuousSubarrays(**test_input) == 8 test_input = { "nums": [1,2,3] } assert my_solution.continuousSubarrays(**test_input) == 6 test_input = { "nums": [31,30,31,32] } assert my_solution.continuousSubarrays(**test_input) == 10 test_inp...
1,688,265,000
weekly-contest-352-sum-of-imbalance-numbers-of-all-subarrays
https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays
sum-of-imbalance-numbers-of-all-subarrays
{ "questionId": "2849", "questionFrontendId": "2763", "title": "Sum of Imbalance Numbers of All Subarrays", "titleSlug": "sum-of-imbalance-numbers-of-all-subarrays", "isPaidOnly": false, "difficulty": "Hard", "likes": 278, "dislikes": 7, "categoryTitle": "Algorithms" }
""" The imbalance number of a 0-indexed integer array arr of length n is defined as the number of indices in sarr = sorted(arr) such that: * 0 <= i < n - 1, and * sarr[i+1] - sarr[i] > 1 Here, sorted(arr) is the function that returns the sorted version of arr. Given a 0-indexed integer array nums, return the sum o...
The imbalance number of a 0-indexed integer array arr of length n is defined as the number of indices in sarr = sorted(arr) such that: * 0 <= i < n - 1, and * sarr[i+1] - sarr[i] > 1 Here, sorted(arr) is the function that returns the sorted version of arr. Given a 0-indexed integer array nums, return the sum of im...
my_solution = Solution() test_input = { "nums": [2,3,1,4] } assert my_solution.sumImbalanceNumbers(**test_input) == 3 test_input = { "nums": [1,3,3,3,5] } assert my_solution.sumImbalanceNumbers(**test_input) == 8 test_input = { "nums": [1] } assert my_solution.sumImbalanceNumbers(**test_input) == 0 test_input = { ...
1,688,265,000