{"id":9484,"date":"2022-02-04T22:00:35","date_gmt":"2022-02-05T06:00:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9484"},"modified":"2022-02-05T06:34:15","modified_gmt":"2022-02-05T14:34:15","slug":"leetcode-2155-all-divisions-with-the-highest-score-of-a-binary-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2155-all-divisions-with-the-highest-score-of-a-binary-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2155. All Divisions With the Highest Score of a Binary Array"},"content":{"rendered":"\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<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>\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 = [0,0,1,0]\n<strong>Output:<\/strong> [2,4]\n<strong>Explanation:<\/strong> Division at index\n- 0: nums<sub>left<\/sub> is []. nums<sub>right<\/sub> is [0,0,<strong>1<\/strong>,0]. The score is 0 + 1 = 1.\n- 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.\n- 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.\n- 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.\n- 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.\nIndices 2 and 4 both have the highest possible division score 3.\nNote the answer [4,2] would also be accepted.<\/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 = [0,0,0]\n<strong>Output:<\/strong> [3]\n<strong>Explanation:<\/strong> Division at index\n- 0: nums<sub>left<\/sub> is []. nums<sub>right<\/sub> is [0,0,0]. The score is 0 + 0 = 0.\n- 1: nums<sub>left<\/sub> is [<strong>0<\/strong>]. nums<sub>right<\/sub> is [0,0]. The score is 1 + 0 = 1.\n- 2: nums<sub>left<\/sub> is [<strong>0<\/strong>,<strong>0<\/strong>]. nums<sub>right<\/sub> is [0]. The score is 2 + 0 = 2.\n- 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.\nOnly index 3 has the highest possible division score 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1]\n<strong>Output:<\/strong> [0]\n<strong>Explanation:<\/strong> Division at index\n- 0: nums<sub>left<\/sub> is []. nums<sub>right<\/sub> is [<strong>1<\/strong>,<strong>1<\/strong>]. The score is 0 + 2 = 2.\n- 1: nums<sub>left<\/sub> is [1]. nums<sub>right<\/sub> is [<strong>1<\/strong>]. The score is 0 + 1 = 1.\n- 2: nums<sub>left<\/sub> is [1,1]. nums<sub>right<\/sub> is []. The score is 0 + 0 = 0.\nOnly index 0 has the highest possible division score 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Precompute + Prefix Sum<\/strong><\/h2>\n\n\n\n<p>Count how many ones in the array, track the prefix sum to compute score for each index in O(1).<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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  vector<int> maxScoreIndices(vector<int>& nums) {\n    const int n = nums.size();\n    const int t = accumulate(begin(nums), end(nums), 0);    \n    vector<int> ans;\n    for (int i = 0, p = 0, b = 0; i <= n; ++i) {\n      const int s = (i - p) + (t - p);      \n      if (s > b) {\n        b = s; ans.clear();\n      }\n      if (s == b) ans.push_back(i);\n      if (i != n) p += nums[i];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;binary array&nbsp;nums&nbsp;of length&nbsp;n.&nbsp;nums&nbsp;can be divided at index&nbsp;i&nbsp;(where&nbsp;0 &lt;= i &lt;= n)&nbsp;into two arrays (possibly empty)&nbsp;numsleft&nbsp;and&nbsp;numsright: numsleft&nbsp;has all the elements of&nbsp;nums&nbsp;between index&nbsp;0&nbsp;and&nbsp;i -&#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":[763,177,764,200],"class_list":["post-9484","post","type-post","status-publish","format-standard","hentry","category-array","tag-divide","tag-medium","tag-precompute","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9484","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=9484"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9484\/revisions"}],"predecessor-version":[{"id":9486,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9484\/revisions\/9486"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}