Minimize Rounding Error to Meet Target Problem
Description
LeetCode Problem 1058.
Given an array of prices [p_1,p_2...,p_n] and a target, round each price p_i to Round_i(p_i) so that the rounded array [Round_1(p_1),Round_2(p_2)...,Round_n(p_n)] sums to the given target. Each operation Round_i(p_i) could be either Floor(p_i) or Ceil(p_i).
Return the string "-1" if the rounded array is impossible to sum to target. Otherwise, return the smallest rounding error, which is defined as Σ |Round_i(p_i) - (p_i)| for i from 1 to n, as a string with three places after the decimal.
Example 1:
Input: prices = ["0.700","2.800","4.900"], target = 8
Output: "1.000"
Explanation:
Use Floor, Ceil and Ceil operations to get (0.7 - 0) + (3 - 2.8) + (5 - 4.9) = 0.7 + 0.2 + 0.1 = 1.0 .
Example 2:
Input: prices = ["1.500","2.500","3.500"], target = 10
Output: "-1"
Explanation: It is impossible to meet the target.
Example 3:
Input: prices = ["1.500","2.500","3.500"], target = 9
Output: "1.500"
Constraints:
1 <= prices.length <= 500- Each string
prices[i]represents a real number in the range[0.0, 1000.0]and has exactly 3 decimal places. 0 <= target <= 10^6
Solution
Approach: Greedy on the Fractional Parts
Start by rounding every price down. That gives the smallest sum we can reach, floorSum. Rounding a price up adds exactly 1 to the sum, but only if the price has a fractional part; a whole number like 3.000 rounds to itself either way. So if k prices have a non-zero fractional part, the reachable sums are floorSum, floorSum + 1, ..., floorSum + k. If target is outside that range, the answer is "-1".
Otherwise exactly ups = target - floorSum of those prices must round up. A price with fractional part f costs f when rounded down and 1 - f when rounded up, so rounding it up changes the error by 1 - 2f. That is smallest for large f, so we round up the ups prices with the largest fractional parts and round the rest down.
Floating-point numbers would make the last digit risky, so the code works in thousandths: every price has exactly three decimals, so removing the dot gives an exact integer ("2.800" is 2800, floor 2, fraction 800). The total error is then an integer number of thousandths, printed as error / 1000 and three digits of error % 1000.
Complexity: O(n log n) time for sorting the fractional parts and O(n) extra space.
class Solution:
def minimizeError(self, prices: List[str], target: int) -> str:
# Work in thousandths: every price has exactly three decimals, so this is exact.
floor_sum = 0
fractions = [] # fractional parts that are not zero: only these prices can round up
for p in prices:
v = int(p.replace(".", ""))
floor_sum += v // 1000
if v % 1000:
fractions.append(v % 1000)
ups = target - floor_sum # how many prices must round up
if ups < 0 or ups > len(fractions):
return "-1"
# Rounding up costs 1000 - f and rounding down costs f, so round up the largest fractions.
fractions.sort(reverse=True)
error = sum(1000 - f for f in fractions[:ups]) + sum(fractions[ups:])
return "%d.%03d" % (error // 1000, error % 1000)