-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.RoundNumber.cpp
48 lines (39 loc) · 950 Bytes
/
02.RoundNumber.cpp
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
/*
When you book on airbnb the total price is:
Total price = base price + service fee + cleaning fee + …
input : array of decimals ~ X
output : array of int ~ Y
But they need to satisfy the condition:
sum(Y) = round(sum(x))
minmize (|y1-x1| + |y2-x2| + ... + |yn-xn|)
Example1:
input = 30.3, 2.4, 3.5
output = 30 2 4
Example2:
input = 30.9, 2.4, 3.9
output = 31 2 4
*/
#include "LeetcodeTools.h"
using namespace leetcode;
class Solution{
public:
vector<int> roundNumbers(vector<double>& X ) {
vector<int> result;
for(int i = 0; i < X.size(); i++) {
int lb = (int)X[i];
int ub = lb + 1;
if(X[i] - lb < ub - X[i]) {
result.push_back(lb);
}
else {
result.push_back(ub);
}
}
return result;
}
};
int main() {
vector<double> X{30.9, 2.4, 3.9};
Solution sol;
cout << sol.roundNumbers(X);
}