<?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>divide Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/divide/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/divide/</link>
	<description></description>
	<lastBuildDate>Sat, 05 Feb 2022 14:34:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>divide Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/divide/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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[<p>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;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2155-all-divisions-with-the-highest-score-of-a-binary-array/">花花酱 LeetCode 2155. All Divisions With the Highest Score of a Binary Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></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><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><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><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="crayon-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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2155-all-divisions-with-the-highest-score-of-a-binary-array/">花花酱 LeetCode 2155. All Divisions With the Highest Score of a Binary Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></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>
