← Home

1475. Final Prices With a Special Discount in a Shop

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
stackC++Markdown
147

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1475. Final Prices With a Special Discount in a Shop, the solution in this repository is mainly a stack solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: stack.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are finalPrices, print_stack, stack_contents.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 12 ms, faster than 33.33% of C++ online submissions for Final Prices With a Special Discount in a Shop.
02//Memory Usage: 10 MB, less than 100.00% of C++ online submissions for Final Prices With a Special Discount in a Shop.
03class Solution {
04public:
05    vector<int> finalPrices(vector<int>& prices) {
06        int n = prices.size();
07        vector<int> ans(n);
08        
09        for(int i = 0; i < n; i++){
10            int j;
11            for(j = i+1; j < n; j++){
12                if(prices[j] <= prices[i]){
13                    break;
14                }
15            }
16            if(j < n && prices[j] <= prices[i]){
17                prices[i] -= prices[j];
18            }
19        }
20        
21        return prices;
22    }
23};
24
25//monotonic stack
26//https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/discuss/685390/JavaC%2B%2BPython-Stack-One-Pass
27//Runtime: 8 ms, faster than 100.00% of C++ online submissions for Final Prices With a Special Discount in a Shop.
28//Memory Usage: 9.9 MB, less than 100.00% of C++ online submissions for Final Prices With a Special Discount in a Shop.
29class Solution {
30public:
31    void print_stack(vector<int>& vec, stack<int>& stk){
32        if(stk.empty()) return;
33        int* end   = &stk.top() + 1;
34        int* begin = end - stk.size();
35        vector<int> stack_contents(begin, end);
36
37        for(int e : stack_contents){
38            cout << vec[e] << " ";
39        }
40        cout << endl;
41    }
42    vector<int> finalPrices(vector<int>& prices) {
43        int n = prices.size();
44        stack<int> stk;
45        
46        for(int i = 0; i < n; i++){
47            /*
48            prices[i] is the first element s.t. 
49            prices[i] <= previous pushed elements,
50            so here we discount prices[stk.top()] the amount of prices[i]
51            
52            the stack is always increasing(from bottom to top),
53            that's because when me meet an element smaller than stk.top(),
54            we will pop the stack until not
55            
56            since the stack is increasing,
57            so we can just stop when the top element < current element,
58            no need to check the lower elements in the stack
59            */
60            while(!stk.empty() && prices[stk.top()] >= prices[i]){
61                // cout << "pop: " << prices[stk.top()] << endl;
62                prices[stk.top()] -= prices[i];
63                stk.pop();
64                // print_stack(prices, stk);
65            }
66            /*
67            push current index into the stack,
68            it will be processed later
69            */
70            stk.push(i);
71            // cout << "push: " << prices[i] << endl;
72            // print_stack(prices, stk);
73        }
74        
75        return prices;
76    }
77};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.