diff --git a/minimum-operations-to-make-binary-array-elements-equal-to-one-ii.json b/minimum-operations-to-make-binary-array-elements-equal-to-one-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..f8ae2ecbb92de6ebe492bc5ecfa932ed9485d30f --- /dev/null +++ b/minimum-operations-to-make-binary-array-elements-equal-to-one-ii.json @@ -0,0 +1,35 @@ +{ + "id": 3477, + "name": "minimum-operations-to-make-binary-array-elements-equal-to-one-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/", + "date": "2024-06-08", + "task_description": "You are given a binary array `nums`. You can do the following operation on the array **any** number of times (possibly zero): Choose **any** index `i` from the array and **flip** **all** the elements from index `i` to the end of the array. **Flipping** an element means changing its value from 0 to 1, and from 1 to 0. Return the **minimum** number of operations required to make all elements in `nums` equal to 1. **Example 1:** **Input:** nums = [0,1,1,0,1] **Output:** 4 **Explanation:** We can do the following operations: Choose the index `i = 1`. The resulting array will be `nums = [0,**0**,**0**,**1**,**0**]`. Choose the index `i = 0`. The resulting array will be `nums = [**1**,**1**,**1**,**0**,**1**]`. Choose the index `i = 4`. The resulting array will be `nums = [1,1,1,0,**0**]`. Choose the index `i = 3`. The resulting array will be `nums = [1,1,1,**1**,**1**]`. **Example 2:** **Input:** nums = [1,0,0,0] **Output:** 1 **Explanation:** We can do the following operation: Choose the index `i = 1`. The resulting array will be `nums = [1,**1**,**1**,**1**]`. **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 1`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,1,0,1]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,0,0,0]", + "output": "1 " + } + ], + "constraints": [ + "Choose any index i from the array and flip all the elements from index i to the end of the array.", + "Choose the index i = 1. The resulting array will be nums = [0,0,0,1,0].", + "Choose the index i = 0. The resulting array will be nums = [1,1,1,0,1].", + "Choose the index i = 4. The resulting array will be nums = [1,1,1,0,0].", + "Choose the index i = 3. The resulting array will be nums = [1,1,1,1,1].", + "Choose the index i = 1. The resulting array will be nums = [1,1,1,1].", + "1 <= nums.length <= 105", + "0 <= nums[i] <= 1" + ], + "python_template": "class Solution(object):\n def minOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minOperations(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-columns-strictly-increasing.json b/minimum-operations-to-make-columns-strictly-increasing.json new file mode 100644 index 0000000000000000000000000000000000000000..1dffd584d77559fa6f26c791219049e96c033cf3 --- /dev/null +++ b/minimum-operations-to-make-columns-strictly-increasing.json @@ -0,0 +1,36 @@ +{ + "id": 3691, + "name": "minimum-operations-to-make-columns-strictly-increasing", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-operations-to-make-columns-strictly-increasing/", + "date": "2024-12-22", + "task_description": "You are given a `m x n` matrix `grid` consisting of non-negative integers. In one operation, you can increment the value of any `grid[i][j]` by 1. Return the **minimum** number of operations needed to make all columns of `grid` **strictly increasing**. **Example 1:** **Input:** grid = [[3,2],[1,3],[3,4],[0,1]] **Output:** 15 **Explanation:** To make the `0th` column strictly increasing, we can apply 3 operations on `grid[1][0]`, 2 operations on `grid[2][0]`, and 6 operations on `grid[3][0]`. To make the `1st` column strictly increasing, we can apply 4 operations on `grid[3][1]`. **Example 2:** **Input:** grid = [[3,2,1],[2,1,0],[1,2,3]] **Output:** 12 **Explanation:** To make the `0th` column strictly increasing, we can apply 2 operations on `grid[1][0]`, and 4 operations on `grid[2][0]`. To make the `1st` column strictly increasing, we can apply 2 operations on `grid[1][1]`, and 2 operations on `grid[2][1]`. To make the `2nd` column strictly increasing, we can apply 2 operations on `grid[1][2]`. **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 50` `0 <= grid[i][j] < 2500` ``` ```", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[3,2],[1,3],[3,4],[0,1]]", + "output": "15 " + }, + { + "label": "Example 2", + "input": "grid = [[3,2,1],[2,1,0],[1,2,3]]", + "output": "12 " + } + ], + "constraints": [ + "To make the 0th column strictly increasing, we can apply 3 operations on grid[1][0], 2 operations on grid[2][0], and 6 operations on grid[3][0].", + "To make the 1st column strictly increasing, we can apply 4 operations on grid[3][1].", + "To make the 0th column strictly increasing, we can apply 2 operations on grid[1][0], and 4 operations on grid[2][0].", + "To make the 1st column strictly increasing, we can apply 2 operations on grid[1][1], and 2 operations on grid[2][1].", + "To make the 2nd column strictly increasing, we can apply 2 operations on grid[1][2].", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 50", + "0 <= grid[i][j] < 2500" + ], + "python_template": "class Solution(object):\n def minimumOperations(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumOperations(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-median-of-array-equal-to-k.json b/minimum-operations-to-make-median-of-array-equal-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..6f24019170a0cc8e873227f31bd333b8536a4314 --- /dev/null +++ b/minimum-operations-to-make-median-of-array-equal-to-k.json @@ -0,0 +1,35 @@ +{ + "id": 3387, + "name": "minimum-operations-to-make-median-of-array-equal-to-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-make-median-of-array-equal-to-k/", + "date": "2024-03-31", + "task_description": "You are given an integer array `nums` and a **non-negative** integer `k`. In one operation, you can increase or decrease any element by 1. Return the **minimum** number of operations needed to make the **median** of `nums` _equal_ to `k`. The median of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the larger of the two values is taken. **Example 1:** **Input:** nums = [2,5,6,8,5], k = 4 **Output:** 2 **Explanation:** We can subtract one from `nums[1]` and `nums[4]` to obtain `[2, 4, 6, 8, 4]`. The median of the resulting array is equal to `k`. **Example 2:** **Input:** nums = [2,5,6,8,5], k = 7 **Output:** 3 **Explanation:** We can add one to `nums[1]` twice and add one to `nums[2]` once to obtain `[2, 7, 7, 8, 5]`. **Example 3:** **Input:** nums = [1,2,3,4,5,6], k = 4 **Output:** 0 **Explanation:** The median of the array is already equal to `k`. **Constraints:** `1 <= nums.length <= 2 * 105` `1 <= nums[i] <= 109` `1 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,5,6,8,5], k = 4", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [2,5,6,8,5], k = 7", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3,4,5,6], k = 4", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 2 * 105", + "1 <= nums[i] <= 109", + "1 <= k <= 109" + ], + "python_template": "class Solution(object):\n def minOperationsToMakeMedianK(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minOperationsToMakeMedianK(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOperationsToMakeMedianK" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-the-array-alternating.json b/minimum-operations-to-make-the-array-alternating.json new file mode 100644 index 0000000000000000000000000000000000000000..137837778772102fe23f1ce12d6f26aa6aad6cd8 --- /dev/null +++ b/minimum-operations-to-make-the-array-alternating.json @@ -0,0 +1,31 @@ +{ + "id": 2289, + "name": "minimum-operations-to-make-the-array-alternating", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/", + "date": "2022-02-06", + "task_description": "You are given a **0-indexed** array `nums` consisting of `n` positive integers. The array `nums` is called **alternating** if: `nums[i - 2] == nums[i]`, where `2 <= i <= n - 1`. `nums[i - 1] != nums[i]`, where `1 <= i <= n - 1`. In one **operation**, you can choose an index `i` and **change** `nums[i]` into **any** positive integer. Return _the **minimum number of operations** required to make the array alternating_. **Example 1:** ``` **Input:** nums = [3,1,3,2,4,3] **Output:** 3 **Explanation:** One way to make the array alternating is by converting it to [3,1,3,**1**,**3**,**1**]. The number of operations required in this case is 3. It can be proven that it is not possible to make the array alternating in less than 3 operations. ``` **Example 2:** ``` **Input:** nums = [1,2,2,2,2] **Output:** 2 **Explanation:** One way to make the array alternating is by converting it to [1,2,**1**,2,**1**]. The number of operations required in this case is 2. Note that the array cannot be converted to [**2**,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,1,3,2,4,3]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,2,2,2]", + "output": "2 " + } + ], + "constraints": [ + "nums[i - 2] == nums[i], where 2 <= i <= n - 1.", + "nums[i - 1] != nums[i], where 1 <= i <= n - 1.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def minimumOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumOperations(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-maximize-last-elements-in-arrays.json b/minimum-operations-to-maximize-last-elements-in-arrays.json new file mode 100644 index 0000000000000000000000000000000000000000..7ae37caee96097625104884e39744f870094d7f6 --- /dev/null +++ b/minimum-operations-to-maximize-last-elements-in-arrays.json @@ -0,0 +1,37 @@ +{ + "id": 3190, + "name": "minimum-operations-to-maximize-last-elements-in-arrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-maximize-last-elements-in-arrays/", + "date": "2023-11-05", + "task_description": "You are given two **0-indexed** integer arrays, `nums1` and `nums2`, both having length `n`. You are allowed to perform a series of **operations** (**possibly none**). In an operation, you select an index `i` in the range `[0, n - 1]` and **swap** the values of `nums1[i]` and `nums2[i]`. Your task is to find the **minimum** number of operations required to satisfy the following conditions: `nums1[n - 1]` is equal to the **maximum value** among all elements of `nums1`, i.e., `nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1])`. `nums2[n - 1]` is equal to the **maximum** **value** among all elements of `nums2`, i.e., `nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1])`. Return _an integer denoting the **minimum** number of operations needed to meet **both** conditions_, _or _`-1`_ if it is **impossible** to satisfy both conditions._ **Example 1:** ``` **Input:** nums1 = [1,2,7], nums2 = [4,5,3] **Output:** 1 **Explanation:** In this example, an operation can be performed using index i = 2. When nums1[2] and nums2[2] are swapped, nums1 becomes [1,2,3] and nums2 becomes [4,5,7]. Both conditions are now satisfied. It can be shown that the minimum number of operations needed to be performed is 1. So, the answer is 1. ``` **Example 2:** ``` **Input:** nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4] **Output:** 2 **Explanation:** In this example, the following operations can be performed: First operation using index i = 4. When nums1[4] and nums2[4] are swapped, nums1 becomes [2,3,4,5,4], and nums2 becomes [8,8,4,4,9]. Another operation using index i = 3. When nums1[3] and nums2[3] are swapped, nums1 becomes [2,3,4,4,4], and nums2 becomes [8,8,4,5,9]. Both conditions are now satisfied. It can be shown that the minimum number of operations needed to be performed is 2. So, the answer is 2. ``` **Example 3:** ``` **Input:** nums1 = [1,5,4], nums2 = [2,5,3] **Output:** -1 **Explanation:** In this example, it is not possible to satisfy both conditions. So, the answer is -1. ``` **Constraints:** `1 <= n == nums1.length == nums2.length <= 1000` `1 <= nums1[i] <= 109` `1 <= nums2[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,2,7], nums2 = [4,5,3]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums1 = [2,3,4,5,9], nums2 = [8,8,4,4,4]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums1 = [1,5,4], nums2 = [2,5,3]", + "output": "-1 " + } + ], + "constraints": [ + "nums1[n - 1] is equal to the maximum value among all elements of nums1, i.e., nums1[n - 1] = max(nums1[0], nums1[1], ..., nums1[n - 1]).", + "nums2[n - 1] is equal to the maximum value among all elements of nums2, i.e., nums2[n - 1] = max(nums2[0], nums2[1], ..., nums2[n - 1]).", + "1 <= n == nums1.length == nums2.length <= 1000", + "1 <= nums1[i] <= 109", + "1 <= nums2[i] <= 109" + ], + "python_template": "class Solution(object):\n def minOperations(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minOperations(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-write-the-letter-y-on-a-grid.json b/minimum-operations-to-write-the-letter-y-on-a-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..6ad7410bad98162a476513b25258f6258a56bd51 --- /dev/null +++ b/minimum-operations-to-write-the-letter-y-on-a-grid.json @@ -0,0 +1,37 @@ +{ + "id": 3335, + "name": "minimum-operations-to-write-the-letter-y-on-a-grid", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-write-the-letter-y-on-a-grid/", + "date": "2024-02-25", + "task_description": "You are given a **0-indexed** `n x n` grid where `n` is odd, and `grid[r][c]` is `0`, `1`, or `2`. We say that a cell belongs to the Letter **Y** if it belongs to one of the following: The diagonal starting at the top-left cell and ending at the center cell of the grid. The diagonal starting at the top-right cell and ending at the center cell of the grid. The vertical line starting at the center cell and ending at the bottom border of the grid. The Letter **Y** is written on the grid if and only if: All values at cells belonging to the Y are equal. All values at cells not belonging to the Y are equal. The values at cells belonging to the Y are different from the values at cells not belonging to the Y. Return _the **minimum** number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to_ `0`_,_ `1`_,_ _or_ `2`_._ **Example 1:** ``` **Input:** grid = [[1,2,2],[1,1,0],[0,1,0]] **Output:** 3 **Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0. It can be shown that 3 is the minimum number of operations needed to write Y on the grid. ``` **Example 2:** ``` **Input:** grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]] **Output:** 12 **Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2. It can be shown that 12 is the minimum number of operations needed to write Y on the grid. ``` **Constraints:** `3 <= n <= 49 ` `n == grid.length == grid[i].length` `0 <= grid[i][j] <= 2` `n` is odd.", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,2,2],[1,1,0],[0,1,0]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]", + "output": "12 " + } + ], + "constraints": [ + "The diagonal starting at the top-left cell and ending at the center cell of the grid.", + "The diagonal starting at the top-right cell and ending at the center cell of the grid.", + "The vertical line starting at the center cell and ending at the bottom border of the grid.", + "All values at cells belonging to the Y are equal.", + "All values at cells not belonging to the Y are equal.", + "The values at cells belonging to the Y are different from the values at cells not belonging to the Y.", + "3 <= n <= 49", + "n == grid.length == grid[i].length", + "0 <= grid[i][j] <= 2", + "n is odd." + ], + "python_template": "class Solution(object):\n def minimumOperationsToWriteY(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumOperationsToWriteY(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperationsToWriteY" + } +} \ No newline at end of file diff --git a/minimum-penalty-for-a-shop.json b/minimum-penalty-for-a-shop.json new file mode 100644 index 0000000000000000000000000000000000000000..b57b7c1f5bdd521fc5012b2cbd8703fbdaef17a0 --- /dev/null +++ b/minimum-penalty-for-a-shop.json @@ -0,0 +1,38 @@ +{ + "id": 2576, + "name": "minimum-penalty-for-a-shop", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-penalty-for-a-shop/", + "date": "2022-11-12", + "task_description": "You are given the customer visit log of a shop represented by a **0-indexed** string `customers` consisting only of characters `'N'` and `'Y'`: if the `ith` character is `'Y'`, it means that customers come at the `ith` hour whereas `'N'` indicates that no customers come at the `ith` hour. If the shop closes at the `jth` hour (`0 <= j <= n`), the **penalty** is calculated as follows: For every hour when the shop is open and no customers come, the penalty increases by `1`. For every hour when the shop is closed and customers come, the penalty increases by `1`. Return_ the **earliest** hour at which the shop must be closed to incur a **minimum** penalty._ **Note** that if a shop closes at the `jth` hour, it means the shop is closed at the hour `j`. **Example 1:** ``` **Input:** customers = \"YYNY\" **Output:** 2 **Explanation:** - Closing the shop at the 0th hour incurs in 1+1+0+1 = 3 penalty. - Closing the shop at the 1st hour incurs in 0+1+0+1 = 2 penalty. - Closing the shop at the 2nd hour incurs in 0+0+0+1 = 1 penalty. - Closing the shop at the 3rd hour incurs in 0+0+1+1 = 2 penalty. - Closing the shop at the 4th hour incurs in 0+0+1+0 = 1 penalty. Closing the shop at 2nd or 4th hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2. ``` **Example 2:** ``` **Input:** customers = \"NNNNN\" **Output:** 0 **Explanation:** It is best to close the shop at the 0th hour as no customers arrive. ``` **Example 3:** ``` **Input:** customers = \"YYYY\" **Output:** 4 **Explanation:** It is best to close the shop at the 4th hour as customers arrive at each hour. ``` **Constraints:** `1 <= customers.length <= 105` `customers` consists only of characters `'Y'` and `'N'`.", + "test_case": [ + { + "label": "Example 1", + "input": "customers = \"YYNY\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "customers = \"NNNNN\"", + "output": "0 " + }, + { + "label": "Example 3", + "input": "customers = \"YYYY\"", + "output": "4 " + } + ], + "constraints": [ + "if the ith character is 'Y', it means that customers come at the ith hour", + "whereas 'N' indicates that no customers come at the ith hour.", + "For every hour when the shop is open and no customers come, the penalty increases by 1.", + "For every hour when the shop is closed and customers come, the penalty increases by 1.", + "1 <= customers.length <= 105", + "customers consists only of characters 'Y' and 'N'." + ], + "python_template": "class Solution(object):\n def bestClosingTime(self, customers):\n \"\"\"\n :type customers: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int bestClosingTime(String customers) {\n \n }\n}", + "metadata": { + "func_name": "bestClosingTime" + } +} \ No newline at end of file diff --git a/minimum-processing-time.json b/minimum-processing-time.json new file mode 100644 index 0000000000000000000000000000000000000000..70752a5b795c7776d49e3b09f7e35c8d00c16e68 --- /dev/null +++ b/minimum-processing-time.json @@ -0,0 +1,32 @@ +{ + "id": 3151, + "name": "minimum-processing-time", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-processing-time/", + "date": "2023-10-01", + "task_description": "You have a certain number of processors, each having 4 cores. The number of tasks to be executed is four times the number of processors. Each task must be assigned to a unique core, and each core can only be used once. You are given an array `processorTime` representing the time each processor becomes available and an array `tasks` representing how long each task takes to complete. Return the _minimum_ time needed to complete all tasks. **Example 1:** **Input:** processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5] **Output:** 16 **Explanation:** Assign the tasks at indices 4, 5, 6, 7 to the first processor which becomes available at `time = 8`, and the tasks at indices 0, 1, 2, 3 to the second processor which becomes available at `time = 10`. The time taken by the first processor to finish the execution of all tasks is `max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16`. The time taken by the second processor to finish the execution of all tasks is `max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13`. **Example 2:** **Input:** processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3] **Output:** 23 **Explanation:** Assign the tasks at indices 1, 4, 5, 6 to the first processor and the others to the second processor. The time taken by the first processor to finish the execution of all tasks is `max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18`. The time taken by the second processor to finish the execution of all tasks is `max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23`. **Constraints:** `1 <= n == processorTime.length <= 25000` `1 <= tasks.length <= 105` `0 <= processorTime[i] <= 109` `1 <= tasks[i] <= 109` `tasks.length == 4 * n`", + "test_case": [ + { + "label": "Example 1", + "input": "processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]", + "output": "16 " + }, + { + "label": "Example 2", + "input": "processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]", + "output": "23 " + } + ], + "constraints": [ + "1 <= n == processorTime.length <= 25000", + "1 <= tasks.length <= 105", + "0 <= processorTime[i] <= 109", + "1 <= tasks[i] <= 109", + "tasks.length == 4 * n" + ], + "python_template": "class Solution(object):\n def minProcessingTime(self, processorTime, tasks):\n \"\"\"\n :type processorTime: List[int]\n :type tasks: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minProcessingTime(List processorTime, List tasks) {\n \n }\n}", + "metadata": { + "func_name": "minProcessingTime" + } +} \ No newline at end of file diff --git a/minimum-recolors-to-get-k-consecutive-black-blocks.json b/minimum-recolors-to-get-k-consecutive-black-blocks.json new file mode 100644 index 0000000000000000000000000000000000000000..37e8e869ca88aa515fb9ad4a3ec8f839aa907427 --- /dev/null +++ b/minimum-recolors-to-get-k-consecutive-black-blocks.json @@ -0,0 +1,31 @@ +{ + "id": 2463, + "name": "minimum-recolors-to-get-k-consecutive-black-blocks", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/", + "date": "2022-08-06", + "task_description": "You are given a **0-indexed** string `blocks` of length `n`, where `blocks[i]` is either `'W'` or `'B'`, representing the color of the `ith` block. The characters `'W'` and `'B'` denote the colors white and black, respectively. You are also given an integer `k`, which is the desired number of **consecutive** black blocks. In one operation, you can **recolor** a white block such that it becomes a black block. Return_ the **minimum** number of operations needed such that there is at least **one** occurrence of _`k`_ consecutive black blocks._ **Example 1:** ``` **Input:** blocks = \"WBBWWBBWBW\", k = 7 **Output:** 3 **Explanation:** One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks so that blocks = \"BBBBBBBWBW\". It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations. Therefore, we return 3. ``` **Example 2:** ``` **Input:** blocks = \"WBWBBBW\", k = 2 **Output:** 0 **Explanation:** No changes need to be made, since 2 consecutive black blocks already exist. Therefore, we return 0. ``` **Constraints:** `n == blocks.length` `1 <= n <= 100` `blocks[i]` is either `'W'` or `'B'`. `1 <= k <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "blocks = \"WBBWWBBWBW\", k = 7", + "output": "3 " + }, + { + "label": "Example 2", + "input": "blocks = \"WBWBBBW\", k = 2", + "output": "0 " + } + ], + "constraints": [ + "n == blocks.length", + "1 <= n <= 100", + "blocks[i] is either 'W' or 'B'.", + "1 <= k <= n" + ], + "python_template": "class Solution(object):\n def minimumRecolors(self, blocks, k):\n \"\"\"\n :type blocks: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumRecolors(String blocks, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumRecolors" + } +} \ No newline at end of file diff --git a/minimum-replacements-to-sort-the-array.json b/minimum-replacements-to-sort-the-array.json new file mode 100644 index 0000000000000000000000000000000000000000..0b91b414e9777c679083fa9d4e0733a4ea02232e --- /dev/null +++ b/minimum-replacements-to-sort-the-array.json @@ -0,0 +1,30 @@ +{ + "id": 2450, + "name": "minimum-replacements-to-sort-the-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-replacements-to-sort-the-array/", + "date": "2022-07-23", + "task_description": "You are given a **0-indexed** integer array `nums`. In one operation you can replace any element of the array with **any two** elements that **sum** to it. For example, consider `nums = [5,6,7]`. In one operation, we can replace `nums[1]` with `2` and `4` and convert `nums` to `[5,2,4,7]`. Return _the minimum number of operations to make an array that is sorted in **non-decreasing** order_. **Example 1:** ``` **Input:** nums = [3,9,3] **Output:** 2 **Explanation:** Here are the steps to sort the array in non-decreasing order: - From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3] - From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3] There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4,5] **Output:** 0 **Explanation:** The array is already in non-decreasing order. Therefore, we return 0. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,9,3]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4,5]", + "output": "0 " + } + ], + "constraints": [ + "For example, consider nums = [5,6,7]. In one operation, we can replace nums[1] with 2 and 4 and convert nums to [5,2,4,7].", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minimumReplacement(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumReplacement(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumReplacement" + } +} \ No newline at end of file diff --git a/minimum-reverse-operations.json b/minimum-reverse-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..e43489a9bd22eebe48567f6e7dad38d2aa6769b9 --- /dev/null +++ b/minimum-reverse-operations.json @@ -0,0 +1,47 @@ +{ + "id": 2726, + "name": "minimum-reverse-operations", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-reverse-operations/", + "date": "2023-03-26", + "task_description": "You are given an integer `n` and an integer `p` representing an array `arr` of length `n` where all elements are set to 0's, except position `p` which is set to 1. You are also given an integer array `banned` containing restricted positions. Perform the following operation on `arr`: Reverse a **subarray** with size `k` if the single 1 is not set to a position in `banned`. Return an integer array `answer` with `n` results where the `ith` result is_ _the **minimum** number of operations needed to bring the single 1 to position `i` in `arr`, or -1 if it is impossible. **Example 1:** **Input:** n = 4, p = 0, banned = [1,2], k = 4 **Output:** [0,-1,-1,1] **Explanation:** Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0. We can never place 1 on the banned positions, so the answer for positions 1 and 2 is -1. Perform the operation of size 4 to reverse the whole array. After a single operation 1 is at position 3 so the answer for position 3 is 1. **Example 2:** **Input:** n = 5, p = 0, banned = [2,4], k = 3 **Output:** [0,-1,-1,-1,-1] **Explanation:** Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0. We cannot perform the operation on the subarray positions `[0, 2]` because position 2 is in banned. Because 1 cannot be set at position 2, it is impossible to set 1 at other positions in more operations. **Example 3:** **Input:** n = 4, p = 2, banned = [0,1,3], k = 1 **Output:** [-1,-1,0,-1] **Explanation:** Perform operations of size 1 and 1 never changes its position. **Constraints:** `1 <= n <= 105` `0 <= p <= n - 1` `0 <= banned.length <= n - 1` `0 <= banned[i] <= n - 1` `1 <= k <= n ` `banned[i] != p` all values in `banned` are **unique**", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, p = 0, banned = [1,2], k = 4", + "output": "[0,-1,-1,1] " + }, + { + "label": "Example 2", + "input": "n = 5, p = 0, banned = [2,4], k = 3", + "output": "[0,-1,-1,-1,-1] " + }, + { + "label": "Example 3", + "input": "n = 4, p = 2, banned = [0,1,3], k = 1", + "output": "[-1,-1,0,-1] " + } + ], + "constraints": [ + "Reverse a subarray with size k if the single 1 is not set to a position in banned.", + "Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.", + "We can never place 1 on the banned positions, so the answer for positions 1 and 2 is -1.", + "Perform the operation of size 4 to reverse the whole array.", + "After a single operation 1 is at position 3 so the answer for position 3 is 1.", + "Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.", + "We cannot perform the operation on the subarray positions [0, 2] because position 2 is in banned.", + "Because 1 cannot be set at position 2, it is impossible to set 1 at other positions in more operations.", + "1 <= n <= 105", + "0 <= p <= n - 1", + "0 <= banned.length <= n - 1", + "0 <= banned[i] <= n - 1", + "1 <= k <= n", + "banned[i] != p", + "all values in bannedĀ are unique" + ], + "python_template": "class Solution(object):\n def minReverseOperations(self, n, p, banned, k):\n \"\"\"\n :type n: int\n :type p: int\n :type banned: List[int]\n :type k: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] minReverseOperations(int n, int p, int[] banned, int k) {\n \n }\n}", + "metadata": { + "func_name": "minReverseOperations" + } +} \ No newline at end of file diff --git a/minimum-right-shifts-to-sort-the-array.json b/minimum-right-shifts-to-sort-the-array.json new file mode 100644 index 0000000000000000000000000000000000000000..3ff2d8350d5c8fb0c4dfd6cb8055cf2773310dc4 --- /dev/null +++ b/minimum-right-shifts-to-sort-the-array.json @@ -0,0 +1,35 @@ +{ + "id": 3045, + "name": "minimum-right-shifts-to-sort-the-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array/", + "date": "2023-09-02", + "task_description": "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,2] **Output:** 2 **Explanation:** After the first right shift, nums = [2,3,4,5,1]. After the second right shift, nums = [1,2,3,4,5]. Now nums is sorted; therefore the answer is 2. ``` **Example 2:** ``` **Input:** nums = [1,3,5] **Output:** 0 **Explanation:** nums is already sorted therefore, the answer is 0. ``` **Example 3:** ``` **Input:** nums = [2,1,4] **Output:** -1 **Explanation:** It's impossible to sort the array using right shifts. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100` `nums` contains distinct integers.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,4,5,1,2]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,5]", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [2,1,4]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100", + "nums contains distinct integers." + ], + "python_template": "class Solution(object):\n def minimumRightShifts(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumRightShifts(List nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumRightShifts" + } +} \ No newline at end of file diff --git a/minimum-rounds-to-complete-all-tasks.json b/minimum-rounds-to-complete-all-tasks.json new file mode 100644 index 0000000000000000000000000000000000000000..3e0fc697ec08d37214a61a91c80737a4711bdff8 --- /dev/null +++ b/minimum-rounds-to-complete-all-tasks.json @@ -0,0 +1,29 @@ +{ + "id": 2362, + "name": "minimum-rounds-to-complete-all-tasks", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/", + "date": "2022-04-10", + "task_description": "You are given a **0-indexed** integer array `tasks`, where `tasks[i]` represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the **same difficulty level**. Return _the **minimum** rounds required to complete all the tasks, or _`-1`_ if it is not possible to complete all the tasks._ **Example 1:** ``` **Input:** tasks = [2,2,3,3,2,4,4,4,4,4] **Output:** 4 **Explanation:** To complete all the tasks, a possible plan is: - In the first round, you complete 3 tasks of difficulty level 2. - In the second round, you complete 2 tasks of difficulty level 3. - In the third round, you complete 3 tasks of difficulty level 4. - In the fourth round, you complete 2 tasks of difficulty level 4. It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4. ``` **Example 2:** ``` **Input:** tasks = [2,3,3] **Output:** -1 **Explanation:** There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1. ``` **Constraints:** `1 <= tasks.length <= 105` `1 <= tasks[i] <= 109` **Note:** This question is the same as 2870: Minimum Number of Operations to Make Array Empty.", + "test_case": [ + { + "label": "Example 1", + "input": "tasks = [2,2,3,3,2,4,4,4,4,4]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "tasks = [2,3,3]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= tasks.length <= 105", + "1 <= tasks[i] <= 109" + ], + "python_template": "class Solution(object):\n def minimumRounds(self, tasks):\n \"\"\"\n :type tasks: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumRounds(int[] tasks) {\n \n }\n}", + "metadata": { + "func_name": "minimumRounds" + } +} \ No newline at end of file diff --git a/minimum-score-after-removals-on-a-tree.json b/minimum-score-after-removals-on-a-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..02aa305eb1d4e0c284eb54318ffd6e312cff7de7 --- /dev/null +++ b/minimum-score-after-removals-on-a-tree.json @@ -0,0 +1,36 @@ +{ + "id": 2400, + "name": "minimum-score-after-removals-on-a-tree", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-score-after-removals-on-a-tree/", + "date": "2022-06-19", + "task_description": "There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges. You are given a **0-indexed** integer array `nums` of length `n` where `nums[i]` represents the value of the `ith` node. You are also given 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. Remove two **distinct** edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined: Get the XOR of all the values of the nodes for **each** of the three components respectively. The **difference** between the **largest** XOR value and the **smallest** XOR value is the **score** of the pair. For example, say the three components have the node values: `[4,5,7]`, `[1,9]`, and `[3,3,3]`. The three XOR values are `4 ^ 5 ^ 7 = **6**`, `1 ^ 9 = **8**`, and `3 ^ 3 ^ 3 = **3**`. The largest XOR value is `8` and the smallest XOR value is `3`. The score is then `8 - 3 = 5`. Return _the **minimum** score of any possible pair of edge removals on the given tree_. **Example 1:** ``` **Input:** nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]] **Output:** 9 **Explanation:** The diagram above shows a way to make a pair of removals. - The 1st component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10. - The 2nd component has node [0] with value [1]. Its XOR value is 1 = 1. - The 3rd component has node [2] with value [5]. Its XOR value is 5 = 5. The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9. It can be shown that no other pair of removals will obtain a smaller score than 9. ``` **Example 2:** ``` **Input:** nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]] **Output:** 0 **Explanation:** The diagram above shows a way to make a pair of removals. - The 1st component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 = 0. - The 2nd component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 = 0. - The 3rd component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 = 0. The score is the difference between the largest and smallest XOR value which is 0 - 0 = 0. We cannot obtain a smaller score than 0. ``` **Constraints:** `n == nums.length` `3 <= n <= 1000` `1 <= nums[i] <= 108` `edges.length == n - 1` `edges[i].length == 2` `0 <= ai, bi < n` `ai != bi` `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]", + "output": "0 " + } + ], + "constraints": [ + "For example, say the three components have the node values: [4,5,7], [1,9], and [3,3,3]. The three XOR values are 4 ^ 5 ^ 7 = 6, 1 ^ 9 = 8, and 3 ^ 3 ^ 3 = 3. The largest XOR value is 8 and the smallest XOR value is 3. The score is then 8 - 3 = 5.", + "n == nums.length", + "3 <= n <= 1000", + "1 <= nums[i] <= 108", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ai, bi < n", + "ai != bi", + "edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def minimumScore(self, nums, edges):\n \"\"\"\n :type nums: List[int]\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumScore(int[] nums, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "minimumScore" + } +} \ No newline at end of file diff --git a/minimum-score-by-changing-two-elements.json b/minimum-score-by-changing-two-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..ea8543f88bcdab835c38dbd747aa55f45607590b --- /dev/null +++ b/minimum-score-by-changing-two-elements.json @@ -0,0 +1,38 @@ +{ + "id": 2706, + "name": "minimum-score-by-changing-two-elements", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-score-by-changing-two-elements/", + "date": "2023-02-04", + "task_description": "You are given an integer array `nums`. The **low** score of `nums` is the **minimum** absolute difference between any two integers. The **high** score of `nums` is the **maximum** absolute difference between any two integers. The **score** of `nums` is the sum of the **high** and **low** scores. Return the **minimum score** after **changing two elements** of `nums`. **Example 1:** **Input:** nums = [1,4,7,8,5] **Output:** 3 **Explanation:** Change `nums[0]` and `nums[1]` to be 6 so that `nums` becomes [6,6,7,8,5]. The low score is the minimum absolute difference: |6 - 6| = 0. The high score is the maximum absolute difference: |8 - 5| = 3. The sum of high and low score is 3. **Example 2:** **Input:** nums = [1,4,3] **Output:** 0 **Explanation:** Change `nums[1]` and `nums[2]` to 1 so that `nums` becomes [1,1,1]. The sum of maximum absolute difference and minimum absolute difference is 0. **Constraints:** `3 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,7,8,5]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,4,3]", + "output": "0 " + } + ], + "constraints": [ + "The low score of nums is the minimum absolute difference between any two integers.", + "The high score of nums is the maximum absolute difference between any two integers.", + "The score of nums is the sum of the high and low scores.", + "Change nums[0] and nums[1] to be 6 so that nums becomes [6,6,7,8,5].", + "The low score is the minimum absolute difference: |6 - 6| = 0.", + "The high score is the maximum absolute difference: |8 - 5| = 3.", + "The sum of high and low score is 3.", + "Change nums[1] and nums[2] to 1 so that nums becomes [1,1,1].", + "The sum of maximum absolute difference and minimum absolute difference is 0.", + "3 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minimizeSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimizeSum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimizeSum" + } +} \ No newline at end of file diff --git a/minimum-score-of-a-path-between-two-cities.json b/minimum-score-of-a-path-between-two-cities.json new file mode 100644 index 0000000000000000000000000000000000000000..15d963d2b2782bfd09a76940fa1f5d340091aef1 --- /dev/null +++ b/minimum-score-of-a-path-between-two-cities.json @@ -0,0 +1,38 @@ +{ + "id": 2582, + "name": "minimum-score-of-a-path-between-two-cities", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/", + "date": "2022-11-27", + "task_description": "You are given a positive integer `n` representing `n` cities numbered from `1` to `n`. You are also given a **2D** array `roads` where `roads[i] = [ai, bi, distancei]` indicates that there is a **bidirectional **road between cities `ai` and `bi` with a distance equal to `distancei`. The cities graph is not necessarily connected. The **score** of a path between two cities is defined as the **minimum **distance of a road in this path. Return _the **minimum **possible score of a path between cities _`1`_ and _`n`. **Note**: A path is a sequence of roads between two cities. It is allowed for a path to contain the same road **multiple** times, and you can visit cities `1` and `n` multiple times along the path. The test cases are generated such that there is **at least** one path between `1` and `n`. **Example 1:** ``` **Input:** n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]] **Output:** 5 **Explanation:** The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5. It can be shown that no other path has less score. ``` **Example 2:** ``` **Input:** n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]] **Output:** 2 **Explanation:** The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2. ``` **Constraints:** `2 <= n <= 105` `1 <= roads.length <= 105` `roads[i].length == 3` `1 <= ai, bi <= n` `ai != bi` `1 <= distancei <= 104` There are no repeated edges. There is at least one path between `1` and `n`.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]", + "output": "2 " + } + ], + "constraints": [ + "A path is a sequence of roads between two cities.", + "It is allowed for a path to contain the same road multiple times, and you can visit cities 1 and n multiple times along the path.", + "The test cases are generated such that there is at least one path between 1 and n.", + "2 <= n <= 105", + "1 <= roads.length <= 105", + "roads[i].length == 3", + "1 <= ai, bi <= n", + "ai != bi", + "1 <= distancei <= 104", + "There are no repeated edges.", + "There is at least one path between 1 and n." + ], + "python_template": "class Solution(object):\n def minScore(self, n, roads):\n \"\"\"\n :type n: int\n :type roads: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minScore(int n, int[][] roads) {\n \n }\n}", + "metadata": { + "func_name": "minScore" + } +} \ No newline at end of file diff --git a/minimum-seconds-to-equalize-a-circular-array.json b/minimum-seconds-to-equalize-a-circular-array.json new file mode 100644 index 0000000000000000000000000000000000000000..1c5324b6197507b103d59f07d541bf1f03a83948 --- /dev/null +++ b/minimum-seconds-to-equalize-a-circular-array.json @@ -0,0 +1,35 @@ +{ + "id": 2920, + "name": "minimum-seconds-to-equalize-a-circular-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-seconds-to-equalize-a-circular-array/", + "date": "2023-07-22", + "task_description": "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 _the **minimum** number of seconds needed to make all elements in the array_ `nums` _equal_. **Example 1:** ``` **Input:** nums = [1,2,1,2] **Output:** 1 **Explanation:** We can equalize the array in 1 second in the following way: - At 1st second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2]. It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array. ``` **Example 2:** ``` **Input:** nums = [2,1,3,3,2] **Output:** 2 **Explanation:** We can equalize the array in 2 seconds in the following way: - At 1st second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3]. - At 2nd second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3]. It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array. ``` **Example 3:** ``` **Input:** nums = [5,5,5,5] **Output:** 0 **Explanation:** We don't need to perform any operations as all elements in the initial array are the same. ``` **Constraints:** `1 <= n == nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1,2]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [2,1,3,3,2]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [5,5,5,5]", + "output": "0 " + } + ], + "constraints": [ + "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].", + "1 <= n == nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minimumSeconds(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumSeconds(List nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumSeconds" + } +} \ No newline at end of file diff --git a/minimum-size-subarray-in-infinite-array.json b/minimum-size-subarray-in-infinite-array.json new file mode 100644 index 0000000000000000000000000000000000000000..1bb84e1bbf4d051e98d55db4bdffc198d4ca0e32 --- /dev/null +++ b/minimum-size-subarray-in-infinite-array.json @@ -0,0 +1,35 @@ +{ + "id": 3141, + "name": "minimum-size-subarray-in-infinite-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-size-subarray-in-infinite-array/", + "date": "2023-09-24", + "task_description": "You are given a **0-indexed** array `nums` and an integer `target`. A **0-indexed** array `infinite_nums` is generated by infinitely appending the elements of `nums` to itself. Return _the length of the **shortest** subarray of the array _`infinite_nums`_ with a sum equal to _`target`_._ If there is no such subarray return `-1`. **Example 1:** ``` **Input:** nums = [1,2,3], target = 5 **Output:** 2 **Explanation:** In this example infinite_nums = [1,2,3,1,2,3,1,2,...]. The subarray in the range [1,2], has the sum equal to target = 5 and length = 2. It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5. ``` **Example 2:** ``` **Input:** nums = [1,1,1,2,3], target = 4 **Output:** 2 **Explanation:** In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...]. The subarray in the range [4,5], has the sum equal to target = 4 and length = 2. It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4. ``` **Example 3:** ``` **Input:** nums = [2,4,6,8], target = 3 **Output:** -1 **Explanation:** In this example infinite_nums = [2,4,6,8,2,4,6,8,...]. It can be proven that there is no subarray with sum equal to target = 3. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105` `1 <= target <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3], target = 5", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1,2,3], target = 4", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [2,4,6,8], target = 3", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105", + "1 <= target <= 109" + ], + "python_template": "class Solution(object):\n def minSizeSubarray(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minSizeSubarray(int[] nums, int target) {\n \n }\n}", + "metadata": { + "func_name": "minSizeSubarray" + } +} \ No newline at end of file diff --git a/minimum-string-length-after-removing-substrings.json b/minimum-string-length-after-removing-substrings.json new file mode 100644 index 0000000000000000000000000000000000000000..3e44cbf3d42b74ace4815915a5389fb767668191 --- /dev/null +++ b/minimum-string-length-after-removing-substrings.json @@ -0,0 +1,29 @@ +{ + "id": 2800, + "name": "minimum-string-length-after-removing-substrings", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-string-length-after-removing-substrings/", + "date": "2023-05-14", + "task_description": "You are given a string `s` consisting only of **uppercase** English letters. You can apply some operations to this string where, in one operation, you can remove **any** occurrence of one of the substrings `\"AB\"` or `\"CD\"` from `s`. Return _the **minimum** possible length of the resulting string that you can obtain_. **Note** that the string concatenates after removing the substring and could produce new `\"AB\"` or `\"CD\"` substrings. **Example 1:** ``` **Input:** s = \"ABFCACDB\" **Output:** 2 **Explanation:** We can do the following operations: - Remove the substring \"ABFCACDB\", so s = \"FCACDB\". - Remove the substring \"FCACDB\", so s = \"FCAB\". - Remove the substring \"FCAB\", so s = \"FC\". So the resulting length of the string is 2. It can be shown that it is the minimum length that we can obtain. ``` **Example 2:** ``` **Input:** s = \"ACBBD\" **Output:** 5 **Explanation:** We cannot do any operations on the string so the length remains the same. ``` **Constraints:** `1 <= s.length <= 100` `s` consists only of uppercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"ABFCACDB\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"ACBBD\"", + "output": "5 " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "sĀ consists only of uppercase English letters." + ], + "python_template": "class Solution(object):\n def minLength(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minLength(String s) {\n \n }\n}", + "metadata": { + "func_name": "minLength" + } +} \ No newline at end of file diff --git a/minimum-substring-partition-of-equal-character-frequency.json b/minimum-substring-partition-of-equal-character-frequency.json new file mode 100644 index 0000000000000000000000000000000000000000..c09275644160e133b31f400870cbd3979b298b92 --- /dev/null +++ b/minimum-substring-partition-of-equal-character-frequency.json @@ -0,0 +1,29 @@ +{ + "id": 3403, + "name": "minimum-substring-partition-of-equal-character-frequency", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-substring-partition-of-equal-character-frequency/", + "date": "2024-04-27", + "task_description": "Given a string `s`, you need to partition it into one or more **balanced** substrings. For example, if `s == \"ababcc\"` then `(\"abab\", \"c\", \"c\")`, `(\"ab\", \"abc\", \"c\")`, and `(\"ababcc\")` are all valid partitions, but `(\"a\", **\"bab\"**, \"cc\")`, `(**\"aba\"**, \"bc\", \"c\")`, and `(\"ab\", **\"abcc\"**)` are not. The unbalanced substrings are bolded. Return the **minimum** number of substrings that you can partition `s` into. **Note:** A **balanced** string is a string where each character in the string occurs the same number of times. **Example 1:** **Input:** s = \"fabccddg\" **Output:** 3 **Explanation:** We can partition the string `s` into 3 substrings in one of the following ways: `(\"fab, \"ccdd\", \"g\")`, or `(\"fabc\", \"cd\", \"dg\")`. **Example 2:** **Input:** s = \"abababaccddb\" **Output:** 2 **Explanation:** We can partition the string `s` into 2 substrings like so: `(\"abab\", \"abaccddb\")`. **Constraints:** `1 <= s.length <= 1000` `s` consists only of English lowercase letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"fabccddg\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \"abababaccddb\"", + "output": "2 " + } + ], + "constraints": [ + "1 <= s.length <= 1000", + "s consists only of English lowercase letters." + ], + "python_template": "class Solution(object):\n def minimumSubstringsInPartition(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumSubstringsInPartition(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimumSubstringsInPartition" + } +} \ No newline at end of file diff --git a/minimum-sum-of-mountain-triplets-i.json b/minimum-sum-of-mountain-triplets-i.json new file mode 100644 index 0000000000000000000000000000000000000000..e495407bcf7c7d40790c7c8c1ed19c955e74d112 --- /dev/null +++ b/minimum-sum-of-mountain-triplets-i.json @@ -0,0 +1,36 @@ +{ + "id": 3176, + "name": "minimum-sum-of-mountain-triplets-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-sum-of-mountain-triplets-i/", + "date": "2023-10-15", + "task_description": "You are given a **0-indexed** array `nums` of integers. A triplet of indices `(i, j, k)` is a **mountain** if: `i < j < k` `nums[i] < nums[j]` and `nums[k] < nums[j]` Return _the **minimum possible sum** of a mountain triplet of_ `nums`. _If no such triplet exists, return_ `-1`. **Example 1:** ``` **Input:** nums = [8,6,1,5,3] **Output:** 9 **Explanation:** Triplet (2, 3, 4) is a mountain triplet of sum 9 since: - 2 < 3 < 4 - nums[2] < nums[3] and nums[4] < nums[3] And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9. ``` **Example 2:** ``` **Input:** nums = [5,4,8,7,10,2] **Output:** 13 **Explanation:** Triplet (1, 3, 5) is a mountain triplet of sum 13 since: - 1 < 3 < 5 - nums[1] < nums[3] and nums[5] < nums[3] And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13. ``` **Example 3:** ``` **Input:** nums = [6,5,4,3,4,5] **Output:** -1 **Explanation:** It can be shown that there are no mountain triplets in nums. ``` **Constraints:** `3 <= nums.length <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [8,6,1,5,3]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "nums = [5,4,8,7,10,2]", + "output": "13 " + }, + { + "label": "Example 3", + "input": "nums = [6,5,4,3,4,5]", + "output": "-1 " + } + ], + "constraints": [ + "i < j < k", + "nums[i] < nums[j] and nums[k] < nums[j]", + "3 <= nums.length <= 50", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def minimumSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumSum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumSum" + } +} \ No newline at end of file diff --git a/minimum-sum-of-mountain-triplets-ii.json b/minimum-sum-of-mountain-triplets-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..541707245888e4e350daab66fd928f0b6182cb4f --- /dev/null +++ b/minimum-sum-of-mountain-triplets-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3186, + "name": "minimum-sum-of-mountain-triplets-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-sum-of-mountain-triplets-ii/", + "date": "2023-10-15", + "task_description": "You are given a **0-indexed** array `nums` of integers. A triplet of indices `(i, j, k)` is a **mountain** if: `i < j < k` `nums[i] < nums[j]` and `nums[k] < nums[j]` Return _the **minimum possible sum** of a mountain triplet of_ `nums`. _If no such triplet exists, return_ `-1`. **Example 1:** ``` **Input:** nums = [8,6,1,5,3] **Output:** 9 **Explanation:** Triplet (2, 3, 4) is a mountain triplet of sum 9 since: - 2 < 3 < 4 - nums[2] < nums[3] and nums[4] < nums[3] And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9. ``` **Example 2:** ``` **Input:** nums = [5,4,8,7,10,2] **Output:** 13 **Explanation:** Triplet (1, 3, 5) is a mountain triplet of sum 13 since: - 1 < 3 < 5 - nums[1] < nums[3] and nums[5] < nums[3] And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13. ``` **Example 3:** ``` **Input:** nums = [6,5,4,3,4,5] **Output:** -1 **Explanation:** It can be shown that there are no mountain triplets in nums. ``` **Constraints:** `3 <= nums.length <= 105` `1 <= nums[i] <= 108`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [8,6,1,5,3]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "nums = [5,4,8,7,10,2]", + "output": "13 " + }, + { + "label": "Example 3", + "input": "nums = [6,5,4,3,4,5]", + "output": "-1 " + } + ], + "constraints": [ + "i < j < k", + "nums[i] < nums[j] and nums[k] < nums[j]", + "3 <= nums.length <= 105", + "1 <= nums[i] <= 108" + ], + "python_template": "class Solution(object):\n def minimumSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumSum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumSum" + } +} \ No newline at end of file diff --git a/minimum-sum-of-squared-difference.json b/minimum-sum-of-squared-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..5e4fbe601d6ba4605804527d52ed842f2030e6af --- /dev/null +++ b/minimum-sum-of-squared-difference.json @@ -0,0 +1,31 @@ +{ + "id": 2418, + "name": "minimum-sum-of-squared-difference", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-sum-of-squared-difference/", + "date": "2022-06-25", + "task_description": "You are given two positive **0-indexed** integer arrays `nums1` and `nums2`, both of length `n`. The **sum of squared difference** of arrays `nums1` and `nums2` is defined as the **sum** of `(nums1[i] - nums2[i])2` for each `0 <= i < n`. You are also given two positive integers `k1` and `k2`. You can modify any of the elements of `nums1` by `+1` or `-1` at most `k1` times. Similarly, you can modify any of the elements of `nums2` by `+1` or `-1` at most `k2` times. Return _the minimum **sum of squared difference** after modifying array _`nums1`_ at most _`k1`_ times and modifying array _`nums2`_ at most _`k2`_ times_. **Note**: You are allowed to modify the array elements to become **negative** integers. **Example 1:** ``` **Input:** nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0 **Output:** 579 **Explanation:** The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0. The sum of square difference will be: (1 - 2)2 + (2 - 10)2 + (3 - 20)2 + (4 - 19)2 = 579. ``` **Example 2:** ``` **Input:** nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1 **Output:** 43 **Explanation:** One way to obtain the minimum sum of square difference is: - Increase nums1[0] once. - Increase nums2[2] once. The minimum of the sum of square difference will be: (2 - 5)2 + (4 - 8)2 + (10 - 7)2 + (12 - 9)2 = 43. Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43. ``` **Constraints:** `n == nums1.length == nums2.length` `1 <= n <= 105` `0 <= nums1[i], nums2[i] <= 105` `0 <= k1, k2 <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0", + "output": "579 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1", + "output": "43 " + } + ], + "constraints": [ + "n == nums1.length == nums2.length", + "1 <= n <= 105", + "0 <= nums1[i], nums2[i] <= 105", + "0 <= k1, k2 <= 109" + ], + "python_template": "class Solution(object):\n def minSumSquareDiff(self, nums1, nums2, k1, k2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k1: int\n :type k2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minSumSquareDiff(int[] nums1, int[] nums2, int k1, int k2) {\n \n }\n}", + "metadata": { + "func_name": "minSumSquareDiff" + } +} \ No newline at end of file diff --git a/minimum-sum-of-values-by-dividing-array.json b/minimum-sum-of-values-by-dividing-array.json new file mode 100644 index 0000000000000000000000000000000000000000..210c5be8842fa12c660f5d53f4cbfc29d646f2cb --- /dev/null +++ b/minimum-sum-of-values-by-dividing-array.json @@ -0,0 +1,36 @@ +{ + "id": 3364, + "name": "minimum-sum-of-values-by-dividing-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-sum-of-values-by-dividing-array/", + "date": "2024-04-07", + "task_description": "You are given two arrays `nums` and `andValues` of length `n` and `m` respectively. The **value** of an array is equal to the **last** element of that array. You have to divide `nums` into `m` **disjoint contiguous** subarrays such that for the `ith` subarray `[li, ri]`, the bitwise `AND` of the subarray elements is equal to `andValues[i]`, in other words, `nums[li] & nums[li + 1] & ... & nums[ri] == andValues[i]` for all `1 <= i <= m`, where `&` represents the bitwise `AND` operator. Return _the **minimum** possible sum of the **values** of the _`m`_ subarrays _`nums`_ is divided into_. _If it is not possible to divide _`nums`_ into _`m`_ subarrays satisfying these conditions, return_ `-1`. **Example 1:** **Input:** nums = [1,4,3,3,2], andValues = [0,3,3,2] **Output:** 12 **Explanation:** The only possible way to divide `nums` is: `[1,4]` as `1 & 4 == 0`. `[3]` as the bitwise `AND` of a single element subarray is that element itself. `[3]` as the bitwise `AND` of a single element subarray is that element itself. `[2]` as the bitwise `AND` of a single element subarray is that element itself. The sum of the values for these subarrays is `4 + 3 + 3 + 2 = 12`. **Example 2:** **Input:** nums = [2,3,5,7,7,7,5], andValues = [0,7,5] **Output:** 17 **Explanation:** There are three ways to divide `nums`: `[[2,3,5],[7,7,7],[5]]` with the sum of the values `5 + 7 + 5 == 17`. `[[2,3,5,7],[7,7],[5]]` with the sum of the values `7 + 7 + 5 == 19`. `[[2,3,5,7,7],[7],[5]]` with the sum of the values `7 + 7 + 5 == 19`. The minimum possible sum of the values is `17`. **Example 3:** **Input:** nums = [1,2,3,4], andValues = [2] **Output:** -1 **Explanation:** The bitwise `AND` of the entire array `nums` is `0`. As there is no possible way to divide `nums` into a single subarray to have the bitwise `AND` of elements `2`, return `-1`. **Constraints:** `1 <= n == nums.length <= 104` `1 <= m == andValues.length <= min(n, 10)` `1 <= nums[i] < 105` `0 <= andValues[j] < 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,3,3,2], andValues = [0,3,3,2]", + "output": "12 " + }, + { + "label": "Example 2", + "input": "nums = [2,3,5,7,7,7,5], andValues = [0,7,5]", + "output": "17 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3,4], andValues = [2]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= n == nums.length <= 104", + "1 <= m == andValues.length <= min(n, 10)", + "1 <= nums[i] < 105", + "0 <= andValues[j] < 105" + ], + "python_template": "class Solution(object):\n def minimumValueSum(self, nums, andValues):\n \"\"\"\n :type nums: List[int]\n :type andValues: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumValueSum(int[] nums, int[] andValues) {\n \n }\n}", + "metadata": { + "func_name": "minimumValueSum" + } +} \ No newline at end of file diff --git a/minimum-swaps-to-group-all-1s-together-ii.json b/minimum-swaps-to-group-all-1s-together-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..48f099f9dc63270f074da58b93ac3a3c0a73de11 --- /dev/null +++ b/minimum-swaps-to-group-all-1s-together-ii.json @@ -0,0 +1,34 @@ +{ + "id": 2255, + "name": "minimum-swaps-to-group-all-1s-together-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/", + "date": "2022-01-02", + "task_description": "A **swap** is defined as taking two **distinct** positions in an array and swapping the values in them. A **circular** array is defined as an array where we consider the **first** element and the **last** element to be **adjacent**. Given a **binary** **circular** array `nums`, return _the minimum number of swaps required to group all _`1`_'s present in the array together at **any location**_. **Example 1:** ``` **Input:** nums = [0,1,0,1,1,0,0] **Output:** 1 **Explanation:** Here are a few of the ways to group all the 1's together: [0,0,1,1,1,0,0] using 1 swap. [0,1,1,1,0,0,0] using 1 swap. [1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array). There is no way to group all 1's together with 0 swaps. Thus, the minimum number of swaps required is 1. ``` **Example 2:** ``` **Input:** nums = [0,1,1,1,0,0,1,1,0] **Output:** 2 **Explanation:** Here are a few of the ways to group all the 1's together: [1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array). [1,1,1,1,1,0,0,0,0] using 2 swaps. There is no way to group all 1's together with 0 or 1 swaps. Thus, the minimum number of swaps required is 2. ``` **Example 3:** ``` **Input:** nums = [1,1,0,0,1] **Output:** 0 **Explanation:** All the 1's are already grouped together due to the circular property of the array. Thus, the minimum number of swaps required is 0. ``` **Constraints:** `1 <= nums.length <= 105` `nums[i]` is either `0` or `1`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,0,1,1,0,0]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [0,1,1,1,0,0,1,1,0]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [1,1,0,0,1]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "nums[i] is either 0 or 1." + ], + "python_template": "class Solution(object):\n def minSwaps(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minSwaps(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minSwaps" + } +} \ No newline at end of file diff --git a/minimum-time-to-break-locks-i.json b/minimum-time-to-break-locks-i.json new file mode 100644 index 0000000000000000000000000000000000000000..f20bc5d368fbbd2d9179cb851d32ea1d42b90fa6 --- /dev/null +++ b/minimum-time-to-break-locks-i.json @@ -0,0 +1,36 @@ +{ + "id": 3649, + "name": "minimum-time-to-break-locks-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-time-to-break-locks-i/", + "date": "2024-11-23", + "task_description": "Bob is stuck in a dungeon and must break `n` locks, each requiring some amount of **energy** to break. The required energy for each lock is stored in an array called `strength` where `strength[i]` indicates the energy needed to break the `ith` lock. To break a lock, Bob uses a sword with the following characteristics: The initial energy of the sword is 0. The initial factor `x` by which the energy of the sword increases is 1. Every minute, the energy of the sword increases by the current factor `x`. To break the `ith` lock, the energy of the sword must reach **at least** `strength[i]`. After breaking a lock, the energy of the sword resets to 0, and the factor `x` increases by a given value `k`. Your task is to determine the **minimum** time in minutes required for Bob to break all `n` locks and escape the dungeon. Return the **minimum **time required for Bob to break all `n` locks. **Example 1:** **Input:** strength = [3,4,1], k = 1 **Output:** 4 **Explanation:** Time Energy x Action Updated x 0 0 1 Nothing 1 1 1 1 Break 3rd Lock 2 2 2 2 Nothing 2 3 4 2 Break 2nd Lock 3 4 3 3 Break 1st Lock 3 The locks cannot be broken in less than 4 minutes; thus, the answer is 4. **Example 2:** **Input:** strength = [2,5,4], k = 2 **Output:** 5 **Explanation:** Time Energy x Action Updated x 0 0 1 Nothing 1 1 1 1 Nothing 1 2 2 1 Break 1st Lock 3 3 3 3 Nothing 3 4 6 3 Break 2nd Lock 5 5 5 5 Break 3rd Lock 7 The locks cannot be broken in less than 5 minutes; thus, the answer is 5. **Constraints:** `n == strength.length` `1 <= n <= 8` `1 <= K <= 10` `1 <= strength[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "strength = [3,4,1], k = 1", + "output": "4 " + }, + { + "label": "Example 2", + "input": "strength = [2,5,4], k = 2", + "output": "5 " + } + ], + "constraints": [ + "The initial energy of the sword is 0.", + "The initial factor x by which the energy of the sword increases is 1.", + "Every minute, the energy of the sword increases by the current factor x.", + "To break the ith lock, the energy of the sword must reach at least strength[i].", + "After breaking a lock, the energy of the sword resets to 0, and the factor x increases by a given value k.", + "n == strength.length", + "1 <= n <= 8", + "1 <= K <= 10", + "1 <= strength[i] <= 106" + ], + "python_template": "class Solution(object):\n def findMinimumTime(self, strength, k):\n \"\"\"\n :type strength: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findMinimumTime(List strength, int k) {\n \n }\n}", + "metadata": { + "func_name": "findMinimumTime" + } +} \ No newline at end of file diff --git a/minimum-time-to-complete-all-tasks.json b/minimum-time-to-complete-all-tasks.json new file mode 100644 index 0000000000000000000000000000000000000000..f4e297b5d6eb79da7432a7f72855512562fece6f --- /dev/null +++ b/minimum-time-to-complete-all-tasks.json @@ -0,0 +1,31 @@ +{ + "id": 2657, + "name": "minimum-time-to-complete-all-tasks", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-time-to-complete-all-tasks/", + "date": "2023-03-05", + "task_description": "There is a computer that can run an unlimited number of tasks **at the same time**. You are given a 2D integer array `tasks` where `tasks[i] = [starti, endi, durationi]` indicates that the `ith` task should run for a total of `durationi` seconds (not necessarily continuous) within the **inclusive** time range `[starti, endi]`. You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle. Return _the minimum time during which the computer should be turned on to complete all tasks_. **Example 1:** ``` **Input:** tasks = [[2,3,1],[4,5,1],[1,5,2]] **Output:** 2 **Explanation:** - The first task can be run in the inclusive time range [2, 2]. - The second task can be run in the inclusive time range [5, 5]. - The third task can be run in the two inclusive time ranges [2, 2] and [5, 5]. The computer will be on for a total of 2 seconds. ``` **Example 2:** ``` **Input:** tasks = [[1,3,2],[2,5,3],[5,6,2]] **Output:** 4 **Explanation:** - The first task can be run in the inclusive time range [2, 3]. - The second task can be run in the inclusive time ranges [2, 3] and [5, 5]. - The third task can be run in the two inclusive time range [5, 6]. The computer will be on for a total of 4 seconds. ``` **Constraints:** `1 <= tasks.length <= 2000` `tasks[i].length == 3` `1 <= starti, endi <= 2000` `1 <= durationi <= endi - starti + 1 `", + "test_case": [ + { + "label": "Example 1", + "input": "tasks = [[2,3,1],[4,5,1],[1,5,2]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "tasks = [[1,3,2],[2,5,3],[5,6,2]]", + "output": "4 " + } + ], + "constraints": [ + "1 <= tasks.length <= 2000", + "tasks[i].length == 3", + "1 <= starti, endi <= 2000", + "1 <= durationi <= endi - starti + 1" + ], + "python_template": "class Solution(object):\n def findMinimumTime(self, tasks):\n \"\"\"\n :type tasks: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findMinimumTime(int[][] tasks) {\n \n }\n}", + "metadata": { + "func_name": "findMinimumTime" + } +} \ No newline at end of file diff --git a/minimum-time-to-complete-trips.json b/minimum-time-to-complete-trips.json new file mode 100644 index 0000000000000000000000000000000000000000..e45198add683c83dd582b0ee6ae9d9ff0926cfee --- /dev/null +++ b/minimum-time-to-complete-trips.json @@ -0,0 +1,29 @@ +{ + "id": 2294, + "name": "minimum-time-to-complete-trips", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-time-to-complete-trips/", + "date": "2022-02-20", + "task_description": "You are given an array `time` where `time[i]` denotes the time taken by the `ith` bus to complete **one trip**. Each bus can make multiple trips **successively**; that is, the next trip can start **immediately after** completing the current trip. Also, each bus operates **independently**; that is, the trips of one bus do not influence the trips of any other bus. You are also given an integer `totalTrips`, which denotes the number of trips all buses should make **in total**. Return _the **minimum time** required for all buses to complete **at least** _`totalTrips`_ trips_. **Example 1:** ``` **Input:** time = [1,2,3], totalTrips = 5 **Output:** 3 **Explanation:** - At time t = 1, the number of trips completed by each bus are [1,0,0]. The total number of trips completed is 1 + 0 + 0 = 1. - At time t = 2, the number of trips completed by each bus are [2,1,0]. The total number of trips completed is 2 + 1 + 0 = 3. - At time t = 3, the number of trips completed by each bus are [3,1,1]. The total number of trips completed is 3 + 1 + 1 = 5. So the minimum time needed for all buses to complete at least 5 trips is 3. ``` **Example 2:** ``` **Input:** time = [2], totalTrips = 1 **Output:** 2 **Explanation:** There is only one bus, and it will complete its first trip at t = 2. So the minimum time needed to complete 1 trip is 2. ``` **Constraints:** `1 <= time.length <= 105` `1 <= time[i], totalTrips <= 107`", + "test_case": [ + { + "label": "Example 1", + "input": "time = [1,2,3], totalTrips = 5", + "output": "3 " + }, + { + "label": "Example 2", + "input": "time = [2], totalTrips = 1", + "output": "2 " + } + ], + "constraints": [ + "1 <= time.length <= 105", + "1 <= time[i], totalTrips <= 107" + ], + "python_template": "class Solution(object):\n def minimumTime(self, time, totalTrips):\n \"\"\"\n :type time: List[int]\n :type totalTrips: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumTime(int[] time, int totalTrips) {\n \n }\n}", + "metadata": { + "func_name": "minimumTime" + } +} \ No newline at end of file diff --git a/minimum-time-to-make-array-sum-at-most-x.json b/minimum-time-to-make-array-sum-at-most-x.json new file mode 100644 index 0000000000000000000000000000000000000000..4447f206bf171e8b44dbcf0435983e46a9edb10e --- /dev/null +++ b/minimum-time-to-make-array-sum-at-most-x.json @@ -0,0 +1,33 @@ +{ + "id": 2952, + "name": "minimum-time-to-make-array-sum-at-most-x", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-time-to-make-array-sum-at-most-x/", + "date": "2023-07-22", + "task_description": "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 integer `x`. Return _the **minimum** time in which you can make the sum of all elements of _`nums1`_ to be** less than or equal** to _`x`, _or _`-1`_ if this is not possible._ **Example 1:** ``` **Input:** nums1 = [1,2,3], nums2 = [1,2,3], x = 4 **Output:** 3 **Explanation:** For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6]. For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9]. For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0]. Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3. ``` **Example 2:** ``` **Input:** nums1 = [1,2,3], nums2 = [3,3,3], x = 4 **Output:** -1 **Explanation:** It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed. ``` **Constraints:** `1 <= nums1.length <= 103` `1 <= nums1[i] <= 103` `0 <= nums2[i] <= 103` `nums1.length == nums2.length` `0 <= x <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,2,3], nums2 = [1,2,3], x = 4", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,2,3], nums2 = [3,3,3], x = 4", + "output": "-1 " + } + ], + "constraints": [ + "Choose an index 0 <= i < nums1.length and make nums1[i] = 0.", + "1 <= nums1.length <= 103", + "1 <= nums1[i] <= 103", + "0 <= nums2[i] <= 103", + "nums1.length == nums2.length", + "0 <= x <= 106" + ], + "python_template": "class Solution(object):\n def minimumTime(self, nums1, nums2, x):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type x: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumTime(List nums1, List nums2, int x) {\n \n }\n}", + "metadata": { + "func_name": "minimumTime" + } +} \ No newline at end of file diff --git a/minimum-time-to-remove-all-cars-containing-illegal-goods.json b/minimum-time-to-remove-all-cars-containing-illegal-goods.json new file mode 100644 index 0000000000000000000000000000000000000000..603bf5634d1b90bd5e7535c3689cab60cc0c4fdf --- /dev/null +++ b/minimum-time-to-remove-all-cars-containing-illegal-goods.json @@ -0,0 +1,29 @@ +{ + "id": 2286, + "name": "minimum-time-to-remove-all-cars-containing-illegal-goods", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/", + "date": "2022-01-30", + "task_description": "You are given a **0-indexed** binary string `s` which represents a sequence of train cars. `s[i] = '0'` denotes that the `ith` car does **not** contain illegal goods and `s[i] = '1'` denotes that the `ith` car does contain illegal goods. As the train conductor, you would like to get rid of all the cars containing illegal goods. You can do any of the following three operations **any** number of times: Remove a train car from the **left** end (i.e., remove `s[0]`) which takes 1 unit of time. Remove a train car from the **right** end (i.e., remove `s[s.length - 1]`) which takes 1 unit of time. Remove a train car from **anywhere** in the sequence which takes 2 units of time. Return _the **minimum** time to remove all the cars containing illegal goods_. Note that an empty sequence of cars is considered to have no cars containing illegal goods. **Example 1:** ``` **Input:** s = \"**11**00**1**0**1**\" **Output:** 5 **Explanation:** One way to remove all the cars containing illegal goods from the sequence is to - remove a car from the left end 2 times. Time taken is 2 * 1 = 2. - remove a car from the right end. Time taken is 1. - remove the car containing illegal goods found in the middle. Time taken is 2. This obtains a total time of 2 + 1 + 2 = 5. An alternative way is to - remove a car from the left end 2 times. Time taken is 2 * 1 = 2. - remove a car from the right end 3 times. Time taken is 3 * 1 = 3. This also obtains a total time of 2 + 3 = 5. 5 is the minimum time taken to remove all the cars containing illegal goods. There are no other ways to remove them with less time. ``` **Example 2:** ``` **Input:** s = \"00**1**0\" **Output:** 2 **Explanation:** One way to remove all the cars containing illegal goods from the sequence is to - remove a car from the left end 3 times. Time taken is 3 * 1 = 3. This obtains a total time of 3. Another way to remove all the cars containing illegal goods from the sequence is to - remove the car containing illegal goods found in the middle. Time taken is 2. This obtains a total time of 2. Another way to remove all the cars containing illegal goods from the sequence is to - remove a car from the right end 2 times. Time taken is 2 * 1 = 2. This obtains a total time of 2. 2 is the minimum time taken to remove all the cars containing illegal goods. There are no other ways to remove them with less time. ``` **Constraints:** `1 <= s.length <= 2 * 105` `s[i]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \" 11 00 1 0 1 \"", + "output": "5 " + }, + { + "label": "Example 2", + "input": "s = \"00 1 0\"", + "output": "2 " + } + ], + "constraints": [ + "1 <= s.length <= 2 * 105", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def minimumTime(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumTime(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimumTime" + } +} \ No newline at end of file diff --git a/minimum-time-to-repair-cars.json b/minimum-time-to-repair-cars.json new file mode 100644 index 0000000000000000000000000000000000000000..cc87efd2ae0a75ab955817dd65747bca10b1cb93 --- /dev/null +++ b/minimum-time-to-repair-cars.json @@ -0,0 +1,30 @@ +{ + "id": 2665, + "name": "minimum-time-to-repair-cars", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-time-to-repair-cars/", + "date": "2023-03-04", + "task_description": "You are given an integer array `ranks` representing the **ranks** of some mechanics. ranksi is the rank of the ith mechanic. A mechanic with a rank `r` can repair n cars in `r * n2` minutes. You are also given an integer `cars` representing the total number of cars waiting in the garage to be repaired. Return _the **minimum** time taken to repair all the cars._ **Note:** All the mechanics can repair the cars simultaneously. **Example 1:** ``` **Input:** ranks = [4,2,3,1], cars = 10 **Output:** 16 **Explanation:** - The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes. - The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes. - The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes. - The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes. It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​ ``` **Example 2:** ``` **Input:** ranks = [5,1,8], cars = 6 **Output:** 16 **Explanation:** - The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes. - The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes. - The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes. It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​ ``` **Constraints:** `1 <= ranks.length <= 105` `1 <= ranks[i] <= 100` `1 <= cars <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "ranks = [4,2,3,1], cars = 10", + "output": "16 " + }, + { + "label": "Example 2", + "input": "ranks = [5,1,8], cars = 6", + "output": "16 " + } + ], + "constraints": [ + "1 <= ranks.length <= 105", + "1 <= ranks[i] <= 100", + "1 <= cars <= 106" + ], + "python_template": "class Solution(object):\n def repairCars(self, ranks, cars):\n \"\"\"\n :type ranks: List[int]\n :type cars: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long repairCars(int[] ranks, int cars) {\n \n }\n}", + "metadata": { + "func_name": "repairCars" + } +} \ No newline at end of file diff --git a/minimum-time-to-revert-word-to-initial-state-i.json b/minimum-time-to-revert-word-to-initial-state-i.json new file mode 100644 index 0000000000000000000000000000000000000000..e993105312da38107c9ef9e2c4b29b312c74e5b8 --- /dev/null +++ b/minimum-time-to-revert-word-to-initial-state-i.json @@ -0,0 +1,37 @@ +{ + "id": 3297, + "name": "minimum-time-to-revert-word-to-initial-state-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-i/", + "date": "2024-01-28", + "task_description": "You are given a **0-indexed** string `word` and an integer `k`. At every second, you must perform the following operations: Remove the first `k` characters of `word`. Add any `k` characters to the end of `word`. **Note** that you do not necessarily need to add the same characters that you removed. However, you must perform **both** operations at every second. Return _the **minimum** time greater than zero required for_ `word` _to revert to its **initial** state_. **Example 1:** ``` **Input:** word = \"abacaba\", k = 3 **Output:** 2 **Explanation:** At the 1st second, we remove characters \"aba\" from the prefix of word, and add characters \"bac\" to the end of word. Thus, word becomes equal to \"cababac\". At the 2nd second, we remove characters \"cab\" from the prefix of word, and add \"aba\" to the end of word. Thus, word becomes equal to \"abacaba\" and reverts to its initial state. It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state. ``` **Example 2:** ``` **Input:** word = \"abacaba\", k = 4 **Output:** 1 **Explanation:** At the 1st second, we remove characters \"abac\" from the prefix of word, and add characters \"caba\" to the end of word. Thus, word becomes equal to \"abacaba\" and reverts to its initial state. It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state. ``` **Example 3:** ``` **Input:** word = \"abcbabcd\", k = 2 **Output:** 4 **Explanation:** At every second, we will remove the first 2 characters of word, and add the same characters to the end of word. After 4 seconds, word becomes equal to \"abcbabcd\" and reverts to its initial state. It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state. ``` **Constraints:** `1 <= word.length <= 50 ` `1 <= k <= word.length` `word` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"abacaba\", k = 3", + "output": "2 " + }, + { + "label": "Example 2", + "input": "word = \"abacaba\", k = 4", + "output": "1 " + }, + { + "label": "Example 3", + "input": "word = \"abcbabcd\", k = 2", + "output": "4 " + } + ], + "constraints": [ + "Remove the first k characters of word.", + "Add any k characters to the end of word.", + "1 <= word.length <= 50", + "1 <= k <= word.length", + "word consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumTimeToInitialState(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumTimeToInitialState(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumTimeToInitialState" + } +} \ No newline at end of file diff --git a/minimum-time-to-revert-word-to-initial-state-ii.json b/minimum-time-to-revert-word-to-initial-state-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..8e9ddc55b135e1509815621c1c668274e61d2d8f --- /dev/null +++ b/minimum-time-to-revert-word-to-initial-state-ii.json @@ -0,0 +1,37 @@ +{ + "id": 3296, + "name": "minimum-time-to-revert-word-to-initial-state-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-ii/", + "date": "2024-01-28", + "task_description": "You are given a **0-indexed** string `word` and an integer `k`. At every second, you must perform the following operations: Remove the first `k` characters of `word`. Add any `k` characters to the end of `word`. **Note** that you do not necessarily need to add the same characters that you removed. However, you must perform **both** operations at every second. Return _the **minimum** time greater than zero required for_ `word` _to revert to its **initial** state_. **Example 1:** ``` **Input:** word = \"abacaba\", k = 3 **Output:** 2 **Explanation:** At the 1st second, we remove characters \"aba\" from the prefix of word, and add characters \"bac\" to the end of word. Thus, word becomes equal to \"cababac\". At the 2nd second, we remove characters \"cab\" from the prefix of word, and add \"aba\" to the end of word. Thus, word becomes equal to \"abacaba\" and reverts to its initial state. It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state. ``` **Example 2:** ``` **Input:** word = \"abacaba\", k = 4 **Output:** 1 **Explanation:** At the 1st second, we remove characters \"abac\" from the prefix of word, and add characters \"caba\" to the end of word. Thus, word becomes equal to \"abacaba\" and reverts to its initial state. It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state. ``` **Example 3:** ``` **Input:** word = \"abcbabcd\", k = 2 **Output:** 4 **Explanation:** At every second, we will remove the first 2 characters of word, and add the same characters to the end of word. After 4 seconds, word becomes equal to \"abcbabcd\" and reverts to its initial state. It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state. ``` **Constraints:** `1 <= word.length <= 106` `1 <= k <= word.length` `word` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"abacaba\", k = 3", + "output": "2 " + }, + { + "label": "Example 2", + "input": "word = \"abacaba\", k = 4", + "output": "1 " + }, + { + "label": "Example 3", + "input": "word = \"abcbabcd\", k = 2", + "output": "4 " + } + ], + "constraints": [ + "Remove the first k characters of word.", + "Add any k characters to the end of word.", + "1 <= word.length <= 106", + "1 <= k <= word.length", + "word consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumTimeToInitialState(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumTimeToInitialState(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumTimeToInitialState" + } +} \ No newline at end of file diff --git a/minimum-time-to-visit-disappearing-nodes.json b/minimum-time-to-visit-disappearing-nodes.json new file mode 100644 index 0000000000000000000000000000000000000000..1a5c98143f778117565f5f89ef738edf50bd17c6 --- /dev/null +++ b/minimum-time-to-visit-disappearing-nodes.json @@ -0,0 +1,45 @@ +{ + "id": 3389, + "name": "minimum-time-to-visit-disappearing-nodes", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-time-to-visit-disappearing-nodes/", + "date": "2024-03-30", + "task_description": "There is an undirected graph of `n` nodes. You are given a 2D array `edges`, where `edges[i] = [ui, vi, lengthi]` describes an edge between node `ui` and node `vi` with a traversal time of `lengthi` units. Additionally, you are given an array `disappear`, where `disappear[i]` denotes the time when the node `i` disappears from the graph and you won't be able to visit it. **Note** that the graph might be _disconnected_ and might contain _multiple edges_. Return the array `answer`, with `answer[i]` denoting the **minimum** units of time required to reach node `i` from node 0. If node `i` is **unreachable** from node 0 then `answer[i]` is `-1`. **Example 1:** **Input:** n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5] **Output:** [0,-1,4] **Explanation:** We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears. For node 0, we don't need any time as it is our starting point. For node 1, we need at least 2 units of time to traverse `edges[0]`. Unfortunately, it disappears at that moment, so we won't be able to visit it. For node 2, we need at least 4 units of time to traverse `edges[2]`. **Example 2:** **Input:** n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5] **Output:** [0,2,3] **Explanation:** We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears. For node 0, we don't need any time as it is the starting point. For node 1, we need at least 2 units of time to traverse `edges[0]`. For node 2, we need at least 3 units of time to traverse `edges[0]` and `edges[1]`. **Example 3:** **Input:** n = 2, edges = [[0,1,1]], disappear = [1,1] **Output:** [0,-1] **Explanation:** Exactly when we reach node 1, it disappears. **Constraints:** `1 <= n <= 5 * 104` `0 <= edges.length <= 105` `edges[i] == [ui, vi, lengthi]` `0 <= ui, vi <= n - 1` `1 <= lengthi <= 105` `disappear.length == n` `1 <= disappear[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]", + "output": "[0,-1,4] " + }, + { + "label": "Example 2", + "input": "n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]", + "output": "[0,2,3] " + }, + { + "label": "Example 3", + "input": "n = 2, edges = [[0,1,1]], disappear = [1,1]", + "output": "[0,-1] " + } + ], + "constraints": [ + "For node 0, we don't need any time as it is our starting point.", + "For node 1, we need at least 2 units of time to traverse edges[0]. Unfortunately, it disappears at that moment, so we won't be able to visit it.", + "For node 2, we need at least 4 units of time to traverse edges[2].", + "For node 0, we don't need any time as it is the starting point.", + "For node 1, we need at least 2 units of time to traverse edges[0].", + "For node 2, we need at least 3 units of time to traverse edges[0] and edges[1].", + "1 <= n <= 5 * 104", + "0 <= edges.length <= 105", + "edges[i] == [ui, vi, lengthi]", + "0 <= ui, vi <= n - 1", + "1 <= lengthi <= 105", + "disappear.length == n", + "1 <= disappear[i] <= 105" + ], + "python_template": "class Solution(object):\n def minimumTime(self, n, edges, disappear):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type disappear: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] minimumTime(int n, int[][] edges, int[] disappear) {\n \n }\n}", + "metadata": { + "func_name": "minimumTime" + } +} \ No newline at end of file diff --git a/minimum-total-cost-to-make-arrays-unequal.json b/minimum-total-cost-to-make-arrays-unequal.json new file mode 100644 index 0000000000000000000000000000000000000000..2320814442d3083497b6359e7a16ffed1cbd1eb3 --- /dev/null +++ b/minimum-total-cost-to-make-arrays-unequal.json @@ -0,0 +1,35 @@ +{ + "id": 2592, + "name": "minimum-total-cost-to-make-arrays-unequal", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-total-cost-to-make-arrays-unequal/", + "date": "2022-11-26", + "task_description": "You are given two **0-indexed** integer arrays `nums1` and `nums2`, of equal length `n`. In one operation, you can swap the values of any two indices of `nums1`. The **cost** of this operation is the **sum** of the indices. Find the **minimum** total cost of performing the given operation **any** number of times such that `nums1[i] != nums2[i]` for all `0 <= i <= n - 1` after performing all the operations. Return _the **minimum total cost** such that _`nums1` and `nums2`_ satisfy the above condition_. In case it is not possible, return `-1`. **Example 1:** ``` **Input:** nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5] **Output:** 10 **Explanation:** One of the ways we can perform the operations is: - Swap values at indices 0 and 3, incurring cost = 0 + 3 = 3. Now, nums1 = [4,2,3,1,5] - Swap values at indices 1 and 2, incurring cost = 1 + 2 = 3. Now, nums1 = [4,3,2,1,5]. - Swap values at indices 0 and 4, incurring cost = 0 + 4 = 4. Now, nums1 =[5,3,2,1,4]. We can see that for each index i, nums1[i] != nums2[i]. The cost required here is 10. Note that there are other ways to swap values, but it can be proven that it is not possible to obtain a cost less than 10. ``` **Example 2:** ``` **Input:** nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3] **Output:** 10 **Explanation:** One of the ways we can perform the operations is: - Swap values at indices 2 and 3, incurring cost = 2 + 3 = 5. Now, nums1 = [2,2,1,2,3]. - Swap values at indices 1 and 4, incurring cost = 1 + 4 = 5. Now, nums1 = [2,3,1,2,2]. The total cost needed here is 10, which is the minimum possible. ``` **Example 3:** ``` **Input:** nums1 = [1,2,2], nums2 = [1,2,2] **Output:** -1 **Explanation:** It can be shown that it is not possible to satisfy the given conditions irrespective of the number of operations we perform. Hence, we return -1. ``` **Constraints:** `n == nums1.length == nums2.length` `1 <= n <= 105` `1 <= nums1[i], nums2[i] <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]", + "output": "10 " + }, + { + "label": "Example 2", + "input": "nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]", + "output": "10 " + }, + { + "label": "Example 3", + "input": "nums1 = [1,2,2], nums2 = [1,2,2]", + "output": "-1 " + } + ], + "constraints": [ + "n == nums1.length == nums2.length", + "1 <= n <= 105", + "1 <= nums1[i], nums2[i] <= n" + ], + "python_template": "class Solution(object):\n def minimumTotalCost(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumTotalCost(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "minimumTotalCost" + } +} \ No newline at end of file diff --git a/minimum-total-distance-traveled.json b/minimum-total-distance-traveled.json new file mode 100644 index 0000000000000000000000000000000000000000..7756cf3dc26527978271bf22f65bd835e1f47214 --- /dev/null +++ b/minimum-total-distance-traveled.json @@ -0,0 +1,37 @@ +{ + "id": 2554, + "name": "minimum-total-distance-traveled", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-total-distance-traveled/", + "date": "2022-10-30", + "task_description": "There are some robots and factories on the X-axis. You are given an integer array `robot` where `robot[i]` is the position of the `ith` robot. You are also given a 2D integer array `factory` where `factory[j] = [positionj, limitj]` indicates that `positionj` is the position of the `jth` factory and that the `jth` factory can repair at most `limitj` robots. The positions of each robot are **unique**. The positions of each factory are also **unique**. Note that a robot can be **in the same position** as a factory initially. All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving. **At any moment**, you can set the initial direction of moving for **some** robot. Your target is to minimize the total distance traveled by all the robots. Return _the minimum total distance traveled by all the robots_. The test cases are generated such that all the robots can be repaired. **Note that** All robots move at the same speed. If two robots move in the same direction, they will never collide. If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other. If a robot passes by a factory that reached its limits, it crosses it as if it does not exist. If the robot moved from a position `x` to a position `y`, the distance it moved is `|y - x|`. **Example 1:** ``` **Input:** robot = [0,4,6], factory = [[2,2],[6,2]] **Output:** 4 **Explanation:** As shown in the figure: - The first robot at position 0 moves in the positive direction. It will be repaired at the first factory. - The second robot at position 4 moves in the negative direction. It will be repaired at the first factory. - The third robot at position 6 will be repaired at the second factory. It does not need to move. The limit of the first factory is 2, and it fixed 2 robots. The limit of the second factory is 2, and it fixed 1 robot. The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4. ``` **Example 2:** ``` **Input:** robot = [1,-1], factory = [[-2,1],[2,1]] **Output:** 2 **Explanation:** As shown in the figure: - The first robot at position 1 moves in the positive direction. It will be repaired at the second factory. - The second robot at position -1 moves in the negative direction. It will be repaired at the first factory. The limit of the first factory is 1, and it fixed 1 robot. The limit of the second factory is 1, and it fixed 1 robot. The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2. ``` **Constraints:** `1 <= robot.length, factory.length <= 100` `factory[j].length == 2` `-109 <= robot[i], positionj <= 109` `0 <= limitj <= robot.length` The input will be generated such that it is always possible to repair every robot.", + "test_case": [ + { + "label": "Example 1", + "input": "robot = [0,4,6], factory = [[2,2],[6,2]]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "robot = [1,-1], factory = [[-2,1],[2,1]]", + "output": "2 " + } + ], + "constraints": [ + "All robots move at the same speed.", + "If two robots move in the same direction, they will never collide.", + "If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.", + "If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.", + "If the robot moved from a position x to a position y, the distance it moved is |y - x|.", + "1 <= robot.length, factory.length <= 100", + "factory[j].length == 2", + "-109 <= robot[i], positionj <= 109", + "0 <= limitj <= robot.length", + "The input will be generated such that it is always possible to repair every robot." + ], + "python_template": "class Solution(object):\n def minimumTotalDistance(self, robot, factory):\n \"\"\"\n :type robot: List[int]\n :type factory: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumTotalDistance(List robot, int[][] factory) {\n \n }\n}", + "metadata": { + "func_name": "minimumTotalDistance" + } +} \ No newline at end of file diff --git a/minimum-weighted-subgraph-with-the-required-paths.json b/minimum-weighted-subgraph-with-the-required-paths.json new file mode 100644 index 0000000000000000000000000000000000000000..43aeb7bde58d747f9fcf774ace216ff21d349726 --- /dev/null +++ b/minimum-weighted-subgraph-with-the-required-paths.json @@ -0,0 +1,34 @@ +{ + "id": 2321, + "name": "minimum-weighted-subgraph-with-the-required-paths", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-weighted-subgraph-with-the-required-paths/", + "date": "2022-03-06", + "task_description": "You are given an integer `n` denoting the number of nodes of a **weighted directed** graph. The nodes are numbered from `0` to `n - 1`. You are also given a 2D integer array `edges` where `edges[i] = [fromi, toi, weighti]` denotes that there exists a **directed** edge from `fromi` to `toi` with weight `weighti`. Lastly, you are given three **distinct** integers `src1`, `src2`, and `dest` denoting three distinct nodes of the graph. Return _the **minimum weight** of a subgraph of the graph such that it is **possible** to reach_ `dest` _from both_ `src1` _and_ `src2` _via a set of edges of this subgraph_. In case such a subgraph does not exist, return `-1`. A **subgraph** is a graph whose vertices and edges are subsets of the original graph. The **weight** of a subgraph is the sum of weights of its constituent edges. **Example 1:** ``` **Input:** n = 6, edges = [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 = 0, src2 = 1, dest = 5 **Output:** 9 **Explanation:** The above figure represents the input graph. The blue edges represent one of the subgraphs that yield the optimal answer. Note that the subgraph [[1,0,3],[0,5,6]] also yields the optimal answer. It is not possible to get a subgraph with less weight satisfying all the constraints. ``` **Example 2:** ``` **Input:** n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2 **Output:** -1 **Explanation:** The above figure represents the input graph. It can be seen that there does not exist any path from node 1 to node 2, hence there are no subgraphs satisfying all the constraints. ``` **Constraints:** `3 <= n <= 105` `0 <= edges.length <= 105` `edges[i].length == 3` `0 <= fromi, toi, src1, src2, dest <= n - 1` `fromi != toi` `src1`, `src2`, and `dest` are pairwise distinct. `1 <= weight[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 6, edges = [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 = 0, src2 = 1, dest = 5", + "output": "9 " + }, + { + "label": "Example 2", + "input": "n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2", + "output": "-1 " + } + ], + "constraints": [ + "3 <= n <= 105", + "0 <= edges.length <= 105", + "edges[i].length == 3", + "0 <= fromi, toi, src1, src2, dest <= n - 1", + "fromi != toi", + "src1, src2, and dest are pairwise distinct.", + "1 <= weight[i] <= 105" + ], + "python_template": "class Solution(object):\n def minimumWeight(self, n, edges, src1, src2, dest):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type src1: int\n :type src2: int\n :type dest: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumWeight(int n, int[][] edges, int src1, int src2, int dest) {\n \n }\n}", + "metadata": { + "func_name": "minimumWeight" + } +} \ No newline at end of file diff --git a/minimum-white-tiles-after-covering-with-carpets.json b/minimum-white-tiles-after-covering-with-carpets.json new file mode 100644 index 0000000000000000000000000000000000000000..2d5d43acf81b530b79cfcca62d2c2bf2e8ad38ce --- /dev/null +++ b/minimum-white-tiles-after-covering-with-carpets.json @@ -0,0 +1,32 @@ +{ + "id": 2311, + "name": "minimum-white-tiles-after-covering-with-carpets", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-white-tiles-after-covering-with-carpets/", + "date": "2022-03-05", + "task_description": "You are given a **0-indexed binary** string `floor`, which represents the colors of tiles on a floor: `floor[i] = '0'` denotes that the `ith` tile of the floor is colored **black**. On the other hand, `floor[i] = '1'` denotes that the `ith` tile of the floor is colored **white**. You are also given `numCarpets` and `carpetLen`. You have `numCarpets` **black** carpets, each of length `carpetLen` tiles. Cover the tiles with the given carpets such that the number of **white** tiles still visible is **minimum**. Carpets may overlap one another. Return _the **minimum** number of white tiles still visible._ **Example 1:** ``` **Input:** floor = \"10110101\", numCarpets = 2, carpetLen = 2 **Output:** 2 **Explanation:** The figure above shows one way of covering the tiles with the carpets such that only 2 white tiles are visible. No other way of covering the tiles with the carpets can leave less than 2 white tiles visible. ``` **Example 2:** ``` **Input:** floor = \"11111\", numCarpets = 2, carpetLen = 3 **Output:** 0 **Explanation:** The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible. Note that the carpets are able to overlap one another. ``` **Constraints:** `1 <= carpetLen <= floor.length <= 1000` `floor[i]` is either `'0'` or `'1'`. `1 <= numCarpets <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "floor = \"10110101\", numCarpets = 2, carpetLen = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "floor = \"11111\", numCarpets = 2, carpetLen = 3", + "output": "0 " + } + ], + "constraints": [ + "floor[i] = '0' denotes that the ith tile of the floor is colored black.", + "On the other hand, floor[i] = '1' denotes that the ith tile of the floor is colored white.", + "1 <= carpetLen <= floor.length <= 1000", + "floor[i] is either '0' or '1'.", + "1 <= numCarpets <= 1000" + ], + "python_template": "class Solution(object):\n def minimumWhiteTiles(self, floor, numCarpets, carpetLen):\n \"\"\"\n :type floor: str\n :type numCarpets: int\n :type carpetLen: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {\n \n }\n}", + "metadata": { + "func_name": "minimumWhiteTiles" + } +} \ No newline at end of file diff --git a/modify-graph-edge-weights.json b/modify-graph-edge-weights.json new file mode 100644 index 0000000000000000000000000000000000000000..67debb7b8f8698dde6507775383d6b99c51c2901 --- /dev/null +++ b/modify-graph-edge-weights.json @@ -0,0 +1,42 @@ +{ + "id": 2803, + "name": "modify-graph-edge-weights", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/modify-graph-edge-weights/", + "date": "2023-05-14", + "task_description": "You are given an **undirected weighted** **connected** graph containing `n` nodes labeled from `0` to `n - 1`, and an integer array `edges` where `edges[i] = [ai, bi, wi]` indicates that there is an edge between nodes `ai` and `bi` with weight `wi`. Some edges have a weight of `-1` (`wi = -1`), while others have a **positive** weight (`wi > 0`). Your task is to modify **all edges** with a weight of `-1` by assigning them **positive integer values **in the range `[1, 2 * 109]` so that the **shortest distance** between the nodes `source` and `destination` becomes equal to an integer `target`. If there are **multiple** **modifications** that make the shortest distance between `source` and `destination` equal to `target`, any of them will be considered correct. Return _an array containing all edges (even unmodified ones) in any order if it is possible to make the shortest distance from _`source`_ to _`destination`_ equal to _`target`_, or an **empty array** if it's impossible._ **Note:** You are not allowed to modify the weights of edges with initial positive weights. **Example 1:** **** ``` **Input:** n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5 **Output:** [[4,1,1],[2,0,1],[0,3,3],[4,3,1]] **Explanation:** The graph above shows a possible modification to the edges, making the distance from 0 to 1 equal to 5. ``` **Example 2:** **** ``` **Input:** n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6 **Output:** [] **Explanation:** The graph above contains the initial edges. It is not possible to make the distance from 0 to 2 equal to 6 by modifying the edge with weight -1. So, an empty array is returned. ``` **Example 3:** **** ``` **Input:** n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6 **Output:** [[1,0,4],[1,2,3],[2,3,5],[0,3,1]] **Explanation:** The graph above shows a modified graph having the shortest distance from 0 to 2 as 6. ``` **Constraints:** `1 <= n <= 100` `1 <= edges.length <= n * (n - 1) / 2` `edges[i].length == 3` `0 <= ai, bi < n` `wi = -1 `or `1 <= wi <= 107` `ai != bi` `0 <= source, destination < n` `source != destination` `1 <= target <= 109` The graph is connected, and there are no self-loops or repeated edges", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5", + "output": "[[4,1,1],[2,0,1],[0,3,3],[4,3,1]] " + }, + { + "label": "Example 2", + "input": "n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6", + "output": "[] " + }, + { + "label": "Example 3", + "input": "n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6", + "output": "[[1,0,4],[1,2,3],[2,3,5],[0,3,1]] " + } + ], + "constraints": [ + "1 <= n <= 100", + "1 <= edges.length <= n * (n - 1) / 2", + "edges[i].length == 3", + "0 <= ai, biĀ <Ā n", + "wiĀ = -1Ā or 1 <= wiĀ <= 107", + "aiĀ !=Ā bi", + "0 <= source, destination < n", + "source != destination", + "1 <= target <= 109", + "The graph is connected, and there are no self-loops or repeated edges" + ], + "python_template": "class Solution(object):\n def modifiedGraphEdges(self, n, edges, source, destination, target):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type source: int\n :type destination: int\n :type target: int\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] modifiedGraphEdges(int n, int[][] edges, int source, int destination, int target) {\n \n }\n}", + "metadata": { + "func_name": "modifiedGraphEdges" + } +} \ No newline at end of file diff --git a/modify-the-matrix.json b/modify-the-matrix.json new file mode 100644 index 0000000000000000000000000000000000000000..e113bdb6ea2ffb0e9160ae553fe7107ea272c017 --- /dev/null +++ b/modify-the-matrix.json @@ -0,0 +1,32 @@ +{ + "id": 3330, + "name": "modify-the-matrix", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/modify-the-matrix/", + "date": "2024-02-04", + "task_description": "Given a **0-indexed** `m x n` integer matrix `matrix`, create a new **0-indexed** matrix called `answer`. Make `answer` equal to `matrix`, then replace each element with the value `-1` with the **maximum** element in its respective column. Return _the matrix_ `answer`. **Example 1:** ``` **Input:** matrix = [[1,2,-1],[4,-1,6],[7,8,9]] **Output:** [[1,2,9],[4,8,6],[7,8,9]] **Explanation:** The diagram above shows the elements that are changed (in blue). - We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8. - We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9. ``` **Example 2:** ``` **Input:** matrix = [[3,-1],[5,2]] **Output:** [[3,2],[5,2]] **Explanation:** The diagram above shows the elements that are changed (in blue). ``` **Constraints:** `m == matrix.length` `n == matrix[i].length` `2 <= m, n <= 50` `-1 <= matrix[i][j] <= 100` The input is generated such that each column contains at least one non-negative integer.", + "test_case": [ + { + "label": "Example 1", + "input": "matrix = [[1,2,-1],[4,-1,6],[7,8,9]]", + "output": "[[1,2,9],[4,8,6],[7,8,9]] " + }, + { + "label": "Example 2", + "input": "matrix = [[3,-1],[5,2]]", + "output": "[[3,2],[5,2]] " + } + ], + "constraints": [ + "m == matrix.length", + "n == matrix[i].length", + "2 <= m, n <= 50", + "-1 <= matrix[i][j] <= 100", + "The input is generated such that each column contains at least one non-negative integer." + ], + "python_template": "class Solution(object):\n def modifiedMatrix(self, matrix):\n \"\"\"\n :type matrix: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] modifiedMatrix(int[][] matrix) {\n \n }\n}", + "metadata": { + "func_name": "modifiedMatrix" + } +} \ No newline at end of file diff --git a/most-frequent-even-element.json b/most-frequent-even-element.json new file mode 100644 index 0000000000000000000000000000000000000000..86c348252ee8b71df110203b617726aa60f04bd2 --- /dev/null +++ b/most-frequent-even-element.json @@ -0,0 +1,34 @@ +{ + "id": 2486, + "name": "most-frequent-even-element", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/most-frequent-even-element/", + "date": "2022-09-04", + "task_description": "Given an integer array `nums`, return _the most frequent even element_. If there is a tie, return the **smallest** one. If there is no such element, return `-1`. **Example 1:** ``` **Input:** nums = [0,1,2,2,4,4,1] **Output:** 2 **Explanation:** The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most. We return the smallest one, which is 2. ``` **Example 2:** ``` **Input:** nums = [4,4,4,9,2,4] **Output:** 4 **Explanation:** 4 is the even element appears the most. ``` **Example 3:** ``` **Input:** nums = [29,47,21,41,13,37,25,7] **Output:** -1 **Explanation:** There is no even element. ``` **Constraints:** `1 <= nums.length <= 2000` `0 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,2,2,4,4,1]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,4,4,9,2,4]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [29,47,21,41,13,37,25,7]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums.length <= 2000", + "0 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def mostFrequentEven(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int mostFrequentEven(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "mostFrequentEven" + } +} \ No newline at end of file diff --git a/most-frequent-ids.json b/most-frequent-ids.json new file mode 100644 index 0000000000000000000000000000000000000000..1a8a8da11eafd1f3c1d791145502a27ece9c7e49 --- /dev/null +++ b/most-frequent-ids.json @@ -0,0 +1,34 @@ +{ + "id": 3363, + "name": "most-frequent-ids", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/most-frequent-ids/", + "date": "2024-03-17", + "task_description": "The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, `nums` and `freq`, of equal length `n`. Each element in `nums` represents an ID, and the corresponding element in `freq` indicates how many times that ID should be added to or removed from the collection at each step. **Addition of IDs:** If `freq[i]` is positive, it means `freq[i]` IDs with the value `nums[i]` are added to the collection at step `i`. **Removal of IDs:** If `freq[i]` is negative, it means `-freq[i]` IDs with the value `nums[i]` are removed from the collection at step `i`. Return an array `ans` of length `n`, where `ans[i]` represents the **count** of the _most frequent ID_ in the collection after the `ith` step. If the collection is empty at any step, `ans[i]` should be 0 for that step. **Example 1:** **Input:** nums = [2,3,2,1], freq = [3,2,-3,1] **Output:** [3,3,2,2] **Explanation:** After step 0, we have 3 IDs with the value of 2. So `ans[0] = 3`. After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So `ans[1] = 3`. After step 2, we have 2 IDs with the value of 3. So `ans[2] = 2`. After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So `ans[3] = 2`. **Example 2:** **Input:** nums = [5,5,3], freq = [2,-2,1] **Output:** [2,0,1] **Explanation:** After step 0, we have 2 IDs with the value of 5. So `ans[0] = 2`. After step 1, there are no IDs. So `ans[1] = 0`. After step 2, we have 1 ID with the value of 3. So `ans[2] = 1`. **Constraints:** `1 <= nums.length == freq.length <= 105` `1 <= nums[i] <= 105` `-105 <= freq[i] <= 105` `freq[i] != 0` The input is generated such that the occurrences of an ID will not be negative in any step.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,2,1], freq = [3,2,-3,1]", + "output": "[3,3,2,2] " + }, + { + "label": "Example 2", + "input": "nums = [5,5,3], freq = [2,-2,1]", + "output": "[2,0,1] " + } + ], + "constraints": [ + "Addition of IDs: If freq[i] is positive, it means freq[i] IDs with the value nums[i] are added to the collection at step i.", + "Removal of IDs: If freq[i] is negative, it means -freq[i] IDs with the value nums[i] are removed from the collection at step i.", + "1 <= nums.length == freq.length <= 105", + "1 <= nums[i] <= 105", + "-105 <= freq[i] <= 105", + "freq[i] != 0", + "The input is generated such that the occurrences of an ID will not be negative in any step." + ], + "python_template": "class Solution(object):\n def mostFrequentIDs(self, nums, freq):\n \"\"\"\n :type nums: List[int]\n :type freq: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] mostFrequentIDs(int[] nums, int[] freq) {\n \n }\n}", + "metadata": { + "func_name": "mostFrequentIDs" + } +} \ No newline at end of file diff --git a/most-frequent-number-following-key-in-an-array.json b/most-frequent-number-following-key-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..c0f499e892138769552bc7e35f2be0fa45604bbf --- /dev/null +++ b/most-frequent-number-following-key-in-an-array.json @@ -0,0 +1,33 @@ +{ + "id": 2312, + "name": "most-frequent-number-following-key-in-an-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/", + "date": "2022-02-19", + "task_description": "You are given a **0-indexed** integer array `nums`.** **You are also given an integer `key`, which is present in `nums`. For every unique integer `target` in `nums`, **count** the number of times `target` immediately follows an occurrence of `key` in `nums`. In other words, count the number of indices `i` such that: `0 <= i <= nums.length - 2`, `nums[i] == key` and, `nums[i + 1] == target`. Return _the _`target`_ with the **maximum** count_. The test cases will be generated such that the `target` with maximum count is unique. **Example 1:** ``` **Input:** nums = [1,100,200,1,100], key = 1 **Output:** 100 **Explanation:** For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key. No other integers follow an occurrence of key, so we return 100. ``` **Example 2:** ``` **Input:** nums = [2,2,2,2,3], key = 2 **Output:** 2 **Explanation:** For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key. For target = 3, there is only one occurrence at index 4 which follows an occurrence of key. target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2. ``` **Constraints:** `2 <= nums.length <= 1000` `1 <= nums[i] <= 1000` The test cases will be generated such that the answer is unique.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,100,200,1,100], key = 1", + "output": "100 " + }, + { + "label": "Example 2", + "input": "nums = [2,2,2,2,3], key = 2", + "output": "2 " + } + ], + "constraints": [ + "0 <= i <= nums.length - 2,", + "nums[i] == key and,", + "nums[i + 1] == target.", + "2 <= nums.length <= 1000", + "1 <= nums[i] <= 1000", + "The test cases will be generated such that the answer is unique." + ], + "python_template": "class Solution(object):\n def mostFrequent(self, nums, key):\n \"\"\"\n :type nums: List[int]\n :type key: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int mostFrequent(int[] nums, int key) {\n \n }\n}", + "metadata": { + "func_name": "mostFrequent" + } +} \ No newline at end of file diff --git a/most-frequent-prime.json b/most-frequent-prime.json new file mode 100644 index 0000000000000000000000000000000000000000..b9e0069bc3299d13e4118df3e71ac798d86a0fa8 --- /dev/null +++ b/most-frequent-prime.json @@ -0,0 +1,39 @@ +{ + "id": 3314, + "name": "most-frequent-prime", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/most-frequent-prime/", + "date": "2024-02-11", + "task_description": "You are given a `m x n` **0-indexed **2D** **matrix `mat`. From every cell, you can create numbers in the following way: There could be at most `8` paths from the cells namely: east, south-east, south, south-west, west, north-west, north, and north-east. Select a path from them and append digits in this path to the number being formed by traveling in this direction. Note that numbers are generated at every step, for example, if the digits along the path are `1, 9, 1`, then there will be three numbers generated along the way: `1, 19, 191`. Return _the most frequent prime number **greater** than _`10`_ out of all the numbers created by traversing the matrix or _`-1`_ if no such prime number exists. If there are multiple prime numbers with the highest frequency, then return the largest among them._ **Note:** It is invalid to change the direction during the move. **Example 1:** ** ** ``` ** Input:** mat = [[1,1],[9,9],[1,1]] **Output:** 19 **Explanation:** From cell (0,0) there are 3 possible directions and the numbers greater than 10 which can be created in those directions are: East: [11], South-East: [19], South: [19,191]. Numbers greater than 10 created from the cell (0,1) in all possible directions are: [19,191,19,11]. Numbers greater than 10 created from the cell (1,0) in all possible directions are: [99,91,91,91,91]. Numbers greater than 10 created from the cell (1,1) in all possible directions are: [91,91,99,91,91]. Numbers greater than 10 created from the cell (2,0) in all possible directions are: [11,19,191,19]. Numbers greater than 10 created from the cell (2,1) in all possible directions are: [11,19,19,191]. The most frequent prime number among all the created numbers is 19. ``` **Example 2:** ``` **Input:** mat = [[7]] **Output:** -1 **Explanation:** The only number which can be formed is 7. It is a prime number however it is not greater than 10, so return -1. ``` **Example 3:** ``` **Input:** mat = [[9,7,8],[4,6,5],[2,8,6]] **Output:** 97 **Explanation:** Numbers greater than 10 created from the cell (0,0) in all possible directions are: [97,978,96,966,94,942]. Numbers greater than 10 created from the cell (0,1) in all possible directions are: [78,75,76,768,74,79]. Numbers greater than 10 created from the cell (0,2) in all possible directions are: [85,856,86,862,87,879]. Numbers greater than 10 created from the cell (1,0) in all possible directions are: [46,465,48,42,49,47]. Numbers greater than 10 created from the cell (1,1) in all possible directions are: [65,66,68,62,64,69,67,68]. Numbers greater than 10 created from the cell (1,2) in all possible directions are: [56,58,56,564,57,58]. Numbers greater than 10 created from the cell (2,0) in all possible directions are: [28,286,24,249,26,268]. Numbers greater than 10 created from the cell (2,1) in all possible directions are: [86,82,84,86,867,85]. Numbers greater than 10 created from the cell (2,2) in all possible directions are: [68,682,66,669,65,658]. The most frequent prime number among all the created numbers is 97. ``` **Constraints:** `m == mat.length` `n == mat[i].length` `1 <= m, n <= 6` `1 <= mat[i][j] <= 9`", + "test_case": [ + { + "label": "Example 1", + "input": "mat = [[1,1],[9,9],[1,1]]", + "output": "19 " + }, + { + "label": "Example 2", + "input": "mat = [[7]]", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "mat = [[9,7,8],[4,6,5],[2,8,6]]", + "output": "97 " + } + ], + "constraints": [ + "There could be at most 8 paths from the cells namely: east, south-east, south, south-west, west, north-west, north, and north-east.", + "Select a path from them and append digits in this path to the number being formed by traveling in this direction.", + "Note that numbers are generated at every step, for example, if the digits along the path are 1, 9, 1, then there will be three numbers generated along the way: 1, 19, 191.", + "m == mat.length", + "n == mat[i].length", + "1 <= m, n <= 6", + "1 <= mat[i][j] <= 9" + ], + "python_template": "class Solution(object):\n def mostFrequentPrime(self, mat):\n \"\"\"\n :type mat: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int mostFrequentPrime(int[][] mat) {\n \n }\n}", + "metadata": { + "func_name": "mostFrequentPrime" + } +} \ No newline at end of file diff --git a/most-popular-video-creator.json b/most-popular-video-creator.json new file mode 100644 index 0000000000000000000000000000000000000000..10cabd7eed95c44a88839d43f25a0596bb15fca3 --- /dev/null +++ b/most-popular-video-creator.json @@ -0,0 +1,34 @@ +{ + "id": 2543, + "name": "most-popular-video-creator", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/most-popular-video-creator/", + "date": "2022-10-23", + "task_description": "You are given two string arrays `creators` and `ids`, and an integer array `views`, all of length `n`. The `ith` video on a platform was created by `creators[i]`, has an id of `ids[i]`, and has `views[i]` views. The **popularity** of a creator is the **sum** of the number of views on **all** of the creator's videos. Find the creator with the **highest** popularity and the id of their **most** viewed video. If multiple creators have the highest popularity, find all of them. If multiple videos have the highest view count for a creator, find the lexicographically **smallest** id. Note: It is possible for different videos to have the same `id`, meaning that `id`s do not uniquely identify a video. For example, two videos with the same ID are considered as distinct videos with their own viewcount. Return_ _a **2D array** of **strings** `answer` where `answer[i] = [creatorsi, idi]` means that `creatorsi` has the **highest** popularity and `idi` is the **id** of their most **popular** video. The answer can be returned in any order. **Example 1:** **Input:** creators = [\"alice\",\"bob\",\"alice\",\"chris\"], ids = [\"one\",\"two\",\"three\",\"four\"], views = [5,10,5,4] **Output:** [[\"alice\",\"one\"],[\"bob\",\"two\"]] **Explanation:** The popularity of alice is 5 + 5 = 10. The popularity of bob is 10. The popularity of chris is 4. alice and bob are the most popular creators. For bob, the video with the highest view count is \"two\". For alice, the videos with the highest view count are \"one\" and \"three\". Since \"one\" is lexicographically smaller than \"three\", it is included in the answer. **Example 2:** **Input:** creators = [\"alice\",\"alice\",\"alice\"], ids = [\"a\",\"b\",\"c\"], views = [1,2,2] **Output:** [[\"alice\",\"b\"]] **Explanation:** The videos with id \"b\" and \"c\" have the highest view count. Since \"b\" is lexicographically smaller than \"c\", it is included in the answer. **Constraints:** `n == creators.length == ids.length == views.length` `1 <= n <= 105` `1 <= creators[i].length, ids[i].length <= 5` `creators[i]` and `ids[i]` consist only of lowercase English letters. `0 <= views[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "creators = [\"alice\",\"bob\",\"alice\",\"chris\"], ids = [\"one\",\"two\",\"three\",\"four\"], views = [5,10,5,4]", + "output": "[[\"alice\",\"one\"],[\"bob\",\"two\"]] " + }, + { + "label": "Example 2", + "input": "creators = [\"alice\",\"alice\",\"alice\"], ids = [\"a\",\"b\",\"c\"], views = [1,2,2]", + "output": "[[\"alice\",\"b\"]] " + } + ], + "constraints": [ + "If multiple creators have the highest popularity, find all of them.", + "If multiple videos have the highest view count for a creator, find the lexicographically smallest id.", + "n == creators.length == ids.length == views.length", + "1 <= n <= 105", + "1 <= creators[i].length, ids[i].length <= 5", + "creators[i] and ids[i] consist only of lowercase English letters.", + "0 <= views[i] <= 105" + ], + "python_template": "class Solution(object):\n def mostPopularCreator(self, creators, ids, views):\n \"\"\"\n :type creators: List[str]\n :type ids: List[str]\n :type views: List[int]\n :rtype: List[List[str]]\n \"\"\"\n ", + "java_template": "class Solution {\n public List> mostPopularCreator(String[] creators, String[] ids, int[] views) {\n \n }\n}", + "metadata": { + "func_name": "mostPopularCreator" + } +} \ No newline at end of file diff --git a/most-profitable-path-in-a-tree.json b/most-profitable-path-in-a-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..1c64834fe7e5ac29c59bf79f8eed868acffb5998 --- /dev/null +++ b/most-profitable-path-in-a-tree.json @@ -0,0 +1,46 @@ +{ + "id": 2564, + "name": "most-profitable-path-in-a-tree", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/most-profitable-path-in-a-tree/", + "date": "2022-10-29", + "task_description": "There is an undirected tree with `n` nodes labeled from `0` to `n - 1`, rooted at node `0`. You are given 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. At every node `i`, there is a gate. You are also given an array of even integers `amount`, where `amount[i]` represents: the price needed to open the gate at node `i`, if `amount[i]` is negative, or, the cash reward obtained on opening the gate at node `i`, otherwise. The game goes on as follows: Initially, Alice is at node `0` and Bob is at node `bob`. At every second, Alice and Bob each move to an adjacent node. Alice moves towards some **leaf node**, while Bob moves towards node `0`. For **every** node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that: If the gate is **already open**, no price will be required, nor will there be any cash reward. If Alice and Bob reach the node **simultaneously**, they share the price/reward for opening the gate there. In other words, if the price to open the gate is `c`, then both Alice and Bob pay `c / 2` each. Similarly, if the reward at the gate is `c`, both of them receive `c / 2` each. If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node `0`, he stops moving. Note that these events are **independent** of each other. Return_ the **maximum** net income Alice can have if she travels towards the optimal leaf node._ **Example 1:** ``` **Input:** edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6] **Output:** 6 **Explanation:** The above diagram represents the given tree. The game goes as follows: - Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes. Alice's net income is now -2. - Both Alice and Bob move to node 1. Since they reach here simultaneously, they open the gate together and share the reward. Alice's net income becomes -2 + (4 / 2) = 0. - Alice moves on to node 3. Since Bob already opened its gate, Alice's income remains unchanged. Bob moves on to node 0, and stops moving. - Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6. Now, neither Alice nor Bob can make any further moves, and the game ends. It is not possible for Alice to get a higher net income. ``` **Example 2:** ``` **Input:** edges = [[0,1]], bob = 1, amount = [-7280,2350] **Output:** -7280 **Explanation:** Alice follows the path 0->1 whereas Bob follows the path 1->0. Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280. ``` **Constraints:** `2 <= n <= 105` `edges.length == n - 1` `edges[i].length == 2` `0 <= ai, bi < n` `ai != bi` `edges` represents a valid tree. `1 <= bob < n` `amount.length == n` `amount[i]` is an **even** integer in the range `[-104, 104]`.", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "edges = [[0,1]], bob = 1, amount = [-7280,2350]", + "output": "-7280 " + } + ], + "constraints": [ + "the price needed to open the gate at node i, if amount[i] is negative, or,", + "the cash reward obtained on opening the gate at node i, otherwise.", + "Initially, Alice is at node 0 and Bob is at node bob.", + "At every second, Alice and Bob each move to an adjacent node. Alice moves towards some leaf node, while Bob moves towards node 0.", + "For every node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:\n\t\nIf the gate is already open, no price will be required, nor will there be any cash reward.\nIf Alice and Bob reach the node simultaneously, they share the price/reward for opening the gate there. In other words, if the price to open the gate is c, then both Alice and Bob payĀ c / 2 each. Similarly, if the reward at the gate is c, both of them receive c / 2 each.", + "If the gate is already open, no price will be required, nor will there be any cash reward.", + "If Alice and Bob reach the node simultaneously, they share the price/reward for opening the gate there. In other words, if the price to open the gate is c, then both Alice and Bob payĀ c / 2 each. Similarly, if the reward at the gate is c, both of them receive c / 2 each.", + "If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node 0, he stops moving. Note that these events are independent of each other.", + "If the gate is already open, no price will be required, nor will there be any cash reward.", + "If Alice and Bob reach the node simultaneously, they share the price/reward for opening the gate there. In other words, if the price to open the gate is c, then both Alice and Bob payĀ c / 2 each. Similarly, if the reward at the gate is c, both of them receive c / 2 each.", + "2 <= n <= 105", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ai, bi < n", + "ai != bi", + "edges represents a valid tree.", + "1 <= bob < n", + "amount.length == n", + "amount[i] is an even integer in the range [-104, 104]." + ], + "python_template": "class Solution(object):\n def mostProfitablePath(self, edges, bob, amount):\n \"\"\"\n :type edges: List[List[int]]\n :type bob: int\n :type amount: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int mostProfitablePath(int[][] edges, int bob, int[] amount) {\n \n }\n}", + "metadata": { + "func_name": "mostProfitablePath" + } +} \ No newline at end of file diff --git a/move-pieces-to-obtain-a-string.json b/move-pieces-to-obtain-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..d799431a1b6f31edce3415eddcf00934f4d7b1ea --- /dev/null +++ b/move-pieces-to-obtain-a-string.json @@ -0,0 +1,37 @@ +{ + "id": 2414, + "name": "move-pieces-to-obtain-a-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/move-pieces-to-obtain-a-string/", + "date": "2022-07-03", + "task_description": "You are given two strings `start` and `target`, both of length `n`. Each string consists **only** of the characters `'L'`, `'R'`, and `'_'` where: The characters `'L'` and `'R'` represent pieces, where a piece `'L'` can move to the **left** only if there is a **blank** space directly to its left, and a piece `'R'` can move to the **right** only if there is a **blank** space directly to its right. The character `'_'` represents a blank space that can be occupied by **any** of the `'L'` or `'R'` pieces. Return `true` _if it is possible to obtain the string_ `target`_ by moving the pieces of the string _`start`_ **any** number of times_. Otherwise, return `false`. **Example 1:** ``` **Input:** start = \"_L__R__R_\", target = \"L______RR\" **Output:** true **Explanation:** We can obtain the string target from start by doing the following moves: - Move the first piece one step to the left, start becomes equal to \"**L**___R__R_\". - Move the last piece one step to the right, start becomes equal to \"L___R___**R**\". - Move the second piece three steps to the right, start becomes equal to \"L______**R**R\". Since it is possible to get the string target from start, we return true. ``` **Example 2:** ``` **Input:** start = \"R_L_\", target = \"__LR\" **Output:** false **Explanation:** The 'R' piece in the string start can move one step to the right to obtain \"_**R**L_\". After that, no pieces can move anymore, so it is impossible to obtain the string target from start. ``` **Example 3:** ``` **Input:** start = \"_R\", target = \"R_\" **Output:** false **Explanation:** The piece in the string start can move only to the right, so it is impossible to obtain the string target from start. ``` **Constraints:** `n == start.length == target.length` `1 <= n <= 105` `start` and `target` consist of the characters `'L'`, `'R'`, and `'_'`.", + "test_case": [ + { + "label": "Example 1", + "input": "start = \"_L__R__R_\", target = \"L______RR\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "start = \"R_L_\", target = \"__LR\"", + "output": "false " + }, + { + "label": "Example 3", + "input": "start = \"_R\", target = \"R_\"", + "output": "false " + } + ], + "constraints": [ + "The characters 'L' and 'R' represent pieces, where a piece 'L' can move to the left only if there is a blank space directly to its left, and a piece 'R' can move to the right only if there is a blank space directly to its right.", + "The character '_' represents a blank space that can be occupied by any of the 'L' or 'R' pieces.", + "n == start.length == target.length", + "1 <= n <= 105", + "start and target consist of the characters 'L', 'R', and '_'." + ], + "python_template": "class Solution(object):\n def canChange(self, start, target):\n \"\"\"\n :type start: str\n :type target: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean canChange(String start, String target) {\n \n }\n}", + "metadata": { + "func_name": "canChange" + } +} \ No newline at end of file diff --git a/movement-of-robots.json b/movement-of-robots.json new file mode 100644 index 0000000000000000000000000000000000000000..7ba2b79e9cc153e439b5e93592fd68925a7bfd01 --- /dev/null +++ b/movement-of-robots.json @@ -0,0 +1,40 @@ +{ + "id": 2787, + "name": "movement-of-robots", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/movement-of-robots/", + "date": "2023-05-27", + "task_description": "Some robots are standing on an infinite number line with their initial coordinates given by a **0-indexed** integer array `nums` and will start moving once given the command to move. The robots will move a unit distance each second. You are given a string `s` denoting the direction in which robots will move on command. `'L'` means the robot will move towards the left side or negative side of the number line, whereas `'R'` means the robot will move towards the right side or positive side of the number line. If two robots collide, they will start moving in opposite directions. Return _the sum of distances between all the pairs of robots _`d` _seconds after the command. _Since the sum can be very large, return it modulo `109 + 7`. Note: For two robots at the index `i` and `j`, pair `(i,j)` and pair `(j,i)` are considered the same pair. When robots collide, they **instantly change** their directions without wasting any time. Collision happens when two robots share the same place in a moment. For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right. For example, if a robot is positioned in 0 going to the right and another is positioned in 1 going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right. **Example 1:** ``` **Input:** nums = [-2,0,2], s = \"RLL\", d = 3 **Output:** 8 **Explanation:** After 1 second, the positions are [-1,-1,1]. Now, the robot at index 0 will move left, and the robot at index 1 will move right. After 2 seconds, the positions are [-2,0,0]. Now, the robot at index 1 will move left, and the robot at index 2 will move right. After 3 seconds, the positions are [-3,-1,1]. The distance between the robot at index 0 and 1 is abs(-3 - (-1)) = 2. The distance between the robot at index 0 and 2 is abs(-3 - 1) = 4. The distance between the robot at index 1 and 2 is abs(-1 - 1) = 2. The sum of the pairs of all distances = 2 + 4 + 2 = 8. ``` **Example 2:** ``` **Input:** nums = [1,0], s = \"RL\", d = 2 **Output:** 5 **Explanation:** After 1 second, the positions are [2,-1]. After 2 seconds, the positions are [3,-2]. The distance between the two robots is abs(-2 - 3) = 5. ``` **Constraints:** `2 <= nums.length <= 105` `-2 * 109 <= nums[i] <= 2 * 109` `0 <= d <= 109` `nums.length == s.length ` `s` consists of 'L' and 'R' only `nums[i]` will be unique.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [-2,0,2], s = \"RLL\", d = 3", + "output": "8 " + }, + { + "label": "Example 2", + "input": "nums = [1,0], s = \"RL\", d = 2", + "output": "5 " + } + ], + "constraints": [ + "For two robots at the index i and j, pair (i,j) and pair (j,i) are considered the same pair.", + "When robots collide, they instantly change their directions without wasting any time.", + "Collision happensĀ when two robots share the same place in aĀ moment.\n\t\nFor example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right.\nFor example,Ā if a robot is positioned in 0 going to the right and another is positioned in 1Ā going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.", + "For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right.", + "For example,Ā if a robot is positioned in 0 going to the right and another is positioned in 1Ā going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.", + "For example, if a robot is positioned in 0 going to the right and another is positioned in 2 going to the left, the next second they'll be both in 1 and they will change direction and the next second the first one will be in 0, heading left, and another will be in 2, heading right.", + "For example,Ā if a robot is positioned in 0 going to the right and another is positioned in 1Ā going to the left, the next second the first one will be in 0, heading left, and another will be in 1, heading right.", + "2 <= nums.length <= 105", + "-2 * 109Ā <= nums[i] <= 2 * 109", + "0 <= d <= 109", + "nums.length == s.length", + "s consists of 'L' and 'R' only", + "nums[i]Ā will be unique." + ], + "python_template": "class Solution(object):\n def sumDistance(self, nums, s, d):\n \"\"\"\n :type nums: List[int]\n :type s: str\n :type d: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumDistance(int[] nums, String s, int d) {\n \n }\n}", + "metadata": { + "func_name": "sumDistance" + } +} \ No newline at end of file diff --git a/neighboring-bitwise-xor.json b/neighboring-bitwise-xor.json new file mode 100644 index 0000000000000000000000000000000000000000..a1905e11d56cdfa64688a03cd4c850ba26fc5da9 --- /dev/null +++ b/neighboring-bitwise-xor.json @@ -0,0 +1,38 @@ +{ + "id": 2792, + "name": "neighboring-bitwise-xor", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/neighboring-bitwise-xor/", + "date": "2023-05-07", + "task_description": "A **0-indexed** array `derived` with length `n` is derived by computing the **bitwise XOR** (āŠ•) of adjacent values in a **binary array** `original` of length `n`. Specifically, for each index `i` in the range `[0, n - 1]`: If `i = n - 1`, then `derived[i] = original[i] āŠ• original[0]`. Otherwise, `derived[i] = original[i] āŠ• original[i + 1]`. Given an array `derived`, your task is to determine whether there exists a **valid binary array** `original` that could have formed `derived`. Return _**true** if such an array exists or **false** otherwise._ A binary array is an array containing only **0's** and **1's** **Example 1:** ``` **Input:** derived = [1,1,0] **Output:** true **Explanation:** A valid original array that gives derived is [0,1,0]. derived[0] = original[0] āŠ• original[1] = 0 āŠ• 1 = 1 derived[1] = original[1] āŠ• original[2] = 1 āŠ• 0 = 1 derived[2] = original[2] āŠ• original[0] = 0 āŠ• 0 = 0 ``` **Example 2:** ``` **Input:** derived = [1,1] **Output:** true **Explanation:** A valid original array that gives derived is [0,1]. derived[0] = original[0] āŠ• original[1] = 1 derived[1] = original[1] āŠ• original[0] = 1 ``` **Example 3:** ``` **Input:** derived = [1,0] **Output:** false **Explanation:** There is no valid original array that gives derived. ``` **Constraints:** `n == derived.length` `1 <= n <= 105` The values in `derived` are either **0's** or **1's**", + "test_case": [ + { + "label": "Example 1", + "input": "derived = [1,1,0]", + "output": "true " + }, + { + "label": "Example 2", + "input": "derived = [1,1]", + "output": "true " + }, + { + "label": "Example 3", + "input": "derived = [1,0]", + "output": "false " + } + ], + "constraints": [ + "If i = n - 1, then derived[i] = original[i] āŠ• original[0].", + "Otherwise, derived[i] = original[i] āŠ• original[i + 1].", + "A binary array is an array containing only 0's and 1's", + "n == derived.length", + "1 <= nĀ <= 105", + "The values in derivedĀ are either 0's or 1's" + ], + "python_template": "class Solution(object):\n def doesValidArrayExist(self, derived):\n \"\"\"\n :type derived: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean doesValidArrayExist(int[] derived) {\n \n }\n}", + "metadata": { + "func_name": "doesValidArrayExist" + } +} \ No newline at end of file diff --git a/neither-minimum-nor-maximum.json b/neither-minimum-nor-maximum.json new file mode 100644 index 0000000000000000000000000000000000000000..2a5c37502a960a4b1f048c949bee70dc3312bf7c --- /dev/null +++ b/neither-minimum-nor-maximum.json @@ -0,0 +1,35 @@ +{ + "id": 2836, + "name": "neither-minimum-nor-maximum", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/neither-minimum-nor-maximum/", + "date": "2023-06-04", + "task_description": "Given an integer array `nums` containing **distinct** **positive** integers, find and return **any** number from the array that is neither the **minimum** nor the **maximum** value in the array, or **`-1`** if there is no such number. Return _the selected integer._ **Example 1:** ``` **Input:** nums = [3,2,1,4] **Output:** 2 **Explanation:** In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers. ``` **Example 2:** ``` **Input:** nums = [1,2] **Output:** -1 **Explanation:** Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer. ``` **Example 3:** ``` **Input:** nums = [2,1,3] **Output:** 2 **Explanation:** Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100` All values in `nums` are distinct", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,2,1,4]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,2]", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "nums = [2,1,3]", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100", + "All values in nums are distinct" + ], + "python_template": "class Solution(object):\n def findNonMinOrMax(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findNonMinOrMax(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findNonMinOrMax" + } +} \ No newline at end of file diff --git a/node-with-highest-edge-score.json b/node-with-highest-edge-score.json new file mode 100644 index 0000000000000000000000000000000000000000..e33c2eade53238dd920f22b32b61fa704c61f342 --- /dev/null +++ b/node-with-highest-edge-score.json @@ -0,0 +1,31 @@ +{ + "id": 2455, + "name": "node-with-highest-edge-score", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/node-with-highest-edge-score/", + "date": "2022-08-07", + "task_description": "You are given a directed graph with `n` nodes labeled from `0` to `n - 1`, where each node has **exactly one** outgoing edge. The graph is represented by a given **0-indexed** integer array `edges` of length `n`, where `edges[i]` indicates that there is a **directed** edge from node `i` to node `edges[i]`. The **edge score** of a node `i` is defined as the sum of the **labels** of all the nodes that have an edge pointing to `i`. Return _the node with the highest **edge score**_. If multiple nodes have the same **edge score**, return the node with the **smallest** index. **Example 1:** ``` **Input:** edges = [1,0,0,0,0,7,7,5] **Output:** 7 **Explanation:** - The nodes 1, 2, 3 and 4 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 + 3 + 4 = 10. - The node 0 has an edge pointing to node 1. The edge score of node 1 is 0. - The node 7 has an edge pointing to node 5. The edge score of node 5 is 7. - The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11. Node 7 has the highest edge score so return 7. ``` **Example 2:** ``` **Input:** edges = [2,0,0,2] **Output:** 0 **Explanation:** - The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3. - The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3. Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0. ``` **Constraints:** `n == edges.length` `2 <= n <= 105` `0 <= edges[i] < n` `edges[i] != i`", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [1,0,0,0,0,7,7,5]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "edges = [2,0,0,2]", + "output": "0 " + } + ], + "constraints": [ + "n == edges.length", + "2 <= n <= 105", + "0 <= edges[i] < n", + "edges[i] != i" + ], + "python_template": "class Solution(object):\n def edgeScore(self, edges):\n \"\"\"\n :type edges: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int edgeScore(int[] edges) {\n \n }\n}", + "metadata": { + "func_name": "edgeScore" + } +} \ No newline at end of file diff --git a/number-of-arithmetic-triplets.json b/number-of-arithmetic-triplets.json new file mode 100644 index 0000000000000000000000000000000000000000..acc31825cbad29e3cd3f540932ba508e36b25e9a --- /dev/null +++ b/number-of-arithmetic-triplets.json @@ -0,0 +1,34 @@ +{ + "id": 2442, + "name": "number-of-arithmetic-triplets", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/number-of-arithmetic-triplets/", + "date": "2022-07-31", + "task_description": "You are given a **0-indexed**, **strictly increasing** integer array `nums` and a positive integer `diff`. A triplet `(i, j, k)` is an **arithmetic triplet** if the following conditions are met: `i < j < k`, `nums[j] - nums[i] == diff`, and `nums[k] - nums[j] == diff`. Return _the number of unique **arithmetic triplets**._ **Example 1:** ``` **Input:** nums = [0,1,4,6,7,10], diff = 3 **Output:** 2 **Explanation:** (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3. (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. ``` **Example 2:** ``` **Input:** nums = [4,5,6,7,8,9], diff = 2 **Output:** 2 **Explanation:** (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2. (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2. ``` **Constraints:** `3 <= nums.length <= 200` `0 <= nums[i] <= 200` `1 <= diff <= 50` `nums` is **strictly** increasing.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,4,6,7,10], diff = 3", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,5,6,7,8,9], diff = 2", + "output": "2 " + } + ], + "constraints": [ + "i < j < k,", + "nums[j] - nums[i] == diff, and", + "nums[k] - nums[j] == diff.", + "3 <= nums.length <= 200", + "0 <= nums[i] <= 200", + "1 <= diff <= 50", + "nums is strictly increasing." + ], + "python_template": "class Solution(object):\n def arithmeticTriplets(self, nums, diff):\n \"\"\"\n :type nums: List[int]\n :type diff: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int arithmeticTriplets(int[] nums, int diff) {\n \n }\n}", + "metadata": { + "func_name": "arithmeticTriplets" + } +} \ No newline at end of file diff --git a/number-of-beautiful-pairs.json b/number-of-beautiful-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..9fe935fcc4639be11e5c8e624ebabb4ba1276de1 --- /dev/null +++ b/number-of-beautiful-pairs.json @@ -0,0 +1,30 @@ +{ + "id": 2831, + "name": "number-of-beautiful-pairs", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/number-of-beautiful-pairs/", + "date": "2023-06-18", + "task_description": "You are given a **0-indexed **integer array `nums`. A pair of indices `i`, `j` where `0 <= i < j < nums.length` is called beautiful if the **first digit** of `nums[i]` and the **last digit** of `nums[j]` are **coprime**. Return _the total number of beautiful pairs in _`nums`. Two integers `x` and `y` are **coprime** if there is no integer greater than 1 that divides both of them. In other words, `x` and `y` are coprime if `gcd(x, y) == 1`, where `gcd(x, y)` is the **greatest common divisor** of `x` and `y`. **Example 1:** ``` **Input:** nums = [2,5,1,4] **Output:** 5 **Explanation:** There are 5 beautiful pairs in nums: When i = 0 and j = 1: the first digit of nums[0] is 2, and the last digit of nums[1] is 5. We can confirm that 2 and 5 are coprime, since gcd(2,5) == 1. When i = 0 and j = 2: the first digit of nums[0] is 2, and the last digit of nums[2] is 1. Indeed, gcd(2,1) == 1. When i = 1 and j = 2: the first digit of nums[1] is 5, and the last digit of nums[2] is 1. Indeed, gcd(5,1) == 1. When i = 1 and j = 3: the first digit of nums[1] is 5, and the last digit of nums[3] is 4. Indeed, gcd(5,4) == 1. When i = 2 and j = 3: the first digit of nums[2] is 1, and the last digit of nums[3] is 4. Indeed, gcd(1,4) == 1. Thus, we return 5. ``` **Example 2:** ``` **Input:** nums = [11,21,12] **Output:** 2 **Explanation:** There are 2 beautiful pairs: When i = 0 and j = 1: the first digit of nums[0] is 1, and the last digit of nums[1] is 1. Indeed, gcd(1,1) == 1. When i = 0 and j = 2: the first digit of nums[0] is 1, and the last digit of nums[2] is 2. Indeed, gcd(1,2) == 1. Thus, we return 2. ``` **Constraints:** `2 <= nums.length <= 100` `1 <= nums[i] <= 9999` `nums[i] % 10 != 0`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,5,1,4]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [11,21,12]", + "output": "2 " + } + ], + "constraints": [ + "2 <= nums.length <= 100", + "1 <= nums[i] <= 9999", + "nums[i] % 10 != 0" + ], + "python_template": "class Solution(object):\n def countBeautifulPairs(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countBeautifulPairs(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "countBeautifulPairs" + } +} \ No newline at end of file diff --git a/number-of-beautiful-partitions.json b/number-of-beautiful-partitions.json new file mode 100644 index 0000000000000000000000000000000000000000..63bb6607cd20daff824b9ee7038f490eaf73081e --- /dev/null +++ b/number-of-beautiful-partitions.json @@ -0,0 +1,37 @@ +{ + "id": 2569, + "name": "number-of-beautiful-partitions", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-beautiful-partitions/", + "date": "2022-11-13", + "task_description": "You are given a string `s` that consists of the digits `'1'` to `'9'` and two integers `k` and `minLength`. A partition of `s` is called **beautiful** if: `s` is partitioned into `k` non-intersecting substrings. Each substring has a length of **at least** `minLength`. Each substring starts with a **prime** digit and ends with a **non-prime** digit. Prime digits are `'2'`, `'3'`, `'5'`, and `'7'`, and the rest of the digits are non-prime. Return_ the number of **beautiful** partitions of _`s`. Since the answer may be very large, return it **modulo** `109 + 7`. A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` **Input:** s = \"23542185131\", k = 3, minLength = 2 **Output:** 3 **Explanation:** There exists three ways to create a beautiful partition: \"2354 | 218 | 5131\" \"2354 | 21851 | 31\" \"2354218 | 51 | 31\" ``` **Example 2:** ``` **Input:** s = \"23542185131\", k = 3, minLength = 3 **Output:** 1 **Explanation:** There exists one way to create a beautiful partition: \"2354 | 218 | 5131\". ``` **Example 3:** ``` **Input:** s = \"3312958\", k = 3, minLength = 1 **Output:** 1 **Explanation:** There exists one way to create a beautiful partition: \"331 | 29 | 58\". ``` **Constraints:** `1 <= k, minLength <= s.length <= 1000` `s` consists of the digits `'1'` to `'9'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"23542185131\", k = 3, minLength = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \"23542185131\", k = 3, minLength = 3", + "output": "1 " + }, + { + "label": "Example 3", + "input": "s = \"3312958\", k = 3, minLength = 1", + "output": "1 " + } + ], + "constraints": [ + "s is partitioned into k non-intersecting substrings.", + "Each substring has a length of at least minLength.", + "Each substring starts with a prime digit and ends with a non-prime digit. Prime digits are '2', '3', '5', and '7', and the rest of the digits are non-prime.", + "1 <= k, minLength <= s.length <= 1000", + "s consists of the digits '1' to '9'." + ], + "python_template": "class Solution(object):\n def beautifulPartitions(self, s, k, minLength):\n \"\"\"\n :type s: str\n :type k: int\n :type minLength: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int beautifulPartitions(String s, int k, int minLength) {\n \n }\n}", + "metadata": { + "func_name": "beautifulPartitions" + } +} \ No newline at end of file diff --git a/number-of-changing-keys.json b/number-of-changing-keys.json new file mode 100644 index 0000000000000000000000000000000000000000..14e550506174d9d245b2b25ae4eb93f1abe0ddea --- /dev/null +++ b/number-of-changing-keys.json @@ -0,0 +1,29 @@ +{ + "id": 3312, + "name": "number-of-changing-keys", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/number-of-changing-keys/", + "date": "2024-01-21", + "task_description": "You are given a **0-indexed **string `s` typed by a user. Changing a key is defined as using a key different from the last used key. For example, `s = \"ab\"` has a change of a key while `s = \"bBBb\"` does not have any. Return _the number of times the user had to change the key. _ **Note: **Modifiers like `shift` or `caps lock` won't be counted in changing the key that is if a user typed the letter `'a'` and then the letter `'A'` then it will not be considered as a changing of key. **Example 1:** ``` **Input:** s = \"aAbBcC\" **Output:** 2 **Explanation:** From s[0] = 'a' to s[1] = 'A', there is no change of key as caps lock or shift is not counted. From s[1] = 'A' to s[2] = 'b', there is a change of key. From s[2] = 'b' to s[3] = 'B', there is no change of key as caps lock or shift is not counted. From s[3] = 'B' to s[4] = 'c', there is a change of key. From s[4] = 'c' to s[5] = 'C', there is no change of key as caps lock or shift is not counted. ``` **Example 2:** ``` **Input:** s = \"AaAaAaaA\" **Output:** 0 **Explanation:** There is no change of key since only the letters 'a' and 'A' are pressed which does not require change of key. ``` **Constraints:** `1 <= s.length <= 100` `s` consists of only upper case and lower case English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aAbBcC\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"AaAaAaaA\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "s consists of only upper case and lower case English letters." + ], + "python_template": "class Solution(object):\n def countKeyChanges(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countKeyChanges(String s) {\n \n }\n}", + "metadata": { + "func_name": "countKeyChanges" + } +} \ No newline at end of file diff --git a/number-of-common-factors.json b/number-of-common-factors.json new file mode 100644 index 0000000000000000000000000000000000000000..d7058355961967d93fbc96fbff061e835b1ecf49 --- /dev/null +++ b/number-of-common-factors.json @@ -0,0 +1,28 @@ +{ + "id": 2507, + "name": "number-of-common-factors", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/number-of-common-factors/", + "date": "2022-09-25", + "task_description": "Given two positive integers `a` and `b`, return _the number of **common** factors of _`a`_ and _`b`. An integer `x` is a **common factor** of `a` and `b` if `x` divides both `a` and `b`. **Example 1:** ``` **Input:** a = 12, b = 6 **Output:** 4 **Explanation:** The common factors of 12 and 6 are 1, 2, 3, 6. ``` **Example 2:** ``` **Input:** a = 25, b = 30 **Output:** 2 **Explanation:** The common factors of 25 and 30 are 1, 5. ``` **Constraints:** `1 <= a, b <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "a = 12, b = 6", + "output": "4 " + }, + { + "label": "Example 2", + "input": "a = 25, b = 30", + "output": "2 " + } + ], + "constraints": [ + "1 <= a, b <= 1000" + ], + "python_template": "class Solution(object):\n def commonFactors(self, a, b):\n \"\"\"\n :type a: int\n :type b: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int commonFactors(int a, int b) {\n \n }\n}", + "metadata": { + "func_name": "commonFactors" + } +} \ No newline at end of file diff --git a/number-of-distinct-averages.json b/number-of-distinct-averages.json new file mode 100644 index 0000000000000000000000000000000000000000..2945e92a3c18e7de870e6ebe78ceefc79cd659fb --- /dev/null +++ b/number-of-distinct-averages.json @@ -0,0 +1,34 @@ +{ + "id": 2561, + "name": "number-of-distinct-averages", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/number-of-distinct-averages/", + "date": "2022-10-29", + "task_description": "You are given a **0-indexed** integer array `nums` of **even** length. As long as `nums` is **not** empty, you must repetitively: Find the minimum number in `nums` and remove it. Find the maximum number in `nums` and remove it. Calculate the average of the two removed numbers. The **average** of two numbers `a` and `b` is `(a + b) / 2`. For example, the average of `2` and `3` is `(2 + 3) / 2 = 2.5`. Return_ the number of **distinct** averages calculated using the above process_. **Note** that when there is a tie for a minimum or maximum number, any can be removed. **Example 1:** ``` **Input:** nums = [4,1,4,0,3,5] **Output:** 2 **Explanation:** 1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3]. 2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3]. 3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5. Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2. ``` **Example 2:** ``` **Input:** nums = [1,100] **Output:** 1 **Explanation:** There is only one average to be calculated after removing 1 and 100, so we return 1. ``` **Constraints:** `2 <= nums.length <= 100` `nums.length` is even. `0 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,1,4,0,3,5]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,100]", + "output": "1 " + } + ], + "constraints": [ + "Find the minimum number in nums and remove it.", + "Find the maximum number in nums and remove it.", + "Calculate the average of the two removed numbers.", + "For example, the average of 2 and 3 is (2 + 3) / 2 = 2.5.", + "2 <= nums.length <= 100", + "nums.length is even.", + "0 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def distinctAverages(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int distinctAverages(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "distinctAverages" + } +} \ No newline at end of file diff --git a/number-of-employees-who-met-the-target.json b/number-of-employees-who-met-the-target.json new file mode 100644 index 0000000000000000000000000000000000000000..e0e23056a1a0017000b8cb0371dc76dc0575b66f --- /dev/null +++ b/number-of-employees-who-met-the-target.json @@ -0,0 +1,29 @@ +{ + "id": 2876, + "name": "number-of-employees-who-met-the-target", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/number-of-employees-who-met-the-target/", + "date": "2023-07-23", + "task_description": "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 integer denoting the number of employees who worked at least_ `target` _hours_. **Example 1:** ``` **Input:** hours = [0,1,2,3,4], target = 2 **Output:** 3 **Explanation:** The company wants each employee to work for at least 2 hours. - Employee 0 worked for 0 hours and didn't meet the target. - Employee 1 worked for 1 hours and didn't meet the target. - Employee 2 worked for 2 hours and met the target. - Employee 3 worked for 3 hours and met the target. - Employee 4 worked for 4 hours and met the target. There are 3 employees who met the target. ``` **Example 2:** ``` **Input:** hours = [5,1,4,2,2], target = 6 **Output:** 0 **Explanation:** The company wants each employee to work for at least 6 hours. There are 0 employees who met the target. ``` **Constraints:** `1 <= n == hours.length <= 50` `0 <= hours[i], target <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "hours = [0,1,2,3,4], target = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "hours = [5,1,4,2,2], target = 6", + "output": "0 " + } + ], + "constraints": [ + "1 <= n == hours.length <= 50", + "0 <=Ā hours[i], target <= 105" + ], + "python_template": "class Solution(object):\n def numberOfEmployeesWhoMetTarget(self, hours, target):\n \"\"\"\n :type hours: List[int]\n :type target: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {\n \n }\n}", + "metadata": { + "func_name": "numberOfEmployeesWhoMetTarget" + } +} \ No newline at end of file diff --git a/number-of-excellent-pairs.json b/number-of-excellent-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..d559b3381f3b8a366b0fd842a32c7f11e8c4cf22 --- /dev/null +++ b/number-of-excellent-pairs.json @@ -0,0 +1,32 @@ +{ + "id": 2430, + "name": "number-of-excellent-pairs", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-excellent-pairs/", + "date": "2022-07-17", + "task_description": "You are given a **0-indexed** positive integer array `nums` and a positive integer `k`. A pair of numbers `(num1, num2)` is called **excellent** if the following conditions are satisfied: **Both** the numbers `num1` and `num2` exist in the array `nums`. The sum of the number of set bits in `num1 OR num2` and `num1 AND num2` is greater than or equal to `k`, where `OR` is the bitwise **OR** operation and `AND` is the bitwise **AND** operation. Return _the number of **distinct** excellent pairs_. Two pairs `(a, b)` and `(c, d)` are considered distinct if either `a != c` or `b != d`. For example, `(1, 2)` and `(2, 1)` are distinct. **Note** that a pair `(num1, num2)` such that `num1 == num2` can also be excellent if you have at least **one** occurrence of `num1` in the array. **Example 1:** ``` **Input:** nums = [1,2,3,1], k = 3 **Output:** 5 **Explanation:** The excellent pairs are the following: - (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3. - (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3. - (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3. So the number of excellent pairs is 5. ``` **Example 2:** ``` **Input:** nums = [5,1,1], k = 10 **Output:** 0 **Explanation:** There are no excellent pairs for this array. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `1 <= k <= 60`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,1], k = 3", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [5,1,1], k = 10", + "output": "0 " + } + ], + "constraints": [ + "Both the numbers num1 and num2 exist in the array nums.", + "The sum of the number of set bits in num1 OR num2 and num1 AND num2 is greater than or equal to k, where OR is the bitwise OR operation and AND is the bitwise AND operation.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "1 <= k <= 60" + ], + "python_template": "class Solution(object):\n def countExcellentPairs(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countExcellentPairs(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "countExcellentPairs" + } +} \ No newline at end of file diff --git a/number-of-flowers-in-full-bloom.json b/number-of-flowers-in-full-bloom.json new file mode 100644 index 0000000000000000000000000000000000000000..88176b793cda12183887c076cdb835b02ebcb075 --- /dev/null +++ b/number-of-flowers-in-full-bloom.json @@ -0,0 +1,32 @@ +{ + "id": 2334, + "name": "number-of-flowers-in-full-bloom", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-flowers-in-full-bloom/", + "date": "2022-04-17", + "task_description": "You are given a **0-indexed** 2D integer array `flowers`, where `flowers[i] = [starti, endi]` means the `ith` flower will be in **full bloom** from `starti` to `endi` (**inclusive**). You are also given a **0-indexed** integer array `people` of size `n`, where `people[i]` is the time that the `ith` person will arrive to see the flowers. Return _an integer array _`answer`_ of size _`n`_, where _`answer[i]`_ is the **number** of flowers that are in full bloom when the _`ith`_ person arrives._ **Example 1:** ``` **Input:** flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11] **Output:** [1,2,2,2] **Explanation: **The figure above shows the times when the flowers are in full bloom and when the people arrive. For each person, we return the number of flowers in full bloom during their arrival. ``` **Example 2:** ``` **Input:** flowers = [[1,10],[3,3]], people = [3,3,2] **Output:** [2,2,1] **Explanation:** The figure above shows the times when the flowers are in full bloom and when the people arrive. For each person, we return the number of flowers in full bloom during their arrival. ``` **Constraints:** `1 <= flowers.length <= 5 * 104` `flowers[i].length == 2` `1 <= starti <= endi <= 109` `1 <= people.length <= 5 * 104` `1 <= people[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11]", + "output": "[1,2,2,2] " + }, + { + "label": "Example 2", + "input": "flowers = [[1,10],[3,3]], people = [3,3,2]", + "output": "[2,2,1] " + } + ], + "constraints": [ + "1 <= flowers.length <= 5 * 104", + "flowers[i].length == 2", + "1 <= starti <= endi <= 109", + "1 <= people.length <= 5 * 104", + "1 <= people[i] <= 109" + ], + "python_template": "class Solution(object):\n def fullBloomFlowers(self, flowers, people):\n \"\"\"\n :type flowers: List[List[int]]\n :type people: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] fullBloomFlowers(int[][] flowers, int[] people) {\n \n }\n}", + "metadata": { + "func_name": "fullBloomFlowers" + } +} \ No newline at end of file diff --git a/number-of-good-paths.json b/number-of-good-paths.json new file mode 100644 index 0000000000000000000000000000000000000000..163ef576230ef571c959b5b3d6e3cec9e3f6b3b0 --- /dev/null +++ b/number-of-good-paths.json @@ -0,0 +1,40 @@ +{ + "id": 2505, + "name": "number-of-good-paths", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-good-paths/", + "date": "2022-09-18", + "task_description": "There is a tree (i.e. a connected, undirected graph with no cycles) consisting of `n` nodes numbered from `0` to `n - 1` and exactly `n - 1` edges. You are given a **0-indexed** integer array `vals` of length `n` where `vals[i]` denotes the value of the `ith` node. You are also given a 2D integer array `edges` where `edges[i] = [ai, bi]` denotes that there exists an **undirected** edge connecting nodes `ai` and `bi`. A **good path** is a simple path that satisfies the following conditions: The starting node and the ending node have the **same** value. All nodes between the starting node and the ending node have values **less than or equal to** the starting node (i.e. the starting node's value should be the maximum value along the path). Return _the number of distinct good paths_. Note that a path and its reverse are counted as the **same** path. For example, `0 -> 1` is considered to be the same as `1 -> 0`. A single node is also considered as a valid path. **Example 1:** ``` **Input:** vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] **Output:** 6 **Explanation:** There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -> 0 -> 2 -> 4. (The reverse path 4 -> 2 -> 0 -> 1 is treated as the same as 1 -> 0 -> 2 -> 4.) Note that 0 -> 2 -> 3 is not a good path because vals[2] > vals[0]. ``` **Example 2:** ``` **Input:** vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] **Output:** 7 **Explanation:** There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -> 1 and 2 -> 3. ``` **Example 3:** ``` **Input:** vals = [1], edges = [] **Output:** 1 **Explanation:** The tree consists of only one node, so there is one good path. ``` **Constraints:** `n == vals.length` `1 <= n <= 3 * 104` `0 <= vals[i] <= 105` `edges.length == n - 1` `edges[i].length == 2` `0 <= ai, bi < n` `ai != bi` `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]", + "output": "7 " + }, + { + "label": "Example 3", + "input": "vals = [1], edges = []", + "output": "1 " + } + ], + "constraints": [ + "n == vals.length", + "1 <= n <= 3 * 104", + "0 <= vals[i] <= 105", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ai, bi < n", + "ai != bi", + "edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def numberOfGoodPaths(self, vals, edges):\n \"\"\"\n :type vals: List[int]\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfGoodPaths(int[] vals, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "numberOfGoodPaths" + } +} \ No newline at end of file diff --git a/number-of-great-partitions.json b/number-of-great-partitions.json new file mode 100644 index 0000000000000000000000000000000000000000..44cec3d031d7c0bdfa9960feaad5a554d7b34e12 --- /dev/null +++ b/number-of-great-partitions.json @@ -0,0 +1,34 @@ +{ + "id": 2601, + "name": "number-of-great-partitions", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-great-partitions/", + "date": "2022-12-18", + "task_description": "You are given an array `nums` consisting of **positive** integers and an integer `k`. **Partition** the array into two ordered **groups** such that each element is in exactly **one** group. A partition is called great if the **sum** of elements of each group is greater than or equal to `k`. Return _the number of **distinct** great partitions_. Since the answer may be too large, return it **modulo** `109 + 7`. Two partitions are considered distinct if some element `nums[i]` is in different groups in the two partitions. **Example 1:** ``` **Input:** nums = [1,2,3,4], k = 4 **Output:** 6 **Explanation:** The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]). ``` **Example 2:** ``` **Input:** nums = [3,3,3], k = 4 **Output:** 0 **Explanation:** There are no great partitions for this array. ``` **Example 3:** ``` **Input:** nums = [6,6], k = 2 **Output:** 2 **Explanation:** We can either put nums[0] in the first partition or in the second partition. The great partitions will be ([6], [6]) and ([6], [6]). ``` **Constraints:** `1 <= nums.length, k <= 1000` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4], k = 4", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [3,3,3], k = 4", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [6,6], k = 2", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length, k <= 1000", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def countPartitions(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countPartitions(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "countPartitions" + } +} \ No newline at end of file diff --git a/number-of-laser-beams-in-a-bank.json b/number-of-laser-beams-in-a-bank.json new file mode 100644 index 0000000000000000000000000000000000000000..6f66e3c1704f99ed0f4db1dc0be7181b629da9ac --- /dev/null +++ b/number-of-laser-beams-in-a-bank.json @@ -0,0 +1,33 @@ +{ + "id": 2244, + "name": "number-of-laser-beams-in-a-bank", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-laser-beams-in-a-bank/", + "date": "2021-12-26", + "task_description": "Anti-theft security devices are activated inside a bank. You are given a **0-indexed** binary string array `bank` representing the floor plan of the bank, which is an `m x n` 2D matrix. `bank[i]` represents the `ith` row, consisting of `'0'`s and `'1'`s. `'0'` means the cell is empty, while`'1'` means the cell has a security device. There is **one** laser beam between any **two** security devices **if both** conditions are met: The two devices are located on two **different rows**: `r1` and `r2`, where `r1 < r2`. For **each** row `i` where `r1 < i < r2`, there are **no security devices** in the `ith` row. Laser beams are independent, i.e., one beam does not interfere nor join with another. Return _the total number of laser beams in the bank_. **Example 1:** ``` **Input:** bank = [\"011001\",\"000000\",\"010100\",\"001000\"] **Output:** 8 **Explanation:** Between each of the following device pairs, there is one beam. In total, there are 8 beams: * bank[0][1] -- bank[2][1] * bank[0][1] -- bank[2][3] * bank[0][2] -- bank[2][1] * bank[0][2] -- bank[2][3] * bank[0][5] -- bank[2][1] * bank[0][5] -- bank[2][3] * bank[2][1] -- bank[3][2] * bank[2][3] -- bank[3][2] Note that there is no beam between any device on the 0th row with any on the 3rd row. This is because the 2nd row contains security devices, which breaks the second condition. ``` **Example 2:** ``` **Input:** bank = [\"000\",\"111\",\"000\"] **Output:** 0 **Explanation:** There does not exist two devices located on two different rows. ``` **Constraints:** `m == bank.length` `n == bank[i].length` `1 <= m, n <= 500` `bank[i][j]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "bank = [\"011001\",\"000000\",\"010100\",\"001000\"]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "bank = [\"000\",\"111\",\"000\"]", + "output": "0 " + } + ], + "constraints": [ + "The two devices are located on two different rows: r1 and r2, where r1 < r2.", + "For each row i where r1 < i < r2, there are no security devices in the ith row.", + "m == bank.length", + "n == bank[i].length", + "1 <= m, n <= 500", + "bank[i][j] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def numberOfBeams(self, bank):\n \"\"\"\n :type bank: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfBeams(String[] bank) {\n \n }\n}", + "metadata": { + "func_name": "numberOfBeams" + } +} \ No newline at end of file diff --git a/number-of-pairs-satisfying-inequality.json b/number-of-pairs-satisfying-inequality.json new file mode 100644 index 0000000000000000000000000000000000000000..430fb7d0c269436d33588dce687f38deec308517 --- /dev/null +++ b/number-of-pairs-satisfying-inequality.json @@ -0,0 +1,33 @@ +{ + "id": 2513, + "name": "number-of-pairs-satisfying-inequality", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-pairs-satisfying-inequality/", + "date": "2022-09-17", + "task_description": "You are given two **0-indexed** integer arrays `nums1` and `nums2`, each of size `n`, and an integer `diff`. Find the number of **pairs** `(i, j)` such that: `0 <= i < j <= n - 1` **and** `nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff`. Return_ the **number of pairs** that satisfy the conditions._ **Example 1:** ``` **Input:** nums1 = [3,2,5], nums2 = [2,2,1], diff = 1 **Output:** 3 **Explanation:** There are 3 pairs that satisfy the conditions: 1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions. 2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions. 3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions. Therefore, we return 3. ``` **Example 2:** ``` **Input:** nums1 = [3,-1], nums2 = [-2,2], diff = -1 **Output:** 0 **Explanation:** Since there does not exist any pair that satisfies the conditions, we return 0. ``` **Constraints:** `n == nums1.length == nums2.length` `2 <= n <= 105` `-104 <= nums1[i], nums2[i] <= 104` `-104 <= diff <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [3,2,5], nums2 = [2,2,1], diff = 1", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums1 = [3,-1], nums2 = [-2,2], diff = -1", + "output": "0 " + } + ], + "constraints": [ + "0 <= i < j <= n - 1 and", + "nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff.", + "n == nums1.length == nums2.length", + "2 <= n <= 105", + "-104 <= nums1[i], nums2[i] <= 104", + "-104 <= diff <= 104" + ], + "python_template": "class Solution(object):\n def numberOfPairs(self, nums1, nums2, diff):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type diff: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long numberOfPairs(int[] nums1, int[] nums2, int diff) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPairs" + } +} \ No newline at end of file diff --git a/number-of-people-aware-of-a-secret.json b/number-of-people-aware-of-a-secret.json new file mode 100644 index 0000000000000000000000000000000000000000..fba154a0b372e7c37a4cba96c70cc97a029f1942 --- /dev/null +++ b/number-of-people-aware-of-a-secret.json @@ -0,0 +1,29 @@ +{ + "id": 2408, + "name": "number-of-people-aware-of-a-secret", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-people-aware-of-a-secret/", + "date": "2022-06-26", + "task_description": "On day `1`, one person discovers a secret. You are given an integer `delay`, which means that each person will **share** the secret with a new person **every day**, starting from `delay` days after discovering the secret. You are also given an integer `forget`, which means that each person will **forget** the secret `forget` days after discovering it. A person **cannot** share the secret on the same day they forgot it, or on any day afterwards. Given an integer `n`, return_ the number of people who know the secret at the end of day _`n`. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** n = 6, delay = 2, forget = 4 **Output:** 5 **Explanation:** Day 1: Suppose the first person is named A. (1 person) Day 2: A is the only person who knows the secret. (1 person) Day 3: A shares the secret with a new person, B. (2 people) Day 4: A shares the secret with a new person, C. (3 people) Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people) Day 6: B shares the secret with E, and C shares the secret with F. (5 people) ``` **Example 2:** ``` **Input:** n = 4, delay = 1, forget = 3 **Output:** 6 **Explanation:** Day 1: The first person is named A. (1 person) Day 2: A shares the secret with B. (2 people) Day 3: A and B share the secret with 2 new people, C and D. (4 people) Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people) ``` **Constraints:** `2 <= n <= 1000` `1 <= delay < forget <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 6, delay = 2, forget = 4", + "output": "5 " + }, + { + "label": "Example 2", + "input": "n = 4, delay = 1, forget = 3", + "output": "6 " + } + ], + "constraints": [ + "2 <= n <= 1000", + "1 <= delay < forget <= n" + ], + "python_template": "class Solution(object):\n def peopleAwareOfSecret(self, n, delay, forget):\n \"\"\"\n :type n: int\n :type delay: int\n :type forget: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int peopleAwareOfSecret(int n, int delay, int forget) {\n \n }\n}", + "metadata": { + "func_name": "peopleAwareOfSecret" + } +} \ No newline at end of file diff --git a/number-of-subarrays-that-match-a-pattern-i.json b/number-of-subarrays-that-match-a-pattern-i.json new file mode 100644 index 0000000000000000000000000000000000000000..643693396e960f1867495eb42d237e2da71c9cc9 --- /dev/null +++ b/number-of-subarrays-that-match-a-pattern-i.json @@ -0,0 +1,34 @@ +{ + "id": 3269, + "name": "number-of-subarrays-that-match-a-pattern-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i/", + "date": "2024-02-04", + "task_description": "You are given a **0-indexed** integer array `nums` of size `n`, and a **0-indexed** integer array `pattern` of size `m` consisting of integers `-1`, `0`, and `1`. A subarray `nums[i..j]` of size `m + 1` is said to match the `pattern` if the following conditions hold for each element `pattern[k]`: `nums[i + k + 1] > nums[i + k]` if `pattern[k] == 1`. `nums[i + k + 1] == nums[i + k]` if `pattern[k] == 0`. `nums[i + k + 1] < nums[i + k]` if `pattern[k] == -1`. Return _the** count** of subarrays in_ `nums` _that match the_ `pattern`. **Example 1:** ``` **Input:** nums = [1,2,3,4,5,6], pattern = [1,1] **Output:** 4 **Explanation:** The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern. Hence, there are 4 subarrays in nums that match the pattern. ``` **Example 2:** ``` **Input:** nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1] **Output:** 2 **Explanation: **Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern. Hence, there are 2 subarrays in nums that match the pattern. ``` **Constraints:** `2 <= n == nums.length <= 100` `1 <= nums[i] <= 109` `1 <= m == pattern.length < n` `-1 <= pattern[i] <= 1`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5,6], pattern = [1,1]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]", + "output": "2 " + } + ], + "constraints": [ + "nums[i + k + 1] > nums[i + k] if pattern[k] == 1.", + "nums[i + k + 1] == nums[i + k] if pattern[k] == 0.", + "nums[i + k + 1] < nums[i + k] if pattern[k] == -1.", + "2 <= n == nums.length <= 100", + "1 <= nums[i] <= 109", + "1 <= m == pattern.length < n", + "-1 <= pattern[i] <= 1" + ], + "python_template": "class Solution(object):\n def countMatchingSubarrays(self, nums, pattern):\n \"\"\"\n :type nums: List[int]\n :type pattern: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countMatchingSubarrays(int[] nums, int[] pattern) {\n \n }\n}", + "metadata": { + "func_name": "countMatchingSubarrays" + } +} \ No newline at end of file diff --git a/number-of-subarrays-with-and-value-of-k.json b/number-of-subarrays-with-and-value-of-k.json new file mode 100644 index 0000000000000000000000000000000000000000..28363be30272dc16c64853cbc024422d92c06674 --- /dev/null +++ b/number-of-subarrays-with-and-value-of-k.json @@ -0,0 +1,34 @@ +{ + "id": 3466, + "name": "number-of-subarrays-with-and-value-of-k", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-subarrays-with-and-value-of-k/", + "date": "2024-06-22", + "task_description": "Given an array of integers `nums` and an integer `k`, return the number of subarrays of `nums` where the bitwise `AND` of the elements of the subarray equals `k`. **Example 1:** **Input:** nums = [1,1,1], k = 1 **Output:** 6 **Explanation:** All subarrays contain only 1's. **Example 2:** **Input:** nums = [1,1,2], k = 1 **Output:** 3 **Explanation:** Subarrays having an `AND` value of 1 are: `[**1**,1,2]`, `[1,**1**,2]`, `[**1,1**,2]`. **Example 3:** **Input:** nums = [1,2,3], k = 2 **Output:** 2 **Explanation:** Subarrays having an `AND` value of 2 are: `[1,2,3]`, `[1,**2,3**]`. **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i], k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1,1], k = 1", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,2], k = 1", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3], k = 2", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i], k <= 109" + ], + "python_template": "class Solution(object):\n def countSubarrays(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countSubarrays(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "countSubarrays" + } +} \ No newline at end of file diff --git a/number-of-subarrays-with-gcd-equal-to-k.json b/number-of-subarrays-with-gcd-equal-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..dccaf94ee1a4c9914d29c6784afe3babf23604e1 --- /dev/null +++ b/number-of-subarrays-with-gcd-equal-to-k.json @@ -0,0 +1,29 @@ +{ + "id": 2546, + "name": "number-of-subarrays-with-gcd-equal-to-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/", + "date": "2022-10-16", + "task_description": "Given an integer array `nums` and an integer `k`, return _the number of **subarrays** of _`nums`_ where the greatest common divisor of the subarray's elements is _`k`. A **subarray** is a contiguous non-empty sequence of elements within an array. The **greatest common divisor of an array** is the largest integer that evenly divides all the array elements. **Example 1:** ``` **Input:** nums = [9,3,1,2,6,3], k = 3 **Output:** 4 **Explanation:** The subarrays of nums where 3 is the greatest common divisor of all the subarray's elements are: - [9,**3**,1,2,6,3] - [9,3,1,2,6,**3**] - [**9,3**,1,2,6,3] - [9,3,1,2,**6,3**] ``` **Example 2:** ``` **Input:** nums = [4], k = 7 **Output:** 0 **Explanation:** There are no subarrays of nums where 7 is the greatest common divisor of all the subarray's elements. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i], k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [9,3,1,2,6,3], k = 3", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [4], k = 7", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "1 <= nums[i], k <= 109" + ], + "python_template": "class Solution(object):\n def subarrayGCD(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int subarrayGCD(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "subarrayGCD" + } +} \ No newline at end of file diff --git a/number-of-subarrays-with-lcm-equal-to-k.json b/number-of-subarrays-with-lcm-equal-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..0b85171c962884e9a66ef8d46b70ae93e83fc53b --- /dev/null +++ b/number-of-subarrays-with-lcm-equal-to-k.json @@ -0,0 +1,29 @@ +{ + "id": 2557, + "name": "number-of-subarrays-with-lcm-equal-to-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-subarrays-with-lcm-equal-to-k/", + "date": "2022-11-06", + "task_description": "Given an integer array `nums` and an integer `k`, return _the number of **subarrays** of _`nums`_ where the least common multiple of the subarray's elements is _`k`. A **subarray** is a contiguous non-empty sequence of elements within an array. The **least common multiple of an array** is the smallest positive integer that is divisible by all the array elements. **Example 1:** ``` **Input:** nums = [3,6,2,7,1], k = 6 **Output:** 4 **Explanation:** The subarrays of nums where 6 is the least common multiple of all the subarray's elements are: - [**3**,**6**,2,7,1] - [**3**,**6**,**2**,7,1] - [3,**6**,2,7,1] - [3,**6**,**2**,7,1] ``` **Example 2:** ``` **Input:** nums = [3], k = 2 **Output:** 0 **Explanation:** There are no subarrays of nums where 2 is the least common multiple of all the subarray's elements. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i], k <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,6,2,7,1], k = 6", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [3], k = 2", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "1 <= nums[i], k <= 1000" + ], + "python_template": "class Solution(object):\n def subarrayLCM(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int subarrayLCM(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "subarrayLCM" + } +} \ No newline at end of file diff --git a/number-of-valid-clock-times.json b/number-of-valid-clock-times.json new file mode 100644 index 0000000000000000000000000000000000000000..1216f3e5b308de8ac9e1412dc262e627f0035623 --- /dev/null +++ b/number-of-valid-clock-times.json @@ -0,0 +1,36 @@ +{ + "id": 2528, + "name": "number-of-valid-clock-times", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/number-of-valid-clock-times/", + "date": "2022-10-01", + "task_description": "You are given a string of length `5` called `time`, representing the current time on a digital clock in the format `\"hh:mm\"`. The **earliest** possible time is `\"00:00\"` and the **latest** possible time is `\"23:59\"`. In the string `time`, the digits represented by the `?` symbol are **unknown**, and must be **replaced** with a digit from `0` to `9`. Return_ an integer _`answer`_, the number of valid clock times that can be created by replacing every _`?`_ with a digit from _`0`_ to _`9`. **Example 1:** ``` **Input:** time = \"?5:00\" **Output:** 2 **Explanation:** We can replace the ? with either a 0 or 1, producing \"05:00\" or \"15:00\". Note that we cannot replace it with a 2, since the time \"25:00\" is invalid. In total, we have two choices. ``` **Example 2:** ``` **Input:** time = \"0?:0?\" **Output:** 100 **Explanation:** Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices. ``` **Example 3:** ``` **Input:** time = \"??:??\" **Output:** 1440 **Explanation:** There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 * 60 = 1440 choices. ``` **Constraints:** `time` is a valid string of length `5` in the format `\"hh:mm\"`. `\"00\" <= hh <= \"23\"` `\"00\" <= mm <= \"59\"` Some of the digits might be replaced with `'?'` and need to be replaced with digits from `0` to `9`.", + "test_case": [ + { + "label": "Example 1", + "input": "time = \"?5:00\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "time = \"0?:0?\"", + "output": "100 " + }, + { + "label": "Example 3", + "input": "time = \"??:??\"", + "output": "1440 " + } + ], + "constraints": [ + "time is a valid string of length 5 in the format \"hh:mm\".", + "\"00\" <= hh <= \"23\"", + "\"00\" <= mm <= \"59\"", + "Some of the digits might be replaced with '?' and need to be replaced with digits from 0 to 9." + ], + "python_template": "class Solution(object):\n def countTime(self, time):\n \"\"\"\n :type time: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countTime(String time) {\n \n }\n}", + "metadata": { + "func_name": "countTime" + } +} \ No newline at end of file diff --git a/number-of-ways-to-buy-pens-and-pencils.json b/number-of-ways-to-buy-pens-and-pencils.json new file mode 100644 index 0000000000000000000000000000000000000000..ca6af288b0e7655cbd6c4bbca7ac0abd7b9b724c --- /dev/null +++ b/number-of-ways-to-buy-pens-and-pencils.json @@ -0,0 +1,28 @@ +{ + "id": 2351, + "name": "number-of-ways-to-buy-pens-and-pencils", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-ways-to-buy-pens-and-pencils/", + "date": "2022-04-02", + "task_description": "You are given an integer `total` indicating the amount of money you have. You are also given two integers `cost1` and `cost2` indicating the price of a pen and pencil respectively. You can spend **part or all** of your money to buy multiple quantities (or none) of each kind of writing utensil. Return _the **number of distinct ways** you can buy some number of pens and pencils._ **Example 1:** ``` **Input:** total = 20, cost1 = 10, cost2 = 5 **Output:** 9 **Explanation:** The price of a pen is 10 and the price of a pencil is 5. - If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils. - If you buy 1 pen, you can buy 0, 1, or 2 pencils. - If you buy 2 pens, you cannot buy any pencils. The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9. ``` **Example 2:** ``` **Input:** total = 5, cost1 = 10, cost2 = 10 **Output:** 1 **Explanation:** The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils. ``` **Constraints:** `1 <= total, cost1, cost2 <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "total = 20, cost1 = 10, cost2 = 5", + "output": "9 " + }, + { + "label": "Example 2", + "input": "total = 5, cost1 = 10, cost2 = 10", + "output": "1 " + } + ], + "constraints": [ + "1 <= total, cost1, cost2 <= 106" + ], + "python_template": "class Solution(object):\n def waysToBuyPensPencils(self, total, cost1, cost2):\n \"\"\"\n :type total: int\n :type cost1: int\n :type cost2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long waysToBuyPensPencils(int total, int cost1, int cost2) {\n \n }\n}", + "metadata": { + "func_name": "waysToBuyPensPencils" + } +} \ No newline at end of file diff --git a/number-of-ways-to-divide-a-long-corridor.json b/number-of-ways-to-divide-a-long-corridor.json new file mode 100644 index 0000000000000000000000000000000000000000..09206fe6b56879564f5579810367b1c06c734ffd --- /dev/null +++ b/number-of-ways-to-divide-a-long-corridor.json @@ -0,0 +1,35 @@ +{ + "id": 2251, + "name": "number-of-ways-to-divide-a-long-corridor", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/", + "date": "2022-01-08", + "task_description": "Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant. One room divider has **already** been installed to the left of index `0`, and **another** to the right of index `n - 1`. Additional room dividers can be installed. For each position between indices `i - 1` and `i` (`1 <= i <= n - 1`), at most one divider can be installed. Divide the corridor into non-overlapping sections, where each section has **exactly two seats** with any number of plants. There may be multiple ways to perform the division. Two ways are **different** if there is a position with a room divider installed in the first way but not in the second way. Return _the number of ways to divide the corridor_. Since the answer may be very large, return it **modulo** `109 + 7`. If there is no way, return `0`. **Example 1:** ``` **Input:** corridor = \"SSPPSPS\" **Output:** 3 **Explanation:** There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, **each** section has exactly **two** seats. ``` **Example 2:** ``` **Input:** corridor = \"PPSPSP\" **Output:** 1 **Explanation:** There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats. ``` **Example 3:** ``` **Input:** corridor = \"S\" **Output:** 0 **Explanation:** There is no way to divide the corridor because there will always be a section that does not have exactly two seats. ``` **Constraints:** `n == corridor.length` `1 <= n <= 105` `corridor[i]` is either `'S'` or `'P'`.", + "test_case": [ + { + "label": "Example 1", + "input": "corridor = \"SSPPSPS\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "corridor = \"PPSPSP\"", + "output": "1 " + }, + { + "label": "Example 3", + "input": "corridor = \"S\"", + "output": "0 " + } + ], + "constraints": [ + "n == corridor.length", + "1 <= n <= 105", + "corridor[i] is either 'S' or 'P'." + ], + "python_template": "class Solution(object):\n def numberOfWays(self, corridor):\n \"\"\"\n :type corridor: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfWays(String corridor) {\n \n }\n}", + "metadata": { + "func_name": "numberOfWays" + } +} \ No newline at end of file diff --git a/number-of-ways-to-earn-points.json b/number-of-ways-to-earn-points.json new file mode 100644 index 0000000000000000000000000000000000000000..74f70beb26744a57c195bf055c4fa8d75ac1818c --- /dev/null +++ b/number-of-ways-to-earn-points.json @@ -0,0 +1,38 @@ +{ + "id": 2648, + "name": "number-of-ways-to-earn-points", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/number-of-ways-to-earn-points/", + "date": "2023-02-26", + "task_description": "There is a test that has `n` types of questions. You are given an integer `target` and a **0-indexed** 2D integer array `types` where `types[i] = [counti, marksi]` indicates that there are `counti` questions of the `ith` type, and each one of them is worth `marksi` points. Return _the number of ways you can earn **exactly** _`target`_ points in the exam_. Since the answer may be too large, return it **modulo** `109 + 7`. **Note** that questions of the same type are indistinguishable. For example, if there are `3` questions of the same type, then solving the `1st` and `2nd` questions is the same as solving the `1st` and `3rd` questions, or the `2nd` and `3rd` questions. **Example 1:** ``` **Input:** target = 6, types = [[6,1],[3,2],[2,3]] **Output:** 7 **Explanation:** You can earn 6 points in one of the seven ways: - Solve 6 questions of the 0th type: 1 + 1 + 1 + 1 + 1 + 1 = 6 - Solve 4 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 1 + 2 = 6 - Solve 2 questions of the 0th type and 2 questions of the 1st type: 1 + 1 + 2 + 2 = 6 - Solve 3 questions of the 0th type and 1 question of the 2nd type: 1 + 1 + 1 + 3 = 6 - Solve 1 question of the 0th type, 1 question of the 1st type and 1 question of the 2nd type: 1 + 2 + 3 = 6 - Solve 3 questions of the 1st type: 2 + 2 + 2 = 6 - Solve 2 questions of the 2nd type: 3 + 3 = 6 ``` **Example 2:** ``` **Input:** target = 5, types = [[50,1],[50,2],[50,5]] **Output:** 4 **Explanation:** You can earn 5 points in one of the four ways: - Solve 5 questions of the 0th type: 1 + 1 + 1 + 1 + 1 = 5 - Solve 3 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 2 = 5 - Solve 1 questions of the 0th type and 2 questions of the 1st type: 1 + 2 + 2 = 5 - Solve 1 question of the 2nd type: 5 ``` **Example 3:** ``` **Input:** target = 18, types = [[6,1],[3,2],[2,3]] **Output:** 1 **Explanation:** You can only earn 18 points by answering all questions. ``` **Constraints:** `1 <= target <= 1000` `n == types.length` `1 <= n <= 50` `types[i].length == 2` `1 <= counti, marksi <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "target = 6, types = [[6,1],[3,2],[2,3]]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "target = 5, types = [[50,1],[50,2],[50,5]]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "target = 18, types = [[6,1],[3,2],[2,3]]", + "output": "1 " + } + ], + "constraints": [ + "For example, if there are 3 questions of the same type, then solving the 1st and 2nd questions is the same as solving the 1st and 3rd questions, or the 2nd and 3rd questions.", + "1 <= target <= 1000", + "n == types.length", + "1 <= n <= 50", + "types[i].length == 2", + "1 <= counti, marksi <= 50" + ], + "python_template": "class Solution(object):\n def waysToReachTarget(self, target, types):\n \"\"\"\n :type target: int\n :type types: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int waysToReachTarget(int target, int[][] types) {\n \n }\n}", + "metadata": { + "func_name": "waysToReachTarget" + } +} \ No newline at end of file diff --git a/number-of-ways-to-reach-a-position-after-exactly-k-steps.json b/number-of-ways-to-reach-a-position-after-exactly-k-steps.json new file mode 100644 index 0000000000000000000000000000000000000000..a4e2f15c5b63a2849eb69c874e463d1a92a84b40 --- /dev/null +++ b/number-of-ways-to-reach-a-position-after-exactly-k-steps.json @@ -0,0 +1,28 @@ +{ + "id": 2477, + "name": "number-of-ways-to-reach-a-position-after-exactly-k-steps", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/", + "date": "2022-08-28", + "task_description": "You are given two **positive** integers `startPos` and `endPos`. Initially, you are standing at position `startPos` on an **infinite** number line. With one step, you can move either one position to the left, or one position to the right. Given a positive integer `k`, return _the number of **different** ways to reach the position _`endPos`_ starting from _`startPos`_, such that you perform **exactly** _`k`_ steps_. Since the answer may be very large, return it **modulo** `109 + 7`. Two ways are considered different if the order of the steps made is not exactly the same. **Note** that the number line includes negative integers. **Example 1:** ``` **Input:** startPos = 1, endPos = 2, k = 3 **Output:** 3 **Explanation:** We can reach position 2 from 1 in exactly 3 steps in three ways: - 1 -> 2 -> 3 -> 2. - 1 -> 2 -> 1 -> 2. - 1 -> 0 -> 1 -> 2. It can be proven that no other way is possible, so we return 3. ``` **Example 2:** ``` **Input:** startPos = 2, endPos = 5, k = 10 **Output:** 0 **Explanation:** It is impossible to reach position 5 from position 2 in exactly 10 steps. ``` **Constraints:** `1 <= startPos, endPos, k <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "startPos = 1, endPos = 2, k = 3", + "output": "3 " + }, + { + "label": "Example 2", + "input": "startPos = 2, endPos = 5, k = 10", + "output": "0 " + } + ], + "constraints": [ + "1 <= startPos, endPos, k <= 1000" + ], + "python_template": "class Solution(object):\n def numberOfWays(self, startPos, endPos, k):\n \"\"\"\n :type startPos: int\n :type endPos: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfWays(int startPos, int endPos, int k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfWays" + } +} \ No newline at end of file diff --git a/number-of-ways-to-select-buildings.json b/number-of-ways-to-select-buildings.json new file mode 100644 index 0000000000000000000000000000000000000000..5a766894a2adb11124a592481d28f2a6924a6354 --- /dev/null +++ b/number-of-ways-to-select-buildings.json @@ -0,0 +1,32 @@ +{ + "id": 2325, + "name": "number-of-ways-to-select-buildings", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-ways-to-select-buildings/", + "date": "2022-03-19", + "task_description": "You are given a **0-indexed** binary string `s` which represents the types of buildings along a street where: `s[i] = '0'` denotes that the `ith` building is an office and `s[i] = '1'` denotes that the `ith` building is a restaurant. As a city official, you would like to **select** 3 buildings for random inspection. However, to ensure variety, **no two consecutive** buildings out of the **selected** buildings can be of the same type. For example, given `s = \"0**0**1**1**0**1**\"`, we cannot select the `1st`, `3rd`, and `5th` buildings as that would form `\"0**11**\"` which is **not** allowed due to having two consecutive buildings of the same type. Return _the number of valid ways to select 3 buildings._ **Example 1:** ``` **Input:** s = \"001101\" **Output:** 6 **Explanation:** The following sets of indices selected are valid: - [0,2,4] from \"**0**0**1**1**0**1\" forms \"010\" - [0,3,4] from \"**0**01**10**1\" forms \"010\" - [1,2,4] from \"0**01**1**0**1\" forms \"010\" - [1,3,4] from \"0**0**1**10**1\" forms \"010\" - [2,4,5] from \"00**1**1**01**\" forms \"101\" - [3,4,5] from \"001**101**\" forms \"101\" No other selection is valid. Thus, there are 6 total ways. ``` **Example 2:** ``` **Input:** s = \"11100\" **Output:** 0 **Explanation:** It can be shown that there are no valid selections. ``` **Constraints:** `3 <= s.length <= 105` `s[i]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"001101\"", + "output": "6 " + }, + { + "label": "Example 2", + "input": "s = \"11100\"", + "output": "0 " + } + ], + "constraints": [ + "s[i] = '0' denotes that the ith building is an office and", + "s[i] = '1' denotes that the ith building is a restaurant.", + "For example, given s = \"001101\", we cannot select the 1st, 3rd, and 5th buildings as that would form \"011\" which is not allowed due to having two consecutive buildings of the same type.", + "3 <= s.length <= 105", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def numberOfWays(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long numberOfWays(String s) {\n \n }\n}", + "metadata": { + "func_name": "numberOfWays" + } +} \ No newline at end of file diff --git a/number-of-ways-to-split-array.json b/number-of-ways-to-split-array.json new file mode 100644 index 0000000000000000000000000000000000000000..ddb1ce706b6687e810b8351f2c1e4c878c5bd402 --- /dev/null +++ b/number-of-ways-to-split-array.json @@ -0,0 +1,31 @@ +{ + "id": 2358, + "name": "number-of-ways-to-split-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-ways-to-split-array/", + "date": "2022-04-30", + "task_description": "You are given a **0-indexed** integer array `nums` of length `n`. `nums` contains a **valid split** at index `i` if the following are true: The sum of the first `i + 1` elements is **greater than or equal to** the sum of the last `n - i - 1` elements. There is **at least one** element to the right of `i`. That is, `0 <= i < n - 1`. Return _the number of **valid splits** in_ `nums`. **Example 1:** ``` **Input:** nums = [10,4,-8,7] **Output:** 2 **Explanation:** There are three ways of splitting nums into two non-empty parts: - Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split. - Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split. - Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split. Thus, the number of valid splits in nums is 2. ``` **Example 2:** ``` **Input:** nums = [2,3,1,0] **Output:** 2 **Explanation:** There are two valid splits in nums: - Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split. - Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split. ``` **Constraints:** `2 <= nums.length <= 105` `-105 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [10,4,-8,7]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [2,3,1,0]", + "output": "2 " + } + ], + "constraints": [ + "The sum of the first i + 1 elements is greater than or equal to the sum of the last n - i - 1 elements.", + "There is at least one element to the right of i. That is, 0 <= i < n - 1.", + "2 <= nums.length <= 105", + "-105 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def waysToSplitArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int waysToSplitArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "waysToSplitArray" + } +} \ No newline at end of file diff --git a/number-of-zero-filled-subarrays.json b/number-of-zero-filled-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..0cfa3ed3e330336376681163ed91b1d0f28e4643 --- /dev/null +++ b/number-of-zero-filled-subarrays.json @@ -0,0 +1,34 @@ +{ + "id": 2432, + "name": "number-of-zero-filled-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/number-of-zero-filled-subarrays/", + "date": "2022-07-09", + "task_description": "Given an integer array `nums`, return _the number of **subarrays** filled with _`0`. A **subarray** is a contiguous non-empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,3,0,0,2,0,0,4] **Output:** 6 **Explanation:** There are 4 occurrences of [0] as a subarray. There are 2 occurrences of [0,0] as a subarray. There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6. ``` **Example 2:** ``` **Input:** nums = [0,0,0,2,0,0] **Output:** 9 **Explanation: **There are 5 occurrences of [0] as a subarray. There are 3 occurrences of [0,0] as a subarray. There is 1 occurrence of [0,0,0] as a subarray. There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9. ``` **Example 3:** ``` **Input:** nums = [2,10,2019] **Output:** 0 **Explanation:** There is no subarray filled with 0. Therefore, we return 0. ``` **Constraints:** `1 <= nums.length <= 105` `-109 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,0,0,2,0,0,4]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [0,0,0,2,0,0]", + "output": "9 " + }, + { + "label": "Example 3", + "input": "nums = [2,10,2019]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "-109 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def zeroFilledSubarray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long zeroFilledSubarray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "zeroFilledSubarray" + } +} \ No newline at end of file diff --git a/odd-string-difference.json b/odd-string-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..4ff6675e404bd0e71960cfbee9809297f3de0212 --- /dev/null +++ b/odd-string-difference.json @@ -0,0 +1,32 @@ +{ + "id": 2547, + "name": "odd-string-difference", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/odd-string-difference/", + "date": "2022-10-15", + "task_description": "You are given an array of equal-length strings `words`. Assume that the length of each string is `n`. Each string `words[i]` can be converted into a **difference integer array** `difference[i]` of length `n - 1` where `difference[i][j] = words[i][j+1] - words[i][j]` where `0 <= j <= n - 2`. Note that the difference between two letters is the difference between their **positions** in the alphabet i.e. the position of `'a'` is `0`, `'b'` is `1`, and `'z'` is `25`. For example, for the string `\"acb\"`, the difference integer array is `[2 - 0, 1 - 2] = [2, -1]`. All the strings in words have the same difference integer array, **except one**. You should find that string. Return_ the string in _`words`_ that has different **difference integer array**._ **Example 1:** ``` **Input:** words = [\"adc\",\"wzy\",\"abc\"] **Output:** \"abc\" **Explanation:** - The difference integer array of \"adc\" is [3 - 0, 2 - 3] = [3, -1]. - The difference integer array of \"wzy\" is [25 - 22, 24 - 25]= [3, -1]. - The difference integer array of \"abc\" is [1 - 0, 2 - 1] = [1, 1]. The odd array out is [1, 1], so we return the corresponding string, \"abc\". ``` **Example 2:** ``` **Input:** words = [\"aaa\",\"bob\",\"ccc\",\"ddd\"] **Output:** \"bob\" **Explanation:** All the integer arrays are [0, 0] except for \"bob\", which corresponds to [13, -13]. ``` **Constraints:** `3 <= words.length <= 100` `n == words[i].length` `2 <= n <= 20` `words[i]` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"adc\",\"wzy\",\"abc\"]", + "output": "\"abc\" " + }, + { + "label": "Example 2", + "input": "words = [\"aaa\",\"bob\",\"ccc\",\"ddd\"]", + "output": "\"bob\" " + } + ], + "constraints": [ + "For example, for the string \"acb\", the difference integer array is [2 - 0, 1 - 2] = [2, -1].", + "3 <= words.length <= 100", + "n == words[i].length", + "2 <= n <= 20", + "words[i] consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def oddString(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String oddString(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "oddString" + } +} \ No newline at end of file diff --git a/optimal-partition-of-string.json b/optimal-partition-of-string.json new file mode 100644 index 0000000000000000000000000000000000000000..4101a7e7a94beb54928654b76f0d1b5c84444c05 --- /dev/null +++ b/optimal-partition-of-string.json @@ -0,0 +1,29 @@ +{ + "id": 2487, + "name": "optimal-partition-of-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/optimal-partition-of-string/", + "date": "2022-09-04", + "task_description": "Given a string `s`, partition the string into one or more **substrings** such that the characters in each substring are **unique**. That is, no letter appears in a single substring more than **once**. Return _the **minimum** number of substrings in such a partition._ Note that each character should belong to exactly one substring in a partition. **Example 1:** ``` **Input:** s = \"abacaba\" **Output:** 4 **Explanation:** Two possible partitions are (\"a\",\"ba\",\"cab\",\"a\") and (\"ab\",\"a\",\"ca\",\"ba\"). It can be shown that 4 is the minimum number of substrings needed. ``` **Example 2:** ``` **Input:** s = \"ssssss\" **Output:** 6 **Explanation: **The only valid partition is (\"s\",\"s\",\"s\",\"s\",\"s\",\"s\"). ``` **Constraints:** `1 <= s.length <= 105` `s` consists of only English lowercase letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abacaba\"", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"ssssss\"", + "output": "6 " + } + ], + "constraints": [ + "1 <= s.length <= 105", + "s consists of only English lowercase letters." + ], + "python_template": "class Solution(object):\n def partitionString(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int partitionString(String s) {\n \n }\n}", + "metadata": { + "func_name": "partitionString" + } +} \ No newline at end of file diff --git a/paint-house-iv.json b/paint-house-iv.json new file mode 100644 index 0000000000000000000000000000000000000000..9e5945d20c6fade7275dde8bcb288fe783803256 --- /dev/null +++ b/paint-house-iv.json @@ -0,0 +1,41 @@ +{ + "id": 3737, + "name": "paint-house-iv", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/paint-house-iv/", + "date": "2025-01-12", + "task_description": "You are given an **even** integer `n` representing the number of houses arranged in a straight line, and a 2D array `cost` of size `n x 3`, where `cost[i][j]` represents the cost of painting house `i` with color `j + 1`. The houses will look **beautiful** if they satisfy the following conditions: No **two** adjacent houses are painted the same color. Houses **equidistant** from the ends of the row are **not** painted the same color. For example, if `n = 6`, houses at positions `(0, 5)`, `(1, 4)`, and `(2, 3)` are considered equidistant. Return the **minimum** cost to paint the houses such that they look **beautiful**. **Example 1:** **Input:** n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]] **Output:** 9 **Explanation:** The optimal painting sequence is `[1, 2, 3, 2]` with corresponding costs `[3, 2, 1, 3]`. This satisfies the following conditions: No adjacent houses have the same color. Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color `(1 != 2)`. Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color `(2 != 3)`. The minimum cost to paint the houses so that they look beautiful is `3 + 2 + 1 + 3 = 9`. **Example 2:** **Input:** n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]] **Output:** 18 **Explanation:** The optimal painting sequence is `[1, 3, 2, 3, 1, 2]` with corresponding costs `[2, 8, 1, 2, 3, 2]`. This satisfies the following conditions: No adjacent houses have the same color. Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color `(1 != 2)`. Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color `(3 != 1)`. Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color `(2 != 3)`. The minimum cost to paint the houses so that they look beautiful is `2 + 8 + 1 + 2 + 3 + 2 = 18`. **Constraints:** `2 <= n <= 105` `n` is even. `cost.length == n` `cost[i].length == 3` `0 <= cost[i][j] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]]", + "output": "18 " + } + ], + "constraints": [ + "No two adjacent houses are painted the same color.", + "Houses equidistant from the ends of the row are not painted the same color. For example, if n = 6, houses at positions (0, 5), (1, 4), and (2, 3) are considered equidistant.", + "No adjacent houses have the same color.", + "Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color (1 != 2).", + "Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color (2 != 3).", + "No adjacent houses have the same color.", + "Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color (1 != 2).", + "Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color (3 != 1).", + "Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color (2 != 3).", + "2 <= n <= 105", + "n is even.", + "cost.length == n", + "cost[i].length == 3", + "0 <= cost[i][j] <= 105" + ], + "python_template": "class Solution(object):\n def minCost(self, n, cost):\n \"\"\"\n :type n: int\n :type cost: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minCost(int n, int[][] cost) {\n \n }\n}", + "metadata": { + "func_name": "minCost" + } +} \ No newline at end of file diff --git a/painting-the-walls.json b/painting-the-walls.json new file mode 100644 index 0000000000000000000000000000000000000000..38df6f2c6cf7442a7e314f6ad7a779f1e361dbbb --- /dev/null +++ b/painting-the-walls.json @@ -0,0 +1,33 @@ +{ + "id": 2808, + "name": "painting-the-walls", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/painting-the-walls/", + "date": "2023-06-11", + "task_description": "You are given two **0-indexed** integer arrays, `cost` and `time`, of size `n` representing the costs and the time taken to paint `n` different walls respectively. There are two painters available: A** paid painter** that paints the `ith` wall in `time[i]` units of time and takes `cost[i]` units of money. A** free painter** that paints **any** wall in `1` unit of time at a cost of `0`. But the free painter can only be used if the paid painter is already **occupied**. Return _the minimum amount of money required to paint the _`n`_ walls._ **Example 1:** ``` **Input:** cost = [1,2,3,2], time = [1,2,3,2] **Output:** 3 **Explanation:** The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3. ``` **Example 2:** ``` **Input:** cost = [2,3,4,2], time = [1,1,1,1] **Output:** 4 **Explanation:** The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4. ``` **Constraints:** `1 <= cost.length <= 500` `cost.length == time.length` `1 <= cost[i] <= 106` `1 <= time[i] <= 500`", + "test_case": [ + { + "label": "Example 1", + "input": "cost = [1,2,3,2], time = [1,2,3,2]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "cost = [2,3,4,2], time = [1,1,1,1]", + "output": "4 " + } + ], + "constraints": [ + "AĀ paid painterĀ that paints the ith wall in time[i] units of time and takes cost[i] units of money.", + "AĀ free painter that paintsĀ any wall in 1 unit of time at a cost of 0. But theĀ free painter can only be used if the paid painter is already occupied.", + "1 <= cost.length <= 500", + "cost.length == time.length", + "1 <= cost[i] <= 106", + "1 <= time[i] <= 500" + ], + "python_template": "class Solution(object):\n def paintWalls(self, cost, time):\n \"\"\"\n :type cost: List[int]\n :type time: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int paintWalls(int[] cost, int[] time) {\n \n }\n}", + "metadata": { + "func_name": "paintWalls" + } +} \ No newline at end of file diff --git a/partition-array-according-to-given-pivot.json b/partition-array-according-to-given-pivot.json new file mode 100644 index 0000000000000000000000000000000000000000..a573b2f373ec24c5262ad268733395791860ffa4 --- /dev/null +++ b/partition-array-according-to-given-pivot.json @@ -0,0 +1,35 @@ +{ + "id": 2265, + "name": "partition-array-according-to-given-pivot", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/partition-array-according-to-given-pivot/", + "date": "2022-01-22", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `pivot`. Rearrange `nums` such that the following conditions are satisfied: Every element less than `pivot` appears **before** every element greater than `pivot`. Every element equal to `pivot` appears **in between** the elements less than and greater than `pivot`. The **relative order** of the elements less than `pivot` and the elements greater than `pivot` is maintained. More formally, consider every `pi`, `pj` where `pi` is the new position of the `ith` element and `pj` is the new position of the `jth` element. If `i < j` and **both** elements are smaller (_or larger_) than `pivot`, then `pi < pj`. Return `nums`_ after the rearrangement._ **Example 1:** ``` **Input:** nums = [9,12,5,10,14,3,10], pivot = 10 **Output:** [9,5,3,10,10,12,14] **Explanation:** The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array. The elements 12 and 14 are greater than the pivot so they are on the right side of the array. The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings. ``` **Example 2:** ``` **Input:** nums = [-3,4,3,2], pivot = 2 **Output:** [-3,2,4,3] **Explanation:** The element -3 is less than the pivot so it is on the left side of the array. The elements 4 and 3 are greater than the pivot so they are on the right side of the array. The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings. ``` **Constraints:** `1 <= nums.length <= 105` `-106 <= nums[i] <= 106` `pivot` equals to an element of `nums`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [9,12,5,10,14,3,10], pivot = 10", + "output": "[9,5,3,10,10,12,14] " + }, + { + "label": "Example 2", + "input": "nums = [-3,4,3,2], pivot = 2", + "output": "[-3,2,4,3] " + } + ], + "constraints": [ + "Every element less than pivot appears before every element greater than pivot.", + "Every element equal to pivot appears in between the elements less than and greater than pivot.", + "The relative order of the elements less than pivot and the elements greater than pivot is maintained.\n\t\nMore formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.", + "More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.", + "More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.", + "1 <= nums.length <= 105", + "-106 <= nums[i] <= 106", + "pivot equals to an element of nums." + ], + "python_template": "class Solution(object):\n def pivotArray(self, nums, pivot):\n \"\"\"\n :type nums: List[int]\n :type pivot: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] pivotArray(int[] nums, int pivot) {\n \n }\n}", + "metadata": { + "func_name": "pivotArray" + } +} \ No newline at end of file diff --git a/partition-array-such-that-maximum-difference-is-k.json b/partition-array-such-that-maximum-difference-is-k.json new file mode 100644 index 0000000000000000000000000000000000000000..2ca6900dc91371f6a3910635fec694dab59d5334 --- /dev/null +++ b/partition-array-such-that-maximum-difference-is-k.json @@ -0,0 +1,35 @@ +{ + "id": 2387, + "name": "partition-array-such-that-maximum-difference-is-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/", + "date": "2022-05-29", + "task_description": "You are given an integer array `nums` and an integer `k`. You may partition `nums` into one or more **subsequences** such that each element in `nums` appears in **exactly** one of the subsequences. Return _the **minimum **number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is **at most** _`k`_._ A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. **Example 1:** ``` **Input:** nums = [3,6,1,2,5], k = 2 **Output:** 2 **Explanation:** We can partition nums into the two subsequences [3,1,2] and [6,5]. The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2. The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1. Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed. ``` **Example 2:** ``` **Input:** nums = [1,2,3], k = 1 **Output:** 2 **Explanation:** We can partition nums into the two subsequences [1,2] and [3]. The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1. The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0. Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3]. ``` **Example 3:** ``` **Input:** nums = [2,2,4,5], k = 0 **Output:** 3 **Explanation:** We can partition nums into the three subsequences [2,2], [4], and [5]. The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0. The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0. The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0. Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 105` `0 <= k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,6,1,2,5], k = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3], k = 1", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [2,2,4,5], k = 0", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i] <= 105", + "0 <= k <= 105" + ], + "python_template": "class Solution(object):\n def partitionArray(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int partitionArray(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "partitionArray" + } +} \ No newline at end of file diff --git a/partition-string-into-minimum-beautiful-substrings.json b/partition-string-into-minimum-beautiful-substrings.json new file mode 100644 index 0000000000000000000000000000000000000000..665cfbca13479d4455b12e7d04fc3f70eea36135 --- /dev/null +++ b/partition-string-into-minimum-beautiful-substrings.json @@ -0,0 +1,36 @@ +{ + "id": 2883, + "name": "partition-string-into-minimum-beautiful-substrings", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/partition-string-into-minimum-beautiful-substrings/", + "date": "2023-06-24", + "task_description": "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 impossible to partition the string `s` into beautiful substrings, return `-1`. A **substring** is a contiguous sequence of characters in a string. **Example 1:** ``` **Input:** s = \"1011\" **Output:** 2 **Explanation:** We can paritition the given string into [\"101\", \"1\"]. - The string \"101\" does not contain leading zeros and is the binary representation of integer 51 = 5. - The string \"1\" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into. ``` **Example 2:** ``` **Input:** s = \"111\" **Output:** 3 **Explanation:** We can paritition the given string into [\"1\", \"1\", \"1\"]. - The string \"1\" does not contain leading zeros and is the binary representation of integer 50 = 1. It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into. ``` **Example 3:** ``` **Input:** s = \"0\" **Output:** -1 **Explanation:** We can not partition the given string into beautiful substrings. ``` **Constraints:** `1 <= s.length <= 15` `s[i]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"1011\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"111\"", + "output": "3 " + }, + { + "label": "Example 3", + "input": "s = \"0\"", + "output": "-1 " + } + ], + "constraints": [ + "It doesn't contain leading zeros.", + "It's the binary representation of a number that is a power of 5.", + "1 <= s.length <= 15", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def minimumBeautifulSubstrings(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumBeautifulSubstrings(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimumBeautifulSubstrings" + } +} \ No newline at end of file diff --git a/partition-string-into-substrings-with-values-at-most-k.json b/partition-string-into-substrings-with-values-at-most-k.json new file mode 100644 index 0000000000000000000000000000000000000000..efb669bb97496a0b02d805d30a4af1d4275434ff --- /dev/null +++ b/partition-string-into-substrings-with-values-at-most-k.json @@ -0,0 +1,34 @@ +{ + "id": 2511, + "name": "partition-string-into-substrings-with-values-at-most-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/partition-string-into-substrings-with-values-at-most-k/", + "date": "2022-12-25", + "task_description": "You are given a string `s` consisting of digits from `1` to `9` and an integer `k`. A partition of a string `s` is called **good** if: Each digit of `s` is part of **exactly** one substring. The value of each substring is less than or equal to `k`. Return _the **minimum** number of substrings in a **good** partition of_ `s`. If no **good** partition of `s` exists, return `-1`. Note that: The **value** of a string is its result when interpreted as an integer. For example, the value of `\"123\"` is `123` and the value of `\"1\"` is `1`. A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` **Input:** s = \"165462\", k = 60 **Output:** 4 **Explanation:** We can partition the string into substrings \"16\", \"54\", \"6\", and \"2\". Each substring has a value less than or equal to k = 60. It can be shown that we cannot partition the string into less than 4 substrings. ``` **Example 2:** ``` **Input:** s = \"238182\", k = 5 **Output:** -1 **Explanation:** There is no good partition for this string. ``` **Constraints:** `1 <= s.length <= 105` `s[i]` is a digit from `'1'` to `'9'`. `1 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"165462\", k = 60", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"238182\", k = 5", + "output": "-1 " + } + ], + "constraints": [ + "Each digit of s is part of exactly one substring.", + "The value of each substring is less than or equal to k.", + "The value of a string is its result when interpreted as an integer. For example, the value of \"123\" is 123 and the value of \"1\" is 1.", + "A substring is a contiguous sequence of characters within a string.", + "1 <= s.length <= 105", + "s[i] is a digit from '1' to '9'.", + "1 <= k <= 109" + ], + "python_template": "class Solution(object):\n def minimumPartition(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumPartition(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumPartition" + } +} \ No newline at end of file diff --git a/pass-the-pillow.json b/pass-the-pillow.json new file mode 100644 index 0000000000000000000000000000000000000000..1379e1a7c7832ae65df695de97eb29406078ecc1 --- /dev/null +++ b/pass-the-pillow.json @@ -0,0 +1,30 @@ +{ + "id": 2645, + "name": "pass-the-pillow", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/pass-the-pillow/", + "date": "2023-02-26", + "task_description": "There are `n` people standing in a line labeled from `1` to `n`. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction. For example, once the pillow reaches the `nth` person they pass it to the `n - 1th` person, then to the `n - 2th` person and so on. Given the two positive integers `n` and `time`, return _the index of the person holding the pillow after _`time`_ seconds_. **Example 1:** ``` **Input:** n = 4, time = 5 **Output:** 2 **Explanation:** People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2. After five seconds, the 2nd person is holding the pillow. ``` **Example 2:** ``` **Input:** n = 3, time = 2 **Output:** 3 **Explanation:** People pass the pillow in the following way: 1 -> 2 -> 3. After two seconds, the 3rd person is holding the pillow. ``` **Constraints:** `2 <= n <= 1000` `1 <= time <= 1000` **Note:** This question is the same as 3178: Find the Child Who Has the Ball After K Seconds.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, time = 5", + "output": "2 " + }, + { + "label": "Example 2", + "input": "n = 3, time = 2", + "output": "3 " + } + ], + "constraints": [ + "For example, once the pillow reaches the nth person they pass it to the n - 1th person, then to the n - 2th person and so on.", + "2 <= n <= 1000", + "1 <= time <= 1000" + ], + "python_template": "class Solution(object):\n def passThePillow(self, n, time):\n \"\"\"\n :type n: int\n :type time: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int passThePillow(int n, int time) {\n \n }\n}", + "metadata": { + "func_name": "passThePillow" + } +} \ No newline at end of file diff --git a/paths-in-matrix-whose-sum-is-divisible-by-k.json b/paths-in-matrix-whose-sum-is-divisible-by-k.json new file mode 100644 index 0000000000000000000000000000000000000000..8cdacfc24a633a8d152101fd21b8e0431fd4a345 --- /dev/null +++ b/paths-in-matrix-whose-sum-is-divisible-by-k.json @@ -0,0 +1,38 @@ +{ + "id": 2521, + "name": "paths-in-matrix-whose-sum-is-divisible-by-k", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/", + "date": "2022-10-02", + "task_description": "You are given a **0-indexed** `m x n` integer matrix `grid` and an integer `k`. You are currently at position `(0, 0)` and you want to reach position `(m - 1, n - 1)` moving only **down** or **right**. Return_ the number of paths where the sum of the elements on the path is divisible by _`k`. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3 **Output:** 2 **Explanation:** There are two paths where the sum of the elements on the path is divisible by k. The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3. The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3. ``` **Example 2:** ``` **Input:** grid = [[0,0]], k = 5 **Output:** 1 **Explanation:** The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5. ``` **Example 3:** ``` **Input:** grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1 **Output:** 10 **Explanation:** Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 5 * 104` `1 <= m * n <= 5 * 104` `0 <= grid[i][j] <= 100` `1 <= k <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3", + "output": "2 " + }, + { + "label": "Example 2", + "input": "grid = [[0,0]], k = 5", + "output": "1 " + }, + { + "label": "Example 3", + "input": "grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1", + "output": "10 " + } + ], + "constraints": [ + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 5 * 104", + "1 <= m * n <= 5 * 104", + "0 <= grid[i][j] <= 100", + "1 <= k <= 50" + ], + "python_template": "class Solution(object):\n def numberOfPaths(self, grid, k):\n \"\"\"\n :type grid: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfPaths(int[][] grid, int k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPaths" + } +} \ No newline at end of file diff --git a/peaks-in-array.json b/peaks-in-array.json new file mode 100644 index 0000000000000000000000000000000000000000..c235ea538860cb6c00ab1693de987e43587d1808 --- /dev/null +++ b/peaks-in-array.json @@ -0,0 +1,39 @@ +{ + "id": 3438, + "name": "peaks-in-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/peaks-in-array/", + "date": "2024-06-09", + "task_description": "A **peak** in an array `arr` is an element that is **greater** than its previous and next element in `arr`. You are given an integer array `nums` and a 2D integer array `queries`. You have to process queries of two types: `queries[i] = [1, li, ri]`, determine the count of **peak** elements in the subarray `nums[li..ri]`. `queries[i] = [2, indexi, vali]`, change `nums[indexi]` to `vali`. Return an array `answer` containing the results of the queries of the first type in order. **Notes:** The **first** and the **last** element of an array or a subarray **cannot** be a peak. **Example 1:** **Input:** nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]] **Output:** [0] **Explanation:** First query: We change `nums[3]` to 4 and `nums` becomes `[3,1,4,4,5]`. Second query: The number of peaks in the `[3,1,4,4,5]` is 0. **Example 2:** **Input:** nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]] **Output:** [0,1] **Explanation:** First query: `nums[2]` should become 4, but it is already set to 4. Second query: The number of peaks in the `[4,1,4]` is 0. Third query: The second 4 is a peak in the `[4,1,4,2,1]`. **Constraints:** `3 <= nums.length <= 105` `1 <= nums[i] <= 105` `1 <= queries.length <= 105` `queries[i][0] == 1` or `queries[i][0] == 2` For all `i` that: `queries[i][0] == 1`: `0 <= queries[i][1] <= queries[i][2] <= nums.length - 1` `queries[i][0] == 2`: `0 <= queries[i][1] <= nums.length - 1`, `1 <= queries[i][2] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]", + "output": "[0] " + }, + { + "label": "Example 2", + "input": "nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]", + "output": "[0,1] " + } + ], + "constraints": [ + "queries[i] = [1, li, ri], determine the count of peak elements in the subarray nums[li..ri].", + "queries[i] = [2, indexi, vali], change nums[indexi] to vali.", + "The first and the last element of an array or a subarray cannot be a peak.", + "3 <= nums.length <= 105", + "1 <= nums[i] <= 105", + "1 <= queries.length <= 105", + "queries[i][0] == 1 or queries[i][0] == 2", + "For all i that:\n\t\nqueries[i][0] == 1: 0 <= queries[i][1] <= queries[i][2] <= nums.length - 1\nqueries[i][0] == 2: 0 <= queries[i][1] <= nums.length - 1, 1 <= queries[i][2] <= 105", + "queries[i][0] == 1: 0 <= queries[i][1] <= queries[i][2] <= nums.length - 1", + "queries[i][0] == 2: 0 <= queries[i][1] <= nums.length - 1, 1 <= queries[i][2] <= 105", + "queries[i][0] == 1: 0 <= queries[i][1] <= queries[i][2] <= nums.length - 1", + "queries[i][0] == 2: 0 <= queries[i][1] <= nums.length - 1, 1 <= queries[i][2] <= 105" + ], + "python_template": "class Solution(object):\n def countOfPeaks(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List countOfPeaks(int[] nums, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "countOfPeaks" + } +} \ No newline at end of file diff --git a/percentage-of-letter-in-string.json b/percentage-of-letter-in-string.json new file mode 100644 index 0000000000000000000000000000000000000000..4acc989ab47d41d3b5f49c9c012dfa056e15c628 --- /dev/null +++ b/percentage-of-letter-in-string.json @@ -0,0 +1,30 @@ +{ + "id": 2365, + "name": "percentage-of-letter-in-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/percentage-of-letter-in-string/", + "date": "2022-05-15", + "task_description": "Given a string `s` and a character `letter`, return_ the **percentage** of characters in _`s`_ that equal _`letter`_ **rounded down** to the nearest whole percent._ **Example 1:** ``` **Input:** s = \"foobar\", letter = \"o\" **Output:** 33 **Explanation:** The percentage of characters in s that equal the letter 'o' is 2 / 6 * 100% = 33% when rounded down, so we return 33. ``` **Example 2:** ``` **Input:** s = \"jjjj\", letter = \"k\" **Output:** 0 **Explanation:** The percentage of characters in s that equal the letter 'k' is 0%, so we return 0. ``` **Constraints:** `1 <= s.length <= 100` `s` consists of lowercase English letters. `letter` is a lowercase English letter.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"foobar\", letter = \"o\"", + "output": "33 " + }, + { + "label": "Example 2", + "input": "s = \"jjjj\", letter = \"k\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "s consists of lowercase English letters.", + "letter is a lowercase English letter." + ], + "python_template": "class Solution(object):\n def percentageLetter(self, s, letter):\n \"\"\"\n :type s: str\n :type letter: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int percentageLetter(String s, char letter) {\n \n }\n}", + "metadata": { + "func_name": "percentageLetter" + } +} \ No newline at end of file diff --git a/permutation-difference-between-two-strings.json b/permutation-difference-between-two-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..203097bc6d679ff1f1eae71d684e1dcd0e48318b --- /dev/null +++ b/permutation-difference-between-two-strings.json @@ -0,0 +1,34 @@ +{ + "id": 3412, + "name": "permutation-difference-between-two-strings", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/permutation-difference-between-two-strings/", + "date": "2024-05-05", + "task_description": "You are given two strings `s` and `t` such that every character occurs at most once in `s` and `t` is a permutation of `s`. The **permutation difference** between `s` and `t` is defined as the **sum** of the absolute difference between the index of the occurrence of each character in `s` and the index of the occurrence of the same character in `t`. Return the **permutation difference** between `s` and `t`. **Example 1:** **Input:** s = \"abc\", t = \"bac\" **Output:** 2 **Explanation:** For `s = \"abc\"` and `t = \"bac\"`, the permutation difference of `s` and `t` is equal to the sum of: The absolute difference between the index of the occurrence of `\"a\"` in `s` and the index of the occurrence of `\"a\"` in `t`. The absolute difference between the index of the occurrence of `\"b\"` in `s` and the index of the occurrence of `\"b\"` in `t`. The absolute difference between the index of the occurrence of `\"c\"` in `s` and the index of the occurrence of `\"c\"` in `t`. That is, the permutation difference between `s` and `t` is equal to `|0 - 1| + |1 - 0| + |2 - 2| = 2`. **Example 2:** **Input:** s = \"abcde\", t = \"edbac\" **Output:** 12 **Explanation:** The permutation difference between `s` and `t` is equal to `|0 - 3| + |1 - 2| + |2 - 4| + |3 - 1| + |4 - 0| = 12`. **Constraints:** `1 <= s.length <= 26` Each character occurs at most once in `s`. `t` is a permutation of `s`. `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abc\", t = \"bac\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abcde\", t = \"edbac\"", + "output": "12 " + } + ], + "constraints": [ + "The absolute difference between the index of the occurrence of \"a\" in s and the index of the occurrence of \"a\" in t.", + "The absolute difference between the index of the occurrence of \"b\" in s and the index of the occurrence of \"b\" in t.", + "The absolute difference between the index of the occurrence of \"c\" in s and the index of the occurrence of \"c\" in t.", + "1 <= s.length <= 26", + "Each character occurs at most once in s.", + "t is a permutation of s.", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def findPermutationDifference(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findPermutationDifference(String s, String t) {\n \n }\n}", + "metadata": { + "func_name": "findPermutationDifference" + } +} \ No newline at end of file diff --git a/permutations-iv.json b/permutations-iv.json new file mode 100644 index 0000000000000000000000000000000000000000..4ae25a280bdd00373343d38886c85a2590e4455f --- /dev/null +++ b/permutations-iv.json @@ -0,0 +1,34 @@ +{ + "id": 3783, + "name": "permutations-iv", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/permutations-iv/", + "date": "2025-02-15", + "task_description": "Given two integers, `n` and `k`, an **alternating permutation** is a permutation of the first `n` positive integers such that no **two** adjacent elements are both odd or both even. Return the **k-th** **alternating permutation** sorted in _lexicographical order_. If there are fewer than `k` valid **alternating permutations**, return an empty list. **Example 1:** **Input:** n = 4, k = 6 **Output:** [3,4,1,2] **Explanation:** The lexicographically-sorted alternating permutations of `[1, 2, 3, 4]` are: `[1, 2, 3, 4]` `[1, 4, 3, 2]` `[2, 1, 4, 3]` `[2, 3, 4, 1]` `[3, 2, 1, 4]` `[3, 4, 1, 2]` ← 6th permutation `[4, 1, 2, 3]` `[4, 3, 2, 1]` Since `k = 6`, we return `[3, 4, 1, 2]`. **Example 2:** **Input:** n = 3, k = 2 **Output:** [3,2,1] **Explanation:** The lexicographically-sorted alternating permutations of `[1, 2, 3]` are: `[1, 2, 3]` `[3, 2, 1]` ← 2nd permutation Since `k = 2`, we return `[3, 2, 1]`. **Example 3:** **Input:** n = 2, k = 3 **Output:** [] **Explanation:** The lexicographically-sorted alternating permutations of `[1, 2]` are: `[1, 2]` `[2, 1]` There are only 2 alternating permutations, but `k = 3`, which is out of range. Thus, we return an empty list `[]`. **Constraints:** `1 <= n <= 100` `1 <= k <= 1015`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, k = 6", + "output": "[3,4,1,2] " + }, + { + "label": "Example 2", + "input": "n = 3, k = 2", + "output": "[3,2,1] " + }, + { + "label": "Example 3", + "input": "n = 2, k = 3", + "output": "[] " + } + ], + "constraints": [ + "1 <= n <= 100", + "1 <= k <= 1015" + ], + "python_template": "class Solution(object):\n def permute(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] permute(int n, long k) {\n \n }\n}", + "metadata": { + "func_name": "permute" + } +} \ No newline at end of file diff --git a/points-that-intersect-with-cars.json b/points-that-intersect-with-cars.json new file mode 100644 index 0000000000000000000000000000000000000000..43b42f6af2d8e3a43cb5e5638846175f8664cfab --- /dev/null +++ b/points-that-intersect-with-cars.json @@ -0,0 +1,30 @@ +{ + "id": 3034, + "name": "points-that-intersect-with-cars", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/points-that-intersect-with-cars/", + "date": "2023-09-03", + "task_description": "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 with **any part** of a car._ **Example 1:** ``` **Input:** nums = [[3,6],[1,5],[4,7]] **Output:** 7 **Explanation:** All the points from 1 to 7 intersect at least one car, therefore the answer would be 7. ``` **Example 2:** ``` **Input:** nums = [[1,3],[5,8]] **Output:** 7 **Explanation:** Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7. ``` **Constraints:** `1 <= nums.length <= 100` `nums[i].length == 2` `1 <= starti <= endi <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [[3,6],[1,5],[4,7]]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [[1,3],[5,8]]", + "output": "7 " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "nums[i].length == 2", + "1 <= startiĀ <= endiĀ <= 100" + ], + "python_template": "class Solution(object):\n def numberOfPoints(self, nums):\n \"\"\"\n :type nums: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfPoints(List> nums) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPoints" + } +} \ No newline at end of file diff --git a/power-of-heroes.json b/power-of-heroes.json new file mode 100644 index 0000000000000000000000000000000000000000..6f709e6fa8a4edaa74bff61e092b773cbaa3d2dc --- /dev/null +++ b/power-of-heroes.json @@ -0,0 +1,30 @@ +{ + "id": 2784, + "name": "power-of-heroes", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/power-of-heroes/", + "date": "2023-04-29", + "task_description": "You are given a **0-indexed** integer array `nums` representing the strength of some heroes. The power of a group of heroes is defined as follows: Let `i0`, `i1`, ... ,`ik` be the indices of the heroes in a group. Then, the power of this group is `max(nums[i0], nums[i1], ... ,nums[ik])2 * min(nums[i0], nums[i1], ... ,nums[ik])`. Return _the sum of the **power** of all **non-empty** groups of heroes possible._ Since the sum could be very large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** nums = [2,1,4] **Output:** 141 **Explanation:** 1st group: [2] has power = 22 * 2 = 8. 2nd group: [1] has power = 12 * 1 = 1. 3rd group: [4] has power = 42 * 4 = 64. 4th group: [2,1] has power = 22 * 1 = 4. 5th group: [2,4] has power = 42 * 2 = 32. 6th group: [1,4] has power = 42 * 1 = 16. ​​​​​​​7th group: [2,1,4] has power = 42​​​​​​​ * 1 = 16. The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141. ``` **Example 2:** ``` **Input:** nums = [1,1,1] **Output:** 7 **Explanation:** A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,4]", + "output": "141 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1]", + "output": "7 " + } + ], + "constraints": [ + "Let i0, i1, ... ,ik be the indices of the heroes in a group. Then, the power of this group is max(nums[i0], nums[i1], ... ,nums[ik])2 * min(nums[i0], nums[i1], ... ,nums[ik]).", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def sumOfPower(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumOfPower(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "sumOfPower" + } +} \ No newline at end of file diff --git a/prime-in-diagonal.json b/prime-in-diagonal.json new file mode 100644 index 0000000000000000000000000000000000000000..4266bb0c499350800e7c8d9cfe552886c2195814 --- /dev/null +++ b/prime-in-diagonal.json @@ -0,0 +1,32 @@ +{ + "id": 2722, + "name": "prime-in-diagonal", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/prime-in-diagonal/", + "date": "2023-04-02", + "task_description": "You are given a 0-indexed two-dimensional integer array `nums`. Return _the largest **prime** number that lies on at least one of the diagonals of _`nums`. In case, no prime is present on any of the diagonals, return_ 0._ Note that: An integer is **prime** if it is greater than `1` and has no positive integer divisors other than `1` and itself. An integer `val` is on one of the **diagonals** of `nums` if there exists an integer `i` for which `nums[i][i] = val` or an `i` for which `nums[i][nums.length - i - 1] = val`. In the above diagram, one diagonal is **[1,5,9]** and another diagonal is** [3,5,7]**. **Example 1:** ``` **Input:** nums = [[1,2,3],[5,6,7],[9,10,11]] **Output:** 11 **Explanation:** The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11. ``` **Example 2:** ``` **Input:** nums = [[1,2,3],[5,17,7],[9,11,10]] **Output:** 17 **Explanation:** The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17. ``` **Constraints:** `1 <= nums.length <= 300` `nums.length == numsi.length` `1 <= nums[i][j] <= 4*106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [[1,2,3],[5,6,7],[9,10,11]]", + "output": "11 " + }, + { + "label": "Example 2", + "input": "nums = [[1,2,3],[5,17,7],[9,11,10]]", + "output": "17 " + } + ], + "constraints": [ + "An integer is prime if it is greater than 1 and has no positive integer divisors other than 1 and itself.", + "An integer val is on one of the diagonals of nums if there exists an integer i for which nums[i][i] = val or an i for which nums[i][nums.length - i - 1] = val.", + "1 <= nums.length <= 300", + "nums.length == numsi.length", + "1 <= nums[i][j]Ā <= 4*106" + ], + "python_template": "class Solution(object):\n def diagonalPrime(self, nums):\n \"\"\"\n :type nums: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int diagonalPrime(int[][] nums) {\n \n }\n}", + "metadata": { + "func_name": "diagonalPrime" + } +} \ No newline at end of file diff --git a/prime-subtraction-operation.json b/prime-subtraction-operation.json new file mode 100644 index 0000000000000000000000000000000000000000..9534db314d7a629888b8ef66cfa15738d29c6673 --- /dev/null +++ b/prime-subtraction-operation.json @@ -0,0 +1,36 @@ +{ + "id": 2716, + "name": "prime-subtraction-operation", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/prime-subtraction-operation/", + "date": "2023-03-19", + "task_description": "You are given a **0-indexed** integer array `nums` of length `n`. You can perform the following operation as many times as you want: Pick an index `i` that you haven’t picked before, and pick a prime `p` **strictly less than** `nums[i]`, then subtract `p` from `nums[i]`. Return _true if you can make `nums` a strictly increasing array using the above operation and false otherwise._ A **strictly increasing array** is an array whose each element is strictly greater than its preceding element. **Example 1:** ``` **Input:** nums = [4,9,6,10] **Output:** true **Explanation:** In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10]. In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10]. After the second operation, nums is sorted in strictly increasing order, so the answer is true. ``` **Example 2:** ``` **Input:** nums = [6,8,11,12] **Output:** true **Explanation: **Initially nums is sorted in strictly increasing order, so we don't need to make any operations. ``` **Example 3:** ``` **Input:** nums = [5,8,3] **Output:** false **Explanation:** It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 1000` `nums.length == n`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,9,6,10]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [6,8,11,12]", + "output": "true " + }, + { + "label": "Example 3", + "input": "nums = [5,8,3]", + "output": "false " + } + ], + "constraints": [ + "Pick an index i that you haven’t picked before, and pick a prime p strictly less than nums[i], then subtract p from nums[i].", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 1000", + "nums.length == n" + ], + "python_template": "class Solution(object):\n def primeSubOperation(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean primeSubOperation(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "primeSubOperation" + } +} \ No newline at end of file diff --git a/properties-graph.json b/properties-graph.json new file mode 100644 index 0000000000000000000000000000000000000000..4c42932b613bbd7f6cf05ebe3bb1cdc7fa3e30e8 --- /dev/null +++ b/properties-graph.json @@ -0,0 +1,36 @@ +{ + "id": 3809, + "name": "properties-graph", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/properties-graph/", + "date": "2025-03-16", + "task_description": "You are given a 2D integer array `properties` having dimensions `n x m` and an integer `k`. Define a function `intersect(a, b)` that returns the **number of distinct integers** common to both arrays `a` and `b`. Construct an **undirected** graph where each index `i` corresponds to `properties[i]`. There is an edge between node `i` and node `j` if and only if `intersect(properties[i], properties[j]) >= k`, where `i` and `j` are in the range `[0, n - 1]` and `i != j`. Return the number of **connected components** in the resulting graph. **Example 1:** **Input:** properties = [[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]], k = 1 **Output:** 3 **Explanation:** The graph formed has 3 connected components: **Example 2:** **Input:** properties = [[1,2,3],[2,3,4],[4,3,5]], k = 2 **Output:** 1 **Explanation:** The graph formed has 1 connected component: **Example 3:** **Input:** properties = [[1,1],[1,1]], k = 2 **Output:** 2 **Explanation:** `intersect(properties[0], properties[1]) = 1`, which is less than `k`. This means there is no edge between `properties[0]` and `properties[1]` in the graph. **Constraints:** `1 <= n == properties.length <= 100` `1 <= m == properties[i].length <= 100` `1 <= properties[i][j] <= 100` `1 <= k <= m`", + "test_case": [ + { + "label": "Example 1", + "input": "properties = [[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]], k = 1", + "output": "3 " + }, + { + "label": "Example 2", + "input": "properties = [[1,2,3],[2,3,4],[4,3,5]], k = 2", + "output": "1 " + }, + { + "label": "Example 3", + "input": "properties = [[1,1],[1,1]], k = 2", + "output": "2 " + } + ], + "constraints": [ + "1 <= n == properties.length <= 100", + "1 <= m == properties[i].length <= 100", + "1 <= properties[i][j] <= 100", + "1 <= k <= m" + ], + "python_template": "class Solution(object):\n def numberOfComponents(self, properties, k):\n \"\"\"\n :type properties: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfComponents(int[][] properties, int k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfComponents" + } +} \ No newline at end of file diff --git a/put-marbles-in-bags.json b/put-marbles-in-bags.json new file mode 100644 index 0000000000000000000000000000000000000000..6fc407ef76c685d7ccee2474f88396350aab79fc --- /dev/null +++ b/put-marbles-in-bags.json @@ -0,0 +1,32 @@ +{ + "id": 2681, + "name": "put-marbles-in-bags", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/put-marbles-in-bags/", + "date": "2023-01-22", + "task_description": "You have `k` bags. You are given a **0-indexed** integer array `weights` where `weights[i]` is the weight of the `ith` marble. You are also given the integer `k.` Divide the marbles into the `k` bags according to the following rules: No bag is empty. If the `ith` marble and `jth` marble are in a bag, then all marbles with an index between the `ith` and `jth` indices should also be in that same bag. If a bag consists of all the marbles with an index from `i` to `j` inclusively, then the cost of the bag is `weights[i] + weights[j]`. The **score** after distributing the marbles is the sum of the costs of all the `k` bags. Return _the **difference** between the **maximum** and **minimum** scores among marble distributions_. **Example 1:** ``` **Input:** weights = [1,3,5,1], k = 2 **Output:** 4 **Explanation:** The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. Thus, we return their difference 10 - 6 = 4. ``` **Example 2:** ``` **Input:** weights = [1, 3], k = 2 **Output:** 0 **Explanation:** The only distribution possible is [1],[3]. Since both the maximal and minimal score are the same, we return 0. ``` **Constraints:** `1 <= k <= weights.length <= 105` `1 <= weights[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "weights = [1,3,5,1], k = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "weights = [1, 3], k = 2", + "output": "0 " + } + ], + "constraints": [ + "No bag is empty.", + "If the ith marble and jth marble are in a bag, then all marbles with an index between the ith and jth indices should also be in that same bag.", + "If a bag consists of all the marbles with an index from i to j inclusively, then the cost of the bag is weights[i] + weights[j].", + "1 <= k <= weights.length <= 105", + "1 <= weights[i] <= 109" + ], + "python_template": "class Solution(object):\n def putMarbles(self, weights, k):\n \"\"\"\n :type weights: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long putMarbles(int[] weights, int k) {\n \n }\n}", + "metadata": { + "func_name": "putMarbles" + } +} \ No newline at end of file diff --git a/query-kth-smallest-trimmed-number.json b/query-kth-smallest-trimmed-number.json new file mode 100644 index 0000000000000000000000000000000000000000..1edc1c7c40fd8310b16baaa52b6b6ef84acea9ff --- /dev/null +++ b/query-kth-smallest-trimmed-number.json @@ -0,0 +1,40 @@ +{ + "id": 2422, + "name": "query-kth-smallest-trimmed-number", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/query-kth-smallest-trimmed-number/", + "date": "2022-07-10", + "task_description": "You are given a **0-indexed** array of strings `nums`, where each string is of **equal length** and consists of only digits. You are also given a **0-indexed** 2D integer array `queries` where `queries[i] = [ki, trimi]`. For each `queries[i]`, you need to: **Trim** each number in `nums` to its **rightmost** `trimi` digits. Determine the **index** of the `kith` smallest trimmed number in `nums`. If two trimmed numbers are equal, the number with the **lower** index is considered to be smaller. Reset each number in `nums` to its original length. Return _an array _`answer`_ of the same length as _`queries`,_ where _`answer[i]`_ is the answer to the _`ith`_ query._ **Note**: To trim to the rightmost `x` digits means to keep removing the leftmost digit, until only `x` digits remain. Strings in `nums` may contain leading zeros. **Example 1:** ``` **Input:** nums = [\"102\",\"473\",\"251\",\"814\"], queries = [[1,1],[2,3],[4,2],[1,2]] **Output:** [2,2,1,0] **Explanation:** 1. After trimming to the last digit, nums = [\"2\",\"3\",\"1\",\"4\"]. The smallest number is 1 at index 2. 2. Trimmed to the last 3 digits, nums is unchanged. The 2nd smallest number is 251 at index 2. 3. Trimmed to the last 2 digits, nums = [\"02\",\"73\",\"51\",\"14\"]. The 4th smallest number is 73. 4. Trimmed to the last 2 digits, the smallest number is 2 at index 0. Note that the trimmed number \"02\" is evaluated as 2. ``` **Example 2:** ``` **Input:** nums = [\"24\",\"37\",\"96\",\"04\"], queries = [[2,1],[2,2]] **Output:** [3,0] **Explanation:** 1. Trimmed to the last digit, nums = [\"4\",\"7\",\"6\",\"4\"]. The 2nd smallest number is 4 at index 3. There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3. 2. Trimmed to the last 2 digits, nums is unchanged. The 2nd smallest number is 24. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i].length <= 100` `nums[i]` consists of only digits. All `nums[i].length` are **equal**. `1 <= queries.length <= 100` `queries[i].length == 2` `1 <= ki <= nums.length` `1 <= trimi <= nums[i].length` **Follow up:** Could you use the **Radix Sort Algorithm** to solve this problem? What will be the complexity of that solution?", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [\"102\",\"473\",\"251\",\"814\"], queries = [[1,1],[2,3],[4,2],[1,2]]", + "output": "[2,2,1,0] " + }, + { + "label": "Example 2", + "input": "nums = [\"24\",\"37\",\"96\",\"04\"], queries = [[2,1],[2,2]]", + "output": "[3,0] " + } + ], + "constraints": [ + "Trim each number in nums to its rightmost trimi digits.", + "Determine the index of the kith smallest trimmed number in nums. If two trimmed numbers are equal, the number with the lower index is considered to be smaller.", + "Reset each number in nums to its original length.", + "To trim to the rightmost x digits means to keep removing the leftmost digit, until only x digits remain.", + "Strings in nums may contain leading zeros.", + "1 <= nums.length <= 100", + "1 <= nums[i].length <= 100", + "nums[i] consists of only digits.", + "All nums[i].length are equal.", + "1 <= queries.length <= 100", + "queries[i].length == 2", + "1 <= ki <= nums.length", + "1 <= trimi <= nums[i].length" + ], + "python_template": "class Solution(object):\n def smallestTrimmedNumbers(self, nums, queries):\n \"\"\"\n :type nums: List[str]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] smallestTrimmedNumbers(String[] nums, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "smallestTrimmedNumbers" + } +} \ No newline at end of file diff --git a/range-product-queries-of-powers.json b/range-product-queries-of-powers.json new file mode 100644 index 0000000000000000000000000000000000000000..2ed690abc085b08d6140cb6d222394b2b659a2ed --- /dev/null +++ b/range-product-queries-of-powers.json @@ -0,0 +1,30 @@ +{ + "id": 2529, + "name": "range-product-queries-of-powers", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/range-product-queries-of-powers/", + "date": "2022-10-01", + "task_description": "Given a positive integer `n`, there exists a **0-indexed** array called `powers`, composed of the **minimum** number of powers of `2` that sum to `n`. The array is sorted in **non-decreasing** order, and there is **only one** way to form the array. You are also given a **0-indexed** 2D integer array `queries`, where `queries[i] = [lefti, righti]`. Each `queries[i]` represents a query where you have to find the product of all `powers[j]` with `lefti <= j <= righti`. Return_ an array _`answers`_, equal in length to _`queries`_, where _`answers[i]`_ is the answer to the _`ith`_ query_. Since the answer to the `ith` query may be too large, each `answers[i]` should be returned **modulo** `109 + 7`. **Example 1:** ``` **Input:** n = 15, queries = [[0,1],[2,2],[0,3]] **Output:** [2,4,64] **Explanation:** For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size. Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2. Answer to 2nd query: powers[2] = 4. Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64. Each answer modulo 109 + 7 yields the same answer, so [2,4,64] is returned. ``` **Example 2:** ``` **Input:** n = 2, queries = [[0,0]] **Output:** [2] **Explanation:** For n = 2, powers = [2]. The answer to the only query is powers[0] = 2. The answer modulo 109 + 7 is the same, so [2] is returned. ``` **Constraints:** `1 <= n <= 109` `1 <= queries.length <= 105` `0 <= starti <= endi < powers.length`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 15, queries = [[0,1],[2,2],[0,3]]", + "output": "[2,4,64] " + }, + { + "label": "Example 2", + "input": "n = 2, queries = [[0,0]]", + "output": "[2] " + } + ], + "constraints": [ + "1 <= n <= 109", + "1 <= queries.length <= 105", + "0 <= starti <= endi < powers.length" + ], + "python_template": "class Solution(object):\n def productQueries(self, n, queries):\n \"\"\"\n :type n: int\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] productQueries(int n, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "productQueries" + } +} \ No newline at end of file diff --git a/rearrange-array-elements-by-sign.json b/rearrange-array-elements-by-sign.json new file mode 100644 index 0000000000000000000000000000000000000000..414ba5caf4ccb2823259560d946fca1be0b178d5 --- /dev/null +++ b/rearrange-array-elements-by-sign.json @@ -0,0 +1,31 @@ +{ + "id": 2271, + "name": "rearrange-array-elements-by-sign", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/rearrange-array-elements-by-sign/", + "date": "2022-01-16", + "task_description": "You are given a **0-indexed** integer array `nums` of **even** length consisting of an **equal** number of positive and negative integers. You should return the array of nums such that the the array follows the given conditions: Every **consecutive pair** of integers have **opposite signs**. For all integers with the same sign, the **order** in which they were present in `nums` is **preserved**. The rearranged array begins with a positive integer. Return _the modified array after rearranging the elements to satisfy the aforementioned conditions_. **Example 1:** ``` **Input:** nums = [3,1,-2,-5,2,-4] **Output:** [3,-2,1,-5,2,-4] **Explanation:** The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions. ``` **Example 2:** ``` **Input:** nums = [-1,1] **Output:** [1,-1] **Explanation:** 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to [1,-1]. ``` **Constraints:** `2 <= nums.length <= 2 * 105` `nums.length` is **even** `1 <= |nums[i]| <= 105` `nums` consists of **equal** number of positive and negative integers. It is not required to do the modifications in-place.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,1,-2,-5,2,-4]", + "output": "[3,-2,1,-5,2,-4] " + }, + { + "label": "Example 2", + "input": "nums = [-1,1]", + "output": "[1,-1] " + } + ], + "constraints": [ + "2 <= nums.length <= 2 * 105", + "nums.length is even", + "1 <= |nums[i]| <= 105", + "nums consists of equal number of positive and negative integers." + ], + "python_template": "class Solution(object):\n def rearrangeArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] rearrangeArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "rearrangeArray" + } +} \ No newline at end of file diff --git a/rearrange-array-to-maximize-prefix-score.json b/rearrange-array-to-maximize-prefix-score.json new file mode 100644 index 0000000000000000000000000000000000000000..87db5afffff098de4ec0b0a276d3be13e17a109a --- /dev/null +++ b/rearrange-array-to-maximize-prefix-score.json @@ -0,0 +1,29 @@ +{ + "id": 2655, + "name": "rearrange-array-to-maximize-prefix-score", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/", + "date": "2023-03-05", + "task_description": "You are given a **0-indexed** integer array `nums`. You can rearrange the elements of `nums` to **any order** (including the given order). Let `prefix` be the array containing the prefix sums of `nums` after rearranging it. In other words, `prefix[i]` is the sum of the elements from `0` to `i` in `nums` after rearranging it. The **score** of `nums` is the number of positive integers in the array `prefix`. Return _the maximum score you can achieve_. **Example 1:** ``` **Input:** nums = [2,-1,0,1,-3,3,-3] **Output:** 6 **Explanation:** We can rearrange the array into nums = [2,3,1,-1,-3,0,-3]. prefix = [2,5,6,5,2,2,-1], so the score is 6. It can be shown that 6 is the maximum score we can obtain. ``` **Example 2:** ``` **Input:** nums = [-2,-3,0] **Output:** 0 **Explanation:** Any rearrangement of the array will result in a score of 0. ``` **Constraints:** `1 <= nums.length <= 105` `-106 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,-1,0,1,-3,3,-3]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [-2,-3,0]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "-106 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maxScore(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxScore(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxScore" + } +} \ No newline at end of file diff --git a/rearrange-characters-to-make-target-string.json b/rearrange-characters-to-make-target-string.json new file mode 100644 index 0000000000000000000000000000000000000000..2c55c7602267a3969a49f1c16a0a02000b42d209 --- /dev/null +++ b/rearrange-characters-to-make-target-string.json @@ -0,0 +1,35 @@ +{ + "id": 2372, + "name": "rearrange-characters-to-make-target-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/rearrange-characters-to-make-target-string/", + "date": "2022-05-22", + "task_description": "You are given two **0-indexed** strings `s` and `target`. You can take some letters from `s` and rearrange them to form new strings. Return_ the **maximum** number of copies of _`target`_ that can be formed by taking letters from _`s`_ and rearranging them._ **Example 1:** ``` **Input:** s = \"ilovecodingonleetcode\", target = \"code\" **Output:** 2 **Explanation:** For the first copy of \"code\", take the letters at indices 4, 5, 6, and 7. For the second copy of \"code\", take the letters at indices 17, 18, 19, and 20. The strings that are formed are \"ecod\" and \"code\" which can both be rearranged into \"code\". We can make at most two copies of \"code\", so we return 2. ``` **Example 2:** ``` **Input:** s = \"abcba\", target = \"abc\" **Output:** 1 **Explanation:** We can make one copy of \"abc\" by taking the letters at indices 0, 1, and 2. We can make at most one copy of \"abc\", so we return 1. Note that while there is an extra 'a' and 'b' at indices 3 and 4, we cannot reuse the letter 'c' at index 2, so we cannot make a second copy of \"abc\". ``` **Example 3:** ``` **Input:** s = \"abbaccaddaeea\", target = \"aaaaa\" **Output:** 1 **Explanation:** We can make one copy of \"aaaaa\" by taking the letters at indices 0, 3, 6, 9, and 12. We can make at most one copy of \"aaaaa\", so we return 1. ``` **Constraints:** `1 <= s.length <= 100` `1 <= target.length <= 10` `s` and `target` consist of lowercase English letters. **Note:** This question is the same as 1189: Maximum Number of Balloons.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"ilovecodingonleetcode\", target = \"code\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abcba\", target = \"abc\"", + "output": "1 " + }, + { + "label": "Example 3", + "input": "s = \"abbaccaddaeea\", target = \"aaaaa\"", + "output": "1 " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "1 <= target.length <= 10", + "s and target consist of lowercase English letters." + ], + "python_template": "class Solution(object):\n def rearrangeCharacters(self, s, target):\n \"\"\"\n :type s: str\n :type target: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int rearrangeCharacters(String s, String target) {\n \n }\n}", + "metadata": { + "func_name": "rearrangeCharacters" + } +} \ No newline at end of file diff --git a/rearrange-k-substrings-to-form-target-string.json b/rearrange-k-substrings-to-form-target-string.json new file mode 100644 index 0000000000000000000000000000000000000000..314e83557a611418beb873eeb7e8d88c721d420a --- /dev/null +++ b/rearrange-k-substrings-to-form-target-string.json @@ -0,0 +1,43 @@ +{ + "id": 3595, + "name": "rearrange-k-substrings-to-form-target-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/rearrange-k-substrings-to-form-target-string/", + "date": "2024-11-17", + "task_description": "You are given two strings `s` and `t`, both of which are anagrams of each other, and an integer `k`. Your task is to determine whether it is possible to split the string `s` into `k` equal-sized substrings, rearrange the substrings, and concatenate them in _any order_ to create a new string that matches the given string `t`. Return `true` if this is possible, otherwise, return `false`. An **anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. A **substring** is a contiguous non-empty sequence of characters within a string. **Example 1:** **Input:** s = \"abcd\", t = \"cdab\", k = 2 **Output:** true **Explanation:** Split `s` into 2 substrings of length 2: `[\"ab\", \"cd\"]`. Rearranging these substrings as `[\"cd\", \"ab\"]`, and then concatenating them results in `\"cdab\"`, which matches `t`. **Example 2:** **Input:** s = \"aabbcc\", t = \"bbaacc\", k = 3 **Output:** true **Explanation:** Split `s` into 3 substrings of length 2: `[\"aa\", \"bb\", \"cc\"]`. Rearranging these substrings as `[\"bb\", \"aa\", \"cc\"]`, and then concatenating them results in `\"bbaacc\"`, which matches `t`. **Example 3:** **Input:** s = \"aabbcc\", t = \"bbaacc\", k = 2 **Output:** false **Explanation:** Split `s` into 2 substrings of length 3: `[\"aab\", \"bcc\"]`. These substrings cannot be rearranged to form `t = \"bbaacc\"`, so the output is `false`. **Constraints:** `1 <= s.length == t.length <= 2 * 105` `1 <= k <= s.length` `s.length` is divisible by `k`. `s` and `t` consist only of lowercase English letters. The input is generated such that `s` and `t` are anagrams of each other.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcd\", t = \"cdab\", k = 2", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"aabbcc\", t = \"bbaacc\", k = 3", + "output": "true " + }, + { + "label": "Example 3", + "input": "s = \"aabbcc\", t = \"bbaacc\", k = 2", + "output": "false " + } + ], + "constraints": [ + "Split s into 2 substrings of length 2: [\"ab\", \"cd\"].", + "Rearranging these substrings as [\"cd\", \"ab\"], and then concatenating them results in \"cdab\", which matches t.", + "Split s into 3 substrings of length 2: [\"aa\", \"bb\", \"cc\"].", + "Rearranging these substrings as [\"bb\", \"aa\", \"cc\"], and then concatenating them results in \"bbaacc\", which matches t.", + "Split s into 2 substrings of length 3: [\"aab\", \"bcc\"].", + "These substrings cannot be rearranged to form t = \"bbaacc\", so the output is false.", + "1 <= s.length == t.length <= 2 * 105", + "1 <= k <= s.length", + "s.length is divisible by k.", + "s and t consist only of lowercase English letters.", + "The input is generated such that s and t are anagrams of each other." + ], + "python_template": "class Solution(object):\n def isPossibleToRearrange(self, s, t, k):\n \"\"\"\n :type s: str\n :type t: str\n :type k: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isPossibleToRearrange(String s, String t, int k) {\n \n }\n}", + "metadata": { + "func_name": "isPossibleToRearrange" + } +} \ No newline at end of file diff --git a/rearranging-fruits.json b/rearranging-fruits.json new file mode 100644 index 0000000000000000000000000000000000000000..935102c5b5c83c51dfb6c7eda9bf134886be0a6a --- /dev/null +++ b/rearranging-fruits.json @@ -0,0 +1,32 @@ +{ + "id": 2689, + "name": "rearranging-fruits", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/rearranging-fruits/", + "date": "2023-01-29", + "task_description": "You have two fruit baskets containing `n` fruits each. You are given two **0-indexed** integer arrays `basket1` and `basket2` representing the cost of fruit in each basket. You want to make both baskets **equal**. To do so, you can use the following operation as many times as you want: Chose two indices `i` and `j`, and swap the `ith `fruit of `basket1` with the `jth` fruit of `basket2`. The cost of the swap is `min(basket1[i],basket2[j])`. Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets. Return _the minimum cost to make both the baskets equal or _`-1`_ if impossible._ **Example 1:** ``` **Input:** basket1 = [4,2,2,2], basket2 = [1,4,1,2] **Output:** 1 **Explanation:** Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal. ``` **Example 2:** ``` **Input:** basket1 = [2,3,4,1], basket2 = [3,2,5,1] **Output:** -1 **Explanation:** It can be shown that it is impossible to make both the baskets equal. ``` **Constraints:** `basket1.length == basket2.length` `1 <= basket1.length <= 105` `1 <= basket1[i],basket2[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "basket1 = [4,2,2,2], basket2 = [1,4,1,2]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "basket1 = [2,3,4,1], basket2 = [3,2,5,1]", + "output": "-1 " + } + ], + "constraints": [ + "Chose two indices i and j, and swap the ithĀ fruit of basket1 with the jthĀ fruit of basket2.", + "The cost of the swap is min(basket1[i],basket2[j]).", + "basket1.length == basket2.length", + "1 <= basket1.length <= 105", + "1 <= basket1[i],basket2[i]Ā <= 109" + ], + "python_template": "class Solution(object):\n def minCost(self, basket1, basket2):\n \"\"\"\n :type basket1: List[int]\n :type basket2: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minCost(int[] basket1, int[] basket2) {\n \n }\n}", + "metadata": { + "func_name": "minCost" + } +} \ No newline at end of file diff --git a/recover-the-original-array.json b/recover-the-original-array.json new file mode 100644 index 0000000000000000000000000000000000000000..a8b28b935739f48de1d8d536f61590fa26890311 --- /dev/null +++ b/recover-the-original-array.json @@ -0,0 +1,36 @@ +{ + "id": 2241, + "name": "recover-the-original-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/recover-the-original-array/", + "date": "2021-12-19", + "task_description": "Alice had a **0-indexed** array `arr` consisting of `n` **positive** integers. She chose an arbitrary **positive integer** `k` and created two new **0-indexed** integer arrays `lower` and `higher` in the following manner: `lower[i] = arr[i] - k`, for every index `i` where `0 <= i < n` `higher[i] = arr[i] + k`, for every index `i` where `0 <= i < n` Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays `lower` and `higher`, but not the array each integer belonged to. Help Alice and recover the original array. Given an array `nums` consisting of `2n` integers, where **exactly** `n` of the integers were present in `lower` and the remaining in `higher`, return _the **original** array_ `arr`. In case the answer is not unique, return _**any** valid array_. **Note:** The test cases are generated such that there exists **at least one** valid array `arr`. **Example 1:** ``` **Input:** nums = [2,10,6,4,8,12] **Output:** [3,7,11] **Explanation:** If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12]. Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums. Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12]. ``` **Example 2:** ``` **Input:** nums = [1,1,3,3] **Output:** [2,2] **Explanation:** If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3]. Combining lower and higher gives us [1,1,3,3], which is equal to nums. Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0. This is invalid since k must be positive. ``` **Example 3:** ``` **Input:** nums = [5,435] **Output:** [220] **Explanation:** The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435]. ``` **Constraints:** `2 * n == nums.length` `1 <= n <= 1000` `1 <= nums[i] <= 109` The test cases are generated such that there exists **at least one** valid array `arr`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,10,6,4,8,12]", + "output": "[3,7,11] " + }, + { + "label": "Example 2", + "input": "nums = [1,1,3,3]", + "output": "[2,2] " + }, + { + "label": "Example 3", + "input": "nums = [5,435]", + "output": "[220] " + } + ], + "constraints": [ + "2 * n == nums.length", + "1 <= n <= 1000", + "1 <= nums[i] <= 109", + "The test cases are generated such that there exists at least one valid array arr." + ], + "python_template": "class Solution(object):\n def recoverArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] recoverArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "recoverArray" + } +} \ No newline at end of file diff --git a/relocate-marbles.json b/relocate-marbles.json new file mode 100644 index 0000000000000000000000000000000000000000..c5d97b1de1f5bab534f96b673370af89e25e5979 --- /dev/null +++ b/relocate-marbles.json @@ -0,0 +1,34 @@ +{ + "id": 2834, + "name": "relocate-marbles", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/relocate-marbles/", + "date": "2023-06-24", + "task_description": "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 `moveFrom[i]` to position `moveTo[i]`. After completing all the steps, return _the sorted list of **occupied** positions_. **Notes:** We call a position **occupied** if there is at least one marble in that position. There may be multiple marbles in a single position. **Example 1:** ``` **Input:** nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5] **Output:** [5,6,8,9] **Explanation:** Initially, the marbles are at positions 1,6,7,8. At the i = 0th step, we move the marbles at position 1 to position 2. Then, positions 2,6,7,8 are occupied. At the i = 1st step, we move the marbles at position 7 to position 9. Then, positions 2,6,8,9 are occupied. At the i = 2nd step, we move the marbles at position 2 to position 5. Then, positions 5,6,8,9 are occupied. At the end, the final positions containing at least one marbles are [5,6,8,9]. ``` **Example 2:** ``` **Input:** nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2] **Output:** [2] **Explanation:** Initially, the marbles are at positions [1,1,3,3]. At the i = 0th step, we move all the marbles at position 1 to position 2. Then, the marbles are at positions [2,2,3,3]. At the i = 1st step, we move all the marbles at position 3 to position 2. Then, the marbles are at positions [2,2,2,2]. Since 2 is the only occupied position, we return [2]. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= moveFrom.length <= 105` `moveFrom.length == moveTo.length` `1 <= nums[i], moveFrom[i], moveTo[i] <= 109` The test cases are generated such that there is at least a marble in `moveFrom[i]` at the moment we want to apply the `ith` move.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5]", + "output": "[5,6,8,9] " + }, + { + "label": "Example 2", + "input": "nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2]", + "output": "[2] " + } + ], + "constraints": [ + "We call a position occupied if there is at least one marble in that position.", + "There may be multiple marbles in a single position.", + "1 <= nums.length <= 105", + "1 <= moveFrom.length <= 105", + "moveFrom.length == moveTo.length", + "1 <= nums[i], moveFrom[i], moveTo[i] <= 109", + "The test cases are generated such that there is at least a marble inĀ moveFrom[i]Ā at the moment we want to applyĀ the ithĀ move." + ], + "python_template": "class Solution(object):\n def relocateMarbles(self, nums, moveFrom, moveTo):\n \"\"\"\n :type nums: List[int]\n :type moveFrom: List[int]\n :type moveTo: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List relocateMarbles(int[] nums, int[] moveFrom, int[] moveTo) {\n \n }\n}", + "metadata": { + "func_name": "relocateMarbles" + } +} \ No newline at end of file diff --git a/remove-adjacent-almost-equal-characters.json b/remove-adjacent-almost-equal-characters.json new file mode 100644 index 0000000000000000000000000000000000000000..efeb45c907ea1d7bf77eee705eac8f3ffc4b4346 --- /dev/null +++ b/remove-adjacent-almost-equal-characters.json @@ -0,0 +1,34 @@ +{ + "id": 3230, + "name": "remove-adjacent-almost-equal-characters", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/remove-adjacent-almost-equal-characters/", + "date": "2023-11-25", + "task_description": "You are given a **0-indexed** string `word`. In one operation, you can pick any index `i` of `word` and change `word[i]` to any lowercase English letter. Return _the **minimum** number of operations needed to remove all adjacent **almost-equal** characters from_ `word`. Two characters `a` and `b` are **almost-equal** if `a == b` or `a` and `b` are adjacent in the alphabet. **Example 1:** ``` **Input:** word = \"aaaaa\" **Output:** 2 **Explanation:** We can change word into \"a**c**a**c**a\" which does not have any adjacent almost-equal characters. It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2. ``` **Example 2:** ``` **Input:** word = \"abddez\" **Output:** 2 **Explanation:** We can change word into \"**y**bd**o**ez\" which does not have any adjacent almost-equal characters. It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2. ``` **Example 3:** ``` **Input:** word = \"zyxyxyz\" **Output:** 3 **Explanation:** We can change word into \"z**a**x**a**x**a**z\" which does not have any adjacent almost-equal characters. It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3. ``` **Constraints:** `1 <= word.length <= 100` `word` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"aaaaa\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "word = \"abddez\"", + "output": "2 " + }, + { + "label": "Example 3", + "input": "word = \"zyxyxyz\"", + "output": "3 " + } + ], + "constraints": [ + "1 <= word.length <= 100", + "word consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def removeAlmostEqualCharacters(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int removeAlmostEqualCharacters(String word) {\n \n }\n}", + "metadata": { + "func_name": "removeAlmostEqualCharacters" + } +} \ No newline at end of file diff --git a/remove-digit-from-number-to-maximize-result.json b/remove-digit-from-number-to-maximize-result.json new file mode 100644 index 0000000000000000000000000000000000000000..9682cc86cbecff5f9ec86c306626fbce4577919e --- /dev/null +++ b/remove-digit-from-number-to-maximize-result.json @@ -0,0 +1,36 @@ +{ + "id": 2337, + "name": "remove-digit-from-number-to-maximize-result", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/", + "date": "2022-04-24", + "task_description": "You are given a string `number` representing a **positive integer** and a character `digit`. Return _the resulting string after removing **exactly one occurrence** of _`digit`_ from _`number`_ such that the value of the resulting string in **decimal** form is **maximized**_. The test cases are generated such that `digit` occurs at least once in `number`. **Example 1:** ``` **Input:** number = \"123\", digit = \"3\" **Output:** \"12\" **Explanation:** There is only one '3' in \"123\". After removing '3', the result is \"12\". ``` **Example 2:** ``` **Input:** number = \"1231\", digit = \"1\" **Output:** \"231\" **Explanation:** We can remove the first '1' to get \"231\" or remove the second '1' to get \"123\". Since 231 > 123, we return \"231\". ``` **Example 3:** ``` **Input:** number = \"551\", digit = \"5\" **Output:** \"51\" **Explanation:** We can remove either the first or second '5' from \"551\". Both result in the string \"51\". ``` **Constraints:** `2 <= number.length <= 100` `number` consists of digits from `'1'` to `'9'`. `digit` is a digit from `'1'` to `'9'`. `digit` occurs at least once in `number`.", + "test_case": [ + { + "label": "Example 1", + "input": "number = \"123\", digit = \"3\"", + "output": "\"12\" " + }, + { + "label": "Example 2", + "input": "number = \"1231\", digit = \"1\"", + "output": "\"231\" " + }, + { + "label": "Example 3", + "input": "number = \"551\", digit = \"5\"", + "output": "\"51\" " + } + ], + "constraints": [ + "2 <= number.length <= 100", + "number consists of digits from '1' to '9'.", + "digit is a digit from '1' to '9'.", + "digit occurs at least once in number." + ], + "python_template": "class Solution(object):\n def removeDigit(self, number, digit):\n \"\"\"\n :type number: str\n :type digit: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String removeDigit(String number, char digit) {\n \n }\n}", + "metadata": { + "func_name": "removeDigit" + } +} \ No newline at end of file diff --git a/remove-letter-to-equalize-frequency.json b/remove-letter-to-equalize-frequency.json new file mode 100644 index 0000000000000000000000000000000000000000..2daeb03661a909b855b874798c361a76fb96cf12 --- /dev/null +++ b/remove-letter-to-equalize-frequency.json @@ -0,0 +1,31 @@ +{ + "id": 2532, + "name": "remove-letter-to-equalize-frequency", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/remove-letter-to-equalize-frequency/", + "date": "2022-09-17", + "task_description": "You are given a **0-indexed** string `word`, consisting of lowercase English letters. You need to select **one** index and **remove** the letter at that index from `word` so that the **frequency** of every letter present in `word` is equal. Return_ _`true`_ if it is possible to remove one letter so that the frequency of all letters in _`word`_ are equal, and _`false`_ otherwise_. **Note:** The frequency of a letter `x` is the number of times it occurs in the string. You **must** remove exactly one letter and cannot choose to do nothing. **Example 1:** ``` **Input:** word = \"abcc\" **Output:** true **Explanation:** Select index 3 and delete it: word becomes \"abc\" and each character has a frequency of 1. ``` **Example 2:** ``` **Input:** word = \"aazz\" **Output:** false **Explanation:** We must delete a character, so either the frequency of \"a\" is 1 and the frequency of \"z\" is 2, or vice versa. It is impossible to make all present letters have equal frequency. ``` **Constraints:** `2 <= word.length <= 100` `word` consists of lowercase English letters only.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"abcc\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "word = \"aazz\"", + "output": "false " + } + ], + "constraints": [ + "The frequency of a letter x is the number of times it occurs in the string.", + "You must remove exactly one letter and cannot choose to do nothing.", + "2 <= word.length <= 100", + "word consists of lowercase English letters only." + ], + "python_template": "class Solution(object):\n def equalFrequency(self, word):\n \"\"\"\n :type word: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean equalFrequency(String word) {\n \n }\n}", + "metadata": { + "func_name": "equalFrequency" + } +} \ No newline at end of file diff --git a/remove-methods-from-project.json b/remove-methods-from-project.json new file mode 100644 index 0000000000000000000000000000000000000000..81ec1a6f3d3a8011d799a63b39ba0bef0bd0735a --- /dev/null +++ b/remove-methods-from-project.json @@ -0,0 +1,39 @@ +{ + "id": 3561, + "name": "remove-methods-from-project", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/remove-methods-from-project/", + "date": "2024-09-29", + "task_description": "You are maintaining a project that has `n` methods numbered from `0` to `n - 1`. You are given two integers `n` and `k`, and a 2D integer array `invocations`, where `invocations[i] = [ai, bi]` indicates that method `ai` invokes method `bi`. There is a known bug in method `k`. Method `k`, along with any method invoked by it, either **directly** or **indirectly**, are considered **suspicious** and we aim to remove them. A group of methods can only be removed if no method **outside** the group invokes any methods **within** it. Return an array containing all the remaining methods after removing all the **suspicious** methods. You may return the answer in _any order_. If it is not possible to remove **all** the suspicious methods, **none** should be removed. **Example 1:** **Input:** n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]] **Output:** [0,1,2,3] **Explanation:** Method 2 and method 1 are suspicious, but they are directly invoked by methods 3 and 0, which are not suspicious. We return all elements without removing anything. **Example 2:** **Input:** n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]] **Output:** [3,4] **Explanation:** Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them. **Example 3:** **Input:** n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]] **Output:** [] **Explanation:** All methods are suspicious. We can remove them. **Constraints:** `1 <= n <= 105` `0 <= k <= n - 1` `0 <= invocations.length <= 2 * 105` `invocations[i] == [ai, bi]` `0 <= ai, bi <= n - 1` `ai != bi` `invocations[i] != invocations[j]`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]", + "output": "[0,1,2,3] " + }, + { + "label": "Example 2", + "input": "n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]", + "output": "[3,4] " + }, + { + "label": "Example 3", + "input": "n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]", + "output": "[] " + } + ], + "constraints": [ + "1 <= n <= 105", + "0 <= k <= n - 1", + "0 <= invocations.length <= 2 * 105", + "invocations[i] == [ai, bi]", + "0 <= ai, bi <= n - 1", + "ai != bi", + "invocations[i] != invocations[j]" + ], + "python_template": "class Solution(object):\n def remainingMethods(self, n, k, invocations):\n \"\"\"\n :type n: int\n :type k: int\n :type invocations: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List remainingMethods(int n, int k, int[][] invocations) {\n \n }\n}", + "metadata": { + "func_name": "remainingMethods" + } +} \ No newline at end of file diff --git a/remove-trailing-zeros-from-a-string.json b/remove-trailing-zeros-from-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..811eca6d4e37cd6a2136ab32961988d20a58309a --- /dev/null +++ b/remove-trailing-zeros-from-a-string.json @@ -0,0 +1,30 @@ +{ + "id": 2819, + "name": "remove-trailing-zeros-from-a-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/remove-trailing-zeros-from-a-string/", + "date": "2023-05-21", + "task_description": "Given a **positive** integer `num` represented as a string, return _the integer _`num`_ without trailing zeros as a string_. **Example 1:** ``` **Input:** num = \"51230100\" **Output:** \"512301\" **Explanation:** Integer \"51230100\" has 2 trailing zeros, we remove them and return integer \"512301\". ``` **Example 2:** ``` **Input:** num = \"123\" **Output:** \"123\" **Explanation:** Integer \"123\" has no trailing zeros, we return integer \"123\". ``` **Constraints:** `1 <= num.length <= 1000` `num` consists of only digits. `num` doesn't have any leading zeros.", + "test_case": [ + { + "label": "Example 1", + "input": "num = \"51230100\"", + "output": "\"512301\" " + }, + { + "label": "Example 2", + "input": "num = \"123\"", + "output": "\"123\" " + } + ], + "constraints": [ + "1 <= num.length <= 1000", + "num consistsĀ of only digits.", + "num doesn'tĀ have any leading zeros." + ], + "python_template": "class Solution(object):\n def removeTrailingZeros(self, num):\n \"\"\"\n :type num: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String removeTrailingZeros(String num) {\n \n }\n}", + "metadata": { + "func_name": "removeTrailingZeros" + } +} \ No newline at end of file diff --git a/removing-minimum-number-of-magic-beans.json b/removing-minimum-number-of-magic-beans.json new file mode 100644 index 0000000000000000000000000000000000000000..c61f5450ef02104d35869b8d9364602235e37320 --- /dev/null +++ b/removing-minimum-number-of-magic-beans.json @@ -0,0 +1,29 @@ +{ + "id": 2290, + "name": "removing-minimum-number-of-magic-beans", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/removing-minimum-number-of-magic-beans/", + "date": "2022-02-06", + "task_description": "You are given an array of **positive** integers `beans`, where each integer represents the number of magic beans found in a particular magic bag. **Remove** any number of beans (**possibly none**) from each bag such that the number of beans in each remaining **non-empty** bag (still containing **at least one** bean) is **equal**. Once a bean has been removed from a bag, you are **not** allowed to return it to any of the bags. Return _the **minimum** number of magic beans that you have to remove_. **Example 1:** ``` **Input:** beans = [4,1,6,5] **Output:** 4 **Explanation:** - We remove 1 bean from the bag with only 1 bean. This results in the remaining bags: [4,**0**,6,5] - Then we remove 2 beans from the bag with 6 beans. This results in the remaining bags: [4,0,**4**,5] - Then we remove 1 bean from the bag with 5 beans. This results in the remaining bags: [4,0,4,**4**] We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans. There are no other solutions that remove 4 beans or fewer. ``` **Example 2:** ``` **Input:** beans = [2,10,3,2] **Output:** 7 **Explanation:** - We remove 2 beans from one of the bags with 2 beans. This results in the remaining bags: [**0**,10,3,2] - Then we remove 2 beans from the other bag with 2 beans. This results in the remaining bags: [0,10,3,**0**] - Then we remove 3 beans from the bag with 3 beans. This results in the remaining bags: [0,10,**0**,0] We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans. There are no other solutions that removes 7 beans or fewer. ``` **Constraints:** `1 <= beans.length <= 105` `1 <= beans[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "beans = [4,1,6,5]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "beans = [2,10,3,2]", + "output": "7 " + } + ], + "constraints": [ + "1 <= beans.length <= 105", + "1 <= beans[i] <= 105" + ], + "python_template": "class Solution(object):\n def minimumRemoval(self, beans):\n \"\"\"\n :type beans: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumRemoval(int[] beans) {\n \n }\n}", + "metadata": { + "func_name": "minimumRemoval" + } +} \ No newline at end of file diff --git a/removing-stars-from-a-string.json b/removing-stars-from-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..dc0329d4db11aaa12dbd8f816acc9c865887a306 --- /dev/null +++ b/removing-stars-from-a-string.json @@ -0,0 +1,34 @@ +{ + "id": 2470, + "name": "removing-stars-from-a-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/removing-stars-from-a-string/", + "date": "2022-08-21", + "task_description": "You are given a string `s`, which contains stars `*`. In one operation, you can: Choose a star in `s`. Remove the closest **non-star** character to its **left**, as well as remove the star itself. Return _the string after **all** stars have been removed_. **Note:** The input will be generated such that the operation is always possible. It can be shown that the resulting string will always be unique. **Example 1:** ``` **Input:** s = \"leet**cod*e\" **Output:** \"lecoe\" **Explanation:** Performing the removals from left to right: - The closest character to the 1st star is 't' in \"lee**t****cod*e\". s becomes \"lee*cod*e\". - The closest character to the 2nd star is 'e' in \"le**e***cod*e\". s becomes \"lecod*e\". - The closest character to the 3rd star is 'd' in \"leco**d***e\". s becomes \"lecoe\". There are no more stars, so we return \"lecoe\". ``` **Example 2:** ``` **Input:** s = \"erase*****\" **Output:** \"\" **Explanation:** The entire string is removed, so we return an empty string. ``` **Constraints:** `1 <= s.length <= 105` `s` consists of lowercase English letters and stars `*`. The operation above can be performed on `s`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"leet**cod*e\"", + "output": "\"lecoe\" " + }, + { + "label": "Example 2", + "input": "s = \"erase*****\"", + "output": "\"\" " + } + ], + "constraints": [ + "Choose a star in s.", + "Remove the closest non-star character to its left, as well as remove the star itself.", + "The input will be generated such that the operation is always possible.", + "It can be shown that the resulting string will always be unique.", + "1 <= s.length <= 105", + "s consists of lowercase English letters and stars *.", + "The operation above can be performed on s." + ], + "python_template": "class Solution(object):\n def removeStars(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String removeStars(String s) {\n \n }\n}", + "metadata": { + "func_name": "removeStars" + } +} \ No newline at end of file diff --git a/replace-non-coprime-numbers-in-array.json b/replace-non-coprime-numbers-in-array.json new file mode 100644 index 0000000000000000000000000000000000000000..f220a2d8470546c391f6fbe6cd7284133d3e2519 --- /dev/null +++ b/replace-non-coprime-numbers-in-array.json @@ -0,0 +1,30 @@ +{ + "id": 2307, + "name": "replace-non-coprime-numbers-in-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/replace-non-coprime-numbers-in-array/", + "date": "2022-02-27", + "task_description": "You are given an array of integers `nums`. Perform the following steps: Find **any** two **adjacent** numbers in `nums` that are **non-coprime**. If no such numbers are found, **stop** the process. Otherwise, delete the two numbers and **replace** them with their **LCM (Least Common Multiple)**. **Repeat** this process as long as you keep finding two adjacent non-coprime numbers. Return _the **final** modified array._ It can be shown that replacing adjacent non-coprime numbers in **any** arbitrary order will lead to the same result. The test cases are generated such that the values in the final array are **less than or equal** to `108`. Two values `x` and `y` are **non-coprime** if `GCD(x, y) > 1` where `GCD(x, y)` is the **Greatest Common Divisor** of `x` and `y`. **Example 1:** ``` **Input:** nums = [6,4,3,2,7,6,2] **Output:** [12,7,6] **Explanation:** - (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [**12**,3,2,7,6,2]. - (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [**12**,2,7,6,2]. - (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [**12**,7,6,2]. - (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,**6**]. There are no more adjacent non-coprime numbers in nums. Thus, the final modified array is [12,7,6]. Note that there are other ways to obtain the same resultant array. ``` **Example 2:** ``` **Input:** nums = [2,2,1,1,3,3,3] **Output:** [2,1,1,3] **Explanation:** - (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,**3**,3]. - (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,**3**]. - (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [**2**,1,1,3]. There are no more adjacent non-coprime numbers in nums. Thus, the final modified array is [2,1,1,3]. Note that there are other ways to obtain the same resultant array. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105` The test cases are generated such that the values in the final array are **less than or equal** to `108`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [6,4,3,2,7,6,2]", + "output": "[12,7,6] " + }, + { + "label": "Example 2", + "input": "nums = [2,2,1,1,3,3,3]", + "output": "[2,1,1,3] " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105", + "The test cases are generated such that the values in the final array are less than or equal to 108." + ], + "python_template": "class Solution(object):\n def replaceNonCoprimes(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List replaceNonCoprimes(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "replaceNonCoprimes" + } +} \ No newline at end of file diff --git a/replace-question-marks-in-string-to-minimize-its-value.json b/replace-question-marks-in-string-to-minimize-its-value.json new file mode 100644 index 0000000000000000000000000000000000000000..d79615f1bd137c837e25a9b0fb6353723244ee47 --- /dev/null +++ b/replace-question-marks-in-string-to-minimize-its-value.json @@ -0,0 +1,33 @@ +{ + "id": 3354, + "name": "replace-question-marks-in-string-to-minimize-its-value", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/replace-question-marks-in-string-to-minimize-its-value/", + "date": "2024-03-02", + "task_description": "You are given a string `s`. `s[i]` is either a lowercase English letter or `'?'`. For a string `t` having length `m` containing **only** lowercase English letters, we define the function `cost(i)` for an index `i` as the number of characters **equal** to `t[i]` that appeared before it, i.e. in the range `[0, i - 1]`. The **value** of `t` is the **sum** of `cost(i)` for all indices `i`. For example, for the string `t = \"aab\"`: `cost(0) = 0` `cost(1) = 1` `cost(2) = 0` Hence, the value of `\"aab\"` is `0 + 1 + 0 = 1`. Your task is to **replace all** occurrences of `'?'` in `s` with any lowercase English letter so that the **value** of `s` is **minimized**. Return _a string denoting the modified string with replaced occurrences of _`'?'`_. If there are multiple strings resulting in the **minimum value**, return the lexicographically smallest one._ **Example 1:** **Input: ** s = \"???\" **Output: ** \"abc\" **Explanation: ** In this example, we can replace the occurrences of `'?'` to make `s` equal to `\"abc\"`. For `\"abc\"`, `cost(0) = 0`, `cost(1) = 0`, and `cost(2) = 0`. The value of `\"abc\"` is `0`. Some other modifications of `s` that have a value of `0` are `\"cba\"`, `\"abz\"`, and, `\"hey\"`. Among all of them, we choose the lexicographically smallest. **Example 2:** **Input: ** s = \"a?a?\" **Output: ** \"abac\" **Explanation: ** In this example, the occurrences of `'?'` can be replaced to make `s` equal to `\"abac\"`. For `\"abac\"`, `cost(0) = 0`, `cost(1) = 0`, `cost(2) = 1`, and `cost(3) = 0`. The value of `\"abac\"` is `1`. **Constraints:** `1 <= s.length <= 105` `s[i]` is either a lowercase English letter or `'?'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"???\"", + "output": "\"abc\" " + }, + { + "label": "Example 2", + "input": "s = \"a?a?\"", + "output": "\"abac\" " + } + ], + "constraints": [ + "cost(0) = 0", + "cost(1) = 1", + "cost(2) = 0", + "Hence, the value of \"aab\" is 0 + 1 + 0 = 1.", + "1 <= s.length <= 105", + "s[i] is either a lowercase English letter or '?'." + ], + "python_template": "class Solution(object):\n def minimizeStringValue(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String minimizeStringValue(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimizeStringValue" + } +} \ No newline at end of file diff --git a/robot-collisions.json b/robot-collisions.json new file mode 100644 index 0000000000000000000000000000000000000000..1ec770f2e66974f2c8d292504d268da0c490983f --- /dev/null +++ b/robot-collisions.json @@ -0,0 +1,36 @@ +{ + "id": 2846, + "name": "robot-collisions", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/robot-collisions/", + "date": "2023-06-18", + "task_description": "There are `n` **1-indexed** robots, each having a position on a line, health, and movement direction. You are given **0-indexed** integer arrays `positions`, `healths`, and a string `directions` (`directions[i]` is either **'L'** for **left** or **'R'** for **right**). All integers in `positions` are **unique**. All robots start moving on the line** simultaneously** at the **same speed **in their given directions. If two robots ever share the same position while moving, they will **collide**. If two robots collide, the robot with **lower health** is **removed** from the line, and the health of the other robot **decreases** **by one**. The surviving robot continues in the **same** direction it was going. If both robots have the **same** health, they are both** **removed from the line. Your task is to determine the **health** of the robots that survive the collisions, in the same **order **that the robots were given,** **i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array. Return _an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur._ **Note:** The positions may be unsorted. **Example 1:** ``` **Input:** positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = \"RRRRR\" **Output:** [2,17,9,15,10] **Explanation:** No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. ``` **Example 2:** ``` **Input:** positions = [3,5,2,6], healths = [10,10,15,12], directions = \"RLRL\" **Output:** [14] **Explanation:** There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4's health is smaller, it gets removed, and robot 3's health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. ``` **Example 3:** ``` **Input:** positions = [1,2,5,6], healths = [10,10,11,11], directions = \"RLRL\" **Output:** [] **Explanation:** Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, []. ``` **Constraints:** `1 <= positions.length == healths.length == directions.length == n <= 105` `1 <= positions[i], healths[i] <= 109` `directions[i] == 'L'` or `directions[i] == 'R'` All values in `positions` are distinct", + "test_case": [ + { + "label": "Example 1", + "input": "positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = \"RRRRR\"", + "output": "[2,17,9,15,10] " + }, + { + "label": "Example 2", + "input": "positions = [3,5,2,6], healths = [10,10,15,12], directions = \"RLRL\"", + "output": "[14] " + }, + { + "label": "Example 3", + "input": "positions = [1,2,5,6], healths = [10,10,11,11], directions = \"RLRL\"", + "output": "[] " + } + ], + "constraints": [ + "1 <= positions.length == healths.length == directions.length == n <= 105", + "1 <= positions[i], healths[i] <= 109", + "directions[i] == 'L' or directions[i] == 'R'", + "All values in positions are distinct" + ], + "python_template": "class Solution(object):\n def survivedRobotsHealths(self, positions, healths, directions):\n \"\"\"\n :type positions: List[int]\n :type healths: List[int]\n :type directions: str\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List survivedRobotsHealths(int[] positions, int[] healths, String directions) {\n \n }\n}", + "metadata": { + "func_name": "survivedRobotsHealths" + } +} \ No newline at end of file diff --git a/row-with-maximum-ones.json b/row-with-maximum-ones.json new file mode 100644 index 0000000000000000000000000000000000000000..694665f5aad6123531a00260eec6218e9351818c --- /dev/null +++ b/row-with-maximum-ones.json @@ -0,0 +1,36 @@ +{ + "id": 2737, + "name": "row-with-maximum-ones", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/row-with-maximum-ones/", + "date": "2023-04-09", + "task_description": "Given a `m x n` binary matrix `mat`, find the **0-indexed** position of the row that contains the **maximum** count of **ones,** and the number of ones in that row. In case there are multiple rows that have the maximum count of ones, the row with the **smallest row number** should be selected. Return_ an array containing the index of the row, and the number of ones in it._ **Example 1:** ``` **Input:** mat = [[0,1],[1,0]] **Output:** [0,1] **Explanation:** Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1`)`. So, the answer is [0,1]. ``` **Example 2:** ``` **Input:** mat = [[0,0,0],[0,1,1]] **Output:** [1,2] **Explanation:** The row indexed 1 has the maximum count of ones `(2)`. So we return its index, `1`, and the count. So, the answer is [1,2]. ``` **Example 3:** ``` **Input:** mat = [[0,0],[1,1],[0,0]] **Output:** [1,2] **Explanation:** The row indexed 1 has the maximum count of ones (2). So the answer is [1,2]. ``` **Constraints:** `m == mat.length` `n == mat[i].length` `1 <= m, n <= 100` `mat[i][j]` is either `0` or `1`.", + "test_case": [ + { + "label": "Example 1", + "input": "mat = [[0,1],[1,0]]", + "output": "[0,1] " + }, + { + "label": "Example 2", + "input": "mat = [[0,0,0],[0,1,1]]", + "output": "[1,2] " + }, + { + "label": "Example 3", + "input": "mat = [[0,0],[1,1],[0,0]]", + "output": "[1,2] " + } + ], + "constraints": [ + "m == mat.length", + "n == mat[i].length", + "1 <= m, n <= 100", + "mat[i][j] is either 0 or 1." + ], + "python_template": "class Solution(object):\n def rowAndMaximumOnes(self, mat):\n \"\"\"\n :type mat: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] rowAndMaximumOnes(int[][] mat) {\n \n }\n}", + "metadata": { + "func_name": "rowAndMaximumOnes" + } +} \ No newline at end of file diff --git a/score-of-a-string.json b/score-of-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..4a7a1cbbd887741bd7d97f8cde5ee0b649ae6402 --- /dev/null +++ b/score-of-a-string.json @@ -0,0 +1,29 @@ +{ + "id": 3379, + "name": "score-of-a-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/score-of-a-string/", + "date": "2024-03-30", + "task_description": "You are given a string `s`. The **score** of a string is defined as the sum of the absolute difference between the **ASCII** values of adjacent characters. Return the **score** of_ _`s`. **Example 1:** **Input:** s = \"hello\" **Output:** 13 **Explanation:** The **ASCII** values of the characters in `s` are: `'h' = 104`, `'e' = 101`, `'l' = 108`, `'o' = 111`. So, the score of `s` would be `|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13`. **Example 2:** **Input:** s = \"zaz\" **Output:** 50 **Explanation:** The **ASCII** values of the characters in `s` are: `'z' = 122`, `'a' = 97`. So, the score of `s` would be `|122 - 97| + |97 - 122| = 25 + 25 = 50`. **Constraints:** `2 <= s.length <= 100` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"hello\"", + "output": "13 " + }, + { + "label": "Example 2", + "input": "s = \"zaz\"", + "output": "50 " + } + ], + "constraints": [ + "2 <= s.length <= 100", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def scoreOfString(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int scoreOfString(String s) {\n \n }\n}", + "metadata": { + "func_name": "scoreOfString" + } +} \ No newline at end of file diff --git a/select-k-disjoint-special-substrings.json b/select-k-disjoint-special-substrings.json new file mode 100644 index 0000000000000000000000000000000000000000..24c57ce1c993d5f2c46612679a5a86513db2d723 --- /dev/null +++ b/select-k-disjoint-special-substrings.json @@ -0,0 +1,40 @@ +{ + "id": 3771, + "name": "select-k-disjoint-special-substrings", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/select-k-disjoint-special-substrings/", + "date": "2025-02-09", + "task_description": "Given a string `s` of length `n` and an integer `k`, determine whether it is possible to select `k` disjoint **special substrings**. A **special substring** is a substring where: Any character present inside the substring should not appear outside it in the string. The substring is not the entire string `s`. **Note** that all `k` substrings must be disjoint, meaning they cannot overlap. Return `true` if it is possible to select `k` such disjoint special substrings; otherwise, return `false`. **Example 1:** **Input:** s = \"abcdbaefab\", k = 2 **Output:** true **Explanation:** We can select two disjoint special substrings: `\"cd\"` and `\"ef\"`. `\"cd\"` contains the characters `'c'` and `'d'`, which do not appear elsewhere in `s`. `\"ef\"` contains the characters `'e'` and `'f'`, which do not appear elsewhere in `s`. **Example 2:** **Input:** s = \"cdefdc\", k = 3 **Output:** false **Explanation:** There can be at most 2 disjoint special substrings: `\"e\"` and `\"f\"`. Since `k = 3`, the output is `false`. **Example 3:** **Input:** s = \"abeabe\", k = 0 **Output:** true **Constraints:** `2 <= n == s.length <= 5 * 104` `0 <= k <= 26` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcdbaefab\", k = 2", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"cdefdc\", k = 3", + "output": "false " + }, + { + "label": "Example 3", + "input": "s = \"abeabe\", k = 0", + "output": "true" + } + ], + "constraints": [ + "Any character present inside the substring should not appear outside it in the string.", + "The substring is not the entire string s.", + "We can select two disjoint special substrings: \"cd\" and \"ef\".", + "\"cd\" contains the characters 'c' and 'd', which do not appear elsewhere in s.", + "\"ef\" contains the characters 'e' and 'f', which do not appear elsewhere in s.", + "2 <= n == s.length <= 5 * 104", + "0 <= k <= 26", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def maxSubstringLength(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean maxSubstringLength(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxSubstringLength" + } +} \ No newline at end of file diff --git a/semi-ordered-permutation.json b/semi-ordered-permutation.json new file mode 100644 index 0000000000000000000000000000000000000000..3532d74fd5c344017928a42438537f57064720b5 --- /dev/null +++ b/semi-ordered-permutation.json @@ -0,0 +1,36 @@ +{ + "id": 2785, + "name": "semi-ordered-permutation", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/semi-ordered-permutation/", + "date": "2023-05-28", + "task_description": "You are given a **0-indexed** permutation of `n` integers `nums`. A permutation is called **semi-ordered** if the first number equals `1` and the last number equals `n`. You can perform the below operation as many times as you want until you make `nums` a **semi-ordered** permutation: Pick two adjacent elements in `nums`, then swap them. Return _the minimum number of operations to make _`nums`_ a **semi-ordered permutation**_. A **permutation** is a sequence of integers from `1` to `n` of length `n` containing each number exactly once. **Example 1:** ``` **Input:** nums = [2,1,4,3] **Output:** 2 **Explanation:** We can make the permutation semi-ordered using these sequence of operations: 1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3]. 2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4]. It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation. ``` **Example 2:** ``` **Input:** nums = [2,4,1,3] **Output:** 3 **Explanation:** We can make the permutation semi-ordered using these sequence of operations: 1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3]. 2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3]. 3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4]. It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation. ``` **Example 3:** ``` **Input:** nums = [1,3,4,2,5] **Output:** 0 **Explanation:** The permutation is already a semi-ordered permutation. ``` **Constraints:** `2 <= nums.length == n <= 50` `1 <= nums[i] <= 50` `nums is a permutation.`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,4,3]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [2,4,1,3]", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [1,3,4,2,5]", + "output": "0 " + } + ], + "constraints": [ + "Pick two adjacent elements in nums, then swap them.", + "2 <= nums.length == n <= 50", + "1 <= nums[i]Ā <= 50", + "nums is a permutation." + ], + "python_template": "class Solution(object):\n def semiOrderedPermutation(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int semiOrderedPermutation(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "semiOrderedPermutation" + } +} \ No newline at end of file diff --git a/separate-black-and-white-balls.json b/separate-black-and-white-balls.json new file mode 100644 index 0000000000000000000000000000000000000000..3e8c660c8e5744490193669a67e91b374c77b601 --- /dev/null +++ b/separate-black-and-white-balls.json @@ -0,0 +1,34 @@ +{ + "id": 3195, + "name": "separate-black-and-white-balls", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/separate-black-and-white-balls/", + "date": "2023-11-12", + "task_description": "There are `n` balls on a table, each ball has a color black or white. You are given a **0-indexed** binary string `s` of length `n`, where `1` and `0` represent black and white balls, respectively. In each step, you can choose two adjacent balls and swap them. Return _the **minimum** number of steps to group all the black balls to the right and all the white balls to the left_. **Example 1:** ``` **Input:** s = \"101\" **Output:** 1 **Explanation:** We can group all the black balls to the right in the following way: - Swap s[0] and s[1], s = \"011\". Initially, 1s are not grouped together, requiring at least 1 step to group them to the right. ``` **Example 2:** ``` **Input:** s = \"100\" **Output:** 2 **Explanation:** We can group all the black balls to the right in the following way: - Swap s[0] and s[1], s = \"010\". - Swap s[1] and s[2], s = \"001\". It can be proven that the minimum number of steps needed is 2. ``` **Example 3:** ``` **Input:** s = \"0111\" **Output:** 0 **Explanation:** All the black balls are already grouped to the right. ``` **Constraints:** `1 <= n == s.length <= 105` `s[i]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"101\"", + "output": "1 " + }, + { + "label": "Example 2", + "input": "s = \"100\"", + "output": "2 " + }, + { + "label": "Example 3", + "input": "s = \"0111\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= n == s.length <= 105", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def minimumSteps(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumSteps(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimumSteps" + } +} \ No newline at end of file diff --git a/separate-squares-ii.json b/separate-squares-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..0af4e5f349ccf70dad2dd4a1398566b473f16b85 --- /dev/null +++ b/separate-squares-ii.json @@ -0,0 +1,33 @@ +{ + "id": 3775, + "name": "separate-squares-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/separate-squares-ii/", + "date": "2025-02-01", + "task_description": "You are given a 2D integer array `squares`. Each `squares[i] = [xi, yi, li]` represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis. Find the **minimum** y-coordinate value of a horizontal line such that the total area covered by squares above the line _equals_ the total area covered by squares below the line. Answers within `10-5` of the actual answer will be accepted. **Note**: Squares **may** overlap. Overlapping areas should be counted **only once** in this version. **Example 1:** **Input:** squares = [[0,0,1],[2,2,1]] **Output:** 1.00000 **Explanation:** Any horizontal line between `y = 1` and `y = 2` results in an equal split, with 1 square unit above and 1 square unit below. The minimum y-value is 1. **Example 2:** **Input:** squares = [[0,0,2],[1,1,1]] **Output:** 1.00000 **Explanation:** Since the blue square overlaps with the red square, it will not be counted again. Thus, the line `y = 1` splits the squares into two equal parts. **Constraints:** `1 <= squares.length <= 5 * 104` `squares[i] = [xi, yi, li]` `squares[i].length == 3` `0 <= xi, yi <= 109` `1 <= li <= 109` The total area of all the squares will not exceed `1015`.", + "test_case": [ + { + "label": "Example 1", + "input": "squares = [[0,0,1],[2,2,1]]", + "output": "1.00000 " + }, + { + "label": "Example 2", + "input": "squares = [[0,0,2],[1,1,1]]", + "output": "1.00000 " + } + ], + "constraints": [ + "1 <= squares.length <= 5 * 104", + "squares[i] = [xi, yi, li]", + "squares[i].length == 3", + "0 <= xi, yi <= 109", + "1 <= li <= 109", + "The total area of all the squares will not exceed 1015." + ], + "python_template": "class Solution(object):\n def separateSquares(self, squares):\n \"\"\"\n :type squares: List[List[int]]\n :rtype: float\n \"\"\"\n ", + "java_template": "class Solution {\n public double separateSquares(int[][] squares) {\n \n }\n}", + "metadata": { + "func_name": "separateSquares" + } +} \ No newline at end of file diff --git a/separate-the-digits-in-an-array.json b/separate-the-digits-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..4655e14c6414149788796a372f4fbc8c40776230 --- /dev/null +++ b/separate-the-digits-in-an-array.json @@ -0,0 +1,30 @@ +{ + "id": 2639, + "name": "separate-the-digits-in-an-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/separate-the-digits-in-an-array/", + "date": "2023-01-21", + "task_description": "Given an array of positive integers `nums`, return _an array _`answer`_ that consists of the digits of each integer in _`nums`_ after separating them in **the same order** they appear in _`nums`. To separate the digits of an integer is to get all the digits it has in the same order. For example, for the integer `10921`, the separation of its digits is `[1,0,9,2,1]`. **Example 1:** ``` **Input:** nums = [13,25,83,77] **Output:** [1,3,2,5,8,3,7,7] **Explanation:** - The separation of 13 is [1,3]. - The separation of 25 is [2,5]. - The separation of 83 is [8,3]. - The separation of 77 is [7,7]. answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order. ``` **Example 2:** ``` **Input:** nums = [7,1,3,9] **Output:** [7,1,3,9] **Explanation:** The separation of each integer in nums is itself. answer = [7,1,3,9]. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [13,25,83,77]", + "output": "[1,3,2,5,8,3,7,7] " + }, + { + "label": "Example 2", + "input": "nums = [7,1,3,9]", + "output": "[7,1,3,9] " + } + ], + "constraints": [ + "For example, for the integer 10921, the separation of its digits is [1,0,9,2,1].", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def separateDigits(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] separateDigits(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "separateDigits" + } +} \ No newline at end of file diff --git a/shift-distance-between-two-strings.json b/shift-distance-between-two-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..4e17e957830af057f01d7980963aa3b0f6d6ef8b --- /dev/null +++ b/shift-distance-between-two-strings.json @@ -0,0 +1,41 @@ +{ + "id": 3591, + "name": "shift-distance-between-two-strings", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/shift-distance-between-two-strings/", + "date": "2024-11-09", + "task_description": "You are given two strings `s` and `t` of the same length, and two integer arrays `nextCost` and `previousCost`. In one operation, you can pick any index `i` of `s`, and perform **either one** of the following actions: Shift `s[i]` to the next letter in the alphabet. If `s[i] == 'z'`, you should replace it with `'a'`. This operation costs `nextCost[j]` where `j` is the index of `s[i]` in the alphabet. Shift `s[i]` to the previous letter in the alphabet. If `s[i] == 'a'`, you should replace it with `'z'`. This operation costs `previousCost[j]` where `j` is the index of `s[i]` in the alphabet. The **shift distance** is the **minimum** total cost of operations required to transform `s` into `t`. Return the **shift distance** from `s` to `t`. **Example 1:** **Input:** s = \"abab\", t = \"baba\", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] **Output:** 2 **Explanation:** We choose index `i = 0` and shift `s[0]` 25 times to the previous character for a total cost of 1. We choose index `i = 1` and shift `s[1]` 25 times to the next character for a total cost of 0. We choose index `i = 2` and shift `s[2]` 25 times to the previous character for a total cost of 1. We choose index `i = 3` and shift `s[3]` 25 times to the next character for a total cost of 0. **Example 2:** **Input:** s = \"leet\", t = \"code\", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] **Output:** 31 **Explanation:** We choose index `i = 0` and shift `s[0]` 9 times to the previous character for a total cost of 9. We choose index `i = 1` and shift `s[1]` 10 times to the next character for a total cost of 10. We choose index `i = 2` and shift `s[2]` 1 time to the previous character for a total cost of 1. We choose index `i = 3` and shift `s[3]` 11 times to the next character for a total cost of 11. **Constraints:** `1 <= s.length == t.length <= 105` `s` and `t` consist only of lowercase English letters. `nextCost.length == previousCost.length == 26` `0 <= nextCost[i], previousCost[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abab\", t = \"baba\", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"leet\", t = \"code\", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]", + "output": "31 " + } + ], + "constraints": [ + "Shift s[i] to the next letter in the alphabet. If s[i] == 'z', you should replace it with 'a'. This operation costs nextCost[j] where j is the index of s[i] in the alphabet.", + "Shift s[i] to the previous letter in the alphabet. If s[i] == 'a', you should replace it with 'z'. This operation costs previousCost[j] where j is the index of s[i] in the alphabet.", + "We choose index i = 0 and shift s[0] 25 times to the previous character for a total cost of 1.", + "We choose index i = 1 and shift s[1] 25 times to the next character for a total cost of 0.", + "We choose index i = 2 and shift s[2] 25 times to the previous character for a total cost of 1.", + "We choose index i = 3 and shift s[3] 25 times to the next character for a total cost of 0.", + "We choose index i = 0 and shift s[0] 9 times to the previous character for a total cost of 9.", + "We choose index i = 1 and shift s[1] 10 times to the next character for a total cost of 10.", + "We choose index i = 2 and shift s[2] 1 time to the previous character for a total cost of 1.", + "We choose index i = 3 and shift s[3] 11 times to the next character for a total cost of 11.", + "1 <= s.length == t.length <= 105", + "s and t consist only of lowercase English letters.", + "nextCost.length == previousCost.length == 26", + "0 <= nextCost[i], previousCost[i] <= 109" + ], + "python_template": "class Solution(object):\n def shiftDistance(self, s, t, nextCost, previousCost):\n \"\"\"\n :type s: str\n :type t: str\n :type nextCost: List[int]\n :type previousCost: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) {\n \n }\n}", + "metadata": { + "func_name": "shiftDistance" + } +} \ No newline at end of file diff --git a/shifting-letters-ii.json b/shifting-letters-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..3aa086f420b2eeb578a436fd1b1cf368be54b11c --- /dev/null +++ b/shifting-letters-ii.json @@ -0,0 +1,32 @@ +{ + "id": 2465, + "name": "shifting-letters-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/shifting-letters-ii/", + "date": "2022-08-06", + "task_description": "You are given a string `s` of lowercase English letters and a 2D integer array `shifts` where `shifts[i] = [starti, endi, directioni]`. For every `i`, **shift** the characters in `s` from the index `starti` to the index `endi` (**inclusive**) forward if `directioni = 1`, or shift the characters backward if `directioni = 0`. Shifting a character **forward** means replacing it with the **next** letter in the alphabet (wrapping around so that `'z'` becomes `'a'`). Similarly, shifting a character **backward** means replacing it with the **previous** letter in the alphabet (wrapping around so that `'a'` becomes `'z'`). Return _the final string after all such shifts to _`s`_ are applied_. **Example 1:** ``` **Input:** s = \"abc\", shifts = [[0,1,0],[1,2,1],[0,2,1]] **Output:** \"ace\" **Explanation:** Firstly, shift the characters from index 0 to index 1 backward. Now s = \"zac\". Secondly, shift the characters from index 1 to index 2 forward. Now s = \"zbd\". Finally, shift the characters from index 0 to index 2 forward. Now s = \"ace\". ``` **Example 2:** ``` **Input:** s = \"dztz\", shifts = [[0,0,0],[1,1,1]] **Output:** \"catz\" **Explanation:** Firstly, shift the characters from index 0 to index 0 backward. Now s = \"cztz\". Finally, shift the characters from index 1 to index 1 forward. Now s = \"catz\". ``` **Constraints:** `1 <= s.length, shifts.length <= 5 * 104` `shifts[i].length == 3` `0 <= starti <= endi < s.length` `0 <= directioni <= 1` `s` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abc\", shifts = [[0,1,0],[1,2,1],[0,2,1]]", + "output": "\"ace\" " + }, + { + "label": "Example 2", + "input": "s = \"dztz\", shifts = [[0,0,0],[1,1,1]]", + "output": "\"catz\" " + } + ], + "constraints": [ + "1 <= s.length, shifts.length <= 5 * 104", + "shifts[i].length == 3", + "0 <= starti <= endi < s.length", + "0 <= directioni <= 1", + "s consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def shiftingLetters(self, s, shifts):\n \"\"\"\n :type s: str\n :type shifts: List[List[int]]\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String shiftingLetters(String s, int[][] shifts) {\n \n }\n}", + "metadata": { + "func_name": "shiftingLetters" + } +} \ No newline at end of file diff --git a/shortest-cycle-in-a-graph.json b/shortest-cycle-in-a-graph.json new file mode 100644 index 0000000000000000000000000000000000000000..1cfec40769f4e94b89aa47adbabbce52aab230cb --- /dev/null +++ b/shortest-cycle-in-a-graph.json @@ -0,0 +1,33 @@ +{ + "id": 2671, + "name": "shortest-cycle-in-a-graph", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/shortest-cycle-in-a-graph/", + "date": "2023-03-18", + "task_description": "There is a **bi-directional **graph with `n` vertices, where each vertex is labeled from `0` to `n - 1`. The edges in the graph are represented by a given 2D integer array `edges`, where `edges[i] = [ui, vi]` denotes an edge between vertex `ui` and vertex `vi`. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. Return _the length of the **shortest **cycle in the graph_. If no cycle exists, return `-1`. A cycle is a path that starts and ends at the same node, and each edge in the path is used only once. **Example 1:** ``` **Input:** n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]] **Output:** 3 **Explanation:** The cycle with the smallest length is : 0 -> 1 -> 2 -> 0 ``` **Example 2:** ``` **Input:** n = 4, edges = [[0,1],[0,2]] **Output:** -1 **Explanation:** There are no cycles in this graph. ``` **Constraints:** `2 <= n <= 1000` `1 <= edges.length <= 1000` `edges[i].length == 2` `0 <= ui, vi < n` `ui != vi` There are no repeated edges.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "n = 4, edges = [[0,1],[0,2]]", + "output": "-1 " + } + ], + "constraints": [ + "2 <= n <= 1000", + "1 <= edges.length <= 1000", + "edges[i].length == 2", + "0 <= ui, vi < n", + "ui != vi", + "There are no repeated edges." + ], + "python_template": "class Solution(object):\n def findShortestCycle(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findShortestCycle(int n, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "findShortestCycle" + } +} \ No newline at end of file diff --git a/shortest-impossible-sequence-of-rolls.json b/shortest-impossible-sequence-of-rolls.json new file mode 100644 index 0000000000000000000000000000000000000000..508b3ce9e44a8709ee588f97d1d02f21cf2b2adf --- /dev/null +++ b/shortest-impossible-sequence-of-rolls.json @@ -0,0 +1,35 @@ +{ + "id": 2435, + "name": "shortest-impossible-sequence-of-rolls", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/", + "date": "2022-07-09", + "task_description": "You are given an integer array `rolls` of length `n` and an integer `k`. You roll a `k` sided dice numbered from `1` to `k`, `n` times, where the result of the `ith` roll is `rolls[i]`. Return_ the length of the **shortest** sequence of rolls so that there's no such subsequence in _`rolls`. A **sequence of rolls** of length `len` is the result of rolling a `k` sided dice `len` times. **Example 1:** ``` **Input:** rolls = [4,2,1,2,3,3,2,4,1], k = 4 **Output:** 3 **Explanation:** Every sequence of rolls of length 1, [1], [2], [3], [4], can be taken from rolls. Every sequence of rolls of length 2, [1, 1], [1, 2], ..., [4, 4], can be taken from rolls. The sequence [1, 4, 2] cannot be taken from rolls, so we return 3. Note that there are other sequences that cannot be taken from rolls. ``` **Example 2:** ``` **Input:** rolls = [1,1,2,2], k = 2 **Output:** 2 **Explanation:** Every sequence of rolls of length 1, [1], [2], can be taken from rolls. The sequence [2, 1] cannot be taken from rolls, so we return 2. Note that there are other sequences that cannot be taken from rolls but [2, 1] is the shortest. ``` **Example 3:** ``` **Input:** rolls = [1,1,3,2,2,2,3,3], k = 4 **Output:** 1 **Explanation:** The sequence [4] cannot be taken from rolls, so we return 1. Note that there are other sequences that cannot be taken from rolls but [4] is the shortest. ``` **Constraints:** `n == rolls.length` `1 <= n <= 105` `1 <= rolls[i] <= k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "rolls = [4,2,1,2,3,3,2,4,1], k = 4", + "output": "3 " + }, + { + "label": "Example 2", + "input": "rolls = [1,1,2,2], k = 2", + "output": "2 " + }, + { + "label": "Example 3", + "input": "rolls = [1,1,3,2,2,2,3,3], k = 4", + "output": "1 " + } + ], + "constraints": [ + "n == rolls.length", + "1 <= n <= 105", + "1 <= rolls[i] <= k <= 105" + ], + "python_template": "class Solution(object):\n def shortestSequence(self, rolls, k):\n \"\"\"\n :type rolls: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int shortestSequence(int[] rolls, int k) {\n \n }\n}", + "metadata": { + "func_name": "shortestSequence" + } +} \ No newline at end of file diff --git a/shortest-matching-substring.json b/shortest-matching-substring.json new file mode 100644 index 0000000000000000000000000000000000000000..879c774e62d17fbd09a1923207754e8379475e93 --- /dev/null +++ b/shortest-matching-substring.json @@ -0,0 +1,41 @@ +{ + "id": 3692, + "name": "shortest-matching-substring", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/shortest-matching-substring/", + "date": "2025-02-01", + "task_description": "You are given a string `s` and a pattern string `p`, where `p` contains **exactly two** `'*'` characters. The `'*'` in `p` matches any sequence of zero or more characters. Return the length of the **shortest** substring in `s` that matches `p`. If there is no such substring, return -1. **Note:** The empty substring is considered valid. **Example 1:** **Input:** s = \"abaacbaecebce\", p = \"ba*c*ce\" **Output:** 8 **Explanation:** The shortest matching substring of `p` in `s` is `\"**ba**e**c**eb**ce**\"`. **Example 2:** **Input:** s = \"baccbaadbc\", p = \"cc*baa*adb\" **Output:** -1 **Explanation:** There is no matching substring in `s`. **Example 3:** **Input:** s = \"a\", p = \"**\" **Output:** 0 **Explanation:** The empty substring is the shortest matching substring. **Example 4:** **Input:** s = \"madlogic\", p = \"*adlogi*\" **Output:** 6 **Explanation:** The shortest matching substring of `p` in `s` is `\"**adlogi**\"`. **Constraints:** `1 <= s.length <= 105` `2 <= p.length <= 105` `s` contains only lowercase English letters. `p` contains only lowercase English letters and exactly two `'*'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abaacbaecebce\", p = \"ba*c*ce\"", + "output": "8 " + }, + { + "label": "Example 2", + "input": "s = \"baccbaadbc\", p = \"cc*baa*adb\"", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "s = \"a\", p = \"**\"", + "output": "0 " + }, + { + "label": "Example 4", + "input": "s = \"madlogic\", p = \"*adlogi*\"", + "output": "6 " + } + ], + "constraints": [ + "1 <= s.length <= 105", + "2 <= p.length <= 105", + "s contains only lowercase English letters.", + "p contains only lowercase English letters and exactly two '*'." + ], + "python_template": "class Solution(object):\n def shortestMatchingSubstring(self, s, p):\n \"\"\"\n :type s: str\n :type p: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int shortestMatchingSubstring(String s, String p) {\n \n }\n}", + "metadata": { + "func_name": "shortestMatchingSubstring" + } +} \ No newline at end of file diff --git a/shortest-string-that-contains-three-strings.json b/shortest-string-that-contains-three-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..af3755e491355959d525f64da6730e0227dc0c7d --- /dev/null +++ b/shortest-string-that-contains-three-strings.json @@ -0,0 +1,31 @@ +{ + "id": 2877, + "name": "shortest-string-that-contains-three-strings", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/shortest-string-that-contains-three-strings/", + "date": "2023-07-23", + "task_description": "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** than a string `b` (of the same length) if in the first position where `a` and `b` differ, string `a` has a letter that appears **earlier **in the alphabet than the corresponding letter in `b`. A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` **Input:** a = \"abc\", b = \"bca\", c = \"aaa\" **Output:** \"aaabca\" **Explanation:** We show that \"aaabca\" contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and \"aaabca\" is the lexicographically smallest one. ``` **Example 2:** ``` **Input:** a = \"ab\", b = \"ba\", c = \"aba\" **Output:** \"aba\" **Explanation: **We show that the string \"aba\" contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that \"aba\" is the lexicographically smallest one. ``` **Constraints:** `1 <= a.length, b.length, c.length <= 100` `a`, `b`, `c` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "a = \"abc\", b = \"bca\", c = \"aaa\"", + "output": "\"aaabca\" " + }, + { + "label": "Example 2", + "input": "a = \"ab\", b = \"ba\", c = \"aba\"", + "output": "\"aba\" " + } + ], + "constraints": [ + "A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b.", + "A substring is a contiguous sequence of characters within a string.", + "1 <= a.length, b.length, c.length <= 100", + "a, b, c consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumString(self, a, b, c):\n \"\"\"\n :type a: str\n :type b: str\n :type c: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String minimumString(String a, String b, String c) {\n \n }\n}", + "metadata": { + "func_name": "minimumString" + } +} \ No newline at end of file diff --git a/shortest-subarray-with-or-at-least-k-i.json b/shortest-subarray-with-or-at-least-k-i.json new file mode 100644 index 0000000000000000000000000000000000000000..da3a404cf1224abb79a4ebbe157a5d58899ae2d4 --- /dev/null +++ b/shortest-subarray-with-or-at-least-k-i.json @@ -0,0 +1,35 @@ +{ + "id": 3381, + "name": "shortest-subarray-with-or-at-least-k-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i/", + "date": "2024-03-16", + "task_description": "You are given an array `nums` of **non-negative** integers and an integer `k`. An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`. Return _the length of the **shortest** **special** **non-empty** subarray of_ `nums`, _or return_ `-1` _if no special subarray exists_. **Example 1:** **Input:** nums = [1,2,3], k = 2 **Output:** 1 **Explanation:** The subarray `[3]` has `OR` value of `3`. Hence, we return `1`. Note that `[2]` is also a special subarray. **Example 2:** **Input:** nums = [2,1,8], k = 10 **Output:** 3 **Explanation:** The subarray `[2,1,8]` has `OR` value of `11`. Hence, we return `3`. **Example 3:** **Input:** nums = [1,2], k = 0 **Output:** 1 **Explanation:** The subarray `[1]` has `OR` value of `1`. Hence, we return `1`. **Constraints:** `1 <= nums.length <= 50` `0 <= nums[i] <= 50` `0 <= k < 64`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3], k = 2", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [2,1,8], k = 10", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [1,2], k = 0", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "0 <= nums[i] <= 50", + "0 <= k < 64" + ], + "python_template": "class Solution(object):\n def minimumSubarrayLength(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumSubarrayLength(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumSubarrayLength" + } +} \ No newline at end of file diff --git a/shortest-subarray-with-or-at-least-k-ii.json b/shortest-subarray-with-or-at-least-k-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..887e09e1073e698b11b1ebc170d5a8f892eb48d0 --- /dev/null +++ b/shortest-subarray-with-or-at-least-k-ii.json @@ -0,0 +1,35 @@ +{ + "id": 3380, + "name": "shortest-subarray-with-or-at-least-k-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii/", + "date": "2024-03-16", + "task_description": "You are given an array `nums` of **non-negative** integers and an integer `k`. An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`. Return _the length of the **shortest** **special** **non-empty** subarray of_ `nums`, _or return_ `-1` _if no special subarray exists_. **Example 1:** **Input:** nums = [1,2,3], k = 2 **Output:** 1 **Explanation:** The subarray `[3]` has `OR` value of `3`. Hence, we return `1`. **Example 2:** **Input:** nums = [2,1,8], k = 10 **Output:** 3 **Explanation:** The subarray `[2,1,8]` has `OR` value of `11`. Hence, we return `3`. **Example 3:** **Input:** nums = [1,2], k = 0 **Output:** 1 **Explanation:** The subarray `[1]` has `OR` value of `1`. Hence, we return `1`. **Constraints:** `1 <= nums.length <= 2 * 105` `0 <= nums[i] <= 109` `0 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3], k = 2", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [2,1,8], k = 10", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [1,2], k = 0", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 2 * 105", + "0 <= nums[i] <= 109", + "0 <= k <= 109" + ], + "python_template": "class Solution(object):\n def minimumSubarrayLength(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumSubarrayLength(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumSubarrayLength" + } +} \ No newline at end of file diff --git a/shortest-uncommon-substring-in-an-array.json b/shortest-uncommon-substring-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..d6bc90323fc5611cf62e7dd8f9b8036a44899c86 --- /dev/null +++ b/shortest-uncommon-substring-in-an-array.json @@ -0,0 +1,32 @@ +{ + "id": 3356, + "name": "shortest-uncommon-substring-in-an-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/shortest-uncommon-substring-in-an-array/", + "date": "2024-03-03", + "task_description": "You are given an array `arr` of size `n` consisting of **non-empty** strings. Find a string array `answer` of size `n` such that: `answer[i]` is the **shortest** substring of `arr[i]` that does **not** occur as a substring in any other string in `arr`. If multiple such substrings exist, `answer[i]` should be the lexicographically smallest. And if no such substring exists, `answer[i]` should be an empty string. Return _the array _`answer`. **Example 1:** ``` **Input:** arr = [\"cab\",\"ad\",\"bad\",\"c\"] **Output:** [\"ab\",\"\",\"ba\",\"\"] **Explanation:** We have the following: - For the string \"cab\", the shortest substring that does not occur in any other string is either \"ca\" or \"ab\", we choose the lexicographically smaller substring, which is \"ab\". - For the string \"ad\", there is no substring that does not occur in any other string. - For the string \"bad\", the shortest substring that does not occur in any other string is \"ba\". - For the string \"c\", there is no substring that does not occur in any other string. ``` **Example 2:** ``` **Input:** arr = [\"abc\",\"bcd\",\"abcd\"] **Output:** [\"\",\"\",\"abcd\"] **Explanation:** We have the following: - For the string \"abc\", there is no substring that does not occur in any other string. - For the string \"bcd\", there is no substring that does not occur in any other string. - For the string \"abcd\", the shortest substring that does not occur in any other string is \"abcd\". ``` **Constraints:** `n == arr.length` `2 <= n <= 100` `1 <= arr[i].length <= 20` `arr[i]` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "arr = [\"cab\",\"ad\",\"bad\",\"c\"]", + "output": "[\"ab\",\"\",\"ba\",\"\"] " + }, + { + "label": "Example 2", + "input": "arr = [\"abc\",\"bcd\",\"abcd\"]", + "output": "[\"\",\"\",\"abcd\"] " + } + ], + "constraints": [ + "answer[i] is the shortest substring of arr[i] that does not occur as a substring in any other string in arr. If multiple such substrings exist, answer[i] should be the lexicographically smallest. And if no such substring exists, answer[i] should be an empty string.", + "n == arr.length", + "2 <= n <= 100", + "1 <= arr[i].length <= 20", + "arr[i] consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def shortestSubstrings(self, arr):\n \"\"\"\n :type arr: List[str]\n :rtype: List[str]\n \"\"\"\n ", + "java_template": "class Solution {\n public String[] shortestSubstrings(String[] arr) {\n \n }\n}", + "metadata": { + "func_name": "shortestSubstrings" + } +} \ No newline at end of file diff --git a/sliding-subarray-beauty.json b/sliding-subarray-beauty.json new file mode 100644 index 0000000000000000000000000000000000000000..098df894ca87a40fdd9b9d5e91b1adc8c73342bd --- /dev/null +++ b/sliding-subarray-beauty.json @@ -0,0 +1,38 @@ +{ + "id": 2751, + "name": "sliding-subarray-beauty", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sliding-subarray-beauty/", + "date": "2023-04-16", + "task_description": "Given an integer array `nums` containing `n` integers, find the **beauty** of each subarray of size `k`. The **beauty** of a subarray is the `xth`** smallest integer **in the subarray if it is **negative**, or `0` if there are fewer than `x` negative integers. Return _an integer array containing _`n - k + 1` _integers, which denote the _**beauty**_ of the subarrays **in order** from the first index in the array._ A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,-1,-3,-2,3], k = 3, x = 2 **Output:** [-1,-2,-2] **Explanation:** There are 3 subarrays with size k = 3. The first subarray is `[1, -1, -3]` and the 2nd smallest negative integer is -1. The second subarray is `[-1, -3, -2]` and the 2nd smallest negative integer is -2. The third subarray is `[-3, -2, 3] `and the 2nd smallest negative integer is -2. ``` **Example 2:** ``` **Input:** nums = [-1,-2,-3,-4,-5], k = 2, x = 2 **Output:** [-1,-2,-3,-4] **Explanation:** There are 4 subarrays with size k = 2. For `[-1, -2]`, the 2nd smallest negative integer is -1. For `[-2, -3]`, the 2nd smallest negative integer is -2. For `[-3, -4]`, the 2nd smallest negative integer is -3. For `[-4, -5]`, the 2nd smallest negative integer is -4. ``` **Example 3:** ``` **Input:** nums = [-3,1,2,-3,0,-3], k = 2, x = 1 **Output:** [-3,0,-3,-3,-3] **Explanation:** There are 5 subarrays with size k = 2**.** For `[-3, 1]`, the 1st smallest negative integer is -3. For `[1, 2]`, there is no negative integer so the beauty is 0. For `[2, -3]`, the 1st smallest negative integer is -3. For `[-3, 0]`, the 1st smallest negative integer is -3. For `[0, -3]`, the 1st smallest negative integer is -3. ``` **Constraints:** `n == nums.length ` `1 <= n <= 105` `1 <= k <= n` `1 <= x <= k ` `-50 <= nums[i] <= 50 `", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,-1,-3,-2,3], k = 3, x = 2", + "output": "[-1,-2,-2] " + }, + { + "label": "Example 2", + "input": "nums = [-1,-2,-3,-4,-5], k = 2, x = 2", + "output": "[-1,-2,-3,-4] " + }, + { + "label": "Example 3", + "input": "nums = [-3,1,2,-3,0,-3], k = 2, x = 1", + "output": "[-3,0,-3,-3,-3] " + } + ], + "constraints": [ + "A subarray is a contiguous non-empty sequence of elements within an array.", + "n == nums.length", + "1 <= n <= 105", + "1 <= k <= n", + "1 <= x <= k", + "-50Ā <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def getSubarrayBeauty(self, nums, k, x):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type x: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] getSubarrayBeauty(int[] nums, int k, int x) {\n \n }\n}", + "metadata": { + "func_name": "getSubarrayBeauty" + } +} \ No newline at end of file diff --git a/smallest-divisible-digit-product-i.json b/smallest-divisible-digit-product-i.json new file mode 100644 index 0000000000000000000000000000000000000000..632c1b52d31aea9a1974500de59449973eb24988 --- /dev/null +++ b/smallest-divisible-digit-product-i.json @@ -0,0 +1,29 @@ +{ + "id": 3626, + "name": "smallest-divisible-digit-product-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/smallest-divisible-digit-product-i/", + "date": "2024-10-26", + "task_description": "You are given two integers `n` and `t`. Return the **smallest** number greater than or equal to `n` such that the **product of its digits** is divisible by `t`. **Example 1:** **Input:** n = 10, t = 2 **Output:** 10 **Explanation:** The digit product of 10 is 0, which is divisible by 2, making it the smallest number greater than or equal to 10 that satisfies the condition. **Example 2:** **Input:** n = 15, t = 3 **Output:** 16 **Explanation:** The digit product of 16 is 6, which is divisible by 3, making it the smallest number greater than or equal to 15 that satisfies the condition. **Constraints:** `1 <= n <= 100` `1 <= t <= 10`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 10, t = 2", + "output": "10 " + }, + { + "label": "Example 2", + "input": "n = 15, t = 3", + "output": "16 " + } + ], + "constraints": [ + "1 <= n <= 100", + "1 <= t <= 10" + ], + "python_template": "class Solution(object):\n def smallestNumber(self, n, t):\n \"\"\"\n :type n: int\n :type t: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int smallestNumber(int n, int t) {\n \n }\n}", + "metadata": { + "func_name": "smallestNumber" + } +} \ No newline at end of file diff --git a/smallest-divisible-digit-product-ii.json b/smallest-divisible-digit-product-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..b407f43494bb27546314e39378bbd34c33270ff0 --- /dev/null +++ b/smallest-divisible-digit-product-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3635, + "name": "smallest-divisible-digit-product-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/smallest-divisible-digit-product-ii/", + "date": "2024-10-26", + "task_description": "You are given a string `num` which represents a **positive** integer, and an integer `t`. A number is called **zero-free** if _none_ of its digits are 0. Return a string representing the **smallest** **zero-free** number greater than or equal to `num` such that the **product of its digits** is divisible by `t`. If no such number exists, return `\"-1\"`. **Example 1:** **Input:** num = \"1234\", t = 256 **Output:** \"1488\" **Explanation:** The smallest zero-free number that is greater than 1234 and has the product of its digits divisible by 256 is 1488, with the product of its digits equal to 256. **Example 2:** **Input:** num = \"12355\", t = 50 **Output:** \"12355\" **Explanation:** 12355 is already zero-free and has the product of its digits divisible by 50, with the product of its digits equal to 150. **Example 3:** **Input:** num = \"11111\", t = 26 **Output:** \"-1\" **Explanation:** No number greater than 11111 has the product of its digits divisible by 26. **Constraints:** `2 <= num.length <= 2 * 105` `num` consists only of digits in the range `['0', '9']`. `num` does not contain leading zeros. `1 <= t <= 1014`", + "test_case": [ + { + "label": "Example 1", + "input": "num = \"1234\", t = 256", + "output": "\"1488\" " + }, + { + "label": "Example 2", + "input": "num = \"12355\", t = 50", + "output": "\"12355\" " + }, + { + "label": "Example 3", + "input": "num = \"11111\", t = 26", + "output": "\"-1\" " + } + ], + "constraints": [ + "2 <= num.length <= 2 * 105", + "num consists only of digits in the range ['0', '9'].", + "num does not contain leading zeros.", + "1 <= t <= 1014" + ], + "python_template": "class Solution(object):\n def smallestNumber(self, num, t):\n \"\"\"\n :type num: str\n :type t: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String smallestNumber(String num, long t) {\n \n }\n}", + "metadata": { + "func_name": "smallestNumber" + } +} \ No newline at end of file diff --git a/smallest-missing-integer-greater-than-sequential-prefix-sum.json b/smallest-missing-integer-greater-than-sequential-prefix-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..c6cc206129c50ba0241fa4c96933d008fd8fb2e5 --- /dev/null +++ b/smallest-missing-integer-greater-than-sequential-prefix-sum.json @@ -0,0 +1,29 @@ +{ + "id": 3236, + "name": "smallest-missing-integer-greater-than-sequential-prefix-sum", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum/", + "date": "2023-12-23", + "task_description": "You are given a **0-indexed** array of integers `nums`. A prefix `nums[0..i]` is **sequential** if, for all `1 <= j <= i`, `nums[j] = nums[j - 1] + 1`. In particular, the prefix consisting only of `nums[0]` is **sequential**. Return _the **smallest** integer_ `x` _missing from_ `nums` _such that_ `x` _is greater than or equal to the sum of the **longest** sequential prefix._ **Example 1:** ``` **Input:** nums = [1,2,3,2,5] **Output:** 6 **Explanation:** The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix. ``` **Example 2:** ``` **Input:** nums = [3,4,5,1,12,14,13] **Output:** 15 **Explanation:** The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix. ``` **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,2,5]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [3,4,5,1,12,14,13]", + "output": "15 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def missingInteger(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int missingInteger(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "missingInteger" + } +} \ No newline at end of file diff --git a/smallest-missing-non-negative-integer-after-operations.json b/smallest-missing-non-negative-integer-after-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..97b78a6eb59a29ceb8aeecf5663b7cff0e0ba874 --- /dev/null +++ b/smallest-missing-non-negative-integer-after-operations.json @@ -0,0 +1,31 @@ +{ + "id": 2661, + "name": "smallest-missing-non-negative-integer-after-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/", + "date": "2023-03-12", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `value`. In one operation, you can add or subtract `value` from any element of `nums`. For example, if `nums = [1,2,3]` and `value = 2`, you can choose to subtract `value` from `nums[0]` to make `nums = [-1,2,3]`. The MEX (minimum excluded) of an array is the smallest missing **non-negative** integer in it. For example, the MEX of `[-1,2,3]` is `0` while the MEX of `[1,0,3]` is `2`. Return _the maximum MEX of _`nums`_ after applying the mentioned operation **any number of times**_. **Example 1:** ``` **Input:** nums = [1,-10,7,13,6,8], value = 5 **Output:** 4 **Explanation:** One can achieve this result by applying the following operations: - Add value to nums[1] twice to make nums = [1,**0**,7,13,6,8] - Subtract value from nums[2] once to make nums = [1,0,**2**,13,6,8] - Subtract value from nums[3] twice to make nums = [1,0,2,**3**,6,8] The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve. ``` **Example 2:** ``` **Input:** nums = [1,-10,7,13,6,8], value = 7 **Output:** 2 **Explanation:** One can achieve this result by applying the following operation: - subtract value from nums[2] once to make nums = [1,-10,**0**,13,6,8] The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve. ``` **Constraints:** `1 <= nums.length, value <= 105` `-109 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,-10,7,13,6,8], value = 5", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,-10,7,13,6,8], value = 7", + "output": "2 " + } + ], + "constraints": [ + "For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] to make nums = [-1,2,3].", + "For example, the MEX of [-1,2,3] is 0 while the MEX of [1,0,3] is 2.", + "1 <= nums.length, value <= 105", + "-109 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def findSmallestInteger(self, nums, value):\n \"\"\"\n :type nums: List[int]\n :type value: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findSmallestInteger(int[] nums, int value) {\n \n }\n}", + "metadata": { + "func_name": "findSmallestInteger" + } +} \ No newline at end of file diff --git a/smallest-subarrays-with-maximum-bitwise-or.json b/smallest-subarrays-with-maximum-bitwise-or.json new file mode 100644 index 0000000000000000000000000000000000000000..137cdb3e57586220e4b46df9a47e528432928f19 --- /dev/null +++ b/smallest-subarrays-with-maximum-bitwise-or.json @@ -0,0 +1,31 @@ +{ + "id": 2498, + "name": "smallest-subarrays-with-maximum-bitwise-or", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/", + "date": "2022-09-03", + "task_description": "You are given a **0-indexed** array `nums` of length `n`, consisting of non-negative integers. For each index `i` from `0` to `n - 1`, you must determine the size of the **minimum sized** non-empty subarray of `nums` starting at `i` (**inclusive**) that has the **maximum** possible **bitwise OR**. In other words, let `Bij` be the bitwise OR of the subarray `nums[i...j]`. You need to find the smallest subarray starting at `i`, such that bitwise OR of this subarray is equal to `max(Bik)` where `i <= k <= n - 1`. The bitwise OR of an array is the bitwise OR of all the numbers in it. Return _an integer array _`answer`_ of size _`n`_ where _`answer[i]`_ is the length of the **minimum** sized subarray starting at _`i`_ with **maximum** bitwise OR._ A **subarray** is a contiguous non-empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,0,2,1,3] **Output:** [3,3,2,2,1] **Explanation:** The maximum possible bitwise OR starting at any index is 3. - Starting at index 0, the shortest subarray that yields it is [1,0,2]. - Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1]. - Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1]. - Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3]. - Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3]. Therefore, we return [3,3,2,2,1]. ``` **Example 2:** ``` **Input:** nums = [1,2] **Output:** [2,1] **Explanation: **Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2. Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1. Therefore, we return [2,1]. ``` **Constraints:** `n == nums.length` `1 <= n <= 105` `0 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,0,2,1,3]", + "output": "[3,3,2,2,1] " + }, + { + "label": "Example 2", + "input": "nums = [1,2]", + "output": "[2,1] " + } + ], + "constraints": [ + "In other words, let Bij be the bitwise OR of the subarray nums[i...j]. You need to find the smallest subarray starting at i, such that bitwise OR of this subarray is equal to max(Bik) where i <= k <= n - 1.", + "n == nums.length", + "1 <= n <= 105", + "0 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def smallestSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] smallestSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "smallestSubarrays" + } +} \ No newline at end of file diff --git a/smallest-substring-with-identical-characters-i.json b/smallest-substring-with-identical-characters-i.json new file mode 100644 index 0000000000000000000000000000000000000000..b4d3836c1593afb64b80ca51b360e5e8198fcb18 --- /dev/null +++ b/smallest-substring-with-identical-characters-i.json @@ -0,0 +1,36 @@ +{ + "id": 3690, + "name": "smallest-substring-with-identical-characters-i", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/smallest-substring-with-identical-characters-i/", + "date": "2024-12-15", + "task_description": "You are given a binary string `s` of length `n` and an integer `numOps`. You are allowed to perform the following operation on `s` **at most** `numOps` times: Select any index `i` (where `0 <= i < n`) and **flip** `s[i]`. If `s[i] == '1'`, change `s[i]` to `'0'` and vice versa. You need to **minimize** the length of the **longest** substring of `s` such that all the characters in the substring are **identical**. Return the **minimum** length after the operations. **Example 1:** **Input:** s = \"000001\", numOps = 1 **Output:** 2 **Explanation:** By changing `s[2]` to `'1'`, `s` becomes `\"001001\"`. The longest substrings with identical characters are `s[0..1]` and `s[3..4]`. **Example 2:** **Input:** s = \"0000\", numOps = 2 **Output:** 1 **Explanation:** By changing `s[0]` and `s[2]` to `'1'`, `s` becomes `\"1010\"`. **Example 3:** **Input:** s = \"0101\", numOps = 0 **Output:** 1 **Constraints:** `1 <= n == s.length <= 1000` `s` consists only of `'0'` and `'1'`. `0 <= numOps <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"000001\", numOps = 1", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"0000\", numOps = 2", + "output": "1 " + }, + { + "label": "Example 3", + "input": "s = \"0101\", numOps = 0", + "output": "" + } + ], + "constraints": [ + "Select any index i (where 0 <= i < n) and flip s[i]. If s[i] == '1', change s[i] to '0' and vice versa.", + "1 <= n == s.length <= 1000", + "s consists only of '0' and '1'.", + "0 <= numOps <= n" + ], + "python_template": "class Solution(object):\n def minLength(self, s, numOps):\n \"\"\"\n :type s: str\n :type numOps: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minLength(String s, int numOps) {\n \n }\n}", + "metadata": { + "func_name": "minLength" + } +} \ No newline at end of file diff --git a/smallest-substring-with-identical-characters-ii.json b/smallest-substring-with-identical-characters-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..6e225a873e04e00bc5d185616286421998e34035 --- /dev/null +++ b/smallest-substring-with-identical-characters-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3706, + "name": "smallest-substring-with-identical-characters-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/smallest-substring-with-identical-characters-ii/", + "date": "2024-12-15", + "task_description": "You are given a binary string `s` of length `n` and an integer `numOps`. You are allowed to perform the following operation on `s` **at most** `numOps` times: Select any index `i` (where `0 <= i < n`) and **flip** `s[i]`. If `s[i] == '1'`, change `s[i]` to `'0'` and vice versa. You need to **minimize** the length of the **longest** substring of `s` such that all the characters in the substring are **identical**. Return the **minimum** length after the operations. **Example 1:** **Input:** s = \"000001\", numOps = 1 **Output:** 2 **Explanation:** By changing `s[2]` to `'1'`, `s` becomes `\"001001\"`. The longest substrings with identical characters are `s[0..1]` and `s[3..4]`. **Example 2:** **Input:** s = \"0000\", numOps = 2 **Output:** 1 **Explanation:** By changing `s[0]` and `s[2]` to `'1'`, `s` becomes `\"1010\"`. **Example 3:** **Input:** s = \"0101\", numOps = 0 **Output:** 1 **Constraints:** `1 <= n == s.length <= 105` `s` consists only of `'0'` and `'1'`. `0 <= numOps <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"000001\", numOps = 1", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"0000\", numOps = 2", + "output": "1 " + }, + { + "label": "Example 3", + "input": "s = \"0101\", numOps = 0", + "output": "" + } + ], + "constraints": [ + "Select any index i (where 0 <= i < n) and flip s[i]. If s[i] == '1', change s[i] to '0' and vice versa.", + "1 <= n == s.length <= 105", + "s consists only of '0' and '1'.", + "0 <= numOps <= n" + ], + "python_template": "class Solution(object):\n def minLength(self, s, numOps):\n \"\"\"\n :type s: str\n :type numOps: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minLength(String s, int numOps) {\n \n }\n}", + "metadata": { + "func_name": "minLength" + } +} \ No newline at end of file diff --git a/solving-questions-with-brainpower.json b/solving-questions-with-brainpower.json new file mode 100644 index 0000000000000000000000000000000000000000..4a1ad8ee05e1822b71bf8fa748829cf9026b91bf --- /dev/null +++ b/solving-questions-with-brainpower.json @@ -0,0 +1,35 @@ +{ + "id": 2262, + "name": "solving-questions-with-brainpower", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/solving-questions-with-brainpower/", + "date": "2022-01-09", + "task_description": "You are given a **0-indexed** 2D integer array `questions` where `questions[i] = [pointsi, brainpoweri]`. The array describes the questions of an exam, where you have to process the questions **in order** (i.e., starting from question `0`) and make a decision whether to **solve** or **skip** each question. Solving question `i` will **earn** you `pointsi` points but you will be **unable** to solve each of the next `brainpoweri` questions. If you skip question `i`, you get to make the decision on the next question. For example, given `questions = [[3, 2], [4, 3], [4, 4], [2, 5]]`: If question `0` is solved, you will earn `3` points but you will be unable to solve questions `1` and `2`. If instead, question `0` is skipped and question `1` is solved, you will earn `4` points but you will be unable to solve questions `2` and `3`. Return _the **maximum** points you can earn for the exam_. **Example 1:** ``` **Input:** questions = [[3,2],[4,3],[4,4],[2,5]] **Output:** 5 **Explanation:** The maximum points can be earned by solving questions 0 and 3. - Solve question 0: Earn 3 points, will be unable to solve the next 2 questions - Unable to solve questions 1 and 2 - Solve question 3: Earn 2 points Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points. ``` **Example 2:** ``` **Input:** questions = [[1,1],[2,2],[3,3],[4,4],[5,5]] **Output:** 7 **Explanation:** The maximum points can be earned by solving questions 1 and 4. - Skip question 0 - Solve question 1: Earn 2 points, will be unable to solve the next 2 questions - Unable to solve questions 2 and 3 - Solve question 4: Earn 5 points Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points. ``` **Constraints:** `1 <= questions.length <= 105` `questions[i].length == 2` `1 <= pointsi, brainpoweri <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "questions = [[3,2],[4,3],[4,4],[2,5]]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]", + "output": "7 " + } + ], + "constraints": [ + "For example, given questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:\n\n\t\nIf question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.\nIf instead, question 0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.", + "If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.", + "If instead, question 0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.", + "If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.", + "If instead, question 0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.", + "1 <= questions.length <= 105", + "questions[i].length == 2", + "1 <= pointsi, brainpoweri <= 105" + ], + "python_template": "class Solution(object):\n def mostPoints(self, questions):\n \"\"\"\n :type questions: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long mostPoints(int[][] questions) {\n \n }\n}", + "metadata": { + "func_name": "mostPoints" + } +} \ No newline at end of file diff --git a/sort-even-and-odd-indices-independently.json b/sort-even-and-odd-indices-independently.json new file mode 100644 index 0000000000000000000000000000000000000000..60024e288a2820682e84ac9058ac56e6a9402031 --- /dev/null +++ b/sort-even-and-odd-indices-independently.json @@ -0,0 +1,31 @@ +{ + "id": 2283, + "name": "sort-even-and-odd-indices-independently", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/sort-even-and-odd-indices-independently/", + "date": "2022-01-30", + "task_description": "You are given a **0-indexed** integer array `nums`. Rearrange the values of `nums` according to the following rules: Sort the values at **odd indices** of `nums` in **non-increasing** order. For example, if `nums = [4,**1**,2,**3**]` before this step, it becomes `[4,**3**,2,**1**]` after. The values at odd indices `1` and `3` are sorted in non-increasing order. Sort the values at **even indices** of `nums` in **non-decreasing** order. For example, if `nums = [**4**,1,**2**,3]` before this step, it becomes `[**2**,1,**4**,3]` after. The values at even indices `0` and `2` are sorted in non-decreasing order. Return _the array formed after rearranging the values of_ `nums`. **Example 1:** ``` **Input:** nums = [4,1,2,3] **Output:** [2,3,4,1] **Explanation:** First, we sort the values present at odd indices (1 and 3) in non-increasing order. So, nums changes from [4,**1**,2,**3**] to [4,**3**,2,**1**]. Next, we sort the values present at even indices (0 and 2) in non-decreasing order. So, nums changes from [**4**,1,**2**,3] to [**2**,3,**4**,1]. Thus, the array formed after rearranging the values is [2,3,4,1]. ``` **Example 2:** ``` **Input:** nums = [2,1] **Output:** [2,1] **Explanation:** Since there is exactly one odd index and one even index, no rearrangement of values takes place. The resultant array formed is [2,1], which is the same as the initial array. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,1,2,3]", + "output": "[2,3,4,1] " + }, + { + "label": "Example 2", + "input": "nums = [2,1]", + "output": "[2,1] " + } + ], + "constraints": [ + "For example, if nums = [4,1,2,3] before this step, it becomes [4,3,2,1] after. The values at odd indices 1 and 3 are sorted in non-increasing order.", + "For example, if nums = [4,1,2,3] before this step, it becomes [2,1,4,3] after. The values at even indices 0 and 2 are sorted in non-decreasing order.", + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def sortEvenOdd(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] sortEvenOdd(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "sortEvenOdd" + } +} \ No newline at end of file diff --git a/sort-matrix-by-diagonals.json b/sort-matrix-by-diagonals.json new file mode 100644 index 0000000000000000000000000000000000000000..d07f89598604d031555f0e8e92e1917ff3c5b70f --- /dev/null +++ b/sort-matrix-by-diagonals.json @@ -0,0 +1,41 @@ +{ + "id": 3748, + "name": "sort-matrix-by-diagonals", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sort-matrix-by-diagonals/", + "date": "2025-02-02", + "task_description": "You are given an `n x n` square matrix of integers `grid`. Return the matrix such that: The diagonals in the **bottom-left triangle** (including the middle diagonal) are sorted in **non-increasing order**. The diagonals in the **top-right triangle** are sorted in **non-decreasing order**. **Example 1:** **Input:** grid = [[1,7,3],[9,8,2],[4,5,6]] **Output:** [[8,2,3],[9,6,7],[4,5,1]] **Explanation:** The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order: `[1, 8, 6]` becomes `[8, 6, 1]`. `[9, 5]` and `[4]` remain unchanged. The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order: `[7, 2]` becomes `[2, 7]`. `[3]` remains unchanged. **Example 2:** **Input:** grid = [[0,1],[1,2]] **Output:** [[2,1],[1,0]] **Explanation:** The diagonals with a black arrow must be non-increasing, so `[0, 2]` is changed to `[2, 0]`. The other diagonals are already in the correct order. **Example 3:** **Input:** grid = [[1]] **Output:** [[1]] **Explanation:** Diagonals with exactly one element are already in order, so no changes are needed. **Constraints:** `grid.length == grid[i].length == n` `1 <= n <= 10` `-105 <= grid[i][j] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,7,3],[9,8,2],[4,5,6]]", + "output": "[[8,2,3],[9,6,7],[4,5,1]] " + }, + { + "label": "Example 2", + "input": "grid = [[0,1],[1,2]]", + "output": "[[2,1],[1,0]] " + }, + { + "label": "Example 3", + "input": "grid = [[1]]", + "output": "[[1]] " + } + ], + "constraints": [ + "The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order.", + "The diagonals in the top-right triangle are sorted in non-decreasing order.", + "[1, 8, 6] becomes [8, 6, 1].", + "[9, 5] and [4] remain unchanged.", + "[7, 2] becomes [2, 7].", + "[3] remains unchanged.", + "grid.length == grid[i].length == n", + "1 <= n <= 10", + "-105 <= grid[i][j] <= 105" + ], + "python_template": "class Solution(object):\n def sortMatrix(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] sortMatrix(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "sortMatrix" + } +} \ No newline at end of file diff --git a/sort-the-people.json b/sort-the-people.json new file mode 100644 index 0000000000000000000000000000000000000000..c8ff9975f922233023b623fd326f385450799bf0 --- /dev/null +++ b/sort-the-people.json @@ -0,0 +1,33 @@ +{ + "id": 2502, + "name": "sort-the-people", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/sort-the-people/", + "date": "2022-09-18", + "task_description": "You are given an array of strings `names`, and an array `heights` that consists of **distinct** positive integers. Both arrays are of length `n`. For each index `i`, `names[i]` and `heights[i]` denote the name and height of the `ith` person. Return `names`_ sorted in **descending** order by the people's heights_. **Example 1:** ``` **Input:** names = [\"Mary\",\"John\",\"Emma\"], heights = [180,165,170] **Output:** [\"Mary\",\"Emma\",\"John\"] **Explanation:** Mary is the tallest, followed by Emma and John. ``` **Example 2:** ``` **Input:** names = [\"Alice\",\"Bob\",\"Bob\"], heights = [155,185,150] **Output:** [\"Bob\",\"Alice\",\"Bob\"] **Explanation:** The first Bob is the tallest, followed by Alice and the second Bob. ``` **Constraints:** `n == names.length == heights.length` `1 <= n <= 103` `1 <= names[i].length <= 20` `1 <= heights[i] <= 105` `names[i]` consists of lower and upper case English letters. All the values of `heights` are distinct.", + "test_case": [ + { + "label": "Example 1", + "input": "names = [\"Mary\",\"John\",\"Emma\"], heights = [180,165,170]", + "output": "[\"Mary\",\"Emma\",\"John\"] " + }, + { + "label": "Example 2", + "input": "names = [\"Alice\",\"Bob\",\"Bob\"], heights = [155,185,150]", + "output": "[\"Bob\",\"Alice\",\"Bob\"] " + } + ], + "constraints": [ + "n == names.length == heights.length", + "1 <= n <= 103", + "1 <= names[i].length <= 20", + "1 <= heights[i] <= 105", + "names[i] consists of lower and upper case English letters.", + "All the values of heights are distinct." + ], + "python_template": "class Solution(object):\n def sortPeople(self, names, heights):\n \"\"\"\n :type names: List[str]\n :type heights: List[int]\n :rtype: List[str]\n \"\"\"\n ", + "java_template": "class Solution {\n public String[] sortPeople(String[] names, int[] heights) {\n \n }\n}", + "metadata": { + "func_name": "sortPeople" + } +} \ No newline at end of file diff --git a/sort-vowels-in-a-string.json b/sort-vowels-in-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..a1a345d6be01ed7e936a9b077fd58ca19f19fceb --- /dev/null +++ b/sort-vowels-in-a-string.json @@ -0,0 +1,31 @@ +{ + "id": 2887, + "name": "sort-vowels-in-a-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sort-vowels-in-a-string/", + "date": "2023-07-08", + "task_description": "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 formally, for pairs of indices `i`, `j` with `0 <= i < j < s.length` such that `s[i]` and `s[j]` are vowels, then `t[i]` must not have a higher ASCII value than `t[j]`. Return _the resulting string_. The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels. **Example 1:** ``` **Input:** s = \"lEetcOde\" **Output:** \"lEOtcede\" **Explanation:** 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. ``` **Example 2:** ``` **Input:** s = \"lYmpH\" **Output:** \"lYmpH\" **Explanation:** There are no vowels in s (all characters in s are consonants), so we return \"lYmpH\". ``` **Constraints:** `1 <= s.length <= 105` `s` consists only of letters of the English alphabet in **uppercase and lowercase**.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"lEetcOde\"", + "output": "\"lEOtcede\" " + }, + { + "label": "Example 2", + "input": "s = \"lYmpH\"", + "output": "\"lYmpH\" " + } + ], + "constraints": [ + "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 formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].", + "1 <= s.length <= 105", + "s consists only of letters of theĀ English alphabetĀ in uppercase and lowercase." + ], + "python_template": "class Solution(object):\n def sortVowels(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String sortVowels(String s) {\n \n }\n}", + "metadata": { + "func_name": "sortVowels" + } +} \ No newline at end of file diff --git a/sorted-gcd-pair-queries.json b/sorted-gcd-pair-queries.json new file mode 100644 index 0000000000000000000000000000000000000000..5a7c77be950e84f24d5b4df6f160e7fbcf0fe5be --- /dev/null +++ b/sorted-gcd-pair-queries.json @@ -0,0 +1,36 @@ +{ + "id": 3583, + "name": "sorted-gcd-pair-queries", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/sorted-gcd-pair-queries/", + "date": "2024-09-29", + "task_description": "You are given an integer array `nums` of length `n` and an integer array `queries`. Let `gcdPairs` denote an array obtained by calculating the GCD of all possible pairs `(nums[i], nums[j])`, where `0 <= i < j < n`, and then sorting these values in **ascending** order. For each query `queries[i]`, you need to find the element at index `queries[i]` in `gcdPairs`. Return an integer array `answer`, where `answer[i]` is the value at `gcdPairs[queries[i]]` for each query. The term `gcd(a, b)` denotes the **greatest common divisor** of `a` and `b`. **Example 1:** **Input:** nums = [2,3,4], queries = [0,2,2] **Output:** [1,2,2] **Explanation:** `gcdPairs = [gcd(nums[0], nums[1]), gcd(nums[0], nums[2]), gcd(nums[1], nums[2])] = [1, 2, 1]`. After sorting in ascending order, `gcdPairs = [1, 1, 2]`. So, the answer is `[gcdPairs[queries[0]], gcdPairs[queries[1]], gcdPairs[queries[2]]] = [1, 2, 2]`. **Example 2:** **Input:** nums = [4,4,2,1], queries = [5,3,1,0] **Output:** [4,2,1,1] **Explanation:** `gcdPairs` sorted in ascending order is `[1, 1, 1, 2, 2, 4]`. **Example 3:** **Input:** nums = [2,2], queries = [0,0] **Output:** [2,2] **Explanation:** `gcdPairs = [2]`. **Constraints:** `2 <= n == nums.length <= 105` `1 <= nums[i] <= 5 * 104` `1 <= queries.length <= 105` `0 <= queries[i] < n * (n - 1) / 2`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,4], queries = [0,2,2]", + "output": "[1,2,2] " + }, + { + "label": "Example 2", + "input": "nums = [4,4,2,1], queries = [5,3,1,0]", + "output": "[4,2,1,1] " + }, + { + "label": "Example 3", + "input": "nums = [2,2], queries = [0,0]", + "output": "[2,2] " + } + ], + "constraints": [ + "2 <= n == nums.length <= 105", + "1 <= nums[i] <= 5 * 104", + "1 <= queries.length <= 105", + "0 <= queries[i] < n * (n - 1) / 2" + ], + "python_template": "class Solution(object):\n def gcdValues(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] gcdValues(int[] nums, long[] queries) {\n \n }\n}", + "metadata": { + "func_name": "gcdValues" + } +} \ No newline at end of file diff --git a/sorting-three-groups.json b/sorting-three-groups.json new file mode 100644 index 0000000000000000000000000000000000000000..84d185a887441222dc82fca0cf0f84fa6f9e510b --- /dev/null +++ b/sorting-three-groups.json @@ -0,0 +1,34 @@ +{ + "id": 2904, + "name": "sorting-three-groups", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sorting-three-groups/", + "date": "2023-08-05", + "task_description": "You are given an integer array `nums`. Each element in `nums` is 1, 2 or 3. In each operation, you can remove an element from `nums`. Return the **minimum** number of operations to make `nums` **non-decreasing**. **Example 1:** **Input:** nums = [2,1,3,2,1] **Output:** 3 **Explanation:** One of the optimal solutions is to remove `nums[0]`, `nums[2]` and `nums[3]`. **Example 2:** **Input:** nums = [1,3,2,1,3,3] **Output:** 2 **Explanation:** One of the optimal solutions is to remove `nums[1]` and `nums[2]`. **Example 3:** **Input:** nums = [2,2,2,2,3,3] **Output:** 0 **Explanation:** `nums` is already non-decreasing. **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 3` **Follow-up:** Can you come up with an algorithm that runs in `O(n)` time complexity?", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,3,2,1]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,2,1,3,3]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [2,2,2,2,3,3]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "1 <= nums[i] <= 3" + ], + "python_template": "class Solution(object):\n def minimumOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumOperations(List nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperations" + } +} \ No newline at end of file diff --git a/special-array-i.json b/special-array-i.json new file mode 100644 index 0000000000000000000000000000000000000000..bbc2efba71e6318afdd187bd86af7b246a908e9e --- /dev/null +++ b/special-array-i.json @@ -0,0 +1,34 @@ +{ + "id": 3429, + "name": "special-array-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/special-array-i/", + "date": "2024-05-12", + "task_description": "An array is considered **special** if the _parity_ of every pair of adjacent elements is different. In other words, one element in each pair **must** be even, and the other **must** be odd. You are given an array of integers `nums`. Return `true` if `nums` is a **special** array, otherwise, return `false`. **Example 1:** **Input:** nums = [1] **Output:** true **Explanation:** There is only one element. So the answer is `true`. **Example 2:** **Input:** nums = [2,1,4] **Output:** true **Explanation:** There is only two pairs: `(2,1)` and `(1,4)`, and both of them contain numbers with different parity. So the answer is `true`. **Example 3:** **Input:** nums = [4,3,1,6] **Output:** false **Explanation:** `nums[1]` and `nums[2]` are both odd. So the answer is `false`. **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [2,1,4]", + "output": "true " + }, + { + "label": "Example 3", + "input": "nums = [4,3,1,6]", + "output": "false " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def isArraySpecial(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isArraySpecial(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "isArraySpecial" + } +} \ No newline at end of file diff --git a/special-permutations.json b/special-permutations.json new file mode 100644 index 0000000000000000000000000000000000000000..fdc1126ecc5124ba778367fb0848b92bb3833a66 --- /dev/null +++ b/special-permutations.json @@ -0,0 +1,30 @@ +{ + "id": 2848, + "name": "special-permutations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/special-permutations/", + "date": "2023-06-11", + "task_description": "You are given a **0-indexed** integer array `nums` containing `n` **distinct** positive integers. A permutation of `nums` is called special if: For all indexes `0 <= i < n - 1`, either `nums[i] % nums[i+1] == 0` or `nums[i+1] % nums[i] == 0`. Return _the total number of special permutations. _As the answer could be large, return it **modulo **`109 + 7`. **Example 1:** ``` **Input:** nums = [2,3,6] **Output:** 2 **Explanation:** [3,6,2] and [2,6,3] are the two special permutations of nums. ``` **Example 2:** ``` **Input:** nums = [1,4,3] **Output:** 2 **Explanation:** [3,1,4] and [4,1,3] are the two special permutations of nums. ``` **Constraints:** `2 <= nums.length <= 14` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,6]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,4,3]", + "output": "2 " + } + ], + "constraints": [ + "For all indexesĀ 0 <= i < n - 1, eitherĀ nums[i] % nums[i+1] == 0Ā orĀ nums[i+1] % nums[i] == 0.", + "2 <= nums.length <= 14", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def specialPerm(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int specialPerm(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "specialPerm" + } +} \ No newline at end of file diff --git a/split-array-into-maximum-number-of-subarrays.json b/split-array-into-maximum-number-of-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..a3eed2e7bf0e0e8c215a239ceeecc34b8c91903c --- /dev/null +++ b/split-array-into-maximum-number-of-subarrays.json @@ -0,0 +1,31 @@ +{ + "id": 3080, + "name": "split-array-into-maximum-number-of-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/split-array-into-maximum-number-of-subarrays/", + "date": "2023-09-16", + "task_description": "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: **E****ach** element of the array belongs to **exactly** one subarray. The sum of scores of the subarrays is the **minimum** possible. Return _the **maximum** number of subarrays in a split that satisfies the conditions above._ A **subarray** is a contiguous part of an array. **Example 1:** ``` **Input:** nums = [1,0,2,0,1,2] **Output:** 3 **Explanation:** We can split the array into the following subarrays: - [1,0]. The score of this subarray is 1 AND 0 = 0. - [2,0]. The score of this subarray is 2 AND 0 = 0. - [1,2]. The score of this subarray is 1 AND 2 = 0. The sum of scores is 0 + 0 + 0 = 0, which is the minimum possible score that we can obtain. It can be shown that we cannot split the array into more than 3 subarrays with a total score of 0. So we return 3. ``` **Example 2:** ``` **Input:** nums = [5,7,1,3] **Output:** 1 **Explanation:** We can split the array into one subarray: [5,7,1,3] with a score of 1, which is the minimum possible score that we can obtain. It can be shown that we cannot split the array into more than 1 subarray with a total score of 1. So we return 1. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,0,2,0,1,2]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [5,7,1,3]", + "output": "1 " + } + ], + "constraints": [ + "Each element of the array belongs to exactly one subarray.", + "The sum of scores of the subarrays is the minimum possible.", + "1 <= nums.length <= 105", + "0 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maxSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxSubarrays" + } +} \ No newline at end of file diff --git a/split-the-array-to-make-coprime-products.json b/split-the-array-to-make-coprime-products.json new file mode 100644 index 0000000000000000000000000000000000000000..d254ee1b28854ade62c537667822c224da394c0f --- /dev/null +++ b/split-the-array-to-make-coprime-products.json @@ -0,0 +1,31 @@ +{ + "id": 2647, + "name": "split-the-array-to-make-coprime-products", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/split-the-array-to-make-coprime-products/", + "date": "2023-02-26", + "task_description": "You are given a **0-indexed** integer array `nums` of length `n`. A **split** at an index `i` where `0 <= i <= n - 2` is called **valid** if the product of the first `i + 1` elements and the product of the remaining elements are coprime. For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is valid because `2` and `9` are coprime, while a split at the index `i = 1` is not valid because `6` and `3` are not coprime. A split at the index `i = 2` is not valid because `i == n - 1`. Return _the smallest index _`i`_ at which the array can be split validly or _`-1`_ if there is no such split_. Two values `val1` and `val2` are coprime if `gcd(val1, val2) == 1` where `gcd(val1, val2)` is the greatest common divisor of `val1` and `val2`. **Example 1:** ``` **Input:** nums = [4,7,8,15,3,5] **Output:** 2 **Explanation:** The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. The only valid split is at index 2. ``` **Example 2:** ``` **Input:** nums = [4,7,15,8,3,5] **Output:** -1 **Explanation:** The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. There is no valid split. ``` **Constraints:** `n == nums.length` `1 <= n <= 104` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,7,8,15,3,5]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,7,15,8,3,5]", + "output": "-1 " + } + ], + "constraints": [ + "For example, if nums = [2, 3, 3], then a split at the index i = 0 is valid because 2 and 9 are coprime, while a split at the index i = 1 is not valid because 6 and 3 are not coprime. A split at the index i = 2 is not valid because i == n - 1.", + "n == nums.length", + "1 <= n <= 104", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def findValidSplit(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findValidSplit(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findValidSplit" + } +} \ No newline at end of file diff --git a/split-the-array.json b/split-the-array.json new file mode 100644 index 0000000000000000000000000000000000000000..2021e244213a2c2f121b01ef1e8b0107a69d2cac --- /dev/null +++ b/split-the-array.json @@ -0,0 +1,33 @@ +{ + "id": 3324, + "name": "split-the-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/split-the-array/", + "date": "2024-02-18", + "task_description": "You are given an integer array `nums` of **even** length. You have to split the array into two parts `nums1` and `nums2` such that: `nums1.length == nums2.length == nums.length / 2`. `nums1` should contain **distinct **elements. `nums2` should also contain **distinct** elements. Return `true`_ if it is possible to split the array, and _`false` _otherwise__._ **Example 1:** ``` **Input:** nums = [1,1,2,2,3,4] **Output:** true **Explanation:** One of the possible ways to split nums is nums1 = [1,2,3] and nums2 = [1,2,4]. ``` **Example 2:** ``` **Input:** nums = [1,1,1,1] **Output:** false **Explanation:** The only possible way to split nums is nums1 = [1,1] and nums2 = [1,1]. Both nums1 and nums2 do not contain distinct elements. Therefore, we return false. ``` **Constraints:** `1 <= nums.length <= 100` `nums.length % 2 == 0 ` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1,2,2,3,4]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1,1]", + "output": "false " + } + ], + "constraints": [ + "nums1.length == nums2.length == nums.length / 2.", + "nums1 should contain distinct elements.", + "nums2 should also contain distinct elements.", + "1 <= nums.length <= 100", + "nums.length % 2 == 0", + "1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def isPossibleToSplit(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isPossibleToSplit(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "isPossibleToSplit" + } +} \ No newline at end of file diff --git a/string-compression-iii.json b/string-compression-iii.json new file mode 100644 index 0000000000000000000000000000000000000000..0b2eb7defa4762cd1463964cbd5134574cf5767b --- /dev/null +++ b/string-compression-iii.json @@ -0,0 +1,37 @@ +{ + "id": 3451, + "name": "string-compression-iii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/string-compression-iii/", + "date": "2024-05-19", + "task_description": "Given a string `word`, compress it using the following algorithm: Begin with an empty string `comp`. While `word` is **not** empty, use the following operation: Remove a maximum length prefix of `word` made of a _single character_ `c` repeating **at most** 9 times. Append the length of the prefix followed by `c` to `comp`. Return the string `comp`. **Example 1:** **Input:** word = \"abcde\" **Output:** \"1a1b1c1d1e\" **Explanation:** Initially, `comp = \"\"`. Apply the operation 5 times, choosing `\"a\"`, `\"b\"`, `\"c\"`, `\"d\"`, and `\"e\"` as the prefix in each operation. For each prefix, append `\"1\"` followed by the character to `comp`. **Example 2:** **Input:** word = \"aaaaaaaaaaaaaabb\" **Output:** \"9a5a2b\" **Explanation:** Initially, `comp = \"\"`. Apply the operation 3 times, choosing `\"aaaaaaaaa\"`, `\"aaaaa\"`, and `\"bb\"` as the prefix in each operation. For prefix `\"aaaaaaaaa\"`, append `\"9\"` followed by `\"a\"` to `comp`. For prefix `\"aaaaa\"`, append `\"5\"` followed by `\"a\"` to `comp`. For prefix `\"bb\"`, append `\"2\"` followed by `\"b\"` to `comp`. **Constraints:** `1 <= word.length <= 2 * 105` `word` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"abcde\"", + "output": "\"1a1b1c1d1e\" " + }, + { + "label": "Example 2", + "input": "word = \"aaaaaaaaaaaaaabb\"", + "output": "\"9a5a2b\" " + } + ], + "constraints": [ + "Begin with an empty string comp. While word is not empty, use the following operation:\n\n\t\nRemove a maximum length prefix of word made of a single character c repeating at most 9 times.\nAppend the length of the prefix followed by c to comp.", + "Remove a maximum length prefix of word made of a single character c repeating at most 9 times.", + "Append the length of the prefix followed by c to comp.", + "Remove a maximum length prefix of word made of a single character c repeating at most 9 times.", + "Append the length of the prefix followed by c to comp.", + "For prefix \"aaaaaaaaa\", append \"9\" followed by \"a\" to comp.", + "For prefix \"aaaaa\", append \"5\" followed by \"a\" to comp.", + "For prefix \"bb\", append \"2\" followed by \"b\" to comp.", + "1 <= word.length <= 2 * 105", + "word consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def compressedString(self, word):\n \"\"\"\n :type word: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String compressedString(String word) {\n \n }\n}", + "metadata": { + "func_name": "compressedString" + } +} \ No newline at end of file diff --git a/string-transformation.json b/string-transformation.json new file mode 100644 index 0000000000000000000000000000000000000000..fa5b22ba03185b774fb56528fba9c3b344043eab --- /dev/null +++ b/string-transformation.json @@ -0,0 +1,32 @@ +{ + "id": 3024, + "name": "string-transformation", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/string-transformation/", + "date": "2023-09-03", + "task_description": "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 = 'cdab'`. You are also given an integer `k`. Return _the number of ways in which _`s` _can be transformed into _`t`_ in **exactly** _`k`_ operations._ Since the answer can be large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** s = \"abcd\", t = \"cdab\", k = 2 **Output:** 2 **Explanation:** First way: In first operation, choose suffix from index = 3, so resulting s = \"dabc\". In second operation, choose suffix from index = 3, so resulting s = \"cdab\". Second way: In first operation, choose suffix from index = 1, so resulting s = \"bcda\". In second operation, choose suffix from index = 1, so resulting s = \"cdab\". ``` **Example 2:** ``` **Input:** s = \"ababab\", t = \"ababab\", k = 1 **Output:** 2 **Explanation:** First way: Choose suffix from index = 2, so resulting s = \"ababab\". Second way: Choose suffix from index = 4, so resulting s = \"ababab\". ``` **Constraints:** `2 <= s.length <= 5 * 105` `1 <= k <= 1015` `s.length == t.length` `s` and `t` consist of only lowercase English alphabets.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcd\", t = \"cdab\", k = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"ababab\", t = \"ababab\", k = 1", + "output": "2 " + } + ], + "constraints": [ + "Remove a suffix of s of length l where 0 < l < n and append it at the start of s.\n\tFor example, let s = 'abcd' then in one operation you can remove the suffix 'cd' and append it in front of s making s = 'cdab'.", + "2 <= s.length <= 5 * 105", + "1 <= k <= 1015", + "s.length == t.length", + "s and t consist of only lowercase English alphabets." + ], + "python_template": "class Solution(object):\n def numberOfWays(self, s, t, k):\n \"\"\"\n :type s: str\n :type t: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfWays(String s, String t, long k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfWays" + } +} \ No newline at end of file diff --git a/strong-password-checker-ii.json b/strong-password-checker-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..0ee9c14065b61c0123dd63eeeea7c87a96550a39 --- /dev/null +++ b/strong-password-checker-ii.json @@ -0,0 +1,40 @@ +{ + "id": 2391, + "name": "strong-password-checker-ii", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/strong-password-checker-ii/", + "date": "2022-05-28", + "task_description": "A password is said to be **strong** if it satisfies all the following criteria: It has at least `8` characters. It contains at least **one lowercase** letter. It contains at least **one uppercase** letter. It contains at least **one digit**. It contains at least **one special character**. The special characters are the characters in the following string: `\"!@#$%^&*()-+\"`. It does **not** contain `2` of the same character in adjacent positions (i.e., `\"aab\"` violates this condition, but `\"aba\"` does not). Given a string `password`, return `true`_ if it is a **strong** password_. Otherwise, return `false`. **Example 1:** ``` **Input:** password = \"IloveLe3tcode!\" **Output:** true **Explanation:** The password meets all the requirements. Therefore, we return true. ``` **Example 2:** ``` **Input:** password = \"Me+You--IsMyDream\" **Output:** false **Explanation:** The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false. ``` **Example 3:** ``` **Input:** password = \"1aB!\" **Output:** false **Explanation:** The password does not meet the length requirement. Therefore, we return false. ``` **Constraints:** `1 <= password.length <= 100` `password` consists of letters, digits, and special characters: `\"!@#$%^&*()-+\"`.", + "test_case": [ + { + "label": "Example 1", + "input": "password = \"IloveLe3tcode!\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "password = \"Me+You--IsMyDream\"", + "output": "false " + }, + { + "label": "Example 3", + "input": "password = \"1aB!\"", + "output": "false " + } + ], + "constraints": [ + "It has at least 8 characters.", + "It contains at least one lowercase letter.", + "It contains at least one uppercase letter.", + "It contains at least one digit.", + "It contains at least one special character. The special characters are the characters in the following string: \"!@#$%^&*()-+\".", + "It does not contain 2 of the same character in adjacent positions (i.e., \"aab\" violates this condition, but \"aba\" does not).", + "1 <= password.length <= 100", + "password consists of letters, digits, and special characters: \"!@#$%^&*()-+\"." + ], + "python_template": "class Solution(object):\n def strongPasswordCheckerII(self, password):\n \"\"\"\n :type password: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean strongPasswordCheckerII(String password) {\n \n }\n}", + "metadata": { + "func_name": "strongPasswordCheckerII" + } +} \ No newline at end of file diff --git a/subarray-with-elements-greater-than-varying-threshold.json b/subarray-with-elements-greater-than-varying-threshold.json new file mode 100644 index 0000000000000000000000000000000000000000..8a53724a8eaf28f31bdebbb4b824fa0b0f38a9c8 --- /dev/null +++ b/subarray-with-elements-greater-than-varying-threshold.json @@ -0,0 +1,29 @@ +{ + "id": 2419, + "name": "subarray-with-elements-greater-than-varying-threshold", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/", + "date": "2022-06-25", + "task_description": "You are given an integer array `nums` and an integer `threshold`. Find any subarray of `nums` of length `k` such that **every** element in the subarray is **greater** than `threshold / k`. Return_ the **size** of **any** such subarray_. If there is no such subarray, return `-1`. A **subarray** is a contiguous non-empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,3,4,3,1], threshold = 6 **Output:** 3 **Explanation:** The subarray [3,4,3] has a size of 3, and every element is greater than 6 / 3 = 2. Note that this is the only valid subarray. ``` **Example 2:** ``` **Input:** nums = [6,5,6,5,8], threshold = 7 **Output:** 1 **Explanation:** The subarray [8] has a size of 1, and 8 > 7 / 1 = 7. So 1 is returned. Note that the subarray [6,5] has a size of 2, and every element is greater than 7 / 2 = 3.5. Similarly, the subarrays [6,5,6], [6,5,6,5], [6,5,6,5,8] also satisfy the given conditions. Therefore, 2, 3, 4, or 5 may also be returned. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i], threshold <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,4,3,1], threshold = 6", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [6,5,6,5,8], threshold = 7", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i], threshold <= 109" + ], + "python_template": "class Solution(object):\n def validSubarraySize(self, nums, threshold):\n \"\"\"\n :type nums: List[int]\n :type threshold: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int validSubarraySize(int[] nums, int threshold) {\n \n }\n}", + "metadata": { + "func_name": "validSubarraySize" + } +} \ No newline at end of file diff --git a/subarrays-distinct-element-sum-of-squares-i.json b/subarrays-distinct-element-sum-of-squares-i.json new file mode 100644 index 0000000000000000000000000000000000000000..9d7e87c305f868d7f42c78b0ea9bad5443100d74 --- /dev/null +++ b/subarrays-distinct-element-sum-of-squares-i.json @@ -0,0 +1,30 @@ +{ + "id": 3163, + "name": "subarrays-distinct-element-sum-of-squares-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i/", + "date": "2023-10-14", + "task_description": "You are given a **0-indexed **integer array `nums`. The **distinct count** of a subarray of `nums` is defined as: Let `nums[i..j]` be a subarray of `nums` consisting of all the indices from `i` to `j` such that `0 <= i <= j < nums.length`. Then the number of distinct values in `nums[i..j]` is called the distinct count of `nums[i..j]`. Return _the sum of the **squares** of **distinct counts** of all subarrays of _`nums`. A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,2,1] **Output:** 15 **Explanation:** Six possible subarrays are: [1]: 1 distinct value [2]: 1 distinct value [1]: 1 distinct value [1,2]: 2 distinct values [2,1]: 2 distinct values [1,2,1]: 2 distinct values The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15. ``` **Example 2:** ``` **Input:** nums = [1,1] **Output:** 3 **Explanation:** Three possible subarrays are: [1]: 1 distinct value [1]: 1 distinct value [1,1]: 1 distinct value The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1]", + "output": "15 " + }, + { + "label": "Example 2", + "input": "nums = [1,1]", + "output": "3 " + } + ], + "constraints": [ + "Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].", + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def sumCounts(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumCounts(List nums) {\n \n }\n}", + "metadata": { + "func_name": "sumCounts" + } +} \ No newline at end of file diff --git a/subarrays-distinct-element-sum-of-squares-ii.json b/subarrays-distinct-element-sum-of-squares-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..98e3014b2e29ef95091ff4d36715b811bd85ab51 --- /dev/null +++ b/subarrays-distinct-element-sum-of-squares-ii.json @@ -0,0 +1,30 @@ +{ + "id": 3139, + "name": "subarrays-distinct-element-sum-of-squares-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-ii/", + "date": "2023-10-14", + "task_description": "You are given a **0-indexed **integer array `nums`. The **distinct count** of a subarray of `nums` is defined as: Let `nums[i..j]` be a subarray of `nums` consisting of all the indices from `i` to `j` such that `0 <= i <= j < nums.length`. Then the number of distinct values in `nums[i..j]` is called the distinct count of `nums[i..j]`. Return _the sum of the **squares** of **distinct counts** of all subarrays of _`nums`. Since the answer may be very large, return it **modulo** `109 + 7`. A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,2,1] **Output:** 15 **Explanation:** Six possible subarrays are: [1]: 1 distinct value [2]: 1 distinct value [1]: 1 distinct value [1,2]: 2 distinct values [2,1]: 2 distinct values [1,2,1]: 2 distinct values The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15. ``` **Example 2:** ``` **Input:** nums = [2,2] **Output:** 3 **Explanation:** Three possible subarrays are: [2]: 1 distinct value [2]: 1 distinct value [2,2]: 1 distinct value The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1]", + "output": "15 " + }, + { + "label": "Example 2", + "input": "nums = [2,2]", + "output": "3 " + } + ], + "constraints": [ + "Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def sumCounts(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumCounts(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "sumCounts" + } +} \ No newline at end of file diff --git a/subsequence-with-the-minimum-score.json b/subsequence-with-the-minimum-score.json new file mode 100644 index 0000000000000000000000000000000000000000..96e6717307c28634897b99bfcad254485c71df66 --- /dev/null +++ b/subsequence-with-the-minimum-score.json @@ -0,0 +1,31 @@ +{ + "id": 2701, + "name": "subsequence-with-the-minimum-score", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/subsequence-with-the-minimum-score/", + "date": "2023-02-05", + "task_description": "You are given two strings `s` and `t`. You are allowed to remove any number of characters from the string `t`. The score of the string is `0` if no characters are removed from the string `t`, otherwise: Let `left` be the minimum index among all removed characters. Let `right` be the maximum index among all removed characters. Then the score of the string is `right - left + 1`. Return _the minimum possible score to make _`t`_ a subsequence of _`s`_._ A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `\"ace\"` is a subsequence of `\"abcde\"` while `\"aec\"` is not). **Example 1:** ``` **Input:** s = \"abacaba\", t = \"bzaa\" **Output:** 1 **Explanation:** In this example, we remove the character \"z\" at index 1 (0-indexed). The string t becomes \"baa\" which is a subsequence of the string \"abacaba\" and the score is 1 - 1 + 1 = 1. It can be proven that 1 is the minimum score that we can achieve. ``` **Example 2:** ``` **Input:** s = \"cde\", t = \"xyz\" **Output:** 3 **Explanation:** In this example, we remove characters \"x\", \"y\" and \"z\" at indices 0, 1, and 2 (0-indexed). The string t becomes \"\" which is a subsequence of the string \"cde\" and the score is 2 - 0 + 1 = 3. It can be proven that 3 is the minimum score that we can achieve. ``` **Constraints:** `1 <= s.length, t.length <= 105` `s` and `t` consist of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abacaba\", t = \"bzaa\"", + "output": "1 " + }, + { + "label": "Example 2", + "input": "s = \"cde\", t = \"xyz\"", + "output": "3 " + } + ], + "constraints": [ + "Let left be the minimum index among all removed characters.", + "Let right be the maximum index among all removed characters.", + "1 <= s.length, t.length <= 105", + "s and t consist of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumScore(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumScore(String s, String t) {\n \n }\n}", + "metadata": { + "func_name": "minimumScore" + } +} \ No newline at end of file diff --git a/subsequences-with-a-unique-middle-mode-i.json b/subsequences-with-a-unique-middle-mode-i.json new file mode 100644 index 0000000000000000000000000000000000000000..21443e715085fb368669fbd8d4e65dbfb4dc795d --- /dev/null +++ b/subsequences-with-a-unique-middle-mode-i.json @@ -0,0 +1,34 @@ +{ + "id": 3700, + "name": "subsequences-with-a-unique-middle-mode-i", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/subsequences-with-a-unique-middle-mode-i/", + "date": "2024-12-07", + "task_description": "Given an integer array `nums`, find the number of subsequences of size 5 of `nums` with a **unique middle mode**. Since the answer may be very large, return it **modulo** `109 + 7`. A **mode** of a sequence of numbers is defined as the element that appears the **maximum** number of times in the sequence. A sequence of numbers contains a** unique mode** if it has only one mode. A sequence of numbers `seq` of size 5 contains a **unique middle mode** if the _middle element_ (`seq[2]`) is a **unique mode**. **Example 1:** **Input:** nums = [1,1,1,1,1,1] **Output:** 6 **Explanation:** `[1, 1, 1, 1, 1]` is the only subsequence of size 5 that can be formed, and it has a unique middle mode of 1. This subsequence can be formed in 6 different ways, so the output is 6. **Example 2:** **Input:** nums = [1,2,2,3,3,4] **Output:** 4 **Explanation:** `[1, 2, 2, 3, 4]` and `[1, 2, 3, 3, 4]` each have a unique middle mode because the number at index 2 has the greatest frequency in the subsequence. `[1, 2, 2, 3, 3]` does not have a unique middle mode because 2 and 3 appear twice. **Example 3:** **Input:** nums = [0,1,2,3,4,5,6,7,8] **Output:** 0 **Explanation:** There is no subsequence of length 5 with a unique middle mode. **Constraints:** `5 <= nums.length <= 1000` `-109 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1,1,1,1,1]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,2,3,3,4]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [0,1,2,3,4,5,6,7,8]", + "output": "0 " + } + ], + "constraints": [ + "5 <= nums.length <= 1000", + "-109 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def subsequencesWithMiddleMode(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int subsequencesWithMiddleMode(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "subsequencesWithMiddleMode" + } +} \ No newline at end of file diff --git a/substring-matching-pattern.json b/substring-matching-pattern.json new file mode 100644 index 0000000000000000000000000000000000000000..608c64baf08425af3803cfed075a68ab124cc5f0 --- /dev/null +++ b/substring-matching-pattern.json @@ -0,0 +1,36 @@ +{ + "id": 3684, + "name": "substring-matching-pattern", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/substring-matching-pattern/", + "date": "2024-12-21", + "task_description": "You are given a string `s` and a pattern string `p`, where `p` contains **exactly one** `'*'` character. The `'*'` in `p` can be replaced with any sequence of zero or more characters. Return `true` if `p` can be made a substring of `s`, and `false` otherwise. **Example 1:** **Input:** s = \"leetcode\", p = \"ee*e\" **Output:** true **Explanation:** By replacing the `'*'` with `\"tcod\"`, the substring `\"eetcode\"` matches the pattern. **Example 2:** **Input:** s = \"car\", p = \"c*v\" **Output:** false **Explanation:** There is no substring matching the pattern. **Example 3:** **Input:** s = \"luck\", p = \"u*\" **Output:** true **Explanation:** The substrings `\"u\"`, `\"uc\"`, and `\"uck\"` match the pattern. **Constraints:** `1 <= s.length <= 50` `1 <= p.length <= 50 ` `s` contains only lowercase English letters. `p` contains only lowercase English letters and exactly one `'*'`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"leetcode\", p = \"ee*e\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"car\", p = \"c*v\"", + "output": "false " + }, + { + "label": "Example 3", + "input": "s = \"luck\", p = \"u*\"", + "output": "true " + } + ], + "constraints": [ + "1 <= s.length <= 50", + "1 <= p.length <= 50", + "s contains only lowercase English letters.", + "p contains only lowercase English letters and exactly one '*'" + ], + "python_template": "class Solution(object):\n def hasMatch(self, s, p):\n \"\"\"\n :type s: str\n :type p: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean hasMatch(String s, String p) {\n \n }\n}", + "metadata": { + "func_name": "hasMatch" + } +} \ No newline at end of file diff --git a/substring-with-largest-variance.json b/substring-with-largest-variance.json new file mode 100644 index 0000000000000000000000000000000000000000..bd05f3a22b2904cdede72a5dd3ba938014a0f0eb --- /dev/null +++ b/substring-with-largest-variance.json @@ -0,0 +1,29 @@ +{ + "id": 2360, + "name": "substring-with-largest-variance", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/substring-with-largest-variance/", + "date": "2022-04-30", + "task_description": "The **variance** of a string is defined as the largest difference between the number of occurrences of **any** `2` characters present in the string. Note the two characters may or may not be the same. Given a string `s` consisting of lowercase English letters only, return _the **largest variance** possible among all **substrings** of_ `s`. A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` **Input:** s = \"aababbb\" **Output:** 3 **Explanation:** All possible variances along with their respective substrings are listed below: - Variance 0 for substrings \"a\", \"aa\", \"ab\", \"abab\", \"aababb\", \"ba\", \"b\", \"bb\", and \"bbb\". - Variance 1 for substrings \"aab\", \"aba\", \"abb\", \"aabab\", \"ababb\", \"aababbb\", and \"bab\". - Variance 2 for substrings \"aaba\", \"ababbb\", \"abbb\", and \"babb\". - Variance 3 for substring \"babbb\". Since the largest possible variance is 3, we return it. ``` **Example 2:** ``` **Input:** s = \"abcde\" **Output:** 0 **Explanation:** No letter occurs more than once in s, so the variance of every substring is 0. ``` **Constraints:** `1 <= s.length <= 104` `s` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aababbb\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \"abcde\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= s.length <= 104", + "s consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def largestVariance(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int largestVariance(String s) {\n \n }\n}", + "metadata": { + "func_name": "largestVariance" + } +} \ No newline at end of file diff --git a/substring-xor-queries.json b/substring-xor-queries.json new file mode 100644 index 0000000000000000000000000000000000000000..95d235b519ae3bf8cad3ce6ef20995499f80fe3e --- /dev/null +++ b/substring-xor-queries.json @@ -0,0 +1,36 @@ +{ + "id": 2700, + "name": "substring-xor-queries", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/substring-xor-queries/", + "date": "2023-02-05", + "task_description": "You are given a **binary string** `s`, and a **2D** integer array `queries` where `queries[i] = [firsti, secondi]`. For the `ith` query, find the **shortest substring** of `s` whose **decimal value**, `val`, yields `secondi` when **bitwise XORed** with `firsti`. In other words, `val ^ firsti == secondi`. The answer to the `ith` query is the endpoints (**0-indexed**) of the substring `[lefti, righti]` or `[-1, -1]` if no such substring exists. If there are multiple answers, choose the one with the **minimum** `lefti`. _Return an array_ `ans` _where_ `ans[i] = [lefti, righti]` _is the answer to the_ `ith` _query._ A **substring** is a contiguous non-empty sequence of characters within a string. **Example 1:** ``` **Input:** s = \"101101\", queries = [[0,5],[1,2]] **Output:** [[0,2],[2,3]] **Explanation:** For the first query the substring in range `[0,2]` is **\"101\"** which has a decimal value of **`5`**, and **`5 ^ 0 = 5`**, hence the answer to the first query is `[0,2]`. In the second query, the substring in range `[2,3]` is **\"11\",** and has a decimal value of **3**, and **3` ^ 1 = 2`**. So, `[2,3]` is returned for the second query. ``` **Example 2:** ``` **Input:** s = \"0101\", queries = [[12,8]] **Output:** [[-1,-1]] **Explanation:** In this example there is no substring that answers the query, hence `[-1,-1] is returned`. ``` **Example 3:** ``` **Input:** s = \"1\", queries = [[4,5]] **Output:** [[0,0]] **Explanation:** For this example, the substring in range `[0,0]` has a decimal value of **`1`**, and **`1 ^ 4 = 5`**. So, the answer is `[0,0]`. ``` **Constraints:** `1 <= s.length <= 104` `s[i]` is either `'0'` or `'1'`. `1 <= queries.length <= 105` `0 <= firsti, secondi <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"101101\", queries = [[0,5],[1,2]]", + "output": "[[0,2],[2,3]] " + }, + { + "label": "Example 2", + "input": "s = \"0101\", queries = [[12,8]]", + "output": "[[-1,-1]] " + }, + { + "label": "Example 3", + "input": "s = \"1\", queries = [[4,5]]", + "output": "[[0,0]] " + } + ], + "constraints": [ + "1 <= s.length <= 104", + "s[i] is either '0' or '1'.", + "1 <= queries.length <= 105", + "0 <= firsti, secondi <= 109" + ], + "python_template": "class Solution(object):\n def substringXorQueries(self, s, queries):\n \"\"\"\n :type s: str\n :type queries: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] substringXorQueries(String s, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "substringXorQueries" + } +} \ No newline at end of file diff --git a/successful-pairs-of-spells-and-potions.json b/successful-pairs-of-spells-and-potions.json new file mode 100644 index 0000000000000000000000000000000000000000..8a4aa5b364c8362e29c5f3b4a15d526b3efda21f --- /dev/null +++ b/successful-pairs-of-spells-and-potions.json @@ -0,0 +1,32 @@ +{ + "id": 2392, + "name": "successful-pairs-of-spells-and-potions", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/successful-pairs-of-spells-and-potions/", + "date": "2022-05-28", + "task_description": "You are given two positive integer arrays `spells` and `potions`, of length `n` and `m` respectively, where `spells[i]` represents the strength of the `ith` spell and `potions[j]` represents the strength of the `jth` potion. You are also given an integer `success`. A spell and potion pair is considered **successful** if the **product** of their strengths is **at least** `success`. Return _an integer array _`pairs`_ of length _`n`_ where _`pairs[i]`_ is the number of **potions** that will form a successful pair with the _`ith`_ spell._ **Example 1:** ``` **Input:** spells = [5,1,3], potions = [1,2,3,4,5], success = 7 **Output:** [4,0,3] **Explanation:** - 0th spell: 5 * [1,2,3,4,5] = [5,**10**,**15**,**20**,**25**]. 4 pairs are successful. - 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful. - 2nd spell: 3 * [1,2,3,4,5] = [3,6,**9**,**12**,**15**]. 3 pairs are successful. Thus, [4,0,3] is returned. ``` **Example 2:** ``` **Input:** spells = [3,1,2], potions = [8,5,8], success = 16 **Output:** [2,0,2] **Explanation:** - 0th spell: 3 * [8,5,8] = [**24**,15,**24**]. 2 pairs are successful. - 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. - 2nd spell: 2 * [8,5,8] = [**16**,10,**16**]. 2 pairs are successful. Thus, [2,0,2] is returned. ``` **Constraints:** `n == spells.length` `m == potions.length` `1 <= n, m <= 105` `1 <= spells[i], potions[i] <= 105` `1 <= success <= 1010`", + "test_case": [ + { + "label": "Example 1", + "input": "spells = [5,1,3], potions = [1,2,3,4,5], success = 7", + "output": "[4,0,3] " + }, + { + "label": "Example 2", + "input": "spells = [3,1,2], potions = [8,5,8], success = 16", + "output": "[2,0,2] " + } + ], + "constraints": [ + "n == spells.length", + "m == potions.length", + "1 <= n, m <= 105", + "1 <= spells[i], potions[i] <= 105", + "1 <= success <= 1010" + ], + "python_template": "class Solution(object):\n def successfulPairs(self, spells, potions, success):\n \"\"\"\n :type spells: List[int]\n :type potions: List[int]\n :type success: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] successfulPairs(int[] spells, int[] potions, long success) {\n \n }\n}", + "metadata": { + "func_name": "successfulPairs" + } +} \ No newline at end of file diff --git a/sum-in-a-matrix.json b/sum-in-a-matrix.json new file mode 100644 index 0000000000000000000000000000000000000000..d75a81af92afff6ded8cc3b93f2433eb90bdcdf2 --- /dev/null +++ b/sum-in-a-matrix.json @@ -0,0 +1,30 @@ +{ + "id": 2728, + "name": "sum-in-a-matrix", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sum-in-a-matrix/", + "date": "2023-04-29", + "task_description": "You are given a **0-indexed** 2D integer array `nums`. Initially, your score is `0`. Perform the following operations until the matrix becomes empty: From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen. Identify the highest number amongst all those removed in step 1. Add that number to your **score**. Return _the final **score**._ **Example 1:** ``` **Input:** nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]] **Output:** 15 **Explanation:** In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15. ``` **Example 2:** ``` **Input:** nums = [[1]] **Output:** 1 **Explanation:** We remove 1 and add it to the answer. We return 1. ``` **Constraints:** `1 <= nums.length <= 300` `1 <= nums[i].length <= 500` `0 <= nums[i][j] <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]", + "output": "15 " + }, + { + "label": "Example 2", + "input": "nums = [[1]]", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 300", + "1 <= nums[i].length <= 500", + "0 <= nums[i][j] <= 103" + ], + "python_template": "class Solution(object):\n def matrixSum(self, nums):\n \"\"\"\n :type nums: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int matrixSum(int[][] nums) {\n \n }\n}", + "metadata": { + "func_name": "matrixSum" + } +} \ No newline at end of file diff --git a/sum-of-digit-differences-of-all-pairs.json b/sum-of-digit-differences-of-all-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..2790969b6108a1e4257dbf58162e00ae82f9799e --- /dev/null +++ b/sum-of-digit-differences-of-all-pairs.json @@ -0,0 +1,30 @@ +{ + "id": 3416, + "name": "sum-of-digit-differences-of-all-pairs", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sum-of-digit-differences-of-all-pairs/", + "date": "2024-05-12", + "task_description": "You are given an array `nums` consisting of **positive** integers where all integers have the **same** number of digits. The **digit difference** between two integers is the _count_ of different digits that are in the **same** position in the two integers. Return the **sum** of the **digit differences** between **all** pairs of integers in `nums`. **Example 1:** **Input:** nums = [13,23,12] **Output:** 4 **Explanation:** We have the following: - The digit difference between **1**3 and **2**3 is 1. - The digit difference between 1**3** and 1**2** is 1. - The digit difference between **23** and **12** is 2. So the total sum of digit differences between all pairs of integers is `1 + 1 + 2 = 4`. **Example 2:** **Input:** nums = [10,10,10,10] **Output:** 0 **Explanation:** All the integers in the array are the same. So the total sum of digit differences between all pairs of integers will be 0. **Constraints:** `2 <= nums.length <= 105` `1 <= nums[i] < 109` All integers in `nums` have the same number of digits.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [13,23,12]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [10,10,10,10]", + "output": "0 " + } + ], + "constraints": [ + "2 <= nums.length <= 105", + "1 <= nums[i] < 109", + "All integers in nums have the same number of digits." + ], + "python_template": "class Solution(object):\n def sumDigitDifferences(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long sumDigitDifferences(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "sumDigitDifferences" + } +} \ No newline at end of file diff --git a/sum-of-distances.json b/sum-of-distances.json new file mode 100644 index 0000000000000000000000000000000000000000..f887d51c48842b6f7d60fb8edf4fe0966b23048a --- /dev/null +++ b/sum-of-distances.json @@ -0,0 +1,29 @@ +{ + "id": 2721, + "name": "sum-of-distances", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sum-of-distances/", + "date": "2023-04-02", + "task_description": "You are given a **0-indexed** integer array `nums`. There exists an array `arr` of length `nums.length`, where `arr[i]` is the sum of `|i - j|` over all `j` such that `nums[j] == nums[i]` and `j != i`. If there is no such `j`, set `arr[i]` to be `0`. Return _the array _`arr`_._ **Example 1:** ``` **Input:** nums = [1,3,1,1,2] **Output:** [5,0,3,4,0] **Explanation:** When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5. When i = 1, arr[1] = 0 because there is no other index with value 3. When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3. When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4. When i = 4, arr[4] = 0 because there is no other index with value 2. ``` **Example 2:** ``` **Input:** nums = [0,5,3] **Output:** [0,0,0] **Explanation:** Since each element in nums is distinct, arr[i] = 0 for all i. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 109` **Note:** This question is the same as 2121: Intervals Between Identical Elements.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,1,1,2]", + "output": "[5,0,3,4,0] " + }, + { + "label": "Example 2", + "input": "nums = [0,5,3]", + "output": "[0,0,0] " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def distance(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] distance(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "distance" + } +} \ No newline at end of file diff --git a/sum-of-good-numbers.json b/sum-of-good-numbers.json new file mode 100644 index 0000000000000000000000000000000000000000..43b8dc498d3a49afaded784b65da9cadcc31b209 --- /dev/null +++ b/sum-of-good-numbers.json @@ -0,0 +1,30 @@ +{ + "id": 3723, + "name": "sum-of-good-numbers", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/sum-of-good-numbers/", + "date": "2025-02-01", + "task_description": "Given an array of integers `nums` and an integer `k`, an element `nums[i]` is considered **good** if it is **strictly** greater than the elements at indices `i - k` and `i + k` (if those indices exist). If neither of these indices _exists_, `nums[i]` is still considered **good**. Return the **sum** of all the **good** elements in the array. **Example 1:** **Input:** nums = [1,3,2,1,5,4], k = 2 **Output:** 12 **Explanation:** The good numbers are `nums[1] = 3`, `nums[4] = 5`, and `nums[5] = 4` because they are strictly greater than the numbers at indices `i - k` and `i + k`. **Example 2:** **Input:** nums = [2,1], k = 1 **Output:** 2 **Explanation:** The only good number is `nums[0] = 2` because it is strictly greater than `nums[1]`. **Constraints:** `2 <= nums.length <= 100` `1 <= nums[i] <= 1000` `1 <= k <= floor(nums.length / 2)`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,2,1,5,4], k = 2", + "output": "12 " + }, + { + "label": "Example 2", + "input": "nums = [2,1], k = 1", + "output": "2 " + } + ], + "constraints": [ + "2 <= nums.length <= 100", + "1 <= nums[i] <= 1000", + "1 <= k <= floor(nums.length / 2)" + ], + "python_template": "class Solution(object):\n def sumOfGoodNumbers(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumOfGoodNumbers(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "sumOfGoodNumbers" + } +} \ No newline at end of file diff --git a/sum-of-imbalance-numbers-of-all-subarrays.json b/sum-of-imbalance-numbers-of-all-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..c392c8efd3b7411ddfc9941c52f05dc8edd234a4 --- /dev/null +++ b/sum-of-imbalance-numbers-of-all-subarrays.json @@ -0,0 +1,31 @@ +{ + "id": 2849, + "name": "sum-of-imbalance-numbers-of-all-subarrays", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/", + "date": "2023-06-25", + "task_description": "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 imbalance numbers** of all its **subarrays**_. A **subarray** is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [2,3,1,4] **Output:** 3 **Explanation:** There are 3 subarrays with non-zero** **imbalance numbers: - Subarray [3, 1] with an imbalance number of 1. - Subarray [3, 1, 4] with an imbalance number of 1. - Subarray [1, 4] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3. ``` **Example 2:** ``` **Input:** nums = [1,3,3,3,5] **Output:** 8 **Explanation:** There are 7 subarrays with non-zero imbalance numbers: - Subarray [1, 3] with an imbalance number of 1. - Subarray [1, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3] with an imbalance number of 1. - Subarray [1, 3, 3, 3, 5] with an imbalance number of 2. - Subarray [3, 3, 3, 5] with an imbalance number of 1. - Subarray [3, 3, 5] with an imbalance number of 1. - Subarray [3, 5] with an imbalance number of 1. The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,1,4]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,3,3,5]", + "output": "8 " + } + ], + "constraints": [ + "0 <= i < n - 1, and", + "sarr[i+1] - sarr[i] > 1", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= nums.length" + ], + "python_template": "class Solution(object):\n def sumImbalanceNumbers(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumImbalanceNumbers(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "sumImbalanceNumbers" + } +} \ No newline at end of file diff --git a/sum-of-k-subarrays-with-length-at-least-m.json b/sum-of-k-subarrays-with-length-at-least-m.json new file mode 100644 index 0000000000000000000000000000000000000000..dd70ed145eeba2fcf53d19f6eb8bd894900e254a --- /dev/null +++ b/sum-of-k-subarrays-with-length-at-least-m.json @@ -0,0 +1,33 @@ +{ + "id": 3722, + "name": "sum-of-k-subarrays-with-length-at-least-m", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sum-of-k-subarrays-with-length-at-least-m/", + "date": "2025-02-23", + "task_description": "You are given an integer array `nums` and two integers, `k` and `m`. Return the **maximum** sum of `k` non-overlapping subarrays of `nums`, where each subarray has a length of **at least** `m`. **Example 1:** **Input:** nums = [1,2,-1,3,3,4], k = 2, m = 2 **Output:** 13 **Explanation:** The optimal choice is: Subarray `nums[3..5]` with sum `3 + 3 + 4 = 10` (length is `3 >= m`). Subarray `nums[0..1]` with sum `1 + 2 = 3` (length is `2 >= m`). The total sum is `10 + 3 = 13`. **Example 2:** **Input:** nums = [-10,3,-1,-2], k = 4, m = 1 **Output:** -10 **Explanation:** The optimal choice is choosing each element as a subarray. The output is `(-10) + 3 + (-1) + (-2) = -10`. **Constraints:** `1 <= nums.length <= 2000` `-104 <= nums[i] <= 104` `1 <= k <= floor(nums.length / m)` `1 <= m <= 3`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,-1,3,3,4], k = 2, m = 2", + "output": "13 " + }, + { + "label": "Example 2", + "input": "nums = [-10,3,-1,-2], k = 4, m = 1", + "output": "-10 " + } + ], + "constraints": [ + "Subarray nums[3..5] with sum 3 + 3 + 4 = 10 (length is 3 >= m).", + "Subarray nums[0..1] with sum 1 + 2 = 3 (length is 2 >= m).", + "1 <= nums.length <= 2000", + "-104 <= nums[i] <= 104", + "1 <= k <= floor(nums.length / m)", + "1 <= m <= 3" + ], + "python_template": "class Solution(object):\n def maxSum(self, nums, k, m):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type m: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxSum(int[] nums, int k, int m) {\n \n }\n}", + "metadata": { + "func_name": "maxSum" + } +} \ No newline at end of file diff --git a/sum-of-matrix-after-queries.json b/sum-of-matrix-after-queries.json new file mode 100644 index 0000000000000000000000000000000000000000..b2458762b3270914b3ee6da4516f9fbc9b58d1ce --- /dev/null +++ b/sum-of-matrix-after-queries.json @@ -0,0 +1,35 @@ +{ + "id": 2838, + "name": "sum-of-matrix-after-queries", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/sum-of-matrix-after-queries/", + "date": "2023-05-28", + "task_description": "You are given an integer `n` and a **0-indexed** **2D array** `queries` where `queries[i] = [typei, indexi, vali]`. Initially, there is a **0-indexed** `n x n` matrix filled with `0`'s. For each query, you must apply one of the following changes: if `typei == 0`, set the values in the row with `indexi` to `vali`, overwriting any previous values. if `typei == 1`, set the values in the column with `indexi` to `vali`, overwriting any previous values. Return _the sum of integers in the matrix after all queries are applied_. **Example 1:** ``` **Input:** n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]] **Output:** 23 **Explanation:** The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 23. ``` **Example 2:** ``` **Input:** n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]] **Output:** 17 **Explanation:** The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 17. ``` **Constraints:** `1 <= n <= 104` `1 <= queries.length <= 5 * 104` `queries[i].length == 3` `0 <= typei <= 1` `0 <= indexi < n` `0 <= vali <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]", + "output": "23 " + }, + { + "label": "Example 2", + "input": "n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]", + "output": "17 " + } + ], + "constraints": [ + "if typei == 0, set the values in the row with indexi to vali, overwriting any previous values.", + "if typei == 1, set the values in the column with indexi to vali, overwriting any previous values.", + "1 <= n <= 104", + "1 <= queries.length <= 5 * 104", + "queries[i].length == 3", + "0 <= typei <= 1", + "0 <= indexiĀ < n", + "0 <= vali <= 105" + ], + "python_template": "class Solution(object):\n def matrixSumQueries(self, n, queries):\n \"\"\"\n :type n: int\n :type queries: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long matrixSumQueries(int n, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "matrixSumQueries" + } +} \ No newline at end of file diff --git a/sum-of-prefix-scores-of-strings.json b/sum-of-prefix-scores-of-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..fdfc7fd84bbdeb1a7129d2fa0d8e5082352182b4 --- /dev/null +++ b/sum-of-prefix-scores-of-strings.json @@ -0,0 +1,31 @@ +{ + "id": 2494, + "name": "sum-of-prefix-scores-of-strings", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/sum-of-prefix-scores-of-strings/", + "date": "2022-09-11", + "task_description": "You are given an array `words` of size `n` consisting of **non-empty** strings. We define the **score** of a string `term` as the **number** of strings `words[i]` such that `term` is a **prefix** of `words[i]`. For example, if `words = [\"a\", \"ab\", \"abc\", \"cab\"]`, then the score of `\"ab\"` is `2`, since `\"ab\"` is a prefix of both `\"ab\"` and `\"abc\"`. Return _an array _`answer`_ of size _`n`_ where _`answer[i]`_ is the **sum** of scores of every **non-empty** prefix of _`words[i]`. **Note** that a string is considered as a prefix of itself. **Example 1:** ``` **Input:** words = [\"abc\",\"ab\",\"bc\",\"b\"] **Output:** [5,4,3,2] **Explanation:** The answer for each string is the following: - \"abc\" has 3 prefixes: \"a\", \"ab\", and \"abc\". - There are 2 strings with the prefix \"a\", 2 strings with the prefix \"ab\", and 1 string with the prefix \"abc\". The total is answer[0] = 2 + 2 + 1 = 5. - \"ab\" has 2 prefixes: \"a\" and \"ab\". - There are 2 strings with the prefix \"a\", and 2 strings with the prefix \"ab\". The total is answer[1] = 2 + 2 = 4. - \"bc\" has 2 prefixes: \"b\" and \"bc\". - There are 2 strings with the prefix \"b\", and 1 string with the prefix \"bc\". The total is answer[2] = 2 + 1 = 3. - \"b\" has 1 prefix: \"b\". - There are 2 strings with the prefix \"b\". The total is answer[3] = 2. ``` **Example 2:** ``` **Input:** words = [\"abcd\"] **Output:** [4] **Explanation:** \"abcd\" has 4 prefixes: \"a\", \"ab\", \"abc\", and \"abcd\". Each prefix has a score of one, so the total is answer[0] = 1 + 1 + 1 + 1 = 4. ``` **Constraints:** `1 <= words.length <= 1000` `1 <= words[i].length <= 1000` `words[i]` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"abc\",\"ab\",\"bc\",\"b\"]", + "output": "[5,4,3,2] " + }, + { + "label": "Example 2", + "input": "words = [\"abcd\"]", + "output": "[4] " + } + ], + "constraints": [ + "For example, if words = [\"a\", \"ab\", \"abc\", \"cab\"], then the score of \"ab\" is 2, since \"ab\" is a prefix of both \"ab\" and \"abc\".", + "1 <= words.length <= 1000", + "1 <= words[i].length <= 1000", + "words[i] consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def sumPrefixScores(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] sumPrefixScores(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "sumPrefixScores" + } +} \ No newline at end of file diff --git a/sum-of-scores-of-built-strings.json b/sum-of-scores-of-built-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..616f3d5be53efa24adc9fca00c6a9e5f4cfdb899 --- /dev/null +++ b/sum-of-scores-of-built-strings.json @@ -0,0 +1,30 @@ +{ + "id": 2326, + "name": "sum-of-scores-of-built-strings", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/sum-of-scores-of-built-strings/", + "date": "2022-03-19", + "task_description": "You are **building** a string `s` of length `n` **one** character at a time, **prepending** each new character to the **front** of the string. The strings are labeled from `1` to `n`, where the string with length `i` is labeled `si`. For example, for `s = \"abaca\"`, `s1 == \"a\"`, `s2 == \"ca\"`, `s3 == \"aca\"`, etc. The **score** of `si` is the length of the **longest common prefix** between `si` and `sn` (Note that `s == sn`). Given the final string `s`, return_ the **sum** of the **score** of every _`si`. **Example 1:** ``` **Input:** s = \"babab\" **Output:** 9 **Explanation:** For s1 == \"b\", the longest common prefix is \"b\" which has a score of 1. For s2 == \"ab\", there is no common prefix so the score is 0. For s3 == \"bab\", the longest common prefix is \"bab\" which has a score of 3. For s4 == \"abab\", there is no common prefix so the score is 0. For s5 == \"babab\", the longest common prefix is \"babab\" which has a score of 5. The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9. ``` **Example 2:** ``` **Input:** s = \"azbazbzaz\" **Output:** 14 **Explanation:** For s2 == \"az\", the longest common prefix is \"az\" which has a score of 2. For s6 == \"azbzaz\", the longest common prefix is \"azb\" which has a score of 3. For s9 == \"azbazbzaz\", the longest common prefix is \"azbazbzaz\" which has a score of 9. For all other si, the score is 0. The sum of the scores is 2 + 3 + 9 = 14, so we return 14. ``` **Constraints:** `1 <= s.length <= 105` `s` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"babab\"", + "output": "9 " + }, + { + "label": "Example 2", + "input": "s = \"azbazbzaz\"", + "output": "14 " + } + ], + "constraints": [ + "For example, for s = \"abaca\", s1 == \"a\", s2 == \"ca\", s3 == \"aca\", etc.", + "1 <= s.length <= 105", + "s consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def sumScores(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long sumScores(String s) {\n \n }\n}", + "metadata": { + "func_name": "sumScores" + } +} \ No newline at end of file diff --git a/sum-of-squares-of-special-elements.json b/sum-of-squares-of-special-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..3330a9faa695fb10d544243201b4be3e229aea19 --- /dev/null +++ b/sum-of-squares-of-special-elements.json @@ -0,0 +1,29 @@ +{ + "id": 2844, + "name": "sum-of-squares-of-special-elements", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/sum-of-squares-of-special-elements/", + "date": "2023-07-09", + "task_description": "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] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21. ``` **Example 2:** ``` **Input:** nums = [2,7,1,19,18,3] **Output:** 63 **Explanation:** There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63. ``` **Constraints:** `1 <= nums.length == n <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4]", + "output": "21 " + }, + { + "label": "Example 2", + "input": "nums = [2,7,1,19,18,3]", + "output": "63 " + } + ], + "constraints": [ + "1 <= nums.length == n <= 50", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def sumOfSquares(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumOfSquares(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "sumOfSquares" + } +} \ No newline at end of file diff --git a/sum-of-total-strength-of-wizards.json b/sum-of-total-strength-of-wizards.json new file mode 100644 index 0000000000000000000000000000000000000000..88e9e7799d77faa3fd3fbed5b624ebb85c0db24b --- /dev/null +++ b/sum-of-total-strength-of-wizards.json @@ -0,0 +1,31 @@ +{ + "id": 2368, + "name": "sum-of-total-strength-of-wizards", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/sum-of-total-strength-of-wizards/", + "date": "2022-05-15", + "task_description": "As the ruler of a kingdom, you have an army of wizards at your command. You are given a **0-indexed** integer array `strength`, where `strength[i]` denotes the strength of the `ith` wizard. For a **contiguous** group of wizards (i.e. the wizards' strengths form a **subarray** of `strength`), the **total strength** is defined as the **product** of the following two values: The strength of the **weakest** wizard in the group. The **total** of all the individual strengths of the wizards in the group. Return _the **sum** of the total strengths of **all** contiguous groups of wizards_. Since the answer may be very large, return it **modulo** `109 + 7`. A **subarray** is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** strength = [1,3,1,2] **Output:** 44 **Explanation:** The following are all the contiguous groups of wizards: - [1] from [**1**,3,1,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1 - [3] from [1,**3**,1,2] has a total strength of min([3]) * sum([3]) = 3 * 3 = 9 - [1] from [1,3,**1**,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1 - [2] from [1,3,1,**2**] has a total strength of min([2]) * sum([2]) = 2 * 2 = 4 - [1,3] from [**1,3**,1,2] has a total strength of min([1,3]) * sum([1,3]) = 1 * 4 = 4 - [3,1] from [1,**3,1**,2] has a total strength of min([3,1]) * sum([3,1]) = 1 * 4 = 4 - [1,2] from [1,3,**1,2**] has a total strength of min([1,2]) * sum([1,2]) = 1 * 3 = 3 - [1,3,1] from [**1,3,1**,2] has a total strength of min([1,3,1]) * sum([1,3,1]) = 1 * 5 = 5 - [3,1,2] from [1,**3,1,2**] has a total strength of min([3,1,2]) * sum([3,1,2]) = 1 * 6 = 6 - [1,3,1,2] from [**1,3,1,2**] has a total strength of min([1,3,1,2]) * sum([1,3,1,2]) = 1 * 7 = 7 The sum of all the total strengths is 1 + 9 + 1 + 4 + 4 + 4 + 3 + 5 + 6 + 7 = 44. ``` **Example 2:** ``` **Input:** strength = [5,4,6] **Output:** 213 **Explanation:** The following are all the contiguous groups of wizards: - [5] from [**5**,4,6] has a total strength of min([5]) * sum([5]) = 5 * 5 = 25 - [4] from [5,**4**,6] has a total strength of min([4]) * sum([4]) = 4 * 4 = 16 - [6] from [5,4,**6**] has a total strength of min([6]) * sum([6]) = 6 * 6 = 36 - [5,4] from [**5,4**,6] has a total strength of min([5,4]) * sum([5,4]) = 4 * 9 = 36 - [4,6] from [5,**4,6**] has a total strength of min([4,6]) * sum([4,6]) = 4 * 10 = 40 - [5,4,6] from [**5,4,6**] has a total strength of min([5,4,6]) * sum([5,4,6]) = 4 * 15 = 60 The sum of all the total strengths is 25 + 16 + 36 + 36 + 40 + 60 = 213. ``` **Constraints:** `1 <= strength.length <= 105` `1 <= strength[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "strength = [1,3,1,2]", + "output": "44 " + }, + { + "label": "Example 2", + "input": "strength = [5,4,6]", + "output": "213 " + } + ], + "constraints": [ + "The strength of the weakest wizard in the group.", + "The total of all the individual strengths of the wizards in the group.", + "1 <= strength.length <= 105", + "1 <= strength[i] <= 109" + ], + "python_template": "class Solution(object):\n def totalStrength(self, strength):\n \"\"\"\n :type strength: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int totalStrength(int[] strength) {\n \n }\n}", + "metadata": { + "func_name": "totalStrength" + } +} \ No newline at end of file diff --git a/sum-of-values-at-indices-with-k-set-bits.json b/sum-of-values-at-indices-with-k-set-bits.json new file mode 100644 index 0000000000000000000000000000000000000000..da50159186f6bf55a6b754fb282399d8234f06d3 --- /dev/null +++ b/sum-of-values-at-indices-with-k-set-bits.json @@ -0,0 +1,31 @@ +{ + "id": 3093, + "name": "sum-of-values-at-indices-with-k-set-bits", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits/", + "date": "2023-09-10", + "task_description": "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 representation of `21` is `10101`, which has `3` set bits. **Example 1:** ``` **Input:** nums = [5,10,1,5,2], k = 1 **Output:** 13 **Explanation:** The binary representation of the indices are: 0 = 0002 1 = 0012 2 = 0102 3 = 0112 4 = 1002 Indices 1, 2, and 4 have k = 1 set bits in their binary representation. Hence, the answer is nums[1] + nums[2] + nums[4] = 13. ``` **Example 2:** ``` **Input:** nums = [4,3,2,1], k = 2 **Output:** 1 **Explanation:** The binary representation of the indices are: 0 = 002 1 = 012 2 = 102 3 = 112 Only index 3 has k = 2 set bits in its binary representation. Hence, the answer is nums[3] = 1. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 105` `0 <= k <= 10`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,10,1,5,2], k = 1", + "output": "13 " + }, + { + "label": "Example 2", + "input": "nums = [4,3,2,1], k = 2", + "output": "1 " + } + ], + "constraints": [ + "For example, the binary representation of 21 is 10101, which has 3 set bits.", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 105", + "0 <= k <= 10" + ], + "python_template": "class Solution(object):\n def sumIndicesWithKSetBits(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumIndicesWithKSetBits(List nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "sumIndicesWithKSetBits" + } +} \ No newline at end of file diff --git a/sum-of-variable-length-subarrays.json b/sum-of-variable-length-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..61b7e4e3546389ea090ca69fb5c7ffeb41013dbd --- /dev/null +++ b/sum-of-variable-length-subarrays.json @@ -0,0 +1,29 @@ +{ + "id": 3731, + "name": "sum-of-variable-length-subarrays", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/sum-of-variable-length-subarrays/", + "date": "2025-01-12", + "task_description": "You are given an integer array `nums` of size `n`. For **each** index `i` where `0 <= i < n`, define a subarray `nums[start ... i]` where `start = max(0, i - nums[i])`. Return the total sum of all elements from the subarray defined for each index in the array. **Example 1:** **Input:** nums = [2,3,1] **Output:** 11 **Explanation:** i Subarray Sum 0 `nums[0] = [2]` 2 1 `nums[0 ... 1] = [2, 3]` 5 2 `nums[1 ... 2] = [3, 1]` 4 **Total Sum** 11 The total sum is 11. Hence, 11 is the output. **Example 2:** **Input:** nums = [3,1,1,2] **Output:** 13 **Explanation:** i Subarray Sum 0 `nums[0] = [3]` 3 1 `nums[0 ... 1] = [3, 1]` 4 2 `nums[1 ... 2] = [1, 1]` 2 3 `nums[1 ... 3] = [1, 1, 2]` 4 **Total Sum** 13 The total sum is 13. Hence, 13 is the output. **Constraints:** `1 <= n == nums.length <= 100` `1 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,1]", + "output": "11 " + }, + { + "label": "Example 2", + "input": "nums = [3,1,1,2]", + "output": "13 " + } + ], + "constraints": [ + "1 <= n == nums.length <= 100", + "1 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def subarraySum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int subarraySum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "subarraySum" + } +} \ No newline at end of file diff --git a/take-gifts-from-the-richest-pile.json b/take-gifts-from-the-richest-pile.json new file mode 100644 index 0000000000000000000000000000000000000000..ec12e130abcef4d29d2caf2e0e71b72b7d548bcc --- /dev/null +++ b/take-gifts-from-the-richest-pile.json @@ -0,0 +1,33 @@ +{ + "id": 2692, + "name": "take-gifts-from-the-richest-pile", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/take-gifts-from-the-richest-pile/", + "date": "2023-01-29", + "task_description": "You are given an integer array `gifts` denoting the number of gifts in various piles. Every second, you do the following: Choose the pile with the maximum number of gifts. If there is more than one pile with the maximum number of gifts, choose any. Reduce the number of gifts in the pile to the floor of the square root of the original number of gifts in the pile. Return _the number of gifts remaining after _`k`_ seconds._ **Example 1:** ``` **Input:** gifts = [25,64,9,4,100], k = 4 **Output:** 29 **Explanation:** The gifts are taken in the following way: - In the first second, the last pile is chosen and 10 gifts are left behind. - Then the second pile is chosen and 8 gifts are left behind. - After that the first pile is chosen and 5 gifts are left behind. - Finally, the last pile is chosen again and 3 gifts are left behind. The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29. ``` **Example 2:** ``` **Input:** gifts = [1,1,1,1], k = 4 **Output:** 4 **Explanation:** In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile. That is, you can't take any pile with you. So, the total gifts remaining are 4. ``` **Constraints:** `1 <= gifts.length <= 103` `1 <= gifts[i] <= 109` `1 <= k <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "gifts = [25,64,9,4,100], k = 4", + "output": "29 " + }, + { + "label": "Example 2", + "input": "gifts = [1,1,1,1], k = 4", + "output": "4 " + } + ], + "constraints": [ + "Choose the pile with the maximum number of gifts.", + "If there is more than one pile with the maximum number of gifts, choose any.", + "Reduce the number of gifts in the pile to the floor of the square root of the original number of gifts in the pile.", + "1 <= gifts.length <= 103", + "1 <= gifts[i] <= 109", + "1 <= k <= 103" + ], + "python_template": "class Solution(object):\n def pickGifts(self, gifts, k):\n \"\"\"\n :type gifts: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long pickGifts(int[] gifts, int k) {\n \n }\n}", + "metadata": { + "func_name": "pickGifts" + } +} \ No newline at end of file diff --git a/take-k-of-each-character-from-left-and-right.json b/take-k-of-each-character-from-left-and-right.json new file mode 100644 index 0000000000000000000000000000000000000000..405d778533ee362f38fa9d4a38441d121009eeb8 --- /dev/null +++ b/take-k-of-each-character-from-left-and-right.json @@ -0,0 +1,30 @@ +{ + "id": 2599, + "name": "take-k-of-each-character-from-left-and-right", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/", + "date": "2022-12-18", + "task_description": "You are given a string `s` consisting of the characters `'a'`, `'b'`, and `'c'` and a non-negative integer `k`. Each minute, you may take either the **leftmost** character of `s`, or the **rightmost** character of `s`. Return_ the **minimum** number of minutes needed for you to take **at least** _`k`_ of each character, or return _`-1`_ if it is not possible to take _`k`_ of each character._ **Example 1:** ``` **Input:** s = \"aabaaaacaabc\", k = 2 **Output:** 8 **Explanation:** Take three characters from the left of s. You now have two 'a' characters, and one 'b' character. Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters. A total of 3 + 5 = 8 minutes is needed. It can be proven that 8 is the minimum number of minutes needed. ``` **Example 2:** ``` **Input:** s = \"a\", k = 1 **Output:** -1 **Explanation:** It is not possible to take one 'b' or 'c' so return -1. ``` **Constraints:** `1 <= s.length <= 105` `s` consists of only the letters `'a'`, `'b'`, and `'c'`. `0 <= k <= s.length`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aabaaaacaabc\", k = 2", + "output": "8 " + }, + { + "label": "Example 2", + "input": "s = \"a\", k = 1", + "output": "-1 " + } + ], + "constraints": [ + "1 <= s.length <= 105", + "s consists of only the letters 'a', 'b', and 'c'.", + "0 <= k <= s.length" + ], + "python_template": "class Solution(object):\n def takeCharacters(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int takeCharacters(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "takeCharacters" + } +} \ No newline at end of file diff --git a/task-scheduler-ii.json b/task-scheduler-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..015471330811cab5129b9ab2048e9c061439d6ac --- /dev/null +++ b/task-scheduler-ii.json @@ -0,0 +1,32 @@ +{ + "id": 2483, + "name": "task-scheduler-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/task-scheduler-ii/", + "date": "2022-07-23", + "task_description": "You are given a **0-indexed** array of positive integers `tasks`, representing tasks that need to be completed **in order**, where `tasks[i]` represents the **type** of the `ith` task. You are also given a positive integer `space`, which represents the **minimum** number of days that must pass **after** the completion of a task before another task of the **same** type can be performed. Each day, until all tasks have been completed, you must either: Complete the next task from `tasks`, or Take a break. Return_ the **minimum** number of days needed to complete all tasks_. **Example 1:** ``` **Input:** tasks = [1,2,1,2,3,1], space = 3 **Output:** 9 **Explanation:** One way to complete all tasks in 9 days is as follows: Day 1: Complete the 0th task. Day 2: Complete the 1st task. Day 3: Take a break. Day 4: Take a break. Day 5: Complete the 2nd task. Day 6: Complete the 3rd task. Day 7: Take a break. Day 8: Complete the 4th task. Day 9: Complete the 5th task. It can be shown that the tasks cannot be completed in less than 9 days. ``` **Example 2:** ``` **Input:** tasks = [5,8,8,5], space = 2 **Output:** 6 **Explanation:** One way to complete all tasks in 6 days is as follows: Day 1: Complete the 0th task. Day 2: Complete the 1st task. Day 3: Take a break. Day 4: Take a break. Day 5: Complete the 2nd task. Day 6: Complete the 3rd task. It can be shown that the tasks cannot be completed in less than 6 days. ``` **Constraints:** `1 <= tasks.length <= 105` `1 <= tasks[i] <= 109` `1 <= space <= tasks.length`", + "test_case": [ + { + "label": "Example 1", + "input": "tasks = [1,2,1,2,3,1], space = 3", + "output": "9 " + }, + { + "label": "Example 2", + "input": "tasks = [5,8,8,5], space = 2", + "output": "6 " + } + ], + "constraints": [ + "Complete the next task from tasks, or", + "Take a break.", + "1 <= tasks.length <= 105", + "1 <= tasks[i] <= 109", + "1 <= space <= tasks.length" + ], + "python_template": "class Solution(object):\n def taskSchedulerII(self, tasks, space):\n \"\"\"\n :type tasks: List[int]\n :type space: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long taskSchedulerII(int[] tasks, int space) {\n \n }\n}", + "metadata": { + "func_name": "taskSchedulerII" + } +} \ No newline at end of file diff --git a/the-latest-time-to-catch-a-bus.json b/the-latest-time-to-catch-a-bus.json new file mode 100644 index 0000000000000000000000000000000000000000..5cbe75e72ef89ab4d90afd19a36b8b6c7bd8c821 --- /dev/null +++ b/the-latest-time-to-catch-a-bus.json @@ -0,0 +1,35 @@ +{ + "id": 2417, + "name": "the-latest-time-to-catch-a-bus", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/the-latest-time-to-catch-a-bus/", + "date": "2022-06-25", + "task_description": "You are given a **0-indexed** integer array `buses` of length `n`, where `buses[i]` represents the departure time of the `ith` bus. You are also given a **0-indexed** integer array `passengers` of length `m`, where `passengers[j]` represents the arrival time of the `jth` passenger. All bus departure times are unique. All passenger arrival times are unique. You are given an integer `capacity`, which represents the **maximum** number of passengers that can get on each bus. When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at `x` minutes if you arrive at `y` minutes where `y <= x`, and the bus is not full. Passengers with the **earliest** arrival times get on the bus first. More formally when a bus arrives, either: If `capacity` or fewer passengers are waiting for a bus, they will **all** get on the bus, or The `capacity` passengers with the **earliest** arrival times will get on the bus. Return _the latest time you may arrive at the bus station to catch a bus_. You **cannot** arrive at the same time as another passenger. **Note: **The arrays `buses` and `passengers` are not necessarily sorted. **Example 1:** ``` **Input:** buses = [10,20], passengers = [2,17,18,19], capacity = 2 **Output:** 16 **Explanation:** Suppose you arrive at time 16. At time 10, the first bus departs with the 0th passenger. At time 20, the second bus departs with you and the 1st passenger. Note that you may not arrive at the same time as another passenger, which is why you must arrive before the 1st passenger to catch the bus. ``` **Example 2:** ``` **Input:** buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2 **Output:** 20 **Explanation:** Suppose you arrive at time 20. At time 10, the first bus departs with the 3rd passenger. At time 20, the second bus departs with the 5th and 1st passengers. At time 30, the third bus departs with the 0th passenger and you. Notice if you had arrived any later, then the 6th passenger would have taken your seat on the third bus. ``` **Constraints:** `n == buses.length` `m == passengers.length` `1 <= n, m, capacity <= 105` `2 <= buses[i], passengers[i] <= 109` Each element in `buses` is **unique**. Each element in `passengers` is **unique**.", + "test_case": [ + { + "label": "Example 1", + "input": "buses = [10,20], passengers = [2,17,18,19], capacity = 2", + "output": "16 " + }, + { + "label": "Example 2", + "input": "buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2", + "output": "20 " + } + ], + "constraints": [ + "If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or", + "The capacity passengers with the earliest arrival times will get on the bus.", + "n == buses.length", + "m == passengers.length", + "1 <= n, m, capacity <= 105", + "2 <= buses[i], passengers[i] <= 109", + "Each element in buses is unique.", + "Each element in passengers is unique." + ], + "python_template": "class Solution(object):\n def latestTimeCatchTheBus(self, buses, passengers, capacity):\n \"\"\"\n :type buses: List[int]\n :type passengers: List[int]\n :type capacity: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int latestTimeCatchTheBus(int[] buses, int[] passengers, int capacity) {\n \n }\n}", + "metadata": { + "func_name": "latestTimeCatchTheBus" + } +} \ No newline at end of file diff --git a/the-number-of-beautiful-subsets.json b/the-number-of-beautiful-subsets.json new file mode 100644 index 0000000000000000000000000000000000000000..8191ea63c2bebaa085d605a70fe93c8bc54b3652 --- /dev/null +++ b/the-number-of-beautiful-subsets.json @@ -0,0 +1,29 @@ +{ + "id": 2696, + "name": "the-number-of-beautiful-subsets", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/the-number-of-beautiful-subsets/", + "date": "2023-03-12", + "task_description": "You are given an array `nums` of positive integers and a **positive** integer `k`. A subset of `nums` is **beautiful** if it does not contain two integers with an absolute difference equal to `k`. Return _the number of **non-empty beautiful **subsets of the array_ `nums`. A **subset** of `nums` is an array that can be obtained by deleting some (possibly none) elements from `nums`. Two subsets are different if and only if the chosen indices to delete are different. **Example 1:** ``` **Input:** nums = [2,4,6], k = 2 **Output:** 4 **Explanation:** The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. ``` **Example 2:** ``` **Input:** nums = [1], k = 1 **Output:** 1 **Explanation:** The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. ``` **Constraints:** `1 <= nums.length <= 18` `1 <= nums[i], k <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,4,6], k = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1], k = 1", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 18", + "1 <= nums[i], k <= 1000" + ], + "python_template": "class Solution(object):\n def beautifulSubsets(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int beautifulSubsets(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "beautifulSubsets" + } +} \ No newline at end of file diff --git a/time-needed-to-rearrange-a-binary-string.json b/time-needed-to-rearrange-a-binary-string.json new file mode 100644 index 0000000000000000000000000000000000000000..fd0246466ebab18853840b5ff6c6501b968d5f4b --- /dev/null +++ b/time-needed-to-rearrange-a-binary-string.json @@ -0,0 +1,29 @@ +{ + "id": 2464, + "name": "time-needed-to-rearrange-a-binary-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/", + "date": "2022-08-06", + "task_description": "You are given a binary string `s`. In one second, **all** occurrences of `\"01\"` are **simultaneously** replaced with `\"10\"`. This process **repeats** until no occurrences of `\"01\"` exist. Return_ the number of seconds needed to complete this process._ **Example 1:** ``` **Input:** s = \"0110101\" **Output:** 4 **Explanation:** After one second, s becomes \"1011010\". After another second, s becomes \"1101100\". After the third second, s becomes \"1110100\". After the fourth second, s becomes \"1111000\". No occurrence of \"01\" exists any longer, and the process needed 4 seconds to complete, so we return 4. ``` **Example 2:** ``` **Input:** s = \"11100\" **Output:** 0 **Explanation:** No occurrence of \"01\" exists in s, and the processes needed 0 seconds to complete, so we return 0. ``` **Constraints:** `1 <= s.length <= 1000` `s[i]` is either `'0'` or `'1'`. **Follow up:** Can you solve this problem in O(n) time complexity?", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"0110101\"", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"11100\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= s.length <= 1000", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def secondsToRemoveOccurrences(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int secondsToRemoveOccurrences(String s) {\n \n }\n}", + "metadata": { + "func_name": "secondsToRemoveOccurrences" + } +} \ No newline at end of file diff --git a/time-to-cross-a-bridge.json b/time-to-cross-a-bridge.json new file mode 100644 index 0000000000000000000000000000000000000000..1470923388c830064719707a39b68032129ce4ad --- /dev/null +++ b/time-to-cross-a-bridge.json @@ -0,0 +1,40 @@ +{ + "id": 2642, + "name": "time-to-cross-a-bridge", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/time-to-cross-a-bridge/", + "date": "2023-01-01", + "task_description": "There are `k` workers who want to move `n` boxes from the right (old) warehouse to the left (new) warehouse. You are given the two integers `n` and `k`, and a 2D integer array `time` of size `k x 4` where `time[i] = [righti, picki, lefti, puti]`. The warehouses are separated by a river and connected by a bridge. Initially, all `k` workers are waiting on the left side of the bridge. To move the boxes, the `ith` worker can do the following: Cross the bridge to the right side in `righti` minutes. Pick a box from the right warehouse in `picki` minutes. Cross the bridge to the left side in `lefti` minutes. Put the box into the left warehouse in `puti` minutes. The `ith` worker is **less efficient** than the j`th` worker if either condition is met: `lefti + righti > leftj + rightj` `lefti + righti == leftj + rightj` and `i > j` The following rules regulate the movement of the workers through the bridge: Only one worker can use the bridge at a time. When the bridge is unused prioritize the **least efficient** worker (who have picked up the box) on the right side to cross. If not, prioritize the **least efficient** worker on the left side to cross. If enough workers have already been dispatched from the left side to pick up all the remaining boxes, **no more** workers will be sent from the left side. Return the **elapsed minutes** at which the last box reaches the **left side of the bridge**. **Example 1:** **Input:** n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]] **Output:** 6 **Explanation:** ``` From 0 to 1 minutes: worker 2 crosses the bridge to the right. From 1 to 2 minutes: worker 2 picks up a box from the right warehouse. From 2 to 6 minutes: worker 2 crosses the bridge to the left. From 6 to 7 minutes: worker 2 puts a box at the left warehouse. The whole process ends after 7 minutes. We return 6 because the problem asks for the instance of time at which the last worker reaches the left side of the bridge. ``` **Example 2:** **Input:** n = 3, k = 2, time = [[1,5,1,8],[10,10,10,10]] **Output:** 37 **Explanation:** ``` ``` The last box reaches the left side at 37 seconds. Notice, how we **do not** put the last boxes down, as that would take more time, and they are already on the left with the workers. **Constraints:** `1 <= n, k <= 104` `time.length == k` `time[i].length == 4` `1 <= lefti, picki, righti, puti <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "n = 3, k = 2, time = [[1,5,1,8],[10,10,10,10]]", + "output": "37 " + } + ], + "constraints": [ + "Cross the bridge to the right side in righti minutes.", + "Pick a box from the right warehouse in picki minutes.", + "Cross the bridge to the left side in lefti minutes.", + "Put the box into the left warehouse in puti minutes.", + "lefti + righti > leftj + rightj", + "lefti + righti == leftj + rightj and i > j", + "Only one worker can use the bridge at a time.", + "When the bridge is unused prioritize the least efficient worker (who have picked up the box) on the right side to cross. If not,Ā prioritize the least efficient worker on the left side to cross.", + "If enough workers have already been dispatched from the left side to pick up all the remaining boxes, no more workers will be sent from the left side.", + "1 <= n, k <= 104", + "time.length == k", + "time[i].length == 4", + "1 <= lefti, picki, righti, puti <= 1000" + ], + "python_template": "class Solution(object):\n def findCrossingTime(self, n, k, time):\n \"\"\"\n :type n: int\n :type k: int\n :type time: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findCrossingTime(int n, int k, int[][] time) {\n \n }\n}", + "metadata": { + "func_name": "findCrossingTime" + } +} \ No newline at end of file diff --git a/total-appeal-of-a-string.json b/total-appeal-of-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..011833edd9bb98bcecc2220d81e3834fc9eb4abb --- /dev/null +++ b/total-appeal-of-a-string.json @@ -0,0 +1,30 @@ +{ + "id": 2340, + "name": "total-appeal-of-a-string", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/total-appeal-of-a-string/", + "date": "2022-04-24", + "task_description": "The appeal of a string is the number of **distinct** characters found in the string. For example, the appeal of `\"abbca\"` is `3` because it has `3` distinct characters: `'a'`, `'b'`, and `'c'`. Given a string `s`, return _the **total appeal of all of its **substrings**.**_ A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` **Input:** s = \"abbca\" **Output:** 28 **Explanation:** The following are the substrings of \"abbca\": - Substrings of length 1: \"a\", \"b\", \"b\", \"c\", \"a\" have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5. - Substrings of length 2: \"ab\", \"bb\", \"bc\", \"ca\" have an appeal of 2, 1, 2, and 2 respectively. The sum is 7. - Substrings of length 3: \"abb\", \"bbc\", \"bca\" have an appeal of 2, 2, and 3 respectively. The sum is 7. - Substrings of length 4: \"abbc\", \"bbca\" have an appeal of 3 and 3 respectively. The sum is 6. - Substrings of length 5: \"abbca\" has an appeal of 3. The sum is 3. The total sum is 5 + 7 + 7 + 6 + 3 = 28. ``` **Example 2:** ``` **Input:** s = \"code\" **Output:** 20 **Explanation:** The following are the substrings of \"code\": - Substrings of length 1: \"c\", \"o\", \"d\", \"e\" have an appeal of 1, 1, 1, and 1 respectively. The sum is 4. - Substrings of length 2: \"co\", \"od\", \"de\" have an appeal of 2, 2, and 2 respectively. The sum is 6. - Substrings of length 3: \"cod\", \"ode\" have an appeal of 3 and 3 respectively. The sum is 6. - Substrings of length 4: \"code\" has an appeal of 4. The sum is 4. The total sum is 4 + 6 + 6 + 4 = 20. ``` **Constraints:** `1 <= s.length <= 105` `s` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abbca\"", + "output": "28 " + }, + { + "label": "Example 2", + "input": "s = \"code\"", + "output": "20 " + } + ], + "constraints": [ + "For example, the appeal of \"abbca\" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'.", + "1 <= s.length <= 105", + "s consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def appealSum(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long appealSum(String s) {\n \n }\n}", + "metadata": { + "func_name": "appealSum" + } +} \ No newline at end of file diff --git a/total-characters-in-string-after-transformations-i.json b/total-characters-in-string-after-transformations-i.json new file mode 100644 index 0000000000000000000000000000000000000000..ba99ff864fddd5805e5e8acae86c9fe6abb8ce9b --- /dev/null +++ b/total-characters-in-string-after-transformations-i.json @@ -0,0 +1,71 @@ +{ + "id": 3629, + "name": "total-characters-in-string-after-transformations-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/total-characters-in-string-after-transformations-i/", + "date": "2024-10-20", + "task_description": "You are given a string `s` and an integer `t`, representing the number of **transformations** to perform. In one **transformation**, every character in `s` is replaced according to the following rules: If the character is `'z'`, replace it with the string `\"ab\"`. Otherwise, replace it with the **next** character in the alphabet. For example, `'a'` is replaced with `'b'`, `'b'` is replaced with `'c'`, and so on. Return the **length** of the resulting string after **exactly** `t` transformations. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** s = \"abcyy\", t = 2 **Output:** 7 **Explanation:** **First Transformation (t = 1)**: `'a'` becomes `'b'` `'b'` becomes `'c'` `'c'` becomes `'d'` `'y'` becomes `'z'` `'y'` becomes `'z'` String after the first transformation: `\"bcdzz\"` **Second Transformation (t = 2)**: `'b'` becomes `'c'` `'c'` becomes `'d'` `'d'` becomes `'e'` `'z'` becomes `\"ab\"` `'z'` becomes `\"ab\"` String after the second transformation: `\"cdeabab\"` **Final Length of the string**: The string is `\"cdeabab\"`, which has 7 characters. **Example 2:** **Input:** s = \"azbk\", t = 1 **Output:** 5 **Explanation:** **First Transformation (t = 1)**: `'a'` becomes `'b'` `'z'` becomes `\"ab\"` `'b'` becomes `'c'` `'k'` becomes `'l'` String after the first transformation: `\"babcl\"` **Final Length of the string**: The string is `\"babcl\"`, which has 5 characters. **Constraints:** `1 <= s.length <= 105` `s` consists only of lowercase English letters. `1 <= t <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcyy\", t = 2", + "output": "7 " + }, + { + "label": "Example 2", + "input": "s = \"azbk\", t = 1", + "output": "5 " + } + ], + "constraints": [ + "If the character is 'z', replace it with the string \"ab\".", + "Otherwise, replace it with the next character in the alphabet. For example, 'a' is replaced with 'b', 'b' is replaced with 'c', and so on.", + "First Transformation (t = 1):\n\n\t\n'a' becomes 'b'\n'b' becomes 'c'\n'c' becomes 'd'\n'y' becomes 'z'\n'y' becomes 'z'\nString after the first transformation: \"bcdzz\"", + "'a' becomes 'b'", + "'b' becomes 'c'", + "'c' becomes 'd'", + "'y' becomes 'z'", + "'y' becomes 'z'", + "String after the first transformation: \"bcdzz\"", + "Second Transformation (t = 2):\n\t\n'b' becomes 'c'\n'c' becomes 'd'\n'd' becomes 'e'\n'z' becomes \"ab\"\n'z' becomes \"ab\"\nString after the second transformation: \"cdeabab\"", + "'b' becomes 'c'", + "'c' becomes 'd'", + "'d' becomes 'e'", + "'z' becomes \"ab\"", + "'z' becomes \"ab\"", + "String after the second transformation: \"cdeabab\"", + "Final Length of the string: The string is \"cdeabab\", which has 7 characters.", + "'a' becomes 'b'", + "'b' becomes 'c'", + "'c' becomes 'd'", + "'y' becomes 'z'", + "'y' becomes 'z'", + "String after the first transformation: \"bcdzz\"", + "'b' becomes 'c'", + "'c' becomes 'd'", + "'d' becomes 'e'", + "'z' becomes \"ab\"", + "'z' becomes \"ab\"", + "String after the second transformation: \"cdeabab\"", + "First Transformation (t = 1):\n\n\t\n'a' becomes 'b'\n'z' becomes \"ab\"\n'b' becomes 'c'\n'k' becomes 'l'\nString after the first transformation: \"babcl\"", + "'a' becomes 'b'", + "'z' becomes \"ab\"", + "'b' becomes 'c'", + "'k' becomes 'l'", + "String after the first transformation: \"babcl\"", + "Final Length of the string: The string is \"babcl\", which has 5 characters.", + "'a' becomes 'b'", + "'z' becomes \"ab\"", + "'b' becomes 'c'", + "'k' becomes 'l'", + "String after the first transformation: \"babcl\"", + "1 <= s.length <= 105", + "s consists only of lowercase English letters.", + "1 <= t <= 105" + ], + "python_template": "class Solution(object):\n def lengthAfterTransformations(self, s, t):\n \"\"\"\n :type s: str\n :type t: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int lengthAfterTransformations(String s, int t) {\n \n }\n}", + "metadata": { + "func_name": "lengthAfterTransformations" + } +} \ No newline at end of file diff --git a/total-characters-in-string-after-transformations-ii.json b/total-characters-in-string-after-transformations-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..10bc2da6ea85439cb1553935b0fab2740a72644a --- /dev/null +++ b/total-characters-in-string-after-transformations-ii.json @@ -0,0 +1,73 @@ +{ + "id": 3630, + "name": "total-characters-in-string-after-transformations-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/total-characters-in-string-after-transformations-ii/", + "date": "2024-10-20", + "task_description": "You are given a string `s` consisting of lowercase English letters, an integer `t` representing the number of **transformations** to perform, and an array `nums` of size 26. In one **transformation**, every character in `s` is replaced according to the following rules: Replace `s[i]` with the **next** `nums[s[i] - 'a']` consecutive characters in the alphabet. For example, if `s[i] = 'a'` and `nums[0] = 3`, the character `'a'` transforms into the next 3 consecutive characters ahead of it, which results in `\"bcd\"`. The transformation **wraps** around the alphabet if it exceeds `'z'`. For example, if `s[i] = 'y'` and `nums[24] = 3`, the character `'y'` transforms into the next 3 consecutive characters ahead of it, which results in `\"zab\"`. Return the length of the resulting string after **exactly** `t` transformations. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** s = \"abcyy\", t = 2, nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2] **Output:** 7 **Explanation:** **First Transformation (t = 1):** `'a'` becomes `'b'` as `nums[0] == 1` `'b'` becomes `'c'` as `nums[1] == 1` `'c'` becomes `'d'` as `nums[2] == 1` `'y'` becomes `'z'` as `nums[24] == 1` `'y'` becomes `'z'` as `nums[24] == 1` String after the first transformation: `\"bcdzz\"` **Second Transformation (t = 2):** `'b'` becomes `'c'` as `nums[1] == 1` `'c'` becomes `'d'` as `nums[2] == 1` `'d'` becomes `'e'` as `nums[3] == 1` `'z'` becomes `'ab'` as `nums[25] == 2` `'z'` becomes `'ab'` as `nums[25] == 2` String after the second transformation: `\"cdeabab\"` **Final Length of the string:** The string is `\"cdeabab\"`, which has 7 characters. **Example 2:** **Input:** s = \"azbk\", t = 1, nums = [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2] **Output:** 8 **Explanation:** **First Transformation (t = 1):** `'a'` becomes `'bc'` as `nums[0] == 2` `'z'` becomes `'ab'` as `nums[25] == 2` `'b'` becomes `'cd'` as `nums[1] == 2` `'k'` becomes `'lm'` as `nums[10] == 2` String after the first transformation: `\"bcabcdlm\"` **Final Length of the string:** The string is `\"bcabcdlm\"`, which has 8 characters. **Constraints:** `1 <= s.length <= 105` `s` consists only of lowercase English letters. `1 <= t <= 109` `nums.length == 26` `1 <= nums[i] <= 25`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcyy\", t = 2, nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "s = \"azbk\", t = 1, nums = [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]", + "output": "8 " + } + ], + "constraints": [ + "Replace s[i] with the next nums[s[i] - 'a'] consecutive characters in the alphabet. For example, if s[i] = 'a' and nums[0] = 3, the character 'a' transforms into the next 3 consecutive characters ahead of it, which results in \"bcd\".", + "The transformation wraps around the alphabet if it exceeds 'z'. For example, if s[i] = 'y' and nums[24] = 3, the character 'y' transforms into the next 3 consecutive characters ahead of it, which results in \"zab\".", + "First Transformation (t = 1):\n\n'a' becomes 'b' as nums[0] == 1\n'b' becomes 'c' as nums[1] == 1\n'c' becomes 'd' as nums[2] == 1\n'y' becomes 'z' as nums[24] == 1\n'y' becomes 'z' as nums[24] == 1\nString after the first transformation: \"bcdzz\"", + "'a' becomes 'b' as nums[0] == 1", + "'b' becomes 'c' as nums[1] == 1", + "'c' becomes 'd' as nums[2] == 1", + "'y' becomes 'z' as nums[24] == 1", + "'y' becomes 'z' as nums[24] == 1", + "String after the first transformation: \"bcdzz\"", + "Second Transformation (t = 2):\n\n'b' becomes 'c' as nums[1] == 1\n'c' becomes 'd' as nums[2] == 1\n'd' becomes 'e' as nums[3] == 1\n'z' becomes 'ab' as nums[25] == 2\n'z' becomes 'ab' as nums[25] == 2\nString after the second transformation: \"cdeabab\"", + "'b' becomes 'c' as nums[1] == 1", + "'c' becomes 'd' as nums[2] == 1", + "'d' becomes 'e' as nums[3] == 1", + "'z' becomes 'ab' as nums[25] == 2", + "'z' becomes 'ab' as nums[25] == 2", + "String after the second transformation: \"cdeabab\"", + "Final Length of the string: The string is \"cdeabab\", which has 7 characters.", + "'a' becomes 'b' as nums[0] == 1", + "'b' becomes 'c' as nums[1] == 1", + "'c' becomes 'd' as nums[2] == 1", + "'y' becomes 'z' as nums[24] == 1", + "'y' becomes 'z' as nums[24] == 1", + "String after the first transformation: \"bcdzz\"", + "'b' becomes 'c' as nums[1] == 1", + "'c' becomes 'd' as nums[2] == 1", + "'d' becomes 'e' as nums[3] == 1", + "'z' becomes 'ab' as nums[25] == 2", + "'z' becomes 'ab' as nums[25] == 2", + "String after the second transformation: \"cdeabab\"", + "First Transformation (t = 1):\n\n'a' becomes 'bc' as nums[0] == 2\n'z' becomes 'ab' as nums[25] == 2\n'b' becomes 'cd' as nums[1] == 2\n'k' becomes 'lm' as nums[10] == 2\nString after the first transformation: \"bcabcdlm\"", + "'a' becomes 'bc' as nums[0] == 2", + "'z' becomes 'ab' as nums[25] == 2", + "'b' becomes 'cd' as nums[1] == 2", + "'k' becomes 'lm' as nums[10] == 2", + "String after the first transformation: \"bcabcdlm\"", + "Final Length of the string: The string is \"bcabcdlm\", which has 8 characters.", + "'a' becomes 'bc' as nums[0] == 2", + "'z' becomes 'ab' as nums[25] == 2", + "'b' becomes 'cd' as nums[1] == 2", + "'k' becomes 'lm' as nums[10] == 2", + "String after the first transformation: \"bcabcdlm\"", + "1 <= s.length <= 105", + "s consists only of lowercase English letters.", + "1 <= t <= 109", + "nums.length == 26", + "1 <= nums[i] <= 25" + ], + "python_template": "class Solution(object):\n def lengthAfterTransformations(self, s, t, nums):\n \"\"\"\n :type s: str\n :type t: int\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int lengthAfterTransformations(String s, int t, List nums) {\n \n }\n}", + "metadata": { + "func_name": "lengthAfterTransformations" + } +} \ No newline at end of file diff --git a/total-cost-to-hire-k-workers.json b/total-cost-to-hire-k-workers.json new file mode 100644 index 0000000000000000000000000000000000000000..824ca4da972275262d86b8d8b1ee9aa2daab2917 --- /dev/null +++ b/total-cost-to-hire-k-workers.json @@ -0,0 +1,38 @@ +{ + "id": 2553, + "name": "total-cost-to-hire-k-workers", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/total-cost-to-hire-k-workers/", + "date": "2022-10-30", + "task_description": "You are given a **0-indexed** integer array `costs` where `costs[i]` is the cost of hiring the `ith` worker. You are also given two integers `k` and `candidates`. We want to hire exactly `k` workers according to the following rules: You will run `k` sessions and hire exactly one worker in each session. In each hiring session, choose the worker with the lowest cost from either the first `candidates` workers or the last `candidates` workers. Break the tie by the smallest index. For example, if `costs = [3,2,7,7,1,2]` and `candidates = 2`, then in the first hiring session, we will choose the `4th` worker because they have the lowest cost `[3,2,7,7,**1**,2]`. In the second hiring session, we will choose `1st` worker because they have the same lowest cost as `4th` worker but they have the smallest index `[3,**2**,7,7,2]`. Please note that the indexing may be changed in the process. If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index. A worker can only be chosen once. Return _the total cost to hire exactly _`k`_ workers._ **Example 1:** ``` **Input:** costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 **Output:** 11 **Explanation:** We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [17,12,10,2,7,2,11,20,8]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [17,12,10,7,2,11,20,8]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [17,12,10,7,11,20,8]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11. ``` **Example 2:** ``` **Input:** costs = [1,2,4,1], k = 3, candidates = 3 **Output:** 4 **Explanation:** We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [1,2,4,1]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [2,4,1]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [2,4]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4. ``` **Constraints:** `1 <= costs.length <= 105 ` `1 <= costs[i] <= 105` `1 <= k, candidates <= costs.length`", + "test_case": [ + { + "label": "Example 1", + "input": "costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4", + "output": "11 " + }, + { + "label": "Example 2", + "input": "costs = [1,2,4,1], k = 3, candidates = 3", + "output": "4 " + } + ], + "constraints": [ + "You will run k sessions and hire exactly one worker in each session.", + "In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. Break the tie by the smallest index.\n\t\nFor example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2].\nIn the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process.", + "For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2].", + "In the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process.", + "If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.", + "A worker can only be chosen once.", + "For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2].", + "In the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process.", + "1 <= costs.length <= 105", + "1 <= costs[i] <= 105", + "1 <= k, candidates <= costs.length" + ], + "python_template": "class Solution(object):\n def totalCost(self, costs, k, candidates):\n \"\"\"\n :type costs: List[int]\n :type k: int\n :type candidates: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long totalCost(int[] costs, int k, int candidates) {\n \n }\n}", + "metadata": { + "func_name": "totalCost" + } +} \ No newline at end of file diff --git a/total-distance-traveled.json b/total-distance-traveled.json new file mode 100644 index 0000000000000000000000000000000000000000..438c76556c836ca03acf1b9fb0690a01dafd794a --- /dev/null +++ b/total-distance-traveled.json @@ -0,0 +1,28 @@ +{ + "id": 2857, + "name": "total-distance-traveled", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/total-distance-traveled/", + "date": "2023-06-11", + "task_description": "A truck has two fuel tanks. You are given two integers, `mainTank` representing the fuel present in the main tank in liters and `additionalTank` representing the fuel present in the additional tank in liters. The truck has a mileage of `10` km per liter. Whenever `5` liters of fuel get used up in the main tank, if the additional tank has at least `1` liters of fuel, `1` liters of fuel will be transferred from the additional tank to the main tank. Return _the maximum distance which can be traveled._ **Note: **Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed. **Example 1:** ``` **Input:** mainTank = 5, additionalTank = 10 **Output:** 60 **Explanation:** After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km. After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty. Total distance traveled is 60km. ``` **Example 2:** ``` **Input:** mainTank = 1, additionalTank = 2 **Output:** 10 **Explanation:** After spending 1 litre of fuel, the main tank becomes empty. Total distance traveled is 10km. ``` **Constraints:** `1 <= mainTank, additionalTank <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "mainTank = 5, additionalTank = 10", + "output": "60 " + }, + { + "label": "Example 2", + "input": "mainTank = 1, additionalTank = 2", + "output": "10 " + } + ], + "constraints": [ + "1 <= mainTank, additionalTank <= 100" + ], + "python_template": "class Solution(object):\n def distanceTraveled(self, mainTank, additionalTank):\n \"\"\"\n :type mainTank: int\n :type additionalTank: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int distanceTraveled(int mainTank, int additionalTank) {\n \n }\n}", + "metadata": { + "func_name": "distanceTraveled" + } +} \ No newline at end of file diff --git a/transform-array-by-parity.json b/transform-array-by-parity.json new file mode 100644 index 0000000000000000000000000000000000000000..aa6465aa24106a4374fb70dacafcfec366bee30f --- /dev/null +++ b/transform-array-by-parity.json @@ -0,0 +1,33 @@ +{ + "id": 3778, + "name": "transform-array-by-parity", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/transform-array-by-parity/", + "date": "2025-02-15", + "task_description": "You are given an integer array `nums`. Transform `nums` by performing the following operations in the **exact** order specified: Replace each even number with 0. Replace each odd numbers with 1. Sort the modified array in **non-decreasing** order. Return the resulting array after performing these operations. **Example 1:** **Input:** nums = [4,3,2,1] **Output:** [0,0,1,1] **Explanation:** Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, `nums = [0, 1, 0, 1]`. After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1]`. **Example 2:** **Input:** nums = [1,5,1,4,2] **Output:** [0,0,1,1,1] **Explanation:** Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, `nums = [1, 1, 1, 0, 0]`. After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1, 1]`. **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,3,2,1]", + "output": "[0,0,1,1] " + }, + { + "label": "Example 2", + "input": "nums = [1,5,1,4,2]", + "output": "[0,0,1,1,1] " + } + ], + "constraints": [ + "Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, nums = [0, 1, 0, 1].", + "After sorting nums in non-descending order, nums = [0, 0, 1, 1].", + "Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, nums = [1, 1, 1, 0, 0].", + "After sorting nums in non-descending order, nums = [0, 0, 1, 1, 1].", + "1 <= nums.length <= 100", + "1 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def transformArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] transformArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "transformArray" + } +} \ No newline at end of file diff --git a/transformed-array.json b/transformed-array.json new file mode 100644 index 0000000000000000000000000000000000000000..ee11865806d0dc05f365fb439c5534bdd8f718ba --- /dev/null +++ b/transformed-array.json @@ -0,0 +1,39 @@ +{ + "id": 3651, + "name": "transformed-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/transformed-array/", + "date": "2024-12-01", + "task_description": "You are given an integer array `nums` that represents a circular array. Your task is to create a new array `result` of the **same** size, following these rules: For each index `i` (where `0 <= i < nums.length`), perform the following **independent** actions: If `nums[i] > 0`: Start at index `i` and move `nums[i]` steps to the **right** in the circular array. Set `result[i]` to the value of the index where you land. If `nums[i] < 0`: Start at index `i` and move `abs(nums[i])` steps to the **left** in the circular array. Set `result[i]` to the value of the index where you land. If `nums[i] == 0`: Set `result[i]` to `nums[i]`. Return the new array `result`. **Note:** Since `nums` is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end. **Example 1:** **Input:** nums = [3,-2,1,1] **Output:** [1,1,1,3] **Explanation:** For `nums[0]` that is equal to 3, If we move 3 steps to right, we reach `nums[3]`. So `result[0]` should be 1. For `nums[1]` that is equal to -2, If we move 2 steps to left, we reach `nums[3]`. So `result[1]` should be 1. For `nums[2]` that is equal to 1, If we move 1 step to right, we reach `nums[3]`. So `result[2]` should be 1. For `nums[3]` that is equal to 1, If we move 1 step to right, we reach `nums[0]`. So `result[3]` should be 3. **Example 2:** **Input:** nums = [-1,4,-1] **Output:** [-1,-1,4] **Explanation:** For `nums[0]` that is equal to -1, If we move 1 step to left, we reach `nums[2]`. So `result[0]` should be -1. For `nums[1]` that is equal to 4, If we move 4 steps to right, we reach `nums[2]`. So `result[1]` should be -1. For `nums[2]` that is equal to -1, If we move 1 step to left, we reach `nums[1]`. So `result[2]` should be 4. **Constraints:** `1 <= nums.length <= 100` `-100 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,-2,1,1]", + "output": "[1,1,1,3] " + }, + { + "label": "Example 2", + "input": "nums = [-1,4,-1]", + "output": "[-1,-1,4] " + } + ], + "constraints": [ + "If nums[i] > 0: Start at index i and move nums[i] steps to the right in the circular array. Set result[i] to the value of the index where you land.", + "If nums[i] < 0: Start at index i and move abs(nums[i]) steps to the left in the circular array. Set result[i] to the value of the index where you land.", + "If nums[i] == 0: Set result[i] to nums[i].", + "For nums[0] that is equal to 3, If we move 3 steps to right, we reach nums[3]. So result[0] should be 1.", + "For nums[1] that is equal to -2, If we move 2 steps to left, we reach nums[3]. So result[1] should be 1.", + "For nums[2] that is equal to 1, If we move 1 step to right, we reach nums[3]. So result[2] should be 1.", + "For nums[3] that is equal to 1, If we move 1 step to right, we reach nums[0]. So result[3] should be 3.", + "For nums[0] that is equal to -1, If we move 1 step to left, we reach nums[2]. So result[0] should be -1.", + "For nums[1] that is equal to 4, If we move 4 steps to right, we reach nums[2]. So result[1] should be -1.", + "For nums[2] that is equal to -1, If we move 1 step to left, we reach nums[1]. So result[2] should be 4.", + "1 <= nums.length <= 100", + "-100 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def constructTransformedArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] constructTransformedArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "constructTransformedArray" + } +} \ No newline at end of file diff --git a/unique-3-digit-even-numbers.json b/unique-3-digit-even-numbers.json new file mode 100644 index 0000000000000000000000000000000000000000..7a910e4ab30a6fa6dd53a78105112caf3b94769d --- /dev/null +++ b/unique-3-digit-even-numbers.json @@ -0,0 +1,39 @@ +{ + "id": 3799, + "name": "unique-3-digit-even-numbers", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/unique-3-digit-even-numbers/", + "date": "2025-03-01", + "task_description": "You are given an array of digits called `digits`. Your task is to determine the number of **distinct** three-digit even numbers that can be formed using these digits. **Note**: Each _copy_ of a digit can only be used **once per number**, and there may **not** be leading zeros. **Example 1:** **Input:** digits = [1,2,3,4] **Output:** 12 **Explanation:** The 12 distinct 3-digit even numbers that can be formed are 124, 132, 134, 142, 214, 234, 312, 314, 324, 342, 412, and 432. Note that 222 cannot be formed because there is only 1 copy of the digit 2. **Example 2:** **Input:** digits = [0,2,2] **Output:** 2 **Explanation:** The only 3-digit even numbers that can be formed are 202 and 220. Note that the digit 2 can be used twice because it appears twice in the array. **Example 3:** **Input:** digits = [6,6,6] **Output:** 1 **Explanation:** Only 666 can be formed. **Example 4:** **Input:** digits = [1,3,5] **Output:** 0 **Explanation:** No even 3-digit numbers can be formed. **Constraints:** `3 <= digits.length <= 10` `0 <= digits[i] <= 9`", + "test_case": [ + { + "label": "Example 1", + "input": "digits = [1,2,3,4]", + "output": "12 " + }, + { + "label": "Example 2", + "input": "digits = [0,2,2]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "digits = [6,6,6]", + "output": "1 " + }, + { + "label": "Example 4", + "input": "digits = [1,3,5]", + "output": "0 " + } + ], + "constraints": [ + "3 <= digits.length <= 10", + "0 <= digits[i] <= 9" + ], + "python_template": "class Solution(object):\n def totalNumbers(self, digits):\n \"\"\"\n :type digits: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int totalNumbers(int[] digits) {\n \n }\n}", + "metadata": { + "func_name": "totalNumbers" + } +} \ No newline at end of file diff --git a/using-a-robot-to-print-the-lexicographically-smallest-string.json b/using-a-robot-to-print-the-lexicographically-smallest-string.json new file mode 100644 index 0000000000000000000000000000000000000000..a098f7a17d42be3f13ceb300c1a07d353a5cf3cc --- /dev/null +++ b/using-a-robot-to-print-the-lexicographically-smallest-string.json @@ -0,0 +1,36 @@ +{ + "id": 2520, + "name": "using-a-robot-to-print-the-lexicographically-smallest-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/", + "date": "2022-10-02", + "task_description": "You are given a string `s` and a robot that currently holds an empty string `t`. Apply one of the following operations until `s` and `t` **are both empty**: Remove the **first** character of a string `s` and give it to the robot. The robot will append this character to the string `t`. Remove the **last** character of a string `t` and give it to the robot. The robot will write this character on paper. Return _the lexicographically smallest string that can be written on the paper._ **Example 1:** ``` **Input:** s = \"zza\" **Output:** \"azz\" **Explanation:** Let p denote the written string. Initially p=\"\", s=\"zza\", t=\"\". Perform first operation three times p=\"\", s=\"\", t=\"zza\". Perform second operation three times p=\"azz\", s=\"\", t=\"\". ``` **Example 2:** ``` **Input:** s = \"bac\" **Output:** \"abc\" **Explanation:** Let p denote the written string. Perform first operation twice p=\"\", s=\"c\", t=\"ba\". Perform second operation twice p=\"ab\", s=\"c\", t=\"\". Perform first operation p=\"ab\", s=\"\", t=\"c\". Perform second operation p=\"abc\", s=\"\", t=\"\". ``` **Example 3:** ``` **Input:** s = \"bdda\" **Output:** \"addb\" **Explanation:** Let p denote the written string. Initially p=\"\", s=\"bdda\", t=\"\". Perform first operation four times p=\"\", s=\"\", t=\"bdda\". Perform second operation four times p=\"addb\", s=\"\", t=\"\". ``` **Constraints:** `1 <= s.length <= 105` `s` consists of only English lowercase letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"zza\"", + "output": "\"azz\" " + }, + { + "label": "Example 2", + "input": "s = \"bac\"", + "output": "\"abc\" " + }, + { + "label": "Example 3", + "input": "s = \"bdda\"", + "output": "\"addb\" " + } + ], + "constraints": [ + "Remove the first character of a string s and give it to the robot. The robot will append this character to the string t.", + "Remove the last character of a string t and give it to the robot. The robot will write this character on paper.", + "1 <= s.length <= 105", + "s consists of only English lowercase letters." + ], + "python_template": "class Solution(object):\n def robotWithString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String robotWithString(String s) {\n \n }\n}", + "metadata": { + "func_name": "robotWithString" + } +} \ No newline at end of file diff --git a/visit-array-positions-to-maximize-score.json b/visit-array-positions-to-maximize-score.json new file mode 100644 index 0000000000000000000000000000000000000000..b94411c7901fefa6d8f9edeac1b256aea33a48c1 --- /dev/null +++ b/visit-array-positions-to-maximize-score.json @@ -0,0 +1,32 @@ +{ + "id": 2893, + "name": "visit-array-positions-to-maximize-score", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/visit-array-positions-to-maximize-score/", + "date": "2023-07-08", + "task_description": "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 get a score of `nums[i]`. If you move from a position `i` to a position `j` and the **parities** of `nums[i]` and `nums[j]` differ, then you lose a score of `x`. Return _the **maximum** total score you can get_. **Note** that initially you have `nums[0]` points. **Example 1:** ``` **Input:** nums = [2,3,6,1,9,2], x = 5 **Output:** 13 **Explanation:** We can visit the following positions in the array: 0 -> 2 -> 3 -> 4. The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5. The total score will be: 2 + 6 + 1 + 9 - 5 = 13. ``` **Example 2:** ``` **Input:** nums = [2,4,6,8], x = 3 **Output:** 20 **Explanation:** All the integers in the array have the same parities, so we can visit all of them without losing any score. The total score is: 2 + 4 + 6 + 8 = 20. ``` **Constraints:** `2 <= nums.length <= 105` `1 <= nums[i], x <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,6,1,9,2], x = 5", + "output": "13 " + }, + { + "label": "Example 2", + "input": "nums = [2,4,6,8], x = 3", + "output": "20 " + } + ], + "constraints": [ + "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 get a score of nums[i].", + "If you move from a position i to a position j and the parities of nums[i] and nums[j] differ, then you lose a score of x.", + "2 <= nums.length <= 105", + "1 <= nums[i], x <= 106" + ], + "python_template": "class Solution(object):\n def maxScore(self, nums, x):\n \"\"\"\n :type nums: List[int]\n :type x: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxScore(int[] nums, int x) {\n \n }\n}", + "metadata": { + "func_name": "maxScore" + } +} \ No newline at end of file diff --git a/vowels-game-in-a-string.json b/vowels-game-in-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..06d9852b4966e39b8ae6115733b6c5c06d8d89bf --- /dev/null +++ b/vowels-game-in-a-string.json @@ -0,0 +1,35 @@ +{ + "id": 3462, + "name": "vowels-game-in-a-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/vowels-game-in-a-string/", + "date": "2024-07-14", + "task_description": "Alice and Bob are playing a game on a string. You are given a string `s`, Alice and Bob will take turns playing the following game where Alice starts **first**: On Alice's turn, she has to remove any **non-empty** substring from `s` that contains an **odd** number of vowels. On Bob's turn, he has to remove any **non-empty** substring from `s` that contains an **even** number of vowels. The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play **optimally**. Return `true` if Alice wins the game, and `false` otherwise. The English vowels are: `a`, `e`, `i`, `o`, and `u`. **Example 1:** **Input:** s = \"leetcoder\" **Output:** true **Explanation:** Alice can win the game as follows: Alice plays first, she can delete the underlined substring in `s = \"**leetco**der\"` which contains 3 vowels. The resulting string is `s = \"der\"`. Bob plays second, he can delete the underlined substring in `s = \"**d**er\"` which contains 0 vowels. The resulting string is `s = \"er\"`. Alice plays third, she can delete the whole string `s = \"**er**\"` which contains 1 vowel. Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game. **Example 2:** **Input:** s = \"bbcd\" **Output:** false **Explanation:** There is no valid play for Alice in her first turn, so Alice loses the game. **Constraints:** `1 <= s.length <= 105` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"leetcoder\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"bbcd\"", + "output": "false " + } + ], + "constraints": [ + "On Alice's turn, she has to remove any non-empty substring from s that contains an odd number of vowels.", + "On Bob's turn, he has to remove any non-empty substring from s that contains an even number of vowels.", + "Alice plays first, she can delete the underlined substring in s = \"leetcoder\" which contains 3 vowels. The resulting string is s = \"der\".", + "Bob plays second, he can delete the underlined substring in s = \"der\" which contains 0 vowels. The resulting string is s = \"er\".", + "Alice plays third, she can delete the whole string s = \"er\" which contains 1 vowel.", + "Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.", + "1 <= s.length <= 105", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def doesAliceWin(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean doesAliceWin(String s) {\n \n }\n}", + "metadata": { + "func_name": "doesAliceWin" + } +} \ No newline at end of file diff --git a/water-bottles-ii.json b/water-bottles-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..4006932f3f950deb329aa130ee1f44e69fecc362 --- /dev/null +++ b/water-bottles-ii.json @@ -0,0 +1,31 @@ +{ + "id": 3336, + "name": "water-bottles-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/water-bottles-ii/", + "date": "2024-03-24", + "task_description": "You are given two integers `numBottles` and `numExchange`. `numBottles` represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations: Drink any number of full water bottles turning them into empty bottles. Exchange `numExchange` empty bottles with one full water bottle. Then, increase `numExchange` by one. Note that you cannot exchange multiple batches of empty bottles for the same value of `numExchange`. For example, if `numBottles == 3` and `numExchange == 1`, you cannot exchange `3` empty water bottles for `3` full bottles. Return _the **maximum** number of water bottles you can drink_. **Example 1:** ``` **Input:** numBottles = 13, numExchange = 6 **Output:** 15 **Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk. ``` **Example 2:** ``` **Input:** numBottles = 10, numExchange = 3 **Output:** 13 **Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk. ``` **Constraints:** `1 <= numBottles <= 100 ` `1 <= numExchange <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "numBottles = 13, numExchange = 6", + "output": "15 " + }, + { + "label": "Example 2", + "input": "numBottles = 10, numExchange = 3", + "output": "13 " + } + ], + "constraints": [ + "Drink any number of full water bottles turning them into empty bottles.", + "Exchange numExchange empty bottles with one full water bottle. Then, increase numExchange by one.", + "1 <= numBottles <= 100", + "1 <= numExchange <= 100" + ], + "python_template": "class Solution(object):\n def maxBottlesDrunk(self, numBottles, numExchange):\n \"\"\"\n :type numBottles: int\n :type numExchange: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxBottlesDrunk(int numBottles, int numExchange) {\n \n }\n}", + "metadata": { + "func_name": "maxBottlesDrunk" + } +} \ No newline at end of file diff --git a/ways-to-express-an-integer-as-sum-of-powers.json b/ways-to-express-an-integer-as-sum-of-powers.json new file mode 100644 index 0000000000000000000000000000000000000000..d8a1e7d06a9ad60d3cedcf0584658aa4bf90813c --- /dev/null +++ b/ways-to-express-an-integer-as-sum-of-powers.json @@ -0,0 +1,29 @@ +{ + "id": 2882, + "name": "ways-to-express-an-integer-as-sum-of-powers", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/ways-to-express-an-integer-as-sum-of-powers/", + "date": "2023-07-08", + "task_description": "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, if `n = 160` and `x = 3`, one way to express `n` is `n = 23 + 33 + 53`. **Example 1:** ``` **Input:** n = 10, x = 2 **Output:** 1 **Explanation:** We can express n as the following: n = 32 + 12 = 10. It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers. ``` **Example 2:** ``` **Input:** n = 4, x = 1 **Output:** 2 **Explanation:** We can express n in the following ways: - n = 41 = 4. - n = 31 + 11 = 4. ``` **Constraints:** `1 <= n <= 300` `1 <= x <= 5`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 10, x = 2", + "output": "1 " + }, + { + "label": "Example 2", + "input": "n = 4, x = 1", + "output": "2 " + } + ], + "constraints": [ + "1 <= n <= 300", + "1 <= x <= 5" + ], + "python_template": "class Solution(object):\n def numberOfWays(self, n, x):\n \"\"\"\n :type n: int\n :type x: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfWays(int n, int x) {\n \n }\n}", + "metadata": { + "func_name": "numberOfWays" + } +} \ No newline at end of file diff --git a/ways-to-split-array-into-good-subarrays.json b/ways-to-split-array-into-good-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..d6c8bb56f5a466045b8c169b941c935bc519ba1c --- /dev/null +++ b/ways-to-split-array-into-good-subarrays.json @@ -0,0 +1,29 @@ +{ + "id": 2867, + "name": "ways-to-split-array-into-good-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/ways-to-split-array-into-good-subarrays/", + "date": "2023-06-18", + "task_description": "You are given a binary array `nums`. A subarray of an array is **good** if it contains **exactly** **one** element with the value `1`. Return _an integer denoting the number of ways to split the array _`nums`_ into **good** subarrays_. As the number may be too large, return it **modulo** `109 + 7`. A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [0,1,0,0,1] **Output:** 3 **Explanation:** There are 3 ways to split nums into good subarrays: - [0,1] [0,0,1] - [0,1,0] [0,1] - [0,1,0,0] [1] ``` **Example 2:** ``` **Input:** nums = [0,1,0] **Output:** 1 **Explanation:** There is 1 way to split nums into good subarrays: - [0,1,0] ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 1`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,0,0,1]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [0,1,0]", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i] <= 1" + ], + "python_template": "class Solution(object):\n def numberOfGoodSubarraySplits(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfGoodSubarraySplits(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "numberOfGoodSubarraySplits" + } +} \ No newline at end of file diff --git a/words-within-two-edits-of-dictionary.json b/words-within-two-edits-of-dictionary.json new file mode 100644 index 0000000000000000000000000000000000000000..b0cadcd0c0ca98adf7745aa8edaf2713a21917dd --- /dev/null +++ b/words-within-two-edits-of-dictionary.json @@ -0,0 +1,31 @@ +{ + "id": 2550, + "name": "words-within-two-edits-of-dictionary", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/words-within-two-edits-of-dictionary/", + "date": "2022-10-15", + "task_description": "You are given two string arrays, `queries` and `dictionary`. All words in each array comprise of lowercase English letters and have the same length. In one **edit** you can take a word from `queries`, and change any letter in it to any other letter. Find all words from `queries` that, after a **maximum** of two edits, equal some word from `dictionary`. Return_ a list of all words from _`queries`_, __that match with some word from _`dictionary`_ after a maximum of **two edits**_. Return the words in the **same order** they appear in `queries`. **Example 1:** ``` **Input:** queries = [\"word\",\"note\",\"ants\",\"wood\"], dictionary = [\"wood\",\"joke\",\"moat\"] **Output:** [\"word\",\"note\",\"wood\"] **Explanation:** - Changing the 'r' in \"word\" to 'o' allows it to equal the dictionary word \"wood\". - Changing the 'n' to 'j' and the 't' to 'k' in \"note\" changes it to \"joke\". - It would take more than 2 edits for \"ants\" to equal a dictionary word. - \"wood\" can remain unchanged (0 edits) and match the corresponding dictionary word. Thus, we return [\"word\",\"note\",\"wood\"]. ``` **Example 2:** ``` **Input:** queries = [\"yes\"], dictionary = [\"not\"] **Output:** [] **Explanation:** Applying any two edits to \"yes\" cannot make it equal to \"not\". Thus, we return an empty array. ``` **Constraints:** `1 <= queries.length, dictionary.length <= 100` `n == queries[i].length == dictionary[j].length` `1 <= n <= 100` All `queries[i]` and `dictionary[j]` are composed of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "queries = [\"word\",\"note\",\"ants\",\"wood\"], dictionary = [\"wood\",\"joke\",\"moat\"]", + "output": "[\"word\",\"note\",\"wood\"] " + }, + { + "label": "Example 2", + "input": "queries = [\"yes\"], dictionary = [\"not\"]", + "output": "[] " + } + ], + "constraints": [ + "1 <= queries.length, dictionary.length <= 100", + "n == queries[i].length == dictionary[j].length", + "1 <= n <= 100", + "All queries[i] and dictionary[j] are composed of lowercase English letters." + ], + "python_template": "class Solution(object):\n def twoEditWords(self, queries, dictionary):\n \"\"\"\n :type queries: List[str]\n :type dictionary: List[str]\n :rtype: List[str]\n \"\"\"\n ", + "java_template": "class Solution {\n public List twoEditWords(String[] queries, String[] dictionary) {\n \n }\n}", + "metadata": { + "func_name": "twoEditWords" + } +} \ No newline at end of file diff --git a/zero-array-transformation-i.json b/zero-array-transformation-i.json new file mode 100644 index 0000000000000000000000000000000000000000..0a068d6a629aed7084b43fb47cddd3f30fecb671 --- /dev/null +++ b/zero-array-transformation-i.json @@ -0,0 +1,49 @@ +{ + "id": 3639, + "name": "zero-array-transformation-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/zero-array-transformation-i/", + "date": "2024-11-10", + "task_description": "You are given an integer array `nums` of length `n` and a 2D array `queries`, where `queries[i] = [li, ri]`. For each `queries[i]`: Select a subset of indices within the range `[li, ri]` in `nums`. Decrement the values at the selected indices by 1. A **Zero Array** is an array where all elements are equal to 0. Return `true` if it is _possible_ to transform `nums` into a **Zero Array **after processing all the queries sequentially, otherwise return `false`. **Example 1:** **Input:** nums = [1,0,1], queries = [[0,2]] **Output:** true **Explanation:** **For i = 0:** Select the subset of indices as `[0, 2]` and decrement the values at these indices by 1. The array will become `[0, 0, 0]`, which is a Zero Array. **Example 2:** **Input:** nums = [4,3,2,1], queries = [[1,3],[0,2]] **Output:** false **Explanation:** **For i = 0:** Select the subset of indices as `[1, 2, 3]` and decrement the values at these indices by 1. The array will become `[4, 2, 1, 0]`. **For i = 1:** Select the subset of indices as `[0, 1, 2]` and decrement the values at these indices by 1. The array will become `[3, 1, 0, 0]`, which is not a Zero Array. **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 105` `1 <= queries.length <= 105` `queries[i].length == 2` `0 <= li <= ri < nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,0,1], queries = [[0,2]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [4,3,2,1], queries = [[1,3],[0,2]]", + "output": "false " + } + ], + "constraints": [ + "Select a subset of indices within the range [li, ri] in nums.", + "Decrement the values at the selected indices by 1.", + "For i = 0:\n\nSelect the subset of indices as [0, 2] and decrement the values at these indices by 1.\nThe array will become [0, 0, 0], which is a Zero Array.", + "Select the subset of indices as [0, 2] and decrement the values at these indices by 1.", + "The array will become [0, 0, 0], which is a Zero Array.", + "Select the subset of indices as [0, 2] and decrement the values at these indices by 1.", + "The array will become [0, 0, 0], which is a Zero Array.", + "For i = 0:\n\nSelect the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.\nThe array will become [4, 2, 1, 0].", + "Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.", + "The array will become [4, 2, 1, 0].", + "For i = 1:\n\nSelect the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.\nThe array will become [3, 1, 0, 0], which is not a Zero Array.", + "Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.", + "The array will become [3, 1, 0, 0], which is not a Zero Array.", + "Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.", + "The array will become [4, 2, 1, 0].", + "Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.", + "The array will become [3, 1, 0, 0], which is not a Zero Array.", + "1 <= nums.length <= 105", + "0 <= nums[i] <= 105", + "1 <= queries.length <= 105", + "queries[i].length == 2", + "0 <= li <= ri < nums.length" + ], + "python_template": "class Solution(object):\n def isZeroArray(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[List[int]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isZeroArray(int[] nums, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "isZeroArray" + } +} \ No newline at end of file diff --git a/zero-array-transformation-ii.json b/zero-array-transformation-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..28f5bf8bffe6567dbf28c95830f115fe58f9d169 --- /dev/null +++ b/zero-array-transformation-ii.json @@ -0,0 +1,55 @@ +{ + "id": 3643, + "name": "zero-array-transformation-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/zero-array-transformation-ii/", + "date": "2024-11-10", + "task_description": "You are given an integer array `nums` of length `n` and a 2D array `queries` where `queries[i] = [li, ri, vali]`. Each `queries[i]` represents the following action on `nums`: Decrement the value at each index in the range `[li, ri]` in `nums` by **at most** `vali`. The amount by which each value is decremented can be chosen **independently** for each index. A **Zero Array** is an array with all its elements equal to 0. Return the **minimum** possible **non-negative** value of `k`, such that after processing the first `k` queries in **sequence**, `nums` becomes a **Zero Array**. If no such `k` exists, return -1. **Example 1:** **Input:** nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]] **Output:** 2 **Explanation:** **For i = 0 (l = 0, r = 2, val = 1):** Decrement values at indices `[0, 1, 2]` by `[1, 0, 1]` respectively. The array will become `[1, 0, 1]`. **For i = 1 (l = 0, r = 2, val = 1):** Decrement values at indices `[0, 1, 2]` by `[1, 0, 1]` respectively. The array will become `[0, 0, 0]`, which is a Zero Array. Therefore, the minimum value of `k` is 2. **Example 2:** **Input:** nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]] **Output:** -1 **Explanation:** **For i = 0 (l = 1, r = 3, val = 2):** Decrement values at indices `[1, 2, 3]` by `[2, 2, 1]` respectively. The array will become `[4, 1, 0, 0]`. **For i = 1 (l = 0, r = 2, val = 1):** Decrement values at indices `[0, 1, 2]` by `[1, 1, 0]` respectively. The array will become `[3, 0, 0, 0]`, which is not a Zero Array. **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 5 * 105` `1 <= queries.length <= 105` `queries[i].length == 3` `0 <= li <= ri < nums.length` `1 <= vali <= 5`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]", + "output": "-1 " + } + ], + "constraints": [ + "Decrement the value at each index in the range [li, ri] in nums by at most vali.", + "The amount by which each value is decremented can be chosen independently for each index.", + "For i = 0 (l = 0, r = 2, val = 1):\n\nDecrement values at indices [0, 1, 2] by [1, 0, 1] respectively.\nThe array will become [1, 0, 1].", + "Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively.", + "The array will become [1, 0, 1].", + "For i = 1 (l = 0, r = 2, val = 1):\n\nDecrement values at indices [0, 1, 2] by [1, 0, 1] respectively.\nThe array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.", + "Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively.", + "The array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.", + "Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively.", + "The array will become [1, 0, 1].", + "Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively.", + "The array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.", + "For i = 0 (l = 1, r = 3, val = 2):\n\nDecrement values at indices [1, 2, 3] by [2, 2, 1] respectively.\nThe array will become [4, 1, 0, 0].", + "Decrement values at indices [1, 2, 3] by [2, 2, 1] respectively.", + "The array will become [4, 1, 0, 0].", + "For i = 1 (l = 0, r = 2, val = 1):\n\nDecrement values at indices [0, 1, 2] by [1, 1, 0] respectively.\nThe array will become [3, 0, 0, 0], which is not a Zero Array.", + "Decrement values at indices [0, 1, 2] by [1, 1, 0] respectively.", + "The array will become [3, 0, 0, 0], which is not a Zero Array.", + "Decrement values at indices [1, 2, 3] by [2, 2, 1] respectively.", + "The array will become [4, 1, 0, 0].", + "Decrement values at indices [0, 1, 2] by [1, 1, 0] respectively.", + "The array will become [3, 0, 0, 0], which is not a Zero Array.", + "1 <= nums.length <= 105", + "0 <= nums[i] <= 5 * 105", + "1 <= queries.length <= 105", + "queries[i].length == 3", + "0 <= li <= ri < nums.length", + "1 <= vali <= 5" + ], + "python_template": "class Solution(object):\n def minZeroArray(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minZeroArray(int[] nums, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "minZeroArray" + } +} \ No newline at end of file diff --git a/zero-array-transformation-iv.json b/zero-array-transformation-iv.json new file mode 100644 index 0000000000000000000000000000000000000000..ace668b42755cf377a248891018e1e931b25e9bb --- /dev/null +++ b/zero-array-transformation-iv.json @@ -0,0 +1,75 @@ +{ + "id": 3795, + "name": "zero-array-transformation-iv", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/zero-array-transformation-iv/", + "date": "2025-03-09", + "task_description": "You are given an integer array `nums` of length `n` and a 2D array `queries`, where `queries[i] = [li, ri, vali]`. Each `queries[i]` represents the following action on `nums`: Select a subset of indices in the range `[li, ri]` from `nums`. Decrement the value at each selected index by **exactly** `vali`. A **Zero Array** is an array with all its elements equal to 0. Return the **minimum** possible **non-negative** value of `k`, such that after processing the first `k` queries in **sequence**, `nums` becomes a **Zero Array**. If no such `k` exists, return -1. **Example 1:** **Input:** nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]] **Output:** 2 **Explanation:** **For query 0 (l = 0, r = 2, val = 1):** Decrement the values at indices `[0, 2]` by 1. The array will become `[1, 0, 1]`. **For query 1 (l = 0, r = 2, val = 1):** Decrement the values at indices `[0, 2]` by 1. The array will become `[0, 0, 0]`, which is a Zero Array. Therefore, the minimum value of `k` is 2. **Example 2:** **Input:** nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]] **Output:** -1 **Explanation:** It is impossible to make nums a Zero Array even after all the queries. **Example 3:** **Input:** nums = [1,2,3,2,1], queries = [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]] **Output:** 4 **Explanation:** **For query 0 (l = 0, r = 1, val = 1):** Decrement the values at indices `[0, 1]` by `1`. The array will become `[0, 1, 3, 2, 1]`. **For query 1 (l = 1, r = 2, val = 1):** Decrement the values at indices `[1, 2]` by 1. The array will become `[0, 0, 2, 2, 1]`. **For query 2 (l = 2, r = 3, val = 2):** Decrement the values at indices `[2, 3]` by 2. The array will become `[0, 0, 0, 0, 1]`. **For query 3 (l = 3, r = 4, val = 1):** Decrement the value at index 4 by 1. The array will become `[0, 0, 0, 0, 0]`. Therefore, the minimum value of `k` is 4. **Example 4:** **Input:** nums = [1,2,3,2,6], queries = [[0,1,1],[0,2,1],[1,4,2],[4,4,4],[3,4,1],[4,4,5]] **Output:** 4 **Constraints:** `1 <= nums.length <= 10` `0 <= nums[i] <= 1000` `1 <= queries.length <= 1000` `queries[i] = [li, ri, vali]` `0 <= li <= ri < nums.length` `1 <= vali <= 10`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3,2,1], queries = [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]]", + "output": "4 " + }, + { + "label": "Example 4", + "input": "nums = [1,2,3,2,6], queries = [[0,1,1],[0,2,1],[1,4,2],[4,4,4],[3,4,1],[4,4,5]]", + "output": "" + } + ], + "constraints": [ + "Select a subset of indices in the range [li, ri] from nums.", + "Decrement the value at each selected index by exactly vali.", + "For query 0 (l = 0, r = 2, val = 1):\n\nDecrement the values at indices [0, 2] by 1.\nThe array will become [1, 0, 1].", + "Decrement the values at indices [0, 2] by 1.", + "The array will become [1, 0, 1].", + "For query 1 (l = 0, r = 2, val = 1):\n\nDecrement the values at indices [0, 2] by 1.\nThe array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.", + "Decrement the values at indices [0, 2] by 1.", + "The array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.", + "Decrement the values at indices [0, 2] by 1.", + "The array will become [1, 0, 1].", + "Decrement the values at indices [0, 2] by 1.", + "The array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.", + "For query 0 (l = 0, r = 1, val = 1):\n\nDecrement the values at indices [0, 1] by 1.\nThe array will become [0, 1, 3, 2, 1].", + "Decrement the values at indices [0, 1] by 1.", + "The array will become [0, 1, 3, 2, 1].", + "For query 1 (l = 1, r = 2, val = 1):\n\nDecrement the values at indices [1, 2] by 1.\nThe array will become [0, 0, 2, 2, 1].", + "Decrement the values at indices [1, 2] by 1.", + "The array will become [0, 0, 2, 2, 1].", + "For query 2 (l = 2, r = 3, val = 2):\n\nDecrement the values at indices [2, 3] by 2.\nThe array will become [0, 0, 0, 0, 1].", + "Decrement the values at indices [2, 3] by 2.", + "The array will become [0, 0, 0, 0, 1].", + "For query 3 (l = 3, r = 4, val = 1):\n\nDecrement the value at index 4 by 1.\nThe array will become [0, 0, 0, 0, 0]. Therefore, the minimum value of k is 4.", + "Decrement the value at index 4 by 1.", + "The array will become [0, 0, 0, 0, 0]. Therefore, the minimum value of k is 4.", + "Decrement the values at indices [0, 1] by 1.", + "The array will become [0, 1, 3, 2, 1].", + "Decrement the values at indices [1, 2] by 1.", + "The array will become [0, 0, 2, 2, 1].", + "Decrement the values at indices [2, 3] by 2.", + "The array will become [0, 0, 0, 0, 1].", + "Decrement the value at index 4 by 1.", + "The array will become [0, 0, 0, 0, 0]. Therefore, the minimum value of k is 4.", + "1 <= nums.length <= 10", + "0 <= nums[i] <= 1000", + "1 <= queries.length <= 1000", + "queries[i] = [li, ri, vali]", + "0 <= li <= ri < nums.length", + "1 <= vali <= 10" + ], + "python_template": "class Solution(object):\n def minZeroArray(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minZeroArray(int[] nums, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "minZeroArray" + } +} \ No newline at end of file diff --git a/zigzag-grid-traversal-with-skip.json b/zigzag-grid-traversal-with-skip.json new file mode 100644 index 0000000000000000000000000000000000000000..261dc2d553e0da84fb9066a4afc46101a67f4a9e --- /dev/null +++ b/zigzag-grid-traversal-with-skip.json @@ -0,0 +1,39 @@ +{ + "id": 3708, + "name": "zigzag-grid-traversal-with-skip", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/zigzag-grid-traversal-with-skip/", + "date": "2025-01-05", + "task_description": "You are given an `m x n` 2D array `grid` of **positive** integers. Your task is to traverse `grid` in a **zigzag** pattern while skipping every **alternate** cell. Zigzag pattern traversal is defined as following the below actions: Start at the top-left cell `(0, 0)`. Move _right_ within a row until the end of the row is reached. Drop down to the next row, then traverse _left_ until the beginning of the row is reached. Continue **alternating** between right and left traversal until every row has been traversed. **Note **that you **must skip** every _alternate_ cell during the traversal. Return an array of integers `result` containing, **in order**, the value of the cells visited during the zigzag traversal with skips. **Example 1:** **Input:** grid = [[1,2],[3,4]] **Output:** [1,4] **Explanation:** **** **Example 2:** **Input:** grid = [[2,1],[2,1],[2,1]] **Output:** [2,1,2] **Explanation:** **Example 3:** **Input:** grid = [[1,2,3],[4,5,6],[7,8,9]] **Output:** [1,3,5,7,9] **Explanation:** **Constraints:** `2 <= n == grid.length <= 50` `2 <= m == grid[i].length <= 50` `1 <= grid[i][j] <= 2500`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,2],[3,4]]", + "output": "[1,4] " + }, + { + "label": "Example 2", + "input": "grid = [[2,1],[2,1],[2,1]]", + "output": "[2,1,2] " + }, + { + "label": "Example 3", + "input": "grid = [[1,2,3],[4,5,6],[7,8,9]]", + "output": "[1,3,5,7,9] " + } + ], + "constraints": [ + "Start at the top-left cell (0, 0).", + "Move right within a row until the end of the row is reached.", + "Drop down to the next row, then traverse left until the beginning of the row is reached.", + "Continue alternating between right and left traversal until every row has been traversed.", + "2 <= n == grid.length <= 50", + "2 <= m == grid[i].length <= 50", + "1 <= grid[i][j] <= 2500" + ], + "python_template": "class Solution(object):\n def zigzagTraversal(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List zigzagTraversal(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "zigzagTraversal" + } +} \ No newline at end of file