mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-19 03:56:46 +08:00
update
This commit is contained in:
File diff suppressed because it is too large
Load Diff
85
leetcode-cn/originData/[no content]beautiful-pairs.json
Normal file
85
leetcode-cn/originData/[no content]beautiful-pairs.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
62
leetcode-cn/originData/flatten-deeply-nested-array.json
Normal file
62
leetcode-cn/originData/flatten-deeply-nested-array.json
Normal file
@@ -0,0 +1,62 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2759",
|
||||
"questionFrontendId": "2625",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2222276,
|
||||
"title": "Flatten Deeply Nested Array",
|
||||
"titleSlug": "flatten-deeply-nested-array",
|
||||
"content": "<p>Write a function that accepts a <strong>multi-dimensional</strong> array <code>arr</code> and a depth <code>n</code>, and returns a <strong>flattened</strong> version of that array.</p>\r\n\r\n<p>A <strong>multi-dimensional</strong> array is a recursive data structure that contains integers or other <strong>multi-dimensional</strong> arrays.</p>\r\n\r\n<p>A <strong>flattened</strong> array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than <code>n</code>. The depth of the elements in the first array are considered to be 0.</p>\r\n\r\n<p>Please solve it without the built-in <code>Array.flat</code> method.</p>\r\n\r\n<p> </p>\r\n<p><strong class=\"example\">Example 1:</strong></p>\r\n\r\n<pre>\r\n<strong>Input</strong>\r\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\nn = 0\r\n<strong>Output</strong>\r\n[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\n\r\n<strong>Explanation</strong>\r\nPassing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. </pre>\r\n\r\n<p><strong class=\"example\">Example 2:</strong></p>\r\n\r\n<pre>\r\n<strong>Input</strong>\r\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\nn = 1\r\n<strong>Output</strong>\r\n[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]\r\n\r\n<strong>Explanation</strong>\r\nThe subarrays starting with 4, 7, and 13 are all flattened. This is because their depth of 0 is less than 1. However [9, 10, 11] remains unflattened because its depth is 1.</pre>\r\n\r\n<p><strong class=\"example\">Example 3:</strong></p>\r\n\r\n<pre>\r\n<strong>Input</strong>\r\narr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\r\nn = 2\r\n<strong>Output</strong>\r\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\r\n\r\n<strong>Explanation</strong>\r\nThe maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>\r\n\r\n<p> </p>\r\n<p><strong>Constraints:</strong></p>\r\n\r\n<ul>\r\n\t<li><code>0 <= count of numbers in arr <= 10<sup>5</sup></code></li>\r\n\t<li><code>0 <= count of subarrays in arr <= 10<sup>5</sup></code></li>\r\n\t<li><code>maxDepth <= 1000</code></li>\r\n\t<li><code>-1000 <= each number <= 1000</code></li>\r\n\t<li><code><font face=\"monospace\">0 <= n <= 1000</font></code></li>\r\n</ul>",
|
||||
"translatedTitle": "扁平化嵌套数组",
|
||||
"translatedContent": "<p>请你编写一个函数,它接收一个 <strong>多维数组 </strong><code>arr</code> 和它的深度 <code>n</code> ,并返回该数组的 <strong>扁平化 </strong>后的结果。</p>\n\n<p><strong>多维数组 </strong>是一种包含整数或其他 <strong>多维数组 </strong>的递归数据结构。</p>\n\n<p>数组 <strong>扁平化</strong> 是对数组的一种操作,定义是将原数组部分或全部子数组删除,并替换为该子数组中的实际元素。只有当嵌套的数组深度大于 <code>n</code> 时,才应该执行扁平化操作。第一层数组中元素的深度被认为是 0。</p>\n\n<p>请在没有使用内置方法 <code>Array.flat</code> 的前提下解决这个问题。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<strong>输入</strong>\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 0\n<strong>输出</strong>\n[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\n\n<strong>解释</strong>\n传递深度 n=0 的多维数组将始终得到原始数组。这是因为 子数组(0) 的最小可能的深度不小于 n=0 。因此,任何子数组都不应该被平面化。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<strong>输入</strong>\narr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 1\n<strong>输出</strong>\n[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]\n\n<strong>解释</strong>\n以 4 、7 和 13 开头的子数组都被扁平化了,这是因为它们的深度为 0 , 而 0 小于 1 。然而 [9,10,11] 其深度为 1 ,所以未被扁平化。</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<strong>输入</strong>\narr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]\nn = 2\n<strong>输出</strong>\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\n\n<strong>解释</strong>\n所有子数组的最大深度都为 1 。因此,它们都被扁平化了。</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>0 <= arr 的元素个数 <= 10<sup>5</sup></code></li>\n\t<li><code>0 <= arr 的子数组个数 <= 10<sup>5</sup></code></li>\n\t<li><code>maxDepth <= 1000</code></li>\n\t<li><code>-1000 <= each number <= 1000</code></li>\n\t<li><code><font face=\"monospace\">0 <= n <= 1000</font></code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type MultiDimensionalArray = (number | MultiDimensionalArray)[];\n\nvar flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"2\", \"totalSubmission\": \"2\", \"totalAcceptedRaw\": 2, \"totalSubmissionRaw\": 2, \"acRate\": \"100.0%\"}",
|
||||
"hints": [
|
||||
"Write a recursive function that keeps track of the current depth.",
|
||||
"if the current depth >= the maximum depth, always just push the value to the returned array. Otherwise recursively call flat on the array."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0",
|
||||
"metaData": "{\n \"classname\": \"Array\",\n \"constructor\": {\n \"params\": []\n },\n \"methods\": [],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"systemdesign\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n0\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n1\n[1,2,3,[4,5,6],[7,8,[9,10,11],12],[13,14,15]]\n2",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
189
leetcode-cn/originData/prime-in-diagonal.json
Normal file
189
leetcode-cn/originData/prime-in-diagonal.json
Normal file
File diff suppressed because one or more lines are too long
184
leetcode-cn/originData/sum-of-distances.json
Normal file
184
leetcode-cn/originData/sum-of-distances.json
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>返回位于 <code>nums</code> 至少一条 <strong>对角线</strong> 上的最大 <strong>质数</strong> 。如果任一对角线上均不存在质数,返回<em> 0 。</em></p>
|
||||
|
||||
<p>注意:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果某个整数大于 <code>1</code> ,且不存在除 <code>1</code> 和自身之外的正整数因子,则认为该整数是一个质数。</li>
|
||||
<li>如果存在整数 <code>i</code> ,使得 <code>nums[i][i] = val</code> 或者 <code>nums[i][nums.length - i - 1]= val</code> ,则认为整数 <code>val</code> 位于 <code>nums</code> 的一条对角线上。</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/06/screenshot-2023-03-06-at-45648-pm.png" style="width: 181px; height: 121px;" /></p>
|
||||
|
||||
<p>在上图中,一条对角线是 <strong>[1,5,9]</strong> ,而另一条对角线是<strong> [3,5,7]</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [[1,2,3],[5,6,7],[9,10,11]]
|
||||
<strong>输出:</strong>11
|
||||
<strong>解释:</strong>数字 1、3、6、9 和 11 是所有 "位于至少一条对角线上" 的数字。由于 11 是最大的质数,故返回 11 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [[1,2,3],[5,17,7],[9,11,10]]
|
||||
<strong>输出:</strong>17
|
||||
<strong>解释:</strong>数字 1、3、9、10 和 17 是所有满足"位于至少一条对角线上"的数字。由于 17 是最大的质数,故返回 17 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>nums.length == nums<sub>i</sub>.length</code></li>
|
||||
<li><code>1 <= nums<span style="">[i][j]</span> <= 4*10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>请你编写一个函数,它接收一个 <strong>多维数组 </strong><code>arr</code> 和它的深度 <code>n</code> ,并返回该数组的 <strong>扁平化 </strong>后的结果。</p>
|
||||
|
||||
<p><strong>多维数组 </strong>是一种包含整数或其他 <strong>多维数组 </strong>的递归数据结构。</p>
|
||||
|
||||
<p>数组 <strong>扁平化</strong> 是对数组的一种操作,定义是将原数组部分或全部子数组删除,并替换为该子数组中的实际元素。只有当嵌套的数组深度大于 <code>n</code> 时,才应该执行扁平化操作。第一层数组中元素的深度被认为是 0。</p>
|
||||
|
||||
<p>请在没有使用内置方法 <code>Array.flat</code> 的前提下解决这个问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 0
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
<strong>解释</strong>
|
||||
传递深度 n=0 的多维数组将始终得到原始数组。这是因为 子数组(0) 的最小可能的深度不小于 n=0 。因此,任何子数组都不应该被平面化。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 1
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
|
||||
|
||||
<strong>解释</strong>
|
||||
以 4 、7 和 13 开头的子数组都被扁平化了,这是因为它们的深度为 0 , 而 0 小于 1 。然而 [9,10,11] 其深度为 1 ,所以未被扁平化。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 2
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
|
||||
|
||||
<strong>解释</strong>
|
||||
所有子数组的最大深度都为 1 。因此,它们都被扁平化了。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr 的元素个数 <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= arr 的子数组个数 <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxDepth <= 1000</code></li>
|
||||
<li><code>-1000 <= each number <= 1000</code></li>
|
||||
<li><code><font face="monospace">0 <= n <= 1000</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>p</code> 。请你从 <code>nums</code> 中找到 <code>p</code> 个下标对,每个下标对对应数值取差值,你需要使得这 <code>p</code> 个差值的 <strong>最大值</strong> <strong>最小</strong>。同时,你需要确保每个下标在这 <code>p</code> 个下标对中最多出现一次。</p>
|
||||
|
||||
<p>对于一个下标对 <code>i</code> 和 <code>j</code> ,这一对的差值为 <code>|nums[i] - nums[j]|</code> ,其中 <code>|x|</code> 表示 <code>x</code> 的 <strong>绝对值</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>p</code> 个下标对对应数值 <strong>最大差值</strong> 的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [10,1,2,7,1,3], p = 2
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>第一个下标对选择 1 和 4 ,第二个下标对选择 2 和 5 。
|
||||
最大差值为 max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1 。所以我们返回 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [4,2,1,2], p = 1
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>选择下标 1 和 3 构成下标对。差值为 |2 - 2| = 0 ,这是最大差值的最小值。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= p <= (nums.length)/2</code></li>
|
||||
</ul>
|
35
leetcode-cn/problem (Chinese)/等值距离和 [sum-of-distances].html
Normal file
35
leetcode-cn/problem (Chinese)/等值距离和 [sum-of-distances].html
Normal file
@@ -0,0 +1,35 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。现有一个长度等于 <code>nums.length</code> 的数组 <code>arr</code> 。对于满足 <code>nums[j] == nums[i]</code> 且 <code>j != i</code> 的所有 <code>j</code> ,<code>arr[i]</code> 等于所有 <code>|i - j|</code> 之和。如果不存在这样的 <code>j</code> ,则令 <code>arr[i]</code> 等于 <code>0</code> 。</p>
|
||||
|
||||
<p>返回数组<em> </em><code>arr</code><em> 。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,1,1,2]
|
||||
<strong>输出:</strong>[5,0,3,4,0]
|
||||
<strong>解释:</strong>
|
||||
i = 0 ,nums[0] == nums[2] 且 nums[0] == nums[3] 。因此,arr[0] = |0 - 2| + |0 - 3| = 5 。
|
||||
i = 1 ,arr[1] = 0 因为不存在值等于 3 的其他下标。
|
||||
i = 2 ,nums[2] == nums[0] 且 nums[2] == nums[3] 。因此,arr[2] = |2 - 0| + |2 - 3| = 3 。
|
||||
i = 3 ,nums[3] == nums[0] 且 nums[3] == nums[2] 。因此,arr[3] = |3 - 0| + |3 - 2| = 4 。
|
||||
i = 4 ,arr[4] = 0 因为不存在值等于 2 的其他下标。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,5,3]
|
||||
<strong>输出:</strong>[0,0,0]
|
||||
<strong>解释:</strong>因为 nums 中的元素互不相同,对于所有 i ,都有 arr[i] = 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的 <code>m x n</code> 整数矩阵 <code>grid</code> 。你一开始的位置在 <strong>左上角</strong> 格子 <code>(0, 0)</code> 。</p>
|
||||
|
||||
<p>当你在格子 <code>(i, j)</code> 的时候,你可以移动到以下格子之一:</p>
|
||||
|
||||
<ul>
|
||||
<li>满足 <code>j < k <= grid[i][j] + j</code> 的格子 <code>(i, k)</code> (向右移动),或者</li>
|
||||
<li>满足 <code>i < k <= grid[i][j] + i</code> 的格子 <code>(k, j)</code> (向下移动)。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回到达 <strong>右下角</strong> 格子 <code>(m - 1, n - 1)</code> 需要经过的最少移动格子数,如果无法到达右下角格子,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>上图展示了到达右下角格子经过的 4 个格子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>上图展示了到达右下角格子经过的 3 个格子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[2,1,0],[1,0,0]]
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>无法到达右下角格子。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] < m * n</code></li>
|
||||
<li><code>grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a 0-indexed two-dimensional integer array <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the largest <strong>prime</strong> number that lies on at least one of the <b>diagonals</b> of </em><code>nums</code>. In case, no prime is present on any of the diagonals, return<em> 0.</em></p>
|
||||
|
||||
<p>Note that:</p>
|
||||
|
||||
<ul>
|
||||
<li>An integer is <strong>prime</strong> if it is greater than <code>1</code> and has no positive integer divisors other than <code>1</code> and itself.</li>
|
||||
<li>An integer <code>val</code> is on one of the <strong>diagonals</strong> of <code>nums</code> if there exists an integer <code>i</code> for which <code>nums[i][i] = val</code> or an <code>i</code> for which <code>nums[i][nums.length - i - 1] = val</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/06/screenshot-2023-03-06-at-45648-pm.png" style="width: 181px; height: 121px;" /></p>
|
||||
|
||||
<p>In the above diagram, one diagonal is <strong>[1,5,9]</strong> and another diagonal is<strong> [3,5,7]</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,6,7],[9,10,11]]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2,3],[5,17,7],[9,11,10]]
|
||||
<strong>Output:</strong> 17
|
||||
<strong>Explanation:</strong> The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>nums.length == nums<sub>i</sub>.length</code></li>
|
||||
<li><code>1 <= nums<span style="font-size: 10.8333px;">[i][j]</span> <= 4*10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>Write a function that accepts a <strong>multi-dimensional</strong> array <code>arr</code> and a depth <code>n</code>, and returns a <strong>flattened</strong> version of that array.</p>
|
||||
|
||||
|
||||
|
||||
<p>A <strong>multi-dimensional</strong> array is a recursive data structure that contains integers or other <strong>multi-dimensional</strong> arrays.</p>
|
||||
|
||||
|
||||
|
||||
<p>A <strong>flattened</strong> array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is greater than <code>n</code>. The depth of the elements in the first array are considered to be 0.</p>
|
||||
|
||||
|
||||
|
||||
<p>Please solve it without the built-in <code>Array.flat</code> method.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input</strong>
|
||||
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
n = 0
|
||||
|
||||
<strong>Output</strong>
|
||||
|
||||
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
|
||||
|
||||
<strong>Explanation</strong>
|
||||
|
||||
Passing a depth of n=0 will always result in the original array. This is because the smallest possible depth of a subarray (0) is not less than n=0. Thus, no subarray should be flattened. </pre>
|
||||
|
||||
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input</strong>
|
||||
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
n = 1
|
||||
|
||||
<strong>Output</strong>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once amongst the <code>p</code> pairs.</p>
|
||||
|
||||
<p>Note that for a pair of elements at the index <code>i</code> and <code>j</code>, the difference of this pair is <code>|nums[i] - nums[j]|</code>, where <code>|x|</code> represents the <strong>absolute</strong> <strong>value</strong> of <code>x</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> <strong>maximum</strong> difference among all </em><code>p</code> <em>pairs.</em> We define the maximum of an empty set to be zero.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,1,2,7,1,3], p = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
|
||||
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,1,2], p = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= p <= (nums.length)/2</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. There exists an array <code>arr</code> of length <code>nums.length</code>, where <code>arr[i]</code> is the sum of <code>|i - j|</code> over all <code>j</code> such that <code>nums[j] == nums[i]</code> and <code>j != i</code>. If there is no such <code>j</code>, set <code>arr[i]</code> to be <code>0</code>.</p>
|
||||
|
||||
<p>Return <em>the array </em><code>arr</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,1,1,2]
|
||||
<strong>Output:</strong> [5,0,3,4,0]
|
||||
<strong>Explanation:</strong>
|
||||
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
|
||||
When i = 1, arr[1] = 0 because there is no other index with value 3.
|
||||
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
|
||||
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
|
||||
When i = 4, arr[4] = 0 because there is no other index with value 2.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,5,3]
|
||||
<strong>Output:</strong> [0,0,0]
|
||||
<strong>Explanation:</strong> Since each element in nums is distinct, arr[i] = 0 for all i.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. Your initial position is at the <strong>top-left</strong> cell <code>(0, 0)</code>.</p>
|
||||
|
||||
<p>Starting from the cell <code>(i, j)</code>, you can move to one of the following cells:</p>
|
||||
|
||||
<ul>
|
||||
<li>Cells <code>(i, k)</code> with <code>j < k <= grid[i][j] + j</code> (rightward movement), or</li>
|
||||
<li>Cells <code>(k, j)</code> with <code>i < k <= grid[i][j] + i</code> (downward movement).</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of cells you need to visit to reach the <strong>bottom-right</strong> cell</em> <code>(m - 1, n - 1)</code>. If there is no valid path, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The image above shows one of the paths that visits exactly 4 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation: </strong>The image above shows one of the paths that visits exactly 3 cells.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,1,0],[1,0,0]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proven that no path exists.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] < m * n</code></li>
|
||||
<li><code>grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user