2023-12-09 18:42:21 +08:00
< p > You are given an integer array < code > prices< / code > where < code > prices[i]< / code > is the price of the < code > i< sup > th< / sup > < / code > item in a shop.< / p >
2022-03-27 18:27:43 +08:00
2023-12-09 18:42:21 +08:00
< p > There is a special discount for items in the shop. If you buy the < code > i< sup > th< / sup > < / code > item, then you will receive a discount equivalent to < code > prices[j]< / code > where < code > j< / code > is the minimum index such that < code > j > i< / code > and < code > prices[j] < = prices[i]< / code > . Otherwise, you will not receive any discount at all.< / p >
< p > Return an integer array < code > answer< / code > where < code > answer[i]< / code > is the final price you will pay for the < code > i< sup > th< / sup > < / code > item of the shop, considering the special discount.< / p >
2022-03-27 18:27:43 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 18:27:43 +08:00
< pre >
< strong > Input:< / strong > prices = [8,4,6,2,3]
< strong > Output:< / strong > [4,2,4,2,3]
2023-12-09 18:42:21 +08:00
< strong > Explanation:< / strong >
For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.
For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.
For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.
2022-03-27 18:27:43 +08:00
For items 3 and 4 you will not receive any discount at all.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 18:27:43 +08:00
< pre >
< strong > Input:< / strong > prices = [1,2,3,4,5]
< strong > Output:< / strong > [1,2,3,4,5]
< strong > Explanation:< / strong > In this case, for all items, you will not receive any discount at all.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 18:27:43 +08:00
< pre >
< strong > Input:< / strong > prices = [10,1,1,6]
< strong > Output:< / strong > [9,0,1,6]
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = prices.length < = 500< / code > < / li >
2023-12-09 18:42:21 +08:00
< li > < code > 1 < = prices[i] < = 1000< / code > < / li >
2022-03-27 18:27:43 +08:00
< / ul >