-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathFinalPricesWithASpecialDiscountInAShop.java
More file actions
59 lines (47 loc) · 1.7 KB
/
FinalPricesWithASpecialDiscountInAShop.java
File metadata and controls
59 lines (47 loc) · 1.7 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
54
55
56
57
58
59
//https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/
//Leetcode1475
/*
You are given an integer array prices where prices[i] is the price of the ith item in a shop.
There is a special discount for items in the shop. If you buy the ith item, then you will receive a discount
equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i]. Otherwise, you will
not receive any discount at all.
Return an integer array answer where answer[i] is the final price you will pay for the ith item of the shop,
considering the special discount.
*/
public class FinalPricesWithASpecialDiscountInAShop {
/*
Using inner loop to search next small number
Time complexity o(n^2)
Space complexity o(1)
*/
public int[] finalPrices(int[] prices) {
for(int i=0; i<prices.length-1; i++){
for(int j=i+1 ; j<prices.length; j++){
if(prices[j] <= prices[i]){
prices[i]-=prices[j];
break;
}
}
}
return prices;
}
/*
Stack to track next small number
Time complexity o(2n) -- o(n)
Space complexity o(n)
*/
public int[] finalPrices(int[] prices) {
Stack<Integer> stack = new Stack<>();
for(int i=prices.length-1; i >=0; i--){
while(!stack.isEmpty() && stack.peek() > prices[i])
stack.pop();
int num =prices[i];
if(stack.isEmpty())
prices[i] = num;
else
prices[i] = num-stack.peek();
stack.push(num);
}
return prices;
}
}