Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int calculate_amount(int[] prices){
- int totalAmount = 0;
- //empty array, nothing to buy
- if(prices.length < 1){
- return totalAmount;
- }
- int minPrice = prices[0];
- totalAmount += prices[0];
- for (int i = 1; i < prices.length; i++) {
- //get the value of the item with the discount
- int currentPrice = prices[i] - minPrice;
- //if the min price is bigger than current price, get the item for free
- if(currentPrice < 0){
- currentPrice = 0;
- }
- //ad it to the total amount
- totalAmount += currentPrice;
- // get current price if it is lower than previous min
- minPrice = minPrice > prices[i] ? prices[i] : minPrice;
- }
- return totalAmount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement