Team Ai
Datasetpublic

FPEvalRepoPublic/LeetCodeMetaData

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes399downloads
minimum-operations-to-make-all-array-elements-equal.json32 linesDownload Raw Back to root
1{2  "id": 2718,3  "name": "minimum-operations-to-make-all-array-elements-equal",4  "difficulty": "Medium",5  "link": "https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/",6  "date": "2023-03-19",7  "task_description": "You are given an array `nums` consisting of positive integers. You are also given an integer array `queries` of size `m`. For the `ith` query, you want to make all of the elements of `nums` equal to` queries[i]`. You can perform the following operation on the array **any** number of times: **Increase** or **decrease** an element of the array by `1`. Return _an array _`answer`_ of size _`m`_ where _`answer[i]`_ is the **minimum** number of operations to make all elements of _`nums`_ equal to _`queries[i]`. **Note** that after each query the array is reset to its original state. **Example 1:** ``` **Input:** nums = [3,1,6,8], queries = [1,5] **Output:** [14,10] **Explanation:** For the first query we can do the following operations: - Decrease nums[0] 2 times, so that nums = [1,1,6,8]. - Decrease nums[2] 5 times, so that nums = [1,1,1,8]. - Decrease nums[3] 7 times, so that nums = [1,1,1,1]. So the total number of operations for the first query is 2 + 5 + 7 = 14. For the second query we can do the following operations: - Increase nums[0] 2 times, so that nums = [5,1,6,8]. - Increase nums[1] 4 times, so that nums = [5,5,6,8]. - Decrease nums[2] 1 time, so that nums = [5,5,5,8]. - Decrease nums[3] 3 times, so that nums = [5,5,5,5]. So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10. ``` **Example 2:** ``` **Input:** nums = [2,9,6,3], queries = [10] **Output:** [20] **Explanation:** We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20. ``` **Constraints:** `n == nums.length` `m == queries.length` `1 <= n, m <= 105` `1 <= nums[i], queries[i] <= 109`",8  "test_case": [9    {10      "label": "Example 1",11      "input": "nums = [3,1,6,8], queries = [1,5]",12      "output": "[14,10] "13    },14    {15      "label": "Example 2",16      "input": "nums = [2,9,6,3], queries = [10]",17      "output": "[20] "18    }19  ],20  "constraints": [21    "Increase or decrease an element of the array by 1.",22    "n == nums.length",23    "m == queries.length",24    "1 <= n, m <= 105",25    "1 <= nums[i], queries[i] <= 109"26  ],27  "python_template": "class Solution(object):\n    def minOperations(self, nums, queries):\n        \"\"\"\n        :type nums: List[int]\n        :type queries: List[int]\n        :rtype: List[int]\n        \"\"\"\n        ",28  "java_template": "class Solution {\n    public List<Long> minOperations(int[] nums, int[] queries) {\n        \n    }\n}",29  "metadata": {30    "func_name": "minOperations"31  }32}