<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>precompute &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/precompute/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 29 Mar 2025 22:37:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>precompute &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 3242. Design Neighbor Sum Service</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-3242-design-neighbor-sum-service/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-3242-design-neighbor-sum-service/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Mar 2025 22:36:51 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[precompute]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10238</guid>

					<description><![CDATA[青铜 Brute Force：每次需要花费O(n*n)的时间去查找query所在的格子，求和是O(1)。总的时间复杂度达到O(n^4)，肯定会超时。 白银 Hashtable：由于所有元素的值的唯一的，我们可以使用hashtable（或者数组）来记录value所在的格子坐标，这样每次Query都是O(1)时间。时间复杂度：初始化O(n^2)，query O(1)。空间复杂度：O(n^2) [crayon-67eadbe8d074a734389530/] 黄金 Precompute：在初始化的时候顺便求合，开两个数组一个存上下左右的，一个存对角线的。query的时候直接返回答案就可以了。时间复杂度和白银是一样的，但会快一些（省去了求合的过程）。 [crayon-67eadbe8d075b934687984/]]]></description>
										<content:encoded><![CDATA[
<p>青铜 Brute Force：每次需要花费O(n*n)的时间去查找query所在的格子，求和是O(1)。总的时间复杂度达到O(n^4)，肯定会超时。</p>



<p>白银 Hashtable：由于所有元素的值的唯一的，我们可以使用hashtable（或者数组）来记录value所在的格子坐标，这样每次Query都是O(1)时间。<br>时间复杂度：初始化O(n^2)，query O(1)。<br>空间复杂度：O(n^2)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class NeighborSum {
public:
  NeighborSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid): 
    n_(grid.size()), g_(&amp;grid), xy_(n_ * n_) {
    for (int i = 0; i &lt; n_; ++i)
      for (int j = 0; j &lt; n_; ++j)
        xy_[grid[i][j]] = {j, i};
  }
  
  int adjacentSum(int value) {
    auto [x, y] = xy_[value];
    return val(x, y - 1) + val(x, y + 1) + val(x + 1, y) + val(x - 1, y);
  }
  
  int diagonalSum(int value) {
    auto [x, y] = xy_[value];
    return val(x - 1, y - 1) + val(x - 1, y + 1) + val(x + 1, y - 1) + val(x + 1, y + 1);
  }
private:
  inline int val(int x, int y) const {
    if (x &lt; 0 || x &gt;= n_ || y &lt; 0 || y &gt;= n_) return 0;
    return (*g_)[y][x];
  }
  int n_;
  vector&lt;vector&lt;int&gt;&gt;* g_;
  vector&lt;pair&lt;int, int&gt;&gt; xy_;
};</pre>



<p>黄金 Precompute：在初始化的时候顺便求合，开两个数组一个存上下左右的，一个存对角线的。query的时候直接返回答案就可以了。<br>时间复杂度和白银是一样的，但会快一些（省去了求合的过程）。</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class NeighborSum {
public:
  NeighborSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    int n = grid.size();
    adj_.resize(n * n);
    dia_.resize(n * n);
    auto v = [&amp;](int x, int y) -&gt; int {
      if (x &lt; 0 || x &gt;= n || y &lt; 0 || y &gt;= n) 
        return 0;
      return grid[y][x];
    };
    for (int y = 0; y &lt; n; ++y)
      for (int x = 0; x &lt; n; ++x) {
        adj_[grid[y][x]] = v(x, y - 1) + v(x, y + 1) + v(x + 1, y) + v(x - 1, y);
        dia_[grid[y][x]] = v(x - 1, y - 1) + v(x - 1, y + 1) + v(x + 1, y - 1) + v(x + 1, y + 1);
      }
  }
  
  int adjacentSum(int value) {
    return adj_[value];
  }
  
  int diagonalSum(int value) {
    return dia_[value];
  }
private:
  vector&lt;int&gt; adj_;
  vector&lt;int&gt; dia_;
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-3242-design-neighbor-sum-service/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2564. Substring XOR Queries</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2564-substring-xor-queries/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2564-substring-xor-queries/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Feb 2023 16:50:41 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[precompute]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[xor]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9942</guid>

					<description><![CDATA[You are given a&#160;binary string&#160;s, and a&#160;2D&#160;integer array&#160;queries&#160;where&#160;queries[i] = [firsti, secondi]. For the&#160;ith&#160;query, find the&#160;shortest substring&#160;of&#160;s&#160;whose&#160;decimal value,&#160;val, yields&#160;secondi&#160;when&#160;bitwise XORed&#160;with&#160;firsti. In other words,&#160;val ^ firsti ==&#8230;]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 2564. Substring XOR Queries - 刷题找工作 EP410" width="500" height="281" src="https://www.youtube.com/embed/UsH8x4Fg4DI?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>
</div></figure>



<p>You are given a&nbsp;<strong>binary string</strong>&nbsp;<code>s</code>, and a&nbsp;<strong>2D</strong>&nbsp;integer array&nbsp;<code>queries</code>&nbsp;where&nbsp;<code>queries[i] = [first<sub>i</sub>, second<sub>i</sub>]</code>.</p>



<p>For the&nbsp;<code>i<sup>th</sup></code>&nbsp;query, find the&nbsp;<strong>shortest substring</strong>&nbsp;of&nbsp;<code>s</code>&nbsp;whose&nbsp;<strong>decimal value</strong>,&nbsp;<code>val</code>, yields&nbsp;<code>second<sub>i</sub></code>&nbsp;when&nbsp;<strong>bitwise XORed</strong>&nbsp;with&nbsp;<code>first<sub>i</sub></code>. In other words,&nbsp;<code>val ^ first<sub>i</sub> == second<sub>i</sub></code>.</p>



<p>The answer to the&nbsp;<code>i<sup>th</sup></code>&nbsp;query is the endpoints (<strong>0-indexed</strong>) of the substring&nbsp;<code>[left<sub>i</sub>, right<sub>i</sub>]</code>&nbsp;or&nbsp;<code>[-1, -1]</code>&nbsp;if no such substring exists. If there are multiple answers, choose the one with the&nbsp;<strong>minimum</strong>&nbsp;<code>left<sub>i</sub></code>.</p>



<p><em>Return an array</em>&nbsp;<code>ans</code>&nbsp;<em>where</em>&nbsp;<code>ans[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>&nbsp;<em>is the answer to the</em>&nbsp;<code>i<sup>th</sup></code>&nbsp;<em>query.</em></p>



<p>A&nbsp;<strong>substring</strong>&nbsp;is a contiguous non-empty sequence of characters within a string.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "101101", queries = [[0,5],[1,2]]
<strong>Output:</strong> [[0,2],[2,3]]
<strong>Explanation:</strong> For the first query the substring in range <code>[0,2]</code> is <strong>"101"</strong> which has a decimal value of <strong><code>5</code></strong>, and <strong><code>5 ^ 0 = 5</code></strong>, hence the answer to the first query is <code>[0,2]</code>. In the second query, the substring in range <code>[2,3]</code> is <strong>"11",</strong> and has a decimal value of <strong>3</strong>, and <strong>3<code> ^ 1 = 2</code></strong>.&nbsp;So, <code>[2,3]</code> is returned for the second query. 
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "0101", queries = [[12,8]]
<strong>Output:</strong> [[-1,-1]]
<strong>Explanation:</strong> In this example there is no substring that answers the query, hence <code>[-1,-1] is returned</code>.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1", queries = [[4,5]]
<strong>Output:</strong> [[0,0]]
<strong>Explanation:</strong> For this example, the substring in range <code>[0,0]</code> has a decimal value of <strong><code>1</code></strong>, and <strong><code>1 ^ 4 = 5</code></strong>. So, the answer is <code>[0,0]</code>.
</pre>



<p><strong>Constraints:</strong></p>



<ul class="wp-block-list"><li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li><li><code>s[i]</code>&nbsp;is either&nbsp;<code>'0'</code>&nbsp;or&nbsp;<code>'1'</code>.</li><li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= first<sub>i</sub>, second<sub>i</sub> &lt;= 10<sup>9</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Pre-compute</strong></h2>



<p>We can pre-compute all possible substrings</p>



<p>Time complexity: O(n*32 + m)<br>Space complexity: O(n*32)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; substringXorQueries(string s, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    unordered_map&lt;int, vector&lt;int&gt;&gt; m;
    const int l = s.length();
    for (int i = 0; i &lt; l; ++i) {
      if (s[i] == '0') {
        if (!m.count(0)) m[0] = {i, i};
        continue;
      }
      for (int j = 0, cur = 0; j &lt; 32 &amp;&amp; i + j &lt; l; ++j) {
        cur = (cur &lt;&lt; 1) | (s[i + j] - '0');
        if (!m.count(cur))
          m[cur] = {i, i + j};
      }
    }
    vector&lt;vector&lt;int&gt;&gt; ans;    
    for (const auto&amp; q : queries) {
      int x = q[0] ^ q[1];
      if (m.count(x)) 
        ans.push_back(m[x]);
      else
        ans.push_back({-1, -1});
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-2564-substring-xor-queries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2420. Find All Good Indices</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2420-find-all-good-indices/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2420-find-all-good-indices/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 26 Sep 2022 17:18:29 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[precompute]]></category>
		<category><![CDATA[prefix sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9850</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;integer array&#160;nums&#160;of size&#160;n&#160;and a positive integer&#160;k. We call an index&#160;i&#160;in the range&#160;k &#60;= i &#60; n - k&#160;good&#160;if the following conditions are&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;of size&nbsp;<code>n</code>&nbsp;and a positive integer&nbsp;<code>k</code>.</p>



<p>We call an index&nbsp;<code>i</code>&nbsp;in the range&nbsp;<code>k &lt;= i &lt; n - k</code>&nbsp;<strong>good</strong>&nbsp;if the following conditions are satisfied:</p>



<ul class="wp-block-list"><li>The&nbsp;<code>k</code>&nbsp;elements that are just&nbsp;<strong>before</strong>&nbsp;the index&nbsp;<code>i</code>&nbsp;are in&nbsp;<strong>non-increasing</strong>&nbsp;order.</li><li>The&nbsp;<code>k</code>&nbsp;elements that are just&nbsp;<strong>after</strong>&nbsp;the index&nbsp;<code>i</code>&nbsp;are in&nbsp;<strong>non-decreasing</strong>&nbsp;order.</li></ul>



<p>Return&nbsp;<em>an array of all good indices sorted in&nbsp;<strong>increasing</strong>&nbsp;order</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,1,1,1,3,4,1], k = 2
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong> There are two good indices in the array:
- Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.
- Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.
Note that the index 4 is not good because [4,1] is not non-decreasing.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,1,1,2], k = 2
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no good indices in this array.
</pre>



<p><strong>Constraints:</strong></p>



<ul class="wp-block-list"><li><code>n == nums.length</code></li><li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li><li><code>1 &lt;= k &lt;= n / 2</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Prefix Sum</strong></h2>



<p>Let before[i] = length of longest non-increasing subarray ends of nums[i].<br>Let after[i] = length of longest non-decreasing subarray ends of nums[i].</p>



<p>An index is good if nums[i &#8211; 1] &gt;= k and nums[i + k] &gt;= k</p>



<p>Time complexity: O(n + (n &#8211; 2*k))<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; goodIndices(vector&lt;int&gt;&amp; nums, int k) {
    const int n = nums.size();
    vector&lt;int&gt; before(n, 1);
    vector&lt;int&gt; after(n, 1);
    for (int i = 1; i &lt; n; ++i) {
      if (nums[i] &lt;= nums[i - 1])
        before[i] = before[i - 1] + 1;      
      if (nums[i] &gt;= nums[i - 1])
        after[i] = after[i - 1] + 1;    
    }
    vector&lt;int&gt; ans;
    for (int i = k; i + k &lt; n; ++i) {
      if (before[i - 1] &gt;= k &amp;&amp; after[i + k] &gt;= k)
        ans.push_back(i);
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2420-find-all-good-indices/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2155. All Divisions With the Highest Score of a Binary Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2155-all-divisions-with-the-highest-score-of-a-binary-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2155-all-divisions-with-the-highest-score-of-a-binary-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Feb 2022 06:00:35 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[divide]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[precompute]]></category>
		<category><![CDATA[prefix sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9484</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;binary array&#160;nums&#160;of length&#160;n.&#160;nums&#160;can be divided at index&#160;i&#160;(where&#160;0 &#60;= i &#60;= n)&#160;into two arrays (possibly empty)&#160;numsleft&#160;and&#160;numsright: numsleft&#160;has all the elements of&#160;nums&#160;between index&#160;0&#160;and&#160;i -&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;binary array&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>n</code>.&nbsp;<code>nums</code>&nbsp;can be divided at index&nbsp;<code>i</code>&nbsp;(where&nbsp;<code>0 &lt;= i &lt;= n)</code>&nbsp;into two arrays (possibly empty)&nbsp;<code>nums<sub>left</sub></code>&nbsp;and&nbsp;<code>nums<sub>right</sub></code>:</p>



<ul class="wp-block-list"><li><code>nums<sub>left</sub></code>&nbsp;has all the elements of&nbsp;<code>nums</code>&nbsp;between index&nbsp;<code>0</code>&nbsp;and&nbsp;<code>i - 1</code>&nbsp;<strong>(inclusive)</strong>, while&nbsp;<code>nums<sub>right</sub></code>&nbsp;has all the elements of nums between index&nbsp;<code>i</code>&nbsp;and&nbsp;<code>n - 1</code>&nbsp;<strong>(inclusive)</strong>.</li><li>If&nbsp;<code>i == 0</code>,&nbsp;<code>nums<sub>left</sub></code>&nbsp;is&nbsp;<strong>empty</strong>, while&nbsp;<code>nums<sub>right</sub></code>&nbsp;has all the elements of&nbsp;<code>nums</code>.</li><li>If&nbsp;<code>i == n</code>,&nbsp;<code>nums<sub>left</sub></code>&nbsp;has all the elements of nums, while&nbsp;<code>nums<sub>right</sub></code>&nbsp;is&nbsp;<strong>empty</strong>.</li></ul>



<p>The&nbsp;<strong>division score</strong>&nbsp;of an index&nbsp;<code>i</code>&nbsp;is the&nbsp;<strong>sum</strong>&nbsp;of the number of&nbsp;<code>0</code>&#8216;s in&nbsp;<code>nums<sub>left</sub></code>&nbsp;and the number of&nbsp;<code>1</code>&#8216;s in&nbsp;<code>nums<sub>right</sub></code>.</p>



<p>Return&nbsp;<em><strong>all distinct indices</strong>&nbsp;that have the&nbsp;<strong>highest</strong>&nbsp;possible&nbsp;<strong>division score</strong></em>. You may return the answer in&nbsp;<strong>any order</strong>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,0,1,0]
<strong>Output:</strong> [2,4]
<strong>Explanation:</strong> Division at index
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,<strong>1</strong>,0]. The score is 0 + 1 = 1.
- 1: nums<sub>left</sub> is [<strong>0</strong>]. nums<sub>right</sub> is [0,<strong>1</strong>,0]. The score is 1 + 1 = 2.
- 2: nums<sub>left</sub> is [<strong>0</strong>,<strong>0</strong>]. nums<sub>right</sub> is [<strong>1</strong>,0]. The score is 2 + 1 = 3.
- 3: nums<sub>left</sub> is [<strong>0</strong>,<strong>0</strong>,1]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
- 4: nums<sub>left</sub> is [<strong>0</strong>,<strong>0</strong>,1,<strong>0</strong>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
Indices 2 and 4 both have the highest possible division score 3.
Note the answer [4,2] would also be accepted.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [3]
<strong>Explanation:</strong> Division at index
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,0]. The score is 0 + 0 = 0.
- 1: nums<sub>left</sub> is [<strong>0</strong>]. nums<sub>right</sub> is [0,0]. The score is 1 + 0 = 1.
- 2: nums<sub>left</sub> is [<strong>0</strong>,<strong>0</strong>]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
- 3: nums<sub>left</sub> is [<strong>0</strong>,<strong>0</strong>,<strong>0</strong>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
Only index 3 has the highest possible division score 3.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1]
<strong>Output:</strong> [0]
<strong>Explanation:</strong> Division at index
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [<strong>1</strong>,<strong>1</strong>]. The score is 0 + 2 = 2.
- 1: nums<sub>left</sub> is [1]. nums<sub>right</sub> is [<strong>1</strong>]. The score is 0 + 1 = 1.
- 2: nums<sub>left</sub> is [1,1]. nums<sub>right</sub> is []. The score is 0 + 0 = 0.
Only index 0 has the highest possible division score 2.
</pre>



<p><strong>Constraints:</strong></p>



<ul class="wp-block-list"><li><code>n == nums.length</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>nums[i]</code>&nbsp;is either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li></ul>



<h2 class="wp-block-heading"><strong>Solution: Precompute + Prefix Sum</strong></h2>



<p>Count how many ones in the array, track the prefix sum to compute score for each index in O(1).</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; maxScoreIndices(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    const int t = accumulate(begin(nums), end(nums), 0);    
    vector&lt;int&gt; ans;
    for (int i = 0, p = 0, b = 0; i &lt;= n; ++i) {
      const int s = (i - p) + (t - p);      
      if (s &gt; b) {
        b = s; ans.clear();
      }
      if (s == b) ans.push_back(i);
      if (i != n) p += nums[i];
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2155-all-divisions-with-the-highest-score-of-a-binary-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
