{"id":10010,"date":"2023-04-29T08:30:47","date_gmt":"2023-04-29T15:30:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10010"},"modified":"2023-04-29T08:31:24","modified_gmt":"2023-04-29T15:31:24","slug":"leetcode-2640-find-the-score-of-all-prefixes-of-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2640-find-the-score-of-all-prefixes-of-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2640. Find the Score of All Prefixes of an Array"},"content":{"rendered":"\n<p>We define the&nbsp;<strong>conversion array<\/strong>&nbsp;<code>conver<\/code>&nbsp;of an array&nbsp;<code>arr<\/code>&nbsp;as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>conver[i] = arr[i] + max(arr[0..i])<\/code>&nbsp;where&nbsp;<code>max(arr[0..i])<\/code>&nbsp;is the maximum value of&nbsp;<code>arr[j]<\/code>&nbsp;over&nbsp;<code>0 &lt;= j &lt;= i<\/code>.<\/li><\/ul>\n\n\n\n<p>We also define the&nbsp;<strong>score<\/strong>&nbsp;of an array&nbsp;<code>arr<\/code>&nbsp;as the sum of the values of the conversion array of&nbsp;<code>arr<\/code>.<\/p>\n\n\n\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of length&nbsp;<code>n<\/code>, return&nbsp;<em>an array&nbsp;<\/em><code>ans<\/code><em>&nbsp;of length&nbsp;<\/em><code>n<\/code><em>&nbsp;where&nbsp;<\/em><code>ans[i]<\/code><em>&nbsp;is the score of the prefix<\/em>&nbsp;<code>nums[0..i]<\/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 = [2,3,7,5,10]\n<strong>Output:<\/strong> [4,10,24,36,56]\n<strong>Explanation:<\/strong> \nFor the prefix [2], the conversion array is [4] hence the score is 4\nFor the prefix [2, 3], the conversion array is [4, 6] hence the score is 10\nFor the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24\nFor the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36\nFor the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56\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 = [1,1,2,4,8,16]\n<strong>Output:<\/strong> [2,4,8,16,32,64]\n<strong>Explanation:<\/strong> \nFor the prefix [1], the conversion array is [2] hence the score is 2\nFor the prefix [1, 1], the conversion array is [2, 2] hence the score is 4\nFor the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8\nFor the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16\nFor the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32\nFor the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum<\/strong><\/h2>\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  vector<long long> findPrefixScore(vector<int>& nums) {\n    vector<long long> ans(nums.size());\n    for (int i = 0, m = 0; i < nums.size(); ++i) {\n      m = max(m, nums[i]);\n      ans[i] = (i ? ans[i - 1] : 0) + nums[i] + m;\n    }    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We define the&nbsp;conversion array&nbsp;conver&nbsp;of an array&nbsp;arr&nbsp;as follows: conver[i] = arr[i] + max(arr[0..i])&nbsp;where&nbsp;max(arr[0..i])&nbsp;is the maximum value of&nbsp;arr[j]&nbsp;over&nbsp;0 &lt;= j &lt;= i. We also define the&nbsp;score&nbsp;of an&#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-10010","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\/10010","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=10010"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10010\/revisions"}],"predecessor-version":[{"id":10012,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10010\/revisions\/10012"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}