{"id":8245,"date":"2021-03-18T19:13:15","date_gmt":"2021-03-19T02:13:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8245"},"modified":"2021-03-18T19:13:37","modified_gmt":"2021-03-19T02:13:37","slug":"leetcode-1793-maximum-score-of-a-good-subarray","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-1793-maximum-score-of-a-good-subarray\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1793. Maximum Score of a Good Subarray"},"content":{"rendered":"\n<p>You are given an array of integers&nbsp;<code>nums<\/code>&nbsp;<strong>(0-indexed)<\/strong>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>score<\/strong>&nbsp;of a subarray&nbsp;<code>(i, j)<\/code>&nbsp;is defined as&nbsp;<code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)<\/code>. A&nbsp;<strong>good<\/strong>&nbsp;subarray is a subarray where&nbsp;<code>i &lt;= k &lt;= j<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the maximum possible&nbsp;<strong>score<\/strong>&nbsp;of a&nbsp;<strong>good<\/strong>&nbsp;subarray.<\/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> nums = [1,4,3,7,4,5], k = 3\n<strong>Output:<\/strong> 15\n<strong>Explanation:<\/strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. \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> nums = [5,5,4,5,4,1,1,1], k = 0\n<strong>Output:<\/strong> 20\n<strong>Explanation:<\/strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= k &lt; nums.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solutions: Two Pointers<\/strong><\/h2>\n\n\n\n<p>maintain a window [i, j],  m = min(nums[i~j]), expend to the left if nums[i &#8211; 1] &gt;= nums[j + 1], otherwise expend to the right. <\/p>\n\n\n\n<p>Time complexity: O(n)<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  int maximumScore(vector<int>& nums, int k) {\n    const int n = nums.size();    \n    int ans = 0;\n    for (int i = k, j = k, m = nums[k];;) {\n      ans = max(ans, m * (j - i + 1));      \n      if (j - i + 1 == n) break;\n      int l = i ? nums[i - 1] : -1;\n      int r = j + 1 < n ? nums[j + 1] : -1;\n      if (l >= r)\n        m = min(m, nums[--i]);\n      else\n        m = min(m, nums[++j]);      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of integers&nbsp;nums&nbsp;(0-indexed)&nbsp;and an integer&nbsp;k. The&nbsp;score&nbsp;of a subarray&nbsp;(i, j)&nbsp;is defined as&nbsp;min(nums[i], nums[i+1], &#8230;, nums[j]) * (j &#8211; i + 1). A&nbsp;good&nbsp;subarray&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[88,217,175],"class_list":["post-8245","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-greedy","tag-hard","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8245","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=8245"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8245\/revisions"}],"predecessor-version":[{"id":8247,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8245\/revisions\/8247"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}