-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1058-minimize-rounding-error-to-meet-target.js
More file actions
53 lines (44 loc) · 1.53 KB
/
1058-minimize-rounding-error-to-meet-target.js
File metadata and controls
53 lines (44 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* 1058. Minimize Rounding Error to Meet Target
* https://leetcode.com/problems/minimize-rounding-error-to-meet-target/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {string[]} prices
* @param {number} target
* @return {string}
*/
var minimizeError = function(prices, target) {
const nums = prices.map(p => parseFloat(p));
const floors = nums.map(n => Math.floor(n));
const ceils = nums.map(n => Math.ceil(n));
const minSum = floors.reduce((sum, f) => sum + f, 0);
const maxSum = ceils.reduce((sum, c) => sum + c, 0);
if (target < minSum || target > maxSum) {
return '-1';
}
const ceilsNeeded = target - minSum;
const items = nums.map((n, i) => ({
floorError: n - floors[i],
ceilError: ceils[i] - n,
diff: (ceils[i] - n) - (n - floors[i])
}));
items.sort((a, b) => a.diff - b.diff);
let totalError = 0;
for (let i = 0; i < nums.length; i++) {
if (i < ceilsNeeded) {
totalError += items[i].ceilError;
} else {
totalError += items[i].floorError;
}
}
return totalError.toFixed(3);
};