1058. Minimize Rounding Error to Meet Target
https://leetcode.com/problems/minimize-rounding-error-to-meet-target
Description
Given an array of prices
[p1,p2...,pn]
and a target
, round each price pi
to Roundi(pi)
so that the rounded array [Round1(p1),Round2(p2)...,Roundn(pn)]
sums to the given target
. Each operation Roundi(pi)
could be either Floor(pi)
or Ceil(pi)
.
Return the string "-1"
if the rounded array is impossible to sum to target
. Otherwise, return the smallest rounding error, which is defined as Σ |Roundi(pi) - (pi)|
for i
from 1
to n
, as a string with three places after the decimal.
Example 1:
Example 2:
Example 3:
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 <= 106
ac
Last updated