diff --git a/add-edges-to-make-degrees-of-all-nodes-even.json b/add-edges-to-make-degrees-of-all-nodes-even.json new file mode 100644 index 0000000000000000000000000000000000000000..ea381bd0c8ce3d616b7e9699625aaf52217ebdfb --- /dev/null +++ b/add-edges-to-make-degrees-of-all-nodes-even.json @@ -0,0 +1,38 @@ +{ + "id": 2596, + "name": "add-edges-to-make-degrees-of-all-nodes-even", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/add-edges-to-make-degrees-of-all-nodes-even/", + "date": "2022-12-11", + "task_description": "There is an **undirected** graph consisting of `n` nodes numbered from `1` to `n`. You are given the integer `n` and a **2D** array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi`. The graph can be disconnected. You can add **at most** two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops. Return `true`_ if it is possible to make the degree of each node in the graph even, otherwise return _`false`_._ The degree of a node is the number of edges connected to it. **Example 1:** ``` **Input:** n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]] **Output:** true **Explanation:** The above diagram shows a valid way of adding an edge. Every node in the resulting graph is connected to an even number of edges. ``` **Example 2:** ``` **Input:** n = 4, edges = [[1,2],[3,4]] **Output:** true **Explanation:** The above diagram shows a valid way of adding two edges. ``` **Example 3:** ``` **Input:** n = 4, edges = [[1,2],[1,3],[1,4]] **Output:** false **Explanation:** It is not possible to obtain a valid graph with adding at most 2 edges. ``` **Constraints:** `3 <= n <= 105` `2 <= edges.length <= 105` `edges[i].length == 2` `1 <= ai, bi <= n` `ai != bi` There are no repeated edges.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "n = 4, edges = [[1,2],[3,4]]", + "output": "true " + }, + { + "label": "Example 3", + "input": "n = 4, edges = [[1,2],[1,3],[1,4]]", + "output": "false " + } + ], + "constraints": [ + "3 <= n <= 105", + "2 <= edges.length <= 105", + "edges[i].length == 2", + "1 <= ai, bi <= n", + "ai != bi", + "There are no repeated edges." + ], + "python_template": "class Solution(object):\n def isPossible(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isPossible(int n, List> edges) {\n \n }\n}", + "metadata": { + "func_name": "isPossible" + } +} \ No newline at end of file diff --git a/alice-and-bob-playing-flower-game.json b/alice-and-bob-playing-flower-game.json new file mode 100644 index 0000000000000000000000000000000000000000..155526a7bbf6218f0af078a42280cc3e151e75bf --- /dev/null +++ b/alice-and-bob-playing-flower-game.json @@ -0,0 +1,31 @@ +{ + "id": 3279, + "name": "alice-and-bob-playing-flower-game", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/alice-and-bob-playing-flower-game/", + "date": "2024-01-21", + "task_description": "Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are `x` flowers in the clockwise direction between Alice and Bob, and `y` flowers in the anti-clockwise direction between them. The game proceeds as follows: Alice takes the first turn. In each turn, a player must choose either the clockwise or anti-clockwise direction and pick one flower from that side. At the end of the turn, if there are no flowers left at all, the **current** player captures their opponent and wins the game. Given two integers, `n` and `m`, the task is to compute the number of possible pairs `(x, y)` that satisfy the conditions: Alice must win the game according to the described rules. The number of flowers `x` in the clockwise direction must be in the range `[1,n]`. The number of flowers `y` in the anti-clockwise direction must be in the range `[1,m]`. Return _the number of possible pairs_ `(x, y)` _that satisfy the conditions mentioned in the statement_. **Example 1:** ``` **Input:** n = 3, m = 2 **Output:** 3 **Explanation:** The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1). ``` **Example 2:** ``` **Input:** n = 1, m = 1 **Output:** 0 **Explanation:** No pairs satisfy the conditions described in the statement. ``` **Constraints:** `1 <= n, m <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, m = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "n = 1, m = 1", + "output": "0 " + } + ], + "constraints": [ + "Alice must win the game according to the described rules.", + "The number of flowers x in the clockwise direction must be in the range [1,n].", + "The number of flowers y in the anti-clockwise direction must be in the range [1,m].", + "1 <= n, m <= 105" + ], + "python_template": "class Solution(object):\n def flowerGame(self, n, m):\n \"\"\"\n :type n: int\n :type m: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long flowerGame(int n, int m) {\n \n }\n}", + "metadata": { + "func_name": "flowerGame" + } +} \ No newline at end of file diff --git a/alternating-groups-i.json b/alternating-groups-i.json new file mode 100644 index 0000000000000000000000000000000000000000..a50656e797acd7e2d39e8b690bcd080e8410b48b --- /dev/null +++ b/alternating-groups-i.json @@ -0,0 +1,31 @@ +{ + "id": 3463, + "name": "alternating-groups-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/alternating-groups-i/", + "date": "2024-06-22", + "task_description": "There is a circle of red and blue tiles. You are given an array of integers `colors`. The color of tile `i` is represented by `colors[i]`: `colors[i] == 0` means that tile `i` is **red**. `colors[i] == 1` means that tile `i` is **blue**. Every 3 contiguous tiles in the circle with **alternating** colors (the middle tile has a different color from its **left** and **right** tiles) is called an **alternating** group. Return the number of **alternating** groups. **Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other. **Example 1:** **Input:** colors = [1,1,1] **Output:** 0 **Explanation:** **Example 2:** **Input:** colors = [0,1,0,0,1] **Output:** 3 **Explanation:** Alternating groups: ******** **Constraints:** `3 <= colors.length <= 100` `0 <= colors[i] <= 1`", + "test_case": [ + { + "label": "Example 1", + "input": "colors = [1,1,1]", + "output": "0 " + }, + { + "label": "Example 2", + "input": "colors = [0,1,0,0,1]", + "output": "3 " + } + ], + "constraints": [ + "colors[i] == 0 means that tile i is red.", + "colors[i] == 1 means that tile i is blue.", + "3 <= colors.length <= 100", + "0 <= colors[i] <= 1" + ], + "python_template": "class Solution(object):\n def numberOfAlternatingGroups(self, colors):\n \"\"\"\n :type colors: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfAlternatingGroups(int[] colors) {\n \n }\n}", + "metadata": { + "func_name": "numberOfAlternatingGroups" + } +} \ No newline at end of file diff --git a/alternating-groups-ii.json b/alternating-groups-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..911188c5aa83114b27f18e43a5334fcd2b9c9d56 --- /dev/null +++ b/alternating-groups-ii.json @@ -0,0 +1,37 @@ +{ + "id": 3483, + "name": "alternating-groups-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/alternating-groups-ii/", + "date": "2024-06-22", + "task_description": "There is a circle of red and blue tiles. You are given an array of integers `colors` and an integer `k`. The color of tile `i` is represented by `colors[i]`: `colors[i] == 0` means that tile `i` is **red**. `colors[i] == 1` means that tile `i` is **blue**. An **alternating** group is every `k` contiguous tiles in the circle with **alternating** colors (each tile in the group except the first and last one has a different color from its **left** and **right** tiles). Return the number of **alternating** groups. **Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other. **Example 1:** **Input:** colors = [0,1,0,1,0], k = 3 **Output:** 3 **Explanation:** **** Alternating groups: **Example 2:** **Input:** colors = [0,1,0,0,1,0,1], k = 6 **Output:** 2 **Explanation:** **** Alternating groups: **Example 3:** **Input:** colors = [1,1,0,1], k = 4 **Output:** 0 **Explanation:** **Constraints:** `3 <= colors.length <= 105` `0 <= colors[i] <= 1` `3 <= k <= colors.length`", + "test_case": [ + { + "label": "Example 1", + "input": "colors = [0,1,0,1,0], k = 3", + "output": "3 " + }, + { + "label": "Example 2", + "input": "colors = [0,1,0,0,1,0,1], k = 6", + "output": "2 " + }, + { + "label": "Example 3", + "input": "colors = [1,1,0,1], k = 4", + "output": "0 " + } + ], + "constraints": [ + "colors[i] == 0 means that tile i is red.", + "colors[i] == 1 means that tile i is blue.", + "3 <= colors.length <= 105", + "0 <= colors[i] <= 1", + "3 <= k <= colors.length" + ], + "python_template": "class Solution(object):\n def numberOfAlternatingGroups(self, colors, k):\n \"\"\"\n :type colors: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfAlternatingGroups(int[] colors, int k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfAlternatingGroups" + } +} \ No newline at end of file diff --git a/alternating-groups-iii.json b/alternating-groups-iii.json new file mode 100644 index 0000000000000000000000000000000000000000..ff091f766da5b568632918f70185b8795bba0824 --- /dev/null +++ b/alternating-groups-iii.json @@ -0,0 +1,40 @@ +{ + "id": 3527, + "name": "alternating-groups-iii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/alternating-groups-iii/", + "date": "2024-07-28", + "task_description": "There are some red and blue tiles arranged circularly. You are given an array of integers `colors` and a 2D integers array `queries`. The color of tile `i` is represented by `colors[i]`: `colors[i] == 0` means that tile `i` is **red**. `colors[i] == 1` means that tile `i` is **blue**. An **alternating** group is a contiguous subset of tiles in the circle with **alternating** colors (each tile in the group except the first and last one has a different color from its adjacent tiles in the group). You have to process queries of two types: `queries[i] = [1, sizei]`, determine the count of **alternating** groups with size `sizei`. `queries[i] = [2, indexi, colori]`, change `colors[indexi]` to `colori`. Return an array `answer` containing the results of the queries of the first type _in order_. **Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other. **Example 1:** **Input:** colors = [0,1,1,0,1], queries = [[2,1,0],[1,4]] **Output:** [2] **Explanation:** **** First query: Change `colors[1]` to 0. Second query: Count of the alternating groups with size 4: **Example 2:** **Input:** colors = [0,0,1,0,1,1], queries = [[1,3],[2,3,0],[1,5]] **Output:** [2,0] **Explanation:** First query: Count of the alternating groups with size 3: Second query: `colors` will not change. Third query: There is no alternating group with size 5. **Constraints:** `4 <= colors.length <= 5 * 104` `0 <= colors[i] <= 1` `1 <= queries.length <= 5 * 104` `queries[i][0] == 1` or `queries[i][0] == 2` For all `i` that: `queries[i][0] == 1`: `queries[i].length == 2`, `3 <= queries[i][1] <= colors.length - 1` `queries[i][0] == 2`: `queries[i].length == 3`, `0 <= queries[i][1] <= colors.length - 1`, `0 <= queries[i][2] <= 1`", + "test_case": [ + { + "label": "Example 1", + "input": "colors = [0,1,1,0,1], queries = [[2,1,0],[1,4]]", + "output": "[2] " + }, + { + "label": "Example 2", + "input": "colors = [0,0,1,0,1,1], queries = [[1,3],[2,3,0],[1,5]]", + "output": "[2,0] " + } + ], + "constraints": [ + "colors[i] == 0 means that tile i is red.", + "colors[i] == 1 means that tile i is blue.", + "queries[i] = [1, sizei], determine the count of alternating groups with size sizei.", + "queries[i] = [2, indexi, colori], change colors[indexi] to colori.", + "4 <= colors.length <= 5 * 104", + "0 <= colors[i] <= 1", + "1 <= queries.length <= 5 * 104", + "queries[i][0] == 1 or queries[i][0] == 2", + "For all i that:\n\t\nqueries[i][0] == 1: queries[i].length == 2, 3 <= queries[i][1] <= colors.length - 1\nqueries[i][0] == 2: queries[i].length == 3, 0 <= queries[i][1] <= colors.length - 1, 0 <= queries[i][2] <= 1", + "queries[i][0] == 1: queries[i].length == 2, 3 <= queries[i][1] <= colors.length - 1", + "queries[i][0] == 2: queries[i].length == 3, 0 <= queries[i][1] <= colors.length - 1, 0 <= queries[i][2] <= 1", + "queries[i][0] == 1: queries[i].length == 2, 3 <= queries[i][1] <= colors.length - 1", + "queries[i][0] == 2: queries[i].length == 3, 0 <= queries[i][1] <= colors.length - 1, 0 <= queries[i][2] <= 1" + ], + "python_template": "class Solution(object):\n def numberOfAlternatingGroups(self, colors, queries):\n \"\"\"\n :type colors: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List numberOfAlternatingGroups(int[] colors, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "numberOfAlternatingGroups" + } +} \ No newline at end of file diff --git a/ant-on-the-boundary.json b/ant-on-the-boundary.json new file mode 100644 index 0000000000000000000000000000000000000000..13f64317f89c062738b6ab671ff95d38bb4c7fc5 --- /dev/null +++ b/ant-on-the-boundary.json @@ -0,0 +1,34 @@ +{ + "id": 3311, + "name": "ant-on-the-boundary", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/ant-on-the-boundary/", + "date": "2024-01-28", + "task_description": "An ant is on a boundary. It sometimes goes **left** and sometimes **right**. You are given an array of **non-zero** integers `nums`. The ant starts reading `nums` from the first element of it to its end. At each step, it moves according to the value of the current element: If `nums[i] < 0`, it moves **left** by `-nums[i]` units. If `nums[i] > 0`, it moves **right** by `nums[i]` units. Return _the number of times the ant **returns** to the boundary._ **Notes:** There is an infinite space on both sides of the boundary. We check whether the ant is on the boundary only after it has moved `|nums[i]|` units. In other words, if the ant crosses the boundary during its movement, it does not count. **Example 1:** ``` **Input:** nums = [2,3,-5] **Output:** 1 **Explanation:** After the first step, the ant is 2 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is on the boundary. So the answer is 1. ``` **Example 2:** ``` **Input:** nums = [3,2,-3,-4] **Output:** 0 **Explanation:** After the first step, the ant is 3 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is 2 steps to the right of the boundary. After the fourth step, the ant is 2 steps to the left of the boundary. The ant never returned to the boundary, so the answer is 0. ``` **Constraints:** `1 <= nums.length <= 100` `-10 <= nums[i] <= 10` `nums[i] != 0`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,-5]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [3,2,-3,-4]", + "output": "0 " + } + ], + "constraints": [ + "If nums[i] < 0, it moves left by -nums[i] units.", + "If nums[i] > 0, it moves right by nums[i] units.", + "There is an infinite space on both sides of the boundary.", + "We check whether the ant is on the boundary only after it has moved |nums[i]| units. In other words, if the ant crosses the boundary during its movement, it does not count.", + "1 <= nums.length <= 100", + "-10 <= nums[i] <= 10", + "nums[i] != 0" + ], + "python_template": "class Solution(object):\n def returnToBoundaryCount(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int returnToBoundaryCount(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "returnToBoundaryCount" + } +} \ No newline at end of file diff --git a/append-characters-to-string-to-make-subsequence.json b/append-characters-to-string-to-make-subsequence.json new file mode 100644 index 0000000000000000000000000000000000000000..abcbf59197a3fde11ca2239346f8d1c035cb41cb --- /dev/null +++ b/append-characters-to-string-to-make-subsequence.json @@ -0,0 +1,34 @@ +{ + "id": 2572, + "name": "append-characters-to-string-to-make-subsequence", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/append-characters-to-string-to-make-subsequence/", + "date": "2022-11-20", + "task_description": "You are given two strings `s` and `t` consisting of only lowercase English letters. Return _the minimum number of characters that need to be appended to the end of _`s`_ so that _`t`_ becomes a **subsequence** of _`s`. A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. **Example 1:** ``` **Input:** s = \"coaching\", t = \"coding\" **Output:** 4 **Explanation:** Append the characters \"ding\" to the end of s so that s = \"coachingding\". Now, t is a subsequence of s (\"**co**aching**ding**\"). It can be shown that appending any 3 characters to the end of s will never make t a subsequence. ``` **Example 2:** ``` **Input:** s = \"abcde\", t = \"a\" **Output:** 0 **Explanation:** t is already a subsequence of s (\"**a**bcde\"). ``` **Example 3:** ``` **Input:** s = \"z\", t = \"abcde\" **Output:** 5 **Explanation:** Append the characters \"abcde\" to the end of s so that s = \"zabcde\". Now, t is a subsequence of s (\"z**abcde**\"). It can be shown that appending any 4 characters to the end of s will never make t a subsequence. ``` **Constraints:** `1 <= s.length, t.length <= 105` `s` and `t` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"coaching\", t = \"coding\"", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"abcde\", t = \"a\"", + "output": "0 " + }, + { + "label": "Example 3", + "input": "s = \"z\", t = \"abcde\"", + "output": "5 " + } + ], + "constraints": [ + "1 <= s.length, t.length <= 105", + "s and t consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def appendCharacters(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int appendCharacters(String s, String t) {\n \n }\n}", + "metadata": { + "func_name": "appendCharacters" + } +} \ No newline at end of file diff --git a/append-k-integers-with-minimal-sum.json b/append-k-integers-with-minimal-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..4d27d334630efa87b5b5f2632a6bb72bfe3f253d --- /dev/null +++ b/append-k-integers-with-minimal-sum.json @@ -0,0 +1,30 @@ +{ + "id": 2305, + "name": "append-k-integers-with-minimal-sum", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/append-k-integers-with-minimal-sum/", + "date": "2022-02-27", + "task_description": "You are given an integer array `nums` and an integer `k`. Append `k` **unique positive** integers that do **not** appear in `nums` to `nums` such that the resulting total sum is **minimum**. Return_ the sum of the_ `k` _integers appended to_ `nums`. **Example 1:** ``` **Input:** nums = [1,4,25,10,25], k = 2 **Output:** 5 **Explanation:** The two unique positive integers that do not appear in nums which we append are 2 and 3. The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum. The sum of the two integers appended is 2 + 3 = 5, so we return 5. ``` **Example 2:** ``` **Input:** nums = [5,6], k = 6 **Output:** 25 **Explanation:** The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8. The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum. The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `1 <= k <= 108`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,25,10,25], k = 2", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [5,6], k = 6", + "output": "25 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "1 <= k <= 108" + ], + "python_template": "class Solution(object):\n def minimalKSum(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 minimalKSum(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimalKSum" + } +} \ No newline at end of file diff --git a/apple-redistribution-into-boxes.json b/apple-redistribution-into-boxes.json new file mode 100644 index 0000000000000000000000000000000000000000..a9ad9f0687983560d4cc984dd4c960c4e03538d7 --- /dev/null +++ b/apple-redistribution-into-boxes.json @@ -0,0 +1,31 @@ +{ + "id": 3334, + "name": "apple-redistribution-into-boxes", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/apple-redistribution-into-boxes/", + "date": "2024-03-03", + "task_description": "You are given an array `apple` of size `n` and an array `capacity` of size `m`. There are `n` packs where the `ith` pack contains `apple[i]` apples. There are `m` boxes as well, and the `ith` box has a capacity of `capacity[i]` apples. Return _the **minimum** number of boxes you need to select to redistribute these _`n`_ packs of apples into boxes_. **Note** that, apples from the same pack can be distributed into different boxes. **Example 1:** ``` **Input:** apple = [1,3,2], capacity = [4,3,1,5,2] **Output:** 2 **Explanation:** We will use boxes with capacities 4 and 5. It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples. ``` **Example 2:** ``` **Input:** apple = [5,5,5], capacity = [2,4,2,7] **Output:** 4 **Explanation:** We will need to use all the boxes. ``` **Constraints:** `1 <= n == apple.length <= 50` `1 <= m == capacity.length <= 50` `1 <= apple[i], capacity[i] <= 50` The input is generated such that it's possible to redistribute packs of apples into boxes.", + "test_case": [ + { + "label": "Example 1", + "input": "apple = [1,3,2], capacity = [4,3,1,5,2]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "apple = [5,5,5], capacity = [2,4,2,7]", + "output": "4 " + } + ], + "constraints": [ + "1 <= n == apple.length <= 50", + "1 <= m == capacity.length <= 50", + "1 <= apple[i], capacity[i] <= 50", + "The input is generated such that it's possible to redistribute packs of apples into boxes." + ], + "python_template": "class Solution(object):\n def minimumBoxes(self, apple, capacity):\n \"\"\"\n :type apple: List[int]\n :type capacity: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumBoxes(int[] apple, int[] capacity) {\n \n }\n}", + "metadata": { + "func_name": "minimumBoxes" + } +} \ No newline at end of file diff --git a/apply-bitwise-operations-to-make-strings-equal.json b/apply-bitwise-operations-to-make-strings-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..9fd8be057b16985a4765bdcf8406d3c792a2536e --- /dev/null +++ b/apply-bitwise-operations-to-make-strings-equal.json @@ -0,0 +1,32 @@ +{ + "id": 2632, + "name": "apply-bitwise-operations-to-make-strings-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/", + "date": "2023-01-15", + "task_description": "You are given two **0-indexed binary** strings `s` and `target` of the same length `n`. You can do the following operation on `s` **any** number of times: Choose two **different** indices `i` and `j` where `0 <= i, j < n`. Simultaneously, replace `s[i]` with (`s[i]` **OR** `s[j]`) and `s[j]` with (`s[i]` **XOR** `s[j]`). For example, if `s = \"0110\"`, you can choose `i = 0` and `j = 2`, then simultaneously replace `s[0]` with (`s[0]` **OR** `s[2]` = `0` **OR** `1` = `1`), and `s[2]` with (`s[0]` **XOR** `s[2]` = `0` **XOR** `1` = `1`), so we will have `s = \"1110\"`. Return `true` _if you can make the string _`s`_ equal to _`target`_, or _`false`_ otherwise_. **Example 1:** ``` **Input:** s = \"1010\", target = \"0110\" **Output:** true **Explanation:** We can do the following operations: - Choose i = 2 and j = 0. We have now s = \"**0**0**1**0\". - Choose i = 2 and j = 1. We have now s = \"0**11**0\". Since we can make s equal to target, we return true. ``` **Example 2:** ``` **Input:** s = \"11\", target = \"00\" **Output:** false **Explanation:** It is not possible to make s equal to target with any number of operations. ``` **Constraints:** `n == s.length == target.length` `2 <= n <= 105` `s` and `target` consist of only the digits `0` and `1`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"1010\", target = \"0110\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"11\", target = \"00\"", + "output": "false " + } + ], + "constraints": [ + "Choose two different indices i and j where 0 <= i, j < n.", + "Simultaneously, replace s[i] with (s[i] OR s[j]) and s[j] with (s[i] XOR s[j]).", + "n == s.length == target.length", + "2 <= n <= 105", + "s and target consist of only the digits 0 and 1." + ], + "python_template": "class Solution(object):\n def makeStringsEqual(self, s, target):\n \"\"\"\n :type s: str\n :type target: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean makeStringsEqual(String s, String target) {\n \n }\n}", + "metadata": { + "func_name": "makeStringsEqual" + } +} \ No newline at end of file diff --git a/apply-discount-to-prices.json b/apply-discount-to-prices.json new file mode 100644 index 0000000000000000000000000000000000000000..a97b5abd46d9621b6eb5ccb75ccd381af1baaf03 --- /dev/null +++ b/apply-discount-to-prices.json @@ -0,0 +1,35 @@ +{ + "id": 2373, + "name": "apply-discount-to-prices", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/apply-discount-to-prices/", + "date": "2022-05-22", + "task_description": "A **sentence** is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign `'$'`. A word represents a **price** if it is a sequence of digits preceded by a dollar sign. For example, `\"$100\"`, `\"$23\"`, and `\"$6\"` represent prices while `\"100\"`, `\"$\"`, and `\"$1e5\"` do not. You are given a string `sentence` representing a sentence and an integer `discount`. For each word representing a price, apply a discount of `discount%` on the price and **update** the word in the sentence. All updated prices should be represented with **exactly two** decimal places. Return _a string representing the modified sentence_. Note that all prices will contain **at most** `10` digits. **Example 1:** ``` **Input:** sentence = \"there are $1 $2 and 5$ candies in the shop\", discount = 50 **Output:** \"there are $0.50 $1.00 and 5$ candies in the shop\" **Explanation:** The words which represent prices are \"$1\" and \"$2\". - A 50% discount on \"$1\" yields \"$0.50\", so \"$1\" is replaced by \"$0.50\". - A 50% discount on \"$2\" yields \"$1\". Since we need to have exactly 2 decimal places after a price, we replace \"$2\" with \"$1.00\". ``` **Example 2:** ``` **Input:** sentence = \"1 2 $3 4 $5 $6 7 8$ $9 $10$\", discount = 100 **Output:** \"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$\" **Explanation:** Applying a 100% discount on any price will result in 0. The words representing prices are \"$3\", \"$5\", \"$6\", and \"$9\". Each of them is replaced by \"$0.00\". ``` **Constraints:** `1 <= sentence.length <= 105` `sentence` consists of lowercase English letters, digits, `' '`, and `'$'`. `sentence` does not have leading or trailing spaces. All words in `sentence` are separated by a single space. All prices will be **positive** numbers without leading zeros. All prices will have **at most** `10` digits. `0 <= discount <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "sentence = \"there are $1 $2 and 5$ candies in the shop\", discount = 50", + "output": "\"there are $0.50 $1.00 and 5$ candies in the shop\" " + }, + { + "label": "Example 2", + "input": "sentence = \"1 2 $3 4 $5 $6 7 8$ $9 $10$\", discount = 100", + "output": "\"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$\" " + } + ], + "constraints": [ + "For example, \"$100\", \"$23\", and \"$6\" represent prices while \"100\", \"$\", and \"$1e5\" do not.", + "1 <= sentence.length <= 105", + "sentence consists of lowercase English letters, digits, ' ', and '$'.", + "sentence does not have leading or trailing spaces.", + "All words in sentence are separated by a single space.", + "All prices will be positive numbers without leading zeros.", + "All prices will have at most 10 digits.", + "0 <= discount <= 100" + ], + "python_template": "class Solution(object):\n def discountPrices(self, sentence, discount):\n \"\"\"\n :type sentence: str\n :type discount: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String discountPrices(String sentence, int discount) {\n \n }\n}", + "metadata": { + "func_name": "discountPrices" + } +} \ No newline at end of file diff --git a/apply-operations-on-array-to-maximize-sum-of-squares.json b/apply-operations-on-array-to-maximize-sum-of-squares.json new file mode 100644 index 0000000000000000000000000000000000000000..2f7104afc0c53557bc77e3a5e9bf9686bef177a1 --- /dev/null +++ b/apply-operations-on-array-to-maximize-sum-of-squares.json @@ -0,0 +1,30 @@ +{ + "id": 3153, + "name": "apply-operations-on-array-to-maximize-sum-of-squares", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/apply-operations-on-array-to-maximize-sum-of-squares/", + "date": "2023-10-01", + "task_description": "You are given a **0-indexed** integer array `nums` and a **positive** integer `k`. You can do the following operation on the array **any** number of times: Choose any two distinct indices `i` and `j` and **simultaneously** update the values of `nums[i]` to `(nums[i] AND nums[j])` and `nums[j]` to `(nums[i] OR nums[j])`. Here, `OR` denotes the bitwise `OR` operation, and `AND` denotes the bitwise `AND` operation. You have to choose `k` elements from the final array and calculate the sum of their **squares**. Return _the **maximum** sum of squares you can achieve_. Since the answer can be very large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** nums = [2,6,5,8], k = 2 **Output:** 261 **Explanation:** We can do the following operations on the array: - Choose i = 0 and j = 3, then change nums[0] to (2 AND 8) = 0 and nums[3] to (2 OR 8) = 10. The resulting array is nums = [0,6,5,10]. - Choose i = 2 and j = 3, then change nums[2] to (5 AND 10) = 0 and nums[3] to (5 OR 10) = 15. The resulting array is nums = [0,6,0,15]. We can choose the elements 15 and 6 from the final array. The sum of squares is 152 + 62 = 261. It can be shown that this is the maximum value we can get. ``` **Example 2:** ``` **Input:** nums = [4,5,4,7], k = 3 **Output:** 90 **Explanation:** We do not need to apply any operations. We can choose the elements 7, 5, and 4 with a sum of squares: 72 + 52 + 42 = 90. It can be shown that this is the maximum value we can get. ``` **Constraints:** `1 <= k <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,6,5,8], k = 2", + "output": "261 " + }, + { + "label": "Example 2", + "input": "nums = [4,5,4,7], k = 3", + "output": "90 " + } + ], + "constraints": [ + "Choose any two distinct indices i and j and simultaneously update the values of nums[i] to (nums[i] AND nums[j]) and nums[j] to (nums[i] OR nums[j]). Here, OR denotes the bitwise OR operation, and AND denotes the bitwise AND operation.", + "1 <= k <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxSum(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 maxSum(List nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxSum" + } +} \ No newline at end of file diff --git a/apply-operations-to-an-array.json b/apply-operations-to-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..fe31cbedebff9c51330ed9a370f0f58dd7520164 --- /dev/null +++ b/apply-operations-to-an-array.json @@ -0,0 +1,31 @@ +{ + "id": 2551, + "name": "apply-operations-to-an-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/apply-operations-to-an-array/", + "date": "2022-10-30", + "task_description": "You are given a **0-indexed** array `nums` of size `n` consisting of **non-negative** integers. You need to apply `n - 1` operations to this array where, in the `ith` operation (**0-indexed**), you will apply the following on the `ith` element of `nums`: If `nums[i] == nums[i + 1]`, then multiply `nums[i]` by `2` and set `nums[i + 1]` to `0`. Otherwise, you skip this operation. After performing **all** the operations, **shift** all the `0`'s to the **end** of the array. For example, the array `[1,0,2,0,0,1]` after shifting all its `0`'s to the end, is `[1,2,1,0,0,0]`. Return _the resulting array_. **Note** that the operations are applied **sequentially**, not all at once. **Example 1:** ``` **Input:** nums = [1,2,2,1,1,0] **Output:** [1,4,2,0,0,0] **Explanation:** We do the following operations: - i = 0: nums[0] and nums[1] are not equal, so we skip this operation. - i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,**4**,**0**,1,1,0]. - i = 2: nums[2] and nums[3] are not equal, so we skip this operation. - i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,**2**,**0**,0]. - i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,**0**,**0**]. After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0]. ``` **Example 2:** ``` **Input:** nums = [0,1] **Output:** [1,0] **Explanation:** No operation can be applied, we just shift the 0 to the end. ``` **Constraints:** `2 <= nums.length <= 2000` `0 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,2,1,1,0]", + "output": "[1,4,2,0,0,0] " + }, + { + "label": "Example 2", + "input": "nums = [0,1]", + "output": "[1,0] " + } + ], + "constraints": [ + "If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.", + "For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].", + "2 <= nums.length <= 2000", + "0 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def applyOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] applyOperations(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "applyOperations" + } +} \ No newline at end of file diff --git a/apply-operations-to-make-all-array-elements-equal-to-zero.json b/apply-operations-to-make-all-array-elements-equal-to-zero.json new file mode 100644 index 0000000000000000000000000000000000000000..3d477a503e6a34e642ec4e8f5c151e271e8d92e1 --- /dev/null +++ b/apply-operations-to-make-all-array-elements-equal-to-zero.json @@ -0,0 +1,30 @@ +{ + "id": 2878, + "name": "apply-operations-to-make-all-array-elements-equal-to-zero", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/apply-operations-to-make-all-array-elements-equal-to-zero/", + "date": "2023-07-02", + "task_description": "You are given a **0-indexed** integer array `nums` and a positive integer `k`. You can apply the following operation on the array **any** number of times: Choose **any** subarray of size `k` from the array and **decrease** all its elements by `1`. Return `true`_ if you can make all the array elements equal to _`0`_, or _`false`_ otherwise_. A **subarray** is a contiguous non-empty part of an array. **Example 1:** ``` **Input:** nums = [2,2,3,1,1,0], k = 3 **Output:** true **Explanation:** We can do the following operations: - Choose the subarray [2,2,3]. The resulting array will be nums = [**1**,**1**,**2**,1,1,0]. - Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,**1**,**0**,**0**,0]. - Choose the subarray [1,1,1]. The resulting array will be nums = [**0**,**0**,**0**,0,0,0]. ``` **Example 2:** ``` **Input:** nums = [1,3,1,1], k = 2 **Output:** false **Explanation:** It is not possible to make all the array elements equal to 0. ``` **Constraints:** `1 <= k <= nums.length <= 105` `0 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,2,3,1,1,0], k = 3", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [1,3,1,1], k = 2", + "output": "false " + } + ], + "constraints": [ + "Choose any subarray of size k from the array and decrease all its elements by 1.", + "1 <= k <= nums.length <= 105", + "0 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def checkArray(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean checkArray(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "checkArray" + } +} \ No newline at end of file diff --git a/apply-operations-to-make-string-empty.json b/apply-operations-to-make-string-empty.json new file mode 100644 index 0000000000000000000000000000000000000000..f53e32fa19d12b98e072175be97af88997393e7f --- /dev/null +++ b/apply-operations-to-make-string-empty.json @@ -0,0 +1,33 @@ +{ + "id": 3308, + "name": "apply-operations-to-make-string-empty", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/apply-operations-to-make-string-empty/", + "date": "2024-02-03", + "task_description": "You are given a string `s`. Consider performing the following operation until `s` becomes **empty**: For **every** alphabet character from `'a'` to `'z'`, remove the **first** occurrence of that character in `s` (if it exists). For example, let initially `s = \"aabcbbca\"`. We do the following operations: Remove the underlined characters `s = \"**a**a**bc**bbca\"`. The resulting string is `s = \"abbca\"`. Remove the underlined characters `s = \"**ab**b**c**a\"`. The resulting string is `s = \"ba\"`. Remove the underlined characters `s = \"**ba**\"`. The resulting string is `s = \"\"`. Return _the value of the string _`s`_ right **before** applying the **last** operation_. In the example above, answer is `\"ba\"`. **Example 1:** ``` **Input:** s = \"aabcbbca\" **Output:** \"ba\" **Explanation:** Explained in the statement. ``` **Example 2:** ``` **Input:** s = \"abcd\" **Output:** \"abcd\" **Explanation:** We do the following operation: - Remove the underlined characters s = \"**abcd**\". The resulting string is s = \"\". The string just before the last operation is \"abcd\". ``` **Constraints:** `1 <= s.length <= 5 * 105` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aabcbbca\"", + "output": "\"ba\" " + }, + { + "label": "Example 2", + "input": "s = \"abcd\"", + "output": "\"abcd\" " + } + ], + "constraints": [ + "For every alphabet character from 'a' to 'z', remove the first occurrence of that character in s (if it exists).", + "Remove the underlined characters s = \"aabcbbca\". The resulting string is s = \"abbca\".", + "Remove the underlined characters s = \"abbca\". The resulting string is s = \"ba\".", + "Remove the underlined characters s = \"ba\". The resulting string is s = \"\".", + "1 <= s.length <= 5 * 105", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def lastNonEmptyString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String lastNonEmptyString(String s) {\n \n }\n}", + "metadata": { + "func_name": "lastNonEmptyString" + } +} \ No newline at end of file diff --git a/apply-operations-to-make-two-strings-equal.json b/apply-operations-to-make-two-strings-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..31ac182258a3c41a8197f9fee3eb51b66792b947 --- /dev/null +++ b/apply-operations-to-make-two-strings-equal.json @@ -0,0 +1,32 @@ +{ + "id": 3033, + "name": "apply-operations-to-make-two-strings-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/apply-operations-to-make-two-strings-equal/", + "date": "2023-10-01", + "task_description": "You are given two **0-indexed** binary strings `s1` and `s2`, both of length `n`, and a positive integer `x`. You can perform any of the following operations on the string `s1` **any** number of times: Choose two indices `i` and `j`, and flip both `s1[i]` and `s1[j]`. The cost of this operation is `x`. Choose an index `i` such that `i < n - 1` and flip both `s1[i]` and `s1[i + 1]`. The cost of this operation is `1`. Return _the **minimum** cost needed to make the strings _`s1`_ and _`s2`_ equal, or return _`-1`_ if it is impossible._ **Note** that flipping a character means changing it from `0` to `1` or vice-versa. **Example 1:** ``` **Input:** s1 = \"1100011000\", s2 = \"0101001010\", x = 2 **Output:** 4 **Explanation:** We can do the following operations: - Choose i = 3 and apply the second operation. The resulting string is s1 = \"110**11**11000\". - Choose i = 4 and apply the second operation. The resulting string is s1 = \"1101**00**1000\". - Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = \"**0**1010010**1**0\" = s2. The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible. ``` **Example 2:** ``` **Input:** s1 = \"10110\", s2 = \"00011\", x = 4 **Output:** -1 **Explanation:** It is not possible to make the two strings equal. ``` **Constraints:** `n == s1.length == s2.length` `1 <= n, x <= 500` `s1` and `s2` consist only of the characters `'0'` and `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s1 = \"1100011000\", s2 = \"0101001010\", x = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s1 = \"10110\", s2 = \"00011\", x = 4", + "output": "-1 " + } + ], + "constraints": [ + "Choose two indices i and j, and flip both s1[i] and s1[j]. The cost of this operation is x.", + "Choose an index i such that i < n - 1 and flip both s1[i] and s1[i + 1]. The cost of this operation is 1.", + "n == s1.length == s2.length", + "1 <= n, x <= 500", + "s1 and s2 consist only of the characters '0' and '1'." + ], + "python_template": "class Solution(object):\n def minOperations(self, s1, s2, x):\n \"\"\"\n :type s1: str\n :type s2: str\n :type x: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minOperations(String s1, String s2, int x) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/apply-operations-to-maximize-frequency-score.json b/apply-operations-to-maximize-frequency-score.json new file mode 100644 index 0000000000000000000000000000000000000000..3c6a56ad54019bb31e414f599a063fced4c59ee6 --- /dev/null +++ b/apply-operations-to-maximize-frequency-score.json @@ -0,0 +1,31 @@ +{ + "id": 3196, + "name": "apply-operations-to-maximize-frequency-score", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/apply-operations-to-maximize-frequency-score/", + "date": "2023-12-10", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `k`. You can perform the following operation on the array **at most** `k` times: Choose any index `i` from the array and **increase** or **decrease** `nums[i]` by `1`. The score of the final array is the **frequency** of the most frequent element in the array. Return _the **maximum** score you can achieve_. The frequency of an element is the number of occurences of that element in the array. **Example 1:** ``` **Input:** nums = [1,2,6,4], k = 3 **Output:** 3 **Explanation:** We can do the following operations on the array: - Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2]. The element 2 is the most frequent in the final array so our score is 3. It can be shown that we cannot achieve a better score. ``` **Example 2:** ``` **Input:** nums = [1,4,4,2,4], k = 0 **Output:** 3 **Explanation:** We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `0 <= k <= 1014`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,6,4], k = 3", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,4,4,2,4], k = 0", + "output": "3 " + } + ], + "constraints": [ + "Choose any index i from the array and increase or decrease nums[i] by 1.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "0 <= k <= 1014" + ], + "python_template": "class Solution(object):\n def maxFrequencyScore(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 maxFrequencyScore(int[] nums, long k) {\n \n }\n}", + "metadata": { + "func_name": "maxFrequencyScore" + } +} \ No newline at end of file diff --git a/apply-operations-to-maximize-score.json b/apply-operations-to-maximize-score.json new file mode 100644 index 0000000000000000000000000000000000000000..783d35a5ac4f945f6042bd15a194d705157c822a --- /dev/null +++ b/apply-operations-to-maximize-score.json @@ -0,0 +1,33 @@ +{ + "id": 3001, + "name": "apply-operations-to-maximize-score", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/apply-operations-to-maximize-score/", + "date": "2023-08-06", + "task_description": "You are given an array `nums` of `n` positive integers and an integer `k`. Initially, you start with a score of `1`. You have to maximize your score by applying the following operation at most `k` times: Choose any **non-empty** subarray `nums[l, ..., r]` that you haven't chosen previously. Choose an element `x` of `nums[l, ..., r]` with the highest **prime score**. If multiple such elements exist, choose the one with the smallest index. Multiply your score by `x`. Here, `nums[l, ..., r]` denotes the subarray of `nums` starting at index `l` and ending at the index `r`, both ends being inclusive. The **prime score** of an integer `x` is equal to the number of distinct prime factors of `x`. For example, the prime score of `300` is `3` since `300 = 2 * 2 * 3 * 5 * 5`. Return _the **maximum possible score** after applying at most _`k`_ operations_. Since the answer may be large, return it modulo `109 + 7`. **Example 1:** ``` **Input:** nums = [8,3,9,3,8], k = 2 **Output:** 81 **Explanation:** To get a score of 81, we can apply the following operations: - Choose subarray nums[2, ..., 2]. nums[2] is the only element in this subarray. Hence, we multiply the score by nums[2]. The score becomes 1 * 9 = 9. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 1, but nums[2] has the smaller index. Hence, we multiply the score by nums[2]. The score becomes 9 * 9 = 81. It can be proven that 81 is the highest score one can obtain. ``` **Example 2:** ``` **Input:** nums = [19,12,14,6,10,18], k = 3 **Output:** 4788 **Explanation:** To get a score of 4788, we can apply the following operations: - Choose subarray nums[0, ..., 0]. nums[0] is the only element in this subarray. Hence, we multiply the score by nums[0]. The score becomes 1 * 19 = 19. - Choose subarray nums[5, ..., 5]. nums[5] is the only element in this subarray. Hence, we multiply the score by nums[5]. The score becomes 19 * 18 = 342. - Choose subarray nums[2, ..., 3]. Both nums[2] and nums[3] have a prime score of 2, but nums[2] has the smaller index. Hence, we multipy the score by nums[2]. The score becomes 342 * 14 = 4788. It can be proven that 4788 is the highest score one can obtain. ``` **Constraints:** `1 <= nums.length == n <= 105` `1 <= nums[i] <= 105` `1 <= k <= min(n * (n + 1) / 2, 109)`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [8,3,9,3,8], k = 2", + "output": "81 " + }, + { + "label": "Example 2", + "input": "nums = [19,12,14,6,10,18], k = 3", + "output": "4788 " + } + ], + "constraints": [ + "Choose any non-empty subarray nums[l, ..., r] that you haven't chosen previously.", + "Choose an element x of nums[l, ..., r] with the highest prime score. If multiple such elements exist, choose the one with the smallest index.", + "Multiply your score by x.", + "1 <= nums.length == n <= 105", + "1 <= nums[i] <= 105", + "1 <= k <= min(n * (n + 1) / 2, 109)" + ], + "python_template": "class Solution(object):\n def maximumScore(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 maximumScore(List nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumScore" + } +} \ No newline at end of file diff --git a/assign-elements-to-groups-with-constraints.json b/assign-elements-to-groups-with-constraints.json new file mode 100644 index 0000000000000000000000000000000000000000..f63463deb606f6f9f93b3b5fe221c2a89b64671e --- /dev/null +++ b/assign-elements-to-groups-with-constraints.json @@ -0,0 +1,45 @@ +{ + "id": 3760, + "name": "assign-elements-to-groups-with-constraints", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/assign-elements-to-groups-with-constraints/", + "date": "2025-02-02", + "task_description": "You are given an integer array `groups`, where `groups[i]` represents the size of the `ith` group. You are also given an integer array `elements`. Your task is to assign **one** element to each group based on the following rules: An element at index `j` can be assigned to a group `i` if `groups[i]` is **divisible** by `elements[j]`. If there are multiple elements that can be assigned, assign the element with the **smallest index** `j`. If no element satisfies the condition for a group, assign -1 to that group. Return an integer array `assigned`, where `assigned[i]` is the index of the element chosen for group `i`, or -1 if no suitable element exists. **Note**: An element may be assigned to more than one group. **Example 1:** **Input:** groups = [8,4,3,2,4], elements = [4,2] **Output:** [0,0,-1,1,0] **Explanation:** `elements[0] = 4` is assigned to groups 0, 1, and 4. `elements[1] = 2` is assigned to group 3. Group 2 cannot be assigned any element. **Example 2:** **Input:** groups = [2,3,5,7], elements = [5,3,3] **Output:** [-1,1,0,-1] **Explanation:** `elements[1] = 3` is assigned to group 1. `elements[0] = 5` is assigned to group 2. Groups 0 and 3 cannot be assigned any element. **Example 3:** **Input:** groups = [10,21,30,41], elements = [2,1] **Output:** [0,1,0,1] **Explanation:** `elements[0] = 2` is assigned to the groups with even values, and `elements[1] = 1` is assigned to the groups with odd values. **Constraints:** `1 <= groups.length <= 105` `1 <= elements.length <= 105` `1 <= groups[i] <= 105` `1 <= elements[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "groups = [8,4,3,2,4], elements = [4,2]", + "output": "[0,0,-1,1,0] " + }, + { + "label": "Example 2", + "input": "groups = [2,3,5,7], elements = [5,3,3]", + "output": "[-1,1,0,-1] " + }, + { + "label": "Example 3", + "input": "groups = [10,21,30,41], elements = [2,1]", + "output": "[0,1,0,1] " + } + ], + "constraints": [ + "An element at index j can be assigned to a group i if groups[i] is divisible by elements[j].", + "If there are multiple elements that can be assigned, assign the element with the smallest index j.", + "If no element satisfies the condition for a group, assign -1 to that group.", + "elements[0] = 4 is assigned to groups 0, 1, and 4.", + "elements[1] = 2 is assigned to group 3.", + "Group 2 cannot be assigned any element.", + "elements[1] = 3 is assigned to group 1.", + "elements[0] = 5 is assigned to group 2.", + "Groups 0 and 3 cannot be assigned any element.", + "1 <= groups.length <= 105", + "1 <= elements.length <= 105", + "1 <= groups[i] <= 105", + "1 <= elements[i] <= 105" + ], + "python_template": "class Solution(object):\n def assignElements(self, groups, elements):\n \"\"\"\n :type groups: List[int]\n :type elements: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] assignElements(int[] groups, int[] elements) {\n \n }\n}", + "metadata": { + "func_name": "assignElements" + } +} \ No newline at end of file diff --git a/average-value-of-even-numbers-that-are-divisible-by-three.json b/average-value-of-even-numbers-that-are-divisible-by-three.json new file mode 100644 index 0000000000000000000000000000000000000000..57ce9a3f7134f896201b3377181cde221853a41b --- /dev/null +++ b/average-value-of-even-numbers-that-are-divisible-by-three.json @@ -0,0 +1,29 @@ +{ + "id": 2542, + "name": "average-value-of-even-numbers-that-are-divisible-by-three", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/", + "date": "2022-10-23", + "task_description": "Given an integer array `nums` of **positive** integers, return _the average value of all even integers that are divisible by_ `3`. Note that the **average** of `n` elements is the **sum** of the `n` elements divided by `n` and **rounded down** to the nearest integer. **Example 1:** ``` **Input:** nums = [1,3,6,10,12,15] **Output:** 9 **Explanation:** 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9. ``` **Example 2:** ``` **Input:** nums = [1,2,4,7,10] **Output:** 0 **Explanation:** There is no single number that satisfies the requirement, so return 0. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,6,10,12,15]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,4,7,10]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def averageValue(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int averageValue(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "averageValue" + } +} \ No newline at end of file diff --git a/beautiful-towers-i.json b/beautiful-towers-i.json new file mode 100644 index 0000000000000000000000000000000000000000..09f685770fcb9eb212437c510002b1fa59a4f209 --- /dev/null +++ b/beautiful-towers-i.json @@ -0,0 +1,34 @@ +{ + "id": 3114, + "name": "beautiful-towers-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/beautiful-towers-i/", + "date": "2023-09-17", + "task_description": "You are given an array `heights` of `n` integers representing the number of bricks in `n` consecutive towers. Your task is to remove some bricks to form a **mountain-shaped** tower arrangement. In this arrangement, the tower heights are non-decreasing, reaching a maximum peak value with one or multiple consecutive towers and then non-increasing. Return the **maximum possible sum** of heights of a mountain-shaped tower arrangement. **Example 1:** **Input:** heights = [5,3,4,1,1] **Output:** 13 **Explanation:** We remove some bricks to make `heights = [5,3,3,1,1]`, the peak is at index 0. **Example 2:** **Input:** heights = [6,5,3,9,2,7] **Output:** 22 **Explanation:** We remove some bricks to make `heights = [3,3,3,9,2,2]`, the peak is at index 3. **Example 3:** **Input:** heights = [3,2,5,5,2,3] **Output:** 18 **Explanation:** We remove some bricks to make `heights = [2,2,5,5,2,2]`, the peak is at index 2 or 3. **Constraints:** `1 <= n == heights.length <= 103` `1 <= heights[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "heights = [5,3,4,1,1]", + "output": "13 " + }, + { + "label": "Example 2", + "input": "heights = [6,5,3,9,2,7]", + "output": "22 " + }, + { + "label": "Example 3", + "input": "heights = [3,2,5,5,2,3]", + "output": "18 " + } + ], + "constraints": [ + "1 <= n == heights.length <= 103", + "1 <= heights[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumSumOfHeights(self, heights):\n \"\"\"\n :type heights: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumSumOfHeights(int[] heights) {\n \n }\n}", + "metadata": { + "func_name": "maximumSumOfHeights" + } +} \ No newline at end of file diff --git a/beautiful-towers-ii.json b/beautiful-towers-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..44b17b615a1e6ed2bdcf86655c153558923626d6 --- /dev/null +++ b/beautiful-towers-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3113, + "name": "beautiful-towers-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/beautiful-towers-ii/", + "date": "2023-09-17", + "task_description": "You are given a **0-indexed** array `maxHeights` of `n` integers. You are tasked with building `n` towers in the coordinate line. The `ith` tower is built at coordinate `i` and has a height of `heights[i]`. A configuration of towers is **beautiful** if the following conditions hold: `1 <= heights[i] <= maxHeights[i]` `heights` is a **mountain** array. Array `heights` is a **mountain** if there exists an index `i` such that: For all `0 < j <= i`, `heights[j - 1] <= heights[j]` For all `i <= k < n - 1`, `heights[k + 1] <= heights[k]` Return _the **maximum possible sum of heights** of a beautiful configuration of towers_. **Example 1:** ``` **Input:** maxHeights = [5,3,4,1,1] **Output:** 13 **Explanation:** One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 0. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13. ``` **Example 2:** ``` **Input:** maxHeights = [6,5,3,9,2,7] **Output:** 22 **Explanation:** One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 3. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22. ``` **Example 3:** ``` **Input:** maxHeights = [3,2,5,5,2,3] **Output:** 18 **Explanation:** One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since: - 1 <= heights[i] <= maxHeights[i] - heights is a mountain of peak i = 2. Note that, for this configuration, i = 3 can also be considered a peak. It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18. ``` **Constraints:** `1 <= n == maxHeights.length <= 105` `1 <= maxHeights[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "maxHeights = [5,3,4,1,1]", + "output": "13 " + }, + { + "label": "Example 2", + "input": "maxHeights = [6,5,3,9,2,7]", + "output": "22 " + }, + { + "label": "Example 3", + "input": "maxHeights = [3,2,5,5,2,3]", + "output": "18 " + } + ], + "constraints": [ + "For all 0 < j <= i, heights[j - 1] <= heights[j]", + "For all i <= k < n - 1, heights[k + 1] <= heights[k]", + "1 <= n == maxHeights.length <= 105", + "1 <= maxHeights[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumSumOfHeights(self, maxHeights):\n \"\"\"\n :type maxHeights: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumSumOfHeights(List maxHeights) {\n \n }\n}", + "metadata": { + "func_name": "maximumSumOfHeights" + } +} \ No newline at end of file diff --git a/best-poker-hand.json b/best-poker-hand.json new file mode 100644 index 0000000000000000000000000000000000000000..0cd7b677ccd96c0512ac681c182c83b3bd002666 --- /dev/null +++ b/best-poker-hand.json @@ -0,0 +1,36 @@ +{ + "id": 2433, + "name": "best-poker-hand", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/best-poker-hand/", + "date": "2022-07-09", + "task_description": "You are given an integer array `ranks` and a character array `suits`. You have `5` cards where the `ith` card has a rank of `ranks[i]` and a suit of `suits[i]`. The following are the types of **poker hands** you can make from best to worst: `\"Flush\"`: Five cards of the same suit. `\"Three of a Kind\"`: Three cards of the same rank. `\"Pair\"`: Two cards of the same rank. `\"High Card\"`: Any single card. Return _a string representing the **best** type of **poker hand** you can make with the given cards._ **Note** that the return values are **case-sensitive**. **Example 1:** ``` **Input:** ranks = [13,2,3,1,9], suits = [\"a\",\"a\",\"a\",\"a\",\"a\"] **Output:** \"Flush\" **Explanation:** The hand with all the cards consists of 5 cards with the same suit, so we have a \"Flush\". ``` **Example 2:** ``` **Input:** ranks = [4,4,2,4,4], suits = [\"d\",\"a\",\"a\",\"b\",\"c\"] **Output:** \"Three of a Kind\" **Explanation:** The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a \"Three of a Kind\". Note that we could also make a \"Pair\" hand but \"Three of a Kind\" is a better hand. Also note that other cards could be used to make the \"Three of a Kind\" hand. ``` **Example 3:** ``` **Input:** ranks = [10,10,2,12,9], suits = [\"a\",\"b\",\"c\",\"a\",\"d\"] **Output:** \"Pair\" **Explanation:** The hand with the first and second card consists of 2 cards with the same rank, so we have a \"Pair\". Note that we cannot make a \"Flush\" or a \"Three of a Kind\". ``` **Constraints:** `ranks.length == suits.length == 5` `1 <= ranks[i] <= 13` `'a' <= suits[i] <= 'd'` No two cards have the same rank and suit.", + "test_case": [ + { + "label": "Example 1", + "input": "ranks = [13,2,3,1,9], suits = [\"a\",\"a\",\"a\",\"a\",\"a\"]", + "output": "\"Flush\" " + }, + { + "label": "Example 2", + "input": "ranks = [4,4,2,4,4], suits = [\"d\",\"a\",\"a\",\"b\",\"c\"]", + "output": "\"Three of a Kind\" " + }, + { + "label": "Example 3", + "input": "ranks = [10,10,2,12,9], suits = [\"a\",\"b\",\"c\",\"a\",\"d\"]", + "output": "\"Pair\" " + } + ], + "constraints": [ + "ranks.length == suits.length == 5", + "1 <= ranks[i] <= 13", + "'a' <= suits[i] <= 'd'", + "No two cards have the same rank and suit." + ], + "python_template": "class Solution(object):\n def bestHand(self, ranks, suits):\n \"\"\"\n :type ranks: List[int]\n :type suits: List[str]\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String bestHand(int[] ranks, char[] suits) {\n \n }\n}", + "metadata": { + "func_name": "bestHand" + } +} \ No newline at end of file diff --git a/bitwise-xor-of-all-pairings.json b/bitwise-xor-of-all-pairings.json new file mode 100644 index 0000000000000000000000000000000000000000..9b2f8e70fd7129a89fd19f6b0d8bcfb49452af71 --- /dev/null +++ b/bitwise-xor-of-all-pairings.json @@ -0,0 +1,29 @@ +{ + "id": 2533, + "name": "bitwise-xor-of-all-pairings", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/bitwise-xor-of-all-pairings/", + "date": "2022-09-17", + "task_description": "You are given two **0-indexed** arrays, `nums1` and `nums2`, consisting of non-negative integers. Let there be another array, `nums3`, which contains the bitwise XOR of **all pairings** of integers between `nums1` and `nums2` (every integer in `nums1` is paired with every integer in `nums2` **exactly once**). Return_ the **bitwise XOR** of all integers in _`nums3`. **Example 1:** ``` **Input:** nums1 = [2,1,3], nums2 = [10,2,5,0] **Output:** 13 **Explanation:** A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3]. The bitwise XOR of all these numbers is 13, so we return 13. ``` **Example 2:** ``` **Input:** nums1 = [1,2], nums2 = [3,4] **Output:** 0 **Explanation:** All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0], and nums1[1] ^ nums2[1]. Thus, one possible nums3 array is [2,5,1,6]. 2 ^ 5 ^ 1 ^ 6 = 0, so we return 0. ``` **Constraints:** `1 <= nums1.length, nums2.length <= 105` `0 <= nums1[i], nums2[j] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [2,1,3], nums2 = [10,2,5,0]", + "output": "13 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,2], nums2 = [3,4]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums1.length, nums2.length <= 105", + "0 <= nums1[i], nums2[j] <= 109" + ], + "python_template": "class Solution(object):\n def xorAllNums(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 xorAllNums(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "xorAllNums" + } +} \ No newline at end of file diff --git a/buy-two-chocolates.json b/buy-two-chocolates.json new file mode 100644 index 0000000000000000000000000000000000000000..ffae217e783b6d1ca34fa39412f25d626eae16a1 --- /dev/null +++ b/buy-two-chocolates.json @@ -0,0 +1,30 @@ +{ + "id": 2756, + "name": "buy-two-chocolates", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/buy-two-chocolates/", + "date": "2023-05-13", + "task_description": "You are given an integer array `prices` representing the prices of various chocolates in a store. You are also given a single integer `money`, which represents your initial amount of money. You must buy **exactly** two chocolates in such a way that you still have some **non-negative** leftover money. You would like to minimize the sum of the prices of the two chocolates you buy. Return _the amount of money you will have leftover after buying the two chocolates_. If there is no way for you to buy two chocolates without ending up in debt, return `money`. Note that the leftover must be non-negative. **Example 1:** ``` **Input:** prices = [1,2,2], money = 3 **Output:** 0 **Explanation:** Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0. ``` **Example 2:** ``` **Input:** prices = [3,2,3], money = 3 **Output:** 3 **Explanation:** You cannot buy 2 chocolates without going in debt, so we return 3. ``` **Constraints:** `2 <= prices.length <= 50` `1 <= prices[i] <= 100` `1 <= money <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "prices = [1,2,2], money = 3", + "output": "0 " + }, + { + "label": "Example 2", + "input": "prices = [3,2,3], money = 3", + "output": "3 " + } + ], + "constraints": [ + "2 <= prices.length <= 50", + "1 <= prices[i] <= 100", + "1 <= money <= 100" + ], + "python_template": "class Solution(object):\n def buyChoco(self, prices, money):\n \"\"\"\n :type prices: List[int]\n :type money: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int buyChoco(int[] prices, int money) {\n \n }\n}", + "metadata": { + "func_name": "buyChoco" + } +} \ No newline at end of file diff --git a/calculate-delayed-arrival-time.json b/calculate-delayed-arrival-time.json new file mode 100644 index 0000000000000000000000000000000000000000..0efedfd8b4ab75becb4d055297f68ae97106c080 --- /dev/null +++ b/calculate-delayed-arrival-time.json @@ -0,0 +1,29 @@ +{ + "id": 2748, + "name": "calculate-delayed-arrival-time", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/calculate-delayed-arrival-time/", + "date": "2023-04-16", + "task_description": "You are given a positive integer `arrivalTime` denoting the arrival time of a train in hours, and another positive integer `delayedTime` denoting the amount of delay in hours. Return _the time when the train will arrive at the station._ Note that the time in this problem is in 24-hours format. **Example 1:** ``` **Input:** arrivalTime = 15, delayedTime = 5 **Output:** 20 **Explanation:** Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours). ``` **Example 2:** ``` **Input:** arrivalTime = 13, delayedTime = 11 **Output:** 0 **Explanation:** Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0). ``` **Constraints:** `1 <= arrivaltime < 24` `1 <= delayedTime <= 24`", + "test_case": [ + { + "label": "Example 1", + "input": "arrivalTime = 15, delayedTime = 5", + "output": "20 " + }, + { + "label": "Example 2", + "input": "arrivalTime = 13, delayedTime = 11", + "output": "0 " + } + ], + "constraints": [ + "1 <= arrivaltime < 24", + "1 <= delayedTime <= 24" + ], + "python_template": "class Solution(object):\n def findDelayedArrivalTime(self, arrivalTime, delayedTime):\n \"\"\"\n :type arrivalTime: int\n :type delayedTime: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findDelayedArrivalTime(int arrivalTime, int delayedTime) {\n \n }\n}", + "metadata": { + "func_name": "findDelayedArrivalTime" + } +} \ No newline at end of file diff --git a/calculate-digit-sum-of-a-string.json b/calculate-digit-sum-of-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..9d6c419e9af71e1c77ae05f88d2186ac687a1792 --- /dev/null +++ b/calculate-digit-sum-of-a-string.json @@ -0,0 +1,30 @@ +{ + "id": 2361, + "name": "calculate-digit-sum-of-a-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/calculate-digit-sum-of-a-string/", + "date": "2022-04-10", + "task_description": "You are given a string `s` consisting of digits and an integer `k`. A **round** can be completed if the length of `s` is greater than `k`. In one round, do the following: **Divide** `s` into **consecutive groups** of size `k` such that the first `k` characters are in the first group, the next `k` characters are in the second group, and so on. **Note** that the size of the last group can be smaller than `k`. **Replace** each group of `s` with a string representing the sum of all its digits. For example, `\"346\"` is replaced with `\"13\"` because `3 + 4 + 6 = 13`. **Merge** consecutive groups together to form a new string. If the length of the string is greater than `k`, repeat from step `1`. Return `s` _after all rounds have been completed_. **Example 1:** ``` **Input:** s = \"11111222223\", k = 3 **Output:** \"135\" **Explanation:** - For the first round, we divide s into groups of size 3: \"111\", \"112\", \"222\", and \"23\". ​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5. So, s becomes \"3\" + \"4\" + \"6\" + \"5\" = \"3465\" after the first round. - For the second round, we divide s into \"346\" and \"5\". Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5. So, s becomes \"13\" + \"5\" = \"135\" after second round. Now, s.length <= k, so we return \"135\" as the answer. ``` **Example 2:** ``` **Input:** s = \"00000000\", k = 3 **Output:** \"000\" **Explanation:** We divide s into \"000\", \"000\", and \"00\". Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0. s becomes \"0\" + \"0\" + \"0\" = \"000\", whose length is equal to k, so we return \"000\". ``` **Constraints:** `1 <= s.length <= 100` `2 <= k <= 100` `s` consists of digits only.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"11111222223\", k = 3", + "output": "\"135\" " + }, + { + "label": "Example 2", + "input": "s = \"00000000\", k = 3", + "output": "\"000\" " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "2 <= k <= 100", + "s consists of digits only." + ], + "python_template": "class Solution(object):\n def digitSum(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String digitSum(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "digitSum" + } +} \ No newline at end of file diff --git a/categorize-box-according-to-criteria.json b/categorize-box-according-to-criteria.json new file mode 100644 index 0000000000000000000000000000000000000000..b9923eb8ea52126452971cf610076cddecceec24 --- /dev/null +++ b/categorize-box-according-to-criteria.json @@ -0,0 +1,39 @@ +{ + "id": 2619, + "name": "categorize-box-according-to-criteria", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/categorize-box-according-to-criteria/", + "date": "2022-12-24", + "task_description": "Given four integers `length`, `width`, `height`, and `mass`, representing the dimensions and mass of a box, respectively, return _a string representing the **category** of the box_. The box is `\"Bulky\"` if: **Any** of the dimensions of the box is greater or equal to `104`. Or, the **volume** of the box is greater or equal to `109`. If the mass of the box is greater or equal to `100`, it is `\"Heavy\".` If the box is both `\"Bulky\"` and `\"Heavy\"`, then its category is `\"Both\"`. If the box is neither `\"Bulky\"` nor `\"Heavy\"`, then its category is `\"Neither\"`. If the box is `\"Bulky\"` but not `\"Heavy\"`, then its category is `\"Bulky\"`. If the box is `\"Heavy\"` but not `\"Bulky\"`, then its category is `\"Heavy\"`. **Note** that the volume of the box is the product of its length, width and height. **Example 1:** ``` **Input:** length = 1000, width = 35, height = 700, mass = 300 **Output:** \"Heavy\" **Explanation:** None of the dimensions of the box is greater or equal to 104. Its volume = 24500000 <= 109. So it cannot be categorized as \"Bulky\". However mass >= 100, so the box is \"Heavy\". Since the box is not \"Bulky\" but \"Heavy\", we return \"Heavy\". ``` **Example 2:** ``` **Input:** length = 200, width = 50, height = 800, mass = 50 **Output:** \"Neither\" **Explanation:** None of the dimensions of the box is greater or equal to 104. Its volume = 8 * 106 <= 109. So it cannot be categorized as \"Bulky\". Its mass is also less than 100, so it cannot be categorized as \"Heavy\" either. Since its neither of the two above categories, we return \"Neither\". ``` **Constraints:** `1 <= length, width, height <= 105` `1 <= mass <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "length = 1000, width = 35, height = 700, mass = 300", + "output": "\"Heavy\" " + }, + { + "label": "Example 2", + "input": "length = 200, width = 50, height = 800, mass = 50", + "output": "\"Neither\" " + } + ], + "constraints": [ + "The box is \"Bulky\" if:\n\n\t\nAny of the dimensions of the box is greater or equal to 104.\nOr, the volume of the box is greater or equal to 109.", + "Any of the dimensions of the box is greater or equal to 104.", + "Or, the volume of the box is greater or equal to 109.", + "If the mass of the box is greater or equal to 100, it is \"Heavy\".", + "If the box is both \"Bulky\" and \"Heavy\", then its category is \"Both\".", + "If the box is neither \"Bulky\" nor \"Heavy\", then its category is \"Neither\".", + "If the box is \"Bulky\" but not \"Heavy\", then its category is \"Bulky\".", + "If the box is \"Heavy\" but not \"Bulky\", then its category is \"Heavy\".", + "Any of the dimensions of the box is greater or equal to 104.", + "Or, the volume of the box is greater or equal to 109.", + "1 <= length, width, height <= 105", + "1 <= mass <= 103" + ], + "python_template": "class Solution(object):\n def categorizeBox(self, length, width, height, mass):\n \"\"\"\n :type length: int\n :type width: int\n :type height: int\n :type mass: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String categorizeBox(int length, int width, int height, int mass) {\n \n }\n}", + "metadata": { + "func_name": "categorizeBox" + } +} \ No newline at end of file diff --git a/cells-in-a-range-on-an-excel-sheet.json b/cells-in-a-range-on-an-excel-sheet.json new file mode 100644 index 0000000000000000000000000000000000000000..082c5532495da5896375ad964ae7ba2641c210e7 --- /dev/null +++ b/cells-in-a-range-on-an-excel-sheet.json @@ -0,0 +1,35 @@ +{ + "id": 2304, + "name": "cells-in-a-range-on-an-excel-sheet", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/", + "date": "2022-02-27", + "task_description": "A cell `(r, c)` of an excel sheet is represented as a string `\"\"` where: `` denotes the column number `c` of the cell. It is represented by **alphabetical letters**. For example, the `1st` column is denoted by `'A'`, the `2nd` by `'B'`, the `3rd` by `'C'`, and so on. `` is the row number `r` of the cell. The `rth` row is represented by the **integer** `r`. You are given a string `s` in the format `\":\"`, where `` represents the column `c1`, `` represents the row `r1`, `` represents the column `c2`, and `` represents the row `r2`, such that `r1 <= r2` and `c1 <= c2`. Return _the **list of cells**_ `(x, y)` _such that_ `r1 <= x <= r2` _and_ `c1 <= y <= c2`. The cells should be represented as **strings** in the format mentioned above and be sorted in **non-decreasing** order first by columns and then by rows. **Example 1:** ``` **Input:** s = \"K1:L2\" **Output:** [\"K1\",\"K2\",\"L1\",\"L2\"] **Explanation:** The above diagram shows the cells which should be present in the list. The red arrows denote the order in which the cells should be presented. ``` **Example 2:** ``` **Input:** s = \"A1:F1\" **Output:** [\"A1\",\"B1\",\"C1\",\"D1\",\"E1\",\"F1\"] **Explanation:** The above diagram shows the cells which should be present in the list. The red arrow denotes the order in which the cells should be presented. ``` **Constraints:** `s.length == 5` `'A' <= s[0] <= s[3] <= 'Z'` `'1' <= s[1] <= s[4] <= '9'` `s` consists of uppercase English letters, digits and `':'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"K1:L2\"", + "output": "[\"K1\",\"K2\",\"L1\",\"L2\"] " + }, + { + "label": "Example 2", + "input": "s = \"A1:F1\"", + "output": "[\"A1\",\"B1\",\"C1\",\"D1\",\"E1\",\"F1\"] " + } + ], + "constraints": [ + " denotes the column number c of the cell. It is represented by alphabetical letters.\n\n\t\nFor example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.", + "For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.", + " is the row number r of the cell. The rth row is represented by the integer r.", + "For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.", + "s.length == 5", + "'A' <= s[0] <= s[3] <= 'Z'", + "'1' <= s[1] <= s[4] <= '9'", + "s consists of uppercase English letters, digits and ':'." + ], + "python_template": "class Solution(object):\n def cellsInRange(self, s):\n \"\"\"\n :type s: str\n :rtype: List[str]\n \"\"\"\n ", + "java_template": "class Solution {\n public List cellsInRange(String s) {\n \n }\n}", + "metadata": { + "func_name": "cellsInRange" + } +} \ No newline at end of file diff --git a/check-distances-between-same-letters.json b/check-distances-between-same-letters.json new file mode 100644 index 0000000000000000000000000000000000000000..a0faf653b4fe1eec0469eceec72e60547062514e --- /dev/null +++ b/check-distances-between-same-letters.json @@ -0,0 +1,32 @@ +{ + "id": 2476, + "name": "check-distances-between-same-letters", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-distances-between-same-letters/", + "date": "2022-08-28", + "task_description": "You are given a **0-indexed** string `s` consisting of only lowercase English letters, where each letter in `s` appears **exactly** **twice**. You are also given a **0-indexed** integer array `distance` of length `26`. Each letter in the alphabet is numbered from `0` to `25` (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, ... , `'z' -> 25`). In a **well-spaced** string, the number of letters between the two occurrences of the `ith` letter is `distance[i]`. If the `ith` letter does not appear in `s`, then `distance[i]` can be **ignored**. Return `true`_ if _`s`_ is a **well-spaced** string, otherwise return _`false`. **Example 1:** ``` **Input:** s = \"abaccb\", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] **Output:** true **Explanation:** - 'a' appears at indices 0 and 2 so it satisfies distance[0] = 1. - 'b' appears at indices 1 and 5 so it satisfies distance[1] = 3. - 'c' appears at indices 3 and 4 so it satisfies distance[2] = 0. Note that distance[3] = 5, but since 'd' does not appear in s, it can be ignored. Return true because s is a well-spaced string. ``` **Example 2:** ``` **Input:** s = \"aa\", distance = [1,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] **Output:** false **Explanation:** - 'a' appears at indices 0 and 1 so there are zero letters between them. Because distance[0] = 1, s is not a well-spaced string. ``` **Constraints:** `2 <= s.length <= 52` `s` consists only of lowercase English letters. Each letter appears in `s` exactly twice. `distance.length == 26` `0 <= distance[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abaccb\", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"aa\", distance = [1,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]", + "output": "false " + } + ], + "constraints": [ + "2 <= s.length <= 52", + "s consists only of lowercase English letters.", + "Each letter appears in s exactly twice.", + "distance.length == 26", + "0 <= distance[i] <= 50" + ], + "python_template": "class Solution(object):\n def checkDistances(self, s, distance):\n \"\"\"\n :type s: str\n :type distance: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean checkDistances(String s, int[] distance) {\n \n }\n}", + "metadata": { + "func_name": "checkDistances" + } +} \ No newline at end of file diff --git a/check-if-array-is-good.json b/check-if-array-is-good.json new file mode 100644 index 0000000000000000000000000000000000000000..a75f3cef80291b49c94ea21335fce57bafb7b8b0 --- /dev/null +++ b/check-if-array-is-good.json @@ -0,0 +1,39 @@ +{ + "id": 2892, + "name": "check-if-array-is-good", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-array-is-good/", + "date": "2023-07-08", + "task_description": "You are given an integer array `nums`. We consider an array **good **if it is a permutation of an array `base[n]`. `base[n] = [1, 2, ..., n - 1, n, n] `(in other words, it is an array of length `n + 1` which contains `1` to `n - 1 `exactly once, plus two occurrences of `n`). For example, `base[1] = [1, 1]` and` base[3] = [1, 2, 3, 3]`. Return `true` _if the given array is good, otherwise return__ _`false`. **Note: **A permutation of integers represents an arrangement of these numbers. **Example 1:** ``` **Input:** nums = [2, 1, 3] **Output:** false **Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false. ``` **Example 2:** ``` **Input:** nums = [1, 3, 3, 2] **Output:** true **Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true. ``` **Example 3:** ``` **Input:** nums = [1, 1] **Output:** true **Explanation:** Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true. ``` **Example 4:** ``` **Input:** nums = [3, 4, 4, 1, 2, 1] **Output:** false **Explanation:** Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= num[i] <= 200`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2, 1, 3]", + "output": "false " + }, + { + "label": "Example 2", + "input": "nums = [1, 3, 3, 2]", + "output": "true " + }, + { + "label": "Example 3", + "input": "nums = [1, 1]", + "output": "true " + }, + { + "label": "Example 4", + "input": "nums = [3, 4, 4, 1, 2, 1]", + "output": "false " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "1 <= num[i] <= 200" + ], + "python_template": "class Solution(object):\n def isGood(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isGood(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "isGood" + } +} \ No newline at end of file diff --git a/check-if-bitwise-or-has-trailing-zeros.json b/check-if-bitwise-or-has-trailing-zeros.json new file mode 100644 index 0000000000000000000000000000000000000000..1e4cd32311720a13361d968f1f73e1683759f972 --- /dev/null +++ b/check-if-bitwise-or-has-trailing-zeros.json @@ -0,0 +1,34 @@ +{ + "id": 3246, + "name": "check-if-bitwise-or-has-trailing-zeros", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-bitwise-or-has-trailing-zeros/", + "date": "2023-12-24", + "task_description": "You are given an array of **positive** integers `nums`. You have to check if it is possible to select **two or more** elements in the array such that the bitwise `OR` of the selected elements has **at least **one trailing zero in its binary representation. For example, the binary representation of `5`, which is `\"101\"`, does not have any trailing zeros, whereas the binary representation of `4`, which is `\"100\"`, has two trailing zeros. Return `true` _if it is possible to select two or more elements whose bitwise_ `OR` _has trailing zeros, return_ `false` _otherwise_. **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** true **Explanation:** If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation \"110\" with one trailing zero. ``` **Example 2:** ``` **Input:** nums = [2,4,8,16] **Output:** true **Explanation: **If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation \"110\" with one trailing zero. Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16). ``` **Example 3:** ``` **Input:** nums = [1,3,5,7,9] **Output:** false **Explanation:** There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR. ``` **Constraints:** `2 <= nums.length <= 100` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [2,4,8,16]", + "output": "true " + }, + { + "label": "Example 3", + "input": "nums = [1,3,5,7,9]", + "output": "false " + } + ], + "constraints": [ + "2 <= nums.length <= 100", + "1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def hasTrailingZeros(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean hasTrailingZeros(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "hasTrailingZeros" + } +} \ No newline at end of file diff --git a/check-if-digits-are-equal-in-string-after-operations-i.json b/check-if-digits-are-equal-in-string-after-operations-i.json new file mode 100644 index 0000000000000000000000000000000000000000..bc4b41568375fafbedb58854563b1160fb019a0d --- /dev/null +++ b/check-if-digits-are-equal-in-string-after-operations-i.json @@ -0,0 +1,54 @@ +{ + "id": 3768, + "name": "check-if-digits-are-equal-in-string-after-operations-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-digits-are-equal-in-string-after-operations-i/", + "date": "2025-02-16", + "task_description": "You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits: For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10. Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed. Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`. **Example 1:** **Input:** s = \"3902\" **Output:** true **Explanation:** Initially, `s = \"3902\"` First operation: `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2` `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9` `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2` `s` becomes `\"292\"` Second operation: `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1` `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1` `s` becomes `\"11\"` Since the digits in `\"11\"` are the same, the output is `true`. **Example 2:** **Input:** s = \"34789\" **Output:** false **Explanation:** Initially, `s = \"34789\"`. After the first operation, `s = \"7157\"`. After the second operation, `s = \"862\"`. After the third operation, `s = \"48\"`. Since `'4' != '8'`, the output is `false`. **Constraints:** `3 <= s.length <= 100` `s` consists of only digits.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"3902\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"34789\"", + "output": "false " + } + ], + "constraints": [ + "For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.", + "Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed.", + "Initially, s = \"3902\"", + "First operation:\n\t\n(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2\n(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9\n(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2\ns becomes \"292\"", + "(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2", + "(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9", + "(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2", + "s becomes \"292\"", + "Second operation:\n\t\n(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1\n(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1\ns becomes \"11\"", + "(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1", + "(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1", + "s becomes \"11\"", + "Since the digits in \"11\" are the same, the output is true.", + "(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2", + "(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9", + "(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2", + "s becomes \"292\"", + "(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1", + "(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1", + "s becomes \"11\"", + "Initially, s = \"34789\".", + "After the first operation, s = \"7157\".", + "After the second operation, s = \"862\".", + "After the third operation, s = \"48\".", + "Since '4' != '8', the output is false.", + "3 <= s.length <= 100", + "s consists of only digits." + ], + "python_template": "class Solution(object):\n def hasSameDigits(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean hasSameDigits(String s) {\n \n }\n}", + "metadata": { + "func_name": "hasSameDigits" + } +} \ No newline at end of file diff --git a/check-if-digits-are-equal-in-string-after-operations-ii.json b/check-if-digits-are-equal-in-string-after-operations-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..c5979a4a4b6838a99d251bdf1661abe932ecfb13 --- /dev/null +++ b/check-if-digits-are-equal-in-string-after-operations-ii.json @@ -0,0 +1,54 @@ +{ + "id": 3774, + "name": "check-if-digits-are-equal-in-string-after-operations-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/check-if-digits-are-equal-in-string-after-operations-ii/", + "date": "2025-02-16", + "task_description": "You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits: For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10. Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed. Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`. **Example 1:** **Input:** s = \"3902\" **Output:** true **Explanation:** Initially, `s = \"3902\"` First operation: `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2` `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9` `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2` `s` becomes `\"292\"` Second operation: `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1` `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1` `s` becomes `\"11\"` Since the digits in `\"11\"` are the same, the output is `true`. **Example 2:** **Input:** s = \"34789\" **Output:** false **Explanation:** Initially, `s = \"34789\"`. After the first operation, `s = \"7157\"`. After the second operation, `s = \"862\"`. After the third operation, `s = \"48\"`. Since `'4' != '8'`, the output is `false`. **Constraints:** `3 <= s.length <= 105` `s` consists of only digits.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"3902\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"34789\"", + "output": "false " + } + ], + "constraints": [ + "For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.", + "Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed.", + "Initially, s = \"3902\"", + "First operation:\n\t\n(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2\n(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9\n(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2\ns becomes \"292\"", + "(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2", + "(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9", + "(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2", + "s becomes \"292\"", + "Second operation:\n\t\n(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1\n(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1\ns becomes \"11\"", + "(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1", + "(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1", + "s becomes \"11\"", + "Since the digits in \"11\" are the same, the output is true.", + "(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2", + "(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9", + "(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2", + "s becomes \"292\"", + "(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1", + "(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1", + "s becomes \"11\"", + "Initially, s = \"34789\".", + "After the first operation, s = \"7157\".", + "After the second operation, s = \"862\".", + "After the third operation, s = \"48\".", + "Since '4' != '8', the output is false.", + "3 <= s.length <= 105", + "s consists of only digits." + ], + "python_template": "class Solution(object):\n def hasSameDigits(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean hasSameDigits(String s) {\n \n }\n}", + "metadata": { + "func_name": "hasSameDigits" + } +} \ No newline at end of file diff --git a/check-if-every-row-and-column-contains-all-numbers.json b/check-if-every-row-and-column-contains-all-numbers.json new file mode 100644 index 0000000000000000000000000000000000000000..bed183109ac85928ee424781cf919864ab5d8e60 --- /dev/null +++ b/check-if-every-row-and-column-contains-all-numbers.json @@ -0,0 +1,30 @@ +{ + "id": 2254, + "name": "check-if-every-row-and-column-contains-all-numbers", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/", + "date": "2022-01-02", + "task_description": "An `n x n` matrix is **valid** if every row and every column contains **all** the integers from `1` to `n` (**inclusive**). Given an `n x n` integer matrix `matrix`, return `true` _if the matrix is **valid**._ Otherwise, return `false`. **Example 1:** ``` **Input:** matrix = [[1,2,3],[3,1,2],[2,3,1]] **Output:** true **Explanation:** In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true. ``` **Example 2:** ``` **Input:** matrix = [[1,1,1],[1,2,3],[1,2,3]] **Output:** false **Explanation:** In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false. ``` **Constraints:** `n == matrix.length == matrix[i].length` `1 <= n <= 100` `1 <= matrix[i][j] <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "matrix = [[1,2,3],[3,1,2],[2,3,1]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "matrix = [[1,1,1],[1,2,3],[1,2,3]]", + "output": "false " + } + ], + "constraints": [ + "n == matrix.length == matrix[i].length", + "1 <= n <= 100", + "1 <= matrix[i][j] <= n" + ], + "python_template": "class Solution(object):\n def checkValid(self, matrix):\n \"\"\"\n :type matrix: List[List[int]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean checkValid(int[][] matrix) {\n \n }\n}", + "metadata": { + "func_name": "checkValid" + } +} \ No newline at end of file diff --git a/check-if-grid-can-be-cut-into-sections.json b/check-if-grid-can-be-cut-into-sections.json new file mode 100644 index 0000000000000000000000000000000000000000..10442f381ff1d8bc01bf8f6df4a2ed2cddf25654 --- /dev/null +++ b/check-if-grid-can-be-cut-into-sections.json @@ -0,0 +1,41 @@ +{ + "id": 3657, + "name": "check-if-grid-can-be-cut-into-sections", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/check-if-grid-can-be-cut-into-sections/", + "date": "2024-12-07", + "task_description": "You are given an integer `n` representing the dimensions of an `n x n` grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates `rectangles`, where `rectangles[i]` is in the form `[startx, starty, endx, endy]`, representing a rectangle on the grid. Each rectangle is defined as follows: `(startx, starty)`: The bottom-left corner of the rectangle. `(endx, endy)`: The top-right corner of the rectangle. **Note **that the rectangles do not overlap. Your task is to determine if it is possible to make **either two horizontal or two vertical cuts** on the grid such that: Each of the three resulting sections formed by the cuts contains **at least** one rectangle. Every rectangle belongs to **exactly** one section. Return `true` if such cuts can be made; otherwise, return `false`. **Example 1:** **Input:** n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]] **Output:** true **Explanation:** The grid is shown in the diagram. We can make horizontal cuts at `y = 2` and `y = 4`. Hence, output is true. **Example 2:** **Input:** n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]] **Output:** true **Explanation:** We can make vertical cuts at `x = 2` and `x = 3`. Hence, output is true. **Example 3:** **Input:** n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]] **Output:** false **Explanation:** We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false. **Constraints:** `3 <= n <= 109` `3 <= rectangles.length <= 105` `0 <= rectangles[i][0] < rectangles[i][2] <= n` `0 <= rectangles[i][1] < rectangles[i][3] <= n` No two rectangles overlap.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]", + "output": "true " + }, + { + "label": "Example 3", + "input": "n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]", + "output": "false " + } + ], + "constraints": [ + "(startx, starty): The bottom-left corner of the rectangle.", + "(endx, endy): The top-right corner of the rectangle.", + "Each of the three resulting sections formed by the cuts contains at least one rectangle.", + "Every rectangle belongs to exactly one section.", + "3 <= n <= 109", + "3 <= rectangles.length <= 105", + "0 <= rectangles[i][0] < rectangles[i][2] <= n", + "0 <= rectangles[i][1] < rectangles[i][3] <= n", + "No two rectangles overlap." + ], + "python_template": "class Solution(object):\n def checkValidCuts(self, n, rectangles):\n \"\"\"\n :type n: int\n :type rectangles: List[List[int]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean checkValidCuts(int n, int[][] rectangles) {\n \n }\n}", + "metadata": { + "func_name": "checkValidCuts" + } +} \ No newline at end of file diff --git a/check-if-grid-satisfies-conditions.json b/check-if-grid-satisfies-conditions.json new file mode 100644 index 0000000000000000000000000000000000000000..30f0a48bc83f9ff8857b9ee530a576b954a36484 --- /dev/null +++ b/check-if-grid-satisfies-conditions.json @@ -0,0 +1,36 @@ +{ + "id": 3415, + "name": "check-if-grid-satisfies-conditions", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-grid-satisfies-conditions/", + "date": "2024-04-27", + "task_description": "You are given a 2D matrix `grid` of size `m x n`. You need to check if each cell `grid[i][j]` is: Equal to the cell below it, i.e. `grid[i][j] == grid[i + 1][j]` (if it exists). Different from the cell to its right, i.e. `grid[i][j] != grid[i][j + 1]` (if it exists). Return `true` if **all** the cells satisfy these conditions, otherwise, return `false`. **Example 1:** **Input:** grid = [[1,0,2],[1,0,2]] **Output:** true **Explanation:** **** All the cells in the grid satisfy the conditions. **Example 2:** **Input:** grid = [[1,1,1],[0,0,0]] **Output:** false **Explanation:** **** All cells in the first row are equal. **Example 3:** **Input:** grid = [[1],[2],[3]] **Output:** false **Explanation:** Cells in the first column have different values. **Constraints:** `1 <= n, m <= 10` `0 <= grid[i][j] <= 9`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,0,2],[1,0,2]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "grid = [[1,1,1],[0,0,0]]", + "output": "false " + }, + { + "label": "Example 3", + "input": "grid = [[1],[2],[3]]", + "output": "false " + } + ], + "constraints": [ + "Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] (if it exists).", + "Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] (if it exists).", + "1 <= n, m <= 10", + "0 <= grid[i][j] <= 9" + ], + "python_template": "class Solution(object):\n def satisfiesConditions(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean satisfiesConditions(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "satisfiesConditions" + } +} \ No newline at end of file diff --git a/check-if-it-is-possible-to-split-array.json b/check-if-it-is-possible-to-split-array.json new file mode 100644 index 0000000000000000000000000000000000000000..e0a875fab4c38627cb71f7a58304fb8e52594535 --- /dev/null +++ b/check-if-it-is-possible-to-split-array.json @@ -0,0 +1,45 @@ +{ + "id": 2916, + "name": "check-if-it-is-possible-to-split-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/check-if-it-is-possible-to-split-array/", + "date": "2023-07-30", + "task_description": "You are given an array `nums` of length `n` and an integer `m`. You need to determine if it is possible to split the array into `n` arrays of size 1 by performing a series of steps. An array is called **good** if: The length of the array is **one**, or The sum of the elements of the array is **greater than or equal** to `m`. In each step, you can select an existing array (which may be the result of previous steps) with a length of **at least two** and split it into **two **arrays, if both resulting arrays are good. Return true if you can split the given array into `n` arrays, otherwise return false. **Example 1:** **Input:** nums = [2, 2, 1], m = 4 **Output:** true **Explanation:** Split `[2, 2, 1]` to `[2, 2]` and `[1]`. The array `[1]` has a length of one, and the array `[2, 2]` has the sum of its elements equal to `4 >= m`, so both are good arrays. Split `[2, 2]` to `[2]` and `[2]`. both arrays have the length of one, so both are good arrays. **Example 2:** **Input:** nums = [2, 1, 3], m = 5 **Output:** false **Explanation:** The first move has to be either of the following: Split `[2, 1, 3]` to `[2, 1]` and `[3]`. The array `[2, 1]` has neither length of one nor sum of elements greater than or equal to `m`. Split `[2, 1, 3]` to `[2]` and `[1, 3]`. The array `[1, 3]` has neither length of one nor sum of elements greater than or equal to `m`. So as both moves are invalid (they do not divide the array into two good arrays), we are unable to split `nums` into `n` arrays of size 1. **Example 3:** **Input:** nums = [2, 3, 3, 2, 3], m = 6 **Output:** true **Explanation:** Split `[2, 3, 3, 2, 3]` to `[2]` and `[3, 3, 2, 3]`. Split `[3, 3, 2, 3]` to `[3, 3, 2]` and `[3]`. Split `[3, 3, 2]` to `[3, 3]` and `[2]`. Split `[3, 3]` to `[3]` and `[3]`. **Constraints:** `1 <= n == nums.length <= 100` `1 <= nums[i] <= 100` `1 <= m <= 200`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2, 2, 1], m = 4", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [2, 1, 3], m = 5", + "output": "false " + }, + { + "label": "Example 3", + "input": "nums = [2, 3, 3, 2, 3], m = 6", + "output": "true " + } + ], + "constraints": [ + "The length of the array is one, or", + "The sum of the elements of the array is greater than or equal to m.", + "Split [2, 2, 1] to [2, 2] and [1]. The array [1] has a length of one, and the array [2, 2] has the sum of its elements equal to 4 >= m, so both are good arrays.", + "Split [2, 2] to [2] and [2]. both arrays have the length of one, so both are good arrays.", + "Split [2, 1, 3] to [2, 1] and [3]. The array [2, 1] has neither length of one nor sum of elements greater than or equal to m.", + "Split [2, 1, 3] to [2] and [1, 3]. The array [1, 3] has neither length of one nor sum of elements greater than or equal to m.", + "Split [2, 3, 3, 2, 3] to [2] and [3, 3, 2, 3].", + "Split [3, 3, 2, 3] to [3, 3, 2] and [3].", + "Split [3, 3, 2] to [3, 3] and [2].", + "Split [3, 3] to [3] and [3].", + "1 <= n == nums.length <= 100", + "1 <= nums[i] <= 100", + "1 <= m <= 200" + ], + "python_template": "class Solution(object):\n def canSplitArray(self, nums, m):\n \"\"\"\n :type nums: List[int]\n :type m: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean canSplitArray(List nums, int m) {\n \n }\n}", + "metadata": { + "func_name": "canSplitArray" + } +} \ No newline at end of file diff --git a/check-if-matrix-is-x-matrix.json b/check-if-matrix-is-x-matrix.json new file mode 100644 index 0000000000000000000000000000000000000000..b90a0e9ae3a883221efb3b79fe7d6653dcf2cb5b --- /dev/null +++ b/check-if-matrix-is-x-matrix.json @@ -0,0 +1,30 @@ +{ + "id": 2398, + "name": "check-if-matrix-is-x-matrix", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-matrix-is-x-matrix/", + "date": "2022-06-19", + "task_description": "A square matrix is said to be an **X-Matrix** if **both** of the following conditions hold: All the elements in the diagonals of the matrix are **non-zero**. All other elements are 0. Given a 2D integer array `grid` of size `n x n` representing a square matrix, return `true`_ if _`grid`_ is an X-Matrix_. Otherwise, return `false`. **Example 1:** ``` **Input:** grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]] **Output:** true **Explanation:** Refer to the diagram above. An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0. Thus, grid is an X-Matrix. ``` **Example 2:** ``` **Input:** grid = [[5,7,0],[0,3,1],[0,5,0]] **Output:** false **Explanation:** Refer to the diagram above. An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0. Thus, grid is not an X-Matrix. ``` **Constraints:** `n == grid.length == grid[i].length` `3 <= n <= 100` `0 <= grid[i][j] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "grid = [[5,7,0],[0,3,1],[0,5,0]]", + "output": "false " + } + ], + "constraints": [ + "n == grid.length == grid[i].length", + "3 <= n <= 100", + "0 <= grid[i][j] <= 105" + ], + "python_template": "class Solution(object):\n def checkXMatrix(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean checkXMatrix(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "checkXMatrix" + } +} \ No newline at end of file diff --git a/check-if-number-has-equal-digit-count-and-digit-value.json b/check-if-number-has-equal-digit-count-and-digit-value.json new file mode 100644 index 0000000000000000000000000000000000000000..3f1bf0bde13685b9c60f5a9e403c24018917f4cf --- /dev/null +++ b/check-if-number-has-equal-digit-count-and-digit-value.json @@ -0,0 +1,30 @@ +{ + "id": 2377, + "name": "check-if-number-has-equal-digit-count-and-digit-value", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/", + "date": "2022-05-14", + "task_description": "You are given a **0-indexed** string `num` of length `n` consisting of digits. Return `true` _if for **every** index _`i`_ in the range _`0 <= i < n`_, the digit _`i`_ occurs _`num[i]`_ times in _`num`_, otherwise return _`false`. **Example 1:** ``` **Input:** num = \"1210\" **Output:** true **Explanation:** num[0] = '1'. The digit 0 occurs once in num. num[1] = '2'. The digit 1 occurs twice in num. num[2] = '1'. The digit 2 occurs once in num. num[3] = '0'. The digit 3 occurs zero times in num. The condition holds true for every index in \"1210\", so return true. ``` **Example 2:** ``` **Input:** num = \"030\" **Output:** false **Explanation:** num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num. num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num. num[2] = '0'. The digit 2 occurs zero times in num. The indices 0 and 1 both violate the condition, so return false. ``` **Constraints:** `n == num.length` `1 <= n <= 10` `num` consists of digits.", + "test_case": [ + { + "label": "Example 1", + "input": "num = \"1210\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "num = \"030\"", + "output": "false " + } + ], + "constraints": [ + "n == num.length", + "1 <= n <= 10", + "num consists of digits." + ], + "python_template": "class Solution(object):\n def digitCount(self, num):\n \"\"\"\n :type num: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean digitCount(String num) {\n \n }\n}", + "metadata": { + "func_name": "digitCount" + } +} \ No newline at end of file diff --git a/check-if-point-is-reachable.json b/check-if-point-is-reachable.json new file mode 100644 index 0000000000000000000000000000000000000000..fb0f9db8b91a292260b13c9e56088a247291dc60 --- /dev/null +++ b/check-if-point-is-reachable.json @@ -0,0 +1,32 @@ +{ + "id": 2635, + "name": "check-if-point-is-reachable", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/check-if-point-is-reachable/", + "date": "2023-01-07", + "task_description": "There exists an infinitely large grid. You are currently at point `(1, 1)`, and you need to reach the point `(targetX, targetY)` using a finite number of steps. In one **step**, you can move from point `(x, y)` to any one of the following points: `(x, y - x)` `(x - y, y)` `(2 * x, y)` `(x, 2 * y)` Given two integers `targetX` and `targetY` representing the X-coordinate and Y-coordinate of your final position, return `true` _if you can reach the point from_ `(1, 1)` _using some number of steps, and _`false`_ otherwise_. **Example 1:** ``` **Input:** targetX = 6, targetY = 9 **Output:** false **Explanation:** It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned. ``` **Example 2:** ``` **Input:** targetX = 4, targetY = 7 **Output:** true **Explanation:** You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7). ``` **Constraints:** `1 <= targetX, targetY <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "targetX = 6, targetY = 9", + "output": "false " + }, + { + "label": "Example 2", + "input": "targetX = 4, targetY = 7", + "output": "true " + } + ], + "constraints": [ + "(x, y - x)", + "(x - y, y)", + "(2 * x, y)", + "(x, 2 * y)", + "1 <= targetX, targetY <= 109" + ], + "python_template": "class Solution(object):\n def isReachable(self, targetX, targetY):\n \"\"\"\n :type targetX: int\n :type targetY: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isReachable(int targetX, int targetY) {\n \n }\n}", + "metadata": { + "func_name": "isReachable" + } +} \ No newline at end of file diff --git a/check-if-strings-can-be-made-equal-with-operations-i.json b/check-if-strings-can-be-made-equal-with-operations-i.json new file mode 100644 index 0000000000000000000000000000000000000000..7cf57854ea7d95c85ec06cd93f74a42c596a6f57 --- /dev/null +++ b/check-if-strings-can-be-made-equal-with-operations-i.json @@ -0,0 +1,30 @@ +{ + "id": 2999, + "name": "check-if-strings-can-be-made-equal-with-operations-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i/", + "date": "2023-08-19", + "task_description": "You are given two strings `s1` and `s2`, both of length `4`, consisting of **lowercase** English letters. You can apply the following operation on any of the two strings **any** number of times: Choose any two indices `i` and `j` such that `j - i = 2`, then **swap** the two characters at those indices in the string. Return `true`_ if you can make the strings _`s1`_ and _`s2`_ equal, and _`false`_ otherwise_. **Example 1:** ``` **Input:** s1 = \"abcd\", s2 = \"cdab\" **Output:** true **Explanation:** We can do the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = \"cbad\". - Choose the indices i = 1, j = 3. The resulting string is s1 = \"cdab\" = s2. ``` **Example 2:** ``` **Input:** s1 = \"abcd\", s2 = \"dacb\" **Output:** false **Explanation:** It is not possible to make the two strings equal. ``` **Constraints:** `s1.length == s2.length == 4` `s1` and `s2` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s1 = \"abcd\", s2 = \"cdab\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s1 = \"abcd\", s2 = \"dacb\"", + "output": "false " + } + ], + "constraints": [ + "Choose any two indices i and j such that j - i = 2, then swap the two characters at those indices in the string.", + "s1.length == s2.length == 4", + "s1 and s2 consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def canBeEqual(self, s1, s2):\n \"\"\"\n :type s1: str\n :type s2: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean canBeEqual(String s1, String s2) {\n \n }\n}", + "metadata": { + "func_name": "canBeEqual" + } +} \ No newline at end of file diff --git a/check-if-strings-can-be-made-equal-with-operations-ii.json b/check-if-strings-can-be-made-equal-with-operations-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..3c56b9fb377665a143e01277db1241c6c30f0731 --- /dev/null +++ b/check-if-strings-can-be-made-equal-with-operations-ii.json @@ -0,0 +1,31 @@ +{ + "id": 2978, + "name": "check-if-strings-can-be-made-equal-with-operations-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-ii/", + "date": "2023-08-19", + "task_description": "You are given two strings `s1` and `s2`, both of length `n`, consisting of **lowercase** English letters. You can apply the following operation on **any** of the two strings **any** number of times: Choose any two indices `i` and `j` such that `i < j` and the difference `j - i` is **even**, then **swap** the two characters at those indices in the string. Return `true`_ if you can make the strings _`s1`_ and _`s2`_ equal, and _`false`_ otherwise_. **Example 1:** ``` **Input:** s1 = \"abcdba\", s2 = \"cabdab\" **Output:** true **Explanation:** We can apply the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = \"cbadba\". - Choose the indices i = 2, j = 4. The resulting string is s1 = \"cbbdaa\". - Choose the indices i = 1, j = 5. The resulting string is s1 = \"cabdab\" = s2. ``` **Example 2:** ``` **Input:** s1 = \"abe\", s2 = \"bea\" **Output:** false **Explanation:** It is not possible to make the two strings equal. ``` **Constraints:** `n == s1.length == s2.length` `1 <= n <= 105` `s1` and `s2` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s1 = \"abcdba\", s2 = \"cabdab\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s1 = \"abe\", s2 = \"bea\"", + "output": "false " + } + ], + "constraints": [ + "Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in the string.", + "n == s1.length == s2.length", + "1 <= n <= 105", + "s1 and s2 consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def checkStrings(self, s1, s2):\n \"\"\"\n :type s1: str\n :type s2: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean checkStrings(String s1, String s2) {\n \n }\n}", + "metadata": { + "func_name": "checkStrings" + } +} \ No newline at end of file diff --git a/check-if-there-is-a-valid-parentheses-string-path.json b/check-if-there-is-a-valid-parentheses-string-path.json new file mode 100644 index 0000000000000000000000000000000000000000..36255e921c3e72b17318f4628153ae78da8675c3 --- /dev/null +++ b/check-if-there-is-a-valid-parentheses-string-path.json @@ -0,0 +1,38 @@ +{ + "id": 2349, + "name": "check-if-there-is-a-valid-parentheses-string-path", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/check-if-there-is-a-valid-parentheses-string-path/", + "date": "2022-05-01", + "task_description": "A parentheses string is a **non-empty** string consisting only of `'('` and `')'`. It is **valid** if **any** of the following conditions is **true**: It is `()`. It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are valid parentheses strings. It can be written as `(A)`, where `A` is a valid parentheses string. You are given an `m x n` matrix of parentheses `grid`. A **valid parentheses string path** in the grid is a path satisfying **all** of the following conditions: The path starts from the upper left cell `(0, 0)`. The path ends at the bottom-right cell `(m - 1, n - 1)`. The path only ever moves **down** or **right**. The resulting parentheses string formed by the path is **valid**. Return `true` _if there exists a **valid parentheses string path** in the grid._ Otherwise, return `false`. **Example 1:** ``` **Input:** grid = [[\"(\",\"(\",\"(\"],[\")\",\"(\",\")\"],[\"(\",\"(\",\")\"],[\"(\",\"(\",\")\"]] **Output:** true **Explanation:** The above diagram shows two possible paths that form valid parentheses strings. The first path shown results in the valid parentheses string \"()(())\". The second path shown results in the valid parentheses string \"((()))\". Note that there may be other valid parentheses string paths. ``` **Example 2:** ``` **Input:** grid = [[\")\",\")\"],[\"(\",\"(\"]] **Output:** false **Explanation:** The two possible paths form the parentheses strings \"))(\" and \")((\". Since neither of them are valid parentheses strings, we return false. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 100` `grid[i][j]` is either `'('` or `')'`.", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[\"(\",\"(\",\"(\"],[\")\",\"(\",\")\"],[\"(\",\"(\",\")\"],[\"(\",\"(\",\")\"]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "grid = [[\")\",\")\"],[\"(\",\"(\"]]", + "output": "false " + } + ], + "constraints": [ + "It is ().", + "It can be written as AB (A concatenated with B), where A and B are valid parentheses strings.", + "It can be written as (A), where A is a valid parentheses string.", + "The path starts from the upper left cell (0, 0).", + "The path ends at the bottom-right cell (m - 1, n - 1).", + "The path only ever moves down or right.", + "The resulting parentheses string formed by the path is valid.", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 100", + "grid[i][j] is either '(' or ')'." + ], + "python_template": "class Solution(object):\n def hasValidPath(self, grid):\n \"\"\"\n :type grid: List[List[str]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean hasValidPath(char[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "hasValidPath" + } +} \ No newline at end of file diff --git a/check-if-there-is-a-valid-partition-for-the-array.json b/check-if-there-is-a-valid-partition-for-the-array.json new file mode 100644 index 0000000000000000000000000000000000000000..819523944394495774e51311b9785df26db0df1d --- /dev/null +++ b/check-if-there-is-a-valid-partition-for-the-array.json @@ -0,0 +1,29 @@ +{ + "id": 2443, + "name": "check-if-there-is-a-valid-partition-for-the-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/", + "date": "2022-07-31", + "task_description": "You are given a **0-indexed** integer array `nums`. You have to partition the array into one or more **contiguous** subarrays. We call a partition of the array **valid** if each of the obtained subarrays satisfies **one** of the following conditions: The subarray consists of **exactly** `2,` equal elements. For example, the subarray `[2,2]` is good. The subarray consists of **exactly** `3,` equal elements. For example, the subarray `[4,4,4]` is good. The subarray consists of **exactly** `3` consecutive increasing elements, that is, the difference between adjacent elements is `1`. For example, the subarray `[3,4,5]` is good, but the subarray `[1,3,5]` is not. Return `true`_ if the array has **at least** one valid partition_. Otherwise, return `false`. **Example 1:** ``` **Input:** nums = [4,4,4,5,6] **Output:** true **Explanation:** The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true. ``` **Example 2:** ``` **Input:** nums = [1,1,1,2] **Output:** false **Explanation:** There is no valid partition for this array. ``` **Constraints:** `2 <= nums.length <= 105` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,4,4,5,6]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1,2]", + "output": "false " + } + ], + "constraints": [ + "2 <= nums.length <= 105", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def validPartition(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean validPartition(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "validPartition" + } +} \ No newline at end of file diff --git a/check-if-two-chessboard-squares-have-the-same-color.json b/check-if-two-chessboard-squares-have-the-same-color.json new file mode 100644 index 0000000000000000000000000000000000000000..cea126bda36a691fc8c8be53e684997010d0abda --- /dev/null +++ b/check-if-two-chessboard-squares-have-the-same-color.json @@ -0,0 +1,30 @@ +{ + "id": 3553, + "name": "check-if-two-chessboard-squares-have-the-same-color", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/check-if-two-chessboard-squares-have-the-same-color/", + "date": "2024-08-25", + "task_description": "You are given two strings, `coordinate1` and `coordinate2`, representing the coordinates of a square on an `8 x 8` chessboard. Below is the chessboard for reference. Return `true` if these two squares have the same color and `false` otherwise. The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row). **Example 1:** **Input:** coordinate1 = \"a1\", coordinate2 = \"c3\" **Output:** true **Explanation:** Both squares are black. **Example 2:** **Input:** coordinate1 = \"a1\", coordinate2 = \"h3\" **Output:** false **Explanation:** Square `\"a1\"` is black and `\"h3\"` is white. **Constraints:** `coordinate1.length == coordinate2.length == 2` `'a' <= coordinate1[0], coordinate2[0] <= 'h'` `'1' <= coordinate1[1], coordinate2[1] <= '8'`", + "test_case": [ + { + "label": "Example 1", + "input": "coordinate1 = \"a1\", coordinate2 = \"c3\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "coordinate1 = \"a1\", coordinate2 = \"h3\"", + "output": "false " + } + ], + "constraints": [ + "coordinate1.length == coordinate2.length == 2", + "'a' <= coordinate1[0], coordinate2[0] <= 'h'", + "'1' <= coordinate1[1], coordinate2[1] <= '8'" + ], + "python_template": "class Solution(object):\n def checkTwoChessboards(self, coordinate1, coordinate2):\n \"\"\"\n :type coordinate1: str\n :type coordinate2: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean checkTwoChessboards(String coordinate1, String coordinate2) {\n \n }\n}", + "metadata": { + "func_name": "checkTwoChessboards" + } +} \ No newline at end of file diff --git a/circular-sentence.json b/circular-sentence.json new file mode 100644 index 0000000000000000000000000000000000000000..592c1216ad83797f8148515962bd6bf1b3cd4a58 --- /dev/null +++ b/circular-sentence.json @@ -0,0 +1,39 @@ +{ + "id": 2580, + "name": "circular-sentence", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/circular-sentence/", + "date": "2022-11-27", + "task_description": "A **sentence** is a list of words that are separated by a** single** space with no leading or trailing spaces. For example, `\"Hello World\"`, `\"HELLO\"`, `\"hello world hello world\"` are all sentences. Words consist of **only** uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different. A sentence is **circular **if: The last character of each word in the sentence is equal to the first character of its next word. The last character of the last word is equal to the first character of the first word. For example, `\"leetcode exercises sound delightful\"`, `\"eetcode\"`, `\"leetcode eats soul\" `are all circular sentences. However, `\"Leetcode is cool\"`, `\"happy Leetcode\"`, `\"Leetcode\"` and `\"I like Leetcode\"` are **not** circular sentences. Given a string `sentence`, return `true`_ if it is circular_. Otherwise, return `false`. **Example 1:** ``` **Input:** sentence = \"leetcode exercises sound delightful\" **Output:** true **Explanation:** The words in sentence are [\"leetcode\", \"exercises\", \"sound\", \"delightful\"]. - leetcode's last character is equal to exercises's first character. - exercises's last character is equal to sound's first character. - sound's last character is equal to delightful's first character. - delightful's last character is equal to leetcode's first character. The sentence is circular. ``` **Example 2:** ``` **Input:** sentence = \"eetcode\" **Output:** true **Explanation:** The words in sentence are [\"eetcode\"]. - eetcode's last character is equal to eetcode's first character. The sentence is circular. ``` **Example 3:** ``` **Input:** sentence = \"Leetcode is cool\" **Output:** false **Explanation:** The words in sentence are [\"Leetcode\", \"is\", \"cool\"]. - Leetcode's last character is **not** equal to is's first character. The sentence is **not** circular. ``` **Constraints:** `1 <= sentence.length <= 500` `sentence` consist of only lowercase and uppercase English letters and spaces. The words in `sentence` are separated by a single space. There are no leading or trailing spaces.", + "test_case": [ + { + "label": "Example 1", + "input": "sentence = \"leetcode exercises sound delightful\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "sentence = \"eetcode\"", + "output": "true " + }, + { + "label": "Example 3", + "input": "sentence = \"Leetcode is cool\"", + "output": "false " + } + ], + "constraints": [ + "For example, \"Hello World\", \"HELLO\", \"hello world hello world\" are all sentences.", + "The last character of each word in the sentence is equal to the first character of its next word.", + "The last character of the last word is equal to the first character of the first word.", + "1 <= sentence.length <= 500", + "sentence consist of only lowercase and uppercase English letters and spaces.", + "The words in sentence are separated by a single space.", + "There are no leading or trailing spaces." + ], + "python_template": "class Solution(object):\n def isCircularSentence(self, sentence):\n \"\"\"\n :type sentence: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isCircularSentence(String sentence) {\n \n }\n}", + "metadata": { + "func_name": "isCircularSentence" + } +} \ No newline at end of file diff --git a/clear-digits.json b/clear-digits.json new file mode 100644 index 0000000000000000000000000000000000000000..9b730435d9b91a577cc6f7bbc0370e68d060ba93 --- /dev/null +++ b/clear-digits.json @@ -0,0 +1,31 @@ +{ + "id": 3447, + "name": "clear-digits", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/clear-digits/", + "date": "2024-05-25", + "task_description": "You are given a string `s`. Your task is to remove **all** digits by doing this operation repeatedly: Delete the _first_ digit and the **closest** non-digit character to its _left_. Return the resulting string after removing all digits. **Note** that the operation _cannot_ be performed on a digit that does not have any non-digit character to its left. **Example 1:** **Input:** s = \"abc\" **Output:** \"abc\" **Explanation:** There is no digit in the string. **Example 2:** **Input:** s = \"cb34\" **Output:** \"\" **Explanation:** First, we apply the operation on `s[2]`, and `s` becomes `\"c4\"`. Then we apply the operation on `s[1]`, and `s` becomes `\"\"`. **Constraints:** `1 <= s.length <= 100` `s` consists only of lowercase English letters and digits. The input is generated such that it is possible to delete all digits.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abc\"", + "output": "\"abc\" " + }, + { + "label": "Example 2", + "input": "s = \"cb34\"", + "output": "\"\" " + } + ], + "constraints": [ + "Delete the first digit and the closest non-digit character to its left.", + "1 <= s.length <= 100", + "s consists only of lowercase English letters and digits.", + "The input is generated such that it is possible to delete all digits." + ], + "python_template": "class Solution(object):\n def clearDigits(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String clearDigits(String s) {\n \n }\n}", + "metadata": { + "func_name": "clearDigits" + } +} \ No newline at end of file diff --git a/closest-equal-element-queries.json b/closest-equal-element-queries.json new file mode 100644 index 0000000000000000000000000000000000000000..9531841098244568c187c8ae1b57d2ab3c5c433d --- /dev/null +++ b/closest-equal-element-queries.json @@ -0,0 +1,34 @@ +{ + "id": 3750, + "name": "closest-equal-element-queries", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/closest-equal-element-queries/", + "date": "2025-03-09", + "task_description": "You are given a **circular** array `nums` and an array `queries`. For each query `i`, you have to find the following: The **minimum** distance between the element at index `queries[i]` and **any** other index `j` in the **circular** array, where `nums[j] == nums[queries[i]]`. If no such index exists, the answer for that query should be -1. Return an array `answer` of the **same** size as `queries`, where `answer[i]` represents the result for query `i`. **Example 1:** **Input:** nums = [1,3,1,4,1,3,2], queries = [0,3,5] **Output:** [2,-1,3] **Explanation:** Query 0: The element at `queries[0] = 0` is `nums[0] = 1`. The nearest index with the same value is 2, and the distance between them is 2. Query 1: The element at `queries[1] = 3` is `nums[3] = 4`. No other index contains 4, so the result is -1. Query 2: The element at `queries[2] = 5` is `nums[5] = 3`. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: `5 -> 6 -> 0 -> 1`). **Example 2:** **Input:** nums = [1,2,3,4], queries = [0,1,2,3] **Output:** [-1,-1,-1,-1] **Explanation:** Each value in `nums` is unique, so no index shares the same value as the queried element. This results in -1 for all queries. **Constraints:** `1 <= queries.length <= nums.length <= 105` `1 <= nums[i] <= 106` `0 <= queries[i] < nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,1,4,1,3,2], queries = [0,3,5]", + "output": "[2,-1,3] " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4], queries = [0,1,2,3]", + "output": "[-1,-1,-1,-1] " + } + ], + "constraints": [ + "The minimum distance between the element at index queries[i] and any other index j in the circular array, where nums[j] == nums[queries[i]]. If no such index exists, the answer for that query should be -1.", + "Query 0: The element at queries[0] = 0 is nums[0] = 1. The nearest index with the same value is 2, and the distance between them is 2.", + "Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.", + "Query 2: The element at queries[2] = 5 is nums[5] = 3. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: 5 -> 6 -> 0 -> 1).", + "1 <= queries.length <= nums.length <= 105", + "1 <= nums[i] <= 106", + "0 <= queries[i] < nums.length" + ], + "python_template": "class Solution(object):\n def solveQueries(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 List solveQueries(int[] nums, int[] queries) {\n \n }\n}", + "metadata": { + "func_name": "solveQueries" + } +} \ No newline at end of file diff --git a/closest-prime-numbers-in-range.json b/closest-prime-numbers-in-range.json new file mode 100644 index 0000000000000000000000000000000000000000..e3db0e7f1ea893c669ba9ab3aa704e1bfdf21bac --- /dev/null +++ b/closest-prime-numbers-in-range.json @@ -0,0 +1,31 @@ +{ + "id": 2610, + "name": "closest-prime-numbers-in-range", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/closest-prime-numbers-in-range/", + "date": "2022-12-25", + "task_description": "Given two positive integers `left` and `right`, find the two integers `num1` and `num2` such that: `left <= num1 < num2 <= right `. Both `num1` and `num2` are prime numbers. `num2 - num1` is the **minimum** amongst all other pairs satisfying the above conditions. Return the positive integer array `ans = [num1, num2]`. If there are multiple pairs satisfying these conditions, return the one with the **smallest** `num1` value. If no such numbers exist, return `[-1, -1]`_._ **Example 1:** ``` **Input:** left = 10, right = 19 **Output:** [11,13] **Explanation:** The prime numbers between 10 and 19 are 11, 13, 17, and 19. The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19]. Since 11 is smaller than 17, we return the first pair. ``` **Example 2:** ``` **Input:** left = 4, right = 6 **Output:** [-1,-1] **Explanation:** There exists only one prime number in the given range, so the conditions cannot be satisfied. ``` **Constraints:** `1 <= left <= right <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "left = 10, right = 19", + "output": "[11,13] " + }, + { + "label": "Example 2", + "input": "left = 4, right = 6", + "output": "[-1,-1] " + } + ], + "constraints": [ + "left <= num1 < num2 <= right .", + "Both num1 and num2 are prime numbers.", + "num2 - num1 is the minimum amongst all other pairs satisfying the above conditions.", + "1 <= left <= right <= 106" + ], + "python_template": "class Solution(object):\n def closestPrimes(self, left, right):\n \"\"\"\n :type left: int\n :type right: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] closestPrimes(int left, int right) {\n \n }\n}", + "metadata": { + "func_name": "closestPrimes" + } +} \ No newline at end of file diff --git a/collect-coins-in-a-tree.json b/collect-coins-in-a-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..ff12803c31cf0d5ead4b52a37404560b99b88294 --- /dev/null +++ b/collect-coins-in-a-tree.json @@ -0,0 +1,37 @@ +{ + "id": 2717, + "name": "collect-coins-in-a-tree", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/collect-coins-in-a-tree/", + "date": "2023-03-19", + "task_description": "There exists an undirected and unrooted tree with `n` nodes indexed from `0` to `n - 1`. You are given an integer `n` and a 2D integer array edges of length `n - 1`, where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree. You are also given an array `coins` of size `n` where `coins[i]` can be either `0` or `1`, where `1` indicates the presence of a coin in the vertex `i`. Initially, you choose to start at any vertex in the tree. Then, you can perform the following operations any number of times: Collect all the coins that are at a distance of at most `2` from the current vertex, or Move to any adjacent vertex in the tree. Find _the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex_. Note that if you pass an edge several times, you need to count it into the answer several times. **Example 1:** ``` **Input:** coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]] **Output:** 2 **Explanation:** Start at vertex 2, collect the coin at vertex 0, move to vertex 3, collect the coin at vertex 5 then move back to vertex 2. ``` **Example 2:** ``` **Input:** coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]] **Output:** 2 **Explanation:** Start at vertex 0, collect the coins at vertices 4 and 3, move to vertex 2, collect the coin at vertex 7, then move back to vertex 0. ``` **Constraints:** `n == coins.length` `1 <= n <= 3 * 104` `0 <= coins[i] <= 1` `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": "coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]", + "output": "2 " + } + ], + "constraints": [ + "Collect all the coins that are at a distance of at most 2 from the current vertex, or", + "Move to any adjacent vertex in the tree.", + "n == coins.length", + "1 <= n <= 3 * 104", + "0 <= coins[i] <= 1", + "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 collectTheCoins(self, coins, edges):\n \"\"\"\n :type coins: List[int]\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int collectTheCoins(int[] coins, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "collectTheCoins" + } +} \ No newline at end of file diff --git a/collecting-chocolates.json b/collecting-chocolates.json new file mode 100644 index 0000000000000000000000000000000000000000..62a1a7ac9227a7c9577493a78562a07945d69040 --- /dev/null +++ b/collecting-chocolates.json @@ -0,0 +1,31 @@ +{ + "id": 2810, + "name": "collecting-chocolates", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/collecting-chocolates/", + "date": "2023-06-04", + "task_description": "You are given a **0-indexed** integer array `nums` of size `n` representing the cost of collecting different chocolates. The cost of collecting the chocolate at the index `i` is `nums[i]`. Each chocolate is of a different type, and initially, the chocolate at the index `i` is of `ith` type. In one operation, you can do the following with an incurred **cost** of `x`: Simultaneously change the chocolate of `ith` type to `((i + 1) mod n)th` type for all chocolates. Return _the minimum cost to collect chocolates of all types, given that you can perform as many operations as you would like._ **Example 1:** ``` **Input:** nums = [20,1,15], x = 5 **Output:** 13 **Explanation:** Initially, the chocolate types are [0,1,2]. We will buy the 1st type of chocolate at a cost of 1. Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2nd type of chocolate at a cost of 1. Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0th type of chocolate at a cost of 1. Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal. ``` **Example 2:** ``` **Input:** nums = [1,2,3], x = 4 **Output:** 6 **Explanation:** We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 109` `1 <= x <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [20,1,15], x = 5", + "output": "13 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3], x = 4", + "output": "6 " + } + ], + "constraints": [ + "Simultaneously change the chocolate of ith type to ((i + 1) mod n)th type for all chocolates.", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 109", + "1 <= x <= 109" + ], + "python_template": "class Solution(object):\n def minCost(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 minCost(int[] nums, int x) {\n \n }\n}", + "metadata": { + "func_name": "minCost" + } +} \ No newline at end of file diff --git a/construct-product-matrix.json b/construct-product-matrix.json new file mode 100644 index 0000000000000000000000000000000000000000..e1419d1ebe56f5b288aeef8f4bf81e3070d4f9ae --- /dev/null +++ b/construct-product-matrix.json @@ -0,0 +1,32 @@ +{ + "id": 3031, + "name": "construct-product-matrix", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/construct-product-matrix/", + "date": "2023-10-08", + "task_description": "Given a **0-indexed** 2D integer matrix `grid` of size `n * m`, we define a **0-indexed** 2D matrix `p` of size `n * m` as the **product** matrix of `grid` if the following condition is met: Each element `p[i][j]` is calculated as the product of all elements in `grid` except for the element `grid[i][j]`. This product is then taken modulo `12345`. Return _the product matrix of_ `grid`. **Example 1:** ``` **Input:** grid = [[1,2],[3,4]] **Output:** [[24,12],[8,6]] **Explanation:** p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24 p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12 p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8 p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6 So the answer is [[24,12],[8,6]]. ``` **Example 2:** ``` **Input:** grid = [[12345],[2],[1]] **Output:** [[2],[0],[0]] **Explanation:** p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2. p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0. p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0. So the answer is [[2],[0],[0]]. ``` **Constraints:** `1 <= n == grid.length <= 105` `1 <= m == grid[i].length <= 105` `2 <= n * m <= 105` `1 <= grid[i][j] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,2],[3,4]]", + "output": "[[24,12],[8,6]] " + }, + { + "label": "Example 2", + "input": "grid = [[12345],[2],[1]]", + "output": "[[2],[0],[0]] " + } + ], + "constraints": [ + "Each element p[i][j] is calculated as the product of all elements in grid except for the element grid[i][j]. This product is then taken modulo 12345.", + "1 <= n == grid.length <= 105", + "1 <= m == grid[i].length <= 105", + "2 <= n * m <= 105", + "1 <= grid[i][j] <= 109" + ], + "python_template": "class Solution(object):\n def constructProductMatrix(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] constructProductMatrix(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "constructProductMatrix" + } +} \ No newline at end of file diff --git a/construct-smallest-number-from-di-string.json b/construct-smallest-number-from-di-string.json new file mode 100644 index 0000000000000000000000000000000000000000..ffec0940eda5533e7000547bfe6c28e72d7cf1fa --- /dev/null +++ b/construct-smallest-number-from-di-string.json @@ -0,0 +1,32 @@ +{ + "id": 2456, + "name": "construct-smallest-number-from-di-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/construct-smallest-number-from-di-string/", + "date": "2022-08-07", + "task_description": "You are given a **0-indexed** string `pattern` of length `n` consisting of the characters `'I'` meaning **increasing** and `'D'` meaning **decreasing**. A **0-indexed** string `num` of length `n + 1` is created using the following conditions: `num` consists of the digits `'1'` to `'9'`, where each digit is used **at most** once. If `pattern[i] == 'I'`, then `num[i] < num[i + 1]`. If `pattern[i] == 'D'`, then `num[i] > num[i + 1]`. Return _the lexicographically **smallest** possible string _`num`_ that meets the conditions._ **Example 1:** ``` **Input:** pattern = \"IIIDIDDD\" **Output:** \"123549876\" **Explanation: **At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1]. At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1]. Some possible values of num are \"245639871\", \"135749862\", and \"123849765\". It can be proven that \"123549876\" is the smallest possible num that meets the conditions. Note that \"123414321\" is not possible because the digit '1' is used more than once. ``` **Example 2:** ``` **Input:** pattern = \"DDD\" **Output:** \"4321\" **Explanation:** Some possible values of num are \"9876\", \"7321\", and \"8742\". It can be proven that \"4321\" is the smallest possible num that meets the conditions. ``` **Constraints:** `1 <= pattern.length <= 8` `pattern` consists of only the letters `'I'` and `'D'`.", + "test_case": [ + { + "label": "Example 1", + "input": "pattern = \"IIIDIDDD\"", + "output": "\"123549876\" " + }, + { + "label": "Example 2", + "input": "pattern = \"DDD\"", + "output": "\"4321\" " + } + ], + "constraints": [ + "num consists of the digits '1' to '9', where each digit is used at most once.", + "If pattern[i] == 'I', then num[i] < num[i + 1].", + "If pattern[i] == 'D', then num[i] > num[i + 1].", + "1 <= pattern.length <= 8", + "pattern consists of only the letters 'I' and 'D'." + ], + "python_template": "class Solution(object):\n def smallestNumber(self, pattern):\n \"\"\"\n :type pattern: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String smallestNumber(String pattern) {\n \n }\n}", + "metadata": { + "func_name": "smallestNumber" + } +} \ No newline at end of file diff --git a/construct-string-with-repeat-limit.json b/construct-string-with-repeat-limit.json new file mode 100644 index 0000000000000000000000000000000000000000..10310308cd8ad32357556545aa3ba5ccc364d5d9 --- /dev/null +++ b/construct-string-with-repeat-limit.json @@ -0,0 +1,29 @@ +{ + "id": 2300, + "name": "construct-string-with-repeat-limit", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/construct-string-with-repeat-limit/", + "date": "2022-02-13", + "task_description": "You are given a string `s` and an integer `repeatLimit`. Construct a new string `repeatLimitedString` using the characters of `s` such that no letter appears **more than** `repeatLimit` times **in a row**. You do **not** have to use all characters from `s`. Return _the **lexicographically largest** _`repeatLimitedString` _possible_. A string `a` is **lexicographically larger** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears later in the alphabet than the corresponding letter in `b`. If the first `min(a.length, b.length)` characters do not differ, then the longer string is the lexicographically larger one. **Example 1:** ``` **Input:** s = \"cczazcc\", repeatLimit = 3 **Output:** \"zzcccac\" **Explanation:** We use all of the characters from s to construct the repeatLimitedString \"zzcccac\". The letter 'a' appears at most 1 time in a row. The letter 'c' appears at most 3 times in a row. The letter 'z' appears at most 2 times in a row. Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. The string is the lexicographically largest repeatLimitedString possible so we return \"zzcccac\". Note that the string \"zzcccca\" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString. ``` **Example 2:** ``` **Input:** s = \"aababab\", repeatLimit = 2 **Output:** \"bbabaa\" **Explanation:** We use only some of the characters from s to construct the repeatLimitedString \"bbabaa\". The letter 'a' appears at most 2 times in a row. The letter 'b' appears at most 2 times in a row. Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString. The string is the lexicographically largest repeatLimitedString possible so we return \"bbabaa\". Note that the string \"bbabaaa\" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString. ``` **Constraints:** `1 <= repeatLimit <= s.length <= 105` `s` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"cczazcc\", repeatLimit = 3", + "output": "\"zzcccac\" " + }, + { + "label": "Example 2", + "input": "s = \"aababab\", repeatLimit = 2", + "output": "\"bbabaa\" " + } + ], + "constraints": [ + "1 <= repeatLimit <= s.length <= 105", + "s consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def repeatLimitedString(self, s, repeatLimit):\n \"\"\"\n :type s: str\n :type repeatLimit: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String repeatLimitedString(String s, int repeatLimit) {\n \n }\n}", + "metadata": { + "func_name": "repeatLimitedString" + } +} \ No newline at end of file diff --git a/construct-the-longest-new-string.json b/construct-the-longest-new-string.json new file mode 100644 index 0000000000000000000000000000000000000000..4498b3916675abcc17e0237fbd8e610b8116694f --- /dev/null +++ b/construct-the-longest-new-string.json @@ -0,0 +1,28 @@ +{ + "id": 2850, + "name": "construct-the-longest-new-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/construct-the-longest-new-string/", + "date": "2023-06-10", + "task_description": "You are given three integers `x`, `y`, and `z`. You have `x` strings equal to `\"AA\"`, `y` strings equal to `\"BB\"`, and `z` strings equal to `\"AB\"`. You want to choose some (possibly all or none) of these strings and concatenate them in some order to form a new string. This new string must not contain `\"AAA\"` or `\"BBB\"` as a substring. Return _the maximum possible length of the new string_. A substring is a contiguous **non-empty** sequence of characters within a string. **Example 1:** ``` **Input:** x = 2, y = 5, z = 1 **Output:** 12 **Explanation: **We can concatenate the strings \"BB\", \"AA\", \"BB\", \"AA\", \"BB\", and \"AB\" in that order. Then, our new string is \"BBAABBAABBAB\". That string has length 12, and we can show that it is impossible to construct a string of longer length. ``` **Example 2:** ``` **Input:** x = 3, y = 2, z = 2 **Output:** 14 **Explanation:** We can concatenate the strings \"AB\", \"AB\", \"AA\", \"BB\", \"AA\", \"BB\", and \"AA\" in that order. Then, our new string is \"ABABAABBAABBAA\". That string has length 14, and we can show that it is impossible to construct a string of longer length. ``` **Constraints:** `1 <= x, y, z <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "x = 2, y = 5, z = 1", + "output": "12 " + }, + { + "label": "Example 2", + "input": "x = 3, y = 2, z = 2", + "output": "14 " + } + ], + "constraints": [ + "1 <= x, y, z <= 50" + ], + "python_template": "class Solution(object):\n def longestString(self, x, y, z):\n \"\"\"\n :type x: int\n :type y: int\n :type z: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestString(int x, int y, int z) {\n \n }\n}", + "metadata": { + "func_name": "longestString" + } +} \ No newline at end of file diff --git a/convert-an-array-into-a-2d-array-with-conditions.json b/convert-an-array-into-a-2d-array-with-conditions.json new file mode 100644 index 0000000000000000000000000000000000000000..6c344aa673c356629e0549f2a7bce1e204946a84 --- /dev/null +++ b/convert-an-array-into-a-2d-array-with-conditions.json @@ -0,0 +1,32 @@ +{ + "id": 2724, + "name": "convert-an-array-into-a-2d-array-with-conditions", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/", + "date": "2023-03-26", + "task_description": "You are given an integer array `nums`. You need to create a 2D array from `nums` satisfying the following conditions: The 2D array should contain **only** the elements of the array `nums`. Each row in the 2D array contains **distinct** integers. The number of rows in the 2D array should be **minimal**. Return _the resulting array_. If there are multiple answers, return any of them. **Note** that the 2D array can have a different number of elements on each row. **Example 1:** ``` **Input:** nums = [1,3,4,1,2,3,1] **Output:** [[1,3,4,2],[1,3],[1]] **Explanation:** We can create a 2D array that contains the following rows: - 1,3,4,2 - 1,3 - 1 All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer. It can be shown that we cannot have less than 3 rows in a valid array. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** [[4,3,2,1]] **Explanation:** All elements of the array are distinct, so we can keep all of them in the first row of the 2D array. ``` **Constraints:** `1 <= nums.length <= 200` `1 <= nums[i] <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,4,1,2,3,1]", + "output": "[[1,3,4,2],[1,3],[1]] " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4]", + "output": "[[4,3,2,1]] " + } + ], + "constraints": [ + "The 2D array should contain only the elements of the array nums.", + "Each row in the 2D array contains distinct integers.", + "The number of rows in the 2D array should be minimal.", + "1 <= nums.length <= 200", + "1 <= nums[i] <= nums.length" + ], + "python_template": "class Solution(object):\n def findMatrix(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public List> findMatrix(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findMatrix" + } +} \ No newline at end of file diff --git a/count-alternating-subarrays.json b/count-alternating-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..fe9b104aa8945fda5e9537a22736a78dd39452bd --- /dev/null +++ b/count-alternating-subarrays.json @@ -0,0 +1,29 @@ +{ + "id": 3374, + "name": "count-alternating-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-alternating-subarrays/", + "date": "2024-03-24", + "task_description": "You are given a binary array `nums`. We call a subarray **alternating** if **no** two **adjacent** elements in the subarray have the **same** value. Return _the number of alternating subarrays in _`nums`. **Example 1:** **Input:** nums = [0,1,1,1] **Output:** 5 **Explanation:** The following subarrays are alternating: `[0]`, `[1]`, `[1]`, `[1]`, and `[0,1]`. **Example 2:** **Input:** nums = [1,0,1,0] **Output:** 10 **Explanation:** Every subarray of the array is alternating. There are 10 possible subarrays that we can choose. **Constraints:** `1 <= nums.length <= 105` `nums[i]` is either `0` or `1`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,1,1]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [1,0,1,0]", + "output": "10 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "nums[i] is either 0 or 1." + ], + "python_template": "class Solution(object):\n def countAlternatingSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countAlternatingSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "countAlternatingSubarrays" + } +} \ No newline at end of file diff --git a/count-anagrams.json b/count-anagrams.json new file mode 100644 index 0000000000000000000000000000000000000000..12735ec7e689c1fbbb6c3c383edb75a873ee4df2 --- /dev/null +++ b/count-anagrams.json @@ -0,0 +1,31 @@ +{ + "id": 2605, + "name": "count-anagrams", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-anagrams/", + "date": "2022-12-10", + "task_description": "You are given a string `s` containing one or more words. Every consecutive pair of words is separated by a single space `' '`. A string `t` is an **anagram** of string `s` if the `ith` word of `t` is a **permutation** of the `ith` word of `s`. For example, `\"acb dfe\"` is an anagram of `\"abc def\"`, but `\"def cab\"` and `\"adc bef\"` are not. Return _the number of **distinct anagrams** of _`s`. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** s = \"too hot\" **Output:** 18 **Explanation:** Some of the anagrams of the given string are \"too hot\", \"oot hot\", \"oto toh\", \"too toh\", and \"too oht\". ``` **Example 2:** ``` **Input:** s = \"aa\" **Output:** 1 **Explanation:** There is only one anagram possible for the given string. ``` **Constraints:** `1 <= s.length <= 105` `s` consists of lowercase English letters and spaces `' '`. There is single space between consecutive words.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"too hot\"", + "output": "18 " + }, + { + "label": "Example 2", + "input": "s = \"aa\"", + "output": "1 " + } + ], + "constraints": [ + "For example, \"acb dfe\" is an anagram of \"abc def\", but \"def cab\" and \"adc bef\" are not.", + "1 <= s.length <= 105", + "s consists of lowercase English letters and spaces ' '.", + "There is single space between consecutive words." + ], + "python_template": "class Solution(object):\n def countAnagrams(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countAnagrams(String s) {\n \n }\n}", + "metadata": { + "func_name": "countAnagrams" + } +} \ No newline at end of file diff --git a/count-array-pairs-divisible-by-k.json b/count-array-pairs-divisible-by-k.json new file mode 100644 index 0000000000000000000000000000000000000000..059dd13a9520196ead8df870132ce5122d420f92 --- /dev/null +++ b/count-array-pairs-divisible-by-k.json @@ -0,0 +1,31 @@ +{ + "id": 2301, + "name": "count-array-pairs-divisible-by-k", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-array-pairs-divisible-by-k/", + "date": "2022-02-13", + "task_description": "Given a **0-indexed** integer array `nums` of length `n` and an integer `k`, return _the **number of pairs**_ `(i, j)` _such that:_ `0 <= i < j <= n - 1` _and_ `nums[i] * nums[j]` _is divisible by_ `k`. **Example 1:** ``` **Input:** nums = [1,2,3,4,5], k = 2 **Output:** 7 **Explanation:** The 7 pairs of indices whose corresponding products are divisible by 2 are (0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4). Their products are 2, 4, 6, 8, 10, 12, and 20 respectively. Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4], k = 5 **Output:** 0 **Explanation:** There does not exist any pair of indices whose corresponding product is divisible by 5. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i], k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5], k = 2", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4], k = 5", + "output": "0 " + } + ], + "constraints": [ + "0 <= i < j <= n - 1 and", + "nums[i] * nums[j] is divisible by k.", + "1 <= nums.length <= 105", + "1 <= nums[i], k <= 105" + ], + "python_template": "class Solution(object):\n def countPairs(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 countPairs(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "countPairs" + } +} \ No newline at end of file diff --git a/count-asterisks.json b/count-asterisks.json new file mode 100644 index 0000000000000000000000000000000000000000..de3fb408749f691c73fc2c606bc9ed58112b2cee --- /dev/null +++ b/count-asterisks.json @@ -0,0 +1,35 @@ +{ + "id": 2401, + "name": "count-asterisks", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-asterisks/", + "date": "2022-06-11", + "task_description": "You are given a string `s`, where every **two** consecutive vertical bars `'|'` are grouped into a **pair**. In other words, the 1st and 2nd `'|'` make a pair, the 3rd and 4th `'|'` make a pair, and so forth. Return _the number of _`'*'`_ in _`s`_, **excluding** the _`'*'`_ between each pair of _`'|'`. **Note** that each `'|'` will belong to **exactly** one pair. **Example 1:** ``` **Input:** s = \"l|*e*et|c**o|*de|\" **Output:** 2 **Explanation:** The considered characters are underlined: \"l|*e*et|c**o|*de|\". The characters between the first and second '|' are excluded from the answer. Also, the characters between the third and fourth '|' are excluded from the answer. There are 2 asterisks considered. Therefore, we return 2. ``` **Example 2:** ``` **Input:** s = \"iamprogrammer\" **Output:** 0 **Explanation:** In this example, there are no asterisks in s. Therefore, we return 0. ``` **Example 3:** ``` **Input:** s = \"yo|uar|e**|b|e***au|tifu|l\" **Output:** 5 **Explanation:** The considered characters are underlined: \"yo|uar|e**|b|e***au|tifu|l\". There are 5 asterisks considered. Therefore, we return 5. ``` **Constraints:** `1 <= s.length <= 1000` `s` consists of lowercase English letters, vertical bars `'|'`, and asterisks `'*'`. `s` contains an **even** number of vertical bars `'|'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"l|*e*et|c**o|*de|\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"iamprogrammer\"", + "output": "0 " + }, + { + "label": "Example 3", + "input": "s = \"yo|uar|e**|b|e***au|tifu|l\"", + "output": "5 " + } + ], + "constraints": [ + "1 <= s.length <= 1000", + "s consists of lowercase English letters, vertical bars '|', and asterisks '*'.", + "s contains an even number of vertical bars '|'." + ], + "python_template": "class Solution(object):\n def countAsterisks(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countAsterisks(String s) {\n \n }\n}", + "metadata": { + "func_name": "countAsterisks" + } +} \ No newline at end of file diff --git a/count-beautiful-numbers.json b/count-beautiful-numbers.json new file mode 100644 index 0000000000000000000000000000000000000000..fa9028d4643a855e7ea29863166f7c2c3ef955c2 --- /dev/null +++ b/count-beautiful-numbers.json @@ -0,0 +1,28 @@ +{ + "id": 3801, + "name": "count-beautiful-numbers", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-beautiful-numbers/", + "date": "2025-03-09", + "task_description": "You are given two positive integers, `l` and `r`. A positive integer is called **beautiful** if the product of its digits is divisible by the sum of its digits. Return the count of **beautiful** numbers between `l` and `r`, inclusive. **Example 1:** **Input:** l = 10, r = 20 **Output:** 2 **Explanation:** The beautiful numbers in the range are 10 and 20. **Example 2:** **Input:** l = 1, r = 15 **Output:** 10 **Explanation:** The beautiful numbers in the range are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. **Constraints:** `1 <= l <= r < 109`", + "test_case": [ + { + "label": "Example 1", + "input": "l = 10, r = 20", + "output": "2 " + }, + { + "label": "Example 2", + "input": "l = 1, r = 15", + "output": "10 " + } + ], + "constraints": [ + "1 <= l <= r < 109" + ], + "python_template": "class Solution(object):\n def beautifulNumbers(self, l, r):\n \"\"\"\n :type l: int\n :type r: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int beautifulNumbers(int l, int r) {\n \n }\n}", + "metadata": { + "func_name": "beautifulNumbers" + } +} \ No newline at end of file diff --git a/count-beautiful-substrings-i.json b/count-beautiful-substrings-i.json new file mode 100644 index 0000000000000000000000000000000000000000..598e1f71a4ac6f55f9968fbb62bb305291092433 --- /dev/null +++ b/count-beautiful-substrings-i.json @@ -0,0 +1,37 @@ +{ + "id": 3210, + "name": "count-beautiful-substrings-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-beautiful-substrings-i/", + "date": "2023-11-19", + "task_description": "You are given a string `s` and a positive integer `k`. Let `vowels` and `consonants` be the number of vowels and consonants in a string. A string is **beautiful** if: `vowels == consonants`. `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowels` and `consonants` is divisible by `k`. Return _the number of **non-empty beautiful substrings** in the given string_ `s`. A **substring** is a contiguous sequence of characters in a string. **Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. **Consonant letters** in English are every letter except vowels. **Example 1:** ``` **Input:** s = \"baeyh\", k = 2 **Output:** 2 **Explanation:** There are 2 beautiful substrings in the given string. - Substring \"baeyh\", vowels = 2 ([\"a\",e\"]), consonants = 2 ([\"y\",\"h\"]). You can see that string \"aeyh\" is beautiful as vowels == consonants and vowels * consonants % k == 0. - Substring \"baeyh\", vowels = 2 ([\"a\",e\"]), consonants = 2 ([\"b\",\"y\"]). You can see that string \"baey\" is beautiful as vowels == consonants and vowels * consonants % k == 0. It can be shown that there are only 2 beautiful substrings in the given string. ``` **Example 2:** ``` **Input:** s = \"abba\", k = 1 **Output:** 3 **Explanation:** There are 3 beautiful substrings in the given string. - Substring \"abba\", vowels = 1 ([\"a\"]), consonants = 1 ([\"b\"]). - Substring \"abba\", vowels = 1 ([\"a\"]), consonants = 1 ([\"b\"]). - Substring \"abba\", vowels = 2 ([\"a\",\"a\"]), consonants = 2 ([\"b\",\"b\"]). It can be shown that there are only 3 beautiful substrings in the given string. ``` **Example 3:** ``` **Input:** s = \"bcdf\", k = 1 **Output:** 0 **Explanation:** There are no beautiful substrings in the given string. ``` **Constraints:** `1 <= s.length <= 1000` `1 <= k <= 1000` `s` consists of only English lowercase letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"baeyh\", k = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abba\", k = 1", + "output": "3 " + }, + { + "label": "Example 3", + "input": "s = \"bcdf\", k = 1", + "output": "0 " + } + ], + "constraints": [ + "vowels == consonants.", + "(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.", + "1 <= s.length <= 1000", + "1 <= k <= 1000", + "s consists of only English lowercase letters." + ], + "python_template": "class Solution(object):\n def beautifulSubstrings(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int beautifulSubstrings(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "beautifulSubstrings" + } +} \ No newline at end of file diff --git a/count-beautiful-substrings-ii.json b/count-beautiful-substrings-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..c37024b50bd4c66ae6c26821ce7777c9eff41451 --- /dev/null +++ b/count-beautiful-substrings-ii.json @@ -0,0 +1,37 @@ +{ + "id": 3208, + "name": "count-beautiful-substrings-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-beautiful-substrings-ii/", + "date": "2023-11-19", + "task_description": "You are given a string `s` and a positive integer `k`. Let `vowels` and `consonants` be the number of vowels and consonants in a string. A string is **beautiful** if: `vowels == consonants`. `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowels` and `consonants` is divisible by `k`. Return _the number of **non-empty beautiful substrings** in the given string_ `s`. A **substring** is a contiguous sequence of characters in a string. **Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. **Consonant letters** in English are every letter except vowels. **Example 1:** ``` **Input:** s = \"baeyh\", k = 2 **Output:** 2 **Explanation:** There are 2 beautiful substrings in the given string. - Substring \"baeyh\", vowels = 2 ([\"a\",e\"]), consonants = 2 ([\"y\",\"h\"]). You can see that string \"aeyh\" is beautiful as vowels == consonants and vowels * consonants % k == 0. - Substring \"baeyh\", vowels = 2 ([\"a\",e\"]), consonants = 2 ([\"b\",\"y\"]). You can see that string \"baey\" is beautiful as vowels == consonants and vowels * consonants % k == 0. It can be shown that there are only 2 beautiful substrings in the given string. ``` **Example 2:** ``` **Input:** s = \"abba\", k = 1 **Output:** 3 **Explanation:** There are 3 beautiful substrings in the given string. - Substring \"abba\", vowels = 1 ([\"a\"]), consonants = 1 ([\"b\"]). - Substring \"abba\", vowels = 1 ([\"a\"]), consonants = 1 ([\"b\"]). - Substring \"abba\", vowels = 2 ([\"a\",\"a\"]), consonants = 2 ([\"b\",\"b\"]). It can be shown that there are only 3 beautiful substrings in the given string. ``` **Example 3:** ``` **Input:** s = \"bcdf\", k = 1 **Output:** 0 **Explanation:** There are no beautiful substrings in the given string. ``` **Constraints:** `1 <= s.length <= 5 * 104` `1 <= k <= 1000` `s` consists of only English lowercase letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"baeyh\", k = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abba\", k = 1", + "output": "3 " + }, + { + "label": "Example 3", + "input": "s = \"bcdf\", k = 1", + "output": "0 " + } + ], + "constraints": [ + "vowels == consonants.", + "(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.", + "1 <= s.length <= 5 * 104", + "1 <= k <= 1000", + "s consists of only English lowercase letters." + ], + "python_template": "class Solution(object):\n def beautifulSubstrings(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long beautifulSubstrings(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "beautifulSubstrings" + } +} \ No newline at end of file diff --git a/count-complete-subarrays-in-an-array.json b/count-complete-subarrays-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..98fd8323a7ebcb41223eed1bba991dee329d7548 --- /dev/null +++ b/count-complete-subarrays-in-an-array.json @@ -0,0 +1,30 @@ +{ + "id": 2856, + "name": "count-complete-subarrays-in-an-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-complete-subarrays-in-an-array/", + "date": "2023-07-23", + "task_description": "You are given an array `nums` consisting of **positive** integers. We call a subarray of an array **complete** if the following condition is satisfied: The number of **distinct** elements in the subarray is equal to the number of distinct elements in the whole array. Return _the number of **complete** subarrays_. A **subarray** is a contiguous non-empty part of an array. **Example 1:** ``` **Input:** nums = [1,3,1,2,2] **Output:** 4 **Explanation:** The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2]. ``` **Example 2:** ``` **Input:** nums = [5,5,5,5] **Output:** 10 **Explanation:** The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 2000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,1,2,2]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [5,5,5,5]", + "output": "10 " + } + ], + "constraints": [ + "The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array.", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 2000" + ], + "python_template": "class Solution(object):\n def countCompleteSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countCompleteSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "countCompleteSubarrays" + } +} \ No newline at end of file diff --git a/count-complete-substrings.json b/count-complete-substrings.json new file mode 100644 index 0000000000000000000000000000000000000000..c55bf7cacb7094642065b128efdc5e9b40714857 --- /dev/null +++ b/count-complete-substrings.json @@ -0,0 +1,32 @@ +{ + "id": 3223, + "name": "count-complete-substrings", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-complete-substrings/", + "date": "2023-11-26", + "task_description": "You are given a string `word` and an integer `k`. A substring `s` of `word` is **complete** if: Each character in `s` occurs **exactly** `k` times. The difference between two adjacent characters is **at most** `2`. That is, for any two adjacent characters `c1` and `c2` in `s`, the absolute difference in their positions in the alphabet is **at most** `2`. Return _the number of **complete **substrings of_ `word`. A **substring** is a **non-empty** contiguous sequence of characters in a string. **Example 1:** ``` **Input:** word = \"igigee\", k = 2 **Output:** 3 **Explanation:** The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: **igig**ee, igig**ee**, **igigee**. ``` **Example 2:** ``` **Input:** word = \"aaabbbccc\", k = 3 **Output:** 6 **Explanation:** The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: **aaa**bbbccc, aaa**bbb**ccc, aaabbb**ccc**, **aaabbb**ccc, aaa**bbbccc**, **aaabbbccc**. ``` **Constraints:** `1 <= word.length <= 105` `word` consists only of lowercase English letters. `1 <= k <= word.length`", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"igigee\", k = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "word = \"aaabbbccc\", k = 3", + "output": "6 " + } + ], + "constraints": [ + "Each character in s occurs exactly k times.", + "The difference between two adjacent characters is at most 2. That is, for any two adjacent characters c1 and c2 in s, the absolute difference in their positions in the alphabet is at most 2.", + "1 <= word.length <= 105", + "word consists only of lowercase English letters.", + "1 <= k <= word.length" + ], + "python_template": "class Solution(object):\n def countCompleteSubstrings(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countCompleteSubstrings(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "countCompleteSubstrings" + } +} \ No newline at end of file diff --git a/count-connected-components-in-lcm-graph.json b/count-connected-components-in-lcm-graph.json new file mode 100644 index 0000000000000000000000000000000000000000..b3d4193296b7f8ea28068317b33c2e9efdc78e7b --- /dev/null +++ b/count-connected-components-in-lcm-graph.json @@ -0,0 +1,31 @@ +{ + "id": 3680, + "name": "count-connected-components-in-lcm-graph", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-connected-components-in-lcm-graph/", + "date": "2024-11-23", + "task_description": "You are given an array of integers `nums` of size `n` and a **positive** integer `threshold`. There is a graph consisting of `n` nodes with the `ith` node having a value of `nums[i]`. Two nodes `i` and `j` in the graph are connected via an **undirected** edge if `lcm(nums[i], nums[j]) <= threshold`. Return the number of **connected components** in this graph. A **connected component** is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph. The term `lcm(a, b)` denotes the **least common multiple** of `a` and `b`. **Example 1:** **Input:** nums = [2,4,8,3,9], threshold = 5 **Output:** 4 **Explanation:** The four connected components are `(2, 4)`, `(3)`, `(8)`, `(9)`. **Example 2:** **Input:** nums = [2,4,8,3,9,12], threshold = 10 **Output:** 2 **Explanation:** The two connected components are `(2, 3, 4, 8, 9)`, and `(12)`. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` All elements of `nums` are unique. `1 <= threshold <= 2 * 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,4,8,3,9], threshold = 5", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [2,4,8,3,9,12], threshold = 10", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "All elements of nums are unique.", + "1 <= threshold <= 2 * 105" + ], + "python_template": "class Solution(object):\n def countComponents(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 countComponents(int[] nums, int threshold) {\n \n }\n}", + "metadata": { + "func_name": "countComponents" + } +} \ No newline at end of file diff --git a/count-of-substrings-containing-every-vowel-and-k-consonants-i.json b/count-of-substrings-containing-every-vowel-and-k-consonants-i.json new file mode 100644 index 0000000000000000000000000000000000000000..40d52fc3f92cf517e9b8cdaf79eed987a53c6a44 --- /dev/null +++ b/count-of-substrings-containing-every-vowel-and-k-consonants-i.json @@ -0,0 +1,38 @@ +{ + "id": 3570, + "name": "count-of-substrings-containing-every-vowel-and-k-consonants-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/", + "date": "2024-09-22", + "task_description": "You are given a string `word` and a **non-negative** integer `k`. Return the total number of substrings of `word` that contain every vowel (`'a'`, `'e'`, `'i'`, `'o'`, and `'u'`) **at least** once and **exactly** `k` consonants. **Example 1:** **Input:** word = \"aeioqq\", k = 1 **Output:** 0 **Explanation:** There is no substring with every vowel. **Example 2:** **Input:** word = \"aeiou\", k = 0 **Output:** 1 **Explanation:** The only substring with every vowel and zero consonants is `word[0..4]`, which is `\"aeiou\"`. **Example 3:** **Input:** word = \"ieaouqqieaouqq\", k = 1 **Output:** 3 **Explanation:** The substrings with every vowel and one consonant are: `word[0..5]`, which is `\"ieaouq\"`. `word[6..11]`, which is `\"qieaou\"`. `word[7..12]`, which is `\"ieaouq\"`. **Constraints:** `5 <= word.length <= 250` `word` consists only of lowercase English letters. `0 <= k <= word.length - 5`", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"aeioqq\", k = 1", + "output": "0 " + }, + { + "label": "Example 2", + "input": "word = \"aeiou\", k = 0", + "output": "1 " + }, + { + "label": "Example 3", + "input": "word = \" ieaouqqieaouqq \", k = 1", + "output": "3 " + } + ], + "constraints": [ + "word[0..5], which is \"ieaouq\".", + "word[6..11], which is \"qieaou\".", + "word[7..12], which is \"ieaouq\".", + "5 <= word.length <= 250", + "word consists only of lowercase English letters.", + "0 <= k <= word.length - 5" + ], + "python_template": "class Solution(object):\n def countOfSubstrings(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countOfSubstrings(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "countOfSubstrings" + } +} \ No newline at end of file diff --git a/count-of-substrings-containing-every-vowel-and-k-consonants-ii.json b/count-of-substrings-containing-every-vowel-and-k-consonants-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..738972060d20be225f39e8d8ded32147cc02ee8a --- /dev/null +++ b/count-of-substrings-containing-every-vowel-and-k-consonants-ii.json @@ -0,0 +1,38 @@ +{ + "id": 3569, + "name": "count-of-substrings-containing-every-vowel-and-k-consonants-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/", + "date": "2024-09-22", + "task_description": "You are given a string `word` and a **non-negative** integer `k`. Return the total number of substrings of `word` that contain every vowel (`'a'`, `'e'`, `'i'`, `'o'`, and `'u'`) **at least** once and **exactly** `k` consonants. **Example 1:** **Input:** word = \"aeioqq\", k = 1 **Output:** 0 **Explanation:** There is no substring with every vowel. **Example 2:** **Input:** word = \"aeiou\", k = 0 **Output:** 1 **Explanation:** The only substring with every vowel and zero consonants is `word[0..4]`, which is `\"aeiou\"`. **Example 3:** **Input:** word = \"ieaouqqieaouqq\", k = 1 **Output:** 3 **Explanation:** The substrings with every vowel and one consonant are: `word[0..5]`, which is `\"ieaouq\"`. `word[6..11]`, which is `\"qieaou\"`. `word[7..12]`, which is `\"ieaouq\"`. **Constraints:** `5 <= word.length <= 2 * 105` `word` consists only of lowercase English letters. `0 <= k <= word.length - 5`", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"aeioqq\", k = 1", + "output": "0 " + }, + { + "label": "Example 2", + "input": "word = \"aeiou\", k = 0", + "output": "1 " + }, + { + "label": "Example 3", + "input": "word = \" ieaouqqieaouqq \", k = 1", + "output": "3 " + } + ], + "constraints": [ + "word[0..5], which is \"ieaouq\".", + "word[6..11], which is \"qieaou\".", + "word[7..12], which is \"ieaouq\".", + "5 <= word.length <= 2 * 105", + "word consists only of lowercase English letters.", + "0 <= k <= word.length - 5" + ], + "python_template": "class Solution(object):\n def countOfSubstrings(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countOfSubstrings(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "countOfSubstrings" + } +} \ No newline at end of file diff --git a/count-operations-to-obtain-zero.json b/count-operations-to-obtain-zero.json new file mode 100644 index 0000000000000000000000000000000000000000..df4f735431a67910358b45e86220220d2cf2ebaf --- /dev/null +++ b/count-operations-to-obtain-zero.json @@ -0,0 +1,29 @@ +{ + "id": 2288, + "name": "count-operations-to-obtain-zero", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-operations-to-obtain-zero/", + "date": "2022-02-06", + "task_description": "You are given two **non-negative** integers `num1` and `num2`. In one **operation**, if `num1 >= num2`, you must subtract `num2` from `num1`, otherwise subtract `num1` from `num2`. For example, if `num1 = 5` and `num2 = 4`, subtract `num2` from `num1`, thus obtaining `num1 = 1` and `num2 = 4`. However, if `num1 = 4` and `num2 = 5`, after one operation, `num1 = 4` and `num2 = 1`. Return _the **number of operations** required to make either_ `num1 = 0` _or_ `num2 = 0`. **Example 1:** ``` **Input:** num1 = 2, num2 = 3 **Output:** 3 **Explanation:** - Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1. - Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1. - Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1. Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations. So the total number of operations required is 3. ``` **Example 2:** ``` **Input:** num1 = 10, num2 = 10 **Output:** 1 **Explanation:** - Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0. Now num1 = 0 and num2 = 10. Since num1 == 0, we are done. So the total number of operations required is 1. ``` **Constraints:** `0 <= num1, num2 <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "num1 = 2, num2 = 3", + "output": "3 " + }, + { + "label": "Example 2", + "input": "num1 = 10, num2 = 10", + "output": "1 " + } + ], + "constraints": [ + "For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1.", + "0 <= num1, num2 <= 105" + ], + "python_template": "class Solution(object):\n def countOperations(self, num1, num2):\n \"\"\"\n :type num1: int\n :type num2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countOperations(int num1, int num2) {\n \n }\n}", + "metadata": { + "func_name": "countOperations" + } +} \ No newline at end of file diff --git a/count-pairs-of-similar-strings.json b/count-pairs-of-similar-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..111a40e897e9bcf0660117ae09a98bf0e102d44c --- /dev/null +++ b/count-pairs-of-similar-strings.json @@ -0,0 +1,37 @@ +{ + "id": 2594, + "name": "count-pairs-of-similar-strings", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-pairs-of-similar-strings/", + "date": "2022-12-11", + "task_description": "You are given a **0-indexed** string array `words`. Two strings are **similar** if they consist of the same characters. For example, `\"abca\"` and `\"cba\"` are similar since both consist of characters `'a'`, `'b'`, and `'c'`. However, `\"abacba\"` and `\"bcfd\"` are not similar since they do not consist of the same characters. Return _the number of pairs _`(i, j)`_ such that _`0 <= i < j <= word.length - 1`_ and the two strings _`words[i]`_ and _`words[j]`_ are similar_. **Example 1:** ``` **Input:** words = [\"aba\",\"aabb\",\"abcd\",\"bac\",\"aabc\"] **Output:** 2 **Explanation:** There are 2 pairs that satisfy the conditions: - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'. - i = 3 and j = 4 : both words[3] and words[4] only consist of characters 'a', 'b', and 'c'. ``` **Example 2:** ``` **Input:** words = [\"aabb\",\"ab\",\"ba\"] **Output:** 3 **Explanation:** There are 3 pairs that satisfy the conditions: - i = 0 and j = 1 : both words[0] and words[1] only consist of characters 'a' and 'b'. - i = 0 and j = 2 : both words[0] and words[2] only consist of characters 'a' and 'b'. - i = 1 and j = 2 : both words[1] and words[2] only consist of characters 'a' and 'b'. ``` **Example 3:** ``` **Input:** words = [\"nba\",\"cba\",\"dba\"] **Output:** 0 **Explanation:** Since there does not exist any pair that satisfies the conditions, we return 0. ``` **Constraints:** `1 <= words.length <= 100` `1 <= words[i].length <= 100` `words[i]` consist of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"aba\",\"aabb\",\"abcd\",\"bac\",\"aabc\"]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "words = [\"aabb\",\"ab\",\"ba\"]", + "output": "3 " + }, + { + "label": "Example 3", + "input": "words = [\"nba\",\"cba\",\"dba\"]", + "output": "0 " + } + ], + "constraints": [ + "For example, \"abca\" and \"cba\" are similar since both consist of characters 'a', 'b', and 'c'.", + "However, \"abacba\" and \"bcfd\" are not similar since they do not consist of the same characters.", + "1 <= words.length <= 100", + "1 <= words[i].length <= 100", + "words[i] consist of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def similarPairs(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int similarPairs(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "similarPairs" + } +} \ No newline at end of file diff --git a/count-pairs-that-form-a-complete-day-i.json b/count-pairs-that-form-a-complete-day-i.json new file mode 100644 index 0000000000000000000000000000000000000000..65c5444e13278b8fe91a749710226202eb5d8b34 --- /dev/null +++ b/count-pairs-that-form-a-complete-day-i.json @@ -0,0 +1,29 @@ +{ + "id": 3421, + "name": "count-pairs-that-form-a-complete-day-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i/", + "date": "2024-06-09", + "task_description": "Given an integer array `hours` representing times in **hours**, return an integer denoting the number of pairs `i`, `j` where `i < j` and `hours[i] + hours[j]` forms a **complete day**. A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours. For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on. **Example 1:** **Input:** hours = [12,12,30,24,24] **Output:** 2 **Explanation:** The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`. **Example 2:** **Input:** hours = [72,48,24,3] **Output:** 3 **Explanation:** The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`. **Constraints:** `1 <= hours.length <= 100` `1 <= hours[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "hours = [12,12,30,24,24]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "hours = [72,48,24,3]", + "output": "3 " + } + ], + "constraints": [ + "1 <= hours.length <= 100", + "1 <= hours[i] <= 109" + ], + "python_template": "class Solution(object):\n def countCompleteDayPairs(self, hours):\n \"\"\"\n :type hours: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countCompleteDayPairs(int[] hours) {\n \n }\n}", + "metadata": { + "func_name": "countCompleteDayPairs" + } +} \ No newline at end of file diff --git a/count-pairs-that-form-a-complete-day-ii.json b/count-pairs-that-form-a-complete-day-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..8fcd3cb5a8292e3b00f7963861857133d63f68f6 --- /dev/null +++ b/count-pairs-that-form-a-complete-day-ii.json @@ -0,0 +1,29 @@ +{ + "id": 3418, + "name": "count-pairs-that-form-a-complete-day-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-pairs-that-form-a-complete-day-ii/", + "date": "2024-06-09", + "task_description": "Given an integer array `hours` representing times in **hours**, return an integer denoting the number of pairs `i`, `j` where `i < j` and `hours[i] + hours[j]` forms a **complete day**. A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours. For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on. **Example 1:** **Input:** hours = [12,12,30,24,24] **Output:** 2 **Explanation:** The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`. **Example 2:** **Input:** hours = [72,48,24,3] **Output:** 3 **Explanation:** The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`. **Constraints:** `1 <= hours.length <= 5 * 105` `1 <= hours[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "hours = [12,12,30,24,24]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "hours = [72,48,24,3]", + "output": "3 " + } + ], + "constraints": [ + "1 <= hours.length <= 5 * 105", + "1 <= hours[i] <= 109" + ], + "python_template": "class Solution(object):\n def countCompleteDayPairs(self, hours):\n \"\"\"\n :type hours: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countCompleteDayPairs(int[] hours) {\n \n }\n}", + "metadata": { + "func_name": "countCompleteDayPairs" + } +} \ No newline at end of file diff --git a/count-pairs-whose-sum-is-less-than-target.json b/count-pairs-whose-sum-is-less-than-target.json new file mode 100644 index 0000000000000000000000000000000000000000..2a8776f231221f5b987a1d830841ef19a530c3ed --- /dev/null +++ b/count-pairs-whose-sum-is-less-than-target.json @@ -0,0 +1,29 @@ +{ + "id": 2917, + "name": "count-pairs-whose-sum-is-less-than-target", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/", + "date": "2023-08-05", + "task_description": "Given a **0-indexed** integer array `nums` of length `n` and an integer `target`, return _the number of pairs_ `(i, j)` _where_ `0 <= i < j < n` _and_ `nums[i] + nums[j] < target`. **Example 1:** ``` **Input:** nums = [-1,1,2,3,1], target = 2 **Output:** 3 **Explanation:** There are 3 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target - (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target. ``` **Example 2:** ``` **Input:** nums = [-6,2,5,-2,-7,-1,3], target = -2 **Output:** 10 **Explanation:** There are 10 pairs of indices that satisfy the conditions in the statement: - (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target - (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target - (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target - (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target - (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target - (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target - (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target - (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target - (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target - (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target ``` **Constraints:** `1 <= nums.length == n <= 50` `-50 <= nums[i], target <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [-1,1,2,3,1], target = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [-6,2,5,-2,-7,-1,3], target = -2", + "output": "10 " + } + ], + "constraints": [ + "1 <= nums.length == n <= 50", + "-50 <= nums[i], target <= 50" + ], + "python_template": "class Solution(object):\n def countPairs(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 countPairs(List nums, int target) {\n \n }\n}", + "metadata": { + "func_name": "countPairs" + } +} \ No newline at end of file diff --git a/count-palindromic-subsequences.json b/count-palindromic-subsequences.json new file mode 100644 index 0000000000000000000000000000000000000000..89a909d16761817ef48bb97c3bbb2a5baab5e9ad --- /dev/null +++ b/count-palindromic-subsequences.json @@ -0,0 +1,36 @@ +{ + "id": 2577, + "name": "count-palindromic-subsequences", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-palindromic-subsequences/", + "date": "2022-11-12", + "task_description": "Given a string of digits `s`, return _the number of **palindromic subsequences** of_ `s`_ having length _`5`. Since the answer may be very large, return it **modulo** `109 + 7`. **Note:** A string is **palindromic** if it reads the same forward and backward. A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. **Example 1:** ``` **Input:** s = \"103301\" **Output:** 2 **Explanation:** There are 6 possible subsequences of length 5: \"10330\",\"10331\",\"10301\",\"10301\",\"13301\",\"03301\". Two of them (both equal to \"10301\") are palindromic. ``` **Example 2:** ``` **Input:** s = \"0000000\" **Output:** 21 **Explanation:** All 21 subsequences are \"00000\", which is palindromic. ``` **Example 3:** ``` **Input:** s = \"9999900000\" **Output:** 2 **Explanation:** The only two palindromic subsequences are \"99999\" and \"00000\". ``` **Constraints:** `1 <= s.length <= 104` `s` consists of digits.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"103301\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"0000000\"", + "output": "21 " + }, + { + "label": "Example 3", + "input": "s = \"9999900000\"", + "output": "2 " + } + ], + "constraints": [ + "A string is palindromic if it reads the same forward and backward.", + "A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.", + "1 <= s.length <= 104", + "s consists of digits." + ], + "python_template": "class Solution(object):\n def countPalindromes(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countPalindromes(String s) {\n \n }\n}", + "metadata": { + "func_name": "countPalindromes" + } +} \ No newline at end of file diff --git a/count-partitions-with-even-sum-difference.json b/count-partitions-with-even-sum-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..caf4f5e2812757c646d4e88c686598cb2bffcc43 --- /dev/null +++ b/count-partitions-with-even-sum-difference.json @@ -0,0 +1,40 @@ +{ + "id": 3704, + "name": "count-partitions-with-even-sum-difference", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-partitions-with-even-sum-difference/", + "date": "2025-01-19", + "task_description": "You are given an integer array `nums` of length `n`. A **partition** is defined as an index `i` where `0 <= i < n - 1`, splitting the array into two **non-empty** subarrays such that: Left subarray contains indices `[0, i]`. Right subarray contains indices `[i + 1, n - 1]`. Return the number of **partitions** where the **difference** between the **sum** of the left and right subarrays is **even**. **Example 1:** **Input:** nums = [10,10,3,7,6] **Output:** 4 **Explanation:** The 4 partitions are: `[10]`, `[10, 3, 7, 6]` with a sum difference of `10 - 26 = -16`, which is even. `[10, 10]`, `[3, 7, 6]` with a sum difference of `20 - 16 = 4`, which is even. `[10, 10, 3]`, `[7, 6]` with a sum difference of `23 - 13 = 10`, which is even. `[10, 10, 3, 7]`, `[6]` with a sum difference of `30 - 6 = 24`, which is even. **Example 2:** **Input:** nums = [1,2,2] **Output:** 0 **Explanation:** No partition results in an even sum difference. **Example 3:** **Input:** nums = [2,4,6,8] **Output:** 3 **Explanation:** All partitions result in an even sum difference. **Constraints:** `2 <= n == nums.length <= 100` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [10,10,3,7,6]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,2]", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [2,4,6,8]", + "output": "3 " + } + ], + "constraints": [ + "Left subarray contains indices [0, i].", + "Right subarray contains indices [i + 1, n - 1].", + "[10], [10, 3, 7, 6] with a sum difference of 10 - 26 = -16, which is even.", + "[10, 10], [3, 7, 6] with a sum difference of 20 - 16 = 4, which is even.", + "[10, 10, 3], [7, 6] with a sum difference of 23 - 13 = 10, which is even.", + "[10, 10, 3, 7], [6] with a sum difference of 30 - 6 = 24, which is even.", + "2 <= n == nums.length <= 100", + "1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def countPartitions(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countPartitions(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "countPartitions" + } +} \ No newline at end of file diff --git a/count-paths-that-can-form-a-palindrome-in-a-tree.json b/count-paths-that-can-form-a-palindrome-in-a-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..4f89d7d9780556120edcc5114cdf75ff86748713 --- /dev/null +++ b/count-paths-that-can-form-a-palindrome-in-a-tree.json @@ -0,0 +1,33 @@ +{ + "id": 2905, + "name": "count-paths-that-can-form-a-palindrome-in-a-tree", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-paths-that-can-form-a-palindrome-in-a-tree/", + "date": "2023-07-16", + "task_description": "You are given a **tree** (i.e. a connected, undirected graph that has no cycles) **rooted** at node `0` consisting of `n` nodes numbered from `0` to `n - 1`. The tree is represented by a **0-indexed** array `parent` of size `n`, where `parent[i]` is the parent of node `i`. Since node `0` is the root, `parent[0] == -1`. You are also given a string `s` of length `n`, where `s[i]` is the character assigned to the edge between `i` and `parent[i]`. `s[0]` can be ignored. Return _the number of pairs of nodes _`(u, v)`_ such that _`u < v`_ and the characters assigned to edges on the path from _`u`_ to _`v`_ can be **rearranged** to form a **palindrome**_. A string is a **palindrome** when it reads the same backwards as forwards. **Example 1:** ``` **Input:** parent = [-1,0,0,1,1,2], s = \"acaabc\" **Output:** 8 **Explanation:** The valid pairs are: - All the pairs (0,1), (0,2), (1,3), (1,4) and (2,5) result in one character which is always a palindrome. - The pair (2,3) result in the string \"aca\" which is a palindrome. - The pair (1,5) result in the string \"cac\" which is a palindrome. - The pair (3,5) result in the string \"acac\" which can be rearranged into the palindrome \"acca\". ``` **Example 2:** ``` **Input:** parent = [-1,0,0,0,0], s = \"aaaaa\" **Output:** 10 **Explanation:** Any pair of nodes (u,v) where u < v is valid. ``` **Constraints:** `n == parent.length == s.length` `1 <= n <= 105` `0 <= parent[i] <= n - 1` for all `i >= 1` `parent[0] == -1` `parent` represents a valid tree. `s` consists of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "parent = [-1,0,0,1,1,2], s = \"acaabc\"", + "output": "8 " + }, + { + "label": "Example 2", + "input": "parent = [-1,0,0,0,0], s = \"aaaaa\"", + "output": "10 " + } + ], + "constraints": [ + "n == parent.length == s.length", + "1 <= n <= 105", + "0 <= parent[i] <= n - 1 for all i >= 1", + "parent[0] == -1", + "parent represents a valid tree.", + "s consists of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def countPalindromePaths(self, parent, s):\n \"\"\"\n :type parent: List[int]\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countPalindromePaths(List parent, String s) {\n \n }\n}", + "metadata": { + "func_name": "countPalindromePaths" + } +} \ No newline at end of file diff --git a/count-paths-with-the-given-xor-value.json b/count-paths-with-the-given-xor-value.json new file mode 100644 index 0000000000000000000000000000000000000000..ac85f150e92d086f4b9eb67095d187ae5e93d1d2 --- /dev/null +++ b/count-paths-with-the-given-xor-value.json @@ -0,0 +1,46 @@ +{ + "id": 3659, + "name": "count-paths-with-the-given-xor-value", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-paths-with-the-given-xor-value/", + "date": "2024-12-07", + "task_description": "You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`. Your task is to calculate the number of paths you can take from the top-left cell `(0, 0)` to the bottom-right cell `(m - 1, n - 1)` satisfying the following **constraints**: You can either move to the right or down. Formally, from the cell `(i, j)` you may move to the cell `(i, j + 1)` or to the cell `(i + 1, j)` if the target cell _exists_. The `XOR` of all the numbers on the path must be **equal** to `k`. Return the total number of such paths. Since the answer can be very large, return the result **modulo** `109 + 7`. **Example 1:** **Input:** grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11 **Output:** 3 **Explanation:** The 3 paths are: `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)` `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)` `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)` **Example 2:** **Input:** grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2 **Output:** 5 **Explanation:** The 5 paths are: `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)` `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)` `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)` `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)` `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)` **Example 3:** **Input:** grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10 **Output:** 0 **Constraints:** `1 <= m == grid.length <= 300` `1 <= n == grid[r].length <= 300` `0 <= grid[r][c] < 16` `0 <= k < 16`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11", + "output": "3 " + }, + { + "label": "Example 2", + "input": "grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2", + "output": "5 " + }, + { + "label": "Example 3", + "input": "grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10", + "output": "" + } + ], + "constraints": [ + "You can either move to the right or down. Formally, from the cell (i, j) you may move to the cell (i, j + 1) or to the cell (i + 1, j) if the target cell exists.", + "The XOR of all the numbers on the path must be equal to k.", + "(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)", + "(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)", + "(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)", + "(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)", + "(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)", + "(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)", + "(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)", + "(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)", + "1 <= m == grid.length <= 300", + "1 <= n == grid[r].length <= 300", + "0 <= grid[r][c] < 16", + "0 <= k < 16" + ], + "python_template": "class Solution(object):\n def countPathsWithXorValue(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 countPathsWithXorValue(int[][] grid, int k) {\n \n }\n}", + "metadata": { + "func_name": "countPathsWithXorValue" + } +} \ No newline at end of file diff --git a/count-prefix-and-suffix-pairs-i.json b/count-prefix-and-suffix-pairs-i.json new file mode 100644 index 0000000000000000000000000000000000000000..bf6382f47ab9204ef38e4188867fb8cb3545fac8 --- /dev/null +++ b/count-prefix-and-suffix-pairs-i.json @@ -0,0 +1,36 @@ +{ + "id": 3309, + "name": "count-prefix-and-suffix-pairs-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/", + "date": "2024-02-11", + "task_description": "You are given a **0-indexed** string array `words`. Let's define a **boolean** function `isPrefixAndSuffix` that takes two strings, `str1` and `str2`: `isPrefixAndSuffix(str1, str2)` returns `true` if `str1` is **both** a prefix and a suffix of `str2`, and `false` otherwise. For example, `isPrefixAndSuffix(\"aba\", \"ababa\")` is `true` because `\"aba\"` is a prefix of `\"ababa\"` and also a suffix, but `isPrefixAndSuffix(\"abc\", \"abcd\")` is `false`. Return _an integer denoting the **number** of index pairs _`(i, j)`_ such that _`i < j`_, and _`isPrefixAndSuffix(words[i], words[j])`_ is _`true`_._ **Example 1:** ``` **Input:** words = [\"a\",\"aba\",\"ababa\",\"aa\"] **Output:** 4 **Explanation:** In this example, the counted index pairs are: i = 0 and j = 1 because isPrefixAndSuffix(\"a\", \"aba\") is true. i = 0 and j = 2 because isPrefixAndSuffix(\"a\", \"ababa\") is true. i = 0 and j = 3 because isPrefixAndSuffix(\"a\", \"aa\") is true. i = 1 and j = 2 because isPrefixAndSuffix(\"aba\", \"ababa\") is true. Therefore, the answer is 4. ``` **Example 2:** ``` **Input:** words = [\"pa\",\"papa\",\"ma\",\"mama\"] **Output:** 2 **Explanation:** In this example, the counted index pairs are: i = 0 and j = 1 because isPrefixAndSuffix(\"pa\", \"papa\") is true. i = 2 and j = 3 because isPrefixAndSuffix(\"ma\", \"mama\") is true. Therefore, the answer is 2. ``` **Example 3:** ``` **Input:** words = [\"abab\",\"ab\"] **Output:** 0 **Explanation: **In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix(\"abab\", \"ab\") is false. Therefore, the answer is 0. ``` **Constraints:** `1 <= words.length <= 50` `1 <= words[i].length <= 10` `words[i]` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"a\",\"aba\",\"ababa\",\"aa\"]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "words = [\"pa\",\"papa\",\"ma\",\"mama\"]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "words = [\"abab\",\"ab\"]", + "output": "0 " + } + ], + "constraints": [ + "isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of str2, and false otherwise.", + "1 <= words.length <= 50", + "1 <= words[i].length <= 10", + "words[i] consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def countPrefixSuffixPairs(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countPrefixSuffixPairs(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "countPrefixSuffixPairs" + } +} \ No newline at end of file diff --git a/count-special-subsequences.json b/count-special-subsequences.json new file mode 100644 index 0000000000000000000000000000000000000000..490c3e548b85633755c1996b5d80a8f769c156db --- /dev/null +++ b/count-special-subsequences.json @@ -0,0 +1,59 @@ +{ + "id": 3699, + "name": "count-special-subsequences", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-special-subsequences/", + "date": "2024-12-22", + "task_description": "You are given an array `nums` consisting of positive integers. A **special subsequence** is defined as a subsequence of length 4, represented by indices `(p, q, r, s)`, where `p < q < r < s`. This subsequence **must** satisfy the following conditions: `nums[p] * nums[r] == nums[q] * nums[s]` There must be _at least_ **one** element between each pair of indices. In other words, `q - p > 1`, `r - q > 1` and `s - r > 1`. Return the _number_ of different **special** **subsequences** in `nums`. **Example 1:** **Input:** nums = [1,2,3,4,3,6,1] **Output:** 1 **Explanation:** There is one special subsequence in `nums`. `(p, q, r, s) = (0, 2, 4, 6)`: This corresponds to elements `(1, 3, 3, 1)`. `nums[p] * nums[r] = nums[0] * nums[4] = 1 * 3 = 3` `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 1 = 3` **Example 2:** **Input:** nums = [3,4,3,4,3,4,3,4] **Output:** 3 **Explanation:** There are three special subsequences in `nums`. `(p, q, r, s) = (0, 2, 4, 6)`: This corresponds to elements `(3, 3, 3, 3)`. `nums[p] * nums[r] = nums[0] * nums[4] = 3 * 3 = 9` `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 3 = 9` `(p, q, r, s) = (1, 3, 5, 7)`: This corresponds to elements `(4, 4, 4, 4)`. `nums[p] * nums[r] = nums[1] * nums[5] = 4 * 4 = 16` `nums[q] * nums[s] = nums[3] * nums[7] = 4 * 4 = 16` `(p, q, r, s) = (0, 2, 5, 7)`: This corresponds to elements `(3, 3, 4, 4)`. `nums[p] * nums[r] = nums[0] * nums[5] = 3 * 4 = 12` `nums[q] * nums[s] = nums[2] * nums[7] = 3 * 4 = 12` **Constraints:** `7 <= nums.length <= 1000` `1 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,3,6,1]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [3,4,3,4,3,4,3,4]", + "output": "3 " + } + ], + "constraints": [ + "nums[p] * nums[r] == nums[q] * nums[s]", + "There must be at least one element between each pair of indices. In other words, q - p > 1, r - q > 1 and s - r > 1.", + "(p, q, r, s) = (0, 2, 4, 6):\n\n\t\nThis corresponds to elements (1, 3, 3, 1).\nnums[p] * nums[r] = nums[0] * nums[4] = 1 * 3 = 3\nnums[q] * nums[s] = nums[2] * nums[6] = 3 * 1 = 3", + "This corresponds to elements (1, 3, 3, 1).", + "nums[p] * nums[r] = nums[0] * nums[4] = 1 * 3 = 3", + "nums[q] * nums[s] = nums[2] * nums[6] = 3 * 1 = 3", + "This corresponds to elements (1, 3, 3, 1).", + "nums[p] * nums[r] = nums[0] * nums[4] = 1 * 3 = 3", + "nums[q] * nums[s] = nums[2] * nums[6] = 3 * 1 = 3", + "(p, q, r, s) = (0, 2, 4, 6):\n\n\t\nThis corresponds to elements (3, 3, 3, 3).\nnums[p] * nums[r] = nums[0] * nums[4] = 3 * 3 = 9\nnums[q] * nums[s] = nums[2] * nums[6] = 3 * 3 = 9", + "This corresponds to elements (3, 3, 3, 3).", + "nums[p] * nums[r] = nums[0] * nums[4] = 3 * 3 = 9", + "nums[q] * nums[s] = nums[2] * nums[6] = 3 * 3 = 9", + "(p, q, r, s) = (1, 3, 5, 7):\n\t\nThis corresponds to elements (4, 4, 4, 4).\nnums[p] * nums[r] = nums[1] * nums[5] = 4 * 4 = 16\nnums[q] * nums[s] = nums[3] * nums[7] = 4 * 4 = 16", + "This corresponds to elements (4, 4, 4, 4).", + "nums[p] * nums[r] = nums[1] * nums[5] = 4 * 4 = 16", + "nums[q] * nums[s] = nums[3] * nums[7] = 4 * 4 = 16", + "(p, q, r, s) = (0, 2, 5, 7):\n\t\nThis corresponds to elements (3, 3, 4, 4).\nnums[p] * nums[r] = nums[0] * nums[5] = 3 * 4 = 12\nnums[q] * nums[s] = nums[2] * nums[7] = 3 * 4 = 12", + "This corresponds to elements (3, 3, 4, 4).", + "nums[p] * nums[r] = nums[0] * nums[5] = 3 * 4 = 12", + "nums[q] * nums[s] = nums[2] * nums[7] = 3 * 4 = 12", + "This corresponds to elements (3, 3, 3, 3).", + "nums[p] * nums[r] = nums[0] * nums[4] = 3 * 3 = 9", + "nums[q] * nums[s] = nums[2] * nums[6] = 3 * 3 = 9", + "This corresponds to elements (4, 4, 4, 4).", + "nums[p] * nums[r] = nums[1] * nums[5] = 4 * 4 = 16", + "nums[q] * nums[s] = nums[3] * nums[7] = 4 * 4 = 16", + "This corresponds to elements (3, 3, 4, 4).", + "nums[p] * nums[r] = nums[0] * nums[5] = 3 * 4 = 12", + "nums[q] * nums[s] = nums[2] * nums[7] = 3 * 4 = 12", + "7 <= nums.length <= 1000", + "1 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def numberOfSubsequences(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long numberOfSubsequences(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "numberOfSubsequences" + } +} \ No newline at end of file diff --git a/count-stepping-numbers-in-range.json b/count-stepping-numbers-in-range.json new file mode 100644 index 0000000000000000000000000000000000000000..a33535aed59e2d5cf8bd42268292be828c6968f1 --- /dev/null +++ b/count-stepping-numbers-in-range.json @@ -0,0 +1,31 @@ +{ + "id": 2921, + "name": "count-stepping-numbers-in-range", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-stepping-numbers-in-range/", + "date": "2023-07-23", + "task_description": "Given two positive integers `low` and `high` represented as strings, find the count of **stepping numbers** in the inclusive range `[low, high]`. A **stepping number** is an integer such that all of its adjacent digits have an absolute difference of **exactly** `1`. Return _an integer denoting the count of stepping numbers in the inclusive range_ `[low, high]`_. _ Since the answer may be very large, return it **modulo** `109 + 7`. **Note:** A stepping number should not have a leading zero. **Example 1:** ``` **Input:** low = \"1\", high = \"11\" **Output:** 10 **Explanation: **The stepping numbers in the range [1,11] are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. There are a total of 10 stepping numbers in the range. Hence, the output is 10. ``` **Example 2:** ``` **Input:** low = \"90\", high = \"101\" **Output:** 2 **Explanation: **The stepping numbers in the range [90,101] are 98 and 101. There are a total of 2 stepping numbers in the range. Hence, the output is 2. ``` **Constraints:** `1 <= int(low) <= int(high) < 10100` `1 <= low.length, high.length <= 100` `low` and `high` consist of only digits. `low` and `high` don't have any leading zeros.", + "test_case": [ + { + "label": "Example 1", + "input": "low = \"1\", high = \"11\"", + "output": "10 " + }, + { + "label": "Example 2", + "input": "low = \"90\", high = \"101\"", + "output": "2 " + } + ], + "constraints": [ + "1 <= int(low) <= int(high) < 10100", + "1 <= low.length, high.length <= 100", + "low and high consist of only digits.", + "low and high don't have any leading zeros." + ], + "python_template": "class Solution(object):\n def countSteppingNumbers(self, low, high):\n \"\"\"\n :type low: str\n :type high: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countSteppingNumbers(String low, String high) {\n \n }\n}", + "metadata": { + "func_name": "countSteppingNumbers" + } +} \ No newline at end of file diff --git a/count-subarrays-of-length-three-with-a-condition.json b/count-subarrays-of-length-three-with-a-condition.json new file mode 100644 index 0000000000000000000000000000000000000000..6b128d3fbf3629aedba9f29a2df8bd79f892d3d9 --- /dev/null +++ b/count-subarrays-of-length-three-with-a-condition.json @@ -0,0 +1,29 @@ +{ + "id": 3685, + "name": "count-subarrays-of-length-three-with-a-condition", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/", + "date": "2024-12-07", + "task_description": "Given an integer array `nums`, return the number of subarrays of length 3 such that the sum of the first and third numbers equals _exactly_ half of the second number. **Example 1:** **Input:** nums = [1,2,1,4,1] **Output:** 1 **Explanation:** Only the subarray `[1,4,1]` contains exactly 3 elements where the sum of the first and third numbers equals half the middle number. **Example 2:** **Input:** nums = [1,1,1] **Output:** 0 **Explanation:** `[1,1,1]` is the only subarray of length 3. However, its first and third numbers do not add to half the middle number. **Constraints:** `3 <= nums.length <= 100` `-100 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1,4,1]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1]", + "output": "0 " + } + ], + "constraints": [ + "3 <= nums.length <= 100", + "-100 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def countSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "countSubarrays" + } +} \ No newline at end of file diff --git a/count-subarrays-where-max-element-appears-at-least-k-times.json b/count-subarrays-where-max-element-appears-at-least-k-times.json new file mode 100644 index 0000000000000000000000000000000000000000..2f413ae3be22ff779aa41ed7124c53955e415004 --- /dev/null +++ b/count-subarrays-where-max-element-appears-at-least-k-times.json @@ -0,0 +1,30 @@ +{ + "id": 3213, + "name": "count-subarrays-where-max-element-appears-at-least-k-times", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/", + "date": "2023-12-03", + "task_description": "You are given an integer array `nums` and a **positive** integer `k`. Return _the number of subarrays where the **maximum** element of _`nums`_ appears **at least** _`k`_ times in that subarray._ A **subarray** is a contiguous sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,3,2,3,3], k = 2 **Output:** 6 **Explanation:** The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. ``` **Example 2:** ``` **Input:** nums = [1,4,2,1], k = 3 **Output:** 0 **Explanation:** No subarray contains the element 4 at least 3 times. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 106` `1 <= k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,2,3,3], k = 2", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [1,4,2,1], k = 3", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 106", + "1 <= k <= 105" + ], + "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/count-subarrays-with-fixed-bounds.json b/count-subarrays-with-fixed-bounds.json new file mode 100644 index 0000000000000000000000000000000000000000..5163e540a968da6e52b3dcd20e53b1913ce7ceb5 --- /dev/null +++ b/count-subarrays-with-fixed-bounds.json @@ -0,0 +1,31 @@ +{ + "id": 2527, + "name": "count-subarrays-with-fixed-bounds", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-subarrays-with-fixed-bounds/", + "date": "2022-10-09", + "task_description": "You are given an integer array `nums` and two integers `minK` and `maxK`. A **fixed-bound subarray** of `nums` is a subarray that satisfies the following conditions: The **minimum** value in the subarray is equal to `minK`. The **maximum** value in the subarray is equal to `maxK`. Return _the **number** of fixed-bound subarrays_. A **subarray** is a **contiguous** part of an array. **Example 1:** ``` **Input:** nums = [1,3,5,2,7,5], minK = 1, maxK = 5 **Output:** 2 **Explanation:** The fixed-bound subarrays are [1,3,5] and [1,3,5,2]. ``` **Example 2:** ``` **Input:** nums = [1,1,1,1], minK = 1, maxK = 1 **Output:** 10 **Explanation:** Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays. ``` **Constraints:** `2 <= nums.length <= 105` `1 <= nums[i], minK, maxK <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,5,2,7,5], minK = 1, maxK = 5", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1,1], minK = 1, maxK = 1", + "output": "10 " + } + ], + "constraints": [ + "The minimum value in the subarray is equal to minK.", + "The maximum value in the subarray is equal to maxK.", + "2 <= nums.length <= 105", + "1 <= nums[i], minK, maxK <= 106" + ], + "python_template": "class Solution(object):\n def countSubarrays(self, nums, minK, maxK):\n \"\"\"\n :type nums: List[int]\n :type minK: int\n :type maxK: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countSubarrays(int[] nums, int minK, int maxK) {\n \n }\n}", + "metadata": { + "func_name": "countSubarrays" + } +} \ No newline at end of file diff --git a/count-subarrays-with-score-less-than-k.json b/count-subarrays-with-score-less-than-k.json new file mode 100644 index 0000000000000000000000000000000000000000..1825384a5269691305d673add536e0a49504d488 --- /dev/null +++ b/count-subarrays-with-score-less-than-k.json @@ -0,0 +1,31 @@ +{ + "id": 2394, + "name": "count-subarrays-with-score-less-than-k", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-subarrays-with-score-less-than-k/", + "date": "2022-05-28", + "task_description": "The **score** of an array is defined as the **product** of its sum and its length. For example, the score of `[1, 2, 3, 4, 5]` is `(1 + 2 + 3 + 4 + 5) * 5 = 75`. Given a positive integer array `nums` and an integer `k`, return _the **number of non-empty subarrays** of_ `nums` _whose score is **strictly less** than_ `k`. A **subarray** is a contiguous sequence of elements within an array. **Example 1:** ``` **Input:** nums = [2,1,4,3,5], k = 10 **Output:** 6 **Explanation:** The 6 subarrays having scores less than 10 are: - [2] with score 2 * 1 = 2. - [1] with score 1 * 1 = 1. - [4] with score 4 * 1 = 4. - [3] with score 3 * 1 = 3. - [5] with score 5 * 1 = 5. - [2,1] with score (2 + 1) * 2 = 6. Note that subarrays such as [1,4] and [4,3,5] are not considered because their scores are 10 and 36 respectively, while we need scores strictly less than 10. ``` **Example 2:** ``` **Input:** nums = [1,1,1], k = 5 **Output:** 5 **Explanation:** Every subarray except [1,1,1] has a score less than 5. [1,1,1] has a score (1 + 1 + 1) * 3 = 9, which is greater than 5. Thus, there are 5 subarrays having scores less than 5. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105` `1 <= k <= 1015`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,4,3,5], k = 10", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1], k = 5", + "output": "5 " + } + ], + "constraints": [ + "For example, the score of [1, 2, 3, 4, 5] is (1 + 2 + 3 + 4 + 5) * 5 = 75.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105", + "1 <= k <= 1015" + ], + "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, long k) {\n \n }\n}", + "metadata": { + "func_name": "countSubarrays" + } +} \ No newline at end of file diff --git a/count-substrings-starting-and-ending-with-given-character.json b/count-substrings-starting-and-ending-with-given-character.json new file mode 100644 index 0000000000000000000000000000000000000000..85440d4011c647b6ff654ab33beef51eeca42ed3 --- /dev/null +++ b/count-substrings-starting-and-ending-with-given-character.json @@ -0,0 +1,29 @@ +{ + "id": 3337, + "name": "count-substrings-starting-and-ending-with-given-character", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character/", + "date": "2024-03-10", + "task_description": "You are given a string `s` and a character `c`. Return _the total number of substrings of _`s`_ that start and end with _`c`_._ **Example 1:** **Input: **s = \"abada\", c = \"a\" **Output: **6 **Explanation:** Substrings starting and ending with `\"a\"` are: `\"**a**bada\"`, `\"**aba**da\"`, `\"**abada**\"`, `\"ab**a**da\"`, `\"ab**ada**\"`, `\"abad**a**\"`. **Example 2:** **Input: **s = \"zzz\", c = \"z\" **Output: **6 **Explanation:** There are a total of `6` substrings in `s` and all start and end with `\"z\"`. **Constraints:** `1 <= s.length <= 105` `s` and `c` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abada\", c = \"a\"", + "output": "6 " + }, + { + "label": "Example 2", + "input": "s = \"zzz\", c = \"z\"", + "output": "6 " + } + ], + "constraints": [ + "1 <= s.length <= 105", + "s and c consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def countSubstrings(self, s, c):\n \"\"\"\n :type s: str\n :type c: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countSubstrings(String s, char c) {\n \n }\n}", + "metadata": { + "func_name": "countSubstrings" + } +} \ No newline at end of file diff --git a/count-substrings-that-can-be-rearranged-to-contain-a-string-ii.json b/count-substrings-that-can-be-rearranged-to-contain-a-string-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..f869edef0cc374a794477cf3295861fc39187d11 --- /dev/null +++ b/count-substrings-that-can-be-rearranged-to-contain-a-string-ii.json @@ -0,0 +1,35 @@ +{ + "id": 3572, + "name": "count-substrings-that-can-be-rearranged-to-contain-a-string-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-ii/", + "date": "2024-09-15", + "task_description": "You are given two strings `word1` and `word2`. A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix. Return the total number of **valid** substrings of `word1`. **Note** that the memory limits in this problem are **smaller** than usual, so you **must** implement a solution with a _linear_ runtime complexity. **Example 1:** **Input:** word1 = \"bcca\", word2 = \"abc\" **Output:** 1 **Explanation:** The only valid substring is `\"bcca\"` which can be rearranged to `\"abcc\"` having `\"abc\"` as a prefix. **Example 2:** **Input:** word1 = \"abcabc\", word2 = \"abc\" **Output:** 10 **Explanation:** All the substrings except substrings of size 1 and size 2 are valid. **Example 3:** **Input:** word1 = \"abcabc\", word2 = \"aaabc\" **Output:** 0 **Constraints:** `1 <= word1.length <= 106` `1 <= word2.length <= 104` `word1` and `word2` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word1 = \"bcca\", word2 = \"abc\"", + "output": "1 " + }, + { + "label": "Example 2", + "input": "word1 = \"abcabc\", word2 = \"abc\"", + "output": "10 " + }, + { + "label": "Example 3", + "input": "word1 = \"abcabc\", word2 = \"aaabc\"", + "output": "" + } + ], + "constraints": [ + "1 <= word1.length <= 106", + "1 <= word2.length <= 104", + "word1 and word2 consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def validSubstringCount(self, word1, word2):\n \"\"\"\n :type word1: str\n :type word2: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long validSubstringCount(String word1, String word2) {\n \n }\n}", + "metadata": { + "func_name": "validSubstringCount" + } +} \ No newline at end of file diff --git a/count-substrings-that-satisfy-k-constraint-i.json b/count-substrings-that-satisfy-k-constraint-i.json new file mode 100644 index 0000000000000000000000000000000000000000..93b7d914c34d3d913113e4e7cf47d75f45d19f27 --- /dev/null +++ b/count-substrings-that-satisfy-k-constraint-i.json @@ -0,0 +1,37 @@ +{ + "id": 3543, + "name": "count-substrings-that-satisfy-k-constraint-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-i/", + "date": "2024-08-11", + "task_description": "You are given a **binary** string `s` and an integer `k`. A **binary string** satisfies the **k-constraint** if **either** of the following conditions holds: The number of `0`'s in the string is at most `k`. The number of `1`'s in the string is at most `k`. Return an integer denoting the number of substrings of `s` that satisfy the **k-constraint**. **Example 1:** **Input:** s = \"10101\", k = 1 **Output:** 12 **Explanation:** Every substring of `s` except the substrings `\"1010\"`, `\"10101\"`, and `\"0101\"` satisfies the k-constraint. **Example 2:** **Input:** s = \"1010101\", k = 2 **Output:** 25 **Explanation:** Every substring of `s` except the substrings with a length greater than 5 satisfies the k-constraint. **Example 3:** **Input:** s = \"11111\", k = 1 **Output:** 15 **Explanation:** All substrings of `s` satisfy the k-constraint. **Constraints:** `1 <= s.length <= 50 ` `1 <= k <= s.length` `s[i]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"10101\", k = 1", + "output": "12 " + }, + { + "label": "Example 2", + "input": "s = \"1010101\", k = 2", + "output": "25 " + }, + { + "label": "Example 3", + "input": "s = \"11111\", k = 1", + "output": "15 " + } + ], + "constraints": [ + "The number of 0's in the string is at most k.", + "The number of 1's in the string is at most k.", + "1 <= s.length <= 50", + "1 <= k <= s.length", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def countKConstraintSubstrings(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countKConstraintSubstrings(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "countKConstraintSubstrings" + } +} \ No newline at end of file diff --git a/count-substrings-that-satisfy-k-constraint-ii.json b/count-substrings-that-satisfy-k-constraint-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..5a5789450847f132b99369ed504816558a1163e6 --- /dev/null +++ b/count-substrings-that-satisfy-k-constraint-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3546, + "name": "count-substrings-that-satisfy-k-constraint-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-ii/", + "date": "2024-08-11", + "task_description": "You are given a **binary** string `s` and an integer `k`. You are also given a 2D integer array `queries`, where `queries[i] = [li, ri]`. A **binary string** satisfies the **k-constraint** if **either** of the following conditions holds: The number of `0`'s in the string is at most `k`. The number of `1`'s in the string is at most `k`. Return an integer array `answer`, where `answer[i]` is the number of substrings of `s[li..ri]` that satisfy the **k-constraint**. **Example 1:** **Input:** s = \"0001111\", k = 2, queries = [[0,6]] **Output:** [26] **Explanation:** For the query `[0, 6]`, all substrings of `s[0..6] = \"0001111\"` satisfy the k-constraint except for the substrings `s[0..5] = \"000111\"` and `s[0..6] = \"0001111\"`. **Example 2:** **Input:** s = \"010101\", k = 1, queries = [[0,5],[1,4],[2,3]] **Output:** [15,9,3] **Explanation:** The substrings of `s` with a length greater than 3 do not satisfy the k-constraint. **Constraints:** `1 <= s.length <= 105` `s[i]` is either `'0'` or `'1'`. `1 <= k <= s.length` `1 <= queries.length <= 105` `queries[i] == [li, ri]` `0 <= li <= ri < s.length` All queries are distinct.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"0001111\", k = 2, queries = [[0,6]]", + "output": "[26] " + }, + { + "label": "Example 2", + "input": "s = \"010101\", k = 1, queries = [[0,5],[1,4],[2,3]]", + "output": "[15,9,3] " + } + ], + "constraints": [ + "The number of 0's in the string is at most k.", + "The number of 1's in the string is at most k.", + "1 <= s.length <= 105", + "s[i] is either '0' or '1'.", + "1 <= k <= s.length", + "1 <= queries.length <= 105", + "queries[i] == [li, ri]", + "0 <= li <= ri < s.length", + "All queries are distinct." + ], + "python_template": "class Solution(object):\n def countKConstraintSubstrings(self, s, k, queries):\n \"\"\"\n :type s: str\n :type k: int\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] countKConstraintSubstrings(String s, int k, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "countKConstraintSubstrings" + } +} \ No newline at end of file diff --git a/count-tested-devices-after-test-operations.json b/count-tested-devices-after-test-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..01777a6a3edc7a5155f11b5d5bea1223c9f5ec54 --- /dev/null +++ b/count-tested-devices-after-test-operations.json @@ -0,0 +1,37 @@ +{ + "id": 3220, + "name": "count-tested-devices-after-test-operations", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-tested-devices-after-test-operations/", + "date": "2023-12-03", + "task_description": "You are given a **0-indexed** integer array `batteryPercentages` having length `n`, denoting the battery percentages of `n` **0-indexed** devices. Your task is to test each device `i` **in order** from `0` to `n - 1`, by performing the following test operations: If `batteryPercentages[i]` is **greater** than `0`: **Increment** the count of tested devices. **Decrease** the battery percentage of all devices with indices `j` in the range `[i + 1, n - 1]` by `1`, ensuring their battery percentage **never goes below** `0`, i.e, `batteryPercentages[j] = max(0, batteryPercentages[j] - 1)`. Move to the next device. Otherwise, move to the next device without performing any test. Return _an integer denoting the number of devices that will be tested after performing the test operations in order._ **Example 1:** ``` **Input:** batteryPercentages = [1,1,2,1,3] **Output:** 3 **Explanation: **Performing the test operations in order starting from device 0: At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2]. At device 1, batteryPercentages[1] == 0, so we move to the next device without testing. At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1]. At device 3, batteryPercentages[3] == 0, so we move to the next device without testing. At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same. So, the answer is 3. ``` **Example 2:** ``` **Input:** batteryPercentages = [0,1,2] **Output:** 2 **Explanation:** Performing the test operations in order starting from device 0: At device 0, batteryPercentages[0] == 0, so we move to the next device without testing. At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1]. At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same. So, the answer is 2. ``` **Constraints:** `1 <= n == batteryPercentages.length <= 100 ` `0 <= batteryPercentages[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "batteryPercentages = [1,1,2,1,3]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "batteryPercentages = [0,1,2]", + "output": "2 " + } + ], + "constraints": [ + "If batteryPercentages[i] is greater than 0:\n\n\t\nIncrement the count of tested devices.\nDecrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1).\nMove to the next device.", + "Increment the count of tested devices.", + "Decrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1).", + "Move to the next device.", + "Otherwise, move to the next device without performing any test.", + "Increment the count of tested devices.", + "Decrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1).", + "Move to the next device.", + "1 <= n == batteryPercentages.length <= 100", + "0 <= batteryPercentages[i] <= 100" + ], + "python_template": "class Solution(object):\n def countTestedDevices(self, batteryPercentages):\n \"\"\"\n :type batteryPercentages: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countTestedDevices(int[] batteryPercentages) {\n \n }\n}", + "metadata": { + "func_name": "countTestedDevices" + } +} \ No newline at end of file diff --git a/count-the-hidden-sequences.json b/count-the-hidden-sequences.json new file mode 100644 index 0000000000000000000000000000000000000000..4b04babb69091756ad08ed888ddd251b4f338bdd --- /dev/null +++ b/count-the-hidden-sequences.json @@ -0,0 +1,43 @@ +{ + "id": 2249, + "name": "count-the-hidden-sequences", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-hidden-sequences/", + "date": "2022-01-08", + "task_description": "You are given a **0-indexed** array of `n` integers `differences`, which describes the **differences **between each pair of **consecutive **integers of a **hidden** sequence of length `(n + 1)`. More formally, call the hidden sequence `hidden`, then we have that `differences[i] = hidden[i + 1] - hidden[i]`. You are further given two integers `lower` and `upper` that describe the **inclusive** range of values `[lower, upper]` that the hidden sequence can contain. For example, given `differences = [1, -3, 4]`, `lower = 1`, `upper = 6`, the hidden sequence is a sequence of length `4` whose elements are in between `1` and `6` (**inclusive**). `[3, 4, 1, 5]` and `[4, 5, 2, 6]` are possible hidden sequences. `[5, 6, 3, 7]` is not possible since it contains an element greater than `6`. `[1, 2, 3, 4]` is not possible since the differences are not correct. Return _the number of **possible** hidden sequences there are._ If there are no possible sequences, return `0`. **Example 1:** ``` **Input:** differences = [1,-3,4], lower = 1, upper = 6 **Output:** 2 **Explanation:** The possible hidden sequences are: - [3, 4, 1, 5] - [4, 5, 2, 6] Thus, we return 2. ``` **Example 2:** ``` **Input:** differences = [3,-4,5,1,-2], lower = -4, upper = 5 **Output:** 4 **Explanation:** The possible hidden sequences are: - [-3, 0, -4, 1, 2, 0] - [-2, 1, -3, 2, 3, 1] - [-1, 2, -2, 3, 4, 2] - [0, 3, -1, 4, 5, 3] Thus, we return 4. ``` **Example 3:** ``` **Input:** differences = [4,-7,2], lower = 3, upper = 6 **Output:** 0 **Explanation:** There are no possible hidden sequences. Thus, we return 0. ``` **Constraints:** `n == differences.length` `1 <= n <= 105` `-105 <= differences[i] <= 105` `-105 <= lower <= upper <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "differences = [1,-3,4], lower = 1, upper = 6", + "output": "2 " + }, + { + "label": "Example 2", + "input": "differences = [3,-4,5,1,-2], lower = -4, upper = 5", + "output": "4 " + }, + { + "label": "Example 3", + "input": "differences = [4,-7,2], lower = 3, upper = 6", + "output": "0 " + } + ], + "constraints": [ + "For example, given differences = [1, -3, 4], lower = 1, upper = 6, the hidden sequence is a sequence of length 4 whose elements are in between 1 and 6 (inclusive).\n\n\t\n[3, 4, 1, 5] and [4, 5, 2, 6] are possible hidden sequences.\n[5, 6, 3, 7] is not possible since it contains an element greater than 6.\n[1, 2, 3, 4] is not possible since the differences are not correct.", + "[3, 4, 1, 5] and [4, 5, 2, 6] are possible hidden sequences.", + "[5, 6, 3, 7] is not possible since it contains an element greater than 6.", + "[1, 2, 3, 4] is not possible since the differences are not correct.", + "[3, 4, 1, 5] and [4, 5, 2, 6] are possible hidden sequences.", + "[5, 6, 3, 7] is not possible since it contains an element greater than 6.", + "[1, 2, 3, 4] is not possible since the differences are not correct.", + "n == differences.length", + "1 <= n <= 105", + "-105 <= differences[i] <= 105", + "-105 <= lower <= upper <= 105" + ], + "python_template": "class Solution(object):\n def numberOfArrays(self, differences, lower, upper):\n \"\"\"\n :type differences: List[int]\n :type lower: int\n :type upper: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfArrays(int[] differences, int lower, int upper) {\n \n }\n}", + "metadata": { + "func_name": "numberOfArrays" + } +} \ No newline at end of file diff --git a/count-the-number-of-arrays-with-k-matching-adjacent-elements.json b/count-the-number-of-arrays-with-k-matching-adjacent-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..4c92d05f98a9d013d5fd466ff37fd989a7295a49 --- /dev/null +++ b/count-the-number-of-arrays-with-k-matching-adjacent-elements.json @@ -0,0 +1,42 @@ +{ + "id": 3682, + "name": "count-the-number-of-arrays-with-k-matching-adjacent-elements", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-the-number-of-arrays-with-k-matching-adjacent-elements/", + "date": "2024-12-22", + "task_description": "You are given three integers `n`, `m`, `k`. A **good array** `arr` of size `n` is defined as follows: Each element in `arr` is in the **inclusive** range `[1, m]`. _Exactly_ `k` indices `i` (where `1 <= i < n`) satisfy the condition `arr[i - 1] == arr[i]`. Return the number of **good arrays** that can be formed. Since the answer may be very large, return it **modulo **`109 + 7`. **Example 1:** **Input:** n = 3, m = 2, k = 1 **Output:** 4 **Explanation:** There are 4 good arrays. They are `[1, 1, 2]`, `[1, 2, 2]`, `[2, 1, 1]` and `[2, 2, 1]`. Hence, the answer is 4. **Example 2:** **Input:** n = 4, m = 2, k = 2 **Output:** 6 **Explanation:** The good arrays are `[1, 1, 1, 2]`, `[1, 1, 2, 2]`, `[1, 2, 2, 2]`, `[2, 1, 1, 1]`, `[2, 2, 1, 1]` and `[2, 2, 2, 1]`. Hence, the answer is 6. **Example 3:** **Input:** n = 5, m = 2, k = 0 **Output:** 2 **Explanation:** The good arrays are `[1, 2, 1, 2, 1]` and `[2, 1, 2, 1, 2]`. Hence, the answer is 2. **Constraints:** `1 <= n <= 105` `1 <= m <= 105` `0 <= k <= n - 1`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, m = 2, k = 1", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 4, m = 2, k = 2", + "output": "6 " + }, + { + "label": "Example 3", + "input": "n = 5, m = 2, k = 0", + "output": "2 " + } + ], + "constraints": [ + "Each element in arr is in the inclusive range [1, m].", + "Exactly k indices i (where 1 <= i < n) satisfy the condition arr[i - 1] == arr[i].", + "There are 4 good arrays. They are [1, 1, 2], [1, 2, 2], [2, 1, 1] and [2, 2, 1].", + "Hence, the answer is 4.", + "The good arrays are [1, 1, 1, 2], [1, 1, 2, 2], [1, 2, 2, 2], [2, 1, 1, 1], [2, 2, 1, 1] and [2, 2, 2, 1].", + "Hence, the answer is 6.", + "The good arrays are [1, 2, 1, 2, 1] and [2, 1, 2, 1, 2]. Hence, the answer is 2.", + "1 <= n <= 105", + "1 <= m <= 105", + "0 <= k <= n - 1" + ], + "python_template": "class Solution(object):\n def countGoodArrays(self, n, m, k):\n \"\"\"\n :type n: int\n :type m: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countGoodArrays(int n, int m, int k) {\n \n }\n}", + "metadata": { + "func_name": "countGoodArrays" + } +} \ No newline at end of file diff --git a/count-the-number-of-beautiful-subarrays.json b/count-the-number-of-beautiful-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..b858dce0473671db4f8aecb97f3f83c970d068f2 --- /dev/null +++ b/count-the-number-of-beautiful-subarrays.json @@ -0,0 +1,32 @@ +{ + "id": 2656, + "name": "count-the-number-of-beautiful-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/", + "date": "2023-03-05", + "task_description": "You are given a **0-indexed** integer array `nums`. In one operation, you can: Choose two different indices `i` and `j` such that `0 <= i, j < nums.length`. Choose a non-negative integer `k` such that the `kth` bit (**0-indexed**) in the binary representation of `nums[i]` and `nums[j]` is `1`. Subtract `2k` from `nums[i]` and `nums[j]`. A subarray is **beautiful** if it is possible to make all of its elements equal to `0` after applying the above operation any number of times. Return _the number of **beautiful subarrays** in the array_ `nums`. A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [4,3,1,2,4] **Output:** 2 **Explanation:** There are 2 beautiful subarrays in nums: [4,3,1,2,4] and [4,3,1,2,4]. - We can make all elements in the subarray [3,1,2] equal to 0 in the following way: - Choose [3, 1, 2] and k = 1. Subtract 21 from both numbers. The subarray becomes [1, 1, 0]. - Choose [1, 1, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 0, 0]. - We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way: - Choose [4, 3, 1, 2, 4] and k = 2. Subtract 22 from both numbers. The subarray becomes [0, 3, 1, 2, 0]. - Choose [0, 3, 1, 2, 0] and k = 0. Subtract 20 from both numbers. The subarray becomes [0, 2, 0, 2, 0]. - Choose [0, 2, 0, 2, 0] and k = 1. Subtract 21 from both numbers. The subarray becomes [0, 0, 0, 0, 0]. ``` **Example 2:** ``` **Input:** nums = [1,10,4] **Output:** 0 **Explanation:** There are no beautiful subarrays in nums. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,3,1,2,4]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,10,4]", + "output": "0 " + } + ], + "constraints": [ + "Choose two different indices i and j such that 0 <= i, j < nums.length.", + "Choose a non-negative integer k such that the kth bit (0-indexed) in the binary representation of nums[i] and nums[j] is 1.", + "Subtract 2k from nums[i] and nums[j].", + "1 <= nums.length <= 105", + "0 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def beautifulSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long beautifulSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "beautifulSubarrays" + } +} \ No newline at end of file diff --git a/count-the-number-of-fair-pairs.json b/count-the-number-of-fair-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..b6a4309e8e07583a9fea3beefcf0ee75a2db8589 --- /dev/null +++ b/count-the-number-of-fair-pairs.json @@ -0,0 +1,33 @@ +{ + "id": 2699, + "name": "count-the-number-of-fair-pairs", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-fair-pairs/", + "date": "2023-02-05", + "task_description": "Given a **0-indexed** integer array `nums` of size `n` and two integers `lower` and `upper`, return _the number of fair pairs_. A pair `(i, j)` is fair if: `0 <= i < j < n`, and `lower <= nums[i] + nums[j] <= upper` **Example 1:** ``` **Input:** nums = [0,1,7,4,4,5], lower = 3, upper = 6 **Output:** 6 **Explanation:** There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5). ``` **Example 2:** ``` **Input:** nums = [1,7,9,2,5], lower = 11, upper = 11 **Output:** 1 **Explanation:** There is a single fair pair: (2,3). ``` **Constraints:** `1 <= nums.length <= 105` `nums.length == n` `-109 <= nums[i] <= 109` `-109 <= lower <= upper <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,7,4,4,5], lower = 3, upper = 6", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [1,7,9,2,5], lower = 11, upper = 11", + "output": "1 " + } + ], + "constraints": [ + "0 <= i < j < n, and", + "lower <= nums[i] + nums[j] <= upper", + "1 <= nums.length <= 105", + "nums.length == n", + "-109 <= nums[i] <= 109", + "-109 <= lower <= upper <= 109" + ], + "python_template": "class Solution(object):\n def countFairPairs(self, nums, lower, upper):\n \"\"\"\n :type nums: List[int]\n :type lower: int\n :type upper: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countFairPairs(int[] nums, int lower, int upper) {\n \n }\n}", + "metadata": { + "func_name": "countFairPairs" + } +} \ No newline at end of file diff --git a/count-the-number-of-good-nodes.json b/count-the-number-of-good-nodes.json new file mode 100644 index 0000000000000000000000000000000000000000..4a559c4f27f2fff1c9b85c03db1d35ad9b2d1fb2 --- /dev/null +++ b/count-the-number-of-good-nodes.json @@ -0,0 +1,37 @@ +{ + "id": 3486, + "name": "count-the-number-of-good-nodes", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-good-nodes/", + "date": "2024-08-04", + "task_description": "There is an **undirected** tree with `n` nodes labeled from `0` to `n - 1`, and 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. A node is **good** if all the subtrees rooted at its children have the same size. Return the number of **good** nodes in the given tree. A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants. **Example 1:** **Input:** edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]] **Output:** 7 **Explanation:** All of the nodes of the given tree are good. **Example 2:** **Input:** edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]] **Output:** 6 **Explanation:** There are 6 good nodes in the given tree. They are colored in the image above. **Example 3:** **Input:** edges = [[0,1],[1,2],[1,3],[1,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[9,12],[10,11]] **Output:** 12 **Explanation:** All nodes except node 9 are good. **Constraints:** `2 <= n <= 105` `edges.length == n - 1` `edges[i].length == 2` `0 <= ai, bi < n` The input is generated such that `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]", + "output": "6 " + }, + { + "label": "Example 3", + "input": "edges = [[0,1],[1,2],[1,3],[1,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[9,12],[10,11]]", + "output": "12 " + } + ], + "constraints": [ + "2 <= n <= 105", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ai, bi < n", + "The input is generated such that edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def countGoodNodes(self, edges):\n \"\"\"\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countGoodNodes(int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "countGoodNodes" + } +} \ No newline at end of file diff --git a/count-the-number-of-good-partitions.json b/count-the-number-of-good-partitions.json new file mode 100644 index 0000000000000000000000000000000000000000..29a4cb2b60d4351500022644be9b1a90d83eb92f --- /dev/null +++ b/count-the-number-of-good-partitions.json @@ -0,0 +1,34 @@ +{ + "id": 3212, + "name": "count-the-number-of-good-partitions", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-the-number-of-good-partitions/", + "date": "2023-12-03", + "task_description": "You are given a **0-indexed** array `nums` consisting of **positive** integers. A partition of an array into one or more **contiguous** subarrays is called **good** if no two subarrays contain the same number. Return _the **total number** of good partitions of _`nums`. Since the answer may be large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** nums = [1,2,3,4] **Output:** 8 **Explanation:** The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]). ``` **Example 2:** ``` **Input:** nums = [1,1,1,1] **Output:** 1 **Explanation:** The only possible good partition is: ([1,1,1,1]). ``` **Example 3:** ``` **Input:** nums = [1,2,1,3] **Output:** 2 **Explanation:** The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]). ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1,1]", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,1,3]", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def numberOfGoodPartitions(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfGoodPartitions(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "numberOfGoodPartitions" + } +} \ No newline at end of file diff --git a/count-the-number-of-good-subarrays.json b/count-the-number-of-good-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..a80b62dad1924a30226a9ad5b1c625817432ca82 --- /dev/null +++ b/count-the-number-of-good-subarrays.json @@ -0,0 +1,29 @@ +{ + "id": 2626, + "name": "count-the-number-of-good-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-good-subarrays/", + "date": "2023-01-08", + "task_description": "Given an integer array `nums` and an integer `k`, return _the number of **good** subarrays of_ `nums`. A subarray `arr` is **good** if there are **at least **`k` pairs of indices `(i, j)` such that `i < j` and `arr[i] == arr[j]`. A **subarray** is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,1,1,1,1], k = 10 **Output:** 1 **Explanation:** The only good subarray is the array nums itself. ``` **Example 2:** ``` **Input:** nums = [3,1,4,3,2,2,4], k = 2 **Output:** 4 **Explanation:** There are 4 different good subarrays: - [3,1,4,3,2,2] that has 2 pairs. - [3,1,4,3,2,2,4] that has 3 pairs. - [1,4,3,2,2,4] that has 2 pairs. - [4,3,2,2,4] that has 2 pairs. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i], k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1,1,1,1], k = 10", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [3,1,4,3,2,2,4], k = 2", + "output": "4 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i], k <= 109" + ], + "python_template": "class Solution(object):\n def countGood(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 countGood(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "countGood" + } +} \ No newline at end of file diff --git a/count-the-number-of-houses-at-a-certain-distance-i.json b/count-the-number-of-houses-at-a-certain-distance-i.json new file mode 100644 index 0000000000000000000000000000000000000000..858a80786dddfe48ff9bba44f93c1505d089f8d9 --- /dev/null +++ b/count-the-number-of-houses-at-a-certain-distance-i.json @@ -0,0 +1,34 @@ +{ + "id": 3271, + "name": "count-the-number-of-houses-at-a-certain-distance-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-i/", + "date": "2024-01-14", + "task_description": "You are given three **positive** integers `n`, `x`, and `y`. In a city, there exist houses numbered `1` to `n` connected by `n` streets. There is a street connecting the house numbered `i` with the house numbered `i + 1` for all `1 <= i <= n - 1` . An additional street connects the house numbered `x` with the house numbered `y`. For each `k`, such that `1 <= k <= n`, you need to find the number of **pairs of houses** `(house1, house2)` such that the **minimum** number of streets that need to be traveled to reach `house2` from `house1` is `k`. Return _a **1-indexed** array _`result`_ of length _`n`_ where _`result[k]`_ represents the **total** number of pairs of houses such that the **minimum** streets required to reach one house from the other is _`k`. **Note** that `x` and `y` can be **equal**. **Example 1:** ``` **Input:** n = 3, x = 1, y = 3 **Output:** [6,0,0] **Explanation:** Let's look at each pair of houses: - For the pair (1, 2), we can go from house 1 to house 2 directly. - For the pair (2, 1), we can go from house 2 to house 1 directly. - For the pair (1, 3), we can go from house 1 to house 3 directly. - For the pair (3, 1), we can go from house 3 to house 1 directly. - For the pair (2, 3), we can go from house 2 to house 3 directly. - For the pair (3, 2), we can go from house 3 to house 2 directly. ``` **Example 2:** ``` **Input:** n = 5, x = 2, y = 4 **Output:** [10,8,2,0,0] **Explanation:** For each distance k the pairs are: - For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4). - For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3). - For k == 3, the pairs are (1, 5), and (5, 1). - For k == 4 and k == 5, there are no pairs. ``` **Example 3:** ``` **Input:** n = 4, x = 1, y = 1 **Output:** [6,4,2,0] **Explanation:** For each distance k the pairs are: - For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3). - For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2). - For k == 3, the pairs are (1, 4), and (4, 1). - For k == 4, there are no pairs. ``` **Constraints:** `2 <= n <= 100` `1 <= x, y <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, x = 1, y = 3", + "output": "[6,0,0] " + }, + { + "label": "Example 2", + "input": "n = 5, x = 2, y = 4", + "output": "[10,8,2,0,0] " + }, + { + "label": "Example 3", + "input": "n = 4, x = 1, y = 1", + "output": "[6,4,2,0] " + } + ], + "constraints": [ + "2 <= n <= 100", + "1 <= x, y <= n" + ], + "python_template": "class Solution(object):\n def countOfPairs(self, n, x, y):\n \"\"\"\n :type n: int\n :type x: int\n :type y: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] countOfPairs(int n, int x, int y) {\n \n }\n}", + "metadata": { + "func_name": "countOfPairs" + } +} \ No newline at end of file diff --git a/count-the-number-of-houses-at-a-certain-distance-ii.json b/count-the-number-of-houses-at-a-certain-distance-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..8a0d6ae884c46a73bd6efc83de65391c74185ff3 --- /dev/null +++ b/count-the-number-of-houses-at-a-certain-distance-ii.json @@ -0,0 +1,34 @@ +{ + "id": 3310, + "name": "count-the-number-of-houses-at-a-certain-distance-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-ii/", + "date": "2024-01-14", + "task_description": "You are given three **positive** integers `n`, `x`, and `y`. In a city, there exist houses numbered `1` to `n` connected by `n` streets. There is a street connecting the house numbered `i` with the house numbered `i + 1` for all `1 <= i <= n - 1` . An additional street connects the house numbered `x` with the house numbered `y`. For each `k`, such that `1 <= k <= n`, you need to find the number of **pairs of houses** `(house1, house2)` such that the **minimum** number of streets that need to be traveled to reach `house2` from `house1` is `k`. Return _a **1-indexed** array _`result`_ of length _`n`_ where _`result[k]`_ represents the **total** number of pairs of houses such that the **minimum** streets required to reach one house from the other is _`k`. **Note** that `x` and `y` can be **equal**. **Example 1:** ``` **Input:** n = 3, x = 1, y = 3 **Output:** [6,0,0] **Explanation:** Let's look at each pair of houses: - For the pair (1, 2), we can go from house 1 to house 2 directly. - For the pair (2, 1), we can go from house 2 to house 1 directly. - For the pair (1, 3), we can go from house 1 to house 3 directly. - For the pair (3, 1), we can go from house 3 to house 1 directly. - For the pair (2, 3), we can go from house 2 to house 3 directly. - For the pair (3, 2), we can go from house 3 to house 2 directly. ``` **Example 2:** ``` **Input:** n = 5, x = 2, y = 4 **Output:** [10,8,2,0,0] **Explanation:** For each distance k the pairs are: - For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4). - For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3). - For k == 3, the pairs are (1, 5), and (5, 1). - For k == 4 and k == 5, there are no pairs. ``` **Example 3:** ``` **Input:** n = 4, x = 1, y = 1 **Output:** [6,4,2,0] **Explanation:** For each distance k the pairs are: - For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3). - For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2). - For k == 3, the pairs are (1, 4), and (4, 1). - For k == 4, there are no pairs. ``` **Constraints:** `2 <= n <= 105` `1 <= x, y <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, x = 1, y = 3", + "output": "[6,0,0] " + }, + { + "label": "Example 2", + "input": "n = 5, x = 2, y = 4", + "output": "[10,8,2,0,0] " + }, + { + "label": "Example 3", + "input": "n = 4, x = 1, y = 1", + "output": "[6,4,2,0] " + } + ], + "constraints": [ + "2 <= n <= 105", + "1 <= x, y <= n" + ], + "python_template": "class Solution(object):\n def countOfPairs(self, n, x, y):\n \"\"\"\n :type n: int\n :type x: int\n :type y: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] countOfPairs(int n, int x, int y) {\n \n }\n}", + "metadata": { + "func_name": "countOfPairs" + } +} \ No newline at end of file diff --git a/count-the-number-of-ideal-arrays.json b/count-the-number-of-ideal-arrays.json new file mode 100644 index 0000000000000000000000000000000000000000..a08698eed6ab10ee0aa882cb0ceb6e4c6e3ee8c1 --- /dev/null +++ b/count-the-number-of-ideal-arrays.json @@ -0,0 +1,31 @@ +{ + "id": 2415, + "name": "count-the-number-of-ideal-arrays", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-the-number-of-ideal-arrays/", + "date": "2022-07-03", + "task_description": "You are given two integers `n` and `maxValue`, which are used to describe an **ideal** array. A **0-indexed** integer array `arr` of length `n` is considered **ideal** if the following conditions hold: Every `arr[i]` is a value from `1` to `maxValue`, for `0 <= i < n`. Every `arr[i]` is divisible by `arr[i - 1]`, for `0 < i < n`. Return _the number of **distinct** ideal arrays of length _`n`. Since the answer may be very large, return it modulo `109 + 7`. **Example 1:** ``` **Input:** n = 2, maxValue = 5 **Output:** 10 **Explanation:** The following are the possible ideal arrays: - Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5] - Arrays starting with the value 2 (2 arrays): [2,2], [2,4] - Arrays starting with the value 3 (1 array): [3,3] - Arrays starting with the value 4 (1 array): [4,4] - Arrays starting with the value 5 (1 array): [5,5] There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays. ``` **Example 2:** ``` **Input:** n = 5, maxValue = 3 **Output:** 11 **Explanation:** The following are the possible ideal arrays: - Arrays starting with the value 1 (9 arrays): - With no other distinct values (1 array): [1,1,1,1,1] - With 2nd distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2] - With 2nd distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3] - Arrays starting with the value 2 (1 array): [2,2,2,2,2] - Arrays starting with the value 3 (1 array): [3,3,3,3,3] There are a total of 9 + 1 + 1 = 11 distinct ideal arrays. ``` **Constraints:** `2 <= n <= 104` `1 <= maxValue <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 2, maxValue = 5", + "output": "10 " + }, + { + "label": "Example 2", + "input": "n = 5, maxValue = 3", + "output": "11 " + } + ], + "constraints": [ + "Every arr[i] is a value from 1 to maxValue, for 0 <= i < n.", + "Every arr[i] is divisible by arr[i - 1], for 0 < i < n.", + "2 <= n <= 104", + "1 <= maxValue <= 104" + ], + "python_template": "class Solution(object):\n def idealArrays(self, n, maxValue):\n \"\"\"\n :type n: int\n :type maxValue: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int idealArrays(int n, int maxValue) {\n \n }\n}", + "metadata": { + "func_name": "idealArrays" + } +} \ No newline at end of file diff --git a/count-the-number-of-incremovable-subarrays-i.json b/count-the-number-of-incremovable-subarrays-i.json new file mode 100644 index 0000000000000000000000000000000000000000..5d2edc6f062c1ac76e0f7d888424344c810ba697 --- /dev/null +++ b/count-the-number-of-incremovable-subarrays-i.json @@ -0,0 +1,34 @@ +{ + "id": 3252, + "name": "count-the-number-of-incremovable-subarrays-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/", + "date": "2023-12-09", + "task_description": "You are given a **0-indexed** array of **positive** integers `nums`. A subarray of `nums` is called **incremovable** if `nums` becomes **strictly increasing** on removing the subarray. For example, the subarray `[3, 4]` is an incremovable subarray of `[5, 3, 4, 6, 7]` because removing this subarray changes the array `[5, 3, 4, 6, 7]` to `[5, 6, 7]` which is strictly increasing. Return _the total number of **incremovable** subarrays of_ `nums`. **Note** that an empty array is considered strictly increasing. A **subarray** is a contiguous non-empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,2,3,4] **Output:** 10 **Explanation:** The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray. ``` **Example 2:** ``` **Input:** nums = [6,5,7,8] **Output:** 7 **Explanation:** The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8]. It can be shown that there are only 7 incremovable subarrays in nums. ``` **Example 3:** ``` **Input:** nums = [8,7,6,6] **Output:** 3 **Explanation:** The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing. ``` **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4]", + "output": "10 " + }, + { + "label": "Example 2", + "input": "nums = [6,5,7,8]", + "output": "7 " + }, + { + "label": "Example 3", + "input": "nums = [8,7,6,6]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def incremovableSubarrayCount(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int incremovableSubarrayCount(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "incremovableSubarrayCount" + } +} \ No newline at end of file diff --git a/count-the-number-of-infection-sequences.json b/count-the-number-of-infection-sequences.json new file mode 100644 index 0000000000000000000000000000000000000000..82b156a042f98bc5eb09a26e9e30231095cd00d0 --- /dev/null +++ b/count-the-number-of-infection-sequences.json @@ -0,0 +1,35 @@ +{ + "id": 3224, + "name": "count-the-number-of-infection-sequences", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-the-number-of-infection-sequences/", + "date": "2023-11-26", + "task_description": "You are given an integer `n` and an array `sick` sorted in increasing order, representing positions of infected people in a line of `n` people. At each step, **one **uninfected person **adjacent** to an infected person gets infected. This process continues until everyone is infected. An **infection sequence** is the order in which uninfected people become infected, excluding those initially infected. Return the number of different infection sequences possible, modulo `109+7`. **Example 1:** **Input:** n = 5, sick = [0,4] **Output:** 4 **Explanation:** There is a total of 6 different sequences overall. Valid infection sequences are `[1,2,3]`, `[1,3,2]`, `[3,2,1]` and `[3,1,2]`. `[2,3,1]` and `[2,1,3]` are not valid infection sequences because the person at index 2 cannot be infected at the first step. **Example 2:** **Input:** n = 4, sick = [1] **Output:** 3 **Explanation:** There is a total of 6 different sequences overall. Valid infection sequences are `[0,2,3]`, `[2,0,3]` and `[2,3,0]`. `[3,2,0]`, `[3,0,2]`, and `[0,3,2]` are not valid infection sequences because the infection starts at the person at index 1, then the order of infection is 2, then 3, and hence 3 cannot be infected earlier than 2. **Constraints:** `2 <= n <= 105` `1 <= sick.length <= n - 1` `0 <= sick[i] <= n - 1` `sick` is sorted in increasing order.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, sick = [0,4]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 4, sick = [1]", + "output": "3 " + } + ], + "constraints": [ + "Valid infection sequences are [1,2,3], [1,3,2], [3,2,1] and [3,1,2].", + "[2,3,1] and [2,1,3] are not valid infection sequences because the person at index 2 cannot be infected at the first step.", + "Valid infection sequences are [0,2,3], [2,0,3] and [2,3,0].", + "[3,2,0], [3,0,2], and [0,3,2] are not valid infection sequences because the infection starts at the person at index 1, then the order of infection is 2, then 3, and hence 3 cannot be infected earlier than 2.", + "2 <= n <= 105", + "1 <= sick.length <= n - 1", + "0 <= sick[i] <= n - 1", + "sick is sorted in increasing order." + ], + "python_template": "class Solution(object):\n def numberOfSequence(self, n, sick):\n \"\"\"\n :type n: int\n :type sick: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfSequence(int n, int[] sick) {\n \n }\n}", + "metadata": { + "func_name": "numberOfSequence" + } +} \ No newline at end of file diff --git a/count-the-number-of-inversions.json b/count-the-number-of-inversions.json new file mode 100644 index 0000000000000000000000000000000000000000..8b89ad5f75400766b36ead6ef43fc8046d52712e --- /dev/null +++ b/count-the-number-of-inversions.json @@ -0,0 +1,55 @@ +{ + "id": 3460, + "name": "count-the-number-of-inversions", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-the-number-of-inversions/", + "date": "2024-06-08", + "task_description": "You are given an integer `n` and a 2D array `requirements`, where `requirements[i] = [endi, cnti]` represents the end index and the **inversion** count of each requirement. A pair of indices `(i, j)` from an integer array `nums` is called an **inversion** if: `i < j` and `nums[i] > nums[j]` Return the number of permutations `perm` of `[0, 1, 2, ..., n - 1]` such that for **all** `requirements[i]`, `perm[0..endi]` has exactly `cnti` inversions. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** n = 3, requirements = [[2,2],[0,0]] **Output:** 2 **Explanation:** The two permutations are: `[2, 0, 1]` Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`. Prefix `[2]` has 0 inversions. `[1, 2, 0]` Prefix `[1, 2, 0]` has inversions `(0, 2)` and `(1, 2)`. Prefix `[1]` has 0 inversions. **Example 2:** **Input:** n = 3, requirements = [[2,2],[1,1],[0,0]] **Output:** 1 **Explanation:** The only satisfying permutation is `[2, 0, 1]`: Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`. Prefix `[2, 0]` has an inversion `(0, 1)`. Prefix `[2]` has 0 inversions. **Example 3:** **Input:** n = 2, requirements = [[0,0],[1,0]] **Output:** 1 **Explanation:** The only satisfying permutation is `[0, 1]`: Prefix `[0]` has 0 inversions. Prefix `[0, 1]` has an inversion `(0, 1)`. **Constraints:** `2 <= n <= 300` `1 <= requirements.length <= n` `requirements[i] = [endi, cnti]` `0 <= endi <= n - 1` `0 <= cnti <= 400` The input is generated such that there is at least one `i` such that `endi == n - 1`. The input is generated such that all `endi` are unique.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, requirements = [[2,2],[0,0]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "n = 3, requirements = [[2,2],[1,1],[0,0]]", + "output": "1 " + }, + { + "label": "Example 3", + "input": "n = 2, requirements = [[0,0],[1,0]]", + "output": "1 " + } + ], + "constraints": [ + "i < j and nums[i] > nums[j]", + "[2, 0, 1]\n\nPrefix [2, 0, 1] has inversions (0, 1) and (0, 2).\nPrefix [2] has 0 inversions.", + "Prefix [2, 0, 1] has inversions (0, 1) and (0, 2).", + "Prefix [2] has 0 inversions.", + "[1, 2, 0]\n\nPrefix [1, 2, 0] has inversions (0, 2) and (1, 2).\nPrefix [1] has 0 inversions.", + "Prefix [1, 2, 0] has inversions (0, 2) and (1, 2).", + "Prefix [1] has 0 inversions.", + "Prefix [2, 0, 1] has inversions (0, 1) and (0, 2).", + "Prefix [2] has 0 inversions.", + "Prefix [1, 2, 0] has inversions (0, 2) and (1, 2).", + "Prefix [1] has 0 inversions.", + "Prefix [2, 0, 1] has inversions (0, 1) and (0, 2).", + "Prefix [2, 0] has an inversion (0, 1).", + "Prefix [2] has 0 inversions.", + "Prefix [0] has 0 inversions.", + "Prefix [0, 1] has an inversion (0, 1).", + "2 <= n <= 300", + "1 <= requirements.length <= n", + "requirements[i] = [endi, cnti]", + "0 <= endi <= n - 1", + "0 <= cnti <= 400", + "The input is generated such that there is at least one i such that endi == n - 1.", + "The input is generated such that all endi are unique." + ], + "python_template": "class Solution(object):\n def numberOfPermutations(self, n, requirements):\n \"\"\"\n :type n: int\n :type requirements: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfPermutations(int n, int[][] requirements) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPermutations" + } +} \ No newline at end of file diff --git a/count-the-number-of-powerful-integers.json b/count-the-number-of-powerful-integers.json new file mode 100644 index 0000000000000000000000000000000000000000..9dcc943ca517223feadaa233d5087a9e26960e62 --- /dev/null +++ b/count-the-number-of-powerful-integers.json @@ -0,0 +1,37 @@ +{ + "id": 3243, + "name": "count-the-number-of-powerful-integers", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-the-number-of-powerful-integers/", + "date": "2023-12-23", + "task_description": "You are given three integers `start`, `finish`, and `limit`. You are also given a **0-indexed** string `s` representing a **positive** integer. A **positive** integer `x` is called **powerful** if it ends with `s` (in other words, `s` is a **suffix** of `x`) and each digit in `x` is at most `limit`. Return _the **total** number of powerful integers in the range_ `[start..finish]`. A string `x` is a suffix of a string `y` if and only if `x` is a substring of `y` that starts from some index (**including **`0`) in `y` and extends to the index `y.length - 1`. For example, `25` is a suffix of `5125` whereas `512` is not. **Example 1:** ``` **Input:** start = 1, finish = 6000, limit = 4, s = \"124\" **Output:** 5 **Explanation:** The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and \"124\" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4. It can be shown that there are only 5 powerful integers in this range. ``` **Example 2:** ``` **Input:** start = 15, finish = 215, limit = 6, s = \"10\" **Output:** 2 **Explanation:** The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and \"10\" as a suffix. It can be shown that there are only 2 powerful integers in this range. ``` **Example 3:** ``` **Input:** start = 1000, finish = 2000, limit = 4, s = \"3000\" **Output:** 0 **Explanation:** All integers in the range [1000..2000] are smaller than 3000, hence \"3000\" cannot be a suffix of any integer in this range. ``` **Constraints:** `1 <= start <= finish <= 1015` `1 <= limit <= 9` `1 <= s.length <= floor(log10(finish)) + 1` `s` only consists of numeric digits which are at most `limit`. `s` does not have leading zeros.", + "test_case": [ + { + "label": "Example 1", + "input": "start = 1, finish = 6000, limit = 4, s = \"124\"", + "output": "5 " + }, + { + "label": "Example 2", + "input": "start = 15, finish = 215, limit = 6, s = \"10\"", + "output": "2 " + }, + { + "label": "Example 3", + "input": "start = 1000, finish = 2000, limit = 4, s = \"3000\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= start <= finish <= 1015", + "1 <= limit <= 9", + "1 <= s.length <= floor(log10(finish)) + 1", + "s only consists of numeric digits which are at most limit.", + "s does not have leading zeros." + ], + "python_template": "class Solution(object):\n def numberOfPowerfulInt(self, start, finish, limit, s):\n \"\"\"\n :type start: int\n :type finish: int\n :type limit: int\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long numberOfPowerfulInt(long start, long finish, int limit, String s) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPowerfulInt" + } +} \ No newline at end of file diff --git a/count-the-number-of-special-characters-i.json b/count-the-number-of-special-characters-i.json new file mode 100644 index 0000000000000000000000000000000000000000..5f96c9d40fb36123eec32f2d12af75bda1600e7e --- /dev/null +++ b/count-the-number-of-special-characters-i.json @@ -0,0 +1,34 @@ +{ + "id": 3408, + "name": "count-the-number-of-special-characters-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-the-number-of-special-characters-i/", + "date": "2024-04-14", + "task_description": "You are given a string `word`. A letter is called **special** if it appears **both** in lowercase and uppercase in `word`. Return the number of_ _**special** letters in_ _`word`. **Example 1:** **Input:** word = \"aaAbcBC\" **Output:** 3 **Explanation:** The special characters in `word` are `'a'`, `'b'`, and `'c'`. **Example 2:** **Input:** word = \"abc\" **Output:** 0 **Explanation:** No character in `word` appears in uppercase. **Example 3:** **Input:** word = \"abBCab\" **Output:** 1 **Explanation:** The only special character in `word` is `'b'`. **Constraints:** `1 <= word.length <= 50` `word` consists of only lowercase and uppercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"aaAbcBC\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "word = \"abc\"", + "output": "0 " + }, + { + "label": "Example 3", + "input": "word = \"abBCab\"", + "output": "1 " + } + ], + "constraints": [ + "1 <= word.length <= 50", + "word consists of only lowercase and uppercase English letters." + ], + "python_template": "class Solution(object):\n def numberOfSpecialChars(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfSpecialChars(String word) {\n \n }\n}", + "metadata": { + "func_name": "numberOfSpecialChars" + } +} \ No newline at end of file diff --git a/count-the-number-of-special-characters-ii.json b/count-the-number-of-special-characters-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..7fe6da32b56e896db2c82ef20498927c98138c34 --- /dev/null +++ b/count-the-number-of-special-characters-ii.json @@ -0,0 +1,34 @@ +{ + "id": 3405, + "name": "count-the-number-of-special-characters-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-special-characters-ii/", + "date": "2024-04-14", + "task_description": "You are given a string `word`. A letter `c` is called **special** if it appears **both** in lowercase and uppercase in `word`, and **every** lowercase occurrence of `c` appears before the **first** uppercase occurrence of `c`. Return the number of_ _**special** letters_ _in_ _`word`. **Example 1:** **Input:** word = \"aaAbcBC\" **Output:** 3 **Explanation:** The special characters are `'a'`, `'b'`, and `'c'`. **Example 2:** **Input:** word = \"abc\" **Output:** 0 **Explanation:** There are no special characters in `word`. **Example 3:** **Input:** word = \"AbBCab\" **Output:** 0 **Explanation:** There are no special characters in `word`. **Constraints:** `1 <= word.length <= 2 * 105` `word` consists of only lowercase and uppercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"aaAbcBC\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "word = \"abc\"", + "output": "0 " + }, + { + "label": "Example 3", + "input": "word = \"AbBCab\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= word.length <= 2 * 105", + "word consists of only lowercase and uppercase English letters." + ], + "python_template": "class Solution(object):\n def numberOfSpecialChars(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfSpecialChars(String word) {\n \n }\n}", + "metadata": { + "func_name": "numberOfSpecialChars" + } +} \ No newline at end of file diff --git a/count-the-number-of-square-free-subsets.json b/count-the-number-of-square-free-subsets.json new file mode 100644 index 0000000000000000000000000000000000000000..570e5eab7b55f088c68d5b6cdaacaf39c29a08d4 --- /dev/null +++ b/count-the-number-of-square-free-subsets.json @@ -0,0 +1,29 @@ +{ + "id": 2709, + "name": "count-the-number-of-square-free-subsets", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-square-free-subsets/", + "date": "2023-02-12", + "task_description": "You are given a positive integer **0-indexed** array `nums`. A subset of the array `nums` is **square-free** if the product of its elements is a **square-free integer**. A **square-free integer** is an integer that is divisible by no square number other than `1`. Return _the number of square-free non-empty subsets of the array_ **nums**. Since the answer may be too large, return it **modulo** `109 + 7`. A **non-empty** **subset** of `nums` is an array that can be obtained by deleting some (possibly none but not all) elements from `nums`. Two subsets are different if and only if the chosen indices to delete are different. **Example 1:** ``` **Input:** nums = [3,4,4,5] **Output:** 3 **Explanation:** There are 3 square-free subsets in this example: - The subset consisting of the 0th element [3]. The product of its elements is 3, which is a square-free integer. - The subset consisting of the 3rd element [5]. The product of its elements is 5, which is a square-free integer. - The subset consisting of 0th and 3rd elements [3,5]. The product of its elements is 15, which is a square-free integer. It can be proven that there are no more than 3 square-free subsets in the given array. ``` **Example 2:** ``` **Input:** nums = [1] **Output:** 1 **Explanation:** There is 1 square-free subset in this example: - The subset consisting of the 0th element [1]. The product of its elements is 1, which is a square-free integer. It can be proven that there is no more than 1 square-free subset in the given array. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 30`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,4,4,5]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1]", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 30" + ], + "python_template": "class Solution(object):\n def squareFreeSubsets(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int squareFreeSubsets(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "squareFreeSubsets" + } +} \ No newline at end of file diff --git a/count-the-number-of-substrings-with-dominant-ones.json b/count-the-number-of-substrings-with-dominant-ones.json new file mode 100644 index 0000000000000000000000000000000000000000..76e9cc75fc5969a718fd6c8a5300a3c44eb803fb --- /dev/null +++ b/count-the-number-of-substrings-with-dominant-ones.json @@ -0,0 +1,29 @@ +{ + "id": 3479, + "name": "count-the-number-of-substrings-with-dominant-ones", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/", + "date": "2024-07-21", + "task_description": "You are given a binary string `s`. Return the number of substrings with **dominant** ones. A string has **dominant** ones if the number of ones in the string is **greater than or equal to** the **square** of the number of zeros in the string. **Example 1:** **Input:** s = \"00011\" **Output:** 5 **Explanation:** The substrings with dominant ones are shown in the table below. i j s[i..j] Number of Zeros Number of Ones 3 3 1 0 1 4 4 1 0 1 2 3 01 1 1 3 4 11 0 2 2 4 011 1 2 **Example 2:** **Input:** s = \"101101\" **Output:** 16 **Explanation:** The substrings with **non-dominant** ones are shown in the table below. Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones. i j s[i..j] Number of Zeros Number of Ones 1 1 0 1 0 4 4 0 1 0 1 4 0110 2 2 0 4 10110 2 3 1 5 01101 2 3 **Constraints:** `1 <= s.length <= 4 * 104` `s` consists only of characters `'0'` and `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"00011\"", + "output": "5 " + }, + { + "label": "Example 2", + "input": "s = \"101101\"", + "output": "16 " + } + ], + "constraints": [ + "1 <= s.length <= 4 * 104", + "s consists only of characters '0' and '1'." + ], + "python_template": "class Solution(object):\n def numberOfSubstrings(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfSubstrings(String s) {\n \n }\n}", + "metadata": { + "func_name": "numberOfSubstrings" + } +} \ No newline at end of file diff --git a/count-the-number-of-vowel-strings-in-range.json b/count-the-number-of-vowel-strings-in-range.json new file mode 100644 index 0000000000000000000000000000000000000000..7fcbc368fe1a8fabecce20431a882cc181ab06da --- /dev/null +++ b/count-the-number-of-vowel-strings-in-range.json @@ -0,0 +1,31 @@ +{ + "id": 2654, + "name": "count-the-number-of-vowel-strings-in-range", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/", + "date": "2023-03-05", + "task_description": "You are given a **0-indexed** array of string `words` and two integers `left` and `right`. A string is called a **vowel string** if it starts with a vowel character and ends with a vowel character where vowel characters are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. Return _the number of vowel strings _`words[i]`_ where _`i`_ belongs to the inclusive range _`[left, right]`. **Example 1:** ``` **Input:** words = [\"are\",\"amy\",\"u\"], left = 0, right = 2 **Output:** 2 **Explanation:** - \"are\" is a vowel string because it starts with 'a' and ends with 'e'. - \"amy\" is not a vowel string because it does not end with a vowel. - \"u\" is a vowel string because it starts with 'u' and ends with 'u'. The number of vowel strings in the mentioned range is 2. ``` **Example 2:** ``` **Input:** words = [\"hey\",\"aeo\",\"mu\",\"ooo\",\"artro\"], left = 1, right = 4 **Output:** 3 **Explanation:** - \"aeo\" is a vowel string because it starts with 'a' and ends with 'o'. - \"mu\" is not a vowel string because it does not start with a vowel. - \"ooo\" is a vowel string because it starts with 'o' and ends with 'o'. - \"artro\" is a vowel string because it starts with 'a' and ends with 'o'. The number of vowel strings in the mentioned range is 3. ``` **Constraints:** `1 <= words.length <= 1000` `1 <= words[i].length <= 10` `words[i]` consists of only lowercase English letters. `0 <= left <= right < words.length`", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"are\",\"amy\",\"u\"], left = 0, right = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "words = [\"hey\",\"aeo\",\"mu\",\"ooo\",\"artro\"], left = 1, right = 4", + "output": "3 " + } + ], + "constraints": [ + "1 <= words.length <= 1000", + "1 <= words[i].length <= 10", + "words[i] consists of only lowercase English letters.", + "0 <= left <= right < words.length" + ], + "python_template": "class Solution(object):\n def vowelStrings(self, words, left, right):\n \"\"\"\n :type words: List[str]\n :type left: int\n :type right: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int vowelStrings(String[] words, int left, int right) {\n \n }\n}", + "metadata": { + "func_name": "vowelStrings" + } +} \ No newline at end of file diff --git a/count-unguarded-cells-in-the-grid.json b/count-unguarded-cells-in-the-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..4b10bee1cb5a230b80f0b2e0d1782bc35a95b17f --- /dev/null +++ b/count-unguarded-cells-in-the-grid.json @@ -0,0 +1,35 @@ +{ + "id": 2343, + "name": "count-unguarded-cells-in-the-grid", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-unguarded-cells-in-the-grid/", + "date": "2022-04-16", + "task_description": "You are given two integers `m` and `n` representing a **0-indexed** `m x n` grid. You are also given two 2D integer arrays `guards` and `walls` where `guards[i] = [rowi, coli]` and `walls[j] = [rowj, colj]` represent the positions of the `ith` guard and `jth` wall respectively. A guard can see every cell in the four cardinal directions (north, east, south, or west) starting from their position unless **obstructed** by a wall or another guard. A cell is **guarded** if there is **at least** one guard that can see it. Return_ the number of unoccupied cells that are **not** **guarded**._ **Example 1:** ``` **Input:** m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]] **Output:** 7 **Explanation:** The guarded and unguarded cells are shown in red and green respectively in the above diagram. There are a total of 7 unguarded cells, so we return 7. ``` **Example 2:** ``` **Input:** m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]] **Output:** 4 **Explanation:** The unguarded cells are shown in green in the above diagram. There are a total of 4 unguarded cells, so we return 4. ``` **Constraints:** `1 <= m, n <= 105` `2 <= m * n <= 105` `1 <= guards.length, walls.length <= 5 * 104` `2 <= guards.length + walls.length <= m * n` `guards[i].length == walls[j].length == 2` `0 <= rowi, rowj < m` `0 <= coli, colj < n` All the positions in `guards` and `walls` are **unique**.", + "test_case": [ + { + "label": "Example 1", + "input": "m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]", + "output": "4 " + } + ], + "constraints": [ + "1 <= m, n <= 105", + "2 <= m * n <= 105", + "1 <= guards.length, walls.length <= 5 * 104", + "2 <= guards.length + walls.length <= m * n", + "guards[i].length == walls[j].length == 2", + "0 <= rowi, rowj < m", + "0 <= coli, colj < n", + "All the positions in guards and walls are unique." + ], + "python_template": "class Solution(object):\n def countUnguarded(self, m, n, guards, walls):\n \"\"\"\n :type m: int\n :type n: int\n :type guards: List[List[int]]\n :type walls: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {\n \n }\n}", + "metadata": { + "func_name": "countUnguarded" + } +} \ No newline at end of file diff --git a/count-unreachable-pairs-of-nodes-in-an-undirected-graph.json b/count-unreachable-pairs-of-nodes-in-an-undirected-graph.json new file mode 100644 index 0000000000000000000000000000000000000000..709091608e27f7e909b3f35296394989b44837d1 --- /dev/null +++ b/count-unreachable-pairs-of-nodes-in-an-undirected-graph.json @@ -0,0 +1,33 @@ +{ + "id": 2403, + "name": "count-unreachable-pairs-of-nodes-in-an-undirected-graph", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/", + "date": "2022-06-11", + "task_description": "You are given an integer `n`. There is an **undirected** graph with `n` nodes, numbered from `0` to `n - 1`. You are given a 2D integer array `edges` where `edges[i] = [ai, bi]` denotes that there exists an **undirected** edge connecting nodes `ai` and `bi`. Return _the **number of pairs** of different nodes that are **unreachable** from each other_. **Example 1:** ``` **Input:** n = 3, edges = [[0,1],[0,2],[1,2]] **Output:** 0 **Explanation:** There are no pairs of nodes that are unreachable from each other. Therefore, we return 0. ``` **Example 2:** ``` **Input:** n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]] **Output:** 14 **Explanation:** There are 14 pairs of nodes that are unreachable from each other: [[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]]. Therefore, we return 14. ``` **Constraints:** `1 <= n <= 105` `0 <= edges.length <= 2 * 105` `edges[i].length == 2` `0 <= ai, bi < n` `ai != bi` There are no repeated edges.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, edges = [[0,1],[0,2],[1,2]]", + "output": "0 " + }, + { + "label": "Example 2", + "input": "n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]", + "output": "14 " + } + ], + "constraints": [ + "1 <= n <= 105", + "0 <= edges.length <= 2 * 105", + "edges[i].length == 2", + "0 <= ai, bi < n", + "ai != bi", + "There are no repeated edges." + ], + "python_template": "class Solution(object):\n def countPairs(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 long countPairs(int n, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "countPairs" + } +} \ No newline at end of file diff --git a/count-valid-paths-in-a-tree.json b/count-valid-paths-in-a-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..1e6dd3057436ecd63e3a57fe70c6d1acb317d4f6 --- /dev/null +++ b/count-valid-paths-in-a-tree.json @@ -0,0 +1,34 @@ +{ + "id": 3112, + "name": "count-valid-paths-in-a-tree", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/count-valid-paths-in-a-tree/", + "date": "2023-09-17", + "task_description": "There is an undirected tree with `n` nodes labeled from `1` to `n`. You are given the integer `n` and a 2D integer array `edges` of length `n - 1`, where `edges[i] = [ui, vi]` indicates that there is an edge between nodes `ui` and `vi` in the tree. Return _the **number of valid paths** in the tree_. A path `(a, b)` is **valid** if there exists **exactly one** prime number among the node labels in the path from `a` to `b`. **Note** that: The path `(a, b)` is a sequence of **distinct** nodes starting with node `a` and ending with node `b` such that every two adjacent nodes in the sequence share an edge in the tree. Path `(a, b)` and path `(b, a)` are considered the **same** and counted only **once**. **Example 1:** ``` **Input:** n = 5, edges = [[1,2],[1,3],[2,4],[2,5]] **Output:** 4 **Explanation:** The pairs with exactly one prime number on the path between them are: - (1, 2) since the path from 1 to 2 contains prime number 2. - (1, 3) since the path from 1 to 3 contains prime number 3. - (1, 4) since the path from 1 to 4 contains prime number 2. - (2, 4) since the path from 2 to 4 contains prime number 2. It can be shown that there are only 4 valid paths. ``` **Example 2:** ``` **Input:** n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]] **Output:** 6 **Explanation:** The pairs with exactly one prime number on the path between them are: - (1, 2) since the path from 1 to 2 contains prime number 2. - (1, 3) since the path from 1 to 3 contains prime number 3. - (1, 4) since the path from 1 to 4 contains prime number 2. - (1, 6) since the path from 1 to 6 contains prime number 3. - (2, 4) since the path from 2 to 4 contains prime number 2. - (3, 6) since the path from 3 to 6 contains prime number 3. It can be shown that there are only 6 valid paths. ``` **Constraints:** `1 <= n <= 105` `edges.length == n - 1` `edges[i].length == 2` `1 <= ui, vi <= n` The input is generated such that `edges` represent a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]", + "output": "6 " + } + ], + "constraints": [ + "The path (a, b) is a sequence of distinct nodes starting with node a and ending with node b such that every two adjacent nodes in the sequence share an edge in the tree.", + "Path (a, b) and path (b, a) are considered the same and counted only once.", + "1 <= n <= 105", + "edges.length == n - 1", + "edges[i].length == 2", + "1 <= ui, vi <= n", + "The input is generated such that edges represent a valid tree." + ], + "python_template": "class Solution(object):\n def countPaths(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 long countPaths(int n, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "countPaths" + } +} \ No newline at end of file diff --git a/count-ways-to-build-good-strings.json b/count-ways-to-build-good-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..1cd9ef66fc575d8048a82f6bfe2d6400f4cdc837 --- /dev/null +++ b/count-ways-to-build-good-strings.json @@ -0,0 +1,31 @@ +{ + "id": 2562, + "name": "count-ways-to-build-good-strings", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-ways-to-build-good-strings/", + "date": "2022-10-29", + "task_description": "Given the integers `zero`, `one`, `low`, and `high`, we can construct a string by starting with an empty string, and then at each step perform either of the following: Append the character `'0'` `zero` times. Append the character `'1'` `one` times. This can be performed any number of times. A **good** string is a string constructed by the above process having a **length** between `low` and `high` (**inclusive**). Return _the number of **different** good strings that can be constructed satisfying these properties._ Since the answer can be large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** low = 3, high = 3, zero = 1, one = 1 **Output:** 8 **Explanation:** One possible valid good string is \"011\". It can be constructed as follows: \"\" -> \"0\" -> \"01\" -> \"011\". All binary strings from \"000\" to \"111\" are good strings in this example. ``` **Example 2:** ``` **Input:** low = 2, high = 3, zero = 1, one = 2 **Output:** 5 **Explanation:** The good strings are \"00\", \"11\", \"000\", \"110\", and \"011\". ``` **Constraints:** `1 <= low <= high <= 105` `1 <= zero, one <= low`", + "test_case": [ + { + "label": "Example 1", + "input": "low = 3, high = 3, zero = 1, one = 1", + "output": "8 " + }, + { + "label": "Example 2", + "input": "low = 2, high = 3, zero = 1, one = 2", + "output": "5 " + } + ], + "constraints": [ + "Append the character '0' zero times.", + "Append the character '1' one times.", + "1 <= low <= high <= 105", + "1 <= zero, one <= low" + ], + "python_template": "class Solution(object):\n def countGoodStrings(self, low, high, zero, one):\n \"\"\"\n :type low: int\n :type high: int\n :type zero: int\n :type one: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countGoodStrings(int low, int high, int zero, int one) {\n \n }\n}", + "metadata": { + "func_name": "countGoodStrings" + } +} \ No newline at end of file diff --git a/count-ways-to-group-overlapping-ranges.json b/count-ways-to-group-overlapping-ranges.json new file mode 100644 index 0000000000000000000000000000000000000000..626fd3a12b4243a7f5469aeb55bd2ea6c7825476 --- /dev/null +++ b/count-ways-to-group-overlapping-ranges.json @@ -0,0 +1,33 @@ +{ + "id": 2651, + "name": "count-ways-to-group-overlapping-ranges", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-ways-to-group-overlapping-ranges/", + "date": "2023-02-18", + "task_description": "You are given a 2D integer array `ranges` where `ranges[i] = [starti, endi]` denotes that all integers between `starti` and `endi` (both **inclusive**) are contained in the `ith` range. You are to split `ranges` into **two** (possibly empty) groups such that: Each range belongs to exactly one group. Any two **overlapping** ranges must belong to the **same** group. Two ranges are said to be **overlapping** if there exists at least **one** integer that is present in both ranges. For example, `[1, 3]` and `[2, 5]` are overlapping because `2` and `3` occur in both ranges. Return _the **total number** of ways to split_ `ranges` _into two groups_. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** ``` **Input:** ranges = [[6,10],[5,15]] **Output:** 2 **Explanation:** The two ranges are overlapping, so they must be in the same group. Thus, there are two possible ways: - Put both the ranges together in group 1. - Put both the ranges together in group 2. ``` **Example 2:** ``` **Input:** ranges = [[1,3],[10,20],[2,5],[4,8]] **Output:** 4 **Explanation:** Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group. Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group. Thus, there are four possible ways to group them: - All the ranges in group 1. - All the ranges in group 2. - Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2. - Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1. ``` **Constraints:** `1 <= ranges.length <= 105` `ranges[i].length == 2` `0 <= starti <= endi <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "ranges = [[6,10],[5,15]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "ranges = [[1,3],[10,20],[2,5],[4,8]]", + "output": "4 " + } + ], + "constraints": [ + "Each range belongs to exactly one group.", + "Any two overlapping ranges must belong to the same group.", + "For example, [1, 3] and [2, 5] are overlapping because 2 and 3 occur in both ranges.", + "1 <= ranges.length <= 105", + "ranges[i].length == 2", + "0 <= starti <= endi <= 109" + ], + "python_template": "class Solution(object):\n def countWays(self, ranges):\n \"\"\"\n :type ranges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countWays(int[][] ranges) {\n \n }\n}", + "metadata": { + "func_name": "countWays" + } +} \ No newline at end of file diff --git a/count-words-obtained-after-adding-a-letter.json b/count-words-obtained-after-adding-a-letter.json new file mode 100644 index 0000000000000000000000000000000000000000..c827d9926a8e9e608d5b7b4ebd26f64b0efb168a --- /dev/null +++ b/count-words-obtained-after-adding-a-letter.json @@ -0,0 +1,33 @@ +{ + "id": 2256, + "name": "count-words-obtained-after-adding-a-letter", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/", + "date": "2022-01-02", + "task_description": "You are given two **0-indexed** arrays of strings `startWords` and `targetWords`. Each string consists of **lowercase English letters** only. For each string in `targetWords`, check if it is possible to choose a string from `startWords` and perform a **conversion operation** on it to be equal to that from `targetWords`. The **conversion operation** is described in the following two steps: **Append** any lowercase letter that is **not present** in the string to its end. For example, if the string is `\"abc\"`, the letters `'d'`, `'e'`, or `'y'` can be added to it, but not `'a'`. If `'d'` is added, the resulting string will be `\"abcd\"`. **Rearrange** the letters of the new string in **any** arbitrary order. For example, `\"abcd\"` can be rearranged to `\"acbd\"`, `\"bacd\"`, `\"cbda\"`, and so on. Note that it can also be rearranged to `\"abcd\"` itself. Return _the **number of strings** in _`targetWords`_ that can be obtained by performing the operations on **any** string of _`startWords`. **Note** that you will only be verifying if the string in `targetWords` can be obtained from a string in `startWords` by performing the operations. The strings in `startWords` **do not** actually change during this process. **Example 1:** ``` **Input:** startWords = [\"ant\",\"act\",\"tack\"], targetWords = [\"tack\",\"act\",\"acti\"] **Output:** 2 **Explanation:** - In order to form targetWords[0] = \"tack\", we use startWords[1] = \"act\", append 'k' to it, and rearrange \"actk\" to \"tack\". - There is no string in startWords that can be used to obtain targetWords[1] = \"act\". Note that \"act\" does exist in startWords, but we **must** append one letter to the string before rearranging it. - In order to form targetWords[2] = \"acti\", we use startWords[1] = \"act\", append 'i' to it, and rearrange \"acti\" to \"acti\" itself. ``` **Example 2:** ``` **Input:** startWords = [\"ab\",\"a\"], targetWords = [\"abc\",\"abcd\"] **Output:** 1 **Explanation:** - In order to form targetWords[0] = \"abc\", we use startWords[0] = \"ab\", add 'c' to it, and rearrange it to \"abc\". - There is no string in startWords that can be used to obtain targetWords[1] = \"abcd\". ``` **Constraints:** `1 <= startWords.length, targetWords.length <= 5 * 104` `1 <= startWords[i].length, targetWords[j].length <= 26` Each string of `startWords` and `targetWords` consists of lowercase English letters only. No letter occurs more than once in any string of `startWords` or `targetWords`.", + "test_case": [ + { + "label": "Example 1", + "input": "startWords = [\"ant\",\"act\",\"tack\"], targetWords = [\"tack\",\"act\",\"acti\"]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "startWords = [\"ab\",\"a\"], targetWords = [\"abc\",\"abcd\"]", + "output": "1 " + } + ], + "constraints": [ + "For example, if the string is \"abc\", the letters 'd', 'e', or 'y' can be added to it, but not 'a'. If 'd' is added, the resulting string will be \"abcd\".", + "For example, \"abcd\" can be rearranged to \"acbd\", \"bacd\", \"cbda\", and so on. Note that it can also be rearranged to \"abcd\" itself.", + "1 <= startWords.length, targetWords.length <= 5 * 104", + "1 <= startWords[i].length, targetWords[j].length <= 26", + "Each string of startWords and targetWords consists of lowercase English letters only.", + "No letter occurs more than once in any string of startWords or targetWords." + ], + "python_template": "class Solution(object):\n def wordCount(self, startWords, targetWords):\n \"\"\"\n :type startWords: List[str]\n :type targetWords: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int wordCount(String[] startWords, String[] targetWords) {\n \n }\n}", + "metadata": { + "func_name": "wordCount" + } +} \ No newline at end of file diff --git a/count-zero-request-servers.json b/count-zero-request-servers.json new file mode 100644 index 0000000000000000000000000000000000000000..06b315a477e314fbaa952c7d2408cd7445ed6287 --- /dev/null +++ b/count-zero-request-servers.json @@ -0,0 +1,35 @@ +{ + "id": 2833, + "name": "count-zero-request-servers", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/count-zero-request-servers/", + "date": "2023-06-10", + "task_description": "You are given an integer `n` denoting the total number of servers and a **2D** **0-indexed **integer array `logs`, where `logs[i] = [server_id, time]` denotes that the server with id `server_id` received a request at time `time`. You are also given an integer `x` and a **0-indexed** integer array `queries`. Return _a **0-indexed** integer array_ `arr` _of length_ `queries.length` _where_ `arr[i]` _represents the number of servers that **did not receive** any requests during the time interval_ `[queries[i] - x, queries[i]]`. Note that the time intervals are inclusive. **Example 1:** ``` **Input:** n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11] **Output:** [1,2] **Explanation:** For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10]. Hence, only server 3 gets zero requests. For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period. ``` **Example 2:** ``` **Input:** n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4] **Output:** [0,1] **Explanation:** For queries[0]: All servers get at least one request in the duration of [1, 3]. For queries[1]: Only server with id 3 gets no request in the duration [2,4]. ``` **Constraints:** `1 <= n <= 105` `1 <= logs.length <= 105` `1 <= queries.length <= 105` `logs[i].length == 2` `1 <= logs[i][0] <= n` `1 <= logs[i][1] <= 106` `1 <= x <= 105` `x < queries[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]", + "output": "[1,2] " + }, + { + "label": "Example 2", + "input": "n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]", + "output": "[0,1] " + } + ], + "constraints": [ + "1 <= n <= 105", + "1 <= logs.length <= 105", + "1 <= queries.length <= 105", + "logs[i].length == 2", + "1 <= logs[i][0] <= n", + "1 <= logs[i][1] <= 106", + "1 <= x <= 105", + "x < queries[i] <= 106" + ], + "python_template": "class Solution(object):\n def countServers(self, n, logs, x, queries):\n \"\"\"\n :type n: int\n :type logs: List[List[int]]\n :type x: int\n :type queries: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] countServers(int n, int[][] logs, int x, int[] queries) {\n \n }\n}", + "metadata": { + "func_name": "countServers" + } +} \ No newline at end of file diff --git a/counting-words-with-a-given-prefix.json b/counting-words-with-a-given-prefix.json new file mode 100644 index 0000000000000000000000000000000000000000..5896c7e689f1b5abadd067397ccfc0a7c441432b --- /dev/null +++ b/counting-words-with-a-given-prefix.json @@ -0,0 +1,30 @@ +{ + "id": 2292, + "name": "counting-words-with-a-given-prefix", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/counting-words-with-a-given-prefix/", + "date": "2022-02-20", + "task_description": "You are given an array of strings `words` and a string `pref`. Return _the number of strings in _`words`_ that contain _`pref`_ as a **prefix**_. A **prefix** of a string `s` is any leading contiguous substring of `s`. **Example 1:** ``` **Input:** words = [\"pay\",\"**at**tention\",\"practice\",\"**at**tend\"], `pref `= \"at\" **Output:** 2 **Explanation:** The 2 strings that contain \"at\" as a prefix are: \"**at**tention\" and \"**at**tend\". ``` **Example 2:** ``` **Input:** words = [\"leetcode\",\"win\",\"loops\",\"success\"], `pref `= \"code\" **Output:** 0 **Explanation:** There are no strings that contain \"code\" as a prefix. ``` **Constraints:** `1 <= words.length <= 100` `1 <= words[i].length, pref.length <= 100` `words[i]` and `pref` consist of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"pay\",\" at tention\",\"practice\",\" at tend\"], pref = \"at\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "words = [\"leetcode\",\"win\",\"loops\",\"success\"], pref = \"code\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= words.length <= 100", + "1 <= words[i].length, pref.length <= 100", + "words[i] and pref consist of lowercase English letters." + ], + "python_template": "class Solution(object):\n def prefixCount(self, words, pref):\n \"\"\"\n :type words: List[str]\n :type pref: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int prefixCount(String[] words, String pref) {\n \n }\n}", + "metadata": { + "func_name": "prefixCount" + } +} \ No newline at end of file diff --git a/create-components-with-same-value.json b/create-components-with-same-value.json new file mode 100644 index 0000000000000000000000000000000000000000..f9bc91172ba7503d467f301361703a38744d687b --- /dev/null +++ b/create-components-with-same-value.json @@ -0,0 +1,34 @@ +{ + "id": 2531, + "name": "create-components-with-same-value", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/create-components-with-same-value/", + "date": "2022-10-01", + "task_description": "There is an undirected tree with `n` nodes labeled from `0` to `n - 1`. 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. You are allowed to **delete** some edges, splitting the tree into multiple connected components. Let the **value** of a component be the sum of **all** `nums[i]` for which node `i` is in the component. Return_ the **maximum** number of edges you can delete, such that every connected component in the tree has the same value._ **Example 1:** ``` **Input:** nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]] **Output:** 2 **Explanation:** The above figure shows how we can delete the edges [0,1] and [3,4]. The created components are nodes [0], [1,2,3] and [4]. The sum of the values in each component equals 6. It can be proven that no better deletion exists, so the answer is 2. ``` **Example 2:** ``` **Input:** nums = [2], edges = [] **Output:** 0 **Explanation:** There are no edges to be deleted. ``` **Constraints:** `1 <= n <= 2 * 104` `nums.length == n` `1 <= nums[i] <= 50` `edges.length == n - 1` `edges[i].length == 2` `0 <= edges[i][0], edges[i][1] <= n - 1` `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [2], edges = []", + "output": "0 " + } + ], + "constraints": [ + "1 <= n <= 2 * 104", + "nums.length == n", + "1 <= nums[i] <= 50", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= edges[i][0], edges[i][1] <= n - 1", + "edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def componentValue(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 componentValue(int[] nums, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "componentValue" + } +} \ No newline at end of file diff --git a/cycle-length-queries-in-a-tree.json b/cycle-length-queries-in-a-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..e15b937e7e0ed3fe8e392a46e8f73691f81ce1cd --- /dev/null +++ b/cycle-length-queries-in-a-tree.json @@ -0,0 +1,38 @@ +{ + "id": 2597, + "name": "cycle-length-queries-in-a-tree", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/cycle-length-queries-in-a-tree/", + "date": "2022-12-11", + "task_description": "You are given an integer `n`. There is a **complete binary tree** with `2n - 1` nodes. The root of that tree is the node with the value `1`, and every node with a value `val` in the range `[1, 2n - 1 - 1]` has two children where: The left node has the value `2 * val`, and The right node has the value `2 * val + 1`. You are also given a 2D integer array `queries` of length `m`, where `queries[i] = [ai, bi]`. For each query, solve the following problem: Add an edge between the nodes with values `ai` and `bi`. Find the length of the cycle in the graph. Remove the added edge between nodes with values `ai` and `bi`. **Note** that: A **cycle** is a path that starts and ends at the same node, and each edge in the path is visited only once. The length of a cycle is the number of edges visited in the cycle. There could be multiple edges between two nodes in the tree after adding the edge of the query. Return _an array _`answer`_ of length _`m`_ where_ `answer[i]` _is the answer to the_ `ith` _query._ **Example 1:** ``` **Input:** n = 3, queries = [[5,3],[4,7],[2,3]] **Output:** [4,5,3] **Explanation:** The diagrams above show the tree of 23 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge. - After adding the edge between nodes 3 and 5, the graph contains a cycle of nodes [5,2,1,3]. Thus answer to the first query is 4. We delete the added edge and process the next query. - After adding the edge between nodes 4 and 7, the graph contains a cycle of nodes [4,2,1,3,7]. Thus answer to the second query is 5. We delete the added edge and process the next query. - After adding the edge between nodes 2 and 3, the graph contains a cycle of nodes [2,1,3]. Thus answer to the third query is 3. We delete the added edge. ``` **Example 2:** ``` **Input:** n = 2, queries = [[1,2]] **Output:** [2] **Explanation:** The diagram above shows the tree of 22 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge. - After adding the edge between nodes 1 and 2, the graph contains a cycle of nodes [2,1]. Thus answer for the first query is 2. We delete the added edge. ``` **Constraints:** `2 <= n <= 30` `m == queries.length` `1 <= m <= 105` `queries[i].length == 2` `1 <= ai, bi <= 2n - 1` `ai != bi`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, queries = [[5,3],[4,7],[2,3]]", + "output": "[4,5,3] " + }, + { + "label": "Example 2", + "input": "n = 2, queries = [[1,2]]", + "output": "[2] " + } + ], + "constraints": [ + "The left node has the value 2 * val, and", + "The right node has the value 2 * val + 1.", + "A cycle is a path that starts and ends at the same node, and each edge in the path is visited only once.", + "The length of a cycle is the number of edges visited in the cycle.", + "There could be multiple edges between two nodes in the tree after adding the edge of the query.", + "2 <= n <= 30", + "m == queries.length", + "1 <= m <= 105", + "queries[i].length == 2", + "1 <= ai, bi <= 2n - 1", + "ai != bi" + ], + "python_template": "class Solution(object):\n def cycleLengthQueries(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[] cycleLengthQueries(int n, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "cycleLengthQueries" + } +} \ No newline at end of file diff --git a/decode-the-message.json b/decode-the-message.json new file mode 100644 index 0000000000000000000000000000000000000000..fd4ea9bac9cc3c932cd6d54675cb3ed2ee5775ed --- /dev/null +++ b/decode-the-message.json @@ -0,0 +1,33 @@ +{ + "id": 2406, + "name": "decode-the-message", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/decode-the-message/", + "date": "2022-06-26", + "task_description": "You are given the strings `key` and `message`, which represent a cipher key and a secret message, respectively. The steps to decode `message` are as follows: Use the **first** appearance of all 26 lowercase English letters in `key` as the **order** of the substitution table. Align the substitution table with the regular English alphabet. Each letter in `message` is then **substituted** using the table. Spaces `' '` are transformed to themselves. For example, given `key = \"**hap**p**y** **bo**y\"` (actual key would have **at least one** instance of each letter in the alphabet), we have the partial substitution table of (`'h' -> 'a'`, `'a' -> 'b'`, `'p' -> 'c'`, `'y' -> 'd'`, `'b' -> 'e'`, `'o' -> 'f'`). Return _the decoded message_. **Example 1:** ``` **Input:** key = \"the quick brown fox jumps over the lazy dog\", message = \"vkbs bs t suepuv\" **Output:** \"this is a secret\" **Explanation:** The diagram above shows the substitution table. It is obtained by taking the first appearance of each letter in \"**the** **quick** **brown** **f**o**x** **j**u**mps** o**v**er the **lazy** **d**o**g**\". ``` **Example 2:** ``` **Input:** key = \"eljuxhpwnyrdgtqkviszcfmabo\", message = \"zwx hnfx lqantp mnoeius ycgk vcnjrdb\" **Output:** \"the five boxing wizards jump quickly\" **Explanation:** The diagram above shows the substitution table. It is obtained by taking the first appearance of each letter in \"**eljuxhpwnyrdgtqkviszcfmabo**\". ``` **Constraints:** `26 <= key.length <= 2000` `key` consists of lowercase English letters and `' '`. `key` contains every letter in the English alphabet (`'a'` to `'z'`) **at least once**. `1 <= message.length <= 2000` `message` consists of lowercase English letters and `' '`.", + "test_case": [ + { + "label": "Example 1", + "input": "key = \"the quick brown fox jumps over the lazy dog\", message = \"vkbs bs t suepuv\"", + "output": "\"this is a secret\" " + }, + { + "label": "Example 2", + "input": "key = \"eljuxhpwnyrdgtqkviszcfmabo\", message = \"zwx hnfx lqantp mnoeius ycgk vcnjrdb\"", + "output": "\"the five boxing wizards jump quickly\" " + } + ], + "constraints": [ + "For example, given key = \"happy boy\" (actual key would have at least one instance of each letter in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').", + "26 <= key.length <= 2000", + "key consists of lowercase English letters and ' '.", + "key contains every letter in the English alphabet ('a' to 'z') at least once.", + "1 <= message.length <= 2000", + "message consists of lowercase English letters and ' '." + ], + "python_template": "class Solution(object):\n def decodeMessage(self, key, message):\n \"\"\"\n :type key: str\n :type message: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String decodeMessage(String key, String message) {\n \n }\n}", + "metadata": { + "func_name": "decodeMessage" + } +} \ No newline at end of file diff --git a/decremental-string-concatenation.json b/decremental-string-concatenation.json new file mode 100644 index 0000000000000000000000000000000000000000..c35076659d837ffb5027a9503702a85d68f23f17 --- /dev/null +++ b/decremental-string-concatenation.json @@ -0,0 +1,37 @@ +{ + "id": 2854, + "name": "decremental-string-concatenation", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/decremental-string-concatenation/", + "date": "2023-06-10", + "task_description": "You are given a **0-indexed** array `words` containing `n` strings. Let's define a **join** operation `join(x, y)` between two strings `x` and `y` as concatenating them into `xy`. However, if the last character of `x` is equal to the first character of `y`, one of them is **deleted**. For example `join(\"ab\", \"ba\") = \"aba\"` and `join(\"ab\", \"cde\") = \"abcde\"`. You are to perform `n - 1` **join** operations. Let `str0 = words[0]`. Starting from `i = 1` up to `i = n - 1`, for the `ith` operation, you can do one of the following: Make `stri = join(stri - 1, words[i])` Make `stri = join(words[i], stri - 1)` Your task is to **minimize** the length of `strn - 1`. Return _an integer denoting the minimum possible length of_ `strn - 1`. **Example 1:** ``` **Input:** words = [\"aa\",\"ab\",\"bc\"] **Output:** 4 **Explanation: **In this example, we can perform join operations in the following order to minimize the length of str2: str0 = \"aa\" str1 = join(str0, \"ab\") = \"aab\" str2 = join(str1, \"bc\") = \"aabc\" It can be shown that the minimum possible length of str2 is 4. ``` **Example 2:** ``` **Input:** words = [\"ab\",\"b\"] **Output:** 2 **Explanation:** In this example, str0 = \"ab\", there are two ways to get str1: join(str0, \"b\") = \"ab\" or join(\"b\", str0) = \"bab\". The first string, \"ab\", has the minimum length. Hence, the answer is 2. ``` **Example 3:** ``` **Input:** words = [\"aaa\",\"c\",\"aba\"] **Output:** 6 **Explanation:** In this example, we can perform join operations in the following order to minimize the length of str2: str0 = \"aaa\" str1 = join(str0, \"c\") = \"aaac\" str2 = join(\"aba\", str1) = \"abaaac\" It can be shown that the minimum possible length of str2 is 6. ``` **Constraints:** `1 <= words.length <= 1000` `1 <= words[i].length <= 50` Each character in `words[i]` is an English lowercase letter", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"aa\",\"ab\",\"bc\"]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "words = [\"ab\",\"b\"]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "words = [\"aaa\",\"c\",\"aba\"]", + "output": "6 " + } + ], + "constraints": [ + "Make stri = join(stri - 1, words[i])", + "Make stri = join(words[i], stri - 1)", + "1 <= words.length <= 1000", + "1 <= words[i].length <= 50", + "Each character in words[i] is an English lowercase letter" + ], + "python_template": "class Solution(object):\n def minimizeConcatenatedLength(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimizeConcatenatedLength(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "minimizeConcatenatedLength" + } +} \ No newline at end of file diff --git a/delete-greatest-value-in-each-row.json b/delete-greatest-value-in-each-row.json new file mode 100644 index 0000000000000000000000000000000000000000..73861199679dc257f3745fef9595342744494a8f --- /dev/null +++ b/delete-greatest-value-in-each-row.json @@ -0,0 +1,33 @@ +{ + "id": 2585, + "name": "delete-greatest-value-in-each-row", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/delete-greatest-value-in-each-row/", + "date": "2022-12-04", + "task_description": "You are given an `m x n` matrix `grid` consisting of positive integers. Perform the following operation until `grid` becomes empty: Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them. Add the maximum of deleted elements to the answer. **Note** that the number of columns decreases by one after each operation. Return _the answer after performing the operations described above_. **Example 1:** ``` **Input:** grid = [[1,2,4],[3,3,1]] **Output:** 8 **Explanation:** The diagram above shows the removed values in each step. - In the first operation, we remove 4 from the first row and 3 from the second row (notice that, there are two cells with value 3 and we can remove any of them). We add 4 to the answer. - In the second operation, we remove 2 from the first row and 3 from the second row. We add 3 to the answer. - In the third operation, we remove 1 from the first row and 1 from the second row. We add 1 to the answer. The final answer = 4 + 3 + 1 = 8. ``` **Example 2:** ``` **Input:** grid = [[10]] **Output:** 10 **Explanation:** The diagram above shows the removed values in each step. - In the first operation, we remove 10 from the first row. We add 10 to the answer. The final answer = 10. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 50` `1 <= grid[i][j] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,2,4],[3,3,1]]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "grid = [[10]]", + "output": "10 " + } + ], + "constraints": [ + "Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them.", + "Add the maximum of deleted elements to the answer.", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 50", + "1 <= grid[i][j] <= 100" + ], + "python_template": "class Solution(object):\n def deleteGreatestValue(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int deleteGreatestValue(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "deleteGreatestValue" + } +} \ No newline at end of file diff --git a/destroy-sequential-targets.json b/destroy-sequential-targets.json new file mode 100644 index 0000000000000000000000000000000000000000..16a194b65937aaafd0c9263db64ca10fb3034103 --- /dev/null +++ b/destroy-sequential-targets.json @@ -0,0 +1,35 @@ +{ + "id": 2548, + "name": "destroy-sequential-targets", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/destroy-sequential-targets/", + "date": "2022-10-15", + "task_description": "You are given a **0-indexed** array `nums` consisting of positive integers, representing targets on a number line. You are also given an integer `space`. You have a machine which can destroy targets. **Seeding** the machine with some `nums[i]` allows it to destroy all targets with values that can be represented as `nums[i] + c * space`, where `c` is any non-negative integer. You want to destroy the **maximum** number of targets in `nums`. Return_ the **minimum value** of _`nums[i]`_ you can seed the machine with to destroy the maximum number of targets._ **Example 1:** ``` **Input:** nums = [3,7,8,1,1,5], space = 2 **Output:** 1 **Explanation:** If we seed the machine with nums[3], then we destroy all targets equal to 1,3,5,7,9,... In this case, we would destroy 5 total targets (all except for nums[2]). It is impossible to destroy more than 5 targets, so we return nums[3]. ``` **Example 2:** ``` **Input:** nums = [1,3,5,2,4,6], space = 2 **Output:** 1 **Explanation:** Seeding the machine with nums[0], or nums[3] destroys 3 targets. It is not possible to destroy more than 3 targets. Since nums[0] is the minimal integer that can destroy 3 targets, we return 1. ``` **Example 3:** ``` **Input:** nums = [6,2,5], space = 100 **Output:** 2 **Explanation:** Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums[1]. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `1 <= space <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,7,8,1,1,5], space = 2", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,5,2,4,6], space = 2", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [6,2,5], space = 100", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "1 <= space <= 109" + ], + "python_template": "class Solution(object):\n def destroyTargets(self, nums, space):\n \"\"\"\n :type nums: List[int]\n :type space: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int destroyTargets(int[] nums, int space) {\n \n }\n}", + "metadata": { + "func_name": "destroyTargets" + } +} \ No newline at end of file diff --git a/destroying-asteroids.json b/destroying-asteroids.json new file mode 100644 index 0000000000000000000000000000000000000000..332f0ec370551aeb7a435e821cc304250cf00a00 --- /dev/null +++ b/destroying-asteroids.json @@ -0,0 +1,30 @@ +{ + "id": 2245, + "name": "destroying-asteroids", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/destroying-asteroids/", + "date": "2021-12-26", + "task_description": "You are given an integer `mass`, which represents the original mass of a planet. You are further given an integer array `asteroids`, where `asteroids[i]` is the mass of the `ith` asteroid. You can arrange for the planet to collide with the asteroids in **any arbitrary order**. If the mass of the planet is greater than or equal to the mass of the asteroid, the asteroid is **destroyed** and the planet **gains** the mass of the asteroid. Otherwise, the planet is destroyed. Return `true`_ if **all** asteroids can be destroyed. Otherwise, return _`false`_._ **Example 1:** ``` **Input:** mass = 10, asteroids = [3,9,19,5,21] **Output:** true **Explanation:** One way to order the asteroids is [9,19,5,3,21]: - The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19 - The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38 - The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43 - The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46 - The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67 All asteroids are destroyed. ``` **Example 2:** ``` **Input:** mass = 5, asteroids = [4,9,23,4] **Output:** false **Explanation:** The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23. After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22. This is less than 23, so a collision would not destroy the last asteroid. ``` **Constraints:** `1 <= mass <= 105` `1 <= asteroids.length <= 105` `1 <= asteroids[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "mass = 10, asteroids = [3,9,19,5,21]", + "output": "true " + }, + { + "label": "Example 2", + "input": "mass = 5, asteroids = [4,9,23,4]", + "output": "false " + } + ], + "constraints": [ + "1 <= mass <= 105", + "1 <= asteroids.length <= 105", + "1 <= asteroids[i] <= 105" + ], + "python_template": "class Solution(object):\n def asteroidsDestroyed(self, mass, asteroids):\n \"\"\"\n :type mass: int\n :type asteroids: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean asteroidsDestroyed(int mass, int[] asteroids) {\n \n }\n}", + "metadata": { + "func_name": "asteroidsDestroyed" + } +} \ No newline at end of file diff --git a/determine-if-a-cell-is-reachable-at-a-given-time.json b/determine-if-a-cell-is-reachable-at-a-given-time.json new file mode 100644 index 0000000000000000000000000000000000000000..58855ddf9553d87581cc460ab522e551b3970c88 --- /dev/null +++ b/determine-if-a-cell-is-reachable-at-a-given-time.json @@ -0,0 +1,29 @@ +{ + "id": 3056, + "name": "determine-if-a-cell-is-reachable-at-a-given-time", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/", + "date": "2023-09-03", + "task_description": "You are given four integers `sx`, `sy`, `fx`, `fy`, and a **non-negative** integer `t`. In an infinite 2D grid, you start at the cell `(sx, sy)`. Each second, you **must** move to any of its adjacent cells. Return `true` _if you can reach cell _`(fx, fy)` _after** exactly**_ `t` **_seconds_**, _or_ `false` _otherwise_. A cell's **adjacent cells** are the 8 cells around it that share at least one corner with it. You can visit the same cell several times. **Example 1:** ``` **Input:** sx = 2, sy = 4, fx = 7, fy = 7, t = 6 **Output:** true **Explanation:** Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. ``` **Example 2:** ``` **Input:** sx = 3, sy = 1, fx = 7, fy = 3, t = 3 **Output:** false **Explanation:** Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second. ``` **Constraints:** `1 <= sx, sy, fx, fy <= 109` `0 <= t <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "sx = 2, sy = 4, fx = 7, fy = 7, t = 6", + "output": "true " + }, + { + "label": "Example 2", + "input": "sx = 3, sy = 1, fx = 7, fy = 3, t = 3", + "output": "false " + } + ], + "constraints": [ + "1 <= sx, sy, fx, fy <= 109", + "0 <= t <= 109" + ], + "python_template": "class Solution(object):\n def isReachableAtTime(self, sx, sy, fx, fy, t):\n \"\"\"\n :type sx: int\n :type sy: int\n :type fx: int\n :type fy: int\n :type t: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isReachableAtTime(int sx, int sy, int fx, int fy, int t) {\n \n }\n}", + "metadata": { + "func_name": "isReachableAtTime" + } +} \ No newline at end of file diff --git a/determine-the-minimum-sum-of-a-k-avoiding-array.json b/determine-the-minimum-sum-of-a-k-avoiding-array.json new file mode 100644 index 0000000000000000000000000000000000000000..0ffd25f7b3ecf570302cae69e4c0ac5d95939e60 --- /dev/null +++ b/determine-the-minimum-sum-of-a-k-avoiding-array.json @@ -0,0 +1,28 @@ +{ + "id": 2811, + "name": "determine-the-minimum-sum-of-a-k-avoiding-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/determine-the-minimum-sum-of-a-k-avoiding-array/", + "date": "2023-08-13", + "task_description": "You are given two integers, `n` and `k`. An array of **distinct** positive integers is called a k-avoiding array if there does not exist any pair of distinct elements that sum to `k`. Return _the **minimum** possible sum of a k-avoiding array of length _`n`. **Example 1:** ``` **Input:** n = 5, k = 4 **Output:** 18 **Explanation:** Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18. It can be proven that there is no k-avoiding array with a sum less than 18. ``` **Example 2:** ``` **Input:** n = 2, k = 6 **Output:** 3 **Explanation:** We can construct the array [1,2], which has a sum of 3. It can be proven that there is no k-avoiding array with a sum less than 3. ``` **Constraints:** `1 <= n, k <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, k = 4", + "output": "18 " + }, + { + "label": "Example 2", + "input": "n = 2, k = 6", + "output": "3 " + } + ], + "constraints": [ + "1 <= n, k <= 50" + ], + "python_template": "class Solution(object):\n def minimumSum(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumSum(int n, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumSum" + } +} \ No newline at end of file diff --git a/determine-the-winner-of-a-bowling-game.json b/determine-the-winner-of-a-bowling-game.json new file mode 100644 index 0000000000000000000000000000000000000000..45e4a43016a310db0cdd21a24695db8df0ecd741 --- /dev/null +++ b/determine-the-winner-of-a-bowling-game.json @@ -0,0 +1,45 @@ +{ + "id": 2684, + "name": "determine-the-winner-of-a-bowling-game", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/determine-the-winner-of-a-bowling-game/", + "date": "2023-04-23", + "task_description": "You are given two **0-indexed** integer arrays `player1` and `player2`, representing the number of pins that player 1 and player 2 hit in a bowling game, respectively. The bowling game consists of `n` turns, and the number of pins in each turn is exactly 10. Assume a player hits `xi` pins in the ith turn. The value of the ith turn for the player is: `2xi` if the player hits 10 pins in either (i - 1)th or (i - 2)th turn. Otherwise, it is `xi`. The **score** of the player is the sum of the values of their `n` turns. Return 1 if the score of player 1 is more than the score of player 2, 2 if the score of player 2 is more than the score of player 1, and 0 in case of a draw. **Example 1:** **Input:** player1 = [5,10,3,2], player2 = [6,5,7,3] **Output:** 1 **Explanation:** The score of player 1 is 5 + 10 + 2*3 + 2*2 = 25. The score of player 2 is 6 + 5 + 7 + 3 = 21. **Example 2:** **Input:** player1 = [3,5,7,6], player2 = [8,10,10,2] **Output:** 2 **Explanation:** The score of player 1 is 3 + 5 + 7 + 6 = 21. The score of player 2 is 8 + 10 + 2*10 + 2*2 = 42. **Example 3:** **Input:** player1 = [2,3], player2 = [4,1] **Output:** 0 **Explanation:** The score of player1 is 2 + 3 = 5. The score of player2 is 4 + 1 = 5. **Example 4:** **Input:** player1 = [1,1,1,10,10,10,10], player2 = [10,10,10,10,1,1,1] **Output:** 2 **Explanation:** The score of player1 is 1 + 1 + 1 + 10 + 2*10 + 2*10 + 2*10 = 73. The score of player2 is 10 + 2*10 + 2*10 + 2*10 + 2*1 + 2*1 + 1 = 75. **Constraints:** `n == player1.length == player2.length` `1 <= n <= 1000` `0 <= player1[i], player2[i] <= 10`", + "test_case": [ + { + "label": "Example 1", + "input": "player1 = [5,10,3,2], player2 = [6,5,7,3]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "player1 = [3,5,7,6], player2 = [8,10,10,2]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "player1 = [2,3], player2 = [4,1]", + "output": "0 " + }, + { + "label": "Example 4", + "input": "player1 = [1,1,1,10,10,10,10], player2 = [10,10,10,10,1,1,1]", + "output": "2 " + } + ], + "constraints": [ + "2xi if the player hits 10 pins in either (i - 1)th or (i - 2)th turn.", + "Otherwise, it is xi.", + "1 if the score of player 1 is more than the score of player 2,", + "2 if the score of player 2 is more than the score of player 1, and", + "0 in case of a draw.", + "n == player1.length == player2.length", + "1 <= n <= 1000", + "0 <= player1[i], player2[i] <= 10" + ], + "python_template": "class Solution(object):\n def isWinner(self, player1, player2):\n \"\"\"\n :type player1: List[int]\n :type player2: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int isWinner(int[] player1, int[] player2) {\n \n }\n}", + "metadata": { + "func_name": "isWinner" + } +} \ No newline at end of file diff --git a/difference-between-element-sum-and-digit-sum-of-an-array.json b/difference-between-element-sum-and-digit-sum-of-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..5266a4bfe97b47d804d5f78d47efc0fa916a8e3f --- /dev/null +++ b/difference-between-element-sum-and-digit-sum-of-an-array.json @@ -0,0 +1,31 @@ +{ + "id": 2624, + "name": "difference-between-element-sum-and-digit-sum-of-an-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/", + "date": "2023-01-08", + "task_description": "You are given a positive integer array `nums`. The **element sum** is the sum of all the elements in `nums`. The **digit sum** is the sum of all the digits (not necessarily distinct) that appear in `nums`. Return _the **absolute** difference between the **element sum** and **digit sum** of _`nums`. **Note** that the absolute difference between two integers `x` and `y` is defined as `|x - y|`. **Example 1:** ``` **Input:** nums = [1,15,6,3] **Output:** 9 **Explanation:** The element sum of nums is 1 + 15 + 6 + 3 = 25. The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16. The absolute difference between the element sum and digit sum is |25 - 16| = 9. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** 0 **Explanation:** The element sum of nums is 1 + 2 + 3 + 4 = 10. The digit sum of nums is 1 + 2 + 3 + 4 = 10. The absolute difference between the element sum and digit sum is |10 - 10| = 0. ``` **Constraints:** `1 <= nums.length <= 2000` `1 <= nums[i] <= 2000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,15,6,3]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4]", + "output": "0 " + } + ], + "constraints": [ + "The element sum is the sum of all the elements in nums.", + "The digit sum is the sum of all the digits (not necessarily distinct) that appear in nums.", + "1 <= nums.length <= 2000", + "1 <= nums[i] <= 2000" + ], + "python_template": "class Solution(object):\n def differenceOfSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int differenceOfSum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "differenceOfSum" + } +} \ No newline at end of file diff --git a/difference-between-maximum-and-minimum-price-sum.json b/difference-between-maximum-and-minimum-price-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..aad05eb037b6330c7722749bcc28c3b2e2ac989a --- /dev/null +++ b/difference-between-maximum-and-minimum-price-sum.json @@ -0,0 +1,33 @@ +{ + "id": 2627, + "name": "difference-between-maximum-and-minimum-price-sum", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/difference-between-maximum-and-minimum-price-sum/", + "date": "2023-01-08", + "task_description": "There exists an undirected and initially unrooted tree with `n` nodes indexed from `0` to `n - 1`. You are given the integer `n` and a 2D integer array `edges` of length `n - 1`, where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree. Each node has an associated price. You are given an integer array `price`, where `price[i]` is the price of the `ith` node. The **price sum** of a given path is the sum of the prices of all nodes lying on that path. The tree can be rooted at any node `root` of your choice. The incurred **cost** after choosing `root` is the difference between the maximum and minimum **price sum** amongst all paths starting at `root`. Return _the **maximum** possible **cost**_ _amongst all possible root choices_. **Example 1:** ``` **Input:** n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5] **Output:** 24 **Explanation:** The diagram above denotes the tree after rooting it at node 2. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum. - The first path contains nodes [2,1,3,4]: the prices are [7,8,6,10], and the sum of the prices is 31. - The second path contains the node [2] with the price [7]. The difference between the maximum and minimum price sum is 24. It can be proved that 24 is the maximum cost. ``` **Example 2:** ``` **Input:** n = 3, edges = [[0,1],[1,2]], price = [1,1,1] **Output:** 2 **Explanation:** The diagram above denotes the tree after rooting it at node 0. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum. - The first path contains nodes [0,1,2]: the prices are [1,1,1], and the sum of the prices is 3. - The second path contains node [0] with a price [1]. The difference between the maximum and minimum price sum is 2. It can be proved that 2 is the maximum cost. ``` **Constraints:** `1 <= n <= 105` `edges.length == n - 1` `0 <= ai, bi <= n - 1` `edges` represents a valid tree. `price.length == n` `1 <= price[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]", + "output": "24 " + }, + { + "label": "Example 2", + "input": "n = 3, edges = [[0,1],[1,2]], price = [1,1,1]", + "output": "2 " + } + ], + "constraints": [ + "1 <= n <= 105", + "edges.length == n - 1", + "0 <= ai, bi <= n - 1", + "edges represents a valid tree.", + "price.length == n", + "1 <= price[i] <= 105" + ], + "python_template": "class Solution(object):\n def maxOutput(self, n, edges, price):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type price: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxOutput(int n, int[][] edges, int[] price) {\n \n }\n}", + "metadata": { + "func_name": "maxOutput" + } +} \ No newline at end of file diff --git a/difference-of-number-of-distinct-values-on-diagonals.json b/difference-of-number-of-distinct-values-on-diagonals.json new file mode 100644 index 0000000000000000000000000000000000000000..2949a80bdd21bb0dd00492b969d26b78a7c27d56 --- /dev/null +++ b/difference-of-number-of-distinct-values-on-diagonals.json @@ -0,0 +1,38 @@ +{ + "id": 2801, + "name": "difference-of-number-of-distinct-values-on-diagonals", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/difference-of-number-of-distinct-values-on-diagonals/", + "date": "2023-05-21", + "task_description": "Given a 2D `grid` of size `m x n`, you should find the matrix `answer` of size `m x n`. The cell `answer[r][c]` is calculated by looking at the diagonal values of the cell `grid[r][c]`: Let `leftAbove[r][c]` be the number of **distinct** values on the diagonal to the left and above the cell `grid[r][c]` not including the cell `grid[r][c]` itself. Let `rightBelow[r][c]` be the number of **distinct** values on the diagonal to the right and below the cell `grid[r][c]`, not including the cell `grid[r][c]` itself. Then `answer[r][c] = |leftAbove[r][c] - rightBelow[r][c]|`. A **matrix diagonal** is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until the end of the matrix is reached. For example, in the below diagram the diagonal is highlighted using the cell with indices `(2, 3)` colored gray: Red-colored cells are left and above the cell. Blue-colored cells are right and below the cell. Return the matrix `answer`. **Example 1:** **Input:** grid = [[1,2,3],[3,1,5],[3,2,1]] **Output:** Output: [[1,1,0],[1,0,1],[0,1,1]] **Explanation:** To calculate the `answer` cells: answer left-above elements leftAbove right-below elements rightBelow |leftAbove - rightBelow| [0][0] [] 0 [grid[1][1], grid[2][2]] |{1, 1}| = 1 1 [0][1] [] 0 [grid[1][2]] |{5}| = 1 1 [0][2] [] 0 [] 0 0 [1][0] [] 0 [grid[2][1]] |{2}| = 1 1 [1][1] [grid[0][0]] |{1}| = 1 [grid[2][2]] |{1}| = 1 0 [1][2] [grid[0][1]] |{2}| = 1 [] 0 1 [2][0] [] 0 [] 0 0 [2][1] [grid[1][0]] |{3}| = 1 [] 0 1 [2][2] [grid[0][0], grid[1][1]] |{1, 1}| = 1 [] 0 1 **Example 2:** **Input:** grid = [[1]] **Output:** Output: [[0]] **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n, grid[i][j] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,2,3],[3,1,5],[3,2,1]]", + "output": "[[1,1,0],[1,0,1],[0,1,1]] " + }, + { + "label": "Example 2", + "input": "grid = [[1]]", + "output": "[[0]] Constraints: m == grid.length n == grid[i].length 1 <= m, n, grid[i][j] <= 5" + } + ], + "constraints": [ + "Let leftAbove[r][c] be the number of distinct values on the diagonal to the left and above the cell grid[r][c] not including the cell grid[r][c] itself.", + "Let rightBelow[r][c] be the number of distinct values on the diagonal to the right and below the cell grid[r][c], not including the cell grid[r][c] itself.", + "Then answer[r][c] = |leftAbove[r][c] - rightBelow[r][c]|.", + "For example, in the below diagram the diagonal is highlighted using the cell with indices (2, 3) colored gray:\n\n\t\nRed-colored cells are left and above the cell.\nBlue-colored cells are right and below the cell.", + "Red-colored cells are left and above the cell.", + "Blue-colored cells are right and below the cell.", + "Red-colored cells are left and above the cell.", + "Blue-colored cells are right and below the cell.", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n, grid[i][j] <= 50" + ], + "python_template": "class Solution(object):\n def differenceOfDistinctValues(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] differenceOfDistinctValues(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "differenceOfDistinctValues" + } +} \ No newline at end of file diff --git a/digit-operations-to-make-two-integers-equal.json b/digit-operations-to-make-two-integers-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..26cc2e75efc8f1c8b7a8a9da51cf948c98c4d47f --- /dev/null +++ b/digit-operations-to-make-two-integers-equal.json @@ -0,0 +1,40 @@ +{ + "id": 3655, + "name": "digit-operations-to-make-two-integers-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/digit-operations-to-make-two-integers-equal/", + "date": "2024-11-23", + "task_description": "You are given two integers `n` and `m` that consist of the **same** number of digits. You can perform the following operations **any** number of times: Choose **any** digit from `n` that is not 9 and **increase** it by 1. Choose **any** digit from `n` that is not 0 and **decrease** it by 1. The integer `n` must not be a prime number at any point, including its original value and after each operation. The cost of a transformation is the sum of **all** values that `n` takes throughout the operations performed. Return the **minimum** cost to transform `n` into `m`. If it is impossible, return -1. **Example 1:** **Input:** n = 10, m = 12 **Output:** 85 **Explanation:** We perform the following operations: Increase the first digit, now `n = **2**0`. Increase the second digit, now `n = 2**1**`. Increase the second digit, now `n = 2**2**`. Decrease the first digit, now `n = **1**2`. **Example 2:** **Input:** n = 4, m = 8 **Output:** -1 **Explanation:** It is impossible to make `n` equal to `m`. **Example 3:** **Input:** n = 6, m = 2 **Output:** -1 **Explanation:** Since 2 is already a prime, we can't make `n` equal to `m`. **Constraints:** `1 <= n, m < 104` `n` and `m` consist of the same number of digits.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 10, m = 12", + "output": "85 " + }, + { + "label": "Example 2", + "input": "n = 4, m = 8", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "n = 6, m = 2", + "output": "-1 " + } + ], + "constraints": [ + "Choose any digit from n that is not 9 and increase it by 1.", + "Choose any digit from n that is not 0 and decrease it by 1.", + "Increase the first digit, now n = 20.", + "Increase the second digit, now n = 21.", + "Increase the second digit, now n = 22.", + "Decrease the first digit, now n = 12.", + "1 <= n, m < 104", + "n and m consist of the same number of digits." + ], + "python_template": "class Solution(object):\n def minOperations(self, n, m):\n \"\"\"\n :type n: int\n :type m: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minOperations(int n, int m) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/disconnect-path-in-a-binary-matrix-by-at-most-one-flip.json b/disconnect-path-in-a-binary-matrix-by-at-most-one-flip.json new file mode 100644 index 0000000000000000000000000000000000000000..75ca594b03e315d1f3238547f4889c9a13ed7731 --- /dev/null +++ b/disconnect-path-in-a-binary-matrix-by-at-most-one-flip.json @@ -0,0 +1,33 @@ +{ + "id": 2641, + "name": "disconnect-path-in-a-binary-matrix-by-at-most-one-flip", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/", + "date": "2023-01-21", + "task_description": "You are given a **0-indexed** `m x n` **binary** matrix `grid`. You can move from a cell `(row, col)` to any of the cells `(row + 1, col)` or `(row, col + 1)` that has the value `1`. The matrix is **disconnected** if there is no path from `(0, 0)` to `(m - 1, n - 1)`. You can flip the value of **at most one** (possibly none) cell. You **cannot flip** the cells `(0, 0)` and `(m - 1, n - 1)`. Return `true` _if it is possible to make the matrix disconnect or _`false`_ otherwise_. **Note** that flipping a cell changes its value from `0` to `1` or from `1` to `0`. **Example 1:** ``` **Input:** grid = [[1,1,1],[1,0,0],[1,1,1]] **Output:** true **Explanation:** We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid. ``` **Example 2:** ``` **Input:** grid = [[1,1,1],[1,0,1],[1,1,1]] **Output:** false **Explanation:** It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2). ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 1000` `1 <= m * n <= 105` `grid[i][j]` is either `0` or `1`. `grid[0][0] == grid[m - 1][n - 1] == 1`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,1,1],[1,0,0],[1,1,1]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "grid = [[1,1,1],[1,0,1],[1,1,1]]", + "output": "false " + } + ], + "constraints": [ + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 1000", + "1 <= m * n <= 105", + "grid[i][j] is either 0 or 1.", + "grid[0][0] == grid[m - 1][n - 1] == 1" + ], + "python_template": "class Solution(object):\n def isPossibleToCutPath(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isPossibleToCutPath(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "isPossibleToCutPath" + } +} \ No newline at end of file diff --git a/distinct-prime-factors-of-product-of-array.json b/distinct-prime-factors-of-product-of-array.json new file mode 100644 index 0000000000000000000000000000000000000000..75a731199e68c01750a5937a8b63524c123e14d6 --- /dev/null +++ b/distinct-prime-factors-of-product-of-array.json @@ -0,0 +1,31 @@ +{ + "id": 2609, + "name": "distinct-prime-factors-of-product-of-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/distinct-prime-factors-of-product-of-array/", + "date": "2022-12-25", + "task_description": "Given an array of positive integers `nums`, return _the number of **distinct prime factors** in the product of the elements of_ `nums`. **Note** that: A number greater than `1` is called **prime** if it is divisible by only `1` and itself. An integer `val1` is a factor of another integer `val2` if `val2 / val1` is an integer. **Example 1:** ``` **Input:** nums = [2,4,3,7,10,6] **Output:** 4 **Explanation:** The product of all the elements in nums is: 2 * 4 * 3 * 7 * 10 * 6 = 10080 = 25 * 32 * 5 * 7. There are 4 distinct prime factors so we return 4. ``` **Example 2:** ``` **Input:** nums = [2,4,8,16] **Output:** 1 **Explanation:** The product of all the elements in nums is: 2 * 4 * 8 * 16 = 1024 = 210. There is 1 distinct prime factor so we return 1. ``` **Constraints:** `1 <= nums.length <= 104` `2 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,4,3,7,10,6]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [2,4,8,16]", + "output": "1 " + } + ], + "constraints": [ + "A number greater than 1 is called prime if it is divisible by only 1 and itself.", + "An integer val1 is a factor of another integer val2 if val2 / val1 is an integer.", + "1 <= nums.length <= 104", + "2 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def distinctPrimeFactors(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int distinctPrimeFactors(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "distinctPrimeFactors" + } +} \ No newline at end of file diff --git a/distribute-candies-among-children-i.json b/distribute-candies-among-children-i.json new file mode 100644 index 0000000000000000000000000000000000000000..b32c6fed4787f523c0bb3eaecdc5d4c20ed58ea5 --- /dev/null +++ b/distribute-candies-among-children-i.json @@ -0,0 +1,29 @@ +{ + "id": 3199, + "name": "distribute-candies-among-children-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/distribute-candies-among-children-i/", + "date": "2023-10-28", + "task_description": "You are given two positive integers `n` and `limit`. Return _the **total number** of ways to distribute _`n` _candies among _`3`_ children such that no child gets more than _`limit`_ candies._ **Example 1:** ``` **Input:** n = 5, limit = 2 **Output:** 3 **Explanation:** There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1). ``` **Example 2:** ``` **Input:** n = 3, limit = 3 **Output:** 10 **Explanation:** There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0). ``` **Constraints:** `1 <= n <= 50` `1 <= limit <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, limit = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "n = 3, limit = 3", + "output": "10 " + } + ], + "constraints": [ + "1 <= n <= 50", + "1 <= limit <= 50" + ], + "python_template": "class Solution(object):\n def distributeCandies(self, n, limit):\n \"\"\"\n :type n: int\n :type limit: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int distributeCandies(int n, int limit) {\n \n }\n}", + "metadata": { + "func_name": "distributeCandies" + } +} \ No newline at end of file diff --git a/distribute-candies-among-children-ii.json b/distribute-candies-among-children-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..c67f903a0a9cea49518d3142c6a37fbfd710933a --- /dev/null +++ b/distribute-candies-among-children-ii.json @@ -0,0 +1,29 @@ +{ + "id": 3201, + "name": "distribute-candies-among-children-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/distribute-candies-among-children-ii/", + "date": "2023-10-28", + "task_description": "You are given two positive integers `n` and `limit`. Return _the **total number** of ways to distribute _`n` _candies among _`3`_ children such that no child gets more than _`limit`_ candies._ **Example 1:** ``` **Input:** n = 5, limit = 2 **Output:** 3 **Explanation:** There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1). ``` **Example 2:** ``` **Input:** n = 3, limit = 3 **Output:** 10 **Explanation:** There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0). ``` **Constraints:** `1 <= n <= 106` `1 <= limit <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, limit = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "n = 3, limit = 3", + "output": "10 " + } + ], + "constraints": [ + "1 <= n <= 106", + "1 <= limit <= 106" + ], + "python_template": "class Solution(object):\n def distributeCandies(self, n, limit):\n \"\"\"\n :type n: int\n :type limit: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long distributeCandies(int n, int limit) {\n \n }\n}", + "metadata": { + "func_name": "distributeCandies" + } +} \ No newline at end of file diff --git a/distribute-elements-into-two-arrays-i.json b/distribute-elements-into-two-arrays-i.json new file mode 100644 index 0000000000000000000000000000000000000000..c0e8889a1df652c4221a1a65dae0213ddf810f5c --- /dev/null +++ b/distribute-elements-into-two-arrays-i.json @@ -0,0 +1,31 @@ +{ + "id": 3347, + "name": "distribute-elements-into-two-arrays-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/distribute-elements-into-two-arrays-i/", + "date": "2024-02-25", + "task_description": "You are given a **1-indexed** array of **distinct** integers `nums` of length `n`. You need to distribute all the elements of `nums` between two arrays `arr1` and `arr2` using `n` operations. In the first operation, append `nums[1]` to `arr1`. In the second operation, append `nums[2]` to `arr2`. Afterwards, in the `ith` operation: If the last element of `arr1` is** greater** than the last element of `arr2`, append `nums[i]` to `arr1`. Otherwise, append `nums[i]` to `arr2`. The array `result` is formed by concatenating the arrays `arr1` and `arr2`. For example, if `arr1 == [1,2,3]` and `arr2 == [4,5,6]`, then `result = [1,2,3,4,5,6]`. Return _the array_ `result`. **Example 1:** ``` **Input:** nums = [2,1,3] **Output:** [2,3,1] **Explanation:** After the first 2 operations, arr1 = [2] and arr2 = [1]. In the 3rd operation, as the last element of arr1 is greater than the last element of arr2 (2 > 1), append nums[3] to arr1. After 3 operations, arr1 = [2,3] and arr2 = [1]. Hence, the array result formed by concatenation is [2,3,1]. ``` **Example 2:** ``` **Input:** nums = [5,4,3,8] **Output:** [5,3,4,8] **Explanation:** After the first 2 operations, arr1 = [5] and arr2 = [4]. In the 3rd operation, as the last element of arr1 is greater than the last element of arr2 (5 > 4), append nums[3] to arr1, hence arr1 becomes [5,3]. In the 4th operation, as the last element of arr2 is greater than the last element of arr1 (4 > 3), append nums[4] to arr2, hence arr2 becomes [4,8]. After 4 operations, arr1 = [5,3] and arr2 = [4,8]. Hence, the array result formed by concatenation is [5,3,4,8]. ``` **Constraints:** `3 <= n <= 50` `1 <= nums[i] <= 100` All elements in `nums` are distinct.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,3]", + "output": "[2,3,1] " + }, + { + "label": "Example 2", + "input": "nums = [5,4,3,8]", + "output": "[5,3,4,8] " + } + ], + "constraints": [ + "If the last element of arr1 is greater than the last element of arr2, append nums[i] to arr1. Otherwise, append nums[i] to arr2.", + "3 <= n <= 50", + "1 <= nums[i] <= 100", + "All elements in nums are distinct." + ], + "python_template": "class Solution(object):\n def resultArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] resultArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "resultArray" + } +} \ No newline at end of file diff --git a/distribute-elements-into-two-arrays-ii.json b/distribute-elements-into-two-arrays-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..6fcac4211c790881c2a17b0164719bdbcc4a0726 --- /dev/null +++ b/distribute-elements-into-two-arrays-ii.json @@ -0,0 +1,38 @@ +{ + "id": 3350, + "name": "distribute-elements-into-two-arrays-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/distribute-elements-into-two-arrays-ii/", + "date": "2024-02-25", + "task_description": "You are given a **1-indexed** array of integers `nums` of length `n`. We define a function `greaterCount` such that `greaterCount(arr, val)` returns the number of elements in `arr` that are **strictly greater** than `val`. You need to distribute all the elements of `nums` between two arrays `arr1` and `arr2` using `n` operations. In the first operation, append `nums[1]` to `arr1`. In the second operation, append `nums[2]` to `arr2`. Afterwards, in the `ith` operation: If `greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i])`, append `nums[i]` to `arr1`. If `greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i])`, append `nums[i]` to `arr2`. If `greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])`, append `nums[i]` to the array with a **lesser** number of elements. If there is still a tie, append `nums[i]` to `arr1`. The array `result` is formed by concatenating the arrays `arr1` and `arr2`. For example, if `arr1 == [1,2,3]` and `arr2 == [4,5,6]`, then `result = [1,2,3,4,5,6]`. Return _the integer array_ `result`. **Example 1:** ``` **Input:** nums = [2,1,3,3] **Output:** [2,3,1,3] **Explanation:** After the first 2 operations, arr1 = [2] and arr2 = [1]. In the 3rd operation, the number of elements greater than 3 is zero in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1. In the 4th operation, the number of elements greater than 3 is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2. After 4 operations, arr1 = [2,3] and arr2 = [1,3]. Hence, the array result formed by concatenation is [2,3,1,3]. ``` **Example 2:** ``` **Input:** nums = [5,14,3,1,2] **Output:** [5,3,1,2,14] **Explanation:** After the first 2 operations, arr1 = [5] and arr2 = [14]. In the 3rd operation, the number of elements greater than 3 is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1. In the 4th operation, the number of elements greater than 1 is greater in arr1 than arr2 (2 > 1). Hence, append nums[4] to arr1. In the 5th operation, the number of elements greater than 2 is greater in arr1 than arr2 (2 > 1). Hence, append nums[5] to arr1. After 5 operations, arr1 = [5,3,1,2] and arr2 = [14]. Hence, the array result formed by concatenation is [5,3,1,2,14]. ``` **Example 3:** ``` **Input:** nums = [3,3,3,3] **Output:** [3,3,3,3] **Explanation:** At the end of 4 operations, arr1 = [3,3] and arr2 = [3,3]. Hence, the array result formed by concatenation is [3,3,3,3]. ``` **Constraints:** `3 <= n <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,3,3]", + "output": "[2,3,1,3] " + }, + { + "label": "Example 2", + "input": "nums = [5,14,3,1,2]", + "output": "[5,3,1,2,14] " + }, + { + "label": "Example 3", + "input": "nums = [3,3,3,3]", + "output": "[3,3,3,3] " + } + ], + "constraints": [ + "If greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]), append nums[i] to arr1.", + "If greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i]), append nums[i] to arr2.", + "If greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]), append nums[i] to the array with a lesser number of elements.", + "If there is still a tie, append nums[i] to arr1.", + "3 <= n <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def resultArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] resultArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "resultArray" + } +} \ No newline at end of file diff --git a/distribute-money-to-maximum-children.json b/distribute-money-to-maximum-children.json new file mode 100644 index 0000000000000000000000000000000000000000..8c171874f6e8420f99e47d4eb62146340b9826a3 --- /dev/null +++ b/distribute-money-to-maximum-children.json @@ -0,0 +1,32 @@ +{ + "id": 2663, + "name": "distribute-money-to-maximum-children", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/distribute-money-to-maximum-children/", + "date": "2023-03-04", + "task_description": "You are given an integer `money` denoting the amount of money (in dollars) that you have and another integer `children` denoting the number of children that you must distribute the money to. You have to distribute the money according to the following rules: All money must be distributed. Everyone must receive at least `1` dollar. Nobody receives `4` dollars. Return _the **maximum** number of children who may receive **exactly** _`8` _dollars if you distribute the money according to the aforementioned rules_. If there is no way to distribute the money, return `-1`. **Example 1:** ``` **Input:** money = 20, children = 3 **Output:** 1 **Explanation:** The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is: - 8 dollars to the first child. - 9 dollars to the second child. - 3 dollars to the third child. It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1. ``` **Example 2:** ``` **Input:** money = 16, children = 2 **Output:** 2 **Explanation:** Each child can be given 8 dollars. ``` **Constraints:** `1 <= money <= 200` `2 <= children <= 30`", + "test_case": [ + { + "label": "Example 1", + "input": "money = 20, children = 3", + "output": "1 " + }, + { + "label": "Example 2", + "input": "money = 16, children = 2", + "output": "2 " + } + ], + "constraints": [ + "All money must be distributed.", + "Everyone must receive at least 1 dollar.", + "Nobody receives 4 dollars.", + "1 <= money <= 200", + "2 <= children <= 30" + ], + "python_template": "class Solution(object):\n def distMoney(self, money, children):\n \"\"\"\n :type money: int\n :type children: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int distMoney(int money, int children) {\n \n }\n}", + "metadata": { + "func_name": "distMoney" + } +} \ No newline at end of file diff --git a/divide-a-string-into-groups-of-size-k.json b/divide-a-string-into-groups-of-size-k.json new file mode 100644 index 0000000000000000000000000000000000000000..4b7349194fb0d98f6c1fe78540033f2aaf2fd295 --- /dev/null +++ b/divide-a-string-into-groups-of-size-k.json @@ -0,0 +1,33 @@ +{ + "id": 2260, + "name": "divide-a-string-into-groups-of-size-k", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/", + "date": "2022-01-09", + "task_description": "A string `s` can be partitioned into groups of size `k` using the following procedure: The first group consists of the first `k` characters of the string, the second group consists of the next `k` characters of the string, and so on. Each element can be a part of **exactly one** group. For the last group, if the string **does not** have `k` characters remaining, a character `fill` is used to complete the group. Note that the partition is done so that after removing the `fill` character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be `s`. Given the string `s`, the size of each group `k` and the character `fill`, return _a string array denoting the **composition of every group** _`s`_ has been divided into, using the above procedure_. **Example 1:** ``` **Input:** s = \"abcdefghi\", k = 3, fill = \"x\" **Output:** [\"abc\",\"def\",\"ghi\"] **Explanation:** The first 3 characters \"abc\" form the first group. The next 3 characters \"def\" form the second group. The last 3 characters \"ghi\" form the third group. Since all groups can be completely filled by characters from the string, we do not need to use fill. Thus, the groups formed are \"abc\", \"def\", and \"ghi\". ``` **Example 2:** ``` **Input:** s = \"abcdefghij\", k = 3, fill = \"x\" **Output:** [\"abc\",\"def\",\"ghi\",\"jxx\"] **Explanation:** Similar to the previous example, we are forming the first three groups \"abc\", \"def\", and \"ghi\". For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice. Thus, the 4 groups formed are \"abc\", \"def\", \"ghi\", and \"jxx\". ``` **Constraints:** `1 <= s.length <= 100` `s` consists of lowercase English letters only. `1 <= k <= 100` `fill` is a lowercase English letter.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcdefghi\", k = 3, fill = \"x\"", + "output": "[\"abc\",\"def\",\"ghi\"] " + }, + { + "label": "Example 2", + "input": "s = \"abcdefghij\", k = 3, fill = \"x\"", + "output": "[\"abc\",\"def\",\"ghi\",\"jxx\"] " + } + ], + "constraints": [ + "The first group consists of the first k characters of the string, the second group consists of the next k characters of the string, and so on. Each element can be a part of exactly one group.", + "For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.", + "1 <= s.length <= 100", + "s consists of lowercase English letters only.", + "1 <= k <= 100", + "fill is a lowercase English letter." + ], + "python_template": "class Solution(object):\n def divideString(self, s, k, fill):\n \"\"\"\n :type s: str\n :type k: int\n :type fill: str\n :rtype: List[str]\n \"\"\"\n ", + "java_template": "class Solution {\n public String[] divideString(String s, int k, char fill) {\n \n }\n}", + "metadata": { + "func_name": "divideString" + } +} \ No newline at end of file diff --git a/divide-an-array-into-subarrays-with-minimum-cost-i.json b/divide-an-array-into-subarrays-with-minimum-cost-i.json new file mode 100644 index 0000000000000000000000000000000000000000..c1332b508140838ed297c47b4512bf3f8d3703d7 --- /dev/null +++ b/divide-an-array-into-subarrays-with-minimum-cost-i.json @@ -0,0 +1,34 @@ +{ + "id": 3263, + "name": "divide-an-array-into-subarrays-with-minimum-cost-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i/", + "date": "2024-01-06", + "task_description": "You are given an array of integers `nums` of length `n`. The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`. You need to divide `nums` into `3` **disjoint contiguous **subarrays. Return _the **minimum** possible **sum** of the cost of these subarrays_. **Example 1:** ``` **Input:** nums = [1,2,3,12] **Output:** 6 **Explanation:** The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6. The other possible ways to form 3 subarrays are: - [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15. - [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16. ``` **Example 2:** ``` **Input:** nums = [5,4,3] **Output:** 12 **Explanation:** The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12. It can be shown that 12 is the minimum cost achievable. ``` **Example 3:** ``` **Input:** nums = [10,3,1,1] **Output:** 12 **Explanation:** The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12. It can be shown that 12 is the minimum cost achievable. ``` **Constraints:** `3 <= n <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,12]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [5,4,3]", + "output": "12 " + }, + { + "label": "Example 3", + "input": "nums = [10,3,1,1]", + "output": "12 " + } + ], + "constraints": [ + "3 <= n <= 50", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def minimumCost(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumCost(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumCost" + } +} \ No newline at end of file diff --git a/divide-an-array-into-subarrays-with-minimum-cost-ii.json b/divide-an-array-into-subarrays-with-minimum-cost-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..0cc7f4755ca2c1820360f26a07bef6141dfc479f --- /dev/null +++ b/divide-an-array-into-subarrays-with-minimum-cost-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3260, + "name": "divide-an-array-into-subarrays-with-minimum-cost-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-ii/", + "date": "2024-01-06", + "task_description": "You are given a **0-indexed** array of integers `nums` of length `n`, and two **positive** integers `k` and `dist`. The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`. You need to divide `nums` into `k` **disjoint contiguous **subarrays, such that the difference between the starting index of the **second** subarray and the starting index of the `kth` subarray should be **less than or equal to** `dist`. In other words, if you divide `nums` into the subarrays `nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)]`, then `ik-1 - i1 <= dist`. Return _the **minimum** possible sum of the cost of these_ _subarrays_. **Example 1:** ``` **Input:** nums = [1,3,2,6,4,2], k = 3, dist = 3 **Output:** 5 **Explanation:** The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5. It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5. ``` **Example 2:** ``` **Input:** nums = [10,1,2,2,2,1], k = 4, dist = 3 **Output:** 15 **Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15. The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist. It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15. ``` **Example 3:** ``` **Input:** nums = [10,8,18,9], k = 3, dist = 1 **Output:** 36 **Explanation:** The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36. The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist. It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36. ``` **Constraints:** `3 <= n <= 105` `1 <= nums[i] <= 109` `3 <= k <= n` `k - 2 <= dist <= n - 2`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,2,6,4,2], k = 3, dist = 3", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [10,1,2,2,2,1], k = 4, dist = 3", + "output": "15 " + }, + { + "label": "Example 3", + "input": "nums = [10,8,18,9], k = 3, dist = 1", + "output": "36 " + } + ], + "constraints": [ + "3 <= n <= 105", + "1 <= nums[i] <= 109", + "3 <= k <= n", + "k - 2 <= dist <= n - 2" + ], + "python_template": "class Solution(object):\n def minimumCost(self, nums, k, dist):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type dist: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumCost(int[] nums, int k, int dist) {\n \n }\n}", + "metadata": { + "func_name": "minimumCost" + } +} \ No newline at end of file diff --git a/divide-array-into-arrays-with-max-difference.json b/divide-array-into-arrays-with-max-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..bb4c63b222f9ea8d6f44c134f0dd2481edd520ae --- /dev/null +++ b/divide-array-into-arrays-with-max-difference.json @@ -0,0 +1,40 @@ +{ + "id": 3241, + "name": "divide-array-into-arrays-with-max-difference", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/", + "date": "2023-12-10", + "task_description": "You are given an integer array `nums` of size `n` where `n` is a multiple of 3 and a positive integer `k`. Divide the array `nums` into `n / 3` arrays of size **3** satisfying the following condition: The difference between **any** two elements in one array is **less than or equal** to `k`. Return a **2D** array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return **any** of them. **Example 1:** **Input:** nums = [1,3,4,8,7,9,3,5,1], k = 2 **Output:** [[1,1,3],[3,4,5],[7,8,9]] **Explanation:** The difference between any two elements in each array is less than or equal to 2. **Example 2:** **Input:** nums = [2,4,2,2,5,2], k = 2 **Output:** [] **Explanation:** Different ways to divide `nums` into 2 arrays of size 3 are: [[2,2,2],[2,4,5]] (and its permutations) [[2,2,4],[2,2,5]] (and its permutations) Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since `5 - 2 = 3 > k`, the condition is not satisfied and so there is no valid division. **Example 3:** **Input:** nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14 **Output:** [[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]] **Explanation:** The difference between any two elements in each array is less than or equal to 14. **Constraints:** `n == nums.length` `1 <= n <= 105` `n `is a multiple of 3 `1 <= nums[i] <= 105` `1 <= k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,4,8,7,9,3,5,1], k = 2", + "output": "[[1,1,3],[3,4,5],[7,8,9]] " + }, + { + "label": "Example 2", + "input": "nums = [2,4,2,2,5,2], k = 2", + "output": "[] " + }, + { + "label": "Example 3", + "input": "nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14", + "output": "[[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]] " + } + ], + "constraints": [ + "The difference between any two elements in one array is less than or equal to k.", + "[[2,2,2],[2,4,5]] (and its permutations)", + "[[2,2,4],[2,2,5]] (and its permutations)", + "n == nums.length", + "1 <= n <= 105", + "n is a multiple of 3", + "1 <= nums[i] <= 105", + "1 <= k <= 105" + ], + "python_template": "class Solution(object):\n def divideArray(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] divideArray(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "divideArray" + } +} \ No newline at end of file diff --git a/divide-array-into-equal-pairs.json b/divide-array-into-equal-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..0bb7a9009449cfd2a87760ad8b9b72072e579b94 --- /dev/null +++ b/divide-array-into-equal-pairs.json @@ -0,0 +1,32 @@ +{ + "id": 2308, + "name": "divide-array-into-equal-pairs", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/divide-array-into-equal-pairs/", + "date": "2022-03-05", + "task_description": "You are given an integer array `nums` consisting of `2 * n` integers. You need to divide `nums` into `n` pairs such that: Each element belongs to **exactly one** pair. The elements present in a pair are **equal**. Return `true` _if nums can be divided into_ `n` _pairs, otherwise return_ `false`. **Example 1:** ``` **Input:** nums = [3,2,3,2,2,2] **Output:** true **Explanation:** There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs. If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** false **Explanation:** There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition. ``` **Constraints:** `nums.length == 2 * n` `1 <= n <= 500` `1 <= nums[i] <= 500`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,2,3,2,2,2]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4]", + "output": "false " + } + ], + "constraints": [ + "Each element belongs to exactly one pair.", + "The elements present in a pair are equal.", + "nums.length == 2 * n", + "1 <= n <= 500", + "1 <= nums[i] <= 500" + ], + "python_template": "class Solution(object):\n def divideArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean divideArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "divideArray" + } +} \ No newline at end of file diff --git a/divide-intervals-into-minimum-number-of-groups.json b/divide-intervals-into-minimum-number-of-groups.json new file mode 100644 index 0000000000000000000000000000000000000000..6a28f4ac80d8fb91015246dd84875501a78130c8 --- /dev/null +++ b/divide-intervals-into-minimum-number-of-groups.json @@ -0,0 +1,30 @@ +{ + "id": 2488, + "name": "divide-intervals-into-minimum-number-of-groups", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/", + "date": "2022-09-04", + "task_description": "You are given a 2D integer array `intervals` where `intervals[i] = [lefti, righti]` represents the **inclusive** interval `[lefti, righti]`. You have to divide the intervals into one or more **groups** such that each interval is in **exactly** one group, and no two intervals that are in the same group **intersect** each other. Return _the **minimum** number of groups you need to make_. Two intervals **intersect** if there is at least one common number between them. For example, the intervals `[1, 5]` and `[5, 8]` intersect. **Example 1:** ``` **Input:** intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]] **Output:** 3 **Explanation:** We can divide the intervals into the following groups: - Group 1: [1, 5], [6, 8]. - Group 2: [2, 3], [5, 10]. - Group 3: [1, 10]. It can be proven that it is not possible to divide the intervals into fewer than 3 groups. ``` **Example 2:** ``` **Input:** intervals = [[1,3],[5,6],[8,10],[11,13]] **Output:** 1 **Explanation:** None of the intervals overlap, so we can put all of them in one group. ``` **Constraints:** `1 <= intervals.length <= 105` `intervals[i].length == 2` `1 <= lefti <= righti <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "intervals = [[1,3],[5,6],[8,10],[11,13]]", + "output": "1 " + } + ], + "constraints": [ + "1 <= intervals.length <= 105", + "intervals[i].length == 2", + "1 <= lefti <= righti <= 106" + ], + "python_template": "class Solution(object):\n def minGroups(self, intervals):\n \"\"\"\n :type intervals: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minGroups(int[][] intervals) {\n \n }\n}", + "metadata": { + "func_name": "minGroups" + } +} \ No newline at end of file diff --git a/divide-nodes-into-the-maximum-number-of-groups.json b/divide-nodes-into-the-maximum-number-of-groups.json new file mode 100644 index 0000000000000000000000000000000000000000..bf4c792012984e35621f151d5d13874128e9327c --- /dev/null +++ b/divide-nodes-into-the-maximum-number-of-groups.json @@ -0,0 +1,35 @@ +{ + "id": 2583, + "name": "divide-nodes-into-the-maximum-number-of-groups", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/divide-nodes-into-the-maximum-number-of-groups/", + "date": "2022-11-27", + "task_description": "You are given a positive integer `n` representing the number of nodes in an **undirected** graph. The nodes are labeled from `1` to `n`. You are also given a 2D integer array `edges`, where `edges[i] = [ai, bi]` indicates that there is a **bidirectional** edge between nodes `ai` and `bi`. **Notice** that the given graph may be disconnected. Divide the nodes of the graph into `m` groups (**1-indexed**) such that: Each node in the graph belongs to exactly one group. For every pair of nodes in the graph that are connected by an edge `[ai, bi]`, if `ai` belongs to the group with index `x`, and `bi` belongs to the group with index `y`, then `|y - x| = 1`. Return _the maximum number of groups (i.e., maximum _`m`_) into which you can divide the nodes_. Return `-1` _if it is impossible to group the nodes with the given conditions_. **Example 1:** ``` **Input:** n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]] **Output:** 4 **Explanation:** As shown in the image we: - Add node 5 to the first group. - Add node 1 to the second group. - Add nodes 2 and 4 to the third group. - Add nodes 3 and 6 to the fourth group. We can see that every edge is satisfied. It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied. ``` **Example 2:** ``` **Input:** n = 3, edges = [[1,2],[2,3],[3,1]] **Output:** -1 **Explanation:** If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied. It can be shown that no grouping is possible. ``` **Constraints:** `1 <= n <= 500` `1 <= edges.length <= 104` `edges[i].length == 2` `1 <= ai, bi <= n` `ai != bi` There is at most one edge between any pair of vertices.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 3, edges = [[1,2],[2,3],[3,1]]", + "output": "-1 " + } + ], + "constraints": [ + "Each node in the graph belongs to exactly one group.", + "For every pair of nodes in the graph that are connected by an edge [ai, bi], if ai belongs to the group with index x, and bi belongs to the group with index y, then |y - x| = 1.", + "1 <= n <= 500", + "1 <= edges.length <= 104", + "edges[i].length == 2", + "1 <= ai, bi <= n", + "ai != bi", + "There is at most one edge between any pair of vertices." + ], + "python_template": "class Solution(object):\n def magnificentSets(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 magnificentSets(int n, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "magnificentSets" + } +} \ No newline at end of file diff --git a/divide-players-into-teams-of-equal-skill.json b/divide-players-into-teams-of-equal-skill.json new file mode 100644 index 0000000000000000000000000000000000000000..ee6d8e15abea5b8d98d6141ae24105dfe62dbbc6 --- /dev/null +++ b/divide-players-into-teams-of-equal-skill.json @@ -0,0 +1,35 @@ +{ + "id": 2581, + "name": "divide-players-into-teams-of-equal-skill", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/divide-players-into-teams-of-equal-skill/", + "date": "2022-11-27", + "task_description": "You are given a positive integer array `skill` of **even** length `n` where `skill[i]` denotes the skill of the `ith` player. Divide the players into `n / 2` teams of size `2` such that the total skill of each team is **equal**. The **chemistry** of a team is equal to the **product** of the skills of the players on that team. Return _the sum of the **chemistry** of all the teams, or return _`-1`_ if there is no way to divide the players into teams such that the total skill of each team is equal._ **Example 1:** ``` **Input:** skill = [3,2,5,1,3,4] **Output:** 22 **Explanation:** Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6. The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22. ``` **Example 2:** ``` **Input:** skill = [3,4] **Output:** 12 **Explanation:** The two players form a team with a total skill of 7. The chemistry of the team is 3 * 4 = 12. ``` **Example 3:** ``` **Input:** skill = [1,1,2,3] **Output:** -1 **Explanation:** There is no way to divide the players into teams such that the total skill of each team is equal. ``` **Constraints:** `2 <= skill.length <= 105` `skill.length` is even. `1 <= skill[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "skill = [3,2,5,1,3,4]", + "output": "22 " + }, + { + "label": "Example 2", + "input": "skill = [3,4]", + "output": "12 " + }, + { + "label": "Example 3", + "input": "skill = [1,1,2,3]", + "output": "-1 " + } + ], + "constraints": [ + "2 <= skill.length <= 105", + "skill.length is even.", + "1 <= skill[i] <= 1000" + ], + "python_template": "class Solution(object):\n def dividePlayers(self, skill):\n \"\"\"\n :type skill: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long dividePlayers(int[] skill) {\n \n }\n}", + "metadata": { + "func_name": "dividePlayers" + } +} \ No newline at end of file diff --git a/divisible-and-non-divisible-sums-difference.json b/divisible-and-non-divisible-sums-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..2fa30035bc71974c164ed8cfe411f04d79e0fb68 --- /dev/null +++ b/divisible-and-non-divisible-sums-difference.json @@ -0,0 +1,35 @@ +{ + "id": 3172, + "name": "divisible-and-non-divisible-sums-difference", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/divisible-and-non-divisible-sums-difference/", + "date": "2023-10-01", + "task_description": "You are given positive integers `n` and `m`. Define two integers as follows: `num1`: The sum of all integers in the range `[1, n]` (both **inclusive**) that are **not divisible** by `m`. `num2`: The sum of all integers in the range `[1, n]` (both **inclusive**) that are **divisible** by `m`. Return _the integer_ `num1 - num2`. **Example 1:** ``` **Input:** n = 10, m = 3 **Output:** 19 **Explanation:** In the given example: - Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37. - Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18. We return 37 - 18 = 19 as the answer. ``` **Example 2:** ``` **Input:** n = 5, m = 6 **Output:** 15 **Explanation:** In the given example: - Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15. - Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0. We return 15 - 0 = 15 as the answer. ``` **Example 3:** ``` **Input:** n = 5, m = 1 **Output:** -15 **Explanation:** In the given example: - Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0. - Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15. We return 0 - 15 = -15 as the answer. ``` **Constraints:** `1 <= n, m <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 10, m = 3", + "output": "19 " + }, + { + "label": "Example 2", + "input": "n = 5, m = 6", + "output": "15 " + }, + { + "label": "Example 3", + "input": "n = 5, m = 1", + "output": "-15 " + } + ], + "constraints": [ + "num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m.", + "num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m.", + "1 <= n, m <= 1000" + ], + "python_template": "class Solution(object):\n def differenceOfSums(self, n, m):\n \"\"\"\n :type n: int\n :type m: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int differenceOfSums(int n, int m) {\n \n }\n}", + "metadata": { + "func_name": "differenceOfSums" + } +} \ No newline at end of file diff --git a/double-modular-exponentiation.json b/double-modular-exponentiation.json new file mode 100644 index 0000000000000000000000000000000000000000..0b37a295e1cd7923501659677b4b327261b4fd6c --- /dev/null +++ b/double-modular-exponentiation.json @@ -0,0 +1,33 @@ +{ + "id": 3234, + "name": "double-modular-exponentiation", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/double-modular-exponentiation/", + "date": "2023-12-03", + "task_description": "You are given a **0-indexed** 2D array `variables` where `variables[i] = [ai, bi, ci, mi]`, and an integer `target`. An index `i` is **good** if the following formula holds: `0 <= i < variables.length` `((aibi % 10)ci) % mi == target` Return _an array consisting of **good** indices in **any order**_. **Example 1:** ``` **Input:** variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2 **Output:** [0,2] **Explanation:** For each index i in the variables array: 1) For the index 0, variables[0] = [2,3,3,10], (23 % 10)3 % 10 = 2. 2) For the index 1, variables[1] = [3,3,3,1], (33 % 10)3 % 1 = 0. 3) For the index 2, variables[2] = [6,1,1,4], (61 % 10)1 % 4 = 2. Therefore we return [0,2] as the answer. ``` **Example 2:** ``` **Input:** variables = [[39,3,1000,1000]], target = 17 **Output:** [] **Explanation:** For each index i in the variables array: 1) For the index 0, variables[0] = [39,3,1000,1000], (393 % 10)1000 % 1000 = 1. Therefore we return [] as the answer. ``` **Constraints:** `1 <= variables.length <= 100` `variables[i] == [ai, bi, ci, mi]` `1 <= ai, bi, ci, mi <= 103` `0 <= target <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2", + "output": "[0,2] " + }, + { + "label": "Example 2", + "input": "variables = [[39,3,1000,1000]], target = 17", + "output": "[] " + } + ], + "constraints": [ + "0 <= i < variables.length", + "((aibi % 10)ci) % mi == target", + "1 <= variables.length <= 100", + "variables[i] == [ai, bi, ci, mi]", + "1 <= ai, bi, ci, mi <= 103", + "0 <= target <= 103" + ], + "python_template": "class Solution(object):\n def getGoodIndices(self, variables, target):\n \"\"\"\n :type variables: List[List[int]]\n :type target: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List getGoodIndices(int[][] variables, int target) {\n \n }\n}", + "metadata": { + "func_name": "getGoodIndices" + } +} \ No newline at end of file diff --git a/earliest-possible-day-of-full-bloom.json b/earliest-possible-day-of-full-bloom.json new file mode 100644 index 0000000000000000000000000000000000000000..dca9e71e308ef44c494e9249436387c49bcd7d3a --- /dev/null +++ b/earliest-possible-day-of-full-bloom.json @@ -0,0 +1,37 @@ +{ + "id": 2257, + "name": "earliest-possible-day-of-full-bloom", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/earliest-possible-day-of-full-bloom/", + "date": "2022-01-02", + "task_description": "You have `n` flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two **0-indexed** integer arrays `plantTime` and `growTime`, of length `n` each: `plantTime[i]` is the number of **full days** it takes you to **plant** the `ith` seed. Every day, you can work on planting exactly one seed. You **do not** have to work on planting the same seed on consecutive days, but the planting of a seed is not complete **until** you have worked `plantTime[i]` days on planting it in total. `growTime[i]` is the number of **full days** it takes the `ith` seed to grow after being completely planted. **After** the last day of its growth, the flower **blooms** and stays bloomed forever. From the beginning of day `0`, you can plant the seeds in **any** order. Return _the **earliest** possible day where **all** seeds are blooming_. **Example 1:** ``` **Input:** plantTime = [1,4,3], growTime = [2,3,1] **Output:** 9 **Explanation:** The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms. One optimal way is: On day 0, plant the 0th seed. The seed grows for 2 full days and blooms on day 3. On days 1, 2, 3, and 4, plant the 1st seed. The seed grows for 3 full days and blooms on day 8. On days 5, 6, and 7, plant the 2nd seed. The seed grows for 1 full day and blooms on day 9. Thus, on day 9, all the seeds are blooming. ``` **Example 2:** ``` **Input:** plantTime = [1,2,3,2], growTime = [2,1,2,1] **Output:** 9 **Explanation:** The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms. One optimal way is: On day 1, plant the 0th seed. The seed grows for 2 full days and blooms on day 4. On days 0 and 3, plant the 1st seed. The seed grows for 1 full day and blooms on day 5. On days 2, 4, and 5, plant the 2nd seed. The seed grows for 2 full days and blooms on day 8. On days 6 and 7, plant the 3rd seed. The seed grows for 1 full day and blooms on day 9. Thus, on day 9, all the seeds are blooming. ``` **Example 3:** ``` **Input:** plantTime = [1], growTime = [1] **Output:** 2 **Explanation:** On day 0, plant the 0th seed. The seed grows for 1 full day and blooms on day 2. Thus, on day 2, all the seeds are blooming. ``` **Constraints:** `n == plantTime.length == growTime.length` `1 <= n <= 105` `1 <= plantTime[i], growTime[i] <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "plantTime = [1,4,3], growTime = [2,3,1]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "plantTime = [1,2,3,2], growTime = [2,1,2,1]", + "output": "9 " + }, + { + "label": "Example 3", + "input": "plantTime = [1], growTime = [1]", + "output": "2 " + } + ], + "constraints": [ + "plantTime[i] is the number of full days it takes you to plant the ith seed. Every day, you can work on planting exactly one seed. You do not have to work on planting the same seed on consecutive days, but the planting of a seed is not complete until you have worked plantTime[i] days on planting it in total.", + "growTime[i] is the number of full days it takes the ith seed to grow after being completely planted. After the last day of its growth, the flower blooms and stays bloomed forever.", + "n == plantTime.length == growTime.length", + "1 <= n <= 105", + "1 <= plantTime[i], growTime[i] <= 104" + ], + "python_template": "class Solution(object):\n def earliestFullBloom(self, plantTime, growTime):\n \"\"\"\n :type plantTime: List[int]\n :type growTime: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int earliestFullBloom(int[] plantTime, int[] growTime) {\n \n }\n}", + "metadata": { + "func_name": "earliestFullBloom" + } +} \ No newline at end of file diff --git a/earliest-second-to-mark-indices-i.json b/earliest-second-to-mark-indices-i.json new file mode 100644 index 0000000000000000000000000000000000000000..2ca52e5f9d740269697019b158de889afe7411f5 --- /dev/null +++ b/earliest-second-to-mark-indices-i.json @@ -0,0 +1,39 @@ +{ + "id": 3292, + "name": "earliest-second-to-mark-indices-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/earliest-second-to-mark-indices-i/", + "date": "2024-02-18", + "task_description": "You are given two **1-indexed** integer arrays, `nums` and, `changeIndices`, having lengths `n` and `m`, respectively. Initially, all indices in `nums` are unmarked. Your task is to mark **all** indices in `nums`. In each second, `s`, in order from `1` to `m` (**inclusive**), you can perform **one** of the following operations: Choose an index `i` in the range `[1, n]` and **decrement** `nums[i]` by `1`. If `nums[changeIndices[s]]` is **equal** to `0`, **mark** the index `changeIndices[s]`. Do nothing. Return _an integer denoting the **earliest second** in the range _`[1, m]`_ when **all** indices in _`nums`_ can be marked by choosing operations optimally, or _`-1`_ if it is impossible._ **Example 1:** ``` **Input:** nums = [2,2,0], changeIndices = [2,2,2,2,3,2,2,1] **Output:** 8 **Explanation:** In this example, we have 8 seconds. The following operations can be performed to mark all indices: Second 1: Choose index 1 and decrement nums[1] by one. nums becomes [1,2,0]. Second 2: Choose index 1 and decrement nums[1] by one. nums becomes [0,2,0]. Second 3: Choose index 2 and decrement nums[2] by one. nums becomes [0,1,0]. Second 4: Choose index 2 and decrement nums[2] by one. nums becomes [0,0,0]. Second 5: Mark the index changeIndices[5], which is marking index 3, since nums[3] is equal to 0. Second 6: Mark the index changeIndices[6], which is marking index 2, since nums[2] is equal to 0. Second 7: Do nothing. Second 8: Mark the index changeIndices[8], which is marking index 1, since nums[1] is equal to 0. Now all indices have been marked. It can be shown that it is not possible to mark all indices earlier than the 8th second. Hence, the answer is 8. ``` **Example 2:** ``` **Input:** nums = [1,3], changeIndices = [1,1,1,2,1,1,1] **Output:** 6 **Explanation:** In this example, we have 7 seconds. The following operations can be performed to mark all indices: Second 1: Choose index 2 and decrement nums[2] by one. nums becomes [1,2]. Second 2: Choose index 2 and decrement nums[2] by one. nums becomes [1,1]. Second 3: Choose index 2 and decrement nums[2] by one. nums becomes [1,0]. Second 4: Mark the index changeIndices[4], which is marking index 2, since nums[2] is equal to 0. Second 5: Choose index 1 and decrement nums[1] by one. nums becomes [0,0]. Second 6: Mark the index changeIndices[6], which is marking index 1, since nums[1] is equal to 0. Now all indices have been marked. It can be shown that it is not possible to mark all indices earlier than the 6th second. Hence, the answer is 6. ``` **Example 3:** ``` **Input:** nums = [0,1], changeIndices = [2,2,2] **Output:** -1 **Explanation:** In this example, it is impossible to mark all indices because index 1 isn't in changeIndices. Hence, the answer is -1. ``` **Constraints:** `1 <= n == nums.length <= 2000` `0 <= nums[i] <= 109` `1 <= m == changeIndices.length <= 2000` `1 <= changeIndices[i] <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,2,0], changeIndices = [2,2,2,2,3,2,2,1]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "nums = [1,3], changeIndices = [1,1,1,2,1,1,1]", + "output": "6 " + }, + { + "label": "Example 3", + "input": "nums = [0,1], changeIndices = [2,2,2]", + "output": "-1 " + } + ], + "constraints": [ + "Choose an index i in the range [1, n] and decrement nums[i] by 1.", + "If nums[changeIndices[s]] is equal to 0, mark the index changeIndices[s].", + "Do nothing.", + "1 <= n == nums.length <= 2000", + "0 <= nums[i] <= 109", + "1 <= m == changeIndices.length <= 2000", + "1 <= changeIndices[i] <= n" + ], + "python_template": "class Solution(object):\n def earliestSecondToMarkIndices(self, nums, changeIndices):\n \"\"\"\n :type nums: List[int]\n :type changeIndices: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n \n }\n}", + "metadata": { + "func_name": "earliestSecondToMarkIndices" + } +} \ No newline at end of file diff --git a/earliest-second-to-mark-indices-ii.json b/earliest-second-to-mark-indices-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..72a85eece7f00e5a2f8816a305980e5140ad2a43 --- /dev/null +++ b/earliest-second-to-mark-indices-ii.json @@ -0,0 +1,40 @@ +{ + "id": 3289, + "name": "earliest-second-to-mark-indices-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/earliest-second-to-mark-indices-ii/", + "date": "2024-02-18", + "task_description": "You are given two **1-indexed** integer arrays, `nums` and, `changeIndices`, having lengths `n` and `m`, respectively. Initially, all indices in `nums` are unmarked. Your task is to mark **all** indices in `nums`. In each second, `s`, in order from `1` to `m` (**inclusive**), you can perform **one** of the following operations: Choose an index `i` in the range `[1, n]` and **decrement** `nums[i]` by `1`. Set `nums[changeIndices[s]]` to any **non-negative** value. Choose an index `i` in the range `[1, n]`, where `nums[i]` is **equal** to `0`, and **mark** index `i`. Do nothing. Return _an integer denoting the **earliest second** in the range _`[1, m]`_ when **all** indices in _`nums`_ can be marked by choosing operations optimally, or _`-1`_ if it is impossible._ **Example 1:** ``` **Input:** nums = [3,2,3], changeIndices = [1,3,2,2,2,2,3] **Output:** 6 **Explanation:** In this example, we have 7 seconds. The following operations can be performed to mark all indices: Second 1: Set nums[changeIndices[1]] to 0. nums becomes [0,2,3]. Second 2: Set nums[changeIndices[2]] to 0. nums becomes [0,2,0]. Second 3: Set nums[changeIndices[3]] to 0. nums becomes [0,0,0]. Second 4: Mark index 1, since nums[1] is equal to 0. Second 5: Mark index 2, since nums[2] is equal to 0. Second 6: Mark index 3, since nums[3] is equal to 0. Now all indices have been marked. It can be shown that it is not possible to mark all indices earlier than the 6th second. Hence, the answer is 6. ``` **Example 2:** ``` **Input:** nums = [0,0,1,2], changeIndices = [1,2,1,2,1,2,1,2] **Output:** 7 **Explanation:** In this example, we have 8 seconds. The following operations can be performed to mark all indices: Second 1: Mark index 1, since nums[1] is equal to 0. Second 2: Mark index 2, since nums[2] is equal to 0. Second 3: Decrement index 4 by one. nums becomes [0,0,1,1]. Second 4: Decrement index 4 by one. nums becomes [0,0,1,0]. Second 5: Decrement index 3 by one. nums becomes [0,0,0,0]. Second 6: Mark index 3, since nums[3] is equal to 0. Second 7: Mark index 4, since nums[4] is equal to 0. Now all indices have been marked. It can be shown that it is not possible to mark all indices earlier than the 7th second. Hence, the answer is 7. ``` **Example 3:** ``` **Input:** nums = [1,2,3], changeIndices = [1,2,3] **Output:** -1 **Explanation: **In this example, it can be shown that it is impossible to mark all indices, as we don't have enough seconds. Hence, the answer is -1. ``` **Constraints:** `1 <= n == nums.length <= 5000` `0 <= nums[i] <= 109` `1 <= m == changeIndices.length <= 5000` `1 <= changeIndices[i] <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,2,3], changeIndices = [1,3,2,2,2,2,3]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [0,0,1,2], changeIndices = [1,2,1,2,1,2,1,2]", + "output": "7 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3], changeIndices = [1,2,3]", + "output": "-1 " + } + ], + "constraints": [ + "Choose an index i in the range [1, n] and decrement nums[i] by 1.", + "Set nums[changeIndices[s]] to any non-negative value.", + "Choose an index i in the range [1, n], where nums[i] is equal to 0, and mark index i.", + "Do nothing.", + "1 <= n == nums.length <= 5000", + "0 <= nums[i] <= 109", + "1 <= m == changeIndices.length <= 5000", + "1 <= changeIndices[i] <= n" + ], + "python_template": "class Solution(object):\n def earliestSecondToMarkIndices(self, nums, changeIndices):\n \"\"\"\n :type nums: List[int]\n :type changeIndices: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n \n }\n}", + "metadata": { + "func_name": "earliestSecondToMarkIndices" + } +} \ No newline at end of file diff --git a/eat-pizzas.json b/eat-pizzas.json new file mode 100644 index 0000000000000000000000000000000000000000..b25ec9905acb74ad20a50ad1ff9b8e9a241996bc --- /dev/null +++ b/eat-pizzas.json @@ -0,0 +1,36 @@ +{ + "id": 3779, + "name": "eat-pizzas", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/eat-pizzas/", + "date": "2025-02-09", + "task_description": "You are given an integer array `pizzas` of size `n`, where `pizzas[i]` represents the weight of the `ith` pizza. Every day, you eat **exactly** 4 pizzas. Due to your incredible metabolism, when you eat pizzas of weights `W`, `X`, `Y`, and `Z`, where `W <= X <= Y <= Z`, you gain the weight of only 1 pizza! On **odd-numbered** days **(1-indexed)**, you gain a weight of `Z`. On **even-numbered** days, you gain a weight of `Y`. Find the **maximum** total weight you can gain by eating **all** pizzas optimally. **Note**: It is guaranteed that `n` is a multiple of 4, and each pizza can be eaten only once. **Example 1:** **Input:** pizzas = [1,2,3,4,5,6,7,8] **Output:** 14 **Explanation:** On day 1, you eat pizzas at indices `[1, 2, 4, 7] = [2, 3, 5, 8]`. You gain a weight of 8. On day 2, you eat pizzas at indices `[0, 3, 5, 6] = [1, 4, 6, 7]`. You gain a weight of 6. The total weight gained after eating all the pizzas is `8 + 6 = 14`. **Example 2:** **Input:** pizzas = [2,1,1,1,1,1,1,1] **Output:** 3 **Explanation:** On day 1, you eat pizzas at indices `[4, 5, 6, 0] = [1, 1, 1, 2]`. You gain a weight of 2. On day 2, you eat pizzas at indices `[1, 2, 3, 7] = [1, 1, 1, 1]`. You gain a weight of 1. The total weight gained after eating all the pizzas is `2 + 1 = 3.` **Constraints:** `4 <= n == pizzas.length <= 2 * 105` `1 <= pizzas[i] <= 105` `n` is a multiple of 4.", + "test_case": [ + { + "label": "Example 1", + "input": "pizzas = [1,2,3,4,5,6,7,8]", + "output": "14 " + }, + { + "label": "Example 2", + "input": "pizzas = [2,1,1,1,1,1,1,1]", + "output": "3 " + } + ], + "constraints": [ + "On odd-numbered days (1-indexed), you gain a weight of Z.", + "On even-numbered days, you gain a weight of Y.", + "On day 1, you eat pizzas at indices [1, 2, 4, 7] = [2, 3, 5, 8]. You gain a weight of 8.", + "On day 2, you eat pizzas at indices [0, 3, 5, 6] = [1, 4, 6, 7]. You gain a weight of 6.", + "On day 1, you eat pizzas at indices [4, 5, 6, 0] = [1, 1, 1, 2]. You gain a weight of 2.", + "On day 2, you eat pizzas at indices [1, 2, 3, 7] = [1, 1, 1, 1]. You gain a weight of 1.", + "4 <= n == pizzas.length <= 2 * 105", + "1 <= pizzas[i] <= 105", + "n is a multiple of 4." + ], + "python_template": "class Solution(object):\n def maxWeight(self, pizzas):\n \"\"\"\n :type pizzas: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxWeight(int[] pizzas) {\n \n }\n}", + "metadata": { + "func_name": "maxWeight" + } +} \ No newline at end of file diff --git a/equal-row-and-column-pairs.json b/equal-row-and-column-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..0cae3861273d5223c509d6b98458b29dd9c88f37 --- /dev/null +++ b/equal-row-and-column-pairs.json @@ -0,0 +1,30 @@ +{ + "id": 2428, + "name": "equal-row-and-column-pairs", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/equal-row-and-column-pairs/", + "date": "2022-07-17", + "task_description": "Given a **0-indexed** `n x n` integer matrix `grid`, _return the number of pairs _`(ri, cj)`_ such that row _`ri`_ and column _`cj`_ are equal_. A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array). **Example 1:** ``` **Input:** grid = [[3,2,1],[1,7,6],[2,7,7]] **Output:** 1 **Explanation:** There is 1 equal row and column pair: - (Row 2, Column 1): [2,7,7] ``` **Example 2:** ``` **Input:** grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]] **Output:** 3 **Explanation:** There are 3 equal row and column pairs: - (Row 0, Column 0): [3,1,2,2] - (Row 2, Column 2): [2,4,2,2] - (Row 3, Column 2): [2,4,2,2] ``` **Constraints:** `n == grid.length == grid[i].length` `1 <= n <= 200` `1 <= grid[i][j] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[3,2,1],[1,7,6],[2,7,7]]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]", + "output": "3 " + } + ], + "constraints": [ + "n == grid.length == grid[i].length", + "1 <= n <= 200", + "1 <= grid[i][j] <= 105" + ], + "python_template": "class Solution(object):\n def equalPairs(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int equalPairs(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "equalPairs" + } +} \ No newline at end of file diff --git a/escape-the-spreading-fire.json b/escape-the-spreading-fire.json new file mode 100644 index 0000000000000000000000000000000000000000..4bff19dd1d00626f695ca9ae8634099a183453b2 --- /dev/null +++ b/escape-the-spreading-fire.json @@ -0,0 +1,41 @@ +{ + "id": 2344, + "name": "escape-the-spreading-fire", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/escape-the-spreading-fire/", + "date": "2022-04-16", + "task_description": "You are given a **0-indexed** 2D integer array `grid` of size `m x n` which represents a field. Each cell has one of three values: `0` represents grass, `1` represents fire, `2` represents a wall that you and fire cannot pass through. You are situated in the top-left cell, `(0, 0)`, and you want to travel to the safehouse at the bottom-right cell, `(m - 1, n - 1)`. Every minute, you may move to an **adjacent** grass cell. **After** your move, every fire cell will spread to all **adjacent** cells that are not walls. Return _the **maximum** number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse_. If this is impossible, return `-1`. If you can **always** reach the safehouse regardless of the minutes stayed, return `109`. Note that even if the fire spreads to the safehouse immediately after you have reached it, it will be counted as safely reaching the safehouse. A cell is **adjacent** to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching). **Example 1:** ``` **Input:** grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]] **Output:** 3 **Explanation:** The figure above shows the scenario where you stay in the initial position for 3 minutes. You will still be able to safely reach the safehouse. Staying for more than 3 minutes will not allow you to safely reach the safehouse. ``` **Example 2:** ``` **Input:** grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]] **Output:** -1 **Explanation:** The figure above shows the scenario where you immediately move towards the safehouse. Fire will spread to any cell you move towards and it is impossible to safely reach the safehouse. Thus, -1 is returned. ``` **Example 3:** ``` **Input:** grid = [[0,0,0],[2,2,0],[1,2,0]] **Output:** 1000000000 **Explanation:** The figure above shows the initial grid. Notice that the fire is contained by walls and you will always be able to safely reach the safehouse. Thus, 109 is returned. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `2 <= m, n <= 300` `4 <= m * n <= 2 * 104` `grid[i][j]` is either `0`, `1`, or `2`. `grid[0][0] == grid[m - 1][n - 1] == 0`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]]", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "grid = [[0,0,0],[2,2,0],[1,2,0]]", + "output": "1000000000 " + } + ], + "constraints": [ + "0 represents grass,", + "1 represents fire,", + "2 represents a wall that you and fire cannot pass through.", + "m == grid.length", + "n == grid[i].length", + "2 <= m, n <= 300", + "4 <= m * n <= 2 * 104", + "grid[i][j] is either 0, 1, or 2.", + "grid[0][0] == grid[m - 1][n - 1] == 0" + ], + "python_template": "class Solution(object):\n def maximumMinutes(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumMinutes(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "maximumMinutes" + } +} \ No newline at end of file diff --git a/execution-of-all-suffix-instructions-staying-in-a-grid.json b/execution-of-all-suffix-instructions-staying-in-a-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..c3d42c65330ac738769a46b915ba5b05ca60790c --- /dev/null +++ b/execution-of-all-suffix-instructions-staying-in-a-grid.json @@ -0,0 +1,39 @@ +{ + "id": 2239, + "name": "execution-of-all-suffix-instructions-staying-in-a-grid", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/", + "date": "2021-12-19", + "task_description": "There is an `n x n` grid, with the top-left cell at `(0, 0)` and the bottom-right cell at `(n - 1, n - 1)`. You are given the integer `n` and an integer array `startPos` where `startPos = [startrow, startcol]` indicates that a robot is initially at cell `(startrow, startcol)`. You are also given a **0-indexed** string `s` of length `m` where `s[i]` is the `ith` instruction for the robot: `'L'` (move left), `'R'` (move right), `'U'` (move up), and `'D'` (move down). The robot can begin executing from any `ith` instruction in `s`. It executes the instructions one by one towards the end of `s` but it stops if either of these conditions is met: The next instruction will move the robot off the grid. There are no more instructions left to execute. Return _an array_ `answer` _of length_ `m` _where_ `answer[i]` _is **the number of instructions** the robot can execute if the robot **begins executing from** the_ `ith` _instruction in_ `s`. **Example 1:** ``` **Input:** n = 3, startPos = [0,1], s = \"RRDDLU\" **Output:** [1,5,4,3,1,0] **Explanation:** Starting from startPos and beginning execution from the ith instruction: - 0th: \"**R**RDDLU\". Only one instruction \"R\" can be executed before it moves off the grid. - 1st: \"**RDDLU**\". All five instructions can be executed while it stays in the grid and ends at (1, 1). - 2nd: \"**DDLU**\". All four instructions can be executed while it stays in the grid and ends at (1, 0). - 3rd: \"**DLU**\". All three instructions can be executed while it stays in the grid and ends at (0, 0). - 4th: \"**L**U\". Only one instruction \"L\" can be executed before it moves off the grid. - 5th: \"U\". If moving up, it would move off the grid. ``` **Example 2:** ``` **Input:** n = 2, startPos = [1,1], s = \"LURD\" **Output:** [4,1,0,0] **Explanation:** - 0th: \"**LURD**\". - 1st: \"**U**RD\". - 2nd: \"RD\". - 3rd: \"D\". ``` **Example 3:** ``` **Input:** n = 1, startPos = [0,0], s = \"LRUD\" **Output:** [0,0,0,0] **Explanation:** No matter which instruction the robot begins execution from, it would move off the grid. ``` **Constraints:** `m == s.length` `1 <= n, m <= 500` `startPos.length == 2` `0 <= startrow, startcol < n` `s` consists of `'L'`, `'R'`, `'U'`, and `'D'`.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, startPos = [0,1], s = \"RRDDLU\"", + "output": "[1,5,4,3,1,0] " + }, + { + "label": "Example 2", + "input": "n = 2, startPos = [1,1], s = \"LURD\"", + "output": "[4,1,0,0] " + }, + { + "label": "Example 3", + "input": "n = 1, startPos = [0,0], s = \"LRUD\"", + "output": "[0,0,0,0] " + } + ], + "constraints": [ + "The next instruction will move the robot off the grid.", + "There are no more instructions left to execute.", + "m == s.length", + "1 <= n, m <= 500", + "startPos.length == 2", + "0 <= startrow, startcol < n", + "s consists of 'L', 'R', 'U', and 'D'." + ], + "python_template": "class Solution(object):\n def executeInstructions(self, n, startPos, s):\n \"\"\"\n :type n: int\n :type startPos: List[int]\n :type s: str\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] executeInstructions(int n, int[] startPos, String s) {\n \n }\n}", + "metadata": { + "func_name": "executeInstructions" + } +} \ No newline at end of file diff --git a/existence-of-a-substring-in-a-string-and-its-reverse.json b/existence-of-a-substring-in-a-string-and-its-reverse.json new file mode 100644 index 0000000000000000000000000000000000000000..0099151b149a05bfc56de3062f2c871222a8744f --- /dev/null +++ b/existence-of-a-substring-in-a-string-and-its-reverse.json @@ -0,0 +1,34 @@ +{ + "id": 3353, + "name": "existence-of-a-substring-in-a-string-and-its-reverse", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse/", + "date": "2024-03-10", + "task_description": "Given a** **string `s`, find any substring of length `2` which is also present in the reverse of `s`. Return `true`_ if such a substring exists, and _`false`_ otherwise._ **Example 1:** **Input: **s = \"leetcode\" **Output: **true **Explanation:** Substring `\"ee\"` is of length `2` which is also present in `reverse(s) == \"edocteel\"`. **Example 2:** **Input: **s = \"abcba\" **Output: **true **Explanation:** All of the substrings of length `2` `\"ab\"`, `\"bc\"`, `\"cb\"`, `\"ba\"` are also present in `reverse(s) == \"abcba\"`. **Example 3:** **Input: **s = \"abcd\" **Output: **false **Explanation:** There is no substring of length `2` in `s`, which is also present in the reverse of `s`. **Constraints:** `1 <= s.length <= 100` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"leetcode\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"abcba\"", + "output": "true " + }, + { + "label": "Example 3", + "input": "s = \"abcd\"", + "output": "false " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def isSubstringPresent(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isSubstringPresent(String s) {\n \n }\n}", + "metadata": { + "func_name": "isSubstringPresent" + } +} \ No newline at end of file diff --git a/faulty-keyboard.json b/faulty-keyboard.json new file mode 100644 index 0000000000000000000000000000000000000000..ab9c50be838185eace7e4fa9a9799617a55963c8 --- /dev/null +++ b/faulty-keyboard.json @@ -0,0 +1,30 @@ +{ + "id": 2886, + "name": "faulty-keyboard", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/faulty-keyboard/", + "date": "2023-07-30", + "task_description": "Your laptop keyboard is faulty, and whenever you type a character `'i'` on it, it reverses the string that you have written. Typing other characters works as expected. You are given a **0-indexed** string `s`, and you type each character of `s` using your faulty keyboard. Return _the final string that will be present on your laptop screen._ **Example 1:** ``` **Input:** s = \"string\" **Output:** \"rtsng\" **Explanation:** After typing first character, the text on the screen is \"s\". After the second character, the text is \"st\". After the third character, the text is \"str\". Since the fourth character is an 'i', the text gets reversed and becomes \"rts\". After the fifth character, the text is \"rtsn\". After the sixth character, the text is \"rtsng\". Therefore, we return \"rtsng\". ``` **Example 2:** ``` **Input:** s = \"poiinter\" **Output:** \"ponter\" **Explanation:** After the first character, the text on the screen is \"p\". After the second character, the text is \"po\". Since the third character you type is an 'i', the text gets reversed and becomes \"op\". Since the fourth character you type is an 'i', the text gets reversed and becomes \"po\". After the fifth character, the text is \"pon\". After the sixth character, the text is \"pont\". After the seventh character, the text is \"ponte\". After the eighth character, the text is \"ponter\". Therefore, we return \"ponter\". ``` **Constraints:** `1 <= s.length <= 100` `s` consists of lowercase English letters. `s[0] != 'i'`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"string\"", + "output": "\"rtsng\" " + }, + { + "label": "Example 2", + "input": "s = \"poiinter\"", + "output": "\"ponter\" " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "s consists of lowercase English letters.", + "s[0] != 'i'" + ], + "python_template": "class Solution(object):\n def finalString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String finalString(String s) {\n \n }\n}", + "metadata": { + "func_name": "finalString" + } +} \ No newline at end of file diff --git a/find-a-safe-walk-through-a-grid.json b/find-a-safe-walk-through-a-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..749efe5f0a69770e9d3cd67abfd775fd443218e9 --- /dev/null +++ b/find-a-safe-walk-through-a-grid.json @@ -0,0 +1,38 @@ +{ + "id": 3558, + "name": "find-a-safe-walk-through-a-grid", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-a-safe-walk-through-a-grid/", + "date": "2024-08-31", + "task_description": "You are given an `m x n` binary matrix `grid` and an integer `health`. You start on the upper-left corner `(0, 0)` and would like to get to the lower-right corner `(m - 1, n - 1)`. You can move up, down, left, or right from one cell to another adjacent cell as long as your health _remains_ **positive**. Cells `(i, j)` with `grid[i][j] = 1` are considered **unsafe** and reduce your health by 1. Return `true` if you can reach the final cell with a health value of 1 or more, and `false` otherwise. **Example 1:** **Input:** grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1 **Output:** true **Explanation:** The final cell can be reached safely by walking along the gray cells below. **Example 2:** **Input:** grid = [[0,1,1,0,0,0],[1,0,1,0,0,0],[0,1,1,1,0,1],[0,0,1,0,1,0]], health = 3 **Output:** false **Explanation:** A minimum of 4 health points is needed to reach the final cell safely. **Example 3:** **Input:** grid = [[1,1,1],[1,0,1],[1,1,1]], health = 5 **Output:** true **Explanation:** The final cell can be reached safely by walking along the gray cells below. Any path that does not go through the cell `(1, 1)` is unsafe since your health will drop to 0 when reaching the final cell. **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 50` `2 <= m * n` `1 <= health <= m + n` `grid[i][j]` is either 0 or 1.", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1", + "output": "true " + }, + { + "label": "Example 2", + "input": "grid = [[0,1,1,0,0,0],[1,0,1,0,0,0],[0,1,1,1,0,1],[0,0,1,0,1,0]], health = 3", + "output": "false " + }, + { + "label": "Example 3", + "input": "grid = [[1,1,1],[1,0,1],[1,1,1]], health = 5", + "output": "true " + } + ], + "constraints": [ + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 50", + "2 <= m * n", + "1 <= health <= m + n", + "grid[i][j] is either 0 or 1." + ], + "python_template": "class Solution(object):\n def findSafeWalk(self, grid, health):\n \"\"\"\n :type grid: List[List[int]]\n :type health: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean findSafeWalk(List> grid, int health) {\n \n }\n}", + "metadata": { + "func_name": "findSafeWalk" + } +} \ No newline at end of file diff --git a/find-all-good-indices.json b/find-all-good-indices.json new file mode 100644 index 0000000000000000000000000000000000000000..5b748e465ae8c935afce10de3bc6a65dee474262 --- /dev/null +++ b/find-all-good-indices.json @@ -0,0 +1,33 @@ +{ + "id": 2504, + "name": "find-all-good-indices", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-all-good-indices/", + "date": "2022-09-18", + "task_description": "You are given a **0-indexed** integer array `nums` of size `n` and a positive integer `k`. We call an index `i` in the range `k <= i < n - k` **good** if the following conditions are satisfied: The `k` elements that are just **before** the index `i` are in **non-increasing** order. The `k` elements that are just **after** the index `i` are in **non-decreasing** order. Return _an array of all good indices sorted in **increasing** order_. **Example 1:** ``` **Input:** nums = [2,1,1,1,3,4,1], k = 2 **Output:** [2,3] **Explanation:** There are two good indices in the array: - Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order. - Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order. Note that the index 4 is not good because [4,1] is not non-decreasing. ``` **Example 2:** ``` **Input:** nums = [2,1,1,2], k = 2 **Output:** [] **Explanation:** There are no good indices in this array. ``` **Constraints:** `n == nums.length` `3 <= n <= 105` `1 <= nums[i] <= 106` `1 <= k <= n / 2`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,1,1,3,4,1], k = 2", + "output": "[2,3] " + }, + { + "label": "Example 2", + "input": "nums = [2,1,1,2], k = 2", + "output": "[] " + } + ], + "constraints": [ + "The k elements that are just before the index i are in non-increasing order.", + "The k elements that are just after the index i are in non-decreasing order.", + "n == nums.length", + "3 <= n <= 105", + "1 <= nums[i] <= 106", + "1 <= k <= n / 2" + ], + "python_template": "class Solution(object):\n def goodIndices(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List goodIndices(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "goodIndices" + } +} \ No newline at end of file diff --git a/find-all-k-distant-indices-in-an-array.json b/find-all-k-distant-indices-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..87536c934035b499fedb3b87a4e0ebc5386479e1 --- /dev/null +++ b/find-all-k-distant-indices-in-an-array.json @@ -0,0 +1,31 @@ +{ + "id": 2320, + "name": "find-all-k-distant-indices-in-an-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/", + "date": "2022-03-06", + "task_description": "You are given a **0-indexed** integer array `nums` and two integers `key` and `k`. A **k-distant index** is an index `i` of `nums` for which there exists at least one index `j` such that `|i - j| <= k` and `nums[j] == key`. Return _a list of all k-distant indices sorted in **increasing order**_. **Example 1:** ``` **Input:** nums = [3,4,9,1,3,9,5], key = 9, k = 1 **Output:** [1,2,3,4,5,6] **Explanation:** Here, `nums[2] == key` and `nums[5] == key. - For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j` where `|0 - j| <= k` and `nums[j] == key. Thus, 0 is not a k-distant index. - For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index. - For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index. - For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index. - For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index. - For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index. - For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index. `Thus, we return [1,2,3,4,5,6] which is sorted in increasing order. ``` **Example 2:** ``` **Input:** nums = [2,2,2,2,2], key = 2, k = 2 **Output:** [0,1,2,3,4] **Explanation:** For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index. Hence, we return [0,1,2,3,4]. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 1000` `key` is an integer from the array `nums`. `1 <= k <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,4,9,1,3,9,5], key = 9, k = 1", + "output": "[1,2,3,4,5,6] " + }, + { + "label": "Example 2", + "input": "nums = [2,2,2,2,2], key = 2, k = 2", + "output": "[0,1,2,3,4] " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 1000", + "key is an integer from the array nums.", + "1 <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def findKDistantIndices(self, nums, key, k):\n \"\"\"\n :type nums: List[int]\n :type key: int\n :type k: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List findKDistantIndices(int[] nums, int key, int k) {\n \n }\n}", + "metadata": { + "func_name": "findKDistantIndices" + } +} \ No newline at end of file diff --git a/find-all-possible-stable-binary-arrays-i.json b/find-all-possible-stable-binary-arrays-i.json new file mode 100644 index 0000000000000000000000000000000000000000..d539cd47b986009f2def5092f232e2fcc9a9498e --- /dev/null +++ b/find-all-possible-stable-binary-arrays-i.json @@ -0,0 +1,36 @@ +{ + "id": 3406, + "name": "find-all-possible-stable-binary-arrays-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-all-possible-stable-binary-arrays-i/", + "date": "2024-04-13", + "task_description": "You are given 3 positive integers `zero`, `one`, and `limit`. A binary array `arr` is called **stable** if: The number of occurrences of 0 in `arr` is **exactly **`zero`. The number of occurrences of 1 in `arr` is **exactly** `one`. Each subarray of `arr` with a size greater than `limit` must contain **both **0 and 1. Return the _total_ number of **stable** binary arrays. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** zero = 1, one = 1, limit = 2 **Output:** 2 **Explanation:** The two possible stable binary arrays are `[1,0]` and `[0,1]`, as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2. **Example 2:** **Input:** zero = 1, one = 2, limit = 1 **Output:** 1 **Explanation:** The only possible stable binary array is `[1,0,1]`. Note that the binary arrays `[1,1,0]` and `[0,1,1]` have subarrays of length 2 with identical elements, hence, they are not stable. **Example 3:** **Input:** zero = 3, one = 3, limit = 2 **Output:** 14 **Explanation:** All the possible stable binary arrays are `[0,0,1,0,1,1]`, `[0,0,1,1,0,1]`, `[0,1,0,0,1,1]`, `[0,1,0,1,0,1]`, `[0,1,0,1,1,0]`, `[0,1,1,0,0,1]`, `[0,1,1,0,1,0]`, `[1,0,0,1,0,1]`, `[1,0,0,1,1,0]`, `[1,0,1,0,0,1]`, `[1,0,1,0,1,0]`, `[1,0,1,1,0,0]`, `[1,1,0,0,1,0]`, and `[1,1,0,1,0,0]`. **Constraints:** `1 <= zero, one, limit <= 200`", + "test_case": [ + { + "label": "Example 1", + "input": "zero = 1, one = 1, limit = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "zero = 1, one = 2, limit = 1", + "output": "1 " + }, + { + "label": "Example 3", + "input": "zero = 3, one = 3, limit = 2", + "output": "14 " + } + ], + "constraints": [ + "The number of occurrences of 0 in arr is exactly zero.", + "The number of occurrences of 1 in arr is exactly one.", + "Each subarray of arr with a size greater than limit must contain both 0 and 1.", + "1 <= zero, one, limit <= 200" + ], + "python_template": "class Solution(object):\n def numberOfStableArrays(self, zero, one, limit):\n \"\"\"\n :type zero: int\n :type one: int\n :type limit: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfStableArrays(int zero, int one, int limit) {\n \n }\n}", + "metadata": { + "func_name": "numberOfStableArrays" + } +} \ No newline at end of file diff --git a/find-all-possible-stable-binary-arrays-ii.json b/find-all-possible-stable-binary-arrays-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..b845953b317eb16d22ca3a449d148d1562f81aa8 --- /dev/null +++ b/find-all-possible-stable-binary-arrays-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3407, + "name": "find-all-possible-stable-binary-arrays-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-all-possible-stable-binary-arrays-ii/", + "date": "2024-04-13", + "task_description": "You are given 3 positive integers `zero`, `one`, and `limit`. A binary array `arr` is called **stable** if: The number of occurrences of 0 in `arr` is **exactly **`zero`. The number of occurrences of 1 in `arr` is **exactly** `one`. Each subarray of `arr` with a size greater than `limit` must contain **both **0 and 1. Return the _total_ number of **stable** binary arrays. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** zero = 1, one = 1, limit = 2 **Output:** 2 **Explanation:** The two possible stable binary arrays are `[1,0]` and `[0,1]`. **Example 2:** **Input:** zero = 1, one = 2, limit = 1 **Output:** 1 **Explanation:** The only possible stable binary array is `[1,0,1]`. **Example 3:** **Input:** zero = 3, one = 3, limit = 2 **Output:** 14 **Explanation:** All the possible stable binary arrays are `[0,0,1,0,1,1]`, `[0,0,1,1,0,1]`, `[0,1,0,0,1,1]`, `[0,1,0,1,0,1]`, `[0,1,0,1,1,0]`, `[0,1,1,0,0,1]`, `[0,1,1,0,1,0]`, `[1,0,0,1,0,1]`, `[1,0,0,1,1,0]`, `[1,0,1,0,0,1]`, `[1,0,1,0,1,0]`, `[1,0,1,1,0,0]`, `[1,1,0,0,1,0]`, and `[1,1,0,1,0,0]`. **Constraints:** `1 <= zero, one, limit <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "zero = 1, one = 1, limit = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "zero = 1, one = 2, limit = 1", + "output": "1 " + }, + { + "label": "Example 3", + "input": "zero = 3, one = 3, limit = 2", + "output": "14 " + } + ], + "constraints": [ + "The number of occurrences of 0 in arr is exactly zero.", + "The number of occurrences of 1 in arr is exactly one.", + "Each subarray of arr with a size greater than limit must contain both 0 and 1.", + "1 <= zero, one, limit <= 1000" + ], + "python_template": "class Solution(object):\n def numberOfStableArrays(self, zero, one, limit):\n \"\"\"\n :type zero: int\n :type one: int\n :type limit: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfStableArrays(int zero, int one, int limit) {\n \n }\n}", + "metadata": { + "func_name": "numberOfStableArrays" + } +} \ No newline at end of file diff --git a/find-beautiful-indices-in-the-given-array-i.json b/find-beautiful-indices-in-the-given-array-i.json new file mode 100644 index 0000000000000000000000000000000000000000..f9d46277cc2fe9a4bcf93e54a666b3cad196bae3 --- /dev/null +++ b/find-beautiful-indices-in-the-given-array-i.json @@ -0,0 +1,39 @@ +{ + "id": 3245, + "name": "find-beautiful-indices-in-the-given-array-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i/", + "date": "2024-01-07", + "task_description": "You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`. An index `i` is **beautiful** if: `0 <= i <= s.length - a.length` `s[i..(i + a.length - 1)] == a` There exists an index `j` such that: `0 <= j <= s.length - b.length` `s[j..(j + b.length - 1)] == b` `|j - i| <= k` Return _the array that contains beautiful indices in **sorted order from smallest to largest**_. **Example 1:** ``` **Input:** s = \"isawsquirrelnearmysquirrelhouseohmy\", a = \"my\", b = \"squirrel\", k = 15 **Output:** [16,33] **Explanation:** There are 2 beautiful indices: [16,33]. - The index 16 is beautiful as s[16..17] == \"my\" and there exists an index 4 with s[4..11] == \"squirrel\" and |16 - 4| <= 15. - The index 33 is beautiful as s[33..34] == \"my\" and there exists an index 18 with s[18..25] == \"squirrel\" and |33 - 18| <= 15. Thus we return [16,33] as the result. ``` **Example 2:** ``` **Input:** s = \"abcd\", a = \"a\", b = \"a\", k = 4 **Output:** [0] **Explanation:** There is 1 beautiful index: [0]. - The index 0 is beautiful as s[0..0] == \"a\" and there exists an index 0 with s[0..0] == \"a\" and |0 - 0| <= 4. Thus we return [0] as the result. ``` **Constraints:** `1 <= k <= s.length <= 105` `1 <= a.length, b.length <= 10` `s`, `a`, and `b` contain only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"isawsquirrelnearmysquirrelhouseohmy\", a = \"my\", b = \"squirrel\", k = 15", + "output": "[16,33] " + }, + { + "label": "Example 2", + "input": "s = \"abcd\", a = \"a\", b = \"a\", k = 4", + "output": "[0] " + } + ], + "constraints": [ + "0 <= i <= s.length - a.length", + "s[i..(i + a.length - 1)] == a", + "There exists an index j such that:\n\t\n0 <= j <= s.length - b.length\ns[j..(j + b.length - 1)] == b\n|j - i| <= k", + "0 <= j <= s.length - b.length", + "s[j..(j + b.length - 1)] == b", + "|j - i| <= k", + "0 <= j <= s.length - b.length", + "s[j..(j + b.length - 1)] == b", + "|j - i| <= k", + "1 <= k <= s.length <= 105", + "1 <= a.length, b.length <= 10", + "s, a, and b contain only lowercase English letters." + ], + "python_template": "class Solution(object):\n def beautifulIndices(self, s, a, b, k):\n \"\"\"\n :type s: str\n :type a: str\n :type b: str\n :type k: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List beautifulIndices(String s, String a, String b, int k) {\n \n }\n}", + "metadata": { + "func_name": "beautifulIndices" + } +} \ No newline at end of file diff --git a/find-beautiful-indices-in-the-given-array-ii.json b/find-beautiful-indices-in-the-given-array-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..54f7b536ec33a532e9565cd3b52ee04c1f993e2c --- /dev/null +++ b/find-beautiful-indices-in-the-given-array-ii.json @@ -0,0 +1,39 @@ +{ + "id": 3303, + "name": "find-beautiful-indices-in-the-given-array-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-ii/", + "date": "2024-01-07", + "task_description": "You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`. An index `i` is **beautiful** if: `0 <= i <= s.length - a.length` `s[i..(i + a.length - 1)] == a` There exists an index `j` such that: `0 <= j <= s.length - b.length` `s[j..(j + b.length - 1)] == b` `|j - i| <= k` Return _the array that contains beautiful indices in **sorted order from smallest to largest**_. **Example 1:** ``` **Input:** s = \"isawsquirrelnearmysquirrelhouseohmy\", a = \"my\", b = \"squirrel\", k = 15 **Output:** [16,33] **Explanation:** There are 2 beautiful indices: [16,33]. - The index 16 is beautiful as s[16..17] == \"my\" and there exists an index 4 with s[4..11] == \"squirrel\" and |16 - 4| <= 15. - The index 33 is beautiful as s[33..34] == \"my\" and there exists an index 18 with s[18..25] == \"squirrel\" and |33 - 18| <= 15. Thus we return [16,33] as the result. ``` **Example 2:** ``` **Input:** s = \"abcd\", a = \"a\", b = \"a\", k = 4 **Output:** [0] **Explanation:** There is 1 beautiful index: [0]. - The index 0 is beautiful as s[0..0] == \"a\" and there exists an index 0 with s[0..0] == \"a\" and |0 - 0| <= 4. Thus we return [0] as the result. ``` **Constraints:** `1 <= k <= s.length <= 5 * 105` `1 <= a.length, b.length <= 5 * 105` `s`, `a`, and `b` contain only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"isawsquirrelnearmysquirrelhouseohmy\", a = \"my\", b = \"squirrel\", k = 15", + "output": "[16,33] " + }, + { + "label": "Example 2", + "input": "s = \"abcd\", a = \"a\", b = \"a\", k = 4", + "output": "[0] " + } + ], + "constraints": [ + "0 <= i <= s.length - a.length", + "s[i..(i + a.length - 1)] == a", + "There exists an index j such that:\n\t\n0 <= j <= s.length - b.length\ns[j..(j + b.length - 1)] == b\n|j - i| <= k", + "0 <= j <= s.length - b.length", + "s[j..(j + b.length - 1)] == b", + "|j - i| <= k", + "0 <= j <= s.length - b.length", + "s[j..(j + b.length - 1)] == b", + "|j - i| <= k", + "1 <= k <= s.length <= 5 * 105", + "1 <= a.length, b.length <= 5 * 105", + "s, a, and b contain only lowercase English letters." + ], + "python_template": "class Solution(object):\n def beautifulIndices(self, s, a, b, k):\n \"\"\"\n :type s: str\n :type a: str\n :type b: str\n :type k: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List beautifulIndices(String s, String a, String b, int k) {\n \n }\n}", + "metadata": { + "func_name": "beautifulIndices" + } +} \ No newline at end of file diff --git a/find-building-where-alice-and-bob-can-meet.json b/find-building-where-alice-and-bob-can-meet.json new file mode 100644 index 0000000000000000000000000000000000000000..a91c9efa1bb5ed4556ba035c522a3b0e32162c94 --- /dev/null +++ b/find-building-where-alice-and-bob-can-meet.json @@ -0,0 +1,32 @@ +{ + "id": 3181, + "name": "find-building-where-alice-and-bob-can-meet", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-building-where-alice-and-bob-can-meet/", + "date": "2023-11-12", + "task_description": "You are given a **0-indexed** array `heights` of positive integers, where `heights[i]` represents the height of the `ith` building. If a person is in building `i`, they can move to any other building `j` if and only if `i < j` and `heights[i] < heights[j]`. You are also given another array `queries` where `queries[i] = [ai, bi]`. On the `ith` query, Alice is in building `ai` while Bob is in building `bi`. Return _an array_ `ans` _where_ `ans[i]` _is **the index of the leftmost building** where Alice and Bob can meet on the_ `ith` _query_. _If Alice and Bob cannot move to a common building on query_ `i`, _set_ `ans[i]` _to_ `-1`. **Example 1:** ``` **Input:** heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] **Output:** [2,5,-1,5,2] **Explanation:** In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2]. In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. In the third query, Alice cannot meet Bob since Alice cannot move to any other building. In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5]. In the fifth query, Alice and Bob are already in the same building. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. ``` **Example 2:** ``` **Input:** heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] **Output:** [7,6,-1,4,6] **Explanation:** In the first query, Alice can directly move to Bob's building since heights[0] < heights[7]. In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. In the third query, Alice cannot meet Bob since Bob cannot move to any other building. In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4]. In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. ``` **Constraints:** `1 <= heights.length <= 5 * 104` `1 <= heights[i] <= 109` `1 <= queries.length <= 5 * 104` `queries[i] = [ai, bi]` `0 <= ai, bi <= heights.length - 1`", + "test_case": [ + { + "label": "Example 1", + "input": "heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]", + "output": "[2,5,-1,5,2] " + }, + { + "label": "Example 2", + "input": "heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]", + "output": "[7,6,-1,4,6] " + } + ], + "constraints": [ + "1 <= heights.length <= 5 * 104", + "1 <= heights[i] <= 109", + "1 <= queries.length <= 5 * 104", + "queries[i] = [ai, bi]", + "0 <= ai, bi <= heights.length - 1" + ], + "python_template": "class Solution(object):\n def leftmostBuildingQueries(self, heights, queries):\n \"\"\"\n :type heights: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] leftmostBuildingQueries(int[] heights, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "leftmostBuildingQueries" + } +} \ No newline at end of file diff --git a/find-champion-i.json b/find-champion-i.json new file mode 100644 index 0000000000000000000000000000000000000000..77f06b393047f3c37c3f941c5de4f9818be4c591 --- /dev/null +++ b/find-champion-i.json @@ -0,0 +1,34 @@ +{ + "id": 3188, + "name": "find-champion-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-champion-i/", + "date": "2023-10-29", + "task_description": "There are `n` teams numbered from `0` to `n - 1` in a tournament. Given a **0-indexed** 2D boolean matrix `grid` of size `n * n`. For all `i, j` that `0 <= i, j <= n - 1` and `i != j` team `i` is **stronger** than team `j` if `grid[i][j] == 1`, otherwise, team `j` is **stronger** than team `i`. Team `a` will be the **champion** of the tournament if there is no team `b` that is stronger than team `a`. Return _the team that will be the champion of the tournament._ **Example 1:** ``` **Input:** grid = [[0,1],[0,0]] **Output:** 0 **Explanation:** There are two teams in this tournament. grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion. ``` **Example 2:** ``` **Input:** grid = [[0,0,1],[1,0,1],[0,0,0]] **Output:** 1 **Explanation:** There are three teams in this tournament. grid[1][0] == 1 means that team 1 is stronger than team 0. grid[1][2] == 1 means that team 1 is stronger than team 2. So team 1 will be the champion. ``` **Constraints:** `n == grid.length` `n == grid[i].length` `2 <= n <= 100` `grid[i][j]` is either `0` or `1`. For all `i grid[i][i]` is `0.` For all `i, j` that `i != j`, `grid[i][j] != grid[j][i]`. The input is generated such that if team `a` is stronger than team `b` and team `b` is stronger than team `c`, then team `a` is stronger than team `c`.", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[0,1],[0,0]]", + "output": "0 " + }, + { + "label": "Example 2", + "input": "grid = [[0,0,1],[1,0,1],[0,0,0]]", + "output": "1 " + } + ], + "constraints": [ + "n == grid.length", + "n == grid[i].length", + "2 <= n <= 100", + "grid[i][j] is either 0 or 1.", + "For all i grid[i][i] is 0.", + "For all i, j that i != j, grid[i][j] != grid[j][i].", + "The input is generated such that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c." + ], + "python_template": "class Solution(object):\n def findChampion(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findChampion(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "findChampion" + } +} \ No newline at end of file diff --git a/find-closest-node-to-given-two-nodes.json b/find-closest-node-to-given-two-nodes.json new file mode 100644 index 0000000000000000000000000000000000000000..356c6527c7afc54f10ed81d09f6b655ca24b2566 --- /dev/null +++ b/find-closest-node-to-given-two-nodes.json @@ -0,0 +1,32 @@ +{ + "id": 2438, + "name": "find-closest-node-to-given-two-nodes", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-closest-node-to-given-two-nodes/", + "date": "2022-07-24", + "task_description": "You are given a **directed** graph of `n` nodes numbered from `0` to `n - 1`, where each node has **at most one** outgoing edge. The graph is represented with a given **0-indexed** array `edges` of size `n`, indicating that there is a directed edge from node `i` to node `edges[i]`. If there is no outgoing edge from `i`, then `edges[i] == -1`. You are also given two integers `node1` and `node2`. Return _the **index** of the node that can be reached from both _`node1`_ and _`node2`_, such that the **maximum** between the distance from _`node1`_ to that node, and from _`node2`_ to that node is **minimized**_. If there are multiple answers, return the node with the **smallest** index, and if no possible answer exists, return `-1`. Note that `edges` may contain cycles. **Example 1:** ``` **Input:** edges = [2,2,3,-1], node1 = 0, node2 = 1 **Output:** 2 **Explanation:** The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1. The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2. ``` **Example 2:** ``` **Input:** edges = [1,2,-1], node1 = 0, node2 = 2 **Output:** 2 **Explanation:** The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0. The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2. ``` **Constraints:** `n == edges.length` `2 <= n <= 105` `-1 <= edges[i] < n` `edges[i] != i` `0 <= node1, node2 < n`", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [2,2,3,-1], node1 = 0, node2 = 1", + "output": "2 " + }, + { + "label": "Example 2", + "input": "edges = [1,2,-1], node1 = 0, node2 = 2", + "output": "2 " + } + ], + "constraints": [ + "n == edges.length", + "2 <= n <= 105", + "-1 <= edges[i] < n", + "edges[i] != i", + "0 <= node1, node2 < n" + ], + "python_template": "class Solution(object):\n def closestMeetingNode(self, edges, node1, node2):\n \"\"\"\n :type edges: List[int]\n :type node1: int\n :type node2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int closestMeetingNode(int[] edges, int node1, int node2) {\n \n }\n}", + "metadata": { + "func_name": "closestMeetingNode" + } +} \ No newline at end of file diff --git a/find-closest-number-to-zero.json b/find-closest-number-to-zero.json new file mode 100644 index 0000000000000000000000000000000000000000..ae7773e585373aa74d4edbefbddafd91692c5f94 --- /dev/null +++ b/find-closest-number-to-zero.json @@ -0,0 +1,29 @@ +{ + "id": 2350, + "name": "find-closest-number-to-zero", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-closest-number-to-zero/", + "date": "2022-04-02", + "task_description": "Given an integer array `nums` of size `n`, return _the number with the value **closest** to _`0`_ in _`nums`. If there are multiple answers, return _the number with the **largest** value_. **Example 1:** ``` **Input:** nums = [-4,-2,1,4,8] **Output:** 1 **Explanation:** The distance from -4 to 0 is |-4| = 4. The distance from -2 to 0 is |-2| = 2. The distance from 1 to 0 is |1| = 1. The distance from 4 to 0 is |4| = 4. The distance from 8 to 0 is |8| = 8. Thus, the closest number to 0 in the array is 1. ``` **Example 2:** ``` **Input:** nums = [2,-1,1] **Output:** 1 **Explanation:** 1 and -1 are both the closest numbers to 0, so 1 being larger is returned. ``` **Constraints:** `1 <= n <= 1000` `-105 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [-4,-2,1,4,8]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [2,-1,1]", + "output": "1 " + } + ], + "constraints": [ + "1 <= n <= 1000", + "-105 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def findClosestNumber(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findClosestNumber(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findClosestNumber" + } +} \ No newline at end of file diff --git a/find-common-elements-between-two-arrays.json b/find-common-elements-between-two-arrays.json new file mode 100644 index 0000000000000000000000000000000000000000..6964ef07285cfb52e1475cb7f2471845bd6859af --- /dev/null +++ b/find-common-elements-between-two-arrays.json @@ -0,0 +1,38 @@ +{ + "id": 3206, + "name": "find-common-elements-between-two-arrays", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-common-elements-between-two-arrays/", + "date": "2023-11-25", + "task_description": "You are given two integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively. Calculate the following values: `answer1` : the number of indices `i` such that `nums1[i]` exists in `nums2`. `answer2` : the number of indices `i` such that `nums2[i]` exists in `nums1`. Return `[answer1,answer2]`. **Example 1:** **Input:** nums1 = [2,3,2], nums2 = [1,2] **Output:** [2,1] **Explanation:** **Example 2:** **Input:** nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6] **Output:** [3,4] **Explanation:** The elements at indices 1, 2, and 3 in `nums1` exist in `nums2` as well. So `answer1` is 3. The elements at indices 0, 1, 3, and 4 in `nums2` exist in `nums1`. So `answer2` is 4. **Example 3:** **Input:** nums1 = [3,4,2,3], nums2 = [1,5] **Output:** [0,0] **Explanation:** No numbers are common between `nums1` and `nums2`, so answer is [0,0]. **Constraints:** `n == nums1.length` `m == nums2.length` `1 <= n, m <= 100` `1 <= nums1[i], nums2[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [2,3,2], nums2 = [1,2]", + "output": "[2,1] " + }, + { + "label": "Example 2", + "input": "nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]", + "output": "[3,4] " + }, + { + "label": "Example 3", + "input": "nums1 = [3,4,2,3], nums2 = [1,5]", + "output": "[0,0] " + } + ], + "constraints": [ + "answer1 : the number of indices i such that nums1[i] exists in nums2.", + "answer2 : the number of indices i such that nums2[i] exists in nums1.", + "n == nums1.length", + "m == nums2.length", + "1 <= n, m <= 100", + "1 <= nums1[i], nums2[i] <= 100" + ], + "python_template": "class Solution(object):\n def findIntersectionValues(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findIntersectionValues(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "findIntersectionValues" + } +} \ No newline at end of file diff --git a/find-if-array-can-be-sorted.json b/find-if-array-can-be-sorted.json new file mode 100644 index 0000000000000000000000000000000000000000..05c7f6718179056f1bba5e3e364e70cbcd70434d --- /dev/null +++ b/find-if-array-can-be-sorted.json @@ -0,0 +1,34 @@ +{ + "id": 3291, + "name": "find-if-array-can-be-sorted", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-if-array-can-be-sorted/", + "date": "2024-01-06", + "task_description": "You are given a **0-indexed** array of **positive** integers `nums`. In one **operation**, you can swap any two **adjacent** elements if they have the **same** number of set bits. You are allowed to do this operation **any** number of times (**including zero**). Return `true` _if you can sort the array in ascending order, else return _`false`. **Example 1:** ``` **Input:** nums = [8,4,2,30,15] **Output:** true **Explanation:** Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation \"10\", \"100\", and \"1000\" respectively. The numbers 15 and 30 have four set bits each with binary representation \"1111\" and \"11110\". We can sort the array using 4 operations: - Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15]. - Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15]. - Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15]. - Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30]. The array has become sorted, hence we return true. Note that there may be other sequences of operations which also sort the array. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4,5] **Output:** true **Explanation:** The array is already sorted, hence we return true. ``` **Example 3:** ``` **Input:** nums = [3,16,8,4,2] **Output:** false **Explanation:** It can be shown that it is not possible to sort the input array using any number of operations. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 28`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [8,4,2,30,15]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4,5]", + "output": "true " + }, + { + "label": "Example 3", + "input": "nums = [3,16,8,4,2]", + "output": "false " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "1 <= nums[i] <= 28" + ], + "python_template": "class Solution(object):\n def canSortArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean canSortArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "canSortArray" + } +} \ No newline at end of file diff --git a/find-indices-with-index-and-value-difference-i.json b/find-indices-with-index-and-value-difference-i.json new file mode 100644 index 0000000000000000000000000000000000000000..cd0c57583dd398f992e49b941b85461c2070994c --- /dev/null +++ b/find-indices-with-index-and-value-difference-i.json @@ -0,0 +1,38 @@ +{ + "id": 3165, + "name": "find-indices-with-index-and-value-difference-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-indices-with-index-and-value-difference-i/", + "date": "2023-10-08", + "task_description": "You are given a **0-indexed** integer array `nums` having length `n`, an integer `indexDifference`, and an integer `valueDifference`. Your task is to find **two** indices `i` and `j`, both in the range `[0, n - 1]`, that satisfy the following conditions: `abs(i - j) >= indexDifference`, and `abs(nums[i] - nums[j]) >= valueDifference` Return _an integer array_ `answer`, _where_ `answer = [i, j]` _if there are two such indices_, _and_ `answer = [-1, -1]` _otherwise_. If there are multiple choices for the two indices, return _any of them_. **Note:** `i` and `j` may be **equal**. **Example 1:** ``` **Input:** nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 **Output:** [0,3] **Explanation:** In this example, i = 0 and j = 3 can be selected. abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4. Hence, a valid answer is [0,3]. [3,0] is also a valid answer. ``` **Example 2:** ``` **Input:** nums = [2,1], indexDifference = 0, valueDifference = 0 **Output:** [0,0] **Explanation:** In this example, i = 0 and j = 0 can be selected. abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0. Hence, a valid answer is [0,0]. Other valid answers are [0,1], [1,0], and [1,1]. ``` **Example 3:** ``` **Input:** nums = [1,2,3], indexDifference = 2, valueDifference = 4 **Output:** [-1,-1] **Explanation:** In this example, it can be shown that it is impossible to find two indices that satisfy both conditions. Hence, [-1,-1] is returned. ``` **Constraints:** `1 <= n == nums.length <= 100` `0 <= nums[i] <= 50` `0 <= indexDifference <= 100` `0 <= valueDifference <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,1,4,1], indexDifference = 2, valueDifference = 4", + "output": "[0,3] " + }, + { + "label": "Example 2", + "input": "nums = [2,1], indexDifference = 0, valueDifference = 0", + "output": "[0,0] " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3], indexDifference = 2, valueDifference = 4", + "output": "[-1,-1] " + } + ], + "constraints": [ + "abs(i - j) >= indexDifference, and", + "abs(nums[i] - nums[j]) >= valueDifference", + "1 <= n == nums.length <= 100", + "0 <= nums[i] <= 50", + "0 <= indexDifference <= 100", + "0 <= valueDifference <= 50" + ], + "python_template": "class Solution(object):\n def findIndices(self, nums, indexDifference, valueDifference):\n \"\"\"\n :type nums: List[int]\n :type indexDifference: int\n :type valueDifference: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {\n \n }\n}", + "metadata": { + "func_name": "findIndices" + } +} \ No newline at end of file diff --git a/find-indices-with-index-and-value-difference-ii.json b/find-indices-with-index-and-value-difference-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..f90edbc55db30ab8df9be6b8ed78034925a989e0 --- /dev/null +++ b/find-indices-with-index-and-value-difference-ii.json @@ -0,0 +1,38 @@ +{ + "id": 3170, + "name": "find-indices-with-index-and-value-difference-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-indices-with-index-and-value-difference-ii/", + "date": "2023-10-08", + "task_description": "You are given a **0-indexed** integer array `nums` having length `n`, an integer `indexDifference`, and an integer `valueDifference`. Your task is to find **two** indices `i` and `j`, both in the range `[0, n - 1]`, that satisfy the following conditions: `abs(i - j) >= indexDifference`, and `abs(nums[i] - nums[j]) >= valueDifference` Return _an integer array_ `answer`, _where_ `answer = [i, j]` _if there are two such indices_, _and_ `answer = [-1, -1]` _otherwise_. If there are multiple choices for the two indices, return _any of them_. **Note:** `i` and `j` may be **equal**. **Example 1:** ``` **Input:** nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 **Output:** [0,3] **Explanation:** In this example, i = 0 and j = 3 can be selected. abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4. Hence, a valid answer is [0,3]. [3,0] is also a valid answer. ``` **Example 2:** ``` **Input:** nums = [2,1], indexDifference = 0, valueDifference = 0 **Output:** [0,0] **Explanation:** In this example, i = 0 and j = 0 can be selected. abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0. Hence, a valid answer is [0,0]. Other valid answers are [0,1], [1,0], and [1,1]. ``` **Example 3:** ``` **Input:** nums = [1,2,3], indexDifference = 2, valueDifference = 4 **Output:** [-1,-1] **Explanation:** In this example, it can be shown that it is impossible to find two indices that satisfy both conditions. Hence, [-1,-1] is returned. ``` **Constraints:** `1 <= n == nums.length <= 105` `0 <= nums[i] <= 109` `0 <= indexDifference <= 105` `0 <= valueDifference <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,1,4,1], indexDifference = 2, valueDifference = 4", + "output": "[0,3] " + }, + { + "label": "Example 2", + "input": "nums = [2,1], indexDifference = 0, valueDifference = 0", + "output": "[0,0] " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3], indexDifference = 2, valueDifference = 4", + "output": "[-1,-1] " + } + ], + "constraints": [ + "abs(i - j) >= indexDifference, and", + "abs(nums[i] - nums[j]) >= valueDifference", + "1 <= n == nums.length <= 105", + "0 <= nums[i] <= 109", + "0 <= indexDifference <= 105", + "0 <= valueDifference <= 109" + ], + "python_template": "class Solution(object):\n def findIndices(self, nums, indexDifference, valueDifference):\n \"\"\"\n :type nums: List[int]\n :type indexDifference: int\n :type valueDifference: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {\n \n }\n}", + "metadata": { + "func_name": "findIndices" + } +} \ No newline at end of file diff --git a/find-longest-special-substring-that-occurs-thrice-i.json b/find-longest-special-substring-that-occurs-thrice-i.json new file mode 100644 index 0000000000000000000000000000000000000000..2a2a2d636033d4d30a2195e33d0ae32891545a62 --- /dev/null +++ b/find-longest-special-substring-that-occurs-thrice-i.json @@ -0,0 +1,34 @@ +{ + "id": 3267, + "name": "find-longest-special-substring-that-occurs-thrice-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i/", + "date": "2023-12-24", + "task_description": "You are given a string `s` that consists of lowercase English letters. A string is called **special** if it is made up of only a single character. For example, the string `\"abc\"` is not special, whereas the strings `\"ddd\"`, `\"zz\"`, and `\"f\"` are special. Return _the length of the **longest special substring** of _`s` _which occurs **at least thrice**_, _or _`-1`_ if no special substring occurs at least thrice_. A **substring** is a contiguous **non-empty** sequence of characters within a string. **Example 1:** ``` **Input:** s = \"aaaa\" **Output:** 2 **Explanation:** The longest special substring which occurs thrice is \"aa\": substrings \"**aa**aa\", \"a**aa**a\", and \"aa**aa**\". It can be shown that the maximum length achievable is 2. ``` **Example 2:** ``` **Input:** s = \"abcdef\" **Output:** -1 **Explanation:** There exists no special substring which occurs at least thrice. Hence return -1. ``` **Example 3:** ``` **Input:** s = \"abcaba\" **Output:** 1 **Explanation:** The longest special substring which occurs thrice is \"a\": substrings \"**a**bcaba\", \"abc**a**ba\", and \"abcab**a**\". It can be shown that the maximum length achievable is 1. ``` **Constraints:** `3 <= s.length <= 50` `s` consists of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aaaa\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abcdef\"", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "s = \"abcaba\"", + "output": "1 " + } + ], + "constraints": [ + "3 <= s.length <= 50", + "s consists of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def maximumLength(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumLength(String s) {\n \n }\n}", + "metadata": { + "func_name": "maximumLength" + } +} \ No newline at end of file diff --git a/find-longest-special-substring-that-occurs-thrice-ii.json b/find-longest-special-substring-that-occurs-thrice-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..21389a0a7684818f473213c25ce0d4ff0ecf1aff --- /dev/null +++ b/find-longest-special-substring-that-occurs-thrice-ii.json @@ -0,0 +1,34 @@ +{ + "id": 3266, + "name": "find-longest-special-substring-that-occurs-thrice-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-ii/", + "date": "2023-12-24", + "task_description": "You are given a string `s` that consists of lowercase English letters. A string is called **special** if it is made up of only a single character. For example, the string `\"abc\"` is not special, whereas the strings `\"ddd\"`, `\"zz\"`, and `\"f\"` are special. Return _the length of the **longest special substring** of _`s` _which occurs **at least thrice**_, _or _`-1`_ if no special substring occurs at least thrice_. A **substring** is a contiguous **non-empty** sequence of characters within a string. **Example 1:** ``` **Input:** s = \"aaaa\" **Output:** 2 **Explanation:** The longest special substring which occurs thrice is \"aa\": substrings \"**aa**aa\", \"a**aa**a\", and \"aa**aa**\". It can be shown that the maximum length achievable is 2. ``` **Example 2:** ``` **Input:** s = \"abcdef\" **Output:** -1 **Explanation:** There exists no special substring which occurs at least thrice. Hence return -1. ``` **Example 3:** ``` **Input:** s = \"abcaba\" **Output:** 1 **Explanation:** The longest special substring which occurs thrice is \"a\": substrings \"**a**bcaba\", \"abc**a**ba\", and \"abcab**a**\". It can be shown that the maximum length achievable is 1. ``` **Constraints:** `3 <= s.length <= 5 * 105` `s` consists of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aaaa\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abcdef\"", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "s = \"abcaba\"", + "output": "1 " + } + ], + "constraints": [ + "3 <= s.length <= 5 * 105", + "s consists of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def maximumLength(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumLength(String s) {\n \n }\n}", + "metadata": { + "func_name": "maximumLength" + } +} \ No newline at end of file diff --git a/find-maximum-non-decreasing-array-length.json b/find-maximum-non-decreasing-array-length.json new file mode 100644 index 0000000000000000000000000000000000000000..b0dee516d9fd56809502bb0c899a045dac78f955 --- /dev/null +++ b/find-maximum-non-decreasing-array-length.json @@ -0,0 +1,34 @@ +{ + "id": 3211, + "name": "find-maximum-non-decreasing-array-length", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-maximum-non-decreasing-array-length/", + "date": "2023-11-11", + "task_description": "You are given a **0-indexed** integer array `nums`. You can perform any number of operations, where each operation involves selecting a **subarray** of the array and replacing it with the **sum** of its elements. For example, if the given array is `[1,3,5,6]` and you select subarray `[3,5]` the array will convert to `[1,8,6]`. Return _the _**_maximum_**_ length of a _**_non-decreasing_**_ array that can be made after applying operations._ A **subarray** is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [5,2,2] **Output:** 1 **Explanation:** This array with length 3 is not non-decreasing. We have two ways to make the array length two. First, choosing subarray [2,2] converts the array to [5,4]. Second, choosing subarray [5,2] converts the array to [7,2]. In these two ways the array is not non-decreasing. And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing. So the answer is 1. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** 4 **Explanation:** The array is non-decreasing. So the answer is 4. ``` **Example 3:** ``` **Input:** nums = [4,3,2,6] **Output:** 3 **Explanation:** Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing. Because the given array is not non-decreasing, the maximum possible answer is 3. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,2,2]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [4,3,2,6]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def findMaximumLength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findMaximumLength(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findMaximumLength" + } +} \ No newline at end of file diff --git a/find-maximum-number-of-string-pairs.json b/find-maximum-number-of-string-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..016abb7eacdd9df6716d48bf9bc2ea4c37dfe227 --- /dev/null +++ b/find-maximum-number-of-string-pairs.json @@ -0,0 +1,38 @@ +{ + "id": 2847, + "name": "find-maximum-number-of-string-pairs", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-maximum-number-of-string-pairs/", + "date": "2023-06-10", + "task_description": "You are given a **0-indexed** array `words` consisting of **distinct** strings. The string `words[i]` can be paired with the string `words[j]` if: The string `words[i]` is equal to the reversed string of `words[j]`. `0 <= i < j < words.length`. Return _the **maximum** number of pairs that can be formed from the array _`words`_._ Note that each string can belong in **at most one** pair. **Example 1:** ``` **Input:** words = [\"cd\",\"ac\",\"dc\",\"ca\",\"zz\"] **Output:** 2 **Explanation:** In this example, we can form 2 pair of strings in the following way: - We pair the 0th string with the 2nd string, as the reversed string of word[0] is \"dc\" and is equal to words[2]. - We pair the 1st string with the 3rd string, as the reversed string of word[1] is \"ca\" and is equal to words[3]. It can be proven that 2 is the maximum number of pairs that can be formed. ``` **Example 2:** ``` **Input:** words = [\"ab\",\"ba\",\"cc\"] **Output:** 1 **Explanation:** In this example, we can form 1 pair of strings in the following way: - We pair the 0th string with the 1st string, as the reversed string of words[1] is \"ab\" and is equal to words[0]. It can be proven that 1 is the maximum number of pairs that can be formed. ``` **Example 3:** ``` **Input:** words = [\"aa\",\"ab\"] **Output:** 0 **Explanation:** In this example, we are unable to form any pair of strings. ``` **Constraints:** `1 <= words.length <= 50` `words[i].length == 2` `words` consists of distinct strings. `words[i]` contains only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"cd\",\"ac\",\"dc\",\"ca\",\"zz\"]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "words = [\"ab\",\"ba\",\"cc\"]", + "output": "1 " + }, + { + "label": "Example 3", + "input": "words = [\"aa\",\"ab\"]", + "output": "0 " + } + ], + "constraints": [ + "The string words[i] is equal to the reversed string of words[j].", + "0 <= i < j < words.length.", + "1 <= words.length <= 50", + "words[i].length == 2", + "words consists of distinct strings.", + "words[i] contains only lowercase English letters." + ], + "python_template": "class Solution(object):\n def maximumNumberOfStringPairs(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumNumberOfStringPairs(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "maximumNumberOfStringPairs" + } +} \ No newline at end of file diff --git a/find-minimum-cost-to-remove-array-elements.json b/find-minimum-cost-to-remove-array-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..3cf7088c991d06a0987975014d9d1d4ffb8fe9a4 --- /dev/null +++ b/find-minimum-cost-to-remove-array-elements.json @@ -0,0 +1,35 @@ +{ + "id": 3776, + "name": "find-minimum-cost-to-remove-array-elements", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-minimum-cost-to-remove-array-elements/", + "date": "2025-02-15", + "task_description": "You are given an integer array `nums`. Your task is to remove **all elements** from the array by performing one of the following operations at each step until `nums` is empty: Choose any two elements from the first three elements of `nums` and remove them. The cost of this operation is the **maximum** of the two elements removed. If fewer than three elements remain in `nums`, remove all the remaining elements in a single operation. The cost of this operation is the **maximum** of the remaining elements. Return the **minimum** cost required to remove all the elements. **Example 1:** **Input:** nums = [6,2,8,4] **Output:** 12 **Explanation:** Initially, `nums = [6, 2, 8, 4]`. In the first operation, remove `nums[0] = 6` and `nums[2] = 8` with a cost of `max(6, 8) = 8`. Now, `nums = [2, 4]`. In the second operation, remove the remaining elements with a cost of `max(2, 4) = 4`. The cost to remove all elements is `8 + 4 = 12`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 12. **Example 2:** **Input:** nums = [2,1,3,3] **Output:** 5 **Explanation:** Initially, `nums = [2, 1, 3, 3]`. In the first operation, remove `nums[0] = 2` and `nums[1] = 1` with a cost of `max(2, 1) = 2`. Now, `nums = [3, 3]`. In the second operation remove the remaining elements with a cost of `max(3, 3) = 3`. The cost to remove all elements is `2 + 3 = 5`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 5. **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [6,2,8,4]", + "output": "12 " + }, + { + "label": "Example 2", + "input": "nums = [2,1,3,3]", + "output": "5 " + } + ], + "constraints": [ + "Choose any two elements from the first three elements of nums and remove them. The cost of this operation is the maximum of the two elements removed.", + "If fewer than three elements remain in nums, remove all the remaining elements in a single operation. The cost of this operation is the maximum of the remaining elements.", + "In the first operation, remove nums[0] = 6 and nums[2] = 8 with a cost of max(6, 8) = 8. Now, nums = [2, 4].", + "In the second operation, remove the remaining elements with a cost of max(2, 4) = 4.", + "In the first operation, remove nums[0] = 2 and nums[1] = 1 with a cost of max(2, 1) = 2. Now, nums = [3, 3].", + "In the second operation remove the remaining elements with a cost of max(3, 3) = 3.", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def minCost(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minCost(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minCost" + } +} \ No newline at end of file diff --git a/find-minimum-operations-to-make-all-elements-divisible-by-three.json b/find-minimum-operations-to-make-all-elements-divisible-by-three.json new file mode 100644 index 0000000000000000000000000000000000000000..625b970170b0da847627afae7fc07ea4f67918f3 --- /dev/null +++ b/find-minimum-operations-to-make-all-elements-divisible-by-three.json @@ -0,0 +1,32 @@ +{ + "id": 3476, + "name": "find-minimum-operations-to-make-all-elements-divisible-by-three", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/", + "date": "2024-06-08", + "task_description": "You are given an integer array `nums`. In one operation, you can add or subtract 1 from **any** element of `nums`. Return the **minimum** number of operations to make all elements of `nums` divisible by 3. **Example 1:** **Input:** nums = [1,2,3,4] **Output:** 3 **Explanation:** All array elements can be made divisible by 3 using 3 operations: Subtract 1 from 1. Add 1 to 2. Subtract 1 from 4. **Example 2:** **Input:** nums = [3,6,9] **Output:** 0 **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [3,6,9]", + "output": "" + } + ], + "constraints": [ + "Subtract 1 from 1.", + "Add 1 to 2.", + "Subtract 1 from 4.", + "1 <= nums.length <= 50", + "1 <= nums[i] <= 50" + ], + "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/find-mirror-score-of-a-string.json b/find-mirror-score-of-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..ad94bf9148abf276e6ea8986d657d2e8f3709355 --- /dev/null +++ b/find-mirror-score-of-a-string.json @@ -0,0 +1,37 @@ +{ + "id": 3634, + "name": "find-mirror-score-of-a-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-mirror-score-of-a-string/", + "date": "2024-12-29", + "task_description": "You are given a string `s`. We define the **mirror** of a letter in the English alphabet as its corresponding letter when the alphabet is reversed. For example, the mirror of `'a'` is `'z'`, and the mirror of `'y'` is `'b'`. Initially, all characters in the string `s` are **unmarked**. You start with a score of 0, and you perform the following process on the string `s`: Iterate through the string from left to right. At each index `i`, find the closest **unmarked** index `j` such that `j < i` and `s[j]` is the mirror of `s[i]`. Then, **mark** both indices `i` and `j`, and add the value `i - j` to the total score. If no such index `j` exists for the index `i`, move on to the next index without making any changes. Return the total score at the end of the process. **Example 1:** **Input:** s = \"aczzx\" **Output:** 5 **Explanation:** `i = 0`. There is no index `j` that satisfies the conditions, so we skip. `i = 1`. There is no index `j` that satisfies the conditions, so we skip. `i = 2`. The closest index `j` that satisfies the conditions is `j = 0`, so we mark both indices 0 and 2, and then add `2 - 0 = 2` to the score. `i = 3`. There is no index `j` that satisfies the conditions, so we skip. `i = 4`. The closest index `j` that satisfies the conditions is `j = 1`, so we mark both indices 1 and 4, and then add `4 - 1 = 3` to the score. **Example 2:** **Input:** s = \"abcdef\" **Output:** 0 **Explanation:** For each index `i`, there is no index `j` that satisfies the conditions. **Constraints:** `1 <= s.length <= 105` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aczzx\"", + "output": "5 " + }, + { + "label": "Example 2", + "input": "s = \"abcdef\"", + "output": "0 " + } + ], + "constraints": [ + "Iterate through the string from left to right.", + "At each index i, find the closest unmarked index j such that j < i and s[j] is the mirror of s[i]. Then, mark both indices i and j, and add the value i - j to the total score.", + "If no such index j exists for the index i, move on to the next index without making any changes.", + "i = 0. There is no index j that satisfies the conditions, so we skip.", + "i = 1. There is no index j that satisfies the conditions, so we skip.", + "i = 2. The closest index j that satisfies the conditions is j = 0, so we mark both indices 0 and 2, and then add 2 - 0 = 2 to the score.", + "i = 3. There is no index j that satisfies the conditions, so we skip.", + "i = 4. The closest index j that satisfies the conditions is j = 1, so we mark both indices 1 and 4, and then add 4 - 1 = 3 to the score.", + "1 <= s.length <= 105", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def calculateScore(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long calculateScore(String s) {\n \n }\n}", + "metadata": { + "func_name": "calculateScore" + } +} \ No newline at end of file diff --git a/find-missing-and-repeated-values.json b/find-missing-and-repeated-values.json new file mode 100644 index 0000000000000000000000000000000000000000..244c9e276e74e0ee93e75fc6a28f8868969ba1a9 --- /dev/null +++ b/find-missing-and-repeated-values.json @@ -0,0 +1,32 @@ +{ + "id": 3227, + "name": "find-missing-and-repeated-values", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-missing-and-repeated-values/", + "date": "2023-12-10", + "task_description": "You are given a **0-indexed** 2D integer matrix `grid` of size `n * n` with values in the range `[1, n2]`. Each integer appears **exactly once** except `a` which appears **twice** and `b` which is **missing**. The task is to find the repeating and missing numbers `a` and `b`. Return _a **0-indexed **integer array _`ans`_ of size _`2`_ where _`ans[0]`_ equals to _`a`_ and _`ans[1]`_ equals to _`b`_._ **Example 1:** ``` **Input:** grid = [[1,3],[2,2]] **Output:** [2,4] **Explanation:** Number 2 is repeated and number 4 is missing so the answer is [2,4]. ``` **Example 2:** ``` **Input:** grid = [[9,1,7],[8,9,2],[3,4,6]] **Output:** [9,5] **Explanation:** Number 9 is repeated and number 5 is missing so the answer is [9,5]. ``` **Constraints:** `2 <= n == grid.length == grid[i].length <= 50` `1 <= grid[i][j] <= n * n` For all `x` that `1 <= x <= n * n` there is exactly one `x` that is not equal to any of the grid members. For all `x` that `1 <= x <= n * n` there is exactly one `x` that is equal to exactly two of the grid members. For all `x` that `1 <= x <= n * n` except two of them there is exactly one pair of `i, j` that `0 <= i, j <= n - 1` and `grid[i][j] == x`.", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,3],[2,2]]", + "output": "[2,4] " + }, + { + "label": "Example 2", + "input": "grid = [[9,1,7],[8,9,2],[3,4,6]]", + "output": "[9,5] " + } + ], + "constraints": [ + "2 <= n == grid.length == grid[i].length <= 50", + "1 <= grid[i][j] <= n * n", + "For all x that 1 <= x <= n * n there is exactly one x that is not equal to any of the grid members.", + "For all x that 1 <= x <= n * n there is exactly one x that is equal to exactly two of the grid members.", + "For all x that 1 <= x <= n * n except two of them there is exactly one pair of i, j that 0 <= i, j <= n - 1 and grid[i][j] == x." + ], + "python_template": "class Solution(object):\n def findMissingAndRepeatedValues(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findMissingAndRepeatedValues(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "findMissingAndRepeatedValues" + } +} \ No newline at end of file diff --git a/find-number-of-coins-to-place-in-tree-nodes.json b/find-number-of-coins-to-place-in-tree-nodes.json new file mode 100644 index 0000000000000000000000000000000000000000..cc5b39f481d5c1520b69867b7bcfea9d0c26f806 --- /dev/null +++ b/find-number-of-coins-to-place-in-tree-nodes.json @@ -0,0 +1,41 @@ +{ + "id": 3218, + "name": "find-number-of-coins-to-place-in-tree-nodes", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-number-of-coins-to-place-in-tree-nodes/", + "date": "2023-12-09", + "task_description": "You are given an **undirected** tree with `n` nodes labeled from `0` to `n - 1`, and 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. You are also given a **0-indexed** integer array `cost` of length `n`, where `cost[i]` is the **cost** assigned to the `ith` node. You need to place some coins on every node of the tree. The number of coins to be placed at node `i` can be calculated as: If size of the subtree of node `i` is less than `3`, place `1` coin. Otherwise, place an amount of coins equal to the **maximum** product of cost values assigned to `3` distinct nodes in the subtree of node `i`. If this product is **negative**, place `0` coins. Return _an array _`coin`_ of size _`n`_ such that _`coin[i]`_ is the number of coins placed at node _`i`_._ **Example 1:** ``` **Input:** edges = [[0,1],[0,2],[0,3],[0,4],[0,5]], cost = [1,2,3,4,5,6] **Output:** [120,1,1,1,1,1] **Explanation:** For node 0 place 6 * 5 * 4 = 120 coins. All other nodes are leaves with subtree of size 1, place 1 coin on each of them. ``` **Example 2:** ``` **Input:** edges = [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], cost = [1,4,2,3,5,7,8,-4,2] **Output:** [280,140,32,1,1,1,1,1,1] **Explanation:** The coins placed on each node are: - Place 8 * 7 * 5 = 280 coins on node 0. - Place 7 * 5 * 4 = 140 coins on node 1. - Place 8 * 2 * 2 = 32 coins on node 2. - All other nodes are leaves with subtree of size 1, place 1 coin on each of them. ``` **Example 3:** ``` **Input:** edges = [[0,1],[0,2]], cost = [1,2,-2] **Output:** [0,1,1] **Explanation:** Node 1 and 2 are leaves with subtree of size 1, place 1 coin on each of them. For node 0 the only possible product of cost is 2 * 1 * -2 = -4. Hence place 0 coins on node 0. ``` **Constraints:** `2 <= n <= 2 * 104` `edges.length == n - 1` `edges[i].length == 2` `0 <= ai, bi < n` `cost.length == n` `1 <= |cost[i]| <= 104` The input is generated such that `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [[0,1],[0,2],[0,3],[0,4],[0,5]], cost = [1,2,3,4,5,6]", + "output": "[120,1,1,1,1,1] " + }, + { + "label": "Example 2", + "input": "edges = [[0,1],[0,2],[1,3],[1,4],[1,5],[2,6],[2,7],[2,8]], cost = [1,4,2,3,5,7,8,-4,2]", + "output": "[280,140,32,1,1,1,1,1,1] " + }, + { + "label": "Example 3", + "input": "edges = [[0,1],[0,2]], cost = [1,2,-2]", + "output": "[0,1,1] " + } + ], + "constraints": [ + "If size of the subtree of node i is less than 3, place 1 coin.", + "Otherwise, place an amount of coins equal to the maximum product of cost values assigned to 3 distinct nodes in the subtree of node i. If this product is negative, place 0 coins.", + "2 <= n <= 2 * 104", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ai, bi < n", + "cost.length == n", + "1 <= |cost[i]| <= 104", + "The input is generated such that edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def placedCoins(self, edges, cost):\n \"\"\"\n :type edges: List[List[int]]\n :type cost: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] placedCoins(int[][] edges, int[] cost) {\n \n }\n}", + "metadata": { + "func_name": "placedCoins" + } +} \ No newline at end of file diff --git a/find-occurrences-of-an-element-in-an-array.json b/find-occurrences-of-an-element-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..ce7ff783e1e9f8fd9d3fa085971a02bf71d58c47 --- /dev/null +++ b/find-occurrences-of-an-element-in-an-array.json @@ -0,0 +1,35 @@ +{ + "id": 3420, + "name": "find-occurrences-of-an-element-in-an-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-occurrences-of-an-element-in-an-array/", + "date": "2024-05-11", + "task_description": "You are given an integer array `nums`, an integer array `queries`, and an integer `x`. For each `queries[i]`, you need to find the index of the `queries[i]th` occurrence of `x` in the `nums` array. If there are fewer than `queries[i]` occurrences of `x`, the answer should be -1 for that query. Return an integer array `answer` containing the answers to all queries. **Example 1:** **Input:** nums = [1,3,1,7], queries = [1,3,2,4], x = 1 **Output:** [0,-1,2,-1] **Explanation:** For the 1st query, the first occurrence of 1 is at index 0. For the 2nd query, there are only two occurrences of 1 in `nums`, so the answer is -1. For the 3rd query, the second occurrence of 1 is at index 2. For the 4th query, there are only two occurrences of 1 in `nums`, so the answer is -1. **Example 2:** **Input:** nums = [1,2,3], queries = [10], x = 5 **Output:** [-1] **Explanation:** For the 1st query, 5 doesn't exist in `nums`, so the answer is -1. **Constraints:** `1 <= nums.length, queries.length <= 105` `1 <= queries[i] <= 105` `1 <= nums[i], x <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,1,7], queries = [1,3,2,4], x = 1", + "output": "[0,-1,2,-1] " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3], queries = [10], x = 5", + "output": "[-1] " + } + ], + "constraints": [ + "For the 1st query, the first occurrence of 1 is at index 0.", + "For the 2nd query, there are only two occurrences of 1 in nums, so the answer is -1.", + "For the 3rd query, the second occurrence of 1 is at index 2.", + "For the 4th query, there are only two occurrences of 1 in nums, so the answer is -1.", + "For the 1st query, 5 doesn't exist in nums, so the answer is -1.", + "1 <= nums.length, queries.length <= 105", + "1 <= queries[i] <= 105", + "1 <= nums[i], x <= 104" + ], + "python_template": "class Solution(object):\n def occurrencesOfElement(self, nums, queries, x):\n \"\"\"\n :type nums: List[int]\n :type queries: List[int]\n :type x: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] occurrencesOfElement(int[] nums, int[] queries, int x) {\n \n }\n}", + "metadata": { + "func_name": "occurrencesOfElement" + } +} \ No newline at end of file diff --git a/find-polygon-with-the-largest-perimeter.json b/find-polygon-with-the-largest-perimeter.json new file mode 100644 index 0000000000000000000000000000000000000000..ff88c4dc280b2790ae04640827b753319574824f --- /dev/null +++ b/find-polygon-with-the-largest-perimeter.json @@ -0,0 +1,34 @@ +{ + "id": 3262, + "name": "find-polygon-with-the-largest-perimeter", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-polygon-with-the-largest-perimeter/", + "date": "2023-12-09", + "task_description": "You are given an array of **positive** integers `nums` of length `n`. A **polygon** is a closed plane figure that has at least `3` sides. The **longest side** of a polygon is **smaller** than the sum of its other sides. Conversely, if you have `k` (`k >= 3`) **positive** real numbers `a1`, `a2`, `a3`, ..., `ak` where `a1 <= a2 <= a3 <= ... <= ak` **and** `a1 + a2 + a3 + ... + ak-1 > ak`, then there **always** exists a polygon with `k` sides whose lengths are `a1`, `a2`, `a3`, ..., `ak`. The **perimeter** of a polygon is the sum of lengths of its sides. Return _the **largest** possible **perimeter** of a **polygon** whose sides can be formed from_ `nums`, _or_ `-1` _if it is not possible to create a polygon_. **Example 1:** ``` **Input:** nums = [5,5,5] **Output:** 15 **Explanation:** The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15. ``` **Example 2:** ``` **Input:** nums = [1,12,1,2,5,50,3] **Output:** 12 **Explanation:** The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12. We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them. It can be shown that the largest possible perimeter is 12. ``` **Example 3:** ``` **Input:** nums = [5,5,50] **Output:** -1 **Explanation:** There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5. ``` **Constraints:** `3 <= n <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,5,5]", + "output": "15 " + }, + { + "label": "Example 2", + "input": "nums = [1,12,1,2,5,50,3]", + "output": "12 " + }, + { + "label": "Example 3", + "input": "nums = [5,5,50]", + "output": "-1 " + } + ], + "constraints": [ + "3 <= n <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def largestPerimeter(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long largestPerimeter(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "largestPerimeter" + } +} \ No newline at end of file diff --git a/find-products-of-elements-of-big-array.json b/find-products-of-elements-of-big-array.json new file mode 100644 index 0000000000000000000000000000000000000000..708e8f4a1039836f9c0281a1823b232c8fb7247e --- /dev/null +++ b/find-products-of-elements-of-big-array.json @@ -0,0 +1,31 @@ +{ + "id": 3411, + "name": "find-products-of-elements-of-big-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-products-of-elements-of-big-array/", + "date": "2024-04-27", + "task_description": "The **powerful array** of a non-negative integer `x` is defined as the shortest sorted array of powers of two that sum up to `x`. The table below illustrates examples of how the **powerful array** is determined. It can be proven that the powerful array of `x` is unique. num Binary Representation powerful array 1 00001 [1] 8 01000 [8] 10 01010 [2, 8] 13 01101 [1, 4, 8] 23 10111 [1, 2, 4, 16] The array `big_nums` is created by concatenating the **powerful arrays** for every positive integer `i` in ascending order: 1, 2, 3, and so on. Thus, `big_nums` begins as `[1, 2, 1, 2, 4, 1, 4, 2, 4, 1, 2, 4, 8, ...]`. You are given a 2D integer matrix `queries`, where for `queries[i] = [fromi, toi, modi]` you should calculate `(big_nums[fromi] * big_nums[fromi + 1] * ... * big_nums[toi]) % modi`. Return an integer array `answer` such that `answer[i]` is the answer to the `ith` query. **Example 1:** **Input:** queries = [[1,3,7]] **Output:** [4] **Explanation:** There is one query. `big_nums[1..3] = [2,1,2]`. The product of them is 4. The result is `4 % 7 = 4.` **Example 2:** **Input:** queries = [[2,5,3],[7,7,4]] **Output:** [2,2] **Explanation:** There are two queries. First query: `big_nums[2..5] = [1,2,4,1]`. The product of them is 8. The result is `8 % 3 = 2`. Second query: `big_nums[7] = 2`. The result is `2 % 4 = 2`. **Constraints:** `1 <= queries.length <= 500` `queries[i].length == 3` `0 <= queries[i][0] <= queries[i][1] <= 1015` `1 <= queries[i][2] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "queries = [[1,3,7]]", + "output": "[4] " + }, + { + "label": "Example 2", + "input": "queries = [[2,5,3],[7,7,4]]", + "output": "[2,2] " + } + ], + "constraints": [ + "1 <= queries.length <= 500", + "queries[i].length == 3", + "0 <= queries[i][0] <= queries[i][1] <= 1015", + "1 <= queries[i][2] <= 105" + ], + "python_template": "class Solution(object):\n def findProductsOfElements(self, queries):\n \"\"\"\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findProductsOfElements(long[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "findProductsOfElements" + } +} \ No newline at end of file diff --git a/find-score-of-an-array-after-marking-all-elements.json b/find-score-of-an-array-after-marking-all-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..b07ec8e8d849ffd26d4b1a43af04e27e480c0425 --- /dev/null +++ b/find-score-of-an-array-after-marking-all-elements.json @@ -0,0 +1,33 @@ +{ + "id": 2695, + "name": "find-score-of-an-array-after-marking-all-elements", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/", + "date": "2023-03-04", + "task_description": "You are given an array `nums` consisting of positive integers. Starting with `score = 0`, apply the following algorithm: Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. Add the value of the chosen integer to `score`. Mark **the chosen element and its two adjacent elements if they exist**. Repeat until all the array elements are marked. Return _the score you get after applying the above algorithm_. **Example 1:** ``` **Input:** nums = [2,1,3,4,5,2] **Output:** 7 **Explanation:** We mark the elements as follows: - 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2]. - 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2]. - 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2]. Our score is 1 + 2 + 4 = 7. ``` **Example 2:** ``` **Input:** nums = [2,3,5,1,3,2] **Output:** 5 **Explanation:** We mark the elements as follows: - 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2]. - 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [2,3,5,1,3,2]. - 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2]. Our score is 1 + 2 + 2 = 5. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,3,4,5,2]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [2,3,5,1,3,2]", + "output": "5 " + } + ], + "constraints": [ + "Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.", + "Add the value of the chosen integer to score.", + "Mark the chosen element and its two adjacent elements if they exist.", + "Repeat until all the array elements are marked.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def findScore(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long findScore(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findScore" + } +} \ No newline at end of file diff --git a/find-special-substring-of-length-k.json b/find-special-substring-of-length-k.json new file mode 100644 index 0000000000000000000000000000000000000000..d6cb7131691802cbc5201f3c21ba9bc8a8615da8 --- /dev/null +++ b/find-special-substring-of-length-k.json @@ -0,0 +1,33 @@ +{ + "id": 3709, + "name": "find-special-substring-of-length-k", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-special-substring-of-length-k/", + "date": "2025-02-09", + "task_description": "You are given a string `s` and an integer `k`. Determine if there exists a substring of length **exactly** `k` in `s` that satisfies the following conditions: The substring consists of **only one distinct character** (e.g., `\"aaa\"` or `\"bbb\"`). If there is a character **immediately before** the substring, it must be different from the character in the substring. If there is a character **immediately after** the substring, it must also be different from the character in the substring. Return `true` if such a substring exists. Otherwise, return `false`. **Example 1:** **Input:** s = \"aaabaaa\", k = 3 **Output:** true **Explanation:** The substring `s[4..6] == \"aaa\"` satisfies the conditions. It has a length of 3. All characters are the same. The character before `\"aaa\"` is `'b'`, which is different from `'a'`. There is no character after `\"aaa\"`. **Example 2:** **Input:** s = \"abc\", k = 2 **Output:** false **Explanation:** There is no substring of length 2 that consists of one distinct character and satisfies the conditions. **Constraints:** `1 <= k <= s.length <= 100` `s` consists of lowercase English letters only.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aaabaaa\", k = 3", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"abc\", k = 2", + "output": "false " + } + ], + "constraints": [ + "It has a length of 3.", + "All characters are the same.", + "The character before \"aaa\" is 'b', which is different from 'a'.", + "There is no character after \"aaa\".", + "1 <= k <= s.length <= 100", + "s consists of lowercase English letters only." + ], + "python_template": "class Solution(object):\n def hasSpecialSubstring(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean hasSpecialSubstring(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "hasSpecialSubstring" + } +} \ No newline at end of file diff --git a/find-subarray-with-bitwise-or-closest-to-k.json b/find-subarray-with-bitwise-or-closest-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..7c1b7e509ce389974e827c5b3d337066d8ee30cb --- /dev/null +++ b/find-subarray-with-bitwise-or-closest-to-k.json @@ -0,0 +1,35 @@ +{ + "id": 3436, + "name": "find-subarray-with-bitwise-or-closest-to-k", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-subarray-with-bitwise-or-closest-to-k/", + "date": "2024-05-26", + "task_description": "You are given an array `nums` and an integer `k`. You need to find a subarray of `nums` such that the **absolute difference** between `k` and the bitwise `OR` of the subarray elements is as** small** as possible. In other words, select a subarray `nums[l..r]` such that `|k - (nums[l] OR nums[l + 1] ... OR nums[r])|` is minimum. Return the **minimum** possible value of the absolute difference. A **subarray** is a contiguous non-empty sequence of elements within an array. **Example 1:** **Input:** nums = [1,2,4,5], k = 3 **Output:** 0 **Explanation:** The subarray `nums[0..1]` has `OR` value 3, which gives the minimum absolute difference `|3 - 3| = 0`. **Example 2:** **Input:** nums = [1,3,1,3], k = 2 **Output:** 1 **Explanation:** The subarray `nums[1..1]` has `OR` value 3, which gives the minimum absolute difference `|3 - 2| = 1`. **Example 3:** **Input:** nums = [1], k = 10 **Output:** 9 **Explanation:** There is a single subarray with `OR` value 1, which gives the minimum absolute difference `|10 - 1| = 9`. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `1 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,4,5], k = 3", + "output": "0 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,1,3], k = 2", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [1], k = 10", + "output": "9 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "1 <= k <= 109" + ], + "python_template": "class Solution(object):\n def minimumDifference(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 minimumDifference(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumDifference" + } +} \ No newline at end of file diff --git a/find-subarrays-with-equal-sum.json b/find-subarrays-with-equal-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..cc63254440e684ca442169c11abb6df75a42b9be --- /dev/null +++ b/find-subarrays-with-equal-sum.json @@ -0,0 +1,34 @@ +{ + "id": 2480, + "name": "find-subarrays-with-equal-sum", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-subarrays-with-equal-sum/", + "date": "2022-08-20", + "task_description": "Given a **0-indexed** integer array `nums`, determine whether there exist **two** subarrays of length `2` with **equal** sum. Note that the two subarrays must begin at **different** indices. Return `true`_ if these subarrays exist, and _`false`_ otherwise._ A subarray is a contiguous non-empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [4,2,4] **Output:** true **Explanation:** The subarrays with elements [4,2] and [2,4] have the same sum of 6. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4,5] **Output:** false **Explanation:** No two subarrays of size 2 have the same sum. ``` **Example 3:** ``` **Input:** nums = [0,0,0] **Output:** true **Explanation:** The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0. Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array. ``` **Constraints:** `2 <= nums.length <= 1000` `-109 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,2,4]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4,5]", + "output": "false " + }, + { + "label": "Example 3", + "input": "nums = [0,0,0]", + "output": "true " + } + ], + "constraints": [ + "2 <= nums.length <= 1000", + "-109 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def findSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean findSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findSubarrays" + } +} \ No newline at end of file diff --git a/find-substring-with-given-hash-value.json b/find-substring-with-given-hash-value.json new file mode 100644 index 0000000000000000000000000000000000000000..1865513e8b90dacc0a147e3bc750f9a47a3e0d2c --- /dev/null +++ b/find-substring-with-given-hash-value.json @@ -0,0 +1,33 @@ +{ + "id": 2275, + "name": "find-substring-with-given-hash-value", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-substring-with-given-hash-value/", + "date": "2022-01-23", + "task_description": "The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function: `hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m`. Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`. You are given a string `s` and the integers `power`, `modulo`, `k`, and `hashValue.` Return `sub`,_ the **first** **substring** of _`s`_ of length _`k`_ such that _`hash(sub, power, modulo) == hashValue`. The test cases will be generated such that an answer always **exists**. A substring is a contiguous non-empty sequence of characters within a string. **Example 1:** ``` **Input:** s = \"leetcode\", power = 7, modulo = 20, k = 2, hashValue = 0 **Output:** \"ee\" **Explanation:** The hash of \"ee\" can be computed to be hash(\"ee\", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0. \"ee\" is the first substring of length 2 with hashValue 0. Hence, we return \"ee\". ``` **Example 2:** ``` **Input:** s = \"fbxzaad\", power = 31, modulo = 100, k = 3, hashValue = 32 **Output:** \"fbx\" **Explanation:** The hash of \"fbx\" can be computed to be hash(\"fbx\", 31, 100) = (6 * 1 + 2 * 31 + 24 * 312) mod 100 = 23132 mod 100 = 32. The hash of \"bxz\" can be computed to be hash(\"bxz\", 31, 100) = (2 * 1 + 24 * 31 + 26 * 312) mod 100 = 25732 mod 100 = 32. \"fbx\" is the first substring of length 3 with hashValue 32. Hence, we return \"fbx\". Note that \"bxz\" also has a hash of 32 but it appears later than \"fbx\". ``` **Constraints:** `1 <= k <= s.length <= 2 * 104` `1 <= power, modulo <= 109` `0 <= hashValue < modulo` `s` consists of lowercase English letters only. The test cases are generated such that an answer always **exists**.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"leetcode\", power = 7, modulo = 20, k = 2, hashValue = 0", + "output": "\"ee\" " + }, + { + "label": "Example 2", + "input": "s = \"fbxzaad\", power = 31, modulo = 100, k = 3, hashValue = 32", + "output": "\"fbx\" " + } + ], + "constraints": [ + "hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.", + "1 <= k <= s.length <= 2 * 104", + "1 <= power, modulo <= 109", + "0 <= hashValue < modulo", + "s consists of lowercase English letters only.", + "The test cases are generated such that an answer always exists." + ], + "python_template": "class Solution(object):\n def subStrHash(self, s, power, modulo, k, hashValue):\n \"\"\"\n :type s: str\n :type power: int\n :type modulo: int\n :type k: int\n :type hashValue: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String subStrHash(String s, int power, int modulo, int k, int hashValue) {\n \n }\n}", + "metadata": { + "func_name": "subStrHash" + } +} \ No newline at end of file diff --git a/find-subtree-sizes-after-changes.json b/find-subtree-sizes-after-changes.json new file mode 100644 index 0000000000000000000000000000000000000000..7be88e63892d0ace48ab55b3bca9a06590bafe48 --- /dev/null +++ b/find-subtree-sizes-after-changes.json @@ -0,0 +1,38 @@ +{ + "id": 3576, + "name": "find-subtree-sizes-after-changes", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-subtree-sizes-after-changes/", + "date": "2024-10-12", + "task_description": "You are given a tree rooted at node 0 that consists of `n` nodes numbered from `0` to `n - 1`. The tree is represented by an array `parent` of size `n`, where `parent[i]` is the parent of node `i`. Since node 0 is the root, `parent[0] == -1`. You are also given a string `s` of length `n`, where `s[i]` is the character assigned to node `i`. We make the following changes on the tree **one** time **simultaneously** for all nodes `x` from `1` to `n - 1`: Find the **closest** node `y` to node `x` such that `y` is an ancestor of `x`, and `s[x] == s[y]`. If node `y` does not exist, do nothing. Otherwise, **remove** the edge between `x` and its current parent and make node `y` the new parent of `x` by adding an edge between them. Return an array `answer` of size `n` where `answer[i]` is the **size** of the subtree rooted at node `i` in the **final** tree. **Example 1:** **Input:** parent = [-1,0,0,1,1,1], s = \"abaabc\" **Output:** [6,3,1,1,1,1] **Explanation:** The parent of node 3 will change from node 1 to node 0. **Example 2:** **Input:** parent = [-1,0,4,0,1], s = \"abbba\" **Output:** [5,2,1,1,1] **Explanation:** The following changes will happen at the same time: The parent of node 4 will change from node 1 to node 0. The parent of node 2 will change from node 4 to node 1. **Constraints:** `n == parent.length == s.length` `1 <= n <= 105` `0 <= parent[i] <= n - 1` for all `i >= 1`. `parent[0] == -1` `parent` represents a valid tree. `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "parent = [-1,0,0,1,1,1], s = \"abaabc\"", + "output": "[6,3,1,1,1,1] " + }, + { + "label": "Example 2", + "input": "parent = [-1,0,4,0,1], s = \"abbba\"", + "output": "[5,2,1,1,1] " + } + ], + "constraints": [ + "Find the closest node y to node x such that y is an ancestor of x, and s[x] == s[y].", + "If node y does not exist, do nothing.", + "Otherwise, remove the edge between x and its current parent and make node y the new parent of x by adding an edge between them.", + "The parent of node 4 will change from node 1 to node 0.", + "The parent of node 2 will change from node 4 to node 1.", + "n == parent.length == s.length", + "1 <= n <= 105", + "0 <= parent[i] <= n - 1 for all i >= 1.", + "parent[0] == -1", + "parent represents a valid tree.", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def findSubtreeSizes(self, parent, s):\n \"\"\"\n :type parent: List[int]\n :type s: str\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findSubtreeSizes(int[] parent, String s) {\n \n }\n}", + "metadata": { + "func_name": "findSubtreeSizes" + } +} \ No newline at end of file diff --git a/find-the-array-concatenation-value.json b/find-the-array-concatenation-value.json new file mode 100644 index 0000000000000000000000000000000000000000..03659cb645d858ebc484ff4b8a679d4063b18c0f --- /dev/null +++ b/find-the-array-concatenation-value.json @@ -0,0 +1,32 @@ +{ + "id": 2698, + "name": "find-the-array-concatenation-value", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-array-concatenation-value/", + "date": "2023-02-05", + "task_description": "You are given a **0-indexed** integer array `nums`. The **concatenation** of two numbers is the number formed by concatenating their numerals. For example, the concatenation of `15`, `49` is `1549`. The **concatenation value** of `nums` is initially equal to `0`. Perform this operation until `nums` becomes empty: If `nums` has a size greater than one, add the value of the concatenation of the first and the last element to the **concatenation value** of `nums`, and remove those two elements from `nums`. For example, if the `nums` was `[1, 2, 4, 5, 6]`, add 16 to the `concatenation value`. If only one element exists in `nums`, add its value to the **concatenation value** of `nums`, then remove it. Return_ the concatenation value of `nums`_. **Example 1:** ``` **Input:** nums = [7,52,2,4] **Output:** 596 **Explanation:** Before performing any operation, nums is [7,52,2,4] and concatenation value is 0. - In the first operation: We pick the first element, 7, and the last element, 4. Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74. Then we delete them from nums, so nums becomes equal to [52,2]. - In the second operation: We pick the first element, 52, and the last element, 2. Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596. Then we delete them from the nums, so nums becomes empty. Since the concatenation value is 596 so the answer is 596. ``` **Example 2:** ``` **Input:** nums = [5,14,13,8,12] **Output:** 673 **Explanation:** Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0. - In the first operation: We pick the first element, 5, and the last element, 12. Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512. Then we delete them from the nums, so nums becomes equal to [14,13,8]. - In the second operation: We pick the first element, 14, and the last element, 8. Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660. Then we delete them from the nums, so nums becomes equal to [13]. - In the third operation: nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673. Then we delete it from nums, so nums become empty. Since the concatenation value is 673 so the answer is 673. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [7,52,2,4]", + "output": "596 " + }, + { + "label": "Example 2", + "input": "nums = [5,14,13,8,12]", + "output": "673 " + } + ], + "constraints": [ + "For example, the concatenation of 15, 49 is 1549.", + "If nums has a size greater than one, add the value of the concatenation of the first and the last element to the concatenation value of nums, and remove those two elements from nums. For example, if the nums was [1, 2, 4, 5, 6], add 16 to the concatenation value.", + "If only one element exists in nums, add its value to the concatenation value of nums, then remove it.", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 104" + ], + "python_template": "class Solution(object):\n def findTheArrayConcVal(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long findTheArrayConcVal(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findTheArrayConcVal" + } +} \ No newline at end of file diff --git a/find-the-child-who-has-the-ball-after-k-seconds.json b/find-the-child-who-has-the-ball-after-k-seconds.json new file mode 100644 index 0000000000000000000000000000000000000000..55d69014207ce10f14f1d4426731d3561d6dc758 --- /dev/null +++ b/find-the-child-who-has-the-ball-after-k-seconds.json @@ -0,0 +1,34 @@ +{ + "id": 3450, + "name": "find-the-child-who-has-the-ball-after-k-seconds", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds/", + "date": "2024-06-02", + "task_description": "You are given two **positive** integers `n` and `k`. There are `n` children numbered from `0` to `n - 1` standing in a queue _in order_ from left to right. Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches **either** end of the line, i.e. child 0 or child `n - 1`, the direction of passing is **reversed**. Return the number of the child who receives the ball after `k` seconds. **Example 1:** **Input:** n = 3, k = 5 **Output:** 1 **Explanation:** Time elapsed Children `0` `[0, 1, 2]` `1` `[0, 1, 2]` `2` `[0, 1, 2]` `3` `[0, 1, 2]` `4` `[0, 1, 2]` `5` `[0, 1, 2]` **Example 2:** **Input:** n = 5, k = 6 **Output:** 2 **Explanation:** Time elapsed Children `0` `[0, 1, 2, 3, 4]` `1` `[0, 1, 2, 3, 4]` `2` `[0, 1, 2, 3, 4]` `3` `[0, 1, 2, 3, 4]` `4` `[0, 1, 2, 3, 4]` `5` `[0, 1, 2, 3, 4]` `6` `[0, 1, 2, 3, 4]` **Example 3:** **Input:** n = 4, k = 2 **Output:** 2 **Explanation:** Time elapsed Children `0` `[0, 1, 2, 3]` `1` `[0, 1, 2, 3]` `2` `[0, 1, 2, 3]` **Constraints:** `2 <= n <= 50` `1 <= k <= 50` **Note:** This question is the same as 2582: Pass the Pillow.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, k = 5", + "output": "1 " + }, + { + "label": "Example 2", + "input": "n = 5, k = 6", + "output": "2 " + }, + { + "label": "Example 3", + "input": "n = 4, k = 2", + "output": "2 " + } + ], + "constraints": [ + "2 <= n <= 50", + "1 <= k <= 50" + ], + "python_template": "class Solution(object):\n def numberOfChild(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfChild(int n, int k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfChild" + } +} \ No newline at end of file diff --git a/find-the-count-of-good-integers.json b/find-the-count-of-good-integers.json new file mode 100644 index 0000000000000000000000000000000000000000..0147e979760a351db1c83649cac0576a9de9cc3a --- /dev/null +++ b/find-the-count-of-good-integers.json @@ -0,0 +1,38 @@ +{ + "id": 3548, + "name": "find-the-count-of-good-integers", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-count-of-good-integers/", + "date": "2024-08-17", + "task_description": "You are given two **positive** integers `n` and `k`. An integer `x` is called **k-palindromic** if: `x` is a palindrome. `x` is divisible by `k`. An integer is called **good** if its digits can be _rearranged_ to form a **k-palindromic** integer. For example, for `k = 2`, 2020 can be rearranged to form the _k-palindromic_ integer 2002, whereas 1010 cannot be rearranged to form a _k-palindromic_ integer. Return the count of **good** integers containing `n` digits. **Note** that _any_ integer must **not** have leading zeros, **neither** before **nor** after rearrangement. For example, 1010 _cannot_ be rearranged to form 101. **Example 1:** **Input:** n = 3, k = 5 **Output:** 27 **Explanation:** _Some_ of the good integers are: 551 because it can be rearranged to form 515. 525 because it is already k-palindromic. **Example 2:** **Input:** n = 1, k = 4 **Output:** 2 **Explanation:** The two good integers are 4 and 8. **Example 3:** **Input:** n = 5, k = 6 **Output:** 2468 **Constraints:** `1 <= n <= 10` `1 <= k <= 9`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, k = 5", + "output": "27 " + }, + { + "label": "Example 2", + "input": "n = 1, k = 4", + "output": "2 " + }, + { + "label": "Example 3", + "input": "n = 5, k = 6", + "output": "246" + } + ], + "constraints": [ + "x is a palindrome.", + "x is divisible by k.", + "551 because it can be rearranged to form 515.", + "525 because it is already k-palindromic.", + "1 <= n <= 10", + "1 <= k <= 9" + ], + "python_template": "class Solution(object):\n def countGoodIntegers(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long countGoodIntegers(int n, int k) {\n \n }\n}", + "metadata": { + "func_name": "countGoodIntegers" + } +} \ No newline at end of file diff --git a/find-the-distinct-difference-array.json b/find-the-distinct-difference-array.json new file mode 100644 index 0000000000000000000000000000000000000000..ff1ffffeec6bdafe4a09f8ec1692de85cff9f8f3 --- /dev/null +++ b/find-the-distinct-difference-array.json @@ -0,0 +1,29 @@ +{ + "id": 2777, + "name": "find-the-distinct-difference-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-distinct-difference-array/", + "date": "2023-04-30", + "task_description": "You are given a **0-indexed** array `nums` of length `n`. The **distinct difference** array of `nums` is an array `diff` of length `n` such that `diff[i]` is equal to the number of distinct elements in the suffix `nums[i + 1, ..., n - 1]` **subtracted from** the number of distinct elements in the prefix `nums[0, ..., i]`. Return _the **distinct difference** array of _`nums`. Note that `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j` inclusive. Particularly, if `i > j` then `nums[i, ..., j]` denotes an empty subarray. **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** [-3,-1,1,3,5] **Explanation:** For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3. For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1. For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1. For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3. For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5. ``` **Example 2:** ``` **Input:** nums = [3,2,3,4,2] **Output:** [-2,-1,0,2,3] **Explanation:** For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2. For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1. For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0. For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2. For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3. ``` **Constraints:** `1 <= n == nums.length <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5]", + "output": "[-3,-1,1,3,5] " + }, + { + "label": "Example 2", + "input": "nums = [3,2,3,4,2]", + "output": "[-2,-1,0,2,3] " + } + ], + "constraints": [ + "1 <= n == nums.length <= 50", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def distinctDifferenceArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] distinctDifferenceArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "distinctDifferenceArray" + } +} \ No newline at end of file diff --git a/find-the-divisibility-array-of-a-string.json b/find-the-divisibility-array-of-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..638249d6e045b7108882223e2e9e1c13d910a33b --- /dev/null +++ b/find-the-divisibility-array-of-a-string.json @@ -0,0 +1,33 @@ +{ + "id": 2713, + "name": "find-the-divisibility-array-of-a-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-divisibility-array-of-a-string/", + "date": "2023-02-19", + "task_description": "You are given a **0-indexed** string `word` of length `n` consisting of digits, and a positive integer `m`. The **divisibility array** `div` of `word` is an integer array of length `n` such that: `div[i] = 1` if the **numeric value** of `word[0,...,i]` is divisible by `m`, or `div[i] = 0` otherwise. Return_ the divisibility array of__ _`word`. **Example 1:** ``` **Input:** word = \"998244353\", m = 3 **Output:** [1,1,0,0,0,1,1,0,0] **Explanation:** There are only 4 prefixes that are divisible by 3: \"9\", \"99\", \"998244\", and \"9982443\". ``` **Example 2:** ``` **Input:** word = \"1010\", m = 10 **Output:** [0,1,0,1] **Explanation:** There are only 2 prefixes that are divisible by 10: \"10\", and \"1010\". ``` **Constraints:** `1 <= n <= 105` `word.length == n` `word` consists of digits from `0` to `9` `1 <= m <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"998244353\", m = 3", + "output": "[1,1,0,0,0,1,1,0,0] " + }, + { + "label": "Example 2", + "input": "word = \"1010\", m = 10", + "output": "[0,1,0,1] " + } + ], + "constraints": [ + "div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, or", + "div[i] = 0 otherwise.", + "1 <= n <= 105", + "word.length == n", + "word consists of digits from 0 to 9", + "1 <= m <= 109" + ], + "python_template": "class Solution(object):\n def divisibilityArray(self, word, m):\n \"\"\"\n :type word: str\n :type m: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] divisibilityArray(String word, int m) {\n \n }\n}", + "metadata": { + "func_name": "divisibilityArray" + } +} \ No newline at end of file diff --git a/find-the-encrypted-string.json b/find-the-encrypted-string.json new file mode 100644 index 0000000000000000000000000000000000000000..3ef2dd73cc6331c1393e2fc19e871640d8e47595 --- /dev/null +++ b/find-the-encrypted-string.json @@ -0,0 +1,35 @@ +{ + "id": 3468, + "name": "find-the-encrypted-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-encrypted-string/", + "date": "2024-06-30", + "task_description": "You are given a string `s` and an integer `k`. Encrypt the string using the following algorithm: For each character `c` in `s`, replace `c` with the `kth` character after `c` in the string (in a cyclic manner). Return the _encrypted string_. **Example 1:** **Input:** s = \"dart\", k = 3 **Output:** \"tdar\" **Explanation:** For `i = 0`, the 3rd character after `'d'` is `'t'`. For `i = 1`, the 3rd character after `'a'` is `'d'`. For `i = 2`, the 3rd character after `'r'` is `'a'`. For `i = 3`, the 3rd character after `'t'` is `'r'`. **Example 2:** **Input:** s = \"aaa\", k = 1 **Output:** \"aaa\" **Explanation:** As all the characters are the same, the encrypted string will also be the same. **Constraints:** `1 <= s.length <= 100` `1 <= k <= 104` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"dart\", k = 3", + "output": "\"tdar\" " + }, + { + "label": "Example 2", + "input": "s = \"aaa\", k = 1", + "output": "\"aaa\" " + } + ], + "constraints": [ + "For each character c in s, replace c with the kth character after c in the string (in a cyclic manner).", + "For i = 0, the 3rd character after 'd' is 't'.", + "For i = 1, the 3rd character after 'a' is 'd'.", + "For i = 2, the 3rd character after 'r' is 'a'.", + "For i = 3, the 3rd character after 't' is 'r'.", + "1 <= s.length <= 100", + "1 <= k <= 104", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def getEncryptedString(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String getEncryptedString(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "getEncryptedString" + } +} \ No newline at end of file diff --git a/find-the-first-player-to-win-k-games-in-a-row.json b/find-the-first-player-to-win-k-games-in-a-row.json new file mode 100644 index 0000000000000000000000000000000000000000..b6626703dd72a11b5634c641886a732578cd9664 --- /dev/null +++ b/find-the-first-player-to-win-k-games-in-a-row.json @@ -0,0 +1,40 @@ +{ + "id": 3413, + "name": "find-the-first-player-to-win-k-games-in-a-row", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-first-player-to-win-k-games-in-a-row/", + "date": "2024-05-25", + "task_description": "A competition consists of `n` players numbered from `0` to `n - 1`. You are given an integer array `skills` of size `n` and a **positive** integer `k`, where `skills[i]` is the skill level of player `i`. All integers in `skills` are **unique**. All players are standing in a queue in order from player `0` to player `n - 1`. The competition process is as follows: The first two players in the queue play a game, and the player with the **higher** skill level wins. After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it. The winner of the competition is the **first** player who wins `k` games **in a row**. Return the initial index of the _winning_ player. **Example 1:** **Input:** skills = [4,2,6,3,9], k = 2 **Output:** 2 **Explanation:** Initially, the queue of players is `[0,1,2,3,4]`. The following process happens: Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is `[0,2,3,4,1]`. Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is `[2,3,4,1,0]`. Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is `[2,4,1,0,3]`. Player 2 won `k = 2` games in a row, so the winner is player 2. **Example 2:** **Input:** skills = [2,5,4], k = 3 **Output:** 1 **Explanation:** Initially, the queue of players is `[0,1,2]`. The following process happens: Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`. Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is `[1,0,2]`. Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`. Player 1 won `k = 3` games in a row, so the winner is player 1. **Constraints:** `n == skills.length` `2 <= n <= 105` `1 <= k <= 109` `1 <= skills[i] <= 106` All integers in `skills` are unique.", + "test_case": [ + { + "label": "Example 1", + "input": "skills = [4,2,6,3,9], k = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "skills = [2,5,4], k = 3", + "output": "1 " + } + ], + "constraints": [ + "The first two players in the queue play a game, and the player with the higher skill level wins.", + "After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.", + "Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is [0,2,3,4,1].", + "Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is [2,3,4,1,0].", + "Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is [2,4,1,0,3].", + "Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].", + "Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is [1,0,2].", + "Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].", + "n == skills.length", + "2 <= n <= 105", + "1 <= k <= 109", + "1 <= skills[i] <= 106", + "All integers in skills are unique." + ], + "python_template": "class Solution(object):\n def findWinningPlayer(self, skills, k):\n \"\"\"\n :type skills: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findWinningPlayer(int[] skills, int k) {\n \n }\n}", + "metadata": { + "func_name": "findWinningPlayer" + } +} \ No newline at end of file diff --git a/find-the-integer-added-to-array-i.json b/find-the-integer-added-to-array-i.json new file mode 100644 index 0000000000000000000000000000000000000000..b1e522be0465bcf4d30d68d107751e87d76146e6 --- /dev/null +++ b/find-the-integer-added-to-array-i.json @@ -0,0 +1,35 @@ +{ + "id": 3397, + "name": "find-the-integer-added-to-array-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-integer-added-to-array-i/", + "date": "2024-04-21", + "task_description": "You are given two arrays of equal length, `nums1` and `nums2`. Each element in `nums1` has been increased (or decreased in the case of negative) by an integer, represented by the variable `x`. As a result, `nums1` becomes **equal** to `nums2`. Two arrays are considered **equal** when they contain the same integers with the same frequencies. Return the integer `x`. **Example 1:** **Input:** nums1 = [2,6,4], nums2 = [9,7,5] **Output:** 3 **Explanation:** The integer added to each element of `nums1` is 3. **Example 2:** **Input:** nums1 = [10], nums2 = [5] **Output:** -5 **Explanation:** The integer added to each element of `nums1` is -5. **Example 3:** **Input:** nums1 = [1,1,1,1], nums2 = [1,1,1,1] **Output:** 0 **Explanation:** The integer added to each element of `nums1` is 0. **Constraints:** `1 <= nums1.length == nums2.length <= 100` `0 <= nums1[i], nums2[i] <= 1000` The test cases are generated in a way that there is an integer `x` such that `nums1` can become equal to `nums2` by adding `x` to each element of `nums1`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [2,6,4], nums2 = [9,7,5]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums1 = [10], nums2 = [5]", + "output": "-5 " + }, + { + "label": "Example 3", + "input": "nums1 = [1,1,1,1], nums2 = [1,1,1,1]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums1.length == nums2.length <= 100", + "0 <= nums1[i], nums2[i] <= 1000", + "The test cases are generated in a way that there is an integer x such that nums1 can become equal to nums2 by adding x to each element of nums1." + ], + "python_template": "class Solution(object):\n def addedInteger(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 addedInteger(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "addedInteger" + } +} \ No newline at end of file diff --git a/find-the-k-or-of-an-array.json b/find-the-k-or-of-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..e74174414cae1f7ff6d667a07ed518511cac3032 --- /dev/null +++ b/find-the-k-or-of-an-array.json @@ -0,0 +1,35 @@ +{ + "id": 3183, + "name": "find-the-k-or-of-an-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-k-or-of-an-array/", + "date": "2023-10-22", + "task_description": "You are given an integer array `nums`, and an integer `k`. Let's introduce **K-or** operation by extending the standard bitwise OR. In K-or, a bit position in the result is set to `1` if at least `k` numbers in `nums` have a `1` in that position. Return _the K-or of_ `nums`. **Example 1: ** **Input:** nums = [7,12,9,8,9,15], k = 4 **Output:** 9 **Explanation: ** Represent numbers in binary: Number Bit 3 Bit 2 Bit 1 Bit 0 7 0 1 1 1 12 1 1 0 0 9 1 0 0 1 8 1 0 0 0 9 1 0 0 1 15 1 1 1 1 Result = 9 1 0 0 1 Bit 0 is set in 7, 9, 9, and 15. Bit 3 is set in 12, 9, 8, 9, and 15. Only bits 0 and 3 qualify. The result is `(1001)2 = 9`. **Example 2: ** **Input:** nums = [2,12,1,11,4,5], k = 6 **Output:** 0 **Explanation: **No bit appears as 1 in all six array numbers, as required for K-or with `k = 6`. Thus, the result is 0. **Example 3: ** **Input:** nums = [10,8,5,9,11,6,8], k = 1 **Output:** 15 **Explanation: ** Since `k == 1`, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is `10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15`. **Constraints:** `1 <= nums.length <= 50` `0 <= nums[i] < 231` `1 <= k <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [7,12,9,8,9,15], k = 4", + "output": "9 " + }, + { + "label": "Example 2", + "input": "nums = [2,12,1,11,4,5], k = 6", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [10,8,5,9,11,6,8], k = 1", + "output": "15 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "0 <= nums[i] < 231", + "1 <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def findKOr(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 findKOr(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "findKOr" + } +} \ No newline at end of file diff --git a/find-the-k-sum-of-an-array.json b/find-the-k-sum-of-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..bb8994d195a6251a217cfbec7d18479dc6508544 --- /dev/null +++ b/find-the-k-sum-of-an-array.json @@ -0,0 +1,31 @@ +{ + "id": 2462, + "name": "find-the-k-sum-of-an-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-k-sum-of-an-array/", + "date": "2022-08-14", + "task_description": "You are given an integer array `nums` and a **positive** integer `k`. You can choose any **subsequence** of the array and sum all of its elements together. We define the **K-Sum** of the array as the `kth` **largest** subsequence sum that can be obtained (**not** necessarily distinct). Return _the K-Sum of the array_. A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. **Note** that the empty subsequence is considered to have a sum of `0`. **Example 1:** ``` **Input:** nums = [2,4,-2], k = 5 **Output:** 2 **Explanation:** All the possible subsequence sums that we can obtain are the following sorted in decreasing order: - 6, 4, 4, 2, 2, 0, 0, -2. The 5-Sum of the array is 2. ``` **Example 2:** ``` **Input:** nums = [1,-2,3,4,-10,12], k = 16 **Output:** 10 **Explanation:** The 16-Sum of the array is 10. ``` **Constraints:** `n == nums.length` `1 <= n <= 105` `-109 <= nums[i] <= 109` `1 <= k <= min(2000, 2n)`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,4,-2], k = 5", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,-2,3,4,-10,12], k = 16", + "output": "10 " + } + ], + "constraints": [ + "n == nums.length", + "1 <= n <= 105", + "-109 <= nums[i] <= 109", + "1 <= k <= min(2000, 2n)" + ], + "python_template": "class Solution(object):\n def kSum(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 kSum(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "kSum" + } +} \ No newline at end of file diff --git a/find-the-k-th-character-in-string-game-ii.json b/find-the-k-th-character-in-string-game-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..355953767ad702eb594598d0c3add1af2668532a --- /dev/null +++ b/find-the-k-th-character-in-string-game-ii.json @@ -0,0 +1,40 @@ +{ + "id": 3601, + "name": "find-the-k-th-character-in-string-game-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-k-th-character-in-string-game-ii/", + "date": "2024-09-22", + "task_description": "Alice and Bob are playing a game. Initially, Alice has a string `word = \"a\"`. You are given a **positive** integer `k`. You are also given an integer array `operations`, where `operations[i]` represents the **type** of the `ith` operation. Now Bob will ask Alice to perform **all** operations in sequence: If `operations[i] == 0`, **append** a copy of `word` to itself. If `operations[i] == 1`, generate a new string by **changing** each character in `word` to its **next** character in the English alphabet, and **append** it to the _original_ `word`. For example, performing the operation on `\"c\"` generates `\"cd\"` and performing the operation on `\"zb\"` generates `\"zbac\"`. Return the value of the `kth` character in `word` after performing all the operations. **Note** that the character `'z'` can be changed to `'a'` in the second type of operation. **Example 1:** **Input:** k = 5, operations = [0,0,0] **Output:** \"a\" **Explanation:** Initially, `word == \"a\"`. Alice performs the three operations as follows: Appends `\"a\"` to `\"a\"`, `word` becomes `\"aa\"`. Appends `\"aa\"` to `\"aa\"`, `word` becomes `\"aaaa\"`. Appends `\"aaaa\"` to `\"aaaa\"`, `word` becomes `\"aaaaaaaa\"`. **Example 2:** **Input:** k = 10, operations = [0,1,0,1] **Output:** \"b\" **Explanation:** Initially, `word == \"a\"`. Alice performs the four operations as follows: Appends `\"a\"` to `\"a\"`, `word` becomes `\"aa\"`. Appends `\"bb\"` to `\"aa\"`, `word` becomes `\"aabb\"`. Appends `\"aabb\"` to `\"aabb\"`, `word` becomes `\"aabbaabb\"`. Appends `\"bbccbbcc\"` to `\"aabbaabb\"`, `word` becomes `\"aabbaabbbbccbbcc\"`. **Constraints:** `1 <= k <= 1014` `1 <= operations.length <= 100` `operations[i]` is either 0 or 1. The input is generated such that `word` has **at least** `k` characters after all operations.", + "test_case": [ + { + "label": "Example 1", + "input": "k = 5, operations = [0,0,0]", + "output": "\"a\" " + }, + { + "label": "Example 2", + "input": "k = 10, operations = [0,1,0,1]", + "output": "\"b\" " + } + ], + "constraints": [ + "If operations[i] == 0, append a copy of word to itself.", + "If operations[i] == 1, generate a new string by changing each character in word to its next character in the English alphabet, and append it to the original word. For example, performing the operation on \"c\" generates \"cd\" and performing the operation on \"zb\" generates \"zbac\".", + "Appends \"a\" to \"a\", word becomes \"aa\".", + "Appends \"aa\" to \"aa\", word becomes \"aaaa\".", + "Appends \"aaaa\" to \"aaaa\", word becomes \"aaaaaaaa\".", + "Appends \"a\" to \"a\", word becomes \"aa\".", + "Appends \"bb\" to \"aa\", word becomes \"aabb\".", + "Appends \"aabb\" to \"aabb\", word becomes \"aabbaabb\".", + "Appends \"bbccbbcc\" to \"aabbaabb\", word becomes \"aabbaabbbbccbbcc\".", + "1 <= k <= 1014", + "1 <= operations.length <= 100", + "operations[i] is either 0 or 1.", + "The input is generated such that word has at least k characters after all operations." + ], + "python_template": "class Solution(object):\n def kthCharacter(self, k, operations):\n \"\"\"\n :type k: int\n :type operations: List[int]\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public char kthCharacter(long k, int[] operations) {\n \n }\n}", + "metadata": { + "func_name": "kthCharacter" + } +} \ No newline at end of file diff --git a/find-the-key-of-the-numbers.json b/find-the-key-of-the-numbers.json new file mode 100644 index 0000000000000000000000000000000000000000..aaa9f7d592963450d05f45c62f6f8aed527a48e3 --- /dev/null +++ b/find-the-key-of-the-numbers.json @@ -0,0 +1,39 @@ +{ + "id": 3568, + "name": "find-the-key-of-the-numbers", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-key-of-the-numbers/", + "date": "2024-08-17", + "task_description": "You are given three **positive** integers `num1`, `num2`, and `num3`. The `key` of `num1`, `num2`, and `num3` is defined as a four-digit number such that: Initially, if any number has **less than** four digits, it is padded with **leading zeros**. The `ith` digit (`1 <= i <= 4`) of the `key` is generated by taking the **smallest** digit among the `ith` digits of `num1`, `num2`, and `num3`. Return the `key` of the three numbers **without** leading zeros (_if any_). **Example 1:** **Input:** num1 = 1, num2 = 10, num3 = 1000 **Output:** 0 **Explanation:** On padding, `num1` becomes `\"0001\"`, `num2` becomes `\"0010\"`, and `num3` remains `\"1000\"`. The `1st` digit of the `key` is `min(0, 0, 1)`. The `2nd` digit of the `key` is `min(0, 0, 0)`. The `3rd` digit of the `key` is `min(0, 1, 0)`. The `4th` digit of the `key` is `min(1, 0, 0)`. Hence, the `key` is `\"0000\"`, i.e. 0. **Example 2:** **Input:** num1 = 987, num2 = 879, num3 = 798 **Output:** 777 **Example 3:** **Input:** num1 = 1, num2 = 2, num3 = 3 **Output:** 1 **Constraints:** `1 <= num1, num2, num3 <= 9999`", + "test_case": [ + { + "label": "Example 1", + "input": "num1 = 1, num2 = 10, num3 = 1000", + "output": "0 " + }, + { + "label": "Example 2", + "input": "num1 = 987, num2 = 879, num3 = 798", + "output": "77" + }, + { + "label": "Example 3", + "input": "num1 = 1, num2 = 2, num3 = 3", + "output": "" + } + ], + "constraints": [ + "Initially, if any number has less than four digits, it is padded with leading zeros.", + "The ith digit (1 <= i <= 4) of the key is generated by taking the smallest digit among the ith digits of num1, num2, and num3.", + "The 1st digit of the key is min(0, 0, 1).", + "The 2nd digit of the key is min(0, 0, 0).", + "The 3rd digit of the key is min(0, 1, 0).", + "The 4th digit of the key is min(1, 0, 0).", + "1 <= num1, num2, num3 <= 9999" + ], + "python_template": "class Solution(object):\n def generateKey(self, num1, num2, num3):\n \"\"\"\n :type num1: int\n :type num2: int\n :type num3: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int generateKey(int num1, int num2, int num3) {\n \n }\n}", + "metadata": { + "func_name": "generateKey" + } +} \ No newline at end of file diff --git a/find-the-largest-almost-missing-integer.json b/find-the-largest-almost-missing-integer.json new file mode 100644 index 0000000000000000000000000000000000000000..89d3be623f5eec54a8bb5146f46b8d7362af8864 --- /dev/null +++ b/find-the-largest-almost-missing-integer.json @@ -0,0 +1,45 @@ +{ + "id": 3705, + "name": "find-the-largest-almost-missing-integer", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-largest-almost-missing-integer/", + "date": "2025-02-23", + "task_description": "You are given an integer array `nums` and an integer `k`. An integer `x` is **almost missing** from `nums` if `x` appears in _exactly_ one subarray of size `k` within `nums`. Return the largest **almost missing** integer from `nums`. If no such integer exists, return `-1`. A **subarray** is a contiguous sequence of elements within an array. **Example 1:** **Input:** nums = [3,9,2,1,7], k = 3 **Output:** 7 **Explanation:** 1 appears in 2 subarrays of size 3: `[9, 2, 1]` and `[2, 1, 7]`. 2 appears in 3 subarrays of size 3: `[3, 9, 2]`, `[9, 2, 1]`, `[2, 1, 7]`. 3 appears in 1 subarray of size 3: `[3, 9, 2]`. 7 appears in 1 subarray of size 3: `[2, 1, 7]`. 9 appears in 2 subarrays of size 3: `[3, 9, 2]`, and `[9, 2, 1]`. We return 7 since it is the largest integer that appears in exactly one subarray of size `k`. **Example 2:** **Input:** nums = [3,9,7,2,1,7], k = 4 **Output:** 3 **Explanation:** 1 appears in 2 subarrays of size 4: `[9, 7, 2, 1]`, `[7, 2, 1, 7]`. 2 appears in 3 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`, `[7, 2, 1, 7]`. 3 appears in 1 subarray of size 4: `[3, 9, 7, 2]`. 7 appears in 3 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`, `[7, 2, 1, 7]`. 9 appears in 2 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`. We return 3 since it is the largest and only integer that appears in exactly one subarray of size `k`. **Example 3:** **Input:** nums = [0,0], k = 1 **Output:** -1 **Explanation:** There is no integer that appears in only one subarray of size 1. **Constraints:** `1 <= nums.length <= 50` `0 <= nums[i] <= 50` `1 <= k <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,9,2,1,7], k = 3", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [3,9,7,2,1,7], k = 4", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [0,0], k = 1", + "output": "-1 " + } + ], + "constraints": [ + "1 appears in 2 subarrays of size 3: [9, 2, 1] and [2, 1, 7].", + "2 appears in 3 subarrays of size 3: [3, 9, 2], [9, 2, 1], [2, 1, 7].", + "3 appears in 1 subarray of size 3: [3, 9, 2].", + "7 appears in 1 subarray of size 3: [2, 1, 7].", + "9 appears in 2 subarrays of size 3: [3, 9, 2], and [9, 2, 1].", + "1 appears in 2 subarrays of size 4: [9, 7, 2, 1], [7, 2, 1, 7].", + "2 appears in 3 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7].", + "3 appears in 1 subarray of size 4: [3, 9, 7, 2].", + "7 appears in 3 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7].", + "9 appears in 2 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1].", + "1 <= nums.length <= 50", + "0 <= nums[i] <= 50", + "1 <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def largestInteger(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 largestInteger(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "largestInteger" + } +} \ No newline at end of file diff --git a/find-the-largest-area-of-square-inside-two-rectangles.json b/find-the-largest-area-of-square-inside-two-rectangles.json new file mode 100644 index 0000000000000000000000000000000000000000..da3756fa07b4f9e392f2c32c0ef20d68b17a39fc --- /dev/null +++ b/find-the-largest-area-of-square-inside-two-rectangles.json @@ -0,0 +1,44 @@ +{ + "id": 3325, + "name": "find-the-largest-area-of-square-inside-two-rectangles", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles/", + "date": "2024-02-18", + "task_description": "There exist `n` rectangles in a 2D plane with edges parallel to the x and y axis. You are given two 2D integer arrays `bottomLeft` and `topRight` where `bottomLeft[i] = [a_i, b_i]` and `topRight[i] = [c_i, d_i]` represent the **bottom-left** and **top-right** coordinates of the `ith` rectangle, respectively. You need to find the **maximum** area of a **square** that can fit inside the intersecting region of at least two rectangles. Return `0` if such a square does not exist. **Example 1:** **Input:** bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[6,6]] **Output:** 1 **Explanation:** A square with side length 1 can fit inside either the intersecting region of rectangles 0 and 1 or the intersecting region of rectangles 1 and 2. Hence the maximum area is 1. It can be shown that a square with a greater side length can not fit inside any intersecting region of two rectangles. **Example 2:** **Input:** bottomLeft = [[1,1],[1,3],[1,5]], topRight = [[5,5],[5,7],[5,9]] **Output:** 4 **Explanation:** A square with side length 2 can fit inside either the intersecting region of rectangles 0 and 1 or the intersecting region of rectangles 1 and 2. Hence the maximum area is `2 * 2 = 4`. It can be shown that a square with a greater side length can not fit inside any intersecting region of two rectangles. **Example 3:** ` ` **Input:** bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[3,3],[4,4],[3,4]] **Output:** 1 **Explanation:** A square with side length 1 can fit inside the intersecting region of any two rectangles. Also, no larger square can, so the maximum area is 1. Note that the region can be formed by the intersection of more than 2 rectangles. **Example 4:** ` ` **Input: **bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[2,2],[4,4],[4,2]] **Output:** 0 **Explanation:** No pair of rectangles intersect, hence, the answer is 0. **Constraints:** `n == bottomLeft.length == topRight.length` `2 <= n <= 103` `bottomLeft[i].length == topRight[i].length == 2` `1 <= bottomLeft[i][0], bottomLeft[i][1] <= 107` `1 <= topRight[i][0], topRight[i][1] <= 107` `bottomLeft[i][0] < topRight[i][0]` `bottomLeft[i][1] < topRight[i][1]`", + "test_case": [ + { + "label": "Example 1", + "input": "bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[6,6]]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "bottomLeft = [[1,1],[1,3],[1,5]], topRight = [[5,5],[5,7],[5,9]]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[3,3],[4,4],[3,4]]", + "output": "1 " + }, + { + "label": "Example 4", + "input": "bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[2,2],[4,4],[4,2]]", + "output": "0 " + } + ], + "constraints": [ + "n == bottomLeft.length == topRight.length", + "2 <= n <= 103", + "bottomLeft[i].length == topRight[i].length == 2", + "1 <= bottomLeft[i][0], bottomLeft[i][1] <= 107", + "1 <= topRight[i][0], topRight[i][1] <= 107", + "bottomLeft[i][0] < topRight[i][0]", + "bottomLeft[i][1] < topRight[i][1]" + ], + "python_template": "class Solution(object):\n def largestSquareArea(self, bottomLeft, topRight):\n \"\"\"\n :type bottomLeft: List[List[int]]\n :type topRight: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {\n \n }\n}", + "metadata": { + "func_name": "largestSquareArea" + } +} \ No newline at end of file diff --git a/find-the-length-of-the-longest-common-prefix.json b/find-the-length-of-the-longest-common-prefix.json new file mode 100644 index 0000000000000000000000000000000000000000..c7696004d5f016ec262e30d331fe0eab65741a13 --- /dev/null +++ b/find-the-length-of-the-longest-common-prefix.json @@ -0,0 +1,29 @@ +{ + "id": 3329, + "name": "find-the-length-of-the-longest-common-prefix", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix/", + "date": "2024-02-11", + "task_description": "You are given two arrays with **positive** integers `arr1` and `arr2`. A **prefix** of a positive integer is an integer formed by one or more of its digits, starting from its **leftmost** digit. For example, `123` is a prefix of the integer `12345`, while `234` is **not**. A **common prefix** of two integers `a` and `b` is an integer `c`, such that `c` is a prefix of both `a` and `b`. For example, `5655359` and `56554` have common prefixes `565` and `5655` while `1223` and `43456` **do not** have a common prefix. You need to find the length of the **longest common prefix** between all pairs of integers `(x, y)` such that `x` belongs to `arr1` and `y` belongs to `arr2`. Return _the length of the **longest** common prefix among all pairs_._ If no common prefix exists among them_, _return_ `0`. **Example 1:** ``` **Input:** arr1 = [1,10,100], arr2 = [1000] **Output:** 3 **Explanation:** There are 3 pairs (arr1[i], arr2[j]): - The longest common prefix of (1, 1000) is 1. - The longest common prefix of (10, 1000) is 10. - The longest common prefix of (100, 1000) is 100. The longest common prefix is 100 with a length of 3. ``` **Example 2:** ``` **Input:** arr1 = [1,2,3], arr2 = [4,4,4] **Output:** 0 **Explanation:** There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0. Note that common prefixes between elements of the same array do not count. ``` **Constraints:** `1 <= arr1.length, arr2.length <= 5 * 104` `1 <= arr1[i], arr2[i] <= 108`", + "test_case": [ + { + "label": "Example 1", + "input": "arr1 = [1,10,100], arr2 = [1000]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "arr1 = [1,2,3], arr2 = [4,4,4]", + "output": "0 " + } + ], + "constraints": [ + "1 <= arr1.length, arr2.length <= 5 * 104", + "1 <= arr1[i], arr2[i] <= 108" + ], + "python_template": "class Solution(object):\n def longestCommonPrefix(self, arr1, arr2):\n \"\"\"\n :type arr1: List[int]\n :type arr2: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestCommonPrefix(int[] arr1, int[] arr2) {\n \n }\n}", + "metadata": { + "func_name": "longestCommonPrefix" + } +} \ No newline at end of file diff --git a/find-the-lexicographically-largest-string-from-the-box-i.json b/find-the-lexicographically-largest-string-from-the-box-i.json new file mode 100644 index 0000000000000000000000000000000000000000..87e7859a59ee9659b73fb2c9720cc512e6dc5083 --- /dev/null +++ b/find-the-lexicographically-largest-string-from-the-box-i.json @@ -0,0 +1,35 @@ +{ + "id": 3683, + "name": "find-the-lexicographically-largest-string-from-the-box-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-lexicographically-largest-string-from-the-box-i/", + "date": "2024-12-22", + "task_description": "You are given a string `word`, and an integer `numFriends`. Alice is organizing a game for her `numFriends` friends. There are multiple rounds in the game, where in each round: `word` is split into `numFriends` **non-empty** strings, such that no previous round has had the **exact** same split. All the split words are put into a box. Find the lexicographically largest string from the box after all the rounds are finished. **Example 1:** **Input:** word = \"dbca\", numFriends = 2 **Output:** \"dbc\" **Explanation:** All possible splits are: `\"d\"` and `\"bca\"`. `\"db\"` and `\"ca\"`. `\"dbc\"` and `\"a\"`. **Example 2:** **Input:** word = \"gggg\", numFriends = 4 **Output:** \"g\" **Explanation:** The only possible split is: `\"g\"`, `\"g\"`, `\"g\"`, and `\"g\"`. **Constraints:** `1 <= word.length <= 5 * 103` `word` consists only of lowercase English letters. `1 <= numFriends <= word.length`", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"dbca\", numFriends = 2", + "output": "\"dbc\" " + }, + { + "label": "Example 2", + "input": "word = \"gggg\", numFriends = 4", + "output": "\"g\" " + } + ], + "constraints": [ + "word is split into numFriends non-empty strings, such that no previous round has had the exact same split.", + "All the split words are put into a box.", + "\"d\" and \"bca\".", + "\"db\" and \"ca\".", + "\"dbc\" and \"a\".", + "1 <= word.length <= 5 * 103", + "word consists only of lowercase English letters.", + "1 <= numFriends <= word.length" + ], + "python_template": "class Solution(object):\n def answerString(self, word, numFriends):\n \"\"\"\n :type word: str\n :type numFriends: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String answerString(String word, int numFriends) {\n \n }\n}", + "metadata": { + "func_name": "answerString" + } +} \ No newline at end of file diff --git a/find-the-longest-balanced-substring-of-a-binary-string.json b/find-the-longest-balanced-substring-of-a-binary-string.json new file mode 100644 index 0000000000000000000000000000000000000000..ecef2e7337fcf6b0ed6a6b6d90d688794a9daa0b --- /dev/null +++ b/find-the-longest-balanced-substring-of-a-binary-string.json @@ -0,0 +1,34 @@ +{ + "id": 2723, + "name": "find-the-longest-balanced-substring-of-a-binary-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/", + "date": "2023-03-26", + "task_description": "You are given a binary string `s` consisting only of zeroes and ones. A substring of `s` is considered balanced if** all zeroes are before ones** and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring. Return _the length of the longest balanced substring of _`s`. A substring is a contiguous sequence of characters within a string. **Example 1:** ``` **Input:** s = \"01000111\" **Output:** 6 **Explanation:** The longest balanced substring is \"000111\", which has length 6. ``` **Example 2:** ``` **Input:** s = \"00111\" **Output:** 4 **Explanation:** The longest balanced substring is \"0011\", which has length 4. ``` **Example 3:** ``` **Input:** s = \"111\" **Output:** 0 **Explanation:** There is no balanced substring except the empty substring, so the answer is 0. ``` **Constraints:** `1 <= s.length <= 50` `'0' <= s[i] <= '1'`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"01000111\"", + "output": "6 " + }, + { + "label": "Example 2", + "input": "s = \"00111\"", + "output": "4 " + }, + { + "label": "Example 3", + "input": "s = \"111\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= s.length <= 50", + "'0' <= s[i] <= '1'" + ], + "python_template": "class Solution(object):\n def findTheLongestBalancedSubstring(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findTheLongestBalancedSubstring(String s) {\n \n }\n}", + "metadata": { + "func_name": "findTheLongestBalancedSubstring" + } +} \ No newline at end of file diff --git a/find-the-longest-equal-subarray.json b/find-the-longest-equal-subarray.json new file mode 100644 index 0000000000000000000000000000000000000000..f13ea33218b75827792e6d5249b1b118936a8479 --- /dev/null +++ b/find-the-longest-equal-subarray.json @@ -0,0 +1,30 @@ +{ + "id": 2832, + "name": "find-the-longest-equal-subarray", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-longest-equal-subarray/", + "date": "2023-08-13", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `k`. A subarray is called **equal** if all of its elements are equal. Note that the empty subarray is an **equal** subarray. Return _the length of the **longest** possible equal subarray after deleting **at most** _`k`_ elements from _`nums`. A subarray is a contiguous, possibly empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,3,2,3,1,3], k = 3 **Output:** 3 **Explanation:** It's optimal to delete the elements at index 2 and index 4. After deleting them, nums becomes equal to [1, 3, 3, 3]. The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3. It can be proven that no longer equal subarrays can be created. ``` **Example 2:** ``` **Input:** nums = [1,1,2,2,1,1], k = 2 **Output:** 4 **Explanation:** It's optimal to delete the elements at index 2 and index 3. After deleting them, nums becomes equal to [1, 1, 1, 1]. The array itself is an equal subarray, so the answer is 4. It can be proven that no longer equal subarrays can be created. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= nums.length` `0 <= k <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,2,3,1,3], k = 3", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,2,2,1,1], k = 2", + "output": "4 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= nums.length", + "0 <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def longestEqualSubarray(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 longestEqualSubarray(List nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "longestEqualSubarray" + } +} \ No newline at end of file diff --git a/find-the-longest-semi-repetitive-substring.json b/find-the-longest-semi-repetitive-substring.json new file mode 100644 index 0000000000000000000000000000000000000000..4b49ca5b6195c647b0bfbcfb403a0bea87fdaa02 --- /dev/null +++ b/find-the-longest-semi-repetitive-substring.json @@ -0,0 +1,34 @@ +{ + "id": 2786, + "name": "find-the-longest-semi-repetitive-substring", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/", + "date": "2023-05-27", + "task_description": "You are given a digit string `s` that consists of digits from 0 to 9. A string is called **semi-repetitive** if there is **at most** one adjacent pair of the same digit. For example, `\"0010\"`, `\"002020\"`, `\"0123\"`, `\"2002\"`, and `\"54944\"` are semi-repetitive while the following are not: `\"00101022\"` (adjacent same digit pairs are 00 and 22), and `\"1101234883\"` (adjacent same digit pairs are 11 and 88). Return the length of the **longest semi-repetitive substring** of `s`. **Example 1:** **Input:** s = \"52233\" **Output:** 4 **Explanation:** The longest semi-repetitive substring is \"5223\". Picking the whole string \"52233\" has two adjacent same digit pairs 22 and 33, but at most one is allowed. **Example 2:** **Input:** s = \"5494\" **Output:** 4 **Explanation:** `s` is a semi-repetitive string. **Example 3:** **Input:** s = \"1111111\" **Output:** 2 **Explanation:** The longest semi-repetitive substring is \"11\". Picking the substring \"111\" has two adjacent same digit pairs, but at most one is allowed. **Constraints:** `1 <= s.length <= 50` `'0' <= s[i] <= '9'`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"52233\"", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"5494\"", + "output": "4 " + }, + { + "label": "Example 3", + "input": "s = \"1111111\"", + "output": "2 " + } + ], + "constraints": [ + "1 <= s.length <= 50", + "'0' <= s[i] <= '9'" + ], + "python_template": "class Solution(object):\n def longestSemiRepetitiveSubstring(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestSemiRepetitiveSubstring(String s) {\n \n }\n}", + "metadata": { + "func_name": "longestSemiRepetitiveSubstring" + } +} \ No newline at end of file diff --git a/find-the-losers-of-the-circular-game.json b/find-the-losers-of-the-circular-game.json new file mode 100644 index 0000000000000000000000000000000000000000..fbbefc2e08df1e98beea0aa5a3eedcbd39d77bc8 --- /dev/null +++ b/find-the-losers-of-the-circular-game.json @@ -0,0 +1,31 @@ +{ + "id": 2791, + "name": "find-the-losers-of-the-circular-game", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-losers-of-the-circular-game/", + "date": "2023-05-07", + "task_description": "There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend. The rules of the game are as follows: `1st` friend receives the ball. After that, `1st` friend passes it to the friend who is `k` steps away from them in the **clockwise** direction. After that, the friend who receives the ball should pass it to the friend who is `2 * k` steps away from them in the **clockwise** direction. After that, the friend who receives the ball should pass it to the friend who is `3 * k` steps away from them in the **clockwise** direction, and so on and so forth. In other words, on the `ith` turn, the friend holding the ball should pass it to the friend who is `i * k` steps away from them in the **clockwise** direction. The game is finished when some friend receives the ball for the second time. The **losers** of the game are friends who did not receive the ball in the entire game. Given the number of friends, `n`, and an integer `k`, return _the array answer, which contains the losers of the game in the **ascending** order_. **Example 1:** ``` **Input:** n = 5, k = 2 **Output:** [4,5] **Explanation:** The game goes as follows: 1) Start at 1st friend and pass the ball to the friend who is 2 steps away from them - 3rd friend. 2) 3rd friend passes the ball to the friend who is 4 steps away from them - 2nd friend. 3) 2nd friend passes the ball to the friend who is 6 steps away from them - 3rd friend. 4) The game ends as 3rd friend receives the ball for the second time. ``` **Example 2:** ``` **Input:** n = 4, k = 4 **Output:** [2,3,4] **Explanation:** The game goes as follows: 1) Start at the 1st friend and pass the ball to the friend who is 4 steps away from them - 1st friend. 2) The game ends as 1st friend receives the ball for the second time. ``` **Constraints:** `1 <= k <= n <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, k = 2", + "output": "[4,5] " + }, + { + "label": "Example 2", + "input": "n = 4, k = 4", + "output": "[2,3,4] " + } + ], + "constraints": [ + "After that, 1st friend passes it to the friend who is k steps away from them in the clockwise direction.", + "After that, the friend who receives the ball should pass it to the friend who is 2 * k steps away from them in the clockwise direction.", + "After that, the friend who receives the ball should pass it to the friend who is 3 * k steps away from them in the clockwise direction, and so on and so forth.", + "1 <= k <= n <= 50" + ], + "python_template": "class Solution(object):\n def circularGameLosers(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[] circularGameLosers(int n, int k) {\n \n }\n}", + "metadata": { + "func_name": "circularGameLosers" + } +} \ No newline at end of file diff --git a/find-the-maximum-achievable-number.json b/find-the-maximum-achievable-number.json new file mode 100644 index 0000000000000000000000000000000000000000..df4a2f69c0e9f37cb38c610bd3ceac20c3324cb1 --- /dev/null +++ b/find-the-maximum-achievable-number.json @@ -0,0 +1,31 @@ +{ + "id": 2812, + "name": "find-the-maximum-achievable-number", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-maximum-achievable-number/", + "date": "2023-07-02", + "task_description": "Given two integers, `num` and `t`. A **number **`x`** **is** achievable** if it can become equal to `num` after applying the following operation **at most** `t` times: Increase or decrease `x` by `1`, and _simultaneously_ increase or decrease `num` by `1`. Return the **maximum **possible value of `x`. **Example 1:** **Input:** num = 4, t = 1 **Output:** 6 **Explanation:** Apply the following operation once to make the maximum achievable number equal to `num`: Decrease the maximum achievable number by 1, and increase `num` by 1. **Example 2:** **Input:** num = 3, t = 2 **Output:** 7 **Explanation:** Apply the following operation twice to make the maximum achievable number equal to `num`: Decrease the maximum achievable number by 1, and increase `num` by 1. **Constraints:** `1 <= num, t <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "num = 4, t = 1", + "output": "6 " + }, + { + "label": "Example 2", + "input": "num = 3, t = 2", + "output": "7 " + } + ], + "constraints": [ + "Increase or decrease x by 1, and simultaneously increase or decrease num by 1.", + "Decrease the maximum achievable number by 1, and increase num by 1.", + "Decrease the maximum achievable number by 1, and increase num by 1.", + "1 <= num, t <= 50" + ], + "python_template": "class Solution(object):\n def theMaximumAchievableX(self, num, t):\n \"\"\"\n :type num: int\n :type t: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int theMaximumAchievableX(int num, int t) {\n \n }\n}", + "metadata": { + "func_name": "theMaximumAchievableX" + } +} \ No newline at end of file diff --git a/find-the-maximum-divisibility-score.json b/find-the-maximum-divisibility-score.json new file mode 100644 index 0000000000000000000000000000000000000000..57248280512bfabdcc52b9cfe82426b9a003b3cb --- /dev/null +++ b/find-the-maximum-divisibility-score.json @@ -0,0 +1,34 @@ +{ + "id": 2694, + "name": "find-the-maximum-divisibility-score", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-maximum-divisibility-score/", + "date": "2023-04-09", + "task_description": "You are given two integer arrays `nums` and `divisors`. The **divisibility score** of `divisors[i]` is the number of indices `j` such that `nums[j]` is divisible by `divisors[i]`. Return the integer `divisors[i]` with the **maximum** divisibility score. If multiple integers have the maximum score, return the smallest one. **Example 1:** **Input:** nums = [2,9,15,50], divisors = [5,3,7,2] **Output:** 2 **Explanation:** The divisibility score of `divisors[0]` is 2 since `nums[2]` and `nums[3]` are divisible by 5. The divisibility score of `divisors[1]` is 2 since `nums[1]` and `nums[2]` are divisible by 3. The divisibility score of `divisors[2]` is 0 since none of the numbers in `nums` is divisible by 7. The divisibility score of `divisors[3]` is 2 since `nums[0]` and `nums[3]` are divisible by 2. As `divisors[0]`, `divisors[1]`, and `divisors[3]` have the same divisibility score, we return the smaller one which is `divisors[3]`. **Example 2:** **Input:** nums = [4,7,9,3,9], divisors = [5,2,3] **Output:** 3 **Explanation:** The divisibility score of `divisors[0]` is 0 since none of numbers in `nums` is divisible by 5. The divisibility score of `divisors[1]` is 1 since only `nums[0]` is divisible by 2. The divisibility score of `divisors[2]` is 3 since `nums[2]`, `nums[3]` and `nums[4]` are divisible by 3. **Example 3:** **Input:** nums = [20,14,21,10], divisors = [10,16,20] **Output:** 10 **Explanation:** The divisibility score of `divisors[0]` is 2 since `nums[0]` and `nums[3]` are divisible by 10. The divisibility score of `divisors[1]` is 0 since none of the numbers in `nums` is divisible by 16. The divisibility score of `divisors[2]` is 1 since `nums[0]` is divisible by 20. **Constraints:** `1 <= nums.length, divisors.length <= 1000` `1 <= nums[i], divisors[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,9,15,50], divisors = [5,3,7,2]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,7,9,3,9], divisors = [5,2,3]", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [20,14,21,10], divisors = [10,16,20]", + "output": "10 " + } + ], + "constraints": [ + "1 <= nums.length, divisors.length <= 1000", + "1 <= nums[i], divisors[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxDivScore(self, nums, divisors):\n \"\"\"\n :type nums: List[int]\n :type divisors: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxDivScore(int[] nums, int[] divisors) {\n \n }\n}", + "metadata": { + "func_name": "maxDivScore" + } +} \ No newline at end of file diff --git a/find-the-maximum-length-of-a-good-subsequence-i.json b/find-the-maximum-length-of-a-good-subsequence-i.json new file mode 100644 index 0000000000000000000000000000000000000000..3d6dd72987ade2df7c1a8c0fb6c27a858077268a --- /dev/null +++ b/find-the-maximum-length-of-a-good-subsequence-i.json @@ -0,0 +1,30 @@ +{ + "id": 3456, + "name": "find-the-maximum-length-of-a-good-subsequence-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-maximum-length-of-a-good-subsequence-i/", + "date": "2024-05-25", + "task_description": "You are given an integer array `nums` and a **non-negative** integer `k`. A sequence of integers `seq` is called **good** if there are **at most** `k` indices `i` in the range `[0, seq.length - 2]` such that `seq[i] != seq[i + 1]`. Return the **maximum** possible length of a **good** subsequence of `nums`. **Example 1:** **Input:** nums = [1,2,1,1,3], k = 2 **Output:** 4 **Explanation:** The maximum length subsequence is `[1,2,1,1,3]`. **Example 2:** **Input:** nums = [1,2,3,4,5,1], k = 0 **Output:** 2 **Explanation:** The maximum length subsequence is `[1,2,3,4,5,1]`. **Constraints:** `1 <= nums.length <= 500` `1 <= nums[i] <= 109` `0 <= k <= min(nums.length, 25)`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1,1,3], k = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4,5,1], k = 0", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 500", + "1 <= nums[i] <= 109", + "0 <= k <= min(nums.length, 25)" + ], + "python_template": "class Solution(object):\n def maximumLength(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 maximumLength(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumLength" + } +} \ No newline at end of file diff --git a/find-the-maximum-length-of-a-good-subsequence-ii.json b/find-the-maximum-length-of-a-good-subsequence-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..bf5d314c56a431f81e7939aca276feb4399f4f02 --- /dev/null +++ b/find-the-maximum-length-of-a-good-subsequence-ii.json @@ -0,0 +1,30 @@ +{ + "id": 3452, + "name": "find-the-maximum-length-of-a-good-subsequence-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-maximum-length-of-a-good-subsequence-ii/", + "date": "2024-05-25", + "task_description": "You are given an integer array `nums` and a **non-negative** integer `k`. A sequence of integers `seq` is called **good** if there are **at most** `k` indices `i` in the range `[0, seq.length - 2]` such that `seq[i] != seq[i + 1]`. Return the **maximum** possible length of a **good** subsequence of `nums`. **Example 1:** **Input:** nums = [1,2,1,1,3], k = 2 **Output:** 4 **Explanation:** The maximum length subsequence is `[1,2,1,1,3]`. **Example 2:** **Input:** nums = [1,2,3,4,5,1], k = 0 **Output:** 2 **Explanation:** The maximum length subsequence is `[1,2,3,4,5,1]`. **Constraints:** `1 <= nums.length <= 5 * 103` `1 <= nums[i] <= 109` `0 <= k <= min(50, nums.length)`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1,1,3], k = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4,5,1], k = 0", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 5 * 103", + "1 <= nums[i] <= 109", + "0 <= k <= min(50, nums.length)" + ], + "python_template": "class Solution(object):\n def maximumLength(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 maximumLength(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumLength" + } +} \ No newline at end of file diff --git a/find-the-maximum-length-of-valid-subsequence-i.json b/find-the-maximum-length-of-valid-subsequence-i.json new file mode 100644 index 0000000000000000000000000000000000000000..0ff6e2058cf78fbebe2afeaad8e39fc5fbc63b0c --- /dev/null +++ b/find-the-maximum-length-of-valid-subsequence-i.json @@ -0,0 +1,35 @@ +{ + "id": 3490, + "name": "find-the-maximum-length-of-valid-subsequence-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-i/", + "date": "2024-06-23", + "task_description": "You are given an integer array `nums`. A subsequence `sub` of `nums` with length `x` is called **valid** if it satisfies: `(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2.` Return the length of the **longest** **valid** subsequence of `nums`. A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. **Example 1:** **Input:** nums = [1,2,3,4] **Output:** 4 **Explanation:** The longest valid subsequence is `[1, 2, 3, 4]`. **Example 2:** **Input:** nums = [1,2,1,1,2,1,2] **Output:** 6 **Explanation:** The longest valid subsequence is `[1, 2, 1, 2, 1, 2]`. **Example 3:** **Input:** nums = [1,3] **Output:** 2 **Explanation:** The longest valid subsequence is `[1, 3]`. **Constraints:** `2 <= nums.length <= 2 * 105` `1 <= nums[i] <= 107`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,1,1,2,1,2]", + "output": "6 " + }, + { + "label": "Example 3", + "input": "nums = [1,3]", + "output": "2 " + } + ], + "constraints": [ + "(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2.", + "2 <= nums.length <= 2 * 105", + "1 <= nums[i] <= 107" + ], + "python_template": "class Solution(object):\n def maximumLength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumLength(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumLength" + } +} \ No newline at end of file diff --git a/find-the-maximum-length-of-valid-subsequence-ii.json b/find-the-maximum-length-of-valid-subsequence-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..a1ef08d1f138bfe6a238faa88a6450898d6b4402 --- /dev/null +++ b/find-the-maximum-length-of-valid-subsequence-ii.json @@ -0,0 +1,31 @@ +{ + "id": 3491, + "name": "find-the-maximum-length-of-valid-subsequence-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/", + "date": "2024-06-23", + "task_description": "You are given an integer array `nums` and a **positive** integer `k`. A subsequence `sub` of `nums` with length `x` is called **valid** if it satisfies: `(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.` Return the length of the **longest** **valid** subsequence of `nums`. **Example 1:** **Input:** nums = [1,2,3,4,5], k = 2 **Output:** 5 **Explanation:** The longest valid subsequence is `[1, 2, 3, 4, 5]`. **Example 2:** **Input:** nums = [1,4,2,3,1,4], k = 3 **Output:** 4 **Explanation:** The longest valid subsequence is `[1, 4, 1, 4]`. **Constraints:** `2 <= nums.length <= 103` `1 <= nums[i] <= 107` `1 <= k <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5], k = 2", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [1,4,2,3,1,4], k = 3", + "output": "4 " + } + ], + "constraints": [ + "(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.", + "2 <= nums.length <= 103", + "1 <= nums[i] <= 107", + "1 <= k <= 103" + ], + "python_template": "class Solution(object):\n def maximumLength(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 maximumLength(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumLength" + } +} \ No newline at end of file diff --git a/find-the-maximum-number-of-elements-in-subset.json b/find-the-maximum-number-of-elements-in-subset.json new file mode 100644 index 0000000000000000000000000000000000000000..060c383042b2265fc1ff2e7f03ad8a9e22e256ba --- /dev/null +++ b/find-the-maximum-number-of-elements-in-subset.json @@ -0,0 +1,30 @@ +{ + "id": 3299, + "name": "find-the-maximum-number-of-elements-in-subset", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-maximum-number-of-elements-in-subset/", + "date": "2024-01-21", + "task_description": "You are given an array of **positive** integers `nums`. You need to select a subset of `nums` which satisfies the following condition: You can place the selected elements in a **0-indexed** array such that it follows the pattern: `[x, x2, x4, ..., xk/2, xk, xk/2, ..., x4, x2, x]` (**Note** that `k` can be be any **non-negative** power of `2`). For example, `[2, 4, 16, 4, 2]` and `[3, 9, 3]` follow the pattern while `[2, 4, 8, 4, 2]` does not. Return _the **maximum** number of elements in a subset that satisfies these conditions._ **Example 1:** ``` **Input:** nums = [5,4,1,2,2] **Output:** 3 **Explanation:** We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 22 == 4. Hence the answer is 3. ``` **Example 2:** ``` **Input:** nums = [1,3,2,4] **Output:** 1 **Explanation:** We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer. ``` **Constraints:** `2 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,4,1,2,2]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,2,4]", + "output": "1 " + } + ], + "constraints": [ + "You can place the selected elements in a 0-indexed array such that it follows the pattern: [x, x2, x4, ..., xk/2, xk, xk/2, ..., x4, x2, x] (Note that k can be be any non-negative power of 2). For example, [2, 4, 16, 4, 2] and [3, 9, 3] follow the pattern while [2, 4, 8, 4, 2] does not.", + "2 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumLength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumLength(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumLength" + } +} \ No newline at end of file diff --git a/find-the-maximum-number-of-marked-indices.json b/find-the-maximum-number-of-marked-indices.json new file mode 100644 index 0000000000000000000000000000000000000000..18db1756924877e9c0cfde31dec1beb0d825f3d9 --- /dev/null +++ b/find-the-maximum-number-of-marked-indices.json @@ -0,0 +1,35 @@ +{ + "id": 2712, + "name": "find-the-maximum-number-of-marked-indices", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-maximum-number-of-marked-indices/", + "date": "2023-02-19", + "task_description": "You are given a **0-indexed** integer array `nums`. Initially, all of the indices are unmarked. You are allowed to make this operation any number of times: Pick two **different unmarked** indices `i` and `j` such that `2 * nums[i] <= nums[j]`, then mark `i` and `j`. Return _the maximum possible number of marked indices in `nums` using the above operation any number of times_. **Example 1:** ``` **Input:** nums = [3,5,2,4] **Output:** 2 **Explanation: **In the first operation: pick i = 2 and j = 1, the operation is allowed because 2 * nums[2] <= nums[1]. Then mark index 2 and 1. It can be shown that there's no other valid operation so the answer is 2. ``` **Example 2:** ``` **Input:** nums = [9,2,5,4] **Output:** 4 **Explanation: **In the first operation: pick i = 3 and j = 0, the operation is allowed because 2 * nums[3] <= nums[0]. Then mark index 3 and 0. In the second operation: pick i = 1 and j = 2, the operation is allowed because 2 * nums[1] <= nums[2]. Then mark index 1 and 2. Since there is no other operation, the answer is 4. ``` **Example 3:** ``` **Input:** nums = [7,6,8] **Output:** 0 **Explanation: **There is no valid operation to do, so the answer is 0. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,5,2,4]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [9,2,5,4]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [7,6,8]", + "output": "0 " + } + ], + "constraints": [ + "Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark i and j.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxNumOfMarkedIndices(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxNumOfMarkedIndices(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxNumOfMarkedIndices" + } +} \ No newline at end of file diff --git a/find-the-maximum-sequence-value-of-array.json b/find-the-maximum-sequence-value-of-array.json new file mode 100644 index 0000000000000000000000000000000000000000..c6d6186be904ca5eec7e3be2c8bab2afd68364e0 --- /dev/null +++ b/find-the-maximum-sequence-value-of-array.json @@ -0,0 +1,31 @@ +{ + "id": 3575, + "name": "find-the-maximum-sequence-value-of-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-maximum-sequence-value-of-array/", + "date": "2024-08-31", + "task_description": "You are given an integer array `nums` and a **positive** integer `k`. The **value** of a sequence `seq` of size `2 * x` is defined as: `(seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1])`. Return the **maximum** **value** of any subsequence of `nums` having size `2 * k`. **Example 1:** **Input:** nums = [2,6,7], k = 1 **Output:** 5 **Explanation:** The subsequence `[2, 7]` has the maximum value of `2 XOR 7 = 5`. **Example 2:** **Input:** nums = [4,2,5,6,7], k = 2 **Output:** 2 **Explanation:** The subsequence `[4, 5, 6, 7]` has the maximum value of `(4 OR 5) XOR (6 OR 7) = 2`. **Constraints:** `2 <= nums.length <= 400` `1 <= nums[i] < 27` `1 <= k <= nums.length / 2`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,6,7], k = 1", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [4,2,5,6,7], k = 2", + "output": "2 " + } + ], + "constraints": [ + "(seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1]).", + "2 <= nums.length <= 400", + "1 <= nums[i] < 27", + "1 <= k <= nums.length / 2" + ], + "python_template": "class Solution(object):\n def maxValue(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 maxValue(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxValue" + } +} \ No newline at end of file diff --git a/find-the-maximum-sum-of-node-values.json b/find-the-maximum-sum-of-node-values.json new file mode 100644 index 0000000000000000000000000000000000000000..d4da8a2afcc2f442e9ebc83a83f5bd3aa119ae5c --- /dev/null +++ b/find-the-maximum-sum-of-node-values.json @@ -0,0 +1,44 @@ +{ + "id": 3307, + "name": "find-the-maximum-sum-of-node-values", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-maximum-sum-of-node-values/", + "date": "2024-02-17", + "task_description": "There exists an **undirected** tree with `n` nodes numbered `0` to `n - 1`. You are given a **0-indexed** 2D integer array `edges` of length `n - 1`, where `edges[i] = [ui, vi]` indicates that there is an edge between nodes `ui` and `vi` in the tree. You are also given a **positive** integer `k`, and a **0-indexed** array of **non-negative** integers `nums` of length `n`, where `nums[i]` represents the **value** of the node numbered `i`. Alice wants the sum of values of tree nodes to be **maximum**, for which Alice can perform the following operation **any** number of times (**including zero**) on the tree: Choose any edge `[u, v]` connecting the nodes `u` and `v`, and update their values as follows: `nums[u] = nums[u] XOR k` `nums[v] = nums[v] XOR k` Return _the **maximum** possible **sum** of the **values** Alice can achieve by performing the operation **any** number of times_. **Example 1:** ``` **Input:** nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] **Output:** 6 **Explanation:** Alice can achieve the maximum sum of 6 using a single operation: - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2]. The total sum of values is 2 + 2 + 2 = 6. It can be shown that 6 is the maximum achievable sum of values. ``` **Example 2:** ``` **Input:** nums = [2,3], k = 7, edges = [[0,1]] **Output:** 9 **Explanation:** Alice can achieve the maximum sum of 9 using a single operation: - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4]. The total sum of values is 5 + 4 = 9. It can be shown that 9 is the maximum achievable sum of values. ``` **Example 3:** ``` **Input:** nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] **Output:** 42 **Explanation:** The maximum achievable sum is 42 which can be achieved by Alice performing no operations. ``` **Constraints:** `2 <= n == nums.length <= 2 * 104` `1 <= k <= 109` `0 <= nums[i] <= 109` `edges.length == n - 1` `edges[i].length == 2` `0 <= edges[i][0], edges[i][1] <= n - 1` The input is generated such that `edges` represent a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [2,3], k = 7, edges = [[0,1]]", + "output": "9 " + }, + { + "label": "Example 3", + "input": "nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]", + "output": "42 " + } + ], + "constraints": [ + "Choose any edge [u, v] connecting the nodes u and v, and update their values as follows:\n\n\t\nnums[u] = nums[u] XOR k\nnums[v] = nums[v] XOR k", + "nums[u] = nums[u] XOR k", + "nums[v] = nums[v] XOR k", + "nums[u] = nums[u] XOR k", + "nums[v] = nums[v] XOR k", + "2 <= n == nums.length <= 2 * 104", + "1 <= k <= 109", + "0 <= nums[i] <= 109", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= edges[i][0], edges[i][1] <= n - 1", + "The input is generated such that edges represent a valid tree." + ], + "python_template": "class Solution(object):\n def maximumValueSum(self, nums, k, edges):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumValueSum(int[] nums, int k, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "maximumValueSum" + } +} \ No newline at end of file diff --git a/find-the-median-of-the-uniqueness-array.json b/find-the-median-of-the-uniqueness-array.json new file mode 100644 index 0000000000000000000000000000000000000000..4822f5324e7f9a51da96e301ad0b717692af121a --- /dev/null +++ b/find-the-median-of-the-uniqueness-array.json @@ -0,0 +1,34 @@ +{ + "id": 3362, + "name": "find-the-median-of-the-uniqueness-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-median-of-the-uniqueness-array/", + "date": "2024-04-21", + "task_description": "You are given an integer array `nums`. The **uniqueness array** of `nums` is the sorted array that contains the number of distinct elements of all the subarrays of `nums`. In other words, it is a sorted array consisting of `distinct(nums[i..j])`, for all `0 <= i <= j < nums.length`. Here, `distinct(nums[i..j])` denotes the number of distinct elements in the subarray that starts at index `i` and ends at index `j`. Return the **median** of the **uniqueness array** of `nums`. **Note** that 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 **smaller** of the two values is taken. **Example 1:** **Input:** nums = [1,2,3] **Output:** 1 **Explanation:** The uniqueness array of `nums` is `[distinct(nums[0..0]), distinct(nums[1..1]), distinct(nums[2..2]), distinct(nums[0..1]), distinct(nums[1..2]), distinct(nums[0..2])]` which is equal to `[1, 1, 1, 2, 2, 3]`. The uniqueness array has a median of 1. Therefore, the answer is 1. **Example 2:** **Input:** nums = [3,4,3,4,5] **Output:** 2 **Explanation:** The uniqueness array of `nums` is `[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3]`. The uniqueness array has a median of 2. Therefore, the answer is 2. **Example 3:** **Input:** nums = [4,3,5,4] **Output:** 2 **Explanation:** The uniqueness array of `nums` is `[1, 1, 1, 1, 2, 2, 2, 3, 3, 3]`. The uniqueness array has a median of 2. Therefore, the answer is 2. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [3,4,3,4,5]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [4,3,5,4]", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def medianOfUniquenessArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int medianOfUniquenessArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "medianOfUniquenessArray" + } +} \ No newline at end of file diff --git a/find-the-minimum-amount-of-time-to-brew-potions.json b/find-the-minimum-amount-of-time-to-brew-potions.json new file mode 100644 index 0000000000000000000000000000000000000000..20c7c4212d35feb589c7dcd5a7732262e14b5f38 --- /dev/null +++ b/find-the-minimum-amount-of-time-to-brew-potions.json @@ -0,0 +1,36 @@ +{ + "id": 3794, + "name": "find-the-minimum-amount-of-time-to-brew-potions", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-minimum-amount-of-time-to-brew-potions/", + "date": "2025-03-16", + "task_description": "You are given two integer arrays, `skill` and `mana`, of length `n` and `m`, respectively. In a laboratory, `n` wizards must brew `m` potions _in order_. Each potion has a mana capacity `mana[j]` and **must** pass through **all** the wizards sequentially to be brewed properly. The time taken by the `ith` wizard on the `jth` potion is `timeij = skill[i] * mana[j]`. Since the brewing process is delicate, a potion **must** be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be _synchronized_ so that each wizard begins working on a potion **exactly** when it arrives. ​ Return the **minimum** amount of time required for the potions to be brewed properly. **Example 1:** **Input:** skill = [1,5,2,4], mana = [5,1,4,2] **Output:** 110 **Explanation:** Potion Number Start time Wizard 0 done by Wizard 1 done by Wizard 2 done by Wizard 3 done by 0 0 5 30 40 60 1 52 53 58 60 64 2 54 58 78 86 102 3 86 88 98 102 110 As an example for why wizard 0 cannot start working on the 1st potion before time `t = 52`, consider the case where the wizards started preparing the 1st potion at time `t = 50`. At time `t = 58`, wizard 2 is done with the 1st potion, but wizard 3 will still be working on the 0th potion till time `t = 60`. **Example 2:** **Input:** skill = [1,1,1], mana = [1,1,1] **Output:** 5 **Explanation:** Preparation of the 0th potion begins at time `t = 0`, and is completed by time `t = 3`. Preparation of the 1st potion begins at time `t = 1`, and is completed by time `t = 4`. Preparation of the 2nd potion begins at time `t = 2`, and is completed by time `t = 5`. **Example 3:** **Input:** skill = [1,2,3,4], mana = [1,2] **Output:** 21 **Constraints:** `n == skill.length` `m == mana.length` `1 <= n, m <= 5000` `1 <= mana[i], skill[i] <= 5000`", + "test_case": [ + { + "label": "Example 1", + "input": "skill = [1,5,2,4], mana = [5,1,4,2]", + "output": "110 " + }, + { + "label": "Example 2", + "input": "skill = [1,1,1], mana = [1,1,1]", + "output": "5 " + }, + { + "label": "Example 3", + "input": "skill = [1,2,3,4], mana = [1,2]", + "output": "21 Constraints: n == skill.length m == mana.length 1 <= n, m <= 5000 1 <= mana[i], skill[i] <= 500" + } + ], + "constraints": [ + "n == skill.length", + "m == mana.length", + "1 <= n, m <= 5000", + "1 <= mana[i], skill[i] <= 5000" + ], + "python_template": "class Solution(object):\n def minTime(self, skill, mana):\n \"\"\"\n :type skill: List[int]\n :type mana: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minTime(int[] skill, int[] mana) {\n \n }\n}", + "metadata": { + "func_name": "minTime" + } +} \ No newline at end of file diff --git a/find-the-minimum-area-to-cover-all-ones-i.json b/find-the-minimum-area-to-cover-all-ones-i.json new file mode 100644 index 0000000000000000000000000000000000000000..3dec6884683026f2a68d8f49cae3fc578f253174 --- /dev/null +++ b/find-the-minimum-area-to-cover-all-ones-i.json @@ -0,0 +1,30 @@ +{ + "id": 3461, + "name": "find-the-minimum-area-to-cover-all-ones-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-minimum-area-to-cover-all-ones-i/", + "date": "2024-06-16", + "task_description": "You are given a 2D **binary** array `grid`. Find a rectangle with horizontal and vertical sides with the** smallest** area, such that all the 1's in `grid` lie inside this rectangle. Return the **minimum** possible area of the rectangle. **Example 1:** **Input:** grid = [[0,1,0],[1,0,1]] **Output:** 6 **Explanation:** The smallest rectangle has a height of 2 and a width of 3, so it has an area of `2 * 3 = 6`. **Example 2:** **Input:** grid = [[1,0],[0,0]] **Output:** 1 **Explanation:** The smallest rectangle has both height and width 1, so its area is `1 * 1 = 1`. **Constraints:** `1 <= grid.length, grid[i].length <= 1000` `grid[i][j]` is either 0 or 1. The input is generated such that there is at least one 1 in `grid`.", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[0,1,0],[1,0,1]]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "grid = [[1,0],[0,0]]", + "output": "1 " + } + ], + "constraints": [ + "1 <= grid.length, grid[i].length <= 1000", + "grid[i][j] is either 0 or 1.", + "The input is generated such that there is at least one 1 in grid." + ], + "python_template": "class Solution(object):\n def minimumArea(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumArea(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "minimumArea" + } +} \ No newline at end of file diff --git a/find-the-minimum-cost-array-permutation.json b/find-the-minimum-cost-array-permutation.json new file mode 100644 index 0000000000000000000000000000000000000000..e84c41e677276e086317fc9c2d8d7981a1ca0747 --- /dev/null +++ b/find-the-minimum-cost-array-permutation.json @@ -0,0 +1,29 @@ +{ + "id": 3431, + "name": "find-the-minimum-cost-array-permutation", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-minimum-cost-array-permutation/", + "date": "2024-05-05", + "task_description": "You are given an array `nums` which is a permutation of `[0, 1, 2, ..., n - 1]`. The **score** of any permutation of `[0, 1, 2, ..., n - 1]` named `perm` is defined as: `score(perm) = |perm[0] - nums[perm[1]]| + |perm[1] - nums[perm[2]]| + ... + |perm[n - 1] - nums[perm[0]]|` Return the permutation `perm` which has the **minimum** possible score. If _multiple_ permutations exist with this score, return the one that is lexicographically smallest among them. **Example 1:** **Input:** nums = [1,0,2] **Output:** [0,1,2] **Explanation:** **** The lexicographically smallest permutation with minimum cost is `[0,1,2]`. The cost of this permutation is `|0 - 0| + |1 - 2| + |2 - 1| = 2`. **Example 2:** **Input:** nums = [0,2,1] **Output:** [0,2,1] **Explanation:** **** The lexicographically smallest permutation with minimum cost is `[0,2,1]`. The cost of this permutation is `|0 - 1| + |2 - 2| + |1 - 0| = 2`. **Constraints:** `2 <= n == nums.length <= 14` `nums` is a permutation of `[0, 1, 2, ..., n - 1]`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,0,2]", + "output": "[0,1,2] " + }, + { + "label": "Example 2", + "input": "nums = [0,2,1]", + "output": "[0,2,1] " + } + ], + "constraints": [ + "2 <= n == nums.length <= 14", + "nums is a permutation of [0, 1, 2, ..., n - 1]." + ], + "python_template": "class Solution(object):\n def findPermutation(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findPermutation(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findPermutation" + } +} \ No newline at end of file diff --git a/find-the-minimum-possible-sum-of-a-beautiful-array.json b/find-the-minimum-possible-sum-of-a-beautiful-array.json new file mode 100644 index 0000000000000000000000000000000000000000..eb041b02a30cb1e891b20d1a671ad2e4ccaf2a4b --- /dev/null +++ b/find-the-minimum-possible-sum-of-a-beautiful-array.json @@ -0,0 +1,37 @@ +{ + "id": 3026, + "name": "find-the-minimum-possible-sum-of-a-beautiful-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-minimum-possible-sum-of-a-beautiful-array/", + "date": "2023-08-20", + "task_description": "You are given positive integers `n` and `target`. An array `nums` is **beautiful** if it meets the following conditions: `nums.length == n`. `nums` consists of pairwise **distinct** **positive** integers. There doesn't exist two **distinct** indices, `i` and `j`, in the range `[0, n - 1]`, such that `nums[i] + nums[j] == target`. Return _the **minimum** possible sum that a beautiful array could have modulo _`109 + 7`. **Example 1:** ``` **Input:** n = 2, target = 3 **Output:** 4 **Explanation:** We can see that nums = [1,3] is beautiful. - The array nums has length n = 2. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 4 is the minimum possible sum that a beautiful array could have. ``` **Example 2:** ``` **Input:** n = 3, target = 3 **Output:** 8 **Explanation:** We can see that nums = [1,3,4] is beautiful. - The array nums has length n = 3. - The array nums consists of pairwise distinct positive integers. - There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3. It can be proven that 8 is the minimum possible sum that a beautiful array could have. ``` **Example 3:** ``` **Input:** n = 1, target = 1 **Output:** 1 **Explanation:** We can see, that nums = [1] is beautiful. ``` **Constraints:** `1 <= n <= 109` `1 <= target <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 2, target = 3", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 3, target = 3", + "output": "8 " + }, + { + "label": "Example 3", + "input": "n = 1, target = 1", + "output": "1 " + } + ], + "constraints": [ + "nums.length == n.", + "nums consists of pairwise distinct positive integers.", + "There doesn't exist two distinct indices, i and j, in the range [0, n - 1], such that nums[i] + nums[j] == target.", + "1 <= n <= 109", + "1 <= target <= 109" + ], + "python_template": "class Solution(object):\n def minimumPossibleSum(self, n, target):\n \"\"\"\n :type n: int\n :type target: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumPossibleSum(int n, int target) {\n \n }\n}", + "metadata": { + "func_name": "minimumPossibleSum" + } +} \ No newline at end of file diff --git a/find-the-n-th-value-after-k-seconds.json b/find-the-n-th-value-after-k-seconds.json new file mode 100644 index 0000000000000000000000000000000000000000..053949a2717cff4967770f8d1f7c0b1461e64918 --- /dev/null +++ b/find-the-n-th-value-after-k-seconds.json @@ -0,0 +1,28 @@ +{ + "id": 3422, + "name": "find-the-n-th-value-after-k-seconds", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/", + "date": "2024-06-02", + "task_description": "You are given two integers `n` and `k`. Initially, you start with an array `a` of `n` integers where `a[i] = 1` for all `0 <= i <= n - 1`. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, `a[0]` remains the same, `a[1]` becomes `a[0] + a[1]`, `a[2]` becomes `a[0] + a[1] + a[2]`, and so on. Return the **value** of `a[n - 1]` after `k` seconds. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** n = 4, k = 5 **Output:** 56 **Explanation:** Second State After 0 [1,1,1,1] 1 [1,2,3,4] 2 [1,3,6,10] 3 [1,4,10,20] 4 [1,5,15,35] 5 [1,6,21,56] **Example 2:** **Input:** n = 5, k = 3 **Output:** 35 **Explanation:** Second State After 0 [1,1,1,1,1] 1 [1,2,3,4,5] 2 [1,3,6,10,15] 3 [1,4,10,20,35] **Constraints:** `1 <= n, k <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, k = 5", + "output": "56 " + }, + { + "label": "Example 2", + "input": "n = 5, k = 3", + "output": "35 " + } + ], + "constraints": [ + "1 <= n, k <= 1000" + ], + "python_template": "class Solution(object):\n def valueAfterKSeconds(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int valueAfterKSeconds(int n, int k) {\n \n }\n}", + "metadata": { + "func_name": "valueAfterKSeconds" + } +} \ No newline at end of file diff --git a/find-the-number-of-copy-arrays.json b/find-the-number-of-copy-arrays.json new file mode 100644 index 0000000000000000000000000000000000000000..3b76236407eb00775a932d745b32e56413c4f7c8 --- /dev/null +++ b/find-the-number-of-copy-arrays.json @@ -0,0 +1,43 @@ +{ + "id": 3785, + "name": "find-the-number-of-copy-arrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-number-of-copy-arrays/", + "date": "2025-02-15", + "task_description": "You are given an array `original` of length `n` and a 2D array `bounds` of length `n x 2`, where `bounds[i] = [ui, vi]`. You need to find the number of **possible** arrays `copy` of length `n` such that: `(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])` for `1 <= i <= n - 1`. `ui <= copy[i] <= vi` for `0 <= i <= n - 1`. Return the number of such arrays. **Example 1:** **Input:** original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]] **Output:** 2 **Explanation:** The possible arrays are: `[1, 2, 3, 4]` `[2, 3, 4, 5]` **Example 2:** **Input:** original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]] **Output:** 4 **Explanation:** The possible arrays are: `[1, 2, 3, 4]` `[2, 3, 4, 5]` `[3, 4, 5, 6]` `[4, 5, 6, 7]` **Example 3:** **Input:** original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]] **Output:** 0 **Explanation:** No array is possible. **Constraints:** `2 <= n == original.length <= 105` `1 <= original[i] <= 109` `bounds.length == n` `bounds[i].length == 2` `1 <= bounds[i][0] <= bounds[i][1] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]]", + "output": "0 " + } + ], + "constraints": [ + "[1, 2, 3, 4]", + "[2, 3, 4, 5]", + "[1, 2, 3, 4]", + "[2, 3, 4, 5]", + "[3, 4, 5, 6]", + "[4, 5, 6, 7]", + "2 <= n == original.length <= 105", + "1 <= original[i] <= 109", + "bounds.length == n", + "bounds[i].length == 2", + "1 <= bounds[i][0] <= bounds[i][1] <= 109" + ], + "python_template": "class Solution(object):\n def countArrays(self, original, bounds):\n \"\"\"\n :type original: List[int]\n :type bounds: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countArrays(int[] original, int[][] bounds) {\n \n }\n}", + "metadata": { + "func_name": "countArrays" + } +} \ No newline at end of file diff --git a/find-the-number-of-distinct-colors-among-the-balls.json b/find-the-number-of-distinct-colors-among-the-balls.json new file mode 100644 index 0000000000000000000000000000000000000000..5ac6dda176d0f02dcc675685aae0970ae9087c8b --- /dev/null +++ b/find-the-number-of-distinct-colors-among-the-balls.json @@ -0,0 +1,41 @@ +{ + "id": 3434, + "name": "find-the-number-of-distinct-colors-among-the-balls", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/", + "date": "2024-05-11", + "task_description": "You are given an integer `limit` and a 2D array `queries` of size `n x 2`. There are `limit + 1` balls with **distinct** labels in the range `[0, limit]`. Initially, all balls are uncolored. For every query in `queries` that is of the form `[x, y]`, you mark ball `x` with the color `y`. After each query, you need to find the number of colors among the balls. Return an array `result` of length `n`, where `result[i]` denotes the number of colors _after_ `ith` query. **Note** that when answering a query, lack of a color _will not_ be considered as a color. **Example 1:** **Input:** limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]] **Output:** [1,2,2,3] **Explanation:** After query 0, ball 1 has color 4. After query 1, ball 1 has color 4, and ball 2 has color 5. After query 2, ball 1 has color 3, and ball 2 has color 5. After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4. **Example 2:** **Input:** limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]] **Output:** [1,2,2,3,4] **Explanation:** **** After query 0, ball 0 has color 1. After query 1, ball 0 has color 1, and ball 1 has color 2. After query 2, ball 0 has color 1, and balls 1 and 2 have color 2. After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4. After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5. **Constraints:** `1 <= limit <= 109` `1 <= n == queries.length <= 105` `queries[i].length == 2` `0 <= queries[i][0] <= limit` `1 <= queries[i][1] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]", + "output": "[1,2,2,3] " + }, + { + "label": "Example 2", + "input": "limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]", + "output": "[1,2,2,3,4] " + } + ], + "constraints": [ + "After query 0, ball 1 has color 4.", + "After query 1, ball 1 has color 4, and ball 2 has color 5.", + "After query 2, ball 1 has color 3, and ball 2 has color 5.", + "After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.", + "After query 0, ball 0 has color 1.", + "After query 1, ball 0 has color 1, and ball 1 has color 2.", + "After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.", + "After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.", + "After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5.", + "1 <= limit <= 109", + "1 <= n == queries.length <= 105", + "queries[i].length == 2", + "0 <= queries[i][0] <= limit", + "1 <= queries[i][1] <= 109" + ], + "python_template": "class Solution(object):\n def queryResults(self, limit, queries):\n \"\"\"\n :type limit: int\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] queryResults(int limit, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "queryResults" + } +} \ No newline at end of file diff --git a/find-the-number-of-good-pairs-i.json b/find-the-number-of-good-pairs-i.json new file mode 100644 index 0000000000000000000000000000000000000000..cca961843d46f0b450843b55e6da5c6de6063c42 --- /dev/null +++ b/find-the-number-of-good-pairs-i.json @@ -0,0 +1,30 @@ +{ + "id": 3446, + "name": "find-the-number-of-good-pairs-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-number-of-good-pairs-i/", + "date": "2024-05-19", + "task_description": "You are given 2 integer arrays `nums1` and `nums2` of lengths `n` and `m` respectively. You are also given a **positive** integer `k`. A pair `(i, j)` is called **good** if `nums1[i]` is divisible by `nums2[j] * k` (`0 <= i <= n - 1`, `0 <= j <= m - 1`). Return the total number of **good** pairs. **Example 1:** **Input:** nums1 = [1,3,4], nums2 = [1,3,4], k = 1 **Output:** 5 **Explanation:** The 5 good pairs are `(0, 0)`, `(1, 0)`, `(1, 1)`, `(2, 0)`, and `(2, 2)`. **Example 2:** **Input:** nums1 = [1,2,4,12], nums2 = [2,4], k = 3 **Output:** 2 **Explanation:** The 2 good pairs are `(3, 0)` and `(3, 1)`. **Constraints:** `1 <= n, m <= 50` `1 <= nums1[i], nums2[j] <= 50` `1 <= k <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,3,4], nums2 = [1,3,4], k = 1", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,2,4,12], nums2 = [2,4], k = 3", + "output": "2 " + } + ], + "constraints": [ + "1 <= n, m <= 50", + "1 <= nums1[i], nums2[j] <= 50", + "1 <= k <= 50" + ], + "python_template": "class Solution(object):\n def numberOfPairs(self, nums1, nums2, k):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfPairs(int[] nums1, int[] nums2, int k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPairs" + } +} \ No newline at end of file diff --git a/find-the-number-of-good-pairs-ii.json b/find-the-number-of-good-pairs-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..ea33fbd6b1305ad91b47ab2a2c15dbf831f9db58 --- /dev/null +++ b/find-the-number-of-good-pairs-ii.json @@ -0,0 +1,30 @@ +{ + "id": 3444, + "name": "find-the-number-of-good-pairs-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-number-of-good-pairs-ii/", + "date": "2024-05-19", + "task_description": "You are given 2 integer arrays `nums1` and `nums2` of lengths `n` and `m` respectively. You are also given a **positive** integer `k`. A pair `(i, j)` is called **good** if `nums1[i]` is divisible by `nums2[j] * k` (`0 <= i <= n - 1`, `0 <= j <= m - 1`). Return the total number of **good** pairs. **Example 1:** **Input:** nums1 = [1,3,4], nums2 = [1,3,4], k = 1 **Output:** 5 **Explanation:** The 5 good pairs are `(0, 0)`, `(1, 0)`, `(1, 1)`, `(2, 0)`, and `(2, 2)`. **Example 2:** **Input:** nums1 = [1,2,4,12], nums2 = [2,4], k = 3 **Output:** 2 **Explanation:** The 2 good pairs are `(3, 0)` and `(3, 1)`. **Constraints:** `1 <= n, m <= 105` `1 <= nums1[i], nums2[j] <= 106` `1 <= k <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,3,4], nums2 = [1,3,4], k = 1", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,2,4,12], nums2 = [2,4], k = 3", + "output": "2 " + } + ], + "constraints": [ + "1 <= n, m <= 105", + "1 <= nums1[i], nums2[j] <= 106", + "1 <= k <= 103" + ], + "python_template": "class Solution(object):\n def numberOfPairs(self, nums1, nums2, k):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long numberOfPairs(int[] nums1, int[] nums2, int k) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPairs" + } +} \ No newline at end of file diff --git a/find-the-number-of-possible-ways-for-an-event.json b/find-the-number-of-possible-ways-for-an-event.json new file mode 100644 index 0000000000000000000000000000000000000000..2e032691fcf660c171dcf91a328908bf17f72b56 --- /dev/null +++ b/find-the-number-of-possible-ways-for-an-event.json @@ -0,0 +1,39 @@ +{ + "id": 3604, + "name": "find-the-number-of-possible-ways-for-an-event", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-number-of-possible-ways-for-an-event/", + "date": "2024-09-28", + "task_description": "You are given three integers `n`, `x`, and `y`. An event is being held for `n` performers. When a performer arrives, they are **assigned** to one of the `x` stages. All performers assigned to the **same** stage will perform together as a band, though some stages _might_ remain **empty**. After all performances are completed, the jury will **award** each band a score in the range `[1, y]`. Return the **total** number of possible ways the event can take place. Since the answer may be very large, return it **modulo** `109 + 7`. **Note** that two events are considered to have been held **differently** if **either** of the following conditions is satisfied: **Any** performer is _assigned_ a different stage. **Any** band is _awarded_ a different score. **Example 1:** **Input:** n = 1, x = 2, y = 3 **Output:** 6 **Explanation:** There are 2 ways to assign a stage to the performer. The jury can award a score of either 1, 2, or 3 to the only band. **Example 2:** **Input:** n = 5, x = 2, y = 1 **Output:** 32 **Explanation:** Each performer will be assigned either stage 1 or stage 2. All bands will be awarded a score of 1. **Example 3:** **Input:** n = 3, x = 3, y = 4 **Output:** 684 **Constraints:** `1 <= n, x, y <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 1, x = 2, y = 3", + "output": "6 " + }, + { + "label": "Example 2", + "input": "n = 5, x = 2, y = 1", + "output": "32 " + }, + { + "label": "Example 3", + "input": "n = 3, x = 3, y = 4", + "output": "68" + } + ], + "constraints": [ + "Any performer is assigned a different stage.", + "Any band is awarded a different score.", + "There are 2 ways to assign a stage to the performer.", + "The jury can award a score of either 1, 2, or 3 to the only band.", + "Each performer will be assigned either stage 1 or stage 2.", + "All bands will be awarded a score of 1.", + "1 <= n, x, y <= 1000" + ], + "python_template": "class Solution(object):\n def numberOfWays(self, n, x, y):\n \"\"\"\n :type n: int\n :type x: int\n :type y: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfWays(int n, int x, int y) {\n \n }\n}", + "metadata": { + "func_name": "numberOfWays" + } +} \ No newline at end of file diff --git a/find-the-number-of-subarrays-where-boundary-elements-are-maximum.json b/find-the-number-of-subarrays-where-boundary-elements-are-maximum.json new file mode 100644 index 0000000000000000000000000000000000000000..1d8b9c8c46de511d4f4dea4563ec18b0398f223e --- /dev/null +++ b/find-the-number-of-subarrays-where-boundary-elements-are-maximum.json @@ -0,0 +1,46 @@ +{ + "id": 3382, + "name": "find-the-number-of-subarrays-where-boundary-elements-are-maximum", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-number-of-subarrays-where-boundary-elements-are-maximum/", + "date": "2024-03-30", + "task_description": "You are given an array of **positive** integers `nums`. Return the number of subarrays of `nums`, where the **first** and the **last** elements of the subarray are _equal_ to the **largest** element in the subarray. **Example 1:** **Input:** nums = [1,4,3,3,2] **Output:** 6 **Explanation:** There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray: subarray `[**1**,4,3,3,2]`, with its largest element 1. The first element is 1 and the last element is also 1. subarray `[1,**4**,3,3,2]`, with its largest element 4. The first element is 4 and the last element is also 4. subarray `[1,4,**3**,3,2]`, with its largest element 3. The first element is 3 and the last element is also 3. subarray `[1,4,3,**3**,2]`, with its largest element 3. The first element is 3 and the last element is also 3. subarray `[1,4,3,3,**2**]`, with its largest element 2. The first element is 2 and the last element is also 2. subarray `[1,4,**3,3**,2]`, with its largest element 3. The first element is 3 and the last element is also 3. Hence, we return 6. **Example 2:** **Input:** nums = [3,3,3] **Output:** 6 **Explanation:** There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray: subarray `[**3**,3,3]`, with its largest element 3. The first element is 3 and the last element is also 3. subarray `[3,**3**,3]`, with its largest element 3. The first element is 3 and the last element is also 3. subarray `[3,3,**3**]`, with its largest element 3. The first element is 3 and the last element is also 3. subarray `[**3,3**,3]`, with its largest element 3. The first element is 3 and the last element is also 3. subarray `[3,**3,3**]`, with its largest element 3. The first element is 3 and the last element is also 3. subarray `[**3,3,3**]`, with its largest element 3. The first element is 3 and the last element is also 3. Hence, we return 6. **Example 3:** **Input:** nums = [1] **Output:** 1 **Explanation:** There is a single subarray of `nums` which is `[**1**]`, with its largest element 1. The first element is 1 and the last element is also 1. Hence, we return 1. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,3,3,2]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [3,3,3]", + "output": "6 " + }, + { + "label": "Example 3", + "input": "nums = [1]", + "output": "1 " + } + ], + "constraints": [ + "subarray [1,4,3,3,2], with its largest element 1. The first element is 1 and the last element is also 1.", + "subarray [1,4,3,3,2], with its largest element 4. The first element is 4 and the last element is also 4.", + "subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [1,4,3,3,2], with its largest element 2. The first element is 2 and the last element is also 2.", + "subarray [1,4,3,3,2], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.", + "subarray [3,3,3], with its largest element 3. The first element is 3 and the last element is also 3.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def numberOfSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long numberOfSubarrays(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "numberOfSubarrays" + } +} \ No newline at end of file diff --git a/find-the-number-of-ways-to-place-people-i.json b/find-the-number-of-ways-to-place-people-i.json new file mode 100644 index 0000000000000000000000000000000000000000..91ee3a4fd50bbed81432eb2e3f341c9767e201fd --- /dev/null +++ b/find-the-number-of-ways-to-place-people-i.json @@ -0,0 +1,44 @@ +{ + "id": 3278, + "name": "find-the-number-of-ways-to-place-people-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-number-of-ways-to-place-people-i/", + "date": "2024-01-20", + "task_description": "You are given a 2D array `points` of size `n x 2` representing integer coordinates of some points on a 2D plane, where `points[i] = [xi, yi]`. Count the number of pairs of points `(A, B)`, where `A` is on the **upper left** side of `B`, and there are no other points in the rectangle (or line) they make (**including the border**). Return the count. **Example 1:** **Input:** points = [[1,1],[2,2],[3,3]] **Output:** 0 **Explanation:** There is no way to choose `A` and `B` so `A` is on the upper left side of `B`. **Example 2:** **Input:** points = [[6,2],[4,4],[2,6]] **Output:** 2 **Explanation:** The left one is the pair `(points[1], points[0])`, where `points[1]` is on the upper left side of `points[0]` and the rectangle is empty. The middle one is the pair `(points[2], points[1])`, same as the left one it is a valid pair. The right one is the pair `(points[2], points[0])`, where `points[2]` is on the upper left side of `points[0]`, but `points[1]` is inside the rectangle so it's not a valid pair. **Example 3:** **Input:** points = [[3,1],[1,3],[1,1]] **Output:** 2 **Explanation:** The left one is the pair `(points[2], points[0])`, where `points[2]` is on the upper left side of `points[0]` and there are no other points on the line they form. Note that it is a valid state when the two points form a line. The middle one is the pair `(points[1], points[2])`, it is a valid pair same as the left one. The right one is the pair `(points[1], points[0])`, it is not a valid pair as `points[2]` is on the border of the rectangle. **Constraints:** `2 <= n <= 50` `points[i].length == 2` `0 <= points[i][0], points[i][1] <= 50` All `points[i]` are distinct.", + "test_case": [ + { + "label": "Example 1", + "input": "points = [[1,1],[2,2],[3,3]]", + "output": "0 " + }, + { + "label": "Example 2", + "input": "points = [[6,2],[4,4],[2,6]]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "points = [[3,1],[1,3],[1,1]]", + "output": "2 " + } + ], + "constraints": [ + "A is on the upper left side of B, and", + "there are no other points in the rectangle (or line) they make (including the border).", + "The left one is the pair (points[1], points[0]), where points[1] is on the upper left side of points[0] and the rectangle is empty.", + "The middle one is the pair (points[2], points[1]), same as the left one it is a valid pair.", + "The right one is the pair (points[2], points[0]), where points[2] is on the upper left side of points[0], but points[1] is inside the rectangle so it's not a valid pair.", + "The left one is the pair (points[2], points[0]), where points[2] is on the upper left side of points[0] and there are no other points on the line they form. Note that it is a valid state when the two points form a line.", + "The middle one is the pair (points[1], points[2]), it is a valid pair same as the left one.", + "The right one is the pair (points[1], points[0]), it is not a valid pair as points[2] is on the border of the rectangle.", + "2 <= n <= 50", + "points[i].length == 2", + "0 <= points[i][0], points[i][1] <= 50", + "All points[i] are distinct." + ], + "python_template": "class Solution(object):\n def numberOfPairs(self, points):\n \"\"\"\n :type points: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfPairs(int[][] points) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPairs" + } +} \ No newline at end of file diff --git a/find-the-number-of-ways-to-place-people-ii.json b/find-the-number-of-ways-to-place-people-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..12b57e6913ef4fa87ecd324565d0e2d1fd070834 --- /dev/null +++ b/find-the-number-of-ways-to-place-people-ii.json @@ -0,0 +1,38 @@ +{ + "id": 3277, + "name": "find-the-number-of-ways-to-place-people-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-number-of-ways-to-place-people-ii/", + "date": "2024-01-20", + "task_description": "You are given a 2D array `points` of size `n x 2` representing integer coordinates of some points on a 2D-plane, where `points[i] = [xi, yi]`. We define the **right** direction as positive x-axis (**increasing x-coordinate**) and the **left** direction as negative x-axis (**decreasing x-coordinate**). Similarly, we define the **up** direction as positive y-axis (**increasing y-coordinate**) and the **down** direction as negative y-axis (**decreasing y-coordinate**) You have to place `n` people, including Alice and Bob, at these points such that there is **exactly one** person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the **upper left corner** and Bob's position as the **lower right corner** of the fence (**Note** that the fence **might not** enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either **inside** the fence or **on** the fence, Alice will be sad. Return _the number of **pairs of points** where you can place Alice and Bob, such that Alice **does not** become sad on building the fence_. **Note** that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners `(1, 1)`, `(1, 3)`, `(3, 1)`, and `(3, 3)`, because: With Alice at `(3, 3)` and Bob at `(1, 1)`, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence. With Alice at `(1, 3)` and Bob at `(1, 1)`, Bob's position is not the lower right corner of the fence. **Example 1:** ``` **Input:** points = [[1,1],[2,2],[3,3]] **Output:** 0 **Explanation:** There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0. ``` **Example 2:** ``` **Input:** points = [[6,2],[4,4],[2,6]] **Output:** 2 **Explanation:** There are two ways to place Alice and Bob such that Alice will not be sad: - Place Alice at (4, 4) and Bob at (6, 2). - Place Alice at (2, 6) and Bob at (4, 4). You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence. ``` **Example 3:** ``` **Input:** points = [[3,1],[1,3],[1,1]] **Output:** 2 **Explanation:** There are two ways to place Alice and Bob such that Alice will not be sad: - Place Alice at (1, 1) and Bob at (3, 1). - Place Alice at (1, 3) and Bob at (1, 1). You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence. Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid. ``` **Constraints:** `2 <= n <= 1000` `points[i].length == 2` `-109 <= points[i][0], points[i][1] <= 109` All `points[i]` are distinct.", + "test_case": [ + { + "label": "Example 1", + "input": "points = [[1,1],[2,2],[3,3]]", + "output": "0 " + }, + { + "label": "Example 2", + "input": "points = [[6,2],[4,4],[2,6]]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "points = [[3,1],[1,3],[1,1]]", + "output": "2 " + } + ], + "constraints": [ + "With Alice at (3, 3) and Bob at (1, 1), Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.", + "With Alice at (1, 3) and Bob at (1, 1), Bob's position is not the lower right corner of the fence.", + "2 <= n <= 1000", + "points[i].length == 2", + "-109 <= points[i][0], points[i][1] <= 109", + "All points[i] are distinct." + ], + "python_template": "class Solution(object):\n def numberOfPairs(self, points):\n \"\"\"\n :type points: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numberOfPairs(int[][] points) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPairs" + } +} \ No newline at end of file diff --git a/find-the-original-array-of-prefix-xor.json b/find-the-original-array-of-prefix-xor.json new file mode 100644 index 0000000000000000000000000000000000000000..51db3163642e82ec3713e157d8421a67b839fe3b --- /dev/null +++ b/find-the-original-array-of-prefix-xor.json @@ -0,0 +1,30 @@ +{ + "id": 2519, + "name": "find-the-original-array-of-prefix-xor", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-original-array-of-prefix-xor/", + "date": "2022-10-02", + "task_description": "You are given an **integer** array `pref` of size `n`. Find and return _the array _`arr`_ of size _`n`_ that satisfies_: `pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]`. Note that `^` denotes the **bitwise-xor** operation. It can be proven that the answer is **unique**. **Example 1:** ``` **Input:** pref = [5,2,0,3,1] **Output:** [5,7,2,3,2] **Explanation:** From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1. ``` **Example 2:** ``` **Input:** pref = [13] **Output:** [13] **Explanation:** We have pref[0] = arr[0] = 13. ``` **Constraints:** `1 <= pref.length <= 105` `0 <= pref[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "pref = [5,2,0,3,1]", + "output": "[5,7,2,3,2] " + }, + { + "label": "Example 2", + "input": "pref = [13]", + "output": "[13] " + } + ], + "constraints": [ + "pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].", + "1 <= pref.length <= 105", + "0 <= pref[i] <= 106" + ], + "python_template": "class Solution(object):\n def findArray(self, pref):\n \"\"\"\n :type pref: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findArray(int[] pref) {\n \n }\n}", + "metadata": { + "func_name": "findArray" + } +} \ No newline at end of file diff --git a/find-the-original-typed-string-ii.json b/find-the-original-typed-string-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..9a9c033798d2c3a06f4d65e3cac29ae5038f37e7 --- /dev/null +++ b/find-the-original-typed-string-ii.json @@ -0,0 +1,35 @@ +{ + "id": 3618, + "name": "find-the-original-typed-string-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-original-typed-string-ii/", + "date": "2024-10-12", + "task_description": "Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and **may** press a key for too long, resulting in a character being typed **multiple** times. You are given a string `word`, which represents the **final** output displayed on Alice's screen. You are also given a **positive** integer `k`. Return the total number of _possible_ original strings that Alice _might_ have intended to type, if she was trying to type a string of size **at least** `k`. Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** word = \"aabbccdd\", k = 7 **Output:** 5 **Explanation:** The possible strings are: `\"aabbccdd\"`, `\"aabbccd\"`, `\"aabbcdd\"`, `\"aabccdd\"`, and `\"abbccdd\"`. **Example 2:** **Input:** word = \"aabbccdd\", k = 8 **Output:** 1 **Explanation:** The only possible string is `\"aabbccdd\"`. **Example 3:** **Input:** word = \"aaabbb\", k = 3 **Output:** 8 **Constraints:** `1 <= word.length <= 5 * 105` `word` consists only of lowercase English letters. `1 <= k <= 2000`", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"aabbccdd\", k = 7", + "output": "5 " + }, + { + "label": "Example 2", + "input": "word = \"aabbccdd\", k = 8", + "output": "1 " + }, + { + "label": "Example 3", + "input": "word = \"aaabbb\", k = 3", + "output": "" + } + ], + "constraints": [ + "1 <= word.length <= 5 * 105", + "word consists only of lowercase English letters.", + "1 <= k <= 2000" + ], + "python_template": "class Solution(object):\n def possibleStringCount(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int possibleStringCount(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "possibleStringCount" + } +} \ No newline at end of file diff --git a/find-the-peaks.json b/find-the-peaks.json new file mode 100644 index 0000000000000000000000000000000000000000..d028fe292879bc5e7a108b1885c69726f0dc8734 --- /dev/null +++ b/find-the-peaks.json @@ -0,0 +1,31 @@ +{ + "id": 3221, + "name": "find-the-peaks", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-peaks/", + "date": "2023-11-26", + "task_description": "You are given a **0-indexed** array `mountain`. Your task is to find all the **peaks** in the `mountain` array. Return _an array that consists of _indices_ of **peaks** in the given array in **any order**._ **Notes:** A **peak** is defined as an element that is **strictly greater** than its neighboring elements. The first and last elements of the array are **not** a peak. **Example 1:** ``` **Input:** mountain = [2,4,4] **Output:** [] **Explanation:** mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array. mountain[1] also can not be a peak because it is not strictly greater than mountain[2]. So the answer is []. ``` **Example 2:** ``` **Input:** mountain = [1,4,3,8,5] **Output:** [1,3] **Explanation:** mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array. mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1]. But mountain [1] and mountain[3] are strictly greater than their neighboring elements. So the answer is [1,3]. ``` **Constraints:** `3 <= mountain.length <= 100` `1 <= mountain[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "mountain = [2,4,4]", + "output": "[] " + }, + { + "label": "Example 2", + "input": "mountain = [1,4,3,8,5]", + "output": "[1,3] " + } + ], + "constraints": [ + "A peak is defined as an element that is strictly greater than its neighboring elements.", + "The first and last elements of the array are not a peak.", + "3 <= mountain.length <= 100", + "1 <= mountain[i] <= 100" + ], + "python_template": "class Solution(object):\n def findPeaks(self, mountain):\n \"\"\"\n :type mountain: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List findPeaks(int[] mountain) {\n \n }\n}", + "metadata": { + "func_name": "findPeaks" + } +} \ No newline at end of file diff --git a/find-the-score-of-all-prefixes-of-an-array.json b/find-the-score-of-all-prefixes-of-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..13a433e6c07cb262a6a340026afc8708f5b1d13c --- /dev/null +++ b/find-the-score-of-all-prefixes-of-an-array.json @@ -0,0 +1,30 @@ +{ + "id": 2676, + "name": "find-the-score-of-all-prefixes-of-an-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/", + "date": "2023-04-01", + "task_description": "We define the **conversion array** `conver` of an array `arr` as follows: `conver[i] = arr[i] + max(arr[0..i])` where `max(arr[0..i])` is the maximum value of `arr[j]` over `0 <= j <= i`. We also define the **score** of an array `arr` as the sum of the values of the conversion array of `arr`. Given a **0-indexed** integer array `nums` of length `n`, return _an array _`ans`_ of length _`n`_ where _`ans[i]`_ is the score of the prefix_ `nums[0..i]`. **Example 1:** ``` **Input:** nums = [2,3,7,5,10] **Output:** [4,10,24,36,56] **Explanation:** For the prefix [2], the conversion array is [4] hence the score is 4 For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10 For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24 For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36 For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56 ``` **Example 2:** ``` **Input:** nums = [1,1,2,4,8,16] **Output:** [2,4,8,16,32,64] **Explanation:** For the prefix [1], the conversion array is [2] hence the score is 2 For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4 For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8 For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16 For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32 For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64 ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,7,5,10]", + "output": "[4,10,24,36,56] " + }, + { + "label": "Example 2", + "input": "nums = [1,1,2,4,8,16]", + "output": "[2,4,8,16,32,64] " + } + ], + "constraints": [ + "conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def findPrefixScore(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] findPrefixScore(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findPrefixScore" + } +} \ No newline at end of file diff --git a/find-the-substring-with-maximum-cost.json b/find-the-substring-with-maximum-cost.json new file mode 100644 index 0000000000000000000000000000000000000000..50969f8f89a3d59ba4840c4578f5c8598f53cd9d --- /dev/null +++ b/find-the-substring-with-maximum-cost.json @@ -0,0 +1,37 @@ +{ + "id": 2669, + "name": "find-the-substring-with-maximum-cost", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-substring-with-maximum-cost/", + "date": "2023-03-18", + "task_description": "You are given a string `s`, a string `chars` of **distinct** characters and an integer array `vals` of the same length as `chars`. The **cost of the substring **is the sum of the values of each character in the substring. The cost of an empty string is considered `0`. The **value of the character **is defined in the following way: If the character is not in the string `chars`, then its value is its corresponding position **(1-indexed)** in the alphabet. For example, the value of `'a'` is `1`, the value of `'b'` is `2`, and so on. The value of `'z'` is `26`. Otherwise, assuming `i` is the index where the character occurs in the string `chars`, then its value is `vals[i]`. Return _the maximum cost among all substrings of the string_ `s`. **Example 1:** ``` **Input:** s = \"adaa\", chars = \"d\", vals = [-1000] **Output:** 2 **Explanation:** The value of the characters \"a\" and \"d\" is 1 and -1000 respectively. The substring with the maximum cost is \"aa\" and its cost is 1 + 1 = 2. It can be proven that 2 is the maximum cost. ``` **Example 2:** ``` **Input:** s = \"abc\", chars = \"abc\", vals = [-1,-1,-1] **Output:** 0 **Explanation:** The value of the characters \"a\", \"b\" and \"c\" is -1, -1, and -1 respectively. The substring with the maximum cost is the empty substring \"\" and its cost is 0. It can be proven that 0 is the maximum cost. ``` **Constraints:** `1 <= s.length <= 105` `s` consist of lowercase English letters. `1 <= chars.length <= 26` `chars` consist of **distinct** lowercase English letters. `vals.length == chars.length` `-1000 <= vals[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"adaa\", chars = \"d\", vals = [-1000]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abc\", chars = \"abc\", vals = [-1,-1,-1]", + "output": "0 " + } + ], + "constraints": [ + "If the character is not in the string chars, then its value is its corresponding position (1-indexed) in the alphabet.\n\n\t\nFor example, the value of 'a' is 1, the value of 'b' is 2, and so on. The value of 'z' is 26.", + "For example, the value of 'a' is 1, the value of 'b' is 2, and so on. The value of 'z' is 26.", + "Otherwise, assuming i is the index where the character occurs in the string chars, then its value is vals[i].", + "For example, the value of 'a' is 1, the value of 'b' is 2, and so on. The value of 'z' is 26.", + "1 <= s.length <= 105", + "s consist of lowercase English letters.", + "1 <= chars.length <= 26", + "chars consist of distinct lowercase English letters.", + "vals.length == chars.length", + "-1000 <= vals[i] <= 1000" + ], + "python_template": "class Solution(object):\n def maximumCostSubstring(self, s, chars, vals):\n \"\"\"\n :type s: str\n :type chars: str\n :type vals: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumCostSubstring(String s, String chars, int[] vals) {\n \n }\n}", + "metadata": { + "func_name": "maximumCostSubstring" + } +} \ No newline at end of file diff --git a/find-the-sum-of-encrypted-integers.json b/find-the-sum-of-encrypted-integers.json new file mode 100644 index 0000000000000000000000000000000000000000..8d3413124c91d46c3fe8216e4ea81438fd90c740 --- /dev/null +++ b/find-the-sum-of-encrypted-integers.json @@ -0,0 +1,29 @@ +{ + "id": 3367, + "name": "find-the-sum-of-encrypted-integers", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-sum-of-encrypted-integers/", + "date": "2024-03-02", + "task_description": "You are given an integer array `nums` containing **positive** integers. We define a function `encrypt` such that `encrypt(x)` replaces **every** digit in `x` with the **largest** digit in `x`. For example, `encrypt(523) = 555` and `encrypt(213) = 333`. Return _the **sum **of encrypted elements_. **Example 1:** **Input: **nums = [1,2,3] **Output: **6 **Explanation:** The encrypted elements are `[1,2,3]`. The sum of encrypted elements is `1 + 2 + 3 == 6`. **Example 2:** **Input: **nums = [10,21,31] **Output: **66 **Explanation:** The encrypted elements are `[11,22,33]`. The sum of encrypted elements is `11 + 22 + 33 == 66`. **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [10,21,31]", + "output": "66 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "1 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def sumOfEncryptedInt(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int sumOfEncryptedInt(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "sumOfEncryptedInt" + } +} \ No newline at end of file diff --git a/find-the-sum-of-subsequence-powers.json b/find-the-sum-of-subsequence-powers.json new file mode 100644 index 0000000000000000000000000000000000000000..655e780b05bca3518b6382af11d469d45650617e --- /dev/null +++ b/find-the-sum-of-subsequence-powers.json @@ -0,0 +1,35 @@ +{ + "id": 3316, + "name": "find-the-sum-of-subsequence-powers", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-sum-of-subsequence-powers/", + "date": "2024-03-16", + "task_description": "You are given an integer array `nums` of length `n`, and a **positive** integer `k`. The **power** of a subsequence is defined as the **minimum** absolute difference between **any** two elements in the subsequence. Return _the **sum** of **powers** of **all** subsequences of _`nums`_ which have length_ **_equal to_** `k`. Since the answer may be large, return it **modulo** `109 + 7`. **Example 1:** **Input:** nums = [1,2,3,4], k = 3 **Output:** 4 **Explanation:** There are 4 subsequences in `nums` which have length 3: `[1,2,3]`, `[1,3,4]`, `[1,2,4]`, and `[2,3,4]`. The sum of powers is `|2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4`. **Example 2:** **Input:** nums = [2,2], k = 2 **Output:** 0 **Explanation:** The only subsequence in `nums` which has length 2 is `[2,2]`. The sum of powers is `|2 - 2| = 0`. **Example 3:** **Input:** nums = [4,3,-1], k = 2 **Output:** 10 **Explanation:** There are 3 subsequences in `nums` which have length 2: `[4,3]`, `[4,-1]`, and `[3,-1]`. The sum of powers is `|4 - 3| + |4 - (-1)| + |3 - (-1)| = 10`. **Constraints:** `2 <= n == nums.length <= 50` `-108 <= nums[i] <= 108 ` `2 <= k <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4], k = 3", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [2,2], k = 2", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [4,3,-1], k = 2", + "output": "10 " + } + ], + "constraints": [ + "2 <= n == nums.length <= 50", + "-108 <= nums[i] <= 108", + "2 <= k <= n" + ], + "python_template": "class Solution(object):\n def sumOfPowers(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 sumOfPowers(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "sumOfPowers" + } +} \ No newline at end of file diff --git a/find-the-sum-of-the-power-of-all-subsequences.json b/find-the-sum-of-the-power-of-all-subsequences.json new file mode 100644 index 0000000000000000000000000000000000000000..d813945cbd72e67a252ee3b7a56ef6c79625209b --- /dev/null +++ b/find-the-sum-of-the-power-of-all-subsequences.json @@ -0,0 +1,43 @@ +{ + "id": 3345, + "name": "find-the-sum-of-the-power-of-all-subsequences", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-the-sum-of-the-power-of-all-subsequences/", + "date": "2024-03-02", + "task_description": "You are given an integer array `nums` of length `n` and a **positive** integer `k`. The **power** of an array of integers is defined as the number of subsequences with their sum **equal** to `k`. Return _the **sum** of **power** of all subsequences of_ `nums`_._ Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input: ** nums = [1,2,3], k = 3 **Output: ** 6 **Explanation:** There are `5` subsequences of nums with non-zero power: The subsequence `[**1**,**2**,**3**]` has `2` subsequences with `sum == 3`: `[1,2,3]` and `[1,2,3]`. The subsequence `[**1**,2,**3**]` has `1` subsequence with `sum == 3`: `[1,2,3]`. The subsequence `[1,**2**,**3**]` has `1` subsequence with `sum == 3`: `[1,2,3]`. The subsequence `[**1**,**2**,3]` has `1` subsequence with `sum == 3`: `[1,2,3]`. The subsequence `[1,2,**3**]` has `1` subsequence with `sum == 3`: `[1,2,3]`. Hence the answer is `2 + 1 + 1 + 1 + 1 = 6`. **Example 2:** **Input: ** nums = [2,3,3], k = 5 **Output: ** 4 **Explanation:** There are `3` subsequences of nums with non-zero power: The subsequence `[**2**,**3**,**3**]` has 2 subsequences with `sum == 5`: `[2,3,3]` and `[2,3,3]`. The subsequence `[**2**,3,**3**]` has 1 subsequence with `sum == 5`: `[2,3,3]`. The subsequence `[**2**,**3**,3]` has 1 subsequence with `sum == 5`: `[2,3,3]`. Hence the answer is `2 + 1 + 1 = 4`. **Example 3:** **Input: ** nums = [1,2,3], k = 7 **Output: ** 0 **Explanation: **There exists no subsequence with sum `7`. Hence all subsequences of nums have `power = 0`. **Constraints:** `1 <= n <= 100` `1 <= nums[i] <= 104` `1 <= k <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3], k = 3", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [2,3,3], k = 5", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3], k = 7", + "output": "0 " + } + ], + "constraints": [ + "The subsequence [1,2,3] has 2 subsequences with sum == 3: [1,2,3] and [1,2,3].", + "The subsequence [1,2,3] has 1 subsequence with sum == 3: [1,2,3].", + "The subsequence [1,2,3] has 1 subsequence with sum == 3: [1,2,3].", + "The subsequence [1,2,3] has 1 subsequence with sum == 3: [1,2,3].", + "The subsequence [1,2,3] has 1 subsequence with sum == 3: [1,2,3].", + "The subsequence [2,3,3] has 2 subsequences with sum == 5: [2,3,3] and [2,3,3].", + "The subsequence [2,3,3] has 1 subsequence with sum == 5: [2,3,3].", + "The subsequence [2,3,3] has 1 subsequence with sum == 5: [2,3,3].", + "1 <= n <= 100", + "1 <= nums[i] <= 104", + "1 <= k <= 100" + ], + "python_template": "class Solution(object):\n def sumOfPower(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 sumOfPower(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "sumOfPower" + } +} \ No newline at end of file diff --git a/find-the-value-of-the-partition.json b/find-the-value-of-the-partition.json new file mode 100644 index 0000000000000000000000000000000000000000..82b44c87a7f16030c4a19c60b39c943d568cda7a --- /dev/null +++ b/find-the-value-of-the-partition.json @@ -0,0 +1,32 @@ +{ + "id": 2845, + "name": "find-the-value-of-the-partition", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-the-value-of-the-partition/", + "date": "2023-06-11", + "task_description": "You are given a **positive** integer array `nums`. Partition `nums` into two arrays, `nums1` and `nums2`, such that: Each element of the array `nums` belongs to either the array `nums1` or the array `nums2`. Both arrays are **non-empty**. The value of the partition is **minimized**. The value of the partition is `|max(nums1) - min(nums2)|`. Here, `max(nums1)` denotes the maximum element of the array `nums1`, and `min(nums2)` denotes the minimum element of the array `nums2`. Return _the integer denoting the value of such partition_. **Example 1:** ``` **Input:** nums = [1,3,2,4] **Output:** 1 **Explanation:** We can partition the array nums into nums1 = [1,2] and nums2 = [3,4]. - The maximum element of the array nums1 is equal to 2. - The minimum element of the array nums2 is equal to 3. The value of the partition is |2 - 3| = 1. It can be proven that 1 is the minimum value out of all partitions. ``` **Example 2:** ``` **Input:** nums = [100,1,10] **Output:** 9 **Explanation:** We can partition the array nums into nums1 = [10] and nums2 = [100,1]. - The maximum element of the array nums1 is equal to 10. - The minimum element of the array nums2 is equal to 1. The value of the partition is |10 - 1| = 9. It can be proven that 9 is the minimum value out of all partitions. ``` **Constraints:** `2 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,2,4]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [100,1,10]", + "output": "9 " + } + ], + "constraints": [ + "Each element of the array nums belongs to either the array nums1 or the array nums2.", + "Both arrays are non-empty.", + "The value of the partition is minimized.", + "2 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def findValueOfPartition(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findValueOfPartition(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findValueOfPartition" + } +} \ No newline at end of file diff --git a/find-the-width-of-columns-of-a-grid.json b/find-the-width-of-columns-of-a-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..e2702b2a9b1867969c9faeca021a96bf6dfa1267 --- /dev/null +++ b/find-the-width-of-columns-of-a-grid.json @@ -0,0 +1,32 @@ +{ + "id": 2675, + "name": "find-the-width-of-columns-of-a-grid", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-width-of-columns-of-a-grid/", + "date": "2023-04-01", + "task_description": "You are given a **0-indexed** `m x n` integer matrix `grid`. The width of a column is the maximum **length **of its integers. For example, if `grid = [[-10], [3], [12]]`, the width of the only column is `3` since `-10` is of length `3`. Return _an integer array_ `ans` _of size_ `n` _where_ `ans[i]` _is the width of the_ `ith` _column_. The **length** of an integer `x` with `len` digits is equal to `len` if `x` is non-negative, and `len + 1` otherwise. **Example 1:** ``` **Input:** grid = [[1],[22],[333]] **Output:** [3] **Explanation:** In the 0th column, 333 is of length 3. ``` **Example 2:** ``` **Input:** grid = [[-15,1,3],[15,7,12],[5,6,-2]] **Output:** [3,1,2] **Explanation:** In the 0th column, only -15 is of length 3. In the 1st column, all integers are of length 1. In the 2nd column, both 12 and -2 are of length 2. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 100 ` `-109 <= grid[r][c] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1],[22],[333]]", + "output": "[3] " + }, + { + "label": "Example 2", + "input": "grid = [[-15,1,3],[15,7,12],[5,6,-2]]", + "output": "[3,1,2] " + } + ], + "constraints": [ + "For example, if grid = [[-10], [3], [12]], the width of the only column is 3 since -10 is of length 3.", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 100", + "-109 <= grid[r][c] <= 109" + ], + "python_template": "class Solution(object):\n def findColumnWidth(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] findColumnWidth(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "findColumnWidth" + } +} \ No newline at end of file diff --git a/find-the-xor-of-numbers-which-appear-twice.json b/find-the-xor-of-numbers-which-appear-twice.json new file mode 100644 index 0000000000000000000000000000000000000000..bf6adcd63a9c9e48b45ca80b0ea8e8daa2f5e1b6 --- /dev/null +++ b/find-the-xor-of-numbers-which-appear-twice.json @@ -0,0 +1,35 @@ +{ + "id": 3428, + "name": "find-the-xor-of-numbers-which-appear-twice", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice/", + "date": "2024-05-11", + "task_description": "You are given an array `nums`, where each number in the array appears **either**_ _once_ _or_ _twice. Return the bitwise_ _`XOR` of all the numbers that appear twice in the array, or 0 if no number appears twice. **Example 1:** **Input:** nums = [1,2,1,3] **Output:** 1 **Explanation:** The only number that appears twice in `nums` is 1. **Example 2:** **Input:** nums = [1,2,3] **Output:** 0 **Explanation:** No number appears twice in `nums`. **Example 3:** **Input:** nums = [1,2,2,1] **Output:** 3 **Explanation:** Numbers 1 and 2 appeared twice. `1 XOR 2 == 3`. **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 50` Each number in `nums` appears either once or twice.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1,3]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3]", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,2,1]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "1 <= nums[i] <= 50", + "Each number in nums appears either once or twice." + ], + "python_template": "class Solution(object):\n def duplicateNumbersXOR(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int duplicateNumbersXOR(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "duplicateNumbersXOR" + } +} \ No newline at end of file diff --git a/find-triangular-sum-of-an-array.json b/find-triangular-sum-of-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..9ed93ddd162832cf29681c13bf0276a8cdb1f407 --- /dev/null +++ b/find-triangular-sum-of-an-array.json @@ -0,0 +1,29 @@ +{ + "id": 2324, + "name": "find-triangular-sum-of-an-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-triangular-sum-of-an-array/", + "date": "2022-03-19", + "task_description": "You are given a **0-indexed** integer array `nums`, where `nums[i]` is a digit between `0` and `9` (**inclusive**). The **triangular sum** of `nums` is the value of the only element present in `nums` after the following process terminates: Let `nums` comprise of `n` elements. If `n == 1`, **end** the process. Otherwise, **create** a new **0-indexed** integer array `newNums` of length `n - 1`. For each index `i`, where `0 <= i < n - 1`, **assign** the value of `newNums[i]` as `(nums[i] + nums[i+1]) % 10`, where `%` denotes modulo operator. **Replace** the array `nums` with `newNums`. **Repeat** the entire process starting from step 1. Return _the triangular sum of_ `nums`. **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** 8 **Explanation:** The above diagram depicts the process from which we obtain the triangular sum of the array. ``` **Example 2:** ``` **Input:** nums = [5] **Output:** 5 **Explanation:** Since there is only one element in nums, the triangular sum is the value of that element itself. ``` **Constraints:** `1 <= nums.length <= 1000` `0 <= nums[i] <= 9`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "nums = [5]", + "output": "5 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "0 <= nums[i] <= 9" + ], + "python_template": "class Solution(object):\n def triangularSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int triangularSum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "triangularSum" + } +} \ No newline at end of file diff --git a/find-valid-pair-of-adjacent-digits-in-string.json b/find-valid-pair-of-adjacent-digits-in-string.json new file mode 100644 index 0000000000000000000000000000000000000000..8aa7487048879a2c4a469612cfd9d7a6d0c6281a --- /dev/null +++ b/find-valid-pair-of-adjacent-digits-in-string.json @@ -0,0 +1,36 @@ +{ + "id": 3736, + "name": "find-valid-pair-of-adjacent-digits-in-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-valid-pair-of-adjacent-digits-in-string/", + "date": "2025-01-18", + "task_description": "You are given a string `s` consisting only of digits. A **valid pair** is defined as two **adjacent** digits in `s` such that: The first digit is **not equal** to the second. Each digit in the pair appears in `s` **exactly** as many times as its numeric value. Return the first **valid pair** found in the string `s` when traversing from left to right. If no valid pair exists, return an empty string. **Example 1:** **Input:** s = \"2523533\" **Output:** \"23\" **Explanation:** Digit `'2'` appears 2 times and digit `'3'` appears 3 times. Each digit in the pair `\"23\"` appears in `s` exactly as many times as its numeric value. Hence, the output is `\"23\"`. **Example 2:** **Input:** s = \"221\" **Output:** \"21\" **Explanation:** Digit `'2'` appears 2 times and digit `'1'` appears 1 time. Hence, the output is `\"21\"`. **Example 3:** **Input:** s = \"22\" **Output:** \"\" **Explanation:** There are no valid adjacent pairs. **Constraints:** `2 <= s.length <= 100` `s` only consists of digits from `'1'` to `'9'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"2523533\"", + "output": "\"23\" " + }, + { + "label": "Example 2", + "input": "s = \"221\"", + "output": "\"21\" " + }, + { + "label": "Example 3", + "input": "s = \"22\"", + "output": "\"\" " + } + ], + "constraints": [ + "The first digit is not equal to the second.", + "Each digit in the pair appears in s exactly as many times as its numeric value.", + "2 <= s.length <= 100", + "s only consists of digits from '1' to '9'." + ], + "python_template": "class Solution(object):\n def findValidPair(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String findValidPair(String s) {\n \n }\n}", + "metadata": { + "func_name": "findValidPair" + } +} \ No newline at end of file diff --git a/find-x-sum-of-all-k-long-subarrays-i.json b/find-x-sum-of-all-k-long-subarrays-i.json new file mode 100644 index 0000000000000000000000000000000000000000..f4a9e80129893e31b5f49f30f8894ae532ad8c14 --- /dev/null +++ b/find-x-sum-of-all-k-long-subarrays-i.json @@ -0,0 +1,36 @@ +{ + "id": 3610, + "name": "find-x-sum-of-all-k-long-subarrays-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/", + "date": "2024-10-06", + "task_description": "You are given an array `nums` of `n` integers and two integers `k` and `x`. The **x-sum** of an array is calculated by the following procedure: Count the occurrences of all elements in the array. Keep only the occurrences of the top `x` most frequent elements. If two elements have the same number of occurrences, the element with the **bigger** value is considered more frequent. Calculate the sum of the resulting array. **Note** that if an array has less than `x` distinct elements, its **x-sum** is the sum of the array. Return an integer array `answer` of length `n - k + 1` where `answer[i]` is the **x-sum** of the subarray `nums[i..i + k - 1]`. **Example 1:** **Input:** nums = [1,1,2,2,3,4,2,3], k = 6, x = 2 **Output:** [6,10,12] **Explanation:** For subarray `[1, 1, 2, 2, 3, 4]`, only elements 1 and 2 will be kept in the resulting array. Hence, `answer[0] = 1 + 1 + 2 + 2`. For subarray `[1, 2, 2, 3, 4, 2]`, only elements 2 and 4 will be kept in the resulting array. Hence, `answer[1] = 2 + 2 + 2 + 4`. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times. For subarray `[2, 2, 3, 4, 2, 3]`, only elements 2 and 3 are kept in the resulting array. Hence, `answer[2] = 2 + 2 + 2 + 3 + 3`. **Example 2:** **Input:** nums = [3,8,7,8,7,5], k = 2, x = 2 **Output:** [11,15,15,15,12] **Explanation:** Since `k == x`, `answer[i]` is equal to the sum of the subarray `nums[i..i + k - 1]`. **Constraints:** `1 <= n == nums.length <= 50` `1 <= nums[i] <= 50` `1 <= x <= k <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1,2,2,3,4,2,3], k = 6, x = 2", + "output": "[6,10,12] " + }, + { + "label": "Example 2", + "input": "nums = [3,8,7,8,7,5], k = 2, x = 2", + "output": "[11,15,15,15,12] " + } + ], + "constraints": [ + "Count the occurrences of all elements in the array.", + "Keep only the occurrences of the top x most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.", + "Calculate the sum of the resulting array.", + "For subarray [1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.", + "For subarray [1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.", + "For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.", + "1 <= n == nums.length <= 50", + "1 <= nums[i] <= 50", + "1 <= x <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def findXSum(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[] findXSum(int[] nums, int k, int x) {\n \n }\n}", + "metadata": { + "func_name": "findXSum" + } +} \ No newline at end of file diff --git a/find-x-sum-of-all-k-long-subarrays-ii.json b/find-x-sum-of-all-k-long-subarrays-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..8b8eeed0843c95524365139b0312314bb58e3b34 --- /dev/null +++ b/find-x-sum-of-all-k-long-subarrays-ii.json @@ -0,0 +1,37 @@ +{ + "id": 3592, + "name": "find-x-sum-of-all-k-long-subarrays-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-ii/", + "date": "2024-10-06", + "task_description": "You are given an array `nums` of `n` integers and two integers `k` and `x`. The **x-sum** of an array is calculated by the following procedure: Count the occurrences of all elements in the array. Keep only the occurrences of the top `x` most frequent elements. If two elements have the same number of occurrences, the element with the **bigger** value is considered more frequent. Calculate the sum of the resulting array. **Note** that if an array has less than `x` distinct elements, its **x-sum** is the sum of the array. Return an integer array `answer` of length `n - k + 1` where `answer[i]` is the **x-sum** of the subarray `nums[i..i + k - 1]`. **Example 1:** **Input:** nums = [1,1,2,2,3,4,2,3], k = 6, x = 2 **Output:** [6,10,12] **Explanation:** For subarray `[1, 1, 2, 2, 3, 4]`, only elements 1 and 2 will be kept in the resulting array. Hence, `answer[0] = 1 + 1 + 2 + 2`. For subarray `[1, 2, 2, 3, 4, 2]`, only elements 2 and 4 will be kept in the resulting array. Hence, `answer[1] = 2 + 2 + 2 + 4`. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times. For subarray `[2, 2, 3, 4, 2, 3]`, only elements 2 and 3 are kept in the resulting array. Hence, `answer[2] = 2 + 2 + 2 + 3 + 3`. **Example 2:** **Input:** nums = [3,8,7,8,7,5], k = 2, x = 2 **Output:** [11,15,15,15,12] **Explanation:** Since `k == x`, `answer[i]` is equal to the sum of the subarray `nums[i..i + k - 1]`. **Constraints:** `nums.length == n` `1 <= n <= 105` `1 <= nums[i] <= 109` `1 <= x <= k <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1,2,2,3,4,2,3], k = 6, x = 2", + "output": "[6,10,12] " + }, + { + "label": "Example 2", + "input": "nums = [3,8,7,8,7,5], k = 2, x = 2", + "output": "[11,15,15,15,12] " + } + ], + "constraints": [ + "Count the occurrences of all elements in the array.", + "Keep only the occurrences of the top x most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.", + "Calculate the sum of the resulting array.", + "For subarray [1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.", + "For subarray [1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.", + "For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.", + "nums.length == n", + "1 <= n <= 105", + "1 <= nums[i] <= 109", + "1 <= x <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def findXSum(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 long[] findXSum(int[] nums, int k, int x) {\n \n }\n}", + "metadata": { + "func_name": "findXSum" + } +} \ No newline at end of file diff --git a/find-xor-beauty-of-array.json b/find-xor-beauty-of-array.json new file mode 100644 index 0000000000000000000000000000000000000000..daff1f67594f614fe94ca51b6a7e25a07451ca91 --- /dev/null +++ b/find-xor-beauty-of-array.json @@ -0,0 +1,31 @@ +{ + "id": 2621, + "name": "find-xor-beauty-of-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/find-xor-beauty-of-array/", + "date": "2022-12-24", + "task_description": "You are given a **0-indexed** integer array `nums`. The **effective value** of three indices `i`, `j`, and `k` is defined as `((nums[i] | nums[j]) & nums[k])`. The **xor-beauty** of the array is the XORing of **the effective values of all the possible triplets** of indices `(i, j, k)` where `0 <= i, j, k < n`. Return _the xor-beauty of_ `nums`. **Note** that: `val1 | val2` is bitwise OR of `val1` and `val2`. `val1 & val2` is bitwise AND of `val1` and `val2`. **Example 1:** ``` **Input:** nums = [1,4] **Output:** 5 **Explanation:** The triplets and their corresponding effective values are listed below: - (0,0,0) with effective value ((1 | 1) & 1) = 1 - (0,0,1) with effective value ((1 | 1) & 4) = 0 - (0,1,0) with effective value ((1 | 4) & 1) = 1 - (0,1,1) with effective value ((1 | 4) & 4) = 4 - (1,0,0) with effective value ((4 | 1) & 1) = 1 - (1,0,1) with effective value ((4 | 1) & 4) = 4 - (1,1,0) with effective value ((4 | 4) & 1) = 0 - (1,1,1) with effective value ((4 | 4) & 4) = 4 Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5. ``` **Example 2:** ``` **Input:** nums = [15,45,20,2,34,35,5,44,32,30] **Output:** 34 **Explanation:** `The xor-beauty of the given array is 34.` ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [15,45,20,2,34,35,5,44,32,30]", + "output": "34 " + } + ], + "constraints": [ + "val1 | val2 is bitwise OR of val1 and val2.", + "val1 & val2 is bitwise AND of val1 and val2.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def xorBeauty(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int xorBeauty(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "xorBeauty" + } +} \ No newline at end of file diff --git a/first-completely-painted-row-or-column.json b/first-completely-painted-row-or-column.json new file mode 100644 index 0000000000000000000000000000000000000000..12e79203fa4fdfec2d5724af5dbdc45fcfdcd1d6 --- /dev/null +++ b/first-completely-painted-row-or-column.json @@ -0,0 +1,35 @@ +{ + "id": 2685, + "name": "first-completely-painted-row-or-column", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/first-completely-painted-row-or-column/", + "date": "2023-04-23", + "task_description": "You are given a **0-indexed** integer array `arr`, and an `m x n` integer **matrix** `mat`. `arr` and `mat` both contain **all** the integers in the range `[1, m * n]`. Go through each index `i` in `arr` starting from index `0` and paint the cell in `mat` containing the integer `arr[i]`. Return _the smallest index_ `i` _at which either a row or a column will be completely painted in_ `mat`. **Example 1:** ``` **Input:** arr = [1,3,4,2], mat = [[1,4],[2,3]] **Output:** 2 **Explanation:** The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2]. ``` **Example 2:** ``` **Input:** arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]] **Output:** 3 **Explanation:** The second column becomes fully painted at arr[3]. ``` **Constraints:** `m == mat.length` `n = mat[i].length` `arr.length == m * n` `1 <= m, n <= 105` `1 <= m * n <= 105` `1 <= arr[i], mat[r][c] <= m * n` All the integers of `arr` are **unique**. All the integers of `mat` are **unique**.", + "test_case": [ + { + "label": "Example 1", + "input": "arr = [1,3,4,2], mat = [[1,4],[2,3]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]", + "output": "3 " + } + ], + "constraints": [ + "m == mat.length", + "n = mat[i].length", + "arr.length == m * n", + "1 <= m, n <= 105", + "1 <= m * n <= 105", + "1 <= arr[i], mat[r][c] <= m * n", + "All the integers of arr are unique.", + "All the integers of mat are unique." + ], + "python_template": "class Solution(object):\n def firstCompleteIndex(self, arr, mat):\n \"\"\"\n :type arr: List[int]\n :type mat: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int firstCompleteIndex(int[] arr, int[][] mat) {\n \n }\n}", + "metadata": { + "func_name": "firstCompleteIndex" + } +} \ No newline at end of file diff --git a/frog-jump-ii.json b/frog-jump-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..a2c646c8de00e31fbf725f1fc62076dfe0ca8ffb --- /dev/null +++ b/frog-jump-ii.json @@ -0,0 +1,32 @@ +{ + "id": 2591, + "name": "frog-jump-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/frog-jump-ii/", + "date": "2022-11-26", + "task_description": "You are given a **0-indexed** integer array `stones` sorted in **strictly increasing order** representing the positions of stones in a river. A frog, initially on the first stone, wants to travel to the last stone and then return to the first stone. However, it can jump to any stone **at most once**. The **length** of a jump is the absolute difference between the position of the stone the frog is currently on and the position of the stone to which the frog jumps. More formally, if the frog is at `stones[i]` and is jumping to `stones[j]`, the length of the jump is `|stones[i] - stones[j]|`. The **cost** of a path is the **maximum length of a jump** among all jumps in the path. Return _the **minimum** cost of a path for the frog_. **Example 1:** ``` **Input:** stones = [0,2,5,6,7] **Output:** 5 **Explanation:** The above figure represents one of the optimal paths the frog can take. The cost of this path is 5, which is the maximum length of a jump. Since it is not possible to achieve a cost of less than 5, we return it. ``` **Example 2:** ``` **Input:** stones = [0,3,9] **Output:** 9 **Explanation:** The frog can jump directly to the last stone and come back to the first stone. In this case, the length of each jump will be 9. The cost for the path will be max(9, 9) = 9. It can be shown that this is the minimum achievable cost. ``` **Constraints:** `2 <= stones.length <= 105` `0 <= stones[i] <= 109` `stones[0] == 0` `stones` is sorted in a strictly increasing order.", + "test_case": [ + { + "label": "Example 1", + "input": "stones = [0,2,5,6,7]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "stones = [0,3,9]", + "output": "9 " + } + ], + "constraints": [ + "More formally, if the frog is at stones[i] and is jumping to stones[j], the length of the jump is |stones[i] - stones[j]|.", + "2 <= stones.length <= 105", + "0 <= stones[i] <= 109", + "stones[0] == 0", + "stones is sorted in a strictly increasing order." + ], + "python_template": "class Solution(object):\n def maxJump(self, stones):\n \"\"\"\n :type stones: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxJump(int[] stones) {\n \n }\n}", + "metadata": { + "func_name": "maxJump" + } +} \ No newline at end of file diff --git a/fruits-into-baskets-ii.json b/fruits-into-baskets-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..817e7ba7ace800de03ee6496d8d805546c069ad8 --- /dev/null +++ b/fruits-into-baskets-ii.json @@ -0,0 +1,39 @@ +{ + "id": 3790, + "name": "fruits-into-baskets-ii", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/fruits-into-baskets-ii/", + "date": "2025-03-02", + "task_description": "You are given two arrays of integers, `fruits` and `baskets`, each of length `n`, where `fruits[i]` represents the **quantity** of the `ith` type of fruit, and `baskets[j]` represents the **capacity** of the `jth` basket. From left to right, place the fruits according to these rules: Each fruit type must be placed in the **leftmost available basket** with a capacity **greater than or equal** to the quantity of that fruit type. Each basket can hold only one type of fruit. If a fruit type cannot be placed in any basket, it remains unplaced. Return the number of fruit types that remain unplaced after all possible allocations are made. **Example 1:** **Input:** fruits = [4,2,5], baskets = [3,5,4] **Output:** 1 **Explanation:** `fruits[0] = 4` is placed in `baskets[1] = 5`. `fruits[1] = 2` is placed in `baskets[0] = 3`. `fruits[2] = 5` cannot be placed in `baskets[2] = 4`. Since one fruit type remains unplaced, we return 1. **Example 2:** **Input:** fruits = [3,6,1], baskets = [6,4,7] **Output:** 0 **Explanation:** `fruits[0] = 3` is placed in `baskets[0] = 6`. `fruits[1] = 6` cannot be placed in `baskets[1] = 4` (insufficient capacity) but can be placed in the next available basket, `baskets[2] = 7`. `fruits[2] = 1` is placed in `baskets[1] = 4`. Since all fruits are successfully placed, we return 0. **Constraints:** `n == fruits.length == baskets.length` `1 <= n <= 100` `1 <= fruits[i], baskets[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "fruits = [4,2,5], baskets = [3,5,4]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "fruits = [3,6,1], baskets = [6,4,7]", + "output": "0 " + } + ], + "constraints": [ + "Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.", + "Each basket can hold only one type of fruit.", + "If a fruit type cannot be placed in any basket, it remains unplaced.", + "fruits[0] = 4 is placed in baskets[1] = 5.", + "fruits[1] = 2 is placed in baskets[0] = 3.", + "fruits[2] = 5 cannot be placed in baskets[2] = 4.", + "fruits[0] = 3 is placed in baskets[0] = 6.", + "fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.", + "fruits[2] = 1 is placed in baskets[1] = 4.", + "n == fruits.length == baskets.length", + "1 <= n <= 100", + "1 <= fruits[i], baskets[i] <= 1000" + ], + "python_template": "class Solution(object):\n def numOfUnplacedFruits(self, fruits, baskets):\n \"\"\"\n :type fruits: List[int]\n :type baskets: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numOfUnplacedFruits(int[] fruits, int[] baskets) {\n \n }\n}", + "metadata": { + "func_name": "numOfUnplacedFruits" + } +} \ No newline at end of file diff --git a/fruits-into-baskets-iii.json b/fruits-into-baskets-iii.json new file mode 100644 index 0000000000000000000000000000000000000000..2fa819d42244ec5114ea1d835c6a8c065e0fbce4 --- /dev/null +++ b/fruits-into-baskets-iii.json @@ -0,0 +1,39 @@ +{ + "id": 3791, + "name": "fruits-into-baskets-iii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/fruits-into-baskets-iii/", + "date": "2025-03-02", + "task_description": "You are given two arrays of integers, `fruits` and `baskets`, each of length `n`, where `fruits[i]` represents the **quantity** of the `ith` type of fruit, and `baskets[j]` represents the **capacity** of the `jth` basket. From left to right, place the fruits according to these rules: Each fruit type must be placed in the **leftmost available basket** with a capacity **greater than or equal** to the quantity of that fruit type. Each basket can hold only one type of fruit. If a fruit type cannot be placed in any basket, it remains unplaced. Return the number of fruit types that remain unplaced after all possible allocations are made. **Example 1:** **Input:** fruits = [4,2,5], baskets = [3,5,4] **Output:** 1 **Explanation:** `fruits[0] = 4` is placed in `baskets[1] = 5`. `fruits[1] = 2` is placed in `baskets[0] = 3`. `fruits[2] = 5` cannot be placed in `baskets[2] = 4`. Since one fruit type remains unplaced, we return 1. **Example 2:** **Input:** fruits = [3,6,1], baskets = [6,4,7] **Output:** 0 **Explanation:** `fruits[0] = 3` is placed in `baskets[0] = 6`. `fruits[1] = 6` cannot be placed in `baskets[1] = 4` (insufficient capacity) but can be placed in the next available basket, `baskets[2] = 7`. `fruits[2] = 1` is placed in `baskets[1] = 4`. Since all fruits are successfully placed, we return 0. **Constraints:** `n == fruits.length == baskets.length` `1 <= n <= 105` `1 <= fruits[i], baskets[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "fruits = [4,2,5], baskets = [3,5,4]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "fruits = [3,6,1], baskets = [6,4,7]", + "output": "0 " + } + ], + "constraints": [ + "Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.", + "Each basket can hold only one type of fruit.", + "If a fruit type cannot be placed in any basket, it remains unplaced.", + "fruits[0] = 4 is placed in baskets[1] = 5.", + "fruits[1] = 2 is placed in baskets[0] = 3.", + "fruits[2] = 5 cannot be placed in baskets[2] = 4.", + "fruits[0] = 3 is placed in baskets[0] = 6.", + "fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.", + "fruits[2] = 1 is placed in baskets[1] = 4.", + "n == fruits.length == baskets.length", + "1 <= n <= 105", + "1 <= fruits[i], baskets[i] <= 109" + ], + "python_template": "class Solution(object):\n def numOfUnplacedFruits(self, fruits, baskets):\n \"\"\"\n :type fruits: List[int]\n :type baskets: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int numOfUnplacedFruits(int[] fruits, int[] baskets) {\n \n }\n}", + "metadata": { + "func_name": "numOfUnplacedFruits" + } +} \ No newline at end of file diff --git a/furthest-point-from-origin.json b/furthest-point-from-origin.json new file mode 100644 index 0000000000000000000000000000000000000000..3f173a1326472d87b4b5c84b3df662bc0273931c --- /dev/null +++ b/furthest-point-from-origin.json @@ -0,0 +1,36 @@ +{ + "id": 3019, + "name": "furthest-point-from-origin", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/furthest-point-from-origin/", + "date": "2023-08-20", + "task_description": "You are given a string `moves` of length `n` consisting only of characters `'L'`, `'R'`, and `'_'`. The string represents your movement on a number line starting from the origin `0`. In the `ith` move, you can choose one of the following directions: move to the left if `moves[i] = 'L'` or `moves[i] = '_'` move to the right if `moves[i] = 'R'` or `moves[i] = '_'` Return _the **distance from the origin** of the **furthest** point you can get to after _`n`_ moves_. **Example 1:** ``` **Input:** moves = \"L_RL__R\" **Output:** 3 **Explanation:** The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves \"LLRLLLR\". ``` **Example 2:** ``` **Input:** moves = \"_R__LL_\" **Output:** 5 **Explanation:** The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves \"LRLLLLL\". ``` **Example 3:** ``` **Input:** moves = \"_______\" **Output:** 7 **Explanation:** The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves \"RRRRRRR\". ``` **Constraints:** `1 <= moves.length == n <= 50` `moves` consists only of characters `'L'`, `'R'` and `'_'`.", + "test_case": [ + { + "label": "Example 1", + "input": "moves = \"L_RL__R\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "moves = \"_R__LL_\"", + "output": "5 " + }, + { + "label": "Example 3", + "input": "moves = \"_______\"", + "output": "7 " + } + ], + "constraints": [ + "move to the left if moves[i] = 'L' or moves[i] = '_'", + "move to the right if moves[i] = 'R' or moves[i] = '_'", + "1 <= moves.length == n <= 50", + "moves consists only of characters 'L', 'R' and '_'." + ], + "python_template": "class Solution(object):\n def furthestDistanceFromOrigin(self, moves):\n \"\"\"\n :type moves: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int furthestDistanceFromOrigin(String moves) {\n \n }\n}", + "metadata": { + "func_name": "furthestDistanceFromOrigin" + } +} \ No newline at end of file diff --git a/greatest-common-divisor-traversal.json b/greatest-common-divisor-traversal.json new file mode 100644 index 0000000000000000000000000000000000000000..b0cedcc89e6198c00b21356b26fab2d05c3e5eb1 --- /dev/null +++ b/greatest-common-divisor-traversal.json @@ -0,0 +1,34 @@ +{ + "id": 2827, + "name": "greatest-common-divisor-traversal", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/greatest-common-divisor-traversal/", + "date": "2023-05-13", + "task_description": "You are given a **0-indexed** integer array `nums`, and you are allowed to **traverse** between its indices. You can traverse between index `i` and index `j`, `i != j`, if and only if `gcd(nums[i], nums[j]) > 1`, where `gcd` is the **greatest common divisor**. Your task is to determine if for **every pair** of indices `i` and `j` in nums, where `i < j`, there exists a **sequence of traversals** that can take us from `i` to `j`. Return `true`_ if it is possible to traverse between all such pairs of indices,__ or _`false`_ otherwise._ **Example 1:** ``` **Input:** nums = [2,3,6] **Output:** true **Explanation:** In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2). To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1. To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1. ``` **Example 2:** ``` **Input:** nums = [3,9,5] **Output:** false **Explanation:** No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false. ``` **Example 3:** ``` **Input:** nums = [4,3,12,8] **Output:** true **Explanation:** There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,6]", + "output": "true " + }, + { + "label": "Example 2", + "input": "nums = [3,9,5]", + "output": "false " + }, + { + "label": "Example 3", + "input": "nums = [4,3,12,8]", + "output": "true " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def canTraverseAllPairs(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean canTraverseAllPairs(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "canTraverseAllPairs" + } +} \ No newline at end of file diff --git a/groups-of-strings.json b/groups-of-strings.json new file mode 100644 index 0000000000000000000000000000000000000000..f07c885bf036f04764d6100b099f01f618023244 --- /dev/null +++ b/groups-of-strings.json @@ -0,0 +1,38 @@ +{ + "id": 2276, + "name": "groups-of-strings", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/groups-of-strings/", + "date": "2022-01-23", + "task_description": "You are given a **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`. Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one** of the following operations: Adding exactly one letter to the set of the letters of `s1`. Deleting exactly one letter from the set of the letters of `s1`. Replacing exactly one letter from the set of the letters of `s1` with any letter, **including** itself. The array `words` can be divided into one or more non-intersecting **groups**. A string belongs to a group if any **one** of the following is true: It is connected to **at least one** other string of the group. It is the **only** string present in the group. Note that the strings in `words` should be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique. Return _an array_ `ans` _of size_ `2` _where:_ `ans[0]` _is the **maximum number** of groups_ `words` _can be divided into, and_ `ans[1]` _is the **size of the largest** group_. **Example 1:** ``` **Input:** words = [\"a\",\"b\",\"ab\",\"cde\"] **Output:** [2,3] **Explanation:** - words[0] can be used to obtain words[1] (by replacing 'a' with 'b'), and words[2] (by adding 'b'). So words[0] is connected to words[1] and words[2]. - words[1] can be used to obtain words[0] (by replacing 'b' with 'a'), and words[2] (by adding 'a'). So words[1] is connected to words[0] and words[2]. - words[2] can be used to obtain words[0] (by deleting 'b'), and words[1] (by deleting 'a'). So words[2] is connected to words[0] and words[1]. - words[3] is not connected to any string in words. Thus, words can be divided into 2 groups [\"a\",\"b\",\"ab\"] and [\"cde\"]. The size of the largest group is 3. ``` **Example 2:** ``` **Input:** words = [\"a\",\"ab\",\"abc\"] **Output:** [1,3] **Explanation:** - words[0] is connected to words[1]. - words[1] is connected to words[0] and words[2]. - words[2] is connected to words[1]. Since all strings are connected to each other, they should be grouped together. Thus, the size of the largest group is 3. ``` **Constraints:** `1 <= words.length <= 2 * 104` `1 <= words[i].length <= 26` `words[i]` consists of lowercase English letters only. No letter occurs more than once in `words[i]`.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"a\",\"b\",\"ab\",\"cde\"]", + "output": "[2,3] " + }, + { + "label": "Example 2", + "input": "words = [\"a\",\"ab\",\"abc\"]", + "output": "[1,3] " + } + ], + "constraints": [ + "Adding exactly one letter to the set of the letters of s1.", + "Deleting exactly one letter from the set of the letters of s1.", + "Replacing exactly one letter from the set of the letters of s1 with any letter, including itself.", + "It is connected to at least one other string of the group.", + "It is the only string present in the group.", + "ans[0] is the maximum number of groups words can be divided into, and", + "ans[1] is the size of the largest group.", + "1 <= words.length <= 2 * 104", + "1 <= words[i].length <= 26", + "words[i] consists of lowercase English letters only.", + "No letter occurs more than once in words[i]." + ], + "python_template": "class Solution(object):\n def groupStrings(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] groupStrings(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "groupStrings" + } +} \ No newline at end of file diff --git a/handling-sum-queries-after-update.json b/handling-sum-queries-after-update.json new file mode 100644 index 0000000000000000000000000000000000000000..cc1ea46692508bc33914bb03ede9dfa858f8d2f0 --- /dev/null +++ b/handling-sum-queries-after-update.json @@ -0,0 +1,35 @@ +{ + "id": 2703, + "name": "handling-sum-queries-after-update", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/handling-sum-queries-after-update/", + "date": "2023-02-04", + "task_description": "You are given two **0-indexed** arrays `nums1` and `nums2` and a 2D array `queries` of queries. There are three types of queries: For a query of type 1, `queries[i] = [1, l, r]`. Flip the values from `0` to `1` and from `1` to `0` in `nums1` from index `l` to index `r`. Both `l` and `r` are **0-indexed**. For a query of type 2, `queries[i] = [2, p, 0]`. For every index `0 <= i < n`, set `nums2[i] = nums2[i] + nums1[i] * p`. For a query of type 3, `queries[i] = [3, 0, 0]`. Find the sum of the elements in `nums2`. Return _an array containing all the answers to the third type queries._ **Example 1:** ``` **Input:** nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]] **Output:** [3] **Explanation:** After the first query nums1 becomes [1,1,1]. After the second query, nums2 becomes [1,1,1], so the answer to the third query is 3. Thus, [3] is returned. ``` **Example 2:** ``` **Input:** nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]] **Output:** [5] **Explanation:** After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned. ``` **Constraints:** `1 <= nums1.length,nums2.length <= 105` `nums1.length = nums2.length` `1 <= queries.length <= 105` `queries[i].length = 3` `0 <= l <= r <= nums1.length - 1` `0 <= p <= 106` `0 <= nums1[i] <= 1` `0 <= nums2[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]", + "output": "[3] " + }, + { + "label": "Example 2", + "input": "nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]]", + "output": "[5] " + } + ], + "constraints": [ + "1 <= nums1.length,nums2.length <= 105", + "nums1.length = nums2.length", + "1 <= queries.length <= 105", + "queries[i].length = 3", + "0 <= l <= r <= nums1.length - 1", + "0 <= p <= 106", + "0 <= nums1[i] <= 1", + "0 <= nums2[i] <= 109" + ], + "python_template": "class Solution(object):\n def handleQuery(self, nums1, nums2, queries):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] handleQuery(int[] nums1, int[] nums2, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "handleQuery" + } +} \ No newline at end of file diff --git a/happy-students.json b/happy-students.json new file mode 100644 index 0000000000000000000000000000000000000000..1cba19bfdc17419d1cbf6fb45a46f1777c2f9229 --- /dev/null +++ b/happy-students.json @@ -0,0 +1,31 @@ +{ + "id": 3104, + "name": "happy-students", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/happy-students/", + "date": "2023-09-10", + "task_description": "You are given a **0-indexed** integer array `nums` of length `n` where `n` is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy. The `ith` student will become happy if one of these two conditions is met: The student is selected and the total number of selected students is** strictly greater than** `nums[i]`. The student is not selected and the total number of selected students is **strictly** **less than** `nums[i]`. Return _the number of ways to select a group of students so that everyone remains happy._ **Example 1:** ``` **Input:** nums = [1,1] **Output:** 2 **Explanation:** The two possible ways are: The class teacher selects no student. The class teacher selects both students to form the group. If the class teacher selects just one student to form a group then the both students will not be happy. Therefore, there are only two possible ways. ``` **Example 2:** ``` **Input:** nums = [6,0,3,3,6,7,2,7] **Output:** 3 **Explanation:** The three possible ways are: The class teacher selects the student with index = 1 to form the group. The class teacher selects the students with index = 1, 2, 3, 6 to form the group. The class teacher selects all the students to form the group. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] < nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [6,0,3,3,6,7,2,7]", + "output": "3 " + } + ], + "constraints": [ + "The student is selected and the total number of selected students is strictly greater than nums[i].", + "The student is not selected and the total number of selected students is strictly less than nums[i].", + "1 <= nums.length <= 105", + "0 <= nums[i] < nums.length" + ], + "python_template": "class Solution(object):\n def countWays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countWays(List nums) {\n \n }\n}", + "metadata": { + "func_name": "countWays" + } +} \ No newline at end of file diff --git a/house-robber-iv.json b/house-robber-iv.json new file mode 100644 index 0000000000000000000000000000000000000000..24f262bddc6fb8aea2ae2393e98004022e2227d2 --- /dev/null +++ b/house-robber-iv.json @@ -0,0 +1,30 @@ +{ + "id": 2690, + "name": "house-robber-iv", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/house-robber-iv/", + "date": "2023-01-29", + "task_description": "There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he **refuses to steal from adjacent homes**. The **capability** of the robber is the maximum amount of money he steals from one house of all the houses he robbed. You are given an integer array `nums` representing how much money is stashed in each house. More formally, the `ith` house from the left has `nums[i]` dollars. You are also given an integer `k`, representing the **minimum** number of houses the robber will steal from. It is always possible to steal at least `k` houses. Return _the **minimum** capability of the robber out of all the possible ways to steal at least _`k`_ houses_. **Example 1:** ``` **Input:** nums = [2,3,5,9], k = 2 **Output:** 5 **Explanation:** There are three ways to rob at least 2 houses: - Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5. - Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9. - Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9. Therefore, we return min(5, 9, 9) = 5. ``` **Example 2:** ``` **Input:** nums = [2,7,9,3,1], k = 2 **Output:** 2 **Explanation:** There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `1 <= k <= (nums.length + 1)/2`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,5,9], k = 2", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [2,7,9,3,1], k = 2", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "1 <= k <= (nums.length + 1)/2" + ], + "python_template": "class Solution(object):\n def minCapability(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 minCapability(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minCapability" + } +} \ No newline at end of file diff --git a/increment-submatrices-by-one.json b/increment-submatrices-by-one.json new file mode 100644 index 0000000000000000000000000000000000000000..ce30ec98a4444e0ad9fd71ce105dfab92a029e81 --- /dev/null +++ b/increment-submatrices-by-one.json @@ -0,0 +1,32 @@ +{ + "id": 2625, + "name": "increment-submatrices-by-one", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/increment-submatrices-by-one/", + "date": "2023-01-08", + "task_description": "You are given a positive integer `n`, indicating that we initially have an `n x n` **0-indexed** integer matrix `mat` filled with zeroes. You are also given a 2D integer array `query`. For each `query[i] = [row1i, col1i, row2i, col2i]`, you should do the following operation: Add `1` to **every element** in the submatrix with the **top left** corner `(row1i, col1i)` and the **bottom right** corner `(row2i, col2i)`. That is, add `1` to `mat[x][y]` for all `row1i <= x <= row2i` and `col1i <= y <= col2i`. Return_ the matrix_ `mat`_ after performing every query._ **Example 1:** ``` **Input:** n = 3, queries = [[1,1,2,2],[0,0,1,1]] **Output:** [[1,1,0],[1,2,1],[0,1,1]] **Explanation:** The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query. - In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2). - In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1). ``` **Example 2:** ``` **Input:** n = 2, queries = [[0,0,1,1]] **Output:** [[1,1],[1,1]] **Explanation:** The diagram above shows the initial matrix and the matrix after the first query. - In the first query we add 1 to every element in the matrix. ``` **Constraints:** `1 <= n <= 500` `1 <= queries.length <= 104` `0 <= row1i <= row2i < n` `0 <= col1i <= col2i < n`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, queries = [[1,1,2,2],[0,0,1,1]]", + "output": "[[1,1,0],[1,2,1],[0,1,1]] " + }, + { + "label": "Example 2", + "input": "n = 2, queries = [[0,0,1,1]]", + "output": "[[1,1],[1,1]] " + } + ], + "constraints": [ + "Add 1 to every element in the submatrix with the top left corner (row1i, col1i) and the bottom right corner (row2i, col2i). That is, add 1 to mat[x][y] for all row1i <= x <= row2i and col1i <= y <= col2i.", + "1 <= n <= 500", + "1 <= queries.length <= 104", + "0 <= row1i <= row2i < n", + "0 <= col1i <= col2i < n" + ], + "python_template": "class Solution(object):\n def rangeAddQueries(self, n, queries):\n \"\"\"\n :type n: int\n :type queries: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] rangeAddQueries(int n, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "rangeAddQueries" + } +} \ No newline at end of file diff --git a/intervals-between-identical-elements.json b/intervals-between-identical-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..3883e6a3c50bf8ef72ff9765e567422928cc0855 --- /dev/null +++ b/intervals-between-identical-elements.json @@ -0,0 +1,30 @@ +{ + "id": 2240, + "name": "intervals-between-identical-elements", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/intervals-between-identical-elements/", + "date": "2021-12-19", + "task_description": "You are given a **0-indexed** array of `n` integers `arr`. The **interval** between two elements in `arr` is defined as the **absolute difference** between their indices. More formally, the **interval** between `arr[i]` and `arr[j]` is `|i - j|`. Return _an array_ `intervals` _of length_ `n` _where_ `intervals[i]` _is **the sum of intervals** between _`arr[i]`_ and each element in _`arr`_ with the same value as _`arr[i]`_._ **Note:** `|x|` is the absolute value of `x`. **Example 1:** ``` **Input:** arr = [2,1,3,1,2,3,3] **Output:** [4,2,7,2,4,4,5] **Explanation:** - Index 0: Another 2 is found at index 4. |0 - 4| = 4 - Index 1: Another 1 is found at index 3. |1 - 3| = 2 - Index 2: Two more 3s are found at indices 5 and 6. |2 - 5| + |2 - 6| = 7 - Index 3: Another 1 is found at index 1. |3 - 1| = 2 - Index 4: Another 2 is found at index 0. |4 - 0| = 4 - Index 5: Two more 3s are found at indices 2 and 6. |5 - 2| + |5 - 6| = 4 - Index 6: Two more 3s are found at indices 2 and 5. |6 - 2| + |6 - 5| = 5 ``` **Example 2:** ``` **Input:** arr = [10,5,10,10] **Output:** [5,0,3,4] **Explanation:** - Index 0: Two more 10s are found at indices 2 and 3. |0 - 2| + |0 - 3| = 5 - Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0. - Index 2: Two more 10s are found at indices 0 and 3. |2 - 0| + |2 - 3| = 3 - Index 3: Two more 10s are found at indices 0 and 2. |3 - 0| + |3 - 2| = 4 ``` **Constraints:** `n == arr.length` `1 <= n <= 105` `1 <= arr[i] <= 105` **Note:** This question is the same as 2615: Sum of Distances.", + "test_case": [ + { + "label": "Example 1", + "input": "arr = [2,1,3,1,2,3,3]", + "output": "[4,2,7,2,4,4,5] " + }, + { + "label": "Example 2", + "input": "arr = [10,5,10,10]", + "output": "[5,0,3,4] " + } + ], + "constraints": [ + "n == arr.length", + "1 <= n <= 105", + "1 <= arr[i] <= 105" + ], + "python_template": "class Solution(object):\n def getDistances(self, arr):\n \"\"\"\n :type arr: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] getDistances(int[] arr) {\n \n }\n}", + "metadata": { + "func_name": "getDistances" + } +} \ No newline at end of file diff --git a/k-divisible-elements-subarrays.json b/k-divisible-elements-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..5ed6a9727efbb6df53e259f5718365f83de50c9d --- /dev/null +++ b/k-divisible-elements-subarrays.json @@ -0,0 +1,32 @@ +{ + "id": 2339, + "name": "k-divisible-elements-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/k-divisible-elements-subarrays/", + "date": "2022-04-24", + "task_description": "Given an integer array `nums` and two integers `k` and `p`, return _the number of **distinct subarrays,** which have **at most**_ `k` _elements _that are _divisible by_ `p`. Two arrays `nums1` and `nums2` are said to be **distinct** if: They are of **different** lengths, or There exists **at least** one index `i` where `nums1[i] != nums2[i]`. A **subarray** is defined as a **non-empty** contiguous sequence of elements in an array. **Example 1:** ``` **Input:** nums = [**2**,3,3,**2**,**2**], k = 2, p = 2 **Output:** 11 **Explanation:** The elements at indices 0, 3, and 4 are divisible by p = 2. The 11 distinct subarrays which have at most k = 2 elements divisible by 2 are: [2], [2,3], [2,3,3], [2,3,3,2], [3], [3,3], [3,3,2], [3,3,2,2], [3,2], [3,2,2], and [2,2]. Note that the subarrays [2] and [3] occur more than once in nums, but they should each be counted only once. The subarray [2,3,3,2,2] should not be counted because it has 3 elements that are divisible by 2. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4], k = 4, p = 1 **Output:** 10 **Explanation:** All element of nums are divisible by p = 1. Also, every subarray of nums will have at most 4 elements that are divisible by 1. Since all subarrays are distinct, the total number of subarrays satisfying all the constraints is 10. ``` **Constraints:** `1 <= nums.length <= 200` `1 <= nums[i], p <= 200` `1 <= k <= nums.length` **Follow up:** Can you solve this problem in O(n2) time complexity?", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [ 2 ,3,3, 2 , 2 ], k = 2, p = 2", + "output": "11 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4], k = 4, p = 1", + "output": "10 " + } + ], + "constraints": [ + "They are of different lengths, or", + "There exists at least one index i where nums1[i] != nums2[i].", + "1 <= nums.length <= 200", + "1 <= nums[i], p <= 200", + "1 <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def countDistinct(self, nums, k, p):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type p: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int countDistinct(int[] nums, int k, int p) {\n \n }\n}", + "metadata": { + "func_name": "countDistinct" + } +} \ No newline at end of file diff --git a/k-highest-ranked-items-within-a-price-range.json b/k-highest-ranked-items-within-a-price-range.json new file mode 100644 index 0000000000000000000000000000000000000000..51b9364617f8f4ecbd1d99e39531f9fda97d25d6 --- /dev/null +++ b/k-highest-ranked-items-within-a-price-range.json @@ -0,0 +1,47 @@ +{ + "id": 2250, + "name": "k-highest-ranked-items-within-a-price-range", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/", + "date": "2022-01-08", + "task_description": "You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following: `0` represents a wall that you cannot pass through. `1` represents an empty cell that you can freely move to and from. All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells. It takes `1` step to travel between adjacent grid cells. You are also given integer arrays `pricing` and `start` where `pricing = [low, high]` and `start = [row, col]` indicates that you start at the position `(row, col)` and are interested only in items with a price in the range of `[low, high]` (**inclusive**). You are further given an integer `k`. You are interested in the **positions** of the `k` **highest-ranked** items whose prices are **within** the given price range. The rank is determined by the **first** of these criteria that is different: Distance, defined as the length of the shortest path from the `start` (**shorter** distance has a higher rank). Price (**lower** price has a higher rank, but it must be **in the price range**). The row number (**smaller** row number has a higher rank). The column number (**smaller** column number has a higher rank). Return _the _`k`_ highest-ranked items within the price range **sorted** by their rank (highest to lowest)_. If there are fewer than `k` reachable items within the price range, return _**all** of them_. **Example 1:** ``` **Input:** grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3 **Output:** [[0,1],[1,1],[2,1]] **Explanation:** You start at (0,0). With a price range of [2,5], we can take items from (0,1), (1,1), (2,1) and (2,2). The ranks of these items are: - (0,1) with distance 1 - (1,1) with distance 2 - (2,1) with distance 3 - (2,2) with distance 4 Thus, the 3 highest ranked items in the price range are (0,1), (1,1), and (2,1). ``` **Example 2:** ``` **Input:** grid = [[1,2,0,1],[1,3,3,1],[0,2,5,1]], pricing = [2,3], start = [2,3], k = 2 **Output:** [[2,1],[1,2]] **Explanation:** You start at (2,3). With a price range of [2,3], we can take items from (0,1), (1,1), (1,2) and (2,1). The ranks of these items are: - (2,1) with distance 2, price 2 - (1,2) with distance 2, price 3 - (1,1) with distance 3 - (0,1) with distance 4 Thus, the 2 highest ranked items in the price range are (2,1) and (1,2). ``` **Example 3:** ``` **Input:** grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3 **Output:** [[2,1],[2,0]] **Explanation:** You start at (0,0). With a price range of [2,3], we can take items from (2,0) and (2,1). The ranks of these items are: - (2,1) with distance 5 - (2,0) with distance 6 Thus, the 2 highest ranked items in the price range are (2,1) and (2,0). Note that k = 3 but there are only 2 reachable items within the price range. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 105` `1 <= m * n <= 105` `0 <= grid[i][j] <= 105` `pricing.length == 2` `2 <= low <= high <= 105` `start.length == 2` `0 <= row <= m - 1` `0 <= col <= n - 1` `grid[row][col] > 0` `1 <= k <= m * n`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3", + "output": "[[0,1],[1,1],[2,1]] " + }, + { + "label": "Example 2", + "input": "grid = [[1,2,0,1],[1,3,3,1],[0,2,5,1]], pricing = [2,3], start = [2,3], k = 2", + "output": "[[2,1],[1,2]] " + }, + { + "label": "Example 3", + "input": "grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3", + "output": "[[2,1],[2,0]] " + } + ], + "constraints": [ + "0 represents a wall that you cannot pass through.", + "1 represents an empty cell that you can freely move to and from.", + "All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells.", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 105", + "1 <= m * n <= 105", + "0 <= grid[i][j] <= 105", + "pricing.length == 2", + "2 <= low <= high <= 105", + "start.length == 2", + "0 <= row <= m - 1", + "0 <= col <= n - 1", + "grid[row][col] > 0", + "1 <= k <= m * n" + ], + "python_template": "class Solution(object):\n def highestRankedKItems(self, grid, pricing, start, k):\n \"\"\"\n :type grid: List[List[int]]\n :type pricing: List[int]\n :type start: List[int]\n :type k: int\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public List> highestRankedKItems(int[][] grid, int[] pricing, int[] start, int k) {\n \n }\n}", + "metadata": { + "func_name": "highestRankedKItems" + } +} \ No newline at end of file diff --git a/k-items-with-the-maximum-sum.json b/k-items-with-the-maximum-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..f2401fdbf125cd25c80d14384242021e8c696120 --- /dev/null +++ b/k-items-with-the-maximum-sum.json @@ -0,0 +1,32 @@ +{ + "id": 2715, + "name": "k-items-with-the-maximum-sum", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/k-items-with-the-maximum-sum/", + "date": "2023-03-19", + "task_description": "There is a bag that consists of items, each item has a number `1`, `0`, or `-1` written on it. You are given four **non-negative **integers `numOnes`, `numZeros`, `numNegOnes`, and `k`. The bag initially contains: `numOnes` items with `1`s written on them. `numZeroes` items with `0`s written on them. `numNegOnes` items with `-1`s written on them. We want to pick exactly `k` items among the available items. Return _the **maximum** possible sum of numbers written on the items_. **Example 1:** ``` **Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2 **Output:** 2 **Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2. It can be proven that 2 is the maximum possible sum. ``` **Example 2:** ``` **Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4 **Output:** 3 **Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3. It can be proven that 3 is the maximum possible sum. ``` **Constraints:** `0 <= numOnes, numZeros, numNegOnes <= 50` `0 <= k <= numOnes + numZeros + numNegOnes`", + "test_case": [ + { + "label": "Example 1", + "input": "numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4", + "output": "3 " + } + ], + "constraints": [ + "numOnes items with 1s written on them.", + "numZeroes items with 0s written on them.", + "numNegOnes items with -1s written on them.", + "0 <= numOnes, numZeros, numNegOnes <= 50", + "0 <= k <= numOnes + numZeros + numNegOnes" + ], + "python_template": "class Solution(object):\n def kItemsWithMaximumSum(self, numOnes, numZeros, numNegOnes, k):\n \"\"\"\n :type numOnes: int\n :type numZeros: int\n :type numNegOnes: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {\n \n }\n}", + "metadata": { + "func_name": "kItemsWithMaximumSum" + } +} \ No newline at end of file diff --git a/k-th-nearest-obstacle-queries.json b/k-th-nearest-obstacle-queries.json new file mode 100644 index 0000000000000000000000000000000000000000..7084173e508b2f1f4c5bfe2341e4d644fdccfc53 --- /dev/null +++ b/k-th-nearest-obstacle-queries.json @@ -0,0 +1,40 @@ +{ + "id": 3495, + "name": "k-th-nearest-obstacle-queries", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/k-th-nearest-obstacle-queries/", + "date": "2024-08-25", + "task_description": "There is an infinite 2D plane. You are given a positive integer `k`. You are also given a 2D array `queries`, which contains the following queries: `queries[i] = [x, y]`: Build an obstacle at coordinate `(x, y)` in the plane. It is guaranteed that there is **no** obstacle at this coordinate when this query is made. After each query, you need to find the **distance** of the `kth` **nearest** obstacle from the origin. Return an integer array `results` where `results[i]` denotes the `kth` nearest obstacle after query `i`, or `results[i] == -1` if there are less than `k` obstacles. **Note** that initially there are **no** obstacles anywhere. The **distance** of an obstacle at coordinate `(x, y)` from the origin is given by `|x| + |y|`. **Example 1:** **Input:** queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2 **Output:** [-1,7,5,3] **Explanation:** Initially, there are 0 obstacles. After `queries[0]`, there are less than 2 obstacles. After `queries[1]`, there are obstacles at distances 3 and 7. After `queries[2]`, there are obstacles at distances 3, 5, and 7. After `queries[3]`, there are obstacles at distances 3, 3, 5, and 7. **Example 2:** **Input:** queries = [[5,5],[4,4],[3,3]], k = 1 **Output:** [10,8,6] **Explanation:** After `queries[0]`, there is an obstacle at distance 10. After `queries[1]`, there are obstacles at distances 8 and 10. After `queries[2]`, there are obstacles at distances 6, 8, and 10. **Constraints:** `1 <= queries.length <= 2 * 105` All `queries[i]` are unique. `-109 <= queries[i][0], queries[i][1] <= 109` `1 <= k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2", + "output": "[-1,7,5,3] " + }, + { + "label": "Example 2", + "input": "queries = [[5,5],[4,4],[3,3]], k = 1", + "output": "[10,8,6] " + } + ], + "constraints": [ + "queries[i] = [x, y]: Build an obstacle at coordinate (x, y) in the plane. It is guaranteed that there is no obstacle at this coordinate when this query is made.", + "Initially, there are 0 obstacles.", + "After queries[0], there are less than 2 obstacles.", + "After queries[1], there are obstacles at distances 3 and 7.", + "After queries[2], there are obstacles at distances 3, 5, and 7.", + "After queries[3], there are obstacles at distances 3, 3, 5, and 7.", + "After queries[0], there is an obstacle at distance 10.", + "After queries[1], there are obstacles at distances 8 and 10.", + "After queries[2], there are obstacles at distances 6, 8, and 10.", + "1 <= queries.length <= 2 * 105", + "All queries[i] are unique.", + "-109 <= queries[i][0], queries[i][1] <= 109", + "1 <= k <= 105" + ], + "python_template": "class Solution(object):\n def resultsArray(self, queries, k):\n \"\"\"\n :type queries: List[List[int]]\n :type k: int\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] resultsArray(int[][] queries, int k) {\n \n }\n}", + "metadata": { + "func_name": "resultsArray" + } +} \ No newline at end of file diff --git a/keep-multiplying-found-values-by-two.json b/keep-multiplying-found-values-by-two.json new file mode 100644 index 0000000000000000000000000000000000000000..c90d85f5bcf405b61a0157dd8a968bfe7ab0d6b8 --- /dev/null +++ b/keep-multiplying-found-values-by-two.json @@ -0,0 +1,29 @@ +{ + "id": 2274, + "name": "keep-multiplying-found-values-by-two", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/keep-multiplying-found-values-by-two/", + "date": "2022-01-23", + "task_description": "You are given an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`. You then do the following steps: If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`). Otherwise, **stop** the process. **Repeat** this process with the new number as long as you keep finding the number. Return _the **final** value of _`original`. **Example 1:** ``` **Input:** nums = [5,3,6,1,12], original = 3 **Output:** 24 **Explanation:** - 3 is found in nums. 3 is multiplied by 2 to obtain 6. - 6 is found in nums. 6 is multiplied by 2 to obtain 12. - 12 is found in nums. 12 is multiplied by 2 to obtain 24. - 24 is not found in nums. Thus, 24 is returned. ``` **Example 2:** ``` **Input:** nums = [2,7,9], original = 4 **Output:** 4 **Explanation:** - 4 is not found in nums. Thus, 4 is returned. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i], original <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,3,6,1,12], original = 3", + "output": "24 " + }, + { + "label": "Example 2", + "input": "nums = [2,7,9], original = 4", + "output": "4 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "1 <= nums[i], original <= 1000" + ], + "python_template": "class Solution(object):\n def findFinalValue(self, nums, original):\n \"\"\"\n :type nums: List[int]\n :type original: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findFinalValue(int[] nums, int original) {\n \n }\n}", + "metadata": { + "func_name": "findFinalValue" + } +} \ No newline at end of file diff --git a/kth-smallest-amount-with-single-denomination-combination.json b/kth-smallest-amount-with-single-denomination-combination.json new file mode 100644 index 0000000000000000000000000000000000000000..6646d4da4f0b2021578657e7ff78d62a06ecd6c4 --- /dev/null +++ b/kth-smallest-amount-with-single-denomination-combination.json @@ -0,0 +1,31 @@ +{ + "id": 3375, + "name": "kth-smallest-amount-with-single-denomination-combination", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/kth-smallest-amount-with-single-denomination-combination/", + "date": "2024-04-07", + "task_description": "You are given an integer array `coins` representing coins of different denominations and an integer `k`. You have an infinite number of coins of each denomination. However, you are **not allowed** to combine coins of different denominations. Return the `kth` **smallest** amount that can be made using these coins. **Example 1:** **Input:** coins = [3,6,9], k = 3 **Output:** 9 **Explanation:** The given coins can make the following amounts: Coin 3 produces multiples of 3: 3, 6, 9, 12, 15, etc. Coin 6 produces multiples of 6: 6, 12, 18, 24, etc. Coin 9 produces multiples of 9: 9, 18, 27, 36, etc. All of the coins combined produce: 3, 6, **9**, 12, 15, etc. **Example 2:** **Input:** coins = [5,2], k = 7 **Output:** 12 **Explanation:** The given coins can make the following amounts: Coin 5 produces multiples of 5: 5, 10, 15, 20, etc. Coin 2 produces multiples of 2: 2, 4, 6, 8, 10, 12, etc. All of the coins combined produce: 2, 4, 5, 6, 8, 10, **12**, 14, 15, etc. **Constraints:** `1 <= coins.length <= 15` `1 <= coins[i] <= 25` `1 <= k <= 2 * 109` `coins` contains pairwise distinct integers.", + "test_case": [ + { + "label": "Example 1", + "input": "coins = [3,6,9], k = 3", + "output": "9 " + }, + { + "label": "Example 2", + "input": "coins = [5,2], k = 7", + "output": "12 " + } + ], + "constraints": [ + "1 <= coins.length <= 15", + "1 <= coins[i] <= 25", + "1 <= k <= 2 * 109", + "coins contains pairwise distinct integers." + ], + "python_template": "class Solution(object):\n def findKthSmallest(self, coins, k):\n \"\"\"\n :type coins: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long findKthSmallest(int[] coins, int k) {\n \n }\n}", + "metadata": { + "func_name": "findKthSmallest" + } +} \ No newline at end of file diff --git a/largest-3-same-digit-number-in-string.json b/largest-3-same-digit-number-in-string.json new file mode 100644 index 0000000000000000000000000000000000000000..a532ab86552d3fad1676682c8641ba60ba4918c7 --- /dev/null +++ b/largest-3-same-digit-number-in-string.json @@ -0,0 +1,38 @@ +{ + "id": 2346, + "name": "largest-3-same-digit-number-in-string", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/largest-3-same-digit-number-in-string/", + "date": "2022-05-01", + "task_description": "You are given a string `num` representing a large integer. An integer is **good** if it meets the following conditions: It is a **substring** of `num` with length `3`. It consists of only one unique digit. Return _the **maximum good **integer as a **string** or an empty string _`\"\"`_ if no such integer exists_. Note: A **substring** is a contiguous sequence of characters within a string. There may be **leading zeroes** in `num` or a good integer. **Example 1:** ``` **Input:** num = \"6**777**133339\" **Output:** \"777\" **Explanation:** There are two distinct good integers: \"777\" and \"333\". \"777\" is the largest, so we return \"777\". ``` **Example 2:** ``` **Input:** num = \"23**000**19\" **Output:** \"000\" **Explanation:** \"000\" is the only good integer. ``` **Example 3:** ``` **Input:** num = \"42352338\" **Output:** \"\" **Explanation:** No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. ``` **Constraints:** `3 <= num.length <= 1000` `num` only consists of digits.", + "test_case": [ + { + "label": "Example 1", + "input": "num = \"6 777 133339\"", + "output": "\"777\" " + }, + { + "label": "Example 2", + "input": "num = \"23 000 19\"", + "output": "\"000\" " + }, + { + "label": "Example 3", + "input": "num = \"42352338\"", + "output": "\"\" " + } + ], + "constraints": [ + "It is a substring of num with length 3.", + "It consists of only one unique digit.", + "A substring is a contiguous sequence of characters within a string.", + "There may be leading zeroes in num or a good integer.", + "3 <= num.length <= 1000", + "num only consists of digits." + ], + "python_template": "class Solution(object):\n def largestGoodInteger(self, num):\n \"\"\"\n :type num: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String largestGoodInteger(String num) {\n \n }\n}", + "metadata": { + "func_name": "largestGoodInteger" + } +} \ No newline at end of file diff --git a/largest-combination-with-bitwise-and-greater-than-zero.json b/largest-combination-with-bitwise-and-greater-than-zero.json new file mode 100644 index 0000000000000000000000000000000000000000..efced943087364530cb5ab76cb561df0c7ecef8b --- /dev/null +++ b/largest-combination-with-bitwise-and-greater-than-zero.json @@ -0,0 +1,31 @@ +{ + "id": 2356, + "name": "largest-combination-with-bitwise-and-greater-than-zero", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/", + "date": "2022-05-08", + "task_description": "The **bitwise AND** of an array `nums` is the bitwise AND of all integers in `nums`. For example, for `nums = [1, 5, 3]`, the bitwise AND is equal to `1 & 5 & 3 = 1`. Also, for `nums = [7]`, the bitwise AND is `7`. You are given an array of positive integers `candidates`. Compute the **bitwise AND** for all possible **combinations** of elements in the `candidates` array. Return _the size of the **largest** combination of _`candidates`_ with a bitwise AND **greater** than _`0`. **Example 1:** ``` **Input:** candidates = [16,17,71,62,12,24,14] **Output:** 4 **Explanation:** The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0. The size of the combination is 4. It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0. Note that more than one combination may have the largest size. For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0. ``` **Example 2:** ``` **Input:** candidates = [8,8] **Output:** 2 **Explanation:** The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0. The size of the combination is 2, so we return 2. ``` **Constraints:** `1 <= candidates.length <= 105` `1 <= candidates[i] <= 107`", + "test_case": [ + { + "label": "Example 1", + "input": "candidates = [16,17,71,62,12,24,14]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "candidates = [8,8]", + "output": "2 " + } + ], + "constraints": [ + "For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.", + "Also, for nums = [7], the bitwise AND is 7.", + "1 <= candidates.length <= 105", + "1 <= candidates[i] <= 107" + ], + "python_template": "class Solution(object):\n def largestCombination(self, candidates):\n \"\"\"\n :type candidates: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int largestCombination(int[] candidates) {\n \n }\n}", + "metadata": { + "func_name": "largestCombination" + } +} \ No newline at end of file diff --git a/largest-element-in-an-array-after-merge-operations.json b/largest-element-in-an-array-after-merge-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..544d7cb1771ce435bf0a10d03cedd3e68eab159a --- /dev/null +++ b/largest-element-in-an-array-after-merge-operations.json @@ -0,0 +1,30 @@ +{ + "id": 2872, + "name": "largest-element-in-an-array-after-merge-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations/", + "date": "2023-07-16", + "task_description": "You are given a **0-indexed** array `nums` consisting of positive integers. You can do the following operation on the array **any** number of times: Choose an integer `i` such that `0 <= i < nums.length - 1` and `nums[i] <= nums[i + 1]`. Replace the element `nums[i + 1]` with `nums[i] + nums[i + 1]` and delete the element `nums[i]` from the array. Return _the value of the largest element that you can possibly obtain in the final array._ **Example 1:** ``` **Input:** nums = [2,3,7,9,3] **Output:** 21 **Explanation:** We can apply the following operations on the array: - Choose i = 0. The resulting array will be nums = [5,7,9,3]. - Choose i = 1. The resulting array will be nums = [5,16,3]. - Choose i = 0. The resulting array will be nums = [21,3]. The largest element in the final array is 21. It can be shown that we cannot obtain a larger element. ``` **Example 2:** ``` **Input:** nums = [5,3,3] **Output:** 11 **Explanation:** We can do the following operations on the array: - Choose i = 1. The resulting array will be nums = [5,6]. - Choose i = 0. The resulting array will be nums = [11]. There is only one element in the final array, which is 11. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,7,9,3]", + "output": "21 " + }, + { + "label": "Example 2", + "input": "nums = [5,3,3]", + "output": "11 " + } + ], + "constraints": [ + "Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maxArrayValue(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxArrayValue(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxArrayValue" + } +} \ No newline at end of file diff --git a/largest-local-values-in-a-matrix.json b/largest-local-values-in-a-matrix.json new file mode 100644 index 0000000000000000000000000000000000000000..b479e8b19866f10a6ffe54fa28571aea4192cd01 --- /dev/null +++ b/largest-local-values-in-a-matrix.json @@ -0,0 +1,31 @@ +{ + "id": 2454, + "name": "largest-local-values-in-a-matrix", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/largest-local-values-in-a-matrix/", + "date": "2022-08-07", + "task_description": "You are given an `n x n` integer matrix `grid`. Generate an integer matrix `maxLocal` of size `(n - 2) x (n - 2)` such that: `maxLocal[i][j]` is equal to the **largest** value of the `3 x 3` matrix in `grid` centered around row `i + 1` and column `j + 1`. In other words, we want to find the largest value in every contiguous `3 x 3` matrix in `grid`. Return _the generated matrix_. **Example 1:** ``` **Input:** grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]] **Output:** [[9,9],[8,6]] **Explanation:** The diagram above shows the original matrix and the generated matrix. Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid. ``` **Example 2:** ``` **Input:** grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]] **Output:** [[2,2,2],[2,2,2],[2,2,2]] **Explanation:** Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid. ``` **Constraints:** `n == grid.length == grid[i].length` `3 <= n <= 100` `1 <= grid[i][j] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]", + "output": "[[9,9],[8,6]] " + }, + { + "label": "Example 2", + "input": "grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]", + "output": "[[2,2,2],[2,2,2],[2,2,2]] " + } + ], + "constraints": [ + "maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.", + "n == grid.length == grid[i].length", + "3 <= n <= 100", + "1 <= grid[i][j] <= 100" + ], + "python_template": "class Solution(object):\n def largestLocal(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] largestLocal(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "largestLocal" + } +} \ No newline at end of file diff --git a/largest-palindromic-number.json b/largest-palindromic-number.json new file mode 100644 index 0000000000000000000000000000000000000000..cb56840c5ffbe5817b43b0599121c16c8420f21b --- /dev/null +++ b/largest-palindromic-number.json @@ -0,0 +1,31 @@ +{ + "id": 2475, + "name": "largest-palindromic-number", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/largest-palindromic-number/", + "date": "2022-08-14", + "task_description": "You are given a string `num` consisting of digits only. Return _the **largest palindromic** integer (in the form of a string) that can be formed using digits taken from _`num`. It should not contain **leading zeroes**. **Notes:** You do **not** need to use all the digits of `num`, but you must use **at least** one digit. The digits can be reordered. **Example 1:** ``` **Input:** num = \"444947137\" **Output:** \"7449447\" **Explanation:** Use the digits \"4449477\" from \"**44494****7**13**7**\" to form the palindromic integer \"7449447\". It can be shown that \"7449447\" is the largest palindromic integer that can be formed. ``` **Example 2:** ``` **Input:** num = \"00009\" **Output:** \"9\" **Explanation:** It can be shown that \"9\" is the largest palindromic integer that can be formed. Note that the integer returned should not contain leading zeroes. ``` **Constraints:** `1 <= num.length <= 105` `num` consists of digits.", + "test_case": [ + { + "label": "Example 1", + "input": "num = \"444947137\"", + "output": "\"7449447\" " + }, + { + "label": "Example 2", + "input": "num = \"00009\"", + "output": "\"9\" " + } + ], + "constraints": [ + "You do not need to use all the digits of num, but you must use at least one digit.", + "The digits can be reordered.", + "1 <= num.length <= 105", + "num consists of digits." + ], + "python_template": "class Solution(object):\n def largestPalindromic(self, num):\n \"\"\"\n :type num: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String largestPalindromic(String num) {\n \n }\n}", + "metadata": { + "func_name": "largestPalindromic" + } +} \ No newline at end of file diff --git a/largest-positive-integer-that-exists-with-its-negative.json b/largest-positive-integer-that-exists-with-its-negative.json new file mode 100644 index 0000000000000000000000000000000000000000..7066af4cb4068467e03813aa420689d353d2589f --- /dev/null +++ b/largest-positive-integer-that-exists-with-its-negative.json @@ -0,0 +1,35 @@ +{ + "id": 2524, + "name": "largest-positive-integer-that-exists-with-its-negative", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/", + "date": "2022-10-09", + "task_description": "Given an integer array `nums` that **does not contain** any zeros, find **the largest positive** integer `k` such that `-k` also exists in the array. Return _the positive integer _`k`. If there is no such integer, return `-1`. **Example 1:** ``` **Input:** nums = [-1,2,-3,3] **Output:** 3 **Explanation:** 3 is the only valid k we can find in the array. ``` **Example 2:** ``` **Input:** nums = [-1,10,6,7,-7,1] **Output:** 7 **Explanation:** Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value. ``` **Example 3:** ``` **Input:** nums = [-10,8,6,7,-2,-3] **Output:** -1 **Explanation:** There is no a single valid k, we return -1. ``` **Constraints:** `1 <= nums.length <= 1000` `-1000 <= nums[i] <= 1000` `nums[i] != 0`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [-1,2,-3,3]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [-1,10,6,7,-7,1]", + "output": "7 " + }, + { + "label": "Example 3", + "input": "nums = [-10,8,6,7,-2,-3]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "-1000 <= nums[i] <= 1000", + "nums[i] != 0" + ], + "python_template": "class Solution(object):\n def findMaxK(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findMaxK(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "findMaxK" + } +} \ No newline at end of file diff --git a/last-visited-integers.json b/last-visited-integers.json new file mode 100644 index 0000000000000000000000000000000000000000..70a25acae3de46ca63aa2f8b5e0c6a699176fccd --- /dev/null +++ b/last-visited-integers.json @@ -0,0 +1,35 @@ +{ + "id": 3164, + "name": "last-visited-integers", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/last-visited-integers/", + "date": "2023-09-30", + "task_description": "Given an integer array `nums` where `nums[i]` is either a positive integer or `-1`. We need to find for each `-1` the respective positive integer, which we call the last visited integer. To achieve this goal, let's define two empty arrays: `seen` and `ans`. Start iterating from the beginning of the array `nums`. If a positive integer is encountered, prepend it to the **front** of `seen`. If `-1` is encountered, let `k` be the number of **consecutive** `-1`s seen so far (including the current `-1`), If `k` is less than or equal to the length of `seen`, append the `k`-th element of `seen` to `ans`. If `k` is strictly greater than the length of `seen`, append `-1` to `ans`. Return the array_ _`ans`. **Example 1:** **Input:** nums = [1,2,-1,-1,-1] **Output:** [2,1,-1] **Explanation:** Start with `seen = []` and `ans = []`. Process `nums[0]`: The first element in nums is `1`. We prepend it to the front of `seen`. Now, `seen == [1]`. Process `nums[1]`: The next element is `2`. We prepend it to the front of `seen`. Now, `seen == [2, 1]`. Process `nums[2]`: The next element is `-1`. This is the first occurrence of `-1`, so `k == 1`. We look for the first element in seen. We append `2` to `ans`. Now, `ans == [2]`. Process `nums[3]`: Another `-1`. This is the second consecutive `-1`, so `k == 2`. The second element in `seen` is `1`, so we append `1` to `ans`. Now, `ans == [2, 1]`. Process `nums[4]`: Another `-1`, the third in a row, making `k = 3`. However, `seen` only has two elements (`[2, 1]`). Since `k` is greater than the number of elements in `seen`, we append `-1` to `ans`. Finally, `ans == [2, 1, -1]`. **Example 2:** **Input:** nums = [1,-1,2,-1,-1] **Output:** [1,2,1] **Explanation:** Start with `seen = []` and `ans = []`. Process `nums[0]`: The first element in nums is `1`. We prepend it to the front of `seen`. Now, `seen == [1]`. Process `nums[1]`: The next element is `-1`. This is the first occurrence of `-1`, so `k == 1`. We look for the first element in `seen`, which is `1`. Append `1` to `ans`. Now, `ans == [1]`. Process `nums[2]`: The next element is `2`. Prepend this to the front of `seen`. Now, `seen == [2, 1]`. Process `nums[3]`: The next element is `-1`. This `-1` is not consecutive to the first `-1` since `2` was in between. Thus, `k` resets to `1`. The first element in `seen` is `2`, so append `2` to `ans`. Now, `ans == [1, 2]`. Process `nums[4]`: Another `-1`. This is consecutive to the previous `-1`, so `k == 2`. The second element in `seen` is `1`, append `1` to `ans`. Finally, `ans == [1, 2, 1]`. **Constraints:** `1 <= nums.length <= 100` `nums[i] == -1` or `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,-1,-1,-1]", + "output": "[2,1,-1] " + }, + { + "label": "Example 2", + "input": "nums = [1,-1,2,-1,-1]", + "output": "[1,2,1] " + } + ], + "constraints": [ + "If a positive integer is encountered, prepend it to the front of seen.", + "If -1 is encountered, let k be the number of consecutive -1s seen so far (including the current -1),\n\t\nIf k is less than or equal to the length of seen, append the k-th element of seen to ans.\nIf k is strictly greater than the length of seen, append -1 to ans.", + "If k is less than or equal to the length of seen, append the k-th element of seen to ans.", + "If k is strictly greater than the length of seen, append -1 to ans.", + "If k is less than or equal to the length of seen, append the k-th element of seen to ans.", + "If k is strictly greater than the length of seen, append -1 to ans.", + "1 <= nums.length <= 100", + "nums[i] == -1 or 1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def lastVisitedIntegers(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public List lastVisitedIntegers(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "lastVisitedIntegers" + } +} \ No newline at end of file diff --git a/latest-time-you-can-obtain-after-replacing-characters.json b/latest-time-you-can-obtain-after-replacing-characters.json new file mode 100644 index 0000000000000000000000000000000000000000..9609348a1cdca10efde87c901ad16fbe41b5fbb0 --- /dev/null +++ b/latest-time-you-can-obtain-after-replacing-characters.json @@ -0,0 +1,31 @@ +{ + "id": 3361, + "name": "latest-time-you-can-obtain-after-replacing-characters", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters/", + "date": "2024-04-07", + "task_description": "You are given a string `s` representing a 12-hour format time where some of the digits (possibly none) are replaced with a `\"?\"`. 12-hour times are formatted as `\"HH:MM\"`, where `HH` is between `00` and `11`, and `MM` is between `00` and `59`. The earliest 12-hour time is `00:00`, and the latest is `11:59`. You have to replace **all** the `\"?\"` characters in `s` with digits such that the time we obtain by the resulting string is a **valid** 12-hour format time and is the **latest** possible. Return _the resulting string_. **Example 1:** **Input:** s = \"1?:?4\" **Output:** \"11:54\" **Explanation:** The latest 12-hour format time we can achieve by replacing `\"?\"` characters is `\"11:54\"`. **Example 2:** **Input:** s = \"0?:5?\" **Output:** \"09:59\" **Explanation:** The latest 12-hour format time we can achieve by replacing `\"?\"` characters is `\"09:59\"`. **Constraints:** `s.length == 5` `s[2]` is equal to the character `\":\"`. All characters except `s[2]` are digits or `\"?\"` characters. The input is generated such that there is **at least** one time between `\"00:00\"` and `\"11:59\"` that you can obtain after replacing the `\"?\"` characters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"1?:?4\"", + "output": "\"11:54\" " + }, + { + "label": "Example 2", + "input": "s = \"0?:5?\"", + "output": "\"09:59\" " + } + ], + "constraints": [ + "s.length == 5", + "s[2] is equal to the character \":\".", + "All characters except s[2] are digits or \"?\" characters.", + "The input is generated such that there is at least one time between \"00:00\" and \"11:59\" that you can obtain after replacing the \"?\" characters." + ], + "python_template": "class Solution(object):\n def findLatestTime(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String findLatestTime(String s) {\n \n }\n}", + "metadata": { + "func_name": "findLatestTime" + } +} \ No newline at end of file diff --git a/left-and-right-sum-differences.json b/left-and-right-sum-differences.json new file mode 100644 index 0000000000000000000000000000000000000000..a4aca31be8894497d30a3e9ca91513e24045fabf --- /dev/null +++ b/left-and-right-sum-differences.json @@ -0,0 +1,31 @@ +{ + "id": 2714, + "name": "left-and-right-sum-differences", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/left-and-right-sum-differences/", + "date": "2023-02-19", + "task_description": "You are given a **0-indexed** integer array `nums` of size `n`. Define two arrays `leftSum` and `rightSum` where: `leftSum[i]` is the sum of elements to the left of the index `i` in the array `nums`. If there is no such element, `leftSum[i] = 0`. `rightSum[i]` is the sum of elements to the right of the index `i` in the array `nums`. If there is no such element, `rightSum[i] = 0`. Return an integer array `answer` of size `n` where `answer[i] = |leftSum[i] - rightSum[i]|`. **Example 1:** ``` **Input:** nums = [10,4,8,3] **Output:** [15,1,11,22] **Explanation:** The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0]. The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22]. ``` **Example 2:** ``` **Input:** nums = [1] **Output:** [0] **Explanation:** The array leftSum is [0] and the array rightSum is [0]. The array answer is [|0 - 0|] = [0]. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [10,4,8,3]", + "output": "[15,1,11,22] " + }, + { + "label": "Example 2", + "input": "nums = [1]", + "output": "[0] " + } + ], + "constraints": [ + "leftSum[i] is the sum of elements to the left of the index i in the array nums. If there is no such element, leftSum[i] = 0.", + "rightSum[i] is the sum of elements to the right of the index i in the array nums. If there is no such element, rightSum[i] = 0.", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def leftRightDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] leftRightDifference(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "leftRightDifference" + } +} \ No newline at end of file diff --git a/length-of-longest-subarray-with-at-most-k-frequency.json b/length-of-longest-subarray-with-at-most-k-frequency.json new file mode 100644 index 0000000000000000000000000000000000000000..3b3ddca53f381d1fd01ea3026a91ebd93f17d9c0 --- /dev/null +++ b/length-of-longest-subarray-with-at-most-k-frequency.json @@ -0,0 +1,35 @@ +{ + "id": 3225, + "name": "length-of-longest-subarray-with-at-most-k-frequency", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/", + "date": "2023-11-25", + "task_description": "You are given an integer array `nums` and an integer `k`. The **frequency** of an element `x` is the number of times it occurs in an array. An array is called **good** if the frequency of each element in this array is **less than or equal** to `k`. Return _the length of the **longest** **good** subarray of_ `nums`_._ A **subarray** is a contiguous non-empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,2,3,1,2,3,1,2], k = 2 **Output:** 6 **Explanation:** The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good. It can be shown that there are no good subarrays with length more than 6. ``` **Example 2:** ``` **Input:** nums = [1,2,1,2,1,2,1,2], k = 1 **Output:** 2 **Explanation:** The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good. It can be shown that there are no good subarrays with length more than 2. ``` **Example 3:** ``` **Input:** nums = [5,5,5,5,5,5,5], k = 4 **Output:** 4 **Explanation:** The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray. It can be shown that there are no good subarrays with length more than 4. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `1 <= k <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,1,2,3,1,2], k = 2", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,1,2,1,2,1,2], k = 1", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [5,5,5,5,5,5,5], k = 4", + "output": "4 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "1 <= k <= nums.length" + ], + "python_template": "class Solution(object):\n def maxSubarrayLength(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 maxSubarrayLength(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxSubarrayLength" + } +} \ No newline at end of file diff --git a/length-of-longest-v-shaped-diagonal-segment.json b/length-of-longest-v-shaped-diagonal-segment.json new file mode 100644 index 0000000000000000000000000000000000000000..e6bf6501e3a5171c9184db1f15b1c9cdda36d3b0 --- /dev/null +++ b/length-of-longest-v-shaped-diagonal-segment.json @@ -0,0 +1,50 @@ +{ + "id": 3733, + "name": "length-of-longest-v-shaped-diagonal-segment", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/length-of-longest-v-shaped-diagonal-segment/", + "date": "2025-02-09", + "task_description": "You are given a 2D integer matrix `grid` of size `n x m`, where each element is either `0`, `1`, or `2`. A **V-shaped diagonal segment** is defined as: The segment starts with `1`. The subsequent elements follow this infinite sequence: `2, 0, 2, 0, ...`. The segment: Starts **along** a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right). Continues the** sequence** in the same diagonal direction. Makes** at most one clockwise 90-degree**** turn** to another diagonal direction while **maintaining** the sequence. Return the **length** of the **longest** **V-shaped diagonal segment**. If no valid segment _exists_, return 0. **Example 1:** **Input:** grid = [[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]] **Output:** 5 **Explanation:** The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: `(0,2) → (1,3) → (2,4)`, takes a **90-degree clockwise turn** at `(2,4)`, and continues as `(3,3) → (4,2)`. **Example 2:** **Input:** grid = [[2,2,2,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]] **Output:** 4 **Explanation:** **** The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: `(2,3) → (3,2)`, takes a **90-degree clockwise turn** at `(3,2)`, and continues as `(2,1) → (1,0)`. **Example 3:** **Input:** grid = [[1,2,2,2,2],[2,2,2,2,0],[2,0,0,0,0],[0,0,2,2,2],[2,0,0,2,0]] **Output:** 5 **Explanation:** **** The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: `(0,0) → (1,1) → (2,2) → (3,3) → (4,4)`. **Example 4:** **Input:** grid = [[1]] **Output:** 1 **Explanation:** The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: `(0,0)`. **Constraints:** `n == grid.length` `m == grid[i].length` `1 <= n, m <= 500` `grid[i][j]` is either `0`, `1` or `2`.", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "grid = [[2,2,2,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "grid = [[1,2,2,2,2],[2,2,2,2,0],[2,0,0,0,0],[0,0,2,2,2],[2,0,0,2,0]]", + "output": "5 " + }, + { + "label": "Example 4", + "input": "grid = [[1]]", + "output": "1 " + } + ], + "constraints": [ + "The segment starts with 1.", + "The subsequent elements follow this infinite sequence: 2, 0, 2, 0, ....", + "The segment:\n\t\nStarts along a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).\nContinues the sequence in the same diagonal direction.\nMakes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.", + "Starts along a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).", + "Continues the sequence in the same diagonal direction.", + "Makes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.", + "Starts along a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).", + "Continues the sequence in the same diagonal direction.", + "Makes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.", + "n == grid.length", + "m == grid[i].length", + "1 <= n, m <= 500", + "grid[i][j] is either 0, 1 or 2." + ], + "python_template": "class Solution(object):\n def lenOfVDiagonal(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int lenOfVDiagonal(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "lenOfVDiagonal" + } +} \ No newline at end of file diff --git a/length-of-the-longest-alphabetical-continuous-substring.json b/length-of-the-longest-alphabetical-continuous-substring.json new file mode 100644 index 0000000000000000000000000000000000000000..ade6df319139c5b2c88a23ab7f1920bfc0943808 --- /dev/null +++ b/length-of-the-longest-alphabetical-continuous-substring.json @@ -0,0 +1,30 @@ +{ + "id": 2492, + "name": "length-of-the-longest-alphabetical-continuous-substring", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/", + "date": "2022-09-11", + "task_description": "An **alphabetical continuous string** is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string `\"abcdefghijklmnopqrstuvwxyz\"`. For example, `\"abc\"` is an alphabetical continuous string, while `\"acb\"` and `\"za\"` are not. Given a string `s` consisting of lowercase letters only, return the _length of the **longest** alphabetical continuous substring._ **Example 1:** ``` **Input:** s = \"abacaba\" **Output:** 2 **Explanation:** There are 4 distinct continuous substrings: \"a\", \"b\", \"c\" and \"ab\". \"ab\" is the longest continuous substring. ``` **Example 2:** ``` **Input:** s = \"abcde\" **Output:** 5 **Explanation:** \"abcde\" is the longest continuous substring. ``` **Constraints:** `1 <= s.length <= 105` `s` consists of only English lowercase letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abacaba\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"abcde\"", + "output": "5 " + } + ], + "constraints": [ + "For example, \"abc\" is an alphabetical continuous string, while \"acb\" and \"za\" are not.", + "1 <= s.length <= 105", + "s consists of only English lowercase letters." + ], + "python_template": "class Solution(object):\n def longestContinuousSubstring(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestContinuousSubstring(String s) {\n \n }\n}", + "metadata": { + "func_name": "longestContinuousSubstring" + } +} \ No newline at end of file diff --git a/length-of-the-longest-increasing-path.json b/length-of-the-longest-increasing-path.json new file mode 100644 index 0000000000000000000000000000000000000000..65de16add5c363f4217ab604beb0302a473f0150 --- /dev/null +++ b/length-of-the-longest-increasing-path.json @@ -0,0 +1,34 @@ +{ + "id": 3571, + "name": "length-of-the-longest-increasing-path", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/length-of-the-longest-increasing-path/", + "date": "2024-08-31", + "task_description": "You are given a 2D array of integers `coordinates` of length `n` and an integer `k`, where `0 <= k < n`. `coordinates[i] = [xi, yi]` indicates the point `(xi, yi)` in a 2D plane. An **increasing path** of length `m` is defined as a list of points `(x1, y1)`, `(x2, y2)`, `(x3, y3)`, ..., `(xm, ym)` such that: `xi < xi + 1` and `yi < yi + 1` for all `i` where `1 <= i < m`. `(xi, yi)` is in the given coordinates for all `i` where `1 <= i <= m`. Return the **maximum** length of an **increasing path** that contains `coordinates[k]`. **Example 1:** **Input:** coordinates = [[3,1],[2,2],[4,1],[0,0],[5,3]], k = 1 **Output:** 3 **Explanation:** `(0, 0)`, `(2, 2)`, `(5, 3)` is the longest increasing path that contains `(2, 2)`. **Example 2:** **Input:** coordinates = [[2,1],[7,0],[5,6]], k = 2 **Output:** 2 **Explanation:** `(2, 1)`, `(5, 6)` is the longest increasing path that contains `(5, 6)`. **Constraints:** `1 <= n == coordinates.length <= 105` `coordinates[i].length == 2` `0 <= coordinates[i][0], coordinates[i][1] <= 109` All elements in `coordinates` are **distinct**. `0 <= k <= n - 1`", + "test_case": [ + { + "label": "Example 1", + "input": "coordinates = [[3,1],[2,2],[4,1],[0,0],[5,3]], k = 1", + "output": "3 " + }, + { + "label": "Example 2", + "input": "coordinates = [[2,1],[7,0],[5,6]], k = 2", + "output": "2 " + } + ], + "constraints": [ + "xi < xi + 1 and yi < yi + 1 for all i where 1 <= i < m.", + "(xi, yi) is in the given coordinates for all i where 1 <= i <= m.", + "1 <= n == coordinates.length <= 105", + "coordinates[i].length == 2", + "0 <= coordinates[i][0], coordinates[i][1] <= 109", + "All elements in coordinates are distinct.", + "0 <= k <= n - 1" + ], + "python_template": "class Solution(object):\n def maxPathLength(self, coordinates, k):\n \"\"\"\n :type coordinates: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxPathLength(int[][] coordinates, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxPathLength" + } +} \ No newline at end of file diff --git a/length-of-the-longest-subsequence-that-sums-to-target.json b/length-of-the-longest-subsequence-that-sums-to-target.json new file mode 100644 index 0000000000000000000000000000000000000000..53d0445dc34287bc0ee0cb882fc5f5dfe2de68df --- /dev/null +++ b/length-of-the-longest-subsequence-that-sums-to-target.json @@ -0,0 +1,35 @@ +{ + "id": 3106, + "name": "length-of-the-longest-subsequence-that-sums-to-target", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/length-of-the-longest-subsequence-that-sums-to-target/", + "date": "2023-10-14", + "task_description": "You are given a **0-indexed** array of integers `nums`, and an integer `target`. Return _the **length of the longest subsequence** of_ `nums` _that sums up to_ `target`. _If no such subsequence exists, return_ `-1`. A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. **Example 1:** ``` **Input:** nums = [1,2,3,4,5], target = 9 **Output:** 3 **Explanation:** There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3. ``` **Example 2:** ``` **Input:** nums = [4,1,3,2,1,5], target = 7 **Output:** 4 **Explanation:** There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4. ``` **Example 3:** ``` **Input:** nums = [1,1,5,4,5], target = 3 **Output:** -1 **Explanation:** It can be shown that nums has no subsequence that sums up to 3. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 1000` `1 <= target <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5], target = 9", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [4,1,3,2,1,5], target = 7", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [1,1,5,4,5], target = 3", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 1000", + "1 <= target <= 1000" + ], + "python_template": "class Solution(object):\n def lengthOfLongestSubsequence(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 lengthOfLongestSubsequence(List nums, int target) {\n \n }\n}", + "metadata": { + "func_name": "lengthOfLongestSubsequence" + } +} \ No newline at end of file diff --git a/length-of-the-longest-valid-substring.json b/length-of-the-longest-valid-substring.json new file mode 100644 index 0000000000000000000000000000000000000000..bf5c3f2b57480e3a396d8c7e6f07de4e5d723be3 --- /dev/null +++ b/length-of-the-longest-valid-substring.json @@ -0,0 +1,32 @@ +{ + "id": 2884, + "name": "length-of-the-longest-valid-substring", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/length-of-the-longest-valid-substring/", + "date": "2023-07-09", + "task_description": "You are given a string `word` and an array of strings `forbidden`. A string is called **valid** if none of its substrings are present in `forbidden`. Return _the length of the **longest valid substring** of the string _`word`. A **substring** is a contiguous sequence of characters in a string, possibly empty. **Example 1:** ``` **Input:** word = \"cbaaaabc\", forbidden = [\"aaa\",\"cb\"] **Output:** 4 **Explanation:** There are 11 valid substrings in word: \"c\", \"b\", \"a\", \"ba\", \"aa\", \"bc\", \"baa\", \"aab\", \"ab\", \"abc\" and \"aabc\". The length of the longest valid substring is 4. It can be shown that all other substrings contain either \"aaa\" or \"cb\" as a substring. ``` **Example 2:** ``` **Input:** word = \"leetcode\", forbidden = [\"de\",\"le\",\"e\"] **Output:** 4 **Explanation:** There are 11 valid substrings in word: \"l\", \"t\", \"c\", \"o\", \"d\", \"tc\", \"co\", \"od\", \"tco\", \"cod\", and \"tcod\". The length of the longest valid substring is 4. It can be shown that all other substrings contain either \"de\", \"le\", or \"e\" as a substring. ``` **Constraints:** `1 <= word.length <= 105` `word` consists only of lowercase English letters. `1 <= forbidden.length <= 105` `1 <= forbidden[i].length <= 10` `forbidden[i]` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"cbaaaabc\", forbidden = [\"aaa\",\"cb\"]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "word = \"leetcode\", forbidden = [\"de\",\"le\",\"e\"]", + "output": "4 " + } + ], + "constraints": [ + "1 <= word.length <= 105", + "word consists only of lowercase English letters.", + "1 <= forbidden.length <= 105", + "1 <= forbidden[i].length <= 10", + "forbidden[i] consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def longestValidSubstring(self, word, forbidden):\n \"\"\"\n :type word: str\n :type forbidden: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestValidSubstring(String word, List forbidden) {\n \n }\n}", + "metadata": { + "func_name": "longestValidSubstring" + } +} \ No newline at end of file diff --git a/lexicographically-minimum-string-after-removing-stars.json b/lexicographically-minimum-string-after-removing-stars.json new file mode 100644 index 0000000000000000000000000000000000000000..72c6b4bb1cd9b1d5d23dfed41f8dce69e4a84f98 --- /dev/null +++ b/lexicographically-minimum-string-after-removing-stars.json @@ -0,0 +1,31 @@ +{ + "id": 3445, + "name": "lexicographically-minimum-string-after-removing-stars", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/lexicographically-minimum-string-after-removing-stars/", + "date": "2024-05-26", + "task_description": "You are given a string `s`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters. While there is a `'*'`, do the following operation: Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its _left_. If there are several smallest characters, you can delete any of them. Return the lexicographically smallest resulting string after removing all `'*'` characters. **Example 1:** **Input:** s = \"aaba*\" **Output:** \"aab\" **Explanation:** We should delete one of the `'a'` characters with `'*'`. If we choose `s[3]`, `s` becomes the lexicographically smallest. **Example 2:** **Input:** s = \"abc\" **Output:** \"abc\" **Explanation:** There is no `'*'` in the string. **Constraints:** `1 <= s.length <= 105` `s` consists only of lowercase English letters and `'*'`. The input is generated such that it is possible to delete all `'*'` characters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aaba*\"", + "output": "\"aab\" " + }, + { + "label": "Example 2", + "input": "s = \"abc\"", + "output": "\"abc\" " + } + ], + "constraints": [ + "Delete the leftmost '*' and the smallest non-'*' character to its left. If there are several smallest characters, you can delete any of them.", + "1 <= s.length <= 105", + "s consists only of lowercase English letters and '*'.", + "The input is generated such that it is possible to delete all '*' characters." + ], + "python_template": "class Solution(object):\n def clearStars(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String clearStars(String s) {\n \n }\n}", + "metadata": { + "func_name": "clearStars" + } +} \ No newline at end of file diff --git a/lexicographically-smallest-beautiful-string.json b/lexicographically-smallest-beautiful-string.json new file mode 100644 index 0000000000000000000000000000000000000000..ad89f39856803a177244c4d2b108d8b8f66bf6c7 --- /dev/null +++ b/lexicographically-smallest-beautiful-string.json @@ -0,0 +1,33 @@ +{ + "id": 2687, + "name": "lexicographically-smallest-beautiful-string", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/lexicographically-smallest-beautiful-string/", + "date": "2023-04-23", + "task_description": "A string is **beautiful** if: It consists of the first `k` letters of the English lowercase alphabet. It does not contain any substring of length `2` or more which is a palindrome. You are given a beautiful string `s` of length `n` and a positive integer `k`. Return _the lexicographically smallest string of length _`n`_, which is larger than _`s`_ and is **beautiful**_. If there is no such string, return an empty string. A string `a` is lexicographically larger than a string `b` (of the same length) if in the first position where `a` and `b` differ, `a` has a character strictly larger than the corresponding character in `b`. For example, `\"abcd\"` is lexicographically larger than `\"abcc\"` because the first position they differ is at the fourth character, and `d` is greater than `c`. **Example 1:** ``` **Input:** s = \"abcz\", k = 26 **Output:** \"abda\" **Explanation:** The string \"abda\" is beautiful and lexicographically larger than the string \"abcz\". It can be proven that there is no string that is lexicographically larger than the string \"abcz\", beautiful, and lexicographically smaller than the string \"abda\". ``` **Example 2:** ``` **Input:** s = \"dc\", k = 4 **Output:** \"\" **Explanation:** It can be proven that there is no string that is lexicographically larger than the string \"dc\" and is beautiful. ``` **Constraints:** `1 <= n == s.length <= 105` `4 <= k <= 26` `s` is a beautiful string.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcz\", k = 26", + "output": "\"abda\" " + }, + { + "label": "Example 2", + "input": "s = \"dc\", k = 4", + "output": "\"\" " + } + ], + "constraints": [ + "It consists of the first k letters of the English lowercase alphabet.", + "It does not contain any substring of length 2 or more which is a palindrome.", + "For example, \"abcd\" is lexicographically larger than \"abcc\" because the first position they differ is at the fourth character, and d is greater than c.", + "1 <= n == s.length <= 105", + "4 <= k <= 26", + "s is a beautiful string." + ], + "python_template": "class Solution(object):\n def smallestBeautifulString(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String smallestBeautifulString(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "smallestBeautifulString" + } +} \ No newline at end of file diff --git a/lexicographically-smallest-palindrome.json b/lexicographically-smallest-palindrome.json new file mode 100644 index 0000000000000000000000000000000000000000..81d97f41dc8d29d31220d539157aed41035e3f20 --- /dev/null +++ b/lexicographically-smallest-palindrome.json @@ -0,0 +1,34 @@ +{ + "id": 2816, + "name": "lexicographically-smallest-palindrome", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/lexicographically-smallest-palindrome/", + "date": "2023-05-14", + "task_description": "You are given a string `s` consisting of **lowercase English letters**, and you are allowed to perform operations on it. In one operation, you can **replace** a character in `s` with another lowercase English letter. Your task is to make `s` a **palindrome** with the **minimum** **number** **of operations** possible. If there are **multiple palindromes** that can be made using the **minimum** number of operations, make the **lexicographically smallest** one. 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`. Return _the resulting palindrome string._ **Example 1:** ``` **Input:** s = \"egcfe\" **Output:** \"efcfe\" **Explanation:** The minimum number of operations to make \"egcfe\" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is \"efcfe\", by changing 'g'. ``` **Example 2:** ``` **Input:** s = \"abcd\" **Output:** \"abba\" **Explanation:** The minimum number of operations to make \"abcd\" a palindrome is 2, and the lexicographically smallest palindrome string we can get by modifying two characters is \"abba\". ``` **Example 3:** ``` **Input:** s = \"seven\" **Output:** \"neven\" **Explanation:** The minimum number of operations to make \"seven\" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is \"neven\". ``` **Constraints:** `1 <= s.length <= 1000` `s` consists of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"egcfe\"", + "output": "\"efcfe\" " + }, + { + "label": "Example 2", + "input": "s = \"abcd\"", + "output": "\"abba\" " + }, + { + "label": "Example 3", + "input": "s = \"seven\"", + "output": "\"neven\" " + } + ], + "constraints": [ + "1 <= s.length <= 1000", + "s consists of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def makeSmallestPalindrome(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String makeSmallestPalindrome(String s) {\n \n }\n}", + "metadata": { + "func_name": "makeSmallestPalindrome" + } +} \ No newline at end of file diff --git a/lexicographically-smallest-string-after-a-swap.json b/lexicographically-smallest-string-after-a-swap.json new file mode 100644 index 0000000000000000000000000000000000000000..d11b96b67df73e6bcc4d6282850c38c25e8e7f20 --- /dev/null +++ b/lexicographically-smallest-string-after-a-swap.json @@ -0,0 +1,29 @@ +{ + "id": 3484, + "name": "lexicographically-smallest-string-after-a-swap", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/", + "date": "2024-07-07", + "task_description": "Given a string `s` containing only digits, return the lexicographically smallest string that can be obtained after swapping **adjacent** digits in `s` with the same **parity** at most **once**. Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not. **Example 1:** **Input:** s = \"45320\" **Output:** \"43520\" **Explanation: ** `s[1] == '5'` and `s[2] == '3'` both have the same parity, and swapping them results in the lexicographically smallest string. **Example 2:** **Input:** s = \"001\" **Output:** \"001\" **Explanation:** There is no need to perform a swap because `s` is already the lexicographically smallest. **Constraints:** `2 <= s.length <= 100` `s` consists only of digits.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"45320\"", + "output": "\"43520\" " + }, + { + "label": "Example 2", + "input": "s = \"001\"", + "output": "\"001\" " + } + ], + "constraints": [ + "2 <= s.length <= 100", + "s consists only of digits." + ], + "python_template": "class Solution(object):\n def getSmallestString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String getSmallestString(String s) {\n \n }\n}", + "metadata": { + "func_name": "getSmallestString" + } +} \ No newline at end of file diff --git a/lexicographically-smallest-string-after-substring-operation.json b/lexicographically-smallest-string-after-substring-operation.json new file mode 100644 index 0000000000000000000000000000000000000000..68124778e20887c68fa3a9b4cf59d8574a51854f --- /dev/null +++ b/lexicographically-smallest-string-after-substring-operation.json @@ -0,0 +1,40 @@ +{ + "id": 2828, + "name": "lexicographically-smallest-string-after-substring-operation", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/lexicographically-smallest-string-after-substring-operation/", + "date": "2023-06-04", + "task_description": "Given a string `s` consisting of lowercase English letters. Perform the following operation: Select any non-empty substring then replace every letter of the substring with the preceding letter of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'. Return the **lexicographically smallest** string **after performing the operation**. **Example 1:** **Input:** s = \"cbabc\" **Output:** \"baabc\" **Explanation:** Perform the operation on the substring starting at index 0, and ending at index 1 inclusive. **Example 2:** **Input:** s = \"aa\" **Output:** \"az\" **Explanation:** Perform the operation on the last letter. **Example 3:** **Input:** s = \"acbbc\" **Output:** \"abaab\" **Explanation:** Perform the operation on the substring starting at index 1, and ending at index 4 inclusive. **Example 4:** **Input:** s = \"leetcode\" **Output:** \"kddsbncd\" **Explanation:** Perform the operation on the entire string. **Constraints:** `1 <= s.length <= 3 * 105` `s` consists of lowercase English letters", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"cbabc\"", + "output": "\"baabc\" " + }, + { + "label": "Example 2", + "input": "s = \"aa\"", + "output": "\"az\" " + }, + { + "label": "Example 3", + "input": "s = \"acbbc\"", + "output": "\"abaab\" " + }, + { + "label": "Example 4", + "input": "s = \"leetcode\"", + "output": "\"kddsbncd\" " + } + ], + "constraints": [ + "Select any non-empty substring then replace every letter of the substring with the preceding letter of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'.", + "1 <= s.length <= 3 * 105", + "s consists of lowercase English letters" + ], + "python_template": "class Solution(object):\n def smallestString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String smallestString(String s) {\n \n }\n}", + "metadata": { + "func_name": "smallestString" + } +} \ No newline at end of file diff --git a/longest-alternating-subarray.json b/longest-alternating-subarray.json new file mode 100644 index 0000000000000000000000000000000000000000..2ea4e8418a5389fab2f715e8b1bb10110aedec6f --- /dev/null +++ b/longest-alternating-subarray.json @@ -0,0 +1,32 @@ +{ + "id": 2870, + "name": "longest-alternating-subarray", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/longest-alternating-subarray/", + "date": "2023-06-24", + "task_description": "You are given a **0-indexed** integer array `nums`. A subarray `s` of length `m` is called **alternating** if: `m` is greater than `1`. `s1 = s0 + 1`. The **0-indexed** subarray `s` looks like `[s0, s1, s0, s1,...,s(m-1) % 2]`. In other words, `s1 - s0 = 1`, `s2 - s1 = -1`, `s3 - s2 = 1`, `s4 - s3 = -1`, and so on up to `s[m - 1] - s[m - 2] = (-1)m`. Return _the maximum length of all **alternating** subarrays present in _`nums` _or _`-1`_ if no such subarray exists__._ A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** **Input:** nums = [2,3,4,3,4] **Output:** 4 **Explanation:** The alternating subarrays are `[2, 3]`, `[3,4]`, `[3,4,3]`, and `[3,4,3,4]`. The longest of these is `[3,4,3,4]`, which is of length 4. **Example 2:** **Input:** nums = [4,5,6] **Output:** 2 **Explanation:** `[4,5]` and `[5,6]` are the only two alternating subarrays. They are both of length 2. **Constraints:** `2 <= nums.length <= 100` `1 <= nums[i] <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,4,3,4]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [4,5,6]", + "output": "2 " + } + ], + "constraints": [ + "m is greater than 1.", + "s1 = s0 + 1.", + "The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m.", + "2 <= nums.length <= 100", + "1 <= nums[i] <= 104" + ], + "python_template": "class Solution(object):\n def alternatingSubarray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int alternatingSubarray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "alternatingSubarray" + } +} \ No newline at end of file diff --git a/longest-binary-subsequence-less-than-or-equal-to-k.json b/longest-binary-subsequence-less-than-or-equal-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..c4018cef91033984eb96fe55aabe509fdc25926f --- /dev/null +++ b/longest-binary-subsequence-less-than-or-equal-to-k.json @@ -0,0 +1,33 @@ +{ + "id": 2395, + "name": "longest-binary-subsequence-less-than-or-equal-to-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/", + "date": "2022-06-12", + "task_description": "You are given a binary string `s` and a positive integer `k`. Return _the length of the **longest** subsequence of _`s`_ that makes up a **binary** number less than or equal to_ `k`. Note: The subsequence can contain **leading zeroes**. The empty string is considered to be equal to `0`. A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. **Example 1:** ``` **Input:** s = \"1001010\", k = 5 **Output:** 5 **Explanation:** The longest subsequence of s that makes up a binary number less than or equal to 5 is \"00010\", as this number is equal to 2 in decimal. Note that \"00100\" and \"00101\" are also possible, which are equal to 4 and 5 in decimal, respectively. The length of this subsequence is 5, so 5 is returned. ``` **Example 2:** ``` **Input:** s = \"00101001\", k = 1 **Output:** 6 **Explanation:** \"000001\" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal. The length of this subsequence is 6, so 6 is returned. ``` **Constraints:** `1 <= s.length <= 1000` `s[i]` is either `'0'` or `'1'`. `1 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"1001010\", k = 5", + "output": "5 " + }, + { + "label": "Example 2", + "input": "s = \"00101001\", k = 1", + "output": "6 " + } + ], + "constraints": [ + "The subsequence can contain leading zeroes.", + "The empty string is considered to be equal to 0.", + "A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.", + "1 <= s.length <= 1000", + "s[i] is either '0' or '1'.", + "1 <= k <= 109" + ], + "python_template": "class Solution(object):\n def longestSubsequence(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestSubsequence(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "longestSubsequence" + } +} \ No newline at end of file diff --git a/longest-even-odd-subarray-with-threshold.json b/longest-even-odd-subarray-with-threshold.json new file mode 100644 index 0000000000000000000000000000000000000000..065bc7abf8736c174986b7d7d7ca4319588b51e7 --- /dev/null +++ b/longest-even-odd-subarray-with-threshold.json @@ -0,0 +1,38 @@ +{ + "id": 2866, + "name": "longest-even-odd-subarray-with-threshold", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/longest-even-odd-subarray-with-threshold/", + "date": "2023-06-25", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `threshold`. Find the length of the **longest subarray** of `nums` starting at index `l` and ending at index `r` `(0 <= l <= r < nums.length)` that satisfies the following conditions: `nums[l] % 2 == 0` For all indices `i` in the range `[l, r - 1]`, `nums[i] % 2 != nums[i + 1] % 2` For all indices `i` in the range `[l, r]`, `nums[i] <= threshold` Return _an integer denoting the length of the longest such subarray._ **Note:** A **subarray** is a contiguous non-empty sequence of elements within an array. **Example 1:** ``` **Input:** nums = [3,2,5,4], threshold = 5 **Output:** 3 **Explanation:** In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. ``` **Example 2:** ``` **Input:** nums = [1,2], threshold = 2 **Output:** 1 **Explanation:** In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2]. It satisfies all the conditions and we can show that 1 is the maximum possible achievable length. ``` **Example 3:** ``` **Input:** nums = [2,3,4,5], threshold = 4 **Output:** 3 **Explanation:** In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4]. It satisfies all the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length. ``` **Constraints:** `1 <= nums.length <= 100 ` `1 <= nums[i] <= 100 ` `1 <= threshold <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,2,5,4], threshold = 5", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,2], threshold = 2", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [2,3,4,5], threshold = 4", + "output": "3 " + } + ], + "constraints": [ + "nums[l] % 2 == 0", + "For all indices i in the range [l, r - 1], nums[i] % 2 != nums[i + 1] % 2", + "For all indices i in the range [l, r], nums[i] <= threshold", + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100", + "1 <= threshold <= 100" + ], + "python_template": "class Solution(object):\n def longestAlternatingSubarray(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 longestAlternatingSubarray(int[] nums, int threshold) {\n \n }\n}", + "metadata": { + "func_name": "longestAlternatingSubarray" + } +} \ No newline at end of file diff --git a/longest-ideal-subsequence.json b/longest-ideal-subsequence.json new file mode 100644 index 0000000000000000000000000000000000000000..a2a3995ebeb2495fa2ca674296aca1e0475500b8 --- /dev/null +++ b/longest-ideal-subsequence.json @@ -0,0 +1,32 @@ +{ + "id": 2444, + "name": "longest-ideal-subsequence", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-ideal-subsequence/", + "date": "2022-07-31", + "task_description": "You are given a string `s` consisting of lowercase letters and an integer `k`. We call a string `t` **ideal** if the following conditions are satisfied: `t` is a **subsequence** of the string `s`. The absolute difference in the alphabet order of every two **adjacent** letters in `t` is less than or equal to `k`. Return _the length of the **longest** ideal string_. A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. **Note** that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of `'a'` and `'z'` is `25`, not `1`. **Example 1:** ``` **Input:** s = \"acfgbd\", k = 2 **Output:** 4 **Explanation:** The longest ideal string is \"acbd\". The length of this string is 4, so 4 is returned. Note that \"acfgbd\" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order. ``` **Example 2:** ``` **Input:** s = \"abcd\", k = 3 **Output:** 4 **Explanation:** The longest ideal string is \"abcd\". The length of this string is 4, so 4 is returned. ``` **Constraints:** `1 <= s.length <= 105` `0 <= k <= 25` `s` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"acfgbd\", k = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"abcd\", k = 3", + "output": "4 " + } + ], + "constraints": [ + "t is a subsequence of the string s.", + "The absolute difference in the alphabet order of every two adjacent letters in t is less than or equal to k.", + "1 <= s.length <= 105", + "0 <= k <= 25", + "s consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def longestIdealString(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestIdealString(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "longestIdealString" + } +} \ No newline at end of file diff --git a/longest-increasing-subsequence-ii.json b/longest-increasing-subsequence-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..ee11ddc03034121047387659838ecbb24c465c6a --- /dev/null +++ b/longest-increasing-subsequence-ii.json @@ -0,0 +1,36 @@ +{ + "id": 2526, + "name": "longest-increasing-subsequence-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/longest-increasing-subsequence-ii/", + "date": "2022-09-04", + "task_description": "You are given an integer array `nums` and an integer `k`. Find the longest subsequence of `nums` that meets the following requirements: The subsequence is **strictly increasing** and The difference between adjacent elements in the subsequence is **at most** `k`. Return_ the length of the **longest** **subsequence** that meets the requirements._ A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. **Example 1:** ``` **Input:** nums = [4,2,1,4,3,4,5,8,15], k = 3 **Output:** 5 **Explanation:** The longest subsequence that meets the requirements is [1,3,4,5,8]. The subsequence has a length of 5, so we return 5. Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3. ``` **Example 2:** ``` **Input:** nums = [7,4,5,1,8,12,4,7], k = 5 **Output:** 4 **Explanation:** The longest subsequence that meets the requirements is [4,5,8,12]. The subsequence has a length of 4, so we return 4. ``` **Example 3:** ``` **Input:** nums = [1,5], k = 1 **Output:** 1 **Explanation:** The longest subsequence that meets the requirements is [1]. The subsequence has a length of 1, so we return 1. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i], k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,2,1,4,3,4,5,8,15], k = 3", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [7,4,5,1,8,12,4,7], k = 5", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [1,5], k = 1", + "output": "1 " + } + ], + "constraints": [ + "The subsequence is strictly increasing and", + "The difference between adjacent elements in the subsequence is at most k.", + "1 <= nums.length <= 105", + "1 <= nums[i], k <= 105" + ], + "python_template": "class Solution(object):\n def lengthOfLIS(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 lengthOfLIS(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "lengthOfLIS" + } +} \ No newline at end of file diff --git a/longest-nice-subarray.json b/longest-nice-subarray.json new file mode 100644 index 0000000000000000000000000000000000000000..b51fac6284c1c9a492ac12134a1684715384361e --- /dev/null +++ b/longest-nice-subarray.json @@ -0,0 +1,29 @@ +{ + "id": 2478, + "name": "longest-nice-subarray", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-nice-subarray/", + "date": "2022-08-28", + "task_description": "You are given an array `nums` consisting of **positive** integers. We call a subarray of `nums` **nice** if the bitwise **AND** of every pair of elements that are in **different** positions in the subarray is equal to `0`. Return _the length of the **longest** nice subarray_. A **subarray** is a **contiguous** part of an array. **Note** that subarrays of length `1` are always considered nice. **Example 1:** ``` **Input:** nums = [1,3,8,48,10] **Output:** 3 **Explanation:** The longest nice subarray is [3,8,48]. This subarray satisfies the conditions: - 3 AND 8 = 0. - 3 AND 48 = 0. - 8 AND 48 = 0. It can be proven that no longer nice subarray can be obtained, so we return 3. ``` **Example 2:** ``` **Input:** nums = [3,1,5,11,13] **Output:** 1 **Explanation:** The length of the longest nice subarray is 1. Any subarray of length 1 can be chosen. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,8,48,10]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [3,1,5,11,13]", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def longestNiceSubarray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestNiceSubarray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "longestNiceSubarray" + } +} \ No newline at end of file diff --git a/longest-non-decreasing-subarray-from-two-arrays.json b/longest-non-decreasing-subarray-from-two-arrays.json new file mode 100644 index 0000000000000000000000000000000000000000..93a4f49b94efaeb3aa0ebd08af01b76fb1f3630b --- /dev/null +++ b/longest-non-decreasing-subarray-from-two-arrays.json @@ -0,0 +1,34 @@ +{ + "id": 2869, + "name": "longest-non-decreasing-subarray-from-two-arrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-non-decreasing-subarray-from-two-arrays/", + "date": "2023-07-02", + "task_description": "You are given two **0-indexed** integer arrays `nums1` and `nums2` of length `n`. Let's define another **0-indexed** integer array, `nums3`, of length `n`. For each index `i` in the range `[0, n - 1]`, you can assign either `nums1[i]` or `nums2[i]` to `nums3[i]`. Your task is to maximize the length of the **longest non-decreasing subarray** in `nums3` by choosing its values optimally. Return _an integer representing the length of the **longest non-decreasing** subarray in_ `nums3`. **Note: **A **subarray** is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums1 = [2,3,1], nums2 = [1,2,1] **Output:** 2 **Explanation: **One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]. The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2. We can show that 2 is the maximum achievable length. ``` **Example 2:** ``` **Input:** nums1 = [1,3,2,1], nums2 = [2,2,3,4] **Output:** 4 **Explanation:** One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]. The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length. ``` **Example 3:** ``` **Input:** nums1 = [1,1], nums2 = [2,2] **Output:** 2 **Explanation:** One way to construct nums3 is: nums3 = [nums1[0], nums1[1]] => [1,1]. The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length. ``` **Constraints:** `1 <= nums1.length == nums2.length == n <= 105` `1 <= nums1[i], nums2[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [2,3,1], nums2 = [1,2,1]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,3,2,1], nums2 = [2,2,3,4]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums1 = [1,1], nums2 = [2,2]", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums1.length == nums2.length == n <= 105", + "1 <= nums1[i], nums2[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxNonDecreasingLength(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 maxNonDecreasingLength(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "maxNonDecreasingLength" + } +} \ No newline at end of file diff --git a/longest-palindrome-by-concatenating-two-letter-words.json b/longest-palindrome-by-concatenating-two-letter-words.json new file mode 100644 index 0000000000000000000000000000000000000000..d3b9d244ffb694726a7a2a62821b511dbbc19e09 --- /dev/null +++ b/longest-palindrome-by-concatenating-two-letter-words.json @@ -0,0 +1,35 @@ +{ + "id": 2237, + "name": "longest-palindrome-by-concatenating-two-letter-words", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/", + "date": "2021-12-25", + "task_description": "You are given an array of strings `words`. Each element of `words` consists of **two** lowercase English letters. Create the **longest possible palindrome** by selecting some elements from `words` and concatenating them in **any order**. Each element can be selected **at most once**. Return _the **length** of the longest palindrome that you can create_. If it is impossible to create any palindrome, return `0`. A **palindrome** is a string that reads the same forward and backward. **Example 1:** ``` **Input:** words = [\"lc\",\"cl\",\"gg\"] **Output:** 6 **Explanation:** One longest palindrome is \"lc\" + \"gg\" + \"cl\" = \"lcggcl\", of length 6. Note that \"clgglc\" is another longest palindrome that can be created. ``` **Example 2:** ``` **Input:** words = [\"ab\",\"ty\",\"yt\",\"lc\",\"cl\",\"ab\"] **Output:** 8 **Explanation:** One longest palindrome is \"ty\" + \"lc\" + \"cl\" + \"yt\" = \"tylcclyt\", of length 8. Note that \"lcyttycl\" is another longest palindrome that can be created. ``` **Example 3:** ``` **Input:** words = [\"cc\",\"ll\",\"xx\"] **Output:** 2 **Explanation:** One longest palindrome is \"cc\", of length 2. Note that \"ll\" is another longest palindrome that can be created, and so is \"xx\". ``` **Constraints:** `1 <= words.length <= 105` `words[i].length == 2` `words[i]` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"lc\",\"cl\",\"gg\"]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "words = [\"ab\",\"ty\",\"yt\",\"lc\",\"cl\",\"ab\"]", + "output": "8 " + }, + { + "label": "Example 3", + "input": "words = [\"cc\",\"ll\",\"xx\"]", + "output": "2 " + } + ], + "constraints": [ + "1 <= words.length <= 105", + "words[i].length == 2", + "words[i] consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def longestPalindrome(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestPalindrome(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "longestPalindrome" + } +} \ No newline at end of file diff --git a/longest-palindromic-subsequence-after-at-most-k-operations.json b/longest-palindromic-subsequence-after-at-most-k-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..d492c8192c973efec7acb4efcaf5e28b113230f6 --- /dev/null +++ b/longest-palindromic-subsequence-after-at-most-k-operations.json @@ -0,0 +1,35 @@ +{ + "id": 3786, + "name": "longest-palindromic-subsequence-after-at-most-k-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-palindromic-subsequence-after-at-most-k-operations/", + "date": "2025-02-23", + "task_description": "You are given a string `s` and an integer `k`. In one operation, you can replace the character at any position with the next or previous letter in the alphabet (wrapping around so that `'a'` is after `'z'`). For example, replacing `'a'` with the next letter results in `'b'`, and replacing `'a'` with the previous letter results in `'z'`. Similarly, replacing `'z'` with the next letter results in `'a'`, and replacing `'z'` with the previous letter results in `'y'`. Return the length of the **longest palindromic subsequence** of `s` that can be obtained after performing **at most** `k` operations. **Example 1:** **Input:** s = \"abced\", k = 2 **Output:** 3 **Explanation:** Replace `s[1]` with the next letter, and `s` becomes `\"acced\"`. Replace `s[4]` with the previous letter, and `s` becomes `\"accec\"`. The subsequence `\"ccc\"` forms a palindrome of length 3, which is the maximum. **Example 2:** **Input:** s = \"aaazzz\", k = 4 **Output:** 6 **Explanation:** Replace `s[0]` with the previous letter, and `s` becomes `\"zaazzz\"`. Replace `s[4]` with the next letter, and `s` becomes `\"zaazaz\"`. Replace `s[3]` with the next letter, and `s` becomes `\"zaaaaz\"`. The entire string forms a palindrome of length 6. **Constraints:** `1 <= s.length <= 200` `1 <= k <= 200` `s` consists of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abced\", k = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \" aaazzz \", k = 4", + "output": "6 " + } + ], + "constraints": [ + "Replace s[1] with the next letter, and s becomes \"acced\".", + "Replace s[4] with the previous letter, and s becomes \"accec\".", + "Replace s[0] with the previous letter, and s becomes \"zaazzz\".", + "Replace s[4] with the next letter, and s becomes \"zaazaz\".", + "Replace s[3] with the next letter, and s becomes \"zaaaaz\".", + "1 <= s.length <= 200", + "1 <= k <= 200", + "s consists of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def longestPalindromicSubsequence(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestPalindromicSubsequence(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "longestPalindromicSubsequence" + } +} \ No newline at end of file diff --git a/longest-path-with-different-adjacent-characters.json b/longest-path-with-different-adjacent-characters.json new file mode 100644 index 0000000000000000000000000000000000000000..68257e7661c01ab644b26f10258c1fe79eb31362 --- /dev/null +++ b/longest-path-with-different-adjacent-characters.json @@ -0,0 +1,33 @@ +{ + "id": 2364, + "name": "longest-path-with-different-adjacent-characters", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/longest-path-with-different-adjacent-characters/", + "date": "2022-04-10", + "task_description": "You are given a **tree** (i.e. a connected, undirected graph that has no cycles) **rooted** at node `0` consisting of `n` nodes numbered from `0` to `n - 1`. The tree is represented by a **0-indexed** array `parent` of size `n`, where `parent[i]` is the parent of node `i`. Since node `0` is the root, `parent[0] == -1`. You are also given a string `s` of length `n`, where `s[i]` is the character assigned to node `i`. Return _the length of the **longest path** in the tree such that no pair of **adjacent** nodes on the path have the same character assigned to them._ **Example 1:** ``` **Input:** parent = [-1,0,0,1,1,2], s = \"abacbe\" **Output:** 3 **Explanation:** The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned. It can be proven that there is no longer path that satisfies the conditions. ``` **Example 2:** ``` **Input:** parent = [-1,0,0,0], s = \"aabc\" **Output:** 3 **Explanation:** The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned. ``` **Constraints:** `n == parent.length == s.length` `1 <= n <= 105` `0 <= parent[i] <= n - 1` for all `i >= 1` `parent[0] == -1` `parent` represents a valid tree. `s` consists of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "parent = [-1,0,0,1,1,2], s = \"abacbe\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "parent = [-1,0,0,0], s = \"aabc\"", + "output": "3 " + } + ], + "constraints": [ + "n == parent.length == s.length", + "1 <= n <= 105", + "0 <= parent[i] <= n - 1 for all i >= 1", + "parent[0] == -1", + "parent represents a valid tree.", + "s consists of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def longestPath(self, parent, s):\n \"\"\"\n :type parent: List[int]\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestPath(int[] parent, String s) {\n \n }\n}", + "metadata": { + "func_name": "longestPath" + } +} \ No newline at end of file diff --git a/longest-special-path-ii.json b/longest-special-path-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..41e375e31ccd1f20a4e77bc087ee3db0230f3e15 --- /dev/null +++ b/longest-special-path-ii.json @@ -0,0 +1,35 @@ +{ + "id": 3798, + "name": "longest-special-path-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/longest-special-path-ii/", + "date": "2025-03-01", + "task_description": "You are given an undirected tree rooted at node `0`, with `n` nodes numbered from `0` to `n - 1`. This is represented by a 2D array `edges` of length `n - 1`, where `edges[i] = [ui, vi, lengthi]` indicates an edge between nodes `ui` and `vi` with length `lengthi`. You are also given an integer array `nums`, where `nums[i]` represents the value at node `i`. A **special path** is defined as a **downward** path from an ancestor node to a descendant node in which all node values are **distinct**, except for **at most** one value that may appear twice. Return an array `result` of size 2, where `result[0]` is the length of the **longest** special path, and `result[1]` is the minimum number of nodes in all possible **longest** special paths. **Example 1:** **Input:** edges = [[0,1,1],[1,2,3],[1,3,1],[2,4,6],[4,7,2],[3,5,2],[3,6,5],[6,8,3]], nums = [1,1,0,3,1,2,1,1,0] **Output:** [9,3] **Explanation:** In the image below, nodes are colored by their corresponding values in `nums`. The longest special paths are `1 -> 2 -> 4` and `1 -> 3 -> 6 -> 8`, both having a length of 9. The minimum number of nodes across all longest special paths is 3. **Example 2:** **Input:** edges = [[1,0,3],[0,2,4],[0,3,5]], nums = [1,1,0,2] **Output:** [5,2] **Explanation:** The longest path is `0 -> 3` consisting of 2 nodes with a length of 5. **Constraints:** `2 <= n <= 5 * 104` `edges.length == n - 1` `edges[i].length == 3` `0 <= ui, vi < n` `1 <= lengthi <= 103` `nums.length == n` `0 <= nums[i] <= 5 * 104` The input is generated such that `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [[0,1,1],[1,2,3],[1,3,1],[2,4,6],[4,7,2],[3,5,2],[3,6,5],[6,8,3]], nums = [1,1,0,3,1,2,1,1,0]", + "output": "[9,3] " + }, + { + "label": "Example 2", + "input": "edges = [[1,0,3],[0,2,4],[0,3,5]], nums = [1,1,0,2]", + "output": "[5,2] " + } + ], + "constraints": [ + "2 <= n <= 5 * 104", + "edges.length == n - 1", + "edges[i].length == 3", + "0 <= ui, vi < n", + "1 <= lengthi <= 103", + "nums.length == n", + "0 <= nums[i] <= 5 * 104", + "The input is generated such that edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def longestSpecialPath(self, edges, nums):\n \"\"\"\n :type edges: List[List[int]]\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] longestSpecialPath(int[][] edges, int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "longestSpecialPath" + } +} \ No newline at end of file diff --git a/longest-special-path.json b/longest-special-path.json new file mode 100644 index 0000000000000000000000000000000000000000..a11abd5f2a6098e5ff7c41156f5ad53afd24d4dd --- /dev/null +++ b/longest-special-path.json @@ -0,0 +1,35 @@ +{ + "id": 3687, + "name": "longest-special-path", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/longest-special-path/", + "date": "2025-01-04", + "task_description": "You are given an undirected tree rooted at node `0` with `n` nodes numbered from `0` to `n - 1`, represented by a 2D array `edges` of length `n - 1`, where `edges[i] = [ui, vi, lengthi]` indicates an edge between nodes `ui` and `vi` with length `lengthi`. You are also given an integer array `nums`, where `nums[i]` represents the value at node `i`. A special path is defined as a downward path from an ancestor node to a descendant node such that all the values of the nodes in that path are unique. **Note** that a path may start and end at the same node. Return an array `result` of size 2, where `result[0]` is the length of the **longest** special path, and `result[1]` is the minimum number of nodes in all possible **longest** special paths. **Example 1:** **Input:** edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1] **Output:** [6,2] **Explanation:** In the image below, nodes are colored by their corresponding values in `nums` The longest special paths are `2 -> 5` and `0 -> 1 -> 4`, both having a length of 6. The minimum number of nodes across all longest special paths is 2. **Example 2:** **Input:** edges = [[1,0,8]], nums = [2,2] **Output:** [0,1] **Explanation:** The longest special paths are `0` and `1`, both having a length of 0. The minimum number of nodes across all longest special paths is 1. **Constraints:** `2 <= n <= 5 * 104` `edges.length == n - 1` `edges[i].length == 3` `0 <= ui, vi < n` `1 <= lengthi <= 103` `nums.length == n` `0 <= nums[i] <= 5 * 104` The input is generated such that `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1]", + "output": "[6,2] " + }, + { + "label": "Example 2", + "input": "edges = [[1,0,8]], nums = [2,2]", + "output": "[0,1] " + } + ], + "constraints": [ + "2 <= n <= 5 * 104", + "edges.length == n - 1", + "edges[i].length == 3", + "0 <= ui, vi < n", + "1 <= lengthi <= 103", + "nums.length == n", + "0 <= nums[i] <= 5 * 104", + "The input is generated such that edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def longestSpecialPath(self, edges, nums):\n \"\"\"\n :type edges: List[List[int]]\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] longestSpecialPath(int[][] edges, int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "longestSpecialPath" + } +} \ No newline at end of file diff --git a/longest-strictly-increasing-or-strictly-decreasing-subarray.json b/longest-strictly-increasing-or-strictly-decreasing-subarray.json new file mode 100644 index 0000000000000000000000000000000000000000..1f70ec8bdbb81e2efc163754c707e443307efda3 --- /dev/null +++ b/longest-strictly-increasing-or-strictly-decreasing-subarray.json @@ -0,0 +1,34 @@ +{ + "id": 3372, + "name": "longest-strictly-increasing-or-strictly-decreasing-subarray", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray/", + "date": "2024-03-31", + "task_description": "You are given an array of integers `nums`. Return _the length of the **longest** subarray of _`nums`_ which is either **strictly increasing** or **strictly decreasing**_. **Example 1:** **Input:** nums = [1,4,3,3,2] **Output:** 2 **Explanation:** The strictly increasing subarrays of `nums` are `[1]`, `[2]`, `[3]`, `[3]`, `[4]`, and `[1,4]`. The strictly decreasing subarrays of `nums` are `[1]`, `[2]`, `[3]`, `[3]`, `[4]`, `[3,2]`, and `[4,3]`. Hence, we return `2`. **Example 2:** **Input:** nums = [3,3,3,3] **Output:** 1 **Explanation:** The strictly increasing subarrays of `nums` are `[3]`, `[3]`, `[3]`, and `[3]`. The strictly decreasing subarrays of `nums` are `[3]`, `[3]`, `[3]`, and `[3]`. Hence, we return `1`. **Example 3:** **Input:** nums = [3,2,1] **Output:** 3 **Explanation:** The strictly increasing subarrays of `nums` are `[3]`, `[2]`, and `[1]`. The strictly decreasing subarrays of `nums` are `[3]`, `[2]`, `[1]`, `[3,2]`, `[2,1]`, and `[3,2,1]`. Hence, we return `3`. **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,3,3,2]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [3,3,3,3]", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [3,2,1]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def longestMonotonicSubarray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestMonotonicSubarray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "longestMonotonicSubarray" + } +} \ No newline at end of file diff --git a/longest-subarray-with-maximum-bitwise-and.json b/longest-subarray-with-maximum-bitwise-and.json new file mode 100644 index 0000000000000000000000000000000000000000..d4e068c43c4b68a881216ff5c6285d9e7954c823 --- /dev/null +++ b/longest-subarray-with-maximum-bitwise-and.json @@ -0,0 +1,30 @@ +{ + "id": 2503, + "name": "longest-subarray-with-maximum-bitwise-and", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/", + "date": "2022-09-18", + "task_description": "You are given an integer array `nums` of size `n`. Consider a **non-empty** subarray from `nums` that has the **maximum** possible **bitwise AND**. In other words, let `k` be the maximum value of the bitwise AND of **any** subarray of `nums`. Then, only subarrays with a bitwise AND equal to `k` should be considered. Return _the length of the **longest** such subarray_. The bitwise AND of an array is the bitwise AND of all the numbers in it. A **subarray** is a contiguous sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,2,3,3,2,2] **Output:** 2 **Explanation:** The maximum possible bitwise AND of a subarray is 3. The longest subarray with that value is [3,3], so we return 2. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** 1 **Explanation:** The maximum possible bitwise AND of a subarray is 4. The longest subarray with that value is [4], so we return 1. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,3,2,2]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4]", + "output": "1 " + } + ], + "constraints": [ + "In other words, let k be the maximum value of the bitwise AND of any subarray of nums. Then, only subarrays with a bitwise AND equal to k should be considered.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def longestSubarray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestSubarray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "longestSubarray" + } +} \ No newline at end of file diff --git a/longest-subsequence-with-decreasing-adjacent-difference.json b/longest-subsequence-with-decreasing-adjacent-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..e90040fe4716db079797e77738404ce776d2ac78 --- /dev/null +++ b/longest-subsequence-with-decreasing-adjacent-difference.json @@ -0,0 +1,34 @@ +{ + "id": 3716, + "name": "longest-subsequence-with-decreasing-adjacent-difference", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/longest-subsequence-with-decreasing-adjacent-difference/", + "date": "2024-12-21", + "task_description": "You are given an array of integers `nums`. Your task is to find the length of the **longest** subsequence `seq` of `nums`, such that the **absolute differences** between_ consecutive_ elements form a **non-increasing sequence** of integers. In other words, for a subsequence `seq0`, `seq1`, `seq2`, ..., `seqm` of `nums`, `|seq1 - seq0| >= |seq2 - seq1| >= ... >= |seqm - seqm - 1|`. Return the length of such a subsequence. **Example 1:** **Input:** nums = [16,6,3] **Output:** 3 **Explanation:** The longest subsequence is `[16, 6, 3]` with the absolute adjacent differences `[10, 3]`. **Example 2:** **Input:** nums = [6,5,3,4,2,1] **Output:** 4 **Explanation:** The longest subsequence is `[6, 4, 2, 1]` with the absolute adjacent differences `[2, 2, 1]`. **Example 3:** **Input:** nums = [10,20,10,19,10,20] **Output:** 5 **Explanation:** The longest subsequence is `[10, 20, 10, 19, 10]` with the absolute adjacent differences `[10, 10, 9, 9]`. **Constraints:** `2 <= nums.length <= 104` `1 <= nums[i] <= 300`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [16,6,3]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [6,5,3,4,2,1]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [10,20,10,19,10,20]", + "output": "5 " + } + ], + "constraints": [ + "2 <= nums.length <= 104", + "1 <= nums[i] <= 300" + ], + "python_template": "class Solution(object):\n def longestSubsequence(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int longestSubsequence(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "longestSubsequence" + } +} \ No newline at end of file diff --git a/longest-subsequence-with-limited-sum.json b/longest-subsequence-with-limited-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..52ff7884bf34ee2dae852c4c092b7b383466092f --- /dev/null +++ b/longest-subsequence-with-limited-sum.json @@ -0,0 +1,31 @@ +{ + "id": 2469, + "name": "longest-subsequence-with-limited-sum", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/longest-subsequence-with-limited-sum/", + "date": "2022-08-21", + "task_description": "You are given an integer array `nums` of length `n`, and an integer array `queries` of length `m`. Return _an array _`answer`_ of length _`m`_ where _`answer[i]`_ is the **maximum** size of a **subsequence** that you can take from _`nums`_ such that the **sum** of its elements is less than or equal to _`queries[i]`. A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. **Example 1:** ``` **Input:** nums = [4,5,2,1], queries = [3,10,21] **Output:** [2,3,4] **Explanation:** We answer the queries as follows: - The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2. - The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3. - The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4. ``` **Example 2:** ``` **Input:** nums = [2,3,4,5], queries = [1] **Output:** [0] **Explanation:** The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0. ``` **Constraints:** `n == nums.length` `m == queries.length` `1 <= n, m <= 1000` `1 <= nums[i], queries[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,5,2,1], queries = [3,10,21]", + "output": "[2,3,4] " + }, + { + "label": "Example 2", + "input": "nums = [2,3,4,5], queries = [1]", + "output": "[0] " + } + ], + "constraints": [ + "n == nums.length", + "m == queries.length", + "1 <= n, m <= 1000", + "1 <= nums[i], queries[i] <= 106" + ], + "python_template": "class Solution(object):\n def answerQueries(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[] answerQueries(int[] nums, int[] queries) {\n \n }\n}", + "metadata": { + "func_name": "answerQueries" + } +} \ No newline at end of file diff --git a/longest-substring-of-one-repeating-character.json b/longest-substring-of-one-repeating-character.json new file mode 100644 index 0000000000000000000000000000000000000000..3e89dd54cce0b0a55a1dee2ed55a27a24212e817 --- /dev/null +++ b/longest-substring-of-one-repeating-character.json @@ -0,0 +1,33 @@ +{ + "id": 2319, + "name": "longest-substring-of-one-repeating-character", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/longest-substring-of-one-repeating-character/", + "date": "2022-03-13", + "task_description": "You are given a **0-indexed** string `s`. You are also given a **0-indexed** string `queryCharacters` of length `k` and a **0-indexed** array of integer **indices** `queryIndices` of length `k`, both of which are used to describe `k` queries. The `ith` query updates the character in `s` at index `queryIndices[i]` to the character `queryCharacters[i]`. Return _an array_ `lengths` _of length _`k`_ where_ `lengths[i]` _is the **length** of the **longest substring** of _`s`_ consisting of **only one repeating** character **after** the_ `ith` _query__ is performed._ **Example 1:** ``` **Input:** s = \"babacc\", queryCharacters = \"bcb\", queryIndices = [1,3,3] **Output:** [3,3,4] **Explanation:** - 1st query updates s = \"b**b**bacc\". The longest substring consisting of one repeating character is \"bbb\" with length 3. - 2nd query updates s = \"bbb**c**cc\". The longest substring consisting of one repeating character can be \"bbb\" or \"ccc\" with length 3. - 3rd query updates s = \"bbb**b**cc\". The longest substring consisting of one repeating character is \"bbbb\" with length 4. Thus, we return [3,3,4]. ``` **Example 2:** ``` **Input:** s = \"abyzz\", queryCharacters = \"aa\", queryIndices = [2,1] **Output:** [2,3] **Explanation:** - 1st query updates s = \"ab**a**zz\". The longest substring consisting of one repeating character is \"zz\" with length 2. - 2nd query updates s = \"a**a**azz\". The longest substring consisting of one repeating character is \"aaa\" with length 3. Thus, we return [2,3]. ``` **Constraints:** `1 <= s.length <= 105` `s` consists of lowercase English letters. `k == queryCharacters.length == queryIndices.length` `1 <= k <= 105` `queryCharacters` consists of lowercase English letters. `0 <= queryIndices[i] < s.length`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"babacc\", queryCharacters = \"bcb\", queryIndices = [1,3,3]", + "output": "[3,3,4] " + }, + { + "label": "Example 2", + "input": "s = \"abyzz\", queryCharacters = \"aa\", queryIndices = [2,1]", + "output": "[2,3] " + } + ], + "constraints": [ + "1 <= s.length <= 105", + "s consists of lowercase English letters.", + "k == queryCharacters.length == queryIndices.length", + "1 <= k <= 105", + "queryCharacters consists of lowercase English letters.", + "0 <= queryIndices[i] < s.length" + ], + "python_template": "class Solution(object):\n def longestRepeating(self, s, queryCharacters, queryIndices):\n \"\"\"\n :type s: str\n :type queryCharacters: str\n :type queryIndices: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] longestRepeating(String s, String queryCharacters, int[] queryIndices) {\n \n }\n}", + "metadata": { + "func_name": "longestRepeating" + } +} \ No newline at end of file diff --git a/make-array-zero-by-subtracting-equal-amounts.json b/make-array-zero-by-subtracting-equal-amounts.json new file mode 100644 index 0000000000000000000000000000000000000000..8d2f59c85b7253ec6547e3bfcc8a55c045794b04 --- /dev/null +++ b/make-array-zero-by-subtracting-equal-amounts.json @@ -0,0 +1,31 @@ +{ + "id": 2436, + "name": "make-array-zero-by-subtracting-equal-amounts", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/", + "date": "2022-07-24", + "task_description": "You are given a non-negative integer array `nums`. In one operation, you must: Choose a positive integer `x` such that `x` is less than or equal to the **smallest non-zero** element in `nums`. Subtract `x` from every **positive** element in `nums`. Return _the **minimum** number of operations to make every element in _`nums`_ equal to _`0`. **Example 1:** ``` **Input:** nums = [1,5,0,3,5] **Output:** 3 **Explanation:** In the first operation, choose x = 1. Now, nums = [0,4,0,2,4]. In the second operation, choose x = 2. Now, nums = [0,2,0,0,2]. In the third operation, choose x = 2. Now, nums = [0,0,0,0,0]. ``` **Example 2:** ``` **Input:** nums = [0] **Output:** 0 **Explanation:** Each element in nums is already 0 so no operations are needed. ``` **Constraints:** `1 <= nums.length <= 100` `0 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,5,0,3,5]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [0]", + "output": "0 " + } + ], + "constraints": [ + "Choose a positive integer x such that x is less than or equal to the smallest non-zero element in nums.", + "Subtract x from every positive element in nums.", + "1 <= nums.length <= 100", + "0 <= nums[i] <= 100" + ], + "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/make-costs-of-paths-equal-in-a-binary-tree.json b/make-costs-of-paths-equal-in-a-binary-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..cc0e05653061cc555580b31dc4278c49bc8fd54c --- /dev/null +++ b/make-costs-of-paths-equal-in-a-binary-tree.json @@ -0,0 +1,33 @@ +{ + "id": 2780, + "name": "make-costs-of-paths-equal-in-a-binary-tree", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/", + "date": "2023-04-30", + "task_description": "You are given an integer `n` representing the number of nodes in a **perfect binary tree** consisting of nodes numbered from `1` to `n`. The root of the tree is node `1` and each node `i` in the tree has two children where the left child is the node `2 * i` and the right child is `2 * i + 1`. Each node in the tree also has a **cost** represented by a given **0-indexed** integer array `cost` of size `n` where `cost[i]` is the cost of node `i + 1`. You are allowed to **increment** the cost of **any** node by `1` **any** number of times. Return _the **minimum** number of increments you need to make the cost of paths from the root to each **leaf** node equal_. **Note**: A **perfect binary tree **is a tree where each node, except the leaf nodes, has exactly 2 children. The **cost of a path** is the sum of costs of nodes in the path. **Example 1:** ``` **Input:** n = 7, cost = [1,5,2,2,3,3,1] **Output:** 6 **Explanation:** We can do the following increments: - Increase the cost of node 4 one time. - Increase the cost of node 3 three times. - Increase the cost of node 7 two times. Each path from the root to a leaf will have a total cost of 9. The total increments we did is 1 + 3 + 2 = 6. It can be shown that this is the minimum answer we can achieve. ``` **Example 2:** ``` **Input:** n = 3, cost = [5,3,3] **Output:** 0 **Explanation:** The two paths already have equal total costs, so no increments are needed. ``` **Constraints:** `3 <= n <= 105` `n + 1` is a power of `2` `cost.length == n` `1 <= cost[i] <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 7, cost = [1,5,2,2,3,3,1]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "n = 3, cost = [5,3,3]", + "output": "0 " + } + ], + "constraints": [ + "A perfect binary tree is a tree where each node, except the leaf nodes, has exactly 2 children.", + "The cost of a path is the sum of costs of nodes in the path.", + "3 <= n <= 105", + "n + 1 is a power of 2", + "cost.length == n", + "1 <= cost[i] <= 104" + ], + "python_template": "class Solution(object):\n def minIncrements(self, n, cost):\n \"\"\"\n :type n: int\n :type cost: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minIncrements(int n, int[] cost) {\n \n }\n}", + "metadata": { + "func_name": "minIncrements" + } +} \ No newline at end of file diff --git a/make-k-subarray-sums-equal.json b/make-k-subarray-sums-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..27db617d0f86fa0df234c5fc8b29f4e02d7924d5 --- /dev/null +++ b/make-k-subarray-sums-equal.json @@ -0,0 +1,30 @@ +{ + "id": 2670, + "name": "make-k-subarray-sums-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/make-k-subarray-sums-equal/", + "date": "2023-03-18", + "task_description": "You are given a **0-indexed** integer array `arr` and an integer `k`. The array `arr` is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element. You can do the following operation any number of times: Pick any element from `arr` and increase or decrease it by `1`. Return _the minimum number of operations such that the sum of each **subarray** of length _`k`_ is equal_. A **subarray** is a contiguous part of the array. **Example 1:** ``` **Input:** arr = [1,4,1,3], k = 2 **Output:** 1 **Explanation:** we can do one operation on index 1 to make its value equal to 3. The array after the operation is [1,3,1,3] - Subarray starts at index 0 is [1, 3], and its sum is 4 - Subarray starts at index 1 is [3, 1], and its sum is 4 - Subarray starts at index 2 is [1, 3], and its sum is 4 - Subarray starts at index 3 is [3, 1], and its sum is 4 ``` **Example 2:** ``` **Input:** arr = [2,5,5,7], k = 3 **Output:** 5 **Explanation:** we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5. The array after the operations is [5,5,5,5] - Subarray starts at index 0 is [5, 5, 5], and its sum is 15 - Subarray starts at index 1 is [5, 5, 5], and its sum is 15 - Subarray starts at index 2 is [5, 5, 5], and its sum is 15 - Subarray starts at index 3 is [5, 5, 5], and its sum is 15 ``` **Constraints:** `1 <= k <= arr.length <= 105` `1 <= arr[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "arr = [1,4,1,3], k = 2", + "output": "1 " + }, + { + "label": "Example 2", + "input": "arr = [2,5,5,7], k = 3", + "output": "5 " + } + ], + "constraints": [ + "Pick any element from arr and increase or decrease it by 1.", + "1 <= k <= arr.length <= 105", + "1 <= arr[i] <= 109" + ], + "python_template": "class Solution(object):\n def makeSubKSumEqual(self, arr, k):\n \"\"\"\n :type arr: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long makeSubKSumEqual(int[] arr, int k) {\n \n }\n}", + "metadata": { + "func_name": "makeSubKSumEqual" + } +} \ No newline at end of file diff --git a/make-number-of-distinct-characters-equal.json b/make-number-of-distinct-characters-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..a50fbce2a1f03616fdb441a88c0302e5db6d9003 --- /dev/null +++ b/make-number-of-distinct-characters-equal.json @@ -0,0 +1,34 @@ +{ + "id": 2615, + "name": "make-number-of-distinct-characters-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/make-number-of-distinct-characters-equal/", + "date": "2023-01-01", + "task_description": "You are given two **0-indexed** strings `word1` and `word2`. A **move** consists of choosing two indices `i` and `j` such that `0 <= i < word1.length` and `0 <= j < word2.length` and swapping `word1[i]` with `word2[j]`. Return `true` _if it is possible to get the number of distinct characters in_ `word1` _and_ `word2` _to be equal with **exactly one** move. _Return `false` _otherwise_. **Example 1:** ``` **Input:** word1 = \"ac\", word2 = \"b\" **Output:** false **Explanation:** Any pair of swaps would yield two distinct characters in the first string, and one in the second string. ``` **Example 2:** ``` **Input:** word1 = \"abcc\", word2 = \"aab\" **Output:** true **Explanation:** We swap index 2 of the first string with index 0 of the second string. The resulting strings are word1 = \"abac\" and word2 = \"cab\", which both have 3 distinct characters. ``` **Example 3:** ``` **Input:** word1 = \"abcde\", word2 = \"fghij\" **Output:** true **Explanation:** Both resulting strings will have 5 distinct characters, regardless of which indices we swap. ``` **Constraints:** `1 <= word1.length, word2.length <= 105` `word1` and `word2` consist of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word1 = \"ac\", word2 = \"b\"", + "output": "false " + }, + { + "label": "Example 2", + "input": "word1 = \"abcc\", word2 = \"aab\"", + "output": "true " + }, + { + "label": "Example 3", + "input": "word1 = \"abcde\", word2 = \"fghij\"", + "output": "true " + } + ], + "constraints": [ + "1 <= word1.length, word2.length <= 105", + "word1 and word2 consist of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def isItPossible(self, word1, word2):\n \"\"\"\n :type word1: str\n :type word2: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean isItPossible(String word1, String word2) {\n \n }\n}", + "metadata": { + "func_name": "isItPossible" + } +} \ No newline at end of file diff --git a/make-string-a-subsequence-using-cyclic-increments.json b/make-string-a-subsequence-using-cyclic-increments.json new file mode 100644 index 0000000000000000000000000000000000000000..340e3a5b9b89bf9fa15ad272ac88ee0754b004a0 --- /dev/null +++ b/make-string-a-subsequence-using-cyclic-increments.json @@ -0,0 +1,35 @@ +{ + "id": 3018, + "name": "make-string-a-subsequence-using-cyclic-increments", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments/", + "date": "2023-08-05", + "task_description": "You are given two **0-indexed** strings `str1` and `str2`. In an operation, you select a **set** of indices in `str1`, and for each index `i` in the set, increment `str1[i]` to the next character **cyclically**. That is `'a'` becomes `'b'`, `'b'` becomes `'c'`, and so on, and `'z'` becomes `'a'`. Return `true` _if it is possible to make _`str2` _a subsequence of _`str1` _by performing the operation **at most once**_, _and_ `false` _otherwise_. **Note:** A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters. **Example 1:** ``` **Input:** str1 = \"abc\", str2 = \"ad\" **Output:** true **Explanation:** Select index 2 in str1. Increment str1[2] to become 'd'. Hence, str1 becomes \"abd\" and str2 is now a subsequence. Therefore, true is returned. ``` **Example 2:** ``` **Input:** str1 = \"zc\", str2 = \"ad\" **Output:** true **Explanation:** Select indices 0 and 1 in str1. Increment str1[0] to become 'a'. Increment str1[1] to become 'd'. Hence, str1 becomes \"ad\" and str2 is now a subsequence. Therefore, true is returned. ``` **Example 3:** ``` **Input:** str1 = \"ab\", str2 = \"d\" **Output:** false **Explanation:** In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. Therefore, false is returned. ``` **Constraints:** `1 <= str1.length <= 105` `1 <= str2.length <= 105` `str1` and `str2` consist of only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "str1 = \"abc\", str2 = \"ad\"", + "output": "true " + }, + { + "label": "Example 2", + "input": "str1 = \"zc\", str2 = \"ad\"", + "output": "true " + }, + { + "label": "Example 3", + "input": "str1 = \"ab\", str2 = \"d\"", + "output": "false " + } + ], + "constraints": [ + "1 <= str1.length <= 105", + "1 <= str2.length <= 105", + "str1 and str2 consist of only lowercase English letters." + ], + "python_template": "class Solution(object):\n def canMakeSubsequence(self, str1, str2):\n \"\"\"\n :type str1: str\n :type str2: str\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean canMakeSubsequence(String str1, String str2) {\n \n }\n}", + "metadata": { + "func_name": "canMakeSubsequence" + } +} \ No newline at end of file diff --git a/make-three-strings-equal.json b/make-three-strings-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..e7b69f103e8003d8e4acf8104840b184d0525731 --- /dev/null +++ b/make-three-strings-equal.json @@ -0,0 +1,29 @@ +{ + "id": 3207, + "name": "make-three-strings-equal", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/make-three-strings-equal/", + "date": "2023-11-12", + "task_description": "You are given three strings: `s1`, `s2`, and `s3`. In one operation you can choose one of these strings and delete its **rightmost** character. Note that you **cannot** completely empty a string. Return the _minimum number of operations_ required to make the strings equal_. _If it is impossible to make them equal, return `-1`. **Example 1:** **Input: **s1 = \"abc\", s2 = \"abb\", s3 = \"ab\" **Output: **2 **Explanation: **Deleting the rightmost character from both `s1` and `s2` will result in three equal strings. **Example 2:** **Input: **s1 = \"dac\", s2 = \"bac\", s3 = \"cac\" **Output: **-1 **Explanation:** Since the first letters of `s1` and `s2` differ, they cannot be made equal. **Constraints:** `1 <= s1.length, s2.length, s3.length <= 100` `s1`, `s2` and `s3` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s1 = \"abc\", s2 = \"abb\", s3 = \"ab\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s1 = \"dac\", s2 = \"bac\", s3 = \"cac\"", + "output": "-1 " + } + ], + "constraints": [ + "1 <= s1.length, s2.length, s3.length <= 100", + "s1, s2 and s3 consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def findMinimumOperations(self, s1, s2, s3):\n \"\"\"\n :type s1: str\n :type s2: str\n :type s3: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findMinimumOperations(String s1, String s2, String s3) {\n \n }\n}", + "metadata": { + "func_name": "findMinimumOperations" + } +} \ No newline at end of file diff --git a/manhattan-distances-of-all-arrangements-of-pieces.json b/manhattan-distances-of-all-arrangements-of-pieces.json new file mode 100644 index 0000000000000000000000000000000000000000..1fd2486b61069799886deed96963e783f7fd6072 --- /dev/null +++ b/manhattan-distances-of-all-arrangements-of-pieces.json @@ -0,0 +1,34 @@ +{ + "id": 3739, + "name": "manhattan-distances-of-all-arrangements-of-pieces", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/manhattan-distances-of-all-arrangements-of-pieces/", + "date": "2025-01-04", + "task_description": "You are given three integers `m`, `n`, and `k`. There is a rectangular grid of size `m × n` containing `k` identical pieces. Return the sum of Manhattan distances between every pair of pieces over all **valid arrangements** of pieces. A **valid arrangement** is a placement of all `k` pieces on the grid with **at most** one piece per cell. Since the answer may be very large, return it **modulo** `109 + 7`. The Manhattan Distance between two cells `(xi, yi)` and `(xj, yj)` is `|xi - xj| + |yi - yj|`. **Example 1:** **Input:** m = 2, n = 2, k = 2 **Output:** 8 **Explanation:** The valid arrangements of pieces on the board are: In the first 4 arrangements, the Manhattan distance between the two pieces is 1. In the last 2 arrangements, the Manhattan distance between the two pieces is 2. Thus, the total Manhattan distance across all valid arrangements is `1 + 1 + 1 + 1 + 2 + 2 = 8`. **Example 2:** **Input:** m = 1, n = 4, k = 3 **Output:** 20 **Explanation:** The valid arrangements of pieces on the board are: The first and last arrangements have a total Manhattan distance of `1 + 1 + 2 = 4`. The middle two arrangements have a total Manhattan distance of `1 + 2 + 3 = 6`. The total Manhattan distance between all pairs of pieces across all arrangements is `4 + 6 + 6 + 4 = 20`. **Constraints:** `1 <= m, n <= 105` `2 <= m * n <= 105` `2 <= k <= m * n`", + "test_case": [ + { + "label": "Example 1", + "input": "m = 2, n = 2, k = 2", + "output": "8 " + }, + { + "label": "Example 2", + "input": "m = 1, n = 4, k = 3", + "output": "20 " + } + ], + "constraints": [ + "In the first 4 arrangements, the Manhattan distance between the two pieces is 1.", + "In the last 2 arrangements, the Manhattan distance between the two pieces is 2.", + "The first and last arrangements have a total Manhattan distance of 1 + 1 + 2 = 4.", + "The middle two arrangements have a total Manhattan distance of 1 + 2 + 3 = 6.", + "1 <= m, n <= 105", + "2 <= m * n <= 105", + "2 <= k <= m * n" + ], + "python_template": "class Solution(object):\n def distanceSum(self, m, n, k):\n \"\"\"\n :type m: int\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int distanceSum(int m, int n, int k) {\n \n }\n}", + "metadata": { + "func_name": "distanceSum" + } +} \ No newline at end of file diff --git a/match-substring-after-replacement.json b/match-substring-after-replacement.json new file mode 100644 index 0000000000000000000000000000000000000000..8516485181f00831d9d175359fcca90d248b2bbf --- /dev/null +++ b/match-substring-after-replacement.json @@ -0,0 +1,39 @@ +{ + "id": 2393, + "name": "match-substring-after-replacement", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/match-substring-after-replacement/", + "date": "2022-05-28", + "task_description": "You are given two strings `s` and `sub`. You are also given a 2D character array `mappings` where `mappings[i] = [oldi, newi]` indicates that you may perform the following operation **any** number of times: **Replace** a character `oldi` of `sub` with `newi`. Each character in `sub` **cannot** be replaced more than once. Return `true`_ if it is possible to make _`sub`_ a substring of _`s`_ by replacing zero or more characters according to _`mappings`. Otherwise, return `false`. A **substring** is a contiguous non-empty sequence of characters within a string. **Example 1:** ``` **Input:** s = \"fool3e7bar\", sub = \"leet\", mappings = [[\"e\",\"3\"],[\"t\",\"7\"],[\"t\",\"8\"]] **Output:** true **Explanation:** Replace the first 'e' in sub with '3' and 't' in sub with '7'. Now sub = \"l3e7\" is a substring of s, so we return true. ``` **Example 2:** ``` **Input:** s = \"fooleetbar\", sub = \"f00l\", mappings = [[\"o\",\"0\"]] **Output:** false **Explanation:** The string \"f00l\" is not a substring of s and no replacements can be made. Note that we cannot replace '0' with 'o'. ``` **Example 3:** ``` **Input:** s = \"Fool33tbaR\", sub = \"leetd\", mappings = [[\"e\",\"3\"],[\"t\",\"7\"],[\"t\",\"8\"],[\"d\",\"b\"],[\"p\",\"b\"]] **Output:** true **Explanation:** Replace the first and second 'e' in sub with '3' and 'd' in sub with 'b'. Now sub = \"l33tb\" is a substring of s, so we return true. ``` **Constraints:** `1 <= sub.length <= s.length <= 5000` `0 <= mappings.length <= 1000` `mappings[i].length == 2` `oldi != newi` `s` and `sub` consist of uppercase and lowercase English letters and digits. `oldi` and `newi` are either uppercase or lowercase English letters or digits.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"fool3e7bar\", sub = \"leet\", mappings = [[\"e\",\"3\"],[\"t\",\"7\"],[\"t\",\"8\"]]", + "output": "true " + }, + { + "label": "Example 2", + "input": "s = \"fooleetbar\", sub = \"f00l\", mappings = [[\"o\",\"0\"]]", + "output": "false " + }, + { + "label": "Example 3", + "input": "s = \"Fool33tbaR\", sub = \"leetd\", mappings = [[\"e\",\"3\"],[\"t\",\"7\"],[\"t\",\"8\"],[\"d\",\"b\"],[\"p\",\"b\"]]", + "output": "true " + } + ], + "constraints": [ + "Replace a character oldi of sub with newi.", + "1 <= sub.length <= s.length <= 5000", + "0 <= mappings.length <= 1000", + "mappings[i].length == 2", + "oldi != newi", + "s and sub consist of uppercase and lowercase English letters and digits.", + "oldi and newi are either uppercase or lowercase English letters or digits." + ], + "python_template": "class Solution(object):\n def matchReplacement(self, s, sub, mappings):\n \"\"\"\n :type s: str\n :type sub: str\n :type mappings: List[List[str]]\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean matchReplacement(String s, String sub, char[][] mappings) {\n \n }\n}", + "metadata": { + "func_name": "matchReplacement" + } +} \ No newline at end of file diff --git a/matrix-similarity-after-cyclic-shifts.json b/matrix-similarity-after-cyclic-shifts.json new file mode 100644 index 0000000000000000000000000000000000000000..56c2159fe5a423de8ac90395256f1efe80f07c81 --- /dev/null +++ b/matrix-similarity-after-cyclic-shifts.json @@ -0,0 +1,38 @@ +{ + "id": 3215, + "name": "matrix-similarity-after-cyclic-shifts", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts/", + "date": "2023-11-19", + "task_description": "You are given an `m x n` integer matrix `mat` and an integer `k`. The matrix rows are 0-indexed. The following proccess happens `k` times: **Even-indexed** rows (0, 2, 4, ...) are cyclically shifted to the left. **Odd-indexed** rows (1, 3, 5, ...) are cyclically shifted to the right. Return `true` if the final modified matrix after `k` steps is identical to the original matrix, and `false` otherwise. **Example 1:** **Input:** mat = [[1,2,3],[4,5,6],[7,8,9]], k = 4 **Output:** false **Explanation:** In each step left shift is applied to rows 0 and 2 (even indices), and right shift to row 1 (odd index). **Example 2:** **Input:** mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2 **Output:** true **Explanation:** **Example 3:** **Input:** mat = [[2,2],[2,2]], k = 3 **Output:** true **Explanation:** As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same. **Constraints:** `1 <= mat.length <= 25` `1 <= mat[i].length <= 25` `1 <= mat[i][j] <= 25` `1 <= k <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "mat = [[1,2,3],[4,5,6],[7,8,9]], k = 4", + "output": "false " + }, + { + "label": "Example 2", + "input": "mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2", + "output": "true " + }, + { + "label": "Example 3", + "input": "mat = [[2,2],[2,2]], k = 3", + "output": "true " + } + ], + "constraints": [ + "Even-indexed rows (0, 2, 4, ...) are cyclically shifted to the left.", + "Odd-indexed rows (1, 3, 5, ...) are cyclically shifted to the right.", + "1 <= mat.length <= 25", + "1 <= mat[i].length <= 25", + "1 <= mat[i][j] <= 25", + "1 <= k <= 50" + ], + "python_template": "class Solution(object):\n def areSimilar(self, mat, k):\n \"\"\"\n :type mat: List[List[int]]\n :type k: int\n :rtype: bool\n \"\"\"\n ", + "java_template": "class Solution {\n public boolean areSimilar(int[][] mat, int k) {\n \n }\n}", + "metadata": { + "func_name": "areSimilar" + } +} \ No newline at end of file diff --git a/max-sum-of-a-pair-with-equal-sum-of-digits.json b/max-sum-of-a-pair-with-equal-sum-of-digits.json new file mode 100644 index 0000000000000000000000000000000000000000..ead410da53cccbeb91ee0e5b7832d21a911c7e01 --- /dev/null +++ b/max-sum-of-a-pair-with-equal-sum-of-digits.json @@ -0,0 +1,29 @@ +{ + "id": 2473, + "name": "max-sum-of-a-pair-with-equal-sum-of-digits", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/", + "date": "2022-07-10", + "task_description": "You are given a **0-indexed** array `nums` consisting of **positive** integers. You can choose two indices `i` and `j`, such that `i != j`, and the sum of digits of the number `nums[i]` is equal to that of `nums[j]`. Return the **maximum** value of_ _`nums[i] + nums[j]`_ _that you can obtain over all possible indices `i` and `j` that satisfy the conditions. If no such pair of indices exists, return -1. **Example 1:** ``` **Input:** nums = [18,43,36,13,7] **Output:** 54 **Explanation:** The pairs (i, j) that satisfy the conditions are: - (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54. - (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50. So the maximum sum that we can obtain is 54. ``` **Example 2:** ``` **Input:** nums = [10,12,19,14] **Output:** -1 **Explanation:** There are no two numbers that satisfy the conditions, so we return -1. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [18,43,36,13,7]", + "output": "54 " + }, + { + "label": "Example 2", + "input": "nums = [10,12,19,14]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumSum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumSum" + } +} \ No newline at end of file diff --git a/maximal-score-after-applying-k-operations.json b/maximal-score-after-applying-k-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..586a73ff74cad3620c557a7a439f9d3975eeb187 --- /dev/null +++ b/maximal-score-after-applying-k-operations.json @@ -0,0 +1,29 @@ +{ + "id": 2616, + "name": "maximal-score-after-applying-k-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximal-score-after-applying-k-operations/", + "date": "2023-01-01", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `k`. You have a **starting score** of `0`. In one **operation**: choose an index `i` such that `0 <= i < nums.length`, increase your **score** by `nums[i]`, and replace `nums[i]` with `ceil(nums[i] / 3)`. Return _the maximum possible **score** you can attain after applying **exactly**_ `k` _operations_. The ceiling function `ceil(val)` is the least integer greater than or equal to `val`. **Example 1:** ``` **Input:** nums = [10,10,10,10,10], k = 5 **Output:** 50 **Explanation:** Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50. ``` **Example 2:** ``` **Input:** nums = [1,10,3,3,3], k = 3 **Output:** 17 **Explanation: **You can do the following operations: Operation 1: Select i = 1, so nums becomes [1,**4**,3,3,3]. Your score increases by 10. Operation 2: Select i = 1, so nums becomes [1,**2**,3,3,3]. Your score increases by 4. Operation 3: Select i = 2, so nums becomes [1,2,**1**,3,3]. Your score increases by 3. The final score is 10 + 4 + 3 = 17. ``` **Constraints:** `1 <= nums.length, k <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [10,10,10,10,10], k = 5", + "output": "50 " + }, + { + "label": "Example 2", + "input": "nums = [1,10,3,3,3], k = 3", + "output": "17 " + } + ], + "constraints": [ + "1 <= nums.length, k <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxKelements(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 maxKelements(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxKelements" + } +} \ No newline at end of file diff --git a/maximize-area-of-square-hole-in-grid.json b/maximize-area-of-square-hole-in-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..2924d2b8be4f269eacd0f7a219be61f8d001178d --- /dev/null +++ b/maximize-area-of-square-hole-in-grid.json @@ -0,0 +1,40 @@ +{ + "id": 3214, + "name": "maximize-area-of-square-hole-in-grid", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-area-of-square-hole-in-grid/", + "date": "2023-11-11", + "task_description": "You are given the two integers, `n` and `m` and two integer arrays, `hBars` and `vBars`. The grid has `n + 2` horizontal and `m + 2` vertical bars, creating 1 x 1 unit cells. The bars are indexed starting from `1`. You can **remove** some of the bars in `hBars` from horizontal bars and some of the bars in `vBars` from vertical bars. Note that other bars are fixed and cannot be removed. Return an integer denoting the **maximum area** of a _square-shaped_ hole in the grid, after removing some bars (possibly none). **Example 1:** **Input: **n = 2, m = 1, hBars = [2,3], vBars = [2] **Output: **4 **Explanation:** The left image shows the initial grid formed by the bars. The horizontal bars are `[1,2,3,4]`, and the vertical bars are `[1,2,3]`. One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2. **Example 2:** **Input: **n = 1, m = 1, hBars = [2], vBars = [2] **Output: **4 **Explanation:** To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2. **Example 3:** **Input: **n = 2, m = 3, hBars = [2,3], vBars = [2,4] **Output: **4 **Explanation:** One way to get the maximum square-shaped hole is by removing horizontal bar 3, and vertical bar 4. **Constraints:** `1 <= n <= 109` `1 <= m <= 109` `1 <= hBars.length <= 100` `2 <= hBars[i] <= n + 1` `1 <= vBars.length <= 100` `2 <= vBars[i] <= m + 1` All values in `hBars` are distinct. All values in `vBars` are distinct.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 2, m = 1, hBars = [2,3], vBars = [2]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 1, m = 1, hBars = [2], vBars = [2]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "n = 2, m = 3, hBars = [2,3], vBars = [2,4]", + "output": "4 " + } + ], + "constraints": [ + "1 <= n <= 109", + "1 <= m <= 109", + "1 <= hBars.length <= 100", + "2 <= hBars[i] <= n + 1", + "1 <= vBars.length <= 100", + "2 <= vBars[i] <= m + 1", + "All values in hBars are distinct.", + "All values in vBars are distinct." + ], + "python_template": "class Solution(object):\n def maximizeSquareHoleArea(self, n, m, hBars, vBars):\n \"\"\"\n :type n: int\n :type m: int\n :type hBars: List[int]\n :type vBars: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {\n \n }\n}", + "metadata": { + "func_name": "maximizeSquareHoleArea" + } +} \ No newline at end of file diff --git a/maximize-consecutive-elements-in-an-array-after-modification.json b/maximize-consecutive-elements-in-an-array-after-modification.json new file mode 100644 index 0000000000000000000000000000000000000000..c32d7b994e55b1a4ccb52428358e64ba004e8e4f --- /dev/null +++ b/maximize-consecutive-elements-in-an-array-after-modification.json @@ -0,0 +1,29 @@ +{ + "id": 3298, + "name": "maximize-consecutive-elements-in-an-array-after-modification", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximize-consecutive-elements-in-an-array-after-modification/", + "date": "2024-02-03", + "task_description": "You are given a **0-indexed** array `nums` consisting of **positive** integers. Initially, you can increase the value of **any** element in the array by **at most** `1`. After that, you need to select **one or more** elements from the final array such that those elements are **consecutive** when sorted in increasing order. For example, the elements `[3, 4, 5]` are consecutive while `[3, 4, 6]` and `[1, 1, 2, 3]` are not. Return _the **maximum** number of elements that you can select_. **Example 1:** ``` **Input:** nums = [2,1,5,1,1] **Output:** 3 **Explanation:** We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1]. We select the elements [**3**,**1**,5,**2**,1] and we sort them to obtain [1,2,3], which are consecutive. It can be shown that we cannot select more than 3 consecutive elements. ``` **Example 2:** ``` **Input:** nums = [1,4,7,10] **Output:** 1 **Explanation:** The maximum consecutive elements that we can select is 1. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,5,1,1]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,4,7,10]", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maxSelectedElements(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxSelectedElements(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxSelectedElements" + } +} \ No newline at end of file diff --git a/maximize-greatness-of-an-array.json b/maximize-greatness-of-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..6502ee9a1195b978d515c9a4ec9fe844f09e54e7 --- /dev/null +++ b/maximize-greatness-of-an-array.json @@ -0,0 +1,29 @@ +{ + "id": 2664, + "name": "maximize-greatness-of-an-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-greatness-of-an-array/", + "date": "2023-03-04", + "task_description": "You are given a 0-indexed integer array `nums`. You are allowed to permute `nums` into a new array `perm` of your choosing. We define the **greatness** of `nums` be the number of indices `0 <= i < nums.length` for which `perm[i] > nums[i]`. Return _the **maximum** possible greatness you can achieve after permuting_ `nums`. **Example 1:** ``` **Input:** nums = [1,3,5,2,1,3,1] **Output:** 4 **Explanation:** One of the optimal rearrangements is perm = [2,5,1,3,3,1,1]. At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4. ``` **Example 2:** ``` **Input:** nums = [1,2,3,4] **Output:** 3 **Explanation:** We can prove the optimal perm is [2,3,4,1]. At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,5,2,1,3,1]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximizeGreatness(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximizeGreatness(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximizeGreatness" + } +} \ No newline at end of file diff --git a/maximize-happiness-of-selected-children.json b/maximize-happiness-of-selected-children.json new file mode 100644 index 0000000000000000000000000000000000000000..f5156db7346c29745af29415ecb3da97e6c7e1d2 --- /dev/null +++ b/maximize-happiness-of-selected-children.json @@ -0,0 +1,35 @@ +{ + "id": 3351, + "name": "maximize-happiness-of-selected-children", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-happiness-of-selected-children/", + "date": "2024-03-03", + "task_description": "You are given an array `happiness` of length `n`, and a **positive** integer `k`. There are `n` children standing in a queue, where the `ith` child has **happiness value** `happiness[i]`. You want to select `k` children from these `n` children in `k` turns. In each turn, when you select a child, the **happiness value** of all the children that have **not** been selected till now decreases by `1`. Note that the happiness value **cannot** become negative and gets decremented **only** if it is positive. Return _the **maximum** sum of the happiness values of the selected children you can achieve by selecting _`k` _children_. **Example 1:** ``` **Input:** happiness = [1,2,3], k = 2 **Output:** 4 **Explanation:** We can pick 2 children in the following way: - Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. - Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. The sum of the happiness values of the selected children is 3 + 1 = 4. ``` **Example 2:** ``` **Input:** happiness = [1,1,1,1], k = 2 **Output:** 1 **Explanation:** We can pick 2 children in the following way: - Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. - Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. The sum of the happiness values of the selected children is 1 + 0 = 1. ``` **Example 3:** ``` **Input:** happiness = [2,3,4,5], k = 1 **Output:** 5 **Explanation:** We can pick 1 child in the following way: - Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. The sum of the happiness values of the selected children is 5. ``` **Constraints:** `1 <= n == happiness.length <= 2 * 105` `1 <= happiness[i] <= 108` `1 <= k <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "happiness = [1,2,3], k = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "happiness = [1,1,1,1], k = 2", + "output": "1 " + }, + { + "label": "Example 3", + "input": "happiness = [2,3,4,5], k = 1", + "output": "5 " + } + ], + "constraints": [ + "1 <= n == happiness.length <= 2 * 105", + "1 <= happiness[i] <= 108", + "1 <= k <= n" + ], + "python_template": "class Solution(object):\n def maximumHappinessSum(self, happiness, k):\n \"\"\"\n :type happiness: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumHappinessSum(int[] happiness, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumHappinessSum" + } +} \ No newline at end of file diff --git a/maximize-number-of-subsequences-in-a-string.json b/maximize-number-of-subsequences-in-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..60e9de557d1cfdd43ec1b9bd139142836d0539ef --- /dev/null +++ b/maximize-number-of-subsequences-in-a-string.json @@ -0,0 +1,30 @@ +{ + "id": 2309, + "name": "maximize-number-of-subsequences-in-a-string", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-number-of-subsequences-in-a-string/", + "date": "2022-03-05", + "task_description": "You are given a **0-indexed** string `text` and another **0-indexed** string `pattern` of length `2`, both of which consist of only lowercase English letters. You can add **either** `pattern[0]` **or** `pattern[1]` anywhere in `text` **exactly once**. Note that the character can be added even at the beginning or at the end of `text`. Return _the **maximum** number of times_ `pattern` _can occur as a **subsequence** of the modified _`text`. A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. **Example 1:** ``` **Input:** text = \"abdcdbc\", pattern = \"ac\" **Output:** 4 **Explanation:** If we add pattern[0] = 'a' in between text[1] and text[2], we get \"ab**a**dcdbc\". Now, the number of times \"ac\" occurs as a subsequence is 4. Some other strings which have 4 subsequences \"ac\" after adding a character to text are \"**a**abdcdbc\" and \"abd**a**cdbc\". However, strings such as \"abdc**a**dbc\", \"abd**c**cdbc\", and \"abdcdbc**c**\", although obtainable, have only 3 subsequences \"ac\" and are thus suboptimal. It can be shown that it is not possible to get more than 4 subsequences \"ac\" by adding only one character. ``` **Example 2:** ``` **Input:** text = \"aabb\", pattern = \"ab\" **Output:** 6 **Explanation:** Some of the strings which can be obtained from text and have 6 subsequences \"ab\" are \"**a**aabb\", \"aa**a**bb\", and \"aab**b**b\". ``` **Constraints:** `1 <= text.length <= 105` `pattern.length == 2` `text` and `pattern` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "text = \"abdcdbc\", pattern = \"ac\"", + "output": "4 " + }, + { + "label": "Example 2", + "input": "text = \"aabb\", pattern = \"ab\"", + "output": "6 " + } + ], + "constraints": [ + "1 <= text.length <= 105", + "pattern.length == 2", + "text and pattern consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def maximumSubsequenceCount(self, text, pattern):\n \"\"\"\n :type text: str\n :type pattern: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumSubsequenceCount(String text, String pattern) {\n \n }\n}", + "metadata": { + "func_name": "maximumSubsequenceCount" + } +} \ No newline at end of file diff --git a/maximize-score-of-numbers-in-ranges.json b/maximize-score-of-numbers-in-ranges.json new file mode 100644 index 0000000000000000000000000000000000000000..9bc3c9badb7ab7e215e98de7cd5968ceb591ad9a --- /dev/null +++ b/maximize-score-of-numbers-in-ranges.json @@ -0,0 +1,30 @@ +{ + "id": 3485, + "name": "maximize-score-of-numbers-in-ranges", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/", + "date": "2024-09-01", + "task_description": "You are given an array of integers `start` and an integer `d`, representing `n` intervals `[start[i], start[i] + d]`. You are asked to choose `n` integers where the `ith` integer must belong to the `ith` interval. The **score** of the chosen integers is defined as the **minimum** absolute difference between any two integers that have been chosen. Return the **maximum** _possible score_ of the chosen integers. **Example 1:** **Input:** start = [6,0,3], d = 2 **Output:** 4 **Explanation:** The maximum possible score can be obtained by choosing integers: 8, 0, and 4. The score of these chosen integers is `min(|8 - 0|, |8 - 4|, |0 - 4|)` which equals 4. **Example 2:** **Input:** start = [2,6,13,13], d = 5 **Output:** 5 **Explanation:** The maximum possible score can be obtained by choosing integers: 2, 7, 13, and 18. The score of these chosen integers is `min(|2 - 7|, |2 - 13|, |2 - 18|, |7 - 13|, |7 - 18|, |13 - 18|)` which equals 5. **Constraints:** `2 <= start.length <= 105` `0 <= start[i] <= 109` `0 <= d <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "start = [6,0,3], d = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "start = [2,6,13,13], d = 5", + "output": "5 " + } + ], + "constraints": [ + "2 <= start.length <= 105", + "0 <= start[i] <= 109", + "0 <= d <= 109" + ], + "python_template": "class Solution(object):\n def maxPossibleScore(self, start, d):\n \"\"\"\n :type start: List[int]\n :type d: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxPossibleScore(int[] start, int d) {\n \n }\n}", + "metadata": { + "func_name": "maxPossibleScore" + } +} \ No newline at end of file diff --git a/maximize-subarray-sum-after-removing-all-occurrences-of-one-element.json b/maximize-subarray-sum-after-removing-all-occurrences-of-one-element.json new file mode 100644 index 0000000000000000000000000000000000000000..263db08a65725a1765210d1594cb06e75b6f6330 --- /dev/null +++ b/maximize-subarray-sum-after-removing-all-occurrences-of-one-element.json @@ -0,0 +1,36 @@ +{ + "id": 3688, + "name": "maximize-subarray-sum-after-removing-all-occurrences-of-one-element", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximize-subarray-sum-after-removing-all-occurrences-of-one-element/", + "date": "2024-12-21", + "task_description": "You are given an integer array `nums`. You can do the following operation on the array **at most** once: Choose **any** integer `x` such that `nums` remains **non-empty** on removing all occurrences of `x`. Remove **all** occurrences of `x` from the array. Return the **maximum** subarray sum across **all** possible resulting arrays. **Example 1:** **Input:** nums = [-3,2,-2,-1,3,-2,3] **Output:** 7 **Explanation:** We can have the following arrays after at most one operation: The original array is `nums = [-3, 2, -2, -1, **3, -2, 3**]`. The maximum subarray sum is `3 + (-2) + 3 = 4`. Deleting all occurences of `x = -3` results in `nums = [2, -2, -1, **3, -2, 3**]`. The maximum subarray sum is `3 + (-2) + 3 = 4`. Deleting all occurences of `x = -2` results in `nums = [-3, **2, -1, 3, 3**]`. The maximum subarray sum is `2 + (-1) + 3 + 3 = 7`. Deleting all occurences of `x = -1` results in `nums = [-3, 2, -2, **3, -2, 3**]`. The maximum subarray sum is `3 + (-2) + 3 = 4`. Deleting all occurences of `x = 3` results in `nums = [-3, **2**, -2, -1, -2]`. The maximum subarray sum is 2. The output is `max(4, 4, 7, 4, 2) = 7`. **Example 2:** **Input:** nums = [1,2,3,4] **Output:** 10 **Explanation:** It is optimal to not perform any operations. **Constraints:** `1 <= nums.length <= 105` `-106 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [-3,2,-2,-1,3,-2,3]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,4]", + "output": "10 " + } + ], + "constraints": [ + "Choose any integer x such that nums remains non-empty on removing all occurrences of x.", + "Remove all occurrences of x from the array.", + "The original array is nums = [-3, 2, -2, -1, 3, -2, 3]. The maximum subarray sum is 3 + (-2) + 3 = 4.", + "Deleting all occurences of x = -3 results in nums = [2, -2, -1, 3, -2, 3]. The maximum subarray sum is 3 + (-2) + 3 = 4.", + "Deleting all occurences of x = -2 results in nums = [-3, 2, -1, 3, 3]. The maximum subarray sum is 2 + (-1) + 3 + 3 = 7.", + "Deleting all occurences of x = -1 results in nums = [-3, 2, -2, 3, -2, 3]. The maximum subarray sum is 3 + (-2) + 3 = 4.", + "Deleting all occurences of x = 3 results in nums = [-3, 2, -2, -1, -2]. The maximum subarray sum is 2.", + "1 <= nums.length <= 105", + "-106 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maxSubarraySum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxSubarraySum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxSubarraySum" + } +} \ No newline at end of file diff --git a/maximize-subarrays-after-removing-one-conflicting-pair.json b/maximize-subarrays-after-removing-one-conflicting-pair.json new file mode 100644 index 0000000000000000000000000000000000000000..e637c34a5e6cf7dbf4bb16ad13f762e4e280b1cb --- /dev/null +++ b/maximize-subarrays-after-removing-one-conflicting-pair.json @@ -0,0 +1,38 @@ +{ + "id": 3789, + "name": "maximize-subarrays-after-removing-one-conflicting-pair", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximize-subarrays-after-removing-one-conflicting-pair/", + "date": "2025-03-02", + "task_description": "You are given an integer `n` which represents an array `nums` containing the numbers from 1 to `n` in order. Additionally, you are given a 2D array `conflictingPairs`, where `conflictingPairs[i] = [a, b]` indicates that `a` and `b` form a conflicting pair. Remove **exactly** one element from `conflictingPairs`. Afterward, count the number of non-empty subarrays of `nums` which do not contain both `a` and `b` for any remaining conflicting pair `[a, b]`. Return the **maximum** number of subarrays possible after removing **exactly** one conflicting pair. **Example 1:** **Input:** n = 4, conflictingPairs = [[2,3],[1,4]] **Output:** 9 **Explanation:** Remove `[2, 3]` from `conflictingPairs`. Now, `conflictingPairs = [[1, 4]]`. There are 9 subarrays in `nums` where `[1, 4]` do not appear together. They are `[1]`, `[2]`, `[3]`, `[4]`, `[1, 2]`, `[2, 3]`, `[3, 4]`, `[1, 2, 3]` and `[2, 3, 4]`. The maximum number of subarrays we can achieve after removing one element from `conflictingPairs` is 9. **Example 2:** **Input:** n = 5, conflictingPairs = [[1,2],[2,5],[3,5]] **Output:** 12 **Explanation:** Remove `[1, 2]` from `conflictingPairs`. Now, `conflictingPairs = [[2, 5], [3, 5]]`. There are 12 subarrays in `nums` where `[2, 5]` and `[3, 5]` do not appear together. The maximum number of subarrays we can achieve after removing one element from `conflictingPairs` is 12. **Constraints:** `2 <= n <= 105` `1 <= conflictingPairs.length <= 2 * n` `conflictingPairs[i].length == 2` `1 <= conflictingPairs[i][j] <= n` `conflictingPairs[i][0] != conflictingPairs[i][1]`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, conflictingPairs = [[2,3],[1,4]]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]", + "output": "12 " + } + ], + "constraints": [ + "Remove [2, 3] from conflictingPairs. Now, conflictingPairs = [[1, 4]].", + "There are 9 subarrays in nums where [1, 4] do not appear together. They are [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3] and [2, 3, 4].", + "The maximum number of subarrays we can achieve after removing one element from conflictingPairs is 9.", + "Remove [1, 2] from conflictingPairs. Now, conflictingPairs = [[2, 5], [3, 5]].", + "There are 12 subarrays in nums where [2, 5] and [3, 5] do not appear together.", + "The maximum number of subarrays we can achieve after removing one element from conflictingPairs is 12.", + "2 <= n <= 105", + "1 <= conflictingPairs.length <= 2 * n", + "conflictingPairs[i].length == 2", + "1 <= conflictingPairs[i][j] <= n", + "conflictingPairs[i][0] != conflictingPairs[i][1]" + ], + "python_template": "class Solution(object):\n def maxSubarrays(self, n, conflictingPairs):\n \"\"\"\n :type n: int\n :type conflictingPairs: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxSubarrays(int n, int[][] conflictingPairs) {\n \n }\n}", + "metadata": { + "func_name": "maxSubarrays" + } +} \ No newline at end of file diff --git a/maximize-the-minimum-game-score.json b/maximize-the-minimum-game-score.json new file mode 100644 index 0000000000000000000000000000000000000000..41f0d840b9a532964c076c8a504c5084da37ed35 --- /dev/null +++ b/maximize-the-minimum-game-score.json @@ -0,0 +1,32 @@ +{ + "id": 3762, + "name": "maximize-the-minimum-game-score", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximize-the-minimum-game-score/", + "date": "2025-02-02", + "task_description": "You are given an array `points` of size `n` and an integer `m`. There is another array `gameScore` of size `n`, where `gameScore[i]` represents the score achieved at the `ith` game. Initially, `gameScore[i] == 0` for all `i`. You start at index -1, which is outside the array (before the first position at index 0). You can make **at most** `m` moves. In each move, you can either: Increase the index by 1 and add `points[i]` to `gameScore[i]`. Decrease the index by 1 and add `points[i]` to `gameScore[i]`. **Note** that the index must always remain within the bounds of the array after the first move. Return the **maximum possible minimum** value in `gameScore` after **at most** `m` moves. **Example 1:** **Input:** points = [2,4], m = 3 **Output:** 4 **Explanation:** Initially, index `i = -1` and `gameScore = [0, 0]`. Move Index gameScore Increase `i` 0 `[2, 0]` Increase `i` 1 `[2, 4]` Decrease `i` 0 `[4, 4]` The minimum value in `gameScore` is 4, and this is the maximum possible minimum among all configurations. Hence, 4 is the output. **Example 2:** **Input:** points = [1,2,3], m = 5 **Output:** 2 **Explanation:** Initially, index `i = -1` and `gameScore = [0, 0, 0]`. Move Index gameScore Increase `i` 0 `[1, 0, 0]` Increase `i` 1 `[1, 2, 0]` Decrease `i` 0 `[2, 2, 0]` Increase `i` 1 `[2, 4, 0]` Increase `i` 2 `[2, 4, 3]` The minimum value in `gameScore` is 2, and this is the maximum possible minimum among all configurations. Hence, 2 is the output. **Constraints:** `2 <= n == points.length <= 5 * 104` `1 <= points[i] <= 106` `1 <= m <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "points = [2,4], m = 3", + "output": "4 " + }, + { + "label": "Example 2", + "input": "points = [1,2,3], m = 5", + "output": "2 " + } + ], + "constraints": [ + "Increase the index by 1 and add points[i] to gameScore[i].", + "Decrease the index by 1 and add points[i] to gameScore[i].", + "2 <= n == points.length <= 5 * 104", + "1 <= points[i] <= 106", + "1 <= m <= 109" + ], + "python_template": "class Solution(object):\n def maxScore(self, points, m):\n \"\"\"\n :type points: List[int]\n :type m: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxScore(int[] points, int m) {\n \n }\n}", + "metadata": { + "func_name": "maxScore" + } +} \ No newline at end of file diff --git a/maximize-the-minimum-powered-city.json b/maximize-the-minimum-powered-city.json new file mode 100644 index 0000000000000000000000000000000000000000..8f912c0aa67f15c75fb7e236afec8c05d65a528e --- /dev/null +++ b/maximize-the-minimum-powered-city.json @@ -0,0 +1,33 @@ +{ + "id": 2618, + "name": "maximize-the-minimum-powered-city", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximize-the-minimum-powered-city/", + "date": "2022-12-24", + "task_description": "You are given a **0-indexed** integer array `stations` of length `n`, where `stations[i]` represents the number of power stations in the `ith` city. Each power station can provide power to every city in a fixed **range**. In other words, if the range is denoted by `r`, then a power station at city `i` can provide power to all cities `j` such that `|i - j| <= r` and `0 <= i, j <= n - 1`. Note that `|x|` denotes **absolute** value. For example, `|7 - 5| = 2` and `|3 - 10| = 7`. The **power** of a city is the total number of power stations it is being provided power from. The government has sanctioned building `k` more power stations, each of which can be built in any city, and have the same range as the pre-existing ones. Given the two integers `r` and `k`, return _the **maximum possible minimum power** of a city, if the additional power stations are built optimally._ **Note** that you can build the `k` power stations in multiple cities. **Example 1:** ``` **Input:** stations = [1,2,4,5,0], r = 1, k = 2 **Output:** 5 **Explanation:** One of the optimal ways is to install both the power stations at city 1. So stations will become [1,4,4,5,0]. - City 0 is provided by 1 + 4 = 5 power stations. - City 1 is provided by 1 + 4 + 4 = 9 power stations. - City 2 is provided by 4 + 4 + 5 = 13 power stations. - City 3 is provided by 5 + 4 = 9 power stations. - City 4 is provided by 5 + 0 = 5 power stations. So the minimum power of a city is 5. Since it is not possible to obtain a larger power, we return 5. ``` **Example 2:** ``` **Input:** stations = [4,4,4,4], r = 0, k = 3 **Output:** 4 **Explanation:** It can be proved that we cannot make the minimum power of a city greater than 4. ``` **Constraints:** `n == stations.length` `1 <= n <= 105` `0 <= stations[i] <= 105` `0 <= r <= n - 1` `0 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "stations = [1,2,4,5,0], r = 1, k = 2", + "output": "5 " + }, + { + "label": "Example 2", + "input": "stations = [4,4,4,4], r = 0, k = 3", + "output": "4 " + } + ], + "constraints": [ + "Note that |x| denotes absolute value. For example, |7 - 5| = 2 and |3 - 10| = 7.", + "n == stations.length", + "1 <= n <= 105", + "0 <= stations[i] <= 105", + "0 <= r <= n - 1", + "0 <= k <= 109" + ], + "python_template": "class Solution(object):\n def maxPower(self, stations, r, k):\n \"\"\"\n :type stations: List[int]\n :type r: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxPower(int[] stations, int r, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxPower" + } +} \ No newline at end of file diff --git a/maximize-the-number-of-partitions-after-operations.json b/maximize-the-number-of-partitions-after-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..1b1464e14fb2621a3034d3febb33fa2ccef833c5 --- /dev/null +++ b/maximize-the-number-of-partitions-after-operations.json @@ -0,0 +1,37 @@ +{ + "id": 3233, + "name": "maximize-the-number-of-partitions-after-operations", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations/", + "date": "2023-12-31", + "task_description": "You are given a string `s` and an integer `k`. First, you are allowed to change **at most** **one** index in `s` to another lowercase English letter. After that, do the following partitioning operation until `s` is **empty**: Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters. **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order. Return an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change. **Example 1:** **Input:** s = \"accca\", k = 2 **Output:** 3 **Explanation:** The optimal way is to change `s[2]` to something other than a and c, for example, b. then it becomes `\"acbca\"`. Then we perform the operations: The longest prefix containing at most 2 distinct characters is `\"ac\"`, we remove it and `s` becomes `\"bca\"`. Now The longest prefix containing at most 2 distinct characters is `\"bc\"`, so we remove it and `s` becomes `\"a\"`. Finally, we remove `\"a\"` and `s` becomes empty, so the procedure ends. Doing the operations, the string is divided into 3 partitions, so the answer is 3. **Example 2:** **Input:** s = \"aabaab\", k = 3 **Output:** 1 **Explanation:** Initially `s` contains 2 distinct characters, so whichever character we change, it will contain at most 3 distinct characters, so the longest prefix with at most 3 distinct characters would always be all of it, therefore the answer is 1. **Example 3:** **Input:** s = \"xxyz\", k = 1 **Output:** 4 **Explanation:** The optimal way is to change `s[0]` or `s[1]` to something other than characters in `s`, for example, to change `s[0]` to `w`. Then `s` becomes `\"wxyz\"`, which consists of 4 distinct characters, so as `k` is 1, it will divide into 4 partitions. **Constraints:** `1 <= s.length <= 104` `s` consists only of lowercase English letters. `1 <= k <= 26`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"accca\", k = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \"aabaab\", k = 3", + "output": "1 " + }, + { + "label": "Example 3", + "input": "s = \"xxyz\", k = 1", + "output": "4 " + } + ], + "constraints": [ + "Choose the longest prefix of s containing at most k distinct characters.", + "Delete the prefix from s and increase the number of partitions by one. The remaining characters (if any) in s maintain their initial order.", + "1 <= s.length <= 104", + "s consists only of lowercase English letters.", + "1 <= k <= 26" + ], + "python_template": "class Solution(object):\n def maxPartitionsAfterOperations(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxPartitionsAfterOperations(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxPartitionsAfterOperations" + } +} \ No newline at end of file diff --git a/maximize-the-profit-as-the-salesman.json b/maximize-the-profit-as-the-salesman.json new file mode 100644 index 0000000000000000000000000000000000000000..7a123f4f4cdecb74ec492667966b376f67574fef --- /dev/null +++ b/maximize-the-profit-as-the-salesman.json @@ -0,0 +1,32 @@ +{ + "id": 2979, + "name": "maximize-the-profit-as-the-salesman", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-the-profit-as-the-salesman/", + "date": "2023-08-13", + "task_description": "You are given an integer `n` representing the number of houses on a number line, numbered from `0` to `n - 1`. Additionally, you are given a 2D integer array `offers` where `offers[i] = [starti, endi, goldi]`, indicating that `ith` buyer wants to buy all the houses from `starti` to `endi` for `goldi` amount of gold. As a salesman, your goal is to **maximize** your earnings by strategically selecting and selling houses to buyers. Return _the maximum amount of gold you can earn_. **Note** that different buyers can't buy the same house, and some houses may remain unsold. **Example 1:** ``` **Input:** n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]] **Output:** 3 **Explanation:** There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,0] to 1st buyer for 1 gold and houses in the range [1,3] to 3rd buyer for 2 golds. It can be proven that 3 is the maximum amount of gold we can achieve. ``` **Example 2:** ``` **Input:** n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]] **Output:** 10 **Explanation:** There are 5 houses numbered from 0 to 4 and there are 3 purchase offers. We sell houses in the range [0,2] to 2nd buyer for 10 golds. It can be proven that 10 is the maximum amount of gold we can achieve. ``` **Constraints:** `1 <= n <= 105` `1 <= offers.length <= 105` `offers[i].length == 3` `0 <= starti <= endi <= n - 1` `1 <= goldi <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]", + "output": "10 " + } + ], + "constraints": [ + "1 <= n <= 105", + "1 <= offers.length <= 105", + "offers[i].length == 3", + "0 <= starti <= endi <= n - 1", + "1 <= goldi <= 103" + ], + "python_template": "class Solution(object):\n def maximizeTheProfit(self, n, offers):\n \"\"\"\n :type n: int\n :type offers: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximizeTheProfit(int n, List> offers) {\n \n }\n}", + "metadata": { + "func_name": "maximizeTheProfit" + } +} \ No newline at end of file diff --git a/maximize-total-cost-of-alternating-subarrays.json b/maximize-total-cost-of-alternating-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..6efbabcff5f9c4bd750ed31ffc9bce75248d7184 --- /dev/null +++ b/maximize-total-cost-of-alternating-subarrays.json @@ -0,0 +1,39 @@ +{ + "id": 3464, + "name": "maximize-total-cost-of-alternating-subarrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-total-cost-of-alternating-subarrays/", + "date": "2024-06-16", + "task_description": "You are given an integer array `nums` with length `n`. The **cost** of a subarray `nums[l..r]`, where `0 <= l <= r < n`, is defined as: `cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (−1)r − l` Your task is to **split** `nums` into subarrays such that the **total** **cost** of the subarrays is **maximized**, ensuring each element belongs to **exactly one** subarray. Formally, if `nums` is split into `k` subarrays, where `k > 1`, at indices `i1, i2, ..., ik − 1`, where `0 <= i1 < i2 < ... < ik - 1 < n - 1`, then the total cost will be: `cost(0, i1) + cost(i1 + 1, i2) + ... + cost(ik − 1 + 1, n − 1)` Return an integer denoting the _maximum total cost_ of the subarrays after splitting the array optimally. **Note:** If `nums` is not split into subarrays, i.e. `k = 1`, the total cost is simply `cost(0, n - 1)`. **Example 1:** **Input:** nums = [1,-2,3,4] **Output:** 10 **Explanation:** One way to maximize the total cost is by splitting `[1, -2, 3, 4]` into subarrays `[1, -2, 3]` and `[4]`. The total cost will be `(1 + 2 + 3) + 4 = 10`. **Example 2:** **Input:** nums = [1,-1,1,-1] **Output:** 4 **Explanation:** One way to maximize the total cost is by splitting `[1, -1, 1, -1]` into subarrays `[1, -1]` and `[1, -1]`. The total cost will be `(1 + 1) + (1 + 1) = 4`. **Example 3:** **Input:** nums = [0] **Output:** 0 **Explanation:** We cannot split the array further, so the answer is 0. **Example 4:** **Input:** nums = [1,-1] **Output:** 2 **Explanation:** Selecting the whole array gives a total cost of `1 + 1 = 2`, which is the maximum. **Constraints:** `1 <= nums.length <= 105` `-109 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,-2,3,4]", + "output": "10 " + }, + { + "label": "Example 2", + "input": "nums = [1,-1,1,-1]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [0]", + "output": "0 " + }, + { + "label": "Example 4", + "input": "nums = [1,-1]", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "-109 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumTotalCost(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumTotalCost(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumTotalCost" + } +} \ No newline at end of file diff --git a/maximize-value-of-function-in-a-ball-passing-game.json b/maximize-value-of-function-in-a-ball-passing-game.json new file mode 100644 index 0000000000000000000000000000000000000000..e2f19cea37b291315a7623561353995e24f5cb83 --- /dev/null +++ b/maximize-value-of-function-in-a-ball-passing-game.json @@ -0,0 +1,32 @@ +{ + "id": 3032, + "name": "maximize-value-of-function-in-a-ball-passing-game", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximize-value-of-function-in-a-ball-passing-game/", + "date": "2023-08-20", + "task_description": "You are given an integer array `receiver` of length `n` and an integer `k`. `n` players are playing a ball-passing game. You choose the starting player, `i`. The game proceeds as follows: player `i` passes the ball to player `receiver[i]`, who then passes it to `receiver[receiver[i]]`, and so on, for `k` passes in total. The game's score is the sum of the indices of the players who touched the ball, including repetitions, i.e. `i + receiver[i] + receiver[receiver[i]] + ... + receiver(k)[i]`. Return the **maximum** possible score. **Notes:** `receiver` may contain duplicates. `receiver[i]` may be equal to `i`. **Example 1:** **Input:** receiver = [2,0,1], k = 4 **Output:** 6 **Explanation:** Starting with player `i = 2` the initial score is 2: Pass Sender Index Receiver Index Score 1 2 1 3 2 1 0 3 3 0 2 5 4 2 1 6 **Example 2:** **Input:** receiver = [1,1,1,2,3], k = 3 **Output:** 10 **Explanation:** Starting with player `i = 4` the initial score is 4: Pass Sender Index Receiver Index Score 1 4 3 7 2 3 2 9 3 2 1 10 **Constraints:** `1 <= receiver.length == n <= 105` `0 <= receiver[i] <= n - 1` `1 <= k <= 1010`", + "test_case": [ + { + "label": "Example 1", + "input": "receiver = [2,0,1], k = 4", + "output": "6 " + }, + { + "label": "Example 2", + "input": "receiver = [1,1,1,2,3], k = 3", + "output": "10 " + } + ], + "constraints": [ + "receiver may contain duplicates.", + "receiver[i] may be equal to i.", + "1 <= receiver.length == n <= 105", + "0 <= receiver[i] <= n - 1", + "1 <= k <= 1010" + ], + "python_template": "class Solution(object):\n def getMaxFunctionValue(self, receiver, k):\n \"\"\"\n :type receiver: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long getMaxFunctionValue(List receiver, long k) {\n \n }\n}", + "metadata": { + "func_name": "getMaxFunctionValue" + } +} \ No newline at end of file diff --git a/maximize-win-from-two-segments.json b/maximize-win-from-two-segments.json new file mode 100644 index 0000000000000000000000000000000000000000..b8aec5334a806cf4d8a1133b666260affbc83731 --- /dev/null +++ b/maximize-win-from-two-segments.json @@ -0,0 +1,32 @@ +{ + "id": 2673, + "name": "maximize-win-from-two-segments", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximize-win-from-two-segments/", + "date": "2023-01-21", + "task_description": "There are some prizes on the **X-axis**. You are given an integer array `prizePositions` that is **sorted in non-decreasing order**, where `prizePositions[i]` is the position of the `ith` prize. There could be different prizes at the same position on the line. You are also given an integer `k`. You are allowed to select two segments with integer endpoints. The length of each segment must be `k`. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect. For example if `k = 2`, you can choose segments `[1, 3]` and `[2, 4]`, and you will win any prize i that satisfies `1 <= prizePositions[i] <= 3` or `2 <= prizePositions[i] <= 4`. Return _the **maximum** number of prizes you can win if you choose the two segments optimally_. **Example 1:** ``` **Input:** prizePositions = [1,1,2,2,3,3,5], k = 2 **Output:** 7 **Explanation:** In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5]. ``` **Example 2:** ``` **Input:** prizePositions = [1,2,3,4], k = 0 **Output:** 2 **Explanation:** For this example, **one choice** for the segments is `[3, 3]` and `[4, 4],` and you will be able to get `2` prizes. ``` **Constraints:** `1 <= prizePositions.length <= 105` `1 <= prizePositions[i] <= 109` `0 <= k <= 109 ` `prizePositions` is sorted in non-decreasing order.", + "test_case": [ + { + "label": "Example 1", + "input": "prizePositions = [1,1,2,2,3,3,5], k = 2", + "output": "7 " + }, + { + "label": "Example 2", + "input": "prizePositions = [1,2,3,4], k = 0", + "output": "2 " + } + ], + "constraints": [ + "For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize i that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4.", + "1 <= prizePositions.length <= 105", + "1 <= prizePositions[i] <= 109", + "0 <= k <= 109", + "prizePositions is sorted in non-decreasing order." + ], + "python_template": "class Solution(object):\n def maximizeWin(self, prizePositions, k):\n \"\"\"\n :type prizePositions: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximizeWin(int[] prizePositions, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximizeWin" + } +} \ No newline at end of file diff --git a/maximum-amount-of-money-robot-can-earn.json b/maximum-amount-of-money-robot-can-earn.json new file mode 100644 index 0000000000000000000000000000000000000000..43f85e74c3dbeeadd50c1b18a4749bb022f38bc8 --- /dev/null +++ b/maximum-amount-of-money-robot-can-earn.json @@ -0,0 +1,33 @@ +{ + "id": 3677, + "name": "maximum-amount-of-money-robot-can-earn", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-amount-of-money-robot-can-earn/", + "date": "2025-01-05", + "task_description": "You are given an `m x n` grid. A robot starts at the top-left corner of the grid `(0, 0)` and wants to reach the bottom-right corner `(m - 1, n - 1)`. The robot can move either right or down at any point in time. The grid contains a value `coins[i][j]` in each cell: If `coins[i][j] >= 0`, the robot gains that many coins. If `coins[i][j] < 0`, the robot encounters a robber, and the robber steals the **absolute** value of `coins[i][j]` coins. The robot has a special ability to **neutralize robbers** in at most **2 cells** on its path, preventing them from stealing coins in those cells. **Note:** The robot's total coins can be negative. Return the **maximum** profit the robot can gain on the route. **Example 1:** **Input:** coins = [[0,1,-1],[1,-2,3],[2,-3,4]] **Output:** 8 **Explanation:** An optimal path for maximum coins is: Start at `(0, 0)` with `0` coins (total coins = `0`). Move to `(0, 1)`, gaining `1` coin (total coins = `0 + 1 = 1`). Move to `(1, 1)`, where there's a robber stealing `2` coins. The robot uses one neutralization here, avoiding the robbery (total coins = `1`). Move to `(1, 2)`, gaining `3` coins (total coins = `1 + 3 = 4`). Move to `(2, 2)`, gaining `4` coins (total coins = `4 + 4 = 8`). **Example 2:** **Input:** coins = [[10,10,10],[10,10,10]] **Output:** 40 **Explanation:** An optimal path for maximum coins is: Start at `(0, 0)` with `10` coins (total coins = `10`). Move to `(0, 1)`, gaining `10` coins (total coins = `10 + 10 = 20`). Move to `(0, 2)`, gaining another `10` coins (total coins = `20 + 10 = 30`). Move to `(1, 2)`, gaining the final `10` coins (total coins = `30 + 10 = 40`). **Constraints:** `m == coins.length` `n == coins[i].length` `1 <= m, n <= 500` `-1000 <= coins[i][j] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "coins = [[0,1,-1],[1,-2,3],[2,-3,4]]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "coins = [[10,10,10],[10,10,10]]", + "output": "40 " + } + ], + "constraints": [ + "If coins[i][j] >= 0, the robot gains that many coins.", + "If coins[i][j] < 0, the robot encounters a robber, and the robber steals the absolute value of coins[i][j] coins.", + "m == coins.length", + "n == coins[i].length", + "1 <= m, n <= 500", + "-1000 <= coins[i][j] <= 1000" + ], + "python_template": "class Solution(object):\n def maximumAmount(self, coins):\n \"\"\"\n :type coins: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumAmount(int[][] coins) {\n \n }\n}", + "metadata": { + "func_name": "maximumAmount" + } +} \ No newline at end of file diff --git a/maximum-and-minimum-sums-of-at-most-size-k-subarrays.json b/maximum-and-minimum-sums-of-at-most-size-k-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..01417eb9ad1bbfabe2b8fbb0debd59f7405e579f --- /dev/null +++ b/maximum-and-minimum-sums-of-at-most-size-k-subarrays.json @@ -0,0 +1,30 @@ +{ + "id": 3725, + "name": "maximum-and-minimum-sums-of-at-most-size-k-subarrays", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-and-minimum-sums-of-at-most-size-k-subarrays/", + "date": "2025-01-12", + "task_description": "You are given an integer array `nums` and a **positive** integer `k`. Return the sum of the **maximum** and **minimum** elements of all subarrays with **at most** `k` elements. **Example 1:** **Input:** nums = [1,2,3], k = 2 **Output:** 20 **Explanation:** The subarrays of `nums` with at most 2 elements are: Subarray Minimum Maximum Sum `[1]` 1 1 2 `[2]` 2 2 4 `[3]` 3 3 6 `[1, 2]` 1 2 3 `[2, 3]` 2 3 5 **Final Total** 20 The output would be 20. **Example 2:** **Input:** nums = [1,-3,1], k = 2 **Output:** -6 **Explanation:** The subarrays of `nums` with at most 2 elements are: Subarray Minimum Maximum Sum `[1]` 1 1 2 `[-3]` -3 -3 -6 `[1]` 1 1 2 `[1, -3]` -3 1 -2 `[-3, 1]` -3 1 -2 **Final Total** -6 The output would be -6. **Constraints:** `1 <= nums.length <= 80000` `1 <= k <= nums.length` `-106 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3], k = 2", + "output": "20 " + }, + { + "label": "Example 2", + "input": "nums = [1,-3,1], k = 2", + "output": "-6 " + } + ], + "constraints": [ + "1 <= nums.length <= 80000", + "1 <= k <= nums.length", + "-106 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def minMaxSubarraySum(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 minMaxSubarraySum(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minMaxSubarraySum" + } +} \ No newline at end of file diff --git a/maximum-and-sum-of-array.json b/maximum-and-sum-of-array.json new file mode 100644 index 0000000000000000000000000000000000000000..db166a7ad4cc7f87faaa2db1ecf52550c57bdf61 --- /dev/null +++ b/maximum-and-sum-of-array.json @@ -0,0 +1,32 @@ +{ + "id": 2291, + "name": "maximum-and-sum-of-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-and-sum-of-array/", + "date": "2022-02-06", + "task_description": "You are given an integer array `nums` of length `n` and an integer `numSlots` such that `2 * numSlots >= n`. There are `numSlots` slots numbered from `1` to `numSlots`. You have to place all `n` integers into the slots such that each slot contains at **most** two numbers. The **AND sum** of a given placement is the sum of the **bitwise** `AND` of every number with its respective slot number. For example, the **AND sum** of placing the numbers `[1, 3]` into slot `1` and `[4, 6]` into slot `2` is equal to `(1 AND 1) + (3 AND 1) + (4 AND 2) + (6 AND 2) = 1 + 1 + 0 + 2 = 4`. Return _the maximum possible **AND sum** of _`nums`_ given _`numSlots`_ slots._ **Example 1:** ``` **Input:** nums = [1,2,3,4,5,6], numSlots = 3 **Output:** 9 **Explanation:** One possible placement is [1, 4] into slot 1, [2, 6] into slot 2, and [3, 5] into slot 3. This gives the maximum AND sum of (1 AND 1) + (4 AND 1) + (2 AND 2) + (6 AND 2) + (3 AND 3) + (5 AND 3) = 1 + 0 + 2 + 2 + 3 + 1 = 9. ``` **Example 2:** ``` **Input:** nums = [1,3,10,4,7,1], numSlots = 9 **Output:** 24 **Explanation:** One possible placement is [1, 1] into slot 1, [3] into slot 3, [4] into slot 4, [7] into slot 7, and [10] into slot 9. This gives the maximum AND sum of (1 AND 1) + (1 AND 1) + (3 AND 3) + (4 AND 4) + (7 AND 7) + (10 AND 9) = 1 + 1 + 3 + 4 + 7 + 8 = 24. Note that slots 2, 5, 6, and 8 are empty which is permitted. ``` **Constraints:** `n == nums.length` `1 <= numSlots <= 9` `1 <= n <= 2 * numSlots` `1 <= nums[i] <= 15`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5,6], numSlots = 3", + "output": "9 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,10,4,7,1], numSlots = 9", + "output": "24 " + } + ], + "constraints": [ + "For example, the AND sum of placing the numbers [1, 3] into slot 1 and [4, 6] into slot 2 is equal to (1 AND 1) + (3 AND 1) + (4 AND 2) + (6 AND 2) = 1 + 1 + 0 + 2 = 4.", + "n == nums.length", + "1 <= numSlots <= 9", + "1 <= n <= 2 * numSlots", + "1 <= nums[i] <= 15" + ], + "python_template": "class Solution(object):\n def maximumANDSum(self, nums, numSlots):\n \"\"\"\n :type nums: List[int]\n :type numSlots: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumANDSum(int[] nums, int numSlots) {\n \n }\n}", + "metadata": { + "func_name": "maximumANDSum" + } +} \ No newline at end of file diff --git a/maximum-area-of-longest-diagonal-rectangle.json b/maximum-area-of-longest-diagonal-rectangle.json new file mode 100644 index 0000000000000000000000000000000000000000..a826a8f1a5d57c34dba32e749d83906188e103c2 --- /dev/null +++ b/maximum-area-of-longest-diagonal-rectangle.json @@ -0,0 +1,30 @@ +{ + "id": 3251, + "name": "maximum-area-of-longest-diagonal-rectangle", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle/", + "date": "2023-12-31", + "task_description": "You are given a 2D **0-indexed **integer array `dimensions`. For all indices `i`, `0 <= i < dimensions.length`, `dimensions[i][0]` represents the length and `dimensions[i][1]` represents the width of the rectangle `i`. Return _the **area** of the rectangle having the **longest** diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the **maximum** area._ **Example 1:** ``` **Input:** dimensions = [[9,3],[8,6]] **Output:** 48 **Explanation:** For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487. For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10. So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48. ``` **Example 2:** ``` **Input:** dimensions = [[3,4],[4,3]] **Output:** 12 **Explanation:** Length of diagonal is the same for both which is 5, so maximum area = 12. ``` **Constraints:** `1 <= dimensions.length <= 100` `dimensions[i].length == 2` `1 <= dimensions[i][0], dimensions[i][1] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "dimensions = [[9,3],[8,6]]", + "output": "48 " + }, + { + "label": "Example 2", + "input": "dimensions = [[3,4],[4,3]]", + "output": "12 " + } + ], + "constraints": [ + "1 <= dimensions.length <= 100", + "dimensions[i].length == 2", + "1 <= dimensions[i][0], dimensions[i][1] <= 100" + ], + "python_template": "class Solution(object):\n def areaOfMaxDiagonal(self, dimensions):\n \"\"\"\n :type dimensions: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int areaOfMaxDiagonal(int[][] dimensions) {\n \n }\n}", + "metadata": { + "func_name": "areaOfMaxDiagonal" + } +} \ No newline at end of file diff --git a/maximum-area-rectangle-with-point-constraints-i.json b/maximum-area-rectangle-with-point-constraints-i.json new file mode 100644 index 0000000000000000000000000000000000000000..8ecf079b137f59f1805d342a13a0ba32fca6261c --- /dev/null +++ b/maximum-area-rectangle-with-point-constraints-i.json @@ -0,0 +1,39 @@ +{ + "id": 3681, + "name": "maximum-area-rectangle-with-point-constraints-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-area-rectangle-with-point-constraints-i/", + "date": "2024-12-01", + "task_description": "You are given an array `points` where `points[i] = [xi, yi]` represents the coordinates of a point on an infinite plane. Your task is to find the **maximum **area of a rectangle that: Can be formed using **four** of these points as its corners. Does **not** contain any other point inside or on its border. Has its edges **parallel** to the axes. Return the **maximum area** that you can obtain or -1 if no such rectangle is possible. **Example 1:** **Input:** points = [[1,1],[1,3],[3,1],[3,3]] **Output: **4 **Explanation:** **** We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4. **Example 2:** **Input:** points = [[1,1],[1,3],[3,1],[3,3],[2,2]] **Output:** -1 **Explanation:** **** There is only one rectangle possible is with points `[1,1], [1,3], [3,1]` and `[3,3]` but `[2,2]` will always lie inside it. Hence, returning -1. **Example 3:** **Input:** points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]] **Output: **2 **Explanation:** **** The maximum area rectangle is formed by the points `[1,3], [1,2], [3,2], [3,3]`, which has an area of 2. Additionally, the points `[1,1], [1,2], [3,1], [3,2]` also form a valid rectangle with the same area. **Constraints:** `1 <= points.length <= 10` `points[i].length == 2` `0 <= xi, yi <= 100` All the given points are **unique**.", + "test_case": [ + { + "label": "Example 1", + "input": "points = [[1,1],[1,3],[3,1],[3,3]]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "points = [[1,1],[1,3],[3,1],[3,3],[2,2]]", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]]", + "output": "2 " + } + ], + "constraints": [ + "Can be formed using four of these points as its corners.", + "Does not contain any other point inside or on its border.", + "Has its edges parallel to the axes.", + "1 <= points.length <= 10", + "points[i].length == 2", + "0 <= xi, yi <= 100", + "All the given points are unique." + ], + "python_template": "class Solution(object):\n def maxRectangleArea(self, points):\n \"\"\"\n :type points: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxRectangleArea(int[][] points) {\n \n }\n}", + "metadata": { + "func_name": "maxRectangleArea" + } +} \ No newline at end of file diff --git a/maximum-bags-with-full-capacity-of-rocks.json b/maximum-bags-with-full-capacity-of-rocks.json new file mode 100644 index 0000000000000000000000000000000000000000..e1af2378765e5db22fb93a80939925c48eea8ff1 --- /dev/null +++ b/maximum-bags-with-full-capacity-of-rocks.json @@ -0,0 +1,32 @@ +{ + "id": 2366, + "name": "maximum-bags-with-full-capacity-of-rocks", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/", + "date": "2022-05-15", + "task_description": "You have `n` bags numbered from `0` to `n - 1`. You are given two **0-indexed** integer arrays `capacity` and `rocks`. The `ith` bag can hold a maximum of `capacity[i]` rocks and currently contains `rocks[i]` rocks. You are also given an integer `additionalRocks`, the number of additional rocks you can place in **any** of the bags. Return_ the **maximum** number of bags that could have full capacity after placing the additional rocks in some bags._ **Example 1:** ``` **Input:** capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2 **Output:** 3 **Explanation:** Place 1 rock in bag 0 and 1 rock in bag 1. The number of rocks in each bag are now [2,3,4,4]. Bags 0, 1, and 2 have full capacity. There are 3 bags at full capacity, so we return 3. It can be shown that it is not possible to have more than 3 bags at full capacity. Note that there may be other ways of placing the rocks that result in an answer of 3. ``` **Example 2:** ``` **Input:** capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100 **Output:** 3 **Explanation:** Place 8 rocks in bag 0 and 2 rocks in bag 2. The number of rocks in each bag are now [10,2,2]. Bags 0, 1, and 2 have full capacity. There are 3 bags at full capacity, so we return 3. It can be shown that it is not possible to have more than 3 bags at full capacity. Note that we did not use all of the additional rocks. ``` **Constraints:** `n == capacity.length == rocks.length` `1 <= n <= 5 * 104` `1 <= capacity[i] <= 109` `0 <= rocks[i] <= capacity[i]` `1 <= additionalRocks <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100", + "output": "3 " + } + ], + "constraints": [ + "n == capacity.length == rocks.length", + "1 <= n <= 5 * 104", + "1 <= capacity[i] <= 109", + "0 <= rocks[i] <= capacity[i]", + "1 <= additionalRocks <= 109" + ], + "python_template": "class Solution(object):\n def maximumBags(self, capacity, rocks, additionalRocks):\n \"\"\"\n :type capacity: List[int]\n :type rocks: List[int]\n :type additionalRocks: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {\n \n }\n}", + "metadata": { + "func_name": "maximumBags" + } +} \ No newline at end of file diff --git a/maximum-beauty-of-an-array-after-applying-operation.json b/maximum-beauty-of-an-array-after-applying-operation.json new file mode 100644 index 0000000000000000000000000000000000000000..183ac59a7d79fa4eff7e5323ff4c3a7b95e1eaa9 --- /dev/null +++ b/maximum-beauty-of-an-array-after-applying-operation.json @@ -0,0 +1,31 @@ +{ + "id": 2891, + "name": "maximum-beauty-of-an-array-after-applying-operation", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/", + "date": "2023-07-09", + "task_description": "You are given a **0-indexed** array `nums` and a **non-negative** integer `k`. In one operation, you can do the following: Choose an index `i` that **hasn't been chosen before** from the range `[0, nums.length - 1]`. Replace `nums[i]` with any integer from the range `[nums[i] - k, nums[i] + k]`. The **beauty** of the array is the length of the longest subsequence consisting of equal elements. Return _the **maximum** possible beauty of the array _`nums`_ after applying the operation any number of times._ **Note** that you can apply the operation to each index **only once**. A **subsequence** of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements. **Example 1:** ``` **Input:** nums = [4,6,1,2], k = 2 **Output:** 3 **Explanation:** In this example, we apply the following operations: - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2]. - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4]. After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3). It can be proven that 3 is the maximum possible length we can achieve. ``` **Example 2:** ``` **Input:** nums = [1,1,1,1], k = 10 **Output:** 4 **Explanation:** In this example we don't have to apply any operations. The beauty of the array nums is 4 (whole array). ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i], k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,6,1,2], k = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,1,1], k = 10", + "output": "4 " + } + ], + "constraints": [ + "Choose an index i that hasn't been chosen before from the range [0, nums.length - 1].", + "Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k].", + "1 <= nums.length <= 105", + "0 <= nums[i], k <= 105" + ], + "python_template": "class Solution(object):\n def maximumBeauty(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 maximumBeauty(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumBeauty" + } +} \ No newline at end of file diff --git a/maximum-coins-from-k-consecutive-bags.json b/maximum-coins-from-k-consecutive-bags.json new file mode 100644 index 0000000000000000000000000000000000000000..a24e060a51ddf4250fd4a28f5f5336bed71a3514 --- /dev/null +++ b/maximum-coins-from-k-consecutive-bags.json @@ -0,0 +1,33 @@ +{ + "id": 3715, + "name": "maximum-coins-from-k-consecutive-bags", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-coins-from-k-consecutive-bags/", + "date": "2024-12-29", + "task_description": "There are an infinite amount of bags on a number line, one bag for each coordinate. Some of these bags contain coins. You are given a 2D array `coins`, where `coins[i] = [li, ri, ci]` denotes that every bag from `li` to `ri` contains `ci` coins. The segments that `coins` contain are non-overlapping. You are also given an integer `k`. Return the **maximum** amount of coins you can obtain by collecting `k` consecutive bags. **Example 1:** **Input:** coins = [[8,10,1],[1,3,2],[5,6,4]], k = 4 **Output:** 10 **Explanation:** Selecting bags at positions `[3, 4, 5, 6]` gives the maximum number of coins: `2 + 0 + 4 + 4 = 10`. **Example 2:** **Input:** coins = [[1,10,3]], k = 2 **Output:** 6 **Explanation:** Selecting bags at positions `[1, 2]` gives the maximum number of coins: `3 + 3 = 6`. **Constraints:** `1 <= coins.length <= 105` `1 <= k <= 109` `coins[i] == [li, ri, ci]` `1 <= li <= ri <= 109` `1 <= ci <= 1000` The given segments are non-overlapping.", + "test_case": [ + { + "label": "Example 1", + "input": "coins = [[8,10,1],[1,3,2],[5,6,4]], k = 4", + "output": "10 " + }, + { + "label": "Example 2", + "input": "coins = [[1,10,3]], k = 2", + "output": "6 " + } + ], + "constraints": [ + "1 <= coins.length <= 105", + "1 <= k <= 109", + "coins[i] == [li, ri, ci]", + "1 <= li <= ri <= 109", + "1 <= ci <= 1000", + "The given segments are non-overlapping." + ], + "python_template": "class Solution(object):\n def maximumCoins(self, coins, k):\n \"\"\"\n :type coins: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumCoins(int[][] coins, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumCoins" + } +} \ No newline at end of file diff --git a/maximum-containers-on-a-ship.json b/maximum-containers-on-a-ship.json new file mode 100644 index 0000000000000000000000000000000000000000..db03f09edc9bcf927ff1318bc844c181cdc767da --- /dev/null +++ b/maximum-containers-on-a-ship.json @@ -0,0 +1,30 @@ +{ + "id": 3817, + "name": "maximum-containers-on-a-ship", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-containers-on-a-ship/", + "date": "2025-03-16", + "task_description": "You are given a positive integer `n` representing an `n x n` cargo deck on a ship. Each cell on the deck can hold one container with a weight of **exactly** `w`. However, the total weight of all containers, if loaded onto the deck, must not exceed the ship's maximum weight capacity, `maxWeight`. Return the **maximum** number of containers that can be loaded onto the ship. **Example 1:** **Input:** n = 2, w = 3, maxWeight = 15 **Output:** 4 **Explanation: ** The deck has 4 cells, and each container weighs 3. The total weight of loading all containers is 12, which does not exceed `maxWeight`. **Example 2:** **Input:** n = 3, w = 5, maxWeight = 20 **Output:** 4 **Explanation: ** The deck has 9 cells, and each container weighs 5. The maximum number of containers that can be loaded without exceeding `maxWeight` is 4. **Constraints:** `1 <= n <= 1000` `1 <= w <= 1000` `1 <= maxWeight <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 2, w = 3, maxWeight = 15", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 3, w = 5, maxWeight = 20", + "output": "4 " + } + ], + "constraints": [ + "1 <= n <= 1000", + "1 <= w <= 1000", + "1 <= maxWeight <= 109" + ], + "python_template": "class Solution(object):\n def maxContainers(self, n, w, maxWeight):\n \"\"\"\n :type n: int\n :type w: int\n :type maxWeight: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxContainers(int n, int w, int maxWeight) {\n \n }\n}", + "metadata": { + "func_name": "maxContainers" + } +} \ No newline at end of file diff --git a/maximum-count-of-positive-integer-and-negative-integer.json b/maximum-count-of-positive-integer-and-negative-integer.json new file mode 100644 index 0000000000000000000000000000000000000000..691f6b5cf9d4fcf71e2894d8295f3f0006525661 --- /dev/null +++ b/maximum-count-of-positive-integer-and-negative-integer.json @@ -0,0 +1,36 @@ +{ + "id": 2614, + "name": "maximum-count-of-positive-integer-and-negative-integer", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/", + "date": "2023-01-01", + "task_description": "Given an array `nums` sorted in **non-decreasing** order, return _the maximum between the number of positive integers and the number of negative integers._ In other words, if the number of positive integers in `nums` is `pos` and the number of negative integers is `neg`, then return the maximum of `pos` and `neg`. **Note** that `0` is neither positive nor negative. **Example 1:** ``` **Input:** nums = [-2,-1,-1,1,2,3] **Output:** 3 **Explanation:** There are 3 positive integers and 3 negative integers. The maximum count among them is 3. ``` **Example 2:** ``` **Input:** nums = [-3,-2,-1,0,0,1,2] **Output:** 3 **Explanation:** There are 2 positive integers and 3 negative integers. The maximum count among them is 3. ``` **Example 3:** ``` **Input:** nums = [5,20,66,1314] **Output:** 4 **Explanation:** There are 4 positive integers and 0 negative integers. The maximum count among them is 4. ``` **Constraints:** `1 <= nums.length <= 2000` `-2000 <= nums[i] <= 2000` `nums` is sorted in a **non-decreasing order**. **Follow up:** Can you solve the problem in `O(log(n))` time complexity?", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [-2,-1,-1,1,2,3]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [-3,-2,-1,0,0,1,2]", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [5,20,66,1314]", + "output": "4 " + } + ], + "constraints": [ + "In other words, if the number of positive integers in nums is pos and the number of negative integers is neg, then return the maximum of pos and neg.", + "1 <= nums.length <= 2000", + "-2000 <= nums[i] <= 2000", + "nums is sorted in a non-decreasing order." + ], + "python_template": "class Solution(object):\n def maximumCount(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumCount(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumCount" + } +} \ No newline at end of file diff --git a/maximum-deletions-on-a-string.json b/maximum-deletions-on-a-string.json new file mode 100644 index 0000000000000000000000000000000000000000..c5df0adbd1afc83b3d89b65a74f64deff7ab52d4 --- /dev/null +++ b/maximum-deletions-on-a-string.json @@ -0,0 +1,36 @@ +{ + "id": 2510, + "name": "maximum-deletions-on-a-string", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-deletions-on-a-string/", + "date": "2022-09-25", + "task_description": "You are given a string `s` consisting of only lowercase English letters. In one operation, you can: Delete **the entire string** `s`, or Delete the **first** `i` letters of `s` if the first `i` letters of `s` are **equal** to the following `i` letters in `s`, for any `i` in the range `1 <= i <= s.length / 2`. For example, if `s = \"ababc\"`, then in one operation, you could delete the first two letters of `s` to get `\"abc\"`, since the first two letters of `s` and the following two letters of `s` are both equal to `\"ab\"`. Return _the **maximum** number of operations needed to delete all of _`s`. **Example 1:** ``` **Input:** s = \"abcabcdabc\" **Output:** 2 **Explanation:** - Delete the first 3 letters (\"abc\") since the next 3 letters are equal. Now, s = \"abcdabc\". - Delete all the letters. We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed. Note that in the second operation we cannot delete \"abc\" again because the next occurrence of \"abc\" does not happen in the next 3 letters. ``` **Example 2:** ``` **Input:** s = \"aaabaab\" **Output:** 4 **Explanation:** - Delete the first letter (\"a\") since the next letter is equal. Now, s = \"aabaab\". - Delete the first 3 letters (\"aab\") since the next 3 letters are equal. Now, s = \"aab\". - Delete the first letter (\"a\") since the next letter is equal. Now, s = \"ab\". - Delete all the letters. We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed. ``` **Example 3:** ``` **Input:** s = \"aaaaa\" **Output:** 5 **Explanation:** In each operation, we can delete the first letter of s. ``` **Constraints:** `1 <= s.length <= 4000` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcabcdabc\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"aaabaab\"", + "output": "4 " + }, + { + "label": "Example 3", + "input": "s = \"aaaaa\"", + "output": "5 " + } + ], + "constraints": [ + "Delete the entire string s, or", + "Delete the first i letters of s if the first i letters of s are equal to the following i letters in s, for any i in the range 1 <= i <= s.length / 2.", + "1 <= s.length <= 4000", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def deleteString(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int deleteString(String s) {\n \n }\n}", + "metadata": { + "func_name": "deleteString" + } +} \ No newline at end of file diff --git a/maximum-difference-between-adjacent-elements-in-a-circular-array.json b/maximum-difference-between-adjacent-elements-in-a-circular-array.json new file mode 100644 index 0000000000000000000000000000000000000000..73935e76fffb6a5702d27000962f9ae96cf2839d --- /dev/null +++ b/maximum-difference-between-adjacent-elements-in-a-circular-array.json @@ -0,0 +1,29 @@ +{ + "id": 3747, + "name": "maximum-difference-between-adjacent-elements-in-a-circular-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-difference-between-adjacent-elements-in-a-circular-array/", + "date": "2025-01-04", + "task_description": "Given a **circular** array `nums`, find the maximum absolute difference between adjacent elements. **Note**: In a circular array, the first and last elements are adjacent. **Example 1:** **Input:** nums = [1,2,4] **Output:** 3 **Explanation:** Because `nums` is circular, `nums[0]` and `nums[2]` are adjacent. They have the maximum absolute difference of `|4 - 1| = 3`. **Example 2:** **Input:** nums = [-5,-10,-5] **Output:** 5 **Explanation:** The adjacent elements `nums[0]` and `nums[1]` have the maximum absolute difference of `|-5 - (-10)| = 5`. **Constraints:** `2 <= nums.length <= 100` `-100 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,4]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [-5,-10,-5]", + "output": "5 " + } + ], + "constraints": [ + "2 <= nums.length <= 100", + "-100 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def maxAdjacentDistance(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxAdjacentDistance(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxAdjacentDistance" + } +} \ No newline at end of file diff --git a/maximum-difference-between-even-and-odd-frequency-i.json b/maximum-difference-between-even-and-odd-frequency-i.json new file mode 100644 index 0000000000000000000000000000000000000000..f73623548872e9604cf7f62ffe8cb97301d7f453 --- /dev/null +++ b/maximum-difference-between-even-and-odd-frequency-i.json @@ -0,0 +1,36 @@ +{ + "id": 3753, + "name": "maximum-difference-between-even-and-odd-frequency-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i/", + "date": "2025-01-26", + "task_description": "You are given a string `s` consisting of lowercase English letters. Your task is to find the **maximum** difference between the frequency of **two** characters in the string such that: One of the characters has an **even frequency** in the string. The other character has an **odd frequency** in the string. Return the **maximum** difference, calculated as the frequency of the character with an odd frequency **minus** the frequency of the character with an even frequency. **Example 1:** **Input:** s = \"aaaaabbc\" **Output:** 3 **Explanation:** The character `'a'` has an **odd frequency** of `5`, and `'b'` has an **even frequency** of `2`. The maximum difference is `5 - 2 = 3`. **Example 2:** **Input:** s = \"abcabcab\" **Output:** 1 **Explanation:** The character `'a'` has an **odd frequency** of `3`, and `'c'` has an **even frequency** of 2. The maximum difference is `3 - 2 = 1`. **Constraints:** `3 <= s.length <= 100` `s` consists only of lowercase English letters. `s` contains at least one character with an odd frequency and one with an even frequency.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aaaaabbc\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \"abcabcab\"", + "output": "1 " + } + ], + "constraints": [ + "One of the characters has an even frequency in the string.", + "The other character has an odd frequency in the string.", + "The character 'a' has an odd frequency of 5, and 'b' has an even frequency of 2.", + "The maximum difference is 5 - 2 = 3.", + "The character 'a' has an odd frequency of 3, and 'c' has an even frequency of 2.", + "The maximum difference is 3 - 2 = 1.", + "3 <= s.length <= 100", + "s consists only of lowercase English letters.", + "s contains at least one character with an odd frequency and one with an even frequency." + ], + "python_template": "class Solution(object):\n def maxDifference(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxDifference(String s) {\n \n }\n}", + "metadata": { + "func_name": "maxDifference" + } +} \ No newline at end of file diff --git a/maximum-difference-score-in-a-grid.json b/maximum-difference-score-in-a-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..071070bd72c0f6649ae40f74c0b641e9282b8dcc --- /dev/null +++ b/maximum-difference-score-in-a-grid.json @@ -0,0 +1,32 @@ +{ + "id": 3391, + "name": "maximum-difference-score-in-a-grid", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-difference-score-in-a-grid/", + "date": "2024-05-05", + "task_description": "You are given an `m x n` matrix `grid` consisting of **positive** integers. You can move from a cell in the matrix to **any** other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value `c1` to a cell with the value `c2` is `c2 - c1`. You can start at **any** cell, and you have to make **at least** one move. Return the **maximum** total score you can achieve. **Example 1:** **Input:** grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]] **Output:** 9 **Explanation:** We start at the cell `(0, 1)`, and we perform the following moves: - Move from the cell `(0, 1)` to `(2, 1)` with a score of `7 - 5 = 2`. - Move from the cell `(2, 1)` to `(2, 2)` with a score of `14 - 7 = 7`. The total score is `2 + 7 = 9`. **Example 2:** **Input:** grid = [[4,3,2],[3,2,1]] **Output:** -1 **Explanation:** We start at the cell `(0, 0)`, and we perform one move: `(0, 0)` to `(0, 1)`. The score is `3 - 4 = -1`. **Constraints:** `m == grid.length` `n == grid[i].length` `2 <= m, n <= 1000` `4 <= m * n <= 105` `1 <= grid[i][j] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]]", + "output": "9 " + }, + { + "label": "Example 2", + "input": "grid = [[4,3,2],[3,2,1]]", + "output": "-1 " + } + ], + "constraints": [ + "m == grid.length", + "n == grid[i].length", + "2 <= m, n <= 1000", + "4 <= m * n <= 105", + "1 <= grid[i][j] <= 105" + ], + "python_template": "class Solution(object):\n def maxScore(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxScore(List> grid) {\n \n }\n}", + "metadata": { + "func_name": "maxScore" + } +} \ No newline at end of file diff --git a/maximum-elegance-of-a-k-length-subsequence.json b/maximum-elegance-of-a-k-length-subsequence.json new file mode 100644 index 0000000000000000000000000000000000000000..14631395051cd7dd5809c620585eac8159f6cb7d --- /dev/null +++ b/maximum-elegance-of-a-k-length-subsequence.json @@ -0,0 +1,39 @@ +{ + "id": 2894, + "name": "maximum-elegance-of-a-k-length-subsequence", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-elegance-of-a-k-length-subsequence/", + "date": "2023-07-30", + "task_description": "You are given a **0-indexed** 2D integer array `items` of length `n` and an integer `k`. `items[i] = [profiti, categoryi]`, where `profiti` and `categoryi` denote the profit and category of the `ith` item respectively. Let's define the **elegance** of a **subsequence** of `items` as `total_profit + distinct_categories2`, where `total_profit` is the sum of all profits in the subsequence, and `distinct_categories` is the number of **distinct** categories from all the categories in the selected subsequence. Your task is to find the **maximum elegance** from all subsequences of size `k` in `items`. Return _an integer denoting the maximum elegance of a subsequence of _`items`_ with size exactly _`k`. **Note:** A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. **Example 1:** ``` **Input:** items = [[3,2],[5,1],[10,1]], k = 2 **Output:** 17 **Explanation: **In this example, we have to select a subsequence of size 2. We can select items[0] = [3,2] and items[2] = [10,1]. The total profit in this subsequence is 3 + 10 = 13, and the subsequence contains 2 distinct categories [2,1]. Hence, the elegance is 13 + 22 = 17, and we can show that it is the maximum achievable elegance. ``` **Example 2:** ``` **Input:** items = [[3,1],[3,1],[2,2],[5,3]], k = 3 **Output:** 19 **Explanation:** In this example, we have to select a subsequence of size 3. We can select items[0] = [3,1], items[2] = [2,2], and items[3] = [5,3]. The total profit in this subsequence is 3 + 2 + 5 = 10, and the subsequence contains 3 distinct categories [1,2,3]. Hence, the elegance is 10 + 32 = 19, and we can show that it is the maximum achievable elegance. ``` **Example 3:** ``` **Input:** items = [[1,1],[2,1],[3,1]], k = 3 **Output:** 7 **Explanation:** In this example, we have to select a subsequence of size 3. We should select all the items. The total profit will be 1 + 2 + 3 = 6, and the subsequence contains 1 distinct category [1]. Hence, the maximum elegance is 6 + 12 = 7. ``` **Constraints:** `1 <= items.length == n <= 105` `items[i].length == 2` `items[i][0] == profiti` `items[i][1] == categoryi` `1 <= profiti <= 109` `1 <= categoryi <= n ` `1 <= k <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "items = [[3,2],[5,1],[10,1]], k = 2", + "output": "17 " + }, + { + "label": "Example 2", + "input": "items = [[3,1],[3,1],[2,2],[5,3]], k = 3", + "output": "19 " + }, + { + "label": "Example 3", + "input": "items = [[1,1],[2,1],[3,1]], k = 3", + "output": "7 " + } + ], + "constraints": [ + "1 <= items.length == n <= 105", + "items[i].length == 2", + "items[i][0] == profiti", + "items[i][1] == categoryi", + "1 <= profiti <= 109", + "1 <= categoryi <= n", + "1 <= k <= n" + ], + "python_template": "class Solution(object):\n def findMaximumElegance(self, items, k):\n \"\"\"\n :type items: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long findMaximumElegance(int[][] items, int k) {\n \n }\n}", + "metadata": { + "func_name": "findMaximumElegance" + } +} \ No newline at end of file diff --git a/maximum-enemy-forts-that-can-be-captured.json b/maximum-enemy-forts-that-can-be-captured.json new file mode 100644 index 0000000000000000000000000000000000000000..2f3cf6f3660b38e5c1bc72649644fdb46ba99294 --- /dev/null +++ b/maximum-enemy-forts-that-can-be-captured.json @@ -0,0 +1,34 @@ +{ + "id": 2602, + "name": "maximum-enemy-forts-that-can-be-captured", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/", + "date": "2022-12-10", + "task_description": "You are given a **0-indexed** integer array `forts` of length `n` representing the positions of several forts. `forts[i]` can be `-1`, `0`, or `1` where: `-1` represents there is **no fort** at the `ith` position. `0` indicates there is an **enemy** fort at the `ith` position. `1` indicates the fort at the `ith` the position is under your command. Now you have decided to move your army from one of your forts at position `i` to an empty position `j` such that: `0 <= i, j <= n - 1` The army travels over enemy forts **only**. Formally, for all `k` where `min(i,j) < k < max(i,j)`, `forts[k] == 0.` While moving the army, all the enemy forts that come in the way are **captured**. Return_ the **maximum** number of enemy forts that can be captured_. In case it is **impossible** to move your army, or you do not have any fort under your command, return `0`_._ **Example 1:** ``` **Input:** forts = [1,0,0,-1,0,0,0,0,1] **Output:** 4 **Explanation:** - Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2. - Moving the army from position 8 to position 3 captures 4 enemy forts. Since 4 is the maximum number of enemy forts that can be captured, we return 4. ``` **Example 2:** ``` **Input:** forts = [0,0,1,-1] **Output:** 0 **Explanation:** Since no enemy fort can be captured, 0 is returned. ``` **Constraints:** `1 <= forts.length <= 1000` `-1 <= forts[i] <= 1`", + "test_case": [ + { + "label": "Example 1", + "input": "forts = [1,0,0,-1,0,0,0,0,1]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "forts = [0,0,1,-1]", + "output": "0 " + } + ], + "constraints": [ + "-1 represents there is no fort at the ith position.", + "0 indicates there is an enemy fort at the ith position.", + "1 indicates the fort at the ith the position is under your command.", + "0 <= i, j <= n - 1", + "The army travels over enemy forts only. Formally, for all k where min(i,j) < k < max(i,j), forts[k] == 0.", + "1 <= forts.length <= 1000", + "-1 <= forts[i] <= 1" + ], + "python_template": "class Solution(object):\n def captureForts(self, forts):\n \"\"\"\n :type forts: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int captureForts(int[] forts) {\n \n }\n}", + "metadata": { + "func_name": "captureForts" + } +} \ No newline at end of file diff --git a/maximum-frequency-after-subarray-operation.json b/maximum-frequency-after-subarray-operation.json new file mode 100644 index 0000000000000000000000000000000000000000..1a78e0963dd2fa10984cd5d1102f7bb0e4664b20 --- /dev/null +++ b/maximum-frequency-after-subarray-operation.json @@ -0,0 +1,32 @@ +{ + "id": 3751, + "name": "maximum-frequency-after-subarray-operation", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-frequency-after-subarray-operation/", + "date": "2025-01-19", + "task_description": "You are given an array `nums` of length `n`. You are also given an integer `k`. You perform the following operation on `nums` **once**: Select a subarray `nums[i..j]` where `0 <= i <= j <= n - 1`. Select an integer `x` and add `x` to **all** the elements in `nums[i..j]`. Find the **maximum** frequency of the value `k` after the operation. **Example 1:** **Input:** nums = [1,2,3,4,5,6], k = 1 **Output:** 2 **Explanation:** After adding -5 to `nums[2..5]`, 1 has a frequency of 2 in `[1, 2, -2, -1, 0, 1]`. **Example 2:** **Input:** nums = [10,2,3,4,5,5,4,3,2,2], k = 10 **Output:** 4 **Explanation:** After adding 8 to `nums[1..9]`, 10 has a frequency of 4 in `[10, 10, 11, 12, 13, 13, 12, 11, 10, 10]`. **Constraints:** `1 <= n == nums.length <= 105` `1 <= nums[i] <= 50` `1 <= k <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5,6], k = 1", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [10,2,3,4,5,5,4,3,2,2], k = 10", + "output": "4 " + } + ], + "constraints": [ + "Select a subarray nums[i..j] where 0 <= i <= j <= n - 1.", + "Select an integer x and add x to all the elements in nums[i..j].", + "1 <= n == nums.length <= 105", + "1 <= nums[i] <= 50", + "1 <= k <= 50" + ], + "python_template": "class Solution(object):\n def maxFrequency(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 maxFrequency(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxFrequency" + } +} \ No newline at end of file diff --git a/maximum-frequency-of-an-element-after-performing-operations-i.json b/maximum-frequency-of-an-element-after-performing-operations-i.json new file mode 100644 index 0000000000000000000000000000000000000000..a780e0c3d390ab7d7a4753762eae1e725bbf4ce7 --- /dev/null +++ b/maximum-frequency-of-an-element-after-performing-operations-i.json @@ -0,0 +1,36 @@ +{ + "id": 3622, + "name": "maximum-frequency-of-an-element-after-performing-operations-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-i/", + "date": "2024-10-26", + "task_description": "You are given an integer array `nums` and two integers `k` and `numOperations`. You must perform an **operation** `numOperations` times on `nums`, where in each operation you: Select an index `i` that was **not** selected in any previous operations. Add an integer in the range `[-k, k]` to `nums[i]`. Return the **maximum** possible frequency of any element in `nums` after performing the **operations**. **Example 1:** **Input:** nums = [1,4,5], k = 1, numOperations = 2 **Output:** 2 **Explanation:** We can achieve a maximum frequency of two by: Adding 0 to `nums[1]`. `nums` becomes `[1, 4, 5]`. Adding -1 to `nums[2]`. `nums` becomes `[1, 4, 4]`. **Example 2:** **Input:** nums = [5,11,20,20], k = 5, numOperations = 1 **Output:** 2 **Explanation:** We can achieve a maximum frequency of two by: Adding 0 to `nums[1]`. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 105` `0 <= k <= 105` `0 <= numOperations <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,5], k = 1, numOperations = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [5,11,20,20], k = 5, numOperations = 1", + "output": "2 " + } + ], + "constraints": [ + "Select an index i that was not selected in any previous operations.", + "Add an integer in the range [-k, k] to nums[i].", + "Adding 0 to nums[1]. nums becomes [1, 4, 5].", + "Adding -1 to nums[2]. nums becomes [1, 4, 4].", + "Adding 0 to nums[1].", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 105", + "0 <= k <= 105", + "0 <= numOperations <= nums.length" + ], + "python_template": "class Solution(object):\n def maxFrequency(self, nums, k, numOperations):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type numOperations: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxFrequency(int[] nums, int k, int numOperations) {\n \n }\n}", + "metadata": { + "func_name": "maxFrequency" + } +} \ No newline at end of file diff --git a/maximum-frequency-of-an-element-after-performing-operations-ii.json b/maximum-frequency-of-an-element-after-performing-operations-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..287673f1d025c69ac87af8d917d5ce3dcd2d0a06 --- /dev/null +++ b/maximum-frequency-of-an-element-after-performing-operations-ii.json @@ -0,0 +1,36 @@ +{ + "id": 3640, + "name": "maximum-frequency-of-an-element-after-performing-operations-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-ii/", + "date": "2024-10-26", + "task_description": "You are given an integer array `nums` and two integers `k` and `numOperations`. You must perform an **operation** `numOperations` times on `nums`, where in each operation you: Select an index `i` that was **not** selected in any previous operations. Add an integer in the range `[-k, k]` to `nums[i]`. Return the **maximum** possible frequency of any element in `nums` after performing the **operations**. **Example 1:** **Input:** nums = [1,4,5], k = 1, numOperations = 2 **Output:** 2 **Explanation:** We can achieve a maximum frequency of two by: Adding 0 to `nums[1]`, after which `nums` becomes `[1, 4, 5]`. Adding -1 to `nums[2]`, after which `nums` becomes `[1, 4, 4]`. **Example 2:** **Input:** nums = [5,11,20,20], k = 5, numOperations = 1 **Output:** 2 **Explanation:** We can achieve a maximum frequency of two by: Adding 0 to `nums[1]`. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `0 <= k <= 109` `0 <= numOperations <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,5], k = 1, numOperations = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [5,11,20,20], k = 5, numOperations = 1", + "output": "2 " + } + ], + "constraints": [ + "Select an index i that was not selected in any previous operations.", + "Add an integer in the range [-k, k] to nums[i].", + "Adding 0 to nums[1], after which nums becomes [1, 4, 5].", + "Adding -1 to nums[2], after which nums becomes [1, 4, 4].", + "Adding 0 to nums[1].", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "0 <= k <= 109", + "0 <= numOperations <= nums.length" + ], + "python_template": "class Solution(object):\n def maxFrequency(self, nums, k, numOperations):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type numOperations: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxFrequency(int[] nums, int k, int numOperations) {\n \n }\n}", + "metadata": { + "func_name": "maxFrequency" + } +} \ No newline at end of file diff --git a/maximum-good-subarray-sum.json b/maximum-good-subarray-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..fc136e7d78ecc23b0509967dfc59b42f7d64ed4c --- /dev/null +++ b/maximum-good-subarray-sum.json @@ -0,0 +1,35 @@ +{ + "id": 3265, + "name": "maximum-good-subarray-sum", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-good-subarray-sum/", + "date": "2024-01-20", + "task_description": "You are given an array `nums` of length `n` and a **positive** integer `k`. A subarray of `nums` is called **good** if the **absolute difference** between its first and last element is **exactly** `k`, in other words, the subarray `nums[i..j]` is good if `|nums[i] - nums[j]| == k`. Return _the **maximum** sum of a **good** subarray of _`nums`. _If there are no good subarrays__, return _`0`. **Example 1:** ``` **Input:** nums = [1,2,3,4,5,6], k = 1 **Output:** 11 **Explanation:** The absolute difference between the first and last element must be 1 for a good subarray. All the good subarrays are: [1,2], [2,3], [3,4], [4,5], and [5,6]. The maximum subarray sum is 11 for the subarray [5,6]. ``` **Example 2:** ``` **Input:** nums = [-1,3,2,4,5], k = 3 **Output:** 11 **Explanation:** The absolute difference between the first and last element must be 3 for a good subarray. All the good subarrays are: [-1,3,2], and [2,4,5]. The maximum subarray sum is 11 for the subarray [2,4,5]. ``` **Example 3:** ``` **Input:** nums = [-1,-2,-3,-4], k = 2 **Output:** -6 **Explanation:** The absolute difference between the first and last element must be 2 for a good subarray. All the good subarrays are: [-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is -6 for the subarray [-1,-2,-3]. ``` **Constraints:** `2 <= nums.length <= 105` `-109 <= nums[i] <= 109` `1 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5,6], k = 1", + "output": "11 " + }, + { + "label": "Example 2", + "input": "nums = [-1,3,2,4,5], k = 3", + "output": "11 " + }, + { + "label": "Example 3", + "input": "nums = [-1,-2,-3,-4], k = 2", + "output": "-6 " + } + ], + "constraints": [ + "2 <= nums.length <= 105", + "-109 <= nums[i] <= 109", + "1 <= k <= 109" + ], + "python_template": "class Solution(object):\n def maximumSubarraySum(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 maximumSubarraySum(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumSubarraySum" + } +} \ No newline at end of file diff --git a/maximum-height-of-a-triangle.json b/maximum-height-of-a-triangle.json new file mode 100644 index 0000000000000000000000000000000000000000..41c77a7ea2f50d81bead23e3137d1d3daa99350d --- /dev/null +++ b/maximum-height-of-a-triangle.json @@ -0,0 +1,38 @@ +{ + "id": 3469, + "name": "maximum-height-of-a-triangle", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-height-of-a-triangle/", + "date": "2024-06-23", + "task_description": "You are given two integers `red` and `blue` representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1st row will have 1 ball, the 2nd row will have 2 balls, the 3rd row will have 3 balls, and so on. All the balls in a particular row should be the **same** color, and adjacent rows should have **different** colors. Return the **maximum**_ height of the triangle_ that can be achieved. **Example 1:** **Input:** red = 2, blue = 4 **Output:** 3 **Explanation:** The only possible arrangement is shown above. **Example 2:** **Input:** red = 2, blue = 1 **Output:** 2 **Explanation:** The only possible arrangement is shown above. **Example 3:** **Input:** red = 1, blue = 1 **Output:** 1 **Example 4:** **Input:** red = 10, blue = 1 **Output:** 2 **Explanation:** The only possible arrangement is shown above. **Constraints:** `1 <= red, blue <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "red = 2, blue = 4", + "output": "3 " + }, + { + "label": "Example 2", + "input": "red = 2, blue = 1", + "output": "2 " + }, + { + "label": "Example 3", + "input": "red = 1, blue = 1", + "output": "" + }, + { + "label": "Example 4", + "input": "red = 10, blue = 1", + "output": "2 " + } + ], + "constraints": [ + "1 <= red, blue <= 100" + ], + "python_template": "class Solution(object):\n def maxHeightOfTriangle(self, red, blue):\n \"\"\"\n :type red: int\n :type blue: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxHeightOfTriangle(int red, int blue) {\n \n }\n}", + "metadata": { + "func_name": "maxHeightOfTriangle" + } +} \ No newline at end of file diff --git a/maximum-manhattan-distance-after-k-changes.json b/maximum-manhattan-distance-after-k-changes.json new file mode 100644 index 0000000000000000000000000000000000000000..ce87b26bceafd1e41c630a098d3a3e545e0776fb --- /dev/null +++ b/maximum-manhattan-distance-after-k-changes.json @@ -0,0 +1,34 @@ +{ + "id": 3754, + "name": "maximum-manhattan-distance-after-k-changes", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-manhattan-distance-after-k-changes/", + "date": "2025-01-26", + "task_description": "You are given a string `s` consisting of the characters `'N'`, `'S'`, `'E'`, and `'W'`, where `s[i]` indicates movements in an infinite grid: `'N'` : Move north by 1 unit. `'S'` : Move south by 1 unit. `'E'` : Move east by 1 unit. `'W'` : Move west by 1 unit. Initially, you are at the origin `(0, 0)`. You can change **at most** `k` characters to any of the four directions. Find the **maximum** **Manhattan distance** from the origin that can be achieved **at any time** while performing the movements **in order**. The **Manhattan Distance** between two cells `(xi, yi)` and `(xj, yj)` is `|xi - xj| + |yi - yj|`. **Example 1:** **Input:** s = \"NWSE\", k = 1 **Output:** 3 **Explanation:** Change `s[2]` from `'S'` to `'N'`. The string `s` becomes `\"NWNE\"`. Movement Position (x, y) Manhattan Distance Maximum s[0] == 'N' (0, 1) 0 + 1 = 1 1 s[1] == 'W' (-1, 1) 1 + 1 = 2 2 s[2] == 'N' (-1, 2) 1 + 2 = 3 3 s[3] == 'E' (0, 2) 0 + 2 = 2 3 The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output. **Example 2:** **Input:** s = \"NSWWEW\", k = 3 **Output:** 6 **Explanation:** Change `s[1]` from `'S'` to `'N'`, and `s[4]` from `'E'` to `'W'`. The string `s` becomes `\"NNWWWW\"`. The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output. **Constraints:** `1 <= s.length <= 105` `0 <= k <= s.length` `s` consists of only `'N'`, `'S'`, `'E'`, and `'W'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"NWSE\", k = 1", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \"NSWWEW\", k = 3", + "output": "6 " + } + ], + "constraints": [ + "'N' : Move north by 1 unit.", + "'S' : Move south by 1 unit.", + "'E' : Move east by 1 unit.", + "'W' : Move west by 1 unit.", + "1 <= s.length <= 105", + "0 <= k <= s.length", + "s consists of only 'N', 'S', 'E', and 'W'." + ], + "python_template": "class Solution(object):\n def maxDistance(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxDistance(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxDistance" + } +} \ No newline at end of file diff --git a/maximum-matching-of-players-with-trainers.json b/maximum-matching-of-players-with-trainers.json new file mode 100644 index 0000000000000000000000000000000000000000..550c977cfe792525968b8732be483a4df6accd1a --- /dev/null +++ b/maximum-matching-of-players-with-trainers.json @@ -0,0 +1,29 @@ +{ + "id": 2497, + "name": "maximum-matching-of-players-with-trainers", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-matching-of-players-with-trainers/", + "date": "2022-09-03", + "task_description": "You are given a **0-indexed** integer array `players`, where `players[i]` represents the **ability** of the `ith` player. You are also given a **0-indexed** integer array `trainers`, where `trainers[j]` represents the **training capacity **of the `jth` trainer. The `ith` player can **match** with the `jth` trainer if the player's ability is **less than or equal to** the trainer's training capacity. Additionally, the `ith` player can be matched with at most one trainer, and the `jth` trainer can be matched with at most one player. Return _the **maximum** number of matchings between _`players`_ and _`trainers`_ that satisfy these conditions._ **Example 1:** ``` **Input:** players = [4,7,9], trainers = [8,2,5,8] **Output:** 2 **Explanation:** One of the ways we can form two matchings is as follows: - players[0] can be matched with trainers[0] since 4 <= 8. - players[1] can be matched with trainers[3] since 7 <= 8. It can be proven that 2 is the maximum number of matchings that can be formed. ``` **Example 2:** ``` **Input:** players = [1,1,1], trainers = [10] **Output:** 1 **Explanation:** The trainer can be matched with any of the 3 players. Each player can only be matched with one trainer, so the maximum answer is 1. ``` **Constraints:** `1 <= players.length, trainers.length <= 105` `1 <= players[i], trainers[j] <= 109` **Note:** This question is the same as 445: Assign Cookies.", + "test_case": [ + { + "label": "Example 1", + "input": "players = [4,7,9], trainers = [8,2,5,8]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "players = [1,1,1], trainers = [10]", + "output": "1 " + } + ], + "constraints": [ + "1 <= players.length, trainers.length <= 105", + "1 <= players[i], trainers[j] <= 109" + ], + "python_template": "class Solution(object):\n def matchPlayersAndTrainers(self, players, trainers):\n \"\"\"\n :type players: List[int]\n :type trainers: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int matchPlayersAndTrainers(int[] players, int[] trainers) {\n \n }\n}", + "metadata": { + "func_name": "matchPlayersAndTrainers" + } +} \ No newline at end of file diff --git a/maximum-number-of-distinct-elements-after-operations.json b/maximum-number-of-distinct-elements-after-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..7bcbcd936f4ce99c21d04167f15d05775671549e --- /dev/null +++ b/maximum-number-of-distinct-elements-after-operations.json @@ -0,0 +1,31 @@ +{ + "id": 3620, + "name": "maximum-number-of-distinct-elements-after-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-number-of-distinct-elements-after-operations/", + "date": "2024-12-15", + "task_description": "You are given an integer array `nums` and an integer `k`. You are allowed to perform the following **operation** on each element of the array **at most** _once_: Add an integer in the range `[-k, k]` to the element. Return the **maximum** possible number of **distinct** elements in `nums` after performing the **operations**. **Example 1:** **Input:** nums = [1,2,2,3,3,4], k = 2 **Output:** 6 **Explanation:** `nums` changes to `[-1, 0, 1, 2, 3, 4]` after performing operations on the first four elements. **Example 2:** **Input:** nums = [4,4,4,4], k = 1 **Output:** 3 **Explanation:** By adding -1 to `nums[0]` and 1 to `nums[1]`, `nums` changes to `[3, 5, 4, 4]`. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `0 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,2,3,3,4], k = 2", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [4,4,4,4], k = 1", + "output": "3 " + } + ], + "constraints": [ + "Add an integer in the range [-k, k] to the element.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "0 <= k <= 109" + ], + "python_template": "class Solution(object):\n def maxDistinctElements(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 maxDistinctElements(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxDistinctElements" + } +} \ No newline at end of file diff --git a/maximum-number-of-fish-in-a-grid.json b/maximum-number-of-fish-in-a-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..0b3aae30cac53c84ac88051e30e4237148236852 --- /dev/null +++ b/maximum-number-of-fish-in-a-grid.json @@ -0,0 +1,35 @@ +{ + "id": 2764, + "name": "maximum-number-of-fish-in-a-grid", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-number-of-fish-in-a-grid/", + "date": "2023-04-15", + "task_description": "You are given a **0-indexed** 2D matrix `grid` of size `m x n`, where `(r, c)` represents: A **land** cell if `grid[r][c] = 0`, or A **water** cell containing `grid[r][c]` fish, if `grid[r][c] > 0`. A fisher can start at any **water** cell `(r, c)` and can do the following operations any number of times: Catch all the fish at cell `(r, c)`, or Move to any adjacent **water** cell. Return _the **maximum** number of fish the fisher can catch if he chooses his starting cell optimally, or _`0` if no water cell exists. An **adjacent** cell of the cell `(r, c)`, is one of the cells `(r, c + 1)`, `(r, c - 1)`, `(r + 1, c)` or `(r - 1, c)` if it exists. **Example 1:** ``` **Input:** grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]] **Output:** 7 **Explanation:** The fisher can start at cell `(1,3)` and collect 3 fish, then move to cell `(2,3)` and collect 4 fish. ``` **Example 2:** ``` **Input:** grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]] **Output:** 1 **Explanation:** The fisher can start at cells (0,0) or (3,3) and collect a single fish. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 10` `0 <= grid[i][j] <= 10`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]", + "output": "1 " + } + ], + "constraints": [ + "A land cell if grid[r][c] = 0, or", + "A water cell containing grid[r][c] fish, if grid[r][c] > 0.", + "Catch all the fish at cell (r, c), or", + "Move to any adjacent water cell.", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 10", + "0 <= grid[i][j] <= 10" + ], + "python_template": "class Solution(object):\n def findMaxFish(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int findMaxFish(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "findMaxFish" + } +} \ No newline at end of file diff --git a/maximum-number-of-groups-entering-a-competition.json b/maximum-number-of-groups-entering-a-competition.json new file mode 100644 index 0000000000000000000000000000000000000000..f66123bdc00f8e1e8a09f98aceef1b6361e34e09 --- /dev/null +++ b/maximum-number-of-groups-entering-a-competition.json @@ -0,0 +1,31 @@ +{ + "id": 2437, + "name": "maximum-number-of-groups-entering-a-competition", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-number-of-groups-entering-a-competition/", + "date": "2022-07-24", + "task_description": "You are given a positive integer array `grades` which represents the grades of students in a university. You would like to enter **all** these students into a competition in **ordered** non-empty groups, such that the ordering meets the following conditions: The sum of the grades of students in the `ith` group is **less than** the sum of the grades of students in the `(i + 1)th` group, for all groups (except the last). The total number of students in the `ith` group is **less than** the total number of students in the `(i + 1)th` group, for all groups (except the last). Return _the **maximum** number of groups that can be formed_. **Example 1:** ``` **Input:** grades = [10,6,12,7,3,5] **Output:** 3 **Explanation:** The following is a possible way to form 3 groups of students: - 1st group has the students with grades = [12]. Sum of grades: 12. Student count: 1 - 2nd group has the students with grades = [6,7]. Sum of grades: 6 + 7 = 13. Student count: 2 - 3rd group has the students with grades = [10,3,5]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3 It can be shown that it is not possible to form more than 3 groups. ``` **Example 2:** ``` **Input:** grades = [8,8] **Output:** 1 **Explanation:** We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups. ``` **Constraints:** `1 <= grades.length <= 105` `1 <= grades[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "grades = [10,6,12,7,3,5]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "grades = [8,8]", + "output": "1 " + } + ], + "constraints": [ + "The sum of the grades of students in the ith group is less than the sum of the grades of students in the (i + 1)th group, for all groups (except the last).", + "The total number of students in the ith group is less than the total number of students in the (i + 1)th group, for all groups (except the last).", + "1 <= grades.length <= 105", + "1 <= grades[i] <= 105" + ], + "python_template": "class Solution(object):\n def maximumGroups(self, grades):\n \"\"\"\n :type grades: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumGroups(int[] grades) {\n \n }\n}", + "metadata": { + "func_name": "maximumGroups" + } +} \ No newline at end of file diff --git a/maximum-number-of-groups-with-increasing-length.json b/maximum-number-of-groups-with-increasing-length.json new file mode 100644 index 0000000000000000000000000000000000000000..eefda209f9cb8b7f1bb649ed6c1d1f29019fc84a --- /dev/null +++ b/maximum-number-of-groups-with-increasing-length.json @@ -0,0 +1,36 @@ +{ + "id": 2919, + "name": "maximum-number-of-groups-with-increasing-length", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-number-of-groups-with-increasing-length/", + "date": "2023-07-16", + "task_description": "You are given a **0-indexed** array `usageLimits` of length `n`. Your task is to create **groups** using numbers from `0` to `n - 1`, ensuring that each number, `i`, is used no more than `usageLimits[i]` times in total **across all groups**. You must also satisfy the following conditions: Each group must consist of **distinct **numbers, meaning that no duplicate numbers are allowed within a single group. Each group (except the first one) must have a length **strictly greater** than the previous group. Return _an integer denoting the **maximum** number of groups you can create while satisfying these conditions._ **Example 1:** ``` **Input:** `usageLimits` = [1,2,5] **Output:** 3 **Explanation:** In this example, we can use 0 at most once, 1 at most twice, and 2 at most five times. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [2]. Group 2 contains the numbers [1,2]. Group 3 contains the numbers [0,1,2]. It can be shown that the maximum number of groups is 3. So, the output is 3. ``` **Example 2:** ``` **Input:** `usageLimits` = [2,1,2] **Output:** 2 **Explanation:** In this example, we can use 0 at most twice, 1 at most once, and 2 at most twice. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. Group 2 contains the numbers [1,2]. It can be shown that the maximum number of groups is 2. So, the output is 2. ``` **Example 3:** ``` **Input:** `usageLimits` = [1,1] **Output:** 1 **Explanation:** In this example, we can use both 0 and 1 at most once. One way of creating the maximum number of groups while satisfying the conditions is: Group 1 contains the number [0]. It can be shown that the maximum number of groups is 1. So, the output is 1. ``` **Constraints:** `1 <= usageLimits.length <= 105` `1 <= usageLimits[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "usageLimits = [1,2,5]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "usageLimits = [2,1,2]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "usageLimits = [1,1]", + "output": "1 " + } + ], + "constraints": [ + "Each group must consist of distinct numbers, meaning that no duplicate numbers are allowed within a single group.", + "Each group (except the first one) must have a length strictly greater than the previous group.", + "1 <= usageLimits.length <= 105", + "1 <= usageLimits[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxIncreasingGroups(self, usageLimits):\n \"\"\"\n :type usageLimits: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxIncreasingGroups(List usageLimits) {\n \n }\n}", + "metadata": { + "func_name": "maxIncreasingGroups" + } +} \ No newline at end of file diff --git a/maximum-number-of-jumps-to-reach-the-last-index.json b/maximum-number-of-jumps-to-reach-the-last-index.json new file mode 100644 index 0000000000000000000000000000000000000000..b2432daa44bf33398eeaf24d9b88553b2045ba44 --- /dev/null +++ b/maximum-number-of-jumps-to-reach-the-last-index.json @@ -0,0 +1,37 @@ +{ + "id": 2855, + "name": "maximum-number-of-jumps-to-reach-the-last-index", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-number-of-jumps-to-reach-the-last-index/", + "date": "2023-07-02", + "task_description": "You are given a **0-indexed** array `nums` of `n` integers and an integer `target`. You are initially positioned at index `0`. In one step, you can jump from index `i` to any index `j` such that: `0 <= i < j < n` `-target <= nums[j] - nums[i] <= target` Return _the **maximum number of jumps** you can make to reach index_ `n - 1`. If there is no way to reach index `n - 1`, return `-1`. **Example 1:** ``` **Input:** nums = [1,3,6,4,1,2], target = 2 **Output:** 3 **Explanation:** To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 3. - Jump from index 3 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3. ``` **Example 2:** ``` **Input:** nums = [1,3,6,4,1,2], target = 3 **Output:** 5 **Explanation:** To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence: - Jump from index 0 to index 1. - Jump from index 1 to index 2. - Jump from index 2 to index 3. - Jump from index 3 to index 4. - Jump from index 4 to index 5. It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5. ``` **Example 3:** ``` **Input:** nums = [1,3,6,4,1,2], target = 0 **Output:** -1 **Explanation:** It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1. ``` **Constraints:** `2 <= nums.length == n <= 1000` `-109 <= nums[i] <= 109` `0 <= target <= 2 * 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,6,4,1,2], target = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,6,4,1,2], target = 3", + "output": "5 " + }, + { + "label": "Example 3", + "input": "nums = [1,3,6,4,1,2], target = 0", + "output": "-1 " + } + ], + "constraints": [ + "0 <= i < j < n", + "-target <= nums[j] - nums[i] <= target", + "2 <= nums.length == n <= 1000", + "-109 <= nums[i] <= 109", + "0 <= target <= 2 * 109" + ], + "python_template": "class Solution(object):\n def maximumJumps(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 maximumJumps(int[] nums, int target) {\n \n }\n}", + "metadata": { + "func_name": "maximumJumps" + } +} \ No newline at end of file diff --git a/maximum-number-of-k-divisible-components.json b/maximum-number-of-k-divisible-components.json new file mode 100644 index 0000000000000000000000000000000000000000..c3ccda93c0034b9aaaf21c523fe4c41e0a919992 --- /dev/null +++ b/maximum-number-of-k-divisible-components.json @@ -0,0 +1,36 @@ +{ + "id": 3058, + "name": "maximum-number-of-k-divisible-components", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-number-of-k-divisible-components/", + "date": "2023-09-16", + "task_description": "There is an undirected tree with `n` nodes labeled from `0` to `n - 1`. You are given the integer `n` and a 2D integer array `edges` of length `n - 1`, where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree. You are also given a **0-indexed** integer array `values` of length `n`, where `values[i]` is the **value** associated with the `ith` node, and an integer `k`. A **valid split** of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by `k`, where the **value of a connected component** is the sum of the values of its nodes. Return _the **maximum number of components** in any valid split_. **Example 1:** ``` **Input:** n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6 **Output:** 2 **Explanation:** We remove the edge connecting node 1 with 2. The resulting split is valid because: - The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12. - The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6. It can be shown that no other valid split has more than 2 connected components. ``` **Example 2:** ``` **Input:** n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3 **Output:** 3 **Explanation:** We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because: - The value of the component containing node 0 is values[0] = 3. - The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9. - The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6. It can be shown that no other valid split has more than 3 connected components. ``` **Constraints:** `1 <= n <= 3 * 104` `edges.length == n - 1` `edges[i].length == 2` `0 <= ai, bi < n` `values.length == n` `0 <= values[i] <= 109` `1 <= k <= 109` Sum of `values` is divisible by `k`. The input is generated such that `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6", + "output": "2 " + }, + { + "label": "Example 2", + "input": "n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3", + "output": "3 " + } + ], + "constraints": [ + "1 <= n <= 3 * 104", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ai, bi < n", + "values.length == n", + "0 <= values[i] <= 109", + "1 <= k <= 109", + "Sum of values is divisible by k.", + "The input is generated such that edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def maxKDivisibleComponents(self, n, edges, values, k):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type values: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxKDivisibleComponents(int n, int[][] edges, int[] values, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxKDivisibleComponents" + } +} \ No newline at end of file diff --git a/maximum-number-of-moves-to-kill-all-pawns.json b/maximum-number-of-moves-to-kill-all-pawns.json new file mode 100644 index 0000000000000000000000000000000000000000..14e8fa4b7f3f35df17d4c3657c6b816db6856fee --- /dev/null +++ b/maximum-number-of-moves-to-kill-all-pawns.json @@ -0,0 +1,45 @@ +{ + "id": 3560, + "name": "maximum-number-of-moves-to-kill-all-pawns", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-number-of-moves-to-kill-all-pawns/", + "date": "2024-09-01", + "task_description": "There is a `50 x 50` chessboard with **one** knight and some pawns on it. You are given two integers `kx` and `ky` where `(kx, ky)` denotes the position of the knight, and a 2D array `positions` where `positions[i] = [xi, yi]` denotes the position of the pawns on the chessboard. Alice and Bob play a _turn-based_ game, where Alice goes first. In each player's turn: The player _selects _a pawn that still exists on the board and captures it with the knight in the **fewest** possible **moves**. **Note** that the player can select **any** pawn, it **might not** be one that can be captured in the **least** number of moves. In the process of capturing the _selected_ pawn, the knight **may** pass other pawns **without** capturing them. **Only** the _selected_ pawn can be captured in _this_ turn. Alice is trying to **maximize** the **sum** of the number of moves made by _both_ players until there are no more pawns on the board, whereas Bob tries to **minimize** them. Return the **maximum** _total_ number of moves made during the game that Alice can achieve, assuming both players play **optimally**. Note that in one **move, **a chess knight has eight possible positions it can move to, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction. **Example 1:** **Input:** kx = 1, ky = 1, positions = [[0,0]] **Output:** 4 **Explanation:** The knight takes 4 moves to reach the pawn at `(0, 0)`. **Example 2:** **Input:** kx = 0, ky = 2, positions = [[1,1],[2,2],[3,3]] **Output:** 8 **Explanation:** **** Alice picks the pawn at `(2, 2)` and captures it in two moves: `(0, 2) -> (1, 4) -> (2, 2)`. Bob picks the pawn at `(3, 3)` and captures it in two moves: `(2, 2) -> (4, 1) -> (3, 3)`. Alice picks the pawn at `(1, 1)` and captures it in four moves: `(3, 3) -> (4, 1) -> (2, 2) -> (0, 3) -> (1, 1)`. **Example 3:** **Input:** kx = 0, ky = 0, positions = [[1,2],[2,4]] **Output:** 3 **Explanation:** Alice picks the pawn at `(2, 4)` and captures it in two moves: `(0, 0) -> (1, 2) -> (2, 4)`. Note that the pawn at `(1, 2)` is not captured. Bob picks the pawn at `(1, 2)` and captures it in one move: `(2, 4) -> (1, 2)`. **Constraints:** `0 <= kx, ky <= 49` `1 <= positions.length <= 15` `positions[i].length == 2` `0 <= positions[i][0], positions[i][1] <= 49` All `positions[i]` are unique. The input is generated such that `positions[i] != [kx, ky]` for all `0 <= i < positions.length`.", + "test_case": [ + { + "label": "Example 1", + "input": "kx = 1, ky = 1, positions = [[0,0]]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "kx = 0, ky = 2, positions = [[1,1],[2,2],[3,3]]", + "output": "8 " + }, + { + "label": "Example 3", + "input": "kx = 0, ky = 0, positions = [[1,2],[2,4]]", + "output": "3 " + } + ], + "constraints": [ + "The player selects a pawn that still exists on the board and captures it with the knight in the fewest possible moves. Note that the player can select any pawn, it might not be one that can be captured in the least number of moves.", + "In the process of capturing the selected pawn, the knight may pass other pawns without capturing them. Only the selected pawn can be captured in this turn.", + "Alice picks the pawn at (2, 2) and captures it in two moves: (0, 2) -> (1, 4) -> (2, 2).", + "Bob picks the pawn at (3, 3) and captures it in two moves: (2, 2) -> (4, 1) -> (3, 3).", + "Alice picks the pawn at (1, 1) and captures it in four moves: (3, 3) -> (4, 1) -> (2, 2) -> (0, 3) -> (1, 1).", + "Alice picks the pawn at (2, 4) and captures it in two moves: (0, 0) -> (1, 2) -> (2, 4). Note that the pawn at (1, 2) is not captured.", + "Bob picks the pawn at (1, 2) and captures it in one move: (2, 4) -> (1, 2).", + "0 <= kx, ky <= 49", + "1 <= positions.length <= 15", + "positions[i].length == 2", + "0 <= positions[i][0], positions[i][1] <= 49", + "All positions[i] are unique.", + "The input is generated such that positions[i] != [kx, ky] for all 0 <= i < positions.length." + ], + "python_template": "class Solution(object):\n def maxMoves(self, kx, ky, positions):\n \"\"\"\n :type kx: int\n :type ky: int\n :type positions: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxMoves(int kx, int ky, int[][] positions) {\n \n }\n}", + "metadata": { + "func_name": "maxMoves" + } +} \ No newline at end of file diff --git a/maximum-number-of-non-overlapping-palindrome-substrings.json b/maximum-number-of-non-overlapping-palindrome-substrings.json new file mode 100644 index 0000000000000000000000000000000000000000..9662dac28c2bfad58d1a19d51f38d97bba5d9724 --- /dev/null +++ b/maximum-number-of-non-overlapping-palindrome-substrings.json @@ -0,0 +1,31 @@ +{ + "id": 2559, + "name": "maximum-number-of-non-overlapping-palindrome-substrings", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/", + "date": "2022-11-06", + "task_description": "You are given a string `s` and a **positive** integer `k`. Select a set of **non-overlapping** substrings from the string `s` that satisfy the following conditions: The **length** of each substring is **at least** `k`. Each substring is a **palindrome**. Return _the **maximum** number of substrings in an optimal selection_. A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` **Input:** s = \"abaccdbbd\", k = 3 **Output:** 2 **Explanation:** We can select the substrings underlined in s = \"**aba**cc**dbbd**\". Both \"aba\" and \"dbbd\" are palindromes and have a length of at least k = 3. It can be shown that we cannot find a selection with more than two valid substrings. ``` **Example 2:** ``` **Input:** s = \"adbcda\", k = 2 **Output:** 0 **Explanation:** There is no palindrome substring of length at least 2 in the string. ``` **Constraints:** `1 <= k <= s.length <= 2000` `s` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abaccdbbd\", k = 3", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"adbcda\", k = 2", + "output": "0 " + } + ], + "constraints": [ + "The length of each substring is at least k.", + "Each substring is a palindrome.", + "1 <= k <= s.length <= 2000", + "s consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def maxPalindromes(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxPalindromes(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxPalindromes" + } +} \ No newline at end of file diff --git a/maximum-number-of-operations-to-move-ones-to-the-end.json b/maximum-number-of-operations-to-move-ones-to-the-end.json new file mode 100644 index 0000000000000000000000000000000000000000..b208ae07df8ff4549c6c6bc19dc5b1eb2911e53a --- /dev/null +++ b/maximum-number-of-operations-to-move-ones-to-the-end.json @@ -0,0 +1,35 @@ +{ + "id": 3493, + "name": "maximum-number-of-operations-to-move-ones-to-the-end", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end/", + "date": "2024-07-14", + "task_description": "You are given a binary string `s`. You can perform the following operation on the string **any** number of times: Choose **any** index `i` from the string where `i + 1 < s.length` such that `s[i] == '1'` and `s[i + 1] == '0'`. Move the character `s[i]` to the **right** until it reaches the end of the string or another `'1'`. For example, for `s = \"010010\"`, if we choose `i = 1`, the resulting string will be `s = \"0**001**10\"`. Return the **maximum** number of operations that you can perform. **Example 1:** **Input:** s = \"1001101\" **Output:** 4 **Explanation:** We can perform the following operations: Choose index `i = 0`. The resulting string is `s = \"**001**1101\"`. Choose index `i = 4`. The resulting string is `s = \"0011**01**1\"`. Choose index `i = 3`. The resulting string is `s = \"001**01**11\"`. Choose index `i = 2`. The resulting string is `s = \"00**01**111\"`. **Example 2:** **Input:** s = \"00111\" **Output:** 0 **Constraints:** `1 <= s.length <= 105` `s[i]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"1001101\"", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"00111\"", + "output": "" + } + ], + "constraints": [ + "Choose any index i from the string where i + 1 < s.length such that s[i] == '1' and s[i + 1] == '0'.", + "Move the character s[i] to the right until it reaches the end of the string or another '1'. For example, for s = \"010010\", if we choose i = 1, the resulting string will be s = \"000110\".", + "Choose index i = 0. The resulting string is s = \"0011101\".", + "Choose index i = 4. The resulting string is s = \"0011011\".", + "Choose index i = 3. The resulting string is s = \"0010111\".", + "Choose index i = 2. The resulting string is s = \"0001111\".", + "1 <= s.length <= 105", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def maxOperations(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxOperations(String s) {\n \n }\n}", + "metadata": { + "func_name": "maxOperations" + } +} \ No newline at end of file diff --git a/maximum-number-of-operations-with-the-same-score-i.json b/maximum-number-of-operations-with-the-same-score-i.json new file mode 100644 index 0000000000000000000000000000000000000000..98edb2bec5018317c4c1ab4dc27d1dbf7608b0fd --- /dev/null +++ b/maximum-number-of-operations-with-the-same-score-i.json @@ -0,0 +1,41 @@ +{ + "id": 3320, + "name": "maximum-number-of-operations-with-the-same-score-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i/", + "date": "2024-02-03", + "task_description": "You are given an array of integers `nums`. Consider the following operation: Delete the first two elements `nums` and define the _score_ of the operation as the sum of these two elements. You can perform this operation until `nums` contains fewer than two elements. Additionally, the **same** _score_ must be achieved in **all** operations. Return the **maximum** number of operations you can perform. **Example 1:** **Input:** nums = [3,2,1,4,5] **Output:** 2 **Explanation:** We can perform the first operation with the score `3 + 2 = 5`. After this operation, `nums = [1,4,5]`. We can perform the second operation as its score is `4 + 1 = 5`, the same as the previous operation. After this operation, `nums = [5]`. As there are fewer than two elements, we can't perform more operations. **Example 2:** **Input:** nums = [1,5,3,3,4,1,3,2,2,3] **Output:** 2 **Explanation:** We can perform the first operation with the score `1 + 5 = 6`. After this operation, `nums = [3,3,4,1,3,2,2,3]`. We can perform the second operation as its score is `3 + 3 = 6`, the same as the previous operation. After this operation, `nums = [4,1,3,2,2,3]`. We cannot perform the next operation as its score is `4 + 1 = 5`, which is different from the previous scores. **Example 3:** **Input:** nums = [5,3] **Output:** 1 **Constraints:** `2 <= nums.length <= 100` `1 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,2,1,4,5]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,5,3,3,4,1,3,2,2,3]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [5,3]", + "output": "" + } + ], + "constraints": [ + "Delete the first two elements nums and define the score of the operation as the sum of these two elements.", + "We can perform the first operation with the score 3 + 2 = 5. After this operation, nums = [1,4,5].", + "We can perform the second operation as its score is 4 + 1 = 5, the same as the previous operation. After this operation, nums = [5].", + "As there are fewer than two elements, we can't perform more operations.", + "We can perform the first operation with the score 1 + 5 = 6. After this operation, nums = [3,3,4,1,3,2,2,3].", + "We can perform the second operation as its score is 3 + 3 = 6, the same as the previous operation. After this operation, nums = [4,1,3,2,2,3].", + "We cannot perform the next operation as its score is 4 + 1 = 5, which is different from the previous scores.", + "2 <= nums.length <= 100", + "1 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def maxOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxOperations(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxOperations" + } +} \ No newline at end of file diff --git a/maximum-number-of-operations-with-the-same-score-ii.json b/maximum-number-of-operations-with-the-same-score-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..5c2ab5fb019c85203c5f617edb0389d55b7ab73b --- /dev/null +++ b/maximum-number-of-operations-with-the-same-score-ii.json @@ -0,0 +1,32 @@ +{ + "id": 3318, + "name": "maximum-number-of-operations-with-the-same-score-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-ii/", + "date": "2024-02-03", + "task_description": "Given an array of integers called `nums`, you can perform **any** of the following operation while `nums` contains **at least** `2` elements: Choose the first two elements of `nums` and delete them. Choose the last two elements of `nums` and delete them. Choose the first and the last elements of `nums` and delete them. The** score** of the operation is the sum of the deleted elements. Your task is to find the **maximum** number of operations that can be performed, such that **all operations have the same score**. Return _the **maximum** number of operations possible that satisfy the condition mentioned above_. **Example 1:** ``` **Input:** nums = [3,2,1,2,3,4] **Output:** 3 **Explanation:** We perform the following operations: - Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4]. - Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3]. - Delete the first and the last elements, with score 2 + 3 = 5, nums = []. We are unable to perform any more operations as nums is empty. ``` **Example 2:** ``` **Input:** nums = [3,2,6,1,4] **Output:** 2 **Explanation:** We perform the following operations: - Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4]. - Delete the last two elements, with score 1 + 4 = 5, nums = [6]. It can be proven that we can perform at most 2 operations. ``` **Constraints:** `2 <= nums.length <= 2000` `1 <= nums[i] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,2,1,2,3,4]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [3,2,6,1,4]", + "output": "2 " + } + ], + "constraints": [ + "Choose the first two elements of nums and delete them.", + "Choose the last two elements of nums and delete them.", + "Choose the first and the last elements of nums and delete them.", + "2 <= nums.length <= 2000", + "1 <= nums[i] <= 1000" + ], + "python_template": "class Solution(object):\n def maxOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxOperations(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxOperations" + } +} \ No newline at end of file diff --git a/maximum-number-of-pairs-in-array.json b/maximum-number-of-pairs-in-array.json new file mode 100644 index 0000000000000000000000000000000000000000..1de45f308f766b1b6c29c81599f8269a07209518 --- /dev/null +++ b/maximum-number-of-pairs-in-array.json @@ -0,0 +1,36 @@ +{ + "id": 2421, + "name": "maximum-number-of-pairs-in-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-number-of-pairs-in-array/", + "date": "2022-07-10", + "task_description": "You are given a **0-indexed** integer array `nums`. In one operation, you may do the following: Choose **two** integers in `nums` that are **equal**. Remove both integers from `nums`, forming a **pair**. The operation is done on `nums` as many times as possible. Return _a **0-indexed** integer array _`answer`_ of size _`2`_ where _`answer[0]`_ is the number of pairs that are formed and _`answer[1]`_ is the number of leftover integers in _`nums`_ after doing the operation as many times as possible_. **Example 1:** ``` **Input:** nums = [1,3,2,1,3,2,2] **Output:** [3,1] **Explanation:** Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2]. Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2]. Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2]. No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums. ``` **Example 2:** ``` **Input:** nums = [1,1] **Output:** [1,0] **Explanation:** Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = []. No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums. ``` **Example 3:** ``` **Input:** nums = [0] **Output:** [0,1] **Explanation:** No pairs can be formed, and there is 1 number leftover in nums. ``` **Constraints:** `1 <= nums.length <= 100` `0 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,2,1,3,2,2]", + "output": "[3,1] " + }, + { + "label": "Example 2", + "input": "nums = [1,1]", + "output": "[1,0] " + }, + { + "label": "Example 3", + "input": "nums = [0]", + "output": "[0,1] " + } + ], + "constraints": [ + "Choose two integers in nums that are equal.", + "Remove both integers from nums, forming a pair.", + "1 <= nums.length <= 100", + "0 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def numberOfPairs(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] numberOfPairs(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "numberOfPairs" + } +} \ No newline at end of file diff --git a/maximum-number-of-robots-within-budget.json b/maximum-number-of-robots-within-budget.json new file mode 100644 index 0000000000000000000000000000000000000000..3cc5ba23fa4a5068978c90ca2e07d634439e75bd --- /dev/null +++ b/maximum-number-of-robots-within-budget.json @@ -0,0 +1,31 @@ +{ + "id": 2449, + "name": "maximum-number-of-robots-within-budget", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-number-of-robots-within-budget/", + "date": "2022-08-20", + "task_description": "You have `n` robots. You are given two **0-indexed** integer arrays, `chargeTimes` and `runningCosts`, both of length `n`. The `ith` robot costs `chargeTimes[i]` units to charge and costs `runningCosts[i]` units to run. You are also given an integer `budget`. The **total cost** of running `k` chosen robots is equal to `max(chargeTimes) + k * sum(runningCosts)`, where `max(chargeTimes)` is the largest charge cost among the `k` robots and `sum(runningCosts)` is the sum of running costs among the `k` robots. Return_ the **maximum** number of **consecutive** robots you can run such that the total cost **does not** exceed _`budget`. **Example 1:** ``` **Input:** chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25 **Output:** 3 **Explanation:** It is possible to run all individual and consecutive pairs of robots within budget. To obtain answer 3, consider the first 3 robots. The total cost will be max(3,6,1) + 3 * sum(2,1,3) = 6 + 3 * 6 = 24 which is less than 25. It can be shown that it is not possible to run more than 3 consecutive robots within budget, so we return 3. ``` **Example 2:** ``` **Input:** chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19 **Output:** 0 **Explanation:** No robot can be run that does not exceed the budget, so we return 0. ``` **Constraints:** `chargeTimes.length == runningCosts.length == n` `1 <= n <= 5 * 104` `1 <= chargeTimes[i], runningCosts[i] <= 105` `1 <= budget <= 1015`", + "test_case": [ + { + "label": "Example 1", + "input": "chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25", + "output": "3 " + }, + { + "label": "Example 2", + "input": "chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19", + "output": "0 " + } + ], + "constraints": [ + "chargeTimes.length == runningCosts.length == n", + "1 <= n <= 5 * 104", + "1 <= chargeTimes[i], runningCosts[i] <= 105", + "1 <= budget <= 1015" + ], + "python_template": "class Solution(object):\n def maximumRobots(self, chargeTimes, runningCosts, budget):\n \"\"\"\n :type chargeTimes: List[int]\n :type runningCosts: List[int]\n :type budget: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumRobots(int[] chargeTimes, int[] runningCosts, long budget) {\n \n }\n}", + "metadata": { + "func_name": "maximumRobots" + } +} \ No newline at end of file diff --git a/maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k.json b/maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..757e2d87fb0236354d520d7612d386ee608e928a --- /dev/null +++ b/maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k.json @@ -0,0 +1,29 @@ +{ + "id": 3240, + "name": "maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k/", + "date": "2024-01-07", + "task_description": "You are given an integer `k` and an integer `x`. The price of a number `num` is calculated by the count of set bits at positions `x`, `2x`, `3x`, etc., in its binary representation, starting from the least significant bit. The following table contains examples of how price is calculated. x num Binary Representation Price 1 13 00000**1****1**0**1** 3 2 13 00000**1**101 1 2 233 0**1**1**1**0**1**001 3 3 13 000001**1**01 1 3 362 **1**01**1**01010 2 The **accumulated price** of `num` is the total price of numbers from `1` to `num`. `num` is considered **cheap** if its accumulated price is less than or equal to `k`. Return the greatest cheap number. **Example 1:** **Input:** k = 9, x = 1 **Output:** 6 **Explanation:** As shown in the table below, `6` is the greatest cheap number. x num Binary Representation Price Accumulated Price 1 1 00**1** 1 1 1 2 0**1**0 1 2 1 3 0**1****1** 2 4 1 4 **1**00 1 5 1 5 **1**0**1** 2 7 1 6 **1****1**0 2 9 1 7 **1****1****1** 3 12 **Example 2:** **Input:** k = 7, x = 2 **Output:** 9 **Explanation:** As shown in the table below, `9` is the greatest cheap number. x num Binary Representation Price Accumulated Price 2 1 0001 0 0 2 2 00**1**0 1 1 2 3 00**1**1 1 2 2 4 0100 0 2 2 5 0101 0 2 2 6 01**1**0 1 3 2 7 01**1**1 1 4 2 8 **1**000 1 5 2 9 **1**001 1 6 2 10 **1**0**1**0 2 8 **Constraints:** `1 <= k <= 1015` `1 <= x <= 8`", + "test_case": [ + { + "label": "Example 1", + "input": "k = 9, x = 1", + "output": "6 " + }, + { + "label": "Example 2", + "input": "k = 7, x = 2", + "output": "9 " + } + ], + "constraints": [ + "1 <= k <= 1015", + "1 <= x <= 8" + ], + "python_template": "class Solution(object):\n def findMaximumNumber(self, k, x):\n \"\"\"\n :type k: int\n :type x: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long findMaximumNumber(long k, int x) {\n \n }\n}", + "metadata": { + "func_name": "findMaximumNumber" + } +} \ No newline at end of file diff --git a/maximum-odd-binary-number.json b/maximum-odd-binary-number.json new file mode 100644 index 0000000000000000000000000000000000000000..ac9c73266d73a31212ed62592cfb5a7de22024a5 --- /dev/null +++ b/maximum-odd-binary-number.json @@ -0,0 +1,30 @@ +{ + "id": 3055, + "name": "maximum-odd-binary-number", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-odd-binary-number/", + "date": "2023-09-17", + "task_description": "You are given a **binary** string `s` that contains at least one `'1'`. You have to **rearrange** the bits in such a way that the resulting binary number is the **maximum odd binary number** that can be created from this combination. Return _a string representing the maximum odd binary number that can be created from the given combination._ **Note **that the resulting string **can** have leading zeros. **Example 1:** ``` **Input:** s = \"010\" **Output:** \"001\" **Explanation:** Because there is just one '1', it must be in the last position. So the answer is \"001\". ``` **Example 2:** ``` **Input:** s = \"0101\" **Output:** \"1001\" **Explanation: **One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is \"100\". So the answer is \"1001\". ``` **Constraints:** `1 <= s.length <= 100` `s` consists only of `'0'` and `'1'`. `s` contains at least one `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"010\"", + "output": "\"001\" " + }, + { + "label": "Example 2", + "input": "s = \"0101\"", + "output": "\"1001\" " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "s consists only of '0' and '1'.", + "s contains at least one '1'." + ], + "python_template": "class Solution(object):\n def maximumOddBinaryNumber(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String maximumOddBinaryNumber(String s) {\n \n }\n}", + "metadata": { + "func_name": "maximumOddBinaryNumber" + } +} \ No newline at end of file diff --git a/maximum-or.json b/maximum-or.json new file mode 100644 index 0000000000000000000000000000000000000000..7460a90a18f58fe69eeaecfde4b7b3ec70d45c6a --- /dev/null +++ b/maximum-or.json @@ -0,0 +1,30 @@ +{ + "id": 2730, + "name": "maximum-or", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-or/", + "date": "2023-04-29", + "task_description": "You are given a **0-indexed** integer array `nums` of length `n` and an integer `k`. In an operation, you can choose an element and multiply it by `2`. Return _the maximum possible value of _`nums[0] | nums[1] | ... | nums[n - 1]` _that can be obtained after applying the operation on nums at most _`k`_ times_. Note that `a | b` denotes the **bitwise or** between two integers `a` and `b`. **Example 1:** ``` **Input:** nums = [12,9], k = 1 **Output:** 30 **Explanation:** If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30. ``` **Example 2:** ``` **Input:** nums = [8,1,2], k = 2 **Output:** 35 **Explanation:** If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `1 <= k <= 15`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [12,9], k = 1", + "output": "30 " + }, + { + "label": "Example 2", + "input": "nums = [8,1,2], k = 2", + "output": "35 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "1 <= k <= 15" + ], + "python_template": "class Solution(object):\n def maximumOr(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 maximumOr(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumOr" + } +} \ No newline at end of file diff --git a/maximum-palindromes-after-operations.json b/maximum-palindromes-after-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..65230d4de7cd58e26a1ae71fbea2926644acc680 --- /dev/null +++ b/maximum-palindromes-after-operations.json @@ -0,0 +1,36 @@ +{ + "id": 3317, + "name": "maximum-palindromes-after-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-palindromes-after-operations/", + "date": "2024-02-04", + "task_description": "You are given a **0-indexed** string array `words` having length `n` and containing **0-indexed** strings. You are allowed to perform the following operation **any** number of times (**including** **zero**): Choose integers `i`, `j`, `x`, and `y` such that `0 <= i, j < n`, `0 <= x < words[i].length`, `0 <= y < words[j].length`, and **swap** the characters `words[i][x]` and `words[j][y]`. Return _an integer denoting the **maximum** number of palindromes _`words`_ can contain, after performing some operations._ **Note:** `i` and `j` may be equal during an operation. **Example 1:** ``` **Input:** words = [\"abbb\",\"ba\",\"aa\"] **Output:** 3 **Explanation:** In this example, one way to get the maximum number of palindromes is: Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes [\"bbbb\",\"aa\",\"aa\"]. All strings in words are now palindromes. Hence, the maximum number of palindromes achievable is 3. ``` **Example 2:** ``` **Input:** words = [\"abc\",\"ab\"] **Output:** 2 **Explanation: **In this example, one way to get the maximum number of palindromes is: Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes [\"aac\",\"bb\"]. Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes [\"aca\",\"bb\"]. Both strings are now palindromes. Hence, the maximum number of palindromes achievable is 2. ``` **Example 3:** ``` **Input:** words = [\"cd\",\"ef\",\"a\"] **Output:** 1 **Explanation:** In this example, there is no need to perform any operation. There is one palindrome in words \"a\". It can be shown that it is not possible to get more than one palindrome after any number of operations. Hence, the answer is 1. ``` **Constraints:** `1 <= words.length <= 1000` `1 <= words[i].length <= 100` `words[i]` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "words = [\"abbb\",\"ba\",\"aa\"]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "words = [\"abc\",\"ab\"]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "words = [\"cd\",\"ef\",\"a\"]", + "output": "1 " + } + ], + "constraints": [ + "Choose integers i, j, x, and y such that 0 <= i, j < n, 0 <= x < words[i].length, 0 <= y < words[j].length, and swap the characters words[i][x] and words[j][y].", + "1 <= words.length <= 1000", + "1 <= words[i].length <= 100", + "words[i] consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def maxPalindromesAfterOperations(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxPalindromesAfterOperations(String[] words) {\n \n }\n}", + "metadata": { + "func_name": "maxPalindromesAfterOperations" + } +} \ No newline at end of file diff --git a/maximum-points-after-collecting-coins-from-all-nodes.json b/maximum-points-after-collecting-coins-from-all-nodes.json new file mode 100644 index 0000000000000000000000000000000000000000..df0346389a5b4a00ed7d5b6d85855dbf10691117 --- /dev/null +++ b/maximum-points-after-collecting-coins-from-all-nodes.json @@ -0,0 +1,35 @@ +{ + "id": 3179, + "name": "maximum-points-after-collecting-coins-from-all-nodes", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-points-after-collecting-coins-from-all-nodes/", + "date": "2023-10-22", + "task_description": "There exists an undirected tree rooted at node `0` with `n` nodes labeled from `0` to `n - 1`. 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. You are also given a **0-indexed** array `coins` of size `n` where `coins[i]` indicates the number of coins in the vertex `i`, and an integer `k`. Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected. Coins at `nodei` can be collected in one of the following ways: Collect all the coins, but you will get `coins[i] - k` points. If `coins[i] - k` is negative then you will lose `abs(coins[i] - k)` points. Collect all the coins, but you will get `floor(coins[i] / 2)` points. If this way is used, then for all the `nodej` present in the subtree of `nodei`, `coins[j]` will get reduced to `floor(coins[j] / 2)`. Return _the **maximum points** you can get after collecting the coins from **all** the tree nodes._ **Example 1:** ``` **Input:** edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5 **Output:** 11 **Explanation:** Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5. Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10. Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11. Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11. It can be shown that the maximum points we can get after collecting coins from all the nodes is 11. ``` **Example 2:** ** ** ``` **Input:** edges = [[0,1],[0,2]], coins = [8,4,4], k = 0 **Output:** 16 **Explanation:** Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16. ``` **Constraints:** `n == coins.length` `2 <= n <= 105` `0 <= coins[i] <= 104` `edges.length == n - 1` `0 <= edges[i][0], edges[i][1] < n` `0 <= k <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5", + "output": "11 " + }, + { + "label": "Example 2", + "input": "edges = [[0,1],[0,2]], coins = [8,4,4], k = 0", + "output": "16 " + } + ], + "constraints": [ + "Collect all the coins, but you will get coins[i] - k points. If coins[i] - k is negative then you will lose abs(coins[i] - k) points.", + "Collect all the coins, but you will get floor(coins[i] / 2) points. If this way is used, then for all the nodej present in the subtree of nodei, coins[j] will get reduced to floor(coins[j] / 2).", + "n == coins.length", + "2 <= n <= 105", + "0 <= coins[i] <= 104", + "edges.length == n - 1", + "0 <= edges[i][0], edges[i][1] < n", + "0 <= k <= 104" + ], + "python_template": "class Solution(object):\n def maximumPoints(self, edges, coins, k):\n \"\"\"\n :type edges: List[List[int]]\n :type coins: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumPoints(int[][] edges, int[] coins, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumPoints" + } +} \ No newline at end of file diff --git a/maximum-points-in-an-archery-competition.json b/maximum-points-in-an-archery-competition.json new file mode 100644 index 0000000000000000000000000000000000000000..3211af8208c3741eaa4e5552dec91fe014a5bd92 --- /dev/null +++ b/maximum-points-in-an-archery-competition.json @@ -0,0 +1,32 @@ +{ + "id": 2318, + "name": "maximum-points-in-an-archery-competition", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-points-in-an-archery-competition/", + "date": "2022-03-13", + "task_description": "Alice and Bob are opponents in an archery competition. The competition has set the following rules: Alice first shoots `numArrows` arrows and then Bob shoots `numArrows` arrows. The points are then calculated as follows: The target has integer scoring sections ranging from `0` to `11` **inclusive**. For **each** section of the target with score `k` (in between `0` to `11`), say Alice and Bob have shot `ak` and `bk` arrows on that section respectively. If `ak >= bk`, then Alice takes `k` points. If `ak < bk`, then Bob takes `k` points. However, if `ak == bk == 0`, then **nobody** takes `k` points. For example, if Alice and Bob both shot `2` arrows on the section with score `11`, then Alice takes `11` points. On the other hand, if Alice shot `0` arrows on the section with score `11` and Bob shot `2` arrows on that same section, then Bob takes `11` points. You are given the integer `numArrows` and an integer array `aliceArrows` of size `12`, which represents the number of arrows Alice shot on each scoring section from `0` to `11`. Now, Bob wants to **maximize** the total number of points he can obtain. Return _the array _`bobArrows`_ which represents the number of arrows Bob shot on **each** scoring section from _`0`_ to _`11`. The sum of the values in `bobArrows` should equal `numArrows`. If there are multiple ways for Bob to earn the maximum total points, return **any** one of them. **Example 1:** ``` **Input:** numArrows = 9, aliceArrows = [1,1,0,1,0,0,2,1,0,1,2,0] **Output:** [0,0,0,0,1,1,0,0,1,2,3,1] **Explanation:** The table above shows how the competition is scored. Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 = 47. It can be shown that Bob cannot obtain a score higher than 47 points. ``` **Example 2:** ``` **Input:** numArrows = 3, aliceArrows = [0,0,1,0,0,0,0,0,0,0,0,2] **Output:** [0,0,0,0,0,0,0,0,1,1,1,0] **Explanation:** The table above shows how the competition is scored. Bob earns a total point of 8 + 9 + 10 = 27. It can be shown that Bob cannot obtain a score higher than 27 points. ``` **Constraints:** `1 <= numArrows <= 105` `aliceArrows.length == bobArrows.length == 12` `0 <= aliceArrows[i], bobArrows[i] <= numArrows` `sum(aliceArrows[i]) == numArrows`", + "test_case": [ + { + "label": "Example 1", + "input": "numArrows = 9, aliceArrows = [1,1,0,1,0,0,2,1,0,1,2,0]", + "output": "[0,0,0,0,1,1,0,0,1,2,3,1] " + }, + { + "label": "Example 2", + "input": "numArrows = 3, aliceArrows = [0,0,1,0,0,0,0,0,0,0,0,2]", + "output": "[0,0,0,0,0,0,0,0,1,1,1,0] " + } + ], + "constraints": [ + "For example, if Alice and Bob both shot 2 arrows on the section with score 11, then Alice takes 11 points. On the other hand, if Alice shot 0 arrows on the section with score 11 and Bob shot 2 arrows on that same section, then Bob takes 11 points.", + "1 <= numArrows <= 105", + "aliceArrows.length == bobArrows.length == 12", + "0 <= aliceArrows[i], bobArrows[i] <= numArrows", + "sum(aliceArrows[i]) == numArrows" + ], + "python_template": "class Solution(object):\n def maximumBobPoints(self, numArrows, aliceArrows):\n \"\"\"\n :type numArrows: int\n :type aliceArrows: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {\n \n }\n}", + "metadata": { + "func_name": "maximumBobPoints" + } +} \ No newline at end of file diff --git a/maximum-points-tourist-can-earn.json b/maximum-points-tourist-can-earn.json new file mode 100644 index 0000000000000000000000000000000000000000..2422ffcbb9a75d73d0f57a767825bf925edb757c --- /dev/null +++ b/maximum-points-tourist-can-earn.json @@ -0,0 +1,36 @@ +{ + "id": 3587, + "name": "maximum-points-tourist-can-earn", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-points-tourist-can-earn/", + "date": "2024-10-12", + "task_description": "You are given two integers, `n` and `k`, along with two 2D integer arrays, `stayScore` and `travelScore`. A tourist is visiting a country with `n` cities, where each city is **directly** connected to every other city. The tourist's journey consists of **exactly** `k` **0-indexed** days, and they can choose **any** city as their starting point. Each day, the tourist has two choices: **Stay in the current city**: If the tourist stays in their current city `curr` during day `i`, they will earn `stayScore[i][curr]` points. **Move to another city**: If the tourist moves from their current city `curr` to city `dest`, they will earn `travelScore[curr][dest]` points. Return the **maximum** possible points the tourist can earn. **Example 1:** **Input:** n = 2, k = 1, stayScore = [[2,3]], travelScore = [[0,2],[1,0]] **Output:** 3 **Explanation:** The tourist earns the maximum number of points by starting in city 1 and staying in that city. **Example 2:** **Input:** n = 3, k = 2, stayScore = [[3,4,2],[2,1,2]], travelScore = [[0,2,1],[2,0,4],[3,2,0]] **Output:** 8 **Explanation:** The tourist earns the maximum number of points by starting in city 1, staying in that city on day 0, and traveling to city 2 on day 1. **Constraints:** `1 <= n <= 200` `1 <= k <= 200` `n == travelScore.length == travelScore[i].length == stayScore[i].length` `k == stayScore.length` `1 <= stayScore[i][j] <= 100` `0 <= travelScore[i][j] <= 100` `travelScore[i][i] == 0`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 2, k = 1, stayScore = [[2,3]], travelScore = [[0,2],[1,0]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "n = 3, k = 2, stayScore = [[3,4,2],[2,1,2]], travelScore = [[0,2,1],[2,0,4],[3,2,0]]", + "output": "8 " + } + ], + "constraints": [ + "Stay in the current city: If the tourist stays in their current city curr during day i, they will earn stayScore[i][curr] points.", + "Move to another city: If the tourist moves from their current city curr to city dest, they will earn travelScore[curr][dest] points.", + "1 <= n <= 200", + "1 <= k <= 200", + "n == travelScore.length == travelScore[i].length == stayScore[i].length", + "k == stayScore.length", + "1 <= stayScore[i][j] <= 100", + "0 <= travelScore[i][j] <= 100", + "travelScore[i][i] == 0" + ], + "python_template": "class Solution(object):\n def maxScore(self, n, k, stayScore, travelScore):\n \"\"\"\n :type n: int\n :type k: int\n :type stayScore: List[List[int]]\n :type travelScore: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxScore(int n, int k, int[][] stayScore, int[][] travelScore) {\n \n }\n}", + "metadata": { + "func_name": "maxScore" + } +} \ No newline at end of file diff --git a/maximum-prime-difference.json b/maximum-prime-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..3fb8341a30237d876aecff7153744f7cc1326dc2 --- /dev/null +++ b/maximum-prime-difference.json @@ -0,0 +1,30 @@ +{ + "id": 3373, + "name": "maximum-prime-difference", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-prime-difference/", + "date": "2024-04-07", + "task_description": "You are given an integer array `nums`. Return an integer that is the **maximum** distance between the **indices** of two (not necessarily different) prime numbers in `nums`_._ **Example 1:** **Input:** nums = [4,2,9,5,3] **Output:** 3 **Explanation:** `nums[1]`, `nums[3]`, and `nums[4]` are prime. So the answer is `|4 - 1| = 3`. **Example 2:** **Input:** nums = [4,8,2,8] **Output:** 0 **Explanation:** `nums[2]` is prime. Because there is just one prime number, the answer is `|2 - 2| = 0`. **Constraints:** `1 <= nums.length <= 3 * 105` `1 <= nums[i] <= 100` The input is generated such that the number of prime numbers in the `nums` is at least one.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,2,9,5,3]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [4,8,2,8]", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 3 * 105", + "1 <= nums[i] <= 100", + "The input is generated such that the number of prime numbers in the nums is at least one." + ], + "python_template": "class Solution(object):\n def maximumPrimeDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumPrimeDifference(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumPrimeDifference" + } +} \ No newline at end of file diff --git a/maximum-product-after-k-increments.json b/maximum-product-after-k-increments.json new file mode 100644 index 0000000000000000000000000000000000000000..6ee189e83725d66c487bc778b7fab585e3282b6a --- /dev/null +++ b/maximum-product-after-k-increments.json @@ -0,0 +1,29 @@ +{ + "id": 2329, + "name": "maximum-product-after-k-increments", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-product-after-k-increments/", + "date": "2022-04-03", + "task_description": "You are given an array of non-negative integers `nums` and an integer `k`. In one operation, you may choose **any** element from `nums` and **increment** it by `1`. Return_ the **maximum** **product** of _`nums`_ after **at most** _`k`_ operations. _Since the answer may be very large, return it modulo `109 + 7`. Note that you should maximize the product before taking the modulo. **Example 1:** ``` **Input:** nums = [0,4], k = 5 **Output:** 20 **Explanation:** Increment the first number 5 times. Now nums = [5, 4], with a product of 5 * 4 = 20. It can be shown that 20 is maximum product possible, so we return 20. Note that there may be other ways to increment nums to have the maximum product. ``` **Example 2:** ``` **Input:** nums = [6,3,3,2], k = 2 **Output:** 216 **Explanation:** Increment the second number 1 time and increment the fourth number 1 time. Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216. It can be shown that 216 is maximum product possible, so we return 216. Note that there may be other ways to increment nums to have the maximum product. ``` **Constraints:** `1 <= nums.length, k <= 105` `0 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,4], k = 5", + "output": "20 " + }, + { + "label": "Example 2", + "input": "nums = [6,3,3,2], k = 2", + "output": "216 " + } + ], + "constraints": [ + "1 <= nums.length, k <= 105", + "0 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maximumProduct(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 maximumProduct(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumProduct" + } +} \ No newline at end of file diff --git a/maximum-rows-covered-by-columns.json b/maximum-rows-covered-by-columns.json new file mode 100644 index 0000000000000000000000000000000000000000..5ba8b478c50590d64c23ab5a26ffa1ea18f7d09d --- /dev/null +++ b/maximum-rows-covered-by-columns.json @@ -0,0 +1,34 @@ +{ + "id": 2482, + "name": "maximum-rows-covered-by-columns", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-rows-covered-by-columns/", + "date": "2022-08-20", + "task_description": "You are given an `m x n` binary matrix `matrix` and an integer `numSelect`. Your goal is to select exactly `numSelect` **distinct **columns from `matrix` such that you cover as many rows as possible. A row is considered **covered** if all the `1`'s in that row are also part of a column that you have selected. If a row does not have any `1`s, it is also considered covered. More formally, let us consider `selected = {c1, c2, ...., cnumSelect}` as the set of columns selected by you. A row `i` is **covered** by `selected` if: For each cell where `matrix[i][j] == 1`, the column `j` is in `selected`. Or, no cell in row `i` has a value of `1`. Return the **maximum** number of rows that can be **covered** by a set of `numSelect` columns. **Example 1:** **Input:** matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2 **Output:** 3 **Explanation:** One possible way to cover 3 rows is shown in the diagram above. We choose s = {0, 2}. - Row 0 is covered because it has no occurrences of 1. - Row 1 is covered because the columns with value 1, i.e. 0 and 2 are present in s. - Row 2 is not covered because matrix[2][1] == 1 but 1 is not present in s. - Row 3 is covered because matrix[2][2] == 1 and 2 is present in s. Thus, we can cover three rows. Note that s = {1, 2} will also cover 3 rows, but it can be shown that no more than three rows can be covered. **Example 2:** **Input:** matrix = [[1],[0]], numSelect = 1 **Output:** 2 **Explanation:** Selecting the only column will result in both rows being covered since the entire matrix is selected. **Constraints:** `m == matrix.length` `n == matrix[i].length` `1 <= m, n <= 12` `matrix[i][j]` is either `0` or `1`. `1 <= numSelect <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "matrix = [[1],[0]], numSelect = 1", + "output": "2 " + } + ], + "constraints": [ + "For each cell where matrix[i][j] == 1, the column j is in selected.", + "Or, no cell in row i has a value of 1.", + "m == matrix.length", + "n == matrix[i].length", + "1 <= m, n <= 12", + "matrix[i][j] is either 0 or 1.", + "1 <= numSelect <= n" + ], + "python_template": "class Solution(object):\n def maximumRows(self, matrix, numSelect):\n \"\"\"\n :type matrix: List[List[int]]\n :type numSelect: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumRows(int[][] matrix, int numSelect) {\n \n }\n}", + "metadata": { + "func_name": "maximumRows" + } +} \ No newline at end of file diff --git a/maximum-running-time-of-n-computers.json b/maximum-running-time-of-n-computers.json new file mode 100644 index 0000000000000000000000000000000000000000..e28a0bf1ee7d7ff5039b9c62e1e4a9f8fbe4b371 --- /dev/null +++ b/maximum-running-time-of-n-computers.json @@ -0,0 +1,29 @@ +{ + "id": 2263, + "name": "maximum-running-time-of-n-computers", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-running-time-of-n-computers/", + "date": "2022-01-09", + "task_description": "You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries. Initially, you can insert **at most one battery** into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery **any number of times**. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time. Note that the batteries cannot be recharged. Return _the **maximum** number of minutes you can run all the _`n`_ computers simultaneously._ **Example 1:** ``` **Input:** n = 2, batteries = [3,3,3] **Output:** 4 **Explanation:** Initially, insert battery 0 into the first computer and battery 1 into the second computer. After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute. At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead. By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running. We can run the two computers simultaneously for at most 4 minutes, so we return 4. ``` **Example 2:** ``` **Input:** n = 2, batteries = [1,1,1,1] **Output:** 2 **Explanation:** Initially, insert battery 0 into the first computer and battery 2 into the second computer. After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer. After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running. We can run the two computers simultaneously for at most 2 minutes, so we return 2. ``` **Constraints:** `1 <= n <= batteries.length <= 105` `1 <= batteries[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 2, batteries = [3,3,3]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "n = 2, batteries = [1,1,1,1]", + "output": "2 " + } + ], + "constraints": [ + "1 <= n <= batteries.length <= 105", + "1 <= batteries[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxRunTime(self, n, batteries):\n \"\"\"\n :type n: int\n :type batteries: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxRunTime(int n, int[] batteries) {\n \n }\n}", + "metadata": { + "func_name": "maxRunTime" + } +} \ No newline at end of file diff --git a/maximum-score-after-applying-operations-on-a-tree.json b/maximum-score-after-applying-operations-on-a-tree.json new file mode 100644 index 0000000000000000000000000000000000000000..8463490647ec82351be07591b089cd16b9ac5f03 --- /dev/null +++ b/maximum-score-after-applying-operations-on-a-tree.json @@ -0,0 +1,37 @@ +{ + "id": 3191, + "name": "maximum-score-after-applying-operations-on-a-tree", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-score-after-applying-operations-on-a-tree/", + "date": "2023-10-29", + "task_description": "There is an undirected tree with `n` nodes labeled from `0` to `n - 1`, and 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. You are also given a **0-indexed** integer array `values` of length `n`, where `values[i]` is the **value** associated with the `ith` node. You start with a score of `0`. In one operation, you can: Pick any node `i`. Add `values[i]` to your score. Set `values[i]` to `0`. A tree is **healthy** if the sum of values on the path from the root to any leaf node is different than zero. Return _the **maximum score** you can obtain after performing these operations on the tree any number of times so that it remains **healthy**._ **Example 1:** ``` **Input:** edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1] **Output:** 11 **Explanation:** We can choose nodes 1, 2, 3, 4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1] + values[2] + values[3] + values[4] + values[5] = 11. It can be shown that 11 is the maximum score obtainable after any number of operations on the tree. ``` **Example 2:** ``` **Input:** edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5] **Output:** 40 **Explanation:** We can choose nodes 0, 2, 3, and 4. - The sum of values on the path from 0 to 4 is equal to 10. - The sum of values on the path from 0 to 3 is equal to 10. - The sum of values on the path from 0 to 5 is equal to 3. - The sum of values on the path from 0 to 6 is equal to 5. Therefore, the tree is healthy and the score is values[0] + values[2] + values[3] + values[4] = 40. It can be shown that 40 is the maximum score obtainable after any number of operations on the tree. ``` **Constraints:** `2 <= n <= 2 * 104` `edges.length == n - 1` `edges[i].length == 2` `0 <= ai, bi < n` `values.length == n` `1 <= values[i] <= 109` The input is generated such that `edges` represents a valid tree.", + "test_case": [ + { + "label": "Example 1", + "input": "edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]", + "output": "11 " + }, + { + "label": "Example 2", + "input": "edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]", + "output": "40 " + } + ], + "constraints": [ + "Pick any node i.", + "Add values[i] to your score.", + "Set values[i] to 0.", + "2 <= n <= 2 * 104", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ai, bi < n", + "values.length == n", + "1 <= values[i] <= 109", + "The input is generated such that edges represents a valid tree." + ], + "python_template": "class Solution(object):\n def maximumScoreAfterOperations(self, edges, values):\n \"\"\"\n :type edges: List[List[int]]\n :type values: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumScoreAfterOperations(int[][] edges, int[] values) {\n \n }\n}", + "metadata": { + "func_name": "maximumScoreAfterOperations" + } +} \ No newline at end of file diff --git a/maximum-score-from-grid-operations.json b/maximum-score-from-grid-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..7e4ca353255b76c828fac7b67ee17fcda49ff2ba --- /dev/null +++ b/maximum-score-from-grid-operations.json @@ -0,0 +1,30 @@ +{ + "id": 3470, + "name": "maximum-score-from-grid-operations", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-score-from-grid-operations/", + "date": "2024-07-06", + "task_description": "You are given a 2D matrix `grid` of size `n x n`. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices `(i, j)`, and color black all the cells of the `jth` column starting from the top row down to the `ith` row. The grid score is the sum of all `grid[i][j]` such that cell `(i, j)` is white and it has a horizontally adjacent black cell. Return the **maximum** score that can be achieved after some number of operations. **Example 1:** **Input:** grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]] **Output:** 11 **Explanation:** In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is `grid[3][0] + grid[1][2] + grid[3][3]` which is equal to 11. **Example 2:** **Input:** grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]] **Output:** 94 **Explanation:** We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is `grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4]` which is equal to 94. **Constraints:** `1 <= n == grid.length <= 100` `n == grid[i].length` `0 <= grid[i][j] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]", + "output": "11 " + }, + { + "label": "Example 2", + "input": "grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]", + "output": "94 " + } + ], + "constraints": [ + "1 <= n == grid.length <= 100", + "n == grid[i].length", + "0 <= grid[i][j] <= 109" + ], + "python_template": "class Solution(object):\n def maximumScore(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumScore(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "maximumScore" + } +} \ No newline at end of file diff --git a/maximum-score-of-a-node-sequence.json b/maximum-score-of-a-node-sequence.json new file mode 100644 index 0000000000000000000000000000000000000000..9e85f191a88b180408366a53b0b357006c361ede --- /dev/null +++ b/maximum-score-of-a-node-sequence.json @@ -0,0 +1,37 @@ +{ + "id": 2353, + "name": "maximum-score-of-a-node-sequence", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-score-of-a-node-sequence/", + "date": "2022-04-02", + "task_description": "There is an **undirected** graph with `n` nodes, numbered from `0` to `n - 1`. You are given a **0-indexed** integer array `scores` of length `n` where `scores[i]` denotes the score of node `i`. 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 node sequence is valid if it meets the following conditions: There is an edge connecting every pair of **adjacent** nodes in the sequence. No node appears more than once in the sequence. The score of a node sequence is defined as the **sum** of the scores of the nodes in the sequence. Return _the **maximum score** of a valid node sequence with a length of _`4`_. _If no such sequence exists, return_ _`-1`. **Example 1:** ``` **Input:** scores = [5,2,9,8,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]] **Output:** 24 **Explanation:** The figure above shows the graph and the chosen node sequence [0,1,2,3]. The score of the node sequence is 5 + 2 + 9 + 8 = 24. It can be shown that no other node sequence has a score of more than 24. Note that the sequences [3,1,2,0] and [1,0,2,3] are also valid and have a score of 24. The sequence [0,3,2,4] is not valid since no edge connects nodes 0 and 3. ``` **Example 2:** ``` **Input:** scores = [9,20,6,4,11,12], edges = [[0,3],[5,3],[2,4],[1,3]] **Output:** -1 **Explanation:** The figure above shows the graph. There are no valid node sequences of length 4, so we return -1. ``` **Constraints:** `n == scores.length` `4 <= n <= 5 * 104` `1 <= scores[i] <= 108` `0 <= edges.length <= 5 * 104` `edges[i].length == 2` `0 <= ai, bi <= n - 1` `ai != bi` There are no duplicate edges.", + "test_case": [ + { + "label": "Example 1", + "input": "scores = [5,2,9,8,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]", + "output": "24 " + }, + { + "label": "Example 2", + "input": "scores = [9,20,6,4,11,12], edges = [[0,3],[5,3],[2,4],[1,3]]", + "output": "-1 " + } + ], + "constraints": [ + "There is an edge connecting every pair of adjacent nodes in the sequence.", + "No node appears more than once in the sequence.", + "n == scores.length", + "4 <= n <= 5 * 104", + "1 <= scores[i] <= 108", + "0 <= edges.length <= 5 * 104", + "edges[i].length == 2", + "0 <= ai, bi <= n - 1", + "ai != bi", + "There are no duplicate edges." + ], + "python_template": "class Solution(object):\n def maximumScore(self, scores, edges):\n \"\"\"\n :type scores: List[int]\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumScore(int[] scores, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "maximumScore" + } +} \ No newline at end of file diff --git a/maximum-segment-sum-after-removals.json b/maximum-segment-sum-after-removals.json new file mode 100644 index 0000000000000000000000000000000000000000..706db14f738ca38739026907854fba57e2c4a23e --- /dev/null +++ b/maximum-segment-sum-after-removals.json @@ -0,0 +1,32 @@ +{ + "id": 2466, + "name": "maximum-segment-sum-after-removals", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-segment-sum-after-removals/", + "date": "2022-08-06", + "task_description": "You are given two **0-indexed** integer arrays `nums` and `removeQueries`, both of length `n`. For the `ith` query, the element in `nums` at the index `removeQueries[i]` is removed, splitting `nums` into different segments. A **segment** is a contiguous sequence of **positive** integers in `nums`. A **segment sum** is the sum of every element in a segment. Return_ an integer array _`answer`_, of length _`n`_, where _`answer[i]`_ is the **maximum** segment sum after applying the _`ith` _removal._ **Note:** The same index will **not** be removed more than once. **Example 1:** ``` **Input:** nums = [1,2,5,6,1], removeQueries = [0,3,2,4,1] **Output:** [14,7,2,2,0] **Explanation:** Using 0 to indicate a removed element, the answer is as follows: Query 1: Remove the 0th element, nums becomes [0,2,5,6,1] and the maximum segment sum is 14 for segment [2,5,6,1]. Query 2: Remove the 3rd element, nums becomes [0,2,5,0,1] and the maximum segment sum is 7 for segment [2,5]. Query 3: Remove the 2nd element, nums becomes [0,2,0,0,1] and the maximum segment sum is 2 for segment [2]. Query 4: Remove the 4th element, nums becomes [0,2,0,0,0] and the maximum segment sum is 2 for segment [2]. Query 5: Remove the 1st element, nums becomes [0,0,0,0,0] and the maximum segment sum is 0, since there are no segments. Finally, we return [14,7,2,2,0]. ``` **Example 2:** ``` **Input:** nums = [3,2,11,1], removeQueries = [3,2,1,0] **Output:** [16,5,3,0] **Explanation:** Using 0 to indicate a removed element, the answer is as follows: Query 1: Remove the 3rd element, nums becomes [3,2,11,0] and the maximum segment sum is 16 for segment [3,2,11]. Query 2: Remove the 2nd element, nums becomes [3,2,0,0] and the maximum segment sum is 5 for segment [3,2]. Query 3: Remove the 1st element, nums becomes [3,0,0,0] and the maximum segment sum is 3 for segment [3]. Query 4: Remove the 0th element, nums becomes [0,0,0,0] and the maximum segment sum is 0, since there are no segments. Finally, we return [16,5,3,0]. ``` **Constraints:** `n == nums.length == removeQueries.length` `1 <= n <= 105` `1 <= nums[i] <= 109` `0 <= removeQueries[i] < n` All the values of `removeQueries` are **unique**.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,5,6,1], removeQueries = [0,3,2,4,1]", + "output": "[14,7,2,2,0] " + }, + { + "label": "Example 2", + "input": "nums = [3,2,11,1], removeQueries = [3,2,1,0]", + "output": "[16,5,3,0] " + } + ], + "constraints": [ + "n == nums.length == removeQueries.length", + "1 <= n <= 105", + "1 <= nums[i] <= 109", + "0 <= removeQueries[i] < n", + "All the values of removeQueries are unique." + ], + "python_template": "class Solution(object):\n def maximumSegmentSum(self, nums, removeQueries):\n \"\"\"\n :type nums: List[int]\n :type removeQueries: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public long[] maximumSegmentSum(int[] nums, int[] removeQueries) {\n \n }\n}", + "metadata": { + "func_name": "maximumSegmentSum" + } +} \ No newline at end of file diff --git a/maximum-size-of-a-set-after-removals.json b/maximum-size-of-a-set-after-removals.json new file mode 100644 index 0000000000000000000000000000000000000000..f1bf7ccda0fa9345fe6a17ee361b8cf60ce2ea5d --- /dev/null +++ b/maximum-size-of-a-set-after-removals.json @@ -0,0 +1,36 @@ +{ + "id": 3228, + "name": "maximum-size-of-a-set-after-removals", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-size-of-a-set-after-removals/", + "date": "2023-12-31", + "task_description": "You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`. You must remove `n / 2` elements from `nums1` and `n / 2` elements from `nums2`. After the removals, you insert the remaining elements of `nums1` and `nums2` into a set `s`. Return _the **maximum** possible size of the set_ `s`. **Example 1:** ``` **Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1] **Output:** 2 **Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}. It can be shown that 2 is the maximum possible size of the set s after the removals. ``` **Example 2:** ``` **Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3] **Output:** 5 **Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}. It can be shown that 5 is the maximum possible size of the set s after the removals. ``` **Example 3:** ``` **Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6] **Output:** 6 **Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}. It can be shown that 6 is the maximum possible size of the set s after the removals. ``` **Constraints:** `n == nums1.length == nums2.length` `1 <= n <= 2 * 104` `n` is even. `1 <= nums1[i], nums2[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,2,1,2], nums2 = [1,1,1,1]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]", + "output": "5 " + }, + { + "label": "Example 3", + "input": "nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]", + "output": "6 " + } + ], + "constraints": [ + "n == nums1.length == nums2.length", + "1 <= n <= 2 * 104", + "n is even.", + "1 <= nums1[i], nums2[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumSetSize(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 maximumSetSize(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "maximumSetSize" + } +} \ No newline at end of file diff --git a/maximum-spending-after-buying-items.json b/maximum-spending-after-buying-items.json new file mode 100644 index 0000000000000000000000000000000000000000..11f188ae0779f6a5c048ab52d76febd1321ae430 --- /dev/null +++ b/maximum-spending-after-buying-items.json @@ -0,0 +1,33 @@ +{ + "id": 3107, + "name": "maximum-spending-after-buying-items", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-spending-after-buying-items/", + "date": "2023-10-28", + "task_description": "You are given a **0-indexed** `m * n` integer matrix `values`, representing the values of `m * n` different items in `m` different shops. Each shop has `n` items where the `jth` item in the `ith` shop has a value of `values[i][j]`. Additionally, the items in the `ith` shop are sorted in non-increasing order of value. That is, `values[i][j] >= values[i][j + 1]` for all `0 <= j < n - 1`. On each day, you would like to buy a single item from one of the shops. Specifically, On the `dth` day you can: Pick any shop `i`. Buy the rightmost available item `j` for the price of `values[i][j] * d`. That is, find the greatest index `j` such that item `j` was never bought before, and buy it for the price of `values[i][j] * d`. **Note** that all items are pairwise different. For example, if you have bought item `0` from shop `1`, you can still buy item `0` from any other shop. Return _the **maximum amount of money that can be spent** on buying all _ `m * n` _products_. **Example 1:** ``` **Input:** values = [[8,5,2],[6,4,1],[9,7,3]] **Output:** 285 **Explanation:** On the first day, we buy product 2 from shop 1 for a price of values[1][2] * 1 = 1. On the second day, we buy product 2 from shop 0 for a price of values[0][2] * 2 = 4. On the third day, we buy product 2 from shop 2 for a price of values[2][2] * 3 = 9. On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] * 4 = 16. On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] * 5 = 25. On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] * 6 = 36. On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] * 7 = 49. On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] * 8 = 64. On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] * 9 = 81. Hence, our total spending is equal to 285. It can be shown that 285 is the maximum amount of money that can be spent buying all m * n products. ``` **Example 2:** ``` **Input:** values = [[10,8,6,4,2],[9,7,5,3,2]] **Output:** 386 **Explanation:** On the first day, we buy product 4 from shop 0 for a price of values[0][4] * 1 = 2. On the second day, we buy product 4 from shop 1 for a price of values[1][4] * 2 = 4. On the third day, we buy product 3 from shop 1 for a price of values[1][3] * 3 = 9. On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] * 4 = 16. On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] * 5 = 25. On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] * 6 = 36. On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] * 7 = 49. On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] * 8 = 64 On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] * 9 = 81. On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] * 10 = 100. Hence, our total spending is equal to 386. It can be shown that 386 is the maximum amount of money that can be spent buying all m * n products. ``` **Constraints:** `1 <= m == values.length <= 10` `1 <= n == values[i].length <= 104` `1 <= values[i][j] <= 106` `values[i]` are sorted in non-increasing order.", + "test_case": [ + { + "label": "Example 1", + "input": "values = [[8,5,2],[6,4,1],[9,7,3]]", + "output": "285 " + }, + { + "label": "Example 2", + "input": "values = [[10,8,6,4,2],[9,7,5,3,2]]", + "output": "386 " + } + ], + "constraints": [ + "Pick any shop i.", + "Buy the rightmost available item j for the price of values[i][j] * d. That is, find the greatest index j such that item j was never bought before, and buy it for the price of values[i][j] * d.", + "1 <= m == values.length <= 10", + "1 <= n == values[i].length <= 104", + "1 <= values[i][j] <= 106", + "values[i] are sorted in non-increasing order." + ], + "python_template": "class Solution(object):\n def maxSpending(self, values):\n \"\"\"\n :type values: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxSpending(int[][] values) {\n \n }\n}", + "metadata": { + "func_name": "maxSpending" + } +} \ No newline at end of file diff --git a/maximum-square-area-by-removing-fences-from-a-field.json b/maximum-square-area-by-removing-fences-from-a-field.json new file mode 100644 index 0000000000000000000000000000000000000000..9f896e167876d8af2446546731c7e9456e53c58c --- /dev/null +++ b/maximum-square-area-by-removing-fences-from-a-field.json @@ -0,0 +1,32 @@ +{ + "id": 3250, + "name": "maximum-square-area-by-removing-fences-from-a-field", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-square-area-by-removing-fences-from-a-field/", + "date": "2023-12-17", + "task_description": "There is a large `(m - 1) x (n - 1)` rectangular field with corners at `(1, 1)` and `(m, n)` containing some horizontal and vertical fences given in arrays `hFences` and `vFences` respectively. Horizontal fences are from the coordinates `(hFences[i], 1)` to `(hFences[i], n)` and vertical fences are from the coordinates `(1, vFences[i])` to `(m, vFences[i])`. Return _the **maximum** area of a **square** field that can be formed by **removing** some fences (**possibly none**) or _`-1` _if it is impossible to make a square field_. Since the answer may be large, return it **modulo** `109 + 7`. **Note: **The field is surrounded by two horizontal fences from the coordinates `(1, 1)` to `(1, n)` and `(m, 1)` to `(m, n)` and two vertical fences from the coordinates `(1, 1)` to `(m, 1)` and `(1, n)` to `(m, n)`. These fences **cannot** be removed. **Example 1:** ``` **Input:** m = 4, n = 3, hFences = [2,3], vFences = [2] **Output:** 4 **Explanation:** Removing the horizontal fence at 2 and the vertical fence at 2 will give a square field of area 4. ``` **Example 2:** ``` **Input:** m = 6, n = 7, hFences = [2], vFences = [4] **Output:** -1 **Explanation:** It can be proved that there is no way to create a square field by removing fences. ``` **Constraints:** `3 <= m, n <= 109` `1 <= hFences.length, vFences.length <= 600` `1 < hFences[i] < m` `1 < vFences[i] < n` `hFences` and `vFences` are unique.", + "test_case": [ + { + "label": "Example 1", + "input": "m = 4, n = 3, hFences = [2,3], vFences = [2]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "m = 6, n = 7, hFences = [2], vFences = [4]", + "output": "-1 " + } + ], + "constraints": [ + "3 <= m, n <= 109", + "1 <= hFences.length, vFences.length <= 600", + "1 < hFences[i] < m", + "1 < vFences[i] < n", + "hFences and vFences are unique." + ], + "python_template": "class Solution(object):\n def maximizeSquareArea(self, m, n, hFences, vFences):\n \"\"\"\n :type m: int\n :type n: int\n :type hFences: List[int]\n :type vFences: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) {\n \n }\n}", + "metadata": { + "func_name": "maximizeSquareArea" + } +} \ No newline at end of file diff --git a/maximum-strength-of-a-group.json b/maximum-strength-of-a-group.json new file mode 100644 index 0000000000000000000000000000000000000000..c193bccb1adb7d62d913ec46380504fc8296b4fd --- /dev/null +++ b/maximum-strength-of-a-group.json @@ -0,0 +1,29 @@ +{ + "id": 2754, + "name": "maximum-strength-of-a-group", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-strength-of-a-group/", + "date": "2023-05-13", + "task_description": "You are given a **0-indexed** integer array `nums` representing the score of students in an exam. The teacher would like to form one **non-empty** group of students with maximal **strength**, where the strength of a group of students of indices `i0`, `i1`, `i2`, ... , `ik` is defined as `nums[i0] * nums[i1] * nums[i2] * ... * nums[ik​]`. Return _the maximum strength of a group the teacher can create_. **Example 1:** ``` **Input:** nums = [3,-1,-5,2,5,-9] **Output:** 1350 **Explanation:** One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal. ``` **Example 2:** ``` **Input:** nums = [-4,-5,-4] **Output:** 20 **Explanation:** Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength. ``` **Constraints:** `1 <= nums.length <= 13` `-9 <= nums[i] <= 9`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,-1,-5,2,5,-9]", + "output": "1350 " + }, + { + "label": "Example 2", + "input": "nums = [-4,-5,-4]", + "output": "20 " + } + ], + "constraints": [ + "1 <= nums.length <= 13", + "-9 <= nums[i] <= 9" + ], + "python_template": "class Solution(object):\n def maxStrength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxStrength(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxStrength" + } +} \ No newline at end of file diff --git a/maximum-strength-of-k-disjoint-subarrays.json b/maximum-strength-of-k-disjoint-subarrays.json new file mode 100644 index 0000000000000000000000000000000000000000..afe5b4615ec23803cb9f7b76aa05bb9b3d932d25 --- /dev/null +++ b/maximum-strength-of-k-disjoint-subarrays.json @@ -0,0 +1,37 @@ +{ + "id": 3313, + "name": "maximum-strength-of-k-disjoint-subarrays", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-strength-of-k-disjoint-subarrays/", + "date": "2024-03-03", + "task_description": "You are given an array of integers `nums` with length `n`, and a positive **odd** integer `k`. Select exactly `k` disjoint subarrays `sub1, sub2, ..., subk` from `nums` such that the last element of `subi` appears before the first element of `sub{i+1}` for all `1 <= i <= k-1`. The goal is to maximize their combined strength. The strength of the selected subarrays is defined as: `strength = k * sum(sub1)- (k - 1) * sum(sub2) + (k - 2) * sum(sub3) - ... - 2 * sum(sub{k-1}) + sum(subk)` where `sum(subi)` is the sum of the elements in the `i`-th subarray. Return the **maximum** possible strength that can be obtained from selecting exactly `k` disjoint subarrays from `nums`. **Note** that the chosen subarrays **don't** need to cover the entire array. **Example 1:** **Input:** nums = [1,2,3,-1,2], k = 3 **Output:** 22 **Explanation:** The best possible way to select 3 subarrays is: nums[0..2], nums[3..3], and nums[4..4]. The strength is calculated as follows: `strength = 3 * (1 + 2 + 3) - 2 * (-1) + 2 = 22` **Example 2:** **Input:** nums = [12,-2,-2,-2,-2], k = 5 **Output:** 64 **Explanation:** The only possible way to select 5 disjoint subarrays is: nums[0..0], nums[1..1], nums[2..2], nums[3..3], and nums[4..4]. The strength is calculated as follows: `strength = 5 * 12 - 4 * (-2) + 3 * (-2) - 2 * (-2) + (-2) = 64` **Example 3:** **Input:** nums = [-1,-2,-3], k = 1 **Output:** -1 **Explanation:** The best possible way to select 1 subarray is: nums[0..0]. The strength is -1. **Constraints:** `1 <= n <= 104` `-109 <= nums[i] <= 109` `1 <= k <= n` `1 <= n * k <= 106` `k` is odd.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,-1,2], k = 3", + "output": "22 " + }, + { + "label": "Example 2", + "input": "nums = [12,-2,-2,-2,-2], k = 5", + "output": "64 " + }, + { + "label": "Example 3", + "input": "nums = [-1,-2,-3], k = 1", + "output": "-1 " + } + ], + "constraints": [ + "1 <= n <= 104", + "-109 <= nums[i] <= 109", + "1 <= k <= n", + "1 <= n * k <= 106", + "k is odd." + ], + "python_template": "class Solution(object):\n def maximumStrength(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 maximumStrength(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumStrength" + } +} \ No newline at end of file diff --git a/maximum-strictly-increasing-cells-in-a-matrix.json b/maximum-strictly-increasing-cells-in-a-matrix.json new file mode 100644 index 0000000000000000000000000000000000000000..8c96df0e5d657151ecdae32b2df14e6ce8d87d1c --- /dev/null +++ b/maximum-strictly-increasing-cells-in-a-matrix.json @@ -0,0 +1,37 @@ +{ + "id": 2818, + "name": "maximum-strictly-increasing-cells-in-a-matrix", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-strictly-increasing-cells-in-a-matrix/", + "date": "2023-05-21", + "task_description": "Given a **1-indexed** `m x n` integer matrix `mat`, you can select any cell in the matrix as your **starting cell**. From the starting cell, you can move to any other cell **in the** **same row or column**, but only if the value of the destination cell is **strictly greater** than the value of the current cell. You can repeat this process as many times as possible, moving from cell to cell until you can no longer make any moves. Your task is to find the **maximum number of cells** that you can visit in the matrix by starting from some cell. Return _an integer denoting the maximum number of cells that can be visited._ **Example 1:** **** ``` **Input:** mat = [[3,1],[3,4]] **Output:** 2 **Explanation:** The image shows how we can visit 2 cells starting from row 1, column 2. It can be shown that we cannot visit more than 2 cells no matter where we start from, so the answer is 2. ``` **Example 2:** **** ``` **Input:** mat = [[1,1],[1,1]] **Output:** 1 **Explanation:** Since the cells must be strictly increasing, we can only visit one cell in this example. ``` **Example 3:** **** ``` **Input:** mat = [[3,1,6],[-9,5,7]] **Output:** 4 **Explanation:** The image above shows how we can visit 4 cells starting from row 2, column 1. It can be shown that we cannot visit more than 4 cells no matter where we start from, so the answer is 4. ``` **Constraints:** `m == mat.length ` `n == mat[i].length ` `1 <= m, n <= 105` `1 <= m * n <= 105` `-105 <= mat[i][j] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "mat = [[3,1],[3,4]]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "mat = [[1,1],[1,1]]", + "output": "1 " + }, + { + "label": "Example 3", + "input": "mat = [[3,1,6],[-9,5,7]]", + "output": "4 " + } + ], + "constraints": [ + "m == mat.length", + "n == mat[i].length", + "1 <= m, n <= 105", + "1 <= m * n <= 105", + "-105 <= mat[i][j] <= 105" + ], + "python_template": "class Solution(object):\n def maxIncreasingCells(self, mat):\n \"\"\"\n :type mat: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxIncreasingCells(int[][] mat) {\n \n }\n}", + "metadata": { + "func_name": "maxIncreasingCells" + } +} \ No newline at end of file diff --git a/maximum-strong-pair-xor-i.json b/maximum-strong-pair-xor-i.json new file mode 100644 index 0000000000000000000000000000000000000000..b513e47d24ed19d1936a3ebe125db3fafb247e86 --- /dev/null +++ b/maximum-strong-pair-xor-i.json @@ -0,0 +1,35 @@ +{ + "id": 3193, + "name": "maximum-strong-pair-xor-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-strong-pair-xor-i/", + "date": "2023-11-05", + "task_description": "You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition: `|x - y| <= min(x, y)` You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array. Return _the **maximum** _`XOR`_ value out of all possible strong pairs in the array_ `nums`. **Note** that you can pick the same integer twice to form a pair. **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** 7 **Explanation:** There are 11 strong pairs in the array `nums`: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5). The maximum XOR possible from these pairs is 3 XOR 4 = 7. ``` **Example 2:** ``` **Input:** nums = [10,100] **Output:** 0 **Explanation:** There are 2 strong pairs in the array `nums`: (10, 10) and (100, 100). The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0. ``` **Example 3:** ``` **Input:** nums = [5,6,25,30] **Output:** 7 **Explanation:** There are 6 strong pairs in the array `nums`: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30). The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3. ``` **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [10,100]", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [5,6,25,30]", + "output": "7 " + } + ], + "constraints": [ + "|x - y| <= min(x, y)", + "1 <= nums.length <= 50", + "1 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def maximumStrongPairXor(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumStrongPairXor(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumStrongPairXor" + } +} \ No newline at end of file diff --git a/maximum-strong-pair-xor-ii.json b/maximum-strong-pair-xor-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..720198b3fa22bf09b8a67ee326dd2296eb5a0934 --- /dev/null +++ b/maximum-strong-pair-xor-ii.json @@ -0,0 +1,35 @@ +{ + "id": 3197, + "name": "maximum-strong-pair-xor-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-strong-pair-xor-ii/", + "date": "2023-11-05", + "task_description": "You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition: `|x - y| <= min(x, y)` You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array. Return _the **maximum** _`XOR`_ value out of all possible strong pairs in the array_ `nums`. **Note** that you can pick the same integer twice to form a pair. **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** 7 **Explanation:** There are 11 strong pairs in the array `nums`: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5). The maximum XOR possible from these pairs is 3 XOR 4 = 7. ``` **Example 2:** ``` **Input:** nums = [10,100] **Output:** 0 **Explanation:** There are 2 strong pairs in the array nums: (10, 10) and (100, 100). The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0. ``` **Example 3:** ``` **Input:** nums = [500,520,2500,3000] **Output:** 1020 **Explanation:** There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000). The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636. ``` **Constraints:** `1 <= nums.length <= 5 * 104` `1 <= nums[i] <= 220 - 1`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [10,100]", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [500,520,2500,3000]", + "output": "1020 " + } + ], + "constraints": [ + "|x - y| <= min(x, y)", + "1 <= nums.length <= 5 * 104", + "1 <= nums[i] <= 220 - 1" + ], + "python_template": "class Solution(object):\n def maximumStrongPairXor(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumStrongPairXor(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumStrongPairXor" + } +} \ No newline at end of file diff --git a/maximum-subarray-sum-with-length-divisible-by-k.json b/maximum-subarray-sum-with-length-divisible-by-k.json new file mode 100644 index 0000000000000000000000000000000000000000..1a2903961c7948e0f9a560d12f9c9ca31328acc6 --- /dev/null +++ b/maximum-subarray-sum-with-length-divisible-by-k.json @@ -0,0 +1,34 @@ +{ + "id": 3653, + "name": "maximum-subarray-sum-with-length-divisible-by-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-subarray-sum-with-length-divisible-by-k/", + "date": "2024-12-01", + "task_description": "You are given an array of integers `nums` and an integer `k`. Return the **maximum** sum of a subarray of `nums`, such that the size of the subarray is **divisible** by `k`. **Example 1:** **Input:** nums = [1,2], k = 1 **Output:** 3 **Explanation:** The subarray `[1, 2]` with sum 3 has length equal to 2 which is divisible by 1. **Example 2:** **Input:** nums = [-1,-2,-3,-4,-5], k = 4 **Output:** -10 **Explanation:** The maximum sum subarray is `[-1, -2, -3, -4]` which has length equal to 4 which is divisible by 4. **Example 3:** **Input:** nums = [-5,1,2,-3,4], k = 2 **Output:** 4 **Explanation:** The maximum sum subarray is `[1, 2, -3, 4]` which has length equal to 4 which is divisible by 2. **Constraints:** `1 <= k <= nums.length <= 2 * 105` `-109 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2], k = 1", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [-1,-2,-3,-4,-5], k = 4", + "output": "-10 " + }, + { + "label": "Example 3", + "input": "nums = [-5,1,2,-3,4], k = 2", + "output": "4 " + } + ], + "constraints": [ + "1 <= k <= nums.length <= 2 * 105", + "-109 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxSubarraySum(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 maxSubarraySum(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxSubarraySum" + } +} \ No newline at end of file diff --git a/maximum-subarray-with-equal-products.json b/maximum-subarray-with-equal-products.json new file mode 100644 index 0000000000000000000000000000000000000000..6f90eea6df09b83aa9c2cb94bf25d6296b8dbf75 --- /dev/null +++ b/maximum-subarray-with-equal-products.json @@ -0,0 +1,37 @@ +{ + "id": 3702, + "name": "maximum-subarray-with-equal-products", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-subarray-with-equal-products/", + "date": "2024-12-29", + "task_description": "You are given an array of **positive** integers `nums`. An array `arr` is called **product equivalent** if `prod(arr) == lcm(arr) * gcd(arr)`, where: `prod(arr)` is the product of all elements of `arr`. `gcd(arr)` is the GCD of all elements of `arr`. `lcm(arr)` is the LCM of all elements of `arr`. Return the length of the **longest** **product equivalent** subarray of `nums`. **Example 1:** **Input:** nums = [1,2,1,2,1,1,1] **Output:** 5 **Explanation:** The longest product equivalent subarray is `[1, 2, 1, 1, 1]`, where `prod([1, 2, 1, 1, 1]) = 2`, `gcd([1, 2, 1, 1, 1]) = 1`, and `lcm([1, 2, 1, 1, 1]) = 2`. **Example 2:** **Input:** nums = [2,3,4,5,6] **Output:** 3 **Explanation:** The longest product equivalent subarray is `[3, 4, 5].` **Example 3:** **Input:** nums = [1,2,3,1,4,5,1] **Output:** 5 **Constraints:** `2 <= nums.length <= 100` `1 <= nums[i] <= 10`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1,2,1,1,1]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [2,3,4,5,6]", + "output": "3 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3,1,4,5,1]", + "output": "" + } + ], + "constraints": [ + "prod(arr) is the product of all elements of arr.", + "gcd(arr) is the GCD of all elements of arr.", + "lcm(arr) is the LCM of all elements of arr.", + "2 <= nums.length <= 100", + "1 <= nums[i] <= 10" + ], + "python_template": "class Solution(object):\n def maxLength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxLength(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxLength" + } +} \ No newline at end of file diff --git a/maximum-subsequence-score.json b/maximum-subsequence-score.json new file mode 100644 index 0000000000000000000000000000000000000000..a2cc1aadb44a5d114726de32be0ca11790081360 --- /dev/null +++ b/maximum-subsequence-score.json @@ -0,0 +1,33 @@ +{ + "id": 2636, + "name": "maximum-subsequence-score", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-subsequence-score/", + "date": "2023-01-07", + "task_description": "You are given two **0-indexed** integer arrays `nums1` and `nums2` of equal length `n` and a positive integer `k`. You must choose a **subsequence** of indices from `nums1` of length `k`. For chosen indices `i0`, `i1`, ..., `ik - 1`, your **score** is defined as: The sum of the selected elements from `nums1` multiplied with the **minimum** of the selected elements from `nums2`. It can defined simply as: `(nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1])`. Return _the **maximum** possible score._ A **subsequence** of indices of an array is a set that can be derived from the set `{0, 1, ..., n-1}` by deleting some or no elements. **Example 1:** ``` **Input:** nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3 **Output:** 12 **Explanation:** The four possible subsequence scores are: - We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7. - We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6. - We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12. - We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8. Therefore, we return the max score, which is 12. ``` **Example 2:** ``` **Input:** nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1 **Output:** 30 **Explanation:** Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score. ``` **Constraints:** `n == nums1.length == nums2.length` `1 <= n <= 105` `0 <= nums1[i], nums2[j] <= 105` `1 <= k <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3", + "output": "12 " + }, + { + "label": "Example 2", + "input": "nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1", + "output": "30 " + } + ], + "constraints": [ + "The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2.", + "It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).", + "n == nums1.length == nums2.length", + "1 <= n <= 105", + "0 <= nums1[i], nums2[j] <= 105", + "1 <= k <= n" + ], + "python_template": "class Solution(object):\n def maxScore(self, nums1, nums2, k):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxScore(int[] nums1, int[] nums2, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxScore" + } +} \ No newline at end of file diff --git a/maximum-sum-of-almost-unique-subarray.json b/maximum-sum-of-almost-unique-subarray.json new file mode 100644 index 0000000000000000000000000000000000000000..9dcd2be9e5477d981b803d950ce0561d0d1f856a --- /dev/null +++ b/maximum-sum-of-almost-unique-subarray.json @@ -0,0 +1,35 @@ +{ + "id": 2954, + "name": "maximum-sum-of-almost-unique-subarray", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-sum-of-almost-unique-subarray/", + "date": "2023-08-19", + "task_description": "You are given an integer array `nums` and two positive integers `m` and `k`. Return _the **maximum sum** out of all **almost unique** subarrays of length _`k`_ of_ `nums`. If no such subarray exists, return `0`. A subarray of `nums` is **almost unique** if it contains at least `m` distinct elements. A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [2,6,7,3,1,7], m = 3, k = 4 **Output:** 18 **Explanation:** There are 3 almost unique subarrays of size `k = 4`. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18. ``` **Example 2:** ``` **Input:** nums = [5,9,9,2,4,5,4], m = 1, k = 3 **Output:** 23 **Explanation:** There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23. ``` **Example 3:** ``` **Input:** nums = [1,2,1,2,1,2,1], m = 3, k = 3 **Output:** 0 **Explanation:** There are no subarrays of size `k = 3` that contain at least `m = 3` distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0. ``` **Constraints:** `1 <= nums.length <= 2 * 104` `1 <= m <= k <= nums.length` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,6,7,3,1,7], m = 3, k = 4", + "output": "18 " + }, + { + "label": "Example 2", + "input": "nums = [5,9,9,2,4,5,4], m = 1, k = 3", + "output": "23 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,1,2,1,2,1], m = 3, k = 3", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 2 * 104", + "1 <= m <= k <= nums.length", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def maxSum(self, nums, m, k):\n \"\"\"\n :type nums: List[int]\n :type m: int\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxSum(List nums, int m, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxSum" + } +} \ No newline at end of file diff --git a/maximum-sum-of-distinct-subarrays-with-length-k.json b/maximum-sum-of-distinct-subarrays-with-length-k.json new file mode 100644 index 0000000000000000000000000000000000000000..4099c619de448d4f6a0f1e192e3eced6c53da1d4 --- /dev/null +++ b/maximum-sum-of-distinct-subarrays-with-length-k.json @@ -0,0 +1,31 @@ +{ + "id": 2552, + "name": "maximum-sum-of-distinct-subarrays-with-length-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/", + "date": "2022-10-30", + "task_description": "You are given an integer array `nums` and an integer `k`. Find the maximum subarray sum of all the subarrays of `nums` that meet the following conditions: The length of the subarray is `k`, and All the elements of the subarray are **distinct**. Return _the maximum subarray sum of all the subarrays that meet the conditions__._ If no subarray meets the conditions, return `0`. _A **subarray** is a contiguous non-empty sequence of elements within an array._ **Example 1:** ``` **Input:** nums = [1,5,4,2,9,9,9], k = 3 **Output:** 15 **Explanation:** The subarrays of nums with length 3 are: - [1,5,4] which meets the requirements and has a sum of 10. - [5,4,2] which meets the requirements and has a sum of 11. - [4,2,9] which meets the requirements and has a sum of 15. - [2,9,9] which does not meet the requirements because the element 9 is repeated. - [9,9,9] which does not meet the requirements because the element 9 is repeated. We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions ``` **Example 2:** ``` **Input:** nums = [4,4,4], k = 3 **Output:** 0 **Explanation:** The subarrays of nums with length 3 are: - [4,4,4] which does not meet the requirements because the element 4 is repeated. We return 0 because no subarrays meet the conditions. ``` **Constraints:** `1 <= k <= nums.length <= 105` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,5,4,2,9,9,9], k = 3", + "output": "15 " + }, + { + "label": "Example 2", + "input": "nums = [4,4,4], k = 3", + "output": "0 " + } + ], + "constraints": [ + "The length of the subarray is k, and", + "All the elements of the subarray are distinct.", + "1 <= k <= nums.length <= 105", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def maximumSubarraySum(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 maximumSubarraySum(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumSubarraySum" + } +} \ No newline at end of file diff --git a/maximum-sum-queries.json b/maximum-sum-queries.json new file mode 100644 index 0000000000000000000000000000000000000000..b0db5deaf2cbf69cc56d865a6cfc6690f0860a0b --- /dev/null +++ b/maximum-sum-queries.json @@ -0,0 +1,41 @@ +{ + "id": 2839, + "name": "maximum-sum-queries", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-sum-queries/", + "date": "2023-06-04", + "task_description": "You are given two **0-indexed** integer arrays `nums1` and `nums2`, each of length `n`, and a **1-indexed 2D array** `queries` where `queries[i] = [xi, yi]`. For the `ith` query, find the **maximum value** of `nums1[j] + nums2[j]` among all indices `j` `(0 <= j < n)`, where `nums1[j] >= xi` and `nums2[j] >= yi`, or **-1** if there is no `j` satisfying the constraints. Return _an array _`answer`_ where _`answer[i]`_ is the answer to the _`ith`_ query._ **Example 1:** ``` **Input:** nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]] **Output:** [6,10,7] **Explanation:** For the 1st query `xi = 4` and `yi = 1`, we can select index `j = 0` since `nums1[j] >= 4` and `nums2[j] >= 1`. The sum `nums1[j] + nums2[j]` is 6, and we can show that 6 is the maximum we can obtain. For the 2nd query `xi = 1` and `yi = 3`, we can select index `j = 2` since `nums1[j] >= 1` and `nums2[j] >= 3`. The sum `nums1[j] + nums2[j]` is 10, and we can show that 10 is the maximum we can obtain. For the 3rd query `xi = 2` and `yi = 5`, we can select index `j = 3` since `nums1[j] >= 2` and `nums2[j] >= 5`. The sum `nums1[j] + nums2[j]` is 7, and we can show that 7 is the maximum we can obtain. Therefore, we return `[6,10,7]`. ``` **Example 2:** ``` **Input:** nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]] **Output:** [9,9,9] **Explanation:** For this example, we can use index `j = 2` for all the queries since it satisfies the constraints for each query. ``` **Example 3:** ``` **Input:** nums1 = [2,1], nums2 = [2,3], queries = [[3,3]] **Output:** [-1] **Explanation:** There is one query in this example with `xi` = 3 and `yi` = 3. For every index, j, either nums1[j] < `xi` or nums2[j] < `yi`. Hence, there is no solution. ``` **Constraints:** `nums1.length == nums2.length` `n == nums1.length ` `1 <= n <= 105` `1 <= nums1[i], nums2[i] <= 109 ` `1 <= queries.length <= 105` `queries[i].length == 2` `xi == queries[i][1]` `yi == queries[i][2]` `1 <= xi, yi <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [4,3,1,2], nums2 = [2,4,9,5], queries = [[4,1],[1,3],[2,5]]", + "output": "[6,10,7] " + }, + { + "label": "Example 2", + "input": "nums1 = [3,2,5], nums2 = [2,3,4], queries = [[4,4],[3,2],[1,1]]", + "output": "[9,9,9] " + }, + { + "label": "Example 3", + "input": "nums1 = [2,1], nums2 = [2,3], queries = [[3,3]]", + "output": "[-1] " + } + ], + "constraints": [ + "nums1.length == nums2.length", + "n == nums1.length", + "1 <= n <= 105", + "1 <= nums1[i], nums2[i] <= 109", + "1 <= queries.length <= 105", + "queries[i].length == 2", + "xi == queries[i][1]", + "yi == queries[i][2]", + "1 <= xi, yi <= 109" + ], + "python_template": "class Solution(object):\n def maximumSumQueries(self, nums1, nums2, queries):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "maximumSumQueries" + } +} \ No newline at end of file diff --git a/maximum-sum-with-at-most-k-elements.json b/maximum-sum-with-at-most-k-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..479ed799bed664e296f99f0b7d595061bd613905 --- /dev/null +++ b/maximum-sum-with-at-most-k-elements.json @@ -0,0 +1,39 @@ +{ + "id": 3764, + "name": "maximum-sum-with-at-most-k-elements", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-sum-with-at-most-k-elements/", + "date": "2025-02-16", + "task_description": "You are given a 2D integer matrix `grid` of size `n x m`, an integer array `limits` of length `n`, and an integer `k`. The task is to find the **maximum sum** of **at most** `k` elements from the matrix `grid` such that: The number of elements taken from the `ith` row of `grid` does not exceed `limits[i]`. Return the **maximum sum**. **Example 1:** **Input:** grid = [[1,2],[3,4]], limits = [1,2], k = 2 **Output:** 7 **Explanation:** From the second row, we can take at most 2 elements. The elements taken are 4 and 3. The maximum possible sum of at most 2 selected elements is `4 + 3 = 7`. **Example 2:** **Input:** grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3 **Output:** 21 **Explanation:** From the first row, we can take at most 2 elements. The element taken is 7. From the second row, we can take at most 2 elements. The elements taken are 8 and 6. The maximum possible sum of at most 3 selected elements is `7 + 8 + 6 = 21`. **Constraints:** `n == grid.length == limits.length` `m == grid[i].length` `1 <= n, m <= 500` `0 <= grid[i][j] <= 105` `0 <= limits[i] <= m` `0 <= k <= min(n * m, sum(limits))`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[1,2],[3,4]], limits = [1,2], k = 2", + "output": "7 " + }, + { + "label": "Example 2", + "input": "grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3", + "output": "21 " + } + ], + "constraints": [ + "The number of elements taken from the ith row of grid does not exceed limits[i].", + "From the second row, we can take at most 2 elements. The elements taken are 4 and 3.", + "The maximum possible sum of at most 2 selected elements is 4 + 3 = 7.", + "From the first row, we can take at most 2 elements. The element taken is 7.", + "From the second row, we can take at most 2 elements. The elements taken are 8 and 6.", + "The maximum possible sum of at most 3 selected elements is 7 + 8 + 6 = 21.", + "n == grid.length == limits.length", + "m == grid[i].length", + "1 <= n, m <= 500", + "0 <= grid[i][j] <= 105", + "0 <= limits[i] <= m", + "0 <= k <= min(n * m, sum(limits))" + ], + "python_template": "class Solution(object):\n def maxSum(self, grid, limits, k):\n \"\"\"\n :type grid: List[List[int]]\n :type limits: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maxSum(int[][] grid, int[] limits, int k) {\n \n }\n}", + "metadata": { + "func_name": "maxSum" + } +} \ No newline at end of file diff --git a/maximum-sum-with-exactly-k-elements.json b/maximum-sum-with-exactly-k-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..6bd518123fe955e5c29d0cd5db0c46edd70a4229 --- /dev/null +++ b/maximum-sum-with-exactly-k-elements.json @@ -0,0 +1,30 @@ +{ + "id": 2767, + "name": "maximum-sum-with-exactly-k-elements", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/", + "date": "2023-04-15", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `k`. Your task is to perform the following operation **exactly** `k` times in order to maximize your score: Select an element `m` from `nums`. Remove the selected element `m` from the array. Add a new element with a value of `m + 1` to the array. Increase your score by `m`. Return _the maximum score you can achieve after performing the operation exactly_ `k` _times._ **Example 1:** ``` **Input:** nums = [1,2,3,4,5], k = 3 **Output:** 18 **Explanation:** We need to choose exactly 3 elements from nums to maximize the sum. For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6] For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7] For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8] So, we will return 18. It can be proven, that 18 is the maximum answer that we can achieve. ``` **Example 2:** ``` **Input:** nums = [5,5,5], k = 2 **Output:** 11 **Explanation:** We need to choose exactly 2 elements from nums to maximize the sum. For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6] For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7] So, we will return 11. It can be proven, that 11 is the maximum answer that we can achieve. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100` `1 <= k <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5], k = 3", + "output": "18 " + }, + { + "label": "Example 2", + "input": "nums = [5,5,5], k = 2", + "output": "11 " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100", + "1 <= k <= 100" + ], + "python_template": "class Solution(object):\n def maximizeSum(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 maximizeSum(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximizeSum" + } +} \ No newline at end of file diff --git a/maximum-tastiness-of-candy-basket.json b/maximum-tastiness-of-candy-basket.json new file mode 100644 index 0000000000000000000000000000000000000000..42453f5fabaa2e70b8eb32cb612d2af1bcca6462 --- /dev/null +++ b/maximum-tastiness-of-candy-basket.json @@ -0,0 +1,34 @@ +{ + "id": 2600, + "name": "maximum-tastiness-of-candy-basket", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-tastiness-of-candy-basket/", + "date": "2022-12-18", + "task_description": "You are given an array of positive integers `price` where `price[i]` denotes the price of the `ith` candy and a positive integer `k`. The store sells baskets of `k` **distinct** candies. The **tastiness** of a candy basket is the smallest absolute difference of the **prices** of any two candies in the basket. Return _the **maximum** tastiness of a candy basket._ **Example 1:** ``` **Input:** price = [13,5,1,8,21,2], k = 3 **Output:** 8 **Explanation:** Choose the candies with the prices [13,5,21]. The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8. It can be proven that 8 is the maximum tastiness that can be achieved. ``` **Example 2:** ``` **Input:** price = [1,3,1], k = 2 **Output:** 2 **Explanation:** Choose the candies with the prices [1,3]. The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2. It can be proven that 2 is the maximum tastiness that can be achieved. ``` **Example 3:** ``` **Input:** price = [7,7,7,7], k = 2 **Output:** 0 **Explanation:** Choosing any two distinct candies from the candies we have will result in a tastiness of 0. ``` **Constraints:** `2 <= k <= price.length <= 105` `1 <= price[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "price = [13,5,1,8,21,2], k = 3", + "output": "8 " + }, + { + "label": "Example 2", + "input": "price = [1,3,1], k = 2", + "output": "2 " + }, + { + "label": "Example 3", + "input": "price = [7,7,7,7], k = 2", + "output": "0 " + } + ], + "constraints": [ + "2 <= k <= price.length <= 105", + "1 <= price[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumTastiness(self, price, k):\n \"\"\"\n :type price: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumTastiness(int[] price, int k) {\n \n }\n}", + "metadata": { + "func_name": "maximumTastiness" + } +} \ No newline at end of file diff --git a/maximum-total-beauty-of-the-gardens.json b/maximum-total-beauty-of-the-gardens.json new file mode 100644 index 0000000000000000000000000000000000000000..0ff3d4d00346a4fc6fe5938389928afaa1b74881 --- /dev/null +++ b/maximum-total-beauty-of-the-gardens.json @@ -0,0 +1,33 @@ +{ + "id": 2330, + "name": "maximum-total-beauty-of-the-gardens", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-total-beauty-of-the-gardens/", + "date": "2022-04-03", + "task_description": "Alice is a caretaker of `n` gardens and she wants to plant flowers to maximize the total beauty of all her gardens. You are given a **0-indexed** integer array `flowers` of size `n`, where `flowers[i]` is the number of flowers already planted in the `ith` garden. Flowers that are already planted **cannot** be removed. You are then given another integer `newFlowers`, which is the **maximum** number of flowers that Alice can additionally plant. You are also given the integers `target`, `full`, and `partial`. A garden is considered **complete** if it has **at least** `target` flowers. The **total beauty** of the gardens is then determined as the **sum** of the following: The number of **complete** gardens multiplied by `full`. The **minimum** number of flowers in any of the **incomplete** gardens multiplied by `partial`. If there are no incomplete gardens, then this value will be `0`. Return _the **maximum** total beauty that Alice can obtain after planting at most _`newFlowers`_ flowers._ **Example 1:** ``` **Input:** flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 12, partial = 1 **Output:** 14 **Explanation:** Alice can plant - 2 flowers in the 0th garden - 3 flowers in the 1st garden - 1 flower in the 2nd garden - 1 flower in the 3rd garden The gardens will then be [3,6,2,2]. She planted a total of 2 + 3 + 1 + 1 = 7 flowers. There is 1 garden that is complete. The minimum number of flowers in the incomplete gardens is 2. Thus, the total beauty is 1 * 12 + 2 * 1 = 12 + 2 = 14. No other way of planting flowers can obtain a total beauty higher than 14. ``` **Example 2:** ``` **Input:** flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6 **Output:** 30 **Explanation:** Alice can plant - 3 flowers in the 0th garden - 0 flowers in the 1st garden - 0 flowers in the 2nd garden - 2 flowers in the 3rd garden The gardens will then be [5,4,5,5]. She planted a total of 3 + 0 + 0 + 2 = 5 flowers. There are 3 gardens that are complete. The minimum number of flowers in the incomplete gardens is 4. Thus, the total beauty is 3 * 2 + 4 * 6 = 6 + 24 = 30. No other way of planting flowers can obtain a total beauty higher than 30. Note that Alice could make all the gardens complete but in this case, she would obtain a lower total beauty. ``` **Constraints:** `1 <= flowers.length <= 105` `1 <= flowers[i], target <= 105` `1 <= newFlowers <= 1010` `1 <= full, partial <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "flowers = [1,3,1,1], newFlowers = 7, target = 6, full = 12, partial = 1", + "output": "14 " + }, + { + "label": "Example 2", + "input": "flowers = [2,4,5,3], newFlowers = 10, target = 5, full = 2, partial = 6", + "output": "30 " + } + ], + "constraints": [ + "The number of complete gardens multiplied by full.", + "The minimum number of flowers in any of the incomplete gardens multiplied by partial. If there are no incomplete gardens, then this value will be 0.", + "1 <= flowers.length <= 105", + "1 <= flowers[i], target <= 105", + "1 <= newFlowers <= 1010", + "1 <= full, partial <= 105" + ], + "python_template": "class Solution(object):\n def maximumBeauty(self, flowers, newFlowers, target, full, partial):\n \"\"\"\n :type flowers: List[int]\n :type newFlowers: int\n :type target: int\n :type full: int\n :type partial: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumBeauty(int[] flowers, long newFlowers, int target, int full, int partial) {\n \n }\n}", + "metadata": { + "func_name": "maximumBeauty" + } +} \ No newline at end of file diff --git a/maximum-total-damage-with-spell-casting.json b/maximum-total-damage-with-spell-casting.json new file mode 100644 index 0000000000000000000000000000000000000000..a6c50909f28a1e6cd58b1a7de9a27415ecae38f9 --- /dev/null +++ b/maximum-total-damage-with-spell-casting.json @@ -0,0 +1,29 @@ +{ + "id": 3437, + "name": "maximum-total-damage-with-spell-casting", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-total-damage-with-spell-casting/", + "date": "2024-06-09", + "task_description": "A magician has various spells. You are given an array `power`, where each element represents the damage of a spell. Multiple spells can have the same damage value. It is a known fact that if a magician decides to cast a spell with a damage of `power[i]`, they **cannot** cast any spell with a damage of `power[i] - 2`, `power[i] - 1`, `power[i] + 1`, or `power[i] + 2`. Each spell can be cast **only once**. Return the **maximum** possible _total damage_ that a magician can cast. **Example 1:** **Input:** power = [1,1,3,4] **Output:** 6 **Explanation:** The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4. **Example 2:** **Input:** power = [7,1,6,6] **Output:** 13 **Explanation:** The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6. **Constraints:** `1 <= power.length <= 105` `1 <= power[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "power = [1,1,3,4]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "power = [7,1,6,6]", + "output": "13 " + } + ], + "constraints": [ + "1 <= power.length <= 105", + "1 <= power[i] <= 109" + ], + "python_template": "class Solution(object):\n def maximumTotalDamage(self, power):\n \"\"\"\n :type power: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumTotalDamage(int[] power) {\n \n }\n}", + "metadata": { + "func_name": "maximumTotalDamage" + } +} \ No newline at end of file diff --git a/maximum-total-importance-of-roads.json b/maximum-total-importance-of-roads.json new file mode 100644 index 0000000000000000000000000000000000000000..cf2abf26b3cbe29b877e05f715e671ab7f355265 --- /dev/null +++ b/maximum-total-importance-of-roads.json @@ -0,0 +1,33 @@ +{ + "id": 2379, + "name": "maximum-total-importance-of-roads", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-total-importance-of-roads/", + "date": "2022-05-14", + "task_description": "You are given an integer `n` denoting the number of cities in a country. The cities are numbered from `0` to `n - 1`. You are also given a 2D integer array `roads` where `roads[i] = [ai, bi]` denotes that there exists a **bidirectional** road connecting cities `ai` and `bi`. You need to assign each city with an integer value from `1` to `n`, where each value can only be used **once**. The **importance** of a road is then defined as the **sum** of the values of the two cities it connects. Return _the **maximum total importance** of all roads possible after assigning the values optimally._ **Example 1:** ``` **Input:** n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]] **Output:** 43 **Explanation:** The figure above shows the country and the assigned values of [2,4,5,3,1]. - The road (0,1) has an importance of 2 + 4 = 6. - The road (1,2) has an importance of 4 + 5 = 9. - The road (2,3) has an importance of 5 + 3 = 8. - The road (0,2) has an importance of 2 + 5 = 7. - The road (1,3) has an importance of 4 + 3 = 7. - The road (2,4) has an importance of 5 + 1 = 6. The total importance of all roads is 6 + 9 + 8 + 7 + 7 + 6 = 43. It can be shown that we cannot obtain a greater total importance than 43. ``` **Example 2:** ``` **Input:** n = 5, roads = [[0,3],[2,4],[1,3]] **Output:** 20 **Explanation:** The figure above shows the country and the assigned values of [4,3,2,5,1]. - The road (0,3) has an importance of 4 + 5 = 9. - The road (2,4) has an importance of 2 + 1 = 3. - The road (1,3) has an importance of 3 + 5 = 8. The total importance of all roads is 9 + 3 + 8 = 20. It can be shown that we cannot obtain a greater total importance than 20. ``` **Constraints:** `2 <= n <= 5 * 104` `1 <= roads.length <= 5 * 104` `roads[i].length == 2` `0 <= ai, bi <= n - 1` `ai != bi` There are no duplicate roads.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 5, roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]", + "output": "43 " + }, + { + "label": "Example 2", + "input": "n = 5, roads = [[0,3],[2,4],[1,3]]", + "output": "20 " + } + ], + "constraints": [ + "2 <= n <= 5 * 104", + "1 <= roads.length <= 5 * 104", + "roads[i].length == 2", + "0 <= ai, bi <= n - 1", + "ai != bi", + "There are no duplicate roads." + ], + "python_template": "class Solution(object):\n def maximumImportance(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 long maximumImportance(int n, int[][] roads) {\n \n }\n}", + "metadata": { + "func_name": "maximumImportance" + } +} \ No newline at end of file diff --git a/maximum-total-reward-using-operations-i.json b/maximum-total-reward-using-operations-i.json new file mode 100644 index 0000000000000000000000000000000000000000..05250c12a48a889b0ea8fe0638377f5a9c7010ff --- /dev/null +++ b/maximum-total-reward-using-operations-i.json @@ -0,0 +1,31 @@ +{ + "id": 3442, + "name": "maximum-total-reward-using-operations-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-total-reward-using-operations-i/", + "date": "2024-06-02", + "task_description": "You are given an integer array `rewardValues` of length `n`, representing the values of rewards. Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times: Choose an **unmarked** index `i` from the range `[0, n - 1]`. If `rewardValues[i]` is **greater** than your current total reward `x`, then add `rewardValues[i]` to `x` (i.e., `x = x + rewardValues[i]`), and **mark** the index `i`. Return an integer denoting the **maximum **_total reward_ you can collect by performing the operations optimally. **Example 1:** **Input:** rewardValues = [1,1,3,3] **Output:** 4 **Explanation:** During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum. **Example 2:** **Input:** rewardValues = [1,6,4,3,2] **Output:** 11 **Explanation:** Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum. **Constraints:** `1 <= rewardValues.length <= 2000` `1 <= rewardValues[i] <= 2000`", + "test_case": [ + { + "label": "Example 1", + "input": "rewardValues = [1,1,3,3]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "rewardValues = [1,6,4,3,2]", + "output": "11 " + } + ], + "constraints": [ + "Choose an unmarked index i from the range [0, n - 1].", + "If rewardValues[i] is greater than your current total reward x, then add rewardValues[i] to x (i.e., x = x + rewardValues[i]), and mark the index i.", + "1 <= rewardValues.length <= 2000", + "1 <= rewardValues[i] <= 2000" + ], + "python_template": "class Solution(object):\n def maxTotalReward(self, rewardValues):\n \"\"\"\n :type rewardValues: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxTotalReward(int[] rewardValues) {\n \n }\n}", + "metadata": { + "func_name": "maxTotalReward" + } +} \ No newline at end of file diff --git a/maximum-total-reward-using-operations-ii.json b/maximum-total-reward-using-operations-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..7de25fca770ab0af764bccfab26be67f8ca2e7b5 --- /dev/null +++ b/maximum-total-reward-using-operations-ii.json @@ -0,0 +1,31 @@ +{ + "id": 3443, + "name": "maximum-total-reward-using-operations-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-total-reward-using-operations-ii/", + "date": "2024-06-02", + "task_description": "You are given an integer array `rewardValues` of length `n`, representing the values of rewards. Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times: Choose an **unmarked** index `i` from the range `[0, n - 1]`. If `rewardValues[i]` is **greater** than your current total reward `x`, then add `rewardValues[i]` to `x` (i.e., `x = x + rewardValues[i]`), and **mark** the index `i`. Return an integer denoting the **maximum **_total reward_ you can collect by performing the operations optimally. **Example 1:** **Input:** rewardValues = [1,1,3,3] **Output:** 4 **Explanation:** During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum. **Example 2:** **Input:** rewardValues = [1,6,4,3,2] **Output:** 11 **Explanation:** Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum. **Constraints:** `1 <= rewardValues.length <= 5 * 104` `1 <= rewardValues[i] <= 5 * 104`", + "test_case": [ + { + "label": "Example 1", + "input": "rewardValues = [1,1,3,3]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "rewardValues = [1,6,4,3,2]", + "output": "11 " + } + ], + "constraints": [ + "Choose an unmarked index i from the range [0, n - 1].", + "If rewardValues[i] is greater than your current total reward x, then add rewardValues[i] to x (i.e., x = x + rewardValues[i]), and mark the index i.", + "1 <= rewardValues.length <= 5 * 104", + "1 <= rewardValues[i] <= 5 * 104" + ], + "python_template": "class Solution(object):\n def maxTotalReward(self, rewardValues):\n \"\"\"\n :type rewardValues: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxTotalReward(int[] rewardValues) {\n \n }\n}", + "metadata": { + "func_name": "maxTotalReward" + } +} \ No newline at end of file diff --git a/maximum-trailing-zeros-in-a-cornered-path.json b/maximum-trailing-zeros-in-a-cornered-path.json new file mode 100644 index 0000000000000000000000000000000000000000..2ec97e0b9bdccc8e0af146270a886dd150bddd5d --- /dev/null +++ b/maximum-trailing-zeros-in-a-cornered-path.json @@ -0,0 +1,34 @@ +{ + "id": 2363, + "name": "maximum-trailing-zeros-in-a-cornered-path", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/", + "date": "2022-04-10", + "task_description": "You are given a 2D integer array `grid` of size `m x n`, where each cell contains a positive integer. A **cornered path** is defined as a set of adjacent cells with **at most** one turn. More specifically, the path should exclusively move either **horizontally** or **vertically** up to the turn (if there is one), without returning to a previously visited cell. After the turn, the path will then move exclusively in the **alternate** direction: move vertically if it moved horizontally, and vice versa, also without returning to a previously visited cell. The **product** of a path is defined as the product of all the values in the path. Return _the **maximum** number of **trailing zeros** in the product of a cornered path found in _`grid`. Note: **Horizontal** movement means moving in either the left or right direction. **Vertical** movement means moving in either the up or down direction. **Example 1:** ``` **Input:** grid = [[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]] **Output:** 3 **Explanation:** The grid on the left shows a valid cornered path. It has a product of 15 * 20 * 6 * 1 * 10 = 18000 which has 3 trailing zeros. It can be shown that this is the maximum trailing zeros in the product of a cornered path. The grid in the middle is not a cornered path as it has more than one turn. The grid on the right is not a cornered path as it requires a return to a previously visited cell. ``` **Example 2:** ``` **Input:** grid = [[4,3,2],[7,6,1],[8,8,8]] **Output:** 0 **Explanation:** The grid is shown in the figure above. There are no cornered paths in the grid that result in a product with a trailing zero. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 105` `1 <= m * n <= 105` `1 <= grid[i][j] <= 1000`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "grid = [[4,3,2],[7,6,1],[8,8,8]]", + "output": "0 " + } + ], + "constraints": [ + "Horizontal movement means moving in either the left or right direction.", + "Vertical movement means moving in either the up or down direction.", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 105", + "1 <= m * n <= 105", + "1 <= grid[i][j] <= 1000" + ], + "python_template": "class Solution(object):\n def maxTrailingZeros(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxTrailingZeros(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "maxTrailingZeros" + } +} \ No newline at end of file diff --git a/maximum-unique-subarray-sum-after-deletion.json b/maximum-unique-subarray-sum-after-deletion.json new file mode 100644 index 0000000000000000000000000000000000000000..32cbd2c70ab2202683dada68a52f99aa9e9c87c4 --- /dev/null +++ b/maximum-unique-subarray-sum-after-deletion.json @@ -0,0 +1,34 @@ +{ + "id": 3788, + "name": "maximum-unique-subarray-sum-after-deletion", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-unique-subarray-sum-after-deletion/", + "date": "2025-03-09", + "task_description": "You are given an integer array `nums`. You are allowed to delete any number of elements from `nums` without making it **empty**. After performing the deletions, select a subarray of `nums` such that: All elements in the subarray are **unique**. The sum of the elements in the subarray is **maximized**. Return the **maximum sum** of such a subarray. **Example 1:** **Input:** nums = [1,2,3,4,5] **Output:** 15 **Explanation:** Select the entire array without deleting any element to obtain the maximum sum. **Example 2:** **Input:** nums = [1,1,0,1,1] **Output:** 1 **Explanation:** Delete the element `nums[0] == 1`, `nums[1] == 1`, `nums[2] == 0`, and `nums[3] == 1`. Select the entire array `[1]` to obtain the maximum sum. **Example 3:** **Input:** nums = [1,2,-1,-2,1,0,-1] **Output:** 3 **Explanation:** Delete the elements `nums[2] == -1` and `nums[3] == -2`, and select the subarray `[2, 1]` from `[1, 2, 1, 0, -1]` to obtain the maximum sum. **Constraints:** `1 <= nums.length <= 100` `-100 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5]", + "output": "15 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,0,1,1]", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,-1,-2,1,0,-1]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 100", + "-100 <= nums[i] <= 100" + ], + "python_template": "class Solution(object):\n def maxSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maxSum(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maxSum" + } +} \ No newline at end of file diff --git a/maximum-value-of-a-string-in-an-array.json b/maximum-value-of-a-string-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..e86960fe1e108eed89694140699205a2b3f06ed4 --- /dev/null +++ b/maximum-value-of-a-string-in-an-array.json @@ -0,0 +1,32 @@ +{ + "id": 2589, + "name": "maximum-value-of-a-string-in-an-array", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/", + "date": "2022-11-26", + "task_description": "The **value** of an alphanumeric string can be defined as: The **numeric** representation of the string in base `10`, if it comprises of digits **only**. The **length** of the string, otherwise. Given an array `strs` of alphanumeric strings, return _the **maximum value** of any string in _`strs`. **Example 1:** ``` **Input:** strs = [\"alic3\",\"bob\",\"3\",\"4\",\"00000\"] **Output:** 5 **Explanation:** - \"alic3\" consists of both letters and digits, so its value is its length, i.e. 5. - \"bob\" consists only of letters, so its value is also its length, i.e. 3. - \"3\" consists only of digits, so its value is its numeric equivalent, i.e. 3. - \"4\" also consists only of digits, so its value is 4. - \"00000\" consists only of digits, so its value is 0. Hence, the maximum value is 5, of \"alic3\". ``` **Example 2:** ``` **Input:** strs = [\"1\",\"01\",\"001\",\"0001\"] **Output:** 1 **Explanation:** Each string in the array has value 1. Hence, we return 1. ``` **Constraints:** `1 <= strs.length <= 100` `1 <= strs[i].length <= 9` `strs[i]` consists of only lowercase English letters and digits.", + "test_case": [ + { + "label": "Example 1", + "input": "strs = [\"alic3\",\"bob\",\"3\",\"4\",\"00000\"]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "strs = [\"1\",\"01\",\"001\",\"0001\"]", + "output": "1 " + } + ], + "constraints": [ + "The numeric representation of the string in base 10, if it comprises of digits only.", + "The length of the string, otherwise.", + "1 <= strs.length <= 100", + "1 <= strs[i].length <= 9", + "strs[i] consists of only lowercase English letters and digits." + ], + "python_template": "class Solution(object):\n def maximumValue(self, strs):\n \"\"\"\n :type strs: List[str]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumValue(String[] strs) {\n \n }\n}", + "metadata": { + "func_name": "maximumValue" + } +} \ No newline at end of file diff --git a/maximum-value-of-an-ordered-triplet-i.json b/maximum-value-of-an-ordered-triplet-i.json new file mode 100644 index 0000000000000000000000000000000000000000..41b84b9afdf88ca273d254be5463a67a35faa9c5 --- /dev/null +++ b/maximum-value-of-an-ordered-triplet-i.json @@ -0,0 +1,34 @@ +{ + "id": 3154, + "name": "maximum-value-of-an-ordered-triplet-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i/", + "date": "2023-09-24", + "task_description": "You are given a **0-indexed** integer array `nums`. Return _**the maximum value over all triplets of indices**_ `(i, j, k)` _such that_ `i < j < k`. If all such triplets have a negative value, return `0`. The **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`. **Example 1:** ``` **Input:** nums = [12,6,1,2,7] **Output:** 77 **Explanation:** The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77. It can be shown that there are no ordered triplets of indices with a value greater than 77. ``` **Example 2:** ``` **Input:** nums = [1,10,3,4,19] **Output:** 133 **Explanation:** The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133. It can be shown that there are no ordered triplets of indices with a value greater than 133. ``` **Example 3:** ``` **Input:** nums = [1,2,3] **Output:** 0 **Explanation:** The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0. ``` **Constraints:** `3 <= nums.length <= 100` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [12,6,1,2,7]", + "output": "77 " + }, + { + "label": "Example 2", + "input": "nums = [1,10,3,4,19]", + "output": "133 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3]", + "output": "0 " + } + ], + "constraints": [ + "3 <= nums.length <= 100", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maximumTripletValue(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumTripletValue(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumTripletValue" + } +} \ No newline at end of file diff --git a/maximum-value-of-an-ordered-triplet-ii.json b/maximum-value-of-an-ordered-triplet-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..ab22ac5ae67fe8a9dd13f8998ebc0db607cb01e2 --- /dev/null +++ b/maximum-value-of-an-ordered-triplet-ii.json @@ -0,0 +1,34 @@ +{ + "id": 3152, + "name": "maximum-value-of-an-ordered-triplet-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/", + "date": "2023-09-24", + "task_description": "You are given a **0-indexed** integer array `nums`. Return _**the maximum value over all triplets of indices**_ `(i, j, k)` _such that_ `i < j < k`_. _If all such triplets have a negative value, return `0`. The **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`. **Example 1:** ``` **Input:** nums = [12,6,1,2,7] **Output:** 77 **Explanation:** The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77. It can be shown that there are no ordered triplets of indices with a value greater than 77. ``` **Example 2:** ``` **Input:** nums = [1,10,3,4,19] **Output:** 133 **Explanation:** The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133. It can be shown that there are no ordered triplets of indices with a value greater than 133. ``` **Example 3:** ``` **Input:** nums = [1,2,3] **Output:** 0 **Explanation:** The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0. ``` **Constraints:** `3 <= nums.length <= 105` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [12,6,1,2,7]", + "output": "77 " + }, + { + "label": "Example 2", + "input": "nums = [1,10,3,4,19]", + "output": "133 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3]", + "output": "0 " + } + ], + "constraints": [ + "3 <= nums.length <= 105", + "1 <= nums[i] <= 106" + ], + "python_template": "class Solution(object):\n def maximumTripletValue(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long maximumTripletValue(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumTripletValue" + } +} \ No newline at end of file diff --git a/maximum-white-tiles-covered-by-a-carpet.json b/maximum-white-tiles-covered-by-a-carpet.json new file mode 100644 index 0000000000000000000000000000000000000000..f8c6a655c3a7c606d4b10622d429dff4b4de236b --- /dev/null +++ b/maximum-white-tiles-covered-by-a-carpet.json @@ -0,0 +1,32 @@ +{ + "id": 2359, + "name": "maximum-white-tiles-covered-by-a-carpet", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/", + "date": "2022-04-30", + "task_description": "You are given a 2D integer array `tiles` where `tiles[i] = [li, ri]` represents that every tile `j` in the range `li <= j <= ri` is colored white. You are also given an integer `carpetLen`, the length of a single carpet that can be placed **anywhere**. Return _the **maximum** number of white tiles that can be covered by the carpet_. **Example 1:** ``` **Input:** tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10 **Output:** 9 **Explanation:** Place the carpet starting on tile 10. It covers 9 white tiles, so we return 9. Note that there may be other places where the carpet covers 9 white tiles. It can be shown that the carpet cannot cover more than 9 white tiles. ``` **Example 2:** ``` **Input:** tiles = [[10,11],[1,1]], carpetLen = 2 **Output:** 2 **Explanation:** Place the carpet starting on tile 10. It covers 2 white tiles, so we return 2. ``` **Constraints:** `1 <= tiles.length <= 5 * 104` `tiles[i].length == 2` `1 <= li <= ri <= 109` `1 <= carpetLen <= 109` The `tiles` are **non-overlapping**.", + "test_case": [ + { + "label": "Example 1", + "input": "tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10", + "output": "9 " + }, + { + "label": "Example 2", + "input": "tiles = [[10,11],[1,1]], carpetLen = 2", + "output": "2 " + } + ], + "constraints": [ + "1 <= tiles.length <= 5 * 104", + "tiles[i].length == 2", + "1 <= li <= ri <= 109", + "1 <= carpetLen <= 109", + "The tiles are non-overlapping." + ], + "python_template": "class Solution(object):\n def maximumWhiteTiles(self, tiles, carpetLen):\n \"\"\"\n :type tiles: List[List[int]]\n :type carpetLen: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumWhiteTiles(int[][] tiles, int carpetLen) {\n \n }\n}", + "metadata": { + "func_name": "maximumWhiteTiles" + } +} \ No newline at end of file diff --git a/maximum-xor-after-operations.json b/maximum-xor-after-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..6414d55009e2a5e745334e18574018a3cce31757 --- /dev/null +++ b/maximum-xor-after-operations.json @@ -0,0 +1,29 @@ +{ + "id": 2402, + "name": "maximum-xor-after-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/maximum-xor-after-operations/", + "date": "2022-06-11", + "task_description": "You are given a **0-indexed** integer array `nums`. In one operation, select **any** non-negative integer `x` and an index `i`, then **update** `nums[i]` to be equal to `nums[i] AND (nums[i] XOR x)`. Note that `AND` is the bitwise AND operation and `XOR` is the bitwise XOR operation. Return _the **maximum** possible bitwise XOR of all elements of _`nums`_ after applying the operation **any number** of times_. **Example 1:** ``` **Input:** nums = [3,2,4,6] **Output:** 7 **Explanation:** Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2. Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7. It can be shown that 7 is the maximum possible bitwise XOR. Note that other operations may be used to achieve a bitwise XOR of 7. ``` **Example 2:** ``` **Input:** nums = [1,2,3,9,2] **Output:** 11 **Explanation:** Apply the operation zero times. The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11. It can be shown that 11 is the maximum possible bitwise XOR. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 108`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,2,4,6]", + "output": "7 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,3,9,2]", + "output": "11 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i] <= 108" + ], + "python_template": "class Solution(object):\n def maximumXOR(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int maximumXOR(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "maximumXOR" + } +} \ No newline at end of file diff --git a/maximum-xor-score-subarray-queries.json b/maximum-xor-score-subarray-queries.json new file mode 100644 index 0000000000000000000000000000000000000000..f7689150fdcff784bbb17d96b1a89dde59e4ffea --- /dev/null +++ b/maximum-xor-score-subarray-queries.json @@ -0,0 +1,35 @@ +{ + "id": 3551, + "name": "maximum-xor-score-subarray-queries", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/maximum-xor-score-subarray-queries/", + "date": "2024-08-25", + "task_description": "You are given an array `nums` of `n` integers, and a 2D integer array `queries` of size `q`, where `queries[i] = [li, ri]`. For each query, you must find the **maximum XOR score** of any subarray of `nums[li..ri]`. The **XOR score** of an array `a` is found by repeatedly applying the following operations on `a` so that only one element remains, that is the **score**: Simultaneously replace `a[i]` with `a[i] XOR a[i + 1]` for all indices `i` except the last one. Remove the last element of `a`. Return an array `answer` of size `q` where `answer[i]` is the answer to query `i`. **Example 1:** **Input:** nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]] **Output:** [12,60,60] **Explanation:** In the first query, `nums[0..2]` has 6 subarrays `[2]`, `[8]`, `[4]`, `[2, 8]`, `[8, 4]`, and `[2, 8, 4]` each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores. In the second query, the subarray of `nums[1..4]` with the largest XOR score is `nums[1..4]` with a score of 60. In the third query, the subarray of `nums[0..5]` with the largest XOR score is `nums[1..4]` with a score of 60. **Example 2:** **Input:** nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]] **Output:** [7,14,11,14,5] **Explanation:** Index nums[li..ri] Maximum XOR Score Subarray Maximum Subarray XOR Score 0 [0, 7, 3, 2] [7] 7 1 [7, 3, 2, 8, 5] [7, 3, 2, 8] 14 2 [3, 2, 8] [3, 2, 8] 11 3 [3, 2, 8, 5, 1] [2, 8, 5, 1] 14 4 [5, 1] [5] 5 **Constraints:** `1 <= n == nums.length <= 2000` `0 <= nums[i] <= 231 - 1` `1 <= q == queries.length <= 105` `queries[i].length == 2 ` `queries[i] = [li, ri]` `0 <= li <= ri <= n - 1`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]", + "output": "[12,60,60] " + }, + { + "label": "Example 2", + "input": "nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]", + "output": "[7,14,11,14,5] " + } + ], + "constraints": [ + "Simultaneously replace a[i] with a[i] XOR a[i + 1] for all indices i except the last one.", + "Remove the last element of a.", + "1 <= n == nums.length <= 2000", + "0 <= nums[i] <= 231 - 1", + "1 <= q == queries.length <= 105", + "queries[i].length == 2", + "queries[i] = [li, ri]", + "0 <= li <= ri <= n - 1" + ], + "python_template": "class Solution(object):\n def maximumSubarrayXor(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 int[] maximumSubarrayXor(int[] nums, int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "maximumSubarrayXor" + } +} \ No newline at end of file diff --git a/meeting-rooms-iii.json b/meeting-rooms-iii.json new file mode 100644 index 0000000000000000000000000000000000000000..d12d4f2f74435bc2aab08d0986012eaf806f64ff --- /dev/null +++ b/meeting-rooms-iii.json @@ -0,0 +1,32 @@ +{ + "id": 2479, + "name": "meeting-rooms-iii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/meeting-rooms-iii/", + "date": "2022-08-28", + "task_description": "You are given an integer `n`. There are `n` rooms numbered from `0` to `n - 1`. You are given a 2D integer array `meetings` where `meetings[i] = [starti, endi]` means that a meeting will be held during the **half-closed** time interval `[starti, endi)`. All the values of `starti` are **unique**. Meetings are allocated to rooms in the following manner: Each meeting will take place in the unused room with the **lowest** number. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the **same** duration as the original meeting. When a room becomes unused, meetings that have an earlier original **start** time should be given the room. Return_ the **number** of the room that held the most meetings. _If there are multiple rooms, return_ the room with the **lowest** number._ A **half-closed interval** `[a, b)` is the interval between `a` and `b` **including** `a` and **not including** `b`. **Example 1:** ``` **Input:** n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]] **Output:** 0 **Explanation:** - At time 0, both rooms are not being used. The first meeting starts in room 0. - At time 1, only room 1 is not being used. The second meeting starts in room 1. - At time 2, both rooms are being used. The third meeting is delayed. - At time 3, both rooms are being used. The fourth meeting is delayed. - At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10). - At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11). Both rooms 0 and 1 held 2 meetings, so we return 0. ``` **Example 2:** ``` **Input:** n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]] **Output:** 1 **Explanation:** - At time 1, all three rooms are not being used. The first meeting starts in room 0. - At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1. - At time 3, only room 2 is not being used. The third meeting starts in room 2. - At time 4, all three rooms are being used. The fourth meeting is delayed. - At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10). - At time 6, all three rooms are being used. The fifth meeting is delayed. - At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12). Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1. ``` **Constraints:** `1 <= n <= 100` `1 <= meetings.length <= 105` `meetings[i].length == 2` `0 <= starti < endi <= 5 * 105` All the values of `starti` are **unique**.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]", + "output": "0 " + }, + { + "label": "Example 2", + "input": "n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]", + "output": "1 " + } + ], + "constraints": [ + "1 <= n <= 100", + "1 <= meetings.length <= 105", + "meetings[i].length == 2", + "0 <= starti < endi <= 5 * 105", + "All the values of starti are unique." + ], + "python_template": "class Solution(object):\n def mostBooked(self, n, meetings):\n \"\"\"\n :type n: int\n :type meetings: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int mostBooked(int n, int[][] meetings) {\n \n }\n}", + "metadata": { + "func_name": "mostBooked" + } +} \ No newline at end of file diff --git a/merge-similar-items.json b/merge-similar-items.json new file mode 100644 index 0000000000000000000000000000000000000000..7834a0ffc3685f16ece0c9e61b0d7a719af91104 --- /dev/null +++ b/merge-similar-items.json @@ -0,0 +1,39 @@ +{ + "id": 2447, + "name": "merge-similar-items", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/merge-similar-items/", + "date": "2022-07-23", + "task_description": "You are given two 2D integer arrays, `items1` and `items2`, representing two sets of items. Each array `items` has the following properties: `items[i] = [valuei, weighti]` where `valuei` represents the **value** and `weighti` represents the **weight **of the `ith` item. The value of each item in `items` is **unique**. Return _a 2D integer array_ `ret` _where_ `ret[i] = [valuei, weighti]`_,_ _with_ `weighti` _being the **sum of weights** of all items with value_ `valuei`. **Note:** `ret` should be returned in **ascending** order by value. **Example 1:** ``` **Input:** items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]] **Output:** [[1,6],[3,9],[4,5]] **Explanation:** The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6. The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9. The item with value = 4 occurs in items1 with weight = 5, total weight = 5. Therefore, we return [[1,6],[3,9],[4,5]]. ``` **Example 2:** ``` **Input:** items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]] **Output:** [[1,4],[2,4],[3,4]] **Explanation:** The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4. The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4. The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4. Therefore, we return [[1,4],[2,4],[3,4]]. ``` **Example 3:** ``` **Input:** items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]] **Output:** [[1,7],[2,4],[7,1]] **Explanation: **The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7. The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4. The item with value = 7 occurs in items2 with weight = 1, total weight = 1. Therefore, we return [[1,7],[2,4],[7,1]]. ``` **Constraints:** `1 <= items1.length, items2.length <= 1000` `items1[i].length == items2[i].length == 2` `1 <= valuei, weighti <= 1000` Each `valuei` in `items1` is **unique**. Each `valuei` in `items2` is **unique**.", + "test_case": [ + { + "label": "Example 1", + "input": "items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]", + "output": "[[1,6],[3,9],[4,5]] " + }, + { + "label": "Example 2", + "input": "items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]", + "output": "[[1,4],[2,4],[3,4]] " + }, + { + "label": "Example 3", + "input": "items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]", + "output": "[[1,7],[2,4],[7,1]] " + } + ], + "constraints": [ + "items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the ith item.", + "The value of each item in items is unique.", + "1 <= items1.length, items2.length <= 1000", + "items1[i].length == items2[i].length == 2", + "1 <= valuei, weighti <= 1000", + "Each valuei in items1 is unique.", + "Each valuei in items2 is unique." + ], + "python_template": "class Solution(object):\n def mergeSimilarItems(self, items1, items2):\n \"\"\"\n :type items1: List[List[int]]\n :type items2: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public List> mergeSimilarItems(int[][] items1, int[][] items2) {\n \n }\n}", + "metadata": { + "func_name": "mergeSimilarItems" + } +} \ No newline at end of file diff --git a/merge-two-2d-arrays-by-summing-values.json b/merge-two-2d-arrays-by-summing-values.json new file mode 100644 index 0000000000000000000000000000000000000000..426c24aa86bfdec503f392815cf576b9964526ce --- /dev/null +++ b/merge-two-2d-arrays-by-summing-values.json @@ -0,0 +1,36 @@ +{ + "id": 2707, + "name": "merge-two-2d-arrays-by-summing-values", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/", + "date": "2023-02-12", + "task_description": "You are given two **2D** integer arrays `nums1` and `nums2.` `nums1[i] = [idi, vali]` indicate that the number with the id `idi` has a value equal to `vali`. `nums2[i] = [idi, vali]` indicate that the number with the id `idi` has a value equal to `vali`. Each array contains **unique** ids and is sorted in **ascending** order by id. Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions: Only ids that appear in at least one of the two arrays should be included in the resulting array. Each id should be included **only once** and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be `0`. Return _the resulting array_. The returned array must be sorted in ascending order by id. **Example 1:** ``` **Input:** nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]] **Output:** [[1,6],[2,3],[3,2],[4,6]] **Explanation:** The resulting array contains the following: - id = 1, the value of this id is 2 + 4 = 6. - id = 2, the value of this id is 3. - id = 3, the value of this id is 2. - id = 4, the value of this id is 5 + 1 = 6. ``` **Example 2:** ``` **Input:** nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]] **Output:** [[1,3],[2,4],[3,6],[4,3],[5,5]] **Explanation:** There are no common ids, so we just include each id with its value in the resulting list. ``` **Constraints:** `1 <= nums1.length, nums2.length <= 200` `nums1[i].length == nums2[j].length == 2` `1 <= idi, vali <= 1000` Both arrays contain unique ids. Both arrays are in strictly ascending order by id.", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]", + "output": "[[1,6],[2,3],[3,2],[4,6]] " + }, + { + "label": "Example 2", + "input": "nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]", + "output": "[[1,3],[2,4],[3,6],[4,3],[5,5]] " + } + ], + "constraints": [ + "nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.", + "nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.", + "Only ids that appear in at least one of the two arrays should be included in the resulting array.", + "Each id should be included only once and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be 0.", + "1 <= nums1.length, nums2.length <= 200", + "nums1[i].length == nums2[j].length == 2", + "1 <= idi, vali <= 1000", + "Both arrays contain unique ids.", + "Both arrays are in strictly ascending order by id." + ], + "python_template": "class Solution(object):\n def mergeArrays(self, nums1, nums2):\n \"\"\"\n :type nums1: List[List[int]]\n :type nums2: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[][] mergeArrays(int[][] nums1, int[][] nums2) {\n \n }\n}", + "metadata": { + "func_name": "mergeArrays" + } +} \ No newline at end of file diff --git a/mice-and-cheese.json b/mice-and-cheese.json new file mode 100644 index 0000000000000000000000000000000000000000..3a1452439efbae77c3ca943bdf8d9ec58c82bc6f --- /dev/null +++ b/mice-and-cheese.json @@ -0,0 +1,32 @@ +{ + "id": 2725, + "name": "mice-and-cheese", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/mice-and-cheese/", + "date": "2023-03-26", + "task_description": "There are two mice and `n` different types of cheese, each type of cheese should be eaten by exactly one mouse. A point of the cheese with index `i` (**0-indexed**) is: `reward1[i]` if the first mouse eats it. `reward2[i]` if the second mouse eats it. You are given a positive integer array `reward1`, a positive integer array `reward2`, and a non-negative integer `k`. Return _**the maximum** points the mice can achieve if the first mouse eats exactly _`k`_ types of cheese._ **Example 1:** ``` **Input:** reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2 **Output:** 15 **Explanation:** In this example, the first mouse eats the 2nd (0-indexed) and the 3rd types of cheese, and the second mouse eats the 0th and the 1st types of cheese. The total points are 4 + 4 + 3 + 4 = 15. It can be proven that 15 is the maximum total points that the mice can achieve. ``` **Example 2:** ``` **Input:** reward1 = [1,1], reward2 = [1,1], k = 2 **Output:** 2 **Explanation:** In this example, the first mouse eats the 0th (0-indexed) and 1st types of cheese, and the second mouse does not eat any cheese. The total points are 1 + 1 = 2. It can be proven that 2 is the maximum total points that the mice can achieve. ``` **Constraints:** `1 <= n == reward1.length == reward2.length <= 105` `1 <= reward1[i], reward2[i] <= 1000` `0 <= k <= n`", + "test_case": [ + { + "label": "Example 1", + "input": "reward1 = [1,1,3,4], reward2 = [4,4,1,1], k = 2", + "output": "15 " + }, + { + "label": "Example 2", + "input": "reward1 = [1,1], reward2 = [1,1], k = 2", + "output": "2 " + } + ], + "constraints": [ + "reward1[i] if the first mouse eats it.", + "reward2[i] if the second mouse eats it.", + "1 <= n == reward1.length == reward2.length <= 105", + "1 <= reward1[i], reward2[i] <= 1000", + "0 <= k <= n" + ], + "python_template": "class Solution(object):\n def miceAndCheese(self, reward1, reward2, k):\n \"\"\"\n :type reward1: List[int]\n :type reward2: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int miceAndCheese(int[] reward1, int[] reward2, int k) {\n \n }\n}", + "metadata": { + "func_name": "miceAndCheese" + } +} \ No newline at end of file diff --git a/min-max-game.json b/min-max-game.json new file mode 100644 index 0000000000000000000000000000000000000000..342caadf4e02eaac382415e02a1102b8da7c9fc7 --- /dev/null +++ b/min-max-game.json @@ -0,0 +1,30 @@ +{ + "id": 2386, + "name": "min-max-game", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/min-max-game/", + "date": "2022-05-29", + "task_description": "You are given a **0-indexed** integer array `nums` whose length is a power of `2`. Apply the following algorithm on `nums`: Let `n` be the length of `nums`. If `n == 1`, **end** the process. Otherwise, **create** a new **0-indexed** integer array `newNums` of length `n / 2`. For every **even** index `i` where `0 <= i < n / 2`, **assign** the value of `newNums[i]` as `min(nums[2 * i], nums[2 * i + 1])`. For every **odd** index `i` where `0 <= i < n / 2`, **assign** the value of `newNums[i]` as `max(nums[2 * i], nums[2 * i + 1])`. **Replace** the array `nums` with `newNums`. **Repeat** the entire process starting from step 1. Return _the last number that remains in _`nums`_ after applying the algorithm._ **Example 1:** ``` **Input:** nums = [1,3,5,2,4,8,2,2] **Output:** 1 **Explanation:** The following arrays are the results of applying the algorithm repeatedly. First: nums = [1,5,4,2] Second: nums = [1,4] Third: nums = [1] 1 is the last remaining number, so we return 1. ``` **Example 2:** ``` **Input:** nums = [3] **Output:** 3 **Explanation:** 3 is already the last remaining number, so we return 3. ``` **Constraints:** `1 <= nums.length <= 1024` `1 <= nums[i] <= 109` `nums.length` is a power of `2`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,5,2,4,8,2,2]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [3]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 1024", + "1 <= nums[i] <= 109", + "nums.length is a power of 2." + ], + "python_template": "class Solution(object):\n def minMaxGame(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minMaxGame(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minMaxGame" + } +} \ No newline at end of file diff --git a/minimize-length-of-array-using-operations.json b/minimize-length-of-array-using-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..eb887cbeac8ab58fdbfe054344cd98e4a1a17117 --- /dev/null +++ b/minimize-length-of-array-using-operations.json @@ -0,0 +1,37 @@ +{ + "id": 3244, + "name": "minimize-length-of-array-using-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimize-length-of-array-using-operations/", + "date": "2024-01-06", + "task_description": "You are given a **0-indexed** integer array `nums` containing **positive** integers. Your task is to **minimize** the length of `nums` by performing the following operations **any** number of times (including zero): Select **two** **distinct** indices `i` and `j` from `nums`, such that `nums[i] > 0` and `nums[j] > 0`. Insert the result of `nums[i] % nums[j]` at the end of `nums`. Delete the elements at indices `i` and `j` from `nums`. Return _an integer denoting the **minimum** **length** of _`nums`_ after performing the operation any number of times._ **Example 1:** ``` **Input:** nums = [1,4,3,1] **Output:** 1 **Explanation:** One way to minimize the length of the array is as follows: Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1. nums becomes [1,1,3]. Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2. nums becomes [1,1]. Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0. nums becomes [0]. The length of nums cannot be reduced further. Hence, the answer is 1. It can be shown that 1 is the minimum achievable length. ``` **Example 2:** ``` **Input:** nums = [5,5,5,10,5] **Output:** 2 **Explanation:** One way to minimize the length of the array is as follows: Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3. nums becomes [5,5,5,5]. Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3. nums becomes [5,5,0]. Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1. nums becomes [0,0]. The length of nums cannot be reduced further. Hence, the answer is 2. It can be shown that 2 is the minimum achievable length. ``` **Example 3:** ``` **Input:** nums = [2,3,4] **Output:** 1 **Explanation:** One way to minimize the length of the array is as follows: Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2. nums becomes [2,3]. Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0. nums becomes [1]. The length of nums cannot be reduced further. Hence, the answer is 1. It can be shown that 1 is the minimum achievable length. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,4,3,1]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [5,5,5,10,5]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [2,3,4]", + "output": "1 " + } + ], + "constraints": [ + "Select two distinct indices i and j from nums, such that nums[i] > 0 and nums[j] > 0.", + "Insert the result of nums[i] % nums[j] at the end of nums.", + "Delete the elements at indices i and j from nums.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minimumArrayLength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumArrayLength(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumArrayLength" + } +} \ No newline at end of file diff --git a/minimize-manhattan-distances.json b/minimize-manhattan-distances.json new file mode 100644 index 0000000000000000000000000000000000000000..20776999822dabd539cfa19629d2599e7bc5a8fe --- /dev/null +++ b/minimize-manhattan-distances.json @@ -0,0 +1,34 @@ +{ + "id": 3344, + "name": "minimize-manhattan-distances", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimize-manhattan-distances/", + "date": "2024-03-24", + "task_description": "You are given an array `points` representing integer coordinates of some points on a 2D plane, where `points[i] = [xi, yi]`. The distance between two points is defined as their Manhattan distance. Return _the **minimum** possible value for **maximum** distance between any two points by removing exactly one point_. **Example 1:** **Input:** points = [[3,10],[5,15],[10,2],[4,4]] **Output:** 12 **Explanation:** The maximum distance after removing each point is the following: After removing the 0th point the maximum distance is between points (5, 15) and (10, 2), which is `|5 - 10| + |15 - 2| = 18`. After removing the 1st point the maximum distance is between points (3, 10) and (10, 2), which is `|3 - 10| + |10 - 2| = 15`. After removing the 2nd point the maximum distance is between points (5, 15) and (4, 4), which is `|5 - 4| + |15 - 4| = 12`. After removing the 3rd point the maximum distance is between points (5, 15) and (10, 2), which is `|5 - 10| + |15 - 2| = 18`. 12 is the minimum possible maximum distance between any two points after removing exactly one point. **Example 2:** **Input:** points = [[1,1],[1,1],[1,1]] **Output:** 0 **Explanation:** Removing any of the points results in the maximum distance between any two points of 0. **Constraints:** `3 <= points.length <= 105` `points[i].length == 2` `1 <= points[i][0], points[i][1] <= 108`", + "test_case": [ + { + "label": "Example 1", + "input": "points = [[3,10],[5,15],[10,2],[4,4]]", + "output": "12 " + }, + { + "label": "Example 2", + "input": "points = [[1,1],[1,1],[1,1]]", + "output": "0 " + } + ], + "constraints": [ + "After removing the 0th point the maximum distance is between points (5, 15) and (10, 2), which is |5 - 10| + |15 - 2| = 18.", + "After removing the 1st point the maximum distance is between points (3, 10) and (10, 2), which is |3 - 10| + |10 - 2| = 15.", + "After removing the 2nd point the maximum distance is between points (5, 15) and (4, 4), which is |5 - 4| + |15 - 4| = 12.", + "After removing the 3rd point the maximum distance is between points (5, 15) and (10, 2), which is |5 - 10| + |15 - 2| = 18.", + "3 <= points.length <= 105", + "points[i].length == 2", + "1 <= points[i][0], points[i][1] <= 108" + ], + "python_template": "class Solution(object):\n def minimumDistance(self, points):\n \"\"\"\n :type points: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumDistance(int[][] points) {\n \n }\n}", + "metadata": { + "func_name": "minimumDistance" + } +} \ No newline at end of file diff --git a/minimize-maximum-of-array.json b/minimize-maximum-of-array.json new file mode 100644 index 0000000000000000000000000000000000000000..599c49681043d55ebf565f8c47003eb00a06c48d --- /dev/null +++ b/minimize-maximum-of-array.json @@ -0,0 +1,33 @@ +{ + "id": 2530, + "name": "minimize-maximum-of-array", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimize-maximum-of-array/", + "date": "2022-10-01", + "task_description": "You are given a **0-indexed** array `nums` comprising of `n` non-negative integers. In one operation, you must: Choose an integer `i` such that `1 <= i < n` and `nums[i] > 0`. Decrease `nums[i]` by 1. Increase `nums[i - 1]` by 1. Return_ the **minimum** possible value of the **maximum** integer of _`nums`_ after performing **any** number of operations_. **Example 1:** ``` **Input:** nums = [3,7,1,6] **Output:** 5 **Explanation:** One set of optimal operations is as follows: 1. Choose i = 1, and nums becomes [4,6,1,6]. 2. Choose i = 3, and nums becomes [4,6,2,5]. 3. Choose i = 1, and nums becomes [5,5,2,5]. The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5. Therefore, we return 5. ``` **Example 2:** ``` **Input:** nums = [10,1] **Output:** 10 **Explanation:** It is optimal to leave nums as is, and since 10 is the maximum value, we return 10. ``` **Constraints:** `n == nums.length` `2 <= n <= 105` `0 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,7,1,6]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "nums = [10,1]", + "output": "10 " + } + ], + "constraints": [ + "Choose an integer i such that 1 <= i < n and nums[i] > 0.", + "Decrease nums[i] by 1.", + "Increase nums[i - 1] by 1.", + "n == nums.length", + "2 <= n <= 105", + "0 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minimizeArrayValue(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimizeArrayValue(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimizeArrayValue" + } +} \ No newline at end of file diff --git a/minimize-or-of-remaining-elements-using-operations.json b/minimize-or-of-remaining-elements-using-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..cabeaa44364fda946e58cc16726983ad74af9726 --- /dev/null +++ b/minimize-or-of-remaining-elements-using-operations.json @@ -0,0 +1,35 @@ +{ + "id": 3261, + "name": "minimize-or-of-remaining-elements-using-operations", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimize-or-of-remaining-elements-using-operations/", + "date": "2024-01-21", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `k`. In one operation, you can pick any index `i` of `nums` such that `0 <= i < nums.length - 1` and replace `nums[i]` and `nums[i + 1]` with a single occurrence of `nums[i] & nums[i + 1]`, where `&` represents the bitwise `AND` operator. Return _the **minimum** possible value of the bitwise _`OR`_ of the remaining elements of_ `nums` _after applying **at most**_ `k` _operations_. **Example 1:** ``` **Input:** nums = [3,5,3,2,7], k = 2 **Output:** 3 **Explanation:** Let's do the following operations: 1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [1,3,2,7]. 2. Replace nums[2] and nums[3] with (nums[2] & nums[3]) so that nums becomes equal to [1,3,2]. The bitwise-or of the final array is 3. It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations. ``` **Example 2:** ``` **Input:** nums = [7,3,15,14,2,8], k = 4 **Output:** 2 **Explanation:** Let's do the following operations: 1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,15,14,2,8]. 2. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,14,2,8]. 3. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [2,2,8]. 4. Replace nums[1] and nums[2] with (nums[1] & nums[2]) so that nums becomes equal to [2,0]. The bitwise-or of the final array is 2. It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations. ``` **Example 3:** ``` **Input:** nums = [10,7,10,3,9,14,9,4], k = 1 **Output:** 15 **Explanation:** Without applying any operations, the bitwise-or of nums is 15. It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] < 230` `0 <= k < nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,5,3,2,7], k = 2", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [7,3,15,14,2,8], k = 4", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [10,7,10,3,9,14,9,4], k = 1", + "output": "15 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i] < 230", + "0 <= k < nums.length" + ], + "python_template": "class Solution(object):\n def minOrAfterOperations(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 minOrAfterOperations(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOrAfterOperations" + } +} \ No newline at end of file diff --git a/minimize-result-by-adding-parentheses-to-expression.json b/minimize-result-by-adding-parentheses-to-expression.json new file mode 100644 index 0000000000000000000000000000000000000000..13ef8ac9c643d53a3a9df65492930b881b7cd0f4 --- /dev/null +++ b/minimize-result-by-adding-parentheses-to-expression.json @@ -0,0 +1,37 @@ +{ + "id": 2328, + "name": "minimize-result-by-adding-parentheses-to-expression", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimize-result-by-adding-parentheses-to-expression/", + "date": "2022-04-03", + "task_description": "You are given a **0-indexed** string `expression` of the form `\"+\"` where `` and `` represent positive integers. Add a pair of parentheses to `expression` such that after the addition of parentheses, `expression` is a **valid** mathematical expression and evaluates to the **smallest** possible value. The left parenthesis **must** be added to the left of `'+'` and the right parenthesis **must** be added to the right of `'+'`. Return `expression`_ after adding a pair of parentheses such that _`expression`_ evaluates to the **smallest** possible value._ If there are multiple answers that yield the same result, return any of them. The input has been generated such that the original value of `expression`, and the value of `expression` after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer. **Example 1:** ``` **Input:** expression = \"247+38\" **Output:** \"2(47+38)\" **Explanation:** The `expression` evaluates to 2 * (47 + 38) = 2 * 85 = 170. Note that \"2(4)7+38\" is invalid because the right parenthesis must be to the right of the `'+'`. It can be shown that 170 is the smallest possible value. ``` **Example 2:** ``` **Input:** expression = \"12+34\" **Output:** \"1(2+3)4\" **Explanation:** The expression evaluates to 1 * (2 + 3) * 4 = 1 * 5 * 4 = 20. ``` **Example 3:** ``` **Input:** expression = \"999+999\" **Output:** \"(999+999)\" **Explanation:** The `expression` evaluates to 999 + 999 = 1998. ``` **Constraints:** `3 <= expression.length <= 10` `expression` consists of digits from `'1'` to `'9'` and `'+'`. `expression` starts and ends with digits. `expression` contains exactly one `'+'`. The original value of `expression`, and the value of `expression` after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.", + "test_case": [ + { + "label": "Example 1", + "input": "expression = \"247+38\"", + "output": "\"2(47+38)\" " + }, + { + "label": "Example 2", + "input": "expression = \"12+34\"", + "output": "\"1(2+3)4\" " + }, + { + "label": "Example 3", + "input": "expression = \"999+999\"", + "output": "\"(999+999)\" " + } + ], + "constraints": [ + "3 <= expression.length <= 10", + "expression consists of digits from '1' to '9' and '+'.", + "expression starts and ends with digits.", + "expression contains exactly one '+'.", + "The original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer." + ], + "python_template": "class Solution(object):\n def minimizeResult(self, expression):\n \"\"\"\n :type expression: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String minimizeResult(String expression) {\n \n }\n}", + "metadata": { + "func_name": "minimizeResult" + } +} \ No newline at end of file diff --git a/minimize-string-length.json b/minimize-string-length.json new file mode 100644 index 0000000000000000000000000000000000000000..4aab1a4afe16a3b80c315ef4260a14d7ea4e5717 --- /dev/null +++ b/minimize-string-length.json @@ -0,0 +1,34 @@ +{ + "id": 2825, + "name": "minimize-string-length", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimize-string-length/", + "date": "2023-05-28", + "task_description": "Given a string `s`, you have two types of operation: Choose an index `i` in the string, and let `c` be the character in position `i`. **Delete** the **closest occurrence** of `c` to the **left** of `i` (if exists). Choose an index `i` in the string, and let `c` be the character in position `i`. **Delete** the **closest occurrence** of `c` to the **right** of `i` (if exists). Your task is to **minimize** the length of `s` by performing the above operations zero or more times. Return an integer denoting the length of the **minimized** string. **Example 1:** **Input:** s = \"aaabc\" **Output:** 3 **Explanation:** Operation 2: we choose `i = 1` so `c` is 'a', then we remove `s[2]` as it is closest 'a' character to the right of `s[1]`. `s` becomes \"aabc\" after this. Operation 1: we choose `i = 1` so `c` is 'a', then we remove `s[0]` as it is closest 'a' character to the left of `s[1]`. `s` becomes \"abc\" after this. **Example 2:** **Input:** s = \"cbbd\" **Output:** 3 **Explanation:** Operation 1: we choose `i = 2` so `c` is 'b', then we remove `s[1]` as it is closest 'b' character to the left of `s[1]`. `s` becomes \"cbd\" after this. **Example 3:** **Input:** s = \"baadccab\" **Output:** 4 **Explanation:** Operation 1: we choose `i = 6` so `c` is 'a', then we remove `s[2]` as it is closest 'a' character to the left of `s[6]`. `s` becomes \"badccab\" after this. Operation 2: we choose `i = 0` so `c` is 'b', then we remove `s[6]` as it is closest 'b' character to the right of `s[0]`. `s` becomes \"badcca\" fter this. Operation 2: we choose `i = 3` so `c` is 'c', then we remove `s[4]` as it is closest 'c' character to the right of `s[3]`. `s` becomes \"badca\" after this. Operation 1: we choose `i = 4` so `c` is 'a', then we remove `s[1]` as it is closest 'a' character to the left of `s[4]`. `s` becomes \"bdca\" after this. **Constraints:** `1 <= s.length <= 100` `s` contains only lowercase English letters", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"aaabc\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "s = \"cbbd\"", + "output": "3 " + }, + { + "label": "Example 3", + "input": "s = \"baadccab\"", + "output": "4 " + } + ], + "constraints": [ + "1 <= s.length <= 100", + "s contains only lowercase English letters" + ], + "python_template": "class Solution(object):\n def minimizedStringLength(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimizedStringLength(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimizedStringLength" + } +} \ No newline at end of file diff --git a/minimize-the-maximum-adjacent-element-difference.json b/minimize-the-maximum-adjacent-element-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..86cd09e9eb22883e088839fb161314cb685e0e1b --- /dev/null +++ b/minimize-the-maximum-adjacent-element-difference.json @@ -0,0 +1,38 @@ +{ + "id": 3658, + "name": "minimize-the-maximum-adjacent-element-difference", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimize-the-maximum-adjacent-element-difference/", + "date": "2024-11-10", + "task_description": "You are given an array of integers `nums`. Some values in `nums` are **missing** and are denoted by -1. You can choose a pair of **positive** integers `(x, y)` **exactly once** and replace each **missing** element with _either_ `x` or `y`. You need to **minimize**** **the** maximum** **absolute difference** between _adjacent_ elements of `nums` after replacements. Return the **minimum** possible difference. **Example 1:** **Input:** nums = [1,2,-1,10,8] **Output:** 4 **Explanation:** By choosing the pair as `(6, 7)`, nums can be changed to `[1, 2, 6, 10, 8]`. The absolute differences between adjacent elements are: `|1 - 2| == 1` `|2 - 6| == 4` `|6 - 10| == 4` `|10 - 8| == 2` **Example 2:** **Input:** nums = [-1,-1,-1] **Output:** 0 **Explanation:** By choosing the pair as `(4, 4)`, nums can be changed to `[4, 4, 4]`. **Example 3:** **Input:** nums = [-1,10,-1,8] **Output:** 1 **Explanation:** By choosing the pair as `(11, 9)`, nums can be changed to `[11, 10, 9, 8]`. **Constraints:** `2 <= nums.length <= 105` `nums[i]` is either -1 or in the range `[1, 109]`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,-1,10,8]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [-1,-1,-1]", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [-1,10,-1,8]", + "output": "1 " + } + ], + "constraints": [ + "|1 - 2| == 1", + "|2 - 6| == 4", + "|6 - 10| == 4", + "|10 - 8| == 2", + "2 <= nums.length <= 105", + "nums[i] is either -1 or in the range [1, 109]." + ], + "python_template": "class Solution(object):\n def minDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minDifference(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minDifference" + } +} \ No newline at end of file diff --git a/minimize-the-maximum-difference-of-pairs.json b/minimize-the-maximum-difference-of-pairs.json new file mode 100644 index 0000000000000000000000000000000000000000..6ed5315939de21b974d7d735f03cc120b6319024 --- /dev/null +++ b/minimize-the-maximum-difference-of-pairs.json @@ -0,0 +1,30 @@ +{ + "id": 2720, + "name": "minimize-the-maximum-difference-of-pairs", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/", + "date": "2023-04-02", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `p`. Find `p` pairs of indices of `nums` such that the **maximum** difference amongst all the pairs is **minimized**. Also, ensure no index appears more than once amongst the `p` pairs. Note that for a pair of elements at the index `i` and `j`, the difference of this pair is `|nums[i] - nums[j]|`, where `|x|` represents the **absolute** **value** of `x`. Return _the **minimum** **maximum** difference among all _`p` _pairs._ We define the maximum of an empty set to be zero. **Example 1:** ``` **Input:** nums = [10,1,2,7,1,3], p = 2 **Output:** 1 **Explanation:** The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5. The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1. ``` **Example 2:** ``` **Input:** nums = [4,2,1,2], p = 1 **Output:** 0 **Explanation:** Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 109` `0 <= p <= (nums.length)/2`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [10,1,2,7,1,3], p = 2", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [4,2,1,2], p = 1", + "output": "0 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "0 <= nums[i] <= 109", + "0 <= p <= (nums.length)/2" + ], + "python_template": "class Solution(object):\n def minimizeMax(self, nums, p):\n \"\"\"\n :type nums: List[int]\n :type p: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimizeMax(int[] nums, int p) {\n \n }\n}", + "metadata": { + "func_name": "minimizeMax" + } +} \ No newline at end of file diff --git a/minimize-the-maximum-of-two-arrays.json b/minimize-the-maximum-of-two-arrays.json new file mode 100644 index 0000000000000000000000000000000000000000..85d33610b8f7e9f697cd7666876a9179a03c48b0 --- /dev/null +++ b/minimize-the-maximum-of-two-arrays.json @@ -0,0 +1,38 @@ +{ + "id": 2628, + "name": "minimize-the-maximum-of-two-arrays", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimize-the-maximum-of-two-arrays/", + "date": "2022-12-10", + "task_description": "We have two arrays `arr1` and `arr2` which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions: `arr1` contains `uniqueCnt1` **distinct** positive integers, each of which is **not divisible** by `divisor1`. `arr2` contains `uniqueCnt2` **distinct** positive integers, each of which is **not divisible** by `divisor2`. **No** integer is present in both `arr1` and `arr2`. Given `divisor1`, `divisor2`, `uniqueCnt1`, and `uniqueCnt2`, return _the **minimum possible maximum** integer that can be present in either array_. **Example 1:** ``` **Input:** divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3 **Output:** 4 **Explanation:** We can distribute the first 4 natural numbers into arr1 and arr2. arr1 = [1] and arr2 = [2,3,4]. We can see that both arrays satisfy all the conditions. Since the maximum value is 4, we return it. ``` **Example 2:** ``` **Input:** divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1 **Output:** 3 **Explanation:** Here arr1 = [1,2], and arr2 = [3] satisfy all conditions. Since the maximum value is 3, we return it. ``` **Example 3:** ``` **Input:** divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2 **Output:** 15 **Explanation:** Here, the final possible arrays can be arr1 = [1,3,5,7,9,11,13,15], and arr2 = [2,6]. It can be shown that it is not possible to obtain a lower maximum satisfying all conditions. ``` **Constraints:** `2 <= divisor1, divisor2 <= 105` `1 <= uniqueCnt1, uniqueCnt2 < 109` `2 <= uniqueCnt1 + uniqueCnt2 <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3", + "output": "4 " + }, + { + "label": "Example 2", + "input": "divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1", + "output": "3 " + }, + { + "label": "Example 3", + "input": "divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2", + "output": "15 " + } + ], + "constraints": [ + "arr1 contains uniqueCnt1 distinct positive integers, each of which is not divisible by divisor1.", + "arr2 contains uniqueCnt2 distinct positive integers, each of which is not divisible by divisor2.", + "No integer is present in both arr1 and arr2.", + "2 <= divisor1, divisor2 <= 105", + "1 <= uniqueCnt1, uniqueCnt2 < 109", + "2 <= uniqueCnt1 + uniqueCnt2 <= 109" + ], + "python_template": "class Solution(object):\n def minimizeSet(self, divisor1, divisor2, uniqueCnt1, uniqueCnt2):\n \"\"\"\n :type divisor1: int\n :type divisor2: int\n :type uniqueCnt1: int\n :type uniqueCnt2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {\n \n }\n}", + "metadata": { + "func_name": "minimizeSet" + } +} \ No newline at end of file diff --git a/minimize-the-total-price-of-the-trips.json b/minimize-the-total-price-of-the-trips.json new file mode 100644 index 0000000000000000000000000000000000000000..1e5d8d80f5b479be6bf25914218dfddf80e41042 --- /dev/null +++ b/minimize-the-total-price-of-the-trips.json @@ -0,0 +1,36 @@ +{ + "id": 2739, + "name": "minimize-the-total-price-of-the-trips", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimize-the-total-price-of-the-trips/", + "date": "2023-04-09", + "task_description": "There exists an undirected and unrooted tree with `n` nodes indexed from `0` to `n - 1`. You are given the integer `n` and a 2D integer array `edges` of length `n - 1`, where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree. Each node has an associated price. You are given an integer array `price`, where `price[i]` is the price of the `ith` node. The **price sum** of a given path is the sum of the prices of all nodes lying on that path. Additionally, you are given a 2D integer array `trips`, where `trips[i] = [starti, endi]` indicates that you start the `ith` trip from the node `starti` and travel to the node `endi` by any path you like. Before performing your first trip, you can choose some **non-adjacent** nodes and halve the prices. Return _the minimum total price sum to perform all the given trips_. **Example 1:** ``` **Input:** n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]] **Output:** 23 **Explanation:** The diagram above denotes the tree after rooting it at node 2. The first part shows the initial tree and the second part shows the tree after choosing nodes 0, 2, and 3, and making their price half. For the 1st trip, we choose path [0,1,3]. The price sum of that path is 1 + 2 + 3 = 6. For the 2nd trip, we choose path [2,1]. The price sum of that path is 2 + 5 = 7. For the 3rd trip, we choose path [2,1,3]. The price sum of that path is 5 + 2 + 3 = 10. The total price sum of all trips is 6 + 7 + 10 = 23. It can be proven, that 23 is the minimum answer that we can achieve. ``` **Example 2:** ``` **Input:** n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]] **Output:** 1 **Explanation:** The diagram above denotes the tree after rooting it at node 0. The first part shows the initial tree and the second part shows the tree after choosing node 0, and making its price half. For the 1st trip, we choose path [0]. The price sum of that path is 1. The total price sum of all trips is 1. It can be proven, that 1 is the minimum answer that we can achieve. ``` **Constraints:** `1 <= n <= 50` `edges.length == n - 1` `0 <= ai, bi <= n - 1` `edges` represents a valid tree. `price.length == n` `price[i]` is an even integer. `1 <= price[i] <= 1000` `1 <= trips.length <= 100` `0 <= starti, endi <= n - 1`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]]", + "output": "23 " + }, + { + "label": "Example 2", + "input": "n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]]", + "output": "1 " + } + ], + "constraints": [ + "1 <= n <= 50", + "edges.length == n - 1", + "0 <= ai, bi <= n - 1", + "edges represents a valid tree.", + "price.length == n", + "price[i] is an even integer.", + "1 <= price[i] <= 1000", + "1 <= trips.length <= 100", + "0 <= starti, endi <= n - 1" + ], + "python_template": "class Solution(object):\n def minimumTotalPrice(self, n, edges, price, trips):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type price: List[int]\n :type trips: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumTotalPrice(int n, int[][] edges, int[] price, int[][] trips) {\n \n }\n}", + "metadata": { + "func_name": "minimumTotalPrice" + } +} \ No newline at end of file diff --git a/minimize-xor.json b/minimize-xor.json new file mode 100644 index 0000000000000000000000000000000000000000..1e006e81d17b7b19f8e5225a1d2a2ccd55f87804 --- /dev/null +++ b/minimize-xor.json @@ -0,0 +1,30 @@ +{ + "id": 2509, + "name": "minimize-xor", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimize-xor/", + "date": "2022-09-25", + "task_description": "Given two positive integers `num1` and `num2`, find the positive integer `x` such that: `x` has the same number of set bits as `num2`, and The value `x XOR num1` is **minimal**. Note that `XOR` is the bitwise XOR operation. Return _the integer _`x`. The test cases are generated such that `x` is **uniquely determined**. The number of **set bits** of an integer is the number of `1`'s in its binary representation. **Example 1:** ``` **Input:** num1 = 3, num2 = 5 **Output:** 3 **Explanation:** The binary representations of num1 and num2 are 0011 and 0101, respectively. The integer **3** has the same number of set bits as num2, and the value `3 XOR 3 = 0` is minimal. ``` **Example 2:** ``` **Input:** num1 = 1, num2 = 12 **Output:** 3 **Explanation:** The binary representations of num1 and num2 are 0001 and 1100, respectively. The integer **3** has the same number of set bits as num2, and the value `3 XOR 1 = 2` is minimal. ``` **Constraints:** `1 <= num1, num2 <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "num1 = 3, num2 = 5", + "output": "3 " + }, + { + "label": "Example 2", + "input": "num1 = 1, num2 = 12", + "output": "3 " + } + ], + "constraints": [ + "x has the same number of set bits as num2, and", + "The value x XOR num1 is minimal.", + "1 <= num1, num2 <= 109" + ], + "python_template": "class Solution(object):\n def minimizeXor(self, num1, num2):\n \"\"\"\n :type num1: int\n :type num2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimizeXor(int num1, int num2) {\n \n }\n}", + "metadata": { + "func_name": "minimizeXor" + } +} \ No newline at end of file diff --git a/minimum-absolute-difference-between-elements-with-constraint.json b/minimum-absolute-difference-between-elements-with-constraint.json new file mode 100644 index 0000000000000000000000000000000000000000..50742a12674be8e576fcdb00526274b5d492465a --- /dev/null +++ b/minimum-absolute-difference-between-elements-with-constraint.json @@ -0,0 +1,35 @@ +{ + "id": 3000, + "name": "minimum-absolute-difference-between-elements-with-constraint", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-absolute-difference-between-elements-with-constraint/", + "date": "2023-08-06", + "task_description": "You are given a **0-indexed** integer array `nums` and an integer `x`. Find the **minimum absolute difference** between two elements in the array that are at least `x` indices apart. In other words, find two indices `i` and `j` such that `abs(i - j) >= x` and `abs(nums[i] - nums[j])` is minimized. Return_ an integer denoting the **minimum** absolute difference between two elements that are at least_ `x` _indices apart_. **Example 1:** ``` **Input:** nums = [4,3,2,4], x = 2 **Output:** 0 **Explanation:** We can select nums[0] = 4 and nums[3] = 4. They are at least 2 indices apart, and their absolute difference is the minimum, 0. It can be shown that 0 is the optimal answer. ``` **Example 2:** ``` **Input:** nums = [5,3,2,10,15], x = 1 **Output:** 1 **Explanation:** We can select nums[1] = 3 and nums[2] = 2. They are at least 1 index apart, and their absolute difference is the minimum, 1. It can be shown that 1 is the optimal answer. ``` **Example 3:** ``` **Input:** nums = [1,2,3,4], x = 3 **Output:** 3 **Explanation:** We can select nums[0] = 1 and nums[3] = 4. They are at least 3 indices apart, and their absolute difference is the minimum, 3. It can be shown that 3 is the optimal answer. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `0 <= x < nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,3,2,4], x = 2", + "output": "0 " + }, + { + "label": "Example 2", + "input": "nums = [5,3,2,10,15], x = 1", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3,4], x = 3", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "0 <= x < nums.length" + ], + "python_template": "class Solution(object):\n def minAbsoluteDifference(self, nums, x):\n \"\"\"\n :type nums: List[int]\n :type x: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minAbsoluteDifference(List nums, int x) {\n \n }\n}", + "metadata": { + "func_name": "minAbsoluteDifference" + } +} \ No newline at end of file diff --git a/minimum-amount-of-time-to-collect-garbage.json b/minimum-amount-of-time-to-collect-garbage.json new file mode 100644 index 0000000000000000000000000000000000000000..1b8c4f5b288ef4ca3287e5b49b14d68fdb0d9836 --- /dev/null +++ b/minimum-amount-of-time-to-collect-garbage.json @@ -0,0 +1,32 @@ +{ + "id": 2471, + "name": "minimum-amount-of-time-to-collect-garbage", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/", + "date": "2022-08-21", + "task_description": "You are given a **0-indexed** array of strings `garbage` where `garbage[i]` represents the assortment of garbage at the `ith` house. `garbage[i]` consists only of the characters `'M'`, `'P'` and `'G'` representing one unit of metal, paper and glass garbage respectively. Picking up **one** unit of any type of garbage takes `1` minute. You are also given a **0-indexed** integer array `travel` where `travel[i]` is the number of minutes needed to go from house `i` to house `i + 1`. There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house `0` and must visit each house **in order**; however, they do **not** need to visit every house. Only **one** garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks **cannot** do anything. Return_ the **minimum** number of minutes needed to pick up all the garbage._ **Example 1:** ``` **Input:** garbage = [\"G\",\"P\",\"GP\",\"GG\"], travel = [2,4,3] **Output:** 21 **Explanation:** The paper garbage truck: 1. Travels from house 0 to house 1 2. Collects the paper garbage at house 1 3. Travels from house 1 to house 2 4. Collects the paper garbage at house 2 Altogether, it takes 8 minutes to pick up all the paper garbage. The glass garbage truck: 1. Collects the glass garbage at house 0 2. Travels from house 0 to house 1 3. Travels from house 1 to house 2 4. Collects the glass garbage at house 2 5. Travels from house 2 to house 3 6. Collects the glass garbage at house 3 Altogether, it takes 13 minutes to pick up all the glass garbage. Since there is no metal garbage, we do not need to consider the metal garbage truck. Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage. ``` **Example 2:** ``` **Input:** garbage = [\"MMM\",\"PGM\",\"GP\"], travel = [3,10] **Output:** 37 **Explanation:** The metal garbage truck takes 7 minutes to pick up all the metal garbage. The paper garbage truck takes 15 minutes to pick up all the paper garbage. The glass garbage truck takes 15 minutes to pick up all the glass garbage. It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage. ``` **Constraints:** `2 <= garbage.length <= 105` `garbage[i]` consists of only the letters `'M'`, `'P'`, and `'G'`. `1 <= garbage[i].length <= 10` `travel.length == garbage.length - 1` `1 <= travel[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "garbage = [\"G\",\"P\",\"GP\",\"GG\"], travel = [2,4,3]", + "output": "21 " + }, + { + "label": "Example 2", + "input": "garbage = [\"MMM\",\"PGM\",\"GP\"], travel = [3,10]", + "output": "37 " + } + ], + "constraints": [ + "2 <= garbage.length <= 105", + "garbage[i] consists of only the letters 'M', 'P', and 'G'.", + "1 <= garbage[i].length <= 10", + "travel.length == garbage.length - 1", + "1 <= travel[i] <= 100" + ], + "python_template": "class Solution(object):\n def garbageCollection(self, garbage, travel):\n \"\"\"\n :type garbage: List[str]\n :type travel: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int garbageCollection(String[] garbage, int[] travel) {\n \n }\n}", + "metadata": { + "func_name": "garbageCollection" + } +} \ No newline at end of file diff --git a/minimum-amount-of-time-to-fill-cups.json b/minimum-amount-of-time-to-fill-cups.json new file mode 100644 index 0000000000000000000000000000000000000000..ee49154c7b9dc047606a5e26023f6960d6a5fbfb --- /dev/null +++ b/minimum-amount-of-time-to-fill-cups.json @@ -0,0 +1,34 @@ +{ + "id": 2412, + "name": "minimum-amount-of-time-to-fill-cups", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/", + "date": "2022-07-03", + "task_description": "You have a water dispenser that can dispense cold, warm, and hot water. Every second, you can either fill up `2` cups with **different** types of water, or `1` cup of any type of water. You are given a **0-indexed** integer array `amount` of length `3` where `amount[0]`, `amount[1]`, and `amount[2]` denote the number of cold, warm, and hot water cups you need to fill respectively. Return _the **minimum** number of seconds needed to fill up all the cups_. **Example 1:** ``` **Input:** amount = [1,4,2] **Output:** 4 **Explanation:** One way to fill up the cups is: Second 1: Fill up a cold cup and a warm cup. Second 2: Fill up a warm cup and a hot cup. Second 3: Fill up a warm cup and a hot cup. Second 4: Fill up a warm cup. It can be proven that 4 is the minimum number of seconds needed. ``` **Example 2:** ``` **Input:** amount = [5,4,4] **Output:** 7 **Explanation:** One way to fill up the cups is: Second 1: Fill up a cold cup, and a hot cup. Second 2: Fill up a cold cup, and a warm cup. Second 3: Fill up a cold cup, and a warm cup. Second 4: Fill up a warm cup, and a hot cup. Second 5: Fill up a cold cup, and a hot cup. Second 6: Fill up a cold cup, and a warm cup. Second 7: Fill up a hot cup. ``` **Example 3:** ``` **Input:** amount = [5,0,0] **Output:** 5 **Explanation:** Every second, we fill up a cold cup. ``` **Constraints:** `amount.length == 3` `0 <= amount[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "amount = [1,4,2]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "amount = [5,4,4]", + "output": "7 " + }, + { + "label": "Example 3", + "input": "amount = [5,0,0]", + "output": "5 " + } + ], + "constraints": [ + "amount.length == 3", + "0 <= amount[i] <= 100" + ], + "python_template": "class Solution(object):\n def fillCups(self, amount):\n \"\"\"\n :type amount: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int fillCups(int[] amount) {\n \n }\n}", + "metadata": { + "func_name": "fillCups" + } +} \ No newline at end of file diff --git a/minimum-array-end.json b/minimum-array-end.json new file mode 100644 index 0000000000000000000000000000000000000000..82a3aca2d2bd89db7d682ae89d4fb983fe6b7cc9 --- /dev/null +++ b/minimum-array-end.json @@ -0,0 +1,28 @@ +{ + "id": 3394, + "name": "minimum-array-end", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-array-end/", + "date": "2024-04-21", + "task_description": "You are given two integers `n` and `x`. You have to construct an array of **positive** integers `nums` of size `n` where for every `0 <= i < n - 1`, `nums[i + 1]` is **greater than** `nums[i]`, and the result of the bitwise `AND` operation between all elements of `nums` is `x`. Return the **minimum** possible value of `nums[n - 1]`. **Example 1:** **Input:** n = 3, x = 4 **Output:** 6 **Explanation:** `nums` can be `[4,5,6]` and its last element is 6. **Example 2:** **Input:** n = 2, x = 7 **Output:** 15 **Explanation:** `nums` can be `[7,15]` and its last element is 15. **Constraints:** `1 <= n, x <= 108`", + "test_case": [ + { + "label": "Example 1", + "input": "n = 3, x = 4", + "output": "6 " + }, + { + "label": "Example 2", + "input": "n = 2, x = 7", + "output": "15 " + } + ], + "constraints": [ + "1 <= n, x <= 108" + ], + "python_template": "class Solution(object):\n def minEnd(self, n, x):\n \"\"\"\n :type n: int\n :type x: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minEnd(int n, int x) {\n \n }\n}", + "metadata": { + "func_name": "minEnd" + } +} \ No newline at end of file diff --git a/minimum-array-length-after-pair-removals.json b/minimum-array-length-after-pair-removals.json new file mode 100644 index 0000000000000000000000000000000000000000..4e67e4e1956a28c477c582672de1060a5463b393 --- /dev/null +++ b/minimum-array-length-after-pair-removals.json @@ -0,0 +1,42 @@ +{ + "id": 3081, + "name": "minimum-array-length-after-pair-removals", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-array-length-after-pair-removals/", + "date": "2023-09-02", + "task_description": "Given an integer array `num` sorted in non-decreasing order. You can perform the following operation any number of times: Choose **two** indices, `i` and `j`, where `nums[i] < nums[j]`. Then, remove the elements at indices `i` and `j` from `nums`. The remaining elements retain their original order, and the array is re-indexed. Return the **minimum** length of `nums` after applying the operation zero or more times. **Example 1:** **Input:** nums = [1,2,3,4] **Output:** 0 **Explanation:** **Example 2:** **Input:** nums = [1,1,2,2,3,3] **Output:** 0 **Explanation:** **Example 3:** **Input:** nums = [1000000000,1000000000] **Output:** 2 **Explanation:** Since both numbers are equal, they cannot be removed. **Example 4:** **Input:** nums = [2,3,4,4,4] **Output:** 1 **Explanation:** **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `nums` is sorted in **non-decreasing** order.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4]", + "output": "0 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,2,2,3,3]", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [1000000000,1000000000]", + "output": "2 " + }, + { + "label": "Example 4", + "input": "nums = [2,3,4,4,4]", + "output": "1 " + } + ], + "constraints": [ + "Choose two indices, i and j, where nums[i] < nums[j].", + "Then, remove the elements at indices i and j from nums. The remaining elements retain their original order, and the array is re-indexed.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "nums is sorted in non-decreasing order." + ], + "python_template": "class Solution(object):\n def minLengthAfterRemovals(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minLengthAfterRemovals(List nums) {\n \n }\n}", + "metadata": { + "func_name": "minLengthAfterRemovals" + } +} \ No newline at end of file diff --git a/minimum-array-sum.json b/minimum-array-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..4f53129bc25d18b7a2bd477129416bfefe4b0940 --- /dev/null +++ b/minimum-array-sum.json @@ -0,0 +1,40 @@ +{ + "id": 3654, + "name": "minimum-array-sum", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-array-sum/", + "date": "2024-11-17", + "task_description": "You are given an integer array `nums` and three integers `k`, `op1`, and `op2`. You can perform the following operations on `nums`: **Operation 1**: Choose an index `i` and divide `nums[i]` by 2, **rounding up** to the nearest whole number. You can perform this operation at most `op1` times, and not more than **once** per index. **Operation 2**: Choose an index `i` and subtract `k` from `nums[i]`, but only if `nums[i]` is greater than or equal to `k`. You can perform this operation at most `op2` times, and not more than **once** per index. **Note:** Both operations can be applied to the same index, but at most once each. Return the **minimum** possible **sum** of all elements in `nums` after performing any number of operations. **Example 1:** **Input:** nums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1 **Output:** 23 **Explanation:** Apply Operation 2 to `nums[1] = 8`, making `nums[1] = 5`. Apply Operation 1 to `nums[3] = 19`, making `nums[3] = 10`. The resulting array becomes `[2, 5, 3, 10, 3]`, which has the minimum possible sum of 23 after applying the operations. **Example 2:** **Input:** nums = [2,4,3], k = 3, op1 = 2, op2 = 1 **Output:** 3 **Explanation:** Apply Operation 1 to `nums[0] = 2`, making `nums[0] = 1`. Apply Operation 1 to `nums[1] = 4`, making `nums[1] = 2`. Apply Operation 2 to `nums[2] = 3`, making `nums[2] = 0`. The resulting array becomes `[1, 2, 0]`, which has the minimum possible sum of 3 after applying the operations. **Constraints:** `1 <= nums.length <= 100` `0 <= nums[i] <= 105` `0 <= k <= 105` `0 <= op1, op2 <= nums.length`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1", + "output": "23 " + }, + { + "label": "Example 2", + "input": "nums = [2,4,3], k = 3, op1 = 2, op2 = 1", + "output": "3 " + } + ], + "constraints": [ + "Operation 1: Choose an index i and divide nums[i] by 2, rounding up to the nearest whole number. You can perform this operation at most op1 times, and not more than once per index.", + "Operation 2: Choose an index i and subtract k from nums[i], but only if nums[i] is greater than or equal to k. You can perform this operation at most op2 times, and not more than once per index.", + "Apply Operation 2 to nums[1] = 8, making nums[1] = 5.", + "Apply Operation 1 to nums[3] = 19, making nums[3] = 10.", + "The resulting array becomes [2, 5, 3, 10, 3], which has the minimum possible sum of 23 after applying the operations.", + "Apply Operation 1 to nums[0] = 2, making nums[0] = 1.", + "Apply Operation 1 to nums[1] = 4, making nums[1] = 2.", + "Apply Operation 2 to nums[2] = 3, making nums[2] = 0.", + "The resulting array becomes [1, 2, 0], which has the minimum possible sum of 3 after applying the operations.", + "1 <= nums.length <= 100", + "0 <= nums[i] <= 105", + "0 <= k <= 105", + "0 <= op1, op2 <= nums.length" + ], + "python_template": "class Solution(object):\n def minArraySum(self, nums, k, op1, op2):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type op1: int\n :type op2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minArraySum(int[] nums, int k, int op1, int op2) {\n \n }\n}", + "metadata": { + "func_name": "minArraySum" + } +} \ No newline at end of file diff --git a/minimum-average-difference.json b/minimum-average-difference.json new file mode 100644 index 0000000000000000000000000000000000000000..9eff6783322802c4ed03b90c98583f84789c7c98 --- /dev/null +++ b/minimum-average-difference.json @@ -0,0 +1,32 @@ +{ + "id": 2342, + "name": "minimum-average-difference", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-average-difference/", + "date": "2022-04-16", + "task_description": "You are given a **0-indexed** integer array `nums` of length `n`. The **average difference** of the index `i` is the **absolute** **difference** between the average of the **first** `i + 1` elements of `nums` and the average of the **last** `n - i - 1` elements. Both averages should be **rounded down** to the nearest integer. Return_ the index with the **minimum average difference**_. If there are multiple such indices, return the **smallest** one. **Note:** The **absolute difference** of two numbers is the absolute value of their difference. The **average** of `n` elements is the **sum** of the `n` elements divided (**integer division**) by `n`. The average of `0` elements is considered to be `0`. **Example 1:** ``` **Input:** nums = [2,5,3,9,5,3] **Output:** 3 **Explanation:** - The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3. - The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2. - The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2. - The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0. - The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1. - The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4. The average difference of index 3 is the minimum average difference so return 3. ``` **Example 2:** ``` **Input:** nums = [0] **Output:** 0 **Explanation:** The only index is 0 so return 0. The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,5,3,9,5,3]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [0]", + "output": "0 " + } + ], + "constraints": [ + "The absolute difference of two numbers is the absolute value of their difference.", + "The average of n elements is the sum of the n elements divided (integer division) by n.", + "The average of 0 elements is considered to be 0.", + "1 <= nums.length <= 105", + "0 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def minimumAverageDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumAverageDifference(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumAverageDifference" + } +} \ No newline at end of file diff --git a/minimum-average-of-smallest-and-largest-elements.json b/minimum-average-of-smallest-and-largest-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..f387562eb1713dc7e7e21c4c98da99ac947e7c2d --- /dev/null +++ b/minimum-average-of-smallest-and-largest-elements.json @@ -0,0 +1,37 @@ +{ + "id": 3471, + "name": "minimum-average-of-smallest-and-largest-elements", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements/", + "date": "2024-06-16", + "task_description": "You have an array of floating point numbers `averages` which is initially empty. You are given an array `nums` of `n` integers where `n` is even. You repeat the following procedure `n / 2` times: Remove the **smallest** element, `minElement`, and the **largest** element `maxElement`, from `nums`. Add `(minElement + maxElement) / 2` to `averages`. Return the **minimum** element in `averages`. **Example 1:** **Input:** nums = [7,8,3,4,15,13,4,1] **Output:** 5.5 **Explanation:** step nums averages 0 [7,8,3,4,15,13,4,1] [] 1 [7,8,3,4,13,4] [8] 2 [7,8,4,4] [8,8] 3 [7,4] [8,8,6] 4 [] [8,8,6,5.5] The smallest element of averages, 5.5, is returned. **Example 2:** **Input:** nums = [1,9,8,3,10,5] **Output:** 5.5 **Explanation:** step nums averages 0 [1,9,8,3,10,5] [] 1 [9,8,3,5] [5.5] 2 [8,5] [5.5,6] 3 [] [5.5,6,6.5] **Example 3:** **Input:** nums = [1,2,3,7,8,9] **Output:** 5.0 **Explanation:** step nums averages 0 [1,2,3,7,8,9] [] 1 [2,3,7,8] [5] 2 [3,7] [5,5] 3 [] [5,5,5] **Constraints:** `2 <= n == nums.length <= 50` `n` is even. `1 <= nums[i] <= 50`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [7,8,3,4,15,13,4,1]", + "output": "5.5 " + }, + { + "label": "Example 2", + "input": "nums = [1,9,8,3,10,5]", + "output": "5.5 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,3,7,8,9]", + "output": "5.0 " + } + ], + "constraints": [ + "Remove the smallest element, minElement, and the largest element maxElement, from nums.", + "Add (minElement + maxElement) / 2 to averages.", + "2 <= n == nums.length <= 50", + "n is even.", + "1 <= nums[i] <= 50" + ], + "python_template": "class Solution(object):\n def minimumAverage(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: float\n \"\"\"\n ", + "java_template": "class Solution {\n public double minimumAverage(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumAverage" + } +} \ No newline at end of file diff --git a/minimum-bit-flips-to-convert-number.json b/minimum-bit-flips-to-convert-number.json new file mode 100644 index 0000000000000000000000000000000000000000..bb3b2beeea189d292b44629386aad572cc7a1be2 --- /dev/null +++ b/minimum-bit-flips-to-convert-number.json @@ -0,0 +1,29 @@ +{ + "id": 2323, + "name": "minimum-bit-flips-to-convert-number", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-bit-flips-to-convert-number/", + "date": "2022-03-19", + "task_description": "A **bit flip** of a number `x` is choosing a bit in the binary representation of `x` and **flipping** it from either `0` to `1` or `1` to `0`. For example, for `x = 7`, the binary representation is `111` and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get `110`, flip the second bit from the right to get `101`, flip the fifth bit from the right (a leading zero) to get `10111`, etc. Given two integers `start` and `goal`, return_ the **minimum** number of **bit flips** to convert _`start`_ to _`goal`. **Example 1:** ``` **Input:** start = 10, goal = 7 **Output:** 3 **Explanation:** The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps: - Flip the first bit from the right: 1010 -> 1011. - Flip the third bit from the right: 1011 -> 1111. - Flip the fourth bit from the right: 1111 -> 0111. It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3. ``` **Example 2:** ``` **Input:** start = 3, goal = 4 **Output:** 3 **Explanation:** The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps: - Flip the first bit from the right: 011 -> 010. - Flip the second bit from the right: 010 -> 000. - Flip the third bit from the right: 000 -> 100. It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3. ``` **Constraints:** `0 <= start, goal <= 109` **Note:** This question is the same as 461: Hamming Distance.", + "test_case": [ + { + "label": "Example 1", + "input": "start = 10, goal = 7", + "output": "3 " + }, + { + "label": "Example 2", + "input": "start = 3, goal = 4", + "output": "3 " + } + ], + "constraints": [ + "For example, for x = 7, the binary representation is 111 and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get 110, flip the second bit from the right to get 101, flip the fifth bit from the right (a leading zero) to get 10111, etc.", + "0 <= start, goal <= 109" + ], + "python_template": "class Solution(object):\n def minBitFlips(self, start, goal):\n \"\"\"\n :type start: int\n :type goal: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minBitFlips(int start, int goal) {\n \n }\n}", + "metadata": { + "func_name": "minBitFlips" + } +} \ No newline at end of file diff --git a/minimum-changes-to-make-k-semi-palindromes.json b/minimum-changes-to-make-k-semi-palindromes.json new file mode 100644 index 0000000000000000000000000000000000000000..c52b3b7609637f172e58e7ee166c5c85ac884eb0 --- /dev/null +++ b/minimum-changes-to-make-k-semi-palindromes.json @@ -0,0 +1,53 @@ +{ + "id": 2879, + "name": "minimum-changes-to-make-k-semi-palindromes", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-changes-to-make-k-semi-palindromes/", + "date": "2023-10-15", + "task_description": "Given a string `s` and an integer `k`, partition `s` into `k` **substrings** such that the letter changes needed to make each substring a **semi-palindrome** are minimized. Return the _**minimum** number of letter changes_ required_._ A **semi-palindrome** is a special type of string that can be divided into **palindromes** based on a repeating pattern. To check if a string is a semi-palindrome:​ Choose a positive divisor `d` of the string's length. `d` can range from `1` up to, but not including, the string's length. For a string of length `1`, it does not have a valid divisor as per this definition, since the only divisor is its length, which is not allowed. For a given divisor `d`, divide the string into groups where each group contains characters from the string that follow a repeating pattern of length `d`. Specifically, the first group consists of characters at positions `1`, `1 + d`, `1 + 2d`, and so on; the second group includes characters at positions `2`, `2 + d`, `2 + 2d`, etc. The string is considered a semi-palindrome if each of these groups forms a palindrome. Consider the string `\"abcabc\"`: The length of `\"abcabc\"` is `6`. Valid divisors are `1`, `2`, and `3`. For `d = 1`: The entire string `\"abcabc\"` forms one group. Not a palindrome. For `d = 2`: Group 1 (positions `1, 3, 5`): `\"acb\"` Group 2 (positions `2, 4, 6`): `\"bac\"` Neither group forms a palindrome. For `d = 3`: Group 1 (positions `1, 4`): `\"aa\"` Group 2 (positions `2, 5`): `\"bb\"` Group 3 (positions `3, 6`): `\"cc\"` All groups form palindromes. Therefore, `\"abcabc\"` is a semi-palindrome. **Example 1: ** **Input: ** s = \"abcac\", k = 2 **Output: ** 1 **Explanation: ** Divide `s` into `\"ab\"` and `\"cac\"`. `\"cac\"` is already semi-palindrome. Change `\"ab\"` to `\"aa\"`, it becomes semi-palindrome with `d = 1`. **Example 2: ** **Input: ** s = \"abcdef\", k = 2 **Output: ** 2 **Explanation: ** Divide `s` into substrings `\"abc\"` and `\"def\"`. Each needs one change to become semi-palindrome. **Example 3: ** **Input: ** s = \"aabbaa\", k = 3 **Output: ** 0 **Explanation: ** Divide `s` into substrings `\"aa\"`, `\"bb\"` and `\"aa\"`. All are already semi-palindromes. **Constraints:** `2 <= s.length <= 200` `1 <= k <= s.length / 2` `s` contains only lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abcac\", k = 2", + "output": "1 " + }, + { + "label": "Example 2", + "input": "s = \"abcdef\", k = 2", + "output": "2 " + }, + { + "label": "Example 3", + "input": "s = \"aabbaa\", k = 3", + "output": "0 " + } + ], + "constraints": [ + "The length of \"abcabc\" is 6. Valid divisors are 1, 2, and 3.", + "For d = 1: The entire string \"abcabc\" forms one group. Not a palindrome.", + "For d = 2:\n\t\nGroup 1 (positions 1, 3, 5): \"acb\"\nGroup 2 (positions 2, 4, 6): \"bac\"\nNeither group forms a palindrome.", + "Group 1 (positions 1, 3, 5): \"acb\"", + "Group 2 (positions 2, 4, 6): \"bac\"", + "Neither group forms a palindrome.", + "For d = 3:\n\t\nGroup 1 (positions 1, 4): \"aa\"\nGroup 2 (positions 2, 5): \"bb\"\nGroup 3 (positions 3, 6): \"cc\"\nAll groups form palindromes. Therefore, \"abcabc\" is a semi-palindrome.", + "Group 1 (positions 1, 4): \"aa\"", + "Group 2 (positions 2, 5): \"bb\"", + "Group 3 (positions 3, 6): \"cc\"", + "All groups form palindromes. Therefore, \"abcabc\" is a semi-palindrome.", + "Group 1 (positions 1, 3, 5): \"acb\"", + "Group 2 (positions 2, 4, 6): \"bac\"", + "Neither group forms a palindrome.", + "Group 1 (positions 1, 4): \"aa\"", + "Group 2 (positions 2, 5): \"bb\"", + "Group 3 (positions 3, 6): \"cc\"", + "All groups form palindromes. Therefore, \"abcabc\" is a semi-palindrome.", + "2 <= s.length <= 200", + "1 <= k <= s.length / 2", + "s contains only lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumChanges(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumChanges(String s, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumChanges" + } +} \ No newline at end of file diff --git a/minimum-common-value.json b/minimum-common-value.json new file mode 100644 index 0000000000000000000000000000000000000000..77f681428bda3cb2e7fa52b15f1849b0018a8057 --- /dev/null +++ b/minimum-common-value.json @@ -0,0 +1,30 @@ +{ + "id": 2634, + "name": "minimum-common-value", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-common-value/", + "date": "2023-01-07", + "task_description": "Given two integer arrays `nums1` and `nums2`, sorted in non-decreasing order, return _the **minimum integer common** to both arrays_. If there is no common integer amongst `nums1` and `nums2`, return `-1`. Note that an integer is said to be **common** to `nums1` and `nums2` if both arrays have **at least one** occurrence of that integer. **Example 1:** ``` **Input:** nums1 = [1,2,3], nums2 = [2,4] **Output:** 2 **Explanation:** The smallest element common to both arrays is 2, so we return 2. ``` **Example 2:** ``` **Input:** nums1 = [1,2,3,6], nums2 = [2,3,4,5] **Output:** 2 **Explanation:** There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned. ``` **Constraints:** `1 <= nums1.length, nums2.length <= 105` `1 <= nums1[i], nums2[j] <= 109` Both `nums1` and `nums2` are sorted in **non-decreasing** order.", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [1,2,3], nums2 = [2,4]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums1 = [1,2,3,6], nums2 = [2,3,4,5]", + "output": "2 " + } + ], + "constraints": [ + "1 <= nums1.length, nums2.length <= 105", + "1 <= nums1[i], nums2[j] <= 109", + "Both nums1 and nums2 are sorted in non-decreasing order." + ], + "python_template": "class Solution(object):\n def getCommon(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 getCommon(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "getCommon" + } +} \ No newline at end of file diff --git a/minimum-consecutive-cards-to-pick-up.json b/minimum-consecutive-cards-to-pick-up.json new file mode 100644 index 0000000000000000000000000000000000000000..ff480630bf809db01b29a6f8bfddc54f299b332a --- /dev/null +++ b/minimum-consecutive-cards-to-pick-up.json @@ -0,0 +1,29 @@ +{ + "id": 2338, + "name": "minimum-consecutive-cards-to-pick-up", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/", + "date": "2022-04-24", + "task_description": "You are given an integer array `cards` where `cards[i]` represents the **value** of the `ith` card. A pair of cards are **matching** if the cards have the **same** value. Return_ the **minimum** number of **consecutive** cards you have to pick up to have a pair of **matching** cards among the picked cards._ If it is impossible to have matching cards, return `-1`. **Example 1:** ``` **Input:** cards = [3,4,2,3,4,7] **Output:** 4 **Explanation:** We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3. Note that picking up the cards [4,2,3,4] is also optimal. ``` **Example 2:** ``` **Input:** cards = [1,0,5,3] **Output:** -1 **Explanation:** There is no way to pick up a set of consecutive cards that contain a pair of matching cards. ``` **Constraints:** `1 <= cards.length <= 105` `0 <= cards[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "cards = [3,4,2,3,4,7]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "cards = [1,0,5,3]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= cards.length <= 105", + "0 <= cards[i] <= 106" + ], + "python_template": "class Solution(object):\n def minimumCardPickup(self, cards):\n \"\"\"\n :type cards: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumCardPickup(int[] cards) {\n \n }\n}", + "metadata": { + "func_name": "minimumCardPickup" + } +} \ No newline at end of file diff --git a/minimum-cost-for-cutting-cake-i.json b/minimum-cost-for-cutting-cake-i.json new file mode 100644 index 0000000000000000000000000000000000000000..100c1ab2e5bfe50470e4eae5eac9fcc83c1adc53 --- /dev/null +++ b/minimum-cost-for-cutting-cake-i.json @@ -0,0 +1,41 @@ +{ + "id": 3494, + "name": "minimum-cost-for-cutting-cake-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-cost-for-cutting-cake-i/", + "date": "2024-07-07", + "task_description": "There is an `m x n` cake that needs to be cut into `1 x 1` pieces. You are given integers `m`, `n`, and two arrays: `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`. `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`. In one operation, you can choose any piece of cake that is not yet a `1 x 1` square and perform one of the following cuts: Cut along a horizontal line `i` at a cost of `horizontalCut[i]`. Cut along a vertical line `j` at a cost of `verticalCut[j]`. After the cut, the piece of cake is divided into two distinct pieces. The cost of a cut depends only on the initial cost of the line and does not change. Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces. **Example 1:** **Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5] **Output:** 13 **Explanation:** Perform a cut on the vertical line 0 with cost 5, current total cost is 5. Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1. Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1. Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3. Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3. The total cost is `5 + 1 + 1 + 3 + 3 = 13`. **Example 2:** **Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4] **Output:** 15 **Explanation:** Perform a cut on the horizontal line 0 with cost 7. Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4. Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4. The total cost is `7 + 4 + 4 = 15`. **Constraints:** `1 <= m, n <= 20` `horizontalCut.length == m - 1` `verticalCut.length == n - 1` `1 <= horizontalCut[i], verticalCut[i] <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]", + "output": "13 " + }, + { + "label": "Example 2", + "input": "m = 2, n = 2, horizontalCut = [7], verticalCut = [4]", + "output": "15 " + } + ], + "constraints": [ + "horizontalCut of size m - 1, where horizontalCut[i] represents the cost to cut along the horizontal line i.", + "verticalCut of size n - 1, where verticalCut[j] represents the cost to cut along the vertical line j.", + "Perform a cut on the vertical line 0 with cost 5, current total cost is 5.", + "Perform a cut on the horizontal line 0 on 3 x 1 subgrid with cost 1.", + "Perform a cut on the horizontal line 0 on 3 x 1 subgrid with cost 1.", + "Perform a cut on the horizontal line 1 on 2 x 1 subgrid with cost 3.", + "Perform a cut on the horizontal line 1 on 2 x 1 subgrid with cost 3.", + "Perform a cut on the horizontal line 0 with cost 7.", + "Perform a cut on the vertical line 0 on 1 x 2 subgrid with cost 4.", + "Perform a cut on the vertical line 0 on 1 x 2 subgrid with cost 4.", + "1 <= m, n <= 20", + "horizontalCut.length == m - 1", + "verticalCut.length == n - 1", + "1 <= horizontalCut[i], verticalCut[i] <= 103" + ], + "python_template": "class Solution(object):\n def minimumCost(self, m, n, horizontalCut, verticalCut):\n \"\"\"\n :type m: int\n :type n: int\n :type horizontalCut: List[int]\n :type verticalCut: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {\n \n }\n}", + "metadata": { + "func_name": "minimumCost" + } +} \ No newline at end of file diff --git a/minimum-cost-for-cutting-cake-ii.json b/minimum-cost-for-cutting-cake-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..64500109edf4b9cc8b177b25d1f393b179f65257 --- /dev/null +++ b/minimum-cost-for-cutting-cake-ii.json @@ -0,0 +1,41 @@ +{ + "id": 3500, + "name": "minimum-cost-for-cutting-cake-ii", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-cost-for-cutting-cake-ii/", + "date": "2024-07-07", + "task_description": "There is an `m x n` cake that needs to be cut into `1 x 1` pieces. You are given integers `m`, `n`, and two arrays: `horizontalCut` of size `m - 1`, where `horizontalCut[i]` represents the cost to cut along the horizontal line `i`. `verticalCut` of size `n - 1`, where `verticalCut[j]` represents the cost to cut along the vertical line `j`. In one operation, you can choose any piece of cake that is not yet a `1 x 1` square and perform one of the following cuts: Cut along a horizontal line `i` at a cost of `horizontalCut[i]`. Cut along a vertical line `j` at a cost of `verticalCut[j]`. After the cut, the piece of cake is divided into two distinct pieces. The cost of a cut depends only on the initial cost of the line and does not change. Return the **minimum** total cost to cut the entire cake into `1 x 1` pieces. **Example 1:** **Input:** m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5] **Output:** 13 **Explanation:** Perform a cut on the vertical line 0 with cost 5, current total cost is 5. Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1. Perform a cut on the horizontal line 0 on `3 x 1` subgrid with cost 1. Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3. Perform a cut on the horizontal line 1 on `2 x 1` subgrid with cost 3. The total cost is `5 + 1 + 1 + 3 + 3 = 13`. **Example 2:** **Input:** m = 2, n = 2, horizontalCut = [7], verticalCut = [4] **Output:** 15 **Explanation:** Perform a cut on the horizontal line 0 with cost 7. Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4. Perform a cut on the vertical line 0 on `1 x 2` subgrid with cost 4. The total cost is `7 + 4 + 4 = 15`. **Constraints:** `1 <= m, n <= 105` `horizontalCut.length == m - 1` `verticalCut.length == n - 1` `1 <= horizontalCut[i], verticalCut[i] <= 103`", + "test_case": [ + { + "label": "Example 1", + "input": "m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]", + "output": "13 " + }, + { + "label": "Example 2", + "input": "m = 2, n = 2, horizontalCut = [7], verticalCut = [4]", + "output": "15 " + } + ], + "constraints": [ + "horizontalCut of size m - 1, where horizontalCut[i] represents the cost to cut along the horizontal line i.", + "verticalCut of size n - 1, where verticalCut[j] represents the cost to cut along the vertical line j.", + "Perform a cut on the vertical line 0 with cost 5, current total cost is 5.", + "Perform a cut on the horizontal line 0 on 3 x 1 subgrid with cost 1.", + "Perform a cut on the horizontal line 0 on 3 x 1 subgrid with cost 1.", + "Perform a cut on the horizontal line 1 on 2 x 1 subgrid with cost 3.", + "Perform a cut on the horizontal line 1 on 2 x 1 subgrid with cost 3.", + "Perform a cut on the horizontal line 0 with cost 7.", + "Perform a cut on the vertical line 0 on 1 x 2 subgrid with cost 4.", + "Perform a cut on the vertical line 0 on 1 x 2 subgrid with cost 4.", + "1 <= m, n <= 105", + "horizontalCut.length == m - 1", + "verticalCut.length == n - 1", + "1 <= horizontalCut[i], verticalCut[i] <= 103" + ], + "python_template": "class Solution(object):\n def minimumCost(self, m, n, horizontalCut, verticalCut):\n \"\"\"\n :type m: int\n :type n: int\n :type horizontalCut: List[int]\n :type verticalCut: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {\n \n }\n}", + "metadata": { + "func_name": "minimumCost" + } +} \ No newline at end of file diff --git a/minimum-cost-good-caption.json b/minimum-cost-good-caption.json new file mode 100644 index 0000000000000000000000000000000000000000..d106da421724b8b378f776396282e04647ee0956 --- /dev/null +++ b/minimum-cost-good-caption.json @@ -0,0 +1,42 @@ +{ + "id": 3701, + "name": "minimum-cost-good-caption", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-cost-good-caption/", + "date": "2025-01-18", + "task_description": "You are given a string `caption` of length `n`. A **good** caption is a string where **every** character appears in groups of **at least 3** consecutive occurrences. For example: `\"aaabbb\"` and `\"aaaaccc\"` are **good** captions. `\"aabbb\"` and `\"ccccd\"` are **not** good captions. You can perform the following operation **any** number of times: Choose an index `i` (where `0 <= i < n`) and change the character at that index to either: The character immediately **before** it in the alphabet (if `caption[i] != 'a'`). The character immediately **after** it in the alphabet (if `caption[i] != 'z'`). Your task is to convert the given `caption` into a **good** caption using the **minimum** number of operations, and return it. If there are **multiple** possible good captions, return the **lexicographically smallest** one among them. If it is **impossible** to create a good caption, return an empty string `\"\"`. **Example 1:** **Input:** caption = \"cdcd\" **Output:** \"cccc\" **Explanation:** It can be shown that the given caption cannot be transformed into a good caption with fewer than 2 operations. The possible good captions that can be created using exactly 2 operations are: `\"dddd\"`: Change `caption[0]` and `caption[2]` to their next character `'d'`. `\"cccc\"`: Change `caption[1]` and `caption[3]` to their previous character `'c'`. Since `\"cccc\"` is lexicographically smaller than `\"dddd\"`, return `\"cccc\"`. **Example 2:** **Input:** caption = \"aca\" **Output:** \"aaa\" **Explanation:** It can be proven that the given caption requires at least 2 operations to be transformed into a good caption. The only good caption that can be obtained with exactly 2 operations is as follows: Operation 1: Change `caption[1]` to `'b'`. `caption = \"aba\"`. Operation 2: Change `caption[1]` to `'a'`. `caption = \"aaa\"`. Thus, return `\"aaa\"`. **Example 3:** **Input:** caption = \"bc\" **Output:** \"\" **Explanation:** It can be shown that the given caption cannot be converted to a good caption by using any number of operations. **Constraints:** `1 <= caption.length <= 5 * 104` `caption` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "caption = \"cdcd\"", + "output": "\"cccc\" " + }, + { + "label": "Example 2", + "input": "caption = \"aca\"", + "output": "\"aaa\" " + }, + { + "label": "Example 3", + "input": "caption = \"bc\"", + "output": "\"\" " + } + ], + "constraints": [ + "\"aaabbb\" and \"aaaaccc\" are good captions.", + "\"aabbb\" and \"ccccd\" are not good captions.", + "The character immediately before it in the alphabet (if caption[i] != 'a').", + "The character immediately after it in the alphabet (if caption[i] != 'z').", + "\"dddd\": Change caption[0] and caption[2] to their next character 'd'.", + "\"cccc\": Change caption[1] and caption[3] to their previous character 'c'.", + "Operation 1: Change caption[1] to 'b'. caption = \"aba\".", + "Operation 2: Change caption[1] to 'a'. caption = \"aaa\".", + "1 <= caption.length <= 5 * 104", + "caption consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minCostGoodCaption(self, caption):\n \"\"\"\n :type caption: str\n :rtype: str\n \"\"\"\n ", + "java_template": "class Solution {\n public String minCostGoodCaption(String caption) {\n \n }\n}", + "metadata": { + "func_name": "minCostGoodCaption" + } +} \ No newline at end of file diff --git a/minimum-cost-of-buying-candies-with-discount.json b/minimum-cost-of-buying-candies-with-discount.json new file mode 100644 index 0000000000000000000000000000000000000000..d87ed487d01cda6fef0573d831d6fdf85e25b8ed --- /dev/null +++ b/minimum-cost-of-buying-candies-with-discount.json @@ -0,0 +1,35 @@ +{ + "id": 2248, + "name": "minimum-cost-of-buying-candies-with-discount", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount/", + "date": "2022-01-08", + "task_description": "A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**. The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought. For example, if there are `4` candies with costs `1`, `2`, `3`, and `4`, and the customer buys candies with costs `2` and `3`, they can take the candy with cost `1` for free, but not the candy with cost `4`. Given a **0-indexed** integer array `cost`, where `cost[i]` denotes the cost of the `ith` candy, return _the **minimum cost** of buying **all** the candies_. **Example 1:** ``` **Input:** cost = [1,2,3] **Output:** 5 **Explanation:** We buy the candies with costs 2 and 3, and take the candy with cost 1 for free. The total cost of buying all candies is 2 + 3 = 5. This is the **only** way we can buy the candies. Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free. The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies. ``` **Example 2:** ``` **Input:** cost = [6,5,7,9,2,2] **Output:** 23 **Explanation:** The way in which we can get the minimum cost is described below: - Buy candies with costs 9 and 7 - Take the candy with cost 6 for free - We buy candies with costs 5 and 2 - Take the last remaining candy with cost 2 for free Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23. ``` **Example 3:** ``` **Input:** cost = [5,5] **Output:** 10 **Explanation:** Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free. Hence, the minimum cost to buy all candies is 5 + 5 = 10. ``` **Constraints:** `1 <= cost.length <= 100` `1 <= cost[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "cost = [1,2,3]", + "output": "5 " + }, + { + "label": "Example 2", + "input": "cost = [6,5,7,9,2,2]", + "output": "23 " + }, + { + "label": "Example 3", + "input": "cost = [5,5]", + "output": "10 " + } + ], + "constraints": [ + "For example, if there are 4 candies with costs 1, 2, 3, and 4, and the customer buys candies with costs 2 and 3, they can take the candy with cost 1 for free, but not the candy with cost 4.", + "1 <= cost.length <= 100", + "1 <= cost[i] <= 100" + ], + "python_template": "class Solution(object):\n def minimumCost(self, cost):\n \"\"\"\n :type cost: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumCost(int[] cost) {\n \n }\n}", + "metadata": { + "func_name": "minimumCost" + } +} \ No newline at end of file diff --git a/minimum-cost-to-equalize-array.json b/minimum-cost-to-equalize-array.json new file mode 100644 index 0000000000000000000000000000000000000000..50834a97d927c9ee97ebf724b0a093de5699387d --- /dev/null +++ b/minimum-cost-to-equalize-array.json @@ -0,0 +1,50 @@ +{ + "id": 3402, + "name": "minimum-cost-to-equalize-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-cost-to-equalize-array/", + "date": "2024-04-28", + "task_description": "You are given an integer array `nums` and two integers `cost1` and `cost2`. You are allowed to perform **either** of the following operations **any** number of times: Choose an index `i` from `nums` and **increase** `nums[i]` by `1` for a cost of `cost1`. Choose two **different** indices `i`, `j`, from `nums` and **increase** `nums[i]` and `nums[j]` by `1` for a cost of `cost2`. Return the **minimum** **cost** required to make all elements in the array **equal**_. _ Since the answer may be very large, return it **modulo** `109 + 7`. **Example 1:** **Input:** nums = [4,1], cost1 = 5, cost2 = 2 **Output:** 15 **Explanation: ** The following operations can be performed to make the values equal: Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,2]`. Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,3]`. Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,4]`. The total cost is 15. **Example 2:** **Input:** nums = [2,3,3,3,5], cost1 = 2, cost2 = 1 **Output:** 6 **Explanation: ** The following operations can be performed to make the values equal: Increase `nums[0]` and `nums[1]` by 1 for a cost of 1. `nums` becomes `[3,4,3,3,5]`. Increase `nums[0]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[4,4,4,3,5]`. Increase `nums[0]` and `nums[3]` by 1 for a cost of 1. `nums` becomes `[5,4,4,4,5]`. Increase `nums[1]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5,4,5]`. Increase `nums[3]` by 1 for a cost of 2. `nums` becomes `[5,5,5,5,5]`. The total cost is 6. **Example 3:** **Input:** nums = [3,5,3], cost1 = 1, cost2 = 3 **Output:** 4 **Explanation:** The following operations can be performed to make the values equal: Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[4,5,3]`. Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[5,5,3]`. Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,4]`. Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5]`. The total cost is 4. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 106` `1 <= cost1 <= 106` `1 <= cost2 <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [4,1], cost1 = 5, cost2 = 2", + "output": "15 " + }, + { + "label": "Example 2", + "input": "nums = [2,3,3,3,5], cost1 = 2, cost2 = 1", + "output": "6 " + }, + { + "label": "Example 3", + "input": "nums = [3,5,3], cost1 = 1, cost2 = 3", + "output": "4 " + } + ], + "constraints": [ + "Choose an index i from nums and increase nums[i] by 1 for a cost of cost1.", + "Choose two different indices i, j, from nums and increase nums[i] and nums[j] by 1 for a cost of cost2.", + "Increase nums[1] by 1 for a cost of 5. nums becomes [4,2].", + "Increase nums[1] by 1 for a cost of 5. nums becomes [4,3].", + "Increase nums[1] by 1 for a cost of 5. nums becomes [4,4].", + "Increase nums[0] and nums[1] by 1 for a cost of 1. nums becomes [3,4,3,3,5].", + "Increase nums[0] and nums[2] by 1 for a cost of 1. nums becomes [4,4,4,3,5].", + "Increase nums[0] and nums[3] by 1 for a cost of 1. nums becomes [5,4,4,4,5].", + "Increase nums[1] and nums[2] by 1 for a cost of 1. nums becomes [5,5,5,4,5].", + "Increase nums[3] by 1 for a cost of 2. nums becomes [5,5,5,5,5].", + "Increase nums[0] by 1 for a cost of 1. nums becomes [4,5,3].", + "Increase nums[0] by 1 for a cost of 1. nums becomes [5,5,3].", + "Increase nums[2] by 1 for a cost of 1. nums becomes [5,5,4].", + "Increase nums[2] by 1 for a cost of 1. nums becomes [5,5,5].", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 106", + "1 <= cost1 <= 106", + "1 <= cost2 <= 106" + ], + "python_template": "class Solution(object):\n def minCostToEqualizeArray(self, nums, cost1, cost2):\n \"\"\"\n :type nums: List[int]\n :type cost1: int\n :type cost2: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minCostToEqualizeArray(int[] nums, int cost1, int cost2) {\n \n }\n}", + "metadata": { + "func_name": "minCostToEqualizeArray" + } +} \ No newline at end of file diff --git a/minimum-cost-to-make-all-characters-equal.json b/minimum-cost-to-make-all-characters-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..e5c76c1b7494bbf932a8dcfdcb2a3ec55dcbe13f --- /dev/null +++ b/minimum-cost-to-make-all-characters-equal.json @@ -0,0 +1,31 @@ +{ + "id": 2817, + "name": "minimum-cost-to-make-all-characters-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/", + "date": "2023-05-21", + "task_description": "You are given a **0-indexed** binary string `s` of length `n` on which you can apply two types of operations: Choose an index `i` and invert all characters from index `0` to index `i` (both inclusive), with a cost of `i + 1` Choose an index `i` and invert all characters from index `i` to index `n - 1` (both inclusive), with a cost of `n - i` Return _the **minimum cost **to make all characters of the string **equal**_. **Invert** a character means if its value is '0' it becomes '1' and vice-versa. **Example 1:** ``` **Input:** s = \"0011\" **Output:** 2 **Explanation:** Apply the second operation with `i = 2` to obtain `s = \"0000\" for a cost of 2`. It can be shown that 2 is the minimum cost to make all characters equal. ``` **Example 2:** ``` **Input:** s = \"010101\" **Output:** 9 **Explanation:** Apply the first operation with i = 2 to obtain s = \"101101\" for a cost of 3. Apply the first operation with i = 1 to obtain s = \"011101\" for a cost of 2. Apply the first operation with i = 0 to obtain s = \"111101\" for a cost of 1. Apply the second operation with i = 4 to obtain s = \"111110\" for a cost of 2. Apply the second operation with i = 5 to obtain s = \"111111\" for a cost of 1. The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make all characters equal. ``` **Constraints:** `1 <= s.length == n <= 105` `s[i]` is either `'0'` or `'1'`", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"0011\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"010101\"", + "output": "9 " + } + ], + "constraints": [ + "Choose an index i and invert all characters from index 0 to index i (both inclusive), with a cost of i + 1", + "Choose an index i and invert all characters from index i to index n - 1 (both inclusive), with a cost of n - i", + "1 <= s.length == n <= 105", + "s[i] is either '0' or '1'" + ], + "python_template": "class Solution(object):\n def minimumCost(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumCost(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimumCost" + } +} \ No newline at end of file diff --git a/minimum-cost-to-make-array-equal.json b/minimum-cost-to-make-array-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..3bb51f2ff5fc5ec6335ad9c0a0225a3bb5b1db44 --- /dev/null +++ b/minimum-cost-to-make-array-equal.json @@ -0,0 +1,32 @@ +{ + "id": 2538, + "name": "minimum-cost-to-make-array-equal", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-cost-to-make-array-equal/", + "date": "2022-10-16", + "task_description": "You are given two **0-indexed** arrays `nums` and `cost` consisting each of `n` **positive** integers. You can do the following operation **any** number of times: Increase or decrease **any** element of the array `nums` by `1`. The cost of doing one operation on the `ith` element is `cost[i]`. Return _the **minimum** total cost such that all the elements of the array _`nums`_ become **equal**_. **Example 1:** ``` **Input:** nums = [1,3,5,2], cost = [2,3,1,14] **Output:** 8 **Explanation:** We can make all the elements equal to 2 in the following way: - Increase the 0th element one time. The cost is 2. - Decrease the 1st element one time. The cost is 3. - Decrease the 2nd element three times. The cost is 1 + 1 + 1 = 3. The total cost is 2 + 3 + 3 = 8. It can be shown that we cannot make the array equal with a smaller cost. ``` **Example 2:** ``` **Input:** nums = [2,2,2,2,2], cost = [4,2,8,1,3] **Output:** 0 **Explanation:** All the elements are already equal, so no operations are needed. ``` **Constraints:** `n == nums.length == cost.length` `1 <= n <= 105` `1 <= nums[i], cost[i] <= 106` Test cases are generated in a way that the output doesn't exceed 253-1", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,3,5,2], cost = [2,3,1,14]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "nums = [2,2,2,2,2], cost = [4,2,8,1,3]", + "output": "0 " + } + ], + "constraints": [ + "Increase or decrease any element of the array nums by 1.", + "n == nums.length == cost.length", + "1 <= n <= 105", + "1 <= nums[i], cost[i] <= 106", + "Test cases are generated in a way that the output doesn't exceed 253-1" + ], + "python_template": "class Solution(object):\n def minCost(self, nums, cost):\n \"\"\"\n :type nums: List[int]\n :type cost: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minCost(int[] nums, int[] cost) {\n \n }\n}", + "metadata": { + "func_name": "minCost" + } +} \ No newline at end of file diff --git a/minimum-cost-to-make-array-equalindromic.json b/minimum-cost-to-make-array-equalindromic.json new file mode 100644 index 0000000000000000000000000000000000000000..39a92ddec83779f132201043eeb946458269c8c2 --- /dev/null +++ b/minimum-cost-to-make-array-equalindromic.json @@ -0,0 +1,37 @@ +{ + "id": 3229, + "name": "minimum-cost-to-make-array-equalindromic", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-cost-to-make-array-equalindromic/", + "date": "2023-12-10", + "task_description": "You are given a **0-indexed** integer array `nums` having length `n`. You are allowed to perform a special move **any** number of times (**including zero**) on `nums`. In one **special** **move** you perform the following steps **in order**: Choose an index `i` in the range `[0, n - 1]`, and a **positive** integer `x`. Add `|nums[i] - x|` to the total cost. Change the value of `nums[i]` to `x`. A **palindromic number** is a positive integer that remains the same when its digits are reversed. For example, `121`, `2552` and `65756` are palindromic numbers whereas `24`, `46`, `235` are not palindromic numbers. An array is considered **equalindromic** if all the elements in the array are equal to an integer `y`, where `y` is a **palindromic number** less than `109`. Return _an integer denoting the **minimum** possible total cost to make _`nums`_ **equalindromic** by performing any number of special moves._ **Example 1:** ``` **Input:** nums = [1,2,3,4,5] **Output:** 6 **Explanation:** We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6. It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost. ``` **Example 2:** ``` **Input:** nums = [10,12,13,14,15] **Output:** 11 **Explanation:** We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11. It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost. ``` **Example 3:** ``` **Input:** nums = [22,33,22,33,22] **Output:** 22 **Explanation:** We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22. It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost. ``` **Constraints:** `1 <= n <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,5]", + "output": "6 " + }, + { + "label": "Example 2", + "input": "nums = [10,12,13,14,15]", + "output": "11 " + }, + { + "label": "Example 3", + "input": "nums = [22,33,22,33,22]", + "output": "22 " + } + ], + "constraints": [ + "Choose an index i in the range [0, n - 1], and a positive integer x.", + "Add |nums[i] - x| to the total cost.", + "Change the value of nums[i] to x.", + "1 <= n <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minimumCost(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumCost(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumCost" + } +} \ No newline at end of file diff --git a/minimum-cost-to-make-arrays-identical.json b/minimum-cost-to-make-arrays-identical.json new file mode 100644 index 0000000000000000000000000000000000000000..10f492153e2c4e3cfb70f7200e00fe745e7835aa --- /dev/null +++ b/minimum-cost-to-make-arrays-identical.json @@ -0,0 +1,37 @@ +{ + "id": 3712, + "name": "minimum-cost-to-make-arrays-identical", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-cost-to-make-arrays-identical/", + "date": "2025-01-04", + "task_description": "You are given two integer arrays `arr` and `brr` of length `n`, and an integer `k`. You can perform the following operations on `arr` _any_ number of times: Split `arr` into _any_ number of **contiguous** subarrays and rearrange these subarrays in _any order_. This operation has a fixed cost of `k`. Choose any element in `arr` and add or subtract a positive integer `x` to it. The cost of this operation is `x`. Return the **minimum **total cost to make `arr` **equal** to `brr`. **Example 1:** **Input:** arr = [-7,9,5], brr = [7,-2,-5], k = 2 **Output:** 13 **Explanation:** Split `arr` into two contiguous subarrays: `[-7]` and `[9, 5]` and rearrange them as `[9, 5, -7]`, with a cost of 2. Subtract 2 from element `arr[0]`. The array becomes `[7, 5, -7]`. The cost of this operation is 2. Subtract 7 from element `arr[1]`. The array becomes `[7, -2, -7]`. The cost of this operation is 7. Add 2 to element `arr[2]`. The array becomes `[7, -2, -5]`. The cost of this operation is 2. The total cost to make the arrays equal is `2 + 2 + 7 + 2 = 13`. **Example 2:** **Input:** arr = [2,1], brr = [2,1], k = 0 **Output:** 0 **Explanation:** Since the arrays are already equal, no operations are needed, and the total cost is 0. **Constraints:** `1 <= arr.length == brr.length <= 105` `0 <= k <= 2 * 1010` `-105 <= arr[i] <= 105` `-105 <= brr[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "arr = [-7,9,5], brr = [7,-2,-5], k = 2", + "output": "13 " + }, + { + "label": "Example 2", + "input": "arr = [2,1], brr = [2,1], k = 0", + "output": "0 " + } + ], + "constraints": [ + "Split arr into any number of contiguous subarrays and rearrange these subarrays in any order. This operation has a fixed cost of k.", + "Choose any element in arr and add or subtract a positive integer x to it. The cost of this operation is x.", + "Split arr into two contiguous subarrays: [-7] and [9, 5] and rearrange them as [9, 5, -7], with a cost of 2.", + "Subtract 2 from element arr[0]. The array becomes [7, 5, -7]. The cost of this operation is 2.", + "Subtract 7 from element arr[1]. The array becomes [7, -2, -7]. The cost of this operation is 7.", + "Add 2 to element arr[2]. The array becomes [7, -2, -5]. The cost of this operation is 2.", + "1 <= arr.length == brr.length <= 105", + "0 <= k <= 2 * 1010", + "-105 <= arr[i] <= 105", + "-105 <= brr[i] <= 105" + ], + "python_template": "class Solution(object):\n def minCost(self, arr, brr, k):\n \"\"\"\n :type arr: List[int]\n :type brr: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minCost(int[] arr, int[] brr, long k) {\n \n }\n}", + "metadata": { + "func_name": "minCost" + } +} \ No newline at end of file diff --git a/minimum-cost-to-set-cooking-time.json b/minimum-cost-to-set-cooking-time.json new file mode 100644 index 0000000000000000000000000000000000000000..20761420c7d4dbfd9ffccc95a44e998f02cac9c9 --- /dev/null +++ b/minimum-cost-to-set-cooking-time.json @@ -0,0 +1,36 @@ +{ + "id": 2266, + "name": "minimum-cost-to-set-cooking-time", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-cost-to-set-cooking-time/", + "date": "2022-01-22", + "task_description": "A generic microwave supports cooking times for: at least `1` second. at most `99` minutes and `99` seconds. To set the cooking time, you push **at most four digits**. The microwave normalizes what you push as four digits by **prepending zeroes**. It interprets the **first** two digits as the minutes and the **last** two digits as the seconds. It then **adds** them up as the cooking time. For example, You push `9` `5` `4` (three digits). It is normalized as `0954` and interpreted as `9` minutes and `54` seconds. You push `0` `0` `0` `8` (four digits). It is interpreted as `0` minutes and `8` seconds. You push `8` `0` `9` `0`. It is interpreted as `80` minutes and `90` seconds. You push `8` `1` `3` `0`. It is interpreted as `81` minutes and `30` seconds. You are given integers `startAt`, `moveCost`, `pushCost`, and `targetSeconds`. **Initially**, your finger is on the digit `startAt`. Moving the finger above **any specific digit** costs `moveCost` units of fatigue. Pushing the digit below the finger **once** costs `pushCost` units of fatigue. There can be multiple ways to set the microwave to cook for `targetSeconds` seconds but you are interested in the way with the minimum cost. Return _the **minimum cost** to set_ `targetSeconds` _seconds of cooking time_. Remember that one minute consists of `60` seconds. **Example 1:** ``` **Input:** startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600 **Output:** 6 **Explanation:** The following are the possible ways to set the cooking time. - 1 0 0 0, interpreted as 10 minutes and 0 seconds. The finger is already on digit 1, pushes 1 (with cost 1), moves to 0 (with cost 2), pushes 0 (with cost 1), pushes 0 (with cost 1), and pushes 0 (with cost 1). The cost is: 1 + 2 + 1 + 1 + 1 = 6. This is the minimum cost. - 0 9 6 0, interpreted as 9 minutes and 60 seconds. That is also 600 seconds. The finger moves to 0 (with cost 2), pushes 0 (with cost 1), moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1). The cost is: 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12. - 9 6 0, normalized as 0960 and interpreted as 9 minutes and 60 seconds. The finger moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1). The cost is: 2 + 1 + 2 + 1 + 2 + 1 = 9. ``` **Example 2:** ``` **Input:** startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76 **Output:** 6 **Explanation:** The optimal way is to push two digits: 7 6, interpreted as 76 seconds. The finger moves to 7 (with cost 1), pushes 7 (with cost 2), moves to 6 (with cost 1), and pushes 6 (with cost 2). The total cost is: 1 + 2 + 1 + 2 = 6 Note other possible ways are 0076, 076, 0116, and 116, but none of them produces the minimum cost. ``` **Constraints:** `0 <= startAt <= 9` `1 <= moveCost, pushCost <= 105` `1 <= targetSeconds <= 6039`", + "test_case": [ + { + "label": "Example 1", + "input": "startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600", + "output": "6 " + }, + { + "label": "Example 2", + "input": "startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76", + "output": "6 " + } + ], + "constraints": [ + "at least 1 second.", + "at most 99 minutes and 99 seconds.", + "You push 9 5 4 (three digits). It is normalized as 0954 and interpreted as 9 minutes and 54 seconds.", + "You push 0 0 0 8 (four digits). It is interpreted as 0 minutes and 8 seconds.", + "You push 8 0 9 0. It is interpreted as 80 minutes and 90 seconds.", + "You push 8 1 3 0. It is interpreted as 81 minutes and 30 seconds.", + "0 <= startAt <= 9", + "1 <= moveCost, pushCost <= 105", + "1 <= targetSeconds <= 6039" + ], + "python_template": "class Solution(object):\n def minCostSetTime(self, startAt, moveCost, pushCost, targetSeconds):\n \"\"\"\n :type startAt: int\n :type moveCost: int\n :type pushCost: int\n :type targetSeconds: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n }\n}", + "metadata": { + "func_name": "minCostSetTime" + } +} \ No newline at end of file diff --git a/minimum-cost-to-split-an-array.json b/minimum-cost-to-split-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..fd10e9903ca24acd31a3f887243708416d6cfa66 --- /dev/null +++ b/minimum-cost-to-split-an-array.json @@ -0,0 +1,37 @@ +{ + "id": 2633, + "name": "minimum-cost-to-split-an-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-cost-to-split-an-array/", + "date": "2023-01-15", + "task_description": "You are given an integer array `nums` and an integer `k`. Split the array into some number of non-empty subarrays. The **cost** of a split is the sum of the **importance value** of each subarray in the split. Let `trimmed(subarray)` be the version of the subarray where all numbers which appear only once are removed. For example, `trimmed([3,1,2,4,3,4]) = [3,4,3,4].` The **importance value** of a subarray is `k + trimmed(subarray).length`. For example, if a subarray is `[1,2,3,3,3,4,4]`, then trimmed(`[1,2,3,3,3,4,4]) = [3,3,3,4,4].`The importance value of this subarray will be `k + 5`. Return _the minimum possible cost of a split of _`nums`. A **subarray** is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [1,2,1,2,1,3,3], k = 2 **Output:** 8 **Explanation:** We split nums to have two subarrays: [1,2], [1,2,1,3,3]. The importance value of [1,2] is 2 + (0) = 2. The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6. The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits. ``` **Example 2:** ``` **Input:** nums = [1,2,1,2,1], k = 2 **Output:** 6 **Explanation:** We split nums to have two subarrays: [1,2], [1,2,1]. The importance value of [1,2] is 2 + (0) = 2. The importance value of [1,2,1] is 2 + (2) = 4. The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits. ``` **Example 3:** ``` **Input:** nums = [1,2,1,2,1], k = 5 **Output:** 10 **Explanation:** We split nums to have one subarray: [1,2,1,2,1]. The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10. The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits. ``` **Constraints:** `1 <= nums.length <= 1000` `0 <= nums[i] < nums.length` `1 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,1,2,1,3,3], k = 2", + "output": "8 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,1,2,1], k = 2", + "output": "6 " + }, + { + "label": "Example 3", + "input": "nums = [1,2,1,2,1], k = 5", + "output": "10 " + } + ], + "constraints": [ + "For example, trimmed([3,1,2,4,3,4]) = [3,4,3,4].", + "For example, if a subarray is [1,2,3,3,3,4,4], then trimmed([1,2,3,3,3,4,4]) = [3,3,3,4,4].The importance value of this subarray will be k + 5.", + "1 <= nums.length <= 1000", + "0 <= nums[i] < nums.length", + "1 <= k <= 109" + ], + "python_template": "class Solution(object):\n def minCost(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 minCost(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minCost" + } +} \ No newline at end of file diff --git a/minimum-deletions-to-make-array-divisible.json b/minimum-deletions-to-make-array-divisible.json new file mode 100644 index 0000000000000000000000000000000000000000..1e970d06624f64a11ebc767fc4f73ccb6bf8a7b2 --- /dev/null +++ b/minimum-deletions-to-make-array-divisible.json @@ -0,0 +1,29 @@ +{ + "id": 2423, + "name": "minimum-deletions-to-make-array-divisible", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/", + "date": "2022-07-10", + "task_description": "You are given two positive integer arrays `nums` and `numsDivide`. You can delete any number of elements from `nums`. Return _the **minimum** number of deletions such that the **smallest** element in _`nums`_ **divides** all the elements of _`numsDivide`. If this is not possible, return `-1`. Note that an integer `x` divides `y` if `y % x == 0`. **Example 1:** ``` **Input:** nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15] **Output:** 2 **Explanation:** The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide. We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums = [3,4,3]. The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide. It can be shown that 2 is the minimum number of deletions needed. ``` **Example 2:** ``` **Input:** nums = [4,3,6], numsDivide = [8,2,6,10] **Output:** -1 **Explanation:** We want the smallest element in nums to divide all the elements of numsDivide. There is no way to delete elements from nums to allow this. ``` **Constraints:** `1 <= nums.length, numsDivide.length <= 105` `1 <= nums[i], numsDivide[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,3,6], numsDivide = [8,2,6,10]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums.length, numsDivide.length <= 105", + "1 <= nums[i], numsDivide[i] <= 109" + ], + "python_template": "class Solution(object):\n def minOperations(self, nums, numsDivide):\n \"\"\"\n :type nums: List[int]\n :type numsDivide: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minOperations(int[] nums, int[] numsDivide) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-deletions-to-make-string-k-special.json b/minimum-deletions-to-make-string-k-special.json new file mode 100644 index 0000000000000000000000000000000000000000..c7c47a722e395751e559bc30f415ff566aec6cff --- /dev/null +++ b/minimum-deletions-to-make-string-k-special.json @@ -0,0 +1,35 @@ +{ + "id": 3360, + "name": "minimum-deletions-to-make-string-k-special", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-deletions-to-make-string-k-special/", + "date": "2024-03-10", + "task_description": "You are given a string `word` and an integer `k`. We consider `word` to be **k-special** if `|freq(word[i]) - freq(word[j])| <= k` for all indices `i` and `j` in the string. Here, `freq(x)` denotes the frequency of the character `x` in `word`, and `|y|` denotes the absolute value of `y`. Return _the **minimum** number of characters you need to delete to make_ `word` **_k-special_**. **Example 1:** **Input: **word = \"aabcaba\", k = 0 **Output: **3 **Explanation:** We can make `word` `0`-special by deleting `2` occurrences of `\"a\"` and `1` occurrence of `\"c\"`. Therefore, `word` becomes equal to `\"baba\"` where `freq('a') == freq('b') == 2`. **Example 2:** **Input: **word = \"dabdcbdcdcd\", k = 2 **Output: **2 **Explanation:** We can make `word` `2`-special by deleting `1` occurrence of `\"a\"` and `1` occurrence of `\"d\"`. Therefore, `word` becomes equal to \"bdcbdcdcd\" where `freq('b') == 2`, `freq('c') == 3`, and `freq('d') == 4`. **Example 3:** **Input: **word = \"aaabaaa\", k = 2 **Output: **1 **Explanation:** We can make `word` `2`-special by deleting `1` occurrence of `\"b\"`. Therefore, `word` becomes equal to `\"aaaaaa\"` where each letter's frequency is now uniformly `6`. **Constraints:** `1 <= word.length <= 105` `0 <= k <= 105` `word` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"aabcaba\", k = 0", + "output": "3 " + }, + { + "label": "Example 2", + "input": "word = \"dabdcbdcdcd\", k = 2", + "output": "2 " + }, + { + "label": "Example 3", + "input": "word = \"aaabaaa\", k = 2", + "output": "1 " + } + ], + "constraints": [ + "1 <= word.length <= 105", + "0 <= k <= 105", + "word consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumDeletions(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumDeletions(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumDeletions" + } +} \ No newline at end of file diff --git a/minimum-difference-in-sums-after-removal-of-elements.json b/minimum-difference-in-sums-after-removal-of-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..58f68b3e92a3818c4f3ac30721584782093b7c28 --- /dev/null +++ b/minimum-difference-in-sums-after-removal-of-elements.json @@ -0,0 +1,34 @@ +{ + "id": 2267, + "name": "minimum-difference-in-sums-after-removal-of-elements", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/", + "date": "2022-01-22", + "task_description": "You are given a **0-indexed** integer array `nums` consisting of `3 * n` elements. You are allowed to remove any **subsequence** of elements of size **exactly** `n` from `nums`. The remaining `2 * n` elements will be divided into two **equal** parts: The first `n` elements belonging to the first part and their sum is `sumfirst`. The next `n` elements belonging to the second part and their sum is `sumsecond`. The **difference in sums** of the two parts is denoted as `sumfirst - sumsecond`. For example, if `sumfirst = 3` and `sumsecond = 2`, their difference is `1`. Similarly, if `sumfirst = 2` and `sumsecond = 3`, their difference is `-1`. Return _the **minimum difference** possible between the sums of the two parts after the removal of _`n`_ elements_. **Example 1:** ``` **Input:** nums = [3,1,2] **Output:** -1 **Explanation:** Here, nums has 3 elements, so n = 1. Thus we have to remove 1 element from nums and divide the array into two equal parts. - If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two parts will be 1 - 2 = -1. - If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two parts will be 3 - 2 = 1. - If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two parts will be 3 - 1 = 2. The minimum difference between sums of the two parts is min(-1,1,2) = -1. ``` **Example 2:** ``` **Input:** nums = [7,9,5,8,1,3] **Output:** 1 **Explanation:** Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each. If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The difference in sums will be (7+9) - (1+3) = 12. To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) - (8+3) = 1. It can be shown that it is not possible to obtain a difference smaller than 1. ``` **Constraints:** `nums.length == 3 * n` `1 <= n <= 105` `1 <= nums[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,1,2]", + "output": "-1 " + }, + { + "label": "Example 2", + "input": "nums = [7,9,5,8,1,3]", + "output": "1 " + } + ], + "constraints": [ + "The first n elements belonging to the first part and their sum is sumfirst.", + "The next n elements belonging to the second part and their sum is sumsecond.", + "For example, if sumfirst = 3 and sumsecond = 2, their difference is 1.", + "Similarly, if sumfirst = 2 and sumsecond = 3, their difference is -1.", + "nums.length == 3 * n", + "1 <= n <= 105", + "1 <= nums[i] <= 105" + ], + "python_template": "class Solution(object):\n def minimumDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumDifference(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumDifference" + } +} \ No newline at end of file diff --git a/minimum-edge-reversals-so-every-node-is-reachable.json b/minimum-edge-reversals-so-every-node-is-reachable.json new file mode 100644 index 0000000000000000000000000000000000000000..5a62c4534cb9979a091db81c2ae020f33f0eb323 --- /dev/null +++ b/minimum-edge-reversals-so-every-node-is-reachable.json @@ -0,0 +1,34 @@ +{ + "id": 3105, + "name": "minimum-edge-reversals-so-every-node-is-reachable", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-edge-reversals-so-every-node-is-reachable/", + "date": "2023-09-02", + "task_description": "There is a **simple directed graph** with `n` nodes labeled from `0` to `n - 1`. The graph would form a **tree** if its edges were bi-directional. You are given an integer `n` and a **2D** integer array `edges`, where `edges[i] = [ui, vi]` represents a **directed edge** going from node `ui` to node `vi`. An **edge reversal** changes the direction of an edge, i.e., a directed edge going from node `ui` to node `vi` becomes a directed edge going from node `vi` to node `ui`. For every node `i` in the range `[0, n - 1]`, your task is to **independently** calculate the **minimum** number of **edge reversals** required so it is possible to reach any other node starting from node `i` through a **sequence** of **directed edges**. Return _an integer array _`answer`_, where _`answer[i]`_ is the__ _ _**minimum** number of **edge reversals** required so it is possible to reach any other node starting from node _`i`_ through a **sequence** of **directed edges**._ **Example 1:** ``` **Input:** n = 4, edges = [[2,0],[2,1],[1,3]] **Output:** [1,1,0,2] **Explanation:** The image above shows the graph formed by the edges. For node 0: after reversing the edge [2,0], it is possible to reach any other node starting from node 0. So, answer[0] = 1. For node 1: after reversing the edge [2,1], it is possible to reach any other node starting from node 1. So, answer[1] = 1. For node 2: it is already possible to reach any other node starting from node 2. So, answer[2] = 0. For node 3: after reversing the edges [1,3] and [2,1], it is possible to reach any other node starting from node 3. So, answer[3] = 2. ``` **Example 2:** ``` **Input:** n = 3, edges = [[1,2],[2,0]] **Output:** [2,0,1] **Explanation:** The image above shows the graph formed by the edges. For node 0: after reversing the edges [2,0] and [1,2], it is possible to reach any other node starting from node 0. So, answer[0] = 2. For node 1: it is already possible to reach any other node starting from node 1. So, answer[1] = 0. For node 2: after reversing the edge [1, 2], it is possible to reach any other node starting from node 2. So, answer[2] = 1. ``` **Constraints:** `2 <= n <= 105` `edges.length == n - 1` `edges[i].length == 2` `0 <= ui == edges[i][0] < n` `0 <= vi == edges[i][1] < n` `ui != vi` The input is generated such that if the edges were bi-directional, the graph would be a tree.", + "test_case": [ + { + "label": "Example 1", + "input": "n = 4, edges = [[2,0],[2,1],[1,3]]", + "output": "[1,1,0,2] " + }, + { + "label": "Example 2", + "input": "n = 3, edges = [[1,2],[2,0]]", + "output": "[2,0,1] " + } + ], + "constraints": [ + "2 <= n <= 105", + "edges.length == n - 1", + "edges[i].length == 2", + "0 <= ui == edges[i][0] < n", + "0 <= vi == edges[i][1] < n", + "ui != vi", + "The input is generated such that if the edges were bi-directional, the graph would be a tree." + ], + "python_template": "class Solution(object):\n def minEdgeReversals(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] minEdgeReversals(int n, int[][] edges) {\n \n }\n}", + "metadata": { + "func_name": "minEdgeReversals" + } +} \ No newline at end of file diff --git a/minimum-equal-sum-of-two-arrays-after-replacing-zeros.json b/minimum-equal-sum-of-two-arrays-after-replacing-zeros.json new file mode 100644 index 0000000000000000000000000000000000000000..960a9d5b78b32d6028d3298156a6271a73619725 --- /dev/null +++ b/minimum-equal-sum-of-two-arrays-after-replacing-zeros.json @@ -0,0 +1,29 @@ +{ + "id": 3171, + "name": "minimum-equal-sum-of-two-arrays-after-replacing-zeros", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/", + "date": "2023-10-22", + "task_description": "You are given two arrays `nums1` and `nums2` consisting of positive integers. You have to replace **all** the `0`'s in both arrays with **strictly** positive integers such that the sum of elements of both arrays becomes **equal**. Return _the **minimum** equal sum you can obtain, or _`-1`_ if it is impossible_. **Example 1:** ``` **Input:** nums1 = [3,2,0,1,0], nums2 = [6,5,0] **Output:** 12 **Explanation:** We can replace 0's in the following way: - Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4]. - Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1]. Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain. ``` **Example 2:** ``` **Input:** nums1 = [2,0,2,0], nums2 = [1,4] **Output:** -1 **Explanation:** It is impossible to make the sum of both arrays equal. ``` **Constraints:** `1 <= nums1.length, nums2.length <= 105` `0 <= nums1[i], nums2[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [3,2,0,1,0], nums2 = [6,5,0]", + "output": "12 " + }, + { + "label": "Example 2", + "input": "nums1 = [2,0,2,0], nums2 = [1,4]", + "output": "-1 " + } + ], + "constraints": [ + "1 <= nums1.length, nums2.length <= 105", + "0 <= nums1[i], nums2[i] <= 106" + ], + "python_template": "class Solution(object):\n def minSum(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 minSum(int[] nums1, int[] nums2) {\n \n }\n}", + "metadata": { + "func_name": "minSum" + } +} \ No newline at end of file diff --git a/minimum-fuel-cost-to-report-to-the-capital.json b/minimum-fuel-cost-to-report-to-the-capital.json new file mode 100644 index 0000000000000000000000000000000000000000..62dd328c2a9a5d020b90bca07a813390991c7c5d --- /dev/null +++ b/minimum-fuel-cost-to-report-to-the-capital.json @@ -0,0 +1,39 @@ +{ + "id": 2568, + "name": "minimum-fuel-cost-to-report-to-the-capital", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/", + "date": "2022-11-13", + "task_description": "There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of `n` cities numbered from `0` to `n - 1` and exactly `n - 1` roads. The capital city is city `0`. You are given a 2D integer array `roads` where `roads[i] = [ai, bi]` denotes that there exists a **bidirectional road** connecting cities `ai` and `bi`. There is a meeting for the representatives of each city. The meeting is in the capital city. There is a car in each city. You are given an integer `seats` that indicates the number of seats in each car. A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel. Return _the minimum number of liters of fuel to reach the capital city_. **Example 1:** ``` **Input:** roads = [[0,1],[0,2],[0,3]], seats = 5 **Output:** 3 **Explanation:** - Representative1 goes directly to the capital with 1 liter of fuel. - Representative2 goes directly to the capital with 1 liter of fuel. - Representative3 goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. ``` **Example 2:** ``` **Input:** roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 **Output:** 7 **Explanation:** - Representative2 goes directly to city 3 with 1 liter of fuel. - Representative2 and representative3 go together to city 1 with 1 liter of fuel. - Representative2 and representative3 go together to the capital with 1 liter of fuel. - Representative1 goes directly to the capital with 1 liter of fuel. - Representative5 goes directly to the capital with 1 liter of fuel. - Representative6 goes directly to city 4 with 1 liter of fuel. - Representative4 and representative6 go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. ``` **Example 3:** ``` **Input:** roads = [], seats = 1 **Output:** 0 **Explanation:** No representatives need to travel to the capital city. ``` **Constraints:** `1 <= n <= 105` `roads.length == n - 1` `roads[i].length == 2` `0 <= ai, bi < n` `ai != bi` `roads` represents a valid tree. `1 <= seats <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "roads = [[0,1],[0,2],[0,3]], seats = 5", + "output": "3 " + }, + { + "label": "Example 2", + "input": "roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2", + "output": "7 " + }, + { + "label": "Example 3", + "input": "roads = [], seats = 1", + "output": "0 " + } + ], + "constraints": [ + "1 <= n <= 105", + "roads.length == n - 1", + "roads[i].length == 2", + "0 <= ai, bi < n", + "ai != bi", + "roads represents a valid tree.", + "1 <= seats <= 105" + ], + "python_template": "class Solution(object):\n def minimumFuelCost(self, roads, seats):\n \"\"\"\n :type roads: List[List[int]]\n :type seats: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumFuelCost(int[][] roads, int seats) {\n \n }\n}", + "metadata": { + "func_name": "minimumFuelCost" + } +} \ No newline at end of file diff --git a/minimum-hours-of-training-to-win-a-competition.json b/minimum-hours-of-training-to-win-a-competition.json new file mode 100644 index 0000000000000000000000000000000000000000..8b6438d91a08ede6b83335b234a19e24d00f9a5b --- /dev/null +++ b/minimum-hours-of-training-to-win-a-competition.json @@ -0,0 +1,30 @@ +{ + "id": 2459, + "name": "minimum-hours-of-training-to-win-a-competition", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/", + "date": "2022-08-14", + "task_description": "You are entering a competition, and are given two **positive** integers `initialEnergy` and `initialExperience` denoting your initial energy and initial experience respectively. You are also given two **0-indexed** integer arrays `energy` and `experience`, both of length `n`. You will face `n` opponents **in order**. The energy and experience of the `ith` opponent is denoted by `energy[i]` and `experience[i]` respectively. When you face an opponent, you need to have both **strictly** greater experience and energy to defeat them and move to the next opponent if available. Defeating the `ith` opponent **increases** your experience by `experience[i]`, but **decreases** your energy by `energy[i]`. Before starting the competition, you can train for some number of hours. After each hour of training, you can **either** choose to increase your initial experience by one, or increase your initial energy by one. Return _the **minimum** number of training hours required to defeat all _`n`_ opponents_. **Example 1:** ``` **Input:** initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1] **Output:** 8 **Explanation:** You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training. You face the opponents in the following order: - You have more energy and experience than the 0th opponent so you win. Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7. - You have more energy and experience than the 1st opponent so you win. Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13. - You have more energy and experience than the 2nd opponent so you win. Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16. - You have more energy and experience than the 3rd opponent so you win. Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17. You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8. It can be proven that no smaller answer exists. ``` **Example 2:** ``` **Input:** initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3] **Output:** 0 **Explanation:** You do not need any additional energy or experience to win the competition, so we return 0. ``` **Constraints:** `n == energy.length == experience.length` `1 <= n <= 100` `1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]", + "output": "8 " + }, + { + "label": "Example 2", + "input": "initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]", + "output": "0 " + } + ], + "constraints": [ + "n == energy.length == experience.length", + "1 <= n <= 100", + "1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100" + ], + "python_template": "class Solution(object):\n def minNumberOfHours(self, initialEnergy, initialExperience, energy, experience):\n \"\"\"\n :type initialEnergy: int\n :type initialExperience: int\n :type energy: List[int]\n :type experience: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {\n \n }\n}", + "metadata": { + "func_name": "minNumberOfHours" + } +} \ No newline at end of file diff --git a/minimum-impossible-or.json b/minimum-impossible-or.json new file mode 100644 index 0000000000000000000000000000000000000000..068a11aad2f9a209e56ef6abcf74ace428f8cc5c --- /dev/null +++ b/minimum-impossible-or.json @@ -0,0 +1,29 @@ +{ + "id": 2705, + "name": "minimum-impossible-or", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-impossible-or/", + "date": "2023-02-04", + "task_description": "You are given a **0-indexed** integer array `nums`. We say that an integer x is **expressible** from `nums` if there exist some integers `0 <= index1 < index2 < ... < indexk < nums.length` for which `nums[index1] | nums[index2] | ... | nums[indexk] = x`. In other words, an integer is expressible if it can be written as the bitwise OR of some subsequence of `nums`. Return _the minimum **positive non-zero integer** that is not __expressible from _`nums`. **Example 1:** ``` **Input:** nums = [2,1] **Output:** 4 **Explanation:** 1 and 2 are already present in the array. We know that 3 is expressible, since nums[0] | nums[1] = 2 | 1 = 3. Since 4 is not expressible, we return 4. ``` **Example 2:** ``` **Input:** nums = [5,3,2] **Output:** 1 **Explanation:** We can show that 1 is the smallest number that is not expressible. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [5,3,2]", + "output": "1 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minImpossibleOR(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minImpossibleOR(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "minImpossibleOR" + } +} \ No newline at end of file diff --git a/minimum-increment-operations-to-make-array-beautiful.json b/minimum-increment-operations-to-make-array-beautiful.json new file mode 100644 index 0000000000000000000000000000000000000000..84ec0aa68e349b934367e4033ddc7fe6302eb93d --- /dev/null +++ b/minimum-increment-operations-to-make-array-beautiful.json @@ -0,0 +1,36 @@ +{ + "id": 3178, + "name": "minimum-increment-operations-to-make-array-beautiful", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-increment-operations-to-make-array-beautiful/", + "date": "2023-10-22", + "task_description": "You are given a **0-indexed** integer array `nums` having length `n`, and an integer `k`. You can perform the following **increment** operation **any** number of times (**including zero**): Choose an index `i` in the range `[0, n - 1]`, and increase `nums[i]` by `1`. An array is considered **beautiful** if, for any **subarray** with a size of `3` or **more**, its **maximum** element is **greater than or equal** to `k`. Return _an integer denoting the **minimum** number of increment operations needed to make _`nums`_ **beautiful**._ A subarray is a contiguous **non-empty** sequence of elements within an array. **Example 1:** ``` **Input:** nums = [2,3,0,0,2], k = 4 **Output:** 3 **Explanation:** We can perform the following increment operations to make nums beautiful: Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2]. Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3]. Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4]. The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4]. In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful. It can be shown that nums cannot be made beautiful with fewer than 3 increment operations. Hence, the answer is 3. ``` **Example 2:** ``` **Input:** nums = [0,1,3,3], k = 5 **Output:** 2 **Explanation:** We can perform the following increment operations to make nums beautiful: Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3]. Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3]. The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3]. In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful. It can be shown that nums cannot be made beautiful with fewer than 2 increment operations. Hence, the answer is 2. ``` **Example 3:** ``` **Input:** nums = [1,1,2], k = 1 **Output:** 0 **Explanation:** The only subarray with a size of 3 or more in this example is [1,1,2]. The maximum element, 2, is already greater than k = 1, so we don't need any increment operation. Hence, the answer is 0. ``` **Constraints:** `3 <= n == nums.length <= 105` `0 <= nums[i] <= 109` `0 <= k <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,0,0,2], k = 4", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [0,1,3,3], k = 5", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [1,1,2], k = 1", + "output": "0 " + } + ], + "constraints": [ + "Choose an index i in the range [0, n - 1], and increase nums[i] by 1.", + "3 <= n == nums.length <= 105", + "0 <= nums[i] <= 109", + "0 <= k <= 109" + ], + "python_template": "class Solution(object):\n def minIncrementOperations(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 minIncrementOperations(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minIncrementOperations" + } +} \ No newline at end of file diff --git a/minimum-increments-for-target-multiples-in-an-array.json b/minimum-increments-for-target-multiples-in-an-array.json new file mode 100644 index 0000000000000000000000000000000000000000..2c4cb6ef66da60438f94b9f4f7f6a63923e9dd93 --- /dev/null +++ b/minimum-increments-for-target-multiples-in-an-array.json @@ -0,0 +1,38 @@ +{ + "id": 3697, + "name": "minimum-increments-for-target-multiples-in-an-array", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-increments-for-target-multiples-in-an-array/", + "date": "2025-01-26", + "task_description": "You are given two arrays, `nums` and `target`. In a single operation, you may increment any element of `nums` by 1. Return **the minimum number** of operations required so that each element in `target` has **at least** one multiple in `nums`. **Example 1:** **Input:** nums = [1,2,3], target = [4] **Output:** 1 **Explanation:** The minimum number of operations required to satisfy the condition is 1. Increment 3 to 4 with just one operation, making 4 a multiple of itself. **Example 2:** **Input:** nums = [8,4], target = [10,5] **Output:** 2 **Explanation:** The minimum number of operations required to satisfy the condition is 2. Increment 8 to 10 with 2 operations, making 10 a multiple of both 5 and 10. **Example 3:** **Input:** nums = [7,9,10], target = [7] **Output:** 0 **Explanation:** Target 7 already has a multiple in nums, so no additional operations are needed. **Constraints:** `1 <= nums.length <= 5 * 104` `1 <= target.length <= 4` `target.length <= nums.length` `1 <= nums[i], target[i] <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3], target = [4]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [8,4], target = [10,5]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [7,9,10], target = [7]", + "output": "0 " + } + ], + "constraints": [ + "Increment 3 to 4 with just one operation, making 4 a multiple of itself.", + "Increment 8 to 10 with 2 operations, making 10 a multiple of both 5 and 10.", + "1 <= nums.length <= 5 * 104", + "1 <= target.length <= 4", + "target.length <= nums.length", + "1 <= nums[i], target[i] <= 104" + ], + "python_template": "class Solution(object):\n def minimumIncrements(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumIncrements(int[] nums, int[] target) {\n \n }\n}", + "metadata": { + "func_name": "minimumIncrements" + } +} \ No newline at end of file diff --git a/minimum-index-of-a-valid-split.json b/minimum-index-of-a-valid-split.json new file mode 100644 index 0000000000000000000000000000000000000000..ffabbbf059813dd20e9f4a0c96acf33ed99fb407 --- /dev/null +++ b/minimum-index-of-a-valid-split.json @@ -0,0 +1,37 @@ +{ + "id": 2888, + "name": "minimum-index-of-a-valid-split", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-index-of-a-valid-split/", + "date": "2023-07-09", + "task_description": "An element `x` of an integer array `arr` of length `m` is **dominant** if **more than half** the elements of `arr` have a value of `x`. You are given a **0-indexed** integer array `nums` of length `n` with one **dominant** element. You can split `nums` at an index `i` into two arrays `nums[0, ..., i]` and `nums[i + 1, ..., n - 1]`, but the split is only **valid** if: `0 <= i < n - 1` `nums[0, ..., i]`, and `nums[i + 1, ..., n - 1]` have the same dominant element. Here, `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j`, both ends being inclusive. Particularly, if `j < i` then `nums[i, ..., j]` denotes an empty subarray. Return _the **minimum** index of a **valid split**_. If no valid split exists, return `-1`. **Example 1:** ``` **Input:** nums = [1,2,2,2] **Output:** 2 **Explanation:** We can split the array at index 2 to obtain arrays [1,2,2] and [2]. In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3. In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1. Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split. It can be shown that index 2 is the minimum index of a valid split. ``` **Example 2:** ``` **Input:** nums = [2,1,3,1,1,1,7,1,2,1] **Output:** 4 **Explanation:** We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1]. In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split. It can be shown that index 4 is the minimum index of a valid split. ``` **Example 3:** ``` **Input:** nums = [3,3,3,3,7,2,2] **Output:** -1 **Explanation:** It can be shown that there is no valid split. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109` `nums` has exactly one dominant element.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,2,2]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [2,1,3,1,1,1,7,1,2,1]", + "output": "4 " + }, + { + "label": "Example 3", + "input": "nums = [3,3,3,3,7,2,2]", + "output": "-1 " + } + ], + "constraints": [ + "0 <= i < n - 1", + "nums[0, ..., i], and nums[i + 1, ..., n - 1] have the same dominant element.", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109", + "nums has exactly one dominant element." + ], + "python_template": "class Solution(object):\n def minimumIndex(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumIndex(List nums) {\n \n }\n}", + "metadata": { + "func_name": "minimumIndex" + } +} \ No newline at end of file diff --git a/minimum-length-of-anagram-concatenation.json b/minimum-length-of-anagram-concatenation.json new file mode 100644 index 0000000000000000000000000000000000000000..534545497ce01d8d3d0a7d5d724f6830c210b2be --- /dev/null +++ b/minimum-length-of-anagram-concatenation.json @@ -0,0 +1,34 @@ +{ + "id": 3395, + "name": "minimum-length-of-anagram-concatenation", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-length-of-anagram-concatenation/", + "date": "2024-04-28", + "task_description": "You are given a string `s`, which is known to be a concatenation of **anagrams** of some string `t`. Return the **minimum** possible length of the string `t`. An **anagram** is formed by rearranging the letters of a string. For example, \"aab\", \"aba\", and, \"baa\" are anagrams of \"aab\". **Example 1:** **Input:** s = \"abba\" **Output:** 2 **Explanation:** One possible string `t` could be `\"ba\"`. **Example 2:** **Input:** s = \"cdef\" **Output:** 4 **Explanation:** One possible string `t` could be `\"cdef\"`, notice that `t` can be equal to `s`. **Example 2:** **Input:** s = \"abcbcacabbaccba\" **Output:** 3 **Constraints:** `1 <= s.length <= 105` `s` consist only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abba\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"cdef\"", + "output": "4 " + }, + { + "label": "Example 2", + "input": "s = \"abcbcacabbaccba\"", + "output": "" + } + ], + "constraints": [ + "1 <= s.length <= 105", + "s consist only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minAnagramLength(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minAnagramLength(String s) {\n \n }\n}", + "metadata": { + "func_name": "minAnagramLength" + } +} \ No newline at end of file diff --git a/minimum-length-of-string-after-operations.json b/minimum-length-of-string-after-operations.json new file mode 100644 index 0000000000000000000000000000000000000000..4a38497d39d77f6414a7614168d91f1e1c122f25 --- /dev/null +++ b/minimum-length-of-string-after-operations.json @@ -0,0 +1,34 @@ +{ + "id": 3455, + "name": "minimum-length-of-string-after-operations", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-length-of-string-after-operations/", + "date": "2024-07-06", + "task_description": "You are given a string `s`. You can perform the following process on `s` **any** number of times: Choose an index `i` in the string such that there is **at least** one character to the left of index `i` that is equal to `s[i]`, and **at least** one character to the right that is also equal to `s[i]`. Delete the **closest** occurrence of `s[i]` located to the **left** of `i`. Delete the **closest** occurrence of `s[i]` located to the **right** of `i`. Return the **minimum** length of the final string `s` that you can achieve. **Example 1:** **Input:** s = \"abaacbcbb\" **Output:** 5 **Explanation:** We do the following operations: Choose index 2, then remove the characters at indices 0 and 3. The resulting string is `s = \"bacbcbb\"`. Choose index 3, then remove the characters at indices 0 and 5. The resulting string is `s = \"acbcb\"`. **Example 2:** **Input:** s = \"aa\" **Output:** 2 **Explanation:** We cannot perform any operations, so we return the length of the original string. **Constraints:** `1 <= s.length <= 2 * 105` `s` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"abaacbcbb\"", + "output": "5 " + }, + { + "label": "Example 2", + "input": "s = \"aa\"", + "output": "2 " + } + ], + "constraints": [ + "Choose an index i in the string such that there is at least one character to the left of index i that is equal to s[i], and at least one character to the right that is also equal to s[i].", + "Delete the closest occurrence of s[i] located to the left of i.", + "Delete the closest occurrence of s[i] located to the right of i.", + "Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = \"bacbcbb\".", + "Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = \"acbcb\".", + "1 <= s.length <= 2 * 105", + "s consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumLength(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumLength(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimumLength" + } +} \ No newline at end of file diff --git a/minimum-levels-to-gain-more-points.json b/minimum-levels-to-gain-more-points.json new file mode 100644 index 0000000000000000000000000000000000000000..7032e2f16683f695d69dd435a672cabab10a69d5 --- /dev/null +++ b/minimum-levels-to-gain-more-points.json @@ -0,0 +1,41 @@ +{ + "id": 3355, + "name": "minimum-levels-to-gain-more-points", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-levels-to-gain-more-points/", + "date": "2024-03-16", + "task_description": "You are given a binary array `possible` of length `n`. Alice and Bob are playing a game that consists of `n` levels. Some of the levels in the game are **impossible** to clear while others can **always** be cleared. In particular, if `possible[i] == 0`, then the `ith` level is **impossible** to clear for **both** the players. A player gains `1` point on clearing a level and loses `1` point if the player fails to clear it. At the start of the game, Alice will play some levels in the **given order** starting from the `0th` level, after which Bob will play for the rest of the levels. Alice wants to know the **minimum** number of levels she should play to gain more points than Bob, if both players play optimally to **maximize** their points. Return _the **minimum** number of levels Alice should play to gain more points_. _If this is **not** possible, return_ `-1`. **Note** that each player must play at least `1` level. **Example 1:** **Input:** possible = [1,0,1,0] **Output:** 1 **Explanation:** Let's look at all the levels that Alice can play up to: If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has -1 + 1 - 1 = -1 point. If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points. If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 1 - 1 + 1 = 1 point, while Bob has -1 point. Alice must play a minimum of 1 level to gain more points. **Example 2:** **Input:** possible = [1,1,1,1,1] **Output:** 3 **Explanation:** Let's look at all the levels that Alice can play up to: If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points. If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points. If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points. If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point. Alice must play a minimum of 3 levels to gain more points. **Example 3:** **Input:** possible = [0,0] **Output:** -1 **Explanation:** The only possible way is for both players to play 1 level each. Alice plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Alice can't gain more points than Bob. **Constraints:** `2 <= n == possible.length <= 105` `possible[i]` is either `0` or `1`.", + "test_case": [ + { + "label": "Example 1", + "input": "possible = [1,0,1,0]", + "output": "1 " + }, + { + "label": "Example 2", + "input": "possible = [1,1,1,1,1]", + "output": "3 " + }, + { + "label": "Example 3", + "input": "possible = [0,0]", + "output": "-1 " + } + ], + "constraints": [ + "If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has -1 + 1 - 1 = -1 point.", + "If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points.", + "If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 1 - 1 + 1 = 1 point, while Bob has -1 point.", + "If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points.", + "If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points.", + "If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points.", + "If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point.", + "2 <= n == possible.length <= 105", + "possible[i] is either 0 or 1." + ], + "python_template": "class Solution(object):\n def minimumLevels(self, possible):\n \"\"\"\n :type possible: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumLevels(int[] possible) {\n \n }\n}", + "metadata": { + "func_name": "minimumLevels" + } +} \ No newline at end of file diff --git a/minimum-lines-to-represent-a-line-chart.json b/minimum-lines-to-represent-a-line-chart.json new file mode 100644 index 0000000000000000000000000000000000000000..2cd0cf84ea4934ab4e6b03c579844155d3ddf1db --- /dev/null +++ b/minimum-lines-to-represent-a-line-chart.json @@ -0,0 +1,31 @@ +{ + "id": 2367, + "name": "minimum-lines-to-represent-a-line-chart", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/", + "date": "2022-05-15", + "task_description": "You are given a 2D integer array `stockPrices` where `stockPrices[i] = [dayi, pricei]` indicates the price of the stock on day `dayi` is `pricei`. A **line chart** is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price and connecting adjacent points. One such example is shown below: Return _the **minimum number of lines** needed to represent the line chart_. **Example 1:** ``` **Input:** stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]] **Output:** 3 **Explanation:** The diagram above represents the input, with the X-axis representing the day and Y-axis representing the price. The following 3 lines can be drawn to represent the line chart: - Line 1 (in red) from (1,7) to (4,4) passing through (1,7), (2,6), (3,5), and (4,4). - Line 2 (in blue) from (4,4) to (5,4). - Line 3 (in green) from (5,4) to (8,1) passing through (5,4), (6,3), (7,2), and (8,1). It can be shown that it is not possible to represent the line chart using less than 3 lines. ``` **Example 2:** ``` **Input:** stockPrices = [[3,4],[1,2],[7,8],[2,3]] **Output:** 1 **Explanation:** As shown in the diagram above, the line chart can be represented with a single line. ``` **Constraints:** `1 <= stockPrices.length <= 105` `stockPrices[i].length == 2` `1 <= dayi, pricei <= 109` All `dayi` are **distinct**.", + "test_case": [ + { + "label": "Example 1", + "input": "stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "stockPrices = [[3,4],[1,2],[7,8],[2,3]]", + "output": "1 " + } + ], + "constraints": [ + "1 <= stockPrices.length <= 105", + "stockPrices[i].length == 2", + "1 <= dayi, pricei <= 109", + "All dayi are distinct." + ], + "python_template": "class Solution(object):\n def minimumLines(self, stockPrices):\n \"\"\"\n :type stockPrices: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumLines(int[][] stockPrices) {\n \n }\n}", + "metadata": { + "func_name": "minimumLines" + } +} \ No newline at end of file diff --git a/minimum-money-required-before-transactions.json b/minimum-money-required-before-transactions.json new file mode 100644 index 0000000000000000000000000000000000000000..31d0da5dd14da44f4f96c3901993e9e0f2f50d39 --- /dev/null +++ b/minimum-money-required-before-transactions.json @@ -0,0 +1,30 @@ +{ + "id": 2499, + "name": "minimum-money-required-before-transactions", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-money-required-before-transactions/", + "date": "2022-09-03", + "task_description": "You are given a **0-indexed** 2D integer array `transactions`, where `transactions[i] = [costi, cashbacki]`. The array describes transactions, where each transaction must be completed exactly once in **some order**. At any given moment, you have a certain amount of `money`. In order to complete transaction `i`, `money >= costi` must hold true. After performing a transaction, `money` becomes `money - costi + cashbacki`. Return_ the minimum amount of _`money`_ required before any transaction so that all of the transactions can be completed **regardless of the order** of the transactions._ **Example 1:** ``` **Input:** transactions = [[2,1],[5,0],[4,2]] **Output:** 10 **Explanation: **Starting with money = 10, the transactions can be performed in any order. It can be shown that starting with money < 10 will fail to complete all transactions in some order. ``` **Example 2:** ``` **Input:** transactions = [[3,0],[0,3]] **Output:** 3 **Explanation:** - If transactions are in the order [[3,0],[0,3]], the minimum money required to complete the transactions is 3. - If transactions are in the order [[0,3],[3,0]], the minimum money required to complete the transactions is 0. Thus, starting with money = 3, the transactions can be performed in any order. ``` **Constraints:** `1 <= transactions.length <= 105` `transactions[i].length == 2` `0 <= costi, cashbacki <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "transactions = [[2,1],[5,0],[4,2]]", + "output": "10 " + }, + { + "label": "Example 2", + "input": "transactions = [[3,0],[0,3]]", + "output": "3 " + } + ], + "constraints": [ + "1 <= transactions.length <= 105", + "transactions[i].length == 2", + "0 <= costi, cashbacki <= 109" + ], + "python_template": "class Solution(object):\n def minimumMoney(self, transactions):\n \"\"\"\n :type transactions: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumMoney(int[][] transactions) {\n \n }\n}", + "metadata": { + "func_name": "minimumMoney" + } +} \ No newline at end of file diff --git a/minimum-moves-to-capture-the-queen.json b/minimum-moves-to-capture-the-queen.json new file mode 100644 index 0000000000000000000000000000000000000000..ab90b35abf57ec960ea480d26c3e7e58e6fd8f05 --- /dev/null +++ b/minimum-moves-to-capture-the-queen.json @@ -0,0 +1,36 @@ +{ + "id": 3270, + "name": "minimum-moves-to-capture-the-queen", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-moves-to-capture-the-queen/", + "date": "2023-12-31", + "task_description": "There is a **1-indexed** `8 x 8` chessboard containing `3` pieces. You are given `6` integers `a`, `b`, `c`, `d`, `e`, and `f` where: `(a, b)` denotes the position of the white rook. `(c, d)` denotes the position of the white bishop. `(e, f)` denotes the position of the black queen. Given that you can only move the white pieces, return _the **minimum** number of moves required to capture the black queen_. **Note** that: Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces. Bishops can move any number of squares diagonally, but cannot jump over other pieces. A rook or a bishop can capture the queen if it is located in a square that they can move to. The queen does not move. **Example 1:** ``` **Input:** a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 **Output:** 2 **Explanation:** We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3). It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning. ``` **Example 2:** ``` **Input:** a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 **Output:** 1 **Explanation:** We can capture the black queen in a single move by doing one of the following: - Move the white rook to (5, 2). - Move the white bishop to (5, 2). ``` **Constraints:** `1 <= a, b, c, d, e, f <= 8` No two pieces are on the same square.", + "test_case": [ + { + "label": "Example 1", + "input": "a = 1, b = 1, c = 8, d = 8, e = 2, f = 3", + "output": "2 " + }, + { + "label": "Example 2", + "input": "a = 5, b = 3, c = 3, d = 4, e = 5, f = 2", + "output": "1 " + } + ], + "constraints": [ + "(a, b) denotes the position of the white rook.", + "(c, d) denotes the position of the white bishop.", + "(e, f) denotes the position of the black queen.", + "Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.", + "Bishops can move any number of squares diagonally, but cannot jump over other pieces.", + "A rook or a bishop can capture the queen if it is located in a square that they can move to.", + "The queen does not move.", + "1 <= a, b, c, d, e, f <= 8", + "No two pieces are on the same square." + ], + "python_template": "class Solution(object):\n def minMovesToCaptureTheQueen(self, a, b, c, d, e, f):\n \"\"\"\n :type a: int\n :type b: int\n :type c: int\n :type d: int\n :type e: int\n :type f: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {\n \n }\n}", + "metadata": { + "func_name": "minMovesToCaptureTheQueen" + } +} \ No newline at end of file diff --git a/minimum-moves-to-pick-k-ones.json b/minimum-moves-to-pick-k-ones.json new file mode 100644 index 0000000000000000000000000000000000000000..a3b55f5bb0dd220c71eaf894251408012651d597 --- /dev/null +++ b/minimum-moves-to-pick-k-ones.json @@ -0,0 +1,42 @@ +{ + "id": 3327, + "name": "minimum-moves-to-pick-k-ones", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-moves-to-pick-k-ones/", + "date": "2024-03-10", + "task_description": "You are given a binary array `nums` of length `n`, a **positive** integer `k` and a **non-negative** integer `maxChanges`. Alice plays a game, where the goal is for Alice to pick up `k` ones from `nums` using the **minimum** number of **moves**. When the game starts, Alice picks up any index `aliceIndex` in the range `[0, n - 1]` and stands there. If `nums[aliceIndex] == 1` , Alice picks up the one and `nums[aliceIndex]` becomes `0`(this **does not** count as a move). After this, Alice can make **any** number of **moves** (**including** **zero**) where in each move Alice must perform **exactly** one of the following actions: Select any index `j != aliceIndex` such that `nums[j] == 0` and set `nums[j] = 1`. This action can be performed **at** **most** `maxChanges` times. Select any two adjacent indices `x` and `y` (`|x - y| == 1`) such that `nums[x] == 1`, `nums[y] == 0`, then swap their values (set `nums[y] = 1` and `nums[x] = 0`). If `y == aliceIndex`, Alice picks up the one after this move and `nums[y]` becomes `0`. Return _the **minimum** number of moves required by Alice to pick **exactly **_`k` _ones_. **Example 1:** **Input: **nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1 **Output: **3 **Explanation:** Alice can pick up `3` ones in `3` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 1`: At the start of the game Alice picks up the one and `nums[1]` becomes `0`. `nums` becomes `[1,**0**,0,0,0,1,1,0,0,1]`. Select `j == 2` and perform an action of the first type. `nums` becomes `[1,**0**,1,0,0,1,1,0,0,1]` Select `x == 2` and `y == 1`, and perform an action of the second type. `nums` becomes `[1,**1**,0,0,0,1,1,0,0,1]`. As `y == aliceIndex`, Alice picks up the one and `nums` becomes `[1,**0**,0,0,0,1,1,0,0,1]`. Select `x == 0` and `y == 1`, and perform an action of the second type. `nums` becomes `[0,**1**,0,0,0,1,1,0,0,1]`. As `y == aliceIndex`, Alice picks up the one and `nums` becomes `[0,**0**,0,0,0,1,1,0,0,1]`. Note that it may be possible for Alice to pick up `3` ones using some other sequence of `3` moves. **Example 2:** **Input: **nums = [0,0,0,0], k = 2, maxChanges = 3 **Output: **4 **Explanation:** Alice can pick up `2` ones in `4` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 0`: Select `j == 1` and perform an action of the first type. `nums` becomes `[**0**,1,0,0]`. Select `x == 1` and `y == 0`, and perform an action of the second type. `nums` becomes `[**1**,0,0,0]`. As `y == aliceIndex`, Alice picks up the one and `nums` becomes `[**0**,0,0,0]`. Select `j == 1` again and perform an action of the first type. `nums` becomes `[**0**,1,0,0]`. Select `x == 1` and `y == 0` again, and perform an action of the second type. `nums` becomes `[**1**,0,0,0]`. As `y == aliceIndex`, Alice picks up the one and `nums` becomes `[**0**,0,0,0]`. **Constraints:** `2 <= n <= 105` `0 <= nums[i] <= 1` `1 <= k <= 105` `0 <= maxChanges <= 105` `maxChanges + sum(nums) >= k`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [0,0,0,0], k = 2, maxChanges = 3", + "output": "4 " + } + ], + "constraints": [ + "Select any index j != aliceIndex such that nums[j] == 0 and set nums[j] = 1. This action can be performed at most maxChanges times.", + "Select any two adjacent indices x and y (|x - y| == 1) such that nums[x] == 1, nums[y] == 0, then swap their values (set nums[y] = 1 and nums[x] = 0). If y == aliceIndex, Alice picks up the one after this move and nums[y] becomes 0.", + "At the start of the game Alice picks up the one and nums[1] becomes 0. nums becomes [1,0,0,0,0,1,1,0,0,1].", + "Select j == 2 and perform an action of the first type. nums becomes [1,0,1,0,0,1,1,0,0,1]", + "Select x == 2 and y == 1, and perform an action of the second type. nums becomes [1,1,0,0,0,1,1,0,0,1]. As y == aliceIndex, Alice picks up the one and nums becomes [1,0,0,0,0,1,1,0,0,1].", + "Select x == 0 and y == 1, and perform an action of the second type. nums becomes [0,1,0,0,0,1,1,0,0,1]. As y == aliceIndex, Alice picks up the one and nums becomes [0,0,0,0,0,1,1,0,0,1].", + "Select j == 1 and perform an action of the first type. nums becomes [0,1,0,0].", + "Select x == 1 and y == 0, and perform an action of the second type. nums becomes [1,0,0,0]. As y == aliceIndex, Alice picks up the one and nums becomes [0,0,0,0].", + "Select j == 1 again and perform an action of the first type. nums becomes [0,1,0,0].", + "Select x == 1 and y == 0 again, and perform an action of the second type. nums becomes [1,0,0,0]. As y == aliceIndex, Alice picks up the one and nums becomes [0,0,0,0].", + "2 <= n <= 105", + "0 <= nums[i] <= 1", + "1 <= k <= 105", + "0 <= maxChanges <= 105", + "maxChanges + sum(nums) >= k" + ], + "python_template": "class Solution(object):\n def minimumMoves(self, nums, k, maxChanges):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type maxChanges: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumMoves(int[] nums, int k, int maxChanges) {\n \n }\n}", + "metadata": { + "func_name": "minimumMoves" + } +} \ No newline at end of file diff --git a/minimum-number-game.json b/minimum-number-game.json new file mode 100644 index 0000000000000000000000000000000000000000..3e6c3b3f82f935b0d13dbcfb1d81155ee29dbde3 --- /dev/null +++ b/minimum-number-game.json @@ -0,0 +1,33 @@ +{ + "id": 3226, + "name": "minimum-number-game", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-number-game/", + "date": "2023-12-17", + "task_description": "You are given a **0-indexed** integer array `nums` of **even** length and there is also an empty array `arr`. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows: Every round, first Alice will remove the **minimum** element from `nums`, and then Bob does the same. Now, first Bob will append the removed element in the array `arr`, and then Alice does the same. The game continues until `nums` becomes empty. Return _the resulting array _`arr`. **Example 1:** ``` **Input:** nums = [5,4,2,3] **Output:** [3,2,5,4] **Explanation:** In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2]. At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4]. ``` **Example 2:** ``` **Input:** nums = [2,5] **Output:** [5,2] **Explanation:** In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2]. ``` **Constraints:** `2 <= nums.length <= 100` `1 <= nums[i] <= 100` `nums.length % 2 == 0`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,4,2,3]", + "output": "[3,2,5,4] " + }, + { + "label": "Example 2", + "input": "nums = [2,5]", + "output": "[5,2] " + } + ], + "constraints": [ + "Every round, first Alice will remove the minimum element from nums, and then Bob does the same.", + "Now, first Bob will append the removed element in the array arr, and then Alice does the same.", + "The game continues until nums becomes empty.", + "2 <= nums.length <= 100", + "1 <= nums[i] <= 100", + "nums.length % 2 == 0" + ], + "python_template": "class Solution(object):\n def numberGame(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n ", + "java_template": "class Solution {\n public int[] numberGame(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "numberGame" + } +} \ No newline at end of file diff --git a/minimum-number-of-chairs-in-a-waiting-room.json b/minimum-number-of-chairs-in-a-waiting-room.json new file mode 100644 index 0000000000000000000000000000000000000000..8c96d97020c41e1bcb6f79e645056056cfdfb678 --- /dev/null +++ b/minimum-number-of-chairs-in-a-waiting-room.json @@ -0,0 +1,37 @@ +{ + "id": 3426, + "name": "minimum-number-of-chairs-in-a-waiting-room", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-number-of-chairs-in-a-waiting-room/", + "date": "2024-05-26", + "task_description": "You are given a string `s`. Simulate events at each second `i`: If `s[i] == 'E'`, a person enters the waiting room and takes one of the chairs in it. If `s[i] == 'L'`, a person leaves the waiting room, freeing up a chair. Return the **minimum **number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially **empty**. **Example 1:** **Input:** s = \"EEEEEEE\" **Output:** 7 **Explanation:** After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed. **Example 2:** **Input:** s = \"ELELEEL\" **Output:** 2 **Explanation:** Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second. Second Event People in the Waiting Room Available Chairs 0 Enter 1 1 1 Leave 0 2 2 Enter 1 1 3 Leave 0 2 4 Enter 1 1 5 Enter 2 0 6 Leave 1 1 **Example 3:** **Input:** s = \"ELEELEELLL\" **Output:** 3 **Explanation:** Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second. Second Event People in the Waiting Room Available Chairs 0 Enter 1 2 1 Leave 0 3 2 Enter 1 2 3 Enter 2 1 4 Leave 1 2 5 Enter 2 1 6 Enter 3 0 7 Leave 2 1 8 Leave 1 2 9 Leave 0 3 **Constraints:** `1 <= s.length <= 50` `s` consists only of the letters `'E'` and `'L'`. `s` represents a valid sequence of entries and exits.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"EEEEEEE\"", + "output": "7 " + }, + { + "label": "Example 2", + "input": "s = \"ELELEEL\"", + "output": "2 " + }, + { + "label": "Example 3", + "input": "s = \"ELEELEELLL\"", + "output": "3 " + } + ], + "constraints": [ + "If s[i] == 'E', a person enters the waiting room and takes one of the chairs in it.", + "If s[i] == 'L', a person leaves the waiting room, freeing up a chair.", + "1 <= s.length <= 50", + "s consists only of the letters 'E' and 'L'.", + "s represents a valid sequence of entries and exits." + ], + "python_template": "class Solution(object):\n def minimumChairs(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumChairs(String s) {\n \n }\n}", + "metadata": { + "func_name": "minimumChairs" + } +} \ No newline at end of file diff --git a/minimum-number-of-changes-to-make-binary-string-beautiful.json b/minimum-number-of-changes-to-make-binary-string-beautiful.json new file mode 100644 index 0000000000000000000000000000000000000000..a7416b90c0d50d3768946e74e505904576896d21 --- /dev/null +++ b/minimum-number-of-changes-to-make-binary-string-beautiful.json @@ -0,0 +1,37 @@ +{ + "id": 3174, + "name": "minimum-number-of-changes-to-make-binary-string-beautiful", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/", + "date": "2023-10-14", + "task_description": "You are given a **0-indexed** binary string `s` having an even length. A string is **beautiful** if it's possible to partition it into one or more substrings such that: Each substring has an **even length**. Each substring contains **only** `1`'s or **only** `0`'s. You can change any character in `s` to `0` or `1`. Return _the **minimum** number of changes required to make the string _`s` _beautiful_. **Example 1:** ``` **Input:** s = \"1001\" **Output:** 2 **Explanation:** We change s[1] to 1 and s[3] to 0 to get string \"1100\". It can be seen that the string \"1100\" is beautiful because we can partition it into \"11|00\". It can be proven that 2 is the minimum number of changes needed to make the string beautiful. ``` **Example 2:** ``` **Input:** s = \"10\" **Output:** 1 **Explanation:** We change s[1] to 1 to get string \"11\". It can be seen that the string \"11\" is beautiful because we can partition it into \"11\". It can be proven that 1 is the minimum number of changes needed to make the string beautiful. ``` **Example 3:** ``` **Input:** s = \"0000\" **Output:** 0 **Explanation:** We don't need to make any changes as the string \"0000\" is beautiful already. ``` **Constraints:** `2 <= s.length <= 105` `s` has an even length. `s[i]` is either `'0'` or `'1'`.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \"1001\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "s = \"10\"", + "output": "1 " + }, + { + "label": "Example 3", + "input": "s = \"0000\"", + "output": "0 " + } + ], + "constraints": [ + "Each substring has an even length.", + "Each substring contains only 1's or only 0's.", + "2 <= s.length <= 105", + "s has an even length.", + "s[i] is either '0' or '1'." + ], + "python_template": "class Solution(object):\n def minChanges(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minChanges(String s) {\n \n }\n}", + "metadata": { + "func_name": "minChanges" + } +} \ No newline at end of file diff --git a/minimum-number-of-coins-for-fruits.json b/minimum-number-of-coins-for-fruits.json new file mode 100644 index 0000000000000000000000000000000000000000..962cc4150a723cfcfb8e7187343dd16732749314 --- /dev/null +++ b/minimum-number-of-coins-for-fruits.json @@ -0,0 +1,50 @@ +{ + "id": 3209, + "name": "minimum-number-of-coins-for-fruits", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-coins-for-fruits/", + "date": "2023-11-11", + "task_description": "You are given an **0-indexed** integer array `prices` where `prices[i]` denotes the number of coins needed to purchase the `(i + 1)th` fruit. The fruit market has the following reward for each fruit: If you purchase the `(i + 1)th` fruit at `prices[i]` coins, you can get any number of the next `i` fruits for free. **Note** that even if you **can** take fruit `j` for free, you can still purchase it for `prices[j - 1]` coins to receive its reward. Return the **minimum** number of coins needed to acquire all the fruits. **Example 1:** **Input:** prices = [3,1,2] **Output:** 4 **Explanation:** Purchase the 1st fruit with `prices[0] = 3` coins, you are allowed to take the 2nd fruit for free. Purchase the 2nd fruit with `prices[1] = 1` coin, you are allowed to take the 3rd fruit for free. Take the 3rd fruit for free. Note that even though you could take the 2nd fruit for free as a reward of buying 1st fruit, you purchase it to receive its reward, which is more optimal. **Example 2:** **Input:** prices = [1,10,1,1] **Output:** 2 **Explanation:** Purchase the 1st fruit with `prices[0] = 1` coin, you are allowed to take the 2nd fruit for free. Take the 2nd fruit for free. Purchase the 3rd fruit for `prices[2] = 1` coin, you are allowed to take the 4th fruit for free. Take the 4th fruit for free. **Example 3:** **Input:** prices = [26,18,6,12,49,7,45,45] **Output:** 39 **Explanation:** Purchase the 1st fruit with `prices[0] = 26` coin, you are allowed to take the 2nd fruit for free. Take the 2nd fruit for free. Purchase the 3rd fruit for `prices[2] = 6` coin, you are allowed to take the 4th, 5th and 6th (the next three) fruits for free. Take the 4th fruit for free. Take the 5th fruit for free. Purchase the 6th fruit with `prices[5] = 7` coin, you are allowed to take the 8th and 9th fruit for free. Take the 7th fruit for free. Take the 8th fruit for free. Note that even though you could take the 6th fruit for free as a reward of buying 3rd fruit, you purchase it to receive its reward, which is more optimal. **Constraints:** `1 <= prices.length <= 1000` `1 <= prices[i] <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "prices = [3,1,2]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "prices = [1,10,1,1]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "prices = [26,18,6,12,49,7,45,45]", + "output": "39 " + } + ], + "constraints": [ + "If you purchase the (i + 1)th fruit at prices[i] coins, you can get any number of the next i fruits for free.", + "Purchase the 1st fruit with prices[0] = 3 coins, you are allowed to take the 2nd fruit for free.", + "Purchase the 2nd fruit with prices[1] = 1 coin, you are allowed to take the 3rd fruit for free.", + "Take the 3rd fruit for free.", + "Purchase the 1st fruit with prices[0] = 1 coin, you are allowed to take the 2nd fruit for free.", + "Take the 2nd fruit for free.", + "Purchase the 3rd fruit for prices[2] = 1 coin, you are allowed to take the 4th fruit for free.", + "Take the 4th fruit for free.", + "Purchase the 1st fruit with prices[0] = 26 coin, you are allowed to take the 2nd fruit for free.", + "Take the 2nd fruit for free.", + "Purchase the 3rd fruit for prices[2] = 6 coin, you are allowed to take the 4th, 5th and 6th (the next three) fruits for free.", + "Take the 4th fruit for free.", + "Take the 5th fruit for free.", + "Purchase the 6th fruit with prices[5] = 7 coin, you are allowed to take the 8th and 9th fruit for free.", + "Take the 7th fruit for free.", + "Take the 8th fruit for free.", + "1 <= prices.length <= 1000", + "1 <= prices[i] <= 105" + ], + "python_template": "class Solution(object):\n def minimumCoins(self, prices):\n \"\"\"\n :type prices: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumCoins(int[] prices) {\n \n }\n}", + "metadata": { + "func_name": "minimumCoins" + } +} \ No newline at end of file diff --git a/minimum-number-of-coins-to-be-added.json b/minimum-number-of-coins-to-be-added.json new file mode 100644 index 0000000000000000000000000000000000000000..1e5c2b6fa9a7c6199369207469d1b2528321724f --- /dev/null +++ b/minimum-number-of-coins-to-be-added.json @@ -0,0 +1,35 @@ +{ + "id": 3231, + "name": "minimum-number-of-coins-to-be-added", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-coins-to-be-added/", + "date": "2023-11-26", + "task_description": "You are given a **0-indexed** integer array `coins`, representing the values of the coins available, and an integer `target`. An integer `x` is **obtainable** if there exists a subsequence of `coins` that sums to `x`. Return _the** minimum** number of coins **of any value** that need to be added to the array so that every integer in the range_ `[1, target]`_ is **obtainable**_. A **subsequence** of an array is a new **non-empty** array that is formed from the original array by deleting some (**possibly none**) of the elements without disturbing the relative positions of the remaining elements. **Example 1:** ``` **Input:** coins = [1,4,10], target = 19 **Output:** 2 **Explanation:** We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10]. It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array. ``` **Example 2:** ``` **Input:** coins = [1,4,10,5,7,19], target = 19 **Output:** 1 **Explanation:** We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19]. It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array. ``` **Example 3:** ``` **Input:** coins = [1,1,1], target = 20 **Output:** 3 **Explanation:** We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16]. It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array. ``` **Constraints:** `1 <= target <= 105` `1 <= coins.length <= 105` `1 <= coins[i] <= target`", + "test_case": [ + { + "label": "Example 1", + "input": "coins = [1,4,10], target = 19", + "output": "2 " + }, + { + "label": "Example 2", + "input": "coins = [1,4,10,5,7,19], target = 19", + "output": "1 " + }, + { + "label": "Example 3", + "input": "coins = [1,1,1], target = 20", + "output": "3 " + } + ], + "constraints": [ + "1 <= target <= 105", + "1 <= coins.length <= 105", + "1 <= coins[i] <= target" + ], + "python_template": "class Solution(object):\n def minimumAddedCoins(self, coins, target):\n \"\"\"\n :type coins: List[int]\n :type target: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumAddedCoins(int[] coins, int target) {\n \n }\n}", + "metadata": { + "func_name": "minimumAddedCoins" + } +} \ No newline at end of file diff --git a/minimum-number-of-groups-to-create-a-valid-assignment.json b/minimum-number-of-groups-to-create-a-valid-assignment.json new file mode 100644 index 0000000000000000000000000000000000000000..300a6f84bea781aa8fc6096738d9646d924e70fa --- /dev/null +++ b/minimum-number-of-groups-to-create-a-valid-assignment.json @@ -0,0 +1,37 @@ +{ + "id": 3166, + "name": "minimum-number-of-groups-to-create-a-valid-assignment", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-groups-to-create-a-valid-assignment/", + "date": "2023-10-15", + "task_description": "You are given a collection of numbered `balls` and instructed to sort them into boxes for a nearly balanced distribution. There are two rules you must follow: Balls with the same box must have the same value. But, if you have more than one ball with the same number, you can put them in different boxes. The biggest box can only have one more ball than the smallest box. ​Return the _fewest number of boxes_ to sort these balls following these rules. **Example 1: ** **Input: ** balls = [3,2,3,2,3] **Output: ** 2 **Explanation:** We can sort `balls` into boxes as follows: `[3,3,3]` `[2,2]` The size difference between the two boxes doesn't exceed one. **Example 2: ** **Input: ** balls = [10,10,10,3,1,1] **Output: ** 4 **Explanation:** We can sort `balls` into boxes as follows: `[10]` `[10,10]` `[3]` `[1,1]` You can't use fewer than four boxes while still following the rules. For example, putting all three balls numbered 10 in one box would break the rule about the maximum size difference between boxes. **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "balls = [3,2,3,2,3]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "balls = [10,10,10,3,1,1]", + "output": "4 " + } + ], + "constraints": [ + "Balls with the same box must have the same value. But, if you have more than one ball with the same number, you can put them in different boxes.", + "The biggest box can only have one more ball than the smallest box.", + "[3,3,3]", + "[2,2]", + "[10]", + "[10,10]", + "[3]", + "[1,1]", + "1 <= nums.length <= 105", + "1 <= nums[i] <= 109" + ], + "python_template": "class Solution(object):\n def minGroupsForValidAssignment(self, balls):\n \"\"\"\n :type balls: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minGroupsForValidAssignment(int[] balls) {\n \n }\n}", + "metadata": { + "func_name": "minGroupsForValidAssignment" + } +} \ No newline at end of file diff --git a/minimum-number-of-operations-to-convert-time.json b/minimum-number-of-operations-to-convert-time.json new file mode 100644 index 0000000000000000000000000000000000000000..9c8ffaaa94031c6b5589474d4484a1707e404034 --- /dev/null +++ b/minimum-number-of-operations-to-convert-time.json @@ -0,0 +1,29 @@ +{ + "id": 2345, + "name": "minimum-number-of-operations-to-convert-time", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/", + "date": "2022-03-27", + "task_description": "You are given two strings `current` and `correct` representing two **24-hour times**. 24-hour times are formatted as `\"HH:MM\"`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`. In one operation you can increase the time `current` by `1`, `5`, `15`, or `60` minutes. You can perform this operation **any** number of times. Return _the **minimum number of operations** needed to convert _`current`_ to _`correct`. **Example 1:** ``` **Input:** current = \"02:30\", correct = \"04:35\" **Output:** 3 **Explanation: **We can convert current to correct in 3 operations as follows: - Add 60 minutes to current. current becomes \"03:30\". - Add 60 minutes to current. current becomes \"04:30\". - Add 5 minutes to current. current becomes \"04:35\". It can be proven that it is not possible to convert current to correct in fewer than 3 operations. ``` **Example 2:** ``` **Input:** current = \"11:00\", correct = \"11:01\" **Output:** 1 **Explanation:** We only have to add one minute to current, so the minimum number of operations needed is 1. ``` **Constraints:** `current` and `correct` are in the format `\"HH:MM\"` `current <= correct`", + "test_case": [ + { + "label": "Example 1", + "input": "current = \"02:30\", correct = \"04:35\"", + "output": "3 " + }, + { + "label": "Example 2", + "input": "current = \"11:00\", correct = \"11:01\"", + "output": "1 " + } + ], + "constraints": [ + "current and correct are in the format \"HH:MM\"", + "current <= correct" + ], + "python_template": "class Solution(object):\n def convertTime(self, current, correct):\n \"\"\"\n :type current: str\n :type correct: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int convertTime(String current, String correct) {\n \n }\n}", + "metadata": { + "func_name": "convertTime" + } +} \ No newline at end of file diff --git a/minimum-number-of-operations-to-make-all-array-elements-equal-to-1.json b/minimum-number-of-operations-to-make-all-array-elements-equal-to-1.json new file mode 100644 index 0000000000000000000000000000000000000000..a247f72e54d1c5910b7bed977f7b3e294896bc59 --- /dev/null +++ b/minimum-number-of-operations-to-make-all-array-elements-equal-to-1.json @@ -0,0 +1,30 @@ +{ + "id": 2753, + "name": "minimum-number-of-operations-to-make-all-array-elements-equal-to-1", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-make-all-array-elements-equal-to-1/", + "date": "2023-04-16", + "task_description": "You are given a **0-indexed** array `nums` consisiting of **positive** integers. You can do the following operation on the array **any** number of times: Select an index `i` such that `0 <= i < n - 1` and replace either of `nums[i]` or `nums[i+1]` with their gcd value. Return _the **minimum** number of operations to make all elements of _`nums`_ equal to _`1`. If it is impossible, return `-1`. The gcd of two integers is the greatest common divisor of the two integers. **Example 1:** ``` **Input:** nums = [2,6,3,4] **Output:** 4 **Explanation:** We can do the following operations: - Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4]. - Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4]. - Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4]. - Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1]. ``` **Example 2:** ``` **Input:** nums = [2,10,6,14] **Output:** -1 **Explanation:** It can be shown that it is impossible to make all the elements equal to 1. ``` **Constraints:** `2 <= nums.length <= 50` `1 <= nums[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,6,3,4]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [2,10,6,14]", + "output": "-1 " + } + ], + "constraints": [ + "Select an index i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value.", + "2 <= nums.length <= 50", + "1 <= nums[i] <= 106" + ], + "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-number-of-operations-to-make-array-empty.json b/minimum-number-of-operations-to-make-array-empty.json new file mode 100644 index 0000000000000000000000000000000000000000..9857ae23f91d7026687b90205950e70503e76bde --- /dev/null +++ b/minimum-number-of-operations-to-make-array-empty.json @@ -0,0 +1,31 @@ +{ + "id": 3094, + "name": "minimum-number-of-operations-to-make-array-empty", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/", + "date": "2023-09-16", + "task_description": "You are given a **0-indexed** array `nums` consisting of positive integers. There are two types of operations that you can apply on the array **any** number of times: Choose **two** elements with **equal** values and **delete** them from the array. Choose **three** elements with **equal** values and **delete** them from the array. Return _the **minimum** number of operations required to make the array empty, or _`-1`_ if it is not possible_. **Example 1:** ``` **Input:** nums = [2,3,3,2,2,4,2,3,4] **Output:** 4 **Explanation:** We can apply the following operations to make the array empty: - Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4]. - Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4]. - Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4]. - Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = []. It can be shown that we cannot make the array empty in less than 4 operations. ``` **Example 2:** ``` **Input:** nums = [2,1,2,2,3,3] **Output:** -1 **Explanation:** It is impossible to empty the array. ``` **Constraints:** `2 <= nums.length <= 105` `1 <= nums[i] <= 106` **Note:** This question is the same as 2244: Minimum Rounds to Complete All Tasks.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,3,3,2,2,4,2,3,4]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [2,1,2,2,3,3]", + "output": "-1 " + } + ], + "constraints": [ + "Choose two elements with equal values and delete them from the array.", + "Choose three elements with equal values and delete them from the array.", + "2 <= nums.length <= 105", + "1 <= nums[i] <= 106" + ], + "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-number-of-operations-to-make-array-xor-equal-to-k.json b/minimum-number-of-operations-to-make-array-xor-equal-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..9b4bdd5f80c4eb963ee9d22aa80f137c37b44bcb --- /dev/null +++ b/minimum-number-of-operations-to-make-array-xor-equal-to-k.json @@ -0,0 +1,31 @@ +{ + "id": 3249, + "name": "minimum-number-of-operations-to-make-array-xor-equal-to-k", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k/", + "date": "2023-12-23", + "task_description": "You are given a **0-indexed** integer array `nums` and a positive integer `k`. You can apply the following operation on the array **any** number of times: Choose **any** element of the array and **flip** a bit in its **binary** representation. Flipping a bit means changing a `0` to `1` or vice versa. Return _the **minimum** number of operations required to make the bitwise _`XOR`_ of **all** elements of the final array equal to _`k`. **Note** that you can flip leading zero bits in the binary representation of elements. For example, for the number `(101)2` you can flip the fourth bit and obtain `(1101)2`. **Example 1:** ``` **Input:** nums = [2,1,3,4], k = 1 **Output:** 2 **Explanation:** We can do the following operations: - Choose element 2 which is 3 == (011)2, we flip the first bit and we obtain (010)2 == 2. nums becomes [2,1,2,4]. - Choose element 0 which is 2 == (010)2, we flip the third bit and we obtain (110)2 = 6. nums becomes [6,1,2,4]. The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k. It can be shown that we cannot make the XOR equal to k in less than 2 operations. ``` **Example 2:** ``` **Input:** nums = [2,0,2,0], k = 0 **Output:** 0 **Explanation:** The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed. ``` **Constraints:** `1 <= nums.length <= 105` `0 <= nums[i] <= 106` `0 <= k <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,1,3,4], k = 1", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [2,0,2,0], k = 0", + "output": "0 " + } + ], + "constraints": [ + "Choose any element of the array and flip a bit in its binary representation. Flipping a bit means changing a 0 to 1 or vice versa.", + "1 <= nums.length <= 105", + "0 <= nums[i] <= 106", + "0 <= k <= 106" + ], + "python_template": "class Solution(object):\n def minOperations(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 minOperations(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-number-of-operations-to-make-arrays-similar.json b/minimum-number-of-operations-to-make-arrays-similar.json new file mode 100644 index 0000000000000000000000000000000000000000..9ceb8d32e246880e08a61631a7fcc101a608df4f --- /dev/null +++ b/minimum-number-of-operations-to-make-arrays-similar.json @@ -0,0 +1,38 @@ +{ + "id": 2539, + "name": "minimum-number-of-operations-to-make-arrays-similar", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/", + "date": "2022-10-16", + "task_description": "You are given two positive integer arrays `nums` and `target`, of the same length. In one operation, you can choose any two **distinct** indices `i` and `j` where `0 <= i, j < nums.length` and: set `nums[i] = nums[i] + 2` and set `nums[j] = nums[j] - 2`. Two arrays are considered to be **similar** if the frequency of each element is the same. Return _the minimum number of operations required to make _`nums`_ similar to _`target`. The test cases are generated such that `nums` can always be similar to `target`. **Example 1:** ``` **Input:** nums = [8,12,6], target = [2,14,10] **Output:** 2 **Explanation:** It is possible to make nums similar to target in two operations: - Choose i = 0 and j = 2, nums = [10,12,4]. - Choose i = 1 and j = 2, nums = [10,14,2]. It can be shown that 2 is the minimum number of operations needed. ``` **Example 2:** ``` **Input:** nums = [1,2,5], target = [4,1,3] **Output:** 1 **Explanation:** We can make nums similar to target in one operation: - Choose i = 1 and j = 2, nums = [1,4,3]. ``` **Example 3:** ``` **Input:** nums = [1,1,1,1,1], target = [1,1,1,1,1] **Output:** 0 **Explanation:** The array nums is already similiar to target. ``` **Constraints:** `n == nums.length == target.length` `1 <= n <= 105` `1 <= nums[i], target[i] <= 106` It is possible to make `nums` similar to `target`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [8,12,6], target = [2,14,10]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,2,5], target = [4,1,3]", + "output": "1 " + }, + { + "label": "Example 3", + "input": "nums = [1,1,1,1,1], target = [1,1,1,1,1]", + "output": "0 " + } + ], + "constraints": [ + "set nums[i] = nums[i] + 2 and", + "set nums[j] = nums[j] - 2.", + "n == nums.length == target.length", + "1 <= n <= 105", + "1 <= nums[i], target[i] <= 106", + "It is possible to make nums similar to target." + ], + "python_template": "class Solution(object):\n def makeSimilar(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long makeSimilar(int[] nums, int[] target) {\n \n }\n}", + "metadata": { + "func_name": "makeSimilar" + } +} \ No newline at end of file diff --git a/minimum-number-of-operations-to-make-elements-in-array-distinct.json b/minimum-number-of-operations-to-make-elements-in-array-distinct.json new file mode 100644 index 0000000000000000000000000000000000000000..77fc7bd8d5c8787582b31ac5d8caca1b4c52ec24 --- /dev/null +++ b/minimum-number-of-operations-to-make-elements-in-array-distinct.json @@ -0,0 +1,39 @@ +{ + "id": 3656, + "name": "minimum-number-of-operations-to-make-elements-in-array-distinct", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/", + "date": "2024-12-15", + "task_description": "You are given an integer array `nums`. You need to ensure that the elements in the array are **distinct**. To achieve this, you can perform the following operation any number of times: Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements. **Note** that an empty array is considered to have distinct elements. Return the **minimum** number of operations needed to make the elements in the array distinct. **Example 1:** **Input:** nums = [1,2,3,4,2,3,3,5,7] **Output:** 2 **Explanation:** In the first operation, the first 3 elements are removed, resulting in the array `[4, 2, 3, 3, 5, 7]`. In the second operation, the next 3 elements are removed, resulting in the array `[3, 5, 7]`, which has distinct elements. Therefore, the answer is 2. **Example 2:** **Input:** nums = [4,5,6,4,4] **Output:** 2 **Explanation:** In the first operation, the first 3 elements are removed, resulting in the array `[4, 4]`. In the second operation, all remaining elements are removed, resulting in an empty array. Therefore, the answer is 2. **Example 3:** **Input:** nums = [6,7,8,9] **Output:** 0 **Explanation:** The array already contains distinct elements. Therefore, the answer is 0. **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,3,4,2,3,3,5,7]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [4,5,6,4,4]", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [6,7,8,9]", + "output": "0 " + } + ], + "constraints": [ + "Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.", + "In the first operation, the first 3 elements are removed, resulting in the array [4, 2, 3, 3, 5, 7].", + "In the second operation, the next 3 elements are removed, resulting in the array [3, 5, 7], which has distinct elements.", + "In the first operation, the first 3 elements are removed, resulting in the array [4, 4].", + "In the second operation, all remaining elements are removed, resulting in an empty array.", + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100" + ], + "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-number-of-operations-to-make-word-k-periodic.json b/minimum-number-of-operations-to-make-word-k-periodic.json new file mode 100644 index 0000000000000000000000000000000000000000..7f6da1067f7a07b610ed63b0222fdd7a5b72e505 --- /dev/null +++ b/minimum-number-of-operations-to-make-word-k-periodic.json @@ -0,0 +1,31 @@ +{ + "id": 3384, + "name": "minimum-number-of-operations-to-make-word-k-periodic", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-make-word-k-periodic/", + "date": "2024-04-28", + "task_description": "You are given a string `word` of size `n`, and an integer `k` such that `k` divides `n`. In one operation, you can pick any two indices `i` and `j`, that are divisible by `k`, then replace the substring of length `k` starting at `i` with the substring of length `k` starting at `j`. That is, replace the substring `word[i..i + k - 1]` with the substring `word[j..j + k - 1]`. Return _the **minimum** number of operations required to make_ `word` _**k-periodic**_. We say that `word` is **k-periodic** if there is some string `s` of length `k` such that `word` can be obtained by concatenating `s` an arbitrary number of times. For example, if `word == “ababab”`, then `word` is 2-periodic for `s = \"ab\"`. **Example 1:** **Input:** word = \"leetcodeleet\", k = 4 **Output:** 1 **Explanation:** We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to \"leetleetleet\". **Example 2:** **Input:** word = \"leetcoleet\", k = 2 **Output:** 3 **Explanation:** We can obtain a 2-periodic string by applying the operations in the table below. i j word 0 2 etetcoleet 4 0 etetetleet 6 0 etetetetet **Constraints:** `1 <= n == word.length <= 105` `1 <= k <= word.length` `k` divides `word.length`. `word` consists only of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"leetcodeleet\", k = 4", + "output": "1 " + }, + { + "label": "Example 2", + "input": "word = \" leetcoleet \", k = 2", + "output": "3 " + } + ], + "constraints": [ + "1 <= n == word.length <= 105", + "1 <= k <= word.length", + "k divides word.length.", + "word consists only of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumOperationsToMakeKPeriodic(self, word, k):\n \"\"\"\n :type word: str\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumOperationsToMakeKPeriodic(String word, int k) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperationsToMakeKPeriodic" + } +} \ No newline at end of file diff --git a/minimum-number-of-operations-to-make-x-and-y-equal.json b/minimum-number-of-operations-to-make-x-and-y-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..0e57d7ea33f16fa3e683bd876eecfe5ee32ef0d5 --- /dev/null +++ b/minimum-number-of-operations-to-make-x-and-y-equal.json @@ -0,0 +1,33 @@ +{ + "id": 3239, + "name": "minimum-number-of-operations-to-make-x-and-y-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-operations-to-make-x-and-y-equal/", + "date": "2023-12-23", + "task_description": "You are given two positive integers `x` and `y`. In one operation, you can do one of the four following operations: Divide `x` by `11` if `x` is a multiple of `11`. Divide `x` by `5` if `x` is a multiple of `5`. Decrement `x` by `1`. Increment `x` by `1`. Return _the **minimum** number of operations required to make _ `x` and `y` equal. **Example 1:** ``` **Input:** x = 26, y = 1 **Output:** 3 **Explanation:** We can make 26 equal to 1 by applying the following operations: 1. Decrement x by 1 2. Divide x by 5 3. Divide x by 5 It can be shown that 3 is the minimum number of operations required to make 26 equal to 1. ``` **Example 2:** ``` **Input:** x = 54, y = 2 **Output:** 4 **Explanation:** We can make 54 equal to 2 by applying the following operations: 1. Increment x by 1 2. Divide x by 11 3. Divide x by 5 4. Increment x by 1 It can be shown that 4 is the minimum number of operations required to make 54 equal to 2. ``` **Example 3:** ``` **Input:** x = 25, y = 30 **Output:** 5 **Explanation:** We can make 25 equal to 30 by applying the following operations: 1. Increment x by 1 2. Increment x by 1 3. Increment x by 1 4. Increment x by 1 5. Increment x by 1 It can be shown that 5 is the minimum number of operations required to make 25 equal to 30. ``` **Constraints:** `1 <= x, y <= 104`", + "test_case": [ + { + "label": "Example 1", + "input": "x = 26, y = 1", + "output": "3 " + }, + { + "label": "Example 2", + "input": "x = 54, y = 2", + "output": "4 " + }, + { + "label": "Example 3", + "input": "x = 25, y = 30", + "output": "5 " + } + ], + "constraints": [ + "1 <= x, y <= 104" + ], + "python_template": "class Solution(object):\n def minimumOperationsToMakeEqual(self, x, y):\n \"\"\"\n :type x: int\n :type y: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumOperationsToMakeEqual(int x, int y) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperationsToMakeEqual" + } +} \ No newline at end of file diff --git a/minimum-number-of-pushes-to-type-word-i.json b/minimum-number-of-pushes-to-type-word-i.json new file mode 100644 index 0000000000000000000000000000000000000000..2bae1cd293d67cd1a6db0f1958edc21e0527c338 --- /dev/null +++ b/minimum-number-of-pushes-to-type-word-i.json @@ -0,0 +1,30 @@ +{ + "id": 3275, + "name": "minimum-number-of-pushes-to-type-word-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i/", + "date": "2024-01-14", + "task_description": "You are given a string `word` containing **distinct** lowercase English letters. Telephone keypads have keys mapped with **distinct** collections of lowercase English letters, which can be used to form words by pushing them. For example, the key `2` is mapped with `[\"a\",\"b\",\"c\"]`, we need to push the key one time to type `\"a\"`, two times to type `\"b\"`, and three times to type `\"c\"` _._ It is allowed to remap the keys numbered `2` to `9` to **distinct** collections of letters. The keys can be remapped to **any** amount of letters, but each letter **must** be mapped to **exactly** one key. You need to find the **minimum** number of times the keys will be pushed to type the string `word`. Return _the **minimum** number of pushes needed to type _`word` _after remapping the keys_. An example mapping of letters to keys on a telephone keypad is given below. Note that `1`, `*`, `#`, and `0` do **not** map to any letters. **Example 1:** ``` **Input:** word = \"abcde\" **Output:** 5 **Explanation:** The remapped keypad given in the image provides the minimum cost. \"a\" -> one push on key 2 \"b\" -> one push on key 3 \"c\" -> one push on key 4 \"d\" -> one push on key 5 \"e\" -> one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. ``` **Example 2:** ``` **Input:** word = \"xycdefghij\" **Output:** 12 **Explanation:** The remapped keypad given in the image provides the minimum cost. \"x\" -> one push on key 2 \"y\" -> two pushes on key 2 \"c\" -> one push on key 3 \"d\" -> two pushes on key 3 \"e\" -> one push on key 4 \"f\" -> one push on key 5 \"g\" -> one push on key 6 \"h\" -> one push on key 7 \"i\" -> one push on key 8 \"j\" -> one push on key 9 Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12. It can be shown that no other mapping can provide a lower cost. ``` **Constraints:** `1 <= word.length <= 26` `word` consists of lowercase English letters. All letters in `word` are distinct.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"abcde\"", + "output": "5 " + }, + { + "label": "Example 2", + "input": "word = \"xycdefghij\"", + "output": "12 " + } + ], + "constraints": [ + "1 <= word.length <= 26", + "word consists of lowercase English letters.", + "All letters in word are distinct." + ], + "python_template": "class Solution(object):\n def minimumPushes(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumPushes(String word) {\n \n }\n}", + "metadata": { + "func_name": "minimumPushes" + } +} \ No newline at end of file diff --git a/minimum-number-of-pushes-to-type-word-ii.json b/minimum-number-of-pushes-to-type-word-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..354f52cce3e86a530e1351db3b31f3a0a254a35a --- /dev/null +++ b/minimum-number-of-pushes-to-type-word-ii.json @@ -0,0 +1,34 @@ +{ + "id": 3276, + "name": "minimum-number-of-pushes-to-type-word-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/", + "date": "2024-01-14", + "task_description": "You are given a string `word` containing lowercase English letters. Telephone keypads have keys mapped with **distinct** collections of lowercase English letters, which can be used to form words by pushing them. For example, the key `2` is mapped with `[\"a\",\"b\",\"c\"]`, we need to push the key one time to type `\"a\"`, two times to type `\"b\"`, and three times to type `\"c\"` _._ It is allowed to remap the keys numbered `2` to `9` to **distinct** collections of letters. The keys can be remapped to **any** amount of letters, but each letter **must** be mapped to **exactly** one key. You need to find the **minimum** number of times the keys will be pushed to type the string `word`. Return _the **minimum** number of pushes needed to type _`word` _after remapping the keys_. An example mapping of letters to keys on a telephone keypad is given below. Note that `1`, `*`, `#`, and `0` do **not** map to any letters. **Example 1:** ``` **Input:** word = \"abcde\" **Output:** 5 **Explanation:** The remapped keypad given in the image provides the minimum cost. \"a\" -> one push on key 2 \"b\" -> one push on key 3 \"c\" -> one push on key 4 \"d\" -> one push on key 5 \"e\" -> one push on key 6 Total cost is 1 + 1 + 1 + 1 + 1 = 5. It can be shown that no other mapping can provide a lower cost. ``` **Example 2:** ``` **Input:** word = \"xyzxyzxyzxyz\" **Output:** 12 **Explanation:** The remapped keypad given in the image provides the minimum cost. \"x\" -> one push on key 2 \"y\" -> one push on key 3 \"z\" -> one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 It can be shown that no other mapping can provide a lower cost. Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters. ``` **Example 3:** ``` **Input:** word = \"aabbccddeeffgghhiiiiii\" **Output:** 24 **Explanation:** The remapped keypad given in the image provides the minimum cost. \"a\" -> one push on key 2 \"b\" -> one push on key 3 \"c\" -> one push on key 4 \"d\" -> one push on key 5 \"e\" -> one push on key 6 \"f\" -> one push on key 7 \"g\" -> one push on key 8 \"h\" -> two pushes on key 9 \"i\" -> one push on key 9 Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. It can be shown that no other mapping can provide a lower cost. ``` **Constraints:** `1 <= word.length <= 105` `word` consists of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "word = \"abcde\"", + "output": "5 " + }, + { + "label": "Example 2", + "input": "word = \"xyzxyzxyzxyz\"", + "output": "12 " + }, + { + "label": "Example 3", + "input": "word = \"aabbccddeeffgghhiiiiii\"", + "output": "24 " + } + ], + "constraints": [ + "1 <= word.length <= 105", + "word consists of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minimumPushes(self, word):\n \"\"\"\n :type word: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumPushes(String word) {\n \n }\n}", + "metadata": { + "func_name": "minimumPushes" + } +} \ No newline at end of file diff --git a/minimum-number-of-seconds-to-make-mountain-height-zero.json b/minimum-number-of-seconds-to-make-mountain-height-zero.json new file mode 100644 index 0000000000000000000000000000000000000000..94cd0d4584e6e35f2d231f14d920c2ce258f738a --- /dev/null +++ b/minimum-number-of-seconds-to-make-mountain-height-zero.json @@ -0,0 +1,47 @@ +{ + "id": 3496, + "name": "minimum-number-of-seconds-to-make-mountain-height-zero", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-seconds-to-make-mountain-height-zero/", + "date": "2024-09-15", + "task_description": "You are given an integer `mountainHeight` denoting the height of a mountain. You are also given an integer array `workerTimes` representing the work time of workers in **seconds**. The workers work **simultaneously** to **reduce** the height of the mountain. For worker `i`: To decrease the mountain's height by `x`, it takes `workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x` seconds. For example: To reduce the height of the mountain by 1, it takes `workerTimes[i]` seconds. To reduce the height of the mountain by 2, it takes `workerTimes[i] + workerTimes[i] * 2` seconds, and so on. Return an integer representing the **minimum** number of seconds required for the workers to make the height of the mountain 0. **Example 1:** **Input:** mountainHeight = 4, workerTimes = [2,1,1] **Output:** 3 **Explanation:** One way the height of the mountain can be reduced to 0 is: Worker 0 reduces the height by 1, taking `workerTimes[0] = 2` seconds. Worker 1 reduces the height by 2, taking `workerTimes[1] + workerTimes[1] * 2 = 3` seconds. Worker 2 reduces the height by 1, taking `workerTimes[2] = 1` second. Since they work simultaneously, the minimum time needed is `max(2, 3, 1) = 3` seconds. **Example 2:** **Input:** mountainHeight = 10, workerTimes = [3,2,2,4] **Output:** 12 **Explanation:** Worker 0 reduces the height by 2, taking `workerTimes[0] + workerTimes[0] * 2 = 9` seconds. Worker 1 reduces the height by 3, taking `workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12` seconds. Worker 2 reduces the height by 3, taking `workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12` seconds. Worker 3 reduces the height by 2, taking `workerTimes[3] + workerTimes[3] * 2 = 12` seconds. The number of seconds needed is `max(9, 12, 12, 12) = 12` seconds. **Example 3:** **Input:** mountainHeight = 5, workerTimes = [1] **Output:** 15 **Explanation:** There is only one worker in this example, so the answer is `workerTimes[0] + workerTimes[0] * 2 + workerTimes[0] * 3 + workerTimes[0] * 4 + workerTimes[0] * 5 = 15`. **Constraints:** `1 <= mountainHeight <= 105` `1 <= workerTimes.length <= 104` `1 <= workerTimes[i] <= 106`", + "test_case": [ + { + "label": "Example 1", + "input": "mountainHeight = 4, workerTimes = [2,1,1]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "mountainHeight = 10, workerTimes = [3,2,2,4]", + "output": "12 " + }, + { + "label": "Example 3", + "input": "mountainHeight = 5, workerTimes = [1]", + "output": "15 " + } + ], + "constraints": [ + "To decrease the mountain's height by x, it takes workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x seconds. For example:\n\n\t\nTo reduce the height of the mountain by 1, it takes workerTimes[i] seconds.\nTo reduce the height of the mountain by 2, it takes workerTimes[i] + workerTimes[i] * 2 seconds, and so on.", + "To reduce the height of the mountain by 1, it takes workerTimes[i] seconds.", + "To reduce the height of the mountain by 2, it takes workerTimes[i] + workerTimes[i] * 2 seconds, and so on.", + "To reduce the height of the mountain by 1, it takes workerTimes[i] seconds.", + "To reduce the height of the mountain by 2, it takes workerTimes[i] + workerTimes[i] * 2 seconds, and so on.", + "Worker 0 reduces the height by 1, taking workerTimes[0] = 2 seconds.", + "Worker 1 reduces the height by 2, taking workerTimes[1] + workerTimes[1] * 2 = 3 seconds.", + "Worker 2 reduces the height by 1, taking workerTimes[2] = 1 second.", + "Worker 0 reduces the height by 2, taking workerTimes[0] + workerTimes[0] * 2 = 9 seconds.", + "Worker 1 reduces the height by 3, taking workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12 seconds.", + "Worker 2 reduces the height by 3, taking workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12 seconds.", + "Worker 3 reduces the height by 2, taking workerTimes[3] + workerTimes[3] * 2 = 12 seconds.", + "1 <= mountainHeight <= 105", + "1 <= workerTimes.length <= 104", + "1 <= workerTimes[i] <= 106" + ], + "python_template": "class Solution(object):\n def minNumberOfSeconds(self, mountainHeight, workerTimes):\n \"\"\"\n :type mountainHeight: int\n :type workerTimes: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minNumberOfSeconds(int mountainHeight, int[] workerTimes) {\n \n }\n}", + "metadata": { + "func_name": "minNumberOfSeconds" + } +} \ No newline at end of file diff --git a/minimum-number-of-steps-to-make-two-strings-anagram-ii.json b/minimum-number-of-steps-to-make-two-strings-anagram-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..c61b36e9ea198a6b6d9b53c67bc59c2b02e0e67c --- /dev/null +++ b/minimum-number-of-steps-to-make-two-strings-anagram-ii.json @@ -0,0 +1,29 @@ +{ + "id": 2293, + "name": "minimum-number-of-steps-to-make-two-strings-anagram-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/", + "date": "2022-02-20", + "task_description": "You are given two strings `s` and `t`. In one step, you can append **any character** to either `s` or `t`. Return _the minimum number of steps to make _`s`_ and _`t`_ **anagrams** of each other._ An **anagram** of a string is a string that contains the same characters with a different (or the same) ordering. **Example 1:** ``` **Input:** s = \"**lee**tco**de**\", t = \"co**a**t**s**\" **Output:** 7 **Explanation:** - In 2 steps, we can append the letters in \"as\" onto s = \"leetcode\", forming s = \"leetcode**as**\". - In 5 steps, we can append the letters in \"leede\" onto t = \"coats\", forming t = \"coats**leede**\". \"leetcodeas\" and \"coatsleede\" are now anagrams of each other. We used a total of 2 + 5 = 7 steps. It can be shown that there is no way to make them anagrams of each other with less than 7 steps. ``` **Example 2:** ``` **Input:** s = \"night\", t = \"thing\" **Output:** 0 **Explanation:** The given strings are already anagrams of each other. Thus, we do not need any further steps. ``` **Constraints:** `1 <= s.length, t.length <= 2 * 105` `s` and `t` consist of lowercase English letters.", + "test_case": [ + { + "label": "Example 1", + "input": "s = \" lee tco de \", t = \"co a t s \"", + "output": "7 " + }, + { + "label": "Example 2", + "input": "s = \"night\", t = \"thing\"", + "output": "0 " + } + ], + "constraints": [ + "1 <= s.length, t.length <= 2 * 105", + "s and t consist of lowercase English letters." + ], + "python_template": "class Solution(object):\n def minSteps(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minSteps(String s, String t) {\n \n }\n}", + "metadata": { + "func_name": "minSteps" + } +} \ No newline at end of file diff --git a/minimum-number-of-visited-cells-in-a-grid.json b/minimum-number-of-visited-cells-in-a-grid.json new file mode 100644 index 0000000000000000000000000000000000000000..b373bc08ed7bb08dcb0f7dc98824acda218c0ec2 --- /dev/null +++ b/minimum-number-of-visited-cells-in-a-grid.json @@ -0,0 +1,40 @@ +{ + "id": 2697, + "name": "minimum-number-of-visited-cells-in-a-grid", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/", + "date": "2023-04-02", + "task_description": "You are given a **0-indexed** `m x n` integer matrix `grid`. Your initial position is at the **top-left** cell `(0, 0)`. Starting from the cell `(i, j)`, you can move to one of the following cells: Cells `(i, k)` with `j < k <= grid[i][j] + j` (rightward movement), or Cells `(k, j)` with `i < k <= grid[i][j] + i` (downward movement). Return _the minimum number of cells you need to visit to reach the **bottom-right** cell_ `(m - 1, n - 1)`. If there is no valid path, return `-1`. **Example 1:** ``` **Input:** grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]] **Output:** 4 **Explanation:** The image above shows one of the paths that visits exactly 4 cells. ``` **Example 2:** ``` **Input:** grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]] **Output:** 3 **Explanation: **The image above shows one of the paths that visits exactly 3 cells. ``` **Example 3:** ``` **Input:** grid = [[2,1,0],[1,0,0]] **Output:** -1 **Explanation:** It can be proven that no path exists. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 105` `1 <= m * n <= 105` `0 <= grid[i][j] < m * n` `grid[m - 1][n - 1] == 0`", + "test_case": [ + { + "label": "Example 1", + "input": "grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]", + "output": "4 " + }, + { + "label": "Example 2", + "input": "grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]", + "output": "3 " + }, + { + "label": "Example 3", + "input": "grid = [[2,1,0],[1,0,0]]", + "output": "-1 " + } + ], + "constraints": [ + "Cells (i, k) with j < k <= grid[i][j] + j (rightward movement), or", + "Cells (k, j) with i < k <= grid[i][j] + i (downward movement).", + "m == grid.length", + "n == grid[i].length", + "1 <= m, n <= 105", + "1 <= m * n <= 105", + "0 <= grid[i][j] < m * n", + "grid[m - 1][n - 1] == 0" + ], + "python_template": "class Solution(object):\n def minimumVisitedCells(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumVisitedCells(int[][] grid) {\n \n }\n}", + "metadata": { + "func_name": "minimumVisitedCells" + } +} \ No newline at end of file diff --git a/minimum-operations-to-collect-elements.json b/minimum-operations-to-collect-elements.json new file mode 100644 index 0000000000000000000000000000000000000000..0d52960211e728fbe2c4d8de275bd6ceb4301d10 --- /dev/null +++ b/minimum-operations-to-collect-elements.json @@ -0,0 +1,36 @@ +{ + "id": 3044, + "name": "minimum-operations-to-collect-elements", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-operations-to-collect-elements/", + "date": "2023-09-16", + "task_description": "You are given an array `nums` of positive integers and an integer `k`. In one operation, you can remove the last element of the array and add it to your collection. Return _the **minimum number of operations** needed to collect elements_ `1, 2, ..., k`. **Example 1:** ``` **Input:** nums = [3,1,5,4,2], k = 2 **Output:** 4 **Explanation:** After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4. ``` **Example 2:** ``` **Input:** nums = [3,1,5,4,2], k = 5 **Output:** 5 **Explanation:** After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5. ``` **Example 3:** ``` **Input:** nums = [3,2,5,3,1], k = 3 **Output:** 4 **Explanation:** After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4. ``` **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= nums.length` `1 <= k <= nums.length` The input is generated such that you can collect elements `1, 2, ..., k`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,1,5,4,2], k = 2", + "output": "4 " + }, + { + "label": "Example 2", + "input": "nums = [3,1,5,4,2], k = 5", + "output": "5 " + }, + { + "label": "Example 3", + "input": "nums = [3,2,5,3,1], k = 3", + "output": "4 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "1 <= nums[i] <= nums.length", + "1 <= k <= nums.length", + "The input is generated such that you can collect elements 1, 2, ..., k." + ], + "python_template": "class Solution(object):\n def minOperations(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 minOperations(List nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-exceed-threshold-value-i.json b/minimum-operations-to-exceed-threshold-value-i.json new file mode 100644 index 0000000000000000000000000000000000000000..f49874c455e2adf51711c56425acb0c20db8c899 --- /dev/null +++ b/minimum-operations-to-exceed-threshold-value-i.json @@ -0,0 +1,36 @@ +{ + "id": 3331, + "name": "minimum-operations-to-exceed-threshold-value-i", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-i/", + "date": "2024-02-17", + "task_description": "You are given a **0-indexed** integer array `nums`, and an integer `k`. In one operation, you can remove one occurrence of the smallest element of `nums`. Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`. **Example 1:** ``` **Input:** nums = [2,11,10,1,3], k = 10 **Output:** 3 **Explanation:** After one operation, nums becomes equal to [2, 11, 10, 3]. After two operations, nums becomes equal to [11, 10, 3]. After three operations, nums becomes equal to [11, 10]. At this stage, all the elements of nums are greater than or equal to 10 so we can stop. It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10. ``` **Example 2:** ``` **Input:** nums = [1,1,2,4,9], k = 1 **Output:** 0 **Explanation:** All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums. ``` **Example 3:** ``` **Input:** nums = [1,1,2,4,9], k = 9 **Output:** 4 **Explanation:** only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums. ``` **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= 109` `1 <= k <= 109` The input is generated such that there is at least one index `i` such that `nums[i] >= k`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,11,10,1,3], k = 10", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,2,4,9], k = 1", + "output": "0 " + }, + { + "label": "Example 3", + "input": "nums = [1,1,2,4,9], k = 9", + "output": "4 " + } + ], + "constraints": [ + "1 <= nums.length <= 50", + "1 <= nums[i] <= 109", + "1 <= k <= 109", + "The input is generated such that there is at least one index i such that nums[i] >= k." + ], + "python_template": "class Solution(object):\n def minOperations(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 minOperations(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-exceed-threshold-value-ii.json b/minimum-operations-to-exceed-threshold-value-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..3419f58630f95bc4318c8d808d5e2167be774e41 --- /dev/null +++ b/minimum-operations-to-exceed-threshold-value-ii.json @@ -0,0 +1,34 @@ +{ + "id": 3332, + "name": "minimum-operations-to-exceed-threshold-value-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii/", + "date": "2024-02-17", + "task_description": "You are given a **0-indexed** integer array `nums`, and an integer `k`. You are allowed to perform some operations on `nums`, where in a single operation, you can: Select the two **smallest** integers `x` and `y` from `nums`. Remove `x` and `y` from `nums`. Insert `(min(x, y) * 2 + max(x, y))` at any position in the array. **Note** that you can only apply the described operation if `nums` contains **at least** two elements. Return the **minimum** number of operations needed so that all elements of the array are **greater than or equal to** `k`. **Example 1:** **Input:** nums = [2,11,10,1,3], k = 10 **Output:** 2 **Explanation:** In the first operation, we remove elements 1 and 2, then add `1 * 2 + 2` to `nums`. `nums` becomes equal to `[4, 11, 10, 3]`. In the second operation, we remove elements 3 and 4, then add `3 * 2 + 4` to `nums`. `nums` becomes equal to `[10, 11, 10]`. At this stage, all the elements of nums are greater than or equal to 10 so we can stop. It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10. **Example 2:** **Input:** nums = [1,1,2,4,9], k = 20 **Output:** 4 **Explanation:** After one operation, `nums` becomes equal to `[2, 4, 9, 3]`. After two operations, `nums` becomes equal to `[7, 4, 9]`. After three operations, `nums` becomes equal to `[15, 9]`. After four operations, `nums` becomes equal to `[33]`. At this stage, all the elements of `nums` are greater than 20 so we can stop. It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20. **Constraints:** `2 <= nums.length <= 2 * 105` `1 <= nums[i] <= 109` `1 <= k <= 109` The input is generated such that an answer always exists. That is, after performing some number of operations, all elements of the array are greater than or equal to `k`.", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [2,11,10,1,3], k = 10", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,1,2,4,9], k = 20", + "output": "4 " + } + ], + "constraints": [ + "Select the two smallest integers x and y from nums.", + "Remove x and y from nums.", + "Insert (min(x, y) * 2 + max(x, y)) at any position in the array.", + "2 <= nums.length <= 2 * 105", + "1 <= nums[i] <= 109", + "1 <= k <= 109", + "The input is generated such that an answer always exists. That is, after performing some number of operations, all elements of the array are greater than or equal to k." + ], + "python_template": "class Solution(object):\n def minOperations(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 minOperations(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-form-subsequence-with-target-sum.json b/minimum-operations-to-form-subsequence-with-target-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..4d8048baa4e0e66e524d12ba3cd22a5b2ae14aa0 --- /dev/null +++ b/minimum-operations-to-form-subsequence-with-target-sum.json @@ -0,0 +1,39 @@ +{ + "id": 3025, + "name": "minimum-operations-to-form-subsequence-with-target-sum", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-operations-to-form-subsequence-with-target-sum/", + "date": "2023-08-20", + "task_description": "You are given a **0-indexed** array `nums` consisting of **non-negative** powers of `2`, and an integer `target`. In one operation, you must apply the following changes to the array: Choose any element of the array `nums[i]` such that `nums[i] > 1`. Remove `nums[i]` from the array. Add **two** occurrences of `nums[i] / 2` to the **end** of `nums`. Return the _**minimum number of operations** you need to perform so that _`nums`_ contains a **subsequence** whose elements sum to_ `target`. If it is impossible to obtain such a subsequence, return `-1`. A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. **Example 1:** ``` **Input:** nums = [1,2,8], target = 7 **Output:** 1 **Explanation:** In the first operation, we choose element nums[2]. The array becomes equal to nums = [1,2,4,4]. At this stage, nums contains the subsequence [1,2,4] which sums up to 7. It can be shown that there is no shorter sequence of operations that results in a subsequnce that sums up to 7. ``` **Example 2:** ``` **Input:** nums = [1,32,1,2], target = 12 **Output:** 2 **Explanation:** In the first operation, we choose element nums[1]. The array becomes equal to nums = [1,1,2,16,16]. In the second operation, we choose element nums[3]. The array becomes equal to nums = [1,1,2,16,8,8] At this stage, nums contains the subsequence [1,1,2,8] which sums up to 12. It can be shown that there is no shorter sequence of operations that results in a subsequence that sums up to 12. ``` **Example 3:** ``` **Input:** nums = [1,32,1], target = 35 **Output:** -1 **Explanation:** It can be shown that no sequence of operations results in a subsequence that sums up to 35. ``` **Constraints:** `1 <= nums.length <= 1000` `1 <= nums[i] <= 230` `nums` consists only of non-negative powers of two. `1 <= target < 231`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [1,2,8], target = 7", + "output": "1 " + }, + { + "label": "Example 2", + "input": "nums = [1,32,1,2], target = 12", + "output": "2 " + }, + { + "label": "Example 3", + "input": "nums = [1,32,1], target = 35", + "output": "-1 " + } + ], + "constraints": [ + "Choose any element of the array nums[i] such that nums[i] > 1.", + "Remove nums[i] from the array.", + "Add two occurrences of nums[i] / 2 to the end of nums.", + "1 <= nums.length <= 1000", + "1 <= nums[i] <= 230", + "nums consists only of non-negative powers of two.", + "1 <= target < 231" + ], + "python_template": "class Solution(object):\n def minOperations(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 minOperations(List nums, int target) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-halve-array-sum.json b/minimum-operations-to-halve-array-sum.json new file mode 100644 index 0000000000000000000000000000000000000000..590496b96c212e657cd78fa9c657c206d1bef10c --- /dev/null +++ b/minimum-operations-to-halve-array-sum.json @@ -0,0 +1,29 @@ +{ + "id": 2310, + "name": "minimum-operations-to-halve-array-sum", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-halve-array-sum/", + "date": "2022-03-05", + "task_description": "You are given an array `nums` of positive integers. In one operation, you can choose **any** number from `nums` and reduce it to **exactly** half the number. (Note that you may choose this reduced number in future operations.) Return_ the **minimum** number of operations to reduce the sum of _`nums`_ by **at least** half._ **Example 1:** ``` **Input:** nums = [5,19,8,1] **Output:** 3 **Explanation:** The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33. The following is one of the ways to reduce the sum by at least half: Pick the number 19 and reduce it to 9.5. Pick the number 9.5 and reduce it to 4.75. Pick the number 8 and reduce it to 4. The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75. The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 >= 33/2 = 16.5. Overall, 3 operations were used so we return 3. It can be shown that we cannot reduce the sum by at least half in less than 3 operations. ``` **Example 2:** ``` **Input:** nums = [3,8,20] **Output:** 3 **Explanation:** The initial sum of nums is equal to 3 + 8 + 20 = 31. The following is one of the ways to reduce the sum by at least half: Pick the number 20 and reduce it to 10. Pick the number 10 and reduce it to 5. Pick the number 3 and reduce it to 1.5. The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5. The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 >= 31/2 = 15.5. Overall, 3 operations were used so we return 3. It can be shown that we cannot reduce the sum by at least half in less than 3 operations. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 107`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,19,8,1]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [3,8,20]", + "output": "3 " + } + ], + "constraints": [ + "1 <= nums.length <= 105", + "1 <= nums[i] <= 107" + ], + "python_template": "class Solution(object):\n def halveArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int halveArray(int[] nums) {\n \n }\n}", + "metadata": { + "func_name": "halveArray" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-a-special-number.json b/minimum-operations-to-make-a-special-number.json new file mode 100644 index 0000000000000000000000000000000000000000..fe06ad5d6e9d181f8dc2ea0bd7186c256de434e1 --- /dev/null +++ b/minimum-operations-to-make-a-special-number.json @@ -0,0 +1,35 @@ +{ + "id": 3046, + "name": "minimum-operations-to-make-a-special-number", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-make-a-special-number/", + "date": "2023-08-27", + "task_description": "You are given a **0-indexed** string `num` representing a non-negative integer. In one operation, you can pick any digit of `num` and delete it. Note that if you delete all the digits of `num`, `num` becomes `0`. Return _the **minimum number of operations** required to make_ `num` special. An integer `x` is considered **special** if it is divisible by `25`. **Example 1:** ``` **Input:** num = \"2245047\" **Output:** 2 **Explanation:** Delete digits num[5] and num[6]. The resulting number is \"22450\" which is special since it is divisible by 25. It can be shown that 2 is the minimum number of operations required to get a special number. ``` **Example 2:** ``` **Input:** num = \"2908305\" **Output:** 3 **Explanation:** Delete digits num[3], num[4], and num[6]. The resulting number is \"2900\" which is special since it is divisible by 25. It can be shown that 3 is the minimum number of operations required to get a special number. ``` **Example 3:** ``` **Input:** num = \"10\" **Output:** 1 **Explanation:** Delete digit num[0]. The resulting number is \"0\" which is special since it is divisible by 25. It can be shown that 1 is the minimum number of operations required to get a special number. ``` **Constraints:** `1 <= num.length <= 100` `num` only consists of digits `'0'` through `'9'`. `num` does not contain any leading zeros.", + "test_case": [ + { + "label": "Example 1", + "input": "num = \"2245047\"", + "output": "2 " + }, + { + "label": "Example 2", + "input": "num = \"2908305\"", + "output": "3 " + }, + { + "label": "Example 3", + "input": "num = \"10\"", + "output": "1 " + } + ], + "constraints": [ + "1 <= num.length <= 100", + "num only consists of digits '0' through '9'.", + "num does not contain any leading zeros." + ], + "python_template": "class Solution(object):\n def minimumOperations(self, num):\n \"\"\"\n :type num: str\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public int minimumOperations(String num) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-all-array-elements-equal.json b/minimum-operations-to-make-all-array-elements-equal.json new file mode 100644 index 0000000000000000000000000000000000000000..a1a73607628828f020c8f812d20213fe2de524bc --- /dev/null +++ b/minimum-operations-to-make-all-array-elements-equal.json @@ -0,0 +1,32 @@ +{ + "id": 2718, + "name": "minimum-operations-to-make-all-array-elements-equal", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/", + "date": "2023-03-19", + "task_description": "You are given an array `nums` consisting of positive integers. You are also given an integer array `queries` of size `m`. For the `ith` query, you want to make all of the elements of `nums` equal to` queries[i]`. You can perform the following operation on the array **any** number of times: **Increase** or **decrease** an element of the array by `1`. Return _an array _`answer`_ of size _`m`_ where _`answer[i]`_ is the **minimum** number of operations to make all elements of _`nums`_ equal to _`queries[i]`. **Note** that after each query the array is reset to its original state. **Example 1:** ``` **Input:** nums = [3,1,6,8], queries = [1,5] **Output:** [14,10] **Explanation:** For the first query we can do the following operations: - Decrease nums[0] 2 times, so that nums = [1,1,6,8]. - Decrease nums[2] 5 times, so that nums = [1,1,1,8]. - Decrease nums[3] 7 times, so that nums = [1,1,1,1]. So the total number of operations for the first query is 2 + 5 + 7 = 14. For the second query we can do the following operations: - Increase nums[0] 2 times, so that nums = [5,1,6,8]. - Increase nums[1] 4 times, so that nums = [5,5,6,8]. - Decrease nums[2] 1 time, so that nums = [5,5,5,8]. - Decrease nums[3] 3 times, so that nums = [5,5,5,5]. So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10. ``` **Example 2:** ``` **Input:** nums = [2,9,6,3], queries = [10] **Output:** [20] **Explanation:** We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20. ``` **Constraints:** `n == nums.length` `m == queries.length` `1 <= n, m <= 105` `1 <= nums[i], queries[i] <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,1,6,8], queries = [1,5]", + "output": "[14,10] " + }, + { + "label": "Example 2", + "input": "nums = [2,9,6,3], queries = [10]", + "output": "[20] " + } + ], + "constraints": [ + "Increase or decrease an element of the array by 1.", + "n == nums.length", + "m == queries.length", + "1 <= n, m <= 105", + "1 <= nums[i], queries[i] <= 109" + ], + "python_template": "class Solution(object):\n def minOperations(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 List minOperations(int[] nums, int[] queries) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-array-elements-zero.json b/minimum-operations-to-make-array-elements-zero.json new file mode 100644 index 0000000000000000000000000000000000000000..116d7c1f1cfefcc778dcd3c60d9c3e47a49ddadd --- /dev/null +++ b/minimum-operations-to-make-array-elements-zero.json @@ -0,0 +1,46 @@ +{ + "id": 3744, + "name": "minimum-operations-to-make-array-elements-zero", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-operations-to-make-array-elements-zero/", + "date": "2025-03-16", + "task_description": "You are given a 2D array `queries`, where `queries[i]` is of the form `[l, r]`. Each `queries[i]` defines an array of integers `nums` consisting of elements ranging from `l` to `r`, both **inclusive**. In one operation, you can: Select two integers `a` and `b` from the array. Replace them with `floor(a / 4)` and `floor(b / 4)`. Your task is to determine the **minimum** number of operations required to reduce all elements of the array to zero for each query. Return the sum of the results for all queries. **Example 1:** **Input:** queries = [[1,2],[2,4]] **Output:** 3 **Explanation:** For `queries[0]`: The initial array is `nums = [1, 2]`. In the first operation, select `nums[0]` and `nums[1]`. The array becomes `[0, 0]`. The minimum number of operations required is 1. For `queries[1]`: The initial array is `nums = [2, 3, 4]`. In the first operation, select `nums[0]` and `nums[2]`. The array becomes `[0, 3, 1]`. In the second operation, select `nums[1]` and `nums[2]`. The array becomes `[0, 0, 0]`. The minimum number of operations required is 2. The output is `1 + 2 = 3`. **Example 2:** **Input:** queries = [[2,6]] **Output:** 4 **Explanation:** For `queries[0]`: The initial array is `nums = [2, 3, 4, 5, 6]`. In the first operation, select `nums[0]` and `nums[3]`. The array becomes `[0, 3, 4, 1, 6]`. In the second operation, select `nums[2]` and `nums[4]`. The array becomes `[0, 3, 1, 1, 1]`. In the third operation, select `nums[1]` and `nums[2]`. The array becomes `[0, 0, 0, 1, 1]`. In the fourth operation, select `nums[3]` and `nums[4]`. The array becomes `[0, 0, 0, 0, 0]`. The minimum number of operations required is 4. The output is 4. **Constraints:** `1 <= queries.length <= 105` `queries[i].length == 2` `queries[i] == [l, r]` `1 <= l < r <= 109`", + "test_case": [ + { + "label": "Example 1", + "input": "queries = [[1,2],[2,4]]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "queries = [[2,6]]", + "output": "4 " + } + ], + "constraints": [ + "Select two integers a and b from the array.", + "Replace them with floor(a / 4) and floor(b / 4).", + "The initial array is nums = [1, 2].", + "In the first operation, select nums[0] and nums[1]. The array becomes [0, 0].", + "The minimum number of operations required is 1.", + "The initial array is nums = [2, 3, 4].", + "In the first operation, select nums[0] and nums[2]. The array becomes [0, 3, 1].", + "In the second operation, select nums[1] and nums[2]. The array becomes [0, 0, 0].", + "The minimum number of operations required is 2.", + "The initial array is nums = [2, 3, 4, 5, 6].", + "In the first operation, select nums[0] and nums[3]. The array becomes [0, 3, 4, 1, 6].", + "In the second operation, select nums[2] and nums[4]. The array becomes [0, 3, 1, 1, 1].", + "In the third operation, select nums[1] and nums[2]. The array becomes [0, 0, 0, 1, 1].", + "In the fourth operation, select nums[3] and nums[4]. The array becomes [0, 0, 0, 0, 0].", + "The minimum number of operations required is 4.", + "1 <= queries.length <= 105", + "queries[i].length == 2", + "queries[i] == [l, r]", + "1 <= l < r <= 109" + ], + "python_template": "class Solution(object):\n def minOperations(self, queries):\n \"\"\"\n :type queries: List[List[int]]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minOperations(int[][] queries) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-array-equal-ii.json b/minimum-operations-to-make-array-equal-ii.json new file mode 100644 index 0000000000000000000000000000000000000000..f19733a88178c1faa4d32c22e36271058953469b --- /dev/null +++ b/minimum-operations-to-make-array-equal-ii.json @@ -0,0 +1,32 @@ +{ + "id": 2604, + "name": "minimum-operations-to-make-array-equal-ii", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/", + "date": "2023-01-07", + "task_description": "You are given two integer arrays `nums1` and `nums2` of equal length `n` and an integer `k`. You can perform the following operation on `nums1`: Choose two indexes `i` and `j` and increment `nums1[i]` by `k` and decrement `nums1[j]` by `k`. In other words, `nums1[i] = nums1[i] + k` and `nums1[j] = nums1[j] - k`. `nums1` is said to be **equal** to `nums2` if for all indices `i` such that `0 <= i < n`, `nums1[i] == nums2[i]`. Return _the **minimum** number of operations required to make _`nums1`_ equal to _`nums2`. If it is impossible to make them equal, return `-1`. **Example 1:** ``` **Input:** nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3 **Output:** 2 **Explanation:** In 2 operations, we can transform nums1 to nums2. 1st operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4]. 2nd operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1]. One can prove that it is impossible to make arrays equal in fewer operations. ``` **Example 2:** ``` **Input:** nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1 **Output:** -1 **Explanation:** It can be proved that it is impossible to make the two arrays equal. ``` **Constraints:** `n == nums1.length == nums2.length` `2 <= n <= 105` `0 <= nums1[i], nums2[j] <= 109` `0 <= k <= 105`", + "test_case": [ + { + "label": "Example 1", + "input": "nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1", + "output": "-1 " + } + ], + "constraints": [ + "Choose two indexes i and j and increment nums1[i] by k and decrement nums1[j] by k. In other words, nums1[i] = nums1[i] + k and nums1[j] = nums1[j] - k.", + "n == nums1.length == nums2.length", + "2 <= n <= 105", + "0 <= nums1[i], nums2[j] <= 109", + "0 <= k <= 105" + ], + "python_template": "class Solution(object):\n def minOperations(self, nums1, nums2, k):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minOperations(int[] nums1, int[] nums2, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-array-equal-to-target.json b/minimum-operations-to-make-array-equal-to-target.json new file mode 100644 index 0000000000000000000000000000000000000000..382f9a4c902da84f2113118750143569ad51ed66 --- /dev/null +++ b/minimum-operations-to-make-array-equal-to-target.json @@ -0,0 +1,29 @@ +{ + "id": 3454, + "name": "minimum-operations-to-make-array-equal-to-target", + "difficulty": "Hard", + "link": "https://leetcode.com/problems/minimum-operations-to-make-array-equal-to-target/", + "date": "2024-07-14", + "task_description": "You are given two positive integer arrays `nums` and `target`, of the same length. In a single operation, you can select any subarray of `nums` and increment each element within that subarray by 1 or decrement each element within that subarray by 1. Return the **minimum** number of operations required to make `nums` equal to the array `target`. **Example 1:** **Input:** nums = [3,5,1,2], target = [4,6,2,4] **Output:** 2 **Explanation:** We will perform the following operations to make `nums` equal to `target`: - Increment `nums[0..3]` by 1, `nums = [4,6,2,3]`. - Increment `nums[3..3]` by 1, `nums = [4,6,2,4]`. **Example 2:** **Input:** nums = [1,3,2], target = [2,1,4] **Output:** 5 **Explanation:** We will perform the following operations to make `nums` equal to `target`: - Increment `nums[0..0]` by 1, `nums = [2,3,2]`. - Decrement `nums[1..1]` by 1, `nums = [2,2,2]`. - Decrement `nums[1..1]` by 1, `nums = [2,1,2]`. - Increment `nums[2..2]` by 1, `nums = [2,1,3]`. - Increment `nums[2..2]` by 1, `nums = [2,1,4]`. **Constraints:** `1 <= nums.length == target.length <= 105` `1 <= nums[i], target[i] <= 108`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [3,5,1,2], target = [4,6,2,4]", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [1,3,2], target = [2,1,4]", + "output": "5 " + } + ], + "constraints": [ + "1 <= nums.length == target.length <= 105", + "1 <= nums[i], target[i] <= 108" + ], + "python_template": "class Solution(object):\n def minimumOperations(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: List[int]\n :rtype: int\n \"\"\"\n ", + "java_template": "class Solution {\n public long minimumOperations(int[] nums, int[] target) {\n \n }\n}", + "metadata": { + "func_name": "minimumOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-array-values-equal-to-k.json b/minimum-operations-to-make-array-values-equal-to-k.json new file mode 100644 index 0000000000000000000000000000000000000000..4214c198957f300d8ea0f24ba5a4712f244f1b6d --- /dev/null +++ b/minimum-operations-to-make-array-values-equal-to-k.json @@ -0,0 +1,37 @@ +{ + "id": 3621, + "name": "minimum-operations-to-make-array-values-equal-to-k", + "difficulty": "Easy", + "link": "https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/", + "date": "2024-11-23", + "task_description": "You are given an integer array `nums` and an integer `k`. An integer `h` is called **valid** if all values in the array that are **strictly greater** than `h` are _identical_. For example, if `nums = [10, 8, 10, 8]`, a **valid** integer is `h = 9` because all `nums[i] > 9` are equal to 10, but 5 is not a **valid** integer. You are allowed to perform the following operation on `nums`: Select an integer `h` that is _valid_ for the **current** values in `nums`. For each index `i` where `nums[i] > h`, set `nums[i]` to `h`. Return the **minimum** number of operations required to make every element in `nums` **equal** to `k`. If it is impossible to make all elements equal to `k`, return -1. **Example 1:** **Input:** nums = [5,2,5,4,5], k = 2 **Output:** 2 **Explanation:** The operations can be performed in order using valid integers 4 and then 2. **Example 2:** **Input:** nums = [2,1,2], k = 2 **Output:** -1 **Explanation:** It is impossible to make all the values equal to 2. **Example 3:** **Input:** nums = [9,7,5,3], k = 1 **Output:** 4 **Explanation:** The operations can be performed using valid integers in the order 7, 5, 3, and 1. **Constraints:** `1 <= nums.length <= 100 ` `1 <= nums[i] <= 100` `1 <= k <= 100`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [5,2,5,4,5], k = 2", + "output": "2 " + }, + { + "label": "Example 2", + "input": "nums = [2,1,2], k = 2", + "output": "-1 " + }, + { + "label": "Example 3", + "input": "nums = [9,7,5,3], k = 1", + "output": "4 " + } + ], + "constraints": [ + "Select an integer h that is valid for the current values in nums.", + "For each index i where nums[i] > h, set nums[i] to h.", + "1 <= nums.length <= 100", + "1 <= nums[i] <= 100", + "1 <= k <= 100" + ], + "python_template": "class Solution(object):\n def minOperations(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 minOperations(int[] nums, int k) {\n \n }\n}", + "metadata": { + "func_name": "minOperations" + } +} \ No newline at end of file diff --git a/minimum-operations-to-make-binary-array-elements-equal-to-one-i.json b/minimum-operations-to-make-binary-array-elements-equal-to-one-i.json new file mode 100644 index 0000000000000000000000000000000000000000..674c9dc9af22da77efabeab16b6a326b9ff00c8b --- /dev/null +++ b/minimum-operations-to-make-binary-array-elements-equal-to-one-i.json @@ -0,0 +1,33 @@ +{ + "id": 3475, + "name": "minimum-operations-to-make-binary-array-elements-equal-to-one-i", + "difficulty": "Medium", + "link": "https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/", + "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** 3 **consecutive** elements from the array and **flip** **all** of them. **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. If it is impossible, return -1. **Example 1:** **Input:** nums = [0,1,1,1,0,0] **Output:** 3 **Explanation:** We can do the following operations: Choose the elements at indices 0, 1 and 2. The resulting array is `nums = [**1**,**0**,**0**,1,0,0]`. Choose the elements at indices 1, 2 and 3. The resulting array is `nums = [1,**1**,**1**,**0**,0,0]`. Choose the elements at indices 3, 4 and 5. The resulting array is `nums = [1,1,1,**1**,**1**,**1**]`. **Example 2:** **Input:** nums = [0,1,1,1] **Output:** -1 **Explanation:** It is impossible to make all elements equal to 1. **Constraints:** `3 <= nums.length <= 105` `0 <= nums[i] <= 1`", + "test_case": [ + { + "label": "Example 1", + "input": "nums = [0,1,1,1,0,0]", + "output": "3 " + }, + { + "label": "Example 2", + "input": "nums = [0,1,1,1]", + "output": "-1 " + } + ], + "constraints": [ + "Choose any 3 consecutive elements from the array and flip all of them.", + "Choose the elements at indices 0, 1 and 2. The resulting array is nums = [1,0,0,1,0,0].", + "Choose the elements at indices 1, 2 and 3. The resulting array is nums = [1,1,1,0,0,0].", + "Choose the elements at indices 3, 4 and 5. The resulting array is nums = [1,1,1,1,1,1].", + "3 <= 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