LintCode 1852: Final Discounted Price (單調棧經典題)

1852. Final Discounted Price

A shopkeeper has a sale to complete and has arranged the items being sold in an array. Starting from the left, the shopkeeper rings up each item at its full price less the price of the first lower or equal priced item to its right. If there is no item to the right that costs less than or equal to the current item's price, the current item is sold at full price.You should return the actual selling price of each item.

Example

Example 1:
Input:
Prices = [2, 3, 1, 2, 4, 2]
Output: [1, 2, 1, 0, 2, 2]
Explanation: The item 0 and 1 are each discounted by 1 unit, The item 3 at 2 units, is discounted 2 units, as would the item 4 at 4 units. 
Example 2:
Input:
Prices = [1, 2, 3, 4, 5]
Output: [1, 2, 3, 4, 5]
Explanation: each item should keep full price beacause there are not equal or lower priced items to the right

Notice

  • The length of Prices is within range: [1, 100000]
  • Prices[i] is within range: [1, 1000000]

Input test data (one parameter per line)How to understand a testcase?

解法1:單調棧。
1)注意是單調遞增棧(即從棧底到棧底單調不遞減)
2)stack裏面存儲的是index,不是實際值。

以input={2,3,1,2,4,2}爲例,result一開始初始化爲input的值{2,3,1,2,4,2}。
i=0, stack={2}   //start from bottom
i=1, stack={2,3}
i=2, stack={1} //因爲1<3, 1<2, 所以3和2都pop,result[0]和result[1]對應的值(2和3)都減去result[2](=1)。
i=3, stack={1,2}
i=4, stack={1,2,4}
i=5, stack={1,2} //因爲2<4, 2<=2, 所以4 和 2 都pop, result[3]和result[4]對應的值(2和4)減去result[5](=2)。

所以最後result={1,2,1,0,2,2}。
總結:
求最右邊第一個大於等於某元素的值用單調遞減棧。
求最右邊第一個小於等於某元素的值用單調遞增棧。

class Solution {
public:
    /**
     * @param prices: a list of integer
     * @return: return the actual prices
     */
    vector<int> FinalDiscountedPrice(vector<int> &prices) {
        int n = prices.size();
        vector<int> result(n);
        
        stack<int> s;
        for(int i = 0; i < n; ++i) result[i] = prices[i];
        
        for (int i = 0; i < n; ++i) {
            while(!s.empty() && prices[i] <= prices[s.top()]) {
                int top_index = s.top();
                s.pop();
                result[top_index] = prices[top_index] - prices[i];
            }
            
            s.push(i);
        }
        
        return result;
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章