Team Ai
Datasetpublic

FPEvalRepoPublic/LeetCodeMetaData

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes399downloads
minimum-operations-to-halve-array-sum.json29 linesDownload Raw Back to root
1{2  "id": 2310,3  "name": "minimum-operations-to-halve-array-sum",4  "difficulty": "Medium",5  "link": "https://leetcode.com/problems/minimum-operations-to-halve-array-sum/",6  "date": "2022-03-05",7  "task_description": "You are given an array `nums` of positive integers. In one operation, you can choose **any** number from `nums` and reduce it to **exactly** half the number. (Note that you may choose this reduced number in future operations.) Return_ the **minimum** number of operations to reduce the sum of _`nums`_ by **at least** half._ **Example 1:** ``` **Input:** nums = [5,19,8,1] **Output:** 3 **Explanation:** The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33. The following is one of the ways to reduce the sum by at least half: Pick the number 19 and reduce it to 9.5. Pick the number 9.5 and reduce it to 4.75. Pick the number 8 and reduce it to 4. The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75. The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 >= 33/2 = 16.5. Overall, 3 operations were used so we return 3. It can be shown that we cannot reduce the sum by at least half in less than 3 operations. ``` **Example 2:** ``` **Input:** nums = [3,8,20] **Output:** 3 **Explanation:** The initial sum of nums is equal to 3 + 8 + 20 = 31. The following is one of the ways to reduce the sum by at least half: Pick the number 20 and reduce it to 10. Pick the number 10 and reduce it to 5. Pick the number 3 and reduce it to 1.5. The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5. The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 >= 31/2 = 15.5. Overall, 3 operations were used so we return 3. It can be shown that we cannot reduce the sum by at least half in less than 3 operations. ``` **Constraints:** `1 <= nums.length <= 105` `1 <= nums[i] <= 107`",8  "test_case": [9    {10      "label": "Example 1",11      "input": "nums = [5,19,8,1]",12      "output": "3 "13    },14    {15      "label": "Example 2",16      "input": "nums = [3,8,20]",17      "output": "3 "18    }19  ],20  "constraints": [21    "1 <= nums.length <= 105",22    "1 <= nums[i] <= 107"23  ],24  "python_template": "class Solution(object):\n    def halveArray(self, nums):\n        \"\"\"\n        :type nums: List[int]\n        :rtype: int\n        \"\"\"\n        ",25  "java_template": "class Solution {\n    public int halveArray(int[] nums) {\n        \n    }\n}",26  "metadata": {27    "func_name": "halveArray"28  }29}