-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2219-MaximumSumScoreOfArray.go
More file actions
92 lines (81 loc) · 2.89 KB
/
2219-MaximumSumScoreOfArray.go
File metadata and controls
92 lines (81 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
// 2219. Maximum Sum Score of Array
// You are given a 0-indexed integer array nums of length n.
// The sum score of nums at an index i where 0 <= i < n is the maximum of:
// The sum of the first i + 1 elements of nums.
// The sum of the last n - i elements of nums.
// Return the maximum sum score of nums at any index.
// Example 1:
// Input: nums = [4,3,-2,5]
// Output: 10
// Explanation:
// The sum score at index 0 is max(4, 4 + 3 + -2 + 5) = max(4, 10) = 10.
// The sum score at index 1 is max(4 + 3, 3 + -2 + 5) = max(7, 6) = 7.
// The sum score at index 2 is max(4 + 3 + -2, -2 + 5) = max(5, 3) = 5.
// The sum score at index 3 is max(4 + 3 + -2 + 5, 5) = max(10, 5) = 10.
// The maximum sum score of nums is 10.
// Example 2:
// Input: nums = [-3,-5]
// Output: -3
// Explanation:
// The sum score at index 0 is max(-3, -3 + -5) = max(-3, -8) = -3.
// The sum score at index 1 is max(-3 + -5, -5) = max(-8, -5) = -5.
// The maximum sum score of nums is -3.
// Constraints:
// n == nums.length
// 1 <= n <= 10^5
// -10^5 <= nums[i] <= 10^5
import "fmt"
func maximumSumScore(nums []int) int64 {
n, sum := len(nums), 0
for _, v := range nums {
sum += v
}
max := func (x, y int) int { if x > y { return x; }; return y; }
left, right := nums[0], sum
res := max(left, right)
for i := 1; i < n; i++ {
left += nums[i]
right = sum - left + nums[i]
res = max(res, max(left, right))
}
return int64(res)
}
func maximumSumScore1(nums []int) int64 {
n := len(nums)
left, right := make([]int, n), make([]int, n)
for i := 1; i < n; i++ {
left[i] = left[i - 1] + nums[i - 1]
}
for i := n-2; i >= 0; i-- {
right[i] = right[i + 1] + nums[i + 1]
}
max := func (x, y int) int { if x > y { return x; }; return y; }
res := max(left[0], right[0]) + nums[0]
for i := 0; i < n; i++ {
res = max(res, max(left[i], right[i]) + nums[i])
}
return int64(res)
}
func main() {
// Example 1:
// Input: nums = [4,3,-2,5]
// Output: 10
// Explanation:
// The sum score at index 0 is max(4, 4 + 3 + -2 + 5) = max(4, 10) = 10.
// The sum score at index 1 is max(4 + 3, 3 + -2 + 5) = max(7, 6) = 7.
// The sum score at index 2 is max(4 + 3 + -2, -2 + 5) = max(5, 3) = 5.
// The sum score at index 3 is max(4 + 3 + -2 + 5, 5) = max(10, 5) = 10.
// The maximum sum score of nums is 10.
fmt.Println(maximumSumScore([]int{4,3,-2,5})) // 10
// Example 2:
// Input: nums = [-3,-5]
// Output: -3
// Explanation:
// The sum score at index 0 is max(-3, -3 + -5) = max(-3, -8) = -3.
// The sum score at index 1 is max(-3 + -5, -5) = max(-8, -5) = -5.
// The maximum sum score of nums is -3.
fmt.Println(maximumSumScore([]int{-3,-5})) // -3
fmt.Println(maximumSumScore1([]int{4,3,-2,5})) // 10
fmt.Println(maximumSumScore1([]int{-3,-5})) // -3
}