{"id":1933,"date":"2018-03-03T21:36:43","date_gmt":"2018-03-04T05:36:43","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1933"},"modified":"2018-03-09T20:40:56","modified_gmt":"2018-03-10T04:40:56","slug":"leetcode-795-number-of-subarrays-with-bounded-maximum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-795-number-of-subarrays-with-bounded-maximum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 795. Number of Subarrays with Bounded Maximum"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u95ee\u4e00\u4e2a\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u4e2a\u5b50\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\u503c\u5728[L, R]\u7684\u8303\u56f4\u91cc\u3002<\/p>\n<p>We are given an array\u00a0<code>A<\/code>\u00a0of positive integers, and two positive integers\u00a0<code>L<\/code>\u00a0and\u00a0<code>R<\/code>\u00a0(<code>L &lt;= R<\/code>).<\/p>\n<p>Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least\u00a0<code>L<\/code>\u00a0and at most\u00a0<code>R<\/code>.<\/p>\n<pre class=\"\">Example :\r\nInput: \r\nA = [2, 1, 4, 3]\r\nL = 2\r\nR = 3\r\nOutput: 3\r\nExplanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].<\/pre>\n<p>Solution 1:<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 42 ms\r\nclass Solution {\r\npublic:\r\n  int numSubarrayBoundedMax(vector&lt;int&gt;&amp; A, int L, int R) {\r\n    return count(A, R) - count(A, L - 1);\r\n  }\r\nprivate:\r\n  \/\/ Count # of sub arrays whose max element is &lt;= N\r\n  int count(const vector&lt;int&gt;&amp; A, int N) {\r\n    int ans = 0;\r\n    int cur = 0;\r\n    for (int a: A) {\r\n      if (a &lt;= N) \r\n        ans += ++cur;\r\n      else\r\n        cur = 0;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>Solution 2: One pass<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 38 ms\r\nclass Solution {\r\npublic:\r\n  int numSubarrayBoundedMax(vector&lt;int&gt;&amp; A, int L, int R) {\r\n    int ans = 0;\r\n    int left = -1;\r\n    int right = -1;\r\n    for (int i = 0; i &lt; A.size(); ++i) {\r\n      if (A[i] &gt;= L) right = i;\r\n      if (A[i] &gt; R) left = i;      \r\n      ans += (right - left);\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u95ee\u4e00\u4e2a\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u4e2a\u5b50\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\u503c\u5728[L, R]\u7684\u8303\u56f4\u91cc\u3002 We are given an array\u00a0A\u00a0of positive integers, and two positive integers\u00a0L\u00a0and\u00a0R\u00a0(L &lt;= R). Return the number of (contiguous, non-empty) subarrays such that the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[228,177,41],"class_list":["post-1933","post","type-post","status-publish","format-standard","hentry","category-array","tag-max-element","tag-medium","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1933","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=1933"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1933\/revisions"}],"predecessor-version":[{"id":2049,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1933\/revisions\/2049"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}