forked from builder-of-web3/HackR_Java_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin amount
More file actions
36 lines (31 loc) · 984 Bytes
/
min amount
File metadata and controls
36 lines (31 loc) · 984 Bytes
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
class Result {
/*
* Complete the 'calculateAmount' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER_ARRAY prices as parameter.
*/
public static long calculateAmount(List<Integer> prices) {
// Write your code here
List<Integer> discount = new ArrayList();
long cost = 0L;
for (int i = 0 ; i < prices.size(); i++) {
Integer price = prices.get(i);
if (i == 0) {
discount.add(price);
cost = cost + price;
continue;
}
Integer min = Collections.min(discount);
if (prices.get(i) > min) {
cost = cost + (prices.get(i) - min);
discount.add(price);
}
if (prices.get(i) < min) {
cost = cost + 0;
discount.add(price);
}
}
return cost;
}
}