{"id":8269,"date":"2021-03-22T09:29:53","date_gmt":"2021-03-22T16:29:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8269"},"modified":"2021-03-22T09:30:52","modified_gmt":"2021-03-22T16:30:52","slug":"leetcode-1802-maximum-value-at-a-given-index-in-a-bounded-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1802-maximum-value-at-a-given-index-in-a-bounded-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1802. Maximum Value at a Given Index in a Bounded Array"},"content":{"rendered":"\n<p>You are given three positive integers&nbsp;<code>n<\/code>,&nbsp;<code>index<\/code>&nbsp;and&nbsp;<code>maxSum<\/code>. You want to construct an array&nbsp;<code>nums<\/code>&nbsp;<strong>(0-indexed)&nbsp;<\/strong>that satisfies the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums.length == n<\/code><\/li><li><code>nums[i]<\/code>&nbsp;is a&nbsp;<strong>positive<\/strong>&nbsp;integer where&nbsp;<code>0 &lt;= i &lt; n<\/code>.<\/li><li><code>abs(nums[i] - nums[i+1]) &lt;= 1<\/code>&nbsp;where&nbsp;<code>0 &lt;= i &lt; n-1<\/code>.<\/li><li>The sum of all the elements of&nbsp;<code>nums<\/code>&nbsp;does not exceed&nbsp;<code>maxSum<\/code>.<\/li><li><code>nums[index]<\/code>&nbsp;is&nbsp;<strong>maximized<\/strong>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<code>nums[index]<\/code>&nbsp;of the constructed array.<\/p>\n\n\n\n<p>Note that&nbsp;<code>abs(x)<\/code>&nbsp;equals&nbsp;<code>x<\/code>&nbsp;if&nbsp;<code>x &gt;= 0<\/code>, and&nbsp;<code>-x<\/code>&nbsp;otherwise.<\/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> n = 4, index = 2,  maxSum = 6\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The arrays [1,1,<strong>2<\/strong>,1] and [1,2,<strong>2<\/strong>,1] satisfy all the conditions. There are no other valid arrays with a larger value at the given index.\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> n = 6, index = 1,  maxSum = 10\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= maxSum &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>0 &lt;= index &lt; n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>To maximize nums[index], we can construct an array like this:<br>[1, 1, 1, &#8230;, 1, 2, 3, &#8230;, k &#8211; 1, k, k &#8211; 1, &#8230;,3, 2, 1, &#8230;., 1, 1, 1]<\/p>\n\n\n\n<p>Time complexity: O(logn)<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 maxValue(int n, int index, int maxSum) {\n    int l = 1;\n    int r = maxSum + 1;\n    while (l < r) {\n      int64_t m = l + (r - l) \/ 2;\n      int64_t t1 = m - index;\n      int64_t t2 = m - (n - 1 - index);\n      int64_t s1 = (t1 + m) * (index + 1) \/ 2;\n      int64_t s2 = (m + t2) * (n - index) \/ 2;\n      if (t1 <= 0) s1 = (1 + m) * m \/ 2 + (index - m + 1);\n      if (t2 <= 0) s2 = (1 + m) * m \/ 2 + (n - index - m);\n      if (s1 + s2 - m > maxSum)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given three positive integers&nbsp;n,&nbsp;index&nbsp;and&nbsp;maxSum. You want to construct an array&nbsp;nums&nbsp;(0-indexed)&nbsp;that satisfies the following conditions: nums.length == n nums[i]&nbsp;is a&nbsp;positive&nbsp;integer where&nbsp;0 &lt;= i &lt;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,88,31,177],"class_list":["post-8269","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-greedy","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8269","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=8269"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8269\/revisions"}],"predecessor-version":[{"id":8273,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8269\/revisions\/8273"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}