<?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>split array Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/split-array/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/split-array/</link>
	<description></description>
	<lastBuildDate>Sun, 03 Jan 2021 08:57:41 +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>split array Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/split-array/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1712. Ways to Split Array Into Three Subarrays</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1712-ways-to-split-array-into-three-subarrays/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1712-ways-to-split-array-into-three-subarrays/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Jan 2021 08:44:40 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[split array]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7897</guid>

					<description><![CDATA[<p>A split of an integer array is&#160;good&#160;if: The array is split into three&#160;non-empty&#160;contiguous subarrays &#8211; named&#160;left,&#160;mid,&#160;right&#160;respectively from left to right. The sum of the elements&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1712-ways-to-split-array-into-three-subarrays/">花花酱 LeetCode 1712. Ways to Split Array Into Three Subarrays</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>A split of an integer array is&nbsp;<strong>good</strong>&nbsp;if:</p>



<ul><li>The array is split into three&nbsp;<strong>non-empty</strong>&nbsp;contiguous subarrays &#8211; named&nbsp;<code>left</code>,&nbsp;<code>mid</code>,&nbsp;<code>right</code>&nbsp;respectively from left to right.</li><li>The sum of the elements in&nbsp;<code>left</code>&nbsp;is less than or equal to the sum of the elements in&nbsp;<code>mid</code>, and the sum of the elements in&nbsp;<code>mid</code>&nbsp;is less than or equal to the sum of the elements in&nbsp;<code>right</code>.</li></ul>



<p>Given&nbsp;<code>nums</code>, an array of&nbsp;<strong>non-negative</strong>&nbsp;integers, return&nbsp;<em>the number of&nbsp;<strong>good</strong>&nbsp;ways to split</em>&nbsp;<code>nums</code>. As the number may be too large, return it&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9&nbsp;</sup>+ 7</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only good way to split nums is [1] [1] [1].</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,2,2,5,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three good ways of splitting nums:
[1] [2] [2,2,5,0]
[1] [2,2] [2,5,0]
[1,2] [2,2] [5,0]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no good way to split nums.</pre>



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



<ul><li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li></ul>



<h2><strong>Solution 1: Prefix Sum + Binary Search</strong></h2>



<p>We split the array into [0 &#8230; i] [i + 1&#8230; j] [j + 1 &#8230; n &#8211; 1]<br>we can use binary search to find the min and max of j for each i.<br>s.t.  sum(0 ~ i) &lt;= sums(i + 1 ~j) &lt;= sums(j + 1 ~ n &#8211; 1)<br>min is lower_bound(2 * sums(0 ~ i))<br>max is upper_bound(sums(0 ~ i) + (total &#8211; sums(0 ~ i)) / 2)</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int waysToSplit(vector&lt;int&gt;&amp; nums) {
    constexpr int kMod = 1e9 + 7;
    const int n = nums.size();    
    for (int i = 1; i &lt; n; ++i) 
      nums[i] += nums[i - 1];
    const int total = nums.back();
    long ans = 0;  
    // [0 .. i] [i + 1 ... j] [j + 1 ... n - 1]
    for (int i = 0, left = 0; i &lt; n; ++i) {      
      auto it1 = lower_bound(begin(nums) + i + 1, end(nums), 2 * nums[i]);
      auto it2 = upper_bound(begin(nums), end(nums) - 1, nums[i] + (total - nums[i]) / 2);
      if (it2 &lt;= it1) continue;
      ans += it2 - it1;
    }    
    return ans % kMod;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Prefix Sum + Two Pointers</strong></h2>



<p>The right end of the middle array is in range [j, k &#8211; 1] and there are k &#8211; j choices.<br>Time complexity: O(n)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int waysToSplit(vector&lt;int&gt;&amp; nums) {
    constexpr int kMod = 1e9 + 7;
    const int n = nums.size();    
    for (int i = 1; i &lt; n; ++i) 
      nums[i] += nums[i - 1];
    const int total = nums.back();
    long ans = 0;    
    for (int i = 0, j = 0, k = 0; i &lt; n; ++i) {      
      j = max(j, i + 1);
      while (j &lt; n - 1 &amp;&amp; nums[j] &lt; 2 * nums[i]) ++j;
      k = max(k, j);
      while (k &lt; n - 1 &amp;&amp; 2 * nums[k] &lt;= nums[i] + total) ++k;
      ans += (k - j);
    }    
    return ans % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1712-ways-to-split-array-into-three-subarrays/">花花酱 LeetCode 1712. Ways to Split Array Into Three Subarrays</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/binary-search/leetcode-1712-ways-to-split-array-into-three-subarrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
