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.
Input: prices =["0.700","2.800","4.900"], target =8Output: "1.000"Explanation:
Use floor for0.700 and ceil for2.800 and 4.900.The rounded prices are [0,3,5], and the rounding error is|0.700-0|+|2.800-3|+|4.900-5|=0.700+0.200+0.100=1.000
For each price, you can round down (floor) or up (ceil). To minimize the rounding error, round down as much as possible, and only round up for the minimum number of prices needed to reach the target. The prices with the smallest difference between ceil and the original value should be chosen for rounding up.
classSolution {
public: string minimizeError(vector<string>& prices, int target) {
vector<double> errors;
int floor_sum =0, ceil_sum =0;
for (auto& p : prices) {
double v = stod(p);
int f = floor(v), c = ceil(v);
floor_sum += f;
ceil_sum += c;
if (f != c) errors.push_back(v - f);
}
int up = target - floor_sum;
if (up <0|| up > errors.size()) return"-1";
sort(errors.begin(), errors.end());
double ans =0;
for (int i =0; i < errors.size(); ++i) {
if (i < up) ans +=1- errors[i];
else ans += errors[i];
}
char buf[16];
sprintf(buf, "%.3f", ans);
returnstring(buf);
}
};
classSolution {
public String minimizeError(List<String> prices, int target) {
List<Double> errors =new ArrayList<>();
int floorSum = 0, ceilSum = 0;
for (String p : prices) {
double v = Double.parseDouble(p);
int f = (int)Math.floor(v), c = (int)Math.ceil(v);
floorSum += f;
ceilSum += c;
if (f != c) errors.add(v - f);
}
int up = target - floorSum;
if (up < 0 || up > errors.size()) return"-1";
Collections.sort(errors);
double ans = 0;
for (int i = 0; i < errors.size(); ++i) {
if (i < up) ans += 1 - errors.get(i);
else ans += errors.get(i);
}
return String.format("%.3f", ans);
}
}
classSolution {
funminimizeError(prices: List<String>, target: Int): String {
val errors = mutableListOf<Double>()
var floorSum = 0var ceilSum = 0for (p in prices) {
val v = p.toDouble()
val f = v.toInt()
val c = Math.ceil(v).toInt()
floorSum += f
ceilSum += c
if (f != c) errors.add(v - f)
}
val up = target - floorSum
if (up < 0|| up > errors.size) return"-1" errors.sort()
var ans = 0.0for (i in errors.indices) {
ans +=if (i < up) 1 - errors[i] else errors[i]
}
return"%.3f".format(ans)
}
}
defminimize_error(prices: list[str], target: int) -> str:
errors: list[float] = []
floor_sum =0 ceil_sum =0for p in prices:
v = float(p)
f = int(v)
c = int(-(-v //1))
floor_sum += f
ceil_sum += c
if f != c:
errors.append(v - f)
up = target - floor_sum
if up <0or up > len(errors):
return"-1" errors.sort()
ans =0.0for i, e in enumerate(errors):
if i < up:
ans +=1- e
else:
ans += e
returnf"{ans:.3f}"
impl Solution {
pubfnminimize_error(prices: Vec<String>, target: i32) -> String {
letmut errors =vec![];
letmut floor_sum =0;
letmut ceil_sum =0;
for p in prices.iter() {
let v: f64= p.parse().unwrap();
let f = v.floor() asi32;
let c = v.ceil() asi32;
floor_sum += f;
ceil_sum += c;
if f != c {
errors.push(v - f asf64);
}
}
let up = target - floor_sum;
if up <0|| up asusize> errors.len() {
return"-1".to_string();
}
errors.sort_by(|a, b| a.partial_cmp(b).unwrap());
letmut ans =0.0;
for (i, e) in errors.iter().enumerate() {
if i < up asusize {
ans +=1.0- e;
} else {
ans +=*e;
}
}
format!("{:.3}", ans)
}
}