> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Final Prices With a Special Discount in a Shop

> Tested Python solution for LeetCode 1475 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1475, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Stack](/catalog/topics/stack), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1475   # by problem number
lcpy gen -s final_prices_with_a_special_discount_in_a_shop   # by problem name
```

## Problem

Given an integer array `prices` where `prices[i]` is the price of the `i<sup>th</sup>` item in a shop.

There is a special discount for items in the shop. If you buy the `i<sup>th</sup>` 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 `i<sup>th</sup>` item of the shop, considering the special discount.

### Examples

```
Input: prices = [8,4,6,2,3]
Output: [4,2,4,2,3]
Explanation:
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.
For items 3 and 4 you will not receive any discount at all.
```

```
Input: prices = [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation: In this case, for all items, you will not receive any discount at all.
```

```
Input: prices = [10,1,1,6]
Output: [9,0,1,6]
```

### Constraints

* `1 <= prices.length <= 500`
* `1 <= prices[i] <= 1000`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/final_prices_with_a_special_discount_in_a_shop/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/final_prices_with_a_special_discount_in_a_shop/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) - each index is pushed and popped at most once
    # Space: O(n) - for the index stack
    def final_prices(self, prices: list[int]) -> list[int]:
        answer = list(prices)
        stack: list[int] = []
        for i, price in enumerate(prices):
            while stack and prices[stack[-1]] >= price:
                answer[stack.pop()] -= price
            stack.append(i)
        return answer
```

## Complexity

| Time | Space |
| - | - |
| O(n) - each index is pushed and popped at most once | O(n) - for the index stack |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.