1475. Final Prices With a Special Discount in a Shop Easy

@problem@discussion
#Array#Stack#Monotonic Stack



1/**
2 * [1475] Final Prices With a Special Discount in a Shop
3 *
4 * Given the 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.
5 * Return an array where the ith element is the final price you will pay for the ith item of the shop considering the special discount.
6 *  
7 * Example 1:
8 * 
9 * Input: prices = [8,4,6,2,3]
10 * Output: [4,2,4,2,3]
11 * Explanation: 
12 * 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. 
13 * 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. 
14 * 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. 
15 * For items 3 and 4 you will not receive any discount at all.
16 * 
17 * Example 2:
18 * 
19 * Input: prices = [1,2,3,4,5]
20 * Output: [1,2,3,4,5]
21 * Explanation: In this case, for all items, you will not receive any discount at all.
22 * 
23 * Example 3:
24 * 
25 * Input: prices = [10,1,1,6]
26 * Output: [9,0,1,6]
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= prices.length <= 500
32 * 	1 <= prices[i] <= 10^3
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/
38// discuss: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn final_prices(prices: Vec<i32>) -> Vec<i32> {
44        vec![]
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_1475() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.