Title: "Array Reduction Cost Calculation"
Problem Description
Given an array arr
of n
integers, a sequence of n-1
operations must be performed on the array.
Operation Details
- Remove the minimum and maximum elements from the current array and add their sum back to the array.
- The cost of an operation is calculated as:
cost = ceil((minimum_element + maximum_element) / (maximum_element - minimum_element + 1))
whereceil
represents the ceiling function (round up to the nearest integer).
Goal
Find the total cost to reduce the array to a single element.
Example
Given arr = [2, 3, 4, 5, 7]
:
The possible sequence of operations is:
- Choose 2 and 7:
cost = ceil((2 + 7) / (7 - 2 + 1)) = ceil(9 / 6) = 2
Remove 2 and 7, append 9.
Updatedarr = [3, 4, 5, 9]
.total_cost = 2
. - Choose 3 and 9:
cost = ceil((3 + 9) / (9 - 3 + 1)) = ceil(12 / 7) = 2
Remove 3 and 9, append 12.
Updatedarr = [4, 5, 12]
.total_cost = 2 + 2 = 4
. - Choose 4 and 12:
cost = ceil((4 + 12) / (12 - 4 + 1)) = ceil(16 / 9) = 2
Remove 4 and 12, append 16.
Updatedarr = [5, 16]
.total_cost = 4 + 2 = 6
. - Choose 5 and 16:
cost = ceil((5 + 16) / (16 - 5 + 1)) = ceil(21 / 12) = 2
Remove 5 and 16, append 21.
Updatedarr = [21]
.total_cost = 6 + 2 = 8
.
Output
Return the total cost: 8
.
Function Description
Complete the function findTotalCost
in the editor below.
findTotalCost
has the following parameter(s):
- arr (list[int]): An array of integers.
Returns:
- int: The total cost to reduce the array to a single element.
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.