{"id":9744,"date":"2022-05-15T12:09:06","date_gmt":"2022-05-15T19:09:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9744"},"modified":"2022-05-15T12:09:58","modified_gmt":"2022-05-15T19:09:58","slug":"leetcode-2270-number-of-ways-to-split-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2270-number-of-ways-to-split-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2270. Number of Ways to Split Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of length&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p><code>nums<\/code>&nbsp;contains a&nbsp;<strong>valid split<\/strong>&nbsp;at index&nbsp;<code>i<\/code>&nbsp;if the following are true:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The sum of the first&nbsp;<code>i + 1<\/code>&nbsp;elements is&nbsp;<strong>greater than or equal to<\/strong>&nbsp;the sum of the last&nbsp;<code>n - i - 1<\/code>&nbsp;elements.<\/li><li>There is&nbsp;<strong>at least one<\/strong>&nbsp;element to the right of&nbsp;<code>i<\/code>. That is,&nbsp;<code>0 &lt;= i &lt; n - 1<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of&nbsp;<strong>valid splits<\/strong>&nbsp;in<\/em>&nbsp;<code>nums<\/code>.<\/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 = [10,4,-8,7]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \nThere are three ways of splitting nums into two non-empty parts:\n- Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 &gt;= 3, i = 0 is a valid split.\n- Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 &gt;= -1, i = 1 is a valid split.\n- Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 &lt; 7, i = 2 is not a valid split.\nThus, the number of valid splits in nums is 2.\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 = [2,3,1,0]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \nThere are two valid splits in nums:\n- Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 &gt;= 1, i = 1 is a valid split. \n- Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 &gt;= 0, i = 2 is a valid split.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix\/Suffix Sum<\/strong><\/h2>\n\n\n\n<p>Note: sum can be greater than 2^31, use long!<\/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 waysToSplitArray(vector<int>& A) {    \n    int ans = 0;\n    for (long i = 0, l = 0, r = accumulate(begin(A), end(A), 0l); i < A.size() - 1; ++i)       \n      ans += (l += A[i]) >= (r -= A[i]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;of length&nbsp;n. nums&nbsp;contains a&nbsp;valid split&nbsp;at index&nbsp;i&nbsp;if the following are true: The sum of the first&nbsp;i + 1&nbsp;elements is&nbsp;greater than or equal&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,177,200],"class_list":["post-9744","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9744","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=9744"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9744\/revisions"}],"predecessor-version":[{"id":9746,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9744\/revisions\/9746"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}