1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/minimum-cost-to-make-array-equal.json

206 lines
18 KiB
JSON
Raw Normal View History

2022-10-26 20:38:53 +08:00
{
"data": {
"question": {
"questionId": "2538",
"questionFrontendId": "2448",
"boundTopicId": null,
"title": "Minimum Cost to Make Array Equal",
"titleSlug": "minimum-cost-to-make-array-equal",
2023-12-09 18:42:21 +08:00
"content": "<p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>\n\n<p>You can do the following operation <strong>any</strong> number of times:</p>\n\n<ul>\n\t<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>\n</ul>\n\n<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>\n\n<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]\n<strong>Output:</strong> 8\n<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:\n- Increase the 0<sup>th</sup> element one time. The cost is 2.\n- Decrease the 1<sup><span style=\"font-size: 10.8333px;\">st</span></sup> element one time. The cost is 3.\n- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.\nThe total cost is 2 + 3 + 3 = 8.\nIt can be shown that we cannot make the array equal with a smaller cost.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == nums.length == cost.length</code></li>\n\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i], cost[i] &lt;= 10<sup>6</sup></code></li>\n\t<li>Test cases are generated in a way that the output doesn&#39;t exceed&nbsp;2<sup>53</sup>-1</li>\n</ul>\n",
2022-10-26 20:38:53 +08:00
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Hard",
2023-12-09 18:42:21 +08:00
"likes": 2321,
"dislikes": 34,
2022-10-26 20:38:53 +08:00
"isLiked": null,
2023-12-09 18:42:21 +08:00
"similarQuestions": "[{\"title\": \"Minimum Moves to Equal Array Elements II\", \"titleSlug\": \"minimum-moves-to-equal-array-elements-ii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Maximum Product of the Length of Two Palindromic Substrings\", \"titleSlug\": \"maximum-product-of-the-length-of-two-palindromic-substrings\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Minimum Amount of Time to Fill Cups\", \"titleSlug\": \"minimum-amount-of-time-to-fill-cups\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Minimum Operations to Make All Array Elements Equal\", \"titleSlug\": \"minimum-operations-to-make-all-array-elements-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
2022-10-26 20:38:53 +08:00
"exampleTestcases": "[1,3,5,2]\n[2,3,1,14]\n[2,2,2,2,2]\n[4,2,8,1,3]",
"categoryTitle": "Algorithms",
"contributors": [],
"topicTags": [
{
"name": "Array",
"slug": "array",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Binary Search",
"slug": "binary-search",
"translatedName": null,
"__typename": "TopicTagNode"
},
2023-12-09 18:42:21 +08:00
{
"name": "Greedy",
"slug": "greedy",
"translatedName": null,
"__typename": "TopicTagNode"
},
2022-10-26 20:38:53 +08:00
{
"name": "Sorting",
"slug": "sorting",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Prefix Sum",
"slug": "prefix-sum",
"translatedName": null,
"__typename": "TopicTagNode"
}
],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "C++",
"langSlug": "cpp",
"code": "class Solution {\npublic:\n long long minCost(vector<int>& nums, vector<int>& cost) {\n \n }\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Java",
"langSlug": "java",
"code": "class Solution {\n public long minCost(int[] nums, int[] cost) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python",
"langSlug": "python",
"code": "class Solution(object):\n def minCost(self, nums, cost):\n \"\"\"\n :type nums: List[int]\n :type cost: List[int]\n :rtype: int\n \"\"\"\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python3",
"langSlug": "python3",
"code": "class Solution:\n def minCost(self, nums: List[int], cost: List[int]) -> int:\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "C",
"langSlug": "c",
2023-12-09 18:42:21 +08:00
"code": "long long minCost(int* nums, int numsSize, int* cost, int costSize) {\n \n}",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "C#",
"langSlug": "csharp",
"code": "public class Solution {\n public long MinCost(int[] nums, int[] cost) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {number[]} nums\n * @param {number[]} cost\n * @return {number}\n */\nvar minCost = function(nums, cost) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function minCost(nums: number[], cost: number[]): number {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "PHP",
"langSlug": "php",
"code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer[] $cost\n * @return Integer\n */\n function minCost($nums, $cost) {\n \n }\n}",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "Swift",
"langSlug": "swift",
"code": "class Solution {\n func minCost(_ nums: [Int], _ cost: [Int]) -> Int {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Kotlin",
"langSlug": "kotlin",
"code": "class Solution {\n fun minCost(nums: IntArray, cost: IntArray): Long {\n \n }\n}",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Dart",
"langSlug": "dart",
"code": "class Solution {\n int minCost(List<int> nums, List<int> cost) {\n \n }\n}",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Go",
"langSlug": "golang",
"code": "func minCost(nums []int, cost []int) int64 {\n \n}",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Ruby",
"langSlug": "ruby",
"code": "# @param {Integer[]} nums\n# @param {Integer[]} cost\n# @return {Integer}\ndef min_cost(nums, cost)\n \nend",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Scala",
"langSlug": "scala",
"code": "object Solution {\n def minCost(nums: Array[Int], cost: Array[Int]): Long = {\n \n }\n}",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
2023-12-09 18:42:21 +08:00
"lang": "Rust",
"langSlug": "rust",
"code": "impl Solution {\n pub fn min_cost(nums: Vec<i32>, cost: Vec<i32>) -> i64 {\n \n }\n}",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "Racket",
"langSlug": "racket",
2023-12-09 18:42:21 +08:00
"code": "(define/contract (min-cost nums cost)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "Erlang",
"langSlug": "erlang",
"code": "-spec min_cost(Nums :: [integer()], Cost :: [integer()]) -> integer().\nmin_cost(Nums, Cost) ->\n .",
"__typename": "CodeSnippetNode"
},
{
"lang": "Elixir",
"langSlug": "elixir",
2023-12-09 18:42:21 +08:00
"code": "defmodule Solution do\n @spec min_cost(nums :: [integer], cost :: [integer]) :: integer\n def min_cost(nums, cost) do\n \n end\nend",
2022-10-26 20:38:53 +08:00
"__typename": "CodeSnippetNode"
}
],
2023-12-09 18:42:21 +08:00
"stats": "{\"totalAccepted\": \"63K\", \"totalSubmission\": \"137.4K\", \"totalAcceptedRaw\": 62996, \"totalSubmissionRaw\": 137393, \"acRate\": \"45.9%\"}",
2022-10-26 20:38:53 +08:00
"hints": [
"Changing the elements into one of the numbers already existing in the array nums is optimal.",
"Try finding the cost of changing the array into each element, and return the minimum value."
],
2023-12-09 18:42:21 +08:00
"solution": {
"id": "1803",
"canSeeDetail": false,
"paidOnly": true,
"hasVideoSolution": false,
"paidOnlyVideo": true,
"__typename": "ArticleNode"
},
2022-10-26 20:38:53 +08:00
"status": null,
"sampleTestCase": "[1,3,5,2]\n[2,3,1,14]",
"metaData": "{\n \"name\": \"minCost\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"cost\"\n }\n ],\n \"return\": {\n \"type\": \"long\"\n }\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": true,
2023-12-09 18:42:21 +08:00
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 11 </code> using the latest C++ 20 standard.</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 17</code>. Java 8 features such as lambda expressions and stream API can be used. </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 8.2</code> using the gnu11 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O1</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-10\\\" target=\\\"_blank\\\">C# 10 with .NET 6 runtime</a></p>\"], \"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://githu
2022-10-26 20:38:53 +08:00
"libraryUrl": null,
"adminUrl": null,
2023-12-09 18:42:21 +08:00
"challengeQuestion": {
"id": "1380",
"date": "2023-06-21",
"incompleteChallengeCount": 0,
"streakCount": 0,
"type": "DAILY",
"__typename": "ChallengeQuestionNode"
},
2022-10-26 20:38:53 +08:00
"__typename": "QuestionNode"
}
}
}