{"task_id": "minimum-time-to-visit-a-cell-in-a-grid", "prompt": "def minimumTime(grid: List[List[int]]) -> int:\n \"\"\"\n You are given a `m x n` matrix `grid` consisting of non-negative integers\n where `grid[row][col]` represents the minimum time required to be able to\n visit the cell `(row, col)`, which means you can visit the cell `(row, col)`\n only when the time you visit it is greater than or equal to `grid[row][col]`.\n \n You are standing in the top-left cell of the matrix in the `0th` second, and\n you must move to any adjacent cell in the four directions: up, down, left, and\n right. Each move you make takes 1 second.\n \n Return the minimum time required in which you can visit the bottom-right cell\n of the matrix. If you cannot visit the bottom-right cell, then return `-1`.\n \n Example 1:\n \n Input: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]\n Output: 7\n Explanation: One of the paths that we can take is the following:\n - at t = 0, we are on the cell (0,0).\n - at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1.\n - at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2.\n - at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3.\n - at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4.\n - at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5.\n - at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6.\n - at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7.\n The final time is 7. It can be shown that it is the minimum time possible.\n \n Example 2:\n \n Input: grid = [[0,2,4],[3,2,1],[1,0,4]]\n Output: -1\n Explanation: There is no path from the top left to the bottom-right cell.\n \n Constraints:\n \n * `m == grid.length`\n * `n == grid[i].length`\n * `2 <= m, n <= 1000`\n * `4 <= m * n <= 105`\n * `0 <= grid[i][j] <= 105`\n * `grid[0][0] == 0`\n \"\"\"\n", "canonical_solution": "", "test": "assert minimumTime([[0,1,3,2],[5,1,2,5],[4,3,8,6]]) == 7\nassert minimumTime([[0,2,4],[3,2,1],[1,0,4]]) == -1", "signature": "minimumTime(grid: List[List[int]]) -> int:", "docstring": "You are given a `m x n` matrix `grid` consisting of non-negative integers\nwhere `grid[row][col]` represents the minimum time required to be able to\nvisit the cell `(row, col)`, which means you can visit the cell `(row, col)`\nonly when the time you visit it is greater than or equal to `grid[row][col]`.\n\nYou are standing in the top-left cell of the matrix in the `0th` second, and\nyou must move to any adjacent cell in the four directions: up, down, left, and\nright. Each move you make takes 1 second.\n\nReturn the minimum time required in which you can visit the bottom-right cell\nof the matrix. If you cannot visit the bottom-right cell, then return `-1`.\n\nExample 1:\n\nInput: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]\nOutput: 7\nExplanation: One of the paths that we can take is the following:\n- at t = 0, we are on the cell (0,0).\n- at t = 1, we move to the cell (0,1). It is possible because grid[0][1] <= 1.\n- at t = 2, we move to the cell (1,1). It is possible because grid[1][1] <= 2.\n- at t = 3, we move to the cell (1,2). It is possible because grid[1][2] <= 3.\n- at t = 4, we move to the cell (1,1). It is possible because grid[1][1] <= 4.\n- at t = 5, we move to the cell (1,2). It is possible because grid[1][2] <= 5.\n- at t = 6, we move to the cell (1,3). It is possible because grid[1][3] <= 6.\n- at t = 7, we move to the cell (2,3). It is possible because grid[2][3] <= 7.\nThe final time is 7. It can be shown that it is the minimum time possible.\n\nExample 2:\n\nInput: grid = [[0,2,4],[3,2,1],[1,0,4]]\nOutput: -1\nExplanation: There is no path from the top left to the bottom-right cell.\n\nConstraints:\n\n* `m == grid.length`\n* `n == grid[i].length`\n* `2 <= m, n <= 1000`\n* `4 <= m * n <= 105`\n* `0 <= grid[i][j] <= 105`\n* `grid[0][0] == 0`"} {"task_id": "find-the-string-with-lcp", "prompt": "def findTheString(lcp: List[List[int]]) -> str:\n \"\"\"\n We define the `lcp` matrix of any 0-indexed string `word` of `n` lowercase\n English letters as an `n x n` grid such that:\n \n * `lcp[i][j]` is equal to the length of the longest common prefix between the substrings `word[i,n-1]` and `word[j,n-1]`.\n \n Given an `n x n` matrix `lcp`, return the alphabetically smallest string\n `word` that corresponds to `lcp`. If there is no such string, return an empty\n string.\n \n A string `a` is lexicographically smaller than a string `b` (of the same\n length) if in the first position where `a` and `b` differ, string `a` has a\n letter that appears earlier in the alphabet than the corresponding letter in\n `b`. For example, `\"aabd\"` is lexicographically smaller than `\"aaca\"` because\n the first position they differ is at the third letter, and `'b'` comes before\n `'c'`.\n \n Example 1:\n \n Input: lcp = [[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]]\n Output: \"abab\"\n Explanation: lcp corresponds to any 4 letter string with two alternating letters. The lexicographically smallest of them is \"abab\".\n \n Example 2:\n \n Input: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]\n Output: \"aaaa\"\n Explanation: lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is \"aaaa\". \n \n Example 3:\n \n Input: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]]\n Output: \"\"\n Explanation: lcp[3][3] cannot be equal to 3 since word[3,...,3] consists of only a single letter; Thus, no answer exists.\n \n Constraints:\n \n * `1 <= n == ``lcp.length == ``lcp[i].length` `<= 1000`\n * `0 <= lcp[i][j] <= n`\n \"\"\"\n", "canonical_solution": "", "test": "assert findTheString([[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]]) == \"abab\"\nassert findTheString([[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]) == \"aaaa\"\nassert findTheString([[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]]) == \"\"", "signature": "findTheString(lcp: List[List[int]]) -> str:", "docstring": "We define the `lcp` matrix of any 0-indexed string `word` of `n` lowercase\nEnglish letters as an `n x n` grid such that:\n\n* `lcp[i][j]` is equal to the length of the longest common prefix between the substrings `word[i,n-1]` and `word[j,n-1]`.\n\nGiven an `n x n` matrix `lcp`, return the alphabetically smallest string\n`word` that corresponds to `lcp`. If there is no such string, return an empty\nstring.\n\nA string `a` is lexicographically smaller than a string `b` (of the same\nlength) if in the first position where `a` and `b` differ, string `a` has a\nletter that appears earlier in the alphabet than the corresponding letter in\n`b`. For example, `\"aabd\"` is lexicographically smaller than `\"aaca\"` because\nthe first position they differ is at the third letter, and `'b'` comes before\n`'c'`.\n\nExample 1:\n\nInput: lcp = [[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]]\nOutput: \"abab\"\nExplanation: lcp corresponds to any 4 letter string with two alternating letters. The lexicographically smallest of them is \"abab\".\n\nExample 2:\n\nInput: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]\nOutput: \"aaaa\"\nExplanation: lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is \"aaaa\". \n\nExample 3:\n\nInput: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]]\nOutput: \"\"\nExplanation: lcp[3][3] cannot be equal to 3 since word[3,...,3] consists of only a single letter; Thus, no answer exists.\n\nConstraints:\n\n* `1 <= n == ``lcp.length == ``lcp[i].length` `<= 1000`\n* `0 <= lcp[i][j] <= n`"} {"task_id": "handling-sum-queries-after-update", "prompt": "def handleQuery(nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:\n \"\"\"\n You are given two 0-indexed arrays `nums1` and `nums2` and a 2D array\n `queries` of queries. There are three types of queries:\n \n 1. 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.\n 2. 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`.\n 3. For a query of type 3, `queries[i] = [3, 0, 0]`. Find the sum of the elements in `nums2`.\n \n Return an array containing all the answers to the third type queries.\n \n Example 1:\n \n Input: nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]\n Output: [3]\n 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.\n \n Example 2:\n \n Input: nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]]\n Output: [5]\n Explanation: After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned.\n \n Constraints:\n \n * `1 <= nums1.length,nums2.length <= 105`\n * `nums1.length = nums2.length`\n * `1 <= queries.length <= 105`\n * `queries[i].length = 3`\n * `0 <= l <= r <= nums1.length - 1`\n * `0 <= p <= 106`\n * `0 <= nums1[i] <= 1`\n * `0 <= nums2[i] <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert handleQuery([1,0,1], [0,0,0], [[1,1,1],[2,1,0],[3,0,0]]) == [3]\nassert handleQuery([1], [5], [[2,0,0],[3,0,0]]) == [5]", "signature": "handleQuery(nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:", "docstring": "You are given two 0-indexed arrays `nums1` and `nums2` and a 2D array\n`queries` of queries. There are three types of queries:\n\n1. 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.\n2. 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`.\n3. For a query of type 3, `queries[i] = [3, 0, 0]`. Find the sum of the elements in `nums2`.\n\nReturn an array containing all the answers to the third type queries.\n\nExample 1:\n\nInput: nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]\nOutput: [3]\nExplanation: 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.\n\nExample 2:\n\nInput: nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]]\nOutput: [5]\nExplanation: After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned.\n\nConstraints:\n\n* `1 <= nums1.length,nums2.length <= 105`\n* `nums1.length = nums2.length`\n* `1 <= queries.length <= 105`\n* `queries[i].length = 3`\n* `0 <= l <= r <= nums1.length - 1`\n* `0 <= p <= 106`\n* `0 <= nums1[i] <= 1`\n* `0 <= nums2[i] <= 109`"} {"task_id": "subsequence-with-the-minimum-score", "prompt": "def minimumScore(s: str, t: str) -> int:\n \"\"\"\n You are given two strings `s` and `t`.\n \n You are allowed to remove any number of characters from the string `t`.\n \n The score of the string is `0` if no characters are removed from the string\n `t`, otherwise:\n \n * Let `left` be the minimum index among all removed characters.\n * Let `right` be the maximum index among all removed characters.\n \n Then the score of the string is `right - left + 1`.\n \n Return the minimum possible score to make `t` a subsequence of `s`.\n \n A subsequence of a string is a new string that is formed from the original\n string by deleting some (can be none) of the characters without disturbing the\n relative positions of the remaining characters. (i.e., `\"ace\"` is a\n subsequence of `\"abcde\"` while `\"aec\"` is not).\n \n Example 1:\n \n Input: s = \"abacaba\", t = \"bzaa\"\n Output: 1\n Explanation: In this example, we remove the character \"z\" at index 1 (0-indexed).\n The string t becomes \"baa\" which is a subsequence of the string \"abacaba\" and the score is 1 - 1 + 1 = 1.\n It can be proven that 1 is the minimum score that we can achieve.\n \n Example 2:\n \n Input: s = \"cde\", t = \"xyz\"\n Output: 3\n Explanation: In this example, we remove characters \"x\", \"y\" and \"z\" at indices 0, 1, and 2 (0-indexed).\n The string t becomes \"\" which is a subsequence of the string \"cde\" and the score is 2 - 0 + 1 = 3.\n It can be proven that 3 is the minimum score that we can achieve.\n \n Constraints:\n \n * `1 <= s.length, t.length <= 105`\n * `s` and `t` consist of only lowercase English letters.\n \"\"\"\n", "canonical_solution": "", "test": "assert minimumScore(\"abacaba\", \"bzaa\") == 1\nassert minimumScore(\"cde\", \"xyz\") == 3", "signature": "minimumScore(s: str, t: str) -> int:", "docstring": "You are given two strings `s` and `t`.\n\nYou are allowed to remove any number of characters from the string `t`.\n\nThe score of the string is `0` if no characters are removed from the string\n`t`, otherwise:\n\n* Let `left` be the minimum index among all removed characters.\n* Let `right` be the maximum index among all removed characters.\n\nThen the score of the string is `right - left + 1`.\n\nReturn the minimum possible score to make `t` a subsequence of `s`.\n\nA subsequence of a string is a new string that is formed from the original\nstring by deleting some (can be none) of the characters without disturbing the\nrelative positions of the remaining characters. (i.e., `\"ace\"` is a\nsubsequence of `\"abcde\"` while `\"aec\"` is not).\n\nExample 1:\n\nInput: s = \"abacaba\", t = \"bzaa\"\nOutput: 1\nExplanation: In this example, we remove the character \"z\" at index 1 (0-indexed).\nThe string t becomes \"baa\" which is a subsequence of the string \"abacaba\" and the score is 1 - 1 + 1 = 1.\nIt can be proven that 1 is the minimum score that we can achieve.\n\nExample 2:\n\nInput: s = \"cde\", t = \"xyz\"\nOutput: 3\nExplanation: In this example, we remove characters \"x\", \"y\" and \"z\" at indices 0, 1, and 2 (0-indexed).\nThe string t becomes \"\" which is a subsequence of the string \"cde\" and the score is 2 - 0 + 1 = 3.\nIt can be proven that 3 is the minimum score that we can achieve.\n\nConstraints:\n\n* `1 <= s.length, t.length <= 105`\n* `s` and `t` consist of only lowercase English letters."} {"task_id": "minimum-number-of-visited-cells-in-a-grid", "prompt": "def minimumVisitedCells(grid: List[List[int]]) -> int:\n \"\"\"\n You are given a 0-indexed `m x n` integer matrix `grid`. Your initial position\n is at the top-left cell `(0, 0)`.\n \n Starting from the cell `(i, j)`, you can move to one of the following cells:\n \n * Cells `(i, k)` with `j < k <= grid[i][j] + j` (rightward movement), or\n * Cells `(k, j)` with `i < k <= grid[i][j] + i` (downward movement).\n \n Return the minimum number of cells you need to visit to reach the bottom-right\n cell `(m - 1, n - 1)`. If there is no valid path, return `-1`.\n \n Example 1:\n \n Input: grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]\n Output: 4\n Explanation: The image above shows one of the paths that visits exactly 4 cells.\n \n Example 2:\n \n Input: grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]\n Output: 3\n Explanation: The image above shows one of the paths that visits exactly 3 cells.\n \n Example 3:\n \n Input: grid = [[2,1,0],[1,0,0]]\n Output: -1\n Explanation: It can be proven that no path exists.\n \n Constraints:\n \n * `m == grid.length`\n * `n == grid[i].length`\n * `1 <= m, n <= 105`\n * `1 <= m * n <= 105`\n * `0 <= grid[i][j] < m * n`\n * `grid[m - 1][n - 1] == 0`\n \"\"\"\n", "canonical_solution": "", "test": "assert minimumVisitedCells([[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]) == 4\nassert minimumVisitedCells([[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]) == 3\nassert minimumVisitedCells([[2,1,0],[1,0,0]]) == -1", "signature": "minimumVisitedCells(grid: List[List[int]]) -> int:", "docstring": "You are given a 0-indexed `m x n` integer matrix `grid`. Your initial position\nis at the top-left cell `(0, 0)`.\n\nStarting from the cell `(i, j)`, you can move to one of the following cells:\n\n* Cells `(i, k)` with `j < k <= grid[i][j] + j` (rightward movement), or\n* Cells `(k, j)` with `i < k <= grid[i][j] + i` (downward movement).\n\nReturn the minimum number of cells you need to visit to reach the bottom-right\ncell `(m - 1, n - 1)`. If there is no valid path, return `-1`.\n\nExample 1:\n\nInput: grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]\nOutput: 4\nExplanation: The image above shows one of the paths that visits exactly 4 cells.\n\nExample 2:\n\nInput: grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]\nOutput: 3\nExplanation: The image above shows one of the paths that visits exactly 3 cells.\n\nExample 3:\n\nInput: grid = [[2,1,0],[1,0,0]]\nOutput: -1\nExplanation: It can be proven that no path exists.\n\nConstraints:\n\n* `m == grid.length`\n* `n == grid[i].length`\n* `1 <= m, n <= 105`\n* `1 <= m * n <= 105`\n* `0 <= grid[i][j] < m * n`\n* `grid[m - 1][n - 1] == 0`"} {"task_id": "rearranging-fruits", "prompt": "def minCost(basket1: List[int], basket2: List[int]) -> int:\n \"\"\"\n You have two fruit baskets containing `n` fruits each. You are given two\n 0-indexed integer arrays `basket1` and `basket2` representing the cost of\n fruit in each basket. You want to make both baskets equal. To do so, you can\n use the following operation as many times as you want:\n \n * Chose two indices `i` and `j`, and swap the `ith `fruit of `basket1` with the `jth` fruit of `basket2`.\n * The cost of the swap is `min(basket1[i],basket2[j])`.\n \n Two baskets are considered equal if sorting them according to the fruit cost\n makes them exactly the same baskets.\n \n Return the minimum cost to make both the baskets equal or `-1` if impossible.\n \n Example 1:\n \n Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2]\n Output: 1\n Explanation: Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.\n \n Example 2:\n \n Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1]\n Output: -1\n Explanation: It can be shown that it is impossible to make both the baskets equal.\n \n Constraints:\n \n * `basket1.length == basket2.length`\n * `1 <= basket1.length <= 105`\n * `1 <= basket1[i],basket2[i] <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert minCost([4,2,2,2], [1,4,1,2]) == 1\nassert minCost([2,3,4,1], [3,2,5,1]) == -1", "signature": "minCost(basket1: List[int], basket2: List[int]) -> int:", "docstring": "You have two fruit baskets containing `n` fruits each. You are given two\n0-indexed integer arrays `basket1` and `basket2` representing the cost of\nfruit in each basket. You want to make both baskets equal. To do so, you can\nuse the following operation as many times as you want:\n\n* Chose two indices `i` and `j`, and swap the `ith `fruit of `basket1` with the `jth` fruit of `basket2`.\n* The cost of the swap is `min(basket1[i],basket2[j])`.\n\nTwo baskets are considered equal if sorting them according to the fruit cost\nmakes them exactly the same baskets.\n\nReturn the minimum cost to make both the baskets equal or `-1` if impossible.\n\nExample 1:\n\nInput: basket1 = [4,2,2,2], basket2 = [1,4,1,2]\nOutput: 1\nExplanation: Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.\n\nExample 2:\n\nInput: basket1 = [2,3,4,1], basket2 = [3,2,5,1]\nOutput: -1\nExplanation: It can be shown that it is impossible to make both the baskets equal.\n\nConstraints:\n\n* `basket1.length == basket2.length`\n* `1 <= basket1.length <= 105`\n* `1 <= basket1[i],basket2[i] <= 109`"} {"task_id": "lexicographically-smallest-beautiful-string", "prompt": "def smallestBeautifulString(s: str, k: int) -> str:\n \"\"\"\n A string is beautiful if:\n \n * It consists of the first `k` letters of the English lowercase alphabet.\n * It does not contain any substring of length `2` or more which is a palindrome.\n \n You are given a beautiful string `s` of length `n` and a positive integer `k`.\n \n Return the lexicographically smallest string of length `n`, which is larger\n than `s` and is beautiful. If there is no such string, return an empty string.\n \n A string `a` is lexicographically larger than a string `b` (of the same\n length) if in the first position where `a` and `b` differ, `a` has a character\n strictly larger than the corresponding character in `b`.\n \n * 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`.\n \n Example 1:\n \n Input: s = \"abcz\", k = 26\n Output: \"abda\"\n Explanation: The string \"abda\" is beautiful and lexicographically larger than the string \"abcz\".\n 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\".\n \n Example 2:\n \n Input: s = \"dc\", k = 4\n Output: \"\"\n Explanation: It can be proven that there is no string that is lexicographically larger than the string \"dc\" and is beautiful.\n \n Constraints:\n \n * `1 <= n == s.length <= 105`\n * `4 <= k <= 26`\n * `s` is a beautiful string.\n \"\"\"\n", "canonical_solution": "", "test": "assert smallestBeautifulString(\"abcz\", 26) == \"abda\"\nassert smallestBeautifulString(\"dc\", 4) == \"\"", "signature": "smallestBeautifulString(s: str, k: int) -> str:", "docstring": "A string is beautiful if:\n\n* It consists of the first `k` letters of the English lowercase alphabet.\n* It does not contain any substring of length `2` or more which is a palindrome.\n\nYou are given a beautiful string `s` of length `n` and a positive integer `k`.\n\nReturn the lexicographically smallest string of length `n`, which is larger\nthan `s` and is beautiful. If there is no such string, return an empty string.\n\nA string `a` is lexicographically larger than a string `b` (of the same\nlength) if in the first position where `a` and `b` differ, `a` has a character\nstrictly larger than the corresponding character in `b`.\n\n* 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`.\n\nExample 1:\n\nInput: s = \"abcz\", k = 26\nOutput: \"abda\"\nExplanation: The string \"abda\" is beautiful and lexicographically larger than the string \"abcz\".\nIt can be proven that there is no string that is lexicographically larger than the string \"abcz\", beautiful, and lexicographically smaller than the string \"abda\".\n\nExample 2:\n\nInput: s = \"dc\", k = 4\nOutput: \"\"\nExplanation: It can be proven that there is no string that is lexicographically larger than the string \"dc\" and is beautiful.\n\nConstraints:\n\n* `1 <= n == s.length <= 105`\n* `4 <= k <= 26`\n* `s` is a beautiful string."} {"task_id": "count-increasing-quadruplets", "prompt": "def countQuadruplets(nums: List[int]) -> int:\n \"\"\"\n Given a 0-indexed integer array `nums` of size `n` containing all numbers from\n `1` to `n`, return the number of increasing quadruplets.\n \n A quadruplet `(i, j, k, l)` is increasing if:\n \n * `0 <= i < j < k < l < n`, and\n * `nums[i] < nums[k] < nums[j] < nums[l]`.\n \n Example 1:\n \n Input: nums = [1,3,2,4,5]\n Output: 2\n Explanation: \n - When i = 0, j = 1, k = 2, and l = 3, nums[i] < nums[k] < nums[j] < nums[l].\n - When i = 0, j = 1, k = 2, and l = 4, nums[i] < nums[k] < nums[j] < nums[l]. \n There are no other quadruplets, so we return 2.\n \n Example 2:\n \n Input: nums = [1,2,3,4]\n Output: 0\n Explanation: There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] < nums[k], we return 0.\n \n Constraints:\n \n * `4 <= nums.length <= 4000`\n * `1 <= nums[i] <= nums.length`\n * All the integers of `nums` are unique. `nums` is a permutation.\n \"\"\"\n", "canonical_solution": "", "test": "assert countQuadruplets([1,3,2,4,5]) == 2\nassert countQuadruplets([1,2,3,4]) == 0", "signature": "countQuadruplets(nums: List[int]) -> int:", "docstring": "Given a 0-indexed integer array `nums` of size `n` containing all numbers from\n`1` to `n`, return the number of increasing quadruplets.\n\nA quadruplet `(i, j, k, l)` is increasing if:\n\n* `0 <= i < j < k < l < n`, and\n* `nums[i] < nums[k] < nums[j] < nums[l]`.\n\nExample 1:\n\nInput: nums = [1,3,2,4,5]\nOutput: 2\nExplanation: \n- When i = 0, j = 1, k = 2, and l = 3, nums[i] < nums[k] < nums[j] < nums[l].\n- When i = 0, j = 1, k = 2, and l = 4, nums[i] < nums[k] < nums[j] < nums[l]. \nThere are no other quadruplets, so we return 2.\n\nExample 2:\n\nInput: nums = [1,2,3,4]\nOutput: 0\nExplanation: There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] < nums[k], we return 0.\n\nConstraints:\n\n* `4 <= nums.length <= 4000`\n* `1 <= nums[i] <= nums.length`\n* All the integers of `nums` are unique. `nums` is a permutation."} {"task_id": "put-marbles-in-bags", "prompt": "def putMarbles(weights: List[int], k: int) -> int:\n \"\"\"\n You have `k` bags. You are given a 0-indexed integer array `weights` where\n `weights[i]` is the weight of the `ith` marble. You are also given the integer\n `k.`\n \n Divide the marbles into the `k` bags according to the following rules:\n \n * No bag is empty.\n * If the `ith` marble and `jth` marble are in a bag, then all marbles with an index between the `ith` and `jth` indices should also be in that same bag.\n * If a bag consists of all the marbles with an index from `i` to `j` inclusively, then the cost of the bag is `weights[i] + weights[j]`.\n \n The score after distributing the marbles is the sum of the costs of all the\n `k` bags.\n \n Return the difference between the maximum and minimum scores among marble\n distributions.\n \n Example 1:\n \n Input: weights = [1,3,5,1], k = 2\n Output: 4\n Explanation: \n The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. \n The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. \n Thus, we return their difference 10 - 6 = 4.\n \n Example 2:\n \n Input: weights = [1, 3], k = 2\n Output: 0\n Explanation: The only distribution possible is [1],[3]. \n Since both the maximal and minimal score are the same, we return 0.\n \n Constraints:\n \n * `1 <= k <= weights.length <= 105`\n * `1 <= weights[i] <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert putMarbles([1,3,5,1], 2) == 4\nassert putMarbles([1,3], 2) == 0", "signature": "putMarbles(weights: List[int], k: int) -> int:", "docstring": "You have `k` bags. You are given a 0-indexed integer array `weights` where\n`weights[i]` is the weight of the `ith` marble. You are also given the integer\n`k.`\n\nDivide the marbles into the `k` bags according to the following rules:\n\n* No bag is empty.\n* If the `ith` marble and `jth` marble are in a bag, then all marbles with an index between the `ith` and `jth` indices should also be in that same bag.\n* If a bag consists of all the marbles with an index from `i` to `j` inclusively, then the cost of the bag is `weights[i] + weights[j]`.\n\nThe score after distributing the marbles is the sum of the costs of all the\n`k` bags.\n\nReturn the difference between the maximum and minimum scores among marble\ndistributions.\n\nExample 1:\n\nInput: weights = [1,3,5,1], k = 2\nOutput: 4\nExplanation: \nThe distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. \nThe distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. \nThus, we return their difference 10 - 6 = 4.\n\nExample 2:\n\nInput: weights = [1, 3], k = 2\nOutput: 0\nExplanation: The only distribution possible is [1],[3]. \nSince both the maximal and minimal score are the same, we return 0.\n\nConstraints:\n\n* `1 <= k <= weights.length <= 105`\n* `1 <= weights[i] <= 109`"} {"task_id": "shortest-cycle-in-a-graph", "prompt": "def findShortestCycle(n: int, edges: List[List[int]]) -> int:\n \"\"\"\n There is a bi-directional graph with `n` vertices, where each vertex is\n labeled from `0` to `n - 1`. The edges in the graph are represented by a given\n 2D integer array `edges`, where `edges[i] = [ui, vi]` denotes an edge between\n vertex `ui` and vertex `vi`. Every vertex pair is connected by at most one\n edge, and no vertex has an edge to itself.\n \n Return the length of the shortest cycle in the graph. If no cycle exists,\n return `-1`.\n \n A cycle is a path that starts and ends at the same node, and each edge in the\n path is used only once.\n \n Example 1:\n \n Input: n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]\n Output: 3\n Explanation: The cycle with the smallest length is : 0 -> 1 -> 2 -> 0 \n \n Example 2:\n \n Input: n = 4, edges = [[0,1],[0,2]]\n Output: -1\n Explanation: There are no cycles in this graph.\n \n Constraints:\n \n * `2 <= n <= 1000`\n * `1 <= edges.length <= 1000`\n * `edges[i].length == 2`\n * `0 <= ui, vi < n`\n * `ui != vi`\n * There are no repeated edges.\n \"\"\"\n", "canonical_solution": "", "test": "assert findShortestCycle(7, [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]) == 3\nassert findShortestCycle(4, [[0,1],[0,2]]) == -1", "signature": "findShortestCycle(n: int, edges: List[List[int]]) -> int:", "docstring": "There is a bi-directional graph with `n` vertices, where each vertex is\nlabeled from `0` to `n - 1`. The edges in the graph are represented by a given\n2D integer array `edges`, where `edges[i] = [ui, vi]` denotes an edge between\nvertex `ui` and vertex `vi`. Every vertex pair is connected by at most one\nedge, and no vertex has an edge to itself.\n\nReturn the length of the shortest cycle in the graph. If no cycle exists,\nreturn `-1`.\n\nA cycle is a path that starts and ends at the same node, and each edge in the\npath is used only once.\n\nExample 1:\n\nInput: n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]\nOutput: 3\nExplanation: The cycle with the smallest length is : 0 -> 1 -> 2 -> 0 \n\nExample 2:\n\nInput: n = 4, edges = [[0,1],[0,2]]\nOutput: -1\nExplanation: There are no cycles in this graph.\n\nConstraints:\n\n* `2 <= n <= 1000`\n* `1 <= edges.length <= 1000`\n* `edges[i].length == 2`\n* `0 <= ui, vi < n`\n* `ui != vi`\n* There are no repeated edges."} {"task_id": "minimum-time-to-complete-all-tasks", "prompt": "def findMinimumTime(tasks: List[List[int]]) -> int:\n \"\"\"\n There is a computer that can run an unlimited number of tasks at the same\n time. You are given a 2D integer array `tasks` where `tasks[i] = [starti,\n endi, durationi]` indicates that the `ith` task should run for a total of\n `durationi` seconds (not necessarily continuous) within the inclusive time\n range `[starti, endi]`.\n \n You may turn on the computer only when it needs to run a task. You can also\n turn it off if it is idle.\n \n Return the minimum time during which the computer should be turned on to\n complete all tasks.\n \n Example 1:\n \n Input: tasks = [[2,3,1],[4,5,1],[1,5,2]]\n Output: 2\n Explanation: \n - The first task can be run in the inclusive time range [2, 2].\n - The second task can be run in the inclusive time range [5, 5].\n - The third task can be run in the two inclusive time ranges [2, 2] and [5, 5].\n The computer will be on for a total of 2 seconds.\n \n Example 2:\n \n Input: tasks = [[1,3,2],[2,5,3],[5,6,2]]\n Output: 4\n Explanation: \n - The first task can be run in the inclusive time range [2, 3].\n - The second task can be run in the inclusive time ranges [2, 3] and [5, 5].\n - The third task can be run in the two inclusive time range [5, 6].\n The computer will be on for a total of 4 seconds.\n \n Constraints:\n \n * `1 <= tasks.length <= 2000`\n * `tasks[i].length == 3`\n * `1 <= starti, endi <= 2000`\n * `1 <= durationi <= endi - starti + 1 `\n \"\"\"\n", "canonical_solution": "", "test": "assert findMinimumTime([[2,3,1],[4,5,1],[1,5,2]]) == 2\nassert findMinimumTime([[1,3,2],[2,5,3],[5,6,2]]) == 4", "signature": "findMinimumTime(tasks: List[List[int]]) -> int:", "docstring": "There is a computer that can run an unlimited number of tasks at the same\ntime. You are given a 2D integer array `tasks` where `tasks[i] = [starti,\nendi, durationi]` indicates that the `ith` task should run for a total of\n`durationi` seconds (not necessarily continuous) within the inclusive time\nrange `[starti, endi]`.\n\nYou may turn on the computer only when it needs to run a task. You can also\nturn it off if it is idle.\n\nReturn the minimum time during which the computer should be turned on to\ncomplete all tasks.\n\nExample 1:\n\nInput: tasks = [[2,3,1],[4,5,1],[1,5,2]]\nOutput: 2\nExplanation: \n- The first task can be run in the inclusive time range [2, 2].\n- The second task can be run in the inclusive time range [5, 5].\n- The third task can be run in the two inclusive time ranges [2, 2] and [5, 5].\nThe computer will be on for a total of 2 seconds.\n\nExample 2:\n\nInput: tasks = [[1,3,2],[2,5,3],[5,6,2]]\nOutput: 4\nExplanation: \n- The first task can be run in the inclusive time range [2, 3].\n- The second task can be run in the inclusive time ranges [2, 3] and [5, 5].\n- The third task can be run in the two inclusive time range [5, 6].\nThe computer will be on for a total of 4 seconds.\n\nConstraints:\n\n* `1 <= tasks.length <= 2000`\n* `tasks[i].length == 3`\n* `1 <= starti, endi <= 2000`\n* `1 <= durationi <= endi - starti + 1 `"} {"task_id": "count-number-of-possible-root-nodes", "prompt": "def rootCount(edges: List[List[int]], guesses: List[List[int]], k: int) -> int:\n \"\"\"\n Alice has an undirected tree with `n` nodes labeled from `0` to `n - 1`. The\n tree is represented as a 2D integer array `edges` of length `n - 1` where\n `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and\n `bi` in the tree.\n \n Alice wants Bob to find the root of the tree. She allows Bob to make several\n guesses about her tree. In one guess, he does the following:\n \n * Chooses two distinct integers `u` and `v` such that there exists an edge `[u, v]` in the tree.\n * He tells Alice that `u` is the parent of `v` in the tree.\n \n Bob's guesses are represented by a 2D integer array `guesses` where\n `guesses[j] = [uj, vj]` indicates Bob guessed `uj` to be the parent of `vj`.\n \n Alice being lazy, does not reply to each of Bob's guesses, but just says that\n at least `k` of his guesses are `true`.\n \n Given the 2D integer arrays `edges`, `guesses` and the integer `k`, return the\n number of possible nodes that can be the root of Alice's tree. If there is no\n such tree, return `0`.\n \n Example 1:\n \n Input: edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3\n Output: 3\n Explanation: \n Root = 0, correct guesses = [1,3], [0,1], [2,4]\n Root = 1, correct guesses = [1,3], [1,0], [2,4]\n Root = 2, correct guesses = [1,3], [1,0], [2,4]\n Root = 3, correct guesses = [1,0], [2,4]\n Root = 4, correct guesses = [1,3], [1,0]\n Considering 0, 1, or 2 as root node leads to 3 correct guesses.\n \n Example 2:\n \n Input: edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1\n Output: 5\n Explanation: \n Root = 0, correct guesses = [3,4]\n Root = 1, correct guesses = [1,0], [3,4]\n Root = 2, correct guesses = [1,0], [2,1], [3,4]\n Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]\n Root = 4, correct guesses = [1,0], [2,1], [3,2]\n Considering any node as root will give at least 1 correct guess. \n \n Constraints:\n \n * `edges.length == n - 1`\n * `2 <= n <= 105`\n * `1 <= guesses.length <= 105`\n * `0 <= ai, bi, uj, vj <= n - 1`\n * `ai != bi`\n * `uj != vj`\n * `edges` represents a valid tree.\n * `guesses[j]` is an edge of the tree.\n * `guesses` is unique.\n * `0 <= k <= guesses.length`\n \"\"\"\n", "canonical_solution": "", "test": "assert rootCount([[0,1],[1,2],[1,3],[4,2]], [[1,3],[0,1],[1,0],[2,4]], 3) == 3\nassert rootCount([[0,1],[1,2],[2,3],[3,4]], [[1,0],[3,4],[2,1],[3,2]], 1) == 5", "signature": "rootCount(edges: List[List[int]], guesses: List[List[int]], k: int) -> int:", "docstring": "Alice has an undirected tree with `n` nodes labeled from `0` to `n - 1`. The\ntree is represented as a 2D integer array `edges` of length `n - 1` where\n`edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and\n`bi` in the tree.\n\nAlice wants Bob to find the root of the tree. She allows Bob to make several\nguesses about her tree. In one guess, he does the following:\n\n* Chooses two distinct integers `u` and `v` such that there exists an edge `[u, v]` in the tree.\n* He tells Alice that `u` is the parent of `v` in the tree.\n\nBob's guesses are represented by a 2D integer array `guesses` where\n`guesses[j] = [uj, vj]` indicates Bob guessed `uj` to be the parent of `vj`.\n\nAlice being lazy, does not reply to each of Bob's guesses, but just says that\nat least `k` of his guesses are `true`.\n\nGiven the 2D integer arrays `edges`, `guesses` and the integer `k`, return the\nnumber of possible nodes that can be the root of Alice's tree. If there is no\nsuch tree, return `0`.\n\nExample 1:\n\nInput: edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3\nOutput: 3\nExplanation: \nRoot = 0, correct guesses = [1,3], [0,1], [2,4]\nRoot = 1, correct guesses = [1,3], [1,0], [2,4]\nRoot = 2, correct guesses = [1,3], [1,0], [2,4]\nRoot = 3, correct guesses = [1,0], [2,4]\nRoot = 4, correct guesses = [1,3], [1,0]\nConsidering 0, 1, or 2 as root node leads to 3 correct guesses.\n\nExample 2:\n\nInput: edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1\nOutput: 5\nExplanation: \nRoot = 0, correct guesses = [3,4]\nRoot = 1, correct guesses = [1,0], [3,4]\nRoot = 2, correct guesses = [1,0], [2,1], [3,4]\nRoot = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]\nRoot = 4, correct guesses = [1,0], [2,1], [3,2]\nConsidering any node as root will give at least 1 correct guess. \n\nConstraints:\n\n* `edges.length == n - 1`\n* `2 <= n <= 105`\n* `1 <= guesses.length <= 105`\n* `0 <= ai, bi, uj, vj <= n - 1`\n* `ai != bi`\n* `uj != vj`\n* `edges` represents a valid tree.\n* `guesses[j]` is an edge of the tree.\n* `guesses` is unique.\n* `0 <= k <= guesses.length`"} {"task_id": "number-of-ways-to-earn-points", "prompt": "def waysToReachTarget(target: int, types: List[List[int]]) -> int:\n \"\"\"\n There is a test that has `n` types of questions. You are given an integer\n `target` and a 0-indexed 2D integer array `types` where `types[i] = [counti,\n marksi]` indicates that there are `counti` questions of the `ith` type, and\n each one of them is worth `marksi` points.\n \n Return the number of ways you can earn exactly `target` points in the exam.\n Since the answer may be too large, return it modulo `109 + 7`.\n \n Note that questions of the same type are indistinguishable.\n \n * For example, if there are `3` questions of the same type, then solving the `1st` and `2nd` questions is the same as solving the `1st` and `3rd` questions, or the `2nd` and `3rd` questions.\n \n Example 1:\n \n Input: target = 6, types = [[6,1],[3,2],[2,3]]\n Output: 7\n Explanation: You can earn 6 points in one of the seven ways:\n - Solve 6 questions of the 0th type: 1 + 1 + 1 + 1 + 1 + 1 = 6\n - Solve 4 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 1 + 2 = 6\n - Solve 2 questions of the 0th type and 2 questions of the 1st type: 1 + 1 + 2 + 2 = 6\n - Solve 3 questions of the 0th type and 1 question of the 2nd type: 1 + 1 + 1 + 3 = 6\n - Solve 1 question of the 0th type, 1 question of the 1st type and 1 question of the 2nd type: 1 + 2 + 3 = 6\n - Solve 3 questions of the 1st type: 2 + 2 + 2 = 6\n - Solve 2 questions of the 2nd type: 3 + 3 = 6\n \n Example 2:\n \n Input: target = 5, types = [[50,1],[50,2],[50,5]]\n Output: 4\n Explanation: You can earn 5 points in one of the four ways:\n - Solve 5 questions of the 0th type: 1 + 1 + 1 + 1 + 1 = 5\n - Solve 3 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 2 = 5\n - Solve 1 questions of the 0th type and 2 questions of the 1st type: 1 + 2 + 2 = 5\n - Solve 1 question of the 2nd type: 5\n \n Example 3:\n \n Input: target = 18, types = [[6,1],[3,2],[2,3]]\n Output: 1\n Explanation: You can only earn 18 points by answering all questions.\n \n Constraints:\n \n * `1 <= target <= 1000`\n * `n == types.length`\n * `1 <= n <= 50`\n * `types[i].length == 2`\n * `1 <= counti, marksi <= 50`\n \"\"\"\n", "canonical_solution": "", "test": "assert waysToReachTarget(6, [[6,1],[3,2],[2,3]]) == 7\nassert waysToReachTarget(5, [[50,1],[50,2],[50,5]]) == 4\nassert waysToReachTarget(18, [[6,1],[3,2],[2,3]]) == 1", "signature": "waysToReachTarget(target: int, types: List[List[int]]) -> int:", "docstring": "There is a test that has `n` types of questions. You are given an integer\n`target` and a 0-indexed 2D integer array `types` where `types[i] = [counti,\nmarksi]` indicates that there are `counti` questions of the `ith` type, and\neach one of them is worth `marksi` points.\n\nReturn the number of ways you can earn exactly `target` points in the exam.\nSince the answer may be too large, return it modulo `109 + 7`.\n\nNote that questions of the same type are indistinguishable.\n\n* For example, if there are `3` questions of the same type, then solving the `1st` and `2nd` questions is the same as solving the `1st` and `3rd` questions, or the `2nd` and `3rd` questions.\n\nExample 1:\n\nInput: target = 6, types = [[6,1],[3,2],[2,3]]\nOutput: 7\nExplanation: You can earn 6 points in one of the seven ways:\n- Solve 6 questions of the 0th type: 1 + 1 + 1 + 1 + 1 + 1 = 6\n- Solve 4 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 1 + 2 = 6\n- Solve 2 questions of the 0th type and 2 questions of the 1st type: 1 + 1 + 2 + 2 = 6\n- Solve 3 questions of the 0th type and 1 question of the 2nd type: 1 + 1 + 1 + 3 = 6\n- Solve 1 question of the 0th type, 1 question of the 1st type and 1 question of the 2nd type: 1 + 2 + 3 = 6\n- Solve 3 questions of the 1st type: 2 + 2 + 2 = 6\n- Solve 2 questions of the 2nd type: 3 + 3 = 6\n\nExample 2:\n\nInput: target = 5, types = [[50,1],[50,2],[50,5]]\nOutput: 4\nExplanation: You can earn 5 points in one of the four ways:\n- Solve 5 questions of the 0th type: 1 + 1 + 1 + 1 + 1 = 5\n- Solve 3 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 2 = 5\n- Solve 1 questions of the 0th type and 2 questions of the 1st type: 1 + 2 + 2 = 5\n- Solve 1 question of the 2nd type: 5\n\nExample 3:\n\nInput: target = 18, types = [[6,1],[3,2],[2,3]]\nOutput: 1\nExplanation: You can only earn 18 points by answering all questions.\n\nConstraints:\n\n* `1 <= target <= 1000`\n* `n == types.length`\n* `1 <= n <= 50`\n* `types[i].length == 2`\n* `1 <= counti, marksi <= 50`"} {"task_id": "split-the-array-to-make-coprime-products", "prompt": "def findValidSplit(nums: List[int]) -> int:\n \"\"\"\n You are given a 0-indexed integer array `nums` of length `n`.\n \n A split at an index `i` where `0 <= i <= n - 2` is called valid if the product\n of the first `i + 1` elements and the product of the remaining elements are\n coprime.\n \n * For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is valid because `2` and `9` are coprime, while a split at the index `i = 1` is not valid because `6` and `3` are not coprime. A split at the index `i = 2` is not valid because `i == n - 1`.\n \n Return the smallest index `i` at which the array can be split validly or `-1`\n if there is no such split.\n \n Two values `val1` and `val2` are coprime if `gcd(val1, val2) == 1` where\n `gcd(val1, val2)` is the greatest common divisor of `val1` and `val2`.\n \n Example 1:\n \n Input: nums = [4,7,8,15,3,5]\n Output: 2\n Explanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.\n The only valid split is at index 2.\n \n Example 2:\n \n Input: nums = [4,7,15,8,3,5]\n Output: -1\n Explanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.\n There is no valid split.\n \n Constraints:\n \n * `n == nums.length`\n * `1 <= n <= 104`\n * `1 <= nums[i] <= 106`\n \"\"\"\n", "canonical_solution": "", "test": "assert findValidSplit([4,7,8,15,3,5]) == 2\nassert findValidSplit([4,7,15,8,3,5]) == -1", "signature": "findValidSplit(nums: List[int]) -> int:", "docstring": "You are given a 0-indexed integer array `nums` of length `n`.\n\nA split at an index `i` where `0 <= i <= n - 2` is called valid if the product\nof the first `i + 1` elements and the product of the remaining elements are\ncoprime.\n\n* For example, if `nums = [2, 3, 3]`, then a split at the index `i = 0` is valid because `2` and `9` are coprime, while a split at the index `i = 1` is not valid because `6` and `3` are not coprime. A split at the index `i = 2` is not valid because `i == n - 1`.\n\nReturn the smallest index `i` at which the array can be split validly or `-1`\nif there is no such split.\n\nTwo values `val1` and `val2` are coprime if `gcd(val1, val2) == 1` where\n`gcd(val1, val2)` is the greatest common divisor of `val1` and `val2`.\n\nExample 1:\n\nInput: nums = [4,7,8,15,3,5]\nOutput: 2\nExplanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.\nThe only valid split is at index 2.\n\nExample 2:\n\nInput: nums = [4,7,15,8,3,5]\nOutput: -1\nExplanation: The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.\nThere is no valid split.\n\nConstraints:\n\n* `n == nums.length`\n* `1 <= n <= 104`\n* `1 <= nums[i] <= 106`"} {"task_id": "time-to-cross-a-bridge", "prompt": "def findCrossingTime(n: int, k: int, time: List[List[int]]) -> int:\n \"\"\"\n There are `k` workers who want to move `n` boxes from an old warehouse to a\n new one. You are given the two integers `n` and `k`, and a 2D integer array\n `time` of size `k x 4` where `time[i] = [leftToRighti, pickOldi, rightToLefti,\n putNewi]`.\n \n The warehouses are separated by a river and connected by a bridge. The old\n warehouse is on the right bank of the river, and the new warehouse is on the\n left bank of the river. Initially, all `k` workers are waiting on the left\n side of the bridge. To move the boxes, the `ith` worker (0-indexed) can :\n \n * Cross the bridge from the left bank (new warehouse) to the right bank (old warehouse) in `leftToRighti` minutes.\n * Pick a box from the old warehouse and return to the bridge in `pickOldi` minutes. Different workers can pick up their boxes simultaneously.\n * Cross the bridge from the right bank (old warehouse) to the left bank (new warehouse) in `rightToLefti` minutes.\n * Put the box in the new warehouse and return to the bridge in `putNewi` minutes. Different workers can put their boxes simultaneously.\n \n A worker `i` is less efficient than a worker `j` if either condition is met:\n \n * `leftToRighti + rightToLefti > leftToRightj + rightToLeftj`\n * `leftToRighti + rightToLefti == leftToRightj + rightToLeftj` and `i > j`\n \n The following rules regulate the movement of the workers through the bridge :\n \n * If a worker `x` reaches the bridge while another worker `y` is crossing the bridge, `x` waits at their side of the bridge.\n * If the bridge is free, the worker waiting on the right side of the bridge gets to cross the bridge. If more than one worker is waiting on the right side, the one with the lowest efficiency crosses first.\n * If the bridge is free and no worker is waiting on the right side, and at least one box remains at the old warehouse, the worker on the left side of the river gets to cross the bridge. If more than one worker is waiting on the left side, the one with the lowest efficiency crosses first.\n \n Return the instance of time at which the last worker reaches the left bank of\n the river after all n boxes have been put in the new warehouse.\n \n Example 1:\n \n Input: n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]\n Output: 6\n Explanation: \n From 0 to 1: worker 2 crosses the bridge from the left bank to the right bank.\n From 1 to 2: worker 2 picks up a box from the old warehouse.\n From 2 to 6: worker 2 crosses the bridge from the right bank to the left bank.\n From 6 to 7: worker 2 puts a box at the new warehouse.\n The whole process ends after 7 minutes. We return 6 because the problem asks for the instance of time at which the last worker reaches the left bank.\n \n Example 2:\n \n Input: n = 3, k = 2, time = [[1,9,1,8],[10,10,10,10]]\n Output: 50\n Explanation: \n From 0 \u00a0to 10: worker 1 crosses the bridge from the left bank to the right bank.\n From 10 to 20: worker 1 picks up a box from the old warehouse.\n From 10 to 11: worker 0 crosses the bridge from the left bank to the right bank.\n From 11 to 20: worker 0 picks up a box from the old warehouse.\n From 20 to 30: worker 1 crosses the bridge from the right bank to the left bank.\n From 30 to 40: worker 1 puts a box at the new warehouse.\n From 30 to 31: worker 0 crosses the bridge from the right bank to the left bank.\n From 31 to 39: worker 0 puts a box at the new warehouse.\n From 39 to 40: worker 0 crosses the bridge from the left bank to the right bank.\n From 40 to 49: worker 0 picks up a box from the old warehouse.\n From 49 to 50: worker 0 crosses the bridge from the right bank to the left bank.\n From 50 to 58: worker 0 puts a box at the new warehouse.\n The whole process ends after 58 minutes. We return 50 because the problem asks for the instance of time at which the last worker reaches the left bank.\n \n Constraints:\n \n * `1 <= n, k <= 104`\n * `time.length == k`\n * `time[i].length == 4`\n * `1 <= leftToRighti, pickOldi, rightToLefti, putNewi <= 1000`\n \"\"\"\n", "canonical_solution": "", "test": "assert findCrossingTime(1, 3, [[1,1,2,1],[1,1,3,1],[1,1,4,1]]) == 6\nassert findCrossingTime(3, 2, [[1,9,1,8],[10,10,10,10]]) == 50", "signature": "findCrossingTime(n: int, k: int, time: List[List[int]]) -> int:", "docstring": "There are `k` workers who want to move `n` boxes from an old warehouse to a\nnew one. You are given the two integers `n` and `k`, and a 2D integer array\n`time` of size `k x 4` where `time[i] = [leftToRighti, pickOldi, rightToLefti,\nputNewi]`.\n\nThe warehouses are separated by a river and connected by a bridge. The old\nwarehouse is on the right bank of the river, and the new warehouse is on the\nleft bank of the river. Initially, all `k` workers are waiting on the left\nside of the bridge. To move the boxes, the `ith` worker (0-indexed) can :\n\n* Cross the bridge from the left bank (new warehouse) to the right bank (old warehouse) in `leftToRighti` minutes.\n* Pick a box from the old warehouse and return to the bridge in `pickOldi` minutes. Different workers can pick up their boxes simultaneously.\n* Cross the bridge from the right bank (old warehouse) to the left bank (new warehouse) in `rightToLefti` minutes.\n* Put the box in the new warehouse and return to the bridge in `putNewi` minutes. Different workers can put their boxes simultaneously.\n\nA worker `i` is less efficient than a worker `j` if either condition is met:\n\n* `leftToRighti + rightToLefti > leftToRightj + rightToLeftj`\n* `leftToRighti + rightToLefti == leftToRightj + rightToLeftj` and `i > j`\n\nThe following rules regulate the movement of the workers through the bridge :\n\n* If a worker `x` reaches the bridge while another worker `y` is crossing the bridge, `x` waits at their side of the bridge.\n* If the bridge is free, the worker waiting on the right side of the bridge gets to cross the bridge. If more than one worker is waiting on the right side, the one with the lowest efficiency crosses first.\n* If the bridge is free and no worker is waiting on the right side, and at least one box remains at the old warehouse, the worker on the left side of the river gets to cross the bridge. If more than one worker is waiting on the left side, the one with the lowest efficiency crosses first.\n\nReturn the instance of time at which the last worker reaches the left bank of\nthe river after all n boxes have been put in the new warehouse.\n\nExample 1:\n\nInput: n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]\nOutput: 6\nExplanation: \nFrom 0 to 1: worker 2 crosses the bridge from the left bank to the right bank.\nFrom 1 to 2: worker 2 picks up a box from the old warehouse.\nFrom 2 to 6: worker 2 crosses the bridge from the right bank to the left bank.\nFrom 6 to 7: worker 2 puts a box at the new warehouse.\nThe whole process ends after 7 minutes. We return 6 because the problem asks for the instance of time at which the last worker reaches the left bank.\n\nExample 2:\n\nInput: n = 3, k = 2, time = [[1,9,1,8],[10,10,10,10]]\nOutput: 50\nExplanation: \nFrom 0 \u00a0to 10: worker 1 crosses the bridge from the left bank to the right bank.\nFrom 10 to 20: worker 1 picks up a box from the old warehouse.\nFrom 10 to 11: worker 0 crosses the bridge from the left bank to the right bank.\nFrom 11 to 20: worker 0 picks up a box from the old warehouse.\nFrom 20 to 30: worker 1 crosses the bridge from the right bank to the left bank.\nFrom 30 to 40: worker 1 puts a box at the new warehouse.\nFrom 30 to 31: worker 0 crosses the bridge from the right bank to the left bank.\nFrom 31 to 39: worker 0 puts a box at the new warehouse.\nFrom 39 to 40: worker 0 crosses the bridge from the left bank to the right bank.\nFrom 40 to 49: worker 0 picks up a box from the old warehouse.\nFrom 49 to 50: worker 0 crosses the bridge from the right bank to the left bank.\nFrom 50 to 58: worker 0 puts a box at the new warehouse.\nThe whole process ends after 58 minutes. We return 50 because the problem asks for the instance of time at which the last worker reaches the left bank.\n\nConstraints:\n\n* `1 <= n, k <= 104`\n* `time.length == k`\n* `time[i].length == 4`\n* `1 <= leftToRighti, pickOldi, rightToLefti, putNewi <= 1000`"} {"task_id": "check-if-point-is-reachable", "prompt": "def isReachable(targetX: int, targetY: int) -> bool:\n \"\"\"\n There exists an infinitely large grid. You are currently at point `(1, 1)`,\n and you need to reach the point `(targetX, targetY)` using a finite number of\n steps.\n \n In one step, you can move from point `(x, y)` to any one of the following\n points:\n \n * `(x, y - x)`\n * `(x - y, y)`\n * `(2 * x, y)`\n * `(x, 2 * y)`\n \n Given two integers `targetX` and `targetY` representing the X-coordinate and\n Y-coordinate of your final position, return `true` if you can reach the point\n from `(1, 1)` using some number of steps, and `false` otherwise.\n \n Example 1:\n \n Input: targetX = 6, targetY = 9\n Output: false\n Explanation: It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.\n \n Example 2:\n \n Input: targetX = 4, targetY = 7\n Output: true\n Explanation: You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).\n \n Constraints:\n \n * `1 <= targetX, targetY <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert isReachable(6, 9) == False\nassert isReachable(4, 7) == True", "signature": "isReachable(targetX: int, targetY: int) -> bool:", "docstring": "There exists an infinitely large grid. You are currently at point `(1, 1)`,\nand you need to reach the point `(targetX, targetY)` using a finite number of\nsteps.\n\nIn one step, you can move from point `(x, y)` to any one of the following\npoints:\n\n* `(x, y - x)`\n* `(x - y, y)`\n* `(2 * x, y)`\n* `(x, 2 * y)`\n\nGiven two integers `targetX` and `targetY` representing the X-coordinate and\nY-coordinate of your final position, return `true` if you can reach the point\nfrom `(1, 1)` using some number of steps, and `false` otherwise.\n\nExample 1:\n\nInput: targetX = 6, targetY = 9\nOutput: false\nExplanation: It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned.\n\nExample 2:\n\nInput: targetX = 4, targetY = 7\nOutput: true\nExplanation: You can follow the path (1,1) -> (1,2) -> (1,4) -> (1,8) -> (1,7) -> (2,7) -> (4,7).\n\nConstraints:\n\n* `1 <= targetX, targetY <= 109`"} {"task_id": "minimum-cost-to-split-an-array", "prompt": "def minCost(nums: List[int], k: int) -> int:\n \"\"\"\n You are given an integer array `nums` and an integer `k`.\n \n Split the array into some number of non-empty subarrays. The cost of a split\n is the sum of the importance value of each subarray in the split.\n \n Let `trimmed(subarray)` be the version of the subarray where all numbers which\n appear only once are removed.\n \n * For example, `trimmed([3,1,2,4,3,4]) = [3,4,3,4].`\n \n The importance value of a subarray is `k + trimmed(subarray).length`.\n \n * 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`.\n \n Return the minimum possible cost of a split of `nums`.\n \n A subarray is a contiguous non-empty sequence of elements within an array.\n \n Example 1:\n \n Input: nums = [1,2,1,2,1,3,3], k = 2\n Output: 8\n Explanation: We split nums to have two subarrays: [1,2], [1,2,1,3,3].\n The importance value of [1,2] is 2 + (0) = 2.\n The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.\n 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.\n \n Example 2:\n \n Input: nums = [1,2,1,2,1], k = 2\n Output: 6\n Explanation: We split nums to have two subarrays: [1,2], [1,2,1].\n The importance value of [1,2] is 2 + (0) = 2.\n The importance value of [1,2,1] is 2 + (2) = 4.\n 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.\n \n Example 3:\n \n Input: nums = [1,2,1,2,1], k = 5\n Output: 10\n Explanation: We split nums to have one subarray: [1,2,1,2,1].\n The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.\n The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.\n \n Constraints:\n \n * `1 <= nums.length <= 1000`\n * `0 <= nums[i] < nums.length`\n * `1 <= k <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert minCost([1,2,1,2,1,3,3], 2) == 8\nassert minCost([1,2,1,2,1], 2) == 6\nassert minCost([1,2,1,2,1], 5) == 10", "signature": "minCost(nums: List[int], k: int) -> int:", "docstring": "You are given an integer array `nums` and an integer `k`.\n\nSplit the array into some number of non-empty subarrays. The cost of a split\nis the sum of the importance value of each subarray in the split.\n\nLet `trimmed(subarray)` be the version of the subarray where all numbers which\nappear only once are removed.\n\n* For example, `trimmed([3,1,2,4,3,4]) = [3,4,3,4].`\n\nThe importance value of a subarray is `k + trimmed(subarray).length`.\n\n* 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`.\n\nReturn the minimum possible cost of a split of `nums`.\n\nA subarray is a contiguous non-empty sequence of elements within an array.\n\nExample 1:\n\nInput: nums = [1,2,1,2,1,3,3], k = 2\nOutput: 8\nExplanation: We split nums to have two subarrays: [1,2], [1,2,1,3,3].\nThe importance value of [1,2] is 2 + (0) = 2.\nThe importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.\nThe cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.\n\nExample 2:\n\nInput: nums = [1,2,1,2,1], k = 2\nOutput: 6\nExplanation: We split nums to have two subarrays: [1,2], [1,2,1].\nThe importance value of [1,2] is 2 + (0) = 2.\nThe importance value of [1,2,1] is 2 + (2) = 4.\nThe cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.\n\nExample 3:\n\nInput: nums = [1,2,1,2,1], k = 5\nOutput: 10\nExplanation: We split nums to have one subarray: [1,2,1,2,1].\nThe importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.\nThe cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.\n\nConstraints:\n\n* `1 <= nums.length <= 1000`\n* `0 <= nums[i] < nums.length`\n* `1 <= k <= 109`"} {"task_id": "difference-between-maximum-and-minimum-price-sum", "prompt": "def maxOutput(n: int, edges: List[List[int]], price: List[int]) -> int:\n \"\"\"\n There exists an undirected and initially unrooted tree with `n` nodes indexed\n from `0` to `n - 1`. You are given the integer `n` and a 2D integer array\n `edges` of length `n - 1`, where `edges[i] = [ai, bi]` indicates that there is\n an edge between nodes `ai` and `bi` in the tree.\n \n Each node has an associated price. You are given an integer array `price`,\n where `price[i]` is the price of the `ith` node.\n \n The price sum of a given path is the sum of the prices of all nodes lying on\n that path.\n \n The tree can be rooted at any node `root` of your choice. The incurred cost\n after choosing `root` is the difference between the maximum and minimum price\n sum amongst all paths starting at `root`.\n \n Return the maximum possible cost amongst all possible root choices.\n \n Example 1:\n \n Input: n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]\n Output: 24\n 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.\n - The first path contains nodes [2,1,3,4]: the prices are [7,8,6,10], and the sum of the prices is 31.\n - The second path contains the node [2] with the price [7].\n The difference between the maximum and minimum price sum is 24. It can be proved that 24 is the maximum cost.\n \n Example 2:\n \n Input: n = 3, edges = [[0,1],[1,2]], price = [1,1,1]\n Output: 2\n 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.\n - The first path contains nodes [0,1,2]: the prices are [1,1,1], and the sum of the prices is 3.\n - The second path contains node [0] with a price [1].\n The difference between the maximum and minimum price sum is 2. It can be proved that 2 is the maximum cost.\n \n Constraints:\n \n * `1 <= n <= 105`\n * `edges.length == n - 1`\n * `0 <= ai, bi <= n - 1`\n * `edges` represents a valid tree.\n * `price.length == n`\n * `1 <= price[i] <= 105`\n \"\"\"\n", "canonical_solution": "", "test": "assert maxOutput(6, [[0,1],[1,2],[1,3],[3,4],[3,5]], [9,8,7,6,10,5]) == 24\nassert maxOutput(3, [[0,1],[1,2]], [1,1,1]) == 2", "signature": "maxOutput(n: int, edges: List[List[int]], price: List[int]) -> int:", "docstring": "There exists an undirected and initially unrooted tree with `n` nodes indexed\nfrom `0` to `n - 1`. You are given the integer `n` and a 2D integer array\n`edges` of length `n - 1`, where `edges[i] = [ai, bi]` indicates that there is\nan edge between nodes `ai` and `bi` in the tree.\n\nEach node has an associated price. You are given an integer array `price`,\nwhere `price[i]` is the price of the `ith` node.\n\nThe price sum of a given path is the sum of the prices of all nodes lying on\nthat path.\n\nThe tree can be rooted at any node `root` of your choice. The incurred cost\nafter choosing `root` is the difference between the maximum and minimum price\nsum amongst all paths starting at `root`.\n\nReturn the maximum possible cost amongst all possible root choices.\n\nExample 1:\n\nInput: n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5]\nOutput: 24\nExplanation: 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.\n- The first path contains nodes [2,1,3,4]: the prices are [7,8,6,10], and the sum of the prices is 31.\n- The second path contains the node [2] with the price [7].\nThe difference between the maximum and minimum price sum is 24. It can be proved that 24 is the maximum cost.\n\nExample 2:\n\nInput: n = 3, edges = [[0,1],[1,2]], price = [1,1,1]\nOutput: 2\nExplanation: 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.\n- The first path contains nodes [0,1,2]: the prices are [1,1,1], and the sum of the prices is 3.\n- The second path contains node [0] with a price [1].\nThe difference between the maximum and minimum price sum is 2. It can be proved that 2 is the maximum cost.\n\nConstraints:\n\n* `1 <= n <= 105`\n* `edges.length == n - 1`\n* `0 <= ai, bi <= n - 1`\n* `edges` represents a valid tree.\n* `price.length == n`\n* `1 <= price[i] <= 105`"} {"task_id": "maximize-the-minimum-powered-city", "prompt": "def maxPower(stations: List[int], r: int, k: int) -> int:\n \"\"\"\n You are given a 0-indexed integer array `stations` of length `n`, where\n `stations[i]` represents the number of power stations in the `ith` city.\n \n Each power station can provide power to every city in a fixed range. In other\n words, if the range is denoted by `r`, then a power station at city `i` can\n provide power to all cities `j` such that `|i - j| <= r` and `0 <= i, j <= n -\n 1`.\n \n * Note that `|x|` denotes absolute value. For example, `|7 - 5| = 2` and `|3 - 10| = 7`.\n \n The power of a city is the total number of power stations it is being provided\n power from.\n \n The government has sanctioned building `k` more power stations, each of which\n can be built in any city, and have the same range as the pre-existing ones.\n \n Given the two integers `r` and `k`, return the maximum possible minimum power\n of a city, if the additional power stations are built optimally.\n \n Note that you can build the `k` power stations in multiple cities.\n \n Example 1:\n \n Input: stations = [1,2,4,5,0], r = 1, k = 2\n Output: 5\n Explanation: \n One of the optimal ways is to install both the power stations at city 1. \n So stations will become [1,4,4,5,0].\n - City 0 is provided by 1 + 4 = 5 power stations.\n - City 1 is provided by 1 + 4 + 4 = 9 power stations.\n - City 2 is provided by 4 + 4 + 5 = 13 power stations.\n - City 3 is provided by 5 + 4 = 9 power stations.\n - City 4 is provided by 5 + 0 = 5 power stations.\n So the minimum power of a city is 5.\n Since it is not possible to obtain a larger power, we return 5.\n \n Example 2:\n \n Input: stations = [4,4,4,4], r = 0, k = 3\n Output: 4\n Explanation: \n It can be proved that we cannot make the minimum power of a city greater than 4.\n \n Constraints:\n \n * `n == stations.length`\n * `1 <= n <= 105`\n * `0 <= stations[i] <= 105`\n * `0 <= r <= n - 1`\n * `0 <= k <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert maxPower([1,2,4,5,0], 1, 2) == 5\nassert maxPower([4,4,4,4], 0, 3) == 4", "signature": "maxPower(stations: List[int], r: int, k: int) -> int:", "docstring": "You are given a 0-indexed integer array `stations` of length `n`, where\n`stations[i]` represents the number of power stations in the `ith` city.\n\nEach power station can provide power to every city in a fixed range. In other\nwords, if the range is denoted by `r`, then a power station at city `i` can\nprovide power to all cities `j` such that `|i - j| <= r` and `0 <= i, j <= n -\n1`.\n\n* Note that `|x|` denotes absolute value. For example, `|7 - 5| = 2` and `|3 - 10| = 7`.\n\nThe power of a city is the total number of power stations it is being provided\npower from.\n\nThe government has sanctioned building `k` more power stations, each of which\ncan be built in any city, and have the same range as the pre-existing ones.\n\nGiven the two integers `r` and `k`, return the maximum possible minimum power\nof a city, if the additional power stations are built optimally.\n\nNote that you can build the `k` power stations in multiple cities.\n\nExample 1:\n\nInput: stations = [1,2,4,5,0], r = 1, k = 2\nOutput: 5\nExplanation: \nOne of the optimal ways is to install both the power stations at city 1. \nSo stations will become [1,4,4,5,0].\n- City 0 is provided by 1 + 4 = 5 power stations.\n- City 1 is provided by 1 + 4 + 4 = 9 power stations.\n- City 2 is provided by 4 + 4 + 5 = 13 power stations.\n- City 3 is provided by 5 + 4 = 9 power stations.\n- City 4 is provided by 5 + 0 = 5 power stations.\nSo the minimum power of a city is 5.\nSince it is not possible to obtain a larger power, we return 5.\n\nExample 2:\n\nInput: stations = [4,4,4,4], r = 0, k = 3\nOutput: 4\nExplanation: \nIt can be proved that we cannot make the minimum power of a city greater than 4.\n\nConstraints:\n\n* `n == stations.length`\n* `1 <= n <= 105`\n* `0 <= stations[i] <= 105`\n* `0 <= r <= n - 1`\n* `0 <= k <= 109`"} {"task_id": "count-anagrams", "prompt": "def countAnagrams(s: str) -> int:\n \"\"\"\n You are given a string `s` containing one or more words. Every consecutive\n pair of words is separated by a single space `' '`.\n \n A string `t` is an anagram of string `s` if the `ith` word of `t` is a\n permutation of the `ith` word of `s`.\n \n * For example, `\"acb dfe\"` is an anagram of `\"abc def\"`, but `\"def cab\"` and `\"adc bef\"` are not.\n \n Return the number of distinct anagrams of `s`. Since the answer may be very\n large, return it modulo `109 + 7`.\n \n Example 1:\n \n Input: s = \"too hot\"\n Output: 18\n Explanation: Some of the anagrams of the given string are \"too hot\", \"oot hot\", \"oto toh\", \"too toh\", and \"too oht\".\n \n Example 2:\n \n Input: s = \"aa\"\n Output: 1\n Explanation: There is only one anagram possible for the given string.\n \n Constraints:\n \n * `1 <= s.length <= 105`\n * `s` consists of lowercase English letters and spaces `' '`.\n * There is single space between consecutive words.\n \"\"\"\n", "canonical_solution": "", "test": "assert countAnagrams(\"too hot\") == 18\nassert countAnagrams(\"aa\") == 1", "signature": "countAnagrams(s: str) -> int:", "docstring": "You are given a string `s` containing one or more words. Every consecutive\npair of words is separated by a single space `' '`.\n\nA string `t` is an anagram of string `s` if the `ith` word of `t` is a\npermutation of the `ith` word of `s`.\n\n* For example, `\"acb dfe\"` is an anagram of `\"abc def\"`, but `\"def cab\"` and `\"adc bef\"` are not.\n\nReturn the number of distinct anagrams of `s`. Since the answer may be very\nlarge, return it modulo `109 + 7`.\n\nExample 1:\n\nInput: s = \"too hot\"\nOutput: 18\nExplanation: Some of the anagrams of the given string are \"too hot\", \"oot hot\", \"oto toh\", \"too toh\", and \"too oht\".\n\nExample 2:\n\nInput: s = \"aa\"\nOutput: 1\nExplanation: There is only one anagram possible for the given string.\n\nConstraints:\n\n* `1 <= s.length <= 105`\n* `s` consists of lowercase English letters and spaces `' '`.\n* There is single space between consecutive words."} {"task_id": "number-of-great-partitions", "prompt": "def countPartitions(nums: List[int], k: int) -> int:\n \"\"\"\n You are given an array `nums` consisting of positive integers and an integer\n `k`.\n \n Partition the array into two ordered groups such that each element is in\n exactly one group. A partition is called great if the sum of elements of each\n group is greater than or equal to `k`.\n \n Return the number of distinct great partitions. Since the answer may be too\n large, return it modulo `109 + 7`.\n \n Two partitions are considered distinct if some element `nums[i]` is in\n different groups in the two partitions.\n \n Example 1:\n \n Input: nums = [1,2,3,4], k = 4\n Output: 6\n Explanation: The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).\n \n Example 2:\n \n Input: nums = [3,3,3], k = 4\n Output: 0\n Explanation: There are no great partitions for this array.\n \n Example 3:\n \n Input: nums = [6,6], k = 2\n Output: 2\n Explanation: We can either put nums[0] in the first partition or in the second partition.\n The great partitions will be ([6], [6]) and ([6], [6]).\n \n Constraints:\n \n * `1 <= nums.length, k <= 1000`\n * `1 <= nums[i] <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert countPartitions([1,2,3,4], 4) == 6\nassert countPartitions([3,3,3], 4) == 0\nassert countPartitions([6,6], 2) == 2", "signature": "countPartitions(nums: List[int], k: int) -> int:", "docstring": "You are given an array `nums` consisting of positive integers and an integer\n`k`.\n\nPartition the array into two ordered groups such that each element is in\nexactly one group. A partition is called great if the sum of elements of each\ngroup is greater than or equal to `k`.\n\nReturn the number of distinct great partitions. Since the answer may be too\nlarge, return it modulo `109 + 7`.\n\nTwo partitions are considered distinct if some element `nums[i]` is in\ndifferent groups in the two partitions.\n\nExample 1:\n\nInput: nums = [1,2,3,4], k = 4\nOutput: 6\nExplanation: The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).\n\nExample 2:\n\nInput: nums = [3,3,3], k = 4\nOutput: 0\nExplanation: There are no great partitions for this array.\n\nExample 3:\n\nInput: nums = [6,6], k = 2\nOutput: 2\nExplanation: We can either put nums[0] in the first partition or in the second partition.\nThe great partitions will be ([6], [6]) and ([6], [6]).\n\nConstraints:\n\n* `1 <= nums.length, k <= 1000`\n* `1 <= nums[i] <= 109`"} {"task_id": "cycle-length-queries-in-a-tree", "prompt": "def cycleLengthQueries(n: int, queries: List[List[int]]) -> List[int]:\n \"\"\"\n You are given an integer `n`. There is a complete binary tree with `2n - 1`\n nodes. The root of that tree is the node with the value `1`, and every node\n with a value `val` in the range `[1, 2n - 1 - 1]` has two children where:\n \n * The left node has the value `2 * val`, and\n * The right node has the value `2 * val + 1`.\n \n You are also given a 2D integer array `queries` of length `m`, where\n `queries[i] = [ai, bi]`. For each query, solve the following problem:\n \n 1. Add an edge between the nodes with values `ai` and `bi`.\n 2. Find the length of the cycle in the graph.\n 3. Remove the added edge between nodes with values `ai` and `bi`.\n \n Note that:\n \n * A cycle is a path that starts and ends at the same node, and each edge in the path is visited only once.\n * The length of a cycle is the number of edges visited in the cycle.\n * There could be multiple edges between two nodes in the tree after adding the edge of the query.\n \n Return an array `answer` of length `m` where `answer[i]` is the answer to the\n `ith` query.\n \n Example 1:\n \n Input: n = 3, queries = [[5,3],[4,7],[2,3]]\n Output: [4,5,3]\n 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.\n - 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.\n - 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.\n - 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.\n \n Example 2:\n \n Input: n = 2, queries = [[1,2]]\n Output: [2]\n 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.\n - 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.\n \n Constraints:\n \n * `2 <= n <= 30`\n * `m == queries.length`\n * `1 <= m <= 105`\n * `queries[i].length == 2`\n * `1 <= ai, bi <= 2n - 1`\n * `ai != bi`\n \"\"\"\n", "canonical_solution": "", "test": "assert cycleLengthQueries(3, [[5,3],[4,7],[2,3]]) == [4,5,3]\nassert cycleLengthQueries(2, [[1,2]]) == [2]", "signature": "cycleLengthQueries(n: int, queries: List[List[int]]) -> List[int]:", "docstring": "You are given an integer `n`. There is a complete binary tree with `2n - 1`\nnodes. The root of that tree is the node with the value `1`, and every node\nwith a value `val` in the range `[1, 2n - 1 - 1]` has two children where:\n\n* The left node has the value `2 * val`, and\n* The right node has the value `2 * val + 1`.\n\nYou are also given a 2D integer array `queries` of length `m`, where\n`queries[i] = [ai, bi]`. For each query, solve the following problem:\n\n1. Add an edge between the nodes with values `ai` and `bi`.\n2. Find the length of the cycle in the graph.\n3. Remove the added edge between nodes with values `ai` and `bi`.\n\nNote that:\n\n* A cycle is a path that starts and ends at the same node, and each edge in the path is visited only once.\n* The length of a cycle is the number of edges visited in the cycle.\n* There could be multiple edges between two nodes in the tree after adding the edge of the query.\n\nReturn an array `answer` of length `m` where `answer[i]` is the answer to the\n`ith` query.\n\nExample 1:\n\nInput: n = 3, queries = [[5,3],[4,7],[2,3]]\nOutput: [4,5,3]\nExplanation: The diagrams above show the tree of 23 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.\n- 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.\n- 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.\n- 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.\n\nExample 2:\n\nInput: n = 2, queries = [[1,2]]\nOutput: [2]\nExplanation: The diagram above shows the tree of 22 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.\n- 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.\n\nConstraints:\n\n* `2 <= n <= 30`\n* `m == queries.length`\n* `1 <= m <= 105`\n* `queries[i].length == 2`\n* `1 <= ai, bi <= 2n - 1`\n* `ai != bi`"} {"task_id": "add-edges-to-make-degrees-of-all-nodes-even", "prompt": "def isPossible(n: int, edges: List[List[int]]) -> bool:\n \"\"\"\n There is an undirected graph consisting of `n` nodes numbered from `1` to `n`.\n You are given the integer `n` and a 2D array `edges` where `edges[i] = [ai,\n bi]` indicates that there is an edge between nodes `ai` and `bi`. The graph\n can be disconnected.\n \n You can add at most two additional edges (possibly none) to this graph so that\n there are no repeated edges and no self-loops.\n \n Return `true` if it is possible to make the degree of each node in the graph\n even, otherwise return `false`.\n \n The degree of a node is the number of edges connected to it.\n \n Example 1:\n \n Input: n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]\n Output: true\n Explanation: The above diagram shows a valid way of adding an edge.\n Every node in the resulting graph is connected to an even number of edges.\n \n Example 2:\n \n Input: n = 4, edges = [[1,2],[3,4]]\n Output: true\n Explanation: The above diagram shows a valid way of adding two edges.\n \n Example 3:\n \n Input: n = 4, edges = [[1,2],[1,3],[1,4]]\n Output: false\n Explanation: It is not possible to obtain a valid graph with adding at most 2 edges.\n \n Constraints:\n \n * `3 <= n <= 105`\n * `2 <= edges.length <= 105`\n * `edges[i].length == 2`\n * `1 <= ai, bi <= n`\n * `ai != bi`\n * There are no repeated edges.\n \"\"\"\n", "canonical_solution": "", "test": "assert isPossible(5, [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]) == True\nassert isPossible(4, [[1,2],[3,4]]) == True\nassert isPossible(4, [[1,2],[1,3],[1,4]]) == False", "signature": "isPossible(n: int, edges: List[List[int]]) -> bool:", "docstring": "There is an undirected graph consisting of `n` nodes numbered from `1` to `n`.\nYou are given the integer `n` and a 2D array `edges` where `edges[i] = [ai,\nbi]` indicates that there is an edge between nodes `ai` and `bi`. The graph\ncan be disconnected.\n\nYou can add at most two additional edges (possibly none) to this graph so that\nthere are no repeated edges and no self-loops.\n\nReturn `true` if it is possible to make the degree of each node in the graph\neven, otherwise return `false`.\n\nThe degree of a node is the number of edges connected to it.\n\nExample 1:\n\nInput: n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]\nOutput: true\nExplanation: The above diagram shows a valid way of adding an edge.\nEvery node in the resulting graph is connected to an even number of edges.\n\nExample 2:\n\nInput: n = 4, edges = [[1,2],[3,4]]\nOutput: true\nExplanation: The above diagram shows a valid way of adding two edges.\n\nExample 3:\n\nInput: n = 4, edges = [[1,2],[1,3],[1,4]]\nOutput: false\nExplanation: It is not possible to obtain a valid graph with adding at most 2 edges.\n\nConstraints:\n\n* `3 <= n <= 105`\n* `2 <= edges.length <= 105`\n* `edges[i].length == 2`\n* `1 <= ai, bi <= n`\n* `ai != bi`\n* There are no repeated edges."} {"task_id": "minimum-total-cost-to-make-arrays-unequal", "prompt": "def minimumTotalCost(nums1: List[int], nums2: List[int]) -> int:\n \"\"\"\n You are given two 0-indexed integer arrays `nums1` and `nums2`, of equal\n length `n`.\n \n In one operation, you can swap the values of any two indices of `nums1`. The\n cost of this operation is the sum of the indices.\n \n Find the minimum total cost of performing the given operation any number of\n times such that `nums1[i] != nums2[i]` for all `0 <= i <= n - 1` after\n performing all the operations.\n \n Return the minimum total cost such that `nums1` and `nums2` satisfy the above\n condition. In case it is not possible, return `-1`.\n \n Example 1:\n \n Input: nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]\n Output: 10\n Explanation: \n One of the ways we can perform the operations is:\n - Swap values at indices 0 and 3, incurring cost = 0 + 3 = 3. Now, nums1 = [4,2,3,1,5]\n - Swap values at indices 1 and 2, incurring cost = 1 + 2 = 3. Now, nums1 = [4,3,2,1,5].\n - Swap values at indices 0 and 4, incurring cost = 0 + 4 = 4. Now, nums1 =[5,3,2,1,4].\n We can see that for each index i, nums1[i] != nums2[i]. The cost required here is 10.\n Note that there are other ways to swap values, but it can be proven that it is not possible to obtain a cost less than 10.\n \n Example 2:\n \n Input: nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]\n Output: 10\n Explanation: \n One of the ways we can perform the operations is:\n - Swap values at indices 2 and 3, incurring cost = 2 + 3 = 5. Now, nums1 = [2,2,1,2,3].\n - Swap values at indices 1 and 4, incurring cost = 1 + 4 = 5. Now, nums1 = [2,3,1,2,2].\n The total cost needed here is 10, which is the minimum possible.\n \n Example 3:\n \n Input: nums1 = [1,2,2], nums2 = [1,2,2]\n Output: -1\n Explanation: \n It can be shown that it is not possible to satisfy the given conditions irrespective of the number of operations we perform.\n Hence, we return -1.\n \n Constraints:\n \n * `n == nums1.length == nums2.length`\n * `1 <= n <= 105`\n * `1 <= nums1[i], nums2[i] <= n`\n \"\"\"\n", "canonical_solution": "", "test": "assert minimumTotalCost([1,2,3,4,5], [1,2,3,4,5]) == 10\nassert minimumTotalCost([2,2,2,1,3], [1,2,2,3,3]) == 10\nassert minimumTotalCost([1,2,2], [1,2,2]) == -1", "signature": "minimumTotalCost(nums1: List[int], nums2: List[int]) -> int:", "docstring": "You are given two 0-indexed integer arrays `nums1` and `nums2`, of equal\nlength `n`.\n\nIn one operation, you can swap the values of any two indices of `nums1`. The\ncost of this operation is the sum of the indices.\n\nFind the minimum total cost of performing the given operation any number of\ntimes such that `nums1[i] != nums2[i]` for all `0 <= i <= n - 1` after\nperforming all the operations.\n\nReturn the minimum total cost such that `nums1` and `nums2` satisfy the above\ncondition. In case it is not possible, return `-1`.\n\nExample 1:\n\nInput: nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]\nOutput: 10\nExplanation: \nOne of the ways we can perform the operations is:\n- Swap values at indices 0 and 3, incurring cost = 0 + 3 = 3. Now, nums1 = [4,2,3,1,5]\n- Swap values at indices 1 and 2, incurring cost = 1 + 2 = 3. Now, nums1 = [4,3,2,1,5].\n- Swap values at indices 0 and 4, incurring cost = 0 + 4 = 4. Now, nums1 =[5,3,2,1,4].\nWe can see that for each index i, nums1[i] != nums2[i]. The cost required here is 10.\nNote that there are other ways to swap values, but it can be proven that it is not possible to obtain a cost less than 10.\n\nExample 2:\n\nInput: nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]\nOutput: 10\nExplanation: \nOne of the ways we can perform the operations is:\n- Swap values at indices 2 and 3, incurring cost = 2 + 3 = 5. Now, nums1 = [2,2,1,2,3].\n- Swap values at indices 1 and 4, incurring cost = 1 + 4 = 5. Now, nums1 = [2,3,1,2,2].\nThe total cost needed here is 10, which is the minimum possible.\n\nExample 3:\n\nInput: nums1 = [1,2,2], nums2 = [1,2,2]\nOutput: -1\nExplanation: \nIt can be shown that it is not possible to satisfy the given conditions irrespective of the number of operations we perform.\nHence, we return -1.\n\nConstraints:\n\n* `n == nums1.length == nums2.length`\n* `1 <= n <= 105`\n* `1 <= nums1[i], nums2[i] <= n`"} {"task_id": "maximum-number-of-points-from-grid-queries", "prompt": "def maxPoints(grid: List[List[int]], queries: List[int]) -> List[int]:\n \"\"\"\n You are given an `m x n` integer matrix `grid` and an array `queries` of size\n `k`.\n \n Find an array `answer` of size `k` such that for each integer `queries[i]` you\n start in the top left cell of the matrix and repeat the following process:\n \n * If `queries[i]` is strictly greater than the value of the current cell that you are in, then you get one point if it is your first time visiting this cell, and you can move to any adjacent cell in all `4` directions: up, down, left, and right.\n * Otherwise, you do not get any points, and you end this process.\n \n After the process, `answer[i]` is the maximum number of points you can get.\n Note that for each query you are allowed to visit the same cell multiple\n times.\n \n Return the resulting array `answer`.\n \n Example 1:\n \n Input: grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]\n Output: [5,8,1]\n Explanation: The diagrams above show which cells we visit to get points for each query.\n \n Example 2:\n \n Input: grid = [[5,2,1],[1,1,2]], queries = [3]\n Output: [0]\n Explanation: We can not get any points because the value of the top left cell is already greater than or equal to 3.\n \n Constraints:\n \n * `m == grid.length`\n * `n == grid[i].length`\n * `2 <= m, n <= 1000`\n * `4 <= m * n <= 105`\n * `k == queries.length`\n * `1 <= k <= 104`\n * `1 <= grid[i][j], queries[i] <= 106`\n \"\"\"\n", "canonical_solution": "", "test": "assert maxPoints([[1,2,3],[2,5,7],[3,5,1]], [5,6,2]) == [5,8,1]\nassert maxPoints([[5,2,1],[1,1,2]], [3]) == [0]", "signature": "maxPoints(grid: List[List[int]], queries: List[int]) -> List[int]:", "docstring": "You are given an `m x n` integer matrix `grid` and an array `queries` of size\n`k`.\n\nFind an array `answer` of size `k` such that for each integer `queries[i]` you\nstart in the top left cell of the matrix and repeat the following process:\n\n* If `queries[i]` is strictly greater than the value of the current cell that you are in, then you get one point if it is your first time visiting this cell, and you can move to any adjacent cell in all `4` directions: up, down, left, and right.\n* Otherwise, you do not get any points, and you end this process.\n\nAfter the process, `answer[i]` is the maximum number of points you can get.\nNote that for each query you are allowed to visit the same cell multiple\ntimes.\n\nReturn the resulting array `answer`.\n\nExample 1:\n\nInput: grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]\nOutput: [5,8,1]\nExplanation: The diagrams above show which cells we visit to get points for each query.\n\nExample 2:\n\nInput: grid = [[5,2,1],[1,1,2]], queries = [3]\nOutput: [0]\nExplanation: We can not get any points because the value of the top left cell is already greater than or equal to 3.\n\nConstraints:\n\n* `m == grid.length`\n* `n == grid[i].length`\n* `2 <= m, n <= 1000`\n* `4 <= m * n <= 105`\n* `k == queries.length`\n* `1 <= k <= 104`\n* `1 <= grid[i][j], queries[i] <= 106`"} {"task_id": "divide-nodes-into-the-maximum-number-of-groups", "prompt": "def magnificentSets(n: int, edges: List[List[int]]) -> int:\n \"\"\"\n You are given a positive integer `n` representing the number of nodes in an\n undirected graph. The nodes are labeled from `1` to `n`.\n \n You are also given a 2D integer array `edges`, where `edges[i] = [ai, bi]`\n indicates that there is a bidirectional edge between nodes `ai` and `bi`.\n Notice that the given graph may be disconnected.\n \n Divide the nodes of the graph into `m` groups (1-indexed) such that:\n \n * Each node in the graph belongs to exactly one group.\n * 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`.\n \n Return the maximum number of groups (i.e., maximum `m`) into which you can\n divide the nodes. Return `-1` if it is impossible to group the nodes with the\n given conditions.\n \n Example 1:\n \n Input: n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]\n Output: 4\n Explanation: As shown in the image we:\n - Add node 5 to the first group.\n - Add node 1 to the second group.\n - Add nodes 2 and 4 to the third group.\n - Add nodes 3 and 6 to the fourth group.\n We can see that every edge is satisfied.\n 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.\n \n Example 2:\n \n Input: n = 3, edges = [[1,2],[2,3],[3,1]]\n Output: -1\n 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.\n It can be shown that no grouping is possible.\n \n Constraints:\n \n * `1 <= n <= 500`\n * `1 <= edges.length <= 104`\n * `edges[i].length == 2`\n * `1 <= ai, bi <= n`\n * `ai != bi`\n * There is at most one edge between any pair of vertices.\n \"\"\"\n", "canonical_solution": "", "test": "assert magnificentSets(6, [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]) == 4\nassert magnificentSets(3, [[1,2],[2,3],[3,1]]) == -1", "signature": "magnificentSets(n: int, edges: List[List[int]]) -> int:", "docstring": "You are given a positive integer `n` representing the number of nodes in an\nundirected graph. The nodes are labeled from `1` to `n`.\n\nYou are also given a 2D integer array `edges`, where `edges[i] = [ai, bi]`\nindicates that there is a bidirectional edge between nodes `ai` and `bi`.\nNotice that the given graph may be disconnected.\n\nDivide the nodes of the graph into `m` groups (1-indexed) such that:\n\n* Each node in the graph belongs to exactly one group.\n* 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`.\n\nReturn the maximum number of groups (i.e., maximum `m`) into which you can\ndivide the nodes. Return `-1` if it is impossible to group the nodes with the\ngiven conditions.\n\nExample 1:\n\nInput: n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]\nOutput: 4\nExplanation: As shown in the image we:\n- Add node 5 to the first group.\n- Add node 1 to the second group.\n- Add nodes 2 and 4 to the third group.\n- Add nodes 3 and 6 to the fourth group.\nWe can see that every edge is satisfied.\nIt 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.\n\nExample 2:\n\nInput: n = 3, edges = [[1,2],[2,3],[3,1]]\nOutput: -1\nExplanation: 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.\nIt can be shown that no grouping is possible.\n\nConstraints:\n\n* `1 <= n <= 500`\n* `1 <= edges.length <= 104`\n* `edges[i].length == 2`\n* `1 <= ai, bi <= n`\n* `ai != bi`\n* There is at most one edge between any pair of vertices."} {"task_id": "count-palindromic-subsequences", "prompt": "def countPalindromes(s: str) -> int:\n \"\"\"\n Given a string of digits `s`, return the number of palindromic subsequences of\n `s` having length `5`. Since the answer may be very large, return it modulo\n `109 + 7`.\n \n Note:\n \n * A string is palindromic if it reads the same forward and backward.\n * 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.\n \n Example 1:\n \n Input: s = \"103301\"\n Output: 2\n Explanation: \n There are 6 possible subsequences of length 5: \"10330\",\"10331\",\"10301\",\"10301\",\"13301\",\"03301\". \n Two of them (both equal to \"10301\") are palindromic.\n \n Example 2:\n \n Input: s = \"0000000\"\n Output: 21\n Explanation: All 21 subsequences are \"00000\", which is palindromic.\n \n Example 3:\n \n Input: s = \"9999900000\"\n Output: 2\n Explanation: The only two palindromic subsequences are \"99999\" and \"00000\".\n \n Constraints:\n \n * `1 <= s.length <= 104`\n * `s` consists of digits.\n \"\"\"\n", "canonical_solution": "", "test": "assert countPalindromes(\"103301\") == 2\nassert countPalindromes(\"0000000\") == 21\nassert countPalindromes(\"9999900000\") == 2", "signature": "countPalindromes(s: str) -> int:", "docstring": "Given a string of digits `s`, return the number of palindromic subsequences of\n`s` having length `5`. Since the answer may be very large, return it modulo\n`109 + 7`.\n\nNote:\n\n* A string is palindromic if it reads the same forward and backward.\n* 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.\n\nExample 1:\n\nInput: s = \"103301\"\nOutput: 2\nExplanation: \nThere are 6 possible subsequences of length 5: \"10330\",\"10331\",\"10301\",\"10301\",\"13301\",\"03301\". \nTwo of them (both equal to \"10301\") are palindromic.\n\nExample 2:\n\nInput: s = \"0000000\"\nOutput: 21\nExplanation: All 21 subsequences are \"00000\", which is palindromic.\n\nExample 3:\n\nInput: s = \"9999900000\"\nOutput: 2\nExplanation: The only two palindromic subsequences are \"99999\" and \"00000\".\n\nConstraints:\n\n* `1 <= s.length <= 104`\n* `s` consists of digits."} {"task_id": "count-subarrays-with-median-k", "prompt": "def countSubarrays(nums: List[int], k: int) -> int:\n \"\"\"\n You are given an array `nums` of size `n` consisting of distinct integers from\n `1` to `n` and a positive integer `k`.\n \n Return the number of non-empty subarrays in `nums` that have a median equal to\n `k`.\n \n Note:\n \n * The median of an array is the middle element after sorting the array in ascending order. If the array is of even length, the median is the left middle element. \n * For example, the median of `[2,3,1,4]` is `2`, and the median of `[8,4,3,5,1]` is `4`.\n * A subarray is a contiguous part of an array.\n \n Example 1:\n \n Input: nums = [3,2,1,4,5], k = 4\n Output: 3\n Explanation: The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].\n \n Example 2:\n \n Input: nums = [2,3,1], k = 3\n Output: 1\n Explanation: [3] is the only subarray that has a median equal to 3.\n \n Constraints:\n \n * `n == nums.length`\n * `1 <= n <= 105`\n * `1 <= nums[i], k <= n`\n * The integers in `nums` are distinct.\n \"\"\"\n", "canonical_solution": "", "test": "assert countSubarrays([3,2,1,4,5], 4) == 3\nassert countSubarrays([2,3,1], 3) == 1", "signature": "countSubarrays(nums: List[int], k: int) -> int:", "docstring": "You are given an array `nums` of size `n` consisting of distinct integers from\n`1` to `n` and a positive integer `k`.\n\nReturn the number of non-empty subarrays in `nums` that have a median equal to\n`k`.\n\nNote:\n\n* The median of an array is the middle element after sorting the array in ascending order. If the array is of even length, the median is the left middle element. \n* For example, the median of `[2,3,1,4]` is `2`, and the median of `[8,4,3,5,1]` is `4`.\n* A subarray is a contiguous part of an array.\n\nExample 1:\n\nInput: nums = [3,2,1,4,5], k = 4\nOutput: 3\nExplanation: The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].\n\nExample 2:\n\nInput: nums = [2,3,1], k = 3\nOutput: 1\nExplanation: [3] is the only subarray that has a median equal to 3.\n\nConstraints:\n\n* `n == nums.length`\n* `1 <= n <= 105`\n* `1 <= nums[i], k <= n`\n* The integers in `nums` are distinct."} {"task_id": "number-of-beautiful-partitions", "prompt": "def beautifulPartitions(s: str, k: int, minLength: int) -> int:\n \"\"\"\n You are given a string `s` that consists of the digits `'1'` to `'9'` and two\n integers `k` and `minLength`.\n \n A partition of `s` is called beautiful if:\n \n * `s` is partitioned into `k` non-intersecting substrings.\n * Each substring has a length of at least `minLength`.\n * Each substring starts with a prime digit and ends with a non-prime digit. Prime digits are `'2'`, `'3'`, `'5'`, and `'7'`, and the rest of the digits are non-prime.\n \n Return the number of beautiful partitions of `s`. Since the answer may be very\n large, return it modulo `109 + 7`.\n \n A substring is a contiguous sequence of characters within a string.\n \n Example 1:\n \n Input: s = \"23542185131\", k = 3, minLength = 2\n Output: 3\n Explanation: There exists three ways to create a beautiful partition:\n \"2354 | 218 | 5131\"\n \"2354 | 21851 | 31\"\n \"2354218 | 51 | 31\"\n \n Example 2:\n \n Input: s = \"23542185131\", k = 3, minLength = 3\n Output: 1\n Explanation: There exists one way to create a beautiful partition: \"2354 | 218 | 5131\".\n \n Example 3:\n \n Input: s = \"3312958\", k = 3, minLength = 1\n Output: 1\n Explanation: There exists one way to create a beautiful partition: \"331 | 29 | 58\".\n \n Constraints:\n \n * `1 <= k, minLength <= s.length <= 1000`\n * `s` consists of the digits `'1'` to `'9'`.\n \"\"\"\n", "canonical_solution": "", "test": "assert beautifulPartitions(\"23542185131\", 3, 2) == 3\nassert beautifulPartitions(\"23542185131\", 3, 3) == 1\nassert beautifulPartitions(\"3312958\", 3, 1) == 1", "signature": "beautifulPartitions(s: str, k: int, minLength: int) -> int:", "docstring": "You are given a string `s` that consists of the digits `'1'` to `'9'` and two\nintegers `k` and `minLength`.\n\nA partition of `s` is called beautiful if:\n\n* `s` is partitioned into `k` non-intersecting substrings.\n* Each substring has a length of at least `minLength`.\n* Each substring starts with a prime digit and ends with a non-prime digit. Prime digits are `'2'`, `'3'`, `'5'`, and `'7'`, and the rest of the digits are non-prime.\n\nReturn the number of beautiful partitions of `s`. Since the answer may be very\nlarge, return it modulo `109 + 7`.\n\nA substring is a contiguous sequence of characters within a string.\n\nExample 1:\n\nInput: s = \"23542185131\", k = 3, minLength = 2\nOutput: 3\nExplanation: There exists three ways to create a beautiful partition:\n\"2354 | 218 | 5131\"\n\"2354 | 21851 | 31\"\n\"2354218 | 51 | 31\"\n\nExample 2:\n\nInput: s = \"23542185131\", k = 3, minLength = 3\nOutput: 1\nExplanation: There exists one way to create a beautiful partition: \"2354 | 218 | 5131\".\n\nExample 3:\n\nInput: s = \"3312958\", k = 3, minLength = 1\nOutput: 1\nExplanation: There exists one way to create a beautiful partition: \"331 | 29 | 58\".\n\nConstraints:\n\n* `1 <= k, minLength <= s.length <= 1000`\n* `s` consists of the digits `'1'` to `'9'`."} {"task_id": "split-message-based-on-limit", "prompt": "def splitMessage(message: str, limit: int) -> List[str]:\n \"\"\"\n You are given a string, `message`, and a positive integer, `limit`.\n \n You must split `message` into one or more parts based on `limit`. Each\n resulting part should have the suffix `\"\"`, where `\"b\"` is to be replaced\n with the total number of parts and `\"a\"` is to be replaced with the index of\n the part, starting from `1` and going up to `b`. Additionally, the length of\n each resulting part (including its suffix) should be equal to `limit`, except\n for the last part whose length can be at most `limit`.\n \n The resulting parts should be formed such that when their suffixes are removed\n and they are all concatenated in order, they should be equal to `message`.\n Also, the result should contain as few parts as possible.\n \n Return the parts `message` would be split into as an array of strings. If it\n is impossible to split `message` as required, return an empty array.\n \n Example 1:\n \n Input: message = \"this is really a very awesome message\", limit = 9\n Output: [\"thi<1/14>\",\"s i<2/14>\",\"s r<3/14>\",\"eal<4/14>\",\"ly <5/14>\",\"a v<6/14>\",\"ery<7/14>\",\" aw<8/14>\",\"eso<9/14>\",\"me<10/14>\",\" m<11/14>\",\"es<12/14>\",\"sa<13/14>\",\"ge<14/14>\"]\n Explanation:\n The first 9 parts take 3 characters each from the beginning of message.\n The next 5 parts take 2 characters each to finish splitting message. \n In this example, each part, including the last, has length 9. \n It can be shown it is not possible to split message into less than 14 parts.\n \n Example 2:\n \n Input: message = \"short message\", limit = 15\n Output: [\"short mess<1/2>\",\"age<2/2>\"]\n Explanation:\n Under the given constraints, the string can be split into two parts: \n - The first part comprises of the first 10 characters, and has a length 15.\n - The next part comprises of the last 3 characters, and has a length 8.\n \n Constraints:\n \n * `1 <= message.length <= 104`\n * `message` consists only of lowercase English letters and `' '`.\n * `1 <= limit <= 104`\n \"\"\"\n", "canonical_solution": "", "test": "assert splitMessage(\"this is really a very awesome message\", 9) == [\"thi<1/14>\",\"s i<2/14>\",\"s r<3/14>\",\"eal<4/14>\",\"ly <5/14>\",\"a v<6/14>\",\"ery<7/14>\",\" aw<8/14>\",\"eso<9/14>\",\"me<10/14>\",\" m<11/14>\",\"es<12/14>\",\"sa<13/14>\",\"ge<14/14>\"]\nassert splitMessage(\"short message\", 15) == [\"short mess<1/2>\",\"age<2/2>\"]", "signature": "splitMessage(message: str, limit: int) -> List[str]:", "docstring": "You are given a string, `message`, and a positive integer, `limit`.\n\nYou must split `message` into one or more parts based on `limit`. Each\nresulting part should have the suffix `\"\"`, where `\"b\"` is to be replaced\nwith the total number of parts and `\"a\"` is to be replaced with the index of\nthe part, starting from `1` and going up to `b`. Additionally, the length of\neach resulting part (including its suffix) should be equal to `limit`, except\nfor the last part whose length can be at most `limit`.\n\nThe resulting parts should be formed such that when their suffixes are removed\nand they are all concatenated in order, they should be equal to `message`.\nAlso, the result should contain as few parts as possible.\n\nReturn the parts `message` would be split into as an array of strings. If it\nis impossible to split `message` as required, return an empty array.\n\nExample 1:\n\nInput: message = \"this is really a very awesome message\", limit = 9\nOutput: [\"thi<1/14>\",\"s i<2/14>\",\"s r<3/14>\",\"eal<4/14>\",\"ly <5/14>\",\"a v<6/14>\",\"ery<7/14>\",\" aw<8/14>\",\"eso<9/14>\",\"me<10/14>\",\" m<11/14>\",\"es<12/14>\",\"sa<13/14>\",\"ge<14/14>\"]\nExplanation:\nThe first 9 parts take 3 characters each from the beginning of message.\nThe next 5 parts take 2 characters each to finish splitting message. \nIn this example, each part, including the last, has length 9. \nIt can be shown it is not possible to split message into less than 14 parts.\n\nExample 2:\n\nInput: message = \"short message\", limit = 15\nOutput: [\"short mess<1/2>\",\"age<2/2>\"]\nExplanation:\nUnder the given constraints, the string can be split into two parts: \n- The first part comprises of the first 10 characters, and has a length 15.\n- The next part comprises of the last 3 characters, and has a length 8.\n\nConstraints:\n\n* `1 <= message.length <= 104`\n* `message` consists only of lowercase English letters and `' '`.\n* `1 <= limit <= 104`"} {"task_id": "maximum-number-of-non-overlapping-palindrome-substrings", "prompt": "def maxPalindromes(s: str, k: int) -> int:\n \"\"\"\n You are given a string `s` and a positive integer `k`.\n \n Select a set of non-overlapping substrings from the string `s` that satisfy\n the following conditions:\n \n * The length of each substring is at least `k`.\n * Each substring is a palindrome.\n \n Return the maximum number of substrings in an optimal selection.\n \n A substring is a contiguous sequence of characters within a string.\n \n Example 1:\n \n Input: s = \"abaccdbbd\", k = 3\n Output: 2\n Explanation: We can select the substrings underlined in s = \"abaccdbbd\". Both \"aba\" and \"dbbd\" are palindromes and have a length of at least k = 3.\n It can be shown that we cannot find a selection with more than two valid substrings.\n \n Example 2:\n \n Input: s = \"adbcda\", k = 2\n Output: 0\n Explanation: There is no palindrome substring of length at least 2 in the string.\n \n Constraints:\n \n * `1 <= k <= s.length <= 2000`\n * `s` consists of lowercase English letters.\n \"\"\"\n", "canonical_solution": "", "test": "assert maxPalindromes(\"abaccdbbd\", 3) == 2\nassert maxPalindromes(\"adbcda\", 2) == 0", "signature": "maxPalindromes(s: str, k: int) -> int:", "docstring": "You are given a string `s` and a positive integer `k`.\n\nSelect a set of non-overlapping substrings from the string `s` that satisfy\nthe following conditions:\n\n* The length of each substring is at least `k`.\n* Each substring is a palindrome.\n\nReturn the maximum number of substrings in an optimal selection.\n\nA substring is a contiguous sequence of characters within a string.\n\nExample 1:\n\nInput: s = \"abaccdbbd\", k = 3\nOutput: 2\nExplanation: We can select the substrings underlined in s = \"abaccdbbd\". Both \"aba\" and \"dbbd\" are palindromes and have a length of at least k = 3.\nIt can be shown that we cannot find a selection with more than two valid substrings.\n\nExample 2:\n\nInput: s = \"adbcda\", k = 2\nOutput: 0\nExplanation: There is no palindrome substring of length at least 2 in the string.\n\nConstraints:\n\n* `1 <= k <= s.length <= 2000`\n* `s` consists of lowercase English letters."} {"task_id": "minimum-total-distance-traveled", "prompt": "def minimumTotalDistance(robot: List[int], factory: List[List[int]]) -> int:\n \"\"\"\n There are some robots and factories on the X-axis. You are given an integer\n array `robot` where `robot[i]` is the position of the `ith` robot. You are\n also given a 2D integer array `factory` where `factory[j] = [positionj,\n limitj]` indicates that `positionj` is the position of the `jth` factory and\n that the `jth` factory can repair at most `limitj` robots.\n \n The positions of each robot are unique. The positions of each factory are also\n unique. Note that a robot can be in the same position as a factory initially.\n \n All the robots are initially broken; they keep moving in one direction. The\n direction could be the negative or the positive direction of the X-axis. When\n a robot reaches a factory that did not reach its limit, the factory repairs\n the robot, and it stops moving.\n \n At any moment, you can set the initial direction of moving for some robot.\n Your target is to minimize the total distance traveled by all the robots.\n \n Return the minimum total distance traveled by all the robots. The test cases\n are generated such that all the robots can be repaired.\n \n Note that\n \n * All robots move at the same speed.\n * If two robots move in the same direction, they will never collide.\n * If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.\n * If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.\n * If the robot moved from a position `x` to a position `y`, the distance it moved is `|y - x|`.\n \n Example 1:\n \n Input: robot = [0,4,6], factory = [[2,2],[6,2]]\n Output: 4\n Explanation: As shown in the figure:\n - The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.\n - The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.\n - The third robot at position 6 will be repaired at the second factory. It does not need to move.\n The limit of the first factory is 2, and it fixed 2 robots.\n The limit of the second factory is 2, and it fixed 1 robot.\n The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.\n \n Example 2:\n \n Input: robot = [1,-1], factory = [[-2,1],[2,1]]\n Output: 2\n Explanation: As shown in the figure:\n - The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.\n - The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.\n The limit of the first factory is 1, and it fixed 1 robot.\n The limit of the second factory is 1, and it fixed 1 robot.\n The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.\n \n Constraints:\n \n * `1 <= robot.length, factory.length <= 100`\n * `factory[j].length == 2`\n * `-109 <= robot[i], positionj <= 109`\n * `0 <= limitj <= robot.length`\n * The input will be generated such that it is always possible to repair every robot.\n \"\"\"\n", "canonical_solution": "", "test": "assert minimumTotalDistance([0,4,6], [[2,2],[6,2]]) == 4\nassert minimumTotalDistance([1,-1], [[-2,1],[2,1]]) == 2", "signature": "minimumTotalDistance(robot: List[int], factory: List[List[int]]) -> int:", "docstring": "There are some robots and factories on the X-axis. You are given an integer\narray `robot` where `robot[i]` is the position of the `ith` robot. You are\nalso given a 2D integer array `factory` where `factory[j] = [positionj,\nlimitj]` indicates that `positionj` is the position of the `jth` factory and\nthat the `jth` factory can repair at most `limitj` robots.\n\nThe positions of each robot are unique. The positions of each factory are also\nunique. Note that a robot can be in the same position as a factory initially.\n\nAll the robots are initially broken; they keep moving in one direction. The\ndirection could be the negative or the positive direction of the X-axis. When\na robot reaches a factory that did not reach its limit, the factory repairs\nthe robot, and it stops moving.\n\nAt any moment, you can set the initial direction of moving for some robot.\nYour target is to minimize the total distance traveled by all the robots.\n\nReturn the minimum total distance traveled by all the robots. The test cases\nare generated such that all the robots can be repaired.\n\nNote that\n\n* All robots move at the same speed.\n* If two robots move in the same direction, they will never collide.\n* If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.\n* If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.\n* If the robot moved from a position `x` to a position `y`, the distance it moved is `|y - x|`.\n\nExample 1:\n\nInput: robot = [0,4,6], factory = [[2,2],[6,2]]\nOutput: 4\nExplanation: As shown in the figure:\n- The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.\n- The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.\n- The third robot at position 6 will be repaired at the second factory. It does not need to move.\nThe limit of the first factory is 2, and it fixed 2 robots.\nThe limit of the second factory is 2, and it fixed 1 robot.\nThe total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.\n\nExample 2:\n\nInput: robot = [1,-1], factory = [[-2,1],[2,1]]\nOutput: 2\nExplanation: As shown in the figure:\n- The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.\n- The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.\nThe limit of the first factory is 1, and it fixed 1 robot.\nThe limit of the second factory is 1, and it fixed 1 robot.\nThe total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.\n\nConstraints:\n\n* `1 <= robot.length, factory.length <= 100`\n* `factory[j].length == 2`\n* `-109 <= robot[i], positionj <= 109`\n* `0 <= limitj <= robot.length`\n* The input will be generated such that it is always possible to repair every robot."} {"task_id": "next-greater-element-iv", "prompt": "def secondGreaterElement(nums: List[int]) -> List[int]:\n \"\"\"\n You are given a 0-indexed array of non-negative integers `nums`. For each\n integer in `nums`, you must find its respective second greater integer.\n \n The second greater integer of `nums[i]` is `nums[j]` such that:\n \n * `j > i`\n * `nums[j] > nums[i]`\n * There exists exactly one index `k` such that `nums[k] > nums[i]` and `i < k < j`.\n \n If there is no such `nums[j]`, the second greater integer is considered to be\n `-1`.\n \n * For example, in the array `[1, 2, 4, 3]`, the second greater integer of `1` is `4`, `2` is `3`, and that of `3` and `4` is `-1`.\n \n Return an integer array `answer`, where `answer[i]` is the second greater\n integer of `nums[i]`.\n \n Example 1:\n \n Input: nums = [2,4,0,9,6]\n Output: [9,6,6,-1,-1]\n Explanation:\n 0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.\n 1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.\n 2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.\n 3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.\n 4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.\n Thus, we return [9,6,6,-1,-1].\n \n Example 2:\n \n Input: nums = [3,3]\n Output: [-1,-1]\n Explanation:\n We return [-1,-1] since neither integer has any integer greater than it.\n \n Constraints:\n \n * `1 <= nums.length <= 105`\n * `0 <= nums[i] <= 109`\n \"\"\"\n", "canonical_solution": "", "test": "assert secondGreaterElement([2,4,0,9,6]) == [9,6,6,-1,-1]\nassert secondGreaterElement([3,3]) == [-1,-1]", "signature": "secondGreaterElement(nums: List[int]) -> List[int]:", "docstring": "You are given a 0-indexed array of non-negative integers `nums`. For each\ninteger in `nums`, you must find its respective second greater integer.\n\nThe second greater integer of `nums[i]` is `nums[j]` such that:\n\n* `j > i`\n* `nums[j] > nums[i]`\n* There exists exactly one index `k` such that `nums[k] > nums[i]` and `i < k < j`.\n\nIf there is no such `nums[j]`, the second greater integer is considered to be\n`-1`.\n\n* For example, in the array `[1, 2, 4, 3]`, the second greater integer of `1` is `4`, `2` is `3`, and that of `3` and `4` is `-1`.\n\nReturn an integer array `answer`, where `answer[i]` is the second greater\ninteger of `nums[i]`.\n\nExample 1:\n\nInput: nums = [2,4,0,9,6]\nOutput: [9,6,6,-1,-1]\nExplanation:\n0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.\n1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.\n2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.\n3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.\n4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.\nThus, we return [9,6,6,-1,-1].\n\nExample 2:\n\nInput: nums = [3,3]\nOutput: [-1,-1]\nExplanation:\nWe return [-1,-1] since neither integer has any integer greater than it.\n\nConstraints:\n\n* `1 <= nums.length <= 105`\n* `0 <= nums[i] <= 109`"} {"task_id": "minimum-number-of-operations-to-make-arrays-similar", "prompt": "def makeSimilar(nums: List[int], target: List[int]) -> int:\n \"\"\"\n You are given two positive integer arrays `nums` and `target`, of the same\n length.\n \n In one operation, you can choose any two distinct indices `i` and `j` where `0\n <= i, j < nums.length` and:\n \n * set `nums[i] = nums[i] + 2` and\n * set `nums[j] = nums[j] - 2`.\n \n Two arrays are considered to be similar if the frequency of each element is\n the same.\n \n Return the minimum number of operations required to make `nums` similar to\n `target`. The test cases are generated such that `nums` can always be similar\n to `target`.\n \n Example 1:\n \n Input: nums = [8,12,6], target = [2,14,10]\n Output: 2\n Explanation: It is possible to make nums similar to target in two operations:\n - Choose i = 0 and j = 2, nums = [10,12,4].\n - Choose i = 1 and j = 2, nums = [10,14,2].\n It can be shown that 2 is the minimum number of operations needed.\n \n Example 2:\n \n Input: nums = [1,2,5], target = [4,1,3]\n Output: 1\n Explanation: We can make nums similar to target in one operation:\n - Choose i = 1 and j = 2, nums = [1,4,3].\n \n Example 3:\n \n Input: nums = [1,1,1,1,1], target = [1,1,1,1,1]\n Output: 0\n Explanation: The array nums is already similiar to target.\n \n Constraints:\n \n * `n == nums.length == target.length`\n * `1 <= n <= 105`\n * `1 <= nums[i], target[i] <= 106`\n * It is possible to make `nums` similar to `target`.\n \"\"\"\n", "canonical_solution": "", "test": "assert makeSimilar([8,12,6], [2,14,10]) == 2\nassert makeSimilar([1,2,5], [4,1,3]) == 1\nassert makeSimilar([1,1,1,1,1], [1,1,1,1,1]) == 0", "signature": "makeSimilar(nums: List[int], target: List[int]) -> int:", "docstring": "You are given two positive integer arrays `nums` and `target`, of the same\nlength.\n\nIn one operation, you can choose any two distinct indices `i` and `j` where `0\n<= i, j < nums.length` and:\n\n* set `nums[i] = nums[i] + 2` and\n* set `nums[j] = nums[j] - 2`.\n\nTwo arrays are considered to be similar if the frequency of each element is\nthe same.\n\nReturn the minimum number of operations required to make `nums` similar to\n`target`. The test cases are generated such that `nums` can always be similar\nto `target`.\n\nExample 1:\n\nInput: nums = [8,12,6], target = [2,14,10]\nOutput: 2\nExplanation: It is possible to make nums similar to target in two operations:\n- Choose i = 0 and j = 2, nums = [10,12,4].\n- Choose i = 1 and j = 2, nums = [10,14,2].\nIt can be shown that 2 is the minimum number of operations needed.\n\nExample 2:\n\nInput: nums = [1,2,5], target = [4,1,3]\nOutput: 1\nExplanation: We can make nums similar to target in one operation:\n- Choose i = 1 and j = 2, nums = [1,4,3].\n\nExample 3:\n\nInput: nums = [1,1,1,1,1], target = [1,1,1,1,1]\nOutput: 0\nExplanation: The array nums is already similiar to target.\n\nConstraints:\n\n* `n == nums.length == target.length`\n* `1 <= n <= 105`\n* `1 <= nums[i], target[i] <= 106`\n* It is possible to make `nums` similar to `target`."} {"task_id": "minimum-cost-to-make-array-equal", "prompt": "def minCost(nums: List[int], cost: List[int]) -> int:\n \"\"\"\n You are given two 0-indexed arrays `nums` and `cost` consisting each of `n`\n positive integers.\n \n You can do the following operation any number of times:\n \n * Increase or decrease any element of the array `nums` by `1`.\n \n The cost of doing one operation on the `ith` element is `cost[i]`.\n \n Return the minimum total cost such that all the elements of the array `nums`\n become equal.\n \n Example 1:\n \n Input: nums = [1,3,5,2], cost = [2,3,1,14]\n Output: 8\n Explanation: We can make all the elements equal to 2 in the following way:\n - Increase the 0th element one time. The cost is 2.\n - Decrease the 1st element one time. The cost is 3.\n - Decrease the 2nd element three times. The cost is 1 + 1 + 1 = 3.\n The total cost is 2 + 3 + 3 = 8.\n It can be shown that we cannot make the array equal with a smaller cost.\n \n Example 2:\n \n Input: nums = [2,2,2,2,2], cost = [4,2,8,1,3]\n Output: 0\n Explanation: All the elements are already equal, so no operations are needed.\n \n Constraints:\n \n * `n == nums.length == cost.length`\n * `1 <= n <= 105`\n * `1 <= nums[i], cost[i] <= 106`\n * Test cases are generated in a way that the output doesn't exceed 253-1\n \"\"\"\n", "canonical_solution": "", "test": "assert minCost([1,3,5,2], [2,3,1,14]) == 8\nassert minCost([2,2,2,2,2], [4,2,8,1,3]) == 0", "signature": "minCost(nums: List[int], cost: List[int]) -> int:", "docstring": "You are given two 0-indexed arrays `nums` and `cost` consisting each of `n`\npositive integers.\n\nYou can do the following operation any number of times:\n\n* Increase or decrease any element of the array `nums` by `1`.\n\nThe cost of doing one operation on the `ith` element is `cost[i]`.\n\nReturn the minimum total cost such that all the elements of the array `nums`\nbecome equal.\n\nExample 1:\n\nInput: nums = [1,3,5,2], cost = [2,3,1,14]\nOutput: 8\nExplanation: We can make all the elements equal to 2 in the following way:\n- Increase the 0th element one time. The cost is 2.\n- Decrease the 1st element one time. The cost is 3.\n- Decrease the 2nd element three times. The cost is 1 + 1 + 1 = 3.\nThe total cost is 2 + 3 + 3 = 8.\nIt can be shown that we cannot make the array equal with a smaller cost.\n\nExample 2:\n\nInput: nums = [2,2,2,2,2], cost = [4,2,8,1,3]\nOutput: 0\nExplanation: All the elements are already equal, so no operations are needed.\n\nConstraints:\n\n* `n == nums.length == cost.length`\n* `1 <= n <= 105`\n* `1 <= nums[i], cost[i] <= 106`\n* Test cases are generated in a way that the output doesn't exceed 253-1"} {"task_id": "create-components-with-same-value", "prompt": "def componentValue(nums: List[int], edges: List[List[int]]) -> int:\n \"\"\"\n There is an undirected tree with `n` nodes labeled from `0` to `n - 1`.\n \n You are given a 0-indexed integer array `nums` of length `n` where `nums[i]`\n represents the value of the `ith` node. You are also given a 2D integer array\n `edges` of length `n - 1` where `edges[i] = [ai, bi]` indicates that there is\n an edge between nodes `ai` and `bi` in the tree.\n \n You are allowed to delete some edges, splitting the tree into multiple\n connected components. Let the value of a component be the sum of all `nums[i]`\n for which node `i` is in the component.\n \n Return the maximum number of edges you can delete, such that every connected\n component in the tree has the same value.\n \n Example 1:\n \n Input: nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]] \n Output: 2 \n 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.\n \n Example 2:\n \n Input: nums = [2], edges = []\n Output: 0\n Explanation: There are no edges to be deleted.\n \n Constraints:\n \n * `1 <= n <= 2 * 104`\n * `nums.length == n`\n * `1 <= nums[i] <= 50`\n * `edges.length == n - 1`\n * `edges[i].length == 2`\n * `0 <= edges[i][0], edges[i][1] <= n - 1`\n * `edges` represents a valid tree.\n \"\"\"\n", "canonical_solution": "", "test": "assert componentValue([6,2,2,2,6], [[0,1],[1,2],[1,3],[3,4]]) == 2\nassert componentValue([2], []) == 0", "signature": "componentValue(nums: List[int], edges: List[List[int]]) -> int:", "docstring": "There is an undirected tree with `n` nodes labeled from `0` to `n - 1`.\n\nYou are given a 0-indexed integer array `nums` of length `n` where `nums[i]`\nrepresents the value of the `ith` node. You are also given a 2D integer array\n`edges` of length `n - 1` where `edges[i] = [ai, bi]` indicates that there is\nan edge between nodes `ai` and `bi` in the tree.\n\nYou are allowed to delete some edges, splitting the tree into multiple\nconnected components. Let the value of a component be the sum of all `nums[i]`\nfor which node `i` is in the component.\n\nReturn the maximum number of edges you can delete, such that every connected\ncomponent in the tree has the same value.\n\nExample 1:\n\nInput: nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]] \nOutput: 2 \nExplanation: 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.\n\nExample 2:\n\nInput: nums = [2], edges = []\nOutput: 0\nExplanation: There are no edges to be deleted.\n\nConstraints:\n\n* `1 <= n <= 2 * 104`\n* `nums.length == n`\n* `1 <= nums[i] <= 50`\n* `edges.length == n - 1`\n* `edges[i].length == 2`\n* `0 <= edges[i][0], edges[i][1] <= n - 1`\n* `edges` represents a valid tree."} {"task_id": "count-subarrays-with-fixed-bounds", "prompt": "def countSubarrays(nums: List[int], minK: int, maxK: int) -> int:\n \"\"\"\n You are given an integer array `nums` and two integers `minK` and `maxK`.\n \n A fixed-bound subarray of `nums` is a subarray that satisfies the following\n conditions:\n \n * The minimum value in the subarray is equal to `minK`.\n * The maximum value in the subarray is equal to `maxK`.\n \n Return the number of fixed-bound subarrays.\n \n A subarray is a contiguous part of an array.\n \n Example 1:\n \n Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5\n Output: 2\n Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].\n \n Example 2:\n \n Input: nums = [1,1,1,1], minK = 1, maxK = 1\n Output: 10\n Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.\n \n Constraints:\n \n * `2 <= nums.length <= 105`\n * `1 <= nums[i], minK, maxK <= 106`\n \"\"\"\n", "canonical_solution": "", "test": "assert countSubarrays([1,3,5,2,7,5], 1, 5) == 2\nassert countSubarrays([1,1,1,1], 1, 1) == 10", "signature": "countSubarrays(nums: List[int], minK: int, maxK: int) -> int:", "docstring": "You are given an integer array `nums` and two integers `minK` and `maxK`.\n\nA fixed-bound subarray of `nums` is a subarray that satisfies the following\nconditions:\n\n* The minimum value in the subarray is equal to `minK`.\n* The maximum value in the subarray is equal to `maxK`.\n\nReturn the number of fixed-bound subarrays.\n\nA subarray is a contiguous part of an array.\n\nExample 1:\n\nInput: nums = [1,3,5,2,7,5], minK = 1, maxK = 5\nOutput: 2\nExplanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].\n\nExample 2:\n\nInput: nums = [1,1,1,1], minK = 1, maxK = 1\nOutput: 10\nExplanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.\n\nConstraints:\n\n* `2 <= nums.length <= 105`\n* `1 <= nums[i], minK, maxK <= 106`"} {"task_id": "longest-increasing-subsequence-ii", "prompt": "def lengthOfLIS(nums: List[int], k: int) -> int:\n \"\"\"\n You are given an integer array `nums` and an integer `k`.\n \n Find the longest subsequence of `nums` that meets the following requirements:\n \n * The subsequence is strictly increasing and\n * The difference between adjacent elements in the subsequence is at most `k`.\n \n Return the length of the longest subsequence that meets the requirements.\n \n A subsequence is an array that can be derived from another array by deleting\n some or no elements without changing the order of the remaining elements.\n \n Example 1:\n \n Input: nums = [4,2,1,4,3,4,5,8,15], k = 3\n Output: 5\n Explanation:\n The longest subsequence that meets the requirements is [1,3,4,5,8].\n The subsequence has a length of 5, so we return 5.\n Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.\n \n Example 2:\n \n Input: nums = [7,4,5,1,8,12,4,7], k = 5\n Output: 4\n Explanation:\n The longest subsequence that meets the requirements is [4,5,8,12].\n The subsequence has a length of 4, so we return 4.\n \n Example 3:\n \n Input: nums = [1,5], k = 1\n Output: 1\n Explanation:\n The longest subsequence that meets the requirements is [1].\n The subsequence has a length of 1, so we return 1.\n \n Constraints:\n \n * `1 <= nums.length <= 105`\n * `1 <= nums[i], k <= 105`\n \"\"\"\n", "canonical_solution": "", "test": "assert lengthOfLIS([4,2,1,4,3,4,5,8,15], 3) == 5\nassert lengthOfLIS([7,4,5,1,8,12,4,7], 5) == 4\nassert lengthOfLIS([1,5], 1) == 1", "signature": "lengthOfLIS(nums: List[int], k: int) -> int:", "docstring": "You are given an integer array `nums` and an integer `k`.\n\nFind the longest subsequence of `nums` that meets the following requirements:\n\n* The subsequence is strictly increasing and\n* The difference between adjacent elements in the subsequence is at most `k`.\n\nReturn the length of the longest subsequence that meets the requirements.\n\nA subsequence is an array that can be derived from another array by deleting\nsome or no elements without changing the order of the remaining elements.\n\nExample 1:\n\nInput: nums = [4,2,1,4,3,4,5,8,15], k = 3\nOutput: 5\nExplanation:\nThe longest subsequence that meets the requirements is [1,3,4,5,8].\nThe subsequence has a length of 5, so we return 5.\nNote that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.\n\nExample 2:\n\nInput: nums = [7,4,5,1,8,12,4,7], k = 5\nOutput: 4\nExplanation:\nThe longest subsequence that meets the requirements is [4,5,8,12].\nThe subsequence has a length of 4, so we return 4.\n\nExample 3:\n\nInput: nums = [1,5], k = 1\nOutput: 1\nExplanation:\nThe longest subsequence that meets the requirements is [1].\nThe subsequence has a length of 1, so we return 1.\n\nConstraints:\n\n* `1 <= nums.length <= 105`\n* `1 <= nums[i], k <= 105`"} {"task_id": "paths-in-matrix-whose-sum-is-divisible-by-k", "prompt": "def numberOfPaths(grid: List[List[int]], k: int) -> int:\n \"\"\"\n You are given a 0-indexed `m x n` integer matrix `grid` and an integer `k`.\n You are currently at position `(0, 0)` and you want to reach position `(m - 1,\n n - 1)` moving only down or right.\n \n Return the number of paths where the sum of the elements on the path is\n divisible by `k`. Since the answer may be very large, return it modulo `109 +\n 7`.\n \n Example 1:\n \n Input: grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3\n Output: 2\n Explanation: There are two paths where the sum of the elements on the path is divisible by k.\n The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3.\n The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.\n \n Example 2:\n \n Input: grid = [[0,0]], k = 5\n Output: 1\n Explanation: The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.\n \n Example 3:\n \n Input: grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1\n Output: 10\n Explanation: Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.\n \n Constraints:\n \n * `m == grid.length`\n * `n == grid[i].length`\n * `1 <= m, n <= 5 * 104`\n * `1 <= m * n <= 5 * 104`\n * `0 <= grid[i][j] <= 100`\n * `1 <= k <= 50`\n \"\"\"\n", "canonical_solution": "", "test": "assert numberOfPaths([[5,2,4],[3,0,5],[0,7,2]], 3) == 2\nassert numberOfPaths([[0,0]], 5) == 1\nassert numberOfPaths([[7,3,4,9],[2,3,6,2],[2,3,7,0]], 1) == 10", "signature": "numberOfPaths(grid: List[List[int]], k: int) -> int:", "docstring": "You are given a 0-indexed `m x n` integer matrix `grid` and an integer `k`.\nYou are currently at position `(0, 0)` and you want to reach position `(m - 1,\nn - 1)` moving only down or right.\n\nReturn the number of paths where the sum of the elements on the path is\ndivisible by `k`. Since the answer may be very large, return it modulo `109 +\n7`.\n\nExample 1:\n\nInput: grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3\nOutput: 2\nExplanation: There are two paths where the sum of the elements on the path is divisible by k.\nThe first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3.\nThe second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.\n\nExample 2:\n\nInput: grid = [[0,0]], k = 5\nOutput: 1\nExplanation: The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.\n\nExample 3:\n\nInput: grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1\nOutput: 10\nExplanation: Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.\n\nConstraints:\n\n* `m == grid.length`\n* `n == grid[i].length`\n* `1 <= m, n <= 5 * 104`\n* `1 <= m * n <= 5 * 104`\n* `0 <= grid[i][j] <= 100`\n* `1 <= k <= 50`"}