{"id":6901,"date":"2020-06-13T10:04:18","date_gmt":"2020-06-13T17:04:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6901"},"modified":"2020-06-13T15:06:47","modified_gmt":"2020-06-13T22:06:47","slug":"leetcode-1475-final-prices-with-a-special-discount-in-a-shop","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-1475-final-prices-with-a-special-discount-in-a-shop\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1475. Final Prices With a Special Discount in a Shop"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode1475. Final Prices With a Special Discount in a Shop  - \u5237\u9898\u627e\u5de5\u4f5c EP334\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/X98Yc4YtgyA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given the array&nbsp;<code>prices<\/code>&nbsp;where&nbsp;<code>prices[i]<\/code>&nbsp;is the price of the&nbsp;<code>ith<\/code>&nbsp;item in a shop. There is a special discount for items in the shop, if you buy the&nbsp;<code>ith<\/code>&nbsp;item, then you will receive a discount equivalent to&nbsp;<code>prices[j]<\/code>&nbsp;where&nbsp;<code>j<\/code>&nbsp;is the&nbsp;<strong>minimum<\/strong>&nbsp;index such that&nbsp;<code>j &gt; i<\/code>&nbsp;and&nbsp;<code>prices[j] &lt;= prices[i]<\/code>, otherwise, you will not receive any discount at all.<\/p>\n\n\n\n<p><em>Return an array where the&nbsp;<code>ith<\/code>&nbsp;element is the final price you will pay for the&nbsp;<code>ith<\/code>&nbsp;item of the shop considering the special discount.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> prices = [8,4,6,2,3]\n<strong>Output:<\/strong> [4,2,4,2,3]\n<strong>Explanation:<\/strong>&nbsp;\nFor 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.&nbsp;\nFor 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.&nbsp;\nFor 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.&nbsp;\nFor items 3 and 4 you will not receive any discount at all.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> prices = [1,2,3,4,5]\n<strong>Output:<\/strong> [1,2,3,4,5]\n<strong>Explanation:<\/strong> In this case, for all items, you will not receive any discount at all.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> prices = [10,1,1,6]\n<strong>Output:<\/strong> [9,0,1,6]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= prices.length &lt;= 500<\/code><\/li><li><code>1 &lt;= prices[i] &lt;= 10^3<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> finalPrices(vector<int> prices) {\n    const int n = prices.size();\n    for (int i = 0; i < n; ++i)      \n      for (int j = i + 1; j < n; ++j)\n        if (prices[j] <= prices[i]) {\n          prices[i] -= prices[j];\n          break;\n        }     \n    return prices;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Monotonic Stack<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1475-ep334.png\" alt=\"\" class=\"wp-image-6907\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1475-ep334.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1475-ep334-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/06\/1475-ep334-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Use a stack to store monotonically increasing items, when the current item is cheaper than the top of the stack, we get the discount and pop that item. Repeat until the current item is no longer cheaper or the stack becomes empty.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> finalPrices(vector<int> prices) {\n    \/\/ stores pointers of monotonically incraseing elements.\n    stack<int*> s; \n    for (int& p : prices) {\n      while (!s.empty() && *s.top() >= p) {\n        *s.top() -= p;\n        s.pop();\n      }\n      s.push(&p);\n    }      \n    return prices;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>index version<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> finalPrices(vector<int> prices) {\n    \/\/ stores indices of monotonically incraseing elements.\n    stack<int> s; \n    for (int i = 0; i < prices.size(); ++i) {\n      while (!s.empty() &#038;&#038; prices[s.top()] >= prices[i]) {\n        prices[s.top()] -= prices[i];\n        s.pop();\n      }\n      s.push(i);\n    }      \n    return prices;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the array&nbsp;prices&nbsp;where&nbsp;prices[i]&nbsp;is the price of the&nbsp;ith&nbsp;item in a shop. There is a special discount for items in the shop, if you buy the&nbsp;ith&nbsp;item, then&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[20,222,552,179],"class_list":["post-6901","post","type-post","status-publish","format-standard","hentry","category-stack","tag-array","tag-easy","tag-monotonic-stack","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6901"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6901\/revisions"}],"predecessor-version":[{"id":6908,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6901\/revisions\/6908"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}