diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/README.md b/solution/2200-2299/2219.Maximum Sum Score of Array/README.md index 7bdd953e6528a..f0f40d0fb2c7a 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/README.md +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/README.md @@ -70,7 +70,15 @@ nums 可取得的最大总分是 -3 。 -### 方法一 +### 方法一:前缀和 + +我们可以使用两个变量 $l$ 和 $r$ 分别表示数组的前缀和和后缀和,初始时 $l = 0$, $r = \sum_{i=0}^{n-1} \textit{nums}[i]$。 + +接下来,我们遍历数组 $\textit{nums}$,对于每个元素 $x$,我们将 $l$ 增加 $x$,并更新答案 $\textit{ans} = \max(\textit{ans}, l, r)$,然后将 $r$ 减少 $x$。 + +遍历结束后,返回答案 $\textit{ans}$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -79,8 +87,13 @@ nums 可取得的最大总分是 -3 。 ```python class Solution: def maximumSumScore(self, nums: List[int]) -> int: - s = [0] + list(accumulate(nums)) - return max(max(s[i + 1], s[-1] - s[i]) for i in range(len(nums))) + l, r = 0, sum(nums) + ans = -inf + for x in nums: + l += x + ans = max(ans, l, r) + r -= x + return ans ``` #### Java @@ -88,14 +101,15 @@ class Solution: ```java class Solution { public long maximumSumScore(int[] nums) { - int n = nums.length; - long[] s = new long[n + 1]; - for (int i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; + long l = 0, r = 0; + for (int x : nums) { + r += x; } long ans = Long.MIN_VALUE; - for (int i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (int x : nums) { + l += x; + ans = Math.max(ans, Math.max(l, r)); + r -= x; } return ans; } @@ -108,11 +122,13 @@ class Solution { class Solution { public: long long maximumSumScore(vector& nums) { - int n = nums.size(); - vector s(n + 1); - for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; - long long ans = INT_MIN; - for (int i = 0; i < n; ++i) ans = max(ans, max(s[i + 1], s[n] - s[i])); + long long l = 0, r = accumulate(nums.begin(), nums.end(), 0LL); + long long ans = -1e18; + for (int x : nums) { + l += x; + ans = max({ans, l, r}); + r -= x; + } return ans; } }; @@ -122,16 +138,17 @@ public: ```go func maximumSumScore(nums []int) int64 { - n := len(nums) - s := make([]int64, n+1) - for i, v := range nums { - s[i+1] = s[i] + int64(v) + l, r := 0, 0 + for _, x := range nums { + r += x } - var ans int64 = math.MinInt64 - for i := 0; i < n; i++ { - ans = max(ans, max(s[i+1], s[n]-s[i])) + ans := math.MinInt64 + for _, x := range nums { + l += x + ans = max(ans, max(l, r)) + r -= x } - return ans + return int64(ans) } ``` @@ -139,19 +156,36 @@ func maximumSumScore(nums []int) int64 { ```ts function maximumSumScore(nums: number[]): number { - const n = nums.length; - let s = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; - } + let l = 0; + let r = nums.reduce((a, b) => a + b, 0); let ans = -Infinity; - for (let i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (const x of nums) { + l += x; + ans = Math.max(ans, l, r); + r -= x; } return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_sum_score(nums: Vec) -> i64 { + let mut l = 0; + let mut r: i64 = nums.iter().map(|&x| x as i64).sum(); + let mut ans = std::i64::MIN; + for &x in &nums { + l += x as i64; + ans = ans.max(l).max(r); + r -= x as i64; + } + ans + } +} +``` + #### JavaScript ```js @@ -160,14 +194,13 @@ function maximumSumScore(nums: number[]): number { * @return {number} */ var maximumSumScore = function (nums) { - const n = nums.length; - let s = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; - } + let l = 0; + let r = nums.reduce((a, b) => a + b, 0); let ans = -Infinity; - for (let i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (const x of nums) { + l += x; + ans = Math.max(ans, l, r); + r -= x; } return ans; }; diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/README_EN.md b/solution/2200-2299/2219.Maximum Sum Score of Array/README_EN.md index 1a6507db26ad9..365f6b2d5c25c 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/README_EN.md +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/README_EN.md @@ -68,7 +68,15 @@ The maximum sum score of nums is -3. -### Solution 1 +### Solution 1: Prefix Sum + +We can use two variables $l$ and $r$ to represent the prefix sum and suffix sum of the array, respectively. Initially, $l = 0$ and $r = \sum_{i=0}^{n-1} \textit{nums}[i]$. + +Next, we traverse the array $\textit{nums}$. For each element $x$, we add $x$ to $l$ and update the answer $\textit{ans} = \max(\textit{ans}, l, r)$, then subtract $x$ from $r$. + +After the traversal, return the answer $\textit{ans}$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -77,8 +85,13 @@ The maximum sum score of nums is -3. ```python class Solution: def maximumSumScore(self, nums: List[int]) -> int: - s = [0] + list(accumulate(nums)) - return max(max(s[i + 1], s[-1] - s[i]) for i in range(len(nums))) + l, r = 0, sum(nums) + ans = -inf + for x in nums: + l += x + ans = max(ans, l, r) + r -= x + return ans ``` #### Java @@ -86,14 +99,15 @@ class Solution: ```java class Solution { public long maximumSumScore(int[] nums) { - int n = nums.length; - long[] s = new long[n + 1]; - for (int i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; + long l = 0, r = 0; + for (int x : nums) { + r += x; } long ans = Long.MIN_VALUE; - for (int i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (int x : nums) { + l += x; + ans = Math.max(ans, Math.max(l, r)); + r -= x; } return ans; } @@ -106,11 +120,13 @@ class Solution { class Solution { public: long long maximumSumScore(vector& nums) { - int n = nums.size(); - vector s(n + 1); - for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; - long long ans = INT_MIN; - for (int i = 0; i < n; ++i) ans = max(ans, max(s[i + 1], s[n] - s[i])); + long long l = 0, r = accumulate(nums.begin(), nums.end(), 0LL); + long long ans = -1e18; + for (int x : nums) { + l += x; + ans = max({ans, l, r}); + r -= x; + } return ans; } }; @@ -120,16 +136,17 @@ public: ```go func maximumSumScore(nums []int) int64 { - n := len(nums) - s := make([]int64, n+1) - for i, v := range nums { - s[i+1] = s[i] + int64(v) + l, r := 0, 0 + for _, x := range nums { + r += x } - var ans int64 = math.MinInt64 - for i := 0; i < n; i++ { - ans = max(ans, max(s[i+1], s[n]-s[i])) + ans := math.MinInt64 + for _, x := range nums { + l += x + ans = max(ans, max(l, r)) + r -= x } - return ans + return int64(ans) } ``` @@ -137,19 +154,36 @@ func maximumSumScore(nums []int) int64 { ```ts function maximumSumScore(nums: number[]): number { - const n = nums.length; - let s = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; - } + let l = 0; + let r = nums.reduce((a, b) => a + b, 0); let ans = -Infinity; - for (let i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (const x of nums) { + l += x; + ans = Math.max(ans, l, r); + r -= x; } return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_sum_score(nums: Vec) -> i64 { + let mut l = 0; + let mut r: i64 = nums.iter().map(|&x| x as i64).sum(); + let mut ans = std::i64::MIN; + for &x in &nums { + l += x as i64; + ans = ans.max(l).max(r); + r -= x as i64; + } + ans + } +} +``` + #### JavaScript ```js @@ -158,14 +192,13 @@ function maximumSumScore(nums: number[]): number { * @return {number} */ var maximumSumScore = function (nums) { - const n = nums.length; - let s = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; - } + let l = 0; + let r = nums.reduce((a, b) => a + b, 0); let ans = -Infinity; - for (let i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (const x of nums) { + l += x; + ans = Math.max(ans, l, r); + r -= x; } return ans; }; diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.cpp b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.cpp index 916abcc987fd8..c36e3e5ac0359 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.cpp +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.cpp @@ -1,11 +1,13 @@ class Solution { public: long long maximumSumScore(vector& nums) { - int n = nums.size(); - vector s(n + 1); - for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; - long long ans = INT_MIN; - for (int i = 0; i < n; ++i) ans = max(ans, max(s[i + 1], s[n] - s[i])); + long long l = 0, r = accumulate(nums.begin(), nums.end(), 0LL); + long long ans = -1e18; + for (int x : nums) { + l += x; + ans = max({ans, l, r}); + r -= x; + } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.go b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.go index bc8df8911be28..c644787a57e1f 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.go +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.go @@ -1,12 +1,13 @@ func maximumSumScore(nums []int) int64 { - n := len(nums) - s := make([]int64, n+1) - for i, v := range nums { - s[i+1] = s[i] + int64(v) + l, r := 0, 0 + for _, x := range nums { + r += x } - var ans int64 = math.MinInt64 - for i := 0; i < n; i++ { - ans = max(ans, max(s[i+1], s[n]-s[i])) + ans := math.MinInt64 + for _, x := range nums { + l += x + ans = max(ans, max(l, r)) + r -= x } - return ans -} \ No newline at end of file + return int64(ans) +} diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.java b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.java index d754fa4eb72c6..89ce99ccde252 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.java +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.java @@ -1,14 +1,15 @@ class Solution { public long maximumSumScore(int[] nums) { - int n = nums.length; - long[] s = new long[n + 1]; - for (int i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; + long l = 0, r = 0; + for (int x : nums) { + r += x; } long ans = Long.MIN_VALUE; - for (int i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (int x : nums) { + l += x; + ans = Math.max(ans, Math.max(l, r)); + r -= x; } return ans; } -} \ No newline at end of file +} diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.js b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.js index 2a15d5c5aaaad..8c71673d5256c 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.js +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.js @@ -3,14 +3,13 @@ * @return {number} */ var maximumSumScore = function (nums) { - const n = nums.length; - let s = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; - } + let l = 0; + let r = nums.reduce((a, b) => a + b, 0); let ans = -Infinity; - for (let i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (const x of nums) { + l += x; + ans = Math.max(ans, l, r); + r -= x; } return ans; }; diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.py b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.py index 6b8795fe3ebc2..aea5cb8dd158d 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.py +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.py @@ -1,4 +1,9 @@ class Solution: def maximumSumScore(self, nums: List[int]) -> int: - s = [0] + list(accumulate(nums)) - return max(max(s[i + 1], s[-1] - s[i]) for i in range(len(nums))) + l, r = 0, sum(nums) + ans = -inf + for x in nums: + l += x + ans = max(ans, l, r) + r -= x + return ans diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.rs b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.rs new file mode 100644 index 0000000000000..a9d24f01bd704 --- /dev/null +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn maximum_sum_score(nums: Vec) -> i64 { + let mut l = 0; + let mut r: i64 = nums.iter().map(|&x| x as i64).sum(); + let mut ans = std::i64::MIN; + for &x in &nums { + l += x as i64; + ans = ans.max(l).max(r); + r -= x as i64; + } + ans + } +} diff --git a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.ts b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.ts index 05c5a469cbe16..a7aa5e78d5438 100644 --- a/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.ts +++ b/solution/2200-2299/2219.Maximum Sum Score of Array/Solution.ts @@ -1,12 +1,11 @@ function maximumSumScore(nums: number[]): number { - const n = nums.length; - let s = new Array(n + 1).fill(0); - for (let i = 0; i < n; ++i) { - s[i + 1] = s[i] + nums[i]; - } + let l = 0; + let r = nums.reduce((a, b) => a + b, 0); let ans = -Infinity; - for (let i = 0; i < n; ++i) { - ans = Math.max(ans, Math.max(s[i + 1], s[n] - s[i])); + for (const x of nums) { + l += x; + ans = Math.max(ans, l, r); + r -= x; } return ans; }