Skip to main content
LeetCode 1475, Easy. Topics: Array, Stack, Monotonic Stack. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

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

Constraints

  • 1 <= prices.length <= 500
  • 1 <= prices[i] <= 1000

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026