Skip to content

1475. Final Prices With a Special Discount in a Shop #969

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to apply a special discount based on the condition that there is a later item with a price less than or equal to the current price, we can use a brute-force approach. We'll iterate over the prices array and, for each item, look for the first item with a lower or equal price after it. This can be achieved with nested loops. We can utilize a stack to efficiently keep track of the items' prices and apply the special discount.

Approach:

  1. Stack Approach:

    • We can iterate over the prices array from left to right. For each item, we'll use the stack to keep track of prices that haven't yet found a discount.
    • For each price, we'll check if it is smaller than or equal to the price at the top…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Dec 18, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Dec 18, 2024
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants