Final Prices With a Special Discount in a Shop - Problem

You're shopping at a special store where each item has a smart discount system! The store offers an innovative discount policy: when you buy any item, you'll receive a discount equal to the price of the next cheaper or equally-priced item that appears later in the store.

Given an integer array prices where prices[i] represents the price of the i-th item, your task is to calculate the final discounted price for each item.

Discount Rules:

  • For item at position i, find the first item at position j where j > i and prices[j] ≤ prices[i]
  • The discount amount equals prices[j]
  • If no such item exists, no discount is applied

Return an array where each element represents the final price after applying the discount (original price minus discount).

Example: If prices = [8, 4, 6, 2, 3], then:
• Item $8 gets $4 discount (next cheaper item) → Final: $4
• Item $4 gets $2 discount → Final: $2
• Item $6 gets $2 discount → Final: $4
• Item $2 gets no discount → Final: $2
• Item $3 gets no discount → Final: $3

Input & Output

example_1.py — Basic Case
$ Input: [8,4,6,2,3]
Output: [4,2,4,2,3]
💡 Note: Item $8 gets $4 discount (next cheaper), item $4 gets $2 discount, item $6 gets $2 discount, items $2 and $3 get no discount as no cheaper items follow them.
example_2.py — No Discounts
$ Input: [1,2,3,4,5]
Output: [1,2,3,4,5]
💡 Note: Prices are in increasing order, so no item has a cheaper item after it. All items keep their original prices.
example_3.py — All Discounts
$ Input: [10,1,1,6]
Output: [9,1,1,6]
💡 Note: Only the first item $10 gets a discount of $1 from the next item. The remaining items don't have cheaper items following them.

Visualization

Tap to expand
STORE 1$8STORE 2$4STORE 3$6STORE 4$2STORE 5$3$4 discount$2 discountSmart Discount Tracker (Stack)Remembers expensive stores waiting for discountsAutomatically applies discounts when cheaper stores are found🏪 Smart Shopping Mall - Automatic Discount System
Understanding the Visualization
1
Enter the mall
Start with a special tracker (stack) that remembers expensive stores you've visited
2
Visit each store
At each store, check if it can provide discounts to previously visited expensive stores
3
Apply discounts
When you find a cheaper store, it provides discounts to all applicable expensive stores in your tracker
4
Update tracker
Add current store to your tracker for future discount opportunities
Key Takeaway
🎯 Key Insight: The monotonic stack efficiently maintains a decreasing sequence of prices, allowing us to instantly identify when a cheaper item can provide discounts to multiple previous expensive items in O(1) time per discount application.

Time & Space Complexity

Time Complexity
⏱️
O(n²)

For each of the n items, we potentially scan through all remaining n-1 items in the worst case

n
2n
Quadratic Growth
Space Complexity
O(1)

Only using constant extra space besides the input and output arrays

n
2n
Linear Space

Constraints

  • 1 ≤ prices.length ≤ 500
  • 1 ≤ prices[i] ≤ 103
  • All prices are positive integers
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.5K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen