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
47
leetcode/originData/[no content]array-upper-bound.json
Normal file
47
leetcode/originData/[no content]array-upper-bound.json
Normal file
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2768",
|
||||
"questionFrontendId": "2774",
|
||||
"boundTopicId": null,
|
||||
"title": "Array Upper Bound",
|
||||
"titleSlug": "array-upper-bound",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Easy",
|
||||
"likes": 7,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Array Prototype Last\", \"titleSlug\": \"array-prototype-last\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Snail Traversal\", \"titleSlug\": \"snail-traversal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "[3,4,5]\n5\n[1,4,5]\n2\n[3,4,6,6,6,6,7]\n6",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"86\", \"totalSubmission\": \"100\", \"totalAcceptedRaw\": 86, \"totalSubmissionRaw\": 100, \"acRate\": \"86.0%\"}",
|
||||
"hints": [
|
||||
"Inside the Array.prototype.upperBound function you have access to the \"this\" keyword. You can access array elements, values, and methods. For example \"this[0]\", \"this[1]\", \"this.length\", \"this.map()\", etc.",
|
||||
"The most efficient way to solve this problem is with binary search.",
|
||||
"Choose the middle element and check if it's less than or equal to the goal value. If so, you can rule out the left side of the array."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[3,4,5]\n5",
|
||||
"metaData": "{\n \"name\": \"upperBound\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"target\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2782",
|
||||
"questionFrontendId": "2776",
|
||||
"boundTopicId": null,
|
||||
"title": "Convert Callback Based Function to Promise Based Function",
|
||||
"titleSlug": "convert-callback-based-function-to-promise-based-function",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 5,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "(callback, a, b, c) => callback(a * b * c)\n[1, 2, 3]\n(callback, a, b, c) => callback(a * b * c, \"Promise Rejected\")\n[4, 5, 6]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"43\", \"totalSubmission\": \"44\", \"totalAcceptedRaw\": 43, \"totalSubmissionRaw\": 44, \"acRate\": \"97.7%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "(callback, a, b, c) => callback(a * b * c)\n[1, 2, 3]",
|
||||
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"args\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
43
leetcode/originData/[no content]date-range-generator.json
Normal file
43
leetcode/originData/[no content]date-range-generator.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2799",
|
||||
"questionFrontendId": "2777",
|
||||
"boundTopicId": null,
|
||||
"title": "Date Range Generator",
|
||||
"titleSlug": "date-range-generator",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 3,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "\"2023-04-01\"\n\"2023-04-04\"\n1\n\"2023-04-10\"\n\"2023-04-20\"\n3\n\"2023-04-10\"\n\"2023-04-10\"\n1",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"49\", \"totalSubmission\": \"53\", \"totalAcceptedRaw\": 49, \"totalSubmissionRaw\": 53, \"acRate\": \"92.5%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "\"2023-04-01\"\n\"2023-04-04\"\n1",
|
||||
"metaData": "{\n \"name\": \"dateRangeGenerator\",\n \"params\": [\n {\n \"name\": \"start\",\n \"type\": \"integer\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"end\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"step\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
43
leetcode/originData/[no content]undefined-to-null.json
Normal file
43
leetcode/originData/[no content]undefined-to-null.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2781",
|
||||
"questionFrontendId": "2775",
|
||||
"boundTopicId": null,
|
||||
"title": "Undefined to Null",
|
||||
"titleSlug": "undefined-to-null",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 4,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"a\": undefined, \"b\":3}\n{\"a\": undefined, \"b\":[\"a\", undefined]}",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"47\", \"totalSubmission\": \"65\", \"totalAcceptedRaw\": 47, \"totalSubmissionRaw\": 65, \"acRate\": \"72.3%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"a\": undefined, \"b\":3}",
|
||||
"metaData": "{\n \"name\": \"undefinedToNull\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
167
leetcode/originData/find-the-maximum-achievable-number.json
Normal file
167
leetcode/originData/find-the-maximum-achievable-number.json
Normal file
File diff suppressed because one or more lines are too long
158
leetcode/originData/length-of-the-longest-valid-substring.json
Normal file
158
leetcode/originData/length-of-the-longest-valid-substring.json
Normal file
File diff suppressed because one or more lines are too long
173
leetcode/originData/longest-alternating-subarray.json
Normal file
173
leetcode/originData/longest-alternating-subarray.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
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
161
leetcode/originData/minimum-index-of-a-valid-split.json
Normal file
161
leetcode/originData/minimum-index-of-a-valid-split.json
Normal file
File diff suppressed because one or more lines are too long
182
leetcode/originData/number-of-black-blocks.json
Normal file
182
leetcode/originData/number-of-black-blocks.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
186
leetcode/originData/relocate-marbles.json
Normal file
186
leetcode/originData/relocate-marbles.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/sum-of-squares-of-special-elements.json
Normal file
161
leetcode/originData/sum-of-squares-of-special-elements.json
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>You can apply the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <strong>any</strong> subarray of size <code>k</code> from the array and <strong>decrease</strong> all its elements by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if you can make all the array elements equal to </em><code>0</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty part of an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,3,1,1,0], k = 3
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Choose the subarray [2,2,3]. The resulting array will be nums = [<strong><u>1</u></strong>,<strong><u>1</u></strong>,<strong><u>2</u></strong>,1,1,0].
|
||||
- Choose the subarray [2,1,1]. The resulting array will be nums = [1,1,<strong><u>1</u></strong>,<strong><u>0</u></strong>,<strong><u>0</u></strong>,0].
|
||||
- Choose the subarray [1,1,1]. The resulting array will be nums = [<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,1,1], k = 2
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to make all the array elements equal to 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
39
leetcode/problem/find-the-maximum-achievable-number.html
Normal file
39
leetcode/problem/find-the-maximum-achievable-number.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>You are given two integers, <code>num</code> and <code>t</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is called <b>achievable</b> if it can become equal to <code>num</code> after applying the following operation no more than <code>t</code> times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase or decrease <code>x</code> by <code>1</code>, and simultaneously increase or decrease <code>num</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum possible achievable number</em>. It can be proven that there exists at least one achievable number.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 4, t = 1
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The maximum achievable number is x = 6; it can become equal to num after performing this operation:
|
||||
1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
|
||||
It can be proven that there is no achievable number larger than 6.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 3, t = 2
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The maximum achievable number is x = 7; after performing these operations, x will equal num:
|
||||
1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4.
|
||||
2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
|
||||
It can be proven that there is no achievable number larger than 7.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num, t <= 50</code></li>
|
||||
</ul>
|
36
leetcode/problem/length-of-the-longest-valid-substring.html
Normal file
36
leetcode/problem/length-of-the-longest-valid-substring.html
Normal file
@@ -0,0 +1,36 @@
|
||||
<p>You are given a string <code>word</code> and an array of strings <code>forbidden</code>.</p>
|
||||
|
||||
<p>A string is called <strong>valid</strong> if none of its substrings are present in <code>forbidden</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest valid substring</strong> of the string </em><code>word</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string, possibly empty.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "cbaaaabc", forbidden = ["aaa","cb"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are 9 valid substrings in word: "c", "b", "a", "ba", "aa", "bc", "baa", "aab", and "aabc". The length of the longest valid substring is 4.
|
||||
It can be shown that all other substrings contain either "aaa" or "cb" as a substring. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "leetcode", forbidden = ["de","le","e"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are 11 valid substrings in word: "l", "t", "c", "o", "d", "tc", "co", "od", "tco", "cod", and "tcod". The length of the longest valid substring is 4.
|
||||
It can be shown that all other substrings contain either "de", "le", or "e" as a substring.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>word</code> consists only of lowercase English letters.</li>
|
||||
<li><code>1 <= forbidden.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= forbidden[i].length <= 10</code></li>
|
||||
<li><code>forbidden[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
36
leetcode/problem/longest-alternating-subarray.html
Normal file
36
leetcode/problem/longest-alternating-subarray.html
Normal file
@@ -0,0 +1,36 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A subarray <code>s</code> of length <code>m</code> is called <strong>alternating</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>m</code> is greater than <code>1</code>.</li>
|
||||
<li><code>s<sub>1</sub> = s<sub>0</sub> + 1</code>.</li>
|
||||
<li>The <strong>0-indexed</strong> subarray <code>s</code> looks like <code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>. In other words, <code>s<sub>1</sub> - s<sub>0</sub> = 1</code>, <code>s<sub>2</sub> - s<sub>1</sub> = -1</code>, <code>s<sub>3</sub> - s<sub>2</sub> = 1</code>, <code>s<sub>4</sub> - s<sub>3</sub> = -1</code>, and so on up to <code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum length of all <strong>alternating</strong> subarrays present in </em><code>nums</code> <em>or </em><code>-1</code><em> if no such subarray exists</em><em>.</em></p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,4,3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,5,6]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>Let's define another <strong>0-indexed</strong> integer array, <code>nums3</code>, of length <code>n</code>. For each index <code>i</code> in the range <code>[0, n - 1]</code>, you can assign either <code>nums1[i]</code> or <code>nums2[i]</code> to <code>nums3[i]</code>.</p>
|
||||
|
||||
<p>Your task is to maximize the length of the <strong>longest non-decreasing subarray</strong> in <code>nums3</code> by choosing its values optimally.</p>
|
||||
|
||||
<p>Return <em>an integer representing the length of the <strong>longest non-decreasing</strong> subarray in</em> <code>nums3</code>.</p>
|
||||
|
||||
<p><strong>Note: </strong>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,3,1], nums2 = [1,2,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>One way to construct nums3 is:
|
||||
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
|
||||
The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
|
||||
We can show that 2 is the maximum achievable length.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,3,2,1], nums2 = [2,2,3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One way to construct nums3 is:
|
||||
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
|
||||
The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,1], nums2 = [2,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> One way to construct nums3 is:
|
||||
nums3 = [nums1[0], nums1[1]] => [1,1].
|
||||
The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length == nums2.length == n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>In one operation, you can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> that <strong>hasn't been chosen before</strong> from the range <code>[0, nums.length - 1]</code>.</li>
|
||||
<li>Replace <code>nums[i]</code> with any integer from the range <code>[nums[i] - k, nums[i] + k]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>beauty</strong> of the array is the length of the longest subsequence consisting of equal elements.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible beauty of the array </em><code>nums</code><em> after applying the operation any number of times.</em></p>
|
||||
|
||||
<p><strong>Note</strong> that you can apply the operation to each index <strong>only once</strong>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,6,1,2], k = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, we apply the following operations:
|
||||
- Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
|
||||
- Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
|
||||
After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
|
||||
It can be proven that 3 is the maximum possible length we can achieve.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1], k = 10
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example we don't have to apply any operations.
|
||||
The beauty of the array nums is 4 (whole array).
|
||||
</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], k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers and an integer <code>target</code>.</p>
|
||||
|
||||
<p>You are initially positioned at index <code>0</code>. In one step, you can jump from index <code>i</code> to any index <code>j</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < n</code></li>
|
||||
<li><code>-target <= nums[j] - nums[i] <= target</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum number of jumps</strong> you can make to reach index</em> <code>n - 1</code>.</p>
|
||||
|
||||
<p>If there is no way to reach index <code>n - 1</code>, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
|
||||
- Jump from index 0 to index 1.
|
||||
- Jump from index 1 to index 3.
|
||||
- Jump from index 3 to index 5.
|
||||
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
|
||||
- Jump from index 0 to index 1.
|
||||
- Jump from index 1 to index 2.
|
||||
- Jump from index 2 to index 3.
|
||||
- Jump from index 3 to index 4.
|
||||
- Jump from index 4 to index 5.
|
||||
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,6,4,1,2], target = 0
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length == n <= 1000</code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= target <= 2 * 10<sup>9</sup></code></li>
|
||||
</ul>
|
54
leetcode/problem/minimum-index-of-a-valid-split.html
Normal file
54
leetcode/problem/minimum-index-of-a-valid-split.html
Normal file
@@ -0,0 +1,54 @@
|
||||
<p>An element <code>x</code> of an integer array <code>arr</code> of length <code>m</code> is <strong>dominant</strong> if <code>freq(x) * 2 > m</code>, where <code>freq(x)</code> is the number of occurrences of <code>x</code> in <code>arr</code>. Note that this definition implies that <code>arr</code> can have <strong>at most one</strong> dominant element.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> with one dominant element.</p>
|
||||
|
||||
<p>You can split <code>nums</code> at an index <code>i</code> into two arrays <code>nums[0, ..., i]</code> and <code>nums[i + 1, ..., n - 1]</code>, but the split is only <strong>valid</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < n - 1</code></li>
|
||||
<li><code>nums[0, ..., i]</code>, and <code>nums[i + 1, ..., n - 1]</code> have the same dominant element.</li>
|
||||
</ul>
|
||||
|
||||
<p>Here, <code>nums[i, ..., j]</code> denotes the subarray of <code>nums</code> starting at index <code>i</code> and ending at index <code>j</code>, both ends being inclusive. Particularly, if <code>j < i</code> then <code>nums[i, ..., j]</code> denotes an empty subarray.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> index of a <strong>valid split</strong></em>. If no valid split exists, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can split the array at index 2 to obtain arrays [1,2,2] and [2].
|
||||
In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3.
|
||||
In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1.
|
||||
Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split.
|
||||
It can be shown that index 2 is the minimum index of a valid split. </pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,1,1,1,7,1,2,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1].
|
||||
In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
|
||||
In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
|
||||
Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split.
|
||||
It can be shown that index 4 is the minimum index of a valid split.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,3,3,7,2,2]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that there is no valid split.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>nums</code> has exactly one dominant element.</li>
|
||||
</ul>
|
45
leetcode/problem/number-of-black-blocks.html
Normal file
45
leetcode/problem/number-of-black-blocks.html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>You are given two integers <code>m</code> and <code>n</code> representing the dimensions of a <strong>0-indexed</strong> <code>m x n</code> grid.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> 2D integer matrix <code>coordinates</code>, where <code>coordinates[i] = [x, y]</code> indicates that the cell with coordinates <code>[x, y]</code> is colored <strong>black</strong>. All cells in the grid that do not appear in <code>coordinates</code> are <strong>white</strong>.</p>
|
||||
|
||||
<p>A block is defined as a <code>2 x 2</code> submatrix of the grid. More formally, a block with cell <code>[x, y]</code> as its top-left corner where <code>0 <= x < m - 1</code> and <code>0 <= y < n - 1</code> contains the coordinates <code>[x, y]</code>, <code>[x + 1, y]</code>, <code>[x, y + 1]</code>, and <code>[x + 1, y + 1]</code>.</p>
|
||||
|
||||
<p>Return <em>a <strong>0-indexed</strong> integer array</em> <code>arr</code> <em>of size</em> <code>5</code> <em>such that</em> <code>arr[i]</code> <em>is the number of blocks that contains exactly</em> <code>i</code> <em><strong>black</strong> cells</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 3, coordinates = [[0,0]]
|
||||
<strong>Output:</strong> [3,1,0,0,0]
|
||||
<strong>Explanation:</strong> The grid looks like this:
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png" style="width: 150px; height: 128px;" />
|
||||
There is only 1 block with one black cell, and it is the block starting with cell [0,0].
|
||||
The other 3 blocks start with cells [0,1], [1,0] and [1,1]. They all have zero black cells.
|
||||
Thus, we return [3,1,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]]
|
||||
<strong>Output:</strong> [0,2,2,0,0]
|
||||
<strong>Explanation:</strong> The grid looks like this:
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png" style="width: 150px; height: 128px;" />
|
||||
There are 2 blocks with two black cells (the ones starting with cell coordinates [0,0] and [0,1]).
|
||||
The other 2 blocks have starting cell coordinates of [1,0] and [1,1]. They both have 1 black cell.
|
||||
Therefore, we return [0,2,2,0,0].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= m <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= coordinates.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>coordinates[i].length == 2</code></li>
|
||||
<li><code>0 <= coordinates[i][0] < m</code></li>
|
||||
<li><code>0 <= coordinates[i][1] < n</code></li>
|
||||
<li>It is guaranteed that <code>coordinates</code> contains pairwise distinct coordinates.</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>Given a binary string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that each substring is <strong>beautiful</strong>.</p>
|
||||
|
||||
<p>A string is <strong>beautiful</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li>It doesn't contain leading zeros.</li>
|
||||
<li>It's the <strong>binary</strong> representation of a number that is a power of <code>5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of substrings in such partition. </em>If it is impossible to partition the string <code>s</code> into beautiful substrings, return <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1011"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can paritition the given string into ["101", "1"].
|
||||
- The string "101" does not contain leading zeros and is the binary representation of integer 5<sup>1</sup> = 5.
|
||||
- The string "1" does not contain leading zeros and is the binary representation of integer 5<sup>0</sup> = 1.
|
||||
It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "111"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can paritition the given string into ["1", "1", "1"].
|
||||
- The string "1" does not contain leading zeros and is the binary representation of integer 5<sup>0</sup> = 1.
|
||||
It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> We can not partition the given string into beautiful substrings.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 15</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
46
leetcode/problem/relocate-marbles.html
Normal file
46
leetcode/problem/relocate-marbles.html
Normal file
@@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the initial positions of some marbles. You are also given two <strong>0-indexed </strong>integer arrays <code>moveFrom</code> and <code>moveTo</code> of <strong>equal</strong> length.</p>
|
||||
|
||||
<p>Throughout <code>moveFrom.length</code> steps, you will change the positions of the marbles. On the <code>i<sup>th</sup></code> step, you will move <strong>all</strong> marbles at position <code>moveFrom[i]</code> to position <code>moveTo[i]</code>.</p>
|
||||
|
||||
<p>After completing all the steps, return <em>the sorted list of <strong>occupied</strong> positions</em>.</p>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>We call a position <strong>occupied</strong> if there is at least one marble in that position.</li>
|
||||
<li>There may be multiple marbles in a single position.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5]
|
||||
<strong>Output:</strong> [5,6,8,9]
|
||||
<strong>Explanation:</strong> Initially, the marbles are at positions 1,6,7,8.
|
||||
At the i = 0th step, we move the marbles at position 1 to position 2. Then, positions 2,6,7,8 are occupied.
|
||||
At the i = 1st step, we move the marbles at position 7 to position 9. Then, positions 2,6,8,9 are occupied.
|
||||
At the i = 2nd step, we move the marbles at position 2 to position 5. Then, positions 5,6,8,9 are occupied.
|
||||
At the end, the final positions containing at least one marbles are [5,6,8,9].</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong> Initially, the marbles are at positions [1,1,3,3].
|
||||
At the i = 0th step, we move all the marbles at position 1 to position 2. Then, the marbles are at positions [2,2,3,3].
|
||||
At the i = 1st step, we move all the marbles at position 3 to position 2. Then, the marbles are at positions [2,2,2,2].
|
||||
Since 2 is the only occupied position, we return [2].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= moveFrom.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>moveFrom.length == moveTo.length</code></li>
|
||||
<li><code>1 <= nums[i], moveFrom[i], moveTo[i] <= 10<sup>9</sup></code></li>
|
||||
<li>The test cases are generated such that there is at least a marble in <code>moveFrom[i]</code> at the moment we want to apply the <code>i<sup>th</sup></code> move.</li>
|
||||
</ul>
|
32
leetcode/problem/sum-of-squares-of-special-elements.html
Normal file
32
leetcode/problem/sum-of-squares-of-special-elements.html
Normal file
@@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>1-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>An element <code>nums[i]</code> of <code>nums</code> is called <strong>special</strong> if <code>i</code> divides <code>n</code>, i.e. <code>n % i == 0</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>sum of the squares</strong> of all <strong>special</strong> elements of </em><code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 21
|
||||
<strong>Explanation:</strong> There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4.
|
||||
Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,7,1,19,18,3]
|
||||
<strong>Output:</strong> 63
|
||||
<strong>Explanation:</strong> There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6.
|
||||
Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length == n <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user