{"id":4537,"date":"2018-12-25T23:30:42","date_gmt":"2018-12-26T07:30:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4537"},"modified":"2018-12-25T23:37:56","modified_gmt":"2018-12-26T07:37:56","slug":"leetcode-962-maximum-width-ramp","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-962-maximum-width-ramp\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 962. Maximum Width Ramp"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>A<\/code>&nbsp;of integers, a&nbsp;<em>ramp<\/em>&nbsp;is a tuple&nbsp;<code>(i, j)<\/code>&nbsp;for which&nbsp;<code>i &lt; j<\/code>&nbsp;and&nbsp;<code>A[i] &lt;= A[j]<\/code>.&nbsp; The width of such a&nbsp;ramp is&nbsp;<code>j - i<\/code>.<\/p>\n\n\n\n<p>Find the maximum width of a ramp in&nbsp;<code>A<\/code>.&nbsp; If one doesn&#8217;t exist, return 0.<\/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>[6,0,8,2,1,5] <br><strong>Output: <\/strong>4 <br><strong>Explanation: <\/strong> The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5. <\/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>[9,8,1,0,1,9,4,0,4,1] <br><strong>Output: <\/strong>7 <br><strong>Explanation: <\/strong> The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1. <\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>2 &lt;= A.length &lt;= 50000<\/code><\/li><li><code>0 &lt;= A[i] &lt;= 50000<\/code><\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Solution:\u00a0Stack<\/strong><\/h1>\n\n\n\n<ol class=\"wp-block-list\"><li>Using a stack to store start candidates&#8217; (decreasing order) index<\/li><li>Scan from right to left, compare the current number with the one on the top of the stack, pop if greater.<\/li><\/ol>\n\n\n\n<p>e.g. <br>A = [<strong>6<\/strong>,<strong>0<\/strong>,8,2,1,5]<br>stack = [0, 1] => [6, 0] <br>cur: A[5] = 5, stack.top = A[1] = 0, ramp = 5, stack.pop()<br>cur: A[4] = 1, stack.top = A[0] = 6<br>cur: A[3] = 2, stack.top = A[0] = 6<br>cur: A[2] = 8,  stack.top = A[0] = 6, ramp = 2, stack.pop()<br>stack.isEmpty() => END<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 44 ms\nclass Solution {\npublic:\n  int maxWidthRamp(vector<int>& A) {\n    stack<int> s;\n    for (int i = 0; i < A.size(); ++i)\n      if (s.empty() || A[i] < A[s.top()])\n        s.push(i);\n    int ans = 0;\n    for (int i = A.size() - 1; i >= 0; --i)\n      while (!s.empty() && A[i] >= A[s.top()]) {\n        ans = max(ans, i - s.top());\n        s.pop();\n      }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n# Author: Huahua, running time: 92 ms\nclass Solution:\n  def maxWidthRamp(self, A):\n    s = []\n    for i in range(len(A)):\n      if not s or A[i] < A[s[-1]]: s.append(i)\n    ans = 0\n    for i in range(len(A) - 1, -1, -1):\n      while s and A[i] >= A[s[-1]]:\n        ans = max(ans, i - s.pop())        \n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;A&nbsp;of integers, a&nbsp;ramp&nbsp;is a tuple&nbsp;(i, j)&nbsp;for which&nbsp;i &lt; j&nbsp;and&nbsp;A[i] &lt;= A[j].&nbsp; The width of such a&nbsp;ramp is&nbsp;j &#8211; i. Find the maximum width&#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":[413,52,447,177,180],"class_list":["post-4537","post","type-post","status-publish","format-standard","hentry","category-stack","tag-all-pairs","tag-binary-search","tag-decreasing","tag-medium","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4537","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=4537"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4537\/revisions"}],"predecessor-version":[{"id":4543,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4537\/revisions\/4543"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}