1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/array-reduce-transformation.json
2023-04-14 14:39:57 +08:00

59 lines
6.6 KiB
JSON

{
"data": {
"question": {
"questionId": "2761",
"questionFrontendId": "2626",
"boundTopicId": null,
"title": "Array Reduce Transformation",
"titleSlug": "array-reduce-transformation",
"content": "<p>Given an integer array&nbsp;<code>nums</code>, a reducer function&nbsp;<code>fn</code>, and an intial value&nbsp;<code>init</code>, return a&nbsp;<strong>reduced</strong>&nbsp;array.</p>\n\n<p>A&nbsp;<strong>reduced</strong>&nbsp;array is created by applying the following operation:&nbsp;<code>val = fn(init, nums[0])</code>, <code>val&nbsp;= fn(val, nums[1])</code>,&nbsp;<code>val&nbsp;= fn(val, arr[2])</code>,&nbsp;<code>...</code>&nbsp;until every element in the array has been processed. The final value of&nbsp;<code>val</code>&nbsp;is returned.</p>\n\n<p>If the length of the array is 0, it should return&nbsp;<code>init</code>.</p>\n\n<p>Please solve it without using the built-in <code>Array.reduce</code> method.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr; }\ninit = 0\n<strong>Output:</strong> 10\n<strong>Explanation:</strong>\ninitially, the value is init=0.\n(0) + nums[0] = 1\n(1) + nums[1] = 3\n(3) + nums[2] = 6\n(6) + nums[3] = 10\nThe final answer is 10.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr * curr; }\ninit = 100\n<strong>Output:</strong> 130\n<strong>Explanation:</strong>\ninitially, the value is init=100.\n(100) + nums[0]^2 = 101\n(101) + nums[1]^2 = 105\n(105) + nums[2]^2 = 114\n(114) + nums[3]^2 = 130\nThe final answer is 130.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = []\nfn = function sum(accum, curr) { return 0; }\ninit = 25\n<strong>Output:</strong> 25\n<strong>Explanation:</strong> For empty arrays, the answer is always init.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= nums.length &lt;= 1000</code></li>\n\t<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>\n\t<li><code>0 &lt;= init &lt;= 1000</code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 18,
"dislikes": 2,
"isLiked": null,
"similarQuestions": "[{\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Apply Transform Over Each Element in Array\", \"titleSlug\": \"apply-transform-over-each-element-in-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
"exampleTestcases": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0\n[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr * curr; }\n100\n[]\nfunction sum(accum, curr) { return 0; }\n25",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "type Fn = (accum: number, curr: number) => number\n\nfunction reduce(nums: number[], fn: Fn, init: number): number {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"1.1K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1117, \"totalSubmissionRaw\": 1278, \"acRate\": \"87.4%\"}",
"hints": [
"Declare a variable \"res\" and set it it equal to the initial value.",
"Loop over each value in the array and set \"res\" = fn(res, arr[i])."
],
"solution": null,
"status": null,
"sampleTestCase": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0",
"metaData": "{\n \"name\": \"reduce\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"init\"\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 4.5.4, 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 ES2020 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"
}
}
}