mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
182 lines
20 KiB
JSON
182 lines
20 KiB
JSON
|
{
|
||
|
"data": {
|
||
|
"question": {
|
||
|
"questionId": "3438",
|
||
|
"questionFrontendId": "3187",
|
||
|
"boundTopicId": null,
|
||
|
"title": "Peaks in Array",
|
||
|
"titleSlug": "peaks-in-array",
|
||
|
"content": "<p>A <strong>peak</strong> in an array <code>arr</code> is an element that is <strong>greater</strong> than its previous and next element in <code>arr</code>.</p>\n\n<p>You are given an integer array <code>nums</code> and a 2D integer array <code>queries</code>.</p>\n\n<p>You have to process queries of two types:</p>\n\n<ul>\n\t<li><code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of <strong>peak</strong> elements in the <span data-keyword=\"subarray\">subarray</span> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.<!-- notionvc: 73b20b7c-e1ab-4dac-86d0-13761094a9ae --></li>\n\t<li><code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code><font face=\"monospace\">val<sub>i</sub></font></code>.</li>\n</ul>\n\n<p>Return an array <code>answer</code> containing the results of the queries of the first type in order.<!-- notionvc: a9ccef22-4061-4b5a-b4cc-a2b2a0e12f30 --></p>\n\n<p><strong>Notes:</strong></p>\n\n<ul>\n\t<li>The <strong>first</strong> and the <strong>last</strong> element of an array or a subarray<!-- notionvc: fcffef72-deb5-47cb-8719-3a3790102f73 --> <strong>cannot</strong> be a peak.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong> <span class=\"example-io\">nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]</span></p>\n\n<p><strong>Output:</strong> <span class=\"example-io\">[0]</span></p>\n\n<p><strong>Explanation:</strong></p>\n\n<p>First query: We change <code>nums[3]</code> to 4 and <code>nums</code> becomes <code>[3,1,4,4,5]</code>.</p>\n\n<p>Second query: The number of peaks in the <code>[3,1,4,4,5]</code> is 0.</p>\n</div>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<div class=\"example-block\">\n<p><strong>Input:</strong> <span class=\"example-io\">nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]</span></p>\n\n<p><strong>Output:</strong> <span class=\"example-io\">[0,1]</span></p>\n\n<p><strong>Explanation:</strong></p>\n\n<p>First query: <code>nums[2]</code> should become 4, but it is already set to 4.</p>\n\n<p>Second query: The number of peaks in the <code>[4,1,4]</code> is 0.</p>\n\n<p>Third query: The second 4 is a peak in the <code>[4,1,4,2,1]</code>.</p>\n</div>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>\n\t<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>\n\t<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>\n\t<li><code>queries[i][0] == 1</code> or <code>queries[i][0] == 2</code></li>\n\t<li>For all <code>i</code> that:\n\t<ul>\n\t\t<li><code>queries[i][0] == 1</code>: <code>0 <= queries[i][1] <= queries[i][2] <= nums.length - 1</code></li>\n\t\t<li><code>queries[i][0] == 2</code>: <code>0 <= queries[i][1] <= nums.length - 1</code>, <code>1 <= queries[i][2] <= 10<sup>5</sup></code></li>\n\t</ul>\n\t</li>\n</ul>\n",
|
||
|
"translatedTitle": null,
|
||
|
"translatedContent": null,
|
||
|
"isPaidOnly": false,
|
||
|
"difficulty": "Hard",
|
||
|
"likes": 91,
|
||
|
"dislikes": 7,
|
||
|
"isLiked": null,
|
||
|
"similarQuestions": "[]",
|
||
|
"exampleTestcases": "[3,1,4,2,5]\n[[2,3,4],[1,0,4]]\n[4,1,4,2,1,5]\n[[2,2,4],[1,0,2],[1,0,4]]",
|
||
|
"categoryTitle": "Algorithms",
|
||
|
"contributors": [],
|
||
|
"topicTags": [
|
||
|
{
|
||
|
"name": "Array",
|
||
|
"slug": "array",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Binary Indexed Tree",
|
||
|
"slug": "binary-indexed-tree",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
},
|
||
|
{
|
||
|
"name": "Segment Tree",
|
||
|
"slug": "segment-tree",
|
||
|
"translatedName": null,
|
||
|
"__typename": "TopicTagNode"
|
||
|
}
|
||
|
],
|
||
|
"companyTagStats": null,
|
||
|
"codeSnippets": [
|
||
|
{
|
||
|
"lang": "C++",
|
||
|
"langSlug": "cpp",
|
||
|
"code": "class Solution {\npublic:\n vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) {\n \n }\n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Java",
|
||
|
"langSlug": "java",
|
||
|
"code": "class Solution {\n public List<Integer> countOfPeaks(int[] nums, int[][] queries) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python",
|
||
|
"langSlug": "python",
|
||
|
"code": "class Solution(object):\n def countOfPeaks(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Python3",
|
||
|
"langSlug": "python3",
|
||
|
"code": "class Solution:\n def countOfPeaks(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n ",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C",
|
||
|
"langSlug": "c",
|
||
|
"code": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* countOfPeaks(int* nums, int numsSize, int** queries, int queriesSize, int* queriesColSize, int* returnSize) {\n \n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "C#",
|
||
|
"langSlug": "csharp",
|
||
|
"code": "public class Solution {\n public IList<int> CountOfPeaks(int[] nums, int[][] queries) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "JavaScript",
|
||
|
"langSlug": "javascript",
|
||
|
"code": "/**\n * @param {number[]} nums\n * @param {number[][]} queries\n * @return {number[]}\n */\nvar countOfPeaks = function(nums, queries) {\n \n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "TypeScript",
|
||
|
"langSlug": "typescript",
|
||
|
"code": "function countOfPeaks(nums: number[], queries: number[][]): number[] {\n \n};",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "PHP",
|
||
|
"langSlug": "php",
|
||
|
"code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer[][] $queries\n * @return Integer[]\n */\n function countOfPeaks($nums, $queries) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Swift",
|
||
|
"langSlug": "swift",
|
||
|
"code": "class Solution {\n func countOfPeaks(_ nums: [Int], _ queries: [[Int]]) -> [Int] {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Kotlin",
|
||
|
"langSlug": "kotlin",
|
||
|
"code": "class Solution {\n fun countOfPeaks(nums: IntArray, queries: Array<IntArray>): List<Int> {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Dart",
|
||
|
"langSlug": "dart",
|
||
|
"code": "class Solution {\n List<int> countOfPeaks(List<int> nums, List<List<int>> queries) {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Go",
|
||
|
"langSlug": "golang",
|
||
|
"code": "func countOfPeaks(nums []int, queries [][]int) []int {\n \n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Ruby",
|
||
|
"langSlug": "ruby",
|
||
|
"code": "# @param {Integer[]} nums\n# @param {Integer[][]} queries\n# @return {Integer[]}\ndef count_of_peaks(nums, queries)\n \nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Scala",
|
||
|
"langSlug": "scala",
|
||
|
"code": "object Solution {\n def countOfPeaks(nums: Array[Int], queries: Array[Array[Int]]): List[Int] = {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Rust",
|
||
|
"langSlug": "rust",
|
||
|
"code": "impl Solution {\n pub fn count_of_peaks(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {\n \n }\n}",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Racket",
|
||
|
"langSlug": "racket",
|
||
|
"code": "(define/contract (count-of-peaks nums queries)\n (-> (listof exact-integer?) (listof (listof exact-integer?)) (listof exact-integer?))\n )",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Erlang",
|
||
|
"langSlug": "erlang",
|
||
|
"code": "-spec count_of_peaks(Nums :: [integer()], Queries :: [[integer()]]) -> [integer()].\ncount_of_peaks(Nums, Queries) ->\n .",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
},
|
||
|
{
|
||
|
"lang": "Elixir",
|
||
|
"langSlug": "elixir",
|
||
|
"code": "defmodule Solution do\n @spec count_of_peaks(nums :: [integer], queries :: [[integer]]) :: [integer]\n def count_of_peaks(nums, queries) do\n \n end\nend",
|
||
|
"__typename": "CodeSnippetNode"
|
||
|
}
|
||
|
],
|
||
|
"stats": "{\"totalAccepted\": \"7.4K\", \"totalSubmission\": \"28.6K\", \"totalAcceptedRaw\": 7446, \"totalSubmissionRaw\": 28646, \"acRate\": \"26.0%\"}",
|
||
|
"hints": [
|
||
|
"Let <code>p[i]</code> be whether <code>nums[i]</code> is a peak in the original array. Namely <code>p[i] = nums[i] > nums[i - 1] && nums[i] > nums[i + 1]</code>.",
|
||
|
"Updating <code>nums[i]</code>, only affects <code>p[i]</code>, <code>p[i - 1]</code> and <code>p[i + 1]</code>. We can recalculate the 3 values in constant time.",
|
||
|
"The answer for <code>[l<sub>i</sub>, r<sub>i</sub>]</code> is <code>p[l<sub>i</sub> + 1] + p[l<sub>i</sub> + 2] + … + p[r<sub>i</sub> - 1]</code> (note that <code>l<sub>i</sub></code> and <code>r<sub>i</sub></code> are not included).",
|
||
|
"Use some data structures (i.e. segment tree or binary indexed tree) to maintain the subarray sum efficiently."
|
||
|
],
|
||
|
"solution": null,
|
||
|
"status": null,
|
||
|
"sampleTestCase": "[3,1,4,2,5]\n[[2,3,4],[1,0,4]]",
|
||
|
"metaData": "{\n \"name\": \"countOfPeaks\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"queries\"\n }\n ],\n \"return\": {\n \"type\": \"list<integer>\"\n }\n}",
|
||
|
"judgerAvailable": true,
|
||
|
"judgeType": "large",
|
||
|
"mysqlSchemas": [],
|
||
|
"enableRunCode": true,
|
||
|
"enableTestMode": false,
|
||
|
"enableDebugger": true,
|
||
|
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 17 </code> using the latest C++ 20 standard, and <code>libstdc++</code> provided by GCC 11.</p>\\r\\n\\r\\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\"], \"java\": [\"Java\", \"<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>\"], \"python\": [\"Python\", \"<p><code>Python 2.7.12</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/2/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/2/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/2/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\\r\\n\\r\\n<p>Note that Python 2.7 <a href=\\\"https://www.python.org/dev/peps/pep-0373/\\\" target=\\\"_blank\\\">will not be maintained past 2020</a>. For the latest Python, please choose Python3 instead.</p>\"], \"c\": [\"C\", \"<p>Compiled with <code>gcc 11</code> using the gnu11 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n\\r\\n<p>For hash table operations, you may use <a href=\\\"https://troydhanson.github.io/uthash/\\\" target=\\\"_blank\\\">uthash</a>. \\\"uthash.h\\\" is included by default. Below are some examples:</p>\\r\\n\\r\\n<p><b>1. Adding an item to a hash.</b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>2. Looking up an item in a hash:</b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>3. Deleting an item in a hash:</b>\\r\\n<pre>\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n</pre>\\r\\n</p>\"], \"csharp\": [\"C#\", \"<p><a href=\\\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12\\\" target=\\\"_blank\\\">C# 12</a> with .NET 8 runtime</p>\"], \"javascript\": [\"JavaScript\", \"<p><code>Node.js 20.10.0</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.4.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/blob/v5/README.md\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\\\
|
||
|
"libraryUrl": null,
|
||
|
"adminUrl": null,
|
||
|
"challengeQuestion": null,
|
||
|
"__typename": "QuestionNode"
|
||
|
}
|
||
|
}
|
||
|
}
|